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_char(struct rtattr *attr, const char *name)
42 printf(" attr %s (len %d) %s\n", name, RTA_PAYLOAD(attr),
43 (char *) RTA_DATA(attr));
46 static inline void print_byte(struct rtattr *attr, const char *name)
48 printf(" attr %s (len %d) 0x%02x\n", name, RTA_PAYLOAD(attr),
49 *((unsigned char *) RTA_DATA(attr)));
52 static inline void print_attr(struct rtattr *attr, const char *name)
55 printf(" attr %s (len %d)\n", name, RTA_PAYLOAD(attr));
57 printf(" attr %d (len %d)\n",
58 attr->rta_type, RTA_PAYLOAD(attr));
61 static void rtnl_link(struct nlmsghdr *hdr)
63 struct connman_iface *iface;
64 struct ifinfomsg *msg;
68 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
69 bytes = IFLA_PAYLOAD(hdr);
71 DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
73 iface = __connman_iface_find(msg->ifi_index);
77 if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
80 if (iface->carrier != ((msg->ifi_flags & IFF_RUNNING) != 0)) {
81 iface->carrier = ((msg->ifi_flags & IFF_RUNNING) != 0);
82 if (iface->driver->rtnl_carrier)
83 iface->driver->rtnl_carrier(iface, iface->carrier);
85 connman_iface_indicate_carrier(iface, iface->carrier);
88 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
89 attr = RTA_NEXT(attr, bytes)) {
90 switch (attr->rta_type) {
92 print_attr(attr, "address");
95 print_attr(attr, "broadcast");
98 print_char(attr, "ifname");
101 print_attr(attr, "mtu");
104 print_attr(attr, "link");
107 print_attr(attr, "qdisc");
110 print_attr(attr, "stats");
113 print_attr(attr, "cost");
116 print_attr(attr, "priority");
119 print_attr(attr, "master");
122 if (iface->driver->rtnl_wireless)
123 iface->driver->rtnl_wireless(iface,
124 RTA_DATA(attr), RTA_PAYLOAD(attr));
127 print_attr(attr, "protinfo");
130 print_attr(attr, "txqlen");
133 print_attr(attr, "map");
136 print_attr(attr, "weight");
139 print_byte(attr, "operstate");
142 print_byte(attr, "linkmode");
145 print_attr(attr, NULL);
151 static void rtnl_addr(struct nlmsghdr *hdr)
153 struct connman_iface *iface;
154 struct ifaddrmsg *msg;
158 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
159 bytes = IFA_PAYLOAD(hdr);
161 DBG("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
163 iface = __connman_iface_find(msg->ifa_index);
167 if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
170 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
171 attr = RTA_NEXT(attr, bytes)) {
172 switch (attr->rta_type) {
174 print_attr(attr, "address");
175 if (msg->ifa_family == AF_INET) {
177 addr = *((struct in_addr *) RTA_DATA(attr));
178 DBG(" address %s", inet_ntoa(addr));
182 print_attr(attr, "local");
183 if (msg->ifa_family == AF_INET) {
185 addr = *((struct in_addr *) RTA_DATA(attr));
186 DBG(" address %s", inet_ntoa(addr));
190 print_char(attr, "label");
193 print_attr(attr, "broadcast");
194 if (msg->ifa_family == AF_INET) {
196 addr = *((struct in_addr *) RTA_DATA(attr));
197 DBG(" address %s", inet_ntoa(addr));
201 print_attr(attr, "anycast");
204 print_attr(attr, "cacheinfo");
207 print_attr(attr, "multicast");
210 print_attr(attr, NULL);
216 static void rtnl_route(struct nlmsghdr *hdr)
222 msg = (struct rtmsg *) NLMSG_DATA(hdr);
223 bytes = RTM_PAYLOAD(hdr);
225 DBG("rtm_family %d rtm_flags 0x%04x", msg->rtm_family, msg->rtm_flags);
227 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
228 attr = RTA_NEXT(attr, bytes)) {
229 switch (attr->rta_type) {
231 print_attr(attr, "dst");
232 if (msg->rtm_family == AF_INET) {
234 addr = *((struct in_addr *) RTA_DATA(attr));
235 DBG(" address %s", inet_ntoa(addr));
239 print_attr(attr, "src");
240 if (msg->rtm_family == AF_INET) {
242 addr = *((struct in_addr *) RTA_DATA(attr));
243 DBG(" address %s", inet_ntoa(addr));
247 print_char(attr, "iif");
250 print_attr(attr, "oif");
253 print_attr(attr, "gateway");
254 if (msg->rtm_family == AF_INET) {
256 addr = *((struct in_addr *) RTA_DATA(attr));
257 DBG(" address %s", inet_ntoa(addr));
261 print_attr(attr, "priority");
264 print_attr(attr, "prefsrc");
265 if (msg->rtm_family == AF_INET) {
267 addr = *((struct in_addr *) RTA_DATA(attr));
268 DBG(" address %s", inet_ntoa(addr));
272 print_attr(attr, "metrics");
275 print_attr(attr, "table");
278 print_attr(attr, NULL);
284 static void rtnl_message(unsigned char *buf, size_t size)
286 struct nlmsghdr *hdr = (void *) buf;
288 if (!NLMSG_OK(hdr, size))
291 switch (hdr->nlmsg_type) {
323 DBG("type %d", hdr->nlmsg_type);
328 static gboolean netlink_event(GIOChannel *chan,
329 GIOCondition cond, gpointer data)
331 unsigned char buf[256];
335 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
336 g_io_channel_unref(chan);
340 memset(buf, 0, sizeof(buf));
342 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
344 if (err == G_IO_ERROR_AGAIN)
346 g_io_channel_unref(chan);
350 rtnl_message(buf, len);
355 static GIOChannel *channel = NULL;
357 int __connman_rtnl_init(void)
359 struct sockaddr_nl addr;
364 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
368 memset(&addr, 0, sizeof(addr));
369 addr.nl_family = AF_NETLINK;
370 addr.nl_groups = RTMGRP_LINK;
371 //addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
372 addr.nl_pid = getpid();
374 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
379 channel = g_io_channel_unix_new(sk);
380 g_io_channel_set_close_on_unref(channel, TRUE);
382 g_io_add_watch(channel,
383 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
384 netlink_event, NULL);
386 g_io_channel_unref(channel);
391 void __connman_rtnl_cleanup(void)
395 g_io_channel_unref(channel);