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