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 { if (0) connman_info(arg); } 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_from_index(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_from_index(index);
287 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
289 rtnl->newlink(type, index, flags, 0);
292 if (rtnl->newgateway) {
293 const char *gateway = __connman_ipconfig_get_gateway_from_index(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_ipv4_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 extract_ipv6_route(struct rtmsg *msg, int bytes, int *index,
662 struct in6_addr *dst,
663 struct in6_addr *gateway)
667 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
668 attr = RTA_NEXT(attr, bytes)) {
669 switch (attr->rta_type) {
672 *dst = *((struct in6_addr *) RTA_DATA(attr));
677 *((struct in6_addr *) RTA_DATA(attr));
681 *index = *((int *) RTA_DATA(attr));
687 static void process_newroute(unsigned char family, unsigned char scope,
688 struct rtmsg *msg, int bytes)
691 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
694 if (family == AF_INET) {
695 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
697 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
699 inet_ntop(family, &dst, dststr, sizeof(dststr));
700 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
702 __connman_ipconfig_newroute(index, family, scope, dststr,
705 /* skip host specific routes */
706 if (scope != RT_SCOPE_UNIVERSE &&
707 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
710 if (dst.s_addr != INADDR_ANY)
713 } else if (family == AF_INET6) {
714 struct in6_addr dst = IN6ADDR_ANY_INIT,
715 gateway = IN6ADDR_ANY_INIT;
717 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
719 inet_ntop(family, &dst, dststr, sizeof(dststr));
720 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
722 __connman_ipconfig_newroute(index, family, scope, dststr,
725 /* skip host specific routes */
726 if (scope != RT_SCOPE_UNIVERSE &&
727 !(scope == RT_SCOPE_LINK &&
728 IN6_IS_ADDR_UNSPECIFIED(&dst)))
731 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
736 for (list = rtnl_list; list; list = list->next) {
737 struct connman_rtnl *rtnl = list->data;
739 if (rtnl->newgateway)
740 rtnl->newgateway(index, gatewaystr);
744 static void process_delroute(unsigned char family, unsigned char scope,
745 struct rtmsg *msg, int bytes)
748 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
751 if (family == AF_INET) {
752 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
754 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
756 inet_ntop(family, &dst, dststr, sizeof(dststr));
757 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
759 __connman_ipconfig_delroute(index, family, scope, dststr,
762 /* skip host specific routes */
763 if (scope != RT_SCOPE_UNIVERSE &&
764 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
767 if (dst.s_addr != INADDR_ANY)
770 } else if (family == AF_INET6) {
771 struct in6_addr dst = IN6ADDR_ANY_INIT,
772 gateway = IN6ADDR_ANY_INIT;
774 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
776 inet_ntop(family, &dst, dststr, sizeof(dststr));
777 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
779 __connman_ipconfig_delroute(index, family, scope, dststr,
782 /* skip host specific routes */
783 if (scope != RT_SCOPE_UNIVERSE &&
784 !(scope == RT_SCOPE_LINK &&
785 IN6_IS_ADDR_UNSPECIFIED(&dst)))
788 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
793 for (list = rtnl_list; list; list = list->next) {
794 struct connman_rtnl *rtnl = list->data;
796 if (rtnl->delgateway)
797 rtnl->delgateway(index, gatewaystr);
801 static inline void print_ether(struct rtattr *attr, const char *name)
803 int len = (int) RTA_PAYLOAD(attr);
805 if (len == ETH_ALEN) {
806 struct ether_addr eth;
807 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
808 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
810 print(" attr %s (len %d)\n", name, len);
813 static inline void print_inet(struct rtattr *attr, const char *name,
814 unsigned char family)
816 int len = (int) RTA_PAYLOAD(attr);
818 if (family == AF_INET && len == sizeof(struct in_addr)) {
820 addr = *((struct in_addr *) RTA_DATA(attr));
821 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
823 print(" attr %s (len %d)\n", name, len);
826 static inline void print_string(struct rtattr *attr, const char *name)
828 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
829 (char *) RTA_DATA(attr));
832 static inline void print_byte(struct rtattr *attr, const char *name)
834 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
835 *((unsigned char *) RTA_DATA(attr)));
838 static inline void print_integer(struct rtattr *attr, const char *name)
840 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
841 *((int *) RTA_DATA(attr)));
844 static inline void print_attr(struct rtattr *attr, const char *name)
846 int len = (int) RTA_PAYLOAD(attr);
849 print(" attr %s (len %d)\n", name, len);
851 print(" attr %d (len %d)\n", attr->rta_type, len);
854 static void rtnl_link(struct nlmsghdr *hdr)
856 struct ifinfomsg *msg;
860 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
861 bytes = IFLA_PAYLOAD(hdr);
863 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
865 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
866 attr = RTA_NEXT(attr, bytes)) {
867 switch (attr->rta_type) {
869 print_ether(attr, "address");
872 print_ether(attr, "broadcast");
875 print_string(attr, "ifname");
878 print_integer(attr, "mtu");
881 print_attr(attr, "link");
884 print_attr(attr, "qdisc");
887 print_attr(attr, "stats");
890 print_attr(attr, "cost");
893 print_attr(attr, "priority");
896 print_attr(attr, "master");
899 print_attr(attr, "wireless");
902 print_attr(attr, "protinfo");
905 print_integer(attr, "txqlen");
908 print_attr(attr, "map");
911 print_attr(attr, "weight");
914 print_byte(attr, "operstate");
917 print_byte(attr, "linkmode");
920 print_attr(attr, NULL);
926 static void rtnl_newlink(struct nlmsghdr *hdr)
928 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
932 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
933 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
936 static void rtnl_dellink(struct nlmsghdr *hdr)
938 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
942 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
943 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
946 static void rtnl_addr(struct nlmsghdr *hdr)
948 struct ifaddrmsg *msg;
952 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
953 bytes = IFA_PAYLOAD(hdr);
955 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
957 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
958 attr = RTA_NEXT(attr, bytes)) {
959 switch (attr->rta_type) {
961 print_inet(attr, "address", msg->ifa_family);
964 print_inet(attr, "local", msg->ifa_family);
967 print_string(attr, "label");
970 print_inet(attr, "broadcast", msg->ifa_family);
973 print_attr(attr, "anycast");
976 print_attr(attr, "cacheinfo");
979 print_attr(attr, "multicast");
982 print_attr(attr, NULL);
988 static void rtnl_newaddr(struct nlmsghdr *hdr)
990 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
994 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
995 msg, IFA_PAYLOAD(hdr));
998 static void rtnl_deladdr(struct nlmsghdr *hdr)
1000 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1004 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1005 msg, IFA_PAYLOAD(hdr));
1008 static void rtnl_route(struct nlmsghdr *hdr)
1011 struct rtattr *attr;
1014 msg = (struct rtmsg *) NLMSG_DATA(hdr);
1015 bytes = RTM_PAYLOAD(hdr);
1017 print("rtm_family %d rtm_table %d rtm_protocol %d",
1018 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
1019 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
1020 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
1022 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
1023 attr = RTA_NEXT(attr, bytes)) {
1024 switch (attr->rta_type) {
1026 print_inet(attr, "dst", msg->rtm_family);
1029 print_inet(attr, "src", msg->rtm_family);
1032 print_string(attr, "iif");
1035 print_integer(attr, "oif");
1038 print_inet(attr, "gateway", msg->rtm_family);
1041 print_attr(attr, "priority");
1044 print_inet(attr, "prefsrc", msg->rtm_family);
1047 print_attr(attr, "metrics");
1050 print_integer(attr, "table");
1053 print_attr(attr, NULL);
1059 static connman_bool_t is_route_rtmsg(struct rtmsg *msg)
1062 if (msg->rtm_table != RT_TABLE_MAIN)
1065 if (msg->rtm_protocol != RTPROT_BOOT &&
1066 msg->rtm_protocol != RTPROT_KERNEL)
1069 if (msg->rtm_type != RTN_UNICAST)
1075 static void rtnl_newroute(struct nlmsghdr *hdr)
1077 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1081 if (is_route_rtmsg(msg))
1082 process_newroute(msg->rtm_family, msg->rtm_scope,
1083 msg, RTM_PAYLOAD(hdr));
1086 static void rtnl_delroute(struct nlmsghdr *hdr)
1088 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1092 if (is_route_rtmsg(msg))
1093 process_delroute(msg->rtm_family, msg->rtm_scope,
1094 msg, RTM_PAYLOAD(hdr));
1097 static void *rtnl_nd_opt_rdnss(struct nd_opt_hdr *opt, guint32 *lifetime,
1100 guint32 *optint = (void *)opt;
1102 if (opt->nd_opt_len < 3)
1105 if (*lifetime > ntohl(optint[1]))
1106 *lifetime = ntohl(optint[1]);
1108 /* nd_opt_len is in units of 8 bytes. The header is 1 unit (8 bytes)
1109 and each address is another 2 units (16 bytes).
1110 So the number of addresses (given rounding) is nd_opt_len/2 */
1111 *nr_servers = opt->nd_opt_len / 2;
1113 /* And they start 8 bytes into the packet, or two guint32s in. */
1117 static const char **rtnl_nd_opt_dnssl(struct nd_opt_hdr *opt, guint32 *lifetime)
1119 const char **domains = NULL;
1120 guint32 *optint = (void *)opt;
1121 unsigned char *optc = (void *)&optint[2];
1122 int data_len = (opt->nd_opt_len * 8) - 8;
1126 if (*lifetime > ntohl(optint[1]))
1127 *lifetime = ntohl(optint[1]);
1129 /* Turn it into normal strings by converting the length bytes into '.',
1130 and count how many search domains there are while we're at it. */
1132 while (i < data_len) {
1133 if (optc[i] > 0x3f) {
1134 DBG("DNSSL contains compressed elements in violation of RFC6106");
1141 /* Check for double zero */
1142 if (i < data_len && optc[i] == 0)
1150 if (i >= data_len) {
1151 DBG("DNSSL data overflows option length");
1158 domains = g_try_new0(const char *, nr_domains + 1);
1162 /* Now point to the normal strings, missing out the leading '.' that
1163 each of them will have now. */
1164 for (i = 0; i < nr_domains; i++) {
1165 domains[i] = (char *)optc + 1;
1166 optc += strlen((char *)optc) + 1;
1172 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1174 struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1175 struct nd_opt_hdr *opt = (void *)&msg[1];
1176 guint32 lifetime = -1;
1177 const char **domains = NULL;
1178 struct in6_addr *servers = NULL;
1180 int msglen = msg->nduseropt_opts_len;
1183 DBG("family %02x index %x len %04x type %02x code %02x",
1184 msg->nduseropt_family, msg->nduseropt_ifindex,
1185 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1186 msg->nduseropt_icmp_code);
1188 if (msg->nduseropt_family != AF_INET6 ||
1189 msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1190 msg->nduseropt_icmp_code != 0)
1193 interface = connman_inet_ifname(msg->nduseropt_ifindex);
1197 for (opt = (void *)&msg[1];
1198 msglen >= 2 && msglen >= opt->nd_opt_len && opt->nd_opt_len;
1199 msglen -= opt->nd_opt_len,
1200 opt = ((void *)opt) + opt->nd_opt_len*8) {
1202 DBG("nd opt type %d len %d\n",
1203 opt->nd_opt_type, opt->nd_opt_len);
1205 if (opt->nd_opt_type == 25)
1206 servers = rtnl_nd_opt_rdnss(opt, &lifetime,
1208 else if (opt->nd_opt_type == 31)
1209 domains = rtnl_nd_opt_dnssl(opt, &lifetime);
1216 for (i = 0; i < nr_servers; i++) {
1217 if (!inet_ntop(AF_INET6, servers + i, buf, sizeof(buf)))
1220 if (domains == NULL || domains[0] == NULL) {
1221 connman_resolver_append_lifetime(interface,
1222 NULL, buf, lifetime);
1226 for (j = 0; domains[j]; j++)
1227 connman_resolver_append_lifetime(interface,
1236 static const char *type2string(uint16_t type)
1263 case RTM_NEWNDUSEROPT:
1264 return "NEWNDUSEROPT";
1270 static GIOChannel *channel = NULL;
1272 struct rtnl_request {
1273 struct nlmsghdr hdr;
1274 struct rtgenmsg msg;
1276 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1278 static GSList *request_list = NULL;
1279 static guint32 request_seq = 0;
1281 static struct rtnl_request *find_request(guint32 seq)
1285 for (list = request_list; list; list = list->next) {
1286 struct rtnl_request *req = list->data;
1288 if (req->hdr.nlmsg_seq == seq)
1295 static int send_request(struct rtnl_request *req)
1297 struct sockaddr_nl addr;
1300 DBG("%s len %d type %d flags 0x%04x seq %d",
1301 type2string(req->hdr.nlmsg_type),
1302 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1303 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1305 sk = g_io_channel_unix_get_fd(channel);
1307 memset(&addr, 0, sizeof(addr));
1308 addr.nl_family = AF_NETLINK;
1310 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1311 (struct sockaddr *) &addr, sizeof(addr));
1314 static int queue_request(struct rtnl_request *req)
1316 request_list = g_slist_append(request_list, req);
1318 if (g_slist_length(request_list) > 1)
1321 return send_request(req);
1324 static int process_response(guint32 seq)
1326 struct rtnl_request *req;
1330 req = find_request(seq);
1332 request_list = g_slist_remove(request_list, req);
1336 req = g_slist_nth_data(request_list, 0);
1340 return send_request(req);
1343 static void rtnl_message(void *buf, size_t len)
1345 DBG("buf %p len %zd", buf, len);
1348 struct nlmsghdr *hdr = buf;
1349 struct nlmsgerr *err;
1351 if (!NLMSG_OK(hdr, len))
1354 DBG("%s len %d type %d flags 0x%04x seq %d",
1355 type2string(hdr->nlmsg_type),
1356 hdr->nlmsg_len, hdr->nlmsg_type,
1357 hdr->nlmsg_flags, hdr->nlmsg_seq);
1359 switch (hdr->nlmsg_type) {
1364 process_response(hdr->nlmsg_seq);
1367 err = NLMSG_DATA(hdr);
1368 DBG("error %d (%s)", -err->error,
1369 strerror(-err->error));
1389 case RTM_NEWNDUSEROPT:
1390 rtnl_newnduseropt(hdr);
1394 len -= hdr->nlmsg_len;
1395 buf += hdr->nlmsg_len;
1399 static gboolean netlink_event(GIOChannel *chan,
1400 GIOCondition cond, gpointer data)
1402 unsigned char buf[4096];
1406 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1409 memset(buf, 0, sizeof(buf));
1411 status = g_io_channel_read_chars(chan, (gchar *) buf,
1412 sizeof(buf), &len, NULL);
1415 case G_IO_STATUS_NORMAL:
1417 case G_IO_STATUS_AGAIN:
1423 rtnl_message(buf, len);
1428 static int send_getlink(void)
1430 struct rtnl_request *req;
1434 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1438 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1439 req->hdr.nlmsg_type = RTM_GETLINK;
1440 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1441 req->hdr.nlmsg_pid = 0;
1442 req->hdr.nlmsg_seq = request_seq++;
1443 req->msg.rtgen_family = AF_INET;
1445 return queue_request(req);
1448 static int send_getaddr(void)
1450 struct rtnl_request *req;
1454 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1458 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1459 req->hdr.nlmsg_type = RTM_GETADDR;
1460 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1461 req->hdr.nlmsg_pid = 0;
1462 req->hdr.nlmsg_seq = request_seq++;
1463 req->msg.rtgen_family = AF_INET;
1465 return queue_request(req);
1468 static int send_getroute(void)
1470 struct rtnl_request *req;
1474 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1478 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1479 req->hdr.nlmsg_type = RTM_GETROUTE;
1480 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1481 req->hdr.nlmsg_pid = 0;
1482 req->hdr.nlmsg_seq = request_seq++;
1483 req->msg.rtgen_family = AF_INET;
1485 return queue_request(req);
1488 static gboolean update_timeout_cb(gpointer user_data)
1490 __connman_rtnl_request_update();
1495 static void update_interval_callback(guint min)
1497 if (update_timeout > 0)
1498 g_source_remove(update_timeout);
1500 if (min < G_MAXUINT) {
1501 update_interval = min;
1502 update_timeout = g_timeout_add_seconds(update_interval,
1503 update_timeout_cb, NULL);
1506 update_interval = G_MAXUINT;
1510 static gint compare_interval(gconstpointer a, gconstpointer b)
1512 guint val_a = GPOINTER_TO_UINT(a);
1513 guint val_b = GPOINTER_TO_UINT(b);
1515 return val_a - val_b;
1518 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1525 update_list = g_slist_insert_sorted(update_list,
1526 GUINT_TO_POINTER(interval), compare_interval);
1528 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1529 if (min < update_interval) {
1530 update_interval_callback(min);
1531 __connman_rtnl_request_update();
1534 return update_interval;
1537 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1539 guint min = G_MAXUINT;
1544 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1546 if (update_list != NULL)
1547 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1549 if (min > update_interval)
1550 update_interval_callback(min);
1555 int __connman_rtnl_request_update(void)
1557 return send_getlink();
1560 int __connman_rtnl_init(void)
1562 struct sockaddr_nl addr;
1567 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1568 NULL, free_interface);
1570 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
1574 memset(&addr, 0, sizeof(addr));
1575 addr.nl_family = AF_NETLINK;
1576 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1577 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1578 (1<<(RTNLGRP_ND_USEROPT-1));
1580 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1585 channel = g_io_channel_unix_new(sk);
1586 g_io_channel_set_close_on_unref(channel, TRUE);
1588 g_io_channel_set_encoding(channel, NULL, NULL);
1589 g_io_channel_set_buffered(channel, FALSE);
1591 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1592 netlink_event, NULL);
1597 void __connman_rtnl_start(void)
1606 void __connman_rtnl_cleanup(void)
1612 for (list = watch_list; list; list = list->next) {
1613 struct watch_data *watch = list->data;
1615 DBG("removing watch %d", watch->id);
1621 g_slist_free(watch_list);
1624 g_slist_free(update_list);
1627 for (list = request_list; list; list = list->next) {
1628 struct rtnl_request *req = list->data;
1630 DBG("%s len %d type %d flags 0x%04x seq %d",
1631 type2string(req->hdr.nlmsg_type),
1632 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1633 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1639 g_slist_free(request_list);
1640 request_list = NULL;
1642 g_io_channel_shutdown(channel, TRUE, NULL);
1643 g_io_channel_unref(channel);
1647 g_hash_table_destroy(interface_list);