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>
30 #include <netinet/ether.h>
31 #include <net/if_arp.h>
33 #include <linux/netlink.h>
34 #include <linux/rtnetlink.h>
40 #define print(arg...) do { } while (0)
41 //#define print(arg...) connman_info(arg)
46 connman_rtnl_operstate_cb_t operstate;
47 connman_rtnl_link_cb_t newlink;
51 static GSList *watch_list = NULL;
52 static unsigned int watch_id = 0;
55 * connman_rtnl_add_operstate_watch:
56 * @index: network device index
57 * @callback: callback function
58 * @user_data: callback data;
60 * Add a new RTNL watch for operation state events
62 * Returns: %0 on failure and a unique id on success
64 unsigned int connman_rtnl_add_operstate_watch(int index,
65 connman_rtnl_operstate_cb_t callback, void *user_data)
67 struct watch_data *watch;
69 watch = g_try_new0(struct watch_data, 1);
73 watch->id = ++watch_id;
76 watch->operstate = callback;
77 watch->user_data = user_data;
79 watch_list = g_slist_prepend(watch_list, watch);
81 DBG("id %d", watch->id);
84 unsigned char operstate = 0;
87 callback(operstate, user_data);
94 * connman_rtnl_add_newlink_watch:
95 * @index: network device index
96 * @callback: callback function
97 * @user_data: callback data;
99 * Add a new RTNL watch for newlink events
101 * Returns: %0 on failure and a unique id on success
103 unsigned int connman_rtnl_add_newlink_watch(int index,
104 connman_rtnl_link_cb_t callback, void *user_data)
106 struct watch_data *watch;
108 watch = g_try_new0(struct watch_data, 1);
112 watch->id = ++watch_id;
113 watch->index = index;
115 watch->newlink = callback;
116 watch->user_data = user_data;
118 watch_list = g_slist_prepend(watch_list, watch);
120 DBG("id %d", watch->id);
123 unsigned int flags = __connman_ipconfig_get_flags(index);
126 callback(flags, 0, user_data);
133 * connman_rtnl_remove_watch:
134 * @id: watch identifier
136 * Remove the RTNL watch for the identifier
138 void connman_rtnl_remove_watch(unsigned int id)
147 for (list = watch_list; list; list = list->next) {
148 struct watch_data *watch = list->data;
150 if (watch->id == id) {
151 watch_list = g_slist_remove(watch_list, watch);
158 static void trigger_rtnl(int index, void *user_data)
160 struct connman_rtnl *rtnl = user_data;
163 unsigned short type = __connman_ipconfig_get_type(index);
164 unsigned int flags = __connman_ipconfig_get_flags(index);
166 rtnl->newlink(type, index, flags, 0);
169 if (rtnl->newgateway) {
170 const char *gateway = __connman_ipconfig_get_gateway(index);
173 rtnl->newgateway(index, gateway);
177 static GSList *rtnl_list = NULL;
179 static gint compare_priority(gconstpointer a, gconstpointer b)
181 const struct connman_rtnl *rtnl1 = a;
182 const struct connman_rtnl *rtnl2 = b;
184 return rtnl2->priority - rtnl1->priority;
188 * connman_rtnl_register:
191 * Register a new RTNL module
193 * Returns: %0 on success
195 int connman_rtnl_register(struct connman_rtnl *rtnl)
197 DBG("rtnl %p name %s", rtnl, rtnl->name);
199 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
202 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
208 * connman_rtnl_unregister:
211 * Remove a previously registered RTNL module
213 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
215 DBG("rtnl %p name %s", rtnl, rtnl->name);
217 rtnl_list = g_slist_remove(rtnl_list, rtnl);
220 static const char *operstate2str(unsigned char operstate)
223 case IF_OPER_UNKNOWN:
225 case IF_OPER_NOTPRESENT:
226 return "NOT-PRESENT";
229 case IF_OPER_LOWERLAYERDOWN:
230 return "LOWER-LAYER-DOWN";
231 case IF_OPER_TESTING:
233 case IF_OPER_DORMANT:
242 static void extract_link(struct ifinfomsg *msg, int bytes,
243 const char **ifname, unsigned char *operstate)
247 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
248 attr = RTA_NEXT(attr, bytes)) {
249 switch (attr->rta_type) {
252 *ifname = RTA_DATA(attr);
255 if (operstate != NULL)
256 *operstate = *((unsigned char *) RTA_DATA(attr));
264 static void process_newlink(unsigned short type, int index, unsigned flags,
265 unsigned change, struct ifinfomsg *msg, int bytes)
267 unsigned char operstate = 0xff;
268 const char *ifname = NULL;
273 case ARPHRD_LOOPBACK:
275 __connman_ipconfig_newlink(index, type, flags);
279 extract_link(msg, bytes, &ifname, &operstate);
281 if (operstate != 0xff)
282 connman_info("%s {newlink} index %d operstate %d <%s>",
283 ifname, index, operstate,
284 operstate2str(operstate));
286 for (list = rtnl_list; list; list = list->next) {
287 struct connman_rtnl *rtnl = list->data;
290 rtnl->newlink(type, index, flags, change);
293 for (list = watch_list; list; list = list->next) {
294 struct watch_data *watch = list->data;
296 if (watch->index != index)
299 if (operstate != 0xff && watch->operstate)
300 watch->operstate(operstate, watch->user_data);
303 watch->newlink(flags, change, watch->user_data);
307 static void process_dellink(unsigned short type, int index, unsigned flags,
308 unsigned change, struct ifinfomsg *msg, int bytes)
310 unsigned char operstate = 0xff;
311 const char *ifname = NULL;
314 extract_link(msg, bytes, &ifname, &operstate);
316 if (operstate != 0xff)
317 connman_info("%s {dellink} index %d operstate %d <%s>",
318 ifname, index, operstate,
319 operstate2str(operstate));
321 for (list = watch_list; list; list = list->next) {
322 struct watch_data *watch = list->data;
324 if (watch->index != index)
327 if (operstate != 0xff && watch->operstate)
328 watch->operstate(operstate, watch->user_data);
331 for (list = rtnl_list; list; list = list->next) {
332 struct connman_rtnl *rtnl = list->data;
335 rtnl->dellink(type, index, flags, change);
340 case ARPHRD_LOOPBACK:
342 __connman_ipconfig_dellink(index);
347 static void extract_addr(struct ifaddrmsg *msg, int bytes,
349 struct in_addr *local,
350 struct in_addr *address,
351 struct in_addr *broadcast)
355 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
356 attr = RTA_NEXT(attr, bytes)) {
357 switch (attr->rta_type) {
360 *address = *((struct in_addr *) RTA_DATA(attr));
364 *local = *((struct in_addr *) RTA_DATA(attr));
367 if (broadcast != NULL)
368 *broadcast = *((struct in_addr *) RTA_DATA(attr));
372 *label = RTA_DATA(attr);
378 static void process_newaddr(unsigned char family, unsigned char prefixlen,
379 int index, struct ifaddrmsg *msg, int bytes)
381 struct in_addr address = { INADDR_ANY };
382 const char *label = NULL;
384 if (family != AF_INET)
387 extract_addr(msg, bytes, &label, &address, NULL, NULL);
389 __connman_ipconfig_newaddr(index, label,
390 prefixlen, inet_ntoa(address));
393 static void process_deladdr(unsigned char family, unsigned char prefixlen,
394 int index, struct ifaddrmsg *msg, int bytes)
396 struct in_addr address = { INADDR_ANY };
397 const char *label = NULL;
399 if (family != AF_INET)
402 extract_addr(msg, bytes, &label, &address, NULL, NULL);
404 __connman_ipconfig_deladdr(index, label,
405 prefixlen, inet_ntoa(address));
408 static void extract_route(struct rtmsg *msg, int bytes, int *index,
410 struct in_addr *gateway)
414 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
415 attr = RTA_NEXT(attr, bytes)) {
416 switch (attr->rta_type) {
419 *dst = *((struct in_addr *) RTA_DATA(attr));
423 *gateway = *((struct in_addr *) RTA_DATA(attr));
427 *index = *((int *) RTA_DATA(attr));
433 static void process_newroute(unsigned char family, unsigned char scope,
434 struct rtmsg *msg, int bytes)
437 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
438 char dststr[16], gatewaystr[16];
441 if (family != AF_INET)
444 extract_route(msg, bytes, &index, &dst, &gateway);
446 inet_ntop(family, &dst, dststr, sizeof(dststr));
447 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
449 __connman_ipconfig_newroute(index, scope, dststr, gatewaystr);
451 if (scope != RT_SCOPE_UNIVERSE || dst.s_addr != INADDR_ANY)
454 for (list = rtnl_list; list; list = list->next) {
455 struct connman_rtnl *rtnl = list->data;
457 if (rtnl->newgateway)
458 rtnl->newgateway(index, gatewaystr);
462 static void process_delroute(unsigned char family, unsigned char scope,
463 struct rtmsg *msg, int bytes)
466 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
467 char dststr[16], gatewaystr[16];
470 if (family != AF_INET)
473 extract_route(msg, bytes, &index, &dst, &gateway);
475 inet_ntop(family, &dst, dststr, sizeof(dststr));
476 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
478 __connman_ipconfig_delroute(index, scope, dststr, gatewaystr);
480 if (scope != RT_SCOPE_UNIVERSE || dst.s_addr != INADDR_ANY)
483 for (list = rtnl_list; list; list = list->next) {
484 struct connman_rtnl *rtnl = list->data;
486 if (rtnl->delgateway)
487 rtnl->delgateway(index, gatewaystr);
491 static inline void print_ether(struct rtattr *attr, const char *name)
493 int len = (int) RTA_PAYLOAD(attr);
495 if (len == ETH_ALEN) {
496 struct ether_addr eth;
497 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
498 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
500 print(" attr %s (len %d)\n", name, len);
503 static inline void print_inet(struct rtattr *attr, const char *name,
504 unsigned char family)
506 int len = (int) RTA_PAYLOAD(attr);
508 if (family == AF_INET && len == sizeof(struct in_addr)) {
510 addr = *((struct in_addr *) RTA_DATA(attr));
511 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
513 print(" attr %s (len %d)\n", name, len);
516 static inline void print_string(struct rtattr *attr, const char *name)
518 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
519 (char *) RTA_DATA(attr));
522 static inline void print_byte(struct rtattr *attr, const char *name)
524 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
525 *((unsigned char *) RTA_DATA(attr)));
528 static inline void print_integer(struct rtattr *attr, const char *name)
530 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
531 *((int *) RTA_DATA(attr)));
534 static inline void print_attr(struct rtattr *attr, const char *name)
536 int len = (int) RTA_PAYLOAD(attr);
539 print(" attr %s (len %d)\n", name, len);
541 print(" attr %d (len %d)\n", attr->rta_type, len);
544 static void rtnl_link(struct nlmsghdr *hdr)
546 struct ifinfomsg *msg;
550 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
551 bytes = IFLA_PAYLOAD(hdr);
553 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
555 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
556 attr = RTA_NEXT(attr, bytes)) {
557 switch (attr->rta_type) {
559 print_ether(attr, "address");
562 print_ether(attr, "broadcast");
565 print_string(attr, "ifname");
568 print_integer(attr, "mtu");
571 print_attr(attr, "link");
574 print_attr(attr, "qdisc");
577 print_attr(attr, "stats");
580 print_attr(attr, "cost");
583 print_attr(attr, "priority");
586 print_attr(attr, "master");
589 print_attr(attr, "wireless");
592 print_attr(attr, "protinfo");
595 print_integer(attr, "txqlen");
598 print_attr(attr, "map");
601 print_attr(attr, "weight");
604 print_byte(attr, "operstate");
607 print_byte(attr, "linkmode");
610 print_attr(attr, NULL);
616 static void rtnl_newlink(struct nlmsghdr *hdr)
618 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
622 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
623 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
626 static void rtnl_dellink(struct nlmsghdr *hdr)
628 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
632 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
633 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
636 static void rtnl_addr(struct nlmsghdr *hdr)
638 struct ifaddrmsg *msg;
642 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
643 bytes = IFA_PAYLOAD(hdr);
645 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
647 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
648 attr = RTA_NEXT(attr, bytes)) {
649 switch (attr->rta_type) {
651 print_inet(attr, "address", msg->ifa_family);
654 print_inet(attr, "local", msg->ifa_family);
657 print_string(attr, "label");
660 print_inet(attr, "broadcast", msg->ifa_family);
663 print_attr(attr, "anycast");
666 print_attr(attr, "cacheinfo");
669 print_attr(attr, "multicast");
672 print_attr(attr, NULL);
678 static void rtnl_newaddr(struct nlmsghdr *hdr)
680 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
684 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
685 msg, IFA_PAYLOAD(hdr));
688 static void rtnl_deladdr(struct nlmsghdr *hdr)
690 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
694 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
695 msg, IFA_PAYLOAD(hdr));
698 static void rtnl_route(struct nlmsghdr *hdr)
704 msg = (struct rtmsg *) NLMSG_DATA(hdr);
705 bytes = RTM_PAYLOAD(hdr);
707 print("rtm_family %d rtm_table %d rtm_protocol %d",
708 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
709 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
710 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
712 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
713 attr = RTA_NEXT(attr, bytes)) {
714 switch (attr->rta_type) {
716 print_inet(attr, "dst", msg->rtm_family);
719 print_inet(attr, "src", msg->rtm_family);
722 print_string(attr, "iif");
725 print_integer(attr, "oif");
728 print_inet(attr, "gateway", msg->rtm_family);
731 print_attr(attr, "priority");
734 print_inet(attr, "prefsrc", msg->rtm_family);
737 print_attr(attr, "metrics");
740 print_integer(attr, "table");
743 print_attr(attr, NULL);
749 static void rtnl_newroute(struct nlmsghdr *hdr)
751 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
755 if (msg->rtm_table == RT_TABLE_MAIN &&
756 msg->rtm_protocol == RTPROT_BOOT &&
757 msg->rtm_type == RTN_UNICAST)
758 process_newroute(msg->rtm_family, msg->rtm_scope,
759 msg, RTM_PAYLOAD(hdr));
762 static void rtnl_delroute(struct nlmsghdr *hdr)
764 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
768 if (msg->rtm_table == RT_TABLE_MAIN &&
769 msg->rtm_protocol == RTPROT_BOOT &&
770 msg->rtm_type == RTN_UNICAST)
771 process_delroute(msg->rtm_family, msg->rtm_scope,
772 msg, RTM_PAYLOAD(hdr));
775 static const char *type2string(uint16_t type)
807 static GIOChannel *channel = NULL;
809 struct rtnl_request {
813 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
815 static GSList *request_list = NULL;
816 static guint32 request_seq = 0;
818 static struct rtnl_request *find_request(guint32 seq)
822 for (list = request_list; list; list = list->next) {
823 struct rtnl_request *req = list->data;
825 if (req->hdr.nlmsg_seq == seq)
832 static int send_request(struct rtnl_request *req)
834 struct sockaddr_nl addr;
837 DBG("%s len %d type %d flags 0x%04x seq %d",
838 type2string(req->hdr.nlmsg_type),
839 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
840 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
842 sk = g_io_channel_unix_get_fd(channel);
844 memset(&addr, 0, sizeof(addr));
845 addr.nl_family = AF_NETLINK;
847 return sendto(sk, req, req->hdr.nlmsg_len, 0,
848 (struct sockaddr *) &addr, sizeof(addr));
851 static int queue_request(struct rtnl_request *req)
853 request_list = g_slist_append(request_list, req);
855 if (g_slist_length(request_list) > 1)
858 return send_request(req);
861 static int process_response(guint32 seq)
863 struct rtnl_request *req;
867 req = find_request(seq);
869 request_list = g_slist_remove(request_list, req);
873 req = g_slist_nth_data(request_list, 0);
877 return send_request(req);
880 static void rtnl_message(void *buf, size_t len)
882 DBG("buf %p len %zd", buf, len);
885 struct nlmsghdr *hdr = buf;
886 struct nlmsgerr *err;
888 if (!NLMSG_OK(hdr, len))
891 DBG("%s len %d type %d flags 0x%04x seq %d",
892 type2string(hdr->nlmsg_type),
893 hdr->nlmsg_len, hdr->nlmsg_type,
894 hdr->nlmsg_flags, hdr->nlmsg_seq);
896 switch (hdr->nlmsg_type) {
901 process_response(hdr->nlmsg_seq);
904 err = NLMSG_DATA(hdr);
905 DBG("error %d (%s)", -err->error,
906 strerror(-err->error));
928 len -= hdr->nlmsg_len;
929 buf += hdr->nlmsg_len;
933 static gboolean netlink_event(GIOChannel *chan,
934 GIOCondition cond, gpointer data)
936 unsigned char buf[4096];
940 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
943 memset(buf, 0, sizeof(buf));
945 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
947 if (err == G_IO_ERROR_AGAIN)
952 rtnl_message(buf, len);
957 static int send_getlink(void)
959 struct rtnl_request *req;
963 req = g_try_malloc0(RTNL_REQUEST_SIZE);
967 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
968 req->hdr.nlmsg_type = RTM_GETLINK;
969 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
970 req->hdr.nlmsg_pid = 0;
971 req->hdr.nlmsg_seq = request_seq++;
972 req->msg.rtgen_family = AF_INET;
974 return queue_request(req);
977 static int send_getaddr(void)
979 struct rtnl_request *req;
983 req = g_try_malloc0(RTNL_REQUEST_SIZE);
987 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
988 req->hdr.nlmsg_type = RTM_GETADDR;
989 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
990 req->hdr.nlmsg_pid = 0;
991 req->hdr.nlmsg_seq = request_seq++;
992 req->msg.rtgen_family = AF_INET;
994 return queue_request(req);
997 static int send_getroute(void)
999 struct rtnl_request *req;
1003 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1007 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1008 req->hdr.nlmsg_type = RTM_GETROUTE;
1009 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1010 req->hdr.nlmsg_pid = 0;
1011 req->hdr.nlmsg_seq = request_seq++;
1012 req->msg.rtgen_family = AF_INET;
1014 return queue_request(req);
1017 int __connman_rtnl_init(void)
1019 struct sockaddr_nl addr;
1024 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
1028 memset(&addr, 0, sizeof(addr));
1029 addr.nl_family = AF_NETLINK;
1030 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
1032 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1037 channel = g_io_channel_unix_new(sk);
1038 g_io_channel_set_close_on_unref(channel, TRUE);
1040 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1041 netlink_event, NULL);
1046 void __connman_rtnl_start(void)
1055 void __connman_rtnl_cleanup(void)
1061 for (list = watch_list; list; list = list->next) {
1062 struct watch_data *watch = list->data;
1064 DBG("removing watch %d", watch->id);
1070 g_slist_free(watch_list);
1073 for (list = request_list; list; list = list->next) {
1074 struct rtnl_request *req = list->data;
1076 DBG("%s len %d type %d flags 0x%04x seq %d",
1077 type2string(req->hdr.nlmsg_type),
1078 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1079 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1085 g_slist_free(request_list);
1086 request_list = NULL;
1088 g_io_channel_shutdown(channel, TRUE, NULL);
1089 g_io_channel_unref(channel);