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;
180 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
181 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
190 /* We haven't got a DEVTYPE, let's check if it's a wireless device */
191 if (wext_interface(interface->name)) {
192 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
193 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
195 connman_error("%s runs an unsupported 802.11 driver",
200 enum connman_device_type __connman_rtnl_get_device_type(int index)
202 struct interface_data *interface;
204 interface = g_hash_table_lookup(interface_list,
205 GINT_TO_POINTER(index));
206 if (interface == NULL)
207 return CONNMAN_DEVICE_TYPE_UNKNOWN;
209 return interface->device_type;
213 * connman_rtnl_add_newlink_watch:
214 * @index: network device index
215 * @callback: callback function
216 * @user_data: callback data;
218 * Add a new RTNL watch for newlink events
220 * Returns: %0 on failure and a unique id on success
222 unsigned int connman_rtnl_add_newlink_watch(int index,
223 connman_rtnl_link_cb_t callback, void *user_data)
225 struct watch_data *watch;
227 watch = g_try_new0(struct watch_data, 1);
231 watch->id = ++watch_id;
232 watch->index = index;
234 watch->newlink = callback;
235 watch->user_data = user_data;
237 watch_list = g_slist_prepend(watch_list, watch);
239 DBG("id %d", watch->id);
242 unsigned int flags = __connman_ipconfig_get_flags(index);
245 callback(flags, 0, user_data);
252 * connman_rtnl_remove_watch:
253 * @id: watch identifier
255 * Remove the RTNL watch for the identifier
257 void connman_rtnl_remove_watch(unsigned int id)
266 for (list = watch_list; list; list = list->next) {
267 struct watch_data *watch = list->data;
269 if (watch->id == id) {
270 watch_list = g_slist_remove(watch_list, watch);
277 static void trigger_rtnl(int index, void *user_data)
279 struct connman_rtnl *rtnl = user_data;
282 unsigned short type = __connman_ipconfig_get_type(index);
283 unsigned int flags = __connman_ipconfig_get_flags(index);
285 rtnl->newlink(type, index, flags, 0);
288 if (rtnl->newgateway) {
289 const char *gateway = __connman_ipconfig_get_gateway(index);
292 rtnl->newgateway(index, gateway);
296 static GSList *rtnl_list = NULL;
298 static gint compare_priority(gconstpointer a, gconstpointer b)
300 const struct connman_rtnl *rtnl1 = a;
301 const struct connman_rtnl *rtnl2 = b;
303 return rtnl2->priority - rtnl1->priority;
307 * connman_rtnl_register:
310 * Register a new RTNL module
312 * Returns: %0 on success
314 int connman_rtnl_register(struct connman_rtnl *rtnl)
316 DBG("rtnl %p name %s", rtnl, rtnl->name);
318 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
321 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
327 * connman_rtnl_unregister:
330 * Remove a previously registered RTNL module
332 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
334 DBG("rtnl %p name %s", rtnl, rtnl->name);
336 rtnl_list = g_slist_remove(rtnl_list, rtnl);
339 static const char *operstate2str(unsigned char operstate)
342 case IF_OPER_UNKNOWN:
344 case IF_OPER_NOTPRESENT:
345 return "NOT-PRESENT";
348 case IF_OPER_LOWERLAYERDOWN:
349 return "LOWER-LAYER-DOWN";
350 case IF_OPER_TESTING:
352 case IF_OPER_DORMANT:
361 static void extract_link(struct ifinfomsg *msg, int bytes,
362 struct ether_addr *address, const char **ifname,
363 unsigned int *mtu, unsigned char *operstate,
364 struct rtnl_link_stats *stats)
368 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
369 attr = RTA_NEXT(attr, bytes)) {
370 switch (attr->rta_type) {
373 memcpy(address, RTA_DATA(attr), ETH_ALEN);
377 *ifname = RTA_DATA(attr);
381 *mtu = *((unsigned int *) RTA_DATA(attr));
385 memcpy(stats, RTA_DATA(attr),
386 sizeof(struct rtnl_link_stats));
389 if (operstate != NULL)
390 *operstate = *((unsigned char *) RTA_DATA(attr));
398 static void process_newlink(unsigned short type, int index, unsigned flags,
399 unsigned change, struct ifinfomsg *msg, int bytes)
401 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
402 struct ether_addr compare = {{ 0, 0, 0, 0, 0, 0 }};
403 struct rtnl_link_stats stats;
404 unsigned char operstate = 0xff;
405 const char *ifname = NULL;
406 unsigned int mtu = 0;
407 char ident[13], str[18];
410 memset(&stats, 0, sizeof(stats));
411 extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats);
413 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
414 address.ether_addr_octet[0],
415 address.ether_addr_octet[1],
416 address.ether_addr_octet[2],
417 address.ether_addr_octet[3],
418 address.ether_addr_octet[4],
419 address.ether_addr_octet[5]);
421 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
422 address.ether_addr_octet[0],
423 address.ether_addr_octet[1],
424 address.ether_addr_octet[2],
425 address.ether_addr_octet[3],
426 address.ether_addr_octet[4],
427 address.ether_addr_octet[5]);
431 case ARPHRD_LOOPBACK:
432 case ARPHDR_PHONET_PIPE:
434 __connman_ipconfig_newlink(index, type, flags,
439 if (memcmp(&address, &compare, ETH_ALEN) != 0)
440 connman_info("%s {newlink} index %d address %s mtu %u",
441 ifname, index, str, mtu);
443 if (operstate != 0xff)
444 connman_info("%s {newlink} index %d operstate %u <%s>",
445 ifname, index, operstate,
446 operstate2str(operstate));
448 if (g_hash_table_lookup(interface_list,
449 GINT_TO_POINTER(index)) == NULL) {
450 struct interface_data *interface;
452 interface = g_new0(struct interface_data, 1);
453 interface->index = index;
454 interface->name = g_strdup(ifname);
455 interface->ident = g_strdup(ident);
457 g_hash_table_insert(interface_list,
458 GINT_TO_POINTER(index), interface);
460 if (type == ARPHRD_ETHER)
461 read_uevent(interface);
463 __connman_technology_add_interface(interface->service_type,
464 interface->index, interface->name, interface->ident);
467 for (list = rtnl_list; list; list = list->next) {
468 struct connman_rtnl *rtnl = list->data;
471 rtnl->newlink(type, index, flags, change);
474 for (list = watch_list; list; list = list->next) {
475 struct watch_data *watch = list->data;
477 if (watch->index != index)
481 watch->newlink(flags, change, watch->user_data);
485 static void process_dellink(unsigned short type, int index, unsigned flags,
486 unsigned change, struct ifinfomsg *msg, int bytes)
488 struct rtnl_link_stats stats;
489 unsigned char operstate = 0xff;
490 const char *ifname = NULL;
493 memset(&stats, 0, sizeof(stats));
494 extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats);
496 if (operstate != 0xff)
497 connman_info("%s {dellink} index %d operstate %u <%s>",
498 ifname, index, operstate,
499 operstate2str(operstate));
501 for (list = rtnl_list; list; list = list->next) {
502 struct connman_rtnl *rtnl = list->data;
505 rtnl->dellink(type, index, flags, change);
510 case ARPHRD_LOOPBACK:
512 __connman_ipconfig_dellink(index, &stats);
516 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
519 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
521 struct in_addr *local,
522 struct in_addr *address,
523 struct in_addr *broadcast)
527 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
528 attr = RTA_NEXT(attr, bytes)) {
529 switch (attr->rta_type) {
532 *address = *((struct in_addr *) RTA_DATA(attr));
536 *local = *((struct in_addr *) RTA_DATA(attr));
539 if (broadcast != NULL)
540 *broadcast = *((struct in_addr *) RTA_DATA(attr));
544 *label = RTA_DATA(attr);
550 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
551 struct in6_addr *addr,
552 struct in6_addr *local)
556 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
557 attr = RTA_NEXT(attr, bytes)) {
558 switch (attr->rta_type) {
561 *addr = *((struct in6_addr *) RTA_DATA(attr));
565 *local = *((struct in6_addr *) RTA_DATA(attr));
571 static void process_newaddr(unsigned char family, unsigned char prefixlen,
572 int index, struct ifaddrmsg *msg, int bytes)
574 const char *label = NULL;
576 char ip_string[INET6_ADDRSTRLEN];
578 if (family != AF_INET && family != AF_INET6)
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))
596 if (inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN) == NULL)
599 __connman_ipconfig_newaddr(index, family, label,
600 prefixlen, ip_string);
603 static void process_deladdr(unsigned char family, unsigned char prefixlen,
604 int index, struct ifaddrmsg *msg, int bytes)
606 const char *label = NULL;
608 char ip_string[INET6_ADDRSTRLEN];
610 if (family != AF_INET && family != AF_INET6)
613 if (family == AF_INET) {
614 struct in_addr ipv4_addr = { INADDR_ANY };
616 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
618 } else if (family == AF_INET6) {
619 struct in6_addr ipv6_address, ipv6_local;
621 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
622 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
628 if (inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN) == NULL)
631 __connman_ipconfig_deladdr(index, family, label,
632 prefixlen, ip_string);
635 static void extract_route(struct rtmsg *msg, int bytes, int *index,
637 struct in_addr *gateway)
641 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
642 attr = RTA_NEXT(attr, bytes)) {
643 switch (attr->rta_type) {
646 *dst = *((struct in_addr *) RTA_DATA(attr));
650 *gateway = *((struct in_addr *) RTA_DATA(attr));
654 *index = *((int *) RTA_DATA(attr));
660 static void process_newroute(unsigned char family, unsigned char scope,
661 struct rtmsg *msg, int bytes)
664 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
665 char dststr[16], gatewaystr[16];
668 if (family != AF_INET)
671 extract_route(msg, bytes, &index, &dst, &gateway);
673 inet_ntop(family, &dst, dststr, sizeof(dststr));
674 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
676 __connman_ipconfig_newroute(index, family, scope, dststr, gatewaystr);
678 /* skip host specific routes */
679 if (scope != RT_SCOPE_UNIVERSE &&
680 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
683 if (dst.s_addr != INADDR_ANY)
686 for (list = rtnl_list; list; list = list->next) {
687 struct connman_rtnl *rtnl = list->data;
689 if (rtnl->newgateway)
690 rtnl->newgateway(index, gatewaystr);
694 static void process_delroute(unsigned char family, unsigned char scope,
695 struct rtmsg *msg, int bytes)
698 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
699 char dststr[16], gatewaystr[16];
702 if (family != AF_INET)
705 extract_route(msg, bytes, &index, &dst, &gateway);
707 inet_ntop(family, &dst, dststr, sizeof(dststr));
708 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
710 __connman_ipconfig_delroute(index, family, scope, dststr, gatewaystr);
712 /* skip host specific routes */
713 if (scope != RT_SCOPE_UNIVERSE &&
714 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
717 if (dst.s_addr != INADDR_ANY)
720 for (list = rtnl_list; list; list = list->next) {
721 struct connman_rtnl *rtnl = list->data;
723 if (rtnl->delgateway)
724 rtnl->delgateway(index, gatewaystr);
728 static inline void print_ether(struct rtattr *attr, const char *name)
730 int len = (int) RTA_PAYLOAD(attr);
732 if (len == ETH_ALEN) {
733 struct ether_addr eth;
734 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
735 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
737 print(" attr %s (len %d)\n", name, len);
740 static inline void print_inet(struct rtattr *attr, const char *name,
741 unsigned char family)
743 int len = (int) RTA_PAYLOAD(attr);
745 if (family == AF_INET && len == sizeof(struct in_addr)) {
747 addr = *((struct in_addr *) RTA_DATA(attr));
748 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
750 print(" attr %s (len %d)\n", name, len);
753 static inline void print_string(struct rtattr *attr, const char *name)
755 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
756 (char *) RTA_DATA(attr));
759 static inline void print_byte(struct rtattr *attr, const char *name)
761 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
762 *((unsigned char *) RTA_DATA(attr)));
765 static inline void print_integer(struct rtattr *attr, const char *name)
767 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
768 *((int *) RTA_DATA(attr)));
771 static inline void print_attr(struct rtattr *attr, const char *name)
773 int len = (int) RTA_PAYLOAD(attr);
776 print(" attr %s (len %d)\n", name, len);
778 print(" attr %d (len %d)\n", attr->rta_type, len);
781 static void rtnl_link(struct nlmsghdr *hdr)
783 struct ifinfomsg *msg;
787 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
788 bytes = IFLA_PAYLOAD(hdr);
790 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
792 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
793 attr = RTA_NEXT(attr, bytes)) {
794 switch (attr->rta_type) {
796 print_ether(attr, "address");
799 print_ether(attr, "broadcast");
802 print_string(attr, "ifname");
805 print_integer(attr, "mtu");
808 print_attr(attr, "link");
811 print_attr(attr, "qdisc");
814 print_attr(attr, "stats");
817 print_attr(attr, "cost");
820 print_attr(attr, "priority");
823 print_attr(attr, "master");
826 print_attr(attr, "wireless");
829 print_attr(attr, "protinfo");
832 print_integer(attr, "txqlen");
835 print_attr(attr, "map");
838 print_attr(attr, "weight");
841 print_byte(attr, "operstate");
844 print_byte(attr, "linkmode");
847 print_attr(attr, NULL);
853 static void rtnl_newlink(struct nlmsghdr *hdr)
855 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
859 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
860 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
863 static void rtnl_dellink(struct nlmsghdr *hdr)
865 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
869 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
870 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
873 static void rtnl_addr(struct nlmsghdr *hdr)
875 struct ifaddrmsg *msg;
879 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
880 bytes = IFA_PAYLOAD(hdr);
882 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
884 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
885 attr = RTA_NEXT(attr, bytes)) {
886 switch (attr->rta_type) {
888 print_inet(attr, "address", msg->ifa_family);
891 print_inet(attr, "local", msg->ifa_family);
894 print_string(attr, "label");
897 print_inet(attr, "broadcast", msg->ifa_family);
900 print_attr(attr, "anycast");
903 print_attr(attr, "cacheinfo");
906 print_attr(attr, "multicast");
909 print_attr(attr, NULL);
915 static void rtnl_newaddr(struct nlmsghdr *hdr)
917 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
921 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
922 msg, IFA_PAYLOAD(hdr));
925 static void rtnl_deladdr(struct nlmsghdr *hdr)
927 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
931 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
932 msg, IFA_PAYLOAD(hdr));
935 static void rtnl_route(struct nlmsghdr *hdr)
941 msg = (struct rtmsg *) NLMSG_DATA(hdr);
942 bytes = RTM_PAYLOAD(hdr);
944 print("rtm_family %d rtm_table %d rtm_protocol %d",
945 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
946 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
947 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
949 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
950 attr = RTA_NEXT(attr, bytes)) {
951 switch (attr->rta_type) {
953 print_inet(attr, "dst", msg->rtm_family);
956 print_inet(attr, "src", msg->rtm_family);
959 print_string(attr, "iif");
962 print_integer(attr, "oif");
965 print_inet(attr, "gateway", msg->rtm_family);
968 print_attr(attr, "priority");
971 print_inet(attr, "prefsrc", msg->rtm_family);
974 print_attr(attr, "metrics");
977 print_integer(attr, "table");
980 print_attr(attr, NULL);
986 static connman_bool_t is_route_rtmsg(struct rtmsg *msg)
989 if (msg->rtm_table != RT_TABLE_MAIN)
992 if (msg->rtm_protocol != RTPROT_BOOT &&
993 msg->rtm_protocol != RTPROT_KERNEL)
996 if (msg->rtm_type != RTN_UNICAST)
1002 static void rtnl_newroute(struct nlmsghdr *hdr)
1004 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1008 if (is_route_rtmsg(msg))
1009 process_newroute(msg->rtm_family, msg->rtm_scope,
1010 msg, RTM_PAYLOAD(hdr));
1013 static void rtnl_delroute(struct nlmsghdr *hdr)
1015 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1019 if (is_route_rtmsg(msg))
1020 process_delroute(msg->rtm_family, msg->rtm_scope,
1021 msg, RTM_PAYLOAD(hdr));
1024 static void rtnl_nd_opt_rdnss(char *interface, struct nd_opt_hdr *opt)
1026 guint32 *optint = (void *)opt;
1031 if (opt->nd_opt_len < 3)
1034 lifetime = ntohl(optint[1]);
1039 for (i = 0; i < opt->nd_opt_len / 2; i++, optint += 4) {
1040 if (inet_ntop(AF_INET6, optint, buf, sizeof(buf)))
1041 connman_resolver_append_lifetime(interface, NULL,
1046 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1048 struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1049 struct nd_opt_hdr *opt = (void *)&msg[1];
1050 int msglen = msg->nduseropt_opts_len;
1053 DBG("family %02x index %x len %04x type %02x code %02x",
1054 msg->nduseropt_family, msg->nduseropt_ifindex,
1055 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1056 msg->nduseropt_icmp_code);
1058 if (msg->nduseropt_family != AF_INET6 ||
1059 msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1060 msg->nduseropt_icmp_code != 0)
1063 interface = connman_inet_ifname(msg->nduseropt_ifindex);
1067 for (opt = (void *)&msg[1];
1068 msglen >= 2 && msglen >= opt->nd_opt_len && opt->nd_opt_len;
1069 msglen -= opt->nd_opt_len,
1070 opt = ((void *)opt) + opt->nd_opt_len*8) {
1072 DBG("nd opt type %d len %d\n",
1073 opt->nd_opt_type, opt->nd_opt_len);
1075 if (opt->nd_opt_type == 25)
1076 rtnl_nd_opt_rdnss(interface, opt);
1081 static const char *type2string(uint16_t type)
1108 case RTM_NEWNDUSEROPT:
1109 return "NEWNDUSEROPT";
1115 static GIOChannel *channel = NULL;
1117 struct rtnl_request {
1118 struct nlmsghdr hdr;
1119 struct rtgenmsg msg;
1121 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1123 static GSList *request_list = NULL;
1124 static guint32 request_seq = 0;
1126 static struct rtnl_request *find_request(guint32 seq)
1130 for (list = request_list; list; list = list->next) {
1131 struct rtnl_request *req = list->data;
1133 if (req->hdr.nlmsg_seq == seq)
1140 static int send_request(struct rtnl_request *req)
1142 struct sockaddr_nl addr;
1145 DBG("%s len %d type %d flags 0x%04x seq %d",
1146 type2string(req->hdr.nlmsg_type),
1147 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1148 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1150 sk = g_io_channel_unix_get_fd(channel);
1152 memset(&addr, 0, sizeof(addr));
1153 addr.nl_family = AF_NETLINK;
1155 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1156 (struct sockaddr *) &addr, sizeof(addr));
1159 static int queue_request(struct rtnl_request *req)
1161 request_list = g_slist_append(request_list, req);
1163 if (g_slist_length(request_list) > 1)
1166 return send_request(req);
1169 static int process_response(guint32 seq)
1171 struct rtnl_request *req;
1175 req = find_request(seq);
1177 request_list = g_slist_remove(request_list, req);
1181 req = g_slist_nth_data(request_list, 0);
1185 return send_request(req);
1188 static void rtnl_message(void *buf, size_t len)
1190 DBG("buf %p len %zd", buf, len);
1193 struct nlmsghdr *hdr = buf;
1194 struct nlmsgerr *err;
1196 if (!NLMSG_OK(hdr, len))
1199 DBG("%s len %d type %d flags 0x%04x seq %d",
1200 type2string(hdr->nlmsg_type),
1201 hdr->nlmsg_len, hdr->nlmsg_type,
1202 hdr->nlmsg_flags, hdr->nlmsg_seq);
1204 switch (hdr->nlmsg_type) {
1209 process_response(hdr->nlmsg_seq);
1212 err = NLMSG_DATA(hdr);
1213 DBG("error %d (%s)", -err->error,
1214 strerror(-err->error));
1234 case RTM_NEWNDUSEROPT:
1235 rtnl_newnduseropt(hdr);
1239 len -= hdr->nlmsg_len;
1240 buf += hdr->nlmsg_len;
1244 static gboolean netlink_event(GIOChannel *chan,
1245 GIOCondition cond, gpointer data)
1247 unsigned char buf[4096];
1251 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1254 memset(buf, 0, sizeof(buf));
1256 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
1258 if (err == G_IO_ERROR_AGAIN)
1263 rtnl_message(buf, len);
1268 static int send_getlink(void)
1270 struct rtnl_request *req;
1274 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1278 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1279 req->hdr.nlmsg_type = RTM_GETLINK;
1280 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1281 req->hdr.nlmsg_pid = 0;
1282 req->hdr.nlmsg_seq = request_seq++;
1283 req->msg.rtgen_family = AF_INET;
1285 return queue_request(req);
1288 static int send_getaddr(void)
1290 struct rtnl_request *req;
1294 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1298 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1299 req->hdr.nlmsg_type = RTM_GETADDR;
1300 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1301 req->hdr.nlmsg_pid = 0;
1302 req->hdr.nlmsg_seq = request_seq++;
1303 req->msg.rtgen_family = AF_INET;
1305 return queue_request(req);
1308 static int send_getroute(void)
1310 struct rtnl_request *req;
1314 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1318 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1319 req->hdr.nlmsg_type = RTM_GETROUTE;
1320 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1321 req->hdr.nlmsg_pid = 0;
1322 req->hdr.nlmsg_seq = request_seq++;
1323 req->msg.rtgen_family = AF_INET;
1325 return queue_request(req);
1328 static gboolean update_timeout_cb(gpointer user_data)
1330 __connman_rtnl_request_update();
1335 static void update_interval_callback(guint min)
1337 if (update_timeout > 0)
1338 g_source_remove(update_timeout);
1340 if (min < G_MAXUINT) {
1341 update_interval = min;
1342 update_timeout = g_timeout_add_seconds(update_interval,
1343 update_timeout_cb, NULL);
1346 update_interval = G_MAXUINT;
1350 static gint compare_interval(gconstpointer a, gconstpointer b)
1352 guint val_a = GPOINTER_TO_UINT(a);
1353 guint val_b = GPOINTER_TO_UINT(b);
1355 return val_a - val_b;
1358 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1365 update_list = g_slist_insert_sorted(update_list,
1366 GUINT_TO_POINTER(interval), compare_interval);
1368 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1369 if (min < update_interval) {
1370 update_interval_callback(min);
1371 __connman_rtnl_request_update();
1374 return update_interval;
1377 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1379 guint min = G_MAXUINT;
1384 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1386 if (update_list != NULL)
1387 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1389 if (min > update_interval)
1390 update_interval_callback(min);
1395 int __connman_rtnl_request_update(void)
1397 return send_getlink();
1400 int __connman_rtnl_init(void)
1402 struct sockaddr_nl addr;
1407 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1408 NULL, free_interface);
1410 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
1414 memset(&addr, 0, sizeof(addr));
1415 addr.nl_family = AF_NETLINK;
1416 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1417 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1418 (1<<(RTNLGRP_ND_USEROPT-1));
1420 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1425 channel = g_io_channel_unix_new(sk);
1426 g_io_channel_set_close_on_unref(channel, TRUE);
1428 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1429 netlink_event, NULL);
1434 void __connman_rtnl_start(void)
1443 void __connman_rtnl_cleanup(void)
1449 for (list = watch_list; list; list = list->next) {
1450 struct watch_data *watch = list->data;
1452 DBG("removing watch %d", watch->id);
1458 g_slist_free(watch_list);
1461 g_slist_free(update_list);
1464 for (list = request_list; list; list = list->next) {
1465 struct rtnl_request *req = list->data;
1467 DBG("%s len %d type %d flags 0x%04x seq %d",
1468 type2string(req->hdr.nlmsg_type),
1469 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1470 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1476 g_slist_free(request_list);
1477 request_list = NULL;
1479 g_io_channel_shutdown(channel, TRUE, NULL);
1480 g_io_channel_unref(channel);
1484 g_hash_table_destroy(interface_list);