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 GStaticRWLock rtnl_lock = G_STATIC_RW_LOCK_INIT;
40 static GSList *rtnl_list = NULL;
42 static gint compare_priority(gconstpointer a, gconstpointer b)
44 const struct connman_rtnl *rtnl1 = a;
45 const struct connman_rtnl *rtnl2 = b;
47 return rtnl2->priority - rtnl1->priority;
51 * connman_rtnl_register:
54 * Register a new RTNL module
56 * Returns: %0 on success
58 int connman_rtnl_register(struct connman_rtnl *rtnl)
60 DBG("rtnl %p name %s", rtnl, rtnl->name);
62 g_static_rw_lock_writer_lock(&rtnl_lock);
64 rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
67 g_static_rw_lock_writer_unlock(&rtnl_lock);
73 * connman_rtnl_unregister:
76 * Remove a previously registered RTNL module
78 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
80 DBG("rtnl %p name %s", rtnl, rtnl->name);
82 g_static_rw_lock_writer_lock(&rtnl_lock);
84 rtnl_list = g_slist_remove(rtnl_list, rtnl);
86 g_static_rw_lock_writer_unlock(&rtnl_lock);
89 static void process_link_flags(int index, short flags)
93 DBG("idex %d", index);
95 g_static_rw_lock_reader_lock(&rtnl_lock);
97 for (list = rtnl_list; list; list = list->next) {
98 struct connman_rtnl *rtnl = list->data;
100 if (rtnl->link_flags)
101 rtnl->link_flags(index, flags);
104 g_static_rw_lock_reader_unlock(&rtnl_lock);
107 static inline void print_inet(struct rtattr *attr, const char *name, int family)
109 if (family == AF_INET) {
111 addr = *((struct in_addr *) RTA_DATA(attr));
112 DBG(" attr %s (len %jd) %s\n",
113 name, RTA_PAYLOAD(attr), inet_ntoa(addr));
115 DBG(" attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
118 static inline void print_char(struct rtattr *attr, const char *name)
120 DBG(" attr %s (len %jd) %s\n", name, RTA_PAYLOAD(attr),
121 (char *) RTA_DATA(attr));
124 static inline void print_byte(struct rtattr *attr, const char *name)
126 DBG(" attr %s (len %jd) 0x%02x\n", name, RTA_PAYLOAD(attr),
127 *((unsigned char *) RTA_DATA(attr)));
130 static inline void print_attr(struct rtattr *attr, const char *name)
133 DBG(" attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
135 DBG(" attr %d (len %jd)\n",
136 attr->rta_type, RTA_PAYLOAD(attr));
139 static void rtnl_link(struct nlmsghdr *hdr)
141 struct ifinfomsg *msg;
145 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
146 bytes = IFLA_PAYLOAD(hdr);
148 DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
150 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
151 attr = RTA_NEXT(attr, bytes)) {
152 switch (attr->rta_type) {
154 print_attr(attr, "address");
157 print_attr(attr, "broadcast");
160 print_char(attr, "ifname");
163 print_attr(attr, "mtu");
166 print_attr(attr, "link");
169 print_attr(attr, "qdisc");
172 print_attr(attr, "stats");
175 print_attr(attr, "cost");
178 print_attr(attr, "priority");
181 print_attr(attr, "master");
184 print_attr(attr, "wireless");
187 print_attr(attr, "protinfo");
190 print_attr(attr, "txqlen");
193 print_attr(attr, "map");
196 print_attr(attr, "weight");
199 print_byte(attr, "operstate");
202 print_byte(attr, "linkmode");
205 print_attr(attr, NULL);
210 process_link_flags(msg->ifi_index, msg->ifi_flags);
213 static void rtnl_addr(struct nlmsghdr *hdr)
215 struct ifaddrmsg *msg;
219 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
220 bytes = IFA_PAYLOAD(hdr);
222 DBG("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
224 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
225 attr = RTA_NEXT(attr, bytes)) {
226 switch (attr->rta_type) {
228 print_inet(attr, "address", msg->ifa_family);
231 print_inet(attr, "local", msg->ifa_family);
234 print_char(attr, "label");
237 print_inet(attr, "broadcast", msg->ifa_family);
240 print_attr(attr, "anycast");
243 print_attr(attr, "cacheinfo");
246 print_attr(attr, "multicast");
249 print_attr(attr, NULL);
255 static void rtnl_route(struct nlmsghdr *hdr)
261 msg = (struct rtmsg *) NLMSG_DATA(hdr);
262 bytes = RTM_PAYLOAD(hdr);
264 DBG("rtm_family %d rtm_flags 0x%04x", msg->rtm_family, msg->rtm_flags);
266 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
267 attr = RTA_NEXT(attr, bytes)) {
268 switch (attr->rta_type) {
270 print_inet(attr, "dst", msg->rtm_family);
273 print_inet(attr, "src", msg->rtm_family);
276 print_char(attr, "iif");
279 print_attr(attr, "oif");
282 print_inet(attr, "gateway", msg->rtm_family);
285 print_attr(attr, "priority");
288 print_inet(attr, "prefsrc", msg->rtm_family);
291 print_attr(attr, "metrics");
294 print_attr(attr, "table");
297 print_attr(attr, NULL);
303 static void rtnl_message(void *buf, size_t len)
305 DBG("buf %p len %zd", buf, len);
308 struct nlmsghdr *hdr = buf;
309 struct nlmsgerr *err;
311 if (!NLMSG_OK(hdr, len))
314 DBG("len %d type %d flags 0x%04x seq %d",
315 hdr->nlmsg_len, hdr->nlmsg_type,
316 hdr->nlmsg_flags, hdr->nlmsg_seq);
318 switch (hdr->nlmsg_type) {
323 err = NLMSG_DATA(hdr);
324 DBG("ERROR %d (%s)", -err->error,
325 strerror(-err->error));
358 DBG("type %d", hdr->nlmsg_type);
362 len -= hdr->nlmsg_len;
363 buf += hdr->nlmsg_len;
367 static gboolean netlink_event(GIOChannel *chan,
368 GIOCondition cond, gpointer data)
370 unsigned char buf[256];
374 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
377 memset(buf, 0, sizeof(buf));
379 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
381 if (err == G_IO_ERROR_AGAIN)
386 rtnl_message(buf, len);
391 static GIOChannel *channel = NULL;
393 int __connman_rtnl_send(const void *buf, size_t len)
395 struct sockaddr_nl addr;
398 DBG("buf %p len %zd", buf, len);
400 sk = g_io_channel_unix_get_fd(channel);
402 memset(&addr, 0, sizeof(addr));
403 addr.nl_family = AF_NETLINK;
405 return sendto(sk, buf, len, 0,
406 (struct sockaddr *) &addr, sizeof(addr));
409 int __connman_rtnl_init(void)
411 struct sockaddr_nl addr;
416 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
420 memset(&addr, 0, sizeof(addr));
421 addr.nl_family = AF_NETLINK;
422 addr.nl_groups = RTMGRP_LINK;
423 //addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
424 //addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
426 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
431 channel = g_io_channel_unix_new(sk);
432 g_io_channel_set_close_on_unref(channel, TRUE);
434 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
435 netlink_event, NULL);
440 void __connman_rtnl_cleanup(void)
444 g_io_channel_shutdown(channel, TRUE, NULL);
445 g_io_channel_unref(channel);