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>
43 static struct rtnl_data *get_rtnl_data(struct connman_iface *iface)
45 if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
48 if (iface->rtnl_data == NULL)
49 iface->rtnl_data = g_try_new0(struct rtnl_data, 1);
51 return iface->rtnl_data;
54 static inline void print_inet(struct rtattr *attr, const char *name, int family)
56 if (family == AF_INET) {
58 addr = *((struct in_addr *) RTA_DATA(attr));
59 DBG(" attr %s (len %jd) %s\n",
60 name, RTA_PAYLOAD(attr), inet_ntoa(addr));
62 DBG(" attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
65 static inline void print_char(struct rtattr *attr, const char *name)
67 DBG(" attr %s (len %jd) %s\n", name, RTA_PAYLOAD(attr),
68 (char *) RTA_DATA(attr));
71 static inline void print_byte(struct rtattr *attr, const char *name)
73 DBG(" attr %s (len %jd) 0x%02x\n", name, RTA_PAYLOAD(attr),
74 *((unsigned char *) RTA_DATA(attr)));
77 static inline void print_attr(struct rtattr *attr, const char *name)
80 DBG(" attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
82 DBG(" attr %d (len %jd)\n",
83 attr->rta_type, RTA_PAYLOAD(attr));
86 static void rtnl_link(struct nlmsghdr *hdr)
88 struct connman_iface *iface;
89 struct rtnl_data *data;
90 struct ifinfomsg *msg;
94 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
95 bytes = IFLA_PAYLOAD(hdr);
97 DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
99 iface = __connman_iface_find(msg->ifi_index);
103 data = get_rtnl_data(iface);
107 if ((data->ifi_flags & IFF_RUNNING) != (msg->ifi_flags & IFF_RUNNING)) {
108 if (!(iface->flags & CONNMAN_IFACE_FLAG_NOCARRIER)) {
109 if (msg->ifi_flags & IFF_RUNNING)
110 connman_iface_indicate_carrier_on(iface);
112 connman_iface_indicate_carrier_off(iface);
116 if ((data->ifi_flags & IFF_UP) != (msg->ifi_flags & IFF_UP)) {
117 if (msg->ifi_flags & IFF_UP)
118 connman_iface_indicate_ifup(iface);
120 connman_iface_indicate_ifdown(iface);
123 data->ifi_flags = msg->ifi_flags;
127 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
128 attr = RTA_NEXT(attr, bytes)) {
129 switch (attr->rta_type) {
131 print_attr(attr, "address");
134 print_attr(attr, "broadcast");
137 print_char(attr, "ifname");
140 print_attr(attr, "mtu");
143 print_attr(attr, "link");
146 print_attr(attr, "qdisc");
149 print_attr(attr, "stats");
152 print_attr(attr, "cost");
155 print_attr(attr, "priority");
158 print_attr(attr, "master");
161 if (iface->driver->rtnl_wireless)
162 iface->driver->rtnl_wireless(iface,
163 RTA_DATA(attr), RTA_PAYLOAD(attr));
166 print_attr(attr, "protinfo");
169 print_attr(attr, "txqlen");
172 print_attr(attr, "map");
175 print_attr(attr, "weight");
178 print_byte(attr, "operstate");
181 print_byte(attr, "linkmode");
184 print_attr(attr, NULL);
190 static void rtnl_addr(struct nlmsghdr *hdr)
192 struct connman_iface *iface;
193 struct rtnl_data *data;
194 struct ifaddrmsg *msg;
198 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
199 bytes = IFA_PAYLOAD(hdr);
201 DBG("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
203 iface = __connman_iface_find(msg->ifa_index);
207 data = get_rtnl_data(iface);
211 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
212 attr = RTA_NEXT(attr, bytes)) {
213 switch (attr->rta_type) {
215 print_inet(attr, "address", msg->ifa_family);
218 print_inet(attr, "local", msg->ifa_family);
221 print_char(attr, "label");
224 print_inet(attr, "broadcast", msg->ifa_family);
227 print_attr(attr, "anycast");
230 print_attr(attr, "cacheinfo");
233 print_attr(attr, "multicast");
236 print_attr(attr, NULL);
242 static void rtnl_route(struct nlmsghdr *hdr)
248 msg = (struct rtmsg *) NLMSG_DATA(hdr);
249 bytes = RTM_PAYLOAD(hdr);
251 DBG("rtm_family %d rtm_flags 0x%04x", msg->rtm_family, msg->rtm_flags);
253 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
254 attr = RTA_NEXT(attr, bytes)) {
255 switch (attr->rta_type) {
257 print_inet(attr, "dst", msg->rtm_family);
260 print_inet(attr, "src", msg->rtm_family);
263 print_char(attr, "iif");
266 print_attr(attr, "oif");
269 print_inet(attr, "gateway", msg->rtm_family);
272 print_attr(attr, "priority");
275 print_inet(attr, "prefsrc", msg->rtm_family);
278 print_attr(attr, "metrics");
281 print_attr(attr, "table");
284 print_attr(attr, NULL);
290 static void rtnl_message(void *buf, size_t len)
292 DBG("buf %p len %zd", buf, len);
295 struct nlmsghdr *hdr = buf;
296 struct nlmsgerr *err;
298 if (!NLMSG_OK(hdr, len))
301 DBG("len %d type %d flags 0x%04x seq %d",
302 hdr->nlmsg_len, hdr->nlmsg_type,
303 hdr->nlmsg_flags, hdr->nlmsg_seq);
305 switch (hdr->nlmsg_type) {
310 err = NLMSG_DATA(hdr);
311 DBG("ERROR %d (%s)", -err->error,
312 strerror(-err->error));
345 DBG("type %d", hdr->nlmsg_type);
349 len -= hdr->nlmsg_len;
350 buf += hdr->nlmsg_len;
354 static gboolean netlink_event(GIOChannel *chan,
355 GIOCondition cond, gpointer data)
357 unsigned char buf[256];
361 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
364 memset(buf, 0, sizeof(buf));
366 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
368 if (err == G_IO_ERROR_AGAIN)
373 rtnl_message(buf, len);
378 static GIOChannel *channel = NULL;
380 int __connman_rtnl_send(const void *buf, size_t len)
382 struct sockaddr_nl addr;
385 DBG("buf %p len %zd", buf, len);
387 sk = g_io_channel_unix_get_fd(channel);
389 memset(&addr, 0, sizeof(addr));
390 addr.nl_family = AF_NETLINK;
392 return sendto(sk, buf, len, 0,
393 (struct sockaddr *) &addr, sizeof(addr));
396 int __connman_rtnl_init(void)
398 struct sockaddr_nl addr;
403 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
407 memset(&addr, 0, sizeof(addr));
408 addr.nl_family = AF_NETLINK;
409 addr.nl_groups = RTMGRP_LINK;
410 //addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
411 //addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
413 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
418 channel = g_io_channel_unix_new(sk);
419 g_io_channel_set_close_on_unref(channel, TRUE);
421 g_io_add_watch(channel,
422 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
423 netlink_event, NULL);
428 void __connman_rtnl_cleanup(void)
432 g_io_channel_shutdown(channel, TRUE, NULL);
433 g_io_channel_unref(channel);