Fix netlink debugging and enable IPv4 address notifications
[connman] / 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                 struct nlmsgerr *err;
267
268                 if (!NLMSG_OK(hdr, len))
269                         break;
270
271                 DBG("len %d type %d flags 0x%04x seq %d",
272                                         hdr->nlmsg_len, hdr->nlmsg_type,
273                                         hdr->nlmsg_flags, hdr->nlmsg_seq);
274
275                 switch (hdr->nlmsg_type) {
276                 case NLMSG_NOOP:
277                         DBG("NOOP");
278                         return;
279                 case NLMSG_ERROR:
280                         err = NLMSG_DATA(hdr);
281                         DBG("ERROR %d (%s)", -err->error,
282                                                 strerror(-err->error));
283                         return;
284                 case NLMSG_DONE:
285                         DBG("DONE");
286                         return;
287                 case NLMSG_OVERRUN:
288                         DBG("OVERRUN");
289                         return;
290                 case RTM_NEWLINK:
291                         DBG("NEWLINK");
292                         rtnl_link(hdr);
293                         break;
294                 case RTM_DELLINK:
295                         DBG("DELLINK");
296                         rtnl_link(hdr);
297                         break;
298                 case RTM_NEWADDR:
299                         DBG("NEWADDR");
300                         rtnl_addr(hdr);
301                         break;
302                 case RTM_DELADDR:
303                         DBG("DELADDR");
304                         rtnl_addr(hdr);
305                         break;
306                 case RTM_NEWROUTE:
307                         DBG("NEWROUTE");
308                         rtnl_route(hdr);
309                         break;
310                 case RTM_DELROUTE:
311                         DBG("DELROUTE");
312                         rtnl_route(hdr);
313                         break;
314                 default:
315                         DBG("type %d", hdr->nlmsg_type);
316                         break;
317                 }
318
319                 len -= hdr->nlmsg_len;
320                 buf += hdr->nlmsg_len;
321         }
322 }
323
324 static gboolean netlink_event(GIOChannel *chan,
325                                 GIOCondition cond, gpointer data)
326 {
327         unsigned char buf[256];
328         gsize len;
329         GIOError err;
330
331         if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
332                 g_io_channel_unref(chan);
333                 return FALSE;
334         }
335
336         memset(buf, 0, sizeof(buf));
337
338         err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
339         if (err) {
340                 if (err == G_IO_ERROR_AGAIN)
341                         return TRUE;
342                 g_io_channel_unref(chan);
343                 return FALSE;
344         }
345
346         rtnl_message(buf, len);
347
348         return TRUE;
349 }
350
351 static GIOChannel *channel = NULL;
352
353 int __connman_rtnl_send(const void *buf, size_t len)
354 {
355         struct sockaddr_nl addr;
356         int sk;
357
358         DBG("buf %p len %d", buf, len);
359
360         sk = g_io_channel_unix_get_fd(channel);
361
362         memset(&addr, 0, sizeof(addr));
363         addr.nl_family = AF_NETLINK;
364
365         return sendto(sk, buf, len, 0,
366                         (struct sockaddr *) &addr, sizeof(addr));
367 }
368
369 int __connman_rtnl_init(void)
370 {
371         struct sockaddr_nl addr;
372         int sk;
373
374         DBG("");
375
376         sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
377         if (sk < 0)
378                 return -1;
379
380         memset(&addr, 0, sizeof(addr));
381         addr.nl_family = AF_NETLINK;
382         addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
383         //addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
384
385         if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
386                 close(sk);
387                 return -1;
388         }
389
390         channel = g_io_channel_unix_new(sk);
391         g_io_channel_set_close_on_unref(channel, TRUE);
392
393         g_io_add_watch(channel,
394                         G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
395                                                 netlink_event, NULL);
396
397         g_io_channel_unref(channel);
398
399         return 0;
400 }
401
402 void __connman_rtnl_cleanup(void)
403 {
404         DBG("");
405
406         g_io_channel_unref(channel);
407
408         channel = NULL;
409 }