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>
40 #define print(arg...) do { } while (0)
41 //#define print(arg...) connman_info(arg)
46 connman_rtnl_link_cb_t newlink;
50 static GSList *watch_list = NULL;
51 static unsigned int watch_id = 0;
53 static GHashTable *ipconfig_hash = NULL;
54 static GSList *ipconfig_list = NULL;
56 static void register_ipconfig(struct connman_ipconfig *ipconfig)
58 ipconfig_list = g_slist_append(ipconfig_list, ipconfig);
61 static void unregister_ipconfig(struct connman_ipconfig *ipconfig)
63 ipconfig_list = g_slist_remove(ipconfig_list, ipconfig);
66 static void free_ipconfig(gpointer data)
68 struct connman_ipconfig *ipconfig = data;
70 unregister_ipconfig(ipconfig);
72 connman_ipconfig_unref(ipconfig);
76 * connman_rtnl_add_newlink_watch:
77 * @index: network device index
78 * @callback: callback function
79 * @user_data: callback data;
81 * Add a new RTNL watch for newlink events
83 * Returns: %0 on failure and a unique id on success
85 unsigned int connman_rtnl_add_newlink_watch(int index,
86 connman_rtnl_link_cb_t callback, void *user_data)
88 struct connman_ipconfig *ipconfig;
89 struct watch_data *watch;
91 watch = g_try_new0(struct watch_data, 1);
95 watch->id = ++watch_id;
98 watch->newlink = callback;
99 watch->user_data = user_data;
101 watch_list = g_slist_prepend(watch_list, watch);
103 DBG("id %d", watch->id);
105 ipconfig = g_hash_table_lookup(ipconfig_hash, GINT_TO_POINTER(index));
106 if (ipconfig != NULL) {
107 unsigned int flags = __connman_ipconfig_get_flags(ipconfig);
110 callback(flags, 0, user_data);
117 * connman_rtnl_remove_watch:
118 * @id: watch identifier
120 * Remove the RTNL watch for the identifier
122 void connman_rtnl_remove_watch(unsigned int id)
131 for (list = watch_list; list; list = list->next) {
132 struct watch_data *watch = list->data;
134 if (watch->id == id) {
135 watch_list = g_slist_remove(watch_list, watch);
142 static void trigger_newlink(gpointer key, gpointer value, gpointer user_data)
144 struct connman_rtnl *rtnl = user_data;
145 struct connman_ipconfig *ipconfig = value;
146 int index = GPOINTER_TO_INT(key);
148 if (index < 0 || ipconfig == NULL)
152 unsigned short type = __connman_ipconfig_get_type(ipconfig);
153 unsigned int flags = __connman_ipconfig_get_flags(ipconfig);
155 rtnl->newlink(type, index, flags, 0);
159 static GSList *rtnl_list = NULL;
161 static gint compare_priority(gconstpointer a, gconstpointer b)
163 const struct connman_rtnl *rtnl1 = a;
164 const struct connman_rtnl *rtnl2 = b;
166 return rtnl2->priority - rtnl1->priority;
170 * connman_rtnl_register:
173 * Register a new RTNL module
175 * Returns: %0 on success
177 int connman_rtnl_register(struct connman_rtnl *rtnl)
179 DBG("rtnl %p name %s", rtnl, rtnl->name);
181 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
184 g_hash_table_foreach(ipconfig_hash, trigger_newlink, rtnl);
190 * connman_rtnl_unregister:
193 * Remove a previously registered RTNL module
195 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
197 DBG("rtnl %p name %s", rtnl, rtnl->name);
199 rtnl_list = g_slist_remove(rtnl_list, rtnl);
202 static void process_newlink(unsigned short type, int index,
203 unsigned flags, unsigned change)
205 struct connman_ipconfig *ipconfig;
210 case ARPHRD_LOOPBACK:
212 ipconfig = g_hash_table_lookup(ipconfig_hash,
213 GINT_TO_POINTER(index));
214 if (ipconfig == NULL) {
215 ipconfig = connman_ipconfig_create(index);
216 if (ipconfig != NULL) {
217 g_hash_table_insert(ipconfig_hash,
218 GINT_TO_POINTER(index), ipconfig);
220 register_ipconfig(ipconfig);
224 if (ipconfig != NULL)
225 __connman_ipconfig_update_link(ipconfig,
230 for (list = rtnl_list; list; list = list->next) {
231 struct connman_rtnl *rtnl = list->data;
234 rtnl->newlink(type, index, flags, change);
237 for (list = watch_list; list; list = list->next) {
238 struct watch_data *watch = list->data;
240 if (watch->index != index)
244 watch->newlink(flags, change, watch->user_data);
248 static void process_dellink(unsigned short type, int index,
249 unsigned flags, unsigned change)
253 for (list = rtnl_list; list; list = list->next) {
254 struct connman_rtnl *rtnl = list->data;
257 rtnl->dellink(type, index, flags, change);
262 case ARPHRD_LOOPBACK:
264 g_hash_table_remove(ipconfig_hash, GINT_TO_POINTER(index));
269 static void extract_addr(struct ifaddrmsg *msg, int bytes,
271 struct in_addr *local,
272 struct in_addr *address,
273 struct in_addr *broadcast)
277 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
278 attr = RTA_NEXT(attr, bytes)) {
279 switch (attr->rta_type) {
282 *address = *((struct in_addr *) RTA_DATA(attr));
286 *local = *((struct in_addr *) RTA_DATA(attr));
289 if (broadcast != NULL)
290 *broadcast = *((struct in_addr *) RTA_DATA(attr));
294 *label = RTA_DATA(attr);
300 static void process_newaddr(unsigned char family, unsigned char prefixlen,
301 int index, struct ifaddrmsg *msg, int bytes)
305 struct in_addr address = { INADDR_ANY };
307 if (family != AF_INET)
310 extract_addr(msg, bytes, &label, &address, NULL, NULL);
312 for (list = ipconfig_list; list; list = list->next) {
313 struct connman_ipconfig *ipconfig = list->data;
315 if (__connman_ipconfig_get_index(ipconfig) != index)
318 __connman_ipconfig_add_address(ipconfig, label, prefixlen,
319 inet_ntoa(address), NULL);
323 static void process_deladdr(unsigned char family, unsigned char prefixlen,
324 int index, struct ifaddrmsg *msg, int bytes)
328 struct in_addr address = { INADDR_ANY };
330 if (family != AF_INET)
333 extract_addr(msg, bytes, &label, &address, NULL, NULL);
335 for (list = ipconfig_list; list; list = list->next) {
336 struct connman_ipconfig *ipconfig = list->data;
338 if (__connman_ipconfig_get_index(ipconfig) != index)
341 __connman_ipconfig_del_address(ipconfig, label, prefixlen,
342 inet_ntoa(address), NULL);
346 static void extract_route(struct rtmsg *msg, int bytes, int *index,
348 struct in_addr *gateway)
352 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
353 attr = RTA_NEXT(attr, bytes)) {
354 switch (attr->rta_type) {
357 *dst = *((struct in_addr *) RTA_DATA(attr));
361 *gateway = *((struct in_addr *) RTA_DATA(attr));
365 *index = *((int *) RTA_DATA(attr));
371 static void process_newroute(unsigned char family, unsigned char scope,
372 struct rtmsg *msg, int bytes)
375 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
376 char dststr[16], gatewaystr[16];
379 if (family != AF_INET)
382 extract_route(msg, bytes, &index, &dst, &gateway);
384 inet_ntop(family, &dst, dststr, sizeof(dststr));
385 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
387 for (list = ipconfig_list; list; list = list->next) {
388 struct connman_ipconfig *ipconfig = list->data;
390 if (__connman_ipconfig_get_index(ipconfig) != index)
393 __connman_ipconfig_add_route(ipconfig, scope,
398 static void process_delroute(unsigned char family, unsigned char scope,
399 struct rtmsg *msg, int bytes)
402 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
403 char dststr[16], gatewaystr[16];
406 if (family != AF_INET)
409 extract_route(msg, bytes, &index, &dst, &gateway);
411 inet_ntop(family, &dst, dststr, sizeof(dststr));
412 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
414 for (list = ipconfig_list; list; list = list->next) {
415 struct connman_ipconfig *ipconfig = list->data;
417 if (__connman_ipconfig_get_index(ipconfig) != index)
420 __connman_ipconfig_del_route(ipconfig, scope,
425 static void process_newgateway(struct rtmsg *msg, int bytes)
431 extract_route(msg, bytes, &index, NULL, &addr);
433 for (list = rtnl_list; list; list = list->next) {
434 struct connman_rtnl *rtnl = list->data;
436 if (rtnl->newgateway)
437 rtnl->newgateway(index, inet_ntoa(addr));
441 static void process_delgateway(struct rtmsg *msg, int bytes)
447 extract_route(msg, bytes, &index, NULL, &addr);
449 for (list = rtnl_list; list; list = list->next) {
450 struct connman_rtnl *rtnl = list->data;
452 if (rtnl->delgateway)
453 rtnl->delgateway(index, inet_ntoa(addr));
457 static inline void print_inet(struct rtattr *attr, const char *name,
458 unsigned char family)
460 if (family == AF_INET) {
462 addr = *((struct in_addr *) RTA_DATA(attr));
463 print(" attr %s (len %d) %s\n", name,
464 (int) RTA_PAYLOAD(attr), inet_ntoa(addr));
466 print(" attr %s (len %d)\n", name, (int) RTA_PAYLOAD(attr));
469 static inline void print_string(struct rtattr *attr, const char *name)
471 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
472 (char *) RTA_DATA(attr));
475 static inline void print_byte(struct rtattr *attr, const char *name)
477 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
478 *((unsigned char *) RTA_DATA(attr)));
481 static inline void print_integer(struct rtattr *attr, const char *name)
483 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
484 *((int *) RTA_DATA(attr)));
487 static inline void print_attr(struct rtattr *attr, const char *name)
490 print(" attr %s (len %d)\n", name, (int) RTA_PAYLOAD(attr));
492 print(" attr %d (len %d)\n",
493 attr->rta_type, (int) RTA_PAYLOAD(attr));
496 static void rtnl_link(struct nlmsghdr *hdr)
498 struct ifinfomsg *msg;
502 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
503 bytes = IFLA_PAYLOAD(hdr);
505 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
507 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
508 attr = RTA_NEXT(attr, bytes)) {
509 switch (attr->rta_type) {
511 print_attr(attr, "address");
514 print_attr(attr, "broadcast");
517 print_string(attr, "ifname");
520 print_integer(attr, "mtu");
523 print_attr(attr, "link");
526 print_attr(attr, "qdisc");
529 print_attr(attr, "stats");
532 print_attr(attr, "cost");
535 print_attr(attr, "priority");
538 print_attr(attr, "master");
541 print_attr(attr, "wireless");
544 print_attr(attr, "protinfo");
547 print_attr(attr, "txqlen");
550 print_attr(attr, "map");
553 print_attr(attr, "weight");
556 print_byte(attr, "operstate");
559 print_byte(attr, "linkmode");
562 print_attr(attr, NULL);
568 static void rtnl_newlink(struct nlmsghdr *hdr)
570 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
574 process_newlink(msg->ifi_type, msg->ifi_index,
575 msg->ifi_flags, msg->ifi_change);
578 static void rtnl_dellink(struct nlmsghdr *hdr)
580 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
584 process_dellink(msg->ifi_type, msg->ifi_index,
585 msg->ifi_flags, msg->ifi_change);
588 static void rtnl_addr(struct nlmsghdr *hdr)
590 struct ifaddrmsg *msg;
594 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
595 bytes = IFA_PAYLOAD(hdr);
597 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
599 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
600 attr = RTA_NEXT(attr, bytes)) {
601 switch (attr->rta_type) {
603 print_inet(attr, "address", msg->ifa_family);
606 print_inet(attr, "local", msg->ifa_family);
609 print_string(attr, "label");
612 print_inet(attr, "broadcast", msg->ifa_family);
615 print_attr(attr, "anycast");
618 print_attr(attr, "cacheinfo");
621 print_attr(attr, "multicast");
624 print_attr(attr, NULL);
630 static void rtnl_newaddr(struct nlmsghdr *hdr)
632 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
636 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
637 msg, IFA_PAYLOAD(hdr));
640 static void rtnl_deladdr(struct nlmsghdr *hdr)
642 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
646 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
647 msg, IFA_PAYLOAD(hdr));
650 static void rtnl_route(struct nlmsghdr *hdr)
656 msg = (struct rtmsg *) NLMSG_DATA(hdr);
657 bytes = RTM_PAYLOAD(hdr);
659 print("rtm_family %d rtm_table %d rtm_protocol %d",
660 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
661 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
662 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
664 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
665 attr = RTA_NEXT(attr, bytes)) {
666 switch (attr->rta_type) {
668 print_inet(attr, "dst", msg->rtm_family);
671 print_inet(attr, "src", msg->rtm_family);
674 print_string(attr, "iif");
677 print_integer(attr, "oif");
680 print_inet(attr, "gateway", msg->rtm_family);
683 print_attr(attr, "priority");
686 print_inet(attr, "prefsrc", msg->rtm_family);
689 print_attr(attr, "metrics");
692 print_integer(attr, "table");
695 print_attr(attr, NULL);
701 static void rtnl_newroute(struct nlmsghdr *hdr)
703 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
707 if (msg->rtm_table == RT_TABLE_MAIN &&
708 msg->rtm_protocol == RTPROT_BOOT &&
709 msg->rtm_type == RTN_UNICAST) {
711 process_newroute(msg->rtm_family, msg->rtm_scope,
712 msg, RTM_PAYLOAD(hdr));
714 if (msg->rtm_scope == RT_SCOPE_UNIVERSE)
715 process_newgateway(msg, RTM_PAYLOAD(hdr));
719 static void rtnl_delroute(struct nlmsghdr *hdr)
721 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
725 if (msg->rtm_table == RT_TABLE_MAIN &&
726 msg->rtm_protocol == RTPROT_BOOT &&
727 msg->rtm_type == RTN_UNICAST) {
729 process_delroute(msg->rtm_family, msg->rtm_scope,
730 msg, RTM_PAYLOAD(hdr));
732 if (msg->rtm_scope == RT_SCOPE_UNIVERSE)
733 process_delgateway(msg, RTM_PAYLOAD(hdr));
737 static const char *type2string(uint16_t type)
769 static GIOChannel *channel = NULL;
771 struct rtnl_request {
775 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
777 static GSList *request_list = NULL;
778 static guint32 request_seq = 0;
780 static struct rtnl_request *find_request(guint32 seq)
784 for (list = request_list; list; list = list->next) {
785 struct rtnl_request *req = list->data;
787 if (req->hdr.nlmsg_seq == seq)
794 static int send_request(struct rtnl_request *req)
796 struct sockaddr_nl addr;
799 DBG("%s len %d type %d flags 0x%04x seq %d",
800 type2string(req->hdr.nlmsg_type),
801 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
802 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
804 sk = g_io_channel_unix_get_fd(channel);
806 memset(&addr, 0, sizeof(addr));
807 addr.nl_family = AF_NETLINK;
809 return sendto(sk, req, req->hdr.nlmsg_len, 0,
810 (struct sockaddr *) &addr, sizeof(addr));
813 static int queue_request(struct rtnl_request *req)
815 request_list = g_slist_append(request_list, req);
817 if (g_slist_length(request_list) > 1)
820 return send_request(req);
823 static int process_response(guint32 seq)
825 struct rtnl_request *req;
829 req = find_request(seq);
831 request_list = g_slist_remove(request_list, req);
835 req = g_slist_nth_data(request_list, 0);
839 return send_request(req);
842 static void rtnl_message(void *buf, size_t len)
844 DBG("buf %p len %zd", buf, len);
847 struct nlmsghdr *hdr = buf;
848 struct nlmsgerr *err;
850 if (!NLMSG_OK(hdr, len))
853 DBG("%s len %d type %d flags 0x%04x seq %d",
854 type2string(hdr->nlmsg_type),
855 hdr->nlmsg_len, hdr->nlmsg_type,
856 hdr->nlmsg_flags, hdr->nlmsg_seq);
858 switch (hdr->nlmsg_type) {
863 process_response(hdr->nlmsg_seq);
866 err = NLMSG_DATA(hdr);
867 DBG("error %d (%s)", -err->error,
868 strerror(-err->error));
890 len -= hdr->nlmsg_len;
891 buf += hdr->nlmsg_len;
895 static gboolean netlink_event(GIOChannel *chan,
896 GIOCondition cond, gpointer data)
898 unsigned char buf[4096];
902 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
905 memset(buf, 0, sizeof(buf));
907 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
909 if (err == G_IO_ERROR_AGAIN)
914 rtnl_message(buf, len);
919 static int send_getlink(void)
921 struct rtnl_request *req;
925 req = g_try_malloc0(RTNL_REQUEST_SIZE);
929 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
930 req->hdr.nlmsg_type = RTM_GETLINK;
931 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
932 req->hdr.nlmsg_pid = 0;
933 req->hdr.nlmsg_seq = request_seq++;
934 req->msg.rtgen_family = AF_INET;
936 return queue_request(req);
939 static int send_getaddr(void)
941 struct rtnl_request *req;
945 req = g_try_malloc0(RTNL_REQUEST_SIZE);
949 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
950 req->hdr.nlmsg_type = RTM_GETADDR;
951 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
952 req->hdr.nlmsg_pid = 0;
953 req->hdr.nlmsg_seq = request_seq++;
954 req->msg.rtgen_family = AF_INET;
956 return queue_request(req);
959 int connman_rtnl_send_getroute(void)
961 struct rtnl_request *req;
965 req = g_try_malloc0(RTNL_REQUEST_SIZE);
969 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
970 req->hdr.nlmsg_type = RTM_GETROUTE;
971 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
972 req->hdr.nlmsg_pid = 0;
973 req->hdr.nlmsg_seq = request_seq++;
974 req->msg.rtgen_family = AF_INET;
976 return queue_request(req);
979 int __connman_rtnl_init(void)
981 struct sockaddr_nl addr;
986 ipconfig_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
987 NULL, free_ipconfig);
989 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
993 memset(&addr, 0, sizeof(addr));
994 addr.nl_family = AF_NETLINK;
995 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
997 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1002 channel = g_io_channel_unix_new(sk);
1003 g_io_channel_set_close_on_unref(channel, TRUE);
1005 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1006 netlink_event, NULL);
1011 void __connman_rtnl_start(void)
1017 connman_rtnl_send_getroute();
1020 void __connman_rtnl_cleanup(void)
1026 g_slist_free(ipconfig_list);
1027 ipconfig_list = NULL;
1029 g_hash_table_destroy(ipconfig_hash);
1030 ipconfig_hash = NULL;
1032 for (list = watch_list; list; list = list->next) {
1033 struct watch_data *watch = list->data;
1035 DBG("removing watch %d", watch->id);
1041 g_slist_free(watch_list);
1044 for (list = request_list; list; list = list->next) {
1045 struct rtnl_request *req = list->data;
1047 DBG("%s len %d type %d flags 0x%04x seq %d",
1048 type2string(req->hdr.nlmsg_type),
1049 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1050 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1056 g_slist_free(request_list);
1057 request_list = NULL;
1059 g_io_channel_shutdown(channel, TRUE, NULL);
1060 g_io_channel_unref(channel);