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 = (struct ifinfomsg *) NLMSG_DATA(hdr);
514 DBG("ifi_type %d ifi_index %d ifi_flags 0x%04x ifi_change 0x%04x",
515 msg->ifi_type, msg->ifi_index,
516 msg->ifi_flags, msg->ifi_change);
518 process_newlink(msg->ifi_type, msg->ifi_index,
519 msg->ifi_flags, msg->ifi_change);
524 static void rtnl_dellink(struct nlmsghdr *hdr)
526 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
528 DBG("ifi_type %d ifi_index %d ifi_flags 0x%04x ifi_change 0x%04x",
529 msg->ifi_type, msg->ifi_index,
530 msg->ifi_flags, msg->ifi_change);
532 process_dellink(msg->ifi_type, msg->ifi_index,
533 msg->ifi_flags, msg->ifi_change);
538 static void rtnl_addr(struct nlmsghdr *hdr)
541 struct ifaddrmsg *msg;
545 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
546 bytes = IFA_PAYLOAD(hdr);
548 DBG("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
550 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
551 attr = RTA_NEXT(attr, bytes)) {
552 switch (attr->rta_type) {
554 print_inet(attr, "address", msg->ifa_family);
557 print_inet(attr, "local", msg->ifa_family);
560 print_char(attr, "label");
563 print_inet(attr, "broadcast", msg->ifa_family);
566 print_attr(attr, "anycast");
569 print_attr(attr, "cacheinfo");
572 print_attr(attr, "multicast");
575 print_attr(attr, NULL);
582 static void rtnl_newaddr(struct nlmsghdr *hdr)
584 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
586 DBG("ifa_family %d ifa_prefixlen %d ifa_index %d",
587 msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index);
589 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
590 msg, IFA_PAYLOAD(hdr));
595 static void rtnl_deladdr(struct nlmsghdr *hdr)
597 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
599 DBG("ifa_family %d ifa_prefixlen %d ifa_index %d",
600 msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index);
602 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
603 msg, IFA_PAYLOAD(hdr));
608 static void rtnl_route(struct nlmsghdr *hdr)
615 msg = (struct rtmsg *) NLMSG_DATA(hdr);
616 bytes = RTM_PAYLOAD(hdr);
618 DBG("rtm_family %d rtm_flags 0x%04x", msg->rtm_family, msg->rtm_flags);
620 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
621 attr = RTA_NEXT(attr, bytes)) {
622 switch (attr->rta_type) {
624 print_inet(attr, "dst", msg->rtm_family);
627 print_inet(attr, "src", msg->rtm_family);
630 print_char(attr, "iif");
633 print_attr(attr, "oif");
636 print_inet(attr, "gateway", msg->rtm_family);
639 print_attr(attr, "priority");
642 print_inet(attr, "prefsrc", msg->rtm_family);
645 print_attr(attr, "metrics");
648 print_attr(attr, "table");
651 print_attr(attr, NULL);
658 static void rtnl_newroute(struct nlmsghdr *hdr)
660 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
662 if (msg->rtm_type == RTN_UNICAST && msg->rtm_table == RT_TABLE_MAIN &&
663 msg->rtm_scope == RT_SCOPE_UNIVERSE) {
664 DBG("rtm_table %d rtm_scope %d rtm_type %d rtm_flags 0x%04x",
665 msg->rtm_table, msg->rtm_scope,
666 msg->rtm_type, msg->rtm_flags);
667 process_newgateway(msg, RTM_PAYLOAD(hdr));
673 static void rtnl_delroute(struct nlmsghdr *hdr)
675 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
677 if (msg->rtm_type == RTN_UNICAST && msg->rtm_table == RT_TABLE_MAIN &&
678 msg->rtm_scope == RT_SCOPE_UNIVERSE) {
679 DBG("rtm_table %d rtm_scope %d rtm_type %d rtm_flags 0x%04x",
680 msg->rtm_table, msg->rtm_scope,
681 msg->rtm_type, msg->rtm_flags);
682 process_delgateway(msg, RTM_PAYLOAD(hdr));
688 static const char *type2string(uint16_t type)
720 static GIOChannel *channel = NULL;
722 struct rtnl_request {
726 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
728 static GSList *request_list = NULL;
729 static guint32 request_seq = 0;
731 static struct rtnl_request *find_request(guint32 seq)
735 for (list = request_list; list; list = list->next) {
736 struct rtnl_request *req = list->data;
738 if (req->hdr.nlmsg_seq == seq)
745 static int send_request(struct rtnl_request *req)
747 struct sockaddr_nl addr;
750 DBG("%s len %d type %d flags 0x%04x seq %d",
751 type2string(req->hdr.nlmsg_type),
752 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
753 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
755 sk = g_io_channel_unix_get_fd(channel);
757 memset(&addr, 0, sizeof(addr));
758 addr.nl_family = AF_NETLINK;
760 return sendto(sk, req, req->hdr.nlmsg_len, 0,
761 (struct sockaddr *) &addr, sizeof(addr));
764 static int queue_request(struct rtnl_request *req)
766 request_list = g_slist_append(request_list, req);
768 if (g_slist_length(request_list) > 1)
771 return send_request(req);
774 static int process_response(guint32 seq)
776 struct rtnl_request *req;
780 req = find_request(seq);
782 request_list = g_slist_remove(request_list, req);
786 req = g_slist_nth_data(request_list, 0);
790 return send_request(req);
793 static void rtnl_message(void *buf, size_t len)
795 DBG("buf %p len %zd", buf, len);
798 struct nlmsghdr *hdr = buf;
799 struct nlmsgerr *err;
801 if (!NLMSG_OK(hdr, len))
804 DBG("%s len %d type %d flags 0x%04x seq %d",
805 type2string(hdr->nlmsg_type),
806 hdr->nlmsg_len, hdr->nlmsg_type,
807 hdr->nlmsg_flags, hdr->nlmsg_seq);
809 switch (hdr->nlmsg_type) {
814 process_response(hdr->nlmsg_seq);
817 err = NLMSG_DATA(hdr);
818 DBG("error %d (%s)", -err->error,
819 strerror(-err->error));
841 len -= hdr->nlmsg_len;
842 buf += hdr->nlmsg_len;
846 static gboolean netlink_event(GIOChannel *chan,
847 GIOCondition cond, gpointer data)
849 unsigned char buf[4096];
853 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
856 memset(buf, 0, sizeof(buf));
858 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
860 if (err == G_IO_ERROR_AGAIN)
865 rtnl_message(buf, len);
870 static int send_getlink(void)
872 struct rtnl_request *req;
876 req = g_try_malloc0(RTNL_REQUEST_SIZE);
880 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
881 req->hdr.nlmsg_type = RTM_GETLINK;
882 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
883 req->hdr.nlmsg_pid = 0;
884 req->hdr.nlmsg_seq = request_seq++;
885 req->msg.rtgen_family = AF_INET;
887 return queue_request(req);
890 static int send_getaddr(void)
892 struct rtnl_request *req;
896 req = g_try_malloc0(RTNL_REQUEST_SIZE);
900 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
901 req->hdr.nlmsg_type = RTM_GETADDR;
902 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
903 req->hdr.nlmsg_pid = 0;
904 req->hdr.nlmsg_seq = request_seq++;
905 req->msg.rtgen_family = AF_INET;
907 return queue_request(req);
910 int connman_rtnl_send_getroute(void)
912 struct rtnl_request *req;
916 req = g_try_malloc0(RTNL_REQUEST_SIZE);
920 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
921 req->hdr.nlmsg_type = RTM_GETROUTE;
922 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
923 req->hdr.nlmsg_pid = 0;
924 req->hdr.nlmsg_seq = request_seq++;
925 req->msg.rtgen_family = AF_INET;
927 return queue_request(req);
930 int __connman_rtnl_init(void)
932 struct sockaddr_nl addr;
937 ipconfig_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
938 NULL, free_ipconfig);
940 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
944 memset(&addr, 0, sizeof(addr));
945 addr.nl_family = AF_NETLINK;
946 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
948 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
953 channel = g_io_channel_unix_new(sk);
954 g_io_channel_set_close_on_unref(channel, TRUE);
956 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
957 netlink_event, NULL);
962 void __connman_rtnl_start(void)
968 connman_rtnl_send_getroute();
971 void __connman_rtnl_cleanup(void)
977 g_slist_free(ipconfig_list);
978 ipconfig_list = NULL;
980 g_hash_table_destroy(ipconfig_hash);
981 ipconfig_hash = NULL;
983 for (list = watch_list; list; list = list->next) {
984 struct watch_data *watch = list->data;
986 DBG("removing watch %d", watch->id);
992 g_slist_free(watch_list);
995 for (list = request_list; list; list = list->next) {
996 struct rtnl_request *req = list->data;
998 DBG("%s len %d type %d flags 0x%04x seq %d",
999 type2string(req->hdr.nlmsg_type),
1000 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1001 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1007 g_slist_free(request_list);
1008 request_list = NULL;
1010 g_io_channel_shutdown(channel, TRUE, NULL);
1011 g_io_channel_unref(channel);