wifi: Fix tethering with kernel 3.5
[profile/ivi/connman.git] / src / rtnl.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <sys/socket.h>
31 #include <sys/ioctl.h>
32 #include <arpa/inet.h>
33 #include <netinet/ether.h>
34 #include <netinet/icmp6.h>
35 #include <net/if_arp.h>
36 #include <linux/if.h>
37 #include <linux/netlink.h>
38 #include <linux/rtnetlink.h>
39 #include <linux/wireless.h>
40
41 #include <glib.h>
42
43 #include "connman.h"
44
45 #ifndef ARPHDR_PHONET_PIPE
46 #define ARPHDR_PHONET_PIPE (821)
47 #endif
48
49 #if defined TIZEN_EXT
50 #ifndef ARPHDR_RMNET_USB
51 #define ARPHDR_RMNET_USB (530)
52 #endif
53 #endif
54
55 #define print(arg...) do { if (0) connman_info(arg); } while (0)
56 //#define print(arg...) connman_info(arg)
57
58 struct watch_data {
59         unsigned int id;
60         int index;
61         connman_rtnl_link_cb_t newlink;
62         void *user_data;
63 };
64
65 static GSList *watch_list = NULL;
66 static unsigned int watch_id = 0;
67
68 static GSList *update_list = NULL;
69 static guint update_interval = G_MAXUINT;
70 static guint update_timeout = 0;
71
72 struct interface_data {
73         int index;
74         char *name;
75         char *ident;
76         enum connman_service_type service_type;
77         enum connman_device_type device_type;
78 };
79
80 static GHashTable *interface_list = NULL;
81
82 static void free_interface(gpointer data)
83 {
84         struct interface_data *interface = data;
85
86         __connman_technology_remove_interface(interface->service_type,
87                         interface->index, interface->name, interface->ident);
88
89         g_free(interface->ident);
90         g_free(interface->name);
91         g_free(interface);
92 }
93
94 static connman_bool_t ether_blacklisted(const char *name)
95 {
96         if (name == NULL)
97                 return TRUE;
98
99         if (__connman_device_isfiltered(name) == TRUE)
100                 return TRUE;
101
102         return FALSE;
103 }
104
105 static connman_bool_t wext_interface(char *ifname)
106 {
107         struct iwreq wrq;
108         int fd, err;
109
110         fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
111         if (fd < 0)
112                 return FALSE;
113
114         memset(&wrq, 0, sizeof(wrq));
115         strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
116
117         err = ioctl(fd, SIOCGIWNAME, &wrq);
118
119         close(fd);
120
121         if (err < 0)
122                 return FALSE;
123
124         return TRUE;
125 }
126
127 static void read_uevent(struct interface_data *interface)
128 {
129         char *filename, line[128];
130         connman_bool_t found_devtype;
131         FILE *f;
132
133         if (ether_blacklisted(interface->name) == TRUE) {
134                 interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
135                 interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
136         } else {
137                 interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
138                 interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
139         }
140
141         filename = g_strdup_printf("/sys/class/net/%s/uevent",
142                                                 interface->name);
143
144         f = fopen(filename, "re");
145
146         g_free(filename);
147
148         if (f == NULL)
149                 return;
150
151         found_devtype = FALSE;
152         while (fgets(line, sizeof(line), f)) {
153                 char *pos;
154
155                 pos = strchr(line, '\n');
156                 if (pos == NULL)
157                         continue;
158                 pos[0] = '\0';
159
160                 if (strncmp(line, "DEVTYPE=", 8) != 0)
161                         continue;
162
163                 found_devtype = TRUE;
164
165                 if (strcmp(line + 8, "wlan") == 0) {
166                         interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
167                         interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
168                 } else if (strcmp(line + 8, "wwan") == 0) {
169                         interface->service_type = CONNMAN_SERVICE_TYPE_CELLULAR;
170                         interface->device_type = CONNMAN_DEVICE_TYPE_CELLULAR;
171                 } else if (strcmp(line + 8, "bluetooth") == 0) {
172                         interface->service_type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
173                         interface->device_type = CONNMAN_DEVICE_TYPE_BLUETOOTH;
174                 } else if (strcmp(line + 8, "wimax") == 0) {
175                         interface->service_type = CONNMAN_SERVICE_TYPE_WIMAX;
176                         interface->device_type = CONNMAN_DEVICE_TYPE_WIMAX;
177                 } else if (strcmp(line + 8, "gadget") == 0) {
178                         interface->service_type = CONNMAN_SERVICE_TYPE_GADGET;
179                         interface->device_type = CONNMAN_DEVICE_TYPE_GADGET;
180
181                 } else {
182                         interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
183                         interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
184                 }
185         }
186
187         fclose(f);
188
189         if (found_devtype)
190                 return;
191
192         /* We haven't got a DEVTYPE, let's check if it's a wireless device */
193         if (wext_interface(interface->name)) {
194                 interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
195                 interface->device_type = CONNMAN_DEVICE_TYPE_WIFI;
196
197                 connman_error("%s runs an unsupported 802.11 driver",
198                                 interface->name);
199         }
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 == NULL)
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 == NULL)
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 != NULL)
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 void 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 != NULL)
377                                 memcpy(address, RTA_DATA(attr), ETH_ALEN);
378                         break;
379                 case IFLA_IFNAME:
380                         if (ifname != NULL)
381                                 *ifname = RTA_DATA(attr);
382                         break;
383                 case IFLA_MTU:
384                         if (mtu != NULL)
385                                 *mtu = *((unsigned int *) RTA_DATA(attr));
386                         break;
387                 case IFLA_STATS:
388                         if (stats != NULL)
389                                 memcpy(stats, RTA_DATA(attr),
390                                         sizeof(struct rtnl_link_stats));
391                         break;
392                 case IFLA_OPERSTATE:
393                         if (operstate != NULL)
394                                 *operstate = *((unsigned char *) RTA_DATA(attr));
395                         break;
396                 case IFLA_LINKMODE:
397                         break;
398                 }
399         }
400 }
401
402 static void process_newlink(unsigned short type, int index, unsigned flags,
403                         unsigned change, struct ifinfomsg *msg, int bytes)
404 {
405         struct ether_addr address = {{ 0, 0, 0, 0, 0, 0 }};
406         struct ether_addr compare = {{ 0, 0, 0, 0, 0, 0 }};
407         struct rtnl_link_stats stats;
408         unsigned char operstate = 0xff;
409         struct interface_data *interface;
410         const char *ifname = NULL;
411         unsigned int mtu = 0;
412         char ident[13], str[18];
413         GSList *list;
414
415         memset(&stats, 0, sizeof(stats));
416         extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats);
417
418         snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
419                                                 address.ether_addr_octet[0],
420                                                 address.ether_addr_octet[1],
421                                                 address.ether_addr_octet[2],
422                                                 address.ether_addr_octet[3],
423                                                 address.ether_addr_octet[4],
424                                                 address.ether_addr_octet[5]);
425
426         snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
427                                                 address.ether_addr_octet[0],
428                                                 address.ether_addr_octet[1],
429                                                 address.ether_addr_octet[2],
430                                                 address.ether_addr_octet[3],
431                                                 address.ether_addr_octet[4],
432                                                 address.ether_addr_octet[5]);
433
434         switch (type) {
435         case ARPHRD_ETHER:
436         case ARPHRD_LOOPBACK:
437         case ARPHDR_PHONET_PIPE:
438         case ARPHRD_NONE:
439 #if defined TIZEN_EXT
440         case ARPHRD_PPP:
441         case ARPHDR_RMNET_USB:
442 #endif
443                 __connman_ipconfig_newlink(index, type, flags,
444                                                         str, mtu, &stats);
445                 break;
446         }
447
448         if (memcmp(&address, &compare, ETH_ALEN) != 0)
449                 connman_info("%s {newlink} index %d address %s mtu %u",
450                                                 ifname, index, str, mtu);
451
452         if (operstate != 0xff)
453                 connman_info("%s {newlink} index %d operstate %u <%s>",
454                                                 ifname, index, operstate,
455                                                 operstate2str(operstate));
456
457         interface = g_hash_table_lookup(interface_list, GINT_TO_POINTER(index));
458         if (interface == NULL) {
459                 interface = g_new0(struct interface_data, 1);
460                 interface->index = index;
461                 interface->name = g_strdup(ifname);
462                 interface->ident = g_strdup(ident);
463
464                 g_hash_table_insert(interface_list,
465                                         GINT_TO_POINTER(index), interface);
466
467                 if (type == ARPHRD_ETHER)
468                         read_uevent(interface);
469
470 #if defined TIZEN_EXT
471                 if (type == ARPHRD_PPP || type == ARPHDR_RMNET_USB) {
472                         interface->service_type = CONNMAN_SERVICE_TYPE_CELLULAR;
473                         interface->device_type = CONNMAN_DEVICE_TYPE_CELLULAR;
474                 }
475 #endif
476
477                 __connman_technology_add_interface(interface->service_type,
478                         interface->index, interface->name, interface->ident);
479         }
480
481         for (list = rtnl_list; list; list = list->next) {
482                 struct connman_rtnl *rtnl = list->data;
483
484                 if (rtnl->newlink)
485                         rtnl->newlink(type, index, flags, change);
486         }
487
488         for (list = watch_list; list; list = list->next) {
489                 struct watch_data *watch = list->data;
490
491                 if (watch->index != index)
492                         continue;
493
494                 if (watch->newlink)
495                         watch->newlink(flags, change, watch->user_data);
496         }
497 }
498
499 static void process_dellink(unsigned short type, int index, unsigned flags,
500                         unsigned change, struct ifinfomsg *msg, int bytes)
501 {
502         struct rtnl_link_stats stats;
503         unsigned char operstate = 0xff;
504         const char *ifname = NULL;
505         GSList *list;
506
507         memset(&stats, 0, sizeof(stats));
508         extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats);
509
510         if (operstate != 0xff)
511                 connman_info("%s {dellink} index %d operstate %u <%s>",
512                                                 ifname, index, operstate,
513                                                 operstate2str(operstate));
514
515         for (list = rtnl_list; list; list = list->next) {
516                 struct connman_rtnl *rtnl = list->data;
517
518                 if (rtnl->dellink)
519                         rtnl->dellink(type, index, flags, change);
520         }
521
522         switch (type) {
523         case ARPHRD_ETHER:
524         case ARPHRD_LOOPBACK:
525         case ARPHRD_NONE:
526 #if defined TIZEN_EXT
527         case ARPHRD_PPP:
528         case ARPHDR_RMNET_USB:
529                 DBG("interface index(%d)", index);
530 #endif
531                 __connman_ipconfig_dellink(index, &stats);
532                 break;
533         }
534
535         g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
536 }
537
538 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
539                                                 const char **label,
540                                                 struct in_addr *local,
541                                                 struct in_addr *address,
542                                                 struct in_addr *broadcast)
543 {
544         struct rtattr *attr;
545
546         for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
547                                         attr = RTA_NEXT(attr, bytes)) {
548                 switch (attr->rta_type) {
549                 case IFA_ADDRESS:
550                         if (address != NULL)
551                                 *address = *((struct in_addr *) RTA_DATA(attr));
552                         break;
553                 case IFA_LOCAL:
554                         if (local != NULL)
555                                 *local = *((struct in_addr *) RTA_DATA(attr));
556                         break;
557                 case IFA_BROADCAST:
558                         if (broadcast != NULL)
559                                 *broadcast = *((struct in_addr *) RTA_DATA(attr));
560                         break;
561                 case IFA_LABEL:
562                         if (label != NULL)
563                                 *label = RTA_DATA(attr);
564                         break;
565                 }
566         }
567 }
568
569 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
570                                                 struct in6_addr *addr,
571                                                 struct in6_addr *local)
572 {
573         struct rtattr *attr;
574
575         for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
576                                         attr = RTA_NEXT(attr, bytes)) {
577                 switch (attr->rta_type) {
578                 case IFA_ADDRESS:
579                         if (addr != NULL)
580                                 *addr = *((struct in6_addr *) RTA_DATA(attr));
581                         break;
582                 case IFA_LOCAL:
583                         if (local != NULL)
584                                 *local = *((struct in6_addr *) RTA_DATA(attr));
585                         break;
586                 }
587         }
588 }
589
590 static void process_newaddr(unsigned char family, unsigned char prefixlen,
591                                 int index, struct ifaddrmsg *msg, int bytes)
592 {
593         const char *label = NULL;
594         void *src;
595         char ip_string[INET6_ADDRSTRLEN];
596
597         if (family == AF_INET) {
598                 struct in_addr ipv4_addr = { INADDR_ANY };
599
600                 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
601                 src = &ipv4_addr;
602         } else if (family == AF_INET6) {
603                 struct in6_addr ipv6_address, ipv6_local;
604
605                 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
606                 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
607                         return;
608
609                 src = &ipv6_address;
610         } else {
611                 return;
612         }
613
614         if (inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN) == NULL)
615                 return;
616
617         __connman_ipconfig_newaddr(index, family, label,
618                                         prefixlen, ip_string);
619
620         if (family == AF_INET6) {
621                 /*
622                  * Re-create RDNSS configured servers if there are any
623                  * for this interface. This is done because we might
624                  * have now properly configured interface with proper
625                  * autoconfigured address.
626                  */
627                 char *interface = connman_inet_ifname(index);
628
629                 __connman_resolver_redo_servers(interface);
630
631                 g_free(interface);
632         }
633 }
634
635 static void process_deladdr(unsigned char family, unsigned char prefixlen,
636                                 int index, struct ifaddrmsg *msg, int bytes)
637 {
638         const char *label = NULL;
639         void *src;
640         char ip_string[INET6_ADDRSTRLEN];
641
642         if (family == AF_INET) {
643                 struct in_addr ipv4_addr = { INADDR_ANY };
644
645                 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
646                 src = &ipv4_addr;
647         } else if (family == AF_INET6) {
648                 struct in6_addr ipv6_address, ipv6_local;
649
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) == NULL)
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 != NULL)
677                                 *dst = *((struct in_addr *) RTA_DATA(attr));
678                         break;
679                 case RTA_GATEWAY:
680                         if (gateway != NULL)
681                                 *gateway = *((struct in_addr *) RTA_DATA(attr));
682                         break;
683                 case RTA_OIF:
684                         if (index != NULL)
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 != NULL)
702                                 *dst = *((struct in6_addr *) RTA_DATA(attr));
703                         break;
704                 case RTA_GATEWAY:
705                         if (gateway != NULL)
706                                 *gateway =
707                                         *((struct in6_addr *) RTA_DATA(attr));
708                         break;
709                 case RTA_OIF:
710                         if (index != NULL)
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         process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
963                                 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
964 }
965
966 static void rtnl_dellink(struct nlmsghdr *hdr)
967 {
968         struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
969
970         rtnl_link(hdr);
971
972         process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
973                                 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
974 }
975
976 static void rtnl_addr(struct nlmsghdr *hdr)
977 {
978         struct ifaddrmsg *msg;
979         struct rtattr *attr;
980         int bytes;
981
982         msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
983         bytes = IFA_PAYLOAD(hdr);
984
985         print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
986
987         for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
988                                         attr = RTA_NEXT(attr, bytes)) {
989                 switch (attr->rta_type) {
990                 case IFA_ADDRESS:
991                         print_inet(attr, "address", msg->ifa_family);
992                         break;
993                 case IFA_LOCAL:
994                         print_inet(attr, "local", msg->ifa_family);
995                         break;
996                 case IFA_LABEL:
997                         print_string(attr, "label");
998                         break;
999                 case IFA_BROADCAST:
1000                         print_inet(attr, "broadcast", msg->ifa_family);
1001                         break;
1002                 case IFA_ANYCAST:
1003                         print_attr(attr, "anycast");
1004                         break;
1005                 case IFA_CACHEINFO:
1006                         print_attr(attr, "cacheinfo");
1007                         break;
1008                 case IFA_MULTICAST:
1009                         print_attr(attr, "multicast");
1010                         break;
1011                 default:
1012                         print_attr(attr, NULL);
1013                         break;
1014                 }
1015         }
1016 }
1017
1018 static void rtnl_newaddr(struct nlmsghdr *hdr)
1019 {
1020         struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1021
1022         rtnl_addr(hdr);
1023
1024         process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1025                                                 msg, IFA_PAYLOAD(hdr));
1026 }
1027
1028 static void rtnl_deladdr(struct nlmsghdr *hdr)
1029 {
1030         struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1031
1032         rtnl_addr(hdr);
1033
1034         process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1035                                                 msg, IFA_PAYLOAD(hdr));
1036 }
1037
1038 static void rtnl_route(struct nlmsghdr *hdr)
1039 {
1040         struct rtmsg *msg;
1041         struct rtattr *attr;
1042         int bytes;
1043
1044         msg = (struct rtmsg *) NLMSG_DATA(hdr);
1045         bytes = RTM_PAYLOAD(hdr);
1046
1047         print("rtm_family %d rtm_table %d rtm_protocol %d",
1048                         msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
1049         print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
1050                                 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
1051
1052         for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
1053                                         attr = RTA_NEXT(attr, bytes)) {
1054                 switch (attr->rta_type) {
1055                 case RTA_DST:
1056                         print_inet(attr, "dst", msg->rtm_family);
1057                         break;
1058                 case RTA_SRC:
1059                         print_inet(attr, "src", msg->rtm_family);
1060                         break;
1061                 case RTA_IIF:
1062                         print_string(attr, "iif");
1063                         break;
1064                 case RTA_OIF:
1065                         print_integer(attr, "oif");
1066                         break;
1067                 case RTA_GATEWAY:
1068                         print_inet(attr, "gateway", msg->rtm_family);
1069                         break;
1070                 case RTA_PRIORITY:
1071                         print_attr(attr, "priority");
1072                         break;
1073                 case RTA_PREFSRC:
1074                         print_inet(attr, "prefsrc", msg->rtm_family);
1075                         break;
1076                 case RTA_METRICS:
1077                         print_attr(attr, "metrics");
1078                         break;
1079                 case RTA_TABLE:
1080                         print_integer(attr, "table");
1081                         break;
1082                 default:
1083                         print_attr(attr, NULL);
1084                         break;
1085                 }
1086         }
1087 }
1088
1089 static connman_bool_t is_route_rtmsg(struct rtmsg *msg)
1090 {
1091
1092         if (msg->rtm_table != RT_TABLE_MAIN)
1093                 return FALSE;
1094
1095         if (msg->rtm_protocol != RTPROT_BOOT &&
1096                         msg->rtm_protocol != RTPROT_KERNEL)
1097                 return FALSE;
1098
1099         if (msg->rtm_type != RTN_UNICAST)
1100                 return FALSE;
1101
1102         return TRUE;
1103 }
1104
1105 static void rtnl_newroute(struct nlmsghdr *hdr)
1106 {
1107         struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1108
1109         rtnl_route(hdr);
1110
1111         if (is_route_rtmsg(msg))
1112                 process_newroute(msg->rtm_family, msg->rtm_scope,
1113                                                 msg, RTM_PAYLOAD(hdr));
1114 }
1115
1116 static void rtnl_delroute(struct nlmsghdr *hdr)
1117 {
1118         struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1119
1120         rtnl_route(hdr);
1121
1122         if (is_route_rtmsg(msg))
1123                 process_delroute(msg->rtm_family, msg->rtm_scope,
1124                                                 msg, RTM_PAYLOAD(hdr));
1125 }
1126
1127 static void *rtnl_nd_opt_rdnss(struct nd_opt_hdr *opt, guint32 *lifetime,
1128                                int *nr_servers)
1129 {
1130         guint32 *optint = (void *)opt;
1131
1132         if (opt->nd_opt_len < 3)
1133                 return NULL;
1134
1135         if (*lifetime > ntohl(optint[1]))
1136                 *lifetime = ntohl(optint[1]);
1137
1138         /* nd_opt_len is in units of 8 bytes. The header is 1 unit (8 bytes)
1139            and each address is another 2 units (16 bytes).
1140            So the number of addresses (given rounding) is nd_opt_len/2 */
1141         *nr_servers = opt->nd_opt_len / 2;
1142
1143         /* And they start 8 bytes into the packet, or two guint32s in. */
1144         return optint + 2;
1145 }
1146
1147 static const char **rtnl_nd_opt_dnssl(struct nd_opt_hdr *opt, guint32 *lifetime)
1148 {
1149         const char **domains = NULL;
1150         guint32 *optint = (void *)opt;
1151         unsigned char *optc = (void *)&optint[2];
1152         int data_len = (opt->nd_opt_len * 8) - 8;
1153         int nr_domains = 0;
1154         int i, tmp;
1155
1156         if (*lifetime > ntohl(optint[1]))
1157                 *lifetime = ntohl(optint[1]);
1158
1159         /* Turn it into normal strings by converting the length bytes into '.',
1160            and count how many search domains there are while we're at it. */
1161         i = 0;
1162         while (i < data_len) {
1163                 if (optc[i] > 0x3f) {
1164                         DBG("DNSSL contains compressed elements in violation of RFC6106");
1165                         return NULL;
1166                 }
1167
1168                 if (optc[i] == 0) {
1169                         nr_domains++;
1170                         i++;
1171                         /* Check for double zero */
1172                         if (i < data_len && optc[i] == 0)
1173                                 break;
1174                         continue;
1175                 }
1176
1177                 tmp = i;
1178                 i += optc[i] + 1;
1179
1180                 if (i >= data_len) {
1181                         DBG("DNSSL data overflows option length");
1182                         return NULL;
1183                 }
1184
1185                 optc[tmp] = '.';
1186         }
1187
1188         domains = g_try_new0(const char *, nr_domains + 1);
1189         if (!domains)
1190                 return NULL;
1191
1192         /* Now point to the normal strings, missing out the leading '.' that
1193            each of them will have now. */
1194         for (i = 0; i < nr_domains; i++) {
1195                 domains[i] = (char *)optc + 1;
1196                 optc += strlen((char *)optc) + 1;
1197         }
1198
1199         return domains;
1200 }
1201
1202 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1203 {
1204         struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1205         struct nd_opt_hdr *opt;
1206         guint32 lifetime = -1;
1207         const char **domains = NULL;
1208         struct in6_addr *servers = NULL;
1209         int i, nr_servers = 0;
1210         int msglen = msg->nduseropt_opts_len;
1211         char *interface;
1212
1213         DBG("family %d index %d len %d type %d code %d",
1214                 msg->nduseropt_family, msg->nduseropt_ifindex,
1215                 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1216                 msg->nduseropt_icmp_code);
1217
1218         if (msg->nduseropt_family != AF_INET6 ||
1219                         msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1220                         msg->nduseropt_icmp_code != 0)
1221                 return;
1222
1223         interface = connman_inet_ifname(msg->nduseropt_ifindex);
1224         if (!interface)
1225                 return;
1226
1227         for (opt = (void *)&msg[1];
1228                         msglen > 0;
1229                         msglen -= opt->nd_opt_len * 8,
1230                         opt = ((void *)opt) + opt->nd_opt_len*8) {
1231
1232                 DBG("remaining %d nd opt type %d len %d\n",
1233                         msglen, opt->nd_opt_type, opt->nd_opt_len);
1234
1235                 if (opt->nd_opt_type == 25) { /* ND_OPT_RDNSS */
1236                         char buf[40];
1237
1238                         servers = rtnl_nd_opt_rdnss(opt, &lifetime,
1239                                                                 &nr_servers);
1240                         for (i = 0; i < nr_servers; i++) {
1241                                 if (!inet_ntop(AF_INET6, servers + i, buf,
1242                                                                 sizeof(buf)))
1243                                         continue;
1244
1245                                 connman_resolver_append_lifetime(interface,
1246                                                         NULL, buf, lifetime);
1247                         }
1248
1249                 } else if (opt->nd_opt_type == 31) { /* ND_OPT_DNSSL */
1250                         g_free(domains);
1251
1252                         domains = rtnl_nd_opt_dnssl(opt, &lifetime);
1253                         for (i = 0; domains != NULL && domains[i] != NULL; i++)
1254                                 connman_resolver_append_lifetime(interface,
1255                                                 domains[i], NULL, lifetime);
1256                 }
1257         }
1258
1259         g_free(domains);
1260         g_free(interface);
1261 }
1262
1263 static const char *type2string(uint16_t type)
1264 {
1265         switch (type) {
1266         case NLMSG_NOOP:
1267                 return "NOOP";
1268         case NLMSG_ERROR:
1269                 return "ERROR";
1270         case NLMSG_DONE:
1271                 return "DONE";
1272         case NLMSG_OVERRUN:
1273                 return "OVERRUN";
1274         case RTM_GETLINK:
1275                 return "GETLINK";
1276         case RTM_NEWLINK:
1277                 return "NEWLINK";
1278         case RTM_DELLINK:
1279                 return "DELLINK";
1280         case RTM_GETADDR:
1281                 return "GETADDR";
1282         case RTM_NEWADDR:
1283                 return "NEWADDR";
1284         case RTM_DELADDR:
1285                 return "DELADDR";
1286         case RTM_GETROUTE:
1287                 return "GETROUTE";
1288         case RTM_NEWROUTE:
1289                 return "NEWROUTE";
1290         case RTM_DELROUTE:
1291                 return "DELROUTE";
1292         case RTM_NEWNDUSEROPT:
1293                 return "NEWNDUSEROPT";
1294         default:
1295                 return "UNKNOWN";
1296         }
1297 }
1298
1299 static GIOChannel *channel = NULL;
1300
1301 struct rtnl_request {
1302         struct nlmsghdr hdr;
1303         struct rtgenmsg msg;
1304 };
1305 #define RTNL_REQUEST_SIZE  (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1306
1307 static GSList *request_list = NULL;
1308 static guint32 request_seq = 0;
1309
1310 static struct rtnl_request *find_request(guint32 seq)
1311 {
1312         GSList *list;
1313
1314         for (list = request_list; list; list = list->next) {
1315                 struct rtnl_request *req = list->data;
1316
1317                 if (req->hdr.nlmsg_seq == seq)
1318                         return req;
1319         }
1320
1321         return NULL;
1322 }
1323
1324 static int send_request(struct rtnl_request *req)
1325 {
1326         struct sockaddr_nl addr;
1327         int sk;
1328
1329         DBG("%s len %d type %d flags 0x%04x seq %d",
1330                                 type2string(req->hdr.nlmsg_type),
1331                                 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1332                                 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1333
1334         sk = g_io_channel_unix_get_fd(channel);
1335
1336         memset(&addr, 0, sizeof(addr));
1337         addr.nl_family = AF_NETLINK;
1338
1339         return sendto(sk, req, req->hdr.nlmsg_len, 0,
1340                                 (struct sockaddr *) &addr, sizeof(addr));
1341 }
1342
1343 static int queue_request(struct rtnl_request *req)
1344 {
1345         request_list = g_slist_append(request_list, req);
1346
1347         if (g_slist_length(request_list) > 1)
1348                 return 0;
1349
1350         return send_request(req);
1351 }
1352
1353 static int process_response(guint32 seq)
1354 {
1355         struct rtnl_request *req;
1356
1357         DBG("seq %d", seq);
1358
1359         req = find_request(seq);
1360         if (req != NULL) {
1361                 request_list = g_slist_remove(request_list, req);
1362                 g_free(req);
1363         }
1364
1365         req = g_slist_nth_data(request_list, 0);
1366         if (req == NULL)
1367                 return 0;
1368
1369         return send_request(req);
1370 }
1371
1372 static void rtnl_message(void *buf, size_t len)
1373 {
1374         DBG("buf %p len %zd", buf, len);
1375
1376         while (len > 0) {
1377                 struct nlmsghdr *hdr = buf;
1378                 struct nlmsgerr *err;
1379
1380                 if (!NLMSG_OK(hdr, len))
1381                         break;
1382
1383                 DBG("%s len %d type %d flags 0x%04x seq %d pid %d",
1384                                         type2string(hdr->nlmsg_type),
1385                                         hdr->nlmsg_len, hdr->nlmsg_type,
1386                                         hdr->nlmsg_flags, hdr->nlmsg_seq,
1387                                         hdr->nlmsg_pid);
1388
1389                 switch (hdr->nlmsg_type) {
1390                 case NLMSG_NOOP:
1391                 case NLMSG_OVERRUN:
1392                         return;
1393                 case NLMSG_DONE:
1394                         process_response(hdr->nlmsg_seq);
1395                         return;
1396                 case NLMSG_ERROR:
1397                         err = NLMSG_DATA(hdr);
1398                         DBG("error %d (%s)", -err->error,
1399                                                 strerror(-err->error));
1400                         return;
1401                 case RTM_NEWLINK:
1402                         rtnl_newlink(hdr);
1403                         break;
1404                 case RTM_DELLINK:
1405                         rtnl_dellink(hdr);
1406                         break;
1407                 case RTM_NEWADDR:
1408                         rtnl_newaddr(hdr);
1409                         break;
1410                 case RTM_DELADDR:
1411                         rtnl_deladdr(hdr);
1412                         break;
1413                 case RTM_NEWROUTE:
1414                         rtnl_newroute(hdr);
1415                         break;
1416                 case RTM_DELROUTE:
1417                         rtnl_delroute(hdr);
1418                         break;
1419                 case RTM_NEWNDUSEROPT:
1420                         rtnl_newnduseropt(hdr);
1421                         break;
1422                 }
1423
1424                 len -= hdr->nlmsg_len;
1425                 buf += hdr->nlmsg_len;
1426         }
1427 }
1428
1429 static gboolean netlink_event(GIOChannel *chan,
1430                                 GIOCondition cond, gpointer data)
1431 {
1432         unsigned char buf[4096];
1433         struct sockaddr_nl nladdr;
1434         socklen_t addr_len = sizeof(nladdr);
1435         ssize_t status;
1436         int fd;
1437
1438         if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1439                 return FALSE;
1440
1441         memset(buf, 0, sizeof(buf));
1442         memset(&nladdr, 0, sizeof(nladdr));
1443
1444         fd = g_io_channel_unix_get_fd(chan);
1445
1446         status = recvfrom(fd, buf, sizeof(buf), 0,
1447                        (struct sockaddr *) &nladdr, &addr_len);
1448         if (status < 0) {
1449                 if (errno == EINTR || errno == EAGAIN)
1450                         return TRUE;
1451
1452                 return FALSE;
1453         }
1454
1455         if (status == 0)
1456                 return FALSE;
1457
1458         if (nladdr.nl_pid != 0) { /* not sent by kernel, ignore */
1459                 DBG("Received msg from %u, ignoring it", nladdr.nl_pid);
1460                 return TRUE;
1461         }
1462
1463         rtnl_message(buf, status);
1464
1465         return TRUE;
1466 }
1467
1468 static int send_getlink(void)
1469 {
1470         struct rtnl_request *req;
1471
1472         DBG("");
1473
1474         req = g_try_malloc0(RTNL_REQUEST_SIZE);
1475         if (req == NULL)
1476                 return -ENOMEM;
1477
1478         req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1479         req->hdr.nlmsg_type = RTM_GETLINK;
1480         req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1481         req->hdr.nlmsg_pid = 0;
1482         req->hdr.nlmsg_seq = request_seq++;
1483         req->msg.rtgen_family = AF_INET;
1484
1485         return queue_request(req);
1486 }
1487
1488 static int send_getaddr(void)
1489 {
1490         struct rtnl_request *req;
1491
1492         DBG("");
1493
1494         req = g_try_malloc0(RTNL_REQUEST_SIZE);
1495         if (req == NULL)
1496                 return -ENOMEM;
1497
1498         req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1499         req->hdr.nlmsg_type = RTM_GETADDR;
1500         req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1501         req->hdr.nlmsg_pid = 0;
1502         req->hdr.nlmsg_seq = request_seq++;
1503         req->msg.rtgen_family = AF_INET;
1504
1505         return queue_request(req);
1506 }
1507
1508 static int send_getroute(void)
1509 {
1510         struct rtnl_request *req;
1511
1512         DBG("");
1513
1514         req = g_try_malloc0(RTNL_REQUEST_SIZE);
1515         if (req == NULL)
1516                 return -ENOMEM;
1517
1518         req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1519         req->hdr.nlmsg_type = RTM_GETROUTE;
1520         req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1521         req->hdr.nlmsg_pid = 0;
1522         req->hdr.nlmsg_seq = request_seq++;
1523         req->msg.rtgen_family = AF_INET;
1524
1525         return queue_request(req);
1526 }
1527
1528 static gboolean update_timeout_cb(gpointer user_data)
1529 {
1530         __connman_rtnl_request_update();
1531
1532         return TRUE;
1533 }
1534
1535 static void update_interval_callback(guint min)
1536 {
1537         if (update_timeout > 0)
1538                 g_source_remove(update_timeout);
1539
1540         if (min < G_MAXUINT) {
1541                 update_interval = min;
1542                 update_timeout = g_timeout_add_seconds(update_interval,
1543                                                 update_timeout_cb, NULL);
1544         } else {
1545                 update_timeout = 0;
1546                 update_interval = G_MAXUINT;
1547         }
1548 }
1549
1550 static gint compare_interval(gconstpointer a, gconstpointer b)
1551 {
1552         guint val_a = GPOINTER_TO_UINT(a);
1553         guint val_b = GPOINTER_TO_UINT(b);
1554
1555         return val_a - val_b;
1556 }
1557
1558 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1559 {
1560         guint min;
1561
1562         if (interval == 0)
1563                 return 0;
1564
1565         update_list = g_slist_insert_sorted(update_list,
1566                         GUINT_TO_POINTER(interval), compare_interval);
1567
1568         min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1569         if (min < update_interval) {
1570                 update_interval_callback(min);
1571                 __connman_rtnl_request_update();
1572         }
1573
1574         return update_interval;
1575 }
1576
1577 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1578 {
1579         guint min = G_MAXUINT;
1580
1581         if (interval == 0)
1582                 return 0;
1583
1584         update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1585
1586         if (update_list != NULL)
1587                 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1588
1589         if (min > update_interval)
1590                 update_interval_callback(min);
1591
1592         return min;
1593 }
1594
1595 int __connman_rtnl_request_update(void)
1596 {
1597         return send_getlink();
1598 }
1599
1600 int __connman_rtnl_init(void)
1601 {
1602         struct sockaddr_nl addr;
1603         int sk;
1604
1605         DBG("");
1606
1607         interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1608                                                         NULL, free_interface);
1609
1610         sk = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
1611         if (sk < 0)
1612                 return -1;
1613
1614         memset(&addr, 0, sizeof(addr));
1615         addr.nl_family = AF_NETLINK;
1616         addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1617                                 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1618                                 (1<<(RTNLGRP_ND_USEROPT-1));
1619
1620         if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1621                 close(sk);
1622                 return -1;
1623         }
1624
1625         channel = g_io_channel_unix_new(sk);
1626         g_io_channel_set_close_on_unref(channel, TRUE);
1627
1628         g_io_channel_set_encoding(channel, NULL, NULL);
1629         g_io_channel_set_buffered(channel, FALSE);
1630
1631         g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1632                                                         netlink_event, NULL);
1633
1634         return 0;
1635 }
1636
1637 void __connman_rtnl_start(void)
1638 {
1639         DBG("");
1640
1641         send_getlink();
1642         send_getaddr();
1643         send_getroute();
1644 }
1645
1646 void __connman_rtnl_cleanup(void)
1647 {
1648         GSList *list;
1649
1650         DBG("");
1651
1652         for (list = watch_list; list; list = list->next) {
1653                 struct watch_data *watch = list->data;
1654
1655                 DBG("removing watch %d", watch->id);
1656
1657                 g_free(watch);
1658                 list->data = NULL;
1659         }
1660
1661         g_slist_free(watch_list);
1662         watch_list = NULL;
1663
1664         g_slist_free(update_list);
1665         update_list = NULL;
1666
1667         for (list = request_list; list; list = list->next) {
1668                 struct rtnl_request *req = list->data;
1669
1670                 DBG("%s len %d type %d flags 0x%04x seq %d",
1671                                 type2string(req->hdr.nlmsg_type),
1672                                 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1673                                 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1674
1675                 g_free(req);
1676                 list->data = NULL;
1677         }
1678
1679         g_slist_free(request_list);
1680         request_list = NULL;
1681
1682         g_io_channel_shutdown(channel, TRUE, NULL);
1683         g_io_channel_unref(channel);
1684
1685         channel = NULL;
1686
1687         g_hash_table_destroy(interface_list);
1688 }