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 {
62 enum connman_service_type type;
65 static GHashTable *interface_list = NULL;
67 static void free_interface(gpointer data)
69 struct interface_data *interface = data;
71 __connman_technology_remove_interface(interface->type,
72 interface->index, interface->name);
74 g_free(interface->name);
78 static connman_bool_t ether_blacklisted(const char *name)
83 /* virtual interface from VMware */
84 if (g_str_has_prefix(name, "vmnet") == TRUE)
87 /* virtual interface from VirtualBox */
88 if (g_str_has_prefix(name, "vboxnet") == TRUE)
94 static void read_uevent(struct interface_data *interface)
96 char *filename, line[128];
99 filename = g_strdup_printf("/sys/class/net/%s/uevent",
102 f = fopen(filename, "re");
106 while (fgets(line, sizeof(line), f)) {
109 pos = strchr(line, '\n');
114 if (strncmp(line, "DEVTYPE=", 8) != 0)
117 if (strcmp(line + 8, "wlan") == 0)
118 interface->type = CONNMAN_SERVICE_TYPE_WIFI;
119 else if (strcmp(line + 8, "wwan") == 0)
120 interface->type = CONNMAN_SERVICE_TYPE_CELLULAR;
121 else if (strcmp(line + 8, "bluetooth") == 0)
122 interface->type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
123 else if (strcmp(line + 8, "wimax") == 0)
124 interface->type = CONNMAN_SERVICE_TYPE_WIMAX;
131 * connman_rtnl_add_operstate_watch:
132 * @index: network device index
133 * @callback: callback function
134 * @user_data: callback data;
136 * Add a new RTNL watch for operation state events
138 * Returns: %0 on failure and a unique id on success
140 unsigned int connman_rtnl_add_operstate_watch(int index,
141 connman_rtnl_operstate_cb_t callback, void *user_data)
143 struct watch_data *watch;
145 watch = g_try_new0(struct watch_data, 1);
149 watch->id = ++watch_id;
150 watch->index = index;
152 watch->operstate = callback;
153 watch->user_data = user_data;
155 watch_list = g_slist_prepend(watch_list, watch);
157 DBG("id %d", watch->id);
160 unsigned char operstate = 0;
163 callback(operstate, user_data);
170 * connman_rtnl_add_newlink_watch:
171 * @index: network device index
172 * @callback: callback function
173 * @user_data: callback data;
175 * Add a new RTNL watch for newlink events
177 * Returns: %0 on failure and a unique id on success
179 unsigned int connman_rtnl_add_newlink_watch(int index,
180 connman_rtnl_link_cb_t callback, void *user_data)
182 struct watch_data *watch;
184 watch = g_try_new0(struct watch_data, 1);
188 watch->id = ++watch_id;
189 watch->index = index;
191 watch->newlink = callback;
192 watch->user_data = user_data;
194 watch_list = g_slist_prepend(watch_list, watch);
196 DBG("id %d", watch->id);
199 unsigned int flags = __connman_ipconfig_get_flags(index);
202 callback(flags, 0, user_data);
209 * connman_rtnl_remove_watch:
210 * @id: watch identifier
212 * Remove the RTNL watch for the identifier
214 void connman_rtnl_remove_watch(unsigned int id)
223 for (list = watch_list; list; list = list->next) {
224 struct watch_data *watch = list->data;
226 if (watch->id == id) {
227 watch_list = g_slist_remove(watch_list, watch);
234 static void trigger_rtnl(int index, void *user_data)
236 struct connman_rtnl *rtnl = user_data;
239 unsigned short type = __connman_ipconfig_get_type(index);
240 unsigned int flags = __connman_ipconfig_get_flags(index);
242 rtnl->newlink(type, index, flags, 0);
245 if (rtnl->newgateway) {
246 const char *gateway = __connman_ipconfig_get_gateway(index);
249 rtnl->newgateway(index, gateway);
253 static GSList *rtnl_list = NULL;
255 static gint compare_priority(gconstpointer a, gconstpointer b)
257 const struct connman_rtnl *rtnl1 = a;
258 const struct connman_rtnl *rtnl2 = b;
260 return rtnl2->priority - rtnl1->priority;
264 * connman_rtnl_register:
267 * Register a new RTNL module
269 * Returns: %0 on success
271 int connman_rtnl_register(struct connman_rtnl *rtnl)
273 DBG("rtnl %p name %s", rtnl, rtnl->name);
275 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
278 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
284 * connman_rtnl_unregister:
287 * Remove a previously registered RTNL module
289 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
291 DBG("rtnl %p name %s", rtnl, rtnl->name);
293 rtnl_list = g_slist_remove(rtnl_list, rtnl);
296 static const char *operstate2str(unsigned char operstate)
299 case IF_OPER_UNKNOWN:
301 case IF_OPER_NOTPRESENT:
302 return "NOT-PRESENT";
305 case IF_OPER_LOWERLAYERDOWN:
306 return "LOWER-LAYER-DOWN";
307 case IF_OPER_TESTING:
309 case IF_OPER_DORMANT:
318 static void extract_link(struct ifinfomsg *msg, int bytes,
319 struct ether_addr *address, const char **ifname,
320 unsigned int *mtu, unsigned char *operstate,
321 struct rtnl_link_stats *stats)
325 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
326 attr = RTA_NEXT(attr, bytes)) {
327 switch (attr->rta_type) {
330 memcpy(address, RTA_DATA(attr), ETH_ALEN);
334 *ifname = RTA_DATA(attr);
338 *mtu = *((unsigned int *) RTA_DATA(attr));
342 memcpy(stats, RTA_DATA(attr),
343 sizeof(struct rtnl_link_stats));
346 if (operstate != NULL)
347 *operstate = *((unsigned char *) RTA_DATA(attr));
355 static void process_newlink(unsigned short type, int index, unsigned flags,
356 unsigned change, struct ifinfomsg *msg, int bytes)
358 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
359 struct ether_addr compare = {{ 0, 0, 0, 0, 0, 0 }};
360 struct rtnl_link_stats stats;
361 unsigned char operstate = 0xff;
362 const char *ifname = NULL;
363 unsigned int mtu = 0;
367 memset(&stats, 0, sizeof(stats));
368 extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats);
370 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
371 address.ether_addr_octet[0],
372 address.ether_addr_octet[1],
373 address.ether_addr_octet[2],
374 address.ether_addr_octet[3],
375 address.ether_addr_octet[4],
376 address.ether_addr_octet[5]);
380 case ARPHRD_LOOPBACK:
382 __connman_ipconfig_newlink(index, type, flags,
387 if (memcmp(&address, &compare, ETH_ALEN) != 0)
388 connman_info("%s {newlink} index %d address %s mtu %u",
389 ifname, index, str, mtu);
391 if (operstate != 0xff)
392 connman_info("%s {newlink} index %d operstate %u <%s>",
393 ifname, index, operstate,
394 operstate2str(operstate));
396 if (g_hash_table_lookup(interface_list,
397 GINT_TO_POINTER(index)) == NULL) {
398 struct interface_data *interface;
400 interface = g_new0(struct interface_data, 1);
401 interface->index = index;
402 interface->name = g_strdup(ifname);
404 g_hash_table_insert(interface_list,
405 GINT_TO_POINTER(index), interface);
407 read_uevent(interface);
409 if (interface->type == CONNMAN_SERVICE_TYPE_UNKNOWN &&
410 type == ARPHRD_ETHER &&
411 ether_blacklisted(ifname) == FALSE)
412 interface->type = CONNMAN_SERVICE_TYPE_ETHERNET;
414 __connman_technology_add_interface(interface->type,
415 interface->index, interface->name);
418 for (list = rtnl_list; list; list = list->next) {
419 struct connman_rtnl *rtnl = list->data;
422 rtnl->newlink(type, index, flags, change);
425 for (list = watch_list; list; list = list->next) {
426 struct watch_data *watch = list->data;
428 if (watch->index != index)
431 if (operstate != 0xff && watch->operstate)
432 watch->operstate(operstate, watch->user_data);
435 watch->newlink(flags, change, watch->user_data);
439 static void process_dellink(unsigned short type, int index, unsigned flags,
440 unsigned change, struct ifinfomsg *msg, int bytes)
442 struct rtnl_link_stats stats;
443 unsigned char operstate = 0xff;
444 const char *ifname = NULL;
447 memset(&stats, 0, sizeof(stats));
448 extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats);
450 if (operstate != 0xff)
451 connman_info("%s {dellink} index %d operstate %u <%s>",
452 ifname, index, operstate,
453 operstate2str(operstate));
455 for (list = watch_list; list; list = list->next) {
456 struct watch_data *watch = list->data;
458 if (watch->index != index)
461 if (operstate != 0xff && watch->operstate)
462 watch->operstate(operstate, watch->user_data);
465 for (list = rtnl_list; list; list = list->next) {
466 struct connman_rtnl *rtnl = list->data;
469 rtnl->dellink(type, index, flags, change);
474 case ARPHRD_LOOPBACK:
476 __connman_ipconfig_dellink(index, &stats);
480 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
483 static void extract_addr(struct ifaddrmsg *msg, int bytes,
485 struct in_addr *local,
486 struct in_addr *address,
487 struct in_addr *broadcast)
491 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
492 attr = RTA_NEXT(attr, bytes)) {
493 switch (attr->rta_type) {
496 *address = *((struct in_addr *) RTA_DATA(attr));
500 *local = *((struct in_addr *) RTA_DATA(attr));
503 if (broadcast != NULL)
504 *broadcast = *((struct in_addr *) RTA_DATA(attr));
508 *label = RTA_DATA(attr);
514 static void process_newaddr(unsigned char family, unsigned char prefixlen,
515 int index, struct ifaddrmsg *msg, int bytes)
517 struct in_addr address = { INADDR_ANY };
518 const char *label = NULL;
520 if (family != AF_INET)
523 extract_addr(msg, bytes, &label, &address, NULL, NULL);
525 __connman_ipconfig_newaddr(index, label,
526 prefixlen, inet_ntoa(address));
529 static void process_deladdr(unsigned char family, unsigned char prefixlen,
530 int index, struct ifaddrmsg *msg, int bytes)
532 struct in_addr address = { INADDR_ANY };
533 const char *label = NULL;
535 if (family != AF_INET)
538 extract_addr(msg, bytes, &label, &address, NULL, NULL);
540 __connman_ipconfig_deladdr(index, label,
541 prefixlen, inet_ntoa(address));
544 static void extract_route(struct rtmsg *msg, int bytes, int *index,
546 struct in_addr *gateway)
550 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
551 attr = RTA_NEXT(attr, bytes)) {
552 switch (attr->rta_type) {
555 *dst = *((struct in_addr *) RTA_DATA(attr));
559 *gateway = *((struct in_addr *) RTA_DATA(attr));
563 *index = *((int *) RTA_DATA(attr));
569 static void process_newroute(unsigned char family, unsigned char scope,
570 struct rtmsg *msg, int bytes)
573 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
574 char dststr[16], gatewaystr[16];
577 if (family != AF_INET)
580 extract_route(msg, bytes, &index, &dst, &gateway);
582 inet_ntop(family, &dst, dststr, sizeof(dststr));
583 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
585 __connman_ipconfig_newroute(index, scope, dststr, gatewaystr);
587 /* skip host specific routes */
588 if (scope != RT_SCOPE_UNIVERSE &&
589 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
592 if (dst.s_addr != INADDR_ANY)
595 for (list = rtnl_list; list; list = list->next) {
596 struct connman_rtnl *rtnl = list->data;
598 if (rtnl->newgateway)
599 rtnl->newgateway(index, gatewaystr);
603 static void process_delroute(unsigned char family, unsigned char scope,
604 struct rtmsg *msg, int bytes)
607 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
608 char dststr[16], gatewaystr[16];
611 if (family != AF_INET)
614 extract_route(msg, bytes, &index, &dst, &gateway);
616 inet_ntop(family, &dst, dststr, sizeof(dststr));
617 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
619 __connman_ipconfig_delroute(index, scope, dststr, gatewaystr);
621 /* skip host specific routes */
622 if (scope != RT_SCOPE_UNIVERSE &&
623 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
626 if (dst.s_addr != INADDR_ANY)
629 for (list = rtnl_list; list; list = list->next) {
630 struct connman_rtnl *rtnl = list->data;
632 if (rtnl->delgateway)
633 rtnl->delgateway(index, gatewaystr);
637 static inline void print_ether(struct rtattr *attr, const char *name)
639 int len = (int) RTA_PAYLOAD(attr);
641 if (len == ETH_ALEN) {
642 struct ether_addr eth;
643 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
644 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
646 print(" attr %s (len %d)\n", name, len);
649 static inline void print_inet(struct rtattr *attr, const char *name,
650 unsigned char family)
652 int len = (int) RTA_PAYLOAD(attr);
654 if (family == AF_INET && len == sizeof(struct in_addr)) {
656 addr = *((struct in_addr *) RTA_DATA(attr));
657 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
659 print(" attr %s (len %d)\n", name, len);
662 static inline void print_string(struct rtattr *attr, const char *name)
664 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
665 (char *) RTA_DATA(attr));
668 static inline void print_byte(struct rtattr *attr, const char *name)
670 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
671 *((unsigned char *) RTA_DATA(attr)));
674 static inline void print_integer(struct rtattr *attr, const char *name)
676 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
677 *((int *) RTA_DATA(attr)));
680 static inline void print_attr(struct rtattr *attr, const char *name)
682 int len = (int) RTA_PAYLOAD(attr);
685 print(" attr %s (len %d)\n", name, len);
687 print(" attr %d (len %d)\n", attr->rta_type, len);
690 static void rtnl_link(struct nlmsghdr *hdr)
692 struct ifinfomsg *msg;
696 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
697 bytes = IFLA_PAYLOAD(hdr);
699 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
701 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
702 attr = RTA_NEXT(attr, bytes)) {
703 switch (attr->rta_type) {
705 print_ether(attr, "address");
708 print_ether(attr, "broadcast");
711 print_string(attr, "ifname");
714 print_integer(attr, "mtu");
717 print_attr(attr, "link");
720 print_attr(attr, "qdisc");
723 print_attr(attr, "stats");
726 print_attr(attr, "cost");
729 print_attr(attr, "priority");
732 print_attr(attr, "master");
735 print_attr(attr, "wireless");
738 print_attr(attr, "protinfo");
741 print_integer(attr, "txqlen");
744 print_attr(attr, "map");
747 print_attr(attr, "weight");
750 print_byte(attr, "operstate");
753 print_byte(attr, "linkmode");
756 print_attr(attr, NULL);
762 static void rtnl_newlink(struct nlmsghdr *hdr)
764 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
768 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
769 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
772 static void rtnl_dellink(struct nlmsghdr *hdr)
774 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
778 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
779 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
782 static void rtnl_addr(struct nlmsghdr *hdr)
784 struct ifaddrmsg *msg;
788 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
789 bytes = IFA_PAYLOAD(hdr);
791 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
793 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
794 attr = RTA_NEXT(attr, bytes)) {
795 switch (attr->rta_type) {
797 print_inet(attr, "address", msg->ifa_family);
800 print_inet(attr, "local", msg->ifa_family);
803 print_string(attr, "label");
806 print_inet(attr, "broadcast", msg->ifa_family);
809 print_attr(attr, "anycast");
812 print_attr(attr, "cacheinfo");
815 print_attr(attr, "multicast");
818 print_attr(attr, NULL);
824 static void rtnl_newaddr(struct nlmsghdr *hdr)
826 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
830 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
831 msg, IFA_PAYLOAD(hdr));
834 static void rtnl_deladdr(struct nlmsghdr *hdr)
836 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
840 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
841 msg, IFA_PAYLOAD(hdr));
844 static void rtnl_route(struct nlmsghdr *hdr)
850 msg = (struct rtmsg *) NLMSG_DATA(hdr);
851 bytes = RTM_PAYLOAD(hdr);
853 print("rtm_family %d rtm_table %d rtm_protocol %d",
854 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
855 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
856 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
858 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
859 attr = RTA_NEXT(attr, bytes)) {
860 switch (attr->rta_type) {
862 print_inet(attr, "dst", msg->rtm_family);
865 print_inet(attr, "src", msg->rtm_family);
868 print_string(attr, "iif");
871 print_integer(attr, "oif");
874 print_inet(attr, "gateway", msg->rtm_family);
877 print_attr(attr, "priority");
880 print_inet(attr, "prefsrc", msg->rtm_family);
883 print_attr(attr, "metrics");
886 print_integer(attr, "table");
889 print_attr(attr, NULL);
895 static connman_bool_t is_route_rtmsg(struct rtmsg *msg)
898 if (msg->rtm_table != RT_TABLE_MAIN)
901 if (msg->rtm_protocol != RTPROT_BOOT &&
902 msg->rtm_protocol != RTPROT_KERNEL)
905 if (msg->rtm_type != RTN_UNICAST)
911 static void rtnl_newroute(struct nlmsghdr *hdr)
913 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
917 if (is_route_rtmsg(msg))
918 process_newroute(msg->rtm_family, msg->rtm_scope,
919 msg, RTM_PAYLOAD(hdr));
922 static void rtnl_delroute(struct nlmsghdr *hdr)
924 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
928 if (is_route_rtmsg(msg))
929 process_delroute(msg->rtm_family, msg->rtm_scope,
930 msg, RTM_PAYLOAD(hdr));
933 static const char *type2string(uint16_t type)
965 static GIOChannel *channel = NULL;
967 struct rtnl_request {
971 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
973 static GSList *request_list = NULL;
974 static guint32 request_seq = 0;
976 static struct rtnl_request *find_request(guint32 seq)
980 for (list = request_list; list; list = list->next) {
981 struct rtnl_request *req = list->data;
983 if (req->hdr.nlmsg_seq == seq)
990 static int send_request(struct rtnl_request *req)
992 struct sockaddr_nl addr;
995 DBG("%s len %d type %d flags 0x%04x seq %d",
996 type2string(req->hdr.nlmsg_type),
997 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
998 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1000 sk = g_io_channel_unix_get_fd(channel);
1002 memset(&addr, 0, sizeof(addr));
1003 addr.nl_family = AF_NETLINK;
1005 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1006 (struct sockaddr *) &addr, sizeof(addr));
1009 static int queue_request(struct rtnl_request *req)
1011 request_list = g_slist_append(request_list, req);
1013 if (g_slist_length(request_list) > 1)
1016 return send_request(req);
1019 static int process_response(guint32 seq)
1021 struct rtnl_request *req;
1025 req = find_request(seq);
1027 request_list = g_slist_remove(request_list, req);
1031 req = g_slist_nth_data(request_list, 0);
1035 return send_request(req);
1038 static void rtnl_message(void *buf, size_t len)
1040 DBG("buf %p len %zd", buf, len);
1043 struct nlmsghdr *hdr = buf;
1044 struct nlmsgerr *err;
1046 if (!NLMSG_OK(hdr, len))
1049 DBG("%s len %d type %d flags 0x%04x seq %d",
1050 type2string(hdr->nlmsg_type),
1051 hdr->nlmsg_len, hdr->nlmsg_type,
1052 hdr->nlmsg_flags, hdr->nlmsg_seq);
1054 switch (hdr->nlmsg_type) {
1059 process_response(hdr->nlmsg_seq);
1062 err = NLMSG_DATA(hdr);
1063 DBG("error %d (%s)", -err->error,
1064 strerror(-err->error));
1086 len -= hdr->nlmsg_len;
1087 buf += hdr->nlmsg_len;
1091 static gboolean netlink_event(GIOChannel *chan,
1092 GIOCondition cond, gpointer data)
1094 unsigned char buf[4096];
1098 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1101 memset(buf, 0, sizeof(buf));
1103 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
1105 if (err == G_IO_ERROR_AGAIN)
1110 rtnl_message(buf, len);
1115 static int send_getlink(void)
1117 struct rtnl_request *req;
1121 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1125 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1126 req->hdr.nlmsg_type = RTM_GETLINK;
1127 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1128 req->hdr.nlmsg_pid = 0;
1129 req->hdr.nlmsg_seq = request_seq++;
1130 req->msg.rtgen_family = AF_INET;
1132 return queue_request(req);
1135 static int send_getaddr(void)
1137 struct rtnl_request *req;
1141 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1145 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1146 req->hdr.nlmsg_type = RTM_GETADDR;
1147 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1148 req->hdr.nlmsg_pid = 0;
1149 req->hdr.nlmsg_seq = request_seq++;
1150 req->msg.rtgen_family = AF_INET;
1152 return queue_request(req);
1155 static int send_getroute(void)
1157 struct rtnl_request *req;
1161 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1165 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1166 req->hdr.nlmsg_type = RTM_GETROUTE;
1167 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1168 req->hdr.nlmsg_pid = 0;
1169 req->hdr.nlmsg_seq = request_seq++;
1170 req->msg.rtgen_family = AF_INET;
1172 return queue_request(req);
1175 static gboolean update_timeout_cb(gpointer user_data)
1177 __connman_rtnl_request_update();
1182 static void update_interval_callback(guint min)
1184 if (update_timeout > 0)
1185 g_source_remove(update_timeout);
1187 if (min < G_MAXUINT) {
1188 update_interval = min;
1189 update_timeout = g_timeout_add_seconds(update_interval,
1190 update_timeout_cb, NULL);
1193 update_interval = G_MAXUINT;
1197 static gint compare_interval(gconstpointer a, gconstpointer b)
1199 guint val_a = GPOINTER_TO_UINT(a);
1200 guint val_b = GPOINTER_TO_UINT(b);
1202 return val_a - val_b;
1205 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1212 update_list = g_slist_insert_sorted(update_list,
1213 GUINT_TO_POINTER(interval), compare_interval);
1215 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1216 if (min < update_interval) {
1217 update_interval_callback(min);
1218 __connman_rtnl_request_update();
1221 return update_interval;
1224 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1226 guint min = G_MAXUINT;
1231 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1233 if (update_list != NULL)
1234 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1236 if (min > update_interval)
1237 update_interval_callback(min);
1242 int __connman_rtnl_request_update(void)
1244 return send_getlink();
1247 int __connman_rtnl_init(void)
1249 struct sockaddr_nl addr;
1254 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1255 NULL, free_interface);
1257 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
1261 memset(&addr, 0, sizeof(addr));
1262 addr.nl_family = AF_NETLINK;
1263 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
1265 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1270 channel = g_io_channel_unix_new(sk);
1271 g_io_channel_set_close_on_unref(channel, TRUE);
1273 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1274 netlink_event, NULL);
1279 void __connman_rtnl_start(void)
1288 void __connman_rtnl_cleanup(void)
1294 for (list = watch_list; list; list = list->next) {
1295 struct watch_data *watch = list->data;
1297 DBG("removing watch %d", watch->id);
1303 g_slist_free(watch_list);
1306 g_slist_free(update_list);
1309 for (list = request_list; list; list = list->next) {
1310 struct rtnl_request *req = list->data;
1312 DBG("%s len %d type %d flags 0x%04x seq %d",
1313 type2string(req->hdr.nlmsg_type),
1314 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1315 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1321 g_slist_free(request_list);
1322 request_list = NULL;
1324 g_io_channel_shutdown(channel, TRUE, NULL);
1325 g_io_channel_unref(channel);
1329 g_hash_table_destroy(interface_list);