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