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;
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 void process_newlink(unsigned short type, int index,
181 unsigned flags, unsigned change)
187 case ARPHRD_LOOPBACK:
189 __connman_ipconfig_newlink(index, type, flags);
193 for (list = rtnl_list; list; list = list->next) {
194 struct connman_rtnl *rtnl = list->data;
197 rtnl->newlink(type, index, flags, change);
200 for (list = watch_list; list; list = list->next) {
201 struct watch_data *watch = list->data;
203 if (watch->index != index)
207 watch->newlink(flags, change, watch->user_data);
211 static void process_dellink(unsigned short type, int index,
212 unsigned flags, unsigned change)
216 for (list = rtnl_list; list; list = list->next) {
217 struct connman_rtnl *rtnl = list->data;
220 rtnl->dellink(type, index, flags, change);
225 case ARPHRD_LOOPBACK:
227 __connman_ipconfig_dellink(index);
232 static void extract_addr(struct ifaddrmsg *msg, int bytes,
234 struct in_addr *local,
235 struct in_addr *address,
236 struct in_addr *broadcast)
240 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
241 attr = RTA_NEXT(attr, bytes)) {
242 switch (attr->rta_type) {
245 *address = *((struct in_addr *) RTA_DATA(attr));
249 *local = *((struct in_addr *) RTA_DATA(attr));
252 if (broadcast != NULL)
253 *broadcast = *((struct in_addr *) RTA_DATA(attr));
257 *label = RTA_DATA(attr);
263 static void process_newaddr(unsigned char family, unsigned char prefixlen,
264 int index, struct ifaddrmsg *msg, int bytes)
267 struct in_addr address = { INADDR_ANY };
269 if (family != AF_INET)
272 extract_addr(msg, bytes, &label, &address, NULL, NULL);
274 __connman_ipconfig_newaddr(index, label,
275 prefixlen, inet_ntoa(address));
278 static void process_deladdr(unsigned char family, unsigned char prefixlen,
279 int index, struct ifaddrmsg *msg, int bytes)
282 struct in_addr address = { INADDR_ANY };
284 if (family != AF_INET)
287 extract_addr(msg, bytes, &label, &address, NULL, NULL);
289 __connman_ipconfig_deladdr(index, label,
290 prefixlen, inet_ntoa(address));
293 static void extract_route(struct rtmsg *msg, int bytes, int *index,
295 struct in_addr *gateway)
299 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
300 attr = RTA_NEXT(attr, bytes)) {
301 switch (attr->rta_type) {
304 *dst = *((struct in_addr *) RTA_DATA(attr));
308 *gateway = *((struct in_addr *) RTA_DATA(attr));
312 *index = *((int *) RTA_DATA(attr));
318 static void process_newroute(unsigned char family, unsigned char scope,
319 struct rtmsg *msg, int bytes)
322 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
323 char dststr[16], gatewaystr[16];
326 if (family != AF_INET)
329 extract_route(msg, bytes, &index, &dst, &gateway);
331 inet_ntop(family, &dst, dststr, sizeof(dststr));
332 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
334 __connman_ipconfig_newroute(index, scope, dststr, gatewaystr);
336 if (scope != RT_SCOPE_UNIVERSE || dst.s_addr != INADDR_ANY)
339 for (list = rtnl_list; list; list = list->next) {
340 struct connman_rtnl *rtnl = list->data;
342 if (rtnl->newgateway)
343 rtnl->newgateway(index, gatewaystr);
347 static void process_delroute(unsigned char family, unsigned char scope,
348 struct rtmsg *msg, int bytes)
351 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
352 char dststr[16], gatewaystr[16];
355 if (family != AF_INET)
358 extract_route(msg, bytes, &index, &dst, &gateway);
360 inet_ntop(family, &dst, dststr, sizeof(dststr));
361 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
363 __connman_ipconfig_delroute(index, scope, dststr, gatewaystr);
365 if (scope != RT_SCOPE_UNIVERSE || dst.s_addr != INADDR_ANY)
368 for (list = rtnl_list; list; list = list->next) {
369 struct connman_rtnl *rtnl = list->data;
371 if (rtnl->delgateway)
372 rtnl->delgateway(index, gatewaystr);
376 static inline void print_ether(struct rtattr *attr, const char *name)
378 int len = (int) RTA_PAYLOAD(attr);
380 if (len == ETH_ALEN) {
381 struct ether_addr eth;
382 memcpy(ð, RTA_DATA(attr), ETH_ALEN);
383 print(" attr %s (len %d) %s\n", name, len, ether_ntoa(ð));
385 print(" attr %s (len %d)\n", name, len);
388 static inline void print_inet(struct rtattr *attr, const char *name,
389 unsigned char family)
391 int len = (int) RTA_PAYLOAD(attr);
393 if (family == AF_INET && len == sizeof(struct in_addr)) {
395 addr = *((struct in_addr *) RTA_DATA(attr));
396 print(" attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
398 print(" attr %s (len %d)\n", name, len);
401 static inline void print_string(struct rtattr *attr, const char *name)
403 print(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
404 (char *) RTA_DATA(attr));
407 static inline void print_byte(struct rtattr *attr, const char *name)
409 print(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
410 *((unsigned char *) RTA_DATA(attr)));
413 static inline void print_integer(struct rtattr *attr, const char *name)
415 print(" attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
416 *((int *) RTA_DATA(attr)));
419 static inline void print_attr(struct rtattr *attr, const char *name)
421 int len = (int) RTA_PAYLOAD(attr);
424 print(" attr %s (len %d)\n", name, len);
426 print(" attr %d (len %d)\n", attr->rta_type, len);
429 static void rtnl_link(struct nlmsghdr *hdr)
431 struct ifinfomsg *msg;
435 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
436 bytes = IFLA_PAYLOAD(hdr);
438 print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
440 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
441 attr = RTA_NEXT(attr, bytes)) {
442 switch (attr->rta_type) {
444 print_ether(attr, "address");
447 print_ether(attr, "broadcast");
450 print_string(attr, "ifname");
453 print_integer(attr, "mtu");
456 print_attr(attr, "link");
459 print_attr(attr, "qdisc");
462 print_attr(attr, "stats");
465 print_attr(attr, "cost");
468 print_attr(attr, "priority");
471 print_attr(attr, "master");
474 print_attr(attr, "wireless");
477 print_attr(attr, "protinfo");
480 print_integer(attr, "txqlen");
483 print_attr(attr, "map");
486 print_attr(attr, "weight");
489 print_byte(attr, "operstate");
492 print_byte(attr, "linkmode");
495 print_attr(attr, NULL);
501 static void rtnl_newlink(struct nlmsghdr *hdr)
503 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
507 process_newlink(msg->ifi_type, msg->ifi_index,
508 msg->ifi_flags, msg->ifi_change);
511 static void rtnl_dellink(struct nlmsghdr *hdr)
513 struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
517 process_dellink(msg->ifi_type, msg->ifi_index,
518 msg->ifi_flags, msg->ifi_change);
521 static void rtnl_addr(struct nlmsghdr *hdr)
523 struct ifaddrmsg *msg;
527 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
528 bytes = IFA_PAYLOAD(hdr);
530 print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
532 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
533 attr = RTA_NEXT(attr, bytes)) {
534 switch (attr->rta_type) {
536 print_inet(attr, "address", msg->ifa_family);
539 print_inet(attr, "local", msg->ifa_family);
542 print_string(attr, "label");
545 print_inet(attr, "broadcast", msg->ifa_family);
548 print_attr(attr, "anycast");
551 print_attr(attr, "cacheinfo");
554 print_attr(attr, "multicast");
557 print_attr(attr, NULL);
563 static void rtnl_newaddr(struct nlmsghdr *hdr)
565 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
569 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
570 msg, IFA_PAYLOAD(hdr));
573 static void rtnl_deladdr(struct nlmsghdr *hdr)
575 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
579 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
580 msg, IFA_PAYLOAD(hdr));
583 static void rtnl_route(struct nlmsghdr *hdr)
589 msg = (struct rtmsg *) NLMSG_DATA(hdr);
590 bytes = RTM_PAYLOAD(hdr);
592 print("rtm_family %d rtm_table %d rtm_protocol %d",
593 msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
594 print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
595 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
597 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
598 attr = RTA_NEXT(attr, bytes)) {
599 switch (attr->rta_type) {
601 print_inet(attr, "dst", msg->rtm_family);
604 print_inet(attr, "src", msg->rtm_family);
607 print_string(attr, "iif");
610 print_integer(attr, "oif");
613 print_inet(attr, "gateway", msg->rtm_family);
616 print_attr(attr, "priority");
619 print_inet(attr, "prefsrc", msg->rtm_family);
622 print_attr(attr, "metrics");
625 print_integer(attr, "table");
628 print_attr(attr, NULL);
634 static void rtnl_newroute(struct nlmsghdr *hdr)
636 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
640 if (msg->rtm_table == RT_TABLE_MAIN &&
641 msg->rtm_protocol == RTPROT_BOOT &&
642 msg->rtm_type == RTN_UNICAST)
643 process_newroute(msg->rtm_family, msg->rtm_scope,
644 msg, RTM_PAYLOAD(hdr));
647 static void rtnl_delroute(struct nlmsghdr *hdr)
649 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
653 if (msg->rtm_table == RT_TABLE_MAIN &&
654 msg->rtm_protocol == RTPROT_BOOT &&
655 msg->rtm_type == RTN_UNICAST)
656 process_delroute(msg->rtm_family, msg->rtm_scope,
657 msg, RTM_PAYLOAD(hdr));
660 static const char *type2string(uint16_t type)
692 static GIOChannel *channel = NULL;
694 struct rtnl_request {
698 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
700 static GSList *request_list = NULL;
701 static guint32 request_seq = 0;
703 static struct rtnl_request *find_request(guint32 seq)
707 for (list = request_list; list; list = list->next) {
708 struct rtnl_request *req = list->data;
710 if (req->hdr.nlmsg_seq == seq)
717 static int send_request(struct rtnl_request *req)
719 struct sockaddr_nl addr;
722 DBG("%s len %d type %d flags 0x%04x seq %d",
723 type2string(req->hdr.nlmsg_type),
724 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
725 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
727 sk = g_io_channel_unix_get_fd(channel);
729 memset(&addr, 0, sizeof(addr));
730 addr.nl_family = AF_NETLINK;
732 return sendto(sk, req, req->hdr.nlmsg_len, 0,
733 (struct sockaddr *) &addr, sizeof(addr));
736 static int queue_request(struct rtnl_request *req)
738 request_list = g_slist_append(request_list, req);
740 if (g_slist_length(request_list) > 1)
743 return send_request(req);
746 static int process_response(guint32 seq)
748 struct rtnl_request *req;
752 req = find_request(seq);
754 request_list = g_slist_remove(request_list, req);
758 req = g_slist_nth_data(request_list, 0);
762 return send_request(req);
765 static void rtnl_message(void *buf, size_t len)
767 DBG("buf %p len %zd", buf, len);
770 struct nlmsghdr *hdr = buf;
771 struct nlmsgerr *err;
773 if (!NLMSG_OK(hdr, len))
776 DBG("%s len %d type %d flags 0x%04x seq %d",
777 type2string(hdr->nlmsg_type),
778 hdr->nlmsg_len, hdr->nlmsg_type,
779 hdr->nlmsg_flags, hdr->nlmsg_seq);
781 switch (hdr->nlmsg_type) {
786 process_response(hdr->nlmsg_seq);
789 err = NLMSG_DATA(hdr);
790 DBG("error %d (%s)", -err->error,
791 strerror(-err->error));
813 len -= hdr->nlmsg_len;
814 buf += hdr->nlmsg_len;
818 static gboolean netlink_event(GIOChannel *chan,
819 GIOCondition cond, gpointer data)
821 unsigned char buf[4096];
825 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
828 memset(buf, 0, sizeof(buf));
830 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
832 if (err == G_IO_ERROR_AGAIN)
837 rtnl_message(buf, len);
842 static int send_getlink(void)
844 struct rtnl_request *req;
848 req = g_try_malloc0(RTNL_REQUEST_SIZE);
852 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
853 req->hdr.nlmsg_type = RTM_GETLINK;
854 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
855 req->hdr.nlmsg_pid = 0;
856 req->hdr.nlmsg_seq = request_seq++;
857 req->msg.rtgen_family = AF_INET;
859 return queue_request(req);
862 static int send_getaddr(void)
864 struct rtnl_request *req;
868 req = g_try_malloc0(RTNL_REQUEST_SIZE);
872 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
873 req->hdr.nlmsg_type = RTM_GETADDR;
874 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
875 req->hdr.nlmsg_pid = 0;
876 req->hdr.nlmsg_seq = request_seq++;
877 req->msg.rtgen_family = AF_INET;
879 return queue_request(req);
882 static int send_getroute(void)
884 struct rtnl_request *req;
888 req = g_try_malloc0(RTNL_REQUEST_SIZE);
892 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
893 req->hdr.nlmsg_type = RTM_GETROUTE;
894 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
895 req->hdr.nlmsg_pid = 0;
896 req->hdr.nlmsg_seq = request_seq++;
897 req->msg.rtgen_family = AF_INET;
899 return queue_request(req);
902 int __connman_rtnl_init(void)
904 struct sockaddr_nl addr;
909 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
913 memset(&addr, 0, sizeof(addr));
914 addr.nl_family = AF_NETLINK;
915 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
917 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
922 channel = g_io_channel_unix_new(sk);
923 g_io_channel_set_close_on_unref(channel, TRUE);
925 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
926 netlink_event, NULL);
931 void __connman_rtnl_start(void)
940 void __connman_rtnl_cleanup(void)
946 for (list = watch_list; list; list = list->next) {
947 struct watch_data *watch = list->data;
949 DBG("removing watch %d", watch->id);
955 g_slist_free(watch_list);
958 for (list = request_list; list; list = list->next) {
959 struct rtnl_request *req = list->data;
961 DBG("%s len %d type %d flags 0x%04x seq %d",
962 type2string(req->hdr.nlmsg_type),
963 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
964 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
970 g_slist_free(request_list);
973 g_io_channel_shutdown(channel, TRUE, NULL);
974 g_io_channel_unref(channel);