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_attr(struct rtattr *attr, const char *name)
49 printf(" attr %s (len %d)\n", name, RTA_PAYLOAD(attr));
51 printf(" attr %d (len %d)\n",
52 attr->rta_type, RTA_PAYLOAD(attr));
55 static void rtnl_link(struct nlmsghdr *hdr)
57 struct connman_iface *iface;
58 struct ifinfomsg *msg;
62 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
63 bytes = IFLA_PAYLOAD(hdr);
65 DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
67 iface = __connman_iface_find(msg->ifi_index);
71 if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
74 if (iface->carrier != (msg->ifi_flags & IFF_RUNNING)) {
75 iface->carrier = (msg->ifi_flags & IFF_RUNNING);
76 DBG("carrier %s", iface->carrier ? "on" : "off");
79 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
80 attr = RTA_NEXT(attr, bytes)) {
81 switch (attr->rta_type) {
83 print_attr(attr, "address");
86 print_attr(attr, "broadcast");
89 print_char(attr, "ifname");
92 print_attr(attr, "mtu");
95 print_attr(attr, "link");
98 print_attr(attr, "qdisc");
101 print_attr(attr, "stats");
104 print_attr(attr, "cost");
107 print_attr(attr, "priority");
110 print_attr(attr, "master");
113 if (iface->driver->rtnl_wireless)
114 iface->driver->rtnl_wireless(iface,
115 RTA_DATA(attr), RTA_PAYLOAD(attr));
118 print_attr(attr, "protinfo");
121 print_attr(attr, "txqlen");
124 print_attr(attr, "map");
127 print_attr(attr, "weight");
130 print_attr(attr, "operstate");
133 print_attr(attr, "linkmode");
136 print_attr(attr, NULL);
142 static void rtnl_addr(struct nlmsghdr *hdr)
144 struct connman_iface *iface;
145 struct ifaddrmsg *msg;
149 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
150 bytes = IFA_PAYLOAD(hdr);
152 DBG("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
154 iface = __connman_iface_find(msg->ifa_index);
158 if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
161 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
162 attr = RTA_NEXT(attr, bytes)) {
163 switch (attr->rta_type) {
165 print_attr(attr, "address");
166 if (msg->ifa_family == AF_INET) {
168 addr = *((struct in_addr *) RTA_DATA(attr));
169 DBG(" address %s", inet_ntoa(addr));
173 print_attr(attr, "local");
174 if (msg->ifa_family == AF_INET) {
176 addr = *((struct in_addr *) RTA_DATA(attr));
177 DBG(" address %s", inet_ntoa(addr));
181 print_char(attr, "label");
184 print_attr(attr, "broadcast");
185 if (msg->ifa_family == AF_INET) {
187 addr = *((struct in_addr *) RTA_DATA(attr));
188 DBG(" address %s", inet_ntoa(addr));
192 print_attr(attr, "anycast");
195 print_attr(attr, "cacheinfo");
198 print_attr(attr, "multicast");
201 print_attr(attr, NULL);
207 static void rtnl_route(struct nlmsghdr *hdr)
213 msg = (struct rtmsg *) NLMSG_DATA(hdr);
214 bytes = RTM_PAYLOAD(hdr);
216 DBG("rtm_family %d rtm_flags 0x%04x", msg->rtm_family, msg->rtm_flags);
218 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
219 attr = RTA_NEXT(attr, bytes)) {
220 switch (attr->rta_type) {
222 print_attr(attr, "dst");
223 if (msg->rtm_family == AF_INET) {
225 addr = *((struct in_addr *) RTA_DATA(attr));
226 DBG(" address %s", inet_ntoa(addr));
230 print_attr(attr, "src");
231 if (msg->rtm_family == AF_INET) {
233 addr = *((struct in_addr *) RTA_DATA(attr));
234 DBG(" address %s", inet_ntoa(addr));
238 print_char(attr, "iif");
241 print_attr(attr, "oif");
244 print_attr(attr, "gateway");
245 if (msg->rtm_family == AF_INET) {
247 addr = *((struct in_addr *) RTA_DATA(attr));
248 DBG(" address %s", inet_ntoa(addr));
252 print_attr(attr, "priority");
255 print_attr(attr, "prefsrc");
256 if (msg->rtm_family == AF_INET) {
258 addr = *((struct in_addr *) RTA_DATA(attr));
259 DBG(" address %s", inet_ntoa(addr));
263 print_attr(attr, "metrics");
266 print_attr(attr, "table");
269 print_attr(attr, NULL);
275 static void rtnl_message(unsigned char *buf, size_t size)
277 struct nlmsghdr *hdr = (void *) buf;
279 if (!NLMSG_OK(hdr, size))
282 switch (hdr->nlmsg_type) {
314 DBG("type %d", hdr->nlmsg_type);
319 static gboolean netlink_event(GIOChannel *chan,
320 GIOCondition cond, gpointer data)
322 unsigned char buf[256];
326 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
327 g_io_channel_unref(chan);
331 memset(buf, 0, sizeof(buf));
333 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
335 if (err == G_IO_ERROR_AGAIN)
337 g_io_channel_unref(chan);
341 rtnl_message(buf, len);
346 static GIOChannel *channel = NULL;
348 int __connman_rtnl_init(void)
350 struct sockaddr_nl addr;
355 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
359 memset(&addr, 0, sizeof(addr));
360 addr.nl_family = AF_NETLINK;
361 addr.nl_groups = RTMGRP_LINK;
362 //addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
363 addr.nl_pid = getpid();
365 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
370 channel = g_io_channel_unix_new(sk);
371 g_io_channel_set_close_on_unref(channel, TRUE);
373 g_io_add_watch(channel,
374 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
375 netlink_event, NULL);
377 g_io_channel_unref(channel);
382 void __connman_rtnl_cleanup(void)
386 g_io_channel_unref(channel);