Imported Upstream version 1.38
[platform/upstream/connman.git] / src / rtnl.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2013  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 <errno.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <sys/socket.h>
31 #include <sys/ioctl.h>
32 #include <arpa/inet.h>
33 #include <netinet/ether.h>
34 #include <netinet/icmp6.h>
35 #include <net/if_arp.h>
36 #include <linux/if.h>
37 #include <linux/netlink.h>
38 #include <linux/rtnetlink.h>
39 #include <linux/wireless.h>
40
41 #include <glib.h>
42
43 #include "connman.h"
44
45 #ifndef ARPHDR_PHONET_PIPE
46 #define ARPHDR_PHONET_PIPE (821)
47 #endif
48
49 #define print(arg...) do { if (0) connman_info(arg); } while (0)
50 //#define print(arg...) connman_info(arg)
51
52 struct watch_data {
53         unsigned int id;
54         int index;
55         connman_rtnl_link_cb_t newlink;
56         void *user_data;
57 };
58
59 static GSList *watch_list = NULL;
60 static unsigned int watch_id = 0;
61
62 static GSList *update_list = NULL;
63 static guint update_interval = G_MAXUINT;
64 static guint update_timeout = 0;
65
66 struct interface_data {
67         int index;
68         char *ident;
69         enum connman_service_type service_type;
70         enum connman_device_type device_type;
71 };
72
73 static GHashTable *interface_list = NULL;
74
75 static void free_interface(gpointer data)
76 {
77         struct interface_data *interface = data;
78
79         __connman_technology_remove_interface(interface->service_type,
80                         interface->index, interface->ident);
81
82         g_free(interface->ident);
83         g_free(interface);
84 }
85
86 static bool ether_blacklisted(const char *name)
87 {
88         if (!name)
89                 return true;
90
91         if (__connman_device_isfiltered(name))
92                 return true;
93
94         return false;
95 }
96
97 static bool wext_interface(char *ifname)
98 {
99         struct iwreq wrq;
100         int fd, err;
101
102         fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
103         if (fd < 0)
104                 return false;
105
106         memset(&wrq, 0, sizeof(wrq));
107         strncpy(wrq.ifr_name, ifname, sizeof(wrq.ifr_name) - 1);
108
109         err = ioctl(fd, SIOCGIWNAME, &wrq);
110
111         close(fd);
112
113         if (err < 0)
114                 return false;
115
116         return true;
117 }
118
119 static void read_uevent(struct interface_data *interface)
120 {
121         char *filename, *name, line[128];
122         bool found_devtype;
123         FILE *f;
124
125         name = connman_inet_ifname(interface->index);
126
127         if (ether_blacklisted(name)) {
128                 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
129                 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
130                 goto out;
131         } else {
132                 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
133                 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
134         }
135
136         filename = g_strdup_printf("/sys/class/net/%s/uevent", name);
137
138         f = fopen(filename, "re");
139
140         g_free(filename);
141
142         if (!f) {
143                 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
144                 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
145                 goto out;
146         }
147
148         found_devtype = false;
149         while (fgets(line, sizeof(line), f)) {
150                 char *pos;
151
152                 pos = strchr(line, '\n');
153                 if (!pos)
154                         continue;
155                 pos[0] = '\0';
156
157                 if (strncmp(line, "DEVTYPE=", 8) != 0)
158                         continue;
159
160                 found_devtype = true;
161
162                 if (strcmp(line + 8, "wlan") == 0) {
163                         interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
164                         interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
165                 } else if (strcmp(line + 8, "wwan") == 0) {
166                         interface->service_type = CONNMAN_SERVICE_TYPE_CELLULAR;
167                         interface->device_type = CONNMAN_DEVICE_TYPE_CELLULAR;
168                 } else if (strcmp(line + 8, "bluetooth") == 0) {
169                         interface->service_type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
170                         interface->device_type = CONNMAN_DEVICE_TYPE_BLUETOOTH;
171                 } else if (strcmp(line + 8, "gadget") == 0) {
172                         interface->service_type = CONNMAN_SERVICE_TYPE_GADGET;
173                         interface->device_type = CONNMAN_DEVICE_TYPE_GADGET;
174                 } else if (strcmp(line + 8, "vlan") == 0) {
175                         interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
176                         interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
177                 } else if (strcmp(line + 8, "bond") == 0) {
178                         interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
179                         interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
180                 } else {
181                         interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
182                         interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
183                 }
184         }
185
186         fclose(f);
187
188         if (found_devtype)
189                 goto out;
190
191         /* We haven't got a DEVTYPE, let's check if it's a wireless device */
192         if (wext_interface(name)) {
193                 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
194                 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
195
196                 connman_error("%s runs an unsupported 802.11 driver", name);
197         }
198
199 out:
200         g_free(name);
201 }
202
203 enum connman_device_type __connman_rtnl_get_device_type(int index)
204 {
205         struct interface_data *interface;
206
207         interface = g_hash_table_lookup(interface_list,
208                                         GINT_TO_POINTER(index));
209         if (!interface)
210                 return CONNMAN_DEVICE_TYPE_UNKNOWN;
211
212         return interface->device_type;
213 }
214
215 /**
216  * connman_rtnl_add_newlink_watch:
217  * @index: network device index
218  * @callback: callback function
219  * @user_data: callback data;
220  *
221  * Add a new RTNL watch for newlink events
222  *
223  * Returns: %0 on failure and a unique id on success
224  */
225 unsigned int connman_rtnl_add_newlink_watch(int index,
226                         connman_rtnl_link_cb_t callback, void *user_data)
227 {
228         struct watch_data *watch;
229
230         watch = g_try_new0(struct watch_data, 1);
231         if (!watch)
232                 return 0;
233
234         watch->id = ++watch_id;
235         watch->index = index;
236
237         watch->newlink = callback;
238         watch->user_data = user_data;
239
240         watch_list = g_slist_prepend(watch_list, watch);
241
242         DBG("id %d", watch->id);
243
244         if (callback) {
245                 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
246
247                 if (flags > 0)
248                         callback(flags, 0, user_data);
249         }
250
251         return watch->id;
252 }
253
254 /**
255  * connman_rtnl_remove_watch:
256  * @id: watch identifier
257  *
258  * Remove the RTNL watch for the identifier
259  */
260 void connman_rtnl_remove_watch(unsigned int id)
261 {
262         GSList *list;
263
264         DBG("id %d", id);
265
266         if (id == 0)
267                 return;
268
269         for (list = watch_list; list; list = list->next) {
270                 struct watch_data *watch = list->data;
271
272                 if (watch->id  == id) {
273                         watch_list = g_slist_remove(watch_list, watch);
274                         g_free(watch);
275                         break;
276                 }
277         }
278 }
279
280 static void trigger_rtnl(int index, void *user_data)
281 {
282         struct connman_rtnl *rtnl = user_data;
283
284         if (rtnl->newlink) {
285                 unsigned short type = __connman_ipconfig_get_type_from_index(index);
286                 unsigned int flags = __connman_ipconfig_get_flags_from_index(index);
287
288                 rtnl->newlink(type, index, flags, 0);
289         }
290
291         if (rtnl->newgateway) {
292                 const char *gateway =
293                         __connman_ipconfig_get_gateway_from_index(index,
294                                         CONNMAN_IPCONFIG_TYPE_ALL);
295
296                 if (gateway)
297                         rtnl->newgateway(index, gateway);
298         }
299 }
300
301 static GSList *rtnl_list = NULL;
302
303 static gint compare_priority(gconstpointer a, gconstpointer b)
304 {
305         const struct connman_rtnl *rtnl1 = a;
306         const struct connman_rtnl *rtnl2 = b;
307
308         return rtnl2->priority - rtnl1->priority;
309 }
310
311 /**
312  * connman_rtnl_register:
313  * @rtnl: RTNL module
314  *
315  * Register a new RTNL module
316  *
317  * Returns: %0 on success
318  */
319 int connman_rtnl_register(struct connman_rtnl *rtnl)
320 {
321         DBG("rtnl %p name %s", rtnl, rtnl->name);
322
323         rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
324                                                         compare_priority);
325
326         __connman_ipconfig_foreach(trigger_rtnl, rtnl);
327
328         return 0;
329 }
330
331 /**
332  * connman_rtnl_unregister:
333  * @rtnl: RTNL module
334  *
335  * Remove a previously registered RTNL module
336  */
337 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
338 {
339         DBG("rtnl %p name %s", rtnl, rtnl->name);
340
341         rtnl_list = g_slist_remove(rtnl_list, rtnl);
342 }
343
344 static const char *operstate2str(unsigned char operstate)
345 {
346         switch (operstate) {
347         case IF_OPER_UNKNOWN:
348                 return "UNKNOWN";
349         case IF_OPER_NOTPRESENT:
350                 return "NOT-PRESENT";
351         case IF_OPER_DOWN:
352                 return "DOWN";
353         case IF_OPER_LOWERLAYERDOWN:
354                 return "LOWER-LAYER-DOWN";
355         case IF_OPER_TESTING:
356                 return "TESTING";
357         case IF_OPER_DORMANT:
358                 return "DORMANT";
359         case IF_OPER_UP:
360                 return "UP";
361         }
362
363         return "";
364 }
365
366 static bool extract_link(struct ifinfomsg *msg, int bytes,
367                                 struct ether_addr *address, const char **ifname,
368                                 unsigned int *mtu, unsigned char *operstate,
369                                 struct rtnl_link_stats *stats)
370 {
371         struct rtattr *attr;
372
373         for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
374                                         attr = RTA_NEXT(attr, bytes)) {
375                 switch (attr->rta_type) {
376                 case IFLA_ADDRESS:
377                         if (address)
378                                 memcpy(address, RTA_DATA(attr), ETH_ALEN);
379                         break;
380                 case IFLA_IFNAME:
381                         if (ifname)
382                                 *ifname = RTA_DATA(attr);
383                         break;
384                 case IFLA_MTU:
385                         if (mtu)
386                                 *mtu = *((unsigned int *) RTA_DATA(attr));
387                         break;
388                 case IFLA_STATS:
389                         if (stats)
390                                 memcpy(stats, RTA_DATA(attr),
391                                         sizeof(struct rtnl_link_stats));
392                         break;
393                 case IFLA_OPERSTATE:
394                         if (operstate)
395                                 *operstate = *((unsigned char *) RTA_DATA(attr));
396                         break;
397                 case IFLA_LINKMODE:
398                         break;
399                 case IFLA_WIRELESS:
400                         return false;
401                 }
402         }
403
404         return true;
405 }
406
407 static void process_newlink(unsigned short type, int index, unsigned flags,
408                         unsigned change, struct ifinfomsg *msg, int bytes)
409 {
410         struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
411         struct rtnl_link_stats stats;
412         unsigned char operstate = 0xff;
413         struct interface_data *interface;
414         const char *ifname = NULL;
415         unsigned int mtu = 0;
416         char ident[13], str[18];
417         GSList *list;
418
419         memset(&stats, 0, sizeof(stats));
420         if (!extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats))
421                 return;
422
423         snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
424                                                 address.ether_addr_octet[0],
425                                                 address.ether_addr_octet[1],
426                                                 address.ether_addr_octet[2],
427                                                 address.ether_addr_octet[3],
428                                                 address.ether_addr_octet[4],
429                                                 address.ether_addr_octet[5]);
430
431         snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
432                                                 address.ether_addr_octet[0],
433                                                 address.ether_addr_octet[1],
434                                                 address.ether_addr_octet[2],
435                                                 address.ether_addr_octet[3],
436                                                 address.ether_addr_octet[4],
437                                                 address.ether_addr_octet[5]);
438
439         if (flags & IFF_SLAVE) {
440                 connman_info("%s {newlink} ignoring slave, index %d address %s",
441                                                 ifname, index, str);
442                 return;
443         }
444
445         switch (type) {
446         case ARPHRD_ETHER:
447         case ARPHRD_LOOPBACK:
448         case ARPHDR_PHONET_PIPE:
449         case ARPHRD_PPP:
450         case ARPHRD_NONE:
451                 __connman_ipconfig_newlink(index, type, flags,
452                                                         str, mtu, &stats);
453                 break;
454         }
455
456         connman_info("%s {newlink} index %d address %s mtu %u",
457                                         ifname, index, str, mtu);
458
459         if (operstate != 0xff)
460                 connman_info("%s {newlink} index %d operstate %u <%s>",
461                                                 ifname, index, operstate,
462                                                 operstate2str(operstate));
463
464         interface = g_hash_table_lookup(interface_list, GINT_TO_POINTER(index));
465         if (!interface) {
466                 interface = g_new0(struct interface_data, 1);
467                 interface->index = index;
468                 interface->ident = g_strdup(ident);
469
470                 g_hash_table_insert(interface_list,
471                                         GINT_TO_POINTER(index), interface);
472
473                 if (type == ARPHRD_ETHER)
474                         read_uevent(interface);
475         } else if (type == ARPHRD_ETHER && interface->device_type == CONNMAN_DEVICE_TYPE_UNKNOWN)
476                 read_uevent(interface);
477         else
478                 interface = NULL;
479
480         for (list = rtnl_list; list; list = list->next) {
481                 struct connman_rtnl *rtnl = list->data;
482
483                 if (rtnl->newlink)
484                         rtnl->newlink(type, index, flags, change);
485         }
486
487         /*
488          * The interface needs to be added after the newlink call.
489          * The newlink will create the technology when needed and
490          * __connman_technology_add_interface() expects the
491          * technology to be there already.
492          */
493         if (interface)
494                 __connman_technology_add_interface(interface->service_type,
495                         interface->index, interface->ident);
496
497         list = watch_list;
498         while (list) {
499                 GSList *next = list->next;
500                 struct watch_data *watch = list->data;
501
502                 if (watch->index == index && watch->newlink)
503                         watch->newlink(flags, change, watch->user_data);
504
505                 list = next;
506         }
507 }
508
509 static void process_dellink(unsigned short type, int index, unsigned flags,
510                         unsigned change, struct ifinfomsg *msg, int bytes)
511 {
512         struct rtnl_link_stats stats;
513         unsigned char operstate = 0xff;
514         const char *ifname = NULL;
515         GSList *list;
516
517         memset(&stats, 0, sizeof(stats));
518         if (!extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats))
519                 return;
520
521         if (operstate != 0xff)
522                 connman_info("%s {dellink} index %d operstate %u <%s>",
523                                                 ifname, index, operstate,
524                                                 operstate2str(operstate));
525
526         for (list = rtnl_list; list; list = list->next) {
527                 struct connman_rtnl *rtnl = list->data;
528
529                 if (rtnl->dellink)
530                         rtnl->dellink(type, index, flags, change);
531         }
532
533         switch (type) {
534         case ARPHRD_ETHER:
535         case ARPHRD_LOOPBACK:
536         case ARPHDR_PHONET_PIPE:
537         case ARPHRD_PPP:
538         case ARPHRD_NONE:
539                 __connman_ipconfig_dellink(index, &stats);
540                 break;
541         }
542
543         g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
544 }
545
546 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
547                                                 const char **label,
548                                                 struct in_addr *local,
549                                                 struct in_addr *address,
550                                                 struct in_addr *broadcast)
551 {
552         struct rtattr *attr;
553
554         for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
555                                         attr = RTA_NEXT(attr, bytes)) {
556                 switch (attr->rta_type) {
557                 case IFA_ADDRESS:
558                         if (address)
559                                 *address = *((struct in_addr *) RTA_DATA(attr));
560                         break;
561                 case IFA_LOCAL:
562                         if (local)
563                                 *local = *((struct in_addr *) RTA_DATA(attr));
564                         break;
565                 case IFA_BROADCAST:
566                         if (broadcast)
567                                 *broadcast = *((struct in_addr *) RTA_DATA(attr));
568                         break;
569                 case IFA_LABEL:
570                         if (label)
571                                 *label = RTA_DATA(attr);
572                         break;
573                 }
574         }
575 }
576
577 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
578                                                 struct in6_addr *addr,
579                                                 struct in6_addr *local)
580 {
581         struct rtattr *attr;
582
583         for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
584                                         attr = RTA_NEXT(attr, bytes)) {
585                 switch (attr->rta_type) {
586                 case IFA_ADDRESS:
587                         if (addr)
588                                 *addr = *((struct in6_addr *) RTA_DATA(attr));
589                         break;
590                 case IFA_LOCAL:
591                         if (local)
592                                 *local = *((struct in6_addr *) RTA_DATA(attr));
593                         break;
594                 }
595         }
596 }
597
598 static void process_newaddr(unsigned char family, unsigned char prefixlen,
599                                 int index, struct ifaddrmsg *msg, int bytes)
600 {
601         struct in_addr ipv4_addr = { INADDR_ANY };
602         struct in6_addr ipv6_address, ipv6_local;
603         const char *label = NULL;
604         void *src;
605         char ip_string[INET6_ADDRSTRLEN];
606
607         if (family == AF_INET) {
608
609                 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
610                 src = &ipv4_addr;
611         } else if (family == AF_INET6) {
612                 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
613                 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
614                         return;
615
616                 src = &ipv6_address;
617         } else {
618                 return;
619         }
620
621         if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
622                 return;
623
624         if (__connman_ipconfig_newaddr(index, family, label,
625                                         prefixlen, ip_string) >= 0) {
626                 if (family == AF_INET6) {
627                         /*
628                          * Re-create RDNSS configured servers if there
629                          * are any for this interface. This is done
630                          * because we might have now properly
631                          * configured interface with proper
632                          * autoconfigured address.
633                          */
634                         __connman_resolver_redo_servers(index);
635                 }
636         }
637 }
638
639 static void process_deladdr(unsigned char family, unsigned char prefixlen,
640                                 int index, struct ifaddrmsg *msg, int bytes)
641 {
642         struct in_addr ipv4_addr = { INADDR_ANY };
643         struct in6_addr ipv6_address, ipv6_local;
644         const char *label = NULL;
645         void *src;
646         char ip_string[INET6_ADDRSTRLEN];
647
648         if (family == AF_INET) {
649                 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
650                 src = &ipv4_addr;
651         } else if (family == AF_INET6) {
652                 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
653                 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
654                         return;
655
656                 src = &ipv6_address;
657         } else {
658                 return;
659         }
660
661         if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
662                 return;
663
664         __connman_ipconfig_deladdr(index, family, label,
665                                         prefixlen, ip_string);
666 }
667
668 static void extract_ipv4_route(struct rtmsg *msg, int bytes, int *index,
669                                                 struct in_addr *dst,
670                                                 struct in_addr *gateway)
671 {
672         struct rtattr *attr;
673
674         for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
675                                         attr = RTA_NEXT(attr, bytes)) {
676                 switch (attr->rta_type) {
677                 case RTA_DST:
678                         if (dst)
679                                 *dst = *((struct in_addr *) RTA_DATA(attr));
680                         break;
681                 case RTA_GATEWAY:
682                         if (gateway)
683                                 *gateway = *((struct in_addr *) RTA_DATA(attr));
684                         break;
685                 case RTA_OIF:
686                         if (index)
687                                 *index = *((int *) RTA_DATA(attr));
688                         break;
689                 }
690         }
691 }
692
693 static void extract_ipv6_route(struct rtmsg *msg, int bytes, int *index,
694                                                 struct in6_addr *dst,
695                                                 struct in6_addr *gateway)
696 {
697         struct rtattr *attr;
698
699         for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
700                                         attr = RTA_NEXT(attr, bytes)) {
701                 switch (attr->rta_type) {
702                 case RTA_DST:
703                         if (dst)
704                                 *dst = *((struct in6_addr *) RTA_DATA(attr));
705                         break;
706                 case RTA_GATEWAY:
707                         if (gateway)
708                                 *gateway =
709                                         *((struct in6_addr *) RTA_DATA(attr));
710                         break;
711                 case RTA_OIF:
712                         if (index)
713                                 *index = *((int *) RTA_DATA(attr));
714                         break;
715                 }
716         }
717 }
718
719 static void process_newroute(unsigned char family, unsigned char scope,
720                                                 struct rtmsg *msg, int bytes)
721 {
722         GSList *list;
723         char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
724         int index = -1;
725
726         if (family == AF_INET) {
727                 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
728
729                 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
730
731                 inet_ntop(family, &dst, dststr, sizeof(dststr));
732                 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
733
734                 __connman_ipconfig_newroute(index, family, scope, dststr,
735                                                                 gatewaystr);
736
737                 /* skip host specific routes */
738                 if (scope != RT_SCOPE_UNIVERSE &&
739                         !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
740                         return;
741
742                 if (dst.s_addr != INADDR_ANY)
743                         return;
744
745         } else if (family == AF_INET6) {
746                 struct in6_addr dst = IN6ADDR_ANY_INIT,
747                                 gateway = IN6ADDR_ANY_INIT;
748
749                 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
750
751                 inet_ntop(family, &dst, dststr, sizeof(dststr));
752                 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
753
754                 __connman_ipconfig_newroute(index, family, scope, dststr,
755                                                                 gatewaystr);
756
757                 /* skip host specific routes */
758                 if (scope != RT_SCOPE_UNIVERSE &&
759                         !(scope == RT_SCOPE_LINK &&
760                                 IN6_IS_ADDR_UNSPECIFIED(&dst)))
761                         return;
762
763                 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
764                         return;
765         } else
766                 return;
767
768         for (list = rtnl_list; list; list = list->next) {
769                 struct connman_rtnl *rtnl = list->data;
770
771                 if (rtnl->newgateway)
772                         rtnl->newgateway(index, gatewaystr);
773         }
774 }
775
776 static void process_delroute(unsigned char family, unsigned char scope,
777                                                 struct rtmsg *msg, int bytes)
778 {
779         GSList *list;
780         char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
781         int index = -1;
782
783         if (family == AF_INET) {
784                 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
785
786                 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
787
788                 inet_ntop(family, &dst, dststr, sizeof(dststr));
789                 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
790
791                 __connman_ipconfig_delroute(index, family, scope, dststr,
792                                                                 gatewaystr);
793
794                 /* skip host specific routes */
795                 if (scope != RT_SCOPE_UNIVERSE &&
796                         !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
797                         return;
798
799                 if (dst.s_addr != INADDR_ANY)
800                         return;
801
802         }  else if (family == AF_INET6) {
803                 struct in6_addr dst = IN6ADDR_ANY_INIT,
804                                 gateway = IN6ADDR_ANY_INIT;
805
806                 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
807
808                 inet_ntop(family, &dst, dststr, sizeof(dststr));
809                 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
810
811                 __connman_ipconfig_delroute(index, family, scope, dststr,
812                                                 gatewaystr);
813
814                 /* skip host specific routes */
815                 if (scope != RT_SCOPE_UNIVERSE &&
816                         !(scope == RT_SCOPE_LINK &&
817                                 IN6_IS_ADDR_UNSPECIFIED(&dst)))
818                         return;
819
820                 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
821                         return;
822         } else
823                 return;
824
825         for (list = rtnl_list; list; list = list->next) {
826                 struct connman_rtnl *rtnl = list->data;
827
828                 if (rtnl->delgateway)
829                         rtnl->delgateway(index, gatewaystr);
830         }
831 }
832
833 static inline void print_ether(struct rtattr *attr, const char *name)
834 {
835         int len = (int) RTA_PAYLOAD(attr);
836
837         if (len == ETH_ALEN) {
838                 struct ether_addr eth;
839                 memcpy(&eth, RTA_DATA(attr), ETH_ALEN);
840                 print("  attr %s (len %d) %s\n", name, len, ether_ntoa(&eth));
841         } else
842                 print("  attr %s (len %d)\n", name, len);
843 }
844
845 static inline void print_inet(struct rtattr *attr, const char *name,
846                                                         unsigned char family)
847 {
848         int len = (int) RTA_PAYLOAD(attr);
849
850         if (family == AF_INET && len == sizeof(struct in_addr)) {
851                 struct in_addr addr;
852                 addr = *((struct in_addr *) RTA_DATA(attr));
853                 print("  attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
854         } else
855                 print("  attr %s (len %d)\n", name, len);
856 }
857
858 static inline void print_string(struct rtattr *attr, const char *name)
859 {
860         print("  attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
861                                                 (char *) RTA_DATA(attr));
862 }
863
864 static inline void print_byte(struct rtattr *attr, const char *name)
865 {
866         print("  attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
867                                         *((unsigned char *) RTA_DATA(attr)));
868 }
869
870 static inline void print_integer(struct rtattr *attr, const char *name)
871 {
872         print("  attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
873                                                 *((int *) RTA_DATA(attr)));
874 }
875
876 static inline void print_attr(struct rtattr *attr, const char *name)
877 {
878         int len = (int) RTA_PAYLOAD(attr);
879
880         if (name && len > 0)
881                 print("  attr %s (len %d)\n", name, len);
882         else
883                 print("  attr %d (len %d)\n", attr->rta_type, len);
884 }
885
886 static void rtnl_link(struct nlmsghdr *hdr)
887 {
888         struct ifinfomsg *msg;
889         struct rtattr *attr;
890         int bytes;
891
892         msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
893         bytes = IFLA_PAYLOAD(hdr);
894
895         print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
896
897         for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
898                                         attr = RTA_NEXT(attr, bytes)) {
899                 switch (attr->rta_type) {
900                 case IFLA_ADDRESS:
901                         print_ether(attr, "address");
902                         break;
903                 case IFLA_BROADCAST:
904                         print_ether(attr, "broadcast");
905                         break;
906                 case IFLA_IFNAME:
907                         print_string(attr, "ifname");
908                         break;
909                 case IFLA_MTU:
910                         print_integer(attr, "mtu");
911                         break;
912                 case IFLA_LINK:
913                         print_attr(attr, "link");
914                         break;
915                 case IFLA_QDISC:
916                         print_attr(attr, "qdisc");
917                         break;
918                 case IFLA_STATS:
919                         print_attr(attr, "stats");
920                         break;
921                 case IFLA_COST:
922                         print_attr(attr, "cost");
923                         break;
924                 case IFLA_PRIORITY:
925                         print_attr(attr, "priority");
926                         break;
927                 case IFLA_MASTER:
928                         print_attr(attr, "master");
929                         break;
930                 case IFLA_WIRELESS:
931                         print_attr(attr, "wireless");
932                         break;
933                 case IFLA_PROTINFO:
934                         print_attr(attr, "protinfo");
935                         break;
936                 case IFLA_TXQLEN:
937                         print_integer(attr, "txqlen");
938                         break;
939                 case IFLA_MAP:
940                         print_attr(attr, "map");
941                         break;
942                 case IFLA_WEIGHT:
943                         print_attr(attr, "weight");
944                         break;
945                 case IFLA_OPERSTATE:
946                         print_byte(attr, "operstate");
947                         break;
948                 case IFLA_LINKMODE:
949                         print_byte(attr, "linkmode");
950                         break;
951                 default:
952                         print_attr(attr, NULL);
953                         break;
954                 }
955         }
956 }
957
958 static void rtnl_newlink(struct nlmsghdr *hdr)
959 {
960         struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
961
962         rtnl_link(hdr);
963
964         if (hdr->nlmsg_type == IFLA_WIRELESS)
965                 connman_warn_once("Obsolete WEXT WiFi driver detected");
966
967         process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
968                                 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
969 }
970
971 static void rtnl_dellink(struct nlmsghdr *hdr)
972 {
973         struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
974
975         rtnl_link(hdr);
976
977         process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
978                                 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
979 }
980
981 static void rtnl_addr(struct nlmsghdr *hdr)
982 {
983         struct ifaddrmsg *msg;
984         struct rtattr *attr;
985         int bytes;
986
987         msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
988         bytes = IFA_PAYLOAD(hdr);
989
990         print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
991
992         for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
993                                         attr = RTA_NEXT(attr, bytes)) {
994                 switch (attr->rta_type) {
995                 case IFA_ADDRESS:
996                         print_inet(attr, "address", msg->ifa_family);
997                         break;
998                 case IFA_LOCAL:
999                         print_inet(attr, "local", msg->ifa_family);
1000                         break;
1001                 case IFA_LABEL:
1002                         print_string(attr, "label");
1003                         break;
1004                 case IFA_BROADCAST:
1005                         print_inet(attr, "broadcast", msg->ifa_family);
1006                         break;
1007                 case IFA_ANYCAST:
1008                         print_attr(attr, "anycast");
1009                         break;
1010                 case IFA_CACHEINFO:
1011                         print_attr(attr, "cacheinfo");
1012                         break;
1013                 case IFA_MULTICAST:
1014                         print_attr(attr, "multicast");
1015                         break;
1016                 default:
1017                         print_attr(attr, NULL);
1018                         break;
1019                 }
1020         }
1021 }
1022
1023 static void rtnl_newaddr(struct nlmsghdr *hdr)
1024 {
1025         struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1026
1027         rtnl_addr(hdr);
1028
1029         process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1030                                                 msg, IFA_PAYLOAD(hdr));
1031 }
1032
1033 static void rtnl_deladdr(struct nlmsghdr *hdr)
1034 {
1035         struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1036
1037         rtnl_addr(hdr);
1038
1039         process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1040                                                 msg, IFA_PAYLOAD(hdr));
1041 }
1042
1043 static void rtnl_route(struct nlmsghdr *hdr)
1044 {
1045         struct rtmsg *msg;
1046         struct rtattr *attr;
1047         int bytes;
1048
1049         msg = (struct rtmsg *) NLMSG_DATA(hdr);
1050         bytes = RTM_PAYLOAD(hdr);
1051
1052         print("rtm_family %d rtm_table %d rtm_protocol %d",
1053                         msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
1054         print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
1055                                 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
1056
1057         for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
1058                                         attr = RTA_NEXT(attr, bytes)) {
1059                 switch (attr->rta_type) {
1060                 case RTA_DST:
1061                         print_inet(attr, "dst", msg->rtm_family);
1062                         break;
1063                 case RTA_SRC:
1064                         print_inet(attr, "src", msg->rtm_family);
1065                         break;
1066                 case RTA_IIF:
1067                         print_string(attr, "iif");
1068                         break;
1069                 case RTA_OIF:
1070                         print_integer(attr, "oif");
1071                         break;
1072                 case RTA_GATEWAY:
1073                         print_inet(attr, "gateway", msg->rtm_family);
1074                         break;
1075                 case RTA_PRIORITY:
1076                         print_attr(attr, "priority");
1077                         break;
1078                 case RTA_PREFSRC:
1079                         print_inet(attr, "prefsrc", msg->rtm_family);
1080                         break;
1081                 case RTA_METRICS:
1082                         print_attr(attr, "metrics");
1083                         break;
1084                 case RTA_TABLE:
1085                         print_integer(attr, "table");
1086                         break;
1087                 default:
1088                         print_attr(attr, NULL);
1089                         break;
1090                 }
1091         }
1092 }
1093
1094 static bool is_route_rtmsg(struct rtmsg *msg)
1095 {
1096         if (msg->rtm_flags & RTM_F_CLONED)
1097                 return false;
1098
1099         if (msg->rtm_table != RT_TABLE_MAIN)
1100                 return false;
1101
1102         if (msg->rtm_protocol != RTPROT_BOOT &&
1103                         msg->rtm_protocol != RTPROT_KERNEL)
1104                 return false;
1105
1106         if (msg->rtm_type != RTN_UNICAST)
1107                 return false;
1108
1109         return true;
1110 }
1111
1112 static void rtnl_newroute(struct nlmsghdr *hdr)
1113 {
1114         struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1115
1116         rtnl_route(hdr);
1117
1118         if (is_route_rtmsg(msg))
1119                 process_newroute(msg->rtm_family, msg->rtm_scope,
1120                                                 msg, RTM_PAYLOAD(hdr));
1121 }
1122
1123 static void rtnl_delroute(struct nlmsghdr *hdr)
1124 {
1125         struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1126
1127         rtnl_route(hdr);
1128
1129         if (is_route_rtmsg(msg))
1130                 process_delroute(msg->rtm_family, msg->rtm_scope,
1131                                                 msg, RTM_PAYLOAD(hdr));
1132 }
1133
1134 static void *rtnl_nd_opt_rdnss(struct nd_opt_hdr *opt, guint32 *lifetime,
1135                                int *nr_servers)
1136 {
1137         guint32 *optint = (void *)opt;
1138
1139         if (opt->nd_opt_len < 3)
1140                 return NULL;
1141
1142         if (*lifetime > ntohl(optint[1]))
1143                 *lifetime = ntohl(optint[1]);
1144
1145         /* nd_opt_len is in units of 8 bytes. The header is 1 unit (8 bytes)
1146            and each address is another 2 units (16 bytes).
1147            So the number of addresses (given rounding) is nd_opt_len/2 */
1148         *nr_servers = opt->nd_opt_len / 2;
1149
1150         /* And they start 8 bytes into the packet, or two guint32s in. */
1151         return optint + 2;
1152 }
1153
1154 static const char **rtnl_nd_opt_dnssl(struct nd_opt_hdr *opt, guint32 *lifetime)
1155 {
1156         const char **domains = NULL;
1157         guint32 *optint = (void *)opt;
1158         unsigned char *optc = (void *)&optint[2];
1159         int data_len = (opt->nd_opt_len * 8) - 8;
1160         int nr_domains = 0;
1161         int i, tmp;
1162
1163         if (*lifetime > ntohl(optint[1]))
1164                 *lifetime = ntohl(optint[1]);
1165
1166         /* Turn it into normal strings by converting the length bytes into '.',
1167            and count how many search domains there are while we're at it. */
1168         i = 0;
1169         while (i < data_len) {
1170                 if (optc[i] > 0x3f) {
1171                         DBG("DNSSL contains compressed elements in violation of RFC6106");
1172                         return NULL;
1173                 }
1174
1175                 if (optc[i] == 0) {
1176                         nr_domains++;
1177                         i++;
1178                         /* Check for double zero */
1179                         if (i < data_len && optc[i] == 0)
1180                                 break;
1181                         continue;
1182                 }
1183
1184                 tmp = i;
1185                 i += optc[i] + 1;
1186
1187                 if (i >= data_len) {
1188                         DBG("DNSSL data overflows option length");
1189                         return NULL;
1190                 }
1191
1192                 optc[tmp] = '.';
1193         }
1194
1195         domains = g_try_new0(const char *, nr_domains + 1);
1196         if (!domains)
1197                 return NULL;
1198
1199         /* Now point to the normal strings, missing out the leading '.' that
1200            each of them will have now. */
1201         for (i = 0; i < nr_domains; i++) {
1202                 domains[i] = (char *)optc + 1;
1203                 optc += strlen((char *)optc) + 1;
1204         }
1205
1206         return domains;
1207 }
1208
1209 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1210 {
1211         struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1212         struct nd_opt_hdr *opt;
1213         guint32 lifetime = -1;
1214         const char **domains = NULL;
1215         struct in6_addr *servers = NULL;
1216         int i, nr_servers = 0;
1217         int msglen = msg->nduseropt_opts_len;
1218         int index;
1219
1220         DBG("family %d index %d len %d type %d code %d",
1221                 msg->nduseropt_family, msg->nduseropt_ifindex,
1222                 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1223                 msg->nduseropt_icmp_code);
1224
1225         if (msg->nduseropt_family != AF_INET6 ||
1226                         msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1227                         msg->nduseropt_icmp_code != 0)
1228                 return;
1229
1230         index = msg->nduseropt_ifindex;
1231         if (index < 0)
1232                 return;
1233
1234         for (opt = (void *)&msg[1];
1235                         msglen > 0;
1236                         msglen -= opt->nd_opt_len * 8,
1237                         opt = ((void *)opt) + opt->nd_opt_len*8) {
1238
1239                 DBG("remaining %d nd opt type %d len %d\n",
1240                         msglen, opt->nd_opt_type, opt->nd_opt_len);
1241
1242                 if (opt->nd_opt_type == 25) { /* ND_OPT_RDNSS */
1243                         char buf[40];
1244
1245                         servers = rtnl_nd_opt_rdnss(opt, &lifetime,
1246                                                                 &nr_servers);
1247                         for (i = 0; i < nr_servers; i++) {
1248                                 if (!inet_ntop(AF_INET6, servers + i, buf,
1249                                                                 sizeof(buf)))
1250                                         continue;
1251
1252                                 connman_resolver_append_lifetime(index,
1253                                                         NULL, buf, lifetime);
1254                         }
1255
1256                 } else if (opt->nd_opt_type == 31) { /* ND_OPT_DNSSL */
1257                         g_free(domains);
1258
1259                         domains = rtnl_nd_opt_dnssl(opt, &lifetime);
1260                         for (i = 0; domains && domains[i]; i++)
1261                                 connman_resolver_append_lifetime(index,
1262                                                 domains[i], NULL, lifetime);
1263                 }
1264         }
1265
1266         g_free(domains);
1267 }
1268
1269 static const char *type2string(uint16_t type)
1270 {
1271         switch (type) {
1272         case NLMSG_NOOP:
1273                 return "NOOP";
1274         case NLMSG_ERROR:
1275                 return "ERROR";
1276         case NLMSG_DONE:
1277                 return "DONE";
1278         case NLMSG_OVERRUN:
1279                 return "OVERRUN";
1280         case RTM_GETLINK:
1281                 return "GETLINK";
1282         case RTM_NEWLINK:
1283                 return "NEWLINK";
1284         case RTM_DELLINK:
1285                 return "DELLINK";
1286         case RTM_GETADDR:
1287                 return "GETADDR";
1288         case RTM_NEWADDR:
1289                 return "NEWADDR";
1290         case RTM_DELADDR:
1291                 return "DELADDR";
1292         case RTM_GETROUTE:
1293                 return "GETROUTE";
1294         case RTM_NEWROUTE:
1295                 return "NEWROUTE";
1296         case RTM_DELROUTE:
1297                 return "DELROUTE";
1298         case RTM_NEWNDUSEROPT:
1299                 return "NEWNDUSEROPT";
1300         default:
1301                 return "UNKNOWN";
1302         }
1303 }
1304
1305 static GIOChannel *channel = NULL;
1306 static guint channel_watch = 0;
1307
1308 struct rtnl_request {
1309         struct nlmsghdr hdr;
1310         struct rtgenmsg msg;
1311 };
1312 #define RTNL_REQUEST_SIZE  (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1313
1314 static GSList *request_list = NULL;
1315 static guint32 request_seq = 0;
1316
1317 static struct rtnl_request *find_request(guint32 seq)
1318 {
1319         GSList *list;
1320
1321         for (list = request_list; list; list = list->next) {
1322                 struct rtnl_request *req = list->data;
1323
1324                 if (req->hdr.nlmsg_seq == seq)
1325                         return req;
1326         }
1327
1328         return NULL;
1329 }
1330
1331 static int send_request(struct rtnl_request *req)
1332 {
1333         struct sockaddr_nl addr;
1334         int sk;
1335
1336         DBG("%s len %d type %d flags 0x%04x seq %d",
1337                                 type2string(req->hdr.nlmsg_type),
1338                                 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1339                                 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1340
1341         sk = g_io_channel_unix_get_fd(channel);
1342
1343         memset(&addr, 0, sizeof(addr));
1344         addr.nl_family = AF_NETLINK;
1345
1346         return sendto(sk, req, req->hdr.nlmsg_len, 0,
1347                                 (struct sockaddr *) &addr, sizeof(addr));
1348 }
1349
1350 static int queue_request(struct rtnl_request *req)
1351 {
1352         request_list = g_slist_append(request_list, req);
1353
1354         if (g_slist_length(request_list) > 1)
1355                 return 0;
1356
1357         return send_request(req);
1358 }
1359
1360 static int process_response(guint32 seq)
1361 {
1362         struct rtnl_request *req;
1363
1364         DBG("seq %d", seq);
1365
1366         req = find_request(seq);
1367         if (req) {
1368                 request_list = g_slist_remove(request_list, req);
1369                 g_free(req);
1370         }
1371
1372         req = g_slist_nth_data(request_list, 0);
1373         if (!req)
1374                 return 0;
1375
1376         return send_request(req);
1377 }
1378
1379 static void rtnl_message(void *buf, size_t len)
1380 {
1381         while (len > 0) {
1382                 struct nlmsghdr *hdr = buf;
1383                 struct nlmsgerr *err;
1384
1385                 if (!NLMSG_OK(hdr, len))
1386                         break;
1387
1388                 DBG("%s len %u type %u flags 0x%04x seq %u pid %u",
1389                                         type2string(hdr->nlmsg_type),
1390                                         hdr->nlmsg_len, hdr->nlmsg_type,
1391                                         hdr->nlmsg_flags, hdr->nlmsg_seq,
1392                                         hdr->nlmsg_pid);
1393
1394                 switch (hdr->nlmsg_type) {
1395                 case NLMSG_NOOP:
1396                 case NLMSG_OVERRUN:
1397                         return;
1398                 case NLMSG_DONE:
1399                         process_response(hdr->nlmsg_seq);
1400                         return;
1401                 case NLMSG_ERROR:
1402                         err = NLMSG_DATA(hdr);
1403                         DBG("error %d (%s)", -err->error,
1404                                                 strerror(-err->error));
1405                         return;
1406                 case RTM_NEWLINK:
1407                         rtnl_newlink(hdr);
1408                         break;
1409                 case RTM_DELLINK:
1410                         rtnl_dellink(hdr);
1411                         break;
1412                 case RTM_NEWADDR:
1413                         rtnl_newaddr(hdr);
1414                         break;
1415                 case RTM_DELADDR:
1416                         rtnl_deladdr(hdr);
1417                         break;
1418                 case RTM_NEWROUTE:
1419                         rtnl_newroute(hdr);
1420                         break;
1421                 case RTM_DELROUTE:
1422                         rtnl_delroute(hdr);
1423                         break;
1424                 case RTM_NEWNDUSEROPT:
1425                         rtnl_newnduseropt(hdr);
1426                         break;
1427                 }
1428
1429                 len -= hdr->nlmsg_len;
1430                 buf += hdr->nlmsg_len;
1431         }
1432 }
1433
1434 static gboolean netlink_event(GIOChannel *chan, GIOCondition cond, gpointer data)
1435 {
1436         unsigned char buf[4096];
1437         struct sockaddr_nl nladdr;
1438         socklen_t addr_len = sizeof(nladdr);
1439         ssize_t status;
1440         int fd;
1441
1442         if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1443                 return FALSE;
1444
1445         memset(buf, 0, sizeof(buf));
1446         memset(&nladdr, 0, sizeof(nladdr));
1447
1448         fd = g_io_channel_unix_get_fd(chan);
1449
1450         status = recvfrom(fd, buf, sizeof(buf), 0,
1451                        (struct sockaddr *) &nladdr, &addr_len);
1452         if (status < 0) {
1453                 if (errno == EINTR || errno == EAGAIN)
1454                         return TRUE;
1455
1456                 return FALSE;
1457         }
1458
1459         if (status == 0)
1460                 return FALSE;
1461
1462         if (nladdr.nl_pid != 0) { /* not sent by kernel, ignore */
1463                 DBG("Received msg from %u, ignoring it", nladdr.nl_pid);
1464                 return TRUE;
1465         }
1466
1467         rtnl_message(buf, status);
1468
1469         return TRUE;
1470 }
1471
1472 static int send_getlink(void)
1473 {
1474         struct rtnl_request *req;
1475
1476         DBG("");
1477
1478         req = g_try_malloc0(RTNL_REQUEST_SIZE);
1479         if (!req)
1480                 return -ENOMEM;
1481
1482         req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1483         req->hdr.nlmsg_type = RTM_GETLINK;
1484         req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1485         req->hdr.nlmsg_pid = 0;
1486         req->hdr.nlmsg_seq = request_seq++;
1487         req->msg.rtgen_family = AF_INET;
1488
1489         return queue_request(req);
1490 }
1491
1492 static int send_getaddr(void)
1493 {
1494         struct rtnl_request *req;
1495
1496         DBG("");
1497
1498         req = g_try_malloc0(RTNL_REQUEST_SIZE);
1499         if (!req)
1500                 return -ENOMEM;
1501
1502         req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1503         req->hdr.nlmsg_type = RTM_GETADDR;
1504         req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1505         req->hdr.nlmsg_pid = 0;
1506         req->hdr.nlmsg_seq = request_seq++;
1507         req->msg.rtgen_family = AF_INET;
1508
1509         return queue_request(req);
1510 }
1511
1512 static int send_getroute(void)
1513 {
1514         struct rtnl_request *req;
1515
1516         DBG("");
1517
1518         req = g_try_malloc0(RTNL_REQUEST_SIZE);
1519         if (!req)
1520                 return -ENOMEM;
1521
1522         req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1523         req->hdr.nlmsg_type = RTM_GETROUTE;
1524         req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1525         req->hdr.nlmsg_pid = 0;
1526         req->hdr.nlmsg_seq = request_seq++;
1527         req->msg.rtgen_family = AF_INET;
1528
1529         return queue_request(req);
1530 }
1531
1532 static gboolean update_timeout_cb(gpointer user_data)
1533 {
1534         __connman_rtnl_request_update();
1535
1536         return TRUE;
1537 }
1538
1539 static void update_interval_callback(guint min)
1540 {
1541         if (update_timeout > 0)
1542                 g_source_remove(update_timeout);
1543
1544         if (min < G_MAXUINT) {
1545                 update_interval = min;
1546                 update_timeout = g_timeout_add_seconds(update_interval,
1547                                                 update_timeout_cb, NULL);
1548         } else {
1549                 update_timeout = 0;
1550                 update_interval = G_MAXUINT;
1551         }
1552 }
1553
1554 static gint compare_interval(gconstpointer a, gconstpointer b)
1555 {
1556         guint val_a = GPOINTER_TO_UINT(a);
1557         guint val_b = GPOINTER_TO_UINT(b);
1558
1559         return val_a - val_b;
1560 }
1561
1562 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1563 {
1564         guint min;
1565
1566         if (interval == 0)
1567                 return 0;
1568
1569         update_list = g_slist_insert_sorted(update_list,
1570                         GUINT_TO_POINTER(interval), compare_interval);
1571
1572         min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1573         if (min < update_interval) {
1574                 update_interval_callback(min);
1575                 __connman_rtnl_request_update();
1576         }
1577
1578         return update_interval;
1579 }
1580
1581 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1582 {
1583         guint min = G_MAXUINT;
1584
1585         if (interval == 0)
1586                 return 0;
1587
1588         update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1589
1590         if (update_list)
1591                 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1592
1593         if (min > update_interval)
1594                 update_interval_callback(min);
1595
1596         return min;
1597 }
1598
1599 int __connman_rtnl_request_update(void)
1600 {
1601         return send_getlink();
1602 }
1603
1604 int __connman_rtnl_init(void)
1605 {
1606         struct sockaddr_nl addr;
1607         int sk;
1608
1609         DBG("");
1610
1611         interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1612                                                         NULL, free_interface);
1613
1614         sk = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
1615         if (sk < 0)
1616                 return -1;
1617
1618         memset(&addr, 0, sizeof(addr));
1619         addr.nl_family = AF_NETLINK;
1620         addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1621                                 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1622                                 (1<<(RTNLGRP_ND_USEROPT-1));
1623
1624         if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1625                 close(sk);
1626                 return -1;
1627         }
1628
1629         channel = g_io_channel_unix_new(sk);
1630         g_io_channel_set_close_on_unref(channel, TRUE);
1631
1632         g_io_channel_set_encoding(channel, NULL, NULL);
1633         g_io_channel_set_buffered(channel, FALSE);
1634
1635         channel_watch = g_io_add_watch(channel,
1636                                 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1637                                 netlink_event, NULL);
1638
1639         return 0;
1640 }
1641
1642 void __connman_rtnl_start(void)
1643 {
1644         DBG("");
1645
1646         send_getlink();
1647         send_getaddr();
1648         send_getroute();
1649 }
1650
1651 void __connman_rtnl_cleanup(void)
1652 {
1653         GSList *list;
1654
1655         DBG("");
1656
1657         for (list = watch_list; list; list = list->next) {
1658                 struct watch_data *watch = list->data;
1659
1660                 DBG("removing watch %d", watch->id);
1661
1662                 g_free(watch);
1663                 list->data = NULL;
1664         }
1665
1666         g_slist_free(watch_list);
1667         watch_list = NULL;
1668
1669         g_slist_free(update_list);
1670         update_list = NULL;
1671
1672         for (list = request_list; list; list = list->next) {
1673                 struct rtnl_request *req = list->data;
1674
1675                 DBG("%s len %d type %d flags 0x%04x seq %d",
1676                                 type2string(req->hdr.nlmsg_type),
1677                                 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1678                                 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1679
1680                 g_free(req);
1681                 list->data = NULL;
1682         }
1683
1684         g_slist_free(request_list);
1685         request_list = NULL;
1686
1687         if (channel_watch) {
1688                 g_source_remove(channel_watch);
1689                 channel_watch = 0;
1690         }
1691
1692         g_io_channel_shutdown(channel, TRUE, NULL);
1693         g_io_channel_unref(channel);
1694
1695         channel = NULL;
1696
1697         g_hash_table_destroy(interface_list);
1698 }