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;
131 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
132 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
135 filename = g_strdup_printf("/sys/class/net/%s/uevent", name);
137 f = fopen(filename, "re");
142 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
143 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
147 found_devtype = false;
148 while (fgets(line, sizeof(line), f)) {
151 pos = strchr(line, '\n');
156 if (strncmp(line, "DEVTYPE=", 8) != 0)
159 found_devtype = true;
161 if (strcmp(line + 8, "wlan") == 0) {
162 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
163 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
164 } else if (strcmp(line + 8, "wwan") == 0) {
165 interface->service_type = CONNMAN_SERVICE_TYPE_CELLULAR;
166 interface->device_type = CONNMAN_DEVICE_TYPE_CELLULAR;
167 } else if (strcmp(line + 8, "bluetooth") == 0) {
168 interface->service_type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
169 interface->device_type = CONNMAN_DEVICE_TYPE_BLUETOOTH;
170 } else if (strcmp(line + 8, "gadget") == 0) {
171 interface->service_type = CONNMAN_SERVICE_TYPE_GADGET;
172 interface->device_type = CONNMAN_DEVICE_TYPE_GADGET;
175 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
176 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
185 /* We haven't got a DEVTYPE, let's check if it's a wireless device */
186 if (wext_interface(name)) {
187 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
188 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
190 connman_error("%s runs an unsupported 802.11 driver", name);
197 enum connman_device_type __connman_rtnl_get_device_type(int index)
199 struct interface_data *interface;
201 interface = g_hash_table_lookup(interface_list,
202 GINT_TO_POINTER(index));
204 return CONNMAN_DEVICE_TYPE_UNKNOWN;
206 return interface->device_type;
210 * connman_rtnl_add_newlink_watch:
211 * @index: network device index
212 * @callback: callback function
213 * @user_data: callback data;
215 * Add a new RTNL watch for newlink events
217 * Returns: %0 on failure and a unique id on success
219 unsigned int connman_rtnl_add_newlink_watch(int index,
220 connman_rtnl_link_cb_t callback, void *user_data)
222 struct watch_data *watch;
224 watch = g_try_new0(struct watch_data, 1);
228 watch->id = ++watch_id;
229 watch->index = index;
231 watch->newlink = callback;
232 watch->user_data = user_data;
234 watch_list = g_slist_prepend(watch_list, watch);
236 DBG("id %d", watch->id);
239 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
242 callback(flags, 0, user_data);
249 * connman_rtnl_remove_watch:
250 * @id: watch identifier
252 * Remove the RTNL watch for the identifier
254 void connman_rtnl_remove_watch(unsigned int id)
263 for (list = watch_list; list; list = list->next) {
264 struct watch_data *watch = list->data;
266 if (watch->id == id) {
267 watch_list = g_slist_remove(watch_list, watch);
274 static void trigger_rtnl(int index, void *user_data)
276 struct connman_rtnl *rtnl = user_data;
279 unsigned short type = __connman_ipconfig_get_type_from_index(index);
280 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
282 rtnl->newlink(type, index, flags, 0);
285 if (rtnl->newgateway) {
286 const char *gateway =
287 __connman_ipconfig_get_gateway_from_index(index,
288 CONNMAN_IPCONFIG_TYPE_ALL);
291 rtnl->newgateway(index, gateway);
295 static GSList *rtnl_list = NULL;
297 static gint compare_priority(gconstpointer a, gconstpointer b)
299 const struct connman_rtnl *rtnl1 = a;
300 const struct connman_rtnl *rtnl2 = b;
302 return rtnl2->priority - rtnl1->priority;
306 * connman_rtnl_register:
309 * Register a new RTNL module
311 * Returns: %0 on success
313 int connman_rtnl_register(struct connman_rtnl *rtnl)
315 DBG("rtnl %p name %s", rtnl, rtnl->name);
317 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
320 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
326 * connman_rtnl_unregister:
329 * Remove a previously registered RTNL module
331 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
333 DBG("rtnl %p name %s", rtnl, rtnl->name);
335 rtnl_list = g_slist_remove(rtnl_list, rtnl);
338 static const char *operstate2str(unsigned char operstate)
341 case IF_OPER_UNKNOWN:
343 case IF_OPER_NOTPRESENT:
344 return "NOT-PRESENT";
347 case IF_OPER_LOWERLAYERDOWN:
348 return "LOWER-LAYER-DOWN";
349 case IF_OPER_TESTING:
351 case IF_OPER_DORMANT:
360 static bool extract_link(struct ifinfomsg *msg, int bytes,
361 struct ether_addr *address, const char **ifname,
362 unsigned int *mtu, unsigned char *operstate,
363 struct rtnl_link_stats *stats)
367 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
368 attr = RTA_NEXT(attr, bytes)) {
369 switch (attr->rta_type) {
372 memcpy(address, RTA_DATA(attr), ETH_ALEN);
376 *ifname = RTA_DATA(attr);
380 *mtu = *((unsigned int *) RTA_DATA(attr));
384 memcpy(stats, RTA_DATA(attr),
385 sizeof(struct rtnl_link_stats));
389 *operstate = *((unsigned char *) RTA_DATA(attr));
401 static void process_newlink(unsigned short type, int index, unsigned flags,
402 unsigned change, struct ifinfomsg *msg, int bytes)
404 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
405 struct rtnl_link_stats stats;
406 unsigned char operstate = 0xff;
407 struct interface_data *interface;
408 const char *ifname = NULL;
409 unsigned int mtu = 0;
410 char ident[13], str[18];
413 memset(&stats, 0, sizeof(stats));
414 if (!extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats))
417 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
418 address.ether_addr_octet[0],
419 address.ether_addr_octet[1],
420 address.ether_addr_octet[2],
421 address.ether_addr_octet[3],
422 address.ether_addr_octet[4],
423 address.ether_addr_octet[5]);
425 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
426 address.ether_addr_octet[0],
427 address.ether_addr_octet[1],
428 address.ether_addr_octet[2],
429 address.ether_addr_octet[3],
430 address.ether_addr_octet[4],
431 address.ether_addr_octet[5]);
433 if (flags & IFF_SLAVE) {
434 connman_info("%s {newlink} ignoring slave, index %d address %s",
441 case ARPHRD_LOOPBACK:
442 case ARPHDR_PHONET_PIPE:
445 __connman_ipconfig_newlink(index, type, flags,
450 connman_info("%s {newlink} index %d address %s mtu %u",
451 ifname, index, str, mtu);
453 if (operstate != 0xff)
454 connman_info("%s {newlink} index %d operstate %u <%s>",
455 ifname, index, operstate,
456 operstate2str(operstate));
458 interface = g_hash_table_lookup(interface_list, GINT_TO_POINTER(index));
460 interface = g_new0(struct interface_data, 1);
461 interface->index = index;
462 interface->ident = g_strdup(ident);
464 g_hash_table_insert(interface_list,
465 GINT_TO_POINTER(index), interface);
467 if (type == ARPHRD_ETHER)
468 read_uevent(interface);
472 for (list = rtnl_list; list; list = list->next) {
473 struct connman_rtnl *rtnl = list->data;
476 rtnl->newlink(type, index, flags, change);
480 * The interface needs to be added after the newlink call.
481 * The newlink will create the technology when needed and
482 * __connman_technology_add_interface() expects the
483 * technology to be there already.
486 __connman_technology_add_interface(interface->service_type,
487 interface->index, interface->ident);
489 for (list = watch_list; list; list = list->next) {
490 struct watch_data *watch = list->data;
492 if (watch->index != index)
496 watch->newlink(flags, change, watch->user_data);
500 static void process_dellink(unsigned short type, int index, unsigned flags,
501 unsigned change, struct ifinfomsg *msg, int bytes)
503 struct rtnl_link_stats stats;
504 unsigned char operstate = 0xff;
505 const char *ifname = NULL;
508 memset(&stats, 0, sizeof(stats));
509 if (!extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats))
512 if (operstate != 0xff)
513 connman_info("%s {dellink} index %d operstate %u <%s>",
514 ifname, index, operstate,
515 operstate2str(operstate));
517 for (list = rtnl_list; list; list = list->next) {
518 struct connman_rtnl *rtnl = list->data;
521 rtnl->dellink(type, index, flags, change);
526 case ARPHRD_LOOPBACK:
527 case ARPHDR_PHONET_PIPE:
530 __connman_ipconfig_dellink(index, &stats);
534 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
537 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
539 struct in_addr *local,
540 struct in_addr *address,
541 struct in_addr *broadcast)
545 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
546 attr = RTA_NEXT(attr, bytes)) {
547 switch (attr->rta_type) {
550 *address = *((struct in_addr *) RTA_DATA(attr));
554 *local = *((struct in_addr *) RTA_DATA(attr));
558 *broadcast = *((struct in_addr *) RTA_DATA(attr));
562 *label = RTA_DATA(attr);
568 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
569 struct in6_addr *addr,
570 struct in6_addr *local)
574 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
575 attr = RTA_NEXT(attr, bytes)) {
576 switch (attr->rta_type) {
579 *addr = *((struct in6_addr *) RTA_DATA(attr));
583 *local = *((struct in6_addr *) RTA_DATA(attr));
589 static void process_newaddr(unsigned char family, unsigned char prefixlen,
590 int index, struct ifaddrmsg *msg, int bytes)
592 struct in_addr ipv4_addr = { INADDR_ANY };
593 struct in6_addr ipv6_address, ipv6_local;
594 const char *label = NULL;
596 char ip_string[INET6_ADDRSTRLEN];
598 if (family == AF_INET) {
600 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
602 } else if (family == AF_INET6) {
603 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
604 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
612 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
615 __connman_ipconfig_newaddr(index, family, label,
616 prefixlen, ip_string);
618 if (family == AF_INET6) {
620 * Re-create RDNSS configured servers if there are any
621 * for this interface. This is done because we might
622 * have now properly configured interface with proper
623 * autoconfigured address.
625 __connman_resolver_redo_servers(index);
629 static void process_deladdr(unsigned char family, unsigned char prefixlen,
630 int index, struct ifaddrmsg *msg, int bytes)
632 struct in_addr ipv4_addr = { INADDR_ANY };
633 struct in6_addr ipv6_address, ipv6_local;
634 const char *label = NULL;
636 char ip_string[INET6_ADDRSTRLEN];
638 if (family == AF_INET) {
639 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
641 } else if (family == AF_INET6) {
642 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
643 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
651 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
654 __connman_ipconfig_deladdr(index, family, label,
655 prefixlen, ip_string);
658 static void extract_ipv4_route(struct rtmsg *msg, int bytes, int *index,
660 struct in_addr *gateway)
664 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
665 attr = RTA_NEXT(attr, bytes)) {
666 switch (attr->rta_type) {
669 *dst = *((struct in_addr *) RTA_DATA(attr));
673 *gateway = *((struct in_addr *) RTA_DATA(attr));
677 *index = *((int *) RTA_DATA(attr));
683 static void extract_ipv6_route(struct rtmsg *msg, int bytes, int *index,
684 struct in6_addr *dst,
685 struct in6_addr *gateway)
689 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
690 attr = RTA_NEXT(attr, bytes)) {
691 switch (attr->rta_type) {
694 *dst = *((struct in6_addr *) RTA_DATA(attr));
699 *((struct in6_addr *) RTA_DATA(attr));
703 *index = *((int *) RTA_DATA(attr));
709 static void process_newroute(unsigned char family, unsigned char scope,
710 struct rtmsg *msg, int bytes)
713 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
716 if (family == AF_INET) {
717 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
719 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
721 inet_ntop(family, &dst, dststr, sizeof(dststr));
722 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
724 __connman_ipconfig_newroute(index, family, scope, dststr,
727 /* skip host specific routes */
728 if (scope != RT_SCOPE_UNIVERSE &&
729 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
732 if (dst.s_addr != INADDR_ANY)
735 } else if (family == AF_INET6) {
736 struct in6_addr dst = IN6ADDR_ANY_INIT,
737 gateway = IN6ADDR_ANY_INIT;
739 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
741 inet_ntop(family, &dst, dststr, sizeof(dststr));
742 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
744 __connman_ipconfig_newroute(index, family, scope, dststr,
747 /* skip host specific routes */
748 if (scope != RT_SCOPE_UNIVERSE &&
749 !(scope == RT_SCOPE_LINK &&
750 IN6_IS_ADDR_UNSPECIFIED(&dst)))
753 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
758 for (list = rtnl_list; list; list = list->next) {
759 struct connman_rtnl *rtnl = list->data;
761 if (rtnl->newgateway)
762 rtnl->newgateway(index, gatewaystr);
766 static void process_delroute(unsigned char family, unsigned char scope,
767 struct rtmsg *msg, int bytes)
770 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
773 if (family == AF_INET) {
774 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
776 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
778 inet_ntop(family, &dst, dststr, sizeof(dststr));
779 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
781 __connman_ipconfig_delroute(index, family, scope, dststr,
784 /* skip host specific routes */
785 if (scope != RT_SCOPE_UNIVERSE &&
786 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
789 if (dst.s_addr != INADDR_ANY)
792 } else if (family == AF_INET6) {
793 struct in6_addr dst = IN6ADDR_ANY_INIT,
794 gateway = IN6ADDR_ANY_INIT;
796 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
798 inet_ntop(family, &dst, dststr, sizeof(dststr));
799 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
801 __connman_ipconfig_delroute(index, family, scope, dststr,
804 /* skip host specific routes */
805 if (scope != RT_SCOPE_UNIVERSE &&
806 !(scope == RT_SCOPE_LINK &&
807 IN6_IS_ADDR_UNSPECIFIED(&dst)))
810 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
815 for (list = rtnl_list; list; list = list->next) {
816 struct connman_rtnl *rtnl = list->data;
818 if (rtnl->delgateway)
819 rtnl->delgateway(index, gatewaystr);
823 static inline void print_ether(struct rtattr *attr, const char *name)
825 int len = (int) RTA_PAYLOAD(attr);
827 if (len == ETH_ALEN) {
828 struct ether_addr eth;
829 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
830 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
832 print(" attr %s (len %d)\n", name, len);
835 static inline void print_inet(struct rtattr *attr, const char *name,
836 unsigned char family)
838 int len = (int) RTA_PAYLOAD(attr);
840 if (family == AF_INET && len == sizeof(struct in_addr)) {
842 addr = *((struct in_addr *) RTA_DATA(attr));
843 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
845 print(" attr %s (len %d)\n", name, len);
848 static inline void print_string(struct rtattr *attr, const char *name)
850 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
851 (char *) RTA_DATA(attr));
854 static inline void print_byte(struct rtattr *attr, const char *name)
856 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
857 *((unsigned char *) RTA_DATA(attr)));
860 static inline void print_integer(struct rtattr *attr, const char *name)
862 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
863 *((int *) RTA_DATA(attr)));
866 static inline void print_attr(struct rtattr *attr, const char *name)
868 int len = (int) RTA_PAYLOAD(attr);
871 print(" attr %s (len %d)\n", name, len);
873 print(" attr %d (len %d)\n", attr->rta_type, len);
876 static void rtnl_link(struct nlmsghdr *hdr)
878 struct ifinfomsg *msg;
882 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
883 bytes = IFLA_PAYLOAD(hdr);
885 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
887 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
888 attr = RTA_NEXT(attr, bytes)) {
889 switch (attr->rta_type) {
891 print_ether(attr, "address");
894 print_ether(attr, "broadcast");
897 print_string(attr, "ifname");
900 print_integer(attr, "mtu");
903 print_attr(attr, "link");
906 print_attr(attr, "qdisc");
909 print_attr(attr, "stats");
912 print_attr(attr, "cost");
915 print_attr(attr, "priority");
918 print_attr(attr, "master");
921 print_attr(attr, "wireless");
924 print_attr(attr, "protinfo");
927 print_integer(attr, "txqlen");
930 print_attr(attr, "map");
933 print_attr(attr, "weight");
936 print_byte(attr, "operstate");
939 print_byte(attr, "linkmode");
942 print_attr(attr, NULL);
948 static void rtnl_newlink(struct nlmsghdr *hdr)
950 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
954 if (hdr->nlmsg_type == IFLA_WIRELESS)
955 connman_warn_once("Obsolete WEXT WiFi driver detected");
957 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
958 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
961 static void rtnl_dellink(struct nlmsghdr *hdr)
963 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
967 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
968 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
971 static void rtnl_addr(struct nlmsghdr *hdr)
973 struct ifaddrmsg *msg;
977 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
978 bytes = IFA_PAYLOAD(hdr);
980 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
982 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
983 attr = RTA_NEXT(attr, bytes)) {
984 switch (attr->rta_type) {
986 print_inet(attr, "address", msg->ifa_family);
989 print_inet(attr, "local", msg->ifa_family);
992 print_string(attr, "label");
995 print_inet(attr, "broadcast", msg->ifa_family);
998 print_attr(attr, "anycast");
1001 print_attr(attr, "cacheinfo");
1004 print_attr(attr, "multicast");
1007 print_attr(attr, NULL);
1013 static void rtnl_newaddr(struct nlmsghdr *hdr)
1015 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1019 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1020 msg, IFA_PAYLOAD(hdr));
1023 static void rtnl_deladdr(struct nlmsghdr *hdr)
1025 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1029 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1030 msg, IFA_PAYLOAD(hdr));
1033 static void rtnl_route(struct nlmsghdr *hdr)
1036 struct rtattr *attr;
1039 msg = (struct rtmsg *) NLMSG_DATA(hdr);
1040 bytes = RTM_PAYLOAD(hdr);
1042 print("rtm_family %d rtm_table %d rtm_protocol %d",
1043 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
1044 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
1045 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
1047 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
1048 attr = RTA_NEXT(attr, bytes)) {
1049 switch (attr->rta_type) {
1051 print_inet(attr, "dst", msg->rtm_family);
1054 print_inet(attr, "src", msg->rtm_family);
1057 print_string(attr, "iif");
1060 print_integer(attr, "oif");
1063 print_inet(attr, "gateway", msg->rtm_family);
1066 print_attr(attr, "priority");
1069 print_inet(attr, "prefsrc", msg->rtm_family);
1072 print_attr(attr, "metrics");
1075 print_integer(attr, "table");
1078 print_attr(attr, NULL);
1084 static bool is_route_rtmsg(struct rtmsg *msg)
1087 if (msg->rtm_table != RT_TABLE_MAIN)
1090 if (msg->rtm_protocol != RTPROT_BOOT &&
1091 msg->rtm_protocol != RTPROT_KERNEL)
1094 if (msg->rtm_type != RTN_UNICAST)
1100 static void rtnl_newroute(struct nlmsghdr *hdr)
1102 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1106 if (is_route_rtmsg(msg))
1107 process_newroute(msg->rtm_family, msg->rtm_scope,
1108 msg, RTM_PAYLOAD(hdr));
1111 static void rtnl_delroute(struct nlmsghdr *hdr)
1113 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1117 if (is_route_rtmsg(msg))
1118 process_delroute(msg->rtm_family, msg->rtm_scope,
1119 msg, RTM_PAYLOAD(hdr));
1122 static void *rtnl_nd_opt_rdnss(struct nd_opt_hdr *opt, guint32 *lifetime,
1125 guint32 *optint = (void *)opt;
1127 if (opt->nd_opt_len < 3)
1130 if (*lifetime > ntohl(optint[1]))
1131 *lifetime = ntohl(optint[1]);
1133 /* nd_opt_len is in units of 8 bytes. The header is 1 unit (8 bytes)
1134 and each address is another 2 units (16 bytes).
1135 So the number of addresses (given rounding) is nd_opt_len/2 */
1136 *nr_servers = opt->nd_opt_len / 2;
1138 /* And they start 8 bytes into the packet, or two guint32s in. */
1142 static const char **rtnl_nd_opt_dnssl(struct nd_opt_hdr *opt, guint32 *lifetime)
1144 const char **domains = NULL;
1145 guint32 *optint = (void *)opt;
1146 unsigned char *optc = (void *)&optint[2];
1147 int data_len = (opt->nd_opt_len * 8) - 8;
1151 if (*lifetime > ntohl(optint[1]))
1152 *lifetime = ntohl(optint[1]);
1154 /* Turn it into normal strings by converting the length bytes into '.',
1155 and count how many search domains there are while we're at it. */
1157 while (i < data_len) {
1158 if (optc[i] > 0x3f) {
1159 DBG("DNSSL contains compressed elements in violation of RFC6106");
1166 /* Check for double zero */
1167 if (i < data_len && optc[i] == 0)
1175 if (i >= data_len) {
1176 DBG("DNSSL data overflows option length");
1183 domains = g_try_new0(const char *, nr_domains + 1);
1187 /* Now point to the normal strings, missing out the leading '.' that
1188 each of them will have now. */
1189 for (i = 0; i < nr_domains; i++) {
1190 domains[i] = (char *)optc + 1;
1191 optc += strlen((char *)optc) + 1;
1197 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1199 struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1200 struct nd_opt_hdr *opt;
1201 guint32 lifetime = -1;
1202 const char **domains = NULL;
1203 struct in6_addr *servers = NULL;
1204 int i, nr_servers = 0;
1205 int msglen = msg->nduseropt_opts_len;
1208 DBG("family %d index %d len %d type %d code %d",
1209 msg->nduseropt_family, msg->nduseropt_ifindex,
1210 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1211 msg->nduseropt_icmp_code);
1213 if (msg->nduseropt_family != AF_INET6 ||
1214 msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1215 msg->nduseropt_icmp_code != 0)
1218 index = msg->nduseropt_ifindex;
1222 for (opt = (void *)&msg[1];
1224 msglen -= opt->nd_opt_len * 8,
1225 opt = ((void *)opt) + opt->nd_opt_len*8) {
1227 DBG("remaining %d nd opt type %d len %d\n",
1228 msglen, opt->nd_opt_type, opt->nd_opt_len);
1230 if (opt->nd_opt_type == 25) { /* ND_OPT_RDNSS */
1233 servers = rtnl_nd_opt_rdnss(opt, &lifetime,
1235 for (i = 0; i < nr_servers; i++) {
1236 if (!inet_ntop(AF_INET6, servers + i, buf,
1240 connman_resolver_append_lifetime(index,
1241 NULL, buf, lifetime);
1244 } else if (opt->nd_opt_type == 31) { /* ND_OPT_DNSSL */
1247 domains = rtnl_nd_opt_dnssl(opt, &lifetime);
1248 for (i = 0; domains && domains[i]; i++)
1249 connman_resolver_append_lifetime(index,
1250 domains[i], NULL, lifetime);
1257 static const char *type2string(uint16_t type)
1286 case RTM_NEWNDUSEROPT:
1287 return "NEWNDUSEROPT";
1293 static GIOChannel *channel = NULL;
1295 struct rtnl_request {
1296 struct nlmsghdr hdr;
1297 struct rtgenmsg msg;
1299 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1301 static GSList *request_list = NULL;
1302 static guint32 request_seq = 0;
1304 static struct rtnl_request *find_request(guint32 seq)
1308 for (list = request_list; list; list = list->next) {
1309 struct rtnl_request *req = list->data;
1311 if (req->hdr.nlmsg_seq == seq)
1318 static int send_request(struct rtnl_request *req)
1320 struct sockaddr_nl addr;
1323 DBG("%s len %d type %d flags 0x%04x seq %d",
1324 type2string(req->hdr.nlmsg_type),
1325 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1326 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1328 sk = g_io_channel_unix_get_fd(channel);
1330 memset(&addr, 0, sizeof(addr));
1331 addr.nl_family = AF_NETLINK;
1333 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1334 (struct sockaddr *) &addr, sizeof(addr));
1337 static int queue_request(struct rtnl_request *req)
1339 request_list = g_slist_append(request_list, req);
1341 if (g_slist_length(request_list) > 1)
1344 return send_request(req);
1347 static int process_response(guint32 seq)
1349 struct rtnl_request *req;
1353 req = find_request(seq);
1355 request_list = g_slist_remove(request_list, req);
1359 req = g_slist_nth_data(request_list, 0);
1363 return send_request(req);
1366 static void rtnl_message(void *buf, size_t len)
1368 DBG("buf %p len %zd", buf, len);
1371 struct nlmsghdr *hdr = buf;
1372 struct nlmsgerr *err;
1374 if (!NLMSG_OK(hdr, len))
1377 DBG("%s len %d type %d flags 0x%04x seq %d pid %d",
1378 type2string(hdr->nlmsg_type),
1379 hdr->nlmsg_len, hdr->nlmsg_type,
1380 hdr->nlmsg_flags, hdr->nlmsg_seq,
1383 switch (hdr->nlmsg_type) {
1388 process_response(hdr->nlmsg_seq);
1391 err = NLMSG_DATA(hdr);
1392 DBG("error %d (%s)", -err->error,
1393 strerror(-err->error));
1413 case RTM_NEWNDUSEROPT:
1414 rtnl_newnduseropt(hdr);
1418 len -= hdr->nlmsg_len;
1419 buf += hdr->nlmsg_len;
1423 static gboolean netlink_event(GIOChannel *chan, GIOCondition cond, gpointer data)
1425 unsigned char buf[4096];
1426 struct sockaddr_nl nladdr;
1427 socklen_t addr_len = sizeof(nladdr);
1431 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1434 memset(buf, 0, sizeof(buf));
1435 memset(&nladdr, 0, sizeof(nladdr));
1437 fd = g_io_channel_unix_get_fd(chan);
1439 status = recvfrom(fd, buf, sizeof(buf), 0,
1440 (struct sockaddr *) &nladdr, &addr_len);
1442 if (errno == EINTR || errno == EAGAIN)
1451 if (nladdr.nl_pid != 0) { /* not sent by kernel, ignore */
1452 DBG("Received msg from %u, ignoring it", nladdr.nl_pid);
1456 rtnl_message(buf, status);
1461 static int send_getlink(void)
1463 struct rtnl_request *req;
1467 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1471 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1472 req->hdr.nlmsg_type = RTM_GETLINK;
1473 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1474 req->hdr.nlmsg_pid = 0;
1475 req->hdr.nlmsg_seq = request_seq++;
1476 req->msg.rtgen_family = AF_INET;
1478 return queue_request(req);
1481 static int send_getaddr(void)
1483 struct rtnl_request *req;
1487 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1491 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1492 req->hdr.nlmsg_type = RTM_GETADDR;
1493 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1494 req->hdr.nlmsg_pid = 0;
1495 req->hdr.nlmsg_seq = request_seq++;
1496 req->msg.rtgen_family = AF_INET;
1498 return queue_request(req);
1501 static int send_getroute(void)
1503 struct rtnl_request *req;
1507 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1511 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1512 req->hdr.nlmsg_type = RTM_GETROUTE;
1513 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1514 req->hdr.nlmsg_pid = 0;
1515 req->hdr.nlmsg_seq = request_seq++;
1516 req->msg.rtgen_family = AF_INET;
1518 return queue_request(req);
1521 static gboolean update_timeout_cb(gpointer user_data)
1523 __connman_rtnl_request_update();
1528 static void update_interval_callback(guint min)
1530 if (update_timeout > 0)
1531 g_source_remove(update_timeout);
1533 if (min < G_MAXUINT) {
1534 update_interval = min;
1535 update_timeout = g_timeout_add_seconds(update_interval,
1536 update_timeout_cb, NULL);
1539 update_interval = G_MAXUINT;
1543 static gint compare_interval(gconstpointer a, gconstpointer b)
1545 guint val_a = GPOINTER_TO_UINT(a);
1546 guint val_b = GPOINTER_TO_UINT(b);
1548 return val_a - val_b;
1551 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1558 update_list = g_slist_insert_sorted(update_list,
1559 GUINT_TO_POINTER(interval), compare_interval);
1561 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1562 if (min < update_interval) {
1563 update_interval_callback(min);
1564 __connman_rtnl_request_update();
1567 return update_interval;
1570 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1572 guint min = G_MAXUINT;
1577 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1580 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1582 if (min > update_interval)
1583 update_interval_callback(min);
1588 int __connman_rtnl_request_update(void)
1590 return send_getlink();
1593 int __connman_rtnl_init(void)
1595 struct sockaddr_nl addr;
1600 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1601 NULL, free_interface);
1603 sk = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
1607 memset(&addr, 0, sizeof(addr));
1608 addr.nl_family = AF_NETLINK;
1609 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1610 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1611 (1<<(RTNLGRP_ND_USEROPT-1));
1613 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1618 channel = g_io_channel_unix_new(sk);
1619 g_io_channel_set_close_on_unref(channel, TRUE);
1621 g_io_channel_set_encoding(channel, NULL, NULL);
1622 g_io_channel_set_buffered(channel, FALSE);
1624 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1625 netlink_event, NULL);
1630 void __connman_rtnl_start(void)
1639 void __connman_rtnl_cleanup(void)
1645 for (list = watch_list; list; list = list->next) {
1646 struct watch_data *watch = list->data;
1648 DBG("removing watch %d", watch->id);
1654 g_slist_free(watch_list);
1657 g_slist_free(update_list);
1660 for (list = request_list; list; list = list->next) {
1661 struct rtnl_request *req = list->data;
1663 DBG("%s len %d type %d flags 0x%04x seq %d",
1664 type2string(req->hdr.nlmsg_type),
1665 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1666 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1672 g_slist_free(request_list);
1673 request_list = NULL;
1675 g_io_channel_shutdown(channel, TRUE, NULL);
1676 g_io_channel_unref(channel);
1680 g_hash_table_destroy(interface_list);