4 * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 * @desc Common netlink helper function
24 * Created on: Jun 25, 2012
27 #include "nl-helper.h"
31 #include <linux/rtnetlink.h>
36 * create_netlink(): Create netlink socket and returns it.
37 * Returns: Created socket on success and -1 on failure.
39 int create_netlink(int protocol, uint32_t groups)
42 * TODO it's one socket, in future make set of sockets
43 * unique for protocol and groups
46 sock = socket(PF_NETLINK, SOCK_RAW, protocol);
50 struct sockaddr_nl src_addr = { 0, };
52 src_addr.nl_family = AF_NETLINK;
53 src_addr.nl_groups = groups;
55 if (bind(sock, (struct sockaddr *)&src_addr, sizeof(src_addr)) < 0) {
63 void fill_attribute_list(struct rtattr **atb, const int max_len,
64 struct rtattr *rt_na, int rt_len)
67 while (RTA_OK(rt_na, rt_len)) {
68 if (rt_na->rta_type <= max_len)
69 atb[rt_na->rta_type] = rt_na;
71 rt_na = RTA_NEXT(rt_na, rt_len);
78 /* read netlink message from socket
79 * return opaque pointer to genl structure */
81 #ifdef CONFIG_DATAUSAGE_NFACCT
82 int read_netlink(int sock, void *buf, size_t len)
85 struct sockaddr_nl addr;
92 .msg_namelen = sizeof(struct sockaddr_nl),
99 ret = recvmsg(sock, &msg, 0);
103 if (msg.msg_flags & MSG_TRUNC) {
108 if (msg.msg_namelen != sizeof(struct sockaddr_nl)) {
116 int read_netlink(int sock, void *buf, size_t len)
119 struct genl *ans = buf;
121 ans_len = recv(sock, ans, len, MSG_DONTWAIT);
125 if (ans->n.nlmsg_type == NLMSG_ERROR)
128 if (!NLMSG_OK((&ans->n), ans_len))