Fix Tizen alarm dbus interface parameter
[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         for (list = watch_list; list; list = list->next) {
595                 struct watch_data *watch = list->data;
596
597                 if (watch->index != index)
598                         continue;
599
600                 if (watch->newlink)
601                         watch->newlink(flags, change, watch->user_data);
602         }
603 }
604
605 static void process_dellink(unsigned short type, int index, unsigned flags,
606                         unsigned change, struct ifinfomsg *msg, int bytes)
607 {
608         struct rtnl_link_stats stats;
609         unsigned char operstate = 0xff;
610         const char *ifname = NULL;
611         GSList *list;
612
613         memset(&stats, 0, sizeof(stats));
614         if (!extract_link(msg, bytes, NULL, &ifname, NULL, &operstate, &stats))
615                 return;
616
617         if (operstate != 0xff)
618                 connman_info("%s {dellink} index %d operstate %u <%s>",
619                                                 ifname, index, operstate,
620                                                 operstate2str(operstate));
621
622         for (list = rtnl_list; list; list = list->next) {
623                 struct connman_rtnl *rtnl = list->data;
624
625                 if (rtnl->dellink)
626                         rtnl->dellink(type, index, flags, change);
627         }
628
629         switch (type) {
630         case ARPHRD_ETHER:
631         case ARPHRD_LOOPBACK:
632         case ARPHDR_PHONET_PIPE:
633         case ARPHRD_PPP:
634         case ARPHRD_NONE:
635 #if defined TIZEN_EXT
636         /*
637          * Description: SLP requires ARPHRD_PPP for PPP type device
638          *              ARPHDR_RMNET for QC modem using QMI
639          */
640         case ARPHDR_RMNET:
641 #endif
642                 __connman_ipconfig_dellink(index, &stats);
643                 break;
644         }
645
646         g_hash_table_remove(interface_list, GINT_TO_POINTER(index));
647 }
648
649 static void extract_ipv4_addr(struct ifaddrmsg *msg, int bytes,
650                                                 const char **label,
651                                                 struct in_addr *local,
652                                                 struct in_addr *address,
653                                                 struct in_addr *broadcast)
654 {
655         struct rtattr *attr;
656
657         for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
658                                         attr = RTA_NEXT(attr, bytes)) {
659                 switch (attr->rta_type) {
660                 case IFA_ADDRESS:
661                         if (address)
662                                 *address = *((struct in_addr *) RTA_DATA(attr));
663                         break;
664                 case IFA_LOCAL:
665                         if (local)
666                                 *local = *((struct in_addr *) RTA_DATA(attr));
667                         break;
668                 case IFA_BROADCAST:
669                         if (broadcast)
670                                 *broadcast = *((struct in_addr *) RTA_DATA(attr));
671                         break;
672                 case IFA_LABEL:
673                         if (label)
674                                 *label = RTA_DATA(attr);
675                         break;
676                 }
677         }
678 }
679
680 static void extract_ipv6_addr(struct ifaddrmsg *msg, int bytes,
681                                                 struct in6_addr *addr,
682                                                 struct in6_addr *local)
683 {
684         struct rtattr *attr;
685
686         for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
687                                         attr = RTA_NEXT(attr, bytes)) {
688                 switch (attr->rta_type) {
689                 case IFA_ADDRESS:
690                         if (addr)
691                                 *addr = *((struct in6_addr *) RTA_DATA(attr));
692                         break;
693                 case IFA_LOCAL:
694                         if (local)
695                                 *local = *((struct in6_addr *) RTA_DATA(attr));
696                         break;
697                 }
698         }
699 }
700
701 static void process_newaddr(unsigned char family, unsigned char prefixlen,
702                                 int index, struct ifaddrmsg *msg, int bytes)
703 {
704         struct in_addr ipv4_addr = { INADDR_ANY };
705         struct in6_addr ipv6_address, ipv6_local;
706         const char *label = NULL;
707         void *src;
708         char ip_string[INET6_ADDRSTRLEN];
709
710         if (family == AF_INET) {
711
712                 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
713                 src = &ipv4_addr;
714         } else if (family == AF_INET6) {
715                 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
716                 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
717                         return;
718
719                 src = &ipv6_address;
720         } else {
721                 return;
722         }
723
724         if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
725                 return;
726
727         if (__connman_ipconfig_newaddr(index, family, label,
728                                         prefixlen, ip_string) >= 0) {
729                 if (family == AF_INET6) {
730                         /*
731                          * Re-create RDNSS configured servers if there
732                          * are any for this interface. This is done
733                          * because we might have now properly
734                          * configured interface with proper
735                          * autoconfigured address.
736                          */
737                         __connman_resolver_redo_servers(index);
738                 }
739         }
740 }
741
742 static void process_deladdr(unsigned char family, unsigned char prefixlen,
743                                 int index, struct ifaddrmsg *msg, int bytes)
744 {
745         struct in_addr ipv4_addr = { INADDR_ANY };
746         struct in6_addr ipv6_address, ipv6_local;
747         const char *label = NULL;
748         void *src;
749         char ip_string[INET6_ADDRSTRLEN];
750
751         if (family == AF_INET) {
752                 extract_ipv4_addr(msg, bytes, &label, &ipv4_addr, NULL, NULL);
753                 src = &ipv4_addr;
754         } else if (family == AF_INET6) {
755                 extract_ipv6_addr(msg, bytes, &ipv6_address, &ipv6_local);
756                 if (IN6_IS_ADDR_LINKLOCAL(&ipv6_address))
757                         return;
758
759                 src = &ipv6_address;
760         } else {
761                 return;
762         }
763
764         if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
765                 return;
766
767         __connman_ipconfig_deladdr(index, family, label,
768                                         prefixlen, ip_string);
769 }
770
771 static void extract_ipv4_route(struct rtmsg *msg, int bytes, int *index,
772                                                 struct in_addr *dst,
773                                                 struct in_addr *gateway)
774 {
775         struct rtattr *attr;
776
777         for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
778                                         attr = RTA_NEXT(attr, bytes)) {
779                 switch (attr->rta_type) {
780                 case RTA_DST:
781                         if (dst)
782                                 *dst = *((struct in_addr *) RTA_DATA(attr));
783                         break;
784                 case RTA_GATEWAY:
785                         if (gateway)
786                                 *gateway = *((struct in_addr *) RTA_DATA(attr));
787                         break;
788                 case RTA_OIF:
789                         if (index)
790                                 *index = *((int *) RTA_DATA(attr));
791                         break;
792                 }
793         }
794 }
795
796 static void extract_ipv6_route(struct rtmsg *msg, int bytes, int *index,
797                                                 struct in6_addr *dst,
798                                                 struct in6_addr *gateway)
799 {
800         struct rtattr *attr;
801
802         for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
803                                         attr = RTA_NEXT(attr, bytes)) {
804                 switch (attr->rta_type) {
805                 case RTA_DST:
806                         if (dst)
807                                 *dst = *((struct in6_addr *) RTA_DATA(attr));
808                         break;
809                 case RTA_GATEWAY:
810                         if (gateway)
811                                 *gateway =
812                                         *((struct in6_addr *) RTA_DATA(attr));
813                         break;
814                 case RTA_OIF:
815                         if (index)
816                                 *index = *((int *) RTA_DATA(attr));
817                         break;
818                 }
819         }
820 }
821
822 static void process_newroute(unsigned char family, unsigned char scope,
823                                                 struct rtmsg *msg, int bytes)
824 {
825         GSList *list;
826         char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
827         int index = -1;
828
829         if (family == AF_INET) {
830                 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
831
832                 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
833
834                 inet_ntop(family, &dst, dststr, sizeof(dststr));
835                 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
836
837                 __connman_ipconfig_newroute(index, family, scope, dststr,
838                                                                 gatewaystr);
839
840                 /* skip host specific routes */
841                 if (scope != RT_SCOPE_UNIVERSE &&
842                         !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
843                         return;
844
845                 if (dst.s_addr != INADDR_ANY)
846                         return;
847
848         } else if (family == AF_INET6) {
849                 struct in6_addr dst = IN6ADDR_ANY_INIT,
850                                 gateway = IN6ADDR_ANY_INIT;
851
852                 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
853
854                 inet_ntop(family, &dst, dststr, sizeof(dststr));
855                 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
856
857                 __connman_ipconfig_newroute(index, family, scope, dststr,
858                                                                 gatewaystr);
859
860                 /* skip host specific routes */
861                 if (scope != RT_SCOPE_UNIVERSE &&
862                         !(scope == RT_SCOPE_LINK &&
863                                 IN6_IS_ADDR_UNSPECIFIED(&dst)))
864                         return;
865
866                 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
867                         return;
868         } else
869                 return;
870
871         for (list = rtnl_list; list; list = list->next) {
872                 struct connman_rtnl *rtnl = list->data;
873
874                 if (rtnl->newgateway)
875                         rtnl->newgateway(index, gatewaystr);
876         }
877 }
878
879 static void process_delroute(unsigned char family, unsigned char scope,
880                                                 struct rtmsg *msg, int bytes)
881 {
882         GSList *list;
883         char dststr[INET6_ADDRSTRLEN], gatewaystr[INET6_ADDRSTRLEN];
884         int index = -1;
885
886         if (family == AF_INET) {
887                 struct in_addr dst = { INADDR_ANY }, gateway = { INADDR_ANY };
888
889                 extract_ipv4_route(msg, bytes, &index, &dst, &gateway);
890
891                 inet_ntop(family, &dst, dststr, sizeof(dststr));
892                 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
893
894                 __connman_ipconfig_delroute(index, family, scope, dststr,
895                                                                 gatewaystr);
896
897                 /* skip host specific routes */
898                 if (scope != RT_SCOPE_UNIVERSE &&
899                         !(scope == RT_SCOPE_LINK && dst.s_addr == INADDR_ANY))
900                         return;
901
902                 if (dst.s_addr != INADDR_ANY)
903                         return;
904
905         }  else if (family == AF_INET6) {
906                 struct in6_addr dst = IN6ADDR_ANY_INIT,
907                                 gateway = IN6ADDR_ANY_INIT;
908
909                 extract_ipv6_route(msg, bytes, &index, &dst, &gateway);
910
911                 inet_ntop(family, &dst, dststr, sizeof(dststr));
912                 inet_ntop(family, &gateway, gatewaystr, sizeof(gatewaystr));
913
914                 __connman_ipconfig_delroute(index, family, scope, dststr,
915                                                 gatewaystr);
916
917                 /* skip host specific routes */
918                 if (scope != RT_SCOPE_UNIVERSE &&
919                         !(scope == RT_SCOPE_LINK &&
920                                 IN6_IS_ADDR_UNSPECIFIED(&dst)))
921                         return;
922
923                 if (!IN6_IS_ADDR_UNSPECIFIED(&dst))
924                         return;
925         } else
926                 return;
927
928         for (list = rtnl_list; list; list = list->next) {
929                 struct connman_rtnl *rtnl = list->data;
930
931                 if (rtnl->delgateway)
932                         rtnl->delgateway(index, gatewaystr);
933         }
934 }
935
936 static inline void print_ether(struct rtattr *attr, const char *name)
937 {
938         int len = (int) RTA_PAYLOAD(attr);
939
940         if (len == ETH_ALEN) {
941                 struct ether_addr eth;
942                 memcpy(&eth, RTA_DATA(attr), ETH_ALEN);
943                 print("  attr %s (len %d) %s\n", name, len, ether_ntoa(&eth));
944         } else
945                 print("  attr %s (len %d)\n", name, len);
946 }
947
948 static inline void print_inet(struct rtattr *attr, const char *name,
949                                                         unsigned char family)
950 {
951         int len = (int) RTA_PAYLOAD(attr);
952
953         if (family == AF_INET && len == sizeof(struct in_addr)) {
954                 struct in_addr addr;
955                 addr = *((struct in_addr *) RTA_DATA(attr));
956                 print("  attr %s (len %d) %s\n", name, len, inet_ntoa(addr));
957         } else
958                 print("  attr %s (len %d)\n", name, len);
959 }
960
961 static inline void print_string(struct rtattr *attr, const char *name)
962 {
963         print("  attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
964                                                 (char *) RTA_DATA(attr));
965 }
966
967 static inline void print_byte(struct rtattr *attr, const char *name)
968 {
969         print("  attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
970                                         *((unsigned char *) RTA_DATA(attr)));
971 }
972
973 static inline void print_integer(struct rtattr *attr, const char *name)
974 {
975         print("  attr %s (len %d) %d\n", name, (int) RTA_PAYLOAD(attr),
976                                                 *((int *) RTA_DATA(attr)));
977 }
978
979 static inline void print_attr(struct rtattr *attr, const char *name)
980 {
981         int len = (int) RTA_PAYLOAD(attr);
982
983         if (name && len > 0)
984                 print("  attr %s (len %d)\n", name, len);
985         else
986                 print("  attr %d (len %d)\n", attr->rta_type, len);
987 }
988
989 static void rtnl_link(struct nlmsghdr *hdr)
990 {
991         struct ifinfomsg *msg;
992         struct rtattr *attr;
993         int bytes;
994
995         msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
996         bytes = IFLA_PAYLOAD(hdr);
997
998         print("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
999
1000         for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
1001                                         attr = RTA_NEXT(attr, bytes)) {
1002                 switch (attr->rta_type) {
1003                 case IFLA_ADDRESS:
1004                         print_ether(attr, "address");
1005                         break;
1006                 case IFLA_BROADCAST:
1007                         print_ether(attr, "broadcast");
1008                         break;
1009                 case IFLA_IFNAME:
1010                         print_string(attr, "ifname");
1011                         break;
1012                 case IFLA_MTU:
1013                         print_integer(attr, "mtu");
1014                         break;
1015                 case IFLA_LINK:
1016                         print_attr(attr, "link");
1017                         break;
1018                 case IFLA_QDISC:
1019                         print_attr(attr, "qdisc");
1020                         break;
1021                 case IFLA_STATS:
1022                         print_attr(attr, "stats");
1023                         break;
1024                 case IFLA_COST:
1025                         print_attr(attr, "cost");
1026                         break;
1027                 case IFLA_PRIORITY:
1028                         print_attr(attr, "priority");
1029                         break;
1030                 case IFLA_MASTER:
1031                         print_attr(attr, "master");
1032                         break;
1033                 case IFLA_WIRELESS:
1034                         print_attr(attr, "wireless");
1035                         break;
1036                 case IFLA_PROTINFO:
1037                         print_attr(attr, "protinfo");
1038                         break;
1039                 case IFLA_TXQLEN:
1040                         print_integer(attr, "txqlen");
1041                         break;
1042                 case IFLA_MAP:
1043                         print_attr(attr, "map");
1044                         break;
1045                 case IFLA_WEIGHT:
1046                         print_attr(attr, "weight");
1047                         break;
1048                 case IFLA_OPERSTATE:
1049                         print_byte(attr, "operstate");
1050                         break;
1051                 case IFLA_LINKMODE:
1052                         print_byte(attr, "linkmode");
1053                         break;
1054                 default:
1055                         print_attr(attr, NULL);
1056                         break;
1057                 }
1058         }
1059 }
1060
1061 static void rtnl_newlink(struct nlmsghdr *hdr)
1062 {
1063         struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
1064
1065         rtnl_link(hdr);
1066
1067         if (hdr->nlmsg_type == IFLA_WIRELESS)
1068                 connman_warn_once("Obsolete WEXT WiFi driver detected");
1069
1070         process_newlink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
1071                                 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
1072 }
1073
1074 static void rtnl_dellink(struct nlmsghdr *hdr)
1075 {
1076         struct ifinfomsg *msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
1077
1078         rtnl_link(hdr);
1079
1080         process_dellink(msg->ifi_type, msg->ifi_index, msg->ifi_flags,
1081                                 msg->ifi_change, msg, IFA_PAYLOAD(hdr));
1082 }
1083
1084 static void rtnl_addr(struct nlmsghdr *hdr)
1085 {
1086         struct ifaddrmsg *msg;
1087         struct rtattr *attr;
1088         int bytes;
1089
1090         msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1091         bytes = IFA_PAYLOAD(hdr);
1092
1093         print("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
1094
1095         for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
1096                                         attr = RTA_NEXT(attr, bytes)) {
1097                 switch (attr->rta_type) {
1098                 case IFA_ADDRESS:
1099                         print_inet(attr, "address", msg->ifa_family);
1100                         break;
1101                 case IFA_LOCAL:
1102                         print_inet(attr, "local", msg->ifa_family);
1103                         break;
1104                 case IFA_LABEL:
1105                         print_string(attr, "label");
1106                         break;
1107                 case IFA_BROADCAST:
1108                         print_inet(attr, "broadcast", msg->ifa_family);
1109                         break;
1110                 case IFA_ANYCAST:
1111                         print_attr(attr, "anycast");
1112                         break;
1113                 case IFA_CACHEINFO:
1114                         print_attr(attr, "cacheinfo");
1115                         break;
1116                 case IFA_MULTICAST:
1117                         print_attr(attr, "multicast");
1118                         break;
1119                 default:
1120                         print_attr(attr, NULL);
1121                         break;
1122                 }
1123         }
1124 }
1125
1126 static void rtnl_newaddr(struct nlmsghdr *hdr)
1127 {
1128         struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1129
1130         rtnl_addr(hdr);
1131
1132         process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1133                                                 msg, IFA_PAYLOAD(hdr));
1134 }
1135
1136 static void rtnl_deladdr(struct nlmsghdr *hdr)
1137 {
1138         struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
1139
1140         rtnl_addr(hdr);
1141
1142         process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
1143                                                 msg, IFA_PAYLOAD(hdr));
1144 }
1145
1146 static void rtnl_route(struct nlmsghdr *hdr)
1147 {
1148         struct rtmsg *msg;
1149         struct rtattr *attr;
1150         int bytes;
1151
1152         msg = (struct rtmsg *) NLMSG_DATA(hdr);
1153         bytes = RTM_PAYLOAD(hdr);
1154
1155         print("rtm_family %d rtm_table %d rtm_protocol %d",
1156                         msg->rtm_family, msg->rtm_table, msg->rtm_protocol);
1157         print("rtm_scope %d rtm_type %d rtm_flags 0x%04x",
1158                                 msg->rtm_scope, msg->rtm_type, msg->rtm_flags);
1159
1160         for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
1161                                         attr = RTA_NEXT(attr, bytes)) {
1162                 switch (attr->rta_type) {
1163                 case RTA_DST:
1164                         print_inet(attr, "dst", msg->rtm_family);
1165                         break;
1166                 case RTA_SRC:
1167                         print_inet(attr, "src", msg->rtm_family);
1168                         break;
1169                 case RTA_IIF:
1170                         print_string(attr, "iif");
1171                         break;
1172                 case RTA_OIF:
1173                         print_integer(attr, "oif");
1174                         break;
1175                 case RTA_GATEWAY:
1176                         print_inet(attr, "gateway", msg->rtm_family);
1177                         break;
1178                 case RTA_PRIORITY:
1179                         print_attr(attr, "priority");
1180                         break;
1181                 case RTA_PREFSRC:
1182                         print_inet(attr, "prefsrc", msg->rtm_family);
1183                         break;
1184                 case RTA_METRICS:
1185                         print_attr(attr, "metrics");
1186                         break;
1187                 case RTA_TABLE:
1188                         print_integer(attr, "table");
1189                         break;
1190                 default:
1191                         print_attr(attr, NULL);
1192                         break;
1193                 }
1194         }
1195 }
1196
1197 static bool is_route_rtmsg(struct rtmsg *msg)
1198 {
1199         if (msg->rtm_flags & RTM_F_CLONED)
1200                 return false;
1201
1202         if (msg->rtm_table != RT_TABLE_MAIN)
1203                 return false;
1204
1205         if (msg->rtm_protocol != RTPROT_BOOT &&
1206                         msg->rtm_protocol != RTPROT_KERNEL)
1207                 return false;
1208
1209         if (msg->rtm_type != RTN_UNICAST)
1210                 return false;
1211
1212         return true;
1213 }
1214
1215 static void rtnl_newroute(struct nlmsghdr *hdr)
1216 {
1217         struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1218
1219         rtnl_route(hdr);
1220
1221         if (is_route_rtmsg(msg))
1222                 process_newroute(msg->rtm_family, msg->rtm_scope,
1223                                                 msg, RTM_PAYLOAD(hdr));
1224 }
1225
1226 static void rtnl_delroute(struct nlmsghdr *hdr)
1227 {
1228         struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
1229
1230         rtnl_route(hdr);
1231
1232         if (is_route_rtmsg(msg))
1233                 process_delroute(msg->rtm_family, msg->rtm_scope,
1234                                                 msg, RTM_PAYLOAD(hdr));
1235 }
1236
1237 static void *rtnl_nd_opt_rdnss(struct nd_opt_hdr *opt, guint32 *lifetime,
1238                                int *nr_servers)
1239 {
1240         guint32 *optint = (void *)opt;
1241
1242         if (opt->nd_opt_len < 3)
1243                 return NULL;
1244
1245         if (*lifetime > ntohl(optint[1]))
1246                 *lifetime = ntohl(optint[1]);
1247
1248         /* nd_opt_len is in units of 8 bytes. The header is 1 unit (8 bytes)
1249            and each address is another 2 units (16 bytes).
1250            So the number of addresses (given rounding) is nd_opt_len/2 */
1251         *nr_servers = opt->nd_opt_len / 2;
1252
1253         /* And they start 8 bytes into the packet, or two guint32s in. */
1254         return optint + 2;
1255 }
1256
1257 static const char **rtnl_nd_opt_dnssl(struct nd_opt_hdr *opt, guint32 *lifetime)
1258 {
1259         const char **domains = NULL;
1260         guint32 *optint = (void *)opt;
1261         unsigned char *optc = (void *)&optint[2];
1262         int data_len = (opt->nd_opt_len * 8) - 8;
1263         int nr_domains = 0;
1264         int i, tmp;
1265
1266         if (*lifetime > ntohl(optint[1]))
1267                 *lifetime = ntohl(optint[1]);
1268
1269         /* Turn it into normal strings by converting the length bytes into '.',
1270            and count how many search domains there are while we're at it. */
1271         i = 0;
1272         while (i < data_len) {
1273                 if (optc[i] > 0x3f) {
1274                         DBG("DNSSL contains compressed elements in violation of RFC6106");
1275                         return NULL;
1276                 }
1277
1278                 if (optc[i] == 0) {
1279                         nr_domains++;
1280                         i++;
1281                         /* Check for double zero */
1282                         if (i < data_len && optc[i] == 0)
1283                                 break;
1284                         continue;
1285                 }
1286
1287                 tmp = i;
1288                 i += optc[i] + 1;
1289
1290                 if (i >= data_len) {
1291                         DBG("DNSSL data overflows option length");
1292                         return NULL;
1293                 }
1294
1295                 optc[tmp] = '.';
1296         }
1297
1298         domains = g_try_new0(const char *, nr_domains + 1);
1299         if (!domains)
1300                 return NULL;
1301
1302         /* Now point to the normal strings, missing out the leading '.' that
1303            each of them will have now. */
1304         for (i = 0; i < nr_domains; i++) {
1305                 domains[i] = (char *)optc + 1;
1306                 optc += strlen((char *)optc) + 1;
1307         }
1308
1309         return domains;
1310 }
1311
1312 static void rtnl_newnduseropt(struct nlmsghdr *hdr)
1313 {
1314         struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(hdr);
1315         struct nd_opt_hdr *opt;
1316         guint32 lifetime = -1;
1317         const char **domains = NULL;
1318         struct in6_addr *servers = NULL;
1319         int i, nr_servers = 0;
1320         int msglen = msg->nduseropt_opts_len;
1321         int index;
1322
1323         DBG("family %d index %d len %d type %d code %d",
1324                 msg->nduseropt_family, msg->nduseropt_ifindex,
1325                 msg->nduseropt_opts_len, msg->nduseropt_icmp_type,
1326                 msg->nduseropt_icmp_code);
1327
1328         if (msg->nduseropt_family != AF_INET6 ||
1329                         msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
1330                         msg->nduseropt_icmp_code != 0)
1331                 return;
1332
1333         index = msg->nduseropt_ifindex;
1334         if (index < 0)
1335                 return;
1336
1337 #if defined TIZEN_EXT
1338         struct connman_service *service;
1339         enum connman_service_state state;
1340         enum connman_dnsconfig_method ipv6_dns_method;
1341
1342         service = __connman_service_lookup_from_index(index);
1343         if (!service) {
1344                 DBG("Invalid service");
1345                 return;
1346         }
1347
1348         DBG("service: %p index: %d\n", service, index);
1349
1350         if (connman_setting_get_bool("SingleConnectedTechnology") == TRUE) {
1351                 state = __connman_service_ipconfig_get_state(service, CONNMAN_IPCONFIG_TYPE_IPV6);
1352                 if (state != CONNMAN_SERVICE_STATE_ASSOCIATION &&
1353                                 state != CONNMAN_SERVICE_STATE_CONFIGURATION &&
1354                                 state != CONNMAN_SERVICE_STATE_READY &&
1355                                 state != CONNMAN_SERVICE_STATE_ONLINE) {
1356                         DBG("Service state[%d] is not connecting/connected", state);
1357                         return;
1358                 }
1359         }
1360
1361         ipv6_dns_method = connman_service_get_ipv6_dns_method(service);
1362         if (ipv6_dns_method != CONNMAN_DNSCONFIG_METHOD_DHCP) {
1363                 DBG("IPv6 DNS method is not Auto ignore RA!!! [DNS method: %d]", ipv6_dns_method);
1364                 return;
1365         }
1366 #endif
1367
1368         for (opt = (void *)&msg[1];
1369                         msglen > 0;
1370                         msglen -= opt->nd_opt_len * 8,
1371                         opt = ((void *)opt) + opt->nd_opt_len*8) {
1372
1373                 DBG("remaining %d nd opt type %d len %d\n",
1374                         msglen, opt->nd_opt_type, opt->nd_opt_len);
1375
1376                 if (opt->nd_opt_type == 25) { /* ND_OPT_RDNSS */
1377                         char buf[40];
1378 #if defined TIZEN_EXT
1379                         struct connman_service *service;
1380
1381                         service = __connman_service_lookup_from_index(index);
1382                         DBG("service: %p\n",service);
1383 #endif
1384                         servers = rtnl_nd_opt_rdnss(opt, &lifetime,
1385                                                                 &nr_servers);
1386                         for (i = 0; i < nr_servers; i++) {
1387                                 if (!inet_ntop(AF_INET6, servers + i, buf,
1388                                                                 sizeof(buf)))
1389                                         continue;
1390
1391 #if defined TIZEN_EXT
1392                                 __connman_service_nameserver_remove(service,
1393                                                 buf, false,
1394                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
1395                                 __connman_service_nameserver_append(service,
1396                                                 buf, false,
1397                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
1398 #endif
1399                                 connman_resolver_append_lifetime(index,
1400                                                         NULL, buf, lifetime);
1401                         }
1402
1403                 } else if (opt->nd_opt_type == 31) { /* ND_OPT_DNSSL */
1404                         g_free(domains);
1405
1406                         domains = rtnl_nd_opt_dnssl(opt, &lifetime);
1407                         for (i = 0; domains && domains[i]; i++)
1408                                 connman_resolver_append_lifetime(index,
1409                                                 domains[i], NULL, lifetime);
1410                 }
1411         }
1412
1413         g_free(domains);
1414 }
1415
1416 static const char *type2string(uint16_t type)
1417 {
1418         switch (type) {
1419         case NLMSG_NOOP:
1420                 return "NOOP";
1421         case NLMSG_ERROR:
1422                 return "ERROR";
1423         case NLMSG_DONE:
1424                 return "DONE";
1425         case NLMSG_OVERRUN:
1426                 return "OVERRUN";
1427         case RTM_GETLINK:
1428                 return "GETLINK";
1429         case RTM_NEWLINK:
1430                 return "NEWLINK";
1431         case RTM_DELLINK:
1432                 return "DELLINK";
1433         case RTM_GETADDR:
1434                 return "GETADDR";
1435         case RTM_NEWADDR:
1436                 return "NEWADDR";
1437         case RTM_DELADDR:
1438                 return "DELADDR";
1439         case RTM_GETROUTE:
1440                 return "GETROUTE";
1441         case RTM_NEWROUTE:
1442                 return "NEWROUTE";
1443         case RTM_DELROUTE:
1444                 return "DELROUTE";
1445         case RTM_NEWNDUSEROPT:
1446                 return "NEWNDUSEROPT";
1447         default:
1448                 return "UNKNOWN";
1449         }
1450 }
1451
1452 static GIOChannel *channel = NULL;
1453 static guint channel_watch = 0;
1454
1455 struct rtnl_request {
1456         struct nlmsghdr hdr;
1457         struct rtgenmsg msg;
1458 };
1459 #define RTNL_REQUEST_SIZE  (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
1460
1461 static GSList *request_list = NULL;
1462 static guint32 request_seq = 0;
1463
1464 static struct rtnl_request *find_request(guint32 seq)
1465 {
1466         GSList *list;
1467
1468         for (list = request_list; list; list = list->next) {
1469                 struct rtnl_request *req = list->data;
1470
1471                 if (req->hdr.nlmsg_seq == seq)
1472                         return req;
1473         }
1474
1475         return NULL;
1476 }
1477
1478 static int send_request(struct rtnl_request *req)
1479 {
1480         struct sockaddr_nl addr;
1481         int sk;
1482
1483         DBG("%s len %d type %d flags 0x%04x seq %d",
1484                                 type2string(req->hdr.nlmsg_type),
1485                                 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1486                                 req->hdr.nlmsg_flags, req->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, req, req->hdr.nlmsg_len, 0,
1494                                 (struct sockaddr *) &addr, sizeof(addr));
1495 }
1496
1497 static int queue_request(struct rtnl_request *req)
1498 {
1499         request_list = g_slist_append(request_list, req);
1500
1501         if (g_slist_length(request_list) > 1)
1502                 return 0;
1503
1504         return send_request(req);
1505 }
1506
1507 static int process_response(guint32 seq)
1508 {
1509         struct rtnl_request *req;
1510
1511         DBG("seq %d", seq);
1512
1513         req = find_request(seq);
1514         if (req) {
1515                 request_list = g_slist_remove(request_list, req);
1516                 g_free(req);
1517         }
1518
1519         req = g_slist_nth_data(request_list, 0);
1520         if (!req)
1521                 return 0;
1522
1523         return send_request(req);
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 (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1590                 return FALSE;
1591
1592         memset(buf, 0, sizeof(buf));
1593         memset(&nladdr, 0, sizeof(nladdr));
1594
1595         fd = g_io_channel_unix_get_fd(chan);
1596
1597         status = recvfrom(fd, buf, sizeof(buf), 0,
1598                        (struct sockaddr *) &nladdr, &addr_len);
1599         if (status < 0) {
1600                 if (errno == EINTR || errno == EAGAIN)
1601                         return TRUE;
1602
1603                 return FALSE;
1604         }
1605
1606         if (status == 0)
1607                 return FALSE;
1608
1609         if (nladdr.nl_pid != 0) { /* not sent by kernel, ignore */
1610                 DBG("Received msg from %u, ignoring it", nladdr.nl_pid);
1611                 return TRUE;
1612         }
1613
1614         rtnl_message(buf, status);
1615
1616         return TRUE;
1617 }
1618
1619 static int send_getlink(void)
1620 {
1621         struct rtnl_request *req;
1622
1623         DBG("");
1624
1625         req = g_try_malloc0(RTNL_REQUEST_SIZE);
1626         if (!req)
1627                 return -ENOMEM;
1628
1629         req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1630         req->hdr.nlmsg_type = RTM_GETLINK;
1631         req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1632         req->hdr.nlmsg_pid = 0;
1633         req->hdr.nlmsg_seq = request_seq++;
1634         req->msg.rtgen_family = AF_INET;
1635
1636         return queue_request(req);
1637 }
1638
1639 static int send_getaddr(void)
1640 {
1641         struct rtnl_request *req;
1642
1643         DBG("");
1644
1645         req = g_try_malloc0(RTNL_REQUEST_SIZE);
1646         if (!req)
1647                 return -ENOMEM;
1648
1649         req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1650         req->hdr.nlmsg_type = RTM_GETADDR;
1651         req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1652         req->hdr.nlmsg_pid = 0;
1653         req->hdr.nlmsg_seq = request_seq++;
1654         req->msg.rtgen_family = AF_INET;
1655
1656         return queue_request(req);
1657 }
1658
1659 static int send_getroute(void)
1660 {
1661         struct rtnl_request *req;
1662
1663         DBG("");
1664
1665         req = g_try_malloc0(RTNL_REQUEST_SIZE);
1666         if (!req)
1667                 return -ENOMEM;
1668
1669         req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
1670         req->hdr.nlmsg_type = RTM_GETROUTE;
1671         req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1672         req->hdr.nlmsg_pid = 0;
1673         req->hdr.nlmsg_seq = request_seq++;
1674         req->msg.rtgen_family = AF_INET;
1675
1676         return queue_request(req);
1677 }
1678
1679 static gboolean update_timeout_cb(gpointer user_data)
1680 {
1681         __connman_rtnl_request_update();
1682
1683         return TRUE;
1684 }
1685
1686 static void update_interval_callback(guint min)
1687 {
1688         if (update_timeout > 0)
1689                 g_source_remove(update_timeout);
1690
1691         if (min < G_MAXUINT) {
1692                 update_interval = min;
1693                 update_timeout = g_timeout_add_seconds(update_interval,
1694                                                 update_timeout_cb, NULL);
1695         } else {
1696                 update_timeout = 0;
1697                 update_interval = G_MAXUINT;
1698         }
1699 }
1700
1701 static gint compare_interval(gconstpointer a, gconstpointer b)
1702 {
1703         guint val_a = GPOINTER_TO_UINT(a);
1704         guint val_b = GPOINTER_TO_UINT(b);
1705
1706         return val_a - val_b;
1707 }
1708
1709 unsigned int __connman_rtnl_update_interval_add(unsigned int interval)
1710 {
1711         guint min;
1712
1713         if (interval == 0)
1714                 return 0;
1715
1716         update_list = g_slist_insert_sorted(update_list,
1717                         GUINT_TO_POINTER(interval), compare_interval);
1718
1719         min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1720         if (min < update_interval) {
1721                 update_interval_callback(min);
1722                 __connman_rtnl_request_update();
1723         }
1724
1725         return update_interval;
1726 }
1727
1728 unsigned int __connman_rtnl_update_interval_remove(unsigned int interval)
1729 {
1730         guint min = G_MAXUINT;
1731
1732         if (interval == 0)
1733                 return 0;
1734
1735         update_list = g_slist_remove(update_list, GINT_TO_POINTER(interval));
1736
1737         if (update_list)
1738                 min = GPOINTER_TO_UINT(g_slist_nth_data(update_list, 0));
1739
1740         if (min > update_interval)
1741                 update_interval_callback(min);
1742
1743         return min;
1744 }
1745
1746 int __connman_rtnl_request_update(void)
1747 {
1748         return send_getlink();
1749 }
1750
1751 int __connman_rtnl_init(void)
1752 {
1753         struct sockaddr_nl addr;
1754         int sk;
1755
1756         DBG("");
1757
1758         interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1759                                                         NULL, free_interface);
1760
1761         sk = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
1762         if (sk < 0)
1763                 return -1;
1764
1765         memset(&addr, 0, sizeof(addr));
1766         addr.nl_family = AF_NETLINK;
1767         addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1768                                 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE |
1769                                 (1<<(RTNLGRP_ND_USEROPT-1));
1770
1771         if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1772                 close(sk);
1773                 return -1;
1774         }
1775
1776         channel = g_io_channel_unix_new(sk);
1777         g_io_channel_set_close_on_unref(channel, TRUE);
1778
1779         g_io_channel_set_encoding(channel, NULL, NULL);
1780         g_io_channel_set_buffered(channel, FALSE);
1781
1782         channel_watch = g_io_add_watch(channel,
1783                                 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1784                                 netlink_event, NULL);
1785
1786         return 0;
1787 }
1788
1789 void __connman_rtnl_start(void)
1790 {
1791         DBG("");
1792
1793         send_getlink();
1794         send_getaddr();
1795         send_getroute();
1796 }
1797
1798 void __connman_rtnl_cleanup(void)
1799 {
1800         GSList *list;
1801
1802         DBG("");
1803
1804         for (list = watch_list; list; list = list->next) {
1805                 struct watch_data *watch = list->data;
1806
1807                 DBG("removing watch %d", watch->id);
1808
1809                 g_free(watch);
1810                 list->data = NULL;
1811         }
1812
1813         g_slist_free(watch_list);
1814         watch_list = NULL;
1815
1816         g_slist_free(update_list);
1817         update_list = NULL;
1818
1819         for (list = request_list; list; list = list->next) {
1820                 struct rtnl_request *req = list->data;
1821
1822                 DBG("%s len %d type %d flags 0x%04x seq %d",
1823                                 type2string(req->hdr.nlmsg_type),
1824                                 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
1825                                 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
1826
1827                 g_free(req);
1828                 list->data = NULL;
1829         }
1830
1831         g_slist_free(request_list);
1832         request_list = NULL;
1833
1834         if (channel_watch) {
1835                 g_source_remove(channel_watch);
1836                 channel_watch = 0;
1837         }
1838
1839         g_io_channel_shutdown(channel, TRUE, NULL);
1840         g_io_channel_unref(channel);
1841
1842         channel = NULL;
1843
1844         g_hash_table_destroy(interface_list);
1845 }