5 * Copyright (C) 2012 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
30 #include <sys/socket.h>
31 #include <sys/ioctl.h>
32 #include <arpa/inet.h>
33 #include <netinet/ether.h>
34 #include <netinet/icmp6.h>
35 #include <net/if_arp.h>
37 #include <linux/netlink.h>
38 #include <linux/rtnetlink.h>
39 #include <linux/wireless.h>
43 #include <connman/log.h>
49 #ifndef ARPHDR_PHONET_PIPE
50 #define ARPHDR_PHONET_PIPE (821)
53 #define print(arg...) do { if (0) connman_info(arg); } while (0)
54 #define debug(arg...) do { if (0) DBG(arg); } while (0)
59 vpn_rtnl_link_cb_t newlink;
63 static GSList *watch_list = NULL;
64 static unsigned int watch_id = 0;
66 static GSList *update_list = NULL;
67 static guint update_interval = G_MAXUINT;
68 static guint update_timeout = 0;
70 struct interface_data {
76 static GHashTable *interface_list = NULL;
78 static void free_interface(gpointer data)
80 struct interface_data *interface = data;
82 g_free(interface->ident);
83 g_free(interface->name);
88 * vpn_rtnl_add_newlink_watch:
89 * @index: network device index
90 * @callback: callback function
91 * @user_data: callback data;
93 * Add a new RTNL watch for newlink events
95 * Returns: %0 on failure and a unique id on success
97 unsigned int vpn_rtnl_add_newlink_watch(int index,
98 vpn_rtnl_link_cb_t callback, void *user_data)
100 struct watch_data *watch;
102 watch = g_try_new0(struct watch_data, 1);
106 watch->id = ++watch_id;
107 watch->index = index;
109 watch->newlink = callback;
110 watch->user_data = user_data;
112 watch_list = g_slist_prepend(watch_list, watch);
114 DBG("id %d", watch->id);
117 unsigned int flags = __vpn_ipconfig_get_flags_from_index(index);
120 callback(flags, 0, user_data);
127 * vpn_rtnl_remove_watch:
128 * @id: watch identifier
130 * Remove the RTNL watch for the identifier
132 void vpn_rtnl_remove_watch(unsigned int id)
141 for (list = watch_list; list; list = list->next) {
142 struct watch_data *watch = list->data;
144 if (watch->id == id) {
145 watch_list = g_slist_remove(watch_list, watch);
152 static void trigger_rtnl(int index, void *user_data)
154 struct vpn_rtnl *rtnl = user_data;
157 unsigned short type = __vpn_ipconfig_get_type_from_index(index);
158 unsigned int flags = __vpn_ipconfig_get_flags_from_index(index);
160 rtnl->newlink(type, index, flags, 0);
164 static GSList *rtnl_list = NULL;
166 static gint compare_priority(gconstpointer a, gconstpointer b)
168 const struct vpn_rtnl *rtnl1 = a;
169 const struct vpn_rtnl *rtnl2 = b;
171 return rtnl2->priority - rtnl1->priority;
178 * Register a new RTNL module
180 * Returns: %0 on success
182 int vpn_rtnl_register(struct vpn_rtnl *rtnl)
184 DBG("rtnl %p name %s", rtnl, rtnl->name);
186 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
189 __vpn_ipconfig_foreach(trigger_rtnl, rtnl);
195 * vpn_rtnl_unregister:
198 * Remove a previously registered RTNL module
200 void vpn_rtnl_unregister(struct vpn_rtnl *rtnl)
202 DBG("rtnl %p name %s", rtnl, rtnl->name);
204 rtnl_list = g_slist_remove(rtnl_list, rtnl);
207 static const char *operstate2str(unsigned char operstate)
210 case IF_OPER_UNKNOWN:
212 case IF_OPER_NOTPRESENT:
213 return "NOT-PRESENT";
216 case IF_OPER_LOWERLAYERDOWN:
217 return "LOWER-LAYER-DOWN";
218 case IF_OPER_TESTING:
220 case IF_OPER_DORMANT:
229 static void extract_link(struct ifinfomsg *msg, int bytes,
230 struct ether_addr *address, const char **ifname,
231 unsigned int *mtu, unsigned char *operstate,
232 struct rtnl_link_stats *stats)
236 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
237 attr = RTA_NEXT(attr, bytes)) {
238 switch (attr->rta_type) {
241 memcpy(address, RTA_DATA(attr), ETH_ALEN);
245 *ifname = RTA_DATA(attr);
249 *mtu = *((unsigned int *) RTA_DATA(attr));
253 memcpy(stats, RTA_DATA(attr),
254 sizeof(struct rtnl_link_stats));
257 if (operstate != NULL)
258 *operstate = *((unsigned char *) RTA_DATA(attr));
266 static void process_newlink(unsigned short type, int index, unsigned flags,
267 unsigned change, struct ifinfomsg *msg, int bytes)
269 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
270 struct ether_addr compare = {{ 0, 0, 0, 0, 0, 0 }};
271 struct rtnl_link_stats stats;
272 unsigned char operstate = 0xff;
273 struct interface_data *interface;
274 const char *ifname = NULL;
275 unsigned int mtu = 0;
276 char ident[13], str[18];
279 memset(&stats, 0, sizeof(stats));
280 extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats);
282 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
283 address.ether_addr_octet[0],
284 address.ether_addr_octet[1],
285 address.ether_addr_octet[2],
286 address.ether_addr_octet[3],
287 address.ether_addr_octet[4],
288 address.ether_addr_octet[5]);
290 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
291 address.ether_addr_octet[0],
292 address.ether_addr_octet[1],
293 address.ether_addr_octet[2],
294 address.ether_addr_octet[3],
295 address.ether_addr_octet[4],
296 address.ether_addr_octet[5]);
300 case ARPHRD_LOOPBACK:
301 case ARPHDR_PHONET_PIPE:
303 __vpn_ipconfig_newlink(index, type, flags,
308 if (memcmp(&address, &compare, ETH_ALEN) != 0)
309 connman_info("%s {newlink} index %d address %s mtu %u",
310 ifname, index, str, mtu);
312 if (operstate != 0xff)
313 connman_info("%s {newlink} index %d operstate %u <%s>",
314 ifname, index, operstate,
315 operstate2str(operstate));
317 interface = g_hash_table_lookup(interface_list, GINT_TO_POINTER(index));
318 if (interface == NULL) {
319 interface = g_new0(struct interface_data, 1);
320 interface->index = index;
321 interface->name = g_strdup(ifname);
322 interface->ident = g_strdup(ident);
324 g_hash_table_insert(interface_list,
325 GINT_TO_POINTER(index), interface);
328 for (list = rtnl_list; list; list = list->next) {
329 struct vpn_rtnl *rtnl = list->data;
332 rtnl->newlink(type, index, flags, change);
335 for (list = watch_list; list; list = list->next) {
336 struct watch_data *watch = list->data;
338 if (watch->index != index)
342 watch->newlink(flags, change, watch->user_data);
346 static void process_dellink(unsigned short type, int index, unsigned flags,
347 unsigned change, struct ifinfomsg *msg, int bytes)
349 struct rtnl_link_stats stats;
350 unsigned char operstate = 0xff;
351 const char *ifname = NULL;
354 memset(&stats, 0, sizeof(stats));
355 extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats);
357 if (operstate != 0xff)
358 connman_info("%s {dellink} index %d operstate %u <%s>",
359 ifname, index, operstate,
360 operstate2str(operstate));
362 for (list = rtnl_list; list; list = list->next) {
363 struct vpn_rtnl *rtnl = list->data;
366 rtnl->dellink(type, index, flags, change);
371 case ARPHRD_LOOPBACK:
373 __vpn_ipconfig_dellink(index, &stats);
377 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
380 static void extract_ipv4_route(struct rtmsg *msg, int bytes, int *index,
382 struct in_addr *gateway)
386 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
387 attr = RTA_NEXT(attr, bytes)) {
388 switch (attr->rta_type) {
391 *dst = *((struct in_addr *) RTA_DATA(attr));
395 *gateway = *((struct in_addr *) RTA_DATA(attr));
399 *index = *((int *) RTA_DATA(attr));
405 static void extract_ipv6_route(struct rtmsg *msg, int bytes, int *index,
406 struct in6_addr *dst,
407 struct in6_addr *gateway)
411 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
412 attr = RTA_NEXT(attr, bytes)) {
413 switch (attr->rta_type) {
416 *dst = *((struct in6_addr *) RTA_DATA(attr));
421 *((struct in6_addr *) RTA_DATA(attr));
425 *index = *((int *) RTA_DATA(attr));
431 static void process_newroute(unsigned char family, unsigned char scope,
432 struct rtmsg *msg, int bytes)
435 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
438 if (family == AF_INET) {
439 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
441 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
443 inet_ntop(family, &dst, dststr, sizeof(dststr));
444 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
446 /* skip host specific routes */
447 if (scope != RT_SCOPE_UNIVERSE &&
448 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
451 if (dst.s_addr != INADDR_ANY)
454 } else if (family == AF_INET6) {
455 struct in6_addr dst = IN6ADDR_ANY_INIT,
456 gateway = IN6ADDR_ANY_INIT;
458 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
460 inet_ntop(family, &dst, dststr, sizeof(dststr));
461 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
463 /* skip host specific routes */
464 if (scope != RT_SCOPE_UNIVERSE &&
465 !(scope == RT_SCOPE_LINK &&
466 IN6_IS_ADDR_UNSPECIFIED(&dst)))
469 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
474 for (list = rtnl_list; list; list = list->next) {
475 struct vpn_rtnl *rtnl = list->data;
477 if (rtnl->newgateway)
478 rtnl->newgateway(index, gatewaystr);
482 static void process_delroute(unsigned char family, unsigned char scope,
483 struct rtmsg *msg, int bytes)
486 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
489 if (family == AF_INET) {
490 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
492 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
494 inet_ntop(family, &dst, dststr, sizeof(dststr));
495 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
497 /* skip host specific routes */
498 if (scope != RT_SCOPE_UNIVERSE &&
499 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
502 if (dst.s_addr != INADDR_ANY)
505 } else if (family == AF_INET6) {
506 struct in6_addr dst = IN6ADDR_ANY_INIT,
507 gateway = IN6ADDR_ANY_INIT;
509 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
511 inet_ntop(family, &dst, dststr, sizeof(dststr));
512 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
514 /* skip host specific routes */
515 if (scope != RT_SCOPE_UNIVERSE &&
516 !(scope == RT_SCOPE_LINK &&
517 IN6_IS_ADDR_UNSPECIFIED(&dst)))
520 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
525 for (list = rtnl_list; list; list = list->next) {
526 struct vpn_rtnl *rtnl = list->data;
528 if (rtnl->delgateway)
529 rtnl->delgateway(index, gatewaystr);
533 static inline void print_ether(struct rtattr *attr, const char *name)
535 int len = (int) RTA_PAYLOAD(attr);
537 if (len == ETH_ALEN) {
538 struct ether_addr eth;
539 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
540 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
542 print(" attr %s (len %d)\n", name, len);
545 static inline void print_inet(struct rtattr *attr, const char *name,
546 unsigned char family)
548 int len = (int) RTA_PAYLOAD(attr);
550 if (family == AF_INET && len == sizeof(struct in_addr)) {
552 addr = *((struct in_addr *) RTA_DATA(attr));
553 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
555 print(" attr %s (len %d)\n", name, len);
558 static inline void print_string(struct rtattr *attr, const char *name)
560 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
561 (char *) RTA_DATA(attr));
564 static inline void print_byte(struct rtattr *attr, const char *name)
566 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
567 *((unsigned char *) RTA_DATA(attr)));
570 static inline void print_integer(struct rtattr *attr, const char *name)
572 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
573 *((int *) RTA_DATA(attr)));
576 static inline void print_attr(struct rtattr *attr, const char *name)
578 int len = (int) RTA_PAYLOAD(attr);
581 print(" attr %s (len %d)\n", name, len);
583 print(" attr %d (len %d)\n", attr->rta_type, len);
586 static void rtnl_link(struct nlmsghdr *hdr)
588 struct ifinfomsg *msg;
592 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
593 bytes = IFLA_PAYLOAD(hdr);
595 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
597 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
598 attr = RTA_NEXT(attr, bytes)) {
599 switch (attr->rta_type) {
601 print_ether(attr, "address");
604 print_ether(attr, "broadcast");
607 print_string(attr, "ifname");
610 print_integer(attr, "mtu");
613 print_attr(attr, "link");
616 print_attr(attr, "qdisc");
619 print_attr(attr, "stats");
622 print_attr(attr, "cost");
625 print_attr(attr, "priority");
628 print_attr(attr, "master");
631 print_attr(attr, "wireless");
634 print_attr(attr, "protinfo");
637 print_integer(attr, "txqlen");
640 print_attr(attr, "map");
643 print_attr(attr, "weight");
646 print_byte(attr, "operstate");
649 print_byte(attr, "linkmode");
652 print_attr(attr, NULL);
658 static void rtnl_newlink(struct nlmsghdr *hdr)
660 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
664 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
665 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
668 static void rtnl_dellink(struct nlmsghdr *hdr)
670 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
674 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
675 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
678 static void rtnl_route(struct nlmsghdr *hdr)
684 msg = (struct rtmsg *) NLMSG_DATA(hdr);
685 bytes = RTM_PAYLOAD(hdr);
687 print("rtm_family %d rtm_table %d rtm_protocol %d",
688 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
689 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
690 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
692 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
693 attr = RTA_NEXT(attr, bytes)) {
694 switch (attr->rta_type) {
696 print_inet(attr, "dst", msg->rtm_family);
699 print_inet(attr, "src", msg->rtm_family);
702 print_string(attr, "iif");
705 print_integer(attr, "oif");
708 print_inet(attr, "gateway", msg->rtm_family);
711 print_attr(attr, "priority");
714 print_inet(attr, "prefsrc", msg->rtm_family);
717 print_attr(attr, "metrics");
720 print_integer(attr, "table");
723 print_attr(attr, NULL);
729 static connman_bool_t is_route_rtmsg(struct rtmsg *msg)
732 if (msg->rtm_table != RT_TABLE_MAIN)
735 if (msg->rtm_protocol != RTPROT_BOOT &&
736 msg->rtm_protocol != RTPROT_KERNEL)
739 if (msg->rtm_type != RTN_UNICAST)
745 static void rtnl_newroute(struct nlmsghdr *hdr)
747 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
751 if (is_route_rtmsg(msg))
752 process_newroute(msg->rtm_family, msg->rtm_scope,
753 msg, RTM_PAYLOAD(hdr));
756 static void rtnl_delroute(struct nlmsghdr *hdr)
758 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
762 if (is_route_rtmsg(msg))
763 process_delroute(msg->rtm_family, msg->rtm_scope,
764 msg, RTM_PAYLOAD(hdr));
767 static const char *type2string(uint16_t type)
794 case RTM_NEWNDUSEROPT:
795 return "NEWNDUSEROPT";
801 static GIOChannel *channel = NULL;
803 struct rtnl_request {
807 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
809 static GSList *request_list = NULL;
810 static guint32 request_seq = 0;
812 static struct rtnl_request *find_request(guint32 seq)
816 for (list = request_list; list; list = list->next) {
817 struct rtnl_request *req = list->data;
819 if (req->hdr.nlmsg_seq == seq)
826 static int send_request(struct rtnl_request *req)
828 struct sockaddr_nl addr;
831 debug("%s len %d type %d flags 0x%04x seq %d",
832 type2string(req->hdr.nlmsg_type),
833 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
834 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
836 sk = g_io_channel_unix_get_fd(channel);
838 memset(&addr, 0, sizeof(addr));
839 addr.nl_family = AF_NETLINK;
841 return sendto(sk, req, req->hdr.nlmsg_len, 0,
842 (struct sockaddr *) &addr, sizeof(addr));
845 static int queue_request(struct rtnl_request *req)
847 request_list = g_slist_append(request_list, req);
849 if (g_slist_length(request_list) > 1)
852 return send_request(req);
855 static int process_response(guint32 seq)
857 struct rtnl_request *req;
859 debug("seq %d", seq);
861 req = find_request(seq);
863 request_list = g_slist_remove(request_list, req);
867 req = g_slist_nth_data(request_list, 0);
871 return send_request(req);
874 static void rtnl_message(void *buf, size_t len)
876 debug("buf %p len %zd", buf, len);
879 struct nlmsghdr *hdr = buf;
880 struct nlmsgerr *err;
882 if (!NLMSG_OK(hdr, len))
885 debug("%s len %d type %d flags 0x%04x seq %d pid %d",
886 type2string(hdr->nlmsg_type),
887 hdr->nlmsg_len, hdr->nlmsg_type,
888 hdr->nlmsg_flags, hdr->nlmsg_seq,
891 switch (hdr->nlmsg_type) {
896 process_response(hdr->nlmsg_seq);
899 err = NLMSG_DATA(hdr);
900 DBG("error %d (%s)", -err->error,
901 strerror(-err->error));
921 len -= hdr->nlmsg_len;
922 buf += hdr->nlmsg_len;
926 static gboolean netlink_event(GIOChannel *chan,
927 GIOCondition cond, gpointer data)
929 unsigned char buf[4096];
930 struct sockaddr_nl nladdr;
931 socklen_t addr_len = sizeof(nladdr);
935 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
938 memset(buf, 0, sizeof(buf));
939 memset(&nladdr, 0, sizeof(nladdr));
941 fd = g_io_channel_unix_get_fd(chan);
943 status = recvfrom(fd, buf, sizeof(buf), 0,
944 (struct sockaddr *) &nladdr, &addr_len);
946 if (errno == EINTR || errno == EAGAIN)
955 if (nladdr.nl_pid != 0) { /* not sent by kernel, ignore */
956 DBG("Received msg from %u, ignoring it", nladdr.nl_pid);
960 rtnl_message(buf, status);
965 static int send_getlink(void)
967 struct rtnl_request *req;
971 req = g_try_malloc0(RTNL_REQUEST_SIZE);
975 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
976 req->hdr.nlmsg_type = RTM_GETLINK;
977 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
978 req->hdr.nlmsg_pid = 0;
979 req->hdr.nlmsg_seq = request_seq++;
980 req->msg.rtgen_family = AF_INET;
982 return queue_request(req);
985 static int send_getaddr(void)
987 struct rtnl_request *req;
991 req = g_try_malloc0(RTNL_REQUEST_SIZE);
995 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
996 req->hdr.nlmsg_type = RTM_GETADDR;
997 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
998 req->hdr.nlmsg_pid = 0;
999 req->hdr.nlmsg_seq = request_seq++;
1000 req->msg.rtgen_family = AF_INET;
1002 return queue_request(req);
1005 static int send_getroute(void)
1007 struct rtnl_request *req;
1011 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1015 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1016 req->hdr.nlmsg_type = RTM_GETROUTE;
1017 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1018 req->hdr.nlmsg_pid = 0;
1019 req->hdr.nlmsg_seq = request_seq++;
1020 req->msg.rtgen_family = AF_INET;
1022 return queue_request(req);
1025 static gboolean update_timeout_cb(gpointer user_data)
1027 __vpn_rtnl_request_update();
1032 static void update_interval_callback(guint min)
1034 if (update_timeout > 0)
1035 g_source_remove(update_timeout);
1037 if (min < G_MAXUINT) {
1038 update_interval = min;
1039 update_timeout = g_timeout_add_seconds(update_interval,
1040 update_timeout_cb, NULL);
1043 update_interval = G_MAXUINT;
1047 static gint compare_interval(gconstpointer a, gconstpointer b)
1049 guint val_a = GPOINTER_TO_UINT(a);
1050 guint val_b = GPOINTER_TO_UINT(b);
1052 return val_a - val_b;
1055 unsigned int __vpn_rtnl_update_interval_add(unsigned int interval)
1062 update_list = g_slist_insert_sorted(update_list,
1063 GUINT_TO_POINTER(interval), compare_interval);
1065 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1066 if (min < update_interval) {
1067 update_interval_callback(min);
1068 __vpn_rtnl_request_update();
1071 return update_interval;
1074 unsigned int __vpn_rtnl_update_interval_remove(unsigned int interval)
1076 guint min = G_MAXUINT;
1081 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1083 if (update_list != NULL)
1084 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1086 if (min > update_interval)
1087 update_interval_callback(min);
1092 int __vpn_rtnl_request_update(void)
1094 return send_getlink();
1097 int __vpn_rtnl_init(void)
1099 struct sockaddr_nl addr;
1104 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1105 NULL, free_interface);
1107 sk = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
1111 memset(&addr, 0, sizeof(addr));
1112 addr.nl_family = AF_NETLINK;
1113 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1114 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1115 (1<<(RTNLGRP_ND_USEROPT-1));
1117 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1122 channel = g_io_channel_unix_new(sk);
1123 g_io_channel_set_close_on_unref(channel, TRUE);
1125 g_io_channel_set_encoding(channel, NULL, NULL);
1126 g_io_channel_set_buffered(channel, FALSE);
1128 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1129 netlink_event, NULL);
1134 void __vpn_rtnl_start(void)
1143 void __vpn_rtnl_cleanup(void)
1149 for (list = watch_list; list; list = list->next) {
1150 struct watch_data *watch = list->data;
1152 DBG("removing watch %d", watch->id);
1158 g_slist_free(watch_list);
1161 g_slist_free(update_list);
1164 for (list = request_list; list; list = list->next) {
1165 struct rtnl_request *req = list->data;
1167 debug("%s len %d type %d flags 0x%04x seq %d",
1168 type2string(req->hdr.nlmsg_type),
1169 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1170 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1176 g_slist_free(request_list);
1177 request_list = NULL;
1179 g_io_channel_shutdown(channel, TRUE, NULL);
1180 g_io_channel_unref(channel);
1184 g_hash_table_destroy(interface_list);