Line | Count | Source |
1 | | /* |
2 | | * |
3 | | * Authors: |
4 | | * Pedro Roque <roque@di.fc.ul.pt> |
5 | | * Lars Fenneberg <lf@elemental.net> |
6 | | * |
7 | | * This software is Copyright 1996,1997 by the above mentioned author(s), |
8 | | * All Rights Reserved. |
9 | | * |
10 | | * The license which is distributed with this software in the file COPYRIGHT |
11 | | * applies to this software. If your distribution is missing this file, you |
12 | | * may request it from https://github.com/radvd-project/radvd/issues |
13 | | * |
14 | | */ |
15 | | |
16 | | #include "config.h" |
17 | | #include "includes.h" |
18 | | #include "radvd.h" |
19 | | #include "netlink.h" |
20 | | |
21 | | static int really_send(int sock, struct in6_addr const *dest, struct properties const *props, struct safe_buffer const *sb); |
22 | | static int send_ra(int sock, struct Interface *iface, struct in6_addr const *dest); |
23 | | static struct safe_buffer_list *build_ra_options(struct Interface const *iface, struct in6_addr const *dest); |
24 | | |
25 | | static int ensure_iface_setup(int sock, struct Interface *iface); |
26 | | static void decrement_lifetime(const time_t secs, uint32_t *lifetime); |
27 | | static void update_iface_times(struct Interface *iface); |
28 | | |
29 | | // Option helpers |
30 | | static size_t serialize_domain_names(struct safe_buffer *safe_buffer, struct AdvDNSSL const *dnssl); |
31 | | static int get_prefix_lifetimes(struct AdvPrefix const *prefix, unsigned int *valid_lft, unsigned int *preferred_lft); |
32 | | static void limit_prefix_lifetimes(struct AdvPrefix *prefix); |
33 | | |
34 | | // Options that only need a single block |
35 | | static void add_ra_header(struct safe_buffer *sb, struct ra_header_info const *ra_header_info, int cease_adv); |
36 | | static void add_ra_option_prefix(struct safe_buffer *sb, struct AdvPrefix const *prefix, int cease_adv); |
37 | | static void add_ra_option_mtu(struct safe_buffer *sb, uint32_t AdvLinkMTU); |
38 | | static void add_ra_option_sllao(struct safe_buffer *sb, struct sllao const *sllao); |
39 | | static void add_ra_option_mipv6_rtr_adv_interval(struct safe_buffer *sb, double MaxRtrAdvInterval); |
40 | | static void add_ra_option_mipv6_home_agent_info(struct safe_buffer *sb, struct mipv6 const *mipv6); |
41 | | static void add_ra_option_lowpanco(struct safe_buffer *sb, struct AdvLowpanCo const *lowpanco); |
42 | | static void add_ra_option_abro(struct safe_buffer *sb, struct AdvAbro const *abroo); |
43 | | static void add_ra_option_capport(struct safe_buffer *sb, const char *captive_portal); |
44 | | |
45 | | // Options that generate 0 or more blocks |
46 | | static struct safe_buffer_list *add_ra_options_prefix(struct safe_buffer_list *sbl, struct Interface const *iface, |
47 | | char const *ifname, struct AdvPrefix const *prefix, int cease_adv, |
48 | | struct in6_addr const *dest); |
49 | | static struct safe_buffer_list *add_ra_options_nat64prefix(struct safe_buffer_list *sbl, struct NAT64Prefix const *prefix); |
50 | | static struct safe_buffer_list *add_ra_options_route(struct safe_buffer_list *sbl, struct Interface const *iface, |
51 | | struct AdvRoute const *route, int cease_adv, struct in6_addr const *dest); |
52 | | static struct safe_buffer_list *add_ra_options_rdnss(struct safe_buffer_list *sbl, struct Interface const *iface, |
53 | | struct AdvRDNSS const *rdnss, int cease_adv, struct in6_addr const *dest); |
54 | | static struct safe_buffer_list *add_ra_options_dnssl(struct safe_buffer_list *sbl, struct Interface const *iface, |
55 | | struct AdvDNSSL const *dnssl, int cease_adv, struct in6_addr const *dest); |
56 | | |
57 | | // Scheduling of options per RFC7772 |
58 | | static int schedule_helper(struct in6_addr const *dest, struct Interface const *iface, int option_lifetime); |
59 | | static int schedule_option_prefix(struct in6_addr const *dest, struct Interface const *iface, struct AdvPrefix const *prefix); |
60 | | static int schedule_option_route(struct in6_addr const *dest, struct Interface const *iface, struct AdvRoute const *route); |
61 | | static int schedule_option_rdnss(struct in6_addr const *dest, struct Interface const *iface, struct AdvRDNSS const *rdnss); |
62 | | static int schedule_option_dnssl(struct in6_addr const *dest, struct Interface const *iface, struct AdvDNSSL const *dnssl); |
63 | | static int schedule_option_mtu(struct in6_addr const *dest, struct Interface const *iface); |
64 | | static int schedule_option_sllao(struct in6_addr const *dest, struct Interface const *iface); |
65 | | static int schedule_option_mipv6_rtr_adv_interval(struct in6_addr const *dest, struct Interface const *iface); |
66 | | static int schedule_option_mipv6_home_agent_info(struct in6_addr const *dest, struct Interface const *iface); |
67 | | static int schedule_option_lowpanco(struct in6_addr const *dest, struct Interface const *iface); |
68 | | static int schedule_option_abro(struct in6_addr const *dest, struct Interface const *iface); |
69 | | static int schedule_option_capport(struct in6_addr const *dest, struct Interface const *iface); |
70 | | |
71 | | #ifdef UNIT_TEST |
72 | | #include "test/send.c" |
73 | | #endif |
74 | | |
75 | | /* |
76 | | * Sends an advertisement for all specified clients of this interface |
77 | | * (or via broadcast, if there are no restrictions configured). |
78 | | * |
79 | | * If a destination address is given, the RA will be sent to the destination |
80 | | * address only, but only if it was configured. |
81 | | * |
82 | | */ |
83 | | int send_ra_forall(int sock, struct Interface *iface, struct in6_addr *dest) |
84 | 25 | { |
85 | | /* when netlink is not available (disabled or BSD), ensure_iface_setup is necessary. */ |
86 | 25 | if (ensure_iface_setup(sock, iface) < 0) { |
87 | 25 | dlog(LOG_DEBUG, 3, "not sending RA for %s, interface is not ready", iface->props.name); |
88 | 25 | return -1; |
89 | 25 | } |
90 | | |
91 | | // Ignore unicast request/response - otherwise rapid unicast |
92 | | // requests during startup can cause multicast/broadcast RAs to *NOT* be |
93 | | // sent on the desired schedule. |
94 | | // racount is consumed in interface.c to calculate when to send the |
95 | | // next non-unicast RA. |
96 | 0 | if (iface->state_info.racount < MAX_INITIAL_RTR_ADVERTISEMENTS && dest == NULL) |
97 | 0 | iface->state_info.racount++; |
98 | | |
99 | | /* If no list of clients was specified for this interface, we broadcast */ |
100 | 0 | if (iface->ClientList == NULL) { |
101 | 0 | if (dest == NULL && iface->UnicastOnly) { |
102 | 0 | dlog(LOG_DEBUG, 5, "no client list, no destination, unicast only...doing nothing"); |
103 | 0 | return 0; |
104 | 0 | } |
105 | 0 | return send_ra(sock, iface, dest); |
106 | 0 | } |
107 | | |
108 | | /* If clients are configured, send the advertisement to all of them via unicast */ |
109 | 0 | for (struct Clients *current = iface->ClientList; current; current = current->next) { |
110 | | /* If a non-authorized client sent a solicitation, ignore it (logging later) */ |
111 | 0 | if (dest != NULL && memcmp(dest, ¤t->Address, sizeof(struct in6_addr)) != 0) |
112 | 0 | continue; |
113 | | |
114 | | /* Clients that should be ignored */ |
115 | 0 | if (current->ignored) { |
116 | | /* Don't allow fallback to UnrestrictedUnicast for direct queries */ |
117 | 0 | if (dest != NULL) |
118 | 0 | return 0; |
119 | | |
120 | 0 | continue; |
121 | 0 | } |
122 | | |
123 | 0 | send_ra(sock, iface, &(current->Address)); |
124 | | |
125 | | /* If we should only send the RA to a specific address, we are done */ |
126 | 0 | if (dest != NULL) |
127 | 0 | return 0; |
128 | 0 | } |
129 | | |
130 | 0 | if (dest == NULL) |
131 | 0 | return 0; |
132 | | |
133 | | /* Reply with advertisement to unlisted clients */ |
134 | 0 | if (iface->UnrestrictedUnicast) { |
135 | 0 | return send_ra(sock, iface, dest); |
136 | 0 | } |
137 | | |
138 | | /* If we refused a client's solicitation, log it if debugging is high enough */ |
139 | 0 | if (get_debuglevel() >= 5) { |
140 | 0 | char address_text[INET6_ADDRSTRLEN] = {""}; |
141 | 0 | addrtostr(dest, address_text, INET6_ADDRSTRLEN); |
142 | 0 | dlog(LOG_DEBUG, 5, "Not answering request from %s, not configured", address_text); |
143 | 0 | } |
144 | |
|
145 | 0 | return 0; |
146 | 0 | } |
147 | | |
148 | | /******************************************************************************** |
149 | | * support functions * |
150 | | ********************************************************************************/ |
151 | | |
152 | | static int ensure_iface_setup(int sock, struct Interface *iface) |
153 | 25 | { |
154 | | #ifdef HAVE_NETLINK |
155 | | if (iface->state_info.changed) |
156 | | setup_iface(sock, iface); |
157 | | #else |
158 | 25 | setup_iface(sock, iface); |
159 | 25 | #endif |
160 | | |
161 | 25 | return (iface->state_info.ready ? 0 : -1); |
162 | 25 | } |
163 | | |
164 | | static void decrement_lifetime(const time_t secs, uint32_t *lifetime) |
165 | 0 | { |
166 | 0 | if (*lifetime > secs) { |
167 | 0 | *lifetime -= secs; |
168 | 0 | } else { |
169 | 0 | *lifetime = 0; |
170 | 0 | } |
171 | 0 | } |
172 | | |
173 | | static void update_iface_times(struct Interface *iface) |
174 | 0 | { |
175 | 0 | struct timespec last_time = iface->times.last_ra_time; |
176 | 0 | clock_gettime(CLOCK_MONOTONIC, &iface->times.last_ra_time); |
177 | 0 | time_t secs_since_last_ra = timespecdiff(&iface->times.last_ra_time, &last_time) / 1000; |
178 | |
|
179 | 0 | if (secs_since_last_ra < 0) { |
180 | 0 | secs_since_last_ra = 0; |
181 | 0 | flog(LOG_WARNING, "clock_gettime(CLOCK_MONOTONIC) went backwards!"); |
182 | 0 | } |
183 | |
|
184 | 0 | struct AdvPrefix *prefix = iface->AdvPrefixList; |
185 | 0 | while (prefix) { |
186 | 0 | if (!prefix->DecrementLifetimesFlag || prefix->curr_preferredlft > 0) { |
187 | 0 | if (!(iface->state_info.cease_adv && prefix->DeprecatePrefixFlag)) { |
188 | 0 | if (prefix->DecrementLifetimesFlag) { |
189 | |
|
190 | 0 | decrement_lifetime(secs_since_last_ra, &prefix->curr_validlft); |
191 | 0 | decrement_lifetime(secs_since_last_ra, &prefix->curr_preferredlft); |
192 | |
|
193 | 0 | if (prefix->curr_preferredlft == 0) { |
194 | 0 | char pfx_str[INET6_ADDRSTRLEN]; |
195 | 0 | addrtostr(&prefix->Prefix, pfx_str, sizeof(pfx_str)); |
196 | 0 | dlog(LOG_DEBUG, 3, "Will cease advertising %s/%u%%%s, preferred lifetime is 0", |
197 | 0 | pfx_str, prefix->PrefixLen, iface->props.name); |
198 | 0 | } |
199 | 0 | } |
200 | 0 | } |
201 | 0 | } |
202 | 0 | prefix = prefix->next; |
203 | 0 | } |
204 | 0 | } |
205 | | |
206 | | /******************************************************************************** |
207 | | * add_ra_* * |
208 | | ********************************************************************************/ |
209 | | |
210 | | static void add_ra_header(struct safe_buffer *sb, struct ra_header_info const *ra_header_info, int cease_adv) |
211 | 0 | { |
212 | 0 | struct nd_router_advert radvert; |
213 | |
|
214 | 0 | memset(&radvert, 0, sizeof(radvert)); |
215 | |
|
216 | 0 | radvert.nd_ra_type = ND_ROUTER_ADVERT; |
217 | 0 | radvert.nd_ra_code = 0; |
218 | 0 | radvert.nd_ra_cksum = 0; |
219 | 0 | radvert.nd_ra_curhoplimit = ra_header_info->AdvCurHopLimit; |
220 | 0 | radvert.nd_ra_flags_reserved = (ra_header_info->AdvManagedFlag) ? ND_RA_FLAG_MANAGED : 0; |
221 | 0 | radvert.nd_ra_flags_reserved |= (ra_header_info->AdvOtherConfigFlag) ? ND_RA_FLAG_OTHER : 0; |
222 | | /* Mobile IPv6 ext */ |
223 | 0 | radvert.nd_ra_flags_reserved |= (ra_header_info->AdvHomeAgentFlag) ? ND_RA_FLAG_HOME_AGENT : 0; |
224 | 0 | radvert.nd_ra_flags_reserved |= (ra_header_info->AdvSNACRouterFlag) ? ND_RA_FLAG_SNAC_ROUTER : 0; |
225 | |
|
226 | 0 | radvert.nd_ra_router_lifetime = cease_adv ? 0 : htons(ra_header_info->AdvDefaultLifetime); |
227 | 0 | radvert.nd_ra_flags_reserved |= (ra_header_info->AdvDefaultPreference << ND_OPT_RI_PRF_SHIFT) & ND_OPT_RI_PRF_MASK; |
228 | |
|
229 | 0 | radvert.nd_ra_reachable = htonl(ra_header_info->AdvReachableTime); |
230 | 0 | radvert.nd_ra_retransmit = htonl(ra_header_info->AdvRetransTimer); |
231 | |
|
232 | 0 | safe_buffer_append(sb, &radvert, sizeof(radvert)); |
233 | 0 | } |
234 | | |
235 | | static void add_ra_option_prefix(struct safe_buffer *sb, struct AdvPrefix const *prefix, int cease_adv) |
236 | 0 | { |
237 | 0 | struct nd_opt_prefix_info pinfo; |
238 | |
|
239 | 0 | memset(&pinfo, 0, sizeof(pinfo)); |
240 | |
|
241 | 0 | pinfo.nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION; |
242 | 0 | pinfo.nd_opt_pi_len = 4; |
243 | 0 | pinfo.nd_opt_pi_prefix_len = prefix->PrefixLen; |
244 | |
|
245 | 0 | pinfo.nd_opt_pi_flags_reserved = (prefix->AdvOnLinkFlag) ? ND_OPT_PI_FLAG_ONLINK : 0; |
246 | 0 | pinfo.nd_opt_pi_flags_reserved |= (prefix->AdvAutonomousFlag) ? ND_OPT_PI_FLAG_AUTO : 0; |
247 | | /* Mobile IPv6 ext */ |
248 | 0 | pinfo.nd_opt_pi_flags_reserved |= (prefix->AdvRouterAddr) ? ND_OPT_PI_FLAG_RADDR : 0; |
249 | 0 | pinfo.nd_opt_pi_flags_reserved |= (prefix->AdvDHCPv6PDPreferredFlag) ? ND_OPT_PI_FLAG_DHCPv6_PD_PREF : 0; |
250 | |
|
251 | 0 | if (cease_adv && prefix->DeprecatePrefixFlag) { |
252 | | /* RFC4862, 5.5.3, step e) */ |
253 | 0 | if (prefix->curr_validlft < MIN_AdvValidLifetime) { |
254 | 0 | pinfo.nd_opt_pi_valid_time = htonl(prefix->curr_validlft); |
255 | 0 | } else { |
256 | 0 | pinfo.nd_opt_pi_valid_time = htonl(MIN_AdvValidLifetime); |
257 | 0 | } |
258 | 0 | pinfo.nd_opt_pi_preferred_time = 0; |
259 | 0 | } else { |
260 | 0 | pinfo.nd_opt_pi_valid_time = htonl(prefix->curr_validlft); |
261 | 0 | pinfo.nd_opt_pi_preferred_time = htonl(prefix->curr_preferredlft); |
262 | 0 | } |
263 | |
|
264 | 0 | memcpy(&pinfo.nd_opt_pi_prefix, &prefix->Prefix, sizeof(struct in6_addr)); |
265 | |
|
266 | 0 | safe_buffer_append(sb, &pinfo, sizeof(pinfo)); |
267 | 0 | } |
268 | | |
269 | 0 | static int get_prefix_lifetimes (struct AdvPrefix const *prefix, unsigned int *valid_lft, unsigned int *preferred_lft) { |
270 | 0 | unsigned int preferred = 0; |
271 | 0 | unsigned int valid = 0; |
272 | 0 | int ret = 0; |
273 | |
|
274 | | #ifdef HAVE_NETLINK |
275 | | /* Retrieve valid and current lifetimes of the prefix */ |
276 | | ret = netlink_get_address_lifetimes (prefix, &preferred, &valid); |
277 | | #endif |
278 | |
|
279 | 0 | *valid_lft = valid; |
280 | 0 | *preferred_lft = preferred; |
281 | 0 | return ret; |
282 | 0 | } |
283 | | |
284 | 0 | static void limit_prefix_lifetimes(struct AdvPrefix *prefix) { |
285 | 0 | unsigned int valid, preferred; |
286 | 0 | int ret = get_prefix_lifetimes (prefix, &valid, &preferred); |
287 | | /* Retrieve valid and current lifetimes of the prefix */ |
288 | 0 | if(ret) { |
289 | 0 | prefix->curr_validlft = min(valid, prefix->curr_validlft); |
290 | 0 | prefix->curr_preferredlft = min(preferred, prefix->curr_preferredlft); |
291 | 0 | } |
292 | 0 | } |
293 | | |
294 | | static void add_ra_option_nat64prefix(struct safe_buffer *sb, struct NAT64Prefix const *prefix) |
295 | 0 | { |
296 | 0 | struct nd_opt_nat64prefix_info pinfo; |
297 | 0 | uint8_t prefix_length_code = 0; |
298 | |
|
299 | 0 | memset(&pinfo, 0, sizeof(pinfo)); |
300 | |
|
301 | 0 | pinfo.nd_opt_pi_type = ND_OPT_PREF64; |
302 | 0 | pinfo.nd_opt_pi_len = 2; |
303 | | /* |
304 | | PLC (Prefix Length Code): 3-bit unsigned integer. This field |
305 | | encodes the NAT64 Prefix Length defined in [RFC6052]. The PLC |
306 | | field values 0, 1, 2, 3, 4, and 5 indicate the NAT64 prefix length |
307 | | of 96, 64, 56, 48, 40, and 32 bits, respectively. The receiver |
308 | | MUST ignore the PREF64 option if the Prefix Length Code field is |
309 | | not set to one of those values. |
310 | | */ |
311 | 0 | switch (prefix->PrefixLen) { |
312 | 0 | case 96: |
313 | 0 | prefix_length_code = 0; |
314 | 0 | break; |
315 | 0 | case 64: |
316 | 0 | prefix_length_code = 1; |
317 | 0 | break; |
318 | 0 | case 56: |
319 | 0 | prefix_length_code = 2; |
320 | 0 | break; |
321 | 0 | case 48: |
322 | 0 | prefix_length_code = 3; |
323 | 0 | break; |
324 | 0 | case 40: |
325 | 0 | prefix_length_code = 4; |
326 | 0 | break; |
327 | 0 | case 32: |
328 | 0 | prefix_length_code = 5; |
329 | 0 | break; |
330 | 0 | } |
331 | | |
332 | | /* |
333 | | Scaled Lifetime: 13-bit unsigned integer. The maximum time in units |
334 | | of 8 seconds over which this NAT64 prefix MAY be used. See |
335 | | Section 4.1 for the Scaled Lifetime field processing rules. |
336 | | |
337 | | Router vendors SHOULD allow administrators to specify nonzero |
338 | | lifetime values that are not divisible by 8. In such cases, the |
339 | | router SHOULD round the provided value up to the nearest integer that |
340 | | is divisible by 8 and smaller than 65536, then divide the result by 8 |
341 | | (or perform a logical right shift by 3) and set the Scaled Lifetime |
342 | | field to the resulting value. If a nonzero lifetime value that is to |
343 | | be divided by 8 (or subjected to a logical right shift by 3) is less |
344 | | than 8, then the Scaled Lifetime field SHOULD be set to 1. This last |
345 | | step ensures that lifetimes under 8 seconds are encoded as a nonzero |
346 | | Scaled Lifetime. |
347 | | */ |
348 | 0 | pinfo.nd_opt_pi_lifetime_preflen = htons( |
349 | 0 | ((prefix->curr_validlft + 7) & 0xFFF8) | |
350 | 0 | (prefix_length_code & 0x7)); |
351 | | |
352 | | /* Only copy 96 bits of the prefix */ |
353 | 0 | memcpy(&pinfo.nd_opt_pi_nat64prefix, &prefix->Prefix, 12); |
354 | |
|
355 | 0 | safe_buffer_append(sb, &pinfo, sizeof(pinfo)); |
356 | 0 | } |
357 | | |
358 | | static struct safe_buffer_list *add_auto_prefixes_6to4(struct safe_buffer_list *sbl, struct Interface const *iface, |
359 | | char const *ifname, struct AdvPrefix const *prefix, int cease_adv, |
360 | | struct in6_addr const *dest) |
361 | 0 | { |
362 | 0 | #ifdef HAVE_IFADDRS_H |
363 | 0 | struct AdvPrefix xprefix = *prefix; |
364 | 0 | unsigned int dst; |
365 | |
|
366 | 0 | if (get_v4addr(prefix->if6to4, &dst) < 0) { |
367 | 0 | flog(LOG_ERR, "Base6to4interface %s has no IPv4 addresses", prefix->if6to4); |
368 | 0 | } else { |
369 | 0 | memcpy(xprefix.Prefix.s6_addr + 2, &dst, sizeof(dst)); |
370 | 0 | *((uint16_t *)(xprefix.Prefix.s6_addr)) = htons(0x2002); |
371 | 0 | xprefix.PrefixLen = 64; |
372 | |
|
373 | 0 | char pfx_str[INET6_ADDRSTRLEN]; |
374 | 0 | addrtostr(&xprefix.Prefix, pfx_str, sizeof(pfx_str)); |
375 | 0 | dlog(LOG_DEBUG, 3, "auto-selected prefix %s/%d on interface %s", pfx_str, xprefix.PrefixLen, ifname); |
376 | | |
377 | | /** We want to get the lowest value out of the configured lifetime (from /etc/radvd.conf) and the maximum lifetime on |
378 | | * any address that is part of that prefix in the kernel to avoid advertising a prefix that might expire too soon */ |
379 | | // TODO: audit clobbers of prefixes based on original config? |
380 | 0 | limit_prefix_lifetimes(&xprefix); |
381 | |
|
382 | 0 | if (cease_adv || schedule_option_prefix(dest, iface, &xprefix)) { |
383 | 0 | sbl = safe_buffer_list_append(sbl); |
384 | 0 | add_ra_option_prefix(sbl->sb, &xprefix, cease_adv); |
385 | 0 | } |
386 | 0 | } |
387 | 0 | #endif |
388 | 0 | return sbl; |
389 | 0 | } |
390 | | |
391 | | static struct safe_buffer_list *add_auto_prefixes(struct safe_buffer_list *sbl, struct Interface const *iface, char const *ifname, |
392 | | struct AdvPrefix const *prefix, int cease_adv, struct in6_addr const *dest) |
393 | 0 | { |
394 | 0 | #ifdef HAVE_IFADDRS_H |
395 | 0 | struct AdvPrefix xprefix; |
396 | 0 | struct ifaddrs *ifap = 0, *ifa = 0; |
397 | |
|
398 | 0 | if (getifaddrs(&ifap) != 0) |
399 | 0 | flog(LOG_ERR, "getifaddrs failed: %s", strerror(errno)); |
400 | |
|
401 | 0 | for (ifa = ifap; ifa; ifa = ifa->ifa_next) { |
402 | |
|
403 | 0 | if (strncmp(ifa->ifa_name, ifname, IFNAMSIZ)) |
404 | 0 | continue; |
405 | | |
406 | 0 | if (ifa->ifa_addr == NULL) { |
407 | 0 | flog(LOG_WARNING, "ifa_addr == NULL for dev %s !? Ignoring in add_auto_prefixes", ifname); |
408 | 0 | continue; |
409 | 0 | } |
410 | | |
411 | 0 | if (ifa->ifa_addr->sa_family != AF_INET6) |
412 | 0 | continue; |
413 | | |
414 | 0 | struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)ifa->ifa_addr; |
415 | 0 | struct sockaddr_in6 *mask = (struct sockaddr_in6 *)ifa->ifa_netmask; |
416 | |
|
417 | 0 | if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) |
418 | 0 | continue; |
419 | | |
420 | 0 | struct in6_addr prefix6 = get_prefix6(&s6->sin6_addr, &mask->sin6_addr); |
421 | |
|
422 | 0 | int ignore = 0; |
423 | 0 | for (struct AutogenIgnorePrefix *current = iface->IgnorePrefixList; current; current = current->next) { |
424 | 0 | struct in6_addr candidatePrefix6 = get_prefix6(¤t->Prefix, ¤t->Mask); |
425 | |
|
426 | 0 | if (memcmp(&prefix6, &candidatePrefix6, sizeof(struct in6_addr)) == 0 && |
427 | 0 | memcmp(&mask->sin6_addr, ¤t->Mask, sizeof(struct in6_addr)) == 0) { |
428 | 0 | ignore = 1; |
429 | 0 | break; |
430 | 0 | } |
431 | 0 | } |
432 | |
|
433 | 0 | if (ignore) |
434 | 0 | continue; |
435 | | |
436 | 0 | xprefix = *prefix; |
437 | 0 | xprefix.Prefix = prefix6; |
438 | 0 | xprefix.PrefixLen = count_mask(mask); |
439 | |
|
440 | 0 | char pfx_str[INET6_ADDRSTRLEN]; |
441 | 0 | addrtostr(&xprefix.Prefix, pfx_str, sizeof(pfx_str)); |
442 | 0 | dlog(LOG_DEBUG, 3, "auto-selected prefix %s/%d on interface %s", pfx_str, xprefix.PrefixLen, ifname); |
443 | | |
444 | | /** We want to get the lowest value out of the configured lifetime (from /etc/radvd.conf) and the maximum lifetime on |
445 | | * any address that is part of that prefix in the kernel to avoid advertising a prefix that might expire too soon */ |
446 | | // TODO: audit clobbers of prefixes based on original config? |
447 | 0 | limit_prefix_lifetimes(&xprefix); |
448 | |
|
449 | 0 | if (cease_adv || schedule_option_prefix(dest, iface, &xprefix)) { |
450 | 0 | sbl = safe_buffer_list_append(sbl); |
451 | 0 | add_ra_option_prefix(sbl->sb, &xprefix, cease_adv); |
452 | 0 | } |
453 | 0 | } |
454 | |
|
455 | 0 | if (ifap) |
456 | 0 | freeifaddrs(ifap); |
457 | 0 | #endif |
458 | 0 | return sbl; |
459 | 0 | } |
460 | | |
461 | | static struct safe_buffer_list *add_ra_options_nat64prefix(struct safe_buffer_list *sbl, struct NAT64Prefix const *prefix) |
462 | 0 | { |
463 | 0 | while (prefix) { |
464 | 0 | sbl = safe_buffer_list_append(sbl); |
465 | 0 | add_ra_option_nat64prefix(sbl->sb, prefix); |
466 | |
|
467 | 0 | prefix = prefix->next; |
468 | 0 | } |
469 | |
|
470 | 0 | return sbl; |
471 | 0 | } |
472 | | |
473 | | static struct safe_buffer_list *add_ra_options_prefix(struct safe_buffer_list *sbl, struct Interface const *iface, |
474 | | char const *ifname, struct AdvPrefix const *prefix, int cease_adv, |
475 | | struct in6_addr const *dest) |
476 | 0 | { |
477 | 0 | while (prefix) { |
478 | 0 | if (!prefix->DecrementLifetimesFlag || prefix->curr_preferredlft > 0) { |
479 | 0 | struct in6_addr zero = {}; |
480 | 0 | if (prefix->if6to4[0] || prefix->if6[0] || 0 == memcmp(&prefix->Prefix, &zero, sizeof(zero))) { |
481 | 0 | if (prefix->if6to4[0]) { |
482 | 0 | dlog(LOG_DEBUG, 4, "if6to4 auto prefix detected on iface %s", ifname); |
483 | 0 | sbl = add_auto_prefixes_6to4(sbl, iface, prefix->if6to4, prefix, cease_adv, dest); |
484 | 0 | } |
485 | 0 | if (prefix->if6[0]) { |
486 | 0 | dlog(LOG_DEBUG, 4, "if6 auto prefix detected on iface %s", ifname); |
487 | 0 | sbl = add_auto_prefixes(sbl, iface, prefix->if6, prefix, cease_adv, dest); |
488 | 0 | } |
489 | 0 | if (0 == memcmp(&prefix->Prefix, &zero, sizeof(zero))) { |
490 | 0 | dlog(LOG_DEBUG, 4, "::/64 auto prefix detected on iface %s", ifname); |
491 | 0 | sbl = add_auto_prefixes(sbl, iface, iface->props.name, prefix, cease_adv, dest); |
492 | 0 | } |
493 | 0 | } else { |
494 | 0 | if (cease_adv || schedule_option_prefix(dest, iface, prefix)) { |
495 | 0 | sbl = safe_buffer_list_append(sbl); |
496 | | |
497 | | /** We want to get the lowest value out of the configured lifetime (from /etc/radvd.conf) and the maximum lifetime on |
498 | | * any address that is part of that prefix in the kernel to avoid advertising a prefix that might expire too soon */ |
499 | | // TODO: audit clobbers of prefixes based on original config? |
500 | 0 | struct AdvPrefix xprefix = *prefix; |
501 | 0 | limit_prefix_lifetimes(&xprefix); |
502 | 0 | add_ra_option_prefix(sbl->sb, &xprefix, cease_adv); |
503 | 0 | } |
504 | 0 | } |
505 | 0 | } |
506 | |
|
507 | 0 | prefix = prefix->next; |
508 | 0 | } |
509 | 0 | return sbl; |
510 | 0 | } |
511 | | |
512 | | /* clang-format off */ |
513 | | /* |
514 | | * Domain Names of DNS Search List |
515 | | * One or more domain names of DNS Search List that MUST |
516 | | * be encoded using the technique described in Section |
517 | | * 3.1 of [RFC1035]. By this technique, each domain |
518 | | * name is represented as a sequence of labels ending in |
519 | | * a zero octet, defined as domain name representation. |
520 | | * For more than one domain name, the corresponding |
521 | | * domain name representations are concatenated as they |
522 | | * are. Note that for the simple decoding, the domain |
523 | | * names MUST NOT be encoded in a compressed form, as |
524 | | * described in Section 4.1.4 of [RFC1035]. Because the |
525 | | * size of this field MUST be a multiple of 8 octets, |
526 | | * for the minimum multiple including the domain name |
527 | | * representations, the remaining octets other than the |
528 | | * encoding parts of the domain name representations |
529 | | * MUST be padded with zeros. |
530 | | */ |
531 | | /* clang-format on */ |
532 | | static size_t serialize_domain_names(struct safe_buffer *safe_buffer, struct AdvDNSSL const *dnssl) |
533 | 0 | { |
534 | 0 | size_t len = 0; |
535 | |
|
536 | 0 | for (int i = 0; i < dnssl->AdvDNSSLNumber; i++) { |
537 | 0 | char *label = dnssl->AdvDNSSLSuffixes[i]; |
538 | |
|
539 | 0 | while (label[0] != '\0') { |
540 | 0 | unsigned char label_len; |
541 | |
|
542 | 0 | if (strchr(label, '.') == NULL) |
543 | 0 | label_len = (unsigned char)strlen(label); |
544 | 0 | else |
545 | 0 | label_len = (unsigned char)(strchr(label, '.') - label); |
546 | | |
547 | | // +8 is for null & padding, only allocate once. |
548 | 0 | safe_buffer_resize(safe_buffer, safe_buffer->used + sizeof(label_len) + label_len + 8); |
549 | 0 | len += safe_buffer_append(safe_buffer, &label_len, sizeof(label_len)); |
550 | 0 | len += safe_buffer_append(safe_buffer, label, label_len); |
551 | |
|
552 | 0 | label += label_len; |
553 | |
|
554 | 0 | if (label[0] == '.') { |
555 | 0 | label++; |
556 | 0 | } |
557 | |
|
558 | 0 | if (label[0] == '\0') { |
559 | 0 | char zero = 0; |
560 | 0 | len += safe_buffer_append(safe_buffer, &zero, sizeof(zero)); |
561 | 0 | } |
562 | 0 | } |
563 | 0 | } |
564 | 0 | return len; |
565 | 0 | } |
566 | | |
567 | | static struct safe_buffer_list *add_ra_options_route(struct safe_buffer_list *sbl, struct Interface const *iface, |
568 | | struct AdvRoute const *route, int cease_adv, struct in6_addr const *dest) |
569 | 0 | { |
570 | 0 | while (route) { |
571 | 0 | struct nd_opt_route_info_local rinfo; |
572 | |
|
573 | 0 | if (!cease_adv && !schedule_option_route(dest, iface, route)) { |
574 | 0 | route = route->next; |
575 | 0 | continue; |
576 | 0 | } |
577 | | |
578 | 0 | memset(&rinfo, 0, sizeof(rinfo)); |
579 | |
|
580 | 0 | rinfo.nd_opt_ri_type = ND_OPT_ROUTE_INFORMATION; |
581 | 0 | if (route->PrefixLen == 0) { |
582 | 0 | rinfo.nd_opt_ri_len = 1; |
583 | 0 | } else if (route->PrefixLen > 0 && route->PrefixLen <= 64) { |
584 | 0 | rinfo.nd_opt_ri_len = 2; |
585 | 0 | } else if (route->PrefixLen > 64 && route->PrefixLen <= 128) { |
586 | 0 | rinfo.nd_opt_ri_len = 3; |
587 | 0 | } |
588 | 0 | rinfo.nd_opt_ri_prefix_len = route->PrefixLen; |
589 | |
|
590 | 0 | rinfo.nd_opt_ri_flags_reserved = (route->AdvRoutePreference << ND_OPT_RI_PRF_SHIFT) & ND_OPT_RI_PRF_MASK; |
591 | 0 | if (cease_adv && route->RemoveRouteFlag) { |
592 | 0 | rinfo.nd_opt_ri_lifetime = 0; |
593 | 0 | } else { |
594 | 0 | rinfo.nd_opt_ri_lifetime = htonl(route->AdvRouteLifetime); |
595 | 0 | } |
596 | |
|
597 | 0 | memcpy(&rinfo.nd_opt_ri_prefix, &route->Prefix, sizeof(struct in6_addr)); |
598 | |
|
599 | 0 | sbl = safe_buffer_list_append(sbl); |
600 | 0 | safe_buffer_append(sbl->sb, &rinfo, rinfo.nd_opt_ri_len * 8); |
601 | |
|
602 | 0 | route = route->next; |
603 | 0 | } |
604 | 0 | return sbl; |
605 | 0 | } |
606 | | |
607 | | static struct safe_buffer_list *add_ra_options_rdnss(struct safe_buffer_list *sbl, struct Interface const *iface, |
608 | | struct AdvRDNSS const *rdnss, int cease_adv, struct in6_addr const *dest) |
609 | 0 | { |
610 | 0 | while (rdnss) { |
611 | 0 | struct nd_opt_rdnss_info_local rdnssinfo; |
612 | 0 | if (!cease_adv && !schedule_option_rdnss(dest, iface, rdnss)) { |
613 | 0 | rdnss = rdnss->next; |
614 | 0 | continue; |
615 | 0 | } |
616 | | |
617 | 0 | memset(&rdnssinfo, 0, sizeof(rdnssinfo)); |
618 | |
|
619 | 0 | size_t const bytes = sizeof(rdnssinfo) + sizeof(struct in6_addr) * rdnss->AdvRDNSSNumber; |
620 | 0 | size_t const nd_opt_dnssli_len = bytes/8; // deliberate size_t, not uint_8; padding is NOT required for RDNSS |
621 | | // dnsslinfo.nd_opt_rdnssi_len is uint8 count of 8-octet groups; min 3, max 255 |
622 | | // too many DNS servers could exceed it |
623 | | // https://datatracker.ietf.org/doc/html/rfc8106#section-5.1 |
624 | 0 | if(nd_opt_dnssli_len > 255) { |
625 | 0 | flog(LOG_ERR, |
626 | 0 | "Skipping option: RDNSS too long (%ld) for RA, must be <= %d bytes including header.", |
627 | 0 | bytes, (255*8)); |
628 | 0 | rdnss = rdnss->next; |
629 | 0 | continue; |
630 | 0 | } |
631 | | |
632 | 0 | rdnssinfo.nd_opt_rdnssi_type = ND_OPT_RDNSS_INFORMATION; |
633 | 0 | rdnssinfo.nd_opt_rdnssi_len = (uint8_t) nd_opt_dnssli_len; |
634 | 0 | rdnssinfo.nd_opt_rdnssi_pref_flag_reserved = 0; |
635 | |
|
636 | 0 | if (cease_adv && rdnss->FlushRDNSSFlag) { |
637 | 0 | rdnssinfo.nd_opt_rdnssi_lifetime = 0; |
638 | 0 | } else { |
639 | 0 | rdnssinfo.nd_opt_rdnssi_lifetime = htonl(rdnss->AdvRDNSSLifetime); |
640 | 0 | } |
641 | |
|
642 | 0 | sbl = safe_buffer_list_append(sbl); |
643 | 0 | safe_buffer_resize(sbl->sb, sbl->sb->used + bytes); |
644 | 0 | safe_buffer_append(sbl->sb, &rdnssinfo, sizeof(rdnssinfo)); |
645 | 0 | for (int i = 0; i < rdnss->AdvRDNSSNumber; i++) { |
646 | 0 | safe_buffer_append(sbl->sb, &rdnss->AdvRDNSSAddr[i], sizeof(struct in6_addr)); |
647 | 0 | } |
648 | | // padding is only required for DNSSL |
649 | |
|
650 | 0 | rdnss = rdnss->next; |
651 | 0 | } |
652 | |
|
653 | 0 | return sbl; |
654 | 0 | } |
655 | | |
656 | | static struct safe_buffer_list *add_ra_options_dnssl(struct safe_buffer_list *sbl, struct Interface const *iface, |
657 | | struct AdvDNSSL const *dnssl, int cease_adv, struct in6_addr const *dest) |
658 | 0 | { |
659 | 0 | struct safe_buffer *serialized_domains = new_safe_buffer(); |
660 | 0 | while (dnssl) { |
661 | |
|
662 | 0 | struct nd_opt_dnssl_info_local dnsslinfo; |
663 | |
|
664 | 0 | if (!cease_adv && !schedule_option_dnssl(dest, iface, dnssl)) { |
665 | 0 | dnssl = dnssl->next; |
666 | 0 | continue; |
667 | 0 | } |
668 | | |
669 | 0 | memset(&dnsslinfo, 0, sizeof(dnsslinfo)); |
670 | |
|
671 | 0 | serialized_domains->used = 0; |
672 | 0 | size_t const domain_name_bytes = serialize_domain_names(serialized_domains, dnssl); |
673 | 0 | size_t const bytes = sizeof(dnsslinfo) + domain_name_bytes; |
674 | 0 | size_t const nd_opt_dnssli_len = (bytes + 7) / 8; // deliberate size_t, not uint_8 |
675 | | // dnsslinfo.nd_opt_dnssli_len is uint8 count of 8-octet groups; min 3, max 255 |
676 | | // too many long serialized domains could exceed it |
677 | | // https://datatracker.ietf.org/doc/html/rfc8106#section-5.2 |
678 | 0 | size_t const bytes_with_padding = nd_opt_dnssli_len * 8; |
679 | 0 | if(nd_opt_dnssli_len > 255) { |
680 | 0 | flog(LOG_ERR, |
681 | 0 | "Skipping option: DNSSL too long (%ld) for RA, must be <= %d bytes including header and padding.", |
682 | 0 | bytes_with_padding, (255*8)); |
683 | 0 | dnssl = dnssl->next; |
684 | 0 | continue; |
685 | 0 | } |
686 | | |
687 | 0 | dnsslinfo.nd_opt_dnssli_type = ND_OPT_DNSSL_INFORMATION; |
688 | 0 | dnsslinfo.nd_opt_dnssli_len = (uint8_t) nd_opt_dnssli_len; |
689 | 0 | dnsslinfo.nd_opt_dnssli_reserved = 0; |
690 | |
|
691 | 0 | if (cease_adv && dnssl->FlushDNSSLFlag) { |
692 | 0 | dnsslinfo.nd_opt_dnssli_lifetime = 0; |
693 | 0 | } else { |
694 | 0 | dnsslinfo.nd_opt_dnssli_lifetime = htonl(dnssl->AdvDNSSLLifetime); |
695 | 0 | } |
696 | |
|
697 | 0 | size_t const padding = bytes_with_padding - bytes; |
698 | |
|
699 | 0 | sbl = safe_buffer_list_append(sbl); |
700 | 0 | safe_buffer_resize(sbl->sb, sbl->sb->used + sizeof(dnsslinfo) + domain_name_bytes + padding); |
701 | 0 | safe_buffer_append(sbl->sb, &dnsslinfo, sizeof(dnsslinfo)); |
702 | 0 | safe_buffer_append(sbl->sb, serialized_domains->buffer, serialized_domains->used); |
703 | 0 | safe_buffer_pad(sbl->sb, padding); |
704 | | // abort(); |
705 | |
|
706 | 0 | dnssl = dnssl->next; |
707 | 0 | } |
708 | 0 | safe_buffer_free(serialized_domains); |
709 | 0 | return sbl; |
710 | 0 | } |
711 | | |
712 | | /* |
713 | | * add Source Link-layer Address option |
714 | | */ |
715 | | static void add_ra_option_sllao(struct safe_buffer *sb, struct sllao const *sllao) |
716 | 0 | { |
717 | | /* +2 for the ND_OPT_SOURCE_LINKADDR and the length (each occupy one byte) */ |
718 | 0 | size_t const sllao_bytes = (sllao->if_hwaddr_len / 8) + 2; |
719 | 0 | size_t const sllao_len = (sllao_bytes + 7) / 8; |
720 | |
|
721 | 0 | uint8_t buff[2] = {ND_OPT_SOURCE_LINKADDR, (uint8_t)sllao_len}; |
722 | 0 | safe_buffer_append(sb, buff, sizeof(buff)); |
723 | | |
724 | | /* if_hwaddr_len is in bits, so divide by 8 to get the byte count. */ |
725 | 0 | safe_buffer_append(sb, sllao->if_hwaddr, sllao->if_hwaddr_len / 8); |
726 | 0 | safe_buffer_pad(sb, sllao_len * 8 - sllao_bytes); |
727 | 0 | } |
728 | | |
729 | | static void add_ra_option_mtu(struct safe_buffer *sb, uint32_t AdvLinkMTU) |
730 | 0 | { |
731 | 0 | struct nd_opt_mtu mtu; |
732 | |
|
733 | 0 | memset(&mtu, 0, sizeof(mtu)); |
734 | |
|
735 | 0 | mtu.nd_opt_mtu_type = ND_OPT_MTU; |
736 | 0 | mtu.nd_opt_mtu_len = 1; |
737 | 0 | mtu.nd_opt_mtu_reserved = 0; |
738 | 0 | mtu.nd_opt_mtu_mtu = htonl(AdvLinkMTU); |
739 | |
|
740 | 0 | safe_buffer_append(sb, &mtu, sizeof(mtu)); |
741 | 0 | } |
742 | | |
743 | | /* |
744 | | * Mobile IPv6 ext: Advertisement Interval Option to support |
745 | | * movement detection of mobile nodes |
746 | | */ |
747 | | static void add_ra_option_mipv6_rtr_adv_interval(struct safe_buffer *sb, double MaxRtrAdvInterval) |
748 | 0 | { |
749 | 0 | uint32_t ival = 1000; |
750 | |
|
751 | 0 | if (MaxRtrAdvInterval < Cautious_MaxRtrAdvInterval) { |
752 | 0 | ival *= MaxRtrAdvInterval + Cautious_MaxRtrAdvInterval_Leeway; |
753 | 0 | } else { |
754 | 0 | ival *= MaxRtrAdvInterval; |
755 | 0 | } |
756 | |
|
757 | 0 | struct AdvInterval a_ival; |
758 | |
|
759 | 0 | memset(&a_ival, 0, sizeof(a_ival)); |
760 | |
|
761 | 0 | a_ival.type = ND_OPT_RTR_ADV_INTERVAL; |
762 | 0 | a_ival.length = 1; |
763 | 0 | a_ival.reserved = 0; |
764 | 0 | a_ival.adv_ival = htonl(ival); |
765 | |
|
766 | 0 | safe_buffer_append(sb, &a_ival, sizeof(a_ival)); |
767 | 0 | } |
768 | | |
769 | | /* |
770 | | * Mobile IPv6 ext: Home Agent Information Option to support |
771 | | * Dynamic Home Agent Address Discovery |
772 | | */ |
773 | | static void add_ra_option_mipv6_home_agent_info(struct safe_buffer *sb, struct mipv6 const *mipv6) |
774 | 0 | { |
775 | 0 | struct HomeAgentInfo ha_info; |
776 | |
|
777 | 0 | memset(&ha_info, 0, sizeof(ha_info)); |
778 | |
|
779 | 0 | ha_info.type = ND_OPT_HOME_AGENT_INFO; |
780 | 0 | ha_info.length = 1; |
781 | 0 | ha_info.flags_reserved = (mipv6->AdvMobRtrSupportFlag) ? ND_OPT_HAI_FLAG_SUPPORT_MR : 0; |
782 | 0 | ha_info.preference = htons(mipv6->HomeAgentPreference); |
783 | 0 | ha_info.lifetime = htons(mipv6->HomeAgentLifetime); |
784 | |
|
785 | 0 | safe_buffer_append(sb, &ha_info, sizeof(ha_info)); |
786 | 0 | } |
787 | | |
788 | | /* |
789 | | * Add 6co option |
790 | | */ |
791 | | static void add_ra_option_lowpanco(struct safe_buffer *sb, struct AdvLowpanCo const *lowpanco) |
792 | 0 | { |
793 | 0 | struct nd_opt_6co co; |
794 | |
|
795 | 0 | memset(&co, 0, sizeof(co)); |
796 | |
|
797 | 0 | co.nd_opt_6co_type = ND_OPT_6CO; |
798 | 0 | co.nd_opt_6co_len = 3; |
799 | 0 | co.nd_opt_6co_context_len = lowpanco->ContextLength; |
800 | 0 | co.nd_opt_6co_res_c_cid = ((lowpanco->ContextCompressionFlag ? 1 : 0) << 4) |
801 | 0 | | (lowpanco->AdvContextID & 0x0F); |
802 | 0 | co.nd_opt_6co_valid_lifetime = htons(lowpanco->AdvLifeTime); |
803 | 0 | co.nd_opt_6co_con_prefix = lowpanco->AdvContextPrefix; |
804 | |
|
805 | 0 | safe_buffer_append(sb, &co, sizeof(co)); |
806 | 0 | } |
807 | | |
808 | | static void add_ra_option_abro(struct safe_buffer *sb, struct AdvAbro const *abroo) |
809 | 0 | { |
810 | 0 | struct nd_opt_abro abro; |
811 | |
|
812 | 0 | memset(&abro, 0, sizeof(abro)); |
813 | |
|
814 | 0 | abro.nd_opt_abro_type = ND_OPT_ABRO; |
815 | 0 | abro.nd_opt_abro_len = 3; |
816 | 0 | abro.nd_opt_abro_ver_low = htons(abroo->Version[1]); |
817 | 0 | abro.nd_opt_abro_ver_high = htons(abroo->Version[0]); |
818 | 0 | abro.nd_opt_abro_valid_lifetime = htons(abroo->ValidLifeTime); |
819 | 0 | abro.nd_opt_abro_6lbr_address = abroo->LBRaddress; |
820 | |
|
821 | 0 | safe_buffer_append(sb, &abro, sizeof(abro)); |
822 | 0 | } |
823 | | |
824 | | static void add_ra_option_capport(struct safe_buffer *sb, const char *captive_portal) |
825 | 0 | { |
826 | | /* +2 for the ND_OPT_CAPTIVE_PORTAL and the length (each occupy one byte) */ |
827 | 0 | size_t const capport_strlen = strlen(captive_portal); |
828 | 0 | size_t const capport_bytes = capport_strlen + 2; |
829 | 0 | size_t const capport_len = (capport_bytes + 7) / 8; |
830 | |
|
831 | 0 | uint8_t buff[2] = {ND_OPT_CAPTIVE_PORTAL, (uint8_t)capport_len}; |
832 | 0 | safe_buffer_append(sb, buff, sizeof(buff)); |
833 | |
|
834 | 0 | safe_buffer_append(sb, captive_portal, capport_strlen); |
835 | 0 | safe_buffer_pad(sb, (capport_len * 8) - capport_bytes); |
836 | 0 | } |
837 | | |
838 | | static struct safe_buffer_list *build_ra_options(struct Interface const *iface, struct in6_addr const *dest) |
839 | 0 | { |
840 | 0 | struct safe_buffer_list *sbl = new_safe_buffer_list(); |
841 | 0 | struct safe_buffer_list *cur = sbl; |
842 | |
|
843 | 0 | if (iface->AdvPrefixList) { |
844 | 0 | cur = |
845 | 0 | add_ra_options_prefix(cur, iface, iface->props.name, iface->AdvPrefixList, iface->state_info.cease_adv, dest); |
846 | 0 | } |
847 | |
|
848 | 0 | if (iface->NAT64PrefixList) { |
849 | 0 | cur = add_ra_options_nat64prefix(cur, iface->NAT64PrefixList); |
850 | 0 | } |
851 | |
|
852 | 0 | if (iface->AdvRouteList) { |
853 | 0 | cur = add_ra_options_route(cur, iface, iface->AdvRouteList, iface->state_info.cease_adv, dest); |
854 | 0 | } |
855 | |
|
856 | 0 | if (iface->AdvRDNSSList) { |
857 | 0 | cur = add_ra_options_rdnss(cur, iface, iface->AdvRDNSSList, iface->state_info.cease_adv, dest); |
858 | 0 | } |
859 | |
|
860 | 0 | if (iface->AdvDNSSLList) { |
861 | 0 | cur = add_ra_options_dnssl(cur, iface, iface->AdvDNSSLList, iface->state_info.cease_adv, dest); |
862 | 0 | } |
863 | |
|
864 | 0 | if (iface->AdvLinkMTU != 0 && schedule_option_mtu(dest, iface)) { |
865 | 0 | cur->next = new_safe_buffer_list(); |
866 | 0 | cur = cur->next; |
867 | 0 | add_ra_option_mtu(cur->sb, iface->AdvLinkMTU); |
868 | 0 | } |
869 | |
|
870 | 0 | if (iface->AdvSourceLLAddress && iface->sllao.if_hwaddr_len > 0 && schedule_option_sllao(dest, iface)) { |
871 | 0 | cur->next = new_safe_buffer_list(); |
872 | 0 | cur = cur->next; |
873 | 0 | add_ra_option_sllao(cur->sb, &iface->sllao); |
874 | 0 | } |
875 | |
|
876 | 0 | if (iface->mipv6.AdvIntervalOpt && schedule_option_mipv6_rtr_adv_interval(dest, iface)) { |
877 | 0 | cur->next = new_safe_buffer_list(); |
878 | 0 | cur = cur->next; |
879 | 0 | add_ra_option_mipv6_rtr_adv_interval(cur->sb, iface->MaxRtrAdvInterval); |
880 | 0 | } |
881 | |
|
882 | 0 | if (iface->mipv6.AdvHomeAgentInfo && schedule_option_mipv6_home_agent_info(dest, iface) && |
883 | 0 | (iface->mipv6.AdvMobRtrSupportFlag || iface->mipv6.HomeAgentPreference != 0 || |
884 | 0 | iface->mipv6.HomeAgentLifetime != iface->ra_header_info.AdvDefaultLifetime)) { |
885 | 0 | cur->next = new_safe_buffer_list(); |
886 | 0 | cur = cur->next; |
887 | 0 | add_ra_option_mipv6_home_agent_info(cur->sb, &iface->mipv6); |
888 | 0 | } |
889 | |
|
890 | 0 | if (iface->AdvLowpanCoList && schedule_option_lowpanco(dest, iface)) { |
891 | 0 | cur->next = new_safe_buffer_list(); |
892 | 0 | cur = cur->next; |
893 | 0 | add_ra_option_lowpanco(cur->sb, iface->AdvLowpanCoList); |
894 | 0 | } |
895 | |
|
896 | 0 | if (iface->AdvAbroList && schedule_option_abro(dest, iface)) { |
897 | 0 | cur->next = new_safe_buffer_list(); |
898 | 0 | cur = cur->next; |
899 | 0 | add_ra_option_abro(cur->sb, iface->AdvAbroList); |
900 | 0 | } |
901 | |
|
902 | 0 | if (iface->AdvCaptivePortalAPI != NULL && schedule_option_capport(dest, iface)) { |
903 | 0 | cur->next = new_safe_buffer_list(); |
904 | 0 | cur = cur->next; |
905 | 0 | add_ra_option_capport(cur->sb, iface->AdvCaptivePortalAPI); |
906 | 0 | } |
907 | | |
908 | | // Return the root of the list |
909 | 0 | return sbl; |
910 | 0 | } |
911 | | |
912 | | static int send_ra(int sock, struct Interface *iface, struct in6_addr const *dest) |
913 | 0 | { |
914 | 0 | if (!iface->AdvSendAdvert) { |
915 | 0 | dlog(LOG_DEBUG, 2, "AdvSendAdvert is off for %s", iface->props.name); |
916 | 0 | return 0; |
917 | 0 | } |
918 | | |
919 | 0 | if (dest == NULL) { |
920 | 0 | static uint8_t const all_hosts_addr[] = {0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; |
921 | 0 | dest = (struct in6_addr const *)all_hosts_addr; |
922 | 0 | clock_gettime(CLOCK_MONOTONIC, &iface->times.last_multicast); |
923 | 0 | } |
924 | |
|
925 | 0 | update_iface_times(iface); |
926 | |
|
927 | 0 | char dest_text[INET6_ADDRSTRLEN] = {""}; |
928 | 0 | char src_text[INET6_ADDRSTRLEN] = {""}; |
929 | 0 | addrtostr(dest, dest_text, INET6_ADDRSTRLEN); |
930 | 0 | addrtostr(iface->props.if_addr_rasrc, src_text, INET6_ADDRSTRLEN); |
931 | | |
932 | | // Build RA header |
933 | 0 | struct safe_buffer *ra_hdr = new_safe_buffer(); |
934 | | // if forwarding is disabled, send zero router lifetime |
935 | | // the check_ip6 function is hoisted here to enable testing of add_ra_header |
936 | 0 | int cease_adv = iface->state_info.cease_adv || check_ip6_forwarding(); |
937 | 0 | add_ra_header(ra_hdr, &iface->ra_header_info, cease_adv); |
938 | | // Build RA option list |
939 | 0 | struct safe_buffer_list *ra_opts = build_ra_options(iface, dest); |
940 | | |
941 | | // Send out one or more RAs, all in the form of (hdr+options), |
942 | | // such that none of the RAs exceed the link MTU |
943 | | // (max size is pre-computed in iface->props.max_ra_option_size) |
944 | |
|
945 | 0 | struct safe_buffer_list *cur = ra_opts; |
946 | 0 | struct safe_buffer *sb = new_safe_buffer(); |
947 | 0 | do { |
948 | 0 | unsigned long int option_count = 0; |
949 | 0 | sb->used = 0; |
950 | | // Duplicate the RA header |
951 | 0 | safe_buffer_append(sb, ra_hdr->buffer, ra_hdr->used); |
952 | | // Copy in as many RA options as we can fit. |
953 | 0 | while (NULL != cur) { |
954 | 0 | if (sb->used == 0) { |
955 | 0 | dlog(LOG_DEBUG, 5, "send_ra: Saw empty buffer!"); |
956 | 0 | cur = cur->next; |
957 | 0 | continue; |
958 | 0 | } |
959 | | // Not enough room for the next option in our buffer, just send the buffer now. |
960 | 0 | if (sb->used + cur->sb->used > iface->props.max_ra_option_size) { |
961 | | // But make sure we send at least one option in each RA |
962 | | // TODO: a future improvement would be to optimize packing of |
963 | | // the options in the minimal number of RAs, such that each one |
964 | | // does not exceed the MTU where possible. |
965 | 0 | if (option_count > 0) |
966 | 0 | break; |
967 | 0 | } |
968 | | // It's possible that a single option is larger than the MTU, so |
969 | | // fragmentation will always happen in that case. |
970 | | // One known case is a very long DNSSL, which is documented in |
971 | | // RFC6106 errata #4864 |
972 | | // In this case, the RA will contain a single option, consisting of |
973 | | // ONLY the DNSSL, without other options. RFC6980-conforming nodes |
974 | | // should then ignore the DNSSL. |
975 | 0 | if (cur->sb->used > iface->props.max_ra_option_size) { |
976 | 0 | flog(LOG_WARNING, |
977 | 0 | "send_ra: RA option (type=%hhd) length %lu exceeds max RA option size %u, fragmenting anyway (violates RFC6980 section 2)", |
978 | 0 | (unsigned char)(cur->sb->buffer[0]), cur->sb->used, iface->props.max_ra_option_size); |
979 | 0 | } |
980 | | // Add this option to the buffer. |
981 | 0 | safe_buffer_append(sb, cur->sb->buffer, cur->sb->used); |
982 | 0 | option_count++; |
983 | | /* |
984 | | if(cur->sb->used > 0 && (unsigned char)(cur->sb->buffer[0]) == ND_OPT_DNSSL_INFORMATION) { |
985 | | abort(); |
986 | | } |
987 | | */ |
988 | 0 | cur = cur->next; |
989 | 0 | } |
990 | | |
991 | | // RA built, now send it. |
992 | 0 | dlog(LOG_DEBUG, 5, "sending RA to %s on %s (%s), %lu options (using %zd/%u bytes)", dest_text, iface->props.name, |
993 | 0 | src_text, option_count, sb->used, iface->props.max_ra_option_size); |
994 | 0 | int err = really_send(sock, dest, &iface->props, sb); |
995 | 0 | if (err < 0) { |
996 | 0 | if (!iface->IgnoreIfMissing || !(errno == EINVAL || errno == ENODEV)) |
997 | 0 | flog(LOG_WARNING, "sendmsg: %s", strerror(errno)); |
998 | 0 | else |
999 | 0 | dlog(LOG_DEBUG, 3, "sendmsg: %s", strerror(errno)); |
1000 | 0 | safe_buffer_free(sb); |
1001 | 0 | safe_buffer_list_free(ra_opts); |
1002 | 0 | safe_buffer_free(ra_hdr); |
1003 | 0 | return -1; |
1004 | 0 | } |
1005 | |
|
1006 | 0 | } while (NULL != cur); |
1007 | | |
1008 | 0 | safe_buffer_free(sb); |
1009 | 0 | safe_buffer_list_free(ra_opts); |
1010 | 0 | safe_buffer_free(ra_hdr); |
1011 | |
|
1012 | 0 | return 0; |
1013 | 0 | } |
1014 | | |
1015 | | static int really_send(int sock, struct in6_addr const *dest, struct properties const *props, struct safe_buffer const *sb) |
1016 | 0 | { |
1017 | 0 | struct sockaddr_in6 addr; |
1018 | 0 | memset((void *)&addr, 0, sizeof(addr)); |
1019 | 0 | addr.sin6_family = AF_INET6; |
1020 | 0 | addr.sin6_port = htons(IPPROTO_ICMPV6); |
1021 | 0 | memcpy(&addr.sin6_addr, dest, sizeof(struct in6_addr)); |
1022 | |
|
1023 | 0 | struct iovec iov; |
1024 | 0 | iov.iov_len = sb->used; |
1025 | 0 | iov.iov_base = (caddr_t)sb->buffer; |
1026 | |
|
1027 | 0 | char __attribute__((aligned(8))) chdr[CMSG_SPACE(sizeof(struct in6_pktinfo))]; |
1028 | 0 | memset(chdr, 0, sizeof(chdr)); |
1029 | 0 | struct cmsghdr *cmsg = (struct cmsghdr *)chdr; |
1030 | |
|
1031 | 0 | cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); |
1032 | 0 | cmsg->cmsg_level = IPPROTO_IPV6; |
1033 | 0 | cmsg->cmsg_type = IPV6_PKTINFO; |
1034 | |
|
1035 | 0 | struct in6_pktinfo *pkt_info = (struct in6_pktinfo *)CMSG_DATA(cmsg); |
1036 | 0 | pkt_info->ipi6_ifindex = props->if_index; |
1037 | 0 | memcpy(&pkt_info->ipi6_addr, props->if_addr_rasrc, sizeof(struct in6_addr)); |
1038 | |
|
1039 | 0 | #ifdef HAVE_SIN6_SCOPE_ID |
1040 | 0 | if (IN6_IS_ADDR_LINKLOCAL(&addr.sin6_addr) || IN6_IS_ADDR_MC_LINKLOCAL(&addr.sin6_addr)) |
1041 | 0 | addr.sin6_scope_id = props->if_index; |
1042 | 0 | #endif |
1043 | |
|
1044 | 0 | struct msghdr mhdr; |
1045 | 0 | memset(&mhdr, 0, sizeof(mhdr)); |
1046 | 0 | mhdr.msg_name = (caddr_t)&addr; |
1047 | 0 | mhdr.msg_namelen = sizeof(struct sockaddr_in6); |
1048 | 0 | mhdr.msg_iov = &iov; |
1049 | 0 | mhdr.msg_iovlen = 1; |
1050 | 0 | mhdr.msg_control = (void *)cmsg; |
1051 | 0 | mhdr.msg_controllen = sizeof(chdr); |
1052 | |
|
1053 | 0 | return sendmsg(sock, &mhdr, 0); |
1054 | 0 | } |
1055 | | |
1056 | | static int schedule_option_prefix(struct in6_addr const *dest, struct Interface const *iface, struct AdvPrefix const *prefix) |
1057 | 0 | { |
1058 | 0 | return schedule_helper(dest, iface, prefix->curr_preferredlft); |
1059 | 0 | } |
1060 | | |
1061 | | static int schedule_option_route(struct in6_addr const *dest, struct Interface const *iface, struct AdvRoute const *route) |
1062 | 0 | { |
1063 | 0 | return schedule_helper(dest, iface, route->AdvRouteLifetime); |
1064 | 0 | } |
1065 | | |
1066 | | static int schedule_option_rdnss(struct in6_addr const *dest, struct Interface const *iface, struct AdvRDNSS const *rdnss) |
1067 | 0 | { |
1068 | 0 | return schedule_helper(dest, iface, rdnss->AdvRDNSSLifetime); |
1069 | 0 | } |
1070 | | |
1071 | | static int schedule_option_dnssl(struct in6_addr const *dest, struct Interface const *iface, struct AdvDNSSL const *dnssl) |
1072 | 0 | { |
1073 | 0 | return schedule_helper(dest, iface, dnssl->AdvDNSSLLifetime); |
1074 | 0 | } |
1075 | | |
1076 | | static int schedule_option_mtu(struct in6_addr const *dest, struct Interface const *iface) |
1077 | 0 | { |
1078 | 0 | return schedule_helper(dest, iface, iface->ra_header_info.AdvDefaultLifetime); |
1079 | 0 | } |
1080 | | |
1081 | | static int schedule_option_sllao(struct in6_addr const *dest, struct Interface const *iface) |
1082 | 0 | { |
1083 | 0 | return schedule_helper(dest, iface, iface->ra_header_info.AdvDefaultLifetime); |
1084 | 0 | } |
1085 | | |
1086 | | static int schedule_option_mipv6_rtr_adv_interval(struct in6_addr const *dest, struct Interface const *iface) |
1087 | 0 | { |
1088 | 0 | return schedule_helper(dest, iface, iface->ra_header_info.AdvDefaultLifetime); |
1089 | 0 | } |
1090 | | |
1091 | | static int schedule_option_mipv6_home_agent_info(struct in6_addr const *dest, struct Interface const *iface) |
1092 | 0 | { |
1093 | 0 | return schedule_helper(dest, iface, iface->mipv6.HomeAgentLifetime); |
1094 | 0 | } |
1095 | | |
1096 | | static int schedule_option_lowpanco(struct in6_addr const *dest, struct Interface const *iface) |
1097 | 0 | { |
1098 | 0 | return schedule_helper(dest, iface, iface->AdvLowpanCoList->AdvLifeTime); |
1099 | 0 | } |
1100 | | |
1101 | | static int schedule_option_abro(struct in6_addr const *dest, struct Interface const *iface) |
1102 | 0 | { |
1103 | 0 | return schedule_helper(dest, iface, iface->AdvAbroList->ValidLifeTime); |
1104 | 0 | } |
1105 | | |
1106 | | static int schedule_option_capport(struct in6_addr const *dest, struct Interface const *iface) |
1107 | 0 | { |
1108 | 0 | return schedule_helper(dest, iface, iface->ra_header_info.AdvDefaultLifetime); |
1109 | 0 | } |
1110 | | |
1111 | | static int schedule_helper(struct in6_addr const *dest, struct Interface const *iface, int option_lifetime) |
1112 | 0 | { |
1113 | 0 | return 1; |
1114 | | // 1. |
1115 | | // Cases to schedule a complete RA blast: |
1116 | | // - Server received a RS |
1117 | | // - We're in the initial-RAs phase of startup |
1118 | | // - The (unicast) destination was first seen very recently, and probably |
1119 | | // has NOT got a full set of RAs yet. |
1120 | | |
1121 | | // 2. |
1122 | | // If the dest has existed for a while |
1123 | | // (unicast destination): spread RAs out to at least 1/N of the option lifetime |
1124 | | // (multicast destination): spread RAs out to at least 1/N of the option lifetime |
1125 | 0 | }; |