16677b8c6168ff41acda713651246400c856f367
[dragonfly.git] / usr.sbin / rtadvd / config.c
1 /*      $KAME: config.c,v 1.37 2001/05/25 07:34:00 itojun Exp $ */
2
3 /*
4  * Copyright (C) 1998 WIDE Project.
5  * All rights reserved.
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  * 
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * $FreeBSD: src/usr.sbin/rtadvd/config.c,v 1.3.2.5 2003/04/22 09:40:57 suz Exp $
32  * $DragonFly: src/usr.sbin/rtadvd/config.c,v 1.4 2004/02/10 02:59:43 rob Exp $
33  */
34
35 #include <sys/param.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 #include <sys/time.h>
39 #include <sys/sysctl.h>
40
41 #include <net/if.h>
42 #if defined(__DragonFly__) 
43 #include <net/if_var.h>
44 #endif /* __DragonFly__  */
45 #include <net/route.h>
46 #include <net/if_dl.h>
47
48 #include <netinet/in.h>
49 #include <netinet/in_var.h>
50 #include <netinet/ip6.h>
51 #include <netinet6/ip6_var.h>
52 #include <netinet/icmp6.h>
53 #ifdef MIP6
54 #include <netinet6/mip6.h>
55 #endif
56
57 #include <arpa/inet.h>
58
59 #include <stdio.h>
60 #include <syslog.h>
61 #include <errno.h>
62 #include <string.h>
63 #include <stdlib.h>
64 #if defined(__NetBSD__) || defined(__OpenBSD__)
65 #include <search.h>
66 #endif
67 #include <unistd.h>
68 #include <ifaddrs.h>
69
70 #include "rtadvd.h"
71 #include "advcap.h"
72 #include "timer.h"
73 #include "if.h"
74 #include "config.h"
75
76 static time_t prefix_timo = (60 * 120); /* 2 hours.
77                                          * XXX: should be configurable. */
78 extern struct rainfo *ralist;
79
80 static struct rtadvd_timer *prefix_timeout(void *);
81 static void makeentry(char *, size_t, int, char *, int);
82 static void get_prefix(struct rainfo *);
83 static int getinet6sysctl(int);
84
85 void
86 getconfig(intface)
87         char *intface;
88 {
89         int stat, pfxs, i;
90         char tbuf[BUFSIZ];
91         struct rainfo *tmp;
92         long val;
93         long long val64;
94         char buf[BUFSIZ];
95         char *bp = buf;
96         char *addr;
97         static int forwarding = -1;
98
99 #define MUSTHAVE(var, cap)      \
100     do {                                                                \
101         int t;                                                          \
102         if ((t = agetnum(cap)) < 0) {                                   \
103                 fprintf(stderr, "rtadvd: need %s for interface %s\n",   \
104                         cap, intface);                                  \
105                 exit(1);                                                \
106         }                                                               \
107         var = t;                                                        \
108      } while (0)
109 #define MAYHAVE(var, cap, def)  \
110      do {                                                               \
111         if ((var = agetnum(cap)) < 0)                                   \
112                 var = def;                                              \
113      } while (0)
114
115         if ((stat = agetent(tbuf, intface)) <= 0) {
116                 memset(tbuf, 0, sizeof(tbuf));
117                 syslog(LOG_INFO,
118                        "<%s> %s isn't defined in the configuration file"
119                        " or the configuration file doesn't exist."
120                        " Treat it as default",
121                         __FUNCTION__, intface);
122         }
123
124         tmp = (struct rainfo *)malloc(sizeof(*ralist));
125         memset(tmp, 0, sizeof(*tmp));
126         tmp->prefix.next = tmp->prefix.prev = &tmp->prefix;
127         tmp->route.next = tmp->route.prev = &tmp->route;
128
129         /* check if we are allowed to forward packets (if not determined) */
130         if (forwarding < 0) {
131                 if ((forwarding = getinet6sysctl(IPV6CTL_FORWARDING)) < 0)
132                         exit(1);
133         }
134
135         /* get interface information */
136         if (agetflag("nolladdr"))
137                 tmp->advlinkopt = 0;
138         else
139                 tmp->advlinkopt = 1;
140         if (tmp->advlinkopt) {
141                 if ((tmp->sdl = if_nametosdl(intface)) == NULL) {
142                         syslog(LOG_ERR,
143                                "<%s> can't get information of %s",
144                                __FUNCTION__, intface);
145                         exit(1);
146                 }
147                 tmp->ifindex = tmp->sdl->sdl_index;
148         } else
149                 tmp->ifindex = if_nametoindex(intface);
150         strncpy(tmp->ifname, intface, sizeof(tmp->ifname));
151         if ((tmp->phymtu = if_getmtu(intface)) == 0) {
152                 tmp->phymtu = IPV6_MMTU;
153                 syslog(LOG_WARNING,
154                        "<%s> can't get interface mtu of %s. Treat as %d",
155                        __FUNCTION__, intface, IPV6_MMTU);
156         }
157
158         /*
159          * set router configuration variables.
160          */
161         MAYHAVE(val, "maxinterval", DEF_MAXRTRADVINTERVAL);
162         if (val < MIN_MAXINTERVAL || val > MAX_MAXINTERVAL) {
163                 syslog(LOG_ERR,
164                        "<%s> maxinterval must be between %e and %u",
165                        __FUNCTION__, MIN_MAXINTERVAL, MAX_MAXINTERVAL);
166                 exit(1);
167         }
168         tmp->maxinterval = (u_int)val;
169         MAYHAVE(val, "mininterval", tmp->maxinterval/3);
170         if (val < MIN_MININTERVAL || val > (tmp->maxinterval * 3) / 4) {
171                 syslog(LOG_ERR,
172                        "<%s> mininterval must be between %e and %d",
173                        __FUNCTION__,
174                        MIN_MININTERVAL,
175                        (tmp->maxinterval * 3) / 4);
176                 exit(1);
177         }
178         tmp->mininterval = (u_int)val;
179
180         MAYHAVE(val, "chlim", DEF_ADVCURHOPLIMIT);
181         tmp->hoplimit = val & 0xff;
182
183         MAYHAVE(val, "raflags", 0);
184         tmp->managedflg = val & ND_RA_FLAG_MANAGED;
185         tmp->otherflg = val & ND_RA_FLAG_OTHER;
186 #ifdef MIP6
187         if (mobileip6)
188                 tmp->haflg = val & ND_RA_FLAG_HA;
189 #endif
190 #ifndef ND_RA_FLAG_RTPREF_MASK
191 #define ND_RA_FLAG_RTPREF_MASK  0x18 /* 00011000 */
192 #define ND_RA_FLAG_RTPREF_RSV   0x10 /* 00010000 */
193 #endif
194         tmp->rtpref = val & ND_RA_FLAG_RTPREF_MASK;
195         if (tmp->rtpref == ND_RA_FLAG_RTPREF_RSV) {
196                 syslog(LOG_ERR, "<%s> invalid router preference on %s",
197                        __FUNCTION__, intface);
198                 exit(1);
199         }
200
201         MAYHAVE(val, "rltime", tmp->maxinterval * 3);
202         if (val && (val < tmp->maxinterval || val > MAXROUTERLIFETIME)) {
203                 syslog(LOG_ERR,
204                        "<%s> router lifetime on %s must be 0 or"
205                        " between %d and %d",
206                        __FUNCTION__, intface,
207                        tmp->maxinterval, MAXROUTERLIFETIME);
208                 exit(1);
209         }
210         /*
211          * Basically, hosts MUST NOT send Router Advertisement messages at any
212          * time (RFC 2461, Section 6.2.3). However, it would sometimes be
213          * useful to allow hosts to advertise some parameters such as prefix
214          * information and link MTU. Thus, we allow hosts to invoke rtadvd
215          * only when router lifetime (on every advertising interface) is
216          * explicitly set zero. (see also the above section)
217          */
218         if (val && forwarding == 0) {
219                 syslog(LOG_WARNING,
220                        "<%s> non zero router lifetime is specified for %s, "
221                        "which must not be allowed for hosts.",
222                        __FUNCTION__, intface);
223                 exit(1);
224         }
225         tmp->lifetime = val & 0xffff;
226
227         MAYHAVE(val, "rtime", DEF_ADVREACHABLETIME);
228         if (val > MAXREACHABLETIME) {
229                 syslog(LOG_ERR,
230                        "<%s> reachable time must be no greater than %d",
231                        __FUNCTION__, MAXREACHABLETIME);
232                 exit(1);
233         }
234         tmp->reachabletime = (u_int32_t)val;
235
236         MAYHAVE(val64, "retrans", DEF_ADVRETRANSTIMER);
237         if (val64 < 0 || val64 > 0xffffffff) {
238                 syslog(LOG_ERR,
239                        "<%s> retrans time out of range", __FUNCTION__);
240                 exit(1);
241         }
242         tmp->retranstimer = (u_int32_t)val64;
243
244 #ifndef MIP6
245         if (agetstr("hapref", &bp) || agetstr("hatime", &bp)) {
246                 syslog(LOG_ERR,
247                        "<%s> mobile-ip6 configuration not supported",
248                        __FUNCTION__);
249                 exit(1);
250         }
251 #else
252         if (!mobileip6) {
253                 if (agetstr("hapref", &bp) || agetstr("hatime", &bp)) {
254                         syslog(LOG_ERR,
255                                "<%s> mobile-ip6 configuration without "
256                                "proper command line option",
257                                __FUNCTION__);
258                         exit(1);
259                 }
260         } else {
261                 tmp->hapref = 0;
262                 if ((val = agetnum("hapref")) >= 0)
263                         tmp->hapref = (int16_t)val;
264                 if (tmp->hapref != 0) {
265                         tmp->hatime = 0;
266                         MUSTHAVE(val, "hatime");
267                         tmp->hatime = (u_int16_t)val;
268                         if (tmp->hatime <= 0) {
269                                 syslog(LOG_ERR,
270                                        "<%s> home agent lifetime must be greater than 0",
271                                        __FUNCTION__);
272                                 exit(1);
273                         }
274                 }
275         }
276 #endif
277
278         /* prefix information */
279
280         /*
281          * This is an implementation specific parameter to consinder
282          * link propagation delays and poorly synchronized clocks when
283          * checking consistency of advertised lifetimes.
284          */
285         MAYHAVE(val, "clockskew", 0);
286         tmp->clockskew = val;
287
288         if ((pfxs = agetnum("addrs")) < 0) {
289                 /* auto configure prefix information */
290                 if (agetstr("addr", &bp) || agetstr("addr1", &bp)) {
291                         syslog(LOG_ERR,
292                                "<%s> conflicting prefix configuration for %s: "
293                                "automatic and manual config at the same time",
294                                __FUNCTION__, intface);
295                         exit(1);
296                 }
297                 get_prefix(tmp);
298         }
299         else {
300                 tmp->pfxs = pfxs;
301                 for (i = 0; i < pfxs; i++) {
302                         struct prefix *pfx;
303                         char entbuf[256];
304                         int added = (pfxs > 1) ? 1 : 0;
305
306                         /* allocate memory to store prefix information */
307                         if ((pfx = malloc(sizeof(struct prefix))) == NULL) {
308                                 syslog(LOG_ERR,
309                                        "<%s> can't allocate enough memory",
310                                        __FUNCTION__);
311                                 exit(1);
312                         }
313                         memset(pfx, 0, sizeof(*pfx));
314
315                         /* link into chain */
316                         insque(pfx, &tmp->prefix);
317                         pfx->rainfo = tmp;
318
319                         pfx->origin = PREFIX_FROM_CONFIG;
320
321                         makeentry(entbuf, sizeof(entbuf), i, "prefixlen",
322                             added);
323                         MAYHAVE(val, entbuf, 64);
324                         if (val < 0 || val > 128) {
325                                 syslog(LOG_ERR,
326                                        "<%s> prefixlen out of range",
327                                        __FUNCTION__);
328                                 exit(1);
329                         }
330                         pfx->prefixlen = (int)val;
331
332                         makeentry(entbuf, sizeof(entbuf), i, "pinfoflags",
333                             added);
334 #ifdef MIP6
335                         if (mobileip6)
336                         {
337                                 MAYHAVE(val, entbuf,
338                                     (ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO|
339                                          ND_OPT_PI_FLAG_ROUTER));
340                         } else
341 #endif
342                         {
343                                 MAYHAVE(val, entbuf,
344                                     (ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO));
345                         }
346                         pfx->onlinkflg = val & ND_OPT_PI_FLAG_ONLINK;
347                         pfx->autoconfflg = val & ND_OPT_PI_FLAG_AUTO;
348 #ifdef MIP6
349                         pfx->routeraddr = val & ND_OPT_PI_FLAG_ROUTER;
350 #endif
351
352                         makeentry(entbuf, sizeof(entbuf), i, "vltime", added);
353                         MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
354                         if (val64 < 0 || val64 > 0xffffffff) {
355                                 syslog(LOG_ERR,
356                                        "<%s> vltime out of range",
357                                        __FUNCTION__);
358                                 exit(1);
359                         }
360                         pfx->validlifetime = (u_int32_t)val64;
361
362                         makeentry(entbuf, sizeof(entbuf), i, "vltimedecr",
363                             added);
364                         if (agetflag(entbuf)) {
365                                 struct timeval now;
366                                 gettimeofday(&now, 0);
367                                 pfx->vltimeexpire =
368                                         now.tv_sec + pfx->validlifetime;
369                         }
370
371                         makeentry(entbuf, sizeof(entbuf), i, "pltime", added);
372                         MAYHAVE(val64, entbuf, DEF_ADVPREFERREDLIFETIME);
373                         if (val64 < 0 || val64 > 0xffffffff) {
374                                 syslog(LOG_ERR,
375                                        "<%s> pltime out of range",
376                                        __FUNCTION__);
377                                 exit(1);
378                         }
379                         pfx->preflifetime = (u_int32_t)val64;
380
381                         makeentry(entbuf, sizeof(entbuf), i, "pltimedecr",
382                             added);
383                         if (agetflag(entbuf)) {
384                                 struct timeval now;
385                                 gettimeofday(&now, 0);
386                                 pfx->pltimeexpire =
387                                         now.tv_sec + pfx->preflifetime;
388                         }
389
390                         makeentry(entbuf, sizeof(entbuf), i, "addr", added);
391                         addr = (char *)agetstr(entbuf, &bp);
392                         if (addr == NULL) {
393                                 syslog(LOG_ERR,
394                                        "<%s> need %s as an prefix for "
395                                        "interface %s",
396                                        __FUNCTION__, entbuf, intface);
397                                 exit(1);
398                         }
399                         if (inet_pton(AF_INET6, addr,
400                                       &pfx->prefix) != 1) {
401                                 syslog(LOG_ERR,
402                                        "<%s> inet_pton failed for %s",
403                                        __FUNCTION__, addr);
404                                 exit(1);
405                         }
406                         if (IN6_IS_ADDR_MULTICAST(&pfx->prefix)) {
407                                 syslog(LOG_ERR,
408                                        "<%s> multicast prefix(%s) must "
409                                        "not be advertised (IF=%s)",
410                                        __FUNCTION__, addr, intface);
411                                 exit(1);
412                         }
413                         if (IN6_IS_ADDR_LINKLOCAL(&pfx->prefix))
414                                 syslog(LOG_NOTICE,
415                                        "<%s> link-local prefix(%s) will be"
416                                        " advertised on %s",
417                                        __FUNCTION__, addr, intface);
418                 }
419         }
420
421         MAYHAVE(val, "mtu", 0);
422         if (val < 0 || val > 0xffffffff) {
423                 syslog(LOG_ERR,
424                        "<%s> mtu out of range", __FUNCTION__);
425                 exit(1);
426         }
427         tmp->linkmtu = (u_int32_t)val;
428         if (tmp->linkmtu == 0) {
429                 char *mtustr;
430
431                 if ((mtustr = (char *)agetstr("mtu", &bp)) &&
432                     strcmp(mtustr, "auto") == 0)
433                         tmp->linkmtu = tmp->phymtu;
434         }
435         else if (tmp->linkmtu < IPV6_MMTU || tmp->linkmtu > tmp->phymtu) {
436                 syslog(LOG_ERR,
437                        "<%s> advertised link mtu must be between"
438                        " least MTU and physical link MTU",
439                        __FUNCTION__);
440                 exit(1);
441         }
442
443         /* route information */
444
445         MAYHAVE(val, "routes", 0);
446         if (val < 0 || val > 0xffffffff) {
447                 syslog(LOG_ERR,
448                        "<%s> number of route information improper", __FUNCTION__);
449                 exit(1);
450         }
451         tmp->routes = val;
452         for (i = 0; i < tmp->routes; i++) {
453                 struct rtinfo *rti;
454                 char entbuf[256];
455                 int added = (tmp->routes > 1) ? 1 : 0;
456
457                 /* allocate memory to store prefix information */
458                 if ((rti = malloc(sizeof(struct rtinfo))) == NULL) {
459                         syslog(LOG_ERR,
460                                "<%s> can't allocate enough memory",
461                                __FUNCTION__);
462                         exit(1);
463                 }
464                 memset(rti, 0, sizeof(*rti));
465
466                 /* link into chain */
467                 insque(rti, &tmp->route);
468
469                 makeentry(entbuf, sizeof(entbuf), i, "rtrplen", added);
470                 MAYHAVE(val, entbuf, 64);
471                 if (val < 0 || val > 128) {
472                         syslog(LOG_ERR,
473                                "<%s> prefixlen out of range",
474                                __FUNCTION__);
475                         exit(1);
476                 }
477                 rti->prefixlen = (int)val;
478
479                 makeentry(entbuf, sizeof(entbuf), i, "rtrflags", added);
480                 MAYHAVE(val, entbuf, 0);
481                 rti->rtpref = val & ND_RA_FLAG_RTPREF_MASK;
482                 if (rti->rtpref == ND_RA_FLAG_RTPREF_RSV) {
483                         syslog(LOG_ERR, "<%s> invalid route preference",
484                                __FUNCTION__);
485                         exit(1);
486                 }
487
488                 makeentry(entbuf, sizeof(entbuf), i, "rtrltime", added);
489                 /*
490                  * XXX: since default value of route lifetime is not defined in
491                  * draft-draves-route-selection-01.txt, I took the default 
492                  * value of valid lifetime of prefix as its default.
493                  * It need be much considered.
494                  */
495                 MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
496                 if (val64 < 0 || val64 > 0xffffffff) {
497                         syslog(LOG_ERR,
498                                "<%s> rtrltime out of range",
499                                __FUNCTION__);
500                         exit(1);
501                 }
502                 rti->ltime = (u_int32_t)val64;
503
504                 makeentry(entbuf, sizeof(entbuf), i, "rtrprefix", added);
505                 addr = (char *)agetstr(entbuf, &bp);
506                 if (addr == NULL) {
507                         syslog(LOG_ERR,
508                                "<%s> need %s as an route for "
509                                "interface %s",
510                                __FUNCTION__, entbuf, intface);
511                         exit(1);
512                 }
513                 if (inet_pton(AF_INET6, addr, &rti->prefix) != 1) {
514                         syslog(LOG_ERR,
515                                "<%s> inet_pton failed for %s",
516                                __FUNCTION__, addr);
517                         exit(1);
518                 }
519 #if 0
520                 /*
521                  * XXX: currently there's no restriction in route information
522                  * prefix according to draft-draves-route-selection-01.txt,
523                  * however I think the similar restriction be necessary.
524                  */
525                 MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
526                 if (IN6_IS_ADDR_MULTICAST(&rti->prefix)) {
527                         syslog(LOG_ERR,
528                                "<%s> multicast route (%s) must "
529                                "not be advertised (IF=%s)",
530                                __FUNCTION__, addr, intface);
531                         exit(1);
532                 }
533                 if (IN6_IS_ADDR_LINKLOCAL(&rti->prefix)) {
534                         syslog(LOG_NOTICE,
535                                "<%s> link-local route (%s) must "
536                                "not be advertised on %s",
537                                __FUNCTION__, addr, intface);
538                         exit(1);
539                 }
540 #endif
541         }
542
543         /* okey */
544         tmp->next = ralist;
545         ralist = tmp;
546
547         /* construct the sending packet */
548         make_packet(tmp);
549
550         /* set timer */
551         tmp->timer = rtadvd_add_timer(ra_timeout, ra_timer_update,
552                                       tmp, tmp);
553         ra_timer_update((void *)tmp, &tmp->timer->tm);
554         rtadvd_set_timer(&tmp->timer->tm, tmp->timer);
555 }
556
557 static void
558 get_prefix(struct rainfo *rai)
559 {
560         struct ifaddrs *ifap, *ifa;
561         struct prefix *pp;
562         struct in6_addr *a;
563         u_char *p, *ep, *m, *lim;
564         u_char ntopbuf[INET6_ADDRSTRLEN];
565
566         if (getifaddrs(&ifap) < 0) {
567                 syslog(LOG_ERR,
568                        "<%s> can't get interface addresses",
569                        __FUNCTION__);
570                 exit(1);
571         }
572         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
573                 int plen;
574
575                 if (strcmp(ifa->ifa_name, rai->ifname) != 0)
576                         continue;
577                 if (ifa->ifa_addr->sa_family != AF_INET6)
578                         continue;
579                 a = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
580                 if (IN6_IS_ADDR_LINKLOCAL(a))
581                         continue;
582
583                 /* get prefix length */
584                 m = (u_char *)&((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
585                 lim = (u_char *)(ifa->ifa_netmask) + ifa->ifa_netmask->sa_len;
586                 plen = prefixlen(m, lim);
587                 if (plen < 0 || plen > 128) {
588                         syslog(LOG_ERR, "<%s> failed to get prefixlen "
589                                "or prefix is invalid",
590                                __FUNCTION__);
591                         exit(1);
592                 }
593                 if (find_prefix(rai, a, plen)) {
594                         /* ignore a duplicated prefix. */
595                         continue;
596                 }
597
598                 /* allocate memory to store prefix info. */
599                 if ((pp = malloc(sizeof(*pp))) == NULL) {
600                         syslog(LOG_ERR,
601                                "<%s> can't get allocate buffer for prefix",
602                                __FUNCTION__);
603                         exit(1);
604                 }
605                 memset(pp, 0, sizeof(*pp));
606
607                 /* set prefix, sweep bits outside of prefixlen */
608                 pp->prefixlen = plen;
609                 memcpy(&pp->prefix, a, sizeof(*a));
610                 p = (u_char *)&pp->prefix;
611                 ep = (u_char *)(&pp->prefix + 1);
612                 while (m < lim)
613                         *p++ &= *m++;
614                 while (p < ep)
615                         *p++ = 0x00;
616
617                 if (!inet_ntop(AF_INET6, &pp->prefix, ntopbuf,
618                     sizeof(ntopbuf))) {
619                         syslog(LOG_ERR, "<%s> inet_ntop failed", __FUNCTION__);
620                         exit(1);
621                 }
622                 syslog(LOG_DEBUG,
623                        "<%s> add %s/%d to prefix list on %s",
624                        __FUNCTION__, ntopbuf, pp->prefixlen, rai->ifname);
625
626                 /* set other fields with protocol defaults */
627                 pp->validlifetime = DEF_ADVVALIDLIFETIME;
628                 pp->preflifetime = DEF_ADVPREFERREDLIFETIME;
629                 pp->onlinkflg = 1;
630                 pp->autoconfflg = 1;
631                 pp->origin = PREFIX_FROM_KERNEL;
632
633                 /* link into chain */
634                 insque(pp, &rai->prefix);
635
636                 /* counter increment */
637                 rai->pfxs++;
638         }
639
640         freeifaddrs(ifap);
641 }
642
643 static void
644 makeentry(buf, len, id, string, add)
645         char *buf;
646         size_t len;
647         int id;
648         char *string;
649         int add;
650 {
651         char *ep = buf + len;
652
653         strcpy(buf, string);
654         if (add) {
655                 char *cp;
656
657                 cp = (char *)index(buf, '\0');
658                 snprintf(cp, ep - cp, "%d", id);
659         }
660 }
661
662 /*
663  * Add a prefix to the list of specified interface and reconstruct
664  * the outgoing packet.
665  * The prefix must not be in the list.
666  * XXX: other parameter of the prefix(e.g. lifetime) shoule be
667  * able to be specified.
668  */
669 static void
670 add_prefix(struct rainfo *rai, struct in6_prefixreq *ipr)
671 {
672         struct prefix *prefix;
673         u_char ntopbuf[INET6_ADDRSTRLEN];
674
675         if ((prefix = malloc(sizeof(*prefix))) == NULL) {
676                 syslog(LOG_ERR, "<%s> memory allocation failed",
677                        __FUNCTION__);
678                 return;         /* XXX: error or exit? */
679         }
680         memset(prefix, 0, sizeof(*prefix));
681         prefix->prefix = ipr->ipr_prefix.sin6_addr;
682         prefix->prefixlen = ipr->ipr_plen;
683         prefix->validlifetime = ipr->ipr_vltime;
684         prefix->preflifetime = ipr->ipr_pltime;
685         prefix->onlinkflg = ipr->ipr_raf_onlink;
686         prefix->autoconfflg = ipr->ipr_raf_auto;
687         prefix->origin = PREFIX_FROM_DYNAMIC;
688
689         insque(prefix, &rai->prefix);
690         prefix->rainfo = rai;
691
692         syslog(LOG_DEBUG, "<%s> new prefix %s/%d was added on %s",
693                __FUNCTION__, inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr,
694                                        ntopbuf, INET6_ADDRSTRLEN),
695                ipr->ipr_plen, rai->ifname);
696
697         /* free the previous packet */
698         free(rai->ra_data);
699         rai->ra_data = NULL;
700
701         /* reconstruct the packet */
702         rai->pfxs++;
703         make_packet(rai);
704
705         /*
706          * reset the timer so that the new prefix will be advertised quickly.
707          */
708         rai->initcounter = 0;
709         ra_timer_update((void *)rai, &rai->timer->tm);
710         rtadvd_set_timer(&rai->timer->tm, rai->timer);
711 }
712
713 /*
714  * Delete a prefix to the list of specified interface and reconstruct
715  * the outgoing packet.
716  * The prefix must be in the list.
717  */
718 void
719 delete_prefix(struct prefix *prefix)
720 {
721         u_char ntopbuf[INET6_ADDRSTRLEN];
722         struct rainfo *rai = prefix->rainfo;
723
724         remque(prefix);
725         syslog(LOG_DEBUG, "<%s> prefix %s/%d was deleted on %s",
726                __FUNCTION__, inet_ntop(AF_INET6, &prefix->prefix,
727                                        ntopbuf, INET6_ADDRSTRLEN),
728                prefix->prefixlen, rai->ifname);
729         if (prefix->timer)
730                 rtadvd_remove_timer(&prefix->timer);
731         free(prefix);
732         rai->pfxs--;
733 }
734
735 void
736 invalidate_prefix(struct prefix *prefix)
737 {
738         u_char ntopbuf[INET6_ADDRSTRLEN];
739         struct timeval timo;
740         struct rainfo *rai = prefix->rainfo;
741
742         if (prefix->timer) {    /* sanity check */
743                 syslog(LOG_ERR,
744                     "<%s> assumption failure: timer already exists",
745                     __FUNCTION__);
746                 exit(1);
747         }
748
749         syslog(LOG_DEBUG, "<%s> prefix %s/%d was invalidated on %s, "
750             "will expire in %ld seconds", __FUNCTION__,
751             inet_ntop(AF_INET6, &prefix->prefix, ntopbuf, INET6_ADDRSTRLEN),
752             prefix->prefixlen, rai->ifname, (long)prefix_timo);
753
754         /* set the expiration timer */
755         prefix->timer = rtadvd_add_timer(prefix_timeout, NULL, prefix, NULL);
756         if (prefix->timer == NULL) {
757                 syslog(LOG_ERR, "<%s> failed to add a timer for a prefix. "
758                     "remove the prefix", __FUNCTION__);
759                 delete_prefix(prefix);
760         }
761         timo.tv_sec = prefix_timo;
762         timo.tv_usec = 0;
763         rtadvd_set_timer(&timo, prefix->timer);
764 }
765
766 static struct rtadvd_timer *
767 prefix_timeout(void *arg)
768 {
769         struct prefix *prefix = (struct prefix *)arg;
770         
771         delete_prefix(prefix);
772
773         return(NULL);
774 }
775
776 void
777 update_prefix(struct prefix * prefix)
778 {
779         u_char ntopbuf[INET6_ADDRSTRLEN];
780         struct rainfo *rai = prefix->rainfo;
781
782         if (prefix->timer == NULL) { /* sanity check */
783                 syslog(LOG_ERR,
784                     "<%s> assumption failure: timer does not exist",
785                     __FUNCTION__);
786                 exit(1);
787         }
788
789         syslog(LOG_DEBUG, "<%s> prefix %s/%d was re-enabled on %s",
790             __FUNCTION__, inet_ntop(AF_INET6, &prefix->prefix, ntopbuf,
791             INET6_ADDRSTRLEN), prefix->prefixlen, rai->ifname);
792
793         /* stop the expiration timer */
794         rtadvd_remove_timer(&prefix->timer);
795 }
796
797 /*
798  * Try to get an in6_prefixreq contents for a prefix which matches
799  * ipr->ipr_prefix and ipr->ipr_plen and belongs to
800  * the interface whose name is ipr->ipr_name[].
801  */
802 static int
803 init_prefix(struct in6_prefixreq *ipr)
804 {
805 #if 0
806         int s;
807
808         if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
809                 syslog(LOG_ERR, "<%s> socket: %s", __FUNCTION__,
810                        strerror(errno));
811                 exit(1);
812         }
813
814         if (ioctl(s, SIOCGIFPREFIX_IN6, (caddr_t)ipr) < 0) {
815                 syslog(LOG_INFO, "<%s> ioctl:SIOCGIFPREFIX %s", __FUNCTION__,
816                        strerror(errno));
817
818                 ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
819                 ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
820                 ipr->ipr_raf_onlink = 1;
821                 ipr->ipr_raf_auto = 1;
822                 /* omit other field initialization */
823         }
824         else if (ipr->ipr_origin < PR_ORIG_RR) {
825                 u_char ntopbuf[INET6_ADDRSTRLEN];
826
827                 syslog(LOG_WARNING, "<%s> Added prefix(%s)'s origin %d is"
828                        "lower than PR_ORIG_RR(router renumbering)."
829                        "This should not happen if I am router", __FUNCTION__,
830                        inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf,
831                                  sizeof(ntopbuf)), ipr->ipr_origin);
832                 close(s);
833                 return 1;
834         }
835
836         close(s);
837         return 0;
838 #else
839         ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
840         ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
841         ipr->ipr_raf_onlink = 1;
842         ipr->ipr_raf_auto = 1;
843         return 0;
844 #endif
845 }
846
847 void
848 make_prefix(struct rainfo *rai, int ifindex, struct in6_addr *addr, int plen)
849 {
850         struct in6_prefixreq ipr;
851
852         memset(&ipr, 0, sizeof(ipr));
853         if (if_indextoname(ifindex, ipr.ipr_name) == NULL) {
854                 syslog(LOG_ERR, "<%s> Prefix added interface No.%d doesn't"
855                        "exist. This should not happen! %s", __FUNCTION__,
856                        ifindex, strerror(errno));
857                 exit(1);
858         }
859         ipr.ipr_prefix.sin6_len = sizeof(ipr.ipr_prefix);
860         ipr.ipr_prefix.sin6_family = AF_INET6;
861         ipr.ipr_prefix.sin6_addr = *addr;
862         ipr.ipr_plen = plen;
863
864         if (init_prefix(&ipr))
865                 return; /* init failed by some error */
866         add_prefix(rai, &ipr);
867 }
868
869 void
870 make_packet(struct rainfo *rainfo)
871 {
872         size_t packlen, lladdroptlen = 0;
873         char *buf;
874         struct nd_router_advert *ra;
875         struct nd_opt_prefix_info *ndopt_pi;
876         struct nd_opt_mtu *ndopt_mtu;
877 #ifdef MIP6
878         struct nd_opt_advinterval *ndopt_advint;
879         struct nd_opt_homeagent_info *ndopt_hai;
880 #endif
881         struct nd_opt_route_info *ndopt_rti;
882         struct prefix *pfx;
883         struct rtinfo *rti;
884
885         /* calculate total length */
886         packlen = sizeof(struct nd_router_advert);
887         if (rainfo->advlinkopt) {
888                 if ((lladdroptlen = lladdropt_length(rainfo->sdl)) == 0) {
889                         syslog(LOG_INFO,
890                                "<%s> link-layer address option has"
891                                " null length on %s."
892                                " Treat as not included.",
893                                __FUNCTION__, rainfo->ifname);
894                         rainfo->advlinkopt = 0;
895                 }
896                 packlen += lladdroptlen;
897         }
898         if (rainfo->pfxs)
899                 packlen += sizeof(struct nd_opt_prefix_info) * rainfo->pfxs;
900         if (rainfo->linkmtu)
901                 packlen += sizeof(struct nd_opt_mtu);
902 #ifdef MIP6
903         if (mobileip6 && rainfo->maxinterval)
904                 packlen += sizeof(struct nd_opt_advinterval);
905         if (mobileip6 && rainfo->hatime)
906                 packlen += sizeof(struct nd_opt_homeagent_info);
907 #endif
908 #ifdef ND_OPT_ROUTE_INFO
909         for (rti = rainfo->route.next; rti != &rainfo->route; rti = rti->next)
910                 packlen += sizeof(struct nd_opt_route_info) + 
911                            ((rti->prefixlen + 0x3f) >> 6) * 8;
912 #endif
913
914         /* allocate memory for the packet */
915         if ((buf = malloc(packlen)) == NULL) {
916                 syslog(LOG_ERR,
917                        "<%s> can't get enough memory for an RA packet",
918                        __FUNCTION__);
919                 exit(1);
920         }
921         if (rainfo->ra_data) {
922                 /* free the previous packet */
923                 free(rainfo->ra_data);
924                 rainfo->ra_data = NULL;
925         }
926         rainfo->ra_data = buf;
927         /* XXX: what if packlen > 576? */
928         rainfo->ra_datalen = packlen;
929
930         /*
931          * construct the packet
932          */
933         ra = (struct nd_router_advert *)buf;
934         ra->nd_ra_type = ND_ROUTER_ADVERT;
935         ra->nd_ra_code = 0;
936         ra->nd_ra_cksum = 0;
937         ra->nd_ra_curhoplimit = (u_int8_t)(0xff & rainfo->hoplimit);
938         ra->nd_ra_flags_reserved = 0; /* just in case */
939         /*
940          * XXX: the router preference field, which is a 2-bit field, should be
941          * initialized before other fields.
942          */
943         ra->nd_ra_flags_reserved = 0xff & rainfo->rtpref;
944         ra->nd_ra_flags_reserved |=
945                 rainfo->managedflg ? ND_RA_FLAG_MANAGED : 0;
946         ra->nd_ra_flags_reserved |=
947                 rainfo->otherflg ? ND_RA_FLAG_OTHER : 0;
948 #ifdef MIP6
949         ra->nd_ra_flags_reserved |=
950                 rainfo->haflg ? ND_RA_FLAG_HA : 0;
951 #endif
952         ra->nd_ra_router_lifetime = htons(rainfo->lifetime);
953         ra->nd_ra_reachable = htonl(rainfo->reachabletime);
954         ra->nd_ra_retransmit = htonl(rainfo->retranstimer);
955         buf += sizeof(*ra);
956
957         if (rainfo->advlinkopt) {
958                 lladdropt_fill(rainfo->sdl, (struct nd_opt_hdr *)buf);
959                 buf += lladdroptlen;
960         }
961
962         if (rainfo->linkmtu) {
963                 ndopt_mtu = (struct nd_opt_mtu *)buf;
964                 ndopt_mtu->nd_opt_mtu_type = ND_OPT_MTU;
965                 ndopt_mtu->nd_opt_mtu_len = 1;
966                 ndopt_mtu->nd_opt_mtu_reserved = 0;
967                 ndopt_mtu->nd_opt_mtu_mtu = htonl(rainfo->linkmtu);
968                 buf += sizeof(struct nd_opt_mtu);
969         }
970
971 #ifdef MIP6
972         if (mobileip6 && rainfo->maxinterval) {
973                 ndopt_advint = (struct nd_opt_advinterval *)buf;
974                 ndopt_advint->nd_opt_adv_type = ND_OPT_ADVINTERVAL;
975                 ndopt_advint->nd_opt_adv_len = 1;
976                 ndopt_advint->nd_opt_adv_reserved = 0;
977                 ndopt_advint->nd_opt_adv_interval = htonl(rainfo->maxinterval *
978                                                           1000);
979                 buf += sizeof(struct nd_opt_advinterval);
980         }
981 #endif
982         
983 #ifdef MIP6
984         if (rainfo->hatime) {
985                 ndopt_hai = (struct nd_opt_homeagent_info *)buf;
986                 ndopt_hai->nd_opt_hai_type = ND_OPT_HOMEAGENT_INFO;
987                 ndopt_hai->nd_opt_hai_len = 1;
988                 ndopt_hai->nd_opt_hai_reserved = 0;
989                 ndopt_hai->nd_opt_hai_preference = htons(rainfo->hapref);
990                 ndopt_hai->nd_opt_hai_lifetime = htons(rainfo->hatime);
991                 buf += sizeof(struct nd_opt_homeagent_info);
992         }
993 #endif
994         
995         for (pfx = rainfo->prefix.next;
996              pfx != &rainfo->prefix; pfx = pfx->next) {
997                 u_int32_t vltime, pltime;
998                 struct timeval now;
999
1000                 ndopt_pi = (struct nd_opt_prefix_info *)buf;
1001                 ndopt_pi->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
1002                 ndopt_pi->nd_opt_pi_len = 4;
1003                 ndopt_pi->nd_opt_pi_prefix_len = pfx->prefixlen;
1004                 ndopt_pi->nd_opt_pi_flags_reserved = 0;
1005                 if (pfx->onlinkflg)
1006                         ndopt_pi->nd_opt_pi_flags_reserved |=
1007                                 ND_OPT_PI_FLAG_ONLINK;
1008                 if (pfx->autoconfflg)
1009                         ndopt_pi->nd_opt_pi_flags_reserved |=
1010                                 ND_OPT_PI_FLAG_AUTO;
1011 #ifdef MIP6
1012                 if (pfx->routeraddr)
1013                         ndopt_pi->nd_opt_pi_flags_reserved |=
1014                                 ND_OPT_PI_FLAG_ROUTER;
1015 #endif
1016                 if (pfx->timer)
1017                         vltime = 0;
1018                 else {
1019                         if (pfx->vltimeexpire || pfx->pltimeexpire)
1020                                 gettimeofday(&now, NULL);
1021                         if (pfx->vltimeexpire == 0)
1022                                 vltime = pfx->validlifetime;
1023                         else
1024                                 vltime = (pfx->vltimeexpire > now.tv_sec) ?
1025                                     pfx->vltimeexpire - now.tv_sec : 0;
1026                 }
1027                 if (pfx->timer)
1028                         pltime = 0;
1029                 else {
1030                         if (pfx->pltimeexpire == 0)
1031                                 pltime = pfx->preflifetime;
1032                         else
1033                                 pltime = (pfx->pltimeexpire > now.tv_sec) ? 
1034                                     pfx->pltimeexpire - now.tv_sec : 0;
1035                 }
1036                 if (vltime < pltime) {
1037                         /*
1038                          * this can happen if vltime is decrement but pltime
1039                          * is not.
1040                          */
1041                         pltime = vltime;
1042                 }
1043                 ndopt_pi->nd_opt_pi_valid_time = htonl(vltime);
1044                 ndopt_pi->nd_opt_pi_preferred_time = htonl(pltime);
1045                 ndopt_pi->nd_opt_pi_reserved2 = 0;
1046                 ndopt_pi->nd_opt_pi_prefix = pfx->prefix;
1047
1048                 buf += sizeof(struct nd_opt_prefix_info);
1049         }
1050
1051 #ifdef ND_OPT_ROUTE_INFO
1052         for (rti = rainfo->route.next; rti != &rainfo->route; rti = rti->next) {
1053                 u_int8_t psize = (rti->prefixlen + 0x3f) >> 6;
1054
1055                 ndopt_rti = (struct nd_opt_route_info *)buf;
1056                 ndopt_rti->nd_opt_rti_type = ND_OPT_ROUTE_INFO;
1057                 ndopt_rti->nd_opt_rti_len = 1 + psize;
1058                 ndopt_rti->nd_opt_rti_prefixlen = rti->prefixlen;
1059                 ndopt_rti->nd_opt_rti_flags = 0xff & rti->rtpref;
1060                 ndopt_rti->nd_opt_rti_lifetime = htonl(rti->ltime);
1061                 memcpy(ndopt_rti + 1, &rti->prefix, psize * 8);
1062                 buf += sizeof(struct nd_opt_route_info) + psize * 8;
1063         }
1064 #endif
1065
1066         return;
1067 }
1068
1069 static int
1070 getinet6sysctl(int code)
1071 {
1072         int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 };
1073         int value;
1074         size_t size;
1075
1076         mib[3] = code;
1077         size = sizeof(value);
1078         if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), &value, &size, NULL, 0)
1079             < 0) {
1080                 syslog(LOG_ERR, "<%s>: failed to get ip6 sysctl(%d): %s",
1081                        __FUNCTION__, code,
1082                        strerror(errno));
1083                 return(-1);
1084         }
1085         else
1086                 return(value);
1087 }