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 void 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));
396 static void process_newlink(unsigned short type, int index, unsigned flags,
397 unsigned change, struct ifinfomsg *msg, int bytes)
399 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
400 struct ether_addr compare = {{ 0, 0, 0, 0, 0, 0 }};
401 struct rtnl_link_stats stats;
402 unsigned char operstate = 0xff;
403 struct interface_data *interface;
404 const char *ifname = NULL;
405 unsigned int mtu = 0;
406 char ident[13], str[18];
409 memset(&stats, 0, sizeof(stats));
410 extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats);
412 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
413 address.ether_addr_octet[0],
414 address.ether_addr_octet[1],
415 address.ether_addr_octet[2],
416 address.ether_addr_octet[3],
417 address.ether_addr_octet[4],
418 address.ether_addr_octet[5]);
420 snprintf(str, 18, "%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]);
430 case ARPHRD_LOOPBACK:
431 case ARPHDR_PHONET_PIPE:
433 __connman_ipconfig_newlink(index, type, flags,
438 if (memcmp(&address, &compare, ETH_ALEN) != 0)
439 connman_info("%s {newlink} index %d address %s mtu %u",
440 ifname, index, str, mtu);
442 if (operstate != 0xff)
443 connman_info("%s {newlink} index %d operstate %u <%s>",
444 ifname, index, operstate,
445 operstate2str(operstate));
447 interface = g_hash_table_lookup(interface_list, GINT_TO_POINTER(index));
448 if (interface == NULL) {
449 interface = g_new0(struct interface_data, 1);
450 interface->index = index;
451 interface->name = g_strdup(ifname);
452 interface->ident = g_strdup(ident);
454 g_hash_table_insert(interface_list,
455 GINT_TO_POINTER(index), interface);
457 if (type == ARPHRD_ETHER)
458 read_uevent(interface);
460 __connman_technology_add_interface(interface->service_type,
461 interface->index, interface->name, interface->ident);
464 for (list = rtnl_list; list; list = list->next) {
465 struct connman_rtnl *rtnl = list->data;
468 rtnl->newlink(type, index, flags, change);
471 for (list = watch_list; list; list = list->next) {
472 struct watch_data *watch = list->data;
474 if (watch->index != index)
478 watch->newlink(flags, change, watch->user_data);
482 static void process_dellink(unsigned short type, int index, unsigned flags,
483 unsigned change, struct ifinfomsg *msg, int bytes)
485 struct rtnl_link_stats stats;
486 unsigned char operstate = 0xff;
487 const char *ifname = NULL;
490 memset(&stats, 0, sizeof(stats));
491 extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats);
493 if (operstate != 0xff)
494 connman_info("%s {dellink} index %d operstate %u <%s>",
495 ifname, index, operstate,
496 operstate2str(operstate));
498 for (list = rtnl_list; list; list = list->next) {
499 struct connman_rtnl *rtnl = list->data;
502 rtnl->dellink(type, index, flags, change);
507 case ARPHRD_LOOPBACK:
509 __connman_ipconfig_dellink(index, &stats);
513 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
516 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
518 struct in_addr *local,
519 struct in_addr *address,
520 struct in_addr *broadcast)
524 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
525 attr = RTA_NEXT(attr, bytes)) {
526 switch (attr->rta_type) {
529 *address = *((struct in_addr *) RTA_DATA(attr));
533 *local = *((struct in_addr *) RTA_DATA(attr));
536 if (broadcast != NULL)
537 *broadcast = *((struct in_addr *) RTA_DATA(attr));
541 *label = RTA_DATA(attr);
547 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
548 struct in6_addr *addr,
549 struct in6_addr *local)
553 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
554 attr = RTA_NEXT(attr, bytes)) {
555 switch (attr->rta_type) {
558 *addr = *((struct in6_addr *) RTA_DATA(attr));
562 *local = *((struct in6_addr *) RTA_DATA(attr));
568 static void process_newaddr(unsigned char family, unsigned char prefixlen,
569 int index, struct ifaddrmsg *msg, int bytes)
571 const char *label = NULL;
573 char ip_string[INET6_ADDRSTRLEN];
575 if (family == AF_INET) {
576 struct in_addr ipv4_addr = { INADDR_ANY };
578 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
580 } else if (family == AF_INET6) {
581 struct in6_addr ipv6_address, ipv6_local;
583 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
584 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
592 if (inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN) == NULL)
595 __connman_ipconfig_newaddr(index, family, label,
596 prefixlen, ip_string);
598 if (family == AF_INET6) {
600 * Re-create RDNSS configured servers if there are any
601 * for this interface. This is done because we might
602 * have now properly configured interface with proper
603 * autoconfigured address.
605 char *interface = connman_inet_ifname(index);
607 __connman_resolver_redo_servers(interface);
613 static void process_deladdr(unsigned char family, unsigned char prefixlen,
614 int index, struct ifaddrmsg *msg, int bytes)
616 const char *label = NULL;
618 char ip_string[INET6_ADDRSTRLEN];
620 if (family == AF_INET) {
621 struct in_addr ipv4_addr = { INADDR_ANY };
623 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
625 } else if (family == AF_INET6) {
626 struct in6_addr ipv6_address, ipv6_local;
628 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
629 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
637 if (inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN) == NULL)
640 __connman_ipconfig_deladdr(index, family, label,
641 prefixlen, ip_string);
644 static void extract_ipv4_route(struct rtmsg *msg, int bytes, int *index,
646 struct in_addr *gateway)
650 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
651 attr = RTA_NEXT(attr, bytes)) {
652 switch (attr->rta_type) {
655 *dst = *((struct in_addr *) RTA_DATA(attr));
659 *gateway = *((struct in_addr *) RTA_DATA(attr));
663 *index = *((int *) RTA_DATA(attr));
669 static void extract_ipv6_route(struct rtmsg *msg, int bytes, int *index,
670 struct in6_addr *dst,
671 struct in6_addr *gateway)
675 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
676 attr = RTA_NEXT(attr, bytes)) {
677 switch (attr->rta_type) {
680 *dst = *((struct in6_addr *) RTA_DATA(attr));
685 *((struct in6_addr *) RTA_DATA(attr));
689 *index = *((int *) RTA_DATA(attr));
695 static void process_newroute(unsigned char family, unsigned char scope,
696 struct rtmsg *msg, int bytes)
699 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
702 if (family == AF_INET) {
703 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
705 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
707 inet_ntop(family, &dst, dststr, sizeof(dststr));
708 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
710 __connman_ipconfig_newroute(index, family, scope, dststr,
713 /* skip host specific routes */
714 if (scope != RT_SCOPE_UNIVERSE &&
715 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
718 if (dst.s_addr != INADDR_ANY)
721 } else if (family == AF_INET6) {
722 struct in6_addr dst = IN6ADDR_ANY_INIT,
723 gateway = IN6ADDR_ANY_INIT;
725 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
727 inet_ntop(family, &dst, dststr, sizeof(dststr));
728 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
730 __connman_ipconfig_newroute(index, family, scope, dststr,
733 /* skip host specific routes */
734 if (scope != RT_SCOPE_UNIVERSE &&
735 !(scope == RT_SCOPE_LINK &&
736 IN6_IS_ADDR_UNSPECIFIED(&dst)))
739 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
744 for (list = rtnl_list; list; list = list->next) {
745 struct connman_rtnl *rtnl = list->data;
747 if (rtnl->newgateway)
748 rtnl->newgateway(index, gatewaystr);
752 static void process_delroute(unsigned char family, unsigned char scope,
753 struct rtmsg *msg, int bytes)
756 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
759 if (family == AF_INET) {
760 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
762 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
764 inet_ntop(family, &dst, dststr, sizeof(dststr));
765 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
767 __connman_ipconfig_delroute(index, family, scope, dststr,
770 /* skip host specific routes */
771 if (scope != RT_SCOPE_UNIVERSE &&
772 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
775 if (dst.s_addr != INADDR_ANY)
778 } else if (family == AF_INET6) {
779 struct in6_addr dst = IN6ADDR_ANY_INIT,
780 gateway = IN6ADDR_ANY_INIT;
782 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
784 inet_ntop(family, &dst, dststr, sizeof(dststr));
785 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
787 __connman_ipconfig_delroute(index, family, scope, dststr,
790 /* skip host specific routes */
791 if (scope != RT_SCOPE_UNIVERSE &&
792 !(scope == RT_SCOPE_LINK &&
793 IN6_IS_ADDR_UNSPECIFIED(&dst)))
796 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
801 for (list = rtnl_list; list; list = list->next) {
802 struct connman_rtnl *rtnl = list->data;
804 if (rtnl->delgateway)
805 rtnl->delgateway(index, gatewaystr);
809 static inline void print_ether(struct rtattr *attr, const char *name)
811 int len = (int) RTA_PAYLOAD(attr);
813 if (len == ETH_ALEN) {
814 struct ether_addr eth;
815 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
816 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
818 print(" attr %s (len %d)\n", name, len);
821 static inline void print_inet(struct rtattr *attr, const char *name,
822 unsigned char family)
824 int len = (int) RTA_PAYLOAD(attr);
826 if (family == AF_INET && len == sizeof(struct in_addr)) {
828 addr = *((struct in_addr *) RTA_DATA(attr));
829 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
831 print(" attr %s (len %d)\n", name, len);
834 static inline void print_string(struct rtattr *attr, const char *name)
836 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
837 (char *) RTA_DATA(attr));
840 static inline void print_byte(struct rtattr *attr, const char *name)
842 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
843 *((unsigned char *) RTA_DATA(attr)));
846 static inline void print_integer(struct rtattr *attr, const char *name)
848 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
849 *((int *) RTA_DATA(attr)));
852 static inline void print_attr(struct rtattr *attr, const char *name)
854 int len = (int) RTA_PAYLOAD(attr);
857 print(" attr %s (len %d)\n", name, len);
859 print(" attr %d (len %d)\n", attr->rta_type, len);
862 static void rtnl_link(struct nlmsghdr *hdr)
864 struct ifinfomsg *msg;
868 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
869 bytes = IFLA_PAYLOAD(hdr);
871 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
873 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
874 attr = RTA_NEXT(attr, bytes)) {
875 switch (attr->rta_type) {
877 print_ether(attr, "address");
880 print_ether(attr, "broadcast");
883 print_string(attr, "ifname");
886 print_integer(attr, "mtu");
889 print_attr(attr, "link");
892 print_attr(attr, "qdisc");
895 print_attr(attr, "stats");
898 print_attr(attr, "cost");
901 print_attr(attr, "priority");
904 print_attr(attr, "master");
907 print_attr(attr, "wireless");
910 print_attr(attr, "protinfo");
913 print_integer(attr, "txqlen");
916 print_attr(attr, "map");
919 print_attr(attr, "weight");
922 print_byte(attr, "operstate");
925 print_byte(attr, "linkmode");
928 print_attr(attr, NULL);
934 static void rtnl_newlink(struct nlmsghdr *hdr)
936 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
940 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
941 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
944 static void rtnl_dellink(struct nlmsghdr *hdr)
946 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
950 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
951 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
954 static void rtnl_addr(struct nlmsghdr *hdr)
956 struct ifaddrmsg *msg;
960 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
961 bytes = IFA_PAYLOAD(hdr);
963 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
965 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
966 attr = RTA_NEXT(attr, bytes)) {
967 switch (attr->rta_type) {
969 print_inet(attr, "address", msg->ifa_family);
972 print_inet(attr, "local", msg->ifa_family);
975 print_string(attr, "label");
978 print_inet(attr, "broadcast", msg->ifa_family);
981 print_attr(attr, "anycast");
984 print_attr(attr, "cacheinfo");
987 print_attr(attr, "multicast");
990 print_attr(attr, NULL);
996 static void rtnl_newaddr(struct nlmsghdr *hdr)
998 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1002 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1003 msg, IFA_PAYLOAD(hdr));
1006 static void rtnl_deladdr(struct nlmsghdr *hdr)
1008 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1012 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1013 msg, IFA_PAYLOAD(hdr));
1016 static void rtnl_route(struct nlmsghdr *hdr)
1019 struct rtattr *attr;
1022 msg = (struct rtmsg *) NLMSG_DATA(hdr);
1023 bytes = RTM_PAYLOAD(hdr);
1025 print("rtm_family %d rtm_table %d rtm_protocol %d",
1026 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
1027 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
1028 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
1030 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
1031 attr = RTA_NEXT(attr, bytes)) {
1032 switch (attr->rta_type) {
1034 print_inet(attr, "dst", msg->rtm_family);
1037 print_inet(attr, "src", msg->rtm_family);
1040 print_string(attr, "iif");
1043 print_integer(attr, "oif");
1046 print_inet(attr, "gateway", msg->rtm_family);
1049 print_attr(attr, "priority");
1052 print_inet(attr, "prefsrc", msg->rtm_family);
1055 print_attr(attr, "metrics");
1058 print_integer(attr, "table");
1061 print_attr(attr, NULL);
1067 static connman_bool_t is_route_rtmsg(struct rtmsg *msg)
1070 if (msg->rtm_table != RT_TABLE_MAIN)
1073 if (msg->rtm_protocol != RTPROT_BOOT &&
1074 msg->rtm_protocol != RTPROT_KERNEL)
1077 if (msg->rtm_type != RTN_UNICAST)
1083 static void rtnl_newroute(struct nlmsghdr *hdr)
1085 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1089 if (is_route_rtmsg(msg))
1090 process_newroute(msg->rtm_family, msg->rtm_scope,
1091 msg, RTM_PAYLOAD(hdr));
1094 static void rtnl_delroute(struct nlmsghdr *hdr)
1096 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1100 if (is_route_rtmsg(msg))
1101 process_delroute(msg->rtm_family, msg->rtm_scope,
1102 msg, RTM_PAYLOAD(hdr));
1105 static void *rtnl_nd_opt_rdnss(struct nd_opt_hdr *opt, guint32 *lifetime,
1108 guint32 *optint = (void *)opt;
1110 if (opt->nd_opt_len < 3)
1113 if (*lifetime > ntohl(optint[1]))
1114 *lifetime = ntohl(optint[1]);
1116 /* nd_opt_len is in units of 8 bytes. The header is 1 unit (8 bytes)
1117 and each address is another 2 units (16 bytes).
1118 So the number of addresses (given rounding) is nd_opt_len/2 */
1119 *nr_servers = opt->nd_opt_len / 2;
1121 /* And they start 8 bytes into the packet, or two guint32s in. */
1125 static const char **rtnl_nd_opt_dnssl(struct nd_opt_hdr *opt, guint32 *lifetime)
1127 const char **domains = NULL;
1128 guint32 *optint = (void *)opt;
1129 unsigned char *optc = (void *)&optint[2];
1130 int data_len = (opt->nd_opt_len * 8) - 8;
1134 if (*lifetime > ntohl(optint[1]))
1135 *lifetime = ntohl(optint[1]);
1137 /* Turn it into normal strings by converting the length bytes into '.',
1138 and count how many search domains there are while we're at it. */
1140 while (i < data_len) {
1141 if (optc[i] > 0x3f) {
1142 DBG("DNSSL contains compressed elements in violation of RFC6106");
1149 /* Check for double zero */
1150 if (i < data_len && optc[i] == 0)
1158 if (i >= data_len) {
1159 DBG("DNSSL data overflows option length");
1166 domains = g_try_new0(const char *, nr_domains + 1);
1170 /* Now point to the normal strings, missing out the leading '.' that
1171 each of them will have now. */
1172 for (i = 0; i < nr_domains; i++) {
1173 domains[i] = (char *)optc + 1;
1174 optc += strlen((char *)optc) + 1;
1180 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1182 struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1183 struct nd_opt_hdr *opt;
1184 guint32 lifetime = -1;
1185 const char **domains = NULL;
1186 struct in6_addr *servers = NULL;
1187 int i, nr_servers = 0;
1188 int msglen = msg->nduseropt_opts_len;
1191 DBG("family %d index %d len %d type %d code %d",
1192 msg->nduseropt_family, msg->nduseropt_ifindex,
1193 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1194 msg->nduseropt_icmp_code);
1196 if (msg->nduseropt_family != AF_INET6 ||
1197 msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1198 msg->nduseropt_icmp_code != 0)
1201 interface = connman_inet_ifname(msg->nduseropt_ifindex);
1205 for (opt = (void *)&msg[1];
1207 msglen -= opt->nd_opt_len * 8,
1208 opt = ((void *)opt) + opt->nd_opt_len*8) {
1210 DBG("remaining %d nd opt type %d len %d\n",
1211 msglen, opt->nd_opt_type, opt->nd_opt_len);
1213 if (opt->nd_opt_type == 25) { /* ND_OPT_RDNSS */
1216 servers = rtnl_nd_opt_rdnss(opt, &lifetime,
1218 for (i = 0; i < nr_servers; i++) {
1219 if (!inet_ntop(AF_INET6, servers + i, buf,
1223 connman_resolver_append_lifetime(interface,
1224 NULL, buf, lifetime);
1227 } else if (opt->nd_opt_type == 31) { /* ND_OPT_DNSSL */
1230 domains = rtnl_nd_opt_dnssl(opt, &lifetime);
1231 for (i = 0; domains != NULL && domains[i] != NULL; i++)
1232 connman_resolver_append_lifetime(interface,
1233 domains[i], NULL, lifetime);
1241 static const char *type2string(uint16_t type)
1268 case RTM_NEWNDUSEROPT:
1269 return "NEWNDUSEROPT";
1275 static GIOChannel *channel = NULL;
1277 struct rtnl_request {
1278 struct nlmsghdr hdr;
1279 struct rtgenmsg msg;
1281 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1283 static GSList *request_list = NULL;
1284 static guint32 request_seq = 0;
1286 static struct rtnl_request *find_request(guint32 seq)
1290 for (list = request_list; list; list = list->next) {
1291 struct rtnl_request *req = list->data;
1293 if (req->hdr.nlmsg_seq == seq)
1300 static int send_request(struct rtnl_request *req)
1302 struct sockaddr_nl addr;
1305 DBG("%s len %d type %d flags 0x%04x seq %d",
1306 type2string(req->hdr.nlmsg_type),
1307 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1308 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1310 sk = g_io_channel_unix_get_fd(channel);
1312 memset(&addr, 0, sizeof(addr));
1313 addr.nl_family = AF_NETLINK;
1315 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1316 (struct sockaddr *) &addr, sizeof(addr));
1319 static int queue_request(struct rtnl_request *req)
1321 request_list = g_slist_append(request_list, req);
1323 if (g_slist_length(request_list) > 1)
1326 return send_request(req);
1329 static int process_response(guint32 seq)
1331 struct rtnl_request *req;
1335 req = find_request(seq);
1337 request_list = g_slist_remove(request_list, req);
1341 req = g_slist_nth_data(request_list, 0);
1345 return send_request(req);
1348 static void rtnl_message(void *buf, size_t len)
1350 DBG("buf %p len %zd", buf, len);
1353 struct nlmsghdr *hdr = buf;
1354 struct nlmsgerr *err;
1356 if (!NLMSG_OK(hdr, len))
1359 DBG("%s len %d type %d flags 0x%04x seq %d pid %d",
1360 type2string(hdr->nlmsg_type),
1361 hdr->nlmsg_len, hdr->nlmsg_type,
1362 hdr->nlmsg_flags, hdr->nlmsg_seq,
1365 switch (hdr->nlmsg_type) {
1370 process_response(hdr->nlmsg_seq);
1373 err = NLMSG_DATA(hdr);
1374 DBG("error %d (%s)", -err->error,
1375 strerror(-err->error));
1395 case RTM_NEWNDUSEROPT:
1396 rtnl_newnduseropt(hdr);
1400 len -= hdr->nlmsg_len;
1401 buf += hdr->nlmsg_len;
1405 static gboolean netlink_event(GIOChannel *chan,
1406 GIOCondition cond, gpointer data)
1408 unsigned char buf[4096];
1409 struct sockaddr_nl nladdr;
1410 socklen_t addr_len = sizeof(nladdr);
1414 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1417 memset(buf, 0, sizeof(buf));
1418 memset(&nladdr, 0, sizeof(nladdr));
1420 fd = g_io_channel_unix_get_fd(chan);
1422 status = recvfrom(fd, buf, sizeof(buf), 0,
1423 (struct sockaddr *) &nladdr, &addr_len);
1425 if (errno == EINTR || errno == EAGAIN)
1434 if (nladdr.nl_pid != 0) { /* not sent by kernel, ignore */
1435 DBG("Received msg from %u, ignoring it", nladdr.nl_pid);
1439 rtnl_message(buf, status);
1444 static int send_getlink(void)
1446 struct rtnl_request *req;
1450 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1454 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1455 req->hdr.nlmsg_type = RTM_GETLINK;
1456 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1457 req->hdr.nlmsg_pid = 0;
1458 req->hdr.nlmsg_seq = request_seq++;
1459 req->msg.rtgen_family = AF_INET;
1461 return queue_request(req);
1464 static int send_getaddr(void)
1466 struct rtnl_request *req;
1470 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1474 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1475 req->hdr.nlmsg_type = RTM_GETADDR;
1476 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1477 req->hdr.nlmsg_pid = 0;
1478 req->hdr.nlmsg_seq = request_seq++;
1479 req->msg.rtgen_family = AF_INET;
1481 return queue_request(req);
1484 static int send_getroute(void)
1486 struct rtnl_request *req;
1490 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1494 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1495 req->hdr.nlmsg_type = RTM_GETROUTE;
1496 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1497 req->hdr.nlmsg_pid = 0;
1498 req->hdr.nlmsg_seq = request_seq++;
1499 req->msg.rtgen_family = AF_INET;
1501 return queue_request(req);
1504 static gboolean update_timeout_cb(gpointer user_data)
1506 __connman_rtnl_request_update();
1511 static void update_interval_callback(guint min)
1513 if (update_timeout > 0)
1514 g_source_remove(update_timeout);
1516 if (min < G_MAXUINT) {
1517 update_interval = min;
1518 update_timeout = g_timeout_add_seconds(update_interval,
1519 update_timeout_cb, NULL);
1522 update_interval = G_MAXUINT;
1526 static gint compare_interval(gconstpointer a, gconstpointer b)
1528 guint val_a = GPOINTER_TO_UINT(a);
1529 guint val_b = GPOINTER_TO_UINT(b);
1531 return val_a - val_b;
1534 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1541 update_list = g_slist_insert_sorted(update_list,
1542 GUINT_TO_POINTER(interval), compare_interval);
1544 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1545 if (min < update_interval) {
1546 update_interval_callback(min);
1547 __connman_rtnl_request_update();
1550 return update_interval;
1553 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1555 guint min = G_MAXUINT;
1560 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1562 if (update_list != NULL)
1563 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1565 if (min > update_interval)
1566 update_interval_callback(min);
1571 int __connman_rtnl_request_update(void)
1573 return send_getlink();
1576 int __connman_rtnl_init(void)
1578 struct sockaddr_nl addr;
1583 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1584 NULL, free_interface);
1586 sk = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
1590 memset(&addr, 0, sizeof(addr));
1591 addr.nl_family = AF_NETLINK;
1592 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1593 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1594 (1<<(RTNLGRP_ND_USEROPT-1));
1596 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1601 channel = g_io_channel_unix_new(sk);
1602 g_io_channel_set_close_on_unref(channel, TRUE);
1604 g_io_channel_set_encoding(channel, NULL, NULL);
1605 g_io_channel_set_buffered(channel, FALSE);
1607 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1608 netlink_event, NULL);
1613 void __connman_rtnl_start(void)
1622 void __connman_rtnl_cleanup(void)
1628 for (list = watch_list; list; list = list->next) {
1629 struct watch_data *watch = list->data;
1631 DBG("removing watch %d", watch->id);
1637 g_slist_free(watch_list);
1640 g_slist_free(update_list);
1643 for (list = request_list; list; list = list->next) {
1644 struct rtnl_request *req = list->data;
1646 DBG("%s len %d type %d flags 0x%04x seq %d",
1647 type2string(req->hdr.nlmsg_type),
1648 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1649 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1655 g_slist_free(request_list);
1656 request_list = NULL;
1658 g_io_channel_shutdown(channel, TRUE, NULL);
1659 g_io_channel_unref(channel);
1663 g_hash_table_destroy(interface_list);