2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "helper-nl.h"
20 #include <linux/rtnetlink.h>
25 * create_netlink(): Create netlink socket and returns it.
26 * Returns: Created socket on success and -1 on failure.
28 API int create_netlink(int protocol, uint32_t groups)
31 * TODO it's one socket, in future make set of sockets
32 * unique for protocol and groups
35 sock = socket(PF_NETLINK, SOCK_RAW, protocol);
37 return -EINVAL; //LCOV_EXCL_LINE
39 struct sockaddr_nl src_addr = { 0, };
41 src_addr.nl_family = AF_NETLINK;
42 src_addr.nl_groups = groups;
44 if (bind(sock, (struct sockaddr *)&src_addr, sizeof(src_addr)) < 0) {
45 close(sock); //LCOV_EXCL_LINE
46 return -1; //LCOV_EXCL_LINE
52 void fill_attribute_list(struct rtattr **atb, const int max_len,
53 struct rtattr *rt_na, int rt_len)
56 while (RTA_OK(rt_na, rt_len)) {
57 if (rt_na->rta_type <= max_len)
58 atb[rt_na->rta_type] = rt_na;
60 rt_na = RTA_NEXT(rt_na, rt_len);
67 /* read netlink message from socket
68 * return opaque pointer to genl structure
70 API int read_netlink(int sock, void *buf, size_t len)
73 struct sockaddr_nl addr;
80 .msg_namelen = sizeof(struct sockaddr_nl),
87 ret = recvmsg(sock, &msg, 0);
89 return ret; //LCOV_EXCL_LINE
91 if (msg.msg_flags & MSG_TRUNC) {
92 errno = ENOSPC; //LCOV_EXCL_LINE
93 return -1; //LCOV_EXCL_LINE
96 if (msg.msg_namelen != sizeof(struct sockaddr_nl)) {
97 errno = EINVAL; //LCOV_EXCL_LINE
98 return -1; //LCOV_EXCL_LINE