5 * Copyright (C) 2007-2010 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
29 #include <sys/socket.h>
30 #include <arpa/inet.h>
31 #include <netinet/ether.h>
32 #include <net/if_arp.h>
34 #include <linux/netlink.h>
35 #include <linux/rtnetlink.h>
41 #ifndef ARPHDR_PHONET_PIPE
42 #define ARPHDR_PHONET_PIPE (821)
45 #define print(arg...) do { } while (0)
46 //#define print(arg...) connman_info(arg)
51 connman_rtnl_operstate_cb_t operstate;
52 connman_rtnl_link_cb_t newlink;
56 static GSList *watch_list = NULL;
57 static unsigned int watch_id = 0;
59 static GSList *update_list = NULL;
60 static guint update_interval = G_MAXUINT;
61 static guint update_timeout = 0;
63 struct interface_data {
67 enum connman_service_type type;
70 static GHashTable *interface_list = NULL;
72 static void free_interface(gpointer data)
74 struct interface_data *interface = data;
76 __connman_technology_remove_interface(interface->type,
77 interface->index, interface->name, interface->ident);
79 g_free(interface->ident);
80 g_free(interface->name);
84 static connman_bool_t ether_blacklisted(const char *name)
89 /* virtual interface from VMware */
90 if (g_str_has_prefix(name, "vmnet") == TRUE)
93 /* virtual interface from VirtualBox */
94 if (g_str_has_prefix(name, "vboxnet") == TRUE)
100 static void read_uevent(struct interface_data *interface)
102 char *filename, line[128];
105 if (ether_blacklisted(interface->name) == TRUE)
106 interface->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
108 interface->type = CONNMAN_SERVICE_TYPE_ETHERNET;
110 filename = g_strdup_printf("/sys/class/net/%s/uevent",
113 f = fopen(filename, "re");
120 while (fgets(line, sizeof(line), f)) {
123 pos = strchr(line, '\n');
128 if (strncmp(line, "DEVTYPE=", 8) != 0)
131 if (strcmp(line + 8, "wlan") == 0)
132 interface->type = CONNMAN_SERVICE_TYPE_WIFI;
133 else if (strcmp(line + 8, "wwan") == 0)
134 interface->type = CONNMAN_SERVICE_TYPE_CELLULAR;
135 else if (strcmp(line + 8, "bluetooth") == 0)
136 interface->type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
137 else if (strcmp(line + 8, "wimax") == 0)
138 interface->type = CONNMAN_SERVICE_TYPE_WIMAX;
140 interface->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
147 * connman_rtnl_add_operstate_watch:
148 * @index: network device index
149 * @callback: callback function
150 * @user_data: callback data;
152 * Add a new RTNL watch for operation state events
154 * Returns: %0 on failure and a unique id on success
156 unsigned int connman_rtnl_add_operstate_watch(int index,
157 connman_rtnl_operstate_cb_t callback, void *user_data)
159 struct watch_data *watch;
161 watch = g_try_new0(struct watch_data, 1);
165 watch->id = ++watch_id;
166 watch->index = index;
168 watch->operstate = callback;
169 watch->user_data = user_data;
171 watch_list = g_slist_prepend(watch_list, watch);
173 DBG("id %d", watch->id);
176 unsigned char operstate = 0;
179 callback(operstate, user_data);
186 * connman_rtnl_add_newlink_watch:
187 * @index: network device index
188 * @callback: callback function
189 * @user_data: callback data;
191 * Add a new RTNL watch for newlink events
193 * Returns: %0 on failure and a unique id on success
195 unsigned int connman_rtnl_add_newlink_watch(int index,
196 connman_rtnl_link_cb_t callback, void *user_data)
198 struct watch_data *watch;
200 watch = g_try_new0(struct watch_data, 1);
204 watch->id = ++watch_id;
205 watch->index = index;
207 watch->newlink = callback;
208 watch->user_data = user_data;
210 watch_list = g_slist_prepend(watch_list, watch);
212 DBG("id %d", watch->id);
215 unsigned int flags = __connman_ipconfig_get_flags(index);
218 callback(flags, 0, user_data);
225 * connman_rtnl_remove_watch:
226 * @id: watch identifier
228 * Remove the RTNL watch for the identifier
230 void connman_rtnl_remove_watch(unsigned int id)
239 for (list = watch_list; list; list = list->next) {
240 struct watch_data *watch = list->data;
242 if (watch->id == id) {
243 watch_list = g_slist_remove(watch_list, watch);
250 static void trigger_rtnl(int index, void *user_data)
252 struct connman_rtnl *rtnl = user_data;
255 unsigned short type = __connman_ipconfig_get_type(index);
256 unsigned int flags = __connman_ipconfig_get_flags(index);
258 rtnl->newlink(type, index, flags, 0);
261 if (rtnl->newgateway) {
262 const char *gateway = __connman_ipconfig_get_gateway(index);
265 rtnl->newgateway(index, gateway);
269 static GSList *rtnl_list = NULL;
271 static gint compare_priority(gconstpointer a, gconstpointer b)
273 const struct connman_rtnl *rtnl1 = a;
274 const struct connman_rtnl *rtnl2 = b;
276 return rtnl2->priority - rtnl1->priority;
280 * connman_rtnl_register:
283 * Register a new RTNL module
285 * Returns: %0 on success
287 int connman_rtnl_register(struct connman_rtnl *rtnl)
289 DBG("rtnl %p name %s", rtnl, rtnl->name);
291 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
294 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
300 * connman_rtnl_unregister:
303 * Remove a previously registered RTNL module
305 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
307 DBG("rtnl %p name %s", rtnl, rtnl->name);
309 rtnl_list = g_slist_remove(rtnl_list, rtnl);
312 static const char *operstate2str(unsigned char operstate)
315 case IF_OPER_UNKNOWN:
317 case IF_OPER_NOTPRESENT:
318 return "NOT-PRESENT";
321 case IF_OPER_LOWERLAYERDOWN:
322 return "LOWER-LAYER-DOWN";
323 case IF_OPER_TESTING:
325 case IF_OPER_DORMANT:
334 static void extract_link(struct ifinfomsg *msg, int bytes,
335 struct ether_addr *address, const char **ifname,
336 unsigned int *mtu, unsigned char *operstate,
337 struct rtnl_link_stats *stats)
341 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
342 attr = RTA_NEXT(attr, bytes)) {
343 switch (attr->rta_type) {
346 memcpy(address, RTA_DATA(attr), ETH_ALEN);
350 *ifname = RTA_DATA(attr);
354 *mtu = *((unsigned int *) RTA_DATA(attr));
358 memcpy(stats, RTA_DATA(attr),
359 sizeof(struct rtnl_link_stats));
362 if (operstate != NULL)
363 *operstate = *((unsigned char *) RTA_DATA(attr));
371 static void process_newlink(unsigned short type, int index, unsigned flags,
372 unsigned change, struct ifinfomsg *msg, int bytes)
374 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
375 struct ether_addr compare = {{ 0, 0, 0, 0, 0, 0 }};
376 struct rtnl_link_stats stats;
377 unsigned char operstate = 0xff;
378 const char *ifname = NULL;
379 unsigned int mtu = 0;
380 char ident[13], str[18];
383 memset(&stats, 0, sizeof(stats));
384 extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats);
386 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
387 address.ether_addr_octet[0],
388 address.ether_addr_octet[1],
389 address.ether_addr_octet[2],
390 address.ether_addr_octet[3],
391 address.ether_addr_octet[4],
392 address.ether_addr_octet[5]);
394 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
395 address.ether_addr_octet[0],
396 address.ether_addr_octet[1],
397 address.ether_addr_octet[2],
398 address.ether_addr_octet[3],
399 address.ether_addr_octet[4],
400 address.ether_addr_octet[5]);
404 case ARPHRD_LOOPBACK:
405 case ARPHDR_PHONET_PIPE:
407 __connman_ipconfig_newlink(index, type, flags,
412 if (memcmp(&address, &compare, ETH_ALEN) != 0)
413 connman_info("%s {newlink} index %d address %s mtu %u",
414 ifname, index, str, mtu);
416 if (operstate != 0xff)
417 connman_info("%s {newlink} index %d operstate %u <%s>",
418 ifname, index, operstate,
419 operstate2str(operstate));
421 if (g_hash_table_lookup(interface_list,
422 GINT_TO_POINTER(index)) == NULL) {
423 struct interface_data *interface;
425 interface = g_new0(struct interface_data, 1);
426 interface->index = index;
427 interface->name = g_strdup(ifname);
428 interface->ident = g_strdup(ident);
430 g_hash_table_insert(interface_list,
431 GINT_TO_POINTER(index), interface);
433 if (type == ARPHRD_ETHER)
434 read_uevent(interface);
436 __connman_technology_add_interface(interface->type,
437 interface->index, interface->name, interface->ident);
440 for (list = rtnl_list; list; list = list->next) {
441 struct connman_rtnl *rtnl = list->data;
444 rtnl->newlink(type, index, flags, change);
447 for (list = watch_list; list; list = list->next) {
448 struct watch_data *watch = list->data;
450 if (watch->index != index)
453 if (operstate != 0xff && watch->operstate)
454 watch->operstate(operstate, watch->user_data);
457 watch->newlink(flags, change, watch->user_data);
461 static void process_dellink(unsigned short type, int index, unsigned flags,
462 unsigned change, struct ifinfomsg *msg, int bytes)
464 struct rtnl_link_stats stats;
465 unsigned char operstate = 0xff;
466 const char *ifname = NULL;
469 memset(&stats, 0, sizeof(stats));
470 extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats);
472 if (operstate != 0xff)
473 connman_info("%s {dellink} index %d operstate %u <%s>",
474 ifname, index, operstate,
475 operstate2str(operstate));
477 for (list = watch_list; list; list = list->next) {
478 struct watch_data *watch = list->data;
480 if (watch->index != index)
483 if (operstate != 0xff && watch->operstate)
484 watch->operstate(operstate, watch->user_data);
487 for (list = rtnl_list; list; list = list->next) {
488 struct connman_rtnl *rtnl = list->data;
491 rtnl->dellink(type, index, flags, change);
496 case ARPHRD_LOOPBACK:
498 __connman_ipconfig_dellink(index, &stats);
502 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
505 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
507 struct in_addr *local,
508 struct in_addr *address,
509 struct in_addr *broadcast)
513 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
514 attr = RTA_NEXT(attr, bytes)) {
515 switch (attr->rta_type) {
518 *address = *((struct in_addr *) RTA_DATA(attr));
522 *local = *((struct in_addr *) RTA_DATA(attr));
525 if (broadcast != NULL)
526 *broadcast = *((struct in_addr *) RTA_DATA(attr));
530 *label = RTA_DATA(attr);
536 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
537 struct in6_addr *addr,
538 struct in6_addr *local)
542 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
543 attr = RTA_NEXT(attr, bytes)) {
544 switch (attr->rta_type) {
547 *addr = *((struct in6_addr *) RTA_DATA(attr));
551 *local = *((struct in6_addr *) RTA_DATA(attr));
557 static void process_newaddr(unsigned char family, unsigned char prefixlen,
558 int index, struct ifaddrmsg *msg, int bytes)
560 const char *label = NULL;
562 char ip_string[INET6_ADDRSTRLEN];
564 if (family != AF_INET && family != AF_INET6)
567 if (family == AF_INET) {
568 struct in_addr ipv4_addr = { INADDR_ANY };
570 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
572 } else if (family == AF_INET6) {
573 struct in6_addr ipv6_address, ipv6_local;
575 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
576 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
582 if (inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN) == NULL)
585 __connman_ipconfig_newaddr(index, family, label,
586 prefixlen, ip_string);
589 static void process_deladdr(unsigned char family, unsigned char prefixlen,
590 int index, struct ifaddrmsg *msg, int bytes)
592 const char *label = NULL;
594 char ip_string[INET6_ADDRSTRLEN];
596 if (family != AF_INET && family != AF_INET6)
599 if (family == AF_INET) {
600 struct in_addr ipv4_addr = { INADDR_ANY };
602 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
604 } else if (family == AF_INET6) {
605 struct in6_addr ipv6_address, ipv6_local;
607 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
608 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
614 if (inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN) == NULL)
617 __connman_ipconfig_deladdr(index, family, label,
618 prefixlen, ip_string);
621 static void extract_route(struct rtmsg *msg, int bytes, int *index,
623 struct in_addr *gateway)
627 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
628 attr = RTA_NEXT(attr, bytes)) {
629 switch (attr->rta_type) {
632 *dst = *((struct in_addr *) RTA_DATA(attr));
636 *gateway = *((struct in_addr *) RTA_DATA(attr));
640 *index = *((int *) RTA_DATA(attr));
646 static void process_newroute(unsigned char family, unsigned char scope,
647 struct rtmsg *msg, int bytes)
650 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
651 char dststr[16], gatewaystr[16];
654 if (family != AF_INET)
657 extract_route(msg, bytes, &index, &dst, &gateway);
659 inet_ntop(family, &dst, dststr, sizeof(dststr));
660 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
662 __connman_ipconfig_newroute(index, family, scope, dststr, gatewaystr);
664 /* skip host specific routes */
665 if (scope != RT_SCOPE_UNIVERSE &&
666 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
669 if (dst.s_addr != INADDR_ANY)
672 for (list = rtnl_list; list; list = list->next) {
673 struct connman_rtnl *rtnl = list->data;
675 if (rtnl->newgateway)
676 rtnl->newgateway(index, gatewaystr);
680 static void process_delroute(unsigned char family, unsigned char scope,
681 struct rtmsg *msg, int bytes)
684 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
685 char dststr[16], gatewaystr[16];
688 if (family != AF_INET)
691 extract_route(msg, bytes, &index, &dst, &gateway);
693 inet_ntop(family, &dst, dststr, sizeof(dststr));
694 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
696 __connman_ipconfig_delroute(index, family, scope, dststr, gatewaystr);
698 /* skip host specific routes */
699 if (scope != RT_SCOPE_UNIVERSE &&
700 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
703 if (dst.s_addr != INADDR_ANY)
706 for (list = rtnl_list; list; list = list->next) {
707 struct connman_rtnl *rtnl = list->data;
709 if (rtnl->delgateway)
710 rtnl->delgateway(index, gatewaystr);
714 static inline void print_ether(struct rtattr *attr, const char *name)
716 int len = (int) RTA_PAYLOAD(attr);
718 if (len == ETH_ALEN) {
719 struct ether_addr eth;
720 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
721 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
723 print(" attr %s (len %d)\n", name, len);
726 static inline void print_inet(struct rtattr *attr, const char *name,
727 unsigned char family)
729 int len = (int) RTA_PAYLOAD(attr);
731 if (family == AF_INET && len == sizeof(struct in_addr)) {
733 addr = *((struct in_addr *) RTA_DATA(attr));
734 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
736 print(" attr %s (len %d)\n", name, len);
739 static inline void print_string(struct rtattr *attr, const char *name)
741 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
742 (char *) RTA_DATA(attr));
745 static inline void print_byte(struct rtattr *attr, const char *name)
747 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
748 *((unsigned char *) RTA_DATA(attr)));
751 static inline void print_integer(struct rtattr *attr, const char *name)
753 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
754 *((int *) RTA_DATA(attr)));
757 static inline void print_attr(struct rtattr *attr, const char *name)
759 int len = (int) RTA_PAYLOAD(attr);
762 print(" attr %s (len %d)\n", name, len);
764 print(" attr %d (len %d)\n", attr->rta_type, len);
767 static void rtnl_link(struct nlmsghdr *hdr)
769 struct ifinfomsg *msg;
773 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
774 bytes = IFLA_PAYLOAD(hdr);
776 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
778 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
779 attr = RTA_NEXT(attr, bytes)) {
780 switch (attr->rta_type) {
782 print_ether(attr, "address");
785 print_ether(attr, "broadcast");
788 print_string(attr, "ifname");
791 print_integer(attr, "mtu");
794 print_attr(attr, "link");
797 print_attr(attr, "qdisc");
800 print_attr(attr, "stats");
803 print_attr(attr, "cost");
806 print_attr(attr, "priority");
809 print_attr(attr, "master");
812 print_attr(attr, "wireless");
815 print_attr(attr, "protinfo");
818 print_integer(attr, "txqlen");
821 print_attr(attr, "map");
824 print_attr(attr, "weight");
827 print_byte(attr, "operstate");
830 print_byte(attr, "linkmode");
833 print_attr(attr, NULL);
839 static void rtnl_newlink(struct nlmsghdr *hdr)
841 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
845 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
846 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
849 static void rtnl_dellink(struct nlmsghdr *hdr)
851 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
855 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
856 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
859 static void rtnl_addr(struct nlmsghdr *hdr)
861 struct ifaddrmsg *msg;
865 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
866 bytes = IFA_PAYLOAD(hdr);
868 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
870 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
871 attr = RTA_NEXT(attr, bytes)) {
872 switch (attr->rta_type) {
874 print_inet(attr, "address", msg->ifa_family);
877 print_inet(attr, "local", msg->ifa_family);
880 print_string(attr, "label");
883 print_inet(attr, "broadcast", msg->ifa_family);
886 print_attr(attr, "anycast");
889 print_attr(attr, "cacheinfo");
892 print_attr(attr, "multicast");
895 print_attr(attr, NULL);
901 static void rtnl_newaddr(struct nlmsghdr *hdr)
903 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
907 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
908 msg, IFA_PAYLOAD(hdr));
911 static void rtnl_deladdr(struct nlmsghdr *hdr)
913 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
917 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
918 msg, IFA_PAYLOAD(hdr));
921 static void rtnl_route(struct nlmsghdr *hdr)
927 msg = (struct rtmsg *) NLMSG_DATA(hdr);
928 bytes = RTM_PAYLOAD(hdr);
930 print("rtm_family %d rtm_table %d rtm_protocol %d",
931 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
932 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
933 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
935 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
936 attr = RTA_NEXT(attr, bytes)) {
937 switch (attr->rta_type) {
939 print_inet(attr, "dst", msg->rtm_family);
942 print_inet(attr, "src", msg->rtm_family);
945 print_string(attr, "iif");
948 print_integer(attr, "oif");
951 print_inet(attr, "gateway", msg->rtm_family);
954 print_attr(attr, "priority");
957 print_inet(attr, "prefsrc", msg->rtm_family);
960 print_attr(attr, "metrics");
963 print_integer(attr, "table");
966 print_attr(attr, NULL);
972 static connman_bool_t is_route_rtmsg(struct rtmsg *msg)
975 if (msg->rtm_table != RT_TABLE_MAIN)
978 if (msg->rtm_protocol != RTPROT_BOOT &&
979 msg->rtm_protocol != RTPROT_KERNEL)
982 if (msg->rtm_type != RTN_UNICAST)
988 static void rtnl_newroute(struct nlmsghdr *hdr)
990 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
994 if (is_route_rtmsg(msg))
995 process_newroute(msg->rtm_family, msg->rtm_scope,
996 msg, RTM_PAYLOAD(hdr));
999 static void rtnl_delroute(struct nlmsghdr *hdr)
1001 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1005 if (is_route_rtmsg(msg))
1006 process_delroute(msg->rtm_family, msg->rtm_scope,
1007 msg, RTM_PAYLOAD(hdr));
1010 static const char *type2string(uint16_t type)
1042 static GIOChannel *channel = NULL;
1044 struct rtnl_request {
1045 struct nlmsghdr hdr;
1046 struct rtgenmsg msg;
1048 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1050 static GSList *request_list = NULL;
1051 static guint32 request_seq = 0;
1053 static struct rtnl_request *find_request(guint32 seq)
1057 for (list = request_list; list; list = list->next) {
1058 struct rtnl_request *req = list->data;
1060 if (req->hdr.nlmsg_seq == seq)
1067 static int send_request(struct rtnl_request *req)
1069 struct sockaddr_nl addr;
1072 DBG("%s len %d type %d flags 0x%04x seq %d",
1073 type2string(req->hdr.nlmsg_type),
1074 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1075 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1077 sk = g_io_channel_unix_get_fd(channel);
1079 memset(&addr, 0, sizeof(addr));
1080 addr.nl_family = AF_NETLINK;
1082 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1083 (struct sockaddr *) &addr, sizeof(addr));
1086 static int queue_request(struct rtnl_request *req)
1088 request_list = g_slist_append(request_list, req);
1090 if (g_slist_length(request_list) > 1)
1093 return send_request(req);
1096 static int process_response(guint32 seq)
1098 struct rtnl_request *req;
1102 req = find_request(seq);
1104 request_list = g_slist_remove(request_list, req);
1108 req = g_slist_nth_data(request_list, 0);
1112 return send_request(req);
1115 static void rtnl_message(void *buf, size_t len)
1117 DBG("buf %p len %zd", buf, len);
1120 struct nlmsghdr *hdr = buf;
1121 struct nlmsgerr *err;
1123 if (!NLMSG_OK(hdr, len))
1126 DBG("%s len %d type %d flags 0x%04x seq %d",
1127 type2string(hdr->nlmsg_type),
1128 hdr->nlmsg_len, hdr->nlmsg_type,
1129 hdr->nlmsg_flags, hdr->nlmsg_seq);
1131 switch (hdr->nlmsg_type) {
1136 process_response(hdr->nlmsg_seq);
1139 err = NLMSG_DATA(hdr);
1140 DBG("error %d (%s)", -err->error,
1141 strerror(-err->error));
1163 len -= hdr->nlmsg_len;
1164 buf += hdr->nlmsg_len;
1168 static gboolean netlink_event(GIOChannel *chan,
1169 GIOCondition cond, gpointer data)
1171 unsigned char buf[4096];
1175 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1178 memset(buf, 0, sizeof(buf));
1180 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
1182 if (err == G_IO_ERROR_AGAIN)
1187 rtnl_message(buf, len);
1192 static int send_getlink(void)
1194 struct rtnl_request *req;
1198 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1202 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1203 req->hdr.nlmsg_type = RTM_GETLINK;
1204 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1205 req->hdr.nlmsg_pid = 0;
1206 req->hdr.nlmsg_seq = request_seq++;
1207 req->msg.rtgen_family = AF_INET;
1209 return queue_request(req);
1212 static int send_getaddr(void)
1214 struct rtnl_request *req;
1218 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1222 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1223 req->hdr.nlmsg_type = RTM_GETADDR;
1224 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1225 req->hdr.nlmsg_pid = 0;
1226 req->hdr.nlmsg_seq = request_seq++;
1227 req->msg.rtgen_family = AF_INET;
1229 return queue_request(req);
1232 static int send_getroute(void)
1234 struct rtnl_request *req;
1238 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1242 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1243 req->hdr.nlmsg_type = RTM_GETROUTE;
1244 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1245 req->hdr.nlmsg_pid = 0;
1246 req->hdr.nlmsg_seq = request_seq++;
1247 req->msg.rtgen_family = AF_INET;
1249 return queue_request(req);
1252 static gboolean update_timeout_cb(gpointer user_data)
1254 __connman_rtnl_request_update();
1259 static void update_interval_callback(guint min)
1261 if (update_timeout > 0)
1262 g_source_remove(update_timeout);
1264 if (min < G_MAXUINT) {
1265 update_interval = min;
1266 update_timeout = g_timeout_add_seconds(update_interval,
1267 update_timeout_cb, NULL);
1270 update_interval = G_MAXUINT;
1274 static gint compare_interval(gconstpointer a, gconstpointer b)
1276 guint val_a = GPOINTER_TO_UINT(a);
1277 guint val_b = GPOINTER_TO_UINT(b);
1279 return val_a - val_b;
1282 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1289 update_list = g_slist_insert_sorted(update_list,
1290 GUINT_TO_POINTER(interval), compare_interval);
1292 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1293 if (min < update_interval) {
1294 update_interval_callback(min);
1295 __connman_rtnl_request_update();
1298 return update_interval;
1301 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1303 guint min = G_MAXUINT;
1308 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1310 if (update_list != NULL)
1311 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1313 if (min > update_interval)
1314 update_interval_callback(min);
1319 int __connman_rtnl_request_update(void)
1321 return send_getlink();
1324 int __connman_rtnl_init(void)
1326 struct sockaddr_nl addr;
1331 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1332 NULL, free_interface);
1334 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
1338 memset(&addr, 0, sizeof(addr));
1339 addr.nl_family = AF_NETLINK;
1340 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1341 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE;
1343 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1348 channel = g_io_channel_unix_new(sk);
1349 g_io_channel_set_close_on_unref(channel, TRUE);
1351 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1352 netlink_event, NULL);
1357 void __connman_rtnl_start(void)
1366 void __connman_rtnl_cleanup(void)
1372 for (list = watch_list; list; list = list->next) {
1373 struct watch_data *watch = list->data;
1375 DBG("removing watch %d", watch->id);
1381 g_slist_free(watch_list);
1384 g_slist_free(update_list);
1387 for (list = request_list; list; list = list->next) {
1388 struct rtnl_request *req = list->data;
1390 DBG("%s len %d type %d flags 0x%04x seq %d",
1391 type2string(req->hdr.nlmsg_type),
1392 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1393 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1399 g_slist_free(request_list);
1400 request_list = NULL;
1402 g_io_channel_shutdown(channel, TRUE, NULL);
1403 g_io_channel_unref(channel);
1407 g_hash_table_destroy(interface_list);