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)
97 /* virtual interface from Virtual Machine Manager */
98 if (g_str_has_prefix(name, "virbr") == TRUE)
104 static void read_uevent(struct interface_data *interface)
106 char *filename, line[128];
109 if (ether_blacklisted(interface->name) == TRUE)
110 interface->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
112 interface->type = CONNMAN_SERVICE_TYPE_ETHERNET;
114 filename = g_strdup_printf("/sys/class/net/%s/uevent",
117 f = fopen(filename, "re");
124 while (fgets(line, sizeof(line), f)) {
127 pos = strchr(line, '\n');
132 if (strncmp(line, "DEVTYPE=", 8) != 0)
135 if (strcmp(line + 8, "wlan") == 0)
136 interface->type = CONNMAN_SERVICE_TYPE_WIFI;
137 else if (strcmp(line + 8, "wwan") == 0)
138 interface->type = CONNMAN_SERVICE_TYPE_CELLULAR;
139 else if (strcmp(line + 8, "bluetooth") == 0)
140 interface->type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
141 else if (strcmp(line + 8, "wimax") == 0)
142 interface->type = CONNMAN_SERVICE_TYPE_WIMAX;
144 interface->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
151 * connman_rtnl_add_operstate_watch:
152 * @index: network device index
153 * @callback: callback function
154 * @user_data: callback data;
156 * Add a new RTNL watch for operation state events
158 * Returns: %0 on failure and a unique id on success
160 unsigned int connman_rtnl_add_operstate_watch(int index,
161 connman_rtnl_operstate_cb_t callback, void *user_data)
163 struct watch_data *watch;
165 watch = g_try_new0(struct watch_data, 1);
169 watch->id = ++watch_id;
170 watch->index = index;
172 watch->operstate = callback;
173 watch->user_data = user_data;
175 watch_list = g_slist_prepend(watch_list, watch);
177 DBG("id %d", watch->id);
180 unsigned char operstate = 0;
183 callback(operstate, user_data);
190 * connman_rtnl_add_newlink_watch:
191 * @index: network device index
192 * @callback: callback function
193 * @user_data: callback data;
195 * Add a new RTNL watch for newlink events
197 * Returns: %0 on failure and a unique id on success
199 unsigned int connman_rtnl_add_newlink_watch(int index,
200 connman_rtnl_link_cb_t callback, void *user_data)
202 struct watch_data *watch;
204 watch = g_try_new0(struct watch_data, 1);
208 watch->id = ++watch_id;
209 watch->index = index;
211 watch->newlink = callback;
212 watch->user_data = user_data;
214 watch_list = g_slist_prepend(watch_list, watch);
216 DBG("id %d", watch->id);
219 unsigned int flags = __connman_ipconfig_get_flags(index);
222 callback(flags, 0, user_data);
229 * connman_rtnl_remove_watch:
230 * @id: watch identifier
232 * Remove the RTNL watch for the identifier
234 void connman_rtnl_remove_watch(unsigned int id)
243 for (list = watch_list; list; list = list->next) {
244 struct watch_data *watch = list->data;
246 if (watch->id == id) {
247 watch_list = g_slist_remove(watch_list, watch);
254 static void trigger_rtnl(int index, void *user_data)
256 struct connman_rtnl *rtnl = user_data;
259 unsigned short type = __connman_ipconfig_get_type(index);
260 unsigned int flags = __connman_ipconfig_get_flags(index);
262 rtnl->newlink(type, index, flags, 0);
265 if (rtnl->newgateway) {
266 const char *gateway = __connman_ipconfig_get_gateway(index);
269 rtnl->newgateway(index, gateway);
273 static GSList *rtnl_list = NULL;
275 static gint compare_priority(gconstpointer a, gconstpointer b)
277 const struct connman_rtnl *rtnl1 = a;
278 const struct connman_rtnl *rtnl2 = b;
280 return rtnl2->priority - rtnl1->priority;
284 * connman_rtnl_register:
287 * Register a new RTNL module
289 * Returns: %0 on success
291 int connman_rtnl_register(struct connman_rtnl *rtnl)
293 DBG("rtnl %p name %s", rtnl, rtnl->name);
295 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
298 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
304 * connman_rtnl_unregister:
307 * Remove a previously registered RTNL module
309 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
311 DBG("rtnl %p name %s", rtnl, rtnl->name);
313 rtnl_list = g_slist_remove(rtnl_list, rtnl);
316 static const char *operstate2str(unsigned char operstate)
319 case IF_OPER_UNKNOWN:
321 case IF_OPER_NOTPRESENT:
322 return "NOT-PRESENT";
325 case IF_OPER_LOWERLAYERDOWN:
326 return "LOWER-LAYER-DOWN";
327 case IF_OPER_TESTING:
329 case IF_OPER_DORMANT:
338 static void extract_link(struct ifinfomsg *msg, int bytes,
339 struct ether_addr *address, const char **ifname,
340 unsigned int *mtu, unsigned char *operstate,
341 struct rtnl_link_stats *stats)
345 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
346 attr = RTA_NEXT(attr, bytes)) {
347 switch (attr->rta_type) {
350 memcpy(address, RTA_DATA(attr), ETH_ALEN);
354 *ifname = RTA_DATA(attr);
358 *mtu = *((unsigned int *) RTA_DATA(attr));
362 memcpy(stats, RTA_DATA(attr),
363 sizeof(struct rtnl_link_stats));
366 if (operstate != NULL)
367 *operstate = *((unsigned char *) RTA_DATA(attr));
375 static void process_newlink(unsigned short type, int index, unsigned flags,
376 unsigned change, struct ifinfomsg *msg, int bytes)
378 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
379 struct ether_addr compare = {{ 0, 0, 0, 0, 0, 0 }};
380 struct rtnl_link_stats stats;
381 unsigned char operstate = 0xff;
382 const char *ifname = NULL;
383 unsigned int mtu = 0;
384 char ident[13], str[18];
387 memset(&stats, 0, sizeof(stats));
388 extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats);
390 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
391 address.ether_addr_octet[0],
392 address.ether_addr_octet[1],
393 address.ether_addr_octet[2],
394 address.ether_addr_octet[3],
395 address.ether_addr_octet[4],
396 address.ether_addr_octet[5]);
398 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
399 address.ether_addr_octet[0],
400 address.ether_addr_octet[1],
401 address.ether_addr_octet[2],
402 address.ether_addr_octet[3],
403 address.ether_addr_octet[4],
404 address.ether_addr_octet[5]);
408 case ARPHRD_LOOPBACK:
409 case ARPHDR_PHONET_PIPE:
411 __connman_ipconfig_newlink(index, type, flags,
416 if (memcmp(&address, &compare, ETH_ALEN) != 0)
417 connman_info("%s {newlink} index %d address %s mtu %u",
418 ifname, index, str, mtu);
420 if (operstate != 0xff)
421 connman_info("%s {newlink} index %d operstate %u <%s>",
422 ifname, index, operstate,
423 operstate2str(operstate));
425 if (g_hash_table_lookup(interface_list,
426 GINT_TO_POINTER(index)) == NULL) {
427 struct interface_data *interface;
429 interface = g_new0(struct interface_data, 1);
430 interface->index = index;
431 interface->name = g_strdup(ifname);
432 interface->ident = g_strdup(ident);
434 g_hash_table_insert(interface_list,
435 GINT_TO_POINTER(index), interface);
437 if (type == ARPHRD_ETHER)
438 read_uevent(interface);
440 __connman_technology_add_interface(interface->type,
441 interface->index, interface->name, interface->ident);
444 for (list = rtnl_list; list; list = list->next) {
445 struct connman_rtnl *rtnl = list->data;
448 rtnl->newlink(type, index, flags, change);
451 for (list = watch_list; list; list = list->next) {
452 struct watch_data *watch = list->data;
454 if (watch->index != index)
457 if (operstate != 0xff && watch->operstate)
458 watch->operstate(operstate, watch->user_data);
461 watch->newlink(flags, change, watch->user_data);
465 static void process_dellink(unsigned short type, int index, unsigned flags,
466 unsigned change, struct ifinfomsg *msg, int bytes)
468 struct rtnl_link_stats stats;
469 unsigned char operstate = 0xff;
470 const char *ifname = NULL;
473 memset(&stats, 0, sizeof(stats));
474 extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats);
476 if (operstate != 0xff)
477 connman_info("%s {dellink} index %d operstate %u <%s>",
478 ifname, index, operstate,
479 operstate2str(operstate));
481 for (list = watch_list; list; list = list->next) {
482 struct watch_data *watch = list->data;
484 if (watch->index != index)
487 if (operstate != 0xff && watch->operstate)
488 watch->operstate(operstate, watch->user_data);
491 for (list = rtnl_list; list; list = list->next) {
492 struct connman_rtnl *rtnl = list->data;
495 rtnl->dellink(type, index, flags, change);
500 case ARPHRD_LOOPBACK:
502 __connman_ipconfig_dellink(index, &stats);
506 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
509 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
511 struct in_addr *local,
512 struct in_addr *address,
513 struct in_addr *broadcast)
517 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
518 attr = RTA_NEXT(attr, bytes)) {
519 switch (attr->rta_type) {
522 *address = *((struct in_addr *) RTA_DATA(attr));
526 *local = *((struct in_addr *) RTA_DATA(attr));
529 if (broadcast != NULL)
530 *broadcast = *((struct in_addr *) RTA_DATA(attr));
534 *label = RTA_DATA(attr);
540 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
541 struct in6_addr *addr,
542 struct in6_addr *local)
546 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
547 attr = RTA_NEXT(attr, bytes)) {
548 switch (attr->rta_type) {
551 *addr = *((struct in6_addr *) RTA_DATA(attr));
555 *local = *((struct in6_addr *) RTA_DATA(attr));
561 static void process_newaddr(unsigned char family, unsigned char prefixlen,
562 int index, struct ifaddrmsg *msg, int bytes)
564 const char *label = NULL;
566 char ip_string[INET6_ADDRSTRLEN];
568 if (family != AF_INET && family != AF_INET6)
571 if (family == AF_INET) {
572 struct in_addr ipv4_addr = { INADDR_ANY };
574 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
576 } else if (family == AF_INET6) {
577 struct in6_addr ipv6_address, ipv6_local;
579 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
580 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
586 if (inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN) == NULL)
589 __connman_ipconfig_newaddr(index, family, label,
590 prefixlen, ip_string);
593 static void process_deladdr(unsigned char family, unsigned char prefixlen,
594 int index, struct ifaddrmsg *msg, int bytes)
596 const char *label = NULL;
598 char ip_string[INET6_ADDRSTRLEN];
600 if (family != AF_INET && family != AF_INET6)
603 if (family == AF_INET) {
604 struct in_addr ipv4_addr = { INADDR_ANY };
606 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
608 } else if (family == AF_INET6) {
609 struct in6_addr ipv6_address, ipv6_local;
611 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
612 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
618 if (inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN) == NULL)
621 __connman_ipconfig_deladdr(index, family, label,
622 prefixlen, ip_string);
625 static void extract_route(struct rtmsg *msg, int bytes, int *index,
627 struct in_addr *gateway)
631 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
632 attr = RTA_NEXT(attr, bytes)) {
633 switch (attr->rta_type) {
636 *dst = *((struct in_addr *) RTA_DATA(attr));
640 *gateway = *((struct in_addr *) RTA_DATA(attr));
644 *index = *((int *) RTA_DATA(attr));
650 static void process_newroute(unsigned char family, unsigned char scope,
651 struct rtmsg *msg, int bytes)
654 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
655 char dststr[16], gatewaystr[16];
658 if (family != AF_INET)
661 extract_route(msg, bytes, &index, &dst, &gateway);
663 inet_ntop(family, &dst, dststr, sizeof(dststr));
664 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
666 __connman_ipconfig_newroute(index, family, scope, dststr, gatewaystr);
668 /* skip host specific routes */
669 if (scope != RT_SCOPE_UNIVERSE &&
670 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
673 if (dst.s_addr != INADDR_ANY)
676 for (list = rtnl_list; list; list = list->next) {
677 struct connman_rtnl *rtnl = list->data;
679 if (rtnl->newgateway)
680 rtnl->newgateway(index, gatewaystr);
684 static void process_delroute(unsigned char family, unsigned char scope,
685 struct rtmsg *msg, int bytes)
688 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
689 char dststr[16], gatewaystr[16];
692 if (family != AF_INET)
695 extract_route(msg, bytes, &index, &dst, &gateway);
697 inet_ntop(family, &dst, dststr, sizeof(dststr));
698 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
700 __connman_ipconfig_delroute(index, family, scope, dststr, gatewaystr);
702 /* skip host specific routes */
703 if (scope != RT_SCOPE_UNIVERSE &&
704 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
707 if (dst.s_addr != INADDR_ANY)
710 for (list = rtnl_list; list; list = list->next) {
711 struct connman_rtnl *rtnl = list->data;
713 if (rtnl->delgateway)
714 rtnl->delgateway(index, gatewaystr);
718 static inline void print_ether(struct rtattr *attr, const char *name)
720 int len = (int) RTA_PAYLOAD(attr);
722 if (len == ETH_ALEN) {
723 struct ether_addr eth;
724 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
725 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
727 print(" attr %s (len %d)\n", name, len);
730 static inline void print_inet(struct rtattr *attr, const char *name,
731 unsigned char family)
733 int len = (int) RTA_PAYLOAD(attr);
735 if (family == AF_INET && len == sizeof(struct in_addr)) {
737 addr = *((struct in_addr *) RTA_DATA(attr));
738 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
740 print(" attr %s (len %d)\n", name, len);
743 static inline void print_string(struct rtattr *attr, const char *name)
745 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
746 (char *) RTA_DATA(attr));
749 static inline void print_byte(struct rtattr *attr, const char *name)
751 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
752 *((unsigned char *) RTA_DATA(attr)));
755 static inline void print_integer(struct rtattr *attr, const char *name)
757 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
758 *((int *) RTA_DATA(attr)));
761 static inline void print_attr(struct rtattr *attr, const char *name)
763 int len = (int) RTA_PAYLOAD(attr);
766 print(" attr %s (len %d)\n", name, len);
768 print(" attr %d (len %d)\n", attr->rta_type, len);
771 static void rtnl_link(struct nlmsghdr *hdr)
773 struct ifinfomsg *msg;
777 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
778 bytes = IFLA_PAYLOAD(hdr);
780 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
782 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
783 attr = RTA_NEXT(attr, bytes)) {
784 switch (attr->rta_type) {
786 print_ether(attr, "address");
789 print_ether(attr, "broadcast");
792 print_string(attr, "ifname");
795 print_integer(attr, "mtu");
798 print_attr(attr, "link");
801 print_attr(attr, "qdisc");
804 print_attr(attr, "stats");
807 print_attr(attr, "cost");
810 print_attr(attr, "priority");
813 print_attr(attr, "master");
816 print_attr(attr, "wireless");
819 print_attr(attr, "protinfo");
822 print_integer(attr, "txqlen");
825 print_attr(attr, "map");
828 print_attr(attr, "weight");
831 print_byte(attr, "operstate");
834 print_byte(attr, "linkmode");
837 print_attr(attr, NULL);
843 static void rtnl_newlink(struct nlmsghdr *hdr)
845 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
849 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
850 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
853 static void rtnl_dellink(struct nlmsghdr *hdr)
855 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
859 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
860 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
863 static void rtnl_addr(struct nlmsghdr *hdr)
865 struct ifaddrmsg *msg;
869 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
870 bytes = IFA_PAYLOAD(hdr);
872 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
874 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
875 attr = RTA_NEXT(attr, bytes)) {
876 switch (attr->rta_type) {
878 print_inet(attr, "address", msg->ifa_family);
881 print_inet(attr, "local", msg->ifa_family);
884 print_string(attr, "label");
887 print_inet(attr, "broadcast", msg->ifa_family);
890 print_attr(attr, "anycast");
893 print_attr(attr, "cacheinfo");
896 print_attr(attr, "multicast");
899 print_attr(attr, NULL);
905 static void rtnl_newaddr(struct nlmsghdr *hdr)
907 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
911 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
912 msg, IFA_PAYLOAD(hdr));
915 static void rtnl_deladdr(struct nlmsghdr *hdr)
917 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
921 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
922 msg, IFA_PAYLOAD(hdr));
925 static void rtnl_route(struct nlmsghdr *hdr)
931 msg = (struct rtmsg *) NLMSG_DATA(hdr);
932 bytes = RTM_PAYLOAD(hdr);
934 print("rtm_family %d rtm_table %d rtm_protocol %d",
935 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
936 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
937 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
939 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
940 attr = RTA_NEXT(attr, bytes)) {
941 switch (attr->rta_type) {
943 print_inet(attr, "dst", msg->rtm_family);
946 print_inet(attr, "src", msg->rtm_family);
949 print_string(attr, "iif");
952 print_integer(attr, "oif");
955 print_inet(attr, "gateway", msg->rtm_family);
958 print_attr(attr, "priority");
961 print_inet(attr, "prefsrc", msg->rtm_family);
964 print_attr(attr, "metrics");
967 print_integer(attr, "table");
970 print_attr(attr, NULL);
976 static connman_bool_t is_route_rtmsg(struct rtmsg *msg)
979 if (msg->rtm_table != RT_TABLE_MAIN)
982 if (msg->rtm_protocol != RTPROT_BOOT &&
983 msg->rtm_protocol != RTPROT_KERNEL)
986 if (msg->rtm_type != RTN_UNICAST)
992 static void rtnl_newroute(struct nlmsghdr *hdr)
994 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
998 if (is_route_rtmsg(msg))
999 process_newroute(msg->rtm_family, msg->rtm_scope,
1000 msg, RTM_PAYLOAD(hdr));
1003 static void rtnl_delroute(struct nlmsghdr *hdr)
1005 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1009 if (is_route_rtmsg(msg))
1010 process_delroute(msg->rtm_family, msg->rtm_scope,
1011 msg, RTM_PAYLOAD(hdr));
1014 static const char *type2string(uint16_t type)
1046 static GIOChannel *channel = NULL;
1048 struct rtnl_request {
1049 struct nlmsghdr hdr;
1050 struct rtgenmsg msg;
1052 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1054 static GSList *request_list = NULL;
1055 static guint32 request_seq = 0;
1057 static struct rtnl_request *find_request(guint32 seq)
1061 for (list = request_list; list; list = list->next) {
1062 struct rtnl_request *req = list->data;
1064 if (req->hdr.nlmsg_seq == seq)
1071 static int send_request(struct rtnl_request *req)
1073 struct sockaddr_nl addr;
1076 DBG("%s len %d type %d flags 0x%04x seq %d",
1077 type2string(req->hdr.nlmsg_type),
1078 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1079 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1081 sk = g_io_channel_unix_get_fd(channel);
1083 memset(&addr, 0, sizeof(addr));
1084 addr.nl_family = AF_NETLINK;
1086 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1087 (struct sockaddr *) &addr, sizeof(addr));
1090 static int queue_request(struct rtnl_request *req)
1092 request_list = g_slist_append(request_list, req);
1094 if (g_slist_length(request_list) > 1)
1097 return send_request(req);
1100 static int process_response(guint32 seq)
1102 struct rtnl_request *req;
1106 req = find_request(seq);
1108 request_list = g_slist_remove(request_list, req);
1112 req = g_slist_nth_data(request_list, 0);
1116 return send_request(req);
1119 static void rtnl_message(void *buf, size_t len)
1121 DBG("buf %p len %zd", buf, len);
1124 struct nlmsghdr *hdr = buf;
1125 struct nlmsgerr *err;
1127 if (!NLMSG_OK(hdr, len))
1130 DBG("%s len %d type %d flags 0x%04x seq %d",
1131 type2string(hdr->nlmsg_type),
1132 hdr->nlmsg_len, hdr->nlmsg_type,
1133 hdr->nlmsg_flags, hdr->nlmsg_seq);
1135 switch (hdr->nlmsg_type) {
1140 process_response(hdr->nlmsg_seq);
1143 err = NLMSG_DATA(hdr);
1144 DBG("error %d (%s)", -err->error,
1145 strerror(-err->error));
1167 len -= hdr->nlmsg_len;
1168 buf += hdr->nlmsg_len;
1172 static gboolean netlink_event(GIOChannel *chan,
1173 GIOCondition cond, gpointer data)
1175 unsigned char buf[4096];
1179 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1182 memset(buf, 0, sizeof(buf));
1184 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
1186 if (err == G_IO_ERROR_AGAIN)
1191 rtnl_message(buf, len);
1196 static int send_getlink(void)
1198 struct rtnl_request *req;
1202 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1206 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1207 req->hdr.nlmsg_type = RTM_GETLINK;
1208 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1209 req->hdr.nlmsg_pid = 0;
1210 req->hdr.nlmsg_seq = request_seq++;
1211 req->msg.rtgen_family = AF_INET;
1213 return queue_request(req);
1216 static int send_getaddr(void)
1218 struct rtnl_request *req;
1222 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1226 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1227 req->hdr.nlmsg_type = RTM_GETADDR;
1228 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1229 req->hdr.nlmsg_pid = 0;
1230 req->hdr.nlmsg_seq = request_seq++;
1231 req->msg.rtgen_family = AF_INET;
1233 return queue_request(req);
1236 static int send_getroute(void)
1238 struct rtnl_request *req;
1242 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1246 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1247 req->hdr.nlmsg_type = RTM_GETROUTE;
1248 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1249 req->hdr.nlmsg_pid = 0;
1250 req->hdr.nlmsg_seq = request_seq++;
1251 req->msg.rtgen_family = AF_INET;
1253 return queue_request(req);
1256 static gboolean update_timeout_cb(gpointer user_data)
1258 __connman_rtnl_request_update();
1263 static void update_interval_callback(guint min)
1265 if (update_timeout > 0)
1266 g_source_remove(update_timeout);
1268 if (min < G_MAXUINT) {
1269 update_interval = min;
1270 update_timeout = g_timeout_add_seconds(update_interval,
1271 update_timeout_cb, NULL);
1274 update_interval = G_MAXUINT;
1278 static gint compare_interval(gconstpointer a, gconstpointer b)
1280 guint val_a = GPOINTER_TO_UINT(a);
1281 guint val_b = GPOINTER_TO_UINT(b);
1283 return val_a - val_b;
1286 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1293 update_list = g_slist_insert_sorted(update_list,
1294 GUINT_TO_POINTER(interval), compare_interval);
1296 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1297 if (min < update_interval) {
1298 update_interval_callback(min);
1299 __connman_rtnl_request_update();
1302 return update_interval;
1305 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1307 guint min = G_MAXUINT;
1312 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1314 if (update_list != NULL)
1315 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1317 if (min > update_interval)
1318 update_interval_callback(min);
1323 int __connman_rtnl_request_update(void)
1325 return send_getlink();
1328 int __connman_rtnl_init(void)
1330 struct sockaddr_nl addr;
1335 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1336 NULL, free_interface);
1338 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
1342 memset(&addr, 0, sizeof(addr));
1343 addr.nl_family = AF_NETLINK;
1344 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1345 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE;
1347 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1352 channel = g_io_channel_unix_new(sk);
1353 g_io_channel_set_close_on_unref(channel, TRUE);
1355 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1356 netlink_event, NULL);
1361 void __connman_rtnl_start(void)
1370 void __connman_rtnl_cleanup(void)
1376 for (list = watch_list; list; list = list->next) {
1377 struct watch_data *watch = list->data;
1379 DBG("removing watch %d", watch->id);
1385 g_slist_free(watch_list);
1388 g_slist_free(update_list);
1391 for (list = request_list; list; list = list->next) {
1392 struct rtnl_request *req = list->data;
1394 DBG("%s len %d type %d flags 0x%04x seq %d",
1395 type2string(req->hdr.nlmsg_type),
1396 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1397 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1403 g_slist_free(request_list);
1404 request_list = NULL;
1406 g_io_channel_shutdown(channel, TRUE, NULL);
1407 g_io_channel_unref(channel);
1411 g_hash_table_destroy(interface_list);