5 * Copyright (C) 2007-2009 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
28 #include <sys/socket.h>
29 #include <arpa/inet.h>
32 #include <linux/if_arp.h>
33 #include <linux/netlink.h>
34 #include <linux/rtnetlink.h>
43 connman_rtnl_link_cb_t newlink;
47 static GSList *watch_list = NULL;
48 static unsigned int watch_id = 0;
50 static GHashTable *ipconfig_hash = NULL;
51 static GSList *ipconfig_list = NULL;
53 static void register_ipconfig(struct connman_ipconfig *ipconfig)
55 ipconfig_list = g_slist_append(ipconfig_list, ipconfig);
58 static void unregister_ipconfig(struct connman_ipconfig *ipconfig)
60 ipconfig_list = g_slist_remove(ipconfig_list, ipconfig);
63 static void free_ipconfig(gpointer data)
65 struct connman_ipconfig *ipconfig = data;
67 unregister_ipconfig(ipconfig);
69 connman_ipconfig_unref(ipconfig);
73 * connman_rtnl_add_newlink_watch:
74 * @index: network device index
75 * @callback: callback function
76 * @user_data: callback data;
78 * Add a new RTNL watch for newlink events
80 * Returns: %0 on failure and a unique id on success
82 unsigned int connman_rtnl_add_newlink_watch(int index,
83 connman_rtnl_link_cb_t callback, void *user_data)
85 struct connman_ipconfig *ipconfig;
86 struct watch_data *watch;
88 watch = g_try_new0(struct watch_data, 1);
92 watch->id = ++watch_id;
95 watch->newlink = callback;
96 watch->user_data = user_data;
98 watch_list = g_slist_prepend(watch_list, watch);
100 DBG("id %d", watch->id);
102 ipconfig = g_hash_table_lookup(ipconfig_hash, GINT_TO_POINTER(index));
103 if (ipconfig != NULL) {
104 unsigned int flags = __connman_ipconfig_get_flags(ipconfig);
107 callback(flags, 0, user_data);
114 * connman_rtnl_remove_watch:
115 * @id: watch identifier
117 * Remove the RTNL watch for the identifier
119 void connman_rtnl_remove_watch(unsigned int id)
128 for (list = watch_list; list; list = list->next) {
129 struct watch_data *watch = list->data;
131 if (watch->id == id) {
132 watch_list = g_slist_remove(watch_list, watch);
139 static void trigger_newlink(gpointer key, gpointer value, gpointer user_data)
141 struct connman_rtnl *rtnl = user_data;
142 struct connman_ipconfig *ipconfig = value;
143 int index = GPOINTER_TO_INT(key);
145 if (index < 0 || ipconfig == NULL)
149 unsigned short type = __connman_ipconfig_get_type(ipconfig);
150 unsigned int flags = __connman_ipconfig_get_flags(ipconfig);
152 rtnl->newlink(type, index, flags, 0);
156 static GSList *rtnl_list = NULL;
158 static gint compare_priority(gconstpointer a, gconstpointer b)
160 const struct connman_rtnl *rtnl1 = a;
161 const struct connman_rtnl *rtnl2 = b;
163 return rtnl2->priority - rtnl1->priority;
167 * connman_rtnl_register:
170 * Register a new RTNL module
172 * Returns: %0 on success
174 int connman_rtnl_register(struct connman_rtnl *rtnl)
176 DBG("rtnl %p name %s", rtnl, rtnl->name);
178 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
181 g_hash_table_foreach(ipconfig_hash, trigger_newlink, rtnl);
187 * connman_rtnl_unregister:
190 * Remove a previously registered RTNL module
192 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
194 DBG("rtnl %p name %s", rtnl, rtnl->name);
196 rtnl_list = g_slist_remove(rtnl_list, rtnl);
199 static void process_newlink(unsigned short type, int index,
200 unsigned flags, unsigned change)
202 struct connman_ipconfig *ipconfig;
207 case ARPHRD_LOOPBACK:
209 ipconfig = g_hash_table_lookup(ipconfig_hash,
210 GINT_TO_POINTER(index));
211 if (ipconfig == NULL) {
212 ipconfig = connman_ipconfig_create(index);
213 if (ipconfig != NULL) {
214 g_hash_table_insert(ipconfig_hash,
215 GINT_TO_POINTER(index), ipconfig);
217 register_ipconfig(ipconfig);
221 if (ipconfig != NULL)
222 __connman_ipconfig_update_link(ipconfig,
227 for (list = rtnl_list; list; list = list->next) {
228 struct connman_rtnl *rtnl = list->data;
231 rtnl->newlink(type, index, flags, change);
234 for (list = watch_list; list; list = list->next) {
235 struct watch_data *watch = list->data;
237 if (watch->index != index)
241 watch->newlink(flags, change, watch->user_data);
245 static void process_dellink(unsigned short type, int index,
246 unsigned flags, unsigned change)
250 for (list = rtnl_list; list; list = list->next) {
251 struct connman_rtnl *rtnl = list->data;
254 rtnl->dellink(type, index, flags, change);
259 case ARPHRD_LOOPBACK:
261 g_hash_table_remove(ipconfig_hash, GINT_TO_POINTER(index));
266 static char *extract_gateway(struct rtmsg *msg, int bytes, int *index)
268 char *gateway = NULL;
272 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
273 attr = RTA_NEXT(attr, bytes)) {
274 switch (attr->rta_type) {
276 addr = *((struct in_addr *) RTA_DATA(attr));
278 gateway = g_strdup(inet_ntoa(addr));
281 *index = *((int *) RTA_DATA(attr));
289 static void process_newgateway(struct rtmsg *msg, int bytes)
295 gateway = extract_gateway(msg, bytes, &index);
296 if (gateway == NULL || index < 0)
299 for (list = rtnl_list; list; list = list->next) {
300 struct connman_rtnl *rtnl = list->data;
302 if (rtnl->newgateway)
303 rtnl->newgateway(index, gateway);
309 static void process_delgateway(struct rtmsg *msg, int bytes)
315 gateway = extract_gateway(msg, bytes, &index);
316 if (gateway == NULL || index < 0)
319 for (list = rtnl_list; list; list = list->next) {
320 struct connman_rtnl *rtnl = list->data;
322 if (rtnl->delgateway)
323 rtnl->delgateway(index, gateway);
329 static void extract_addr(struct ifaddrmsg *msg, int bytes,
331 struct in_addr *local,
332 struct in_addr *address,
333 struct in_addr *broadcast)
337 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
338 attr = RTA_NEXT(attr, bytes)) {
339 switch (attr->rta_type) {
342 *address = *((struct in_addr *) RTA_DATA(attr));
346 *local = *((struct in_addr *) RTA_DATA(attr));
349 if (broadcast != NULL)
350 *broadcast = *((struct in_addr *) RTA_DATA(attr));
354 *label = RTA_DATA(attr);
360 static void process_newaddr(int family, int prefixlen, int index,
361 struct ifaddrmsg *msg, int bytes)
365 struct in_addr address;
367 if (family != AF_INET)
370 for (list = ipconfig_list; list; list = list->next) {
371 struct connman_ipconfig *ipconfig = list->data;
373 if (__connman_ipconfig_get_index(ipconfig) != index)
376 extract_addr(msg, bytes, &label, &address, NULL, NULL);
377 __connman_ipconfig_add_address(ipconfig, label, prefixlen,
378 inet_ntoa(address), NULL);
382 static void process_deladdr(int family, int prefixlen, int index,
383 struct ifaddrmsg *msg, int bytes)
387 struct in_addr address;
389 if (family != AF_INET)
392 for (list = ipconfig_list; list; list = list->next) {
393 struct connman_ipconfig *ipconfig = list->data;
395 if (__connman_ipconfig_get_index(ipconfig) != index)
398 extract_addr(msg, bytes, &label, &address, NULL, NULL);
399 __connman_ipconfig_del_address(ipconfig, label, prefixlen,
400 inet_ntoa(address), NULL);
404 static inline void print_inet(struct rtattr *attr, const char *name, int family)
406 if (family == AF_INET) {
408 addr = *((struct in_addr *) RTA_DATA(attr));
409 DBG(" attr %s (len %d) %s\n", name,
410 (int) RTA_PAYLOAD(attr), inet_ntoa(addr));
412 DBG(" attr %s (len %d)\n", name, (int) RTA_PAYLOAD(attr));
415 static inline void print_char(struct rtattr *attr, const char *name)
417 DBG(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
418 (char *) RTA_DATA(attr));
421 static inline void print_byte(struct rtattr *attr, const char *name)
423 DBG(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
424 *((unsigned char *) RTA_DATA(attr)));
427 static inline void print_attr(struct rtattr *attr, const char *name)
430 DBG(" attr %s (len %d)\n", name, (int) RTA_PAYLOAD(attr));
432 DBG(" attr %d (len %d)\n",
433 attr->rta_type, (int) RTA_PAYLOAD(attr));
436 static void rtnl_link(struct nlmsghdr *hdr)
439 struct ifinfomsg *msg;
443 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
444 bytes = IFLA_PAYLOAD(hdr);
446 DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
448 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
449 attr = RTA_NEXT(attr, bytes)) {
450 switch (attr->rta_type) {
452 print_attr(attr, "address");
455 print_attr(attr, "broadcast");
458 print_char(attr, "ifname");
461 print_attr(attr, "mtu");
464 print_attr(attr, "link");
467 print_attr(attr, "qdisc");
470 print_attr(attr, "stats");
473 print_attr(attr, "cost");
476 print_attr(attr, "priority");
479 print_attr(attr, "master");
482 print_attr(attr, "wireless");
485 print_attr(attr, "protinfo");
488 print_attr(attr, "txqlen");
491 print_attr(attr, "map");
494 print_attr(attr, "weight");
497 print_byte(attr, "operstate");
500 print_byte(attr, "linkmode");
503 print_attr(attr, NULL);
510 static void rtnl_newlink(struct nlmsghdr *hdr)
512 struct ifinfomsg *msg;
514 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
516 DBG("ifi_type %d ifi_index %d ifi_flags 0x%04x ifi_change 0x%04x",
517 msg->ifi_type, msg->ifi_index,
518 msg->ifi_flags, msg->ifi_change);
520 process_newlink(msg->ifi_type, msg->ifi_index,
521 msg->ifi_flags, msg->ifi_change);
526 static void rtnl_dellink(struct nlmsghdr *hdr)
528 struct ifinfomsg *msg;
530 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
532 DBG("ifi_type %d ifi_index %d ifi_flags 0x%04x ifi_change 0x%04x",
533 msg->ifi_type, msg->ifi_index,
534 msg->ifi_flags, msg->ifi_change);
536 process_dellink(msg->ifi_type, msg->ifi_index,
537 msg->ifi_flags, msg->ifi_change);
542 static void rtnl_addr(struct nlmsghdr *hdr)
545 struct ifaddrmsg *msg;
549 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
550 bytes = IFA_PAYLOAD(hdr);
552 DBG("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
554 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
555 attr = RTA_NEXT(attr, bytes)) {
556 switch (attr->rta_type) {
558 print_inet(attr, "address", msg->ifa_family);
561 print_inet(attr, "local", msg->ifa_family);
564 print_char(attr, "label");
567 print_inet(attr, "broadcast", msg->ifa_family);
570 print_attr(attr, "anycast");
573 print_attr(attr, "cacheinfo");
576 print_attr(attr, "multicast");
579 print_attr(attr, NULL);
586 static void rtnl_newaddr(struct nlmsghdr *hdr)
588 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
590 DBG("ifa_family %d ifa_prefixlen %d ifa_index %d",
591 msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index);
593 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
594 msg, IFA_PAYLOAD(hdr));
599 static void rtnl_deladdr(struct nlmsghdr *hdr)
601 struct ifaddrmsg *msg;
603 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
605 DBG("ifa_family %d ifa_prefixlen %d ifa_index %d",
606 msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index);
608 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
609 msg, IFA_PAYLOAD(hdr));
614 static void rtnl_route(struct nlmsghdr *hdr)
621 msg = (struct rtmsg *) NLMSG_DATA(hdr);
622 bytes = RTM_PAYLOAD(hdr);
624 DBG("rtm_family %d rtm_flags 0x%04x", msg->rtm_family, msg->rtm_flags);
626 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
627 attr = RTA_NEXT(attr, bytes)) {
628 switch (attr->rta_type) {
630 print_inet(attr, "dst", msg->rtm_family);
633 print_inet(attr, "src", msg->rtm_family);
636 print_char(attr, "iif");
639 print_attr(attr, "oif");
642 print_inet(attr, "gateway", msg->rtm_family);
645 print_attr(attr, "priority");
648 print_inet(attr, "prefsrc", msg->rtm_family);
651 print_attr(attr, "metrics");
654 print_attr(attr, "table");
657 print_attr(attr, NULL);
664 static void rtnl_newroute(struct nlmsghdr *hdr)
666 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
668 if (msg->rtm_type == RTN_UNICAST && msg->rtm_table == RT_TABLE_MAIN &&
669 msg->rtm_scope == RT_SCOPE_UNIVERSE) {
670 DBG("rtm_table %d rtm_scope %d rtm_type %d rtm_flags 0x%04x",
671 msg->rtm_table, msg->rtm_scope,
672 msg->rtm_type, msg->rtm_flags);
673 process_newgateway(msg, RTM_PAYLOAD(hdr));
679 static void rtnl_delroute(struct nlmsghdr *hdr)
681 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
683 if (msg->rtm_type == RTN_UNICAST && msg->rtm_table == RT_TABLE_MAIN &&
684 msg->rtm_scope == RT_SCOPE_UNIVERSE) {
685 DBG("rtm_table %d rtm_scope %d rtm_type %d rtm_flags 0x%04x",
686 msg->rtm_table, msg->rtm_scope,
687 msg->rtm_type, msg->rtm_flags);
688 process_delgateway(msg, RTM_PAYLOAD(hdr));
694 static const char *type2string(uint16_t type)
726 static GIOChannel *channel = NULL;
728 struct rtnl_request {
732 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
734 static GSList *request_list = NULL;
735 static guint32 request_seq = 0;
737 static struct rtnl_request *find_request(guint32 seq)
741 for (list = request_list; list; list = list->next) {
742 struct rtnl_request *req = list->data;
744 if (req->hdr.nlmsg_seq == seq)
751 static int send_request(struct rtnl_request *req)
753 struct sockaddr_nl addr;
756 DBG("%s len %d type %d flags 0x%04x seq %d",
757 type2string(req->hdr.nlmsg_type),
758 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
759 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
761 sk = g_io_channel_unix_get_fd(channel);
763 memset(&addr, 0, sizeof(addr));
764 addr.nl_family = AF_NETLINK;
766 return sendto(sk, req, req->hdr.nlmsg_len, 0,
767 (struct sockaddr *) &addr, sizeof(addr));
770 static int queue_request(struct rtnl_request *req)
772 request_list = g_slist_append(request_list, req);
774 if (g_slist_length(request_list) > 1)
777 return send_request(req);
780 static int process_response(guint32 seq)
782 struct rtnl_request *req;
786 req = find_request(seq);
788 request_list = g_slist_remove(request_list, req);
792 req = g_slist_nth_data(request_list, 0);
796 return send_request(req);
799 static void rtnl_message(void *buf, size_t len)
801 DBG("buf %p len %zd", buf, len);
804 struct nlmsghdr *hdr = buf;
805 struct nlmsgerr *err;
807 if (!NLMSG_OK(hdr, len))
810 DBG("%s len %d type %d flags 0x%04x seq %d",
811 type2string(hdr->nlmsg_type),
812 hdr->nlmsg_len, hdr->nlmsg_type,
813 hdr->nlmsg_flags, hdr->nlmsg_seq);
815 switch (hdr->nlmsg_type) {
820 process_response(hdr->nlmsg_seq);
823 err = NLMSG_DATA(hdr);
824 DBG("error %d (%s)", -err->error,
825 strerror(-err->error));
847 len -= hdr->nlmsg_len;
848 buf += hdr->nlmsg_len;
852 static gboolean netlink_event(GIOChannel *chan,
853 GIOCondition cond, gpointer data)
855 unsigned char buf[4096];
859 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
862 memset(buf, 0, sizeof(buf));
864 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
866 if (err == G_IO_ERROR_AGAIN)
871 rtnl_message(buf, len);
876 static int send_getlink(void)
878 struct rtnl_request *req;
882 req = g_try_malloc0(RTNL_REQUEST_SIZE);
886 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
887 req->hdr.nlmsg_type = RTM_GETLINK;
888 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
889 req->hdr.nlmsg_pid = 0;
890 req->hdr.nlmsg_seq = request_seq++;
891 req->msg.rtgen_family = AF_INET;
893 return queue_request(req);
896 static int send_getaddr(void)
898 struct rtnl_request *req;
902 req = g_try_malloc0(RTNL_REQUEST_SIZE);
906 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
907 req->hdr.nlmsg_type = RTM_GETADDR;
908 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
909 req->hdr.nlmsg_pid = 0;
910 req->hdr.nlmsg_seq = request_seq++;
911 req->msg.rtgen_family = AF_INET;
913 return queue_request(req);
916 int connman_rtnl_send_getroute(void)
918 struct rtnl_request *req;
922 req = g_try_malloc0(RTNL_REQUEST_SIZE);
926 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
927 req->hdr.nlmsg_type = RTM_GETROUTE;
928 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
929 req->hdr.nlmsg_pid = 0;
930 req->hdr.nlmsg_seq = request_seq++;
931 req->msg.rtgen_family = AF_INET;
933 return queue_request(req);
936 int __connman_rtnl_init(void)
938 struct sockaddr_nl addr;
943 ipconfig_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
944 NULL, free_ipconfig);
946 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
950 memset(&addr, 0, sizeof(addr));
951 addr.nl_family = AF_NETLINK;
952 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
954 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
959 channel = g_io_channel_unix_new(sk);
960 g_io_channel_set_close_on_unref(channel, TRUE);
962 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
963 netlink_event, NULL);
968 void __connman_rtnl_start(void)
974 connman_rtnl_send_getroute();
977 void __connman_rtnl_cleanup(void)
983 g_slist_free(ipconfig_list);
984 ipconfig_list = NULL;
986 g_hash_table_destroy(ipconfig_hash);
987 ipconfig_hash = NULL;
989 for (list = watch_list; list; list = list->next) {
990 struct watch_data *watch = list->data;
992 DBG("removing watch %d", watch->id);
998 g_slist_free(watch_list);
1001 for (list = request_list; list; list = list->next) {
1002 struct rtnl_request *req = list->data;
1004 DBG("%s len %d type %d flags 0x%04x seq %d",
1005 type2string(req->hdr.nlmsg_type),
1006 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1007 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1013 g_slist_free(request_list);
1014 request_list = NULL;
1016 g_io_channel_shutdown(channel, TRUE, NULL);
1017 g_io_channel_unref(channel);