logging: fix format string/argument disagreement
[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 };
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                         strcpy(buf, "[invalid]");
694
695                 for (i = 0; i < device->rdnss_servers->len; i++) {
696                         cur_server = &(g_array_index (device->rdnss_servers, NMIP6RDNSS, i));
697
698                         if (!IN6_ARE_ADDR_EQUAL (addr, &cur_server->addr))
699                                 continue;
700
701                         cur_server->expires = server.expires;
702
703                         if (server.expires > 0) {
704                                 nm_log_dbg (LOGD_IP6, "(%s): refreshing RA-provided nameserver %s (expires in %zd seconds)",
705                                             device->iface, buf,
706                                             server.expires - now);
707                                 break;
708                         }
709
710                         nm_log_dbg (LOGD_IP6, "(%s): removing RA-provided nameserver %s on router request",
711                                     device->iface, buf);
712
713                         g_array_remove_index (device->rdnss_servers, i);
714                         changed = TRUE;
715                         break;
716                 }
717
718                 if (server.expires == 0)
719                         continue;
720                 if (i < device->rdnss_servers->len)
721                         continue;
722
723                 nm_log_dbg (LOGD_IP6, "(%s): found RA-provided nameserver %s (expires in %zd seconds)",
724                             device->iface, buf, server.expires - now);
725
726                 server.addr = *addr;
727                 g_array_append_val (new_servers, server);
728         }
729
730         /* New servers must be added in the order they are listed in the
731          * RA option and before any existing servers.
732          *
733          * Note: This is the place to remove servers if we want to cap the
734          *       number of resolvers. The RFC states that the one to expire
735          *       first of the existing servers should be removed.
736          */
737         if (new_servers->len) {
738                 g_array_prepend_vals (device->rdnss_servers,
739                                       new_servers->data, new_servers->len);
740                 changed = TRUE;
741         }
742
743         g_array_free (new_servers, TRUE);
744
745         /* Timeouts may have changed even if IPs didn't */
746         set_rdnss_timeout (device);
747
748         return changed;
749 }
750
751 static const char *
752 parse_dnssl_domain (const unsigned char *buffer, size_t maxlen)
753 {
754         static char domain[256];
755         size_t label_len;
756
757         domain[0] = '\0';
758
759         while (maxlen > 0) {
760                 label_len = *buffer;
761                 buffer++;
762                 maxlen--;
763
764                 if (label_len == 0)
765                         return domain;
766
767                 if (label_len > maxlen)
768                         return NULL;
769                 if ((sizeof (domain) - strlen (domain)) < (label_len + 2))
770                         return NULL;
771
772                 if (domain[0] != '\0')
773                         strcat (domain, ".");
774                 strncat (domain, (const char *)buffer, label_len);
775                 buffer += label_len;
776                 maxlen -= label_len;
777         }
778
779         return NULL;
780 }
781
782 static gboolean
783 process_nduseropt_dnssl (NMIP6Device *device, struct nd_opt_hdr *opt)
784 {
785         size_t opt_len;
786         struct nd_opt_dnssl *dnssl_opt;
787         unsigned char *opt_ptr;
788         time_t now = time (NULL);
789         GArray *new_domains;
790         NMIP6DNSSL domain, *cur_domain;
791         gboolean changed;
792         guint i;
793
794         opt_len = opt->nd_opt_len;
795
796         if (opt_len < 2)
797                 return FALSE;
798
799         dnssl_opt = (struct nd_opt_dnssl *) opt;
800
801         opt_ptr = (unsigned char *)(dnssl_opt + 1);
802         opt_len = (opt_len - 1) * 8; /* prefer bytes for later handling */
803
804         new_domains = g_array_new (FALSE, FALSE, sizeof (NMIP6DNSSL));
805
806         changed = FALSE;
807
808         /* Pad the DNS server expiry somewhat to give a bit of slack in cases
809          * where one RA gets lost or something (which can happen on unreliable
810          * links like wifi where certain types of frames are not retransmitted).
811          * Note that 0 has special meaning and is therefore not adjusted.
812          */
813         domain.expires = ntohl (dnssl_opt->nd_opt_dnssl_lifetime);
814         if (domain.expires > 0)
815                 domain.expires += now + 10;
816
817         while (opt_len) {
818                 const char *domain_str;
819
820                 domain_str = parse_dnssl_domain (opt_ptr, opt_len);
821                 if (domain_str == NULL) {
822                         nm_log_dbg (LOGD_IP6, "(%s): invalid DNSSL option, parsing aborted",
823                                     device->iface);
824                         break;
825                 }
826
827                 /* The DNSSL encoding of domains happen to occupy the same size
828                  * as the length of the resulting string, including terminating
829                  * null. */
830                 opt_ptr += strlen (domain_str) + 1;
831                 opt_len -= strlen (domain_str) + 1;
832
833                 /* Ignore empty domains. They're probably just padding... */
834                 if (domain_str[0] == '\0')
835                         continue;
836
837                 for (i = 0; i < device->dnssl_domains->len; i++) {
838                         cur_domain = &(g_array_index (device->dnssl_domains, NMIP6DNSSL, i));
839
840                         if (strcmp (domain_str, cur_domain->domain) != 0)
841                                 continue;
842
843                         cur_domain->expires = domain.expires;
844
845                         if (domain.expires > 0) {
846                                 nm_log_dbg (LOGD_IP6, "(%s): refreshing RA-provided domain %s (expires in %zd seconds)",
847                                             device->iface, domain_str,
848                                             domain.expires - now);
849                                 break;
850                         }
851
852                         nm_log_dbg (LOGD_IP6, "(%s): removing RA-provided domain %s on router request",
853                                     device->iface, domain_str);
854
855                         g_array_remove_index (device->dnssl_domains, i);
856                         changed = TRUE;
857                         break;
858                 }
859
860                 if (domain.expires == 0)
861                         continue;
862                 if (i < device->dnssl_domains->len)
863                         continue;
864
865                 nm_log_dbg (LOGD_IP6, "(%s): found RA-provided domain %s (expires in %zd seconds)",
866                             device->iface, domain_str, domain.expires - now);
867
868                 g_assert (strlen (domain_str) < sizeof (domain.domain));
869                 strcpy (domain.domain, domain_str);
870                 g_array_append_val (new_domains, domain);
871         }
872
873         /* New domains must be added in the order they are listed in the
874          * RA option and before any existing domains.
875          *
876          * Note: This is the place to remove domains if we want to cap the
877          *       number of domains. The RFC states that the one to expire
878          *       first of the existing domains should be removed.
879          */
880         if (new_domains->len) {
881                 g_array_prepend_vals (device->dnssl_domains,
882                                       new_domains->data, new_domains->len);
883                 changed = TRUE;
884         }
885
886         g_array_free (new_domains, TRUE);
887
888         /* Timeouts may have changed even if domains didn't */
889         set_dnssl_timeout (device);
890
891         return changed;
892 }
893
894 static NMIP6Device *
895 process_nduseropt (NMIP6Manager *manager, struct nl_msg *msg)
896 {
897         NMIP6Device *device;
898         struct nduseroptmsg *ndmsg;
899         struct nd_opt_hdr *opt;
900         guint opts_len;
901         gboolean changed = FALSE;
902
903         nm_log_dbg (LOGD_IP6, "processing netlink nduseropt message");
904
905         ndmsg = (struct nduseroptmsg *) NLMSG_DATA (nlmsg_hdr (msg));
906
907         if (ndmsg->nduseropt_family != AF_INET6 ||
908                 ndmsg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
909                 ndmsg->nduseropt_icmp_code != 0) {
910                 nm_log_dbg (LOGD_IP6, "ignoring non-Router Advertisement message");
911                 return NULL;
912         }
913
914         device = nm_ip6_manager_get_device (manager, ndmsg->nduseropt_ifindex);
915         if (!device) {
916                 nm_log_dbg (LOGD_IP6, "ignoring message for unknown device");
917                 return NULL;
918         }
919
920         opt = (struct nd_opt_hdr *) (ndmsg + 1);
921         opts_len = ndmsg->nduseropt_opts_len;
922
923         while (opts_len >= sizeof (struct nd_opt_hdr)) {
924                 size_t nd_opt_len = opt->nd_opt_len;
925
926                 if (nd_opt_len == 0 || opts_len < (nd_opt_len << 3))
927                         break;
928
929                 switch (opt->nd_opt_type) {
930                 case ND_OPT_RDNSS:
931                         changed = process_nduseropt_rdnss (device, opt);
932                         break;
933                 case ND_OPT_DNSSL:
934                         changed = process_nduseropt_dnssl (device, opt);
935                         break;
936                 }
937
938                 opts_len -= opt->nd_opt_len << 3;
939                 opt = (struct nd_opt_hdr *) ((uint8_t *) opt + (opt->nd_opt_len << 3));
940         }
941
942         if (changed)
943                 return device;
944         else
945                 return NULL;
946 }
947
948 static struct nla_policy link_policy[IFLA_MAX + 1] = {
949         [IFLA_PROTINFO] = { .type = NLA_NESTED },
950 };
951
952 static struct nla_policy link_prot_policy[IFLA_INET6_MAX + 1] = {
953         [IFLA_INET6_FLAGS]      = { .type = NLA_U32 },
954 };
955
956 static NMIP6Device *
957 process_newlink (NMIP6Manager *manager, struct nl_msg *msg)
958 {
959         struct nlmsghdr *hdr = nlmsg_hdr (msg);
960         struct ifinfomsg *ifi;
961         NMIP6Device *device;
962         struct nlattr *tb[IFLA_MAX + 1];
963         struct nlattr *pi[IFLA_INET6_MAX + 1];
964         int err;
965
966         ifi = nlmsg_data (hdr);
967         if (ifi->ifi_family != AF_INET6) {
968                 nm_log_dbg (LOGD_IP6, "ignoring netlink message family %d", ifi->ifi_family);
969                 return NULL;
970         }
971
972         device = nm_ip6_manager_get_device (manager, ifi->ifi_index);
973         if (!device || device->addrconf_complete) {
974                 nm_log_dbg (LOGD_IP6, "(%s): ignoring unknown or completed device",
975                             device ? device->iface : "(none)");
976                 return NULL;
977         }
978
979         /* FIXME: we have to do this manually for now since libnl doesn't yet
980          * support the IFLA_PROTINFO attribute of NEWLINK messages.  When it does,
981          * we can get rid of this function and just grab IFLA_PROTINFO from
982          * nm_ip6_device_sync_from_netlink(), then get the IFLA_INET6_FLAGS out of
983          * the PROTINFO.
984          */
985
986         err = nlmsg_parse (hdr, sizeof (*ifi), tb, IFLA_MAX, link_policy);
987         if (err < 0) {
988                 nm_log_dbg (LOGD_IP6, "(%s): error parsing PROTINFO attribute", device->iface);
989                 return NULL;
990         }
991         if (!tb[IFLA_PROTINFO]) {
992                 nm_log_dbg (LOGD_IP6, "(%s): message had no PROTINFO attribute", device->iface);
993                 return NULL;
994         }
995
996         err = nla_parse_nested (pi, IFLA_INET6_MAX, tb[IFLA_PROTINFO], link_prot_policy);
997         if (err < 0) {
998                 nm_log_dbg (LOGD_IP6, "(%s): error parsing PROTINFO flags", device->iface);
999                 return NULL;
1000         }
1001         if (!pi[IFLA_INET6_FLAGS]) {
1002                 nm_log_dbg (LOGD_IP6, "(%s): message had no PROTINFO flags", device->iface);
1003                 return NULL;
1004         }
1005
1006         device->ra_flags = nla_get_u32 (pi[IFLA_INET6_FLAGS]);
1007         nm_log_dbg (LOGD_IP6, "(%s): got IPv6 flags 0x%X", device->iface, device->ra_flags);
1008
1009         return device;
1010 }
1011
1012 static void
1013 netlink_notification (NMNetlinkMonitor *monitor, struct nl_msg *msg, gpointer user_data)
1014 {
1015         NMIP6Manager *manager = (NMIP6Manager *) user_data;
1016         NMIP6Device *device;
1017         struct nlmsghdr *hdr;
1018         gboolean config_changed = FALSE;
1019
1020         hdr = nlmsg_hdr (msg);
1021         nm_log_dbg (LOGD_HW, "netlink notificate type %d", hdr->nlmsg_type);
1022         switch (hdr->nlmsg_type) {
1023         case RTM_NEWADDR:
1024         case RTM_DELADDR:
1025                 device = process_addr (manager, msg);
1026                 config_changed = TRUE;
1027                 break;
1028         case RTM_NEWROUTE:
1029         case RTM_DELROUTE:
1030                 device = process_route (manager, msg);
1031                 config_changed = TRUE;
1032                 break;
1033         case RTM_NEWPREFIX:
1034                 device = process_prefix (manager, msg);
1035                 break;
1036         case RTM_NEWNDUSEROPT:
1037                 device = process_nduseropt (manager, msg);
1038                 config_changed = TRUE;
1039                 break;
1040         case RTM_NEWLINK:
1041                 device = process_newlink (manager, msg);
1042                 config_changed = TRUE;
1043                 break;
1044         default:
1045                 return;
1046         }
1047
1048         if (device) {
1049                 nm_log_dbg (LOGD_IP6, "(%s): syncing device with netlink changes", device->iface);
1050                 nm_ip6_device_sync_from_netlink (device, config_changed);
1051         }
1052 }
1053
1054 gboolean
1055 nm_ip6_manager_prepare_interface (NMIP6Manager *manager,
1056                                   int ifindex,
1057                                   NMSettingIP6Config *s_ip6,
1058                                   const char *accept_ra_path)
1059 {
1060         NMIP6ManagerPrivate *priv;
1061         NMIP6Device *device;
1062         const char *method = NULL;
1063
1064         g_return_val_if_fail (NM_IS_IP6_MANAGER (manager), FALSE);
1065         g_return_val_if_fail (ifindex > 0, FALSE);
1066
1067         priv = NM_IP6_MANAGER_GET_PRIVATE (manager);
1068
1069         device = nm_ip6_device_new (manager, ifindex);
1070         g_return_val_if_fail (device != NULL, FALSE);
1071         g_return_val_if_fail (   strchr (device->iface, '/') == NULL
1072                               && strcmp (device->iface, "all") != 0
1073                               && strcmp (device->iface, "default") != 0,
1074                               FALSE);
1075
1076         if (s_ip6)
1077                 method = nm_setting_ip6_config_get_method (s_ip6);
1078         if (!method)
1079                 method = NM_SETTING_IP6_CONFIG_METHOD_AUTO;
1080
1081         /* Establish target state and turn router advertisement acceptance on or off */
1082         if (!strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL)) {
1083                 device->target_state = NM_IP6_DEVICE_GOT_LINK_LOCAL;
1084                 nm_utils_do_sysctl (accept_ra_path, "0\n");
1085         } else {
1086                 device->target_state = NM_IP6_DEVICE_GOT_ADDRESS;
1087                 nm_utils_do_sysctl (accept_ra_path, "1\n");
1088         }
1089
1090         return TRUE;
1091 }
1092
1093 static gboolean
1094 poll_ip6_flags (gpointer user_data)
1095 {
1096         nm_netlink_monitor_request_ip6_info (NM_NETLINK_MONITOR (user_data), NULL);
1097         return TRUE;
1098 }
1099
1100 void
1101 nm_ip6_manager_begin_addrconf (NMIP6Manager *manager, int ifindex)
1102 {
1103         NMIP6ManagerPrivate *priv;
1104         NMIP6Device *device;
1105         CallbackInfo *info;
1106
1107         g_return_if_fail (NM_IS_IP6_MANAGER (manager));
1108         g_return_if_fail (ifindex > 0);
1109
1110         priv = NM_IP6_MANAGER_GET_PRIVATE (manager);
1111
1112         device = (NMIP6Device *) g_hash_table_lookup (priv->devices, GINT_TO_POINTER (ifindex));
1113         g_return_if_fail (device != NULL);
1114
1115         nm_log_info (LOGD_IP6, "Activation (%s) Beginning IP6 addrconf.", device->iface);
1116
1117         device->addrconf_complete = FALSE;
1118         device->ra_flags = 0;
1119
1120         /* Set up a timeout on the transaction to kill it after the timeout */
1121         info = callback_info_new (device, 0, FALSE);
1122         device->finish_addrconf_id = g_timeout_add_seconds_full (G_PRIORITY_DEFAULT,
1123                                                                  NM_IP6_TIMEOUT,
1124                                                                  finish_addrconf,
1125                                                                  info,
1126                                                                  (GDestroyNotify) g_free);
1127
1128         /* Bounce IPv6 on the interface to ensure the kernel will start looking for
1129          * new RAs; there doesn't seem to be a better way to do this right now.
1130          */
1131         if (device->target_state >= NM_IP6_DEVICE_GOT_ADDRESS) {
1132                 nm_utils_do_sysctl (device->disable_ip6_path, "1\n");
1133                 g_usleep (200);
1134                 nm_utils_do_sysctl (device->disable_ip6_path, "0\n");
1135         }
1136
1137         device->ip6flags_poll_id = g_timeout_add_seconds (1, poll_ip6_flags, priv->monitor);
1138
1139         /* Kick off the initial IPv6 flags request */
1140         nm_netlink_monitor_request_ip6_info (priv->monitor, NULL);
1141
1142         /* Sync flags, etc, from netlink; this will also notice if the
1143          * device is already fully configured and schedule the
1144          * ADDRCONF_COMPLETE signal in that case.
1145          */
1146         nm_ip6_device_sync_from_netlink (device, FALSE);
1147 }
1148
1149 void
1150 nm_ip6_manager_cancel_addrconf (NMIP6Manager *manager, int ifindex)
1151 {
1152         g_return_if_fail (NM_IS_IP6_MANAGER (manager));
1153         g_return_if_fail (ifindex > 0);
1154
1155         g_hash_table_remove (NM_IP6_MANAGER_GET_PRIVATE (manager)->devices,
1156                              GINT_TO_POINTER (ifindex));
1157 }
1158
1159 #define FIRST_ROUTE(m) ((struct rtnl_route *) nl_cache_get_first (m))
1160 #define NEXT_ROUTE(m) ((struct rtnl_route *) nl_cache_get_next ((struct nl_object *) m))
1161
1162 #define FIRST_ADDR(m) ((struct rtnl_addr *) nl_cache_get_first (m))
1163 #define NEXT_ADDR(m) ((struct rtnl_addr *) nl_cache_get_next ((struct nl_object *) m))
1164
1165 NMIP6Config *
1166 nm_ip6_manager_get_ip6_config (NMIP6Manager *manager, int ifindex)
1167 {
1168         NMIP6ManagerPrivate *priv;
1169         NMIP6Device *device;
1170         NMIP6Config *config;
1171         struct rtnl_addr *rtnladdr;
1172         struct nl_addr *nladdr;
1173         struct in6_addr *addr;
1174         NMIP6Address *ip6addr;
1175         struct rtnl_route *rtnlroute;
1176         struct nl_addr *nldest, *nlgateway;
1177         struct in6_addr *dest, *gateway;
1178         gboolean defgw_set = FALSE;
1179         struct in6_addr defgw;
1180         uint32_t metric;
1181         NMIP6Route *ip6route;
1182         int i;
1183
1184         g_return_val_if_fail (NM_IS_IP6_MANAGER (manager), NULL);
1185         g_return_val_if_fail (ifindex > 0, NULL);
1186
1187         priv = NM_IP6_MANAGER_GET_PRIVATE (manager);
1188
1189         device = (NMIP6Device *) g_hash_table_lookup (priv->devices,
1190                                                       GINT_TO_POINTER (ifindex));
1191         if (!device) {
1192                 nm_log_warn (LOGD_IP6, "(%d): addrconf not started.", ifindex);
1193                 return NULL;
1194         }
1195
1196         config = nm_ip6_config_new ();
1197         if (!config) {
1198                 nm_log_err (LOGD_IP6, "(%s): out of memory creating IP6 config object.",
1199                             device->iface);
1200                 return NULL;
1201         }
1202
1203         /* Make sure we refill the route and address caches, otherwise we won't get
1204          * up-to-date information here since the netlink route/addr change messages
1205          * may be lagging a bit.
1206          */
1207         nl_cache_refill (priv->nlh, priv->route_cache);
1208         nl_cache_refill (priv->nlh, priv->addr_cache);
1209
1210         /* Add routes */
1211         for (rtnlroute = FIRST_ROUTE (priv->route_cache); rtnlroute; rtnlroute = NEXT_ROUTE (rtnlroute)) {
1212                 /* Make sure it's an IPv6 route for this device */
1213                 if (rtnl_route_get_oif (rtnlroute) != device->ifindex)
1214                         continue;
1215                 if (rtnl_route_get_family (rtnlroute) != AF_INET6)
1216                         continue;
1217
1218                 nldest = rtnl_route_get_dst (rtnlroute);
1219                 if (!nldest || nl_addr_get_family (nldest) != AF_INET6)
1220                         continue;
1221                 dest = nl_addr_get_binary_addr (nldest);
1222
1223                 nlgateway = rtnl_route_get_gateway (rtnlroute);
1224                 if (!nlgateway || nl_addr_get_family (nlgateway) != AF_INET6)
1225                         continue;
1226                 gateway = nl_addr_get_binary_addr (nlgateway);
1227
1228                 if (rtnl_route_get_dst_len (rtnlroute) == 0) {
1229                         /* Default gateway route; don't add to normal routes but to each address */
1230                         if (!defgw_set) {
1231                                 memcpy (&defgw, gateway, sizeof (defgw));
1232                                 defgw_set = TRUE;
1233                         }
1234                         continue;
1235                 }
1236
1237                 /* Also ignore link-local routes where the destination and gateway are
1238                  * the same, which apparently get added by the kernel but return -EINVAL
1239                  * when we try to add them via netlink.
1240                  */
1241                 if (gateway && IN6_ARE_ADDR_EQUAL (dest, gateway))
1242                         continue;
1243
1244                 ip6route = nm_ip6_route_new ();
1245                 nm_ip6_route_set_dest (ip6route, dest);
1246                 nm_ip6_route_set_prefix (ip6route, rtnl_route_get_dst_len (rtnlroute));
1247                 nm_ip6_route_set_next_hop (ip6route, gateway);
1248                 metric = rtnl_route_get_metric (rtnlroute, 1);
1249                 if (metric != UINT_MAX)
1250                         nm_ip6_route_set_metric (ip6route, metric);
1251                 nm_ip6_config_take_route (config, ip6route);
1252         }
1253
1254         /* Add addresses */
1255         for (rtnladdr = FIRST_ADDR (priv->addr_cache); rtnladdr; rtnladdr = NEXT_ADDR (rtnladdr)) {
1256                 if (rtnl_addr_get_ifindex (rtnladdr) != device->ifindex)
1257                         continue;
1258
1259                 nladdr = rtnl_addr_get_local (rtnladdr);
1260                 if (!nladdr || nl_addr_get_family (nladdr) != AF_INET6)
1261                         continue;
1262
1263                 addr = nl_addr_get_binary_addr (nladdr);
1264                 ip6addr = nm_ip6_address_new ();
1265                 nm_ip6_address_set_prefix (ip6addr, rtnl_addr_get_prefixlen (rtnladdr));
1266                 nm_ip6_address_set_address (ip6addr, addr);
1267                 nm_ip6_config_take_address (config, ip6addr);
1268                 if (defgw_set)
1269                         nm_ip6_address_set_gateway (ip6addr, &defgw);
1270         }
1271
1272         /* Add DNS servers */
1273         if (device->rdnss_servers) {
1274                 NMIP6RDNSS *rdnss = (NMIP6RDNSS *)(device->rdnss_servers->data);
1275
1276                 for (i = 0; i < device->rdnss_servers->len; i++)
1277                         nm_ip6_config_add_nameserver (config, &rdnss[i].addr);
1278         }
1279
1280         /* Add DNS domains */
1281         if (device->dnssl_domains) {
1282                 NMIP6DNSSL *dnssl = (NMIP6DNSSL *)(device->dnssl_domains->data);
1283
1284                 for (i = 0; i < device->dnssl_domains->len; i++)
1285                         nm_ip6_config_add_domain (config, dnssl[i].domain);
1286         }
1287
1288         return config;
1289 }
1290
1291 /******************************************************************/
1292
1293 static NMIP6Manager *
1294 nm_ip6_manager_new (void)
1295 {
1296         NMIP6Manager *manager;
1297         NMIP6ManagerPrivate *priv;
1298
1299         manager = g_object_new (NM_TYPE_IP6_MANAGER, NULL);
1300         priv = NM_IP6_MANAGER_GET_PRIVATE (manager);
1301
1302         if (!priv->devices) {
1303                 nm_log_err (LOGD_IP6, "not enough memory to initialize IP6 manager tables");
1304                 g_object_unref (manager);
1305                 manager = NULL;
1306         }
1307
1308         return manager;
1309 }
1310
1311 static NMIP6Manager *singleton = NULL;
1312
1313 NMIP6Manager *
1314 nm_ip6_manager_get (void)
1315 {
1316         if (!singleton) {
1317                 singleton = nm_ip6_manager_new ();
1318                 g_assert (singleton);
1319         } else
1320                 g_object_ref (singleton);
1321
1322         return singleton;
1323 }
1324
1325 static void
1326 nm_ip6_manager_init (NMIP6Manager *manager)
1327 {
1328         NMIP6ManagerPrivate *priv = NM_IP6_MANAGER_GET_PRIVATE (manager);
1329
1330         priv->devices = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1331                                                NULL,
1332                                                (GDestroyNotify) nm_ip6_device_destroy);
1333
1334         priv->monitor = nm_netlink_monitor_get ();
1335         nm_netlink_monitor_subscribe (priv->monitor, RTNLGRP_IPV6_IFADDR, NULL);
1336         nm_netlink_monitor_subscribe (priv->monitor, RTNLGRP_IPV6_PREFIX, NULL);
1337         nm_netlink_monitor_subscribe (priv->monitor, RTNLGRP_ND_USEROPT, NULL);
1338         nm_netlink_monitor_subscribe (priv->monitor, RTNLGRP_LINK, NULL);
1339
1340         priv->netlink_id = g_signal_connect (priv->monitor, "notification",
1341                                              G_CALLBACK (netlink_notification), manager);
1342
1343         priv->nlh = nm_netlink_get_default_handle ();
1344         priv->addr_cache = rtnl_addr_alloc_cache (priv->nlh);
1345         priv->route_cache = rtnl_route_alloc_cache (priv->nlh);
1346 }
1347
1348 static void
1349 finalize (GObject *object)
1350 {
1351         NMIP6ManagerPrivate *priv = NM_IP6_MANAGER_GET_PRIVATE (object);
1352
1353         g_signal_handler_disconnect (priv->monitor, priv->netlink_id);
1354
1355         g_hash_table_destroy (priv->devices);
1356         g_object_unref (priv->monitor);
1357         nl_cache_free (priv->addr_cache);
1358         nl_cache_free (priv->route_cache);
1359
1360         singleton = NULL;
1361
1362         G_OBJECT_CLASS (nm_ip6_manager_parent_class)->finalize (object);
1363 }
1364
1365 static void
1366 nm_ip6_manager_class_init (NMIP6ManagerClass *manager_class)
1367 {
1368         GObjectClass *object_class = G_OBJECT_CLASS (manager_class);
1369
1370         g_type_class_add_private (manager_class, sizeof (NMIP6ManagerPrivate));
1371
1372         /* virtual methods */
1373         object_class->finalize = finalize;
1374
1375         /* signals */
1376         signals[ADDRCONF_COMPLETE] =
1377                 g_signal_new ("addrconf-complete",
1378                                           G_OBJECT_CLASS_TYPE (object_class),
1379                                           G_SIGNAL_RUN_FIRST,
1380                                           G_STRUCT_OFFSET (NMIP6ManagerClass, addrconf_complete),
1381                                           NULL, NULL,
1382                                           _nm_marshal_VOID__INT_UINT_BOOLEAN,
1383                                           G_TYPE_NONE, 3, G_TYPE_INT, G_TYPE_UINT, G_TYPE_BOOLEAN);
1384
1385         signals[CONFIG_CHANGED] =
1386                 g_signal_new ("config-changed",
1387                                           G_OBJECT_CLASS_TYPE (object_class),
1388                                           G_SIGNAL_RUN_FIRST,
1389                                           G_STRUCT_OFFSET (NMIP6ManagerClass, config_changed),
1390                                           NULL, NULL,
1391                                           _nm_marshal_VOID__INT_UINT_BOOLEAN,
1392                                           G_TYPE_NONE, 3, G_TYPE_INT, G_TYPE_UINT, G_TYPE_BOOLEAN);
1393 }
1394