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