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