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);
596 GSList *next = list->next;
597 struct watch_data *watch = list->data;
599 if (watch->index == index && watch->newlink)
600 watch->newlink(flags, change, watch->user_data);
606 static void process_dellink(unsigned short type, int index, unsigned flags,
607 unsigned change, struct ifinfomsg *msg, int bytes)
609 struct rtnl_link_stats stats;
610 unsigned char operstate = 0xff;
611 const char *ifname = NULL;
614 memset(&stats, 0, sizeof(stats));
615 if (!extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats))
618 if (operstate != 0xff)
619 connman_info("%s {dellink} index %d operstate %u <%s>",
620 ifname, index, operstate,
621 operstate2str(operstate));
623 for (list = rtnl_list; list; list = list->next) {
624 struct connman_rtnl *rtnl = list->data;
627 rtnl->dellink(type, index, flags, change);
632 case ARPHRD_LOOPBACK:
633 case ARPHDR_PHONET_PIPE:
636 #if defined TIZEN_EXT
638 * Description: SLP requires ARPHRD_PPP for PPP type device
639 * ARPHDR_RMNET for QC modem using QMI
643 __connman_ipconfig_dellink(index, &stats);
647 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
650 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
652 struct in_addr *local,
653 struct in_addr *address,
654 struct in_addr *broadcast)
658 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
659 attr = RTA_NEXT(attr, bytes)) {
660 switch (attr->rta_type) {
663 *address = *((struct in_addr *) RTA_DATA(attr));
667 *local = *((struct in_addr *) RTA_DATA(attr));
671 *broadcast = *((struct in_addr *) RTA_DATA(attr));
675 *label = RTA_DATA(attr);
681 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
682 struct in6_addr *addr,
683 struct in6_addr *local)
687 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
688 attr = RTA_NEXT(attr, bytes)) {
689 switch (attr->rta_type) {
692 *addr = *((struct in6_addr *) RTA_DATA(attr));
696 *local = *((struct in6_addr *) RTA_DATA(attr));
702 static void process_newaddr(unsigned char family, unsigned char prefixlen,
703 int index, struct ifaddrmsg *msg, int bytes)
705 struct in_addr ipv4_addr = { INADDR_ANY };
706 struct in6_addr ipv6_address, ipv6_local;
707 const char *label = NULL;
709 char ip_string[INET6_ADDRSTRLEN];
711 if (family == AF_INET) {
713 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
715 } else if (family == AF_INET6) {
716 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
717 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
725 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
728 if (__connman_ipconfig_newaddr(index, family, label,
729 prefixlen, ip_string) >= 0) {
730 if (family == AF_INET6) {
732 * Re-create RDNSS configured servers if there
733 * are any for this interface. This is done
734 * because we might have now properly
735 * configured interface with proper
736 * autoconfigured address.
738 __connman_resolver_redo_servers(index);
743 static void process_deladdr(unsigned char family, unsigned char prefixlen,
744 int index, struct ifaddrmsg *msg, int bytes)
746 struct in_addr ipv4_addr = { INADDR_ANY };
747 struct in6_addr ipv6_address, ipv6_local;
748 const char *label = NULL;
750 char ip_string[INET6_ADDRSTRLEN];
752 if (family == AF_INET) {
753 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
755 } else if (family == AF_INET6) {
756 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
757 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
765 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
768 __connman_ipconfig_deladdr(index, family, label,
769 prefixlen, ip_string);
772 static void extract_ipv4_route(struct rtmsg *msg, int bytes, int *index,
774 struct in_addr *gateway)
778 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
779 attr = RTA_NEXT(attr, bytes)) {
780 switch (attr->rta_type) {
783 *dst = *((struct in_addr *) RTA_DATA(attr));
787 *gateway = *((struct in_addr *) RTA_DATA(attr));
791 *index = *((int *) RTA_DATA(attr));
797 static void extract_ipv6_route(struct rtmsg *msg, int bytes, int *index,
798 struct in6_addr *dst,
799 struct in6_addr *gateway)
803 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
804 attr = RTA_NEXT(attr, bytes)) {
805 switch (attr->rta_type) {
808 *dst = *((struct in6_addr *) RTA_DATA(attr));
813 *((struct in6_addr *) RTA_DATA(attr));
817 *index = *((int *) RTA_DATA(attr));
823 static void process_newroute(unsigned char family, unsigned char scope,
824 struct rtmsg *msg, int bytes)
827 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
830 if (family == AF_INET) {
831 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
833 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
835 inet_ntop(family, &dst, dststr, sizeof(dststr));
836 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
838 __connman_ipconfig_newroute(index, family, scope, dststr,
841 /* skip host specific routes */
842 if (scope != RT_SCOPE_UNIVERSE &&
843 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
846 if (dst.s_addr != INADDR_ANY)
849 } else if (family == AF_INET6) {
850 struct in6_addr dst = IN6ADDR_ANY_INIT,
851 gateway = IN6ADDR_ANY_INIT;
853 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
855 inet_ntop(family, &dst, dststr, sizeof(dststr));
856 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
858 __connman_ipconfig_newroute(index, family, scope, dststr,
861 /* skip host specific routes */
862 if (scope != RT_SCOPE_UNIVERSE &&
863 !(scope == RT_SCOPE_LINK &&
864 IN6_IS_ADDR_UNSPECIFIED(&dst)))
867 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
872 for (list = rtnl_list; list; list = list->next) {
873 struct connman_rtnl *rtnl = list->data;
875 if (rtnl->newgateway)
876 rtnl->newgateway(index, gatewaystr);
880 static void process_delroute(unsigned char family, unsigned char scope,
881 struct rtmsg *msg, int bytes)
884 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
887 if (family == AF_INET) {
888 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
890 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
892 inet_ntop(family, &dst, dststr, sizeof(dststr));
893 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
895 __connman_ipconfig_delroute(index, family, scope, dststr,
898 /* skip host specific routes */
899 if (scope != RT_SCOPE_UNIVERSE &&
900 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
903 if (dst.s_addr != INADDR_ANY)
906 } else if (family == AF_INET6) {
907 struct in6_addr dst = IN6ADDR_ANY_INIT,
908 gateway = IN6ADDR_ANY_INIT;
910 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
912 inet_ntop(family, &dst, dststr, sizeof(dststr));
913 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
915 __connman_ipconfig_delroute(index, family, scope, dststr,
918 /* skip host specific routes */
919 if (scope != RT_SCOPE_UNIVERSE &&
920 !(scope == RT_SCOPE_LINK &&
921 IN6_IS_ADDR_UNSPECIFIED(&dst)))
924 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
929 for (list = rtnl_list; list; list = list->next) {
930 struct connman_rtnl *rtnl = list->data;
932 if (rtnl->delgateway)
933 rtnl->delgateway(index, gatewaystr);
937 static inline void print_ether(struct rtattr *attr, const char *name)
939 int len = (int) RTA_PAYLOAD(attr);
941 if (len == ETH_ALEN) {
942 struct ether_addr eth;
943 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
944 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
946 print(" attr %s (len %d)\n", name, len);
949 static inline void print_inet(struct rtattr *attr, const char *name,
950 unsigned char family)
952 int len = (int) RTA_PAYLOAD(attr);
954 if (family == AF_INET && len == sizeof(struct in_addr)) {
956 addr = *((struct in_addr *) RTA_DATA(attr));
957 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
959 print(" attr %s (len %d)\n", name, len);
962 static inline void print_string(struct rtattr *attr, const char *name)
964 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
965 (char *) RTA_DATA(attr));
968 static inline void print_byte(struct rtattr *attr, const char *name)
970 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
971 *((unsigned char *) RTA_DATA(attr)));
974 static inline void print_integer(struct rtattr *attr, const char *name)
976 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
977 *((int *) RTA_DATA(attr)));
980 static inline void print_attr(struct rtattr *attr, const char *name)
982 int len = (int) RTA_PAYLOAD(attr);
985 print(" attr %s (len %d)\n", name, len);
987 print(" attr %d (len %d)\n", attr->rta_type, len);
990 static void rtnl_link(struct nlmsghdr *hdr)
992 struct ifinfomsg *msg;
996 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
997 bytes = IFLA_PAYLOAD(hdr);
999 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
1001 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
1002 attr = RTA_NEXT(attr, bytes)) {
1003 switch (attr->rta_type) {
1005 print_ether(attr, "address");
1007 case IFLA_BROADCAST:
1008 print_ether(attr, "broadcast");
1011 print_string(attr, "ifname");
1014 print_integer(attr, "mtu");
1017 print_attr(attr, "link");
1020 print_attr(attr, "qdisc");
1023 print_attr(attr, "stats");
1026 print_attr(attr, "cost");
1029 print_attr(attr, "priority");
1032 print_attr(attr, "master");
1035 print_attr(attr, "wireless");
1038 print_attr(attr, "protinfo");
1041 print_integer(attr, "txqlen");
1044 print_attr(attr, "map");
1047 print_attr(attr, "weight");
1049 case IFLA_OPERSTATE:
1050 print_byte(attr, "operstate");
1053 print_byte(attr, "linkmode");
1056 print_attr(attr, NULL);
1062 static void rtnl_newlink(struct nlmsghdr *hdr)
1064 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
1068 if (hdr->nlmsg_type == IFLA_WIRELESS)
1069 connman_warn_once("Obsolete WEXT WiFi driver detected");
1071 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
1072 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
1075 static void rtnl_dellink(struct nlmsghdr *hdr)
1077 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
1081 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
1082 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
1085 static void rtnl_addr(struct nlmsghdr *hdr)
1087 struct ifaddrmsg *msg;
1088 struct rtattr *attr;
1091 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1092 bytes = IFA_PAYLOAD(hdr);
1094 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
1096 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
1097 attr = RTA_NEXT(attr, bytes)) {
1098 switch (attr->rta_type) {
1100 print_inet(attr, "address", msg->ifa_family);
1103 print_inet(attr, "local", msg->ifa_family);
1106 print_string(attr, "label");
1109 print_inet(attr, "broadcast", msg->ifa_family);
1112 print_attr(attr, "anycast");
1115 print_attr(attr, "cacheinfo");
1118 print_attr(attr, "multicast");
1121 print_attr(attr, NULL);
1127 static void rtnl_newaddr(struct nlmsghdr *hdr)
1129 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1133 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1134 msg, IFA_PAYLOAD(hdr));
1137 static void rtnl_deladdr(struct nlmsghdr *hdr)
1139 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1143 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1144 msg, IFA_PAYLOAD(hdr));
1147 static void rtnl_route(struct nlmsghdr *hdr)
1150 struct rtattr *attr;
1153 msg = (struct rtmsg *) NLMSG_DATA(hdr);
1154 bytes = RTM_PAYLOAD(hdr);
1156 print("rtm_family %d rtm_table %d rtm_protocol %d",
1157 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
1158 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
1159 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
1161 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
1162 attr = RTA_NEXT(attr, bytes)) {
1163 switch (attr->rta_type) {
1165 print_inet(attr, "dst", msg->rtm_family);
1168 print_inet(attr, "src", msg->rtm_family);
1171 print_string(attr, "iif");
1174 print_integer(attr, "oif");
1177 print_inet(attr, "gateway", msg->rtm_family);
1180 print_attr(attr, "priority");
1183 print_inet(attr, "prefsrc", msg->rtm_family);
1186 print_attr(attr, "metrics");
1189 print_integer(attr, "table");
1192 print_attr(attr, NULL);
1198 static bool is_route_rtmsg(struct rtmsg *msg)
1200 if (msg->rtm_flags & RTM_F_CLONED)
1203 if (msg->rtm_table != RT_TABLE_MAIN)
1206 if (msg->rtm_protocol != RTPROT_BOOT &&
1207 msg->rtm_protocol != RTPROT_KERNEL)
1210 if (msg->rtm_type != RTN_UNICAST)
1216 static void rtnl_newroute(struct nlmsghdr *hdr)
1218 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1222 if (is_route_rtmsg(msg))
1223 process_newroute(msg->rtm_family, msg->rtm_scope,
1224 msg, RTM_PAYLOAD(hdr));
1227 static void rtnl_delroute(struct nlmsghdr *hdr)
1229 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1233 if (is_route_rtmsg(msg))
1234 process_delroute(msg->rtm_family, msg->rtm_scope,
1235 msg, RTM_PAYLOAD(hdr));
1238 static void *rtnl_nd_opt_rdnss(struct nd_opt_hdr *opt, guint32 *lifetime,
1241 guint32 *optint = (void *)opt;
1243 if (opt->nd_opt_len < 3)
1246 if (*lifetime > ntohl(optint[1]))
1247 *lifetime = ntohl(optint[1]);
1249 /* nd_opt_len is in units of 8 bytes. The header is 1 unit (8 bytes)
1250 and each address is another 2 units (16 bytes).
1251 So the number of addresses (given rounding) is nd_opt_len/2 */
1252 *nr_servers = opt->nd_opt_len / 2;
1254 /* And they start 8 bytes into the packet, or two guint32s in. */
1258 static const char **rtnl_nd_opt_dnssl(struct nd_opt_hdr *opt, guint32 *lifetime)
1260 const char **domains = NULL;
1261 guint32 *optint = (void *)opt;
1262 unsigned char *optc = (void *)&optint[2];
1263 int data_len = (opt->nd_opt_len * 8) - 8;
1267 if (*lifetime > ntohl(optint[1]))
1268 *lifetime = ntohl(optint[1]);
1270 /* Turn it into normal strings by converting the length bytes into '.',
1271 and count how many search domains there are while we're at it. */
1273 while (i < data_len) {
1274 if (optc[i] > 0x3f) {
1275 DBG("DNSSL contains compressed elements in violation of RFC6106");
1282 /* Check for double zero */
1283 if (i < data_len && optc[i] == 0)
1291 if (i >= data_len) {
1292 DBG("DNSSL data overflows option length");
1299 domains = g_try_new0(const char *, nr_domains + 1);
1303 /* Now point to the normal strings, missing out the leading '.' that
1304 each of them will have now. */
1305 for (i = 0; i < nr_domains; i++) {
1306 domains[i] = (char *)optc + 1;
1307 optc += strlen((char *)optc) + 1;
1313 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1315 struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1316 struct nd_opt_hdr *opt;
1317 guint32 lifetime = -1;
1318 const char **domains = NULL;
1319 struct in6_addr *servers = NULL;
1320 int i, nr_servers = 0;
1321 int msglen = msg->nduseropt_opts_len;
1324 DBG("family %d index %d len %d type %d code %d",
1325 msg->nduseropt_family, msg->nduseropt_ifindex,
1326 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1327 msg->nduseropt_icmp_code);
1329 if (msg->nduseropt_family != AF_INET6 ||
1330 msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1331 msg->nduseropt_icmp_code != 0)
1334 index = msg->nduseropt_ifindex;
1338 #if defined TIZEN_EXT
1339 struct connman_service *service;
1340 enum connman_service_state state;
1341 enum connman_dnsconfig_method ipv6_dns_method;
1343 service = __connman_service_lookup_from_index(index);
1345 DBG("Invalid service");
1349 DBG("service: %p index: %d\n", service, index);
1351 if (connman_setting_get_bool("SingleConnectedTechnology") == TRUE) {
1352 state = __connman_service_ipconfig_get_state(service, CONNMAN_IPCONFIG_TYPE_IPV6);
1353 if (state != CONNMAN_SERVICE_STATE_ASSOCIATION &&
1354 state != CONNMAN_SERVICE_STATE_CONFIGURATION &&
1355 state != CONNMAN_SERVICE_STATE_READY &&
1356 state != CONNMAN_SERVICE_STATE_ONLINE) {
1357 DBG("Service state[%d] is not connecting/connected", state);
1362 ipv6_dns_method = connman_service_get_ipv6_dns_method(service);
1363 if (ipv6_dns_method != CONNMAN_DNSCONFIG_METHOD_DHCP) {
1364 DBG("IPv6 DNS method is not Auto ignore RA!!! [DNS method: %d]", ipv6_dns_method);
1369 for (opt = (void *)&msg[1];
1371 msglen -= opt->nd_opt_len * 8,
1372 opt = ((void *)opt) + opt->nd_opt_len*8) {
1374 DBG("remaining %d nd opt type %d len %d\n",
1375 msglen, opt->nd_opt_type, opt->nd_opt_len);
1377 if (opt->nd_opt_type == 25) { /* ND_OPT_RDNSS */
1379 #if defined TIZEN_EXT
1380 struct connman_service *service;
1382 service = __connman_service_lookup_from_index(index);
1383 DBG("service: %p\n",service);
1385 servers = rtnl_nd_opt_rdnss(opt, &lifetime,
1387 for (i = 0; i < nr_servers; i++) {
1388 if (!inet_ntop(AF_INET6, servers + i, buf,
1392 #if defined TIZEN_EXT
1393 __connman_service_nameserver_remove(service,
1395 CONNMAN_IPCONFIG_TYPE_IPV6);
1396 __connman_service_nameserver_append(service,
1398 CONNMAN_IPCONFIG_TYPE_IPV6);
1400 connman_resolver_append_lifetime(index,
1401 NULL, buf, lifetime);
1404 } else if (opt->nd_opt_type == 31) { /* ND_OPT_DNSSL */
1407 domains = rtnl_nd_opt_dnssl(opt, &lifetime);
1408 for (i = 0; domains && domains[i]; i++)
1409 connman_resolver_append_lifetime(index,
1410 domains[i], NULL, lifetime);
1417 static const char *type2string(uint16_t type)
1446 case RTM_NEWNDUSEROPT:
1447 return "NEWNDUSEROPT";
1453 static GIOChannel *channel = NULL;
1454 static guint channel_watch = 0;
1456 struct rtnl_request {
1457 struct nlmsghdr hdr;
1458 struct rtgenmsg msg;
1460 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1462 static GSList *request_list = NULL;
1463 static guint32 request_seq = 0;
1465 static struct rtnl_request *find_request(guint32 seq)
1469 for (list = request_list; list; list = list->next) {
1470 struct rtnl_request *req = list->data;
1472 if (req->hdr.nlmsg_seq == seq)
1479 static int send_request(struct rtnl_request *req)
1481 struct sockaddr_nl addr;
1484 DBG("%s len %d type %d flags 0x%04x seq %d",
1485 type2string(req->hdr.nlmsg_type),
1486 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1487 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1489 sk = g_io_channel_unix_get_fd(channel);
1491 memset(&addr, 0, sizeof(addr));
1492 addr.nl_family = AF_NETLINK;
1494 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1495 (struct sockaddr *) &addr, sizeof(addr));
1498 static int queue_request(struct rtnl_request *req)
1500 request_list = g_slist_append(request_list, req);
1502 if (g_slist_length(request_list) > 1)
1505 return send_request(req);
1508 static int process_response(guint32 seq)
1510 struct rtnl_request *req;
1514 req = find_request(seq);
1516 request_list = g_slist_remove(request_list, req);
1520 req = g_slist_nth_data(request_list, 0);
1524 return send_request(req);
1527 static void rtnl_message(void *buf, size_t len)
1530 struct nlmsghdr *hdr = buf;
1531 struct nlmsgerr *err;
1533 if (!NLMSG_OK(hdr, len))
1536 DBG("%s len %u type %u flags 0x%04x seq %u pid %u",
1537 type2string(hdr->nlmsg_type),
1538 hdr->nlmsg_len, hdr->nlmsg_type,
1539 hdr->nlmsg_flags, hdr->nlmsg_seq,
1542 switch (hdr->nlmsg_type) {
1547 process_response(hdr->nlmsg_seq);
1550 err = NLMSG_DATA(hdr);
1551 DBG("error %d (%s)", -err->error,
1552 strerror(-err->error));
1572 case RTM_NEWNDUSEROPT:
1573 rtnl_newnduseropt(hdr);
1577 len -= hdr->nlmsg_len;
1578 buf += hdr->nlmsg_len;
1582 static gboolean netlink_event(GIOChannel *chan, GIOCondition cond, gpointer data)
1584 unsigned char buf[4096];
1585 struct sockaddr_nl nladdr;
1586 socklen_t addr_len = sizeof(nladdr);
1590 #if defined TIZEN_EXT
1591 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
1592 __connman_rtnl_init(GIO_SOCKET_RETRY_COUNT);
1595 #else /* TIZEN_EXT */
1596 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1598 #endif /* TIZEN_EXT */
1600 memset(buf, 0, sizeof(buf));
1601 memset(&nladdr, 0, sizeof(nladdr));
1603 fd = g_io_channel_unix_get_fd(chan);
1605 status = recvfrom(fd, buf, sizeof(buf), 0,
1606 (struct sockaddr *) &nladdr, &addr_len);
1608 if (errno == EINTR || errno == EAGAIN)
1611 #if defined TIZEN_EXT
1612 __connman_rtnl_init(GIO_SOCKET_RETRY_COUNT);
1613 #endif /* TIZEN_EXT */
1617 #if defined TIZEN_EXT
1619 __connman_rtnl_init(GIO_SOCKET_RETRY_COUNT);
1622 #else /* TIZEN_EXT */
1625 #endif /* TIZEN_EXT */
1627 if (nladdr.nl_pid != 0) { /* not sent by kernel, ignore */
1628 DBG("Received msg from %u, ignoring it", nladdr.nl_pid);
1632 rtnl_message(buf, status);
1637 static int send_getlink(void)
1639 struct rtnl_request *req;
1643 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1647 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1648 req->hdr.nlmsg_type = RTM_GETLINK;
1649 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1650 req->hdr.nlmsg_pid = 0;
1651 req->hdr.nlmsg_seq = request_seq++;
1652 req->msg.rtgen_family = AF_INET;
1654 return queue_request(req);
1657 static int send_getaddr(void)
1659 struct rtnl_request *req;
1663 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1667 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1668 req->hdr.nlmsg_type = RTM_GETADDR;
1669 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1670 req->hdr.nlmsg_pid = 0;
1671 req->hdr.nlmsg_seq = request_seq++;
1672 req->msg.rtgen_family = AF_INET;
1674 return queue_request(req);
1677 static int send_getroute(void)
1679 struct rtnl_request *req;
1683 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1687 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1688 req->hdr.nlmsg_type = RTM_GETROUTE;
1689 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1690 req->hdr.nlmsg_pid = 0;
1691 req->hdr.nlmsg_seq = request_seq++;
1692 req->msg.rtgen_family = AF_INET;
1694 return queue_request(req);
1697 static gboolean update_timeout_cb(gpointer user_data)
1699 __connman_rtnl_request_update();
1704 static void update_interval_callback(guint min)
1706 if (update_timeout > 0)
1707 g_source_remove(update_timeout);
1709 if (min < G_MAXUINT) {
1710 update_interval = min;
1711 update_timeout = g_timeout_add_seconds(update_interval,
1712 update_timeout_cb, NULL);
1715 update_interval = G_MAXUINT;
1719 static gint compare_interval(gconstpointer a, gconstpointer b)
1721 guint val_a = GPOINTER_TO_UINT(a);
1722 guint val_b = GPOINTER_TO_UINT(b);
1724 return val_a - val_b;
1727 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1734 update_list = g_slist_insert_sorted(update_list,
1735 GUINT_TO_POINTER(interval), compare_interval);
1737 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1738 if (min < update_interval) {
1739 update_interval_callback(min);
1740 __connman_rtnl_request_update();
1743 return update_interval;
1746 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1748 guint min = G_MAXUINT;
1753 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1756 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1758 if (min > update_interval)
1759 update_interval_callback(min);
1764 int __connman_rtnl_request_update(void)
1766 return send_getlink();
1769 #if defined TIZEN_EXT
1770 int __connman_rtnl_init(int retry_count)
1771 #else /* TIZEN_EXT */
1772 int __connman_rtnl_init(void)
1773 #endif /* TIZEN_EXT */
1775 struct sockaddr_nl addr;
1778 #if defined TIZEN_EXT
1779 if (retry_count < 0)
1782 DBG("retry_count %d", retry_count);
1783 #else /* TIZEN_EXT */
1785 #endif /* TIZEN_EXT */
1787 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1788 NULL, free_interface);
1790 sk = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
1792 #if defined TIZEN_EXT
1793 return __connman_rtnl_init(retry_count - 1);
1794 #else /* TIZEN_EXT */
1796 #endif /* TIZEN_EXT */
1798 memset(&addr, 0, sizeof(addr));
1799 addr.nl_family = AF_NETLINK;
1800 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1801 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1802 (1<<(RTNLGRP_ND_USEROPT-1));
1804 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1806 #if defined TIZEN_EXT
1807 return __connman_rtnl_init(retry_count - 1);
1808 #else /* TIZEN_EXT */
1810 #endif /* TIZEN_EXT */
1813 channel = g_io_channel_unix_new(sk);
1814 g_io_channel_set_close_on_unref(channel, TRUE);
1816 g_io_channel_set_encoding(channel, NULL, NULL);
1817 g_io_channel_set_buffered(channel, FALSE);
1819 channel_watch = g_io_add_watch(channel,
1820 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1821 netlink_event, NULL);
1826 void __connman_rtnl_start(void)
1835 void __connman_rtnl_cleanup(void)
1841 for (list = watch_list; list; list = list->next) {
1842 struct watch_data *watch = list->data;
1844 DBG("removing watch %d", watch->id);
1850 g_slist_free(watch_list);
1853 g_slist_free(update_list);
1856 for (list = request_list; list; list = list->next) {
1857 struct rtnl_request *req = list->data;
1859 DBG("%s len %d type %d flags 0x%04x seq %d",
1860 type2string(req->hdr.nlmsg_type),
1861 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1862 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1868 g_slist_free(request_list);
1869 request_list = NULL;
1871 if (channel_watch) {
1872 g_source_remove(channel_watch);
1876 g_io_channel_shutdown(channel, TRUE, NULL);
1877 g_io_channel_unref(channel);
1881 g_hash_table_destroy(interface_list);