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 #define print(arg...) do { } while (0)
42 //#define print(arg...) connman_info(arg)
47 connman_rtnl_operstate_cb_t operstate;
48 connman_rtnl_link_cb_t newlink;
52 static GSList *watch_list = NULL;
53 static unsigned int watch_id = 0;
55 static GSList *update_list = NULL;
56 static guint update_interval = G_MAXUINT;
57 static guint update_timeout = 0;
59 struct interface_data {
63 enum connman_service_type type;
66 static GHashTable *interface_list = NULL;
68 static void free_interface(gpointer data)
70 struct interface_data *interface = data;
72 __connman_technology_remove_interface(interface->type,
73 interface->index, interface->name, interface->ident);
75 g_free(interface->ident);
76 g_free(interface->name);
80 static connman_bool_t ether_blacklisted(const char *name)
85 /* virtual interface from VMware */
86 if (g_str_has_prefix(name, "vmnet") == TRUE)
89 /* virtual interface from VirtualBox */
90 if (g_str_has_prefix(name, "vboxnet") == TRUE)
96 static void read_uevent(struct interface_data *interface)
98 char *filename, line[128];
101 if (ether_blacklisted(interface->name) == TRUE)
102 interface->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
104 interface->type = CONNMAN_SERVICE_TYPE_ETHERNET;
106 filename = g_strdup_printf("/sys/class/net/%s/uevent",
109 f = fopen(filename, "re");
116 while (fgets(line, sizeof(line), f)) {
119 pos = strchr(line, '\n');
124 if (strncmp(line, "DEVTYPE=", 8) != 0)
127 if (strcmp(line + 8, "wlan") == 0)
128 interface->type = CONNMAN_SERVICE_TYPE_WIFI;
129 else if (strcmp(line + 8, "wwan") == 0)
130 interface->type = CONNMAN_SERVICE_TYPE_CELLULAR;
131 else if (strcmp(line + 8, "bluetooth") == 0)
132 interface->type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
133 else if (strcmp(line + 8, "wimax") == 0)
134 interface->type = CONNMAN_SERVICE_TYPE_WIMAX;
136 interface->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
143 * connman_rtnl_add_operstate_watch:
144 * @index: network device index
145 * @callback: callback function
146 * @user_data: callback data;
148 * Add a new RTNL watch for operation state events
150 * Returns: %0 on failure and a unique id on success
152 unsigned int connman_rtnl_add_operstate_watch(int index,
153 connman_rtnl_operstate_cb_t callback, void *user_data)
155 struct watch_data *watch;
157 watch = g_try_new0(struct watch_data, 1);
161 watch->id = ++watch_id;
162 watch->index = index;
164 watch->operstate = callback;
165 watch->user_data = user_data;
167 watch_list = g_slist_prepend(watch_list, watch);
169 DBG("id %d", watch->id);
172 unsigned char operstate = 0;
175 callback(operstate, user_data);
182 * connman_rtnl_add_newlink_watch:
183 * @index: network device index
184 * @callback: callback function
185 * @user_data: callback data;
187 * Add a new RTNL watch for newlink events
189 * Returns: %0 on failure and a unique id on success
191 unsigned int connman_rtnl_add_newlink_watch(int index,
192 connman_rtnl_link_cb_t callback, void *user_data)
194 struct watch_data *watch;
196 watch = g_try_new0(struct watch_data, 1);
200 watch->id = ++watch_id;
201 watch->index = index;
203 watch->newlink = callback;
204 watch->user_data = user_data;
206 watch_list = g_slist_prepend(watch_list, watch);
208 DBG("id %d", watch->id);
211 unsigned int flags = __connman_ipconfig_get_flags(index);
214 callback(flags, 0, user_data);
221 * connman_rtnl_remove_watch:
222 * @id: watch identifier
224 * Remove the RTNL watch for the identifier
226 void connman_rtnl_remove_watch(unsigned int id)
235 for (list = watch_list; list; list = list->next) {
236 struct watch_data *watch = list->data;
238 if (watch->id == id) {
239 watch_list = g_slist_remove(watch_list, watch);
246 static void trigger_rtnl(int index, void *user_data)
248 struct connman_rtnl *rtnl = user_data;
251 unsigned short type = __connman_ipconfig_get_type(index);
252 unsigned int flags = __connman_ipconfig_get_flags(index);
254 rtnl->newlink(type, index, flags, 0);
257 if (rtnl->newgateway) {
258 const char *gateway = __connman_ipconfig_get_gateway(index);
261 rtnl->newgateway(index, gateway);
265 static GSList *rtnl_list = NULL;
267 static gint compare_priority(gconstpointer a, gconstpointer b)
269 const struct connman_rtnl *rtnl1 = a;
270 const struct connman_rtnl *rtnl2 = b;
272 return rtnl2->priority - rtnl1->priority;
276 * connman_rtnl_register:
279 * Register a new RTNL module
281 * Returns: %0 on success
283 int connman_rtnl_register(struct connman_rtnl *rtnl)
285 DBG("rtnl %p name %s", rtnl, rtnl->name);
287 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
290 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
296 * connman_rtnl_unregister:
299 * Remove a previously registered RTNL module
301 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
303 DBG("rtnl %p name %s", rtnl, rtnl->name);
305 rtnl_list = g_slist_remove(rtnl_list, rtnl);
308 static const char *operstate2str(unsigned char operstate)
311 case IF_OPER_UNKNOWN:
313 case IF_OPER_NOTPRESENT:
314 return "NOT-PRESENT";
317 case IF_OPER_LOWERLAYERDOWN:
318 return "LOWER-LAYER-DOWN";
319 case IF_OPER_TESTING:
321 case IF_OPER_DORMANT:
330 static void extract_link(struct ifinfomsg *msg, int bytes,
331 struct ether_addr *address, const char **ifname,
332 unsigned int *mtu, unsigned char *operstate,
333 struct rtnl_link_stats *stats)
337 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
338 attr = RTA_NEXT(attr, bytes)) {
339 switch (attr->rta_type) {
342 memcpy(address, RTA_DATA(attr), ETH_ALEN);
346 *ifname = RTA_DATA(attr);
350 *mtu = *((unsigned int *) RTA_DATA(attr));
354 memcpy(stats, RTA_DATA(attr),
355 sizeof(struct rtnl_link_stats));
358 if (operstate != NULL)
359 *operstate = *((unsigned char *) RTA_DATA(attr));
367 static void process_newlink(unsigned short type, int index, unsigned flags,
368 unsigned change, struct ifinfomsg *msg, int bytes)
370 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
371 struct ether_addr compare = {{ 0, 0, 0, 0, 0, 0 }};
372 struct rtnl_link_stats stats;
373 unsigned char operstate = 0xff;
374 const char *ifname = NULL;
375 unsigned int mtu = 0;
376 char ident[13], str[18];
379 memset(&stats, 0, sizeof(stats));
380 extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats);
382 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
383 address.ether_addr_octet[0],
384 address.ether_addr_octet[1],
385 address.ether_addr_octet[2],
386 address.ether_addr_octet[3],
387 address.ether_addr_octet[4],
388 address.ether_addr_octet[5]);
390 snprintf(str, 18, "%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]);
400 case ARPHRD_LOOPBACK:
402 __connman_ipconfig_newlink(index, type, flags,
407 if (memcmp(&address, &compare, ETH_ALEN) != 0)
408 connman_info("%s {newlink} index %d address %s mtu %u",
409 ifname, index, str, mtu);
411 if (operstate != 0xff)
412 connman_info("%s {newlink} index %d operstate %u <%s>",
413 ifname, index, operstate,
414 operstate2str(operstate));
416 if (g_hash_table_lookup(interface_list,
417 GINT_TO_POINTER(index)) == NULL) {
418 struct interface_data *interface;
420 interface = g_new0(struct interface_data, 1);
421 interface->index = index;
422 interface->name = g_strdup(ifname);
423 interface->ident = g_strdup(ident);
425 g_hash_table_insert(interface_list,
426 GINT_TO_POINTER(index), interface);
428 if (type == ARPHRD_ETHER)
429 read_uevent(interface);
431 __connman_technology_add_interface(interface->type,
432 interface->index, interface->name, interface->ident);
435 for (list = rtnl_list; list; list = list->next) {
436 struct connman_rtnl *rtnl = list->data;
439 rtnl->newlink(type, index, flags, change);
442 for (list = watch_list; list; list = list->next) {
443 struct watch_data *watch = list->data;
445 if (watch->index != index)
448 if (operstate != 0xff && watch->operstate)
449 watch->operstate(operstate, watch->user_data);
452 watch->newlink(flags, change, watch->user_data);
456 static void process_dellink(unsigned short type, int index, unsigned flags,
457 unsigned change, struct ifinfomsg *msg, int bytes)
459 struct rtnl_link_stats stats;
460 unsigned char operstate = 0xff;
461 const char *ifname = NULL;
464 memset(&stats, 0, sizeof(stats));
465 extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats);
467 if (operstate != 0xff)
468 connman_info("%s {dellink} index %d operstate %u <%s>",
469 ifname, index, operstate,
470 operstate2str(operstate));
472 for (list = watch_list; list; list = list->next) {
473 struct watch_data *watch = list->data;
475 if (watch->index != index)
478 if (operstate != 0xff && watch->operstate)
479 watch->operstate(operstate, watch->user_data);
482 for (list = rtnl_list; list; list = list->next) {
483 struct connman_rtnl *rtnl = list->data;
486 rtnl->dellink(type, index, flags, change);
491 case ARPHRD_LOOPBACK:
493 __connman_ipconfig_dellink(index, &stats);
497 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
500 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
502 struct in_addr *local,
503 struct in_addr *address,
504 struct in_addr *broadcast)
508 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
509 attr = RTA_NEXT(attr, bytes)) {
510 switch (attr->rta_type) {
513 *address = *((struct in_addr *) RTA_DATA(attr));
517 *local = *((struct in_addr *) RTA_DATA(attr));
520 if (broadcast != NULL)
521 *broadcast = *((struct in_addr *) RTA_DATA(attr));
525 *label = RTA_DATA(attr);
531 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
532 struct in6_addr *addr,
533 struct in6_addr *local)
537 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
538 attr = RTA_NEXT(attr, bytes)) {
539 switch (attr->rta_type) {
542 *addr = *((struct in6_addr *) RTA_DATA(attr));
546 *local = *((struct in6_addr *) RTA_DATA(attr));
552 static void process_newaddr(unsigned char family, unsigned char prefixlen,
553 int index, struct ifaddrmsg *msg, int bytes)
555 const char *label = NULL;
557 char ip_string[INET6_ADDRSTRLEN];
559 if (family != AF_INET && family != AF_INET6)
562 if (family == AF_INET) {
563 struct in_addr ipv4_addr = { INADDR_ANY };
565 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
567 } else if (family == AF_INET6) {
568 struct in6_addr ipv6_address, ipv6_local;
570 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
574 if (inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN) == NULL)
577 __connman_ipconfig_newaddr(index, family, label,
578 prefixlen, ip_string);
581 static void process_deladdr(unsigned char family, unsigned char prefixlen,
582 int index, struct ifaddrmsg *msg, int bytes)
584 const char *label = NULL;
586 char ip_string[INET6_ADDRSTRLEN];
588 if (family != AF_INET && family != AF_INET6)
591 if (family == AF_INET) {
592 struct in_addr ipv4_addr = { INADDR_ANY };
594 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
596 } else if (family == AF_INET6) {
597 struct in6_addr ipv6_address, ipv6_local;
599 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
603 if (inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN) == NULL)
606 __connman_ipconfig_deladdr(index, family, label,
607 prefixlen, ip_string);
610 static void extract_route(struct rtmsg *msg, int bytes, int *index,
612 struct in_addr *gateway)
616 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
617 attr = RTA_NEXT(attr, bytes)) {
618 switch (attr->rta_type) {
621 *dst = *((struct in_addr *) RTA_DATA(attr));
625 *gateway = *((struct in_addr *) RTA_DATA(attr));
629 *index = *((int *) RTA_DATA(attr));
635 static void process_newroute(unsigned char family, unsigned char scope,
636 struct rtmsg *msg, int bytes)
639 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
640 char dststr[16], gatewaystr[16];
643 if (family != AF_INET)
646 extract_route(msg, bytes, &index, &dst, &gateway);
648 inet_ntop(family, &dst, dststr, sizeof(dststr));
649 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
651 __connman_ipconfig_newroute(index, scope, dststr, gatewaystr);
653 /* skip host specific routes */
654 if (scope != RT_SCOPE_UNIVERSE &&
655 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
658 if (dst.s_addr != INADDR_ANY)
661 for (list = rtnl_list; list; list = list->next) {
662 struct connman_rtnl *rtnl = list->data;
664 if (rtnl->newgateway)
665 rtnl->newgateway(index, gatewaystr);
669 static void process_delroute(unsigned char family, unsigned char scope,
670 struct rtmsg *msg, int bytes)
673 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
674 char dststr[16], gatewaystr[16];
677 if (family != AF_INET)
680 extract_route(msg, bytes, &index, &dst, &gateway);
682 inet_ntop(family, &dst, dststr, sizeof(dststr));
683 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
685 __connman_ipconfig_delroute(index, scope, dststr, gatewaystr);
687 /* skip host specific routes */
688 if (scope != RT_SCOPE_UNIVERSE &&
689 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
692 if (dst.s_addr != INADDR_ANY)
695 for (list = rtnl_list; list; list = list->next) {
696 struct connman_rtnl *rtnl = list->data;
698 if (rtnl->delgateway)
699 rtnl->delgateway(index, gatewaystr);
703 static inline void print_ether(struct rtattr *attr, const char *name)
705 int len = (int) RTA_PAYLOAD(attr);
707 if (len == ETH_ALEN) {
708 struct ether_addr eth;
709 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
710 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
712 print(" attr %s (len %d)\n", name, len);
715 static inline void print_inet(struct rtattr *attr, const char *name,
716 unsigned char family)
718 int len = (int) RTA_PAYLOAD(attr);
720 if (family == AF_INET && len == sizeof(struct in_addr)) {
722 addr = *((struct in_addr *) RTA_DATA(attr));
723 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
725 print(" attr %s (len %d)\n", name, len);
728 static inline void print_string(struct rtattr *attr, const char *name)
730 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
731 (char *) RTA_DATA(attr));
734 static inline void print_byte(struct rtattr *attr, const char *name)
736 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
737 *((unsigned char *) RTA_DATA(attr)));
740 static inline void print_integer(struct rtattr *attr, const char *name)
742 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
743 *((int *) RTA_DATA(attr)));
746 static inline void print_attr(struct rtattr *attr, const char *name)
748 int len = (int) RTA_PAYLOAD(attr);
751 print(" attr %s (len %d)\n", name, len);
753 print(" attr %d (len %d)\n", attr->rta_type, len);
756 static void rtnl_link(struct nlmsghdr *hdr)
758 struct ifinfomsg *msg;
762 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
763 bytes = IFLA_PAYLOAD(hdr);
765 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
767 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
768 attr = RTA_NEXT(attr, bytes)) {
769 switch (attr->rta_type) {
771 print_ether(attr, "address");
774 print_ether(attr, "broadcast");
777 print_string(attr, "ifname");
780 print_integer(attr, "mtu");
783 print_attr(attr, "link");
786 print_attr(attr, "qdisc");
789 print_attr(attr, "stats");
792 print_attr(attr, "cost");
795 print_attr(attr, "priority");
798 print_attr(attr, "master");
801 print_attr(attr, "wireless");
804 print_attr(attr, "protinfo");
807 print_integer(attr, "txqlen");
810 print_attr(attr, "map");
813 print_attr(attr, "weight");
816 print_byte(attr, "operstate");
819 print_byte(attr, "linkmode");
822 print_attr(attr, NULL);
828 static void rtnl_newlink(struct nlmsghdr *hdr)
830 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
834 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
835 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
838 static void rtnl_dellink(struct nlmsghdr *hdr)
840 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
844 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
845 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
848 static void rtnl_addr(struct nlmsghdr *hdr)
850 struct ifaddrmsg *msg;
854 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
855 bytes = IFA_PAYLOAD(hdr);
857 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
859 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
860 attr = RTA_NEXT(attr, bytes)) {
861 switch (attr->rta_type) {
863 print_inet(attr, "address", msg->ifa_family);
866 print_inet(attr, "local", msg->ifa_family);
869 print_string(attr, "label");
872 print_inet(attr, "broadcast", msg->ifa_family);
875 print_attr(attr, "anycast");
878 print_attr(attr, "cacheinfo");
881 print_attr(attr, "multicast");
884 print_attr(attr, NULL);
890 static void rtnl_newaddr(struct nlmsghdr *hdr)
892 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
896 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
897 msg, IFA_PAYLOAD(hdr));
900 static void rtnl_deladdr(struct nlmsghdr *hdr)
902 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
906 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
907 msg, IFA_PAYLOAD(hdr));
910 static void rtnl_route(struct nlmsghdr *hdr)
916 msg = (struct rtmsg *) NLMSG_DATA(hdr);
917 bytes = RTM_PAYLOAD(hdr);
919 print("rtm_family %d rtm_table %d rtm_protocol %d",
920 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
921 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
922 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
924 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
925 attr = RTA_NEXT(attr, bytes)) {
926 switch (attr->rta_type) {
928 print_inet(attr, "dst", msg->rtm_family);
931 print_inet(attr, "src", msg->rtm_family);
934 print_string(attr, "iif");
937 print_integer(attr, "oif");
940 print_inet(attr, "gateway", msg->rtm_family);
943 print_attr(attr, "priority");
946 print_inet(attr, "prefsrc", msg->rtm_family);
949 print_attr(attr, "metrics");
952 print_integer(attr, "table");
955 print_attr(attr, NULL);
961 static connman_bool_t is_route_rtmsg(struct rtmsg *msg)
964 if (msg->rtm_table != RT_TABLE_MAIN)
967 if (msg->rtm_protocol != RTPROT_BOOT &&
968 msg->rtm_protocol != RTPROT_KERNEL)
971 if (msg->rtm_type != RTN_UNICAST)
977 static void rtnl_newroute(struct nlmsghdr *hdr)
979 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
983 if (is_route_rtmsg(msg))
984 process_newroute(msg->rtm_family, msg->rtm_scope,
985 msg, RTM_PAYLOAD(hdr));
988 static void rtnl_delroute(struct nlmsghdr *hdr)
990 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
994 if (is_route_rtmsg(msg))
995 process_delroute(msg->rtm_family, msg->rtm_scope,
996 msg, RTM_PAYLOAD(hdr));
999 static const char *type2string(uint16_t type)
1031 static GIOChannel *channel = NULL;
1033 struct rtnl_request {
1034 struct nlmsghdr hdr;
1035 struct rtgenmsg msg;
1037 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1039 static GSList *request_list = NULL;
1040 static guint32 request_seq = 0;
1042 static struct rtnl_request *find_request(guint32 seq)
1046 for (list = request_list; list; list = list->next) {
1047 struct rtnl_request *req = list->data;
1049 if (req->hdr.nlmsg_seq == seq)
1056 static int send_request(struct rtnl_request *req)
1058 struct sockaddr_nl addr;
1061 DBG("%s len %d type %d flags 0x%04x seq %d",
1062 type2string(req->hdr.nlmsg_type),
1063 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1064 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1066 sk = g_io_channel_unix_get_fd(channel);
1068 memset(&addr, 0, sizeof(addr));
1069 addr.nl_family = AF_NETLINK;
1071 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1072 (struct sockaddr *) &addr, sizeof(addr));
1075 static int queue_request(struct rtnl_request *req)
1077 request_list = g_slist_append(request_list, req);
1079 if (g_slist_length(request_list) > 1)
1082 return send_request(req);
1085 static int process_response(guint32 seq)
1087 struct rtnl_request *req;
1091 req = find_request(seq);
1093 request_list = g_slist_remove(request_list, req);
1097 req = g_slist_nth_data(request_list, 0);
1101 return send_request(req);
1104 static void rtnl_message(void *buf, size_t len)
1106 DBG("buf %p len %zd", buf, len);
1109 struct nlmsghdr *hdr = buf;
1110 struct nlmsgerr *err;
1112 if (!NLMSG_OK(hdr, len))
1115 DBG("%s len %d type %d flags 0x%04x seq %d",
1116 type2string(hdr->nlmsg_type),
1117 hdr->nlmsg_len, hdr->nlmsg_type,
1118 hdr->nlmsg_flags, hdr->nlmsg_seq);
1120 switch (hdr->nlmsg_type) {
1125 process_response(hdr->nlmsg_seq);
1128 err = NLMSG_DATA(hdr);
1129 DBG("error %d (%s)", -err->error,
1130 strerror(-err->error));
1152 len -= hdr->nlmsg_len;
1153 buf += hdr->nlmsg_len;
1157 static gboolean netlink_event(GIOChannel *chan,
1158 GIOCondition cond, gpointer data)
1160 unsigned char buf[4096];
1164 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1167 memset(buf, 0, sizeof(buf));
1169 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
1171 if (err == G_IO_ERROR_AGAIN)
1176 rtnl_message(buf, len);
1181 static int send_getlink(void)
1183 struct rtnl_request *req;
1187 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1191 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1192 req->hdr.nlmsg_type = RTM_GETLINK;
1193 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1194 req->hdr.nlmsg_pid = 0;
1195 req->hdr.nlmsg_seq = request_seq++;
1196 req->msg.rtgen_family = AF_INET;
1198 return queue_request(req);
1201 static int send_getaddr(void)
1203 struct rtnl_request *req;
1207 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1211 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1212 req->hdr.nlmsg_type = RTM_GETADDR;
1213 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1214 req->hdr.nlmsg_pid = 0;
1215 req->hdr.nlmsg_seq = request_seq++;
1216 req->msg.rtgen_family = AF_INET;
1218 return queue_request(req);
1221 static int send_getroute(void)
1223 struct rtnl_request *req;
1227 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1231 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1232 req->hdr.nlmsg_type = RTM_GETROUTE;
1233 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1234 req->hdr.nlmsg_pid = 0;
1235 req->hdr.nlmsg_seq = request_seq++;
1236 req->msg.rtgen_family = AF_INET;
1238 return queue_request(req);
1241 static gboolean update_timeout_cb(gpointer user_data)
1243 __connman_rtnl_request_update();
1248 static void update_interval_callback(guint min)
1250 if (update_timeout > 0)
1251 g_source_remove(update_timeout);
1253 if (min < G_MAXUINT) {
1254 update_interval = min;
1255 update_timeout = g_timeout_add_seconds(update_interval,
1256 update_timeout_cb, NULL);
1259 update_interval = G_MAXUINT;
1263 static gint compare_interval(gconstpointer a, gconstpointer b)
1265 guint val_a = GPOINTER_TO_UINT(a);
1266 guint val_b = GPOINTER_TO_UINT(b);
1268 return val_a - val_b;
1271 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1278 update_list = g_slist_insert_sorted(update_list,
1279 GUINT_TO_POINTER(interval), compare_interval);
1281 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1282 if (min < update_interval) {
1283 update_interval_callback(min);
1284 __connman_rtnl_request_update();
1287 return update_interval;
1290 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1292 guint min = G_MAXUINT;
1297 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1299 if (update_list != NULL)
1300 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1302 if (min > update_interval)
1303 update_interval_callback(min);
1308 int __connman_rtnl_request_update(void)
1310 return send_getlink();
1313 int __connman_rtnl_init(void)
1315 struct sockaddr_nl addr;
1320 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1321 NULL, free_interface);
1323 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
1327 memset(&addr, 0, sizeof(addr));
1328 addr.nl_family = AF_NETLINK;
1329 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1330 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE;
1332 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1337 channel = g_io_channel_unix_new(sk);
1338 g_io_channel_set_close_on_unref(channel, TRUE);
1340 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1341 netlink_event, NULL);
1346 void __connman_rtnl_start(void)
1355 void __connman_rtnl_cleanup(void)
1361 for (list = watch_list; list; list = list->next) {
1362 struct watch_data *watch = list->data;
1364 DBG("removing watch %d", watch->id);
1370 g_slist_free(watch_list);
1373 g_slist_free(update_list);
1376 for (list = request_list; list; list = list->next) {
1377 struct rtnl_request *req = list->data;
1379 DBG("%s len %d type %d flags 0x%04x seq %d",
1380 type2string(req->hdr.nlmsg_type),
1381 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1382 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1388 g_slist_free(request_list);
1389 request_list = NULL;
1391 g_io_channel_shutdown(channel, TRUE, NULL);
1392 g_io_channel_unref(channel);
1396 g_hash_table_destroy(interface_list);