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, "gadget") == 0) {
169 interface->service_type = CONNMAN_SERVICE_TYPE_GADGET;
170 interface->device_type = CONNMAN_DEVICE_TYPE_GADGET;
173 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
174 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
183 /* We haven't got a DEVTYPE, let's check if it's a wireless device */
184 if (wext_interface(interface->name)) {
185 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
186 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
188 connman_error("%s runs an unsupported 802.11 driver",
193 enum connman_device_type __connman_rtnl_get_device_type(int index)
195 struct interface_data *interface;
197 interface = g_hash_table_lookup(interface_list,
198 GINT_TO_POINTER(index));
199 if (interface == NULL)
200 return CONNMAN_DEVICE_TYPE_UNKNOWN;
202 return interface->device_type;
206 * connman_rtnl_add_newlink_watch:
207 * @index: network device index
208 * @callback: callback function
209 * @user_data: callback data;
211 * Add a new RTNL watch for newlink events
213 * Returns: %0 on failure and a unique id on success
215 unsigned int connman_rtnl_add_newlink_watch(int index,
216 connman_rtnl_link_cb_t callback, void *user_data)
218 struct watch_data *watch;
220 watch = g_try_new0(struct watch_data, 1);
224 watch->id = ++watch_id;
225 watch->index = index;
227 watch->newlink = callback;
228 watch->user_data = user_data;
230 watch_list = g_slist_prepend(watch_list, watch);
232 DBG("id %d", watch->id);
235 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
238 callback(flags, 0, user_data);
245 * connman_rtnl_remove_watch:
246 * @id: watch identifier
248 * Remove the RTNL watch for the identifier
250 void connman_rtnl_remove_watch(unsigned int id)
259 for (list = watch_list; list; list = list->next) {
260 struct watch_data *watch = list->data;
262 if (watch->id == id) {
263 watch_list = g_slist_remove(watch_list, watch);
270 static void trigger_rtnl(int index, void *user_data)
272 struct connman_rtnl *rtnl = user_data;
275 unsigned short type = __connman_ipconfig_get_type_from_index(index);
276 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
278 rtnl->newlink(type, index, flags, 0);
281 if (rtnl->newgateway) {
282 const char *gateway =
283 __connman_ipconfig_get_gateway_from_index(index,
284 CONNMAN_IPCONFIG_TYPE_ALL);
287 rtnl->newgateway(index, gateway);
291 static GSList *rtnl_list = NULL;
293 static gint compare_priority(gconstpointer a, gconstpointer b)
295 const struct connman_rtnl *rtnl1 = a;
296 const struct connman_rtnl *rtnl2 = b;
298 return rtnl2->priority - rtnl1->priority;
302 * connman_rtnl_register:
305 * Register a new RTNL module
307 * Returns: %0 on success
309 int connman_rtnl_register(struct connman_rtnl *rtnl)
311 DBG("rtnl %p name %s", rtnl, rtnl->name);
313 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
316 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
322 * connman_rtnl_unregister:
325 * Remove a previously registered RTNL module
327 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
329 DBG("rtnl %p name %s", rtnl, rtnl->name);
331 rtnl_list = g_slist_remove(rtnl_list, rtnl);
334 static const char *operstate2str(unsigned char operstate)
337 case IF_OPER_UNKNOWN:
339 case IF_OPER_NOTPRESENT:
340 return "NOT-PRESENT";
343 case IF_OPER_LOWERLAYERDOWN:
344 return "LOWER-LAYER-DOWN";
345 case IF_OPER_TESTING:
347 case IF_OPER_DORMANT:
356 static connman_bool_t extract_link(struct ifinfomsg *msg, int bytes,
357 struct ether_addr *address, const char **ifname,
358 unsigned int *mtu, unsigned char *operstate,
359 struct rtnl_link_stats *stats)
363 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
364 attr = RTA_NEXT(attr, bytes)) {
365 switch (attr->rta_type) {
368 memcpy(address, RTA_DATA(attr), ETH_ALEN);
372 *ifname = RTA_DATA(attr);
376 *mtu = *((unsigned int *) RTA_DATA(attr));
380 memcpy(stats, RTA_DATA(attr),
381 sizeof(struct rtnl_link_stats));
384 if (operstate != NULL)
385 *operstate = *((unsigned char *) RTA_DATA(attr));
397 static void process_newlink(unsigned short type, int index, unsigned flags,
398 unsigned change, struct ifinfomsg *msg, int bytes)
400 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
401 struct ether_addr compare = {{ 0, 0, 0, 0, 0, 0 }};
402 struct rtnl_link_stats stats;
403 unsigned char operstate = 0xff;
404 struct interface_data *interface;
405 const char *ifname = NULL;
406 unsigned int mtu = 0;
407 char ident[13], str[18];
410 memset(&stats, 0, sizeof(stats));
411 if (extract_link(msg, bytes, &address, &ifname, &mtu, &operstate,
415 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
416 address.ether_addr_octet[0],
417 address.ether_addr_octet[1],
418 address.ether_addr_octet[2],
419 address.ether_addr_octet[3],
420 address.ether_addr_octet[4],
421 address.ether_addr_octet[5]);
423 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
424 address.ether_addr_octet[0],
425 address.ether_addr_octet[1],
426 address.ether_addr_octet[2],
427 address.ether_addr_octet[3],
428 address.ether_addr_octet[4],
429 address.ether_addr_octet[5]);
433 case ARPHRD_LOOPBACK:
434 case ARPHDR_PHONET_PIPE:
437 __connman_ipconfig_newlink(index, type, flags,
442 if (memcmp(&address, &compare, ETH_ALEN) != 0)
443 connman_info("%s {newlink} index %d address %s mtu %u",
444 ifname, index, str, mtu);
446 if (operstate != 0xff)
447 connman_info("%s {newlink} index %d operstate %u <%s>",
448 ifname, index, operstate,
449 operstate2str(operstate));
451 interface = g_hash_table_lookup(interface_list, GINT_TO_POINTER(index));
452 if (interface == NULL) {
453 interface = g_new0(struct interface_data, 1);
454 interface->index = index;
455 interface->name = g_strdup(ifname);
456 interface->ident = g_strdup(ident);
458 g_hash_table_insert(interface_list,
459 GINT_TO_POINTER(index), interface);
461 if (type == ARPHRD_ETHER)
462 read_uevent(interface);
464 __connman_technology_add_interface(interface->service_type,
465 interface->index, interface->name, interface->ident);
468 for (list = rtnl_list; list; list = list->next) {
469 struct connman_rtnl *rtnl = list->data;
472 rtnl->newlink(type, index, flags, change);
475 for (list = watch_list; list; list = list->next) {
476 struct watch_data *watch = list->data;
478 if (watch->index != index)
482 watch->newlink(flags, change, watch->user_data);
486 static void process_dellink(unsigned short type, int index, unsigned flags,
487 unsigned change, struct ifinfomsg *msg, int bytes)
489 struct rtnl_link_stats stats;
490 unsigned char operstate = 0xff;
491 const char *ifname = NULL;
494 memset(&stats, 0, sizeof(stats));
495 if (extract_link(msg, bytes, NULL, &ifname, NULL, &operstate,
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);
604 if (family == AF_INET6) {
606 * Re-create RDNSS configured servers if there are any
607 * for this interface. This is done because we might
608 * have now properly configured interface with proper
609 * autoconfigured address.
611 __connman_resolver_redo_servers(index);
615 static void process_deladdr(unsigned char family, unsigned char prefixlen,
616 int index, struct ifaddrmsg *msg, int bytes)
618 const char *label = NULL;
620 char ip_string[INET6_ADDRSTRLEN];
622 if (family == AF_INET) {
623 struct in_addr ipv4_addr = { INADDR_ANY };
625 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
627 } else if (family == AF_INET6) {
628 struct in6_addr ipv6_address, ipv6_local;
630 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
631 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
639 if (inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN) == NULL)
642 __connman_ipconfig_deladdr(index, family, label,
643 prefixlen, ip_string);
646 static void extract_ipv4_route(struct rtmsg *msg, int bytes, int *index,
648 struct in_addr *gateway)
652 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
653 attr = RTA_NEXT(attr, bytes)) {
654 switch (attr->rta_type) {
657 *dst = *((struct in_addr *) RTA_DATA(attr));
661 *gateway = *((struct in_addr *) RTA_DATA(attr));
665 *index = *((int *) RTA_DATA(attr));
671 static void extract_ipv6_route(struct rtmsg *msg, int bytes, int *index,
672 struct in6_addr *dst,
673 struct in6_addr *gateway)
677 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
678 attr = RTA_NEXT(attr, bytes)) {
679 switch (attr->rta_type) {
682 *dst = *((struct in6_addr *) RTA_DATA(attr));
687 *((struct in6_addr *) RTA_DATA(attr));
691 *index = *((int *) RTA_DATA(attr));
697 static void process_newroute(unsigned char family, unsigned char scope,
698 struct rtmsg *msg, int bytes)
701 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
704 if (family == AF_INET) {
705 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
707 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
709 inet_ntop(family, &dst, dststr, sizeof(dststr));
710 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
712 __connman_ipconfig_newroute(index, family, scope, dststr,
715 /* skip host specific routes */
716 if (scope != RT_SCOPE_UNIVERSE &&
717 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
720 if (dst.s_addr != INADDR_ANY)
723 } else if (family == AF_INET6) {
724 struct in6_addr dst = IN6ADDR_ANY_INIT,
725 gateway = IN6ADDR_ANY_INIT;
727 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
729 inet_ntop(family, &dst, dststr, sizeof(dststr));
730 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
732 __connman_ipconfig_newroute(index, family, scope, dststr,
735 /* skip host specific routes */
736 if (scope != RT_SCOPE_UNIVERSE &&
737 !(scope == RT_SCOPE_LINK &&
738 IN6_IS_ADDR_UNSPECIFIED(&dst)))
741 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
746 for (list = rtnl_list; list; list = list->next) {
747 struct connman_rtnl *rtnl = list->data;
749 if (rtnl->newgateway)
750 rtnl->newgateway(index, gatewaystr);
754 static void process_delroute(unsigned char family, unsigned char scope,
755 struct rtmsg *msg, int bytes)
758 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
761 if (family == AF_INET) {
762 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
764 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
766 inet_ntop(family, &dst, dststr, sizeof(dststr));
767 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
769 __connman_ipconfig_delroute(index, family, scope, dststr,
772 /* skip host specific routes */
773 if (scope != RT_SCOPE_UNIVERSE &&
774 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
777 if (dst.s_addr != INADDR_ANY)
780 } else if (family == AF_INET6) {
781 struct in6_addr dst = IN6ADDR_ANY_INIT,
782 gateway = IN6ADDR_ANY_INIT;
784 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
786 inet_ntop(family, &dst, dststr, sizeof(dststr));
787 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
789 __connman_ipconfig_delroute(index, family, scope, dststr,
792 /* skip host specific routes */
793 if (scope != RT_SCOPE_UNIVERSE &&
794 !(scope == RT_SCOPE_LINK &&
795 IN6_IS_ADDR_UNSPECIFIED(&dst)))
798 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
803 for (list = rtnl_list; list; list = list->next) {
804 struct connman_rtnl *rtnl = list->data;
806 if (rtnl->delgateway)
807 rtnl->delgateway(index, gatewaystr);
811 static inline void print_ether(struct rtattr *attr, const char *name)
813 int len = (int) RTA_PAYLOAD(attr);
815 if (len == ETH_ALEN) {
816 struct ether_addr eth;
817 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
818 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
820 print(" attr %s (len %d)\n", name, len);
823 static inline void print_inet(struct rtattr *attr, const char *name,
824 unsigned char family)
826 int len = (int) RTA_PAYLOAD(attr);
828 if (family == AF_INET && len == sizeof(struct in_addr)) {
830 addr = *((struct in_addr *) RTA_DATA(attr));
831 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
833 print(" attr %s (len %d)\n", name, len);
836 static inline void print_string(struct rtattr *attr, const char *name)
838 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
839 (char *) RTA_DATA(attr));
842 static inline void print_byte(struct rtattr *attr, const char *name)
844 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
845 *((unsigned char *) RTA_DATA(attr)));
848 static inline void print_integer(struct rtattr *attr, const char *name)
850 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
851 *((int *) RTA_DATA(attr)));
854 static inline void print_attr(struct rtattr *attr, const char *name)
856 int len = (int) RTA_PAYLOAD(attr);
859 print(" attr %s (len %d)\n", name, len);
861 print(" attr %d (len %d)\n", attr->rta_type, len);
864 static void rtnl_link(struct nlmsghdr *hdr)
866 struct ifinfomsg *msg;
870 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
871 bytes = IFLA_PAYLOAD(hdr);
873 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
875 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
876 attr = RTA_NEXT(attr, bytes)) {
877 switch (attr->rta_type) {
879 print_ether(attr, "address");
882 print_ether(attr, "broadcast");
885 print_string(attr, "ifname");
888 print_integer(attr, "mtu");
891 print_attr(attr, "link");
894 print_attr(attr, "qdisc");
897 print_attr(attr, "stats");
900 print_attr(attr, "cost");
903 print_attr(attr, "priority");
906 print_attr(attr, "master");
909 print_attr(attr, "wireless");
912 print_attr(attr, "protinfo");
915 print_integer(attr, "txqlen");
918 print_attr(attr, "map");
921 print_attr(attr, "weight");
924 print_byte(attr, "operstate");
927 print_byte(attr, "linkmode");
930 print_attr(attr, NULL);
936 static void rtnl_newlink(struct nlmsghdr *hdr)
938 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
942 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
943 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
946 static void rtnl_dellink(struct nlmsghdr *hdr)
948 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
952 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
953 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
956 static void rtnl_addr(struct nlmsghdr *hdr)
958 struct ifaddrmsg *msg;
962 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
963 bytes = IFA_PAYLOAD(hdr);
965 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
967 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
968 attr = RTA_NEXT(attr, bytes)) {
969 switch (attr->rta_type) {
971 print_inet(attr, "address", msg->ifa_family);
974 print_inet(attr, "local", msg->ifa_family);
977 print_string(attr, "label");
980 print_inet(attr, "broadcast", msg->ifa_family);
983 print_attr(attr, "anycast");
986 print_attr(attr, "cacheinfo");
989 print_attr(attr, "multicast");
992 print_attr(attr, NULL);
998 static void rtnl_newaddr(struct nlmsghdr *hdr)
1000 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1004 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1005 msg, IFA_PAYLOAD(hdr));
1008 static void rtnl_deladdr(struct nlmsghdr *hdr)
1010 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1014 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1015 msg, IFA_PAYLOAD(hdr));
1018 static void rtnl_route(struct nlmsghdr *hdr)
1021 struct rtattr *attr;
1024 msg = (struct rtmsg *) NLMSG_DATA(hdr);
1025 bytes = RTM_PAYLOAD(hdr);
1027 print("rtm_family %d rtm_table %d rtm_protocol %d",
1028 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
1029 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
1030 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
1032 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
1033 attr = RTA_NEXT(attr, bytes)) {
1034 switch (attr->rta_type) {
1036 print_inet(attr, "dst", msg->rtm_family);
1039 print_inet(attr, "src", msg->rtm_family);
1042 print_string(attr, "iif");
1045 print_integer(attr, "oif");
1048 print_inet(attr, "gateway", msg->rtm_family);
1051 print_attr(attr, "priority");
1054 print_inet(attr, "prefsrc", msg->rtm_family);
1057 print_attr(attr, "metrics");
1060 print_integer(attr, "table");
1063 print_attr(attr, NULL);
1069 static connman_bool_t is_route_rtmsg(struct rtmsg *msg)
1072 if (msg->rtm_table != RT_TABLE_MAIN)
1075 if (msg->rtm_protocol != RTPROT_BOOT &&
1076 msg->rtm_protocol != RTPROT_KERNEL)
1079 if (msg->rtm_type != RTN_UNICAST)
1085 static void rtnl_newroute(struct nlmsghdr *hdr)
1087 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1091 if (is_route_rtmsg(msg))
1092 process_newroute(msg->rtm_family, msg->rtm_scope,
1093 msg, RTM_PAYLOAD(hdr));
1096 static void rtnl_delroute(struct nlmsghdr *hdr)
1098 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1102 if (is_route_rtmsg(msg))
1103 process_delroute(msg->rtm_family, msg->rtm_scope,
1104 msg, RTM_PAYLOAD(hdr));
1107 static void *rtnl_nd_opt_rdnss(struct nd_opt_hdr *opt, guint32 *lifetime,
1110 guint32 *optint = (void *)opt;
1112 if (opt->nd_opt_len < 3)
1115 if (*lifetime > ntohl(optint[1]))
1116 *lifetime = ntohl(optint[1]);
1118 /* nd_opt_len is in units of 8 bytes. The header is 1 unit (8 bytes)
1119 and each address is another 2 units (16 bytes).
1120 So the number of addresses (given rounding) is nd_opt_len/2 */
1121 *nr_servers = opt->nd_opt_len / 2;
1123 /* And they start 8 bytes into the packet, or two guint32s in. */
1127 static const char **rtnl_nd_opt_dnssl(struct nd_opt_hdr *opt, guint32 *lifetime)
1129 const char **domains = NULL;
1130 guint32 *optint = (void *)opt;
1131 unsigned char *optc = (void *)&optint[2];
1132 int data_len = (opt->nd_opt_len * 8) - 8;
1136 if (*lifetime > ntohl(optint[1]))
1137 *lifetime = ntohl(optint[1]);
1139 /* Turn it into normal strings by converting the length bytes into '.',
1140 and count how many search domains there are while we're at it. */
1142 while (i < data_len) {
1143 if (optc[i] > 0x3f) {
1144 DBG("DNSSL contains compressed elements in violation of RFC6106");
1151 /* Check for double zero */
1152 if (i < data_len && optc[i] == 0)
1160 if (i >= data_len) {
1161 DBG("DNSSL data overflows option length");
1168 domains = g_try_new0(const char *, nr_domains + 1);
1172 /* Now point to the normal strings, missing out the leading '.' that
1173 each of them will have now. */
1174 for (i = 0; i < nr_domains; i++) {
1175 domains[i] = (char *)optc + 1;
1176 optc += strlen((char *)optc) + 1;
1182 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1184 struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1185 struct nd_opt_hdr *opt;
1186 guint32 lifetime = -1;
1187 const char **domains = NULL;
1188 struct in6_addr *servers = NULL;
1189 int i, nr_servers = 0;
1190 int msglen = msg->nduseropt_opts_len;
1193 DBG("family %d index %d len %d type %d code %d",
1194 msg->nduseropt_family, msg->nduseropt_ifindex,
1195 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1196 msg->nduseropt_icmp_code);
1198 if (msg->nduseropt_family != AF_INET6 ||
1199 msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1200 msg->nduseropt_icmp_code != 0)
1203 index = msg->nduseropt_ifindex;
1207 for (opt = (void *)&msg[1];
1209 msglen -= opt->nd_opt_len * 8,
1210 opt = ((void *)opt) + opt->nd_opt_len*8) {
1212 DBG("remaining %d nd opt type %d len %d\n",
1213 msglen, opt->nd_opt_type, opt->nd_opt_len);
1215 if (opt->nd_opt_type == 25) { /* ND_OPT_RDNSS */
1218 servers = rtnl_nd_opt_rdnss(opt, &lifetime,
1220 for (i = 0; i < nr_servers; i++) {
1221 if (!inet_ntop(AF_INET6, servers + i, buf,
1225 connman_resolver_append_lifetime(index,
1226 NULL, buf, lifetime);
1229 } else if (opt->nd_opt_type == 31) { /* ND_OPT_DNSSL */
1232 domains = rtnl_nd_opt_dnssl(opt, &lifetime);
1233 for (i = 0; domains != NULL && domains[i] != NULL; i++)
1234 connman_resolver_append_lifetime(index,
1235 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);