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;
131 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
132 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
135 filename = g_strdup_printf("/sys/class/net/%s/uevent", name);
137 f = fopen(filename, "re");
142 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
143 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
147 found_devtype = false;
148 while (fgets(line, sizeof(line), f)) {
151 pos = strchr(line, '\n');
156 if (strncmp(line, "DEVTYPE=", 8) != 0)
159 found_devtype = true;
161 if (strcmp(line + 8, "wlan") == 0) {
162 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
163 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
164 } else if (strcmp(line + 8, "wwan") == 0) {
165 interface->service_type = CONNMAN_SERVICE_TYPE_CELLULAR;
166 interface->device_type = CONNMAN_DEVICE_TYPE_CELLULAR;
167 } else if (strcmp(line + 8, "bluetooth") == 0) {
168 interface->service_type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
169 interface->device_type = CONNMAN_DEVICE_TYPE_BLUETOOTH;
170 } else if (strcmp(line + 8, "gadget") == 0) {
171 interface->service_type = CONNMAN_SERVICE_TYPE_GADGET;
172 interface->device_type = CONNMAN_DEVICE_TYPE_GADGET;
173 } else if (strcmp(line + 8, "vlan") == 0) {
174 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
175 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
178 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
179 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
188 /* We haven't got a DEVTYPE, let's check if it's a wireless device */
189 if (wext_interface(name)) {
190 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
191 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
193 connman_error("%s runs an unsupported 802.11 driver", name);
200 enum connman_device_type __connman_rtnl_get_device_type(int index)
202 struct interface_data *interface;
204 interface = g_hash_table_lookup(interface_list,
205 GINT_TO_POINTER(index));
207 return CONNMAN_DEVICE_TYPE_UNKNOWN;
209 return interface->device_type;
213 * connman_rtnl_add_newlink_watch:
214 * @index: network device index
215 * @callback: callback function
216 * @user_data: callback data;
218 * Add a new RTNL watch for newlink events
220 * Returns: %0 on failure and a unique id on success
222 unsigned int connman_rtnl_add_newlink_watch(int index,
223 connman_rtnl_link_cb_t callback, void *user_data)
225 struct watch_data *watch;
227 watch = g_try_new0(struct watch_data, 1);
231 watch->id = ++watch_id;
232 watch->index = index;
234 watch->newlink = callback;
235 watch->user_data = user_data;
237 watch_list = g_slist_prepend(watch_list, watch);
239 DBG("id %d", watch->id);
242 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
245 callback(flags, 0, user_data);
252 * connman_rtnl_remove_watch:
253 * @id: watch identifier
255 * Remove the RTNL watch for the identifier
257 void connman_rtnl_remove_watch(unsigned int id)
266 for (list = watch_list; list; list = list->next) {
267 struct watch_data *watch = list->data;
269 if (watch->id == id) {
270 watch_list = g_slist_remove(watch_list, watch);
277 static void trigger_rtnl(int index, void *user_data)
279 struct connman_rtnl *rtnl = user_data;
282 unsigned short type = __connman_ipconfig_get_type_from_index(index);
283 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
285 rtnl->newlink(type, index, flags, 0);
288 if (rtnl->newgateway) {
289 const char *gateway =
290 __connman_ipconfig_get_gateway_from_index(index,
291 CONNMAN_IPCONFIG_TYPE_ALL);
294 rtnl->newgateway(index, gateway);
298 static GSList *rtnl_list = NULL;
300 static gint compare_priority(gconstpointer a, gconstpointer b)
302 const struct connman_rtnl *rtnl1 = a;
303 const struct connman_rtnl *rtnl2 = b;
305 return rtnl2->priority - rtnl1->priority;
309 * connman_rtnl_register:
312 * Register a new RTNL module
314 * Returns: %0 on success
316 int connman_rtnl_register(struct connman_rtnl *rtnl)
318 DBG("rtnl %p name %s", rtnl, rtnl->name);
320 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
323 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
329 * connman_rtnl_unregister:
332 * Remove a previously registered RTNL module
334 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
336 DBG("rtnl %p name %s", rtnl, rtnl->name);
338 rtnl_list = g_slist_remove(rtnl_list, rtnl);
341 static const char *operstate2str(unsigned char operstate)
344 case IF_OPER_UNKNOWN:
346 case IF_OPER_NOTPRESENT:
347 return "NOT-PRESENT";
350 case IF_OPER_LOWERLAYERDOWN:
351 return "LOWER-LAYER-DOWN";
352 case IF_OPER_TESTING:
354 case IF_OPER_DORMANT:
363 static bool extract_link(struct ifinfomsg *msg, int bytes,
364 struct ether_addr *address, const char **ifname,
365 unsigned int *mtu, unsigned char *operstate,
366 struct rtnl_link_stats *stats)
370 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
371 attr = RTA_NEXT(attr, bytes)) {
372 switch (attr->rta_type) {
375 memcpy(address, RTA_DATA(attr), ETH_ALEN);
379 *ifname = RTA_DATA(attr);
383 *mtu = *((unsigned int *) RTA_DATA(attr));
387 memcpy(stats, RTA_DATA(attr),
388 sizeof(struct rtnl_link_stats));
392 *operstate = *((unsigned char *) RTA_DATA(attr));
404 static void process_newlink(unsigned short type, int index, unsigned flags,
405 unsigned change, struct ifinfomsg *msg, int bytes)
407 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
408 struct rtnl_link_stats stats;
409 unsigned char operstate = 0xff;
410 struct interface_data *interface;
411 const char *ifname = NULL;
412 unsigned int mtu = 0;
413 char ident[13], str[18];
416 memset(&stats, 0, sizeof(stats));
417 if (!extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats))
420 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
421 address.ether_addr_octet[0],
422 address.ether_addr_octet[1],
423 address.ether_addr_octet[2],
424 address.ether_addr_octet[3],
425 address.ether_addr_octet[4],
426 address.ether_addr_octet[5]);
428 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
429 address.ether_addr_octet[0],
430 address.ether_addr_octet[1],
431 address.ether_addr_octet[2],
432 address.ether_addr_octet[3],
433 address.ether_addr_octet[4],
434 address.ether_addr_octet[5]);
436 if (flags & IFF_SLAVE) {
437 connman_info("%s {newlink} ignoring slave, index %d address %s",
444 case ARPHRD_LOOPBACK:
445 case ARPHDR_PHONET_PIPE:
448 __connman_ipconfig_newlink(index, type, flags,
453 connman_info("%s {newlink} index %d address %s mtu %u",
454 ifname, index, str, mtu);
456 if (operstate != 0xff)
457 connman_info("%s {newlink} index %d operstate %u <%s>",
458 ifname, index, operstate,
459 operstate2str(operstate));
461 interface = g_hash_table_lookup(interface_list, GINT_TO_POINTER(index));
463 interface = g_new0(struct interface_data, 1);
464 interface->index = index;
465 interface->ident = g_strdup(ident);
467 g_hash_table_insert(interface_list,
468 GINT_TO_POINTER(index), interface);
470 if (type == ARPHRD_ETHER)
471 read_uevent(interface);
475 for (list = rtnl_list; list; list = list->next) {
476 struct connman_rtnl *rtnl = list->data;
479 rtnl->newlink(type, index, flags, change);
483 * The interface needs to be added after the newlink call.
484 * The newlink will create the technology when needed and
485 * __connman_technology_add_interface() expects the
486 * technology to be there already.
489 __connman_technology_add_interface(interface->service_type,
490 interface->index, interface->ident);
492 for (list = watch_list; list; list = list->next) {
493 struct watch_data *watch = list->data;
495 if (watch->index != index)
499 watch->newlink(flags, change, watch->user_data);
503 static void process_dellink(unsigned short type, int index, unsigned flags,
504 unsigned change, struct ifinfomsg *msg, int bytes)
506 struct rtnl_link_stats stats;
507 unsigned char operstate = 0xff;
508 const char *ifname = NULL;
511 memset(&stats, 0, sizeof(stats));
512 if (!extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats))
515 if (operstate != 0xff)
516 connman_info("%s {dellink} index %d operstate %u <%s>",
517 ifname, index, operstate,
518 operstate2str(operstate));
520 for (list = rtnl_list; list; list = list->next) {
521 struct connman_rtnl *rtnl = list->data;
524 rtnl->dellink(type, index, flags, change);
529 case ARPHRD_LOOPBACK:
530 case ARPHDR_PHONET_PIPE:
533 __connman_ipconfig_dellink(index, &stats);
537 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
540 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
542 struct in_addr *local,
543 struct in_addr *address,
544 struct in_addr *broadcast)
548 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
549 attr = RTA_NEXT(attr, bytes)) {
550 switch (attr->rta_type) {
553 *address = *((struct in_addr *) RTA_DATA(attr));
557 *local = *((struct in_addr *) RTA_DATA(attr));
561 *broadcast = *((struct in_addr *) RTA_DATA(attr));
565 *label = RTA_DATA(attr);
571 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
572 struct in6_addr *addr,
573 struct in6_addr *local)
577 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
578 attr = RTA_NEXT(attr, bytes)) {
579 switch (attr->rta_type) {
582 *addr = *((struct in6_addr *) RTA_DATA(attr));
586 *local = *((struct in6_addr *) RTA_DATA(attr));
592 static void process_newaddr(unsigned char family, unsigned char prefixlen,
593 int index, struct ifaddrmsg *msg, int bytes)
595 struct in_addr ipv4_addr = { INADDR_ANY };
596 struct in6_addr ipv6_address, ipv6_local;
597 const char *label = NULL;
599 char ip_string[INET6_ADDRSTRLEN];
601 if (family == AF_INET) {
603 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
605 } else if (family == AF_INET6) {
606 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
607 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
615 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
618 if (__connman_ipconfig_newaddr(index, family, label,
619 prefixlen, ip_string) >= 0) {
620 if (family == AF_INET6) {
622 * Re-create RDNSS configured servers if there
623 * are any for this interface. This is done
624 * because we might have now properly
625 * configured interface with proper
626 * autoconfigured address.
628 __connman_resolver_redo_servers(index);
633 static void process_deladdr(unsigned char family, unsigned char prefixlen,
634 int index, struct ifaddrmsg *msg, int bytes)
636 struct in_addr ipv4_addr = { INADDR_ANY };
637 struct in6_addr ipv6_address, ipv6_local;
638 const char *label = NULL;
640 char ip_string[INET6_ADDRSTRLEN];
642 if (family == AF_INET) {
643 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
645 } else if (family == AF_INET6) {
646 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
647 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
655 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
658 __connman_ipconfig_deladdr(index, family, label,
659 prefixlen, ip_string);
662 static void extract_ipv4_route(struct rtmsg *msg, int bytes, int *index,
664 struct in_addr *gateway)
668 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
669 attr = RTA_NEXT(attr, bytes)) {
670 switch (attr->rta_type) {
673 *dst = *((struct in_addr *) RTA_DATA(attr));
677 *gateway = *((struct in_addr *) RTA_DATA(attr));
681 *index = *((int *) RTA_DATA(attr));
687 static void extract_ipv6_route(struct rtmsg *msg, int bytes, int *index,
688 struct in6_addr *dst,
689 struct in6_addr *gateway)
693 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
694 attr = RTA_NEXT(attr, bytes)) {
695 switch (attr->rta_type) {
698 *dst = *((struct in6_addr *) RTA_DATA(attr));
703 *((struct in6_addr *) RTA_DATA(attr));
707 *index = *((int *) RTA_DATA(attr));
713 static void process_newroute(unsigned char family, unsigned char scope,
714 struct rtmsg *msg, int bytes)
717 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
720 if (family == AF_INET) {
721 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
723 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
725 inet_ntop(family, &dst, dststr, sizeof(dststr));
726 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
728 __connman_ipconfig_newroute(index, family, scope, dststr,
731 /* skip host specific routes */
732 if (scope != RT_SCOPE_UNIVERSE &&
733 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
736 if (dst.s_addr != INADDR_ANY)
739 } else if (family == AF_INET6) {
740 struct in6_addr dst = IN6ADDR_ANY_INIT,
741 gateway = IN6ADDR_ANY_INIT;
743 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
745 inet_ntop(family, &dst, dststr, sizeof(dststr));
746 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
748 __connman_ipconfig_newroute(index, family, scope, dststr,
751 /* skip host specific routes */
752 if (scope != RT_SCOPE_UNIVERSE &&
753 !(scope == RT_SCOPE_LINK &&
754 IN6_IS_ADDR_UNSPECIFIED(&dst)))
757 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
762 for (list = rtnl_list; list; list = list->next) {
763 struct connman_rtnl *rtnl = list->data;
765 if (rtnl->newgateway)
766 rtnl->newgateway(index, gatewaystr);
770 static void process_delroute(unsigned char family, unsigned char scope,
771 struct rtmsg *msg, int bytes)
774 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
777 if (family == AF_INET) {
778 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
780 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
782 inet_ntop(family, &dst, dststr, sizeof(dststr));
783 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
785 __connman_ipconfig_delroute(index, family, scope, dststr,
788 /* skip host specific routes */
789 if (scope != RT_SCOPE_UNIVERSE &&
790 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
793 if (dst.s_addr != INADDR_ANY)
796 } else if (family == AF_INET6) {
797 struct in6_addr dst = IN6ADDR_ANY_INIT,
798 gateway = IN6ADDR_ANY_INIT;
800 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
802 inet_ntop(family, &dst, dststr, sizeof(dststr));
803 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
805 __connman_ipconfig_delroute(index, family, scope, dststr,
808 /* skip host specific routes */
809 if (scope != RT_SCOPE_UNIVERSE &&
810 !(scope == RT_SCOPE_LINK &&
811 IN6_IS_ADDR_UNSPECIFIED(&dst)))
814 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
819 for (list = rtnl_list; list; list = list->next) {
820 struct connman_rtnl *rtnl = list->data;
822 if (rtnl->delgateway)
823 rtnl->delgateway(index, gatewaystr);
827 static inline void print_ether(struct rtattr *attr, const char *name)
829 int len = (int) RTA_PAYLOAD(attr);
831 if (len == ETH_ALEN) {
832 struct ether_addr eth;
833 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
834 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
836 print(" attr %s (len %d)\n", name, len);
839 static inline void print_inet(struct rtattr *attr, const char *name,
840 unsigned char family)
842 int len = (int) RTA_PAYLOAD(attr);
844 if (family == AF_INET && len == sizeof(struct in_addr)) {
846 addr = *((struct in_addr *) RTA_DATA(attr));
847 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
849 print(" attr %s (len %d)\n", name, len);
852 static inline void print_string(struct rtattr *attr, const char *name)
854 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
855 (char *) RTA_DATA(attr));
858 static inline void print_byte(struct rtattr *attr, const char *name)
860 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
861 *((unsigned char *) RTA_DATA(attr)));
864 static inline void print_integer(struct rtattr *attr, const char *name)
866 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
867 *((int *) RTA_DATA(attr)));
870 static inline void print_attr(struct rtattr *attr, const char *name)
872 int len = (int) RTA_PAYLOAD(attr);
875 print(" attr %s (len %d)\n", name, len);
877 print(" attr %d (len %d)\n", attr->rta_type, len);
880 static void rtnl_link(struct nlmsghdr *hdr)
882 struct ifinfomsg *msg;
886 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
887 bytes = IFLA_PAYLOAD(hdr);
889 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
891 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
892 attr = RTA_NEXT(attr, bytes)) {
893 switch (attr->rta_type) {
895 print_ether(attr, "address");
898 print_ether(attr, "broadcast");
901 print_string(attr, "ifname");
904 print_integer(attr, "mtu");
907 print_attr(attr, "link");
910 print_attr(attr, "qdisc");
913 print_attr(attr, "stats");
916 print_attr(attr, "cost");
919 print_attr(attr, "priority");
922 print_attr(attr, "master");
925 print_attr(attr, "wireless");
928 print_attr(attr, "protinfo");
931 print_integer(attr, "txqlen");
934 print_attr(attr, "map");
937 print_attr(attr, "weight");
940 print_byte(attr, "operstate");
943 print_byte(attr, "linkmode");
946 print_attr(attr, NULL);
952 static void rtnl_newlink(struct nlmsghdr *hdr)
954 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
958 if (hdr->nlmsg_type == IFLA_WIRELESS)
959 connman_warn_once("Obsolete WEXT WiFi driver detected");
961 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
962 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
965 static void rtnl_dellink(struct nlmsghdr *hdr)
967 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
971 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
972 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
975 static void rtnl_addr(struct nlmsghdr *hdr)
977 struct ifaddrmsg *msg;
981 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
982 bytes = IFA_PAYLOAD(hdr);
984 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
986 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
987 attr = RTA_NEXT(attr, bytes)) {
988 switch (attr->rta_type) {
990 print_inet(attr, "address", msg->ifa_family);
993 print_inet(attr, "local", msg->ifa_family);
996 print_string(attr, "label");
999 print_inet(attr, "broadcast", msg->ifa_family);
1002 print_attr(attr, "anycast");
1005 print_attr(attr, "cacheinfo");
1008 print_attr(attr, "multicast");
1011 print_attr(attr, NULL);
1017 static void rtnl_newaddr(struct nlmsghdr *hdr)
1019 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1023 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1024 msg, IFA_PAYLOAD(hdr));
1027 static void rtnl_deladdr(struct nlmsghdr *hdr)
1029 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1033 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1034 msg, IFA_PAYLOAD(hdr));
1037 static void rtnl_route(struct nlmsghdr *hdr)
1040 struct rtattr *attr;
1043 msg = (struct rtmsg *) NLMSG_DATA(hdr);
1044 bytes = RTM_PAYLOAD(hdr);
1046 print("rtm_family %d rtm_table %d rtm_protocol %d",
1047 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
1048 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
1049 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
1051 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
1052 attr = RTA_NEXT(attr, bytes)) {
1053 switch (attr->rta_type) {
1055 print_inet(attr, "dst", msg->rtm_family);
1058 print_inet(attr, "src", msg->rtm_family);
1061 print_string(attr, "iif");
1064 print_integer(attr, "oif");
1067 print_inet(attr, "gateway", msg->rtm_family);
1070 print_attr(attr, "priority");
1073 print_inet(attr, "prefsrc", msg->rtm_family);
1076 print_attr(attr, "metrics");
1079 print_integer(attr, "table");
1082 print_attr(attr, NULL);
1088 static bool is_route_rtmsg(struct rtmsg *msg)
1090 if (msg->rtm_flags & RTM_F_CLONED)
1093 if (msg->rtm_table != RT_TABLE_MAIN)
1096 if (msg->rtm_protocol != RTPROT_BOOT &&
1097 msg->rtm_protocol != RTPROT_KERNEL)
1100 if (msg->rtm_type != RTN_UNICAST)
1106 static void rtnl_newroute(struct nlmsghdr *hdr)
1108 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1112 if (is_route_rtmsg(msg))
1113 process_newroute(msg->rtm_family, msg->rtm_scope,
1114 msg, RTM_PAYLOAD(hdr));
1117 static void rtnl_delroute(struct nlmsghdr *hdr)
1119 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1123 if (is_route_rtmsg(msg))
1124 process_delroute(msg->rtm_family, msg->rtm_scope,
1125 msg, RTM_PAYLOAD(hdr));
1128 static void *rtnl_nd_opt_rdnss(struct nd_opt_hdr *opt, guint32 *lifetime,
1131 guint32 *optint = (void *)opt;
1133 if (opt->nd_opt_len < 3)
1136 if (*lifetime > ntohl(optint[1]))
1137 *lifetime = ntohl(optint[1]);
1139 /* nd_opt_len is in units of 8 bytes. The header is 1 unit (8 bytes)
1140 and each address is another 2 units (16 bytes).
1141 So the number of addresses (given rounding) is nd_opt_len/2 */
1142 *nr_servers = opt->nd_opt_len / 2;
1144 /* And they start 8 bytes into the packet, or two guint32s in. */
1148 static const char **rtnl_nd_opt_dnssl(struct nd_opt_hdr *opt, guint32 *lifetime)
1150 const char **domains = NULL;
1151 guint32 *optint = (void *)opt;
1152 unsigned char *optc = (void *)&optint[2];
1153 int data_len = (opt->nd_opt_len * 8) - 8;
1157 if (*lifetime > ntohl(optint[1]))
1158 *lifetime = ntohl(optint[1]);
1160 /* Turn it into normal strings by converting the length bytes into '.',
1161 and count how many search domains there are while we're at it. */
1163 while (i < data_len) {
1164 if (optc[i] > 0x3f) {
1165 DBG("DNSSL contains compressed elements in violation of RFC6106");
1172 /* Check for double zero */
1173 if (i < data_len && optc[i] == 0)
1181 if (i >= data_len) {
1182 DBG("DNSSL data overflows option length");
1189 domains = g_try_new0(const char *, nr_domains + 1);
1193 /* Now point to the normal strings, missing out the leading '.' that
1194 each of them will have now. */
1195 for (i = 0; i < nr_domains; i++) {
1196 domains[i] = (char *)optc + 1;
1197 optc += strlen((char *)optc) + 1;
1203 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1205 struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1206 struct nd_opt_hdr *opt;
1207 guint32 lifetime = -1;
1208 const char **domains = NULL;
1209 struct in6_addr *servers = NULL;
1210 int i, nr_servers = 0;
1211 int msglen = msg->nduseropt_opts_len;
1214 DBG("family %d index %d len %d type %d code %d",
1215 msg->nduseropt_family, msg->nduseropt_ifindex,
1216 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1217 msg->nduseropt_icmp_code);
1219 if (msg->nduseropt_family != AF_INET6 ||
1220 msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1221 msg->nduseropt_icmp_code != 0)
1224 index = msg->nduseropt_ifindex;
1228 for (opt = (void *)&msg[1];
1230 msglen -= opt->nd_opt_len * 8,
1231 opt = ((void *)opt) + opt->nd_opt_len*8) {
1233 DBG("remaining %d nd opt type %d len %d\n",
1234 msglen, opt->nd_opt_type, opt->nd_opt_len);
1236 if (opt->nd_opt_type == 25) { /* ND_OPT_RDNSS */
1239 servers = rtnl_nd_opt_rdnss(opt, &lifetime,
1241 for (i = 0; i < nr_servers; i++) {
1242 if (!inet_ntop(AF_INET6, servers + i, buf,
1246 connman_resolver_append_lifetime(index,
1247 NULL, buf, lifetime);
1250 } else if (opt->nd_opt_type == 31) { /* ND_OPT_DNSSL */
1253 domains = rtnl_nd_opt_dnssl(opt, &lifetime);
1254 for (i = 0; domains && domains[i]; i++)
1255 connman_resolver_append_lifetime(index,
1256 domains[i], NULL, lifetime);
1263 static const char *type2string(uint16_t type)
1292 case RTM_NEWNDUSEROPT:
1293 return "NEWNDUSEROPT";
1299 static GIOChannel *channel = NULL;
1300 static guint channel_watch = 0;
1302 struct rtnl_request {
1303 struct nlmsghdr hdr;
1304 struct rtgenmsg msg;
1306 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1308 static GSList *request_list = NULL;
1309 static guint32 request_seq = 0;
1311 static struct rtnl_request *find_request(guint32 seq)
1315 for (list = request_list; list; list = list->next) {
1316 struct rtnl_request *req = list->data;
1318 if (req->hdr.nlmsg_seq == seq)
1325 static int send_request(struct rtnl_request *req)
1327 struct sockaddr_nl addr;
1330 DBG("%s len %d type %d flags 0x%04x seq %d",
1331 type2string(req->hdr.nlmsg_type),
1332 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1333 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1335 sk = g_io_channel_unix_get_fd(channel);
1337 memset(&addr, 0, sizeof(addr));
1338 addr.nl_family = AF_NETLINK;
1340 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1341 (struct sockaddr *) &addr, sizeof(addr));
1344 static int queue_request(struct rtnl_request *req)
1346 request_list = g_slist_append(request_list, req);
1348 if (g_slist_length(request_list) > 1)
1351 return send_request(req);
1354 static int process_response(guint32 seq)
1356 struct rtnl_request *req;
1360 req = find_request(seq);
1362 request_list = g_slist_remove(request_list, req);
1366 req = g_slist_nth_data(request_list, 0);
1370 return send_request(req);
1373 static void rtnl_message(void *buf, size_t len)
1375 DBG("buf %p len %zd", buf, len);
1378 struct nlmsghdr *hdr = buf;
1379 struct nlmsgerr *err;
1381 if (!NLMSG_OK(hdr, len))
1384 DBG("%s len %d type %d flags 0x%04x seq %d pid %d",
1385 type2string(hdr->nlmsg_type),
1386 hdr->nlmsg_len, hdr->nlmsg_type,
1387 hdr->nlmsg_flags, hdr->nlmsg_seq,
1390 switch (hdr->nlmsg_type) {
1395 process_response(hdr->nlmsg_seq);
1398 err = NLMSG_DATA(hdr);
1399 DBG("error %d (%s)", -err->error,
1400 strerror(-err->error));
1420 case RTM_NEWNDUSEROPT:
1421 rtnl_newnduseropt(hdr);
1425 len -= hdr->nlmsg_len;
1426 buf += hdr->nlmsg_len;
1430 static gboolean netlink_event(GIOChannel *chan, GIOCondition cond, gpointer data)
1432 unsigned char buf[4096];
1433 struct sockaddr_nl nladdr;
1434 socklen_t addr_len = sizeof(nladdr);
1438 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1441 memset(buf, 0, sizeof(buf));
1442 memset(&nladdr, 0, sizeof(nladdr));
1444 fd = g_io_channel_unix_get_fd(chan);
1446 status = recvfrom(fd, buf, sizeof(buf), 0,
1447 (struct sockaddr *) &nladdr, &addr_len);
1449 if (errno == EINTR || errno == EAGAIN)
1458 if (nladdr.nl_pid != 0) { /* not sent by kernel, ignore */
1459 DBG("Received msg from %u, ignoring it", nladdr.nl_pid);
1463 rtnl_message(buf, status);
1468 static int send_getlink(void)
1470 struct rtnl_request *req;
1474 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1478 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1479 req->hdr.nlmsg_type = RTM_GETLINK;
1480 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1481 req->hdr.nlmsg_pid = 0;
1482 req->hdr.nlmsg_seq = request_seq++;
1483 req->msg.rtgen_family = AF_INET;
1485 return queue_request(req);
1488 static int send_getaddr(void)
1490 struct rtnl_request *req;
1494 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1498 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1499 req->hdr.nlmsg_type = RTM_GETADDR;
1500 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1501 req->hdr.nlmsg_pid = 0;
1502 req->hdr.nlmsg_seq = request_seq++;
1503 req->msg.rtgen_family = AF_INET;
1505 return queue_request(req);
1508 static int send_getroute(void)
1510 struct rtnl_request *req;
1514 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1518 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1519 req->hdr.nlmsg_type = RTM_GETROUTE;
1520 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1521 req->hdr.nlmsg_pid = 0;
1522 req->hdr.nlmsg_seq = request_seq++;
1523 req->msg.rtgen_family = AF_INET;
1525 return queue_request(req);
1528 static gboolean update_timeout_cb(gpointer user_data)
1530 __connman_rtnl_request_update();
1535 static void update_interval_callback(guint min)
1537 if (update_timeout > 0)
1538 g_source_remove(update_timeout);
1540 if (min < G_MAXUINT) {
1541 update_interval = min;
1542 update_timeout = g_timeout_add_seconds(update_interval,
1543 update_timeout_cb, NULL);
1546 update_interval = G_MAXUINT;
1550 static gint compare_interval(gconstpointer a, gconstpointer b)
1552 guint val_a = GPOINTER_TO_UINT(a);
1553 guint val_b = GPOINTER_TO_UINT(b);
1555 return val_a - val_b;
1558 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1565 update_list = g_slist_insert_sorted(update_list,
1566 GUINT_TO_POINTER(interval), compare_interval);
1568 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1569 if (min < update_interval) {
1570 update_interval_callback(min);
1571 __connman_rtnl_request_update();
1574 return update_interval;
1577 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1579 guint min = G_MAXUINT;
1584 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1587 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1589 if (min > update_interval)
1590 update_interval_callback(min);
1595 int __connman_rtnl_request_update(void)
1597 return send_getlink();
1600 int __connman_rtnl_init(void)
1602 struct sockaddr_nl addr;
1607 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1608 NULL, free_interface);
1610 sk = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
1614 memset(&addr, 0, sizeof(addr));
1615 addr.nl_family = AF_NETLINK;
1616 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1617 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1618 (1<<(RTNLGRP_ND_USEROPT-1));
1620 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1625 channel = g_io_channel_unix_new(sk);
1626 g_io_channel_set_close_on_unref(channel, TRUE);
1628 g_io_channel_set_encoding(channel, NULL, NULL);
1629 g_io_channel_set_buffered(channel, FALSE);
1631 channel_watch = g_io_add_watch(channel,
1632 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1633 netlink_event, NULL);
1638 void __connman_rtnl_start(void)
1647 void __connman_rtnl_cleanup(void)
1653 for (list = watch_list; list; list = list->next) {
1654 struct watch_data *watch = list->data;
1656 DBG("removing watch %d", watch->id);
1662 g_slist_free(watch_list);
1665 g_slist_free(update_list);
1668 for (list = request_list; list; list = list->next) {
1669 struct rtnl_request *req = list->data;
1671 DBG("%s len %d type %d flags 0x%04x seq %d",
1672 type2string(req->hdr.nlmsg_type),
1673 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1674 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1680 g_slist_free(request_list);
1681 request_list = NULL;
1683 if (channel_watch) {
1684 g_source_remove(channel_watch);
1688 g_io_channel_shutdown(channel, TRUE, NULL);
1689 g_io_channel_unref(channel);
1693 g_hash_table_destroy(interface_list);