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 void process_link_flags(int index, short flags)
131 DBG("idex %d", index);
133 g_static_rw_lock_reader_lock(&rtnl_lock);
135 for (list = rtnl_list; list; list = list->next) {
136 struct connman_rtnl *rtnl = list->data;
138 if (rtnl->link_flags)
139 rtnl->link_flags(index, flags);
142 g_static_rw_lock_reader_unlock(&rtnl_lock);
145 static inline void print_inet(struct rtattr *attr, const char *name, int family)
147 if (family == AF_INET) {
149 addr = *((struct in_addr *) RTA_DATA(attr));
150 DBG(" attr %s (len %jd) %s\n",
151 name, RTA_PAYLOAD(attr), inet_ntoa(addr));
153 DBG(" attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
156 static inline void print_char(struct rtattr *attr, const char *name)
158 DBG(" attr %s (len %jd) %s\n", name, RTA_PAYLOAD(attr),
159 (char *) RTA_DATA(attr));
162 static inline void print_byte(struct rtattr *attr, const char *name)
164 DBG(" attr %s (len %jd) 0x%02x\n", name, RTA_PAYLOAD(attr),
165 *((unsigned char *) RTA_DATA(attr)));
168 static inline void print_attr(struct rtattr *attr, const char *name)
171 DBG(" attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
173 DBG(" attr %d (len %jd)\n",
174 attr->rta_type, RTA_PAYLOAD(attr));
177 static void rtnl_link(struct nlmsghdr *hdr)
179 struct ifinfomsg *msg;
183 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
184 bytes = IFLA_PAYLOAD(hdr);
186 DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
188 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
189 attr = RTA_NEXT(attr, bytes)) {
190 switch (attr->rta_type) {
192 print_attr(attr, "address");
195 print_attr(attr, "broadcast");
198 print_char(attr, "ifname");
201 print_attr(attr, "mtu");
204 print_attr(attr, "link");
207 print_attr(attr, "qdisc");
210 print_attr(attr, "stats");
213 print_attr(attr, "cost");
216 print_attr(attr, "priority");
219 print_attr(attr, "master");
222 print_attr(attr, "wireless");
225 print_attr(attr, "protinfo");
228 print_attr(attr, "txqlen");
231 print_attr(attr, "map");
234 print_attr(attr, "weight");
237 print_byte(attr, "operstate");
240 print_byte(attr, "linkmode");
243 print_attr(attr, NULL);
248 process_link_flags(msg->ifi_index, msg->ifi_flags);
251 static void rtnl_newlink(struct nlmsghdr *hdr)
253 struct ifinfomsg *msg;
255 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
257 DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
259 process_newlink(msg->ifi_type, msg->ifi_index,
260 msg->ifi_flags, msg->ifi_change);
265 static void rtnl_dellink(struct nlmsghdr *hdr)
267 struct ifinfomsg *msg;
269 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
271 DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
273 process_dellink(msg->ifi_type, msg->ifi_index,
274 msg->ifi_flags, msg->ifi_change);
279 static void rtnl_addr(struct nlmsghdr *hdr)
281 struct ifaddrmsg *msg;
285 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
286 bytes = IFA_PAYLOAD(hdr);
288 DBG("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
290 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
291 attr = RTA_NEXT(attr, bytes)) {
292 switch (attr->rta_type) {
294 print_inet(attr, "address", msg->ifa_family);
297 print_inet(attr, "local", msg->ifa_family);
300 print_char(attr, "label");
303 print_inet(attr, "broadcast", msg->ifa_family);
306 print_attr(attr, "anycast");
309 print_attr(attr, "cacheinfo");
312 print_attr(attr, "multicast");
315 print_attr(attr, NULL);
321 static void rtnl_route(struct nlmsghdr *hdr)
327 msg = (struct rtmsg *) NLMSG_DATA(hdr);
328 bytes = RTM_PAYLOAD(hdr);
330 DBG("rtm_family %d rtm_flags 0x%04x", msg->rtm_family, msg->rtm_flags);
332 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
333 attr = RTA_NEXT(attr, bytes)) {
334 switch (attr->rta_type) {
336 print_inet(attr, "dst", msg->rtm_family);
339 print_inet(attr, "src", msg->rtm_family);
342 print_char(attr, "iif");
345 print_attr(attr, "oif");
348 print_inet(attr, "gateway", msg->rtm_family);
351 print_attr(attr, "priority");
354 print_inet(attr, "prefsrc", msg->rtm_family);
357 print_attr(attr, "metrics");
360 print_attr(attr, "table");
363 print_attr(attr, NULL);
369 static void rtnl_message(void *buf, size_t len)
371 DBG("buf %p len %zd", buf, len);
374 struct nlmsghdr *hdr = buf;
375 struct nlmsgerr *err;
377 if (!NLMSG_OK(hdr, len))
380 DBG("len %d type %d flags 0x%04x seq %d",
381 hdr->nlmsg_len, hdr->nlmsg_type,
382 hdr->nlmsg_flags, hdr->nlmsg_seq);
384 switch (hdr->nlmsg_type) {
389 err = NLMSG_DATA(hdr);
390 DBG("ERROR %d (%s)", -err->error,
391 strerror(-err->error));
424 DBG("type %d", hdr->nlmsg_type);
428 len -= hdr->nlmsg_len;
429 buf += hdr->nlmsg_len;
433 static gboolean netlink_event(GIOChannel *chan,
434 GIOCondition cond, gpointer data)
436 unsigned char buf[256];
440 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
443 memset(buf, 0, sizeof(buf));
445 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
447 if (err == G_IO_ERROR_AGAIN)
452 rtnl_message(buf, len);
457 static GIOChannel *channel = NULL;
459 int __connman_rtnl_send(const void *buf, size_t len)
461 struct sockaddr_nl addr;
464 DBG("buf %p len %zd", buf, len);
466 sk = g_io_channel_unix_get_fd(channel);
468 memset(&addr, 0, sizeof(addr));
469 addr.nl_family = AF_NETLINK;
471 return sendto(sk, buf, len, 0,
472 (struct sockaddr *) &addr, sizeof(addr));
475 int connman_rtnl_send_getlink(void)
484 memset(&req, 0, sizeof(req));
485 req.hdr.nlmsg_len = sizeof(req.hdr) + sizeof(req.msg);
486 req.hdr.nlmsg_type = RTM_GETLINK;
487 req.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
488 req.hdr.nlmsg_pid = 0;
489 req.hdr.nlmsg_seq = 42;
490 req.msg.rtgen_family = AF_INET;
492 __connman_rtnl_send(&req, sizeof(req));
495 int __connman_rtnl_init(void)
497 struct sockaddr_nl addr;
502 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
506 memset(&addr, 0, sizeof(addr));
507 addr.nl_family = AF_NETLINK;
508 addr.nl_groups = RTMGRP_LINK;
509 //addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
510 //addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
512 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
517 channel = g_io_channel_unix_new(sk);
518 g_io_channel_set_close_on_unref(channel, TRUE);
520 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
521 netlink_event, NULL);
526 void __connman_rtnl_cleanup(void)
530 g_io_channel_shutdown(channel, TRUE, NULL);
531 g_io_channel_unref(channel);