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