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, &index);
171 if (ipconfig == NULL) {
172 ipconfig = connman_ipconfig_create(index);
173 if (ipconfig != NULL) {
174 g_hash_table_insert(ipconfig_hash,
177 __connman_rtnl_register_ipconfig(ipconfig);
179 __connman_ipconfig_update_link(ipconfig,
186 for (list = rtnl_list; list; list = list->next) {
187 struct connman_rtnl *rtnl = list->data;
190 rtnl->newlink(type, index, flags, change);
193 for (list = watch_list; list; list = list->next) {
194 struct watch_data *watch = list->data;
196 if (watch->index != index)
200 watch->newlink(flags, change, watch->user_data);
204 static void process_dellink(unsigned short type, int index,
205 unsigned flags, unsigned change)
209 for (list = rtnl_list; list; list = list->next) {
210 struct connman_rtnl *rtnl = list->data;
213 rtnl->dellink(type, index, flags, change);
218 case ARPHRD_LOOPBACK:
220 g_hash_table_remove(ipconfig_hash, &index);
225 static char *extract_gateway(struct rtmsg *msg, int bytes, int *index)
227 char *gateway = NULL;
231 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
232 attr = RTA_NEXT(attr, bytes)) {
233 switch (attr->rta_type) {
235 addr = *((struct in_addr *) RTA_DATA(attr));
237 gateway = g_strdup(inet_ntoa(addr));
240 *index = *((int *) RTA_DATA(attr));
248 static void process_newgateway(struct rtmsg *msg, int bytes)
254 gateway = extract_gateway(msg, bytes, &index);
255 if (gateway == NULL || index < 0)
258 for (list = rtnl_list; list; list = list->next) {
259 struct connman_rtnl *rtnl = list->data;
261 if (rtnl->newgateway)
262 rtnl->newgateway(index, gateway);
268 static void process_delgateway(struct rtmsg *msg, int bytes)
274 gateway = extract_gateway(msg, bytes, &index);
275 if (gateway == NULL || index < 0)
278 for (list = rtnl_list; list; list = list->next) {
279 struct connman_rtnl *rtnl = list->data;
281 if (rtnl->delgateway)
282 rtnl->delgateway(index, gateway);
288 static void extract_addr(struct ifaddrmsg *msg, int bytes,
290 struct in_addr *local,
291 struct in_addr *address,
292 struct in_addr *broadcast)
296 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
297 attr = RTA_NEXT(attr, bytes)) {
298 switch (attr->rta_type) {
301 *address = *((struct in_addr *) RTA_DATA(attr));
305 *local = *((struct in_addr *) RTA_DATA(attr));
308 if (broadcast != NULL)
309 *broadcast = *((struct in_addr *) RTA_DATA(attr));
313 *label = RTA_DATA(attr);
319 static GSList *ipconfig_list = NULL;
321 static void process_newaddr(int family, int prefixlen, int index,
322 struct ifaddrmsg *msg, int bytes)
326 struct in_addr address;
328 if (family != AF_INET)
331 for (list = ipconfig_list; list; list = list->next) {
332 struct connman_ipconfig *ipconfig = list->data;
334 if (__connman_ipconfig_get_index(ipconfig) != index)
337 extract_addr(msg, bytes, &label, &address, NULL, NULL);
338 __connman_ipconfig_add_address(ipconfig, label, prefixlen,
339 inet_ntoa(address), NULL);
343 static void process_deladdr(int family, int prefixlen, int index,
344 struct ifaddrmsg *msg, int bytes)
348 struct in_addr address;
350 if (family != AF_INET)
353 for (list = ipconfig_list; list; list = list->next) {
354 struct connman_ipconfig *ipconfig = list->data;
356 if (__connman_ipconfig_get_index(ipconfig) != index)
359 extract_addr(msg, bytes, &label, &address, NULL, NULL);
360 __connman_ipconfig_del_address(ipconfig, label, prefixlen,
361 inet_ntoa(address), NULL);
365 int __connman_rtnl_register_ipconfig(struct connman_ipconfig *ipconfig)
367 DBG("ipconfig %p", ipconfig);
369 ipconfig_list = g_slist_append(ipconfig_list, ipconfig);
374 void __connman_rtnl_unregister_ipconfig(struct connman_ipconfig *ipconfig)
376 DBG("ipconfig %p", ipconfig);
378 ipconfig_list = g_slist_remove(ipconfig_list, ipconfig);
381 static inline void print_inet(struct rtattr *attr, const char *name, int family)
383 if (family == AF_INET) {
385 addr = *((struct in_addr *) RTA_DATA(attr));
386 DBG(" attr %s (len %d) %s\n", name,
387 (int) RTA_PAYLOAD(attr), inet_ntoa(addr));
389 DBG(" attr %s (len %d)\n", name, (int) RTA_PAYLOAD(attr));
392 static inline void print_char(struct rtattr *attr, const char *name)
394 DBG(" attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
395 (char *) RTA_DATA(attr));
398 static inline void print_byte(struct rtattr *attr, const char *name)
400 DBG(" attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
401 *((unsigned char *) RTA_DATA(attr)));
404 static inline void print_attr(struct rtattr *attr, const char *name)
407 DBG(" attr %s (len %d)\n", name, (int) RTA_PAYLOAD(attr));
409 DBG(" attr %d (len %d)\n",
410 attr->rta_type, (int) RTA_PAYLOAD(attr));
413 static void rtnl_link(struct nlmsghdr *hdr)
416 struct ifinfomsg *msg;
420 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
421 bytes = IFLA_PAYLOAD(hdr);
423 DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
425 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
426 attr = RTA_NEXT(attr, bytes)) {
427 switch (attr->rta_type) {
429 print_attr(attr, "address");
432 print_attr(attr, "broadcast");
435 print_char(attr, "ifname");
438 print_attr(attr, "mtu");
441 print_attr(attr, "link");
444 print_attr(attr, "qdisc");
447 print_attr(attr, "stats");
450 print_attr(attr, "cost");
453 print_attr(attr, "priority");
456 print_attr(attr, "master");
459 print_attr(attr, "wireless");
462 print_attr(attr, "protinfo");
465 print_attr(attr, "txqlen");
468 print_attr(attr, "map");
471 print_attr(attr, "weight");
474 print_byte(attr, "operstate");
477 print_byte(attr, "linkmode");
480 print_attr(attr, NULL);
487 static void rtnl_newlink(struct nlmsghdr *hdr)
489 struct ifinfomsg *msg;
491 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
493 DBG("ifi_type %d ifi_index %d ifi_flags 0x%04x ifi_change 0x%04x",
494 msg->ifi_type, msg->ifi_index,
495 msg->ifi_flags, msg->ifi_change);
497 process_newlink(msg->ifi_type, msg->ifi_index,
498 msg->ifi_flags, msg->ifi_change);
503 static void rtnl_dellink(struct nlmsghdr *hdr)
505 struct ifinfomsg *msg;
507 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
509 DBG("ifi_type %d ifi_index %d ifi_flags 0x%04x ifi_change 0x%04x",
510 msg->ifi_type, msg->ifi_index,
511 msg->ifi_flags, msg->ifi_change);
513 process_dellink(msg->ifi_type, msg->ifi_index,
514 msg->ifi_flags, msg->ifi_change);
519 static void rtnl_addr(struct nlmsghdr *hdr)
522 struct ifaddrmsg *msg;
526 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
527 bytes = IFA_PAYLOAD(hdr);
529 DBG("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
531 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
532 attr = RTA_NEXT(attr, bytes)) {
533 switch (attr->rta_type) {
535 print_inet(attr, "address", msg->ifa_family);
538 print_inet(attr, "local", msg->ifa_family);
541 print_char(attr, "label");
544 print_inet(attr, "broadcast", msg->ifa_family);
547 print_attr(attr, "anycast");
550 print_attr(attr, "cacheinfo");
553 print_attr(attr, "multicast");
556 print_attr(attr, NULL);
563 static void rtnl_newaddr(struct nlmsghdr *hdr)
565 struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
567 DBG("ifa_family %d ifa_prefixlen %d ifa_index %d",
568 msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index);
570 process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
571 msg, IFA_PAYLOAD(hdr));
576 static void rtnl_deladdr(struct nlmsghdr *hdr)
578 struct ifaddrmsg *msg;
580 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
582 DBG("ifa_family %d ifa_prefixlen %d ifa_index %d",
583 msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index);
585 process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
586 msg, IFA_PAYLOAD(hdr));
591 static void rtnl_route(struct nlmsghdr *hdr)
598 msg = (struct rtmsg *) NLMSG_DATA(hdr);
599 bytes = RTM_PAYLOAD(hdr);
601 DBG("rtm_family %d rtm_flags 0x%04x", msg->rtm_family, msg->rtm_flags);
603 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
604 attr = RTA_NEXT(attr, bytes)) {
605 switch (attr->rta_type) {
607 print_inet(attr, "dst", msg->rtm_family);
610 print_inet(attr, "src", msg->rtm_family);
613 print_char(attr, "iif");
616 print_attr(attr, "oif");
619 print_inet(attr, "gateway", msg->rtm_family);
622 print_attr(attr, "priority");
625 print_inet(attr, "prefsrc", msg->rtm_family);
628 print_attr(attr, "metrics");
631 print_attr(attr, "table");
634 print_attr(attr, NULL);
641 static void rtnl_newroute(struct nlmsghdr *hdr)
643 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
645 if (msg->rtm_type == RTN_UNICAST && msg->rtm_table == RT_TABLE_MAIN &&
646 msg->rtm_scope == RT_SCOPE_UNIVERSE) {
647 DBG("rtm_table %d rtm_scope %d rtm_type %d rtm_flags 0x%04x",
648 msg->rtm_table, msg->rtm_scope,
649 msg->rtm_type, msg->rtm_flags);
650 process_newgateway(msg, RTM_PAYLOAD(hdr));
656 static void rtnl_delroute(struct nlmsghdr *hdr)
658 struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
660 if (msg->rtm_type == RTN_UNICAST && msg->rtm_table == RT_TABLE_MAIN &&
661 msg->rtm_scope == RT_SCOPE_UNIVERSE) {
662 DBG("rtm_table %d rtm_scope %d rtm_type %d rtm_flags 0x%04x",
663 msg->rtm_table, msg->rtm_scope,
664 msg->rtm_type, msg->rtm_flags);
665 process_delgateway(msg, RTM_PAYLOAD(hdr));
671 static const char *type2string(uint16_t type)
703 static GIOChannel *channel = NULL;
705 struct rtnl_request {
709 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
711 static GSList *request_list = NULL;
712 static guint32 request_seq = 0;
714 static struct rtnl_request *find_request(guint32 seq)
718 for (list = request_list; list; list = list->next) {
719 struct rtnl_request *req = list->data;
721 if (req->hdr.nlmsg_seq == seq)
728 static int send_request(struct rtnl_request *req)
730 struct sockaddr_nl addr;
733 DBG("%s len %d type %d flags 0x%04x seq %d",
734 type2string(req->hdr.nlmsg_type),
735 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
736 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
738 sk = g_io_channel_unix_get_fd(channel);
740 memset(&addr, 0, sizeof(addr));
741 addr.nl_family = AF_NETLINK;
743 return sendto(sk, req, req->hdr.nlmsg_len, 0,
744 (struct sockaddr *) &addr, sizeof(addr));
747 static int queue_request(struct rtnl_request *req)
749 request_list = g_slist_append(request_list, req);
751 if (g_slist_length(request_list) > 1)
754 return send_request(req);
757 static int process_response(guint32 seq)
759 struct rtnl_request *req;
763 req = find_request(seq);
765 request_list = g_slist_remove(request_list, req);
769 req = g_slist_nth_data(request_list, 0);
773 return send_request(req);
776 static void rtnl_message(void *buf, size_t len)
778 DBG("buf %p len %zd", buf, len);
781 struct nlmsghdr *hdr = buf;
782 struct nlmsgerr *err;
784 if (!NLMSG_OK(hdr, len))
787 DBG("%s len %d type %d flags 0x%04x seq %d",
788 type2string(hdr->nlmsg_type),
789 hdr->nlmsg_len, hdr->nlmsg_type,
790 hdr->nlmsg_flags, hdr->nlmsg_seq);
792 switch (hdr->nlmsg_type) {
797 process_response(hdr->nlmsg_seq);
800 err = NLMSG_DATA(hdr);
801 DBG("error %d (%s)", -err->error,
802 strerror(-err->error));
824 len -= hdr->nlmsg_len;
825 buf += hdr->nlmsg_len;
829 static gboolean netlink_event(GIOChannel *chan,
830 GIOCondition cond, gpointer data)
832 unsigned char buf[4096];
836 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
839 memset(buf, 0, sizeof(buf));
841 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
843 if (err == G_IO_ERROR_AGAIN)
848 rtnl_message(buf, len);
853 int connman_rtnl_send_getlink(void)
855 struct rtnl_request *req;
859 req = g_try_malloc0(RTNL_REQUEST_SIZE);
863 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
864 req->hdr.nlmsg_type = RTM_GETLINK;
865 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
866 req->hdr.nlmsg_pid = 0;
867 req->hdr.nlmsg_seq = request_seq++;
868 req->msg.rtgen_family = AF_INET;
870 return queue_request(req);
873 static int send_getaddr(void)
875 struct rtnl_request *req;
879 req = g_try_malloc0(RTNL_REQUEST_SIZE);
883 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
884 req->hdr.nlmsg_type = RTM_GETADDR;
885 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
886 req->hdr.nlmsg_pid = 0;
887 req->hdr.nlmsg_seq = request_seq++;
888 req->msg.rtgen_family = AF_INET;
890 return queue_request(req);
893 int connman_rtnl_send_getroute(void)
895 struct rtnl_request *req;
899 req = g_try_malloc0(RTNL_REQUEST_SIZE);
903 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
904 req->hdr.nlmsg_type = RTM_GETROUTE;
905 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
906 req->hdr.nlmsg_pid = 0;
907 req->hdr.nlmsg_seq = request_seq++;
908 req->msg.rtgen_family = AF_INET;
910 return queue_request(req);
913 int __connman_rtnl_init(void)
915 struct sockaddr_nl addr;
920 ipconfig_hash = g_hash_table_new_full(g_int_hash, g_int_equal,
921 NULL, free_ipconfig);
923 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
927 memset(&addr, 0, sizeof(addr));
928 addr.nl_family = AF_NETLINK;
929 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
931 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
936 channel = g_io_channel_unix_new(sk);
937 g_io_channel_set_close_on_unref(channel, TRUE);
939 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
940 netlink_event, NULL);
945 void __connman_rtnl_start(void)
949 connman_rtnl_send_getlink();
953 void __connman_rtnl_cleanup(void)
959 g_slist_free(ipconfig_list);
960 ipconfig_list = NULL;
962 g_hash_table_destroy(ipconfig_hash);
963 ipconfig_hash = NULL;
965 for (list = watch_list; list; list = list->next) {
966 struct watch_data *watch = list->data;
968 DBG("removing watch %d", watch->id);
974 g_slist_free(watch_list);
977 for (list = request_list; list; list = list->next) {
978 struct rtnl_request *req = list->data;
980 DBG("%s len %d type %d flags 0x%04x seq %d",
981 type2string(req->hdr.nlmsg_type),
982 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
983 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
989 g_slist_free(request_list);
992 g_io_channel_shutdown(channel, TRUE, NULL);
993 g_io_channel_unref(channel);