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>
32 #include <linux/netlink.h>
33 #include <linux/rtnetlink.h>
39 static inline void print_char(struct rtattr *attr, const char *name)
41 printf(" attr %s (len %d) %s", name, RTA_PAYLOAD(attr),
42 (char *) RTA_DATA(attr));
45 static inline void print_attr(struct rtattr *attr, const char *name)
48 printf(" attr %s (len %d)", name, RTA_PAYLOAD(attr));
50 printf(" attr %d (len %d)", attr->rta_type, RTA_PAYLOAD(attr));
53 static void rtnl_link(struct nlmsghdr *hdr)
55 struct connman_iface *iface;
56 struct ifinfomsg *msg;
60 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
61 bytes = IFLA_PAYLOAD(hdr);
63 DBG("ifi_index %d ifi_flags %d", msg->ifi_index, msg->ifi_flags);
65 iface = __connman_iface_find(msg->ifi_index);
69 if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
72 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
73 attr = RTA_NEXT(attr, bytes)) {
74 switch (attr->rta_type) {
76 print_attr(attr, "address");
79 print_attr(attr, "broadcast");
82 print_char(attr, "ifname");
85 print_attr(attr, "mtu");
88 print_attr(attr, "link");
91 print_attr(attr, "qdisc");
94 print_attr(attr, "stats");
97 print_attr(attr, "cost");
100 print_attr(attr, "priority");
103 print_attr(attr, "master");
106 if (iface->driver->rtnl_wireless)
107 iface->driver->rtnl_wireless(iface,
108 RTA_DATA(attr), RTA_PAYLOAD(attr));
111 print_attr(attr, "protinfo");
114 print_attr(attr, "txqlen");
117 print_attr(attr, "map");
120 print_attr(attr, "weight");
123 print_attr(attr, "operstate");
126 print_attr(attr, "linkmode");
129 print_attr(attr, NULL);
135 static void rtnl_addr(struct nlmsghdr *hdr)
137 struct connman_iface *iface;
138 struct ifaddrmsg *msg;
142 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
143 bytes = IFA_PAYLOAD(hdr);
145 DBG("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
147 iface = __connman_iface_find(msg->ifa_index);
151 if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
154 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
155 attr = RTA_NEXT(attr, bytes)) {
156 switch (attr->rta_type) {
158 print_attr(attr, "address");
159 if (msg->ifa_family == AF_INET) {
161 addr = *((struct in_addr *) RTA_DATA(attr));
162 DBG(" address %s", inet_ntoa(addr));
166 print_attr(attr, "local");
167 if (msg->ifa_family == AF_INET) {
169 addr = *((struct in_addr *) RTA_DATA(attr));
170 DBG(" address %s", inet_ntoa(addr));
174 print_char(attr, "label");
177 print_attr(attr, "broadcast");
178 if (msg->ifa_family == AF_INET) {
180 addr = *((struct in_addr *) RTA_DATA(attr));
181 DBG(" address %s", inet_ntoa(addr));
185 print_attr(attr, "anycast");
188 print_attr(attr, "cacheinfo");
191 print_attr(attr, "multicast");
194 print_attr(attr, NULL);
200 static void rtnl_route(struct nlmsghdr *hdr)
206 msg = (struct rtmsg *) NLMSG_DATA(hdr);
207 bytes = RTM_PAYLOAD(hdr);
209 DBG("rtm_family %d rtm_flags %d", msg->rtm_family, msg->rtm_flags);
211 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
212 attr = RTA_NEXT(attr, bytes)) {
213 switch (attr->rta_type) {
215 print_attr(attr, "dst");
216 if (msg->rtm_family == AF_INET) {
218 addr = *((struct in_addr *) RTA_DATA(attr));
219 DBG(" address %s", inet_ntoa(addr));
223 print_attr(attr, "src");
224 if (msg->rtm_family == AF_INET) {
226 addr = *((struct in_addr *) RTA_DATA(attr));
227 DBG(" address %s", inet_ntoa(addr));
231 print_char(attr, "iif");
234 print_attr(attr, "oif");
237 print_attr(attr, "gateway");
238 if (msg->rtm_family == AF_INET) {
240 addr = *((struct in_addr *) RTA_DATA(attr));
241 DBG(" address %s", inet_ntoa(addr));
245 print_attr(attr, "priority");
248 print_attr(attr, "prefsrc");
249 if (msg->rtm_family == AF_INET) {
251 addr = *((struct in_addr *) RTA_DATA(attr));
252 DBG(" address %s", inet_ntoa(addr));
256 print_attr(attr, "metrics");
259 print_attr(attr, "table");
262 print_attr(attr, NULL);
268 static void rtnl_message(unsigned char *buf, size_t size)
270 struct nlmsghdr *hdr = (void *) buf;
272 if (!NLMSG_OK(hdr, size))
275 switch (hdr->nlmsg_type) {
307 DBG("type %d", hdr->nlmsg_type);
312 static gboolean netlink_event(GIOChannel *chan,
313 GIOCondition cond, gpointer data)
315 unsigned char buf[256];
319 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
320 g_io_channel_unref(chan);
324 memset(buf, 0, sizeof(buf));
326 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
328 if (err == G_IO_ERROR_AGAIN)
330 g_io_channel_unref(chan);
334 rtnl_message(buf, len);
339 static GIOChannel *channel = NULL;
341 int __connman_rtnl_init(void)
343 struct sockaddr_nl addr;
348 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
352 memset(&addr, 0, sizeof(addr));
353 addr.nl_family = AF_NETLINK;
354 addr.nl_groups = RTMGRP_LINK;
355 //addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
356 addr.nl_pid = getpid();
358 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
363 channel = g_io_channel_unix_new(sk);
364 g_io_channel_set_close_on_unref(channel, TRUE);
366 g_io_add_watch(channel,
367 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
368 netlink_event, NULL);
370 g_io_channel_unref(channel);
375 void __connman_rtnl_cleanup(void)
379 g_io_channel_unref(channel);