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;
220 } else if (strcmp(line + 8, "dsa") == 0) {
221 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
222 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
224 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
225 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
234 #if !defined TIZEN_EXT
235 /* TIZEN does not use old wext interface */
236 /* We haven't got a DEVTYPE, let's check if it's a wireless device */
237 if (wext_interface(name)) {
238 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
239 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
241 connman_error("%s runs an unsupported 802.11 driver", name);
249 enum connman_device_type __connman_rtnl_get_device_type(int index)
251 struct interface_data *interface;
253 interface = g_hash_table_lookup(interface_list,
254 GINT_TO_POINTER(index));
256 return CONNMAN_DEVICE_TYPE_UNKNOWN;
258 return interface->device_type;
262 * connman_rtnl_add_newlink_watch:
263 * @index: network device index
264 * @callback: callback function
265 * @user_data: callback data;
267 * Add a new RTNL watch for newlink events
269 * Returns: %0 on failure and a unique id on success
271 unsigned int connman_rtnl_add_newlink_watch(int index,
272 connman_rtnl_link_cb_t callback, void *user_data)
274 struct watch_data *watch;
276 watch = g_try_new0(struct watch_data, 1);
280 watch->id = ++watch_id;
281 watch->index = index;
283 watch->newlink = callback;
284 watch->user_data = user_data;
286 watch_list = g_slist_prepend(watch_list, watch);
288 DBG("id %d", watch->id);
291 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
294 callback(flags, 0, user_data);
301 * connman_rtnl_remove_watch:
302 * @id: watch identifier
304 * Remove the RTNL watch for the identifier
306 void connman_rtnl_remove_watch(unsigned int id)
315 for (list = watch_list; list; list = list->next) {
316 struct watch_data *watch = list->data;
318 if (watch->id == id) {
319 watch_list = g_slist_remove(watch_list, watch);
326 static void trigger_rtnl(int index, void *user_data)
328 struct connman_rtnl *rtnl = user_data;
331 unsigned short type = __connman_ipconfig_get_type_from_index(index);
332 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
334 rtnl->newlink(type, index, flags, 0);
337 if (rtnl->newgateway) {
338 const char *gateway =
339 __connman_ipconfig_get_gateway_from_index(index,
340 CONNMAN_IPCONFIG_TYPE_ALL);
343 rtnl->newgateway(index, gateway);
347 static GSList *rtnl_list = NULL;
349 static gint compare_priority(gconstpointer a, gconstpointer b)
351 const struct connman_rtnl *rtnl1 = a;
352 const struct connman_rtnl *rtnl2 = b;
354 return rtnl2->priority - rtnl1->priority;
358 * connman_rtnl_register:
361 * Register a new RTNL module
363 * Returns: %0 on success
365 int connman_rtnl_register(struct connman_rtnl *rtnl)
367 DBG("rtnl %p name %s", rtnl, rtnl->name);
369 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
372 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
378 * connman_rtnl_unregister:
381 * Remove a previously registered RTNL module
383 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
385 DBG("rtnl %p name %s", rtnl, rtnl->name);
387 rtnl_list = g_slist_remove(rtnl_list, rtnl);
390 static const char *operstate2str(unsigned char operstate)
393 case IF_OPER_UNKNOWN:
395 case IF_OPER_NOTPRESENT:
396 return "NOT-PRESENT";
399 case IF_OPER_LOWERLAYERDOWN:
400 return "LOWER-LAYER-DOWN";
401 case IF_OPER_TESTING:
403 case IF_OPER_DORMANT:
412 static bool extract_link(struct ifinfomsg *msg, int bytes,
413 struct ether_addr *address, const char **ifname,
414 unsigned int *mtu, unsigned char *operstate,
415 struct rtnl_link_stats *stats)
419 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
420 attr = RTA_NEXT(attr, bytes)) {
421 switch (attr->rta_type) {
424 memcpy(address, RTA_DATA(attr), ETH_ALEN);
428 *ifname = RTA_DATA(attr);
432 *mtu = *((unsigned int *) RTA_DATA(attr));
436 memcpy(stats, RTA_DATA(attr),
437 sizeof(struct rtnl_link_stats));
441 *operstate = *((unsigned char *) RTA_DATA(attr));
453 static void process_newlink(unsigned short type, int index, unsigned flags,
454 unsigned change, struct ifinfomsg *msg, int bytes)
456 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
457 struct rtnl_link_stats stats;
458 unsigned char operstate = 0xff;
459 struct interface_data *interface;
460 const char *ifname = NULL;
461 unsigned int mtu = 0;
462 char ident[13], str[18];
465 memset(&stats, 0, sizeof(stats));
466 if (!extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats))
469 #if defined TIZEN_EXT_WIFI_MESH
470 /* Do not accept Wi-Fi Mesh interface */
471 if (g_strrstr(ifname, "mesh") != NULL) {
472 DBG("Newlink event for Wi-Fi Mesh interface ignored");
476 /* Do not accept Wi-Fi WLAN1 interface "dedicated for softAP */
477 if (!g_strcmp0(ifname, "wlan1")) {
478 DBG("Newlink event for Wi-Fi WLAN1 interface ignored");
483 #if defined TIZEN_EXT
484 /* Do not accept Wi-Fi P2P interface */
485 if (g_strrstr(ifname, "p2p") != NULL) {
486 DBG("Newlink event for Wi-Fi P2P interface ignored");
491 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
492 address.ether_addr_octet[0],
493 address.ether_addr_octet[1],
494 address.ether_addr_octet[2],
495 address.ether_addr_octet[3],
496 address.ether_addr_octet[4],
497 address.ether_addr_octet[5]);
499 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
500 address.ether_addr_octet[0],
501 address.ether_addr_octet[1],
502 address.ether_addr_octet[2],
503 address.ether_addr_octet[3],
504 address.ether_addr_octet[4],
505 address.ether_addr_octet[5]);
507 if (flags & IFF_SLAVE) {
508 connman_info("%s {newlink} ignoring slave, index %d address %s",
514 if (TIZEN_TV_EXT && g_strcmp0(ident, "eeeeeeeeeeee") == 0) {
515 DBG("Newlink event with Dummy MAC. Ignored!");
522 case ARPHRD_LOOPBACK:
523 case ARPHDR_PHONET_PIPE:
526 #if defined TIZEN_EXT
528 * Description: ARPHDR_RMNET for QC modem using QMI
532 __connman_ipconfig_newlink(index, type, flags,
537 connman_info("%s {newlink} index %d address %s mtu %u",
538 ifname, index, str, mtu);
540 if (operstate != 0xff)
541 connman_info("%s {newlink} index %d operstate %u <%s>",
542 ifname, index, operstate,
543 operstate2str(operstate));
545 interface = g_hash_table_lookup(interface_list, GINT_TO_POINTER(index));
547 interface = g_new0(struct interface_data, 1);
548 interface->index = index;
549 interface->ident = g_strdup(ident);
551 g_hash_table_insert(interface_list,
552 GINT_TO_POINTER(index), interface);
554 if (type == ARPHRD_ETHER)
555 read_uevent(interface);
556 #if defined TIZEN_EXT
557 if (type == ARPHRD_PPP || type == ARPHDR_RMNET)
558 read_uevent(interface);
560 } else if (g_strcmp0(interface->ident, ident) != 0) {
561 /* If an original address is built-in physical device,
562 * it's hardly get an address at a initial creation
564 __connman_technology_remove_interface(interface->service_type,
565 interface->index, interface->ident);
567 g_free(interface->ident);
568 interface->ident = g_strdup(ident);
570 __connman_technology_add_interface(interface->service_type,
571 interface->index, interface->ident);
575 } else if (type == ARPHRD_ETHER && interface->device_type == CONNMAN_DEVICE_TYPE_UNKNOWN)
576 read_uevent(interface);
580 for (list = rtnl_list; list; list = list->next) {
581 struct connman_rtnl *rtnl = list->data;
584 rtnl->newlink(type, index, flags, change);
588 * The interface needs to be added after the newlink call.
589 * The newlink will create the technology when needed and
590 * __connman_technology_add_interface() expects the
591 * technology to be there already.
594 __connman_technology_add_interface(interface->service_type,
595 interface->index, interface->ident);
599 GSList *next = list->next;
600 struct watch_data *watch = list->data;
602 if (watch->index == index && watch->newlink)
603 watch->newlink(flags, change, watch->user_data);
609 static void process_dellink(unsigned short type, int index, unsigned flags,
610 unsigned change, struct ifinfomsg *msg, int bytes)
612 struct rtnl_link_stats stats;
613 unsigned char operstate = 0xff;
614 const char *ifname = NULL;
617 memset(&stats, 0, sizeof(stats));
618 if (!extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats))
621 if (operstate != 0xff)
622 connman_info("%s {dellink} index %d operstate %u <%s>",
623 ifname, index, operstate,
624 operstate2str(operstate));
626 for (list = rtnl_list; list; list = list->next) {
627 struct connman_rtnl *rtnl = list->data;
630 rtnl->dellink(type, index, flags, change);
635 case ARPHRD_LOOPBACK:
636 case ARPHDR_PHONET_PIPE:
639 #if defined TIZEN_EXT
641 * Description: SLP requires ARPHRD_PPP for PPP type device
642 * ARPHDR_RMNET for QC modem using QMI
646 __connman_ipconfig_dellink(index, &stats);
650 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
653 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
655 struct in_addr *local,
656 struct in_addr *address,
657 struct in_addr *broadcast)
661 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
662 attr = RTA_NEXT(attr, bytes)) {
663 switch (attr->rta_type) {
666 *address = *((struct in_addr *) RTA_DATA(attr));
670 *local = *((struct in_addr *) RTA_DATA(attr));
674 *broadcast = *((struct in_addr *) RTA_DATA(attr));
678 *label = RTA_DATA(attr);
684 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
685 struct in6_addr *addr,
686 struct in6_addr *local)
690 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
691 attr = RTA_NEXT(attr, bytes)) {
692 switch (attr->rta_type) {
695 *addr = *((struct in6_addr *) RTA_DATA(attr));
699 *local = *((struct in6_addr *) RTA_DATA(attr));
705 static void process_newaddr(unsigned char family, unsigned char prefixlen,
706 int index, struct ifaddrmsg *msg, int bytes)
708 struct in_addr ipv4_addr = { INADDR_ANY };
709 struct in6_addr ipv6_address, ipv6_local;
710 const char *label = NULL;
712 char ip_string[INET6_ADDRSTRLEN];
714 if (family == AF_INET) {
716 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
718 } else if (family == AF_INET6) {
719 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
720 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
728 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
731 if (__connman_ipconfig_newaddr(index, family, label,
732 prefixlen, ip_string) >= 0) {
733 if (family == AF_INET6) {
735 * Re-create RDNSS configured servers if there
736 * are any for this interface. This is done
737 * because we might have now properly
738 * configured interface with proper
739 * autoconfigured address.
741 __connman_resolver_redo_servers(index);
746 static void process_deladdr(unsigned char family, unsigned char prefixlen,
747 int index, struct ifaddrmsg *msg, int bytes)
749 struct in_addr ipv4_addr = { INADDR_ANY };
750 struct in6_addr ipv6_address, ipv6_local;
751 const char *label = NULL;
753 char ip_string[INET6_ADDRSTRLEN];
755 if (family == AF_INET) {
756 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
758 } else if (family == AF_INET6) {
759 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
760 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
768 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
771 __connman_ipconfig_deladdr(index, family, label,
772 prefixlen, ip_string);
775 static void extract_ipv4_route(struct rtmsg *msg, int bytes, int *index,
777 struct in_addr *gateway)
781 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
782 attr = RTA_NEXT(attr, bytes)) {
783 switch (attr->rta_type) {
786 *dst = *((struct in_addr *) RTA_DATA(attr));
790 *gateway = *((struct in_addr *) RTA_DATA(attr));
794 *index = *((int *) RTA_DATA(attr));
800 static void extract_ipv6_route(struct rtmsg *msg, int bytes, int *index,
801 struct in6_addr *dst,
802 struct in6_addr *gateway)
806 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
807 attr = RTA_NEXT(attr, bytes)) {
808 switch (attr->rta_type) {
811 *dst = *((struct in6_addr *) RTA_DATA(attr));
816 *((struct in6_addr *) RTA_DATA(attr));
820 *index = *((int *) RTA_DATA(attr));
826 static void process_newroute(unsigned char family, unsigned char scope,
827 struct rtmsg *msg, int bytes)
830 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
833 if (family == AF_INET) {
834 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
836 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
838 inet_ntop(family, &dst, dststr, sizeof(dststr));
839 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
841 __connman_ipconfig_newroute(index, family, scope, dststr,
844 /* skip host specific routes */
845 if (scope != RT_SCOPE_UNIVERSE &&
846 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
849 if (dst.s_addr != INADDR_ANY)
852 } else if (family == AF_INET6) {
853 struct in6_addr dst = IN6ADDR_ANY_INIT,
854 gateway = IN6ADDR_ANY_INIT;
856 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
858 inet_ntop(family, &dst, dststr, sizeof(dststr));
859 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
861 __connman_ipconfig_newroute(index, family, scope, dststr,
864 /* skip host specific routes */
865 if (scope != RT_SCOPE_UNIVERSE &&
866 !(scope == RT_SCOPE_LINK &&
867 IN6_IS_ADDR_UNSPECIFIED(&dst)))
870 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
875 for (list = rtnl_list; list; list = list->next) {
876 struct connman_rtnl *rtnl = list->data;
878 if (rtnl->newgateway)
879 rtnl->newgateway(index, gatewaystr);
883 static void process_delroute(unsigned char family, unsigned char scope,
884 struct rtmsg *msg, int bytes)
887 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
890 if (family == AF_INET) {
891 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
893 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
895 inet_ntop(family, &dst, dststr, sizeof(dststr));
896 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
898 __connman_ipconfig_delroute(index, family, scope, dststr,
901 /* skip host specific routes */
902 if (scope != RT_SCOPE_UNIVERSE &&
903 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
906 if (dst.s_addr != INADDR_ANY)
909 } else if (family == AF_INET6) {
910 struct in6_addr dst = IN6ADDR_ANY_INIT,
911 gateway = IN6ADDR_ANY_INIT;
913 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
915 inet_ntop(family, &dst, dststr, sizeof(dststr));
916 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
918 __connman_ipconfig_delroute(index, family, scope, dststr,
921 /* skip host specific routes */
922 if (scope != RT_SCOPE_UNIVERSE &&
923 !(scope == RT_SCOPE_LINK &&
924 IN6_IS_ADDR_UNSPECIFIED(&dst)))
927 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
932 for (list = rtnl_list; list; list = list->next) {
933 struct connman_rtnl *rtnl = list->data;
935 if (rtnl->delgateway)
936 rtnl->delgateway(index, gatewaystr);
940 static inline void print_ether(struct rtattr *attr, const char *name)
942 int len = (int) RTA_PAYLOAD(attr);
944 if (len == ETH_ALEN) {
945 struct ether_addr eth;
946 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
947 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
949 print(" attr %s (len %d)\n", name, len);
952 static inline void print_inet(struct rtattr *attr, const char *name,
953 unsigned char family)
955 int len = (int) RTA_PAYLOAD(attr);
957 if (family == AF_INET && len == sizeof(struct in_addr)) {
959 addr = *((struct in_addr *) RTA_DATA(attr));
960 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
962 print(" attr %s (len %d)\n", name, len);
965 static inline void print_string(struct rtattr *attr, const char *name)
967 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
968 (char *) RTA_DATA(attr));
971 static inline void print_byte(struct rtattr *attr, const char *name)
973 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
974 *((unsigned char *) RTA_DATA(attr)));
977 static inline void print_integer(struct rtattr *attr, const char *name)
979 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
980 *((int *) RTA_DATA(attr)));
983 static inline void print_attr(struct rtattr *attr, const char *name)
985 int len = (int) RTA_PAYLOAD(attr);
988 print(" attr %s (len %d)\n", name, len);
990 print(" attr %d (len %d)\n", attr->rta_type, len);
993 static void rtnl_link(struct nlmsghdr *hdr)
995 struct ifinfomsg *msg;
999 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
1000 bytes = IFLA_PAYLOAD(hdr);
1002 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
1004 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
1005 attr = RTA_NEXT(attr, bytes)) {
1006 switch (attr->rta_type) {
1008 print_ether(attr, "address");
1010 case IFLA_BROADCAST:
1011 print_ether(attr, "broadcast");
1014 print_string(attr, "ifname");
1017 print_integer(attr, "mtu");
1020 print_attr(attr, "link");
1023 print_attr(attr, "qdisc");
1026 print_attr(attr, "stats");
1029 print_attr(attr, "cost");
1032 print_attr(attr, "priority");
1035 print_attr(attr, "master");
1038 print_attr(attr, "wireless");
1041 print_attr(attr, "protinfo");
1044 print_integer(attr, "txqlen");
1047 print_attr(attr, "map");
1050 print_attr(attr, "weight");
1052 case IFLA_OPERSTATE:
1053 print_byte(attr, "operstate");
1056 print_byte(attr, "linkmode");
1059 print_attr(attr, NULL);
1065 static void rtnl_newlink(struct nlmsghdr *hdr)
1067 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
1071 if (hdr->nlmsg_type == IFLA_WIRELESS)
1072 connman_warn_once("Obsolete WEXT WiFi driver detected");
1074 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
1075 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
1078 static void rtnl_dellink(struct nlmsghdr *hdr)
1080 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
1084 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
1085 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
1088 static void rtnl_addr(struct nlmsghdr *hdr)
1090 struct ifaddrmsg *msg;
1091 struct rtattr *attr;
1094 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1095 bytes = IFA_PAYLOAD(hdr);
1097 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
1099 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
1100 attr = RTA_NEXT(attr, bytes)) {
1101 switch (attr->rta_type) {
1103 print_inet(attr, "address", msg->ifa_family);
1106 print_inet(attr, "local", msg->ifa_family);
1109 print_string(attr, "label");
1112 print_inet(attr, "broadcast", msg->ifa_family);
1115 print_attr(attr, "anycast");
1118 print_attr(attr, "cacheinfo");
1121 print_attr(attr, "multicast");
1124 print_attr(attr, NULL);
1130 static void rtnl_newaddr(struct nlmsghdr *hdr)
1132 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1136 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1137 msg, IFA_PAYLOAD(hdr));
1140 static void rtnl_deladdr(struct nlmsghdr *hdr)
1142 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1146 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1147 msg, IFA_PAYLOAD(hdr));
1150 static void rtnl_route(struct nlmsghdr *hdr)
1153 struct rtattr *attr;
1156 msg = (struct rtmsg *) NLMSG_DATA(hdr);
1157 bytes = RTM_PAYLOAD(hdr);
1159 print("rtm_family %d rtm_table %d rtm_protocol %d",
1160 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
1161 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
1162 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
1164 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
1165 attr = RTA_NEXT(attr, bytes)) {
1166 switch (attr->rta_type) {
1168 print_inet(attr, "dst", msg->rtm_family);
1171 print_inet(attr, "src", msg->rtm_family);
1174 print_string(attr, "iif");
1177 print_integer(attr, "oif");
1180 print_inet(attr, "gateway", msg->rtm_family);
1183 print_attr(attr, "priority");
1186 print_inet(attr, "prefsrc", msg->rtm_family);
1189 print_attr(attr, "metrics");
1192 print_integer(attr, "table");
1195 print_attr(attr, NULL);
1201 static bool is_route_rtmsg(struct rtmsg *msg)
1203 if (msg->rtm_flags & RTM_F_CLONED)
1206 if (msg->rtm_table != RT_TABLE_MAIN)
1209 if (msg->rtm_protocol != RTPROT_BOOT &&
1210 msg->rtm_protocol != RTPROT_KERNEL)
1213 if (msg->rtm_type != RTN_UNICAST)
1219 static void rtnl_newroute(struct nlmsghdr *hdr)
1221 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1225 if (is_route_rtmsg(msg))
1226 process_newroute(msg->rtm_family, msg->rtm_scope,
1227 msg, RTM_PAYLOAD(hdr));
1230 static void rtnl_delroute(struct nlmsghdr *hdr)
1232 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1236 if (is_route_rtmsg(msg))
1237 process_delroute(msg->rtm_family, msg->rtm_scope,
1238 msg, RTM_PAYLOAD(hdr));
1241 static void *rtnl_nd_opt_rdnss(struct nd_opt_hdr *opt, guint32 *lifetime,
1244 guint32 *optint = (void *)opt;
1246 if (opt->nd_opt_len < 3)
1249 if (*lifetime > ntohl(optint[1]))
1250 *lifetime = ntohl(optint[1]);
1252 /* nd_opt_len is in units of 8 bytes. The header is 1 unit (8 bytes)
1253 and each address is another 2 units (16 bytes).
1254 So the number of addresses (given rounding) is nd_opt_len/2 */
1255 *nr_servers = opt->nd_opt_len / 2;
1257 /* And they start 8 bytes into the packet, or two guint32s in. */
1261 static const char **rtnl_nd_opt_dnssl(struct nd_opt_hdr *opt, guint32 *lifetime)
1263 const char **domains = NULL;
1264 guint32 *optint = (void *)opt;
1265 unsigned char *optc = (void *)&optint[2];
1266 int data_len = (opt->nd_opt_len * 8) - 8;
1270 if (*lifetime > ntohl(optint[1]))
1271 *lifetime = ntohl(optint[1]);
1273 /* Turn it into normal strings by converting the length bytes into '.',
1274 and count how many search domains there are while we're at it. */
1276 while (i < data_len) {
1277 if (optc[i] > 0x3f) {
1278 DBG("DNSSL contains compressed elements in violation of RFC6106");
1285 /* Check for double zero */
1286 if (i < data_len && optc[i] == 0)
1294 if (i >= data_len) {
1295 DBG("DNSSL data overflows option length");
1302 domains = g_try_new0(const char *, nr_domains + 1);
1306 /* Now point to the normal strings, missing out the leading '.' that
1307 each of them will have now. */
1308 for (i = 0; i < nr_domains; i++) {
1309 domains[i] = (char *)optc + 1;
1310 optc += strlen((char *)optc) + 1;
1316 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1318 struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1319 struct nd_opt_hdr *opt;
1320 guint32 lifetime = -1;
1321 const char **domains = NULL;
1322 struct in6_addr *servers = NULL;
1323 int i, nr_servers = 0;
1324 int msglen = msg->nduseropt_opts_len;
1327 DBG("family %d index %d len %d type %d code %d",
1328 msg->nduseropt_family, msg->nduseropt_ifindex,
1329 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1330 msg->nduseropt_icmp_code);
1332 if (msg->nduseropt_family != AF_INET6 ||
1333 msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1334 msg->nduseropt_icmp_code != 0)
1337 index = msg->nduseropt_ifindex;
1341 #if defined TIZEN_EXT
1342 struct connman_service *service;
1343 enum connman_service_state state;
1344 enum connman_dnsconfig_method ipv6_dns_method;
1346 service = __connman_service_lookup_from_index(index);
1348 DBG("Invalid service");
1352 DBG("service: %p index: %d\n", service, index);
1354 if (connman_setting_get_bool("SingleConnectedTechnology") == TRUE) {
1355 state = __connman_service_ipconfig_get_state(service, CONNMAN_IPCONFIG_TYPE_IPV6);
1356 if (state != CONNMAN_SERVICE_STATE_ASSOCIATION &&
1357 state != CONNMAN_SERVICE_STATE_CONFIGURATION &&
1358 state != CONNMAN_SERVICE_STATE_READY &&
1359 state != CONNMAN_SERVICE_STATE_ONLINE) {
1360 DBG("Service state[%d] is not connecting/connected", state);
1365 ipv6_dns_method = connman_service_get_ipv6_dns_method(service);
1366 if (ipv6_dns_method != CONNMAN_DNSCONFIG_METHOD_DHCP) {
1367 DBG("IPv6 DNS method is not Auto ignore RA!!! [DNS method: %d]", ipv6_dns_method);
1372 for (opt = (void *)&msg[1];
1374 msglen -= opt->nd_opt_len * 8,
1375 opt = ((void *)opt) + opt->nd_opt_len*8) {
1377 DBG("remaining %d nd opt type %d len %d\n",
1378 msglen, opt->nd_opt_type, opt->nd_opt_len);
1380 if (opt->nd_opt_type == 25) { /* ND_OPT_RDNSS */
1382 #if defined TIZEN_EXT
1383 struct connman_service *service;
1385 service = __connman_service_lookup_from_index(index);
1386 DBG("service: %p\n",service);
1388 servers = rtnl_nd_opt_rdnss(opt, &lifetime,
1390 for (i = 0; i < nr_servers; i++) {
1391 if (!inet_ntop(AF_INET6, servers + i, buf,
1395 #if defined TIZEN_EXT
1396 __connman_service_nameserver_remove(service,
1398 CONNMAN_IPCONFIG_TYPE_IPV6);
1399 __connman_service_nameserver_append(service,
1401 CONNMAN_IPCONFIG_TYPE_IPV6);
1403 connman_resolver_append_lifetime(index,
1404 NULL, buf, lifetime);
1407 } else if (opt->nd_opt_type == 31) { /* ND_OPT_DNSSL */
1410 domains = rtnl_nd_opt_dnssl(opt, &lifetime);
1411 for (i = 0; domains && domains[i]; i++)
1412 connman_resolver_append_lifetime(index,
1413 domains[i], NULL, lifetime);
1420 static const char *type2string(uint16_t type)
1449 case RTM_NEWNDUSEROPT:
1450 return "NEWNDUSEROPT";
1456 static GIOChannel *channel = NULL;
1457 static guint channel_watch = 0;
1459 struct rtnl_request {
1460 struct nlmsghdr hdr;
1461 struct rtgenmsg msg;
1463 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1465 static GSList *request_list = NULL;
1466 static guint32 request_seq = 0;
1468 static struct rtnl_request *find_request(guint32 seq)
1472 for (list = request_list; list; list = list->next) {
1473 struct rtnl_request *req = list->data;
1475 if (req->hdr.nlmsg_seq == seq)
1482 static int send_request(struct rtnl_request *req)
1484 struct sockaddr_nl addr;
1487 DBG("%s len %d type %d flags 0x%04x seq %d",
1488 type2string(req->hdr.nlmsg_type),
1489 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1490 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1492 sk = g_io_channel_unix_get_fd(channel);
1494 memset(&addr, 0, sizeof(addr));
1495 addr.nl_family = AF_NETLINK;
1497 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1498 (struct sockaddr *) &addr, sizeof(addr));
1501 static int queue_request(struct rtnl_request *req)
1503 request_list = g_slist_append(request_list, req);
1505 if (g_slist_length(request_list) > 1)
1508 return send_request(req);
1511 static int process_response(guint32 seq)
1513 struct rtnl_request *req;
1517 req = find_request(seq);
1519 request_list = g_slist_remove(request_list, req);
1523 req = g_slist_nth_data(request_list, 0);
1527 return send_request(req);
1530 static void rtnl_message(void *buf, size_t len)
1533 struct nlmsghdr *hdr = buf;
1534 struct nlmsgerr *err;
1536 if (!NLMSG_OK(hdr, len))
1539 DBG("%s len %u type %u flags 0x%04x seq %u pid %u",
1540 type2string(hdr->nlmsg_type),
1541 hdr->nlmsg_len, hdr->nlmsg_type,
1542 hdr->nlmsg_flags, hdr->nlmsg_seq,
1545 switch (hdr->nlmsg_type) {
1550 process_response(hdr->nlmsg_seq);
1553 err = NLMSG_DATA(hdr);
1554 DBG("error %d (%s)", -err->error,
1555 strerror(-err->error));
1575 case RTM_NEWNDUSEROPT:
1576 rtnl_newnduseropt(hdr);
1580 len -= hdr->nlmsg_len;
1581 buf += hdr->nlmsg_len;
1585 static gboolean netlink_event(GIOChannel *chan, GIOCondition cond, gpointer data)
1587 unsigned char buf[4096];
1588 struct sockaddr_nl nladdr;
1589 socklen_t addr_len = sizeof(nladdr);
1593 #if defined TIZEN_EXT
1594 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
1595 __connman_rtnl_init(GIO_SOCKET_RETRY_COUNT);
1598 #else /* TIZEN_EXT */
1599 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1601 #endif /* TIZEN_EXT */
1603 memset(buf, 0, sizeof(buf));
1604 memset(&nladdr, 0, sizeof(nladdr));
1606 fd = g_io_channel_unix_get_fd(chan);
1608 status = recvfrom(fd, buf, sizeof(buf), 0,
1609 (struct sockaddr *) &nladdr, &addr_len);
1611 if (errno == EINTR || errno == EAGAIN)
1614 #if defined TIZEN_EXT
1615 __connman_rtnl_init(GIO_SOCKET_RETRY_COUNT);
1616 #endif /* TIZEN_EXT */
1620 #if defined TIZEN_EXT
1622 __connman_rtnl_init(GIO_SOCKET_RETRY_COUNT);
1625 #else /* TIZEN_EXT */
1628 #endif /* TIZEN_EXT */
1630 if (nladdr.nl_pid != 0) { /* not sent by kernel, ignore */
1631 DBG("Received msg from %u, ignoring it", nladdr.nl_pid);
1635 rtnl_message(buf, status);
1640 static int send_getlink(void)
1642 struct rtnl_request *req;
1646 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1650 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1651 req->hdr.nlmsg_type = RTM_GETLINK;
1652 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1653 req->hdr.nlmsg_pid = 0;
1654 req->hdr.nlmsg_seq = request_seq++;
1655 req->msg.rtgen_family = AF_INET;
1657 return queue_request(req);
1660 static int send_getaddr(void)
1662 struct rtnl_request *req;
1666 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1670 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1671 req->hdr.nlmsg_type = RTM_GETADDR;
1672 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1673 req->hdr.nlmsg_pid = 0;
1674 req->hdr.nlmsg_seq = request_seq++;
1675 req->msg.rtgen_family = AF_INET;
1677 return queue_request(req);
1680 static int send_getroute(void)
1682 struct rtnl_request *req;
1686 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1690 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1691 req->hdr.nlmsg_type = RTM_GETROUTE;
1692 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1693 req->hdr.nlmsg_pid = 0;
1694 req->hdr.nlmsg_seq = request_seq++;
1695 req->msg.rtgen_family = AF_INET;
1697 return queue_request(req);
1700 static gboolean update_timeout_cb(gpointer user_data)
1702 __connman_rtnl_request_update();
1707 static void update_interval_callback(guint min)
1709 if (update_timeout > 0)
1710 g_source_remove(update_timeout);
1712 if (min < G_MAXUINT) {
1713 update_interval = min;
1714 update_timeout = g_timeout_add_seconds(update_interval,
1715 update_timeout_cb, NULL);
1718 update_interval = G_MAXUINT;
1722 static gint compare_interval(gconstpointer a, gconstpointer b)
1724 guint val_a = GPOINTER_TO_UINT(a);
1725 guint val_b = GPOINTER_TO_UINT(b);
1727 return val_a - val_b;
1730 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1737 update_list = g_slist_insert_sorted(update_list,
1738 GUINT_TO_POINTER(interval), compare_interval);
1740 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1741 if (min < update_interval) {
1742 update_interval_callback(min);
1743 __connman_rtnl_request_update();
1746 return update_interval;
1749 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1751 guint min = G_MAXUINT;
1756 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1759 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1761 if (min > update_interval)
1762 update_interval_callback(min);
1767 int __connman_rtnl_request_update(void)
1769 return send_getlink();
1772 #if defined TIZEN_EXT
1773 int __connman_rtnl_init(int retry_count)
1774 #else /* TIZEN_EXT */
1775 int __connman_rtnl_init(void)
1776 #endif /* TIZEN_EXT */
1778 struct sockaddr_nl addr;
1781 #if defined TIZEN_EXT
1782 if (retry_count < 0)
1785 DBG("retry_count %d", retry_count);
1786 #else /* TIZEN_EXT */
1788 #endif /* TIZEN_EXT */
1790 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1791 NULL, free_interface);
1793 sk = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
1795 #if defined TIZEN_EXT
1796 return __connman_rtnl_init(retry_count - 1);
1797 #else /* TIZEN_EXT */
1799 #endif /* TIZEN_EXT */
1801 memset(&addr, 0, sizeof(addr));
1802 addr.nl_family = AF_NETLINK;
1803 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1804 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1805 (1<<(RTNLGRP_ND_USEROPT-1));
1807 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1809 #if defined TIZEN_EXT
1810 return __connman_rtnl_init(retry_count - 1);
1811 #else /* TIZEN_EXT */
1813 #endif /* TIZEN_EXT */
1816 channel = g_io_channel_unix_new(sk);
1817 g_io_channel_set_close_on_unref(channel, TRUE);
1819 g_io_channel_set_encoding(channel, NULL, NULL);
1820 g_io_channel_set_buffered(channel, FALSE);
1822 channel_watch = g_io_add_watch(channel,
1823 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1824 netlink_event, NULL);
1829 void __connman_rtnl_start(void)
1838 void __connman_rtnl_cleanup(void)
1844 for (list = watch_list; list; list = list->next) {
1845 struct watch_data *watch = list->data;
1847 DBG("removing watch %d", watch->id);
1853 g_slist_free(watch_list);
1856 g_slist_free(update_list);
1859 for (list = request_list; list; list = list->next) {
1860 struct rtnl_request *req = list->data;
1862 DBG("%s len %d type %d flags 0x%04x seq %d",
1863 type2string(req->hdr.nlmsg_type),
1864 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1865 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1871 g_slist_free(request_list);
1872 request_list = NULL;
1874 if (channel_watch) {
1875 g_source_remove(channel_watch);
1879 g_io_channel_shutdown(channel, TRUE, NULL);
1880 g_io_channel_unref(channel);
1884 g_hash_table_destroy(interface_list);