1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 Copyright © 2017 Intel Corporation. All rights reserved.
8 #include <netinet/icmp6.h>
13 #include "ndisc-option.h"
14 #include "network-common.h"
15 #include "sparse-endian.h"
16 #include "time-util.h"
18 /* RFC 4861 section 6.2.1.
20 * The maximum time allowed between sending unsolicited multicast Router Advertisements from the
21 * interface, in seconds. MUST be no less than 4 seconds and no greater than 1800 seconds.
22 * Default: 600 seconds */
23 #define RADV_MIN_MAX_TIMEOUT_USEC (4 * USEC_PER_SEC)
24 #define RADV_MAX_MAX_TIMEOUT_USEC (1800 * USEC_PER_SEC)
25 #define RADV_DEFAULT_MAX_TIMEOUT_USEC (600 * USEC_PER_SEC)
26 /* RFC 4861 section 6.2.1.
28 * The minimum time allowed between sending unsolicited multicast Router Advertisements from the
29 * interface, in seconds. MUST be no less than 3 seconds and no greater than .75 * MaxRtrAdvInterval.
30 * Default: 0.33 * MaxRtrAdvInterval If MaxRtrAdvInterval >= 9 seconds; otherwise, the Default is
31 * MaxRtrAdvInterval (Note, this should be a typo. We use 0.75 * MaxRtrAdvInterval). */
32 #define RADV_MIN_MIN_TIMEOUT_USEC (3 * USEC_PER_SEC)
33 /* RFC 4861 section 6.2.4.
35 * The value to be placed in the Router Lifetime field of Router Advertisements sent from the interface,
36 * in seconds. MUST be either zero or between MaxRtrAdvInterval and 9000 seconds. A value of zero
37 * indicates that the router is not to be used as a default router. These limits may be overridden by
38 * specific documents that describe how IPv6 operates over different link layers. For instance, in a
39 * point-to-point link the peers may have enough information about the number and status of devices at
40 * the other end so that advertisements are needed less frequently.
41 * Default: 3 * MaxRtrAdvInterval */
42 #define RADV_MIN_ROUTER_LIFETIME_USEC RADV_MIN_MAX_TIMEOUT_USEC
43 #define RADV_MAX_ROUTER_LIFETIME_USEC (9000 * USEC_PER_SEC)
44 #define RADV_DEFAULT_ROUTER_LIFETIME_USEC (3 * RADV_DEFAULT_MAX_TIMEOUT_USEC)
45 /* RFC 4861 section 4.2.
46 * Reachable Time and Retrans Timer
47 * 32-bit unsigned integer. The time, in milliseconds. */
48 #define RADV_MAX_UINT32_MSEC_USEC (UINT32_MAX * USEC_PER_MSEC)
49 #define RADV_MAX_REACHABLE_TIME_USEC RADV_MAX_UINT32_MSEC_USEC
50 #define RADV_MAX_RETRANSMIT_USEC RADV_MAX_UINT32_MSEC_USEC
51 /* draft-ietf-6man-slaac-renum-02 section 4.1.1.
52 * AdvPreferredLifetime: max(AdvDefaultLifetime, 3 * MaxRtrAdvInterval)
53 * AdvValidLifetime: 2 * AdvPreferredLifetime */
54 #define RADV_DEFAULT_PREFERRED_LIFETIME_USEC CONST_MAX(RADV_DEFAULT_ROUTER_LIFETIME_USEC, 3 * RADV_DEFAULT_MAX_TIMEOUT_USEC)
55 #define RADV_DEFAULT_VALID_LIFETIME_USEC (2 * RADV_DEFAULT_PREFERRED_LIFETIME_USEC)
56 /* RFC 4861 section 10.
57 * MAX_INITIAL_RTR_ADVERT_INTERVAL 16 seconds
58 * MAX_INITIAL_RTR_ADVERTISEMENTS 3 transmissions
59 * MAX_FINAL_RTR_ADVERTISEMENTS 3 transmissions
60 * MIN_DELAY_BETWEEN_RAS 3 seconds
61 * MAX_RA_DELAY_TIME .5 seconds */
62 #define RADV_MAX_INITIAL_RTR_ADVERT_INTERVAL_USEC (16 * USEC_PER_SEC)
63 #define RADV_MAX_INITIAL_RTR_ADVERTISEMENTS 3
64 #define RADV_MAX_FINAL_RTR_ADVERTISEMENTS 3
65 #define RADV_MIN_DELAY_BETWEEN_RAS 3
66 #define RADV_MAX_RA_DELAY_TIME_USEC (500 * USEC_PER_MSEC)
67 /* From RFC 8781 section 4.1
68 * By default, the value of the Scaled Lifetime field SHOULD be set to the lesser of 3 x MaxRtrAdvInterval */
69 #define RADV_PREF64_DEFAULT_LIFETIME_USEC (3 * RADV_DEFAULT_MAX_TIMEOUT_USEC)
71 #define RADV_RDNSS_MAX_LIFETIME_USEC (UINT32_MAX * USEC_PER_SEC)
72 #define RADV_DNSSL_MAX_LIFETIME_USEC (UINT32_MAX * USEC_PER_SEC)
73 /* rfc6275 7.4 Neighbor Discovery Home Agent Lifetime.
74 * The default value is the same as the Router Lifetime.
75 * The maximum value corresponds to 18.2 hours. 0 MUST NOT be used. */
76 #define RADV_HOME_AGENT_MAX_LIFETIME_USEC (UINT16_MAX * USEC_PER_SEC)
78 #define RADV_OPT_ROUTE_INFORMATION 24
79 #define RADV_OPT_RDNSS 25
80 #define RADV_OPT_DNSSL 31
81 /* Pref64 option type (RFC8781, section 4) */
82 #define RADV_OPT_PREF64 38
84 typedef enum RAdvState
{
86 RADV_STATE_ADVERTISING
= 1,
89 struct sd_radv_opt_dns
{
102 struct in6_addr ipv6ll
;
107 struct ether_addr mac_addr
;
112 usec_t reachable_usec
;
113 usec_t retransmit_usec
;
114 usec_t lifetime_usec
; /* timespan */
118 sd_event_source
*recv_event_source
;
119 sd_event_source
*timeout_event_source
;
122 LIST_HEAD(sd_radv_prefix
, prefixes
);
124 unsigned n_route_prefixes
;
125 LIST_HEAD(sd_radv_route_prefix
, route_prefixes
);
127 unsigned n_pref64_prefixes
;
128 LIST_HEAD(sd_radv_pref64_prefix
, pref64_prefixes
);
131 struct sd_radv_opt_dns
*rdnss
;
132 struct sd_radv_opt_dns
*dnssl
;
134 /* Mobile IPv6 extension: Home Agent Info. */
135 struct nd_opt_home_agent_info home_agent
;
138 #define radv_prefix_opt__contents { \
143 be32_t lifetime_valid; \
144 be32_t lifetime_preferred; \
146 struct in6_addr in6_addr; \
149 struct radv_prefix_opt radv_prefix_opt__contents
;
151 /* We need the opt substructure to be packed, because we use it in send(). But
152 * if we use _packed_, this means that the structure cannot be used directly in
153 * normal code in general, because the fields might not be properly aligned.
154 * But in this particular case, the structure is defined in a way that gives
155 * proper alignment, even without the explicit _packed_ attribute. To appease
156 * the compiler we use the "unpacked" structure, but we also verify that
157 * structure contains no holes, so offsets are the same when _packed_ is used.
159 struct radv_prefix_opt__packed radv_prefix_opt__contents _packed_
;
160 assert_cc(sizeof(struct radv_prefix_opt
) == sizeof(struct radv_prefix_opt__packed
));
162 struct sd_radv_prefix
{
165 struct radv_prefix_opt opt
;
167 LIST_FIELDS(struct sd_radv_prefix
, prefix
);
169 /* These are timespans, NOT points in time. */
170 usec_t lifetime_valid_usec
;
171 usec_t lifetime_preferred_usec
;
172 /* These are points in time specified with clock_boottime_or_monotonic(), NOT timespans. */
174 usec_t preferred_until
;
177 #define radv_route_prefix_opt__contents { \
181 uint8_t flags_reserved; \
183 struct in6_addr in6_addr; \
186 struct radv_route_prefix_opt radv_route_prefix_opt__contents
;
188 struct radv_route_prefix_opt__packed radv_route_prefix_opt__contents _packed_
;
189 assert_cc(sizeof(struct radv_route_prefix_opt
) == sizeof(struct radv_route_prefix_opt__packed
));
191 struct sd_radv_route_prefix
{
194 struct radv_route_prefix_opt opt
;
196 LIST_FIELDS(struct sd_radv_route_prefix
, prefix
);
198 /* This is a timespan, NOT a point in time. */
199 usec_t lifetime_usec
;
200 /* This is a point in time specified with clock_boottime_or_monotonic(), NOT a timespan. */
204 struct sd_radv_pref64_prefix
{
207 struct nd_opt_prefix64_info opt
;
209 struct in6_addr in6_addr
;
212 usec_t lifetime_usec
;
214 LIST_FIELDS(struct sd_radv_pref64_prefix
, prefix
);
217 #define log_radv_errno(radv, error, fmt, ...) \
218 log_interface_prefix_full_errno( \
221 error, fmt, ##__VA_ARGS__)
222 #define log_radv(radv, fmt, ...) \
223 log_interface_prefix_full_errno_zerook( \
226 0, fmt, ##__VA_ARGS__)