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>
31 #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;
54 * connman_rtnl_add_newlink_watch:
55 * @index: network device index
56 * @callback: callback function
57 * @user_data: callback data;
59 * Add a new RTNL watch for newlink events
61 * Returns: %0 on failure and a unique id on success
63 unsigned int connman_rtnl_add_newlink_watch(int index,
64 connman_rtnl_link_cb_t callback, void *user_data)
66 struct watch_data *watch;
68 watch = g_try_new0(struct watch_data, 1);
72 watch->id = ++watch_id;
75 watch->newlink = callback;
76 watch->user_data = user_data;
78 watch_list = g_slist_prepend(watch_list, watch);
80 DBG("id %d", watch->id);
83 unsigned int flags = __connman_ipconfig_get_flags(index);
86 callback(flags, 0, user_data);
93 * connman_rtnl_remove_watch:
94 * @id: watch identifier
96 * Remove the RTNL watch for the identifier
98 void connman_rtnl_remove_watch(unsigned int id)
107 for (list = watch_list; list; list = list->next) {
108 struct watch_data *watch = list->data;
110 if (watch->id == id) {
111 watch_list = g_slist_remove(watch_list, watch);
118 static void trigger_rtnl(int index, void *user_data)
120 struct connman_rtnl *rtnl = user_data;
123 unsigned short type = __connman_ipconfig_get_type(index);
124 unsigned int flags = __connman_ipconfig_get_flags(index);
126 rtnl->newlink(type, index, flags, 0);
129 if (rtnl->newgateway) {
130 const char *gateway = __connman_ipconfig_get_gateway(index);
133 rtnl->newgateway(index, gateway);
137 static GSList *rtnl_list = NULL;
139 static gint compare_priority(gconstpointer a, gconstpointer b)
141 const struct connman_rtnl *rtnl1 = a;
142 const struct connman_rtnl *rtnl2 = b;
144 return rtnl2->priority - rtnl1->priority;
148 * connman_rtnl_register:
151 * Register a new RTNL module
153 * Returns: %0 on success
155 int connman_rtnl_register(struct connman_rtnl *rtnl)
157 DBG("rtnl %p name %s", rtnl, rtnl->name);
159 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
162 __connman_ipconfig_foreach(trigger_rtnl, rtnl);
168 * connman_rtnl_unregister:
171 * Remove a previously registered RTNL module
173 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
175 DBG("rtnl %p name %s", rtnl, rtnl->name);
177 rtnl_list = g_slist_remove(rtnl_list, rtnl);
180 static const char *operstate2str(unsigned char operstate)
183 case IF_OPER_UNKNOWN:
185 case IF_OPER_NOTPRESENT:
186 return "NOT-PRESENT";
189 case IF_OPER_LOWERLAYERDOWN:
190 return "LOWER-LAYER-DOWN";
191 case IF_OPER_TESTING:
193 case IF_OPER_DORMANT:
202 static void extract_link(struct ifinfomsg *msg, int bytes,
203 const char **ifname, unsigned char *operstate)
207 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
208 attr = RTA_NEXT(attr, bytes)) {
209 switch (attr->rta_type) {
212 *ifname = RTA_DATA(attr);
215 if (operstate != NULL)
216 *operstate = *((unsigned char *) RTA_DATA(attr));
224 static void process_newlink(unsigned short type, int index, unsigned flags,
225 unsigned change, struct ifinfomsg *msg, int bytes)
227 unsigned char operstate = 0xff;
228 const char *ifname = NULL;
233 case ARPHRD_LOOPBACK:
235 __connman_ipconfig_newlink(index, type, flags);
239 extract_link(msg, bytes, &ifname, &operstate);
241 if (operstate != 0xff)
242 connman_info("%s {state} index %d operstate %d <%s>",
243 ifname, index, operstate,
244 operstate2str(operstate));
246 for (list = rtnl_list; list; list = list->next) {
247 struct connman_rtnl *rtnl = list->data;
250 rtnl->newlink(type, index, flags, change);
253 for (list = watch_list; list; list = list->next) {
254 struct watch_data *watch = list->data;
256 if (watch->index != index)
260 watch->newlink(flags, change, watch->user_data);
264 static void process_dellink(unsigned short type, int index, unsigned flags,
265 unsigned change, struct ifinfomsg *msg, int bytes)
269 for (list = rtnl_list; list; list = list->next) {
270 struct connman_rtnl *rtnl = list->data;
273 rtnl->dellink(type, index, flags, change);
278 case ARPHRD_LOOPBACK:
280 __connman_ipconfig_dellink(index);
285 static void extract_addr(struct ifaddrmsg *msg, int bytes,
287 struct in_addr *local,
288 struct in_addr *address,
289 struct in_addr *broadcast)
293 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
294 attr = RTA_NEXT(attr, bytes)) {
295 switch (attr->rta_type) {
298 *address = *((struct in_addr *) RTA_DATA(attr));
302 *local = *((struct in_addr *) RTA_DATA(attr));
305 if (broadcast != NULL)
306 *broadcast = *((struct in_addr *) RTA_DATA(attr));
310 *label = RTA_DATA(attr);
316 static void process_newaddr(unsigned char family, unsigned char prefixlen,
317 int index, struct ifaddrmsg *msg, int bytes)
319 struct in_addr address = { INADDR_ANY };
320 const char *label = NULL;
322 if (family != AF_INET)
325 extract_addr(msg, bytes, &label, &address, NULL, NULL);
327 __connman_ipconfig_newaddr(index, label,
328 prefixlen, inet_ntoa(address));
331 static void process_deladdr(unsigned char family, unsigned char prefixlen,
332 int index, struct ifaddrmsg *msg, int bytes)
334 struct in_addr address = { INADDR_ANY };
335 const char *label = NULL;
337 if (family != AF_INET)
340 extract_addr(msg, bytes, &label, &address, NULL, NULL);
342 __connman_ipconfig_deladdr(index, label,
343 prefixlen, inet_ntoa(address));
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 __connman_ipconfig_newroute(index, scope, dststr, gatewaystr);
389 if (scope != RT_SCOPE_UNIVERSE || dst.s_addr != INADDR_ANY)
392 for (list = rtnl_list; list; list = list->next) {
393 struct connman_rtnl *rtnl = list->data;
395 if (rtnl->newgateway)
396 rtnl->newgateway(index, gatewaystr);
400 static void process_delroute(unsigned char family, unsigned char scope,
401 struct rtmsg *msg, int bytes)
404 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
405 char dststr[16], gatewaystr[16];
408 if (family != AF_INET)
411 extract_route(msg, bytes, &index, &dst, &gateway);
413 inet_ntop(family, &dst, dststr, sizeof(dststr));
414 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
416 __connman_ipconfig_delroute(index, scope, dststr, gatewaystr);
418 if (scope != RT_SCOPE_UNIVERSE || dst.s_addr != INADDR_ANY)
421 for (list = rtnl_list; list; list = list->next) {
422 struct connman_rtnl *rtnl = list->data;
424 if (rtnl->delgateway)
425 rtnl->delgateway(index, gatewaystr);
429 static inline void print_ether(struct rtattr *attr, const char *name)
431 int len = (int) RTA_PAYLOAD(attr);
433 if (len == ETH_ALEN) {
434 struct ether_addr eth;
435 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
436 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
438 print(" attr %s (len %d)\n", name, len);
441 static inline void print_inet(struct rtattr *attr, const char *name,
442 unsigned char family)
444 int len = (int) RTA_PAYLOAD(attr);
446 if (family == AF_INET && len == sizeof(struct in_addr)) {
448 addr = *((struct in_addr *) RTA_DATA(attr));
449 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
451 print(" attr %s (len %d)\n", name, len);
454 static inline void print_string(struct rtattr *attr, const char *name)
456 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
457 (char *) RTA_DATA(attr));
460 static inline void print_byte(struct rtattr *attr, const char *name)
462 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
463 *((unsigned char *) RTA_DATA(attr)));
466 static inline void print_integer(struct rtattr *attr, const char *name)
468 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
469 *((int *) RTA_DATA(attr)));
472 static inline void print_attr(struct rtattr *attr, const char *name)
474 int len = (int) RTA_PAYLOAD(attr);
477 print(" attr %s (len %d)\n", name, len);
479 print(" attr %d (len %d)\n", attr->rta_type, len);
482 static void rtnl_link(struct nlmsghdr *hdr)
484 struct ifinfomsg *msg;
488 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
489 bytes = IFLA_PAYLOAD(hdr);
491 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
493 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
494 attr = RTA_NEXT(attr, bytes)) {
495 switch (attr->rta_type) {
497 print_ether(attr, "address");
500 print_ether(attr, "broadcast");
503 print_string(attr, "ifname");
506 print_integer(attr, "mtu");
509 print_attr(attr, "link");
512 print_attr(attr, "qdisc");
515 print_attr(attr, "stats");
518 print_attr(attr, "cost");
521 print_attr(attr, "priority");
524 print_attr(attr, "master");
527 print_attr(attr, "wireless");
530 print_attr(attr, "protinfo");
533 print_integer(attr, "txqlen");
536 print_attr(attr, "map");
539 print_attr(attr, "weight");
542 print_byte(attr, "operstate");
545 print_byte(attr, "linkmode");
548 print_attr(attr, NULL);
554 static void rtnl_newlink(struct nlmsghdr *hdr)
556 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
560 process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
561 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
564 static void rtnl_dellink(struct nlmsghdr *hdr)
566 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
570 process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
571 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
574 static void rtnl_addr(struct nlmsghdr *hdr)
576 struct ifaddrmsg *msg;
580 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
581 bytes = IFA_PAYLOAD(hdr);
583 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
585 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
586 attr = RTA_NEXT(attr, bytes)) {
587 switch (attr->rta_type) {
589 print_inet(attr, "address", msg->ifa_family);
592 print_inet(attr, "local", msg->ifa_family);
595 print_string(attr, "label");
598 print_inet(attr, "broadcast", msg->ifa_family);
601 print_attr(attr, "anycast");
604 print_attr(attr, "cacheinfo");
607 print_attr(attr, "multicast");
610 print_attr(attr, NULL);
616 static void rtnl_newaddr(struct nlmsghdr *hdr)
618 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
622 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
623 msg, IFA_PAYLOAD(hdr));
626 static void rtnl_deladdr(struct nlmsghdr *hdr)
628 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
632 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
633 msg, IFA_PAYLOAD(hdr));
636 static void rtnl_route(struct nlmsghdr *hdr)
642 msg = (struct rtmsg *) NLMSG_DATA(hdr);
643 bytes = RTM_PAYLOAD(hdr);
645 print("rtm_family %d rtm_table %d rtm_protocol %d",
646 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
647 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
648 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
650 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
651 attr = RTA_NEXT(attr, bytes)) {
652 switch (attr->rta_type) {
654 print_inet(attr, "dst", msg->rtm_family);
657 print_inet(attr, "src", msg->rtm_family);
660 print_string(attr, "iif");
663 print_integer(attr, "oif");
666 print_inet(attr, "gateway", msg->rtm_family);
669 print_attr(attr, "priority");
672 print_inet(attr, "prefsrc", msg->rtm_family);
675 print_attr(attr, "metrics");
678 print_integer(attr, "table");
681 print_attr(attr, NULL);
687 static void rtnl_newroute(struct nlmsghdr *hdr)
689 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
693 if (msg->rtm_table == RT_TABLE_MAIN &&
694 msg->rtm_protocol == RTPROT_BOOT &&
695 msg->rtm_type == RTN_UNICAST)
696 process_newroute(msg->rtm_family, msg->rtm_scope,
697 msg, RTM_PAYLOAD(hdr));
700 static void rtnl_delroute(struct nlmsghdr *hdr)
702 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
706 if (msg->rtm_table == RT_TABLE_MAIN &&
707 msg->rtm_protocol == RTPROT_BOOT &&
708 msg->rtm_type == RTN_UNICAST)
709 process_delroute(msg->rtm_family, msg->rtm_scope,
710 msg, RTM_PAYLOAD(hdr));
713 static const char *type2string(uint16_t type)
745 static GIOChannel *channel = NULL;
747 struct rtnl_request {
751 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
753 static GSList *request_list = NULL;
754 static guint32 request_seq = 0;
756 static struct rtnl_request *find_request(guint32 seq)
760 for (list = request_list; list; list = list->next) {
761 struct rtnl_request *req = list->data;
763 if (req->hdr.nlmsg_seq == seq)
770 static int send_request(struct rtnl_request *req)
772 struct sockaddr_nl addr;
775 DBG("%s len %d type %d flags 0x%04x seq %d",
776 type2string(req->hdr.nlmsg_type),
777 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
778 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
780 sk = g_io_channel_unix_get_fd(channel);
782 memset(&addr, 0, sizeof(addr));
783 addr.nl_family = AF_NETLINK;
785 return sendto(sk, req, req->hdr.nlmsg_len, 0,
786 (struct sockaddr *) &addr, sizeof(addr));
789 static int queue_request(struct rtnl_request *req)
791 request_list = g_slist_append(request_list, req);
793 if (g_slist_length(request_list) > 1)
796 return send_request(req);
799 static int process_response(guint32 seq)
801 struct rtnl_request *req;
805 req = find_request(seq);
807 request_list = g_slist_remove(request_list, req);
811 req = g_slist_nth_data(request_list, 0);
815 return send_request(req);
818 static void rtnl_message(void *buf, size_t len)
820 DBG("buf %p len %zd", buf, len);
823 struct nlmsghdr *hdr = buf;
824 struct nlmsgerr *err;
826 if (!NLMSG_OK(hdr, len))
829 DBG("%s len %d type %d flags 0x%04x seq %d",
830 type2string(hdr->nlmsg_type),
831 hdr->nlmsg_len, hdr->nlmsg_type,
832 hdr->nlmsg_flags, hdr->nlmsg_seq);
834 switch (hdr->nlmsg_type) {
839 process_response(hdr->nlmsg_seq);
842 err = NLMSG_DATA(hdr);
843 DBG("error %d (%s)", -err->error,
844 strerror(-err->error));
866 len -= hdr->nlmsg_len;
867 buf += hdr->nlmsg_len;
871 static gboolean netlink_event(GIOChannel *chan,
872 GIOCondition cond, gpointer data)
874 unsigned char buf[4096];
878 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
881 memset(buf, 0, sizeof(buf));
883 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
885 if (err == G_IO_ERROR_AGAIN)
890 rtnl_message(buf, len);
895 static int send_getlink(void)
897 struct rtnl_request *req;
901 req = g_try_malloc0(RTNL_REQUEST_SIZE);
905 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
906 req->hdr.nlmsg_type = RTM_GETLINK;
907 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
908 req->hdr.nlmsg_pid = 0;
909 req->hdr.nlmsg_seq = request_seq++;
910 req->msg.rtgen_family = AF_INET;
912 return queue_request(req);
915 static int send_getaddr(void)
917 struct rtnl_request *req;
921 req = g_try_malloc0(RTNL_REQUEST_SIZE);
925 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
926 req->hdr.nlmsg_type = RTM_GETADDR;
927 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
928 req->hdr.nlmsg_pid = 0;
929 req->hdr.nlmsg_seq = request_seq++;
930 req->msg.rtgen_family = AF_INET;
932 return queue_request(req);
935 static int send_getroute(void)
937 struct rtnl_request *req;
941 req = g_try_malloc0(RTNL_REQUEST_SIZE);
945 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
946 req->hdr.nlmsg_type = RTM_GETROUTE;
947 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
948 req->hdr.nlmsg_pid = 0;
949 req->hdr.nlmsg_seq = request_seq++;
950 req->msg.rtgen_family = AF_INET;
952 return queue_request(req);
955 int __connman_rtnl_init(void)
957 struct sockaddr_nl addr;
962 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
966 memset(&addr, 0, sizeof(addr));
967 addr.nl_family = AF_NETLINK;
968 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
970 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
975 channel = g_io_channel_unix_new(sk);
976 g_io_channel_set_close_on_unref(channel, TRUE);
978 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
979 netlink_event, NULL);
984 void __connman_rtnl_start(void)
993 void __connman_rtnl_cleanup(void)
999 for (list = watch_list; list; list = list->next) {
1000 struct watch_data *watch = list->data;
1002 DBG("removing watch %d", watch->id);
1008 g_slist_free(watch_list);
1011 for (list = request_list; list; list = list->next) {
1012 struct rtnl_request *req = list->data;
1014 DBG("%s len %d type %d flags 0x%04x seq %d",
1015 type2string(req->hdr.nlmsg_type),
1016 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1017 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1023 g_slist_free(request_list);
1024 request_list = NULL;
1026 g_io_channel_shutdown(channel, TRUE, NULL);
1027 g_io_channel_unref(channel);