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