5 * Copyright (C) 2007-2010 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
29 #include <sys/socket.h>
30 #include <sys/ioctl.h>
31 #include <arpa/inet.h>
32 #include <netinet/ether.h>
33 #include <netinet/icmp6.h>
34 #include <net/if_arp.h>
36 #include <linux/netlink.h>
37 #include <linux/rtnetlink.h>
38 #include <linux/wireless.h>
44 #ifndef ARPHDR_PHONET_PIPE
45 #define ARPHDR_PHONET_PIPE (821)
48 #define print(arg...) do { } while (0)
49 //#define print(arg...) connman_info(arg)
54 connman_rtnl_link_cb_t newlink;
58 static GSList *watch_list = NULL;
59 static unsigned int watch_id = 0;
61 static GSList *update_list = NULL;
62 static guint update_interval = G_MAXUINT;
63 static guint update_timeout = 0;
65 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->name, interface->ident);
82 g_free(interface->ident);
83 g_free(interface->name);
87 static connman_bool_t ether_blacklisted(const char *name)
92 /* virtual interface from VMware */
93 if (g_str_has_prefix(name, "vmnet") == TRUE)
96 /* virtual interface from VirtualBox */
97 if (g_str_has_prefix(name, "vboxnet") == TRUE)
100 /* virtual interface from Virtual Machine Manager */
101 if (g_str_has_prefix(name, "virbr") == TRUE)
107 static connman_bool_t wext_interface(char *ifname)
112 fd = socket(PF_INET, SOCK_DGRAM, 0);
116 memset(&wrq, 0, sizeof(wrq));
117 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
119 err = ioctl(fd, SIOCGIWNAME, &wrq);
129 static void read_uevent(struct interface_data *interface)
131 char *filename, line[128];
132 connman_bool_t found_devtype;
135 if (ether_blacklisted(interface->name) == TRUE) {
136 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
137 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
139 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
140 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
143 filename = g_strdup_printf("/sys/class/net/%s/uevent",
146 f = fopen(filename, "re");
153 found_devtype = FALSE;
154 while (fgets(line, sizeof(line), f)) {
157 pos = strchr(line, '\n');
162 if (strncmp(line, "DEVTYPE=", 8) != 0)
165 found_devtype = TRUE;
167 if (strcmp(line + 8, "wlan") == 0) {
168 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
169 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
170 } else if (strcmp(line + 8, "wwan") == 0) {
171 interface->service_type = CONNMAN_SERVICE_TYPE_CELLULAR;
172 interface->device_type = CONNMAN_DEVICE_TYPE_CELLULAR;
173 } else if (strcmp(line + 8, "bluetooth") == 0) {
174 interface->service_type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
175 interface->device_type = CONNMAN_DEVICE_TYPE_BLUETOOTH;
176 } else if (strcmp(line + 8, "wimax") == 0) {
177 interface->service_type = CONNMAN_SERVICE_TYPE_WIMAX;
178 interface->device_type = CONNMAN_DEVICE_TYPE_WIMAX;
179 } else if (strcmp(line + 8, "gadget") == 0) {
180 interface->service_type = CONNMAN_SERVICE_TYPE_GADGET;
181 interface->device_type = CONNMAN_DEVICE_TYPE_GADGET;
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(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",
204 enum connman_device_type __connman_rtnl_get_device_type(int index)
206 struct interface_data *interface;
208 interface = g_hash_table_lookup(interface_list,
209 GINT_TO_POINTER(index));
210 if (interface == NULL)
211 return CONNMAN_DEVICE_TYPE_UNKNOWN;
213 return interface->device_type;
217 * connman_rtnl_add_newlink_watch:
218 * @index: network device index
219 * @callback: callback function
220 * @user_data: callback data;
222 * Add a new RTNL watch for newlink events
224 * Returns: %0 on failure and a unique id on success
226 unsigned int connman_rtnl_add_newlink_watch(int index,
227 connman_rtnl_link_cb_t callback, void *user_data)
229 struct watch_data *watch;
231 watch = g_try_new0(struct watch_data, 1);
235 watch->id = ++watch_id;
236 watch->index = index;
238 watch->newlink = callback;
239 watch->user_data = user_data;
241 watch_list = g_slist_prepend(watch_list, watch);
243 DBG("id %d", watch->id);
246 unsigned int flags = __connman_ipconfig_get_flags(index);
249 callback(flags, 0, user_data);
256 * connman_rtnl_remove_watch:
257 * @id: watch identifier
259 * Remove the RTNL watch for the identifier
261 void connman_rtnl_remove_watch(unsigned int id)
270 for (list = watch_list; list; list = list->next) {
271 struct watch_data *watch = list->data;
273 if (watch->id == id) {
274 watch_list = g_slist_remove(watch_list, watch);
281 static void trigger_rtnl(int index, void *user_data)
283 struct connman_rtnl *rtnl = user_data;
286 unsigned short type = __connman_ipconfig_get_type(index);
287 unsigned int flags = __connman_ipconfig_get_flags(index);
289 rtnl->newlink(type, index, flags, 0);
292 if (rtnl->newgateway) {
293 const char *gateway = __connman_ipconfig_get_gateway(index);
296 rtnl->newgateway(index, gateway);
300 static GSList *rtnl_list = NULL;
302 static gint compare_priority(gconstpointer a, gconstpointer b)
304 const struct connman_rtnl *rtnl1 = a;
305 const struct connman_rtnl *rtnl2 = b;
307 return rtnl2->priority - rtnl1->priority;
311 * connman_rtnl_register:
314 * Register a new RTNL module
316 * Returns: %0 on success
318 int connman_rtnl_register(struct connman_rtnl *rtnl)
320 DBG("rtnl %p name %s", rtnl, rtnl->name);
322 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
325 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
331 * connman_rtnl_unregister:
334 * Remove a previously registered RTNL module
336 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
338 DBG("rtnl %p name %s", rtnl, rtnl->name);
340 rtnl_list = g_slist_remove(rtnl_list, rtnl);
343 static const char *operstate2str(unsigned char operstate)
346 case IF_OPER_UNKNOWN:
348 case IF_OPER_NOTPRESENT:
349 return "NOT-PRESENT";
352 case IF_OPER_LOWERLAYERDOWN:
353 return "LOWER-LAYER-DOWN";
354 case IF_OPER_TESTING:
356 case IF_OPER_DORMANT:
365 static void extract_link(struct ifinfomsg *msg, int bytes,
366 struct ether_addr *address, const char **ifname,
367 unsigned int *mtu, unsigned char *operstate,
368 struct rtnl_link_stats *stats)
372 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
373 attr = RTA_NEXT(attr, bytes)) {
374 switch (attr->rta_type) {
377 memcpy(address, RTA_DATA(attr), ETH_ALEN);
381 *ifname = RTA_DATA(attr);
385 *mtu = *((unsigned int *) RTA_DATA(attr));
389 memcpy(stats, RTA_DATA(attr),
390 sizeof(struct rtnl_link_stats));
393 if (operstate != NULL)
394 *operstate = *((unsigned char *) RTA_DATA(attr));
402 static void process_newlink(unsigned short type, int index, unsigned flags,
403 unsigned change, struct ifinfomsg *msg, int bytes)
405 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
406 struct ether_addr compare = {{ 0, 0, 0, 0, 0, 0 }};
407 struct rtnl_link_stats stats;
408 unsigned char operstate = 0xff;
409 struct interface_data *interface;
410 const char *ifname = NULL;
411 unsigned int mtu = 0;
412 char ident[13], str[18];
415 memset(&stats, 0, sizeof(stats));
416 extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats);
418 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
419 address.ether_addr_octet[0],
420 address.ether_addr_octet[1],
421 address.ether_addr_octet[2],
422 address.ether_addr_octet[3],
423 address.ether_addr_octet[4],
424 address.ether_addr_octet[5]);
426 snprintf(str, 18, "%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]);
436 case ARPHRD_LOOPBACK:
437 case ARPHDR_PHONET_PIPE:
439 __connman_ipconfig_newlink(index, type, flags,
444 if (memcmp(&address, &compare, ETH_ALEN) != 0)
445 connman_info("%s {newlink} index %d address %s mtu %u",
446 ifname, index, str, mtu);
448 if (operstate != 0xff)
449 connman_info("%s {newlink} index %d operstate %u <%s>",
450 ifname, index, operstate,
451 operstate2str(operstate));
453 interface = g_hash_table_lookup(interface_list, GINT_TO_POINTER(index));
454 if (interface == NULL) {
455 interface = g_new0(struct interface_data, 1);
456 interface->index = index;
457 interface->name = g_strdup(ifname);
458 interface->ident = g_strdup(ident);
460 g_hash_table_insert(interface_list,
461 GINT_TO_POINTER(index), interface);
463 if (type == ARPHRD_ETHER)
464 read_uevent(interface);
466 __connman_technology_add_interface(interface->service_type,
467 interface->index, interface->name, interface->ident);
470 for (list = rtnl_list; list; list = list->next) {
471 struct connman_rtnl *rtnl = list->data;
474 rtnl->newlink(type, index, flags, change);
477 for (list = watch_list; list; list = list->next) {
478 struct watch_data *watch = list->data;
480 if (watch->index != index)
484 watch->newlink(flags, change, watch->user_data);
488 static void process_dellink(unsigned short type, int index, unsigned flags,
489 unsigned change, struct ifinfomsg *msg, int bytes)
491 struct rtnl_link_stats stats;
492 unsigned char operstate = 0xff;
493 const char *ifname = NULL;
496 memset(&stats, 0, sizeof(stats));
497 extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats);
499 if (operstate != 0xff)
500 connman_info("%s {dellink} index %d operstate %u <%s>",
501 ifname, index, operstate,
502 operstate2str(operstate));
504 for (list = rtnl_list; list; list = list->next) {
505 struct connman_rtnl *rtnl = list->data;
508 rtnl->dellink(type, index, flags, change);
513 case ARPHRD_LOOPBACK:
515 __connman_ipconfig_dellink(index, &stats);
519 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
522 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
524 struct in_addr *local,
525 struct in_addr *address,
526 struct in_addr *broadcast)
530 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
531 attr = RTA_NEXT(attr, bytes)) {
532 switch (attr->rta_type) {
535 *address = *((struct in_addr *) RTA_DATA(attr));
539 *local = *((struct in_addr *) RTA_DATA(attr));
542 if (broadcast != NULL)
543 *broadcast = *((struct in_addr *) RTA_DATA(attr));
547 *label = RTA_DATA(attr);
553 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
554 struct in6_addr *addr,
555 struct in6_addr *local)
559 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
560 attr = RTA_NEXT(attr, bytes)) {
561 switch (attr->rta_type) {
564 *addr = *((struct in6_addr *) RTA_DATA(attr));
568 *local = *((struct in6_addr *) RTA_DATA(attr));
574 static void process_newaddr(unsigned char family, unsigned char prefixlen,
575 int index, struct ifaddrmsg *msg, int bytes)
577 const char *label = NULL;
579 char ip_string[INET6_ADDRSTRLEN];
581 if (family == AF_INET) {
582 struct in_addr ipv4_addr = { INADDR_ANY };
584 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
586 } else if (family == AF_INET6) {
587 struct in6_addr ipv6_address, ipv6_local;
589 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
590 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
598 if (inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN) == NULL)
601 __connman_ipconfig_newaddr(index, family, label,
602 prefixlen, ip_string);
605 static void process_deladdr(unsigned char family, unsigned char prefixlen,
606 int index, struct ifaddrmsg *msg, int bytes)
608 const char *label = NULL;
610 char ip_string[INET6_ADDRSTRLEN];
612 if (family == AF_INET) {
613 struct in_addr ipv4_addr = { INADDR_ANY };
615 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
617 } else if (family == AF_INET6) {
618 struct in6_addr ipv6_address, ipv6_local;
620 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
621 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
629 if (inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN) == NULL)
632 __connman_ipconfig_deladdr(index, family, label,
633 prefixlen, ip_string);
636 static void extract_route(struct rtmsg *msg, int bytes, int *index,
638 struct in_addr *gateway)
642 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
643 attr = RTA_NEXT(attr, bytes)) {
644 switch (attr->rta_type) {
647 *dst = *((struct in_addr *) RTA_DATA(attr));
651 *gateway = *((struct in_addr *) RTA_DATA(attr));
655 *index = *((int *) RTA_DATA(attr));
661 static void process_newroute(unsigned char family, unsigned char scope,
662 struct rtmsg *msg, int bytes)
665 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
666 char dststr[16], gatewaystr[16];
669 if (family != AF_INET)
672 extract_route(msg, bytes, &index, &dst, &gateway);
674 inet_ntop(family, &dst, dststr, sizeof(dststr));
675 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
677 __connman_ipconfig_newroute(index, family, scope, dststr, gatewaystr);
679 /* skip host specific routes */
680 if (scope != RT_SCOPE_UNIVERSE &&
681 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
684 if (dst.s_addr != INADDR_ANY)
687 for (list = rtnl_list; list; list = list->next) {
688 struct connman_rtnl *rtnl = list->data;
690 if (rtnl->newgateway)
691 rtnl->newgateway(index, gatewaystr);
695 static void process_delroute(unsigned char family, unsigned char scope,
696 struct rtmsg *msg, int bytes)
699 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
700 char dststr[16], gatewaystr[16];
703 if (family != AF_INET)
706 extract_route(msg, bytes, &index, &dst, &gateway);
708 inet_ntop(family, &dst, dststr, sizeof(dststr));
709 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
711 __connman_ipconfig_delroute(index, family, scope, dststr, gatewaystr);
713 /* skip host specific routes */
714 if (scope != RT_SCOPE_UNIVERSE &&
715 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
718 if (dst.s_addr != INADDR_ANY)
721 for (list = rtnl_list; list; list = list->next) {
722 struct connman_rtnl *rtnl = list->data;
724 if (rtnl->delgateway)
725 rtnl->delgateway(index, gatewaystr);
729 static inline void print_ether(struct rtattr *attr, const char *name)
731 int len = (int) RTA_PAYLOAD(attr);
733 if (len == ETH_ALEN) {
734 struct ether_addr eth;
735 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
736 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
738 print(" attr %s (len %d)\n", name, len);
741 static inline void print_inet(struct rtattr *attr, const char *name,
742 unsigned char family)
744 int len = (int) RTA_PAYLOAD(attr);
746 if (family == AF_INET && len == sizeof(struct in_addr)) {
748 addr = *((struct in_addr *) RTA_DATA(attr));
749 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
751 print(" attr %s (len %d)\n", name, len);
754 static inline void print_string(struct rtattr *attr, const char *name)
756 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
757 (char *) RTA_DATA(attr));
760 static inline void print_byte(struct rtattr *attr, const char *name)
762 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
763 *((unsigned char *) RTA_DATA(attr)));
766 static inline void print_integer(struct rtattr *attr, const char *name)
768 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
769 *((int *) RTA_DATA(attr)));
772 static inline void print_attr(struct rtattr *attr, const char *name)
774 int len = (int) RTA_PAYLOAD(attr);
777 print(" attr %s (len %d)\n", name, len);
779 print(" attr %d (len %d)\n", attr->rta_type, len);
782 static void rtnl_link(struct nlmsghdr *hdr)
784 struct ifinfomsg *msg;
788 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
789 bytes = IFLA_PAYLOAD(hdr);
791 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
793 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
794 attr = RTA_NEXT(attr, bytes)) {
795 switch (attr->rta_type) {
797 print_ether(attr, "address");
800 print_ether(attr, "broadcast");
803 print_string(attr, "ifname");
806 print_integer(attr, "mtu");
809 print_attr(attr, "link");
812 print_attr(attr, "qdisc");
815 print_attr(attr, "stats");
818 print_attr(attr, "cost");
821 print_attr(attr, "priority");
824 print_attr(attr, "master");
827 print_attr(attr, "wireless");
830 print_attr(attr, "protinfo");
833 print_integer(attr, "txqlen");
836 print_attr(attr, "map");
839 print_attr(attr, "weight");
842 print_byte(attr, "operstate");
845 print_byte(attr, "linkmode");
848 print_attr(attr, NULL);
854 static void rtnl_newlink(struct nlmsghdr *hdr)
856 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
860 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
861 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
864 static void rtnl_dellink(struct nlmsghdr *hdr)
866 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
870 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
871 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
874 static void rtnl_addr(struct nlmsghdr *hdr)
876 struct ifaddrmsg *msg;
880 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
881 bytes = IFA_PAYLOAD(hdr);
883 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
885 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
886 attr = RTA_NEXT(attr, bytes)) {
887 switch (attr->rta_type) {
889 print_inet(attr, "address", msg->ifa_family);
892 print_inet(attr, "local", msg->ifa_family);
895 print_string(attr, "label");
898 print_inet(attr, "broadcast", msg->ifa_family);
901 print_attr(attr, "anycast");
904 print_attr(attr, "cacheinfo");
907 print_attr(attr, "multicast");
910 print_attr(attr, NULL);
916 static void rtnl_newaddr(struct nlmsghdr *hdr)
918 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
922 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
923 msg, IFA_PAYLOAD(hdr));
926 static void rtnl_deladdr(struct nlmsghdr *hdr)
928 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
932 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
933 msg, IFA_PAYLOAD(hdr));
936 static void rtnl_route(struct nlmsghdr *hdr)
942 msg = (struct rtmsg *) NLMSG_DATA(hdr);
943 bytes = RTM_PAYLOAD(hdr);
945 print("rtm_family %d rtm_table %d rtm_protocol %d",
946 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
947 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
948 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
950 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
951 attr = RTA_NEXT(attr, bytes)) {
952 switch (attr->rta_type) {
954 print_inet(attr, "dst", msg->rtm_family);
957 print_inet(attr, "src", msg->rtm_family);
960 print_string(attr, "iif");
963 print_integer(attr, "oif");
966 print_inet(attr, "gateway", msg->rtm_family);
969 print_attr(attr, "priority");
972 print_inet(attr, "prefsrc", msg->rtm_family);
975 print_attr(attr, "metrics");
978 print_integer(attr, "table");
981 print_attr(attr, NULL);
987 static connman_bool_t is_route_rtmsg(struct rtmsg *msg)
990 if (msg->rtm_table != RT_TABLE_MAIN)
993 if (msg->rtm_protocol != RTPROT_BOOT &&
994 msg->rtm_protocol != RTPROT_KERNEL)
997 if (msg->rtm_type != RTN_UNICAST)
1003 static void rtnl_newroute(struct nlmsghdr *hdr)
1005 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1009 if (is_route_rtmsg(msg))
1010 process_newroute(msg->rtm_family, msg->rtm_scope,
1011 msg, RTM_PAYLOAD(hdr));
1014 static void rtnl_delroute(struct nlmsghdr *hdr)
1016 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1020 if (is_route_rtmsg(msg))
1021 process_delroute(msg->rtm_family, msg->rtm_scope,
1022 msg, RTM_PAYLOAD(hdr));
1025 static void *rtnl_nd_opt_rdnss(struct nd_opt_hdr *opt, guint32 *lifetime,
1028 guint32 *optint = (void *)opt;
1030 if (opt->nd_opt_len < 3)
1033 if (*lifetime > ntohl(optint[1]))
1034 *lifetime = ntohl(optint[1]);
1036 /* nd_opt_len is in units of 8 bytes. The header is 1 unit (8 bytes)
1037 and each address is another 2 units (16 bytes).
1038 So the number of addresses (given rounding) is nd_opt_len/2 */
1039 *nr_servers = opt->nd_opt_len / 2;
1041 /* And they start 8 bytes into the packet, or two guint32s in. */
1045 static const char **rtnl_nd_opt_dnssl(struct nd_opt_hdr *opt, guint32 *lifetime)
1047 const char **domains = NULL;
1048 guint32 *optint = (void *)opt;
1049 unsigned char *optc = (void *)&optint[2];
1050 int data_len = (opt->nd_opt_len * 8) - 8;
1054 if (*lifetime > ntohl(optint[1]))
1055 *lifetime = ntohl(optint[1]);
1057 /* Turn it into normal strings by converting the length bytes into '.',
1058 and count how many search domains there are while we're at it. */
1060 while (i < data_len) {
1061 if (optc[i] > 0x3f) {
1062 DBG("DNSSL contains compressed elements in violation of RFC6106");
1069 /* Check for double zero */
1070 if (i < data_len && optc[i] == 0)
1078 if (i >= data_len) {
1079 DBG("DNSSL data overflows option length");
1086 domains = g_try_new0(const char *, nr_domains + 1);
1090 /* Now point to the normal strings, missing out the leading '.' that
1091 each of them will have now. */
1092 for (i = 0; i < nr_domains; i++) {
1093 domains[i] = (char *)optc + 1;
1094 optc += strlen((char *)optc) + 1;
1100 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1102 struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1103 struct nd_opt_hdr *opt = (void *)&msg[1];
1104 guint32 lifetime = -1;
1105 const char **domains = NULL;
1106 struct in6_addr *servers = NULL;
1108 int msglen = msg->nduseropt_opts_len;
1111 DBG("family %02x index %x len %04x type %02x code %02x",
1112 msg->nduseropt_family, msg->nduseropt_ifindex,
1113 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1114 msg->nduseropt_icmp_code);
1116 if (msg->nduseropt_family != AF_INET6 ||
1117 msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1118 msg->nduseropt_icmp_code != 0)
1121 interface = connman_inet_ifname(msg->nduseropt_ifindex);
1125 for (opt = (void *)&msg[1];
1126 msglen >= 2 && msglen >= opt->nd_opt_len && opt->nd_opt_len;
1127 msglen -= opt->nd_opt_len,
1128 opt = ((void *)opt) + opt->nd_opt_len*8) {
1130 DBG("nd opt type %d len %d\n",
1131 opt->nd_opt_type, opt->nd_opt_len);
1133 if (opt->nd_opt_type == 25)
1134 servers = rtnl_nd_opt_rdnss(opt, &lifetime,
1136 else if (opt->nd_opt_type == 31)
1137 domains = rtnl_nd_opt_dnssl(opt, &lifetime);
1144 for (i = 0; i < nr_servers; i++) {
1145 if (!inet_ntop(AF_INET6, servers + i, buf, sizeof(buf)))
1148 if (domains == NULL || domains[0] == NULL) {
1149 connman_resolver_append_lifetime(interface,
1150 NULL, buf, lifetime);
1154 for (j = 0; domains[j]; j++)
1155 connman_resolver_append_lifetime(interface,
1164 static const char *type2string(uint16_t type)
1191 case RTM_NEWNDUSEROPT:
1192 return "NEWNDUSEROPT";
1198 static GIOChannel *channel = NULL;
1200 struct rtnl_request {
1201 struct nlmsghdr hdr;
1202 struct rtgenmsg msg;
1204 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1206 static GSList *request_list = NULL;
1207 static guint32 request_seq = 0;
1209 static struct rtnl_request *find_request(guint32 seq)
1213 for (list = request_list; list; list = list->next) {
1214 struct rtnl_request *req = list->data;
1216 if (req->hdr.nlmsg_seq == seq)
1223 static int send_request(struct rtnl_request *req)
1225 struct sockaddr_nl addr;
1228 DBG("%s len %d type %d flags 0x%04x seq %d",
1229 type2string(req->hdr.nlmsg_type),
1230 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1231 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1233 sk = g_io_channel_unix_get_fd(channel);
1235 memset(&addr, 0, sizeof(addr));
1236 addr.nl_family = AF_NETLINK;
1238 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1239 (struct sockaddr *) &addr, sizeof(addr));
1242 static int queue_request(struct rtnl_request *req)
1244 request_list = g_slist_append(request_list, req);
1246 if (g_slist_length(request_list) > 1)
1249 return send_request(req);
1252 static int process_response(guint32 seq)
1254 struct rtnl_request *req;
1258 req = find_request(seq);
1260 request_list = g_slist_remove(request_list, req);
1264 req = g_slist_nth_data(request_list, 0);
1268 return send_request(req);
1271 static void rtnl_message(void *buf, size_t len)
1273 DBG("buf %p len %zd", buf, len);
1276 struct nlmsghdr *hdr = buf;
1277 struct nlmsgerr *err;
1279 if (!NLMSG_OK(hdr, len))
1282 DBG("%s len %d type %d flags 0x%04x seq %d",
1283 type2string(hdr->nlmsg_type),
1284 hdr->nlmsg_len, hdr->nlmsg_type,
1285 hdr->nlmsg_flags, hdr->nlmsg_seq);
1287 switch (hdr->nlmsg_type) {
1292 process_response(hdr->nlmsg_seq);
1295 err = NLMSG_DATA(hdr);
1296 DBG("error %d (%s)", -err->error,
1297 strerror(-err->error));
1317 case RTM_NEWNDUSEROPT:
1318 rtnl_newnduseropt(hdr);
1322 len -= hdr->nlmsg_len;
1323 buf += hdr->nlmsg_len;
1327 static gboolean netlink_event(GIOChannel *chan,
1328 GIOCondition cond, gpointer data)
1330 unsigned char buf[4096];
1334 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1337 memset(buf, 0, sizeof(buf));
1339 status = g_io_channel_read_chars(chan, (gchar *) buf,
1340 sizeof(buf), &len, NULL);
1343 case G_IO_STATUS_NORMAL:
1345 case G_IO_STATUS_AGAIN:
1351 rtnl_message(buf, len);
1356 static int send_getlink(void)
1358 struct rtnl_request *req;
1362 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1366 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1367 req->hdr.nlmsg_type = RTM_GETLINK;
1368 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1369 req->hdr.nlmsg_pid = 0;
1370 req->hdr.nlmsg_seq = request_seq++;
1371 req->msg.rtgen_family = AF_INET;
1373 return queue_request(req);
1376 static int send_getaddr(void)
1378 struct rtnl_request *req;
1382 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1386 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1387 req->hdr.nlmsg_type = RTM_GETADDR;
1388 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1389 req->hdr.nlmsg_pid = 0;
1390 req->hdr.nlmsg_seq = request_seq++;
1391 req->msg.rtgen_family = AF_INET;
1393 return queue_request(req);
1396 static int send_getroute(void)
1398 struct rtnl_request *req;
1402 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1406 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1407 req->hdr.nlmsg_type = RTM_GETROUTE;
1408 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1409 req->hdr.nlmsg_pid = 0;
1410 req->hdr.nlmsg_seq = request_seq++;
1411 req->msg.rtgen_family = AF_INET;
1413 return queue_request(req);
1416 static gboolean update_timeout_cb(gpointer user_data)
1418 __connman_rtnl_request_update();
1423 static void update_interval_callback(guint min)
1425 if (update_timeout > 0)
1426 g_source_remove(update_timeout);
1428 if (min < G_MAXUINT) {
1429 update_interval = min;
1430 update_timeout = g_timeout_add_seconds(update_interval,
1431 update_timeout_cb, NULL);
1434 update_interval = G_MAXUINT;
1438 static gint compare_interval(gconstpointer a, gconstpointer b)
1440 guint val_a = GPOINTER_TO_UINT(a);
1441 guint val_b = GPOINTER_TO_UINT(b);
1443 return val_a - val_b;
1446 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1453 update_list = g_slist_insert_sorted(update_list,
1454 GUINT_TO_POINTER(interval), compare_interval);
1456 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1457 if (min < update_interval) {
1458 update_interval_callback(min);
1459 __connman_rtnl_request_update();
1462 return update_interval;
1465 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1467 guint min = G_MAXUINT;
1472 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1474 if (update_list != NULL)
1475 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1477 if (min > update_interval)
1478 update_interval_callback(min);
1483 int __connman_rtnl_request_update(void)
1485 return send_getlink();
1488 int __connman_rtnl_init(void)
1490 struct sockaddr_nl addr;
1495 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1496 NULL, free_interface);
1498 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
1502 memset(&addr, 0, sizeof(addr));
1503 addr.nl_family = AF_NETLINK;
1504 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1505 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1506 (1<<(RTNLGRP_ND_USEROPT-1));
1508 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1513 channel = g_io_channel_unix_new(sk);
1514 g_io_channel_set_close_on_unref(channel, TRUE);
1516 g_io_channel_set_encoding(channel, NULL, NULL);
1517 g_io_channel_set_buffered(channel, FALSE);
1519 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1520 netlink_event, NULL);
1525 void __connman_rtnl_start(void)
1534 void __connman_rtnl_cleanup(void)
1540 for (list = watch_list; list; list = list->next) {
1541 struct watch_data *watch = list->data;
1543 DBG("removing watch %d", watch->id);
1549 g_slist_free(watch_list);
1552 g_slist_free(update_list);
1555 for (list = request_list; list; list = list->next) {
1556 struct rtnl_request *req = list->data;
1558 DBG("%s len %d type %d flags 0x%04x seq %d",
1559 type2string(req->hdr.nlmsg_type),
1560 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1561 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1567 g_slist_free(request_list);
1568 request_list = NULL;
1570 g_io_channel_shutdown(channel, TRUE, NULL);
1571 g_io_channel_unref(channel);
1575 g_hash_table_destroy(interface_list);