Home | History | Annotate | Line # | Download | only in route6d
route6d.c revision 1.54
      1 /*	$NetBSD: route6d.c,v 1.54 2006/05/25 02:01:40 christos Exp $	*/
      2 /*	$KAME: route6d.c,v 1.94 2002/10/26 20:08:55 itojun Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the project nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 #ifndef	lint
     35 __RCSID("$NetBSD: route6d.c,v 1.54 2006/05/25 02:01:40 christos Exp $");
     36 #endif
     37 
     38 #include <stdio.h>
     39 
     40 #include <time.h>
     41 #include <unistd.h>
     42 #include <stdlib.h>
     43 #include <string.h>
     44 #include <signal.h>
     45 #include <stdarg.h>
     46 #include <syslog.h>
     47 #include <stddef.h>
     48 #include <errno.h>
     49 #include <err.h>
     50 #include <util.h>
     51 #include <poll.h>
     52 
     53 #include <sys/types.h>
     54 #include <sys/param.h>
     55 #include <sys/file.h>
     56 #include <sys/socket.h>
     57 #include <sys/ioctl.h>
     58 #include <sys/sysctl.h>
     59 #include <sys/uio.h>
     60 #include <net/if.h>
     61 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
     62 #include <net/if_var.h>
     63 #endif /* __FreeBSD__ >= 3 */
     64 #define	KERNEL	1
     65 #define	_KERNEL	1
     66 #include <net/route.h>
     67 #undef KERNEL
     68 #undef _KERNEL
     69 #include <netinet/in.h>
     70 #include <netinet/in_var.h>
     71 #include <netinet/ip6.h>
     72 #include <netinet/udp.h>
     73 #include <netdb.h>
     74 #include <ifaddrs.h>
     75 
     76 #include <arpa/inet.h>
     77 
     78 #include "route6d.h"
     79 
     80 #define	MAXFILTER	40
     81 
     82 #ifdef	DEBUG
     83 #define	INIT_INTERVAL6	6
     84 #else
     85 #define	INIT_INTERVAL6	10	/* Wait to submit a initial riprequest */
     86 #endif
     87 
     88 /* alignment constraint for routing socket */
     89 #define ROUNDUP(a) \
     90 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
     91 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
     92 
     93 /*
     94  * Following two macros are highly depending on KAME Release
     95  */
     96 #define	IN6_LINKLOCAL_IFINDEX(addr) \
     97 	((addr).s6_addr[2] << 8 | (addr).s6_addr[3])
     98 
     99 #define	SET_IN6_LINKLOCAL_IFINDEX(addr, index) \
    100 	do { \
    101 		(addr).s6_addr[2] = ((index) >> 8) & 0xff; \
    102 		(addr).s6_addr[3] = (index) & 0xff; \
    103 	} while (0)
    104 
    105 struct	ifc {			/* Configuration of an interface */
    106 	char	*ifc_name;			/* if name */
    107 	struct	ifc *ifc_next;
    108 	int	ifc_index;			/* if index */
    109 	int	ifc_mtu;			/* if mtu */
    110 	int	ifc_metric;			/* if metric */
    111 	u_int	ifc_flags;			/* flags */
    112 	short	ifc_cflags;			/* IFC_XXX */
    113 	struct	in6_addr ifc_mylladdr;		/* my link-local address */
    114 	struct	sockaddr_in6 ifc_ripsin;	/* rip multicast address */
    115 	struct	iff *ifc_filter;		/* filter structure */
    116 	struct	ifac *ifc_addr;			/* list of AF_INET6 addresses */
    117 	int	ifc_joined;			/* joined to ff02::9 */
    118 };
    119 
    120 struct	ifac {			/* Address associated to an interface */
    121 	struct	ifc *ifa_conf;		/* back pointer */
    122 	struct	ifac *ifa_next;
    123 	struct	in6_addr ifa_addr;	/* address */
    124 	struct	in6_addr ifa_raddr;	/* remote address, valid in p2p */
    125 	int	ifa_plen;		/* prefix length */
    126 };
    127 
    128 struct	iff {
    129 	int	iff_type;
    130 	struct	in6_addr iff_addr;
    131 	int	iff_plen;
    132 	struct	iff *iff_next;
    133 };
    134 
    135 struct	ifc *ifc;
    136 int	nifc;		/* number of valid ifc's */
    137 struct	ifc **index2ifc;
    138 int	nindex2ifc;
    139 struct	ifc *loopifcp = NULL;	/* pointing to loopback */
    140 struct	pollfd set[2];
    141 int	rtsock;		/* the routing socket */
    142 int	ripsock;	/* socket to send/receive RIP datagram */
    143 
    144 struct	rip6 *ripbuf;	/* packet buffer for sending */
    145 
    146 /*
    147  * Maintain the routes in a linked list.  When the number of the routes
    148  * grows, somebody would like to introduce a hash based or a radix tree
    149  * based structure.  I believe the number of routes handled by RIP is
    150  * limited and I don't have to manage a complex data structure, however.
    151  *
    152  * One of the major drawbacks of the linear linked list is the difficulty
    153  * of representing the relationship between a couple of routes.  This may
    154  * be a significant problem when we have to support route aggregation with
    155  * supressing the specifices covered by the aggregate.
    156  */
    157 
    158 struct	riprt {
    159 	struct	riprt *rrt_next;	/* next destination */
    160 	struct	riprt *rrt_same;	/* same destination - future use */
    161 	struct	netinfo6 rrt_info;	/* network info */
    162 	struct	in6_addr rrt_gw;	/* gateway */
    163 	u_long	rrt_flags;		/* kernel routing table flags */
    164 	u_long	rrt_rflags;		/* route6d routing table flags */
    165 	time_t	rrt_t;			/* when the route validated */
    166 	int	rrt_index;		/* ifindex from which this route got */
    167 };
    168 
    169 struct	riprt *riprt = 0;
    170 
    171 int	dflag = 0;	/* debug flag */
    172 int	qflag = 0;	/* quiet flag */
    173 int	nflag = 0;	/* don't update kernel routing table */
    174 int	aflag = 0;	/* age out even the statically defined routes */
    175 int	hflag = 0;	/* don't split horizon */
    176 int	lflag = 0;	/* exchange site local routes */
    177 int	sflag = 0;	/* announce static routes w/ split horizon */
    178 int	Sflag = 0;	/* announce static routes to every interface */
    179 unsigned long routetag = 0;	/* route tag attached on originating case */
    180 
    181 char	*filter[MAXFILTER];
    182 int	filtertype[MAXFILTER];
    183 int	nfilter = 0;
    184 
    185 pid_t	pid;
    186 
    187 struct	sockaddr_storage ripsin;
    188 
    189 struct	rtentry rtentry;
    190 
    191 int	interval = 1;
    192 time_t	nextalarm = 0;
    193 time_t	sup_trig_update = 0;
    194 
    195 FILE	*rtlog = NULL;
    196 
    197 int logopened = 0;
    198 
    199 static	int	seq = 0;
    200 
    201 volatile sig_atomic_t seenalrm;
    202 volatile sig_atomic_t seenquit;
    203 volatile sig_atomic_t seenusr1;
    204 
    205 #define	RRTF_AGGREGATE		0x08000000
    206 #define	RRTF_NOADVERTISE	0x10000000
    207 #define	RRTF_NH_NOT_LLADDR	0x20000000
    208 #define RRTF_SENDANYWAY		0x40000000
    209 #define	RRTF_CHANGED		0x80000000
    210 
    211 int main __P((int, char **));
    212 void sighandler __P((int));
    213 void ripalarm __P((void));
    214 void riprecv __P((void));
    215 void ripsend __P((struct ifc *, struct sockaddr_in6 *, int));
    216 int out_filter __P((struct riprt *, struct ifc *));
    217 void init __P((void));
    218 void sockopt __P((struct ifc *));
    219 void ifconfig __P((void));
    220 void ifconfig1 __P((const char *, const struct sockaddr *, struct ifc *, int));
    221 void rtrecv __P((void));
    222 int rt_del __P((const struct sockaddr_in6 *, const struct sockaddr_in6 *,
    223 	const struct sockaddr_in6 *));
    224 int rt_deladdr __P((struct ifc *, const struct sockaddr_in6 *,
    225 	const struct sockaddr_in6 *));
    226 void filterconfig __P((void));
    227 int getifmtu __P((int));
    228 const char *rttypes __P((struct rt_msghdr *));
    229 const char *rtflags __P((struct rt_msghdr *));
    230 const char *ifflags __P((int));
    231 int ifrt __P((struct ifc *, int));
    232 void ifrt_p2p __P((struct ifc *, int));
    233 void applymask __P((struct in6_addr *, struct in6_addr *));
    234 void applyplen __P((struct in6_addr *, int));
    235 void ifrtdump __P((int));
    236 void ifdump __P((int));
    237 void ifdump0 __P((FILE *, const struct ifc *));
    238 void rtdump __P((int));
    239 void rt_entry __P((struct rt_msghdr *, int));
    240 void rtdexit __P((void));
    241 void riprequest __P((struct ifc *, struct netinfo6 *, int,
    242 	struct sockaddr_in6 *));
    243 void ripflush __P((struct ifc *, struct sockaddr_in6 *));
    244 void sendrequest __P((struct ifc *));
    245 int sin6mask2len __P((const struct sockaddr_in6 *));
    246 int mask2len __P((const struct in6_addr *, int));
    247 int sendpacket __P((struct sockaddr_in6 *, int));
    248 int addroute __P((struct riprt *, const struct in6_addr *, struct ifc *));
    249 int delroute __P((struct netinfo6 *, struct in6_addr *));
    250 struct in6_addr *getroute __P((struct netinfo6 *, struct in6_addr *));
    251 void krtread __P((int));
    252 int tobeadv __P((struct riprt *, struct ifc *));
    253 char *allocopy __P((char *));
    254 char *hms __P((void));
    255 const char *inet6_n2p __P((const struct in6_addr *));
    256 struct ifac *ifa_match __P((const struct ifc *, const struct in6_addr *, int));
    257 struct in6_addr *plen2mask __P((int));
    258 struct riprt *rtsearch __P((struct netinfo6 *, struct riprt **));
    259 int ripinterval __P((int));
    260 time_t ripsuptrig __P((void));
    261 void fatal __P((const char *, ...))
    262 	__attribute__((__format__(__printf__, 1, 2)));
    263 void trace __P((int, const char *, ...))
    264 	__attribute__((__format__(__printf__, 2, 3)));
    265 void tracet __P((int, const char *, ...))
    266 	__attribute__((__format__(__printf__, 2, 3)));
    267 unsigned int if_maxindex __P((void));
    268 struct ifc *ifc_find __P((char *));
    269 struct iff *iff_find __P((struct ifc *, int));
    270 void setindex2ifc __P((int, struct ifc *));
    271 
    272 #define	MALLOC(type)	((type *)malloc(sizeof(type)))
    273 
    274 int
    275 main(int argc, char **argv)
    276 {
    277 	int	ch;
    278 	int	error = 0;
    279 	struct	ifc *ifcp;
    280 	sigset_t mask, omask;
    281 	char *progname;
    282 	char *ep;
    283 
    284 	progname = strrchr(*argv, '/');
    285 	if (progname)
    286 		progname++;
    287 	else
    288 		progname = *argv;
    289 
    290 	pid = getpid();
    291 	while ((ch = getopt(argc, argv, "A:N:O:R:T:L:t:adDhlnqsS")) != -1) {
    292 		switch (ch) {
    293 		case 'A':
    294 		case 'N':
    295 		case 'O':
    296 		case 'T':
    297 		case 'L':
    298 			if (nfilter >= MAXFILTER) {
    299 				fatal("Exceeds MAXFILTER");
    300 				/*NOTREACHED*/
    301 			}
    302 			filtertype[nfilter] = ch;
    303 			filter[nfilter++] = allocopy(optarg);
    304 			break;
    305 		case 't':
    306 			ep = NULL;
    307 			routetag = strtoul(optarg, &ep, 0);
    308 			if (!ep || *ep != '\0' || (routetag & ~0xffff) != 0) {
    309 				fatal("invalid route tag");
    310 				/*NOTREACHED*/
    311 			}
    312 			break;
    313 		case 'R':
    314 			if ((rtlog = fopen(optarg, "w")) == NULL) {
    315 				fatal("Can not write to routelog");
    316 				/*NOTREACHED*/
    317 			}
    318 			break;
    319 #define	FLAG(c, flag, n)	case c: do { flag = n; break; } while(0)
    320 		FLAG('a', aflag, 1); break;
    321 		FLAG('d', dflag, 1); break;
    322 		FLAG('D', dflag, 2); break;
    323 		FLAG('h', hflag, 1); break;
    324 		FLAG('l', lflag, 1); break;
    325 		FLAG('n', nflag, 1); break;
    326 		FLAG('q', qflag, 1); break;
    327 		FLAG('s', sflag, 1); break;
    328 		FLAG('S', Sflag, 1); break;
    329 #undef	FLAG
    330 		default:
    331 			fatal("Invalid option specified, terminating");
    332 			/*NOTREACHED*/
    333 		}
    334 	}
    335 	argc -= optind;
    336 	argv += optind;
    337 	if (argc > 0) {
    338 		fatal("bogus extra arguments");
    339 		/*NOTREACHED*/
    340 	}
    341 
    342 	if (geteuid()) {
    343 		nflag = 1;
    344 		fprintf(stderr, "No kernel update is allowed\n");
    345 	}
    346 
    347 	if (dflag == 0) {
    348 		if (daemon(0, 0) < 0) {
    349 			fatal("daemon");
    350 			/*NOTREACHED*/
    351 		}
    352 	}
    353 
    354 	openlog(progname, LOG_NDELAY|LOG_PID, LOG_DAEMON);
    355 	logopened++;
    356 
    357 	if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL)
    358 		fatal("malloc");
    359 	memset(ripbuf, 0, RIP6_MAXMTU);
    360 	ripbuf->rip6_cmd = RIP6_RESPONSE;
    361 	ripbuf->rip6_vers = RIP6_VERSION;
    362 	ripbuf->rip6_res1[0] = 0;
    363 	ripbuf->rip6_res1[1] = 0;
    364 
    365 	init();
    366 	ifconfig();
    367 	for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
    368 		if (ifcp->ifc_index < 0) {
    369 			fprintf(stderr,
    370 "No ifindex found at %s (no link-local address?)\n",
    371 				ifcp->ifc_name);
    372 			error++;
    373 		}
    374 	}
    375 	if (error)
    376 		exit(1);
    377 	if (loopifcp == NULL) {
    378 		fatal("No loopback found");
    379 		/*NOTREACHED*/
    380 	}
    381 	for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next)
    382 		ifrt(ifcp, 0);
    383 	filterconfig();
    384 	krtread(0);
    385 	if (dflag)
    386 		ifrtdump(0);
    387 
    388 	pidfile(NULL);
    389 
    390 	if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL) {
    391 		fatal("malloc");
    392 		/*NOTREACHED*/
    393 	}
    394 	memset(ripbuf, 0, RIP6_MAXMTU);
    395 	ripbuf->rip6_cmd = RIP6_RESPONSE;
    396 	ripbuf->rip6_vers = RIP6_VERSION;
    397 	ripbuf->rip6_res1[0] = 0;
    398 	ripbuf->rip6_res1[1] = 0;
    399 
    400 	if (signal(SIGALRM, sighandler) == SIG_ERR ||
    401 	    signal(SIGQUIT, sighandler) == SIG_ERR ||
    402 	    signal(SIGTERM, sighandler) == SIG_ERR ||
    403 	    signal(SIGUSR1, sighandler) == SIG_ERR ||
    404 	    signal(SIGHUP, sighandler) == SIG_ERR ||
    405 	    signal(SIGINT, sighandler) == SIG_ERR) {
    406 		fatal("signal");
    407 		/*NOTREACHED*/
    408 	}
    409 	/*
    410 	 * To avoid rip packet congestion (not on a cable but in this
    411 	 * process), wait for a moment to send the first RIP6_RESPONSE
    412 	 * packets.
    413 	 */
    414 	alarm(ripinterval(INIT_INTERVAL6));
    415 
    416 	for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
    417 		if (iff_find(ifcp, 'N'))
    418 			continue;
    419 		if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
    420 			sendrequest(ifcp);
    421 	}
    422 
    423 	syslog(LOG_INFO, "**** Started ****");
    424 	sigemptyset(&mask);
    425 	sigaddset(&mask, SIGALRM);
    426 	while (1) {
    427 		if (seenalrm) {
    428 			ripalarm();
    429 			seenalrm = 0;
    430 			continue;
    431 		}
    432 		if (seenquit) {
    433 			rtdexit();
    434 			seenquit = 0;
    435 			continue;
    436 		}
    437 		if (seenusr1) {
    438 			ifrtdump(SIGUSR1);
    439 			seenusr1 = 0;
    440 			continue;
    441 		}
    442 
    443 		switch (poll(set, 2, INFTIM))
    444 		{
    445 		case -1:
    446 			if (errno != EINTR) {
    447 				fatal("select");
    448 				/*NOTREACHED*/
    449 			}
    450 			continue;
    451 		case 0:
    452 			continue;
    453 		default:
    454 			if (set[0].revents & POLLIN)
    455 			{
    456 				sigprocmask(SIG_BLOCK, &mask, &omask);
    457 				riprecv();
    458 				sigprocmask(SIG_SETMASK, &omask, NULL);
    459 			}
    460 			if (set[1].revents & POLLIN)
    461 			{
    462 				sigprocmask(SIG_BLOCK, &mask, &omask);
    463 				rtrecv();
    464 				sigprocmask(SIG_SETMASK, &omask, NULL);
    465 			}
    466 		}
    467 	}
    468 }
    469 
    470 void
    471 sighandler(int signo)
    472 {
    473 
    474 	switch (signo) {
    475 	case SIGALRM:
    476 		seenalrm++;
    477 		break;
    478 	case SIGQUIT:
    479 	case SIGTERM:
    480 		seenquit++;
    481 		break;
    482 	case SIGUSR1:
    483 	case SIGHUP:
    484 	case SIGINT:
    485 		seenusr1++;
    486 		break;
    487 	}
    488 }
    489 
    490 /*
    491  * gracefully exits after resetting sockopts.
    492  */
    493 /* ARGSUSED */
    494 void
    495 rtdexit(void)
    496 {
    497 	struct	riprt *rrt;
    498 
    499 	alarm(0);
    500 	for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
    501 		if (rrt->rrt_rflags & RRTF_AGGREGATE) {
    502 			delroute(&rrt->rrt_info, &rrt->rrt_gw);
    503 		}
    504 	}
    505 	close(ripsock);
    506 	close(rtsock);
    507 	syslog(LOG_INFO, "**** Terminated ****");
    508 	closelog();
    509 	exit(1);
    510 }
    511 
    512 /*
    513  * Called periodically:
    514  *	1. age out the learned route. remove it if necessary.
    515  *	2. submit RIP6_RESPONSE packets.
    516  * Invoked in every SUPPLY_INTERVAL6 (30) seconds.  I believe we don't have
    517  * to invoke this function in every 1 or 5 or 10 seconds only to age the
    518  * routes more precisely.
    519  */
    520 /* ARGSUSED */
    521 void
    522 ripalarm(void)
    523 {
    524 	struct	ifc *ifcp;
    525 	struct	riprt *rrt, *rrt_prev, *rrt_next;
    526 	time_t	t_lifetime, t_holddown;
    527 
    528 	/* age the RIP routes */
    529 	rrt_prev = 0;
    530 	t_lifetime = time(NULL) - RIP_LIFETIME;
    531 	t_holddown = t_lifetime - RIP_HOLDDOWN;
    532 	for (rrt = riprt; rrt; rrt = rrt_next) {
    533 		rrt_next = rrt->rrt_next;
    534 
    535 		if (rrt->rrt_t == 0) {
    536 			rrt_prev = rrt;
    537 			continue;
    538 		}
    539 		if (rrt->rrt_t < t_holddown) {
    540 			if (rrt_prev) {
    541 				rrt_prev->rrt_next = rrt->rrt_next;
    542 			} else {
    543 				riprt = rrt->rrt_next;
    544 			}
    545 			delroute(&rrt->rrt_info, &rrt->rrt_gw);
    546 			free(rrt);
    547 			continue;
    548 		}
    549 		if (rrt->rrt_t < t_lifetime)
    550 			rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
    551 		rrt_prev = rrt;
    552 	}
    553 	/* Supply updates */
    554 	for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
    555 		if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
    556 			ripsend(ifcp, &ifcp->ifc_ripsin, 0);
    557 	}
    558 	alarm(ripinterval(SUPPLY_INTERVAL6));
    559 }
    560 
    561 void
    562 init(void)
    563 {
    564 	int	i, error;
    565 	const int int0 = 0, int1 = 1, int255 = 255;
    566 	struct	addrinfo hints, *res;
    567 	char	port[NI_MAXSERV];
    568 
    569 	ifc = (struct ifc *)NULL;
    570 	nifc = 0;
    571 	nindex2ifc = 0;	/*initial guess*/
    572 	index2ifc = NULL;
    573 	snprintf(port, sizeof(port), "%u", RIP6_PORT);
    574 
    575 	memset(&hints, 0, sizeof(hints));
    576 	hints.ai_family = PF_INET6;
    577 	hints.ai_socktype = SOCK_DGRAM;
    578 	hints.ai_flags = AI_PASSIVE;
    579 	error = getaddrinfo(NULL, port, &hints, &res);
    580 	if (error) {
    581 		fatal("%s", gai_strerror(error));
    582 		/*NOTREACHED*/
    583 	}
    584 	if (res->ai_next) {
    585 		fatal(":: resolved to multiple address");
    586 		/*NOTREACHED*/
    587 	}
    588 
    589 	ripsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    590 	if (ripsock < 0) {
    591 		fatal("rip socket");
    592 		/*NOTREACHED*/
    593 	}
    594 	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_V6ONLY,
    595 	    &int1, sizeof(int1)) < 0) {
    596 		fatal("rip IPV6_V6ONLY");
    597 		/*NOTREACHED*/
    598 	}
    599 	if (bind(ripsock, res->ai_addr, res->ai_addrlen) < 0) {
    600 		fatal("rip bind");
    601 		/*NOTREACHED*/
    602 	}
    603 	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
    604 	    &int255, sizeof(int255)) < 0) {
    605 		fatal("rip IPV6_MULTICAST_HOPS");
    606 		/*NOTREACHED*/
    607 	}
    608 	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
    609 	    &int0, sizeof(int0)) < 0) {
    610 		fatal("rip IPV6_MULTICAST_LOOP");
    611 		/*NOTREACHED*/
    612 	}
    613 
    614 	i = 1;
    615 #ifdef IPV6_RECVPKTINFO
    616 	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &i,
    617 	    sizeof(i)) < 0) {
    618 		fatal("rip IPV6_RECVPKTINFO");
    619 		/*NOTREACHED*/
    620 	}
    621 #else  /* old adv. API */
    622 	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_PKTINFO, &i,
    623 	    sizeof(i)) < 0) {
    624 		fatal("rip IPV6_PKTINFO");
    625 		/*NOTREACHED*/
    626 	}
    627 #endif
    628 
    629 	memset(&hints, 0, sizeof(hints));
    630 	hints.ai_family = PF_INET6;
    631 	hints.ai_socktype = SOCK_DGRAM;
    632 	error = getaddrinfo(RIP6_DEST, port, &hints, &res);
    633 	if (error) {
    634 		fatal("%s", gai_strerror(error));
    635 		/*NOTREACHED*/
    636 	}
    637 	if (res->ai_next) {
    638 		fatal("%s resolved to multiple address", RIP6_DEST);
    639 		/*NOTREACHED*/
    640 	}
    641 	memcpy(&ripsin, res->ai_addr, res->ai_addrlen);
    642 
    643 	set[0].fd = ripsock;
    644 	set[0].events = POLLIN;
    645 
    646 	if (nflag == 0) {
    647 		if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
    648 			fatal("route socket");
    649 			/*NOTREACHED*/
    650 		}
    651 		set[1].fd = rtsock;
    652 		set[1].events = POLLIN;
    653 	} else {
    654 		set[1].fd = -1;
    655 	}
    656 }
    657 
    658 #define	RIPSIZE(n) \
    659 	(sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6))
    660 
    661 /*
    662  * ripflush flushes the rip datagram stored in the rip buffer
    663  */
    664 static int nrt;
    665 static struct netinfo6 *np;
    666 
    667 void
    668 ripflush(struct ifc *ifcp, struct sockaddr_in6 *sin6)
    669 {
    670 	int i;
    671 	int error;
    672 
    673 	if (ifcp)
    674 		tracet(1, "Send(%s): info(%d) to %s.%d\n",
    675 			ifcp->ifc_name, nrt,
    676 			inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
    677 	else
    678 		tracet(1, "Send: info(%d) to %s.%d\n",
    679 			nrt, inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
    680 	if (dflag >= 2) {
    681 		np = ripbuf->rip6_nets;
    682 		for (i = 0; i < nrt; i++, np++) {
    683 			if (np->rip6_metric == NEXTHOP_METRIC) {
    684 				if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest))
    685 					trace(2, "    NextHop reset");
    686 				else {
    687 					trace(2, "    NextHop %s",
    688 						inet6_n2p(&np->rip6_dest));
    689 				}
    690 			} else {
    691 				trace(2, "    %s/%d[%d]",
    692 					inet6_n2p(&np->rip6_dest),
    693 					np->rip6_plen, np->rip6_metric);
    694 			}
    695 			if (np->rip6_tag) {
    696 				trace(2, "  tag=0x%04x",
    697 					ntohs(np->rip6_tag) & 0xffff);
    698 			}
    699 			trace(2, "\n");
    700 		}
    701 	}
    702 	error = sendpacket(sin6, RIPSIZE(nrt));
    703 	if (error == EAFNOSUPPORT && ifcp) {
    704 		/* Protocol not supported */
    705 		tracet(1, "Could not send info to %s (%s): "
    706 			"set IFF_UP to 0\n",
    707 			ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
    708 		ifcp->ifc_flags &= ~IFF_UP;	/* As if down for AF_INET6 */
    709 	}
    710 	nrt = 0; np = ripbuf->rip6_nets;
    711 }
    712 
    713 /*
    714  * Generate RIP6_RESPONSE packets and send them.
    715  */
    716 void
    717 ripsend(struct ifc *ifcp, struct sockaddr_in6 *sin6, int flag)
    718 {
    719 	struct	riprt *rrt;
    720 	struct	in6_addr *nh;	/* next hop */
    721 	int	maxrte;
    722 
    723 	if (qflag)
    724 		return;
    725 
    726 	if (ifcp == NULL) {
    727 		/*
    728 		 * Request from non-link local address is not
    729 		 * a regular route6d update.
    730 		 */
    731 		maxrte = (IFMINMTU - sizeof(struct ip6_hdr) -
    732 				sizeof(struct udphdr) -
    733 				sizeof(struct rip6) + sizeof(struct netinfo6)) /
    734 				sizeof(struct netinfo6);
    735 		nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
    736 		for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
    737 			if (rrt->rrt_rflags & RRTF_NOADVERTISE)
    738 				continue;
    739 			/* Put the route to the buffer */
    740 			*np = rrt->rrt_info;
    741 			np++; nrt++;
    742 			if (nrt == maxrte) {
    743 				ripflush(NULL, sin6);
    744 				nh = NULL;
    745 			}
    746 		}
    747 		if (nrt)	/* Send last packet */
    748 			ripflush(NULL, sin6);
    749 		return;
    750 	}
    751 
    752 	if ((flag & RRTF_SENDANYWAY) == 0 &&
    753 	    (qflag || (ifcp->ifc_flags & IFF_LOOPBACK)))
    754 		return;
    755 
    756 	/* -N: no use */
    757 	if (iff_find(ifcp, 'N') != NULL)
    758 		return;
    759 
    760 	/* -T: generate default route only */
    761 	if (iff_find(ifcp, 'T') != NULL) {
    762 		struct netinfo6 rrt_info;
    763 		memset(&rrt_info, 0, sizeof(struct netinfo6));
    764 		rrt_info.rip6_dest = in6addr_any;
    765 		rrt_info.rip6_plen = 0;
    766 		rrt_info.rip6_metric = 1;
    767 		rrt_info.rip6_metric += ifcp->ifc_metric;
    768 		rrt_info.rip6_tag = htons(routetag & 0xffff);
    769 		np = ripbuf->rip6_nets;
    770 		*np = rrt_info;
    771 		nrt = 1;
    772 		ripflush(ifcp, sin6);
    773 		return;
    774 	}
    775 
    776 	maxrte = (ifcp->ifc_mtu - sizeof(struct ip6_hdr) -
    777 			sizeof(struct udphdr) -
    778 			sizeof(struct rip6) + sizeof(struct netinfo6)) /
    779 			sizeof(struct netinfo6);
    780 
    781 	nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
    782 	for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
    783 		if (rrt->rrt_rflags & RRTF_NOADVERTISE)
    784 			continue;
    785 
    786 		/* Need to check filter here */
    787 		if (out_filter(rrt, ifcp) == 0)
    788 			continue;
    789 
    790 		/* Check split horizon and other conditions */
    791 		if (tobeadv(rrt, ifcp) == 0)
    792 			continue;
    793 
    794 		/* Only considers the routes with flag if specified */
    795 		if ((flag & RRTF_CHANGED) &&
    796 		    (rrt->rrt_rflags & RRTF_CHANGED) == 0)
    797 			continue;
    798 
    799 		/* Check nexthop */
    800 		if (rrt->rrt_index == ifcp->ifc_index &&
    801 		    !IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_gw) &&
    802 		    (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR) == 0) {
    803 			if (nh == NULL || !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw)) {
    804 				if (nrt == maxrte - 2)
    805 					ripflush(ifcp, sin6);
    806 				np->rip6_dest = rrt->rrt_gw;
    807 				if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest))
    808 					SET_IN6_LINKLOCAL_IFINDEX(np->rip6_dest, 0);
    809 				np->rip6_plen = 0;
    810 				np->rip6_tag = 0;
    811 				np->rip6_metric = NEXTHOP_METRIC;
    812 				nh = &rrt->rrt_gw;
    813 				np++; nrt++;
    814 			}
    815 		} else if (nh && (rrt->rrt_index != ifcp->ifc_index ||
    816 			          !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw) ||
    817 				  rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)) {
    818 			/* Reset nexthop */
    819 			if (nrt == maxrte - 2)
    820 				ripflush(ifcp, sin6);
    821 			memset(np, 0, sizeof(struct netinfo6));
    822 			np->rip6_metric = NEXTHOP_METRIC;
    823 			nh = NULL;
    824 			np++; nrt++;
    825 		}
    826 
    827 		/* Put the route to the buffer */
    828 		*np = rrt->rrt_info;
    829 		np++; nrt++;
    830 		if (nrt == maxrte) {
    831 			ripflush(ifcp, sin6);
    832 			nh = NULL;
    833 		}
    834 	}
    835 	if (nrt)	/* Send last packet */
    836 		ripflush(ifcp, sin6);
    837 }
    838 
    839 /*
    840  * outbound filter logic, per-route/interface.
    841  */
    842 int
    843 out_filter(struct riprt *rrt, struct ifc *ifcp)
    844 {
    845 	struct iff *iffp;
    846 	struct in6_addr ia;
    847 	int ok;
    848 
    849 	/*
    850 	 * -A: filter out less specific routes, if we have aggregated
    851 	 * route configured.
    852 	 */
    853 	for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
    854 		if (iffp->iff_type != 'A')
    855 			continue;
    856 		if (rrt->rrt_info.rip6_plen <= iffp->iff_plen)
    857 			continue;
    858 		ia = rrt->rrt_info.rip6_dest;
    859 		applyplen(&ia, iffp->iff_plen);
    860 		if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr))
    861 			return 0;
    862 	}
    863 
    864 	/*
    865 	 * if it is an aggregated route, advertise it only to the
    866 	 * interfaces specified on -A.
    867 	 */
    868 	if ((rrt->rrt_rflags & RRTF_AGGREGATE) != 0) {
    869 		ok = 0;
    870 		for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
    871 			if (iffp->iff_type != 'A')
    872 				continue;
    873 			if (rrt->rrt_info.rip6_plen == iffp->iff_plen &&
    874 			    IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
    875 			    &iffp->iff_addr)) {
    876 				ok = 1;
    877 				break;
    878 			}
    879 		}
    880 		if (!ok)
    881 			return 0;
    882 	}
    883 
    884 	/*
    885 	 * -O: advertise only if prefix matches the configured prefix.
    886 	 */
    887 	if (iff_find(ifcp, 'O')) {
    888 		ok = 0;
    889 		for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
    890 			if (iffp->iff_type != 'O')
    891 				continue;
    892 			if (rrt->rrt_info.rip6_plen < iffp->iff_plen)
    893 				continue;
    894 			ia = rrt->rrt_info.rip6_dest;
    895 			applyplen(&ia, iffp->iff_plen);
    896 			if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
    897 				ok = 1;
    898 				break;
    899 			}
    900 		}
    901 		if (!ok)
    902 			return 0;
    903 	}
    904 
    905 	/* the prefix should be advertised */
    906 	return 1;
    907 }
    908 
    909 /*
    910  * Determine if the route is to be advertised on the specified interface.
    911  * It checks options specified in the arguments and the split horizon rule.
    912  */
    913 int
    914 tobeadv(struct riprt *rrt, struct ifc *ifcp)
    915 {
    916 
    917 	/* Special care for static routes */
    918 	if (rrt->rrt_flags & RTF_STATIC) {
    919 		/* XXX don't advertise reject/blackhole routes */
    920 		if (rrt->rrt_flags & (RTF_REJECT | RTF_BLACKHOLE))
    921 			return 0;
    922 
    923 		if (Sflag)	/* Yes, advertise it anyway */
    924 			return 1;
    925 		if (sflag && rrt->rrt_index != ifcp->ifc_index)
    926 			return 1;
    927 		return 0;
    928 	}
    929 	/* Regular split horizon */
    930 	if (hflag == 0 && rrt->rrt_index == ifcp->ifc_index)
    931 		return 0;
    932 	return 1;
    933 }
    934 
    935 /*
    936  * Send a rip packet actually.
    937  */
    938 int
    939 sendpacket(struct sockaddr_in6 *sin6, int len)
    940 {
    941 	struct msghdr m;
    942 	struct cmsghdr *cm;
    943 	struct iovec iov[2];
    944 	u_char cmsgbuf[256];
    945 	struct in6_pktinfo *pi;
    946 	int idx;
    947 	struct sockaddr_in6 sincopy;
    948 
    949 	/* do not overwrite the given sin */
    950 	sincopy = *sin6;
    951 	sin6 = &sincopy;
    952 
    953 	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
    954 	    IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
    955 		/* XXX: do not mix the interface index and link index */
    956 		idx = IN6_LINKLOCAL_IFINDEX(sin6->sin6_addr);
    957 		SET_IN6_LINKLOCAL_IFINDEX(sin6->sin6_addr, 0);
    958 		sin6->sin6_scope_id = idx;
    959 	} else
    960 		idx = 0;
    961 
    962 	m.msg_name = (caddr_t)sin6;
    963 	m.msg_namelen = sizeof(*sin6);
    964 	iov[0].iov_base = (caddr_t)ripbuf;
    965 	iov[0].iov_len = len;
    966 	m.msg_iov = iov;
    967 	m.msg_iovlen = 1;
    968 	if (!idx) {
    969 		m.msg_control = NULL;
    970 		m.msg_controllen = 0;
    971 	} else {
    972 		memset(cmsgbuf, 0, sizeof(cmsgbuf));
    973 		cm = (struct cmsghdr *)cmsgbuf;
    974 		m.msg_control = (caddr_t)cm;
    975 		m.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
    976 
    977 		cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
    978 		cm->cmsg_level = IPPROTO_IPV6;
    979 		cm->cmsg_type = IPV6_PKTINFO;
    980 		pi = (struct in6_pktinfo *)CMSG_DATA(cm);
    981 		memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*::*/
    982 		pi->ipi6_ifindex = idx;
    983 	}
    984 
    985 	if (sendmsg(ripsock, &m, 0 /*MSG_DONTROUTE*/) < 0) {
    986 		trace(1, "sendmsg: %s\n", strerror(errno));
    987 		return errno;
    988 	}
    989 
    990 	return 0;
    991 }
    992 
    993 /*
    994  * Receive and process RIP packets.  Update the routes/kernel forwarding
    995  * table if necessary.
    996  */
    997 void
    998 riprecv(void)
    999 {
   1000 	struct	ifc *ifcp, *ic;
   1001 	struct	sockaddr_in6 fsock;
   1002 	struct	in6_addr nh;	/* next hop */
   1003 	struct	rip6 *rp;
   1004 	struct	netinfo6 *np, *nq;
   1005 	struct	riprt *rrt;
   1006 	ssize_t	len, nn;
   1007 	unsigned int need_trigger, idx;
   1008 	char	buf[4 * RIP6_MAXMTU];
   1009 	time_t	t;
   1010 	struct msghdr m;
   1011 	struct cmsghdr *cm;
   1012 	struct iovec iov[2];
   1013 	u_char cmsgbuf[256];
   1014 	struct in6_pktinfo *pi;
   1015 	struct iff *iffp;
   1016 	struct in6_addr ia;
   1017 	int ok;
   1018 	time_t t_half_lifetime;
   1019 
   1020 	need_trigger = 0;
   1021 
   1022 	m.msg_name = (caddr_t)&fsock;
   1023 	m.msg_namelen = sizeof(fsock);
   1024 	iov[0].iov_base = (caddr_t)buf;
   1025 	iov[0].iov_len = sizeof(buf);
   1026 	m.msg_iov = iov;
   1027 	m.msg_iovlen = 1;
   1028 	cm = (struct cmsghdr *)cmsgbuf;
   1029 	m.msg_control = (caddr_t)cm;
   1030 	m.msg_controllen = sizeof(cmsgbuf);
   1031 	if ((len = recvmsg(ripsock, &m, 0)) < 0) {
   1032 		fatal("recvmsg");
   1033 		/*NOTREACHED*/
   1034 	}
   1035 	idx = 0;
   1036 	for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&m);
   1037 	     cm;
   1038 	     cm = (struct cmsghdr *)CMSG_NXTHDR(&m, cm)) {
   1039 		if (cm->cmsg_level == IPPROTO_IPV6 &&
   1040 		    cm->cmsg_type == IPV6_PKTINFO) {
   1041 			pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
   1042 			idx = pi->ipi6_ifindex;
   1043 			break;
   1044 		}
   1045 	}
   1046 	if (idx && IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr))
   1047 		SET_IN6_LINKLOCAL_IFINDEX(fsock.sin6_addr, idx);
   1048 
   1049 	if (len < sizeof(struct rip6)) {
   1050 		trace(1, "Packet too short\n");
   1051 		return;
   1052 	}
   1053 
   1054 	nh = fsock.sin6_addr;
   1055 	nn = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
   1056 		sizeof(struct netinfo6);
   1057 	rp = (struct rip6 *)buf;
   1058 	np = rp->rip6_nets;
   1059 
   1060 	if (rp->rip6_vers != RIP6_VERSION) {
   1061 		trace(1, "Incorrect RIP version %d\n", rp->rip6_vers);
   1062 		return;
   1063 	}
   1064 	if (rp->rip6_cmd == RIP6_REQUEST) {
   1065 		if (idx && idx < nindex2ifc) {
   1066 			ifcp = index2ifc[idx];
   1067 			riprequest(ifcp, np, nn, &fsock);
   1068 		} else {
   1069 			riprequest(NULL, np, nn, &fsock);
   1070 		}
   1071 		return;
   1072 	}
   1073 
   1074 	if (!IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr)) {
   1075 		trace(1, "Packets from non-ll addr: %s\n",
   1076 		    inet6_n2p(&fsock.sin6_addr));
   1077 		return;		/* Ignore packets from non-link-local addr */
   1078 	}
   1079 	idx = IN6_LINKLOCAL_IFINDEX(fsock.sin6_addr);
   1080 	ifcp = (idx < nindex2ifc) ? index2ifc[idx] : NULL;
   1081 	if (!ifcp) {
   1082 		trace(1, "Packets to unknown interface index %d\n", idx);
   1083 		return;		/* Ignore it */
   1084 	}
   1085 	if (IN6_ARE_ADDR_EQUAL(&ifcp->ifc_mylladdr, &fsock.sin6_addr))
   1086 		return;		/* The packet is from me; ignore */
   1087 	if (rp->rip6_cmd != RIP6_RESPONSE) {
   1088 		trace(1, "Invalid command %d\n", rp->rip6_cmd);
   1089 		return;
   1090 	}
   1091 
   1092 	/* -N: no use */
   1093 	if (iff_find(ifcp, 'N') != NULL)
   1094 		return;
   1095 
   1096 	tracet(1, "Recv(%s): from %s.%d info(%d)\n",
   1097 	    ifcp->ifc_name, inet6_n2p(&nh), ntohs(fsock.sin6_port), (int)nn);
   1098 
   1099 	t = time(NULL);
   1100 	t_half_lifetime = t - (RIP_LIFETIME/2);
   1101 	for (; nn; nn--, np++) {
   1102 		if (np->rip6_metric == NEXTHOP_METRIC) {
   1103 			/* modify neighbor address */
   1104 			if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
   1105 				nh = np->rip6_dest;
   1106 				SET_IN6_LINKLOCAL_IFINDEX(nh, idx);
   1107 				trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
   1108 			} else if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest)) {
   1109 				nh = fsock.sin6_addr;
   1110 				trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
   1111 			} else {
   1112 				nh = fsock.sin6_addr;
   1113 				trace(1, "\tInvalid Nexthop: %s\n",
   1114 				    inet6_n2p(&np->rip6_dest));
   1115 			}
   1116 			continue;
   1117 		}
   1118 		if (IN6_IS_ADDR_MULTICAST(&np->rip6_dest)) {
   1119 			trace(1, "\tMulticast netinfo6: %s/%d [%d]\n",
   1120 				inet6_n2p(&np->rip6_dest),
   1121 				np->rip6_plen, np->rip6_metric);
   1122 			continue;
   1123 		}
   1124 		if (IN6_IS_ADDR_LOOPBACK(&np->rip6_dest)) {
   1125 			trace(1, "\tLoopback netinfo6: %s/%d [%d]\n",
   1126 				inet6_n2p(&np->rip6_dest),
   1127 				np->rip6_plen, np->rip6_metric);
   1128 			continue;
   1129 		}
   1130 		if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
   1131 			trace(1, "\tLink Local netinfo6: %s/%d [%d]\n",
   1132 				inet6_n2p(&np->rip6_dest),
   1133 				np->rip6_plen, np->rip6_metric);
   1134 			continue;
   1135 		}
   1136 		/* may need to pass sitelocal prefix in some case, however*/
   1137 		if (IN6_IS_ADDR_SITELOCAL(&np->rip6_dest) && !lflag) {
   1138 			trace(1, "\tSite Local netinfo6: %s/%d [%d]\n",
   1139 				inet6_n2p(&np->rip6_dest),
   1140 				np->rip6_plen, np->rip6_metric);
   1141 			continue;
   1142 		}
   1143 		trace(2, "\tnetinfo6: %s/%d [%d]",
   1144 			inet6_n2p(&np->rip6_dest),
   1145 			np->rip6_plen, np->rip6_metric);
   1146 		if (np->rip6_tag)
   1147 			trace(2, "  tag=0x%04x", ntohs(np->rip6_tag) & 0xffff);
   1148 		if (dflag >= 2) {
   1149 			ia = np->rip6_dest;
   1150 			applyplen(&ia, np->rip6_plen);
   1151 			if (!IN6_ARE_ADDR_EQUAL(&ia, &np->rip6_dest))
   1152 				trace(2, " [junk outside prefix]");
   1153 		}
   1154 
   1155 		/*
   1156 		 * -L: listen only if the prefix matches the configuration
   1157 		 */
   1158 		ok = 1;		/* if there's no L filter, it is ok */
   1159 		for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
   1160 			if (iffp->iff_type != 'L')
   1161 				continue;
   1162 			ok = 0;
   1163 			if (np->rip6_plen < iffp->iff_plen)
   1164 				continue;
   1165 			/* special rule: ::/0 means default, not "in /0" */
   1166 			if (iffp->iff_plen == 0 && np->rip6_plen > 0)
   1167 				continue;
   1168 			ia = np->rip6_dest;
   1169 			applyplen(&ia, iffp->iff_plen);
   1170 			if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
   1171 				ok = 1;
   1172 				break;
   1173 			}
   1174 		}
   1175 		if (!ok) {
   1176 			trace(2, "  (filtered)\n");
   1177 			continue;
   1178 		}
   1179 
   1180 		trace(2, "\n");
   1181 		np->rip6_metric++;
   1182 		np->rip6_metric += ifcp->ifc_metric;
   1183 		if (np->rip6_metric > HOPCNT_INFINITY6)
   1184 			np->rip6_metric = HOPCNT_INFINITY6;
   1185 
   1186 		applyplen(&np->rip6_dest, np->rip6_plen);
   1187 		if ((rrt = rtsearch(np, NULL)) != NULL) {
   1188 			if (rrt->rrt_t == 0)
   1189 				continue;	/* Intf route has priority */
   1190 			nq = &rrt->rrt_info;
   1191 			if (nq->rip6_metric > np->rip6_metric) {
   1192 				if (rrt->rrt_index == ifcp->ifc_index &&
   1193 				    IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
   1194 					/* Small metric from the same gateway */
   1195 					nq->rip6_metric = np->rip6_metric;
   1196 				} else {
   1197 					/* Better route found */
   1198 					rrt->rrt_index = ifcp->ifc_index;
   1199 					/* Update routing table */
   1200 					delroute(nq, &rrt->rrt_gw);
   1201 					rrt->rrt_gw = nh;
   1202 					*nq = *np;
   1203 					addroute(rrt, &nh, ifcp);
   1204 				}
   1205 				rrt->rrt_rflags |= RRTF_CHANGED;
   1206 				rrt->rrt_t = t;
   1207 				need_trigger = 1;
   1208 			} else if (nq->rip6_metric < np->rip6_metric &&
   1209 				   rrt->rrt_index == ifcp->ifc_index &&
   1210 				   IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
   1211 				/* Got worse route from same gw */
   1212 				nq->rip6_metric = np->rip6_metric;
   1213 				rrt->rrt_t = t;
   1214 				rrt->rrt_rflags |= RRTF_CHANGED;
   1215 				need_trigger = 1;
   1216 			} else if (nq->rip6_metric == np->rip6_metric &&
   1217 				   np->rip6_metric < HOPCNT_INFINITY6) {
   1218 				if (rrt->rrt_index == ifcp->ifc_index &&
   1219 				   IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
   1220 					/* same metric, same route from same gw */
   1221 					rrt->rrt_t = t;
   1222 				} else if (rrt->rrt_t < t_half_lifetime) {
   1223 					/* Better route found */
   1224 					rrt->rrt_index = ifcp->ifc_index;
   1225 					/* Update routing table */
   1226 					delroute(nq, &rrt->rrt_gw);
   1227 					rrt->rrt_gw = nh;
   1228 					*nq = *np;
   1229 					addroute(rrt, &nh, ifcp);
   1230 					rrt->rrt_rflags |= RRTF_CHANGED;
   1231 					rrt->rrt_t = t;
   1232 				}
   1233 			}
   1234 			/*
   1235 			 * if nq->rip6_metric == HOPCNT_INFINITY6 then
   1236 			 * do not update age value.  Do nothing.
   1237 			 */
   1238 		} else if (np->rip6_metric < HOPCNT_INFINITY6) {
   1239 			/* Got a new valid route */
   1240 			if ((rrt = MALLOC(struct riprt)) == NULL) {
   1241 				fatal("malloc: struct riprt");
   1242 				/*NOTREACHED*/
   1243 			}
   1244 			memset(rrt, 0, sizeof(*rrt));
   1245 			nq = &rrt->rrt_info;
   1246 
   1247 			rrt->rrt_same = NULL;
   1248 			rrt->rrt_index = ifcp->ifc_index;
   1249 			rrt->rrt_flags = RTF_UP|RTF_GATEWAY;
   1250 			rrt->rrt_gw = nh;
   1251 			*nq = *np;
   1252 			applyplen(&nq->rip6_dest, nq->rip6_plen);
   1253 			if (nq->rip6_plen == sizeof(struct in6_addr) * 8)
   1254 				rrt->rrt_flags |= RTF_HOST;
   1255 
   1256 			/* Put the route to the list */
   1257 			rrt->rrt_next = riprt;
   1258 			riprt = rrt;
   1259 			/* Update routing table */
   1260 			addroute(rrt, &nh, ifcp);
   1261 			rrt->rrt_rflags |= RRTF_CHANGED;
   1262 			need_trigger = 1;
   1263 			rrt->rrt_t = t;
   1264 		}
   1265 	}
   1266 	/* XXX need to care the interval between triggered updates */
   1267 	if (need_trigger) {
   1268 		if (nextalarm > time(NULL) + RIP_TRIG_INT6_MAX) {
   1269 			for (ic = ifc; ic; ic = ic->ifc_next) {
   1270 				if (ifcp->ifc_index == ic->ifc_index)
   1271 					continue;
   1272 				if (ic->ifc_flags & IFF_UP)
   1273 					ripsend(ic, &ic->ifc_ripsin,
   1274 						RRTF_CHANGED);
   1275 			}
   1276 		}
   1277 		/* Reset the flag */
   1278 		for (rrt = riprt; rrt; rrt = rrt->rrt_next)
   1279 			rrt->rrt_rflags &= ~RRTF_CHANGED;
   1280 	}
   1281 }
   1282 
   1283 /*
   1284  * Send all routes request packet to the specified interface.
   1285  */
   1286 void
   1287 sendrequest(struct ifc *ifcp)
   1288 {
   1289 	struct netinfo6 *np;
   1290 	int error;
   1291 
   1292 	if (ifcp->ifc_flags & IFF_LOOPBACK)
   1293 		return;
   1294 	ripbuf->rip6_cmd = RIP6_REQUEST;
   1295 	np = ripbuf->rip6_nets;
   1296 	memset(np, 0, sizeof(struct netinfo6));
   1297 	np->rip6_metric = HOPCNT_INFINITY6;
   1298 	tracet(1, "Send rtdump Request to %s (%s)\n",
   1299 		ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
   1300 	error = sendpacket(&ifcp->ifc_ripsin, RIPSIZE(1));
   1301 	if (error == EAFNOSUPPORT) {
   1302 		/* Protocol not supported */
   1303 		tracet(1, "Could not send rtdump Request to %s (%s): "
   1304 			"set IFF_UP to 0\n",
   1305 			ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
   1306 		ifcp->ifc_flags &= ~IFF_UP;	/* As if down for AF_INET6 */
   1307 	}
   1308 	ripbuf->rip6_cmd = RIP6_RESPONSE;
   1309 }
   1310 
   1311 /*
   1312  * Process a RIP6_REQUEST packet.
   1313  */
   1314 void
   1315 riprequest(struct ifc *ifcp, struct netinfo6 *np, int nn,
   1316 	   struct sockaddr_in6 *sin6)
   1317 {
   1318 	int i;
   1319 	struct riprt *rrt;
   1320 
   1321 	if (!(nn == 1 && IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest) &&
   1322 	      np->rip6_plen == 0 && np->rip6_metric == HOPCNT_INFINITY6)) {
   1323 		/* Specific response, don't split-horizon */
   1324 		trace(1, "\tRIP Request\n");
   1325 		for (i = 0; i < nn; i++, np++) {
   1326 			rrt = rtsearch(np, NULL);
   1327 			if (rrt)
   1328 				np->rip6_metric = rrt->rrt_info.rip6_metric;
   1329 			else
   1330 				np->rip6_metric = HOPCNT_INFINITY6;
   1331 		}
   1332 		(void)sendpacket(sin6, RIPSIZE(nn));
   1333 		return;
   1334 	}
   1335 	/* Whole routing table dump */
   1336 	trace(1, "\tRIP Request -- whole routing table\n");
   1337 	ripsend(ifcp, sin6, RRTF_SENDANYWAY);
   1338 }
   1339 
   1340 /*
   1341  * Get information of each interface.
   1342  */
   1343 void
   1344 ifconfig(void)
   1345 {
   1346 	struct ifaddrs *ifap, *ifa;
   1347 	struct ifc *ifcp;
   1348 	struct ipv6_mreq mreq;
   1349 	int s;
   1350 
   1351 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
   1352 		fatal("socket");
   1353 		/*NOTREACHED*/
   1354 	}
   1355 
   1356 	if (getifaddrs(&ifap) != 0) {
   1357 		fatal("getifaddrs");
   1358 		/*NOTREACHED*/
   1359 	}
   1360 
   1361 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
   1362 		if (ifa->ifa_addr->sa_family != AF_INET6)
   1363 			continue;
   1364 		ifcp = ifc_find(ifa->ifa_name);
   1365 		/* we are interested in multicast-capable interfaces */
   1366 		if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
   1367 			continue;
   1368 		if (!ifcp) {
   1369 			/* new interface */
   1370 			if ((ifcp = MALLOC(struct ifc)) == NULL) {
   1371 				fatal("malloc: struct ifc");
   1372 				/*NOTREACHED*/
   1373 			}
   1374 			memset(ifcp, 0, sizeof(*ifcp));
   1375 			ifcp->ifc_index = -1;
   1376 			ifcp->ifc_next = ifc;
   1377 			ifc = ifcp;
   1378 			nifc++;
   1379 			ifcp->ifc_name = allocopy(ifa->ifa_name);
   1380 			ifcp->ifc_addr = 0;
   1381 			ifcp->ifc_filter = 0;
   1382 			ifcp->ifc_flags = ifa->ifa_flags;
   1383 			trace(1, "newif %s <%s>\n", ifcp->ifc_name,
   1384 				ifflags(ifcp->ifc_flags));
   1385 			if (!strcmp(ifcp->ifc_name, LOOPBACK_IF))
   1386 				loopifcp = ifcp;
   1387 		} else {
   1388 			/* update flag, this may be up again */
   1389 			if (ifcp->ifc_flags != ifa->ifa_flags) {
   1390 				trace(1, "%s: <%s> -> ", ifcp->ifc_name,
   1391 					ifflags(ifcp->ifc_flags));
   1392 				trace(1, "<%s>\n", ifflags(ifa->ifa_flags));
   1393 				ifcp->ifc_cflags |= IFC_CHANGED;
   1394 			}
   1395 			ifcp->ifc_flags = ifa->ifa_flags;
   1396 		}
   1397 		ifconfig1(ifa->ifa_name, ifa->ifa_addr, ifcp, s);
   1398 		if ((ifcp->ifc_flags & (IFF_LOOPBACK | IFF_UP)) == IFF_UP
   1399 		 && 0 < ifcp->ifc_index && !ifcp->ifc_joined) {
   1400 			mreq.ipv6mr_multiaddr = ifcp->ifc_ripsin.sin6_addr;
   1401 			mreq.ipv6mr_interface = ifcp->ifc_index;
   1402 			if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
   1403 			    &mreq, sizeof(mreq)) < 0) {
   1404 				fatal("IPV6_JOIN_GROUP");
   1405 				/*NOTREACHED*/
   1406 			}
   1407 			trace(1, "join %s %s\n", ifcp->ifc_name, RIP6_DEST);
   1408 			ifcp->ifc_joined++;
   1409 		}
   1410 	}
   1411 	close(s);
   1412 	freeifaddrs(ifap);
   1413 }
   1414 
   1415 void
   1416 ifconfig1(const char *name, const struct sockaddr *sa, struct ifc *ifcp, int s)
   1417 {
   1418 	struct	in6_ifreq ifr;
   1419 	const struct sockaddr_in6 *sin6;
   1420 	struct	ifac *ifa;
   1421 	int	plen;
   1422 	char	buf[BUFSIZ];
   1423 
   1424 	sin6 = (const struct sockaddr_in6 *)sa;
   1425 	if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && !lflag)
   1426 		return;
   1427 	ifr.ifr_addr = *sin6;
   1428 	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1429 	if (ioctl(s, SIOCGIFNETMASK_IN6, (char *)&ifr) < 0) {
   1430 		fatal("ioctl: SIOCGIFNETMASK_IN6");
   1431 		/*NOTREACHED*/
   1432 	}
   1433 	plen = sin6mask2len(&ifr.ifr_addr);
   1434 	if ((ifa = ifa_match(ifcp, &sin6->sin6_addr, plen)) != NULL) {
   1435 		/* same interface found */
   1436 		/* need check if something changed */
   1437 		/* XXX not yet implemented */
   1438 		return;
   1439 	}
   1440 	/*
   1441 	 * New address is found
   1442 	 */
   1443 	if ((ifa = MALLOC(struct ifac)) == NULL) {
   1444 		fatal("malloc: struct ifac");
   1445 		/*NOTREACHED*/
   1446 	}
   1447 	memset(ifa, 0, sizeof(*ifa));
   1448 	ifa->ifa_conf = ifcp;
   1449 	ifa->ifa_next = ifcp->ifc_addr;
   1450 	ifcp->ifc_addr = ifa;
   1451 	ifa->ifa_addr = sin6->sin6_addr;
   1452 	ifa->ifa_plen = plen;
   1453 	if (ifcp->ifc_flags & IFF_POINTOPOINT) {
   1454 		ifr.ifr_addr = *sin6;
   1455 		if (ioctl(s, SIOCGIFDSTADDR_IN6, (char *)&ifr) < 0) {
   1456 			fatal("ioctl: SIOCGIFDSTADDR_IN6");
   1457 			/*NOTREACHED*/
   1458 		}
   1459 		ifa->ifa_raddr = ifr.ifr_dstaddr.sin6_addr;
   1460 		inet_ntop(AF_INET6, (void *)&ifa->ifa_raddr, buf, sizeof(buf));
   1461 		trace(1, "found address %s/%d -- %s\n",
   1462 			inet6_n2p(&ifa->ifa_addr), ifa->ifa_plen, buf);
   1463 	} else {
   1464 		trace(1, "found address %s/%d\n",
   1465 			inet6_n2p(&ifa->ifa_addr), ifa->ifa_plen);
   1466 	}
   1467 	if (ifcp->ifc_index < 0 && IN6_IS_ADDR_LINKLOCAL(&ifa->ifa_addr)) {
   1468 		ifcp->ifc_mylladdr = ifa->ifa_addr;
   1469 		ifcp->ifc_index = IN6_LINKLOCAL_IFINDEX(ifa->ifa_addr);
   1470 		memcpy(&ifcp->ifc_ripsin, &ripsin, ripsin.ss_len);
   1471 		SET_IN6_LINKLOCAL_IFINDEX(ifcp->ifc_ripsin.sin6_addr,
   1472 			ifcp->ifc_index);
   1473 		setindex2ifc(ifcp->ifc_index, ifcp);
   1474 		ifcp->ifc_mtu = getifmtu(ifcp->ifc_index);
   1475 		if (ifcp->ifc_mtu > RIP6_MAXMTU)
   1476 			ifcp->ifc_mtu = RIP6_MAXMTU;
   1477 		if (ioctl(s, SIOCGIFMETRIC, (char *)&ifr) < 0) {
   1478 			fatal("ioctl: SIOCGIFMETRIC");
   1479 			/*NOTREACHED*/
   1480 		}
   1481 		ifcp->ifc_metric = ifr.ifr_metric;
   1482 		trace(1, "\tindex: %d, mtu: %d, metric: %d\n",
   1483 			ifcp->ifc_index, ifcp->ifc_mtu, ifcp->ifc_metric);
   1484 	} else
   1485 		ifcp->ifc_cflags |= IFC_CHANGED;
   1486 }
   1487 
   1488 /*
   1489  * Receive and process routing messages.
   1490  * Update interface information as necesssary.
   1491  */
   1492 void
   1493 rtrecv(void)
   1494 {
   1495 	char buf[BUFSIZ];
   1496 	char *p, *q;
   1497 	struct rt_msghdr *rtm;
   1498 	struct ifa_msghdr *ifam;
   1499 	struct if_msghdr *ifm;
   1500 	int len;
   1501 	struct ifc *ifcp, *ic;
   1502 	int iface = 0, rtable = 0;
   1503 	struct sockaddr_in6 *rta[RTAX_MAX];
   1504 	struct sockaddr_in6 mask;
   1505 	int i, addrs;
   1506 	struct riprt *rrt;
   1507 
   1508 	if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
   1509 		perror("read from rtsock");
   1510 		exit(1);
   1511 	}
   1512 	if (len < sizeof(*rtm)) {
   1513 		trace(1, "short read from rtsock: %d (should be > %lu)\n",
   1514 			len, (u_long)sizeof(*rtm));
   1515 		return;
   1516 	}
   1517 	if (dflag >= 2) {
   1518 		fprintf(stderr, "rtmsg:\n");
   1519 		for (i = 0; i < len; i++) {
   1520 			fprintf(stderr, "%02x ", buf[i] & 0xff);
   1521 			if (i % 16 == 15) fprintf(stderr, "\n");
   1522 		}
   1523 		fprintf(stderr, "\n");
   1524 	}
   1525 
   1526 	for (p = buf; p - buf < len; p += ((struct rt_msghdr *)p)->rtm_msglen) {
   1527 		/* safety against bogus message */
   1528 		if (((struct rt_msghdr *)p)->rtm_msglen <= 0) {
   1529 			trace(1, "bogus rtmsg: length=%d\n",
   1530 				((struct rt_msghdr *)p)->rtm_msglen);
   1531 			break;
   1532 		}
   1533 		rtm = NULL;
   1534 		ifam = NULL;
   1535 		ifm = NULL;
   1536 		switch (((struct rt_msghdr *)p)->rtm_type) {
   1537 		case RTM_NEWADDR:
   1538 		case RTM_DELADDR:
   1539 			ifam = (struct ifa_msghdr *)p;
   1540 			addrs = ifam->ifam_addrs;
   1541 			q = (char *)(ifam + 1);
   1542 			break;
   1543 		case RTM_IFINFO:
   1544 			ifm = (struct if_msghdr *)p;
   1545 			addrs = ifm->ifm_addrs;
   1546 			q = (char *)(ifm + 1);
   1547 			break;
   1548 		default:
   1549 			rtm = (struct rt_msghdr *)p;
   1550 			addrs = rtm->rtm_addrs;
   1551 			q = (char *)(rtm + 1);
   1552 			if (rtm->rtm_version != RTM_VERSION) {
   1553 				trace(1, "unexpected rtmsg version %d "
   1554 					"(should be %d)\n",
   1555 					rtm->rtm_version, RTM_VERSION);
   1556 				continue;
   1557 			}
   1558 			if (rtm->rtm_pid == pid) {
   1559 #if 0
   1560 				trace(1, "rtmsg looped back to me, ignored\n");
   1561 #endif
   1562 				continue;
   1563 			}
   1564 			break;
   1565 		}
   1566 		memset(&rta, 0, sizeof(rta));
   1567 		for (i = 0; i < RTAX_MAX; i++) {
   1568 			if (addrs & (1 << i)) {
   1569 				rta[i] = (struct sockaddr_in6 *)q;
   1570 				q += ROUNDUP(rta[i]->sin6_len);
   1571 			}
   1572 		}
   1573 
   1574 		trace(1, "rtsock: %s (addrs=%x)\n",
   1575 			rttypes((struct rt_msghdr *)p), addrs);
   1576 		if (dflag >= 2) {
   1577 			for (i = 0;
   1578 			     i < ((struct rt_msghdr *)p)->rtm_msglen;
   1579 			     i++) {
   1580 				fprintf(stderr, "%02x ", p[i] & 0xff);
   1581 				if (i % 16 == 15) fprintf(stderr, "\n");
   1582 			}
   1583 			fprintf(stderr, "\n");
   1584 		}
   1585 
   1586 		/*
   1587 		 * Easy ones first.
   1588 		 *
   1589 		 * We may be able to optimize by using ifm->ifm_index or
   1590 		 * ifam->ifam_index.  For simplicity we don't do that here.
   1591 		 */
   1592 		switch (((struct rt_msghdr *)p)->rtm_type) {
   1593 		case RTM_NEWADDR:
   1594 		case RTM_IFINFO:
   1595 			iface++;
   1596 			continue;
   1597 		case RTM_ADD:
   1598 			rtable++;
   1599 			continue;
   1600 		case RTM_LOSING:
   1601 		case RTM_MISS:
   1602 		case RTM_RESOLVE:
   1603 		case RTM_GET:
   1604 		case RTM_LOCK:
   1605 			/* nothing to be done here */
   1606 			trace(1, "\tnothing to be done, ignored\n");
   1607 			continue;
   1608 		}
   1609 
   1610 #if 0
   1611 		if (rta[RTAX_DST] == NULL) {
   1612 			trace(1, "\tno destination, ignored\n");
   1613 			continue;
   1614 		}
   1615 		if (rta[RTAX_DST]->sin6_family != AF_INET6) {
   1616 			trace(1, "\taf mismatch, ignored\n");
   1617 			continue;
   1618 		}
   1619 		if (IN6_IS_ADDR_LINKLOCAL(&rta[RTAX_DST]->sin6_addr)) {
   1620 			trace(1, "\tlinklocal destination, ignored\n");
   1621 			continue;
   1622 		}
   1623 		if (IN6_ARE_ADDR_EQUAL(&rta[RTAX_DST]->sin6_addr, &in6addr_loopback)) {
   1624 			trace(1, "\tloopback destination, ignored\n");
   1625 			continue;		/* Loopback */
   1626 		}
   1627 		if (IN6_IS_ADDR_MULTICAST(&rta[RTAX_DST]->sin6_addr)) {
   1628 			trace(1, "\tmulticast destination, ignored\n");
   1629 			continue;
   1630 		}
   1631 #endif
   1632 
   1633 		/* hard ones */
   1634 		switch (((struct rt_msghdr *)p)->rtm_type) {
   1635 		case RTM_NEWADDR:
   1636 		case RTM_IFINFO:
   1637 		case RTM_ADD:
   1638 		case RTM_LOSING:
   1639 		case RTM_MISS:
   1640 		case RTM_RESOLVE:
   1641 		case RTM_GET:
   1642 		case RTM_LOCK:
   1643 			/* should already be handled */
   1644 			fatal("rtrecv: never reach here");
   1645 			/*NOTREACHED*/
   1646 		case RTM_DELETE:
   1647 			if (!rta[RTAX_DST] || !rta[RTAX_GATEWAY]) {
   1648 				trace(1, "\tsome of dst/gw/netamsk are "
   1649 				    "unavailable, ignored\n");
   1650 				break;
   1651 			}
   1652 			if ((rtm->rtm_flags & RTF_HOST) != 0) {
   1653 				mask.sin6_len = sizeof(mask);
   1654 				memset(&mask.sin6_addr, 0xff,
   1655 				    sizeof(mask.sin6_addr));
   1656 				rta[RTAX_NETMASK] = &mask;
   1657 			} else if (!rta[RTAX_NETMASK]) {
   1658 				trace(1, "\tsome of dst/gw/netamsk are "
   1659 				    "unavailable, ignored\n");
   1660 				break;
   1661 			}
   1662 			if (rt_del(rta[RTAX_DST], rta[RTAX_GATEWAY],
   1663 			    rta[RTAX_NETMASK]) == 0) {
   1664 				rtable++;	/*just to be sure*/
   1665 			}
   1666 			break;
   1667 		case RTM_CHANGE:
   1668 		case RTM_REDIRECT:
   1669 			trace(1, "\tnot supported yet, ignored\n");
   1670 			break;
   1671 		case RTM_DELADDR:
   1672 			if (!rta[RTAX_NETMASK] || !rta[RTAX_IFA]) {
   1673 				trace(1, "\tno netmask or ifa given, ignored\n");
   1674 				break;
   1675 			}
   1676 			if (ifam->ifam_index < nindex2ifc)
   1677 				ifcp = index2ifc[ifam->ifam_index];
   1678 			else
   1679 				ifcp = NULL;
   1680 			if (!ifcp) {
   1681 				trace(1, "\tinvalid ifam_index %d, ignored\n",
   1682 					ifam->ifam_index);
   1683 				break;
   1684 			}
   1685 			if (!rt_deladdr(ifcp, rta[RTAX_IFA], rta[RTAX_NETMASK]))
   1686 				iface++;
   1687 			break;
   1688 		case RTM_OLDADD:
   1689 		case RTM_OLDDEL:
   1690 			trace(1, "\tnot supported yet, ignored\n");
   1691 			break;
   1692 		}
   1693 
   1694 	}
   1695 
   1696 	if (iface) {
   1697 		trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
   1698 		ifconfig();
   1699 		for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next)
   1700 			if (ifcp->ifc_cflags & IFC_CHANGED) {
   1701 				if (ifrt(ifcp, 1)) {
   1702 					for (ic = ifc; ic; ic = ic->ifc_next) {
   1703 						if (ifcp->ifc_index == ic->ifc_index)
   1704 							continue;
   1705 						if (ic->ifc_flags & IFF_UP)
   1706 							ripsend(ic, &ic->ifc_ripsin,
   1707 							RRTF_CHANGED);
   1708 					}
   1709 					/* Reset the flag */
   1710 					for (rrt = riprt; rrt; rrt = rrt->rrt_next)
   1711 						rrt->rrt_rflags &= ~RRTF_CHANGED;
   1712 				}
   1713 				ifcp->ifc_cflags &= ~IFC_CHANGED;
   1714 			}
   1715 	}
   1716 	if (rtable) {
   1717 		trace(1, "rtsock: read routing table again\n");
   1718 		krtread(1);
   1719 	}
   1720 }
   1721 
   1722 /*
   1723  * remove specified route from the internal routing table.
   1724  */
   1725 int
   1726 rt_del(const struct sockaddr_in6 *sdst, const struct sockaddr_in6 *sgw,
   1727        const struct sockaddr_in6 *smask)
   1728 {
   1729 	const struct in6_addr *dst = NULL;
   1730 	const struct in6_addr *gw = NULL;
   1731 	int prefix;
   1732 	struct netinfo6 ni6;
   1733 	struct riprt *rrt = NULL;
   1734 	time_t t_lifetime;
   1735 
   1736 	if (sdst->sin6_family != AF_INET6) {
   1737 		trace(1, "\tother AF, ignored\n");
   1738 		return -1;
   1739 	}
   1740 	if (IN6_IS_ADDR_LINKLOCAL(&sdst->sin6_addr)
   1741 	 || IN6_ARE_ADDR_EQUAL(&sdst->sin6_addr, &in6addr_loopback)
   1742 	 || IN6_IS_ADDR_MULTICAST(&sdst->sin6_addr)) {
   1743 		trace(1, "\taddress %s not interesting, ignored\n",
   1744 			inet6_n2p(&sdst->sin6_addr));
   1745 		return -1;
   1746 	}
   1747 	dst = &sdst->sin6_addr;
   1748 	if (sgw->sin6_family == AF_INET6) {
   1749 		/* easy case */
   1750 		gw = &sgw->sin6_addr;
   1751 		prefix = sin6mask2len(smask);
   1752 	} else if (sgw->sin6_family == AF_LINK) {
   1753 		/*
   1754 		 * Interface route... a hard case.  We need to get the prefix
   1755 		 * length from the kernel, but we now are parsing rtmsg.
   1756 		 * We'll purge matching routes from my list, then get the
   1757 		 * fresh list.
   1758 		 */
   1759 		struct riprt *longest;
   1760 		trace(1, "\t%s is an interface route, guessing prefixlen\n",
   1761 			inet6_n2p(dst));
   1762 		longest = NULL;
   1763 		for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
   1764 			if (IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
   1765 					&sdst->sin6_addr)
   1766 			 && IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)) {
   1767 				if (!longest
   1768 				 || longest->rrt_info.rip6_plen <
   1769 						 rrt->rrt_info.rip6_plen) {
   1770 					longest = rrt;
   1771 				}
   1772 			}
   1773 		}
   1774 		rrt = longest;
   1775 		if (!rrt) {
   1776 			trace(1, "\tno matching interface route found\n");
   1777 			return -1;
   1778 		}
   1779 		gw = &in6addr_loopback;
   1780 		prefix = rrt->rrt_info.rip6_plen;
   1781 	} else {
   1782 		trace(1, "\tunsupported af: (gw=%d)\n", sgw->sin6_family);
   1783 		return -1;
   1784 	}
   1785 
   1786 	trace(1, "\tdeleting %s/%d ", inet6_n2p(dst), prefix);
   1787 	trace(1, "gw %s\n", inet6_n2p(gw));
   1788 	t_lifetime = time(NULL) - RIP_LIFETIME;
   1789 	/* age route for interface address */
   1790 	memset(&ni6, 0, sizeof(ni6));
   1791 	ni6.rip6_dest = *dst;
   1792 	ni6.rip6_plen = prefix;
   1793 	applyplen(&ni6.rip6_dest, ni6.rip6_plen);	/*to be sure*/
   1794 	trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6.rip6_dest),
   1795 		ni6.rip6_plen);
   1796 	if (!rrt && (rrt = rtsearch(&ni6, NULL)) == NULL) {
   1797 		trace(1, "\tno route found\n");
   1798 		return -1;
   1799 	}
   1800 #if 0
   1801 	if ((rrt->rrt_flags & RTF_STATIC) == 0) {
   1802 		trace(1, "\tyou can delete static routes only\n");
   1803 	} else
   1804 #endif
   1805 	if (!IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, gw)) {
   1806 		trace(1, "\tgw mismatch: %s <-> ",
   1807 			inet6_n2p(&rrt->rrt_gw));
   1808 		trace(1, "%s\n", inet6_n2p(gw));
   1809 	} else {
   1810 		trace(1, "\troute found, age it\n");
   1811 		if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
   1812 			rrt->rrt_t = t_lifetime;
   1813 			rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
   1814 		}
   1815 	}
   1816 	return 0;
   1817 }
   1818 
   1819 /*
   1820  * remove specified address from internal interface/routing table.
   1821  */
   1822 int
   1823 rt_deladdr(struct ifc *ifcp, const struct sockaddr_in6 *sifa,
   1824 	   const struct sockaddr_in6 *smask)
   1825 {
   1826 	const struct in6_addr *addr = NULL;
   1827 	int prefix;
   1828 	struct ifac *ifa = NULL;
   1829 	struct netinfo6 ni6;
   1830 	struct riprt *rrt = NULL;
   1831 	time_t t_lifetime;
   1832 	int updated = 0;
   1833 
   1834 	if (sifa->sin6_family != AF_INET6) {
   1835 		trace(1, "\tother AF, ignored\n");
   1836 		return -1;
   1837 	}
   1838 	addr = &sifa->sin6_addr;
   1839 	prefix = sin6mask2len(smask);
   1840 
   1841 	trace(1, "\tdeleting %s/%d from %s\n",
   1842 		inet6_n2p(addr), prefix, ifcp->ifc_name);
   1843 	ifa = ifa_match(ifcp, addr, prefix);
   1844 	if (!ifa) {
   1845 		trace(1, "\tno matching ifa found for %s/%d on %s\n",
   1846 			inet6_n2p(addr), prefix, ifcp->ifc_name);
   1847 		return -1;
   1848 	}
   1849 	if (ifa->ifa_conf != ifcp) {
   1850 		trace(1, "\taddress table corrupt: back pointer does not match "
   1851 			"(%s != %s)\n",
   1852 			ifcp->ifc_name, ifa->ifa_conf->ifc_name);
   1853 		return -1;
   1854 	}
   1855 	/* remove ifa from interface */
   1856 	if (ifcp->ifc_addr == ifa)
   1857 		ifcp->ifc_addr = ifa->ifa_next;
   1858 	else {
   1859 		struct ifac *p;
   1860 		for (p = ifcp->ifc_addr; p; p = p->ifa_next) {
   1861 			if (p->ifa_next == ifa) {
   1862 				p->ifa_next = ifa->ifa_next;
   1863 				break;
   1864 			}
   1865 		}
   1866 	}
   1867 	ifa->ifa_next = NULL;
   1868 	ifa->ifa_conf = NULL;
   1869 	t_lifetime = time(NULL) - RIP_LIFETIME;
   1870 	/* age route for interface address */
   1871 	memset(&ni6, 0, sizeof(ni6));
   1872 	ni6.rip6_dest = ifa->ifa_addr;
   1873 	ni6.rip6_plen = ifa->ifa_plen;
   1874 	applyplen(&ni6.rip6_dest, ni6.rip6_plen);
   1875 	trace(1, "\tfind interface route %s/%d on %d\n",
   1876 		inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen, ifcp->ifc_index);
   1877 	if ((rrt = rtsearch(&ni6, NULL)) != NULL) {
   1878 		struct in6_addr none;
   1879 		memset(&none, 0, sizeof(none));
   1880 		if (rrt->rrt_index == ifcp->ifc_index &&
   1881 		    (IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &none) ||
   1882 		     IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw))) {
   1883 			trace(1, "\troute found, age it\n");
   1884 			if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
   1885 				rrt->rrt_t = t_lifetime;
   1886 				rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
   1887 			}
   1888 			updated++;
   1889 		} else {
   1890 			trace(1, "\tnon-interface route found: %s/%d on %d\n",
   1891 				inet6_n2p(&rrt->rrt_info.rip6_dest),
   1892 				rrt->rrt_info.rip6_plen,
   1893 				rrt->rrt_index);
   1894 		}
   1895 	} else
   1896 		trace(1, "\tno interface route found\n");
   1897 	/* age route for p2p destination */
   1898 	if (ifcp->ifc_flags & IFF_POINTOPOINT) {
   1899 		memset(&ni6, 0, sizeof(ni6));
   1900 		ni6.rip6_dest = ifa->ifa_raddr;
   1901 		ni6.rip6_plen = 128;
   1902 		applyplen(&ni6.rip6_dest, ni6.rip6_plen);	/*to be sure*/
   1903 		trace(1, "\tfind p2p route %s/%d on %d\n",
   1904 			inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen,
   1905 			ifcp->ifc_index);
   1906 		if ((rrt = rtsearch(&ni6, NULL)) != NULL) {
   1907 			if (rrt->rrt_index == ifcp->ifc_index &&
   1908 			    IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &ifa->ifa_addr)) {
   1909 				trace(1, "\troute found, age it\n");
   1910 				if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
   1911 					rrt->rrt_t = t_lifetime;
   1912 					rrt->rrt_info.rip6_metric =
   1913 					    HOPCNT_INFINITY6;
   1914 					updated++;
   1915 				}
   1916 			} else {
   1917 				trace(1, "\tnon-p2p route found: %s/%d on %d\n",
   1918 					inet6_n2p(&rrt->rrt_info.rip6_dest),
   1919 					rrt->rrt_info.rip6_plen,
   1920 					rrt->rrt_index);
   1921 			}
   1922 		} else
   1923 			trace(1, "\tno p2p route found\n");
   1924 	}
   1925 	return updated ? 0 : -1;
   1926 }
   1927 
   1928 /*
   1929  * Get each interface address and put those interface routes to the route
   1930  * list.
   1931  */
   1932 int
   1933 ifrt(struct ifc *ifcp, int again)
   1934 {
   1935 	struct ifac *ifa;
   1936 	struct riprt *rrt = NULL, *search_rrt, *prev_rrt, *loop_rrt;
   1937 	struct netinfo6 *np;
   1938 	time_t t_lifetime;
   1939 	int need_trigger = 0;
   1940 
   1941 #if 0
   1942 	if (ifcp->ifc_flags & IFF_LOOPBACK)
   1943 		return 0;			/* ignore loopback */
   1944 #endif
   1945 
   1946 	if (ifcp->ifc_flags & IFF_POINTOPOINT) {
   1947 		ifrt_p2p(ifcp, again);
   1948 		return 0;
   1949 	}
   1950 
   1951 	for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
   1952 		if (IN6_IS_ADDR_LINKLOCAL(&ifa->ifa_addr)) {
   1953 #if 0
   1954 			trace(1, "route: %s on %s: "
   1955 			    "skip linklocal interface address\n",
   1956 			    inet6_n2p(&ifa->ifa_addr), ifcp->ifc_name);
   1957 #endif
   1958 			continue;
   1959 		}
   1960 		if (IN6_IS_ADDR_UNSPECIFIED(&ifa->ifa_addr)) {
   1961 #if 0
   1962 			trace(1, "route: %s: skip unspec interface address\n",
   1963 			    ifcp->ifc_name);
   1964 #endif
   1965 			continue;
   1966 		}
   1967 		if (IN6_IS_ADDR_LOOPBACK(&ifa->ifa_addr)) {
   1968 #if 0
   1969 			trace(1, "route: %s: skip loopback address\n",
   1970 			    ifcp->ifc_name);
   1971 #endif
   1972 			continue;
   1973 		}
   1974 		if (ifcp->ifc_flags & IFF_UP) {
   1975 			if ((rrt = MALLOC(struct riprt)) == NULL)
   1976 				fatal("malloc: struct riprt");
   1977 			memset(rrt, 0, sizeof(*rrt));
   1978 			rrt->rrt_same = NULL;
   1979 			rrt->rrt_index = ifcp->ifc_index;
   1980 			rrt->rrt_t = 0;	/* don't age */
   1981 			rrt->rrt_info.rip6_dest = ifa->ifa_addr;
   1982 			rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
   1983 			rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
   1984 			rrt->rrt_info.rip6_plen = ifa->ifa_plen;
   1985 			if (ifa->ifa_plen == 128)
   1986 				rrt->rrt_flags = RTF_HOST;
   1987 			else
   1988 				rrt->rrt_flags = RTF_CLONING;
   1989 			rrt->rrt_rflags |= RRTF_CHANGED;
   1990 			applyplen(&rrt->rrt_info.rip6_dest, ifa->ifa_plen);
   1991 			memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
   1992 			rrt->rrt_gw = ifa->ifa_addr;
   1993 			np = &rrt->rrt_info;
   1994 			search_rrt = rtsearch(np, &prev_rrt);
   1995 			if (search_rrt != NULL) {
   1996 				if (search_rrt->rrt_info.rip6_metric <=
   1997 				    rrt->rrt_info.rip6_metric) {
   1998 					/* Already have better route */
   1999 					if (!again) {
   2000 						trace(1, "route: %s/%d: "
   2001 						    "already registered (%s)\n",
   2002 						    inet6_n2p(&np->rip6_dest), np->rip6_plen,
   2003 						    ifcp->ifc_name);
   2004 					}
   2005 					goto next;
   2006 				}
   2007 
   2008 				if (prev_rrt)
   2009 					prev_rrt->rrt_next = rrt->rrt_next;
   2010 				else
   2011 					riprt = rrt->rrt_next;
   2012 				delroute(&rrt->rrt_info, &rrt->rrt_gw);
   2013 			}
   2014 			/* Attach the route to the list */
   2015 			trace(1, "route: %s/%d: register route (%s)\n",
   2016 			    inet6_n2p(&np->rip6_dest), np->rip6_plen,
   2017 			    ifcp->ifc_name);
   2018 			rrt->rrt_next = riprt;
   2019 			riprt = rrt;
   2020 			addroute(rrt, &rrt->rrt_gw, ifcp);
   2021 			rrt = NULL;
   2022 			sendrequest(ifcp);
   2023 			ripsend(ifcp, &ifcp->ifc_ripsin, 0);
   2024 			need_trigger = 1;
   2025 		} else {
   2026 			for (loop_rrt = riprt; loop_rrt; loop_rrt = loop_rrt->rrt_next) {
   2027 				if (loop_rrt->rrt_index == ifcp->ifc_index) {
   2028 					t_lifetime = time(NULL) - RIP_LIFETIME;
   2029 					if (loop_rrt->rrt_t == 0 || loop_rrt->rrt_t > t_lifetime) {
   2030 						loop_rrt->rrt_t = t_lifetime;
   2031 						loop_rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
   2032 						loop_rrt->rrt_rflags |= RRTF_CHANGED;
   2033 						need_trigger = 1;
   2034 					}
   2035 				}
   2036 			}
   2037                 }
   2038 	next:
   2039 		if (rrt)
   2040 			free(rrt);
   2041 	}
   2042 	return need_trigger;
   2043 }
   2044 
   2045 /*
   2046  * there are couple of p2p interface routing models.  "behavior" lets
   2047  * you pick one.  it looks that gated behavior fits best with BSDs,
   2048  * since BSD kernels do not look at prefix length on p2p interfaces.
   2049  */
   2050 void
   2051 ifrt_p2p(struct ifc *ifcp, int again)
   2052 {
   2053 	struct ifac *ifa;
   2054 	struct riprt *rrt, *orrt, *prevrrt;
   2055 	struct netinfo6 *np;
   2056 	struct in6_addr addr, dest;
   2057 	int advert, ignore, i;
   2058 #define P2PADVERT_NETWORK	1
   2059 #define P2PADVERT_ADDR		2
   2060 #define P2PADVERT_DEST		4
   2061 #define P2PADVERT_MAX		4
   2062 #define	CISCO	0
   2063 #define	GATED	1
   2064 #define ROUTE6D	2
   2065 #define BEHAVIOR GATED
   2066 	const char *category = "";
   2067 	const char *noadv;
   2068 
   2069 	for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
   2070 		addr = ifa->ifa_addr;
   2071 		dest = ifa->ifa_raddr;
   2072 		applyplen(&addr, ifa->ifa_plen);
   2073 		applyplen(&dest, ifa->ifa_plen);
   2074 		advert = ignore = 0;
   2075 #if BEHAVIOR == CISCO
   2076 		/*
   2077 		 * honor addr/plen, just like normal shared medium
   2078 		 * interface.  this may cause trouble if you reuse
   2079 		 * addr/plen on other interfaces.
   2080 		 *
   2081 		 * advertise addr/plen.
   2082 		 */
   2083 		advert |= P2PADVERT_NETWORK;
   2084 #endif
   2085 #if BEHAVIOR == GATED
   2086 		/*
   2087 		 * prefixlen on p2p interface is meaningless.
   2088 		 * advertise addr/128 and dest/128.
   2089 		 *
   2090 		 * do not install network route to route6d routing
   2091 		 * table (if we do, it would prevent route installation
   2092 		 * for other p2p interface that shares addr/plen).
   2093 		 *
   2094 		 * XXX what should we do if dest is ::?  it will not
   2095 		 * get announced anyways (see following filter),
   2096 		 * but we need to think.
   2097 		 */
   2098 		advert |= P2PADVERT_ADDR;
   2099 		advert |= P2PADVERT_DEST;
   2100 		ignore |= P2PADVERT_NETWORK;
   2101 #if BEHAVIOR == ROUTE6D
   2102 		/*
   2103 		 * just for testing.  actually the code is redundant
   2104 		 * given the current p2p interface address assignment
   2105 		 * rule for kame kernel.
   2106 		 *
   2107 		 * intent:
   2108 		 *	A/n -> announce A/n
   2109 		 *	A B/n, A and B share prefix -> A/n (= B/n)
   2110 		 *	A B/n, do not share prefix -> A/128 and B/128
   2111 		 * actually, A/64 and A B/128 are the only cases
   2112 		 * permitted by the kernel:
   2113 		 *	A/64 -> A/64
   2114 		 *	A B/128 -> A/128 and B/128
   2115 		 */
   2116 		if (!IN6_IS_ADDR_UNSPECIFIED(&ifa->ifa_raddr)) {
   2117 			if (IN6_ARE_ADDR_EQUAL(&addr, &dest))
   2118 				advert |= P2PADVERT_NETWORK;
   2119 			else {
   2120 				advert |= P2PADVERT_ADDR;
   2121 				advert |= P2PADVERT_DEST;
   2122 				ignore |= P2PADVERT_NETWORK;
   2123 			}
   2124 		} else
   2125 			advert |= P2PADVERT_NETWORK;
   2126 #endif
   2127 
   2128 		for (i = 1; i <= P2PADVERT_MAX; i *= 2) {
   2129 			if ((ignore & i) != 0)
   2130 				continue;
   2131 			if ((rrt = MALLOC(struct riprt)) == NULL) {
   2132 				fatal("malloc: struct riprt");
   2133 				/*NOTREACHED*/
   2134 			}
   2135 			memset(rrt, 0, sizeof(*rrt));
   2136 			rrt->rrt_same = NULL;
   2137 			rrt->rrt_index = ifcp->ifc_index;
   2138 			rrt->rrt_t = 0;	/* don't age */
   2139 			switch (i) {
   2140 			case P2PADVERT_NETWORK:
   2141 				rrt->rrt_info.rip6_dest = ifa->ifa_addr;
   2142 				rrt->rrt_info.rip6_plen = ifa->ifa_plen;
   2143 				applyplen(&rrt->rrt_info.rip6_dest,
   2144 				    ifa->ifa_plen);
   2145 				category = "network";
   2146 				break;
   2147 			case P2PADVERT_ADDR:
   2148 				rrt->rrt_info.rip6_dest = ifa->ifa_addr;
   2149 				rrt->rrt_info.rip6_plen = 128;
   2150 				rrt->rrt_gw = in6addr_loopback;
   2151 				category = "addr";
   2152 				break;
   2153 			case P2PADVERT_DEST:
   2154 				rrt->rrt_info.rip6_dest = ifa->ifa_raddr;
   2155 				rrt->rrt_info.rip6_plen = 128;
   2156 				rrt->rrt_gw = ifa->ifa_addr;
   2157 				category = "dest";
   2158 				break;
   2159 			}
   2160 			if (IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_info.rip6_dest) ||
   2161 			    IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_info.rip6_dest)) {
   2162 #if 0
   2163 				trace(1, "route: %s: skip unspec/linklocal "
   2164 				    "(%s on %s)\n", category, ifcp->ifc_name);
   2165 #endif
   2166 				free(rrt);
   2167 				continue;
   2168 			}
   2169 			if ((advert & i) == 0) {
   2170 				rrt->rrt_rflags |= RRTF_NOADVERTISE;
   2171 				noadv = ", NO-ADV";
   2172 			} else
   2173 				noadv = "";
   2174 			rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
   2175 			rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
   2176 			np = &rrt->rrt_info;
   2177 			orrt = rtsearch(np, &prevrrt);
   2178 			if (!orrt) {
   2179 				/* Attach the route to the list */
   2180 				trace(1, "route: %s/%d: register route "
   2181 				    "(%s on %s%s)\n",
   2182 				    inet6_n2p(&np->rip6_dest), np->rip6_plen,
   2183 				    category, ifcp->ifc_name, noadv);
   2184 				rrt->rrt_next = riprt;
   2185 				riprt = rrt;
   2186 			} else if (rrt->rrt_index != orrt->rrt_index ||
   2187 			    rrt->rrt_info.rip6_metric != orrt->rrt_info.rip6_metric) {
   2188 				/* swap route */
   2189 				rrt->rrt_next = orrt->rrt_next;
   2190 				if (prevrrt)
   2191 					prevrrt->rrt_next = rrt;
   2192 				else
   2193 					riprt = rrt;
   2194 				free(orrt);
   2195 
   2196 				trace(1, "route: %s/%d: update (%s on %s%s)\n",
   2197 				    inet6_n2p(&np->rip6_dest), np->rip6_plen,
   2198 				    category, ifcp->ifc_name, noadv);
   2199 			} else {
   2200 				/* Already found */
   2201 				if (!again) {
   2202 					trace(1, "route: %s/%d: "
   2203 					    "already registered (%s on %s%s)\n",
   2204 					    inet6_n2p(&np->rip6_dest),
   2205 					    np->rip6_plen, category,
   2206 					    ifcp->ifc_name, noadv);
   2207 				}
   2208 				free(rrt);
   2209 			}
   2210 		}
   2211 	}
   2212 #undef P2PADVERT_NETWORK
   2213 #undef P2PADVERT_ADDR
   2214 #undef P2PADVERT_DEST
   2215 #undef P2PADVERT_MAX
   2216 }
   2217 
   2218 int
   2219 getifmtu(int ifindex)
   2220 {
   2221 	int	mib[6];
   2222 	char	*buf;
   2223 	size_t	msize;
   2224 	struct	if_msghdr *ifm;
   2225 	int	mtu;
   2226 
   2227 	mib[0] = CTL_NET;
   2228 	mib[1] = PF_ROUTE;
   2229 	mib[2] = 0;
   2230 	mib[3] = AF_INET6;
   2231 	mib[4] = NET_RT_IFLIST;
   2232 	mib[5] = ifindex;
   2233 	if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
   2234 		fatal("sysctl estimate NET_RT_IFLIST");
   2235 		/*NOTREACHED*/
   2236 	}
   2237 	if ((buf = malloc(msize)) == NULL) {
   2238 		fatal("malloc");
   2239 		/*NOTREACHED*/
   2240 	}
   2241 	if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
   2242 		fatal("sysctl NET_RT_IFLIST");
   2243 		/*NOTREACHED*/
   2244 	}
   2245 	ifm = (struct if_msghdr *)buf;
   2246 	mtu = ifm->ifm_data.ifi_mtu;
   2247 #ifdef __FreeBSD__
   2248 	if (ifindex != ifm->ifm_index) {
   2249 		fatal("ifindex does not match with ifm_index");
   2250 		/*NOTREACHED*/
   2251 	}
   2252 #endif
   2253 	free(buf);
   2254 	return mtu;
   2255 }
   2256 
   2257 const char *
   2258 rttypes(struct rt_msghdr *rtm)
   2259 {
   2260 #define	RTTYPE(s, f) \
   2261 do { \
   2262 	if (rtm->rtm_type == (f)) \
   2263 		return (s); \
   2264 } while (0)
   2265 	RTTYPE("ADD", RTM_ADD);
   2266 	RTTYPE("DELETE", RTM_DELETE);
   2267 	RTTYPE("CHANGE", RTM_CHANGE);
   2268 	RTTYPE("GET", RTM_GET);
   2269 	RTTYPE("LOSING", RTM_LOSING);
   2270 	RTTYPE("REDIRECT", RTM_REDIRECT);
   2271 	RTTYPE("MISS", RTM_MISS);
   2272 	RTTYPE("LOCK", RTM_LOCK);
   2273 	RTTYPE("OLDADD", RTM_OLDADD);
   2274 	RTTYPE("OLDDEL", RTM_OLDDEL);
   2275 	RTTYPE("RESOLVE", RTM_RESOLVE);
   2276 	RTTYPE("NEWADDR", RTM_NEWADDR);
   2277 	RTTYPE("DELADDR", RTM_DELADDR);
   2278 	RTTYPE("IFINFO", RTM_IFINFO);
   2279 #ifdef RTM_OLDADD
   2280 	RTTYPE("OLDADD", RTM_OLDADD);
   2281 #endif
   2282 #ifdef RTM_OLDDEL
   2283 	RTTYPE("OLDDEL", RTM_OLDDEL);
   2284 #endif
   2285 #ifdef RTM_OIFINFO
   2286 	RTTYPE("OIFINFO", RTM_OIFINFO);
   2287 #endif
   2288 #ifdef RTM_IFANNOUNCE
   2289 	RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE);
   2290 #endif
   2291 #ifdef RTM_NEWMADDR
   2292 	RTTYPE("NEWMADDR", RTM_NEWMADDR);
   2293 #endif
   2294 #ifdef RTM_DELMADDR
   2295 	RTTYPE("DELMADDR", RTM_DELMADDR);
   2296 #endif
   2297 #undef RTTYPE
   2298 	return NULL;
   2299 }
   2300 
   2301 const char *
   2302 rtflags(struct rt_msghdr *rtm)
   2303 {
   2304 	static char buf[BUFSIZ];
   2305 
   2306 	/*
   2307 	 * letter conflict should be okay.  painful when *BSD diverges...
   2308 	 */
   2309 	strlcpy(buf, "", sizeof(buf));
   2310 #define	RTFLAG(s, f) \
   2311 do { \
   2312 	if (rtm->rtm_flags & (f)) \
   2313 		strlcat(buf, (s), sizeof(buf)); \
   2314 } while (0)
   2315 	RTFLAG("U", RTF_UP);
   2316 	RTFLAG("G", RTF_GATEWAY);
   2317 	RTFLAG("H", RTF_HOST);
   2318 	RTFLAG("R", RTF_REJECT);
   2319 	RTFLAG("D", RTF_DYNAMIC);
   2320 	RTFLAG("M", RTF_MODIFIED);
   2321 	RTFLAG("d", RTF_DONE);
   2322 #ifdef	RTF_MASK
   2323 	RTFLAG("m", RTF_MASK);
   2324 #endif
   2325 	RTFLAG("C", RTF_CLONING);
   2326 #ifdef RTF_CLONED
   2327 	RTFLAG("c", RTF_CLONED);
   2328 #endif
   2329 #ifdef RTF_PRCLONING
   2330 	RTFLAG("c", RTF_PRCLONING);
   2331 #endif
   2332 #ifdef RTF_WASCLONED
   2333 	RTFLAG("W", RTF_WASCLONED);
   2334 #endif
   2335 	RTFLAG("X", RTF_XRESOLVE);
   2336 	RTFLAG("L", RTF_LLINFO);
   2337 	RTFLAG("S", RTF_STATIC);
   2338 	RTFLAG("B", RTF_BLACKHOLE);
   2339 #ifdef RTF_PROTO3
   2340 	RTFLAG("3", RTF_PROTO3);
   2341 #endif
   2342 	RTFLAG("2", RTF_PROTO2);
   2343 	RTFLAG("1", RTF_PROTO1);
   2344 #ifdef RTF_BROADCAST
   2345 	RTFLAG("b", RTF_BROADCAST);
   2346 #endif
   2347 #ifdef RTF_DEFAULT
   2348 	RTFLAG("d", RTF_DEFAULT);
   2349 #endif
   2350 #ifdef RTF_ISAROUTER
   2351 	RTFLAG("r", RTF_ISAROUTER);
   2352 #endif
   2353 #ifdef RTF_TUNNEL
   2354 	RTFLAG("T", RTF_TUNNEL);
   2355 #endif
   2356 #ifdef RTF_AUTH
   2357 	RTFLAG("A", RTF_AUTH);
   2358 #endif
   2359 #ifdef RTF_CRYPT
   2360 	RTFLAG("E", RTF_CRYPT);
   2361 #endif
   2362 #undef RTFLAG
   2363 	return buf;
   2364 }
   2365 
   2366 const char *
   2367 ifflags(int flags)
   2368 {
   2369 	static char buf[BUFSIZ];
   2370 
   2371 	strlcpy(buf, "", sizeof(buf));
   2372 #define	IFFLAG(s, f) \
   2373 do { \
   2374 	if (flags & (f)) { \
   2375 		if (buf[0]) \
   2376 			strlcat(buf, ",", sizeof(buf)); \
   2377 		strlcat(buf, (s), sizeof(buf)); \
   2378 	} \
   2379 } while (0)
   2380 	IFFLAG("UP", IFF_UP);
   2381 	IFFLAG("BROADCAST", IFF_BROADCAST);
   2382 	IFFLAG("DEBUG", IFF_DEBUG);
   2383 	IFFLAG("LOOPBACK", IFF_LOOPBACK);
   2384 	IFFLAG("POINTOPOINT", IFF_POINTOPOINT);
   2385 #ifdef IFF_NOTRAILERS
   2386 	IFFLAG("NOTRAILERS", IFF_NOTRAILERS);
   2387 #endif
   2388 #ifdef IFF_SMART
   2389 	IFFLAG("SMART", IFF_SMART);
   2390 #endif
   2391 	IFFLAG("RUNNING", IFF_RUNNING);
   2392 	IFFLAG("NOARP", IFF_NOARP);
   2393 	IFFLAG("PROMISC", IFF_PROMISC);
   2394 	IFFLAG("ALLMULTI", IFF_ALLMULTI);
   2395 	IFFLAG("OACTIVE", IFF_OACTIVE);
   2396 	IFFLAG("SIMPLEX", IFF_SIMPLEX);
   2397 	IFFLAG("LINK0", IFF_LINK0);
   2398 	IFFLAG("LINK1", IFF_LINK1);
   2399 	IFFLAG("LINK2", IFF_LINK2);
   2400 	IFFLAG("MULTICAST", IFF_MULTICAST);
   2401 #undef IFFLAG
   2402 	return buf;
   2403 }
   2404 
   2405 void
   2406 krtread(int again)
   2407 {
   2408 	int mib[6];
   2409 	size_t msize;
   2410 	char *buf, *p, *lim;
   2411 	struct rt_msghdr *rtm;
   2412 	int retry;
   2413 	const char *errmsg;
   2414 
   2415 	retry = 0;
   2416 	buf = NULL;
   2417 	mib[0] = CTL_NET;
   2418 	mib[1] = PF_ROUTE;
   2419 	mib[2] = 0;
   2420 	mib[3] = AF_INET6;	/* Address family */
   2421 	mib[4] = NET_RT_DUMP;	/* Dump the kernel routing table */
   2422 	mib[5] = 0;		/* No flags */
   2423 	do {
   2424 		retry++;
   2425 		errmsg = NULL;
   2426 		if (buf)
   2427 			free(buf);
   2428 		if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
   2429 			errmsg = "sysctl estimate";
   2430 			continue;
   2431 		}
   2432 		if ((buf = malloc(msize)) == NULL) {
   2433 			errmsg = "malloc";
   2434 			continue;
   2435 		}
   2436 		if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
   2437 			errmsg = "sysctl NET_RT_DUMP";
   2438 			continue;
   2439 		}
   2440 	} while (retry < 5 && errmsg != NULL);
   2441 	if (errmsg) {
   2442 		fatal("%s (with %d retries, msize=%lu)", errmsg, retry,
   2443 		    (u_long)msize);
   2444 		/*NOTREACHED*/
   2445 	} else if (1 < retry)
   2446 		syslog(LOG_INFO, "NET_RT_DUMP %d retires", retry);
   2447 
   2448 	lim = buf + msize;
   2449 	for (p = buf; p < lim; p += rtm->rtm_msglen) {
   2450 		rtm = (struct rt_msghdr *)p;
   2451 		rt_entry(rtm, again);
   2452 	}
   2453 	free(buf);
   2454 }
   2455 
   2456 void
   2457 rt_entry(struct rt_msghdr *rtm, int again)
   2458 {
   2459 	struct	sockaddr_in6 *sin6_dst, *sin6_gw, *sin6_mask;
   2460 	struct	sockaddr_in6 *sin6_genmask, *sin6_ifp;
   2461 	char	*rtmp, *ifname = NULL;
   2462 	struct	riprt *rrt, *orrt;
   2463 	struct	netinfo6 *np;
   2464 	int	s;
   2465 
   2466 	sin6_dst = sin6_gw = sin6_mask = sin6_genmask = sin6_ifp = 0;
   2467 	if ((rtm->rtm_flags & RTF_UP) == 0 || rtm->rtm_flags &
   2468 		(RTF_CLONING|RTF_XRESOLVE|RTF_LLINFO|RTF_BLACKHOLE)) {
   2469 		return;		/* not interested in the link route */
   2470 	}
   2471 	/* do not look at cloned routes */
   2472 #ifdef RTF_WASCLONED
   2473 	if (rtm->rtm_flags & RTF_WASCLONED)
   2474 		return;
   2475 #endif
   2476 #ifdef RTF_CLONED
   2477 	if (rtm->rtm_flags & RTF_CLONED)
   2478 		return;
   2479 #endif
   2480 	/*
   2481 	 * do not look at dynamic routes.
   2482 	 * netbsd/openbsd cloned routes have UGHD.
   2483 	 */
   2484 	if (rtm->rtm_flags & RTF_DYNAMIC)
   2485 		return;
   2486 	rtmp = (char *)(rtm + 1);
   2487 	/* Destination */
   2488 	if ((rtm->rtm_addrs & RTA_DST) == 0)
   2489 		return;		/* ignore routes without destination address */
   2490 	sin6_dst = (struct sockaddr_in6 *)rtmp;
   2491 	rtmp += ROUNDUP(sin6_dst->sin6_len);
   2492 	if (rtm->rtm_addrs & RTA_GATEWAY) {
   2493 		sin6_gw = (struct sockaddr_in6 *)rtmp;
   2494 		rtmp += ROUNDUP(sin6_gw->sin6_len);
   2495 	}
   2496 	if (rtm->rtm_addrs & RTA_NETMASK) {
   2497 		sin6_mask = (struct sockaddr_in6 *)rtmp;
   2498 		rtmp += ROUNDUP(sin6_mask->sin6_len);
   2499 	}
   2500 	if (rtm->rtm_addrs & RTA_GENMASK) {
   2501 		sin6_genmask = (struct sockaddr_in6 *)rtmp;
   2502 		rtmp += ROUNDUP(sin6_genmask->sin6_len);
   2503 	}
   2504 	if (rtm->rtm_addrs & RTA_IFP) {
   2505 		sin6_ifp = (struct sockaddr_in6 *)rtmp;
   2506 		rtmp += ROUNDUP(sin6_ifp->sin6_len);
   2507 	}
   2508 
   2509 	/* Destination */
   2510 	if (sin6_dst->sin6_family != AF_INET6)
   2511 		return;
   2512 	if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst->sin6_addr))
   2513 		return;		/* Link-local */
   2514 	if (IN6_ARE_ADDR_EQUAL(&sin6_dst->sin6_addr, &in6addr_loopback))
   2515 		return;		/* Loopback */
   2516 	if (IN6_IS_ADDR_MULTICAST(&sin6_dst->sin6_addr))
   2517 		return;
   2518 
   2519 	if ((rrt = MALLOC(struct riprt)) == NULL) {
   2520 		fatal("malloc: struct riprt");
   2521 		/*NOTREACHED*/
   2522 	}
   2523 	memset(rrt, 0, sizeof(*rrt));
   2524 	np = &rrt->rrt_info;
   2525 	rrt->rrt_same = NULL;
   2526 	rrt->rrt_t = time(NULL);
   2527 	if (aflag == 0 && (rtm->rtm_flags & RTF_STATIC))
   2528 		rrt->rrt_t = 0;	/* Don't age static routes */
   2529 	if ((rtm->rtm_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST)
   2530 		rrt->rrt_t = 0;	/* Don't age non-gateway host routes */
   2531 	np->rip6_tag = 0;
   2532 	np->rip6_metric = rtm->rtm_rmx.rmx_hopcount;
   2533 	if (np->rip6_metric < 1)
   2534 		np->rip6_metric = 1;
   2535 	rrt->rrt_flags = rtm->rtm_flags;
   2536 	np->rip6_dest = sin6_dst->sin6_addr;
   2537 
   2538 	/* Mask or plen */
   2539 	if (rtm->rtm_flags & RTF_HOST)
   2540 		np->rip6_plen = 128;	/* Host route */
   2541 	else if (sin6_mask)
   2542 		np->rip6_plen = sin6mask2len(sin6_mask);
   2543 	else
   2544 		np->rip6_plen = 0;
   2545 
   2546 	orrt = rtsearch(np, NULL);
   2547 	if (orrt && orrt->rrt_info.rip6_metric != HOPCNT_INFINITY6) {
   2548 		/* Already found */
   2549 		if (!again) {
   2550 			trace(1, "route: %s/%d flags %s: already registered\n",
   2551 				inet6_n2p(&np->rip6_dest), np->rip6_plen,
   2552 				rtflags(rtm));
   2553 		}
   2554 		free(rrt);
   2555 		return;
   2556 	}
   2557 	/* Gateway */
   2558 	if (!sin6_gw)
   2559 		memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
   2560 	else {
   2561 		if (sin6_gw->sin6_family == AF_INET6)
   2562 			rrt->rrt_gw = sin6_gw->sin6_addr;
   2563 		else if (sin6_gw->sin6_family == AF_LINK) {
   2564 			/* XXX in case ppp link? */
   2565 			rrt->rrt_gw = in6addr_loopback;
   2566 		} else
   2567 			memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
   2568 	}
   2569 	trace(1, "route: %s/%d flags %s",
   2570 		inet6_n2p(&np->rip6_dest), np->rip6_plen, rtflags(rtm));
   2571 	trace(1, " gw %s", inet6_n2p(&rrt->rrt_gw));
   2572 
   2573 	/* Interface */
   2574 	s = rtm->rtm_index;
   2575 	if (s < nindex2ifc && index2ifc[s])
   2576 		ifname = index2ifc[s]->ifc_name;
   2577 	else {
   2578 		trace(1, " not configured\n");
   2579 		free(rrt);
   2580 		return;
   2581 	}
   2582 	trace(1, " if %s sock %d", ifname, s);
   2583 	rrt->rrt_index = s;
   2584 
   2585 	trace(1, "\n");
   2586 
   2587 	/* Check gateway */
   2588 	if (!IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_gw) &&
   2589 	    !IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)
   2590 #ifdef __FreeBSD__
   2591 	 && (rrt->rrt_flags & RTF_LOCAL) == 0
   2592 #endif
   2593 	    ) {
   2594 		trace(0, "***** Gateway %s is not a link-local address.\n",
   2595 			inet6_n2p(&rrt->rrt_gw));
   2596 		trace(0, "*****     dest(%s) if(%s) -- Not optimized.\n",
   2597 			inet6_n2p(&rrt->rrt_info.rip6_dest), ifname);
   2598 		rrt->rrt_rflags |= RRTF_NH_NOT_LLADDR;
   2599 	}
   2600 
   2601 	/* Put it to the route list */
   2602 	if (orrt && orrt->rrt_info.rip6_metric == HOPCNT_INFINITY6) {
   2603 		/* replace route list */
   2604 		rrt->rrt_next = orrt->rrt_next;
   2605 		*orrt = *rrt;
   2606 		trace(1, "route: %s/%d flags %s: replace new route\n",
   2607 		    inet6_n2p(&np->rip6_dest), np->rip6_plen,
   2608 		    rtflags(rtm));
   2609 		free(rrt);
   2610 	} else {
   2611 		rrt->rrt_next = riprt;
   2612 		riprt = rrt;
   2613 	}
   2614 }
   2615 
   2616 int
   2617 addroute(struct riprt *rrt, const struct in6_addr *gw, struct ifc *ifcp)
   2618 {
   2619 	struct	netinfo6 *np;
   2620 	u_char	buf[BUFSIZ], buf1[BUFSIZ], buf2[BUFSIZ];
   2621 	struct	rt_msghdr	*rtm;
   2622 	struct	sockaddr_in6	*sin6;
   2623 	int	len;
   2624 
   2625 	np = &rrt->rrt_info;
   2626 	inet_ntop(AF_INET6, (const void *)gw, (char *)buf1, sizeof(buf1));
   2627 	inet_ntop(AF_INET6, (void *)&ifcp->ifc_mylladdr, (char *)buf2, sizeof(buf2));
   2628 	tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n",
   2629 		inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
   2630 		np->rip6_metric - 1, buf2);
   2631 	if (rtlog)
   2632 		fprintf(rtlog, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(),
   2633 			inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
   2634 			np->rip6_metric - 1, buf2);
   2635 	if (nflag)
   2636 		return 0;
   2637 
   2638 	memset(buf, 0, sizeof(buf));
   2639 	rtm = (struct rt_msghdr *)buf;
   2640 	rtm->rtm_type = RTM_ADD;
   2641 	rtm->rtm_version = RTM_VERSION;
   2642 	rtm->rtm_seq = ++seq;
   2643 	rtm->rtm_pid = pid;
   2644 	rtm->rtm_flags = rrt->rrt_flags;
   2645 	rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
   2646 	rtm->rtm_rmx.rmx_hopcount = np->rip6_metric - 1;
   2647 	rtm->rtm_inits = RTV_HOPCOUNT;
   2648 	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
   2649 	/* Destination */
   2650 	sin6->sin6_len = sizeof(struct sockaddr_in6);
   2651 	sin6->sin6_family = AF_INET6;
   2652 	sin6->sin6_addr = np->rip6_dest;
   2653 	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
   2654 	/* Gateway */
   2655 	sin6->sin6_len = sizeof(struct sockaddr_in6);
   2656 	sin6->sin6_family = AF_INET6;
   2657 	sin6->sin6_addr = *gw;
   2658 	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
   2659 	/* Netmask */
   2660 	sin6->sin6_len = sizeof(struct sockaddr_in6);
   2661 	sin6->sin6_family = AF_INET6;
   2662 	sin6->sin6_addr = *(plen2mask(np->rip6_plen));
   2663 	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
   2664 
   2665 	len = (char *)sin6 - (char *)buf;
   2666 	rtm->rtm_msglen = len;
   2667 	if (write(rtsock, buf, len) > 0)
   2668 		return 0;
   2669 
   2670 	if (errno == EEXIST) {
   2671 		trace(0, "ADD: Route already exists %s/%d gw %s\n",
   2672 		    inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
   2673 		if (rtlog)
   2674 			fprintf(rtlog, "ADD: Route already exists %s/%d gw %s\n",
   2675 			    inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
   2676 	} else {
   2677 		trace(0, "Can not write to rtsock (addroute): %s\n",
   2678 		    strerror(errno));
   2679 		if (rtlog)
   2680 			fprintf(rtlog, "\tCan not write to rtsock: %s\n",
   2681 			    strerror(errno));
   2682 	}
   2683 	return -1;
   2684 }
   2685 
   2686 int
   2687 delroute(struct netinfo6 *np, struct in6_addr *gw)
   2688 {
   2689 	u_char	buf[BUFSIZ], buf2[BUFSIZ];
   2690 	struct	rt_msghdr	*rtm;
   2691 	struct	sockaddr_in6	*sin6;
   2692 	int	len;
   2693 
   2694 	inet_ntop(AF_INET6, (void *)gw, (char *)buf2, sizeof(buf2));
   2695 	tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np->rip6_dest),
   2696 		np->rip6_plen, buf2);
   2697 	if (rtlog)
   2698 		fprintf(rtlog, "%s: DEL: %s/%d gw %s\n",
   2699 			hms(), inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
   2700 	if (nflag)
   2701 		return 0;
   2702 
   2703 	memset(buf, 0, sizeof(buf));
   2704 	rtm = (struct rt_msghdr *)buf;
   2705 	rtm->rtm_type = RTM_DELETE;
   2706 	rtm->rtm_version = RTM_VERSION;
   2707 	rtm->rtm_seq = ++seq;
   2708 	rtm->rtm_pid = pid;
   2709 	rtm->rtm_flags = RTF_UP | RTF_GATEWAY;
   2710 	if (np->rip6_plen == sizeof(struct in6_addr) * 8)
   2711 		rtm->rtm_flags |= RTF_HOST;
   2712 	rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
   2713 	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
   2714 	/* Destination */
   2715 	sin6->sin6_len = sizeof(struct sockaddr_in6);
   2716 	sin6->sin6_family = AF_INET6;
   2717 	sin6->sin6_addr = np->rip6_dest;
   2718 	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
   2719 	/* Gateway */
   2720 	sin6->sin6_len = sizeof(struct sockaddr_in6);
   2721 	sin6->sin6_family = AF_INET6;
   2722 	sin6->sin6_addr = *gw;
   2723 	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
   2724 	/* Netmask */
   2725 	sin6->sin6_len = sizeof(struct sockaddr_in6);
   2726 	sin6->sin6_family = AF_INET6;
   2727 	sin6->sin6_addr = *(plen2mask(np->rip6_plen));
   2728 	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
   2729 
   2730 	len = (char *)sin6 - (char *)buf;
   2731 	rtm->rtm_msglen = len;
   2732 	if (write(rtsock, buf, len) >= 0)
   2733 		return 0;
   2734 
   2735 	if (errno == ESRCH) {
   2736 		trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n",
   2737 		    inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
   2738 		if (rtlog)
   2739 			fprintf(rtlog, "RTDEL: Route does not exist: %s/%d gw %s\n",
   2740 			    inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
   2741 	} else {
   2742 		trace(0, "Can not write to rtsock (delroute): %s\n",
   2743 		    strerror(errno));
   2744 		if (rtlog)
   2745 			fprintf(rtlog, "\tCan not write to rtsock: %s\n",
   2746 			    strerror(errno));
   2747 	}
   2748 	return -1;
   2749 }
   2750 
   2751 struct in6_addr *
   2752 getroute(struct netinfo6 *np, struct in6_addr *gw)
   2753 {
   2754 	u_char buf[BUFSIZ];
   2755 	int myseq;
   2756 	int len;
   2757 	struct rt_msghdr *rtm;
   2758 	struct sockaddr_in6 *sin6;
   2759 
   2760 	rtm = (struct rt_msghdr *)buf;
   2761 	len = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in6);
   2762 	memset(rtm, 0, len);
   2763 	rtm->rtm_type = RTM_GET;
   2764 	rtm->rtm_version = RTM_VERSION;
   2765 	myseq = ++seq;
   2766 	rtm->rtm_seq = myseq;
   2767 	rtm->rtm_addrs = RTA_DST;
   2768 	rtm->rtm_msglen = len;
   2769 	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
   2770 	sin6->sin6_len = sizeof(struct sockaddr_in6);
   2771 	sin6->sin6_family = AF_INET6;
   2772 	sin6->sin6_addr = np->rip6_dest;
   2773 	if (write(rtsock, buf, len) < 0) {
   2774 		if (errno == ESRCH)	/* No such route found */
   2775 			return NULL;
   2776 		perror("write to rtsock");
   2777 		exit(1);
   2778 	}
   2779 	do {
   2780 		if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
   2781 			perror("read from rtsock");
   2782 			exit(1);
   2783 		}
   2784 		rtm = (struct rt_msghdr *)buf;
   2785 	} while (rtm->rtm_seq != myseq || rtm->rtm_pid != pid);
   2786 	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
   2787 	if (rtm->rtm_addrs & RTA_DST) {
   2788 		sin6 = (struct sockaddr_in6 *)
   2789 			((char *)sin6 + ROUNDUP(sin6->sin6_len));
   2790 	}
   2791 	if (rtm->rtm_addrs & RTA_GATEWAY) {
   2792 		*gw = sin6->sin6_addr;
   2793 		return gw;
   2794 	}
   2795 	return NULL;
   2796 }
   2797 
   2798 const char *
   2799 inet6_n2p(const struct in6_addr *p)
   2800 {
   2801 	static char buf[BUFSIZ];
   2802 
   2803 	return inet_ntop(AF_INET6, (const void *)p, buf, sizeof(buf));
   2804 }
   2805 
   2806 void
   2807 ifrtdump(int sig)
   2808 {
   2809 
   2810 	ifdump(sig);
   2811 	rtdump(sig);
   2812 }
   2813 
   2814 void
   2815 ifdump(int sig)
   2816 {
   2817 	struct ifc *ifcp;
   2818 	FILE *dump;
   2819 	int i;
   2820 
   2821 	if (sig == 0)
   2822 		dump = stderr;
   2823 	else
   2824 		if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
   2825 			dump = stderr;
   2826 
   2827 	fprintf(dump, "%s: Interface Table Dump\n", hms());
   2828 	fprintf(dump, "  Number of interfaces: %d\n", nifc);
   2829 	for (i = 0; i < 2; i++) {
   2830 		fprintf(dump, "  %sadvertising interfaces:\n", i ? "non-" : "");
   2831 		for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
   2832 			if (i == 0) {
   2833 				if ((ifcp->ifc_flags & IFF_UP) == 0)
   2834 					continue;
   2835 				if (iff_find(ifcp, 'N') != NULL)
   2836 					continue;
   2837 			} else {
   2838 				if (ifcp->ifc_flags & IFF_UP)
   2839 					continue;
   2840 			}
   2841 			ifdump0(dump, ifcp);
   2842 		}
   2843 	}
   2844 	fprintf(dump, "\n");
   2845 	if (dump != stderr)
   2846 		fclose(dump);
   2847 }
   2848 
   2849 void
   2850 ifdump0(FILE *dump, const struct ifc *ifcp)
   2851 {
   2852 	struct ifac *ifa;
   2853 	struct iff *iffp;
   2854 	char buf[BUFSIZ];
   2855 	const char *ft;
   2856 	int addr;
   2857 
   2858 	fprintf(dump, "    %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n",
   2859 		ifcp->ifc_name, ifcp->ifc_index, ifflags(ifcp->ifc_flags),
   2860 		inet6_n2p(&ifcp->ifc_mylladdr),
   2861 		ifcp->ifc_mtu, ifcp->ifc_metric);
   2862 	for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
   2863 		if (ifcp->ifc_flags & IFF_POINTOPOINT) {
   2864 			inet_ntop(AF_INET6, (void *)&ifa->ifa_raddr,
   2865 				buf, sizeof(buf));
   2866 			fprintf(dump, "\t%s/%d -- %s\n",
   2867 				inet6_n2p(&ifa->ifa_addr),
   2868 				ifa->ifa_plen, buf);
   2869 		} else {
   2870 			fprintf(dump, "\t%s/%d\n",
   2871 				inet6_n2p(&ifa->ifa_addr),
   2872 				ifa->ifa_plen);
   2873 		}
   2874 	}
   2875 	if (ifcp->ifc_filter) {
   2876 		fprintf(dump, "\tFilter:");
   2877 		for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
   2878 			addr = 0;
   2879 			switch (iffp->iff_type) {
   2880 			case 'A':
   2881 				ft = "Aggregate"; addr++; break;
   2882 			case 'N':
   2883 				ft = "No-use"; break;
   2884 			case 'O':
   2885 				ft = "Advertise-only"; addr++; break;
   2886 			case 'T':
   2887 				ft = "Default-only"; break;
   2888 			case 'L':
   2889 				ft = "Listen-only"; addr++; break;
   2890 			default:
   2891 				snprintf(buf, sizeof(buf), "Unknown-%c", iffp->iff_type);
   2892 				ft = buf;
   2893 				addr++;
   2894 				break;
   2895 			}
   2896 			fprintf(dump, " %s", ft);
   2897 			if (addr) {
   2898 				fprintf(dump, "(%s/%d)", inet6_n2p(&iffp->iff_addr),
   2899 					iffp->iff_plen);
   2900 			}
   2901 		}
   2902 		fprintf(dump, "\n");
   2903 	}
   2904 }
   2905 
   2906 void
   2907 rtdump(int sig)
   2908 {
   2909 	struct	riprt *rrt;
   2910 	char	buf[BUFSIZ];
   2911 	FILE	*dump;
   2912 	time_t	t, age;
   2913 
   2914 	if (sig == 0)
   2915 		dump = stderr;
   2916 	else
   2917 		if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
   2918 			dump = stderr;
   2919 
   2920 	t = time(NULL);
   2921 	fprintf(dump, "\n%s: Routing Table Dump\n", hms());
   2922 	for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
   2923 		if (rrt->rrt_t == 0)
   2924 			age = 0;
   2925 		else
   2926 			age = t - rrt->rrt_t;
   2927 		inet_ntop(AF_INET6, (void *)&rrt->rrt_info.rip6_dest,
   2928 			buf, sizeof(buf));
   2929 		fprintf(dump, "    %s/%d if(%d:%s) gw(%s) [%d] age(%ld)",
   2930 			buf, rrt->rrt_info.rip6_plen, rrt->rrt_index,
   2931 			index2ifc[rrt->rrt_index]->ifc_name,
   2932 			inet6_n2p(&rrt->rrt_gw),
   2933 			rrt->rrt_info.rip6_metric, (long)age);
   2934 		if (rrt->rrt_info.rip6_tag) {
   2935 			fprintf(dump, " tag(0x%04x)",
   2936 				ntohs(rrt->rrt_info.rip6_tag) & 0xffff);
   2937 		}
   2938 		if (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)
   2939 			fprintf(dump, " NOT-LL");
   2940 		if (rrt->rrt_rflags & RRTF_NOADVERTISE)
   2941 			fprintf(dump, " NO-ADV");
   2942 		fprintf(dump, "\n");
   2943 	}
   2944 	fprintf(dump, "\n");
   2945 	if (dump != stderr)
   2946 		fclose(dump);
   2947 }
   2948 
   2949 /*
   2950  * Parse the -A (and -O) options and put corresponding filter object to the
   2951  * specified interface structures.  Each of the -A/O option has the following
   2952  * syntax:	-A 5f09:c400::/32,ef0,ef1  (aggregate)
   2953  * 		-O 5f09:c400::/32,ef0,ef1  (only when match)
   2954  */
   2955 void
   2956 filterconfig(void)
   2957 {
   2958 	int i;
   2959 	char *p, *ap, *iflp, *ifname, *ep;
   2960 	struct iff ftmp, *iff_obj;
   2961 	struct ifc *ifcp;
   2962 	struct riprt *rrt;
   2963 #if 0
   2964 	struct in6_addr gw;
   2965 #endif
   2966 	u_long plen;
   2967 
   2968 	for (i = 0; i < nfilter; i++) {
   2969 		ap = filter[i];
   2970 		iflp = NULL;
   2971 		ifcp = NULL;
   2972 		if (filtertype[i] == 'N' || filtertype[i] == 'T') {
   2973 			iflp = ap;
   2974 			goto ifonly;
   2975 		}
   2976 		if ((p = strchr(ap, ',')) != NULL) {
   2977 			*p++ = '\0';
   2978 			iflp = p;
   2979 		}
   2980 		if ((p = strchr(ap, '/')) == NULL) {
   2981 			fatal("no prefixlen specified for '%s'", ap);
   2982 			/*NOTREACHED*/
   2983 		}
   2984 		*p++ = '\0';
   2985 		if (inet_pton(AF_INET6, ap, &ftmp.iff_addr) != 1) {
   2986 			fatal("invalid prefix specified for '%s'", ap);
   2987 			/*NOTREACHED*/
   2988 		}
   2989 		errno = 0;
   2990 		ep = NULL;
   2991 		plen = strtoul(p, &ep, 10);
   2992 		if (errno || !*p || *ep || plen > sizeof(ftmp.iff_addr) * 8) {
   2993 			fatal("invalid prefix length specified for '%s'", ap);
   2994 			/*NOTREACHED*/
   2995 		}
   2996 		ftmp.iff_plen = plen;
   2997 		ftmp.iff_next = NULL;
   2998 		applyplen(&ftmp.iff_addr, ftmp.iff_plen);
   2999 ifonly:
   3000 		ftmp.iff_type = filtertype[i];
   3001 		if (iflp == NULL || *iflp == '\0') {
   3002 			fatal("no interface specified for '%s'", ap);
   3003 			/*NOTREACHED*/
   3004 		}
   3005 		/* parse the interface listing portion */
   3006 		while (iflp) {
   3007 			ifname = iflp;
   3008 			if ((iflp = strchr(iflp, ',')) != NULL)
   3009 				*iflp++ = '\0';
   3010 			ifcp = ifc_find(ifname);
   3011 			if (ifcp == NULL) {
   3012 				fatal("no interface %s exists", ifname);
   3013 				/*NOTREACHED*/
   3014 			}
   3015 			iff_obj = (struct iff *)malloc(sizeof(struct iff));
   3016 			if (iff_obj == NULL) {
   3017 				fatal("malloc of iff_obj");
   3018 				/*NOTREACHED*/
   3019 			}
   3020 			memcpy((void *)iff_obj, (void *)&ftmp,
   3021 			    sizeof(struct iff));
   3022 			/* link it to the interface filter */
   3023 			iff_obj->iff_next = ifcp->ifc_filter;
   3024 			ifcp->ifc_filter = iff_obj;
   3025 		}
   3026 
   3027 		/*
   3028 		 * -A: aggregate configuration.
   3029 		 */
   3030 		if (filtertype[i] != 'A')
   3031 			continue;
   3032 		/* put the aggregate to the kernel routing table */
   3033 		rrt = (struct riprt *)malloc(sizeof(struct riprt));
   3034 		if (rrt == NULL) {
   3035 			fatal("malloc: rrt");
   3036 			/*NOTREACHED*/
   3037 		}
   3038 		memset(rrt, 0, sizeof(struct riprt));
   3039 		rrt->rrt_info.rip6_dest = ftmp.iff_addr;
   3040 		rrt->rrt_info.rip6_plen = ftmp.iff_plen;
   3041 		rrt->rrt_info.rip6_metric = 1;
   3042 		rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
   3043 		rrt->rrt_gw = in6addr_loopback;
   3044 		rrt->rrt_flags = RTF_UP | RTF_REJECT;
   3045 		rrt->rrt_rflags = RRTF_AGGREGATE;
   3046 		rrt->rrt_t = 0;
   3047 		rrt->rrt_index = loopifcp->ifc_index;
   3048 #if 0
   3049 		if (getroute(&rrt->rrt_info, &gw)) {
   3050 #if 0
   3051 			/*
   3052 			 * When the address has already been registered in the
   3053 			 * kernel routing table, it should be removed
   3054 			 */
   3055 			delroute(&rrt->rrt_info, &gw);
   3056 #else
   3057 			/* it is safer behavior */
   3058 			errno = EINVAL;
   3059 			fatal("%s/%u already in routing table, "
   3060 			    "cannot aggregate",
   3061 			    inet6_n2p(&rrt->rrt_info.rip6_dest),
   3062 			    rrt->rrt_info.rip6_plen);
   3063 			/*NOTREACHED*/
   3064 #endif
   3065 		}
   3066 #endif
   3067 		/* Put the route to the list */
   3068 		rrt->rrt_next = riprt;
   3069 		riprt = rrt;
   3070 		trace(1, "Aggregate: %s/%d for %s\n",
   3071 			inet6_n2p(&ftmp.iff_addr), ftmp.iff_plen,
   3072 			ifcp->ifc_name);
   3073 		/* Add this route to the kernel */
   3074 		if (nflag) 	/* do not modify kernel routing table */
   3075 			continue;
   3076 		addroute(rrt, &in6addr_loopback, loopifcp);
   3077 	}
   3078 }
   3079 
   3080 /***************** utility functions *****************/
   3081 
   3082 /*
   3083  * Returns a pointer to ifac whose address and prefix length matches
   3084  * with the address and prefix length specified in the arguments.
   3085  */
   3086 struct ifac *
   3087 ifa_match(const struct ifc *ifcp, const struct in6_addr *ia, int plen)
   3088 {
   3089 	struct ifac *ifa;
   3090 
   3091 	for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
   3092 		if (IN6_ARE_ADDR_EQUAL(&ifa->ifa_addr, ia) &&
   3093 		    ifa->ifa_plen == plen)
   3094 			break;
   3095 	}
   3096 	return ifa;
   3097 }
   3098 
   3099 /*
   3100  * Return a pointer to riprt structure whose address and prefix length
   3101  * matches with the address and prefix length found in the argument.
   3102  * Note: This is not a rtalloc().  Therefore exact match is necessary.
   3103  */
   3104 struct riprt *
   3105 rtsearch(struct netinfo6 *np, struct riprt **prev_rrt)
   3106 {
   3107 	struct	riprt	*rrt;
   3108 
   3109 	if (prev_rrt)
   3110 		*prev_rrt = NULL;
   3111 	for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
   3112 		if (rrt->rrt_info.rip6_plen == np->rip6_plen &&
   3113 		    IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
   3114 				       &np->rip6_dest))
   3115 			return rrt;
   3116 		if (prev_rrt)
   3117 			*prev_rrt = rrt;
   3118 	}
   3119 	if (prev_rrt)
   3120 		*prev_rrt = NULL;
   3121 	return 0;
   3122 }
   3123 
   3124 int
   3125 sin6mask2len(const struct sockaddr_in6 *sin6)
   3126 {
   3127 
   3128 	return mask2len(&sin6->sin6_addr,
   3129 	    sin6->sin6_len - offsetof(struct sockaddr_in6, sin6_addr));
   3130 }
   3131 
   3132 int
   3133 mask2len(const struct in6_addr *addr, int lenlim)
   3134 {
   3135 	int i = 0, j;
   3136 	const u_char *p = (const u_char *)addr;
   3137 
   3138 	for (j = 0; j < lenlim; j++, p++) {
   3139 		if (*p != 0xff)
   3140 			break;
   3141 		i += 8;
   3142 	}
   3143 	if (j < lenlim) {
   3144 		switch (*p) {
   3145 #define	MASKLEN(m, l)	case m: do { i += l; break; } while (0)
   3146 		MASKLEN(0xfe, 7); break;
   3147 		MASKLEN(0xfc, 6); break;
   3148 		MASKLEN(0xf8, 5); break;
   3149 		MASKLEN(0xf0, 4); break;
   3150 		MASKLEN(0xe0, 3); break;
   3151 		MASKLEN(0xc0, 2); break;
   3152 		MASKLEN(0x80, 1); break;
   3153 #undef	MASKLEN
   3154 		}
   3155 	}
   3156 	return i;
   3157 }
   3158 
   3159 void
   3160 applymask(struct in6_addr *addr, struct in6_addr *mask)
   3161 {
   3162 	int	i;
   3163 	u_long	*p, *q;
   3164 
   3165 	p = (u_long *)addr; q = (u_long *)mask;
   3166 	for (i = 0; i < 4; i++)
   3167 		*p++ &= *q++;
   3168 }
   3169 
   3170 static const u_char plent[8] = {
   3171 	0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
   3172 };
   3173 
   3174 void
   3175 applyplen(struct in6_addr *ia, int plen)
   3176 {
   3177 	u_char	*p;
   3178 	int	i;
   3179 
   3180 	p = ia->s6_addr;
   3181 	for (i = 0; i < 16; i++) {
   3182 		if (plen <= 0)
   3183 			*p = 0;
   3184 		else if (plen < 8)
   3185 			*p &= plent[plen];
   3186 		p++, plen -= 8;
   3187 	}
   3188 }
   3189 
   3190 static const int pl2m[9] = {
   3191 	0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
   3192 };
   3193 
   3194 struct in6_addr *
   3195 plen2mask(int n)
   3196 {
   3197 	static struct in6_addr ia;
   3198 	u_char	*p;
   3199 	int	i;
   3200 
   3201 	memset(&ia, 0, sizeof(struct in6_addr));
   3202 	p = (u_char *)&ia;
   3203 	for (i = 0; i < 16; i++, p++, n -= 8) {
   3204 		if (n >= 8) {
   3205 			*p = 0xff;
   3206 			continue;
   3207 		}
   3208 		*p = pl2m[n];
   3209 		break;
   3210 	}
   3211 	return &ia;
   3212 }
   3213 
   3214 char *
   3215 allocopy(char *p)
   3216 {
   3217 	int len = strlen(p) + 1;
   3218 	char *q = (char *)malloc(len);
   3219 
   3220 	if (!q) {
   3221 		fatal("malloc");
   3222 		/*NOTREACHED*/
   3223 	}
   3224 
   3225 	strlcpy(q, p, len);
   3226 	return q;
   3227 }
   3228 
   3229 char *
   3230 hms(void)
   3231 {
   3232 	static char buf[BUFSIZ];
   3233 	time_t t;
   3234 	struct	tm *tm;
   3235 
   3236 	t = time(NULL);
   3237 	if ((tm = localtime(&t)) == 0) {
   3238 		fatal("localtime");
   3239 		/*NOTREACHED*/
   3240 	}
   3241 	snprintf(buf, sizeof(buf), "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
   3242 	    tm->tm_sec);
   3243 	return buf;
   3244 }
   3245 
   3246 #define	RIPRANDDEV	1.0	/* 30 +- 15, max - min = 30 */
   3247 
   3248 int
   3249 ripinterval(int timer)
   3250 {
   3251 	double r = rand();
   3252 
   3253 	interval = (int)(timer + timer * RIPRANDDEV * (r / RAND_MAX - 0.5));
   3254 	nextalarm = time(NULL) + interval;
   3255 	return interval;
   3256 }
   3257 
   3258 time_t
   3259 ripsuptrig(void)
   3260 {
   3261 	time_t t;
   3262 
   3263 	double r = rand();
   3264 	t  = (int)(RIP_TRIG_INT6_MIN +
   3265 		(RIP_TRIG_INT6_MAX - RIP_TRIG_INT6_MIN) * (r / RAND_MAX));
   3266 	sup_trig_update = time(NULL) + t;
   3267 	return t;
   3268 }
   3269 
   3270 void
   3271 fatal(const char *fmt, ...)
   3272 {
   3273 	va_list ap;
   3274 	char buf[1024];
   3275 
   3276 	va_start(ap, fmt);
   3277 	vsnprintf(buf, sizeof(buf), fmt, ap);
   3278 	va_end(ap);
   3279 	perror(buf);
   3280 	if (errno)
   3281 		syslog(LOG_ERR, "%s: %s", buf, strerror(errno));
   3282 	else
   3283 		syslog(LOG_ERR, "%s", buf);
   3284 	rtdexit();
   3285 }
   3286 
   3287 void
   3288 tracet(int level, const char *fmt, ...)
   3289 {
   3290 	va_list ap;
   3291 
   3292 	if (level <= dflag) {
   3293 		va_start(ap, fmt);
   3294 		fprintf(stderr, "%s: ", hms());
   3295 		vfprintf(stderr, fmt, ap);
   3296 		va_end(ap);
   3297 	}
   3298 	if (dflag) {
   3299 		va_start(ap, fmt);
   3300 		if (level > 0)
   3301 			vsyslog(LOG_DEBUG, fmt, ap);
   3302 		else
   3303 			vsyslog(LOG_WARNING, fmt, ap);
   3304 		va_end(ap);
   3305 	}
   3306 }
   3307 
   3308 void
   3309 trace(int level, const char *fmt, ...)
   3310 {
   3311 	va_list ap;
   3312 
   3313 	if (level <= dflag) {
   3314 		va_start(ap, fmt);
   3315 		vfprintf(stderr, fmt, ap);
   3316 		va_end(ap);
   3317 	}
   3318 	if (dflag) {
   3319 		va_start(ap, fmt);
   3320 		if (level > 0)
   3321 			vsyslog(LOG_DEBUG, fmt, ap);
   3322 		else
   3323 			vsyslog(LOG_WARNING, fmt, ap);
   3324 		va_end(ap);
   3325 	}
   3326 }
   3327 
   3328 unsigned int
   3329 if_maxindex(void)
   3330 {
   3331 	struct if_nameindex *p, *p0;
   3332 	unsigned int max = 0;
   3333 
   3334 	p0 = if_nameindex();
   3335 	for (p = p0; p && p->if_index && p->if_name; p++) {
   3336 		if (max < p->if_index)
   3337 			max = p->if_index;
   3338 	}
   3339 	if_freenameindex(p0);
   3340 	return max;
   3341 }
   3342 
   3343 struct ifc *
   3344 ifc_find(char *name)
   3345 {
   3346 	struct ifc *ifcp;
   3347 
   3348 	for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
   3349 		if (strcmp(name, ifcp->ifc_name) == 0)
   3350 			return ifcp;
   3351 	}
   3352 	return (struct ifc *)NULL;
   3353 }
   3354 
   3355 struct iff *
   3356 iff_find(struct ifc *ifcp, int type)
   3357 {
   3358 	struct iff *iffp;
   3359 
   3360 	for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
   3361 		if (iffp->iff_type == type)
   3362 			return iffp;
   3363 	}
   3364 	return NULL;
   3365 }
   3366 
   3367 void
   3368 setindex2ifc(int idx, struct ifc *ifcp)
   3369 {
   3370 	int n, nsize;
   3371 	struct ifc **p;
   3372 
   3373 	if (!index2ifc) {
   3374 		nindex2ifc = 5;	/*initial guess*/
   3375 		index2ifc = (struct ifc **)
   3376 			malloc(sizeof(*index2ifc) * nindex2ifc);
   3377 		if (index2ifc == NULL) {
   3378 			fatal("malloc");
   3379 			/*NOTREACHED*/
   3380 		}
   3381 		memset(index2ifc, 0, sizeof(*index2ifc) * nindex2ifc);
   3382 	}
   3383 	n = nindex2ifc;
   3384 	for (nsize = nindex2ifc; nsize <= idx; nsize *= 2)
   3385 		;
   3386 	if (n != nsize) {
   3387 		p = (struct ifc **)realloc(index2ifc,
   3388 		    sizeof(*index2ifc) * nsize);
   3389 		if (p == NULL) {
   3390 			fatal("realloc");
   3391 			/*NOTREACHED*/
   3392 		}
   3393 		memset(p + n, 0, sizeof(*index2ifc) * (nindex2ifc - n));
   3394 		index2ifc = p;
   3395 		nindex2ifc = nsize;
   3396 	}
   3397 	index2ifc[idx] = ifcp;
   3398 }
   3399