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_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 process_newaddr(unsigned char family, unsigned char prefixlen,
532 int index, struct ifaddrmsg *msg, int bytes)
534 struct in_addr address = { INADDR_ANY };
535 const char *label = NULL;
537 if (family != AF_INET)
540 extract_addr(msg, bytes, &label, &address, NULL, NULL);
542 __connman_ipconfig_newaddr(index, label,
543 prefixlen, inet_ntoa(address));
546 static void process_deladdr(unsigned char family, unsigned char prefixlen,
547 int index, struct ifaddrmsg *msg, int bytes)
549 struct in_addr address = { INADDR_ANY };
550 const char *label = NULL;
552 if (family != AF_INET)
555 extract_addr(msg, bytes, &label, &address, NULL, NULL);
557 __connman_ipconfig_deladdr(index, label,
558 prefixlen, inet_ntoa(address));
561 static void extract_route(struct rtmsg *msg, int bytes, int *index,
563 struct in_addr *gateway)
567 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
568 attr = RTA_NEXT(attr, bytes)) {
569 switch (attr->rta_type) {
572 *dst = *((struct in_addr *) RTA_DATA(attr));
576 *gateway = *((struct in_addr *) RTA_DATA(attr));
580 *index = *((int *) RTA_DATA(attr));
586 static void process_newroute(unsigned char family, unsigned char scope,
587 struct rtmsg *msg, int bytes)
590 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
591 char dststr[16], gatewaystr[16];
594 if (family != AF_INET)
597 extract_route(msg, bytes, &index, &dst, &gateway);
599 inet_ntop(family, &dst, dststr, sizeof(dststr));
600 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
602 __connman_ipconfig_newroute(index, scope, dststr, gatewaystr);
604 /* skip host specific routes */
605 if (scope != RT_SCOPE_UNIVERSE &&
606 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
609 if (dst.s_addr != INADDR_ANY)
612 for (list = rtnl_list; list; list = list->next) {
613 struct connman_rtnl *rtnl = list->data;
615 if (rtnl->newgateway)
616 rtnl->newgateway(index, gatewaystr);
620 static void process_delroute(unsigned char family, unsigned char scope,
621 struct rtmsg *msg, int bytes)
624 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
625 char dststr[16], gatewaystr[16];
628 if (family != AF_INET)
631 extract_route(msg, bytes, &index, &dst, &gateway);
633 inet_ntop(family, &dst, dststr, sizeof(dststr));
634 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
636 __connman_ipconfig_delroute(index, scope, dststr, gatewaystr);
638 /* skip host specific routes */
639 if (scope != RT_SCOPE_UNIVERSE &&
640 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
643 if (dst.s_addr != INADDR_ANY)
646 for (list = rtnl_list; list; list = list->next) {
647 struct connman_rtnl *rtnl = list->data;
649 if (rtnl->delgateway)
650 rtnl->delgateway(index, gatewaystr);
654 static inline void print_ether(struct rtattr *attr, const char *name)
656 int len = (int) RTA_PAYLOAD(attr);
658 if (len == ETH_ALEN) {
659 struct ether_addr eth;
660 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
661 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
663 print(" attr %s (len %d)\n", name, len);
666 static inline void print_inet(struct rtattr *attr, const char *name,
667 unsigned char family)
669 int len = (int) RTA_PAYLOAD(attr);
671 if (family == AF_INET && len == sizeof(struct in_addr)) {
673 addr = *((struct in_addr *) RTA_DATA(attr));
674 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
676 print(" attr %s (len %d)\n", name, len);
679 static inline void print_string(struct rtattr *attr, const char *name)
681 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
682 (char *) RTA_DATA(attr));
685 static inline void print_byte(struct rtattr *attr, const char *name)
687 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
688 *((unsigned char *) RTA_DATA(attr)));
691 static inline void print_integer(struct rtattr *attr, const char *name)
693 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
694 *((int *) RTA_DATA(attr)));
697 static inline void print_attr(struct rtattr *attr, const char *name)
699 int len = (int) RTA_PAYLOAD(attr);
702 print(" attr %s (len %d)\n", name, len);
704 print(" attr %d (len %d)\n", attr->rta_type, len);
707 static void rtnl_link(struct nlmsghdr *hdr)
709 struct ifinfomsg *msg;
713 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
714 bytes = IFLA_PAYLOAD(hdr);
716 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
718 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
719 attr = RTA_NEXT(attr, bytes)) {
720 switch (attr->rta_type) {
722 print_ether(attr, "address");
725 print_ether(attr, "broadcast");
728 print_string(attr, "ifname");
731 print_integer(attr, "mtu");
734 print_attr(attr, "link");
737 print_attr(attr, "qdisc");
740 print_attr(attr, "stats");
743 print_attr(attr, "cost");
746 print_attr(attr, "priority");
749 print_attr(attr, "master");
752 print_attr(attr, "wireless");
755 print_attr(attr, "protinfo");
758 print_integer(attr, "txqlen");
761 print_attr(attr, "map");
764 print_attr(attr, "weight");
767 print_byte(attr, "operstate");
770 print_byte(attr, "linkmode");
773 print_attr(attr, NULL);
779 static void rtnl_newlink(struct nlmsghdr *hdr)
781 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
785 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
786 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
789 static void rtnl_dellink(struct nlmsghdr *hdr)
791 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
795 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
796 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
799 static void rtnl_addr(struct nlmsghdr *hdr)
801 struct ifaddrmsg *msg;
805 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
806 bytes = IFA_PAYLOAD(hdr);
808 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
810 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
811 attr = RTA_NEXT(attr, bytes)) {
812 switch (attr->rta_type) {
814 print_inet(attr, "address", msg->ifa_family);
817 print_inet(attr, "local", msg->ifa_family);
820 print_string(attr, "label");
823 print_inet(attr, "broadcast", msg->ifa_family);
826 print_attr(attr, "anycast");
829 print_attr(attr, "cacheinfo");
832 print_attr(attr, "multicast");
835 print_attr(attr, NULL);
841 static void rtnl_newaddr(struct nlmsghdr *hdr)
843 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
847 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
848 msg, IFA_PAYLOAD(hdr));
851 static void rtnl_deladdr(struct nlmsghdr *hdr)
853 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
857 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
858 msg, IFA_PAYLOAD(hdr));
861 static void rtnl_route(struct nlmsghdr *hdr)
867 msg = (struct rtmsg *) NLMSG_DATA(hdr);
868 bytes = RTM_PAYLOAD(hdr);
870 print("rtm_family %d rtm_table %d rtm_protocol %d",
871 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
872 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
873 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
875 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
876 attr = RTA_NEXT(attr, bytes)) {
877 switch (attr->rta_type) {
879 print_inet(attr, "dst", msg->rtm_family);
882 print_inet(attr, "src", msg->rtm_family);
885 print_string(attr, "iif");
888 print_integer(attr, "oif");
891 print_inet(attr, "gateway", msg->rtm_family);
894 print_attr(attr, "priority");
897 print_inet(attr, "prefsrc", msg->rtm_family);
900 print_attr(attr, "metrics");
903 print_integer(attr, "table");
906 print_attr(attr, NULL);
912 static connman_bool_t is_route_rtmsg(struct rtmsg *msg)
915 if (msg->rtm_table != RT_TABLE_MAIN)
918 if (msg->rtm_protocol != RTPROT_BOOT &&
919 msg->rtm_protocol != RTPROT_KERNEL)
922 if (msg->rtm_type != RTN_UNICAST)
928 static void rtnl_newroute(struct nlmsghdr *hdr)
930 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
934 if (is_route_rtmsg(msg))
935 process_newroute(msg->rtm_family, msg->rtm_scope,
936 msg, RTM_PAYLOAD(hdr));
939 static void rtnl_delroute(struct nlmsghdr *hdr)
941 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
945 if (is_route_rtmsg(msg))
946 process_delroute(msg->rtm_family, msg->rtm_scope,
947 msg, RTM_PAYLOAD(hdr));
950 static const char *type2string(uint16_t type)
982 static GIOChannel *channel = NULL;
984 struct rtnl_request {
988 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
990 static GSList *request_list = NULL;
991 static guint32 request_seq = 0;
993 static struct rtnl_request *find_request(guint32 seq)
997 for (list = request_list; list; list = list->next) {
998 struct rtnl_request *req = list->data;
1000 if (req->hdr.nlmsg_seq == seq)
1007 static int send_request(struct rtnl_request *req)
1009 struct sockaddr_nl addr;
1012 DBG("%s len %d type %d flags 0x%04x seq %d",
1013 type2string(req->hdr.nlmsg_type),
1014 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1015 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1017 sk = g_io_channel_unix_get_fd(channel);
1019 memset(&addr, 0, sizeof(addr));
1020 addr.nl_family = AF_NETLINK;
1022 return sendto(sk, req, req->hdr.nlmsg_len, 0,
1023 (struct sockaddr *) &addr, sizeof(addr));
1026 static int queue_request(struct rtnl_request *req)
1028 request_list = g_slist_append(request_list, req);
1030 if (g_slist_length(request_list) > 1)
1033 return send_request(req);
1036 static int process_response(guint32 seq)
1038 struct rtnl_request *req;
1042 req = find_request(seq);
1044 request_list = g_slist_remove(request_list, req);
1048 req = g_slist_nth_data(request_list, 0);
1052 return send_request(req);
1055 static void rtnl_message(void *buf, size_t len)
1057 DBG("buf %p len %zd", buf, len);
1060 struct nlmsghdr *hdr = buf;
1061 struct nlmsgerr *err;
1063 if (!NLMSG_OK(hdr, len))
1066 DBG("%s len %d type %d flags 0x%04x seq %d",
1067 type2string(hdr->nlmsg_type),
1068 hdr->nlmsg_len, hdr->nlmsg_type,
1069 hdr->nlmsg_flags, hdr->nlmsg_seq);
1071 switch (hdr->nlmsg_type) {
1076 process_response(hdr->nlmsg_seq);
1079 err = NLMSG_DATA(hdr);
1080 DBG("error %d (%s)", -err->error,
1081 strerror(-err->error));
1103 len -= hdr->nlmsg_len;
1104 buf += hdr->nlmsg_len;
1108 static gboolean netlink_event(GIOChannel *chan,
1109 GIOCondition cond, gpointer data)
1111 unsigned char buf[4096];
1115 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1118 memset(buf, 0, sizeof(buf));
1120 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
1122 if (err == G_IO_ERROR_AGAIN)
1127 rtnl_message(buf, len);
1132 static int send_getlink(void)
1134 struct rtnl_request *req;
1138 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1142 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1143 req->hdr.nlmsg_type = RTM_GETLINK;
1144 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1145 req->hdr.nlmsg_pid = 0;
1146 req->hdr.nlmsg_seq = request_seq++;
1147 req->msg.rtgen_family = AF_INET;
1149 return queue_request(req);
1152 static int send_getaddr(void)
1154 struct rtnl_request *req;
1158 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1162 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1163 req->hdr.nlmsg_type = RTM_GETADDR;
1164 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1165 req->hdr.nlmsg_pid = 0;
1166 req->hdr.nlmsg_seq = request_seq++;
1167 req->msg.rtgen_family = AF_INET;
1169 return queue_request(req);
1172 static int send_getroute(void)
1174 struct rtnl_request *req;
1178 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1182 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1183 req->hdr.nlmsg_type = RTM_GETROUTE;
1184 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1185 req->hdr.nlmsg_pid = 0;
1186 req->hdr.nlmsg_seq = request_seq++;
1187 req->msg.rtgen_family = AF_INET;
1189 return queue_request(req);
1192 static gboolean update_timeout_cb(gpointer user_data)
1194 __connman_rtnl_request_update();
1199 static void update_interval_callback(guint min)
1201 if (update_timeout > 0)
1202 g_source_remove(update_timeout);
1204 if (min < G_MAXUINT) {
1205 update_interval = min;
1206 update_timeout = g_timeout_add_seconds(update_interval,
1207 update_timeout_cb, NULL);
1210 update_interval = G_MAXUINT;
1214 static gint compare_interval(gconstpointer a, gconstpointer b)
1216 guint val_a = GPOINTER_TO_UINT(a);
1217 guint val_b = GPOINTER_TO_UINT(b);
1219 return val_a - val_b;
1222 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1229 update_list = g_slist_insert_sorted(update_list,
1230 GUINT_TO_POINTER(interval), compare_interval);
1232 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1233 if (min < update_interval) {
1234 update_interval_callback(min);
1235 __connman_rtnl_request_update();
1238 return update_interval;
1241 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1243 guint min = G_MAXUINT;
1248 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1250 if (update_list != NULL)
1251 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1253 if (min > update_interval)
1254 update_interval_callback(min);
1259 int __connman_rtnl_request_update(void)
1261 return send_getlink();
1264 int __connman_rtnl_init(void)
1266 struct sockaddr_nl addr;
1271 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1272 NULL, free_interface);
1274 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
1278 memset(&addr, 0, sizeof(addr));
1279 addr.nl_family = AF_NETLINK;
1280 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
1282 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1287 channel = g_io_channel_unix_new(sk);
1288 g_io_channel_set_close_on_unref(channel, TRUE);
1290 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1291 netlink_event, NULL);
1296 void __connman_rtnl_start(void)
1305 void __connman_rtnl_cleanup(void)
1311 for (list = watch_list; list; list = list->next) {
1312 struct watch_data *watch = list->data;
1314 DBG("removing watch %d", watch->id);
1320 g_slist_free(watch_list);
1323 g_slist_free(update_list);
1326 for (list = request_list; list; list = list->next) {
1327 struct rtnl_request *req = list->data;
1329 DBG("%s len %d type %d flags 0x%04x seq %d",
1330 type2string(req->hdr.nlmsg_type),
1331 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1332 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1338 g_slist_free(request_list);
1339 request_list = NULL;
1341 g_io_channel_shutdown(channel, TRUE, NULL);
1342 g_io_channel_unref(channel);
1346 g_hash_table_destroy(interface_list);