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 if (ether_blacklisted(interface->name) == TRUE)
100 interface->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
102 interface->type = CONNMAN_SERVICE_TYPE_ETHERNET;
104 filename = g_strdup_printf("/sys/class/net/%s/uevent",
107 f = fopen(filename, "re");
114 while (fgets(line, sizeof(line), f)) {
117 pos = strchr(line, '\n');
122 if (strncmp(line, "DEVTYPE=", 8) != 0)
125 if (strcmp(line + 8, "wlan") == 0)
126 interface->type = CONNMAN_SERVICE_TYPE_WIFI;
127 else if (strcmp(line + 8, "wwan") == 0)
128 interface->type = CONNMAN_SERVICE_TYPE_CELLULAR;
129 else if (strcmp(line + 8, "bluetooth") == 0)
130 interface->type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
131 else if (strcmp(line + 8, "wimax") == 0)
132 interface->type = CONNMAN_SERVICE_TYPE_WIMAX;
134 interface->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
141 * connman_rtnl_add_operstate_watch:
142 * @index: network device index
143 * @callback: callback function
144 * @user_data: callback data;
146 * Add a new RTNL watch for operation state events
148 * Returns: %0 on failure and a unique id on success
150 unsigned int connman_rtnl_add_operstate_watch(int index,
151 connman_rtnl_operstate_cb_t callback, void *user_data)
153 struct watch_data *watch;
155 watch = g_try_new0(struct watch_data, 1);
159 watch->id = ++watch_id;
160 watch->index = index;
162 watch->operstate = callback;
163 watch->user_data = user_data;
165 watch_list = g_slist_prepend(watch_list, watch);
167 DBG("id %d", watch->id);
170 unsigned char operstate = 0;
173 callback(operstate, user_data);
180 * connman_rtnl_add_newlink_watch:
181 * @index: network device index
182 * @callback: callback function
183 * @user_data: callback data;
185 * Add a new RTNL watch for newlink events
187 * Returns: %0 on failure and a unique id on success
189 unsigned int connman_rtnl_add_newlink_watch(int index,
190 connman_rtnl_link_cb_t callback, void *user_data)
192 struct watch_data *watch;
194 watch = g_try_new0(struct watch_data, 1);
198 watch->id = ++watch_id;
199 watch->index = index;
201 watch->newlink = callback;
202 watch->user_data = user_data;
204 watch_list = g_slist_prepend(watch_list, watch);
206 DBG("id %d", watch->id);
209 unsigned int flags = __connman_ipconfig_get_flags(index);
212 callback(flags, 0, user_data);
219 * connman_rtnl_remove_watch:
220 * @id: watch identifier
222 * Remove the RTNL watch for the identifier
224 void connman_rtnl_remove_watch(unsigned int id)
233 for (list = watch_list; list; list = list->next) {
234 struct watch_data *watch = list->data;
236 if (watch->id == id) {
237 watch_list = g_slist_remove(watch_list, watch);
244 static void trigger_rtnl(int index, void *user_data)
246 struct connman_rtnl *rtnl = user_data;
249 unsigned short type = __connman_ipconfig_get_type(index);
250 unsigned int flags = __connman_ipconfig_get_flags(index);
252 rtnl->newlink(type, index, flags, 0);
255 if (rtnl->newgateway) {
256 const char *gateway = __connman_ipconfig_get_gateway(index);
259 rtnl->newgateway(index, gateway);
263 static GSList *rtnl_list = NULL;
265 static gint compare_priority(gconstpointer a, gconstpointer b)
267 const struct connman_rtnl *rtnl1 = a;
268 const struct connman_rtnl *rtnl2 = b;
270 return rtnl2->priority - rtnl1->priority;
274 * connman_rtnl_register:
277 * Register a new RTNL module
279 * Returns: %0 on success
281 int connman_rtnl_register(struct connman_rtnl *rtnl)
283 DBG("rtnl %p name %s", rtnl, rtnl->name);
285 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
288 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
294 * connman_rtnl_unregister:
297 * Remove a previously registered RTNL module
299 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
301 DBG("rtnl %p name %s", rtnl, rtnl->name);
303 rtnl_list = g_slist_remove(rtnl_list, rtnl);
306 static const char *operstate2str(unsigned char operstate)
309 case IF_OPER_UNKNOWN:
311 case IF_OPER_NOTPRESENT:
312 return "NOT-PRESENT";
315 case IF_OPER_LOWERLAYERDOWN:
316 return "LOWER-LAYER-DOWN";
317 case IF_OPER_TESTING:
319 case IF_OPER_DORMANT:
328 static void extract_link(struct ifinfomsg *msg, int bytes,
329 struct ether_addr *address, const char **ifname,
330 unsigned int *mtu, unsigned char *operstate,
331 struct rtnl_link_stats *stats)
335 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
336 attr = RTA_NEXT(attr, bytes)) {
337 switch (attr->rta_type) {
340 memcpy(address, RTA_DATA(attr), ETH_ALEN);
344 *ifname = RTA_DATA(attr);
348 *mtu = *((unsigned int *) RTA_DATA(attr));
352 memcpy(stats, RTA_DATA(attr),
353 sizeof(struct rtnl_link_stats));
356 if (operstate != NULL)
357 *operstate = *((unsigned char *) RTA_DATA(attr));
365 static void process_newlink(unsigned short type, int index, unsigned flags,
366 unsigned change, struct ifinfomsg *msg, int bytes)
368 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
369 struct ether_addr compare = {{ 0, 0, 0, 0, 0, 0 }};
370 struct rtnl_link_stats stats;
371 unsigned char operstate = 0xff;
372 const char *ifname = NULL;
373 unsigned int mtu = 0;
377 memset(&stats, 0, sizeof(stats));
378 extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats);
380 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
381 address.ether_addr_octet[0],
382 address.ether_addr_octet[1],
383 address.ether_addr_octet[2],
384 address.ether_addr_octet[3],
385 address.ether_addr_octet[4],
386 address.ether_addr_octet[5]);
390 case ARPHRD_LOOPBACK:
392 __connman_ipconfig_newlink(index, type, flags,
397 if (memcmp(&address, &compare, ETH_ALEN) != 0)
398 connman_info("%s {newlink} index %d address %s mtu %u",
399 ifname, index, str, mtu);
401 if (operstate != 0xff)
402 connman_info("%s {newlink} index %d operstate %u <%s>",
403 ifname, index, operstate,
404 operstate2str(operstate));
406 if (g_hash_table_lookup(interface_list,
407 GINT_TO_POINTER(index)) == NULL) {
408 struct interface_data *interface;
410 interface = g_new0(struct interface_data, 1);
411 interface->index = index;
412 interface->name = g_strdup(ifname);
414 g_hash_table_insert(interface_list,
415 GINT_TO_POINTER(index), interface);
417 if (type == ARPHRD_ETHER)
418 read_uevent(interface);
420 __connman_technology_add_interface(interface->type,
421 interface->index, interface->name);
424 for (list = rtnl_list; list; list = list->next) {
425 struct connman_rtnl *rtnl = list->data;
428 rtnl->newlink(type, index, flags, change);
431 for (list = watch_list; list; list = list->next) {
432 struct watch_data *watch = list->data;
434 if (watch->index != index)
437 if (operstate != 0xff && watch->operstate)
438 watch->operstate(operstate, watch->user_data);
441 watch->newlink(flags, change, watch->user_data);
445 static void process_dellink(unsigned short type, int index, unsigned flags,
446 unsigned change, struct ifinfomsg *msg, int bytes)
448 struct rtnl_link_stats stats;
449 unsigned char operstate = 0xff;
450 const char *ifname = NULL;
453 memset(&stats, 0, sizeof(stats));
454 extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats);
456 if (operstate != 0xff)
457 connman_info("%s {dellink} index %d operstate %u <%s>",
458 ifname, index, operstate,
459 operstate2str(operstate));
461 for (list = watch_list; list; list = list->next) {
462 struct watch_data *watch = list->data;
464 if (watch->index != index)
467 if (operstate != 0xff && watch->operstate)
468 watch->operstate(operstate, watch->user_data);
471 for (list = rtnl_list; list; list = list->next) {
472 struct connman_rtnl *rtnl = list->data;
475 rtnl->dellink(type, index, flags, change);
480 case ARPHRD_LOOPBACK:
482 __connman_ipconfig_dellink(index, &stats);
486 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
489 static void extract_addr(struct ifaddrmsg *msg, int bytes,
491 struct in_addr *local,
492 struct in_addr *address,
493 struct in_addr *broadcast)
497 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
498 attr = RTA_NEXT(attr, bytes)) {
499 switch (attr->rta_type) {
502 *address = *((struct in_addr *) RTA_DATA(attr));
506 *local = *((struct in_addr *) RTA_DATA(attr));
509 if (broadcast != NULL)
510 *broadcast = *((struct in_addr *) RTA_DATA(attr));
514 *label = RTA_DATA(attr);
520 static void process_newaddr(unsigned char family, unsigned char prefixlen,
521 int index, struct ifaddrmsg *msg, int bytes)
523 struct in_addr address = { INADDR_ANY };
524 const char *label = NULL;
526 if (family != AF_INET)
529 extract_addr(msg, bytes, &label, &address, NULL, NULL);
531 __connman_ipconfig_newaddr(index, label,
532 prefixlen, inet_ntoa(address));
535 static void process_deladdr(unsigned char family, unsigned char prefixlen,
536 int index, struct ifaddrmsg *msg, int bytes)
538 struct in_addr address = { INADDR_ANY };
539 const char *label = NULL;
541 if (family != AF_INET)
544 extract_addr(msg, bytes, &label, &address, NULL, NULL);
546 __connman_ipconfig_deladdr(index, label,
547 prefixlen, inet_ntoa(address));
550 static void extract_route(struct rtmsg *msg, int bytes, int *index,
552 struct in_addr *gateway)
556 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
557 attr = RTA_NEXT(attr, bytes)) {
558 switch (attr->rta_type) {
561 *dst = *((struct in_addr *) RTA_DATA(attr));
565 *gateway = *((struct in_addr *) RTA_DATA(attr));
569 *index = *((int *) RTA_DATA(attr));
575 static void process_newroute(unsigned char family, unsigned char scope,
576 struct rtmsg *msg, int bytes)
579 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
580 char dststr[16], gatewaystr[16];
583 if (family != AF_INET)
586 extract_route(msg, bytes, &index, &dst, &gateway);
588 inet_ntop(family, &dst, dststr, sizeof(dststr));
589 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
591 __connman_ipconfig_newroute(index, scope, dststr, gatewaystr);
593 /* skip host specific routes */
594 if (scope != RT_SCOPE_UNIVERSE &&
595 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
598 if (dst.s_addr != INADDR_ANY)
601 for (list = rtnl_list; list; list = list->next) {
602 struct connman_rtnl *rtnl = list->data;
604 if (rtnl->newgateway)
605 rtnl->newgateway(index, gatewaystr);
609 static void process_delroute(unsigned char family, unsigned char scope,
610 struct rtmsg *msg, int bytes)
613 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
614 char dststr[16], gatewaystr[16];
617 if (family != AF_INET)
620 extract_route(msg, bytes, &index, &dst, &gateway);
622 inet_ntop(family, &dst, dststr, sizeof(dststr));
623 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
625 __connman_ipconfig_delroute(index, scope, dststr, gatewaystr);
627 /* skip host specific routes */
628 if (scope != RT_SCOPE_UNIVERSE &&
629 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
632 if (dst.s_addr != INADDR_ANY)
635 for (list = rtnl_list; list; list = list->next) {
636 struct connman_rtnl *rtnl = list->data;
638 if (rtnl->delgateway)
639 rtnl->delgateway(index, gatewaystr);
643 static inline void print_ether(struct rtattr *attr, const char *name)
645 int len = (int) RTA_PAYLOAD(attr);
647 if (len == ETH_ALEN) {
648 struct ether_addr eth;
649 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
650 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
652 print(" attr %s (len %d)\n", name, len);
655 static inline void print_inet(struct rtattr *attr, const char *name,
656 unsigned char family)
658 int len = (int) RTA_PAYLOAD(attr);
660 if (family == AF_INET && len == sizeof(struct in_addr)) {
662 addr = *((struct in_addr *) RTA_DATA(attr));
663 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
665 print(" attr %s (len %d)\n", name, len);
668 static inline void print_string(struct rtattr *attr, const char *name)
670 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
671 (char *) RTA_DATA(attr));
674 static inline void print_byte(struct rtattr *attr, const char *name)
676 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
677 *((unsigned char *) RTA_DATA(attr)));
680 static inline void print_integer(struct rtattr *attr, const char *name)
682 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
683 *((int *) RTA_DATA(attr)));
686 static inline void print_attr(struct rtattr *attr, const char *name)
688 int len = (int) RTA_PAYLOAD(attr);
691 print(" attr %s (len %d)\n", name, len);
693 print(" attr %d (len %d)\n", attr->rta_type, len);
696 static void rtnl_link(struct nlmsghdr *hdr)
698 struct ifinfomsg *msg;
702 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
703 bytes = IFLA_PAYLOAD(hdr);
705 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
707 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
708 attr = RTA_NEXT(attr, bytes)) {
709 switch (attr->rta_type) {
711 print_ether(attr, "address");
714 print_ether(attr, "broadcast");
717 print_string(attr, "ifname");
720 print_integer(attr, "mtu");
723 print_attr(attr, "link");
726 print_attr(attr, "qdisc");
729 print_attr(attr, "stats");
732 print_attr(attr, "cost");
735 print_attr(attr, "priority");
738 print_attr(attr, "master");
741 print_attr(attr, "wireless");
744 print_attr(attr, "protinfo");
747 print_integer(attr, "txqlen");
750 print_attr(attr, "map");
753 print_attr(attr, "weight");
756 print_byte(attr, "operstate");
759 print_byte(attr, "linkmode");
762 print_attr(attr, NULL);
768 static void rtnl_newlink(struct nlmsghdr *hdr)
770 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
774 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
775 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
778 static void rtnl_dellink(struct nlmsghdr *hdr)
780 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
784 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
785 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
788 static void rtnl_addr(struct nlmsghdr *hdr)
790 struct ifaddrmsg *msg;
794 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
795 bytes = IFA_PAYLOAD(hdr);
797 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
799 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
800 attr = RTA_NEXT(attr, bytes)) {
801 switch (attr->rta_type) {
803 print_inet(attr, "address", msg->ifa_family);
806 print_inet(attr, "local", msg->ifa_family);
809 print_string(attr, "label");
812 print_inet(attr, "broadcast", msg->ifa_family);
815 print_attr(attr, "anycast");
818 print_attr(attr, "cacheinfo");
821 print_attr(attr, "multicast");
824 print_attr(attr, NULL);
830 static void rtnl_newaddr(struct nlmsghdr *hdr)
832 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
836 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
837 msg, IFA_PAYLOAD(hdr));
840 static void rtnl_deladdr(struct nlmsghdr *hdr)
842 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
846 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
847 msg, IFA_PAYLOAD(hdr));
850 static void rtnl_route(struct nlmsghdr *hdr)
856 msg = (struct rtmsg *) NLMSG_DATA(hdr);
857 bytes = RTM_PAYLOAD(hdr);
859 print("rtm_family %d rtm_table %d rtm_protocol %d",
860 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
861 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
862 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
864 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
865 attr = RTA_NEXT(attr, bytes)) {
866 switch (attr->rta_type) {
868 print_inet(attr, "dst", msg->rtm_family);
871 print_inet(attr, "src", msg->rtm_family);
874 print_string(attr, "iif");
877 print_integer(attr, "oif");
880 print_inet(attr, "gateway", msg->rtm_family);
883 print_attr(attr, "priority");
886 print_inet(attr, "prefsrc", msg->rtm_family);
889 print_attr(attr, "metrics");
892 print_integer(attr, "table");
895 print_attr(attr, NULL);
901 static connman_bool_t is_route_rtmsg(struct rtmsg *msg)
904 if (msg->rtm_table != RT_TABLE_MAIN)
907 if (msg->rtm_protocol != RTPROT_BOOT &&
908 msg->rtm_protocol != RTPROT_KERNEL)
911 if (msg->rtm_type != RTN_UNICAST)
917 static void rtnl_newroute(struct nlmsghdr *hdr)
919 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
923 if (is_route_rtmsg(msg))
924 process_newroute(msg->rtm_family, msg->rtm_scope,
925 msg, RTM_PAYLOAD(hdr));
928 static void rtnl_delroute(struct nlmsghdr *hdr)
930 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
934 if (is_route_rtmsg(msg))
935 process_delroute(msg->rtm_family, msg->rtm_scope,
936 msg, RTM_PAYLOAD(hdr));
939 static const char *type2string(uint16_t type)
971 static GIOChannel *channel = NULL;
973 struct rtnl_request {
977 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
979 static GSList *request_list = NULL;
980 static guint32 request_seq = 0;
982 static struct rtnl_request *find_request(guint32 seq)
986 for (list = request_list; list; list = list->next) {
987 struct rtnl_request *req = list->data;
989 if (req->hdr.nlmsg_seq == seq)
996 static int send_request(struct rtnl_request *req)
998 struct sockaddr_nl addr;
1001 DBG("%s len %d type %d flags 0x%04x seq %d",
1002 type2string(req->hdr.nlmsg_type),
1003 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1004 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1006 sk = g_io_channel_unix_get_fd(channel);
1008 memset(&addr, 0, sizeof(addr));
1009 addr.nl_family = AF_NETLINK;
1011 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1012 (struct sockaddr *) &addr, sizeof(addr));
1015 static int queue_request(struct rtnl_request *req)
1017 request_list = g_slist_append(request_list, req);
1019 if (g_slist_length(request_list) > 1)
1022 return send_request(req);
1025 static int process_response(guint32 seq)
1027 struct rtnl_request *req;
1031 req = find_request(seq);
1033 request_list = g_slist_remove(request_list, req);
1037 req = g_slist_nth_data(request_list, 0);
1041 return send_request(req);
1044 static void rtnl_message(void *buf, size_t len)
1046 DBG("buf %p len %zd", buf, len);
1049 struct nlmsghdr *hdr = buf;
1050 struct nlmsgerr *err;
1052 if (!NLMSG_OK(hdr, len))
1055 DBG("%s len %d type %d flags 0x%04x seq %d",
1056 type2string(hdr->nlmsg_type),
1057 hdr->nlmsg_len, hdr->nlmsg_type,
1058 hdr->nlmsg_flags, hdr->nlmsg_seq);
1060 switch (hdr->nlmsg_type) {
1065 process_response(hdr->nlmsg_seq);
1068 err = NLMSG_DATA(hdr);
1069 DBG("error %d (%s)", -err->error,
1070 strerror(-err->error));
1092 len -= hdr->nlmsg_len;
1093 buf += hdr->nlmsg_len;
1097 static gboolean netlink_event(GIOChannel *chan,
1098 GIOCondition cond, gpointer data)
1100 unsigned char buf[4096];
1104 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1107 memset(buf, 0, sizeof(buf));
1109 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
1111 if (err == G_IO_ERROR_AGAIN)
1116 rtnl_message(buf, len);
1121 static int send_getlink(void)
1123 struct rtnl_request *req;
1127 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1131 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1132 req->hdr.nlmsg_type = RTM_GETLINK;
1133 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1134 req->hdr.nlmsg_pid = 0;
1135 req->hdr.nlmsg_seq = request_seq++;
1136 req->msg.rtgen_family = AF_INET;
1138 return queue_request(req);
1141 static int send_getaddr(void)
1143 struct rtnl_request *req;
1147 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1151 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1152 req->hdr.nlmsg_type = RTM_GETADDR;
1153 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1154 req->hdr.nlmsg_pid = 0;
1155 req->hdr.nlmsg_seq = request_seq++;
1156 req->msg.rtgen_family = AF_INET;
1158 return queue_request(req);
1161 static int send_getroute(void)
1163 struct rtnl_request *req;
1167 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1171 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1172 req->hdr.nlmsg_type = RTM_GETROUTE;
1173 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1174 req->hdr.nlmsg_pid = 0;
1175 req->hdr.nlmsg_seq = request_seq++;
1176 req->msg.rtgen_family = AF_INET;
1178 return queue_request(req);
1181 static gboolean update_timeout_cb(gpointer user_data)
1183 __connman_rtnl_request_update();
1188 static void update_interval_callback(guint min)
1190 if (update_timeout > 0)
1191 g_source_remove(update_timeout);
1193 if (min < G_MAXUINT) {
1194 update_interval = min;
1195 update_timeout = g_timeout_add_seconds(update_interval,
1196 update_timeout_cb, NULL);
1199 update_interval = G_MAXUINT;
1203 static gint compare_interval(gconstpointer a, gconstpointer b)
1205 guint val_a = GPOINTER_TO_UINT(a);
1206 guint val_b = GPOINTER_TO_UINT(b);
1208 return val_a - val_b;
1211 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1218 update_list = g_slist_insert_sorted(update_list,
1219 GUINT_TO_POINTER(interval), compare_interval);
1221 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1222 if (min < update_interval) {
1223 update_interval_callback(min);
1224 __connman_rtnl_request_update();
1227 return update_interval;
1230 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1232 guint min = G_MAXUINT;
1237 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1239 if (update_list != NULL)
1240 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1242 if (min > update_interval)
1243 update_interval_callback(min);
1248 int __connman_rtnl_request_update(void)
1250 return send_getlink();
1253 int __connman_rtnl_init(void)
1255 struct sockaddr_nl addr;
1260 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1261 NULL, free_interface);
1263 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
1267 memset(&addr, 0, sizeof(addr));
1268 addr.nl_family = AF_NETLINK;
1269 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
1271 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1276 channel = g_io_channel_unix_new(sk);
1277 g_io_channel_set_close_on_unref(channel, TRUE);
1279 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1280 netlink_event, NULL);
1285 void __connman_rtnl_start(void)
1294 void __connman_rtnl_cleanup(void)
1300 for (list = watch_list; list; list = list->next) {
1301 struct watch_data *watch = list->data;
1303 DBG("removing watch %d", watch->id);
1309 g_slist_free(watch_list);
1312 g_slist_free(update_list);
1315 for (list = request_list; list; list = list->next) {
1316 struct rtnl_request *req = list->data;
1318 DBG("%s len %d type %d flags 0x%04x seq %d",
1319 type2string(req->hdr.nlmsg_type),
1320 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1321 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1327 g_slist_free(request_list);
1328 request_list = NULL;
1330 g_io_channel_shutdown(channel, TRUE, NULL);
1331 g_io_channel_unref(channel);
1335 g_hash_table_destroy(interface_list);