5 * Copyright (C) 2007-2013 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
30 #include <sys/socket.h>
31 #include <sys/ioctl.h>
32 #include <arpa/inet.h>
33 #include <netinet/ether.h>
34 #include <netinet/icmp6.h>
35 #include <net/if_arp.h>
37 #include <linux/netlink.h>
38 #include <linux/rtnetlink.h>
39 #include <linux/wireless.h>
45 #ifndef ARPHDR_PHONET_PIPE
46 #define ARPHDR_PHONET_PIPE (821)
49 #define print(arg...) do { if (0) connman_info(arg); } while (0)
50 //#define print(arg...) connman_info(arg)
55 connman_rtnl_link_cb_t newlink;
59 static GSList *watch_list = NULL;
60 static unsigned int watch_id = 0;
62 static GSList *update_list = NULL;
63 static guint update_interval = G_MAXUINT;
64 static guint update_timeout = 0;
66 struct interface_data {
69 enum connman_service_type service_type;
70 enum connman_device_type device_type;
73 static GHashTable *interface_list = NULL;
75 static void free_interface(gpointer data)
77 struct interface_data *interface = data;
79 __connman_technology_remove_interface(interface->service_type,
80 interface->index, interface->ident);
82 g_free(interface->ident);
86 static bool ether_blacklisted(const char *name)
91 if (__connman_device_isfiltered(name))
97 static bool wext_interface(char *ifname)
102 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
106 memset(&wrq, 0, sizeof(wrq));
107 strncpy(wrq.ifr_name, ifname, sizeof(wrq.ifr_name) - 1);
109 err = ioctl(fd, SIOCGIWNAME, &wrq);
119 static void read_uevent(struct interface_data *interface)
121 char *filename, *name, line[128];
125 name = connman_inet_ifname(interface->index);
127 if (ether_blacklisted(name)) {
128 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
129 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
132 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
133 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
136 filename = g_strdup_printf("/sys/class/net/%s/uevent", name);
138 f = fopen(filename, "re");
143 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
144 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
148 found_devtype = false;
149 while (fgets(line, sizeof(line), f)) {
152 pos = strchr(line, '\n');
157 if (strncmp(line, "DEVTYPE=", 8) != 0)
160 found_devtype = true;
162 if (strcmp(line + 8, "wlan") == 0) {
163 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
164 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
165 } else if (strcmp(line + 8, "wwan") == 0) {
166 interface->service_type = CONNMAN_SERVICE_TYPE_CELLULAR;
167 interface->device_type = CONNMAN_DEVICE_TYPE_CELLULAR;
168 } else if (strcmp(line + 8, "bluetooth") == 0) {
169 interface->service_type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
170 interface->device_type = CONNMAN_DEVICE_TYPE_BLUETOOTH;
171 } else if (strcmp(line + 8, "gadget") == 0) {
172 interface->service_type = CONNMAN_SERVICE_TYPE_GADGET;
173 interface->device_type = CONNMAN_DEVICE_TYPE_GADGET;
174 } else if (strcmp(line + 8, "vlan") == 0) {
175 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
176 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
177 } else if (strcmp(line + 8, "bond") == 0) {
178 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
179 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
181 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
182 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
191 /* We haven't got a DEVTYPE, let's check if it's a wireless device */
192 if (wext_interface(name)) {
193 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
194 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
196 connman_error("%s runs an unsupported 802.11 driver", name);
203 enum connman_device_type __connman_rtnl_get_device_type(int index)
205 struct interface_data *interface;
207 interface = g_hash_table_lookup(interface_list,
208 GINT_TO_POINTER(index));
210 return CONNMAN_DEVICE_TYPE_UNKNOWN;
212 return interface->device_type;
216 * connman_rtnl_add_newlink_watch:
217 * @index: network device index
218 * @callback: callback function
219 * @user_data: callback data;
221 * Add a new RTNL watch for newlink events
223 * Returns: %0 on failure and a unique id on success
225 unsigned int connman_rtnl_add_newlink_watch(int index,
226 connman_rtnl_link_cb_t callback, void *user_data)
228 struct watch_data *watch;
230 watch = g_try_new0(struct watch_data, 1);
234 watch->id = ++watch_id;
235 watch->index = index;
237 watch->newlink = callback;
238 watch->user_data = user_data;
240 watch_list = g_slist_prepend(watch_list, watch);
242 DBG("id %d", watch->id);
245 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
248 callback(flags, 0, user_data);
255 * connman_rtnl_remove_watch:
256 * @id: watch identifier
258 * Remove the RTNL watch for the identifier
260 void connman_rtnl_remove_watch(unsigned int id)
269 for (list = watch_list; list; list = list->next) {
270 struct watch_data *watch = list->data;
272 if (watch->id == id) {
273 watch_list = g_slist_remove(watch_list, watch);
280 static void trigger_rtnl(int index, void *user_data)
282 struct connman_rtnl *rtnl = user_data;
285 unsigned short type = __connman_ipconfig_get_type_from_index(index);
286 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
288 rtnl->newlink(type, index, flags, 0);
291 if (rtnl->newgateway) {
292 const char *gateway =
293 __connman_ipconfig_get_gateway_from_index(index,
294 CONNMAN_IPCONFIG_TYPE_ALL);
297 rtnl->newgateway(index, gateway);
301 static GSList *rtnl_list = NULL;
303 static gint compare_priority(gconstpointer a, gconstpointer b)
305 const struct connman_rtnl *rtnl1 = a;
306 const struct connman_rtnl *rtnl2 = b;
308 return rtnl2->priority - rtnl1->priority;
312 * connman_rtnl_register:
315 * Register a new RTNL module
317 * Returns: %0 on success
319 int connman_rtnl_register(struct connman_rtnl *rtnl)
321 DBG("rtnl %p name %s", rtnl, rtnl->name);
323 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
326 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
332 * connman_rtnl_unregister:
335 * Remove a previously registered RTNL module
337 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
339 DBG("rtnl %p name %s", rtnl, rtnl->name);
341 rtnl_list = g_slist_remove(rtnl_list, rtnl);
344 static const char *operstate2str(unsigned char operstate)
347 case IF_OPER_UNKNOWN:
349 case IF_OPER_NOTPRESENT:
350 return "NOT-PRESENT";
353 case IF_OPER_LOWERLAYERDOWN:
354 return "LOWER-LAYER-DOWN";
355 case IF_OPER_TESTING:
357 case IF_OPER_DORMANT:
366 static bool extract_link(struct ifinfomsg *msg, int bytes,
367 struct ether_addr *address, const char **ifname,
368 unsigned int *mtu, unsigned char *operstate,
369 struct rtnl_link_stats *stats)
373 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
374 attr = RTA_NEXT(attr, bytes)) {
375 switch (attr->rta_type) {
378 memcpy(address, RTA_DATA(attr), ETH_ALEN);
382 *ifname = RTA_DATA(attr);
386 *mtu = *((unsigned int *) RTA_DATA(attr));
390 memcpy(stats, RTA_DATA(attr),
391 sizeof(struct rtnl_link_stats));
395 *operstate = *((unsigned char *) RTA_DATA(attr));
407 static void process_newlink(unsigned short type, int index, unsigned flags,
408 unsigned change, struct ifinfomsg *msg, int bytes)
410 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
411 struct rtnl_link_stats stats;
412 unsigned char operstate = 0xff;
413 struct interface_data *interface;
414 const char *ifname = NULL;
415 unsigned int mtu = 0;
416 char ident[13], str[18];
419 memset(&stats, 0, sizeof(stats));
420 if (!extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats))
423 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
424 address.ether_addr_octet[0],
425 address.ether_addr_octet[1],
426 address.ether_addr_octet[2],
427 address.ether_addr_octet[3],
428 address.ether_addr_octet[4],
429 address.ether_addr_octet[5]);
431 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
432 address.ether_addr_octet[0],
433 address.ether_addr_octet[1],
434 address.ether_addr_octet[2],
435 address.ether_addr_octet[3],
436 address.ether_addr_octet[4],
437 address.ether_addr_octet[5]);
439 if (flags & IFF_SLAVE) {
440 connman_info("%s {newlink} ignoring slave, index %d address %s",
447 case ARPHRD_LOOPBACK:
448 case ARPHDR_PHONET_PIPE:
451 __connman_ipconfig_newlink(index, type, flags,
456 connman_info("%s {newlink} index %d address %s mtu %u",
457 ifname, index, str, mtu);
459 if (operstate != 0xff)
460 connman_info("%s {newlink} index %d operstate %u <%s>",
461 ifname, index, operstate,
462 operstate2str(operstate));
464 interface = g_hash_table_lookup(interface_list, GINT_TO_POINTER(index));
466 interface = g_new0(struct interface_data, 1);
467 interface->index = index;
468 interface->ident = g_strdup(ident);
470 g_hash_table_insert(interface_list,
471 GINT_TO_POINTER(index), interface);
473 if (type == ARPHRD_ETHER)
474 read_uevent(interface);
475 } else if (type == ARPHRD_ETHER && interface->device_type == CONNMAN_DEVICE_TYPE_UNKNOWN)
476 read_uevent(interface);
480 for (list = rtnl_list; list; list = list->next) {
481 struct connman_rtnl *rtnl = list->data;
484 rtnl->newlink(type, index, flags, change);
488 * The interface needs to be added after the newlink call.
489 * The newlink will create the technology when needed and
490 * __connman_technology_add_interface() expects the
491 * technology to be there already.
494 __connman_technology_add_interface(interface->service_type,
495 interface->index, interface->ident);
499 GSList *next = list->next;
500 struct watch_data *watch = list->data;
502 if (watch->index == index && watch->newlink)
503 watch->newlink(flags, change, watch->user_data);
509 static void process_dellink(unsigned short type, int index, unsigned flags,
510 unsigned change, struct ifinfomsg *msg, int bytes)
512 struct rtnl_link_stats stats;
513 unsigned char operstate = 0xff;
514 const char *ifname = NULL;
517 memset(&stats, 0, sizeof(stats));
518 if (!extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats))
521 if (operstate != 0xff)
522 connman_info("%s {dellink} index %d operstate %u <%s>",
523 ifname, index, operstate,
524 operstate2str(operstate));
526 for (list = rtnl_list; list; list = list->next) {
527 struct connman_rtnl *rtnl = list->data;
530 rtnl->dellink(type, index, flags, change);
535 case ARPHRD_LOOPBACK:
536 case ARPHDR_PHONET_PIPE:
539 __connman_ipconfig_dellink(index, &stats);
543 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
546 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
548 struct in_addr *local,
549 struct in_addr *address,
550 struct in_addr *broadcast)
554 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
555 attr = RTA_NEXT(attr, bytes)) {
556 switch (attr->rta_type) {
559 *address = *((struct in_addr *) RTA_DATA(attr));
563 *local = *((struct in_addr *) RTA_DATA(attr));
567 *broadcast = *((struct in_addr *) RTA_DATA(attr));
571 *label = RTA_DATA(attr);
577 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
578 struct in6_addr *addr,
579 struct in6_addr *local)
583 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
584 attr = RTA_NEXT(attr, bytes)) {
585 switch (attr->rta_type) {
588 *addr = *((struct in6_addr *) RTA_DATA(attr));
592 *local = *((struct in6_addr *) RTA_DATA(attr));
598 static void process_newaddr(unsigned char family, unsigned char prefixlen,
599 int index, struct ifaddrmsg *msg, int bytes)
601 struct in_addr ipv4_addr = { INADDR_ANY };
602 struct in6_addr ipv6_address, ipv6_local;
603 const char *label = NULL;
605 char ip_string[INET6_ADDRSTRLEN];
607 if (family == AF_INET) {
609 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
611 } else if (family == AF_INET6) {
612 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
613 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
621 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
624 if (__connman_ipconfig_newaddr(index, family, label,
625 prefixlen, ip_string) >= 0) {
626 if (family == AF_INET6) {
628 * Re-create RDNSS configured servers if there
629 * are any for this interface. This is done
630 * because we might have now properly
631 * configured interface with proper
632 * autoconfigured address.
634 __connman_resolver_redo_servers(index);
639 static void process_deladdr(unsigned char family, unsigned char prefixlen,
640 int index, struct ifaddrmsg *msg, int bytes)
642 struct in_addr ipv4_addr = { INADDR_ANY };
643 struct in6_addr ipv6_address, ipv6_local;
644 const char *label = NULL;
646 char ip_string[INET6_ADDRSTRLEN];
648 if (family == AF_INET) {
649 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
651 } else if (family == AF_INET6) {
652 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
653 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
661 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
664 __connman_ipconfig_deladdr(index, family, label,
665 prefixlen, ip_string);
668 static void extract_ipv4_route(struct rtmsg *msg, int bytes, int *index,
670 struct in_addr *gateway)
674 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
675 attr = RTA_NEXT(attr, bytes)) {
676 switch (attr->rta_type) {
679 *dst = *((struct in_addr *) RTA_DATA(attr));
683 *gateway = *((struct in_addr *) RTA_DATA(attr));
687 *index = *((int *) RTA_DATA(attr));
693 static void extract_ipv6_route(struct rtmsg *msg, int bytes, int *index,
694 struct in6_addr *dst,
695 struct in6_addr *gateway)
699 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
700 attr = RTA_NEXT(attr, bytes)) {
701 switch (attr->rta_type) {
704 *dst = *((struct in6_addr *) RTA_DATA(attr));
709 *((struct in6_addr *) RTA_DATA(attr));
713 *index = *((int *) RTA_DATA(attr));
719 static void process_newroute(unsigned char family, unsigned char scope,
720 struct rtmsg *msg, int bytes)
723 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
726 if (family == AF_INET) {
727 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
729 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
731 inet_ntop(family, &dst, dststr, sizeof(dststr));
732 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
734 __connman_ipconfig_newroute(index, family, scope, dststr,
737 /* skip host specific routes */
738 if (scope != RT_SCOPE_UNIVERSE &&
739 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
742 if (dst.s_addr != INADDR_ANY)
745 } else if (family == AF_INET6) {
746 struct in6_addr dst = IN6ADDR_ANY_INIT,
747 gateway = IN6ADDR_ANY_INIT;
749 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
751 inet_ntop(family, &dst, dststr, sizeof(dststr));
752 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
754 __connman_ipconfig_newroute(index, family, scope, dststr,
757 /* skip host specific routes */
758 if (scope != RT_SCOPE_UNIVERSE &&
759 !(scope == RT_SCOPE_LINK &&
760 IN6_IS_ADDR_UNSPECIFIED(&dst)))
763 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
768 for (list = rtnl_list; list; list = list->next) {
769 struct connman_rtnl *rtnl = list->data;
771 if (rtnl->newgateway)
772 rtnl->newgateway(index, gatewaystr);
776 static void process_delroute(unsigned char family, unsigned char scope,
777 struct rtmsg *msg, int bytes)
780 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
783 if (family == AF_INET) {
784 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
786 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
788 inet_ntop(family, &dst, dststr, sizeof(dststr));
789 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
791 __connman_ipconfig_delroute(index, family, scope, dststr,
794 /* skip host specific routes */
795 if (scope != RT_SCOPE_UNIVERSE &&
796 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
799 if (dst.s_addr != INADDR_ANY)
802 } else if (family == AF_INET6) {
803 struct in6_addr dst = IN6ADDR_ANY_INIT,
804 gateway = IN6ADDR_ANY_INIT;
806 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
808 inet_ntop(family, &dst, dststr, sizeof(dststr));
809 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
811 __connman_ipconfig_delroute(index, family, scope, dststr,
814 /* skip host specific routes */
815 if (scope != RT_SCOPE_UNIVERSE &&
816 !(scope == RT_SCOPE_LINK &&
817 IN6_IS_ADDR_UNSPECIFIED(&dst)))
820 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
825 for (list = rtnl_list; list; list = list->next) {
826 struct connman_rtnl *rtnl = list->data;
828 if (rtnl->delgateway)
829 rtnl->delgateway(index, gatewaystr);
833 static inline void print_ether(struct rtattr *attr, const char *name)
835 int len = (int) RTA_PAYLOAD(attr);
837 if (len == ETH_ALEN) {
838 struct ether_addr eth;
839 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
840 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
842 print(" attr %s (len %d)\n", name, len);
845 static inline void print_inet(struct rtattr *attr, const char *name,
846 unsigned char family)
848 int len = (int) RTA_PAYLOAD(attr);
850 if (family == AF_INET && len == sizeof(struct in_addr)) {
852 addr = *((struct in_addr *) RTA_DATA(attr));
853 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
855 print(" attr %s (len %d)\n", name, len);
858 static inline void print_string(struct rtattr *attr, const char *name)
860 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
861 (char *) RTA_DATA(attr));
864 static inline void print_byte(struct rtattr *attr, const char *name)
866 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
867 *((unsigned char *) RTA_DATA(attr)));
870 static inline void print_integer(struct rtattr *attr, const char *name)
872 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
873 *((int *) RTA_DATA(attr)));
876 static inline void print_attr(struct rtattr *attr, const char *name)
878 int len = (int) RTA_PAYLOAD(attr);
881 print(" attr %s (len %d)\n", name, len);
883 print(" attr %d (len %d)\n", attr->rta_type, len);
886 static void rtnl_link(struct nlmsghdr *hdr)
888 struct ifinfomsg *msg;
892 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
893 bytes = IFLA_PAYLOAD(hdr);
895 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
897 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
898 attr = RTA_NEXT(attr, bytes)) {
899 switch (attr->rta_type) {
901 print_ether(attr, "address");
904 print_ether(attr, "broadcast");
907 print_string(attr, "ifname");
910 print_integer(attr, "mtu");
913 print_attr(attr, "link");
916 print_attr(attr, "qdisc");
919 print_attr(attr, "stats");
922 print_attr(attr, "cost");
925 print_attr(attr, "priority");
928 print_attr(attr, "master");
931 print_attr(attr, "wireless");
934 print_attr(attr, "protinfo");
937 print_integer(attr, "txqlen");
940 print_attr(attr, "map");
943 print_attr(attr, "weight");
946 print_byte(attr, "operstate");
949 print_byte(attr, "linkmode");
952 print_attr(attr, NULL);
958 static void rtnl_newlink(struct nlmsghdr *hdr)
960 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
964 if (hdr->nlmsg_type == IFLA_WIRELESS)
965 connman_warn_once("Obsolete WEXT WiFi driver detected");
967 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
968 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
971 static void rtnl_dellink(struct nlmsghdr *hdr)
973 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
977 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
978 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
981 static void rtnl_addr(struct nlmsghdr *hdr)
983 struct ifaddrmsg *msg;
987 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
988 bytes = IFA_PAYLOAD(hdr);
990 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
992 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
993 attr = RTA_NEXT(attr, bytes)) {
994 switch (attr->rta_type) {
996 print_inet(attr, "address", msg->ifa_family);
999 print_inet(attr, "local", msg->ifa_family);
1002 print_string(attr, "label");
1005 print_inet(attr, "broadcast", msg->ifa_family);
1008 print_attr(attr, "anycast");
1011 print_attr(attr, "cacheinfo");
1014 print_attr(attr, "multicast");
1017 print_attr(attr, NULL);
1023 static void rtnl_newaddr(struct nlmsghdr *hdr)
1025 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1029 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1030 msg, IFA_PAYLOAD(hdr));
1033 static void rtnl_deladdr(struct nlmsghdr *hdr)
1035 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1039 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1040 msg, IFA_PAYLOAD(hdr));
1043 static void rtnl_route(struct nlmsghdr *hdr)
1046 struct rtattr *attr;
1049 msg = (struct rtmsg *) NLMSG_DATA(hdr);
1050 bytes = RTM_PAYLOAD(hdr);
1052 print("rtm_family %d rtm_table %d rtm_protocol %d",
1053 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
1054 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
1055 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
1057 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
1058 attr = RTA_NEXT(attr, bytes)) {
1059 switch (attr->rta_type) {
1061 print_inet(attr, "dst", msg->rtm_family);
1064 print_inet(attr, "src", msg->rtm_family);
1067 print_string(attr, "iif");
1070 print_integer(attr, "oif");
1073 print_inet(attr, "gateway", msg->rtm_family);
1076 print_attr(attr, "priority");
1079 print_inet(attr, "prefsrc", msg->rtm_family);
1082 print_attr(attr, "metrics");
1085 print_integer(attr, "table");
1088 print_attr(attr, NULL);
1094 static bool is_route_rtmsg(struct rtmsg *msg)
1096 if (msg->rtm_flags & RTM_F_CLONED)
1099 if (msg->rtm_table != RT_TABLE_MAIN)
1102 if (msg->rtm_protocol != RTPROT_BOOT &&
1103 msg->rtm_protocol != RTPROT_KERNEL)
1106 if (msg->rtm_type != RTN_UNICAST)
1112 static void rtnl_newroute(struct nlmsghdr *hdr)
1114 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1118 if (is_route_rtmsg(msg))
1119 process_newroute(msg->rtm_family, msg->rtm_scope,
1120 msg, RTM_PAYLOAD(hdr));
1123 static void rtnl_delroute(struct nlmsghdr *hdr)
1125 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1129 if (is_route_rtmsg(msg))
1130 process_delroute(msg->rtm_family, msg->rtm_scope,
1131 msg, RTM_PAYLOAD(hdr));
1134 static void *rtnl_nd_opt_rdnss(struct nd_opt_hdr *opt, guint32 *lifetime,
1137 guint32 *optint = (void *)opt;
1139 if (opt->nd_opt_len < 3)
1142 if (*lifetime > ntohl(optint[1]))
1143 *lifetime = ntohl(optint[1]);
1145 /* nd_opt_len is in units of 8 bytes. The header is 1 unit (8 bytes)
1146 and each address is another 2 units (16 bytes).
1147 So the number of addresses (given rounding) is nd_opt_len/2 */
1148 *nr_servers = opt->nd_opt_len / 2;
1150 /* And they start 8 bytes into the packet, or two guint32s in. */
1154 static const char **rtnl_nd_opt_dnssl(struct nd_opt_hdr *opt, guint32 *lifetime)
1156 const char **domains = NULL;
1157 guint32 *optint = (void *)opt;
1158 unsigned char *optc = (void *)&optint[2];
1159 int data_len = (opt->nd_opt_len * 8) - 8;
1163 if (*lifetime > ntohl(optint[1]))
1164 *lifetime = ntohl(optint[1]);
1166 /* Turn it into normal strings by converting the length bytes into '.',
1167 and count how many search domains there are while we're at it. */
1169 while (i < data_len) {
1170 if (optc[i] > 0x3f) {
1171 DBG("DNSSL contains compressed elements in violation of RFC6106");
1178 /* Check for double zero */
1179 if (i < data_len && optc[i] == 0)
1187 if (i >= data_len) {
1188 DBG("DNSSL data overflows option length");
1195 domains = g_try_new0(const char *, nr_domains + 1);
1199 /* Now point to the normal strings, missing out the leading '.' that
1200 each of them will have now. */
1201 for (i = 0; i < nr_domains; i++) {
1202 domains[i] = (char *)optc + 1;
1203 optc += strlen((char *)optc) + 1;
1209 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1211 struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1212 struct nd_opt_hdr *opt;
1213 guint32 lifetime = -1;
1214 const char **domains = NULL;
1215 struct in6_addr *servers = NULL;
1216 int i, nr_servers = 0;
1217 int msglen = msg->nduseropt_opts_len;
1220 DBG("family %d index %d len %d type %d code %d",
1221 msg->nduseropt_family, msg->nduseropt_ifindex,
1222 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1223 msg->nduseropt_icmp_code);
1225 if (msg->nduseropt_family != AF_INET6 ||
1226 msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1227 msg->nduseropt_icmp_code != 0)
1230 index = msg->nduseropt_ifindex;
1234 for (opt = (void *)&msg[1];
1236 msglen -= opt->nd_opt_len * 8,
1237 opt = ((void *)opt) + opt->nd_opt_len*8) {
1239 DBG("remaining %d nd opt type %d len %d\n",
1240 msglen, opt->nd_opt_type, opt->nd_opt_len);
1242 if (opt->nd_opt_type == 25) { /* ND_OPT_RDNSS */
1245 servers = rtnl_nd_opt_rdnss(opt, &lifetime,
1247 for (i = 0; i < nr_servers; i++) {
1248 if (!inet_ntop(AF_INET6, servers + i, buf,
1252 connman_resolver_append_lifetime(index,
1253 NULL, buf, lifetime);
1256 } else if (opt->nd_opt_type == 31) { /* ND_OPT_DNSSL */
1259 domains = rtnl_nd_opt_dnssl(opt, &lifetime);
1260 for (i = 0; domains && domains[i]; i++)
1261 connman_resolver_append_lifetime(index,
1262 domains[i], NULL, lifetime);
1269 static const char *type2string(uint16_t type)
1298 case RTM_NEWNDUSEROPT:
1299 return "NEWNDUSEROPT";
1305 static GIOChannel *channel = NULL;
1306 static guint channel_watch = 0;
1308 struct rtnl_request {
1309 struct nlmsghdr hdr;
1310 struct rtgenmsg msg;
1312 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1314 static GSList *request_list = NULL;
1315 static guint32 request_seq = 0;
1317 static struct rtnl_request *find_request(guint32 seq)
1321 for (list = request_list; list; list = list->next) {
1322 struct rtnl_request *req = list->data;
1324 if (req->hdr.nlmsg_seq == seq)
1331 static int send_request(struct rtnl_request *req)
1333 struct sockaddr_nl addr;
1336 DBG("%s len %d type %d flags 0x%04x seq %d",
1337 type2string(req->hdr.nlmsg_type),
1338 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1339 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1341 sk = g_io_channel_unix_get_fd(channel);
1343 memset(&addr, 0, sizeof(addr));
1344 addr.nl_family = AF_NETLINK;
1346 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1347 (struct sockaddr *) &addr, sizeof(addr));
1350 static int queue_request(struct rtnl_request *req)
1352 request_list = g_slist_append(request_list, req);
1354 if (g_slist_length(request_list) > 1)
1357 return send_request(req);
1360 static int process_response(guint32 seq)
1362 struct rtnl_request *req;
1366 req = find_request(seq);
1368 request_list = g_slist_remove(request_list, req);
1372 req = g_slist_nth_data(request_list, 0);
1376 return send_request(req);
1379 static void rtnl_message(void *buf, size_t len)
1382 struct nlmsghdr *hdr = buf;
1383 struct nlmsgerr *err;
1385 if (!NLMSG_OK(hdr, len))
1388 DBG("%s len %u type %u flags 0x%04x seq %u pid %u",
1389 type2string(hdr->nlmsg_type),
1390 hdr->nlmsg_len, hdr->nlmsg_type,
1391 hdr->nlmsg_flags, hdr->nlmsg_seq,
1394 switch (hdr->nlmsg_type) {
1399 process_response(hdr->nlmsg_seq);
1402 err = NLMSG_DATA(hdr);
1403 DBG("error %d (%s)", -err->error,
1404 strerror(-err->error));
1424 case RTM_NEWNDUSEROPT:
1425 rtnl_newnduseropt(hdr);
1429 len -= hdr->nlmsg_len;
1430 buf += hdr->nlmsg_len;
1434 static gboolean netlink_event(GIOChannel *chan, GIOCondition cond, gpointer data)
1436 unsigned char buf[4096];
1437 struct sockaddr_nl nladdr;
1438 socklen_t addr_len = sizeof(nladdr);
1442 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1445 memset(buf, 0, sizeof(buf));
1446 memset(&nladdr, 0, sizeof(nladdr));
1448 fd = g_io_channel_unix_get_fd(chan);
1450 status = recvfrom(fd, buf, sizeof(buf), 0,
1451 (struct sockaddr *) &nladdr, &addr_len);
1453 if (errno == EINTR || errno == EAGAIN)
1462 if (nladdr.nl_pid != 0) { /* not sent by kernel, ignore */
1463 DBG("Received msg from %u, ignoring it", nladdr.nl_pid);
1467 rtnl_message(buf, status);
1472 static int send_getlink(void)
1474 struct rtnl_request *req;
1478 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1482 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1483 req->hdr.nlmsg_type = RTM_GETLINK;
1484 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1485 req->hdr.nlmsg_pid = 0;
1486 req->hdr.nlmsg_seq = request_seq++;
1487 req->msg.rtgen_family = AF_INET;
1489 return queue_request(req);
1492 static int send_getaddr(void)
1494 struct rtnl_request *req;
1498 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1502 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1503 req->hdr.nlmsg_type = RTM_GETADDR;
1504 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1505 req->hdr.nlmsg_pid = 0;
1506 req->hdr.nlmsg_seq = request_seq++;
1507 req->msg.rtgen_family = AF_INET;
1509 return queue_request(req);
1512 static int send_getroute(void)
1514 struct rtnl_request *req;
1518 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1522 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1523 req->hdr.nlmsg_type = RTM_GETROUTE;
1524 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1525 req->hdr.nlmsg_pid = 0;
1526 req->hdr.nlmsg_seq = request_seq++;
1527 req->msg.rtgen_family = AF_INET;
1529 return queue_request(req);
1532 static gboolean update_timeout_cb(gpointer user_data)
1534 __connman_rtnl_request_update();
1539 static void update_interval_callback(guint min)
1541 if (update_timeout > 0)
1542 g_source_remove(update_timeout);
1544 if (min < G_MAXUINT) {
1545 update_interval = min;
1546 update_timeout = g_timeout_add_seconds(update_interval,
1547 update_timeout_cb, NULL);
1550 update_interval = G_MAXUINT;
1554 static gint compare_interval(gconstpointer a, gconstpointer b)
1556 guint val_a = GPOINTER_TO_UINT(a);
1557 guint val_b = GPOINTER_TO_UINT(b);
1559 return val_a - val_b;
1562 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1569 update_list = g_slist_insert_sorted(update_list,
1570 GUINT_TO_POINTER(interval), compare_interval);
1572 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1573 if (min < update_interval) {
1574 update_interval_callback(min);
1575 __connman_rtnl_request_update();
1578 return update_interval;
1581 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1583 guint min = G_MAXUINT;
1588 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1591 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1593 if (min > update_interval)
1594 update_interval_callback(min);
1599 int __connman_rtnl_request_update(void)
1601 return send_getlink();
1604 int __connman_rtnl_init(void)
1606 struct sockaddr_nl addr;
1611 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1612 NULL, free_interface);
1614 sk = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
1618 memset(&addr, 0, sizeof(addr));
1619 addr.nl_family = AF_NETLINK;
1620 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1621 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1622 (1<<(RTNLGRP_ND_USEROPT-1));
1624 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1629 channel = g_io_channel_unix_new(sk);
1630 g_io_channel_set_close_on_unref(channel, TRUE);
1632 g_io_channel_set_encoding(channel, NULL, NULL);
1633 g_io_channel_set_buffered(channel, FALSE);
1635 channel_watch = g_io_add_watch(channel,
1636 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1637 netlink_event, NULL);
1642 void __connman_rtnl_start(void)
1651 void __connman_rtnl_cleanup(void)
1657 for (list = watch_list; list; list = list->next) {
1658 struct watch_data *watch = list->data;
1660 DBG("removing watch %d", watch->id);
1666 g_slist_free(watch_list);
1669 g_slist_free(update_list);
1672 for (list = request_list; list; list = list->next) {
1673 struct rtnl_request *req = list->data;
1675 DBG("%s len %d type %d flags 0x%04x seq %d",
1676 type2string(req->hdr.nlmsg_type),
1677 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1678 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1684 g_slist_free(request_list);
1685 request_list = NULL;
1687 if (channel_watch) {
1688 g_source_remove(channel_watch);
1692 g_io_channel_shutdown(channel, TRUE, NULL);
1693 g_io_channel_unref(channel);
1697 g_hash_table_destroy(interface_list);