5 * Copyright (C) 2007-2013 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <sys/socket.h>
31 #include <sys/ioctl.h>
32 #include <arpa/inet.h>
33 #include <netinet/ether.h>
34 #include <netinet/icmp6.h>
35 #include <net/if_arp.h>
37 #include <linux/netlink.h>
38 #include <linux/rtnetlink.h>
39 #include <linux/wireless.h>
45 #ifndef ARPHDR_PHONET_PIPE
46 #define ARPHDR_PHONET_PIPE (821)
49 #define print(arg...) do { if (0) connman_info(arg); } while (0)
50 //#define print(arg...) connman_info(arg)
55 connman_rtnl_link_cb_t newlink;
59 static GSList *watch_list = NULL;
60 static unsigned int watch_id = 0;
62 static GSList *update_list = NULL;
63 static guint update_interval = G_MAXUINT;
64 static guint update_timeout = 0;
66 struct interface_data {
69 enum connman_service_type service_type;
70 enum connman_device_type device_type;
73 static GHashTable *interface_list = NULL;
75 static void free_interface(gpointer data)
77 struct interface_data *interface = data;
79 __connman_technology_remove_interface(interface->service_type,
80 interface->index, interface->ident);
82 g_free(interface->ident);
86 static bool ether_blacklisted(const char *name)
91 if (__connman_device_isfiltered(name))
97 static bool wext_interface(char *ifname)
102 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
106 memset(&wrq, 0, sizeof(wrq));
107 strncpy(wrq.ifr_name, ifname, sizeof(wrq.ifr_name) - 1);
109 err = ioctl(fd, SIOCGIWNAME, &wrq);
119 static void read_uevent(struct interface_data *interface)
121 char *filename, *name, line[128];
125 name = connman_inet_ifname(interface->index);
127 if (ether_blacklisted(name)) {
128 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
129 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
132 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
133 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
136 filename = g_strdup_printf("/sys/class/net/%s/uevent", name);
138 f = fopen(filename, "re");
143 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
144 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
148 found_devtype = false;
149 while (fgets(line, sizeof(line), f)) {
152 pos = strchr(line, '\n');
157 if (strncmp(line, "DEVTYPE=", 8) != 0)
160 found_devtype = true;
162 if (strcmp(line + 8, "wlan") == 0) {
163 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
164 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
165 } else if (strcmp(line + 8, "wwan") == 0) {
166 interface->service_type = CONNMAN_SERVICE_TYPE_CELLULAR;
167 interface->device_type = CONNMAN_DEVICE_TYPE_CELLULAR;
168 } else if (strcmp(line + 8, "bluetooth") == 0) {
169 interface->service_type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
170 interface->device_type = CONNMAN_DEVICE_TYPE_BLUETOOTH;
171 } else if (strcmp(line + 8, "gadget") == 0) {
172 interface->service_type = CONNMAN_SERVICE_TYPE_GADGET;
173 interface->device_type = CONNMAN_DEVICE_TYPE_GADGET;
174 } else if (strcmp(line + 8, "vlan") == 0) {
175 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
176 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
177 } else if (strcmp(line + 8, "bond") == 0) {
178 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
179 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
180 } else if (strcmp(line + 8, "dsa") == 0) {
181 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
182 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
184 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
185 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
194 /* We haven't got a DEVTYPE, let's check if it's a wireless device */
195 if (wext_interface(name)) {
196 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
197 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
199 connman_error("%s runs an unsupported 802.11 driver", name);
206 enum connman_device_type __connman_rtnl_get_device_type(int index)
208 struct interface_data *interface;
210 interface = g_hash_table_lookup(interface_list,
211 GINT_TO_POINTER(index));
213 return CONNMAN_DEVICE_TYPE_UNKNOWN;
215 return interface->device_type;
219 * connman_rtnl_add_newlink_watch:
220 * @index: network device index
221 * @callback: callback function
222 * @user_data: callback data;
224 * Add a new RTNL watch for newlink events
226 * Returns: %0 on failure and a unique id on success
228 unsigned int connman_rtnl_add_newlink_watch(int index,
229 connman_rtnl_link_cb_t callback, void *user_data)
231 struct watch_data *watch;
233 watch = g_try_new0(struct watch_data, 1);
237 watch->id = ++watch_id;
238 watch->index = index;
240 watch->newlink = callback;
241 watch->user_data = user_data;
243 watch_list = g_slist_prepend(watch_list, watch);
245 DBG("id %d", watch->id);
248 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
251 callback(flags, 0, user_data);
258 * connman_rtnl_remove_watch:
259 * @id: watch identifier
261 * Remove the RTNL watch for the identifier
263 void connman_rtnl_remove_watch(unsigned int id)
272 for (list = watch_list; list; list = list->next) {
273 struct watch_data *watch = list->data;
275 if (watch->id == id) {
276 watch_list = g_slist_remove(watch_list, watch);
283 static void trigger_rtnl(int index, void *user_data)
285 struct connman_rtnl *rtnl = user_data;
288 unsigned short type = __connman_ipconfig_get_type_from_index(index);
289 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
291 rtnl->newlink(type, index, flags, 0);
294 if (rtnl->newgateway) {
295 const char *gateway =
296 __connman_ipconfig_get_gateway_from_index(index,
297 CONNMAN_IPCONFIG_TYPE_ALL);
300 rtnl->newgateway(index, gateway);
304 static GSList *rtnl_list = NULL;
306 static gint compare_priority(gconstpointer a, gconstpointer b)
308 const struct connman_rtnl *rtnl1 = a;
309 const struct connman_rtnl *rtnl2 = b;
311 return rtnl2->priority - rtnl1->priority;
315 * connman_rtnl_register:
318 * Register a new RTNL module
320 * Returns: %0 on success
322 int connman_rtnl_register(struct connman_rtnl *rtnl)
324 DBG("rtnl %p name %s", rtnl, rtnl->name);
326 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
329 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
335 * connman_rtnl_unregister:
338 * Remove a previously registered RTNL module
340 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
342 DBG("rtnl %p name %s", rtnl, rtnl->name);
344 rtnl_list = g_slist_remove(rtnl_list, rtnl);
347 static const char *operstate2str(unsigned char operstate)
350 case IF_OPER_UNKNOWN:
352 case IF_OPER_NOTPRESENT:
353 return "NOT-PRESENT";
356 case IF_OPER_LOWERLAYERDOWN:
357 return "LOWER-LAYER-DOWN";
358 case IF_OPER_TESTING:
360 case IF_OPER_DORMANT:
369 static bool extract_link(struct ifinfomsg *msg, int bytes,
370 struct ether_addr *address, const char **ifname,
371 unsigned int *mtu, unsigned char *operstate,
372 struct rtnl_link_stats *stats)
376 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
377 attr = RTA_NEXT(attr, bytes)) {
378 switch (attr->rta_type) {
381 memcpy(address, RTA_DATA(attr), ETH_ALEN);
385 *ifname = RTA_DATA(attr);
389 *mtu = *((unsigned int *) RTA_DATA(attr));
393 memcpy(stats, RTA_DATA(attr),
394 sizeof(struct rtnl_link_stats));
398 *operstate = *((unsigned char *) RTA_DATA(attr));
410 static void process_newlink(unsigned short type, int index, unsigned flags,
411 unsigned change, struct ifinfomsg *msg, int bytes)
413 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
414 struct rtnl_link_stats stats;
415 unsigned char operstate = 0xff;
416 struct interface_data *interface;
417 const char *ifname = NULL;
418 unsigned int mtu = 0;
419 char ident[13], str[18];
422 memset(&stats, 0, sizeof(stats));
423 if (!extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats))
426 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
427 address.ether_addr_octet[0],
428 address.ether_addr_octet[1],
429 address.ether_addr_octet[2],
430 address.ether_addr_octet[3],
431 address.ether_addr_octet[4],
432 address.ether_addr_octet[5]);
434 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
435 address.ether_addr_octet[0],
436 address.ether_addr_octet[1],
437 address.ether_addr_octet[2],
438 address.ether_addr_octet[3],
439 address.ether_addr_octet[4],
440 address.ether_addr_octet[5]);
442 if (flags & IFF_SLAVE) {
443 connman_info("%s {newlink} ignoring slave, index %d address %s",
450 case ARPHRD_LOOPBACK:
451 case ARPHDR_PHONET_PIPE:
454 __connman_ipconfig_newlink(index, type, flags,
459 connman_info("%s {newlink} index %d address %s mtu %u",
460 ifname, index, str, mtu);
462 if (operstate != 0xff)
463 connman_info("%s {newlink} index %d operstate %u <%s>",
464 ifname, index, operstate,
465 operstate2str(operstate));
467 interface = g_hash_table_lookup(interface_list, GINT_TO_POINTER(index));
469 interface = g_new0(struct interface_data, 1);
470 interface->index = index;
471 interface->ident = g_strdup(ident);
473 g_hash_table_insert(interface_list,
474 GINT_TO_POINTER(index), interface);
476 if (type == ARPHRD_ETHER)
477 read_uevent(interface);
478 } else if (type == ARPHRD_ETHER && interface->device_type == CONNMAN_DEVICE_TYPE_UNKNOWN)
479 read_uevent(interface);
483 for (list = rtnl_list; list; list = list->next) {
484 struct connman_rtnl *rtnl = list->data;
487 rtnl->newlink(type, index, flags, change);
491 * The interface needs to be added after the newlink call.
492 * The newlink will create the technology when needed and
493 * __connman_technology_add_interface() expects the
494 * technology to be there already.
497 __connman_technology_add_interface(interface->service_type,
498 interface->index, interface->ident);
502 GSList *next = list->next;
503 struct watch_data *watch = list->data;
505 if (watch->index == index && watch->newlink)
506 watch->newlink(flags, change, watch->user_data);
512 static void process_dellink(unsigned short type, int index, unsigned flags,
513 unsigned change, struct ifinfomsg *msg, int bytes)
515 struct rtnl_link_stats stats;
516 unsigned char operstate = 0xff;
517 const char *ifname = NULL;
520 memset(&stats, 0, sizeof(stats));
521 if (!extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats))
524 if (operstate != 0xff)
525 connman_info("%s {dellink} index %d operstate %u <%s>",
526 ifname, index, operstate,
527 operstate2str(operstate));
529 for (list = rtnl_list; list; list = list->next) {
530 struct connman_rtnl *rtnl = list->data;
533 rtnl->dellink(type, index, flags, change);
538 case ARPHRD_LOOPBACK:
539 case ARPHDR_PHONET_PIPE:
542 __connman_ipconfig_dellink(index, &stats);
546 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
549 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
551 struct in_addr *local,
552 struct in_addr *address,
553 struct in_addr *broadcast)
557 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
558 attr = RTA_NEXT(attr, bytes)) {
559 switch (attr->rta_type) {
562 *address = *((struct in_addr *) RTA_DATA(attr));
566 *local = *((struct in_addr *) RTA_DATA(attr));
570 *broadcast = *((struct in_addr *) RTA_DATA(attr));
574 *label = RTA_DATA(attr);
580 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
581 struct in6_addr *addr,
582 struct in6_addr *local)
586 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
587 attr = RTA_NEXT(attr, bytes)) {
588 switch (attr->rta_type) {
591 *addr = *((struct in6_addr *) RTA_DATA(attr));
595 *local = *((struct in6_addr *) RTA_DATA(attr));
601 static void process_newaddr(unsigned char family, unsigned char prefixlen,
602 int index, struct ifaddrmsg *msg, int bytes)
604 struct in_addr ipv4_addr = { INADDR_ANY };
605 struct in6_addr ipv6_address, ipv6_local;
606 const char *label = NULL;
608 char ip_string[INET6_ADDRSTRLEN];
610 if (family == AF_INET) {
612 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
614 } else if (family == AF_INET6) {
615 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
616 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
624 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
627 if (__connman_ipconfig_newaddr(index, family, label,
628 prefixlen, ip_string) >= 0) {
629 if (family == AF_INET6) {
631 * Re-create RDNSS configured servers if there
632 * are any for this interface. This is done
633 * because we might have now properly
634 * configured interface with proper
635 * autoconfigured address.
637 __connman_resolver_redo_servers(index);
642 static void process_deladdr(unsigned char family, unsigned char prefixlen,
643 int index, struct ifaddrmsg *msg, int bytes)
645 struct in_addr ipv4_addr = { INADDR_ANY };
646 struct in6_addr ipv6_address, ipv6_local;
647 const char *label = NULL;
649 char ip_string[INET6_ADDRSTRLEN];
651 if (family == AF_INET) {
652 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
654 } else if (family == AF_INET6) {
655 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
656 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
664 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
667 __connman_ipconfig_deladdr(index, family, label,
668 prefixlen, ip_string);
671 static void extract_ipv4_route(struct rtmsg *msg, int bytes, int *index,
673 struct in_addr *gateway)
677 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
678 attr = RTA_NEXT(attr, bytes)) {
679 switch (attr->rta_type) {
682 *dst = *((struct in_addr *) RTA_DATA(attr));
686 *gateway = *((struct in_addr *) RTA_DATA(attr));
690 *index = *((int *) RTA_DATA(attr));
696 static void extract_ipv6_route(struct rtmsg *msg, int bytes, int *index,
697 struct in6_addr *dst,
698 struct in6_addr *gateway)
702 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
703 attr = RTA_NEXT(attr, bytes)) {
704 switch (attr->rta_type) {
707 *dst = *((struct in6_addr *) RTA_DATA(attr));
712 *((struct in6_addr *) RTA_DATA(attr));
716 *index = *((int *) RTA_DATA(attr));
722 static void process_newroute(unsigned char family, unsigned char scope,
723 struct rtmsg *msg, int bytes)
726 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
729 if (family == AF_INET) {
730 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
732 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
734 inet_ntop(family, &dst, dststr, sizeof(dststr));
735 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
737 __connman_ipconfig_newroute(index, family, scope, dststr,
740 /* skip host specific routes */
741 if (scope != RT_SCOPE_UNIVERSE &&
742 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
745 if (dst.s_addr != INADDR_ANY)
748 } else if (family == AF_INET6) {
749 struct in6_addr dst = IN6ADDR_ANY_INIT,
750 gateway = IN6ADDR_ANY_INIT;
752 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
754 inet_ntop(family, &dst, dststr, sizeof(dststr));
755 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
757 __connman_ipconfig_newroute(index, family, scope, dststr,
760 /* skip host specific routes */
761 if (scope != RT_SCOPE_UNIVERSE &&
762 !(scope == RT_SCOPE_LINK &&
763 IN6_IS_ADDR_UNSPECIFIED(&dst)))
766 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
771 for (list = rtnl_list; list; list = list->next) {
772 struct connman_rtnl *rtnl = list->data;
774 if (rtnl->newgateway)
775 rtnl->newgateway(index, gatewaystr);
779 static void process_delroute(unsigned char family, unsigned char scope,
780 struct rtmsg *msg, int bytes)
783 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
786 if (family == AF_INET) {
787 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
789 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
791 inet_ntop(family, &dst, dststr, sizeof(dststr));
792 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
794 __connman_ipconfig_delroute(index, family, scope, dststr,
797 /* skip host specific routes */
798 if (scope != RT_SCOPE_UNIVERSE &&
799 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
802 if (dst.s_addr != INADDR_ANY)
805 } else if (family == AF_INET6) {
806 struct in6_addr dst = IN6ADDR_ANY_INIT,
807 gateway = IN6ADDR_ANY_INIT;
809 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
811 inet_ntop(family, &dst, dststr, sizeof(dststr));
812 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
814 __connman_ipconfig_delroute(index, family, scope, dststr,
817 /* skip host specific routes */
818 if (scope != RT_SCOPE_UNIVERSE &&
819 !(scope == RT_SCOPE_LINK &&
820 IN6_IS_ADDR_UNSPECIFIED(&dst)))
823 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
828 for (list = rtnl_list; list; list = list->next) {
829 struct connman_rtnl *rtnl = list->data;
831 if (rtnl->delgateway)
832 rtnl->delgateway(index, gatewaystr);
836 static inline void print_ether(struct rtattr *attr, const char *name)
838 int len = (int) RTA_PAYLOAD(attr);
840 if (len == ETH_ALEN) {
841 struct ether_addr eth;
842 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
843 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
845 print(" attr %s (len %d)\n", name, len);
848 static inline void print_inet(struct rtattr *attr, const char *name,
849 unsigned char family)
851 int len = (int) RTA_PAYLOAD(attr);
853 if (family == AF_INET && len == sizeof(struct in_addr)) {
855 addr = *((struct in_addr *) RTA_DATA(attr));
856 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
858 print(" attr %s (len %d)\n", name, len);
861 static inline void print_string(struct rtattr *attr, const char *name)
863 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
864 (char *) RTA_DATA(attr));
867 static inline void print_byte(struct rtattr *attr, const char *name)
869 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
870 *((unsigned char *) RTA_DATA(attr)));
873 static inline void print_integer(struct rtattr *attr, const char *name)
875 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
876 *((int *) RTA_DATA(attr)));
879 static inline void print_attr(struct rtattr *attr, const char *name)
881 int len = (int) RTA_PAYLOAD(attr);
884 print(" attr %s (len %d)\n", name, len);
886 print(" attr %d (len %d)\n", attr->rta_type, len);
889 static void rtnl_link(struct nlmsghdr *hdr)
891 struct ifinfomsg *msg;
895 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
896 bytes = IFLA_PAYLOAD(hdr);
898 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
900 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
901 attr = RTA_NEXT(attr, bytes)) {
902 switch (attr->rta_type) {
904 print_ether(attr, "address");
907 print_ether(attr, "broadcast");
910 print_string(attr, "ifname");
913 print_integer(attr, "mtu");
916 print_attr(attr, "link");
919 print_attr(attr, "qdisc");
922 print_attr(attr, "stats");
925 print_attr(attr, "cost");
928 print_attr(attr, "priority");
931 print_attr(attr, "master");
934 print_attr(attr, "wireless");
937 print_attr(attr, "protinfo");
940 print_integer(attr, "txqlen");
943 print_attr(attr, "map");
946 print_attr(attr, "weight");
949 print_byte(attr, "operstate");
952 print_byte(attr, "linkmode");
955 print_attr(attr, NULL);
961 static void rtnl_newlink(struct nlmsghdr *hdr)
963 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
967 if (hdr->nlmsg_type == IFLA_WIRELESS)
968 connman_warn_once("Obsolete WEXT WiFi driver detected");
970 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
971 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
974 static void rtnl_dellink(struct nlmsghdr *hdr)
976 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
980 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
981 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
984 static void rtnl_addr(struct nlmsghdr *hdr)
986 struct ifaddrmsg *msg;
990 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
991 bytes = IFA_PAYLOAD(hdr);
993 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
995 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
996 attr = RTA_NEXT(attr, bytes)) {
997 switch (attr->rta_type) {
999 print_inet(attr, "address", msg->ifa_family);
1002 print_inet(attr, "local", msg->ifa_family);
1005 print_string(attr, "label");
1008 print_inet(attr, "broadcast", msg->ifa_family);
1011 print_attr(attr, "anycast");
1014 print_attr(attr, "cacheinfo");
1017 print_attr(attr, "multicast");
1020 print_attr(attr, NULL);
1026 static void rtnl_newaddr(struct nlmsghdr *hdr)
1028 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1032 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1033 msg, IFA_PAYLOAD(hdr));
1036 static void rtnl_deladdr(struct nlmsghdr *hdr)
1038 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1042 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1043 msg, IFA_PAYLOAD(hdr));
1046 static void rtnl_route(struct nlmsghdr *hdr)
1049 struct rtattr *attr;
1052 msg = (struct rtmsg *) NLMSG_DATA(hdr);
1053 bytes = RTM_PAYLOAD(hdr);
1055 print("rtm_family %d rtm_table %d rtm_protocol %d",
1056 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
1057 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
1058 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
1060 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
1061 attr = RTA_NEXT(attr, bytes)) {
1062 switch (attr->rta_type) {
1064 print_inet(attr, "dst", msg->rtm_family);
1067 print_inet(attr, "src", msg->rtm_family);
1070 print_string(attr, "iif");
1073 print_integer(attr, "oif");
1076 print_inet(attr, "gateway", msg->rtm_family);
1079 print_attr(attr, "priority");
1082 print_inet(attr, "prefsrc", msg->rtm_family);
1085 print_attr(attr, "metrics");
1088 print_integer(attr, "table");
1091 print_attr(attr, NULL);
1097 static bool is_route_rtmsg(struct rtmsg *msg)
1099 if (msg->rtm_flags & RTM_F_CLONED)
1102 if (msg->rtm_table != RT_TABLE_MAIN)
1105 if (msg->rtm_protocol != RTPROT_BOOT &&
1106 msg->rtm_protocol != RTPROT_KERNEL)
1109 if (msg->rtm_type != RTN_UNICAST)
1115 static void rtnl_newroute(struct nlmsghdr *hdr)
1117 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1121 if (is_route_rtmsg(msg))
1122 process_newroute(msg->rtm_family, msg->rtm_scope,
1123 msg, RTM_PAYLOAD(hdr));
1126 static void rtnl_delroute(struct nlmsghdr *hdr)
1128 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1132 if (is_route_rtmsg(msg))
1133 process_delroute(msg->rtm_family, msg->rtm_scope,
1134 msg, RTM_PAYLOAD(hdr));
1137 static void *rtnl_nd_opt_rdnss(struct nd_opt_hdr *opt, guint32 *lifetime,
1140 guint32 *optint = (void *)opt;
1142 if (opt->nd_opt_len < 3)
1145 if (*lifetime > ntohl(optint[1]))
1146 *lifetime = ntohl(optint[1]);
1148 /* nd_opt_len is in units of 8 bytes. The header is 1 unit (8 bytes)
1149 and each address is another 2 units (16 bytes).
1150 So the number of addresses (given rounding) is nd_opt_len/2 */
1151 *nr_servers = opt->nd_opt_len / 2;
1153 /* And they start 8 bytes into the packet, or two guint32s in. */
1157 static const char **rtnl_nd_opt_dnssl(struct nd_opt_hdr *opt, guint32 *lifetime)
1159 const char **domains = NULL;
1160 guint32 *optint = (void *)opt;
1161 unsigned char *optc = (void *)&optint[2];
1162 int data_len = (opt->nd_opt_len * 8) - 8;
1166 if (*lifetime > ntohl(optint[1]))
1167 *lifetime = ntohl(optint[1]);
1169 /* Turn it into normal strings by converting the length bytes into '.',
1170 and count how many search domains there are while we're at it. */
1172 while (i < data_len) {
1173 if (optc[i] > 0x3f) {
1174 DBG("DNSSL contains compressed elements in violation of RFC6106");
1181 /* Check for double zero */
1182 if (i < data_len && optc[i] == 0)
1190 if (i >= data_len) {
1191 DBG("DNSSL data overflows option length");
1198 domains = g_try_new0(const char *, nr_domains + 1);
1202 /* Now point to the normal strings, missing out the leading '.' that
1203 each of them will have now. */
1204 for (i = 0; i < nr_domains; i++) {
1205 domains[i] = (char *)optc + 1;
1206 optc += strlen((char *)optc) + 1;
1212 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1214 struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1215 struct nd_opt_hdr *opt;
1216 guint32 lifetime = -1;
1217 const char **domains = NULL;
1218 struct in6_addr *servers = NULL;
1219 int i, nr_servers = 0;
1220 int msglen = msg->nduseropt_opts_len;
1223 DBG("family %d index %d len %d type %d code %d",
1224 msg->nduseropt_family, msg->nduseropt_ifindex,
1225 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1226 msg->nduseropt_icmp_code);
1228 if (msg->nduseropt_family != AF_INET6 ||
1229 msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1230 msg->nduseropt_icmp_code != 0)
1233 index = msg->nduseropt_ifindex;
1237 for (opt = (void *)&msg[1];
1239 msglen -= opt->nd_opt_len * 8,
1240 opt = ((void *)opt) + opt->nd_opt_len*8) {
1242 DBG("remaining %d nd opt type %d len %d\n",
1243 msglen, opt->nd_opt_type, opt->nd_opt_len);
1245 if (opt->nd_opt_type == 25) { /* ND_OPT_RDNSS */
1248 servers = rtnl_nd_opt_rdnss(opt, &lifetime,
1250 for (i = 0; i < nr_servers; i++) {
1251 if (!inet_ntop(AF_INET6, servers + i, buf,
1255 connman_resolver_append_lifetime(index,
1256 NULL, buf, lifetime);
1259 } else if (opt->nd_opt_type == 31) { /* ND_OPT_DNSSL */
1262 domains = rtnl_nd_opt_dnssl(opt, &lifetime);
1263 for (i = 0; domains && domains[i]; i++)
1264 connman_resolver_append_lifetime(index,
1265 domains[i], NULL, lifetime);
1272 static const char *type2string(uint16_t type)
1301 case RTM_NEWNDUSEROPT:
1302 return "NEWNDUSEROPT";
1308 static GIOChannel *channel = NULL;
1309 static guint channel_watch = 0;
1311 struct rtnl_request {
1312 struct nlmsghdr hdr;
1313 struct rtgenmsg msg;
1315 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1317 static GSList *request_list = NULL;
1318 static guint32 request_seq = 0;
1320 static struct rtnl_request *find_request(guint32 seq)
1324 for (list = request_list; list; list = list->next) {
1325 struct rtnl_request *req = list->data;
1327 if (req->hdr.nlmsg_seq == seq)
1334 static int send_request(struct rtnl_request *req)
1336 struct sockaddr_nl addr;
1339 DBG("%s len %d type %d flags 0x%04x seq %d",
1340 type2string(req->hdr.nlmsg_type),
1341 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1342 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1344 sk = g_io_channel_unix_get_fd(channel);
1346 memset(&addr, 0, sizeof(addr));
1347 addr.nl_family = AF_NETLINK;
1349 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1350 (struct sockaddr *) &addr, sizeof(addr));
1353 static int queue_request(struct rtnl_request *req)
1355 request_list = g_slist_append(request_list, req);
1357 if (g_slist_length(request_list) > 1)
1360 return send_request(req);
1363 static int process_response(guint32 seq)
1365 struct rtnl_request *req;
1369 req = find_request(seq);
1371 request_list = g_slist_remove(request_list, req);
1375 req = g_slist_nth_data(request_list, 0);
1379 return send_request(req);
1382 static void rtnl_message(void *buf, size_t len)
1385 struct nlmsghdr *hdr = buf;
1386 struct nlmsgerr *err;
1388 if (!NLMSG_OK(hdr, len))
1391 DBG("%s len %u type %u flags 0x%04x seq %u pid %u",
1392 type2string(hdr->nlmsg_type),
1393 hdr->nlmsg_len, hdr->nlmsg_type,
1394 hdr->nlmsg_flags, hdr->nlmsg_seq,
1397 switch (hdr->nlmsg_type) {
1402 process_response(hdr->nlmsg_seq);
1405 err = NLMSG_DATA(hdr);
1406 DBG("error %d (%s)", -err->error,
1407 strerror(-err->error));
1427 case RTM_NEWNDUSEROPT:
1428 rtnl_newnduseropt(hdr);
1432 len -= hdr->nlmsg_len;
1433 buf += hdr->nlmsg_len;
1437 static gboolean netlink_event(GIOChannel *chan, GIOCondition cond, gpointer data)
1439 unsigned char buf[4096];
1440 struct sockaddr_nl nladdr;
1441 socklen_t addr_len = sizeof(nladdr);
1445 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1448 memset(buf, 0, sizeof(buf));
1449 memset(&nladdr, 0, sizeof(nladdr));
1451 fd = g_io_channel_unix_get_fd(chan);
1453 status = recvfrom(fd, buf, sizeof(buf), 0,
1454 (struct sockaddr *) &nladdr, &addr_len);
1456 if (errno == EINTR || errno == EAGAIN)
1465 if (nladdr.nl_pid != 0) { /* not sent by kernel, ignore */
1466 DBG("Received msg from %u, ignoring it", nladdr.nl_pid);
1470 rtnl_message(buf, status);
1475 static int send_getlink(void)
1477 struct rtnl_request *req;
1481 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1485 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1486 req->hdr.nlmsg_type = RTM_GETLINK;
1487 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1488 req->hdr.nlmsg_pid = 0;
1489 req->hdr.nlmsg_seq = request_seq++;
1490 req->msg.rtgen_family = AF_INET;
1492 return queue_request(req);
1495 static int send_getaddr(void)
1497 struct rtnl_request *req;
1501 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1505 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1506 req->hdr.nlmsg_type = RTM_GETADDR;
1507 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1508 req->hdr.nlmsg_pid = 0;
1509 req->hdr.nlmsg_seq = request_seq++;
1510 req->msg.rtgen_family = AF_INET;
1512 return queue_request(req);
1515 static int send_getroute(void)
1517 struct rtnl_request *req;
1521 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1525 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1526 req->hdr.nlmsg_type = RTM_GETROUTE;
1527 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1528 req->hdr.nlmsg_pid = 0;
1529 req->hdr.nlmsg_seq = request_seq++;
1530 req->msg.rtgen_family = AF_INET;
1532 return queue_request(req);
1535 static gboolean update_timeout_cb(gpointer user_data)
1537 __connman_rtnl_request_update();
1542 static void update_interval_callback(guint min)
1544 if (update_timeout > 0)
1545 g_source_remove(update_timeout);
1547 if (min < G_MAXUINT) {
1548 update_interval = min;
1549 update_timeout = g_timeout_add_seconds(update_interval,
1550 update_timeout_cb, NULL);
1553 update_interval = G_MAXUINT;
1557 static gint compare_interval(gconstpointer a, gconstpointer b)
1559 guint val_a = GPOINTER_TO_UINT(a);
1560 guint val_b = GPOINTER_TO_UINT(b);
1562 return val_a - val_b;
1565 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1572 update_list = g_slist_insert_sorted(update_list,
1573 GUINT_TO_POINTER(interval), compare_interval);
1575 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1576 if (min < update_interval) {
1577 update_interval_callback(min);
1578 __connman_rtnl_request_update();
1581 return update_interval;
1584 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1586 guint min = G_MAXUINT;
1591 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1594 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1596 if (min > update_interval)
1597 update_interval_callback(min);
1602 int __connman_rtnl_request_update(void)
1604 return send_getlink();
1607 int __connman_rtnl_init(void)
1609 struct sockaddr_nl addr;
1614 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1615 NULL, free_interface);
1617 sk = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
1621 memset(&addr, 0, sizeof(addr));
1622 addr.nl_family = AF_NETLINK;
1623 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1624 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1625 (1<<(RTNLGRP_ND_USEROPT-1));
1627 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1632 channel = g_io_channel_unix_new(sk);
1633 g_io_channel_set_close_on_unref(channel, TRUE);
1635 g_io_channel_set_encoding(channel, NULL, NULL);
1636 g_io_channel_set_buffered(channel, FALSE);
1638 channel_watch = g_io_add_watch(channel,
1639 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1640 netlink_event, NULL);
1645 void __connman_rtnl_start(void)
1654 void __connman_rtnl_cleanup(void)
1660 for (list = watch_list; list; list = list->next) {
1661 struct watch_data *watch = list->data;
1663 DBG("removing watch %d", watch->id);
1669 g_slist_free(watch_list);
1672 g_slist_free(update_list);
1675 for (list = request_list; list; list = list->next) {
1676 struct rtnl_request *req = list->data;
1678 DBG("%s len %d type %d flags 0x%04x seq %d",
1679 type2string(req->hdr.nlmsg_type),
1680 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1681 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1687 g_slist_free(request_list);
1688 request_list = NULL;
1690 if (channel_watch) {
1691 g_source_remove(channel_watch);
1695 g_io_channel_shutdown(channel, TRUE, NULL);
1696 g_io_channel_unref(channel);
1700 g_hash_table_destroy(interface_list);