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)
51 #define ARPHDR_RMNET (530)
55 #define print(arg...) do { if (0) connman_info(arg); } while (0)
56 //#define print(arg...) connman_info(arg)
61 connman_rtnl_link_cb_t newlink;
65 static GSList *watch_list = NULL;
66 static unsigned int watch_id = 0;
68 static GSList *update_list = NULL;
69 static guint update_interval = G_MAXUINT;
70 static guint update_timeout = 0;
72 struct interface_data {
75 enum connman_service_type service_type;
76 enum connman_device_type device_type;
79 static GHashTable *interface_list = NULL;
81 static void free_interface(gpointer data)
83 struct interface_data *interface = data;
85 __connman_technology_remove_interface(interface->service_type,
86 interface->index, interface->ident);
88 g_free(interface->ident);
92 static bool ether_blacklisted(const char *name)
97 if (__connman_device_isfiltered(name))
103 #if !defined TIZEN_EXT
104 static bool wext_interface(char *ifname)
109 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
113 memset(&wrq, 0, sizeof(wrq));
114 strncpy(wrq.ifr_name, ifname, sizeof(wrq.ifr_name) - 1);
116 err = ioctl(fd, SIOCGIWNAME, &wrq);
127 #if defined TIZEN_EXT
128 static bool __connman_rtnl_is_cellular_device(const char *name)
131 char **cellular_interfaces;
133 cellular_interfaces =
134 connman_setting_get_string_list(
135 "NetworkCellularInterfaceList");
136 if (!cellular_interfaces)
139 for (pattern = cellular_interfaces; *pattern; pattern++) {
140 if (g_str_has_prefix(name, *pattern)) {
141 DBG("Cellular interface: %s", name);
150 static void read_uevent(struct interface_data *interface)
152 char *filename, *name, line[128];
156 name = connman_inet_ifname(interface->index);
158 #if defined TIZEN_EXT
159 if (__connman_rtnl_is_cellular_device(name)) {
160 interface->service_type = CONNMAN_SERVICE_TYPE_CELLULAR;
161 interface->device_type = CONNMAN_DEVICE_TYPE_CELLULAR;
166 if (ether_blacklisted(name)) {
167 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
168 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
170 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
171 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
174 filename = g_strdup_printf("/sys/class/net/%s/uevent", name);
176 f = fopen(filename, "re");
181 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
182 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
186 found_devtype = false;
187 while (fgets(line, sizeof(line), f)) {
190 pos = strchr(line, '\n');
195 if (strncmp(line, "DEVTYPE=", 8) != 0)
198 found_devtype = true;
200 if (strcmp(line + 8, "wlan") == 0) {
201 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
202 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
203 } else if (strcmp(line + 8, "wwan") == 0) {
204 interface->service_type = CONNMAN_SERVICE_TYPE_CELLULAR;
205 interface->device_type = CONNMAN_DEVICE_TYPE_CELLULAR;
206 } else if (strcmp(line + 8, "bluetooth") == 0) {
207 interface->service_type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
208 interface->device_type = CONNMAN_DEVICE_TYPE_BLUETOOTH;
209 } else if (strcmp(line + 8, "gadget") == 0) {
210 interface->service_type = CONNMAN_SERVICE_TYPE_GADGET;
211 interface->device_type = CONNMAN_DEVICE_TYPE_GADGET;
212 } else if (strcmp(line + 8, "vlan") == 0) {
213 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
214 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
217 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
218 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
227 #if !defined TIZEN_EXT
228 /* TIZEN does not use old wext interface */
229 /* We haven't got a DEVTYPE, let's check if it's a wireless device */
230 if (wext_interface(name)) {
231 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
232 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
234 connman_error("%s runs an unsupported 802.11 driver", name);
242 enum connman_device_type __connman_rtnl_get_device_type(int index)
244 struct interface_data *interface;
246 interface = g_hash_table_lookup(interface_list,
247 GINT_TO_POINTER(index));
249 return CONNMAN_DEVICE_TYPE_UNKNOWN;
251 return interface->device_type;
255 * connman_rtnl_add_newlink_watch:
256 * @index: network device index
257 * @callback: callback function
258 * @user_data: callback data;
260 * Add a new RTNL watch for newlink events
262 * Returns: %0 on failure and a unique id on success
264 unsigned int connman_rtnl_add_newlink_watch(int index,
265 connman_rtnl_link_cb_t callback, void *user_data)
267 struct watch_data *watch;
269 watch = g_try_new0(struct watch_data, 1);
273 watch->id = ++watch_id;
274 watch->index = index;
276 watch->newlink = callback;
277 watch->user_data = user_data;
279 watch_list = g_slist_prepend(watch_list, watch);
281 DBG("id %d", watch->id);
284 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
287 callback(flags, 0, user_data);
294 * connman_rtnl_remove_watch:
295 * @id: watch identifier
297 * Remove the RTNL watch for the identifier
299 void connman_rtnl_remove_watch(unsigned int id)
308 for (list = watch_list; list; list = list->next) {
309 struct watch_data *watch = list->data;
311 if (watch->id == id) {
312 watch_list = g_slist_remove(watch_list, watch);
319 static void trigger_rtnl(int index, void *user_data)
321 struct connman_rtnl *rtnl = user_data;
324 unsigned short type = __connman_ipconfig_get_type_from_index(index);
325 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
327 rtnl->newlink(type, index, flags, 0);
330 if (rtnl->newgateway) {
331 const char *gateway =
332 __connman_ipconfig_get_gateway_from_index(index,
333 CONNMAN_IPCONFIG_TYPE_ALL);
336 rtnl->newgateway(index, gateway);
340 static GSList *rtnl_list = NULL;
342 static gint compare_priority(gconstpointer a, gconstpointer b)
344 const struct connman_rtnl *rtnl1 = a;
345 const struct connman_rtnl *rtnl2 = b;
347 return rtnl2->priority - rtnl1->priority;
351 * connman_rtnl_register:
354 * Register a new RTNL module
356 * Returns: %0 on success
358 int connman_rtnl_register(struct connman_rtnl *rtnl)
360 DBG("rtnl %p name %s", rtnl, rtnl->name);
362 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
365 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
371 * connman_rtnl_unregister:
374 * Remove a previously registered RTNL module
376 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
378 DBG("rtnl %p name %s", rtnl, rtnl->name);
380 rtnl_list = g_slist_remove(rtnl_list, rtnl);
383 static const char *operstate2str(unsigned char operstate)
386 case IF_OPER_UNKNOWN:
388 case IF_OPER_NOTPRESENT:
389 return "NOT-PRESENT";
392 case IF_OPER_LOWERLAYERDOWN:
393 return "LOWER-LAYER-DOWN";
394 case IF_OPER_TESTING:
396 case IF_OPER_DORMANT:
405 static bool extract_link(struct ifinfomsg *msg, int bytes,
406 struct ether_addr *address, const char **ifname,
407 unsigned int *mtu, unsigned char *operstate,
408 struct rtnl_link_stats *stats)
412 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
413 attr = RTA_NEXT(attr, bytes)) {
414 switch (attr->rta_type) {
417 memcpy(address, RTA_DATA(attr), ETH_ALEN);
421 *ifname = RTA_DATA(attr);
425 *mtu = *((unsigned int *) RTA_DATA(attr));
429 memcpy(stats, RTA_DATA(attr),
430 sizeof(struct rtnl_link_stats));
434 *operstate = *((unsigned char *) RTA_DATA(attr));
446 static void process_newlink(unsigned short type, int index, unsigned flags,
447 unsigned change, struct ifinfomsg *msg, int bytes)
449 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
450 struct rtnl_link_stats stats;
451 unsigned char operstate = 0xff;
452 struct interface_data *interface;
453 const char *ifname = NULL;
454 unsigned int mtu = 0;
455 char ident[13], str[18];
458 memset(&stats, 0, sizeof(stats));
459 if (!extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats))
462 #if defined TIZEN_EXT
463 /* Do not accept Wi-Fi P2P interface */
464 if (g_strrstr(ifname, "p2p") != NULL) {
465 DBG("Newlink event for Wi-Fi P2P interface ignored");
470 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
471 address.ether_addr_octet[0],
472 address.ether_addr_octet[1],
473 address.ether_addr_octet[2],
474 address.ether_addr_octet[3],
475 address.ether_addr_octet[4],
476 address.ether_addr_octet[5]);
478 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
479 address.ether_addr_octet[0],
480 address.ether_addr_octet[1],
481 address.ether_addr_octet[2],
482 address.ether_addr_octet[3],
483 address.ether_addr_octet[4],
484 address.ether_addr_octet[5]);
486 if (flags & IFF_SLAVE) {
487 connman_info("%s {newlink} ignoring slave, index %d address %s",
492 #if defined TIZEN_TV_EXT
493 if (g_strcmp0(ident, "eeeeeeeeeeee") == 0) {
494 DBG("Newlink event with Dummy MAC. Ignored!");
501 case ARPHRD_LOOPBACK:
502 case ARPHDR_PHONET_PIPE:
505 #if defined TIZEN_EXT
507 * Description: ARPHDR_RMNET for QC modem using QMI
511 __connman_ipconfig_newlink(index, type, flags,
516 connman_info("%s {newlink} index %d address %s mtu %u",
517 ifname, index, str, mtu);
519 if (operstate != 0xff)
520 connman_info("%s {newlink} index %d operstate %u <%s>",
521 ifname, index, operstate,
522 operstate2str(operstate));
524 interface = g_hash_table_lookup(interface_list, GINT_TO_POINTER(index));
526 interface = g_new0(struct interface_data, 1);
527 interface->index = index;
528 interface->ident = g_strdup(ident);
530 g_hash_table_insert(interface_list,
531 GINT_TO_POINTER(index), interface);
533 if (type == ARPHRD_ETHER)
534 read_uevent(interface);
535 #if defined TIZEN_EXT
536 if (type == ARPHRD_PPP || type == ARPHDR_RMNET)
537 read_uevent(interface);
539 } else if (g_strcmp0(interface->ident, ident) != 0) {
540 /* If an original address is built-in physical device,
541 * it's hardly get an address at a initial creation
543 __connman_technology_remove_interface(interface->service_type,
544 interface->index, interface->ident);
546 g_free(interface->ident);
547 interface->ident = g_strdup(ident);
549 __connman_technology_add_interface(interface->service_type,
550 interface->index, interface->ident);
557 for (list = rtnl_list; list; list = list->next) {
558 struct connman_rtnl *rtnl = list->data;
561 rtnl->newlink(type, index, flags, change);
565 * The interface needs to be added after the newlink call.
566 * The newlink will create the technology when needed and
567 * __connman_technology_add_interface() expects the
568 * technology to be there already.
571 __connman_technology_add_interface(interface->service_type,
572 interface->index, interface->ident);
574 for (list = watch_list; list; list = list->next) {
575 struct watch_data *watch = list->data;
577 if (watch->index != index)
581 watch->newlink(flags, change, watch->user_data);
585 static void process_dellink(unsigned short type, int index, unsigned flags,
586 unsigned change, struct ifinfomsg *msg, int bytes)
588 struct rtnl_link_stats stats;
589 unsigned char operstate = 0xff;
590 const char *ifname = NULL;
593 memset(&stats, 0, sizeof(stats));
594 if (!extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats))
597 if (operstate != 0xff)
598 connman_info("%s {dellink} index %d operstate %u <%s>",
599 ifname, index, operstate,
600 operstate2str(operstate));
602 for (list = rtnl_list; list; list = list->next) {
603 struct connman_rtnl *rtnl = list->data;
606 rtnl->dellink(type, index, flags, change);
611 case ARPHRD_LOOPBACK:
612 case ARPHDR_PHONET_PIPE:
615 #if defined TIZEN_EXT
617 * Description: SLP requires ARPHRD_PPP for PPP type device
618 * ARPHDR_RMNET for QC modem using QMI
622 __connman_ipconfig_dellink(index, &stats);
626 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
629 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
631 struct in_addr *local,
632 struct in_addr *address,
633 struct in_addr *broadcast)
637 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
638 attr = RTA_NEXT(attr, bytes)) {
639 switch (attr->rta_type) {
642 *address = *((struct in_addr *) RTA_DATA(attr));
646 *local = *((struct in_addr *) RTA_DATA(attr));
650 *broadcast = *((struct in_addr *) RTA_DATA(attr));
654 *label = RTA_DATA(attr);
660 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
661 struct in6_addr *addr,
662 struct in6_addr *local)
666 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
667 attr = RTA_NEXT(attr, bytes)) {
668 switch (attr->rta_type) {
671 *addr = *((struct in6_addr *) RTA_DATA(attr));
675 *local = *((struct in6_addr *) RTA_DATA(attr));
681 static void process_newaddr(unsigned char family, unsigned char prefixlen,
682 int index, struct ifaddrmsg *msg, int bytes)
684 struct in_addr ipv4_addr = { INADDR_ANY };
685 struct in6_addr ipv6_address, ipv6_local;
686 const char *label = NULL;
688 char ip_string[INET6_ADDRSTRLEN];
690 if (family == AF_INET) {
692 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
694 } else if (family == AF_INET6) {
695 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
696 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
704 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
707 if (__connman_ipconfig_newaddr(index, family, label,
708 prefixlen, ip_string) >= 0) {
709 if (family == AF_INET6) {
711 * Re-create RDNSS configured servers if there
712 * are any for this interface. This is done
713 * because we might have now properly
714 * configured interface with proper
715 * autoconfigured address.
717 __connman_resolver_redo_servers(index);
722 static void process_deladdr(unsigned char family, unsigned char prefixlen,
723 int index, struct ifaddrmsg *msg, int bytes)
725 struct in_addr ipv4_addr = { INADDR_ANY };
726 struct in6_addr ipv6_address, ipv6_local;
727 const char *label = NULL;
729 char ip_string[INET6_ADDRSTRLEN];
731 if (family == AF_INET) {
732 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
734 } else if (family == AF_INET6) {
735 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
736 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
744 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
747 __connman_ipconfig_deladdr(index, family, label,
748 prefixlen, ip_string);
751 static void extract_ipv4_route(struct rtmsg *msg, int bytes, int *index,
753 struct in_addr *gateway)
757 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
758 attr = RTA_NEXT(attr, bytes)) {
759 switch (attr->rta_type) {
762 *dst = *((struct in_addr *) RTA_DATA(attr));
766 *gateway = *((struct in_addr *) RTA_DATA(attr));
770 *index = *((int *) RTA_DATA(attr));
776 static void extract_ipv6_route(struct rtmsg *msg, int bytes, int *index,
777 struct in6_addr *dst,
778 struct in6_addr *gateway)
782 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
783 attr = RTA_NEXT(attr, bytes)) {
784 switch (attr->rta_type) {
787 *dst = *((struct in6_addr *) RTA_DATA(attr));
792 *((struct in6_addr *) RTA_DATA(attr));
796 *index = *((int *) RTA_DATA(attr));
802 static void process_newroute(unsigned char family, unsigned char scope,
803 struct rtmsg *msg, int bytes)
806 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
809 if (family == AF_INET) {
810 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
812 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
814 inet_ntop(family, &dst, dststr, sizeof(dststr));
815 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
817 __connman_ipconfig_newroute(index, family, scope, dststr,
820 /* skip host specific routes */
821 if (scope != RT_SCOPE_UNIVERSE &&
822 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
825 if (dst.s_addr != INADDR_ANY)
828 } else if (family == AF_INET6) {
829 struct in6_addr dst = IN6ADDR_ANY_INIT,
830 gateway = IN6ADDR_ANY_INIT;
832 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
834 inet_ntop(family, &dst, dststr, sizeof(dststr));
835 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
837 __connman_ipconfig_newroute(index, family, scope, dststr,
840 /* skip host specific routes */
841 if (scope != RT_SCOPE_UNIVERSE &&
842 !(scope == RT_SCOPE_LINK &&
843 IN6_IS_ADDR_UNSPECIFIED(&dst)))
846 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
851 for (list = rtnl_list; list; list = list->next) {
852 struct connman_rtnl *rtnl = list->data;
854 if (rtnl->newgateway)
855 rtnl->newgateway(index, gatewaystr);
859 static void process_delroute(unsigned char family, unsigned char scope,
860 struct rtmsg *msg, int bytes)
863 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
866 if (family == AF_INET) {
867 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
869 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
871 inet_ntop(family, &dst, dststr, sizeof(dststr));
872 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
874 __connman_ipconfig_delroute(index, family, scope, dststr,
877 /* skip host specific routes */
878 if (scope != RT_SCOPE_UNIVERSE &&
879 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
882 if (dst.s_addr != INADDR_ANY)
885 } else if (family == AF_INET6) {
886 struct in6_addr dst = IN6ADDR_ANY_INIT,
887 gateway = IN6ADDR_ANY_INIT;
889 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
891 inet_ntop(family, &dst, dststr, sizeof(dststr));
892 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
894 __connman_ipconfig_delroute(index, family, scope, dststr,
897 /* skip host specific routes */
898 if (scope != RT_SCOPE_UNIVERSE &&
899 !(scope == RT_SCOPE_LINK &&
900 IN6_IS_ADDR_UNSPECIFIED(&dst)))
903 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
908 for (list = rtnl_list; list; list = list->next) {
909 struct connman_rtnl *rtnl = list->data;
911 if (rtnl->delgateway)
912 rtnl->delgateway(index, gatewaystr);
916 static inline void print_ether(struct rtattr *attr, const char *name)
918 int len = (int) RTA_PAYLOAD(attr);
920 if (len == ETH_ALEN) {
921 struct ether_addr eth;
922 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
923 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
925 print(" attr %s (len %d)\n", name, len);
928 static inline void print_inet(struct rtattr *attr, const char *name,
929 unsigned char family)
931 int len = (int) RTA_PAYLOAD(attr);
933 if (family == AF_INET && len == sizeof(struct in_addr)) {
935 addr = *((struct in_addr *) RTA_DATA(attr));
936 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
938 print(" attr %s (len %d)\n", name, len);
941 static inline void print_string(struct rtattr *attr, const char *name)
943 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
944 (char *) RTA_DATA(attr));
947 static inline void print_byte(struct rtattr *attr, const char *name)
949 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
950 *((unsigned char *) RTA_DATA(attr)));
953 static inline void print_integer(struct rtattr *attr, const char *name)
955 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
956 *((int *) RTA_DATA(attr)));
959 static inline void print_attr(struct rtattr *attr, const char *name)
961 int len = (int) RTA_PAYLOAD(attr);
964 print(" attr %s (len %d)\n", name, len);
966 print(" attr %d (len %d)\n", attr->rta_type, len);
969 static void rtnl_link(struct nlmsghdr *hdr)
971 struct ifinfomsg *msg;
975 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
976 bytes = IFLA_PAYLOAD(hdr);
978 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
980 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
981 attr = RTA_NEXT(attr, bytes)) {
982 switch (attr->rta_type) {
984 print_ether(attr, "address");
987 print_ether(attr, "broadcast");
990 print_string(attr, "ifname");
993 print_integer(attr, "mtu");
996 print_attr(attr, "link");
999 print_attr(attr, "qdisc");
1002 print_attr(attr, "stats");
1005 print_attr(attr, "cost");
1008 print_attr(attr, "priority");
1011 print_attr(attr, "master");
1014 print_attr(attr, "wireless");
1017 print_attr(attr, "protinfo");
1020 print_integer(attr, "txqlen");
1023 print_attr(attr, "map");
1026 print_attr(attr, "weight");
1028 case IFLA_OPERSTATE:
1029 print_byte(attr, "operstate");
1032 print_byte(attr, "linkmode");
1035 print_attr(attr, NULL);
1041 static void rtnl_newlink(struct nlmsghdr *hdr)
1043 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
1047 if (hdr->nlmsg_type == IFLA_WIRELESS)
1048 connman_warn_once("Obsolete WEXT WiFi driver detected");
1050 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
1051 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
1054 static void rtnl_dellink(struct nlmsghdr *hdr)
1056 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
1060 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
1061 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
1064 static void rtnl_addr(struct nlmsghdr *hdr)
1066 struct ifaddrmsg *msg;
1067 struct rtattr *attr;
1070 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1071 bytes = IFA_PAYLOAD(hdr);
1073 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
1075 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
1076 attr = RTA_NEXT(attr, bytes)) {
1077 switch (attr->rta_type) {
1079 print_inet(attr, "address", msg->ifa_family);
1082 print_inet(attr, "local", msg->ifa_family);
1085 print_string(attr, "label");
1088 print_inet(attr, "broadcast", msg->ifa_family);
1091 print_attr(attr, "anycast");
1094 print_attr(attr, "cacheinfo");
1097 print_attr(attr, "multicast");
1100 print_attr(attr, NULL);
1106 static void rtnl_newaddr(struct nlmsghdr *hdr)
1108 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1112 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1113 msg, IFA_PAYLOAD(hdr));
1116 static void rtnl_deladdr(struct nlmsghdr *hdr)
1118 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1122 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1123 msg, IFA_PAYLOAD(hdr));
1126 static void rtnl_route(struct nlmsghdr *hdr)
1129 struct rtattr *attr;
1132 msg = (struct rtmsg *) NLMSG_DATA(hdr);
1133 bytes = RTM_PAYLOAD(hdr);
1135 print("rtm_family %d rtm_table %d rtm_protocol %d",
1136 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
1137 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
1138 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
1140 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
1141 attr = RTA_NEXT(attr, bytes)) {
1142 switch (attr->rta_type) {
1144 print_inet(attr, "dst", msg->rtm_family);
1147 print_inet(attr, "src", msg->rtm_family);
1150 print_string(attr, "iif");
1153 print_integer(attr, "oif");
1156 print_inet(attr, "gateway", msg->rtm_family);
1159 print_attr(attr, "priority");
1162 print_inet(attr, "prefsrc", msg->rtm_family);
1165 print_attr(attr, "metrics");
1168 print_integer(attr, "table");
1171 print_attr(attr, NULL);
1177 static bool is_route_rtmsg(struct rtmsg *msg)
1179 if (msg->rtm_flags & RTM_F_CLONED)
1182 if (msg->rtm_table != RT_TABLE_MAIN)
1185 if (msg->rtm_protocol != RTPROT_BOOT &&
1186 msg->rtm_protocol != RTPROT_KERNEL)
1189 if (msg->rtm_type != RTN_UNICAST)
1195 static void rtnl_newroute(struct nlmsghdr *hdr)
1197 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1201 if (is_route_rtmsg(msg))
1202 process_newroute(msg->rtm_family, msg->rtm_scope,
1203 msg, RTM_PAYLOAD(hdr));
1206 static void rtnl_delroute(struct nlmsghdr *hdr)
1208 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1212 if (is_route_rtmsg(msg))
1213 process_delroute(msg->rtm_family, msg->rtm_scope,
1214 msg, RTM_PAYLOAD(hdr));
1217 static void *rtnl_nd_opt_rdnss(struct nd_opt_hdr *opt, guint32 *lifetime,
1220 guint32 *optint = (void *)opt;
1222 if (opt->nd_opt_len < 3)
1225 if (*lifetime > ntohl(optint[1]))
1226 *lifetime = ntohl(optint[1]);
1228 /* nd_opt_len is in units of 8 bytes. The header is 1 unit (8 bytes)
1229 and each address is another 2 units (16 bytes).
1230 So the number of addresses (given rounding) is nd_opt_len/2 */
1231 *nr_servers = opt->nd_opt_len / 2;
1233 /* And they start 8 bytes into the packet, or two guint32s in. */
1237 static const char **rtnl_nd_opt_dnssl(struct nd_opt_hdr *opt, guint32 *lifetime)
1239 const char **domains = NULL;
1240 guint32 *optint = (void *)opt;
1241 unsigned char *optc = (void *)&optint[2];
1242 int data_len = (opt->nd_opt_len * 8) - 8;
1246 if (*lifetime > ntohl(optint[1]))
1247 *lifetime = ntohl(optint[1]);
1249 /* Turn it into normal strings by converting the length bytes into '.',
1250 and count how many search domains there are while we're at it. */
1252 while (i < data_len) {
1253 if (optc[i] > 0x3f) {
1254 DBG("DNSSL contains compressed elements in violation of RFC6106");
1261 /* Check for double zero */
1262 if (i < data_len && optc[i] == 0)
1270 if (i >= data_len) {
1271 DBG("DNSSL data overflows option length");
1278 domains = g_try_new0(const char *, nr_domains + 1);
1282 /* Now point to the normal strings, missing out the leading '.' that
1283 each of them will have now. */
1284 for (i = 0; i < nr_domains; i++) {
1285 domains[i] = (char *)optc + 1;
1286 optc += strlen((char *)optc) + 1;
1292 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1294 struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1295 struct nd_opt_hdr *opt;
1296 guint32 lifetime = -1;
1297 const char **domains = NULL;
1298 struct in6_addr *servers = NULL;
1299 int i, nr_servers = 0;
1300 int msglen = msg->nduseropt_opts_len;
1303 DBG("family %d index %d len %d type %d code %d",
1304 msg->nduseropt_family, msg->nduseropt_ifindex,
1305 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1306 msg->nduseropt_icmp_code);
1308 if (msg->nduseropt_family != AF_INET6 ||
1309 msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1310 msg->nduseropt_icmp_code != 0)
1313 index = msg->nduseropt_ifindex;
1317 for (opt = (void *)&msg[1];
1319 msglen -= opt->nd_opt_len * 8,
1320 opt = ((void *)opt) + opt->nd_opt_len*8) {
1322 DBG("remaining %d nd opt type %d len %d\n",
1323 msglen, opt->nd_opt_type, opt->nd_opt_len);
1325 if (opt->nd_opt_type == 25) { /* ND_OPT_RDNSS */
1328 servers = rtnl_nd_opt_rdnss(opt, &lifetime,
1330 for (i = 0; i < nr_servers; i++) {
1331 if (!inet_ntop(AF_INET6, servers + i, buf,
1335 connman_resolver_append_lifetime(index,
1336 NULL, buf, lifetime);
1339 } else if (opt->nd_opt_type == 31) { /* ND_OPT_DNSSL */
1342 domains = rtnl_nd_opt_dnssl(opt, &lifetime);
1343 for (i = 0; domains && domains[i]; i++)
1344 connman_resolver_append_lifetime(index,
1345 domains[i], NULL, lifetime);
1352 static const char *type2string(uint16_t type)
1381 case RTM_NEWNDUSEROPT:
1382 return "NEWNDUSEROPT";
1388 static GIOChannel *channel = NULL;
1389 static guint channel_watch = 0;
1391 struct rtnl_request {
1392 struct nlmsghdr hdr;
1393 struct rtgenmsg msg;
1395 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1397 static GSList *request_list = NULL;
1398 static guint32 request_seq = 0;
1400 static struct rtnl_request *find_request(guint32 seq)
1404 for (list = request_list; list; list = list->next) {
1405 struct rtnl_request *req = list->data;
1407 if (req->hdr.nlmsg_seq == seq)
1414 static int send_request(struct rtnl_request *req)
1416 struct sockaddr_nl addr;
1419 DBG("%s len %d type %d flags 0x%04x seq %d",
1420 type2string(req->hdr.nlmsg_type),
1421 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1422 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1424 sk = g_io_channel_unix_get_fd(channel);
1426 memset(&addr, 0, sizeof(addr));
1427 addr.nl_family = AF_NETLINK;
1429 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1430 (struct sockaddr *) &addr, sizeof(addr));
1433 static int queue_request(struct rtnl_request *req)
1435 request_list = g_slist_append(request_list, req);
1437 if (g_slist_length(request_list) > 1)
1440 return send_request(req);
1443 static int process_response(guint32 seq)
1445 struct rtnl_request *req;
1449 req = find_request(seq);
1451 request_list = g_slist_remove(request_list, req);
1455 req = g_slist_nth_data(request_list, 0);
1459 return send_request(req);
1462 static void rtnl_message(void *buf, size_t len)
1464 DBG("buf %p len %zd", buf, len);
1467 struct nlmsghdr *hdr = buf;
1468 struct nlmsgerr *err;
1470 if (!NLMSG_OK(hdr, len))
1473 DBG("%s len %d type %d flags 0x%04x seq %d pid %d",
1474 type2string(hdr->nlmsg_type),
1475 hdr->nlmsg_len, hdr->nlmsg_type,
1476 hdr->nlmsg_flags, hdr->nlmsg_seq,
1479 switch (hdr->nlmsg_type) {
1484 process_response(hdr->nlmsg_seq);
1487 err = NLMSG_DATA(hdr);
1488 DBG("error %d (%s)", -err->error,
1489 strerror(-err->error));
1509 case RTM_NEWNDUSEROPT:
1510 rtnl_newnduseropt(hdr);
1514 len -= hdr->nlmsg_len;
1515 buf += hdr->nlmsg_len;
1519 static gboolean netlink_event(GIOChannel *chan, GIOCondition cond, gpointer data)
1521 unsigned char buf[4096];
1522 struct sockaddr_nl nladdr;
1523 socklen_t addr_len = sizeof(nladdr);
1527 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1530 memset(buf, 0, sizeof(buf));
1531 memset(&nladdr, 0, sizeof(nladdr));
1533 fd = g_io_channel_unix_get_fd(chan);
1535 status = recvfrom(fd, buf, sizeof(buf), 0,
1536 (struct sockaddr *) &nladdr, &addr_len);
1538 if (errno == EINTR || errno == EAGAIN)
1547 if (nladdr.nl_pid != 0) { /* not sent by kernel, ignore */
1548 DBG("Received msg from %u, ignoring it", nladdr.nl_pid);
1552 rtnl_message(buf, status);
1557 static int send_getlink(void)
1559 struct rtnl_request *req;
1563 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1567 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1568 req->hdr.nlmsg_type = RTM_GETLINK;
1569 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1570 req->hdr.nlmsg_pid = 0;
1571 req->hdr.nlmsg_seq = request_seq++;
1572 req->msg.rtgen_family = AF_INET;
1574 return queue_request(req);
1577 static int send_getaddr(void)
1579 struct rtnl_request *req;
1583 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1587 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1588 req->hdr.nlmsg_type = RTM_GETADDR;
1589 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1590 req->hdr.nlmsg_pid = 0;
1591 req->hdr.nlmsg_seq = request_seq++;
1592 req->msg.rtgen_family = AF_INET;
1594 return queue_request(req);
1597 static int send_getroute(void)
1599 struct rtnl_request *req;
1603 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1607 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1608 req->hdr.nlmsg_type = RTM_GETROUTE;
1609 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1610 req->hdr.nlmsg_pid = 0;
1611 req->hdr.nlmsg_seq = request_seq++;
1612 req->msg.rtgen_family = AF_INET;
1614 return queue_request(req);
1617 static gboolean update_timeout_cb(gpointer user_data)
1619 __connman_rtnl_request_update();
1624 static void update_interval_callback(guint min)
1626 if (update_timeout > 0)
1627 g_source_remove(update_timeout);
1629 if (min < G_MAXUINT) {
1630 update_interval = min;
1631 update_timeout = g_timeout_add_seconds(update_interval,
1632 update_timeout_cb, NULL);
1635 update_interval = G_MAXUINT;
1639 static gint compare_interval(gconstpointer a, gconstpointer b)
1641 guint val_a = GPOINTER_TO_UINT(a);
1642 guint val_b = GPOINTER_TO_UINT(b);
1644 return val_a - val_b;
1647 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1654 update_list = g_slist_insert_sorted(update_list,
1655 GUINT_TO_POINTER(interval), compare_interval);
1657 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1658 if (min < update_interval) {
1659 update_interval_callback(min);
1660 __connman_rtnl_request_update();
1663 return update_interval;
1666 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1668 guint min = G_MAXUINT;
1673 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1676 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1678 if (min > update_interval)
1679 update_interval_callback(min);
1684 int __connman_rtnl_request_update(void)
1686 return send_getlink();
1689 int __connman_rtnl_init(void)
1691 struct sockaddr_nl addr;
1696 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1697 NULL, free_interface);
1699 sk = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
1703 memset(&addr, 0, sizeof(addr));
1704 addr.nl_family = AF_NETLINK;
1705 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1706 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1707 (1<<(RTNLGRP_ND_USEROPT-1));
1709 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1714 channel = g_io_channel_unix_new(sk);
1715 g_io_channel_set_close_on_unref(channel, TRUE);
1717 g_io_channel_set_encoding(channel, NULL, NULL);
1718 g_io_channel_set_buffered(channel, FALSE);
1720 channel_watch = g_io_add_watch(channel,
1721 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1722 netlink_event, NULL);
1727 void __connman_rtnl_start(void)
1736 void __connman_rtnl_cleanup(void)
1742 for (list = watch_list; list; list = list->next) {
1743 struct watch_data *watch = list->data;
1745 DBG("removing watch %d", watch->id);
1751 g_slist_free(watch_list);
1754 g_slist_free(update_list);
1757 for (list = request_list; list; list = list->next) {
1758 struct rtnl_request *req = list->data;
1760 DBG("%s len %d type %d flags 0x%04x seq %d",
1761 type2string(req->hdr.nlmsg_type),
1762 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1763 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1769 g_slist_free(request_list);
1770 request_list = NULL;
1772 if (channel_watch) {
1773 g_source_remove(channel_watch);
1777 g_io_channel_shutdown(channel, TRUE, NULL);
1778 g_io_channel_unref(channel);
1782 g_hash_table_destroy(interface_list);