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