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
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 /* virtual interface from VMware */
94 if (g_str_has_prefix(name, "vmnet") == TRUE)
97 /* virtual interface from VirtualBox */
98 if (g_str_has_prefix(name, "vboxnet") == TRUE)
101 /* virtual interface from Virtual Machine Manager */
102 if (g_str_has_prefix(name, "virbr") == TRUE)
108 static connman_bool_t wext_interface(char *ifname)
113 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
117 memset(&wrq, 0, sizeof(wrq));
118 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
120 err = ioctl(fd, SIOCGIWNAME, &wrq);
130 static void read_uevent(struct interface_data *interface)
132 char *filename, line[128];
133 connman_bool_t found_devtype;
136 if (ether_blacklisted(interface->name) == TRUE) {
137 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
138 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
140 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
141 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
144 filename = g_strdup_printf("/sys/class/net/%s/uevent",
147 f = fopen(filename, "re");
154 found_devtype = FALSE;
155 while (fgets(line, sizeof(line), f)) {
158 pos = strchr(line, '\n');
163 if (strncmp(line, "DEVTYPE=", 8) != 0)
166 found_devtype = TRUE;
168 if (strcmp(line + 8, "wlan") == 0) {
169 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
170 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
171 } else if (strcmp(line + 8, "wwan") == 0) {
172 interface->service_type = CONNMAN_SERVICE_TYPE_CELLULAR;
173 interface->device_type = CONNMAN_DEVICE_TYPE_CELLULAR;
174 } else if (strcmp(line + 8, "bluetooth") == 0) {
175 interface->service_type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
176 interface->device_type = CONNMAN_DEVICE_TYPE_BLUETOOTH;
177 } else if (strcmp(line + 8, "wimax") == 0) {
178 interface->service_type = CONNMAN_SERVICE_TYPE_WIMAX;
179 interface->device_type = CONNMAN_DEVICE_TYPE_WIMAX;
180 } else if (strcmp(line + 8, "gadget") == 0) {
181 interface->service_type = CONNMAN_SERVICE_TYPE_GADGET;
182 interface->device_type = CONNMAN_DEVICE_TYPE_GADGET;
185 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
186 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
195 /* We haven't got a DEVTYPE, let's check if it's a wireless device */
196 if (wext_interface(interface->name)) {
197 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
198 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
200 connman_error("%s runs an unsupported 802.11 driver",
205 enum connman_device_type __connman_rtnl_get_device_type(int index)
207 struct interface_data *interface;
209 interface = g_hash_table_lookup(interface_list,
210 GINT_TO_POINTER(index));
211 if (interface == NULL)
212 return CONNMAN_DEVICE_TYPE_UNKNOWN;
214 return interface->device_type;
218 * connman_rtnl_add_newlink_watch:
219 * @index: network device index
220 * @callback: callback function
221 * @user_data: callback data;
223 * Add a new RTNL watch for newlink events
225 * Returns: %0 on failure and a unique id on success
227 unsigned int connman_rtnl_add_newlink_watch(int index,
228 connman_rtnl_link_cb_t callback, void *user_data)
230 struct watch_data *watch;
232 watch = g_try_new0(struct watch_data, 1);
236 watch->id = ++watch_id;
237 watch->index = index;
239 watch->newlink = callback;
240 watch->user_data = user_data;
242 watch_list = g_slist_prepend(watch_list, watch);
244 DBG("id %d", watch->id);
247 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
250 callback(flags, 0, user_data);
257 * connman_rtnl_remove_watch:
258 * @id: watch identifier
260 * Remove the RTNL watch for the identifier
262 void connman_rtnl_remove_watch(unsigned int id)
271 for (list = watch_list; list; list = list->next) {
272 struct watch_data *watch = list->data;
274 if (watch->id == id) {
275 watch_list = g_slist_remove(watch_list, watch);
282 static void trigger_rtnl(int index, void *user_data)
284 struct connman_rtnl *rtnl = user_data;
287 unsigned short type = __connman_ipconfig_get_type_from_index(index);
288 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
290 rtnl->newlink(type, index, flags, 0);
293 if (rtnl->newgateway) {
294 const char *gateway = __connman_ipconfig_get_gateway_from_index(index);
297 rtnl->newgateway(index, gateway);
301 static GSList *rtnl_list = NULL;
303 static gint compare_priority(gconstpointer a, gconstpointer b)
305 const struct connman_rtnl *rtnl1 = a;
306 const struct connman_rtnl *rtnl2 = b;
308 return rtnl2->priority - rtnl1->priority;
312 * connman_rtnl_register:
315 * Register a new RTNL module
317 * Returns: %0 on success
319 int connman_rtnl_register(struct connman_rtnl *rtnl)
321 DBG("rtnl %p name %s", rtnl, rtnl->name);
323 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
326 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
332 * connman_rtnl_unregister:
335 * Remove a previously registered RTNL module
337 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
339 DBG("rtnl %p name %s", rtnl, rtnl->name);
341 rtnl_list = g_slist_remove(rtnl_list, rtnl);
344 static const char *operstate2str(unsigned char operstate)
347 case IF_OPER_UNKNOWN:
349 case IF_OPER_NOTPRESENT:
350 return "NOT-PRESENT";
353 case IF_OPER_LOWERLAYERDOWN:
354 return "LOWER-LAYER-DOWN";
355 case IF_OPER_TESTING:
357 case IF_OPER_DORMANT:
366 static void extract_link(struct ifinfomsg *msg, int bytes,
367 struct ether_addr *address, const char **ifname,
368 unsigned int *mtu, unsigned char *operstate,
369 struct rtnl_link_stats *stats)
373 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
374 attr = RTA_NEXT(attr, bytes)) {
375 switch (attr->rta_type) {
378 memcpy(address, RTA_DATA(attr), ETH_ALEN);
382 *ifname = RTA_DATA(attr);
386 *mtu = *((unsigned int *) RTA_DATA(attr));
390 memcpy(stats, RTA_DATA(attr),
391 sizeof(struct rtnl_link_stats));
394 if (operstate != NULL)
395 *operstate = *((unsigned char *) RTA_DATA(attr));
403 static void process_newlink(unsigned short type, int index, unsigned flags,
404 unsigned change, struct ifinfomsg *msg, int bytes)
406 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
407 struct ether_addr compare = {{ 0, 0, 0, 0, 0, 0 }};
408 struct rtnl_link_stats stats;
409 unsigned char operstate = 0xff;
410 struct interface_data *interface;
411 const char *ifname = NULL;
412 unsigned int mtu = 0;
413 char ident[13], str[18];
416 memset(&stats, 0, sizeof(stats));
417 extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats);
419 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
420 address.ether_addr_octet[0],
421 address.ether_addr_octet[1],
422 address.ether_addr_octet[2],
423 address.ether_addr_octet[3],
424 address.ether_addr_octet[4],
425 address.ether_addr_octet[5]);
427 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
428 address.ether_addr_octet[0],
429 address.ether_addr_octet[1],
430 address.ether_addr_octet[2],
431 address.ether_addr_octet[3],
432 address.ether_addr_octet[4],
433 address.ether_addr_octet[5]);
437 case ARPHRD_LOOPBACK:
438 case ARPHDR_PHONET_PIPE:
440 #if defined TIZEN_EXT
442 * Description: SLP requires ARPHRD_PPP PPP type device
446 __connman_ipconfig_newlink(index, type, flags,
451 if (memcmp(&address, &compare, ETH_ALEN) != 0)
452 connman_info("%s {newlink} index %d address %s mtu %u",
453 ifname, index, str, mtu);
455 if (operstate != 0xff)
456 connman_info("%s {newlink} index %d operstate %u <%s>",
457 ifname, index, operstate,
458 operstate2str(operstate));
460 interface = g_hash_table_lookup(interface_list, GINT_TO_POINTER(index));
461 if (interface == NULL) {
462 interface = g_new0(struct interface_data, 1);
463 interface->index = index;
464 interface->name = g_strdup(ifname);
465 interface->ident = g_strdup(ident);
467 g_hash_table_insert(interface_list,
468 GINT_TO_POINTER(index), interface);
470 if (type == ARPHRD_ETHER)
471 read_uevent(interface);
473 #if defined TIZEN_EXT
474 if (type == ARPHRD_PPP) {
475 interface->service_type = CONNMAN_SERVICE_TYPE_CELLULAR;
476 interface->device_type = CONNMAN_DEVICE_TYPE_CELLULAR;
480 __connman_technology_add_interface(interface->service_type,
481 interface->index, interface->name, interface->ident);
484 for (list = rtnl_list; list; list = list->next) {
485 struct connman_rtnl *rtnl = list->data;
488 rtnl->newlink(type, index, flags, change);
491 for (list = watch_list; list; list = list->next) {
492 struct watch_data *watch = list->data;
494 if (watch->index != index)
498 watch->newlink(flags, change, watch->user_data);
502 static void process_dellink(unsigned short type, int index, unsigned flags,
503 unsigned change, struct ifinfomsg *msg, int bytes)
505 struct rtnl_link_stats stats;
506 unsigned char operstate = 0xff;
507 const char *ifname = NULL;
510 memset(&stats, 0, sizeof(stats));
511 extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats);
513 if (operstate != 0xff)
514 connman_info("%s {dellink} index %d operstate %u <%s>",
515 ifname, index, operstate,
516 operstate2str(operstate));
518 for (list = rtnl_list; list; list = list->next) {
519 struct connman_rtnl *rtnl = list->data;
522 rtnl->dellink(type, index, flags, change);
527 case ARPHRD_LOOPBACK:
529 #if defined TIZEN_EXT
531 * Description: SLP requires ARPHRD_PPP PPP type device
534 DBG("interface index(%d)", index);
536 __connman_ipconfig_dellink(index, &stats);
540 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
543 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
545 struct in_addr *local,
546 struct in_addr *address,
547 struct in_addr *broadcast)
551 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
552 attr = RTA_NEXT(attr, bytes)) {
553 switch (attr->rta_type) {
556 *address = *((struct in_addr *) RTA_DATA(attr));
560 *local = *((struct in_addr *) RTA_DATA(attr));
563 if (broadcast != NULL)
564 *broadcast = *((struct in_addr *) RTA_DATA(attr));
568 *label = RTA_DATA(attr);
574 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
575 struct in6_addr *addr,
576 struct in6_addr *local)
580 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
581 attr = RTA_NEXT(attr, bytes)) {
582 switch (attr->rta_type) {
585 *addr = *((struct in6_addr *) RTA_DATA(attr));
589 *local = *((struct in6_addr *) RTA_DATA(attr));
595 static void process_newaddr(unsigned char family, unsigned char prefixlen,
596 int index, struct ifaddrmsg *msg, int bytes)
598 const char *label = NULL;
600 char ip_string[INET6_ADDRSTRLEN];
602 if (family == AF_INET) {
603 struct in_addr ipv4_addr = { INADDR_ANY };
605 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
607 } else if (family == AF_INET6) {
608 struct in6_addr ipv6_address, ipv6_local;
610 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
611 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
619 if (inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN) == NULL)
622 __connman_ipconfig_newaddr(index, family, label,
623 prefixlen, ip_string);
626 static void process_deladdr(unsigned char family, unsigned char prefixlen,
627 int index, struct ifaddrmsg *msg, int bytes)
629 const char *label = NULL;
631 char ip_string[INET6_ADDRSTRLEN];
633 if (family == AF_INET) {
634 struct in_addr ipv4_addr = { INADDR_ANY };
636 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
638 } else if (family == AF_INET6) {
639 struct in6_addr ipv6_address, ipv6_local;
641 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
642 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
650 if (inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN) == NULL)
653 __connman_ipconfig_deladdr(index, family, label,
654 prefixlen, ip_string);
657 static void extract_ipv4_route(struct rtmsg *msg, int bytes, int *index,
659 struct in_addr *gateway)
663 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
664 attr = RTA_NEXT(attr, bytes)) {
665 switch (attr->rta_type) {
668 *dst = *((struct in_addr *) RTA_DATA(attr));
672 *gateway = *((struct in_addr *) RTA_DATA(attr));
676 *index = *((int *) RTA_DATA(attr));
682 static void extract_ipv6_route(struct rtmsg *msg, int bytes, int *index,
683 struct in6_addr *dst,
684 struct in6_addr *gateway)
688 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
689 attr = RTA_NEXT(attr, bytes)) {
690 switch (attr->rta_type) {
693 *dst = *((struct in6_addr *) RTA_DATA(attr));
698 *((struct in6_addr *) RTA_DATA(attr));
702 *index = *((int *) RTA_DATA(attr));
708 static void process_newroute(unsigned char family, unsigned char scope,
709 struct rtmsg *msg, int bytes)
712 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
715 if (family == AF_INET) {
716 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
718 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
720 inet_ntop(family, &dst, dststr, sizeof(dststr));
721 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
723 __connman_ipconfig_newroute(index, family, scope, dststr,
726 /* skip host specific routes */
727 if (scope != RT_SCOPE_UNIVERSE &&
728 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
731 if (dst.s_addr != INADDR_ANY)
734 } else if (family == AF_INET6) {
735 struct in6_addr dst = IN6ADDR_ANY_INIT,
736 gateway = IN6ADDR_ANY_INIT;
738 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
740 inet_ntop(family, &dst, dststr, sizeof(dststr));
741 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
743 __connman_ipconfig_newroute(index, family, scope, dststr,
746 /* skip host specific routes */
747 if (scope != RT_SCOPE_UNIVERSE &&
748 !(scope == RT_SCOPE_LINK &&
749 IN6_IS_ADDR_UNSPECIFIED(&dst)))
752 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
757 for (list = rtnl_list; list; list = list->next) {
758 struct connman_rtnl *rtnl = list->data;
760 if (rtnl->newgateway)
761 rtnl->newgateway(index, gatewaystr);
765 static void process_delroute(unsigned char family, unsigned char scope,
766 struct rtmsg *msg, int bytes)
769 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
772 if (family == AF_INET) {
773 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
775 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
777 inet_ntop(family, &dst, dststr, sizeof(dststr));
778 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
780 __connman_ipconfig_delroute(index, family, scope, dststr,
783 /* skip host specific routes */
784 if (scope != RT_SCOPE_UNIVERSE &&
785 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
788 if (dst.s_addr != INADDR_ANY)
791 } else if (family == AF_INET6) {
792 struct in6_addr dst = IN6ADDR_ANY_INIT,
793 gateway = IN6ADDR_ANY_INIT;
795 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
797 inet_ntop(family, &dst, dststr, sizeof(dststr));
798 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
800 __connman_ipconfig_delroute(index, family, scope, dststr,
803 /* skip host specific routes */
804 if (scope != RT_SCOPE_UNIVERSE &&
805 !(scope == RT_SCOPE_LINK &&
806 IN6_IS_ADDR_UNSPECIFIED(&dst)))
809 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
814 for (list = rtnl_list; list; list = list->next) {
815 struct connman_rtnl *rtnl = list->data;
817 if (rtnl->delgateway)
818 rtnl->delgateway(index, gatewaystr);
822 static inline void print_ether(struct rtattr *attr, const char *name)
824 int len = (int) RTA_PAYLOAD(attr);
826 if (len == ETH_ALEN) {
827 struct ether_addr eth;
828 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
829 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
831 print(" attr %s (len %d)\n", name, len);
834 static inline void print_inet(struct rtattr *attr, const char *name,
835 unsigned char family)
837 int len = (int) RTA_PAYLOAD(attr);
839 if (family == AF_INET && len == sizeof(struct in_addr)) {
841 addr = *((struct in_addr *) RTA_DATA(attr));
842 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
844 print(" attr %s (len %d)\n", name, len);
847 static inline void print_string(struct rtattr *attr, const char *name)
849 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
850 (char *) RTA_DATA(attr));
853 static inline void print_byte(struct rtattr *attr, const char *name)
855 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
856 *((unsigned char *) RTA_DATA(attr)));
859 static inline void print_integer(struct rtattr *attr, const char *name)
861 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
862 *((int *) RTA_DATA(attr)));
865 static inline void print_attr(struct rtattr *attr, const char *name)
867 int len = (int) RTA_PAYLOAD(attr);
870 print(" attr %s (len %d)\n", name, len);
872 print(" attr %d (len %d)\n", attr->rta_type, len);
875 static void rtnl_link(struct nlmsghdr *hdr)
877 struct ifinfomsg *msg;
881 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
882 bytes = IFLA_PAYLOAD(hdr);
884 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
886 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
887 attr = RTA_NEXT(attr, bytes)) {
888 switch (attr->rta_type) {
890 print_ether(attr, "address");
893 print_ether(attr, "broadcast");
896 print_string(attr, "ifname");
899 print_integer(attr, "mtu");
902 print_attr(attr, "link");
905 print_attr(attr, "qdisc");
908 print_attr(attr, "stats");
911 print_attr(attr, "cost");
914 print_attr(attr, "priority");
917 print_attr(attr, "master");
920 print_attr(attr, "wireless");
923 print_attr(attr, "protinfo");
926 print_integer(attr, "txqlen");
929 print_attr(attr, "map");
932 print_attr(attr, "weight");
935 print_byte(attr, "operstate");
938 print_byte(attr, "linkmode");
941 print_attr(attr, NULL);
947 static void rtnl_newlink(struct nlmsghdr *hdr)
949 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
953 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
954 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
957 static void rtnl_dellink(struct nlmsghdr *hdr)
959 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
963 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
964 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
967 static void rtnl_addr(struct nlmsghdr *hdr)
969 struct ifaddrmsg *msg;
973 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
974 bytes = IFA_PAYLOAD(hdr);
976 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
978 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
979 attr = RTA_NEXT(attr, bytes)) {
980 switch (attr->rta_type) {
982 print_inet(attr, "address", msg->ifa_family);
985 print_inet(attr, "local", msg->ifa_family);
988 print_string(attr, "label");
991 print_inet(attr, "broadcast", msg->ifa_family);
994 print_attr(attr, "anycast");
997 print_attr(attr, "cacheinfo");
1000 print_attr(attr, "multicast");
1003 print_attr(attr, NULL);
1009 static void rtnl_newaddr(struct nlmsghdr *hdr)
1011 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1015 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1016 msg, IFA_PAYLOAD(hdr));
1019 static void rtnl_deladdr(struct nlmsghdr *hdr)
1021 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1025 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1026 msg, IFA_PAYLOAD(hdr));
1029 static void rtnl_route(struct nlmsghdr *hdr)
1032 struct rtattr *attr;
1035 msg = (struct rtmsg *) NLMSG_DATA(hdr);
1036 bytes = RTM_PAYLOAD(hdr);
1038 print("rtm_family %d rtm_table %d rtm_protocol %d",
1039 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
1040 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
1041 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
1043 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
1044 attr = RTA_NEXT(attr, bytes)) {
1045 switch (attr->rta_type) {
1047 print_inet(attr, "dst", msg->rtm_family);
1050 print_inet(attr, "src", msg->rtm_family);
1053 print_string(attr, "iif");
1056 print_integer(attr, "oif");
1059 print_inet(attr, "gateway", msg->rtm_family);
1062 print_attr(attr, "priority");
1065 print_inet(attr, "prefsrc", msg->rtm_family);
1068 print_attr(attr, "metrics");
1071 print_integer(attr, "table");
1074 print_attr(attr, NULL);
1080 static connman_bool_t is_route_rtmsg(struct rtmsg *msg)
1083 if (msg->rtm_table != RT_TABLE_MAIN)
1086 if (msg->rtm_protocol != RTPROT_BOOT &&
1087 msg->rtm_protocol != RTPROT_KERNEL)
1090 if (msg->rtm_type != RTN_UNICAST)
1096 static void rtnl_newroute(struct nlmsghdr *hdr)
1098 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1102 if (is_route_rtmsg(msg))
1103 process_newroute(msg->rtm_family, msg->rtm_scope,
1104 msg, RTM_PAYLOAD(hdr));
1107 static void rtnl_delroute(struct nlmsghdr *hdr)
1109 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1113 if (is_route_rtmsg(msg))
1114 process_delroute(msg->rtm_family, msg->rtm_scope,
1115 msg, RTM_PAYLOAD(hdr));
1118 static void *rtnl_nd_opt_rdnss(struct nd_opt_hdr *opt, guint32 *lifetime,
1121 guint32 *optint = (void *)opt;
1123 if (opt->nd_opt_len < 3)
1126 if (*lifetime > ntohl(optint[1]))
1127 *lifetime = ntohl(optint[1]);
1129 /* nd_opt_len is in units of 8 bytes. The header is 1 unit (8 bytes)
1130 and each address is another 2 units (16 bytes).
1131 So the number of addresses (given rounding) is nd_opt_len/2 */
1132 *nr_servers = opt->nd_opt_len / 2;
1134 /* And they start 8 bytes into the packet, or two guint32s in. */
1138 static const char **rtnl_nd_opt_dnssl(struct nd_opt_hdr *opt, guint32 *lifetime)
1140 const char **domains = NULL;
1141 guint32 *optint = (void *)opt;
1142 unsigned char *optc = (void *)&optint[2];
1143 int data_len = (opt->nd_opt_len * 8) - 8;
1147 if (*lifetime > ntohl(optint[1]))
1148 *lifetime = ntohl(optint[1]);
1150 /* Turn it into normal strings by converting the length bytes into '.',
1151 and count how many search domains there are while we're at it. */
1153 while (i < data_len) {
1154 if (optc[i] > 0x3f) {
1155 DBG("DNSSL contains compressed elements in violation of RFC6106");
1162 /* Check for double zero */
1163 if (i < data_len && optc[i] == 0)
1171 if (i >= data_len) {
1172 DBG("DNSSL data overflows option length");
1179 domains = g_try_new0(const char *, nr_domains + 1);
1183 /* Now point to the normal strings, missing out the leading '.' that
1184 each of them will have now. */
1185 for (i = 0; i < nr_domains; i++) {
1186 domains[i] = (char *)optc + 1;
1187 optc += strlen((char *)optc) + 1;
1193 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1195 struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1196 struct nd_opt_hdr *opt = (void *)&msg[1];
1197 guint32 lifetime = -1;
1198 const char **domains = NULL;
1199 struct in6_addr *servers = NULL;
1201 int msglen = msg->nduseropt_opts_len;
1204 DBG("family %02x index %x len %04x type %02x code %02x",
1205 msg->nduseropt_family, msg->nduseropt_ifindex,
1206 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1207 msg->nduseropt_icmp_code);
1209 if (msg->nduseropt_family != AF_INET6 ||
1210 msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1211 msg->nduseropt_icmp_code != 0)
1214 interface = connman_inet_ifname(msg->nduseropt_ifindex);
1218 for (opt = (void *)&msg[1];
1219 msglen >= 2 && msglen >= opt->nd_opt_len && opt->nd_opt_len;
1220 msglen -= opt->nd_opt_len,
1221 opt = ((void *)opt) + opt->nd_opt_len*8) {
1223 DBG("nd opt type %d len %d\n",
1224 opt->nd_opt_type, opt->nd_opt_len);
1226 if (opt->nd_opt_type == 25)
1227 servers = rtnl_nd_opt_rdnss(opt, &lifetime,
1229 else if (opt->nd_opt_type == 31)
1230 domains = rtnl_nd_opt_dnssl(opt, &lifetime);
1237 for (i = 0; i < nr_servers; i++) {
1238 if (!inet_ntop(AF_INET6, servers + i, buf, sizeof(buf)))
1241 if (domains == NULL || domains[0] == NULL) {
1242 connman_resolver_append_lifetime(interface,
1243 NULL, buf, lifetime);
1247 for (j = 0; domains[j]; j++)
1248 connman_resolver_append_lifetime(interface,
1257 static const char *type2string(uint16_t type)
1284 case RTM_NEWNDUSEROPT:
1285 return "NEWNDUSEROPT";
1291 static GIOChannel *channel = NULL;
1293 struct rtnl_request {
1294 struct nlmsghdr hdr;
1295 struct rtgenmsg msg;
1297 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1299 static GSList *request_list = NULL;
1300 static guint32 request_seq = 0;
1302 static struct rtnl_request *find_request(guint32 seq)
1306 for (list = request_list; list; list = list->next) {
1307 struct rtnl_request *req = list->data;
1309 if (req->hdr.nlmsg_seq == seq)
1316 static int send_request(struct rtnl_request *req)
1318 struct sockaddr_nl addr;
1321 DBG("%s len %d type %d flags 0x%04x seq %d",
1322 type2string(req->hdr.nlmsg_type),
1323 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1324 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1326 sk = g_io_channel_unix_get_fd(channel);
1328 memset(&addr, 0, sizeof(addr));
1329 addr.nl_family = AF_NETLINK;
1331 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1332 (struct sockaddr *) &addr, sizeof(addr));
1335 static int queue_request(struct rtnl_request *req)
1337 request_list = g_slist_append(request_list, req);
1339 if (g_slist_length(request_list) > 1)
1342 return send_request(req);
1345 static int process_response(guint32 seq)
1347 struct rtnl_request *req;
1351 req = find_request(seq);
1353 request_list = g_slist_remove(request_list, req);
1357 req = g_slist_nth_data(request_list, 0);
1361 return send_request(req);
1364 static void rtnl_message(void *buf, size_t len)
1366 DBG("buf %p len %zd", buf, len);
1369 struct nlmsghdr *hdr = buf;
1370 struct nlmsgerr *err;
1372 if (!NLMSG_OK(hdr, len))
1375 DBG("%s len %d type %d flags 0x%04x seq %d",
1376 type2string(hdr->nlmsg_type),
1377 hdr->nlmsg_len, hdr->nlmsg_type,
1378 hdr->nlmsg_flags, hdr->nlmsg_seq);
1380 switch (hdr->nlmsg_type) {
1385 process_response(hdr->nlmsg_seq);
1388 err = NLMSG_DATA(hdr);
1389 DBG("error %d (%s)", -err->error,
1390 strerror(-err->error));
1410 case RTM_NEWNDUSEROPT:
1411 rtnl_newnduseropt(hdr);
1415 len -= hdr->nlmsg_len;
1416 buf += hdr->nlmsg_len;
1420 static gboolean netlink_event(GIOChannel *chan,
1421 GIOCondition cond, gpointer data)
1423 unsigned char buf[4096];
1427 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1430 memset(buf, 0, sizeof(buf));
1432 status = g_io_channel_read_chars(chan, (gchar *) buf,
1433 sizeof(buf), &len, NULL);
1436 case G_IO_STATUS_NORMAL:
1438 case G_IO_STATUS_AGAIN:
1444 rtnl_message(buf, len);
1449 static int send_getlink(void)
1451 struct rtnl_request *req;
1455 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1459 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1460 req->hdr.nlmsg_type = RTM_GETLINK;
1461 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1462 req->hdr.nlmsg_pid = 0;
1463 req->hdr.nlmsg_seq = request_seq++;
1464 req->msg.rtgen_family = AF_INET;
1466 return queue_request(req);
1469 static int send_getaddr(void)
1471 struct rtnl_request *req;
1475 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1479 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1480 req->hdr.nlmsg_type = RTM_GETADDR;
1481 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1482 req->hdr.nlmsg_pid = 0;
1483 req->hdr.nlmsg_seq = request_seq++;
1484 req->msg.rtgen_family = AF_INET;
1486 return queue_request(req);
1489 static int send_getroute(void)
1491 struct rtnl_request *req;
1495 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1499 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1500 req->hdr.nlmsg_type = RTM_GETROUTE;
1501 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1502 req->hdr.nlmsg_pid = 0;
1503 req->hdr.nlmsg_seq = request_seq++;
1504 req->msg.rtgen_family = AF_INET;
1506 return queue_request(req);
1509 static gboolean update_timeout_cb(gpointer user_data)
1511 __connman_rtnl_request_update();
1516 static void update_interval_callback(guint min)
1518 if (update_timeout > 0)
1519 g_source_remove(update_timeout);
1521 if (min < G_MAXUINT) {
1522 update_interval = min;
1523 update_timeout = g_timeout_add_seconds(update_interval,
1524 update_timeout_cb, NULL);
1527 update_interval = G_MAXUINT;
1531 static gint compare_interval(gconstpointer a, gconstpointer b)
1533 guint val_a = GPOINTER_TO_UINT(a);
1534 guint val_b = GPOINTER_TO_UINT(b);
1536 return val_a - val_b;
1539 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1546 update_list = g_slist_insert_sorted(update_list,
1547 GUINT_TO_POINTER(interval), compare_interval);
1549 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1550 if (min < update_interval) {
1551 update_interval_callback(min);
1552 __connman_rtnl_request_update();
1555 return update_interval;
1558 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1560 guint min = G_MAXUINT;
1565 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1567 if (update_list != NULL)
1568 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1570 if (min > update_interval)
1571 update_interval_callback(min);
1576 int __connman_rtnl_request_update(void)
1578 return send_getlink();
1581 int __connman_rtnl_init(void)
1583 struct sockaddr_nl addr;
1588 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1589 NULL, free_interface);
1591 sk = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
1595 memset(&addr, 0, sizeof(addr));
1596 addr.nl_family = AF_NETLINK;
1597 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1598 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1599 (1<<(RTNLGRP_ND_USEROPT-1));
1601 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1606 channel = g_io_channel_unix_new(sk);
1607 g_io_channel_set_close_on_unref(channel, TRUE);
1609 g_io_channel_set_encoding(channel, NULL, NULL);
1610 g_io_channel_set_buffered(channel, FALSE);
1612 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1613 netlink_event, NULL);
1618 void __connman_rtnl_start(void)
1627 void __connman_rtnl_cleanup(void)
1633 for (list = watch_list; list; list = list->next) {
1634 struct watch_data *watch = list->data;
1636 DBG("removing watch %d", watch->id);
1642 g_slist_free(watch_list);
1645 g_slist_free(update_list);
1648 for (list = request_list; list; list = list->next) {
1649 struct rtnl_request *req = list->data;
1651 DBG("%s len %d type %d flags 0x%04x seq %d",
1652 type2string(req->hdr.nlmsg_type),
1653 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1654 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1660 g_slist_free(request_list);
1661 request_list = NULL;
1663 g_io_channel_shutdown(channel, TRUE, NULL);
1664 g_io_channel_unref(channel);
1668 g_hash_table_destroy(interface_list);