5 * Copyright (C) 2007-2013 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)
51 #define ARPHDR_RMNET (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 {
75 enum connman_service_type service_type;
76 enum connman_device_type device_type;
79 static GHashTable *interface_list = NULL;
81 static void free_interface(gpointer data)
83 struct interface_data *interface = data;
85 __connman_technology_remove_interface(interface->service_type,
86 interface->index, interface->ident);
88 g_free(interface->ident);
92 static bool ether_blacklisted(const char *name)
97 if (__connman_device_isfiltered(name))
103 #if !defined TIZEN_EXT
104 static bool wext_interface(char *ifname)
109 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
113 memset(&wrq, 0, sizeof(wrq));
114 strncpy(wrq.ifr_name, ifname, sizeof(wrq.ifr_name) - 1);
116 err = ioctl(fd, SIOCGIWNAME, &wrq);
127 #if defined TIZEN_EXT
128 static bool __connman_rtnl_is_cellular_device(const char *name)
131 char **cellular_interfaces;
133 cellular_interfaces =
134 connman_setting_get_string_list(
135 "NetworkCellularInterfaceList");
136 if (!cellular_interfaces)
139 for (pattern = cellular_interfaces; *pattern; pattern++) {
140 if (g_str_has_prefix(name, *pattern)) {
141 DBG("Cellular interface: %s", name);
150 static void read_uevent(struct interface_data *interface)
152 char *filename, *name, line[128];
156 name = connman_inet_ifname(interface->index);
158 #if defined TIZEN_EXT
159 if (__connman_rtnl_is_cellular_device(name)) {
160 interface->service_type = CONNMAN_SERVICE_TYPE_CELLULAR;
161 interface->device_type = CONNMAN_DEVICE_TYPE_CELLULAR;
167 if (ether_blacklisted(name)) {
168 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
169 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
172 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
173 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
176 filename = g_strdup_printf("/sys/class/net/%s/uevent", name);
178 f = fopen(filename, "re");
183 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
184 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
188 found_devtype = false;
189 while (fgets(line, sizeof(line), f)) {
192 pos = strchr(line, '\n');
197 if (strncmp(line, "DEVTYPE=", 8) != 0)
200 found_devtype = true;
202 if (strcmp(line + 8, "wlan") == 0) {
203 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
204 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
205 } else if (strcmp(line + 8, "wwan") == 0) {
206 interface->service_type = CONNMAN_SERVICE_TYPE_CELLULAR;
207 interface->device_type = CONNMAN_DEVICE_TYPE_CELLULAR;
208 } else if (strcmp(line + 8, "bluetooth") == 0) {
209 interface->service_type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
210 interface->device_type = CONNMAN_DEVICE_TYPE_BLUETOOTH;
211 } else if (strcmp(line + 8, "gadget") == 0) {
212 interface->service_type = CONNMAN_SERVICE_TYPE_GADGET;
213 interface->device_type = CONNMAN_DEVICE_TYPE_GADGET;
214 } else if (strcmp(line + 8, "vlan") == 0) {
215 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
216 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
217 } else if (strcmp(line + 8, "bond") == 0) {
218 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
219 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
221 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
222 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
231 #if !defined TIZEN_EXT
232 /* TIZEN does not use old wext interface */
233 /* We haven't got a DEVTYPE, let's check if it's a wireless device */
234 if (wext_interface(name)) {
235 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
236 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
238 connman_error("%s runs an unsupported 802.11 driver", name);
246 enum connman_device_type __connman_rtnl_get_device_type(int index)
248 struct interface_data *interface;
250 interface = g_hash_table_lookup(interface_list,
251 GINT_TO_POINTER(index));
253 return CONNMAN_DEVICE_TYPE_UNKNOWN;
255 return interface->device_type;
259 * connman_rtnl_add_newlink_watch:
260 * @index: network device index
261 * @callback: callback function
262 * @user_data: callback data;
264 * Add a new RTNL watch for newlink events
266 * Returns: %0 on failure and a unique id on success
268 unsigned int connman_rtnl_add_newlink_watch(int index,
269 connman_rtnl_link_cb_t callback, void *user_data)
271 struct watch_data *watch;
273 watch = g_try_new0(struct watch_data, 1);
277 watch->id = ++watch_id;
278 watch->index = index;
280 watch->newlink = callback;
281 watch->user_data = user_data;
283 watch_list = g_slist_prepend(watch_list, watch);
285 DBG("id %d", watch->id);
288 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
291 callback(flags, 0, user_data);
298 * connman_rtnl_remove_watch:
299 * @id: watch identifier
301 * Remove the RTNL watch for the identifier
303 void connman_rtnl_remove_watch(unsigned int id)
312 for (list = watch_list; list; list = list->next) {
313 struct watch_data *watch = list->data;
315 if (watch->id == id) {
316 watch_list = g_slist_remove(watch_list, watch);
323 static void trigger_rtnl(int index, void *user_data)
325 struct connman_rtnl *rtnl = user_data;
328 unsigned short type = __connman_ipconfig_get_type_from_index(index);
329 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
331 rtnl->newlink(type, index, flags, 0);
334 if (rtnl->newgateway) {
335 const char *gateway =
336 __connman_ipconfig_get_gateway_from_index(index,
337 CONNMAN_IPCONFIG_TYPE_ALL);
340 rtnl->newgateway(index, gateway);
344 static GSList *rtnl_list = NULL;
346 static gint compare_priority(gconstpointer a, gconstpointer b)
348 const struct connman_rtnl *rtnl1 = a;
349 const struct connman_rtnl *rtnl2 = b;
351 return rtnl2->priority - rtnl1->priority;
355 * connman_rtnl_register:
358 * Register a new RTNL module
360 * Returns: %0 on success
362 int connman_rtnl_register(struct connman_rtnl *rtnl)
364 DBG("rtnl %p name %s", rtnl, rtnl->name);
366 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
369 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
375 * connman_rtnl_unregister:
378 * Remove a previously registered RTNL module
380 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
382 DBG("rtnl %p name %s", rtnl, rtnl->name);
384 rtnl_list = g_slist_remove(rtnl_list, rtnl);
387 static const char *operstate2str(unsigned char operstate)
390 case IF_OPER_UNKNOWN:
392 case IF_OPER_NOTPRESENT:
393 return "NOT-PRESENT";
396 case IF_OPER_LOWERLAYERDOWN:
397 return "LOWER-LAYER-DOWN";
398 case IF_OPER_TESTING:
400 case IF_OPER_DORMANT:
409 static bool extract_link(struct ifinfomsg *msg, int bytes,
410 struct ether_addr *address, const char **ifname,
411 unsigned int *mtu, unsigned char *operstate,
412 struct rtnl_link_stats *stats)
416 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
417 attr = RTA_NEXT(attr, bytes)) {
418 switch (attr->rta_type) {
421 memcpy(address, RTA_DATA(attr), ETH_ALEN);
425 *ifname = RTA_DATA(attr);
429 *mtu = *((unsigned int *) RTA_DATA(attr));
433 memcpy(stats, RTA_DATA(attr),
434 sizeof(struct rtnl_link_stats));
438 *operstate = *((unsigned char *) RTA_DATA(attr));
450 static void process_newlink(unsigned short type, int index, unsigned flags,
451 unsigned change, struct ifinfomsg *msg, int bytes)
453 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
454 struct rtnl_link_stats stats;
455 unsigned char operstate = 0xff;
456 struct interface_data *interface;
457 const char *ifname = NULL;
458 unsigned int mtu = 0;
459 char ident[13], str[18];
462 memset(&stats, 0, sizeof(stats));
463 if (!extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats))
466 #if defined TIZEN_EXT_WIFI_MESH
467 /* Do not accept Wi-Fi Mesh interface */
468 if (g_strrstr(ifname, "mesh") != NULL) {
469 DBG("Newlink event for Wi-Fi Mesh interface ignored");
473 /* Do not accept Wi-Fi WLAN1 interface "dedicated for softAP */
474 if (!g_strcmp0(ifname, "wlan1")) {
475 DBG("Newlink event for Wi-Fi WLAN1 interface ignored");
480 #if defined TIZEN_EXT
481 /* Do not accept Wi-Fi P2P interface */
482 if (g_strrstr(ifname, "p2p") != NULL) {
483 DBG("Newlink event for Wi-Fi P2P interface ignored");
488 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
489 address.ether_addr_octet[0],
490 address.ether_addr_octet[1],
491 address.ether_addr_octet[2],
492 address.ether_addr_octet[3],
493 address.ether_addr_octet[4],
494 address.ether_addr_octet[5]);
496 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
497 address.ether_addr_octet[0],
498 address.ether_addr_octet[1],
499 address.ether_addr_octet[2],
500 address.ether_addr_octet[3],
501 address.ether_addr_octet[4],
502 address.ether_addr_octet[5]);
504 if (flags & IFF_SLAVE) {
505 connman_info("%s {newlink} ignoring slave, index %d address %s",
511 if (TIZEN_TV_EXT && g_strcmp0(ident, "eeeeeeeeeeee") == 0) {
512 DBG("Newlink event with Dummy MAC. Ignored!");
519 case ARPHRD_LOOPBACK:
520 case ARPHDR_PHONET_PIPE:
523 #if defined TIZEN_EXT
525 * Description: ARPHDR_RMNET for QC modem using QMI
529 __connman_ipconfig_newlink(index, type, flags,
534 connman_info("%s {newlink} index %d address %s mtu %u",
535 ifname, index, str, mtu);
537 if (operstate != 0xff)
538 connman_info("%s {newlink} index %d operstate %u <%s>",
539 ifname, index, operstate,
540 operstate2str(operstate));
542 interface = g_hash_table_lookup(interface_list, GINT_TO_POINTER(index));
544 interface = g_new0(struct interface_data, 1);
545 interface->index = index;
546 interface->ident = g_strdup(ident);
548 g_hash_table_insert(interface_list,
549 GINT_TO_POINTER(index), interface);
551 if (type == ARPHRD_ETHER)
552 read_uevent(interface);
553 #if defined TIZEN_EXT
554 if (type == ARPHRD_PPP || type == ARPHDR_RMNET)
555 read_uevent(interface);
557 } else if (g_strcmp0(interface->ident, ident) != 0) {
558 /* If an original address is built-in physical device,
559 * it's hardly get an address at a initial creation
561 __connman_technology_remove_interface(interface->service_type,
562 interface->index, interface->ident);
564 g_free(interface->ident);
565 interface->ident = g_strdup(ident);
567 __connman_technology_add_interface(interface->service_type,
568 interface->index, interface->ident);
572 } else if (type == ARPHRD_ETHER && interface->device_type == CONNMAN_DEVICE_TYPE_UNKNOWN)
573 read_uevent(interface);
577 for (list = rtnl_list; list; list = list->next) {
578 struct connman_rtnl *rtnl = list->data;
581 rtnl->newlink(type, index, flags, change);
585 * The interface needs to be added after the newlink call.
586 * The newlink will create the technology when needed and
587 * __connman_technology_add_interface() expects the
588 * technology to be there already.
591 __connman_technology_add_interface(interface->service_type,
592 interface->index, interface->ident);
594 for (list = watch_list; list; list = list->next) {
595 struct watch_data *watch = list->data;
597 if (watch->index != index)
601 watch->newlink(flags, change, watch->user_data);
605 static void process_dellink(unsigned short type, int index, unsigned flags,
606 unsigned change, struct ifinfomsg *msg, int bytes)
608 struct rtnl_link_stats stats;
609 unsigned char operstate = 0xff;
610 const char *ifname = NULL;
613 memset(&stats, 0, sizeof(stats));
614 if (!extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats))
617 if (operstate != 0xff)
618 connman_info("%s {dellink} index %d operstate %u <%s>",
619 ifname, index, operstate,
620 operstate2str(operstate));
622 for (list = rtnl_list; list; list = list->next) {
623 struct connman_rtnl *rtnl = list->data;
626 rtnl->dellink(type, index, flags, change);
631 case ARPHRD_LOOPBACK:
632 case ARPHDR_PHONET_PIPE:
635 #if defined TIZEN_EXT
637 * Description: SLP requires ARPHRD_PPP for PPP type device
638 * ARPHDR_RMNET for QC modem using QMI
642 __connman_ipconfig_dellink(index, &stats);
646 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
649 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
651 struct in_addr *local,
652 struct in_addr *address,
653 struct in_addr *broadcast)
657 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
658 attr = RTA_NEXT(attr, bytes)) {
659 switch (attr->rta_type) {
662 *address = *((struct in_addr *) RTA_DATA(attr));
666 *local = *((struct in_addr *) RTA_DATA(attr));
670 *broadcast = *((struct in_addr *) RTA_DATA(attr));
674 *label = RTA_DATA(attr);
680 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
681 struct in6_addr *addr,
682 struct in6_addr *local)
686 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
687 attr = RTA_NEXT(attr, bytes)) {
688 switch (attr->rta_type) {
691 *addr = *((struct in6_addr *) RTA_DATA(attr));
695 *local = *((struct in6_addr *) RTA_DATA(attr));
701 static void process_newaddr(unsigned char family, unsigned char prefixlen,
702 int index, struct ifaddrmsg *msg, int bytes)
704 struct in_addr ipv4_addr = { INADDR_ANY };
705 struct in6_addr ipv6_address, ipv6_local;
706 const char *label = NULL;
708 char ip_string[INET6_ADDRSTRLEN];
710 if (family == AF_INET) {
712 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
714 } else if (family == AF_INET6) {
715 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
716 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
724 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
727 if (__connman_ipconfig_newaddr(index, family, label,
728 prefixlen, ip_string) >= 0) {
729 if (family == AF_INET6) {
731 * Re-create RDNSS configured servers if there
732 * are any for this interface. This is done
733 * because we might have now properly
734 * configured interface with proper
735 * autoconfigured address.
737 __connman_resolver_redo_servers(index);
742 static void process_deladdr(unsigned char family, unsigned char prefixlen,
743 int index, struct ifaddrmsg *msg, int bytes)
745 struct in_addr ipv4_addr = { INADDR_ANY };
746 struct in6_addr ipv6_address, ipv6_local;
747 const char *label = NULL;
749 char ip_string[INET6_ADDRSTRLEN];
751 if (family == AF_INET) {
752 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
754 } else if (family == AF_INET6) {
755 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
756 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
764 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
767 __connman_ipconfig_deladdr(index, family, label,
768 prefixlen, ip_string);
771 static void extract_ipv4_route(struct rtmsg *msg, int bytes, int *index,
773 struct in_addr *gateway)
777 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
778 attr = RTA_NEXT(attr, bytes)) {
779 switch (attr->rta_type) {
782 *dst = *((struct in_addr *) RTA_DATA(attr));
786 *gateway = *((struct in_addr *) RTA_DATA(attr));
790 *index = *((int *) RTA_DATA(attr));
796 static void extract_ipv6_route(struct rtmsg *msg, int bytes, int *index,
797 struct in6_addr *dst,
798 struct in6_addr *gateway)
802 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
803 attr = RTA_NEXT(attr, bytes)) {
804 switch (attr->rta_type) {
807 *dst = *((struct in6_addr *) RTA_DATA(attr));
812 *((struct in6_addr *) RTA_DATA(attr));
816 *index = *((int *) RTA_DATA(attr));
822 static void process_newroute(unsigned char family, unsigned char scope,
823 struct rtmsg *msg, int bytes)
826 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
829 if (family == AF_INET) {
830 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
832 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
834 inet_ntop(family, &dst, dststr, sizeof(dststr));
835 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
837 __connman_ipconfig_newroute(index, family, scope, dststr,
840 /* skip host specific routes */
841 if (scope != RT_SCOPE_UNIVERSE &&
842 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
845 if (dst.s_addr != INADDR_ANY)
848 } else if (family == AF_INET6) {
849 struct in6_addr dst = IN6ADDR_ANY_INIT,
850 gateway = IN6ADDR_ANY_INIT;
852 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
854 inet_ntop(family, &dst, dststr, sizeof(dststr));
855 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
857 __connman_ipconfig_newroute(index, family, scope, dststr,
860 /* skip host specific routes */
861 if (scope != RT_SCOPE_UNIVERSE &&
862 !(scope == RT_SCOPE_LINK &&
863 IN6_IS_ADDR_UNSPECIFIED(&dst)))
866 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
871 for (list = rtnl_list; list; list = list->next) {
872 struct connman_rtnl *rtnl = list->data;
874 if (rtnl->newgateway)
875 rtnl->newgateway(index, gatewaystr);
879 static void process_delroute(unsigned char family, unsigned char scope,
880 struct rtmsg *msg, int bytes)
883 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
886 if (family == AF_INET) {
887 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
889 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
891 inet_ntop(family, &dst, dststr, sizeof(dststr));
892 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
894 __connman_ipconfig_delroute(index, family, scope, dststr,
897 /* skip host specific routes */
898 if (scope != RT_SCOPE_UNIVERSE &&
899 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
902 if (dst.s_addr != INADDR_ANY)
905 } else if (family == AF_INET6) {
906 struct in6_addr dst = IN6ADDR_ANY_INIT,
907 gateway = IN6ADDR_ANY_INIT;
909 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
911 inet_ntop(family, &dst, dststr, sizeof(dststr));
912 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
914 __connman_ipconfig_delroute(index, family, scope, dststr,
917 /* skip host specific routes */
918 if (scope != RT_SCOPE_UNIVERSE &&
919 !(scope == RT_SCOPE_LINK &&
920 IN6_IS_ADDR_UNSPECIFIED(&dst)))
923 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
928 for (list = rtnl_list; list; list = list->next) {
929 struct connman_rtnl *rtnl = list->data;
931 if (rtnl->delgateway)
932 rtnl->delgateway(index, gatewaystr);
936 static inline void print_ether(struct rtattr *attr, const char *name)
938 int len = (int) RTA_PAYLOAD(attr);
940 if (len == ETH_ALEN) {
941 struct ether_addr eth;
942 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
943 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
945 print(" attr %s (len %d)\n", name, len);
948 static inline void print_inet(struct rtattr *attr, const char *name,
949 unsigned char family)
951 int len = (int) RTA_PAYLOAD(attr);
953 if (family == AF_INET && len == sizeof(struct in_addr)) {
955 addr = *((struct in_addr *) RTA_DATA(attr));
956 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
958 print(" attr %s (len %d)\n", name, len);
961 static inline void print_string(struct rtattr *attr, const char *name)
963 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
964 (char *) RTA_DATA(attr));
967 static inline void print_byte(struct rtattr *attr, const char *name)
969 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
970 *((unsigned char *) RTA_DATA(attr)));
973 static inline void print_integer(struct rtattr *attr, const char *name)
975 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
976 *((int *) RTA_DATA(attr)));
979 static inline void print_attr(struct rtattr *attr, const char *name)
981 int len = (int) RTA_PAYLOAD(attr);
984 print(" attr %s (len %d)\n", name, len);
986 print(" attr %d (len %d)\n", attr->rta_type, len);
989 static void rtnl_link(struct nlmsghdr *hdr)
991 struct ifinfomsg *msg;
995 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
996 bytes = IFLA_PAYLOAD(hdr);
998 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
1000 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
1001 attr = RTA_NEXT(attr, bytes)) {
1002 switch (attr->rta_type) {
1004 print_ether(attr, "address");
1006 case IFLA_BROADCAST:
1007 print_ether(attr, "broadcast");
1010 print_string(attr, "ifname");
1013 print_integer(attr, "mtu");
1016 print_attr(attr, "link");
1019 print_attr(attr, "qdisc");
1022 print_attr(attr, "stats");
1025 print_attr(attr, "cost");
1028 print_attr(attr, "priority");
1031 print_attr(attr, "master");
1034 print_attr(attr, "wireless");
1037 print_attr(attr, "protinfo");
1040 print_integer(attr, "txqlen");
1043 print_attr(attr, "map");
1046 print_attr(attr, "weight");
1048 case IFLA_OPERSTATE:
1049 print_byte(attr, "operstate");
1052 print_byte(attr, "linkmode");
1055 print_attr(attr, NULL);
1061 static void rtnl_newlink(struct nlmsghdr *hdr)
1063 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
1067 if (hdr->nlmsg_type == IFLA_WIRELESS)
1068 connman_warn_once("Obsolete WEXT WiFi driver detected");
1070 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
1071 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
1074 static void rtnl_dellink(struct nlmsghdr *hdr)
1076 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
1080 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
1081 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
1084 static void rtnl_addr(struct nlmsghdr *hdr)
1086 struct ifaddrmsg *msg;
1087 struct rtattr *attr;
1090 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1091 bytes = IFA_PAYLOAD(hdr);
1093 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
1095 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
1096 attr = RTA_NEXT(attr, bytes)) {
1097 switch (attr->rta_type) {
1099 print_inet(attr, "address", msg->ifa_family);
1102 print_inet(attr, "local", msg->ifa_family);
1105 print_string(attr, "label");
1108 print_inet(attr, "broadcast", msg->ifa_family);
1111 print_attr(attr, "anycast");
1114 print_attr(attr, "cacheinfo");
1117 print_attr(attr, "multicast");
1120 print_attr(attr, NULL);
1126 static void rtnl_newaddr(struct nlmsghdr *hdr)
1128 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1132 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1133 msg, IFA_PAYLOAD(hdr));
1136 static void rtnl_deladdr(struct nlmsghdr *hdr)
1138 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1142 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1143 msg, IFA_PAYLOAD(hdr));
1146 static void rtnl_route(struct nlmsghdr *hdr)
1149 struct rtattr *attr;
1152 msg = (struct rtmsg *) NLMSG_DATA(hdr);
1153 bytes = RTM_PAYLOAD(hdr);
1155 print("rtm_family %d rtm_table %d rtm_protocol %d",
1156 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
1157 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
1158 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
1160 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
1161 attr = RTA_NEXT(attr, bytes)) {
1162 switch (attr->rta_type) {
1164 print_inet(attr, "dst", msg->rtm_family);
1167 print_inet(attr, "src", msg->rtm_family);
1170 print_string(attr, "iif");
1173 print_integer(attr, "oif");
1176 print_inet(attr, "gateway", msg->rtm_family);
1179 print_attr(attr, "priority");
1182 print_inet(attr, "prefsrc", msg->rtm_family);
1185 print_attr(attr, "metrics");
1188 print_integer(attr, "table");
1191 print_attr(attr, NULL);
1197 static bool is_route_rtmsg(struct rtmsg *msg)
1199 if (msg->rtm_flags & RTM_F_CLONED)
1202 if (msg->rtm_table != RT_TABLE_MAIN)
1205 if (msg->rtm_protocol != RTPROT_BOOT &&
1206 msg->rtm_protocol != RTPROT_KERNEL)
1209 if (msg->rtm_type != RTN_UNICAST)
1215 static void rtnl_newroute(struct nlmsghdr *hdr)
1217 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1221 if (is_route_rtmsg(msg))
1222 process_newroute(msg->rtm_family, msg->rtm_scope,
1223 msg, RTM_PAYLOAD(hdr));
1226 static void rtnl_delroute(struct nlmsghdr *hdr)
1228 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1232 if (is_route_rtmsg(msg))
1233 process_delroute(msg->rtm_family, msg->rtm_scope,
1234 msg, RTM_PAYLOAD(hdr));
1237 static void *rtnl_nd_opt_rdnss(struct nd_opt_hdr *opt, guint32 *lifetime,
1240 guint32 *optint = (void *)opt;
1242 if (opt->nd_opt_len < 3)
1245 if (*lifetime > ntohl(optint[1]))
1246 *lifetime = ntohl(optint[1]);
1248 /* nd_opt_len is in units of 8 bytes. The header is 1 unit (8 bytes)
1249 and each address is another 2 units (16 bytes).
1250 So the number of addresses (given rounding) is nd_opt_len/2 */
1251 *nr_servers = opt->nd_opt_len / 2;
1253 /* And they start 8 bytes into the packet, or two guint32s in. */
1257 static const char **rtnl_nd_opt_dnssl(struct nd_opt_hdr *opt, guint32 *lifetime)
1259 const char **domains = NULL;
1260 guint32 *optint = (void *)opt;
1261 unsigned char *optc = (void *)&optint[2];
1262 int data_len = (opt->nd_opt_len * 8) - 8;
1266 if (*lifetime > ntohl(optint[1]))
1267 *lifetime = ntohl(optint[1]);
1269 /* Turn it into normal strings by converting the length bytes into '.',
1270 and count how many search domains there are while we're at it. */
1272 while (i < data_len) {
1273 if (optc[i] > 0x3f) {
1274 DBG("DNSSL contains compressed elements in violation of RFC6106");
1281 /* Check for double zero */
1282 if (i < data_len && optc[i] == 0)
1290 if (i >= data_len) {
1291 DBG("DNSSL data overflows option length");
1298 domains = g_try_new0(const char *, nr_domains + 1);
1302 /* Now point to the normal strings, missing out the leading '.' that
1303 each of them will have now. */
1304 for (i = 0; i < nr_domains; i++) {
1305 domains[i] = (char *)optc + 1;
1306 optc += strlen((char *)optc) + 1;
1312 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1314 struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1315 struct nd_opt_hdr *opt;
1316 guint32 lifetime = -1;
1317 const char **domains = NULL;
1318 struct in6_addr *servers = NULL;
1319 int i, nr_servers = 0;
1320 int msglen = msg->nduseropt_opts_len;
1323 DBG("family %d index %d len %d type %d code %d",
1324 msg->nduseropt_family, msg->nduseropt_ifindex,
1325 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1326 msg->nduseropt_icmp_code);
1328 if (msg->nduseropt_family != AF_INET6 ||
1329 msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1330 msg->nduseropt_icmp_code != 0)
1333 index = msg->nduseropt_ifindex;
1337 #if defined TIZEN_EXT
1338 struct connman_service *service;
1339 enum connman_service_state state;
1340 enum connman_dnsconfig_method ipv6_dns_method;
1342 service = __connman_service_lookup_from_index(index);
1344 DBG("Invalid service");
1348 DBG("service: %p index: %d\n", service, index);
1350 if (connman_setting_get_bool("SingleConnectedTechnology") == TRUE) {
1351 state = __connman_service_ipconfig_get_state(service, CONNMAN_IPCONFIG_TYPE_IPV6);
1352 if (state != CONNMAN_SERVICE_STATE_ASSOCIATION &&
1353 state != CONNMAN_SERVICE_STATE_CONFIGURATION &&
1354 state != CONNMAN_SERVICE_STATE_READY &&
1355 state != CONNMAN_SERVICE_STATE_ONLINE) {
1356 DBG("Service state[%d] is not connecting/connected", state);
1361 ipv6_dns_method = connman_service_get_ipv6_dns_method(service);
1362 if (ipv6_dns_method != CONNMAN_DNSCONFIG_METHOD_DHCP) {
1363 DBG("IPv6 DNS method is not Auto ignore RA!!! [DNS method: %d]", ipv6_dns_method);
1368 for (opt = (void *)&msg[1];
1370 msglen -= opt->nd_opt_len * 8,
1371 opt = ((void *)opt) + opt->nd_opt_len*8) {
1373 DBG("remaining %d nd opt type %d len %d\n",
1374 msglen, opt->nd_opt_type, opt->nd_opt_len);
1376 if (opt->nd_opt_type == 25) { /* ND_OPT_RDNSS */
1378 #if defined TIZEN_EXT
1379 struct connman_service *service;
1381 service = __connman_service_lookup_from_index(index);
1382 DBG("service: %p\n",service);
1384 servers = rtnl_nd_opt_rdnss(opt, &lifetime,
1386 for (i = 0; i < nr_servers; i++) {
1387 if (!inet_ntop(AF_INET6, servers + i, buf,
1391 #if defined TIZEN_EXT
1392 __connman_service_nameserver_remove(service,
1394 CONNMAN_IPCONFIG_TYPE_IPV6);
1395 __connman_service_nameserver_append(service,
1397 CONNMAN_IPCONFIG_TYPE_IPV6);
1399 connman_resolver_append_lifetime(index,
1400 NULL, buf, lifetime);
1403 } else if (opt->nd_opt_type == 31) { /* ND_OPT_DNSSL */
1406 domains = rtnl_nd_opt_dnssl(opt, &lifetime);
1407 for (i = 0; domains && domains[i]; i++)
1408 connman_resolver_append_lifetime(index,
1409 domains[i], NULL, lifetime);
1416 static const char *type2string(uint16_t type)
1445 case RTM_NEWNDUSEROPT:
1446 return "NEWNDUSEROPT";
1452 static GIOChannel *channel = NULL;
1453 static guint channel_watch = 0;
1455 struct rtnl_request {
1456 struct nlmsghdr hdr;
1457 struct rtgenmsg msg;
1459 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1461 static GSList *request_list = NULL;
1462 static guint32 request_seq = 0;
1464 static struct rtnl_request *find_request(guint32 seq)
1468 for (list = request_list; list; list = list->next) {
1469 struct rtnl_request *req = list->data;
1471 if (req->hdr.nlmsg_seq == seq)
1478 static int send_request(struct rtnl_request *req)
1480 struct sockaddr_nl addr;
1483 DBG("%s len %d type %d flags 0x%04x seq %d",
1484 type2string(req->hdr.nlmsg_type),
1485 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1486 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1488 sk = g_io_channel_unix_get_fd(channel);
1490 memset(&addr, 0, sizeof(addr));
1491 addr.nl_family = AF_NETLINK;
1493 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1494 (struct sockaddr *) &addr, sizeof(addr));
1497 static int queue_request(struct rtnl_request *req)
1499 request_list = g_slist_append(request_list, req);
1501 if (g_slist_length(request_list) > 1)
1504 return send_request(req);
1507 static int process_response(guint32 seq)
1509 struct rtnl_request *req;
1513 req = find_request(seq);
1515 request_list = g_slist_remove(request_list, req);
1519 req = g_slist_nth_data(request_list, 0);
1523 return send_request(req);
1526 static void rtnl_message(void *buf, size_t len)
1529 struct nlmsghdr *hdr = buf;
1530 struct nlmsgerr *err;
1532 if (!NLMSG_OK(hdr, len))
1535 DBG("%s len %u type %u flags 0x%04x seq %u pid %u",
1536 type2string(hdr->nlmsg_type),
1537 hdr->nlmsg_len, hdr->nlmsg_type,
1538 hdr->nlmsg_flags, hdr->nlmsg_seq,
1541 switch (hdr->nlmsg_type) {
1546 process_response(hdr->nlmsg_seq);
1549 err = NLMSG_DATA(hdr);
1550 DBG("error %d (%s)", -err->error,
1551 strerror(-err->error));
1571 case RTM_NEWNDUSEROPT:
1572 rtnl_newnduseropt(hdr);
1576 len -= hdr->nlmsg_len;
1577 buf += hdr->nlmsg_len;
1581 static gboolean netlink_event(GIOChannel *chan, GIOCondition cond, gpointer data)
1583 unsigned char buf[4096];
1584 struct sockaddr_nl nladdr;
1585 socklen_t addr_len = sizeof(nladdr);
1589 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1592 memset(buf, 0, sizeof(buf));
1593 memset(&nladdr, 0, sizeof(nladdr));
1595 fd = g_io_channel_unix_get_fd(chan);
1597 status = recvfrom(fd, buf, sizeof(buf), 0,
1598 (struct sockaddr *) &nladdr, &addr_len);
1600 if (errno == EINTR || errno == EAGAIN)
1609 if (nladdr.nl_pid != 0) { /* not sent by kernel, ignore */
1610 DBG("Received msg from %u, ignoring it", nladdr.nl_pid);
1614 rtnl_message(buf, status);
1619 static int send_getlink(void)
1621 struct rtnl_request *req;
1625 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1629 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1630 req->hdr.nlmsg_type = RTM_GETLINK;
1631 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1632 req->hdr.nlmsg_pid = 0;
1633 req->hdr.nlmsg_seq = request_seq++;
1634 req->msg.rtgen_family = AF_INET;
1636 return queue_request(req);
1639 static int send_getaddr(void)
1641 struct rtnl_request *req;
1645 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1649 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1650 req->hdr.nlmsg_type = RTM_GETADDR;
1651 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1652 req->hdr.nlmsg_pid = 0;
1653 req->hdr.nlmsg_seq = request_seq++;
1654 req->msg.rtgen_family = AF_INET;
1656 return queue_request(req);
1659 static int send_getroute(void)
1661 struct rtnl_request *req;
1665 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1669 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1670 req->hdr.nlmsg_type = RTM_GETROUTE;
1671 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1672 req->hdr.nlmsg_pid = 0;
1673 req->hdr.nlmsg_seq = request_seq++;
1674 req->msg.rtgen_family = AF_INET;
1676 return queue_request(req);
1679 static gboolean update_timeout_cb(gpointer user_data)
1681 __connman_rtnl_request_update();
1686 static void update_interval_callback(guint min)
1688 if (update_timeout > 0)
1689 g_source_remove(update_timeout);
1691 if (min < G_MAXUINT) {
1692 update_interval = min;
1693 update_timeout = g_timeout_add_seconds(update_interval,
1694 update_timeout_cb, NULL);
1697 update_interval = G_MAXUINT;
1701 static gint compare_interval(gconstpointer a, gconstpointer b)
1703 guint val_a = GPOINTER_TO_UINT(a);
1704 guint val_b = GPOINTER_TO_UINT(b);
1706 return val_a - val_b;
1709 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1716 update_list = g_slist_insert_sorted(update_list,
1717 GUINT_TO_POINTER(interval), compare_interval);
1719 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1720 if (min < update_interval) {
1721 update_interval_callback(min);
1722 __connman_rtnl_request_update();
1725 return update_interval;
1728 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1730 guint min = G_MAXUINT;
1735 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1738 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1740 if (min > update_interval)
1741 update_interval_callback(min);
1746 int __connman_rtnl_request_update(void)
1748 return send_getlink();
1751 int __connman_rtnl_init(void)
1753 struct sockaddr_nl addr;
1758 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1759 NULL, free_interface);
1761 sk = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
1765 memset(&addr, 0, sizeof(addr));
1766 addr.nl_family = AF_NETLINK;
1767 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1768 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1769 (1<<(RTNLGRP_ND_USEROPT-1));
1771 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1776 channel = g_io_channel_unix_new(sk);
1777 g_io_channel_set_close_on_unref(channel, TRUE);
1779 g_io_channel_set_encoding(channel, NULL, NULL);
1780 g_io_channel_set_buffered(channel, FALSE);
1782 channel_watch = g_io_add_watch(channel,
1783 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1784 netlink_event, NULL);
1789 void __connman_rtnl_start(void)
1798 void __connman_rtnl_cleanup(void)
1804 for (list = watch_list; list; list = list->next) {
1805 struct watch_data *watch = list->data;
1807 DBG("removing watch %d", watch->id);
1813 g_slist_free(watch_list);
1816 g_slist_free(update_list);
1819 for (list = request_list; list; list = list->next) {
1820 struct rtnl_request *req = list->data;
1822 DBG("%s len %d type %d flags 0x%04x seq %d",
1823 type2string(req->hdr.nlmsg_type),
1824 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1825 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1831 g_slist_free(request_list);
1832 request_list = NULL;
1834 if (channel_watch) {
1835 g_source_remove(channel_watch);
1839 g_io_channel_shutdown(channel, TRUE, NULL);
1840 g_io_channel_unref(channel);
1844 g_hash_table_destroy(interface_list);