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;
181 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
182 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
191 /* We haven't got a DEVTYPE, let's check if it's a wireless device */
192 if (wext_interface(name)) {
193 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
194 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
196 connman_error("%s runs an unsupported 802.11 driver", name);
203 enum connman_device_type __connman_rtnl_get_device_type(int index)
205 struct interface_data *interface;
207 interface = g_hash_table_lookup(interface_list,
208 GINT_TO_POINTER(index));
210 return CONNMAN_DEVICE_TYPE_UNKNOWN;
212 return interface->device_type;
216 * connman_rtnl_add_newlink_watch:
217 * @index: network device index
218 * @callback: callback function
219 * @user_data: callback data;
221 * Add a new RTNL watch for newlink events
223 * Returns: %0 on failure and a unique id on success
225 unsigned int connman_rtnl_add_newlink_watch(int index,
226 connman_rtnl_link_cb_t callback, void *user_data)
228 struct watch_data *watch;
230 watch = g_try_new0(struct watch_data, 1);
234 watch->id = ++watch_id;
235 watch->index = index;
237 watch->newlink = callback;
238 watch->user_data = user_data;
240 watch_list = g_slist_prepend(watch_list, watch);
242 DBG("id %d", watch->id);
245 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
248 callback(flags, 0, user_data);
255 * connman_rtnl_remove_watch:
256 * @id: watch identifier
258 * Remove the RTNL watch for the identifier
260 void connman_rtnl_remove_watch(unsigned int id)
269 for (list = watch_list; list; list = list->next) {
270 struct watch_data *watch = list->data;
272 if (watch->id == id) {
273 watch_list = g_slist_remove(watch_list, watch);
280 static void trigger_rtnl(int index, void *user_data)
282 struct connman_rtnl *rtnl = user_data;
285 unsigned short type = __connman_ipconfig_get_type_from_index(index);
286 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
288 rtnl->newlink(type, index, flags, 0);
291 if (rtnl->newgateway) {
292 const char *gateway =
293 __connman_ipconfig_get_gateway_from_index(index,
294 CONNMAN_IPCONFIG_TYPE_ALL);
297 rtnl->newgateway(index, gateway);
301 static GSList *rtnl_list = NULL;
303 static gint compare_priority(gconstpointer a, gconstpointer b)
305 const struct connman_rtnl *rtnl1 = a;
306 const struct connman_rtnl *rtnl2 = b;
308 return rtnl2->priority - rtnl1->priority;
312 * connman_rtnl_register:
315 * Register a new RTNL module
317 * Returns: %0 on success
319 int connman_rtnl_register(struct connman_rtnl *rtnl)
321 DBG("rtnl %p name %s", rtnl, rtnl->name);
323 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
326 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
332 * connman_rtnl_unregister:
335 * Remove a previously registered RTNL module
337 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
339 DBG("rtnl %p name %s", rtnl, rtnl->name);
341 rtnl_list = g_slist_remove(rtnl_list, rtnl);
344 static const char *operstate2str(unsigned char operstate)
347 case IF_OPER_UNKNOWN:
349 case IF_OPER_NOTPRESENT:
350 return "NOT-PRESENT";
353 case IF_OPER_LOWERLAYERDOWN:
354 return "LOWER-LAYER-DOWN";
355 case IF_OPER_TESTING:
357 case IF_OPER_DORMANT:
366 static bool extract_link(struct ifinfomsg *msg, int bytes,
367 struct ether_addr *address, const char **ifname,
368 unsigned int *mtu, unsigned char *operstate,
369 struct rtnl_link_stats *stats)
373 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
374 attr = RTA_NEXT(attr, bytes)) {
375 switch (attr->rta_type) {
378 memcpy(address, RTA_DATA(attr), ETH_ALEN);
382 *ifname = RTA_DATA(attr);
386 *mtu = *((unsigned int *) RTA_DATA(attr));
390 memcpy(stats, RTA_DATA(attr),
391 sizeof(struct rtnl_link_stats));
395 *operstate = *((unsigned char *) RTA_DATA(attr));
407 static void process_newlink(unsigned short type, int index, unsigned flags,
408 unsigned change, struct ifinfomsg *msg, int bytes)
410 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
411 struct rtnl_link_stats stats;
412 unsigned char operstate = 0xff;
413 struct interface_data *interface;
414 const char *ifname = NULL;
415 unsigned int mtu = 0;
416 char ident[13], str[18];
419 memset(&stats, 0, sizeof(stats));
420 if (!extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats))
423 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
424 address.ether_addr_octet[0],
425 address.ether_addr_octet[1],
426 address.ether_addr_octet[2],
427 address.ether_addr_octet[3],
428 address.ether_addr_octet[4],
429 address.ether_addr_octet[5]);
431 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
432 address.ether_addr_octet[0],
433 address.ether_addr_octet[1],
434 address.ether_addr_octet[2],
435 address.ether_addr_octet[3],
436 address.ether_addr_octet[4],
437 address.ether_addr_octet[5]);
439 if (flags & IFF_SLAVE) {
440 connman_info("%s {newlink} ignoring slave, index %d address %s",
447 case ARPHRD_LOOPBACK:
448 case ARPHDR_PHONET_PIPE:
451 __connman_ipconfig_newlink(index, type, flags,
456 connman_info("%s {newlink} index %d address %s mtu %u",
457 ifname, index, str, mtu);
459 if (operstate != 0xff)
460 connman_info("%s {newlink} index %d operstate %u <%s>",
461 ifname, index, operstate,
462 operstate2str(operstate));
464 interface = g_hash_table_lookup(interface_list, GINT_TO_POINTER(index));
466 interface = g_new0(struct interface_data, 1);
467 interface->index = index;
468 interface->ident = g_strdup(ident);
470 g_hash_table_insert(interface_list,
471 GINT_TO_POINTER(index), interface);
473 if (type == ARPHRD_ETHER)
474 read_uevent(interface);
475 } else if (type == ARPHRD_ETHER && interface->device_type == CONNMAN_DEVICE_TYPE_UNKNOWN)
476 read_uevent(interface);
480 for (list = rtnl_list; list; list = list->next) {
481 struct connman_rtnl *rtnl = list->data;
484 rtnl->newlink(type, index, flags, change);
488 * The interface needs to be added after the newlink call.
489 * The newlink will create the technology when needed and
490 * __connman_technology_add_interface() expects the
491 * technology to be there already.
494 __connman_technology_add_interface(interface->service_type,
495 interface->index, interface->ident);
497 for (list = watch_list; list; list = list->next) {
498 struct watch_data *watch = list->data;
500 if (watch->index != index)
504 watch->newlink(flags, change, watch->user_data);
508 static void process_dellink(unsigned short type, int index, unsigned flags,
509 unsigned change, struct ifinfomsg *msg, int bytes)
511 struct rtnl_link_stats stats;
512 unsigned char operstate = 0xff;
513 const char *ifname = NULL;
516 memset(&stats, 0, sizeof(stats));
517 if (!extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats))
520 if (operstate != 0xff)
521 connman_info("%s {dellink} index %d operstate %u <%s>",
522 ifname, index, operstate,
523 operstate2str(operstate));
525 for (list = rtnl_list; list; list = list->next) {
526 struct connman_rtnl *rtnl = list->data;
529 rtnl->dellink(type, index, flags, change);
534 case ARPHRD_LOOPBACK:
535 case ARPHDR_PHONET_PIPE:
538 __connman_ipconfig_dellink(index, &stats);
542 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
545 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
547 struct in_addr *local,
548 struct in_addr *address,
549 struct in_addr *broadcast)
553 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
554 attr = RTA_NEXT(attr, bytes)) {
555 switch (attr->rta_type) {
558 *address = *((struct in_addr *) RTA_DATA(attr));
562 *local = *((struct in_addr *) RTA_DATA(attr));
566 *broadcast = *((struct in_addr *) RTA_DATA(attr));
570 *label = RTA_DATA(attr);
576 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
577 struct in6_addr *addr,
578 struct in6_addr *local)
582 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
583 attr = RTA_NEXT(attr, bytes)) {
584 switch (attr->rta_type) {
587 *addr = *((struct in6_addr *) RTA_DATA(attr));
591 *local = *((struct in6_addr *) RTA_DATA(attr));
597 static void process_newaddr(unsigned char family, unsigned char prefixlen,
598 int index, struct ifaddrmsg *msg, int bytes)
600 struct in_addr ipv4_addr = { INADDR_ANY };
601 struct in6_addr ipv6_address, ipv6_local;
602 const char *label = NULL;
604 char ip_string[INET6_ADDRSTRLEN];
606 if (family == AF_INET) {
608 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
610 } else if (family == AF_INET6) {
611 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
612 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
620 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
623 if (__connman_ipconfig_newaddr(index, family, label,
624 prefixlen, ip_string) >= 0) {
625 if (family == AF_INET6) {
627 * Re-create RDNSS configured servers if there
628 * are any for this interface. This is done
629 * because we might have now properly
630 * configured interface with proper
631 * autoconfigured address.
633 __connman_resolver_redo_servers(index);
638 static void process_deladdr(unsigned char family, unsigned char prefixlen,
639 int index, struct ifaddrmsg *msg, int bytes)
641 struct in_addr ipv4_addr = { INADDR_ANY };
642 struct in6_addr ipv6_address, ipv6_local;
643 const char *label = NULL;
645 char ip_string[INET6_ADDRSTRLEN];
647 if (family == AF_INET) {
648 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
650 } else if (family == AF_INET6) {
651 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
652 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
660 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
663 __connman_ipconfig_deladdr(index, family, label,
664 prefixlen, ip_string);
667 static void extract_ipv4_route(struct rtmsg *msg, int bytes, int *index,
669 struct in_addr *gateway)
673 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
674 attr = RTA_NEXT(attr, bytes)) {
675 switch (attr->rta_type) {
678 *dst = *((struct in_addr *) RTA_DATA(attr));
682 *gateway = *((struct in_addr *) RTA_DATA(attr));
686 *index = *((int *) RTA_DATA(attr));
692 static void extract_ipv6_route(struct rtmsg *msg, int bytes, int *index,
693 struct in6_addr *dst,
694 struct in6_addr *gateway)
698 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
699 attr = RTA_NEXT(attr, bytes)) {
700 switch (attr->rta_type) {
703 *dst = *((struct in6_addr *) RTA_DATA(attr));
708 *((struct in6_addr *) RTA_DATA(attr));
712 *index = *((int *) RTA_DATA(attr));
718 static void process_newroute(unsigned char family, unsigned char scope,
719 struct rtmsg *msg, int bytes)
722 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
725 if (family == AF_INET) {
726 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
728 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
730 inet_ntop(family, &dst, dststr, sizeof(dststr));
731 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
733 __connman_ipconfig_newroute(index, family, scope, dststr,
736 /* skip host specific routes */
737 if (scope != RT_SCOPE_UNIVERSE &&
738 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
741 if (dst.s_addr != INADDR_ANY)
744 } else if (family == AF_INET6) {
745 struct in6_addr dst = IN6ADDR_ANY_INIT,
746 gateway = IN6ADDR_ANY_INIT;
748 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
750 inet_ntop(family, &dst, dststr, sizeof(dststr));
751 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
753 __connman_ipconfig_newroute(index, family, scope, dststr,
756 /* skip host specific routes */
757 if (scope != RT_SCOPE_UNIVERSE &&
758 !(scope == RT_SCOPE_LINK &&
759 IN6_IS_ADDR_UNSPECIFIED(&dst)))
762 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
767 for (list = rtnl_list; list; list = list->next) {
768 struct connman_rtnl *rtnl = list->data;
770 if (rtnl->newgateway)
771 rtnl->newgateway(index, gatewaystr);
775 static void process_delroute(unsigned char family, unsigned char scope,
776 struct rtmsg *msg, int bytes)
779 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
782 if (family == AF_INET) {
783 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
785 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
787 inet_ntop(family, &dst, dststr, sizeof(dststr));
788 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
790 __connman_ipconfig_delroute(index, family, scope, dststr,
793 /* skip host specific routes */
794 if (scope != RT_SCOPE_UNIVERSE &&
795 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
798 if (dst.s_addr != INADDR_ANY)
801 } else if (family == AF_INET6) {
802 struct in6_addr dst = IN6ADDR_ANY_INIT,
803 gateway = IN6ADDR_ANY_INIT;
805 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
807 inet_ntop(family, &dst, dststr, sizeof(dststr));
808 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
810 __connman_ipconfig_delroute(index, family, scope, dststr,
813 /* skip host specific routes */
814 if (scope != RT_SCOPE_UNIVERSE &&
815 !(scope == RT_SCOPE_LINK &&
816 IN6_IS_ADDR_UNSPECIFIED(&dst)))
819 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
824 for (list = rtnl_list; list; list = list->next) {
825 struct connman_rtnl *rtnl = list->data;
827 if (rtnl->delgateway)
828 rtnl->delgateway(index, gatewaystr);
832 static inline void print_ether(struct rtattr *attr, const char *name)
834 int len = (int) RTA_PAYLOAD(attr);
836 if (len == ETH_ALEN) {
837 struct ether_addr eth;
838 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
839 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
841 print(" attr %s (len %d)\n", name, len);
844 static inline void print_inet(struct rtattr *attr, const char *name,
845 unsigned char family)
847 int len = (int) RTA_PAYLOAD(attr);
849 if (family == AF_INET && len == sizeof(struct in_addr)) {
851 addr = *((struct in_addr *) RTA_DATA(attr));
852 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
854 print(" attr %s (len %d)\n", name, len);
857 static inline void print_string(struct rtattr *attr, const char *name)
859 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
860 (char *) RTA_DATA(attr));
863 static inline void print_byte(struct rtattr *attr, const char *name)
865 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
866 *((unsigned char *) RTA_DATA(attr)));
869 static inline void print_integer(struct rtattr *attr, const char *name)
871 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
872 *((int *) RTA_DATA(attr)));
875 static inline void print_attr(struct rtattr *attr, const char *name)
877 int len = (int) RTA_PAYLOAD(attr);
880 print(" attr %s (len %d)\n", name, len);
882 print(" attr %d (len %d)\n", attr->rta_type, len);
885 static void rtnl_link(struct nlmsghdr *hdr)
887 struct ifinfomsg *msg;
891 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
892 bytes = IFLA_PAYLOAD(hdr);
894 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
896 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
897 attr = RTA_NEXT(attr, bytes)) {
898 switch (attr->rta_type) {
900 print_ether(attr, "address");
903 print_ether(attr, "broadcast");
906 print_string(attr, "ifname");
909 print_integer(attr, "mtu");
912 print_attr(attr, "link");
915 print_attr(attr, "qdisc");
918 print_attr(attr, "stats");
921 print_attr(attr, "cost");
924 print_attr(attr, "priority");
927 print_attr(attr, "master");
930 print_attr(attr, "wireless");
933 print_attr(attr, "protinfo");
936 print_integer(attr, "txqlen");
939 print_attr(attr, "map");
942 print_attr(attr, "weight");
945 print_byte(attr, "operstate");
948 print_byte(attr, "linkmode");
951 print_attr(attr, NULL);
957 static void rtnl_newlink(struct nlmsghdr *hdr)
959 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
963 if (hdr->nlmsg_type == IFLA_WIRELESS)
964 connman_warn_once("Obsolete WEXT WiFi driver detected");
966 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
967 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
970 static void rtnl_dellink(struct nlmsghdr *hdr)
972 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
976 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
977 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
980 static void rtnl_addr(struct nlmsghdr *hdr)
982 struct ifaddrmsg *msg;
986 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
987 bytes = IFA_PAYLOAD(hdr);
989 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
991 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
992 attr = RTA_NEXT(attr, bytes)) {
993 switch (attr->rta_type) {
995 print_inet(attr, "address", msg->ifa_family);
998 print_inet(attr, "local", msg->ifa_family);
1001 print_string(attr, "label");
1004 print_inet(attr, "broadcast", msg->ifa_family);
1007 print_attr(attr, "anycast");
1010 print_attr(attr, "cacheinfo");
1013 print_attr(attr, "multicast");
1016 print_attr(attr, NULL);
1022 static void rtnl_newaddr(struct nlmsghdr *hdr)
1024 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1028 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1029 msg, IFA_PAYLOAD(hdr));
1032 static void rtnl_deladdr(struct nlmsghdr *hdr)
1034 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1038 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1039 msg, IFA_PAYLOAD(hdr));
1042 static void rtnl_route(struct nlmsghdr *hdr)
1045 struct rtattr *attr;
1048 msg = (struct rtmsg *) NLMSG_DATA(hdr);
1049 bytes = RTM_PAYLOAD(hdr);
1051 print("rtm_family %d rtm_table %d rtm_protocol %d",
1052 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
1053 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
1054 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
1056 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
1057 attr = RTA_NEXT(attr, bytes)) {
1058 switch (attr->rta_type) {
1060 print_inet(attr, "dst", msg->rtm_family);
1063 print_inet(attr, "src", msg->rtm_family);
1066 print_string(attr, "iif");
1069 print_integer(attr, "oif");
1072 print_inet(attr, "gateway", msg->rtm_family);
1075 print_attr(attr, "priority");
1078 print_inet(attr, "prefsrc", msg->rtm_family);
1081 print_attr(attr, "metrics");
1084 print_integer(attr, "table");
1087 print_attr(attr, NULL);
1093 static bool is_route_rtmsg(struct rtmsg *msg)
1095 if (msg->rtm_flags & RTM_F_CLONED)
1098 if (msg->rtm_table != RT_TABLE_MAIN)
1101 if (msg->rtm_protocol != RTPROT_BOOT &&
1102 msg->rtm_protocol != RTPROT_KERNEL)
1105 if (msg->rtm_type != RTN_UNICAST)
1111 static void rtnl_newroute(struct nlmsghdr *hdr)
1113 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1117 if (is_route_rtmsg(msg))
1118 process_newroute(msg->rtm_family, msg->rtm_scope,
1119 msg, RTM_PAYLOAD(hdr));
1122 static void rtnl_delroute(struct nlmsghdr *hdr)
1124 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1128 if (is_route_rtmsg(msg))
1129 process_delroute(msg->rtm_family, msg->rtm_scope,
1130 msg, RTM_PAYLOAD(hdr));
1133 static void *rtnl_nd_opt_rdnss(struct nd_opt_hdr *opt, guint32 *lifetime,
1136 guint32 *optint = (void *)opt;
1138 if (opt->nd_opt_len < 3)
1141 if (*lifetime > ntohl(optint[1]))
1142 *lifetime = ntohl(optint[1]);
1144 /* nd_opt_len is in units of 8 bytes. The header is 1 unit (8 bytes)
1145 and each address is another 2 units (16 bytes).
1146 So the number of addresses (given rounding) is nd_opt_len/2 */
1147 *nr_servers = opt->nd_opt_len / 2;
1149 /* And they start 8 bytes into the packet, or two guint32s in. */
1153 static const char **rtnl_nd_opt_dnssl(struct nd_opt_hdr *opt, guint32 *lifetime)
1155 const char **domains = NULL;
1156 guint32 *optint = (void *)opt;
1157 unsigned char *optc = (void *)&optint[2];
1158 int data_len = (opt->nd_opt_len * 8) - 8;
1162 if (*lifetime > ntohl(optint[1]))
1163 *lifetime = ntohl(optint[1]);
1165 /* Turn it into normal strings by converting the length bytes into '.',
1166 and count how many search domains there are while we're at it. */
1168 while (i < data_len) {
1169 if (optc[i] > 0x3f) {
1170 DBG("DNSSL contains compressed elements in violation of RFC6106");
1177 /* Check for double zero */
1178 if (i < data_len && optc[i] == 0)
1186 if (i >= data_len) {
1187 DBG("DNSSL data overflows option length");
1194 domains = g_try_new0(const char *, nr_domains + 1);
1198 /* Now point to the normal strings, missing out the leading '.' that
1199 each of them will have now. */
1200 for (i = 0; i < nr_domains; i++) {
1201 domains[i] = (char *)optc + 1;
1202 optc += strlen((char *)optc) + 1;
1208 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1210 struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1211 struct nd_opt_hdr *opt;
1212 guint32 lifetime = -1;
1213 const char **domains = NULL;
1214 struct in6_addr *servers = NULL;
1215 int i, nr_servers = 0;
1216 int msglen = msg->nduseropt_opts_len;
1219 DBG("family %d index %d len %d type %d code %d",
1220 msg->nduseropt_family, msg->nduseropt_ifindex,
1221 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1222 msg->nduseropt_icmp_code);
1224 if (msg->nduseropt_family != AF_INET6 ||
1225 msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1226 msg->nduseropt_icmp_code != 0)
1229 index = msg->nduseropt_ifindex;
1233 for (opt = (void *)&msg[1];
1235 msglen -= opt->nd_opt_len * 8,
1236 opt = ((void *)opt) + opt->nd_opt_len*8) {
1238 DBG("remaining %d nd opt type %d len %d\n",
1239 msglen, opt->nd_opt_type, opt->nd_opt_len);
1241 if (opt->nd_opt_type == 25) { /* ND_OPT_RDNSS */
1244 servers = rtnl_nd_opt_rdnss(opt, &lifetime,
1246 for (i = 0; i < nr_servers; i++) {
1247 if (!inet_ntop(AF_INET6, servers + i, buf,
1251 connman_resolver_append_lifetime(index,
1252 NULL, buf, lifetime);
1255 } else if (opt->nd_opt_type == 31) { /* ND_OPT_DNSSL */
1258 domains = rtnl_nd_opt_dnssl(opt, &lifetime);
1259 for (i = 0; domains && domains[i]; i++)
1260 connman_resolver_append_lifetime(index,
1261 domains[i], NULL, lifetime);
1268 static const char *type2string(uint16_t type)
1297 case RTM_NEWNDUSEROPT:
1298 return "NEWNDUSEROPT";
1304 static GIOChannel *channel = NULL;
1305 static guint channel_watch = 0;
1307 struct rtnl_request {
1308 struct nlmsghdr hdr;
1309 struct rtgenmsg msg;
1311 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1313 static GSList *request_list = NULL;
1314 static guint32 request_seq = 0;
1316 static struct rtnl_request *find_request(guint32 seq)
1320 for (list = request_list; list; list = list->next) {
1321 struct rtnl_request *req = list->data;
1323 if (req->hdr.nlmsg_seq == seq)
1330 static int send_request(struct rtnl_request *req)
1332 struct sockaddr_nl addr;
1335 DBG("%s len %d type %d flags 0x%04x seq %d",
1336 type2string(req->hdr.nlmsg_type),
1337 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1338 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1340 sk = g_io_channel_unix_get_fd(channel);
1342 memset(&addr, 0, sizeof(addr));
1343 addr.nl_family = AF_NETLINK;
1345 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1346 (struct sockaddr *) &addr, sizeof(addr));
1349 static int queue_request(struct rtnl_request *req)
1351 request_list = g_slist_append(request_list, req);
1353 if (g_slist_length(request_list) > 1)
1356 return send_request(req);
1359 static int process_response(guint32 seq)
1361 struct rtnl_request *req;
1365 req = find_request(seq);
1367 request_list = g_slist_remove(request_list, req);
1371 req = g_slist_nth_data(request_list, 0);
1375 return send_request(req);
1378 static void rtnl_message(void *buf, size_t len)
1381 struct nlmsghdr *hdr = buf;
1382 struct nlmsgerr *err;
1384 if (!NLMSG_OK(hdr, len))
1387 DBG("%s len %u type %u flags 0x%04x seq %u pid %u",
1388 type2string(hdr->nlmsg_type),
1389 hdr->nlmsg_len, hdr->nlmsg_type,
1390 hdr->nlmsg_flags, hdr->nlmsg_seq,
1393 switch (hdr->nlmsg_type) {
1398 process_response(hdr->nlmsg_seq);
1401 err = NLMSG_DATA(hdr);
1402 DBG("error %d (%s)", -err->error,
1403 strerror(-err->error));
1423 case RTM_NEWNDUSEROPT:
1424 rtnl_newnduseropt(hdr);
1428 len -= hdr->nlmsg_len;
1429 buf += hdr->nlmsg_len;
1433 static gboolean netlink_event(GIOChannel *chan, GIOCondition cond, gpointer data)
1435 unsigned char buf[4096];
1436 struct sockaddr_nl nladdr;
1437 socklen_t addr_len = sizeof(nladdr);
1441 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1444 memset(buf, 0, sizeof(buf));
1445 memset(&nladdr, 0, sizeof(nladdr));
1447 fd = g_io_channel_unix_get_fd(chan);
1449 status = recvfrom(fd, buf, sizeof(buf), 0,
1450 (struct sockaddr *) &nladdr, &addr_len);
1452 if (errno == EINTR || errno == EAGAIN)
1461 if (nladdr.nl_pid != 0) { /* not sent by kernel, ignore */
1462 DBG("Received msg from %u, ignoring it", nladdr.nl_pid);
1466 rtnl_message(buf, status);
1471 static int send_getlink(void)
1473 struct rtnl_request *req;
1477 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1481 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1482 req->hdr.nlmsg_type = RTM_GETLINK;
1483 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1484 req->hdr.nlmsg_pid = 0;
1485 req->hdr.nlmsg_seq = request_seq++;
1486 req->msg.rtgen_family = AF_INET;
1488 return queue_request(req);
1491 static int send_getaddr(void)
1493 struct rtnl_request *req;
1497 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1501 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1502 req->hdr.nlmsg_type = RTM_GETADDR;
1503 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1504 req->hdr.nlmsg_pid = 0;
1505 req->hdr.nlmsg_seq = request_seq++;
1506 req->msg.rtgen_family = AF_INET;
1508 return queue_request(req);
1511 static int send_getroute(void)
1513 struct rtnl_request *req;
1517 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1521 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1522 req->hdr.nlmsg_type = RTM_GETROUTE;
1523 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1524 req->hdr.nlmsg_pid = 0;
1525 req->hdr.nlmsg_seq = request_seq++;
1526 req->msg.rtgen_family = AF_INET;
1528 return queue_request(req);
1531 static gboolean update_timeout_cb(gpointer user_data)
1533 __connman_rtnl_request_update();
1538 static void update_interval_callback(guint min)
1540 if (update_timeout > 0)
1541 g_source_remove(update_timeout);
1543 if (min < G_MAXUINT) {
1544 update_interval = min;
1545 update_timeout = g_timeout_add_seconds(update_interval,
1546 update_timeout_cb, NULL);
1549 update_interval = G_MAXUINT;
1553 static gint compare_interval(gconstpointer a, gconstpointer b)
1555 guint val_a = GPOINTER_TO_UINT(a);
1556 guint val_b = GPOINTER_TO_UINT(b);
1558 return val_a - val_b;
1561 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1568 update_list = g_slist_insert_sorted(update_list,
1569 GUINT_TO_POINTER(interval), compare_interval);
1571 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1572 if (min < update_interval) {
1573 update_interval_callback(min);
1574 __connman_rtnl_request_update();
1577 return update_interval;
1580 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1582 guint min = G_MAXUINT;
1587 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1590 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1592 if (min > update_interval)
1593 update_interval_callback(min);
1598 int __connman_rtnl_request_update(void)
1600 return send_getlink();
1603 int __connman_rtnl_init(void)
1605 struct sockaddr_nl addr;
1610 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1611 NULL, free_interface);
1613 sk = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
1617 memset(&addr, 0, sizeof(addr));
1618 addr.nl_family = AF_NETLINK;
1619 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1620 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1621 (1<<(RTNLGRP_ND_USEROPT-1));
1623 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1628 channel = g_io_channel_unix_new(sk);
1629 g_io_channel_set_close_on_unref(channel, TRUE);
1631 g_io_channel_set_encoding(channel, NULL, NULL);
1632 g_io_channel_set_buffered(channel, FALSE);
1634 channel_watch = g_io_add_watch(channel,
1635 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1636 netlink_event, NULL);
1641 void __connman_rtnl_start(void)
1650 void __connman_rtnl_cleanup(void)
1656 for (list = watch_list; list; list = list->next) {
1657 struct watch_data *watch = list->data;
1659 DBG("removing watch %d", watch->id);
1665 g_slist_free(watch_list);
1668 g_slist_free(update_list);
1671 for (list = request_list; list; list = list->next) {
1672 struct rtnl_request *req = list->data;
1674 DBG("%s len %d type %d flags 0x%04x seq %d",
1675 type2string(req->hdr.nlmsg_type),
1676 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1677 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1683 g_slist_free(request_list);
1684 request_list = NULL;
1686 if (channel_watch) {
1687 g_source_remove(channel_watch);
1691 g_io_channel_shutdown(channel, TRUE, NULL);
1692 g_io_channel_unref(channel);
1696 g_hash_table_destroy(interface_list);