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