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>
40 static inline void print_inet(struct rtattr *attr, const char *name, int family)
42 if (family == AF_INET) {
44 addr = *((struct in_addr *) RTA_DATA(attr));
45 printf(" attr %s (len %d) %s\n",
46 name, RTA_PAYLOAD(attr), inet_ntoa(addr));
48 printf(" attr %s (len %d)\n", name, RTA_PAYLOAD(attr));
51 static inline void print_char(struct rtattr *attr, const char *name)
53 printf(" attr %s (len %d) %s\n", name, RTA_PAYLOAD(attr),
54 (char *) RTA_DATA(attr));
57 static inline void print_byte(struct rtattr *attr, const char *name)
59 printf(" attr %s (len %d) 0x%02x\n", name, RTA_PAYLOAD(attr),
60 *((unsigned char *) RTA_DATA(attr)));
63 static inline void print_attr(struct rtattr *attr, const char *name)
66 printf(" attr %s (len %d)\n", name, RTA_PAYLOAD(attr));
68 printf(" attr %d (len %d)\n",
69 attr->rta_type, RTA_PAYLOAD(attr));
72 static void rtnl_link(struct nlmsghdr *hdr)
74 struct connman_iface *iface;
75 struct ifinfomsg *msg;
79 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
80 bytes = IFLA_PAYLOAD(hdr);
82 DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
84 iface = __connman_iface_find(msg->ifi_index);
88 if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
91 if (iface->carrier != ((msg->ifi_flags & IFF_RUNNING) != 0)) {
92 iface->carrier = ((msg->ifi_flags & IFF_RUNNING) != 0);
93 if (iface->driver->rtnl_carrier)
94 iface->driver->rtnl_carrier(iface, iface->carrier);
96 connman_iface_indicate_carrier(iface, iface->carrier);
99 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
100 attr = RTA_NEXT(attr, bytes)) {
101 switch (attr->rta_type) {
103 print_attr(attr, "address");
106 print_attr(attr, "broadcast");
109 print_char(attr, "ifname");
112 print_attr(attr, "mtu");
115 print_attr(attr, "link");
118 print_attr(attr, "qdisc");
121 print_attr(attr, "stats");
124 print_attr(attr, "cost");
127 print_attr(attr, "priority");
130 print_attr(attr, "master");
133 if (iface->driver->rtnl_wireless)
134 iface->driver->rtnl_wireless(iface,
135 RTA_DATA(attr), RTA_PAYLOAD(attr));
138 print_attr(attr, "protinfo");
141 print_attr(attr, "txqlen");
144 print_attr(attr, "map");
147 print_attr(attr, "weight");
150 print_byte(attr, "operstate");
153 print_byte(attr, "linkmode");
156 print_attr(attr, NULL);
162 static void rtnl_addr(struct nlmsghdr *hdr)
164 struct connman_iface *iface;
165 struct ifaddrmsg *msg;
169 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
170 bytes = IFA_PAYLOAD(hdr);
172 DBG("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
174 iface = __connman_iface_find(msg->ifa_index);
178 if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
181 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
182 attr = RTA_NEXT(attr, bytes)) {
183 switch (attr->rta_type) {
185 print_inet(attr, "address", msg->ifa_family);
188 print_inet(attr, "local", msg->ifa_family);
191 print_char(attr, "label");
194 print_inet(attr, "broadcast", msg->ifa_family);
197 print_attr(attr, "anycast");
200 print_attr(attr, "cacheinfo");
203 print_attr(attr, "multicast");
206 print_attr(attr, NULL);
212 static void rtnl_route(struct nlmsghdr *hdr)
218 msg = (struct rtmsg *) NLMSG_DATA(hdr);
219 bytes = RTM_PAYLOAD(hdr);
221 DBG("rtm_family %d rtm_flags 0x%04x", msg->rtm_family, msg->rtm_flags);
223 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
224 attr = RTA_NEXT(attr, bytes)) {
225 switch (attr->rta_type) {
227 print_inet(attr, "dst", msg->rtm_family);
230 print_inet(attr, "src", msg->rtm_family);
233 print_char(attr, "iif");
236 print_attr(attr, "oif");
239 print_inet(attr, "gateway", msg->rtm_family);
242 print_attr(attr, "priority");
245 print_inet(attr, "prefsrc", msg->rtm_family);
248 print_attr(attr, "metrics");
251 print_attr(attr, "table");
254 print_attr(attr, NULL);
260 static void rtnl_message(void *buf, size_t len)
262 DBG("buf %p len %d", buf, len);
265 struct nlmsghdr *hdr = buf;
266 struct nlmsgerr *err;
268 if (!NLMSG_OK(hdr, len))
271 DBG("len %d type %d flags 0x%04x seq %d",
272 hdr->nlmsg_len, hdr->nlmsg_type,
273 hdr->nlmsg_flags, hdr->nlmsg_seq);
275 switch (hdr->nlmsg_type) {
280 err = NLMSG_DATA(hdr);
281 DBG("ERROR %d (%s)", -err->error,
282 strerror(-err->error));
315 DBG("type %d", hdr->nlmsg_type);
319 len -= hdr->nlmsg_len;
320 buf += hdr->nlmsg_len;
324 static gboolean netlink_event(GIOChannel *chan,
325 GIOCondition cond, gpointer data)
327 unsigned char buf[256];
331 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
332 g_io_channel_unref(chan);
336 memset(buf, 0, sizeof(buf));
338 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
340 if (err == G_IO_ERROR_AGAIN)
342 g_io_channel_unref(chan);
346 rtnl_message(buf, len);
351 static GIOChannel *channel = NULL;
353 int __connman_rtnl_send(const void *buf, size_t len)
355 struct sockaddr_nl addr;
358 DBG("buf %p len %d", buf, len);
360 sk = g_io_channel_unix_get_fd(channel);
362 memset(&addr, 0, sizeof(addr));
363 addr.nl_family = AF_NETLINK;
365 return sendto(sk, buf, len, 0,
366 (struct sockaddr *) &addr, sizeof(addr));
369 int __connman_rtnl_init(void)
371 struct sockaddr_nl addr;
376 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
380 memset(&addr, 0, sizeof(addr));
381 addr.nl_family = AF_NETLINK;
382 addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
383 //addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
385 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
390 channel = g_io_channel_unix_new(sk);
391 g_io_channel_set_close_on_unref(channel, TRUE);
393 g_io_add_watch(channel,
394 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
395 netlink_event, NULL);
397 g_io_channel_unref(channel);
402 void __connman_rtnl_cleanup(void)
406 g_io_channel_unref(channel);