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 GStaticRWLock rtnl_lock = G_STATIC_RW_LOCK_INIT;
40 static GSList *rtnl_list = NULL;
42 static gint compare_priority(gconstpointer a, gconstpointer b)
44 const struct connman_rtnl *rtnl1 = a;
45 const struct connman_rtnl *rtnl2 = b;
47 return rtnl2->priority - rtnl1->priority;
51 * connman_rtnl_register:
54 * Register a new RTNL module
56 * Returns: %0 on success
58 int connman_rtnl_register(struct connman_rtnl *rtnl)
60 DBG("rtnl %p name %s", rtnl, rtnl->name);
62 g_static_rw_lock_writer_lock(&rtnl_lock);
64 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
67 g_static_rw_lock_writer_unlock(&rtnl_lock);
73 * connman_rtnl_unregister:
76 * Remove a previously registered RTNL module
78 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
80 DBG("rtnl %p name %s", rtnl, rtnl->name);
82 g_static_rw_lock_writer_lock(&rtnl_lock);
84 rtnl_list = g_slist_remove(rtnl_list, rtnl);
86 g_static_rw_lock_writer_unlock(&rtnl_lock);
89 static void process_newlink(unsigned short type, int index,
90 unsigned flags, unsigned change)
94 DBG("idex %d", index);
96 g_static_rw_lock_reader_lock(&rtnl_lock);
98 for (list = rtnl_list; list; list = list->next) {
99 struct connman_rtnl *rtnl = list->data;
102 rtnl->newlink(type, index, flags, change);
105 g_static_rw_lock_reader_unlock(&rtnl_lock);
108 static void process_dellink(unsigned short type, int index,
109 unsigned flags, unsigned change)
113 DBG("idex %d", index);
115 g_static_rw_lock_reader_lock(&rtnl_lock);
117 for (list = rtnl_list; list; list = list->next) {
118 struct connman_rtnl *rtnl = list->data;
121 rtnl->dellink(type, index, flags, change);
124 g_static_rw_lock_reader_unlock(&rtnl_lock);
127 static inline void print_inet(struct rtattr *attr, const char *name, int family)
129 if (family == AF_INET) {
131 addr = *((struct in_addr *) RTA_DATA(attr));
132 DBG(" attr %s (len %jd) %s\n",
133 name, RTA_PAYLOAD(attr), inet_ntoa(addr));
135 DBG(" attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
138 static inline void print_char(struct rtattr *attr, const char *name)
140 DBG(" attr %s (len %jd) %s\n", name, RTA_PAYLOAD(attr),
141 (char *) RTA_DATA(attr));
144 static inline void print_byte(struct rtattr *attr, const char *name)
146 DBG(" attr %s (len %jd) 0x%02x\n", name, RTA_PAYLOAD(attr),
147 *((unsigned char *) RTA_DATA(attr)));
150 static inline void print_attr(struct rtattr *attr, const char *name)
153 DBG(" attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
155 DBG(" attr %d (len %jd)\n",
156 attr->rta_type, RTA_PAYLOAD(attr));
159 static void rtnl_link(struct nlmsghdr *hdr)
162 struct ifinfomsg *msg;
166 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
167 bytes = IFLA_PAYLOAD(hdr);
169 DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
171 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
172 attr = RTA_NEXT(attr, bytes)) {
173 switch (attr->rta_type) {
175 print_attr(attr, "address");
178 print_attr(attr, "broadcast");
181 print_char(attr, "ifname");
184 print_attr(attr, "mtu");
187 print_attr(attr, "link");
190 print_attr(attr, "qdisc");
193 print_attr(attr, "stats");
196 print_attr(attr, "cost");
199 print_attr(attr, "priority");
202 print_attr(attr, "master");
205 print_attr(attr, "wireless");
208 print_attr(attr, "protinfo");
211 print_attr(attr, "txqlen");
214 print_attr(attr, "map");
217 print_attr(attr, "weight");
220 print_byte(attr, "operstate");
223 print_byte(attr, "linkmode");
226 print_attr(attr, NULL);
233 static void rtnl_newlink(struct nlmsghdr *hdr)
235 struct ifinfomsg *msg;
237 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
239 DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
241 process_newlink(msg->ifi_type, msg->ifi_index,
242 msg->ifi_flags, msg->ifi_change);
247 static void rtnl_dellink(struct nlmsghdr *hdr)
249 struct ifinfomsg *msg;
251 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
253 DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
255 process_dellink(msg->ifi_type, msg->ifi_index,
256 msg->ifi_flags, msg->ifi_change);
261 static void rtnl_addr(struct nlmsghdr *hdr)
263 struct ifaddrmsg *msg;
267 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
268 bytes = IFA_PAYLOAD(hdr);
270 DBG("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
272 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
273 attr = RTA_NEXT(attr, bytes)) {
274 switch (attr->rta_type) {
276 print_inet(attr, "address", msg->ifa_family);
279 print_inet(attr, "local", msg->ifa_family);
282 print_char(attr, "label");
285 print_inet(attr, "broadcast", msg->ifa_family);
288 print_attr(attr, "anycast");
291 print_attr(attr, "cacheinfo");
294 print_attr(attr, "multicast");
297 print_attr(attr, NULL);
303 static void rtnl_route(struct nlmsghdr *hdr)
309 msg = (struct rtmsg *) NLMSG_DATA(hdr);
310 bytes = RTM_PAYLOAD(hdr);
312 DBG("rtm_family %d rtm_flags 0x%04x", msg->rtm_family, msg->rtm_flags);
314 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
315 attr = RTA_NEXT(attr, bytes)) {
316 switch (attr->rta_type) {
318 print_inet(attr, "dst", msg->rtm_family);
321 print_inet(attr, "src", msg->rtm_family);
324 print_char(attr, "iif");
327 print_attr(attr, "oif");
330 print_inet(attr, "gateway", msg->rtm_family);
333 print_attr(attr, "priority");
336 print_inet(attr, "prefsrc", msg->rtm_family);
339 print_attr(attr, "metrics");
342 print_attr(attr, "table");
345 print_attr(attr, NULL);
351 static void rtnl_message(void *buf, size_t len)
353 DBG("buf %p len %zd", buf, len);
356 struct nlmsghdr *hdr = buf;
357 struct nlmsgerr *err;
359 if (!NLMSG_OK(hdr, len))
362 DBG("len %d type %d flags 0x%04x seq %d",
363 hdr->nlmsg_len, hdr->nlmsg_type,
364 hdr->nlmsg_flags, hdr->nlmsg_seq);
366 switch (hdr->nlmsg_type) {
371 err = NLMSG_DATA(hdr);
372 DBG("ERROR %d (%s)", -err->error,
373 strerror(-err->error));
406 DBG("type %d", hdr->nlmsg_type);
410 len -= hdr->nlmsg_len;
411 buf += hdr->nlmsg_len;
415 static gboolean netlink_event(GIOChannel *chan,
416 GIOCondition cond, gpointer data)
418 unsigned char buf[256];
422 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
425 memset(buf, 0, sizeof(buf));
427 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
429 if (err == G_IO_ERROR_AGAIN)
434 rtnl_message(buf, len);
439 static GIOChannel *channel = NULL;
441 int __connman_rtnl_send(const void *buf, size_t len)
443 struct sockaddr_nl addr;
446 DBG("buf %p len %zd", buf, len);
448 sk = g_io_channel_unix_get_fd(channel);
450 memset(&addr, 0, sizeof(addr));
451 addr.nl_family = AF_NETLINK;
453 return sendto(sk, buf, len, 0,
454 (struct sockaddr *) &addr, sizeof(addr));
457 int connman_rtnl_send_getlink(void)
466 memset(&req, 0, sizeof(req));
467 req.hdr.nlmsg_len = sizeof(req.hdr) + sizeof(req.msg);
468 req.hdr.nlmsg_type = RTM_GETLINK;
469 req.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
470 req.hdr.nlmsg_pid = 0;
471 req.hdr.nlmsg_seq = 42;
472 req.msg.rtgen_family = AF_INET;
474 return __connman_rtnl_send(&req, sizeof(req));
477 int __connman_rtnl_init(void)
479 struct sockaddr_nl addr;
484 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
488 memset(&addr, 0, sizeof(addr));
489 addr.nl_family = AF_NETLINK;
490 addr.nl_groups = RTMGRP_LINK;
491 //addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
492 //addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
494 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
499 channel = g_io_channel_unix_new(sk);
500 g_io_channel_set_close_on_unref(channel, TRUE);
502 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
503 netlink_event, NULL);
508 void __connman_rtnl_cleanup(void)
512 g_io_channel_shutdown(channel, TRUE, NULL);
513 g_io_channel_unref(channel);