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)
50 #ifndef ARPHDR_RMNET_USB
51 #define ARPHDR_RMNET_USB (530)
55 #define print(arg...) do { if (0) connman_info(arg); } while (0)
56 //#define print(arg...) connman_info(arg)
61 connman_rtnl_link_cb_t newlink;
65 static GSList *watch_list = NULL;
66 static unsigned int watch_id = 0;
68 static GSList *update_list = NULL;
69 static guint update_interval = G_MAXUINT;
70 static guint update_timeout = 0;
72 struct interface_data {
76 enum connman_service_type service_type;
77 enum connman_device_type device_type;
80 static GHashTable *interface_list = NULL;
82 static void free_interface(gpointer data)
84 struct interface_data *interface = data;
86 __connman_technology_remove_interface(interface->service_type,
87 interface->index, interface->name, interface->ident);
89 g_free(interface->ident);
90 g_free(interface->name);
94 static connman_bool_t ether_blacklisted(const char *name)
99 if (__connman_device_isfiltered(name) == TRUE)
105 static connman_bool_t wext_interface(char *ifname)
110 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
114 memset(&wrq, 0, sizeof(wrq));
115 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
117 err = ioctl(fd, SIOCGIWNAME, &wrq);
127 static void read_uevent(struct interface_data *interface)
129 char *filename, line[128];
130 connman_bool_t found_devtype;
133 if (ether_blacklisted(interface->name) == TRUE) {
134 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
135 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
137 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
138 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
141 filename = g_strdup_printf("/sys/class/net/%s/uevent",
144 f = fopen(filename, "re");
151 found_devtype = FALSE;
152 while (fgets(line, sizeof(line), f)) {
155 pos = strchr(line, '\n');
160 if (strncmp(line, "DEVTYPE=", 8) != 0)
163 found_devtype = TRUE;
165 if (strcmp(line + 8, "wlan") == 0) {
166 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
167 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
168 } else if (strcmp(line + 8, "wwan") == 0) {
169 interface->service_type = CONNMAN_SERVICE_TYPE_CELLULAR;
170 interface->device_type = CONNMAN_DEVICE_TYPE_CELLULAR;
171 } else if (strcmp(line + 8, "bluetooth") == 0) {
172 interface->service_type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
173 interface->device_type = CONNMAN_DEVICE_TYPE_BLUETOOTH;
174 } else if (strcmp(line + 8, "wimax") == 0) {
175 interface->service_type = CONNMAN_SERVICE_TYPE_WIMAX;
176 interface->device_type = CONNMAN_DEVICE_TYPE_WIMAX;
177 } else if (strcmp(line + 8, "gadget") == 0) {
178 interface->service_type = CONNMAN_SERVICE_TYPE_GADGET;
179 interface->device_type = CONNMAN_DEVICE_TYPE_GADGET;
182 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
183 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
192 /* We haven't got a DEVTYPE, let's check if it's a wireless device */
193 if (wext_interface(interface->name)) {
194 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
195 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
197 connman_error("%s runs an unsupported 802.11 driver",
202 enum connman_device_type __connman_rtnl_get_device_type(int index)
204 struct interface_data *interface;
206 interface = g_hash_table_lookup(interface_list,
207 GINT_TO_POINTER(index));
208 if (interface == NULL)
209 return CONNMAN_DEVICE_TYPE_UNKNOWN;
211 return interface->device_type;
215 * connman_rtnl_add_newlink_watch:
216 * @index: network device index
217 * @callback: callback function
218 * @user_data: callback data;
220 * Add a new RTNL watch for newlink events
222 * Returns: %0 on failure and a unique id on success
224 unsigned int connman_rtnl_add_newlink_watch(int index,
225 connman_rtnl_link_cb_t callback, void *user_data)
227 struct watch_data *watch;
229 watch = g_try_new0(struct watch_data, 1);
233 watch->id = ++watch_id;
234 watch->index = index;
236 watch->newlink = callback;
237 watch->user_data = user_data;
239 watch_list = g_slist_prepend(watch_list, watch);
241 DBG("id %d", watch->id);
244 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
247 callback(flags, 0, user_data);
254 * connman_rtnl_remove_watch:
255 * @id: watch identifier
257 * Remove the RTNL watch for the identifier
259 void connman_rtnl_remove_watch(unsigned int id)
268 for (list = watch_list; list; list = list->next) {
269 struct watch_data *watch = list->data;
271 if (watch->id == id) {
272 watch_list = g_slist_remove(watch_list, watch);
279 static void trigger_rtnl(int index, void *user_data)
281 struct connman_rtnl *rtnl = user_data;
284 unsigned short type = __connman_ipconfig_get_type_from_index(index);
285 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
287 rtnl->newlink(type, index, flags, 0);
290 if (rtnl->newgateway) {
291 const char *gateway =
292 __connman_ipconfig_get_gateway_from_index(index,
293 CONNMAN_IPCONFIG_TYPE_ALL);
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 #if defined TIZEN_EXT
441 case ARPHDR_RMNET_USB:
443 __connman_ipconfig_newlink(index, type, flags,
448 if (memcmp(&address, &compare, ETH_ALEN) != 0)
449 connman_info("%s {newlink} index %d address %s mtu %u",
450 ifname, index, str, mtu);
452 if (operstate != 0xff)
453 connman_info("%s {newlink} index %d operstate %u <%s>",
454 ifname, index, operstate,
455 operstate2str(operstate));
457 interface = g_hash_table_lookup(interface_list, GINT_TO_POINTER(index));
458 if (interface == NULL) {
459 interface = g_new0(struct interface_data, 1);
460 interface->index = index;
461 interface->name = g_strdup(ifname);
462 interface->ident = g_strdup(ident);
464 g_hash_table_insert(interface_list,
465 GINT_TO_POINTER(index), interface);
467 if (type == ARPHRD_ETHER)
468 read_uevent(interface);
470 #if defined TIZEN_EXT
471 if (type == ARPHRD_PPP || type == ARPHDR_RMNET_USB) {
472 interface->service_type = CONNMAN_SERVICE_TYPE_CELLULAR;
473 interface->device_type = CONNMAN_DEVICE_TYPE_CELLULAR;
477 __connman_technology_add_interface(interface->service_type,
478 interface->index, interface->name, interface->ident);
481 for (list = rtnl_list; list; list = list->next) {
482 struct connman_rtnl *rtnl = list->data;
485 rtnl->newlink(type, index, flags, change);
488 for (list = watch_list; list; list = list->next) {
489 struct watch_data *watch = list->data;
491 if (watch->index != index)
495 watch->newlink(flags, change, watch->user_data);
499 static void process_dellink(unsigned short type, int index, unsigned flags,
500 unsigned change, struct ifinfomsg *msg, int bytes)
502 struct rtnl_link_stats stats;
503 unsigned char operstate = 0xff;
504 const char *ifname = NULL;
507 memset(&stats, 0, sizeof(stats));
508 extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats);
510 if (operstate != 0xff)
511 connman_info("%s {dellink} index %d operstate %u <%s>",
512 ifname, index, operstate,
513 operstate2str(operstate));
515 for (list = rtnl_list; list; list = list->next) {
516 struct connman_rtnl *rtnl = list->data;
519 rtnl->dellink(type, index, flags, change);
524 case ARPHRD_LOOPBACK:
526 #if defined TIZEN_EXT
528 case ARPHDR_RMNET_USB:
529 DBG("interface index(%d)", index);
531 __connman_ipconfig_dellink(index, &stats);
535 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
538 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
540 struct in_addr *local,
541 struct in_addr *address,
542 struct in_addr *broadcast)
546 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
547 attr = RTA_NEXT(attr, bytes)) {
548 switch (attr->rta_type) {
551 *address = *((struct in_addr *) RTA_DATA(attr));
555 *local = *((struct in_addr *) RTA_DATA(attr));
558 if (broadcast != NULL)
559 *broadcast = *((struct in_addr *) RTA_DATA(attr));
563 *label = RTA_DATA(attr);
569 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
570 struct in6_addr *addr,
571 struct in6_addr *local)
575 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
576 attr = RTA_NEXT(attr, bytes)) {
577 switch (attr->rta_type) {
580 *addr = *((struct in6_addr *) RTA_DATA(attr));
584 *local = *((struct in6_addr *) RTA_DATA(attr));
590 static void process_newaddr(unsigned char family, unsigned char prefixlen,
591 int index, struct ifaddrmsg *msg, int bytes)
593 const char *label = NULL;
595 char ip_string[INET6_ADDRSTRLEN];
597 if (family == AF_INET) {
598 struct in_addr ipv4_addr = { INADDR_ANY };
600 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
602 } else if (family == AF_INET6) {
603 struct in6_addr ipv6_address, ipv6_local;
605 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
606 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
614 if (inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN) == NULL)
617 __connman_ipconfig_newaddr(index, family, label,
618 prefixlen, ip_string);
620 if (family == AF_INET6) {
622 * Re-create RDNSS configured servers if there are any
623 * for this interface. This is done because we might
624 * have now properly configured interface with proper
625 * autoconfigured address.
627 char *interface = connman_inet_ifname(index);
629 __connman_resolver_redo_servers(interface);
635 static void process_deladdr(unsigned char family, unsigned char prefixlen,
636 int index, struct ifaddrmsg *msg, int bytes)
638 const char *label = NULL;
640 char ip_string[INET6_ADDRSTRLEN];
642 if (family == AF_INET) {
643 struct in_addr ipv4_addr = { INADDR_ANY };
645 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
647 } else if (family == AF_INET6) {
648 struct in6_addr ipv6_address, ipv6_local;
650 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
651 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
659 if (inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN) == NULL)
662 __connman_ipconfig_deladdr(index, family, label,
663 prefixlen, ip_string);
666 static void extract_ipv4_route(struct rtmsg *msg, int bytes, int *index,
668 struct in_addr *gateway)
672 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
673 attr = RTA_NEXT(attr, bytes)) {
674 switch (attr->rta_type) {
677 *dst = *((struct in_addr *) RTA_DATA(attr));
681 *gateway = *((struct in_addr *) RTA_DATA(attr));
685 *index = *((int *) RTA_DATA(attr));
691 static void extract_ipv6_route(struct rtmsg *msg, int bytes, int *index,
692 struct in6_addr *dst,
693 struct in6_addr *gateway)
697 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
698 attr = RTA_NEXT(attr, bytes)) {
699 switch (attr->rta_type) {
702 *dst = *((struct in6_addr *) RTA_DATA(attr));
707 *((struct in6_addr *) RTA_DATA(attr));
711 *index = *((int *) RTA_DATA(attr));
717 static void process_newroute(unsigned char family, unsigned char scope,
718 struct rtmsg *msg, int bytes)
721 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
724 if (family == AF_INET) {
725 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
727 extract_ipv4_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 && dst.s_addr == INADDR_ANY))
740 if (dst.s_addr != INADDR_ANY)
743 } else if (family == AF_INET6) {
744 struct in6_addr dst = IN6ADDR_ANY_INIT,
745 gateway = IN6ADDR_ANY_INIT;
747 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
749 inet_ntop(family, &dst, dststr, sizeof(dststr));
750 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
752 __connman_ipconfig_newroute(index, family, scope, dststr,
755 /* skip host specific routes */
756 if (scope != RT_SCOPE_UNIVERSE &&
757 !(scope == RT_SCOPE_LINK &&
758 IN6_IS_ADDR_UNSPECIFIED(&dst)))
761 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
766 for (list = rtnl_list; list; list = list->next) {
767 struct connman_rtnl *rtnl = list->data;
769 if (rtnl->newgateway)
770 rtnl->newgateway(index, gatewaystr);
774 static void process_delroute(unsigned char family, unsigned char scope,
775 struct rtmsg *msg, int bytes)
778 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
781 if (family == AF_INET) {
782 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
784 extract_ipv4_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 && dst.s_addr == INADDR_ANY))
797 if (dst.s_addr != INADDR_ANY)
800 } else if (family == AF_INET6) {
801 struct in6_addr dst = IN6ADDR_ANY_INIT,
802 gateway = IN6ADDR_ANY_INIT;
804 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
806 inet_ntop(family, &dst, dststr, sizeof(dststr));
807 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
809 __connman_ipconfig_delroute(index, family, scope, dststr,
812 /* skip host specific routes */
813 if (scope != RT_SCOPE_UNIVERSE &&
814 !(scope == RT_SCOPE_LINK &&
815 IN6_IS_ADDR_UNSPECIFIED(&dst)))
818 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
823 for (list = rtnl_list; list; list = list->next) {
824 struct connman_rtnl *rtnl = list->data;
826 if (rtnl->delgateway)
827 rtnl->delgateway(index, gatewaystr);
831 static inline void print_ether(struct rtattr *attr, const char *name)
833 int len = (int) RTA_PAYLOAD(attr);
835 if (len == ETH_ALEN) {
836 struct ether_addr eth;
837 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
838 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
840 print(" attr %s (len %d)\n", name, len);
843 static inline void print_inet(struct rtattr *attr, const char *name,
844 unsigned char family)
846 int len = (int) RTA_PAYLOAD(attr);
848 if (family == AF_INET && len == sizeof(struct in_addr)) {
850 addr = *((struct in_addr *) RTA_DATA(attr));
851 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
853 print(" attr %s (len %d)\n", name, len);
856 static inline void print_string(struct rtattr *attr, const char *name)
858 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
859 (char *) RTA_DATA(attr));
862 static inline void print_byte(struct rtattr *attr, const char *name)
864 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
865 *((unsigned char *) RTA_DATA(attr)));
868 static inline void print_integer(struct rtattr *attr, const char *name)
870 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
871 *((int *) RTA_DATA(attr)));
874 static inline void print_attr(struct rtattr *attr, const char *name)
876 int len = (int) RTA_PAYLOAD(attr);
879 print(" attr %s (len %d)\n", name, len);
881 print(" attr %d (len %d)\n", attr->rta_type, len);
884 static void rtnl_link(struct nlmsghdr *hdr)
886 struct ifinfomsg *msg;
890 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
891 bytes = IFLA_PAYLOAD(hdr);
893 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
895 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
896 attr = RTA_NEXT(attr, bytes)) {
897 switch (attr->rta_type) {
899 print_ether(attr, "address");
902 print_ether(attr, "broadcast");
905 print_string(attr, "ifname");
908 print_integer(attr, "mtu");
911 print_attr(attr, "link");
914 print_attr(attr, "qdisc");
917 print_attr(attr, "stats");
920 print_attr(attr, "cost");
923 print_attr(attr, "priority");
926 print_attr(attr, "master");
929 print_attr(attr, "wireless");
932 print_attr(attr, "protinfo");
935 print_integer(attr, "txqlen");
938 print_attr(attr, "map");
941 print_attr(attr, "weight");
944 print_byte(attr, "operstate");
947 print_byte(attr, "linkmode");
950 print_attr(attr, NULL);
956 static void rtnl_newlink(struct nlmsghdr *hdr)
958 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
962 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
963 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
966 static void rtnl_dellink(struct nlmsghdr *hdr)
968 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
972 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
973 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
976 static void rtnl_addr(struct nlmsghdr *hdr)
978 struct ifaddrmsg *msg;
982 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
983 bytes = IFA_PAYLOAD(hdr);
985 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
987 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
988 attr = RTA_NEXT(attr, bytes)) {
989 switch (attr->rta_type) {
991 print_inet(attr, "address", msg->ifa_family);
994 print_inet(attr, "local", msg->ifa_family);
997 print_string(attr, "label");
1000 print_inet(attr, "broadcast", msg->ifa_family);
1003 print_attr(attr, "anycast");
1006 print_attr(attr, "cacheinfo");
1009 print_attr(attr, "multicast");
1012 print_attr(attr, NULL);
1018 static void rtnl_newaddr(struct nlmsghdr *hdr)
1020 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1024 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1025 msg, IFA_PAYLOAD(hdr));
1028 static void rtnl_deladdr(struct nlmsghdr *hdr)
1030 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1034 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1035 msg, IFA_PAYLOAD(hdr));
1038 static void rtnl_route(struct nlmsghdr *hdr)
1041 struct rtattr *attr;
1044 msg = (struct rtmsg *) NLMSG_DATA(hdr);
1045 bytes = RTM_PAYLOAD(hdr);
1047 print("rtm_family %d rtm_table %d rtm_protocol %d",
1048 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
1049 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
1050 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
1052 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
1053 attr = RTA_NEXT(attr, bytes)) {
1054 switch (attr->rta_type) {
1056 print_inet(attr, "dst", msg->rtm_family);
1059 print_inet(attr, "src", msg->rtm_family);
1062 print_string(attr, "iif");
1065 print_integer(attr, "oif");
1068 print_inet(attr, "gateway", msg->rtm_family);
1071 print_attr(attr, "priority");
1074 print_inet(attr, "prefsrc", msg->rtm_family);
1077 print_attr(attr, "metrics");
1080 print_integer(attr, "table");
1083 print_attr(attr, NULL);
1089 static connman_bool_t is_route_rtmsg(struct rtmsg *msg)
1092 if (msg->rtm_table != RT_TABLE_MAIN)
1095 if (msg->rtm_protocol != RTPROT_BOOT &&
1096 msg->rtm_protocol != RTPROT_KERNEL)
1099 if (msg->rtm_type != RTN_UNICAST)
1105 static void rtnl_newroute(struct nlmsghdr *hdr)
1107 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1111 if (is_route_rtmsg(msg))
1112 process_newroute(msg->rtm_family, msg->rtm_scope,
1113 msg, RTM_PAYLOAD(hdr));
1116 static void rtnl_delroute(struct nlmsghdr *hdr)
1118 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1122 if (is_route_rtmsg(msg))
1123 process_delroute(msg->rtm_family, msg->rtm_scope,
1124 msg, RTM_PAYLOAD(hdr));
1127 static void *rtnl_nd_opt_rdnss(struct nd_opt_hdr *opt, guint32 *lifetime,
1130 guint32 *optint = (void *)opt;
1132 if (opt->nd_opt_len < 3)
1135 if (*lifetime > ntohl(optint[1]))
1136 *lifetime = ntohl(optint[1]);
1138 /* nd_opt_len is in units of 8 bytes. The header is 1 unit (8 bytes)
1139 and each address is another 2 units (16 bytes).
1140 So the number of addresses (given rounding) is nd_opt_len/2 */
1141 *nr_servers = opt->nd_opt_len / 2;
1143 /* And they start 8 bytes into the packet, or two guint32s in. */
1147 static const char **rtnl_nd_opt_dnssl(struct nd_opt_hdr *opt, guint32 *lifetime)
1149 const char **domains = NULL;
1150 guint32 *optint = (void *)opt;
1151 unsigned char *optc = (void *)&optint[2];
1152 int data_len = (opt->nd_opt_len * 8) - 8;
1156 if (*lifetime > ntohl(optint[1]))
1157 *lifetime = ntohl(optint[1]);
1159 /* Turn it into normal strings by converting the length bytes into '.',
1160 and count how many search domains there are while we're at it. */
1162 while (i < data_len) {
1163 if (optc[i] > 0x3f) {
1164 DBG("DNSSL contains compressed elements in violation of RFC6106");
1171 /* Check for double zero */
1172 if (i < data_len && optc[i] == 0)
1180 if (i >= data_len) {
1181 DBG("DNSSL data overflows option length");
1188 domains = g_try_new0(const char *, nr_domains + 1);
1192 /* Now point to the normal strings, missing out the leading '.' that
1193 each of them will have now. */
1194 for (i = 0; i < nr_domains; i++) {
1195 domains[i] = (char *)optc + 1;
1196 optc += strlen((char *)optc) + 1;
1202 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1204 struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1205 struct nd_opt_hdr *opt;
1206 guint32 lifetime = -1;
1207 const char **domains = NULL;
1208 struct in6_addr *servers = NULL;
1209 int i, nr_servers = 0;
1210 int msglen = msg->nduseropt_opts_len;
1213 DBG("family %d index %d len %d type %d code %d",
1214 msg->nduseropt_family, msg->nduseropt_ifindex,
1215 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1216 msg->nduseropt_icmp_code);
1218 if (msg->nduseropt_family != AF_INET6 ||
1219 msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1220 msg->nduseropt_icmp_code != 0)
1223 interface = connman_inet_ifname(msg->nduseropt_ifindex);
1227 for (opt = (void *)&msg[1];
1229 msglen -= opt->nd_opt_len * 8,
1230 opt = ((void *)opt) + opt->nd_opt_len*8) {
1232 DBG("remaining %d nd opt type %d len %d\n",
1233 msglen, opt->nd_opt_type, opt->nd_opt_len);
1235 if (opt->nd_opt_type == 25) { /* ND_OPT_RDNSS */
1238 servers = rtnl_nd_opt_rdnss(opt, &lifetime,
1240 for (i = 0; i < nr_servers; i++) {
1241 if (!inet_ntop(AF_INET6, servers + i, buf,
1245 connman_resolver_append_lifetime(interface,
1246 NULL, buf, lifetime);
1249 } else if (opt->nd_opt_type == 31) { /* ND_OPT_DNSSL */
1252 domains = rtnl_nd_opt_dnssl(opt, &lifetime);
1253 for (i = 0; domains != NULL && domains[i] != NULL; i++)
1254 connman_resolver_append_lifetime(interface,
1255 domains[i], NULL, lifetime);
1263 static const char *type2string(uint16_t type)
1292 case RTM_NEWNDUSEROPT:
1293 return "NEWNDUSEROPT";
1299 static GIOChannel *channel = NULL;
1301 struct rtnl_request {
1302 struct nlmsghdr hdr;
1303 struct rtgenmsg msg;
1305 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1307 static GSList *request_list = NULL;
1308 static guint32 request_seq = 0;
1310 static struct rtnl_request *find_request(guint32 seq)
1314 for (list = request_list; list; list = list->next) {
1315 struct rtnl_request *req = list->data;
1317 if (req->hdr.nlmsg_seq == seq)
1324 static int send_request(struct rtnl_request *req)
1326 struct sockaddr_nl addr;
1329 DBG("%s len %d type %d flags 0x%04x seq %d",
1330 type2string(req->hdr.nlmsg_type),
1331 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1332 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1334 sk = g_io_channel_unix_get_fd(channel);
1336 memset(&addr, 0, sizeof(addr));
1337 addr.nl_family = AF_NETLINK;
1339 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1340 (struct sockaddr *) &addr, sizeof(addr));
1343 static int queue_request(struct rtnl_request *req)
1345 request_list = g_slist_append(request_list, req);
1347 if (g_slist_length(request_list) > 1)
1350 return send_request(req);
1353 static int process_response(guint32 seq)
1355 struct rtnl_request *req;
1359 req = find_request(seq);
1361 request_list = g_slist_remove(request_list, req);
1365 req = g_slist_nth_data(request_list, 0);
1369 return send_request(req);
1372 static void rtnl_message(void *buf, size_t len)
1374 DBG("buf %p len %zd", buf, len);
1377 struct nlmsghdr *hdr = buf;
1378 struct nlmsgerr *err;
1380 if (!NLMSG_OK(hdr, len))
1383 DBG("%s len %d type %d flags 0x%04x seq %d pid %d",
1384 type2string(hdr->nlmsg_type),
1385 hdr->nlmsg_len, hdr->nlmsg_type,
1386 hdr->nlmsg_flags, hdr->nlmsg_seq,
1389 switch (hdr->nlmsg_type) {
1394 process_response(hdr->nlmsg_seq);
1397 err = NLMSG_DATA(hdr);
1398 DBG("error %d (%s)", -err->error,
1399 strerror(-err->error));
1419 case RTM_NEWNDUSEROPT:
1420 rtnl_newnduseropt(hdr);
1424 len -= hdr->nlmsg_len;
1425 buf += hdr->nlmsg_len;
1429 static gboolean netlink_event(GIOChannel *chan,
1430 GIOCondition cond, gpointer data)
1432 unsigned char buf[4096];
1433 struct sockaddr_nl nladdr;
1434 socklen_t addr_len = sizeof(nladdr);
1438 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1441 memset(buf, 0, sizeof(buf));
1442 memset(&nladdr, 0, sizeof(nladdr));
1444 fd = g_io_channel_unix_get_fd(chan);
1446 status = recvfrom(fd, buf, sizeof(buf), 0,
1447 (struct sockaddr *) &nladdr, &addr_len);
1449 if (errno == EINTR || errno == EAGAIN)
1458 if (nladdr.nl_pid != 0) { /* not sent by kernel, ignore */
1459 DBG("Received msg from %u, ignoring it", nladdr.nl_pid);
1463 rtnl_message(buf, status);
1468 static int send_getlink(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_GETLINK;
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 int send_getaddr(void)
1490 struct rtnl_request *req;
1494 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1498 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1499 req->hdr.nlmsg_type = RTM_GETADDR;
1500 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1501 req->hdr.nlmsg_pid = 0;
1502 req->hdr.nlmsg_seq = request_seq++;
1503 req->msg.rtgen_family = AF_INET;
1505 return queue_request(req);
1508 static int send_getroute(void)
1510 struct rtnl_request *req;
1514 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1518 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1519 req->hdr.nlmsg_type = RTM_GETROUTE;
1520 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1521 req->hdr.nlmsg_pid = 0;
1522 req->hdr.nlmsg_seq = request_seq++;
1523 req->msg.rtgen_family = AF_INET;
1525 return queue_request(req);
1528 static gboolean update_timeout_cb(gpointer user_data)
1530 __connman_rtnl_request_update();
1535 static void update_interval_callback(guint min)
1537 if (update_timeout > 0)
1538 g_source_remove(update_timeout);
1540 if (min < G_MAXUINT) {
1541 update_interval = min;
1542 update_timeout = g_timeout_add_seconds(update_interval,
1543 update_timeout_cb, NULL);
1546 update_interval = G_MAXUINT;
1550 static gint compare_interval(gconstpointer a, gconstpointer b)
1552 guint val_a = GPOINTER_TO_UINT(a);
1553 guint val_b = GPOINTER_TO_UINT(b);
1555 return val_a - val_b;
1558 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1565 update_list = g_slist_insert_sorted(update_list,
1566 GUINT_TO_POINTER(interval), compare_interval);
1568 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1569 if (min < update_interval) {
1570 update_interval_callback(min);
1571 __connman_rtnl_request_update();
1574 return update_interval;
1577 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1579 guint min = G_MAXUINT;
1584 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1586 if (update_list != NULL)
1587 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1589 if (min > update_interval)
1590 update_interval_callback(min);
1595 int __connman_rtnl_request_update(void)
1597 return send_getlink();
1600 int __connman_rtnl_init(void)
1602 struct sockaddr_nl addr;
1607 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1608 NULL, free_interface);
1610 sk = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
1614 memset(&addr, 0, sizeof(addr));
1615 addr.nl_family = AF_NETLINK;
1616 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1617 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1618 (1<<(RTNLGRP_ND_USEROPT-1));
1620 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1625 channel = g_io_channel_unix_new(sk);
1626 g_io_channel_set_close_on_unref(channel, TRUE);
1628 g_io_channel_set_encoding(channel, NULL, NULL);
1629 g_io_channel_set_buffered(channel, FALSE);
1631 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1632 netlink_event, NULL);
1637 void __connman_rtnl_start(void)
1646 void __connman_rtnl_cleanup(void)
1652 for (list = watch_list; list; list = list->next) {
1653 struct watch_data *watch = list->data;
1655 DBG("removing watch %d", watch->id);
1661 g_slist_free(watch_list);
1664 g_slist_free(update_list);
1667 for (list = request_list; list; list = list->next) {
1668 struct rtnl_request *req = list->data;
1670 DBG("%s len %d type %d flags 0x%04x seq %d",
1671 type2string(req->hdr.nlmsg_type),
1672 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1673 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1679 g_slist_free(request_list);
1680 request_list = NULL;
1682 g_io_channel_shutdown(channel, TRUE, NULL);
1683 g_io_channel_unref(channel);
1687 g_hash_table_destroy(interface_list);