1 /* $KAME: config.c,v 1.84 2003/08/05 12:34:23 itojun Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 * Copyright (C) 1998 WIDE Project.
7 * Copyright (C) 2011 Hiroki Sato <hrs@FreeBSD.org>
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the project nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/param.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38
39 #include <net/if.h>
40 #include <net/route.h>
41 #include <net/if_dl.h>
42
43 #include <netinet/in.h>
44 #include <netinet/in_var.h>
45 #include <netinet/ip6.h>
46 #include <netinet6/ip6_var.h>
47 #include <netinet/icmp6.h>
48 #include <netinet6/nd6.h>
49
50 #include <arpa/inet.h>
51
52 #include <stdio.h>
53 #include <syslog.h>
54 #include <errno.h>
55 #include <inttypes.h>
56 #include <netdb.h>
57 #include <string.h>
58 #include <search.h>
59 #include <stdlib.h>
60 #include <time.h>
61 #include <unistd.h>
62 #include <ifaddrs.h>
63
64 #include "rtadvd.h"
65 #include "advcap.h"
66 #include "timer.h"
67 #include "if.h"
68 #include "config.h"
69
70 /* label of tcapcode + number + domain name + zero octet */
71 static char entbuf[10 + 3 + NI_MAXHOST + 1];
72 static char oentbuf[10 + 3 + NI_MAXHOST + 1];
73 static char abuf[DNAME_LABELENC_MAXLEN];
74
75 static time_t prefix_timo = (60 * 120); /* 2 hours.
76 * XXX: should be configurable. */
77
78 static struct rtadvd_timer *prefix_timeout(void *);
79 static void makeentry(char *, size_t, int, const char *);
80 static ssize_t dname_labelenc(char *, const char *);
81
82 /* Encode domain name label encoding in RFC 1035 Section 3.1 */
83 static ssize_t
dname_labelenc(char * dst,const char * src)84 dname_labelenc(char *dst, const char *src)
85 {
86 char *dst_origin;
87 char *p;
88 size_t len;
89
90 dst_origin = dst;
91 len = strlen(src);
92
93 if (len + len / 64 + 1 + 1 > DNAME_LABELENC_MAXLEN)
94 return (-1);
95 /* Length fields per 63 octets + '\0' (<= DNAME_LABELENC_MAXLEN) */
96 memset(dst, 0, len + len / 64 + 1 + 1);
97
98 syslog(LOG_DEBUG, "<%s> labelenc = %s", __func__, src);
99 while (src && (len = strlen(src)) != 0) {
100 /* Put a length field with 63 octet limitation first. */
101 p = strchr(src, '.');
102 if (p == NULL)
103 *dst = len = MIN(63, len);
104 else
105 *dst = len = MIN(63, p - src);
106 if (dst + 1 + len < dst_origin + DNAME_LABELENC_MAXLEN)
107 dst++;
108 else
109 return (-1);
110 /* Copy 63 octets at most. */
111 memcpy(dst, src, len);
112 dst += len;
113 if (p == NULL) /* the last label */
114 break;
115 src = p + 1;
116 }
117 /* Always need a 0-length label at the tail. */
118 *dst++ = '\0';
119
120 syslog(LOG_DEBUG, "<%s> labellen = %td", __func__, dst - dst_origin);
121 return (dst - dst_origin);
122 }
123
124 #define MUSTHAVE(var, cap) \
125 do { \
126 int64_t t; \
127 if ((t = agetnum(cap)) < 0) { \
128 fprintf(stderr, "rtadvd: need %s for interface %s\n", \
129 cap, intface); \
130 exit(1); \
131 } \
132 var = t; \
133 } while (0)
134
135 #define MAYHAVE(var, cap, def) \
136 do { \
137 if ((var = agetnum(cap)) < 0) \
138 var = def; \
139 } while (0)
140
141 int
loadconfig_index(int idx)142 loadconfig_index(int idx)
143 {
144 char ifname[IFNAMSIZ];
145
146 syslog(LOG_DEBUG, "<%s> enter", __func__);
147
148 if (if_indextoname(idx, ifname) != NULL)
149 return (loadconfig_ifname(ifname));
150 else
151 return (1);
152 }
153
154 int
loadconfig_ifname(char * ifname)155 loadconfig_ifname(char *ifname)
156 {
157 struct ifinfo *ifi;
158
159 syslog(LOG_DEBUG, "<%s> enter", __func__);
160
161 update_ifinfo(&ifilist, UPDATE_IFINFO_ALL);
162 TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
163 /* NULL means all IFs will be processed. */
164 if (ifname != NULL &&
165 strcmp(ifi->ifi_ifname, ifname) != 0)
166 continue;
167
168 if (!ifi->ifi_persist) {
169 syslog(LOG_INFO,
170 "<%s> %s is not a target interface. "
171 "Ignored at this moment.", __func__,
172 ifi->ifi_ifname);
173 continue;
174
175 }
176 if (ifi->ifi_ifindex == 0) {
177 syslog(LOG_ERR,
178 "<%s> %s not found. "
179 "Ignored at this moment.", __func__,
180 ifi->ifi_ifname);
181 continue;
182 }
183 if (getconfig(ifi) == NULL) {
184 syslog(LOG_ERR,
185 "<%s> invalid configuration for %s. "
186 "Ignored at this moment.", __func__,
187 ifi->ifi_ifname);
188 continue;
189 }
190 }
191 return (0);
192 }
193
194 int
rm_ifinfo_index(int idx)195 rm_ifinfo_index(int idx)
196 {
197 struct ifinfo *ifi;
198
199 ifi = if_indextoifinfo(idx);
200 if (ifi == NULL) {
201 syslog(LOG_ERR, "<%s>: ifinfo not found (idx=%d)",
202 __func__, idx);
203 return (-1);
204 }
205
206 return (rm_ifinfo(ifi));
207 }
208
209 int
rm_ifinfo(struct ifinfo * ifi)210 rm_ifinfo(struct ifinfo *ifi)
211 {
212 int error;
213
214 syslog(LOG_DEBUG, "<%s> enter (%s).", __func__, ifi->ifi_ifname);
215 switch (ifi->ifi_state) {
216 case IFI_STATE_UNCONFIGURED:
217 return (0);
218 break;
219 default:
220 ifi->ifi_state = IFI_STATE_UNCONFIGURED;
221 syslog(LOG_DEBUG,
222 "<%s> ifname=%s marked as UNCONFIGURED.",
223 __func__, ifi->ifi_ifname);
224
225 /* XXX: No MC leaving here because index is disappeared */
226
227 /* Inactivate timer */
228 rtadvd_remove_timer(ifi->ifi_ra_timer);
229 ifi->ifi_ra_timer = NULL;
230 break;
231 }
232
233 /* clean up ifi */
234 if (!ifi->ifi_persist) {
235 TAILQ_REMOVE(&ifilist, ifi, ifi_next);
236 syslog(LOG_DEBUG, "<%s>: ifinfo (idx=%d) removed.",
237 __func__, ifi->ifi_ifindex);
238 } else {
239 /* recreate an empty entry */
240 update_persist_ifinfo(&ifilist, ifi->ifi_ifname);
241 syslog(LOG_DEBUG, "<%s>: ifname=%s is persistent.",
242 __func__, ifi->ifi_ifname);
243 }
244
245 /* clean up rai if any */
246 switch (ifi->ifi_state) {
247 case IFI_STATE_CONFIGURED:
248 if (ifi->ifi_rainfo != NULL) {
249 error = rm_rainfo(ifi->ifi_rainfo);
250 if (error)
251 return (error);
252 ifi->ifi_rainfo = NULL;
253 }
254 break;
255 case IFI_STATE_TRANSITIVE:
256 if (ifi->ifi_rainfo == ifi->ifi_rainfo_trans) {
257 if (ifi->ifi_rainfo != NULL) {
258 error = rm_rainfo(ifi->ifi_rainfo);
259 if (error)
260 return (error);
261 ifi->ifi_rainfo = NULL;
262 ifi->ifi_rainfo_trans = NULL;
263 }
264 } else {
265 if (ifi->ifi_rainfo != NULL) {
266 error = rm_rainfo(ifi->ifi_rainfo);
267 if (error)
268 return (error);
269 ifi->ifi_rainfo = NULL;
270 }
271 if (ifi->ifi_rainfo_trans != NULL) {
272 error = rm_rainfo(ifi->ifi_rainfo_trans);
273 if (error)
274 return (error);
275 ifi->ifi_rainfo_trans = NULL;
276 }
277 }
278 }
279
280 syslog(LOG_DEBUG, "<%s> leave (%s).", __func__, ifi->ifi_ifname);
281 if (!ifi->ifi_persist)
282 free(ifi);
283 return (0);
284 }
285
286 int
rm_rainfo(struct rainfo * rai)287 rm_rainfo(struct rainfo *rai)
288 {
289 struct prefix *pfx;
290 struct soliciter *sol;
291 struct rdnss *rdn;
292 struct rdnss_addr *rdna;
293 struct dnssl *dns;
294 struct rtinfo *rti;
295
296 syslog(LOG_DEBUG, "<%s>: enter", __func__);
297
298 TAILQ_REMOVE(&railist, rai, rai_next);
299 if (rai->rai_ifinfo != NULL)
300 syslog(LOG_DEBUG, "<%s>: rainfo (idx=%d) removed.",
301 __func__, rai->rai_ifinfo->ifi_ifindex);
302
303 if (rai->rai_ra_data != NULL)
304 free(rai->rai_ra_data);
305
306 while ((pfx = TAILQ_FIRST(&rai->rai_prefix)) != NULL)
307 delete_prefix(pfx);
308 while ((sol = TAILQ_FIRST(&rai->rai_soliciter)) != NULL) {
309 TAILQ_REMOVE(&rai->rai_soliciter, sol, sol_next);
310 free(sol);
311 }
312 while ((rdn = TAILQ_FIRST(&rai->rai_rdnss)) != NULL) {
313 TAILQ_REMOVE(&rai->rai_rdnss, rdn, rd_next);
314 while ((rdna = TAILQ_FIRST(&rdn->rd_list)) != NULL) {
315 TAILQ_REMOVE(&rdn->rd_list, rdna, ra_next);
316 free(rdna);
317 }
318 free(rdn);
319 }
320 while ((dns = TAILQ_FIRST(&rai->rai_dnssl)) != NULL) {
321 TAILQ_REMOVE(&rai->rai_dnssl, dns, dn_next);
322 free(dns);
323 }
324 while ((rti = TAILQ_FIRST(&rai->rai_route)) != NULL) {
325 TAILQ_REMOVE(&rai->rai_route, rti, rti_next);
326 free(rti);
327 }
328 free(rai);
329 syslog(LOG_DEBUG, "<%s>: leave", __func__);
330
331 return (0);
332 }
333
334 struct ifinfo *
getconfig(struct ifinfo * ifi)335 getconfig(struct ifinfo *ifi)
336 {
337 int stat, i;
338 int error;
339 char tbuf[BUFSIZ];
340 struct rainfo *rai;
341 struct rainfo *rai_old;
342 int32_t val;
343 int64_t val64;
344 char buf[BUFSIZ];
345 char *bp = buf;
346 char *addr, *flagstr;
347
348 if (ifi == NULL) /* if does not exist */
349 return (NULL);
350
351 if (ifi->ifi_state == IFI_STATE_TRANSITIVE &&
352 ifi->ifi_rainfo == NULL) {
353 syslog(LOG_INFO, "<%s> %s is shutting down. Skipped.",
354 __func__, ifi->ifi_ifname);
355 return (NULL);
356 }
357
358 if ((stat = agetent(tbuf, ifi->ifi_ifname)) <= 0) {
359 memset(tbuf, 0, sizeof(tbuf));
360 syslog(LOG_INFO,
361 "<%s> %s isn't defined in the configuration file"
362 " or the configuration file doesn't exist."
363 " Treat it as default",
364 __func__, ifi->ifi_ifname);
365 }
366
367 ELM_MALLOC(rai, exit(1));
368 TAILQ_INIT(&rai->rai_prefix);
369 TAILQ_INIT(&rai->rai_route);
370 TAILQ_INIT(&rai->rai_rdnss);
371 TAILQ_INIT(&rai->rai_dnssl);
372 TAILQ_INIT(&rai->rai_soliciter);
373 rai->rai_ifinfo = ifi;
374
375 /* gather on-link prefixes from the network interfaces. */
376 if (agetflag("noifprefix"))
377 rai->rai_advifprefix = 0;
378 else
379 rai->rai_advifprefix = 1;
380
381 /* get interface information */
382 if (agetflag("nolladdr"))
383 rai->rai_advlinkopt = 0;
384 else
385 rai->rai_advlinkopt = 1;
386 if (rai->rai_advlinkopt) {
387 if (ifi->ifi_sdl.sdl_type == 0) {
388 syslog(LOG_ERR,
389 "<%s> can't get information of %s",
390 __func__, ifi->ifi_ifname);
391 goto getconfig_free_rai;
392 }
393 }
394
395 /*
396 * set router configuration variables.
397 */
398 MAYHAVE(val, "maxinterval", DEF_MAXRTRADVINTERVAL);
399 if (val < MIN_MAXINTERVAL || val > MAX_MAXINTERVAL) {
400 syslog(LOG_ERR,
401 "<%s> maxinterval (%" PRIu32 ") on %s is invalid "
402 "(must be between %u and %u)", __func__, val,
403 ifi->ifi_ifname, MIN_MAXINTERVAL, MAX_MAXINTERVAL);
404 goto getconfig_free_rai;
405 }
406 rai->rai_maxinterval = (uint16_t)val;
407
408 MAYHAVE(val, "mininterval", rai->rai_maxinterval/3);
409 if ((uint16_t)val < MIN_MININTERVAL ||
410 (uint16_t)val > (rai->rai_maxinterval * 3) / 4) {
411 syslog(LOG_ERR,
412 "<%s> mininterval (%" PRIu32 ") on %s is invalid "
413 "(must be between %d and %d)",
414 __func__, val, ifi->ifi_ifname, MIN_MININTERVAL,
415 (rai->rai_maxinterval * 3) / 4);
416 goto getconfig_free_rai;
417 }
418 rai->rai_mininterval = (uint16_t)val;
419
420 MAYHAVE(val, "chlim", DEF_ADVCURHOPLIMIT);
421 rai->rai_hoplimit = val & 0xff;
422
423 if ((flagstr = (char *)agetstr("raflags", &bp))) {
424 val = 0;
425 if (strchr(flagstr, 'm'))
426 val |= ND_RA_FLAG_MANAGED;
427 if (strchr(flagstr, 'o'))
428 val |= ND_RA_FLAG_OTHER;
429 if (strchr(flagstr, 'h'))
430 val |= ND_RA_FLAG_RTPREF_HIGH;
431 if (strchr(flagstr, 'l')) {
432 if ((val & ND_RA_FLAG_RTPREF_HIGH)) {
433 syslog(LOG_ERR, "<%s> the \'h\' and \'l\'"
434 " router flags are exclusive", __func__);
435 goto getconfig_free_rai;
436 }
437 val |= ND_RA_FLAG_RTPREF_LOW;
438 }
439 #ifdef DRAFT_IETF_6MAN_IPV6ONLY_FLAG
440 if (strchr(flagstr, 'S'))
441 val |= ND_RA_FLAG_IPV6_ONLY;
442 #endif
443 } else
444 MAYHAVE(val, "raflags", 0);
445
446 rai->rai_managedflg = val & ND_RA_FLAG_MANAGED;
447 rai->rai_otherflg = val & ND_RA_FLAG_OTHER;
448 #ifndef ND_RA_FLAG_RTPREF_MASK
449 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
450 #define ND_RA_FLAG_RTPREF_RSV 0x10 /* 00010000 */
451 #endif
452 rai->rai_rtpref = val & ND_RA_FLAG_RTPREF_MASK;
453 if (rai->rai_rtpref == ND_RA_FLAG_RTPREF_RSV) {
454 syslog(LOG_ERR, "<%s> invalid router preference (%02x) on %s",
455 __func__, rai->rai_rtpref, ifi->ifi_ifname);
456 goto getconfig_free_rai;
457 }
458 #ifdef DRAFT_IETF_6MAN_IPV6ONLY_FLAG
459 rai->rai_ipv6onlyflg = val & ND_RA_FLAG_IPV6_ONLY;
460 #endif
461
462 MAYHAVE(val, "rltime", rai->rai_maxinterval * 3);
463 if ((uint16_t)val && ((uint16_t)val < rai->rai_maxinterval ||
464 (uint16_t)val > MAXROUTERLIFETIME)) {
465 syslog(LOG_ERR,
466 "<%s> router lifetime (%" PRIu32 ") on %s is invalid "
467 "(must be 0 or between %d and %d)",
468 __func__, val, ifi->ifi_ifname, rai->rai_maxinterval,
469 MAXROUTERLIFETIME);
470 goto getconfig_free_rai;
471 }
472 rai->rai_lifetime = val & 0xffff;
473
474 MAYHAVE(val, "rtime", DEF_ADVREACHABLETIME);
475 if (val < 0 || val > MAXREACHABLETIME) {
476 syslog(LOG_ERR,
477 "<%s> reachable time (%" PRIu32 ") on %s is invalid "
478 "(must be no greater than %d)",
479 __func__, val, ifi->ifi_ifname, MAXREACHABLETIME);
480 goto getconfig_free_rai;
481 }
482 rai->rai_reachabletime = (uint32_t)val;
483
484 MAYHAVE(val64, "retrans", DEF_ADVRETRANSTIMER);
485 if (val64 < 0 || val64 > 0xffffffff) {
486 syslog(LOG_ERR, "<%s> retrans time (%" PRIu64 ") on %s out of range",
487 __func__, val64, ifi->ifi_ifname);
488 goto getconfig_free_rai;
489 }
490 rai->rai_retranstimer = (uint32_t)val64;
491
492 if (agetnum("hapref") != -1 || agetnum("hatime") != -1) {
493 syslog(LOG_ERR,
494 "<%s> mobile-ip6 configuration not supported",
495 __func__);
496 goto getconfig_free_rai;
497 }
498 /* prefix information */
499
500 /*
501 * This is an implementation specific parameter to consider
502 * link propagation delays and poorly synchronized clocks when
503 * checking consistency of advertised lifetimes.
504 */
505 MAYHAVE(val, "clockskew", 0);
506 rai->rai_clockskew = val;
507
508 rai->rai_pfxs = 0;
509 for (i = -1; i < MAXPREFIX; i++) {
510 struct prefix *pfx;
511
512 makeentry(entbuf, sizeof(entbuf), i, "addr");
513 addr = (char *)agetstr(entbuf, &bp);
514 if (addr == NULL)
515 continue;
516
517 /* allocate memory to store prefix information */
518 ELM_MALLOC(pfx, exit(1));
519 pfx->pfx_rainfo = rai;
520 pfx->pfx_origin = PREFIX_FROM_CONFIG;
521
522 if (inet_pton(AF_INET6, addr, &pfx->pfx_prefix) != 1) {
523 syslog(LOG_ERR,
524 "<%s> inet_pton failed for %s",
525 __func__, addr);
526 goto getconfig_free_pfx;
527 }
528 if (IN6_IS_ADDR_MULTICAST(&pfx->pfx_prefix)) {
529 syslog(LOG_ERR,
530 "<%s> multicast prefix (%s) must "
531 "not be advertised on %s",
532 __func__, addr, ifi->ifi_ifname);
533 goto getconfig_free_pfx;
534 }
535 if (IN6_IS_ADDR_LINKLOCAL(&pfx->pfx_prefix))
536 syslog(LOG_NOTICE,
537 "<%s> link-local prefix (%s) will be"
538 " advertised on %s",
539 __func__, addr, ifi->ifi_ifname);
540
541 makeentry(entbuf, sizeof(entbuf), i, "prefixlen");
542 MAYHAVE(val, entbuf, 64);
543 if (val < 0 || val > 128) {
544 syslog(LOG_ERR, "<%s> prefixlen (%" PRIu32 ") for %s "
545 "on %s out of range",
546 __func__, val, addr, ifi->ifi_ifname);
547 goto getconfig_free_pfx;
548 }
549 pfx->pfx_prefixlen = (int)val;
550
551 makeentry(entbuf, sizeof(entbuf), i, "pinfoflags");
552 if ((flagstr = (char *)agetstr(entbuf, &bp))) {
553 val = 0;
554 if (strchr(flagstr, 'l'))
555 val |= ND_OPT_PI_FLAG_ONLINK;
556 if (strchr(flagstr, 'a'))
557 val |= ND_OPT_PI_FLAG_AUTO;
558 } else {
559 MAYHAVE(val, entbuf,
560 (ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO));
561 }
562 pfx->pfx_onlinkflg = val & ND_OPT_PI_FLAG_ONLINK;
563 pfx->pfx_autoconfflg = val & ND_OPT_PI_FLAG_AUTO;
564
565 makeentry(entbuf, sizeof(entbuf), i, "vltime");
566 MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
567 if (val64 < 0 || val64 > 0xffffffff) {
568 syslog(LOG_ERR, "<%s> vltime (%" PRIu64 ") for "
569 "%s/%d on %s is out of range",
570 __func__, val64,
571 addr, pfx->pfx_prefixlen, ifi->ifi_ifname);
572 goto getconfig_free_pfx;
573 }
574 pfx->pfx_validlifetime = (uint32_t)val64;
575
576 makeentry(entbuf, sizeof(entbuf), i, "vltimedecr");
577 if (agetflag(entbuf)) {
578 struct timespec now;
579
580 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
581 pfx->pfx_vltimeexpire =
582 now.tv_sec + pfx->pfx_validlifetime;
583 }
584
585 makeentry(entbuf, sizeof(entbuf), i, "pltime");
586 MAYHAVE(val64, entbuf, DEF_ADVPREFERREDLIFETIME);
587 if (val64 < 0 || val64 > 0xffffffff) {
588 syslog(LOG_ERR,
589 "<%s> pltime (%" PRIu64 ") for %s/%d on %s "
590 "is out of range",
591 __func__, val64,
592 addr, pfx->pfx_prefixlen, ifi->ifi_ifname);
593 goto getconfig_free_pfx;
594 }
595 pfx->pfx_preflifetime = (uint32_t)val64;
596
597 makeentry(entbuf, sizeof(entbuf), i, "pltimedecr");
598 if (agetflag(entbuf)) {
599 struct timespec now;
600
601 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
602 pfx->pfx_pltimeexpire =
603 now.tv_sec + pfx->pfx_preflifetime;
604 }
605 /* link into chain */
606 TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next);
607 rai->rai_pfxs++;
608 continue;
609 getconfig_free_pfx:
610 free(pfx);
611 }
612 if (rai->rai_advifprefix && rai->rai_pfxs == 0)
613 get_prefix(rai);
614
615 MAYHAVE(val64, "mtu", 0);
616 if (val < 0 || val64 > 0xffffffff) {
617 syslog(LOG_ERR,
618 "<%s> mtu (%" PRIu64 ") on %s out of range",
619 __func__, val64, ifi->ifi_ifname);
620 goto getconfig_free_rai;
621 }
622 rai->rai_linkmtu = (uint32_t)val64;
623 if (rai->rai_linkmtu == 0) {
624 char *mtustr;
625
626 if ((mtustr = (char *)agetstr("mtu", &bp)) &&
627 strcmp(mtustr, "auto") == 0)
628 rai->rai_linkmtu = ifi->ifi_phymtu;
629 }
630 else if (rai->rai_linkmtu < IPV6_MMTU ||
631 rai->rai_linkmtu > ifi->ifi_phymtu) {
632 syslog(LOG_ERR,
633 "<%s> advertised link mtu (%" PRIu32 ") on %s is invalid (must "
634 "be between least MTU (%d) and physical link MTU (%d)",
635 __func__, rai->rai_linkmtu, ifi->ifi_ifname,
636 IPV6_MMTU, ifi->ifi_phymtu);
637 goto getconfig_free_rai;
638 }
639
640 #ifdef SIOCSIFINFO_IN6
641 {
642 struct in6_ndireq ndi;
643 int s;
644
645 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
646 syslog(LOG_ERR, "<%s> socket: %s", __func__,
647 strerror(errno));
648 exit(1);
649 }
650 memset(&ndi, 0, sizeof(ndi));
651 strlcpy(ndi.ifname, ifi->ifi_ifname, sizeof(ndi.ifname));
652 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&ndi) < 0)
653 syslog(LOG_INFO, "<%s> ioctl:SIOCGIFINFO_IN6 at %s: %s",
654 __func__, ifi->ifi_ifname, strerror(errno));
655
656 /* reflect the RA info to the host variables in kernel */
657 ndi.ndi.chlim = rai->rai_hoplimit;
658 ndi.ndi.retrans = rai->rai_retranstimer;
659 ndi.ndi.basereachable = rai->rai_reachabletime;
660 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&ndi) < 0)
661 syslog(LOG_INFO, "<%s> ioctl:SIOCSIFINFO_IN6 at %s: %s",
662 __func__, ifi->ifi_ifname, strerror(errno));
663
664 close(s);
665 }
666 #endif
667
668 /* route information */
669 rai->rai_routes = 0;
670 for (i = -1; i < MAXROUTE; i++) {
671 struct rtinfo *rti;
672
673 makeentry(entbuf, sizeof(entbuf), i, "rtprefix");
674 addr = (char *)agetstr(entbuf, &bp);
675 if (addr == NULL) {
676 makeentry(oentbuf, sizeof(oentbuf), i, "rtrprefix");
677 addr = (char *)agetstr(oentbuf, &bp);
678 if (addr)
679 fprintf(stderr, "%s was obsoleted. Use %s.\n",
680 oentbuf, entbuf);
681 }
682 if (addr == NULL)
683 continue;
684
685 /* allocate memory to store prefix information */
686 ELM_MALLOC(rti, exit(1));
687
688 if (inet_pton(AF_INET6, addr, &rti->rti_prefix) != 1) {
689 syslog(LOG_ERR, "<%s> inet_pton failed for %s",
690 __func__, addr);
691 goto getconfig_free_rti;
692 }
693 #if 0
694 /*
695 * XXX: currently there's no restriction in route information
696 * prefix according to
697 * draft-ietf-ipngwg-router-selection-00.txt.
698 * However, I think the similar restriction be necessary.
699 */
700 MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
701 if (IN6_IS_ADDR_MULTICAST(&rti->prefix)) {
702 syslog(LOG_ERR,
703 "<%s> multicast route (%s) must "
704 "not be advertised on %s",
705 __func__, addr, ifi->ifi_ifname);
706 goto getconfig_free_rti;
707 }
708 if (IN6_IS_ADDR_LINKLOCAL(&rti->prefix)) {
709 syslog(LOG_NOTICE,
710 "<%s> link-local route (%s) will "
711 "be advertised on %s",
712 __func__, addr, ifi->ifi_ifname);
713 goto getconfig_free_rti;
714 }
715 #endif
716
717 makeentry(entbuf, sizeof(entbuf), i, "rtplen");
718 /* XXX: 256 is a magic number for compatibility check. */
719 MAYHAVE(val, entbuf, 256);
720 if (val == 256) {
721 makeentry(oentbuf, sizeof(oentbuf), i, "rtrplen");
722 MAYHAVE(val, oentbuf, 256);
723 if (val != 256)
724 fprintf(stderr, "%s was obsoleted. Use %s.\n",
725 oentbuf, entbuf);
726 else
727 val = 64;
728 }
729 if (val < 0 || val > 128) {
730 syslog(LOG_ERR, "<%s> prefixlen (%" PRIu32 ") for %s on %s "
731 "out of range",
732 __func__, val, addr, ifi->ifi_ifname);
733 goto getconfig_free_rti;
734 }
735 rti->rti_prefixlen = (int)val;
736
737 makeentry(entbuf, sizeof(entbuf), i, "rtflags");
738 if ((flagstr = (char *)agetstr(entbuf, &bp))) {
739 val = 0;
740 if (strchr(flagstr, 'h'))
741 val |= ND_RA_FLAG_RTPREF_HIGH;
742 if (strchr(flagstr, 'l')) {
743 if ((val & ND_RA_FLAG_RTPREF_HIGH)) {
744 syslog(LOG_ERR,
745 "<%s> the \'h\' and \'l\' route"
746 " preferences are exclusive",
747 __func__);
748 goto getconfig_free_rti;
749 }
750 val |= ND_RA_FLAG_RTPREF_LOW;
751 }
752 } else
753 MAYHAVE(val, entbuf, 256); /* XXX */
754 if (val == 256) {
755 makeentry(oentbuf, sizeof(oentbuf), i, "rtrflags");
756 MAYHAVE(val, oentbuf, 256);
757 if (val != 256) {
758 fprintf(stderr, "%s was obsoleted. Use %s.\n",
759 oentbuf, entbuf);
760 } else
761 val = 0;
762 }
763 rti->rti_rtpref = val & ND_RA_FLAG_RTPREF_MASK;
764 if (rti->rti_rtpref == ND_RA_FLAG_RTPREF_RSV) {
765 syslog(LOG_ERR, "<%s> invalid route preference (%02x) "
766 "for %s/%d on %s",
767 __func__, rti->rti_rtpref, addr,
768 rti->rti_prefixlen, ifi->ifi_ifname);
769 goto getconfig_free_rti;
770 }
771
772 /*
773 * Since the spec does not a default value, we should make
774 * this entry mandatory. However, FreeBSD 4.4 has shipped
775 * with this field being optional, we use the router lifetime
776 * as an ad-hoc default value with a warning message.
777 */
778 makeentry(entbuf, sizeof(entbuf), i, "rtltime");
779 MAYHAVE(val64, entbuf, -1);
780 if (val64 == -1) {
781 makeentry(oentbuf, sizeof(oentbuf), i, "rtrltime");
782 MAYHAVE(val64, oentbuf, -1);
783 if (val64 != -1)
784 fprintf(stderr, "%s was obsoleted. Use %s.\n",
785 oentbuf, entbuf);
786 else {
787 fprintf(stderr, "%s should be specified "
788 "for interface %s.\n", entbuf,
789 ifi->ifi_ifname);
790 val64 = rai->rai_lifetime;
791 }
792 }
793 if (val64 < 0 || val64 > 0xffffffff) {
794 syslog(LOG_ERR, "<%s> route lifetime (%" PRIu64 ") for "
795 "%s/%d on %s out of range", __func__,
796 val64, addr, rti->rti_prefixlen,
797 ifi->ifi_ifname);
798 goto getconfig_free_rti;
799 }
800 rti->rti_ltime = (uint32_t)val64;
801
802 /* link into chain */
803 TAILQ_INSERT_TAIL(&rai->rai_route, rti, rti_next);
804 rai->rai_routes++;
805 continue;
806 getconfig_free_rti:
807 free(rti);
808 }
809
810 /* DNS server and DNS search list information */
811 for (i = -1; i < MAXRDNSSENT ; i++) {
812 struct rdnss *rdn;
813 struct rdnss_addr *rdna;
814 char *ap;
815 int c;
816
817 makeentry(entbuf, sizeof(entbuf), i, "rdnss");
818 addr = (char *)agetstr(entbuf, &bp);
819 if (addr == NULL)
820 continue;
821 ELM_MALLOC(rdn, exit(1));
822
823 TAILQ_INIT(&rdn->rd_list);
824
825 for (ap = addr; ap - addr < (ssize_t)strlen(addr); ap += c+1) {
826 c = strcspn(ap, ",");
827 strncpy(abuf, ap, c);
828 abuf[c] = '\0';
829 ELM_MALLOC(rdna, goto getconfig_free_rdn);
830 if (inet_pton(AF_INET6, abuf, &rdna->ra_dns) != 1) {
831 syslog(LOG_ERR, "<%s> inet_pton failed for %s",
832 __func__, abuf);
833 free(rdna);
834 goto getconfig_free_rdn;
835 }
836 TAILQ_INSERT_TAIL(&rdn->rd_list, rdna, ra_next);
837 }
838
839 makeentry(entbuf, sizeof(entbuf), i, "rdnssltime");
840 MAYHAVE(val, entbuf, (rai->rai_maxinterval * 3 / 2));
841 if ((uint16_t)val < rai->rai_maxinterval ||
842 (uint16_t)val > rai->rai_maxinterval * 2) {
843 syslog(LOG_ERR, "%s (%" PRIu16 ") on %s is invalid "
844 "(must be between %d and %d)",
845 entbuf, val, ifi->ifi_ifname, rai->rai_maxinterval,
846 rai->rai_maxinterval * 2);
847 goto getconfig_free_rdn;
848 }
849 rdn->rd_ltime = val;
850
851 /* link into chain */
852 TAILQ_INSERT_TAIL(&rai->rai_rdnss, rdn, rd_next);
853 continue;
854 getconfig_free_rdn:
855 while ((rdna = TAILQ_FIRST(&rdn->rd_list)) != NULL) {
856 TAILQ_REMOVE(&rdn->rd_list, rdna, ra_next);
857 free(rdna);
858 }
859 free(rdn);
860 }
861
862 for (i = -1; i < MAXDNSSLENT ; i++) {
863 struct dnssl *dns;
864 struct dnssl_addr *dnsa;
865 char *ap;
866 int c;
867
868 makeentry(entbuf, sizeof(entbuf), i, "dnssl");
869 addr = (char *)agetstr(entbuf, &bp);
870 if (addr == NULL)
871 continue;
872
873 ELM_MALLOC(dns, exit(1));
874
875 TAILQ_INIT(&dns->dn_list);
876
877 for (ap = addr; ap - addr < (ssize_t)strlen(addr); ap += c+1) {
878 c = strcspn(ap, ",");
879 strncpy(abuf, ap, c);
880 abuf[c] = '\0';
881 ELM_MALLOC(dnsa, goto getconfig_free_dns);
882 dnsa->da_len = dname_labelenc(dnsa->da_dom, abuf);
883 if (dnsa->da_len < 0) {
884 syslog(LOG_ERR, "Invalid dnssl entry: %s",
885 abuf);
886 goto getconfig_free_dns;
887 }
888 syslog(LOG_DEBUG, "<%s>: dnsa->da_len = %d", __func__,
889 dnsa->da_len);
890 TAILQ_INSERT_TAIL(&dns->dn_list, dnsa, da_next);
891 }
892
893 makeentry(entbuf, sizeof(entbuf), i, "dnsslltime");
894 MAYHAVE(val, entbuf, (rai->rai_maxinterval * 3 / 2));
895 if ((uint16_t)val < rai->rai_maxinterval ||
896 (uint16_t)val > rai->rai_maxinterval * 2) {
897 syslog(LOG_ERR, "%s (%" PRIu16 ") on %s is invalid "
898 "(must be between %d and %d)",
899 entbuf, val, ifi->ifi_ifname, rai->rai_maxinterval,
900 rai->rai_maxinterval * 2);
901 goto getconfig_free_dns;
902 }
903 dns->dn_ltime = val;
904
905 /* link into chain */
906 TAILQ_INSERT_TAIL(&rai->rai_dnssl, dns, dn_next);
907 continue;
908 getconfig_free_dns:
909 while ((dnsa = TAILQ_FIRST(&dns->dn_list)) != NULL) {
910 TAILQ_REMOVE(&dns->dn_list, dnsa, da_next);
911 free(dnsa);
912 }
913 free(dns);
914 }
915
916 /*
917 * handle pref64
918 */
919 rai->rai_pref64.p64_enabled = false;
920
921 if ((addr = (char *)agetstr("pref64", &bp))) {
922 if (inet_pton(AF_INET6, addr, &rai->rai_pref64.p64_prefix) != 1) {
923 syslog(LOG_ERR, "<%s> inet_pton failed for %s",
924 __func__, addr);
925 } else {
926 rai->rai_pref64.p64_enabled = true;
927
928 switch (val64 = agetnum("pref64len")) {
929 case -1:
930 case 96:
931 rai->rai_pref64.p64_plc = 0;
932 break;
933 case 64:
934 rai->rai_pref64.p64_plc = 1;
935 break;
936 case 56:
937 rai->rai_pref64.p64_plc = 2;
938 break;
939 case 48:
940 rai->rai_pref64.p64_plc = 3;
941 break;
942 case 40:
943 rai->rai_pref64.p64_plc = 4;
944 break;
945 case 32:
946 rai->rai_pref64.p64_plc = 5;
947 break;
948 default:
949 syslog(LOG_ERR, "prefix length %" PRIi64
950 "on %s is invalid; disabling PREF64",
951 val64, ifi->ifi_ifname);
952 rai->rai_pref64.p64_enabled = 0;
953 break;
954 }
955
956 /* This logic is from RFC 8781 section 4.1. */
957 val64 = agetnum("pref64lifetime");
958 if (val64 == -1)
959 val64 = rai->rai_lifetime * 3;
960 if (val64 > 65528)
961 val64 = 65528;
962 val64 = (val64 + 7) / 8;
963 rai->rai_pref64.p64_sl = (uint16_t) (uint64_t) val64;
964 }
965 }
966
967 /* construct the sending packet */
968 make_packet(rai);
969
970 /*
971 * If an entry with the same ifindex exists, remove it first.
972 * Before the removal, RDNSS and DNSSL options with
973 * zero-lifetime will be sent.
974 */
975 switch (ifi->ifi_state) {
976 case IFI_STATE_UNCONFIGURED:
977 /* UNCONFIGURED -> TRANSITIVE */
978
979 error = sock_mc_join(&sock, ifi->ifi_ifindex);
980 if (error)
981 exit(1);
982
983 ifi->ifi_state = IFI_STATE_TRANSITIVE;
984 ifi->ifi_burstcount = MAX_INITIAL_RTR_ADVERTISEMENTS;
985 ifi->ifi_burstinterval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
986
987 /* The same two rai mean initial burst */
988 ifi->ifi_rainfo = rai;
989 ifi->ifi_rainfo_trans = rai;
990 TAILQ_INSERT_TAIL(&railist, rai, rai_next);
991
992 if (ifi->ifi_ra_timer == NULL)
993 ifi->ifi_ra_timer = rtadvd_add_timer(ra_timeout,
994 ra_timer_update, ifi, ifi);
995 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
996 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
997 ifi->ifi_ra_timer);
998
999 syslog(LOG_DEBUG,
1000 "<%s> ifname=%s marked as TRANSITIVE (initial burst).",
1001 __func__, ifi->ifi_ifname);
1002 break;
1003 case IFI_STATE_CONFIGURED:
1004 /* CONFIGURED -> TRANSITIVE */
1005 rai_old = ifi->ifi_rainfo;
1006 if (rai_old == NULL) {
1007 syslog(LOG_ERR,
1008 "<%s> ifi_rainfo is NULL"
1009 " in IFI_STATE_CONFIGURED.", __func__);
1010 ifi = NULL;
1011 break;
1012 } else {
1013 struct rdnss *rdn;
1014 struct dnssl *dns;
1015
1016 rai_old->rai_lifetime = 0;
1017 TAILQ_FOREACH(rdn, &rai_old->rai_rdnss, rd_next)
1018 rdn->rd_ltime = 0;
1019 TAILQ_FOREACH(dns, &rai_old->rai_dnssl, dn_next)
1020 dns->dn_ltime = 0;
1021
1022 ifi->ifi_rainfo_trans = rai_old;
1023 ifi->ifi_state = IFI_STATE_TRANSITIVE;
1024 ifi->ifi_burstcount = MAX_FINAL_RTR_ADVERTISEMENTS;
1025 ifi->ifi_burstinterval = MIN_DELAY_BETWEEN_RAS;
1026
1027 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
1028 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
1029 ifi->ifi_ra_timer);
1030
1031 syslog(LOG_DEBUG,
1032 "<%s> ifname=%s marked as TRANSITIVE"
1033 " (transitional burst)",
1034 __func__, ifi->ifi_ifname);
1035 }
1036 ifi->ifi_rainfo = rai;
1037 TAILQ_INSERT_TAIL(&railist, rai, rai_next);
1038 break;
1039 case IFI_STATE_TRANSITIVE:
1040 if (ifi->ifi_rainfo != NULL) {
1041 if (ifi->ifi_rainfo == ifi->ifi_rainfo_trans) {
1042 /* Reinitialize initial burst */
1043 rm_rainfo(ifi->ifi_rainfo);
1044 ifi->ifi_rainfo = rai;
1045 ifi->ifi_rainfo_trans = rai;
1046 ifi->ifi_burstcount =
1047 MAX_INITIAL_RTR_ADVERTISEMENTS;
1048 ifi->ifi_burstinterval =
1049 MAX_INITIAL_RTR_ADVERT_INTERVAL;
1050 } else {
1051 /* Replace ifi_rainfo with the new one */
1052 rm_rainfo(ifi->ifi_rainfo);
1053 ifi->ifi_rainfo = rai;
1054 }
1055 TAILQ_INSERT_TAIL(&railist, rai, rai_next);
1056
1057 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
1058 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
1059 ifi->ifi_ra_timer);
1060 } else {
1061 /* XXX: NOTREACHED. Being shut down. */
1062 syslog(LOG_ERR,
1063 "<%s> %s is shutting down. Skipped.",
1064 __func__, ifi->ifi_ifname);
1065 rm_rainfo(rai);
1066
1067 return (NULL);
1068 }
1069 break;
1070 }
1071
1072 return (ifi);
1073
1074 getconfig_free_rai:
1075 free(rai);
1076 return (NULL);
1077 }
1078
1079 void
get_prefix(struct rainfo * rai)1080 get_prefix(struct rainfo *rai)
1081 {
1082 struct ifaddrs *ifap, *ifa;
1083 struct prefix *pfx;
1084 struct in6_addr *a;
1085 struct ifinfo *ifi;
1086 char *p, *ep, *m, *lim;
1087 char ntopbuf[INET6_ADDRSTRLEN];
1088
1089 if (getifaddrs(&ifap) < 0) {
1090 syslog(LOG_ERR,
1091 "<%s> can't get interface addresses",
1092 __func__);
1093 exit(1);
1094 }
1095 ifi = rai->rai_ifinfo;
1096
1097 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1098 int plen;
1099
1100 if (strcmp(ifa->ifa_name, ifi->ifi_ifname) != 0)
1101 continue;
1102 if (ifa->ifa_addr->sa_family != AF_INET6)
1103 continue;
1104 a = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
1105 if (IN6_IS_ADDR_LINKLOCAL(a))
1106 continue;
1107
1108 /* get prefix length */
1109 m = (char *)&((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
1110 lim = (char *)(ifa->ifa_netmask) + ifa->ifa_netmask->sa_len;
1111 plen = prefixlen(m, lim);
1112 if (plen <= 0 || plen > 128) {
1113 syslog(LOG_ERR, "<%s> failed to get prefixlen "
1114 "or prefix is invalid",
1115 __func__);
1116 exit(1);
1117 }
1118 if (plen == 128) /* XXX */
1119 continue;
1120 if (find_prefix(rai, a, plen)) {
1121 /* ignore a duplicated prefix. */
1122 continue;
1123 }
1124
1125 /* allocate memory to store prefix info. */
1126 ELM_MALLOC(pfx, exit(1));
1127
1128 /* set prefix, sweep bits outside of prefixlen */
1129 pfx->pfx_prefixlen = plen;
1130 memcpy(&pfx->pfx_prefix, a, sizeof(*a));
1131 p = (char *)&pfx->pfx_prefix;
1132 ep = (char *)(&pfx->pfx_prefix + 1);
1133 while (m < lim && p < ep)
1134 *p++ &= *m++;
1135 while (p < ep)
1136 *p++ = 0x00;
1137 if (!inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf,
1138 sizeof(ntopbuf))) {
1139 syslog(LOG_ERR, "<%s> inet_ntop failed", __func__);
1140 exit(1);
1141 }
1142 syslog(LOG_DEBUG,
1143 "<%s> add %s/%d to prefix list on %s",
1144 __func__, ntopbuf, pfx->pfx_prefixlen, ifi->ifi_ifname);
1145
1146 /* set other fields with protocol defaults */
1147 pfx->pfx_validlifetime = DEF_ADVVALIDLIFETIME;
1148 pfx->pfx_preflifetime = DEF_ADVPREFERREDLIFETIME;
1149 pfx->pfx_onlinkflg = 1;
1150 pfx->pfx_autoconfflg = 1;
1151 pfx->pfx_origin = PREFIX_FROM_KERNEL;
1152 pfx->pfx_rainfo = rai;
1153
1154 /* link into chain */
1155 TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next);
1156
1157 /* counter increment */
1158 rai->rai_pfxs++;
1159 }
1160
1161 freeifaddrs(ifap);
1162 }
1163
1164 static void
makeentry(char * buf,size_t len,int id,const char * string)1165 makeentry(char *buf, size_t len, int id, const char *string)
1166 {
1167
1168 if (id < 0)
1169 strlcpy(buf, string, len);
1170 else
1171 snprintf(buf, len, "%s%d", string, id);
1172 }
1173
1174 /*
1175 * Add a prefix to the list of specified interface and reconstruct
1176 * the outgoing packet.
1177 * The prefix must not be in the list.
1178 * XXX: other parameters of the prefix (e.g. lifetime) should be
1179 * able to be specified.
1180 */
1181 static void
add_prefix(struct rainfo * rai,struct in6_prefixreq * ipr)1182 add_prefix(struct rainfo *rai, struct in6_prefixreq *ipr)
1183 {
1184 struct prefix *pfx;
1185 struct ifinfo *ifi;
1186 char ntopbuf[INET6_ADDRSTRLEN];
1187
1188 ifi = rai->rai_ifinfo;
1189 ELM_MALLOC(pfx, return);
1190 pfx->pfx_prefix = ipr->ipr_prefix.sin6_addr;
1191 pfx->pfx_prefixlen = ipr->ipr_plen;
1192 pfx->pfx_validlifetime = ipr->ipr_vltime;
1193 pfx->pfx_preflifetime = ipr->ipr_pltime;
1194 pfx->pfx_onlinkflg = ipr->ipr_raf_onlink;
1195 pfx->pfx_autoconfflg = ipr->ipr_raf_auto;
1196 pfx->pfx_origin = PREFIX_FROM_DYNAMIC;
1197 pfx->pfx_rainfo = rai;
1198
1199 TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next);
1200
1201 syslog(LOG_DEBUG, "<%s> new prefix %s/%d was added on %s",
1202 __func__,
1203 inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf,
1204 sizeof(ntopbuf)), ipr->ipr_plen, ifi->ifi_ifname);
1205
1206 rai->rai_pfxs++;
1207 }
1208
1209 /*
1210 * Delete a prefix to the list of specified interface and reconstruct
1211 * the outgoing packet.
1212 * The prefix must be in the list.
1213 */
1214 void
delete_prefix(struct prefix * pfx)1215 delete_prefix(struct prefix *pfx)
1216 {
1217 struct rainfo *rai;
1218 struct ifinfo *ifi;
1219 char ntopbuf[INET6_ADDRSTRLEN];
1220
1221 rai = pfx->pfx_rainfo;
1222 ifi = rai->rai_ifinfo;
1223 TAILQ_REMOVE(&rai->rai_prefix, pfx, pfx_next);
1224 syslog(LOG_DEBUG, "<%s> prefix %s/%d was deleted on %s",
1225 __func__,
1226 inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf,
1227 sizeof(ntopbuf)), pfx->pfx_prefixlen, ifi->ifi_ifname);
1228 if (pfx->pfx_timer)
1229 rtadvd_remove_timer(pfx->pfx_timer);
1230 free(pfx);
1231
1232 rai->rai_pfxs--;
1233 }
1234
1235 void
invalidate_prefix(struct prefix * pfx)1236 invalidate_prefix(struct prefix *pfx)
1237 {
1238 struct timespec timo;
1239 struct rainfo *rai;
1240 struct ifinfo *ifi;
1241 char ntopbuf[INET6_ADDRSTRLEN];
1242
1243 rai = pfx->pfx_rainfo;
1244 ifi = rai->rai_ifinfo;
1245 if (pfx->pfx_timer) { /* sanity check */
1246 syslog(LOG_ERR,
1247 "<%s> assumption failure: timer already exists",
1248 __func__);
1249 exit(1);
1250 }
1251
1252 syslog(LOG_DEBUG, "<%s> prefix %s/%d was invalidated on %s, "
1253 "will expire in %ld seconds", __func__,
1254 inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf, sizeof(ntopbuf)),
1255 pfx->pfx_prefixlen, ifi->ifi_ifname, (long)prefix_timo);
1256
1257 /* set the expiration timer */
1258 pfx->pfx_timer = rtadvd_add_timer(prefix_timeout, NULL, pfx, NULL);
1259 if (pfx->pfx_timer == NULL) {
1260 syslog(LOG_ERR, "<%s> failed to add a timer for a prefix. "
1261 "remove the prefix", __func__);
1262 delete_prefix(pfx);
1263 }
1264 timo.tv_sec = prefix_timo;
1265 timo.tv_nsec = 0;
1266 rtadvd_set_timer(&timo, pfx->pfx_timer);
1267 }
1268
1269 static struct rtadvd_timer *
prefix_timeout(void * arg)1270 prefix_timeout(void *arg)
1271 {
1272
1273 delete_prefix((struct prefix *)arg);
1274
1275 return (NULL);
1276 }
1277
1278 void
update_prefix(struct prefix * pfx)1279 update_prefix(struct prefix *pfx)
1280 {
1281 struct rainfo *rai;
1282 struct ifinfo *ifi;
1283 char ntopbuf[INET6_ADDRSTRLEN];
1284
1285 rai = pfx->pfx_rainfo;
1286 ifi = rai->rai_ifinfo;
1287 if (pfx->pfx_timer == NULL) { /* sanity check */
1288 syslog(LOG_ERR,
1289 "<%s> assumption failure: timer does not exist",
1290 __func__);
1291 exit(1);
1292 }
1293
1294 syslog(LOG_DEBUG, "<%s> prefix %s/%d was re-enabled on %s",
1295 __func__, inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf,
1296 sizeof(ntopbuf)), pfx->pfx_prefixlen, ifi->ifi_ifname);
1297
1298 /* stop the expiration timer */
1299 rtadvd_remove_timer(pfx->pfx_timer);
1300 pfx->pfx_timer = NULL;
1301 }
1302
1303 /*
1304 * Try to get an in6_prefixreq contents for a prefix which matches
1305 * ipr->ipr_prefix and ipr->ipr_plen and belongs to
1306 * the interface whose name is ipr->ipr_name[].
1307 */
1308 static int
init_prefix(struct in6_prefixreq * ipr)1309 init_prefix(struct in6_prefixreq *ipr)
1310 {
1311 #if 0
1312 int s;
1313
1314 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1315 syslog(LOG_ERR, "<%s> socket: %s", __func__,
1316 strerror(errno));
1317 exit(1);
1318 }
1319
1320 if (ioctl(s, SIOCGIFPREFIX_IN6, (caddr_t)ipr) < 0) {
1321 syslog(LOG_INFO, "<%s> ioctl:SIOCGIFPREFIX %s", __func__,
1322 strerror(errno));
1323
1324 ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
1325 ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
1326 ipr->ipr_raf_onlink = 1;
1327 ipr->ipr_raf_auto = 1;
1328 /* omit other field initialization */
1329 }
1330 else if (ipr->ipr_origin < PR_ORIG_RR) {
1331 char ntopbuf[INET6_ADDRSTRLEN];
1332
1333 syslog(LOG_WARNING, "<%s> Added prefix(%s)'s origin %d is"
1334 "lower than PR_ORIG_RR(router renumbering)."
1335 "This should not happen if I am router", __func__,
1336 inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf,
1337 sizeof(ntopbuf)), ipr->ipr_origin);
1338 close(s);
1339 return (1);
1340 }
1341
1342 close(s);
1343 return (0);
1344 #else
1345 ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
1346 ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
1347 ipr->ipr_raf_onlink = 1;
1348 ipr->ipr_raf_auto = 1;
1349 return (0);
1350 #endif
1351 }
1352
1353 void
make_prefix(struct rainfo * rai,int ifindex,struct in6_addr * addr,int plen)1354 make_prefix(struct rainfo *rai, int ifindex, struct in6_addr *addr, int plen)
1355 {
1356 struct in6_prefixreq ipr;
1357
1358 memset(&ipr, 0, sizeof(ipr));
1359 if (if_indextoname(ifindex, ipr.ipr_name) == NULL) {
1360 syslog(LOG_ERR, "<%s> Prefix added interface No.%d doesn't "
1361 "exist. This should not happen! %s", __func__,
1362 ifindex, strerror(errno));
1363 exit(1);
1364 }
1365 ipr.ipr_prefix.sin6_len = sizeof(ipr.ipr_prefix);
1366 ipr.ipr_prefix.sin6_family = AF_INET6;
1367 ipr.ipr_prefix.sin6_addr = *addr;
1368 ipr.ipr_plen = plen;
1369
1370 if (init_prefix(&ipr))
1371 return; /* init failed by some error */
1372 add_prefix(rai, &ipr);
1373 }
1374
1375 void
make_packet(struct rainfo * rai)1376 make_packet(struct rainfo *rai)
1377 {
1378 size_t packlen, lladdroptlen = 0;
1379 char *buf;
1380 struct nd_router_advert *ra;
1381 struct nd_opt_prefix_info *ndopt_pi;
1382 struct nd_opt_mtu *ndopt_mtu;
1383 struct nd_opt_route_info *ndopt_rti;
1384 struct rtinfo *rti;
1385 struct nd_opt_rdnss *ndopt_rdnss;
1386 struct rdnss *rdn;
1387 struct nd_opt_dnssl *ndopt_dnssl;
1388 struct dnssl *dns;
1389 struct nd_opt_pref64 *ndopt_pref64;
1390 size_t len;
1391 struct prefix *pfx;
1392 struct ifinfo *ifi;
1393
1394 ifi = rai->rai_ifinfo;
1395 /* calculate total length */
1396 packlen = sizeof(struct nd_router_advert);
1397 if (rai->rai_advlinkopt) {
1398 if ((lladdroptlen = lladdropt_length(&ifi->ifi_sdl)) == 0) {
1399 syslog(LOG_INFO,
1400 "<%s> link-layer address option has"
1401 " null length on %s. Treat as not included.",
1402 __func__, ifi->ifi_ifname);
1403 rai->rai_advlinkopt = 0;
1404 }
1405 packlen += lladdroptlen;
1406 }
1407 if (rai->rai_pfxs)
1408 packlen += sizeof(struct nd_opt_prefix_info) * rai->rai_pfxs;
1409 if (rai->rai_linkmtu)
1410 packlen += sizeof(struct nd_opt_mtu);
1411 if (rai->rai_pref64.p64_enabled)
1412 packlen += sizeof(struct nd_opt_pref64);
1413
1414 TAILQ_FOREACH(rti, &rai->rai_route, rti_next)
1415 packlen += sizeof(struct nd_opt_route_info) +
1416 ((rti->rti_prefixlen + 0x3f) >> 6) * 8;
1417
1418 TAILQ_FOREACH(rdn, &rai->rai_rdnss, rd_next) {
1419 struct rdnss_addr *rdna;
1420
1421 packlen += sizeof(struct nd_opt_rdnss);
1422 TAILQ_FOREACH(rdna, &rdn->rd_list, ra_next)
1423 packlen += sizeof(rdna->ra_dns);
1424 }
1425 TAILQ_FOREACH(dns, &rai->rai_dnssl, dn_next) {
1426 struct dnssl_addr *dnsa;
1427
1428 packlen += sizeof(struct nd_opt_dnssl);
1429 len = 0;
1430 TAILQ_FOREACH(dnsa, &dns->dn_list, da_next)
1431 len += dnsa->da_len;
1432
1433 /* A zero octet and 8 octet boundary */
1434 len++;
1435 len += (len % 8) ? 8 - len % 8 : 0;
1436
1437 packlen += len;
1438 }
1439 /* allocate memory for the packet */
1440 if ((buf = malloc(packlen)) == NULL) {
1441 syslog(LOG_ERR,
1442 "<%s> can't get enough memory for an RA packet",
1443 __func__);
1444 exit(1);
1445 }
1446 memset(buf, 0, packlen);
1447 if (rai->rai_ra_data) /* Free old data if any. */
1448 free(rai->rai_ra_data);
1449 rai->rai_ra_data = buf;
1450 /* XXX: what if packlen > 576? */
1451 rai->rai_ra_datalen = packlen;
1452
1453 /*
1454 * construct the packet
1455 */
1456 ra = (struct nd_router_advert *)buf;
1457 ra->nd_ra_type = ND_ROUTER_ADVERT;
1458 ra->nd_ra_code = 0;
1459 ra->nd_ra_cksum = 0;
1460 ra->nd_ra_curhoplimit = (uint8_t)(0xff & rai->rai_hoplimit);
1461 /*
1462 * XXX: the router preference field, which is a 2-bit field, should be
1463 * initialized before other fields.
1464 */
1465 ra->nd_ra_flags_reserved = 0xff & rai->rai_rtpref;
1466 ra->nd_ra_flags_reserved |=
1467 rai->rai_managedflg ? ND_RA_FLAG_MANAGED : 0;
1468 ra->nd_ra_flags_reserved |=
1469 rai->rai_otherflg ? ND_RA_FLAG_OTHER : 0;
1470 #ifdef DRAFT_IETF_6MAN_IPV6ONLY_FLAG
1471 ra->nd_ra_flags_reserved |=
1472 rai->rai_ipv6onlyflg ? ND_RA_FLAG_IPV6_ONLY : 0;
1473 #endif
1474 ra->nd_ra_router_lifetime = htons(rai->rai_lifetime);
1475 ra->nd_ra_reachable = htonl(rai->rai_reachabletime);
1476 ra->nd_ra_retransmit = htonl(rai->rai_retranstimer);
1477 buf += sizeof(*ra);
1478
1479 if (rai->rai_advlinkopt) {
1480 lladdropt_fill(&ifi->ifi_sdl, (struct nd_opt_hdr *)buf);
1481 buf += lladdroptlen;
1482 }
1483
1484 if (rai->rai_linkmtu) {
1485 ndopt_mtu = (struct nd_opt_mtu *)buf;
1486 ndopt_mtu->nd_opt_mtu_type = ND_OPT_MTU;
1487 ndopt_mtu->nd_opt_mtu_len = 1;
1488 ndopt_mtu->nd_opt_mtu_reserved = 0;
1489 ndopt_mtu->nd_opt_mtu_mtu = htonl(rai->rai_linkmtu);
1490 buf += sizeof(struct nd_opt_mtu);
1491 }
1492
1493 if (rai->rai_pref64.p64_enabled) {
1494 ndopt_pref64 = (struct nd_opt_pref64 *)buf;
1495 ndopt_pref64->nd_opt_pref64_type = ND_OPT_PREF64;
1496 ndopt_pref64->nd_opt_pref64_len = 2;
1497 ndopt_pref64->nd_opt_pref64_sl_plc =
1498 (htons(rai->rai_pref64.p64_sl << 3)) |
1499 htons((rai->rai_pref64.p64_plc & 0x7));
1500 memcpy(&ndopt_pref64->nd_opt_prefix[0],
1501 &rai->rai_pref64.p64_prefix,
1502 sizeof(ndopt_pref64->nd_opt_prefix));
1503 buf += sizeof(struct nd_opt_pref64);
1504 }
1505
1506 TAILQ_FOREACH(pfx, &rai->rai_prefix, pfx_next) {
1507 uint32_t vltime, pltime;
1508 struct timespec now;
1509
1510 ndopt_pi = (struct nd_opt_prefix_info *)buf;
1511 ndopt_pi->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
1512 ndopt_pi->nd_opt_pi_len = 4;
1513 ndopt_pi->nd_opt_pi_prefix_len = pfx->pfx_prefixlen;
1514 ndopt_pi->nd_opt_pi_flags_reserved = 0;
1515 if (pfx->pfx_onlinkflg)
1516 ndopt_pi->nd_opt_pi_flags_reserved |=
1517 ND_OPT_PI_FLAG_ONLINK;
1518 if (pfx->pfx_autoconfflg)
1519 ndopt_pi->nd_opt_pi_flags_reserved |=
1520 ND_OPT_PI_FLAG_AUTO;
1521 if (pfx->pfx_timer)
1522 vltime = 0;
1523 else {
1524 if (pfx->pfx_vltimeexpire || pfx->pfx_pltimeexpire)
1525 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
1526 if (pfx->pfx_vltimeexpire == 0)
1527 vltime = pfx->pfx_validlifetime;
1528 else
1529 vltime = ((time_t)pfx->pfx_vltimeexpire > now.tv_sec) ?
1530 pfx->pfx_vltimeexpire - now.tv_sec : 0;
1531 }
1532 if (pfx->pfx_timer)
1533 pltime = 0;
1534 else {
1535 if (pfx->pfx_pltimeexpire == 0)
1536 pltime = pfx->pfx_preflifetime;
1537 else
1538 pltime = ((time_t)pfx->pfx_pltimeexpire > now.tv_sec) ?
1539 pfx->pfx_pltimeexpire - now.tv_sec : 0;
1540 }
1541 if (vltime < pltime) {
1542 /*
1543 * this can happen if vltime is decrement but pltime
1544 * is not.
1545 */
1546 pltime = vltime;
1547 }
1548 ndopt_pi->nd_opt_pi_valid_time = htonl(vltime);
1549 ndopt_pi->nd_opt_pi_preferred_time = htonl(pltime);
1550 ndopt_pi->nd_opt_pi_reserved2 = 0;
1551 ndopt_pi->nd_opt_pi_prefix = pfx->pfx_prefix;
1552
1553 buf += sizeof(struct nd_opt_prefix_info);
1554 }
1555
1556 TAILQ_FOREACH(rti, &rai->rai_route, rti_next) {
1557 uint8_t psize = (rti->rti_prefixlen + 0x3f) >> 6;
1558
1559 ndopt_rti = (struct nd_opt_route_info *)buf;
1560 ndopt_rti->nd_opt_rti_type = ND_OPT_ROUTE_INFO;
1561 ndopt_rti->nd_opt_rti_len = 1 + psize;
1562 ndopt_rti->nd_opt_rti_prefixlen = rti->rti_prefixlen;
1563 ndopt_rti->nd_opt_rti_flags = 0xff & rti->rti_rtpref;
1564 ndopt_rti->nd_opt_rti_lifetime = htonl(rti->rti_ltime);
1565 memcpy(ndopt_rti + 1, &rti->rti_prefix, psize * 8);
1566 buf += sizeof(struct nd_opt_route_info) + psize * 8;
1567 }
1568
1569 TAILQ_FOREACH(rdn, &rai->rai_rdnss, rd_next) {
1570 struct rdnss_addr *rdna;
1571
1572 ndopt_rdnss = (struct nd_opt_rdnss *)buf;
1573 ndopt_rdnss->nd_opt_rdnss_type = ND_OPT_RDNSS;
1574 ndopt_rdnss->nd_opt_rdnss_len = 0;
1575 ndopt_rdnss->nd_opt_rdnss_reserved = 0;
1576 ndopt_rdnss->nd_opt_rdnss_lifetime = htonl(rdn->rd_ltime);
1577 buf += sizeof(struct nd_opt_rdnss);
1578
1579 TAILQ_FOREACH(rdna, &rdn->rd_list, ra_next) {
1580 memcpy(buf, &rdna->ra_dns, sizeof(rdna->ra_dns));
1581 buf += sizeof(rdna->ra_dns);
1582 }
1583 /* Length field should be in 8 octets */
1584 ndopt_rdnss->nd_opt_rdnss_len = (buf - (char *)ndopt_rdnss) / 8;
1585
1586 syslog(LOG_DEBUG, "<%s>: nd_opt_dnss_len = %d", __func__,
1587 ndopt_rdnss->nd_opt_rdnss_len);
1588 }
1589
1590 TAILQ_FOREACH(dns, &rai->rai_dnssl, dn_next) {
1591 struct dnssl_addr *dnsa;
1592
1593 ndopt_dnssl = (struct nd_opt_dnssl *)buf;
1594 ndopt_dnssl->nd_opt_dnssl_type = ND_OPT_DNSSL;
1595 ndopt_dnssl->nd_opt_dnssl_len = 0;
1596 ndopt_dnssl->nd_opt_dnssl_reserved = 0;
1597 ndopt_dnssl->nd_opt_dnssl_lifetime = htonl(dns->dn_ltime);
1598 buf += sizeof(*ndopt_dnssl);
1599
1600 TAILQ_FOREACH(dnsa, &dns->dn_list, da_next) {
1601 memcpy(buf, dnsa->da_dom, dnsa->da_len);
1602 buf += dnsa->da_len;
1603 }
1604
1605 /* A zero octet after encoded DNS server list. */
1606 *buf++ = '\0';
1607
1608 /* Padding to next 8 octets boundary */
1609 len = buf - (char *)ndopt_dnssl;
1610 len += (len % 8) ? 8 - len % 8 : 0;
1611 buf = (char *)ndopt_dnssl + len;
1612
1613 /* Length field must be in 8 octets */
1614 ndopt_dnssl->nd_opt_dnssl_len = len / 8;
1615
1616 syslog(LOG_DEBUG, "<%s>: nd_opt_dnssl_len = %d", __func__,
1617 ndopt_dnssl->nd_opt_dnssl_len);
1618 }
1619 }
1620