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;
60 * connman_rtnl_add_operstate_watch:
61 * @index: network device index
62 * @callback: callback function
63 * @user_data: callback data;
65 * Add a new RTNL watch for operation state events
67 * Returns: %0 on failure and a unique id on success
69 unsigned int connman_rtnl_add_operstate_watch(int index,
70 connman_rtnl_operstate_cb_t callback, void *user_data)
72 struct watch_data *watch;
74 watch = g_try_new0(struct watch_data, 1);
78 watch->id = ++watch_id;
81 watch->operstate = callback;
82 watch->user_data = user_data;
84 watch_list = g_slist_prepend(watch_list, watch);
86 DBG("id %d", watch->id);
89 unsigned char operstate = 0;
92 callback(operstate, user_data);
99 * connman_rtnl_add_newlink_watch:
100 * @index: network device index
101 * @callback: callback function
102 * @user_data: callback data;
104 * Add a new RTNL watch for newlink events
106 * Returns: %0 on failure and a unique id on success
108 unsigned int connman_rtnl_add_newlink_watch(int index,
109 connman_rtnl_link_cb_t callback, void *user_data)
111 struct watch_data *watch;
113 watch = g_try_new0(struct watch_data, 1);
117 watch->id = ++watch_id;
118 watch->index = index;
120 watch->newlink = callback;
121 watch->user_data = user_data;
123 watch_list = g_slist_prepend(watch_list, watch);
125 DBG("id %d", watch->id);
128 unsigned int flags = __connman_ipconfig_get_flags(index);
131 callback(flags, 0, user_data);
138 * connman_rtnl_remove_watch:
139 * @id: watch identifier
141 * Remove the RTNL watch for the identifier
143 void connman_rtnl_remove_watch(unsigned int id)
152 for (list = watch_list; list; list = list->next) {
153 struct watch_data *watch = list->data;
155 if (watch->id == id) {
156 watch_list = g_slist_remove(watch_list, watch);
163 static void trigger_rtnl(int index, void *user_data)
165 struct connman_rtnl *rtnl = user_data;
168 unsigned short type = __connman_ipconfig_get_type(index);
169 unsigned int flags = __connman_ipconfig_get_flags(index);
171 rtnl->newlink(type, index, flags, 0);
174 if (rtnl->newgateway) {
175 const char *gateway = __connman_ipconfig_get_gateway(index);
178 rtnl->newgateway(index, gateway);
182 static GSList *rtnl_list = NULL;
184 static gint compare_priority(gconstpointer a, gconstpointer b)
186 const struct connman_rtnl *rtnl1 = a;
187 const struct connman_rtnl *rtnl2 = b;
189 return rtnl2->priority - rtnl1->priority;
193 * connman_rtnl_register:
196 * Register a new RTNL module
198 * Returns: %0 on success
200 int connman_rtnl_register(struct connman_rtnl *rtnl)
202 DBG("rtnl %p name %s", rtnl, rtnl->name);
204 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
207 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
213 * connman_rtnl_unregister:
216 * Remove a previously registered RTNL module
218 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
220 DBG("rtnl %p name %s", rtnl, rtnl->name);
222 rtnl_list = g_slist_remove(rtnl_list, rtnl);
225 static const char *operstate2str(unsigned char operstate)
228 case IF_OPER_UNKNOWN:
230 case IF_OPER_NOTPRESENT:
231 return "NOT-PRESENT";
234 case IF_OPER_LOWERLAYERDOWN:
235 return "LOWER-LAYER-DOWN";
236 case IF_OPER_TESTING:
238 case IF_OPER_DORMANT:
247 static void extract_link(struct ifinfomsg *msg, int bytes,
248 struct ether_addr *address, const char **ifname,
249 unsigned int *mtu, unsigned char *operstate,
250 struct rtnl_link_stats *stats)
254 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
255 attr = RTA_NEXT(attr, bytes)) {
256 switch (attr->rta_type) {
259 memcpy(address, RTA_DATA(attr), ETH_ALEN);
263 *ifname = RTA_DATA(attr);
267 *mtu = *((unsigned int *) RTA_DATA(attr));
271 memcpy(stats, RTA_DATA(attr),
272 sizeof(struct rtnl_link_stats));
275 if (operstate != NULL)
276 *operstate = *((unsigned char *) RTA_DATA(attr));
284 static void process_newlink(unsigned short type, int index, unsigned flags,
285 unsigned change, struct ifinfomsg *msg, int bytes)
287 struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
288 struct ether_addr compare = {{ 0, 0, 0, 0, 0, 0 }};
289 struct rtnl_link_stats stats;
290 unsigned char operstate = 0xff;
291 const char *ifname = NULL;
292 unsigned int mtu = 0;
296 memset(&stats, 0, sizeof(stats));
297 extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats);
299 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
300 address.ether_addr_octet[0],
301 address.ether_addr_octet[1],
302 address.ether_addr_octet[2],
303 address.ether_addr_octet[3],
304 address.ether_addr_octet[4],
305 address.ether_addr_octet[5]);
309 case ARPHRD_LOOPBACK:
311 __connman_ipconfig_newlink(index, type, flags,
316 if (memcmp(&address, &compare, ETH_ALEN) != 0)
317 connman_info("%s {newlink} index %d address %s mtu %u",
318 ifname, index, str, mtu);
320 if (operstate != 0xff)
321 connman_info("%s {newlink} index %d operstate %u <%s>",
322 ifname, index, operstate,
323 operstate2str(operstate));
325 for (list = rtnl_list; list; list = list->next) {
326 struct connman_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)
338 if (operstate != 0xff && watch->operstate)
339 watch->operstate(operstate, watch->user_data);
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 = watch_list; list; list = list->next) {
363 struct watch_data *watch = list->data;
365 if (watch->index != index)
368 if (operstate != 0xff && watch->operstate)
369 watch->operstate(operstate, watch->user_data);
372 for (list = rtnl_list; list; list = list->next) {
373 struct connman_rtnl *rtnl = list->data;
376 rtnl->dellink(type, index, flags, change);
381 case ARPHRD_LOOPBACK:
383 __connman_ipconfig_dellink(index, &stats);
388 static void extract_addr(struct ifaddrmsg *msg, int bytes,
390 struct in_addr *local,
391 struct in_addr *address,
392 struct in_addr *broadcast)
396 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
397 attr = RTA_NEXT(attr, bytes)) {
398 switch (attr->rta_type) {
401 *address = *((struct in_addr *) RTA_DATA(attr));
405 *local = *((struct in_addr *) RTA_DATA(attr));
408 if (broadcast != NULL)
409 *broadcast = *((struct in_addr *) RTA_DATA(attr));
413 *label = RTA_DATA(attr);
419 static void process_newaddr(unsigned char family, unsigned char prefixlen,
420 int index, struct ifaddrmsg *msg, int bytes)
422 struct in_addr address = { INADDR_ANY };
423 const char *label = NULL;
425 if (family != AF_INET)
428 extract_addr(msg, bytes, &label, &address, NULL, NULL);
430 __connman_ipconfig_newaddr(index, label,
431 prefixlen, inet_ntoa(address));
434 static void process_deladdr(unsigned char family, unsigned char prefixlen,
435 int index, struct ifaddrmsg *msg, int bytes)
437 struct in_addr address = { INADDR_ANY };
438 const char *label = NULL;
440 if (family != AF_INET)
443 extract_addr(msg, bytes, &label, &address, NULL, NULL);
445 __connman_ipconfig_deladdr(index, label,
446 prefixlen, inet_ntoa(address));
449 static void extract_route(struct rtmsg *msg, int bytes, int *index,
451 struct in_addr *gateway)
455 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
456 attr = RTA_NEXT(attr, bytes)) {
457 switch (attr->rta_type) {
460 *dst = *((struct in_addr *) RTA_DATA(attr));
464 *gateway = *((struct in_addr *) RTA_DATA(attr));
468 *index = *((int *) RTA_DATA(attr));
474 static void process_newroute(unsigned char family, unsigned char scope,
475 struct rtmsg *msg, int bytes)
478 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
479 char dststr[16], gatewaystr[16];
482 if (family != AF_INET)
485 extract_route(msg, bytes, &index, &dst, &gateway);
487 inet_ntop(family, &dst, dststr, sizeof(dststr));
488 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
490 __connman_ipconfig_newroute(index, scope, dststr, gatewaystr);
492 /* skip host specific routes */
493 if (scope != RT_SCOPE_UNIVERSE &&
494 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
497 if (dst.s_addr != INADDR_ANY)
500 for (list = rtnl_list; list; list = list->next) {
501 struct connman_rtnl *rtnl = list->data;
503 if (rtnl->newgateway)
504 rtnl->newgateway(index, gatewaystr);
508 static void process_delroute(unsigned char family, unsigned char scope,
509 struct rtmsg *msg, int bytes)
512 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
513 char dststr[16], gatewaystr[16];
516 if (family != AF_INET)
519 extract_route(msg, bytes, &index, &dst, &gateway);
521 inet_ntop(family, &dst, dststr, sizeof(dststr));
522 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
524 __connman_ipconfig_delroute(index, scope, dststr, gatewaystr);
526 /* skip host specific routes */
527 if (scope != RT_SCOPE_UNIVERSE &&
528 !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
531 if (dst.s_addr != INADDR_ANY)
534 for (list = rtnl_list; list; list = list->next) {
535 struct connman_rtnl *rtnl = list->data;
537 if (rtnl->delgateway)
538 rtnl->delgateway(index, gatewaystr);
542 static inline void print_ether(struct rtattr *attr, const char *name)
544 int len = (int) RTA_PAYLOAD(attr);
546 if (len == ETH_ALEN) {
547 struct ether_addr eth;
548 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
549 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
551 print(" attr %s (len %d)\n", name, len);
554 static inline void print_inet(struct rtattr *attr, const char *name,
555 unsigned char family)
557 int len = (int) RTA_PAYLOAD(attr);
559 if (family == AF_INET && len == sizeof(struct in_addr)) {
561 addr = *((struct in_addr *) RTA_DATA(attr));
562 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
564 print(" attr %s (len %d)\n", name, len);
567 static inline void print_string(struct rtattr *attr, const char *name)
569 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
570 (char *) RTA_DATA(attr));
573 static inline void print_byte(struct rtattr *attr, const char *name)
575 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
576 *((unsigned char *) RTA_DATA(attr)));
579 static inline void print_integer(struct rtattr *attr, const char *name)
581 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
582 *((int *) RTA_DATA(attr)));
585 static inline void print_attr(struct rtattr *attr, const char *name)
587 int len = (int) RTA_PAYLOAD(attr);
590 print(" attr %s (len %d)\n", name, len);
592 print(" attr %d (len %d)\n", attr->rta_type, len);
595 static void rtnl_link(struct nlmsghdr *hdr)
597 struct ifinfomsg *msg;
601 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
602 bytes = IFLA_PAYLOAD(hdr);
604 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
606 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
607 attr = RTA_NEXT(attr, bytes)) {
608 switch (attr->rta_type) {
610 print_ether(attr, "address");
613 print_ether(attr, "broadcast");
616 print_string(attr, "ifname");
619 print_integer(attr, "mtu");
622 print_attr(attr, "link");
625 print_attr(attr, "qdisc");
628 print_attr(attr, "stats");
631 print_attr(attr, "cost");
634 print_attr(attr, "priority");
637 print_attr(attr, "master");
640 print_attr(attr, "wireless");
643 print_attr(attr, "protinfo");
646 print_integer(attr, "txqlen");
649 print_attr(attr, "map");
652 print_attr(attr, "weight");
655 print_byte(attr, "operstate");
658 print_byte(attr, "linkmode");
661 print_attr(attr, NULL);
667 static void rtnl_newlink(struct nlmsghdr *hdr)
669 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
673 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
674 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
677 static void rtnl_dellink(struct nlmsghdr *hdr)
679 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
683 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
684 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
687 static void rtnl_addr(struct nlmsghdr *hdr)
689 struct ifaddrmsg *msg;
693 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
694 bytes = IFA_PAYLOAD(hdr);
696 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
698 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
699 attr = RTA_NEXT(attr, bytes)) {
700 switch (attr->rta_type) {
702 print_inet(attr, "address", msg->ifa_family);
705 print_inet(attr, "local", msg->ifa_family);
708 print_string(attr, "label");
711 print_inet(attr, "broadcast", msg->ifa_family);
714 print_attr(attr, "anycast");
717 print_attr(attr, "cacheinfo");
720 print_attr(attr, "multicast");
723 print_attr(attr, NULL);
729 static void rtnl_newaddr(struct nlmsghdr *hdr)
731 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
735 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
736 msg, IFA_PAYLOAD(hdr));
739 static void rtnl_deladdr(struct nlmsghdr *hdr)
741 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
745 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
746 msg, IFA_PAYLOAD(hdr));
749 static void rtnl_route(struct nlmsghdr *hdr)
755 msg = (struct rtmsg *) NLMSG_DATA(hdr);
756 bytes = RTM_PAYLOAD(hdr);
758 print("rtm_family %d rtm_table %d rtm_protocol %d",
759 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
760 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
761 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
763 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
764 attr = RTA_NEXT(attr, bytes)) {
765 switch (attr->rta_type) {
767 print_inet(attr, "dst", msg->rtm_family);
770 print_inet(attr, "src", msg->rtm_family);
773 print_string(attr, "iif");
776 print_integer(attr, "oif");
779 print_inet(attr, "gateway", msg->rtm_family);
782 print_attr(attr, "priority");
785 print_inet(attr, "prefsrc", msg->rtm_family);
788 print_attr(attr, "metrics");
791 print_integer(attr, "table");
794 print_attr(attr, NULL);
800 static connman_bool_t is_route_rtmsg(struct rtmsg *msg)
803 if (msg->rtm_table != RT_TABLE_MAIN)
806 if (msg->rtm_protocol != RTPROT_BOOT &&
807 msg->rtm_protocol != RTPROT_KERNEL)
810 if (msg->rtm_type != RTN_UNICAST)
816 static void rtnl_newroute(struct nlmsghdr *hdr)
818 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
822 if (is_route_rtmsg(msg))
823 process_newroute(msg->rtm_family, msg->rtm_scope,
824 msg, RTM_PAYLOAD(hdr));
827 static void rtnl_delroute(struct nlmsghdr *hdr)
829 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
833 if (is_route_rtmsg(msg))
834 process_delroute(msg->rtm_family, msg->rtm_scope,
835 msg, RTM_PAYLOAD(hdr));
838 static const char *type2string(uint16_t type)
870 static GIOChannel *channel = NULL;
872 struct rtnl_request {
876 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
878 static GSList *request_list = NULL;
879 static guint32 request_seq = 0;
881 static struct rtnl_request *find_request(guint32 seq)
885 for (list = request_list; list; list = list->next) {
886 struct rtnl_request *req = list->data;
888 if (req->hdr.nlmsg_seq == seq)
895 static int send_request(struct rtnl_request *req)
897 struct sockaddr_nl addr;
900 DBG("%s len %d type %d flags 0x%04x seq %d",
901 type2string(req->hdr.nlmsg_type),
902 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
903 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
905 sk = g_io_channel_unix_get_fd(channel);
907 memset(&addr, 0, sizeof(addr));
908 addr.nl_family = AF_NETLINK;
910 return sendto(sk, req, req->hdr.nlmsg_len, 0,
911 (struct sockaddr *) &addr, sizeof(addr));
914 static int queue_request(struct rtnl_request *req)
916 request_list = g_slist_append(request_list, req);
918 if (g_slist_length(request_list) > 1)
921 return send_request(req);
924 static int process_response(guint32 seq)
926 struct rtnl_request *req;
930 req = find_request(seq);
932 request_list = g_slist_remove(request_list, req);
936 req = g_slist_nth_data(request_list, 0);
940 return send_request(req);
943 static void rtnl_message(void *buf, size_t len)
945 DBG("buf %p len %zd", buf, len);
948 struct nlmsghdr *hdr = buf;
949 struct nlmsgerr *err;
951 if (!NLMSG_OK(hdr, len))
954 DBG("%s len %d type %d flags 0x%04x seq %d",
955 type2string(hdr->nlmsg_type),
956 hdr->nlmsg_len, hdr->nlmsg_type,
957 hdr->nlmsg_flags, hdr->nlmsg_seq);
959 switch (hdr->nlmsg_type) {
964 process_response(hdr->nlmsg_seq);
967 err = NLMSG_DATA(hdr);
968 DBG("error %d (%s)", -err->error,
969 strerror(-err->error));
991 len -= hdr->nlmsg_len;
992 buf += hdr->nlmsg_len;
996 static gboolean netlink_event(GIOChannel *chan,
997 GIOCondition cond, gpointer data)
999 unsigned char buf[4096];
1003 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1006 memset(buf, 0, sizeof(buf));
1008 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
1010 if (err == G_IO_ERROR_AGAIN)
1015 rtnl_message(buf, len);
1020 static int send_getlink(void)
1022 struct rtnl_request *req;
1026 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1030 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1031 req->hdr.nlmsg_type = RTM_GETLINK;
1032 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1033 req->hdr.nlmsg_pid = 0;
1034 req->hdr.nlmsg_seq = request_seq++;
1035 req->msg.rtgen_family = AF_INET;
1037 return queue_request(req);
1040 static int send_getaddr(void)
1042 struct rtnl_request *req;
1046 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1050 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1051 req->hdr.nlmsg_type = RTM_GETADDR;
1052 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1053 req->hdr.nlmsg_pid = 0;
1054 req->hdr.nlmsg_seq = request_seq++;
1055 req->msg.rtgen_family = AF_INET;
1057 return queue_request(req);
1060 static int send_getroute(void)
1062 struct rtnl_request *req;
1066 req = g_try_malloc0(RTNL_REQUEST_SIZE);
1070 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1071 req->hdr.nlmsg_type = RTM_GETROUTE;
1072 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1073 req->hdr.nlmsg_pid = 0;
1074 req->hdr.nlmsg_seq = request_seq++;
1075 req->msg.rtgen_family = AF_INET;
1077 return queue_request(req);
1080 static gboolean update_timeout_cb(gpointer user_data)
1082 __connman_rtnl_request_update();
1087 static void update_interval_callback(guint min)
1089 if (update_timeout > 0)
1090 g_source_remove(update_timeout);
1092 if (min < G_MAXUINT) {
1093 update_interval = min;
1094 update_timeout = g_timeout_add_seconds(update_interval,
1095 update_timeout_cb, NULL);
1098 update_interval = G_MAXUINT;
1102 static gint compare_interval(gconstpointer a, gconstpointer b)
1104 guint val_a = GPOINTER_TO_UINT(a);
1105 guint val_b = GPOINTER_TO_UINT(b);
1107 return val_a - val_b;
1110 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1117 update_list = g_slist_insert_sorted(update_list,
1118 GUINT_TO_POINTER(interval), compare_interval);
1120 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1121 if (min < update_interval) {
1122 update_interval_callback(min);
1123 __connman_rtnl_request_update();
1126 return update_interval;
1129 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1131 guint min = G_MAXUINT;
1136 update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1138 if (update_list != NULL)
1139 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1141 if (min > update_interval)
1142 update_interval_callback(min);
1147 int __connman_rtnl_request_update(void)
1149 return send_getlink();
1152 int __connman_rtnl_init(void)
1154 struct sockaddr_nl addr;
1159 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
1163 memset(&addr, 0, sizeof(addr));
1164 addr.nl_family = AF_NETLINK;
1165 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
1167 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1172 channel = g_io_channel_unix_new(sk);
1173 g_io_channel_set_close_on_unref(channel, TRUE);
1175 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1176 netlink_event, NULL);
1181 void __connman_rtnl_start(void)
1190 void __connman_rtnl_cleanup(void)
1196 for (list = watch_list; list; list = list->next) {
1197 struct watch_data *watch = list->data;
1199 DBG("removing watch %d", watch->id);
1205 g_slist_free(watch_list);
1208 g_slist_free(update_list);
1211 for (list = request_list; list; list = list->next) {
1212 struct rtnl_request *req = list->data;
1214 DBG("%s len %d type %d flags 0x%04x seq %d",
1215 type2string(req->hdr.nlmsg_type),
1216 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1217 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1223 g_slist_free(request_list);
1224 request_list = NULL;
1226 g_io_channel_shutdown(channel, TRUE, NULL);
1227 g_io_channel_unref(channel);