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
28 #include <sys/socket.h>
29 #include <arpa/inet.h>
32 #include <linux/netlink.h>
33 #include <linux/rtnetlink.h>
43 static struct rtnl_data *get_rtnl_data(struct connman_iface *iface)
45 if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
48 if (iface->rtnl_data == NULL)
49 iface->rtnl_data = g_try_new0(struct rtnl_data, 1);
51 return iface->rtnl_data;
54 static inline void print_inet(struct rtattr *attr, const char *name, int family)
56 if (family == AF_INET) {
58 addr = *((struct in_addr *) RTA_DATA(attr));
59 DBG(" attr %s (len %jd) %s\n",
60 name, RTA_PAYLOAD(attr), inet_ntoa(addr));
62 DBG(" attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
65 static inline void print_char(struct rtattr *attr, const char *name)
67 DBG(" attr %s (len %jd) %s\n", name, RTA_PAYLOAD(attr),
68 (char *) RTA_DATA(attr));
71 static inline void print_byte(struct rtattr *attr, const char *name)
73 DBG(" attr %s (len %jd) 0x%02x\n", name, RTA_PAYLOAD(attr),
74 *((unsigned char *) RTA_DATA(attr)));
77 static inline void print_attr(struct rtattr *attr, const char *name)
80 DBG(" attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
82 DBG(" attr %d (len %jd)\n",
83 attr->rta_type, RTA_PAYLOAD(attr));
86 static void rtnl_link(struct nlmsghdr *hdr)
88 struct connman_iface *iface;
89 struct rtnl_data *data;
90 struct ifinfomsg *msg;
94 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
95 bytes = IFLA_PAYLOAD(hdr);
97 DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
99 iface = __connman_iface_find(msg->ifi_index);
103 data = get_rtnl_data(iface);
107 if ((data->ifi_flags & IFF_RUNNING) != (msg->ifi_flags & IFF_RUNNING)) {
108 if (msg->ifi_flags & IFF_RUNNING)
109 connman_iface_indicate_carrier_on(iface);
111 connman_iface_indicate_carrier_off(iface);
114 if ((data->ifi_flags & IFF_UP) != (msg->ifi_flags & IFF_UP)) {
115 if (msg->ifi_flags & IFF_UP)
116 connman_iface_indicate_enabled(iface);
118 connman_iface_indicate_disabled(iface);
121 data->ifi_flags = msg->ifi_flags;
123 for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
124 attr = RTA_NEXT(attr, bytes)) {
125 switch (attr->rta_type) {
127 print_attr(attr, "address");
130 print_attr(attr, "broadcast");
133 print_char(attr, "ifname");
136 print_attr(attr, "mtu");
139 print_attr(attr, "link");
142 print_attr(attr, "qdisc");
145 print_attr(attr, "stats");
148 print_attr(attr, "cost");
151 print_attr(attr, "priority");
154 print_attr(attr, "master");
157 if (iface->driver->rtnl_wireless)
158 iface->driver->rtnl_wireless(iface,
159 RTA_DATA(attr), RTA_PAYLOAD(attr));
162 print_attr(attr, "protinfo");
165 print_attr(attr, "txqlen");
168 print_attr(attr, "map");
171 print_attr(attr, "weight");
174 print_byte(attr, "operstate");
177 print_byte(attr, "linkmode");
180 print_attr(attr, NULL);
186 static void rtnl_addr(struct nlmsghdr *hdr)
188 struct connman_iface *iface;
189 struct rtnl_data *data;
190 struct ifaddrmsg *msg;
194 msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
195 bytes = IFA_PAYLOAD(hdr);
197 DBG("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
199 iface = __connman_iface_find(msg->ifa_index);
203 data = get_rtnl_data(iface);
207 for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
208 attr = RTA_NEXT(attr, bytes)) {
209 switch (attr->rta_type) {
211 print_inet(attr, "address", msg->ifa_family);
214 print_inet(attr, "local", msg->ifa_family);
217 print_char(attr, "label");
220 print_inet(attr, "broadcast", msg->ifa_family);
223 print_attr(attr, "anycast");
226 print_attr(attr, "cacheinfo");
229 print_attr(attr, "multicast");
232 print_attr(attr, NULL);
238 static void rtnl_route(struct nlmsghdr *hdr)
244 msg = (struct rtmsg *) NLMSG_DATA(hdr);
245 bytes = RTM_PAYLOAD(hdr);
247 DBG("rtm_family %d rtm_flags 0x%04x", msg->rtm_family, msg->rtm_flags);
249 for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
250 attr = RTA_NEXT(attr, bytes)) {
251 switch (attr->rta_type) {
253 print_inet(attr, "dst", msg->rtm_family);
256 print_inet(attr, "src", msg->rtm_family);
259 print_char(attr, "iif");
262 print_attr(attr, "oif");
265 print_inet(attr, "gateway", msg->rtm_family);
268 print_attr(attr, "priority");
271 print_inet(attr, "prefsrc", msg->rtm_family);
274 print_attr(attr, "metrics");
277 print_attr(attr, "table");
280 print_attr(attr, NULL);
286 static void rtnl_message(void *buf, size_t len)
288 DBG("buf %p len %zd", buf, len);
291 struct nlmsghdr *hdr = buf;
292 struct nlmsgerr *err;
294 if (!NLMSG_OK(hdr, len))
297 DBG("len %d type %d flags 0x%04x seq %d",
298 hdr->nlmsg_len, hdr->nlmsg_type,
299 hdr->nlmsg_flags, hdr->nlmsg_seq);
301 switch (hdr->nlmsg_type) {
306 err = NLMSG_DATA(hdr);
307 DBG("ERROR %d (%s)", -err->error,
308 strerror(-err->error));
341 DBG("type %d", hdr->nlmsg_type);
345 len -= hdr->nlmsg_len;
346 buf += hdr->nlmsg_len;
350 static gboolean netlink_event(GIOChannel *chan,
351 GIOCondition cond, gpointer data)
353 unsigned char buf[256];
357 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
360 memset(buf, 0, sizeof(buf));
362 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
364 if (err == G_IO_ERROR_AGAIN)
369 rtnl_message(buf, len);
374 static GIOChannel *channel = NULL;
376 int __connman_rtnl_send(const void *buf, size_t len)
378 struct sockaddr_nl addr;
381 DBG("buf %p len %zd", buf, len);
383 sk = g_io_channel_unix_get_fd(channel);
385 memset(&addr, 0, sizeof(addr));
386 addr.nl_family = AF_NETLINK;
388 return sendto(sk, buf, len, 0,
389 (struct sockaddr *) &addr, sizeof(addr));
392 int __connman_rtnl_init(void)
394 struct sockaddr_nl addr;
399 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
403 memset(&addr, 0, sizeof(addr));
404 addr.nl_family = AF_NETLINK;
405 addr.nl_groups = RTMGRP_LINK;
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);