5 * Copyright (C) 2007 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
29 #include <sys/socket.h>
30 #include <arpa/inet.h>
33 #include <linux/netlink.h>
34 #include <linux/rtnetlink.h>
44 static struct rtnl_data *get_rtnl_data(struct connman_iface *iface)
46 if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
49 if (iface->rtnl_data == NULL)
50 iface->rtnl_data = g_try_new0(struct rtnl_data, 1);
52 return iface->rtnl_data;
55 static inline void print_inet(struct rtattr *attr, const char *name, int family)
57 if (family == AF_INET) {
59 addr = *((struct in_addr *) RTA_DATA(attr));
60 printf(" attr %s (len %jd) %s\n",
61 name, RTA_PAYLOAD(attr), inet_ntoa(addr));
63 printf(" attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
66 static inline void print_char(struct rtattr *attr, const char *name)
68 printf(" attr %s (len %jd) %s\n", name, RTA_PAYLOAD(attr),
69 (char *) RTA_DATA(attr));
72 static inline void print_byte(struct rtattr *attr, const char *name)
74 printf(" attr %s (len %jd) 0x%02x\n", name, RTA_PAYLOAD(attr),
75 *((unsigned char *) RTA_DATA(attr)));
78 static inline void print_attr(struct rtattr *attr, const char *name)
81 printf(" attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
83 printf(" attr %d (len %jd)\n",
84 attr->rta_type, RTA_PAYLOAD(attr));
87 static void rtnl_link(struct nlmsghdr *hdr)
89 struct connman_iface *iface;
90 struct rtnl_data *data;
91 struct ifinfomsg *msg;
95 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
96 bytes = IFLA_PAYLOAD(hdr);
98 DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
100 iface = __connman_iface_find(msg->ifi_index);
104 data = get_rtnl_data(iface);
108 if ((data->ifi_flags & IFF_RUNNING) != (msg->ifi_flags & IFF_RUNNING)) {
109 if (msg->ifi_flags & IFF_RUNNING)
110 connman_iface_indicate_carrier_on(iface);
112 connman_iface_indicate_carrier_off(iface);
115 if ((data->ifi_flags & IFF_UP) != (msg->ifi_flags & IFF_UP)) {
116 if (msg->ifi_flags & IFF_UP)
117 connman_iface_indicate_enabled(iface);
119 connman_iface_indicate_disabled(iface);
122 data->ifi_flags = msg->ifi_flags;
124 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
125 attr = RTA_NEXT(attr, bytes)) {
126 switch (attr->rta_type) {
128 print_attr(attr, "address");
131 print_attr(attr, "broadcast");
134 print_char(attr, "ifname");
137 print_attr(attr, "mtu");
140 print_attr(attr, "link");
143 print_attr(attr, "qdisc");
146 print_attr(attr, "stats");
149 print_attr(attr, "cost");
152 print_attr(attr, "priority");
155 print_attr(attr, "master");
158 if (iface->driver->rtnl_wireless)
159 iface->driver->rtnl_wireless(iface,
160 RTA_DATA(attr), RTA_PAYLOAD(attr));
163 print_attr(attr, "protinfo");
166 print_attr(attr, "txqlen");
169 print_attr(attr, "map");
172 print_attr(attr, "weight");
175 print_byte(attr, "operstate");
178 print_byte(attr, "linkmode");
181 print_attr(attr, NULL);
187 static void rtnl_addr(struct nlmsghdr *hdr)
189 struct connman_iface *iface;
190 struct rtnl_data *data;
191 struct ifaddrmsg *msg;
195 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
196 bytes = IFA_PAYLOAD(hdr);
198 DBG("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
200 iface = __connman_iface_find(msg->ifa_index);
204 data = get_rtnl_data(iface);
208 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
209 attr = RTA_NEXT(attr, bytes)) {
210 switch (attr->rta_type) {
212 print_inet(attr, "address", msg->ifa_family);
215 print_inet(attr, "local", msg->ifa_family);
218 print_char(attr, "label");
221 print_inet(attr, "broadcast", msg->ifa_family);
224 print_attr(attr, "anycast");
227 print_attr(attr, "cacheinfo");
230 print_attr(attr, "multicast");
233 print_attr(attr, NULL);
239 static void rtnl_route(struct nlmsghdr *hdr)
245 msg = (struct rtmsg *) NLMSG_DATA(hdr);
246 bytes = RTM_PAYLOAD(hdr);
248 DBG("rtm_family %d rtm_flags 0x%04x", msg->rtm_family, msg->rtm_flags);
250 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
251 attr = RTA_NEXT(attr, bytes)) {
252 switch (attr->rta_type) {
254 print_inet(attr, "dst", msg->rtm_family);
257 print_inet(attr, "src", msg->rtm_family);
260 print_char(attr, "iif");
263 print_attr(attr, "oif");
266 print_inet(attr, "gateway", msg->rtm_family);
269 print_attr(attr, "priority");
272 print_inet(attr, "prefsrc", msg->rtm_family);
275 print_attr(attr, "metrics");
278 print_attr(attr, "table");
281 print_attr(attr, NULL);
287 static void rtnl_message(void *buf, size_t len)
289 DBG("buf %p len %zd", buf, len);
292 struct nlmsghdr *hdr = buf;
293 struct nlmsgerr *err;
295 if (!NLMSG_OK(hdr, len))
298 DBG("len %d type %d flags 0x%04x seq %d",
299 hdr->nlmsg_len, hdr->nlmsg_type,
300 hdr->nlmsg_flags, hdr->nlmsg_seq);
302 switch (hdr->nlmsg_type) {
307 err = NLMSG_DATA(hdr);
308 DBG("ERROR %d (%s)", -err->error,
309 strerror(-err->error));
342 DBG("type %d", hdr->nlmsg_type);
346 len -= hdr->nlmsg_len;
347 buf += hdr->nlmsg_len;
351 static gboolean netlink_event(GIOChannel *chan,
352 GIOCondition cond, gpointer data)
354 unsigned char buf[256];
358 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
361 memset(buf, 0, sizeof(buf));
363 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
365 if (err == G_IO_ERROR_AGAIN)
370 rtnl_message(buf, len);
375 static GIOChannel *channel = NULL;
377 int __connman_rtnl_send(const void *buf, size_t len)
379 struct sockaddr_nl addr;
382 DBG("buf %p len %zd", buf, len);
384 sk = g_io_channel_unix_get_fd(channel);
386 memset(&addr, 0, sizeof(addr));
387 addr.nl_family = AF_NETLINK;
389 return sendto(sk, buf, len, 0,
390 (struct sockaddr *) &addr, sizeof(addr));
393 int __connman_rtnl_init(void)
395 struct sockaddr_nl addr;
400 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
404 memset(&addr, 0, sizeof(addr));
405 addr.nl_family = AF_NETLINK;
406 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
407 //addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
409 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
414 channel = g_io_channel_unix_new(sk);
415 g_io_channel_set_close_on_unref(channel, TRUE);
417 g_io_add_watch(channel,
418 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
419 netlink_event, NULL);
424 void __connman_rtnl_cleanup(void)
428 g_io_channel_shutdown(channel, TRUE, NULL);
429 g_io_channel_unref(channel);