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