5 * Copyright (C) 2007-2012 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 {
70 enum connman_service_type service_type;
71 enum connman_device_type device_type;
74 static GHashTable *interface_list = NULL;
76 static void free_interface(gpointer data)
78 struct interface_data *interface = data;
80 __connman_technology_remove_interface(interface->service_type,
81 interface->index, interface->name, interface->ident);
83 g_free(interface->ident);
84 g_free(interface->name);
88 static connman_bool_t ether_blacklisted(const char *name)
93 if (__connman_device_isfiltered(name) == TRUE)
99 static connman_bool_t wext_interface(char *ifname)
104 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
108 memset(&wrq, 0, sizeof(wrq));
109 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
111 err = ioctl(fd, SIOCGIWNAME, &wrq);
121 static void read_uevent(struct interface_data *interface)
123 char *filename, line[128];
124 connman_bool_t found_devtype;
127 if (ether_blacklisted(interface->name) == TRUE) {
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",
138 f = fopen(filename, "re");
145 found_devtype = FALSE;
146 while (fgets(line, sizeof(line), f)) {
149 pos = strchr(line, '\n');
154 if (strncmp(line, "DEVTYPE=", 8) != 0)
157 found_devtype = TRUE;
159 if (strcmp(line + 8, "wlan") == 0) {
160 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
161 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
162 } else if (strcmp(line + 8, "wwan") == 0) {
163 interface->service_type = CONNMAN_SERVICE_TYPE_CELLULAR;
164 interface->device_type = CONNMAN_DEVICE_TYPE_CELLULAR;
165 } else if (strcmp(line + 8, "bluetooth") == 0) {
166 interface->service_type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
167 interface->device_type = CONNMAN_DEVICE_TYPE_BLUETOOTH;
168 } else if (strcmp(line + 8, "wimax") == 0) {
169 interface->service_type = CONNMAN_SERVICE_TYPE_WIMAX;
170 interface->device_type = CONNMAN_DEVICE_TYPE_WIMAX;
171 } else if (strcmp(line + 8, "gadget") == 0) {
172 interface->service_type = CONNMAN_SERVICE_TYPE_GADGET;
173 interface->device_type = CONNMAN_DEVICE_TYPE_GADGET;
176 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
177 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
186 /* We haven't got a DEVTYPE, let's check if it's a wireless device */
187 if (wext_interface(interface->name)) {
188 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
189 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
191 connman_error("%s runs an unsupported 802.11 driver",
196 enum connman_device_type __connman_rtnl_get_device_type(int index)
198 struct interface_data *interface;
200 interface = g_hash_table_lookup(interface_list,
201 GINT_TO_POINTER(index));
202 if (interface == NULL)
203 return CONNMAN_DEVICE_TYPE_UNKNOWN;
205 return interface->device_type;
209 * connman_rtnl_add_newlink_watch:
210 * @index: network device index
211 * @callback: callback function
212 * @user_data: callback data;
214 * Add a new RTNL watch for newlink events
216 * Returns: %0 on failure and a unique id on success
218 unsigned int connman_rtnl_add_newlink_watch(int index,
219 connman_rtnl_link_cb_t callback, void *user_data)
221 struct watch_data *watch;
223 watch = g_try_new0(struct watch_data, 1);
227 watch->id = ++watch_id;
228 watch->index = index;
230 watch->newlink = callback;
231 watch->user_data = user_data;
233 watch_list = g_slist_prepend(watch_list, watch);
235 DBG("id %d", watch->id);
238 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
241 callback(flags, 0, user_data);
248 * connman_rtnl_remove_watch:
249 * @id: watch identifier
251 * Remove the RTNL watch for the identifier
253 void connman_rtnl_remove_watch(unsigned int id)
262 for (list = watch_list; list; list = list->next) {
263 struct watch_data *watch = list->data;
265 if (watch->id == id) {
266 watch_list = g_slist_remove(watch_list, watch);
273 static void trigger_rtnl(int index, void *user_data)
275 struct connman_rtnl *rtnl = user_data;
278 unsigned short type = __connman_ipconfig_get_type_from_index(index);
279 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
281 rtnl->newlink(type, index, flags, 0);
284 if (rtnl->newgateway) {
285 const char *gateway =
286 __connman_ipconfig_get_gateway_from_index(index,
287 CONNMAN_IPCONFIG_TYPE_ALL);
290 rtnl->newgateway(index, gateway);
294 static GSList *rtnl_list = NULL;
296 static gint compare_priority(gconstpointer a, gconstpointer b)
298 const struct connman_rtnl *rtnl1 = a;
299 const struct connman_rtnl *rtnl2 = b;
301 return rtnl2->priority - rtnl1->priority;
305 * connman_rtnl_register:
308 * Register a new RTNL module
310 * Returns: %0 on success
312 int connman_rtnl_register(struct connman_rtnl *rtnl)
314 DBG("rtnl %p name %s", rtnl, rtnl->name);
316 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
319 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
325 * connman_rtnl_unregister:
328 * Remove a previously registered RTNL module
330 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
332 DBG("rtnl %p name %s", rtnl, rtnl->name);
334 rtnl_list = g_slist_remove(rtnl_list, rtnl);
337 static const char *operstate2str(unsigned char operstate)
340 case IF_OPER_UNKNOWN:
342 case IF_OPER_NOTPRESENT:
343 return "NOT-PRESENT";
346 case IF_OPER_LOWERLAYERDOWN:
347 return "LOWER-LAYER-DOWN";
348 case IF_OPER_TESTING:
350 case IF_OPER_DORMANT:
359 static void extract_link(struct ifinfomsg *msg, int bytes,
360 struct ether_addr *address, const char **ifname,
361 unsigned int *mtu, unsigned char *operstate,
362 struct rtnl_link_stats *stats)
366 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
367 attr = RTA_NEXT(attr, bytes)) {
368 switch (attr->rta_type) {
371 memcpy(address, RTA_DATA(attr), ETH_ALEN);
375 *ifname = RTA_DATA(attr);
379 *mtu = *((unsigned int *) RTA_DATA(attr));
383 memcpy(stats, RTA_DATA(attr),
384 sizeof(struct rtnl_link_stats));
387 if (operstate != NULL)
388 *operstate = *((unsigned char *) RTA_DATA(attr));
396 static void process_newlink(unsigned short type, int index, unsigned flags,
397 unsigned change, struct ifinfomsg *msg, int bytes)
399 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
400 struct ether_addr compare = {{ 0, 0, 0, 0, 0, 0 }};
401 struct rtnl_link_stats stats;
402 unsigned char operstate = 0xff;
403 struct interface_data *interface;
404 const char *ifname = NULL;
405 unsigned int mtu = 0;
406 char ident[13], str[18];
409 memset(&stats, 0, sizeof(stats));
410 extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats);
412 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
413 address.ether_addr_octet[0],
414 address.ether_addr_octet[1],
415 address.ether_addr_octet[2],
416 address.ether_addr_octet[3],
417 address.ether_addr_octet[4],
418 address.ether_addr_octet[5]);
420 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
421 address.ether_addr_octet[0],
422 address.ether_addr_octet[1],
423 address.ether_addr_octet[2],
424 address.ether_addr_octet[3],
425 address.ether_addr_octet[4],
426 address.ether_addr_octet[5]);
430 case ARPHRD_LOOPBACK:
431 case ARPHDR_PHONET_PIPE:
434 __connman_ipconfig_newlink(index, type, flags,
439 if (memcmp(&address, &compare, ETH_ALEN) != 0)
440 connman_info("%s {newlink} index %d address %s mtu %u",
441 ifname, index, str, mtu);
443 if (operstate != 0xff)
444 connman_info("%s {newlink} index %d operstate %u <%s>",
445 ifname, index, operstate,
446 operstate2str(operstate));
448 interface = g_hash_table_lookup(interface_list, GINT_TO_POINTER(index));
449 if (interface == NULL) {
450 interface = g_new0(struct interface_data, 1);
451 interface->index = index;
452 interface->name = g_strdup(ifname);
453 interface->ident = g_strdup(ident);
455 g_hash_table_insert(interface_list,
456 GINT_TO_POINTER(index), interface);
458 if (type == ARPHRD_ETHER)
459 read_uevent(interface);
461 __connman_technology_add_interface(interface->service_type,
462 interface->index, interface->name, interface->ident);
465 for (list = rtnl_list; list; list = list->next) {
466 struct connman_rtnl *rtnl = list->data;
469 rtnl->newlink(type, index, flags, change);
472 for (list = watch_list; list; list = list->next) {
473 struct watch_data *watch = list->data;
475 if (watch->index != index)
479 watch->newlink(flags, change, watch->user_data);
483 static void process_dellink(unsigned short type, int index, unsigned flags,
484 unsigned change, struct ifinfomsg *msg, int bytes)
486 struct rtnl_link_stats stats;
487 unsigned char operstate = 0xff;
488 const char *ifname = NULL;
491 memset(&stats, 0, sizeof(stats));
492 extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats);
494 if (operstate != 0xff)
495 connman_info("%s {dellink} index %d operstate %u <%s>",
496 ifname, index, operstate,
497 operstate2str(operstate));
499 for (list = rtnl_list; list; list = list->next) {
500 struct connman_rtnl *rtnl = list->data;
503 rtnl->dellink(type, index, flags, change);
508 case ARPHRD_LOOPBACK:
510 __connman_ipconfig_dellink(index, &stats);
514 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
517 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
519 struct in_addr *local,
520 struct in_addr *address,
521 struct in_addr *broadcast)
525 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
526 attr = RTA_NEXT(attr, bytes)) {
527 switch (attr->rta_type) {
530 *address = *((struct in_addr *) RTA_DATA(attr));
534 *local = *((struct in_addr *) RTA_DATA(attr));
537 if (broadcast != NULL)
538 *broadcast = *((struct in_addr *) RTA_DATA(attr));
542 *label = RTA_DATA(attr);
548 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
549 struct in6_addr *addr,
550 struct in6_addr *local)
554 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
555 attr = RTA_NEXT(attr, bytes)) {
556 switch (attr->rta_type) {
559 *addr = *((struct in6_addr *) RTA_DATA(attr));
563 *local = *((struct in6_addr *) RTA_DATA(attr));
569 static void process_newaddr(unsigned char family, unsigned char prefixlen,
570 int index, struct ifaddrmsg *msg, int bytes)
572 const char *label = NULL;
574 char ip_string[INET6_ADDRSTRLEN];
576 if (family == AF_INET) {
577 struct in_addr ipv4_addr = { INADDR_ANY };
579 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
581 } else if (family == AF_INET6) {
582 struct in6_addr ipv6_address, ipv6_local;
584 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
585 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
593 if (inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN) == NULL)
596 __connman_ipconfig_newaddr(index, family, label,
597 prefixlen, ip_string);
599 if (family == AF_INET6) {
601 * Re-create RDNSS configured servers if there are any
602 * for this interface. This is done because we might
603 * have now properly configured interface with proper
604 * autoconfigured address.
606 char *interface = connman_inet_ifname(index);
608 __connman_resolver_redo_servers(interface);
614 static void process_deladdr(unsigned char family, unsigned char prefixlen,
615 int index, struct ifaddrmsg *msg, int bytes)
617 const char *label = NULL;
619 char ip_string[INET6_ADDRSTRLEN];
621 if (family == AF_INET) {
622 struct in_addr ipv4_addr = { INADDR_ANY };
624 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
626 } else if (family == AF_INET6) {
627 struct in6_addr ipv6_address, ipv6_local;
629 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
630 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
638 if (inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN) == NULL)
641 __connman_ipconfig_deladdr(index, family, label,
642 prefixlen, ip_string);
645 static void extract_ipv4_route(struct rtmsg *msg, int bytes, int *index,
647 struct in_addr *gateway)
651 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
652 attr = RTA_NEXT(attr, bytes)) {
653 switch (attr->rta_type) {
656 *dst = *((struct in_addr *) RTA_DATA(attr));
660 *gateway = *((struct in_addr *) RTA_DATA(attr));
664 *index = *((int *) RTA_DATA(attr));
670 static void extract_ipv6_route(struct rtmsg *msg, int bytes, int *index,
671 struct in6_addr *dst,
672 struct in6_addr *gateway)
676 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
677 attr = RTA_NEXT(attr, bytes)) {
678 switch (attr->rta_type) {
681 *dst = *((struct in6_addr *) RTA_DATA(attr));
686 *((struct in6_addr *) RTA_DATA(attr));
690 *index = *((int *) RTA_DATA(attr));
696 static void process_newroute(unsigned char family, unsigned char scope,
697 struct rtmsg *msg, int bytes)
700 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
703 if (family == AF_INET) {
704 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
706 extract_ipv4_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_newroute(index, family, scope, dststr,
714 /* skip host specific routes */
715 if (scope != RT_SCOPE_UNIVERSE &&
716 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
719 if (dst.s_addr != INADDR_ANY)
722 } else if (family == AF_INET6) {
723 struct in6_addr dst = IN6ADDR_ANY_INIT,
724 gateway = IN6ADDR_ANY_INIT;
726 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
728 inet_ntop(family, &dst, dststr, sizeof(dststr));
729 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
731 __connman_ipconfig_newroute(index, family, scope, dststr,
734 /* skip host specific routes */
735 if (scope != RT_SCOPE_UNIVERSE &&
736 !(scope == RT_SCOPE_LINK &&
737 IN6_IS_ADDR_UNSPECIFIED(&dst)))
740 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
745 for (list = rtnl_list; list; list = list->next) {
746 struct connman_rtnl *rtnl = list->data;
748 if (rtnl->newgateway)
749 rtnl->newgateway(index, gatewaystr);
753 static void process_delroute(unsigned char family, unsigned char scope,
754 struct rtmsg *msg, int bytes)
757 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
760 if (family == AF_INET) {
761 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
763 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
765 inet_ntop(family, &dst, dststr, sizeof(dststr));
766 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
768 __connman_ipconfig_delroute(index, family, scope, dststr,
771 /* skip host specific routes */
772 if (scope != RT_SCOPE_UNIVERSE &&
773 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
776 if (dst.s_addr != INADDR_ANY)
779 } else if (family == AF_INET6) {
780 struct in6_addr dst = IN6ADDR_ANY_INIT,
781 gateway = IN6ADDR_ANY_INIT;
783 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
785 inet_ntop(family, &dst, dststr, sizeof(dststr));
786 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
788 __connman_ipconfig_delroute(index, family, scope, dststr,
791 /* skip host specific routes */
792 if (scope != RT_SCOPE_UNIVERSE &&
793 !(scope == RT_SCOPE_LINK &&
794 IN6_IS_ADDR_UNSPECIFIED(&dst)))
797 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
802 for (list = rtnl_list; list; list = list->next) {
803 struct connman_rtnl *rtnl = list->data;
805 if (rtnl->delgateway)
806 rtnl->delgateway(index, gatewaystr);
810 static inline void print_ether(struct rtattr *attr, const char *name)
812 int len = (int) RTA_PAYLOAD(attr);
814 if (len == ETH_ALEN) {
815 struct ether_addr eth;
816 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
817 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
819 print(" attr %s (len %d)\n", name, len);
822 static inline void print_inet(struct rtattr *attr, const char *name,
823 unsigned char family)
825 int len = (int) RTA_PAYLOAD(attr);
827 if (family == AF_INET && len == sizeof(struct in_addr)) {
829 addr = *((struct in_addr *) RTA_DATA(attr));
830 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
832 print(" attr %s (len %d)\n", name, len);
835 static inline void print_string(struct rtattr *attr, const char *name)
837 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
838 (char *) RTA_DATA(attr));
841 static inline void print_byte(struct rtattr *attr, const char *name)
843 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
844 *((unsigned char *) RTA_DATA(attr)));
847 static inline void print_integer(struct rtattr *attr, const char *name)
849 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
850 *((int *) RTA_DATA(attr)));
853 static inline void print_attr(struct rtattr *attr, const char *name)
855 int len = (int) RTA_PAYLOAD(attr);
858 print(" attr %s (len %d)\n", name, len);
860 print(" attr %d (len %d)\n", attr->rta_type, len);
863 static void rtnl_link(struct nlmsghdr *hdr)
865 struct ifinfomsg *msg;
869 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
870 bytes = IFLA_PAYLOAD(hdr);
872 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
874 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
875 attr = RTA_NEXT(attr, bytes)) {
876 switch (attr->rta_type) {
878 print_ether(attr, "address");
881 print_ether(attr, "broadcast");
884 print_string(attr, "ifname");
887 print_integer(attr, "mtu");
890 print_attr(attr, "link");
893 print_attr(attr, "qdisc");
896 print_attr(attr, "stats");
899 print_attr(attr, "cost");
902 print_attr(attr, "priority");
905 print_attr(attr, "master");
908 print_attr(attr, "wireless");
911 print_attr(attr, "protinfo");
914 print_integer(attr, "txqlen");
917 print_attr(attr, "map");
920 print_attr(attr, "weight");
923 print_byte(attr, "operstate");
926 print_byte(attr, "linkmode");
929 print_attr(attr, NULL);
935 static void rtnl_newlink(struct nlmsghdr *hdr)
937 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
941 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
942 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
945 static void rtnl_dellink(struct nlmsghdr *hdr)
947 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
951 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
952 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
955 static void rtnl_addr(struct nlmsghdr *hdr)
957 struct ifaddrmsg *msg;
961 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
962 bytes = IFA_PAYLOAD(hdr);
964 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
966 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
967 attr = RTA_NEXT(attr, bytes)) {
968 switch (attr->rta_type) {
970 print_inet(attr, "address", msg->ifa_family);
973 print_inet(attr, "local", msg->ifa_family);
976 print_string(attr, "label");
979 print_inet(attr, "broadcast", msg->ifa_family);
982 print_attr(attr, "anycast");
985 print_attr(attr, "cacheinfo");
988 print_attr(attr, "multicast");
991 print_attr(attr, NULL);
997 static void rtnl_newaddr(struct nlmsghdr *hdr)
999 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1003 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1004 msg, IFA_PAYLOAD(hdr));
1007 static void rtnl_deladdr(struct nlmsghdr *hdr)
1009 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1013 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1014 msg, IFA_PAYLOAD(hdr));
1017 static void rtnl_route(struct nlmsghdr *hdr)
1020 struct rtattr *attr;
1023 msg = (struct rtmsg *) NLMSG_DATA(hdr);
1024 bytes = RTM_PAYLOAD(hdr);
1026 print("rtm_family %d rtm_table %d rtm_protocol %d",
1027 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
1028 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
1029 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
1031 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
1032 attr = RTA_NEXT(attr, bytes)) {
1033 switch (attr->rta_type) {
1035 print_inet(attr, "dst", msg->rtm_family);
1038 print_inet(attr, "src", msg->rtm_family);
1041 print_string(attr, "iif");
1044 print_integer(attr, "oif");
1047 print_inet(attr, "gateway", msg->rtm_family);
1050 print_attr(attr, "priority");
1053 print_inet(attr, "prefsrc", msg->rtm_family);
1056 print_attr(attr, "metrics");
1059 print_integer(attr, "table");
1062 print_attr(attr, NULL);
1068 static connman_bool_t is_route_rtmsg(struct rtmsg *msg)
1071 if (msg->rtm_table != RT_TABLE_MAIN)
1074 if (msg->rtm_protocol != RTPROT_BOOT &&
1075 msg->rtm_protocol != RTPROT_KERNEL)
1078 if (msg->rtm_type != RTN_UNICAST)
1084 static void rtnl_newroute(struct nlmsghdr *hdr)
1086 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1090 if (is_route_rtmsg(msg))
1091 process_newroute(msg->rtm_family, msg->rtm_scope,
1092 msg, RTM_PAYLOAD(hdr));
1095 static void rtnl_delroute(struct nlmsghdr *hdr)
1097 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1101 if (is_route_rtmsg(msg))
1102 process_delroute(msg->rtm_family, msg->rtm_scope,
1103 msg, RTM_PAYLOAD(hdr));
1106 static void *rtnl_nd_opt_rdnss(struct nd_opt_hdr *opt, guint32 *lifetime,
1109 guint32 *optint = (void *)opt;
1111 if (opt->nd_opt_len < 3)
1114 if (*lifetime > ntohl(optint[1]))
1115 *lifetime = ntohl(optint[1]);
1117 /* nd_opt_len is in units of 8 bytes. The header is 1 unit (8 bytes)
1118 and each address is another 2 units (16 bytes).
1119 So the number of addresses (given rounding) is nd_opt_len/2 */
1120 *nr_servers = opt->nd_opt_len / 2;
1122 /* And they start 8 bytes into the packet, or two guint32s in. */
1126 static const char **rtnl_nd_opt_dnssl(struct nd_opt_hdr *opt, guint32 *lifetime)
1128 const char **domains = NULL;
1129 guint32 *optint = (void *)opt;
1130 unsigned char *optc = (void *)&optint[2];
1131 int data_len = (opt->nd_opt_len * 8) - 8;
1135 if (*lifetime > ntohl(optint[1]))
1136 *lifetime = ntohl(optint[1]);
1138 /* Turn it into normal strings by converting the length bytes into '.',
1139 and count how many search domains there are while we're at it. */
1141 while (i < data_len) {
1142 if (optc[i] > 0x3f) {
1143 DBG("DNSSL contains compressed elements in violation of RFC6106");
1150 /* Check for double zero */
1151 if (i < data_len && optc[i] == 0)
1159 if (i >= data_len) {
1160 DBG("DNSSL data overflows option length");
1167 domains = g_try_new0(const char *, nr_domains + 1);
1171 /* Now point to the normal strings, missing out the leading '.' that
1172 each of them will have now. */
1173 for (i = 0; i < nr_domains; i++) {
1174 domains[i] = (char *)optc + 1;
1175 optc += strlen((char *)optc) + 1;
1181 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1183 struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1184 struct nd_opt_hdr *opt;
1185 guint32 lifetime = -1;
1186 const char **domains = NULL;
1187 struct in6_addr *servers = NULL;
1188 int i, nr_servers = 0;
1189 int msglen = msg->nduseropt_opts_len;
1192 DBG("family %d index %d len %d type %d code %d",
1193 msg->nduseropt_family, msg->nduseropt_ifindex,
1194 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1195 msg->nduseropt_icmp_code);
1197 if (msg->nduseropt_family != AF_INET6 ||
1198 msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1199 msg->nduseropt_icmp_code != 0)
1202 interface = connman_inet_ifname(msg->nduseropt_ifindex);
1206 for (opt = (void *)&msg[1];
1208 msglen -= opt->nd_opt_len * 8,
1209 opt = ((void *)opt) + opt->nd_opt_len*8) {
1211 DBG("remaining %d nd opt type %d len %d\n",
1212 msglen, opt->nd_opt_type, opt->nd_opt_len);
1214 if (opt->nd_opt_type == 25) { /* ND_OPT_RDNSS */
1217 servers = rtnl_nd_opt_rdnss(opt, &lifetime,
1219 for (i = 0; i < nr_servers; i++) {
1220 if (!inet_ntop(AF_INET6, servers + i, buf,
1224 connman_resolver_append_lifetime(interface,
1225 NULL, buf, lifetime);
1228 } else if (opt->nd_opt_type == 31) { /* ND_OPT_DNSSL */
1231 domains = rtnl_nd_opt_dnssl(opt, &lifetime);
1232 for (i = 0; domains != NULL && domains[i] != NULL; i++)
1233 connman_resolver_append_lifetime(interface,
1234 domains[i], NULL, lifetime);
1242 static const char *type2string(uint16_t type)
1271 case RTM_NEWNDUSEROPT:
1272 return "NEWNDUSEROPT";
1278 static GIOChannel *channel = NULL;
1280 struct rtnl_request {
1281 struct nlmsghdr hdr;
1282 struct rtgenmsg msg;
1284 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1286 static GSList *request_list = NULL;
1287 static guint32 request_seq = 0;
1289 static struct rtnl_request *find_request(guint32 seq)
1293 for (list = request_list; list; list = list->next) {
1294 struct rtnl_request *req = list->data;
1296 if (req->hdr.nlmsg_seq == seq)
1303 static int send_request(struct rtnl_request *req)
1305 struct sockaddr_nl addr;
1308 DBG("%s len %d type %d flags 0x%04x seq %d",
1309 type2string(req->hdr.nlmsg_type),
1310 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1311 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1313 sk = g_io_channel_unix_get_fd(channel);
1315 memset(&addr, 0, sizeof(addr));
1316 addr.nl_family = AF_NETLINK;
1318 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1319 (struct sockaddr *) &addr, sizeof(addr));
1322 static int queue_request(struct rtnl_request *req)
1324 request_list = g_slist_append(request_list, req);
1326 if (g_slist_length(request_list) > 1)
1329 return send_request(req);
1332 static int process_response(guint32 seq)
1334 struct rtnl_request *req;
1338 req = find_request(seq);
1340 request_list = g_slist_remove(request_list, req);
1344 req = g_slist_nth_data(request_list, 0);
1348 return send_request(req);
1351 static void rtnl_message(void *buf, size_t len)
1353 DBG("buf %p len %zd", buf, len);
1356 struct nlmsghdr *hdr = buf;
1357 struct nlmsgerr *err;
1359 if (!NLMSG_OK(hdr, len))
1362 DBG("%s len %d type %d flags 0x%04x seq %d pid %d",
1363 type2string(hdr->nlmsg_type),
1364 hdr->nlmsg_len, hdr->nlmsg_type,
1365 hdr->nlmsg_flags, hdr->nlmsg_seq,
1368 switch (hdr->nlmsg_type) {
1373 process_response(hdr->nlmsg_seq);
1376 err = NLMSG_DATA(hdr);
1377 DBG("error %d (%s)", -err->error,
1378 strerror(-err->error));
1398 case RTM_NEWNDUSEROPT:
1399 rtnl_newnduseropt(hdr);
1403 len -= hdr->nlmsg_len;
1404 buf += hdr->nlmsg_len;
1408 static gboolean netlink_event(GIOChannel *chan,
1409 GIOCondition cond, gpointer data)
1411 unsigned char buf[4096];
1412 struct sockaddr_nl nladdr;
1413 socklen_t addr_len = sizeof(nladdr);
1417 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1420 memset(buf, 0, sizeof(buf));
1421 memset(&nladdr, 0, sizeof(nladdr));
1423 fd = g_io_channel_unix_get_fd(chan);
1425 status = recvfrom(fd, buf, sizeof(buf), 0,
1426 (struct sockaddr *) &nladdr, &addr_len);
1428 if (errno == EINTR || errno == EAGAIN)
1437 if (nladdr.nl_pid != 0) { /* not sent by kernel, ignore */
1438 DBG("Received msg from %u, ignoring it", nladdr.nl_pid);
1442 rtnl_message(buf, status);
1447 static int send_getlink(void)
1449 struct rtnl_request *req;
1453 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1457 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1458 req->hdr.nlmsg_type = RTM_GETLINK;
1459 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1460 req->hdr.nlmsg_pid = 0;
1461 req->hdr.nlmsg_seq = request_seq++;
1462 req->msg.rtgen_family = AF_INET;
1464 return queue_request(req);
1467 static int send_getaddr(void)
1469 struct rtnl_request *req;
1473 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1477 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1478 req->hdr.nlmsg_type = RTM_GETADDR;
1479 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1480 req->hdr.nlmsg_pid = 0;
1481 req->hdr.nlmsg_seq = request_seq++;
1482 req->msg.rtgen_family = AF_INET;
1484 return queue_request(req);
1487 static int send_getroute(void)
1489 struct rtnl_request *req;
1493 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1497 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1498 req->hdr.nlmsg_type = RTM_GETROUTE;
1499 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1500 req->hdr.nlmsg_pid = 0;
1501 req->hdr.nlmsg_seq = request_seq++;
1502 req->msg.rtgen_family = AF_INET;
1504 return queue_request(req);
1507 static gboolean update_timeout_cb(gpointer user_data)
1509 __connman_rtnl_request_update();
1514 static void update_interval_callback(guint min)
1516 if (update_timeout > 0)
1517 g_source_remove(update_timeout);
1519 if (min < G_MAXUINT) {
1520 update_interval = min;
1521 update_timeout = g_timeout_add_seconds(update_interval,
1522 update_timeout_cb, NULL);
1525 update_interval = G_MAXUINT;
1529 static gint compare_interval(gconstpointer a, gconstpointer b)
1531 guint val_a = GPOINTER_TO_UINT(a);
1532 guint val_b = GPOINTER_TO_UINT(b);
1534 return val_a - val_b;
1537 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1544 update_list = g_slist_insert_sorted(update_list,
1545 GUINT_TO_POINTER(interval), compare_interval);
1547 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1548 if (min < update_interval) {
1549 update_interval_callback(min);
1550 __connman_rtnl_request_update();
1553 return update_interval;
1556 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1558 guint min = G_MAXUINT;
1563 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1565 if (update_list != NULL)
1566 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1568 if (min > update_interval)
1569 update_interval_callback(min);
1574 int __connman_rtnl_request_update(void)
1576 return send_getlink();
1579 int __connman_rtnl_init(void)
1581 struct sockaddr_nl addr;
1586 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1587 NULL, free_interface);
1589 sk = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
1593 memset(&addr, 0, sizeof(addr));
1594 addr.nl_family = AF_NETLINK;
1595 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1596 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1597 (1<<(RTNLGRP_ND_USEROPT-1));
1599 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1604 channel = g_io_channel_unix_new(sk);
1605 g_io_channel_set_close_on_unref(channel, TRUE);
1607 g_io_channel_set_encoding(channel, NULL, NULL);
1608 g_io_channel_set_buffered(channel, FALSE);
1610 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1611 netlink_event, NULL);
1616 void __connman_rtnl_start(void)
1625 void __connman_rtnl_cleanup(void)
1631 for (list = watch_list; list; list = list->next) {
1632 struct watch_data *watch = list->data;
1634 DBG("removing watch %d", watch->id);
1640 g_slist_free(watch_list);
1643 g_slist_free(update_list);
1646 for (list = request_list; list; list = list->next) {
1647 struct rtnl_request *req = list->data;
1649 DBG("%s len %d type %d flags 0x%04x seq %d",
1650 type2string(req->hdr.nlmsg_type),
1651 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1652 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1658 g_slist_free(request_list);
1659 request_list = NULL;
1661 g_io_channel_shutdown(channel, TRUE, NULL);
1662 g_io_channel_unref(channel);
1666 g_hash_table_destroy(interface_list);