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;
51 * connman_rtnl_add_newlink_watch:
52 * @index: network device index
53 * @callback: callback function
54 * @user_data: callback data;
56 * Add a new RTNL watch for newlink events
58 * Returns: %0 on failure and a unique id on success
60 unsigned int connman_rtnl_add_newlink_watch(int index,
61 connman_rtnl_link_cb_t callback, void *user_data)
63 struct watch_data *watch;
65 watch = g_try_new0(struct watch_data, 1);
69 watch->id = ++watch_id;
72 watch->newlink = callback;
73 watch->user_data = user_data;
75 watch_list = g_slist_prepend(watch_list, watch);
77 DBG("id %d", watch->id);
83 * connman_rtnl_remove_watch:
84 * @id: watch identifier
86 * Remove the RTNL watch for the identifier
88 void connman_rtnl_remove_watch(unsigned int id)
97 for (list = watch_list; list; list = list->next) {
98 struct watch_data *watch = list->data;
100 if (watch->id == id) {
101 watch_list = g_slist_remove(watch_list, watch);
108 static GSList *rtnl_list = NULL;
110 static gint compare_priority(gconstpointer a, gconstpointer b)
112 const struct connman_rtnl *rtnl1 = a;
113 const struct connman_rtnl *rtnl2 = b;
115 return rtnl2->priority - rtnl1->priority;
119 * connman_rtnl_register:
122 * Register a new RTNL module
124 * Returns: %0 on success
126 int connman_rtnl_register(struct connman_rtnl *rtnl)
128 DBG("rtnl %p name %s", rtnl, rtnl->name);
130 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
137 * connman_rtnl_unregister:
140 * Remove a previously registered RTNL module
142 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
144 DBG("rtnl %p name %s", rtnl, rtnl->name);
146 rtnl_list = g_slist_remove(rtnl_list, rtnl);
149 static GHashTable *ipconfig_hash = NULL;
151 static void free_ipconfig(gpointer data)
153 struct connman_ipconfig *ipconfig = data;
155 __connman_rtnl_unregister_ipconfig(ipconfig);
157 connman_ipconfig_unref(ipconfig);
160 static void process_newlink(unsigned short type, int index,
161 unsigned flags, unsigned change)
163 struct connman_ipconfig *ipconfig;
168 case ARPHRD_LOOPBACK:
170 ipconfig = g_hash_table_lookup(ipconfig_hash,
171 GINT_TO_POINTER(index));
172 if (ipconfig == NULL) {
173 ipconfig = connman_ipconfig_create(index);
174 if (ipconfig != NULL) {
175 g_hash_table_insert(ipconfig_hash,
176 GINT_TO_POINTER(index), ipconfig);
178 __connman_rtnl_register_ipconfig(ipconfig);
182 if (ipconfig != NULL)
183 __connman_ipconfig_update_link(ipconfig,
188 for (list = rtnl_list; list; list = list->next) {
189 struct connman_rtnl *rtnl = list->data;
192 rtnl->newlink(type, index, flags, change);
195 for (list = watch_list; list; list = list->next) {
196 struct watch_data *watch = list->data;
198 if (watch->index != index)
202 watch->newlink(flags, change, watch->user_data);
206 static void process_dellink(unsigned short type, int index,
207 unsigned flags, unsigned change)
211 for (list = rtnl_list; list; list = list->next) {
212 struct connman_rtnl *rtnl = list->data;
215 rtnl->dellink(type, index, flags, change);
220 case ARPHRD_LOOPBACK:
222 g_hash_table_remove(ipconfig_hash, GINT_TO_POINTER(index));
227 static char *extract_gateway(struct rtmsg *msg, int bytes, int *index)
229 char *gateway = NULL;
233 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
234 attr = RTA_NEXT(attr, bytes)) {
235 switch (attr->rta_type) {
237 addr = *((struct in_addr *) RTA_DATA(attr));
239 gateway = g_strdup(inet_ntoa(addr));
242 *index = *((int *) RTA_DATA(attr));
250 static void process_newgateway(struct rtmsg *msg, int bytes)
256 gateway = extract_gateway(msg, bytes, &index);
257 if (gateway == NULL || index < 0)
260 for (list = rtnl_list; list; list = list->next) {
261 struct connman_rtnl *rtnl = list->data;
263 if (rtnl->newgateway)
264 rtnl->newgateway(index, gateway);
270 static void process_delgateway(struct rtmsg *msg, int bytes)
276 gateway = extract_gateway(msg, bytes, &index);
277 if (gateway == NULL || index < 0)
280 for (list = rtnl_list; list; list = list->next) {
281 struct connman_rtnl *rtnl = list->data;
283 if (rtnl->delgateway)
284 rtnl->delgateway(index, gateway);
290 static void extract_addr(struct ifaddrmsg *msg, int bytes,
292 struct in_addr *local,
293 struct in_addr *address,
294 struct in_addr *broadcast)
298 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
299 attr = RTA_NEXT(attr, bytes)) {
300 switch (attr->rta_type) {
303 *address = *((struct in_addr *) RTA_DATA(attr));
307 *local = *((struct in_addr *) RTA_DATA(attr));
310 if (broadcast != NULL)
311 *broadcast = *((struct in_addr *) RTA_DATA(attr));
315 *label = RTA_DATA(attr);
321 static GSList *ipconfig_list = NULL;
323 static void process_newaddr(int family, int prefixlen, int index,
324 struct ifaddrmsg *msg, int bytes)
328 struct in_addr address;
330 if (family != AF_INET)
333 for (list = ipconfig_list; list; list = list->next) {
334 struct connman_ipconfig *ipconfig = list->data;
336 if (__connman_ipconfig_get_index(ipconfig) != index)
339 extract_addr(msg, bytes, &label, &address, NULL, NULL);
340 __connman_ipconfig_add_address(ipconfig, label, prefixlen,
341 inet_ntoa(address), NULL);
345 static void process_deladdr(int family, int prefixlen, int index,
346 struct ifaddrmsg *msg, int bytes)
350 struct in_addr address;
352 if (family != AF_INET)
355 for (list = ipconfig_list; list; list = list->next) {
356 struct connman_ipconfig *ipconfig = list->data;
358 if (__connman_ipconfig_get_index(ipconfig) != index)
361 extract_addr(msg, bytes, &label, &address, NULL, NULL);
362 __connman_ipconfig_del_address(ipconfig, label, prefixlen,
363 inet_ntoa(address), NULL);
367 int __connman_rtnl_register_ipconfig(struct connman_ipconfig *ipconfig)
369 DBG("ipconfig %p", ipconfig);
371 ipconfig_list = g_slist_append(ipconfig_list, ipconfig);
376 void __connman_rtnl_unregister_ipconfig(struct connman_ipconfig *ipconfig)
378 DBG("ipconfig %p", ipconfig);
380 ipconfig_list = g_slist_remove(ipconfig_list, ipconfig);
383 static inline void print_inet(struct rtattr *attr, const char *name, int family)
385 if (family == AF_INET) {
387 addr = *((struct in_addr *) RTA_DATA(attr));
388 DBG(" attr %s (len %d) %s\n", name,
389 (int) RTA_PAYLOAD(attr), inet_ntoa(addr));
391 DBG(" attr %s (len %d)\n", name, (int) RTA_PAYLOAD(attr));
394 static inline void print_char(struct rtattr *attr, const char *name)
396 DBG(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
397 (char *) RTA_DATA(attr));
400 static inline void print_byte(struct rtattr *attr, const char *name)
402 DBG(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
403 *((unsigned char *) RTA_DATA(attr)));
406 static inline void print_attr(struct rtattr *attr, const char *name)
409 DBG(" attr %s (len %d)\n", name, (int) RTA_PAYLOAD(attr));
411 DBG(" attr %d (len %d)\n",
412 attr->rta_type, (int) RTA_PAYLOAD(attr));
415 static void rtnl_link(struct nlmsghdr *hdr)
418 struct ifinfomsg *msg;
422 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
423 bytes = IFLA_PAYLOAD(hdr);
425 DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
427 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
428 attr = RTA_NEXT(attr, bytes)) {
429 switch (attr->rta_type) {
431 print_attr(attr, "address");
434 print_attr(attr, "broadcast");
437 print_char(attr, "ifname");
440 print_attr(attr, "mtu");
443 print_attr(attr, "link");
446 print_attr(attr, "qdisc");
449 print_attr(attr, "stats");
452 print_attr(attr, "cost");
455 print_attr(attr, "priority");
458 print_attr(attr, "master");
461 print_attr(attr, "wireless");
464 print_attr(attr, "protinfo");
467 print_attr(attr, "txqlen");
470 print_attr(attr, "map");
473 print_attr(attr, "weight");
476 print_byte(attr, "operstate");
479 print_byte(attr, "linkmode");
482 print_attr(attr, NULL);
489 static void rtnl_newlink(struct nlmsghdr *hdr)
491 struct ifinfomsg *msg;
493 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
495 DBG("ifi_type %d ifi_index %d ifi_flags 0x%04x ifi_change 0x%04x",
496 msg->ifi_type, msg->ifi_index,
497 msg->ifi_flags, msg->ifi_change);
499 process_newlink(msg->ifi_type, msg->ifi_index,
500 msg->ifi_flags, msg->ifi_change);
505 static void rtnl_dellink(struct nlmsghdr *hdr)
507 struct ifinfomsg *msg;
509 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
511 DBG("ifi_type %d ifi_index %d ifi_flags 0x%04x ifi_change 0x%04x",
512 msg->ifi_type, msg->ifi_index,
513 msg->ifi_flags, msg->ifi_change);
515 process_dellink(msg->ifi_type, msg->ifi_index,
516 msg->ifi_flags, msg->ifi_change);
521 static void rtnl_addr(struct nlmsghdr *hdr)
524 struct ifaddrmsg *msg;
528 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
529 bytes = IFA_PAYLOAD(hdr);
531 DBG("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
533 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
534 attr = RTA_NEXT(attr, bytes)) {
535 switch (attr->rta_type) {
537 print_inet(attr, "address", msg->ifa_family);
540 print_inet(attr, "local", msg->ifa_family);
543 print_char(attr, "label");
546 print_inet(attr, "broadcast", msg->ifa_family);
549 print_attr(attr, "anycast");
552 print_attr(attr, "cacheinfo");
555 print_attr(attr, "multicast");
558 print_attr(attr, NULL);
565 static void rtnl_newaddr(struct nlmsghdr *hdr)
567 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
569 DBG("ifa_family %d ifa_prefixlen %d ifa_index %d",
570 msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index);
572 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
573 msg, IFA_PAYLOAD(hdr));
578 static void rtnl_deladdr(struct nlmsghdr *hdr)
580 struct ifaddrmsg *msg;
582 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
584 DBG("ifa_family %d ifa_prefixlen %d ifa_index %d",
585 msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index);
587 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
588 msg, IFA_PAYLOAD(hdr));
593 static void rtnl_route(struct nlmsghdr *hdr)
600 msg = (struct rtmsg *) NLMSG_DATA(hdr);
601 bytes = RTM_PAYLOAD(hdr);
603 DBG("rtm_family %d rtm_flags 0x%04x", msg->rtm_family, msg->rtm_flags);
605 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
606 attr = RTA_NEXT(attr, bytes)) {
607 switch (attr->rta_type) {
609 print_inet(attr, "dst", msg->rtm_family);
612 print_inet(attr, "src", msg->rtm_family);
615 print_char(attr, "iif");
618 print_attr(attr, "oif");
621 print_inet(attr, "gateway", msg->rtm_family);
624 print_attr(attr, "priority");
627 print_inet(attr, "prefsrc", msg->rtm_family);
630 print_attr(attr, "metrics");
633 print_attr(attr, "table");
636 print_attr(attr, NULL);
643 static void rtnl_newroute(struct nlmsghdr *hdr)
645 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
647 if (msg->rtm_type == RTN_UNICAST && msg->rtm_table == RT_TABLE_MAIN &&
648 msg->rtm_scope == RT_SCOPE_UNIVERSE) {
649 DBG("rtm_table %d rtm_scope %d rtm_type %d rtm_flags 0x%04x",
650 msg->rtm_table, msg->rtm_scope,
651 msg->rtm_type, msg->rtm_flags);
652 process_newgateway(msg, RTM_PAYLOAD(hdr));
658 static void rtnl_delroute(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_delgateway(msg, RTM_PAYLOAD(hdr));
673 static const char *type2string(uint16_t type)
705 static GIOChannel *channel = NULL;
707 struct rtnl_request {
711 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
713 static GSList *request_list = NULL;
714 static guint32 request_seq = 0;
716 static struct rtnl_request *find_request(guint32 seq)
720 for (list = request_list; list; list = list->next) {
721 struct rtnl_request *req = list->data;
723 if (req->hdr.nlmsg_seq == seq)
730 static int send_request(struct rtnl_request *req)
732 struct sockaddr_nl addr;
735 DBG("%s len %d type %d flags 0x%04x seq %d",
736 type2string(req->hdr.nlmsg_type),
737 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
738 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
740 sk = g_io_channel_unix_get_fd(channel);
742 memset(&addr, 0, sizeof(addr));
743 addr.nl_family = AF_NETLINK;
745 return sendto(sk, req, req->hdr.nlmsg_len, 0,
746 (struct sockaddr *) &addr, sizeof(addr));
749 static int queue_request(struct rtnl_request *req)
751 request_list = g_slist_append(request_list, req);
753 if (g_slist_length(request_list) > 1)
756 return send_request(req);
759 static int process_response(guint32 seq)
761 struct rtnl_request *req;
765 req = find_request(seq);
767 request_list = g_slist_remove(request_list, req);
771 req = g_slist_nth_data(request_list, 0);
775 return send_request(req);
778 static void rtnl_message(void *buf, size_t len)
780 DBG("buf %p len %zd", buf, len);
783 struct nlmsghdr *hdr = buf;
784 struct nlmsgerr *err;
786 if (!NLMSG_OK(hdr, len))
789 DBG("%s len %d type %d flags 0x%04x seq %d",
790 type2string(hdr->nlmsg_type),
791 hdr->nlmsg_len, hdr->nlmsg_type,
792 hdr->nlmsg_flags, hdr->nlmsg_seq);
794 switch (hdr->nlmsg_type) {
799 process_response(hdr->nlmsg_seq);
802 err = NLMSG_DATA(hdr);
803 DBG("error %d (%s)", -err->error,
804 strerror(-err->error));
826 len -= hdr->nlmsg_len;
827 buf += hdr->nlmsg_len;
831 static gboolean netlink_event(GIOChannel *chan,
832 GIOCondition cond, gpointer data)
834 unsigned char buf[4096];
838 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
841 memset(buf, 0, sizeof(buf));
843 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
845 if (err == G_IO_ERROR_AGAIN)
850 rtnl_message(buf, len);
855 int connman_rtnl_send_getlink(void)
857 struct rtnl_request *req;
861 req = g_try_malloc0(RTNL_REQUEST_SIZE);
865 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
866 req->hdr.nlmsg_type = RTM_GETLINK;
867 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
868 req->hdr.nlmsg_pid = 0;
869 req->hdr.nlmsg_seq = request_seq++;
870 req->msg.rtgen_family = AF_INET;
872 return queue_request(req);
875 static int send_getaddr(void)
877 struct rtnl_request *req;
881 req = g_try_malloc0(RTNL_REQUEST_SIZE);
885 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
886 req->hdr.nlmsg_type = RTM_GETADDR;
887 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
888 req->hdr.nlmsg_pid = 0;
889 req->hdr.nlmsg_seq = request_seq++;
890 req->msg.rtgen_family = AF_INET;
892 return queue_request(req);
895 int connman_rtnl_send_getroute(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_GETROUTE;
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 int __connman_rtnl_init(void)
917 struct sockaddr_nl addr;
922 ipconfig_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
923 NULL, free_ipconfig);
925 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
929 memset(&addr, 0, sizeof(addr));
930 addr.nl_family = AF_NETLINK;
931 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
933 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
938 channel = g_io_channel_unix_new(sk);
939 g_io_channel_set_close_on_unref(channel, TRUE);
941 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
942 netlink_event, NULL);
947 void __connman_rtnl_start(void)
951 connman_rtnl_send_getlink();
953 connman_rtnl_send_getroute();
956 void __connman_rtnl_cleanup(void)
962 g_slist_free(ipconfig_list);
963 ipconfig_list = NULL;
965 g_hash_table_destroy(ipconfig_hash);
966 ipconfig_hash = NULL;
968 for (list = watch_list; list; list = list->next) {
969 struct watch_data *watch = list->data;
971 DBG("removing watch %d", watch->id);
977 g_slist_free(watch_list);
980 for (list = request_list; list; list = list->next) {
981 struct rtnl_request *req = list->data;
983 DBG("%s len %d type %d flags 0x%04x seq %d",
984 type2string(req->hdr.nlmsg_type),
985 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
986 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
992 g_slist_free(request_list);
995 g_io_channel_shutdown(channel, TRUE, NULL);
996 g_io_channel_unref(channel);