5 * Copyright (C) 2012-2013 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 {
75 static GHashTable *interface_list = NULL;
77 static void free_interface(gpointer data)
79 struct interface_data *interface = data;
81 g_free(interface->ident);
86 * vpn_rtnl_add_newlink_watch:
87 * @index: network device index
88 * @callback: callback function
89 * @user_data: callback data;
91 * Add a new RTNL watch for newlink events
93 * Returns: %0 on failure and a unique id on success
95 unsigned int vpn_rtnl_add_newlink_watch(int index,
96 vpn_rtnl_link_cb_t callback, void *user_data)
98 struct watch_data *watch;
100 watch = g_try_new0(struct watch_data, 1);
104 watch->id = ++watch_id;
105 watch->index = index;
107 watch->newlink = callback;
108 watch->user_data = user_data;
110 watch_list = g_slist_prepend(watch_list, watch);
112 DBG("id %d", watch->id);
115 unsigned int flags = __vpn_ipconfig_get_flags_from_index(index);
118 callback(flags, 0, user_data);
125 * vpn_rtnl_remove_watch:
126 * @id: watch identifier
128 * Remove the RTNL watch for the identifier
130 void vpn_rtnl_remove_watch(unsigned int id)
139 for (list = watch_list; list; list = list->next) {
140 struct watch_data *watch = list->data;
142 if (watch->id == id) {
143 watch_list = g_slist_remove(watch_list, watch);
150 static void trigger_rtnl(int index, void *user_data)
152 struct vpn_rtnl *rtnl = user_data;
155 unsigned short type = __vpn_ipconfig_get_type_from_index(index);
156 unsigned int flags = __vpn_ipconfig_get_flags_from_index(index);
158 rtnl->newlink(type, index, flags, 0);
162 static GSList *rtnl_list = NULL;
164 static gint compare_priority(gconstpointer a, gconstpointer b)
166 const struct vpn_rtnl *rtnl1 = a;
167 const struct vpn_rtnl *rtnl2 = b;
169 return rtnl2->priority - rtnl1->priority;
176 * Register a new RTNL module
178 * Returns: %0 on success
180 int vpn_rtnl_register(struct vpn_rtnl *rtnl)
182 DBG("rtnl %p name %s", rtnl, rtnl->name);
184 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
187 vpn_ipconfig_foreach(trigger_rtnl, rtnl);
193 * vpn_rtnl_unregister:
196 * Remove a previously registered RTNL module
198 void vpn_rtnl_unregister(struct vpn_rtnl *rtnl)
200 DBG("rtnl %p name %s", rtnl, rtnl->name);
202 rtnl_list = g_slist_remove(rtnl_list, rtnl);
205 static const char *operstate2str(unsigned char operstate)
208 case IF_OPER_UNKNOWN:
210 case IF_OPER_NOTPRESENT:
211 return "NOT-PRESENT";
214 case IF_OPER_LOWERLAYERDOWN:
215 return "LOWER-LAYER-DOWN";
216 case IF_OPER_TESTING:
218 case IF_OPER_DORMANT:
227 static void extract_link(struct ifinfomsg *msg, int bytes,
228 struct ether_addr *address, const char **ifname,
229 unsigned int *mtu, unsigned char *operstate,
230 struct rtnl_link_stats *stats)
234 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
235 attr = RTA_NEXT(attr, bytes)) {
236 switch (attr->rta_type) {
239 memcpy(address, RTA_DATA(attr), ETH_ALEN);
243 *ifname = RTA_DATA(attr);
247 *mtu = *((unsigned int *) RTA_DATA(attr));
251 memcpy(stats, RTA_DATA(attr),
252 sizeof(struct rtnl_link_stats));
256 *operstate = *((unsigned char *) RTA_DATA(attr));
264 static void process_newlink(unsigned short type, int index, unsigned flags,
265 unsigned change, struct ifinfomsg *msg, int bytes)
267 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
268 struct ether_addr compare = {{ 0, 0, 0, 0, 0, 0 }};
269 struct rtnl_link_stats stats;
270 unsigned char operstate = 0xff;
271 struct interface_data *interface;
272 const char *ifname = NULL;
273 unsigned int mtu = 0;
274 char ident[13], str[18];
277 memset(&stats, 0, sizeof(stats));
278 extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats);
280 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
281 address.ether_addr_octet[0],
282 address.ether_addr_octet[1],
283 address.ether_addr_octet[2],
284 address.ether_addr_octet[3],
285 address.ether_addr_octet[4],
286 address.ether_addr_octet[5]);
288 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
289 address.ether_addr_octet[0],
290 address.ether_addr_octet[1],
291 address.ether_addr_octet[2],
292 address.ether_addr_octet[3],
293 address.ether_addr_octet[4],
294 address.ether_addr_octet[5]);
298 case ARPHRD_LOOPBACK:
299 case ARPHDR_PHONET_PIPE:
301 __vpn_ipconfig_newlink(index, type, flags,
306 if (memcmp(&address, &compare, ETH_ALEN) != 0)
307 connman_info("%s {newlink} index %d address %s mtu %u",
308 ifname, index, str, mtu);
310 if (operstate != 0xff)
311 connman_info("%s {newlink} index %d operstate %u <%s>",
312 ifname, index, operstate,
313 operstate2str(operstate));
315 interface = g_hash_table_lookup(interface_list, GINT_TO_POINTER(index));
317 interface = g_new0(struct interface_data, 1);
318 interface->index = index;
319 interface->ident = g_strdup(ident);
321 g_hash_table_insert(interface_list,
322 GINT_TO_POINTER(index), interface);
325 for (list = rtnl_list; list; list = list->next) {
326 struct vpn_rtnl *rtnl = list->data;
329 rtnl->newlink(type, index, flags, change);
332 for (list = watch_list; list; list = list->next) {
333 struct watch_data *watch = list->data;
335 if (watch->index != index)
339 watch->newlink(flags, change, watch->user_data);
343 static void process_dellink(unsigned short type, int index, unsigned flags,
344 unsigned change, struct ifinfomsg *msg, int bytes)
346 struct rtnl_link_stats stats;
347 unsigned char operstate = 0xff;
348 const char *ifname = NULL;
351 memset(&stats, 0, sizeof(stats));
352 extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats);
354 if (operstate != 0xff)
355 connman_info("%s {dellink} index %d operstate %u <%s>",
356 ifname, index, operstate,
357 operstate2str(operstate));
359 for (list = rtnl_list; list; list = list->next) {
360 struct vpn_rtnl *rtnl = list->data;
363 rtnl->dellink(type, index, flags, change);
368 case ARPHRD_LOOPBACK:
370 __vpn_ipconfig_dellink(index, &stats);
374 g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
377 static void extract_ipv4_route(struct rtmsg *msg, int bytes, int *index,
379 struct in_addr *gateway)
383 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
384 attr = RTA_NEXT(attr, bytes)) {
385 switch (attr->rta_type) {
388 *dst = *((struct in_addr *) RTA_DATA(attr));
392 *gateway = *((struct in_addr *) RTA_DATA(attr));
396 *index = *((int *) RTA_DATA(attr));
402 static void extract_ipv6_route(struct rtmsg *msg, int bytes, int *index,
403 struct in6_addr *dst,
404 struct in6_addr *gateway)
408 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
409 attr = RTA_NEXT(attr, bytes)) {
410 switch (attr->rta_type) {
413 *dst = *((struct in6_addr *) RTA_DATA(attr));
418 *((struct in6_addr *) RTA_DATA(attr));
422 *index = *((int *) RTA_DATA(attr));
428 static void process_newroute(unsigned char family, unsigned char scope,
429 struct rtmsg *msg, int bytes)
432 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
435 if (family == AF_INET) {
436 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
438 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
440 inet_ntop(family, &dst, dststr, sizeof(dststr));
441 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
443 /* skip host specific routes */
444 if (scope != RT_SCOPE_UNIVERSE &&
445 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
448 if (dst.s_addr != INADDR_ANY)
451 } else if (family == AF_INET6) {
452 struct in6_addr dst = IN6ADDR_ANY_INIT,
453 gateway = IN6ADDR_ANY_INIT;
455 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
457 inet_ntop(family, &dst, dststr, sizeof(dststr));
458 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
460 /* skip host specific routes */
461 if (scope != RT_SCOPE_UNIVERSE &&
462 !(scope == RT_SCOPE_LINK &&
463 IN6_IS_ADDR_UNSPECIFIED(&dst)))
466 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
471 for (list = rtnl_list; list; list = list->next) {
472 struct vpn_rtnl *rtnl = list->data;
474 if (rtnl->newgateway)
475 rtnl->newgateway(index, gatewaystr);
479 static void process_delroute(unsigned char family, unsigned char scope,
480 struct rtmsg *msg, int bytes)
483 char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
486 if (family == AF_INET) {
487 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
489 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
491 inet_ntop(family, &dst, dststr, sizeof(dststr));
492 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
494 /* skip host specific routes */
495 if (scope != RT_SCOPE_UNIVERSE &&
496 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
499 if (dst.s_addr != INADDR_ANY)
502 } else if (family == AF_INET6) {
503 struct in6_addr dst = IN6ADDR_ANY_INIT,
504 gateway = IN6ADDR_ANY_INIT;
506 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
508 inet_ntop(family, &dst, dststr, sizeof(dststr));
509 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
511 /* skip host specific routes */
512 if (scope != RT_SCOPE_UNIVERSE &&
513 !(scope == RT_SCOPE_LINK &&
514 IN6_IS_ADDR_UNSPECIFIED(&dst)))
517 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
522 for (list = rtnl_list; list; list = list->next) {
523 struct vpn_rtnl *rtnl = list->data;
525 if (rtnl->delgateway)
526 rtnl->delgateway(index, gatewaystr);
530 static inline void print_ether(struct rtattr *attr, const char *name)
532 int len = (int) RTA_PAYLOAD(attr);
534 if (len == ETH_ALEN) {
535 struct ether_addr eth;
536 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
537 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
539 print(" attr %s (len %d)\n", name, len);
542 static inline void print_inet(struct rtattr *attr, const char *name,
543 unsigned char family)
545 int len = (int) RTA_PAYLOAD(attr);
547 if (family == AF_INET && len == sizeof(struct in_addr)) {
549 addr = *((struct in_addr *) RTA_DATA(attr));
550 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
552 print(" attr %s (len %d)\n", name, len);
555 static inline void print_string(struct rtattr *attr, const char *name)
557 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
558 (char *) RTA_DATA(attr));
561 static inline void print_byte(struct rtattr *attr, const char *name)
563 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
564 *((unsigned char *) RTA_DATA(attr)));
567 static inline void print_integer(struct rtattr *attr, const char *name)
569 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
570 *((int *) RTA_DATA(attr)));
573 static inline void print_attr(struct rtattr *attr, const char *name)
575 int len = (int) RTA_PAYLOAD(attr);
578 print(" attr %s (len %d)\n", name, len);
580 print(" attr %d (len %d)\n", attr->rta_type, len);
583 static void rtnl_link(struct nlmsghdr *hdr)
585 struct ifinfomsg *msg;
589 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
590 bytes = IFLA_PAYLOAD(hdr);
592 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
594 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
595 attr = RTA_NEXT(attr, bytes)) {
596 switch (attr->rta_type) {
598 print_ether(attr, "address");
601 print_ether(attr, "broadcast");
604 print_string(attr, "ifname");
607 print_integer(attr, "mtu");
610 print_attr(attr, "link");
613 print_attr(attr, "qdisc");
616 print_attr(attr, "stats");
619 print_attr(attr, "cost");
622 print_attr(attr, "priority");
625 print_attr(attr, "master");
628 print_attr(attr, "wireless");
631 print_attr(attr, "protinfo");
634 print_integer(attr, "txqlen");
637 print_attr(attr, "map");
640 print_attr(attr, "weight");
643 print_byte(attr, "operstate");
646 print_byte(attr, "linkmode");
649 print_attr(attr, NULL);
655 static void rtnl_newlink(struct nlmsghdr *hdr)
657 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
661 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
662 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
665 static void rtnl_dellink(struct nlmsghdr *hdr)
667 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
671 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
672 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
675 static void rtnl_route(struct nlmsghdr *hdr)
681 msg = (struct rtmsg *) NLMSG_DATA(hdr);
682 bytes = RTM_PAYLOAD(hdr);
684 print("rtm_family %d rtm_table %d rtm_protocol %d",
685 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
686 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
687 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
689 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
690 attr = RTA_NEXT(attr, bytes)) {
691 switch (attr->rta_type) {
693 print_inet(attr, "dst", msg->rtm_family);
696 print_inet(attr, "src", msg->rtm_family);
699 print_string(attr, "iif");
702 print_integer(attr, "oif");
705 print_inet(attr, "gateway", msg->rtm_family);
708 print_attr(attr, "priority");
711 print_inet(attr, "prefsrc", msg->rtm_family);
714 print_attr(attr, "metrics");
717 print_integer(attr, "table");
720 print_attr(attr, NULL);
726 static bool is_route_rtmsg(struct rtmsg *msg)
729 if (msg->rtm_table != RT_TABLE_MAIN)
732 if (msg->rtm_protocol != RTPROT_BOOT &&
733 msg->rtm_protocol != RTPROT_KERNEL)
736 if (msg->rtm_type != RTN_UNICAST)
742 static void rtnl_newroute(struct nlmsghdr *hdr)
744 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
748 if (is_route_rtmsg(msg))
749 process_newroute(msg->rtm_family, msg->rtm_scope,
750 msg, RTM_PAYLOAD(hdr));
753 static void rtnl_delroute(struct nlmsghdr *hdr)
755 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
759 if (is_route_rtmsg(msg))
760 process_delroute(msg->rtm_family, msg->rtm_scope,
761 msg, RTM_PAYLOAD(hdr));
764 static const char *type2string(uint16_t type)
791 case RTM_NEWNDUSEROPT:
792 return "NEWNDUSEROPT";
798 static GIOChannel *channel = NULL;
800 #define RTNL_REQUEST_SIZE (NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(struct rtgenmsg)))
802 static GSList *request_list = NULL;
803 static guint32 request_seq = 0;
805 static struct nlmsghdr *find_request(guint32 seq)
809 for (list = request_list; list; list = list->next) {
810 struct nlmsghdr *hdr = list->data;
812 if (hdr->nlmsg_seq == seq)
819 static int send_request(struct nlmsghdr *hdr)
821 struct sockaddr_nl addr;
824 debug("%s len %d type %d flags 0x%04x seq %d",
825 type2string(hdr->nlmsg_type),
826 hdr->nlmsg_len, hdr->nlmsg_type,
827 hdr->nlmsg_flags, hdr->nlmsg_seq);
829 sk = g_io_channel_unix_get_fd(channel);
831 memset(&addr, 0, sizeof(addr));
832 addr.nl_family = AF_NETLINK;
834 return sendto(sk, hdr, hdr->nlmsg_len, 0,
835 (struct sockaddr *) &addr, sizeof(addr));
838 static int queue_request(struct nlmsghdr *hdr)
840 request_list = g_slist_append(request_list, hdr);
842 if (g_slist_length(request_list) > 1)
845 return send_request(hdr);
848 static int process_response(guint32 seq)
850 struct nlmsghdr *hdr;
852 debug("seq %d", seq);
854 hdr = find_request(seq);
856 request_list = g_slist_remove(request_list, hdr);
860 hdr = g_slist_nth_data(request_list, 0);
864 return send_request(hdr);
867 static void rtnl_message(void *buf, size_t len)
869 debug("buf %p len %zd", buf, len);
872 struct nlmsghdr *hdr = buf;
873 struct nlmsgerr *err;
875 if (!NLMSG_OK(hdr, len))
878 debug("%s len %u type %u flags 0x%04x seq %u pid %u",
879 type2string(hdr->nlmsg_type),
880 hdr->nlmsg_len, hdr->nlmsg_type,
881 hdr->nlmsg_flags, hdr->nlmsg_seq,
884 switch (hdr->nlmsg_type) {
889 process_response(hdr->nlmsg_seq);
892 err = NLMSG_DATA(hdr);
893 DBG("error %d (%s)", -err->error,
894 strerror(-err->error));
914 len -= hdr->nlmsg_len;
915 buf += hdr->nlmsg_len;
919 static gboolean netlink_event(GIOChannel *chan, GIOCondition cond, gpointer data)
921 unsigned char buf[4096];
922 struct sockaddr_nl nladdr;
923 socklen_t addr_len = sizeof(nladdr);
927 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
930 memset(buf, 0, sizeof(buf));
931 memset(&nladdr, 0, sizeof(nladdr));
933 fd = g_io_channel_unix_get_fd(chan);
935 status = recvfrom(fd, buf, sizeof(buf), 0,
936 (struct sockaddr *) &nladdr, &addr_len);
938 if (errno == EINTR || errno == EAGAIN)
947 if (nladdr.nl_pid != 0) { /* not sent by kernel, ignore */
948 DBG("Received msg from %u, ignoring it", nladdr.nl_pid);
952 rtnl_message(buf, status);
957 static int send_getlink(void)
959 struct nlmsghdr *hdr;
960 struct rtgenmsg *msg;
964 hdr = g_malloc0(RTNL_REQUEST_SIZE);
966 hdr->nlmsg_len = RTNL_REQUEST_SIZE;
967 hdr->nlmsg_type = RTM_GETLINK;
968 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
970 hdr->nlmsg_seq = request_seq++;
972 msg = (struct rtgenmsg *) NLMSG_DATA(hdr);
973 msg->rtgen_family = AF_INET;
975 return queue_request(hdr);
978 static int send_getaddr(void)
980 struct nlmsghdr *hdr;
981 struct rtgenmsg *msg;
985 hdr = g_malloc0(RTNL_REQUEST_SIZE);
987 hdr->nlmsg_len = RTNL_REQUEST_SIZE;
988 hdr->nlmsg_type = RTM_GETADDR;
989 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
991 hdr->nlmsg_seq = request_seq++;
993 msg = (struct rtgenmsg *) NLMSG_DATA(hdr);
994 msg->rtgen_family = AF_INET;
996 return queue_request(hdr);
999 static int send_getroute(void)
1001 struct nlmsghdr *hdr;
1002 struct rtgenmsg *msg;
1006 hdr = g_malloc0(RTNL_REQUEST_SIZE);
1008 hdr->nlmsg_len = RTNL_REQUEST_SIZE;
1009 hdr->nlmsg_type = RTM_GETROUTE;
1010 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1012 hdr->nlmsg_seq = request_seq++;
1014 msg = (struct rtgenmsg *) NLMSG_DATA(hdr);
1015 msg->rtgen_family = AF_INET;
1017 return queue_request(hdr);
1020 static gboolean update_timeout_cb(gpointer user_data)
1022 __vpn_rtnl_request_update();
1027 static void update_interval_callback(guint min)
1029 if (update_timeout > 0)
1030 g_source_remove(update_timeout);
1032 if (min < G_MAXUINT) {
1033 update_interval = min;
1034 update_timeout = g_timeout_add_seconds(update_interval,
1035 update_timeout_cb, NULL);
1038 update_interval = G_MAXUINT;
1042 static gint compare_interval(gconstpointer a, gconstpointer b)
1044 guint val_a = GPOINTER_TO_UINT(a);
1045 guint val_b = GPOINTER_TO_UINT(b);
1047 return val_a - val_b;
1050 unsigned int __vpn_rtnl_update_interval_add(unsigned int interval)
1057 update_list = g_slist_insert_sorted(update_list,
1058 GUINT_TO_POINTER(interval), compare_interval);
1060 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1061 if (min < update_interval) {
1062 update_interval_callback(min);
1063 __vpn_rtnl_request_update();
1066 return update_interval;
1069 unsigned int __vpn_rtnl_update_interval_remove(unsigned int interval)
1071 guint min = G_MAXUINT;
1076 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1079 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1081 if (min > update_interval)
1082 update_interval_callback(min);
1087 int __vpn_rtnl_request_update(void)
1089 return send_getlink();
1092 int __vpn_rtnl_init(void)
1094 struct sockaddr_nl addr;
1099 interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1100 NULL, free_interface);
1102 sk = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
1106 memset(&addr, 0, sizeof(addr));
1107 addr.nl_family = AF_NETLINK;
1108 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1109 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1110 (1<<(RTNLGRP_ND_USEROPT-1));
1112 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1117 channel = g_io_channel_unix_new(sk);
1118 g_io_channel_set_close_on_unref(channel, TRUE);
1120 g_io_channel_set_encoding(channel, NULL, NULL);
1121 g_io_channel_set_buffered(channel, FALSE);
1123 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1124 netlink_event, NULL);
1129 void __vpn_rtnl_start(void)
1138 void __vpn_rtnl_cleanup(void)
1144 for (list = watch_list; list; list = list->next) {
1145 struct watch_data *watch = list->data;
1147 DBG("removing watch %d", watch->id);
1153 g_slist_free(watch_list);
1156 g_slist_free(update_list);
1159 for (list = request_list; list; list = list->next) {
1160 struct nlmsghdr *hdr = list->data;
1162 debug("%s len %d type %d flags 0x%04x seq %d",
1163 type2string(hdr->nlmsg_type),
1164 hdr->nlmsg_len, hdr->nlmsg_type,
1165 hdr->nlmsg_flags, hdr->nlmsg_seq);
1171 g_slist_free(request_list);
1172 request_list = NULL;
1174 g_io_channel_shutdown(channel, TRUE, NULL);
1175 g_io_channel_unref(channel);
1179 g_hash_table_destroy(interface_list);