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