ip6: bump the autoconf RA timeout a bit
[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 /******************************************************************/
80
81 typedef struct {
82         NMIP6Manager *manager;
83         char *iface;
84         int ifindex;
85
86         char *accept_ra_path;
87         gboolean accept_ra_save_valid;
88         guint32 accept_ra_save;
89
90         char *disable_ip6_path;
91         gboolean disable_ip6_save_valid;
92         guint32 disable_ip6_save;
93
94         guint finish_addrconf_id;
95         guint config_changed_id;
96
97         NMIP6DeviceState state;
98         NMIP6DeviceState target_state;
99         gboolean addrconf_complete;
100
101         GArray *rdnss_servers;
102         guint rdnss_timeout_id;
103
104         guint ip6flags_poll_id;
105
106         guint32 ra_flags;
107 } NMIP6Device;
108
109 static void
110 nm_ip6_device_destroy (NMIP6Device *device)
111 {
112         g_return_if_fail (device != NULL);
113
114         /* reset the saved RA value */
115         if (device->accept_ra_save_valid) {
116                 nm_utils_do_sysctl (device->accept_ra_path,
117                                     device->accept_ra_save ? "1\n" : "0\n");
118         }
119
120         /* reset the saved IPv6 value */
121         if (device->disable_ip6_save_valid) {
122                 nm_utils_do_sysctl (device->disable_ip6_path,
123                                     device->disable_ip6_save ? "1\n" : "0\n");
124         }
125
126         if (device->finish_addrconf_id)
127                 g_source_remove (device->finish_addrconf_id);
128         if (device->config_changed_id)
129                 g_source_remove (device->config_changed_id);
130         g_free (device->iface);
131         if (device->rdnss_servers)
132                 g_array_free (device->rdnss_servers, TRUE);
133         if (device->rdnss_timeout_id)
134                 g_source_remove (device->rdnss_timeout_id);
135         if (device->ip6flags_poll_id)
136                 g_source_remove (device->ip6flags_poll_id);
137
138         g_free (device->accept_ra_path);
139         g_slice_free (NMIP6Device, device);
140 }
141
142 static gboolean
143 get_proc_sys_net_value (const char *path, const char *iface, guint32 *out_value)
144 {
145         GError *error = NULL;
146         char *contents = NULL;
147         gboolean success = FALSE;
148         long int tmp;
149
150         if (!g_file_get_contents (path, &contents, NULL, &error)) {
151                 nm_log_warn (LOGD_IP6, "(%s): error reading %s: (%d) %s",
152                              iface, path,
153                              error ? error->code : -1,
154                              error && error->message ? error->message : "(unknown)");
155                 g_clear_error (&error);
156         } else {
157                 errno = 0;
158                 tmp = strtol (contents, NULL, 10);
159                 if ((errno == 0) && (tmp == 0 || tmp == 1)) {
160                         *out_value = (guint32) tmp;
161                         success = TRUE;
162                 }
163                 g_free (contents);
164         }
165
166         return success;
167 }
168
169 static NMIP6Device *
170 nm_ip6_device_new (NMIP6Manager *manager, int ifindex)
171 {
172         NMIP6ManagerPrivate *priv = NM_IP6_MANAGER_GET_PRIVATE (manager);
173         NMIP6Device *device;
174
175         g_return_val_if_fail (ifindex > 0, NULL);
176
177         device = g_slice_new0 (NMIP6Device);
178         if (!device) {
179                 nm_log_err (LOGD_IP6, "(%d): out of memory creating IP6 addrconf object.",
180                             ifindex);
181                 return NULL;
182         }
183
184         device->ifindex = ifindex;
185         device->iface = g_strdup (nm_netlink_index_to_iface (ifindex));
186         if (!device->iface) {
187                 nm_log_err (LOGD_IP6, "(%d): could not find interface name from index.",
188                             ifindex);
189                 goto error;
190         }
191
192         device->manager = manager;
193
194         device->rdnss_servers = g_array_new (FALSE, FALSE, sizeof (NMIP6RDNSS));
195
196         g_hash_table_replace (priv->devices, GINT_TO_POINTER (device->ifindex), device);
197
198         /* Grab the original value of "accept_ra" so we can restore it when the
199          * device is taken down.
200          */
201         device->accept_ra_path = g_strdup_printf ("/proc/sys/net/ipv6/conf/%s/accept_ra",
202                                                   device->iface);
203         g_assert (device->accept_ra_path);
204         device->accept_ra_save_valid = get_proc_sys_net_value (device->accept_ra_path,
205                                                                device->iface,
206                                                                &device->accept_ra_save);
207
208         /* and the original value of IPv6 enable/disable */
209         device->disable_ip6_path = g_strdup_printf ("/proc/sys/net/ipv6/conf/%s/disable_ipv6",
210                                                     device->iface);
211         g_assert (device->disable_ip6_path);
212         device->disable_ip6_save_valid = get_proc_sys_net_value (device->disable_ip6_path,
213                                                                  device->iface,
214                                                                  &device->disable_ip6_save);
215
216         return device;
217
218 error:
219         nm_ip6_device_destroy (device);
220         return NULL;
221 }
222
223 static NMIP6Device *
224 nm_ip6_manager_get_device (NMIP6Manager *manager, int ifindex)
225 {
226         NMIP6ManagerPrivate *priv;
227
228         g_return_val_if_fail (manager != NULL, NULL);
229         g_return_val_if_fail (NM_IS_IP6_MANAGER (manager), NULL);
230
231         priv = NM_IP6_MANAGER_GET_PRIVATE (manager);
232         return g_hash_table_lookup (priv->devices, GINT_TO_POINTER (ifindex));
233 }
234
235 /******************************************************************/
236
237 typedef struct {
238         NMIP6Device *device;
239         guint dhcp_opts;
240         gboolean success;
241 } CallbackInfo;
242
243 static gboolean
244 finish_addrconf (gpointer user_data)
245 {
246         CallbackInfo *info = user_data;
247         NMIP6Device *device = info->device;
248         NMIP6Manager *manager = device->manager;
249         int ifindex;
250
251         device->finish_addrconf_id = 0;
252         device->addrconf_complete = TRUE;
253         ifindex = device->ifindex;
254
255         /* We're done, stop polling IPv6 flags */
256         if (device->ip6flags_poll_id) {
257                 g_source_remove (device->ip6flags_poll_id);
258                 device->ip6flags_poll_id = 0;
259         }
260
261         /* And tell listeners that addrconf is complete */
262         if (info->success) {
263                 g_signal_emit (manager, signals[ADDRCONF_COMPLETE], 0,
264                                ifindex, info->dhcp_opts, TRUE);
265         } else {
266                 nm_log_info (LOGD_IP6, "(%s): IP6 addrconf timed out or failed.",
267                              device->iface);
268
269                 nm_ip6_manager_cancel_addrconf (manager, ifindex);
270                 g_signal_emit (manager, signals[ADDRCONF_COMPLETE], 0,
271                                ifindex, info->dhcp_opts, FALSE);
272         }
273
274         return FALSE;
275 }
276
277 static gboolean
278 emit_config_changed (gpointer user_data)
279 {
280         CallbackInfo *info = user_data;
281         NMIP6Device *device = info->device;
282         NMIP6Manager *manager = device->manager;
283
284         device->config_changed_id = 0;
285         g_signal_emit (manager, signals[CONFIG_CHANGED], 0, device->ifindex, info->dhcp_opts);
286         return FALSE;
287 }
288
289 static void set_rdnss_timeout (NMIP6Device *device);
290
291 static gboolean
292 rdnss_expired (gpointer user_data)
293 {
294         NMIP6Device *device = user_data;
295         CallbackInfo info = { device, IP6_DHCP_OPT_NONE };
296
297         set_rdnss_timeout (device);
298         emit_config_changed (&info);
299         return FALSE;
300 }
301
302 static void
303 set_rdnss_timeout (NMIP6Device *device)
304 {
305         time_t expires = 0, now = time (NULL);
306         NMIP6RDNSS *rdnss;
307         int i;
308
309         if (device->rdnss_timeout_id) {
310                 g_source_remove (device->rdnss_timeout_id);
311                 device->rdnss_timeout_id = 0;
312         }
313
314         /* Find the soonest expiration time. */
315         for (i = 0; i < device->rdnss_servers->len; i++) {
316                 rdnss = &g_array_index (device->rdnss_servers, NMIP6RDNSS, i);
317                 if (rdnss->expires == 0)
318                         continue;
319
320                 /* If the entry has already expired, remove it; the "+ 1" is
321                  * because g_timeout_add_seconds() might fudge the timing a
322                  * bit.
323                  */
324                 if (rdnss->expires <= now + 1) {
325                         g_array_remove_index_fast (device->rdnss_servers, i--);
326                         continue;
327                 }
328
329                 if (!expires || rdnss->expires < expires)
330                         expires = rdnss->expires;
331         }
332
333         if (expires) {
334                 device->rdnss_timeout_id = g_timeout_add_seconds (expires - now,
335                                                                                                                   rdnss_expired,
336                                                                                                                   device);
337         }
338 }
339
340 static CallbackInfo *
341 callback_info_new (NMIP6Device *device, guint dhcp_opts, gboolean success)
342 {
343         CallbackInfo *info;
344
345         info = g_malloc0 (sizeof (CallbackInfo));
346         info->device = device;
347         info->dhcp_opts = dhcp_opts;
348         info->success = success;
349         return info;
350 }
351
352 static void
353 nm_ip6_device_sync_from_netlink (NMIP6Device *device, gboolean config_changed)
354 {
355         NMIP6Manager *manager = device->manager;
356         NMIP6ManagerPrivate *priv = NM_IP6_MANAGER_GET_PRIVATE (manager);
357         struct rtnl_addr *rtnladdr;
358         struct nl_addr *nladdr;
359         struct in6_addr *addr;
360         CallbackInfo *info;
361         guint dhcp_opts = IP6_DHCP_OPT_NONE;
362
363         /* Look for any IPv6 addresses the kernel may have set for the device */
364         for (rtnladdr = (struct rtnl_addr *) nl_cache_get_first (priv->addr_cache);
365                  rtnladdr;
366                  rtnladdr = (struct rtnl_addr *) nl_cache_get_next ((struct nl_object *) rtnladdr)) {
367                 if (rtnl_addr_get_ifindex (rtnladdr) != device->ifindex)
368                         continue;
369
370                 nladdr = rtnl_addr_get_local (rtnladdr);
371                 if (!nladdr || nl_addr_get_family (nladdr) != AF_INET6)
372                         continue;
373
374                 addr = nl_addr_get_binary_addr (nladdr);
375                 if (IN6_IS_ADDR_LINKLOCAL (addr)) {
376                         if (device->state == NM_IP6_DEVICE_UNCONFIGURED)
377                                 device->state = NM_IP6_DEVICE_GOT_LINK_LOCAL;
378                 } else {
379                         if (device->state < NM_IP6_DEVICE_GOT_ADDRESS)
380                                 device->state = NM_IP6_DEVICE_GOT_ADDRESS;
381                 }
382         }
383
384         /* We only care about router advertisements if we want for a real IPv6 address */
385         if (device->target_state == NM_IP6_DEVICE_GOT_ADDRESS) {
386                 if (   (device->ra_flags & IF_RA_RCVD)
387                     && (device->state < NM_IP6_DEVICE_GOT_ROUTER_ADVERTISEMENT))
388                         device->state = NM_IP6_DEVICE_GOT_ROUTER_ADVERTISEMENT;
389
390                 if (device->ra_flags & IF_RA_MANAGED) {
391                         dhcp_opts = IP6_DHCP_OPT_MANAGED;
392                         nm_log_dbg (LOGD_IP6, "router advertisement deferred to DHCPv6");
393                 } else if (device->ra_flags & IF_RA_OTHERCONF) {
394                         dhcp_opts = IP6_DHCP_OPT_OTHERCONF;
395                         nm_log_dbg (LOGD_IP6, "router advertisement requests parallel DHCPv6");
396                 }
397         }
398
399         if (!device->addrconf_complete) {
400                 /* Managed mode (ie DHCP only) short-circuits automatic addrconf, so
401                  * we don't bother waiting for the device's target state to be reached
402                  * when the RA requests managed mode.
403                  */
404                 if (   (device->state >= device->target_state)
405                     || (dhcp_opts == IP6_DHCP_OPT_MANAGED)) {
406                         /* device->finish_addrconf_id may currently be a timeout
407                          * rather than an idle, so we remove the existing source.
408                          */
409                         if (device->finish_addrconf_id)
410                                 g_source_remove (device->finish_addrconf_id);
411
412                         info = callback_info_new (device, dhcp_opts, TRUE);
413                         device->finish_addrconf_id = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
414                                                                       finish_addrconf,
415                                                                       info,
416                                                                       (GDestroyNotify) g_free);
417                 }
418         } else if (config_changed) {
419                 if (!device->config_changed_id) {
420                         info = callback_info_new (device, dhcp_opts, TRUE);
421                         device->config_changed_id = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
422                                                                      emit_config_changed,
423                                                                      info,
424                                                                      (GDestroyNotify) g_free);
425                 }
426         }
427 }
428
429 static void
430 ref_object (struct nl_object *obj, void *data)
431 {
432         struct nl_object **out = data;
433
434         nl_object_get (obj);
435         *out = obj;
436 }
437
438 static NMIP6Device *
439 process_addr (NMIP6Manager *manager, struct nl_msg *msg)
440 {
441         NMIP6ManagerPrivate *priv = NM_IP6_MANAGER_GET_PRIVATE (manager);
442         NMIP6Device *device;
443         struct rtnl_addr *rtnladdr;
444         int old_size;
445
446         rtnladdr = NULL;
447         nl_msg_parse (msg, ref_object, &rtnladdr);
448         if (!rtnladdr)
449                 return NULL;
450
451         device = nm_ip6_manager_get_device (manager, rtnl_addr_get_ifindex (rtnladdr));
452         if (!device)
453                 return NULL;
454
455         old_size = nl_cache_nitems (priv->addr_cache);
456         nl_cache_include (priv->addr_cache, (struct nl_object *)rtnladdr, NULL);
457         rtnl_addr_put (rtnladdr);
458
459         /* The kernel will re-notify us of automatically-added addresses
460          * every time it gets another router advertisement. We only want
461          * to notify higher levels if we actually changed something.
462          */
463         if (nl_cache_nitems (priv->addr_cache) == old_size)
464                 return NULL;
465
466         return device;
467 }
468
469 static NMIP6Device *
470 process_route (NMIP6Manager *manager, struct nl_msg *msg)
471 {
472         NMIP6ManagerPrivate *priv = NM_IP6_MANAGER_GET_PRIVATE (manager);
473         NMIP6Device *device;
474         struct rtnl_route *rtnlroute;
475         int old_size;
476
477         rtnlroute = NULL;
478         nl_msg_parse (msg, ref_object, &rtnlroute);
479         if (!rtnlroute)
480                 return NULL;
481
482         device = nm_ip6_manager_get_device (manager, rtnl_route_get_oif (rtnlroute));
483         if (!device)
484                 return NULL;
485
486         old_size = nl_cache_nitems (priv->route_cache);
487         nl_cache_include (priv->route_cache, (struct nl_object *)rtnlroute, NULL);
488         rtnl_route_put (rtnlroute);
489
490         /* As above in process_addr */
491         if (nl_cache_nitems (priv->route_cache) == old_size)
492                 return NULL;
493
494         return device;
495 }
496
497 static NMIP6Device *
498 process_prefix (NMIP6Manager *manager, struct nl_msg *msg)
499 {
500         struct prefixmsg *pmsg;
501         NMIP6Device *device;
502
503         /* We don't care about the prefix itself, but if we receive a
504          * router advertisement telling us to use DHCP, we might not
505          * get any RTM_NEWADDRs or RTM_NEWROUTEs, so this is our only
506          * way to notice immediately that an RA was received.
507          */
508
509         pmsg = (struct prefixmsg *) NLMSG_DATA (nlmsg_hdr (msg));
510         device = nm_ip6_manager_get_device (manager, pmsg->prefix_ifindex);
511
512         if (!device || device->addrconf_complete)
513                 return NULL;
514
515         return device;
516 }
517
518 /* RDNSS parsing code based on rdnssd, Copyright 2007 Pierre Ynard,
519  * RĂ©mi Denis-Courmont. GPLv2/3
520  */
521
522 #define ND_OPT_RDNSS 25
523 struct nd_opt_rdnss {
524         uint8_t nd_opt_rdnss_type;
525         uint8_t nd_opt_rdnss_len;
526         uint16_t nd_opt_rdnss_reserved1;
527         uint32_t nd_opt_rdnss_lifetime;
528         /* followed by one or more IPv6 addresses */
529 };
530
531 static NMIP6Device *
532 process_nduseropt (NMIP6Manager *manager, struct nl_msg *msg)
533 {
534         NMIP6Device *device;
535         struct nduseroptmsg *ndmsg;
536         struct nd_opt_hdr *opt;
537         guint opts_len, i;
538         time_t now = time (NULL);
539         struct nd_opt_rdnss *rdnss_opt;
540         struct in6_addr *addr;
541         GArray *servers;
542         NMIP6RDNSS server, *sa, *sb;
543         gboolean changed;
544
545         ndmsg = (struct nduseroptmsg *) NLMSG_DATA (nlmsg_hdr (msg));
546
547         if (ndmsg->nduseropt_family != AF_INET6 ||
548                 ndmsg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
549                 ndmsg->nduseropt_icmp_code != 0)
550                 return NULL;
551
552         device = nm_ip6_manager_get_device (manager, ndmsg->nduseropt_ifindex);
553         if (!device)
554                 return NULL;
555
556         servers = g_array_new (FALSE, FALSE, sizeof (NMIP6RDNSS));
557
558         opt = (struct nd_opt_hdr *) (ndmsg + 1);
559         opts_len = ndmsg->nduseropt_opts_len;
560
561         while (opts_len >= sizeof (struct nd_opt_hdr)) {
562                 size_t nd_opt_len = opt->nd_opt_len;
563
564                 if (nd_opt_len == 0 || opts_len < (nd_opt_len << 3))
565                         break;
566
567                 if (opt->nd_opt_type != ND_OPT_RDNSS)
568                         goto next;
569
570                 if (nd_opt_len < 3 || (nd_opt_len & 1) == 0)
571                         goto next;
572
573                 rdnss_opt = (struct nd_opt_rdnss *) opt;
574
575                 server.expires = now + ntohl (rdnss_opt->nd_opt_rdnss_lifetime);
576                 for (addr = (struct in6_addr *) (rdnss_opt + 1); nd_opt_len >= 2; addr++, nd_opt_len -= 2) {
577                         server.addr = *addr;
578                         g_array_append_val (servers, server);
579                 }
580
581         next:
582                 opts_len -= opt->nd_opt_len << 3;
583                 opt = (struct nd_opt_hdr *) ((uint8_t *) opt + (opt->nd_opt_len << 3));
584         }
585
586         /* See if anything (other than expiration time) changed */
587         if (servers->len != device->rdnss_servers->len)
588                 changed = TRUE;
589         else {
590                 for (i = 0; i < servers->len; i++) {
591                         sa = &(g_array_index (servers, NMIP6RDNSS, i));
592                         sb = &(g_array_index (device->rdnss_servers, NMIP6RDNSS, i));
593                         if (memcmp (&sa->addr, &sb->addr, sizeof (struct in6_addr)) != 0) {
594                                 changed = TRUE;
595                                 break;
596                         }
597                 }
598                 changed = FALSE;
599         }
600
601         if (changed) {
602                 g_array_free (device->rdnss_servers, TRUE);
603                 device->rdnss_servers = servers;
604         } else
605                 g_array_free (servers, TRUE);
606
607         /* Timeouts may have changed even if IPs didn't */
608         set_rdnss_timeout (device);
609
610         if (changed)
611                 return device;
612         else
613                 return NULL;
614 }
615
616 static struct nla_policy link_policy[IFLA_MAX + 1] = {
617         [IFLA_PROTINFO] = { .type = NLA_NESTED },
618 };
619
620 static struct nla_policy link_prot_policy[IFLA_INET6_MAX + 1] = {
621         [IFLA_INET6_FLAGS]      = { .type = NLA_U32 },
622 };
623
624 static NMIP6Device *
625 process_newlink (NMIP6Manager *manager, struct nl_msg *msg)
626 {
627         struct nlmsghdr *hdr = nlmsg_hdr (msg);
628         struct ifinfomsg *ifi;
629         NMIP6Device *device;
630         struct nlattr *tb[IFLA_MAX + 1];
631         struct nlattr *pi[IFLA_INET6_MAX + 1];
632         int err;
633
634         ifi = nlmsg_data (hdr);
635         if (ifi->ifi_family != AF_INET6)
636                 return NULL;
637
638         device = nm_ip6_manager_get_device (manager, ifi->ifi_index);
639         if (!device || device->addrconf_complete)
640                 return NULL;
641
642         /* FIXME: we have to do this manually for now since libnl doesn't yet
643          * support the IFLA_PROTINFO attribute of NEWLINK messages.  When it does,
644          * we can get rid of this function and just grab IFLA_PROTINFO from
645          * nm_ip6_device_sync_from_netlink(), then get the IFLA_INET6_FLAGS out of
646          * the PROTINFO.
647          */
648
649         err = nlmsg_parse (hdr, sizeof (*ifi), tb, IFLA_MAX, link_policy);
650         if (err < 0)
651                 return NULL;
652         if (!tb[IFLA_PROTINFO])
653                 return NULL;
654
655         err = nla_parse_nested (pi, IFLA_INET6_MAX, tb[IFLA_PROTINFO], link_prot_policy);
656         if (err < 0)
657                 return NULL;
658         if (!pi[IFLA_INET6_FLAGS])
659                 return NULL;
660
661         device->ra_flags = nla_get_u32 (pi[IFLA_INET6_FLAGS]);
662         nm_log_dbg (LOGD_IP6, "(%s): got IPv6 flags 0x%X", device->iface, device->ra_flags);
663
664         return device;
665 }
666
667 static void
668 netlink_notification (NMNetlinkMonitor *monitor, struct nl_msg *msg, gpointer user_data)
669 {
670         NMIP6Manager *manager = (NMIP6Manager *) user_data;
671         NMIP6Device *device;
672         struct nlmsghdr *hdr;
673         gboolean config_changed = FALSE;
674
675         hdr = nlmsg_hdr (msg);
676         switch (hdr->nlmsg_type) {
677         case RTM_NEWADDR:
678         case RTM_DELADDR:
679                 device = process_addr (manager, msg);
680                 config_changed = TRUE;
681                 break;
682         case RTM_NEWROUTE:
683         case RTM_DELROUTE:
684                 device = process_route (manager, msg);
685                 config_changed = TRUE;
686                 break;
687         case RTM_NEWPREFIX:
688                 device = process_prefix (manager, msg);
689                 break;
690         case RTM_NEWNDUSEROPT:
691                 device = process_nduseropt (manager, msg);
692                 config_changed = TRUE;
693                 break;
694         case RTM_NEWLINK:
695                 device = process_newlink (manager, msg);
696                 config_changed = TRUE;
697                 break;
698         default:
699                 return;
700         }
701
702         if (device)
703                 nm_ip6_device_sync_from_netlink (device, config_changed);
704 }
705
706 void
707 nm_ip6_manager_prepare_interface (NMIP6Manager *manager,
708                                                                   int ifindex,
709                                                                   NMSettingIP6Config *s_ip6)
710 {
711         NMIP6ManagerPrivate *priv;
712         NMIP6Device *device;
713         const char *method = NULL;
714
715         g_return_if_fail (NM_IS_IP6_MANAGER (manager));
716         g_return_if_fail (ifindex > 0);
717
718         priv = NM_IP6_MANAGER_GET_PRIVATE (manager);
719
720         device = nm_ip6_device_new (manager, ifindex);
721         g_return_if_fail (device != NULL);
722         g_return_if_fail (   strchr (device->iface, '/') == NULL
723                           && strcmp (device->iface, "all") != 0
724                           && strcmp (device->iface, "default") != 0);
725
726         if (s_ip6)
727                 method = nm_setting_ip6_config_get_method (s_ip6);
728         if (!method)
729                 method = NM_SETTING_IP6_CONFIG_METHOD_AUTO;
730
731         /* Establish target state and turn router advertisement acceptance on or off */
732         if (!strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL)) {
733                 device->target_state = NM_IP6_DEVICE_GOT_LINK_LOCAL;
734                 nm_utils_do_sysctl (device->accept_ra_path, "0\n");
735         } else {
736                 device->target_state = NM_IP6_DEVICE_GOT_ADDRESS;
737                 nm_utils_do_sysctl (device->accept_ra_path, "1\n");
738         }
739 }
740
741 static gboolean
742 poll_ip6_flags (gpointer user_data)
743 {
744         nm_netlink_monitor_request_ip6_info (NM_NETLINK_MONITOR (user_data), NULL);
745         return TRUE;
746 }
747
748 void
749 nm_ip6_manager_begin_addrconf (NMIP6Manager *manager, int ifindex)
750 {
751         NMIP6ManagerPrivate *priv;
752         NMIP6Device *device;
753         CallbackInfo *info;
754
755         g_return_if_fail (NM_IS_IP6_MANAGER (manager));
756         g_return_if_fail (ifindex > 0);
757
758         priv = NM_IP6_MANAGER_GET_PRIVATE (manager);
759
760         device = (NMIP6Device *) g_hash_table_lookup (priv->devices, GINT_TO_POINTER (ifindex));
761         g_return_if_fail (device != NULL);
762
763         nm_log_info (LOGD_IP6, "Activation (%s) Beginning IP6 addrconf.", device->iface);
764
765         device->addrconf_complete = FALSE;
766         device->ra_flags = 0;
767
768         /* Set up a timeout on the transaction to kill it after the timeout */
769         info = callback_info_new (device, 0, FALSE);
770         device->finish_addrconf_id = g_timeout_add_seconds_full (G_PRIORITY_DEFAULT,
771                                                                  NM_IP6_TIMEOUT,
772                                                                  finish_addrconf,
773                                                                  info,
774                                                                  (GDestroyNotify) g_free);
775
776         /* Bounce IPv6 on the interface to ensure the kernel will start looking for
777          * new RAs; there doesn't seem to be a better way to do this right now.
778          */
779         if (device->target_state >= NM_IP6_DEVICE_GOT_ADDRESS) {
780                 nm_utils_do_sysctl (device->disable_ip6_path, "1\n");
781                 g_usleep (200);
782                 nm_utils_do_sysctl (device->disable_ip6_path, "0\n");
783         }
784
785         device->ip6flags_poll_id = g_timeout_add_seconds (1, poll_ip6_flags, priv->monitor);
786
787         /* Kick off the initial IPv6 flags request */
788         nm_netlink_monitor_request_ip6_info (priv->monitor, NULL);
789
790         /* Sync flags, etc, from netlink; this will also notice if the
791          * device is already fully configured and schedule the
792          * ADDRCONF_COMPLETE signal in that case.
793          */
794         nm_ip6_device_sync_from_netlink (device, FALSE);
795 }
796
797 void
798 nm_ip6_manager_cancel_addrconf (NMIP6Manager *manager, int ifindex)
799 {
800         g_return_if_fail (NM_IS_IP6_MANAGER (manager));
801         g_return_if_fail (ifindex > 0);
802
803         g_hash_table_remove (NM_IP6_MANAGER_GET_PRIVATE (manager)->devices,
804                              GINT_TO_POINTER (ifindex));
805 }
806
807 #define FIRST_ROUTE(m) ((struct rtnl_route *) nl_cache_get_first (m))
808 #define NEXT_ROUTE(m) ((struct rtnl_route *) nl_cache_get_next ((struct nl_object *) m))
809
810 #define FIRST_ADDR(m) ((struct rtnl_addr *) nl_cache_get_first (m))
811 #define NEXT_ADDR(m) ((struct rtnl_addr *) nl_cache_get_next ((struct nl_object *) m))
812
813 NMIP6Config *
814 nm_ip6_manager_get_ip6_config (NMIP6Manager *manager, int ifindex)
815 {
816         NMIP6ManagerPrivate *priv;
817         NMIP6Device *device;
818         NMIP6Config *config;
819         struct rtnl_addr *rtnladdr;
820         struct nl_addr *nladdr;
821         struct in6_addr *addr;
822         NMIP6Address *ip6addr;
823         struct rtnl_route *rtnlroute;
824         struct nl_addr *nldest, *nlgateway;
825         struct in6_addr *dest, *gateway;
826         gboolean defgw_set = FALSE;
827         struct in6_addr defgw;
828         uint32_t metric;
829         NMIP6Route *ip6route;
830         int i;
831
832         g_return_val_if_fail (NM_IS_IP6_MANAGER (manager), NULL);
833         g_return_val_if_fail (ifindex > 0, NULL);
834
835         priv = NM_IP6_MANAGER_GET_PRIVATE (manager);
836
837         device = (NMIP6Device *) g_hash_table_lookup (priv->devices,
838                                                       GINT_TO_POINTER (ifindex));
839         if (!device) {
840                 nm_log_warn (LOGD_IP6, "(%d): addrconf not started.", ifindex);
841                 return NULL;
842         }
843
844         config = nm_ip6_config_new ();
845         if (!config) {
846                 nm_log_err (LOGD_IP6, "(%s): out of memory creating IP6 config object.",
847                             device->iface);
848                 return NULL;
849         }
850
851         /* Make sure we refill the route and address caches, otherwise we won't get
852          * up-to-date information here since the netlink route/addr change messages
853          * may be lagging a bit.
854          */
855         nl_cache_refill (priv->nlh, priv->route_cache);
856         nl_cache_refill (priv->nlh, priv->addr_cache);
857
858         /* Add routes */
859         for (rtnlroute = FIRST_ROUTE (priv->route_cache); rtnlroute; rtnlroute = NEXT_ROUTE (rtnlroute)) {
860                 /* Make sure it's an IPv6 route for this device */
861                 if (rtnl_route_get_oif (rtnlroute) != device->ifindex)
862                         continue;
863                 if (rtnl_route_get_family (rtnlroute) != AF_INET6)
864                         continue;
865
866                 nldest = rtnl_route_get_dst (rtnlroute);
867                 if (!nldest || nl_addr_get_family (nldest) != AF_INET6)
868                         continue;
869                 dest = nl_addr_get_binary_addr (nldest);
870
871                 nlgateway = rtnl_route_get_gateway (rtnlroute);
872                 if (!nlgateway || nl_addr_get_family (nlgateway) != AF_INET6)
873                         continue;
874                 gateway = nl_addr_get_binary_addr (nlgateway);
875
876                 if (rtnl_route_get_dst_len (rtnlroute) == 0) {
877                         /* Default gateway route; don't add to normal routes but to each address */
878                         if (!defgw_set) {
879                                 memcpy (&defgw, gateway, sizeof (defgw));
880                                 defgw_set = TRUE;
881                         }
882                         continue;
883                 }
884
885                 ip6route = nm_ip6_route_new ();
886                 nm_ip6_route_set_dest (ip6route, dest);
887                 nm_ip6_route_set_prefix (ip6route, rtnl_route_get_dst_len (rtnlroute));
888                 nm_ip6_route_set_next_hop (ip6route, gateway);
889                 metric = rtnl_route_get_metric (rtnlroute, 1);
890                 if (metric != UINT_MAX)
891                         nm_ip6_route_set_metric (ip6route, metric);
892                 nm_ip6_config_take_route (config, ip6route);
893         }
894
895         /* Add addresses */
896         for (rtnladdr = FIRST_ADDR (priv->addr_cache); rtnladdr; rtnladdr = NEXT_ADDR (rtnladdr)) {
897                 if (rtnl_addr_get_ifindex (rtnladdr) != device->ifindex)
898                         continue;
899
900                 nladdr = rtnl_addr_get_local (rtnladdr);
901                 if (!nladdr || nl_addr_get_family (nladdr) != AF_INET6)
902                         continue;
903
904                 addr = nl_addr_get_binary_addr (nladdr);
905                 ip6addr = nm_ip6_address_new ();
906                 nm_ip6_address_set_prefix (ip6addr, rtnl_addr_get_prefixlen (rtnladdr));
907                 nm_ip6_address_set_address (ip6addr, addr);
908                 nm_ip6_config_take_address (config, ip6addr);
909                 if (defgw_set)
910                         nm_ip6_address_set_gateway (ip6addr, &defgw);
911         }
912
913         /* Add DNS servers */
914         if (device->rdnss_servers) {
915                 NMIP6RDNSS *rdnss = (NMIP6RDNSS *)(device->rdnss_servers->data);
916
917                 for (i = 0; i < device->rdnss_servers->len; i++)
918                         nm_ip6_config_add_nameserver (config, &rdnss[i].addr);
919         }
920
921         return config;
922 }
923
924 /******************************************************************/
925
926 static NMIP6Manager *
927 nm_ip6_manager_new (void)
928 {
929         NMIP6Manager *manager;
930         NMIP6ManagerPrivate *priv;
931
932         manager = g_object_new (NM_TYPE_IP6_MANAGER, NULL);
933         priv = NM_IP6_MANAGER_GET_PRIVATE (manager);
934
935         if (!priv->devices) {
936                 nm_log_err (LOGD_IP6, "not enough memory to initialize IP6 manager tables");
937                 g_object_unref (manager);
938                 manager = NULL;
939         }
940
941         return manager;
942 }
943
944 NMIP6Manager *
945 nm_ip6_manager_get (void)
946 {
947         static NMIP6Manager *singleton = NULL;
948
949         if (!singleton) {
950                 singleton = nm_ip6_manager_new ();
951                 g_assert (singleton);
952         } else
953                 g_object_ref (singleton);
954
955         return singleton;
956 }
957
958 static void
959 nm_ip6_manager_init (NMIP6Manager *manager)
960 {
961         NMIP6ManagerPrivate *priv = NM_IP6_MANAGER_GET_PRIVATE (manager);
962
963         priv->devices = g_hash_table_new_full (g_direct_hash, g_direct_equal,
964                                                NULL,
965                                                (GDestroyNotify) nm_ip6_device_destroy);
966
967         priv->monitor = nm_netlink_monitor_get ();
968         nm_netlink_monitor_subscribe (priv->monitor, RTNLGRP_IPV6_IFADDR, NULL);
969         nm_netlink_monitor_subscribe (priv->monitor, RTNLGRP_IPV6_PREFIX, NULL);
970         nm_netlink_monitor_subscribe (priv->monitor, RTNLGRP_ND_USEROPT, NULL);
971         nm_netlink_monitor_subscribe (priv->monitor, RTNLGRP_LINK, NULL);
972
973         priv->netlink_id = g_signal_connect (priv->monitor, "notification",
974                                              G_CALLBACK (netlink_notification), manager);
975
976         priv->nlh = nm_netlink_get_default_handle ();
977         priv->addr_cache = rtnl_addr_alloc_cache (priv->nlh);
978         priv->route_cache = rtnl_route_alloc_cache (priv->nlh);
979 }
980
981 static void
982 finalize (GObject *object)
983 {
984         NMIP6ManagerPrivate *priv = NM_IP6_MANAGER_GET_PRIVATE (object);
985
986         g_signal_handler_disconnect (priv->monitor, priv->netlink_id);
987
988         g_hash_table_destroy (priv->devices);
989         g_object_unref (priv->monitor);
990         nl_cache_free (priv->addr_cache);
991         nl_cache_free (priv->route_cache);
992
993         G_OBJECT_CLASS (nm_ip6_manager_parent_class)->finalize (object);
994 }
995
996 static void
997 nm_ip6_manager_class_init (NMIP6ManagerClass *manager_class)
998 {
999         GObjectClass *object_class = G_OBJECT_CLASS (manager_class);
1000
1001         g_type_class_add_private (manager_class, sizeof (NMIP6ManagerPrivate));
1002
1003         /* virtual methods */
1004         object_class->finalize = finalize;
1005
1006         /* signals */
1007         signals[ADDRCONF_COMPLETE] =
1008                 g_signal_new ("addrconf-complete",
1009                                           G_OBJECT_CLASS_TYPE (object_class),
1010                                           G_SIGNAL_RUN_FIRST,
1011                                           G_STRUCT_OFFSET (NMIP6ManagerClass, addrconf_complete),
1012                                           NULL, NULL,
1013                                           _nm_marshal_VOID__INT_UINT_BOOLEAN,
1014                                           G_TYPE_NONE, 3, G_TYPE_INT, G_TYPE_UINT, G_TYPE_BOOLEAN);
1015
1016         signals[CONFIG_CHANGED] =
1017                 g_signal_new ("config-changed",
1018                                           G_OBJECT_CLASS_TYPE (object_class),
1019                                           G_SIGNAL_RUN_FIRST,
1020                                           G_STRUCT_OFFSET (NMIP6ManagerClass, config_changed),
1021                                           NULL, NULL,
1022                                           _nm_marshal_VOID__INT_UINT,
1023                                           G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_UINT);
1024 }
1025