5 * Copyright (C) 2007-2008 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/netlink.h>
33 #include <linux/rtnetlink.h>
39 static GSList *rtnl_list = NULL;
41 static gint compare_priority(gconstpointer a, gconstpointer b)
43 const struct connman_rtnl *rtnl1 = a;
44 const struct connman_rtnl *rtnl2 = b;
46 return rtnl2->priority - rtnl1->priority;
50 * connman_rtnl_register:
53 * Register a new RTNL module
55 * Returns: %0 on success
57 int connman_rtnl_register(struct connman_rtnl *rtnl)
59 DBG("rtnl %p name %s", rtnl, rtnl->name);
61 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
68 * connman_rtnl_unregister:
71 * Remove a previously registered RTNL module
73 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
75 DBG("rtnl %p name %s", rtnl, rtnl->name);
77 rtnl_list = g_slist_remove(rtnl_list, rtnl);
80 static void process_newlink(unsigned short type, int index,
81 unsigned flags, unsigned change)
85 for (list = rtnl_list; list; list = list->next) {
86 struct connman_rtnl *rtnl = list->data;
89 rtnl->newlink(type, index, flags, change);
93 static void process_dellink(unsigned short type, int index,
94 unsigned flags, unsigned change)
98 for (list = rtnl_list; list; list = list->next) {
99 struct connman_rtnl *rtnl = list->data;
102 rtnl->dellink(type, index, flags, change);
106 static char *extract_gateway(struct rtmsg *msg, int bytes, int *index)
108 char *gateway = NULL;
112 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
113 attr = RTA_NEXT(attr, bytes)) {
114 switch (attr->rta_type) {
116 addr = *((struct in_addr *) RTA_DATA(attr));
118 gateway = g_strdup(inet_ntoa(addr));
121 *index = *((int *) RTA_DATA(attr));
129 static void process_newgateway(struct rtmsg *msg, int bytes)
135 gateway = extract_gateway(msg, bytes, &index);
136 if (gateway == NULL || index < 0)
139 for (list = rtnl_list; list; list = list->next) {
140 struct connman_rtnl *rtnl = list->data;
142 if (rtnl->newgateway)
143 rtnl->newgateway(index, gateway);
149 static void process_delgateway(struct rtmsg *msg, int bytes)
155 gateway = extract_gateway(msg, bytes, &index);
156 if (gateway == NULL || index < 0)
159 for (list = rtnl_list; list; list = list->next) {
160 struct connman_rtnl *rtnl = list->data;
162 if (rtnl->delgateway)
163 rtnl->delgateway(index, gateway);
169 static inline void print_inet(struct rtattr *attr, const char *name, int family)
171 if (family == AF_INET) {
173 addr = *((struct in_addr *) RTA_DATA(attr));
174 DBG(" attr %s (len %jd) %s\n",
175 name, RTA_PAYLOAD(attr), inet_ntoa(addr));
177 DBG(" attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
180 static inline void print_char(struct rtattr *attr, const char *name)
182 DBG(" attr %s (len %jd) %s\n", name, RTA_PAYLOAD(attr),
183 (char *) RTA_DATA(attr));
186 static inline void print_byte(struct rtattr *attr, const char *name)
188 DBG(" attr %s (len %jd) 0x%02x\n", name, RTA_PAYLOAD(attr),
189 *((unsigned char *) RTA_DATA(attr)));
192 static inline void print_attr(struct rtattr *attr, const char *name)
195 DBG(" attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
197 DBG(" attr %d (len %jd)\n",
198 attr->rta_type, RTA_PAYLOAD(attr));
201 static void rtnl_link(struct nlmsghdr *hdr)
204 struct ifinfomsg *msg;
208 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
209 bytes = IFLA_PAYLOAD(hdr);
211 DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
213 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
214 attr = RTA_NEXT(attr, bytes)) {
215 switch (attr->rta_type) {
217 print_attr(attr, "address");
220 print_attr(attr, "broadcast");
223 print_char(attr, "ifname");
226 print_attr(attr, "mtu");
229 print_attr(attr, "link");
232 print_attr(attr, "qdisc");
235 print_attr(attr, "stats");
238 print_attr(attr, "cost");
241 print_attr(attr, "priority");
244 print_attr(attr, "master");
247 print_attr(attr, "wireless");
250 print_attr(attr, "protinfo");
253 print_attr(attr, "txqlen");
256 print_attr(attr, "map");
259 print_attr(attr, "weight");
262 print_byte(attr, "operstate");
265 print_byte(attr, "linkmode");
268 print_attr(attr, NULL);
275 static void rtnl_newlink(struct nlmsghdr *hdr)
277 struct ifinfomsg *msg;
279 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
281 DBG("ifi_type %d ifi_index %d ifi_flags 0x%04x ifi_change 0x%04x",
282 msg->ifi_type, msg->ifi_index,
283 msg->ifi_flags, msg->ifi_change);
285 process_newlink(msg->ifi_type, msg->ifi_index,
286 msg->ifi_flags, msg->ifi_change);
291 static void rtnl_dellink(struct nlmsghdr *hdr)
293 struct ifinfomsg *msg;
295 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
297 DBG("ifi_type %d ifi_index %d ifi_flags 0x%04x ifi_change 0x%04x",
298 msg->ifi_type, msg->ifi_index,
299 msg->ifi_flags, msg->ifi_change);
301 process_dellink(msg->ifi_type, msg->ifi_index,
302 msg->ifi_flags, msg->ifi_change);
307 static void rtnl_addr(struct nlmsghdr *hdr)
309 struct ifaddrmsg *msg;
313 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
314 bytes = IFA_PAYLOAD(hdr);
316 DBG("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
318 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
319 attr = RTA_NEXT(attr, bytes)) {
320 switch (attr->rta_type) {
322 print_inet(attr, "address", msg->ifa_family);
325 print_inet(attr, "local", msg->ifa_family);
328 print_char(attr, "label");
331 print_inet(attr, "broadcast", msg->ifa_family);
334 print_attr(attr, "anycast");
337 print_attr(attr, "cacheinfo");
340 print_attr(attr, "multicast");
343 print_attr(attr, NULL);
349 static void rtnl_route(struct nlmsghdr *hdr)
356 msg = (struct rtmsg *) NLMSG_DATA(hdr);
357 bytes = RTM_PAYLOAD(hdr);
359 DBG("rtm_family %d rtm_flags 0x%04x", msg->rtm_family, msg->rtm_flags);
361 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
362 attr = RTA_NEXT(attr, bytes)) {
363 switch (attr->rta_type) {
365 print_inet(attr, "dst", msg->rtm_family);
368 print_inet(attr, "src", msg->rtm_family);
371 print_char(attr, "iif");
374 print_attr(attr, "oif");
377 print_inet(attr, "gateway", msg->rtm_family);
380 print_attr(attr, "priority");
383 print_inet(attr, "prefsrc", msg->rtm_family);
386 print_attr(attr, "metrics");
389 print_attr(attr, "table");
392 print_attr(attr, NULL);
399 static void rtnl_newroute(struct nlmsghdr *hdr)
403 msg = (struct rtmsg *) NLMSG_DATA(hdr);
405 if (msg->rtm_type == RTN_UNICAST && msg->rtm_table == RT_TABLE_MAIN &&
406 msg->rtm_scope == RT_SCOPE_UNIVERSE) {
407 DBG("rtm_table %d rtm_scope %d rtm_type %d rtm_flags 0x%04x",
408 msg->rtm_table, msg->rtm_scope,
409 msg->rtm_type, msg->rtm_flags);
410 process_newgateway(msg, RTM_PAYLOAD(hdr));
416 static void rtnl_delroute(struct nlmsghdr *hdr)
420 msg = (struct rtmsg *) NLMSG_DATA(hdr);
422 if (msg->rtm_type == RTN_UNICAST && msg->rtm_table == RT_TABLE_MAIN &&
423 msg->rtm_scope == RT_SCOPE_UNIVERSE) {
424 DBG("rtm_table %d rtm_scope %d rtm_type %d rtm_flags 0x%04x",
425 msg->rtm_table, msg->rtm_scope,
426 msg->rtm_type, msg->rtm_flags);
427 process_delgateway(msg, RTM_PAYLOAD(hdr));
433 static const char *type2string(uint16_t type)
465 static GIOChannel *channel = NULL;
467 struct rtnl_request {
471 #define RTNL_REQUEST_SIZE (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
473 static GSList *request_list = NULL;
474 static guint32 request_seq = 0;
476 static struct rtnl_request *find_request(guint32 seq)
480 for (list = request_list; list; list = list->next) {
481 struct rtnl_request *req = list->data;
483 if (req->hdr.nlmsg_seq == seq)
490 static int send_request(struct rtnl_request *req)
492 struct sockaddr_nl addr;
495 DBG("%s len %d type %d flags 0x%04x seq %d",
496 type2string(req->hdr.nlmsg_type),
497 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
498 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
500 sk = g_io_channel_unix_get_fd(channel);
502 memset(&addr, 0, sizeof(addr));
503 addr.nl_family = AF_NETLINK;
505 return sendto(sk, req, req->hdr.nlmsg_len, 0,
506 (struct sockaddr *) &addr, sizeof(addr));
509 static int queue_request(struct rtnl_request *req)
511 request_list = g_slist_append(request_list, req);
513 if (g_slist_length(request_list) > 1)
516 return send_request(req);
519 static int process_response(guint32 seq)
521 struct rtnl_request *req;
525 req = find_request(seq);
527 request_list = g_slist_remove(request_list, req);
531 req = g_slist_nth_data(request_list, 0);
535 return send_request(req);
538 static void rtnl_message(void *buf, size_t len)
540 DBG("buf %p len %zd", buf, len);
543 struct nlmsghdr *hdr = buf;
544 struct nlmsgerr *err;
546 if (!NLMSG_OK(hdr, len))
549 DBG("%s len %d type %d flags 0x%04x seq %d",
550 type2string(hdr->nlmsg_type),
551 hdr->nlmsg_len, hdr->nlmsg_type,
552 hdr->nlmsg_flags, hdr->nlmsg_seq);
554 switch (hdr->nlmsg_type) {
559 process_response(hdr->nlmsg_seq);
562 err = NLMSG_DATA(hdr);
563 DBG("error %d (%s)", -err->error,
564 strerror(-err->error));
584 len -= hdr->nlmsg_len;
585 buf += hdr->nlmsg_len;
589 static gboolean netlink_event(GIOChannel *chan,
590 GIOCondition cond, gpointer data)
592 unsigned char buf[4096];
596 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
599 memset(buf, 0, sizeof(buf));
601 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
603 if (err == G_IO_ERROR_AGAIN)
608 rtnl_message(buf, len);
613 int connman_rtnl_send_getlink(void)
615 struct rtnl_request *req;
619 req = g_try_malloc0(RTNL_REQUEST_SIZE);
623 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
624 req->hdr.nlmsg_type = RTM_GETLINK;
625 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
626 req->hdr.nlmsg_pid = 0;
627 req->hdr.nlmsg_seq = request_seq++;
628 req->msg.rtgen_family = AF_INET;
630 return queue_request(req);
633 int connman_rtnl_send_getroute(void)
635 struct rtnl_request *req;
639 req = g_try_malloc0(RTNL_REQUEST_SIZE);
643 req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
644 req->hdr.nlmsg_type = RTM_GETROUTE;
645 req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
646 req->hdr.nlmsg_pid = 0;
647 req->hdr.nlmsg_seq = request_seq++;
648 req->msg.rtgen_family = AF_INET;
650 return queue_request(req);
653 int __connman_rtnl_init(void)
655 struct sockaddr_nl addr;
660 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
664 memset(&addr, 0, sizeof(addr));
665 addr.nl_family = AF_NETLINK;
666 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_ROUTE;
667 //addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
668 //addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
670 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
675 channel = g_io_channel_unix_new(sk);
676 g_io_channel_set_close_on_unref(channel, TRUE);
678 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
679 netlink_event, NULL);
684 void __connman_rtnl_cleanup(void)
690 for (list = request_list; list; list = list->next) {
691 struct rtnl_request *req = list->data;
693 DBG("%s len %d type %d flags 0x%04x seq %d",
694 type2string(req->hdr.nlmsg_type),
695 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
696 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
702 g_slist_free(request_list);
705 g_io_channel_shutdown(channel, TRUE, NULL);
706 g_io_channel_unref(channel);