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