ip6: don't try to process invalid RA nameservers
[NetworkManager.git] / src / ip6-manager / nm-ip6-manager.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* nm-ip6-manager.c - Handle IPv6 address configuration for NetworkManager
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * Copyright (C) 2009 - 2010 Red Hat, Inc.
19  */
20
21 #include <errno.h>
22 #include <netinet/icmp6.h>
23
24 #include <netlink/route/addr.h>
25 #include <netlink/route/rtnl.h>
26 #include <netlink/route/route.h>
27
28 #include "nm-ip6-manager.h"
29 #include "nm-netlink-monitor.h"
30 #include "NetworkManagerUtils.h"
31 #include "nm-marshal.h"
32 #include "nm-logging.h"
33 #include "nm-system.h"
34
35 /* Pre-DHCP addrconf timeout, in seconds */
36 #define NM_IP6_TIMEOUT 20
37
38 /* FIXME? Stolen from the kernel sources */
39 #define IF_RA_OTHERCONF 0x80
40 #define IF_RA_MANAGED   0x40
41 #define IF_RA_RCVD      0x20
42 #define IF_RS_SENT      0x10
43
44 typedef struct {
45         NMNetlinkMonitor *monitor;
46         GHashTable *devices;
47
48         struct nl_handle *nlh;
49         struct nl_cache *addr_cache, *route_cache;
50
51         guint netlink_id;
52 } NMIP6ManagerPrivate;
53
54 #define NM_IP6_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_IP6_MANAGER, NMIP6ManagerPrivate))
55
56 G_DEFINE_TYPE (NMIP6Manager, nm_ip6_manager, G_TYPE_OBJECT)
57
58 enum {
59         ADDRCONF_COMPLETE,
60         CONFIG_CHANGED,
61         LAST_SIGNAL
62 };
63
64 static guint signals[LAST_SIGNAL] = { 0 };
65
66 typedef enum {
67         NM_IP6_DEVICE_UNCONFIGURED,
68         NM_IP6_DEVICE_GOT_LINK_LOCAL,
69         NM_IP6_DEVICE_GOT_ROUTER_ADVERTISEMENT,
70         NM_IP6_DEVICE_GOT_ADDRESS,
71         NM_IP6_DEVICE_TIMED_OUT
72 } NMIP6DeviceState;
73
74 typedef struct {
75         struct in6_addr addr;
76         time_t expires;
77 } NMIP6RDNSS;
78
79 typedef struct {
80         char domain[256];
81         time_t expires;
82 } NMIP6DNSSL;
83
84 /******************************************************************/
85
86 typedef struct {
87         NMIP6Manager *manager;
88         char *iface;
89         int ifindex;
90
91         char *disable_ip6_path;
92         gboolean disable_ip6_save_valid;
93         guint32 disable_ip6_save;
94
95         guint finish_addrconf_id;
96         guint config_changed_id;
97
98         NMIP6DeviceState state;
99         NMIP6DeviceState target_state;
100         gboolean addrconf_complete;
101
102         GArray *rdnss_servers;
103         guint rdnss_timeout_id;
104
105         GArray *dnssl_domains;
106         guint dnssl_timeout_id;
107
108         guint ip6flags_poll_id;
109
110         guint32 ra_flags;
111 } NMIP6Device;
112
113 static void
114 nm_ip6_device_destroy (NMIP6Device *device)
115 {
116         g_return_if_fail (device != NULL);
117
118         /* reset the saved IPv6 value */
119         if (device->disable_ip6_save_valid) {
120                 nm_utils_do_sysctl (device->disable_ip6_path,
121                                     device->disable_ip6_save ? "1\n" : "0\n");
122         }
123
124         if (device->finish_addrconf_id)
125                 g_source_remove (device->finish_addrconf_id);
126         if (device->config_changed_id)
127                 g_source_remove (device->config_changed_id);
128         g_free (device->iface);
129         if (device->rdnss_servers)
130                 g_array_free (device->rdnss_servers, TRUE);
131         if (device->rdnss_timeout_id)
132                 g_source_remove (device->rdnss_timeout_id);
133         if (device->dnssl_domains)
134                 g_array_free (device->dnssl_domains, TRUE);
135         if (device->dnssl_timeout_id)
136                 g_source_remove (device->dnssl_timeout_id);
137         if (device->ip6flags_poll_id)
138                 g_source_remove (device->ip6flags_poll_id);
139
140         g_slice_free (NMIP6Device, device);
141 }
142
143 static NMIP6Device *
144 nm_ip6_device_new (NMIP6Manager *manager, int ifindex)
145 {
146         NMIP6ManagerPrivate *priv = NM_IP6_MANAGER_GET_PRIVATE (manager);
147         NMIP6Device *device;
148
149         g_return_val_if_fail (ifindex > 0, NULL);
150
151         device = g_slice_new0 (NMIP6Device);
152         if (!device) {
153                 nm_log_err (LOGD_IP6, "(%d): out of memory creating IP6 addrconf object.",
154                             ifindex);
155                 return NULL;
156         }
157
158         device->ifindex = ifindex;
159         device->iface = g_strdup (nm_netlink_index_to_iface (ifindex));
160         if (!device->iface) {
161                 nm_log_err (LOGD_IP6, "(%d): could not find interface name from index.",
162                             ifindex);
163                 goto error;
164         }
165
166         device->manager = manager;
167
168         device->rdnss_servers = g_array_new (FALSE, FALSE, sizeof (NMIP6RDNSS));
169
170         device->dnssl_domains = g_array_new (FALSE, FALSE, sizeof (NMIP6DNSSL));
171
172         g_hash_table_replace (priv->devices, GINT_TO_POINTER (device->ifindex), device);
173
174         /* and the original value of IPv6 enable/disable */
175         device->disable_ip6_path = g_strdup_printf ("/proc/sys/net/ipv6/conf/%s/disable_ipv6",
176                                                     device->iface);
177         g_assert (device->disable_ip6_path);
178         device->disable_ip6_save_valid = nm_utils_get_proc_sys_net_value (device->disable_ip6_path,
179                                                                           device->iface,
180                                                                           &device->disable_ip6_save);
181
182         return device;
183
184 error:
185         nm_ip6_device_destroy (device);
186         return NULL;
187 }
188
189 static NMIP6Device *
190 nm_ip6_manager_get_device (NMIP6Manager *manager, int ifindex)
191 {
192         NMIP6ManagerPrivate *priv;
193
194         g_return_val_if_fail (manager != NULL, NULL);
195         g_return_val_if_fail (NM_IS_IP6_MANAGER (manager), NULL);
196
197         priv = NM_IP6_MANAGER_GET_PRIVATE (manager);
198         return g_hash_table_lookup (priv->devices, GINT_TO_POINTER (ifindex));
199 }
200
201 /******************************************************************/
202
203 typedef struct {
204         NMIP6Device *device;
205         guint dhcp_opts;
206         gboolean success;
207 } CallbackInfo;
208
209 static gboolean
210 finish_addrconf (gpointer user_data)
211 {
212         CallbackInfo *info = user_data;
213         NMIP6Device *device = info->device;
214         NMIP6Manager *manager = device->manager;
215         int ifindex;
216
217         device->finish_addrconf_id = 0;
218         device->addrconf_complete = TRUE;
219         ifindex = device->ifindex;
220
221         /* We're done, stop polling IPv6 flags */
222         if (device->ip6flags_poll_id) {
223                 g_source_remove (device->ip6flags_poll_id);
224                 device->ip6flags_poll_id = 0;
225         }
226
227         /* And tell listeners that addrconf is complete */
228         if (info->success) {
229                 g_signal_emit (manager, signals[ADDRCONF_COMPLETE], 0,
230                                ifindex, info->dhcp_opts, TRUE);
231         } else {
232                 nm_log_info (LOGD_IP6, "(%s): IP6 addrconf timed out or failed.",
233                              device->iface);
234
235                 nm_ip6_manager_cancel_addrconf (manager, ifindex);
236                 g_signal_emit (manager, signals[ADDRCONF_COMPLETE], 0,
237                                ifindex, info->dhcp_opts, FALSE);
238         }
239
240         return FALSE;
241 }
242
243 static gboolean
244 emit_config_changed (gpointer user_data)
245 {
246         CallbackInfo *info = user_data;
247         NMIP6Device *device = info->device;
248         NMIP6Manager *manager = device->manager;
249
250         device->config_changed_id = 0;
251         g_signal_emit (manager, signals[CONFIG_CHANGED], 0,
252                        device->ifindex,
253                        info->dhcp_opts,
254                        info->success);
255         return FALSE;
256 }
257
258 static void set_rdnss_timeout (NMIP6Device *device);
259
260 static gboolean
261 rdnss_expired (gpointer user_data)
262 {
263         NMIP6Device *device = user_data;
264         CallbackInfo info = { device, IP6_DHCP_OPT_NONE, FALSE };
265
266         nm_log_dbg (LOGD_IP6, "(%s): IPv6 RDNSS information expired", device->iface);
267
268         set_rdnss_timeout (device);
269         emit_config_changed (&info);
270         return FALSE;
271 }
272
273 static void
274 set_rdnss_timeout (NMIP6Device *device)
275 {
276         time_t expires = 0, now = time (NULL);
277         NMIP6RDNSS *rdnss;
278         int i;
279
280         if (device->rdnss_timeout_id) {
281                 g_source_remove (device->rdnss_timeout_id);
282                 device->rdnss_timeout_id = 0;
283         }
284
285         /* Find the soonest expiration time. */
286         for (i = 0; i < device->rdnss_servers->len; i++) {
287                 rdnss = &g_array_index (device->rdnss_servers, NMIP6RDNSS, i);
288                 if (rdnss->expires == 0)
289                         continue;
290
291                 /* If the entry has already expired, remove it; the "+ 1" is
292                  * because g_timeout_add_seconds() might fudge the timing a
293                  * bit.
294                  */
295                 if (rdnss->expires <= now + 1) {
296                         char buf[INET6_ADDRSTRLEN + 1];
297
298                         if (inet_ntop (AF_INET6, &(rdnss->addr), buf, sizeof (buf)) > 0) {
299                                 nm_log_dbg (LOGD_IP6, "(%s): removing expired RA-provided nameserver %s",
300                                             device->iface, buf);
301                         }
302                         g_array_remove_index (device->rdnss_servers, i--);
303                         continue;
304                 }
305
306                 if (!expires || rdnss->expires < expires)
307                         expires = rdnss->expires;
308         }
309
310         if (expires) {
311                 device->rdnss_timeout_id = g_timeout_add_seconds (expires - now,
312                                                                                                                   rdnss_expired,
313                                                                                                                   device);
314         }
315 }
316
317 static void set_dnssl_timeout (NMIP6Device *device);
318
319 static gboolean
320 dnssl_expired (gpointer user_data)
321 {
322         NMIP6Device *device = user_data;
323         CallbackInfo info = { device, IP6_DHCP_OPT_NONE };
324
325         nm_log_dbg (LOGD_IP6, "(%s): IPv6 DNSSL information expired", device->iface);
326
327         set_dnssl_timeout (device);
328         emit_config_changed (&info);
329         return FALSE;
330 }
331
332 static void
333 set_dnssl_timeout (NMIP6Device *device)
334 {
335         time_t expires = 0, now = time (NULL);
336         NMIP6DNSSL *dnssl;
337         int i;
338
339         if (device->dnssl_timeout_id) {
340                 g_source_remove (device->dnssl_timeout_id);
341                 device->dnssl_timeout_id = 0;
342         }
343
344         /* Find the soonest expiration time. */
345         for (i = 0; i < device->dnssl_domains->len; i++) {
346                 dnssl = &g_array_index (device->dnssl_domains, NMIP6DNSSL, i);
347                 if (dnssl->expires == 0)
348                         continue;
349
350                 /* If the entry has already expired, remove it; the "+ 1" is
351                  * because g_timeout_add_seconds() might fudge the timing a
352                  * bit.
353                  */
354                 if (dnssl->expires <= now + 1) {
355                         nm_log_dbg (LOGD_IP6, "(%s): removing expired RA-provided domain %s",
356                                     device->iface, dnssl->domain);
357                         g_array_remove_index (device->dnssl_domains, i--);
358                         continue;
359                 }
360
361                 if (!expires || dnssl->expires < expires)
362                         expires = dnssl->expires;
363         }
364
365         if (expires) {
366                 device->dnssl_timeout_id = g_timeout_add_seconds (expires - now,
367                                                                   dnssl_expired,
368                                                                   device);
369         }
370 }
371
372 static CallbackInfo *
373 callback_info_new (NMIP6Device *device, guint dhcp_opts, gboolean success)
374 {
375         CallbackInfo *info;
376
377         info = g_malloc0 (sizeof (CallbackInfo));
378         info->device = device;
379         info->dhcp_opts = dhcp_opts;
380         info->success = success;
381         return info;
382 }
383
384 static const char *
385 state_to_string (NMIP6DeviceState state)
386 {
387         switch (state) {
388         case NM_IP6_DEVICE_UNCONFIGURED:
389                 return "unconfigured";
390         case NM_IP6_DEVICE_GOT_LINK_LOCAL:
391                 return "got-link-local";
392         case NM_IP6_DEVICE_GOT_ROUTER_ADVERTISEMENT:
393                 return "got-ra";
394         case NM_IP6_DEVICE_GOT_ADDRESS:
395                 return "got-address";
396         case NM_IP6_DEVICE_TIMED_OUT:
397                 return "timed-out";
398         default:
399                 return "unknown";
400         }
401 }
402
403 static void
404 nm_ip6_device_sync_from_netlink (NMIP6Device *device, gboolean config_changed)
405 {
406         NMIP6Manager *manager = device->manager;
407         NMIP6ManagerPrivate *priv = NM_IP6_MANAGER_GET_PRIVATE (manager);
408         struct rtnl_addr *rtnladdr;
409         struct nl_addr *nladdr;
410         struct in6_addr *addr;
411         CallbackInfo *info;
412         guint dhcp_opts = IP6_DHCP_OPT_NONE;
413         gboolean found_linklocal = FALSE, found_other = FALSE;
414
415         nm_log_dbg (LOGD_IP6, "(%s): syncing with netlink (ra_flags 0x%X) (state/target '%s'/'%s')",
416                     device->iface, device->ra_flags,
417                     state_to_string (device->state),
418                     state_to_string (device->target_state));
419
420         /* Look for any IPv6 addresses the kernel may have set for the device */
421         for (rtnladdr = (struct rtnl_addr *) nl_cache_get_first (priv->addr_cache);
422                  rtnladdr;
423                  rtnladdr = (struct rtnl_addr *) nl_cache_get_next ((struct nl_object *) rtnladdr)) {
424                 char buf[INET6_ADDRSTRLEN];
425
426                 if (rtnl_addr_get_ifindex (rtnladdr) != device->ifindex)
427                         continue;
428
429                 nladdr = rtnl_addr_get_local (rtnladdr);
430                 if (!nladdr || nl_addr_get_family (nladdr) != AF_INET6)
431                         continue;
432
433                 addr = nl_addr_get_binary_addr (nladdr);
434
435                 if (inet_ntop (AF_INET6, addr, buf, INET6_ADDRSTRLEN) > 0) {
436                         nm_log_dbg (LOGD_IP6, "(%s): netlink address: %s",
437                                     device->iface, buf);
438                 }
439
440                 if (IN6_IS_ADDR_LINKLOCAL (addr)) {
441                         if (device->state == NM_IP6_DEVICE_UNCONFIGURED)
442                                 device->state = NM_IP6_DEVICE_GOT_LINK_LOCAL;
443                         found_linklocal = TRUE;
444                 } else {
445                         if (device->state < NM_IP6_DEVICE_GOT_ADDRESS)
446                                 device->state = NM_IP6_DEVICE_GOT_ADDRESS;
447                         found_other = TRUE;
448                 }
449         }
450
451         /* There might be a LL address hanging around on the interface from
452          * before in the initial run, but if it goes away later, make sure we
453          * regress from GOT_LINK_LOCAL back to UNCONFIGURED.
454          */
455         if ((device->state == NM_IP6_DEVICE_GOT_LINK_LOCAL) && !found_linklocal)
456                 device->state = NM_IP6_DEVICE_UNCONFIGURED;
457
458         nm_log_dbg (LOGD_IP6, "(%s): addresses synced (state %s)",
459                     device->iface, state_to_string (device->state));
460
461         /* We only care about router advertisements if we want a real IPv6 address */
462         if (   (device->target_state == NM_IP6_DEVICE_GOT_ADDRESS)
463             && (device->ra_flags & IF_RA_RCVD)) {
464
465                 if (device->state < NM_IP6_DEVICE_GOT_ROUTER_ADVERTISEMENT)
466                         device->state = NM_IP6_DEVICE_GOT_ROUTER_ADVERTISEMENT;
467
468                 if (device->ra_flags & IF_RA_MANAGED) {
469                         dhcp_opts = IP6_DHCP_OPT_MANAGED;
470                         nm_log_dbg (LOGD_IP6, "router advertisement deferred to DHCPv6");
471                 } else if (device->ra_flags & IF_RA_OTHERCONF) {
472                         dhcp_opts = IP6_DHCP_OPT_OTHERCONF;
473                         nm_log_dbg (LOGD_IP6, "router advertisement requests parallel DHCPv6");
474                 }
475         }
476
477         if (!device->addrconf_complete) {
478                 /* Managed mode (ie DHCP only) short-circuits automatic addrconf, so
479                  * we don't bother waiting for the device's target state to be reached
480                  * when the RA requests managed mode.
481                  */
482                 if (   (device->state >= device->target_state)
483                     || (dhcp_opts == IP6_DHCP_OPT_MANAGED)) {
484                         /* device->finish_addrconf_id may currently be a timeout
485                          * rather than an idle, so we remove the existing source.
486                          */
487                         if (device->finish_addrconf_id)
488                                 g_source_remove (device->finish_addrconf_id);
489
490                         nm_log_dbg (LOGD_IP6, "(%s): reached target state or Managed-mode requested (state '%s') (dhcp opts 0x%X)",
491                                     device->iface, state_to_string (device->state),
492                                     dhcp_opts);
493
494                         info = callback_info_new (device, dhcp_opts, TRUE);
495                         device->finish_addrconf_id = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
496                                                                       finish_addrconf,
497                                                                       info,
498                                                                       (GDestroyNotify) g_free);
499                 }
500         } else if (config_changed) {
501                 if (!device->config_changed_id) {
502                         gboolean success = TRUE;
503
504                         /* If for some reason an RA-provided address disappeared, we need
505                          * to make sure we fail the connection as it's no longer valid.
506                          */
507                         if (   (device->state == NM_IP6_DEVICE_GOT_ADDRESS)
508                             && (device->target_state == NM_IP6_DEVICE_GOT_ADDRESS)
509                             && !found_other) {
510                                 nm_log_dbg (LOGD_IP6, "(%s): RA-provided address no longer valid",
511                                             device->iface);
512                                 success = FALSE;
513                         }
514
515                         info = callback_info_new (device, dhcp_opts, success);
516                         device->config_changed_id = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
517                                                                      emit_config_changed,
518                                                                      info,
519                                                                      (GDestroyNotify) g_free);
520                 }
521         }
522 }
523
524 static void
525 ref_object (struct nl_object *obj, void *data)
526 {
527         struct nl_object **out = data;
528
529         nl_object_get (obj);
530         *out = obj;
531 }
532
533 static NMIP6Device *
534 process_addr (NMIP6Manager *manager, struct nl_msg *msg)
535 {
536         NMIP6ManagerPrivate *priv = NM_IP6_MANAGER_GET_PRIVATE (manager);
537         NMIP6Device *device;
538         struct rtnl_addr *rtnladdr;
539         int old_size;
540
541         nm_log_dbg (LOGD_IP6, "processing netlink new/del address message");
542
543         rtnladdr = NULL;
544         nl_msg_parse (msg, ref_object, &rtnladdr);
545         if (!rtnladdr) {
546                 nm_log_dbg (LOGD_IP6, "error processing netlink new/del address message");
547                 return NULL;
548         }
549
550         device = nm_ip6_manager_get_device (manager, rtnl_addr_get_ifindex (rtnladdr));
551         if (!device) {
552                 nm_log_dbg (LOGD_IP6, "ignoring message for unknown device");
553                 return NULL;
554         }
555
556         old_size = nl_cache_nitems (priv->addr_cache);
557         nl_cache_include (priv->addr_cache, (struct nl_object *)rtnladdr, NULL);
558         rtnl_addr_put (rtnladdr);
559
560         /* The kernel will re-notify us of automatically-added addresses
561          * every time it gets another router advertisement. We only want
562          * to notify higher levels if we actually changed something.
563          */
564         if (nl_cache_nitems (priv->addr_cache) == old_size) {
565                 nm_log_dbg (LOGD_IP6, "(%s): address cache unchanged, ignoring message",
566                             device->iface);
567                 return NULL;
568         }
569
570         return device;
571 }
572
573 static NMIP6Device *
574 process_route (NMIP6Manager *manager, struct nl_msg *msg)
575 {
576         NMIP6ManagerPrivate *priv = NM_IP6_MANAGER_GET_PRIVATE (manager);
577         NMIP6Device *device;
578         struct rtnl_route *rtnlroute;
579         int old_size;
580
581         nm_log_dbg (LOGD_IP6, "processing netlink new/del route message");
582
583         rtnlroute = NULL;
584         nl_msg_parse (msg, ref_object, &rtnlroute);
585         if (!rtnlroute) {
586                 nm_log_dbg (LOGD_IP6, "error processing netlink new/del route message");
587                 return NULL;
588         }
589
590         device = nm_ip6_manager_get_device (manager, rtnl_route_get_oif (rtnlroute));
591         if (!device) {
592                 nm_log_dbg (LOGD_IP6, "ignoring message for unknown device");
593                 return NULL;
594         }
595
596         old_size = nl_cache_nitems (priv->route_cache);
597         nl_cache_include (priv->route_cache, (struct nl_object *)rtnlroute, NULL);
598         rtnl_route_put (rtnlroute);
599
600         /* As above in process_addr */
601         if (nl_cache_nitems (priv->route_cache) == old_size) {
602                 nm_log_dbg (LOGD_IP6, "(%s): route cache unchanged, ignoring message",
603                             device->iface);
604                 return NULL;
605         }
606
607         return device;
608 }
609
610 static NMIP6Device *
611 process_prefix (NMIP6Manager *manager, struct nl_msg *msg)
612 {
613         struct prefixmsg *pmsg;
614         NMIP6Device *device;
615
616         /* We don't care about the prefix itself, but if we receive a
617          * router advertisement telling us to use DHCP, we might not
618          * get any RTM_NEWADDRs or RTM_NEWROUTEs, so this is our only
619          * way to notice immediately that an RA was received.
620          */
621
622         nm_log_dbg (LOGD_IP6, "processing netlink new prefix message");
623
624         pmsg = (struct prefixmsg *) NLMSG_DATA (nlmsg_hdr (msg));
625         device = nm_ip6_manager_get_device (manager, pmsg->prefix_ifindex);
626
627         if (!device || device->addrconf_complete) {
628                 nm_log_dbg (LOGD_IP6, "(%s): ignoring unknown or completed device",
629                             device ? device->iface : "(none)");
630                 return NULL;
631         }
632
633         return device;
634 }
635
636 /* RDNSS parsing code based on rdnssd, Copyright 2007 Pierre Ynard,
637  * RĂ©mi Denis-Courmont. GPLv2/3
638  */
639
640 #define ND_OPT_RDNSS 25
641 #define ND_OPT_DNSSL 31
642
643 struct nd_opt_rdnss {
644         uint8_t nd_opt_rdnss_type;
645         uint8_t nd_opt_rdnss_len;
646         uint16_t nd_opt_rdnss_reserved1;
647         uint32_t nd_opt_rdnss_lifetime;
648         /* followed by one or more IPv6 addresses */
649 } __attribute__ ((packed));
650
651 struct nd_opt_dnssl {
652         uint8_t nd_opt_dnssl_type;
653         uint8_t nd_opt_dnssl_len;
654         uint16_t nd_opt_dnssl_reserved1;
655         uint32_t nd_opt_dnssl_lifetime;
656         /* followed by one or more suffixes */
657 } __attribute__ ((packed));
658
659 static gboolean
660 process_nduseropt_rdnss (NMIP6Device *device, struct nd_opt_hdr *opt)
661 {
662         size_t opt_len;
663         struct nd_opt_rdnss *rdnss_opt;
664         time_t now = time (NULL);
665         struct in6_addr *addr;
666         GArray *new_servers;
667         NMIP6RDNSS server, *cur_server;
668         gboolean changed = FALSE;
669         guint i;
670
671         opt_len = opt->nd_opt_len;
672
673         if (opt_len < 3 || (opt_len & 1) == 0)
674                 return FALSE;
675
676         rdnss_opt = (struct nd_opt_rdnss *) opt;
677
678         new_servers = g_array_new (FALSE, FALSE, sizeof (NMIP6RDNSS));
679
680         /* Pad the DNS server expiry somewhat to give a bit of slack in cases
681          * where one RA gets lost or something (which can happen on unreliable
682          * links like WiFi where certain types of frames are not retransmitted).
683          * Note that 0 has special meaning and is therefore not adjusted.
684          */
685         server.expires = ntohl (rdnss_opt->nd_opt_rdnss_lifetime);
686         if (server.expires > 0)
687                 server.expires += now + 10;
688
689         for (addr = (struct in6_addr *) (rdnss_opt + 1); opt_len >= 2; addr++, opt_len -= 2) {
690                 char buf[INET6_ADDRSTRLEN + 1];
691
692                 if (!inet_ntop (AF_INET6, addr, buf, sizeof (buf))) {
693                         nm_log_warn (LOGD_IP6, "(%s): received invalid RA-provided nameserver", device->iface);
694                         continue;
695                 }
696
697                 for (i = 0; i < device->rdnss_servers->len; i++) {
698                         cur_server = &(g_array_index (device->rdnss_servers, NMIP6RDNSS, i));
699
700                         if (!IN6_ARE_ADDR_EQUAL (addr, &cur_server->addr))
701                                 continue;
702
703                         cur_server->expires = server.expires;
704
705                         if (server.expires > 0) {
706                                 nm_log_dbg (LOGD_IP6, "(%s): refreshing RA-provided nameserver %s (expires in %ld seconds)",
707                                             device->iface, buf,
708                                             server.expires - now);
709                                 break;
710                         }
711
712                         nm_log_dbg (LOGD_IP6, "(%s): removing RA-provided nameserver %s on router request",
713                                     device->iface, buf);
714
715                         g_array_remove_index (device->rdnss_servers, i);
716                         changed = TRUE;
717                         break;
718                 }
719
720                 if (server.expires == 0)
721                         continue;
722                 if (i < device->rdnss_servers->len)
723                         continue;
724
725                 nm_log_dbg (LOGD_IP6, "(%s): found RA-provided nameserver %s (expires in %ld seconds)",
726                             device->iface, buf, server.expires - now);
727
728                 server.addr = *addr;
729                 g_array_append_val (new_servers, server);
730         }
731
732         /* New servers must be added in the order they are listed in the
733          * RA option and before any existing servers.
734          *
735          * Note: This is the place to remove servers if we want to cap the
736          *       number of resolvers. The RFC states that the one to expire
737          *       first of the existing servers should be removed.
738          */
739         if (new_servers->len) {
740                 g_array_prepend_vals (device->rdnss_servers,
741                                       new_servers->data, new_servers->len);
742                 changed = TRUE;
743         }
744
745         g_array_free (new_servers, TRUE);
746
747         /* Timeouts may have changed even if IPs didn't */
748         set_rdnss_timeout (device);
749
750         return changed;
751 }
752
753 static const char *
754 parse_dnssl_domain (const unsigned char *buffer, size_t maxlen)
755 {
756         static char domain[256];
757         size_t label_len;
758
759         domain[0] = '\0';
760
761         while (maxlen > 0) {
762                 label_len = *buffer;
763                 buffer++;
764                 maxlen--;
765
766                 if (label_len == 0)
767                         return domain;
768
769                 if (label_len > maxlen)
770                         return NULL;
771                 if ((sizeof (domain) - strlen (domain)) < (label_len + 2))
772                         return NULL;
773
774                 if (domain[0] != '\0')
775                         strcat (domain, ".");
776                 strncat (domain, (const char *)buffer, label_len);
777                 buffer += label_len;
778                 maxlen -= label_len;
779         }
780
781         return NULL;
782 }
783
784 static gboolean
785 process_nduseropt_dnssl (NMIP6Device *device, struct nd_opt_hdr *opt)
786 {
787         size_t opt_len;
788         struct nd_opt_dnssl *dnssl_opt;
789         unsigned char *opt_ptr;
790         time_t now = time (NULL);
791         GArray *new_domains;
792         NMIP6DNSSL domain, *cur_domain;
793         gboolean changed;
794         guint i;
795
796         opt_len = opt->nd_opt_len;
797
798         if (opt_len < 2)
799                 return FALSE;
800
801         dnssl_opt = (struct nd_opt_dnssl *) opt;
802
803         opt_ptr = (unsigned char *)(dnssl_opt + 1);
804         opt_len = (opt_len - 1) * 8; /* prefer bytes for later handling */
805
806         new_domains = g_array_new (FALSE, FALSE, sizeof (NMIP6DNSSL));
807
808         changed = FALSE;
809
810         /* Pad the DNS server expiry somewhat to give a bit of slack in cases
811          * where one RA gets lost or something (which can happen on unreliable
812          * links like wifi where certain types of frames are not retransmitted).
813          * Note that 0 has special meaning and is therefore not adjusted.
814          */
815         domain.expires = ntohl (dnssl_opt->nd_opt_dnssl_lifetime);
816         if (domain.expires > 0)
817                 domain.expires += now + 10;
818
819         while (opt_len) {
820                 const char *domain_str;
821
822                 domain_str = parse_dnssl_domain (opt_ptr, opt_len);
823                 if (domain_str == NULL) {
824                         nm_log_dbg (LOGD_IP6, "(%s): invalid DNSSL option, parsing aborted",
825                                     device->iface);
826                         break;
827                 }
828
829                 /* The DNSSL encoding of domains happen to occupy the same size
830                  * as the length of the resulting string, including terminating
831                  * null. */
832                 opt_ptr += strlen (domain_str) + 1;
833                 opt_len -= strlen (domain_str) + 1;
834
835                 /* Ignore empty domains. They're probably just padding... */
836                 if (domain_str[0] == '\0')
837                         continue;
838
839                 for (i = 0; i < device->dnssl_domains->len; i++) {
840                         cur_domain = &(g_array_index (device->dnssl_domains, NMIP6DNSSL, i));
841
842                         if (strcmp (domain_str, cur_domain->domain) != 0)
843                                 continue;
844
845                         cur_domain->expires = domain.expires;
846
847                         if (domain.expires > 0) {
848                                 nm_log_dbg (LOGD_IP6, "(%s): refreshing RA-provided domain %s (expires in %ld seconds)",
849                                             device->iface, domain_str,
850                                             domain.expires - now);
851                                 break;
852                         }
853
854                         nm_log_dbg (LOGD_IP6, "(%s): removing RA-provided domain %s on router request",
855                                     device->iface, domain_str);
856
857                         g_array_remove_index (device->dnssl_domains, i);
858                         changed = TRUE;
859                         break;
860                 }
861
862                 if (domain.expires == 0)
863                         continue;
864                 if (i < device->dnssl_domains->len)
865                         continue;
866
867                 nm_log_dbg (LOGD_IP6, "(%s): found RA-provided domain %s (expires in %ld seconds)",
868                             device->iface, domain_str, domain.expires - now);
869
870                 g_assert (strlen (domain_str) < sizeof (domain.domain));
871                 strcpy (domain.domain, domain_str);
872                 g_array_append_val (new_domains, domain);
873         }
874
875         /* New domains must be added in the order they are listed in the
876          * RA option and before any existing domains.
877          *
878          * Note: This is the place to remove domains if we want to cap the
879          *       number of domains. The RFC states that the one to expire
880          *       first of the existing domains should be removed.
881          */
882         if (new_domains->len) {
883                 g_array_prepend_vals (device->dnssl_domains,
884                                       new_domains->data, new_domains->len);
885                 changed = TRUE;
886         }
887
888         g_array_free (new_domains, TRUE);
889
890         /* Timeouts may have changed even if domains didn't */
891         set_dnssl_timeout (device);
892
893         return changed;
894 }
895
896 static NMIP6Device *
897 process_nduseropt (NMIP6Manager *manager, struct nl_msg *msg)
898 {
899         NMIP6Device *device;
900         struct nduseroptmsg *ndmsg;
901         struct nd_opt_hdr *opt;
902         guint opts_len;
903         gboolean changed = FALSE;
904
905         nm_log_dbg (LOGD_IP6, "processing netlink nduseropt message");
906
907         ndmsg = (struct nduseroptmsg *) NLMSG_DATA (nlmsg_hdr (msg));
908
909         if (ndmsg->nduseropt_family != AF_INET6 ||
910                 ndmsg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
911                 ndmsg->nduseropt_icmp_code != 0) {
912                 nm_log_dbg (LOGD_IP6, "ignoring non-Router Advertisement message");
913                 return NULL;
914         }
915
916         device = nm_ip6_manager_get_device (manager, ndmsg->nduseropt_ifindex);
917         if (!device) {
918                 nm_log_dbg (LOGD_IP6, "ignoring message for unknown device");
919                 return NULL;
920         }
921
922         opt = (struct nd_opt_hdr *) (ndmsg + 1);
923         opts_len = ndmsg->nduseropt_opts_len;
924
925         while (opts_len >= sizeof (struct nd_opt_hdr)) {
926                 size_t nd_opt_len = opt->nd_opt_len;
927
928                 if (nd_opt_len == 0 || opts_len < (nd_opt_len << 3))
929                         break;
930
931                 switch (opt->nd_opt_type) {
932                 case ND_OPT_RDNSS:
933                         changed = process_nduseropt_rdnss (device, opt);
934                         break;
935                 case ND_OPT_DNSSL:
936                         changed = process_nduseropt_dnssl (device, opt);
937                         break;
938                 }
939
940                 opts_len -= opt->nd_opt_len << 3;
941                 opt = (struct nd_opt_hdr *) ((uint8_t *) opt + (opt->nd_opt_len << 3));
942         }
943
944         if (changed)
945                 return device;
946         else
947                 return NULL;
948 }
949
950 static struct nla_policy link_policy[IFLA_MAX + 1] = {
951         [IFLA_PROTINFO] = { .type = NLA_NESTED },
952 };
953
954 static struct nla_policy link_prot_policy[IFLA_INET6_MAX + 1] = {
955         [IFLA_INET6_FLAGS]      = { .type = NLA_U32 },
956 };
957
958 static NMIP6Device *
959 process_newlink (NMIP6Manager *manager, struct nl_msg *msg)
960 {
961         struct nlmsghdr *hdr = nlmsg_hdr (msg);
962         struct ifinfomsg *ifi;
963         NMIP6Device *device;
964         struct nlattr *tb[IFLA_MAX + 1];
965         struct nlattr *pi[IFLA_INET6_MAX + 1];
966         int err;
967
968         ifi = nlmsg_data (hdr);
969         if (ifi->ifi_family != AF_INET6) {
970                 nm_log_dbg (LOGD_IP6, "ignoring netlink message family %d", ifi->ifi_family);
971                 return NULL;
972         }
973
974         device = nm_ip6_manager_get_device (manager, ifi->ifi_index);
975         if (!device || device->addrconf_complete) {
976                 nm_log_dbg (LOGD_IP6, "(%s): ignoring unknown or completed device",
977                             device ? device->iface : "(none)");
978                 return NULL;
979         }
980
981         /* FIXME: we have to do this manually for now since libnl doesn't yet
982          * support the IFLA_PROTINFO attribute of NEWLINK messages.  When it does,
983          * we can get rid of this function and just grab IFLA_PROTINFO from
984          * nm_ip6_device_sync_from_netlink(), then get the IFLA_INET6_FLAGS out of
985          * the PROTINFO.
986          */
987
988         err = nlmsg_parse (hdr, sizeof (*ifi), tb, IFLA_MAX, link_policy);
989         if (err < 0) {
990                 nm_log_dbg (LOGD_IP6, "(%s): error parsing PROTINFO attribute", device->iface);
991                 return NULL;
992         }
993         if (!tb[IFLA_PROTINFO]) {
994                 nm_log_dbg (LOGD_IP6, "(%s): message had no PROTINFO attribute", device->iface);
995                 return NULL;
996         }
997
998         err = nla_parse_nested (pi, IFLA_INET6_MAX, tb[IFLA_PROTINFO], link_prot_policy);
999         if (err < 0) {
1000                 nm_log_dbg (LOGD_IP6, "(%s): error parsing PROTINFO flags", device->iface);
1001                 return NULL;
1002         }
1003         if (!pi[IFLA_INET6_FLAGS]) {
1004                 nm_log_dbg (LOGD_IP6, "(%s): message had no PROTINFO flags", device->iface);
1005                 return NULL;
1006         }
1007
1008         device->ra_flags = nla_get_u32 (pi[IFLA_INET6_FLAGS]);
1009         nm_log_dbg (LOGD_IP6, "(%s): got IPv6 flags 0x%X", device->iface, device->ra_flags);
1010
1011         return device;
1012 }
1013
1014 static void
1015 netlink_notification (NMNetlinkMonitor *monitor, struct nl_msg *msg, gpointer user_data)
1016 {
1017         NMIP6Manager *manager = (NMIP6Manager *) user_data;
1018         NMIP6Device *device;
1019         struct nlmsghdr *hdr;
1020         gboolean config_changed = FALSE;
1021
1022         hdr = nlmsg_hdr (msg);
1023         nm_log_dbg (LOGD_HW, "netlink notificate type %d", hdr->nlmsg_type);
1024         switch (hdr->nlmsg_type) {
1025         case RTM_NEWADDR:
1026         case RTM_DELADDR:
1027                 device = process_addr (manager, msg);
1028                 config_changed = TRUE;
1029                 break;
1030         case RTM_NEWROUTE:
1031         case RTM_DELROUTE:
1032                 device = process_route (manager, msg);
1033                 config_changed = TRUE;
1034                 break;
1035         case RTM_NEWPREFIX:
1036                 device = process_prefix (manager, msg);
1037                 break;
1038         case RTM_NEWNDUSEROPT:
1039                 device = process_nduseropt (manager, msg);
1040                 config_changed = TRUE;
1041                 break;
1042         case RTM_NEWLINK:
1043                 device = process_newlink (manager, msg);
1044                 config_changed = TRUE;
1045                 break;
1046         default:
1047                 return;
1048         }
1049
1050         if (device) {
1051                 nm_log_dbg (LOGD_IP6, "(%s): syncing device with netlink changes", device->iface);
1052                 nm_ip6_device_sync_from_netlink (device, config_changed);
1053         }
1054 }
1055
1056 gboolean
1057 nm_ip6_manager_prepare_interface (NMIP6Manager *manager,
1058                                   int ifindex,
1059                                   NMSettingIP6Config *s_ip6,
1060                                   const char *accept_ra_path)
1061 {
1062         NMIP6ManagerPrivate *priv;
1063         NMIP6Device *device;
1064         const char *method = NULL;
1065
1066         g_return_val_if_fail (NM_IS_IP6_MANAGER (manager), FALSE);
1067         g_return_val_if_fail (ifindex > 0, FALSE);
1068
1069         priv = NM_IP6_MANAGER_GET_PRIVATE (manager);
1070
1071         device = nm_ip6_device_new (manager, ifindex);
1072         g_return_val_if_fail (device != NULL, FALSE);
1073         g_return_val_if_fail (   strchr (device->iface, '/') == NULL
1074                               && strcmp (device->iface, "all") != 0
1075                               && strcmp (device->iface, "default") != 0,
1076                               FALSE);
1077
1078         if (s_ip6)
1079                 method = nm_setting_ip6_config_get_method (s_ip6);
1080         if (!method)
1081                 method = NM_SETTING_IP6_CONFIG_METHOD_AUTO;
1082
1083         /* Establish target state and turn router advertisement acceptance on or off */
1084         if (!strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL)) {
1085                 device->target_state = NM_IP6_DEVICE_GOT_LINK_LOCAL;
1086                 nm_utils_do_sysctl (accept_ra_path, "0\n");
1087         } else {
1088                 device->target_state = NM_IP6_DEVICE_GOT_ADDRESS;
1089                 nm_utils_do_sysctl (accept_ra_path, "1\n");
1090         }
1091
1092         return TRUE;
1093 }
1094
1095 static gboolean
1096 poll_ip6_flags (gpointer user_data)
1097 {
1098         nm_netlink_monitor_request_ip6_info (NM_NETLINK_MONITOR (user_data), NULL);
1099         return TRUE;
1100 }
1101
1102 void
1103 nm_ip6_manager_begin_addrconf (NMIP6Manager *manager, int ifindex)
1104 {
1105         NMIP6ManagerPrivate *priv;
1106         NMIP6Device *device;
1107         CallbackInfo *info;
1108
1109         g_return_if_fail (NM_IS_IP6_MANAGER (manager));
1110         g_return_if_fail (ifindex > 0);
1111
1112         priv = NM_IP6_MANAGER_GET_PRIVATE (manager);
1113
1114         device = (NMIP6Device *) g_hash_table_lookup (priv->devices, GINT_TO_POINTER (ifindex));
1115         g_return_if_fail (device != NULL);
1116
1117         nm_log_info (LOGD_IP6, "Activation (%s) Beginning IP6 addrconf.", device->iface);
1118
1119         device->addrconf_complete = FALSE;
1120         device->ra_flags = 0;
1121
1122         /* Set up a timeout on the transaction to kill it after the timeout */
1123         info = callback_info_new (device, 0, FALSE);
1124         device->finish_addrconf_id = g_timeout_add_seconds_full (G_PRIORITY_DEFAULT,
1125                                                                  NM_IP6_TIMEOUT,
1126                                                                  finish_addrconf,
1127                                                                  info,
1128                                                                  (GDestroyNotify) g_free);
1129
1130         /* Bounce IPv6 on the interface to ensure the kernel will start looking for
1131          * new RAs; there doesn't seem to be a better way to do this right now.
1132          */
1133         if (device->target_state >= NM_IP6_DEVICE_GOT_ADDRESS) {
1134                 nm_utils_do_sysctl (device->disable_ip6_path, "1\n");
1135                 g_usleep (200);
1136                 nm_utils_do_sysctl (device->disable_ip6_path, "0\n");
1137         }
1138
1139         device->ip6flags_poll_id = g_timeout_add_seconds (1, poll_ip6_flags, priv->monitor);
1140
1141         /* Kick off the initial IPv6 flags request */
1142         nm_netlink_monitor_request_ip6_info (priv->monitor, NULL);
1143
1144         /* Sync flags, etc, from netlink; this will also notice if the
1145          * device is already fully configured and schedule the
1146          * ADDRCONF_COMPLETE signal in that case.
1147          */
1148         nm_ip6_device_sync_from_netlink (device, FALSE);
1149 }
1150
1151 void
1152 nm_ip6_manager_cancel_addrconf (NMIP6Manager *manager, int ifindex)
1153 {
1154         g_return_if_fail (NM_IS_IP6_MANAGER (manager));
1155         g_return_if_fail (ifindex > 0);
1156
1157         g_hash_table_remove (NM_IP6_MANAGER_GET_PRIVATE (manager)->devices,
1158                              GINT_TO_POINTER (ifindex));
1159 }
1160
1161 #define FIRST_ROUTE(m) ((struct rtnl_route *) nl_cache_get_first (m))
1162 #define NEXT_ROUTE(m) ((struct rtnl_route *) nl_cache_get_next ((struct nl_object *) m))
1163
1164 #define FIRST_ADDR(m) ((struct rtnl_addr *) nl_cache_get_first (m))
1165 #define NEXT_ADDR(m) ((struct rtnl_addr *) nl_cache_get_next ((struct nl_object *) m))
1166
1167 NMIP6Config *
1168 nm_ip6_manager_get_ip6_config (NMIP6Manager *manager, int ifindex)
1169 {
1170         NMIP6ManagerPrivate *priv;
1171         NMIP6Device *device;
1172         NMIP6Config *config;
1173         struct rtnl_addr *rtnladdr;
1174         struct nl_addr *nladdr;
1175         struct in6_addr *addr;
1176         NMIP6Address *ip6addr;
1177         struct rtnl_route *rtnlroute;
1178         struct nl_addr *nldest, *nlgateway;
1179         struct in6_addr *dest, *gateway;
1180         gboolean defgw_set = FALSE;
1181         struct in6_addr defgw;
1182         uint32_t metric;
1183         NMIP6Route *ip6route;
1184         int i;
1185
1186         g_return_val_if_fail (NM_IS_IP6_MANAGER (manager), NULL);
1187         g_return_val_if_fail (ifindex > 0, NULL);
1188
1189         priv = NM_IP6_MANAGER_GET_PRIVATE (manager);
1190
1191         device = (NMIP6Device *) g_hash_table_lookup (priv->devices,
1192                                                       GINT_TO_POINTER (ifindex));
1193         if (!device) {
1194                 nm_log_warn (LOGD_IP6, "(%d): addrconf not started.", ifindex);
1195                 return NULL;
1196         }
1197
1198         config = nm_ip6_config_new ();
1199         if (!config) {
1200                 nm_log_err (LOGD_IP6, "(%s): out of memory creating IP6 config object.",
1201                             device->iface);
1202                 return NULL;
1203         }
1204
1205         /* Make sure we refill the route and address caches, otherwise we won't get
1206          * up-to-date information here since the netlink route/addr change messages
1207          * may be lagging a bit.
1208          */
1209         nl_cache_refill (priv->nlh, priv->route_cache);
1210         nl_cache_refill (priv->nlh, priv->addr_cache);
1211
1212         /* Add routes */
1213         for (rtnlroute = FIRST_ROUTE (priv->route_cache); rtnlroute; rtnlroute = NEXT_ROUTE (rtnlroute)) {
1214                 /* Make sure it's an IPv6 route for this device */
1215                 if (rtnl_route_get_oif (rtnlroute) != device->ifindex)
1216                         continue;
1217                 if (rtnl_route_get_family (rtnlroute) != AF_INET6)
1218                         continue;
1219
1220                 nldest = rtnl_route_get_dst (rtnlroute);
1221                 if (!nldest || nl_addr_get_family (nldest) != AF_INET6)
1222                         continue;
1223                 dest = nl_addr_get_binary_addr (nldest);
1224
1225                 nlgateway = rtnl_route_get_gateway (rtnlroute);
1226                 if (!nlgateway || nl_addr_get_family (nlgateway) != AF_INET6)
1227                         continue;
1228                 gateway = nl_addr_get_binary_addr (nlgateway);
1229
1230                 if (rtnl_route_get_dst_len (rtnlroute) == 0) {
1231                         /* Default gateway route; don't add to normal routes but to each address */
1232                         if (!defgw_set) {
1233                                 memcpy (&defgw, gateway, sizeof (defgw));
1234                                 defgw_set = TRUE;
1235                         }
1236                         continue;
1237                 }
1238
1239                 /* Also ignore link-local routes where the destination and gateway are
1240                  * the same, which apparently get added by the kernel but return -EINVAL
1241                  * when we try to add them via netlink.
1242                  */
1243                 if (gateway && IN6_ARE_ADDR_EQUAL (dest, gateway))
1244                         continue;
1245
1246                 ip6route = nm_ip6_route_new ();
1247                 nm_ip6_route_set_dest (ip6route, dest);
1248                 nm_ip6_route_set_prefix (ip6route, rtnl_route_get_dst_len (rtnlroute));
1249                 nm_ip6_route_set_next_hop (ip6route, gateway);
1250                 metric = rtnl_route_get_metric (rtnlroute, 1);
1251                 if (metric != UINT_MAX)
1252                         nm_ip6_route_set_metric (ip6route, metric);
1253                 nm_ip6_config_take_route (config, ip6route);
1254         }
1255
1256         /* Add addresses */
1257         for (rtnladdr = FIRST_ADDR (priv->addr_cache); rtnladdr; rtnladdr = NEXT_ADDR (rtnladdr)) {
1258                 if (rtnl_addr_get_ifindex (rtnladdr) != device->ifindex)
1259                         continue;
1260
1261                 nladdr = rtnl_addr_get_local (rtnladdr);
1262                 if (!nladdr || nl_addr_get_family (nladdr) != AF_INET6)
1263                         continue;
1264
1265                 addr = nl_addr_get_binary_addr (nladdr);
1266                 ip6addr = nm_ip6_address_new ();
1267                 nm_ip6_address_set_prefix (ip6addr, rtnl_addr_get_prefixlen (rtnladdr));
1268                 nm_ip6_address_set_address (ip6addr, addr);
1269                 nm_ip6_config_take_address (config, ip6addr);
1270                 if (defgw_set)
1271                         nm_ip6_address_set_gateway (ip6addr, &defgw);
1272         }
1273
1274         /* Add DNS servers */
1275         if (device->rdnss_servers) {
1276                 NMIP6RDNSS *rdnss = (NMIP6RDNSS *)(device->rdnss_servers->data);
1277
1278                 for (i = 0; i < device->rdnss_servers->len; i++)
1279                         nm_ip6_config_add_nameserver (config, &rdnss[i].addr);
1280         }
1281
1282         /* Add DNS domains */
1283         if (device->dnssl_domains) {
1284                 NMIP6DNSSL *dnssl = (NMIP6DNSSL *)(device->dnssl_domains->data);
1285
1286                 for (i = 0; i < device->dnssl_domains->len; i++)
1287                         nm_ip6_config_add_domain (config, dnssl[i].domain);
1288         }
1289
1290         return config;
1291 }
1292
1293 /******************************************************************/
1294
1295 static NMIP6Manager *
1296 nm_ip6_manager_new (void)
1297 {
1298         NMIP6Manager *manager;
1299         NMIP6ManagerPrivate *priv;
1300
1301         manager = g_object_new (NM_TYPE_IP6_MANAGER, NULL);
1302         priv = NM_IP6_MANAGER_GET_PRIVATE (manager);
1303
1304         if (!priv->devices) {
1305                 nm_log_err (LOGD_IP6, "not enough memory to initialize IP6 manager tables");
1306                 g_object_unref (manager);
1307                 manager = NULL;
1308         }
1309
1310         return manager;
1311 }
1312
1313 static NMIP6Manager *singleton = NULL;
1314
1315 NMIP6Manager *
1316 nm_ip6_manager_get (void)
1317 {
1318         if (!singleton) {
1319                 singleton = nm_ip6_manager_new ();
1320                 g_assert (singleton);
1321         } else
1322                 g_object_ref (singleton);
1323
1324         return singleton;
1325 }
1326
1327 static void
1328 nm_ip6_manager_init (NMIP6Manager *manager)
1329 {
1330         NMIP6ManagerPrivate *priv = NM_IP6_MANAGER_GET_PRIVATE (manager);
1331
1332         priv->devices = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1333                                                NULL,
1334                                                (GDestroyNotify) nm_ip6_device_destroy);
1335
1336         priv->monitor = nm_netlink_monitor_get ();
1337         nm_netlink_monitor_subscribe (priv->monitor, RTNLGRP_IPV6_IFADDR, NULL);
1338         nm_netlink_monitor_subscribe (priv->monitor, RTNLGRP_IPV6_PREFIX, NULL);
1339         nm_netlink_monitor_subscribe (priv->monitor, RTNLGRP_ND_USEROPT, NULL);
1340         nm_netlink_monitor_subscribe (priv->monitor, RTNLGRP_LINK, NULL);
1341
1342         priv->netlink_id = g_signal_connect (priv->monitor, "notification",
1343                                              G_CALLBACK (netlink_notification), manager);
1344
1345         priv->nlh = nm_netlink_get_default_handle ();
1346         priv->addr_cache = rtnl_addr_alloc_cache (priv->nlh);
1347         priv->route_cache = rtnl_route_alloc_cache (priv->nlh);
1348 }
1349
1350 static void
1351 finalize (GObject *object)
1352 {
1353         NMIP6ManagerPrivate *priv = NM_IP6_MANAGER_GET_PRIVATE (object);
1354
1355         g_signal_handler_disconnect (priv->monitor, priv->netlink_id);
1356
1357         g_hash_table_destroy (priv->devices);
1358         g_object_unref (priv->monitor);
1359         nl_cache_free (priv->addr_cache);
1360         nl_cache_free (priv->route_cache);
1361
1362         singleton = NULL;
1363
1364         G_OBJECT_CLASS (nm_ip6_manager_parent_class)->finalize (object);
1365 }
1366
1367 static void
1368 nm_ip6_manager_class_init (NMIP6ManagerClass *manager_class)
1369 {
1370         GObjectClass *object_class = G_OBJECT_CLASS (manager_class);
1371
1372         g_type_class_add_private (manager_class, sizeof (NMIP6ManagerPrivate));
1373
1374         /* virtual methods */
1375         object_class->finalize = finalize;
1376
1377         /* signals */
1378         signals[ADDRCONF_COMPLETE] =
1379                 g_signal_new ("addrconf-complete",
1380                                           G_OBJECT_CLASS_TYPE (object_class),
1381                                           G_SIGNAL_RUN_FIRST,
1382                                           G_STRUCT_OFFSET (NMIP6ManagerClass, addrconf_complete),
1383                                           NULL, NULL,
1384                                           _nm_marshal_VOID__INT_UINT_BOOLEAN,
1385                                           G_TYPE_NONE, 3, G_TYPE_INT, G_TYPE_UINT, G_TYPE_BOOLEAN);
1386
1387         signals[CONFIG_CHANGED] =
1388                 g_signal_new ("config-changed",
1389                                           G_OBJECT_CLASS_TYPE (object_class),
1390                                           G_SIGNAL_RUN_FIRST,
1391                                           G_STRUCT_OFFSET (NMIP6ManagerClass, config_changed),
1392                                           NULL, NULL,
1393                                           _nm_marshal_VOID__INT_UINT_BOOLEAN,
1394                                           G_TYPE_NONE, 3, G_TYPE_INT, G_TYPE_UINT, G_TYPE_BOOLEAN);
1395 }
1396