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