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