5 * Copyright (C) 2007-2009 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>
42 connman_rtnl_link_cb_t newlink;
46 static GSList *watch_list = NULL;
47 static unsigned int watch_id = 0;
50 * connman_rtnl_add_newlink_watch:
51 * @index: network device index
52 * @callback: callback function
53 * @user_data: callback data;
55 * Add a new RTNL watch for newlink events
57 * Returns: %0 on failure and a unique id on success
59 unsigned int connman_rtnl_add_newlink_watch(int index,
60 connman_rtnl_link_cb_t callback, void *user_data)
62 struct watch_data *watch;
64 watch = g_try_new0(struct watch_data, 1);
68 watch->id = ++watch_id;
71 watch->newlink = callback;
72 watch->user_data = user_data;
74 watch_list = g_slist_prepend(watch_list, watch);
76 DBG("id %d", watch->id);
82 * connman_rtnl_remove_watch:
83 * @id: watch identifier
85 * Remove the RTNL watch for the identifier
87 void connman_rtnl_remove_watch(unsigned int id)
96 for (list = watch_list; list; list = list->next) {
97 struct watch_data *watch = list->data;
99 if (watch->id == id) {
100 watch_list = g_slist_remove(watch_list, watch);
107 static GSList *rtnl_list = NULL;
109 static gint compare_priority(gconstpointer a, gconstpointer b)
111 const struct connman_rtnl *rtnl1 = a;
112 const struct connman_rtnl *rtnl2 = b;
114 return rtnl2->priority - rtnl1->priority;
118 * connman_rtnl_register:
121 * Register a new RTNL module
123 * Returns: %0 on success
125 int connman_rtnl_register(struct connman_rtnl *rtnl)
127 DBG("rtnl %p name %s", rtnl, rtnl->name);
129 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
136 * connman_rtnl_unregister:
139 * Remove a previously registered RTNL module
141 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
143 DBG("rtnl %p name %s", rtnl, rtnl->name);
145 rtnl_list = g_slist_remove(rtnl_list, rtnl);
148 static void process_newlink(unsigned short type, int index,
149 unsigned flags, unsigned change)
153 for (list = rtnl_list; list; list = list->next) {
154 struct connman_rtnl *rtnl = list->data;
157 rtnl->newlink(type, index, flags, change);
160 for (list = watch_list; list; list = list->next) {
161 struct watch_data *watch = list->data;
163 if (watch->index != index)
167 watch->newlink(flags, change, watch->user_data);
171 static void process_dellink(unsigned short type, int index,
172 unsigned flags, unsigned change)
176 for (list = rtnl_list; list; list = list->next) {
177 struct connman_rtnl *rtnl = list->data;
180 rtnl->dellink(type, index, flags, change);
184 static char *extract_gateway(struct rtmsg *msg, int bytes, int *index)
186 char *gateway = NULL;
190 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
191 attr = RTA_NEXT(attr, bytes)) {
192 switch (attr->rta_type) {
194 addr = *((struct in_addr *) RTA_DATA(attr));
196 gateway = g_strdup(inet_ntoa(addr));
199 *index = *((int *) RTA_DATA(attr));
207 static void process_newgateway(struct rtmsg *msg, int bytes)
213 gateway = extract_gateway(msg, bytes, &index);
214 if (gateway == NULL || index < 0)
217 for (list = rtnl_list; list; list = list->next) {
218 struct connman_rtnl *rtnl = list->data;
220 if (rtnl->newgateway)
221 rtnl->newgateway(index, gateway);
227 static void process_delgateway(struct rtmsg *msg, int bytes)
233 gateway = extract_gateway(msg, bytes, &index);
234 if (gateway == NULL || index < 0)
237 for (list = rtnl_list; list; list = list->next) {
238 struct connman_rtnl *rtnl = list->data;
240 if (rtnl->delgateway)
241 rtnl->delgateway(index, gateway);
247 static inline void print_inet(struct rtattr *attr, const char *name, int family)
249 if (family == AF_INET) {
251 addr = *((struct in_addr *) RTA_DATA(attr));
252 DBG(" attr %s (len %jd) %s\n",
253 name, RTA_PAYLOAD(attr), inet_ntoa(addr));
255 DBG(" attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
258 static inline void print_char(struct rtattr *attr, const char *name)
260 DBG(" attr %s (len %jd) %s\n", name, RTA_PAYLOAD(attr),
261 (char *) RTA_DATA(attr));
264 static inline void print_byte(struct rtattr *attr, const char *name)
266 DBG(" attr %s (len %jd) 0x%02x\n", name, RTA_PAYLOAD(attr),
267 *((unsigned char *) RTA_DATA(attr)));
270 static inline void print_attr(struct rtattr *attr, const char *name)
273 DBG(" attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
275 DBG(" attr %d (len %jd)\n",
276 attr->rta_type, RTA_PAYLOAD(attr));
279 static void rtnl_link(struct nlmsghdr *hdr)
282 struct ifinfomsg *msg;
286 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
287 bytes = IFLA_PAYLOAD(hdr);
289 DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
291 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
292 attr = RTA_NEXT(attr, bytes)) {
293 switch (attr->rta_type) {
295 print_attr(attr, "address");
298 print_attr(attr, "broadcast");
301 print_char(attr, "ifname");
304 print_attr(attr, "mtu");
307 print_attr(attr, "link");
310 print_attr(attr, "qdisc");
313 print_attr(attr, "stats");
316 print_attr(attr, "cost");
319 print_attr(attr, "priority");
322 print_attr(attr, "master");
325 print_attr(attr, "wireless");
328 print_attr(attr, "protinfo");
331 print_attr(attr, "txqlen");
334 print_attr(attr, "map");
337 print_attr(attr, "weight");
340 print_byte(attr, "operstate");
343 print_byte(attr, "linkmode");
346 print_attr(attr, NULL);
353 static void rtnl_newlink(struct nlmsghdr *hdr)
355 struct ifinfomsg *msg;
357 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
359 DBG("ifi_type %d ifi_index %d ifi_flags 0x%04x ifi_change 0x%04x",
360 msg->ifi_type, msg->ifi_index,
361 msg->ifi_flags, msg->ifi_change);
363 process_newlink(msg->ifi_type, msg->ifi_index,
364 msg->ifi_flags, msg->ifi_change);
369 static void rtnl_dellink(struct nlmsghdr *hdr)
371 struct ifinfomsg *msg;
373 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
375 DBG("ifi_type %d ifi_index %d ifi_flags 0x%04x ifi_change 0x%04x",
376 msg->ifi_type, msg->ifi_index,
377 msg->ifi_flags, msg->ifi_change);
379 process_dellink(msg->ifi_type, msg->ifi_index,
380 msg->ifi_flags, msg->ifi_change);
385 static void rtnl_addr(struct nlmsghdr *hdr)
387 struct ifaddrmsg *msg;
391 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
392 bytes = IFA_PAYLOAD(hdr);
394 DBG("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
396 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
397 attr = RTA_NEXT(attr, bytes)) {
398 switch (attr->rta_type) {
400 print_inet(attr, "address", msg->ifa_family);
403 print_inet(attr, "local", msg->ifa_family);
406 print_char(attr, "label");
409 print_inet(attr, "broadcast", msg->ifa_family);
412 print_attr(attr, "anycast");
415 print_attr(attr, "cacheinfo");
418 print_attr(attr, "multicast");
421 print_attr(attr, NULL);
427 static void rtnl_route(struct nlmsghdr *hdr)
434 msg = (struct rtmsg *) NLMSG_DATA(hdr);
435 bytes = RTM_PAYLOAD(hdr);
437 DBG("rtm_family %d rtm_flags 0x%04x", msg->rtm_family, msg->rtm_flags);
439 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
440 attr = RTA_NEXT(attr, bytes)) {
441 switch (attr->rta_type) {
443 print_inet(attr, "dst", msg->rtm_family);
446 print_inet(attr, "src", msg->rtm_family);
449 print_char(attr, "iif");
452 print_attr(attr, "oif");
455 print_inet(attr, "gateway", msg->rtm_family);
458 print_attr(attr, "priority");
461 print_inet(attr, "prefsrc", msg->rtm_family);
464 print_attr(attr, "metrics");
467 print_attr(attr, "table");
470 print_attr(attr, NULL);
477 static void rtnl_newroute(struct nlmsghdr *hdr)
481 msg = (struct rtmsg *) NLMSG_DATA(hdr);
483 if (msg->rtm_type == RTN_UNICAST && msg->rtm_table == RT_TABLE_MAIN &&
484 msg->rtm_scope == RT_SCOPE_UNIVERSE) {
485 DBG("rtm_table %d rtm_scope %d rtm_type %d rtm_flags 0x%04x",
486 msg->rtm_table, msg->rtm_scope,
487 msg->rtm_type, msg->rtm_flags);
488 process_newgateway(msg, RTM_PAYLOAD(hdr));
494 static void rtnl_delroute(struct nlmsghdr *hdr)
498 msg = (struct rtmsg *) NLMSG_DATA(hdr);
500 if (msg->rtm_type == RTN_UNICAST && msg->rtm_table == RT_TABLE_MAIN &&
501 msg->rtm_scope == RT_SCOPE_UNIVERSE) {
502 DBG("rtm_table %d rtm_scope %d rtm_type %d rtm_flags 0x%04x",
503 msg->rtm_table, msg->rtm_scope,
504 msg->rtm_type, msg->rtm_flags);
505 process_delgateway(msg, RTM_PAYLOAD(hdr));
511 static const char *type2string(uint16_t type)
543 static GIOChannel *channel = NULL;
545 struct rtnl_request {
549 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
551 static GSList *request_list = NULL;
552 static guint32 request_seq = 0;
554 static struct rtnl_request *find_request(guint32 seq)
558 for (list = request_list; list; list = list->next) {
559 struct rtnl_request *req = list->data;
561 if (req->hdr.nlmsg_seq == seq)
568 static int send_request(struct rtnl_request *req)
570 struct sockaddr_nl addr;
573 DBG("%s len %d type %d flags 0x%04x seq %d",
574 type2string(req->hdr.nlmsg_type),
575 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
576 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
578 sk = g_io_channel_unix_get_fd(channel);
580 memset(&addr, 0, sizeof(addr));
581 addr.nl_family = AF_NETLINK;
583 return sendto(sk, req, req->hdr.nlmsg_len, 0,
584 (struct sockaddr *) &addr, sizeof(addr));
587 static int queue_request(struct rtnl_request *req)
589 request_list = g_slist_append(request_list, req);
591 if (g_slist_length(request_list) > 1)
594 return send_request(req);
597 static int process_response(guint32 seq)
599 struct rtnl_request *req;
603 req = find_request(seq);
605 request_list = g_slist_remove(request_list, req);
609 req = g_slist_nth_data(request_list, 0);
613 return send_request(req);
616 static void rtnl_message(void *buf, size_t len)
618 DBG("buf %p len %zd", buf, len);
621 struct nlmsghdr *hdr = buf;
622 struct nlmsgerr *err;
624 if (!NLMSG_OK(hdr, len))
627 DBG("%s len %d type %d flags 0x%04x seq %d",
628 type2string(hdr->nlmsg_type),
629 hdr->nlmsg_len, hdr->nlmsg_type,
630 hdr->nlmsg_flags, hdr->nlmsg_seq);
632 switch (hdr->nlmsg_type) {
637 process_response(hdr->nlmsg_seq);
640 err = NLMSG_DATA(hdr);
641 DBG("error %d (%s)", -err->error,
642 strerror(-err->error));
662 len -= hdr->nlmsg_len;
663 buf += hdr->nlmsg_len;
667 static gboolean netlink_event(GIOChannel *chan,
668 GIOCondition cond, gpointer data)
670 unsigned char buf[4096];
674 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
677 memset(buf, 0, sizeof(buf));
679 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
681 if (err == G_IO_ERROR_AGAIN)
686 rtnl_message(buf, len);
691 int connman_rtnl_send_getlink(void)
693 struct rtnl_request *req;
697 req = g_try_malloc0(RTNL_REQUEST_SIZE);
701 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
702 req->hdr.nlmsg_type = RTM_GETLINK;
703 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
704 req->hdr.nlmsg_pid = 0;
705 req->hdr.nlmsg_seq = request_seq++;
706 req->msg.rtgen_family = AF_INET;
708 return queue_request(req);
711 int connman_rtnl_send_getroute(void)
713 struct rtnl_request *req;
717 req = g_try_malloc0(RTNL_REQUEST_SIZE);
721 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
722 req->hdr.nlmsg_type = RTM_GETROUTE;
723 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
724 req->hdr.nlmsg_pid = 0;
725 req->hdr.nlmsg_seq = request_seq++;
726 req->msg.rtgen_family = AF_INET;
728 return queue_request(req);
731 int __connman_rtnl_init(void)
733 struct sockaddr_nl addr;
738 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
742 memset(&addr, 0, sizeof(addr));
743 addr.nl_family = AF_NETLINK;
744 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_ROUTE;
745 //addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
746 //addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
748 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
753 channel = g_io_channel_unix_new(sk);
754 g_io_channel_set_close_on_unref(channel, TRUE);
756 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
757 netlink_event, NULL);
762 void __connman_rtnl_cleanup(void)
768 for (list = watch_list; list; list = list->next) {
769 struct watch_data *watch = list->data;
771 DBG("removing watch %d", watch->id);
777 g_slist_free(watch_list);
780 for (list = request_list; list; list = list->next) {
781 struct rtnl_request *req = list->data;
783 DBG("%s len %d type %d flags 0x%04x seq %d",
784 type2string(req->hdr.nlmsg_type),
785 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
786 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
792 g_slist_free(request_list);
795 g_io_channel_shutdown(channel, TRUE, NULL);
796 g_io_channel_unref(channel);