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
466 /* Do not accept Wi-Fi P2P interface */
467 if (g_strrstr(ifname, "p2p") != NULL) {
468 DBG("Newlink event for Wi-Fi P2P interface ignored");
473 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
474 address.ether_addr_octet[0],
475 address.ether_addr_octet[1],
476 address.ether_addr_octet[2],
477 address.ether_addr_octet[3],
478 address.ether_addr_octet[4],
479 address.ether_addr_octet[5]);
481 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
482 address.ether_addr_octet[0],
483 address.ether_addr_octet[1],
484 address.ether_addr_octet[2],
485 address.ether_addr_octet[3],
486 address.ether_addr_octet[4],
487 address.ether_addr_octet[5]);
489 if (flags & IFF_SLAVE) {
490 connman_info("%s {newlink} ignoring slave, index %d address %s",
496 if (TIZEN_TV_EXT && g_strcmp0(ident, "eeeeeeeeeeee") == 0) {
497 DBG("Newlink event with Dummy MAC. Ignored!");
504 case ARPHRD_LOOPBACK:
505 case ARPHDR_PHONET_PIPE:
508 #if defined TIZEN_EXT
510 * Description: ARPHDR_RMNET for QC modem using QMI
514 __connman_ipconfig_newlink(index, type, flags,
519 connman_info("%s {newlink} index %d address %s mtu %u",
520 ifname, index, str, mtu);
522 if (operstate != 0xff)
523 connman_info("%s {newlink} index %d operstate %u <%s>",
524 ifname, index, operstate,
525 operstate2str(operstate));
527 interface = g_hash_table_lookup(interface_list, GINT_TO_POINTER(index));
529 interface = g_new0(struct interface_data, 1);
530 interface->index = index;
531 interface->ident = g_strdup(ident);
533 g_hash_table_insert(interface_list,
534 GINT_TO_POINTER(index), interface);
536 if (type == ARPHRD_ETHER)
537 read_uevent(interface);
538 #if defined TIZEN_EXT
539 if (type == ARPHRD_PPP || type == ARPHDR_RMNET)
540 read_uevent(interface);
542 } else if (g_strcmp0(interface->ident, ident) != 0) {
543 /* If an original address is built-in physical device,
544 * it's hardly get an address at a initial creation
546 __connman_technology_remove_interface(interface->service_type,
547 interface->index, interface->ident);
549 g_free(interface->ident);
550 interface->ident = g_strdup(ident);
552 __connman_technology_add_interface(interface->service_type,
553 interface->index, interface->ident);
557 } else if (type == ARPHRD_ETHER && interface->device_type == CONNMAN_DEVICE_TYPE_UNKNOWN)
558 read_uevent(interface);
562 for (list = rtnl_list; list; list = list->next) {
563 struct connman_rtnl *rtnl = list->data;
566 rtnl->newlink(type, index, flags, change);
570 * The interface needs to be added after the newlink call.
571 * The newlink will create the technology when needed and
572 * __connman_technology_add_interface() expects the
573 * technology to be there already.
576 __connman_technology_add_interface(interface->service_type,
577 interface->index, interface->ident);
579 for (list = watch_list; list; list = list->next) {
580 struct watch_data *watch = list->data;
582 if (watch->index != index)
586 watch->newlink(flags, change, watch->user_data);
590 static void process_dellink(unsigned short type, int index, unsigned flags,
591 unsigned change, struct ifinfomsg *msg, int bytes)
593 struct rtnl_link_stats stats;
594 unsigned char operstate = 0xff;
595 const char *ifname = NULL;
598 memset(&stats, 0, sizeof(stats));
599 if (!extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats))
602 if (operstate != 0xff)
603 connman_info("%s {dellink} index %d operstate %u <%s>",
604 ifname, index, operstate,
605 operstate2str(operstate));
607 for (list = rtnl_list; list; list = list->next) {
608 struct connman_rtnl *rtnl = list->data;
611 rtnl->dellink(type, index, flags, change);
616 case ARPHRD_LOOPBACK:
617 case ARPHDR_PHONET_PIPE:
620 #if defined TIZEN_EXT
622 * Description: SLP requires ARPHRD_PPP for PPP type device
623 * ARPHDR_RMNET for QC modem using QMI
627 __connman_ipconfig_dellink(index, &stats);
631 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
634 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
636 struct in_addr *local,
637 struct in_addr *address,
638 struct in_addr *broadcast)
642 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
643 attr = RTA_NEXT(attr, bytes)) {
644 switch (attr->rta_type) {
647 *address = *((struct in_addr *) RTA_DATA(attr));
651 *local = *((struct in_addr *) RTA_DATA(attr));
655 *broadcast = *((struct in_addr *) RTA_DATA(attr));
659 *label = RTA_DATA(attr);
665 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
666 struct in6_addr *addr,
667 struct in6_addr *local)
671 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
672 attr = RTA_NEXT(attr, bytes)) {
673 switch (attr->rta_type) {
676 *addr = *((struct in6_addr *) RTA_DATA(attr));
680 *local = *((struct in6_addr *) RTA_DATA(attr));
686 static void process_newaddr(unsigned char family, unsigned char prefixlen,
687 int index, struct ifaddrmsg *msg, int bytes)
689 struct in_addr ipv4_addr = { INADDR_ANY };
690 struct in6_addr ipv6_address, ipv6_local;
691 const char *label = NULL;
693 char ip_string[INET6_ADDRSTRLEN];
695 if (family == AF_INET) {
697 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
699 } else if (family == AF_INET6) {
700 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
701 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
709 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
712 if (__connman_ipconfig_newaddr(index, family, label,
713 prefixlen, ip_string) >= 0) {
714 if (family == AF_INET6) {
716 * Re-create RDNSS configured servers if there
717 * are any for this interface. This is done
718 * because we might have now properly
719 * configured interface with proper
720 * autoconfigured address.
722 __connman_resolver_redo_servers(index);
727 static void process_deladdr(unsigned char family, unsigned char prefixlen,
728 int index, struct ifaddrmsg *msg, int bytes)
730 struct in_addr ipv4_addr = { INADDR_ANY };
731 struct in6_addr ipv6_address, ipv6_local;
732 const char *label = NULL;
734 char ip_string[INET6_ADDRSTRLEN];
736 if (family == AF_INET) {
737 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
739 } else if (family == AF_INET6) {
740 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
741 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
749 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
752 __connman_ipconfig_deladdr(index, family, label,
753 prefixlen, ip_string);
756 static void extract_ipv4_route(struct rtmsg *msg, int bytes, int *index,
758 struct in_addr *gateway)
762 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
763 attr = RTA_NEXT(attr, bytes)) {
764 switch (attr->rta_type) {
767 *dst = *((struct in_addr *) RTA_DATA(attr));
771 *gateway = *((struct in_addr *) RTA_DATA(attr));
775 *index = *((int *) RTA_DATA(attr));
781 static void extract_ipv6_route(struct rtmsg *msg, int bytes, int *index,
782 struct in6_addr *dst,
783 struct in6_addr *gateway)
787 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
788 attr = RTA_NEXT(attr, bytes)) {
789 switch (attr->rta_type) {
792 *dst = *((struct in6_addr *) RTA_DATA(attr));
797 *((struct in6_addr *) RTA_DATA(attr));
801 *index = *((int *) RTA_DATA(attr));
807 static void process_newroute(unsigned char family, unsigned char scope,
808 struct rtmsg *msg, int bytes)
811 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
814 if (family == AF_INET) {
815 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
817 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
819 inet_ntop(family, &dst, dststr, sizeof(dststr));
820 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
822 __connman_ipconfig_newroute(index, family, scope, dststr,
825 /* skip host specific routes */
826 if (scope != RT_SCOPE_UNIVERSE &&
827 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
830 if (dst.s_addr != INADDR_ANY)
833 } else if (family == AF_INET6) {
834 struct in6_addr dst = IN6ADDR_ANY_INIT,
835 gateway = IN6ADDR_ANY_INIT;
837 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
839 inet_ntop(family, &dst, dststr, sizeof(dststr));
840 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
842 __connman_ipconfig_newroute(index, family, scope, dststr,
845 /* skip host specific routes */
846 if (scope != RT_SCOPE_UNIVERSE &&
847 !(scope == RT_SCOPE_LINK &&
848 IN6_IS_ADDR_UNSPECIFIED(&dst)))
851 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
856 for (list = rtnl_list; list; list = list->next) {
857 struct connman_rtnl *rtnl = list->data;
859 if (rtnl->newgateway)
860 rtnl->newgateway(index, gatewaystr);
864 static void process_delroute(unsigned char family, unsigned char scope,
865 struct rtmsg *msg, int bytes)
868 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
871 if (family == AF_INET) {
872 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
874 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
876 inet_ntop(family, &dst, dststr, sizeof(dststr));
877 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
879 __connman_ipconfig_delroute(index, family, scope, dststr,
882 /* skip host specific routes */
883 if (scope != RT_SCOPE_UNIVERSE &&
884 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
887 if (dst.s_addr != INADDR_ANY)
890 } else if (family == AF_INET6) {
891 struct in6_addr dst = IN6ADDR_ANY_INIT,
892 gateway = IN6ADDR_ANY_INIT;
894 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
896 inet_ntop(family, &dst, dststr, sizeof(dststr));
897 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
899 __connman_ipconfig_delroute(index, family, scope, dststr,
902 /* skip host specific routes */
903 if (scope != RT_SCOPE_UNIVERSE &&
904 !(scope == RT_SCOPE_LINK &&
905 IN6_IS_ADDR_UNSPECIFIED(&dst)))
908 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
913 for (list = rtnl_list; list; list = list->next) {
914 struct connman_rtnl *rtnl = list->data;
916 if (rtnl->delgateway)
917 rtnl->delgateway(index, gatewaystr);
921 static inline void print_ether(struct rtattr *attr, const char *name)
923 int len = (int) RTA_PAYLOAD(attr);
925 if (len == ETH_ALEN) {
926 struct ether_addr eth;
927 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
928 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
930 print(" attr %s (len %d)\n", name, len);
933 static inline void print_inet(struct rtattr *attr, const char *name,
934 unsigned char family)
936 int len = (int) RTA_PAYLOAD(attr);
938 if (family == AF_INET && len == sizeof(struct in_addr)) {
940 addr = *((struct in_addr *) RTA_DATA(attr));
941 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
943 print(" attr %s (len %d)\n", name, len);
946 static inline void print_string(struct rtattr *attr, const char *name)
948 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
949 (char *) RTA_DATA(attr));
952 static inline void print_byte(struct rtattr *attr, const char *name)
954 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
955 *((unsigned char *) RTA_DATA(attr)));
958 static inline void print_integer(struct rtattr *attr, const char *name)
960 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
961 *((int *) RTA_DATA(attr)));
964 static inline void print_attr(struct rtattr *attr, const char *name)
966 int len = (int) RTA_PAYLOAD(attr);
969 print(" attr %s (len %d)\n", name, len);
971 print(" attr %d (len %d)\n", attr->rta_type, len);
974 static void rtnl_link(struct nlmsghdr *hdr)
976 struct ifinfomsg *msg;
980 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
981 bytes = IFLA_PAYLOAD(hdr);
983 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
985 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
986 attr = RTA_NEXT(attr, bytes)) {
987 switch (attr->rta_type) {
989 print_ether(attr, "address");
992 print_ether(attr, "broadcast");
995 print_string(attr, "ifname");
998 print_integer(attr, "mtu");
1001 print_attr(attr, "link");
1004 print_attr(attr, "qdisc");
1007 print_attr(attr, "stats");
1010 print_attr(attr, "cost");
1013 print_attr(attr, "priority");
1016 print_attr(attr, "master");
1019 print_attr(attr, "wireless");
1022 print_attr(attr, "protinfo");
1025 print_integer(attr, "txqlen");
1028 print_attr(attr, "map");
1031 print_attr(attr, "weight");
1033 case IFLA_OPERSTATE:
1034 print_byte(attr, "operstate");
1037 print_byte(attr, "linkmode");
1040 print_attr(attr, NULL);
1046 static void rtnl_newlink(struct nlmsghdr *hdr)
1048 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
1052 if (hdr->nlmsg_type == IFLA_WIRELESS)
1053 connman_warn_once("Obsolete WEXT WiFi driver detected");
1055 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
1056 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
1059 static void rtnl_dellink(struct nlmsghdr *hdr)
1061 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
1065 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
1066 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
1069 static void rtnl_addr(struct nlmsghdr *hdr)
1071 struct ifaddrmsg *msg;
1072 struct rtattr *attr;
1075 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1076 bytes = IFA_PAYLOAD(hdr);
1078 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
1080 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
1081 attr = RTA_NEXT(attr, bytes)) {
1082 switch (attr->rta_type) {
1084 print_inet(attr, "address", msg->ifa_family);
1087 print_inet(attr, "local", msg->ifa_family);
1090 print_string(attr, "label");
1093 print_inet(attr, "broadcast", msg->ifa_family);
1096 print_attr(attr, "anycast");
1099 print_attr(attr, "cacheinfo");
1102 print_attr(attr, "multicast");
1105 print_attr(attr, NULL);
1111 static void rtnl_newaddr(struct nlmsghdr *hdr)
1113 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1117 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1118 msg, IFA_PAYLOAD(hdr));
1121 static void rtnl_deladdr(struct nlmsghdr *hdr)
1123 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1127 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1128 msg, IFA_PAYLOAD(hdr));
1131 static void rtnl_route(struct nlmsghdr *hdr)
1134 struct rtattr *attr;
1137 msg = (struct rtmsg *) NLMSG_DATA(hdr);
1138 bytes = RTM_PAYLOAD(hdr);
1140 print("rtm_family %d rtm_table %d rtm_protocol %d",
1141 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
1142 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
1143 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
1145 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
1146 attr = RTA_NEXT(attr, bytes)) {
1147 switch (attr->rta_type) {
1149 print_inet(attr, "dst", msg->rtm_family);
1152 print_inet(attr, "src", msg->rtm_family);
1155 print_string(attr, "iif");
1158 print_integer(attr, "oif");
1161 print_inet(attr, "gateway", msg->rtm_family);
1164 print_attr(attr, "priority");
1167 print_inet(attr, "prefsrc", msg->rtm_family);
1170 print_attr(attr, "metrics");
1173 print_integer(attr, "table");
1176 print_attr(attr, NULL);
1182 static bool is_route_rtmsg(struct rtmsg *msg)
1184 if (msg->rtm_flags & RTM_F_CLONED)
1187 if (msg->rtm_table != RT_TABLE_MAIN)
1190 if (msg->rtm_protocol != RTPROT_BOOT &&
1191 msg->rtm_protocol != RTPROT_KERNEL)
1194 if (msg->rtm_type != RTN_UNICAST)
1200 static void rtnl_newroute(struct nlmsghdr *hdr)
1202 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1206 if (is_route_rtmsg(msg))
1207 process_newroute(msg->rtm_family, msg->rtm_scope,
1208 msg, RTM_PAYLOAD(hdr));
1211 static void rtnl_delroute(struct nlmsghdr *hdr)
1213 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1217 if (is_route_rtmsg(msg))
1218 process_delroute(msg->rtm_family, msg->rtm_scope,
1219 msg, RTM_PAYLOAD(hdr));
1222 static void *rtnl_nd_opt_rdnss(struct nd_opt_hdr *opt, guint32 *lifetime,
1225 guint32 *optint = (void *)opt;
1227 if (opt->nd_opt_len < 3)
1230 if (*lifetime > ntohl(optint[1]))
1231 *lifetime = ntohl(optint[1]);
1233 /* nd_opt_len is in units of 8 bytes. The header is 1 unit (8 bytes)
1234 and each address is another 2 units (16 bytes).
1235 So the number of addresses (given rounding) is nd_opt_len/2 */
1236 *nr_servers = opt->nd_opt_len / 2;
1238 /* And they start 8 bytes into the packet, or two guint32s in. */
1242 static const char **rtnl_nd_opt_dnssl(struct nd_opt_hdr *opt, guint32 *lifetime)
1244 const char **domains = NULL;
1245 guint32 *optint = (void *)opt;
1246 unsigned char *optc = (void *)&optint[2];
1247 int data_len = (opt->nd_opt_len * 8) - 8;
1251 if (*lifetime > ntohl(optint[1]))
1252 *lifetime = ntohl(optint[1]);
1254 /* Turn it into normal strings by converting the length bytes into '.',
1255 and count how many search domains there are while we're at it. */
1257 while (i < data_len) {
1258 if (optc[i] > 0x3f) {
1259 DBG("DNSSL contains compressed elements in violation of RFC6106");
1266 /* Check for double zero */
1267 if (i < data_len && optc[i] == 0)
1275 if (i >= data_len) {
1276 DBG("DNSSL data overflows option length");
1283 domains = g_try_new0(const char *, nr_domains + 1);
1287 /* Now point to the normal strings, missing out the leading '.' that
1288 each of them will have now. */
1289 for (i = 0; i < nr_domains; i++) {
1290 domains[i] = (char *)optc + 1;
1291 optc += strlen((char *)optc) + 1;
1297 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1299 struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1300 struct nd_opt_hdr *opt;
1301 guint32 lifetime = -1;
1302 const char **domains = NULL;
1303 struct in6_addr *servers = NULL;
1304 int i, nr_servers = 0;
1305 int msglen = msg->nduseropt_opts_len;
1308 DBG("family %d index %d len %d type %d code %d",
1309 msg->nduseropt_family, msg->nduseropt_ifindex,
1310 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1311 msg->nduseropt_icmp_code);
1313 if (msg->nduseropt_family != AF_INET6 ||
1314 msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1315 msg->nduseropt_icmp_code != 0)
1318 index = msg->nduseropt_ifindex;
1322 #if defined TIZEN_EXT
1323 struct connman_service *service;
1324 enum connman_service_state state;
1325 enum connman_dnsconfig_method ipv6_dns_method;
1327 service = __connman_service_lookup_from_index(index);
1329 DBG("Invalid service");
1333 DBG("service: %p index: %d\n", service, index);
1335 if (connman_setting_get_bool("SingleConnectedTechnology") == TRUE) {
1336 state = __connman_service_ipconfig_get_state(service, CONNMAN_IPCONFIG_TYPE_IPV6);
1337 if (state != CONNMAN_SERVICE_STATE_ASSOCIATION &&
1338 state != CONNMAN_SERVICE_STATE_CONFIGURATION &&
1339 state != CONNMAN_SERVICE_STATE_READY &&
1340 state != CONNMAN_SERVICE_STATE_ONLINE) {
1341 DBG("Service state[%d] is not connecting/connected", state);
1346 ipv6_dns_method = connman_service_get_ipv6_dns_method(service);
1347 if (ipv6_dns_method != CONNMAN_DNSCONFIG_METHOD_DHCP) {
1348 DBG("IPv6 DNS method is not Auto ignore RA!!! [DNS method: %d]", ipv6_dns_method);
1353 for (opt = (void *)&msg[1];
1355 msglen -= opt->nd_opt_len * 8,
1356 opt = ((void *)opt) + opt->nd_opt_len*8) {
1358 DBG("remaining %d nd opt type %d len %d\n",
1359 msglen, opt->nd_opt_type, opt->nd_opt_len);
1361 if (opt->nd_opt_type == 25) { /* ND_OPT_RDNSS */
1363 #if defined TIZEN_EXT
1364 struct connman_service *service;
1366 service = __connman_service_lookup_from_index(index);
1367 DBG("service: %p\n",service);
1369 servers = rtnl_nd_opt_rdnss(opt, &lifetime,
1371 for (i = 0; i < nr_servers; i++) {
1372 if (!inet_ntop(AF_INET6, servers + i, buf,
1376 #if defined TIZEN_EXT
1377 __connman_service_nameserver_remove(service,
1379 CONNMAN_IPCONFIG_TYPE_IPV6);
1380 __connman_service_nameserver_append(service,
1382 CONNMAN_IPCONFIG_TYPE_IPV6);
1384 connman_resolver_append_lifetime(index,
1385 NULL, buf, lifetime);
1388 } else if (opt->nd_opt_type == 31) { /* ND_OPT_DNSSL */
1391 domains = rtnl_nd_opt_dnssl(opt, &lifetime);
1392 for (i = 0; domains && domains[i]; i++)
1393 connman_resolver_append_lifetime(index,
1394 domains[i], NULL, lifetime);
1401 static const char *type2string(uint16_t type)
1430 case RTM_NEWNDUSEROPT:
1431 return "NEWNDUSEROPT";
1437 static GIOChannel *channel = NULL;
1438 static guint channel_watch = 0;
1440 struct rtnl_request {
1441 struct nlmsghdr hdr;
1442 struct rtgenmsg msg;
1444 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1446 static GSList *request_list = NULL;
1447 static guint32 request_seq = 0;
1449 static struct rtnl_request *find_request(guint32 seq)
1453 for (list = request_list; list; list = list->next) {
1454 struct rtnl_request *req = list->data;
1456 if (req->hdr.nlmsg_seq == seq)
1463 static int send_request(struct rtnl_request *req)
1465 struct sockaddr_nl addr;
1468 DBG("%s len %d type %d flags 0x%04x seq %d",
1469 type2string(req->hdr.nlmsg_type),
1470 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1471 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1473 sk = g_io_channel_unix_get_fd(channel);
1475 memset(&addr, 0, sizeof(addr));
1476 addr.nl_family = AF_NETLINK;
1478 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1479 (struct sockaddr *) &addr, sizeof(addr));
1482 static int queue_request(struct rtnl_request *req)
1484 request_list = g_slist_append(request_list, req);
1486 if (g_slist_length(request_list) > 1)
1489 return send_request(req);
1492 static int process_response(guint32 seq)
1494 struct rtnl_request *req;
1498 req = find_request(seq);
1500 request_list = g_slist_remove(request_list, req);
1504 req = g_slist_nth_data(request_list, 0);
1508 return send_request(req);
1511 static void rtnl_message(void *buf, size_t len)
1514 struct nlmsghdr *hdr = buf;
1515 struct nlmsgerr *err;
1517 if (!NLMSG_OK(hdr, len))
1520 DBG("%s len %d type %d flags 0x%04x seq %d pid %d",
1521 type2string(hdr->nlmsg_type),
1522 hdr->nlmsg_len, hdr->nlmsg_type,
1523 hdr->nlmsg_flags, hdr->nlmsg_seq,
1526 switch (hdr->nlmsg_type) {
1531 process_response(hdr->nlmsg_seq);
1534 err = NLMSG_DATA(hdr);
1535 DBG("error %d (%s)", -err->error,
1536 strerror(-err->error));
1556 case RTM_NEWNDUSEROPT:
1557 rtnl_newnduseropt(hdr);
1561 len -= hdr->nlmsg_len;
1562 buf += hdr->nlmsg_len;
1566 static gboolean netlink_event(GIOChannel *chan, GIOCondition cond, gpointer data)
1568 unsigned char buf[4096];
1569 struct sockaddr_nl nladdr;
1570 socklen_t addr_len = sizeof(nladdr);
1574 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1577 memset(buf, 0, sizeof(buf));
1578 memset(&nladdr, 0, sizeof(nladdr));
1580 fd = g_io_channel_unix_get_fd(chan);
1582 status = recvfrom(fd, buf, sizeof(buf), 0,
1583 (struct sockaddr *) &nladdr, &addr_len);
1585 if (errno == EINTR || errno == EAGAIN)
1594 if (nladdr.nl_pid != 0) { /* not sent by kernel, ignore */
1595 DBG("Received msg from %u, ignoring it", nladdr.nl_pid);
1599 rtnl_message(buf, status);
1604 static int send_getlink(void)
1606 struct rtnl_request *req;
1610 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1614 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1615 req->hdr.nlmsg_type = RTM_GETLINK;
1616 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1617 req->hdr.nlmsg_pid = 0;
1618 req->hdr.nlmsg_seq = request_seq++;
1619 req->msg.rtgen_family = AF_INET;
1621 return queue_request(req);
1624 static int send_getaddr(void)
1626 struct rtnl_request *req;
1630 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1634 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1635 req->hdr.nlmsg_type = RTM_GETADDR;
1636 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1637 req->hdr.nlmsg_pid = 0;
1638 req->hdr.nlmsg_seq = request_seq++;
1639 req->msg.rtgen_family = AF_INET;
1641 return queue_request(req);
1644 static int send_getroute(void)
1646 struct rtnl_request *req;
1650 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1654 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1655 req->hdr.nlmsg_type = RTM_GETROUTE;
1656 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1657 req->hdr.nlmsg_pid = 0;
1658 req->hdr.nlmsg_seq = request_seq++;
1659 req->msg.rtgen_family = AF_INET;
1661 return queue_request(req);
1664 static gboolean update_timeout_cb(gpointer user_data)
1666 __connman_rtnl_request_update();
1671 static void update_interval_callback(guint min)
1673 if (update_timeout > 0)
1674 g_source_remove(update_timeout);
1676 if (min < G_MAXUINT) {
1677 update_interval = min;
1678 update_timeout = g_timeout_add_seconds(update_interval,
1679 update_timeout_cb, NULL);
1682 update_interval = G_MAXUINT;
1686 static gint compare_interval(gconstpointer a, gconstpointer b)
1688 guint val_a = GPOINTER_TO_UINT(a);
1689 guint val_b = GPOINTER_TO_UINT(b);
1691 return val_a - val_b;
1694 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1701 update_list = g_slist_insert_sorted(update_list,
1702 GUINT_TO_POINTER(interval), compare_interval);
1704 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1705 if (min < update_interval) {
1706 update_interval_callback(min);
1707 __connman_rtnl_request_update();
1710 return update_interval;
1713 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1715 guint min = G_MAXUINT;
1720 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1723 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1725 if (min > update_interval)
1726 update_interval_callback(min);
1731 int __connman_rtnl_request_update(void)
1733 return send_getlink();
1736 int __connman_rtnl_init(void)
1738 struct sockaddr_nl addr;
1743 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1744 NULL, free_interface);
1746 sk = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
1750 memset(&addr, 0, sizeof(addr));
1751 addr.nl_family = AF_NETLINK;
1752 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1753 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1754 (1<<(RTNLGRP_ND_USEROPT-1));
1756 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1761 channel = g_io_channel_unix_new(sk);
1762 g_io_channel_set_close_on_unref(channel, TRUE);
1764 g_io_channel_set_encoding(channel, NULL, NULL);
1765 g_io_channel_set_buffered(channel, FALSE);
1767 channel_watch = g_io_add_watch(channel,
1768 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1769 netlink_event, NULL);
1774 void __connman_rtnl_start(void)
1783 void __connman_rtnl_cleanup(void)
1789 for (list = watch_list; list; list = list->next) {
1790 struct watch_data *watch = list->data;
1792 DBG("removing watch %d", watch->id);
1798 g_slist_free(watch_list);
1801 g_slist_free(update_list);
1804 for (list = request_list; list; list = list->next) {
1805 struct rtnl_request *req = list->data;
1807 DBG("%s len %d type %d flags 0x%04x seq %d",
1808 type2string(req->hdr.nlmsg_type),
1809 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1810 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1816 g_slist_free(request_list);
1817 request_list = NULL;
1819 if (channel_watch) {
1820 g_source_remove(channel_watch);
1824 g_io_channel_shutdown(channel, TRUE, NULL);
1825 g_io_channel_unref(channel);
1829 g_hash_table_destroy(interface_list);