5 * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <sys/socket.h>
29 #include <arpa/inet.h>
32 #include <linux/netlink.h>
33 #include <linux/rtnetlink.h>
39 static inline void print_inet(struct rtattr *attr, const char *name, int family)
41 if (family == AF_INET) {
43 addr = *((struct in_addr *) RTA_DATA(attr));
44 DBG(" attr %s (len %jd) %s\n",
45 name, RTA_PAYLOAD(attr), inet_ntoa(addr));
47 DBG(" attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
50 static inline void print_char(struct rtattr *attr, const char *name)
52 DBG(" attr %s (len %jd) %s\n", name, RTA_PAYLOAD(attr),
53 (char *) RTA_DATA(attr));
56 static inline void print_byte(struct rtattr *attr, const char *name)
58 DBG(" attr %s (len %jd) 0x%02x\n", name, RTA_PAYLOAD(attr),
59 *((unsigned char *) RTA_DATA(attr)));
62 static inline void print_attr(struct rtattr *attr, const char *name)
65 DBG(" attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
67 DBG(" attr %d (len %jd)\n",
68 attr->rta_type, RTA_PAYLOAD(attr));
71 static void rtnl_link(struct nlmsghdr *hdr)
73 struct ifinfomsg *msg;
77 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
78 bytes = IFLA_PAYLOAD(hdr);
80 DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
82 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
83 attr = RTA_NEXT(attr, bytes)) {
84 switch (attr->rta_type) {
86 print_attr(attr, "address");
89 print_attr(attr, "broadcast");
92 print_char(attr, "ifname");
95 print_attr(attr, "mtu");
98 print_attr(attr, "link");
101 print_attr(attr, "qdisc");
104 print_attr(attr, "stats");
107 print_attr(attr, "cost");
110 print_attr(attr, "priority");
113 print_attr(attr, "master");
116 print_attr(attr, "wireless");
119 print_attr(attr, "protinfo");
122 print_attr(attr, "txqlen");
125 print_attr(attr, "map");
128 print_attr(attr, "weight");
131 print_byte(attr, "operstate");
134 print_byte(attr, "linkmode");
137 print_attr(attr, NULL);
143 static void rtnl_addr(struct nlmsghdr *hdr)
145 struct ifaddrmsg *msg;
149 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
150 bytes = IFA_PAYLOAD(hdr);
152 DBG("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
154 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
155 attr = RTA_NEXT(attr, bytes)) {
156 switch (attr->rta_type) {
158 print_inet(attr, "address", msg->ifa_family);
161 print_inet(attr, "local", msg->ifa_family);
164 print_char(attr, "label");
167 print_inet(attr, "broadcast", msg->ifa_family);
170 print_attr(attr, "anycast");
173 print_attr(attr, "cacheinfo");
176 print_attr(attr, "multicast");
179 print_attr(attr, NULL);
185 static void rtnl_route(struct nlmsghdr *hdr)
191 msg = (struct rtmsg *) NLMSG_DATA(hdr);
192 bytes = RTM_PAYLOAD(hdr);
194 DBG("rtm_family %d rtm_flags 0x%04x", msg->rtm_family, msg->rtm_flags);
196 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
197 attr = RTA_NEXT(attr, bytes)) {
198 switch (attr->rta_type) {
200 print_inet(attr, "dst", msg->rtm_family);
203 print_inet(attr, "src", msg->rtm_family);
206 print_char(attr, "iif");
209 print_attr(attr, "oif");
212 print_inet(attr, "gateway", msg->rtm_family);
215 print_attr(attr, "priority");
218 print_inet(attr, "prefsrc", msg->rtm_family);
221 print_attr(attr, "metrics");
224 print_attr(attr, "table");
227 print_attr(attr, NULL);
233 static void rtnl_message(void *buf, size_t len)
235 DBG("buf %p len %zd", buf, len);
238 struct nlmsghdr *hdr = buf;
239 struct nlmsgerr *err;
241 if (!NLMSG_OK(hdr, len))
244 DBG("len %d type %d flags 0x%04x seq %d",
245 hdr->nlmsg_len, hdr->nlmsg_type,
246 hdr->nlmsg_flags, hdr->nlmsg_seq);
248 switch (hdr->nlmsg_type) {
253 err = NLMSG_DATA(hdr);
254 DBG("ERROR %d (%s)", -err->error,
255 strerror(-err->error));
288 DBG("type %d", hdr->nlmsg_type);
292 len -= hdr->nlmsg_len;
293 buf += hdr->nlmsg_len;
297 static gboolean netlink_event(GIOChannel *chan,
298 GIOCondition cond, gpointer data)
300 unsigned char buf[256];
304 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
307 memset(buf, 0, sizeof(buf));
309 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
311 if (err == G_IO_ERROR_AGAIN)
316 rtnl_message(buf, len);
321 static GIOChannel *channel = NULL;
323 int __connman_rtnl_send(const void *buf, size_t len)
325 struct sockaddr_nl addr;
328 DBG("buf %p len %zd", buf, len);
330 sk = g_io_channel_unix_get_fd(channel);
332 memset(&addr, 0, sizeof(addr));
333 addr.nl_family = AF_NETLINK;
335 return sendto(sk, buf, len, 0,
336 (struct sockaddr *) &addr, sizeof(addr));
339 int __connman_rtnl_init(void)
341 struct sockaddr_nl addr;
346 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
350 memset(&addr, 0, sizeof(addr));
351 addr.nl_family = AF_NETLINK;
352 //addr.nl_groups = RTMGRP_LINK;
353 //addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
354 //addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
356 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
361 channel = g_io_channel_unix_new(sk);
362 g_io_channel_set_close_on_unref(channel, TRUE);
364 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
365 netlink_event, NULL);
370 void __connman_rtnl_cleanup(void)
374 g_io_channel_shutdown(channel, TRUE, NULL);
375 g_io_channel_unref(channel);