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>
30 #include <netinet/ether.h>
32 #include <net/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_rtnl(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);
158 if (rtnl->newgateway) {
159 const char *gateway = __connman_ipconfig_get_gateway(ipconfig);
162 rtnl->newgateway(index, gateway);
166 static GSList *rtnl_list = NULL;
168 static gint compare_priority(gconstpointer a, gconstpointer b)
170 const struct connman_rtnl *rtnl1 = a;
171 const struct connman_rtnl *rtnl2 = b;
173 return rtnl2->priority - rtnl1->priority;
177 * connman_rtnl_register:
180 * Register a new RTNL module
182 * Returns: %0 on success
184 int connman_rtnl_register(struct connman_rtnl *rtnl)
186 DBG("rtnl %p name %s", rtnl, rtnl->name);
188 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
191 g_hash_table_foreach(ipconfig_hash, trigger_rtnl, rtnl);
197 * connman_rtnl_unregister:
200 * Remove a previously registered RTNL module
202 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
204 DBG("rtnl %p name %s", rtnl, rtnl->name);
206 rtnl_list = g_slist_remove(rtnl_list, rtnl);
209 static void process_newlink(unsigned short type, int index,
210 unsigned flags, unsigned change)
212 struct connman_ipconfig *ipconfig;
217 case ARPHRD_LOOPBACK:
219 ipconfig = g_hash_table_lookup(ipconfig_hash,
220 GINT_TO_POINTER(index));
221 if (ipconfig == NULL) {
222 ipconfig = connman_ipconfig_create(index);
223 if (ipconfig != NULL) {
224 g_hash_table_insert(ipconfig_hash,
225 GINT_TO_POINTER(index), ipconfig);
227 register_ipconfig(ipconfig);
231 if (ipconfig != NULL)
232 __connman_ipconfig_update_link(ipconfig,
237 for (list = rtnl_list; list; list = list->next) {
238 struct connman_rtnl *rtnl = list->data;
241 rtnl->newlink(type, index, flags, change);
244 for (list = watch_list; list; list = list->next) {
245 struct watch_data *watch = list->data;
247 if (watch->index != index)
251 watch->newlink(flags, change, watch->user_data);
255 static void process_dellink(unsigned short type, int index,
256 unsigned flags, unsigned change)
260 for (list = rtnl_list; list; list = list->next) {
261 struct connman_rtnl *rtnl = list->data;
264 rtnl->dellink(type, index, flags, change);
269 case ARPHRD_LOOPBACK:
271 g_hash_table_remove(ipconfig_hash, GINT_TO_POINTER(index));
276 static void extract_addr(struct ifaddrmsg *msg, int bytes,
278 struct in_addr *local,
279 struct in_addr *address,
280 struct in_addr *broadcast)
284 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
285 attr = RTA_NEXT(attr, bytes)) {
286 switch (attr->rta_type) {
289 *address = *((struct in_addr *) RTA_DATA(attr));
293 *local = *((struct in_addr *) RTA_DATA(attr));
296 if (broadcast != NULL)
297 *broadcast = *((struct in_addr *) RTA_DATA(attr));
301 *label = RTA_DATA(attr);
307 static void process_newaddr(unsigned char family, unsigned char prefixlen,
308 int index, struct ifaddrmsg *msg, int bytes)
312 struct in_addr address = { INADDR_ANY };
314 if (family != AF_INET)
317 extract_addr(msg, bytes, &label, &address, NULL, NULL);
319 for (list = ipconfig_list; list; list = list->next) {
320 struct connman_ipconfig *ipconfig = list->data;
322 if (__connman_ipconfig_get_index(ipconfig) != index)
325 __connman_ipconfig_add_address(ipconfig, label, prefixlen,
326 inet_ntoa(address), NULL);
330 static void process_deladdr(unsigned char family, unsigned char prefixlen,
331 int index, struct ifaddrmsg *msg, int bytes)
335 struct in_addr address = { INADDR_ANY };
337 if (family != AF_INET)
340 extract_addr(msg, bytes, &label, &address, NULL, NULL);
342 for (list = ipconfig_list; list; list = list->next) {
343 struct connman_ipconfig *ipconfig = list->data;
345 if (__connman_ipconfig_get_index(ipconfig) != index)
348 __connman_ipconfig_del_address(ipconfig, label, prefixlen,
349 inet_ntoa(address), NULL);
353 static void extract_route(struct rtmsg *msg, int bytes, int *index,
355 struct in_addr *gateway)
359 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
360 attr = RTA_NEXT(attr, bytes)) {
361 switch (attr->rta_type) {
364 *dst = *((struct in_addr *) RTA_DATA(attr));
368 *gateway = *((struct in_addr *) RTA_DATA(attr));
372 *index = *((int *) RTA_DATA(attr));
378 static void process_newroute(unsigned char family, unsigned char scope,
379 struct rtmsg *msg, int bytes)
382 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
383 char dststr[16], gatewaystr[16];
386 if (family != AF_INET)
389 extract_route(msg, bytes, &index, &dst, &gateway);
391 inet_ntop(family, &dst, dststr, sizeof(dststr));
392 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
394 for (list = ipconfig_list; list; list = list->next) {
395 struct connman_ipconfig *ipconfig = list->data;
397 if (__connman_ipconfig_get_index(ipconfig) != index)
400 __connman_ipconfig_add_route(ipconfig, scope,
404 if (scope != RT_SCOPE_UNIVERSE || dst.s_addr != INADDR_ANY)
407 for (list = rtnl_list; list; list = list->next) {
408 struct connman_rtnl *rtnl = list->data;
410 if (rtnl->newgateway)
411 rtnl->newgateway(index, gatewaystr);
415 static void process_delroute(unsigned char family, unsigned char scope,
416 struct rtmsg *msg, int bytes)
419 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
420 char dststr[16], gatewaystr[16];
423 if (family != AF_INET)
426 extract_route(msg, bytes, &index, &dst, &gateway);
428 inet_ntop(family, &dst, dststr, sizeof(dststr));
429 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
431 for (list = ipconfig_list; list; list = list->next) {
432 struct connman_ipconfig *ipconfig = list->data;
434 if (__connman_ipconfig_get_index(ipconfig) != index)
437 __connman_ipconfig_del_route(ipconfig, scope,
441 if (scope != RT_SCOPE_UNIVERSE || dst.s_addr != INADDR_ANY)
444 for (list = rtnl_list; list; list = list->next) {
445 struct connman_rtnl *rtnl = list->data;
447 if (rtnl->delgateway)
448 rtnl->delgateway(index, gatewaystr);
452 static inline void print_ether(struct rtattr *attr, const char *name)
454 int len = (int) RTA_PAYLOAD(attr);
456 if (len == ETH_ALEN) {
457 struct ether_addr eth;
458 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
459 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
461 print(" attr %s (len %d)\n", name, len);
464 static inline void print_inet(struct rtattr *attr, const char *name,
465 unsigned char family)
467 int len = (int) RTA_PAYLOAD(attr);
469 if (family == AF_INET && len == sizeof(struct in_addr)) {
471 addr = *((struct in_addr *) RTA_DATA(attr));
472 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
474 print(" attr %s (len %d)\n", name, len);
477 static inline void print_string(struct rtattr *attr, const char *name)
479 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
480 (char *) RTA_DATA(attr));
483 static inline void print_byte(struct rtattr *attr, const char *name)
485 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
486 *((unsigned char *) RTA_DATA(attr)));
489 static inline void print_integer(struct rtattr *attr, const char *name)
491 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
492 *((int *) RTA_DATA(attr)));
495 static inline void print_attr(struct rtattr *attr, const char *name)
497 int len = (int) RTA_PAYLOAD(attr);
500 print(" attr %s (len %d)\n", name, len);
502 print(" attr %d (len %d)\n", attr->rta_type, len);
505 static void rtnl_link(struct nlmsghdr *hdr)
507 struct ifinfomsg *msg;
511 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
512 bytes = IFLA_PAYLOAD(hdr);
514 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
516 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
517 attr = RTA_NEXT(attr, bytes)) {
518 switch (attr->rta_type) {
520 print_ether(attr, "address");
523 print_ether(attr, "broadcast");
526 print_string(attr, "ifname");
529 print_integer(attr, "mtu");
532 print_attr(attr, "link");
535 print_attr(attr, "qdisc");
538 print_attr(attr, "stats");
541 print_attr(attr, "cost");
544 print_attr(attr, "priority");
547 print_attr(attr, "master");
550 print_attr(attr, "wireless");
553 print_attr(attr, "protinfo");
556 print_integer(attr, "txqlen");
559 print_attr(attr, "map");
562 print_attr(attr, "weight");
565 print_byte(attr, "operstate");
568 print_byte(attr, "linkmode");
571 print_attr(attr, NULL);
577 static void rtnl_newlink(struct nlmsghdr *hdr)
579 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
583 process_newlink(msg->ifi_type, msg->ifi_index,
584 msg->ifi_flags, msg->ifi_change);
587 static void rtnl_dellink(struct nlmsghdr *hdr)
589 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
593 process_dellink(msg->ifi_type, msg->ifi_index,
594 msg->ifi_flags, msg->ifi_change);
597 static void rtnl_addr(struct nlmsghdr *hdr)
599 struct ifaddrmsg *msg;
603 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
604 bytes = IFA_PAYLOAD(hdr);
606 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
608 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
609 attr = RTA_NEXT(attr, bytes)) {
610 switch (attr->rta_type) {
612 print_inet(attr, "address", msg->ifa_family);
615 print_inet(attr, "local", msg->ifa_family);
618 print_string(attr, "label");
621 print_inet(attr, "broadcast", msg->ifa_family);
624 print_attr(attr, "anycast");
627 print_attr(attr, "cacheinfo");
630 print_attr(attr, "multicast");
633 print_attr(attr, NULL);
639 static void rtnl_newaddr(struct nlmsghdr *hdr)
641 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
645 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
646 msg, IFA_PAYLOAD(hdr));
649 static void rtnl_deladdr(struct nlmsghdr *hdr)
651 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
655 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
656 msg, IFA_PAYLOAD(hdr));
659 static void rtnl_route(struct nlmsghdr *hdr)
665 msg = (struct rtmsg *) NLMSG_DATA(hdr);
666 bytes = RTM_PAYLOAD(hdr);
668 print("rtm_family %d rtm_table %d rtm_protocol %d",
669 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
670 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
671 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
673 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
674 attr = RTA_NEXT(attr, bytes)) {
675 switch (attr->rta_type) {
677 print_inet(attr, "dst", msg->rtm_family);
680 print_inet(attr, "src", msg->rtm_family);
683 print_string(attr, "iif");
686 print_integer(attr, "oif");
689 print_inet(attr, "gateway", msg->rtm_family);
692 print_attr(attr, "priority");
695 print_inet(attr, "prefsrc", msg->rtm_family);
698 print_attr(attr, "metrics");
701 print_integer(attr, "table");
704 print_attr(attr, NULL);
710 static void rtnl_newroute(struct nlmsghdr *hdr)
712 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
716 if (msg->rtm_table == RT_TABLE_MAIN &&
717 msg->rtm_protocol == RTPROT_BOOT &&
718 msg->rtm_type == RTN_UNICAST)
719 process_newroute(msg->rtm_family, msg->rtm_scope,
720 msg, RTM_PAYLOAD(hdr));
723 static void rtnl_delroute(struct nlmsghdr *hdr)
725 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
729 if (msg->rtm_table == RT_TABLE_MAIN &&
730 msg->rtm_protocol == RTPROT_BOOT &&
731 msg->rtm_type == RTN_UNICAST)
732 process_delroute(msg->rtm_family, msg->rtm_scope,
733 msg, RTM_PAYLOAD(hdr));
736 static const char *type2string(uint16_t type)
768 static GIOChannel *channel = NULL;
770 struct rtnl_request {
774 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
776 static GSList *request_list = NULL;
777 static guint32 request_seq = 0;
779 static struct rtnl_request *find_request(guint32 seq)
783 for (list = request_list; list; list = list->next) {
784 struct rtnl_request *req = list->data;
786 if (req->hdr.nlmsg_seq == seq)
793 static int send_request(struct rtnl_request *req)
795 struct sockaddr_nl addr;
798 DBG("%s len %d type %d flags 0x%04x seq %d",
799 type2string(req->hdr.nlmsg_type),
800 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
801 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
803 sk = g_io_channel_unix_get_fd(channel);
805 memset(&addr, 0, sizeof(addr));
806 addr.nl_family = AF_NETLINK;
808 return sendto(sk, req, req->hdr.nlmsg_len, 0,
809 (struct sockaddr *) &addr, sizeof(addr));
812 static int queue_request(struct rtnl_request *req)
814 request_list = g_slist_append(request_list, req);
816 if (g_slist_length(request_list) > 1)
819 return send_request(req);
822 static int process_response(guint32 seq)
824 struct rtnl_request *req;
828 req = find_request(seq);
830 request_list = g_slist_remove(request_list, req);
834 req = g_slist_nth_data(request_list, 0);
838 return send_request(req);
841 static void rtnl_message(void *buf, size_t len)
843 DBG("buf %p len %zd", buf, len);
846 struct nlmsghdr *hdr = buf;
847 struct nlmsgerr *err;
849 if (!NLMSG_OK(hdr, len))
852 DBG("%s len %d type %d flags 0x%04x seq %d",
853 type2string(hdr->nlmsg_type),
854 hdr->nlmsg_len, hdr->nlmsg_type,
855 hdr->nlmsg_flags, hdr->nlmsg_seq);
857 switch (hdr->nlmsg_type) {
862 process_response(hdr->nlmsg_seq);
865 err = NLMSG_DATA(hdr);
866 DBG("error %d (%s)", -err->error,
867 strerror(-err->error));
889 len -= hdr->nlmsg_len;
890 buf += hdr->nlmsg_len;
894 static gboolean netlink_event(GIOChannel *chan,
895 GIOCondition cond, gpointer data)
897 unsigned char buf[4096];
901 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
904 memset(buf, 0, sizeof(buf));
906 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
908 if (err == G_IO_ERROR_AGAIN)
913 rtnl_message(buf, len);
918 static int send_getlink(void)
920 struct rtnl_request *req;
924 req = g_try_malloc0(RTNL_REQUEST_SIZE);
928 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
929 req->hdr.nlmsg_type = RTM_GETLINK;
930 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
931 req->hdr.nlmsg_pid = 0;
932 req->hdr.nlmsg_seq = request_seq++;
933 req->msg.rtgen_family = AF_INET;
935 return queue_request(req);
938 static int send_getaddr(void)
940 struct rtnl_request *req;
944 req = g_try_malloc0(RTNL_REQUEST_SIZE);
948 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
949 req->hdr.nlmsg_type = RTM_GETADDR;
950 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
951 req->hdr.nlmsg_pid = 0;
952 req->hdr.nlmsg_seq = request_seq++;
953 req->msg.rtgen_family = AF_INET;
955 return queue_request(req);
958 static int send_getroute(void)
960 struct rtnl_request *req;
964 req = g_try_malloc0(RTNL_REQUEST_SIZE);
968 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
969 req->hdr.nlmsg_type = RTM_GETROUTE;
970 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
971 req->hdr.nlmsg_pid = 0;
972 req->hdr.nlmsg_seq = request_seq++;
973 req->msg.rtgen_family = AF_INET;
975 return queue_request(req);
978 int __connman_rtnl_init(void)
980 struct sockaddr_nl addr;
985 ipconfig_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
986 NULL, free_ipconfig);
988 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
992 memset(&addr, 0, sizeof(addr));
993 addr.nl_family = AF_NETLINK;
994 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
996 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1001 channel = g_io_channel_unix_new(sk);
1002 g_io_channel_set_close_on_unref(channel, TRUE);
1004 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1005 netlink_event, NULL);
1010 void __connman_rtnl_start(void)
1019 void __connman_rtnl_cleanup(void)
1025 g_slist_free(ipconfig_list);
1026 ipconfig_list = NULL;
1028 g_hash_table_destroy(ipconfig_hash);
1029 ipconfig_hash = NULL;
1031 for (list = watch_list; list; list = list->next) {
1032 struct watch_data *watch = list->data;
1034 DBG("removing watch %d", watch->id);
1040 g_slist_free(watch_list);
1043 for (list = request_list; list; list = list->next) {
1044 struct rtnl_request *req = list->data;
1046 DBG("%s len %d type %d flags 0x%04x seq %d",
1047 type2string(req->hdr.nlmsg_type),
1048 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1049 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1055 g_slist_free(request_list);
1056 request_list = NULL;
1058 g_io_channel_shutdown(channel, TRUE, NULL);
1059 g_io_channel_unref(channel);