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 static bool wext_interface(char *ifname)
108 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
112 memset(&wrq, 0, sizeof(wrq));
113 strncpy(wrq.ifr_name, ifname, sizeof(wrq.ifr_name) - 1);
115 err = ioctl(fd, SIOCGIWNAME, &wrq);
125 #if defined TIZEN_EXT
126 static bool __connman_rtnl_is_cellular_device(const char *name)
129 char **cellular_interfaces;
131 cellular_interfaces =
132 connman_setting_get_string_list(
133 "NetworkCellularInterfaceList");
134 if (!cellular_interfaces)
137 for (pattern = cellular_interfaces; *pattern; pattern++) {
138 if (g_str_has_prefix(name, *pattern)) {
139 DBG("Cellular interface: %s", name);
148 static void read_uevent(struct interface_data *interface)
150 char *filename, *name, line[128];
154 name = connman_inet_ifname(interface->index);
156 #if defined TIZEN_EXT
157 if (__connman_rtnl_is_cellular_device(name)) {
158 interface->service_type = CONNMAN_SERVICE_TYPE_CELLULAR;
159 interface->device_type = CONNMAN_DEVICE_TYPE_CELLULAR;
164 if (ether_blacklisted(name)) {
165 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
166 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
168 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
169 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
172 filename = g_strdup_printf("/sys/class/net/%s/uevent", name);
174 f = fopen(filename, "re");
179 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
180 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
184 found_devtype = false;
185 while (fgets(line, sizeof(line), f)) {
188 pos = strchr(line, '\n');
193 if (strncmp(line, "DEVTYPE=", 8) != 0)
196 found_devtype = true;
198 if (strcmp(line + 8, "wlan") == 0) {
199 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
200 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
201 } else if (strcmp(line + 8, "wwan") == 0) {
202 interface->service_type = CONNMAN_SERVICE_TYPE_CELLULAR;
203 interface->device_type = CONNMAN_DEVICE_TYPE_CELLULAR;
204 } else if (strcmp(line + 8, "bluetooth") == 0) {
205 interface->service_type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
206 interface->device_type = CONNMAN_DEVICE_TYPE_BLUETOOTH;
207 } else if (strcmp(line + 8, "gadget") == 0) {
208 interface->service_type = CONNMAN_SERVICE_TYPE_GADGET;
209 interface->device_type = CONNMAN_DEVICE_TYPE_GADGET;
210 } else if (strcmp(line + 8, "vlan") == 0) {
211 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
212 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
215 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
216 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
225 /* We haven't got a DEVTYPE, let's check if it's a wireless device */
226 if (wext_interface(name)) {
227 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
228 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
230 connman_error("%s runs an unsupported 802.11 driver", name);
237 enum connman_device_type __connman_rtnl_get_device_type(int index)
239 struct interface_data *interface;
241 interface = g_hash_table_lookup(interface_list,
242 GINT_TO_POINTER(index));
244 return CONNMAN_DEVICE_TYPE_UNKNOWN;
246 return interface->device_type;
250 * connman_rtnl_add_newlink_watch:
251 * @index: network device index
252 * @callback: callback function
253 * @user_data: callback data;
255 * Add a new RTNL watch for newlink events
257 * Returns: %0 on failure and a unique id on success
259 unsigned int connman_rtnl_add_newlink_watch(int index,
260 connman_rtnl_link_cb_t callback, void *user_data)
262 struct watch_data *watch;
264 watch = g_try_new0(struct watch_data, 1);
268 watch->id = ++watch_id;
269 watch->index = index;
271 watch->newlink = callback;
272 watch->user_data = user_data;
274 watch_list = g_slist_prepend(watch_list, watch);
276 DBG("id %d", watch->id);
279 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
282 callback(flags, 0, user_data);
289 * connman_rtnl_remove_watch:
290 * @id: watch identifier
292 * Remove the RTNL watch for the identifier
294 void connman_rtnl_remove_watch(unsigned int id)
303 for (list = watch_list; list; list = list->next) {
304 struct watch_data *watch = list->data;
306 if (watch->id == id) {
307 watch_list = g_slist_remove(watch_list, watch);
314 static void trigger_rtnl(int index, void *user_data)
316 struct connman_rtnl *rtnl = user_data;
319 unsigned short type = __connman_ipconfig_get_type_from_index(index);
320 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
322 rtnl->newlink(type, index, flags, 0);
325 if (rtnl->newgateway) {
326 const char *gateway =
327 __connman_ipconfig_get_gateway_from_index(index,
328 CONNMAN_IPCONFIG_TYPE_ALL);
331 rtnl->newgateway(index, gateway);
335 static GSList *rtnl_list = NULL;
337 static gint compare_priority(gconstpointer a, gconstpointer b)
339 const struct connman_rtnl *rtnl1 = a;
340 const struct connman_rtnl *rtnl2 = b;
342 return rtnl2->priority - rtnl1->priority;
346 * connman_rtnl_register:
349 * Register a new RTNL module
351 * Returns: %0 on success
353 int connman_rtnl_register(struct connman_rtnl *rtnl)
355 DBG("rtnl %p name %s", rtnl, rtnl->name);
357 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
360 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
366 * connman_rtnl_unregister:
369 * Remove a previously registered RTNL module
371 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
373 DBG("rtnl %p name %s", rtnl, rtnl->name);
375 rtnl_list = g_slist_remove(rtnl_list, rtnl);
378 static const char *operstate2str(unsigned char operstate)
381 case IF_OPER_UNKNOWN:
383 case IF_OPER_NOTPRESENT:
384 return "NOT-PRESENT";
387 case IF_OPER_LOWERLAYERDOWN:
388 return "LOWER-LAYER-DOWN";
389 case IF_OPER_TESTING:
391 case IF_OPER_DORMANT:
400 static bool extract_link(struct ifinfomsg *msg, int bytes,
401 struct ether_addr *address, const char **ifname,
402 unsigned int *mtu, unsigned char *operstate,
403 struct rtnl_link_stats *stats)
407 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
408 attr = RTA_NEXT(attr, bytes)) {
409 switch (attr->rta_type) {
412 memcpy(address, RTA_DATA(attr), ETH_ALEN);
416 *ifname = RTA_DATA(attr);
420 *mtu = *((unsigned int *) RTA_DATA(attr));
424 memcpy(stats, RTA_DATA(attr),
425 sizeof(struct rtnl_link_stats));
429 *operstate = *((unsigned char *) RTA_DATA(attr));
441 static void process_newlink(unsigned short type, int index, unsigned flags,
442 unsigned change, struct ifinfomsg *msg, int bytes)
444 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
445 struct rtnl_link_stats stats;
446 unsigned char operstate = 0xff;
447 struct interface_data *interface;
448 const char *ifname = NULL;
449 unsigned int mtu = 0;
450 char ident[13], str[18];
453 memset(&stats, 0, sizeof(stats));
454 if (!extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats))
457 #if defined TIZEN_EXT
458 /* Do not accept Wi-Fi P2P interface */
459 if (g_strrstr(ifname, "p2p") != NULL) {
460 DBG("Newlink event for Wi-Fi P2P interface ignored");
465 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
466 address.ether_addr_octet[0],
467 address.ether_addr_octet[1],
468 address.ether_addr_octet[2],
469 address.ether_addr_octet[3],
470 address.ether_addr_octet[4],
471 address.ether_addr_octet[5]);
473 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
474 address.ether_addr_octet[0],
475 address.ether_addr_octet[1],
476 address.ether_addr_octet[2],
477 address.ether_addr_octet[3],
478 address.ether_addr_octet[4],
479 address.ether_addr_octet[5]);
481 if (flags & IFF_SLAVE) {
482 connman_info("%s {newlink} ignoring slave, index %d address %s",
487 #if defined TIZEN_TV_EXT
488 if (g_strcmp0(ident, "eeeeeeeeeeee") == 0) {
489 DBG("Newlink event with Dummy MAC. Ignored!");
496 case ARPHRD_LOOPBACK:
497 case ARPHDR_PHONET_PIPE:
500 #if defined TIZEN_EXT
502 * Description: ARPHDR_RMNET for QC modem using QMI
506 __connman_ipconfig_newlink(index, type, flags,
511 connman_info("%s {newlink} index %d address %s mtu %u",
512 ifname, index, str, mtu);
514 if (operstate != 0xff)
515 connman_info("%s {newlink} index %d operstate %u <%s>",
516 ifname, index, operstate,
517 operstate2str(operstate));
519 interface = g_hash_table_lookup(interface_list, GINT_TO_POINTER(index));
521 interface = g_new0(struct interface_data, 1);
522 interface->index = index;
523 interface->ident = g_strdup(ident);
525 g_hash_table_insert(interface_list,
526 GINT_TO_POINTER(index), interface);
528 if (type == ARPHRD_ETHER)
529 read_uevent(interface);
530 #if defined TIZEN_EXT
531 if (type == ARPHRD_PPP || type == ARPHDR_RMNET)
532 read_uevent(interface);
534 } else if (g_strcmp0(interface->ident, ident) != 0) {
535 /* If an original address is built-in physical device,
536 * it's hardly get an address at a initial creation
538 __connman_technology_remove_interface(interface->service_type,
539 interface->index, interface->ident);
541 g_free(interface->ident);
542 interface->ident = g_strdup(ident);
544 __connman_technology_add_interface(interface->service_type,
545 interface->index, interface->ident);
552 for (list = rtnl_list; list; list = list->next) {
553 struct connman_rtnl *rtnl = list->data;
556 rtnl->newlink(type, index, flags, change);
560 * The interface needs to be added after the newlink call.
561 * The newlink will create the technology when needed and
562 * __connman_technology_add_interface() expects the
563 * technology to be there already.
566 __connman_technology_add_interface(interface->service_type,
567 interface->index, interface->ident);
569 for (list = watch_list; list; list = list->next) {
570 struct watch_data *watch = list->data;
572 if (watch->index != index)
576 watch->newlink(flags, change, watch->user_data);
580 static void process_dellink(unsigned short type, int index, unsigned flags,
581 unsigned change, struct ifinfomsg *msg, int bytes)
583 struct rtnl_link_stats stats;
584 unsigned char operstate = 0xff;
585 const char *ifname = NULL;
588 memset(&stats, 0, sizeof(stats));
589 if (!extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats))
592 if (operstate != 0xff)
593 connman_info("%s {dellink} index %d operstate %u <%s>",
594 ifname, index, operstate,
595 operstate2str(operstate));
597 for (list = rtnl_list; list; list = list->next) {
598 struct connman_rtnl *rtnl = list->data;
601 rtnl->dellink(type, index, flags, change);
606 case ARPHRD_LOOPBACK:
607 case ARPHDR_PHONET_PIPE:
610 #if defined TIZEN_EXT
612 * Description: SLP requires ARPHRD_PPP for PPP type device
613 * ARPHDR_RMNET for QC modem using QMI
617 __connman_ipconfig_dellink(index, &stats);
621 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
624 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
626 struct in_addr *local,
627 struct in_addr *address,
628 struct in_addr *broadcast)
632 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
633 attr = RTA_NEXT(attr, bytes)) {
634 switch (attr->rta_type) {
637 *address = *((struct in_addr *) RTA_DATA(attr));
641 *local = *((struct in_addr *) RTA_DATA(attr));
645 *broadcast = *((struct in_addr *) RTA_DATA(attr));
649 *label = RTA_DATA(attr);
655 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
656 struct in6_addr *addr,
657 struct in6_addr *local)
661 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
662 attr = RTA_NEXT(attr, bytes)) {
663 switch (attr->rta_type) {
666 *addr = *((struct in6_addr *) RTA_DATA(attr));
670 *local = *((struct in6_addr *) RTA_DATA(attr));
676 static void process_newaddr(unsigned char family, unsigned char prefixlen,
677 int index, struct ifaddrmsg *msg, int bytes)
679 struct in_addr ipv4_addr = { INADDR_ANY };
680 struct in6_addr ipv6_address, ipv6_local;
681 const char *label = NULL;
683 char ip_string[INET6_ADDRSTRLEN];
685 if (family == AF_INET) {
687 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
689 } else if (family == AF_INET6) {
690 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
691 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
699 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
702 if (__connman_ipconfig_newaddr(index, family, label,
703 prefixlen, ip_string) >= 0) {
704 if (family == AF_INET6) {
706 * Re-create RDNSS configured servers if there
707 * are any for this interface. This is done
708 * because we might have now properly
709 * configured interface with proper
710 * autoconfigured address.
712 __connman_resolver_redo_servers(index);
717 static void process_deladdr(unsigned char family, unsigned char prefixlen,
718 int index, struct ifaddrmsg *msg, int bytes)
720 struct in_addr ipv4_addr = { INADDR_ANY };
721 struct in6_addr ipv6_address, ipv6_local;
722 const char *label = NULL;
724 char ip_string[INET6_ADDRSTRLEN];
726 if (family == AF_INET) {
727 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
729 } else if (family == AF_INET6) {
730 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
731 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
739 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
742 __connman_ipconfig_deladdr(index, family, label,
743 prefixlen, ip_string);
746 static void extract_ipv4_route(struct rtmsg *msg, int bytes, int *index,
748 struct in_addr *gateway)
752 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
753 attr = RTA_NEXT(attr, bytes)) {
754 switch (attr->rta_type) {
757 *dst = *((struct in_addr *) RTA_DATA(attr));
761 *gateway = *((struct in_addr *) RTA_DATA(attr));
765 *index = *((int *) RTA_DATA(attr));
771 static void extract_ipv6_route(struct rtmsg *msg, int bytes, int *index,
772 struct in6_addr *dst,
773 struct in6_addr *gateway)
777 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
778 attr = RTA_NEXT(attr, bytes)) {
779 switch (attr->rta_type) {
782 *dst = *((struct in6_addr *) RTA_DATA(attr));
787 *((struct in6_addr *) RTA_DATA(attr));
791 *index = *((int *) RTA_DATA(attr));
797 static void process_newroute(unsigned char family, unsigned char scope,
798 struct rtmsg *msg, int bytes)
801 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
804 if (family == AF_INET) {
805 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
807 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
809 inet_ntop(family, &dst, dststr, sizeof(dststr));
810 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
812 __connman_ipconfig_newroute(index, family, scope, dststr,
815 /* skip host specific routes */
816 if (scope != RT_SCOPE_UNIVERSE &&
817 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
820 if (dst.s_addr != INADDR_ANY)
823 } else if (family == AF_INET6) {
824 struct in6_addr dst = IN6ADDR_ANY_INIT,
825 gateway = IN6ADDR_ANY_INIT;
827 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
829 inet_ntop(family, &dst, dststr, sizeof(dststr));
830 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
832 __connman_ipconfig_newroute(index, family, scope, dststr,
835 /* skip host specific routes */
836 if (scope != RT_SCOPE_UNIVERSE &&
837 !(scope == RT_SCOPE_LINK &&
838 IN6_IS_ADDR_UNSPECIFIED(&dst)))
841 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
846 for (list = rtnl_list; list; list = list->next) {
847 struct connman_rtnl *rtnl = list->data;
849 if (rtnl->newgateway)
850 rtnl->newgateway(index, gatewaystr);
854 static void process_delroute(unsigned char family, unsigned char scope,
855 struct rtmsg *msg, int bytes)
858 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
861 if (family == AF_INET) {
862 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
864 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
866 inet_ntop(family, &dst, dststr, sizeof(dststr));
867 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
869 __connman_ipconfig_delroute(index, family, scope, dststr,
872 /* skip host specific routes */
873 if (scope != RT_SCOPE_UNIVERSE &&
874 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
877 if (dst.s_addr != INADDR_ANY)
880 } else if (family == AF_INET6) {
881 struct in6_addr dst = IN6ADDR_ANY_INIT,
882 gateway = IN6ADDR_ANY_INIT;
884 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
886 inet_ntop(family, &dst, dststr, sizeof(dststr));
887 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
889 __connman_ipconfig_delroute(index, family, scope, dststr,
892 /* skip host specific routes */
893 if (scope != RT_SCOPE_UNIVERSE &&
894 !(scope == RT_SCOPE_LINK &&
895 IN6_IS_ADDR_UNSPECIFIED(&dst)))
898 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
903 for (list = rtnl_list; list; list = list->next) {
904 struct connman_rtnl *rtnl = list->data;
906 if (rtnl->delgateway)
907 rtnl->delgateway(index, gatewaystr);
911 static inline void print_ether(struct rtattr *attr, const char *name)
913 int len = (int) RTA_PAYLOAD(attr);
915 if (len == ETH_ALEN) {
916 struct ether_addr eth;
917 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
918 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
920 print(" attr %s (len %d)\n", name, len);
923 static inline void print_inet(struct rtattr *attr, const char *name,
924 unsigned char family)
926 int len = (int) RTA_PAYLOAD(attr);
928 if (family == AF_INET && len == sizeof(struct in_addr)) {
930 addr = *((struct in_addr *) RTA_DATA(attr));
931 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
933 print(" attr %s (len %d)\n", name, len);
936 static inline void print_string(struct rtattr *attr, const char *name)
938 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
939 (char *) RTA_DATA(attr));
942 static inline void print_byte(struct rtattr *attr, const char *name)
944 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
945 *((unsigned char *) RTA_DATA(attr)));
948 static inline void print_integer(struct rtattr *attr, const char *name)
950 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
951 *((int *) RTA_DATA(attr)));
954 static inline void print_attr(struct rtattr *attr, const char *name)
956 int len = (int) RTA_PAYLOAD(attr);
959 print(" attr %s (len %d)\n", name, len);
961 print(" attr %d (len %d)\n", attr->rta_type, len);
964 static void rtnl_link(struct nlmsghdr *hdr)
966 struct ifinfomsg *msg;
970 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
971 bytes = IFLA_PAYLOAD(hdr);
973 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
975 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
976 attr = RTA_NEXT(attr, bytes)) {
977 switch (attr->rta_type) {
979 print_ether(attr, "address");
982 print_ether(attr, "broadcast");
985 print_string(attr, "ifname");
988 print_integer(attr, "mtu");
991 print_attr(attr, "link");
994 print_attr(attr, "qdisc");
997 print_attr(attr, "stats");
1000 print_attr(attr, "cost");
1003 print_attr(attr, "priority");
1006 print_attr(attr, "master");
1009 print_attr(attr, "wireless");
1012 print_attr(attr, "protinfo");
1015 print_integer(attr, "txqlen");
1018 print_attr(attr, "map");
1021 print_attr(attr, "weight");
1023 case IFLA_OPERSTATE:
1024 print_byte(attr, "operstate");
1027 print_byte(attr, "linkmode");
1030 print_attr(attr, NULL);
1036 static void rtnl_newlink(struct nlmsghdr *hdr)
1038 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
1042 if (hdr->nlmsg_type == IFLA_WIRELESS)
1043 connman_warn_once("Obsolete WEXT WiFi driver detected");
1045 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
1046 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
1049 static void rtnl_dellink(struct nlmsghdr *hdr)
1051 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
1055 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
1056 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
1059 static void rtnl_addr(struct nlmsghdr *hdr)
1061 struct ifaddrmsg *msg;
1062 struct rtattr *attr;
1065 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1066 bytes = IFA_PAYLOAD(hdr);
1068 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
1070 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
1071 attr = RTA_NEXT(attr, bytes)) {
1072 switch (attr->rta_type) {
1074 print_inet(attr, "address", msg->ifa_family);
1077 print_inet(attr, "local", msg->ifa_family);
1080 print_string(attr, "label");
1083 print_inet(attr, "broadcast", msg->ifa_family);
1086 print_attr(attr, "anycast");
1089 print_attr(attr, "cacheinfo");
1092 print_attr(attr, "multicast");
1095 print_attr(attr, NULL);
1101 static void rtnl_newaddr(struct nlmsghdr *hdr)
1103 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1107 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1108 msg, IFA_PAYLOAD(hdr));
1111 static void rtnl_deladdr(struct nlmsghdr *hdr)
1113 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1117 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1118 msg, IFA_PAYLOAD(hdr));
1121 static void rtnl_route(struct nlmsghdr *hdr)
1124 struct rtattr *attr;
1127 msg = (struct rtmsg *) NLMSG_DATA(hdr);
1128 bytes = RTM_PAYLOAD(hdr);
1130 print("rtm_family %d rtm_table %d rtm_protocol %d",
1131 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
1132 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
1133 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
1135 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
1136 attr = RTA_NEXT(attr, bytes)) {
1137 switch (attr->rta_type) {
1139 print_inet(attr, "dst", msg->rtm_family);
1142 print_inet(attr, "src", msg->rtm_family);
1145 print_string(attr, "iif");
1148 print_integer(attr, "oif");
1151 print_inet(attr, "gateway", msg->rtm_family);
1154 print_attr(attr, "priority");
1157 print_inet(attr, "prefsrc", msg->rtm_family);
1160 print_attr(attr, "metrics");
1163 print_integer(attr, "table");
1166 print_attr(attr, NULL);
1172 static bool is_route_rtmsg(struct rtmsg *msg)
1174 if (msg->rtm_flags & RTM_F_CLONED)
1177 if (msg->rtm_table != RT_TABLE_MAIN)
1180 if (msg->rtm_protocol != RTPROT_BOOT &&
1181 msg->rtm_protocol != RTPROT_KERNEL)
1184 if (msg->rtm_type != RTN_UNICAST)
1190 static void rtnl_newroute(struct nlmsghdr *hdr)
1192 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1196 if (is_route_rtmsg(msg))
1197 process_newroute(msg->rtm_family, msg->rtm_scope,
1198 msg, RTM_PAYLOAD(hdr));
1201 static void rtnl_delroute(struct nlmsghdr *hdr)
1203 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1207 if (is_route_rtmsg(msg))
1208 process_delroute(msg->rtm_family, msg->rtm_scope,
1209 msg, RTM_PAYLOAD(hdr));
1212 static void *rtnl_nd_opt_rdnss(struct nd_opt_hdr *opt, guint32 *lifetime,
1215 guint32 *optint = (void *)opt;
1217 if (opt->nd_opt_len < 3)
1220 if (*lifetime > ntohl(optint[1]))
1221 *lifetime = ntohl(optint[1]);
1223 /* nd_opt_len is in units of 8 bytes. The header is 1 unit (8 bytes)
1224 and each address is another 2 units (16 bytes).
1225 So the number of addresses (given rounding) is nd_opt_len/2 */
1226 *nr_servers = opt->nd_opt_len / 2;
1228 /* And they start 8 bytes into the packet, or two guint32s in. */
1232 static const char **rtnl_nd_opt_dnssl(struct nd_opt_hdr *opt, guint32 *lifetime)
1234 const char **domains = NULL;
1235 guint32 *optint = (void *)opt;
1236 unsigned char *optc = (void *)&optint[2];
1237 int data_len = (opt->nd_opt_len * 8) - 8;
1241 if (*lifetime > ntohl(optint[1]))
1242 *lifetime = ntohl(optint[1]);
1244 /* Turn it into normal strings by converting the length bytes into '.',
1245 and count how many search domains there are while we're at it. */
1247 while (i < data_len) {
1248 if (optc[i] > 0x3f) {
1249 DBG("DNSSL contains compressed elements in violation of RFC6106");
1256 /* Check for double zero */
1257 if (i < data_len && optc[i] == 0)
1265 if (i >= data_len) {
1266 DBG("DNSSL data overflows option length");
1273 domains = g_try_new0(const char *, nr_domains + 1);
1277 /* Now point to the normal strings, missing out the leading '.' that
1278 each of them will have now. */
1279 for (i = 0; i < nr_domains; i++) {
1280 domains[i] = (char *)optc + 1;
1281 optc += strlen((char *)optc) + 1;
1287 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1289 struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1290 struct nd_opt_hdr *opt;
1291 guint32 lifetime = -1;
1292 const char **domains = NULL;
1293 struct in6_addr *servers = NULL;
1294 int i, nr_servers = 0;
1295 int msglen = msg->nduseropt_opts_len;
1298 DBG("family %d index %d len %d type %d code %d",
1299 msg->nduseropt_family, msg->nduseropt_ifindex,
1300 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1301 msg->nduseropt_icmp_code);
1303 if (msg->nduseropt_family != AF_INET6 ||
1304 msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1305 msg->nduseropt_icmp_code != 0)
1308 index = msg->nduseropt_ifindex;
1312 for (opt = (void *)&msg[1];
1314 msglen -= opt->nd_opt_len * 8,
1315 opt = ((void *)opt) + opt->nd_opt_len*8) {
1317 DBG("remaining %d nd opt type %d len %d\n",
1318 msglen, opt->nd_opt_type, opt->nd_opt_len);
1320 if (opt->nd_opt_type == 25) { /* ND_OPT_RDNSS */
1323 servers = rtnl_nd_opt_rdnss(opt, &lifetime,
1325 for (i = 0; i < nr_servers; i++) {
1326 if (!inet_ntop(AF_INET6, servers + i, buf,
1330 connman_resolver_append_lifetime(index,
1331 NULL, buf, lifetime);
1334 } else if (opt->nd_opt_type == 31) { /* ND_OPT_DNSSL */
1337 domains = rtnl_nd_opt_dnssl(opt, &lifetime);
1338 for (i = 0; domains && domains[i]; i++)
1339 connman_resolver_append_lifetime(index,
1340 domains[i], NULL, lifetime);
1347 static const char *type2string(uint16_t type)
1376 case RTM_NEWNDUSEROPT:
1377 return "NEWNDUSEROPT";
1383 static GIOChannel *channel = NULL;
1384 static guint channel_watch = 0;
1386 struct rtnl_request {
1387 struct nlmsghdr hdr;
1388 struct rtgenmsg msg;
1390 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1392 static GSList *request_list = NULL;
1393 static guint32 request_seq = 0;
1395 static struct rtnl_request *find_request(guint32 seq)
1399 for (list = request_list; list; list = list->next) {
1400 struct rtnl_request *req = list->data;
1402 if (req->hdr.nlmsg_seq == seq)
1409 static int send_request(struct rtnl_request *req)
1411 struct sockaddr_nl addr;
1414 DBG("%s len %d type %d flags 0x%04x seq %d",
1415 type2string(req->hdr.nlmsg_type),
1416 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1417 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1419 sk = g_io_channel_unix_get_fd(channel);
1421 memset(&addr, 0, sizeof(addr));
1422 addr.nl_family = AF_NETLINK;
1424 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1425 (struct sockaddr *) &addr, sizeof(addr));
1428 static int queue_request(struct rtnl_request *req)
1430 request_list = g_slist_append(request_list, req);
1432 if (g_slist_length(request_list) > 1)
1435 return send_request(req);
1438 static int process_response(guint32 seq)
1440 struct rtnl_request *req;
1444 req = find_request(seq);
1446 request_list = g_slist_remove(request_list, req);
1450 req = g_slist_nth_data(request_list, 0);
1454 return send_request(req);
1457 static void rtnl_message(void *buf, size_t len)
1459 DBG("buf %p len %zd", buf, len);
1462 struct nlmsghdr *hdr = buf;
1463 struct nlmsgerr *err;
1465 if (!NLMSG_OK(hdr, len))
1468 DBG("%s len %d type %d flags 0x%04x seq %d pid %d",
1469 type2string(hdr->nlmsg_type),
1470 hdr->nlmsg_len, hdr->nlmsg_type,
1471 hdr->nlmsg_flags, hdr->nlmsg_seq,
1474 switch (hdr->nlmsg_type) {
1479 process_response(hdr->nlmsg_seq);
1482 err = NLMSG_DATA(hdr);
1483 DBG("error %d (%s)", -err->error,
1484 strerror(-err->error));
1504 case RTM_NEWNDUSEROPT:
1505 rtnl_newnduseropt(hdr);
1509 len -= hdr->nlmsg_len;
1510 buf += hdr->nlmsg_len;
1514 static gboolean netlink_event(GIOChannel *chan, GIOCondition cond, gpointer data)
1516 unsigned char buf[4096];
1517 struct sockaddr_nl nladdr;
1518 socklen_t addr_len = sizeof(nladdr);
1522 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1525 memset(buf, 0, sizeof(buf));
1526 memset(&nladdr, 0, sizeof(nladdr));
1528 fd = g_io_channel_unix_get_fd(chan);
1530 status = recvfrom(fd, buf, sizeof(buf), 0,
1531 (struct sockaddr *) &nladdr, &addr_len);
1533 if (errno == EINTR || errno == EAGAIN)
1542 if (nladdr.nl_pid != 0) { /* not sent by kernel, ignore */
1543 DBG("Received msg from %u, ignoring it", nladdr.nl_pid);
1547 rtnl_message(buf, status);
1552 static int send_getlink(void)
1554 struct rtnl_request *req;
1558 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1562 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1563 req->hdr.nlmsg_type = RTM_GETLINK;
1564 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1565 req->hdr.nlmsg_pid = 0;
1566 req->hdr.nlmsg_seq = request_seq++;
1567 req->msg.rtgen_family = AF_INET;
1569 return queue_request(req);
1572 static int send_getaddr(void)
1574 struct rtnl_request *req;
1578 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1582 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1583 req->hdr.nlmsg_type = RTM_GETADDR;
1584 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1585 req->hdr.nlmsg_pid = 0;
1586 req->hdr.nlmsg_seq = request_seq++;
1587 req->msg.rtgen_family = AF_INET;
1589 return queue_request(req);
1592 static int send_getroute(void)
1594 struct rtnl_request *req;
1598 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1602 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1603 req->hdr.nlmsg_type = RTM_GETROUTE;
1604 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1605 req->hdr.nlmsg_pid = 0;
1606 req->hdr.nlmsg_seq = request_seq++;
1607 req->msg.rtgen_family = AF_INET;
1609 return queue_request(req);
1612 static gboolean update_timeout_cb(gpointer user_data)
1614 __connman_rtnl_request_update();
1619 static void update_interval_callback(guint min)
1621 if (update_timeout > 0)
1622 g_source_remove(update_timeout);
1624 if (min < G_MAXUINT) {
1625 update_interval = min;
1626 update_timeout = g_timeout_add_seconds(update_interval,
1627 update_timeout_cb, NULL);
1630 update_interval = G_MAXUINT;
1634 static gint compare_interval(gconstpointer a, gconstpointer b)
1636 guint val_a = GPOINTER_TO_UINT(a);
1637 guint val_b = GPOINTER_TO_UINT(b);
1639 return val_a - val_b;
1642 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1649 update_list = g_slist_insert_sorted(update_list,
1650 GUINT_TO_POINTER(interval), compare_interval);
1652 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1653 if (min < update_interval) {
1654 update_interval_callback(min);
1655 __connman_rtnl_request_update();
1658 return update_interval;
1661 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1663 guint min = G_MAXUINT;
1668 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1671 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1673 if (min > update_interval)
1674 update_interval_callback(min);
1679 int __connman_rtnl_request_update(void)
1681 return send_getlink();
1684 int __connman_rtnl_init(void)
1686 struct sockaddr_nl addr;
1691 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1692 NULL, free_interface);
1694 sk = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
1698 memset(&addr, 0, sizeof(addr));
1699 addr.nl_family = AF_NETLINK;
1700 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1701 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1702 (1<<(RTNLGRP_ND_USEROPT-1));
1704 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1709 channel = g_io_channel_unix_new(sk);
1710 g_io_channel_set_close_on_unref(channel, TRUE);
1712 g_io_channel_set_encoding(channel, NULL, NULL);
1713 g_io_channel_set_buffered(channel, FALSE);
1715 channel_watch = g_io_add_watch(channel,
1716 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1717 netlink_event, NULL);
1722 void __connman_rtnl_start(void)
1731 void __connman_rtnl_cleanup(void)
1737 for (list = watch_list; list; list = list->next) {
1738 struct watch_data *watch = list->data;
1740 DBG("removing watch %d", watch->id);
1746 g_slist_free(watch_list);
1749 g_slist_free(update_list);
1752 for (list = request_list; list; list = list->next) {
1753 struct rtnl_request *req = list->data;
1755 DBG("%s len %d type %d flags 0x%04x seq %d",
1756 type2string(req->hdr.nlmsg_type),
1757 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1758 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1764 g_slist_free(request_list);
1765 request_list = NULL;
1767 if (channel_watch) {
1768 g_source_remove(channel_watch);
1772 g_io_channel_shutdown(channel, TRUE, NULL);
1773 g_io_channel_unref(channel);
1777 g_hash_table_destroy(interface_list);