162607Sitojun/*	$FreeBSD$	*/
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.
762607Sitojun *
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.
1962607Sitojun *
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 <stdio.h>
3855163Sshin
3955163Sshin#include <time.h>
4055163Sshin#include <unistd.h>
41243233Shrs#include <fnmatch.h>
4255163Sshin#include <stdlib.h>
4355163Sshin#include <string.h>
4455163Sshin#include <signal.h>
4555163Sshin#ifdef __STDC__
4655163Sshin#include <stdarg.h>
4755163Sshin#else
4855163Sshin#include <varargs.h>
4955163Sshin#endif
5055163Sshin#include <syslog.h>
5155163Sshin#include <stddef.h>
5262607Sitojun#include <errno.h>
5355163Sshin#include <err.h>
54119076Sume#ifdef HAVE_POLL_H
55119076Sume#include <poll.h>
56119076Sume#endif
5755163Sshin
5855163Sshin#include <sys/types.h>
5955163Sshin#include <sys/param.h>
6055163Sshin#include <sys/file.h>
6155163Sshin#include <sys/socket.h>
6255163Sshin#include <sys/ioctl.h>
6355163Sshin#include <sys/sysctl.h>
6455163Sshin#include <sys/uio.h>
6555163Sshin#include <net/if.h>
6655163Sshin#include <net/if_var.h>
6755163Sshin#include <net/route.h>
6855163Sshin#include <netinet/in.h>
6955163Sshin#include <netinet/in_var.h>
7055163Sshin#include <netinet/ip6.h>
7155163Sshin#include <netinet/udp.h>
7255163Sshin#include <netdb.h>
7362607Sitojun#include <ifaddrs.h>
7455163Sshin
7555163Sshin#include <arpa/inet.h>
7655163Sshin
7755163Sshin#include "route6d.h"
7855163Sshin
7955163Sshin#define	MAXFILTER	40
80243233Shrs#define RT_DUMP_MAXRETRY	15
8155163Sshin
8255163Sshin#ifdef	DEBUG
8355163Sshin#define	INIT_INTERVAL6	6
8455163Sshin#else
85119039Sume#define	INIT_INTERVAL6	10	/* Wait to submit an initial riprequest */
8655163Sshin#endif
8755163Sshin
8855163Sshin/* alignment constraint for routing socket */
8962607Sitojun#define ROUNDUP(a) \
9055163Sshin	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
9162607Sitojun#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
9255163Sshin
93243232Shrsstruct ifc {			/* Configuration of an interface */
94243232Shrs	TAILQ_ENTRY(ifc) ifc_next;
95243232Shrs
96243232Shrs	char	ifc_name[IFNAMSIZ];		/* if name */
9755163Sshin	int	ifc_index;			/* if index */
9855163Sshin	int	ifc_mtu;			/* if mtu */
9955163Sshin	int	ifc_metric;			/* if metric */
10078064Sume	u_int	ifc_flags;			/* flags */
10178064Sume	short	ifc_cflags;			/* IFC_XXX */
10255163Sshin	struct	in6_addr ifc_mylladdr;		/* my link-local address */
10355163Sshin	struct	sockaddr_in6 ifc_ripsin;	/* rip multicast address */
104243232Shrs	TAILQ_HEAD(, ifac) ifc_ifac_head;	/* list of AF_INET6 addrs */
105243232Shrs	TAILQ_HEAD(, iff) ifc_iff_head;		/* list of filters */
10655163Sshin	int	ifc_joined;			/* joined to ff02::9 */
10755163Sshin};
108243232ShrsTAILQ_HEAD(, ifc) ifc_head = TAILQ_HEAD_INITIALIZER(ifc_head);
10955163Sshin
110243232Shrsstruct ifac {			/* Adddress associated to an interface */
111243232Shrs	TAILQ_ENTRY(ifac) ifac_next;
112243232Shrs
113243232Shrs	struct	ifc *ifac_ifc;		/* back pointer */
114243232Shrs	struct	in6_addr ifac_addr;	/* address */
115243232Shrs	struct	in6_addr ifac_raddr;	/* remote address, valid in p2p */
116243232Shrs	int	ifac_scope_id;		/* scope id */
117243232Shrs	int	ifac_plen;		/* prefix length */
11855163Sshin};
11955163Sshin
120243232Shrsstruct iff {			/* Filters for an interface */
121243232Shrs	TAILQ_ENTRY(iff) iff_next;
122243232Shrs
12355163Sshin	int	iff_type;
12455163Sshin	struct	in6_addr iff_addr;
12555163Sshin	int	iff_plen;
12655163Sshin};
12755163Sshin
12855163Sshinstruct	ifc **index2ifc;
129243232Shrsunsigned int	nindex2ifc;
13055163Sshinstruct	ifc *loopifcp = NULL;	/* pointing to loopback */
131119076Sume#ifdef HAVE_POLL_H
132119076Sumestruct	pollfd set[2];
133119076Sume#else
134119070Sumefd_set	*sockvecp;	/* vector to select() for receiving */
135119070Sumefd_set	*recvecp;
136119070Sumeint	fdmasks;
137119070Sumeint	maxfd;		/* maximum fd for select() */
138119076Sume#endif
13955163Sshinint	rtsock;		/* the routing socket */
14055163Sshinint	ripsock;	/* socket to send/receive RIP datagram */
14155163Sshin
14255163Sshinstruct	rip6 *ripbuf;	/* packet buffer for sending */
14355163Sshin
14455163Sshin/*
14578064Sume * Maintain the routes in a linked list.  When the number of the routes
14655163Sshin * grows, somebody would like to introduce a hash based or a radix tree
14778064Sume * based structure.  I believe the number of routes handled by RIP is
14855163Sshin * limited and I don't have to manage a complex data structure, however.
14955163Sshin *
15055163Sshin * One of the major drawbacks of the linear linked list is the difficulty
15178064Sume * of representing the relationship between a couple of routes.  This may
15255163Sshin * be a significant problem when we have to support route aggregation with
153228990Suqs * suppressing the specifics covered by the aggregate.
15455163Sshin */
15555163Sshin
156243232Shrsstruct riprt {
157243232Shrs	TAILQ_ENTRY(riprt) rrt_next;	/* next destination */
158243232Shrs
15955163Sshin	struct	riprt *rrt_same;	/* same destination - future use */
16055163Sshin	struct	netinfo6 rrt_info;	/* network info */
16155163Sshin	struct	in6_addr rrt_gw;	/* gateway */
16262607Sitojun	u_long	rrt_flags;		/* kernel routing table flags */
16362607Sitojun	u_long	rrt_rflags;		/* route6d routing table flags */
16455163Sshin	time_t	rrt_t;			/* when the route validated */
16555163Sshin	int	rrt_index;		/* ifindex from which this route got */
16655163Sshin};
167243232ShrsTAILQ_HEAD(, riprt) riprt_head = TAILQ_HEAD_INITIALIZER(riprt_head);
16855163Sshin
16955163Sshinint	dflag = 0;	/* debug flag */
17055163Sshinint	qflag = 0;	/* quiet flag */
17155163Sshinint	nflag = 0;	/* don't update kernel routing table */
17255163Sshinint	aflag = 0;	/* age out even the statically defined routes */
17355163Sshinint	hflag = 0;	/* don't split horizon */
17455163Sshinint	lflag = 0;	/* exchange site local routes */
175243233Shrsint	Pflag = 0;	/* don't age out routes with RTF_PROTO[123] */
176243233Shrsint	Qflag = RTF_PROTO2;	/* set RTF_PROTO[123] flag to routes by RIPng */
17755163Sshinint	sflag = 0;	/* announce static routes w/ split horizon */
17855163Sshinint	Sflag = 0;	/* announce static routes to every interface */
17962607Sitojununsigned long routetag = 0;	/* route tag attached on originating case */
18055163Sshin
18155163Sshinchar	*filter[MAXFILTER];
18255163Sshinint	filtertype[MAXFILTER];
18355163Sshinint	nfilter = 0;
18455163Sshin
18555163Sshinpid_t	pid;
18655163Sshin
18755163Sshinstruct	sockaddr_storage ripsin;
18855163Sshin
18955163Sshinint	interval = 1;
19055163Sshintime_t	nextalarm = 0;
19155163Sshintime_t	sup_trig_update = 0;
19255163Sshin
19355163SshinFILE	*rtlog = NULL;
19455163Sshin
19555163Sshinint logopened = 0;
19655163Sshin
197119085Sumestatic	int	seq = 0;
19855163Sshin
19978064Sumevolatile sig_atomic_t seenalrm;
20078064Sumevolatile sig_atomic_t seenquit;
20178064Sumevolatile sig_atomic_t seenusr1;
20278064Sume
20362607Sitojun#define	RRTF_AGGREGATE		0x08000000
20462607Sitojun#define	RRTF_NOADVERTISE	0x10000000
20562607Sitojun#define	RRTF_NH_NOT_LLADDR	0x20000000
20662607Sitojun#define RRTF_SENDANYWAY		0x40000000
20762607Sitojun#define	RRTF_CHANGED		0x80000000
20855163Sshin
209173412Skevloint main(int, char **);
210173412Skevlovoid sighandler(int);
211173412Skevlovoid ripalarm(void);
212173412Skevlovoid riprecv(void);
213173412Skevlovoid ripsend(struct ifc *, struct sockaddr_in6 *, int);
214173412Skevloint out_filter(struct riprt *, struct ifc *);
215173412Skevlovoid init(void);
216173412Skevlovoid sockopt(struct ifc *);
217173412Skevlovoid ifconfig(void);
218243232Shrsint ifconfig1(const char *, const struct sockaddr *, struct ifc *, int);
219173412Skevlovoid rtrecv(void);
220173412Skevloint rt_del(const struct sockaddr_in6 *, const struct sockaddr_in6 *,
221173412Skevlo	const struct sockaddr_in6 *);
222173412Skevloint rt_deladdr(struct ifc *, const struct sockaddr_in6 *,
223173412Skevlo	const struct sockaddr_in6 *);
224173412Skevlovoid filterconfig(void);
225173412Skevloint getifmtu(int);
226173412Skevloconst char *rttypes(struct rt_msghdr *);
227173412Skevloconst char *rtflags(struct rt_msghdr *);
228173412Skevloconst char *ifflags(int);
229173412Skevloint ifrt(struct ifc *, int);
230173412Skevlovoid ifrt_p2p(struct ifc *, int);
231173412Skevlovoid applymask(struct in6_addr *, struct in6_addr *);
232173412Skevlovoid applyplen(struct in6_addr *, int);
233173412Skevlovoid ifrtdump(int);
234173412Skevlovoid ifdump(int);
235173412Skevlovoid ifdump0(FILE *, const struct ifc *);
236243233Shrsvoid ifremove(int);
237173412Skevlovoid rtdump(int);
238173412Skevlovoid rt_entry(struct rt_msghdr *, int);
239173412Skevlovoid rtdexit(void);
240173412Skevlovoid riprequest(struct ifc *, struct netinfo6 *, int,
241173412Skevlo	struct sockaddr_in6 *);
242243232Shrsvoid ripflush(struct ifc *, struct sockaddr_in6 *, int, struct netinfo6 *np);
243173412Skevlovoid sendrequest(struct ifc *);
244173412Skevloint sin6mask2len(const struct sockaddr_in6 *);
245173412Skevloint mask2len(const struct in6_addr *, int);
246173412Skevloint sendpacket(struct sockaddr_in6 *, int);
247173412Skevloint addroute(struct riprt *, const struct in6_addr *, struct ifc *);
248173412Skevloint delroute(struct netinfo6 *, struct in6_addr *);
249173412Skevlostruct in6_addr *getroute(struct netinfo6 *, struct in6_addr *);
250173412Skevlovoid krtread(int);
251173412Skevloint tobeadv(struct riprt *, struct ifc *);
252173412Skevlochar *allocopy(char *);
253173412Skevlochar *hms(void);
254173412Skevloconst char *inet6_n2p(const struct in6_addr *);
255173412Skevlostruct ifac *ifa_match(const struct ifc *, const struct in6_addr *, int);
256173412Skevlostruct in6_addr *plen2mask(int);
257243232Shrsstruct riprt *rtsearch(struct netinfo6 *);
258173412Skevloint ripinterval(int);
259173412Skevlotime_t ripsuptrig(void);
260173412Skevlovoid fatal(const char *, ...)
26166807Skris	__attribute__((__format__(__printf__, 1, 2)));
262173412Skevlovoid trace(int, const char *, ...)
26366807Skris	__attribute__((__format__(__printf__, 2, 3)));
264173412Skevlovoid tracet(int, const char *, ...)
26566807Skris	__attribute__((__format__(__printf__, 2, 3)));
266173412Skevlounsigned int if_maxindex(void);
267173412Skevlostruct ifc *ifc_find(char *);
268173412Skevlostruct iff *iff_find(struct ifc *, int);
269173412Skevlovoid setindex2ifc(int, struct ifc *);
27055163Sshin
27155163Sshin#define	MALLOC(type)	((type *)malloc(sizeof(type)))
27255163Sshin
273243232Shrs#define IFIL_TYPE_ANY	0x0
274243232Shrs#define IFIL_TYPE_A	'A'
275243232Shrs#define IFIL_TYPE_N	'N'
276243232Shrs#define IFIL_TYPE_T	'T'
277243232Shrs#define IFIL_TYPE_O	'O'
278243232Shrs#define IFIL_TYPE_L	'L'
279243232Shrs
28055163Sshinint
281243232Shrsmain(int argc, char *argv[])
28255163Sshin{
28355163Sshin	int	ch;
28455163Sshin	int	error = 0;
285243233Shrs	unsigned long proto;
28655163Sshin	struct	ifc *ifcp;
28755163Sshin	sigset_t mask, omask;
288243233Shrs	const char *pidfile = ROUTE6D_PID;
289243233Shrs	FILE *pidfh;
29055163Sshin	char *progname;
29162607Sitojun	char *ep;
29255163Sshin
29355163Sshin	progname = strrchr(*argv, '/');
29455163Sshin	if (progname)
29555163Sshin		progname++;
29655163Sshin	else
29755163Sshin		progname = *argv;
29855163Sshin
29955163Sshin	pid = getpid();
300243233Shrs	while ((ch = getopt(argc, argv, "A:N:O:R:T:L:t:adDhlnp:P:Q:qsS")) != -1) {
30155163Sshin		switch (ch) {
30255163Sshin		case 'A':
30355163Sshin		case 'N':
30455163Sshin		case 'O':
30555163Sshin		case 'T':
30655163Sshin		case 'L':
30762607Sitojun			if (nfilter >= MAXFILTER) {
30855163Sshin				fatal("Exceeds MAXFILTER");
30962607Sitojun				/*NOTREACHED*/
31062607Sitojun			}
31155163Sshin			filtertype[nfilter] = ch;
31255163Sshin			filter[nfilter++] = allocopy(optarg);
31355163Sshin			break;
31455163Sshin		case 't':
31562607Sitojun			ep = NULL;
31662607Sitojun			routetag = strtoul(optarg, &ep, 0);
31762607Sitojun			if (!ep || *ep != '\0' || (routetag & ~0xffff) != 0) {
31855163Sshin				fatal("invalid route tag");
31955163Sshin				/*NOTREACHED*/
32055163Sshin			}
32155163Sshin			break;
322243233Shrs		case 'p':
323243233Shrs			pidfile = optarg;
324243233Shrs			break;
325243233Shrs		case 'P':
326243233Shrs			ep = NULL;
327243233Shrs			proto = strtoul(optarg, &ep, 0);
328243233Shrs			if (!ep || *ep != '\0' || 3 < proto) {
329243233Shrs				fatal("invalid P flag");
330243233Shrs				/*NOTREACHED*/
331243233Shrs			}
332243233Shrs			if (proto == 0)
333243233Shrs				Pflag = 0;
334243233Shrs			if (proto == 1)
335243233Shrs				Pflag |= RTF_PROTO1;
336243233Shrs			if (proto == 2)
337243233Shrs				Pflag |= RTF_PROTO2;
338243233Shrs			if (proto == 3)
339243233Shrs				Pflag |= RTF_PROTO3;
340243233Shrs			break;
341243233Shrs		case 'Q':
342243233Shrs			ep = NULL;
343243233Shrs			proto = strtoul(optarg, &ep, 0);
344243233Shrs			if (!ep || *ep != '\0' || 3 < proto) {
345243233Shrs				fatal("invalid Q flag");
346243233Shrs				/*NOTREACHED*/
347243233Shrs			}
348243233Shrs			if (proto == 0)
349243233Shrs				Qflag = 0;
350243233Shrs			if (proto == 1)
351243233Shrs				Qflag |= RTF_PROTO1;
352243233Shrs			if (proto == 2)
353243233Shrs				Qflag |= RTF_PROTO2;
354243233Shrs			if (proto == 3)
355243233Shrs				Qflag |= RTF_PROTO3;
356243233Shrs			break;
35755163Sshin		case 'R':
35862607Sitojun			if ((rtlog = fopen(optarg, "w")) == NULL) {
35955163Sshin				fatal("Can not write to routelog");
36062607Sitojun				/*NOTREACHED*/
36162607Sitojun			}
36255163Sshin			break;
36362607Sitojun#define	FLAG(c, flag, n)	case c: do { flag = n; break; } while(0)
36462607Sitojun		FLAG('a', aflag, 1); break;
36562607Sitojun		FLAG('d', dflag, 1); break;
36662607Sitojun		FLAG('D', dflag, 2); break;
36762607Sitojun		FLAG('h', hflag, 1); break;
36862607Sitojun		FLAG('l', lflag, 1); break;
36962607Sitojun		FLAG('n', nflag, 1); break;
37062607Sitojun		FLAG('q', qflag, 1); break;
37162607Sitojun		FLAG('s', sflag, 1); break;
37262607Sitojun		FLAG('S', Sflag, 1); break;
37355163Sshin#undef	FLAG
37455163Sshin		default:
37555163Sshin			fatal("Invalid option specified, terminating");
37662607Sitojun			/*NOTREACHED*/
37755163Sshin		}
37855163Sshin	}
37955163Sshin	argc -= optind;
38055163Sshin	argv += optind;
38178064Sume	if (argc > 0) {
38255163Sshin		fatal("bogus extra arguments");
38378064Sume		/*NOTREACHED*/
38478064Sume	}
38555163Sshin
38655163Sshin	if (geteuid()) {
38755163Sshin		nflag = 1;
38855163Sshin		fprintf(stderr, "No kernel update is allowed\n");
38955163Sshin	}
390119037Sume
391119037Sume	if (dflag == 0) {
392119037Sume		if (daemon(0, 0) < 0) {
393119037Sume			fatal("daemon");
394119037Sume			/*NOTREACHED*/
395119037Sume		}
396119037Sume	}
397119037Sume
39855163Sshin	openlog(progname, LOG_NDELAY|LOG_PID, LOG_DAEMON);
39955163Sshin	logopened++;
40078064Sume
40178064Sume	if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL)
40278064Sume		fatal("malloc");
40378064Sume	memset(ripbuf, 0, RIP6_MAXMTU);
40478064Sume	ripbuf->rip6_cmd = RIP6_RESPONSE;
40578064Sume	ripbuf->rip6_vers = RIP6_VERSION;
40678064Sume	ripbuf->rip6_res1[0] = 0;
40778064Sume	ripbuf->rip6_res1[1] = 0;
40878064Sume
40955163Sshin	init();
41055163Sshin	ifconfig();
411243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
41255163Sshin		if (ifcp->ifc_index < 0) {
413243232Shrs			fprintf(stderr, "No ifindex found at %s "
414243232Shrs			    "(no link-local address?)\n", ifcp->ifc_name);
41555163Sshin			error++;
41655163Sshin		}
41755163Sshin	}
41855163Sshin	if (error)
41955163Sshin		exit(1);
42078064Sume	if (loopifcp == NULL) {
42155163Sshin		fatal("No loopback found");
42278064Sume		/*NOTREACHED*/
42378064Sume	}
424243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
42555163Sshin		ifrt(ifcp, 0);
426243232Shrs	}
42755163Sshin	filterconfig();
42855163Sshin	krtread(0);
42955163Sshin	if (dflag)
43055163Sshin		ifrtdump(0);
43155163Sshin
43255163Sshin	pid = getpid();
433243233Shrs	if ((pidfh = fopen(pidfile, "w")) != NULL) {
434243233Shrs		fprintf(pidfh, "%d\n", pid);
435243233Shrs		fclose(pidfh);
43655163Sshin	}
43755163Sshin
43878064Sume	if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL) {
43955163Sshin		fatal("malloc");
44078064Sume		/*NOTREACHED*/
44178064Sume	}
44262607Sitojun	memset(ripbuf, 0, RIP6_MAXMTU);
44355163Sshin	ripbuf->rip6_cmd = RIP6_RESPONSE;
44455163Sshin	ripbuf->rip6_vers = RIP6_VERSION;
44555163Sshin	ripbuf->rip6_res1[0] = 0;
44655163Sshin	ripbuf->rip6_res1[1] = 0;
44755163Sshin
44878064Sume	if (signal(SIGALRM, sighandler) == SIG_ERR ||
44978064Sume	    signal(SIGQUIT, sighandler) == SIG_ERR ||
45078064Sume	    signal(SIGTERM, sighandler) == SIG_ERR ||
45178064Sume	    signal(SIGUSR1, sighandler) == SIG_ERR ||
45278064Sume	    signal(SIGHUP, sighandler) == SIG_ERR ||
45378064Sume	    signal(SIGINT, sighandler) == SIG_ERR) {
45478064Sume		fatal("signal");
45578064Sume		/*NOTREACHED*/
45678064Sume	}
45755163Sshin	/*
45855163Sshin	 * To avoid rip packet congestion (not on a cable but in this
45955163Sshin	 * process), wait for a moment to send the first RIP6_RESPONSE
46055163Sshin	 * packets.
46155163Sshin	 */
46255163Sshin	alarm(ripinterval(INIT_INTERVAL6));
46355163Sshin
464243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
465243232Shrs		if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
466119041Sume			continue;
46755163Sshin		if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
46855163Sshin			sendrequest(ifcp);
46955163Sshin	}
47055163Sshin
47155163Sshin	syslog(LOG_INFO, "**** Started ****");
47255163Sshin	sigemptyset(&mask);
47355163Sshin	sigaddset(&mask, SIGALRM);
47455163Sshin	while (1) {
47578064Sume		if (seenalrm) {
47678064Sume			ripalarm();
47778064Sume			seenalrm = 0;
47878064Sume			continue;
47978064Sume		}
48078064Sume		if (seenquit) {
48178064Sume			rtdexit();
48278064Sume			seenquit = 0;
48378064Sume			continue;
48478064Sume		}
48578064Sume		if (seenusr1) {
48678064Sume			ifrtdump(SIGUSR1);
48778064Sume			seenusr1 = 0;
48878064Sume			continue;
48978064Sume		}
49078064Sume
491119076Sume#ifdef HAVE_POLL_H
492119076Sume		switch (poll(set, 2, INFTIM))
493119076Sume#else
494119070Sume		memcpy(recvecp, sockvecp, fdmasks);
495119076Sume		switch (select(maxfd + 1, recvecp, 0, 0, 0))
496119076Sume#endif
497119076Sume		{
49855163Sshin		case -1:
49978064Sume			if (errno != EINTR) {
50078064Sume				fatal("select");
50178064Sume				/*NOTREACHED*/
50278064Sume			}
50378064Sume			continue;
50455163Sshin		case 0:
50555163Sshin			continue;
50655163Sshin		default:
507119076Sume#ifdef HAVE_POLL_H
508119076Sume			if (set[0].revents & POLLIN)
509119076Sume#else
510119076Sume			if (FD_ISSET(ripsock, recvecp))
511119076Sume#endif
512119076Sume			{
51355163Sshin				sigprocmask(SIG_BLOCK, &mask, &omask);
51455163Sshin				riprecv();
51555163Sshin				sigprocmask(SIG_SETMASK, &omask, NULL);
51655163Sshin			}
517119076Sume#ifdef HAVE_POLL_H
518119076Sume			if (set[1].revents & POLLIN)
519119076Sume#else
520119076Sume			if (FD_ISSET(rtsock, recvecp))
521119076Sume#endif
522119076Sume			{
52355163Sshin				sigprocmask(SIG_BLOCK, &mask, &omask);
52455163Sshin				rtrecv();
52555163Sshin				sigprocmask(SIG_SETMASK, &omask, NULL);
52655163Sshin			}
52755163Sshin		}
52855163Sshin	}
52955163Sshin}
53055163Sshin
53178064Sumevoid
532243232Shrssighandler(int signo)
53378064Sume{
53478064Sume
53578064Sume	switch (signo) {
53678064Sume	case SIGALRM:
53778064Sume		seenalrm++;
53878064Sume		break;
53978064Sume	case SIGQUIT:
54078064Sume	case SIGTERM:
54178064Sume		seenquit++;
54278064Sume		break;
54378064Sume	case SIGUSR1:
54478064Sume	case SIGHUP:
54578064Sume	case SIGINT:
54678064Sume		seenusr1++;
54778064Sume		break;
54878064Sume	}
54978064Sume}
55078064Sume
55155163Sshin/*
55255163Sshin * gracefully exits after resetting sockopts.
55355163Sshin */
55455163Sshin/* ARGSUSED */
55555163Sshinvoid
556243232Shrsrtdexit(void)
55755163Sshin{
55855163Sshin	struct	riprt *rrt;
55955163Sshin
56055163Sshin	alarm(0);
561243232Shrs	TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
56262607Sitojun		if (rrt->rrt_rflags & RRTF_AGGREGATE) {
56355163Sshin			delroute(&rrt->rrt_info, &rrt->rrt_gw);
56455163Sshin		}
56555163Sshin	}
56655163Sshin	close(ripsock);
56755163Sshin	close(rtsock);
56855163Sshin	syslog(LOG_INFO, "**** Terminated ****");
56955163Sshin	closelog();
57055163Sshin	exit(1);
57155163Sshin}
57255163Sshin
57355163Sshin/*
57455163Sshin * Called periodically:
57555163Sshin *	1. age out the learned route. remove it if necessary.
57655163Sshin *	2. submit RIP6_RESPONSE packets.
57778064Sume * Invoked in every SUPPLY_INTERVAL6 (30) seconds.  I believe we don't have
57855163Sshin * to invoke this function in every 1 or 5 or 10 seconds only to age the
57955163Sshin * routes more precisely.
58055163Sshin */
58155163Sshin/* ARGSUSED */
58255163Sshinvoid
583243232Shrsripalarm(void)
58455163Sshin{
58555163Sshin	struct	ifc *ifcp;
586243232Shrs	struct	riprt *rrt, *rrt_tmp;
58755163Sshin	time_t	t_lifetime, t_holddown;
58855163Sshin
58955163Sshin	/* age the RIP routes */
59055163Sshin	t_lifetime = time(NULL) - RIP_LIFETIME;
59155163Sshin	t_holddown = t_lifetime - RIP_HOLDDOWN;
592243232Shrs	TAILQ_FOREACH_SAFE(rrt, &riprt_head, rrt_next, rrt_tmp) {
593243232Shrs		if (rrt->rrt_t == 0)
59455163Sshin			continue;
595243232Shrs		else if (rrt->rrt_t < t_holddown) {
596243232Shrs			TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
59755163Sshin			delroute(&rrt->rrt_info, &rrt->rrt_gw);
59855163Sshin			free(rrt);
599243232Shrs		} else if (rrt->rrt_t < t_lifetime)
60055163Sshin			rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
60155163Sshin	}
60255163Sshin	/* Supply updates */
603243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
60455163Sshin		if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
60555163Sshin			ripsend(ifcp, &ifcp->ifc_ripsin, 0);
60655163Sshin	}
60755163Sshin	alarm(ripinterval(SUPPLY_INTERVAL6));
60855163Sshin}
60955163Sshin
61055163Sshinvoid
611243232Shrsinit(void)
61255163Sshin{
613119034Sume	int	error;
614119034Sume	const int int0 = 0, int1 = 1, int255 = 255;
61555163Sshin	struct	addrinfo hints, *res;
616119081Sume	char	port[NI_MAXSERV];
61755163Sshin
618243232Shrs	TAILQ_INIT(&ifc_head);
61955163Sshin	nindex2ifc = 0;	/*initial guess*/
62055163Sshin	index2ifc = NULL;
621119081Sume	snprintf(port, sizeof(port), "%u", RIP6_PORT);
62255163Sshin
62355163Sshin	memset(&hints, 0, sizeof(hints));
62455163Sshin	hints.ai_family = PF_INET6;
62555163Sshin	hints.ai_socktype = SOCK_DGRAM;
626119080Sume	hints.ai_protocol = IPPROTO_UDP;
62755163Sshin	hints.ai_flags = AI_PASSIVE;
62855163Sshin	error = getaddrinfo(NULL, port, &hints, &res);
62978064Sume	if (error) {
63066807Skris		fatal("%s", gai_strerror(error));
63178064Sume		/*NOTREACHED*/
63278064Sume	}
63378064Sume	if (res->ai_next) {
63455163Sshin		fatal(":: resolved to multiple address");
63578064Sume		/*NOTREACHED*/
63678064Sume	}
63755163Sshin
63855163Sshin	ripsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
63978064Sume	if (ripsock < 0) {
64055163Sshin		fatal("rip socket");
64178064Sume		/*NOTREACHED*/
64278064Sume	}
643119034Sume#ifdef IPV6_V6ONLY
644119034Sume	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_V6ONLY,
645119034Sume	    &int1, sizeof(int1)) < 0) {
646119034Sume		fatal("rip IPV6_V6ONLY");
647119034Sume		/*NOTREACHED*/
648119034Sume	}
649119034Sume#endif
65078064Sume	if (bind(ripsock, res->ai_addr, res->ai_addrlen) < 0) {
65155163Sshin		fatal("rip bind");
65278064Sume		/*NOTREACHED*/
65378064Sume	}
65455163Sshin	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
65578064Sume	    &int255, sizeof(int255)) < 0) {
65655163Sshin		fatal("rip IPV6_MULTICAST_HOPS");
65778064Sume		/*NOTREACHED*/
65878064Sume	}
65955163Sshin	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
66078064Sume	    &int0, sizeof(int0)) < 0) {
66155163Sshin		fatal("rip IPV6_MULTICAST_LOOP");
66278064Sume		/*NOTREACHED*/
66378064Sume	}
66462921Sume
66562607Sitojun#ifdef IPV6_RECVPKTINFO
666119034Sume	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVPKTINFO,
667119034Sume	    &int1, sizeof(int1)) < 0) {
66862607Sitojun		fatal("rip IPV6_RECVPKTINFO");
66978064Sume		/*NOTREACHED*/
67078064Sume	}
67162607Sitojun#else  /* old adv. API */
672119034Sume	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_PKTINFO,
673119034Sume	    &int1, sizeof(int1)) < 0) {
67455163Sshin		fatal("rip IPV6_PKTINFO");
67578064Sume		/*NOTREACHED*/
67678064Sume	}
67762607Sitojun#endif
67855163Sshin
679164339Ssuz#ifdef IPV6_RECVPKTINFO
680164339Ssuz	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
681164339Ssuz	    &int1, sizeof(int1)) < 0) {
682164339Ssuz		fatal("rip IPV6_RECVHOPLIMIT");
683164339Ssuz		/*NOTREACHED*/
684164339Ssuz	}
685164339Ssuz#else  /* old adv. API */
686164339Ssuz	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_HOPLIMIT,
687164339Ssuz	    &int1, sizeof(int1)) < 0) {
688164339Ssuz		fatal("rip IPV6_HOPLIMIT");
689164339Ssuz		/*NOTREACHED*/
690164339Ssuz	}
691164339Ssuz#endif
692164339Ssuz
69355163Sshin	memset(&hints, 0, sizeof(hints));
69455163Sshin	hints.ai_family = PF_INET6;
69555163Sshin	hints.ai_socktype = SOCK_DGRAM;
696119080Sume	hints.ai_protocol = IPPROTO_UDP;
69755163Sshin	error = getaddrinfo(RIP6_DEST, port, &hints, &res);
69878064Sume	if (error) {
69966807Skris		fatal("%s", gai_strerror(error));
70078064Sume		/*NOTREACHED*/
70178064Sume	}
70278064Sume	if (res->ai_next) {
70355163Sshin		fatal("%s resolved to multiple address", RIP6_DEST);
70478064Sume		/*NOTREACHED*/
70578064Sume	}
70655163Sshin	memcpy(&ripsin, res->ai_addr, res->ai_addrlen);
70755163Sshin
708119076Sume#ifdef HAVE_POLL_H
709119076Sume	set[0].fd = ripsock;
710119076Sume	set[0].events = POLLIN;
711119076Sume#else
712119070Sume	maxfd = ripsock;
713119076Sume#endif
71455163Sshin
71555163Sshin	if (nflag == 0) {
71678064Sume		if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
71755163Sshin			fatal("route socket");
71878064Sume			/*NOTREACHED*/
71978064Sume		}
720119076Sume#ifdef HAVE_POLL_H
721119076Sume		set[1].fd = rtsock;
722119076Sume		set[1].events = POLLIN;
723119076Sume#else
724119070Sume		if (rtsock > maxfd)
725119070Sume			maxfd = rtsock;
726119076Sume#endif
727119076Sume	} else {
728119076Sume#ifdef HAVE_POLL_H
729119076Sume		set[1].fd = -1;
730119076Sume#else
73155163Sshin		rtsock = -1;	/*just for safety */
732119076Sume#endif
733119076Sume	}
734119070Sume
735119076Sume#ifndef HAVE_POLL_H
736119070Sume	fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
737119070Sume	if ((sockvecp = malloc(fdmasks)) == NULL) {
738119070Sume		fatal("malloc");
739119070Sume		/*NOTREACHED*/
740119070Sume	}
741119070Sume	if ((recvecp = malloc(fdmasks)) == NULL) {
742119070Sume		fatal("malloc");
743119070Sume		/*NOTREACHED*/
744119070Sume	}
745119070Sume	memset(sockvecp, 0, fdmasks);
746119070Sume	FD_SET(ripsock, sockvecp);
747119070Sume	if (rtsock >= 0)
748119070Sume		FD_SET(rtsock, sockvecp);
749119076Sume#endif
75055163Sshin}
75155163Sshin
75262607Sitojun#define	RIPSIZE(n) \
75362607Sitojun	(sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6))
75455163Sshin
75555163Sshin/*
75655163Sshin * ripflush flushes the rip datagram stored in the rip buffer
75755163Sshin */
75855163Sshinvoid
759243232Shrsripflush(struct ifc *ifcp, struct sockaddr_in6 *sin6, int nrt, struct netinfo6 *np)
76055163Sshin{
76155163Sshin	int i;
76255163Sshin	int error;
76355163Sshin
76455163Sshin	if (ifcp)
76555163Sshin		tracet(1, "Send(%s): info(%d) to %s.%d\n",
76655163Sshin			ifcp->ifc_name, nrt,
767119031Sume			inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
76855163Sshin	else
76955163Sshin		tracet(1, "Send: info(%d) to %s.%d\n",
770119031Sume			nrt, inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
77155163Sshin	if (dflag >= 2) {
77255163Sshin		np = ripbuf->rip6_nets;
77355163Sshin		for (i = 0; i < nrt; i++, np++) {
77455163Sshin			if (np->rip6_metric == NEXTHOP_METRIC) {
77555163Sshin				if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest))
77662607Sitojun					trace(2, "    NextHop reset");
77755163Sshin				else {
77855163Sshin					trace(2, "    NextHop %s",
77955163Sshin						inet6_n2p(&np->rip6_dest));
78055163Sshin				}
78155163Sshin			} else {
78255163Sshin				trace(2, "    %s/%d[%d]",
78355163Sshin					inet6_n2p(&np->rip6_dest),
78455163Sshin					np->rip6_plen, np->rip6_metric);
78555163Sshin			}
78655163Sshin			if (np->rip6_tag) {
78755163Sshin				trace(2, "  tag=0x%04x",
78855163Sshin					ntohs(np->rip6_tag) & 0xffff);
78955163Sshin			}
79055163Sshin			trace(2, "\n");
79155163Sshin		}
79255163Sshin	}
793119031Sume	error = sendpacket(sin6, RIPSIZE(nrt));
79455163Sshin	if (error == EAFNOSUPPORT) {
79555163Sshin		/* Protocol not supported */
79655163Sshin		tracet(1, "Could not send info to %s (%s): "
79755163Sshin			"set IFF_UP to 0\n",
79855163Sshin			ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
79955163Sshin		ifcp->ifc_flags &= ~IFF_UP;	/* As if down for AF_INET6 */
80055163Sshin	}
80155163Sshin}
80255163Sshin
80355163Sshin/*
80455163Sshin * Generate RIP6_RESPONSE packets and send them.
80555163Sshin */
80655163Sshinvoid
807243232Shrsripsend(struct	ifc *ifcp, struct sockaddr_in6 *sin6, int flag)
80855163Sshin{
80955163Sshin	struct	riprt *rrt;
81055163Sshin	struct	in6_addr *nh;	/* next hop */
811243232Shrs	struct netinfo6 *np;
81278064Sume	int	maxrte;
813243232Shrs	int nrt;
81455163Sshin
815119084Sume	if (qflag)
816119084Sume		return;
817119084Sume
81855163Sshin	if (ifcp == NULL) {
81955163Sshin		/*
82055163Sshin		 * Request from non-link local address is not
82155163Sshin		 * a regular route6d update.
82255163Sshin		 */
82362607Sitojun		maxrte = (IFMINMTU - sizeof(struct ip6_hdr) -
82462607Sitojun				sizeof(struct udphdr) -
82555163Sshin				sizeof(struct rip6) + sizeof(struct netinfo6)) /
82655163Sshin				sizeof(struct netinfo6);
827243232Shrs		nh = NULL;
828243232Shrs		nrt = 0;
829243232Shrs		np = ripbuf->rip6_nets;
830243232Shrs		TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
83162607Sitojun			if (rrt->rrt_rflags & RRTF_NOADVERTISE)
83255163Sshin				continue;
83355163Sshin			/* Put the route to the buffer */
83455163Sshin			*np = rrt->rrt_info;
83555163Sshin			np++; nrt++;
83655163Sshin			if (nrt == maxrte) {
837243232Shrs				ripflush(NULL, sin6, nrt, np);
83855163Sshin				nh = NULL;
839243232Shrs				nrt = 0;
840243232Shrs				np = ripbuf->rip6_nets;
84155163Sshin			}
84255163Sshin		}
84355163Sshin		if (nrt)	/* Send last packet */
844243232Shrs			ripflush(NULL, sin6, nrt, np);
84555163Sshin		return;
84655163Sshin	}
84755163Sshin
84862607Sitojun	if ((flag & RRTF_SENDANYWAY) == 0 &&
84955163Sshin	    (qflag || (ifcp->ifc_flags & IFF_LOOPBACK)))
85055163Sshin		return;
85178064Sume
85278064Sume	/* -N: no use */
853243232Shrs	if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
85455163Sshin		return;
85578064Sume
85678064Sume	/* -T: generate default route only */
857243232Shrs	if (iff_find(ifcp, IFIL_TYPE_T) != NULL) {
85855163Sshin		struct netinfo6 rrt_info;
85955163Sshin		memset(&rrt_info, 0, sizeof(struct netinfo6));
86055163Sshin		rrt_info.rip6_dest = in6addr_any;
86155163Sshin		rrt_info.rip6_plen = 0;
86255163Sshin		rrt_info.rip6_metric = 1;
86378064Sume		rrt_info.rip6_metric += ifcp->ifc_metric;
86455163Sshin		rrt_info.rip6_tag = htons(routetag & 0xffff);
86555163Sshin		np = ripbuf->rip6_nets;
86655163Sshin		*np = rrt_info;
86755163Sshin		nrt = 1;
868243232Shrs		ripflush(ifcp, sin6, nrt, np);
86955163Sshin		return;
87055163Sshin	}
87178064Sume
87262607Sitojun	maxrte = (ifcp->ifc_mtu - sizeof(struct ip6_hdr) -
87362607Sitojun			sizeof(struct udphdr) -
87455163Sshin			sizeof(struct rip6) + sizeof(struct netinfo6)) /
87555163Sshin			sizeof(struct netinfo6);
87678064Sume
87755163Sshin	nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
878243232Shrs	TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
87962607Sitojun		if (rrt->rrt_rflags & RRTF_NOADVERTISE)
88055163Sshin			continue;
88178064Sume
88278064Sume		/* Need to check filter here */
88378064Sume		if (out_filter(rrt, ifcp) == 0)
88455163Sshin			continue;
88578064Sume
88655163Sshin		/* Check split horizon and other conditions */
88755163Sshin		if (tobeadv(rrt, ifcp) == 0)
88855163Sshin			continue;
88978064Sume
89055163Sshin		/* Only considers the routes with flag if specified */
89162607Sitojun		if ((flag & RRTF_CHANGED) &&
89262607Sitojun		    (rrt->rrt_rflags & RRTF_CHANGED) == 0)
89355163Sshin			continue;
89478064Sume
89555163Sshin		/* Check nexthop */
89655163Sshin		if (rrt->rrt_index == ifcp->ifc_index &&
89755163Sshin		    !IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_gw) &&
89862607Sitojun		    (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR) == 0) {
89955163Sshin			if (nh == NULL || !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw)) {
900243232Shrs				if (nrt == maxrte - 2) {
901243232Shrs					ripflush(ifcp, sin6, nrt, np);
902243232Shrs					nh = NULL;
903243232Shrs					nrt = 0;
904243232Shrs					np = ripbuf->rip6_nets;
905243232Shrs				}
906243232Shrs
90755163Sshin				np->rip6_dest = rrt->rrt_gw;
90855163Sshin				np->rip6_plen = 0;
90955163Sshin				np->rip6_tag = 0;
91055163Sshin				np->rip6_metric = NEXTHOP_METRIC;
91155163Sshin				nh = &rrt->rrt_gw;
91255163Sshin				np++; nrt++;
91355163Sshin			}
91455163Sshin		} else if (nh && (rrt->rrt_index != ifcp->ifc_index ||
91555163Sshin			          !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw) ||
91662607Sitojun				  rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)) {
91755163Sshin			/* Reset nexthop */
918243232Shrs			if (nrt == maxrte - 2) {
919243232Shrs				ripflush(ifcp, sin6, nrt, np);
920243232Shrs				nh = NULL;
921243232Shrs				nrt = 0;
922243232Shrs				np = ripbuf->rip6_nets;
923243232Shrs			}
92455163Sshin			memset(np, 0, sizeof(struct netinfo6));
92555163Sshin			np->rip6_metric = NEXTHOP_METRIC;
92655163Sshin			nh = NULL;
92755163Sshin			np++; nrt++;
92855163Sshin		}
92978064Sume
93055163Sshin		/* Put the route to the buffer */
93155163Sshin		*np = rrt->rrt_info;
93255163Sshin		np++; nrt++;
93355163Sshin		if (nrt == maxrte) {
934243232Shrs			ripflush(ifcp, sin6, nrt, np);
93555163Sshin			nh = NULL;
936243232Shrs			nrt = 0;
937243232Shrs			np = ripbuf->rip6_nets;
93855163Sshin		}
93955163Sshin	}
94055163Sshin	if (nrt)	/* Send last packet */
941243232Shrs		ripflush(ifcp, sin6, nrt, np);
94255163Sshin}
94355163Sshin
94455163Sshin/*
94578064Sume * outbound filter logic, per-route/interface.
94678064Sume */
94778064Sumeint
948243232Shrsout_filter(struct riprt *rrt, struct ifc *ifcp)
94978064Sume{
95078064Sume	struct iff *iffp;
95178064Sume	struct in6_addr ia;
95278064Sume	int ok;
95378064Sume
95478064Sume	/*
95578064Sume	 * -A: filter out less specific routes, if we have aggregated
95678064Sume	 * route configured.
95778064Sume	 */
958243232Shrs	TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
95978064Sume		if (iffp->iff_type != 'A')
96078064Sume			continue;
96178064Sume		if (rrt->rrt_info.rip6_plen <= iffp->iff_plen)
96278064Sume			continue;
96378064Sume		ia = rrt->rrt_info.rip6_dest;
96478064Sume		applyplen(&ia, iffp->iff_plen);
96578064Sume		if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr))
96678064Sume			return 0;
96778064Sume	}
96878064Sume
96978064Sume	/*
97078064Sume	 * if it is an aggregated route, advertise it only to the
97178064Sume	 * interfaces specified on -A.
97278064Sume	 */
97378064Sume	if ((rrt->rrt_rflags & RRTF_AGGREGATE) != 0) {
97478064Sume		ok = 0;
975243232Shrs		TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
97678064Sume			if (iffp->iff_type != 'A')
97778064Sume				continue;
97878064Sume			if (rrt->rrt_info.rip6_plen == iffp->iff_plen &&
97978064Sume			    IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
98078064Sume			    &iffp->iff_addr)) {
98178064Sume				ok = 1;
98278064Sume				break;
98378064Sume			}
98478064Sume		}
98578064Sume		if (!ok)
98678064Sume			return 0;
98778064Sume	}
98878064Sume
98978064Sume	/*
99078064Sume	 * -O: advertise only if prefix matches the configured prefix.
99178064Sume	 */
992243232Shrs	if (iff_find(ifcp, IFIL_TYPE_O) != NULL) {
99378064Sume		ok = 0;
994243232Shrs		TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
99578064Sume			if (iffp->iff_type != 'O')
99678064Sume				continue;
99778064Sume			if (rrt->rrt_info.rip6_plen < iffp->iff_plen)
99878064Sume				continue;
99978064Sume			ia = rrt->rrt_info.rip6_dest;
100078064Sume			applyplen(&ia, iffp->iff_plen);
100178064Sume			if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
100278064Sume				ok = 1;
100378064Sume				break;
100478064Sume			}
100578064Sume		}
100678064Sume		if (!ok)
100778064Sume			return 0;
100878064Sume	}
100978064Sume
101078064Sume	/* the prefix should be advertised */
101178064Sume	return 1;
101278064Sume}
101378064Sume
101478064Sume/*
101555163Sshin * Determine if the route is to be advertised on the specified interface.
101655163Sshin * It checks options specified in the arguments and the split horizon rule.
101755163Sshin */
101855163Sshinint
1019243232Shrstobeadv(struct riprt *rrt, struct ifc *ifcp)
102055163Sshin{
102155163Sshin
102255163Sshin	/* Special care for static routes */
102355163Sshin	if (rrt->rrt_flags & RTF_STATIC) {
102462607Sitojun		/* XXX don't advertise reject/blackhole routes */
102562607Sitojun		if (rrt->rrt_flags & (RTF_REJECT | RTF_BLACKHOLE))
102662607Sitojun			return 0;
102762607Sitojun
102855163Sshin		if (Sflag)	/* Yes, advertise it anyway */
102955163Sshin			return 1;
103055163Sshin		if (sflag && rrt->rrt_index != ifcp->ifc_index)
103155163Sshin			return 1;
103255163Sshin		return 0;
103355163Sshin	}
103455163Sshin	/* Regular split horizon */
103555163Sshin	if (hflag == 0 && rrt->rrt_index == ifcp->ifc_index)
103655163Sshin		return 0;
103755163Sshin	return 1;
103855163Sshin}
103955163Sshin
104055163Sshin/*
104155163Sshin * Send a rip packet actually.
104255163Sshin */
104355163Sshinint
1044243232Shrssendpacket(struct sockaddr_in6 *sin6, int len)
104555163Sshin{
104655163Sshin	struct msghdr m;
104755163Sshin	struct cmsghdr *cm;
104855163Sshin	struct iovec iov[2];
104955163Sshin	u_char cmsgbuf[256];
105055163Sshin	struct in6_pktinfo *pi;
105178064Sume	int idx;
105255163Sshin	struct sockaddr_in6 sincopy;
105355163Sshin
105455163Sshin	/* do not overwrite the given sin */
1055119031Sume	sincopy = *sin6;
1056119031Sume	sin6 = &sincopy;
105755163Sshin
1058119035Sume	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
1059243231Shrs	    IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1060243231Shrs		idx = sin6->sin6_scope_id;
1061243231Shrs	else
106278064Sume		idx = 0;
106355163Sshin
1064119031Sume	m.msg_name = (caddr_t)sin6;
1065119031Sume	m.msg_namelen = sizeof(*sin6);
106655163Sshin	iov[0].iov_base = (caddr_t)ripbuf;
106755163Sshin	iov[0].iov_len = len;
106855163Sshin	m.msg_iov = iov;
106955163Sshin	m.msg_iovlen = 1;
107078064Sume	if (!idx) {
107155163Sshin		m.msg_control = NULL;
107255163Sshin		m.msg_controllen = 0;
107355163Sshin	} else {
107455163Sshin		memset(cmsgbuf, 0, sizeof(cmsgbuf));
107555163Sshin		cm = (struct cmsghdr *)cmsgbuf;
107655163Sshin		m.msg_control = (caddr_t)cm;
107755163Sshin		m.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
107855163Sshin
107955163Sshin		cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
108055163Sshin		cm->cmsg_level = IPPROTO_IPV6;
108155163Sshin		cm->cmsg_type = IPV6_PKTINFO;
108255163Sshin		pi = (struct in6_pktinfo *)CMSG_DATA(cm);
108355163Sshin		memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*::*/
108478064Sume		pi->ipi6_ifindex = idx;
108555163Sshin	}
108655163Sshin
108755163Sshin	if (sendmsg(ripsock, &m, 0 /*MSG_DONTROUTE*/) < 0) {
108855163Sshin		trace(1, "sendmsg: %s\n", strerror(errno));
108955163Sshin		return errno;
109055163Sshin	}
109162921Sume
109255163Sshin	return 0;
109355163Sshin}
109455163Sshin
109555163Sshin/*
109678064Sume * Receive and process RIP packets.  Update the routes/kernel forwarding
109755163Sshin * table if necessary.
109855163Sshin */
109955163Sshinvoid
1100243232Shrsriprecv(void)
110155163Sshin{
110255163Sshin	struct	ifc *ifcp, *ic;
110355163Sshin	struct	sockaddr_in6 fsock;
110455163Sshin	struct	in6_addr nh;	/* next hop */
110555163Sshin	struct	rip6 *rp;
110655163Sshin	struct	netinfo6 *np, *nq;
110755163Sshin	struct	riprt *rrt;
1108122677Sume	ssize_t	len, nn;
1109122677Sume	unsigned int need_trigger, idx;
111055163Sshin	char	buf[4 * RIP6_MAXMTU];
111155163Sshin	time_t	t;
111255163Sshin	struct msghdr m;
111355163Sshin	struct cmsghdr *cm;
111455163Sshin	struct iovec iov[2];
111555163Sshin	u_char cmsgbuf[256];
1116164339Ssuz	struct in6_pktinfo *pi = NULL;
1117164339Ssuz	int *hlimp = NULL;
111855163Sshin	struct iff *iffp;
111955163Sshin	struct in6_addr ia;
112055163Sshin	int ok;
112178064Sume	time_t t_half_lifetime;
112255163Sshin
112355163Sshin	need_trigger = 0;
112462921Sume
112555163Sshin	m.msg_name = (caddr_t)&fsock;
112655163Sshin	m.msg_namelen = sizeof(fsock);
112755163Sshin	iov[0].iov_base = (caddr_t)buf;
112855163Sshin	iov[0].iov_len = sizeof(buf);
112955163Sshin	m.msg_iov = iov;
113055163Sshin	m.msg_iovlen = 1;
113155163Sshin	cm = (struct cmsghdr *)cmsgbuf;
113255163Sshin	m.msg_control = (caddr_t)cm;
113355163Sshin	m.msg_controllen = sizeof(cmsgbuf);
113478064Sume	if ((len = recvmsg(ripsock, &m, 0)) < 0) {
113555163Sshin		fatal("recvmsg");
113678064Sume		/*NOTREACHED*/
113778064Sume	}
113878064Sume	idx = 0;
113955163Sshin	for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&m);
114055163Sshin	     cm;
114155163Sshin	     cm = (struct cmsghdr *)CMSG_NXTHDR(&m, cm)) {
1142164339Ssuz		if (cm->cmsg_level != IPPROTO_IPV6)
1143164339Ssuz		    continue;
1144164339Ssuz		switch (cm->cmsg_type) {
1145164339Ssuz		case IPV6_PKTINFO:
1146164339Ssuz			if (cm->cmsg_len != CMSG_LEN(sizeof(*pi))) {
1147164339Ssuz				trace(1,
1148164339Ssuz				    "invalid cmsg length for IPV6_PKTINFO\n");
1149164339Ssuz				return;
1150164339Ssuz			}
115155163Sshin			pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
115278064Sume			idx = pi->ipi6_ifindex;
115355163Sshin			break;
1154164339Ssuz		case IPV6_HOPLIMIT:
1155164339Ssuz			if (cm->cmsg_len != CMSG_LEN(sizeof(int))) {
1156164339Ssuz				trace(1,
1157164339Ssuz				    "invalid cmsg length for IPV6_HOPLIMIT\n");
1158164339Ssuz				return;
1159164339Ssuz			}
1160164339Ssuz			hlimp = (int *)CMSG_DATA(cm);
1161164339Ssuz			break;
116255163Sshin		}
116355163Sshin	}
116455163Sshin
1165243232Shrs	if ((size_t)len < sizeof(struct rip6)) {
1166121779Ssuz		trace(1, "Packet too short\n");
1167121779Ssuz		return;
1168121779Ssuz	}
1169121779Ssuz
1170164339Ssuz	if (pi == NULL || hlimp == NULL) {
1171164339Ssuz		/*
1172164339Ssuz		 * This can happen when the kernel failed to allocate memory
1173164339Ssuz		 * for the ancillary data.  Although we might be able to handle
1174164339Ssuz		 * some cases without this info, those are minor and not so
1175164339Ssuz		 * important, so it's better to discard the packet for safer
1176164339Ssuz		 * operation.
1177164339Ssuz		 */
1178164339Ssuz		trace(1, "IPv6 packet information cannot be retrieved\n");
1179164339Ssuz		return;
1180164339Ssuz	}
1181164339Ssuz
118255163Sshin	nh = fsock.sin6_addr;
118355163Sshin	nn = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
118455163Sshin		sizeof(struct netinfo6);
118555163Sshin	rp = (struct rip6 *)buf;
118655163Sshin	np = rp->rip6_nets;
118755163Sshin
1188119035Sume	if (rp->rip6_vers != RIP6_VERSION) {
118955163Sshin		trace(1, "Incorrect RIP version %d\n", rp->rip6_vers);
119055163Sshin		return;
119155163Sshin	}
119255163Sshin	if (rp->rip6_cmd == RIP6_REQUEST) {
119378064Sume		if (idx && idx < nindex2ifc) {
119478064Sume			ifcp = index2ifc[idx];
119555163Sshin			riprequest(ifcp, np, nn, &fsock);
119655163Sshin		} else {
119755163Sshin			riprequest(NULL, np, nn, &fsock);
119855163Sshin		}
119962607Sitojun		return;
120062607Sitojun	}
120155163Sshin
120255163Sshin	if (!IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr)) {
1203164339Ssuz		trace(1, "Response from non-ll addr: %s\n",
120478064Sume		    inet6_n2p(&fsock.sin6_addr));
120555163Sshin		return;		/* Ignore packets from non-link-local addr */
120655163Sshin	}
1207164339Ssuz	if (ntohs(fsock.sin6_port) != RIP6_PORT) {
1208164339Ssuz		trace(1, "Response from non-rip port from %s\n",
1209164339Ssuz		    inet6_n2p(&fsock.sin6_addr));
1210164339Ssuz		return;
1211164339Ssuz	}
1212164339Ssuz	if (IN6_IS_ADDR_MULTICAST(&pi->ipi6_addr) && *hlimp != 255) {
1213164339Ssuz		trace(1,
1214164339Ssuz		    "Response packet with a smaller hop limit (%d) from %s\n",
1215164339Ssuz		    *hlimp, inet6_n2p(&fsock.sin6_addr));
1216164339Ssuz		return;
1217164339Ssuz	}
1218164339Ssuz	/*
1219164339Ssuz	 * Further validation: since this program does not send off-link
1220164339Ssuz	 * requests, an incoming response must always come from an on-link
1221164339Ssuz	 * node.  Although this is normally ensured by the source address
1222164339Ssuz	 * check above, it may not 100% be safe because there are router
1223164339Ssuz	 * implementations that (invalidly) allow a packet with a link-local
1224164339Ssuz	 * source address to be forwarded to a different link.
1225164339Ssuz	 * So we also check whether the destination address is a link-local
1226164339Ssuz	 * address or the hop limit is 255.  Note that RFC2080 does not require
1227164339Ssuz	 * the specific hop limit for a unicast response, so we cannot assume
1228164339Ssuz	 * the limitation.
1229164339Ssuz	 */
1230164339Ssuz	if (!IN6_IS_ADDR_LINKLOCAL(&pi->ipi6_addr) && *hlimp != 255) {
1231164339Ssuz		trace(1,
1232164339Ssuz		    "Response packet possibly from an off-link node: "
1233164339Ssuz		    "from %s to %s hlim=%d\n",
1234164339Ssuz		    inet6_n2p(&fsock.sin6_addr),
1235164339Ssuz		    inet6_n2p(&pi->ipi6_addr), *hlimp);
1236164339Ssuz		return;
1237164339Ssuz	}
1238164339Ssuz
1239243231Shrs	idx = fsock.sin6_scope_id;
124078064Sume	ifcp = (idx < nindex2ifc) ? index2ifc[idx] : NULL;
124155163Sshin	if (!ifcp) {
124278064Sume		trace(1, "Packets to unknown interface index %d\n", idx);
124355163Sshin		return;		/* Ignore it */
124455163Sshin	}
124555163Sshin	if (IN6_ARE_ADDR_EQUAL(&ifcp->ifc_mylladdr, &fsock.sin6_addr))
124655163Sshin		return;		/* The packet is from me; ignore */
124755163Sshin	if (rp->rip6_cmd != RIP6_RESPONSE) {
124855163Sshin		trace(1, "Invalid command %d\n", rp->rip6_cmd);
124962607Sitojun		return;
125055163Sshin	}
125178064Sume
125278064Sume	/* -N: no use */
1253243232Shrs	if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
125455163Sshin		return;
125578064Sume
1256228674Sdim	tracet(1, "Recv(%s): from %s.%d info(%zd)\n",
125778064Sume	    ifcp->ifc_name, inet6_n2p(&nh), ntohs(fsock.sin6_port), nn);
125855163Sshin
125955163Sshin	t = time(NULL);
126078064Sume	t_half_lifetime = t - (RIP_LIFETIME/2);
126155163Sshin	for (; nn; nn--, np++) {
126255163Sshin		if (np->rip6_metric == NEXTHOP_METRIC) {
126355163Sshin			/* modify neighbor address */
126455163Sshin			if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
126555163Sshin				nh = np->rip6_dest;
126655163Sshin				trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
126755163Sshin			} else if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest)) {
126855163Sshin				nh = fsock.sin6_addr;
126955163Sshin				trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
127055163Sshin			} else {
127155163Sshin				nh = fsock.sin6_addr;
127255163Sshin				trace(1, "\tInvalid Nexthop: %s\n",
127378064Sume				    inet6_n2p(&np->rip6_dest));
127455163Sshin			}
127555163Sshin			continue;
127655163Sshin		}
127755163Sshin		if (IN6_IS_ADDR_MULTICAST(&np->rip6_dest)) {
127855163Sshin			trace(1, "\tMulticast netinfo6: %s/%d [%d]\n",
127955163Sshin				inet6_n2p(&np->rip6_dest),
128055163Sshin				np->rip6_plen, np->rip6_metric);
128155163Sshin			continue;
128255163Sshin		}
128355163Sshin		if (IN6_IS_ADDR_LOOPBACK(&np->rip6_dest)) {
128455163Sshin			trace(1, "\tLoopback netinfo6: %s/%d [%d]\n",
128555163Sshin				inet6_n2p(&np->rip6_dest),
128655163Sshin				np->rip6_plen, np->rip6_metric);
128755163Sshin			continue;
128855163Sshin		}
128955163Sshin		if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
129055163Sshin			trace(1, "\tLink Local netinfo6: %s/%d [%d]\n",
129155163Sshin				inet6_n2p(&np->rip6_dest),
129255163Sshin				np->rip6_plen, np->rip6_metric);
129355163Sshin			continue;
129455163Sshin		}
129555163Sshin		/* may need to pass sitelocal prefix in some case, however*/
129655163Sshin		if (IN6_IS_ADDR_SITELOCAL(&np->rip6_dest) && !lflag) {
129755163Sshin			trace(1, "\tSite Local netinfo6: %s/%d [%d]\n",
129855163Sshin				inet6_n2p(&np->rip6_dest),
129955163Sshin				np->rip6_plen, np->rip6_metric);
130055163Sshin			continue;
130155163Sshin		}
130255163Sshin		trace(2, "\tnetinfo6: %s/%d [%d]",
130355163Sshin			inet6_n2p(&np->rip6_dest),
130455163Sshin			np->rip6_plen, np->rip6_metric);
130555163Sshin		if (np->rip6_tag)
130655163Sshin			trace(2, "  tag=0x%04x", ntohs(np->rip6_tag) & 0xffff);
130762607Sitojun		if (dflag >= 2) {
130862607Sitojun			ia = np->rip6_dest;
130962607Sitojun			applyplen(&ia, np->rip6_plen);
131062607Sitojun			if (!IN6_ARE_ADDR_EQUAL(&ia, &np->rip6_dest))
131162607Sitojun				trace(2, " [junk outside prefix]");
131262607Sitojun		}
131355163Sshin
131478064Sume		/*
131578064Sume		 * -L: listen only if the prefix matches the configuration
131678064Sume		 */
1317243232Shrs                ok = 1;	/* if there's no L filter, it is ok */
1318243232Shrs                TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
1319243232Shrs                        if (iffp->iff_type != IFIL_TYPE_L)
1320243232Shrs                                continue;
1321243232Shrs                        ok = 0;
1322243232Shrs                        if (np->rip6_plen < iffp->iff_plen)
1323243232Shrs                                continue;
1324243232Shrs                        /* special rule: ::/0 means default, not "in /0" */
1325243232Shrs                        if (iffp->iff_plen == 0 && np->rip6_plen > 0)
1326243232Shrs                                continue;
1327243232Shrs                        ia = np->rip6_dest;
1328243232Shrs                        applyplen(&ia, iffp->iff_plen);
1329243232Shrs                        if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1330243232Shrs                                ok = 1;
1331243232Shrs                                break;
1332243232Shrs                        }
1333243232Shrs                }
133455163Sshin		if (!ok) {
133555163Sshin			trace(2, "  (filtered)\n");
133655163Sshin			continue;
133755163Sshin		}
133855163Sshin
133955163Sshin		trace(2, "\n");
134055163Sshin		np->rip6_metric++;
134155163Sshin		np->rip6_metric += ifcp->ifc_metric;
134255163Sshin		if (np->rip6_metric > HOPCNT_INFINITY6)
134355163Sshin			np->rip6_metric = HOPCNT_INFINITY6;
134455163Sshin
134555163Sshin		applyplen(&np->rip6_dest, np->rip6_plen);
1346243232Shrs		if ((rrt = rtsearch(np)) != NULL) {
134755163Sshin			if (rrt->rrt_t == 0)
134855163Sshin				continue;	/* Intf route has priority */
134955163Sshin			nq = &rrt->rrt_info;
135055163Sshin			if (nq->rip6_metric > np->rip6_metric) {
135155163Sshin				if (rrt->rrt_index == ifcp->ifc_index &&
135255163Sshin				    IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
135355163Sshin					/* Small metric from the same gateway */
135455163Sshin					nq->rip6_metric = np->rip6_metric;
135555163Sshin				} else {
135655163Sshin					/* Better route found */
135755163Sshin					rrt->rrt_index = ifcp->ifc_index;
135855163Sshin					/* Update routing table */
135955163Sshin					delroute(nq, &rrt->rrt_gw);
136055163Sshin					rrt->rrt_gw = nh;
136155163Sshin					*nq = *np;
136255163Sshin					addroute(rrt, &nh, ifcp);
136355163Sshin				}
136462607Sitojun				rrt->rrt_rflags |= RRTF_CHANGED;
136555163Sshin				rrt->rrt_t = t;
136655163Sshin				need_trigger = 1;
136755163Sshin			} else if (nq->rip6_metric < np->rip6_metric &&
136855163Sshin				   rrt->rrt_index == ifcp->ifc_index &&
136955163Sshin				   IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
137055163Sshin				/* Got worse route from same gw */
137155163Sshin				nq->rip6_metric = np->rip6_metric;
137255163Sshin				rrt->rrt_t = t;
137362607Sitojun				rrt->rrt_rflags |= RRTF_CHANGED;
137455163Sshin				need_trigger = 1;
137555163Sshin			} else if (nq->rip6_metric == np->rip6_metric &&
137655163Sshin				   np->rip6_metric < HOPCNT_INFINITY6) {
137778064Sume				if (rrt->rrt_index == ifcp->ifc_index &&
137878064Sume				   IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
137978064Sume					/* same metric, same route from same gw */
138078064Sume					rrt->rrt_t = t;
138178064Sume				} else if (rrt->rrt_t < t_half_lifetime) {
138278064Sume					/* Better route found */
138378064Sume					rrt->rrt_index = ifcp->ifc_index;
138478064Sume					/* Update routing table */
138578064Sume					delroute(nq, &rrt->rrt_gw);
138678064Sume					rrt->rrt_gw = nh;
138778064Sume					*nq = *np;
138878064Sume					addroute(rrt, &nh, ifcp);
138978064Sume					rrt->rrt_rflags |= RRTF_CHANGED;
139078064Sume					rrt->rrt_t = t;
139178064Sume				}
139255163Sshin			}
139362607Sitojun			/*
139455163Sshin			 * if nq->rip6_metric == HOPCNT_INFINITY6 then
139578064Sume			 * do not update age value.  Do nothing.
139655163Sshin			 */
139755163Sshin		} else if (np->rip6_metric < HOPCNT_INFINITY6) {
139855163Sshin			/* Got a new valid route */
139978064Sume			if ((rrt = MALLOC(struct riprt)) == NULL) {
140055163Sshin				fatal("malloc: struct riprt");
140178064Sume				/*NOTREACHED*/
140278064Sume			}
140362607Sitojun			memset(rrt, 0, sizeof(*rrt));
140455163Sshin			nq = &rrt->rrt_info;
140555163Sshin
140655163Sshin			rrt->rrt_same = NULL;
140755163Sshin			rrt->rrt_index = ifcp->ifc_index;
140855163Sshin			rrt->rrt_flags = RTF_UP|RTF_GATEWAY;
140955163Sshin			rrt->rrt_gw = nh;
141055163Sshin			*nq = *np;
141155163Sshin			applyplen(&nq->rip6_dest, nq->rip6_plen);
141255163Sshin			if (nq->rip6_plen == sizeof(struct in6_addr) * 8)
141355163Sshin				rrt->rrt_flags |= RTF_HOST;
141455163Sshin
141555163Sshin			/* Update routing table */
141655163Sshin			addroute(rrt, &nh, ifcp);
141762607Sitojun			rrt->rrt_rflags |= RRTF_CHANGED;
141855163Sshin			need_trigger = 1;
141955163Sshin			rrt->rrt_t = t;
1420243232Shrs
1421243232Shrs			/* Put the route to the list */
1422243232Shrs			TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
142355163Sshin		}
142455163Sshin	}
142555163Sshin	/* XXX need to care the interval between triggered updates */
142655163Sshin	if (need_trigger) {
142755163Sshin		if (nextalarm > time(NULL) + RIP_TRIG_INT6_MAX) {
1428243232Shrs			TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
142955163Sshin				if (ifcp->ifc_index == ic->ifc_index)
143055163Sshin					continue;
143155163Sshin				if (ic->ifc_flags & IFF_UP)
143255163Sshin					ripsend(ic, &ic->ifc_ripsin,
143362607Sitojun						RRTF_CHANGED);
143455163Sshin			}
143555163Sshin		}
143655163Sshin		/* Reset the flag */
1437243232Shrs		TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
143862607Sitojun			rrt->rrt_rflags &= ~RRTF_CHANGED;
1439243232Shrs		}
144055163Sshin	}
144155163Sshin}
144255163Sshin
144355163Sshin/*
144455163Sshin * Send all routes request packet to the specified interface.
144555163Sshin */
144655163Sshinvoid
1447243232Shrssendrequest(struct ifc *ifcp)
144855163Sshin{
144955163Sshin	struct netinfo6 *np;
145055163Sshin	int error;
145155163Sshin
145255163Sshin	if (ifcp->ifc_flags & IFF_LOOPBACK)
145355163Sshin		return;
145455163Sshin	ripbuf->rip6_cmd = RIP6_REQUEST;
145555163Sshin	np = ripbuf->rip6_nets;
145655163Sshin	memset(np, 0, sizeof(struct netinfo6));
145755163Sshin	np->rip6_metric = HOPCNT_INFINITY6;
145855163Sshin	tracet(1, "Send rtdump Request to %s (%s)\n",
145955163Sshin		ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
146055163Sshin	error = sendpacket(&ifcp->ifc_ripsin, RIPSIZE(1));
146155163Sshin	if (error == EAFNOSUPPORT) {
146255163Sshin		/* Protocol not supported */
146355163Sshin		tracet(1, "Could not send rtdump Request to %s (%s): "
146455163Sshin			"set IFF_UP to 0\n",
146555163Sshin			ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
146655163Sshin		ifcp->ifc_flags &= ~IFF_UP;	/* As if down for AF_INET6 */
146755163Sshin	}
146855163Sshin	ripbuf->rip6_cmd = RIP6_RESPONSE;
146955163Sshin}
147055163Sshin
147155163Sshin/*
147255163Sshin * Process a RIP6_REQUEST packet.
147355163Sshin */
147455163Sshinvoid
1475243232Shrsriprequest(struct ifc *ifcp,
1476243232Shrs	struct netinfo6 *np,
1477243232Shrs	int nn,
1478243232Shrs	struct sockaddr_in6 *sin6)
147955163Sshin{
148055163Sshin	int i;
148155163Sshin	struct riprt *rrt;
148255163Sshin
148355163Sshin	if (!(nn == 1 && IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest) &&
148455163Sshin	      np->rip6_plen == 0 && np->rip6_metric == HOPCNT_INFINITY6)) {
148555163Sshin		/* Specific response, don't split-horizon */
148655163Sshin		trace(1, "\tRIP Request\n");
148755163Sshin		for (i = 0; i < nn; i++, np++) {
1488243232Shrs			rrt = rtsearch(np);
148955163Sshin			if (rrt)
149055163Sshin				np->rip6_metric = rrt->rrt_info.rip6_metric;
149155163Sshin			else
149255163Sshin				np->rip6_metric = HOPCNT_INFINITY6;
149355163Sshin		}
1494119031Sume		(void)sendpacket(sin6, RIPSIZE(nn));
149555163Sshin		return;
149655163Sshin	}
149755163Sshin	/* Whole routing table dump */
149855163Sshin	trace(1, "\tRIP Request -- whole routing table\n");
1499119031Sume	ripsend(ifcp, sin6, RRTF_SENDANYWAY);
150055163Sshin}
150155163Sshin
150255163Sshin/*
150355163Sshin * Get information of each interface.
150455163Sshin */
150555163Sshinvoid
1506243232Shrsifconfig(void)
150755163Sshin{
150862607Sitojun	struct ifaddrs *ifap, *ifa;
150962607Sitojun	struct ifc *ifcp;
151062607Sitojun	struct ipv6_mreq mreq;
151162607Sitojun	int s;
151262607Sitojun
151378064Sume	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
151462607Sitojun		fatal("socket");
151578064Sume		/*NOTREACHED*/
151678064Sume	}
151762607Sitojun
151878064Sume	if (getifaddrs(&ifap) != 0) {
151962607Sitojun		fatal("getifaddrs");
152078064Sume		/*NOTREACHED*/
152178064Sume	}
152262607Sitojun
152362607Sitojun	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
152462607Sitojun		if (ifa->ifa_addr->sa_family != AF_INET6)
152562607Sitojun			continue;
152662607Sitojun		ifcp = ifc_find(ifa->ifa_name);
152762607Sitojun		/* we are interested in multicast-capable interfaces */
152862607Sitojun		if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
152962607Sitojun			continue;
153062607Sitojun		if (!ifcp) {
153162607Sitojun			/* new interface */
153278064Sume			if ((ifcp = MALLOC(struct ifc)) == NULL) {
153362607Sitojun				fatal("malloc: struct ifc");
153478064Sume				/*NOTREACHED*/
153578064Sume			}
153662607Sitojun			memset(ifcp, 0, sizeof(*ifcp));
1537243232Shrs
153862607Sitojun			ifcp->ifc_index = -1;
1539243232Shrs			strlcpy(ifcp->ifc_name, ifa->ifa_name,
1540243232Shrs			    sizeof(ifcp->ifc_name));
1541243232Shrs			TAILQ_INIT(&ifcp->ifc_ifac_head);
1542243232Shrs			TAILQ_INIT(&ifcp->ifc_iff_head);
154362607Sitojun			ifcp->ifc_flags = ifa->ifa_flags;
1544243232Shrs			TAILQ_INSERT_HEAD(&ifc_head, ifcp, ifc_next);
154562607Sitojun			trace(1, "newif %s <%s>\n", ifcp->ifc_name,
154662607Sitojun				ifflags(ifcp->ifc_flags));
154762607Sitojun			if (!strcmp(ifcp->ifc_name, LOOPBACK_IF))
154862607Sitojun				loopifcp = ifcp;
154962607Sitojun		} else {
155062607Sitojun			/* update flag, this may be up again */
155162607Sitojun			if (ifcp->ifc_flags != ifa->ifa_flags) {
155262607Sitojun				trace(1, "%s: <%s> -> ", ifcp->ifc_name,
155362607Sitojun					ifflags(ifcp->ifc_flags));
155462607Sitojun				trace(1, "<%s>\n", ifflags(ifa->ifa_flags));
155578064Sume				ifcp->ifc_cflags |= IFC_CHANGED;
155662607Sitojun			}
155762607Sitojun			ifcp->ifc_flags = ifa->ifa_flags;
155862607Sitojun		}
1559243232Shrs		if (ifconfig1(ifa->ifa_name, ifa->ifa_addr, ifcp, s) < 0) {
1560243232Shrs			/* maybe temporary failure */
1561243232Shrs			continue;
1562243232Shrs		}
156362607Sitojun		if ((ifcp->ifc_flags & (IFF_LOOPBACK | IFF_UP)) == IFF_UP
156462607Sitojun		 && 0 < ifcp->ifc_index && !ifcp->ifc_joined) {
156562607Sitojun			mreq.ipv6mr_multiaddr = ifcp->ifc_ripsin.sin6_addr;
156662607Sitojun			mreq.ipv6mr_interface = ifcp->ifc_index;
156778064Sume			if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
156878064Sume			    &mreq, sizeof(mreq)) < 0) {
156962607Sitojun				fatal("IPV6_JOIN_GROUP");
157078064Sume				/*NOTREACHED*/
157178064Sume			}
157262607Sitojun			trace(1, "join %s %s\n", ifcp->ifc_name, RIP6_DEST);
157362607Sitojun			ifcp->ifc_joined++;
157462607Sitojun		}
157562607Sitojun	}
157662607Sitojun	close(s);
157762607Sitojun	freeifaddrs(ifap);
157855163Sshin}
157955163Sshin
1580243232Shrsint
1581243232Shrsifconfig1(const char *name,
1582243232Shrs	const struct sockaddr *sa,
1583243232Shrs	struct ifc *ifcp,
1584243232Shrs	int s)
158555163Sshin{
158655163Sshin	struct	in6_ifreq ifr;
1587119031Sume	const struct sockaddr_in6 *sin6;
1588243232Shrs	struct	ifac *ifac;
158955163Sshin	int	plen;
159055163Sshin	char	buf[BUFSIZ];
159155163Sshin
1592119031Sume	sin6 = (const struct sockaddr_in6 *)sa;
1593122677Sume	if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && !lflag)
1594243232Shrs		return (-1);
1595119031Sume	ifr.ifr_addr = *sin6;
1596119032Sume	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
159778064Sume	if (ioctl(s, SIOCGIFNETMASK_IN6, (char *)&ifr) < 0) {
1598243232Shrs		syslog(LOG_INFO, "ioctl: SIOCGIFNETMASK_IN6");
1599243232Shrs		return (-1);
160078064Sume	}
160178064Sume	plen = sin6mask2len(&ifr.ifr_addr);
1602243232Shrs	if ((ifac = ifa_match(ifcp, &sin6->sin6_addr, plen)) != NULL) {
160355163Sshin		/* same interface found */
160455163Sshin		/* need check if something changed */
160555163Sshin		/* XXX not yet implemented */
1606243232Shrs		return (-1);
160755163Sshin	}
160855163Sshin	/*
160955163Sshin	 * New address is found
161055163Sshin	 */
1611243232Shrs	if ((ifac = MALLOC(struct ifac)) == NULL) {
161255163Sshin		fatal("malloc: struct ifac");
161378064Sume		/*NOTREACHED*/
161478064Sume	}
1615243232Shrs	memset(ifac, 0, sizeof(*ifac));
1616243232Shrs
1617243232Shrs	ifac->ifac_ifc = ifcp;
1618243232Shrs	ifac->ifac_addr = sin6->sin6_addr;
1619243232Shrs	ifac->ifac_plen = plen;
1620243232Shrs	ifac->ifac_scope_id = sin6->sin6_scope_id;
162155163Sshin	if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1622119031Sume		ifr.ifr_addr = *sin6;
162378064Sume		if (ioctl(s, SIOCGIFDSTADDR_IN6, (char *)&ifr) < 0) {
162455163Sshin			fatal("ioctl: SIOCGIFDSTADDR_IN6");
162578064Sume			/*NOTREACHED*/
162678064Sume		}
1627243232Shrs		ifac->ifac_raddr = ifr.ifr_dstaddr.sin6_addr;
1628243232Shrs		inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr, buf,
1629243232Shrs		    sizeof(buf));
163055163Sshin		trace(1, "found address %s/%d -- %s\n",
1631243232Shrs			inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen, buf);
163255163Sshin	} else {
163355163Sshin		trace(1, "found address %s/%d\n",
1634243232Shrs			inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen);
163555163Sshin	}
1636243232Shrs	if (ifcp->ifc_index < 0 && IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
1637243232Shrs		ifcp->ifc_mylladdr = ifac->ifac_addr;
1638243232Shrs		ifcp->ifc_index = ifac->ifac_scope_id;
163955163Sshin		memcpy(&ifcp->ifc_ripsin, &ripsin, ripsin.ss_len);
1640243231Shrs		ifcp->ifc_ripsin.sin6_scope_id = ifcp->ifc_index;
164155163Sshin		setindex2ifc(ifcp->ifc_index, ifcp);
164255163Sshin		ifcp->ifc_mtu = getifmtu(ifcp->ifc_index);
164355163Sshin		if (ifcp->ifc_mtu > RIP6_MAXMTU)
164455163Sshin			ifcp->ifc_mtu = RIP6_MAXMTU;
164578064Sume		if (ioctl(s, SIOCGIFMETRIC, (char *)&ifr) < 0) {
164655163Sshin			fatal("ioctl: SIOCGIFMETRIC");
164778064Sume			/*NOTREACHED*/
164878064Sume		}
164955163Sshin		ifcp->ifc_metric = ifr.ifr_metric;
165055163Sshin		trace(1, "\tindex: %d, mtu: %d, metric: %d\n",
165155163Sshin			ifcp->ifc_index, ifcp->ifc_mtu, ifcp->ifc_metric);
165278064Sume	} else
165378064Sume		ifcp->ifc_cflags |= IFC_CHANGED;
1654243232Shrs
1655243232Shrs	TAILQ_INSERT_HEAD(&ifcp->ifc_ifac_head, ifac, ifac_next);
1656243232Shrs
1657243232Shrs	return 0;
165855163Sshin}
165955163Sshin
1660243233Shrsvoid
1661243233Shrsifremove(int ifindex)
1662243233Shrs{
1663243233Shrs	struct ifc *ifcp;
1664243233Shrs	struct riprt *rrt;
1665243233Shrs
1666243233Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
1667243233Shrs		if (ifcp->ifc_index == ifindex)
1668243233Shrs			break;
1669243233Shrs	}
1670243233Shrs	if (ifcp == NULL)
1671243233Shrs		return;
1672243233Shrs
1673243233Shrs	tracet(1, "ifremove: %s is departed.\n", ifcp->ifc_name);
1674243233Shrs	TAILQ_REMOVE(&ifc_head, ifcp, ifc_next);
1675243233Shrs
1676243233Shrs	TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1677243233Shrs		if (rrt->rrt_index == ifcp->ifc_index &&
1678243233Shrs		    rrt->rrt_rflags & RRTF_AGGREGATE)
1679243233Shrs			delroute(&rrt->rrt_info, &rrt->rrt_gw);
1680243233Shrs	}
1681243233Shrs	free(ifcp);
1682243233Shrs}
1683243233Shrs
168455163Sshin/*
168555163Sshin * Receive and process routing messages.
168655163Sshin * Update interface information as necesssary.
168755163Sshin */
168855163Sshinvoid
1689243232Shrsrtrecv(void)
169055163Sshin{
169155163Sshin	char buf[BUFSIZ];
1692243232Shrs	char *p, *q = NULL;
169355163Sshin	struct rt_msghdr *rtm;
169455163Sshin	struct ifa_msghdr *ifam;
169555163Sshin	struct if_msghdr *ifm;
1696243233Shrs	struct if_announcemsghdr *ifan;
169755163Sshin	int len;
169878064Sume	struct ifc *ifcp, *ic;
169955163Sshin	int iface = 0, rtable = 0;
170055163Sshin	struct sockaddr_in6 *rta[RTAX_MAX];
170178064Sume	struct sockaddr_in6 mask;
1702243232Shrs	int i, addrs = 0;
170378064Sume	struct riprt *rrt;
170455163Sshin
170555163Sshin	if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
170655163Sshin		perror("read from rtsock");
170778064Sume		exit(1);
170855163Sshin	}
1709243232Shrs	if (len == 0)
1710243232Shrs		return;
1711243232Shrs#if 0
171255163Sshin	if (len < sizeof(*rtm)) {
171369279Sume		trace(1, "short read from rtsock: %d (should be > %lu)\n",
171469279Sume			len, (u_long)sizeof(*rtm));
171555163Sshin		return;
171655163Sshin	}
1717243232Shrs#endif
1718119042Sume	if (dflag >= 2) {
1719119042Sume		fprintf(stderr, "rtmsg:\n");
1720119042Sume		for (i = 0; i < len; i++) {
1721119042Sume			fprintf(stderr, "%02x ", buf[i] & 0xff);
1722119042Sume			if (i % 16 == 15) fprintf(stderr, "\n");
1723119042Sume		}
1724119042Sume		fprintf(stderr, "\n");
1725119042Sume	}
172655163Sshin
172755163Sshin	for (p = buf; p - buf < len; p += ((struct rt_msghdr *)p)->rtm_msglen) {
1728243232Shrs		if (((struct rt_msghdr *)p)->rtm_version != RTM_VERSION)
1729243232Shrs			continue;
1730243232Shrs
173155163Sshin		/* safety against bogus message */
173255163Sshin		if (((struct rt_msghdr *)p)->rtm_msglen <= 0) {
173355163Sshin			trace(1, "bogus rtmsg: length=%d\n",
173455163Sshin				((struct rt_msghdr *)p)->rtm_msglen);
173555163Sshin			break;
173655163Sshin		}
173755163Sshin		rtm = NULL;
173855163Sshin		ifam = NULL;
173955163Sshin		ifm = NULL;
174055163Sshin		switch (((struct rt_msghdr *)p)->rtm_type) {
174155163Sshin		case RTM_NEWADDR:
174255163Sshin		case RTM_DELADDR:
174355163Sshin			ifam = (struct ifa_msghdr *)p;
174455163Sshin			addrs = ifam->ifam_addrs;
174555163Sshin			q = (char *)(ifam + 1);
174655163Sshin			break;
174755163Sshin		case RTM_IFINFO:
174855163Sshin			ifm = (struct if_msghdr *)p;
174955163Sshin			addrs = ifm->ifm_addrs;
175055163Sshin			q = (char *)(ifm + 1);
175155163Sshin			break;
1752243233Shrs		case RTM_IFANNOUNCE:
1753243233Shrs			ifan = (struct if_announcemsghdr *)p;
1754243233Shrs			switch (ifan->ifan_what) {
1755243233Shrs			case IFAN_ARRIVAL:
1756243233Shrs				iface++;
1757243233Shrs				break;
1758243233Shrs			case IFAN_DEPARTURE:
1759243233Shrs				ifremove(ifan->ifan_index);
1760243233Shrs				iface++;
1761243233Shrs				break;
1762243233Shrs			}
1763243233Shrs			break;
176455163Sshin		default:
176555163Sshin			rtm = (struct rt_msghdr *)p;
176655163Sshin			addrs = rtm->rtm_addrs;
176755163Sshin			q = (char *)(rtm + 1);
176855163Sshin			if (rtm->rtm_version != RTM_VERSION) {
176955163Sshin				trace(1, "unexpected rtmsg version %d "
177055163Sshin					"(should be %d)\n",
177155163Sshin					rtm->rtm_version, RTM_VERSION);
177255163Sshin				continue;
177355163Sshin			}
177455163Sshin			if (rtm->rtm_pid == pid) {
177555163Sshin#if 0
177655163Sshin				trace(1, "rtmsg looped back to me, ignored\n");
177755163Sshin#endif
177855163Sshin				continue;
177955163Sshin			}
178055163Sshin			break;
178155163Sshin		}
178255163Sshin		memset(&rta, 0, sizeof(rta));
178355163Sshin		for (i = 0; i < RTAX_MAX; i++) {
178455163Sshin			if (addrs & (1 << i)) {
178555163Sshin				rta[i] = (struct sockaddr_in6 *)q;
178655163Sshin				q += ROUNDUP(rta[i]->sin6_len);
178755163Sshin			}
178855163Sshin		}
178955163Sshin
179055163Sshin		trace(1, "rtsock: %s (addrs=%x)\n",
179155163Sshin			rttypes((struct rt_msghdr *)p), addrs);
179255163Sshin		if (dflag >= 2) {
179355163Sshin			for (i = 0;
179455163Sshin			     i < ((struct rt_msghdr *)p)->rtm_msglen;
179555163Sshin			     i++) {
179655163Sshin				fprintf(stderr, "%02x ", p[i] & 0xff);
179755163Sshin				if (i % 16 == 15) fprintf(stderr, "\n");
179855163Sshin			}
179955163Sshin			fprintf(stderr, "\n");
180055163Sshin		}
180155163Sshin
180255163Sshin		/*
180355163Sshin		 * Easy ones first.
180455163Sshin		 *
180555163Sshin		 * We may be able to optimize by using ifm->ifm_index or
180655163Sshin		 * ifam->ifam_index.  For simplicity we don't do that here.
180755163Sshin		 */
180855163Sshin		switch (((struct rt_msghdr *)p)->rtm_type) {
180955163Sshin		case RTM_NEWADDR:
181055163Sshin		case RTM_IFINFO:
181155163Sshin			iface++;
181255163Sshin			continue;
181355163Sshin		case RTM_ADD:
181455163Sshin			rtable++;
181555163Sshin			continue;
181655163Sshin		case RTM_LOSING:
181755163Sshin		case RTM_MISS:
181855163Sshin		case RTM_GET:
181955163Sshin		case RTM_LOCK:
182055163Sshin			/* nothing to be done here */
182155163Sshin			trace(1, "\tnothing to be done, ignored\n");
182255163Sshin			continue;
182355163Sshin		}
182455163Sshin
182555163Sshin#if 0
182655163Sshin		if (rta[RTAX_DST] == NULL) {
182755163Sshin			trace(1, "\tno destination, ignored\n");
182862607Sitojun			continue;
182955163Sshin		}
183055163Sshin		if (rta[RTAX_DST]->sin6_family != AF_INET6) {
183155163Sshin			trace(1, "\taf mismatch, ignored\n");
183255163Sshin			continue;
183355163Sshin		}
183455163Sshin		if (IN6_IS_ADDR_LINKLOCAL(&rta[RTAX_DST]->sin6_addr)) {
183555163Sshin			trace(1, "\tlinklocal destination, ignored\n");
183655163Sshin			continue;
183755163Sshin		}
183855163Sshin		if (IN6_ARE_ADDR_EQUAL(&rta[RTAX_DST]->sin6_addr, &in6addr_loopback)) {
183955163Sshin			trace(1, "\tloopback destination, ignored\n");
184055163Sshin			continue;		/* Loopback */
184155163Sshin		}
184255163Sshin		if (IN6_IS_ADDR_MULTICAST(&rta[RTAX_DST]->sin6_addr)) {
184355163Sshin			trace(1, "\tmulticast destination, ignored\n");
184455163Sshin			continue;
184555163Sshin		}
184655163Sshin#endif
184755163Sshin
184855163Sshin		/* hard ones */
184955163Sshin		switch (((struct rt_msghdr *)p)->rtm_type) {
185055163Sshin		case RTM_NEWADDR:
185155163Sshin		case RTM_IFINFO:
185255163Sshin		case RTM_ADD:
185355163Sshin		case RTM_LOSING:
185455163Sshin		case RTM_MISS:
185555163Sshin		case RTM_GET:
185655163Sshin		case RTM_LOCK:
185755163Sshin			/* should already be handled */
185855163Sshin			fatal("rtrecv: never reach here");
185978064Sume			/*NOTREACHED*/
186055163Sshin		case RTM_DELETE:
186178064Sume			if (!rta[RTAX_DST] || !rta[RTAX_GATEWAY]) {
186278064Sume				trace(1, "\tsome of dst/gw/netamsk are "
186378064Sume				    "unavailable, ignored\n");
186455163Sshin				break;
186555163Sshin			}
186678064Sume			if ((rtm->rtm_flags & RTF_HOST) != 0) {
186778064Sume				mask.sin6_len = sizeof(mask);
186878064Sume				memset(&mask.sin6_addr, 0xff,
186978064Sume				    sizeof(mask.sin6_addr));
187078064Sume				rta[RTAX_NETMASK] = &mask;
187178064Sume			} else if (!rta[RTAX_NETMASK]) {
187278064Sume				trace(1, "\tsome of dst/gw/netamsk are "
187378064Sume				    "unavailable, ignored\n");
187478064Sume				break;
187578064Sume			}
187678064Sume			if (rt_del(rta[RTAX_DST], rta[RTAX_GATEWAY],
187778064Sume			    rta[RTAX_NETMASK]) == 0) {
187855163Sshin				rtable++;	/*just to be sure*/
187955163Sshin			}
188055163Sshin			break;
188155163Sshin		case RTM_CHANGE:
188255163Sshin		case RTM_REDIRECT:
188355163Sshin			trace(1, "\tnot supported yet, ignored\n");
188455163Sshin			break;
188555163Sshin		case RTM_DELADDR:
188655163Sshin			if (!rta[RTAX_NETMASK] || !rta[RTAX_IFA]) {
188755163Sshin				trace(1, "\tno netmask or ifa given, ignored\n");
188855163Sshin				break;
188955163Sshin			}
189055163Sshin			if (ifam->ifam_index < nindex2ifc)
189155163Sshin				ifcp = index2ifc[ifam->ifam_index];
189255163Sshin			else
189355163Sshin				ifcp = NULL;
189455163Sshin			if (!ifcp) {
189555163Sshin				trace(1, "\tinvalid ifam_index %d, ignored\n",
189655163Sshin					ifam->ifam_index);
189755163Sshin				break;
189855163Sshin			}
189978064Sume			if (!rt_deladdr(ifcp, rta[RTAX_IFA], rta[RTAX_NETMASK]))
190078064Sume				iface++;
190155163Sshin			break;
190255163Sshin		}
190355163Sshin
190455163Sshin	}
190555163Sshin
190655163Sshin	if (iface) {
190755163Sshin		trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
190855163Sshin		ifconfig();
1909243232Shrs		TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
191078064Sume			if (ifcp->ifc_cflags & IFC_CHANGED) {
191178064Sume				if (ifrt(ifcp, 1)) {
1912243232Shrs					TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
191378064Sume						if (ifcp->ifc_index == ic->ifc_index)
191478064Sume							continue;
191578064Sume						if (ic->ifc_flags & IFF_UP)
191678064Sume							ripsend(ic, &ic->ifc_ripsin,
191778064Sume							RRTF_CHANGED);
191878064Sume					}
191978064Sume					/* Reset the flag */
1920243232Shrs					TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
192178064Sume						rrt->rrt_rflags &= ~RRTF_CHANGED;
1922243232Shrs					}
192378064Sume				}
192478064Sume				ifcp->ifc_cflags &= ~IFC_CHANGED;
192578064Sume			}
1926243232Shrs		}
192755163Sshin	}
192855163Sshin	if (rtable) {
192955163Sshin		trace(1, "rtsock: read routing table again\n");
193055163Sshin		krtread(1);
193155163Sshin	}
193255163Sshin}
193355163Sshin
193455163Sshin/*
193555163Sshin * remove specified route from the internal routing table.
193655163Sshin */
193755163Sshinint
1938243232Shrsrt_del(const struct sockaddr_in6 *sdst,
1939243232Shrs	const struct sockaddr_in6 *sgw,
1940243232Shrs	const struct sockaddr_in6 *smask)
194155163Sshin{
194255163Sshin	const struct in6_addr *dst = NULL;
194355163Sshin	const struct in6_addr *gw = NULL;
194455163Sshin	int prefix;
194555163Sshin	struct netinfo6 ni6;
194655163Sshin	struct riprt *rrt = NULL;
194755163Sshin	time_t t_lifetime;
194855163Sshin
194955163Sshin	if (sdst->sin6_family != AF_INET6) {
195055163Sshin		trace(1, "\tother AF, ignored\n");
195155163Sshin		return -1;
195255163Sshin	}
195355163Sshin	if (IN6_IS_ADDR_LINKLOCAL(&sdst->sin6_addr)
195455163Sshin	 || IN6_ARE_ADDR_EQUAL(&sdst->sin6_addr, &in6addr_loopback)
195555163Sshin	 || IN6_IS_ADDR_MULTICAST(&sdst->sin6_addr)) {
195655163Sshin		trace(1, "\taddress %s not interesting, ignored\n",
195755163Sshin			inet6_n2p(&sdst->sin6_addr));
195855163Sshin		return -1;
195955163Sshin	}
196055163Sshin	dst = &sdst->sin6_addr;
196178064Sume	if (sgw->sin6_family == AF_INET6) {
196255163Sshin		/* easy case */
196355163Sshin		gw = &sgw->sin6_addr;
196478064Sume		prefix = sin6mask2len(smask);
196555163Sshin	} else if (sgw->sin6_family == AF_LINK) {
196655163Sshin		/*
196755163Sshin		 * Interface route... a hard case.  We need to get the prefix
196855163Sshin		 * length from the kernel, but we now are parsing rtmsg.
196955163Sshin		 * We'll purge matching routes from my list, then get the
197055163Sshin		 * fresh list.
197155163Sshin		 */
197255163Sshin		struct riprt *longest;
1973108533Sschweikh		trace(1, "\t%s is an interface route, guessing prefixlen\n",
197455163Sshin			inet6_n2p(dst));
197555163Sshin		longest = NULL;
1976243232Shrs		TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
197755163Sshin			if (IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
197855163Sshin					&sdst->sin6_addr)
197955163Sshin			 && IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)) {
198055163Sshin				if (!longest
198155163Sshin				 || longest->rrt_info.rip6_plen <
198255163Sshin						 rrt->rrt_info.rip6_plen) {
198355163Sshin					longest = rrt;
198455163Sshin				}
198555163Sshin			}
198655163Sshin		}
198755163Sshin		rrt = longest;
198855163Sshin		if (!rrt) {
198955163Sshin			trace(1, "\tno matching interface route found\n");
199055163Sshin			return -1;
199155163Sshin		}
199255163Sshin		gw = &in6addr_loopback;
199355163Sshin		prefix = rrt->rrt_info.rip6_plen;
199455163Sshin	} else {
199578064Sume		trace(1, "\tunsupported af: (gw=%d)\n", sgw->sin6_family);
199655163Sshin		return -1;
199755163Sshin	}
199855163Sshin
199955163Sshin	trace(1, "\tdeleting %s/%d ", inet6_n2p(dst), prefix);
200055163Sshin	trace(1, "gw %s\n", inet6_n2p(gw));
200155163Sshin	t_lifetime = time(NULL) - RIP_LIFETIME;
200255163Sshin	/* age route for interface address */
200355163Sshin	memset(&ni6, 0, sizeof(ni6));
200455163Sshin	ni6.rip6_dest = *dst;
200555163Sshin	ni6.rip6_plen = prefix;
200655163Sshin	applyplen(&ni6.rip6_dest, ni6.rip6_plen);	/*to be sure*/
200755163Sshin	trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6.rip6_dest),
200855163Sshin		ni6.rip6_plen);
2009243232Shrs	if (!rrt && (rrt = rtsearch(&ni6)) == NULL) {
201055163Sshin		trace(1, "\tno route found\n");
201155163Sshin		return -1;
201255163Sshin	}
201378064Sume#if 0
201455163Sshin	if ((rrt->rrt_flags & RTF_STATIC) == 0) {
201555163Sshin		trace(1, "\tyou can delete static routes only\n");
201678064Sume	} else
201778064Sume#endif
201878064Sume	if (!IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, gw)) {
201955163Sshin		trace(1, "\tgw mismatch: %s <-> ",
202055163Sshin			inet6_n2p(&rrt->rrt_gw));
202155163Sshin		trace(1, "%s\n", inet6_n2p(gw));
202255163Sshin	} else {
202355163Sshin		trace(1, "\troute found, age it\n");
202455163Sshin		if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
202555163Sshin			rrt->rrt_t = t_lifetime;
202655163Sshin			rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
202755163Sshin		}
202855163Sshin	}
202955163Sshin	return 0;
203055163Sshin}
203155163Sshin
203255163Sshin/*
203355163Sshin * remove specified address from internal interface/routing table.
203455163Sshin */
203555163Sshinint
2036243232Shrsrt_deladdr(struct ifc *ifcp,
2037243232Shrs	const struct sockaddr_in6 *sifa,
2038243232Shrs	const struct sockaddr_in6 *smask)
203955163Sshin{
204055163Sshin	const struct in6_addr *addr = NULL;
204155163Sshin	int prefix;
2042243232Shrs	struct ifac *ifac = NULL;
204355163Sshin	struct netinfo6 ni6;
204455163Sshin	struct riprt *rrt = NULL;
204555163Sshin	time_t t_lifetime;
204655163Sshin	int updated = 0;
204755163Sshin
204878064Sume	if (sifa->sin6_family != AF_INET6) {
204955163Sshin		trace(1, "\tother AF, ignored\n");
205055163Sshin		return -1;
205155163Sshin	}
205255163Sshin	addr = &sifa->sin6_addr;
205378064Sume	prefix = sin6mask2len(smask);
205455163Sshin
205555163Sshin	trace(1, "\tdeleting %s/%d from %s\n",
205655163Sshin		inet6_n2p(addr), prefix, ifcp->ifc_name);
2057243232Shrs	ifac = ifa_match(ifcp, addr, prefix);
2058243232Shrs	if (!ifac) {
205955163Sshin		trace(1, "\tno matching ifa found for %s/%d on %s\n",
206055163Sshin			inet6_n2p(addr), prefix, ifcp->ifc_name);
206155163Sshin		return -1;
206255163Sshin	}
2063243232Shrs	if (ifac->ifac_ifc != ifcp) {
206455163Sshin		trace(1, "\taddress table corrupt: back pointer does not match "
206555163Sshin			"(%s != %s)\n",
2066243232Shrs			ifcp->ifc_name, ifac->ifac_ifc->ifc_name);
206755163Sshin		return -1;
206855163Sshin	}
2069243232Shrs	TAILQ_REMOVE(&ifcp->ifc_ifac_head, ifac, ifac_next);
207055163Sshin	t_lifetime = time(NULL) - RIP_LIFETIME;
207155163Sshin	/* age route for interface address */
207255163Sshin	memset(&ni6, 0, sizeof(ni6));
2073243232Shrs	ni6.rip6_dest = ifac->ifac_addr;
2074243232Shrs	ni6.rip6_plen = ifac->ifac_plen;
207555163Sshin	applyplen(&ni6.rip6_dest, ni6.rip6_plen);
207655163Sshin	trace(1, "\tfind interface route %s/%d on %d\n",
207755163Sshin		inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen, ifcp->ifc_index);
2078243232Shrs	if ((rrt = rtsearch(&ni6)) != NULL) {
207955163Sshin		struct in6_addr none;
208055163Sshin		memset(&none, 0, sizeof(none));
208178064Sume		if (rrt->rrt_index == ifcp->ifc_index &&
208278064Sume		    (IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &none) ||
208378064Sume		     IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw))) {
208455163Sshin			trace(1, "\troute found, age it\n");
208555163Sshin			if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
208655163Sshin				rrt->rrt_t = t_lifetime;
208755163Sshin				rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
208855163Sshin			}
208955163Sshin			updated++;
209055163Sshin		} else {
209155163Sshin			trace(1, "\tnon-interface route found: %s/%d on %d\n",
209255163Sshin				inet6_n2p(&rrt->rrt_info.rip6_dest),
209355163Sshin				rrt->rrt_info.rip6_plen,
209455163Sshin				rrt->rrt_index);
209555163Sshin		}
209655163Sshin	} else
209755163Sshin		trace(1, "\tno interface route found\n");
209855163Sshin	/* age route for p2p destination */
209955163Sshin	if (ifcp->ifc_flags & IFF_POINTOPOINT) {
210055163Sshin		memset(&ni6, 0, sizeof(ni6));
2101243232Shrs		ni6.rip6_dest = ifac->ifac_raddr;
210255163Sshin		ni6.rip6_plen = 128;
210355163Sshin		applyplen(&ni6.rip6_dest, ni6.rip6_plen);	/*to be sure*/
210455163Sshin		trace(1, "\tfind p2p route %s/%d on %d\n",
210555163Sshin			inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen,
210655163Sshin			ifcp->ifc_index);
2107243232Shrs		if ((rrt = rtsearch(&ni6)) != NULL) {
210878064Sume			if (rrt->rrt_index == ifcp->ifc_index &&
2109243232Shrs			    IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw,
2110243232Shrs			    &ifac->ifac_addr)) {
211155163Sshin				trace(1, "\troute found, age it\n");
211255163Sshin				if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
211355163Sshin					rrt->rrt_t = t_lifetime;
211455163Sshin					rrt->rrt_info.rip6_metric =
211578064Sume					    HOPCNT_INFINITY6;
211655163Sshin					updated++;
211755163Sshin				}
211855163Sshin			} else {
211955163Sshin				trace(1, "\tnon-p2p route found: %s/%d on %d\n",
212055163Sshin					inet6_n2p(&rrt->rrt_info.rip6_dest),
212155163Sshin					rrt->rrt_info.rip6_plen,
212255163Sshin					rrt->rrt_index);
212355163Sshin			}
212455163Sshin		} else
212555163Sshin			trace(1, "\tno p2p route found\n");
212655163Sshin	}
2127243232Shrs	free(ifac);
2128243232Shrs
2129243232Shrs	return ((updated) ? 0 : -1);
213055163Sshin}
213155163Sshin
213255163Sshin/*
213355163Sshin * Get each interface address and put those interface routes to the route
213455163Sshin * list.
213555163Sshin */
213678064Sumeint
2137243232Shrsifrt(struct ifc *ifcp, int again)
213855163Sshin{
2139243232Shrs	struct ifac *ifac;
2140243232Shrs	struct riprt *rrt = NULL, *search_rrt, *loop_rrt;
214162607Sitojun	struct netinfo6 *np;
214278064Sume	time_t t_lifetime;
214378064Sume	int need_trigger = 0;
214455163Sshin
2145122677Sume#if 0
214655163Sshin	if (ifcp->ifc_flags & IFF_LOOPBACK)
214778064Sume		return 0;			/* ignore loopback */
2148122677Sume#endif
2149122677Sume
215062607Sitojun	if (ifcp->ifc_flags & IFF_POINTOPOINT) {
215162607Sitojun		ifrt_p2p(ifcp, again);
215278064Sume		return 0;
215362607Sitojun	}
215462607Sitojun
2155243232Shrs	TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2156243232Shrs		if (IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
215762607Sitojun#if 0
215862607Sitojun			trace(1, "route: %s on %s: "
215962607Sitojun			    "skip linklocal interface address\n",
2160243232Shrs			    inet6_n2p(&ifac->ifac_addr), ifcp->ifc_name);
216162607Sitojun#endif
216262607Sitojun			continue;
216362607Sitojun		}
2164243232Shrs		if (IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_addr)) {
216562607Sitojun#if 0
216662607Sitojun			trace(1, "route: %s: skip unspec interface address\n",
216762607Sitojun			    ifcp->ifc_name);
216862607Sitojun#endif
216962607Sitojun			continue;
217062607Sitojun		}
2171243232Shrs		if (IN6_IS_ADDR_LOOPBACK(&ifac->ifac_addr)) {
2172122677Sume#if 0
2173122677Sume			trace(1, "route: %s: skip loopback address\n",
2174122677Sume			    ifcp->ifc_name);
2175122677Sume#endif
2176122677Sume			continue;
2177122677Sume		}
217878064Sume		if (ifcp->ifc_flags & IFF_UP) {
217978064Sume			if ((rrt = MALLOC(struct riprt)) == NULL)
218078064Sume				fatal("malloc: struct riprt");
218178064Sume			memset(rrt, 0, sizeof(*rrt));
218278064Sume			rrt->rrt_same = NULL;
218378064Sume			rrt->rrt_index = ifcp->ifc_index;
218478064Sume			rrt->rrt_t = 0;	/* don't age */
2185243232Shrs			rrt->rrt_info.rip6_dest = ifac->ifac_addr;
218678064Sume			rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
218778064Sume			rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2188243232Shrs			rrt->rrt_info.rip6_plen = ifac->ifac_plen;
2189186119Sqingli			rrt->rrt_flags = RTF_HOST;
219078064Sume			rrt->rrt_rflags |= RRTF_CHANGED;
2191243232Shrs			applyplen(&rrt->rrt_info.rip6_dest, ifac->ifac_plen);
219278064Sume			memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2193243232Shrs			rrt->rrt_gw = ifac->ifac_addr;
219478064Sume			np = &rrt->rrt_info;
2195243232Shrs			search_rrt = rtsearch(np);
219678064Sume			if (search_rrt != NULL) {
2197119042Sume				if (search_rrt->rrt_info.rip6_metric <=
219878064Sume				    rrt->rrt_info.rip6_metric) {
219978064Sume					/* Already have better route */
220078064Sume					if (!again) {
220178064Sume						trace(1, "route: %s/%d: "
220278064Sume						    "already registered (%s)\n",
220378064Sume						    inet6_n2p(&np->rip6_dest), np->rip6_plen,
220478064Sume						    ifcp->ifc_name);
220578064Sume					}
2206119042Sume					goto next;
220778064Sume				}
2208119042Sume
2209243232Shrs				TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
2210119042Sume				delroute(&rrt->rrt_info, &rrt->rrt_gw);
221178064Sume			}
221255163Sshin			/* Attach the route to the list */
221362607Sitojun			trace(1, "route: %s/%d: register route (%s)\n",
221462607Sitojun			    inet6_n2p(&np->rip6_dest), np->rip6_plen,
221562607Sitojun			    ifcp->ifc_name);
2216243232Shrs			TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
221778064Sume			addroute(rrt, &rrt->rrt_gw, ifcp);
2218119042Sume			rrt = NULL;
221978064Sume			sendrequest(ifcp);
222078064Sume			ripsend(ifcp, &ifcp->ifc_ripsin, 0);
222178064Sume			need_trigger = 1;
222255163Sshin		} else {
2223243232Shrs			TAILQ_FOREACH(loop_rrt, &riprt_head, rrt_next) {
222478064Sume				if (loop_rrt->rrt_index == ifcp->ifc_index) {
222578064Sume					t_lifetime = time(NULL) - RIP_LIFETIME;
222678064Sume					if (loop_rrt->rrt_t == 0 || loop_rrt->rrt_t > t_lifetime) {
222778064Sume						loop_rrt->rrt_t = t_lifetime;
222878064Sume						loop_rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
222978064Sume						loop_rrt->rrt_rflags |= RRTF_CHANGED;
223078064Sume						need_trigger = 1;
223178064Sume					}
223278064Sume				}
223355163Sshin			}
223478064Sume                }
2235119042Sume	next:
2236119042Sume		if (rrt)
2237119042Sume			free(rrt);
223862607Sitojun	}
223978064Sume	return need_trigger;
224062607Sitojun}
224155163Sshin
224262607Sitojun/*
224362607Sitojun * there are couple of p2p interface routing models.  "behavior" lets
224462607Sitojun * you pick one.  it looks that gated behavior fits best with BSDs,
2245119035Sume * since BSD kernels do not look at prefix length on p2p interfaces.
224662607Sitojun */
224762607Sitojunvoid
2248243232Shrsifrt_p2p(struct ifc *ifcp, int again)
224962607Sitojun{
2250243232Shrs	struct ifac *ifac;
2251243232Shrs	struct riprt *rrt, *orrt;
225262607Sitojun	struct netinfo6 *np;
225362607Sitojun	struct in6_addr addr, dest;
225462607Sitojun	int advert, ignore, i;
225562607Sitojun#define P2PADVERT_NETWORK	1
225662607Sitojun#define P2PADVERT_ADDR		2
225762607Sitojun#define P2PADVERT_DEST		4
225862607Sitojun#define P2PADVERT_MAX		4
225962607Sitojun	const enum { CISCO, GATED, ROUTE6D } behavior = GATED;
226078064Sume	const char *category = "";
226162607Sitojun	const char *noadv;
226262607Sitojun
2263243232Shrs	TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2264243232Shrs		addr = ifac->ifac_addr;
2265243232Shrs		dest = ifac->ifac_raddr;
2266243232Shrs		applyplen(&addr, ifac->ifac_plen);
2267243232Shrs		applyplen(&dest, ifac->ifac_plen);
226862607Sitojun		advert = ignore = 0;
226962607Sitojun		switch (behavior) {
227062607Sitojun		case CISCO:
227162607Sitojun			/*
227262607Sitojun			 * honor addr/plen, just like normal shared medium
227362607Sitojun			 * interface.  this may cause trouble if you reuse
227462607Sitojun			 * addr/plen on other interfaces.
227562607Sitojun			 *
227662607Sitojun			 * advertise addr/plen.
227762607Sitojun			 */
227862607Sitojun			advert |= P2PADVERT_NETWORK;
227962607Sitojun			break;
228062607Sitojun		case GATED:
228162607Sitojun			/*
228262607Sitojun			 * prefixlen on p2p interface is meaningless.
228362607Sitojun			 * advertise addr/128 and dest/128.
228462607Sitojun			 *
228562607Sitojun			 * do not install network route to route6d routing
228662607Sitojun			 * table (if we do, it would prevent route installation
228762607Sitojun			 * for other p2p interface that shares addr/plen).
228878064Sume			 *
228978064Sume			 * XXX what should we do if dest is ::?  it will not
229078064Sume			 * get announced anyways (see following filter),
229178064Sume			 * but we need to think.
229262607Sitojun			 */
229362607Sitojun			advert |= P2PADVERT_ADDR;
229462607Sitojun			advert |= P2PADVERT_DEST;
229562607Sitojun			ignore |= P2PADVERT_NETWORK;
229662607Sitojun			break;
229762607Sitojun		case ROUTE6D:
229862607Sitojun			/*
229978064Sume			 * just for testing.  actually the code is redundant
230078064Sume			 * given the current p2p interface address assignment
230178064Sume			 * rule for kame kernel.
230278064Sume			 *
230378064Sume			 * intent:
230478064Sume			 *	A/n -> announce A/n
230578064Sume			 *	A B/n, A and B share prefix -> A/n (= B/n)
230678064Sume			 *	A B/n, do not share prefix -> A/128 and B/128
230778064Sume			 * actually, A/64 and A B/128 are the only cases
230878064Sume			 * permitted by the kernel:
230978064Sume			 *	A/64 -> A/64
231078064Sume			 *	A B/128 -> A/128 and B/128
231162607Sitojun			 */
2312243232Shrs			if (!IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_raddr)) {
231378064Sume				if (IN6_ARE_ADDR_EQUAL(&addr, &dest))
231478064Sume					advert |= P2PADVERT_NETWORK;
231578064Sume				else {
231678064Sume					advert |= P2PADVERT_ADDR;
231778064Sume					advert |= P2PADVERT_DEST;
231878064Sume					ignore |= P2PADVERT_NETWORK;
231978064Sume				}
232078064Sume			} else
232162607Sitojun				advert |= P2PADVERT_NETWORK;
232262607Sitojun			break;
232362607Sitojun		}
232462607Sitojun
232562607Sitojun		for (i = 1; i <= P2PADVERT_MAX; i *= 2) {
232662607Sitojun			if ((ignore & i) != 0)
232762607Sitojun				continue;
232878064Sume			if ((rrt = MALLOC(struct riprt)) == NULL) {
232955163Sshin				fatal("malloc: struct riprt");
233078064Sume				/*NOTREACHED*/
233178064Sume			}
233262607Sitojun			memset(rrt, 0, sizeof(*rrt));
233355163Sshin			rrt->rrt_same = NULL;
233455163Sshin			rrt->rrt_index = ifcp->ifc_index;
233562607Sitojun			rrt->rrt_t = 0;	/* don't age */
233662607Sitojun			switch (i) {
233762607Sitojun			case P2PADVERT_NETWORK:
2338243232Shrs				rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2339243232Shrs				rrt->rrt_info.rip6_plen = ifac->ifac_plen;
234062607Sitojun				applyplen(&rrt->rrt_info.rip6_dest,
2341243232Shrs				    ifac->ifac_plen);
234262607Sitojun				category = "network";
234362607Sitojun				break;
234462607Sitojun			case P2PADVERT_ADDR:
2345243232Shrs				rrt->rrt_info.rip6_dest = ifac->ifac_addr;
234662607Sitojun				rrt->rrt_info.rip6_plen = 128;
234778064Sume				rrt->rrt_gw = in6addr_loopback;
234862607Sitojun				category = "addr";
234962607Sitojun				break;
235062607Sitojun			case P2PADVERT_DEST:
2351243232Shrs				rrt->rrt_info.rip6_dest = ifac->ifac_raddr;
235262607Sitojun				rrt->rrt_info.rip6_plen = 128;
2353243232Shrs				rrt->rrt_gw = ifac->ifac_addr;
235462607Sitojun				category = "dest";
235562607Sitojun				break;
235662607Sitojun			}
235762607Sitojun			if (IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_info.rip6_dest) ||
235862607Sitojun			    IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_info.rip6_dest)) {
235962607Sitojun#if 0
236062607Sitojun				trace(1, "route: %s: skip unspec/linklocal "
236162607Sitojun				    "(%s on %s)\n", category, ifcp->ifc_name);
236262607Sitojun#endif
236362607Sitojun				free(rrt);
236462607Sitojun				continue;
236562607Sitojun			}
236662607Sitojun			if ((advert & i) == 0) {
236762607Sitojun				rrt->rrt_rflags |= RRTF_NOADVERTISE;
236862607Sitojun				noadv = ", NO-ADV";
236962607Sitojun			} else
237062607Sitojun				noadv = "";
237155163Sshin			rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
237262607Sitojun			rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
237355163Sshin			np = &rrt->rrt_info;
2374243232Shrs			orrt = rtsearch(np);
237578064Sume			if (!orrt) {
237655163Sshin				/* Attach the route to the list */
237762607Sitojun				trace(1, "route: %s/%d: register route "
237862607Sitojun				    "(%s on %s%s)\n",
237962607Sitojun				    inet6_n2p(&np->rip6_dest), np->rip6_plen,
238062607Sitojun				    category, ifcp->ifc_name, noadv);
2381243232Shrs				TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
238278064Sume			} else if (rrt->rrt_index != orrt->rrt_index ||
238378064Sume			    rrt->rrt_info.rip6_metric != orrt->rrt_info.rip6_metric) {
2384243232Shrs				/* replace route */
2385243232Shrs				TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2386243232Shrs				TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
238778064Sume				free(orrt);
238878064Sume
238978064Sume				trace(1, "route: %s/%d: update (%s on %s%s)\n",
239078064Sume				    inet6_n2p(&np->rip6_dest), np->rip6_plen,
239178064Sume				    category, ifcp->ifc_name, noadv);
239255163Sshin			} else {
239355163Sshin				/* Already found */
239455163Sshin				if (!again) {
239562607Sitojun					trace(1, "route: %s/%d: "
239662607Sitojun					    "already registered (%s on %s%s)\n",
239762607Sitojun					    inet6_n2p(&np->rip6_dest),
239862607Sitojun					    np->rip6_plen, category,
239962607Sitojun					    ifcp->ifc_name, noadv);
240055163Sshin				}
240155163Sshin				free(rrt);
240255163Sshin			}
240355163Sshin		}
240455163Sshin	}
240562607Sitojun#undef P2PADVERT_NETWORK
240662607Sitojun#undef P2PADVERT_ADDR
240762607Sitojun#undef P2PADVERT_DEST
240862607Sitojun#undef P2PADVERT_MAX
240955163Sshin}
241055163Sshin
241155163Sshinint
2412243232Shrsgetifmtu(int ifindex)
241355163Sshin{
241455163Sshin	int	mib[6];
241555163Sshin	char	*buf;
241655163Sshin	size_t	msize;
241755163Sshin	struct	if_msghdr *ifm;
241855163Sshin	int	mtu;
241955163Sshin
242055163Sshin	mib[0] = CTL_NET;
242155163Sshin	mib[1] = PF_ROUTE;
242255163Sshin	mib[2] = 0;
242355163Sshin	mib[3] = AF_INET6;
242455163Sshin	mib[4] = NET_RT_IFLIST;
242555163Sshin	mib[5] = ifindex;
242678064Sume	if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
242755163Sshin		fatal("sysctl estimate NET_RT_IFLIST");
242878064Sume		/*NOTREACHED*/
242978064Sume	}
243078064Sume	if ((buf = malloc(msize)) == NULL) {
243155163Sshin		fatal("malloc");
243278064Sume		/*NOTREACHED*/
243378064Sume	}
243478064Sume	if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
243555163Sshin		fatal("sysctl NET_RT_IFLIST");
243678064Sume		/*NOTREACHED*/
243778064Sume	}
243855163Sshin	ifm = (struct if_msghdr *)buf;
243955163Sshin	mtu = ifm->ifm_data.ifi_mtu;
244078064Sume	if (ifindex != ifm->ifm_index) {
244155163Sshin		fatal("ifindex does not match with ifm_index");
244278064Sume		/*NOTREACHED*/
244378064Sume	}
244455163Sshin	free(buf);
244555163Sshin	return mtu;
244655163Sshin}
244755163Sshin
244855163Sshinconst char *
2449243232Shrsrttypes(struct rt_msghdr *rtm)
245055163Sshin{
245162607Sitojun#define	RTTYPE(s, f) \
245262607Sitojundo { \
245362607Sitojun	if (rtm->rtm_type == (f)) \
245462607Sitojun		return (s); \
245562607Sitojun} while (0)
245655163Sshin	RTTYPE("ADD", RTM_ADD);
245755163Sshin	RTTYPE("DELETE", RTM_DELETE);
245855163Sshin	RTTYPE("CHANGE", RTM_CHANGE);
245955163Sshin	RTTYPE("GET", RTM_GET);
246055163Sshin	RTTYPE("LOSING", RTM_LOSING);
246155163Sshin	RTTYPE("REDIRECT", RTM_REDIRECT);
246255163Sshin	RTTYPE("MISS", RTM_MISS);
246355163Sshin	RTTYPE("LOCK", RTM_LOCK);
246455163Sshin	RTTYPE("NEWADDR", RTM_NEWADDR);
246555163Sshin	RTTYPE("DELADDR", RTM_DELADDR);
246655163Sshin	RTTYPE("IFINFO", RTM_IFINFO);
246778064Sume#ifdef RTM_OIFINFO
246878064Sume	RTTYPE("OIFINFO", RTM_OIFINFO);
246978064Sume#endif
247078064Sume#ifdef RTM_IFANNOUNCE
247178064Sume	RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE);
247278064Sume#endif
247378064Sume#ifdef RTM_NEWMADDR
247478064Sume	RTTYPE("NEWMADDR", RTM_NEWMADDR);
247578064Sume#endif
247678064Sume#ifdef RTM_DELMADDR
247778064Sume	RTTYPE("DELMADDR", RTM_DELMADDR);
247878064Sume#endif
247955163Sshin#undef RTTYPE
248055163Sshin	return NULL;
248155163Sshin}
248255163Sshin
248355163Sshinconst char *
2484243232Shrsrtflags(struct rt_msghdr *rtm)
248555163Sshin{
248655163Sshin	static char buf[BUFSIZ];
248755163Sshin
248878064Sume	/*
248978064Sume	 * letter conflict should be okay.  painful when *BSD diverges...
249078064Sume	 */
249178064Sume	strlcpy(buf, "", sizeof(buf));
249262607Sitojun#define	RTFLAG(s, f) \
249362607Sitojundo { \
249462607Sitojun	if (rtm->rtm_flags & (f)) \
249578064Sume		strlcat(buf, (s), sizeof(buf)); \
249662607Sitojun} while (0)
249755163Sshin	RTFLAG("U", RTF_UP);
249855163Sshin	RTFLAG("G", RTF_GATEWAY);
249955163Sshin	RTFLAG("H", RTF_HOST);
250055163Sshin	RTFLAG("R", RTF_REJECT);
250155163Sshin	RTFLAG("D", RTF_DYNAMIC);
250255163Sshin	RTFLAG("M", RTF_MODIFIED);
250355163Sshin	RTFLAG("d", RTF_DONE);
250455163Sshin#ifdef	RTF_MASK
250555163Sshin	RTFLAG("m", RTF_MASK);
250655163Sshin#endif
250778064Sume#ifdef RTF_CLONED
250878064Sume	RTFLAG("c", RTF_CLONED);
250978064Sume#endif
251055163Sshin	RTFLAG("X", RTF_XRESOLVE);
2511186119Sqingli#ifdef RTF_LLINFO
251255163Sshin	RTFLAG("L", RTF_LLINFO);
2513186119Sqingli#endif
251455163Sshin	RTFLAG("S", RTF_STATIC);
251555163Sshin	RTFLAG("B", RTF_BLACKHOLE);
251678064Sume#ifdef RTF_PROTO3
251778064Sume	RTFLAG("3", RTF_PROTO3);
251878064Sume#endif
251955163Sshin	RTFLAG("2", RTF_PROTO2);
252055163Sshin	RTFLAG("1", RTF_PROTO1);
252178064Sume#ifdef RTF_BROADCAST
252278064Sume	RTFLAG("b", RTF_BROADCAST);
252378064Sume#endif
252478064Sume#ifdef RTF_DEFAULT
252578064Sume	RTFLAG("d", RTF_DEFAULT);
252678064Sume#endif
252778064Sume#ifdef RTF_ISAROUTER
252878064Sume	RTFLAG("r", RTF_ISAROUTER);
252978064Sume#endif
253078064Sume#ifdef RTF_TUNNEL
253178064Sume	RTFLAG("T", RTF_TUNNEL);
253278064Sume#endif
253378064Sume#ifdef RTF_AUTH
253478064Sume	RTFLAG("A", RTF_AUTH);
253578064Sume#endif
253678064Sume#ifdef RTF_CRYPT
253778064Sume	RTFLAG("E", RTF_CRYPT);
253878064Sume#endif
253955163Sshin#undef RTFLAG
254055163Sshin	return buf;
254155163Sshin}
254255163Sshin
254355163Sshinconst char *
2544243232Shrsifflags(int flags)
254555163Sshin{
254655163Sshin	static char buf[BUFSIZ];
254755163Sshin
254878064Sume	strlcpy(buf, "", sizeof(buf));
254962607Sitojun#define	IFFLAG(s, f) \
255062607Sitojundo { \
2551119040Sume	if (flags & (f)) { \
255262607Sitojun		if (buf[0]) \
255378064Sume			strlcat(buf, ",", sizeof(buf)); \
2554119040Sume		strlcat(buf, (s), sizeof(buf)); \
255562607Sitojun	} \
255662607Sitojun} while (0)
255755163Sshin	IFFLAG("UP", IFF_UP);
255855163Sshin	IFFLAG("BROADCAST", IFF_BROADCAST);
255955163Sshin	IFFLAG("DEBUG", IFF_DEBUG);
256055163Sshin	IFFLAG("LOOPBACK", IFF_LOOPBACK);
256155163Sshin	IFFLAG("POINTOPOINT", IFF_POINTOPOINT);
256255163Sshin#ifdef IFF_NOTRAILERS
256355163Sshin	IFFLAG("NOTRAILERS", IFF_NOTRAILERS);
256455163Sshin#endif
256578064Sume#ifdef IFF_SMART
256678064Sume	IFFLAG("SMART", IFF_SMART);
256778064Sume#endif
256855163Sshin	IFFLAG("RUNNING", IFF_RUNNING);
256955163Sshin	IFFLAG("NOARP", IFF_NOARP);
257055163Sshin	IFFLAG("PROMISC", IFF_PROMISC);
257155163Sshin	IFFLAG("ALLMULTI", IFF_ALLMULTI);
257255163Sshin	IFFLAG("OACTIVE", IFF_OACTIVE);
257355163Sshin	IFFLAG("SIMPLEX", IFF_SIMPLEX);
257455163Sshin	IFFLAG("LINK0", IFF_LINK0);
257555163Sshin	IFFLAG("LINK1", IFF_LINK1);
257655163Sshin	IFFLAG("LINK2", IFF_LINK2);
257755163Sshin	IFFLAG("MULTICAST", IFF_MULTICAST);
257855163Sshin#undef IFFLAG
257955163Sshin	return buf;
258055163Sshin}
258155163Sshin
258255163Sshinvoid
2583243232Shrskrtread(int again)
258455163Sshin{
258555163Sshin	int mib[6];
258655163Sshin	size_t msize;
258755163Sshin	char *buf, *p, *lim;
258855163Sshin	struct rt_msghdr *rtm;
258955163Sshin	int retry;
259055163Sshin	const char *errmsg;
259155163Sshin
259255163Sshin	retry = 0;
259355163Sshin	buf = NULL;
259455163Sshin	mib[0] = CTL_NET;
259555163Sshin	mib[1] = PF_ROUTE;
259655163Sshin	mib[2] = 0;
259755163Sshin	mib[3] = AF_INET6;	/* Address family */
259855163Sshin	mib[4] = NET_RT_DUMP;	/* Dump the kernel routing table */
259955163Sshin	mib[5] = 0;		/* No flags */
260055163Sshin	do {
2601243232Shrs		if (retry)
2602243232Shrs			sleep(1);
260355163Sshin		retry++;
260455163Sshin		errmsg = NULL;
260555163Sshin		if (buf)
260655163Sshin			free(buf);
260755163Sshin		if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
260855163Sshin			errmsg = "sysctl estimate";
260955163Sshin			continue;
261055163Sshin		}
261155163Sshin		if ((buf = malloc(msize)) == NULL) {
261255163Sshin			errmsg = "malloc";
261355163Sshin			continue;
261455163Sshin		}
261555163Sshin		if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
261655163Sshin			errmsg = "sysctl NET_RT_DUMP";
261755163Sshin			continue;
261855163Sshin		}
2619243233Shrs	} while (retry < RT_DUMP_MAXRETRY && errmsg != NULL);
262078064Sume	if (errmsg) {
262169279Sume		fatal("%s (with %d retries, msize=%lu)", errmsg, retry,
262269279Sume		    (u_long)msize);
262378064Sume		/*NOTREACHED*/
262478064Sume	} else if (1 < retry)
262555163Sshin		syslog(LOG_INFO, "NET_RT_DUMP %d retires", retry);
262655163Sshin
262755163Sshin	lim = buf + msize;
262855163Sshin	for (p = buf; p < lim; p += rtm->rtm_msglen) {
262955163Sshin		rtm = (struct rt_msghdr *)p;
263055163Sshin		rt_entry(rtm, again);
263155163Sshin	}
263255163Sshin	free(buf);
263355163Sshin}
263455163Sshin
263555163Sshinvoid
2636243232Shrsrt_entry(struct rt_msghdr *rtm, int again)
263755163Sshin{
263855163Sshin	struct	sockaddr_in6 *sin6_dst, *sin6_gw, *sin6_mask;
263955163Sshin	struct	sockaddr_in6 *sin6_genmask, *sin6_ifp;
264055163Sshin	char	*rtmp, *ifname = NULL;
264178064Sume	struct	riprt *rrt, *orrt;
264255163Sshin	struct	netinfo6 *np;
2643243232Shrs	int ifindex;
264455163Sshin
264555163Sshin	sin6_dst = sin6_gw = sin6_mask = sin6_genmask = sin6_ifp = 0;
264655163Sshin	if ((rtm->rtm_flags & RTF_UP) == 0 || rtm->rtm_flags &
2647186119Sqingli		(RTF_XRESOLVE|RTF_BLACKHOLE)) {
264855163Sshin		return;		/* not interested in the link route */
264962607Sitojun	}
265069279Sume	/* do not look at cloned routes */
265169279Sume#ifdef RTF_WASCLONED
265269279Sume	if (rtm->rtm_flags & RTF_WASCLONED)
265369279Sume		return;
265469279Sume#endif
265569279Sume#ifdef RTF_CLONED
265669279Sume	if (rtm->rtm_flags & RTF_CLONED)
265769279Sume		return;
265869279Sume#endif
2659243233Shrs	/* XXX: Ignore connected routes. */
2660243233Shrs	if (!(rtm->rtm_flags & (RTF_GATEWAY|RTF_HOST|RTF_STATIC)))
2661243233Shrs		return;
266269279Sume	/*
266369279Sume	 * do not look at dynamic routes.
266469279Sume	 * netbsd/openbsd cloned routes have UGHD.
266569279Sume	 */
266669279Sume	if (rtm->rtm_flags & RTF_DYNAMIC)
266769279Sume		return;
266855163Sshin	rtmp = (char *)(rtm + 1);
266955163Sshin	/* Destination */
267055163Sshin	if ((rtm->rtm_addrs & RTA_DST) == 0)
267155163Sshin		return;		/* ignore routes without destination address */
267255163Sshin	sin6_dst = (struct sockaddr_in6 *)rtmp;
267364631Sitojun	rtmp += ROUNDUP(sin6_dst->sin6_len);
267455163Sshin	if (rtm->rtm_addrs & RTA_GATEWAY) {
267555163Sshin		sin6_gw = (struct sockaddr_in6 *)rtmp;
267655163Sshin		rtmp += ROUNDUP(sin6_gw->sin6_len);
267755163Sshin	}
267855163Sshin	if (rtm->rtm_addrs & RTA_NETMASK) {
267955163Sshin		sin6_mask = (struct sockaddr_in6 *)rtmp;
268055163Sshin		rtmp += ROUNDUP(sin6_mask->sin6_len);
268155163Sshin	}
268255163Sshin	if (rtm->rtm_addrs & RTA_GENMASK) {
268355163Sshin		sin6_genmask = (struct sockaddr_in6 *)rtmp;
268455163Sshin		rtmp += ROUNDUP(sin6_genmask->sin6_len);
268555163Sshin	}
268655163Sshin	if (rtm->rtm_addrs & RTA_IFP) {
268755163Sshin		sin6_ifp = (struct sockaddr_in6 *)rtmp;
268855163Sshin		rtmp += ROUNDUP(sin6_ifp->sin6_len);
268955163Sshin	}
269055163Sshin
269155163Sshin	/* Destination */
269255163Sshin	if (sin6_dst->sin6_family != AF_INET6)
269355163Sshin		return;
269455163Sshin	if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst->sin6_addr))
269555163Sshin		return;		/* Link-local */
269655163Sshin	if (IN6_ARE_ADDR_EQUAL(&sin6_dst->sin6_addr, &in6addr_loopback))
269755163Sshin		return;		/* Loopback */
269855163Sshin	if (IN6_IS_ADDR_MULTICAST(&sin6_dst->sin6_addr))
269955163Sshin		return;
270055163Sshin
270178064Sume	if ((rrt = MALLOC(struct riprt)) == NULL) {
270255163Sshin		fatal("malloc: struct riprt");
270378064Sume		/*NOTREACHED*/
270478064Sume	}
270562607Sitojun	memset(rrt, 0, sizeof(*rrt));
270655163Sshin	np = &rrt->rrt_info;
270755163Sshin	rrt->rrt_same = NULL;
270855163Sshin	rrt->rrt_t = time(NULL);
270955163Sshin	if (aflag == 0 && (rtm->rtm_flags & RTF_STATIC))
271055163Sshin		rrt->rrt_t = 0;	/* Don't age static routes */
2711243233Shrs	if (rtm->rtm_flags & Pflag)
2712243233Shrs		rrt->rrt_t = 0;	/* Don't age PROTO[123] routes */
2713122677Sume	if ((rtm->rtm_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST)
2714122677Sume		rrt->rrt_t = 0;	/* Don't age non-gateway host routes */
271555163Sshin	np->rip6_tag = 0;
271655163Sshin	np->rip6_metric = rtm->rtm_rmx.rmx_hopcount;
271755163Sshin	if (np->rip6_metric < 1)
271855163Sshin		np->rip6_metric = 1;
271955163Sshin	rrt->rrt_flags = rtm->rtm_flags;
272055163Sshin	np->rip6_dest = sin6_dst->sin6_addr;
272155163Sshin
272255163Sshin	/* Mask or plen */
272355163Sshin	if (rtm->rtm_flags & RTF_HOST)
272455163Sshin		np->rip6_plen = 128;	/* Host route */
272578064Sume	else if (sin6_mask)
272678064Sume		np->rip6_plen = sin6mask2len(sin6_mask);
272778064Sume	else
272855163Sshin		np->rip6_plen = 0;
272955163Sshin
2730243232Shrs	orrt = rtsearch(np);
273178064Sume	if (orrt && orrt->rrt_info.rip6_metric != HOPCNT_INFINITY6) {
273255163Sshin		/* Already found */
273355163Sshin		if (!again) {
273455163Sshin			trace(1, "route: %s/%d flags %s: already registered\n",
273555163Sshin				inet6_n2p(&np->rip6_dest), np->rip6_plen,
273655163Sshin				rtflags(rtm));
273755163Sshin		}
273855163Sshin		free(rrt);
273955163Sshin		return;
274055163Sshin	}
274155163Sshin	/* Gateway */
274255163Sshin	if (!sin6_gw)
274355163Sshin		memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
274455163Sshin	else {
274555163Sshin		if (sin6_gw->sin6_family == AF_INET6)
274655163Sshin			rrt->rrt_gw = sin6_gw->sin6_addr;
274755163Sshin		else if (sin6_gw->sin6_family == AF_LINK) {
274855163Sshin			/* XXX in case ppp link? */
274955163Sshin			rrt->rrt_gw = in6addr_loopback;
275055163Sshin		} else
275155163Sshin			memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
275255163Sshin	}
275355163Sshin	trace(1, "route: %s/%d flags %s",
275455163Sshin		inet6_n2p(&np->rip6_dest), np->rip6_plen, rtflags(rtm));
275555163Sshin	trace(1, " gw %s", inet6_n2p(&rrt->rrt_gw));
275655163Sshin
275755163Sshin	/* Interface */
2758243232Shrs	ifindex = rtm->rtm_index;
2759243232Shrs	if ((unsigned int)ifindex < nindex2ifc && index2ifc[ifindex])
2760243232Shrs		ifname = index2ifc[ifindex]->ifc_name;
276158070Sshin	else {
276258070Sshin		trace(1, " not configured\n");
276362607Sitojun		free(rrt);
276458070Sshin		return;
276558070Sshin	}
2766243232Shrs	trace(1, " if %s sock %d", ifname, ifindex);
2767243232Shrs	rrt->rrt_index = ifindex;
276855163Sshin
276962607Sitojun	trace(1, "\n");
277062607Sitojun
277155163Sshin	/* Check gateway */
277255163Sshin	if (!IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_gw) &&
2773122677Sume	    !IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw) &&
2774122677Sume	    (rrt->rrt_flags & RTF_LOCAL) == 0) {
277555163Sshin		trace(0, "***** Gateway %s is not a link-local address.\n",
277655163Sshin			inet6_n2p(&rrt->rrt_gw));
277755163Sshin		trace(0, "*****     dest(%s) if(%s) -- Not optimized.\n",
277862607Sitojun			inet6_n2p(&rrt->rrt_info.rip6_dest), ifname);
277962607Sitojun		rrt->rrt_rflags |= RRTF_NH_NOT_LLADDR;
278055163Sshin	}
278155163Sshin
278255163Sshin	/* Put it to the route list */
278378064Sume	if (orrt && orrt->rrt_info.rip6_metric == HOPCNT_INFINITY6) {
278478064Sume		/* replace route list */
2785243232Shrs		TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2786243232Shrs		TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
2787243232Shrs
278878064Sume		trace(1, "route: %s/%d flags %s: replace new route\n",
278978064Sume		    inet6_n2p(&np->rip6_dest), np->rip6_plen,
279078064Sume		    rtflags(rtm));
2791243232Shrs		free(orrt);
2792243232Shrs	} else
2793243232Shrs		TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
279455163Sshin}
279555163Sshin
279655163Sshinint
2797243232Shrsaddroute(struct riprt *rrt,
2798243232Shrs	const struct in6_addr *gw,
2799243232Shrs	struct ifc *ifcp)
280055163Sshin{
280155163Sshin	struct	netinfo6 *np;
280255163Sshin	u_char	buf[BUFSIZ], buf1[BUFSIZ], buf2[BUFSIZ];
280355163Sshin	struct	rt_msghdr	*rtm;
2804119031Sume	struct	sockaddr_in6	*sin6;
280555163Sshin	int	len;
280655163Sshin
280755163Sshin	np = &rrt->rrt_info;
280878064Sume	inet_ntop(AF_INET6, (const void *)gw, (char *)buf1, sizeof(buf1));
280955163Sshin	inet_ntop(AF_INET6, (void *)&ifcp->ifc_mylladdr, (char *)buf2, sizeof(buf2));
281055163Sshin	tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n",
281155163Sshin		inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
281255163Sshin		np->rip6_metric - 1, buf2);
281355163Sshin	if (rtlog)
281455163Sshin		fprintf(rtlog, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(),
281555163Sshin			inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
281655163Sshin			np->rip6_metric - 1, buf2);
281755163Sshin	if (nflag)
281855163Sshin		return 0;
281955163Sshin
282055163Sshin	memset(buf, 0, sizeof(buf));
282155163Sshin	rtm = (struct rt_msghdr *)buf;
282255163Sshin	rtm->rtm_type = RTM_ADD;
282355163Sshin	rtm->rtm_version = RTM_VERSION;
282455163Sshin	rtm->rtm_seq = ++seq;
282555163Sshin	rtm->rtm_pid = pid;
282662607Sitojun	rtm->rtm_flags = rrt->rrt_flags;
2827243233Shrs	rtm->rtm_flags |= Qflag;
282855163Sshin	rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
282955163Sshin	rtm->rtm_rmx.rmx_hopcount = np->rip6_metric - 1;
283055163Sshin	rtm->rtm_inits = RTV_HOPCOUNT;
2831119031Sume	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
283255163Sshin	/* Destination */
2833119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2834119031Sume	sin6->sin6_family = AF_INET6;
2835119031Sume	sin6->sin6_addr = np->rip6_dest;
2836119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
283755163Sshin	/* Gateway */
2838119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2839119031Sume	sin6->sin6_family = AF_INET6;
2840119031Sume	sin6->sin6_addr = *gw;
2841273188Shrs	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
2842273188Shrs		sin6->sin6_scope_id = ifcp->ifc_index;
2843119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
284455163Sshin	/* Netmask */
2845119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2846119031Sume	sin6->sin6_family = AF_INET6;
2847119031Sume	sin6->sin6_addr = *(plen2mask(np->rip6_plen));
2848119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
284955163Sshin
2850119031Sume	len = (char *)sin6 - (char *)buf;
285155163Sshin	rtm->rtm_msglen = len;
285255163Sshin	if (write(rtsock, buf, len) > 0)
285355163Sshin		return 0;
285455163Sshin
285555163Sshin	if (errno == EEXIST) {
285655163Sshin		trace(0, "ADD: Route already exists %s/%d gw %s\n",
2857119035Sume		    inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
285855163Sshin		if (rtlog)
285955163Sshin			fprintf(rtlog, "ADD: Route already exists %s/%d gw %s\n",
2860119035Sume			    inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
286155163Sshin	} else {
286255163Sshin		trace(0, "Can not write to rtsock (addroute): %s\n",
2863119035Sume		    strerror(errno));
286455163Sshin		if (rtlog)
286555163Sshin			fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2866119035Sume			    strerror(errno));
286755163Sshin	}
286855163Sshin	return -1;
286955163Sshin}
287055163Sshin
287155163Sshinint
2872243232Shrsdelroute(struct netinfo6 *np, struct in6_addr *gw)
287355163Sshin{
287455163Sshin	u_char	buf[BUFSIZ], buf2[BUFSIZ];
287555163Sshin	struct	rt_msghdr	*rtm;
2876119031Sume	struct	sockaddr_in6	*sin6;
287755163Sshin	int	len;
287855163Sshin
287955163Sshin	inet_ntop(AF_INET6, (void *)gw, (char *)buf2, sizeof(buf2));
288055163Sshin	tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np->rip6_dest),
288155163Sshin		np->rip6_plen, buf2);
288255163Sshin	if (rtlog)
288355163Sshin		fprintf(rtlog, "%s: DEL: %s/%d gw %s\n",
288455163Sshin			hms(), inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
288555163Sshin	if (nflag)
288655163Sshin		return 0;
288755163Sshin
288855163Sshin	memset(buf, 0, sizeof(buf));
288955163Sshin	rtm = (struct rt_msghdr *)buf;
289055163Sshin	rtm->rtm_type = RTM_DELETE;
289155163Sshin	rtm->rtm_version = RTM_VERSION;
289255163Sshin	rtm->rtm_seq = ++seq;
289355163Sshin	rtm->rtm_pid = pid;
289455163Sshin	rtm->rtm_flags = RTF_UP | RTF_GATEWAY;
2895243233Shrs	rtm->rtm_flags |= Qflag;
289678064Sume	if (np->rip6_plen == sizeof(struct in6_addr) * 8)
289778064Sume		rtm->rtm_flags |= RTF_HOST;
289855163Sshin	rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2899119031Sume	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
290055163Sshin	/* Destination */
2901119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2902119031Sume	sin6->sin6_family = AF_INET6;
2903119031Sume	sin6->sin6_addr = np->rip6_dest;
2904119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
290555163Sshin	/* Gateway */
2906119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2907119031Sume	sin6->sin6_family = AF_INET6;
2908119031Sume	sin6->sin6_addr = *gw;
2909119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
291055163Sshin	/* Netmask */
2911119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2912119031Sume	sin6->sin6_family = AF_INET6;
2913119031Sume	sin6->sin6_addr = *(plen2mask(np->rip6_plen));
2914119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
291555163Sshin
2916119031Sume	len = (char *)sin6 - (char *)buf;
291755163Sshin	rtm->rtm_msglen = len;
291855163Sshin	if (write(rtsock, buf, len) >= 0)
291955163Sshin		return 0;
292055163Sshin
292155163Sshin	if (errno == ESRCH) {
292255163Sshin		trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n",
2923119035Sume		    inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
292455163Sshin		if (rtlog)
292555163Sshin			fprintf(rtlog, "RTDEL: Route does not exist: %s/%d gw %s\n",
2926119035Sume			    inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
292755163Sshin	} else {
292855163Sshin		trace(0, "Can not write to rtsock (delroute): %s\n",
2929119035Sume		    strerror(errno));
293055163Sshin		if (rtlog)
293155163Sshin			fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2932119035Sume			    strerror(errno));
293355163Sshin	}
293455163Sshin	return -1;
293555163Sshin}
293655163Sshin
293755163Sshinstruct in6_addr *
2938243232Shrsgetroute(struct netinfo6 *np, struct in6_addr *gw)
293955163Sshin{
294055163Sshin	u_char buf[BUFSIZ];
2941119085Sume	int myseq;
294255163Sshin	int len;
294355163Sshin	struct rt_msghdr *rtm;
2944119031Sume	struct sockaddr_in6 *sin6;
294555163Sshin
294655163Sshin	rtm = (struct rt_msghdr *)buf;
294755163Sshin	len = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in6);
294855163Sshin	memset(rtm, 0, len);
294955163Sshin	rtm->rtm_type = RTM_GET;
295055163Sshin	rtm->rtm_version = RTM_VERSION;
295155163Sshin	myseq = ++seq;
295255163Sshin	rtm->rtm_seq = myseq;
295355163Sshin	rtm->rtm_addrs = RTA_DST;
295455163Sshin	rtm->rtm_msglen = len;
2955119031Sume	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2956119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2957119031Sume	sin6->sin6_family = AF_INET6;
2958119031Sume	sin6->sin6_addr = np->rip6_dest;
295955163Sshin	if (write(rtsock, buf, len) < 0) {
296055163Sshin		if (errno == ESRCH)	/* No such route found */
296155163Sshin			return NULL;
296255163Sshin		perror("write to rtsock");
296378064Sume		exit(1);
296455163Sshin	}
296555163Sshin	do {
296655163Sshin		if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
296755163Sshin			perror("read from rtsock");
296878064Sume			exit(1);
296955163Sshin		}
297055163Sshin		rtm = (struct rt_msghdr *)buf;
297155163Sshin	} while (rtm->rtm_seq != myseq || rtm->rtm_pid != pid);
2972119031Sume	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
297355163Sshin	if (rtm->rtm_addrs & RTA_DST) {
2974119031Sume		sin6 = (struct sockaddr_in6 *)
2975119031Sume			((char *)sin6 + ROUNDUP(sin6->sin6_len));
297655163Sshin	}
297755163Sshin	if (rtm->rtm_addrs & RTA_GATEWAY) {
2978119031Sume		*gw = sin6->sin6_addr;
297955163Sshin		return gw;
298055163Sshin	}
298155163Sshin	return NULL;
298255163Sshin}
298355163Sshin
298455163Sshinconst char *
2985243232Shrsinet6_n2p(const struct in6_addr *p)
298655163Sshin{
298755163Sshin	static char buf[BUFSIZ];
298855163Sshin
298978064Sume	return inet_ntop(AF_INET6, (const void *)p, buf, sizeof(buf));
299055163Sshin}
299155163Sshin
299255163Sshinvoid
2993243232Shrsifrtdump(int sig)
299455163Sshin{
299555163Sshin
299655163Sshin	ifdump(sig);
299755163Sshin	rtdump(sig);
299855163Sshin}
299955163Sshin
300055163Sshinvoid
3001243232Shrsifdump(int sig)
300255163Sshin{
300355163Sshin	struct ifc *ifcp;
300455163Sshin	FILE *dump;
3005243232Shrs	int nifc = 0;
300655163Sshin
300755163Sshin	if (sig == 0)
300855163Sshin		dump = stderr;
300955163Sshin	else
301055163Sshin		if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
301155163Sshin			dump = stderr;
301255163Sshin
301355163Sshin	fprintf(dump, "%s: Interface Table Dump\n", hms());
3014243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next)
3015243232Shrs		nifc++;
301655163Sshin	fprintf(dump, "  Number of interfaces: %d\n", nifc);
3017243232Shrs
3018243232Shrs	fprintf(dump, "  advertising interfaces:\n");
3019243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3020243232Shrs		if ((ifcp->ifc_flags & IFF_UP) == 0)
3021243232Shrs			continue;
3022243232Shrs		if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
3023243232Shrs			continue;
3024243232Shrs		ifdump0(dump, ifcp);
302555163Sshin	}
302655163Sshin	fprintf(dump, "\n");
3027243232Shrs	fprintf(dump, "  non-advertising interfaces:\n");
3028243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3029243232Shrs		if ((ifcp->ifc_flags & IFF_UP) &&
3030243232Shrs		    (iff_find(ifcp, IFIL_TYPE_N) == NULL))
3031243232Shrs			continue;
3032243232Shrs		ifdump0(dump, ifcp);
3033243232Shrs	}
3034243232Shrs	fprintf(dump, "\n");
303555163Sshin	if (dump != stderr)
303655163Sshin		fclose(dump);
303755163Sshin}
303855163Sshin
303955163Sshinvoid
3040243232Shrsifdump0(FILE *dump, const struct ifc *ifcp)
304155163Sshin{
3042243232Shrs	struct ifac *ifac;
304355163Sshin	struct iff *iffp;
304455163Sshin	char buf[BUFSIZ];
304555163Sshin	const char *ft;
304655163Sshin	int addr;
304755163Sshin
304855163Sshin	fprintf(dump, "    %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n",
304955163Sshin		ifcp->ifc_name, ifcp->ifc_index, ifflags(ifcp->ifc_flags),
305055163Sshin		inet6_n2p(&ifcp->ifc_mylladdr),
305155163Sshin		ifcp->ifc_mtu, ifcp->ifc_metric);
3052243232Shrs	TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
305355163Sshin		if (ifcp->ifc_flags & IFF_POINTOPOINT) {
3054243232Shrs			inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr,
305555163Sshin				buf, sizeof(buf));
305655163Sshin			fprintf(dump, "\t%s/%d -- %s\n",
3057243232Shrs				inet6_n2p(&ifac->ifac_addr),
3058243232Shrs				ifac->ifac_plen, buf);
305955163Sshin		} else {
306055163Sshin			fprintf(dump, "\t%s/%d\n",
3061243232Shrs				inet6_n2p(&ifac->ifac_addr),
3062243232Shrs				ifac->ifac_plen);
306355163Sshin		}
306455163Sshin	}
3065243232Shrs
3066243232Shrs	fprintf(dump, "\tFilter:\n");
3067243232Shrs	TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
3068243232Shrs		addr = 0;
3069243232Shrs		switch (iffp->iff_type) {
3070243232Shrs		case IFIL_TYPE_A:
3071243232Shrs			ft = "Aggregate"; addr++; break;
3072243232Shrs		case IFIL_TYPE_N:
3073243232Shrs			ft = "No-use"; break;
3074243232Shrs		case IFIL_TYPE_O:
3075243232Shrs			ft = "Advertise-only"; addr++; break;
3076243232Shrs		case IFIL_TYPE_T:
3077243232Shrs			ft = "Default-only"; break;
3078243232Shrs		case IFIL_TYPE_L:
3079243232Shrs			ft = "Listen-only"; addr++; break;
3080243232Shrs		default:
3081243232Shrs			snprintf(buf, sizeof(buf), "Unknown-%c", iffp->iff_type);
3082243232Shrs			ft = buf;
3083243232Shrs			addr++;
3084243232Shrs			break;
308555163Sshin		}
3086243232Shrs		fprintf(dump, "\t\t%s", ft);
3087243232Shrs		if (addr)
3088243232Shrs			fprintf(dump, "(%s/%d)", inet6_n2p(&iffp->iff_addr),
3089243232Shrs				iffp->iff_plen);
309055163Sshin		fprintf(dump, "\n");
309155163Sshin	}
3092243232Shrs	fprintf(dump, "\n");
309355163Sshin}
309455163Sshin
309555163Sshinvoid
3096243232Shrsrtdump(int sig)
309755163Sshin{
309855163Sshin	struct	riprt *rrt;
309955163Sshin	char	buf[BUFSIZ];
310055163Sshin	FILE	*dump;
310155163Sshin	time_t	t, age;
310255163Sshin
310355163Sshin	if (sig == 0)
310455163Sshin		dump = stderr;
310555163Sshin	else
310655163Sshin		if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
310755163Sshin			dump = stderr;
310855163Sshin
310955163Sshin	t = time(NULL);
311055163Sshin	fprintf(dump, "\n%s: Routing Table Dump\n", hms());
3111243232Shrs	TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
311255163Sshin		if (rrt->rrt_t == 0)
311355163Sshin			age = 0;
311455163Sshin		else
311555163Sshin			age = t - rrt->rrt_t;
311655163Sshin		inet_ntop(AF_INET6, (void *)&rrt->rrt_info.rip6_dest,
311755163Sshin			buf, sizeof(buf));
311855163Sshin		fprintf(dump, "    %s/%d if(%d:%s) gw(%s) [%d] age(%ld)",
311955163Sshin			buf, rrt->rrt_info.rip6_plen, rrt->rrt_index,
312055163Sshin			index2ifc[rrt->rrt_index]->ifc_name,
312155163Sshin			inet6_n2p(&rrt->rrt_gw),
312255163Sshin			rrt->rrt_info.rip6_metric, (long)age);
312355163Sshin		if (rrt->rrt_info.rip6_tag) {
312455163Sshin			fprintf(dump, " tag(0x%04x)",
312555163Sshin				ntohs(rrt->rrt_info.rip6_tag) & 0xffff);
312655163Sshin		}
312762607Sitojun		if (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)
312855163Sshin			fprintf(dump, " NOT-LL");
312962607Sitojun		if (rrt->rrt_rflags & RRTF_NOADVERTISE)
313055163Sshin			fprintf(dump, " NO-ADV");
313155163Sshin		fprintf(dump, "\n");
313255163Sshin	}
313355163Sshin	fprintf(dump, "\n");
313455163Sshin	if (dump != stderr)
313555163Sshin		fclose(dump);
313655163Sshin}
313755163Sshin
313855163Sshin/*
313955163Sshin * Parse the -A (and -O) options and put corresponding filter object to the
314078064Sume * specified interface structures.  Each of the -A/O option has the following
314155163Sshin * syntax:	-A 5f09:c400::/32,ef0,ef1  (aggregate)
314255163Sshin * 		-O 5f09:c400::/32,ef0,ef1  (only when match)
314355163Sshin */
314455163Sshinvoid
3145243232Shrsfilterconfig(void)
314655163Sshin{
314755163Sshin	int i;
3148119083Sume	char *p, *ap, *iflp, *ifname, *ep;
3149243232Shrs	struct iff iff, *iffp;
315078064Sume	struct ifc *ifcp;
315178064Sume	struct riprt *rrt;
315264631Sitojun#if 0
315378064Sume	struct in6_addr gw;
315464631Sitojun#endif
3155119083Sume	u_long plen;
315655163Sshin
315755163Sshin	for (i = 0; i < nfilter; i++) {
315855163Sshin		ap = filter[i];
315955163Sshin		iflp = NULL;
3160243232Shrs		iffp = &iff;
3161243232Shrs		memset(iffp, 0, sizeof(*iffp));
316255163Sshin		if (filtertype[i] == 'N' || filtertype[i] == 'T') {
316355163Sshin			iflp = ap;
316455163Sshin			goto ifonly;
316555163Sshin		}
3166119038Sume		if ((p = strchr(ap, ',')) != NULL) {
316755163Sshin			*p++ = '\0';
316855163Sshin			iflp = p;
316955163Sshin		}
3170119038Sume		if ((p = strchr(ap, '/')) == NULL) {
317155163Sshin			fatal("no prefixlen specified for '%s'", ap);
317278064Sume			/*NOTREACHED*/
317378064Sume		}
317455163Sshin		*p++ = '\0';
3175243232Shrs		if (inet_pton(AF_INET6, ap, &iffp->iff_addr) != 1) {
317655163Sshin			fatal("invalid prefix specified for '%s'", ap);
317778064Sume			/*NOTREACHED*/
317878064Sume		}
3179119083Sume		errno = 0;
3180119083Sume		ep = NULL;
3181119083Sume		plen = strtoul(p, &ep, 10);
3182243232Shrs		if (errno || !*p || *ep || plen > sizeof(iffp->iff_addr) * 8) {
3183119083Sume			fatal("invalid prefix length specified for '%s'", ap);
3184119083Sume			/*NOTREACHED*/
3185119083Sume		}
3186243232Shrs		iffp->iff_plen = plen;
3187243232Shrs		applyplen(&iffp->iff_addr, iffp->iff_plen);
318855163Sshinifonly:
3189243232Shrs		iffp->iff_type = filtertype[i];
319078064Sume		if (iflp == NULL || *iflp == '\0') {
319155163Sshin			fatal("no interface specified for '%s'", ap);
319278064Sume			/*NOTREACHED*/
319378064Sume		}
319455163Sshin		/* parse the interface listing portion */
319555163Sshin		while (iflp) {
319655163Sshin			ifname = iflp;
3197119038Sume			if ((iflp = strchr(iflp, ',')) != NULL)
319855163Sshin				*iflp++ = '\0';
3199243232Shrs
3200243232Shrs			TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3201243233Shrs				if (fnmatch(ifname, ifcp->ifc_name, 0) != 0)
3202243232Shrs					continue;
3203243232Shrs
3204243232Shrs				iffp = malloc(sizeof(*iffp));
3205243232Shrs				if (iffp == NULL) {
3206243232Shrs					fatal("malloc of iff");
3207243232Shrs					/*NOTREACHED*/
3208243232Shrs				}
3209243232Shrs				memcpy(iffp, &iff, sizeof(*iffp));
3210243233Shrs#if 0
3211243233Shrs				syslog(LOG_INFO, "Add filter: type %d, ifname %s.", iffp->iff_type, ifname);
3212243233Shrs#endif
3213243232Shrs				TAILQ_INSERT_HEAD(&ifcp->ifc_iff_head, iffp, iff_next);
321478064Sume			}
321555163Sshin		}
321678064Sume
321778064Sume		/*
321878064Sume		 * -A: aggregate configuration.
321978064Sume		 */
3220243232Shrs		if (filtertype[i] != IFIL_TYPE_A)
322155163Sshin			continue;
322255163Sshin		/* put the aggregate to the kernel routing table */
322355163Sshin		rrt = (struct riprt *)malloc(sizeof(struct riprt));
322478064Sume		if (rrt == NULL) {
322555163Sshin			fatal("malloc: rrt");
322678064Sume			/*NOTREACHED*/
322778064Sume		}
322855163Sshin		memset(rrt, 0, sizeof(struct riprt));
3229243232Shrs		rrt->rrt_info.rip6_dest = iff.iff_addr;
3230243232Shrs		rrt->rrt_info.rip6_plen = iff.iff_plen;
323155163Sshin		rrt->rrt_info.rip6_metric = 1;
323255163Sshin		rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
323355163Sshin		rrt->rrt_gw = in6addr_loopback;
323462607Sitojun		rrt->rrt_flags = RTF_UP | RTF_REJECT;
323562607Sitojun		rrt->rrt_rflags = RRTF_AGGREGATE;
323655163Sshin		rrt->rrt_t = 0;
3237119039Sume		rrt->rrt_index = loopifcp->ifc_index;
323864631Sitojun#if 0
323964631Sitojun		if (getroute(&rrt->rrt_info, &gw)) {
324064631Sitojun#if 0
324164631Sitojun			/*
324264631Sitojun			 * When the address has already been registered in the
324364631Sitojun			 * kernel routing table, it should be removed
324464631Sitojun			 */
324564631Sitojun			delroute(&rrt->rrt_info, &gw);
324664631Sitojun#else
324778064Sume			/* it is safer behavior */
324864631Sitojun			errno = EINVAL;
324964631Sitojun			fatal("%s/%u already in routing table, "
325064631Sitojun			    "cannot aggregate",
325164631Sitojun			    inet6_n2p(&rrt->rrt_info.rip6_dest),
325264631Sitojun			    rrt->rrt_info.rip6_plen);
325378064Sume			/*NOTREACHED*/
325464631Sitojun#endif
325564631Sitojun		}
325664631Sitojun#endif
325755163Sshin		/* Put the route to the list */
3258243232Shrs		TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
325955163Sshin		trace(1, "Aggregate: %s/%d for %s\n",
3260243232Shrs			inet6_n2p(&iff.iff_addr), iff.iff_plen,
3261243232Shrs			loopifcp->ifc_name);
326255163Sshin		/* Add this route to the kernel */
326355163Sshin		if (nflag) 	/* do not modify kernel routing table */
326455163Sshin			continue;
326555163Sshin		addroute(rrt, &in6addr_loopback, loopifcp);
326655163Sshin	}
326755163Sshin}
326855163Sshin
326955163Sshin/***************** utility functions *****************/
327055163Sshin
327155163Sshin/*
327255163Sshin * Returns a pointer to ifac whose address and prefix length matches
327355163Sshin * with the address and prefix length specified in the arguments.
327455163Sshin */
327555163Sshinstruct ifac *
3276243232Shrsifa_match(const struct ifc *ifcp,
3277243232Shrs	const struct in6_addr *ia,
3278243232Shrs	int plen)
327955163Sshin{
3280243232Shrs	struct ifac *ifac;
328155163Sshin
3282243232Shrs	TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
3283243232Shrs		if (IN6_ARE_ADDR_EQUAL(&ifac->ifac_addr, ia) &&
3284243232Shrs		    ifac->ifac_plen == plen)
328555163Sshin			break;
328655163Sshin	}
3287243232Shrs
3288243232Shrs	return (ifac);
328955163Sshin}
329055163Sshin
329155163Sshin/*
329255163Sshin * Return a pointer to riprt structure whose address and prefix length
329355163Sshin * matches with the address and prefix length found in the argument.
329478064Sume * Note: This is not a rtalloc().  Therefore exact match is necessary.
329555163Sshin */
329655163Sshinstruct riprt *
3297243232Shrsrtsearch(struct netinfo6 *np)
329855163Sshin{
329955163Sshin	struct	riprt	*rrt;
330055163Sshin
3301243232Shrs	TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
330255163Sshin		if (rrt->rrt_info.rip6_plen == np->rip6_plen &&
330355163Sshin		    IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
330455163Sshin				       &np->rip6_dest))
3305243232Shrs			break;
330655163Sshin	}
3307243232Shrs
3308243232Shrs	return (rrt);
330955163Sshin}
331055163Sshin
331155163Sshinint
3312243232Shrssin6mask2len(const struct sockaddr_in6 *sin6)
331378064Sume{
331478064Sume
331578064Sume	return mask2len(&sin6->sin6_addr,
331678064Sume	    sin6->sin6_len - offsetof(struct sockaddr_in6, sin6_addr));
331778064Sume}
331878064Sume
331978064Sumeint
3320243232Shrsmask2len(const struct in6_addr *addr, int lenlim)
332155163Sshin{
332255163Sshin	int i = 0, j;
332378064Sume	const u_char *p = (const u_char *)addr;
332462607Sitojun
332555163Sshin	for (j = 0; j < lenlim; j++, p++) {
332655163Sshin		if (*p != 0xff)
332755163Sshin			break;
332855163Sshin		i += 8;
332955163Sshin	}
333055163Sshin	if (j < lenlim) {
333155163Sshin		switch (*p) {
333262607Sitojun#define	MASKLEN(m, l)	case m: do { i += l; break; } while (0)
333362607Sitojun		MASKLEN(0xfe, 7); break;
333462607Sitojun		MASKLEN(0xfc, 6); break;
333562607Sitojun		MASKLEN(0xf8, 5); break;
333662607Sitojun		MASKLEN(0xf0, 4); break;
333762607Sitojun		MASKLEN(0xe0, 3); break;
333862607Sitojun		MASKLEN(0xc0, 2); break;
333962607Sitojun		MASKLEN(0x80, 1); break;
334055163Sshin#undef	MASKLEN
334155163Sshin		}
334255163Sshin	}
334355163Sshin	return i;
334455163Sshin}
334555163Sshin
334655163Sshinvoid
3347243232Shrsapplymask(struct in6_addr *addr, struct in6_addr *mask)
334855163Sshin{
334955163Sshin	int	i;
335055163Sshin	u_long	*p, *q;
335155163Sshin
335255163Sshin	p = (u_long *)addr; q = (u_long *)mask;
335355163Sshin	for (i = 0; i < 4; i++)
335455163Sshin		*p++ &= *q++;
335555163Sshin}
335655163Sshin
335755163Sshinstatic const u_char plent[8] = {
335855163Sshin	0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
335955163Sshin};
336055163Sshin
336155163Sshinvoid
3362243232Shrsapplyplen(struct in6_addr *ia, int plen)
336355163Sshin{
336455163Sshin	u_char	*p;
336555163Sshin	int	i;
336655163Sshin
336755163Sshin	p = ia->s6_addr;
336855163Sshin	for (i = 0; i < 16; i++) {
336955163Sshin		if (plen <= 0)
337055163Sshin			*p = 0;
337155163Sshin		else if (plen < 8)
337255163Sshin			*p &= plent[plen];
337355163Sshin		p++, plen -= 8;
337455163Sshin	}
337555163Sshin}
337655163Sshin
337755163Sshinstatic const int pl2m[9] = {
337855163Sshin	0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
337955163Sshin};
338055163Sshin
338155163Sshinstruct in6_addr *
3382243232Shrsplen2mask(int n)
338355163Sshin{
338455163Sshin	static struct in6_addr ia;
338555163Sshin	u_char	*p;
338655163Sshin	int	i;
338755163Sshin
338855163Sshin	memset(&ia, 0, sizeof(struct in6_addr));
338955163Sshin	p = (u_char *)&ia;
339055163Sshin	for (i = 0; i < 16; i++, p++, n -= 8) {
339155163Sshin		if (n >= 8) {
339255163Sshin			*p = 0xff;
339355163Sshin			continue;
339455163Sshin		}
339555163Sshin		*p = pl2m[n];
339655163Sshin		break;
339755163Sshin	}
339855163Sshin	return &ia;
339955163Sshin}
340055163Sshin
340155163Sshinchar *
3402243232Shrsallocopy(char *p)
340355163Sshin{
3404119033Sume	int len = strlen(p) + 1;
3405119033Sume	char *q = (char *)malloc(len);
340655163Sshin
3407119033Sume	if (!q) {
3408119033Sume		fatal("malloc");
3409119033Sume		/*NOTREACHED*/
3410119033Sume	}
3411119033Sume
3412119033Sume	strlcpy(q, p, len);
341355163Sshin	return q;
341455163Sshin}
341555163Sshin
341655163Sshinchar *
3417243232Shrshms(void)
341855163Sshin{
341955163Sshin	static char buf[BUFSIZ];
342055163Sshin	time_t t;
342155163Sshin	struct	tm *tm;
342255163Sshin
342355163Sshin	t = time(NULL);
342478064Sume	if ((tm = localtime(&t)) == 0) {
342555163Sshin		fatal("localtime");
342678064Sume		/*NOTREACHED*/
342778064Sume	}
342878064Sume	snprintf(buf, sizeof(buf), "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
342978064Sume	    tm->tm_sec);
343055163Sshin	return buf;
343155163Sshin}
343255163Sshin
343355163Sshin#define	RIPRANDDEV	1.0	/* 30 +- 15, max - min = 30 */
343455163Sshin
343555163Sshinint
3436243232Shrsripinterval(int timer)
343755163Sshin{
343855163Sshin	double r = rand();
343955163Sshin
344055163Sshin	interval = (int)(timer + timer * RIPRANDDEV * (r / RAND_MAX - 0.5));
344155163Sshin	nextalarm = time(NULL) + interval;
344255163Sshin	return interval;
344355163Sshin}
344455163Sshin
344555163Sshintime_t
3446243232Shrsripsuptrig(void)
344755163Sshin{
344855163Sshin	time_t t;
344955163Sshin
345055163Sshin	double r = rand();
345162607Sitojun	t  = (int)(RIP_TRIG_INT6_MIN +
345278064Sume		(RIP_TRIG_INT6_MAX - RIP_TRIG_INT6_MIN) * (r / RAND_MAX));
345355163Sshin	sup_trig_update = time(NULL) + t;
345455163Sshin	return t;
345555163Sshin}
345655163Sshin
345755163Sshinvoid
345855163Sshin#ifdef __STDC__
345955163Sshinfatal(const char *fmt, ...)
346055163Sshin#else
346155163Sshinfatal(fmt, va_alist)
346255163Sshin	char	*fmt;
346355163Sshin	va_dcl
346455163Sshin#endif
346555163Sshin{
346655163Sshin	va_list ap;
346755163Sshin	char buf[1024];
346855163Sshin
346955163Sshin#ifdef __STDC__
347055163Sshin	va_start(ap, fmt);
347155163Sshin#else
347255163Sshin	va_start(ap);
347355163Sshin#endif
347455163Sshin	vsnprintf(buf, sizeof(buf), fmt, ap);
3475119043Sume	va_end(ap);
347655163Sshin	perror(buf);
3477119043Sume	if (errno)
3478119043Sume		syslog(LOG_ERR, "%s: %s", buf, strerror(errno));
3479119043Sume	else
3480119043Sume		syslog(LOG_ERR, "%s", buf);
348178064Sume	rtdexit();
348255163Sshin}
348355163Sshin
348455163Sshinvoid
348555163Sshin#ifdef __STDC__
348655163Sshintracet(int level, const char *fmt, ...)
348755163Sshin#else
348855163Sshintracet(level, fmt, va_alist)
348955163Sshin	int level;
349055163Sshin	char *fmt;
349155163Sshin	va_dcl
349255163Sshin#endif
349355163Sshin{
349455163Sshin	va_list ap;
349555163Sshin
3496119043Sume	if (level <= dflag) {
349755163Sshin#ifdef __STDC__
3498119043Sume		va_start(ap, fmt);
349955163Sshin#else
3500119043Sume		va_start(ap);
350155163Sshin#endif
350255163Sshin		fprintf(stderr, "%s: ", hms());
350355163Sshin		vfprintf(stderr, fmt, ap);
3504119043Sume		va_end(ap);
350555163Sshin	}
350655163Sshin	if (dflag) {
3507119043Sume#ifdef __STDC__
3508119043Sume		va_start(ap, fmt);
3509119043Sume#else
3510119043Sume		va_start(ap);
3511119043Sume#endif
351255163Sshin		if (level > 0)
351355163Sshin			vsyslog(LOG_DEBUG, fmt, ap);
351455163Sshin		else
351555163Sshin			vsyslog(LOG_WARNING, fmt, ap);
3516119043Sume		va_end(ap);
351755163Sshin	}
351855163Sshin}
351955163Sshin
352055163Sshinvoid
352155163Sshin#ifdef __STDC__
352255163Sshintrace(int level, const char *fmt, ...)
352355163Sshin#else
352455163Sshintrace(level, fmt, va_alist)
352555163Sshin	int level;
352655163Sshin	char *fmt;
352755163Sshin	va_dcl
352855163Sshin#endif
352955163Sshin{
353055163Sshin	va_list ap;
353155163Sshin
3532119043Sume	if (level <= dflag) {
353355163Sshin#ifdef __STDC__
3534119043Sume		va_start(ap, fmt);
353555163Sshin#else
3536119043Sume		va_start(ap);
353755163Sshin#endif
353855163Sshin		vfprintf(stderr, fmt, ap);
3539119043Sume		va_end(ap);
3540119043Sume	}
354155163Sshin	if (dflag) {
3542119043Sume#ifdef __STDC__
3543119043Sume		va_start(ap, fmt);
3544119043Sume#else
3545119043Sume		va_start(ap);
3546119043Sume#endif
354755163Sshin		if (level > 0)
354855163Sshin			vsyslog(LOG_DEBUG, fmt, ap);
354955163Sshin		else
355055163Sshin			vsyslog(LOG_WARNING, fmt, ap);
3551119043Sume		va_end(ap);
355255163Sshin	}
355355163Sshin}
355455163Sshin
355555163Sshinunsigned int
3556243232Shrsif_maxindex(void)
355755163Sshin{
355855163Sshin	struct if_nameindex *p, *p0;
355955163Sshin	unsigned int max = 0;
356055163Sshin
356155163Sshin	p0 = if_nameindex();
356255163Sshin	for (p = p0; p && p->if_index && p->if_name; p++) {
356355163Sshin		if (max < p->if_index)
356455163Sshin			max = p->if_index;
356555163Sshin	}
356655163Sshin	if_freenameindex(p0);
356755163Sshin	return max;
356855163Sshin}
356955163Sshin
357055163Sshinstruct ifc *
3571243232Shrsifc_find(char *name)
357255163Sshin{
357355163Sshin	struct ifc *ifcp;
357455163Sshin
3575243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
357655163Sshin		if (strcmp(name, ifcp->ifc_name) == 0)
3577243232Shrs			break;
357855163Sshin	}
3579243232Shrs	return (ifcp);
358055163Sshin}
358155163Sshin
358255163Sshinstruct iff *
3583243232Shrsiff_find(struct ifc *ifcp, int type)
358455163Sshin{
358555163Sshin	struct iff *iffp;
358655163Sshin
3587243232Shrs	TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
3588243232Shrs		if (type == IFIL_TYPE_ANY ||
3589243232Shrs		    type == iffp->iff_type)
3590243232Shrs			break;
359155163Sshin	}
3592243232Shrs
3593243232Shrs	return (iffp);
359455163Sshin}
359555163Sshin
359655163Sshinvoid
3597243232Shrssetindex2ifc(int idx, struct ifc *ifcp)
359855163Sshin{
3599122677Sume	int n, nsize;
360062607Sitojun	struct ifc **p;
360155163Sshin
360255163Sshin	if (!index2ifc) {
360355163Sshin		nindex2ifc = 5;	/*initial guess*/
360455163Sshin		index2ifc = (struct ifc **)
360555163Sshin			malloc(sizeof(*index2ifc) * nindex2ifc);
360678064Sume		if (index2ifc == NULL) {
360755163Sshin			fatal("malloc");
360878064Sume			/*NOTREACHED*/
360978064Sume		}
361055163Sshin		memset(index2ifc, 0, sizeof(*index2ifc) * nindex2ifc);
361155163Sshin	}
361255163Sshin	n = nindex2ifc;
3613122677Sume	for (nsize = nindex2ifc; nsize <= idx; nsize *= 2)
3614122677Sume		;
3615122677Sume	if (n != nsize) {
361662607Sitojun		p = (struct ifc **)realloc(index2ifc,
3617122677Sume		    sizeof(*index2ifc) * nsize);
361878064Sume		if (p == NULL) {
361955163Sshin			fatal("realloc");
362078064Sume			/*NOTREACHED*/
362178064Sume		}
362278064Sume		memset(p + n, 0, sizeof(*index2ifc) * (nindex2ifc - n));
362362607Sitojun		index2ifc = p;
3624122677Sume		nindex2ifc = nsize;
362555163Sshin	}
362678064Sume	index2ifc[idx] = ifcp;
362755163Sshin}
3628