5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <sys/socket.h>
30 #include <sys/ioctl.h>
31 #include <arpa/inet.h>
32 #include <netinet/ether.h>
33 #include <netinet/icmp6.h>
34 #include <net/if_arp.h>
36 #include <linux/netlink.h>
37 #include <linux/rtnetlink.h>
38 #include <linux/wireless.h>
44 #ifndef ARPHDR_PHONET_PIPE
45 #define ARPHDR_PHONET_PIPE (821)
48 #define print(arg...) do { } while (0)
49 //#define print(arg...) connman_info(arg)
54 connman_rtnl_link_cb_t newlink;
58 static GSList *watch_list = NULL;
59 static unsigned int watch_id = 0;
61 static GSList *update_list = NULL;
62 static guint update_interval = G_MAXUINT;
63 static guint update_timeout = 0;
65 struct interface_data {
69 enum connman_service_type service_type;
70 enum connman_device_type device_type;
73 static GHashTable *interface_list = NULL;
75 static void free_interface(gpointer data)
77 struct interface_data *interface = data;
79 __connman_technology_remove_interface(interface->service_type,
80 interface->index, interface->name, interface->ident);
82 g_free(interface->ident);
83 g_free(interface->name);
87 static connman_bool_t ether_blacklisted(const char *name)
92 /* virtual interface from VMware */
93 if (g_str_has_prefix(name, "vmnet") == TRUE)
96 /* virtual interface from VirtualBox */
97 if (g_str_has_prefix(name, "vboxnet") == TRUE)
100 /* virtual interface from Virtual Machine Manager */
101 if (g_str_has_prefix(name, "virbr") == TRUE)
107 static connman_bool_t wext_interface(char *ifname)
112 fd = socket(PF_INET, SOCK_DGRAM, 0);
116 memset(&wrq, 0, sizeof(wrq));
117 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
119 err = ioctl(fd, SIOCGIWNAME, &wrq);
129 static void read_uevent(struct interface_data *interface)
131 char *filename, line[128];
132 connman_bool_t found_devtype;
135 if (ether_blacklisted(interface->name) == TRUE) {
136 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
137 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
139 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
140 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
143 filename = g_strdup_printf("/sys/class/net/%s/uevent",
146 f = fopen(filename, "re");
153 found_devtype = FALSE;
154 while (fgets(line, sizeof(line), f)) {
157 pos = strchr(line, '\n');
162 if (strncmp(line, "DEVTYPE=", 8) != 0)
165 found_devtype = TRUE;
167 if (strcmp(line + 8, "wlan") == 0) {
168 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
169 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
170 } else if (strcmp(line + 8, "wwan") == 0) {
171 interface->service_type = CONNMAN_SERVICE_TYPE_CELLULAR;
172 interface->device_type = CONNMAN_DEVICE_TYPE_CELLULAR;
173 } else if (strcmp(line + 8, "bluetooth") == 0) {
174 interface->service_type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
175 interface->device_type = CONNMAN_DEVICE_TYPE_BLUETOOTH;
176 } else if (strcmp(line + 8, "wimax") == 0) {
177 interface->service_type = CONNMAN_SERVICE_TYPE_WIMAX;
178 interface->device_type = CONNMAN_DEVICE_TYPE_WIMAX;
179 } else if (strcmp(line + 8, "gadget") == 0) {
180 interface->service_type = CONNMAN_SERVICE_TYPE_GADGET;
181 interface->device_type = CONNMAN_DEVICE_TYPE_GADGET;
184 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
185 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
194 /* We haven't got a DEVTYPE, let's check if it's a wireless device */
195 if (wext_interface(interface->name)) {
196 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
197 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
199 connman_error("%s runs an unsupported 802.11 driver",
204 enum connman_device_type __connman_rtnl_get_device_type(int index)
206 struct interface_data *interface;
208 interface = g_hash_table_lookup(interface_list,
209 GINT_TO_POINTER(index));
210 if (interface == NULL)
211 return CONNMAN_DEVICE_TYPE_UNKNOWN;
213 return interface->device_type;
217 * connman_rtnl_add_newlink_watch:
218 * @index: network device index
219 * @callback: callback function
220 * @user_data: callback data;
222 * Add a new RTNL watch for newlink events
224 * Returns: %0 on failure and a unique id on success
226 unsigned int connman_rtnl_add_newlink_watch(int index,
227 connman_rtnl_link_cb_t callback, void *user_data)
229 struct watch_data *watch;
231 watch = g_try_new0(struct watch_data, 1);
235 watch->id = ++watch_id;
236 watch->index = index;
238 watch->newlink = callback;
239 watch->user_data = user_data;
241 watch_list = g_slist_prepend(watch_list, watch);
243 DBG("id %d", watch->id);
246 unsigned int flags = __connman_ipconfig_get_flags(index);
249 callback(flags, 0, user_data);
256 * connman_rtnl_remove_watch:
257 * @id: watch identifier
259 * Remove the RTNL watch for the identifier
261 void connman_rtnl_remove_watch(unsigned int id)
270 for (list = watch_list; list; list = list->next) {
271 struct watch_data *watch = list->data;
273 if (watch->id == id) {
274 watch_list = g_slist_remove(watch_list, watch);
281 static void trigger_rtnl(int index, void *user_data)
283 struct connman_rtnl *rtnl = user_data;
286 unsigned short type = __connman_ipconfig_get_type(index);
287 unsigned int flags = __connman_ipconfig_get_flags(index);
289 rtnl->newlink(type, index, flags, 0);
292 if (rtnl->newgateway) {
293 const char *gateway = __connman_ipconfig_get_gateway(index);
296 rtnl->newgateway(index, gateway);
300 static GSList *rtnl_list = NULL;
302 static gint compare_priority(gconstpointer a, gconstpointer b)
304 const struct connman_rtnl *rtnl1 = a;
305 const struct connman_rtnl *rtnl2 = b;
307 return rtnl2->priority - rtnl1->priority;
311 * connman_rtnl_register:
314 * Register a new RTNL module
316 * Returns: %0 on success
318 int connman_rtnl_register(struct connman_rtnl *rtnl)
320 DBG("rtnl %p name %s", rtnl, rtnl->name);
322 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
325 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
331 * connman_rtnl_unregister:
334 * Remove a previously registered RTNL module
336 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
338 DBG("rtnl %p name %s", rtnl, rtnl->name);
340 rtnl_list = g_slist_remove(rtnl_list, rtnl);
343 static const char *operstate2str(unsigned char operstate)
346 case IF_OPER_UNKNOWN:
348 case IF_OPER_NOTPRESENT:
349 return "NOT-PRESENT";
352 case IF_OPER_LOWERLAYERDOWN:
353 return "LOWER-LAYER-DOWN";
354 case IF_OPER_TESTING:
356 case IF_OPER_DORMANT:
365 static void extract_link(struct ifinfomsg *msg, int bytes,
366 struct ether_addr *address, const char **ifname,
367 unsigned int *mtu, unsigned char *operstate,
368 struct rtnl_link_stats *stats)
372 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
373 attr = RTA_NEXT(attr, bytes)) {
374 switch (attr->rta_type) {
377 memcpy(address, RTA_DATA(attr), ETH_ALEN);
381 *ifname = RTA_DATA(attr);
385 *mtu = *((unsigned int *) RTA_DATA(attr));
389 memcpy(stats, RTA_DATA(attr),
390 sizeof(struct rtnl_link_stats));
393 if (operstate != NULL)
394 *operstate = *((unsigned char *) RTA_DATA(attr));
402 static void process_newlink(unsigned short type, int index, unsigned flags,
403 unsigned change, struct ifinfomsg *msg, int bytes)
405 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
406 struct ether_addr compare = {{ 0, 0, 0, 0, 0, 0 }};
407 struct rtnl_link_stats stats;
408 unsigned char operstate = 0xff;
409 const char *ifname = NULL;
410 unsigned int mtu = 0;
411 char ident[13], str[18];
414 memset(&stats, 0, sizeof(stats));
415 extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats);
417 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
418 address.ether_addr_octet[0],
419 address.ether_addr_octet[1],
420 address.ether_addr_octet[2],
421 address.ether_addr_octet[3],
422 address.ether_addr_octet[4],
423 address.ether_addr_octet[5]);
425 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
426 address.ether_addr_octet[0],
427 address.ether_addr_octet[1],
428 address.ether_addr_octet[2],
429 address.ether_addr_octet[3],
430 address.ether_addr_octet[4],
431 address.ether_addr_octet[5]);
435 case ARPHRD_LOOPBACK:
436 case ARPHDR_PHONET_PIPE:
438 __connman_ipconfig_newlink(index, type, flags,
443 if (memcmp(&address, &compare, ETH_ALEN) != 0)
444 connman_info("%s {newlink} index %d address %s mtu %u",
445 ifname, index, str, mtu);
447 if (operstate != 0xff)
448 connman_info("%s {newlink} index %d operstate %u <%s>",
449 ifname, index, operstate,
450 operstate2str(operstate));
452 if (g_hash_table_lookup(interface_list,
453 GINT_TO_POINTER(index)) == NULL) {
454 struct interface_data *interface;
456 interface = g_new0(struct interface_data, 1);
457 interface->index = index;
458 interface->name = g_strdup(ifname);
459 interface->ident = g_strdup(ident);
461 g_hash_table_insert(interface_list,
462 GINT_TO_POINTER(index), interface);
464 if (type == ARPHRD_ETHER)
465 read_uevent(interface);
467 __connman_technology_add_interface(interface->service_type,
468 interface->index, interface->name, interface->ident);
471 for (list = rtnl_list; list; list = list->next) {
472 struct connman_rtnl *rtnl = list->data;
475 rtnl->newlink(type, index, flags, change);
478 for (list = watch_list; list; list = list->next) {
479 struct watch_data *watch = list->data;
481 if (watch->index != index)
485 watch->newlink(flags, change, watch->user_data);
489 static void process_dellink(unsigned short type, int index, unsigned flags,
490 unsigned change, struct ifinfomsg *msg, int bytes)
492 struct rtnl_link_stats stats;
493 unsigned char operstate = 0xff;
494 const char *ifname = NULL;
497 memset(&stats, 0, sizeof(stats));
498 extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats);
500 if (operstate != 0xff)
501 connman_info("%s {dellink} index %d operstate %u <%s>",
502 ifname, index, operstate,
503 operstate2str(operstate));
505 for (list = rtnl_list; list; list = list->next) {
506 struct connman_rtnl *rtnl = list->data;
509 rtnl->dellink(type, index, flags, change);
514 case ARPHRD_LOOPBACK:
516 __connman_ipconfig_dellink(index, &stats);
520 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
523 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
525 struct in_addr *local,
526 struct in_addr *address,
527 struct in_addr *broadcast)
531 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
532 attr = RTA_NEXT(attr, bytes)) {
533 switch (attr->rta_type) {
536 *address = *((struct in_addr *) RTA_DATA(attr));
540 *local = *((struct in_addr *) RTA_DATA(attr));
543 if (broadcast != NULL)
544 *broadcast = *((struct in_addr *) RTA_DATA(attr));
548 *label = RTA_DATA(attr);
554 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
555 struct in6_addr *addr,
556 struct in6_addr *local)
560 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
561 attr = RTA_NEXT(attr, bytes)) {
562 switch (attr->rta_type) {
565 *addr = *((struct in6_addr *) RTA_DATA(attr));
569 *local = *((struct in6_addr *) RTA_DATA(attr));
575 static void process_newaddr(unsigned char family, unsigned char prefixlen,
576 int index, struct ifaddrmsg *msg, int bytes)
578 const char *label = NULL;
580 char ip_string[INET6_ADDRSTRLEN];
582 if (family != AF_INET && family != AF_INET6)
585 if (family == AF_INET) {
586 struct in_addr ipv4_addr = { INADDR_ANY };
588 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
590 } else if (family == AF_INET6) {
591 struct in6_addr ipv6_address, ipv6_local;
593 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
594 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
600 if (inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN) == NULL)
603 __connman_ipconfig_newaddr(index, family, label,
604 prefixlen, ip_string);
607 static void process_deladdr(unsigned char family, unsigned char prefixlen,
608 int index, struct ifaddrmsg *msg, int bytes)
610 const char *label = NULL;
612 char ip_string[INET6_ADDRSTRLEN];
614 if (family != AF_INET && family != AF_INET6)
617 if (family == AF_INET) {
618 struct in_addr ipv4_addr = { INADDR_ANY };
620 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
622 } else if (family == AF_INET6) {
623 struct in6_addr ipv6_address, ipv6_local;
625 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
626 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
632 if (inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN) == NULL)
635 __connman_ipconfig_deladdr(index, family, label,
636 prefixlen, ip_string);
639 static void extract_route(struct rtmsg *msg, int bytes, int *index,
641 struct in_addr *gateway)
645 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
646 attr = RTA_NEXT(attr, bytes)) {
647 switch (attr->rta_type) {
650 *dst = *((struct in_addr *) RTA_DATA(attr));
654 *gateway = *((struct in_addr *) RTA_DATA(attr));
658 *index = *((int *) RTA_DATA(attr));
664 static void process_newroute(unsigned char family, unsigned char scope,
665 struct rtmsg *msg, int bytes)
668 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
669 char dststr[16], gatewaystr[16];
672 if (family != AF_INET)
675 extract_route(msg, bytes, &index, &dst, &gateway);
677 inet_ntop(family, &dst, dststr, sizeof(dststr));
678 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
680 __connman_ipconfig_newroute(index, family, scope, dststr, gatewaystr);
682 /* skip host specific routes */
683 if (scope != RT_SCOPE_UNIVERSE &&
684 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
687 if (dst.s_addr != INADDR_ANY)
690 for (list = rtnl_list; list; list = list->next) {
691 struct connman_rtnl *rtnl = list->data;
693 if (rtnl->newgateway)
694 rtnl->newgateway(index, gatewaystr);
698 static void process_delroute(unsigned char family, unsigned char scope,
699 struct rtmsg *msg, int bytes)
702 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
703 char dststr[16], gatewaystr[16];
706 if (family != AF_INET)
709 extract_route(msg, bytes, &index, &dst, &gateway);
711 inet_ntop(family, &dst, dststr, sizeof(dststr));
712 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
714 __connman_ipconfig_delroute(index, family, scope, dststr, gatewaystr);
716 /* skip host specific routes */
717 if (scope != RT_SCOPE_UNIVERSE &&
718 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
721 if (dst.s_addr != INADDR_ANY)
724 for (list = rtnl_list; list; list = list->next) {
725 struct connman_rtnl *rtnl = list->data;
727 if (rtnl->delgateway)
728 rtnl->delgateway(index, gatewaystr);
732 static inline void print_ether(struct rtattr *attr, const char *name)
734 int len = (int) RTA_PAYLOAD(attr);
736 if (len == ETH_ALEN) {
737 struct ether_addr eth;
738 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
739 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
741 print(" attr %s (len %d)\n", name, len);
744 static inline void print_inet(struct rtattr *attr, const char *name,
745 unsigned char family)
747 int len = (int) RTA_PAYLOAD(attr);
749 if (family == AF_INET && len == sizeof(struct in_addr)) {
751 addr = *((struct in_addr *) RTA_DATA(attr));
752 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
754 print(" attr %s (len %d)\n", name, len);
757 static inline void print_string(struct rtattr *attr, const char *name)
759 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
760 (char *) RTA_DATA(attr));
763 static inline void print_byte(struct rtattr *attr, const char *name)
765 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
766 *((unsigned char *) RTA_DATA(attr)));
769 static inline void print_integer(struct rtattr *attr, const char *name)
771 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
772 *((int *) RTA_DATA(attr)));
775 static inline void print_attr(struct rtattr *attr, const char *name)
777 int len = (int) RTA_PAYLOAD(attr);
780 print(" attr %s (len %d)\n", name, len);
782 print(" attr %d (len %d)\n", attr->rta_type, len);
785 static void rtnl_link(struct nlmsghdr *hdr)
787 struct ifinfomsg *msg;
791 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
792 bytes = IFLA_PAYLOAD(hdr);
794 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
796 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
797 attr = RTA_NEXT(attr, bytes)) {
798 switch (attr->rta_type) {
800 print_ether(attr, "address");
803 print_ether(attr, "broadcast");
806 print_string(attr, "ifname");
809 print_integer(attr, "mtu");
812 print_attr(attr, "link");
815 print_attr(attr, "qdisc");
818 print_attr(attr, "stats");
821 print_attr(attr, "cost");
824 print_attr(attr, "priority");
827 print_attr(attr, "master");
830 print_attr(attr, "wireless");
833 print_attr(attr, "protinfo");
836 print_integer(attr, "txqlen");
839 print_attr(attr, "map");
842 print_attr(attr, "weight");
845 print_byte(attr, "operstate");
848 print_byte(attr, "linkmode");
851 print_attr(attr, NULL);
857 static void rtnl_newlink(struct nlmsghdr *hdr)
859 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
863 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
864 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
867 static void rtnl_dellink(struct nlmsghdr *hdr)
869 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
873 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
874 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
877 static void rtnl_addr(struct nlmsghdr *hdr)
879 struct ifaddrmsg *msg;
883 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
884 bytes = IFA_PAYLOAD(hdr);
886 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
888 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
889 attr = RTA_NEXT(attr, bytes)) {
890 switch (attr->rta_type) {
892 print_inet(attr, "address", msg->ifa_family);
895 print_inet(attr, "local", msg->ifa_family);
898 print_string(attr, "label");
901 print_inet(attr, "broadcast", msg->ifa_family);
904 print_attr(attr, "anycast");
907 print_attr(attr, "cacheinfo");
910 print_attr(attr, "multicast");
913 print_attr(attr, NULL);
919 static void rtnl_newaddr(struct nlmsghdr *hdr)
921 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
925 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
926 msg, IFA_PAYLOAD(hdr));
929 static void rtnl_deladdr(struct nlmsghdr *hdr)
931 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
935 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
936 msg, IFA_PAYLOAD(hdr));
939 static void rtnl_route(struct nlmsghdr *hdr)
945 msg = (struct rtmsg *) NLMSG_DATA(hdr);
946 bytes = RTM_PAYLOAD(hdr);
948 print("rtm_family %d rtm_table %d rtm_protocol %d",
949 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
950 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
951 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
953 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
954 attr = RTA_NEXT(attr, bytes)) {
955 switch (attr->rta_type) {
957 print_inet(attr, "dst", msg->rtm_family);
960 print_inet(attr, "src", msg->rtm_family);
963 print_string(attr, "iif");
966 print_integer(attr, "oif");
969 print_inet(attr, "gateway", msg->rtm_family);
972 print_attr(attr, "priority");
975 print_inet(attr, "prefsrc", msg->rtm_family);
978 print_attr(attr, "metrics");
981 print_integer(attr, "table");
984 print_attr(attr, NULL);
990 static connman_bool_t is_route_rtmsg(struct rtmsg *msg)
993 if (msg->rtm_table != RT_TABLE_MAIN)
996 if (msg->rtm_protocol != RTPROT_BOOT &&
997 msg->rtm_protocol != RTPROT_KERNEL)
1000 if (msg->rtm_type != RTN_UNICAST)
1006 static void rtnl_newroute(struct nlmsghdr *hdr)
1008 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1012 if (is_route_rtmsg(msg))
1013 process_newroute(msg->rtm_family, msg->rtm_scope,
1014 msg, RTM_PAYLOAD(hdr));
1017 static void rtnl_delroute(struct nlmsghdr *hdr)
1019 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1023 if (is_route_rtmsg(msg))
1024 process_delroute(msg->rtm_family, msg->rtm_scope,
1025 msg, RTM_PAYLOAD(hdr));
1028 static void rtnl_nd_opt_rdnss(char *interface, struct nd_opt_hdr *opt)
1030 guint32 *optint = (void *)opt;
1035 if (opt->nd_opt_len < 3)
1038 lifetime = ntohl(optint[1]);
1043 for (i = 0; i < opt->nd_opt_len / 2; i++, optint += 4) {
1044 if (inet_ntop(AF_INET6, optint, buf, sizeof(buf)))
1045 connman_resolver_append_lifetime(interface, NULL,
1050 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1052 struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1053 struct nd_opt_hdr *opt = (void *)&msg[1];
1054 int msglen = msg->nduseropt_opts_len;
1057 DBG("family %02x index %x len %04x type %02x code %02x",
1058 msg->nduseropt_family, msg->nduseropt_ifindex,
1059 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1060 msg->nduseropt_icmp_code);
1062 if (msg->nduseropt_family != AF_INET6 ||
1063 msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1064 msg->nduseropt_icmp_code != 0)
1067 interface = connman_inet_ifname(msg->nduseropt_ifindex);
1071 for (opt = (void *)&msg[1];
1072 msglen >= 2 && msglen >= opt->nd_opt_len && opt->nd_opt_len;
1073 msglen -= opt->nd_opt_len,
1074 opt = ((void *)opt) + opt->nd_opt_len*8) {
1076 DBG("nd opt type %d len %d\n",
1077 opt->nd_opt_type, opt->nd_opt_len);
1079 if (opt->nd_opt_type == 25)
1080 rtnl_nd_opt_rdnss(interface, opt);
1085 static const char *type2string(uint16_t type)
1112 case RTM_NEWNDUSEROPT:
1113 return "NEWNDUSEROPT";
1119 static GIOChannel *channel = NULL;
1121 struct rtnl_request {
1122 struct nlmsghdr hdr;
1123 struct rtgenmsg msg;
1125 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1127 static GSList *request_list = NULL;
1128 static guint32 request_seq = 0;
1130 static struct rtnl_request *find_request(guint32 seq)
1134 for (list = request_list; list; list = list->next) {
1135 struct rtnl_request *req = list->data;
1137 if (req->hdr.nlmsg_seq == seq)
1144 static int send_request(struct rtnl_request *req)
1146 struct sockaddr_nl addr;
1149 DBG("%s len %d type %d flags 0x%04x seq %d",
1150 type2string(req->hdr.nlmsg_type),
1151 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1152 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1154 sk = g_io_channel_unix_get_fd(channel);
1156 memset(&addr, 0, sizeof(addr));
1157 addr.nl_family = AF_NETLINK;
1159 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1160 (struct sockaddr *) &addr, sizeof(addr));
1163 static int queue_request(struct rtnl_request *req)
1165 request_list = g_slist_append(request_list, req);
1167 if (g_slist_length(request_list) > 1)
1170 return send_request(req);
1173 static int process_response(guint32 seq)
1175 struct rtnl_request *req;
1179 req = find_request(seq);
1181 request_list = g_slist_remove(request_list, req);
1185 req = g_slist_nth_data(request_list, 0);
1189 return send_request(req);
1192 static void rtnl_message(void *buf, size_t len)
1194 DBG("buf %p len %zd", buf, len);
1197 struct nlmsghdr *hdr = buf;
1198 struct nlmsgerr *err;
1200 if (!NLMSG_OK(hdr, len))
1203 DBG("%s len %d type %d flags 0x%04x seq %d",
1204 type2string(hdr->nlmsg_type),
1205 hdr->nlmsg_len, hdr->nlmsg_type,
1206 hdr->nlmsg_flags, hdr->nlmsg_seq);
1208 switch (hdr->nlmsg_type) {
1213 process_response(hdr->nlmsg_seq);
1216 err = NLMSG_DATA(hdr);
1217 DBG("error %d (%s)", -err->error,
1218 strerror(-err->error));
1238 case RTM_NEWNDUSEROPT:
1239 rtnl_newnduseropt(hdr);
1243 len -= hdr->nlmsg_len;
1244 buf += hdr->nlmsg_len;
1248 static gboolean netlink_event(GIOChannel *chan,
1249 GIOCondition cond, gpointer data)
1251 unsigned char buf[4096];
1255 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1258 memset(buf, 0, sizeof(buf));
1260 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
1262 if (err == G_IO_ERROR_AGAIN)
1267 rtnl_message(buf, len);
1272 static int send_getlink(void)
1274 struct rtnl_request *req;
1278 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1282 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1283 req->hdr.nlmsg_type = RTM_GETLINK;
1284 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1285 req->hdr.nlmsg_pid = 0;
1286 req->hdr.nlmsg_seq = request_seq++;
1287 req->msg.rtgen_family = AF_INET;
1289 return queue_request(req);
1292 static int send_getaddr(void)
1294 struct rtnl_request *req;
1298 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1302 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1303 req->hdr.nlmsg_type = RTM_GETADDR;
1304 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1305 req->hdr.nlmsg_pid = 0;
1306 req->hdr.nlmsg_seq = request_seq++;
1307 req->msg.rtgen_family = AF_INET;
1309 return queue_request(req);
1312 static int send_getroute(void)
1314 struct rtnl_request *req;
1318 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1322 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1323 req->hdr.nlmsg_type = RTM_GETROUTE;
1324 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1325 req->hdr.nlmsg_pid = 0;
1326 req->hdr.nlmsg_seq = request_seq++;
1327 req->msg.rtgen_family = AF_INET;
1329 return queue_request(req);
1332 static gboolean update_timeout_cb(gpointer user_data)
1334 __connman_rtnl_request_update();
1339 static void update_interval_callback(guint min)
1341 if (update_timeout > 0)
1342 g_source_remove(update_timeout);
1344 if (min < G_MAXUINT) {
1345 update_interval = min;
1346 update_timeout = g_timeout_add_seconds(update_interval,
1347 update_timeout_cb, NULL);
1350 update_interval = G_MAXUINT;
1354 static gint compare_interval(gconstpointer a, gconstpointer b)
1356 guint val_a = GPOINTER_TO_UINT(a);
1357 guint val_b = GPOINTER_TO_UINT(b);
1359 return val_a - val_b;
1362 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1369 update_list = g_slist_insert_sorted(update_list,
1370 GUINT_TO_POINTER(interval), compare_interval);
1372 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1373 if (min < update_interval) {
1374 update_interval_callback(min);
1375 __connman_rtnl_request_update();
1378 return update_interval;
1381 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1383 guint min = G_MAXUINT;
1388 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1390 if (update_list != NULL)
1391 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1393 if (min > update_interval)
1394 update_interval_callback(min);
1399 int __connman_rtnl_request_update(void)
1401 return send_getlink();
1404 int __connman_rtnl_init(void)
1406 struct sockaddr_nl addr;
1411 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1412 NULL, free_interface);
1414 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
1418 memset(&addr, 0, sizeof(addr));
1419 addr.nl_family = AF_NETLINK;
1420 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1421 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1422 (1<<(RTNLGRP_ND_USEROPT-1));
1424 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1429 channel = g_io_channel_unix_new(sk);
1430 g_io_channel_set_close_on_unref(channel, TRUE);
1432 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1433 netlink_event, NULL);
1438 void __connman_rtnl_start(void)
1447 void __connman_rtnl_cleanup(void)
1453 for (list = watch_list; list; list = list->next) {
1454 struct watch_data *watch = list->data;
1456 DBG("removing watch %d", watch->id);
1462 g_slist_free(watch_list);
1465 g_slist_free(update_list);
1468 for (list = request_list; list; list = list->next) {
1469 struct rtnl_request *req = list->data;
1471 DBG("%s len %d type %d flags 0x%04x seq %d",
1472 type2string(req->hdr.nlmsg_type),
1473 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1474 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1480 g_slist_free(request_list);
1481 request_list = NULL;
1483 g_io_channel_shutdown(channel, TRUE, NULL);
1484 g_io_channel_unref(channel);
1488 g_hash_table_destroy(interface_list);