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