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