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