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;
176 } else if (strcmp(line + 8, "bond") == 0) {
177 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
178 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
180 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
181 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
190 /* We haven't got a DEVTYPE, let's check if it's a wireless device */
191 if (wext_interface(name)) {
192 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
193 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
195 connman_error("%s runs an unsupported 802.11 driver", name);
202 enum connman_device_type __connman_rtnl_get_device_type(int index)
204 struct interface_data *interface;
206 interface = g_hash_table_lookup(interface_list,
207 GINT_TO_POINTER(index));
209 return CONNMAN_DEVICE_TYPE_UNKNOWN;
211 return interface->device_type;
215 * connman_rtnl_add_newlink_watch:
216 * @index: network device index
217 * @callback: callback function
218 * @user_data: callback data;
220 * Add a new RTNL watch for newlink events
222 * Returns: %0 on failure and a unique id on success
224 unsigned int connman_rtnl_add_newlink_watch(int index,
225 connman_rtnl_link_cb_t callback, void *user_data)
227 struct watch_data *watch;
229 watch = g_try_new0(struct watch_data, 1);
233 watch->id = ++watch_id;
234 watch->index = index;
236 watch->newlink = callback;
237 watch->user_data = user_data;
239 watch_list = g_slist_prepend(watch_list, watch);
241 DBG("id %d", watch->id);
244 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
247 callback(flags, 0, user_data);
254 * connman_rtnl_remove_watch:
255 * @id: watch identifier
257 * Remove the RTNL watch for the identifier
259 void connman_rtnl_remove_watch(unsigned int id)
268 for (list = watch_list; list; list = list->next) {
269 struct watch_data *watch = list->data;
271 if (watch->id == id) {
272 watch_list = g_slist_remove(watch_list, watch);
279 static void trigger_rtnl(int index, void *user_data)
281 struct connman_rtnl *rtnl = user_data;
284 unsigned short type = __connman_ipconfig_get_type_from_index(index);
285 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
287 rtnl->newlink(type, index, flags, 0);
290 if (rtnl->newgateway) {
291 const char *gateway =
292 __connman_ipconfig_get_gateway_from_index(index,
293 CONNMAN_IPCONFIG_TYPE_ALL);
296 rtnl->newgateway(index, gateway);
300 static GSList *rtnl_list = NULL;
302 static gint compare_priority(gconstpointer a, gconstpointer b)
304 const struct connman_rtnl *rtnl1 = a;
305 const struct connman_rtnl *rtnl2 = b;
307 return rtnl2->priority - rtnl1->priority;
311 * connman_rtnl_register:
314 * Register a new RTNL module
316 * Returns: %0 on success
318 int connman_rtnl_register(struct connman_rtnl *rtnl)
320 DBG("rtnl %p name %s", rtnl, rtnl->name);
322 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
325 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
331 * connman_rtnl_unregister:
334 * Remove a previously registered RTNL module
336 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
338 DBG("rtnl %p name %s", rtnl, rtnl->name);
340 rtnl_list = g_slist_remove(rtnl_list, rtnl);
343 static const char *operstate2str(unsigned char operstate)
346 case IF_OPER_UNKNOWN:
348 case IF_OPER_NOTPRESENT:
349 return "NOT-PRESENT";
352 case IF_OPER_LOWERLAYERDOWN:
353 return "LOWER-LAYER-DOWN";
354 case IF_OPER_TESTING:
356 case IF_OPER_DORMANT:
365 static bool extract_link(struct ifinfomsg *msg, int bytes,
366 struct ether_addr *address, const char **ifname,
367 unsigned int *mtu, unsigned char *operstate,
368 struct rtnl_link_stats *stats)
372 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
373 attr = RTA_NEXT(attr, bytes)) {
374 switch (attr->rta_type) {
377 memcpy(address, RTA_DATA(attr), ETH_ALEN);
381 *ifname = RTA_DATA(attr);
385 *mtu = *((unsigned int *) RTA_DATA(attr));
389 memcpy(stats, RTA_DATA(attr),
390 sizeof(struct rtnl_link_stats));
394 *operstate = *((unsigned char *) RTA_DATA(attr));
406 static void process_newlink(unsigned short type, int index, unsigned flags,
407 unsigned change, struct ifinfomsg *msg, int bytes)
409 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
410 struct rtnl_link_stats stats;
411 unsigned char operstate = 0xff;
412 struct interface_data *interface;
413 const char *ifname = NULL;
414 unsigned int mtu = 0;
415 char ident[13], str[18];
418 memset(&stats, 0, sizeof(stats));
419 if (!extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats))
422 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
423 address.ether_addr_octet[0],
424 address.ether_addr_octet[1],
425 address.ether_addr_octet[2],
426 address.ether_addr_octet[3],
427 address.ether_addr_octet[4],
428 address.ether_addr_octet[5]);
430 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
431 address.ether_addr_octet[0],
432 address.ether_addr_octet[1],
433 address.ether_addr_octet[2],
434 address.ether_addr_octet[3],
435 address.ether_addr_octet[4],
436 address.ether_addr_octet[5]);
438 if (flags & IFF_SLAVE) {
439 connman_info("%s {newlink} ignoring slave, index %d address %s",
446 case ARPHRD_LOOPBACK:
447 case ARPHDR_PHONET_PIPE:
450 __connman_ipconfig_newlink(index, type, flags,
455 connman_info("%s {newlink} index %d address %s mtu %u",
456 ifname, index, str, mtu);
458 if (operstate != 0xff)
459 connman_info("%s {newlink} index %d operstate %u <%s>",
460 ifname, index, operstate,
461 operstate2str(operstate));
463 interface = g_hash_table_lookup(interface_list, GINT_TO_POINTER(index));
465 interface = g_new0(struct interface_data, 1);
466 interface->index = index;
467 interface->ident = g_strdup(ident);
469 g_hash_table_insert(interface_list,
470 GINT_TO_POINTER(index), interface);
472 if (type == ARPHRD_ETHER)
473 read_uevent(interface);
474 } else if (type == ARPHRD_ETHER && interface->device_type == CONNMAN_DEVICE_TYPE_UNKNOWN)
475 read_uevent(interface);
479 for (list = rtnl_list; list; list = list->next) {
480 struct connman_rtnl *rtnl = list->data;
483 rtnl->newlink(type, index, flags, change);
487 * The interface needs to be added after the newlink call.
488 * The newlink will create the technology when needed and
489 * __connman_technology_add_interface() expects the
490 * technology to be there already.
493 __connman_technology_add_interface(interface->service_type,
494 interface->index, interface->ident);
496 for (list = watch_list; list; list = list->next) {
497 struct watch_data *watch = list->data;
499 if (watch->index != index)
503 watch->newlink(flags, change, watch->user_data);
507 static void process_dellink(unsigned short type, int index, unsigned flags,
508 unsigned change, struct ifinfomsg *msg, int bytes)
510 struct rtnl_link_stats stats;
511 unsigned char operstate = 0xff;
512 const char *ifname = NULL;
515 memset(&stats, 0, sizeof(stats));
516 if (!extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats))
519 if (operstate != 0xff)
520 connman_info("%s {dellink} index %d operstate %u <%s>",
521 ifname, index, operstate,
522 operstate2str(operstate));
524 for (list = rtnl_list; list; list = list->next) {
525 struct connman_rtnl *rtnl = list->data;
528 rtnl->dellink(type, index, flags, change);
533 case ARPHRD_LOOPBACK:
534 case ARPHDR_PHONET_PIPE:
537 __connman_ipconfig_dellink(index, &stats);
541 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
544 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
546 struct in_addr *local,
547 struct in_addr *address,
548 struct in_addr *broadcast)
552 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
553 attr = RTA_NEXT(attr, bytes)) {
554 switch (attr->rta_type) {
557 *address = *((struct in_addr *) RTA_DATA(attr));
561 *local = *((struct in_addr *) RTA_DATA(attr));
565 *broadcast = *((struct in_addr *) RTA_DATA(attr));
569 *label = RTA_DATA(attr);
575 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
576 struct in6_addr *addr,
577 struct in6_addr *local)
581 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
582 attr = RTA_NEXT(attr, bytes)) {
583 switch (attr->rta_type) {
586 *addr = *((struct in6_addr *) RTA_DATA(attr));
590 *local = *((struct in6_addr *) RTA_DATA(attr));
596 static void process_newaddr(unsigned char family, unsigned char prefixlen,
597 int index, struct ifaddrmsg *msg, int bytes)
599 struct in_addr ipv4_addr = { INADDR_ANY };
600 struct in6_addr ipv6_address, ipv6_local;
601 const char *label = NULL;
603 char ip_string[INET6_ADDRSTRLEN];
605 if (family == AF_INET) {
607 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
609 } else if (family == AF_INET6) {
610 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
611 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
619 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
622 if (__connman_ipconfig_newaddr(index, family, label,
623 prefixlen, ip_string) >= 0) {
624 if (family == AF_INET6) {
626 * Re-create RDNSS configured servers if there
627 * are any for this interface. This is done
628 * because we might have now properly
629 * configured interface with proper
630 * autoconfigured address.
632 __connman_resolver_redo_servers(index);
637 static void process_deladdr(unsigned char family, unsigned char prefixlen,
638 int index, struct ifaddrmsg *msg, int bytes)
640 struct in_addr ipv4_addr = { INADDR_ANY };
641 struct in6_addr ipv6_address, ipv6_local;
642 const char *label = NULL;
644 char ip_string[INET6_ADDRSTRLEN];
646 if (family == AF_INET) {
647 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
649 } else if (family == AF_INET6) {
650 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
651 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
659 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
662 __connman_ipconfig_deladdr(index, family, label,
663 prefixlen, ip_string);
666 static void extract_ipv4_route(struct rtmsg *msg, int bytes, int *index,
668 struct in_addr *gateway)
672 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
673 attr = RTA_NEXT(attr, bytes)) {
674 switch (attr->rta_type) {
677 *dst = *((struct in_addr *) RTA_DATA(attr));
681 *gateway = *((struct in_addr *) RTA_DATA(attr));
685 *index = *((int *) RTA_DATA(attr));
691 static void extract_ipv6_route(struct rtmsg *msg, int bytes, int *index,
692 struct in6_addr *dst,
693 struct in6_addr *gateway)
697 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
698 attr = RTA_NEXT(attr, bytes)) {
699 switch (attr->rta_type) {
702 *dst = *((struct in6_addr *) RTA_DATA(attr));
707 *((struct in6_addr *) RTA_DATA(attr));
711 *index = *((int *) RTA_DATA(attr));
717 static void process_newroute(unsigned char family, unsigned char scope,
718 struct rtmsg *msg, int bytes)
721 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
724 if (family == AF_INET) {
725 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
727 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
729 inet_ntop(family, &dst, dststr, sizeof(dststr));
730 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
732 __connman_ipconfig_newroute(index, family, scope, dststr,
735 /* skip host specific routes */
736 if (scope != RT_SCOPE_UNIVERSE &&
737 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
740 if (dst.s_addr != INADDR_ANY)
743 } else if (family == AF_INET6) {
744 struct in6_addr dst = IN6ADDR_ANY_INIT,
745 gateway = IN6ADDR_ANY_INIT;
747 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
749 inet_ntop(family, &dst, dststr, sizeof(dststr));
750 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
752 __connman_ipconfig_newroute(index, family, scope, dststr,
755 /* skip host specific routes */
756 if (scope != RT_SCOPE_UNIVERSE &&
757 !(scope == RT_SCOPE_LINK &&
758 IN6_IS_ADDR_UNSPECIFIED(&dst)))
761 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
766 for (list = rtnl_list; list; list = list->next) {
767 struct connman_rtnl *rtnl = list->data;
769 if (rtnl->newgateway)
770 rtnl->newgateway(index, gatewaystr);
774 static void process_delroute(unsigned char family, unsigned char scope,
775 struct rtmsg *msg, int bytes)
778 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
781 if (family == AF_INET) {
782 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
784 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
786 inet_ntop(family, &dst, dststr, sizeof(dststr));
787 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
789 __connman_ipconfig_delroute(index, family, scope, dststr,
792 /* skip host specific routes */
793 if (scope != RT_SCOPE_UNIVERSE &&
794 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
797 if (dst.s_addr != INADDR_ANY)
800 } else if (family == AF_INET6) {
801 struct in6_addr dst = IN6ADDR_ANY_INIT,
802 gateway = IN6ADDR_ANY_INIT;
804 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
806 inet_ntop(family, &dst, dststr, sizeof(dststr));
807 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
809 __connman_ipconfig_delroute(index, family, scope, dststr,
812 /* skip host specific routes */
813 if (scope != RT_SCOPE_UNIVERSE &&
814 !(scope == RT_SCOPE_LINK &&
815 IN6_IS_ADDR_UNSPECIFIED(&dst)))
818 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
823 for (list = rtnl_list; list; list = list->next) {
824 struct connman_rtnl *rtnl = list->data;
826 if (rtnl->delgateway)
827 rtnl->delgateway(index, gatewaystr);
831 static inline void print_ether(struct rtattr *attr, const char *name)
833 int len = (int) RTA_PAYLOAD(attr);
835 if (len == ETH_ALEN) {
836 struct ether_addr eth;
837 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
838 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
840 print(" attr %s (len %d)\n", name, len);
843 static inline void print_inet(struct rtattr *attr, const char *name,
844 unsigned char family)
846 int len = (int) RTA_PAYLOAD(attr);
848 if (family == AF_INET && len == sizeof(struct in_addr)) {
850 addr = *((struct in_addr *) RTA_DATA(attr));
851 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
853 print(" attr %s (len %d)\n", name, len);
856 static inline void print_string(struct rtattr *attr, const char *name)
858 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
859 (char *) RTA_DATA(attr));
862 static inline void print_byte(struct rtattr *attr, const char *name)
864 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
865 *((unsigned char *) RTA_DATA(attr)));
868 static inline void print_integer(struct rtattr *attr, const char *name)
870 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
871 *((int *) RTA_DATA(attr)));
874 static inline void print_attr(struct rtattr *attr, const char *name)
876 int len = (int) RTA_PAYLOAD(attr);
879 print(" attr %s (len %d)\n", name, len);
881 print(" attr %d (len %d)\n", attr->rta_type, len);
884 static void rtnl_link(struct nlmsghdr *hdr)
886 struct ifinfomsg *msg;
890 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
891 bytes = IFLA_PAYLOAD(hdr);
893 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
895 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
896 attr = RTA_NEXT(attr, bytes)) {
897 switch (attr->rta_type) {
899 print_ether(attr, "address");
902 print_ether(attr, "broadcast");
905 print_string(attr, "ifname");
908 print_integer(attr, "mtu");
911 print_attr(attr, "link");
914 print_attr(attr, "qdisc");
917 print_attr(attr, "stats");
920 print_attr(attr, "cost");
923 print_attr(attr, "priority");
926 print_attr(attr, "master");
929 print_attr(attr, "wireless");
932 print_attr(attr, "protinfo");
935 print_integer(attr, "txqlen");
938 print_attr(attr, "map");
941 print_attr(attr, "weight");
944 print_byte(attr, "operstate");
947 print_byte(attr, "linkmode");
950 print_attr(attr, NULL);
956 static void rtnl_newlink(struct nlmsghdr *hdr)
958 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
962 if (hdr->nlmsg_type == IFLA_WIRELESS)
963 connman_warn_once("Obsolete WEXT WiFi driver detected");
965 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
966 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
969 static void rtnl_dellink(struct nlmsghdr *hdr)
971 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
975 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
976 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
979 static void rtnl_addr(struct nlmsghdr *hdr)
981 struct ifaddrmsg *msg;
985 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
986 bytes = IFA_PAYLOAD(hdr);
988 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
990 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
991 attr = RTA_NEXT(attr, bytes)) {
992 switch (attr->rta_type) {
994 print_inet(attr, "address", msg->ifa_family);
997 print_inet(attr, "local", msg->ifa_family);
1000 print_string(attr, "label");
1003 print_inet(attr, "broadcast", msg->ifa_family);
1006 print_attr(attr, "anycast");
1009 print_attr(attr, "cacheinfo");
1012 print_attr(attr, "multicast");
1015 print_attr(attr, NULL);
1021 static void rtnl_newaddr(struct nlmsghdr *hdr)
1023 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1027 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1028 msg, IFA_PAYLOAD(hdr));
1031 static void rtnl_deladdr(struct nlmsghdr *hdr)
1033 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1037 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1038 msg, IFA_PAYLOAD(hdr));
1041 static void rtnl_route(struct nlmsghdr *hdr)
1044 struct rtattr *attr;
1047 msg = (struct rtmsg *) NLMSG_DATA(hdr);
1048 bytes = RTM_PAYLOAD(hdr);
1050 print("rtm_family %d rtm_table %d rtm_protocol %d",
1051 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
1052 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
1053 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
1055 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
1056 attr = RTA_NEXT(attr, bytes)) {
1057 switch (attr->rta_type) {
1059 print_inet(attr, "dst", msg->rtm_family);
1062 print_inet(attr, "src", msg->rtm_family);
1065 print_string(attr, "iif");
1068 print_integer(attr, "oif");
1071 print_inet(attr, "gateway", msg->rtm_family);
1074 print_attr(attr, "priority");
1077 print_inet(attr, "prefsrc", msg->rtm_family);
1080 print_attr(attr, "metrics");
1083 print_integer(attr, "table");
1086 print_attr(attr, NULL);
1092 static bool is_route_rtmsg(struct rtmsg *msg)
1094 if (msg->rtm_flags & RTM_F_CLONED)
1097 if (msg->rtm_table != RT_TABLE_MAIN)
1100 if (msg->rtm_protocol != RTPROT_BOOT &&
1101 msg->rtm_protocol != RTPROT_KERNEL)
1104 if (msg->rtm_type != RTN_UNICAST)
1110 static void rtnl_newroute(struct nlmsghdr *hdr)
1112 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1116 if (is_route_rtmsg(msg))
1117 process_newroute(msg->rtm_family, msg->rtm_scope,
1118 msg, RTM_PAYLOAD(hdr));
1121 static void rtnl_delroute(struct nlmsghdr *hdr)
1123 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1127 if (is_route_rtmsg(msg))
1128 process_delroute(msg->rtm_family, msg->rtm_scope,
1129 msg, RTM_PAYLOAD(hdr));
1132 static void *rtnl_nd_opt_rdnss(struct nd_opt_hdr *opt, guint32 *lifetime,
1135 guint32 *optint = (void *)opt;
1137 if (opt->nd_opt_len < 3)
1140 if (*lifetime > ntohl(optint[1]))
1141 *lifetime = ntohl(optint[1]);
1143 /* nd_opt_len is in units of 8 bytes. The header is 1 unit (8 bytes)
1144 and each address is another 2 units (16 bytes).
1145 So the number of addresses (given rounding) is nd_opt_len/2 */
1146 *nr_servers = opt->nd_opt_len / 2;
1148 /* And they start 8 bytes into the packet, or two guint32s in. */
1152 static const char **rtnl_nd_opt_dnssl(struct nd_opt_hdr *opt, guint32 *lifetime)
1154 const char **domains = NULL;
1155 guint32 *optint = (void *)opt;
1156 unsigned char *optc = (void *)&optint[2];
1157 int data_len = (opt->nd_opt_len * 8) - 8;
1161 if (*lifetime > ntohl(optint[1]))
1162 *lifetime = ntohl(optint[1]);
1164 /* Turn it into normal strings by converting the length bytes into '.',
1165 and count how many search domains there are while we're at it. */
1167 while (i < data_len) {
1168 if (optc[i] > 0x3f) {
1169 DBG("DNSSL contains compressed elements in violation of RFC6106");
1176 /* Check for double zero */
1177 if (i < data_len && optc[i] == 0)
1185 if (i >= data_len) {
1186 DBG("DNSSL data overflows option length");
1193 domains = g_try_new0(const char *, nr_domains + 1);
1197 /* Now point to the normal strings, missing out the leading '.' that
1198 each of them will have now. */
1199 for (i = 0; i < nr_domains; i++) {
1200 domains[i] = (char *)optc + 1;
1201 optc += strlen((char *)optc) + 1;
1207 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1209 struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1210 struct nd_opt_hdr *opt;
1211 guint32 lifetime = -1;
1212 const char **domains = NULL;
1213 struct in6_addr *servers = NULL;
1214 int i, nr_servers = 0;
1215 int msglen = msg->nduseropt_opts_len;
1218 DBG("family %d index %d len %d type %d code %d",
1219 msg->nduseropt_family, msg->nduseropt_ifindex,
1220 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1221 msg->nduseropt_icmp_code);
1223 if (msg->nduseropt_family != AF_INET6 ||
1224 msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1225 msg->nduseropt_icmp_code != 0)
1228 index = msg->nduseropt_ifindex;
1232 for (opt = (void *)&msg[1];
1234 msglen -= opt->nd_opt_len * 8,
1235 opt = ((void *)opt) + opt->nd_opt_len*8) {
1237 DBG("remaining %d nd opt type %d len %d\n",
1238 msglen, opt->nd_opt_type, opt->nd_opt_len);
1240 if (opt->nd_opt_type == 25) { /* ND_OPT_RDNSS */
1243 servers = rtnl_nd_opt_rdnss(opt, &lifetime,
1245 for (i = 0; i < nr_servers; i++) {
1246 if (!inet_ntop(AF_INET6, servers + i, buf,
1250 connman_resolver_append_lifetime(index,
1251 NULL, buf, lifetime);
1254 } else if (opt->nd_opt_type == 31) { /* ND_OPT_DNSSL */
1257 domains = rtnl_nd_opt_dnssl(opt, &lifetime);
1258 for (i = 0; domains && domains[i]; i++)
1259 connman_resolver_append_lifetime(index,
1260 domains[i], NULL, lifetime);
1267 static const char *type2string(uint16_t type)
1296 case RTM_NEWNDUSEROPT:
1297 return "NEWNDUSEROPT";
1303 static GIOChannel *channel = NULL;
1304 static guint channel_watch = 0;
1306 struct rtnl_request {
1307 struct nlmsghdr hdr;
1308 struct rtgenmsg msg;
1310 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1312 static GSList *request_list = NULL;
1313 static guint32 request_seq = 0;
1315 static struct rtnl_request *find_request(guint32 seq)
1319 for (list = request_list; list; list = list->next) {
1320 struct rtnl_request *req = list->data;
1322 if (req->hdr.nlmsg_seq == seq)
1329 static int send_request(struct rtnl_request *req)
1331 struct sockaddr_nl addr;
1334 DBG("%s len %d type %d flags 0x%04x seq %d",
1335 type2string(req->hdr.nlmsg_type),
1336 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1337 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1339 sk = g_io_channel_unix_get_fd(channel);
1341 memset(&addr, 0, sizeof(addr));
1342 addr.nl_family = AF_NETLINK;
1344 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1345 (struct sockaddr *) &addr, sizeof(addr));
1348 static int queue_request(struct rtnl_request *req)
1350 request_list = g_slist_append(request_list, req);
1352 if (g_slist_length(request_list) > 1)
1355 return send_request(req);
1358 static int process_response(guint32 seq)
1360 struct rtnl_request *req;
1364 req = find_request(seq);
1366 request_list = g_slist_remove(request_list, req);
1370 req = g_slist_nth_data(request_list, 0);
1374 return send_request(req);
1377 static void rtnl_message(void *buf, size_t len)
1380 struct nlmsghdr *hdr = buf;
1381 struct nlmsgerr *err;
1383 if (!NLMSG_OK(hdr, len))
1386 DBG("%s len %d type %d flags 0x%04x seq %d pid %d",
1387 type2string(hdr->nlmsg_type),
1388 hdr->nlmsg_len, hdr->nlmsg_type,
1389 hdr->nlmsg_flags, hdr->nlmsg_seq,
1392 switch (hdr->nlmsg_type) {
1397 process_response(hdr->nlmsg_seq);
1400 err = NLMSG_DATA(hdr);
1401 DBG("error %d (%s)", -err->error,
1402 strerror(-err->error));
1422 case RTM_NEWNDUSEROPT:
1423 rtnl_newnduseropt(hdr);
1427 len -= hdr->nlmsg_len;
1428 buf += hdr->nlmsg_len;
1432 static gboolean netlink_event(GIOChannel *chan, GIOCondition cond, gpointer data)
1434 unsigned char buf[4096];
1435 struct sockaddr_nl nladdr;
1436 socklen_t addr_len = sizeof(nladdr);
1440 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1443 memset(buf, 0, sizeof(buf));
1444 memset(&nladdr, 0, sizeof(nladdr));
1446 fd = g_io_channel_unix_get_fd(chan);
1448 status = recvfrom(fd, buf, sizeof(buf), 0,
1449 (struct sockaddr *) &nladdr, &addr_len);
1451 if (errno == EINTR || errno == EAGAIN)
1460 if (nladdr.nl_pid != 0) { /* not sent by kernel, ignore */
1461 DBG("Received msg from %u, ignoring it", nladdr.nl_pid);
1465 rtnl_message(buf, status);
1470 static int send_getlink(void)
1472 struct rtnl_request *req;
1476 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1480 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1481 req->hdr.nlmsg_type = RTM_GETLINK;
1482 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1483 req->hdr.nlmsg_pid = 0;
1484 req->hdr.nlmsg_seq = request_seq++;
1485 req->msg.rtgen_family = AF_INET;
1487 return queue_request(req);
1490 static int send_getaddr(void)
1492 struct rtnl_request *req;
1496 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1500 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1501 req->hdr.nlmsg_type = RTM_GETADDR;
1502 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1503 req->hdr.nlmsg_pid = 0;
1504 req->hdr.nlmsg_seq = request_seq++;
1505 req->msg.rtgen_family = AF_INET;
1507 return queue_request(req);
1510 static int send_getroute(void)
1512 struct rtnl_request *req;
1516 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1520 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1521 req->hdr.nlmsg_type = RTM_GETROUTE;
1522 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1523 req->hdr.nlmsg_pid = 0;
1524 req->hdr.nlmsg_seq = request_seq++;
1525 req->msg.rtgen_family = AF_INET;
1527 return queue_request(req);
1530 static gboolean update_timeout_cb(gpointer user_data)
1532 __connman_rtnl_request_update();
1537 static void update_interval_callback(guint min)
1539 if (update_timeout > 0)
1540 g_source_remove(update_timeout);
1542 if (min < G_MAXUINT) {
1543 update_interval = min;
1544 update_timeout = g_timeout_add_seconds(update_interval,
1545 update_timeout_cb, NULL);
1548 update_interval = G_MAXUINT;
1552 static gint compare_interval(gconstpointer a, gconstpointer b)
1554 guint val_a = GPOINTER_TO_UINT(a);
1555 guint val_b = GPOINTER_TO_UINT(b);
1557 return val_a - val_b;
1560 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1567 update_list = g_slist_insert_sorted(update_list,
1568 GUINT_TO_POINTER(interval), compare_interval);
1570 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1571 if (min < update_interval) {
1572 update_interval_callback(min);
1573 __connman_rtnl_request_update();
1576 return update_interval;
1579 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1581 guint min = G_MAXUINT;
1586 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1589 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1591 if (min > update_interval)
1592 update_interval_callback(min);
1597 int __connman_rtnl_request_update(void)
1599 return send_getlink();
1602 int __connman_rtnl_init(void)
1604 struct sockaddr_nl addr;
1609 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1610 NULL, free_interface);
1612 sk = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
1616 memset(&addr, 0, sizeof(addr));
1617 addr.nl_family = AF_NETLINK;
1618 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1619 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1620 (1<<(RTNLGRP_ND_USEROPT-1));
1622 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1627 channel = g_io_channel_unix_new(sk);
1628 g_io_channel_set_close_on_unref(channel, TRUE);
1630 g_io_channel_set_encoding(channel, NULL, NULL);
1631 g_io_channel_set_buffered(channel, FALSE);
1633 channel_watch = g_io_add_watch(channel,
1634 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1635 netlink_event, NULL);
1640 void __connman_rtnl_start(void)
1649 void __connman_rtnl_cleanup(void)
1655 for (list = watch_list; list; list = list->next) {
1656 struct watch_data *watch = list->data;
1658 DBG("removing watch %d", watch->id);
1664 g_slist_free(watch_list);
1667 g_slist_free(update_list);
1670 for (list = request_list; list; list = list->next) {
1671 struct rtnl_request *req = list->data;
1673 DBG("%s len %d type %d flags 0x%04x seq %d",
1674 type2string(req->hdr.nlmsg_type),
1675 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1676 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1682 g_slist_free(request_list);
1683 request_list = NULL;
1685 if (channel_watch) {
1686 g_source_remove(channel_watch);
1690 g_io_channel_shutdown(channel, TRUE, NULL);
1691 g_io_channel_unref(channel);
1695 g_hash_table_destroy(interface_list);