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