1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 /***
5   Copyright © 2017 Intel Corporation. All rights reserved.
6 ***/
7 
8 #include "sd-radv.h"
9 
10 #include "list.h"
11 #include "network-common.h"
12 #include "sparse-endian.h"
13 #include "time-util.h"
14 
15 /* RFC 4861 section 6.2.1.
16  * MaxRtrAdvInterval
17  * The maximum time allowed between sending unsolicited multicast Router Advertisements from the
18  * interface, in seconds. MUST be no less than 4 seconds and no greater than 1800 seconds.
19  * Default: 600 seconds */
20 #define RADV_MIN_MAX_TIMEOUT_USEC                 (4 * USEC_PER_SEC)
21 #define RADV_MAX_MAX_TIMEOUT_USEC                 (1800 * USEC_PER_SEC)
22 #define RADV_DEFAULT_MAX_TIMEOUT_USEC             (600 * USEC_PER_SEC)
23 /* RFC 4861 section 6.2.1.
24  * MinRtrAdvInterval
25  * The minimum time allowed between sending unsolicited multicast Router Advertisements from the
26  * interface, in seconds. MUST be no less than 3 seconds and no greater than .75 * MaxRtrAdvInterval.
27  * Default: 0.33 * MaxRtrAdvInterval If MaxRtrAdvInterval >= 9 seconds; otherwise, the Default is
28  * MaxRtrAdvInterval (Note, this should be a typo. We use 0.75 * MaxRtrAdvInterval). */
29 #define RADV_MIN_MIN_TIMEOUT_USEC                 (3 * USEC_PER_SEC)
30 /* RFC 4861 section 6.2.4.
31  * AdvDefaultLifetime
32  * The value to be placed in the Router Lifetime field of Router Advertisements sent from the interface,
33  * in seconds. MUST be either zero or between MaxRtrAdvInterval and 9000 seconds. A value of zero
34  * indicates that the router is not to be used as a default router. These limits may be overridden by
35  * specific documents that describe how IPv6 operates over different link layers. For instance, in a
36  * point-to-point link the peers may have enough information about the number and status of devices at
37  * the other end so that advertisements are needed less frequently.
38  * Default: 3 * MaxRtrAdvInterval */
39 #define RADV_MIN_ROUTER_LIFETIME_USEC             RADV_MIN_MAX_TIMEOUT_USEC
40 #define RADV_MAX_ROUTER_LIFETIME_USEC             (9000 * USEC_PER_SEC)
41 #define RADV_DEFAULT_ROUTER_LIFETIME_USEC         (3 * RADV_DEFAULT_MAX_TIMEOUT_USEC)
42 /* draft-ietf-6man-slaac-renum-02 section 4.1.1.
43  * AdvPreferredLifetime: max(AdvDefaultLifetime, 3 * MaxRtrAdvInterval)
44  * AdvValidLifetime: 2 * AdvPreferredLifetime */
45 #define RADV_DEFAULT_PREFERRED_LIFETIME_USEC      CONST_MAX(RADV_DEFAULT_ROUTER_LIFETIME_USEC, 3 * RADV_DEFAULT_MAX_TIMEOUT_USEC)
46 #define RADV_DEFAULT_VALID_LIFETIME_USEC          (2 * RADV_DEFAULT_PREFERRED_LIFETIME_USEC)
47 /* RFC 4861 section 10.
48  * MAX_INITIAL_RTR_ADVERT_INTERVAL  16 seconds
49  * MAX_INITIAL_RTR_ADVERTISEMENTS    3 transmissions
50  * MAX_FINAL_RTR_ADVERTISEMENTS      3 transmissions
51  * MIN_DELAY_BETWEEN_RAS             3 seconds
52  * MAX_RA_DELAY_TIME                .5 seconds */
53 #define RADV_MAX_INITIAL_RTR_ADVERT_INTERVAL_USEC (16 * USEC_PER_SEC)
54 #define RADV_MAX_INITIAL_RTR_ADVERTISEMENTS       3
55 #define RADV_MAX_FINAL_RTR_ADVERTISEMENTS         3
56 #define RADV_MIN_DELAY_BETWEEN_RAS                3
57 #define RADV_MAX_RA_DELAY_TIME_USEC               (500 * USEC_PER_MSEC)
58 
59 #define RADV_OPT_ROUTE_INFORMATION                24
60 #define RADV_OPT_RDNSS                            25
61 #define RADV_OPT_DNSSL                            31
62 
63 enum RAdvState {
64         RADV_STATE_IDLE                      = 0,
65         RADV_STATE_ADVERTISING               = 1,
66 };
67 typedef enum RAdvState RAdvState;
68 
69 struct sd_radv_opt_dns {
70         uint8_t type;
71         uint8_t length;
72         uint16_t reserved;
73         be32_t lifetime;
74 } _packed_;
75 
76 struct sd_radv {
77         unsigned n_ref;
78         RAdvState state;
79 
80         int ifindex;
81         char *ifname;
82 
83         sd_event *event;
84         int event_priority;
85 
86         struct ether_addr mac_addr;
87         uint8_t hop_limit;
88         uint8_t flags;
89         uint32_t mtu;
90         usec_t lifetime_usec; /* timespan */
91 
92         int fd;
93         unsigned ra_sent;
94         sd_event_source *recv_event_source;
95         sd_event_source *timeout_event_source;
96 
97         unsigned n_prefixes;
98         LIST_HEAD(sd_radv_prefix, prefixes);
99 
100         unsigned n_route_prefixes;
101         LIST_HEAD(sd_radv_route_prefix, route_prefixes);
102 
103         size_t n_rdnss;
104         struct sd_radv_opt_dns *rdnss;
105         struct sd_radv_opt_dns *dnssl;
106 };
107 
108 #define radv_prefix_opt__contents {             \
109         uint8_t type;                           \
110         uint8_t length;                         \
111         uint8_t prefixlen;                      \
112         uint8_t flags;                          \
113         be32_t lifetime_valid;                  \
114         be32_t lifetime_preferred;              \
115         uint32_t reserved;                      \
116         struct in6_addr in6_addr;               \
117 }
118 
119 struct radv_prefix_opt radv_prefix_opt__contents;
120 
121 /* We need the opt substructure to be packed, because we use it in send(). But
122  * if we use _packed_, this means that the structure cannot be used directly in
123  * normal code in general, because the fields might not be properly aligned.
124  * But in this particular case, the structure is defined in a way that gives
125  * proper alignment, even without the explicit _packed_ attribute. To appease
126  * the compiler we use the "unpacked" structure, but we also verify that
127  * structure contains no holes, so offsets are the same when _packed_ is used.
128  */
129 struct radv_prefix_opt__packed radv_prefix_opt__contents _packed_;
130 assert_cc(sizeof(struct radv_prefix_opt) == sizeof(struct radv_prefix_opt__packed));
131 
132 struct sd_radv_prefix {
133         unsigned n_ref;
134 
135         struct radv_prefix_opt opt;
136 
137         LIST_FIELDS(struct sd_radv_prefix, prefix);
138 
139         /* These are timespans, NOT points in time. */
140         usec_t lifetime_valid_usec;
141         usec_t lifetime_preferred_usec;
142         /* These are points in time specified with clock_boottime_or_monotonic(), NOT timespans. */
143         usec_t valid_until;
144         usec_t preferred_until;
145 };
146 
147 #define radv_route_prefix_opt__contents {       \
148         uint8_t type;                           \
149         uint8_t length;                         \
150         uint8_t prefixlen;                      \
151         uint8_t flags_reserved;                 \
152         be32_t  lifetime;                       \
153         struct in6_addr in6_addr;               \
154 }
155 
156 struct radv_route_prefix_opt radv_route_prefix_opt__contents;
157 
158 struct radv_route_prefix_opt__packed radv_route_prefix_opt__contents _packed_;
159 assert_cc(sizeof(struct radv_route_prefix_opt) == sizeof(struct radv_route_prefix_opt__packed));
160 
161 struct sd_radv_route_prefix {
162         unsigned n_ref;
163 
164         struct radv_route_prefix_opt opt;
165 
166         LIST_FIELDS(struct sd_radv_route_prefix, prefix);
167 
168         /* This is a timespan, NOT a point in time. */
169         usec_t lifetime_usec;
170         /* This is a point in time specified with clock_boottime_or_monotonic(), NOT a timespan. */
171         usec_t valid_until;
172 };
173 
174 #define log_radv_errno(radv, error, fmt, ...)           \
175         log_interface_prefix_full_errno(                \
176                 "RADV: ",                               \
177                 sd_radv, radv,                          \
178                 error, fmt, ##__VA_ARGS__)
179 #define log_radv(radv, fmt, ...)                        \
180         log_interface_prefix_full_errno_zerook(         \
181                 "RADV: ",                               \
182                 sd_radv, radv,                          \
183                 0, fmt, ##__VA_ARGS__)
184