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