route6d.c revision 314427
162607Sitojun/*	$FreeBSD: stable/11/usr.sbin/route6d/route6d.c 314427 2017-02-28 22:49:41Z asomers $	*/
2122677Sume/*	$KAME: route6d.c,v 1.104 2003/10/31 00:30:20 itojun Exp $	*/
355163Sshin
455163Sshin/*
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
2206243232Shrs				TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
2207119042Sume				delroute(&rrt->rrt_info, &rrt->rrt_gw);
220878064Sume			}
220955163Sshin			/* Attach the route to the list */
221062607Sitojun			trace(1, "route: %s/%d: register route (%s)\n",
221162607Sitojun			    inet6_n2p(&np->rip6_dest), np->rip6_plen,
221262607Sitojun			    ifcp->ifc_name);
2213243232Shrs			TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
221478064Sume			addroute(rrt, &rrt->rrt_gw, ifcp);
2215119042Sume			rrt = NULL;
221678064Sume			sendrequest(ifcp);
221778064Sume			ripsend(ifcp, &ifcp->ifc_ripsin, 0);
221878064Sume			need_trigger = 1;
221955163Sshin		} else {
2220243232Shrs			TAILQ_FOREACH(loop_rrt, &riprt_head, rrt_next) {
222178064Sume				if (loop_rrt->rrt_index == ifcp->ifc_index) {
222278064Sume					t_lifetime = time(NULL) - RIP_LIFETIME;
222378064Sume					if (loop_rrt->rrt_t == 0 || loop_rrt->rrt_t > t_lifetime) {
222478064Sume						loop_rrt->rrt_t = t_lifetime;
222578064Sume						loop_rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
222678064Sume						loop_rrt->rrt_rflags |= RRTF_CHANGED;
222778064Sume						need_trigger = 1;
222878064Sume					}
222978064Sume				}
223055163Sshin			}
223178064Sume                }
2232119042Sume	next:
2233119042Sume		if (rrt)
2234119042Sume			free(rrt);
223562607Sitojun	}
223678064Sume	return need_trigger;
223762607Sitojun}
223855163Sshin
223962607Sitojun/*
224062607Sitojun * there are couple of p2p interface routing models.  "behavior" lets
224162607Sitojun * you pick one.  it looks that gated behavior fits best with BSDs,
2242119035Sume * since BSD kernels do not look at prefix length on p2p interfaces.
224362607Sitojun */
224462607Sitojunvoid
2245243232Shrsifrt_p2p(struct ifc *ifcp, int again)
224662607Sitojun{
2247243232Shrs	struct ifac *ifac;
2248243232Shrs	struct riprt *rrt, *orrt;
224962607Sitojun	struct netinfo6 *np;
225062607Sitojun	struct in6_addr addr, dest;
225162607Sitojun	int advert, ignore, i;
225262607Sitojun#define P2PADVERT_NETWORK	1
225362607Sitojun#define P2PADVERT_ADDR		2
225462607Sitojun#define P2PADVERT_DEST		4
225562607Sitojun#define P2PADVERT_MAX		4
225662607Sitojun	const enum { CISCO, GATED, ROUTE6D } behavior = GATED;
225778064Sume	const char *category = "";
225862607Sitojun	const char *noadv;
225962607Sitojun
2260243232Shrs	TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2261243232Shrs		addr = ifac->ifac_addr;
2262243232Shrs		dest = ifac->ifac_raddr;
2263243232Shrs		applyplen(&addr, ifac->ifac_plen);
2264243232Shrs		applyplen(&dest, ifac->ifac_plen);
226562607Sitojun		advert = ignore = 0;
226662607Sitojun		switch (behavior) {
226762607Sitojun		case CISCO:
226862607Sitojun			/*
226962607Sitojun			 * honor addr/plen, just like normal shared medium
227062607Sitojun			 * interface.  this may cause trouble if you reuse
227162607Sitojun			 * addr/plen on other interfaces.
227262607Sitojun			 *
227362607Sitojun			 * advertise addr/plen.
227462607Sitojun			 */
227562607Sitojun			advert |= P2PADVERT_NETWORK;
227662607Sitojun			break;
227762607Sitojun		case GATED:
227862607Sitojun			/*
227962607Sitojun			 * prefixlen on p2p interface is meaningless.
228062607Sitojun			 * advertise addr/128 and dest/128.
228162607Sitojun			 *
228262607Sitojun			 * do not install network route to route6d routing
228362607Sitojun			 * table (if we do, it would prevent route installation
228462607Sitojun			 * for other p2p interface that shares addr/plen).
228578064Sume			 *
228678064Sume			 * XXX what should we do if dest is ::?  it will not
228778064Sume			 * get announced anyways (see following filter),
228878064Sume			 * but we need to think.
228962607Sitojun			 */
229062607Sitojun			advert |= P2PADVERT_ADDR;
229162607Sitojun			advert |= P2PADVERT_DEST;
229262607Sitojun			ignore |= P2PADVERT_NETWORK;
229362607Sitojun			break;
229462607Sitojun		case ROUTE6D:
229562607Sitojun			/*
229678064Sume			 * just for testing.  actually the code is redundant
229778064Sume			 * given the current p2p interface address assignment
229878064Sume			 * rule for kame kernel.
229978064Sume			 *
230078064Sume			 * intent:
230178064Sume			 *	A/n -> announce A/n
230278064Sume			 *	A B/n, A and B share prefix -> A/n (= B/n)
230378064Sume			 *	A B/n, do not share prefix -> A/128 and B/128
230478064Sume			 * actually, A/64 and A B/128 are the only cases
230578064Sume			 * permitted by the kernel:
230678064Sume			 *	A/64 -> A/64
230778064Sume			 *	A B/128 -> A/128 and B/128
230862607Sitojun			 */
2309243232Shrs			if (!IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_raddr)) {
231078064Sume				if (IN6_ARE_ADDR_EQUAL(&addr, &dest))
231178064Sume					advert |= P2PADVERT_NETWORK;
231278064Sume				else {
231378064Sume					advert |= P2PADVERT_ADDR;
231478064Sume					advert |= P2PADVERT_DEST;
231578064Sume					ignore |= P2PADVERT_NETWORK;
231678064Sume				}
231778064Sume			} else
231862607Sitojun				advert |= P2PADVERT_NETWORK;
231962607Sitojun			break;
232062607Sitojun		}
232162607Sitojun
232262607Sitojun		for (i = 1; i <= P2PADVERT_MAX; i *= 2) {
232362607Sitojun			if ((ignore & i) != 0)
232462607Sitojun				continue;
232578064Sume			if ((rrt = MALLOC(struct riprt)) == NULL) {
232655163Sshin				fatal("malloc: struct riprt");
232778064Sume				/*NOTREACHED*/
232878064Sume			}
232962607Sitojun			memset(rrt, 0, sizeof(*rrt));
233055163Sshin			rrt->rrt_same = NULL;
233155163Sshin			rrt->rrt_index = ifcp->ifc_index;
233262607Sitojun			rrt->rrt_t = 0;	/* don't age */
233362607Sitojun			switch (i) {
233462607Sitojun			case P2PADVERT_NETWORK:
2335243232Shrs				rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2336243232Shrs				rrt->rrt_info.rip6_plen = ifac->ifac_plen;
233762607Sitojun				applyplen(&rrt->rrt_info.rip6_dest,
2338243232Shrs				    ifac->ifac_plen);
233962607Sitojun				category = "network";
234062607Sitojun				break;
234162607Sitojun			case P2PADVERT_ADDR:
2342243232Shrs				rrt->rrt_info.rip6_dest = ifac->ifac_addr;
234362607Sitojun				rrt->rrt_info.rip6_plen = 128;
234478064Sume				rrt->rrt_gw = in6addr_loopback;
234562607Sitojun				category = "addr";
234662607Sitojun				break;
234762607Sitojun			case P2PADVERT_DEST:
2348243232Shrs				rrt->rrt_info.rip6_dest = ifac->ifac_raddr;
234962607Sitojun				rrt->rrt_info.rip6_plen = 128;
2350243232Shrs				rrt->rrt_gw = ifac->ifac_addr;
235162607Sitojun				category = "dest";
235262607Sitojun				break;
235362607Sitojun			}
235462607Sitojun			if (IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_info.rip6_dest) ||
235562607Sitojun			    IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_info.rip6_dest)) {
235662607Sitojun#if 0
235762607Sitojun				trace(1, "route: %s: skip unspec/linklocal "
235862607Sitojun				    "(%s on %s)\n", category, ifcp->ifc_name);
235962607Sitojun#endif
236062607Sitojun				free(rrt);
236162607Sitojun				continue;
236262607Sitojun			}
236362607Sitojun			if ((advert & i) == 0) {
236462607Sitojun				rrt->rrt_rflags |= RRTF_NOADVERTISE;
236562607Sitojun				noadv = ", NO-ADV";
236662607Sitojun			} else
236762607Sitojun				noadv = "";
236855163Sshin			rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
236962607Sitojun			rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
237055163Sshin			np = &rrt->rrt_info;
2371243232Shrs			orrt = rtsearch(np);
237278064Sume			if (!orrt) {
237355163Sshin				/* Attach the route to the list */
237462607Sitojun				trace(1, "route: %s/%d: register route "
237562607Sitojun				    "(%s on %s%s)\n",
237662607Sitojun				    inet6_n2p(&np->rip6_dest), np->rip6_plen,
237762607Sitojun				    category, ifcp->ifc_name, noadv);
2378243232Shrs				TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
237978064Sume			} else if (rrt->rrt_index != orrt->rrt_index ||
238078064Sume			    rrt->rrt_info.rip6_metric != orrt->rrt_info.rip6_metric) {
2381243232Shrs				/* replace route */
2382243232Shrs				TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2383243232Shrs				TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
238478064Sume				free(orrt);
238578064Sume
238678064Sume				trace(1, "route: %s/%d: update (%s on %s%s)\n",
238778064Sume				    inet6_n2p(&np->rip6_dest), np->rip6_plen,
238878064Sume				    category, ifcp->ifc_name, noadv);
238955163Sshin			} else {
239055163Sshin				/* Already found */
239155163Sshin				if (!again) {
239262607Sitojun					trace(1, "route: %s/%d: "
239362607Sitojun					    "already registered (%s on %s%s)\n",
239462607Sitojun					    inet6_n2p(&np->rip6_dest),
239562607Sitojun					    np->rip6_plen, category,
239662607Sitojun					    ifcp->ifc_name, noadv);
239755163Sshin				}
239855163Sshin				free(rrt);
239955163Sshin			}
240055163Sshin		}
240155163Sshin	}
240262607Sitojun#undef P2PADVERT_NETWORK
240362607Sitojun#undef P2PADVERT_ADDR
240462607Sitojun#undef P2PADVERT_DEST
240562607Sitojun#undef P2PADVERT_MAX
240655163Sshin}
240755163Sshin
240855163Sshinint
2409243232Shrsgetifmtu(int ifindex)
241055163Sshin{
241155163Sshin	int	mib[6];
241255163Sshin	char	*buf;
241355163Sshin	size_t	msize;
241455163Sshin	struct	if_msghdr *ifm;
241555163Sshin	int	mtu;
241655163Sshin
241755163Sshin	mib[0] = CTL_NET;
241855163Sshin	mib[1] = PF_ROUTE;
241955163Sshin	mib[2] = 0;
242055163Sshin	mib[3] = AF_INET6;
242155163Sshin	mib[4] = NET_RT_IFLIST;
242255163Sshin	mib[5] = ifindex;
2423312022Sngie	if (sysctl(mib, nitems(mib), NULL, &msize, NULL, 0) < 0) {
242455163Sshin		fatal("sysctl estimate NET_RT_IFLIST");
242578064Sume		/*NOTREACHED*/
242678064Sume	}
242778064Sume	if ((buf = malloc(msize)) == NULL) {
242855163Sshin		fatal("malloc");
242978064Sume		/*NOTREACHED*/
243078064Sume	}
2431312022Sngie	if (sysctl(mib, nitems(mib), buf, &msize, NULL, 0) < 0) {
243255163Sshin		fatal("sysctl NET_RT_IFLIST");
243378064Sume		/*NOTREACHED*/
243478064Sume	}
243555163Sshin	ifm = (struct if_msghdr *)buf;
243655163Sshin	mtu = ifm->ifm_data.ifi_mtu;
243778064Sume	if (ifindex != ifm->ifm_index) {
243855163Sshin		fatal("ifindex does not match with ifm_index");
243978064Sume		/*NOTREACHED*/
244078064Sume	}
244155163Sshin	free(buf);
244255163Sshin	return mtu;
244355163Sshin}
244455163Sshin
244555163Sshinconst char *
2446243232Shrsrttypes(struct rt_msghdr *rtm)
244755163Sshin{
244862607Sitojun#define	RTTYPE(s, f) \
244962607Sitojundo { \
245062607Sitojun	if (rtm->rtm_type == (f)) \
245162607Sitojun		return (s); \
245262607Sitojun} while (0)
245355163Sshin	RTTYPE("ADD", RTM_ADD);
245455163Sshin	RTTYPE("DELETE", RTM_DELETE);
245555163Sshin	RTTYPE("CHANGE", RTM_CHANGE);
245655163Sshin	RTTYPE("GET", RTM_GET);
245755163Sshin	RTTYPE("LOSING", RTM_LOSING);
245855163Sshin	RTTYPE("REDIRECT", RTM_REDIRECT);
245955163Sshin	RTTYPE("MISS", RTM_MISS);
246055163Sshin	RTTYPE("LOCK", RTM_LOCK);
246155163Sshin	RTTYPE("NEWADDR", RTM_NEWADDR);
246255163Sshin	RTTYPE("DELADDR", RTM_DELADDR);
246355163Sshin	RTTYPE("IFINFO", RTM_IFINFO);
246478064Sume#ifdef RTM_OIFINFO
246578064Sume	RTTYPE("OIFINFO", RTM_OIFINFO);
246678064Sume#endif
246778064Sume#ifdef RTM_IFANNOUNCE
246878064Sume	RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE);
246978064Sume#endif
247078064Sume#ifdef RTM_NEWMADDR
247178064Sume	RTTYPE("NEWMADDR", RTM_NEWMADDR);
247278064Sume#endif
247378064Sume#ifdef RTM_DELMADDR
247478064Sume	RTTYPE("DELMADDR", RTM_DELMADDR);
247578064Sume#endif
247655163Sshin#undef RTTYPE
247755163Sshin	return NULL;
247855163Sshin}
247955163Sshin
248055163Sshinconst char *
2481243232Shrsrtflags(struct rt_msghdr *rtm)
248255163Sshin{
248355163Sshin	static char buf[BUFSIZ];
248455163Sshin
248578064Sume	/*
248678064Sume	 * letter conflict should be okay.  painful when *BSD diverges...
248778064Sume	 */
248878064Sume	strlcpy(buf, "", sizeof(buf));
248962607Sitojun#define	RTFLAG(s, f) \
249062607Sitojundo { \
249162607Sitojun	if (rtm->rtm_flags & (f)) \
249278064Sume		strlcat(buf, (s), sizeof(buf)); \
249362607Sitojun} while (0)
249455163Sshin	RTFLAG("U", RTF_UP);
249555163Sshin	RTFLAG("G", RTF_GATEWAY);
249655163Sshin	RTFLAG("H", RTF_HOST);
249755163Sshin	RTFLAG("R", RTF_REJECT);
249855163Sshin	RTFLAG("D", RTF_DYNAMIC);
249955163Sshin	RTFLAG("M", RTF_MODIFIED);
250055163Sshin	RTFLAG("d", RTF_DONE);
250155163Sshin#ifdef	RTF_MASK
250255163Sshin	RTFLAG("m", RTF_MASK);
250355163Sshin#endif
250478064Sume#ifdef RTF_CLONED
250578064Sume	RTFLAG("c", RTF_CLONED);
250678064Sume#endif
250755163Sshin	RTFLAG("X", RTF_XRESOLVE);
2508186119Sqingli#ifdef RTF_LLINFO
250955163Sshin	RTFLAG("L", RTF_LLINFO);
2510186119Sqingli#endif
251155163Sshin	RTFLAG("S", RTF_STATIC);
251255163Sshin	RTFLAG("B", RTF_BLACKHOLE);
251378064Sume#ifdef RTF_PROTO3
251478064Sume	RTFLAG("3", RTF_PROTO3);
251578064Sume#endif
251655163Sshin	RTFLAG("2", RTF_PROTO2);
251755163Sshin	RTFLAG("1", RTF_PROTO1);
251878064Sume#ifdef RTF_BROADCAST
251978064Sume	RTFLAG("b", RTF_BROADCAST);
252078064Sume#endif
252178064Sume#ifdef RTF_DEFAULT
252278064Sume	RTFLAG("d", RTF_DEFAULT);
252378064Sume#endif
252478064Sume#ifdef RTF_ISAROUTER
252578064Sume	RTFLAG("r", RTF_ISAROUTER);
252678064Sume#endif
252778064Sume#ifdef RTF_TUNNEL
252878064Sume	RTFLAG("T", RTF_TUNNEL);
252978064Sume#endif
253078064Sume#ifdef RTF_AUTH
253178064Sume	RTFLAG("A", RTF_AUTH);
253278064Sume#endif
253378064Sume#ifdef RTF_CRYPT
253478064Sume	RTFLAG("E", RTF_CRYPT);
253578064Sume#endif
253655163Sshin#undef RTFLAG
253755163Sshin	return buf;
253855163Sshin}
253955163Sshin
254055163Sshinconst char *
2541243232Shrsifflags(int flags)
254255163Sshin{
254355163Sshin	static char buf[BUFSIZ];
254455163Sshin
254578064Sume	strlcpy(buf, "", sizeof(buf));
254662607Sitojun#define	IFFLAG(s, f) \
254762607Sitojundo { \
2548119040Sume	if (flags & (f)) { \
254962607Sitojun		if (buf[0]) \
255078064Sume			strlcat(buf, ",", sizeof(buf)); \
2551119040Sume		strlcat(buf, (s), sizeof(buf)); \
255262607Sitojun	} \
255362607Sitojun} while (0)
255455163Sshin	IFFLAG("UP", IFF_UP);
255555163Sshin	IFFLAG("BROADCAST", IFF_BROADCAST);
255655163Sshin	IFFLAG("DEBUG", IFF_DEBUG);
255755163Sshin	IFFLAG("LOOPBACK", IFF_LOOPBACK);
255855163Sshin	IFFLAG("POINTOPOINT", IFF_POINTOPOINT);
255955163Sshin#ifdef IFF_NOTRAILERS
256055163Sshin	IFFLAG("NOTRAILERS", IFF_NOTRAILERS);
256155163Sshin#endif
256255163Sshin	IFFLAG("RUNNING", IFF_RUNNING);
256355163Sshin	IFFLAG("NOARP", IFF_NOARP);
256455163Sshin	IFFLAG("PROMISC", IFF_PROMISC);
256555163Sshin	IFFLAG("ALLMULTI", IFF_ALLMULTI);
256655163Sshin	IFFLAG("OACTIVE", IFF_OACTIVE);
256755163Sshin	IFFLAG("SIMPLEX", IFF_SIMPLEX);
256855163Sshin	IFFLAG("LINK0", IFF_LINK0);
256955163Sshin	IFFLAG("LINK1", IFF_LINK1);
257055163Sshin	IFFLAG("LINK2", IFF_LINK2);
257155163Sshin	IFFLAG("MULTICAST", IFF_MULTICAST);
257255163Sshin#undef IFFLAG
257355163Sshin	return buf;
257455163Sshin}
257555163Sshin
257655163Sshinvoid
2577243232Shrskrtread(int again)
257855163Sshin{
257955163Sshin	int mib[6];
258055163Sshin	size_t msize;
258155163Sshin	char *buf, *p, *lim;
258255163Sshin	struct rt_msghdr *rtm;
258355163Sshin	int retry;
258455163Sshin	const char *errmsg;
258555163Sshin
258655163Sshin	retry = 0;
258755163Sshin	buf = NULL;
258855163Sshin	mib[0] = CTL_NET;
258955163Sshin	mib[1] = PF_ROUTE;
259055163Sshin	mib[2] = 0;
259155163Sshin	mib[3] = AF_INET6;	/* Address family */
259255163Sshin	mib[4] = NET_RT_DUMP;	/* Dump the kernel routing table */
259355163Sshin	mib[5] = 0;		/* No flags */
259455163Sshin	do {
2595243232Shrs		if (retry)
2596243232Shrs			sleep(1);
259755163Sshin		retry++;
259855163Sshin		errmsg = NULL;
2599299491Scem		if (buf) {
260055163Sshin			free(buf);
2601299491Scem			buf = NULL;
2602299491Scem		}
2603312022Sngie		if (sysctl(mib, nitems(mib), NULL, &msize, NULL, 0) < 0) {
260455163Sshin			errmsg = "sysctl estimate";
260555163Sshin			continue;
260655163Sshin		}
260755163Sshin		if ((buf = malloc(msize)) == NULL) {
260855163Sshin			errmsg = "malloc";
260955163Sshin			continue;
261055163Sshin		}
2611312022Sngie		if (sysctl(mib, nitems(mib), buf, &msize, NULL, 0) < 0) {
261255163Sshin			errmsg = "sysctl NET_RT_DUMP";
261355163Sshin			continue;
261455163Sshin		}
2615243233Shrs	} while (retry < RT_DUMP_MAXRETRY && errmsg != NULL);
261678064Sume	if (errmsg) {
261769279Sume		fatal("%s (with %d retries, msize=%lu)", errmsg, retry,
261869279Sume		    (u_long)msize);
261978064Sume		/*NOTREACHED*/
262078064Sume	} else if (1 < retry)
262155163Sshin		syslog(LOG_INFO, "NET_RT_DUMP %d retires", retry);
262255163Sshin
262355163Sshin	lim = buf + msize;
262455163Sshin	for (p = buf; p < lim; p += rtm->rtm_msglen) {
262555163Sshin		rtm = (struct rt_msghdr *)p;
262655163Sshin		rt_entry(rtm, again);
262755163Sshin	}
262855163Sshin	free(buf);
262955163Sshin}
263055163Sshin
263155163Sshinvoid
2632243232Shrsrt_entry(struct rt_msghdr *rtm, int again)
263355163Sshin{
263455163Sshin	struct	sockaddr_in6 *sin6_dst, *sin6_gw, *sin6_mask;
263555163Sshin	struct	sockaddr_in6 *sin6_genmask, *sin6_ifp;
263655163Sshin	char	*rtmp, *ifname = NULL;
263778064Sume	struct	riprt *rrt, *orrt;
263855163Sshin	struct	netinfo6 *np;
2639243232Shrs	int ifindex;
264055163Sshin
264155163Sshin	sin6_dst = sin6_gw = sin6_mask = sin6_genmask = sin6_ifp = 0;
264255163Sshin	if ((rtm->rtm_flags & RTF_UP) == 0 || rtm->rtm_flags &
2643186119Sqingli		(RTF_XRESOLVE|RTF_BLACKHOLE)) {
264455163Sshin		return;		/* not interested in the link route */
264562607Sitojun	}
264669279Sume	/* do not look at cloned routes */
264769279Sume#ifdef RTF_WASCLONED
264869279Sume	if (rtm->rtm_flags & RTF_WASCLONED)
264969279Sume		return;
265069279Sume#endif
265169279Sume#ifdef RTF_CLONED
265269279Sume	if (rtm->rtm_flags & RTF_CLONED)
265369279Sume		return;
265469279Sume#endif
2655243233Shrs	/* XXX: Ignore connected routes. */
2656243233Shrs	if (!(rtm->rtm_flags & (RTF_GATEWAY|RTF_HOST|RTF_STATIC)))
2657243233Shrs		return;
265869279Sume	/*
265969279Sume	 * do not look at dynamic routes.
266069279Sume	 * netbsd/openbsd cloned routes have UGHD.
266169279Sume	 */
266269279Sume	if (rtm->rtm_flags & RTF_DYNAMIC)
266369279Sume		return;
266455163Sshin	rtmp = (char *)(rtm + 1);
266555163Sshin	/* Destination */
266655163Sshin	if ((rtm->rtm_addrs & RTA_DST) == 0)
266755163Sshin		return;		/* ignore routes without destination address */
266855163Sshin	sin6_dst = (struct sockaddr_in6 *)rtmp;
266964631Sitojun	rtmp += ROUNDUP(sin6_dst->sin6_len);
267055163Sshin	if (rtm->rtm_addrs & RTA_GATEWAY) {
267155163Sshin		sin6_gw = (struct sockaddr_in6 *)rtmp;
267255163Sshin		rtmp += ROUNDUP(sin6_gw->sin6_len);
267355163Sshin	}
267455163Sshin	if (rtm->rtm_addrs & RTA_NETMASK) {
267555163Sshin		sin6_mask = (struct sockaddr_in6 *)rtmp;
267655163Sshin		rtmp += ROUNDUP(sin6_mask->sin6_len);
267755163Sshin	}
267855163Sshin	if (rtm->rtm_addrs & RTA_GENMASK) {
267955163Sshin		sin6_genmask = (struct sockaddr_in6 *)rtmp;
268055163Sshin		rtmp += ROUNDUP(sin6_genmask->sin6_len);
268155163Sshin	}
268255163Sshin	if (rtm->rtm_addrs & RTA_IFP) {
268355163Sshin		sin6_ifp = (struct sockaddr_in6 *)rtmp;
268455163Sshin		rtmp += ROUNDUP(sin6_ifp->sin6_len);
268555163Sshin	}
268655163Sshin
268755163Sshin	/* Destination */
268855163Sshin	if (sin6_dst->sin6_family != AF_INET6)
268955163Sshin		return;
269055163Sshin	if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst->sin6_addr))
269155163Sshin		return;		/* Link-local */
269255163Sshin	if (IN6_ARE_ADDR_EQUAL(&sin6_dst->sin6_addr, &in6addr_loopback))
269355163Sshin		return;		/* Loopback */
269455163Sshin	if (IN6_IS_ADDR_MULTICAST(&sin6_dst->sin6_addr))
269555163Sshin		return;
269655163Sshin
269778064Sume	if ((rrt = MALLOC(struct riprt)) == NULL) {
269855163Sshin		fatal("malloc: struct riprt");
269978064Sume		/*NOTREACHED*/
270078064Sume	}
270162607Sitojun	memset(rrt, 0, sizeof(*rrt));
270255163Sshin	np = &rrt->rrt_info;
270355163Sshin	rrt->rrt_same = NULL;
270455163Sshin	rrt->rrt_t = time(NULL);
270555163Sshin	if (aflag == 0 && (rtm->rtm_flags & RTF_STATIC))
270655163Sshin		rrt->rrt_t = 0;	/* Don't age static routes */
2707243233Shrs	if (rtm->rtm_flags & Pflag)
2708243233Shrs		rrt->rrt_t = 0;	/* Don't age PROTO[123] routes */
2709122677Sume	if ((rtm->rtm_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST)
2710122677Sume		rrt->rrt_t = 0;	/* Don't age non-gateway host routes */
271155163Sshin	np->rip6_tag = 0;
271255163Sshin	np->rip6_metric = rtm->rtm_rmx.rmx_hopcount;
271355163Sshin	if (np->rip6_metric < 1)
271455163Sshin		np->rip6_metric = 1;
271555163Sshin	rrt->rrt_flags = rtm->rtm_flags;
271655163Sshin	np->rip6_dest = sin6_dst->sin6_addr;
271755163Sshin
271855163Sshin	/* Mask or plen */
271955163Sshin	if (rtm->rtm_flags & RTF_HOST)
272055163Sshin		np->rip6_plen = 128;	/* Host route */
272178064Sume	else if (sin6_mask)
272278064Sume		np->rip6_plen = sin6mask2len(sin6_mask);
272378064Sume	else
272455163Sshin		np->rip6_plen = 0;
272555163Sshin
2726243232Shrs	orrt = rtsearch(np);
272778064Sume	if (orrt && orrt->rrt_info.rip6_metric != HOPCNT_INFINITY6) {
272855163Sshin		/* Already found */
272955163Sshin		if (!again) {
273055163Sshin			trace(1, "route: %s/%d flags %s: already registered\n",
273155163Sshin				inet6_n2p(&np->rip6_dest), np->rip6_plen,
273255163Sshin				rtflags(rtm));
273355163Sshin		}
273455163Sshin		free(rrt);
273555163Sshin		return;
273655163Sshin	}
273755163Sshin	/* Gateway */
273855163Sshin	if (!sin6_gw)
273955163Sshin		memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
274055163Sshin	else {
274155163Sshin		if (sin6_gw->sin6_family == AF_INET6)
274255163Sshin			rrt->rrt_gw = sin6_gw->sin6_addr;
274355163Sshin		else if (sin6_gw->sin6_family == AF_LINK) {
274455163Sshin			/* XXX in case ppp link? */
274555163Sshin			rrt->rrt_gw = in6addr_loopback;
274655163Sshin		} else
274755163Sshin			memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
274855163Sshin	}
274955163Sshin	trace(1, "route: %s/%d flags %s",
275055163Sshin		inet6_n2p(&np->rip6_dest), np->rip6_plen, rtflags(rtm));
275155163Sshin	trace(1, " gw %s", inet6_n2p(&rrt->rrt_gw));
275255163Sshin
275355163Sshin	/* Interface */
2754243232Shrs	ifindex = rtm->rtm_index;
2755243232Shrs	if ((unsigned int)ifindex < nindex2ifc && index2ifc[ifindex])
2756243232Shrs		ifname = index2ifc[ifindex]->ifc_name;
275758070Sshin	else {
275858070Sshin		trace(1, " not configured\n");
275962607Sitojun		free(rrt);
276058070Sshin		return;
276158070Sshin	}
2762243232Shrs	trace(1, " if %s sock %d", ifname, ifindex);
2763243232Shrs	rrt->rrt_index = ifindex;
276455163Sshin
276562607Sitojun	trace(1, "\n");
276662607Sitojun
276755163Sshin	/* Check gateway */
276855163Sshin	if (!IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_gw) &&
2769122677Sume	    !IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw) &&
2770122677Sume	    (rrt->rrt_flags & RTF_LOCAL) == 0) {
277155163Sshin		trace(0, "***** Gateway %s is not a link-local address.\n",
277255163Sshin			inet6_n2p(&rrt->rrt_gw));
277355163Sshin		trace(0, "*****     dest(%s) if(%s) -- Not optimized.\n",
277462607Sitojun			inet6_n2p(&rrt->rrt_info.rip6_dest), ifname);
277562607Sitojun		rrt->rrt_rflags |= RRTF_NH_NOT_LLADDR;
277655163Sshin	}
277755163Sshin
277855163Sshin	/* Put it to the route list */
277978064Sume	if (orrt && orrt->rrt_info.rip6_metric == HOPCNT_INFINITY6) {
278078064Sume		/* replace route list */
2781243232Shrs		TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2782243232Shrs		TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
2783243232Shrs
278478064Sume		trace(1, "route: %s/%d flags %s: replace new route\n",
278578064Sume		    inet6_n2p(&np->rip6_dest), np->rip6_plen,
278678064Sume		    rtflags(rtm));
2787243232Shrs		free(orrt);
2788243232Shrs	} else
2789243232Shrs		TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
279055163Sshin}
279155163Sshin
279255163Sshinint
2793243232Shrsaddroute(struct riprt *rrt,
2794243232Shrs	const struct in6_addr *gw,
2795243232Shrs	struct ifc *ifcp)
279655163Sshin{
279755163Sshin	struct	netinfo6 *np;
279855163Sshin	u_char	buf[BUFSIZ], buf1[BUFSIZ], buf2[BUFSIZ];
279955163Sshin	struct	rt_msghdr	*rtm;
2800119031Sume	struct	sockaddr_in6	*sin6;
280155163Sshin	int	len;
280255163Sshin
280355163Sshin	np = &rrt->rrt_info;
280478064Sume	inet_ntop(AF_INET6, (const void *)gw, (char *)buf1, sizeof(buf1));
280555163Sshin	inet_ntop(AF_INET6, (void *)&ifcp->ifc_mylladdr, (char *)buf2, sizeof(buf2));
280655163Sshin	tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n",
280755163Sshin		inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
280855163Sshin		np->rip6_metric - 1, buf2);
280955163Sshin	if (rtlog)
281055163Sshin		fprintf(rtlog, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(),
281155163Sshin			inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
281255163Sshin			np->rip6_metric - 1, buf2);
281355163Sshin	if (nflag)
281455163Sshin		return 0;
281555163Sshin
281655163Sshin	memset(buf, 0, sizeof(buf));
281755163Sshin	rtm = (struct rt_msghdr *)buf;
281855163Sshin	rtm->rtm_type = RTM_ADD;
281955163Sshin	rtm->rtm_version = RTM_VERSION;
282055163Sshin	rtm->rtm_seq = ++seq;
282155163Sshin	rtm->rtm_pid = pid;
282262607Sitojun	rtm->rtm_flags = rrt->rrt_flags;
2823243233Shrs	rtm->rtm_flags |= Qflag;
282455163Sshin	rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
282555163Sshin	rtm->rtm_rmx.rmx_hopcount = np->rip6_metric - 1;
282655163Sshin	rtm->rtm_inits = RTV_HOPCOUNT;
2827119031Sume	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
282855163Sshin	/* Destination */
2829119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2830119031Sume	sin6->sin6_family = AF_INET6;
2831119031Sume	sin6->sin6_addr = np->rip6_dest;
2832119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
283355163Sshin	/* Gateway */
2834119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2835119031Sume	sin6->sin6_family = AF_INET6;
2836119031Sume	sin6->sin6_addr = *gw;
2837270234Shrs	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
2838270234Shrs		sin6->sin6_scope_id = ifcp->ifc_index;
2839119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
284055163Sshin	/* Netmask */
2841119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2842119031Sume	sin6->sin6_family = AF_INET6;
2843119031Sume	sin6->sin6_addr = *(plen2mask(np->rip6_plen));
2844119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
284555163Sshin
2846119031Sume	len = (char *)sin6 - (char *)buf;
284755163Sshin	rtm->rtm_msglen = len;
284855163Sshin	if (write(rtsock, buf, len) > 0)
284955163Sshin		return 0;
285055163Sshin
285155163Sshin	if (errno == EEXIST) {
285255163Sshin		trace(0, "ADD: Route already exists %s/%d gw %s\n",
2853119035Sume		    inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
285455163Sshin		if (rtlog)
285555163Sshin			fprintf(rtlog, "ADD: Route already exists %s/%d gw %s\n",
2856119035Sume			    inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
285755163Sshin	} else {
285855163Sshin		trace(0, "Can not write to rtsock (addroute): %s\n",
2859119035Sume		    strerror(errno));
286055163Sshin		if (rtlog)
286155163Sshin			fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2862119035Sume			    strerror(errno));
286355163Sshin	}
286455163Sshin	return -1;
286555163Sshin}
286655163Sshin
286755163Sshinint
2868243232Shrsdelroute(struct netinfo6 *np, struct in6_addr *gw)
286955163Sshin{
287055163Sshin	u_char	buf[BUFSIZ], buf2[BUFSIZ];
287155163Sshin	struct	rt_msghdr	*rtm;
2872119031Sume	struct	sockaddr_in6	*sin6;
287355163Sshin	int	len;
287455163Sshin
287555163Sshin	inet_ntop(AF_INET6, (void *)gw, (char *)buf2, sizeof(buf2));
287655163Sshin	tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np->rip6_dest),
287755163Sshin		np->rip6_plen, buf2);
287855163Sshin	if (rtlog)
287955163Sshin		fprintf(rtlog, "%s: DEL: %s/%d gw %s\n",
288055163Sshin			hms(), inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
288155163Sshin	if (nflag)
288255163Sshin		return 0;
288355163Sshin
288455163Sshin	memset(buf, 0, sizeof(buf));
288555163Sshin	rtm = (struct rt_msghdr *)buf;
288655163Sshin	rtm->rtm_type = RTM_DELETE;
288755163Sshin	rtm->rtm_version = RTM_VERSION;
288855163Sshin	rtm->rtm_seq = ++seq;
288955163Sshin	rtm->rtm_pid = pid;
289055163Sshin	rtm->rtm_flags = RTF_UP | RTF_GATEWAY;
2891243233Shrs	rtm->rtm_flags |= Qflag;
289278064Sume	if (np->rip6_plen == sizeof(struct in6_addr) * 8)
289378064Sume		rtm->rtm_flags |= RTF_HOST;
289455163Sshin	rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2895119031Sume	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
289655163Sshin	/* Destination */
2897119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2898119031Sume	sin6->sin6_family = AF_INET6;
2899119031Sume	sin6->sin6_addr = np->rip6_dest;
2900119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
290155163Sshin	/* Gateway */
2902119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2903119031Sume	sin6->sin6_family = AF_INET6;
2904119031Sume	sin6->sin6_addr = *gw;
2905119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
290655163Sshin	/* Netmask */
2907119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2908119031Sume	sin6->sin6_family = AF_INET6;
2909119031Sume	sin6->sin6_addr = *(plen2mask(np->rip6_plen));
2910119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
291155163Sshin
2912119031Sume	len = (char *)sin6 - (char *)buf;
291355163Sshin	rtm->rtm_msglen = len;
291455163Sshin	if (write(rtsock, buf, len) >= 0)
291555163Sshin		return 0;
291655163Sshin
291755163Sshin	if (errno == ESRCH) {
291855163Sshin		trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n",
2919119035Sume		    inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
292055163Sshin		if (rtlog)
292155163Sshin			fprintf(rtlog, "RTDEL: Route does not exist: %s/%d gw %s\n",
2922119035Sume			    inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
292355163Sshin	} else {
292455163Sshin		trace(0, "Can not write to rtsock (delroute): %s\n",
2925119035Sume		    strerror(errno));
292655163Sshin		if (rtlog)
292755163Sshin			fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2928119035Sume			    strerror(errno));
292955163Sshin	}
293055163Sshin	return -1;
293155163Sshin}
293255163Sshin
293355163Sshinstruct in6_addr *
2934243232Shrsgetroute(struct netinfo6 *np, struct in6_addr *gw)
293555163Sshin{
293655163Sshin	u_char buf[BUFSIZ];
2937119085Sume	int myseq;
293855163Sshin	int len;
293955163Sshin	struct rt_msghdr *rtm;
2940119031Sume	struct sockaddr_in6 *sin6;
294155163Sshin
294255163Sshin	rtm = (struct rt_msghdr *)buf;
294355163Sshin	len = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in6);
294455163Sshin	memset(rtm, 0, len);
294555163Sshin	rtm->rtm_type = RTM_GET;
294655163Sshin	rtm->rtm_version = RTM_VERSION;
294755163Sshin	myseq = ++seq;
294855163Sshin	rtm->rtm_seq = myseq;
294955163Sshin	rtm->rtm_addrs = RTA_DST;
295055163Sshin	rtm->rtm_msglen = len;
2951119031Sume	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2952119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2953119031Sume	sin6->sin6_family = AF_INET6;
2954119031Sume	sin6->sin6_addr = np->rip6_dest;
295555163Sshin	if (write(rtsock, buf, len) < 0) {
295655163Sshin		if (errno == ESRCH)	/* No such route found */
295755163Sshin			return NULL;
295855163Sshin		perror("write to rtsock");
295978064Sume		exit(1);
296055163Sshin	}
296155163Sshin	do {
296255163Sshin		if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
296355163Sshin			perror("read from rtsock");
296478064Sume			exit(1);
296555163Sshin		}
296655163Sshin		rtm = (struct rt_msghdr *)buf;
296755163Sshin	} while (rtm->rtm_seq != myseq || rtm->rtm_pid != pid);
2968119031Sume	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
296955163Sshin	if (rtm->rtm_addrs & RTA_DST) {
2970119031Sume		sin6 = (struct sockaddr_in6 *)
2971119031Sume			((char *)sin6 + ROUNDUP(sin6->sin6_len));
297255163Sshin	}
297355163Sshin	if (rtm->rtm_addrs & RTA_GATEWAY) {
2974119031Sume		*gw = sin6->sin6_addr;
297555163Sshin		return gw;
297655163Sshin	}
297755163Sshin	return NULL;
297855163Sshin}
297955163Sshin
298055163Sshinconst char *
2981243232Shrsinet6_n2p(const struct in6_addr *p)
298255163Sshin{
298355163Sshin	static char buf[BUFSIZ];
298455163Sshin
298578064Sume	return inet_ntop(AF_INET6, (const void *)p, buf, sizeof(buf));
298655163Sshin}
298755163Sshin
298855163Sshinvoid
2989243232Shrsifrtdump(int sig)
299055163Sshin{
299155163Sshin
299255163Sshin	ifdump(sig);
299355163Sshin	rtdump(sig);
299455163Sshin}
299555163Sshin
299655163Sshinvoid
2997243232Shrsifdump(int sig)
299855163Sshin{
299955163Sshin	struct ifc *ifcp;
300055163Sshin	FILE *dump;
3001243232Shrs	int nifc = 0;
300255163Sshin
300355163Sshin	if (sig == 0)
300455163Sshin		dump = stderr;
300555163Sshin	else
300655163Sshin		if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
300755163Sshin			dump = stderr;
300855163Sshin
300955163Sshin	fprintf(dump, "%s: Interface Table Dump\n", hms());
3010243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next)
3011243232Shrs		nifc++;
301255163Sshin	fprintf(dump, "  Number of interfaces: %d\n", nifc);
3013243232Shrs
3014243232Shrs	fprintf(dump, "  advertising interfaces:\n");
3015243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3016243232Shrs		if ((ifcp->ifc_flags & IFF_UP) == 0)
3017243232Shrs			continue;
3018243232Shrs		if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
3019243232Shrs			continue;
3020243232Shrs		ifdump0(dump, ifcp);
302155163Sshin	}
302255163Sshin	fprintf(dump, "\n");
3023243232Shrs	fprintf(dump, "  non-advertising interfaces:\n");
3024243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3025243232Shrs		if ((ifcp->ifc_flags & IFF_UP) &&
3026243232Shrs		    (iff_find(ifcp, IFIL_TYPE_N) == NULL))
3027243232Shrs			continue;
3028243232Shrs		ifdump0(dump, ifcp);
3029243232Shrs	}
3030243232Shrs	fprintf(dump, "\n");
303155163Sshin	if (dump != stderr)
303255163Sshin		fclose(dump);
303355163Sshin}
303455163Sshin
303555163Sshinvoid
3036243232Shrsifdump0(FILE *dump, const struct ifc *ifcp)
303755163Sshin{
3038243232Shrs	struct ifac *ifac;
303955163Sshin	struct iff *iffp;
304055163Sshin	char buf[BUFSIZ];
304155163Sshin	const char *ft;
304255163Sshin	int addr;
304355163Sshin
304455163Sshin	fprintf(dump, "    %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n",
304555163Sshin		ifcp->ifc_name, ifcp->ifc_index, ifflags(ifcp->ifc_flags),
304655163Sshin		inet6_n2p(&ifcp->ifc_mylladdr),
304755163Sshin		ifcp->ifc_mtu, ifcp->ifc_metric);
3048243232Shrs	TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
304955163Sshin		if (ifcp->ifc_flags & IFF_POINTOPOINT) {
3050243232Shrs			inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr,
305155163Sshin				buf, sizeof(buf));
305255163Sshin			fprintf(dump, "\t%s/%d -- %s\n",
3053243232Shrs				inet6_n2p(&ifac->ifac_addr),
3054243232Shrs				ifac->ifac_plen, buf);
305555163Sshin		} else {
305655163Sshin			fprintf(dump, "\t%s/%d\n",
3057243232Shrs				inet6_n2p(&ifac->ifac_addr),
3058243232Shrs				ifac->ifac_plen);
305955163Sshin		}
306055163Sshin	}
3061243232Shrs
3062243232Shrs	fprintf(dump, "\tFilter:\n");
3063243232Shrs	TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
3064243232Shrs		addr = 0;
3065243232Shrs		switch (iffp->iff_type) {
3066243232Shrs		case IFIL_TYPE_A:
3067243232Shrs			ft = "Aggregate"; addr++; break;
3068243232Shrs		case IFIL_TYPE_N:
3069243232Shrs			ft = "No-use"; break;
3070243232Shrs		case IFIL_TYPE_O:
3071243232Shrs			ft = "Advertise-only"; addr++; break;
3072243232Shrs		case IFIL_TYPE_T:
3073243232Shrs			ft = "Default-only"; break;
3074243232Shrs		case IFIL_TYPE_L:
3075243232Shrs			ft = "Listen-only"; addr++; break;
3076243232Shrs		default:
3077243232Shrs			snprintf(buf, sizeof(buf), "Unknown-%c", iffp->iff_type);
3078243232Shrs			ft = buf;
3079243232Shrs			addr++;
3080243232Shrs			break;
308155163Sshin		}
3082243232Shrs		fprintf(dump, "\t\t%s", ft);
3083243232Shrs		if (addr)
3084243232Shrs			fprintf(dump, "(%s/%d)", inet6_n2p(&iffp->iff_addr),
3085243232Shrs				iffp->iff_plen);
308655163Sshin		fprintf(dump, "\n");
308755163Sshin	}
3088243232Shrs	fprintf(dump, "\n");
308955163Sshin}
309055163Sshin
309155163Sshinvoid
3092243232Shrsrtdump(int sig)
309355163Sshin{
309455163Sshin	struct	riprt *rrt;
309555163Sshin	char	buf[BUFSIZ];
309655163Sshin	FILE	*dump;
309755163Sshin	time_t	t, age;
309855163Sshin
309955163Sshin	if (sig == 0)
310055163Sshin		dump = stderr;
310155163Sshin	else
310255163Sshin		if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
310355163Sshin			dump = stderr;
310455163Sshin
310555163Sshin	t = time(NULL);
310655163Sshin	fprintf(dump, "\n%s: Routing Table Dump\n", hms());
3107243232Shrs	TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
310855163Sshin		if (rrt->rrt_t == 0)
310955163Sshin			age = 0;
311055163Sshin		else
311155163Sshin			age = t - rrt->rrt_t;
311255163Sshin		inet_ntop(AF_INET6, (void *)&rrt->rrt_info.rip6_dest,
311355163Sshin			buf, sizeof(buf));
311455163Sshin		fprintf(dump, "    %s/%d if(%d:%s) gw(%s) [%d] age(%ld)",
311555163Sshin			buf, rrt->rrt_info.rip6_plen, rrt->rrt_index,
311655163Sshin			index2ifc[rrt->rrt_index]->ifc_name,
311755163Sshin			inet6_n2p(&rrt->rrt_gw),
311855163Sshin			rrt->rrt_info.rip6_metric, (long)age);
311955163Sshin		if (rrt->rrt_info.rip6_tag) {
312055163Sshin			fprintf(dump, " tag(0x%04x)",
312155163Sshin				ntohs(rrt->rrt_info.rip6_tag) & 0xffff);
312255163Sshin		}
312362607Sitojun		if (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)
312455163Sshin			fprintf(dump, " NOT-LL");
312562607Sitojun		if (rrt->rrt_rflags & RRTF_NOADVERTISE)
312655163Sshin			fprintf(dump, " NO-ADV");
312755163Sshin		fprintf(dump, "\n");
312855163Sshin	}
312955163Sshin	fprintf(dump, "\n");
313055163Sshin	if (dump != stderr)
313155163Sshin		fclose(dump);
313255163Sshin}
313355163Sshin
313455163Sshin/*
313555163Sshin * Parse the -A (and -O) options and put corresponding filter object to the
313678064Sume * specified interface structures.  Each of the -A/O option has the following
313755163Sshin * syntax:	-A 5f09:c400::/32,ef0,ef1  (aggregate)
313855163Sshin * 		-O 5f09:c400::/32,ef0,ef1  (only when match)
313955163Sshin */
314055163Sshinvoid
3141243232Shrsfilterconfig(void)
314255163Sshin{
314355163Sshin	int i;
3144119083Sume	char *p, *ap, *iflp, *ifname, *ep;
3145243232Shrs	struct iff iff, *iffp;
314678064Sume	struct ifc *ifcp;
314778064Sume	struct riprt *rrt;
314864631Sitojun#if 0
314978064Sume	struct in6_addr gw;
315064631Sitojun#endif
3151119083Sume	u_long plen;
315255163Sshin
315355163Sshin	for (i = 0; i < nfilter; i++) {
315455163Sshin		ap = filter[i];
315555163Sshin		iflp = NULL;
3156243232Shrs		iffp = &iff;
3157243232Shrs		memset(iffp, 0, sizeof(*iffp));
315855163Sshin		if (filtertype[i] == 'N' || filtertype[i] == 'T') {
315955163Sshin			iflp = ap;
316055163Sshin			goto ifonly;
316155163Sshin		}
3162119038Sume		if ((p = strchr(ap, ',')) != NULL) {
316355163Sshin			*p++ = '\0';
316455163Sshin			iflp = p;
316555163Sshin		}
3166119038Sume		if ((p = strchr(ap, '/')) == NULL) {
316755163Sshin			fatal("no prefixlen specified for '%s'", ap);
316878064Sume			/*NOTREACHED*/
316978064Sume		}
317055163Sshin		*p++ = '\0';
3171243232Shrs		if (inet_pton(AF_INET6, ap, &iffp->iff_addr) != 1) {
317255163Sshin			fatal("invalid prefix specified for '%s'", ap);
317378064Sume			/*NOTREACHED*/
317478064Sume		}
3175119083Sume		errno = 0;
3176119083Sume		ep = NULL;
3177119083Sume		plen = strtoul(p, &ep, 10);
3178243232Shrs		if (errno || !*p || *ep || plen > sizeof(iffp->iff_addr) * 8) {
3179119083Sume			fatal("invalid prefix length specified for '%s'", ap);
3180119083Sume			/*NOTREACHED*/
3181119083Sume		}
3182243232Shrs		iffp->iff_plen = plen;
3183243232Shrs		applyplen(&iffp->iff_addr, iffp->iff_plen);
318455163Sshinifonly:
3185243232Shrs		iffp->iff_type = filtertype[i];
318678064Sume		if (iflp == NULL || *iflp == '\0') {
318755163Sshin			fatal("no interface specified for '%s'", ap);
318878064Sume			/*NOTREACHED*/
318978064Sume		}
319055163Sshin		/* parse the interface listing portion */
319155163Sshin		while (iflp) {
319255163Sshin			ifname = iflp;
3193119038Sume			if ((iflp = strchr(iflp, ',')) != NULL)
319455163Sshin				*iflp++ = '\0';
3195243232Shrs
3196243232Shrs			TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3197243233Shrs				if (fnmatch(ifname, ifcp->ifc_name, 0) != 0)
3198243232Shrs					continue;
3199243232Shrs
3200243232Shrs				iffp = malloc(sizeof(*iffp));
3201243232Shrs				if (iffp == NULL) {
3202243232Shrs					fatal("malloc of iff");
3203243232Shrs					/*NOTREACHED*/
3204243232Shrs				}
3205243232Shrs				memcpy(iffp, &iff, sizeof(*iffp));
3206243233Shrs#if 0
3207243233Shrs				syslog(LOG_INFO, "Add filter: type %d, ifname %s.", iffp->iff_type, ifname);
3208243233Shrs#endif
3209243232Shrs				TAILQ_INSERT_HEAD(&ifcp->ifc_iff_head, iffp, iff_next);
321078064Sume			}
321155163Sshin		}
321278064Sume
321378064Sume		/*
321478064Sume		 * -A: aggregate configuration.
321578064Sume		 */
3216243232Shrs		if (filtertype[i] != IFIL_TYPE_A)
321755163Sshin			continue;
321855163Sshin		/* put the aggregate to the kernel routing table */
321955163Sshin		rrt = (struct riprt *)malloc(sizeof(struct riprt));
322078064Sume		if (rrt == NULL) {
322155163Sshin			fatal("malloc: rrt");
322278064Sume			/*NOTREACHED*/
322378064Sume		}
322455163Sshin		memset(rrt, 0, sizeof(struct riprt));
3225243232Shrs		rrt->rrt_info.rip6_dest = iff.iff_addr;
3226243232Shrs		rrt->rrt_info.rip6_plen = iff.iff_plen;
322755163Sshin		rrt->rrt_info.rip6_metric = 1;
322855163Sshin		rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
322955163Sshin		rrt->rrt_gw = in6addr_loopback;
323062607Sitojun		rrt->rrt_flags = RTF_UP | RTF_REJECT;
323162607Sitojun		rrt->rrt_rflags = RRTF_AGGREGATE;
323255163Sshin		rrt->rrt_t = 0;
3233119039Sume		rrt->rrt_index = loopifcp->ifc_index;
323464631Sitojun#if 0
323564631Sitojun		if (getroute(&rrt->rrt_info, &gw)) {
323664631Sitojun#if 0
323764631Sitojun			/*
323864631Sitojun			 * When the address has already been registered in the
3239312022Sngie			 * kernel routing table, it should be removed
324064631Sitojun			 */
324164631Sitojun			delroute(&rrt->rrt_info, &gw);
324264631Sitojun#else
324378064Sume			/* it is safer behavior */
324464631Sitojun			errno = EINVAL;
324564631Sitojun			fatal("%s/%u already in routing table, "
324664631Sitojun			    "cannot aggregate",
324764631Sitojun			    inet6_n2p(&rrt->rrt_info.rip6_dest),
324864631Sitojun			    rrt->rrt_info.rip6_plen);
324978064Sume			/*NOTREACHED*/
325064631Sitojun#endif
325164631Sitojun		}
325264631Sitojun#endif
325355163Sshin		/* Put the route to the list */
3254243232Shrs		TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
325555163Sshin		trace(1, "Aggregate: %s/%d for %s\n",
3256243232Shrs			inet6_n2p(&iff.iff_addr), iff.iff_plen,
3257243232Shrs			loopifcp->ifc_name);
325855163Sshin		/* Add this route to the kernel */
325955163Sshin		if (nflag) 	/* do not modify kernel routing table */
326055163Sshin			continue;
326155163Sshin		addroute(rrt, &in6addr_loopback, loopifcp);
326255163Sshin	}
326355163Sshin}
326455163Sshin
326555163Sshin/***************** utility functions *****************/
326655163Sshin
326755163Sshin/*
326855163Sshin * Returns a pointer to ifac whose address and prefix length matches
326955163Sshin * with the address and prefix length specified in the arguments.
327055163Sshin */
327155163Sshinstruct ifac *
3272243232Shrsifa_match(const struct ifc *ifcp,
3273243232Shrs	const struct in6_addr *ia,
3274243232Shrs	int plen)
327555163Sshin{
3276243232Shrs	struct ifac *ifac;
327755163Sshin
3278243232Shrs	TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
3279243232Shrs		if (IN6_ARE_ADDR_EQUAL(&ifac->ifac_addr, ia) &&
3280243232Shrs		    ifac->ifac_plen == plen)
328155163Sshin			break;
328255163Sshin	}
3283243232Shrs
3284243232Shrs	return (ifac);
328555163Sshin}
328655163Sshin
328755163Sshin/*
328855163Sshin * Return a pointer to riprt structure whose address and prefix length
328955163Sshin * matches with the address and prefix length found in the argument.
329078064Sume * Note: This is not a rtalloc().  Therefore exact match is necessary.
329155163Sshin */
329255163Sshinstruct riprt *
3293243232Shrsrtsearch(struct netinfo6 *np)
329455163Sshin{
329555163Sshin	struct	riprt	*rrt;
329655163Sshin
3297243232Shrs	TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
329855163Sshin		if (rrt->rrt_info.rip6_plen == np->rip6_plen &&
329955163Sshin		    IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
330055163Sshin				       &np->rip6_dest))
3301243232Shrs			break;
330255163Sshin	}
3303243232Shrs
3304243232Shrs	return (rrt);
330555163Sshin}
330655163Sshin
330755163Sshinint
3308243232Shrssin6mask2len(const struct sockaddr_in6 *sin6)
330978064Sume{
331078064Sume
331178064Sume	return mask2len(&sin6->sin6_addr,
331278064Sume	    sin6->sin6_len - offsetof(struct sockaddr_in6, sin6_addr));
331378064Sume}
331478064Sume
331578064Sumeint
3316243232Shrsmask2len(const struct in6_addr *addr, int lenlim)
331755163Sshin{
331855163Sshin	int i = 0, j;
331978064Sume	const u_char *p = (const u_char *)addr;
3320312022Sngie
332155163Sshin	for (j = 0; j < lenlim; j++, p++) {
332255163Sshin		if (*p != 0xff)
332355163Sshin			break;
332455163Sshin		i += 8;
332555163Sshin	}
332655163Sshin	if (j < lenlim) {
332755163Sshin		switch (*p) {
332862607Sitojun#define	MASKLEN(m, l)	case m: do { i += l; break; } while (0)
332962607Sitojun		MASKLEN(0xfe, 7); break;
333062607Sitojun		MASKLEN(0xfc, 6); break;
333162607Sitojun		MASKLEN(0xf8, 5); break;
333262607Sitojun		MASKLEN(0xf0, 4); break;
333362607Sitojun		MASKLEN(0xe0, 3); break;
333462607Sitojun		MASKLEN(0xc0, 2); break;
333562607Sitojun		MASKLEN(0x80, 1); break;
333655163Sshin#undef	MASKLEN
333755163Sshin		}
333855163Sshin	}
333955163Sshin	return i;
334055163Sshin}
334155163Sshin
334255163Sshinvoid
3343243232Shrsapplymask(struct in6_addr *addr, struct in6_addr *mask)
334455163Sshin{
334555163Sshin	int	i;
334655163Sshin	u_long	*p, *q;
334755163Sshin
334855163Sshin	p = (u_long *)addr; q = (u_long *)mask;
334955163Sshin	for (i = 0; i < 4; i++)
335055163Sshin		*p++ &= *q++;
335155163Sshin}
335255163Sshin
335355163Sshinstatic const u_char plent[8] = {
335455163Sshin	0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
335555163Sshin};
335655163Sshin
335755163Sshinvoid
3358243232Shrsapplyplen(struct in6_addr *ia, int plen)
335955163Sshin{
336055163Sshin	u_char	*p;
336155163Sshin	int	i;
336255163Sshin
336355163Sshin	p = ia->s6_addr;
336455163Sshin	for (i = 0; i < 16; i++) {
336555163Sshin		if (plen <= 0)
336655163Sshin			*p = 0;
336755163Sshin		else if (plen < 8)
336855163Sshin			*p &= plent[plen];
336955163Sshin		p++, plen -= 8;
337055163Sshin	}
337155163Sshin}
337255163Sshin
337355163Sshinstatic const int pl2m[9] = {
337455163Sshin	0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
337555163Sshin};
337655163Sshin
337755163Sshinstruct in6_addr *
3378243232Shrsplen2mask(int n)
337955163Sshin{
338055163Sshin	static struct in6_addr ia;
338155163Sshin	u_char	*p;
338255163Sshin	int	i;
338355163Sshin
338455163Sshin	memset(&ia, 0, sizeof(struct in6_addr));
338555163Sshin	p = (u_char *)&ia;
338655163Sshin	for (i = 0; i < 16; i++, p++, n -= 8) {
338755163Sshin		if (n >= 8) {
338855163Sshin			*p = 0xff;
338955163Sshin			continue;
339055163Sshin		}
339155163Sshin		*p = pl2m[n];
339255163Sshin		break;
339355163Sshin	}
339455163Sshin	return &ia;
339555163Sshin}
339655163Sshin
339755163Sshinchar *
3398243232Shrsallocopy(char *p)
339955163Sshin{
3400119033Sume	int len = strlen(p) + 1;
3401119033Sume	char *q = (char *)malloc(len);
340255163Sshin
3403119033Sume	if (!q) {
3404119033Sume		fatal("malloc");
3405119033Sume		/*NOTREACHED*/
3406119033Sume	}
3407119033Sume
3408119033Sume	strlcpy(q, p, len);
340955163Sshin	return q;
341055163Sshin}
341155163Sshin
341255163Sshinchar *
3413243232Shrshms(void)
341455163Sshin{
341555163Sshin	static char buf[BUFSIZ];
341655163Sshin	time_t t;
341755163Sshin	struct	tm *tm;
341855163Sshin
341955163Sshin	t = time(NULL);
342078064Sume	if ((tm = localtime(&t)) == 0) {
342155163Sshin		fatal("localtime");
342278064Sume		/*NOTREACHED*/
342378064Sume	}
342478064Sume	snprintf(buf, sizeof(buf), "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
342578064Sume	    tm->tm_sec);
342655163Sshin	return buf;
342755163Sshin}
342855163Sshin
342955163Sshin#define	RIPRANDDEV	1.0	/* 30 +- 15, max - min = 30 */
343055163Sshin
343155163Sshinint
3432243232Shrsripinterval(int timer)
343355163Sshin{
343455163Sshin	double r = rand();
343555163Sshin
343655163Sshin	interval = (int)(timer + timer * RIPRANDDEV * (r / RAND_MAX - 0.5));
343755163Sshin	nextalarm = time(NULL) + interval;
343855163Sshin	return interval;
343955163Sshin}
344055163Sshin
344155163Sshintime_t
3442243232Shrsripsuptrig(void)
344355163Sshin{
344455163Sshin	time_t t;
344555163Sshin
344655163Sshin	double r = rand();
3447312022Sngie	t  = (int)(RIP_TRIG_INT6_MIN +
344878064Sume		(RIP_TRIG_INT6_MAX - RIP_TRIG_INT6_MIN) * (r / RAND_MAX));
344955163Sshin	sup_trig_update = time(NULL) + t;
345055163Sshin	return t;
345155163Sshin}
345255163Sshin
345355163Sshinvoid
345455163Sshin#ifdef __STDC__
345555163Sshinfatal(const char *fmt, ...)
345655163Sshin#else
345755163Sshinfatal(fmt, va_alist)
345855163Sshin	char	*fmt;
345955163Sshin	va_dcl
346055163Sshin#endif
346155163Sshin{
346255163Sshin	va_list ap;
346355163Sshin	char buf[1024];
346455163Sshin
346555163Sshin#ifdef __STDC__
346655163Sshin	va_start(ap, fmt);
346755163Sshin#else
346855163Sshin	va_start(ap);
346955163Sshin#endif
347055163Sshin	vsnprintf(buf, sizeof(buf), fmt, ap);
3471119043Sume	va_end(ap);
347255163Sshin	perror(buf);
3473119043Sume	if (errno)
3474119043Sume		syslog(LOG_ERR, "%s: %s", buf, strerror(errno));
3475119043Sume	else
3476119043Sume		syslog(LOG_ERR, "%s", buf);
347778064Sume	rtdexit();
347855163Sshin}
347955163Sshin
348055163Sshinvoid
348155163Sshin#ifdef __STDC__
348255163Sshintracet(int level, const char *fmt, ...)
348355163Sshin#else
348455163Sshintracet(level, fmt, va_alist)
348555163Sshin	int level;
348655163Sshin	char *fmt;
348755163Sshin	va_dcl
348855163Sshin#endif
348955163Sshin{
349055163Sshin	va_list ap;
349155163Sshin
3492119043Sume	if (level <= dflag) {
349355163Sshin#ifdef __STDC__
3494119043Sume		va_start(ap, fmt);
349555163Sshin#else
3496119043Sume		va_start(ap);
349755163Sshin#endif
349855163Sshin		fprintf(stderr, "%s: ", hms());
349955163Sshin		vfprintf(stderr, fmt, ap);
3500119043Sume		va_end(ap);
350155163Sshin	}
350255163Sshin	if (dflag) {
3503119043Sume#ifdef __STDC__
3504119043Sume		va_start(ap, fmt);
3505119043Sume#else
3506119043Sume		va_start(ap);
3507119043Sume#endif
350855163Sshin		if (level > 0)
350955163Sshin			vsyslog(LOG_DEBUG, fmt, ap);
351055163Sshin		else
351155163Sshin			vsyslog(LOG_WARNING, fmt, ap);
3512119043Sume		va_end(ap);
351355163Sshin	}
351455163Sshin}
351555163Sshin
351655163Sshinvoid
351755163Sshin#ifdef __STDC__
351855163Sshintrace(int level, const char *fmt, ...)
351955163Sshin#else
352055163Sshintrace(level, fmt, va_alist)
352155163Sshin	int level;
352255163Sshin	char *fmt;
352355163Sshin	va_dcl
352455163Sshin#endif
352555163Sshin{
352655163Sshin	va_list ap;
352755163Sshin
3528119043Sume	if (level <= dflag) {
352955163Sshin#ifdef __STDC__
3530119043Sume		va_start(ap, fmt);
353155163Sshin#else
3532119043Sume		va_start(ap);
353355163Sshin#endif
353455163Sshin		vfprintf(stderr, fmt, ap);
3535119043Sume		va_end(ap);
3536119043Sume	}
353755163Sshin	if (dflag) {
3538119043Sume#ifdef __STDC__
3539119043Sume		va_start(ap, fmt);
3540119043Sume#else
3541119043Sume		va_start(ap);
3542119043Sume#endif
354355163Sshin		if (level > 0)
354455163Sshin			vsyslog(LOG_DEBUG, fmt, ap);
354555163Sshin		else
354655163Sshin			vsyslog(LOG_WARNING, fmt, ap);
3547119043Sume		va_end(ap);
354855163Sshin	}
354955163Sshin}
355055163Sshin
355155163Sshinunsigned int
3552243232Shrsif_maxindex(void)
355355163Sshin{
355455163Sshin	struct if_nameindex *p, *p0;
355555163Sshin	unsigned int max = 0;
355655163Sshin
355755163Sshin	p0 = if_nameindex();
355855163Sshin	for (p = p0; p && p->if_index && p->if_name; p++) {
355955163Sshin		if (max < p->if_index)
356055163Sshin			max = p->if_index;
356155163Sshin	}
356255163Sshin	if_freenameindex(p0);
356355163Sshin	return max;
356455163Sshin}
356555163Sshin
356655163Sshinstruct ifc *
3567243232Shrsifc_find(char *name)
356855163Sshin{
356955163Sshin	struct ifc *ifcp;
357055163Sshin
3571243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
357255163Sshin		if (strcmp(name, ifcp->ifc_name) == 0)
3573243232Shrs			break;
357455163Sshin	}
3575243232Shrs	return (ifcp);
357655163Sshin}
357755163Sshin
357855163Sshinstruct iff *
3579243232Shrsiff_find(struct ifc *ifcp, int type)
358055163Sshin{
358155163Sshin	struct iff *iffp;
358255163Sshin
3583243232Shrs	TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
3584243232Shrs		if (type == IFIL_TYPE_ANY ||
3585243232Shrs		    type == iffp->iff_type)
3586243232Shrs			break;
358755163Sshin	}
3588243232Shrs
3589243232Shrs	return (iffp);
359055163Sshin}
359155163Sshin
359255163Sshinvoid
3593243232Shrssetindex2ifc(int idx, struct ifc *ifcp)
359455163Sshin{
3595122677Sume	int n, nsize;
359662607Sitojun	struct ifc **p;
359755163Sshin
359855163Sshin	if (!index2ifc) {
359955163Sshin		nindex2ifc = 5;	/*initial guess*/
360055163Sshin		index2ifc = (struct ifc **)
360155163Sshin			malloc(sizeof(*index2ifc) * nindex2ifc);
360278064Sume		if (index2ifc == NULL) {
360355163Sshin			fatal("malloc");
360478064Sume			/*NOTREACHED*/
360578064Sume		}
360655163Sshin		memset(index2ifc, 0, sizeof(*index2ifc) * nindex2ifc);
360755163Sshin	}
360855163Sshin	n = nindex2ifc;
3609122677Sume	for (nsize = nindex2ifc; nsize <= idx; nsize *= 2)
3610122677Sume		;
3611122677Sume	if (n != nsize) {
361262607Sitojun		p = (struct ifc **)realloc(index2ifc,
3613122677Sume		    sizeof(*index2ifc) * nsize);
361478064Sume		if (p == NULL) {
361555163Sshin			fatal("realloc");
361678064Sume			/*NOTREACHED*/
361778064Sume		}
361878064Sume		memset(p + n, 0, sizeof(*index2ifc) * (nindex2ifc - n));
361962607Sitojun		index2ifc = p;
3620122677Sume		nindex2ifc = nsize;
362155163Sshin	}
362278064Sume	index2ifc[idx] = ifcp;
362355163Sshin}
3624