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