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;
166 if (ether_blacklisted(name)) {
167 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
168 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
170 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
171 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
174 filename = g_strdup_printf("/sys/class/net/%s/uevent", name);
176 f = fopen(filename, "re");
181 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
182 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
186 found_devtype = false;
187 while (fgets(line, sizeof(line), f)) {
190 pos = strchr(line, '\n');
195 if (strncmp(line, "DEVTYPE=", 8) != 0)
198 found_devtype = true;
200 if (strcmp(line + 8, "wlan") == 0) {
201 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
202 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
203 } else if (strcmp(line + 8, "wwan") == 0) {
204 interface->service_type = CONNMAN_SERVICE_TYPE_CELLULAR;
205 interface->device_type = CONNMAN_DEVICE_TYPE_CELLULAR;
206 } else if (strcmp(line + 8, "bluetooth") == 0) {
207 interface->service_type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
208 interface->device_type = CONNMAN_DEVICE_TYPE_BLUETOOTH;
209 } else if (strcmp(line + 8, "gadget") == 0) {
210 interface->service_type = CONNMAN_SERVICE_TYPE_GADGET;
211 interface->device_type = CONNMAN_DEVICE_TYPE_GADGET;
212 } else if (strcmp(line + 8, "vlan") == 0) {
213 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
214 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
215 } else if (strcmp(line + 8, "bond") == 0) {
216 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
217 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
219 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
220 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
229 #if !defined TIZEN_EXT
230 /* TIZEN does not use old wext interface */
231 /* We haven't got a DEVTYPE, let's check if it's a wireless device */
232 if (wext_interface(name)) {
233 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
234 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
236 connman_error("%s runs an unsupported 802.11 driver", name);
244 enum connman_device_type __connman_rtnl_get_device_type(int index)
246 struct interface_data *interface;
248 interface = g_hash_table_lookup(interface_list,
249 GINT_TO_POINTER(index));
251 return CONNMAN_DEVICE_TYPE_UNKNOWN;
253 return interface->device_type;
257 * connman_rtnl_add_newlink_watch:
258 * @index: network device index
259 * @callback: callback function
260 * @user_data: callback data;
262 * Add a new RTNL watch for newlink events
264 * Returns: %0 on failure and a unique id on success
266 unsigned int connman_rtnl_add_newlink_watch(int index,
267 connman_rtnl_link_cb_t callback, void *user_data)
269 struct watch_data *watch;
271 watch = g_try_new0(struct watch_data, 1);
275 watch->id = ++watch_id;
276 watch->index = index;
278 watch->newlink = callback;
279 watch->user_data = user_data;
281 watch_list = g_slist_prepend(watch_list, watch);
283 DBG("id %d", watch->id);
286 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
289 callback(flags, 0, user_data);
296 * connman_rtnl_remove_watch:
297 * @id: watch identifier
299 * Remove the RTNL watch for the identifier
301 void connman_rtnl_remove_watch(unsigned int id)
310 for (list = watch_list; list; list = list->next) {
311 struct watch_data *watch = list->data;
313 if (watch->id == id) {
314 watch_list = g_slist_remove(watch_list, watch);
321 static void trigger_rtnl(int index, void *user_data)
323 struct connman_rtnl *rtnl = user_data;
326 unsigned short type = __connman_ipconfig_get_type_from_index(index);
327 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
329 rtnl->newlink(type, index, flags, 0);
332 if (rtnl->newgateway) {
333 const char *gateway =
334 __connman_ipconfig_get_gateway_from_index(index,
335 CONNMAN_IPCONFIG_TYPE_ALL);
338 rtnl->newgateway(index, gateway);
342 static GSList *rtnl_list = NULL;
344 static gint compare_priority(gconstpointer a, gconstpointer b)
346 const struct connman_rtnl *rtnl1 = a;
347 const struct connman_rtnl *rtnl2 = b;
349 return rtnl2->priority - rtnl1->priority;
353 * connman_rtnl_register:
356 * Register a new RTNL module
358 * Returns: %0 on success
360 int connman_rtnl_register(struct connman_rtnl *rtnl)
362 DBG("rtnl %p name %s", rtnl, rtnl->name);
364 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
367 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
373 * connman_rtnl_unregister:
376 * Remove a previously registered RTNL module
378 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
380 DBG("rtnl %p name %s", rtnl, rtnl->name);
382 rtnl_list = g_slist_remove(rtnl_list, rtnl);
385 static const char *operstate2str(unsigned char operstate)
388 case IF_OPER_UNKNOWN:
390 case IF_OPER_NOTPRESENT:
391 return "NOT-PRESENT";
394 case IF_OPER_LOWERLAYERDOWN:
395 return "LOWER-LAYER-DOWN";
396 case IF_OPER_TESTING:
398 case IF_OPER_DORMANT:
407 static bool extract_link(struct ifinfomsg *msg, int bytes,
408 struct ether_addr *address, const char **ifname,
409 unsigned int *mtu, unsigned char *operstate,
410 struct rtnl_link_stats *stats)
414 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
415 attr = RTA_NEXT(attr, bytes)) {
416 switch (attr->rta_type) {
419 memcpy(address, RTA_DATA(attr), ETH_ALEN);
423 *ifname = RTA_DATA(attr);
427 *mtu = *((unsigned int *) RTA_DATA(attr));
431 memcpy(stats, RTA_DATA(attr),
432 sizeof(struct rtnl_link_stats));
436 *operstate = *((unsigned char *) RTA_DATA(attr));
448 static void process_newlink(unsigned short type, int index, unsigned flags,
449 unsigned change, struct ifinfomsg *msg, int bytes)
451 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
452 struct rtnl_link_stats stats;
453 unsigned char operstate = 0xff;
454 struct interface_data *interface;
455 const char *ifname = NULL;
456 unsigned int mtu = 0;
457 char ident[13], str[18];
460 memset(&stats, 0, sizeof(stats));
461 if (!extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats))
464 #if defined TIZEN_EXT
465 /* Do not accept Wi-Fi P2P interface */
466 if (g_strrstr(ifname, "p2p") != NULL) {
467 DBG("Newlink event for Wi-Fi P2P interface ignored");
472 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
473 address.ether_addr_octet[0],
474 address.ether_addr_octet[1],
475 address.ether_addr_octet[2],
476 address.ether_addr_octet[3],
477 address.ether_addr_octet[4],
478 address.ether_addr_octet[5]);
480 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
481 address.ether_addr_octet[0],
482 address.ether_addr_octet[1],
483 address.ether_addr_octet[2],
484 address.ether_addr_octet[3],
485 address.ether_addr_octet[4],
486 address.ether_addr_octet[5]);
488 if (flags & IFF_SLAVE) {
489 connman_info("%s {newlink} ignoring slave, index %d address %s",
495 if (TIZEN_TV_EXT && g_strcmp0(ident, "eeeeeeeeeeee") == 0) {
496 DBG("Newlink event with Dummy MAC. Ignored!");
503 case ARPHRD_LOOPBACK:
504 case ARPHDR_PHONET_PIPE:
507 #if defined TIZEN_EXT
509 * Description: ARPHDR_RMNET for QC modem using QMI
513 __connman_ipconfig_newlink(index, type, flags,
518 connman_info("%s {newlink} index %d address %s mtu %u",
519 ifname, index, str, mtu);
521 if (operstate != 0xff)
522 connman_info("%s {newlink} index %d operstate %u <%s>",
523 ifname, index, operstate,
524 operstate2str(operstate));
526 interface = g_hash_table_lookup(interface_list, GINT_TO_POINTER(index));
528 interface = g_new0(struct interface_data, 1);
529 interface->index = index;
530 interface->ident = g_strdup(ident);
532 g_hash_table_insert(interface_list,
533 GINT_TO_POINTER(index), interface);
535 if (type == ARPHRD_ETHER)
536 read_uevent(interface);
537 #if defined TIZEN_EXT
538 if (type == ARPHRD_PPP || type == ARPHDR_RMNET)
539 read_uevent(interface);
541 } else if (g_strcmp0(interface->ident, ident) != 0) {
542 /* If an original address is built-in physical device,
543 * it's hardly get an address at a initial creation
545 __connman_technology_remove_interface(interface->service_type,
546 interface->index, interface->ident);
548 g_free(interface->ident);
549 interface->ident = g_strdup(ident);
551 __connman_technology_add_interface(interface->service_type,
552 interface->index, interface->ident);
556 } else if (type == ARPHRD_ETHER && interface->device_type == CONNMAN_DEVICE_TYPE_UNKNOWN)
557 read_uevent(interface);
561 for (list = rtnl_list; list; list = list->next) {
562 struct connman_rtnl *rtnl = list->data;
565 rtnl->newlink(type, index, flags, change);
569 * The interface needs to be added after the newlink call.
570 * The newlink will create the technology when needed and
571 * __connman_technology_add_interface() expects the
572 * technology to be there already.
575 __connman_technology_add_interface(interface->service_type,
576 interface->index, interface->ident);
578 for (list = watch_list; list; list = list->next) {
579 struct watch_data *watch = list->data;
581 if (watch->index != index)
585 watch->newlink(flags, change, watch->user_data);
589 static void process_dellink(unsigned short type, int index, unsigned flags,
590 unsigned change, struct ifinfomsg *msg, int bytes)
592 struct rtnl_link_stats stats;
593 unsigned char operstate = 0xff;
594 const char *ifname = NULL;
597 memset(&stats, 0, sizeof(stats));
598 if (!extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats))
601 if (operstate != 0xff)
602 connman_info("%s {dellink} index %d operstate %u <%s>",
603 ifname, index, operstate,
604 operstate2str(operstate));
606 for (list = rtnl_list; list; list = list->next) {
607 struct connman_rtnl *rtnl = list->data;
610 rtnl->dellink(type, index, flags, change);
615 case ARPHRD_LOOPBACK:
616 case ARPHDR_PHONET_PIPE:
619 #if defined TIZEN_EXT
621 * Description: SLP requires ARPHRD_PPP for PPP type device
622 * ARPHDR_RMNET for QC modem using QMI
626 __connman_ipconfig_dellink(index, &stats);
630 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
633 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
635 struct in_addr *local,
636 struct in_addr *address,
637 struct in_addr *broadcast)
641 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
642 attr = RTA_NEXT(attr, bytes)) {
643 switch (attr->rta_type) {
646 *address = *((struct in_addr *) RTA_DATA(attr));
650 *local = *((struct in_addr *) RTA_DATA(attr));
654 *broadcast = *((struct in_addr *) RTA_DATA(attr));
658 *label = RTA_DATA(attr);
664 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
665 struct in6_addr *addr,
666 struct in6_addr *local)
670 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
671 attr = RTA_NEXT(attr, bytes)) {
672 switch (attr->rta_type) {
675 *addr = *((struct in6_addr *) RTA_DATA(attr));
679 *local = *((struct in6_addr *) RTA_DATA(attr));
685 static void process_newaddr(unsigned char family, unsigned char prefixlen,
686 int index, struct ifaddrmsg *msg, int bytes)
688 struct in_addr ipv4_addr = { INADDR_ANY };
689 struct in6_addr ipv6_address, ipv6_local;
690 const char *label = NULL;
692 char ip_string[INET6_ADDRSTRLEN];
694 if (family == AF_INET) {
696 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
698 } else if (family == AF_INET6) {
699 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
700 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
708 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
711 if (__connman_ipconfig_newaddr(index, family, label,
712 prefixlen, ip_string) >= 0) {
713 if (family == AF_INET6) {
715 * Re-create RDNSS configured servers if there
716 * are any for this interface. This is done
717 * because we might have now properly
718 * configured interface with proper
719 * autoconfigured address.
721 __connman_resolver_redo_servers(index);
726 static void process_deladdr(unsigned char family, unsigned char prefixlen,
727 int index, struct ifaddrmsg *msg, int bytes)
729 struct in_addr ipv4_addr = { INADDR_ANY };
730 struct in6_addr ipv6_address, ipv6_local;
731 const char *label = NULL;
733 char ip_string[INET6_ADDRSTRLEN];
735 if (family == AF_INET) {
736 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
738 } else if (family == AF_INET6) {
739 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
740 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
748 if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
751 __connman_ipconfig_deladdr(index, family, label,
752 prefixlen, ip_string);
755 static void extract_ipv4_route(struct rtmsg *msg, int bytes, int *index,
757 struct in_addr *gateway)
761 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
762 attr = RTA_NEXT(attr, bytes)) {
763 switch (attr->rta_type) {
766 *dst = *((struct in_addr *) RTA_DATA(attr));
770 *gateway = *((struct in_addr *) RTA_DATA(attr));
774 *index = *((int *) RTA_DATA(attr));
780 static void extract_ipv6_route(struct rtmsg *msg, int bytes, int *index,
781 struct in6_addr *dst,
782 struct in6_addr *gateway)
786 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
787 attr = RTA_NEXT(attr, bytes)) {
788 switch (attr->rta_type) {
791 *dst = *((struct in6_addr *) RTA_DATA(attr));
796 *((struct in6_addr *) RTA_DATA(attr));
800 *index = *((int *) RTA_DATA(attr));
806 static void process_newroute(unsigned char family, unsigned char scope,
807 struct rtmsg *msg, int bytes)
810 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
813 if (family == AF_INET) {
814 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
816 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
818 inet_ntop(family, &dst, dststr, sizeof(dststr));
819 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
821 __connman_ipconfig_newroute(index, family, scope, dststr,
824 /* skip host specific routes */
825 if (scope != RT_SCOPE_UNIVERSE &&
826 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
829 if (dst.s_addr != INADDR_ANY)
832 } else if (family == AF_INET6) {
833 struct in6_addr dst = IN6ADDR_ANY_INIT,
834 gateway = IN6ADDR_ANY_INIT;
836 extract_ipv6_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 &&
847 IN6_IS_ADDR_UNSPECIFIED(&dst)))
850 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
855 for (list = rtnl_list; list; list = list->next) {
856 struct connman_rtnl *rtnl = list->data;
858 if (rtnl->newgateway)
859 rtnl->newgateway(index, gatewaystr);
863 static void process_delroute(unsigned char family, unsigned char scope,
864 struct rtmsg *msg, int bytes)
867 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
870 if (family == AF_INET) {
871 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
873 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
875 inet_ntop(family, &dst, dststr, sizeof(dststr));
876 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
878 __connman_ipconfig_delroute(index, family, scope, dststr,
881 /* skip host specific routes */
882 if (scope != RT_SCOPE_UNIVERSE &&
883 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
886 if (dst.s_addr != INADDR_ANY)
889 } else if (family == AF_INET6) {
890 struct in6_addr dst = IN6ADDR_ANY_INIT,
891 gateway = IN6ADDR_ANY_INIT;
893 extract_ipv6_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 &&
904 IN6_IS_ADDR_UNSPECIFIED(&dst)))
907 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
912 for (list = rtnl_list; list; list = list->next) {
913 struct connman_rtnl *rtnl = list->data;
915 if (rtnl->delgateway)
916 rtnl->delgateway(index, gatewaystr);
920 static inline void print_ether(struct rtattr *attr, const char *name)
922 int len = (int) RTA_PAYLOAD(attr);
924 if (len == ETH_ALEN) {
925 struct ether_addr eth;
926 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
927 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
929 print(" attr %s (len %d)\n", name, len);
932 static inline void print_inet(struct rtattr *attr, const char *name,
933 unsigned char family)
935 int len = (int) RTA_PAYLOAD(attr);
937 if (family == AF_INET && len == sizeof(struct in_addr)) {
939 addr = *((struct in_addr *) RTA_DATA(attr));
940 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
942 print(" attr %s (len %d)\n", name, len);
945 static inline void print_string(struct rtattr *attr, const char *name)
947 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
948 (char *) RTA_DATA(attr));
951 static inline void print_byte(struct rtattr *attr, const char *name)
953 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
954 *((unsigned char *) RTA_DATA(attr)));
957 static inline void print_integer(struct rtattr *attr, const char *name)
959 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
960 *((int *) RTA_DATA(attr)));
963 static inline void print_attr(struct rtattr *attr, const char *name)
965 int len = (int) RTA_PAYLOAD(attr);
968 print(" attr %s (len %d)\n", name, len);
970 print(" attr %d (len %d)\n", attr->rta_type, len);
973 static void rtnl_link(struct nlmsghdr *hdr)
975 struct ifinfomsg *msg;
979 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
980 bytes = IFLA_PAYLOAD(hdr);
982 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
984 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
985 attr = RTA_NEXT(attr, bytes)) {
986 switch (attr->rta_type) {
988 print_ether(attr, "address");
991 print_ether(attr, "broadcast");
994 print_string(attr, "ifname");
997 print_integer(attr, "mtu");
1000 print_attr(attr, "link");
1003 print_attr(attr, "qdisc");
1006 print_attr(attr, "stats");
1009 print_attr(attr, "cost");
1012 print_attr(attr, "priority");
1015 print_attr(attr, "master");
1018 print_attr(attr, "wireless");
1021 print_attr(attr, "protinfo");
1024 print_integer(attr, "txqlen");
1027 print_attr(attr, "map");
1030 print_attr(attr, "weight");
1032 case IFLA_OPERSTATE:
1033 print_byte(attr, "operstate");
1036 print_byte(attr, "linkmode");
1039 print_attr(attr, NULL);
1045 static void rtnl_newlink(struct nlmsghdr *hdr)
1047 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
1051 if (hdr->nlmsg_type == IFLA_WIRELESS)
1052 connman_warn_once("Obsolete WEXT WiFi driver detected");
1054 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
1055 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
1058 static void rtnl_dellink(struct nlmsghdr *hdr)
1060 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
1064 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
1065 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
1068 static void rtnl_addr(struct nlmsghdr *hdr)
1070 struct ifaddrmsg *msg;
1071 struct rtattr *attr;
1074 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1075 bytes = IFA_PAYLOAD(hdr);
1077 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
1079 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
1080 attr = RTA_NEXT(attr, bytes)) {
1081 switch (attr->rta_type) {
1083 print_inet(attr, "address", msg->ifa_family);
1086 print_inet(attr, "local", msg->ifa_family);
1089 print_string(attr, "label");
1092 print_inet(attr, "broadcast", msg->ifa_family);
1095 print_attr(attr, "anycast");
1098 print_attr(attr, "cacheinfo");
1101 print_attr(attr, "multicast");
1104 print_attr(attr, NULL);
1110 static void rtnl_newaddr(struct nlmsghdr *hdr)
1112 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1116 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1117 msg, IFA_PAYLOAD(hdr));
1120 static void rtnl_deladdr(struct nlmsghdr *hdr)
1122 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1126 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1127 msg, IFA_PAYLOAD(hdr));
1130 static void rtnl_route(struct nlmsghdr *hdr)
1133 struct rtattr *attr;
1136 msg = (struct rtmsg *) NLMSG_DATA(hdr);
1137 bytes = RTM_PAYLOAD(hdr);
1139 print("rtm_family %d rtm_table %d rtm_protocol %d",
1140 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
1141 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
1142 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
1144 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
1145 attr = RTA_NEXT(attr, bytes)) {
1146 switch (attr->rta_type) {
1148 print_inet(attr, "dst", msg->rtm_family);
1151 print_inet(attr, "src", msg->rtm_family);
1154 print_string(attr, "iif");
1157 print_integer(attr, "oif");
1160 print_inet(attr, "gateway", msg->rtm_family);
1163 print_attr(attr, "priority");
1166 print_inet(attr, "prefsrc", msg->rtm_family);
1169 print_attr(attr, "metrics");
1172 print_integer(attr, "table");
1175 print_attr(attr, NULL);
1181 static bool is_route_rtmsg(struct rtmsg *msg)
1183 if (msg->rtm_flags & RTM_F_CLONED)
1186 if (msg->rtm_table != RT_TABLE_MAIN)
1189 if (msg->rtm_protocol != RTPROT_BOOT &&
1190 msg->rtm_protocol != RTPROT_KERNEL)
1193 if (msg->rtm_type != RTN_UNICAST)
1199 static void rtnl_newroute(struct nlmsghdr *hdr)
1201 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1205 if (is_route_rtmsg(msg))
1206 process_newroute(msg->rtm_family, msg->rtm_scope,
1207 msg, RTM_PAYLOAD(hdr));
1210 static void rtnl_delroute(struct nlmsghdr *hdr)
1212 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1216 if (is_route_rtmsg(msg))
1217 process_delroute(msg->rtm_family, msg->rtm_scope,
1218 msg, RTM_PAYLOAD(hdr));
1221 static void *rtnl_nd_opt_rdnss(struct nd_opt_hdr *opt, guint32 *lifetime,
1224 guint32 *optint = (void *)opt;
1226 if (opt->nd_opt_len < 3)
1229 if (*lifetime > ntohl(optint[1]))
1230 *lifetime = ntohl(optint[1]);
1232 /* nd_opt_len is in units of 8 bytes. The header is 1 unit (8 bytes)
1233 and each address is another 2 units (16 bytes).
1234 So the number of addresses (given rounding) is nd_opt_len/2 */
1235 *nr_servers = opt->nd_opt_len / 2;
1237 /* And they start 8 bytes into the packet, or two guint32s in. */
1241 static const char **rtnl_nd_opt_dnssl(struct nd_opt_hdr *opt, guint32 *lifetime)
1243 const char **domains = NULL;
1244 guint32 *optint = (void *)opt;
1245 unsigned char *optc = (void *)&optint[2];
1246 int data_len = (opt->nd_opt_len * 8) - 8;
1250 if (*lifetime > ntohl(optint[1]))
1251 *lifetime = ntohl(optint[1]);
1253 /* Turn it into normal strings by converting the length bytes into '.',
1254 and count how many search domains there are while we're at it. */
1256 while (i < data_len) {
1257 if (optc[i] > 0x3f) {
1258 DBG("DNSSL contains compressed elements in violation of RFC6106");
1265 /* Check for double zero */
1266 if (i < data_len && optc[i] == 0)
1274 if (i >= data_len) {
1275 DBG("DNSSL data overflows option length");
1282 domains = g_try_new0(const char *, nr_domains + 1);
1286 /* Now point to the normal strings, missing out the leading '.' that
1287 each of them will have now. */
1288 for (i = 0; i < nr_domains; i++) {
1289 domains[i] = (char *)optc + 1;
1290 optc += strlen((char *)optc) + 1;
1296 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1298 struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1299 struct nd_opt_hdr *opt;
1300 guint32 lifetime = -1;
1301 const char **domains = NULL;
1302 struct in6_addr *servers = NULL;
1303 int i, nr_servers = 0;
1304 int msglen = msg->nduseropt_opts_len;
1307 DBG("family %d index %d len %d type %d code %d",
1308 msg->nduseropt_family, msg->nduseropt_ifindex,
1309 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1310 msg->nduseropt_icmp_code);
1312 if (msg->nduseropt_family != AF_INET6 ||
1313 msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1314 msg->nduseropt_icmp_code != 0)
1317 index = msg->nduseropt_ifindex;
1321 #if defined TIZEN_EXT
1322 struct connman_service *service;
1323 enum connman_service_state state;
1324 enum connman_dnsconfig_method ipv6_dns_method;
1326 service = __connman_service_lookup_from_index(index);
1328 DBG("Invalid service");
1332 DBG("service: %p index: %d\n", service, index);
1334 if (connman_setting_get_bool("SingleConnectedTechnology") == TRUE) {
1335 state = __connman_service_ipconfig_get_state(service, CONNMAN_IPCONFIG_TYPE_IPV6);
1336 if (state != CONNMAN_SERVICE_STATE_ASSOCIATION &&
1337 state != CONNMAN_SERVICE_STATE_CONFIGURATION &&
1338 state != CONNMAN_SERVICE_STATE_READY &&
1339 state != CONNMAN_SERVICE_STATE_ONLINE) {
1340 DBG("Service state[%d] is not connecting/connected", state);
1345 ipv6_dns_method = connman_service_get_ipv6_dns_method(service);
1346 if (ipv6_dns_method != CONNMAN_DNSCONFIG_METHOD_DHCP) {
1347 DBG("IPv6 DNS method is not Auto ignore RA!!! [DNS method: %d]", ipv6_dns_method);
1352 for (opt = (void *)&msg[1];
1354 msglen -= opt->nd_opt_len * 8,
1355 opt = ((void *)opt) + opt->nd_opt_len*8) {
1357 DBG("remaining %d nd opt type %d len %d\n",
1358 msglen, opt->nd_opt_type, opt->nd_opt_len);
1360 if (opt->nd_opt_type == 25) { /* ND_OPT_RDNSS */
1362 #if defined TIZEN_EXT
1363 struct connman_service *service;
1365 service = __connman_service_lookup_from_index(index);
1366 DBG("service: %p\n",service);
1368 servers = rtnl_nd_opt_rdnss(opt, &lifetime,
1370 for (i = 0; i < nr_servers; i++) {
1371 if (!inet_ntop(AF_INET6, servers + i, buf,
1375 #if defined TIZEN_EXT
1376 __connman_service_nameserver_remove(service,
1378 CONNMAN_IPCONFIG_TYPE_IPV6);
1379 __connman_service_nameserver_append(service,
1381 CONNMAN_IPCONFIG_TYPE_IPV6);
1383 connman_resolver_append_lifetime(index,
1384 NULL, buf, lifetime);
1387 } else if (opt->nd_opt_type == 31) { /* ND_OPT_DNSSL */
1390 domains = rtnl_nd_opt_dnssl(opt, &lifetime);
1391 for (i = 0; domains && domains[i]; i++)
1392 connman_resolver_append_lifetime(index,
1393 domains[i], NULL, lifetime);
1400 static const char *type2string(uint16_t type)
1429 case RTM_NEWNDUSEROPT:
1430 return "NEWNDUSEROPT";
1436 static GIOChannel *channel = NULL;
1437 static guint channel_watch = 0;
1439 struct rtnl_request {
1440 struct nlmsghdr hdr;
1441 struct rtgenmsg msg;
1443 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1445 static GSList *request_list = NULL;
1446 static guint32 request_seq = 0;
1448 static struct rtnl_request *find_request(guint32 seq)
1452 for (list = request_list; list; list = list->next) {
1453 struct rtnl_request *req = list->data;
1455 if (req->hdr.nlmsg_seq == seq)
1462 static int send_request(struct rtnl_request *req)
1464 struct sockaddr_nl addr;
1467 DBG("%s len %d type %d flags 0x%04x seq %d",
1468 type2string(req->hdr.nlmsg_type),
1469 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1470 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1472 sk = g_io_channel_unix_get_fd(channel);
1474 memset(&addr, 0, sizeof(addr));
1475 addr.nl_family = AF_NETLINK;
1477 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1478 (struct sockaddr *) &addr, sizeof(addr));
1481 static int queue_request(struct rtnl_request *req)
1483 request_list = g_slist_append(request_list, req);
1485 if (g_slist_length(request_list) > 1)
1488 return send_request(req);
1491 static int process_response(guint32 seq)
1493 struct rtnl_request *req;
1497 req = find_request(seq);
1499 request_list = g_slist_remove(request_list, req);
1503 req = g_slist_nth_data(request_list, 0);
1507 return send_request(req);
1510 static void rtnl_message(void *buf, size_t len)
1513 struct nlmsghdr *hdr = buf;
1514 struct nlmsgerr *err;
1516 if (!NLMSG_OK(hdr, len))
1519 DBG("%s len %d type %d flags 0x%04x seq %d pid %d",
1520 type2string(hdr->nlmsg_type),
1521 hdr->nlmsg_len, hdr->nlmsg_type,
1522 hdr->nlmsg_flags, hdr->nlmsg_seq,
1525 switch (hdr->nlmsg_type) {
1530 process_response(hdr->nlmsg_seq);
1533 err = NLMSG_DATA(hdr);
1534 DBG("error %d (%s)", -err->error,
1535 strerror(-err->error));
1555 case RTM_NEWNDUSEROPT:
1556 rtnl_newnduseropt(hdr);
1560 len -= hdr->nlmsg_len;
1561 buf += hdr->nlmsg_len;
1565 static gboolean netlink_event(GIOChannel *chan, GIOCondition cond, gpointer data)
1567 unsigned char buf[4096];
1568 struct sockaddr_nl nladdr;
1569 socklen_t addr_len = sizeof(nladdr);
1573 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1576 memset(buf, 0, sizeof(buf));
1577 memset(&nladdr, 0, sizeof(nladdr));
1579 fd = g_io_channel_unix_get_fd(chan);
1581 status = recvfrom(fd, buf, sizeof(buf), 0,
1582 (struct sockaddr *) &nladdr, &addr_len);
1584 if (errno == EINTR || errno == EAGAIN)
1593 if (nladdr.nl_pid != 0) { /* not sent by kernel, ignore */
1594 DBG("Received msg from %u, ignoring it", nladdr.nl_pid);
1598 rtnl_message(buf, status);
1603 static int send_getlink(void)
1605 struct rtnl_request *req;
1609 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1613 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1614 req->hdr.nlmsg_type = RTM_GETLINK;
1615 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1616 req->hdr.nlmsg_pid = 0;
1617 req->hdr.nlmsg_seq = request_seq++;
1618 req->msg.rtgen_family = AF_INET;
1620 return queue_request(req);
1623 static int send_getaddr(void)
1625 struct rtnl_request *req;
1629 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1633 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1634 req->hdr.nlmsg_type = RTM_GETADDR;
1635 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1636 req->hdr.nlmsg_pid = 0;
1637 req->hdr.nlmsg_seq = request_seq++;
1638 req->msg.rtgen_family = AF_INET;
1640 return queue_request(req);
1643 static int send_getroute(void)
1645 struct rtnl_request *req;
1649 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1653 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1654 req->hdr.nlmsg_type = RTM_GETROUTE;
1655 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1656 req->hdr.nlmsg_pid = 0;
1657 req->hdr.nlmsg_seq = request_seq++;
1658 req->msg.rtgen_family = AF_INET;
1660 return queue_request(req);
1663 static gboolean update_timeout_cb(gpointer user_data)
1665 __connman_rtnl_request_update();
1670 static void update_interval_callback(guint min)
1672 if (update_timeout > 0)
1673 g_source_remove(update_timeout);
1675 if (min < G_MAXUINT) {
1676 update_interval = min;
1677 update_timeout = g_timeout_add_seconds(update_interval,
1678 update_timeout_cb, NULL);
1681 update_interval = G_MAXUINT;
1685 static gint compare_interval(gconstpointer a, gconstpointer b)
1687 guint val_a = GPOINTER_TO_UINT(a);
1688 guint val_b = GPOINTER_TO_UINT(b);
1690 return val_a - val_b;
1693 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1700 update_list = g_slist_insert_sorted(update_list,
1701 GUINT_TO_POINTER(interval), compare_interval);
1703 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1704 if (min < update_interval) {
1705 update_interval_callback(min);
1706 __connman_rtnl_request_update();
1709 return update_interval;
1712 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1714 guint min = G_MAXUINT;
1719 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1722 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1724 if (min > update_interval)
1725 update_interval_callback(min);
1730 int __connman_rtnl_request_update(void)
1732 return send_getlink();
1735 int __connman_rtnl_init(void)
1737 struct sockaddr_nl addr;
1742 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1743 NULL, free_interface);
1745 sk = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
1749 memset(&addr, 0, sizeof(addr));
1750 addr.nl_family = AF_NETLINK;
1751 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1752 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1753 (1<<(RTNLGRP_ND_USEROPT-1));
1755 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1760 channel = g_io_channel_unix_new(sk);
1761 g_io_channel_set_close_on_unref(channel, TRUE);
1763 g_io_channel_set_encoding(channel, NULL, NULL);
1764 g_io_channel_set_buffered(channel, FALSE);
1766 channel_watch = g_io_add_watch(channel,
1767 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1768 netlink_event, NULL);
1773 void __connman_rtnl_start(void)
1782 void __connman_rtnl_cleanup(void)
1788 for (list = watch_list; list; list = list->next) {
1789 struct watch_data *watch = list->data;
1791 DBG("removing watch %d", watch->id);
1797 g_slist_free(watch_list);
1800 g_slist_free(update_list);
1803 for (list = request_list; list; list = list->next) {
1804 struct rtnl_request *req = list->data;
1806 DBG("%s len %d type %d flags 0x%04x seq %d",
1807 type2string(req->hdr.nlmsg_type),
1808 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1809 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1815 g_slist_free(request_list);
1816 request_list = NULL;
1818 if (channel_watch) {
1819 g_source_remove(channel_watch);
1823 g_io_channel_shutdown(channel, TRUE, NULL);
1824 g_io_channel_unref(channel);
1828 g_hash_table_destroy(interface_list);