162607Sitojun/*	$FreeBSD: stable/11/usr.sbin/route6d/route6d.c 338279 2018-08-23 21:24:22Z markj $	*/
2122677Sume/*	$KAME: route6d.c,v 1.104 2003/10/31 00:30:20 itojun Exp $	*/
355163Sshin
4331722Seadler/*
555163Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
655163Sshin * All rights reserved.
7312022Sngie *
855163Sshin * Redistribution and use in source and binary forms, with or without
955163Sshin * modification, are permitted provided that the following conditions
1055163Sshin * are met:
1155163Sshin * 1. Redistributions of source code must retain the above copyright
1255163Sshin *    notice, this list of conditions and the following disclaimer.
1355163Sshin * 2. Redistributions in binary form must reproduce the above copyright
1455163Sshin *    notice, this list of conditions and the following disclaimer in the
1555163Sshin *    documentation and/or other materials provided with the distribution.
1655163Sshin * 3. Neither the name of the project nor the names of its contributors
1755163Sshin *    may be used to endorse or promote products derived from this software
1855163Sshin *    without specific prior written permission.
19312022Sngie *
2055163Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2155163Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2255163Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2355163Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2455163Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2555163Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2655163Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2755163Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2855163Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2955163Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3055163Sshin * SUCH DAMAGE.
3155163Sshin */
3255163Sshin
3355163Sshin#ifndef	lint
34243232Shrsstatic const char _rcsid[] = "$KAME: route6d.c,v 1.104 2003/10/31 00:30:20 itojun Exp $";
3555163Sshin#endif
3655163Sshin
3755163Sshin#include <sys/param.h>
3855163Sshin#include <sys/file.h>
39312022Sngie#include <sys/ioctl.h>
4055163Sshin#include <sys/socket.h>
4155163Sshin#include <sys/sysctl.h>
4255163Sshin#include <sys/uio.h>
43312022Sngie#include <arpa/inet.h>
4455163Sshin#include <net/if.h>
4555163Sshin#include <net/route.h>
4655163Sshin#include <netinet/in.h>
4755163Sshin#include <netinet/in_var.h>
4855163Sshin#include <netinet/ip6.h>
4955163Sshin#include <netinet/udp.h>
50312022Sngie#include <err.h>
51312022Sngie#include <errno.h>
52312022Sngie#include <fnmatch.h>
53312022Sngie#include <ifaddrs.h>
5455163Sshin#include <netdb.h>
55312022Sngie#ifdef HAVE_POLL_H
56312022Sngie#include <poll.h>
57312022Sngie#endif
58312022Sngie#include <signal.h>
59312022Sngie#include <stdio.h>
60312022Sngie#ifdef __STDC__
61312022Sngie#include <stdarg.h>
62312022Sngie#else
63312022Sngie#include <varargs.h>
64312022Sngie#endif
65312022Sngie#include <stddef.h>
66312022Sngie#include <stdlib.h>
67312022Sngie#include <string.h>
68312022Sngie#include <syslog.h>
69312022Sngie#include <time.h>
70312022Sngie#include <unistd.h>
7155163Sshin
7255163Sshin#include "route6d.h"
7355163Sshin
7455163Sshin#define	MAXFILTER	40
75243233Shrs#define RT_DUMP_MAXRETRY	15
7655163Sshin
7755163Sshin#ifdef	DEBUG
7855163Sshin#define	INIT_INTERVAL6	6
7955163Sshin#else
80119039Sume#define	INIT_INTERVAL6	10	/* Wait to submit an initial riprequest */
8155163Sshin#endif
8255163Sshin
8355163Sshin/* alignment constraint for routing socket */
8462607Sitojun#define ROUNDUP(a) \
8555163Sshin	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
8662607Sitojun#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
8755163Sshin
88243232Shrsstruct ifc {			/* Configuration of an interface */
89243232Shrs	TAILQ_ENTRY(ifc) ifc_next;
90243232Shrs
91243232Shrs	char	ifc_name[IFNAMSIZ];		/* if name */
9255163Sshin	int	ifc_index;			/* if index */
9355163Sshin	int	ifc_mtu;			/* if mtu */
9455163Sshin	int	ifc_metric;			/* if metric */
9578064Sume	u_int	ifc_flags;			/* flags */
9678064Sume	short	ifc_cflags;			/* IFC_XXX */
9755163Sshin	struct	in6_addr ifc_mylladdr;		/* my link-local address */
9855163Sshin	struct	sockaddr_in6 ifc_ripsin;	/* rip multicast address */
99243232Shrs	TAILQ_HEAD(, ifac) ifc_ifac_head;	/* list of AF_INET6 addrs */
100243232Shrs	TAILQ_HEAD(, iff) ifc_iff_head;		/* list of filters */
10155163Sshin	int	ifc_joined;			/* joined to ff02::9 */
10255163Sshin};
103243232ShrsTAILQ_HEAD(, ifc) ifc_head = TAILQ_HEAD_INITIALIZER(ifc_head);
10455163Sshin
105312022Sngiestruct ifac {			/* Adddress associated to an interface */
106243232Shrs	TAILQ_ENTRY(ifac) ifac_next;
107243232Shrs
108243232Shrs	struct	ifc *ifac_ifc;		/* back pointer */
109243232Shrs	struct	in6_addr ifac_addr;	/* address */
110243232Shrs	struct	in6_addr ifac_raddr;	/* remote address, valid in p2p */
111243232Shrs	int	ifac_scope_id;		/* scope id */
112243232Shrs	int	ifac_plen;		/* prefix length */
11355163Sshin};
11455163Sshin
115243232Shrsstruct iff {			/* Filters for an interface */
116243232Shrs	TAILQ_ENTRY(iff) iff_next;
117243232Shrs
11855163Sshin	int	iff_type;
11955163Sshin	struct	in6_addr iff_addr;
12055163Sshin	int	iff_plen;
12155163Sshin};
12255163Sshin
12355163Sshinstruct	ifc **index2ifc;
124243232Shrsunsigned int	nindex2ifc;
12555163Sshinstruct	ifc *loopifcp = NULL;	/* pointing to loopback */
126119076Sume#ifdef HAVE_POLL_H
127119076Sumestruct	pollfd set[2];
128119076Sume#else
129119070Sumefd_set	*sockvecp;	/* vector to select() for receiving */
130119070Sumefd_set	*recvecp;
131119070Sumeint	fdmasks;
132119070Sumeint	maxfd;		/* maximum fd for select() */
133119076Sume#endif
13455163Sshinint	rtsock;		/* the routing socket */
13555163Sshinint	ripsock;	/* socket to send/receive RIP datagram */
13655163Sshin
13755163Sshinstruct	rip6 *ripbuf;	/* packet buffer for sending */
13855163Sshin
13955163Sshin/*
14078064Sume * Maintain the routes in a linked list.  When the number of the routes
14155163Sshin * grows, somebody would like to introduce a hash based or a radix tree
14278064Sume * based structure.  I believe the number of routes handled by RIP is
14355163Sshin * limited and I don't have to manage a complex data structure, however.
14455163Sshin *
14555163Sshin * One of the major drawbacks of the linear linked list is the difficulty
14678064Sume * of representing the relationship between a couple of routes.  This may
14755163Sshin * be a significant problem when we have to support route aggregation with
148228990Suqs * suppressing the specifics covered by the aggregate.
14955163Sshin */
15055163Sshin
151243232Shrsstruct riprt {
152243232Shrs	TAILQ_ENTRY(riprt) rrt_next;	/* next destination */
153243232Shrs
15455163Sshin	struct	riprt *rrt_same;	/* same destination - future use */
15555163Sshin	struct	netinfo6 rrt_info;	/* network info */
15655163Sshin	struct	in6_addr rrt_gw;	/* gateway */
15762607Sitojun	u_long	rrt_flags;		/* kernel routing table flags */
15862607Sitojun	u_long	rrt_rflags;		/* route6d routing table flags */
15955163Sshin	time_t	rrt_t;			/* when the route validated */
16055163Sshin	int	rrt_index;		/* ifindex from which this route got */
16155163Sshin};
162243232ShrsTAILQ_HEAD(, riprt) riprt_head = TAILQ_HEAD_INITIALIZER(riprt_head);
16355163Sshin
16455163Sshinint	dflag = 0;	/* debug flag */
16555163Sshinint	qflag = 0;	/* quiet flag */
16655163Sshinint	nflag = 0;	/* don't update kernel routing table */
16755163Sshinint	aflag = 0;	/* age out even the statically defined routes */
16855163Sshinint	hflag = 0;	/* don't split horizon */
16955163Sshinint	lflag = 0;	/* exchange site local routes */
170243233Shrsint	Pflag = 0;	/* don't age out routes with RTF_PROTO[123] */
171243233Shrsint	Qflag = RTF_PROTO2;	/* set RTF_PROTO[123] flag to routes by RIPng */
17255163Sshinint	sflag = 0;	/* announce static routes w/ split horizon */
17355163Sshinint	Sflag = 0;	/* announce static routes to every interface */
17462607Sitojununsigned long routetag = 0;	/* route tag attached on originating case */
17555163Sshin
17655163Sshinchar	*filter[MAXFILTER];
17755163Sshinint	filtertype[MAXFILTER];
17855163Sshinint	nfilter = 0;
17955163Sshin
18055163Sshinpid_t	pid;
18155163Sshin
18255163Sshinstruct	sockaddr_storage ripsin;
18355163Sshin
18455163Sshinint	interval = 1;
18555163Sshintime_t	nextalarm = 0;
18655163Sshintime_t	sup_trig_update = 0;
18755163Sshin
18855163SshinFILE	*rtlog = NULL;
18955163Sshin
19055163Sshinint logopened = 0;
19155163Sshin
192119085Sumestatic	int	seq = 0;
19355163Sshin
19478064Sumevolatile sig_atomic_t seenalrm;
19578064Sumevolatile sig_atomic_t seenquit;
19678064Sumevolatile sig_atomic_t seenusr1;
19778064Sume
19862607Sitojun#define	RRTF_AGGREGATE		0x08000000
19962607Sitojun#define	RRTF_NOADVERTISE	0x10000000
20062607Sitojun#define	RRTF_NH_NOT_LLADDR	0x20000000
20162607Sitojun#define RRTF_SENDANYWAY		0x40000000
20262607Sitojun#define	RRTF_CHANGED		0x80000000
20355163Sshin
204173412Skevloint main(int, char **);
205173412Skevlovoid sighandler(int);
206173412Skevlovoid ripalarm(void);
207173412Skevlovoid riprecv(void);
208173412Skevlovoid ripsend(struct ifc *, struct sockaddr_in6 *, int);
209173412Skevloint out_filter(struct riprt *, struct ifc *);
210173412Skevlovoid init(void);
211173412Skevlovoid sockopt(struct ifc *);
212173412Skevlovoid ifconfig(void);
213243232Shrsint ifconfig1(const char *, const struct sockaddr *, struct ifc *, int);
214173412Skevlovoid rtrecv(void);
215173412Skevloint rt_del(const struct sockaddr_in6 *, const struct sockaddr_in6 *,
216173412Skevlo	const struct sockaddr_in6 *);
217173412Skevloint rt_deladdr(struct ifc *, const struct sockaddr_in6 *,
218173412Skevlo	const struct sockaddr_in6 *);
219173412Skevlovoid filterconfig(void);
220173412Skevloint getifmtu(int);
221173412Skevloconst char *rttypes(struct rt_msghdr *);
222173412Skevloconst char *rtflags(struct rt_msghdr *);
223173412Skevloconst char *ifflags(int);
224173412Skevloint ifrt(struct ifc *, int);
225173412Skevlovoid ifrt_p2p(struct ifc *, int);
226173412Skevlovoid applymask(struct in6_addr *, struct in6_addr *);
227173412Skevlovoid applyplen(struct in6_addr *, int);
228173412Skevlovoid ifrtdump(int);
229173412Skevlovoid ifdump(int);
230173412Skevlovoid ifdump0(FILE *, const struct ifc *);
231243233Shrsvoid ifremove(int);
232173412Skevlovoid rtdump(int);
233173412Skevlovoid rt_entry(struct rt_msghdr *, int);
234173412Skevlovoid rtdexit(void);
235173412Skevlovoid riprequest(struct ifc *, struct netinfo6 *, int,
236173412Skevlo	struct sockaddr_in6 *);
237243232Shrsvoid ripflush(struct ifc *, struct sockaddr_in6 *, int, struct netinfo6 *np);
238173412Skevlovoid sendrequest(struct ifc *);
239173412Skevloint sin6mask2len(const struct sockaddr_in6 *);
240173412Skevloint mask2len(const struct in6_addr *, int);
241173412Skevloint sendpacket(struct sockaddr_in6 *, int);
242173412Skevloint addroute(struct riprt *, const struct in6_addr *, struct ifc *);
243173412Skevloint delroute(struct netinfo6 *, struct in6_addr *);
244173412Skevlostruct in6_addr *getroute(struct netinfo6 *, struct in6_addr *);
245173412Skevlovoid krtread(int);
246173412Skevloint tobeadv(struct riprt *, struct ifc *);
247173412Skevlochar *allocopy(char *);
248173412Skevlochar *hms(void);
249173412Skevloconst char *inet6_n2p(const struct in6_addr *);
250173412Skevlostruct ifac *ifa_match(const struct ifc *, const struct in6_addr *, int);
251173412Skevlostruct in6_addr *plen2mask(int);
252243232Shrsstruct riprt *rtsearch(struct netinfo6 *);
253173412Skevloint ripinterval(int);
254173412Skevlotime_t ripsuptrig(void);
255173412Skevlovoid fatal(const char *, ...)
25666807Skris	__attribute__((__format__(__printf__, 1, 2)));
257173412Skevlovoid trace(int, const char *, ...)
25866807Skris	__attribute__((__format__(__printf__, 2, 3)));
259173412Skevlovoid tracet(int, const char *, ...)
26066807Skris	__attribute__((__format__(__printf__, 2, 3)));
261173412Skevlounsigned int if_maxindex(void);
262173412Skevlostruct ifc *ifc_find(char *);
263173412Skevlostruct iff *iff_find(struct ifc *, int);
264173412Skevlovoid setindex2ifc(int, struct ifc *);
26555163Sshin
26655163Sshin#define	MALLOC(type)	((type *)malloc(sizeof(type)))
26755163Sshin
268243232Shrs#define IFIL_TYPE_ANY	0x0
269243232Shrs#define IFIL_TYPE_A	'A'
270243232Shrs#define IFIL_TYPE_N	'N'
271243232Shrs#define IFIL_TYPE_T	'T'
272243232Shrs#define IFIL_TYPE_O	'O'
273243232Shrs#define IFIL_TYPE_L	'L'
274243232Shrs
27555163Sshinint
276243232Shrsmain(int argc, char *argv[])
27755163Sshin{
27855163Sshin	int	ch;
27955163Sshin	int	error = 0;
280243233Shrs	unsigned long proto;
28155163Sshin	struct	ifc *ifcp;
28255163Sshin	sigset_t mask, omask;
283243233Shrs	const char *pidfile = ROUTE6D_PID;
284243233Shrs	FILE *pidfh;
28555163Sshin	char *progname;
28662607Sitojun	char *ep;
28755163Sshin
28855163Sshin	progname = strrchr(*argv, '/');
28955163Sshin	if (progname)
29055163Sshin		progname++;
29155163Sshin	else
29255163Sshin		progname = *argv;
29355163Sshin
29455163Sshin	pid = getpid();
295243233Shrs	while ((ch = getopt(argc, argv, "A:N:O:R:T:L:t:adDhlnp:P:Q:qsS")) != -1) {
29655163Sshin		switch (ch) {
29755163Sshin		case 'A':
29855163Sshin		case 'N':
29955163Sshin		case 'O':
30055163Sshin		case 'T':
30155163Sshin		case 'L':
30262607Sitojun			if (nfilter >= MAXFILTER) {
30355163Sshin				fatal("Exceeds MAXFILTER");
30462607Sitojun				/*NOTREACHED*/
30562607Sitojun			}
30655163Sshin			filtertype[nfilter] = ch;
30755163Sshin			filter[nfilter++] = allocopy(optarg);
30855163Sshin			break;
30955163Sshin		case 't':
31062607Sitojun			ep = NULL;
31162607Sitojun			routetag = strtoul(optarg, &ep, 0);
31262607Sitojun			if (!ep || *ep != '\0' || (routetag & ~0xffff) != 0) {
31355163Sshin				fatal("invalid route tag");
31455163Sshin				/*NOTREACHED*/
31555163Sshin			}
31655163Sshin			break;
317243233Shrs		case 'p':
318243233Shrs			pidfile = optarg;
319243233Shrs			break;
320243233Shrs		case 'P':
321243233Shrs			ep = NULL;
322243233Shrs			proto = strtoul(optarg, &ep, 0);
323243233Shrs			if (!ep || *ep != '\0' || 3 < proto) {
324243233Shrs				fatal("invalid P flag");
325243233Shrs				/*NOTREACHED*/
326243233Shrs			}
327243233Shrs			if (proto == 0)
328243233Shrs				Pflag = 0;
329243233Shrs			if (proto == 1)
330243233Shrs				Pflag |= RTF_PROTO1;
331243233Shrs			if (proto == 2)
332243233Shrs				Pflag |= RTF_PROTO2;
333243233Shrs			if (proto == 3)
334243233Shrs				Pflag |= RTF_PROTO3;
335243233Shrs			break;
336243233Shrs		case 'Q':
337243233Shrs			ep = NULL;
338243233Shrs			proto = strtoul(optarg, &ep, 0);
339243233Shrs			if (!ep || *ep != '\0' || 3 < proto) {
340243233Shrs				fatal("invalid Q flag");
341243233Shrs				/*NOTREACHED*/
342243233Shrs			}
343243233Shrs			if (proto == 0)
344243233Shrs				Qflag = 0;
345243233Shrs			if (proto == 1)
346243233Shrs				Qflag |= RTF_PROTO1;
347243233Shrs			if (proto == 2)
348243233Shrs				Qflag |= RTF_PROTO2;
349243233Shrs			if (proto == 3)
350243233Shrs				Qflag |= RTF_PROTO3;
351243233Shrs			break;
35255163Sshin		case 'R':
35362607Sitojun			if ((rtlog = fopen(optarg, "w")) == NULL) {
35455163Sshin				fatal("Can not write to routelog");
35562607Sitojun				/*NOTREACHED*/
35662607Sitojun			}
35755163Sshin			break;
35862607Sitojun#define	FLAG(c, flag, n)	case c: do { flag = n; break; } while(0)
35962607Sitojun		FLAG('a', aflag, 1); break;
36062607Sitojun		FLAG('d', dflag, 1); break;
36162607Sitojun		FLAG('D', dflag, 2); break;
36262607Sitojun		FLAG('h', hflag, 1); break;
36362607Sitojun		FLAG('l', lflag, 1); break;
36462607Sitojun		FLAG('n', nflag, 1); break;
36562607Sitojun		FLAG('q', qflag, 1); break;
36662607Sitojun		FLAG('s', sflag, 1); break;
36762607Sitojun		FLAG('S', Sflag, 1); break;
36855163Sshin#undef	FLAG
36955163Sshin		default:
37055163Sshin			fatal("Invalid option specified, terminating");
37162607Sitojun			/*NOTREACHED*/
37255163Sshin		}
37355163Sshin	}
37455163Sshin	argc -= optind;
37555163Sshin	argv += optind;
37678064Sume	if (argc > 0) {
37755163Sshin		fatal("bogus extra arguments");
37878064Sume		/*NOTREACHED*/
37978064Sume	}
38055163Sshin
38155163Sshin	if (geteuid()) {
38255163Sshin		nflag = 1;
38355163Sshin		fprintf(stderr, "No kernel update is allowed\n");
38455163Sshin	}
385119037Sume
386119037Sume	if (dflag == 0) {
387119037Sume		if (daemon(0, 0) < 0) {
388119037Sume			fatal("daemon");
389119037Sume			/*NOTREACHED*/
390119037Sume		}
391119037Sume	}
392119037Sume
39355163Sshin	openlog(progname, LOG_NDELAY|LOG_PID, LOG_DAEMON);
39455163Sshin	logopened++;
39578064Sume
39678064Sume	if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL)
39778064Sume		fatal("malloc");
39878064Sume	memset(ripbuf, 0, RIP6_MAXMTU);
39978064Sume	ripbuf->rip6_cmd = RIP6_RESPONSE;
40078064Sume	ripbuf->rip6_vers = RIP6_VERSION;
40178064Sume	ripbuf->rip6_res1[0] = 0;
40278064Sume	ripbuf->rip6_res1[1] = 0;
40378064Sume
40455163Sshin	init();
40555163Sshin	ifconfig();
406243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
40755163Sshin		if (ifcp->ifc_index < 0) {
408243232Shrs			fprintf(stderr, "No ifindex found at %s "
409243232Shrs			    "(no link-local address?)\n", ifcp->ifc_name);
41055163Sshin			error++;
41155163Sshin		}
41255163Sshin	}
41355163Sshin	if (error)
41455163Sshin		exit(1);
41578064Sume	if (loopifcp == NULL) {
41655163Sshin		fatal("No loopback found");
41778064Sume		/*NOTREACHED*/
41878064Sume	}
419243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
42055163Sshin		ifrt(ifcp, 0);
421243232Shrs	}
42255163Sshin	filterconfig();
42355163Sshin	krtread(0);
42455163Sshin	if (dflag)
42555163Sshin		ifrtdump(0);
42655163Sshin
42755163Sshin	pid = getpid();
428243233Shrs	if ((pidfh = fopen(pidfile, "w")) != NULL) {
429243233Shrs		fprintf(pidfh, "%d\n", pid);
430243233Shrs		fclose(pidfh);
43155163Sshin	}
43255163Sshin
43378064Sume	if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL) {
43455163Sshin		fatal("malloc");
43578064Sume		/*NOTREACHED*/
43678064Sume	}
43762607Sitojun	memset(ripbuf, 0, RIP6_MAXMTU);
43855163Sshin	ripbuf->rip6_cmd = RIP6_RESPONSE;
43955163Sshin	ripbuf->rip6_vers = RIP6_VERSION;
44055163Sshin	ripbuf->rip6_res1[0] = 0;
44155163Sshin	ripbuf->rip6_res1[1] = 0;
44255163Sshin
44378064Sume	if (signal(SIGALRM, sighandler) == SIG_ERR ||
44478064Sume	    signal(SIGQUIT, sighandler) == SIG_ERR ||
44578064Sume	    signal(SIGTERM, sighandler) == SIG_ERR ||
44678064Sume	    signal(SIGUSR1, sighandler) == SIG_ERR ||
44778064Sume	    signal(SIGHUP, sighandler) == SIG_ERR ||
44878064Sume	    signal(SIGINT, sighandler) == SIG_ERR) {
44978064Sume		fatal("signal");
45078064Sume		/*NOTREACHED*/
45178064Sume	}
45255163Sshin	/*
45355163Sshin	 * To avoid rip packet congestion (not on a cable but in this
45455163Sshin	 * process), wait for a moment to send the first RIP6_RESPONSE
45555163Sshin	 * packets.
45655163Sshin	 */
45755163Sshin	alarm(ripinterval(INIT_INTERVAL6));
45855163Sshin
459243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
460243232Shrs		if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
461119041Sume			continue;
46255163Sshin		if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
46355163Sshin			sendrequest(ifcp);
46455163Sshin	}
46555163Sshin
46655163Sshin	syslog(LOG_INFO, "**** Started ****");
46755163Sshin	sigemptyset(&mask);
46855163Sshin	sigaddset(&mask, SIGALRM);
46955163Sshin	while (1) {
47078064Sume		if (seenalrm) {
47178064Sume			ripalarm();
47278064Sume			seenalrm = 0;
47378064Sume			continue;
47478064Sume		}
47578064Sume		if (seenquit) {
47678064Sume			rtdexit();
47778064Sume			seenquit = 0;
47878064Sume			continue;
47978064Sume		}
48078064Sume		if (seenusr1) {
48178064Sume			ifrtdump(SIGUSR1);
48278064Sume			seenusr1 = 0;
48378064Sume			continue;
48478064Sume		}
48578064Sume
486119076Sume#ifdef HAVE_POLL_H
487119076Sume		switch (poll(set, 2, INFTIM))
488119076Sume#else
489119070Sume		memcpy(recvecp, sockvecp, fdmasks);
490119076Sume		switch (select(maxfd + 1, recvecp, 0, 0, 0))
491119076Sume#endif
492119076Sume		{
49355163Sshin		case -1:
49478064Sume			if (errno != EINTR) {
49578064Sume				fatal("select");
49678064Sume				/*NOTREACHED*/
49778064Sume			}
49878064Sume			continue;
49955163Sshin		case 0:
50055163Sshin			continue;
50155163Sshin		default:
502119076Sume#ifdef HAVE_POLL_H
503119076Sume			if (set[0].revents & POLLIN)
504119076Sume#else
505119076Sume			if (FD_ISSET(ripsock, recvecp))
506119076Sume#endif
507119076Sume			{
50855163Sshin				sigprocmask(SIG_BLOCK, &mask, &omask);
50955163Sshin				riprecv();
51055163Sshin				sigprocmask(SIG_SETMASK, &omask, NULL);
51155163Sshin			}
512119076Sume#ifdef HAVE_POLL_H
513119076Sume			if (set[1].revents & POLLIN)
514119076Sume#else
515119076Sume			if (FD_ISSET(rtsock, recvecp))
516119076Sume#endif
517119076Sume			{
51855163Sshin				sigprocmask(SIG_BLOCK, &mask, &omask);
51955163Sshin				rtrecv();
52055163Sshin				sigprocmask(SIG_SETMASK, &omask, NULL);
52155163Sshin			}
52255163Sshin		}
52355163Sshin	}
52455163Sshin}
52555163Sshin
52678064Sumevoid
527243232Shrssighandler(int signo)
52878064Sume{
52978064Sume
53078064Sume	switch (signo) {
53178064Sume	case SIGALRM:
53278064Sume		seenalrm++;
53378064Sume		break;
53478064Sume	case SIGQUIT:
53578064Sume	case SIGTERM:
53678064Sume		seenquit++;
53778064Sume		break;
53878064Sume	case SIGUSR1:
53978064Sume	case SIGHUP:
54078064Sume	case SIGINT:
54178064Sume		seenusr1++;
54278064Sume		break;
54378064Sume	}
54478064Sume}
54578064Sume
54655163Sshin/*
54755163Sshin * gracefully exits after resetting sockopts.
54855163Sshin */
54955163Sshin/* ARGSUSED */
55055163Sshinvoid
551243232Shrsrtdexit(void)
55255163Sshin{
55355163Sshin	struct	riprt *rrt;
55455163Sshin
55555163Sshin	alarm(0);
556243232Shrs	TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
55762607Sitojun		if (rrt->rrt_rflags & RRTF_AGGREGATE) {
55855163Sshin			delroute(&rrt->rrt_info, &rrt->rrt_gw);
55955163Sshin		}
56055163Sshin	}
56155163Sshin	close(ripsock);
56255163Sshin	close(rtsock);
56355163Sshin	syslog(LOG_INFO, "**** Terminated ****");
56455163Sshin	closelog();
56555163Sshin	exit(1);
56655163Sshin}
56755163Sshin
56855163Sshin/*
56955163Sshin * Called periodically:
57055163Sshin *	1. age out the learned route. remove it if necessary.
57155163Sshin *	2. submit RIP6_RESPONSE packets.
57278064Sume * Invoked in every SUPPLY_INTERVAL6 (30) seconds.  I believe we don't have
57355163Sshin * to invoke this function in every 1 or 5 or 10 seconds only to age the
57455163Sshin * routes more precisely.
57555163Sshin */
57655163Sshin/* ARGSUSED */
57755163Sshinvoid
578243232Shrsripalarm(void)
57955163Sshin{
58055163Sshin	struct	ifc *ifcp;
581243232Shrs	struct	riprt *rrt, *rrt_tmp;
58255163Sshin	time_t	t_lifetime, t_holddown;
58355163Sshin
58455163Sshin	/* age the RIP routes */
58555163Sshin	t_lifetime = time(NULL) - RIP_LIFETIME;
58655163Sshin	t_holddown = t_lifetime - RIP_HOLDDOWN;
587243232Shrs	TAILQ_FOREACH_SAFE(rrt, &riprt_head, rrt_next, rrt_tmp) {
588243232Shrs		if (rrt->rrt_t == 0)
58955163Sshin			continue;
590243232Shrs		else if (rrt->rrt_t < t_holddown) {
591243232Shrs			TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
59255163Sshin			delroute(&rrt->rrt_info, &rrt->rrt_gw);
59355163Sshin			free(rrt);
594243232Shrs		} else if (rrt->rrt_t < t_lifetime)
59555163Sshin			rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
59655163Sshin	}
59755163Sshin	/* Supply updates */
598243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
59955163Sshin		if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
60055163Sshin			ripsend(ifcp, &ifcp->ifc_ripsin, 0);
60155163Sshin	}
60255163Sshin	alarm(ripinterval(SUPPLY_INTERVAL6));
60355163Sshin}
60455163Sshin
60555163Sshinvoid
606243232Shrsinit(void)
60755163Sshin{
608119034Sume	int	error;
609119034Sume	const int int0 = 0, int1 = 1, int255 = 255;
61055163Sshin	struct	addrinfo hints, *res;
611119081Sume	char	port[NI_MAXSERV];
61255163Sshin
613243232Shrs	TAILQ_INIT(&ifc_head);
61455163Sshin	nindex2ifc = 0;	/*initial guess*/
61555163Sshin	index2ifc = NULL;
616119081Sume	snprintf(port, sizeof(port), "%u", RIP6_PORT);
61755163Sshin
61855163Sshin	memset(&hints, 0, sizeof(hints));
61955163Sshin	hints.ai_family = PF_INET6;
62055163Sshin	hints.ai_socktype = SOCK_DGRAM;
621119080Sume	hints.ai_protocol = IPPROTO_UDP;
62255163Sshin	hints.ai_flags = AI_PASSIVE;
62355163Sshin	error = getaddrinfo(NULL, port, &hints, &res);
62478064Sume	if (error) {
62566807Skris		fatal("%s", gai_strerror(error));
62678064Sume		/*NOTREACHED*/
62778064Sume	}
62878064Sume	if (res->ai_next) {
62955163Sshin		fatal(":: resolved to multiple address");
63078064Sume		/*NOTREACHED*/
63178064Sume	}
63255163Sshin
63355163Sshin	ripsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
63478064Sume	if (ripsock < 0) {
63555163Sshin		fatal("rip socket");
63678064Sume		/*NOTREACHED*/
63778064Sume	}
638119034Sume#ifdef IPV6_V6ONLY
639119034Sume	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_V6ONLY,
640119034Sume	    &int1, sizeof(int1)) < 0) {
641119034Sume		fatal("rip IPV6_V6ONLY");
642119034Sume		/*NOTREACHED*/
643119034Sume	}
644119034Sume#endif
64578064Sume	if (bind(ripsock, res->ai_addr, res->ai_addrlen) < 0) {
64655163Sshin		fatal("rip bind");
64778064Sume		/*NOTREACHED*/
64878064Sume	}
64955163Sshin	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
65078064Sume	    &int255, sizeof(int255)) < 0) {
65155163Sshin		fatal("rip IPV6_MULTICAST_HOPS");
65278064Sume		/*NOTREACHED*/
65378064Sume	}
65455163Sshin	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
65578064Sume	    &int0, sizeof(int0)) < 0) {
65655163Sshin		fatal("rip IPV6_MULTICAST_LOOP");
65778064Sume		/*NOTREACHED*/
65878064Sume	}
65962921Sume
66062607Sitojun#ifdef IPV6_RECVPKTINFO
661119034Sume	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVPKTINFO,
662119034Sume	    &int1, sizeof(int1)) < 0) {
66362607Sitojun		fatal("rip IPV6_RECVPKTINFO");
66478064Sume		/*NOTREACHED*/
66578064Sume	}
66662607Sitojun#else  /* old adv. API */
667119034Sume	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_PKTINFO,
668119034Sume	    &int1, sizeof(int1)) < 0) {
66955163Sshin		fatal("rip IPV6_PKTINFO");
67078064Sume		/*NOTREACHED*/
67178064Sume	}
672312022Sngie#endif
67355163Sshin
674164339Ssuz#ifdef IPV6_RECVPKTINFO
675164339Ssuz	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
676164339Ssuz	    &int1, sizeof(int1)) < 0) {
677164339Ssuz		fatal("rip IPV6_RECVHOPLIMIT");
678164339Ssuz		/*NOTREACHED*/
679164339Ssuz	}
680164339Ssuz#else  /* old adv. API */
681164339Ssuz	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_HOPLIMIT,
682164339Ssuz	    &int1, sizeof(int1)) < 0) {
683164339Ssuz		fatal("rip IPV6_HOPLIMIT");
684164339Ssuz		/*NOTREACHED*/
685164339Ssuz	}
686164339Ssuz#endif
687164339Ssuz
68855163Sshin	memset(&hints, 0, sizeof(hints));
68955163Sshin	hints.ai_family = PF_INET6;
69055163Sshin	hints.ai_socktype = SOCK_DGRAM;
691119080Sume	hints.ai_protocol = IPPROTO_UDP;
69255163Sshin	error = getaddrinfo(RIP6_DEST, port, &hints, &res);
69378064Sume	if (error) {
69466807Skris		fatal("%s", gai_strerror(error));
69578064Sume		/*NOTREACHED*/
69678064Sume	}
69778064Sume	if (res->ai_next) {
69855163Sshin		fatal("%s resolved to multiple address", RIP6_DEST);
69978064Sume		/*NOTREACHED*/
70078064Sume	}
70155163Sshin	memcpy(&ripsin, res->ai_addr, res->ai_addrlen);
70255163Sshin
703119076Sume#ifdef HAVE_POLL_H
704119076Sume	set[0].fd = ripsock;
705119076Sume	set[0].events = POLLIN;
706119076Sume#else
707119070Sume	maxfd = ripsock;
708119076Sume#endif
70955163Sshin
71055163Sshin	if (nflag == 0) {
71178064Sume		if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
71255163Sshin			fatal("route socket");
71378064Sume			/*NOTREACHED*/
71478064Sume		}
715119076Sume#ifdef HAVE_POLL_H
716119076Sume		set[1].fd = rtsock;
717119076Sume		set[1].events = POLLIN;
718119076Sume#else
719119070Sume		if (rtsock > maxfd)
720119070Sume			maxfd = rtsock;
721119076Sume#endif
722119076Sume	} else {
723119076Sume#ifdef HAVE_POLL_H
724119076Sume		set[1].fd = -1;
725119076Sume#else
72655163Sshin		rtsock = -1;	/*just for safety */
727119076Sume#endif
728119076Sume	}
729119070Sume
730119076Sume#ifndef HAVE_POLL_H
731119070Sume	fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
732119070Sume	if ((sockvecp = malloc(fdmasks)) == NULL) {
733119070Sume		fatal("malloc");
734119070Sume		/*NOTREACHED*/
735119070Sume	}
736119070Sume	if ((recvecp = malloc(fdmasks)) == NULL) {
737119070Sume		fatal("malloc");
738119070Sume		/*NOTREACHED*/
739119070Sume	}
740119070Sume	memset(sockvecp, 0, fdmasks);
741119070Sume	FD_SET(ripsock, sockvecp);
742119070Sume	if (rtsock >= 0)
743119070Sume		FD_SET(rtsock, sockvecp);
744119076Sume#endif
74555163Sshin}
74655163Sshin
74762607Sitojun#define	RIPSIZE(n) \
74862607Sitojun	(sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6))
74955163Sshin
75055163Sshin/*
75155163Sshin * ripflush flushes the rip datagram stored in the rip buffer
75255163Sshin */
75355163Sshinvoid
754243232Shrsripflush(struct ifc *ifcp, struct sockaddr_in6 *sin6, int nrt, struct netinfo6 *np)
75555163Sshin{
75655163Sshin	int i;
75755163Sshin	int error;
75855163Sshin
75955163Sshin	if (ifcp)
76055163Sshin		tracet(1, "Send(%s): info(%d) to %s.%d\n",
76155163Sshin			ifcp->ifc_name, nrt,
762119031Sume			inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
76355163Sshin	else
76455163Sshin		tracet(1, "Send: info(%d) to %s.%d\n",
765119031Sume			nrt, inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
76655163Sshin	if (dflag >= 2) {
76755163Sshin		np = ripbuf->rip6_nets;
76855163Sshin		for (i = 0; i < nrt; i++, np++) {
76955163Sshin			if (np->rip6_metric == NEXTHOP_METRIC) {
77055163Sshin				if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest))
77162607Sitojun					trace(2, "    NextHop reset");
77255163Sshin				else {
77355163Sshin					trace(2, "    NextHop %s",
77455163Sshin						inet6_n2p(&np->rip6_dest));
77555163Sshin				}
77655163Sshin			} else {
77755163Sshin				trace(2, "    %s/%d[%d]",
77855163Sshin					inet6_n2p(&np->rip6_dest),
77955163Sshin					np->rip6_plen, np->rip6_metric);
78055163Sshin			}
78155163Sshin			if (np->rip6_tag) {
78255163Sshin				trace(2, "  tag=0x%04x",
78355163Sshin					ntohs(np->rip6_tag) & 0xffff);
78455163Sshin			}
78555163Sshin			trace(2, "\n");
78655163Sshin		}
78755163Sshin	}
788119031Sume	error = sendpacket(sin6, RIPSIZE(nrt));
78955163Sshin	if (error == EAFNOSUPPORT) {
79055163Sshin		/* Protocol not supported */
79155163Sshin		tracet(1, "Could not send info to %s (%s): "
79255163Sshin			"set IFF_UP to 0\n",
79355163Sshin			ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
79455163Sshin		ifcp->ifc_flags &= ~IFF_UP;	/* As if down for AF_INET6 */
79555163Sshin	}
79655163Sshin}
79755163Sshin
79855163Sshin/*
79955163Sshin * Generate RIP6_RESPONSE packets and send them.
80055163Sshin */
80155163Sshinvoid
802243232Shrsripsend(struct	ifc *ifcp, struct sockaddr_in6 *sin6, int flag)
80355163Sshin{
80455163Sshin	struct	riprt *rrt;
80555163Sshin	struct	in6_addr *nh;	/* next hop */
806243232Shrs	struct netinfo6 *np;
80778064Sume	int	maxrte;
808243232Shrs	int nrt;
80955163Sshin
810119084Sume	if (qflag)
811119084Sume		return;
812119084Sume
81355163Sshin	if (ifcp == NULL) {
81455163Sshin		/*
81555163Sshin		 * Request from non-link local address is not
81655163Sshin		 * a regular route6d update.
81755163Sshin		 */
818312022Sngie		maxrte = (IFMINMTU - sizeof(struct ip6_hdr) -
819312022Sngie				sizeof(struct udphdr) -
82055163Sshin				sizeof(struct rip6) + sizeof(struct netinfo6)) /
82155163Sshin				sizeof(struct netinfo6);
822243232Shrs		nh = NULL;
823243232Shrs		nrt = 0;
824243232Shrs		np = ripbuf->rip6_nets;
825243232Shrs		TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
82662607Sitojun			if (rrt->rrt_rflags & RRTF_NOADVERTISE)
82755163Sshin				continue;
82855163Sshin			/* Put the route to the buffer */
82955163Sshin			*np = rrt->rrt_info;
83055163Sshin			np++; nrt++;
83155163Sshin			if (nrt == maxrte) {
832243232Shrs				ripflush(NULL, sin6, nrt, np);
83355163Sshin				nh = NULL;
834243232Shrs				nrt = 0;
835243232Shrs				np = ripbuf->rip6_nets;
83655163Sshin			}
83755163Sshin		}
83855163Sshin		if (nrt)	/* Send last packet */
839243232Shrs			ripflush(NULL, sin6, nrt, np);
84055163Sshin		return;
84155163Sshin	}
84255163Sshin
84362607Sitojun	if ((flag & RRTF_SENDANYWAY) == 0 &&
84455163Sshin	    (qflag || (ifcp->ifc_flags & IFF_LOOPBACK)))
84555163Sshin		return;
84678064Sume
84778064Sume	/* -N: no use */
848243232Shrs	if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
84955163Sshin		return;
85078064Sume
85178064Sume	/* -T: generate default route only */
852243232Shrs	if (iff_find(ifcp, IFIL_TYPE_T) != NULL) {
85355163Sshin		struct netinfo6 rrt_info;
85455163Sshin		memset(&rrt_info, 0, sizeof(struct netinfo6));
85555163Sshin		rrt_info.rip6_dest = in6addr_any;
85655163Sshin		rrt_info.rip6_plen = 0;
85755163Sshin		rrt_info.rip6_metric = 1;
85878064Sume		rrt_info.rip6_metric += ifcp->ifc_metric;
85955163Sshin		rrt_info.rip6_tag = htons(routetag & 0xffff);
86055163Sshin		np = ripbuf->rip6_nets;
86155163Sshin		*np = rrt_info;
86255163Sshin		nrt = 1;
863243232Shrs		ripflush(ifcp, sin6, nrt, np);
86455163Sshin		return;
86555163Sshin	}
86678064Sume
867312022Sngie	maxrte = (ifcp->ifc_mtu - sizeof(struct ip6_hdr) -
868312022Sngie			sizeof(struct udphdr) -
86955163Sshin			sizeof(struct rip6) + sizeof(struct netinfo6)) /
87055163Sshin			sizeof(struct netinfo6);
87178064Sume
87255163Sshin	nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
873243232Shrs	TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
87462607Sitojun		if (rrt->rrt_rflags & RRTF_NOADVERTISE)
87555163Sshin			continue;
87678064Sume
87778064Sume		/* Need to check filter here */
87878064Sume		if (out_filter(rrt, ifcp) == 0)
87955163Sshin			continue;
88078064Sume
88155163Sshin		/* Check split horizon and other conditions */
88255163Sshin		if (tobeadv(rrt, ifcp) == 0)
88355163Sshin			continue;
88478064Sume
88555163Sshin		/* Only considers the routes with flag if specified */
88662607Sitojun		if ((flag & RRTF_CHANGED) &&
88762607Sitojun		    (rrt->rrt_rflags & RRTF_CHANGED) == 0)
88855163Sshin			continue;
88978064Sume
89055163Sshin		/* Check nexthop */
89155163Sshin		if (rrt->rrt_index == ifcp->ifc_index &&
89255163Sshin		    !IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_gw) &&
89362607Sitojun		    (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR) == 0) {
89455163Sshin			if (nh == NULL || !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw)) {
895243232Shrs				if (nrt == maxrte - 2) {
896243232Shrs					ripflush(ifcp, sin6, nrt, np);
897243232Shrs					nh = NULL;
898243232Shrs					nrt = 0;
899243232Shrs					np = ripbuf->rip6_nets;
900243232Shrs				}
901243232Shrs
90255163Sshin				np->rip6_dest = rrt->rrt_gw;
90355163Sshin				np->rip6_plen = 0;
90455163Sshin				np->rip6_tag = 0;
90555163Sshin				np->rip6_metric = NEXTHOP_METRIC;
90655163Sshin				nh = &rrt->rrt_gw;
90755163Sshin				np++; nrt++;
90855163Sshin			}
90955163Sshin		} else if (nh && (rrt->rrt_index != ifcp->ifc_index ||
91055163Sshin			          !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw) ||
91162607Sitojun				  rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)) {
91255163Sshin			/* Reset nexthop */
913243232Shrs			if (nrt == maxrte - 2) {
914243232Shrs				ripflush(ifcp, sin6, nrt, np);
915243232Shrs				nh = NULL;
916243232Shrs				nrt = 0;
917243232Shrs				np = ripbuf->rip6_nets;
918243232Shrs			}
91955163Sshin			memset(np, 0, sizeof(struct netinfo6));
92055163Sshin			np->rip6_metric = NEXTHOP_METRIC;
92155163Sshin			nh = NULL;
92255163Sshin			np++; nrt++;
92355163Sshin		}
92478064Sume
92555163Sshin		/* Put the route to the buffer */
92655163Sshin		*np = rrt->rrt_info;
92755163Sshin		np++; nrt++;
92855163Sshin		if (nrt == maxrte) {
929243232Shrs			ripflush(ifcp, sin6, nrt, np);
93055163Sshin			nh = NULL;
931243232Shrs			nrt = 0;
932243232Shrs			np = ripbuf->rip6_nets;
93355163Sshin		}
93455163Sshin	}
93555163Sshin	if (nrt)	/* Send last packet */
936243232Shrs		ripflush(ifcp, sin6, nrt, np);
93755163Sshin}
93855163Sshin
93955163Sshin/*
94078064Sume * outbound filter logic, per-route/interface.
94178064Sume */
94278064Sumeint
943243232Shrsout_filter(struct riprt *rrt, struct ifc *ifcp)
94478064Sume{
94578064Sume	struct iff *iffp;
94678064Sume	struct in6_addr ia;
94778064Sume	int ok;
94878064Sume
94978064Sume	/*
95078064Sume	 * -A: filter out less specific routes, if we have aggregated
95178064Sume	 * route configured.
952312022Sngie	 */
953243232Shrs	TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
95478064Sume		if (iffp->iff_type != 'A')
95578064Sume			continue;
95678064Sume		if (rrt->rrt_info.rip6_plen <= iffp->iff_plen)
95778064Sume			continue;
958312022Sngie		ia = rrt->rrt_info.rip6_dest;
95978064Sume		applyplen(&ia, iffp->iff_plen);
96078064Sume		if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr))
96178064Sume			return 0;
96278064Sume	}
96378064Sume
96478064Sume	/*
96578064Sume	 * if it is an aggregated route, advertise it only to the
96678064Sume	 * interfaces specified on -A.
96778064Sume	 */
96878064Sume	if ((rrt->rrt_rflags & RRTF_AGGREGATE) != 0) {
96978064Sume		ok = 0;
970243232Shrs		TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
97178064Sume			if (iffp->iff_type != 'A')
97278064Sume				continue;
97378064Sume			if (rrt->rrt_info.rip6_plen == iffp->iff_plen &&
97478064Sume			    IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
97578064Sume			    &iffp->iff_addr)) {
97678064Sume				ok = 1;
97778064Sume				break;
97878064Sume			}
97978064Sume		}
98078064Sume		if (!ok)
98178064Sume			return 0;
98278064Sume	}
98378064Sume
98478064Sume	/*
98578064Sume	 * -O: advertise only if prefix matches the configured prefix.
98678064Sume	 */
987243232Shrs	if (iff_find(ifcp, IFIL_TYPE_O) != NULL) {
98878064Sume		ok = 0;
989243232Shrs		TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
99078064Sume			if (iffp->iff_type != 'O')
99178064Sume				continue;
99278064Sume			if (rrt->rrt_info.rip6_plen < iffp->iff_plen)
99378064Sume				continue;
994312022Sngie			ia = rrt->rrt_info.rip6_dest;
99578064Sume			applyplen(&ia, iffp->iff_plen);
99678064Sume			if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
99778064Sume				ok = 1;
99878064Sume				break;
99978064Sume			}
100078064Sume		}
100178064Sume		if (!ok)
100278064Sume			return 0;
100378064Sume	}
100478064Sume
100578064Sume	/* the prefix should be advertised */
100678064Sume	return 1;
100778064Sume}
100878064Sume
100978064Sume/*
101055163Sshin * Determine if the route is to be advertised on the specified interface.
101155163Sshin * It checks options specified in the arguments and the split horizon rule.
101255163Sshin */
101355163Sshinint
1014243232Shrstobeadv(struct riprt *rrt, struct ifc *ifcp)
101555163Sshin{
101655163Sshin
101755163Sshin	/* Special care for static routes */
101855163Sshin	if (rrt->rrt_flags & RTF_STATIC) {
101962607Sitojun		/* XXX don't advertise reject/blackhole routes */
102062607Sitojun		if (rrt->rrt_flags & (RTF_REJECT | RTF_BLACKHOLE))
102162607Sitojun			return 0;
102262607Sitojun
102355163Sshin		if (Sflag)	/* Yes, advertise it anyway */
102455163Sshin			return 1;
102555163Sshin		if (sflag && rrt->rrt_index != ifcp->ifc_index)
102655163Sshin			return 1;
102755163Sshin		return 0;
102855163Sshin	}
102955163Sshin	/* Regular split horizon */
103055163Sshin	if (hflag == 0 && rrt->rrt_index == ifcp->ifc_index)
103155163Sshin		return 0;
103255163Sshin	return 1;
103355163Sshin}
103455163Sshin
103555163Sshin/*
103655163Sshin * Send a rip packet actually.
103755163Sshin */
103855163Sshinint
1039243232Shrssendpacket(struct sockaddr_in6 *sin6, int len)
104055163Sshin{
104155163Sshin	struct msghdr m;
104255163Sshin	struct cmsghdr *cm;
104355163Sshin	struct iovec iov[2];
104455163Sshin	u_char cmsgbuf[256];
104555163Sshin	struct in6_pktinfo *pi;
104678064Sume	int idx;
104755163Sshin	struct sockaddr_in6 sincopy;
104855163Sshin
104955163Sshin	/* do not overwrite the given sin */
1050119031Sume	sincopy = *sin6;
1051119031Sume	sin6 = &sincopy;
105255163Sshin
1053119035Sume	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
1054243231Shrs	    IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1055243231Shrs		idx = sin6->sin6_scope_id;
1056243231Shrs	else
105778064Sume		idx = 0;
105855163Sshin
1059119031Sume	m.msg_name = (caddr_t)sin6;
1060119031Sume	m.msg_namelen = sizeof(*sin6);
106155163Sshin	iov[0].iov_base = (caddr_t)ripbuf;
106255163Sshin	iov[0].iov_len = len;
106355163Sshin	m.msg_iov = iov;
106455163Sshin	m.msg_iovlen = 1;
1065314427Sasomers	m.msg_flags = 0;
106678064Sume	if (!idx) {
106755163Sshin		m.msg_control = NULL;
106855163Sshin		m.msg_controllen = 0;
106955163Sshin	} else {
107055163Sshin		memset(cmsgbuf, 0, sizeof(cmsgbuf));
107155163Sshin		cm = (struct cmsghdr *)cmsgbuf;
107255163Sshin		m.msg_control = (caddr_t)cm;
107355163Sshin		m.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
107455163Sshin
107555163Sshin		cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
107655163Sshin		cm->cmsg_level = IPPROTO_IPV6;
107755163Sshin		cm->cmsg_type = IPV6_PKTINFO;
107855163Sshin		pi = (struct in6_pktinfo *)CMSG_DATA(cm);
107955163Sshin		memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*::*/
108078064Sume		pi->ipi6_ifindex = idx;
108155163Sshin	}
108255163Sshin
108355163Sshin	if (sendmsg(ripsock, &m, 0 /*MSG_DONTROUTE*/) < 0) {
108455163Sshin		trace(1, "sendmsg: %s\n", strerror(errno));
108555163Sshin		return errno;
108655163Sshin	}
108762921Sume
108855163Sshin	return 0;
108955163Sshin}
109055163Sshin
109155163Sshin/*
109278064Sume * Receive and process RIP packets.  Update the routes/kernel forwarding
109355163Sshin * table if necessary.
109455163Sshin */
109555163Sshinvoid
1096243232Shrsriprecv(void)
109755163Sshin{
109855163Sshin	struct	ifc *ifcp, *ic;
109955163Sshin	struct	sockaddr_in6 fsock;
110055163Sshin	struct	in6_addr nh;	/* next hop */
110155163Sshin	struct	rip6 *rp;
110255163Sshin	struct	netinfo6 *np, *nq;
110355163Sshin	struct	riprt *rrt;
1104122677Sume	ssize_t	len, nn;
1105122677Sume	unsigned int need_trigger, idx;
110655163Sshin	char	buf[4 * RIP6_MAXMTU];
110755163Sshin	time_t	t;
110855163Sshin	struct msghdr m;
110955163Sshin	struct cmsghdr *cm;
111055163Sshin	struct iovec iov[2];
111155163Sshin	u_char cmsgbuf[256];
1112164339Ssuz	struct in6_pktinfo *pi = NULL;
1113164339Ssuz	int *hlimp = NULL;
111455163Sshin	struct iff *iffp;
111555163Sshin	struct in6_addr ia;
111655163Sshin	int ok;
111778064Sume	time_t t_half_lifetime;
111855163Sshin
111955163Sshin	need_trigger = 0;
112062921Sume
112155163Sshin	m.msg_name = (caddr_t)&fsock;
112255163Sshin	m.msg_namelen = sizeof(fsock);
112355163Sshin	iov[0].iov_base = (caddr_t)buf;
112455163Sshin	iov[0].iov_len = sizeof(buf);
112555163Sshin	m.msg_iov = iov;
112655163Sshin	m.msg_iovlen = 1;
112755163Sshin	cm = (struct cmsghdr *)cmsgbuf;
112855163Sshin	m.msg_control = (caddr_t)cm;
112955163Sshin	m.msg_controllen = sizeof(cmsgbuf);
1130314427Sasomers	m.msg_flags = 0;
113178064Sume	if ((len = recvmsg(ripsock, &m, 0)) < 0) {
113255163Sshin		fatal("recvmsg");
113378064Sume		/*NOTREACHED*/
113478064Sume	}
113578064Sume	idx = 0;
113655163Sshin	for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&m);
113755163Sshin	     cm;
113855163Sshin	     cm = (struct cmsghdr *)CMSG_NXTHDR(&m, cm)) {
1139164339Ssuz		if (cm->cmsg_level != IPPROTO_IPV6)
1140164339Ssuz		    continue;
1141164339Ssuz		switch (cm->cmsg_type) {
1142164339Ssuz		case IPV6_PKTINFO:
1143164339Ssuz			if (cm->cmsg_len != CMSG_LEN(sizeof(*pi))) {
1144164339Ssuz				trace(1,
1145164339Ssuz				    "invalid cmsg length for IPV6_PKTINFO\n");
1146164339Ssuz				return;
1147164339Ssuz			}
114855163Sshin			pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
114978064Sume			idx = pi->ipi6_ifindex;
115055163Sshin			break;
1151164339Ssuz		case IPV6_HOPLIMIT:
1152164339Ssuz			if (cm->cmsg_len != CMSG_LEN(sizeof(int))) {
1153164339Ssuz				trace(1,
1154164339Ssuz				    "invalid cmsg length for IPV6_HOPLIMIT\n");
1155164339Ssuz				return;
1156164339Ssuz			}
1157164339Ssuz			hlimp = (int *)CMSG_DATA(cm);
1158164339Ssuz			break;
115955163Sshin		}
116055163Sshin	}
116155163Sshin
1162243232Shrs	if ((size_t)len < sizeof(struct rip6)) {
1163121779Ssuz		trace(1, "Packet too short\n");
1164121779Ssuz		return;
1165121779Ssuz	}
1166121779Ssuz
1167164339Ssuz	if (pi == NULL || hlimp == NULL) {
1168164339Ssuz		/*
1169164339Ssuz		 * This can happen when the kernel failed to allocate memory
1170164339Ssuz		 * for the ancillary data.  Although we might be able to handle
1171164339Ssuz		 * some cases without this info, those are minor and not so
1172164339Ssuz		 * important, so it's better to discard the packet for safer
1173164339Ssuz		 * operation.
1174164339Ssuz		 */
1175164339Ssuz		trace(1, "IPv6 packet information cannot be retrieved\n");
1176164339Ssuz		return;
1177164339Ssuz	}
1178164339Ssuz
117955163Sshin	nh = fsock.sin6_addr;
118055163Sshin	nn = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
118155163Sshin		sizeof(struct netinfo6);
118255163Sshin	rp = (struct rip6 *)buf;
118355163Sshin	np = rp->rip6_nets;
118455163Sshin
1185119035Sume	if (rp->rip6_vers != RIP6_VERSION) {
118655163Sshin		trace(1, "Incorrect RIP version %d\n", rp->rip6_vers);
118755163Sshin		return;
118855163Sshin	}
118955163Sshin	if (rp->rip6_cmd == RIP6_REQUEST) {
119078064Sume		if (idx && idx < nindex2ifc) {
119178064Sume			ifcp = index2ifc[idx];
119255163Sshin			riprequest(ifcp, np, nn, &fsock);
119355163Sshin		} else {
119455163Sshin			riprequest(NULL, np, nn, &fsock);
119555163Sshin		}
1196312022Sngie		return;
1197312022Sngie	}
119855163Sshin
119955163Sshin	if (!IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr)) {
1200164339Ssuz		trace(1, "Response from non-ll addr: %s\n",
120178064Sume		    inet6_n2p(&fsock.sin6_addr));
120255163Sshin		return;		/* Ignore packets from non-link-local addr */
120355163Sshin	}
1204164339Ssuz	if (ntohs(fsock.sin6_port) != RIP6_PORT) {
1205164339Ssuz		trace(1, "Response from non-rip port from %s\n",
1206164339Ssuz		    inet6_n2p(&fsock.sin6_addr));
1207164339Ssuz		return;
1208164339Ssuz	}
1209164339Ssuz	if (IN6_IS_ADDR_MULTICAST(&pi->ipi6_addr) && *hlimp != 255) {
1210164339Ssuz		trace(1,
1211164339Ssuz		    "Response packet with a smaller hop limit (%d) from %s\n",
1212164339Ssuz		    *hlimp, inet6_n2p(&fsock.sin6_addr));
1213164339Ssuz		return;
1214164339Ssuz	}
1215164339Ssuz	/*
1216164339Ssuz	 * Further validation: since this program does not send off-link
1217164339Ssuz	 * requests, an incoming response must always come from an on-link
1218164339Ssuz	 * node.  Although this is normally ensured by the source address
1219164339Ssuz	 * check above, it may not 100% be safe because there are router
1220164339Ssuz	 * implementations that (invalidly) allow a packet with a link-local
1221164339Ssuz	 * source address to be forwarded to a different link.
1222164339Ssuz	 * So we also check whether the destination address is a link-local
1223164339Ssuz	 * address or the hop limit is 255.  Note that RFC2080 does not require
1224312022Sngie	 * the specific hop limit for a unicast response, so we cannot assume
1225164339Ssuz	 * the limitation.
1226164339Ssuz	 */
1227164339Ssuz	if (!IN6_IS_ADDR_LINKLOCAL(&pi->ipi6_addr) && *hlimp != 255) {
1228164339Ssuz		trace(1,
1229164339Ssuz		    "Response packet possibly from an off-link node: "
1230164339Ssuz		    "from %s to %s hlim=%d\n",
1231164339Ssuz		    inet6_n2p(&fsock.sin6_addr),
1232164339Ssuz		    inet6_n2p(&pi->ipi6_addr), *hlimp);
1233164339Ssuz		return;
1234164339Ssuz	}
1235164339Ssuz
1236243231Shrs	idx = fsock.sin6_scope_id;
123778064Sume	ifcp = (idx < nindex2ifc) ? index2ifc[idx] : NULL;
123855163Sshin	if (!ifcp) {
123978064Sume		trace(1, "Packets to unknown interface index %d\n", idx);
124055163Sshin		return;		/* Ignore it */
124155163Sshin	}
124255163Sshin	if (IN6_ARE_ADDR_EQUAL(&ifcp->ifc_mylladdr, &fsock.sin6_addr))
124355163Sshin		return;		/* The packet is from me; ignore */
124455163Sshin	if (rp->rip6_cmd != RIP6_RESPONSE) {
124555163Sshin		trace(1, "Invalid command %d\n", rp->rip6_cmd);
1246312022Sngie		return;
124755163Sshin	}
124878064Sume
124978064Sume	/* -N: no use */
1250243232Shrs	if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
125155163Sshin		return;
125278064Sume
1253228674Sdim	tracet(1, "Recv(%s): from %s.%d info(%zd)\n",
125478064Sume	    ifcp->ifc_name, inet6_n2p(&nh), ntohs(fsock.sin6_port), nn);
125555163Sshin
125655163Sshin	t = time(NULL);
125778064Sume	t_half_lifetime = t - (RIP_LIFETIME/2);
125855163Sshin	for (; nn; nn--, np++) {
125955163Sshin		if (np->rip6_metric == NEXTHOP_METRIC) {
126055163Sshin			/* modify neighbor address */
126155163Sshin			if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
126255163Sshin				nh = np->rip6_dest;
126355163Sshin				trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
126455163Sshin			} else if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest)) {
126555163Sshin				nh = fsock.sin6_addr;
126655163Sshin				trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
126755163Sshin			} else {
126855163Sshin				nh = fsock.sin6_addr;
126955163Sshin				trace(1, "\tInvalid Nexthop: %s\n",
127078064Sume				    inet6_n2p(&np->rip6_dest));
127155163Sshin			}
127255163Sshin			continue;
127355163Sshin		}
127455163Sshin		if (IN6_IS_ADDR_MULTICAST(&np->rip6_dest)) {
127555163Sshin			trace(1, "\tMulticast netinfo6: %s/%d [%d]\n",
127655163Sshin				inet6_n2p(&np->rip6_dest),
127755163Sshin				np->rip6_plen, np->rip6_metric);
127855163Sshin			continue;
127955163Sshin		}
128055163Sshin		if (IN6_IS_ADDR_LOOPBACK(&np->rip6_dest)) {
128155163Sshin			trace(1, "\tLoopback netinfo6: %s/%d [%d]\n",
128255163Sshin				inet6_n2p(&np->rip6_dest),
128355163Sshin				np->rip6_plen, np->rip6_metric);
128455163Sshin			continue;
128555163Sshin		}
128655163Sshin		if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
128755163Sshin			trace(1, "\tLink Local netinfo6: %s/%d [%d]\n",
128855163Sshin				inet6_n2p(&np->rip6_dest),
128955163Sshin				np->rip6_plen, np->rip6_metric);
129055163Sshin			continue;
129155163Sshin		}
129255163Sshin		/* may need to pass sitelocal prefix in some case, however*/
129355163Sshin		if (IN6_IS_ADDR_SITELOCAL(&np->rip6_dest) && !lflag) {
129455163Sshin			trace(1, "\tSite Local netinfo6: %s/%d [%d]\n",
129555163Sshin				inet6_n2p(&np->rip6_dest),
129655163Sshin				np->rip6_plen, np->rip6_metric);
129755163Sshin			continue;
129855163Sshin		}
129955163Sshin		trace(2, "\tnetinfo6: %s/%d [%d]",
130055163Sshin			inet6_n2p(&np->rip6_dest),
130155163Sshin			np->rip6_plen, np->rip6_metric);
130255163Sshin		if (np->rip6_tag)
130355163Sshin			trace(2, "  tag=0x%04x", ntohs(np->rip6_tag) & 0xffff);
130462607Sitojun		if (dflag >= 2) {
130562607Sitojun			ia = np->rip6_dest;
130662607Sitojun			applyplen(&ia, np->rip6_plen);
130762607Sitojun			if (!IN6_ARE_ADDR_EQUAL(&ia, &np->rip6_dest))
130862607Sitojun				trace(2, " [junk outside prefix]");
130962607Sitojun		}
131055163Sshin
131178064Sume		/*
131278064Sume		 * -L: listen only if the prefix matches the configuration
131378064Sume		 */
1314243232Shrs                ok = 1;	/* if there's no L filter, it is ok */
1315243232Shrs                TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
1316243232Shrs                        if (iffp->iff_type != IFIL_TYPE_L)
1317243232Shrs                                continue;
1318243232Shrs                        ok = 0;
1319243232Shrs                        if (np->rip6_plen < iffp->iff_plen)
1320243232Shrs                                continue;
1321243232Shrs                        /* special rule: ::/0 means default, not "in /0" */
1322243232Shrs                        if (iffp->iff_plen == 0 && np->rip6_plen > 0)
1323243232Shrs                                continue;
1324312022Sngie                        ia = np->rip6_dest;
1325243232Shrs                        applyplen(&ia, iffp->iff_plen);
1326243232Shrs                        if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1327243232Shrs                                ok = 1;
1328243232Shrs                                break;
1329243232Shrs                        }
1330243232Shrs                }
133155163Sshin		if (!ok) {
133255163Sshin			trace(2, "  (filtered)\n");
133355163Sshin			continue;
133455163Sshin		}
133555163Sshin
133655163Sshin		trace(2, "\n");
133755163Sshin		np->rip6_metric++;
133855163Sshin		np->rip6_metric += ifcp->ifc_metric;
133955163Sshin		if (np->rip6_metric > HOPCNT_INFINITY6)
134055163Sshin			np->rip6_metric = HOPCNT_INFINITY6;
134155163Sshin
134255163Sshin		applyplen(&np->rip6_dest, np->rip6_plen);
1343243232Shrs		if ((rrt = rtsearch(np)) != NULL) {
134455163Sshin			if (rrt->rrt_t == 0)
134555163Sshin				continue;	/* Intf route has priority */
134655163Sshin			nq = &rrt->rrt_info;
134755163Sshin			if (nq->rip6_metric > np->rip6_metric) {
134855163Sshin				if (rrt->rrt_index == ifcp->ifc_index &&
134955163Sshin				    IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
135055163Sshin					/* Small metric from the same gateway */
135155163Sshin					nq->rip6_metric = np->rip6_metric;
135255163Sshin				} else {
135355163Sshin					/* Better route found */
135455163Sshin					rrt->rrt_index = ifcp->ifc_index;
135555163Sshin					/* Update routing table */
135655163Sshin					delroute(nq, &rrt->rrt_gw);
135755163Sshin					rrt->rrt_gw = nh;
135855163Sshin					*nq = *np;
135955163Sshin					addroute(rrt, &nh, ifcp);
136055163Sshin				}
136162607Sitojun				rrt->rrt_rflags |= RRTF_CHANGED;
136255163Sshin				rrt->rrt_t = t;
136355163Sshin				need_trigger = 1;
136455163Sshin			} else if (nq->rip6_metric < np->rip6_metric &&
136555163Sshin				   rrt->rrt_index == ifcp->ifc_index &&
136655163Sshin				   IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
136755163Sshin				/* Got worse route from same gw */
136855163Sshin				nq->rip6_metric = np->rip6_metric;
136955163Sshin				rrt->rrt_t = t;
137062607Sitojun				rrt->rrt_rflags |= RRTF_CHANGED;
137155163Sshin				need_trigger = 1;
137255163Sshin			} else if (nq->rip6_metric == np->rip6_metric &&
137355163Sshin				   np->rip6_metric < HOPCNT_INFINITY6) {
137478064Sume				if (rrt->rrt_index == ifcp->ifc_index &&
1375312022Sngie				   IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
137678064Sume					/* same metric, same route from same gw */
137778064Sume					rrt->rrt_t = t;
137878064Sume				} else if (rrt->rrt_t < t_half_lifetime) {
137978064Sume					/* Better route found */
138078064Sume					rrt->rrt_index = ifcp->ifc_index;
138178064Sume					/* Update routing table */
138278064Sume					delroute(nq, &rrt->rrt_gw);
138378064Sume					rrt->rrt_gw = nh;
138478064Sume					*nq = *np;
138578064Sume					addroute(rrt, &nh, ifcp);
138678064Sume					rrt->rrt_rflags |= RRTF_CHANGED;
138778064Sume					rrt->rrt_t = t;
138878064Sume				}
138955163Sshin			}
1390312022Sngie			/*
139155163Sshin			 * if nq->rip6_metric == HOPCNT_INFINITY6 then
139278064Sume			 * do not update age value.  Do nothing.
139355163Sshin			 */
139455163Sshin		} else if (np->rip6_metric < HOPCNT_INFINITY6) {
139555163Sshin			/* Got a new valid route */
139678064Sume			if ((rrt = MALLOC(struct riprt)) == NULL) {
139755163Sshin				fatal("malloc: struct riprt");
139878064Sume				/*NOTREACHED*/
139978064Sume			}
140062607Sitojun			memset(rrt, 0, sizeof(*rrt));
140155163Sshin			nq = &rrt->rrt_info;
140255163Sshin
140355163Sshin			rrt->rrt_same = NULL;
140455163Sshin			rrt->rrt_index = ifcp->ifc_index;
140555163Sshin			rrt->rrt_flags = RTF_UP|RTF_GATEWAY;
140655163Sshin			rrt->rrt_gw = nh;
140755163Sshin			*nq = *np;
140855163Sshin			applyplen(&nq->rip6_dest, nq->rip6_plen);
140955163Sshin			if (nq->rip6_plen == sizeof(struct in6_addr) * 8)
141055163Sshin				rrt->rrt_flags |= RTF_HOST;
141155163Sshin
141255163Sshin			/* Update routing table */
141355163Sshin			addroute(rrt, &nh, ifcp);
141462607Sitojun			rrt->rrt_rflags |= RRTF_CHANGED;
141555163Sshin			need_trigger = 1;
141655163Sshin			rrt->rrt_t = t;
1417243232Shrs
1418243232Shrs			/* Put the route to the list */
1419243232Shrs			TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
142055163Sshin		}
142155163Sshin	}
142255163Sshin	/* XXX need to care the interval between triggered updates */
142355163Sshin	if (need_trigger) {
142455163Sshin		if (nextalarm > time(NULL) + RIP_TRIG_INT6_MAX) {
1425243232Shrs			TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
142655163Sshin				if (ifcp->ifc_index == ic->ifc_index)
142755163Sshin					continue;
142855163Sshin				if (ic->ifc_flags & IFF_UP)
142955163Sshin					ripsend(ic, &ic->ifc_ripsin,
143062607Sitojun						RRTF_CHANGED);
143155163Sshin			}
143255163Sshin		}
143355163Sshin		/* Reset the flag */
1434243232Shrs		TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
143562607Sitojun			rrt->rrt_rflags &= ~RRTF_CHANGED;
1436243232Shrs		}
143755163Sshin	}
143855163Sshin}
143955163Sshin
144055163Sshin/*
144155163Sshin * Send all routes request packet to the specified interface.
144255163Sshin */
144355163Sshinvoid
1444243232Shrssendrequest(struct ifc *ifcp)
144555163Sshin{
144655163Sshin	struct netinfo6 *np;
144755163Sshin	int error;
144855163Sshin
144955163Sshin	if (ifcp->ifc_flags & IFF_LOOPBACK)
145055163Sshin		return;
145155163Sshin	ripbuf->rip6_cmd = RIP6_REQUEST;
145255163Sshin	np = ripbuf->rip6_nets;
145355163Sshin	memset(np, 0, sizeof(struct netinfo6));
145455163Sshin	np->rip6_metric = HOPCNT_INFINITY6;
145555163Sshin	tracet(1, "Send rtdump Request to %s (%s)\n",
145655163Sshin		ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
145755163Sshin	error = sendpacket(&ifcp->ifc_ripsin, RIPSIZE(1));
145855163Sshin	if (error == EAFNOSUPPORT) {
145955163Sshin		/* Protocol not supported */
146055163Sshin		tracet(1, "Could not send rtdump Request to %s (%s): "
146155163Sshin			"set IFF_UP to 0\n",
146255163Sshin			ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
146355163Sshin		ifcp->ifc_flags &= ~IFF_UP;	/* As if down for AF_INET6 */
146455163Sshin	}
146555163Sshin	ripbuf->rip6_cmd = RIP6_RESPONSE;
146655163Sshin}
146755163Sshin
146855163Sshin/*
146955163Sshin * Process a RIP6_REQUEST packet.
147055163Sshin */
147155163Sshinvoid
1472243232Shrsriprequest(struct ifc *ifcp,
1473243232Shrs	struct netinfo6 *np,
1474243232Shrs	int nn,
1475243232Shrs	struct sockaddr_in6 *sin6)
147655163Sshin{
147755163Sshin	int i;
147855163Sshin	struct riprt *rrt;
147955163Sshin
148055163Sshin	if (!(nn == 1 && IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest) &&
148155163Sshin	      np->rip6_plen == 0 && np->rip6_metric == HOPCNT_INFINITY6)) {
148255163Sshin		/* Specific response, don't split-horizon */
148355163Sshin		trace(1, "\tRIP Request\n");
148455163Sshin		for (i = 0; i < nn; i++, np++) {
1485243232Shrs			rrt = rtsearch(np);
148655163Sshin			if (rrt)
148755163Sshin				np->rip6_metric = rrt->rrt_info.rip6_metric;
148855163Sshin			else
148955163Sshin				np->rip6_metric = HOPCNT_INFINITY6;
149055163Sshin		}
1491119031Sume		(void)sendpacket(sin6, RIPSIZE(nn));
149255163Sshin		return;
149355163Sshin	}
149455163Sshin	/* Whole routing table dump */
149555163Sshin	trace(1, "\tRIP Request -- whole routing table\n");
1496119031Sume	ripsend(ifcp, sin6, RRTF_SENDANYWAY);
149755163Sshin}
149855163Sshin
149955163Sshin/*
150055163Sshin * Get information of each interface.
150155163Sshin */
150255163Sshinvoid
1503243232Shrsifconfig(void)
150455163Sshin{
150562607Sitojun	struct ifaddrs *ifap, *ifa;
150662607Sitojun	struct ifc *ifcp;
150762607Sitojun	struct ipv6_mreq mreq;
150862607Sitojun	int s;
150962607Sitojun
151078064Sume	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
151162607Sitojun		fatal("socket");
151278064Sume		/*NOTREACHED*/
151378064Sume	}
151462607Sitojun
151578064Sume	if (getifaddrs(&ifap) != 0) {
151662607Sitojun		fatal("getifaddrs");
151778064Sume		/*NOTREACHED*/
151878064Sume	}
151962607Sitojun
152062607Sitojun	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
152162607Sitojun		if (ifa->ifa_addr->sa_family != AF_INET6)
152262607Sitojun			continue;
152362607Sitojun		ifcp = ifc_find(ifa->ifa_name);
152462607Sitojun		/* we are interested in multicast-capable interfaces */
152562607Sitojun		if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
152662607Sitojun			continue;
152762607Sitojun		if (!ifcp) {
152862607Sitojun			/* new interface */
152978064Sume			if ((ifcp = MALLOC(struct ifc)) == NULL) {
153062607Sitojun				fatal("malloc: struct ifc");
153178064Sume				/*NOTREACHED*/
153278064Sume			}
153362607Sitojun			memset(ifcp, 0, sizeof(*ifcp));
1534243232Shrs
153562607Sitojun			ifcp->ifc_index = -1;
1536243232Shrs			strlcpy(ifcp->ifc_name, ifa->ifa_name,
1537243232Shrs			    sizeof(ifcp->ifc_name));
1538243232Shrs			TAILQ_INIT(&ifcp->ifc_ifac_head);
1539243232Shrs			TAILQ_INIT(&ifcp->ifc_iff_head);
154062607Sitojun			ifcp->ifc_flags = ifa->ifa_flags;
1541243232Shrs			TAILQ_INSERT_HEAD(&ifc_head, ifcp, ifc_next);
154262607Sitojun			trace(1, "newif %s <%s>\n", ifcp->ifc_name,
154362607Sitojun				ifflags(ifcp->ifc_flags));
154462607Sitojun			if (!strcmp(ifcp->ifc_name, LOOPBACK_IF))
154562607Sitojun				loopifcp = ifcp;
154662607Sitojun		} else {
154762607Sitojun			/* update flag, this may be up again */
154862607Sitojun			if (ifcp->ifc_flags != ifa->ifa_flags) {
154962607Sitojun				trace(1, "%s: <%s> -> ", ifcp->ifc_name,
155062607Sitojun					ifflags(ifcp->ifc_flags));
155162607Sitojun				trace(1, "<%s>\n", ifflags(ifa->ifa_flags));
155278064Sume				ifcp->ifc_cflags |= IFC_CHANGED;
155362607Sitojun			}
155462607Sitojun			ifcp->ifc_flags = ifa->ifa_flags;
155562607Sitojun		}
1556243232Shrs		if (ifconfig1(ifa->ifa_name, ifa->ifa_addr, ifcp, s) < 0) {
1557243232Shrs			/* maybe temporary failure */
1558243232Shrs			continue;
1559243232Shrs		}
156062607Sitojun		if ((ifcp->ifc_flags & (IFF_LOOPBACK | IFF_UP)) == IFF_UP
156162607Sitojun		 && 0 < ifcp->ifc_index && !ifcp->ifc_joined) {
156262607Sitojun			mreq.ipv6mr_multiaddr = ifcp->ifc_ripsin.sin6_addr;
156362607Sitojun			mreq.ipv6mr_interface = ifcp->ifc_index;
156478064Sume			if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
156578064Sume			    &mreq, sizeof(mreq)) < 0) {
156662607Sitojun				fatal("IPV6_JOIN_GROUP");
156778064Sume				/*NOTREACHED*/
156878064Sume			}
156962607Sitojun			trace(1, "join %s %s\n", ifcp->ifc_name, RIP6_DEST);
157062607Sitojun			ifcp->ifc_joined++;
157162607Sitojun		}
157262607Sitojun	}
157362607Sitojun	close(s);
157462607Sitojun	freeifaddrs(ifap);
157555163Sshin}
157655163Sshin
1577243232Shrsint
1578243232Shrsifconfig1(const char *name,
1579243232Shrs	const struct sockaddr *sa,
1580243232Shrs	struct ifc *ifcp,
1581243232Shrs	int s)
158255163Sshin{
158355163Sshin	struct	in6_ifreq ifr;
1584119031Sume	const struct sockaddr_in6 *sin6;
1585243232Shrs	struct	ifac *ifac;
158655163Sshin	int	plen;
158755163Sshin	char	buf[BUFSIZ];
158855163Sshin
1589119031Sume	sin6 = (const struct sockaddr_in6 *)sa;
1590122677Sume	if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && !lflag)
1591243232Shrs		return (-1);
1592119031Sume	ifr.ifr_addr = *sin6;
1593299869Struckman	strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
159478064Sume	if (ioctl(s, SIOCGIFNETMASK_IN6, (char *)&ifr) < 0) {
1595243232Shrs		syslog(LOG_INFO, "ioctl: SIOCGIFNETMASK_IN6");
1596243232Shrs		return (-1);
159778064Sume	}
159878064Sume	plen = sin6mask2len(&ifr.ifr_addr);
1599243232Shrs	if ((ifac = ifa_match(ifcp, &sin6->sin6_addr, plen)) != NULL) {
160055163Sshin		/* same interface found */
160155163Sshin		/* need check if something changed */
160255163Sshin		/* XXX not yet implemented */
1603243232Shrs		return (-1);
160455163Sshin	}
160555163Sshin	/*
160655163Sshin	 * New address is found
160755163Sshin	 */
1608243232Shrs	if ((ifac = MALLOC(struct ifac)) == NULL) {
160955163Sshin		fatal("malloc: struct ifac");
161078064Sume		/*NOTREACHED*/
161178064Sume	}
1612243232Shrs	memset(ifac, 0, sizeof(*ifac));
1613243232Shrs
1614243232Shrs	ifac->ifac_ifc = ifcp;
1615243232Shrs	ifac->ifac_addr = sin6->sin6_addr;
1616243232Shrs	ifac->ifac_plen = plen;
1617243232Shrs	ifac->ifac_scope_id = sin6->sin6_scope_id;
161855163Sshin	if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1619119031Sume		ifr.ifr_addr = *sin6;
162078064Sume		if (ioctl(s, SIOCGIFDSTADDR_IN6, (char *)&ifr) < 0) {
162155163Sshin			fatal("ioctl: SIOCGIFDSTADDR_IN6");
162278064Sume			/*NOTREACHED*/
162378064Sume		}
1624243232Shrs		ifac->ifac_raddr = ifr.ifr_dstaddr.sin6_addr;
1625243232Shrs		inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr, buf,
1626243232Shrs		    sizeof(buf));
162755163Sshin		trace(1, "found address %s/%d -- %s\n",
1628243232Shrs			inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen, buf);
162955163Sshin	} else {
163055163Sshin		trace(1, "found address %s/%d\n",
1631243232Shrs			inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen);
163255163Sshin	}
1633243232Shrs	if (ifcp->ifc_index < 0 && IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
1634243232Shrs		ifcp->ifc_mylladdr = ifac->ifac_addr;
1635243232Shrs		ifcp->ifc_index = ifac->ifac_scope_id;
163655163Sshin		memcpy(&ifcp->ifc_ripsin, &ripsin, ripsin.ss_len);
1637243231Shrs		ifcp->ifc_ripsin.sin6_scope_id = ifcp->ifc_index;
163855163Sshin		setindex2ifc(ifcp->ifc_index, ifcp);
163955163Sshin		ifcp->ifc_mtu = getifmtu(ifcp->ifc_index);
164055163Sshin		if (ifcp->ifc_mtu > RIP6_MAXMTU)
164155163Sshin			ifcp->ifc_mtu = RIP6_MAXMTU;
164278064Sume		if (ioctl(s, SIOCGIFMETRIC, (char *)&ifr) < 0) {
164355163Sshin			fatal("ioctl: SIOCGIFMETRIC");
164478064Sume			/*NOTREACHED*/
164578064Sume		}
164655163Sshin		ifcp->ifc_metric = ifr.ifr_metric;
164755163Sshin		trace(1, "\tindex: %d, mtu: %d, metric: %d\n",
164855163Sshin			ifcp->ifc_index, ifcp->ifc_mtu, ifcp->ifc_metric);
164978064Sume	} else
165078064Sume		ifcp->ifc_cflags |= IFC_CHANGED;
1651243232Shrs
1652243232Shrs	TAILQ_INSERT_HEAD(&ifcp->ifc_ifac_head, ifac, ifac_next);
1653243232Shrs
1654243232Shrs	return 0;
165555163Sshin}
165655163Sshin
1657243233Shrsvoid
1658243233Shrsifremove(int ifindex)
1659243233Shrs{
1660243233Shrs	struct ifc *ifcp;
1661243233Shrs	struct riprt *rrt;
1662243233Shrs
1663243233Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
1664243233Shrs		if (ifcp->ifc_index == ifindex)
1665243233Shrs			break;
1666243233Shrs	}
1667243233Shrs	if (ifcp == NULL)
1668312022Sngie		return;
1669243233Shrs
1670243233Shrs	tracet(1, "ifremove: %s is departed.\n", ifcp->ifc_name);
1671243233Shrs	TAILQ_REMOVE(&ifc_head, ifcp, ifc_next);
1672243233Shrs
1673243233Shrs	TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1674243233Shrs		if (rrt->rrt_index == ifcp->ifc_index &&
1675243233Shrs		    rrt->rrt_rflags & RRTF_AGGREGATE)
1676243233Shrs			delroute(&rrt->rrt_info, &rrt->rrt_gw);
1677243233Shrs	}
1678243233Shrs	free(ifcp);
1679243233Shrs}
1680243233Shrs
168155163Sshin/*
168255163Sshin * Receive and process routing messages.
168355163Sshin * Update interface information as necesssary.
168455163Sshin */
168555163Sshinvoid
1686243232Shrsrtrecv(void)
168755163Sshin{
168855163Sshin	char buf[BUFSIZ];
1689243232Shrs	char *p, *q = NULL;
169055163Sshin	struct rt_msghdr *rtm;
169155163Sshin	struct ifa_msghdr *ifam;
169255163Sshin	struct if_msghdr *ifm;
1693243233Shrs	struct if_announcemsghdr *ifan;
169455163Sshin	int len;
169578064Sume	struct ifc *ifcp, *ic;
169655163Sshin	int iface = 0, rtable = 0;
169755163Sshin	struct sockaddr_in6 *rta[RTAX_MAX];
169878064Sume	struct sockaddr_in6 mask;
1699243232Shrs	int i, addrs = 0;
170078064Sume	struct riprt *rrt;
170155163Sshin
170255163Sshin	if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
170355163Sshin		perror("read from rtsock");
170478064Sume		exit(1);
170555163Sshin	}
1706243232Shrs	if (len == 0)
1707243232Shrs		return;
1708243232Shrs#if 0
170955163Sshin	if (len < sizeof(*rtm)) {
171069279Sume		trace(1, "short read from rtsock: %d (should be > %lu)\n",
171169279Sume			len, (u_long)sizeof(*rtm));
171255163Sshin		return;
171355163Sshin	}
1714243232Shrs#endif
1715119042Sume	if (dflag >= 2) {
1716119042Sume		fprintf(stderr, "rtmsg:\n");
1717119042Sume		for (i = 0; i < len; i++) {
1718119042Sume			fprintf(stderr, "%02x ", buf[i] & 0xff);
1719119042Sume			if (i % 16 == 15) fprintf(stderr, "\n");
1720119042Sume		}
1721119042Sume		fprintf(stderr, "\n");
1722119042Sume	}
172355163Sshin
172455163Sshin	for (p = buf; p - buf < len; p += ((struct rt_msghdr *)p)->rtm_msglen) {
1725243232Shrs		if (((struct rt_msghdr *)p)->rtm_version != RTM_VERSION)
1726243232Shrs			continue;
1727243232Shrs
172855163Sshin		/* safety against bogus message */
172955163Sshin		if (((struct rt_msghdr *)p)->rtm_msglen <= 0) {
173055163Sshin			trace(1, "bogus rtmsg: length=%d\n",
173155163Sshin				((struct rt_msghdr *)p)->rtm_msglen);
173255163Sshin			break;
173355163Sshin		}
173455163Sshin		rtm = NULL;
173555163Sshin		ifam = NULL;
173655163Sshin		ifm = NULL;
173755163Sshin		switch (((struct rt_msghdr *)p)->rtm_type) {
173855163Sshin		case RTM_NEWADDR:
173955163Sshin		case RTM_DELADDR:
174055163Sshin			ifam = (struct ifa_msghdr *)p;
174155163Sshin			addrs = ifam->ifam_addrs;
174255163Sshin			q = (char *)(ifam + 1);
174355163Sshin			break;
174455163Sshin		case RTM_IFINFO:
174555163Sshin			ifm = (struct if_msghdr *)p;
174655163Sshin			addrs = ifm->ifm_addrs;
174755163Sshin			q = (char *)(ifm + 1);
174855163Sshin			break;
1749243233Shrs		case RTM_IFANNOUNCE:
1750243233Shrs			ifan = (struct if_announcemsghdr *)p;
1751243233Shrs			switch (ifan->ifan_what) {
1752243233Shrs			case IFAN_ARRIVAL:
1753243233Shrs				iface++;
1754243233Shrs				break;
1755243233Shrs			case IFAN_DEPARTURE:
1756243233Shrs				ifremove(ifan->ifan_index);
1757243233Shrs				iface++;
1758243233Shrs				break;
1759243233Shrs			}
1760243233Shrs			break;
176155163Sshin		default:
176255163Sshin			rtm = (struct rt_msghdr *)p;
176355163Sshin			addrs = rtm->rtm_addrs;
176455163Sshin			q = (char *)(rtm + 1);
176555163Sshin			if (rtm->rtm_version != RTM_VERSION) {
176655163Sshin				trace(1, "unexpected rtmsg version %d "
176755163Sshin					"(should be %d)\n",
176855163Sshin					rtm->rtm_version, RTM_VERSION);
176955163Sshin				continue;
177055163Sshin			}
177155163Sshin			if (rtm->rtm_pid == pid) {
177255163Sshin#if 0
177355163Sshin				trace(1, "rtmsg looped back to me, ignored\n");
177455163Sshin#endif
177555163Sshin				continue;
177655163Sshin			}
177755163Sshin			break;
177855163Sshin		}
177955163Sshin		memset(&rta, 0, sizeof(rta));
178055163Sshin		for (i = 0; i < RTAX_MAX; i++) {
178155163Sshin			if (addrs & (1 << i)) {
178255163Sshin				rta[i] = (struct sockaddr_in6 *)q;
178355163Sshin				q += ROUNDUP(rta[i]->sin6_len);
178455163Sshin			}
178555163Sshin		}
178655163Sshin
178755163Sshin		trace(1, "rtsock: %s (addrs=%x)\n",
178855163Sshin			rttypes((struct rt_msghdr *)p), addrs);
178955163Sshin		if (dflag >= 2) {
179055163Sshin			for (i = 0;
179155163Sshin			     i < ((struct rt_msghdr *)p)->rtm_msglen;
179255163Sshin			     i++) {
179355163Sshin				fprintf(stderr, "%02x ", p[i] & 0xff);
179455163Sshin				if (i % 16 == 15) fprintf(stderr, "\n");
179555163Sshin			}
179655163Sshin			fprintf(stderr, "\n");
179755163Sshin		}
179855163Sshin
179955163Sshin		/*
180055163Sshin		 * Easy ones first.
180155163Sshin		 *
180255163Sshin		 * We may be able to optimize by using ifm->ifm_index or
180355163Sshin		 * ifam->ifam_index.  For simplicity we don't do that here.
180455163Sshin		 */
180555163Sshin		switch (((struct rt_msghdr *)p)->rtm_type) {
180655163Sshin		case RTM_NEWADDR:
180755163Sshin		case RTM_IFINFO:
180855163Sshin			iface++;
180955163Sshin			continue;
181055163Sshin		case RTM_ADD:
181155163Sshin			rtable++;
181255163Sshin			continue;
181355163Sshin		case RTM_LOSING:
181455163Sshin		case RTM_MISS:
181555163Sshin		case RTM_GET:
181655163Sshin		case RTM_LOCK:
181755163Sshin			/* nothing to be done here */
181855163Sshin			trace(1, "\tnothing to be done, ignored\n");
181955163Sshin			continue;
182055163Sshin		}
182155163Sshin
182255163Sshin#if 0
182355163Sshin		if (rta[RTAX_DST] == NULL) {
182455163Sshin			trace(1, "\tno destination, ignored\n");
1825312022Sngie			continue;
182655163Sshin		}
182755163Sshin		if (rta[RTAX_DST]->sin6_family != AF_INET6) {
182855163Sshin			trace(1, "\taf mismatch, ignored\n");
182955163Sshin			continue;
183055163Sshin		}
183155163Sshin		if (IN6_IS_ADDR_LINKLOCAL(&rta[RTAX_DST]->sin6_addr)) {
183255163Sshin			trace(1, "\tlinklocal destination, ignored\n");
183355163Sshin			continue;
183455163Sshin		}
183555163Sshin		if (IN6_ARE_ADDR_EQUAL(&rta[RTAX_DST]->sin6_addr, &in6addr_loopback)) {
183655163Sshin			trace(1, "\tloopback destination, ignored\n");
183755163Sshin			continue;		/* Loopback */
183855163Sshin		}
183955163Sshin		if (IN6_IS_ADDR_MULTICAST(&rta[RTAX_DST]->sin6_addr)) {
184055163Sshin			trace(1, "\tmulticast destination, ignored\n");
184155163Sshin			continue;
184255163Sshin		}
184355163Sshin#endif
184455163Sshin
184555163Sshin		/* hard ones */
184655163Sshin		switch (((struct rt_msghdr *)p)->rtm_type) {
184755163Sshin		case RTM_NEWADDR:
184855163Sshin		case RTM_IFINFO:
184955163Sshin		case RTM_ADD:
185055163Sshin		case RTM_LOSING:
185155163Sshin		case RTM_MISS:
185255163Sshin		case RTM_GET:
185355163Sshin		case RTM_LOCK:
185455163Sshin			/* should already be handled */
185555163Sshin			fatal("rtrecv: never reach here");
185678064Sume			/*NOTREACHED*/
185755163Sshin		case RTM_DELETE:
185878064Sume			if (!rta[RTAX_DST] || !rta[RTAX_GATEWAY]) {
185978064Sume				trace(1, "\tsome of dst/gw/netamsk are "
186078064Sume				    "unavailable, ignored\n");
186155163Sshin				break;
186255163Sshin			}
186378064Sume			if ((rtm->rtm_flags & RTF_HOST) != 0) {
186478064Sume				mask.sin6_len = sizeof(mask);
186578064Sume				memset(&mask.sin6_addr, 0xff,
186678064Sume				    sizeof(mask.sin6_addr));
186778064Sume				rta[RTAX_NETMASK] = &mask;
186878064Sume			} else if (!rta[RTAX_NETMASK]) {
186978064Sume				trace(1, "\tsome of dst/gw/netamsk are "
187078064Sume				    "unavailable, ignored\n");
187178064Sume				break;
187278064Sume			}
187378064Sume			if (rt_del(rta[RTAX_DST], rta[RTAX_GATEWAY],
187478064Sume			    rta[RTAX_NETMASK]) == 0) {
187555163Sshin				rtable++;	/*just to be sure*/
187655163Sshin			}
187755163Sshin			break;
187855163Sshin		case RTM_CHANGE:
187955163Sshin		case RTM_REDIRECT:
188055163Sshin			trace(1, "\tnot supported yet, ignored\n");
188155163Sshin			break;
188255163Sshin		case RTM_DELADDR:
188355163Sshin			if (!rta[RTAX_NETMASK] || !rta[RTAX_IFA]) {
188455163Sshin				trace(1, "\tno netmask or ifa given, ignored\n");
188555163Sshin				break;
188655163Sshin			}
188755163Sshin			if (ifam->ifam_index < nindex2ifc)
188855163Sshin				ifcp = index2ifc[ifam->ifam_index];
188955163Sshin			else
189055163Sshin				ifcp = NULL;
189155163Sshin			if (!ifcp) {
189255163Sshin				trace(1, "\tinvalid ifam_index %d, ignored\n",
189355163Sshin					ifam->ifam_index);
189455163Sshin				break;
189555163Sshin			}
189678064Sume			if (!rt_deladdr(ifcp, rta[RTAX_IFA], rta[RTAX_NETMASK]))
189778064Sume				iface++;
189855163Sshin			break;
189955163Sshin		}
190055163Sshin
190155163Sshin	}
190255163Sshin
190355163Sshin	if (iface) {
190455163Sshin		trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
190555163Sshin		ifconfig();
1906243232Shrs		TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
190778064Sume			if (ifcp->ifc_cflags & IFC_CHANGED) {
190878064Sume				if (ifrt(ifcp, 1)) {
1909243232Shrs					TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
191078064Sume						if (ifcp->ifc_index == ic->ifc_index)
191178064Sume							continue;
191278064Sume						if (ic->ifc_flags & IFF_UP)
191378064Sume							ripsend(ic, &ic->ifc_ripsin,
191478064Sume							RRTF_CHANGED);
191578064Sume					}
191678064Sume					/* Reset the flag */
1917243232Shrs					TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
191878064Sume						rrt->rrt_rflags &= ~RRTF_CHANGED;
1919243232Shrs					}
192078064Sume				}
192178064Sume				ifcp->ifc_cflags &= ~IFC_CHANGED;
192278064Sume			}
1923243232Shrs		}
192455163Sshin	}
192555163Sshin	if (rtable) {
192655163Sshin		trace(1, "rtsock: read routing table again\n");
192755163Sshin		krtread(1);
192855163Sshin	}
192955163Sshin}
193055163Sshin
193155163Sshin/*
193255163Sshin * remove specified route from the internal routing table.
193355163Sshin */
193455163Sshinint
1935243232Shrsrt_del(const struct sockaddr_in6 *sdst,
1936243232Shrs	const struct sockaddr_in6 *sgw,
1937243232Shrs	const struct sockaddr_in6 *smask)
193855163Sshin{
193955163Sshin	const struct in6_addr *dst = NULL;
194055163Sshin	const struct in6_addr *gw = NULL;
194155163Sshin	int prefix;
194255163Sshin	struct netinfo6 ni6;
194355163Sshin	struct riprt *rrt = NULL;
194455163Sshin	time_t t_lifetime;
194555163Sshin
194655163Sshin	if (sdst->sin6_family != AF_INET6) {
194755163Sshin		trace(1, "\tother AF, ignored\n");
194855163Sshin		return -1;
194955163Sshin	}
195055163Sshin	if (IN6_IS_ADDR_LINKLOCAL(&sdst->sin6_addr)
195155163Sshin	 || IN6_ARE_ADDR_EQUAL(&sdst->sin6_addr, &in6addr_loopback)
195255163Sshin	 || IN6_IS_ADDR_MULTICAST(&sdst->sin6_addr)) {
195355163Sshin		trace(1, "\taddress %s not interesting, ignored\n",
195455163Sshin			inet6_n2p(&sdst->sin6_addr));
195555163Sshin		return -1;
195655163Sshin	}
195755163Sshin	dst = &sdst->sin6_addr;
195878064Sume	if (sgw->sin6_family == AF_INET6) {
195955163Sshin		/* easy case */
196055163Sshin		gw = &sgw->sin6_addr;
196178064Sume		prefix = sin6mask2len(smask);
196255163Sshin	} else if (sgw->sin6_family == AF_LINK) {
196355163Sshin		/*
196455163Sshin		 * Interface route... a hard case.  We need to get the prefix
196555163Sshin		 * length from the kernel, but we now are parsing rtmsg.
196655163Sshin		 * We'll purge matching routes from my list, then get the
196755163Sshin		 * fresh list.
196855163Sshin		 */
196955163Sshin		struct riprt *longest;
1970108533Sschweikh		trace(1, "\t%s is an interface route, guessing prefixlen\n",
197155163Sshin			inet6_n2p(dst));
197255163Sshin		longest = NULL;
1973243232Shrs		TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
197455163Sshin			if (IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
197555163Sshin					&sdst->sin6_addr)
197655163Sshin			 && IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)) {
197755163Sshin				if (!longest
197855163Sshin				 || longest->rrt_info.rip6_plen <
197955163Sshin						 rrt->rrt_info.rip6_plen) {
198055163Sshin					longest = rrt;
198155163Sshin				}
198255163Sshin			}
198355163Sshin		}
198455163Sshin		rrt = longest;
198555163Sshin		if (!rrt) {
198655163Sshin			trace(1, "\tno matching interface route found\n");
198755163Sshin			return -1;
198855163Sshin		}
198955163Sshin		gw = &in6addr_loopback;
199055163Sshin		prefix = rrt->rrt_info.rip6_plen;
199155163Sshin	} else {
199278064Sume		trace(1, "\tunsupported af: (gw=%d)\n", sgw->sin6_family);
199355163Sshin		return -1;
199455163Sshin	}
199555163Sshin
199655163Sshin	trace(1, "\tdeleting %s/%d ", inet6_n2p(dst), prefix);
199755163Sshin	trace(1, "gw %s\n", inet6_n2p(gw));
199855163Sshin	t_lifetime = time(NULL) - RIP_LIFETIME;
199955163Sshin	/* age route for interface address */
200055163Sshin	memset(&ni6, 0, sizeof(ni6));
200155163Sshin	ni6.rip6_dest = *dst;
200255163Sshin	ni6.rip6_plen = prefix;
200355163Sshin	applyplen(&ni6.rip6_dest, ni6.rip6_plen);	/*to be sure*/
200455163Sshin	trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6.rip6_dest),
200555163Sshin		ni6.rip6_plen);
2006243232Shrs	if (!rrt && (rrt = rtsearch(&ni6)) == NULL) {
200755163Sshin		trace(1, "\tno route found\n");
200855163Sshin		return -1;
200955163Sshin	}
201078064Sume#if 0
201155163Sshin	if ((rrt->rrt_flags & RTF_STATIC) == 0) {
201255163Sshin		trace(1, "\tyou can delete static routes only\n");
201378064Sume	} else
201478064Sume#endif
201578064Sume	if (!IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, gw)) {
201655163Sshin		trace(1, "\tgw mismatch: %s <-> ",
201755163Sshin			inet6_n2p(&rrt->rrt_gw));
201855163Sshin		trace(1, "%s\n", inet6_n2p(gw));
201955163Sshin	} else {
202055163Sshin		trace(1, "\troute found, age it\n");
202155163Sshin		if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
202255163Sshin			rrt->rrt_t = t_lifetime;
202355163Sshin			rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
202455163Sshin		}
202555163Sshin	}
202655163Sshin	return 0;
202755163Sshin}
202855163Sshin
202955163Sshin/*
203055163Sshin * remove specified address from internal interface/routing table.
203155163Sshin */
203255163Sshinint
2033243232Shrsrt_deladdr(struct ifc *ifcp,
2034243232Shrs	const struct sockaddr_in6 *sifa,
2035243232Shrs	const struct sockaddr_in6 *smask)
203655163Sshin{
203755163Sshin	const struct in6_addr *addr = NULL;
203855163Sshin	int prefix;
2039243232Shrs	struct ifac *ifac = NULL;
204055163Sshin	struct netinfo6 ni6;
204155163Sshin	struct riprt *rrt = NULL;
204255163Sshin	time_t t_lifetime;
204355163Sshin	int updated = 0;
204455163Sshin
204578064Sume	if (sifa->sin6_family != AF_INET6) {
204655163Sshin		trace(1, "\tother AF, ignored\n");
204755163Sshin		return -1;
204855163Sshin	}
204955163Sshin	addr = &sifa->sin6_addr;
205078064Sume	prefix = sin6mask2len(smask);
205155163Sshin
205255163Sshin	trace(1, "\tdeleting %s/%d from %s\n",
205355163Sshin		inet6_n2p(addr), prefix, ifcp->ifc_name);
2054243232Shrs	ifac = ifa_match(ifcp, addr, prefix);
2055243232Shrs	if (!ifac) {
205655163Sshin		trace(1, "\tno matching ifa found for %s/%d on %s\n",
205755163Sshin			inet6_n2p(addr), prefix, ifcp->ifc_name);
205855163Sshin		return -1;
205955163Sshin	}
2060243232Shrs	if (ifac->ifac_ifc != ifcp) {
206155163Sshin		trace(1, "\taddress table corrupt: back pointer does not match "
206255163Sshin			"(%s != %s)\n",
2063243232Shrs			ifcp->ifc_name, ifac->ifac_ifc->ifc_name);
206455163Sshin		return -1;
206555163Sshin	}
2066243232Shrs	TAILQ_REMOVE(&ifcp->ifc_ifac_head, ifac, ifac_next);
206755163Sshin	t_lifetime = time(NULL) - RIP_LIFETIME;
206855163Sshin	/* age route for interface address */
206955163Sshin	memset(&ni6, 0, sizeof(ni6));
2070243232Shrs	ni6.rip6_dest = ifac->ifac_addr;
2071243232Shrs	ni6.rip6_plen = ifac->ifac_plen;
207255163Sshin	applyplen(&ni6.rip6_dest, ni6.rip6_plen);
207355163Sshin	trace(1, "\tfind interface route %s/%d on %d\n",
207455163Sshin		inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen, ifcp->ifc_index);
2075243232Shrs	if ((rrt = rtsearch(&ni6)) != NULL) {
207655163Sshin		struct in6_addr none;
207755163Sshin		memset(&none, 0, sizeof(none));
207878064Sume		if (rrt->rrt_index == ifcp->ifc_index &&
207978064Sume		    (IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &none) ||
208078064Sume		     IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw))) {
208155163Sshin			trace(1, "\troute found, age it\n");
208255163Sshin			if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
208355163Sshin				rrt->rrt_t = t_lifetime;
208455163Sshin				rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
208555163Sshin			}
208655163Sshin			updated++;
208755163Sshin		} else {
208855163Sshin			trace(1, "\tnon-interface route found: %s/%d on %d\n",
208955163Sshin				inet6_n2p(&rrt->rrt_info.rip6_dest),
209055163Sshin				rrt->rrt_info.rip6_plen,
209155163Sshin				rrt->rrt_index);
209255163Sshin		}
209355163Sshin	} else
209455163Sshin		trace(1, "\tno interface route found\n");
209555163Sshin	/* age route for p2p destination */
209655163Sshin	if (ifcp->ifc_flags & IFF_POINTOPOINT) {
209755163Sshin		memset(&ni6, 0, sizeof(ni6));
2098243232Shrs		ni6.rip6_dest = ifac->ifac_raddr;
209955163Sshin		ni6.rip6_plen = 128;
210055163Sshin		applyplen(&ni6.rip6_dest, ni6.rip6_plen);	/*to be sure*/
210155163Sshin		trace(1, "\tfind p2p route %s/%d on %d\n",
210255163Sshin			inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen,
210355163Sshin			ifcp->ifc_index);
2104243232Shrs		if ((rrt = rtsearch(&ni6)) != NULL) {
210578064Sume			if (rrt->rrt_index == ifcp->ifc_index &&
2106243232Shrs			    IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw,
2107243232Shrs			    &ifac->ifac_addr)) {
210855163Sshin				trace(1, "\troute found, age it\n");
210955163Sshin				if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
211055163Sshin					rrt->rrt_t = t_lifetime;
211155163Sshin					rrt->rrt_info.rip6_metric =
211278064Sume					    HOPCNT_INFINITY6;
211355163Sshin					updated++;
211455163Sshin				}
211555163Sshin			} else {
211655163Sshin				trace(1, "\tnon-p2p route found: %s/%d on %d\n",
211755163Sshin					inet6_n2p(&rrt->rrt_info.rip6_dest),
211855163Sshin					rrt->rrt_info.rip6_plen,
211955163Sshin					rrt->rrt_index);
212055163Sshin			}
212155163Sshin		} else
212255163Sshin			trace(1, "\tno p2p route found\n");
212355163Sshin	}
2124243232Shrs	free(ifac);
2125243232Shrs
2126243232Shrs	return ((updated) ? 0 : -1);
212755163Sshin}
212855163Sshin
212955163Sshin/*
213055163Sshin * Get each interface address and put those interface routes to the route
213155163Sshin * list.
213255163Sshin */
213378064Sumeint
2134243232Shrsifrt(struct ifc *ifcp, int again)
213555163Sshin{
2136243232Shrs	struct ifac *ifac;
2137243232Shrs	struct riprt *rrt = NULL, *search_rrt, *loop_rrt;
213862607Sitojun	struct netinfo6 *np;
213978064Sume	time_t t_lifetime;
214078064Sume	int need_trigger = 0;
214155163Sshin
2142122677Sume#if 0
214355163Sshin	if (ifcp->ifc_flags & IFF_LOOPBACK)
214478064Sume		return 0;			/* ignore loopback */
2145122677Sume#endif
2146122677Sume
214762607Sitojun	if (ifcp->ifc_flags & IFF_POINTOPOINT) {
214862607Sitojun		ifrt_p2p(ifcp, again);
214978064Sume		return 0;
215062607Sitojun	}
215162607Sitojun
2152243232Shrs	TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2153243232Shrs		if (IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
215462607Sitojun#if 0
215562607Sitojun			trace(1, "route: %s on %s: "
215662607Sitojun			    "skip linklocal interface address\n",
2157243232Shrs			    inet6_n2p(&ifac->ifac_addr), ifcp->ifc_name);
215862607Sitojun#endif
215962607Sitojun			continue;
216062607Sitojun		}
2161243232Shrs		if (IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_addr)) {
216262607Sitojun#if 0
216362607Sitojun			trace(1, "route: %s: skip unspec interface address\n",
216462607Sitojun			    ifcp->ifc_name);
216562607Sitojun#endif
216662607Sitojun			continue;
216762607Sitojun		}
2168243232Shrs		if (IN6_IS_ADDR_LOOPBACK(&ifac->ifac_addr)) {
2169122677Sume#if 0
2170122677Sume			trace(1, "route: %s: skip loopback address\n",
2171122677Sume			    ifcp->ifc_name);
2172122677Sume#endif
2173122677Sume			continue;
2174122677Sume		}
217578064Sume		if (ifcp->ifc_flags & IFF_UP) {
217678064Sume			if ((rrt = MALLOC(struct riprt)) == NULL)
217778064Sume				fatal("malloc: struct riprt");
217878064Sume			memset(rrt, 0, sizeof(*rrt));
217978064Sume			rrt->rrt_same = NULL;
218078064Sume			rrt->rrt_index = ifcp->ifc_index;
218178064Sume			rrt->rrt_t = 0;	/* don't age */
2182243232Shrs			rrt->rrt_info.rip6_dest = ifac->ifac_addr;
218378064Sume			rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
218478064Sume			rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2185243232Shrs			rrt->rrt_info.rip6_plen = ifac->ifac_plen;
2186186119Sqingli			rrt->rrt_flags = RTF_HOST;
218778064Sume			rrt->rrt_rflags |= RRTF_CHANGED;
2188243232Shrs			applyplen(&rrt->rrt_info.rip6_dest, ifac->ifac_plen);
218978064Sume			memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2190243232Shrs			rrt->rrt_gw = ifac->ifac_addr;
219178064Sume			np = &rrt->rrt_info;
2192243232Shrs			search_rrt = rtsearch(np);
219378064Sume			if (search_rrt != NULL) {
2194119042Sume				if (search_rrt->rrt_info.rip6_metric <=
219578064Sume				    rrt->rrt_info.rip6_metric) {
219678064Sume					/* Already have better route */
219778064Sume					if (!again) {
219878064Sume						trace(1, "route: %s/%d: "
219978064Sume						    "already registered (%s)\n",
220078064Sume						    inet6_n2p(&np->rip6_dest), np->rip6_plen,
220178064Sume						    ifcp->ifc_name);
220278064Sume					}
2203119042Sume					goto next;
220478064Sume				}
2205119042Sume
2206338279Smarkj				TAILQ_REMOVE(&riprt_head, search_rrt, rrt_next);
2207338279Smarkj				delroute(&search_rrt->rrt_info,
2208338279Smarkj				    &search_rrt->rrt_gw);
2209338279Smarkj				free(search_rrt);
221078064Sume			}
221155163Sshin			/* Attach the route to the list */
221262607Sitojun			trace(1, "route: %s/%d: register route (%s)\n",
221362607Sitojun			    inet6_n2p(&np->rip6_dest), np->rip6_plen,
221462607Sitojun			    ifcp->ifc_name);
2215243232Shrs			TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
221678064Sume			addroute(rrt, &rrt->rrt_gw, ifcp);
2217119042Sume			rrt = NULL;
221878064Sume			sendrequest(ifcp);
221978064Sume			ripsend(ifcp, &ifcp->ifc_ripsin, 0);
222078064Sume			need_trigger = 1;
222155163Sshin		} else {
2222243232Shrs			TAILQ_FOREACH(loop_rrt, &riprt_head, rrt_next) {
222378064Sume				if (loop_rrt->rrt_index == ifcp->ifc_index) {
222478064Sume					t_lifetime = time(NULL) - RIP_LIFETIME;
222578064Sume					if (loop_rrt->rrt_t == 0 || loop_rrt->rrt_t > t_lifetime) {
222678064Sume						loop_rrt->rrt_t = t_lifetime;
222778064Sume						loop_rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
222878064Sume						loop_rrt->rrt_rflags |= RRTF_CHANGED;
222978064Sume						need_trigger = 1;
223078064Sume					}
223178064Sume				}
223255163Sshin			}
223378064Sume                }
2234119042Sume	next:
2235119042Sume		if (rrt)
2236119042Sume			free(rrt);
223762607Sitojun	}
223878064Sume	return need_trigger;
223962607Sitojun}
224055163Sshin
224162607Sitojun/*
224262607Sitojun * there are couple of p2p interface routing models.  "behavior" lets
224362607Sitojun * you pick one.  it looks that gated behavior fits best with BSDs,
2244119035Sume * since BSD kernels do not look at prefix length on p2p interfaces.
224562607Sitojun */
224662607Sitojunvoid
2247243232Shrsifrt_p2p(struct ifc *ifcp, int again)
224862607Sitojun{
2249243232Shrs	struct ifac *ifac;
2250243232Shrs	struct riprt *rrt, *orrt;
225162607Sitojun	struct netinfo6 *np;
225262607Sitojun	struct in6_addr addr, dest;
225362607Sitojun	int advert, ignore, i;
225462607Sitojun#define P2PADVERT_NETWORK	1
225562607Sitojun#define P2PADVERT_ADDR		2
225662607Sitojun#define P2PADVERT_DEST		4
225762607Sitojun#define P2PADVERT_MAX		4
225862607Sitojun	const enum { CISCO, GATED, ROUTE6D } behavior = GATED;
225978064Sume	const char *category = "";
226062607Sitojun	const char *noadv;
226162607Sitojun
2262243232Shrs	TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2263243232Shrs		addr = ifac->ifac_addr;
2264243232Shrs		dest = ifac->ifac_raddr;
2265243232Shrs		applyplen(&addr, ifac->ifac_plen);
2266243232Shrs		applyplen(&dest, ifac->ifac_plen);
226762607Sitojun		advert = ignore = 0;
226862607Sitojun		switch (behavior) {
226962607Sitojun		case CISCO:
227062607Sitojun			/*
227162607Sitojun			 * honor addr/plen, just like normal shared medium
227262607Sitojun			 * interface.  this may cause trouble if you reuse
227362607Sitojun			 * addr/plen on other interfaces.
227462607Sitojun			 *
227562607Sitojun			 * advertise addr/plen.
227662607Sitojun			 */
227762607Sitojun			advert |= P2PADVERT_NETWORK;
227862607Sitojun			break;
227962607Sitojun		case GATED:
228062607Sitojun			/*
228162607Sitojun			 * prefixlen on p2p interface is meaningless.
228262607Sitojun			 * advertise addr/128 and dest/128.
228362607Sitojun			 *
228462607Sitojun			 * do not install network route to route6d routing
228562607Sitojun			 * table (if we do, it would prevent route installation
228662607Sitojun			 * for other p2p interface that shares addr/plen).
228778064Sume			 *
228878064Sume			 * XXX what should we do if dest is ::?  it will not
228978064Sume			 * get announced anyways (see following filter),
229078064Sume			 * but we need to think.
229162607Sitojun			 */
229262607Sitojun			advert |= P2PADVERT_ADDR;
229362607Sitojun			advert |= P2PADVERT_DEST;
229462607Sitojun			ignore |= P2PADVERT_NETWORK;
229562607Sitojun			break;
229662607Sitojun		case ROUTE6D:
229762607Sitojun			/*
229878064Sume			 * just for testing.  actually the code is redundant
229978064Sume			 * given the current p2p interface address assignment
230078064Sume			 * rule for kame kernel.
230178064Sume			 *
230278064Sume			 * intent:
230378064Sume			 *	A/n -> announce A/n
230478064Sume			 *	A B/n, A and B share prefix -> A/n (= B/n)
230578064Sume			 *	A B/n, do not share prefix -> A/128 and B/128
230678064Sume			 * actually, A/64 and A B/128 are the only cases
230778064Sume			 * permitted by the kernel:
230878064Sume			 *	A/64 -> A/64
230978064Sume			 *	A B/128 -> A/128 and B/128
231062607Sitojun			 */
2311243232Shrs			if (!IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_raddr)) {
231278064Sume				if (IN6_ARE_ADDR_EQUAL(&addr, &dest))
231378064Sume					advert |= P2PADVERT_NETWORK;
231478064Sume				else {
231578064Sume					advert |= P2PADVERT_ADDR;
231678064Sume					advert |= P2PADVERT_DEST;
231778064Sume					ignore |= P2PADVERT_NETWORK;
231878064Sume				}
231978064Sume			} else
232062607Sitojun				advert |= P2PADVERT_NETWORK;
232162607Sitojun			break;
232262607Sitojun		}
232362607Sitojun
232462607Sitojun		for (i = 1; i <= P2PADVERT_MAX; i *= 2) {
232562607Sitojun			if ((ignore & i) != 0)
232662607Sitojun				continue;
232778064Sume			if ((rrt = MALLOC(struct riprt)) == NULL) {
232855163Sshin				fatal("malloc: struct riprt");
232978064Sume				/*NOTREACHED*/
233078064Sume			}
233162607Sitojun			memset(rrt, 0, sizeof(*rrt));
233255163Sshin			rrt->rrt_same = NULL;
233355163Sshin			rrt->rrt_index = ifcp->ifc_index;
233462607Sitojun			rrt->rrt_t = 0;	/* don't age */
233562607Sitojun			switch (i) {
233662607Sitojun			case P2PADVERT_NETWORK:
2337243232Shrs				rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2338243232Shrs				rrt->rrt_info.rip6_plen = ifac->ifac_plen;
233962607Sitojun				applyplen(&rrt->rrt_info.rip6_dest,
2340243232Shrs				    ifac->ifac_plen);
234162607Sitojun				category = "network";
234262607Sitojun				break;
234362607Sitojun			case P2PADVERT_ADDR:
2344243232Shrs				rrt->rrt_info.rip6_dest = ifac->ifac_addr;
234562607Sitojun				rrt->rrt_info.rip6_plen = 128;
234678064Sume				rrt->rrt_gw = in6addr_loopback;
234762607Sitojun				category = "addr";
234862607Sitojun				break;
234962607Sitojun			case P2PADVERT_DEST:
2350243232Shrs				rrt->rrt_info.rip6_dest = ifac->ifac_raddr;
235162607Sitojun				rrt->rrt_info.rip6_plen = 128;
2352243232Shrs				rrt->rrt_gw = ifac->ifac_addr;
235362607Sitojun				category = "dest";
235462607Sitojun				break;
235562607Sitojun			}
235662607Sitojun			if (IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_info.rip6_dest) ||
235762607Sitojun			    IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_info.rip6_dest)) {
235862607Sitojun#if 0
235962607Sitojun				trace(1, "route: %s: skip unspec/linklocal "
236062607Sitojun				    "(%s on %s)\n", category, ifcp->ifc_name);
236162607Sitojun#endif
236262607Sitojun				free(rrt);
236362607Sitojun				continue;
236462607Sitojun			}
236562607Sitojun			if ((advert & i) == 0) {
236662607Sitojun				rrt->rrt_rflags |= RRTF_NOADVERTISE;
236762607Sitojun				noadv = ", NO-ADV";
236862607Sitojun			} else
236962607Sitojun				noadv = "";
237055163Sshin			rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
237162607Sitojun			rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
237255163Sshin			np = &rrt->rrt_info;
2373243232Shrs			orrt = rtsearch(np);
237478064Sume			if (!orrt) {
237555163Sshin				/* Attach the route to the list */
237662607Sitojun				trace(1, "route: %s/%d: register route "
237762607Sitojun				    "(%s on %s%s)\n",
237862607Sitojun				    inet6_n2p(&np->rip6_dest), np->rip6_plen,
237962607Sitojun				    category, ifcp->ifc_name, noadv);
2380243232Shrs				TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
238178064Sume			} else if (rrt->rrt_index != orrt->rrt_index ||
238278064Sume			    rrt->rrt_info.rip6_metric != orrt->rrt_info.rip6_metric) {
2383243232Shrs				/* replace route */
2384243232Shrs				TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2385243232Shrs				TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
238678064Sume				free(orrt);
238778064Sume
238878064Sume				trace(1, "route: %s/%d: update (%s on %s%s)\n",
238978064Sume				    inet6_n2p(&np->rip6_dest), np->rip6_plen,
239078064Sume				    category, ifcp->ifc_name, noadv);
239155163Sshin			} else {
239255163Sshin				/* Already found */
239355163Sshin				if (!again) {
239462607Sitojun					trace(1, "route: %s/%d: "
239562607Sitojun					    "already registered (%s on %s%s)\n",
239662607Sitojun					    inet6_n2p(&np->rip6_dest),
239762607Sitojun					    np->rip6_plen, category,
239862607Sitojun					    ifcp->ifc_name, noadv);
239955163Sshin				}
240055163Sshin				free(rrt);
240155163Sshin			}
240255163Sshin		}
240355163Sshin	}
240462607Sitojun#undef P2PADVERT_NETWORK
240562607Sitojun#undef P2PADVERT_ADDR
240662607Sitojun#undef P2PADVERT_DEST
240762607Sitojun#undef P2PADVERT_MAX
240855163Sshin}
240955163Sshin
241055163Sshinint
2411243232Shrsgetifmtu(int ifindex)
241255163Sshin{
241355163Sshin	int	mib[6];
241455163Sshin	char	*buf;
241555163Sshin	size_t	msize;
241655163Sshin	struct	if_msghdr *ifm;
241755163Sshin	int	mtu;
241855163Sshin
241955163Sshin	mib[0] = CTL_NET;
242055163Sshin	mib[1] = PF_ROUTE;
242155163Sshin	mib[2] = 0;
242255163Sshin	mib[3] = AF_INET6;
242355163Sshin	mib[4] = NET_RT_IFLIST;
242455163Sshin	mib[5] = ifindex;
2425312022Sngie	if (sysctl(mib, nitems(mib), NULL, &msize, NULL, 0) < 0) {
242655163Sshin		fatal("sysctl estimate NET_RT_IFLIST");
242778064Sume		/*NOTREACHED*/
242878064Sume	}
242978064Sume	if ((buf = malloc(msize)) == NULL) {
243055163Sshin		fatal("malloc");
243178064Sume		/*NOTREACHED*/
243278064Sume	}
2433312022Sngie	if (sysctl(mib, nitems(mib), buf, &msize, NULL, 0) < 0) {
243455163Sshin		fatal("sysctl NET_RT_IFLIST");
243578064Sume		/*NOTREACHED*/
243678064Sume	}
243755163Sshin	ifm = (struct if_msghdr *)buf;
243855163Sshin	mtu = ifm->ifm_data.ifi_mtu;
243978064Sume	if (ifindex != ifm->ifm_index) {
244055163Sshin		fatal("ifindex does not match with ifm_index");
244178064Sume		/*NOTREACHED*/
244278064Sume	}
244355163Sshin	free(buf);
244455163Sshin	return mtu;
244555163Sshin}
244655163Sshin
244755163Sshinconst char *
2448243232Shrsrttypes(struct rt_msghdr *rtm)
244955163Sshin{
245062607Sitojun#define	RTTYPE(s, f) \
245162607Sitojundo { \
245262607Sitojun	if (rtm->rtm_type == (f)) \
245362607Sitojun		return (s); \
245462607Sitojun} while (0)
245555163Sshin	RTTYPE("ADD", RTM_ADD);
245655163Sshin	RTTYPE("DELETE", RTM_DELETE);
245755163Sshin	RTTYPE("CHANGE", RTM_CHANGE);
245855163Sshin	RTTYPE("GET", RTM_GET);
245955163Sshin	RTTYPE("LOSING", RTM_LOSING);
246055163Sshin	RTTYPE("REDIRECT", RTM_REDIRECT);
246155163Sshin	RTTYPE("MISS", RTM_MISS);
246255163Sshin	RTTYPE("LOCK", RTM_LOCK);
246355163Sshin	RTTYPE("NEWADDR", RTM_NEWADDR);
246455163Sshin	RTTYPE("DELADDR", RTM_DELADDR);
246555163Sshin	RTTYPE("IFINFO", RTM_IFINFO);
246678064Sume#ifdef RTM_OIFINFO
246778064Sume	RTTYPE("OIFINFO", RTM_OIFINFO);
246878064Sume#endif
246978064Sume#ifdef RTM_IFANNOUNCE
247078064Sume	RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE);
247178064Sume#endif
247278064Sume#ifdef RTM_NEWMADDR
247378064Sume	RTTYPE("NEWMADDR", RTM_NEWMADDR);
247478064Sume#endif
247578064Sume#ifdef RTM_DELMADDR
247678064Sume	RTTYPE("DELMADDR", RTM_DELMADDR);
247778064Sume#endif
247855163Sshin#undef RTTYPE
247955163Sshin	return NULL;
248055163Sshin}
248155163Sshin
248255163Sshinconst char *
2483243232Shrsrtflags(struct rt_msghdr *rtm)
248455163Sshin{
248555163Sshin	static char buf[BUFSIZ];
248655163Sshin
248778064Sume	/*
248878064Sume	 * letter conflict should be okay.  painful when *BSD diverges...
248978064Sume	 */
249078064Sume	strlcpy(buf, "", sizeof(buf));
249162607Sitojun#define	RTFLAG(s, f) \
249262607Sitojundo { \
249362607Sitojun	if (rtm->rtm_flags & (f)) \
249478064Sume		strlcat(buf, (s), sizeof(buf)); \
249562607Sitojun} while (0)
249655163Sshin	RTFLAG("U", RTF_UP);
249755163Sshin	RTFLAG("G", RTF_GATEWAY);
249855163Sshin	RTFLAG("H", RTF_HOST);
249955163Sshin	RTFLAG("R", RTF_REJECT);
250055163Sshin	RTFLAG("D", RTF_DYNAMIC);
250155163Sshin	RTFLAG("M", RTF_MODIFIED);
250255163Sshin	RTFLAG("d", RTF_DONE);
250355163Sshin#ifdef	RTF_MASK
250455163Sshin	RTFLAG("m", RTF_MASK);
250555163Sshin#endif
250678064Sume#ifdef RTF_CLONED
250778064Sume	RTFLAG("c", RTF_CLONED);
250878064Sume#endif
250955163Sshin	RTFLAG("X", RTF_XRESOLVE);
2510186119Sqingli#ifdef RTF_LLINFO
251155163Sshin	RTFLAG("L", RTF_LLINFO);
2512186119Sqingli#endif
251355163Sshin	RTFLAG("S", RTF_STATIC);
251455163Sshin	RTFLAG("B", RTF_BLACKHOLE);
251578064Sume#ifdef RTF_PROTO3
251678064Sume	RTFLAG("3", RTF_PROTO3);
251778064Sume#endif
251855163Sshin	RTFLAG("2", RTF_PROTO2);
251955163Sshin	RTFLAG("1", RTF_PROTO1);
252078064Sume#ifdef RTF_BROADCAST
252178064Sume	RTFLAG("b", RTF_BROADCAST);
252278064Sume#endif
252378064Sume#ifdef RTF_DEFAULT
252478064Sume	RTFLAG("d", RTF_DEFAULT);
252578064Sume#endif
252678064Sume#ifdef RTF_ISAROUTER
252778064Sume	RTFLAG("r", RTF_ISAROUTER);
252878064Sume#endif
252978064Sume#ifdef RTF_TUNNEL
253078064Sume	RTFLAG("T", RTF_TUNNEL);
253178064Sume#endif
253278064Sume#ifdef RTF_AUTH
253378064Sume	RTFLAG("A", RTF_AUTH);
253478064Sume#endif
253578064Sume#ifdef RTF_CRYPT
253678064Sume	RTFLAG("E", RTF_CRYPT);
253778064Sume#endif
253855163Sshin#undef RTFLAG
253955163Sshin	return buf;
254055163Sshin}
254155163Sshin
254255163Sshinconst char *
2543243232Shrsifflags(int flags)
254455163Sshin{
254555163Sshin	static char buf[BUFSIZ];
254655163Sshin
254778064Sume	strlcpy(buf, "", sizeof(buf));
254862607Sitojun#define	IFFLAG(s, f) \
254962607Sitojundo { \
2550119040Sume	if (flags & (f)) { \
255162607Sitojun		if (buf[0]) \
255278064Sume			strlcat(buf, ",", sizeof(buf)); \
2553119040Sume		strlcat(buf, (s), sizeof(buf)); \
255462607Sitojun	} \
255562607Sitojun} while (0)
255655163Sshin	IFFLAG("UP", IFF_UP);
255755163Sshin	IFFLAG("BROADCAST", IFF_BROADCAST);
255855163Sshin	IFFLAG("DEBUG", IFF_DEBUG);
255955163Sshin	IFFLAG("LOOPBACK", IFF_LOOPBACK);
256055163Sshin	IFFLAG("POINTOPOINT", IFF_POINTOPOINT);
256155163Sshin#ifdef IFF_NOTRAILERS
256255163Sshin	IFFLAG("NOTRAILERS", IFF_NOTRAILERS);
256355163Sshin#endif
256455163Sshin	IFFLAG("RUNNING", IFF_RUNNING);
256555163Sshin	IFFLAG("NOARP", IFF_NOARP);
256655163Sshin	IFFLAG("PROMISC", IFF_PROMISC);
256755163Sshin	IFFLAG("ALLMULTI", IFF_ALLMULTI);
256855163Sshin	IFFLAG("OACTIVE", IFF_OACTIVE);
256955163Sshin	IFFLAG("SIMPLEX", IFF_SIMPLEX);
257055163Sshin	IFFLAG("LINK0", IFF_LINK0);
257155163Sshin	IFFLAG("LINK1", IFF_LINK1);
257255163Sshin	IFFLAG("LINK2", IFF_LINK2);
257355163Sshin	IFFLAG("MULTICAST", IFF_MULTICAST);
257455163Sshin#undef IFFLAG
257555163Sshin	return buf;
257655163Sshin}
257755163Sshin
257855163Sshinvoid
2579243232Shrskrtread(int again)
258055163Sshin{
258155163Sshin	int mib[6];
258255163Sshin	size_t msize;
258355163Sshin	char *buf, *p, *lim;
258455163Sshin	struct rt_msghdr *rtm;
258555163Sshin	int retry;
258655163Sshin	const char *errmsg;
258755163Sshin
258855163Sshin	retry = 0;
258955163Sshin	buf = NULL;
259055163Sshin	mib[0] = CTL_NET;
259155163Sshin	mib[1] = PF_ROUTE;
259255163Sshin	mib[2] = 0;
259355163Sshin	mib[3] = AF_INET6;	/* Address family */
259455163Sshin	mib[4] = NET_RT_DUMP;	/* Dump the kernel routing table */
259555163Sshin	mib[5] = 0;		/* No flags */
259655163Sshin	do {
2597243232Shrs		if (retry)
2598243232Shrs			sleep(1);
259955163Sshin		retry++;
260055163Sshin		errmsg = NULL;
2601299491Scem		if (buf) {
260255163Sshin			free(buf);
2603299491Scem			buf = NULL;
2604299491Scem		}
2605312022Sngie		if (sysctl(mib, nitems(mib), NULL, &msize, NULL, 0) < 0) {
260655163Sshin			errmsg = "sysctl estimate";
260755163Sshin			continue;
260855163Sshin		}
260955163Sshin		if ((buf = malloc(msize)) == NULL) {
261055163Sshin			errmsg = "malloc";
261155163Sshin			continue;
261255163Sshin		}
2613312022Sngie		if (sysctl(mib, nitems(mib), buf, &msize, NULL, 0) < 0) {
261455163Sshin			errmsg = "sysctl NET_RT_DUMP";
261555163Sshin			continue;
261655163Sshin		}
2617243233Shrs	} while (retry < RT_DUMP_MAXRETRY && errmsg != NULL);
261878064Sume	if (errmsg) {
261969279Sume		fatal("%s (with %d retries, msize=%lu)", errmsg, retry,
262069279Sume		    (u_long)msize);
262178064Sume		/*NOTREACHED*/
262278064Sume	} else if (1 < retry)
262355163Sshin		syslog(LOG_INFO, "NET_RT_DUMP %d retires", retry);
262455163Sshin
262555163Sshin	lim = buf + msize;
262655163Sshin	for (p = buf; p < lim; p += rtm->rtm_msglen) {
262755163Sshin		rtm = (struct rt_msghdr *)p;
262855163Sshin		rt_entry(rtm, again);
262955163Sshin	}
263055163Sshin	free(buf);
263155163Sshin}
263255163Sshin
263355163Sshinvoid
2634243232Shrsrt_entry(struct rt_msghdr *rtm, int again)
263555163Sshin{
263655163Sshin	struct	sockaddr_in6 *sin6_dst, *sin6_gw, *sin6_mask;
263755163Sshin	struct	sockaddr_in6 *sin6_genmask, *sin6_ifp;
263855163Sshin	char	*rtmp, *ifname = NULL;
263978064Sume	struct	riprt *rrt, *orrt;
264055163Sshin	struct	netinfo6 *np;
2641243232Shrs	int ifindex;
264255163Sshin
264355163Sshin	sin6_dst = sin6_gw = sin6_mask = sin6_genmask = sin6_ifp = 0;
264455163Sshin	if ((rtm->rtm_flags & RTF_UP) == 0 || rtm->rtm_flags &
2645186119Sqingli		(RTF_XRESOLVE|RTF_BLACKHOLE)) {
264655163Sshin		return;		/* not interested in the link route */
264762607Sitojun	}
264869279Sume	/* do not look at cloned routes */
264969279Sume#ifdef RTF_WASCLONED
265069279Sume	if (rtm->rtm_flags & RTF_WASCLONED)
265169279Sume		return;
265269279Sume#endif
265369279Sume#ifdef RTF_CLONED
265469279Sume	if (rtm->rtm_flags & RTF_CLONED)
265569279Sume		return;
265669279Sume#endif
2657243233Shrs	/* XXX: Ignore connected routes. */
2658243233Shrs	if (!(rtm->rtm_flags & (RTF_GATEWAY|RTF_HOST|RTF_STATIC)))
2659243233Shrs		return;
266069279Sume	/*
266169279Sume	 * do not look at dynamic routes.
266269279Sume	 * netbsd/openbsd cloned routes have UGHD.
266369279Sume	 */
266469279Sume	if (rtm->rtm_flags & RTF_DYNAMIC)
266569279Sume		return;
266655163Sshin	rtmp = (char *)(rtm + 1);
266755163Sshin	/* Destination */
266855163Sshin	if ((rtm->rtm_addrs & RTA_DST) == 0)
266955163Sshin		return;		/* ignore routes without destination address */
267055163Sshin	sin6_dst = (struct sockaddr_in6 *)rtmp;
267164631Sitojun	rtmp += ROUNDUP(sin6_dst->sin6_len);
267255163Sshin	if (rtm->rtm_addrs & RTA_GATEWAY) {
267355163Sshin		sin6_gw = (struct sockaddr_in6 *)rtmp;
267455163Sshin		rtmp += ROUNDUP(sin6_gw->sin6_len);
267555163Sshin	}
267655163Sshin	if (rtm->rtm_addrs & RTA_NETMASK) {
267755163Sshin		sin6_mask = (struct sockaddr_in6 *)rtmp;
267855163Sshin		rtmp += ROUNDUP(sin6_mask->sin6_len);
267955163Sshin	}
268055163Sshin	if (rtm->rtm_addrs & RTA_GENMASK) {
268155163Sshin		sin6_genmask = (struct sockaddr_in6 *)rtmp;
268255163Sshin		rtmp += ROUNDUP(sin6_genmask->sin6_len);
268355163Sshin	}
268455163Sshin	if (rtm->rtm_addrs & RTA_IFP) {
268555163Sshin		sin6_ifp = (struct sockaddr_in6 *)rtmp;
268655163Sshin		rtmp += ROUNDUP(sin6_ifp->sin6_len);
268755163Sshin	}
268855163Sshin
268955163Sshin	/* Destination */
269055163Sshin	if (sin6_dst->sin6_family != AF_INET6)
269155163Sshin		return;
269255163Sshin	if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst->sin6_addr))
269355163Sshin		return;		/* Link-local */
269455163Sshin	if (IN6_ARE_ADDR_EQUAL(&sin6_dst->sin6_addr, &in6addr_loopback))
269555163Sshin		return;		/* Loopback */
269655163Sshin	if (IN6_IS_ADDR_MULTICAST(&sin6_dst->sin6_addr))
269755163Sshin		return;
269855163Sshin
269978064Sume	if ((rrt = MALLOC(struct riprt)) == NULL) {
270055163Sshin		fatal("malloc: struct riprt");
270178064Sume		/*NOTREACHED*/
270278064Sume	}
270362607Sitojun	memset(rrt, 0, sizeof(*rrt));
270455163Sshin	np = &rrt->rrt_info;
270555163Sshin	rrt->rrt_same = NULL;
270655163Sshin	rrt->rrt_t = time(NULL);
270755163Sshin	if (aflag == 0 && (rtm->rtm_flags & RTF_STATIC))
270855163Sshin		rrt->rrt_t = 0;	/* Don't age static routes */
2709243233Shrs	if (rtm->rtm_flags & Pflag)
2710243233Shrs		rrt->rrt_t = 0;	/* Don't age PROTO[123] routes */
2711122677Sume	if ((rtm->rtm_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST)
2712122677Sume		rrt->rrt_t = 0;	/* Don't age non-gateway host routes */
271355163Sshin	np->rip6_tag = 0;
271455163Sshin	np->rip6_metric = rtm->rtm_rmx.rmx_hopcount;
271555163Sshin	if (np->rip6_metric < 1)
271655163Sshin		np->rip6_metric = 1;
271755163Sshin	rrt->rrt_flags = rtm->rtm_flags;
271855163Sshin	np->rip6_dest = sin6_dst->sin6_addr;
271955163Sshin
272055163Sshin	/* Mask or plen */
272155163Sshin	if (rtm->rtm_flags & RTF_HOST)
272255163Sshin		np->rip6_plen = 128;	/* Host route */
272378064Sume	else if (sin6_mask)
272478064Sume		np->rip6_plen = sin6mask2len(sin6_mask);
272578064Sume	else
272655163Sshin		np->rip6_plen = 0;
272755163Sshin
2728243232Shrs	orrt = rtsearch(np);
272978064Sume	if (orrt && orrt->rrt_info.rip6_metric != HOPCNT_INFINITY6) {
273055163Sshin		/* Already found */
273155163Sshin		if (!again) {
273255163Sshin			trace(1, "route: %s/%d flags %s: already registered\n",
273355163Sshin				inet6_n2p(&np->rip6_dest), np->rip6_plen,
273455163Sshin				rtflags(rtm));
273555163Sshin		}
273655163Sshin		free(rrt);
273755163Sshin		return;
273855163Sshin	}
273955163Sshin	/* Gateway */
274055163Sshin	if (!sin6_gw)
274155163Sshin		memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
274255163Sshin	else {
274355163Sshin		if (sin6_gw->sin6_family == AF_INET6)
274455163Sshin			rrt->rrt_gw = sin6_gw->sin6_addr;
274555163Sshin		else if (sin6_gw->sin6_family == AF_LINK) {
274655163Sshin			/* XXX in case ppp link? */
274755163Sshin			rrt->rrt_gw = in6addr_loopback;
274855163Sshin		} else
274955163Sshin			memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
275055163Sshin	}
275155163Sshin	trace(1, "route: %s/%d flags %s",
275255163Sshin		inet6_n2p(&np->rip6_dest), np->rip6_plen, rtflags(rtm));
275355163Sshin	trace(1, " gw %s", inet6_n2p(&rrt->rrt_gw));
275455163Sshin
275555163Sshin	/* Interface */
2756243232Shrs	ifindex = rtm->rtm_index;
2757243232Shrs	if ((unsigned int)ifindex < nindex2ifc && index2ifc[ifindex])
2758243232Shrs		ifname = index2ifc[ifindex]->ifc_name;
275958070Sshin	else {
276058070Sshin		trace(1, " not configured\n");
276162607Sitojun		free(rrt);
276258070Sshin		return;
276358070Sshin	}
2764243232Shrs	trace(1, " if %s sock %d", ifname, ifindex);
2765243232Shrs	rrt->rrt_index = ifindex;
276655163Sshin
276762607Sitojun	trace(1, "\n");
276862607Sitojun
276955163Sshin	/* Check gateway */
277055163Sshin	if (!IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_gw) &&
2771122677Sume	    !IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw) &&
2772122677Sume	    (rrt->rrt_flags & RTF_LOCAL) == 0) {
277355163Sshin		trace(0, "***** Gateway %s is not a link-local address.\n",
277455163Sshin			inet6_n2p(&rrt->rrt_gw));
277555163Sshin		trace(0, "*****     dest(%s) if(%s) -- Not optimized.\n",
277662607Sitojun			inet6_n2p(&rrt->rrt_info.rip6_dest), ifname);
277762607Sitojun		rrt->rrt_rflags |= RRTF_NH_NOT_LLADDR;
277855163Sshin	}
277955163Sshin
278055163Sshin	/* Put it to the route list */
278178064Sume	if (orrt && orrt->rrt_info.rip6_metric == HOPCNT_INFINITY6) {
278278064Sume		/* replace route list */
2783243232Shrs		TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2784243232Shrs		TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
2785243232Shrs
278678064Sume		trace(1, "route: %s/%d flags %s: replace new route\n",
278778064Sume		    inet6_n2p(&np->rip6_dest), np->rip6_plen,
278878064Sume		    rtflags(rtm));
2789243232Shrs		free(orrt);
2790243232Shrs	} else
2791243232Shrs		TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
279255163Sshin}
279355163Sshin
279455163Sshinint
2795243232Shrsaddroute(struct riprt *rrt,
2796243232Shrs	const struct in6_addr *gw,
2797243232Shrs	struct ifc *ifcp)
279855163Sshin{
279955163Sshin	struct	netinfo6 *np;
280055163Sshin	u_char	buf[BUFSIZ], buf1[BUFSIZ], buf2[BUFSIZ];
280155163Sshin	struct	rt_msghdr	*rtm;
2802119031Sume	struct	sockaddr_in6	*sin6;
280355163Sshin	int	len;
280455163Sshin
280555163Sshin	np = &rrt->rrt_info;
280678064Sume	inet_ntop(AF_INET6, (const void *)gw, (char *)buf1, sizeof(buf1));
280755163Sshin	inet_ntop(AF_INET6, (void *)&ifcp->ifc_mylladdr, (char *)buf2, sizeof(buf2));
280855163Sshin	tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n",
280955163Sshin		inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
281055163Sshin		np->rip6_metric - 1, buf2);
281155163Sshin	if (rtlog)
281255163Sshin		fprintf(rtlog, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(),
281355163Sshin			inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
281455163Sshin			np->rip6_metric - 1, buf2);
281555163Sshin	if (nflag)
281655163Sshin		return 0;
281755163Sshin
281855163Sshin	memset(buf, 0, sizeof(buf));
281955163Sshin	rtm = (struct rt_msghdr *)buf;
282055163Sshin	rtm->rtm_type = RTM_ADD;
282155163Sshin	rtm->rtm_version = RTM_VERSION;
282255163Sshin	rtm->rtm_seq = ++seq;
282355163Sshin	rtm->rtm_pid = pid;
282462607Sitojun	rtm->rtm_flags = rrt->rrt_flags;
2825243233Shrs	rtm->rtm_flags |= Qflag;
282655163Sshin	rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
282755163Sshin	rtm->rtm_rmx.rmx_hopcount = np->rip6_metric - 1;
282855163Sshin	rtm->rtm_inits = RTV_HOPCOUNT;
2829119031Sume	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
283055163Sshin	/* Destination */
2831119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2832119031Sume	sin6->sin6_family = AF_INET6;
2833119031Sume	sin6->sin6_addr = np->rip6_dest;
2834119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
283555163Sshin	/* Gateway */
2836119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2837119031Sume	sin6->sin6_family = AF_INET6;
2838119031Sume	sin6->sin6_addr = *gw;
2839270234Shrs	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
2840270234Shrs		sin6->sin6_scope_id = ifcp->ifc_index;
2841119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
284255163Sshin	/* Netmask */
2843119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2844119031Sume	sin6->sin6_family = AF_INET6;
2845119031Sume	sin6->sin6_addr = *(plen2mask(np->rip6_plen));
2846119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
284755163Sshin
2848119031Sume	len = (char *)sin6 - (char *)buf;
284955163Sshin	rtm->rtm_msglen = len;
285055163Sshin	if (write(rtsock, buf, len) > 0)
285155163Sshin		return 0;
285255163Sshin
285355163Sshin	if (errno == EEXIST) {
285455163Sshin		trace(0, "ADD: Route already exists %s/%d gw %s\n",
2855119035Sume		    inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
285655163Sshin		if (rtlog)
285755163Sshin			fprintf(rtlog, "ADD: Route already exists %s/%d gw %s\n",
2858119035Sume			    inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
285955163Sshin	} else {
286055163Sshin		trace(0, "Can not write to rtsock (addroute): %s\n",
2861119035Sume		    strerror(errno));
286255163Sshin		if (rtlog)
286355163Sshin			fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2864119035Sume			    strerror(errno));
286555163Sshin	}
286655163Sshin	return -1;
286755163Sshin}
286855163Sshin
286955163Sshinint
2870243232Shrsdelroute(struct netinfo6 *np, struct in6_addr *gw)
287155163Sshin{
287255163Sshin	u_char	buf[BUFSIZ], buf2[BUFSIZ];
287355163Sshin	struct	rt_msghdr	*rtm;
2874119031Sume	struct	sockaddr_in6	*sin6;
287555163Sshin	int	len;
287655163Sshin
287755163Sshin	inet_ntop(AF_INET6, (void *)gw, (char *)buf2, sizeof(buf2));
287855163Sshin	tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np->rip6_dest),
287955163Sshin		np->rip6_plen, buf2);
288055163Sshin	if (rtlog)
288155163Sshin		fprintf(rtlog, "%s: DEL: %s/%d gw %s\n",
288255163Sshin			hms(), inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
288355163Sshin	if (nflag)
288455163Sshin		return 0;
288555163Sshin
288655163Sshin	memset(buf, 0, sizeof(buf));
288755163Sshin	rtm = (struct rt_msghdr *)buf;
288855163Sshin	rtm->rtm_type = RTM_DELETE;
288955163Sshin	rtm->rtm_version = RTM_VERSION;
289055163Sshin	rtm->rtm_seq = ++seq;
289155163Sshin	rtm->rtm_pid = pid;
289255163Sshin	rtm->rtm_flags = RTF_UP | RTF_GATEWAY;
2893243233Shrs	rtm->rtm_flags |= Qflag;
289478064Sume	if (np->rip6_plen == sizeof(struct in6_addr) * 8)
289578064Sume		rtm->rtm_flags |= RTF_HOST;
289655163Sshin	rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2897119031Sume	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
289855163Sshin	/* Destination */
2899119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2900119031Sume	sin6->sin6_family = AF_INET6;
2901119031Sume	sin6->sin6_addr = np->rip6_dest;
2902119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
290355163Sshin	/* Gateway */
2904119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2905119031Sume	sin6->sin6_family = AF_INET6;
2906119031Sume	sin6->sin6_addr = *gw;
2907119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
290855163Sshin	/* Netmask */
2909119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2910119031Sume	sin6->sin6_family = AF_INET6;
2911119031Sume	sin6->sin6_addr = *(plen2mask(np->rip6_plen));
2912119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
291355163Sshin
2914119031Sume	len = (char *)sin6 - (char *)buf;
291555163Sshin	rtm->rtm_msglen = len;
291655163Sshin	if (write(rtsock, buf, len) >= 0)
291755163Sshin		return 0;
291855163Sshin
291955163Sshin	if (errno == ESRCH) {
292055163Sshin		trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n",
2921119035Sume		    inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
292255163Sshin		if (rtlog)
292355163Sshin			fprintf(rtlog, "RTDEL: Route does not exist: %s/%d gw %s\n",
2924119035Sume			    inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
292555163Sshin	} else {
292655163Sshin		trace(0, "Can not write to rtsock (delroute): %s\n",
2927119035Sume		    strerror(errno));
292855163Sshin		if (rtlog)
292955163Sshin			fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2930119035Sume			    strerror(errno));
293155163Sshin	}
293255163Sshin	return -1;
293355163Sshin}
293455163Sshin
293555163Sshinstruct in6_addr *
2936243232Shrsgetroute(struct netinfo6 *np, struct in6_addr *gw)
293755163Sshin{
293855163Sshin	u_char buf[BUFSIZ];
2939119085Sume	int myseq;
294055163Sshin	int len;
294155163Sshin	struct rt_msghdr *rtm;
2942119031Sume	struct sockaddr_in6 *sin6;
294355163Sshin
294455163Sshin	rtm = (struct rt_msghdr *)buf;
294555163Sshin	len = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in6);
294655163Sshin	memset(rtm, 0, len);
294755163Sshin	rtm->rtm_type = RTM_GET;
294855163Sshin	rtm->rtm_version = RTM_VERSION;
294955163Sshin	myseq = ++seq;
295055163Sshin	rtm->rtm_seq = myseq;
295155163Sshin	rtm->rtm_addrs = RTA_DST;
295255163Sshin	rtm->rtm_msglen = len;
2953119031Sume	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2954119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2955119031Sume	sin6->sin6_family = AF_INET6;
2956119031Sume	sin6->sin6_addr = np->rip6_dest;
295755163Sshin	if (write(rtsock, buf, len) < 0) {
295855163Sshin		if (errno == ESRCH)	/* No such route found */
295955163Sshin			return NULL;
296055163Sshin		perror("write to rtsock");
296178064Sume		exit(1);
296255163Sshin	}
296355163Sshin	do {
296455163Sshin		if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
296555163Sshin			perror("read from rtsock");
296678064Sume			exit(1);
296755163Sshin		}
296855163Sshin		rtm = (struct rt_msghdr *)buf;
296955163Sshin	} while (rtm->rtm_seq != myseq || rtm->rtm_pid != pid);
2970119031Sume	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
297155163Sshin	if (rtm->rtm_addrs & RTA_DST) {
2972119031Sume		sin6 = (struct sockaddr_in6 *)
2973119031Sume			((char *)sin6 + ROUNDUP(sin6->sin6_len));
297455163Sshin	}
297555163Sshin	if (rtm->rtm_addrs & RTA_GATEWAY) {
2976119031Sume		*gw = sin6->sin6_addr;
297755163Sshin		return gw;
297855163Sshin	}
297955163Sshin	return NULL;
298055163Sshin}
298155163Sshin
298255163Sshinconst char *
2983243232Shrsinet6_n2p(const struct in6_addr *p)
298455163Sshin{
298555163Sshin	static char buf[BUFSIZ];
298655163Sshin
298778064Sume	return inet_ntop(AF_INET6, (const void *)p, buf, sizeof(buf));
298855163Sshin}
298955163Sshin
299055163Sshinvoid
2991243232Shrsifrtdump(int sig)
299255163Sshin{
299355163Sshin
299455163Sshin	ifdump(sig);
299555163Sshin	rtdump(sig);
299655163Sshin}
299755163Sshin
299855163Sshinvoid
2999243232Shrsifdump(int sig)
300055163Sshin{
300155163Sshin	struct ifc *ifcp;
300255163Sshin	FILE *dump;
3003243232Shrs	int nifc = 0;
300455163Sshin
300555163Sshin	if (sig == 0)
300655163Sshin		dump = stderr;
300755163Sshin	else
300855163Sshin		if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
300955163Sshin			dump = stderr;
301055163Sshin
301155163Sshin	fprintf(dump, "%s: Interface Table Dump\n", hms());
3012243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next)
3013243232Shrs		nifc++;
301455163Sshin	fprintf(dump, "  Number of interfaces: %d\n", nifc);
3015243232Shrs
3016243232Shrs	fprintf(dump, "  advertising interfaces:\n");
3017243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3018243232Shrs		if ((ifcp->ifc_flags & IFF_UP) == 0)
3019243232Shrs			continue;
3020243232Shrs		if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
3021243232Shrs			continue;
3022243232Shrs		ifdump0(dump, ifcp);
302355163Sshin	}
302455163Sshin	fprintf(dump, "\n");
3025243232Shrs	fprintf(dump, "  non-advertising interfaces:\n");
3026243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3027243232Shrs		if ((ifcp->ifc_flags & IFF_UP) &&
3028243232Shrs		    (iff_find(ifcp, IFIL_TYPE_N) == NULL))
3029243232Shrs			continue;
3030243232Shrs		ifdump0(dump, ifcp);
3031243232Shrs	}
3032243232Shrs	fprintf(dump, "\n");
303355163Sshin	if (dump != stderr)
303455163Sshin		fclose(dump);
303555163Sshin}
303655163Sshin
303755163Sshinvoid
3038243232Shrsifdump0(FILE *dump, const struct ifc *ifcp)
303955163Sshin{
3040243232Shrs	struct ifac *ifac;
304155163Sshin	struct iff *iffp;
304255163Sshin	char buf[BUFSIZ];
304355163Sshin	const char *ft;
304455163Sshin	int addr;
304555163Sshin
304655163Sshin	fprintf(dump, "    %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n",
304755163Sshin		ifcp->ifc_name, ifcp->ifc_index, ifflags(ifcp->ifc_flags),
304855163Sshin		inet6_n2p(&ifcp->ifc_mylladdr),
304955163Sshin		ifcp->ifc_mtu, ifcp->ifc_metric);
3050243232Shrs	TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
305155163Sshin		if (ifcp->ifc_flags & IFF_POINTOPOINT) {
3052243232Shrs			inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr,
305355163Sshin				buf, sizeof(buf));
305455163Sshin			fprintf(dump, "\t%s/%d -- %s\n",
3055243232Shrs				inet6_n2p(&ifac->ifac_addr),
3056243232Shrs				ifac->ifac_plen, buf);
305755163Sshin		} else {
305855163Sshin			fprintf(dump, "\t%s/%d\n",
3059243232Shrs				inet6_n2p(&ifac->ifac_addr),
3060243232Shrs				ifac->ifac_plen);
306155163Sshin		}
306255163Sshin	}
3063243232Shrs
3064243232Shrs	fprintf(dump, "\tFilter:\n");
3065243232Shrs	TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
3066243232Shrs		addr = 0;
3067243232Shrs		switch (iffp->iff_type) {
3068243232Shrs		case IFIL_TYPE_A:
3069243232Shrs			ft = "Aggregate"; addr++; break;
3070243232Shrs		case IFIL_TYPE_N:
3071243232Shrs			ft = "No-use"; break;
3072243232Shrs		case IFIL_TYPE_O:
3073243232Shrs			ft = "Advertise-only"; addr++; break;
3074243232Shrs		case IFIL_TYPE_T:
3075243232Shrs			ft = "Default-only"; break;
3076243232Shrs		case IFIL_TYPE_L:
3077243232Shrs			ft = "Listen-only"; addr++; break;
3078243232Shrs		default:
3079243232Shrs			snprintf(buf, sizeof(buf), "Unknown-%c", iffp->iff_type);
3080243232Shrs			ft = buf;
3081243232Shrs			addr++;
3082243232Shrs			break;
308355163Sshin		}
3084243232Shrs		fprintf(dump, "\t\t%s", ft);
3085243232Shrs		if (addr)
3086243232Shrs			fprintf(dump, "(%s/%d)", inet6_n2p(&iffp->iff_addr),
3087243232Shrs				iffp->iff_plen);
308855163Sshin		fprintf(dump, "\n");
308955163Sshin	}
3090243232Shrs	fprintf(dump, "\n");
309155163Sshin}
309255163Sshin
309355163Sshinvoid
3094243232Shrsrtdump(int sig)
309555163Sshin{
309655163Sshin	struct	riprt *rrt;
309755163Sshin	char	buf[BUFSIZ];
309855163Sshin	FILE	*dump;
309955163Sshin	time_t	t, age;
310055163Sshin
310155163Sshin	if (sig == 0)
310255163Sshin		dump = stderr;
310355163Sshin	else
310455163Sshin		if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
310555163Sshin			dump = stderr;
310655163Sshin
310755163Sshin	t = time(NULL);
310855163Sshin	fprintf(dump, "\n%s: Routing Table Dump\n", hms());
3109243232Shrs	TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
311055163Sshin		if (rrt->rrt_t == 0)
311155163Sshin			age = 0;
311255163Sshin		else
311355163Sshin			age = t - rrt->rrt_t;
311455163Sshin		inet_ntop(AF_INET6, (void *)&rrt->rrt_info.rip6_dest,
311555163Sshin			buf, sizeof(buf));
311655163Sshin		fprintf(dump, "    %s/%d if(%d:%s) gw(%s) [%d] age(%ld)",
311755163Sshin			buf, rrt->rrt_info.rip6_plen, rrt->rrt_index,
311855163Sshin			index2ifc[rrt->rrt_index]->ifc_name,
311955163Sshin			inet6_n2p(&rrt->rrt_gw),
312055163Sshin			rrt->rrt_info.rip6_metric, (long)age);
312155163Sshin		if (rrt->rrt_info.rip6_tag) {
312255163Sshin			fprintf(dump, " tag(0x%04x)",
312355163Sshin				ntohs(rrt->rrt_info.rip6_tag) & 0xffff);
312455163Sshin		}
312562607Sitojun		if (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)
312655163Sshin			fprintf(dump, " NOT-LL");
312762607Sitojun		if (rrt->rrt_rflags & RRTF_NOADVERTISE)
312855163Sshin			fprintf(dump, " NO-ADV");
312955163Sshin		fprintf(dump, "\n");
313055163Sshin	}
313155163Sshin	fprintf(dump, "\n");
313255163Sshin	if (dump != stderr)
313355163Sshin		fclose(dump);
313455163Sshin}
313555163Sshin
313655163Sshin/*
313755163Sshin * Parse the -A (and -O) options and put corresponding filter object to the
313878064Sume * specified interface structures.  Each of the -A/O option has the following
313955163Sshin * syntax:	-A 5f09:c400::/32,ef0,ef1  (aggregate)
314055163Sshin * 		-O 5f09:c400::/32,ef0,ef1  (only when match)
314155163Sshin */
314255163Sshinvoid
3143243232Shrsfilterconfig(void)
314455163Sshin{
314555163Sshin	int i;
3146119083Sume	char *p, *ap, *iflp, *ifname, *ep;
3147243232Shrs	struct iff iff, *iffp;
314878064Sume	struct ifc *ifcp;
314978064Sume	struct riprt *rrt;
315064631Sitojun#if 0
315178064Sume	struct in6_addr gw;
315264631Sitojun#endif
3153119083Sume	u_long plen;
315455163Sshin
315555163Sshin	for (i = 0; i < nfilter; i++) {
315655163Sshin		ap = filter[i];
315755163Sshin		iflp = NULL;
3158243232Shrs		iffp = &iff;
3159243232Shrs		memset(iffp, 0, sizeof(*iffp));
316055163Sshin		if (filtertype[i] == 'N' || filtertype[i] == 'T') {
316155163Sshin			iflp = ap;
316255163Sshin			goto ifonly;
316355163Sshin		}
3164119038Sume		if ((p = strchr(ap, ',')) != NULL) {
316555163Sshin			*p++ = '\0';
316655163Sshin			iflp = p;
316755163Sshin		}
3168119038Sume		if ((p = strchr(ap, '/')) == NULL) {
316955163Sshin			fatal("no prefixlen specified for '%s'", ap);
317078064Sume			/*NOTREACHED*/
317178064Sume		}
317255163Sshin		*p++ = '\0';
3173243232Shrs		if (inet_pton(AF_INET6, ap, &iffp->iff_addr) != 1) {
317455163Sshin			fatal("invalid prefix specified for '%s'", ap);
317578064Sume			/*NOTREACHED*/
317678064Sume		}
3177119083Sume		errno = 0;
3178119083Sume		ep = NULL;
3179119083Sume		plen = strtoul(p, &ep, 10);
3180243232Shrs		if (errno || !*p || *ep || plen > sizeof(iffp->iff_addr) * 8) {
3181119083Sume			fatal("invalid prefix length specified for '%s'", ap);
3182119083Sume			/*NOTREACHED*/
3183119083Sume		}
3184243232Shrs		iffp->iff_plen = plen;
3185243232Shrs		applyplen(&iffp->iff_addr, iffp->iff_plen);
318655163Sshinifonly:
3187243232Shrs		iffp->iff_type = filtertype[i];
318878064Sume		if (iflp == NULL || *iflp == '\0') {
318955163Sshin			fatal("no interface specified for '%s'", ap);
319078064Sume			/*NOTREACHED*/
319178064Sume		}
319255163Sshin		/* parse the interface listing portion */
319355163Sshin		while (iflp) {
319455163Sshin			ifname = iflp;
3195119038Sume			if ((iflp = strchr(iflp, ',')) != NULL)
319655163Sshin				*iflp++ = '\0';
3197243232Shrs
3198243232Shrs			TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3199243233Shrs				if (fnmatch(ifname, ifcp->ifc_name, 0) != 0)
3200243232Shrs					continue;
3201243232Shrs
3202243232Shrs				iffp = malloc(sizeof(*iffp));
3203243232Shrs				if (iffp == NULL) {
3204243232Shrs					fatal("malloc of iff");
3205243232Shrs					/*NOTREACHED*/
3206243232Shrs				}
3207243232Shrs				memcpy(iffp, &iff, sizeof(*iffp));
3208243233Shrs#if 0
3209243233Shrs				syslog(LOG_INFO, "Add filter: type %d, ifname %s.", iffp->iff_type, ifname);
3210243233Shrs#endif
3211243232Shrs				TAILQ_INSERT_HEAD(&ifcp->ifc_iff_head, iffp, iff_next);
321278064Sume			}
321355163Sshin		}
321478064Sume
321578064Sume		/*
321678064Sume		 * -A: aggregate configuration.
321778064Sume		 */
3218243232Shrs		if (filtertype[i] != IFIL_TYPE_A)
321955163Sshin			continue;
322055163Sshin		/* put the aggregate to the kernel routing table */
322155163Sshin		rrt = (struct riprt *)malloc(sizeof(struct riprt));
322278064Sume		if (rrt == NULL) {
322355163Sshin			fatal("malloc: rrt");
322478064Sume			/*NOTREACHED*/
322578064Sume		}
322655163Sshin		memset(rrt, 0, sizeof(struct riprt));
3227243232Shrs		rrt->rrt_info.rip6_dest = iff.iff_addr;
3228243232Shrs		rrt->rrt_info.rip6_plen = iff.iff_plen;
322955163Sshin		rrt->rrt_info.rip6_metric = 1;
323055163Sshin		rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
323155163Sshin		rrt->rrt_gw = in6addr_loopback;
323262607Sitojun		rrt->rrt_flags = RTF_UP | RTF_REJECT;
323362607Sitojun		rrt->rrt_rflags = RRTF_AGGREGATE;
323455163Sshin		rrt->rrt_t = 0;
3235119039Sume		rrt->rrt_index = loopifcp->ifc_index;
323664631Sitojun#if 0
323764631Sitojun		if (getroute(&rrt->rrt_info, &gw)) {
323864631Sitojun#if 0
323964631Sitojun			/*
324064631Sitojun			 * When the address has already been registered in the
3241312022Sngie			 * kernel routing table, it should be removed
324264631Sitojun			 */
324364631Sitojun			delroute(&rrt->rrt_info, &gw);
324464631Sitojun#else
324578064Sume			/* it is safer behavior */
324664631Sitojun			errno = EINVAL;
324764631Sitojun			fatal("%s/%u already in routing table, "
324864631Sitojun			    "cannot aggregate",
324964631Sitojun			    inet6_n2p(&rrt->rrt_info.rip6_dest),
325064631Sitojun			    rrt->rrt_info.rip6_plen);
325178064Sume			/*NOTREACHED*/
325264631Sitojun#endif
325364631Sitojun		}
325464631Sitojun#endif
325555163Sshin		/* Put the route to the list */
3256243232Shrs		TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
325755163Sshin		trace(1, "Aggregate: %s/%d for %s\n",
3258243232Shrs			inet6_n2p(&iff.iff_addr), iff.iff_plen,
3259243232Shrs			loopifcp->ifc_name);
326055163Sshin		/* Add this route to the kernel */
326155163Sshin		if (nflag) 	/* do not modify kernel routing table */
326255163Sshin			continue;
326355163Sshin		addroute(rrt, &in6addr_loopback, loopifcp);
326455163Sshin	}
326555163Sshin}
326655163Sshin
326755163Sshin/***************** utility functions *****************/
326855163Sshin
326955163Sshin/*
327055163Sshin * Returns a pointer to ifac whose address and prefix length matches
327155163Sshin * with the address and prefix length specified in the arguments.
327255163Sshin */
327355163Sshinstruct ifac *
3274243232Shrsifa_match(const struct ifc *ifcp,
3275243232Shrs	const struct in6_addr *ia,
3276243232Shrs	int plen)
327755163Sshin{
3278243232Shrs	struct ifac *ifac;
327955163Sshin
3280243232Shrs	TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
3281243232Shrs		if (IN6_ARE_ADDR_EQUAL(&ifac->ifac_addr, ia) &&
3282243232Shrs		    ifac->ifac_plen == plen)
328355163Sshin			break;
328455163Sshin	}
3285243232Shrs
3286243232Shrs	return (ifac);
328755163Sshin}
328855163Sshin
328955163Sshin/*
329055163Sshin * Return a pointer to riprt structure whose address and prefix length
329155163Sshin * matches with the address and prefix length found in the argument.
329278064Sume * Note: This is not a rtalloc().  Therefore exact match is necessary.
329355163Sshin */
329455163Sshinstruct riprt *
3295243232Shrsrtsearch(struct netinfo6 *np)
329655163Sshin{
329755163Sshin	struct	riprt	*rrt;
329855163Sshin
3299243232Shrs	TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
330055163Sshin		if (rrt->rrt_info.rip6_plen == np->rip6_plen &&
330155163Sshin		    IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
330255163Sshin				       &np->rip6_dest))
3303243232Shrs			break;
330455163Sshin	}
3305243232Shrs
3306243232Shrs	return (rrt);
330755163Sshin}
330855163Sshin
330955163Sshinint
3310243232Shrssin6mask2len(const struct sockaddr_in6 *sin6)
331178064Sume{
331278064Sume
331378064Sume	return mask2len(&sin6->sin6_addr,
331478064Sume	    sin6->sin6_len - offsetof(struct sockaddr_in6, sin6_addr));
331578064Sume}
331678064Sume
331778064Sumeint
3318243232Shrsmask2len(const struct in6_addr *addr, int lenlim)
331955163Sshin{
332055163Sshin	int i = 0, j;
332178064Sume	const u_char *p = (const u_char *)addr;
3322312022Sngie
332355163Sshin	for (j = 0; j < lenlim; j++, p++) {
332455163Sshin		if (*p != 0xff)
332555163Sshin			break;
332655163Sshin		i += 8;
332755163Sshin	}
332855163Sshin	if (j < lenlim) {
332955163Sshin		switch (*p) {
333062607Sitojun#define	MASKLEN(m, l)	case m: do { i += l; break; } while (0)
333162607Sitojun		MASKLEN(0xfe, 7); break;
333262607Sitojun		MASKLEN(0xfc, 6); break;
333362607Sitojun		MASKLEN(0xf8, 5); break;
333462607Sitojun		MASKLEN(0xf0, 4); break;
333562607Sitojun		MASKLEN(0xe0, 3); break;
333662607Sitojun		MASKLEN(0xc0, 2); break;
333762607Sitojun		MASKLEN(0x80, 1); break;
333855163Sshin#undef	MASKLEN
333955163Sshin		}
334055163Sshin	}
334155163Sshin	return i;
334255163Sshin}
334355163Sshin
334455163Sshinvoid
3345243232Shrsapplymask(struct in6_addr *addr, struct in6_addr *mask)
334655163Sshin{
334755163Sshin	int	i;
334855163Sshin	u_long	*p, *q;
334955163Sshin
335055163Sshin	p = (u_long *)addr; q = (u_long *)mask;
335155163Sshin	for (i = 0; i < 4; i++)
335255163Sshin		*p++ &= *q++;
335355163Sshin}
335455163Sshin
335555163Sshinstatic const u_char plent[8] = {
335655163Sshin	0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
335755163Sshin};
335855163Sshin
335955163Sshinvoid
3360243232Shrsapplyplen(struct in6_addr *ia, int plen)
336155163Sshin{
336255163Sshin	u_char	*p;
336355163Sshin	int	i;
336455163Sshin
336555163Sshin	p = ia->s6_addr;
336655163Sshin	for (i = 0; i < 16; i++) {
336755163Sshin		if (plen <= 0)
336855163Sshin			*p = 0;
336955163Sshin		else if (plen < 8)
337055163Sshin			*p &= plent[plen];
337155163Sshin		p++, plen -= 8;
337255163Sshin	}
337355163Sshin}
337455163Sshin
337555163Sshinstatic const int pl2m[9] = {
337655163Sshin	0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
337755163Sshin};
337855163Sshin
337955163Sshinstruct in6_addr *
3380243232Shrsplen2mask(int n)
338155163Sshin{
338255163Sshin	static struct in6_addr ia;
338355163Sshin	u_char	*p;
338455163Sshin	int	i;
338555163Sshin
338655163Sshin	memset(&ia, 0, sizeof(struct in6_addr));
338755163Sshin	p = (u_char *)&ia;
338855163Sshin	for (i = 0; i < 16; i++, p++, n -= 8) {
338955163Sshin		if (n >= 8) {
339055163Sshin			*p = 0xff;
339155163Sshin			continue;
339255163Sshin		}
339355163Sshin		*p = pl2m[n];
339455163Sshin		break;
339555163Sshin	}
339655163Sshin	return &ia;
339755163Sshin}
339855163Sshin
339955163Sshinchar *
3400243232Shrsallocopy(char *p)
340155163Sshin{
3402119033Sume	int len = strlen(p) + 1;
3403119033Sume	char *q = (char *)malloc(len);
340455163Sshin
3405119033Sume	if (!q) {
3406119033Sume		fatal("malloc");
3407119033Sume		/*NOTREACHED*/
3408119033Sume	}
3409119033Sume
3410119033Sume	strlcpy(q, p, len);
341155163Sshin	return q;
341255163Sshin}
341355163Sshin
341455163Sshinchar *
3415243232Shrshms(void)
341655163Sshin{
341755163Sshin	static char buf[BUFSIZ];
341855163Sshin	time_t t;
341955163Sshin	struct	tm *tm;
342055163Sshin
342155163Sshin	t = time(NULL);
342278064Sume	if ((tm = localtime(&t)) == 0) {
342355163Sshin		fatal("localtime");
342478064Sume		/*NOTREACHED*/
342578064Sume	}
342678064Sume	snprintf(buf, sizeof(buf), "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
342778064Sume	    tm->tm_sec);
342855163Sshin	return buf;
342955163Sshin}
343055163Sshin
343155163Sshin#define	RIPRANDDEV	1.0	/* 30 +- 15, max - min = 30 */
343255163Sshin
343355163Sshinint
3434243232Shrsripinterval(int timer)
343555163Sshin{
343655163Sshin	double r = rand();
343755163Sshin
343855163Sshin	interval = (int)(timer + timer * RIPRANDDEV * (r / RAND_MAX - 0.5));
343955163Sshin	nextalarm = time(NULL) + interval;
344055163Sshin	return interval;
344155163Sshin}
344255163Sshin
344355163Sshintime_t
3444243232Shrsripsuptrig(void)
344555163Sshin{
344655163Sshin	time_t t;
344755163Sshin
344855163Sshin	double r = rand();
3449312022Sngie	t  = (int)(RIP_TRIG_INT6_MIN +
345078064Sume		(RIP_TRIG_INT6_MAX - RIP_TRIG_INT6_MIN) * (r / RAND_MAX));
345155163Sshin	sup_trig_update = time(NULL) + t;
345255163Sshin	return t;
345355163Sshin}
345455163Sshin
345555163Sshinvoid
345655163Sshin#ifdef __STDC__
345755163Sshinfatal(const char *fmt, ...)
345855163Sshin#else
345955163Sshinfatal(fmt, va_alist)
346055163Sshin	char	*fmt;
346155163Sshin	va_dcl
346255163Sshin#endif
346355163Sshin{
346455163Sshin	va_list ap;
346555163Sshin	char buf[1024];
346655163Sshin
346755163Sshin#ifdef __STDC__
346855163Sshin	va_start(ap, fmt);
346955163Sshin#else
347055163Sshin	va_start(ap);
347155163Sshin#endif
347255163Sshin	vsnprintf(buf, sizeof(buf), fmt, ap);
3473119043Sume	va_end(ap);
347455163Sshin	perror(buf);
3475119043Sume	if (errno)
3476119043Sume		syslog(LOG_ERR, "%s: %s", buf, strerror(errno));
3477119043Sume	else
3478119043Sume		syslog(LOG_ERR, "%s", buf);
347978064Sume	rtdexit();
348055163Sshin}
348155163Sshin
348255163Sshinvoid
348355163Sshin#ifdef __STDC__
348455163Sshintracet(int level, const char *fmt, ...)
348555163Sshin#else
348655163Sshintracet(level, fmt, va_alist)
348755163Sshin	int level;
348855163Sshin	char *fmt;
348955163Sshin	va_dcl
349055163Sshin#endif
349155163Sshin{
349255163Sshin	va_list ap;
349355163Sshin
3494119043Sume	if (level <= dflag) {
349555163Sshin#ifdef __STDC__
3496119043Sume		va_start(ap, fmt);
349755163Sshin#else
3498119043Sume		va_start(ap);
349955163Sshin#endif
350055163Sshin		fprintf(stderr, "%s: ", hms());
350155163Sshin		vfprintf(stderr, fmt, ap);
3502119043Sume		va_end(ap);
350355163Sshin	}
350455163Sshin	if (dflag) {
3505119043Sume#ifdef __STDC__
3506119043Sume		va_start(ap, fmt);
3507119043Sume#else
3508119043Sume		va_start(ap);
3509119043Sume#endif
351055163Sshin		if (level > 0)
351155163Sshin			vsyslog(LOG_DEBUG, fmt, ap);
351255163Sshin		else
351355163Sshin			vsyslog(LOG_WARNING, fmt, ap);
3514119043Sume		va_end(ap);
351555163Sshin	}
351655163Sshin}
351755163Sshin
351855163Sshinvoid
351955163Sshin#ifdef __STDC__
352055163Sshintrace(int level, const char *fmt, ...)
352155163Sshin#else
352255163Sshintrace(level, fmt, va_alist)
352355163Sshin	int level;
352455163Sshin	char *fmt;
352555163Sshin	va_dcl
352655163Sshin#endif
352755163Sshin{
352855163Sshin	va_list ap;
352955163Sshin
3530119043Sume	if (level <= dflag) {
353155163Sshin#ifdef __STDC__
3532119043Sume		va_start(ap, fmt);
353355163Sshin#else
3534119043Sume		va_start(ap);
353555163Sshin#endif
353655163Sshin		vfprintf(stderr, fmt, ap);
3537119043Sume		va_end(ap);
3538119043Sume	}
353955163Sshin	if (dflag) {
3540119043Sume#ifdef __STDC__
3541119043Sume		va_start(ap, fmt);
3542119043Sume#else
3543119043Sume		va_start(ap);
3544119043Sume#endif
354555163Sshin		if (level > 0)
354655163Sshin			vsyslog(LOG_DEBUG, fmt, ap);
354755163Sshin		else
354855163Sshin			vsyslog(LOG_WARNING, fmt, ap);
3549119043Sume		va_end(ap);
355055163Sshin	}
355155163Sshin}
355255163Sshin
355355163Sshinunsigned int
3554243232Shrsif_maxindex(void)
355555163Sshin{
355655163Sshin	struct if_nameindex *p, *p0;
355755163Sshin	unsigned int max = 0;
355855163Sshin
355955163Sshin	p0 = if_nameindex();
356055163Sshin	for (p = p0; p && p->if_index && p->if_name; p++) {
356155163Sshin		if (max < p->if_index)
356255163Sshin			max = p->if_index;
356355163Sshin	}
356455163Sshin	if_freenameindex(p0);
356555163Sshin	return max;
356655163Sshin}
356755163Sshin
356855163Sshinstruct ifc *
3569243232Shrsifc_find(char *name)
357055163Sshin{
357155163Sshin	struct ifc *ifcp;
357255163Sshin
3573243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
357455163Sshin		if (strcmp(name, ifcp->ifc_name) == 0)
3575243232Shrs			break;
357655163Sshin	}
3577243232Shrs	return (ifcp);
357855163Sshin}
357955163Sshin
358055163Sshinstruct iff *
3581243232Shrsiff_find(struct ifc *ifcp, int type)
358255163Sshin{
358355163Sshin	struct iff *iffp;
358455163Sshin
3585243232Shrs	TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
3586243232Shrs		if (type == IFIL_TYPE_ANY ||
3587243232Shrs		    type == iffp->iff_type)
3588243232Shrs			break;
358955163Sshin	}
3590243232Shrs
3591243232Shrs	return (iffp);
359255163Sshin}
359355163Sshin
359455163Sshinvoid
3595243232Shrssetindex2ifc(int idx, struct ifc *ifcp)
359655163Sshin{
3597122677Sume	int n, nsize;
359862607Sitojun	struct ifc **p;
359955163Sshin
360055163Sshin	if (!index2ifc) {
360155163Sshin		nindex2ifc = 5;	/*initial guess*/
360255163Sshin		index2ifc = (struct ifc **)
360355163Sshin			malloc(sizeof(*index2ifc) * nindex2ifc);
360478064Sume		if (index2ifc == NULL) {
360555163Sshin			fatal("malloc");
360678064Sume			/*NOTREACHED*/
360778064Sume		}
360855163Sshin		memset(index2ifc, 0, sizeof(*index2ifc) * nindex2ifc);
360955163Sshin	}
361055163Sshin	n = nindex2ifc;
3611122677Sume	for (nsize = nindex2ifc; nsize <= idx; nsize *= 2)
3612122677Sume		;
3613122677Sume	if (n != nsize) {
361462607Sitojun		p = (struct ifc **)realloc(index2ifc,
3615122677Sume		    sizeof(*index2ifc) * nsize);
361678064Sume		if (p == NULL) {
361755163Sshin			fatal("realloc");
361878064Sume			/*NOTREACHED*/
361978064Sume		}
362078064Sume		memset(p + n, 0, sizeof(*index2ifc) * (nindex2ifc - n));
362162607Sitojun		index2ifc = p;
3622122677Sume		nindex2ifc = nsize;
362355163Sshin	}
362478064Sume	index2ifc[idx] = ifcp;
362555163Sshin}
3626