5 * Copyright (C) 2007-2012 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 {
70 enum connman_service_type service_type;
71 enum connman_device_type device_type;
74 static GHashTable *interface_list = NULL;
76 static void free_interface(gpointer data)
78 struct interface_data *interface = data;
80 __connman_technology_remove_interface(interface->service_type,
81 interface->index, interface->name, interface->ident);
83 g_free(interface->ident);
84 g_free(interface->name);
88 static connman_bool_t ether_blacklisted(const char *name)
93 if (__connman_device_isfiltered(name) == TRUE)
99 static connman_bool_t wext_interface(char *ifname)
104 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
108 memset(&wrq, 0, sizeof(wrq));
109 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
111 err = ioctl(fd, SIOCGIWNAME, &wrq);
121 static void read_uevent(struct interface_data *interface)
123 char *filename, line[128];
124 connman_bool_t found_devtype;
127 if (ether_blacklisted(interface->name) == TRUE) {
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",
138 f = fopen(filename, "re");
145 found_devtype = FALSE;
146 while (fgets(line, sizeof(line), f)) {
149 pos = strchr(line, '\n');
154 if (strncmp(line, "DEVTYPE=", 8) != 0)
157 found_devtype = TRUE;
159 if (strcmp(line + 8, "wlan") == 0) {
160 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
161 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
162 } else if (strcmp(line + 8, "wwan") == 0) {
163 interface->service_type = CONNMAN_SERVICE_TYPE_CELLULAR;
164 interface->device_type = CONNMAN_DEVICE_TYPE_CELLULAR;
165 } else if (strcmp(line + 8, "bluetooth") == 0) {
166 interface->service_type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
167 interface->device_type = CONNMAN_DEVICE_TYPE_BLUETOOTH;
168 } else if (strcmp(line + 8, "wimax") == 0) {
169 interface->service_type = CONNMAN_SERVICE_TYPE_WIMAX;
170 interface->device_type = CONNMAN_DEVICE_TYPE_WIMAX;
171 } else if (strcmp(line + 8, "gadget") == 0) {
172 interface->service_type = CONNMAN_SERVICE_TYPE_GADGET;
173 interface->device_type = CONNMAN_DEVICE_TYPE_GADGET;
176 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
177 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
186 /* We haven't got a DEVTYPE, let's check if it's a wireless device */
187 if (wext_interface(interface->name)) {
188 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
189 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
191 connman_error("%s runs an unsupported 802.11 driver",
196 enum connman_device_type __connman_rtnl_get_device_type(int index)
198 struct interface_data *interface;
200 interface = g_hash_table_lookup(interface_list,
201 GINT_TO_POINTER(index));
202 if (interface == NULL)
203 return CONNMAN_DEVICE_TYPE_UNKNOWN;
205 return interface->device_type;
209 * connman_rtnl_add_newlink_watch:
210 * @index: network device index
211 * @callback: callback function
212 * @user_data: callback data;
214 * Add a new RTNL watch for newlink events
216 * Returns: %0 on failure and a unique id on success
218 unsigned int connman_rtnl_add_newlink_watch(int index,
219 connman_rtnl_link_cb_t callback, void *user_data)
221 struct watch_data *watch;
223 watch = g_try_new0(struct watch_data, 1);
227 watch->id = ++watch_id;
228 watch->index = index;
230 watch->newlink = callback;
231 watch->user_data = user_data;
233 watch_list = g_slist_prepend(watch_list, watch);
235 DBG("id %d", watch->id);
238 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
241 callback(flags, 0, user_data);
248 * connman_rtnl_remove_watch:
249 * @id: watch identifier
251 * Remove the RTNL watch for the identifier
253 void connman_rtnl_remove_watch(unsigned int id)
262 for (list = watch_list; list; list = list->next) {
263 struct watch_data *watch = list->data;
265 if (watch->id == id) {
266 watch_list = g_slist_remove(watch_list, watch);
273 static void trigger_rtnl(int index, void *user_data)
275 struct connman_rtnl *rtnl = user_data;
278 unsigned short type = __connman_ipconfig_get_type_from_index(index);
279 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
281 rtnl->newlink(type, index, flags, 0);
284 if (rtnl->newgateway) {
285 const char *gateway =
286 __connman_ipconfig_get_gateway_from_index(index,
287 CONNMAN_IPCONFIG_TYPE_ALL);
290 rtnl->newgateway(index, gateway);
294 static GSList *rtnl_list = NULL;
296 static gint compare_priority(gconstpointer a, gconstpointer b)
298 const struct connman_rtnl *rtnl1 = a;
299 const struct connman_rtnl *rtnl2 = b;
301 return rtnl2->priority - rtnl1->priority;
305 * connman_rtnl_register:
308 * Register a new RTNL module
310 * Returns: %0 on success
312 int connman_rtnl_register(struct connman_rtnl *rtnl)
314 DBG("rtnl %p name %s", rtnl, rtnl->name);
316 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
319 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
325 * connman_rtnl_unregister:
328 * Remove a previously registered RTNL module
330 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
332 DBG("rtnl %p name %s", rtnl, rtnl->name);
334 rtnl_list = g_slist_remove(rtnl_list, rtnl);
337 static const char *operstate2str(unsigned char operstate)
340 case IF_OPER_UNKNOWN:
342 case IF_OPER_NOTPRESENT:
343 return "NOT-PRESENT";
346 case IF_OPER_LOWERLAYERDOWN:
347 return "LOWER-LAYER-DOWN";
348 case IF_OPER_TESTING:
350 case IF_OPER_DORMANT:
359 static connman_bool_t extract_link(struct ifinfomsg *msg, int bytes,
360 struct ether_addr *address, const char **ifname,
361 unsigned int *mtu, unsigned char *operstate,
362 struct rtnl_link_stats *stats)
366 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
367 attr = RTA_NEXT(attr, bytes)) {
368 switch (attr->rta_type) {
371 memcpy(address, RTA_DATA(attr), ETH_ALEN);
375 *ifname = RTA_DATA(attr);
379 *mtu = *((unsigned int *) RTA_DATA(attr));
383 memcpy(stats, RTA_DATA(attr),
384 sizeof(struct rtnl_link_stats));
387 if (operstate != NULL)
388 *operstate = *((unsigned char *) RTA_DATA(attr));
400 static void process_newlink(unsigned short type, int index, unsigned flags,
401 unsigned change, struct ifinfomsg *msg, int bytes)
403 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
404 struct ether_addr compare = {{ 0, 0, 0, 0, 0, 0 }};
405 struct rtnl_link_stats stats;
406 unsigned char operstate = 0xff;
407 struct interface_data *interface;
408 const char *ifname = NULL;
409 unsigned int mtu = 0;
410 char ident[13], str[18];
413 memset(&stats, 0, sizeof(stats));
414 if (extract_link(msg, bytes, &address, &ifname, &mtu, &operstate,
418 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
419 address.ether_addr_octet[0],
420 address.ether_addr_octet[1],
421 address.ether_addr_octet[2],
422 address.ether_addr_octet[3],
423 address.ether_addr_octet[4],
424 address.ether_addr_octet[5]);
426 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
427 address.ether_addr_octet[0],
428 address.ether_addr_octet[1],
429 address.ether_addr_octet[2],
430 address.ether_addr_octet[3],
431 address.ether_addr_octet[4],
432 address.ether_addr_octet[5]);
436 case ARPHRD_LOOPBACK:
437 case ARPHDR_PHONET_PIPE:
440 __connman_ipconfig_newlink(index, type, flags,
445 if (memcmp(&address, &compare, ETH_ALEN) != 0)
446 connman_info("%s {newlink} index %d address %s mtu %u",
447 ifname, index, str, mtu);
449 if (operstate != 0xff)
450 connman_info("%s {newlink} index %d operstate %u <%s>",
451 ifname, index, operstate,
452 operstate2str(operstate));
454 interface = g_hash_table_lookup(interface_list, GINT_TO_POINTER(index));
455 if (interface == NULL) {
456 interface = g_new0(struct interface_data, 1);
457 interface->index = index;
458 interface->name = g_strdup(ifname);
459 interface->ident = g_strdup(ident);
461 g_hash_table_insert(interface_list,
462 GINT_TO_POINTER(index), interface);
464 if (type == ARPHRD_ETHER)
465 read_uevent(interface);
467 __connman_technology_add_interface(interface->service_type,
468 interface->index, interface->name, interface->ident);
471 for (list = rtnl_list; list; list = list->next) {
472 struct connman_rtnl *rtnl = list->data;
475 rtnl->newlink(type, index, flags, change);
478 for (list = watch_list; list; list = list->next) {
479 struct watch_data *watch = list->data;
481 if (watch->index != index)
485 watch->newlink(flags, change, watch->user_data);
489 static void process_dellink(unsigned short type, int index, unsigned flags,
490 unsigned change, struct ifinfomsg *msg, int bytes)
492 struct rtnl_link_stats stats;
493 unsigned char operstate = 0xff;
494 const char *ifname = NULL;
497 memset(&stats, 0, sizeof(stats));
498 if (extract_link(msg, bytes, NULL, &ifname, NULL, &operstate,
502 if (operstate != 0xff)
503 connman_info("%s {dellink} index %d operstate %u <%s>",
504 ifname, index, operstate,
505 operstate2str(operstate));
507 for (list = rtnl_list; list; list = list->next) {
508 struct connman_rtnl *rtnl = list->data;
511 rtnl->dellink(type, index, flags, change);
516 case ARPHRD_LOOPBACK:
518 __connman_ipconfig_dellink(index, &stats);
522 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
525 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
527 struct in_addr *local,
528 struct in_addr *address,
529 struct in_addr *broadcast)
533 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
534 attr = RTA_NEXT(attr, bytes)) {
535 switch (attr->rta_type) {
538 *address = *((struct in_addr *) RTA_DATA(attr));
542 *local = *((struct in_addr *) RTA_DATA(attr));
545 if (broadcast != NULL)
546 *broadcast = *((struct in_addr *) RTA_DATA(attr));
550 *label = RTA_DATA(attr);
556 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
557 struct in6_addr *addr,
558 struct in6_addr *local)
562 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
563 attr = RTA_NEXT(attr, bytes)) {
564 switch (attr->rta_type) {
567 *addr = *((struct in6_addr *) RTA_DATA(attr));
571 *local = *((struct in6_addr *) RTA_DATA(attr));
577 static void process_newaddr(unsigned char family, unsigned char prefixlen,
578 int index, struct ifaddrmsg *msg, int bytes)
580 const char *label = NULL;
582 char ip_string[INET6_ADDRSTRLEN];
584 if (family == AF_INET) {
585 struct in_addr ipv4_addr = { INADDR_ANY };
587 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
589 } else if (family == AF_INET6) {
590 struct in6_addr ipv6_address, ipv6_local;
592 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
593 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
601 if (inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN) == NULL)
604 __connman_ipconfig_newaddr(index, family, label,
605 prefixlen, ip_string);
607 if (family == AF_INET6) {
609 * Re-create RDNSS configured servers if there are any
610 * for this interface. This is done because we might
611 * have now properly configured interface with proper
612 * autoconfigured address.
614 char *interface = connman_inet_ifname(index);
616 __connman_resolver_redo_servers(interface);
622 static void process_deladdr(unsigned char family, unsigned char prefixlen,
623 int index, struct ifaddrmsg *msg, int bytes)
625 const char *label = NULL;
627 char ip_string[INET6_ADDRSTRLEN];
629 if (family == AF_INET) {
630 struct in_addr ipv4_addr = { INADDR_ANY };
632 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
634 } else if (family == AF_INET6) {
635 struct in6_addr ipv6_address, ipv6_local;
637 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
638 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
646 if (inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN) == NULL)
649 __connman_ipconfig_deladdr(index, family, label,
650 prefixlen, ip_string);
653 static void extract_ipv4_route(struct rtmsg *msg, int bytes, int *index,
655 struct in_addr *gateway)
659 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
660 attr = RTA_NEXT(attr, bytes)) {
661 switch (attr->rta_type) {
664 *dst = *((struct in_addr *) RTA_DATA(attr));
668 *gateway = *((struct in_addr *) RTA_DATA(attr));
672 *index = *((int *) RTA_DATA(attr));
678 static void extract_ipv6_route(struct rtmsg *msg, int bytes, int *index,
679 struct in6_addr *dst,
680 struct in6_addr *gateway)
684 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
685 attr = RTA_NEXT(attr, bytes)) {
686 switch (attr->rta_type) {
689 *dst = *((struct in6_addr *) RTA_DATA(attr));
694 *((struct in6_addr *) RTA_DATA(attr));
698 *index = *((int *) RTA_DATA(attr));
704 static void process_newroute(unsigned char family, unsigned char scope,
705 struct rtmsg *msg, int bytes)
708 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
711 if (family == AF_INET) {
712 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
714 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
716 inet_ntop(family, &dst, dststr, sizeof(dststr));
717 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
719 __connman_ipconfig_newroute(index, family, scope, dststr,
722 /* skip host specific routes */
723 if (scope != RT_SCOPE_UNIVERSE &&
724 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
727 if (dst.s_addr != INADDR_ANY)
730 } else if (family == AF_INET6) {
731 struct in6_addr dst = IN6ADDR_ANY_INIT,
732 gateway = IN6ADDR_ANY_INIT;
734 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
736 inet_ntop(family, &dst, dststr, sizeof(dststr));
737 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
739 __connman_ipconfig_newroute(index, family, scope, dststr,
742 /* skip host specific routes */
743 if (scope != RT_SCOPE_UNIVERSE &&
744 !(scope == RT_SCOPE_LINK &&
745 IN6_IS_ADDR_UNSPECIFIED(&dst)))
748 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
753 for (list = rtnl_list; list; list = list->next) {
754 struct connman_rtnl *rtnl = list->data;
756 if (rtnl->newgateway)
757 rtnl->newgateway(index, gatewaystr);
761 static void process_delroute(unsigned char family, unsigned char scope,
762 struct rtmsg *msg, int bytes)
765 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
768 if (family == AF_INET) {
769 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
771 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
773 inet_ntop(family, &dst, dststr, sizeof(dststr));
774 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
776 __connman_ipconfig_delroute(index, family, scope, dststr,
779 /* skip host specific routes */
780 if (scope != RT_SCOPE_UNIVERSE &&
781 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
784 if (dst.s_addr != INADDR_ANY)
787 } else if (family == AF_INET6) {
788 struct in6_addr dst = IN6ADDR_ANY_INIT,
789 gateway = IN6ADDR_ANY_INIT;
791 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
793 inet_ntop(family, &dst, dststr, sizeof(dststr));
794 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
796 __connman_ipconfig_delroute(index, family, scope, dststr,
799 /* skip host specific routes */
800 if (scope != RT_SCOPE_UNIVERSE &&
801 !(scope == RT_SCOPE_LINK &&
802 IN6_IS_ADDR_UNSPECIFIED(&dst)))
805 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
810 for (list = rtnl_list; list; list = list->next) {
811 struct connman_rtnl *rtnl = list->data;
813 if (rtnl->delgateway)
814 rtnl->delgateway(index, gatewaystr);
818 static inline void print_ether(struct rtattr *attr, const char *name)
820 int len = (int) RTA_PAYLOAD(attr);
822 if (len == ETH_ALEN) {
823 struct ether_addr eth;
824 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
825 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
827 print(" attr %s (len %d)\n", name, len);
830 static inline void print_inet(struct rtattr *attr, const char *name,
831 unsigned char family)
833 int len = (int) RTA_PAYLOAD(attr);
835 if (family == AF_INET && len == sizeof(struct in_addr)) {
837 addr = *((struct in_addr *) RTA_DATA(attr));
838 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
840 print(" attr %s (len %d)\n", name, len);
843 static inline void print_string(struct rtattr *attr, const char *name)
845 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
846 (char *) RTA_DATA(attr));
849 static inline void print_byte(struct rtattr *attr, const char *name)
851 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
852 *((unsigned char *) RTA_DATA(attr)));
855 static inline void print_integer(struct rtattr *attr, const char *name)
857 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
858 *((int *) RTA_DATA(attr)));
861 static inline void print_attr(struct rtattr *attr, const char *name)
863 int len = (int) RTA_PAYLOAD(attr);
866 print(" attr %s (len %d)\n", name, len);
868 print(" attr %d (len %d)\n", attr->rta_type, len);
871 static void rtnl_link(struct nlmsghdr *hdr)
873 struct ifinfomsg *msg;
877 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
878 bytes = IFLA_PAYLOAD(hdr);
880 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
882 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
883 attr = RTA_NEXT(attr, bytes)) {
884 switch (attr->rta_type) {
886 print_ether(attr, "address");
889 print_ether(attr, "broadcast");
892 print_string(attr, "ifname");
895 print_integer(attr, "mtu");
898 print_attr(attr, "link");
901 print_attr(attr, "qdisc");
904 print_attr(attr, "stats");
907 print_attr(attr, "cost");
910 print_attr(attr, "priority");
913 print_attr(attr, "master");
916 print_attr(attr, "wireless");
919 print_attr(attr, "protinfo");
922 print_integer(attr, "txqlen");
925 print_attr(attr, "map");
928 print_attr(attr, "weight");
931 print_byte(attr, "operstate");
934 print_byte(attr, "linkmode");
937 print_attr(attr, NULL);
943 static void rtnl_newlink(struct nlmsghdr *hdr)
945 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
949 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
950 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
953 static void rtnl_dellink(struct nlmsghdr *hdr)
955 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
959 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
960 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
963 static void rtnl_addr(struct nlmsghdr *hdr)
965 struct ifaddrmsg *msg;
969 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
970 bytes = IFA_PAYLOAD(hdr);
972 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
974 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
975 attr = RTA_NEXT(attr, bytes)) {
976 switch (attr->rta_type) {
978 print_inet(attr, "address", msg->ifa_family);
981 print_inet(attr, "local", msg->ifa_family);
984 print_string(attr, "label");
987 print_inet(attr, "broadcast", msg->ifa_family);
990 print_attr(attr, "anycast");
993 print_attr(attr, "cacheinfo");
996 print_attr(attr, "multicast");
999 print_attr(attr, NULL);
1005 static void rtnl_newaddr(struct nlmsghdr *hdr)
1007 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1011 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1012 msg, IFA_PAYLOAD(hdr));
1015 static void rtnl_deladdr(struct nlmsghdr *hdr)
1017 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1021 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1022 msg, IFA_PAYLOAD(hdr));
1025 static void rtnl_route(struct nlmsghdr *hdr)
1028 struct rtattr *attr;
1031 msg = (struct rtmsg *) NLMSG_DATA(hdr);
1032 bytes = RTM_PAYLOAD(hdr);
1034 print("rtm_family %d rtm_table %d rtm_protocol %d",
1035 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
1036 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
1037 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
1039 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
1040 attr = RTA_NEXT(attr, bytes)) {
1041 switch (attr->rta_type) {
1043 print_inet(attr, "dst", msg->rtm_family);
1046 print_inet(attr, "src", msg->rtm_family);
1049 print_string(attr, "iif");
1052 print_integer(attr, "oif");
1055 print_inet(attr, "gateway", msg->rtm_family);
1058 print_attr(attr, "priority");
1061 print_inet(attr, "prefsrc", msg->rtm_family);
1064 print_attr(attr, "metrics");
1067 print_integer(attr, "table");
1070 print_attr(attr, NULL);
1076 static connman_bool_t is_route_rtmsg(struct rtmsg *msg)
1079 if (msg->rtm_table != RT_TABLE_MAIN)
1082 if (msg->rtm_protocol != RTPROT_BOOT &&
1083 msg->rtm_protocol != RTPROT_KERNEL)
1086 if (msg->rtm_type != RTN_UNICAST)
1092 static void rtnl_newroute(struct nlmsghdr *hdr)
1094 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1098 if (is_route_rtmsg(msg))
1099 process_newroute(msg->rtm_family, msg->rtm_scope,
1100 msg, RTM_PAYLOAD(hdr));
1103 static void rtnl_delroute(struct nlmsghdr *hdr)
1105 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1109 if (is_route_rtmsg(msg))
1110 process_delroute(msg->rtm_family, msg->rtm_scope,
1111 msg, RTM_PAYLOAD(hdr));
1114 static void *rtnl_nd_opt_rdnss(struct nd_opt_hdr *opt, guint32 *lifetime,
1117 guint32 *optint = (void *)opt;
1119 if (opt->nd_opt_len < 3)
1122 if (*lifetime > ntohl(optint[1]))
1123 *lifetime = ntohl(optint[1]);
1125 /* nd_opt_len is in units of 8 bytes. The header is 1 unit (8 bytes)
1126 and each address is another 2 units (16 bytes).
1127 So the number of addresses (given rounding) is nd_opt_len/2 */
1128 *nr_servers = opt->nd_opt_len / 2;
1130 /* And they start 8 bytes into the packet, or two guint32s in. */
1134 static const char **rtnl_nd_opt_dnssl(struct nd_opt_hdr *opt, guint32 *lifetime)
1136 const char **domains = NULL;
1137 guint32 *optint = (void *)opt;
1138 unsigned char *optc = (void *)&optint[2];
1139 int data_len = (opt->nd_opt_len * 8) - 8;
1143 if (*lifetime > ntohl(optint[1]))
1144 *lifetime = ntohl(optint[1]);
1146 /* Turn it into normal strings by converting the length bytes into '.',
1147 and count how many search domains there are while we're at it. */
1149 while (i < data_len) {
1150 if (optc[i] > 0x3f) {
1151 DBG("DNSSL contains compressed elements in violation of RFC6106");
1158 /* Check for double zero */
1159 if (i < data_len && optc[i] == 0)
1167 if (i >= data_len) {
1168 DBG("DNSSL data overflows option length");
1175 domains = g_try_new0(const char *, nr_domains + 1);
1179 /* Now point to the normal strings, missing out the leading '.' that
1180 each of them will have now. */
1181 for (i = 0; i < nr_domains; i++) {
1182 domains[i] = (char *)optc + 1;
1183 optc += strlen((char *)optc) + 1;
1189 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1191 struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1192 struct nd_opt_hdr *opt;
1193 guint32 lifetime = -1;
1194 const char **domains = NULL;
1195 struct in6_addr *servers = NULL;
1196 int i, nr_servers = 0;
1197 int msglen = msg->nduseropt_opts_len;
1200 DBG("family %d index %d len %d type %d code %d",
1201 msg->nduseropt_family, msg->nduseropt_ifindex,
1202 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1203 msg->nduseropt_icmp_code);
1205 if (msg->nduseropt_family != AF_INET6 ||
1206 msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1207 msg->nduseropt_icmp_code != 0)
1210 interface = connman_inet_ifname(msg->nduseropt_ifindex);
1214 for (opt = (void *)&msg[1];
1216 msglen -= opt->nd_opt_len * 8,
1217 opt = ((void *)opt) + opt->nd_opt_len*8) {
1219 DBG("remaining %d nd opt type %d len %d\n",
1220 msglen, opt->nd_opt_type, opt->nd_opt_len);
1222 if (opt->nd_opt_type == 25) { /* ND_OPT_RDNSS */
1225 servers = rtnl_nd_opt_rdnss(opt, &lifetime,
1227 for (i = 0; i < nr_servers; i++) {
1228 if (!inet_ntop(AF_INET6, servers + i, buf,
1232 connman_resolver_append_lifetime(interface,
1233 NULL, buf, lifetime);
1236 } else if (opt->nd_opt_type == 31) { /* ND_OPT_DNSSL */
1239 domains = rtnl_nd_opt_dnssl(opt, &lifetime);
1240 for (i = 0; domains != NULL && domains[i] != NULL; i++)
1241 connman_resolver_append_lifetime(interface,
1242 domains[i], NULL, lifetime);
1250 static const char *type2string(uint16_t type)
1279 case RTM_NEWNDUSEROPT:
1280 return "NEWNDUSEROPT";
1286 static GIOChannel *channel = NULL;
1288 struct rtnl_request {
1289 struct nlmsghdr hdr;
1290 struct rtgenmsg msg;
1292 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1294 static GSList *request_list = NULL;
1295 static guint32 request_seq = 0;
1297 static struct rtnl_request *find_request(guint32 seq)
1301 for (list = request_list; list; list = list->next) {
1302 struct rtnl_request *req = list->data;
1304 if (req->hdr.nlmsg_seq == seq)
1311 static int send_request(struct rtnl_request *req)
1313 struct sockaddr_nl addr;
1316 DBG("%s len %d type %d flags 0x%04x seq %d",
1317 type2string(req->hdr.nlmsg_type),
1318 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1319 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1321 sk = g_io_channel_unix_get_fd(channel);
1323 memset(&addr, 0, sizeof(addr));
1324 addr.nl_family = AF_NETLINK;
1326 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1327 (struct sockaddr *) &addr, sizeof(addr));
1330 static int queue_request(struct rtnl_request *req)
1332 request_list = g_slist_append(request_list, req);
1334 if (g_slist_length(request_list) > 1)
1337 return send_request(req);
1340 static int process_response(guint32 seq)
1342 struct rtnl_request *req;
1346 req = find_request(seq);
1348 request_list = g_slist_remove(request_list, req);
1352 req = g_slist_nth_data(request_list, 0);
1356 return send_request(req);
1359 static void rtnl_message(void *buf, size_t len)
1361 DBG("buf %p len %zd", buf, len);
1364 struct nlmsghdr *hdr = buf;
1365 struct nlmsgerr *err;
1367 if (!NLMSG_OK(hdr, len))
1370 DBG("%s len %d type %d flags 0x%04x seq %d pid %d",
1371 type2string(hdr->nlmsg_type),
1372 hdr->nlmsg_len, hdr->nlmsg_type,
1373 hdr->nlmsg_flags, hdr->nlmsg_seq,
1376 switch (hdr->nlmsg_type) {
1381 process_response(hdr->nlmsg_seq);
1384 err = NLMSG_DATA(hdr);
1385 DBG("error %d (%s)", -err->error,
1386 strerror(-err->error));
1406 case RTM_NEWNDUSEROPT:
1407 rtnl_newnduseropt(hdr);
1411 len -= hdr->nlmsg_len;
1412 buf += hdr->nlmsg_len;
1416 static gboolean netlink_event(GIOChannel *chan,
1417 GIOCondition cond, gpointer data)
1419 unsigned char buf[4096];
1420 struct sockaddr_nl nladdr;
1421 socklen_t addr_len = sizeof(nladdr);
1425 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1428 memset(buf, 0, sizeof(buf));
1429 memset(&nladdr, 0, sizeof(nladdr));
1431 fd = g_io_channel_unix_get_fd(chan);
1433 status = recvfrom(fd, buf, sizeof(buf), 0,
1434 (struct sockaddr *) &nladdr, &addr_len);
1436 if (errno == EINTR || errno == EAGAIN)
1445 if (nladdr.nl_pid != 0) { /* not sent by kernel, ignore */
1446 DBG("Received msg from %u, ignoring it", nladdr.nl_pid);
1450 rtnl_message(buf, status);
1455 static int send_getlink(void)
1457 struct rtnl_request *req;
1461 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1465 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1466 req->hdr.nlmsg_type = RTM_GETLINK;
1467 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1468 req->hdr.nlmsg_pid = 0;
1469 req->hdr.nlmsg_seq = request_seq++;
1470 req->msg.rtgen_family = AF_INET;
1472 return queue_request(req);
1475 static int send_getaddr(void)
1477 struct rtnl_request *req;
1481 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1485 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1486 req->hdr.nlmsg_type = RTM_GETADDR;
1487 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1488 req->hdr.nlmsg_pid = 0;
1489 req->hdr.nlmsg_seq = request_seq++;
1490 req->msg.rtgen_family = AF_INET;
1492 return queue_request(req);
1495 static int send_getroute(void)
1497 struct rtnl_request *req;
1501 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1505 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1506 req->hdr.nlmsg_type = RTM_GETROUTE;
1507 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1508 req->hdr.nlmsg_pid = 0;
1509 req->hdr.nlmsg_seq = request_seq++;
1510 req->msg.rtgen_family = AF_INET;
1512 return queue_request(req);
1515 static gboolean update_timeout_cb(gpointer user_data)
1517 __connman_rtnl_request_update();
1522 static void update_interval_callback(guint min)
1524 if (update_timeout > 0)
1525 g_source_remove(update_timeout);
1527 if (min < G_MAXUINT) {
1528 update_interval = min;
1529 update_timeout = g_timeout_add_seconds(update_interval,
1530 update_timeout_cb, NULL);
1533 update_interval = G_MAXUINT;
1537 static gint compare_interval(gconstpointer a, gconstpointer b)
1539 guint val_a = GPOINTER_TO_UINT(a);
1540 guint val_b = GPOINTER_TO_UINT(b);
1542 return val_a - val_b;
1545 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1552 update_list = g_slist_insert_sorted(update_list,
1553 GUINT_TO_POINTER(interval), compare_interval);
1555 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1556 if (min < update_interval) {
1557 update_interval_callback(min);
1558 __connman_rtnl_request_update();
1561 return update_interval;
1564 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1566 guint min = G_MAXUINT;
1571 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1573 if (update_list != NULL)
1574 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1576 if (min > update_interval)
1577 update_interval_callback(min);
1582 int __connman_rtnl_request_update(void)
1584 return send_getlink();
1587 int __connman_rtnl_init(void)
1589 struct sockaddr_nl addr;
1594 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1595 NULL, free_interface);
1597 sk = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
1601 memset(&addr, 0, sizeof(addr));
1602 addr.nl_family = AF_NETLINK;
1603 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1604 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1605 (1<<(RTNLGRP_ND_USEROPT-1));
1607 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1612 channel = g_io_channel_unix_new(sk);
1613 g_io_channel_set_close_on_unref(channel, TRUE);
1615 g_io_channel_set_encoding(channel, NULL, NULL);
1616 g_io_channel_set_buffered(channel, FALSE);
1618 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1619 netlink_event, NULL);
1624 void __connman_rtnl_start(void)
1633 void __connman_rtnl_cleanup(void)
1639 for (list = watch_list; list; list = list->next) {
1640 struct watch_data *watch = list->data;
1642 DBG("removing watch %d", watch->id);
1648 g_slist_free(watch_list);
1651 g_slist_free(update_list);
1654 for (list = request_list; list; list = list->next) {
1655 struct rtnl_request *req = list->data;
1657 DBG("%s len %d type %d flags 0x%04x seq %d",
1658 type2string(req->hdr.nlmsg_type),
1659 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1660 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1666 g_slist_free(request_list);
1667 request_list = NULL;
1669 g_io_channel_shutdown(channel, TRUE, NULL);
1670 g_io_channel_unref(channel);
1674 g_hash_table_destroy(interface_list);