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;
171 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
172 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
175 filename = g_strdup_printf("/sys/class/net/%s/uevent", name);
177 f = fopen(filename, "re");
182 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
183 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
187 found_devtype = false;
188 while (fgets(line, sizeof(line), f)) {
191 pos = strchr(line, '\n');
196 if (strncmp(line, "DEVTYPE=", 8) != 0)
199 found_devtype = true;
201 if (strcmp(line + 8, "wlan") == 0) {
202 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
203 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
204 } else if (strcmp(line + 8, "wwan") == 0) {
205 interface->service_type = CONNMAN_SERVICE_TYPE_CELLULAR;
206 interface->device_type = CONNMAN_DEVICE_TYPE_CELLULAR;
207 } else if (strcmp(line + 8, "bluetooth") == 0) {
208 interface->service_type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
209 interface->device_type = CONNMAN_DEVICE_TYPE_BLUETOOTH;
210 } else if (strcmp(line + 8, "gadget") == 0) {
211 interface->service_type = CONNMAN_SERVICE_TYPE_GADGET;
212 interface->device_type = CONNMAN_DEVICE_TYPE_GADGET;
213 } else if (strcmp(line + 8, "vlan") == 0) {
214 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
215 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
216 } else if (strcmp(line + 8, "bond") == 0) {
217 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
218 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
220 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
221 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
230 #if !defined TIZEN_EXT
231 /* TIZEN does not use old wext interface */
232 /* We haven't got a DEVTYPE, let's check if it's a wireless device */
233 if (wext_interface(name)) {
234 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
235 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
237 connman_error("%s runs an unsupported 802.11 driver", name);
245 enum connman_device_type __connman_rtnl_get_device_type(int index)
247 struct interface_data *interface;
249 interface = g_hash_table_lookup(interface_list,
250 GINT_TO_POINTER(index));
252 return CONNMAN_DEVICE_TYPE_UNKNOWN;
254 return interface->device_type;
258 * connman_rtnl_add_newlink_watch:
259 * @index: network device index
260 * @callback: callback function
261 * @user_data: callback data;
263 * Add a new RTNL watch for newlink events
265 * Returns: %0 on failure and a unique id on success
267 unsigned int connman_rtnl_add_newlink_watch(int index,
268 connman_rtnl_link_cb_t callback, void *user_data)
270 struct watch_data *watch;
272 watch = g_try_new0(struct watch_data, 1);
276 watch->id = ++watch_id;
277 watch->index = index;
279 watch->newlink = callback;
280 watch->user_data = user_data;
282 watch_list = g_slist_prepend(watch_list, watch);
284 DBG("id %d", watch->id);
287 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
290 callback(flags, 0, user_data);
297 * connman_rtnl_remove_watch:
298 * @id: watch identifier
300 * Remove the RTNL watch for the identifier
302 void connman_rtnl_remove_watch(unsigned int id)
311 for (list = watch_list; list; list = list->next) {
312 struct watch_data *watch = list->data;
314 if (watch->id == id) {
315 watch_list = g_slist_remove(watch_list, watch);
322 static void trigger_rtnl(int index, void *user_data)
324 struct connman_rtnl *rtnl = user_data;
327 unsigned short type = __connman_ipconfig_get_type_from_index(index);
328 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
330 rtnl->newlink(type, index, flags, 0);
333 if (rtnl->newgateway) {
334 const char *gateway =
335 __connman_ipconfig_get_gateway_from_index(index,
336 CONNMAN_IPCONFIG_TYPE_ALL);
339 rtnl->newgateway(index, gateway);
343 static GSList *rtnl_list = NULL;
345 static gint compare_priority(gconstpointer a, gconstpointer b)
347 const struct connman_rtnl *rtnl1 = a;
348 const struct connman_rtnl *rtnl2 = b;
350 return rtnl2->priority - rtnl1->priority;
354 * connman_rtnl_register:
357 * Register a new RTNL module
359 * Returns: %0 on success
361 int connman_rtnl_register(struct connman_rtnl *rtnl)
363 DBG("rtnl %p name %s", rtnl, rtnl->name);
365 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
368 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
374 * connman_rtnl_unregister:
377 * Remove a previously registered RTNL module
379 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
381 DBG("rtnl %p name %s", rtnl, rtnl->name);
383 rtnl_list = g_slist_remove(rtnl_list, rtnl);
386 static const char *operstate2str(unsigned char operstate)
389 case IF_OPER_UNKNOWN:
391 case IF_OPER_NOTPRESENT:
392 return "NOT-PRESENT";
395 case IF_OPER_LOWERLAYERDOWN:
396 return "LOWER-LAYER-DOWN";
397 case IF_OPER_TESTING:
399 case IF_OPER_DORMANT:
408 static bool extract_link(struct ifinfomsg *msg, int bytes,
409 struct ether_addr *address, const char **ifname,
410 unsigned int *mtu, unsigned char *operstate,
411 struct rtnl_link_stats *stats)
415 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
416 attr = RTA_NEXT(attr, bytes)) {
417 switch (attr->rta_type) {
420 memcpy(address, RTA_DATA(attr), ETH_ALEN);
424 *ifname = RTA_DATA(attr);
428 *mtu = *((unsigned int *) RTA_DATA(attr));
432 memcpy(stats, RTA_DATA(attr),
433 sizeof(struct rtnl_link_stats));
437 *operstate = *((unsigned char *) RTA_DATA(attr));
449 static void process_newlink(unsigned short type, int index, unsigned flags,
450 unsigned change, struct ifinfomsg *msg, int bytes)
452 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
453 struct rtnl_link_stats stats;
454 unsigned char operstate = 0xff;
455 struct interface_data *interface;
456 const char *ifname = NULL;
457 unsigned int mtu = 0;
458 char ident[13], str[18];
461 memset(&stats, 0, sizeof(stats));
462 if (!extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats))
465 #if defined TIZEN_EXT_WIFI_MESH
466 /* Do not accept Wi-Fi Mesh interface */
467 if (g_strrstr(ifname, "mesh") != NULL) {
468 DBG("Newlink event for Wi-Fi Mesh interface ignored");
472 /* Do not accept Wi-Fi WLAN1 interface "dedicated for softAP */
473 if (!g_strcmp0(ifname, "wlan1")) {
474 DBG("Newlink event for Wi-Fi WLAN1 interface ignored");
479 #if defined TIZEN_EXT
480 /* Do not accept Wi-Fi P2P interface */
481 if (g_strrstr(ifname, "p2p") != NULL) {
482 DBG("Newlink event for Wi-Fi P2P interface ignored");
487 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
488 address.ether_addr_octet[0],
489 address.ether_addr_octet[1],
490 address.ether_addr_octet[2],
491 address.ether_addr_octet[3],
492 address.ether_addr_octet[4],
493 address.ether_addr_octet[5]);
495 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
496 address.ether_addr_octet[0],
497 address.ether_addr_octet[1],
498 address.ether_addr_octet[2],
499 address.ether_addr_octet[3],
500 address.ether_addr_octet[4],
501 address.ether_addr_octet[5]);
503 if (flags & IFF_SLAVE) {
504 connman_info("%s {newlink} ignoring slave, index %d address %s",
510 if (TIZEN_TV_EXT && g_strcmp0(ident, "eeeeeeeeeeee") == 0) {
511 DBG("Newlink event with Dummy MAC. Ignored!");
518 case ARPHRD_LOOPBACK:
519 case ARPHDR_PHONET_PIPE:
522 #if defined TIZEN_EXT
524 * Description: ARPHDR_RMNET for QC modem using QMI
528 __connman_ipconfig_newlink(index, type, flags,
533 connman_info("%s {newlink} index %d address %s mtu %u",
534 ifname, index, str, mtu);
536 if (operstate != 0xff)
537 connman_info("%s {newlink} index %d operstate %u <%s>",
538 ifname, index, operstate,
539 operstate2str(operstate));
541 interface = g_hash_table_lookup(interface_list, GINT_TO_POINTER(index));
543 interface = g_new0(struct interface_data, 1);
544 interface->index = index;
545 interface->ident = g_strdup(ident);
547 g_hash_table_insert(interface_list,
548 GINT_TO_POINTER(index), interface);
550 if (type == ARPHRD_ETHER)
551 read_uevent(interface);
552 #if defined TIZEN_EXT
553 if (type == ARPHRD_PPP || type == ARPHDR_RMNET)
554 read_uevent(interface);
556 } else if (g_strcmp0(interface->ident, ident) != 0) {
557 /* If an original address is built-in physical device,
558 * it's hardly get an address at a initial creation
560 __connman_technology_remove_interface(interface->service_type,
561 interface->index, interface->ident);
563 g_free(interface->ident);
564 interface->ident = g_strdup(ident);
566 __connman_technology_add_interface(interface->service_type,
567 interface->index, interface->ident);
571 } else if (type == ARPHRD_ETHER && interface->device_type == CONNMAN_DEVICE_TYPE_UNKNOWN)
572 read_uevent(interface);
576 for (list = rtnl_list; list; list = list->next) {
577 struct connman_rtnl *rtnl = list->data;
580 rtnl->newlink(type, index, flags, change);
584 * The interface needs to be added after the newlink call.
585 * The newlink will create the technology when needed and
586 * __connman_technology_add_interface() expects the
587 * technology to be there already.
590 __connman_technology_add_interface(interface->service_type,
591 interface->index, interface->ident);
593 for (list = watch_list; list; list = list->next) {
594 struct watch_data *watch = list->data;
596 if (watch->index != index)
600 watch->newlink(flags, change, watch->user_data);
604 static void process_dellink(unsigned short type, int index, unsigned flags,
605 unsigned change, struct ifinfomsg *msg, int bytes)
607 struct rtnl_link_stats stats;
608 unsigned char operstate = 0xff;
609 const char *ifname = NULL;
612 memset(&stats, 0, sizeof(stats));
613 if (!extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats))
616 if (operstate != 0xff)
617 connman_info("%s {dellink} index %d operstate %u <%s>",
618 ifname, index, operstate,
619 operstate2str(operstate));
621 for (list = rtnl_list; list; list = list->next) {
622 struct connman_rtnl *rtnl = list->data;
625 rtnl->dellink(type, index, flags, change);
630 case ARPHRD_LOOPBACK:
631 case ARPHDR_PHONET_PIPE:
634 #if defined TIZEN_EXT
636 * Description: SLP requires ARPHRD_PPP for PPP type device
637 * ARPHDR_RMNET for QC modem using QMI
641 __connman_ipconfig_dellink(index, &stats);
645 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
648 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
650 struct in_addr *local,
651 struct in_addr *address,
652 struct in_addr *broadcast)
656 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
657 attr = RTA_NEXT(attr, bytes)) {
658 switch (attr->rta_type) {
661 *address = *((struct in_addr *) RTA_DATA(attr));
665 *local = *((struct in_addr *) RTA_DATA(attr));
669 *broadcast = *((struct in_addr *) RTA_DATA(attr));
673 *label = RTA_DATA(attr);
679 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
680 struct in6_addr *addr,
681 struct in6_addr *local)
685 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
686 attr = RTA_NEXT(attr, bytes)) {
687 switch (attr->rta_type) {
690 *addr = *((struct in6_addr *) RTA_DATA(attr));
694 *local = *((struct in6_addr *) RTA_DATA(attr));
700 static void process_newaddr(unsigned char family, unsigned char prefixlen,
701 int index, struct ifaddrmsg *msg, int bytes)
703 struct in_addr ipv4_addr = { INADDR_ANY };
704 struct in6_addr ipv6_address, ipv6_local;
705 const char *label = NULL;
707 char ip_string[INET6_ADDRSTRLEN];
709 if (family == AF_INET) {
711 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
713 } else if (family == AF_INET6) {
714 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
715 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
723 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
726 if (__connman_ipconfig_newaddr(index, family, label,
727 prefixlen, ip_string) >= 0) {
728 if (family == AF_INET6) {
730 * Re-create RDNSS configured servers if there
731 * are any for this interface. This is done
732 * because we might have now properly
733 * configured interface with proper
734 * autoconfigured address.
736 __connman_resolver_redo_servers(index);
741 static void process_deladdr(unsigned char family, unsigned char prefixlen,
742 int index, struct ifaddrmsg *msg, int bytes)
744 struct in_addr ipv4_addr = { INADDR_ANY };
745 struct in6_addr ipv6_address, ipv6_local;
746 const char *label = NULL;
748 char ip_string[INET6_ADDRSTRLEN];
750 if (family == AF_INET) {
751 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
753 } else if (family == AF_INET6) {
754 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
755 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
763 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
766 __connman_ipconfig_deladdr(index, family, label,
767 prefixlen, ip_string);
770 static void extract_ipv4_route(struct rtmsg *msg, int bytes, int *index,
772 struct in_addr *gateway)
776 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
777 attr = RTA_NEXT(attr, bytes)) {
778 switch (attr->rta_type) {
781 *dst = *((struct in_addr *) RTA_DATA(attr));
785 *gateway = *((struct in_addr *) RTA_DATA(attr));
789 *index = *((int *) RTA_DATA(attr));
795 static void extract_ipv6_route(struct rtmsg *msg, int bytes, int *index,
796 struct in6_addr *dst,
797 struct in6_addr *gateway)
801 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
802 attr = RTA_NEXT(attr, bytes)) {
803 switch (attr->rta_type) {
806 *dst = *((struct in6_addr *) RTA_DATA(attr));
811 *((struct in6_addr *) RTA_DATA(attr));
815 *index = *((int *) RTA_DATA(attr));
821 static void process_newroute(unsigned char family, unsigned char scope,
822 struct rtmsg *msg, int bytes)
825 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
828 if (family == AF_INET) {
829 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
831 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
833 inet_ntop(family, &dst, dststr, sizeof(dststr));
834 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
836 __connman_ipconfig_newroute(index, family, scope, dststr,
839 /* skip host specific routes */
840 if (scope != RT_SCOPE_UNIVERSE &&
841 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
844 if (dst.s_addr != INADDR_ANY)
847 } else if (family == AF_INET6) {
848 struct in6_addr dst = IN6ADDR_ANY_INIT,
849 gateway = IN6ADDR_ANY_INIT;
851 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
853 inet_ntop(family, &dst, dststr, sizeof(dststr));
854 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
856 __connman_ipconfig_newroute(index, family, scope, dststr,
859 /* skip host specific routes */
860 if (scope != RT_SCOPE_UNIVERSE &&
861 !(scope == RT_SCOPE_LINK &&
862 IN6_IS_ADDR_UNSPECIFIED(&dst)))
865 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
870 for (list = rtnl_list; list; list = list->next) {
871 struct connman_rtnl *rtnl = list->data;
873 if (rtnl->newgateway)
874 rtnl->newgateway(index, gatewaystr);
878 static void process_delroute(unsigned char family, unsigned char scope,
879 struct rtmsg *msg, int bytes)
882 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
885 if (family == AF_INET) {
886 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
888 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
890 inet_ntop(family, &dst, dststr, sizeof(dststr));
891 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
893 __connman_ipconfig_delroute(index, family, scope, dststr,
896 /* skip host specific routes */
897 if (scope != RT_SCOPE_UNIVERSE &&
898 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
901 if (dst.s_addr != INADDR_ANY)
904 } else if (family == AF_INET6) {
905 struct in6_addr dst = IN6ADDR_ANY_INIT,
906 gateway = IN6ADDR_ANY_INIT;
908 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
910 inet_ntop(family, &dst, dststr, sizeof(dststr));
911 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
913 __connman_ipconfig_delroute(index, family, scope, dststr,
916 /* skip host specific routes */
917 if (scope != RT_SCOPE_UNIVERSE &&
918 !(scope == RT_SCOPE_LINK &&
919 IN6_IS_ADDR_UNSPECIFIED(&dst)))
922 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
927 for (list = rtnl_list; list; list = list->next) {
928 struct connman_rtnl *rtnl = list->data;
930 if (rtnl->delgateway)
931 rtnl->delgateway(index, gatewaystr);
935 static inline void print_ether(struct rtattr *attr, const char *name)
937 int len = (int) RTA_PAYLOAD(attr);
939 if (len == ETH_ALEN) {
940 struct ether_addr eth;
941 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
942 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
944 print(" attr %s (len %d)\n", name, len);
947 static inline void print_inet(struct rtattr *attr, const char *name,
948 unsigned char family)
950 int len = (int) RTA_PAYLOAD(attr);
952 if (family == AF_INET && len == sizeof(struct in_addr)) {
954 addr = *((struct in_addr *) RTA_DATA(attr));
955 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
957 print(" attr %s (len %d)\n", name, len);
960 static inline void print_string(struct rtattr *attr, const char *name)
962 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
963 (char *) RTA_DATA(attr));
966 static inline void print_byte(struct rtattr *attr, const char *name)
968 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
969 *((unsigned char *) RTA_DATA(attr)));
972 static inline void print_integer(struct rtattr *attr, const char *name)
974 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
975 *((int *) RTA_DATA(attr)));
978 static inline void print_attr(struct rtattr *attr, const char *name)
980 int len = (int) RTA_PAYLOAD(attr);
983 print(" attr %s (len %d)\n", name, len);
985 print(" attr %d (len %d)\n", attr->rta_type, len);
988 static void rtnl_link(struct nlmsghdr *hdr)
990 struct ifinfomsg *msg;
994 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
995 bytes = IFLA_PAYLOAD(hdr);
997 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
999 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
1000 attr = RTA_NEXT(attr, bytes)) {
1001 switch (attr->rta_type) {
1003 print_ether(attr, "address");
1005 case IFLA_BROADCAST:
1006 print_ether(attr, "broadcast");
1009 print_string(attr, "ifname");
1012 print_integer(attr, "mtu");
1015 print_attr(attr, "link");
1018 print_attr(attr, "qdisc");
1021 print_attr(attr, "stats");
1024 print_attr(attr, "cost");
1027 print_attr(attr, "priority");
1030 print_attr(attr, "master");
1033 print_attr(attr, "wireless");
1036 print_attr(attr, "protinfo");
1039 print_integer(attr, "txqlen");
1042 print_attr(attr, "map");
1045 print_attr(attr, "weight");
1047 case IFLA_OPERSTATE:
1048 print_byte(attr, "operstate");
1051 print_byte(attr, "linkmode");
1054 print_attr(attr, NULL);
1060 static void rtnl_newlink(struct nlmsghdr *hdr)
1062 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
1066 if (hdr->nlmsg_type == IFLA_WIRELESS)
1067 connman_warn_once("Obsolete WEXT WiFi driver detected");
1069 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
1070 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
1073 static void rtnl_dellink(struct nlmsghdr *hdr)
1075 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
1079 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
1080 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
1083 static void rtnl_addr(struct nlmsghdr *hdr)
1085 struct ifaddrmsg *msg;
1086 struct rtattr *attr;
1089 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1090 bytes = IFA_PAYLOAD(hdr);
1092 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
1094 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
1095 attr = RTA_NEXT(attr, bytes)) {
1096 switch (attr->rta_type) {
1098 print_inet(attr, "address", msg->ifa_family);
1101 print_inet(attr, "local", msg->ifa_family);
1104 print_string(attr, "label");
1107 print_inet(attr, "broadcast", msg->ifa_family);
1110 print_attr(attr, "anycast");
1113 print_attr(attr, "cacheinfo");
1116 print_attr(attr, "multicast");
1119 print_attr(attr, NULL);
1125 static void rtnl_newaddr(struct nlmsghdr *hdr)
1127 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1131 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1132 msg, IFA_PAYLOAD(hdr));
1135 static void rtnl_deladdr(struct nlmsghdr *hdr)
1137 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1141 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1142 msg, IFA_PAYLOAD(hdr));
1145 static void rtnl_route(struct nlmsghdr *hdr)
1148 struct rtattr *attr;
1151 msg = (struct rtmsg *) NLMSG_DATA(hdr);
1152 bytes = RTM_PAYLOAD(hdr);
1154 print("rtm_family %d rtm_table %d rtm_protocol %d",
1155 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
1156 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
1157 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
1159 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
1160 attr = RTA_NEXT(attr, bytes)) {
1161 switch (attr->rta_type) {
1163 print_inet(attr, "dst", msg->rtm_family);
1166 print_inet(attr, "src", msg->rtm_family);
1169 print_string(attr, "iif");
1172 print_integer(attr, "oif");
1175 print_inet(attr, "gateway", msg->rtm_family);
1178 print_attr(attr, "priority");
1181 print_inet(attr, "prefsrc", msg->rtm_family);
1184 print_attr(attr, "metrics");
1187 print_integer(attr, "table");
1190 print_attr(attr, NULL);
1196 static bool is_route_rtmsg(struct rtmsg *msg)
1198 if (msg->rtm_flags & RTM_F_CLONED)
1201 if (msg->rtm_table != RT_TABLE_MAIN)
1204 if (msg->rtm_protocol != RTPROT_BOOT &&
1205 msg->rtm_protocol != RTPROT_KERNEL)
1208 if (msg->rtm_type != RTN_UNICAST)
1214 static void rtnl_newroute(struct nlmsghdr *hdr)
1216 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1220 if (is_route_rtmsg(msg))
1221 process_newroute(msg->rtm_family, msg->rtm_scope,
1222 msg, RTM_PAYLOAD(hdr));
1225 static void rtnl_delroute(struct nlmsghdr *hdr)
1227 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1231 if (is_route_rtmsg(msg))
1232 process_delroute(msg->rtm_family, msg->rtm_scope,
1233 msg, RTM_PAYLOAD(hdr));
1236 static void *rtnl_nd_opt_rdnss(struct nd_opt_hdr *opt, guint32 *lifetime,
1239 guint32 *optint = (void *)opt;
1241 if (opt->nd_opt_len < 3)
1244 if (*lifetime > ntohl(optint[1]))
1245 *lifetime = ntohl(optint[1]);
1247 /* nd_opt_len is in units of 8 bytes. The header is 1 unit (8 bytes)
1248 and each address is another 2 units (16 bytes).
1249 So the number of addresses (given rounding) is nd_opt_len/2 */
1250 *nr_servers = opt->nd_opt_len / 2;
1252 /* And they start 8 bytes into the packet, or two guint32s in. */
1256 static const char **rtnl_nd_opt_dnssl(struct nd_opt_hdr *opt, guint32 *lifetime)
1258 const char **domains = NULL;
1259 guint32 *optint = (void *)opt;
1260 unsigned char *optc = (void *)&optint[2];
1261 int data_len = (opt->nd_opt_len * 8) - 8;
1265 if (*lifetime > ntohl(optint[1]))
1266 *lifetime = ntohl(optint[1]);
1268 /* Turn it into normal strings by converting the length bytes into '.',
1269 and count how many search domains there are while we're at it. */
1271 while (i < data_len) {
1272 if (optc[i] > 0x3f) {
1273 DBG("DNSSL contains compressed elements in violation of RFC6106");
1280 /* Check for double zero */
1281 if (i < data_len && optc[i] == 0)
1289 if (i >= data_len) {
1290 DBG("DNSSL data overflows option length");
1297 domains = g_try_new0(const char *, nr_domains + 1);
1301 /* Now point to the normal strings, missing out the leading '.' that
1302 each of them will have now. */
1303 for (i = 0; i < nr_domains; i++) {
1304 domains[i] = (char *)optc + 1;
1305 optc += strlen((char *)optc) + 1;
1311 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1313 struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1314 struct nd_opt_hdr *opt;
1315 guint32 lifetime = -1;
1316 const char **domains = NULL;
1317 struct in6_addr *servers = NULL;
1318 int i, nr_servers = 0;
1319 int msglen = msg->nduseropt_opts_len;
1322 DBG("family %d index %d len %d type %d code %d",
1323 msg->nduseropt_family, msg->nduseropt_ifindex,
1324 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1325 msg->nduseropt_icmp_code);
1327 if (msg->nduseropt_family != AF_INET6 ||
1328 msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1329 msg->nduseropt_icmp_code != 0)
1332 index = msg->nduseropt_ifindex;
1336 #if defined TIZEN_EXT
1337 struct connman_service *service;
1338 enum connman_service_state state;
1339 enum connman_dnsconfig_method ipv6_dns_method;
1341 service = __connman_service_lookup_from_index(index);
1343 DBG("Invalid service");
1347 DBG("service: %p index: %d\n", service, index);
1349 if (connman_setting_get_bool("SingleConnectedTechnology") == TRUE) {
1350 state = __connman_service_ipconfig_get_state(service, CONNMAN_IPCONFIG_TYPE_IPV6);
1351 if (state != CONNMAN_SERVICE_STATE_ASSOCIATION &&
1352 state != CONNMAN_SERVICE_STATE_CONFIGURATION &&
1353 state != CONNMAN_SERVICE_STATE_READY &&
1354 state != CONNMAN_SERVICE_STATE_ONLINE) {
1355 DBG("Service state[%d] is not connecting/connected", state);
1360 ipv6_dns_method = connman_service_get_ipv6_dns_method(service);
1361 if (ipv6_dns_method != CONNMAN_DNSCONFIG_METHOD_DHCP) {
1362 DBG("IPv6 DNS method is not Auto ignore RA!!! [DNS method: %d]", ipv6_dns_method);
1367 for (opt = (void *)&msg[1];
1369 msglen -= opt->nd_opt_len * 8,
1370 opt = ((void *)opt) + opt->nd_opt_len*8) {
1372 DBG("remaining %d nd opt type %d len %d\n",
1373 msglen, opt->nd_opt_type, opt->nd_opt_len);
1375 if (opt->nd_opt_type == 25) { /* ND_OPT_RDNSS */
1377 #if defined TIZEN_EXT
1378 struct connman_service *service;
1380 service = __connman_service_lookup_from_index(index);
1381 DBG("service: %p\n",service);
1383 servers = rtnl_nd_opt_rdnss(opt, &lifetime,
1385 for (i = 0; i < nr_servers; i++) {
1386 if (!inet_ntop(AF_INET6, servers + i, buf,
1390 #if defined TIZEN_EXT
1391 __connman_service_nameserver_remove(service,
1393 CONNMAN_IPCONFIG_TYPE_IPV6);
1394 __connman_service_nameserver_append(service,
1396 CONNMAN_IPCONFIG_TYPE_IPV6);
1398 connman_resolver_append_lifetime(index,
1399 NULL, buf, lifetime);
1402 } else if (opt->nd_opt_type == 31) { /* ND_OPT_DNSSL */
1405 domains = rtnl_nd_opt_dnssl(opt, &lifetime);
1406 for (i = 0; domains && domains[i]; i++)
1407 connman_resolver_append_lifetime(index,
1408 domains[i], NULL, lifetime);
1415 static const char *type2string(uint16_t type)
1444 case RTM_NEWNDUSEROPT:
1445 return "NEWNDUSEROPT";
1451 static GIOChannel *channel = NULL;
1452 static guint channel_watch = 0;
1454 struct rtnl_request {
1455 struct nlmsghdr hdr;
1456 struct rtgenmsg msg;
1458 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1460 static GSList *request_list = NULL;
1461 static guint32 request_seq = 0;
1463 static struct rtnl_request *find_request(guint32 seq)
1467 for (list = request_list; list; list = list->next) {
1468 struct rtnl_request *req = list->data;
1470 if (req->hdr.nlmsg_seq == seq)
1477 static int send_request(struct rtnl_request *req)
1479 struct sockaddr_nl addr;
1482 DBG("%s len %d type %d flags 0x%04x seq %d",
1483 type2string(req->hdr.nlmsg_type),
1484 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1485 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1487 sk = g_io_channel_unix_get_fd(channel);
1489 memset(&addr, 0, sizeof(addr));
1490 addr.nl_family = AF_NETLINK;
1492 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1493 (struct sockaddr *) &addr, sizeof(addr));
1496 static int queue_request(struct rtnl_request *req)
1498 request_list = g_slist_append(request_list, req);
1500 if (g_slist_length(request_list) > 1)
1503 return send_request(req);
1506 static int process_response(guint32 seq)
1508 struct rtnl_request *req;
1512 req = find_request(seq);
1514 request_list = g_slist_remove(request_list, req);
1518 req = g_slist_nth_data(request_list, 0);
1522 return send_request(req);
1525 static void rtnl_message(void *buf, size_t len)
1528 struct nlmsghdr *hdr = buf;
1529 struct nlmsgerr *err;
1531 if (!NLMSG_OK(hdr, len))
1534 DBG("%s len %d type %d flags 0x%04x seq %d pid %d",
1535 type2string(hdr->nlmsg_type),
1536 hdr->nlmsg_len, hdr->nlmsg_type,
1537 hdr->nlmsg_flags, hdr->nlmsg_seq,
1540 switch (hdr->nlmsg_type) {
1545 process_response(hdr->nlmsg_seq);
1548 err = NLMSG_DATA(hdr);
1549 DBG("error %d (%s)", -err->error,
1550 strerror(-err->error));
1570 case RTM_NEWNDUSEROPT:
1571 rtnl_newnduseropt(hdr);
1575 len -= hdr->nlmsg_len;
1576 buf += hdr->nlmsg_len;
1580 static gboolean netlink_event(GIOChannel *chan, GIOCondition cond, gpointer data)
1582 unsigned char buf[4096];
1583 struct sockaddr_nl nladdr;
1584 socklen_t addr_len = sizeof(nladdr);
1588 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1591 memset(buf, 0, sizeof(buf));
1592 memset(&nladdr, 0, sizeof(nladdr));
1594 fd = g_io_channel_unix_get_fd(chan);
1596 status = recvfrom(fd, buf, sizeof(buf), 0,
1597 (struct sockaddr *) &nladdr, &addr_len);
1599 if (errno == EINTR || errno == EAGAIN)
1608 if (nladdr.nl_pid != 0) { /* not sent by kernel, ignore */
1609 DBG("Received msg from %u, ignoring it", nladdr.nl_pid);
1613 rtnl_message(buf, status);
1618 static int send_getlink(void)
1620 struct rtnl_request *req;
1624 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1628 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1629 req->hdr.nlmsg_type = RTM_GETLINK;
1630 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1631 req->hdr.nlmsg_pid = 0;
1632 req->hdr.nlmsg_seq = request_seq++;
1633 req->msg.rtgen_family = AF_INET;
1635 return queue_request(req);
1638 static int send_getaddr(void)
1640 struct rtnl_request *req;
1644 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1648 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1649 req->hdr.nlmsg_type = RTM_GETADDR;
1650 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1651 req->hdr.nlmsg_pid = 0;
1652 req->hdr.nlmsg_seq = request_seq++;
1653 req->msg.rtgen_family = AF_INET;
1655 return queue_request(req);
1658 static int send_getroute(void)
1660 struct rtnl_request *req;
1664 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1668 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1669 req->hdr.nlmsg_type = RTM_GETROUTE;
1670 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1671 req->hdr.nlmsg_pid = 0;
1672 req->hdr.nlmsg_seq = request_seq++;
1673 req->msg.rtgen_family = AF_INET;
1675 return queue_request(req);
1678 static gboolean update_timeout_cb(gpointer user_data)
1680 __connman_rtnl_request_update();
1685 static void update_interval_callback(guint min)
1687 if (update_timeout > 0)
1688 g_source_remove(update_timeout);
1690 if (min < G_MAXUINT) {
1691 update_interval = min;
1692 update_timeout = g_timeout_add_seconds(update_interval,
1693 update_timeout_cb, NULL);
1696 update_interval = G_MAXUINT;
1700 static gint compare_interval(gconstpointer a, gconstpointer b)
1702 guint val_a = GPOINTER_TO_UINT(a);
1703 guint val_b = GPOINTER_TO_UINT(b);
1705 return val_a - val_b;
1708 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1715 update_list = g_slist_insert_sorted(update_list,
1716 GUINT_TO_POINTER(interval), compare_interval);
1718 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1719 if (min < update_interval) {
1720 update_interval_callback(min);
1721 __connman_rtnl_request_update();
1724 return update_interval;
1727 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1729 guint min = G_MAXUINT;
1734 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1737 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1739 if (min > update_interval)
1740 update_interval_callback(min);
1745 int __connman_rtnl_request_update(void)
1747 return send_getlink();
1750 int __connman_rtnl_init(void)
1752 struct sockaddr_nl addr;
1757 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1758 NULL, free_interface);
1760 sk = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
1764 memset(&addr, 0, sizeof(addr));
1765 addr.nl_family = AF_NETLINK;
1766 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1767 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1768 (1<<(RTNLGRP_ND_USEROPT-1));
1770 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1775 channel = g_io_channel_unix_new(sk);
1776 g_io_channel_set_close_on_unref(channel, TRUE);
1778 g_io_channel_set_encoding(channel, NULL, NULL);
1779 g_io_channel_set_buffered(channel, FALSE);
1781 channel_watch = g_io_add_watch(channel,
1782 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1783 netlink_event, NULL);
1788 void __connman_rtnl_start(void)
1797 void __connman_rtnl_cleanup(void)
1803 for (list = watch_list; list; list = list->next) {
1804 struct watch_data *watch = list->data;
1806 DBG("removing watch %d", watch->id);
1812 g_slist_free(watch_list);
1815 g_slist_free(update_list);
1818 for (list = request_list; list; list = list->next) {
1819 struct rtnl_request *req = list->data;
1821 DBG("%s len %d type %d flags 0x%04x seq %d",
1822 type2string(req->hdr.nlmsg_type),
1823 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1824 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1830 g_slist_free(request_list);
1831 request_list = NULL;
1833 if (channel_watch) {
1834 g_source_remove(channel_watch);
1838 g_io_channel_shutdown(channel, TRUE, NULL);
1839 g_io_channel_unref(channel);
1843 g_hash_table_destroy(interface_list);