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 DBG("index %d", index);
87 for (list = rtnl_list; list; list = list->next) {
88 struct connman_rtnl *rtnl = list->data;
91 rtnl->newlink(type, index, flags, change);
95 static void process_dellink(unsigned short type, int index,
96 unsigned flags, unsigned change)
100 DBG("index %d", index);
102 for (list = rtnl_list; list; list = list->next) {
103 struct connman_rtnl *rtnl = list->data;
106 rtnl->dellink(type, index, flags, change);
110 static inline void print_inet(struct rtattr *attr, const char *name, int family)
112 if (family == AF_INET) {
114 addr = *((struct in_addr *) RTA_DATA(attr));
115 DBG(" attr %s (len %jd) %s\n",
116 name, RTA_PAYLOAD(attr), inet_ntoa(addr));
118 DBG(" attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
121 static inline void print_char(struct rtattr *attr, const char *name)
123 DBG(" attr %s (len %jd) %s\n", name, RTA_PAYLOAD(attr),
124 (char *) RTA_DATA(attr));
127 static inline void print_byte(struct rtattr *attr, const char *name)
129 DBG(" attr %s (len %jd) 0x%02x\n", name, RTA_PAYLOAD(attr),
130 *((unsigned char *) RTA_DATA(attr)));
133 static inline void print_attr(struct rtattr *attr, const char *name)
136 DBG(" attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
138 DBG(" attr %d (len %jd)\n",
139 attr->rta_type, RTA_PAYLOAD(attr));
142 static void rtnl_link(struct nlmsghdr *hdr)
145 struct ifinfomsg *msg;
149 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
150 bytes = IFLA_PAYLOAD(hdr);
152 DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
154 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
155 attr = RTA_NEXT(attr, bytes)) {
156 switch (attr->rta_type) {
158 print_attr(attr, "address");
161 print_attr(attr, "broadcast");
164 print_char(attr, "ifname");
167 print_attr(attr, "mtu");
170 print_attr(attr, "link");
173 print_attr(attr, "qdisc");
176 print_attr(attr, "stats");
179 print_attr(attr, "cost");
182 print_attr(attr, "priority");
185 print_attr(attr, "master");
188 print_attr(attr, "wireless");
191 print_attr(attr, "protinfo");
194 print_attr(attr, "txqlen");
197 print_attr(attr, "map");
200 print_attr(attr, "weight");
203 print_byte(attr, "operstate");
206 print_byte(attr, "linkmode");
209 print_attr(attr, NULL);
216 static void rtnl_newlink(struct nlmsghdr *hdr)
218 struct ifinfomsg *msg;
220 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
222 DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
224 process_newlink(msg->ifi_type, msg->ifi_index,
225 msg->ifi_flags, msg->ifi_change);
230 static void rtnl_dellink(struct nlmsghdr *hdr)
232 struct ifinfomsg *msg;
234 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
236 DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
238 process_dellink(msg->ifi_type, msg->ifi_index,
239 msg->ifi_flags, msg->ifi_change);
244 static void rtnl_addr(struct nlmsghdr *hdr)
246 struct ifaddrmsg *msg;
250 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
251 bytes = IFA_PAYLOAD(hdr);
253 DBG("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
255 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
256 attr = RTA_NEXT(attr, bytes)) {
257 switch (attr->rta_type) {
259 print_inet(attr, "address", msg->ifa_family);
262 print_inet(attr, "local", msg->ifa_family);
265 print_char(attr, "label");
268 print_inet(attr, "broadcast", msg->ifa_family);
271 print_attr(attr, "anycast");
274 print_attr(attr, "cacheinfo");
277 print_attr(attr, "multicast");
280 print_attr(attr, NULL);
286 static void rtnl_route(struct nlmsghdr *hdr)
292 msg = (struct rtmsg *) NLMSG_DATA(hdr);
293 bytes = RTM_PAYLOAD(hdr);
295 DBG("rtm_family %d rtm_flags 0x%04x", msg->rtm_family, msg->rtm_flags);
297 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
298 attr = RTA_NEXT(attr, bytes)) {
299 switch (attr->rta_type) {
301 print_inet(attr, "dst", msg->rtm_family);
304 print_inet(attr, "src", msg->rtm_family);
307 print_char(attr, "iif");
310 print_attr(attr, "oif");
313 print_inet(attr, "gateway", msg->rtm_family);
316 print_attr(attr, "priority");
319 print_inet(attr, "prefsrc", msg->rtm_family);
322 print_attr(attr, "metrics");
325 print_attr(attr, "table");
328 print_attr(attr, NULL);
334 static void rtnl_message(void *buf, size_t len)
336 DBG("buf %p len %zd", buf, len);
339 struct nlmsghdr *hdr = buf;
340 struct nlmsgerr *err;
342 if (!NLMSG_OK(hdr, len))
345 DBG("len %d type %d flags 0x%04x seq %d",
346 hdr->nlmsg_len, hdr->nlmsg_type,
347 hdr->nlmsg_flags, hdr->nlmsg_seq);
349 switch (hdr->nlmsg_type) {
354 err = NLMSG_DATA(hdr);
355 DBG("ERROR %d (%s)", -err->error,
356 strerror(-err->error));
389 DBG("type %d", hdr->nlmsg_type);
393 len -= hdr->nlmsg_len;
394 buf += hdr->nlmsg_len;
398 static gboolean netlink_event(GIOChannel *chan,
399 GIOCondition cond, gpointer data)
401 unsigned char buf[4096];
405 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
408 memset(buf, 0, sizeof(buf));
410 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
412 if (err == G_IO_ERROR_AGAIN)
417 rtnl_message(buf, len);
422 static GIOChannel *channel = NULL;
424 int __connman_rtnl_send(const void *buf, size_t len)
426 struct sockaddr_nl addr;
429 DBG("buf %p len %zd", buf, len);
431 sk = g_io_channel_unix_get_fd(channel);
433 memset(&addr, 0, sizeof(addr));
434 addr.nl_family = AF_NETLINK;
436 return sendto(sk, buf, len, 0,
437 (struct sockaddr *) &addr, sizeof(addr));
440 int connman_rtnl_send_getlink(void)
449 memset(&req, 0, sizeof(req));
450 req.hdr.nlmsg_len = sizeof(req.hdr) + sizeof(req.msg);
451 req.hdr.nlmsg_type = RTM_GETLINK;
452 req.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
453 req.hdr.nlmsg_pid = 0;
454 req.hdr.nlmsg_seq = 42;
455 req.msg.rtgen_family = AF_INET;
457 return __connman_rtnl_send(&req, sizeof(req));
460 int __connman_rtnl_init(void)
462 struct sockaddr_nl addr;
467 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
471 memset(&addr, 0, sizeof(addr));
472 addr.nl_family = AF_NETLINK;
473 addr.nl_groups = RTMGRP_LINK;
474 //addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
475 //addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
477 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
482 channel = g_io_channel_unix_new(sk);
483 g_io_channel_set_close_on_unref(channel, TRUE);
485 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
486 netlink_event, NULL);
491 void __connman_rtnl_cleanup(void)
495 g_io_channel_shutdown(channel, TRUE, NULL);
496 g_io_channel_unref(channel);