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