Add experimental IPv4 address retrieval over netlink
[platform/upstream/connman.git] / src / rtnl.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <sys/socket.h>
30 #include <arpa/inet.h>
31
32 #include <linux/if.h>
33 #include <linux/netlink.h>
34 #include <linux/rtnetlink.h>
35
36 #include <glib.h>
37
38 #include "connman.h"
39
40 static inline void print_inet(struct rtattr *attr, const char *name, int family)
41 {
42         if (family == AF_INET) {
43                 struct in_addr addr;
44                 addr = *((struct in_addr *) RTA_DATA(attr));
45                 printf("  attr %s (len %d) %s\n",
46                                 name, RTA_PAYLOAD(attr), inet_ntoa(addr));
47         } else
48                 printf("  attr %s (len %d)\n", name, RTA_PAYLOAD(attr));
49 }
50
51 static inline void print_char(struct rtattr *attr, const char *name)
52 {
53         printf("  attr %s (len %d) %s\n", name, RTA_PAYLOAD(attr),
54                                                 (char *) RTA_DATA(attr));
55 }
56
57 static inline void print_byte(struct rtattr *attr, const char *name)
58 {
59         printf("  attr %s (len %d) 0x%02x\n", name, RTA_PAYLOAD(attr),
60                                         *((unsigned char *) RTA_DATA(attr)));
61 }
62
63 static inline void print_attr(struct rtattr *attr, const char *name)
64 {
65         if (name)
66                 printf("  attr %s (len %d)\n", name, RTA_PAYLOAD(attr));
67         else
68                 printf("  attr %d (len %d)\n",
69                                         attr->rta_type, RTA_PAYLOAD(attr));
70 }
71
72 static void rtnl_link(struct nlmsghdr *hdr)
73 {
74         struct connman_iface *iface;
75         struct ifinfomsg *msg;
76         struct rtattr *attr;
77         int bytes;
78
79         msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
80         bytes = IFLA_PAYLOAD(hdr);
81
82         DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
83
84         iface = __connman_iface_find(msg->ifi_index);
85         if (iface == NULL)
86                 return;
87
88         if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
89                 return;
90
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);
95                 else
96                         connman_iface_indicate_carrier(iface, iface->carrier);
97         }
98
99         for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
100                                         attr = RTA_NEXT(attr, bytes)) {
101                 switch (attr->rta_type) {
102                 case IFLA_ADDRESS:
103                         print_attr(attr, "address");
104                         break;
105                 case IFLA_BROADCAST:
106                         print_attr(attr, "broadcast");
107                         break;
108                 case IFLA_IFNAME:
109                         print_char(attr, "ifname");
110                         break;
111                 case IFLA_MTU:
112                         print_attr(attr, "mtu");
113                         break;
114                 case IFLA_LINK:
115                         print_attr(attr, "link");
116                         break;
117                 case IFLA_QDISC:
118                         print_attr(attr, "qdisc");
119                         break;
120                 case IFLA_STATS:
121                         print_attr(attr, "stats");
122                         break;
123                 case IFLA_COST:
124                         print_attr(attr, "cost");
125                         break;
126                 case IFLA_PRIORITY:
127                         print_attr(attr, "priority");
128                         break;
129                 case IFLA_MASTER:
130                         print_attr(attr, "master");
131                         break;
132                 case IFLA_WIRELESS:
133                         if (iface->driver->rtnl_wireless)
134                                 iface->driver->rtnl_wireless(iface,
135                                         RTA_DATA(attr), RTA_PAYLOAD(attr));
136                         break;
137                 case IFLA_PROTINFO:
138                         print_attr(attr, "protinfo");
139                         break;
140                 case IFLA_TXQLEN:
141                         print_attr(attr, "txqlen");
142                         break;
143                 case IFLA_MAP:
144                         print_attr(attr, "map");
145                         break;
146                 case IFLA_WEIGHT:
147                         print_attr(attr, "weight");
148                         break;
149                 case IFLA_OPERSTATE:
150                         print_byte(attr, "operstate");
151                         break;
152                 case IFLA_LINKMODE:
153                         print_byte(attr, "linkmode");
154                         break;
155                 default:
156                         print_attr(attr, NULL);
157                         break;
158                 }
159         }
160 }
161
162 static void rtnl_addr(struct nlmsghdr *hdr)
163 {
164         struct connman_iface *iface;
165         struct ifaddrmsg *msg;
166         struct rtattr *attr;
167         int bytes;
168
169         msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
170         bytes = IFA_PAYLOAD(hdr);
171
172         DBG("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
173
174         iface = __connman_iface_find(msg->ifa_index);
175         if (iface == NULL)
176                 return;
177
178         if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
179                 return;
180
181         for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
182                                         attr = RTA_NEXT(attr, bytes)) {
183                 switch (attr->rta_type) {
184                 case IFA_ADDRESS:
185                         print_inet(attr, "address", msg->ifa_family);
186                         break;
187                 case IFA_LOCAL:
188                         print_inet(attr, "local", msg->ifa_family);
189                         break;
190                 case IFA_LABEL:
191                         print_char(attr, "label");
192                         break;
193                 case IFA_BROADCAST:
194                         print_inet(attr, "broadcast", msg->ifa_family);
195                         break;
196                 case IFA_ANYCAST:
197                         print_attr(attr, "anycast");
198                         break;
199                 case IFA_CACHEINFO:
200                         print_attr(attr, "cacheinfo");
201                         break;
202                 case IFA_MULTICAST:
203                         print_attr(attr, "multicast");
204                         break;
205                 default:
206                         print_attr(attr, NULL);
207                         break;
208                 }
209         }
210 }
211
212 static void rtnl_route(struct nlmsghdr *hdr)
213 {
214         struct rtmsg *msg;
215         struct rtattr *attr;
216         int bytes;
217
218         msg = (struct rtmsg *) NLMSG_DATA(hdr);
219         bytes = RTM_PAYLOAD(hdr);
220
221         DBG("rtm_family %d rtm_flags 0x%04x", msg->rtm_family, msg->rtm_flags);
222
223         for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
224                                         attr = RTA_NEXT(attr, bytes)) {
225                 switch (attr->rta_type) {
226                 case RTA_DST:
227                         print_inet(attr, "dst", msg->rtm_family);
228                         break;
229                 case RTA_SRC:
230                         print_inet(attr, "src", msg->rtm_family);
231                         break;
232                 case RTA_IIF:
233                         print_char(attr, "iif");
234                         break;
235                 case RTA_OIF:
236                         print_attr(attr, "oif");
237                         break;
238                 case RTA_GATEWAY:
239                         print_inet(attr, "gateway", msg->rtm_family);
240                         break;
241                 case RTA_PRIORITY:
242                         print_attr(attr, "priority");
243                         break;
244                 case RTA_PREFSRC:
245                         print_inet(attr, "prefsrc", msg->rtm_family);
246                         break;
247                 case RTA_METRICS:
248                         print_attr(attr, "metrics");
249                         break;
250                 case RTA_TABLE:
251                         print_attr(attr, "table");
252                         break;
253                 default:
254                         print_attr(attr, NULL);
255                         break;
256                 }
257         }
258 }
259
260 static void rtnl_message(void *buf, size_t len)
261 {
262         DBG("buf %p len %d", buf, len);
263
264         while (len > 0) {
265                 struct nlmsghdr *hdr = buf;
266
267                 if (!NLMSG_OK(hdr, len))
268                         break;
269
270                 DBG("len %d type %d flags 0x%04x",
271                         hdr->nlmsg_len, hdr->nlmsg_type, hdr->nlmsg_flags);
272
273                 switch (hdr->nlmsg_type) {
274                 case NLMSG_DONE:
275                         DBG("done");
276                         return;
277                 case NLMSG_NOOP:
278                         DBG("noop");
279                         return;
280                 case NLMSG_OVERRUN:
281                         DBG("overrun");
282                         return;
283                 case NLMSG_ERROR:
284                         DBG("error");
285                         return;
286                 case RTM_NEWLINK:
287                         rtnl_link(hdr);
288                         break;
289                 case RTM_DELLINK:
290                         rtnl_link(hdr);
291                         break;
292                 case RTM_NEWADDR:
293                         rtnl_addr(hdr);
294                         break;
295                 case RTM_DELADDR:
296                         rtnl_addr(hdr);
297                         break;
298                 case RTM_NEWROUTE:
299                         rtnl_route(hdr);
300                         break;
301                 case RTM_DELROUTE:
302                         rtnl_route(hdr);
303                         break;
304                 default:
305                         DBG("type %d", hdr->nlmsg_type);
306                         break;
307                 }
308
309                 len -= hdr->nlmsg_len;
310                 buf += hdr->nlmsg_len;
311         }
312 }
313
314 static gboolean netlink_event(GIOChannel *chan,
315                                 GIOCondition cond, gpointer data)
316 {
317         unsigned char buf[256];
318         gsize len;
319         GIOError err;
320
321         if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
322                 g_io_channel_unref(chan);
323                 return FALSE;
324         }
325
326         memset(buf, 0, sizeof(buf));
327
328         err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
329         if (err) {
330                 if (err == G_IO_ERROR_AGAIN)
331                         return TRUE;
332                 g_io_channel_unref(chan);
333                 return FALSE;
334         }
335
336         rtnl_message(buf, len);
337
338         return TRUE;
339 }
340
341 static GIOChannel *channel = NULL;
342
343 int __connman_rtnl_send(const void *buf, size_t len)
344 {
345         struct sockaddr_nl addr;
346         int sk;
347
348         DBG("buf %p len %d", buf, len);
349
350         sk = g_io_channel_unix_get_fd(channel);
351
352         memset(&addr, 0, sizeof(addr));
353         addr.nl_family = AF_NETLINK;
354
355         return sendto(sk, buf, len, 0,
356                         (struct sockaddr *) &addr, sizeof(addr));
357 }
358
359 int __connman_rtnl_init(void)
360 {
361         struct sockaddr_nl addr;
362         int sk;
363
364         DBG("");
365
366         sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
367         if (sk < 0)
368                 return -1;
369
370         memset(&addr, 0, sizeof(addr));
371         addr.nl_family = AF_NETLINK;
372         addr.nl_groups = RTMGRP_LINK;
373         //addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
374         addr.nl_pid = getpid();
375
376         if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
377                 close(sk);
378                 return -1;
379         }
380
381         channel = g_io_channel_unix_new(sk);
382         g_io_channel_set_close_on_unref(channel, TRUE);
383
384         g_io_add_watch(channel,
385                         G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
386                                                 netlink_event, NULL);
387
388         g_io_channel_unref(channel);
389
390         return 0;
391 }
392
393 void __connman_rtnl_cleanup(void)
394 {
395         DBG("");
396
397         g_io_channel_unref(channel);
398
399         channel = NULL;
400 }