route6d.c revision 243233
162607Sitojun/*	$FreeBSD: head/usr.sbin/route6d/route6d.c 243233 2012-11-18 15:48:02Z hrs $	*/
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>
6762607Sitojun#define	_KERNEL	1
6855163Sshin#include <net/route.h>
6962607Sitojun#undef _KERNEL
7055163Sshin#include <netinet/in.h>
7155163Sshin#include <netinet/in_var.h>
7255163Sshin#include <netinet/ip6.h>
7355163Sshin#include <netinet/udp.h>
7455163Sshin#include <netdb.h>
7562607Sitojun#include <ifaddrs.h>
7655163Sshin
7755163Sshin#include <arpa/inet.h>
7855163Sshin
7955163Sshin#include "route6d.h"
8055163Sshin
8155163Sshin#define	MAXFILTER	40
82243233Shrs#define RT_DUMP_MAXRETRY	15
8355163Sshin
8455163Sshin#ifdef	DEBUG
8555163Sshin#define	INIT_INTERVAL6	6
8655163Sshin#else
87119039Sume#define	INIT_INTERVAL6	10	/* Wait to submit an initial riprequest */
8855163Sshin#endif
8955163Sshin
9055163Sshin/* alignment constraint for routing socket */
9162607Sitojun#define ROUNDUP(a) \
9255163Sshin	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
9362607Sitojun#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
9455163Sshin
95243232Shrsstruct ifc {			/* Configuration of an interface */
96243232Shrs	TAILQ_ENTRY(ifc) ifc_next;
97243232Shrs
98243232Shrs	char	ifc_name[IFNAMSIZ];		/* if name */
9955163Sshin	int	ifc_index;			/* if index */
10055163Sshin	int	ifc_mtu;			/* if mtu */
10155163Sshin	int	ifc_metric;			/* if metric */
10278064Sume	u_int	ifc_flags;			/* flags */
10378064Sume	short	ifc_cflags;			/* IFC_XXX */
10455163Sshin	struct	in6_addr ifc_mylladdr;		/* my link-local address */
10555163Sshin	struct	sockaddr_in6 ifc_ripsin;	/* rip multicast address */
106243232Shrs	TAILQ_HEAD(, ifac) ifc_ifac_head;	/* list of AF_INET6 addrs */
107243232Shrs	TAILQ_HEAD(, iff) ifc_iff_head;		/* list of filters */
10855163Sshin	int	ifc_joined;			/* joined to ff02::9 */
10955163Sshin};
110243232ShrsTAILQ_HEAD(, ifc) ifc_head = TAILQ_HEAD_INITIALIZER(ifc_head);
11155163Sshin
112243232Shrsstruct ifac {			/* Adddress associated to an interface */
113243232Shrs	TAILQ_ENTRY(ifac) ifac_next;
114243232Shrs
115243232Shrs	struct	ifc *ifac_ifc;		/* back pointer */
116243232Shrs	struct	in6_addr ifac_addr;	/* address */
117243232Shrs	struct	in6_addr ifac_raddr;	/* remote address, valid in p2p */
118243232Shrs	int	ifac_scope_id;		/* scope id */
119243232Shrs	int	ifac_plen;		/* prefix length */
12055163Sshin};
12155163Sshin
122243232Shrsstruct iff {			/* Filters for an interface */
123243232Shrs	TAILQ_ENTRY(iff) iff_next;
124243232Shrs
12555163Sshin	int	iff_type;
12655163Sshin	struct	in6_addr iff_addr;
12755163Sshin	int	iff_plen;
12855163Sshin};
12955163Sshin
13055163Sshinstruct	ifc **index2ifc;
131243232Shrsunsigned int	nindex2ifc;
13255163Sshinstruct	ifc *loopifcp = NULL;	/* pointing to loopback */
133119076Sume#ifdef HAVE_POLL_H
134119076Sumestruct	pollfd set[2];
135119076Sume#else
136119070Sumefd_set	*sockvecp;	/* vector to select() for receiving */
137119070Sumefd_set	*recvecp;
138119070Sumeint	fdmasks;
139119070Sumeint	maxfd;		/* maximum fd for select() */
140119076Sume#endif
14155163Sshinint	rtsock;		/* the routing socket */
14255163Sshinint	ripsock;	/* socket to send/receive RIP datagram */
14355163Sshin
14455163Sshinstruct	rip6 *ripbuf;	/* packet buffer for sending */
14555163Sshin
14655163Sshin/*
14778064Sume * Maintain the routes in a linked list.  When the number of the routes
14855163Sshin * grows, somebody would like to introduce a hash based or a radix tree
14978064Sume * based structure.  I believe the number of routes handled by RIP is
15055163Sshin * limited and I don't have to manage a complex data structure, however.
15155163Sshin *
15255163Sshin * One of the major drawbacks of the linear linked list is the difficulty
15378064Sume * of representing the relationship between a couple of routes.  This may
15455163Sshin * be a significant problem when we have to support route aggregation with
155228990Suqs * suppressing the specifics covered by the aggregate.
15655163Sshin */
15755163Sshin
158243232Shrsstruct riprt {
159243232Shrs	TAILQ_ENTRY(riprt) rrt_next;	/* next destination */
160243232Shrs
16155163Sshin	struct	riprt *rrt_same;	/* same destination - future use */
16255163Sshin	struct	netinfo6 rrt_info;	/* network info */
16355163Sshin	struct	in6_addr rrt_gw;	/* gateway */
16462607Sitojun	u_long	rrt_flags;		/* kernel routing table flags */
16562607Sitojun	u_long	rrt_rflags;		/* route6d routing table flags */
16655163Sshin	time_t	rrt_t;			/* when the route validated */
16755163Sshin	int	rrt_index;		/* ifindex from which this route got */
16855163Sshin};
169243232ShrsTAILQ_HEAD(, riprt) riprt_head = TAILQ_HEAD_INITIALIZER(riprt_head);
17055163Sshin
17155163Sshinint	dflag = 0;	/* debug flag */
17255163Sshinint	qflag = 0;	/* quiet flag */
17355163Sshinint	nflag = 0;	/* don't update kernel routing table */
17455163Sshinint	aflag = 0;	/* age out even the statically defined routes */
17555163Sshinint	hflag = 0;	/* don't split horizon */
17655163Sshinint	lflag = 0;	/* exchange site local routes */
177243233Shrsint	Pflag = 0;	/* don't age out routes with RTF_PROTO[123] */
178243233Shrsint	Qflag = RTF_PROTO2;	/* set RTF_PROTO[123] flag to routes by RIPng */
17955163Sshinint	sflag = 0;	/* announce static routes w/ split horizon */
18055163Sshinint	Sflag = 0;	/* announce static routes to every interface */
18162607Sitojununsigned long routetag = 0;	/* route tag attached on originating case */
18255163Sshin
18355163Sshinchar	*filter[MAXFILTER];
18455163Sshinint	filtertype[MAXFILTER];
18555163Sshinint	nfilter = 0;
18655163Sshin
18755163Sshinpid_t	pid;
18855163Sshin
18955163Sshinstruct	sockaddr_storage ripsin;
19055163Sshin
19155163Sshinint	interval = 1;
19255163Sshintime_t	nextalarm = 0;
19355163Sshintime_t	sup_trig_update = 0;
19455163Sshin
19555163SshinFILE	*rtlog = NULL;
19655163Sshin
19755163Sshinint logopened = 0;
19855163Sshin
199119085Sumestatic	int	seq = 0;
20055163Sshin
20178064Sumevolatile sig_atomic_t seenalrm;
20278064Sumevolatile sig_atomic_t seenquit;
20378064Sumevolatile sig_atomic_t seenusr1;
20478064Sume
20562607Sitojun#define	RRTF_AGGREGATE		0x08000000
20662607Sitojun#define	RRTF_NOADVERTISE	0x10000000
20762607Sitojun#define	RRTF_NH_NOT_LLADDR	0x20000000
20862607Sitojun#define RRTF_SENDANYWAY		0x40000000
20962607Sitojun#define	RRTF_CHANGED		0x80000000
21055163Sshin
211173412Skevloint main(int, char **);
212173412Skevlovoid sighandler(int);
213173412Skevlovoid ripalarm(void);
214173412Skevlovoid riprecv(void);
215173412Skevlovoid ripsend(struct ifc *, struct sockaddr_in6 *, int);
216173412Skevloint out_filter(struct riprt *, struct ifc *);
217173412Skevlovoid init(void);
218173412Skevlovoid sockopt(struct ifc *);
219173412Skevlovoid ifconfig(void);
220243232Shrsint ifconfig1(const char *, const struct sockaddr *, struct ifc *, int);
221173412Skevlovoid rtrecv(void);
222173412Skevloint rt_del(const struct sockaddr_in6 *, const struct sockaddr_in6 *,
223173412Skevlo	const struct sockaddr_in6 *);
224173412Skevloint rt_deladdr(struct ifc *, const struct sockaddr_in6 *,
225173412Skevlo	const struct sockaddr_in6 *);
226173412Skevlovoid filterconfig(void);
227173412Skevloint getifmtu(int);
228173412Skevloconst char *rttypes(struct rt_msghdr *);
229173412Skevloconst char *rtflags(struct rt_msghdr *);
230173412Skevloconst char *ifflags(int);
231173412Skevloint ifrt(struct ifc *, int);
232173412Skevlovoid ifrt_p2p(struct ifc *, int);
233173412Skevlovoid applymask(struct in6_addr *, struct in6_addr *);
234173412Skevlovoid applyplen(struct in6_addr *, int);
235173412Skevlovoid ifrtdump(int);
236173412Skevlovoid ifdump(int);
237173412Skevlovoid ifdump0(FILE *, const struct ifc *);
238243233Shrsvoid ifremove(int);
239173412Skevlovoid rtdump(int);
240173412Skevlovoid rt_entry(struct rt_msghdr *, int);
241173412Skevlovoid rtdexit(void);
242173412Skevlovoid riprequest(struct ifc *, struct netinfo6 *, int,
243173412Skevlo	struct sockaddr_in6 *);
244243232Shrsvoid ripflush(struct ifc *, struct sockaddr_in6 *, int, struct netinfo6 *np);
245173412Skevlovoid sendrequest(struct ifc *);
246173412Skevloint sin6mask2len(const struct sockaddr_in6 *);
247173412Skevloint mask2len(const struct in6_addr *, int);
248173412Skevloint sendpacket(struct sockaddr_in6 *, int);
249173412Skevloint addroute(struct riprt *, const struct in6_addr *, struct ifc *);
250173412Skevloint delroute(struct netinfo6 *, struct in6_addr *);
251173412Skevlostruct in6_addr *getroute(struct netinfo6 *, struct in6_addr *);
252173412Skevlovoid krtread(int);
253173412Skevloint tobeadv(struct riprt *, struct ifc *);
254173412Skevlochar *allocopy(char *);
255173412Skevlochar *hms(void);
256173412Skevloconst char *inet6_n2p(const struct in6_addr *);
257173412Skevlostruct ifac *ifa_match(const struct ifc *, const struct in6_addr *, int);
258173412Skevlostruct in6_addr *plen2mask(int);
259243232Shrsstruct riprt *rtsearch(struct netinfo6 *);
260173412Skevloint ripinterval(int);
261173412Skevlotime_t ripsuptrig(void);
262173412Skevlovoid fatal(const char *, ...)
26366807Skris	__attribute__((__format__(__printf__, 1, 2)));
264173412Skevlovoid trace(int, const char *, ...)
26566807Skris	__attribute__((__format__(__printf__, 2, 3)));
266173412Skevlovoid tracet(int, const char *, ...)
26766807Skris	__attribute__((__format__(__printf__, 2, 3)));
268173412Skevlounsigned int if_maxindex(void);
269173412Skevlostruct ifc *ifc_find(char *);
270173412Skevlostruct iff *iff_find(struct ifc *, int);
271173412Skevlovoid setindex2ifc(int, struct ifc *);
27255163Sshin
27355163Sshin#define	MALLOC(type)	((type *)malloc(sizeof(type)))
27455163Sshin
275243232Shrs#define IFIL_TYPE_ANY	0x0
276243232Shrs#define IFIL_TYPE_A	'A'
277243232Shrs#define IFIL_TYPE_N	'N'
278243232Shrs#define IFIL_TYPE_T	'T'
279243232Shrs#define IFIL_TYPE_O	'O'
280243232Shrs#define IFIL_TYPE_L	'L'
281243232Shrs
28255163Sshinint
283243232Shrsmain(int argc, char *argv[])
28455163Sshin{
28555163Sshin	int	ch;
28655163Sshin	int	error = 0;
287243233Shrs	unsigned long proto;
28855163Sshin	struct	ifc *ifcp;
28955163Sshin	sigset_t mask, omask;
290243233Shrs	const char *pidfile = ROUTE6D_PID;
291243233Shrs	FILE *pidfh;
29255163Sshin	char *progname;
29362607Sitojun	char *ep;
29455163Sshin
29555163Sshin	progname = strrchr(*argv, '/');
29655163Sshin	if (progname)
29755163Sshin		progname++;
29855163Sshin	else
29955163Sshin		progname = *argv;
30055163Sshin
30155163Sshin	pid = getpid();
302243233Shrs	while ((ch = getopt(argc, argv, "A:N:O:R:T:L:t:adDhlnp:P:Q:qsS")) != -1) {
30355163Sshin		switch (ch) {
30455163Sshin		case 'A':
30555163Sshin		case 'N':
30655163Sshin		case 'O':
30755163Sshin		case 'T':
30855163Sshin		case 'L':
30962607Sitojun			if (nfilter >= MAXFILTER) {
31055163Sshin				fatal("Exceeds MAXFILTER");
31162607Sitojun				/*NOTREACHED*/
31262607Sitojun			}
31355163Sshin			filtertype[nfilter] = ch;
31455163Sshin			filter[nfilter++] = allocopy(optarg);
31555163Sshin			break;
31655163Sshin		case 't':
31762607Sitojun			ep = NULL;
31862607Sitojun			routetag = strtoul(optarg, &ep, 0);
31962607Sitojun			if (!ep || *ep != '\0' || (routetag & ~0xffff) != 0) {
32055163Sshin				fatal("invalid route tag");
32155163Sshin				/*NOTREACHED*/
32255163Sshin			}
32355163Sshin			break;
324243233Shrs		case 'p':
325243233Shrs			pidfile = optarg;
326243233Shrs			break;
327243233Shrs		case 'P':
328243233Shrs			ep = NULL;
329243233Shrs			proto = strtoul(optarg, &ep, 0);
330243233Shrs			if (!ep || *ep != '\0' || 3 < proto) {
331243233Shrs				fatal("invalid P flag");
332243233Shrs				/*NOTREACHED*/
333243233Shrs			}
334243233Shrs			if (proto == 0)
335243233Shrs				Pflag = 0;
336243233Shrs			if (proto == 1)
337243233Shrs				Pflag |= RTF_PROTO1;
338243233Shrs			if (proto == 2)
339243233Shrs				Pflag |= RTF_PROTO2;
340243233Shrs			if (proto == 3)
341243233Shrs				Pflag |= RTF_PROTO3;
342243233Shrs			break;
343243233Shrs		case 'Q':
344243233Shrs			ep = NULL;
345243233Shrs			proto = strtoul(optarg, &ep, 0);
346243233Shrs			if (!ep || *ep != '\0' || 3 < proto) {
347243233Shrs				fatal("invalid Q flag");
348243233Shrs				/*NOTREACHED*/
349243233Shrs			}
350243233Shrs			if (proto == 0)
351243233Shrs				Qflag = 0;
352243233Shrs			if (proto == 1)
353243233Shrs				Qflag |= RTF_PROTO1;
354243233Shrs			if (proto == 2)
355243233Shrs				Qflag |= RTF_PROTO2;
356243233Shrs			if (proto == 3)
357243233Shrs				Qflag |= RTF_PROTO3;
358243233Shrs			break;
35955163Sshin		case 'R':
36062607Sitojun			if ((rtlog = fopen(optarg, "w")) == NULL) {
36155163Sshin				fatal("Can not write to routelog");
36262607Sitojun				/*NOTREACHED*/
36362607Sitojun			}
36455163Sshin			break;
36562607Sitojun#define	FLAG(c, flag, n)	case c: do { flag = n; break; } while(0)
36662607Sitojun		FLAG('a', aflag, 1); break;
36762607Sitojun		FLAG('d', dflag, 1); break;
36862607Sitojun		FLAG('D', dflag, 2); break;
36962607Sitojun		FLAG('h', hflag, 1); break;
37062607Sitojun		FLAG('l', lflag, 1); break;
37162607Sitojun		FLAG('n', nflag, 1); break;
37262607Sitojun		FLAG('q', qflag, 1); break;
37362607Sitojun		FLAG('s', sflag, 1); break;
37462607Sitojun		FLAG('S', Sflag, 1); break;
37555163Sshin#undef	FLAG
37655163Sshin		default:
37755163Sshin			fatal("Invalid option specified, terminating");
37862607Sitojun			/*NOTREACHED*/
37955163Sshin		}
38055163Sshin	}
38155163Sshin	argc -= optind;
38255163Sshin	argv += optind;
38378064Sume	if (argc > 0) {
38455163Sshin		fatal("bogus extra arguments");
38578064Sume		/*NOTREACHED*/
38678064Sume	}
38755163Sshin
38855163Sshin	if (geteuid()) {
38955163Sshin		nflag = 1;
39055163Sshin		fprintf(stderr, "No kernel update is allowed\n");
39155163Sshin	}
392119037Sume
393119037Sume	if (dflag == 0) {
394119037Sume		if (daemon(0, 0) < 0) {
395119037Sume			fatal("daemon");
396119037Sume			/*NOTREACHED*/
397119037Sume		}
398119037Sume	}
399119037Sume
40055163Sshin	openlog(progname, LOG_NDELAY|LOG_PID, LOG_DAEMON);
40155163Sshin	logopened++;
40278064Sume
40378064Sume	if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL)
40478064Sume		fatal("malloc");
40578064Sume	memset(ripbuf, 0, RIP6_MAXMTU);
40678064Sume	ripbuf->rip6_cmd = RIP6_RESPONSE;
40778064Sume	ripbuf->rip6_vers = RIP6_VERSION;
40878064Sume	ripbuf->rip6_res1[0] = 0;
40978064Sume	ripbuf->rip6_res1[1] = 0;
41078064Sume
41155163Sshin	init();
41255163Sshin	ifconfig();
413243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
41455163Sshin		if (ifcp->ifc_index < 0) {
415243232Shrs			fprintf(stderr, "No ifindex found at %s "
416243232Shrs			    "(no link-local address?)\n", ifcp->ifc_name);
41755163Sshin			error++;
41855163Sshin		}
41955163Sshin	}
42055163Sshin	if (error)
42155163Sshin		exit(1);
42278064Sume	if (loopifcp == NULL) {
42355163Sshin		fatal("No loopback found");
42478064Sume		/*NOTREACHED*/
42578064Sume	}
426243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
42755163Sshin		ifrt(ifcp, 0);
428243232Shrs	}
42955163Sshin	filterconfig();
43055163Sshin	krtread(0);
43155163Sshin	if (dflag)
43255163Sshin		ifrtdump(0);
43355163Sshin
43455163Sshin	pid = getpid();
435243233Shrs	if ((pidfh = fopen(pidfile, "w")) != NULL) {
436243233Shrs		fprintf(pidfh, "%d\n", pid);
437243233Shrs		fclose(pidfh);
43855163Sshin	}
43955163Sshin
44078064Sume	if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL) {
44155163Sshin		fatal("malloc");
44278064Sume		/*NOTREACHED*/
44378064Sume	}
44462607Sitojun	memset(ripbuf, 0, RIP6_MAXMTU);
44555163Sshin	ripbuf->rip6_cmd = RIP6_RESPONSE;
44655163Sshin	ripbuf->rip6_vers = RIP6_VERSION;
44755163Sshin	ripbuf->rip6_res1[0] = 0;
44855163Sshin	ripbuf->rip6_res1[1] = 0;
44955163Sshin
45078064Sume	if (signal(SIGALRM, sighandler) == SIG_ERR ||
45178064Sume	    signal(SIGQUIT, sighandler) == SIG_ERR ||
45278064Sume	    signal(SIGTERM, sighandler) == SIG_ERR ||
45378064Sume	    signal(SIGUSR1, sighandler) == SIG_ERR ||
45478064Sume	    signal(SIGHUP, sighandler) == SIG_ERR ||
45578064Sume	    signal(SIGINT, sighandler) == SIG_ERR) {
45678064Sume		fatal("signal");
45778064Sume		/*NOTREACHED*/
45878064Sume	}
45955163Sshin	/*
46055163Sshin	 * To avoid rip packet congestion (not on a cable but in this
46155163Sshin	 * process), wait for a moment to send the first RIP6_RESPONSE
46255163Sshin	 * packets.
46355163Sshin	 */
46455163Sshin	alarm(ripinterval(INIT_INTERVAL6));
46555163Sshin
466243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
467243232Shrs		if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
468119041Sume			continue;
46955163Sshin		if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
47055163Sshin			sendrequest(ifcp);
47155163Sshin	}
47255163Sshin
47355163Sshin	syslog(LOG_INFO, "**** Started ****");
47455163Sshin	sigemptyset(&mask);
47555163Sshin	sigaddset(&mask, SIGALRM);
47655163Sshin	while (1) {
47778064Sume		if (seenalrm) {
47878064Sume			ripalarm();
47978064Sume			seenalrm = 0;
48078064Sume			continue;
48178064Sume		}
48278064Sume		if (seenquit) {
48378064Sume			rtdexit();
48478064Sume			seenquit = 0;
48578064Sume			continue;
48678064Sume		}
48778064Sume		if (seenusr1) {
48878064Sume			ifrtdump(SIGUSR1);
48978064Sume			seenusr1 = 0;
49078064Sume			continue;
49178064Sume		}
49278064Sume
493119076Sume#ifdef HAVE_POLL_H
494119076Sume		switch (poll(set, 2, INFTIM))
495119076Sume#else
496119070Sume		memcpy(recvecp, sockvecp, fdmasks);
497119076Sume		switch (select(maxfd + 1, recvecp, 0, 0, 0))
498119076Sume#endif
499119076Sume		{
50055163Sshin		case -1:
50178064Sume			if (errno != EINTR) {
50278064Sume				fatal("select");
50378064Sume				/*NOTREACHED*/
50478064Sume			}
50578064Sume			continue;
50655163Sshin		case 0:
50755163Sshin			continue;
50855163Sshin		default:
509119076Sume#ifdef HAVE_POLL_H
510119076Sume			if (set[0].revents & POLLIN)
511119076Sume#else
512119076Sume			if (FD_ISSET(ripsock, recvecp))
513119076Sume#endif
514119076Sume			{
51555163Sshin				sigprocmask(SIG_BLOCK, &mask, &omask);
51655163Sshin				riprecv();
51755163Sshin				sigprocmask(SIG_SETMASK, &omask, NULL);
51855163Sshin			}
519119076Sume#ifdef HAVE_POLL_H
520119076Sume			if (set[1].revents & POLLIN)
521119076Sume#else
522119076Sume			if (FD_ISSET(rtsock, recvecp))
523119076Sume#endif
524119076Sume			{
52555163Sshin				sigprocmask(SIG_BLOCK, &mask, &omask);
52655163Sshin				rtrecv();
52755163Sshin				sigprocmask(SIG_SETMASK, &omask, NULL);
52855163Sshin			}
52955163Sshin		}
53055163Sshin	}
53155163Sshin}
53255163Sshin
53378064Sumevoid
534243232Shrssighandler(int signo)
53578064Sume{
53678064Sume
53778064Sume	switch (signo) {
53878064Sume	case SIGALRM:
53978064Sume		seenalrm++;
54078064Sume		break;
54178064Sume	case SIGQUIT:
54278064Sume	case SIGTERM:
54378064Sume		seenquit++;
54478064Sume		break;
54578064Sume	case SIGUSR1:
54678064Sume	case SIGHUP:
54778064Sume	case SIGINT:
54878064Sume		seenusr1++;
54978064Sume		break;
55078064Sume	}
55178064Sume}
55278064Sume
55355163Sshin/*
55455163Sshin * gracefully exits after resetting sockopts.
55555163Sshin */
55655163Sshin/* ARGSUSED */
55755163Sshinvoid
558243232Shrsrtdexit(void)
55955163Sshin{
56055163Sshin	struct	riprt *rrt;
56155163Sshin
56255163Sshin	alarm(0);
563243232Shrs	TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
56462607Sitojun		if (rrt->rrt_rflags & RRTF_AGGREGATE) {
56555163Sshin			delroute(&rrt->rrt_info, &rrt->rrt_gw);
56655163Sshin		}
56755163Sshin	}
56855163Sshin	close(ripsock);
56955163Sshin	close(rtsock);
57055163Sshin	syslog(LOG_INFO, "**** Terminated ****");
57155163Sshin	closelog();
57255163Sshin	exit(1);
57355163Sshin}
57455163Sshin
57555163Sshin/*
57655163Sshin * Called periodically:
57755163Sshin *	1. age out the learned route. remove it if necessary.
57855163Sshin *	2. submit RIP6_RESPONSE packets.
57978064Sume * Invoked in every SUPPLY_INTERVAL6 (30) seconds.  I believe we don't have
58055163Sshin * to invoke this function in every 1 or 5 or 10 seconds only to age the
58155163Sshin * routes more precisely.
58255163Sshin */
58355163Sshin/* ARGSUSED */
58455163Sshinvoid
585243232Shrsripalarm(void)
58655163Sshin{
58755163Sshin	struct	ifc *ifcp;
588243232Shrs	struct	riprt *rrt, *rrt_tmp;
58955163Sshin	time_t	t_lifetime, t_holddown;
59055163Sshin
59155163Sshin	/* age the RIP routes */
59255163Sshin	t_lifetime = time(NULL) - RIP_LIFETIME;
59355163Sshin	t_holddown = t_lifetime - RIP_HOLDDOWN;
594243232Shrs	TAILQ_FOREACH_SAFE(rrt, &riprt_head, rrt_next, rrt_tmp) {
595243232Shrs		if (rrt->rrt_t == 0)
59655163Sshin			continue;
597243232Shrs		else if (rrt->rrt_t < t_holddown) {
598243232Shrs			TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
59955163Sshin			delroute(&rrt->rrt_info, &rrt->rrt_gw);
60055163Sshin			free(rrt);
601243232Shrs		} else if (rrt->rrt_t < t_lifetime)
60255163Sshin			rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
60355163Sshin	}
60455163Sshin	/* Supply updates */
605243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
60655163Sshin		if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
60755163Sshin			ripsend(ifcp, &ifcp->ifc_ripsin, 0);
60855163Sshin	}
60955163Sshin	alarm(ripinterval(SUPPLY_INTERVAL6));
61055163Sshin}
61155163Sshin
61255163Sshinvoid
613243232Shrsinit(void)
61455163Sshin{
615119034Sume	int	error;
616119034Sume	const int int0 = 0, int1 = 1, int255 = 255;
61755163Sshin	struct	addrinfo hints, *res;
618119081Sume	char	port[NI_MAXSERV];
61955163Sshin
620243232Shrs	TAILQ_INIT(&ifc_head);
62155163Sshin	nindex2ifc = 0;	/*initial guess*/
62255163Sshin	index2ifc = NULL;
623119081Sume	snprintf(port, sizeof(port), "%u", RIP6_PORT);
62455163Sshin
62555163Sshin	memset(&hints, 0, sizeof(hints));
62655163Sshin	hints.ai_family = PF_INET6;
62755163Sshin	hints.ai_socktype = SOCK_DGRAM;
628119080Sume	hints.ai_protocol = IPPROTO_UDP;
62955163Sshin	hints.ai_flags = AI_PASSIVE;
63055163Sshin	error = getaddrinfo(NULL, port, &hints, &res);
63178064Sume	if (error) {
63266807Skris		fatal("%s", gai_strerror(error));
63378064Sume		/*NOTREACHED*/
63478064Sume	}
63578064Sume	if (res->ai_next) {
63655163Sshin		fatal(":: resolved to multiple address");
63778064Sume		/*NOTREACHED*/
63878064Sume	}
63955163Sshin
64055163Sshin	ripsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
64178064Sume	if (ripsock < 0) {
64255163Sshin		fatal("rip socket");
64378064Sume		/*NOTREACHED*/
64478064Sume	}
645119034Sume#ifdef IPV6_V6ONLY
646119034Sume	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_V6ONLY,
647119034Sume	    &int1, sizeof(int1)) < 0) {
648119034Sume		fatal("rip IPV6_V6ONLY");
649119034Sume		/*NOTREACHED*/
650119034Sume	}
651119034Sume#endif
65278064Sume	if (bind(ripsock, res->ai_addr, res->ai_addrlen) < 0) {
65355163Sshin		fatal("rip bind");
65478064Sume		/*NOTREACHED*/
65578064Sume	}
65655163Sshin	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
65778064Sume	    &int255, sizeof(int255)) < 0) {
65855163Sshin		fatal("rip IPV6_MULTICAST_HOPS");
65978064Sume		/*NOTREACHED*/
66078064Sume	}
66155163Sshin	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
66278064Sume	    &int0, sizeof(int0)) < 0) {
66355163Sshin		fatal("rip IPV6_MULTICAST_LOOP");
66478064Sume		/*NOTREACHED*/
66578064Sume	}
66662921Sume
66762607Sitojun#ifdef IPV6_RECVPKTINFO
668119034Sume	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVPKTINFO,
669119034Sume	    &int1, sizeof(int1)) < 0) {
67062607Sitojun		fatal("rip IPV6_RECVPKTINFO");
67178064Sume		/*NOTREACHED*/
67278064Sume	}
67362607Sitojun#else  /* old adv. API */
674119034Sume	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_PKTINFO,
675119034Sume	    &int1, sizeof(int1)) < 0) {
67655163Sshin		fatal("rip IPV6_PKTINFO");
67778064Sume		/*NOTREACHED*/
67878064Sume	}
67962607Sitojun#endif
68055163Sshin
681164339Ssuz#ifdef IPV6_RECVPKTINFO
682164339Ssuz	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
683164339Ssuz	    &int1, sizeof(int1)) < 0) {
684164339Ssuz		fatal("rip IPV6_RECVHOPLIMIT");
685164339Ssuz		/*NOTREACHED*/
686164339Ssuz	}
687164339Ssuz#else  /* old adv. API */
688164339Ssuz	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_HOPLIMIT,
689164339Ssuz	    &int1, sizeof(int1)) < 0) {
690164339Ssuz		fatal("rip IPV6_HOPLIMIT");
691164339Ssuz		/*NOTREACHED*/
692164339Ssuz	}
693164339Ssuz#endif
694164339Ssuz
69555163Sshin	memset(&hints, 0, sizeof(hints));
69655163Sshin	hints.ai_family = PF_INET6;
69755163Sshin	hints.ai_socktype = SOCK_DGRAM;
698119080Sume	hints.ai_protocol = IPPROTO_UDP;
69955163Sshin	error = getaddrinfo(RIP6_DEST, port, &hints, &res);
70078064Sume	if (error) {
70166807Skris		fatal("%s", gai_strerror(error));
70278064Sume		/*NOTREACHED*/
70378064Sume	}
70478064Sume	if (res->ai_next) {
70555163Sshin		fatal("%s resolved to multiple address", RIP6_DEST);
70678064Sume		/*NOTREACHED*/
70778064Sume	}
70855163Sshin	memcpy(&ripsin, res->ai_addr, res->ai_addrlen);
70955163Sshin
710119076Sume#ifdef HAVE_POLL_H
711119076Sume	set[0].fd = ripsock;
712119076Sume	set[0].events = POLLIN;
713119076Sume#else
714119070Sume	maxfd = ripsock;
715119076Sume#endif
71655163Sshin
71755163Sshin	if (nflag == 0) {
71878064Sume		if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
71955163Sshin			fatal("route socket");
72078064Sume			/*NOTREACHED*/
72178064Sume		}
722119076Sume#ifdef HAVE_POLL_H
723119076Sume		set[1].fd = rtsock;
724119076Sume		set[1].events = POLLIN;
725119076Sume#else
726119070Sume		if (rtsock > maxfd)
727119070Sume			maxfd = rtsock;
728119076Sume#endif
729119076Sume	} else {
730119076Sume#ifdef HAVE_POLL_H
731119076Sume		set[1].fd = -1;
732119076Sume#else
73355163Sshin		rtsock = -1;	/*just for safety */
734119076Sume#endif
735119076Sume	}
736119070Sume
737119076Sume#ifndef HAVE_POLL_H
738119070Sume	fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
739119070Sume	if ((sockvecp = malloc(fdmasks)) == NULL) {
740119070Sume		fatal("malloc");
741119070Sume		/*NOTREACHED*/
742119070Sume	}
743119070Sume	if ((recvecp = malloc(fdmasks)) == NULL) {
744119070Sume		fatal("malloc");
745119070Sume		/*NOTREACHED*/
746119070Sume	}
747119070Sume	memset(sockvecp, 0, fdmasks);
748119070Sume	FD_SET(ripsock, sockvecp);
749119070Sume	if (rtsock >= 0)
750119070Sume		FD_SET(rtsock, sockvecp);
751119076Sume#endif
75255163Sshin}
75355163Sshin
75462607Sitojun#define	RIPSIZE(n) \
75562607Sitojun	(sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6))
75655163Sshin
75755163Sshin/*
75855163Sshin * ripflush flushes the rip datagram stored in the rip buffer
75955163Sshin */
76055163Sshinvoid
761243232Shrsripflush(struct ifc *ifcp, struct sockaddr_in6 *sin6, int nrt, struct netinfo6 *np)
76255163Sshin{
76355163Sshin	int i;
76455163Sshin	int error;
76555163Sshin
76655163Sshin	if (ifcp)
76755163Sshin		tracet(1, "Send(%s): info(%d) to %s.%d\n",
76855163Sshin			ifcp->ifc_name, nrt,
769119031Sume			inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
77055163Sshin	else
77155163Sshin		tracet(1, "Send: info(%d) to %s.%d\n",
772119031Sume			nrt, inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
77355163Sshin	if (dflag >= 2) {
77455163Sshin		np = ripbuf->rip6_nets;
77555163Sshin		for (i = 0; i < nrt; i++, np++) {
77655163Sshin			if (np->rip6_metric == NEXTHOP_METRIC) {
77755163Sshin				if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest))
77862607Sitojun					trace(2, "    NextHop reset");
77955163Sshin				else {
78055163Sshin					trace(2, "    NextHop %s",
78155163Sshin						inet6_n2p(&np->rip6_dest));
78255163Sshin				}
78355163Sshin			} else {
78455163Sshin				trace(2, "    %s/%d[%d]",
78555163Sshin					inet6_n2p(&np->rip6_dest),
78655163Sshin					np->rip6_plen, np->rip6_metric);
78755163Sshin			}
78855163Sshin			if (np->rip6_tag) {
78955163Sshin				trace(2, "  tag=0x%04x",
79055163Sshin					ntohs(np->rip6_tag) & 0xffff);
79155163Sshin			}
79255163Sshin			trace(2, "\n");
79355163Sshin		}
79455163Sshin	}
795119031Sume	error = sendpacket(sin6, RIPSIZE(nrt));
79655163Sshin	if (error == EAFNOSUPPORT) {
79755163Sshin		/* Protocol not supported */
79855163Sshin		tracet(1, "Could not send info to %s (%s): "
79955163Sshin			"set IFF_UP to 0\n",
80055163Sshin			ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
80155163Sshin		ifcp->ifc_flags &= ~IFF_UP;	/* As if down for AF_INET6 */
80255163Sshin	}
80355163Sshin}
80455163Sshin
80555163Sshin/*
80655163Sshin * Generate RIP6_RESPONSE packets and send them.
80755163Sshin */
80855163Sshinvoid
809243232Shrsripsend(struct	ifc *ifcp, struct sockaddr_in6 *sin6, int flag)
81055163Sshin{
81155163Sshin	struct	riprt *rrt;
81255163Sshin	struct	in6_addr *nh;	/* next hop */
813243232Shrs	struct netinfo6 *np;
81478064Sume	int	maxrte;
815243232Shrs	int nrt;
81655163Sshin
817119084Sume	if (qflag)
818119084Sume		return;
819119084Sume
82055163Sshin	if (ifcp == NULL) {
82155163Sshin		/*
82255163Sshin		 * Request from non-link local address is not
82355163Sshin		 * a regular route6d update.
82455163Sshin		 */
82562607Sitojun		maxrte = (IFMINMTU - sizeof(struct ip6_hdr) -
82662607Sitojun				sizeof(struct udphdr) -
82755163Sshin				sizeof(struct rip6) + sizeof(struct netinfo6)) /
82855163Sshin				sizeof(struct netinfo6);
829243232Shrs		nh = NULL;
830243232Shrs		nrt = 0;
831243232Shrs		np = ripbuf->rip6_nets;
832243232Shrs		TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
83362607Sitojun			if (rrt->rrt_rflags & RRTF_NOADVERTISE)
83455163Sshin				continue;
83555163Sshin			/* Put the route to the buffer */
83655163Sshin			*np = rrt->rrt_info;
83755163Sshin			np++; nrt++;
83855163Sshin			if (nrt == maxrte) {
839243232Shrs				ripflush(NULL, sin6, nrt, np);
84055163Sshin				nh = NULL;
841243232Shrs				nrt = 0;
842243232Shrs				np = ripbuf->rip6_nets;
84355163Sshin			}
84455163Sshin		}
84555163Sshin		if (nrt)	/* Send last packet */
846243232Shrs			ripflush(NULL, sin6, nrt, np);
84755163Sshin		return;
84855163Sshin	}
84955163Sshin
85062607Sitojun	if ((flag & RRTF_SENDANYWAY) == 0 &&
85155163Sshin	    (qflag || (ifcp->ifc_flags & IFF_LOOPBACK)))
85255163Sshin		return;
85378064Sume
85478064Sume	/* -N: no use */
855243232Shrs	if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
85655163Sshin		return;
85778064Sume
85878064Sume	/* -T: generate default route only */
859243232Shrs	if (iff_find(ifcp, IFIL_TYPE_T) != NULL) {
86055163Sshin		struct netinfo6 rrt_info;
86155163Sshin		memset(&rrt_info, 0, sizeof(struct netinfo6));
86255163Sshin		rrt_info.rip6_dest = in6addr_any;
86355163Sshin		rrt_info.rip6_plen = 0;
86455163Sshin		rrt_info.rip6_metric = 1;
86578064Sume		rrt_info.rip6_metric += ifcp->ifc_metric;
86655163Sshin		rrt_info.rip6_tag = htons(routetag & 0xffff);
86755163Sshin		np = ripbuf->rip6_nets;
86855163Sshin		*np = rrt_info;
86955163Sshin		nrt = 1;
870243232Shrs		ripflush(ifcp, sin6, nrt, np);
87155163Sshin		return;
87255163Sshin	}
87378064Sume
87462607Sitojun	maxrte = (ifcp->ifc_mtu - sizeof(struct ip6_hdr) -
87562607Sitojun			sizeof(struct udphdr) -
87655163Sshin			sizeof(struct rip6) + sizeof(struct netinfo6)) /
87755163Sshin			sizeof(struct netinfo6);
87878064Sume
87955163Sshin	nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
880243232Shrs	TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
88162607Sitojun		if (rrt->rrt_rflags & RRTF_NOADVERTISE)
88255163Sshin			continue;
88378064Sume
88478064Sume		/* Need to check filter here */
88578064Sume		if (out_filter(rrt, ifcp) == 0)
88655163Sshin			continue;
88778064Sume
88855163Sshin		/* Check split horizon and other conditions */
88955163Sshin		if (tobeadv(rrt, ifcp) == 0)
89055163Sshin			continue;
89178064Sume
89255163Sshin		/* Only considers the routes with flag if specified */
89362607Sitojun		if ((flag & RRTF_CHANGED) &&
89462607Sitojun		    (rrt->rrt_rflags & RRTF_CHANGED) == 0)
89555163Sshin			continue;
89678064Sume
89755163Sshin		/* Check nexthop */
89855163Sshin		if (rrt->rrt_index == ifcp->ifc_index &&
89955163Sshin		    !IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_gw) &&
90062607Sitojun		    (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR) == 0) {
90155163Sshin			if (nh == NULL || !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw)) {
902243232Shrs				if (nrt == maxrte - 2) {
903243232Shrs					ripflush(ifcp, sin6, nrt, np);
904243232Shrs					nh = NULL;
905243232Shrs					nrt = 0;
906243232Shrs					np = ripbuf->rip6_nets;
907243232Shrs				}
908243232Shrs
90955163Sshin				np->rip6_dest = rrt->rrt_gw;
91055163Sshin				np->rip6_plen = 0;
91155163Sshin				np->rip6_tag = 0;
91255163Sshin				np->rip6_metric = NEXTHOP_METRIC;
91355163Sshin				nh = &rrt->rrt_gw;
91455163Sshin				np++; nrt++;
91555163Sshin			}
91655163Sshin		} else if (nh && (rrt->rrt_index != ifcp->ifc_index ||
91755163Sshin			          !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw) ||
91862607Sitojun				  rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)) {
91955163Sshin			/* Reset nexthop */
920243232Shrs			if (nrt == maxrte - 2) {
921243232Shrs				ripflush(ifcp, sin6, nrt, np);
922243232Shrs				nh = NULL;
923243232Shrs				nrt = 0;
924243232Shrs				np = ripbuf->rip6_nets;
925243232Shrs			}
92655163Sshin			memset(np, 0, sizeof(struct netinfo6));
92755163Sshin			np->rip6_metric = NEXTHOP_METRIC;
92855163Sshin			nh = NULL;
92955163Sshin			np++; nrt++;
93055163Sshin		}
93178064Sume
93255163Sshin		/* Put the route to the buffer */
93355163Sshin		*np = rrt->rrt_info;
93455163Sshin		np++; nrt++;
93555163Sshin		if (nrt == maxrte) {
936243232Shrs			ripflush(ifcp, sin6, nrt, np);
93755163Sshin			nh = NULL;
938243232Shrs			nrt = 0;
939243232Shrs			np = ripbuf->rip6_nets;
94055163Sshin		}
94155163Sshin	}
94255163Sshin	if (nrt)	/* Send last packet */
943243232Shrs		ripflush(ifcp, sin6, nrt, np);
94455163Sshin}
94555163Sshin
94655163Sshin/*
94778064Sume * outbound filter logic, per-route/interface.
94878064Sume */
94978064Sumeint
950243232Shrsout_filter(struct riprt *rrt, struct ifc *ifcp)
95178064Sume{
95278064Sume	struct iff *iffp;
95378064Sume	struct in6_addr ia;
95478064Sume	int ok;
95578064Sume
95678064Sume	/*
95778064Sume	 * -A: filter out less specific routes, if we have aggregated
95878064Sume	 * route configured.
95978064Sume	 */
960243232Shrs	TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
96178064Sume		if (iffp->iff_type != 'A')
96278064Sume			continue;
96378064Sume		if (rrt->rrt_info.rip6_plen <= iffp->iff_plen)
96478064Sume			continue;
96578064Sume		ia = rrt->rrt_info.rip6_dest;
96678064Sume		applyplen(&ia, iffp->iff_plen);
96778064Sume		if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr))
96878064Sume			return 0;
96978064Sume	}
97078064Sume
97178064Sume	/*
97278064Sume	 * if it is an aggregated route, advertise it only to the
97378064Sume	 * interfaces specified on -A.
97478064Sume	 */
97578064Sume	if ((rrt->rrt_rflags & RRTF_AGGREGATE) != 0) {
97678064Sume		ok = 0;
977243232Shrs		TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
97878064Sume			if (iffp->iff_type != 'A')
97978064Sume				continue;
98078064Sume			if (rrt->rrt_info.rip6_plen == iffp->iff_plen &&
98178064Sume			    IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
98278064Sume			    &iffp->iff_addr)) {
98378064Sume				ok = 1;
98478064Sume				break;
98578064Sume			}
98678064Sume		}
98778064Sume		if (!ok)
98878064Sume			return 0;
98978064Sume	}
99078064Sume
99178064Sume	/*
99278064Sume	 * -O: advertise only if prefix matches the configured prefix.
99378064Sume	 */
994243232Shrs	if (iff_find(ifcp, IFIL_TYPE_O) != NULL) {
99578064Sume		ok = 0;
996243232Shrs		TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
99778064Sume			if (iffp->iff_type != 'O')
99878064Sume				continue;
99978064Sume			if (rrt->rrt_info.rip6_plen < iffp->iff_plen)
100078064Sume				continue;
100178064Sume			ia = rrt->rrt_info.rip6_dest;
100278064Sume			applyplen(&ia, iffp->iff_plen);
100378064Sume			if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
100478064Sume				ok = 1;
100578064Sume				break;
100678064Sume			}
100778064Sume		}
100878064Sume		if (!ok)
100978064Sume			return 0;
101078064Sume	}
101178064Sume
101278064Sume	/* the prefix should be advertised */
101378064Sume	return 1;
101478064Sume}
101578064Sume
101678064Sume/*
101755163Sshin * Determine if the route is to be advertised on the specified interface.
101855163Sshin * It checks options specified in the arguments and the split horizon rule.
101955163Sshin */
102055163Sshinint
1021243232Shrstobeadv(struct riprt *rrt, struct ifc *ifcp)
102255163Sshin{
102355163Sshin
102455163Sshin	/* Special care for static routes */
102555163Sshin	if (rrt->rrt_flags & RTF_STATIC) {
102662607Sitojun		/* XXX don't advertise reject/blackhole routes */
102762607Sitojun		if (rrt->rrt_flags & (RTF_REJECT | RTF_BLACKHOLE))
102862607Sitojun			return 0;
102962607Sitojun
103055163Sshin		if (Sflag)	/* Yes, advertise it anyway */
103155163Sshin			return 1;
103255163Sshin		if (sflag && rrt->rrt_index != ifcp->ifc_index)
103355163Sshin			return 1;
103455163Sshin		return 0;
103555163Sshin	}
103655163Sshin	/* Regular split horizon */
103755163Sshin	if (hflag == 0 && rrt->rrt_index == ifcp->ifc_index)
103855163Sshin		return 0;
103955163Sshin	return 1;
104055163Sshin}
104155163Sshin
104255163Sshin/*
104355163Sshin * Send a rip packet actually.
104455163Sshin */
104555163Sshinint
1046243232Shrssendpacket(struct sockaddr_in6 *sin6, int len)
104755163Sshin{
104855163Sshin	struct msghdr m;
104955163Sshin	struct cmsghdr *cm;
105055163Sshin	struct iovec iov[2];
105155163Sshin	u_char cmsgbuf[256];
105255163Sshin	struct in6_pktinfo *pi;
105378064Sume	int idx;
105455163Sshin	struct sockaddr_in6 sincopy;
105555163Sshin
105655163Sshin	/* do not overwrite the given sin */
1057119031Sume	sincopy = *sin6;
1058119031Sume	sin6 = &sincopy;
105955163Sshin
1060119035Sume	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
1061243231Shrs	    IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1062243231Shrs		idx = sin6->sin6_scope_id;
1063243231Shrs	else
106478064Sume		idx = 0;
106555163Sshin
1066119031Sume	m.msg_name = (caddr_t)sin6;
1067119031Sume	m.msg_namelen = sizeof(*sin6);
106855163Sshin	iov[0].iov_base = (caddr_t)ripbuf;
106955163Sshin	iov[0].iov_len = len;
107055163Sshin	m.msg_iov = iov;
107155163Sshin	m.msg_iovlen = 1;
107278064Sume	if (!idx) {
107355163Sshin		m.msg_control = NULL;
107455163Sshin		m.msg_controllen = 0;
107555163Sshin	} else {
107655163Sshin		memset(cmsgbuf, 0, sizeof(cmsgbuf));
107755163Sshin		cm = (struct cmsghdr *)cmsgbuf;
107855163Sshin		m.msg_control = (caddr_t)cm;
107955163Sshin		m.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
108055163Sshin
108155163Sshin		cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
108255163Sshin		cm->cmsg_level = IPPROTO_IPV6;
108355163Sshin		cm->cmsg_type = IPV6_PKTINFO;
108455163Sshin		pi = (struct in6_pktinfo *)CMSG_DATA(cm);
108555163Sshin		memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*::*/
108678064Sume		pi->ipi6_ifindex = idx;
108755163Sshin	}
108855163Sshin
108955163Sshin	if (sendmsg(ripsock, &m, 0 /*MSG_DONTROUTE*/) < 0) {
109055163Sshin		trace(1, "sendmsg: %s\n", strerror(errno));
109155163Sshin		return errno;
109255163Sshin	}
109362921Sume
109455163Sshin	return 0;
109555163Sshin}
109655163Sshin
109755163Sshin/*
109878064Sume * Receive and process RIP packets.  Update the routes/kernel forwarding
109955163Sshin * table if necessary.
110055163Sshin */
110155163Sshinvoid
1102243232Shrsriprecv(void)
110355163Sshin{
110455163Sshin	struct	ifc *ifcp, *ic;
110555163Sshin	struct	sockaddr_in6 fsock;
110655163Sshin	struct	in6_addr nh;	/* next hop */
110755163Sshin	struct	rip6 *rp;
110855163Sshin	struct	netinfo6 *np, *nq;
110955163Sshin	struct	riprt *rrt;
1110122677Sume	ssize_t	len, nn;
1111122677Sume	unsigned int need_trigger, idx;
111255163Sshin	char	buf[4 * RIP6_MAXMTU];
111355163Sshin	time_t	t;
111455163Sshin	struct msghdr m;
111555163Sshin	struct cmsghdr *cm;
111655163Sshin	struct iovec iov[2];
111755163Sshin	u_char cmsgbuf[256];
1118164339Ssuz	struct in6_pktinfo *pi = NULL;
1119164339Ssuz	int *hlimp = NULL;
112055163Sshin	struct iff *iffp;
112155163Sshin	struct in6_addr ia;
112255163Sshin	int ok;
112378064Sume	time_t t_half_lifetime;
112455163Sshin
112555163Sshin	need_trigger = 0;
112662921Sume
112755163Sshin	m.msg_name = (caddr_t)&fsock;
112855163Sshin	m.msg_namelen = sizeof(fsock);
112955163Sshin	iov[0].iov_base = (caddr_t)buf;
113055163Sshin	iov[0].iov_len = sizeof(buf);
113155163Sshin	m.msg_iov = iov;
113255163Sshin	m.msg_iovlen = 1;
113355163Sshin	cm = (struct cmsghdr *)cmsgbuf;
113455163Sshin	m.msg_control = (caddr_t)cm;
113555163Sshin	m.msg_controllen = sizeof(cmsgbuf);
113678064Sume	if ((len = recvmsg(ripsock, &m, 0)) < 0) {
113755163Sshin		fatal("recvmsg");
113878064Sume		/*NOTREACHED*/
113978064Sume	}
114078064Sume	idx = 0;
114155163Sshin	for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&m);
114255163Sshin	     cm;
114355163Sshin	     cm = (struct cmsghdr *)CMSG_NXTHDR(&m, cm)) {
1144164339Ssuz		if (cm->cmsg_level != IPPROTO_IPV6)
1145164339Ssuz		    continue;
1146164339Ssuz		switch (cm->cmsg_type) {
1147164339Ssuz		case IPV6_PKTINFO:
1148164339Ssuz			if (cm->cmsg_len != CMSG_LEN(sizeof(*pi))) {
1149164339Ssuz				trace(1,
1150164339Ssuz				    "invalid cmsg length for IPV6_PKTINFO\n");
1151164339Ssuz				return;
1152164339Ssuz			}
115355163Sshin			pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
115478064Sume			idx = pi->ipi6_ifindex;
115555163Sshin			break;
1156164339Ssuz		case IPV6_HOPLIMIT:
1157164339Ssuz			if (cm->cmsg_len != CMSG_LEN(sizeof(int))) {
1158164339Ssuz				trace(1,
1159164339Ssuz				    "invalid cmsg length for IPV6_HOPLIMIT\n");
1160164339Ssuz				return;
1161164339Ssuz			}
1162164339Ssuz			hlimp = (int *)CMSG_DATA(cm);
1163164339Ssuz			break;
116455163Sshin		}
116555163Sshin	}
116655163Sshin
1167243232Shrs	if ((size_t)len < sizeof(struct rip6)) {
1168121779Ssuz		trace(1, "Packet too short\n");
1169121779Ssuz		return;
1170121779Ssuz	}
1171121779Ssuz
1172164339Ssuz	if (pi == NULL || hlimp == NULL) {
1173164339Ssuz		/*
1174164339Ssuz		 * This can happen when the kernel failed to allocate memory
1175164339Ssuz		 * for the ancillary data.  Although we might be able to handle
1176164339Ssuz		 * some cases without this info, those are minor and not so
1177164339Ssuz		 * important, so it's better to discard the packet for safer
1178164339Ssuz		 * operation.
1179164339Ssuz		 */
1180164339Ssuz		trace(1, "IPv6 packet information cannot be retrieved\n");
1181164339Ssuz		return;
1182164339Ssuz	}
1183164339Ssuz
118455163Sshin	nh = fsock.sin6_addr;
118555163Sshin	nn = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
118655163Sshin		sizeof(struct netinfo6);
118755163Sshin	rp = (struct rip6 *)buf;
118855163Sshin	np = rp->rip6_nets;
118955163Sshin
1190119035Sume	if (rp->rip6_vers != RIP6_VERSION) {
119155163Sshin		trace(1, "Incorrect RIP version %d\n", rp->rip6_vers);
119255163Sshin		return;
119355163Sshin	}
119455163Sshin	if (rp->rip6_cmd == RIP6_REQUEST) {
119578064Sume		if (idx && idx < nindex2ifc) {
119678064Sume			ifcp = index2ifc[idx];
119755163Sshin			riprequest(ifcp, np, nn, &fsock);
119855163Sshin		} else {
119955163Sshin			riprequest(NULL, np, nn, &fsock);
120055163Sshin		}
120162607Sitojun		return;
120262607Sitojun	}
120355163Sshin
120455163Sshin	if (!IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr)) {
1205164339Ssuz		trace(1, "Response from non-ll addr: %s\n",
120678064Sume		    inet6_n2p(&fsock.sin6_addr));
120755163Sshin		return;		/* Ignore packets from non-link-local addr */
120855163Sshin	}
1209164339Ssuz	if (ntohs(fsock.sin6_port) != RIP6_PORT) {
1210164339Ssuz		trace(1, "Response from non-rip port from %s\n",
1211164339Ssuz		    inet6_n2p(&fsock.sin6_addr));
1212164339Ssuz		return;
1213164339Ssuz	}
1214164339Ssuz	if (IN6_IS_ADDR_MULTICAST(&pi->ipi6_addr) && *hlimp != 255) {
1215164339Ssuz		trace(1,
1216164339Ssuz		    "Response packet with a smaller hop limit (%d) from %s\n",
1217164339Ssuz		    *hlimp, inet6_n2p(&fsock.sin6_addr));
1218164339Ssuz		return;
1219164339Ssuz	}
1220164339Ssuz	/*
1221164339Ssuz	 * Further validation: since this program does not send off-link
1222164339Ssuz	 * requests, an incoming response must always come from an on-link
1223164339Ssuz	 * node.  Although this is normally ensured by the source address
1224164339Ssuz	 * check above, it may not 100% be safe because there are router
1225164339Ssuz	 * implementations that (invalidly) allow a packet with a link-local
1226164339Ssuz	 * source address to be forwarded to a different link.
1227164339Ssuz	 * So we also check whether the destination address is a link-local
1228164339Ssuz	 * address or the hop limit is 255.  Note that RFC2080 does not require
1229164339Ssuz	 * the specific hop limit for a unicast response, so we cannot assume
1230164339Ssuz	 * the limitation.
1231164339Ssuz	 */
1232164339Ssuz	if (!IN6_IS_ADDR_LINKLOCAL(&pi->ipi6_addr) && *hlimp != 255) {
1233164339Ssuz		trace(1,
1234164339Ssuz		    "Response packet possibly from an off-link node: "
1235164339Ssuz		    "from %s to %s hlim=%d\n",
1236164339Ssuz		    inet6_n2p(&fsock.sin6_addr),
1237164339Ssuz		    inet6_n2p(&pi->ipi6_addr), *hlimp);
1238164339Ssuz		return;
1239164339Ssuz	}
1240164339Ssuz
1241243231Shrs	idx = fsock.sin6_scope_id;
124278064Sume	ifcp = (idx < nindex2ifc) ? index2ifc[idx] : NULL;
124355163Sshin	if (!ifcp) {
124478064Sume		trace(1, "Packets to unknown interface index %d\n", idx);
124555163Sshin		return;		/* Ignore it */
124655163Sshin	}
124755163Sshin	if (IN6_ARE_ADDR_EQUAL(&ifcp->ifc_mylladdr, &fsock.sin6_addr))
124855163Sshin		return;		/* The packet is from me; ignore */
124955163Sshin	if (rp->rip6_cmd != RIP6_RESPONSE) {
125055163Sshin		trace(1, "Invalid command %d\n", rp->rip6_cmd);
125162607Sitojun		return;
125255163Sshin	}
125378064Sume
125478064Sume	/* -N: no use */
1255243232Shrs	if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
125655163Sshin		return;
125778064Sume
1258228674Sdim	tracet(1, "Recv(%s): from %s.%d info(%zd)\n",
125978064Sume	    ifcp->ifc_name, inet6_n2p(&nh), ntohs(fsock.sin6_port), nn);
126055163Sshin
126155163Sshin	t = time(NULL);
126278064Sume	t_half_lifetime = t - (RIP_LIFETIME/2);
126355163Sshin	for (; nn; nn--, np++) {
126455163Sshin		if (np->rip6_metric == NEXTHOP_METRIC) {
126555163Sshin			/* modify neighbor address */
126655163Sshin			if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
126755163Sshin				nh = np->rip6_dest;
126855163Sshin				trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
126955163Sshin			} else if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest)) {
127055163Sshin				nh = fsock.sin6_addr;
127155163Sshin				trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
127255163Sshin			} else {
127355163Sshin				nh = fsock.sin6_addr;
127455163Sshin				trace(1, "\tInvalid Nexthop: %s\n",
127578064Sume				    inet6_n2p(&np->rip6_dest));
127655163Sshin			}
127755163Sshin			continue;
127855163Sshin		}
127955163Sshin		if (IN6_IS_ADDR_MULTICAST(&np->rip6_dest)) {
128055163Sshin			trace(1, "\tMulticast netinfo6: %s/%d [%d]\n",
128155163Sshin				inet6_n2p(&np->rip6_dest),
128255163Sshin				np->rip6_plen, np->rip6_metric);
128355163Sshin			continue;
128455163Sshin		}
128555163Sshin		if (IN6_IS_ADDR_LOOPBACK(&np->rip6_dest)) {
128655163Sshin			trace(1, "\tLoopback netinfo6: %s/%d [%d]\n",
128755163Sshin				inet6_n2p(&np->rip6_dest),
128855163Sshin				np->rip6_plen, np->rip6_metric);
128955163Sshin			continue;
129055163Sshin		}
129155163Sshin		if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
129255163Sshin			trace(1, "\tLink Local netinfo6: %s/%d [%d]\n",
129355163Sshin				inet6_n2p(&np->rip6_dest),
129455163Sshin				np->rip6_plen, np->rip6_metric);
129555163Sshin			continue;
129655163Sshin		}
129755163Sshin		/* may need to pass sitelocal prefix in some case, however*/
129855163Sshin		if (IN6_IS_ADDR_SITELOCAL(&np->rip6_dest) && !lflag) {
129955163Sshin			trace(1, "\tSite Local netinfo6: %s/%d [%d]\n",
130055163Sshin				inet6_n2p(&np->rip6_dest),
130155163Sshin				np->rip6_plen, np->rip6_metric);
130255163Sshin			continue;
130355163Sshin		}
130455163Sshin		trace(2, "\tnetinfo6: %s/%d [%d]",
130555163Sshin			inet6_n2p(&np->rip6_dest),
130655163Sshin			np->rip6_plen, np->rip6_metric);
130755163Sshin		if (np->rip6_tag)
130855163Sshin			trace(2, "  tag=0x%04x", ntohs(np->rip6_tag) & 0xffff);
130962607Sitojun		if (dflag >= 2) {
131062607Sitojun			ia = np->rip6_dest;
131162607Sitojun			applyplen(&ia, np->rip6_plen);
131262607Sitojun			if (!IN6_ARE_ADDR_EQUAL(&ia, &np->rip6_dest))
131362607Sitojun				trace(2, " [junk outside prefix]");
131462607Sitojun		}
131555163Sshin
131678064Sume		/*
131778064Sume		 * -L: listen only if the prefix matches the configuration
131878064Sume		 */
1319243232Shrs                ok = 1;	/* if there's no L filter, it is ok */
1320243232Shrs                TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
1321243232Shrs                        if (iffp->iff_type != IFIL_TYPE_L)
1322243232Shrs                                continue;
1323243232Shrs                        ok = 0;
1324243232Shrs                        if (np->rip6_plen < iffp->iff_plen)
1325243232Shrs                                continue;
1326243232Shrs                        /* special rule: ::/0 means default, not "in /0" */
1327243232Shrs                        if (iffp->iff_plen == 0 && np->rip6_plen > 0)
1328243232Shrs                                continue;
1329243232Shrs                        ia = np->rip6_dest;
1330243232Shrs                        applyplen(&ia, iffp->iff_plen);
1331243232Shrs                        if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1332243232Shrs                                ok = 1;
1333243232Shrs                                break;
1334243232Shrs                        }
1335243232Shrs                }
133655163Sshin		if (!ok) {
133755163Sshin			trace(2, "  (filtered)\n");
133855163Sshin			continue;
133955163Sshin		}
134055163Sshin
134155163Sshin		trace(2, "\n");
134255163Sshin		np->rip6_metric++;
134355163Sshin		np->rip6_metric += ifcp->ifc_metric;
134455163Sshin		if (np->rip6_metric > HOPCNT_INFINITY6)
134555163Sshin			np->rip6_metric = HOPCNT_INFINITY6;
134655163Sshin
134755163Sshin		applyplen(&np->rip6_dest, np->rip6_plen);
1348243232Shrs		if ((rrt = rtsearch(np)) != NULL) {
134955163Sshin			if (rrt->rrt_t == 0)
135055163Sshin				continue;	/* Intf route has priority */
135155163Sshin			nq = &rrt->rrt_info;
135255163Sshin			if (nq->rip6_metric > np->rip6_metric) {
135355163Sshin				if (rrt->rrt_index == ifcp->ifc_index &&
135455163Sshin				    IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
135555163Sshin					/* Small metric from the same gateway */
135655163Sshin					nq->rip6_metric = np->rip6_metric;
135755163Sshin				} else {
135855163Sshin					/* Better route found */
135955163Sshin					rrt->rrt_index = ifcp->ifc_index;
136055163Sshin					/* Update routing table */
136155163Sshin					delroute(nq, &rrt->rrt_gw);
136255163Sshin					rrt->rrt_gw = nh;
136355163Sshin					*nq = *np;
136455163Sshin					addroute(rrt, &nh, ifcp);
136555163Sshin				}
136662607Sitojun				rrt->rrt_rflags |= RRTF_CHANGED;
136755163Sshin				rrt->rrt_t = t;
136855163Sshin				need_trigger = 1;
136955163Sshin			} else if (nq->rip6_metric < np->rip6_metric &&
137055163Sshin				   rrt->rrt_index == ifcp->ifc_index &&
137155163Sshin				   IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
137255163Sshin				/* Got worse route from same gw */
137355163Sshin				nq->rip6_metric = np->rip6_metric;
137455163Sshin				rrt->rrt_t = t;
137562607Sitojun				rrt->rrt_rflags |= RRTF_CHANGED;
137655163Sshin				need_trigger = 1;
137755163Sshin			} else if (nq->rip6_metric == np->rip6_metric &&
137855163Sshin				   np->rip6_metric < HOPCNT_INFINITY6) {
137978064Sume				if (rrt->rrt_index == ifcp->ifc_index &&
138078064Sume				   IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
138178064Sume					/* same metric, same route from same gw */
138278064Sume					rrt->rrt_t = t;
138378064Sume				} else if (rrt->rrt_t < t_half_lifetime) {
138478064Sume					/* Better route found */
138578064Sume					rrt->rrt_index = ifcp->ifc_index;
138678064Sume					/* Update routing table */
138778064Sume					delroute(nq, &rrt->rrt_gw);
138878064Sume					rrt->rrt_gw = nh;
138978064Sume					*nq = *np;
139078064Sume					addroute(rrt, &nh, ifcp);
139178064Sume					rrt->rrt_rflags |= RRTF_CHANGED;
139278064Sume					rrt->rrt_t = t;
139378064Sume				}
139455163Sshin			}
139562607Sitojun			/*
139655163Sshin			 * if nq->rip6_metric == HOPCNT_INFINITY6 then
139778064Sume			 * do not update age value.  Do nothing.
139855163Sshin			 */
139955163Sshin		} else if (np->rip6_metric < HOPCNT_INFINITY6) {
140055163Sshin			/* Got a new valid route */
140178064Sume			if ((rrt = MALLOC(struct riprt)) == NULL) {
140255163Sshin				fatal("malloc: struct riprt");
140378064Sume				/*NOTREACHED*/
140478064Sume			}
140562607Sitojun			memset(rrt, 0, sizeof(*rrt));
140655163Sshin			nq = &rrt->rrt_info;
140755163Sshin
140855163Sshin			rrt->rrt_same = NULL;
140955163Sshin			rrt->rrt_index = ifcp->ifc_index;
141055163Sshin			rrt->rrt_flags = RTF_UP|RTF_GATEWAY;
141155163Sshin			rrt->rrt_gw = nh;
141255163Sshin			*nq = *np;
141355163Sshin			applyplen(&nq->rip6_dest, nq->rip6_plen);
141455163Sshin			if (nq->rip6_plen == sizeof(struct in6_addr) * 8)
141555163Sshin				rrt->rrt_flags |= RTF_HOST;
141655163Sshin
141755163Sshin			/* Update routing table */
141855163Sshin			addroute(rrt, &nh, ifcp);
141962607Sitojun			rrt->rrt_rflags |= RRTF_CHANGED;
142055163Sshin			need_trigger = 1;
142155163Sshin			rrt->rrt_t = t;
1422243232Shrs
1423243232Shrs			/* Put the route to the list */
1424243232Shrs			TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
142555163Sshin		}
142655163Sshin	}
142755163Sshin	/* XXX need to care the interval between triggered updates */
142855163Sshin	if (need_trigger) {
142955163Sshin		if (nextalarm > time(NULL) + RIP_TRIG_INT6_MAX) {
1430243232Shrs			TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
143155163Sshin				if (ifcp->ifc_index == ic->ifc_index)
143255163Sshin					continue;
143355163Sshin				if (ic->ifc_flags & IFF_UP)
143455163Sshin					ripsend(ic, &ic->ifc_ripsin,
143562607Sitojun						RRTF_CHANGED);
143655163Sshin			}
143755163Sshin		}
143855163Sshin		/* Reset the flag */
1439243232Shrs		TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
144062607Sitojun			rrt->rrt_rflags &= ~RRTF_CHANGED;
1441243232Shrs		}
144255163Sshin	}
144355163Sshin}
144455163Sshin
144555163Sshin/*
144655163Sshin * Send all routes request packet to the specified interface.
144755163Sshin */
144855163Sshinvoid
1449243232Shrssendrequest(struct ifc *ifcp)
145055163Sshin{
145155163Sshin	struct netinfo6 *np;
145255163Sshin	int error;
145355163Sshin
145455163Sshin	if (ifcp->ifc_flags & IFF_LOOPBACK)
145555163Sshin		return;
145655163Sshin	ripbuf->rip6_cmd = RIP6_REQUEST;
145755163Sshin	np = ripbuf->rip6_nets;
145855163Sshin	memset(np, 0, sizeof(struct netinfo6));
145955163Sshin	np->rip6_metric = HOPCNT_INFINITY6;
146055163Sshin	tracet(1, "Send rtdump Request to %s (%s)\n",
146155163Sshin		ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
146255163Sshin	error = sendpacket(&ifcp->ifc_ripsin, RIPSIZE(1));
146355163Sshin	if (error == EAFNOSUPPORT) {
146455163Sshin		/* Protocol not supported */
146555163Sshin		tracet(1, "Could not send rtdump Request to %s (%s): "
146655163Sshin			"set IFF_UP to 0\n",
146755163Sshin			ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
146855163Sshin		ifcp->ifc_flags &= ~IFF_UP;	/* As if down for AF_INET6 */
146955163Sshin	}
147055163Sshin	ripbuf->rip6_cmd = RIP6_RESPONSE;
147155163Sshin}
147255163Sshin
147355163Sshin/*
147455163Sshin * Process a RIP6_REQUEST packet.
147555163Sshin */
147655163Sshinvoid
1477243232Shrsriprequest(struct ifc *ifcp,
1478243232Shrs	struct netinfo6 *np,
1479243232Shrs	int nn,
1480243232Shrs	struct sockaddr_in6 *sin6)
148155163Sshin{
148255163Sshin	int i;
148355163Sshin	struct riprt *rrt;
148455163Sshin
148555163Sshin	if (!(nn == 1 && IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest) &&
148655163Sshin	      np->rip6_plen == 0 && np->rip6_metric == HOPCNT_INFINITY6)) {
148755163Sshin		/* Specific response, don't split-horizon */
148855163Sshin		trace(1, "\tRIP Request\n");
148955163Sshin		for (i = 0; i < nn; i++, np++) {
1490243232Shrs			rrt = rtsearch(np);
149155163Sshin			if (rrt)
149255163Sshin				np->rip6_metric = rrt->rrt_info.rip6_metric;
149355163Sshin			else
149455163Sshin				np->rip6_metric = HOPCNT_INFINITY6;
149555163Sshin		}
1496119031Sume		(void)sendpacket(sin6, RIPSIZE(nn));
149755163Sshin		return;
149855163Sshin	}
149955163Sshin	/* Whole routing table dump */
150055163Sshin	trace(1, "\tRIP Request -- whole routing table\n");
1501119031Sume	ripsend(ifcp, sin6, RRTF_SENDANYWAY);
150255163Sshin}
150355163Sshin
150455163Sshin/*
150555163Sshin * Get information of each interface.
150655163Sshin */
150755163Sshinvoid
1508243232Shrsifconfig(void)
150955163Sshin{
151062607Sitojun	struct ifaddrs *ifap, *ifa;
151162607Sitojun	struct ifc *ifcp;
151262607Sitojun	struct ipv6_mreq mreq;
151362607Sitojun	int s;
151462607Sitojun
151578064Sume	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
151662607Sitojun		fatal("socket");
151778064Sume		/*NOTREACHED*/
151878064Sume	}
151962607Sitojun
152078064Sume	if (getifaddrs(&ifap) != 0) {
152162607Sitojun		fatal("getifaddrs");
152278064Sume		/*NOTREACHED*/
152378064Sume	}
152462607Sitojun
152562607Sitojun	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
152662607Sitojun		if (ifa->ifa_addr->sa_family != AF_INET6)
152762607Sitojun			continue;
152862607Sitojun		ifcp = ifc_find(ifa->ifa_name);
152962607Sitojun		/* we are interested in multicast-capable interfaces */
153062607Sitojun		if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
153162607Sitojun			continue;
153262607Sitojun		if (!ifcp) {
153362607Sitojun			/* new interface */
153478064Sume			if ((ifcp = MALLOC(struct ifc)) == NULL) {
153562607Sitojun				fatal("malloc: struct ifc");
153678064Sume				/*NOTREACHED*/
153778064Sume			}
153862607Sitojun			memset(ifcp, 0, sizeof(*ifcp));
1539243232Shrs
154062607Sitojun			ifcp->ifc_index = -1;
1541243232Shrs			strlcpy(ifcp->ifc_name, ifa->ifa_name,
1542243232Shrs			    sizeof(ifcp->ifc_name));
1543243232Shrs			TAILQ_INIT(&ifcp->ifc_ifac_head);
1544243232Shrs			TAILQ_INIT(&ifcp->ifc_iff_head);
154562607Sitojun			ifcp->ifc_flags = ifa->ifa_flags;
1546243232Shrs			TAILQ_INSERT_HEAD(&ifc_head, ifcp, ifc_next);
154762607Sitojun			trace(1, "newif %s <%s>\n", ifcp->ifc_name,
154862607Sitojun				ifflags(ifcp->ifc_flags));
154962607Sitojun			if (!strcmp(ifcp->ifc_name, LOOPBACK_IF))
155062607Sitojun				loopifcp = ifcp;
155162607Sitojun		} else {
155262607Sitojun			/* update flag, this may be up again */
155362607Sitojun			if (ifcp->ifc_flags != ifa->ifa_flags) {
155462607Sitojun				trace(1, "%s: <%s> -> ", ifcp->ifc_name,
155562607Sitojun					ifflags(ifcp->ifc_flags));
155662607Sitojun				trace(1, "<%s>\n", ifflags(ifa->ifa_flags));
155778064Sume				ifcp->ifc_cflags |= IFC_CHANGED;
155862607Sitojun			}
155962607Sitojun			ifcp->ifc_flags = ifa->ifa_flags;
156062607Sitojun		}
1561243232Shrs		if (ifconfig1(ifa->ifa_name, ifa->ifa_addr, ifcp, s) < 0) {
1562243232Shrs			/* maybe temporary failure */
1563243232Shrs			continue;
1564243232Shrs		}
156562607Sitojun		if ((ifcp->ifc_flags & (IFF_LOOPBACK | IFF_UP)) == IFF_UP
156662607Sitojun		 && 0 < ifcp->ifc_index && !ifcp->ifc_joined) {
156762607Sitojun			mreq.ipv6mr_multiaddr = ifcp->ifc_ripsin.sin6_addr;
156862607Sitojun			mreq.ipv6mr_interface = ifcp->ifc_index;
156978064Sume			if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
157078064Sume			    &mreq, sizeof(mreq)) < 0) {
157162607Sitojun				fatal("IPV6_JOIN_GROUP");
157278064Sume				/*NOTREACHED*/
157378064Sume			}
157462607Sitojun			trace(1, "join %s %s\n", ifcp->ifc_name, RIP6_DEST);
157562607Sitojun			ifcp->ifc_joined++;
157662607Sitojun		}
157762607Sitojun	}
157862607Sitojun	close(s);
157962607Sitojun	freeifaddrs(ifap);
158055163Sshin}
158155163Sshin
1582243232Shrsint
1583243232Shrsifconfig1(const char *name,
1584243232Shrs	const struct sockaddr *sa,
1585243232Shrs	struct ifc *ifcp,
1586243232Shrs	int s)
158755163Sshin{
158855163Sshin	struct	in6_ifreq ifr;
1589119031Sume	const struct sockaddr_in6 *sin6;
1590243232Shrs	struct	ifac *ifac;
159155163Sshin	int	plen;
159255163Sshin	char	buf[BUFSIZ];
159355163Sshin
1594119031Sume	sin6 = (const struct sockaddr_in6 *)sa;
1595122677Sume	if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && !lflag)
1596243232Shrs		return (-1);
1597119031Sume	ifr.ifr_addr = *sin6;
1598119032Sume	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
159978064Sume	if (ioctl(s, SIOCGIFNETMASK_IN6, (char *)&ifr) < 0) {
1600243232Shrs		syslog(LOG_INFO, "ioctl: SIOCGIFNETMASK_IN6");
1601243232Shrs		return (-1);
160278064Sume	}
160378064Sume	plen = sin6mask2len(&ifr.ifr_addr);
1604243232Shrs	if ((ifac = ifa_match(ifcp, &sin6->sin6_addr, plen)) != NULL) {
160555163Sshin		/* same interface found */
160655163Sshin		/* need check if something changed */
160755163Sshin		/* XXX not yet implemented */
1608243232Shrs		return (-1);
160955163Sshin	}
161055163Sshin	/*
161155163Sshin	 * New address is found
161255163Sshin	 */
1613243232Shrs	if ((ifac = MALLOC(struct ifac)) == NULL) {
161455163Sshin		fatal("malloc: struct ifac");
161578064Sume		/*NOTREACHED*/
161678064Sume	}
1617243232Shrs	memset(ifac, 0, sizeof(*ifac));
1618243232Shrs
1619243232Shrs	ifac->ifac_ifc = ifcp;
1620243232Shrs	ifac->ifac_addr = sin6->sin6_addr;
1621243232Shrs	ifac->ifac_plen = plen;
1622243232Shrs	ifac->ifac_scope_id = sin6->sin6_scope_id;
162355163Sshin	if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1624119031Sume		ifr.ifr_addr = *sin6;
162578064Sume		if (ioctl(s, SIOCGIFDSTADDR_IN6, (char *)&ifr) < 0) {
162655163Sshin			fatal("ioctl: SIOCGIFDSTADDR_IN6");
162778064Sume			/*NOTREACHED*/
162878064Sume		}
1629243232Shrs		ifac->ifac_raddr = ifr.ifr_dstaddr.sin6_addr;
1630243232Shrs		inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr, buf,
1631243232Shrs		    sizeof(buf));
163255163Sshin		trace(1, "found address %s/%d -- %s\n",
1633243232Shrs			inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen, buf);
163455163Sshin	} else {
163555163Sshin		trace(1, "found address %s/%d\n",
1636243232Shrs			inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen);
163755163Sshin	}
1638243232Shrs	if (ifcp->ifc_index < 0 && IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
1639243232Shrs		ifcp->ifc_mylladdr = ifac->ifac_addr;
1640243232Shrs		ifcp->ifc_index = ifac->ifac_scope_id;
164155163Sshin		memcpy(&ifcp->ifc_ripsin, &ripsin, ripsin.ss_len);
1642243231Shrs		ifcp->ifc_ripsin.sin6_scope_id = ifcp->ifc_index;
164355163Sshin		setindex2ifc(ifcp->ifc_index, ifcp);
164455163Sshin		ifcp->ifc_mtu = getifmtu(ifcp->ifc_index);
164555163Sshin		if (ifcp->ifc_mtu > RIP6_MAXMTU)
164655163Sshin			ifcp->ifc_mtu = RIP6_MAXMTU;
164778064Sume		if (ioctl(s, SIOCGIFMETRIC, (char *)&ifr) < 0) {
164855163Sshin			fatal("ioctl: SIOCGIFMETRIC");
164978064Sume			/*NOTREACHED*/
165078064Sume		}
165155163Sshin		ifcp->ifc_metric = ifr.ifr_metric;
165255163Sshin		trace(1, "\tindex: %d, mtu: %d, metric: %d\n",
165355163Sshin			ifcp->ifc_index, ifcp->ifc_mtu, ifcp->ifc_metric);
165478064Sume	} else
165578064Sume		ifcp->ifc_cflags |= IFC_CHANGED;
1656243232Shrs
1657243232Shrs	TAILQ_INSERT_HEAD(&ifcp->ifc_ifac_head, ifac, ifac_next);
1658243232Shrs
1659243232Shrs	return 0;
166055163Sshin}
166155163Sshin
1662243233Shrsvoid
1663243233Shrsifremove(int ifindex)
1664243233Shrs{
1665243233Shrs	struct ifc *ifcp;
1666243233Shrs	struct riprt *rrt;
1667243233Shrs
1668243233Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
1669243233Shrs		if (ifcp->ifc_index == ifindex)
1670243233Shrs			break;
1671243233Shrs	}
1672243233Shrs	if (ifcp == NULL)
1673243233Shrs		return;
1674243233Shrs
1675243233Shrs	tracet(1, "ifremove: %s is departed.\n", ifcp->ifc_name);
1676243233Shrs	TAILQ_REMOVE(&ifc_head, ifcp, ifc_next);
1677243233Shrs
1678243233Shrs	TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1679243233Shrs		if (rrt->rrt_index == ifcp->ifc_index &&
1680243233Shrs		    rrt->rrt_rflags & RRTF_AGGREGATE)
1681243233Shrs			delroute(&rrt->rrt_info, &rrt->rrt_gw);
1682243233Shrs	}
1683243233Shrs	free(ifcp);
1684243233Shrs}
1685243233Shrs
168655163Sshin/*
168755163Sshin * Receive and process routing messages.
168855163Sshin * Update interface information as necesssary.
168955163Sshin */
169055163Sshinvoid
1691243232Shrsrtrecv(void)
169255163Sshin{
169355163Sshin	char buf[BUFSIZ];
1694243232Shrs	char *p, *q = NULL;
169555163Sshin	struct rt_msghdr *rtm;
169655163Sshin	struct ifa_msghdr *ifam;
169755163Sshin	struct if_msghdr *ifm;
1698243233Shrs	struct if_announcemsghdr *ifan;
169955163Sshin	int len;
170078064Sume	struct ifc *ifcp, *ic;
170155163Sshin	int iface = 0, rtable = 0;
170255163Sshin	struct sockaddr_in6 *rta[RTAX_MAX];
170378064Sume	struct sockaddr_in6 mask;
1704243232Shrs	int i, addrs = 0;
170578064Sume	struct riprt *rrt;
170655163Sshin
170755163Sshin	if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
170855163Sshin		perror("read from rtsock");
170978064Sume		exit(1);
171055163Sshin	}
1711243232Shrs	if (len == 0)
1712243232Shrs		return;
1713243232Shrs#if 0
171455163Sshin	if (len < sizeof(*rtm)) {
171569279Sume		trace(1, "short read from rtsock: %d (should be > %lu)\n",
171669279Sume			len, (u_long)sizeof(*rtm));
171755163Sshin		return;
171855163Sshin	}
1719243232Shrs#endif
1720119042Sume	if (dflag >= 2) {
1721119042Sume		fprintf(stderr, "rtmsg:\n");
1722119042Sume		for (i = 0; i < len; i++) {
1723119042Sume			fprintf(stderr, "%02x ", buf[i] & 0xff);
1724119042Sume			if (i % 16 == 15) fprintf(stderr, "\n");
1725119042Sume		}
1726119042Sume		fprintf(stderr, "\n");
1727119042Sume	}
172855163Sshin
172955163Sshin	for (p = buf; p - buf < len; p += ((struct rt_msghdr *)p)->rtm_msglen) {
1730243232Shrs		if (((struct rt_msghdr *)p)->rtm_version != RTM_VERSION)
1731243232Shrs			continue;
1732243232Shrs
173355163Sshin		/* safety against bogus message */
173455163Sshin		if (((struct rt_msghdr *)p)->rtm_msglen <= 0) {
173555163Sshin			trace(1, "bogus rtmsg: length=%d\n",
173655163Sshin				((struct rt_msghdr *)p)->rtm_msglen);
173755163Sshin			break;
173855163Sshin		}
173955163Sshin		rtm = NULL;
174055163Sshin		ifam = NULL;
174155163Sshin		ifm = NULL;
174255163Sshin		switch (((struct rt_msghdr *)p)->rtm_type) {
174355163Sshin		case RTM_NEWADDR:
174455163Sshin		case RTM_DELADDR:
174555163Sshin			ifam = (struct ifa_msghdr *)p;
174655163Sshin			addrs = ifam->ifam_addrs;
174755163Sshin			q = (char *)(ifam + 1);
174855163Sshin			break;
174955163Sshin		case RTM_IFINFO:
175055163Sshin			ifm = (struct if_msghdr *)p;
175155163Sshin			addrs = ifm->ifm_addrs;
175255163Sshin			q = (char *)(ifm + 1);
175355163Sshin			break;
1754243233Shrs		case RTM_IFANNOUNCE:
1755243233Shrs			ifan = (struct if_announcemsghdr *)p;
1756243233Shrs			switch (ifan->ifan_what) {
1757243233Shrs			case IFAN_ARRIVAL:
1758243233Shrs				iface++;
1759243233Shrs				break;
1760243233Shrs			case IFAN_DEPARTURE:
1761243233Shrs				ifremove(ifan->ifan_index);
1762243233Shrs				iface++;
1763243233Shrs				break;
1764243233Shrs			}
1765243233Shrs			break;
176655163Sshin		default:
176755163Sshin			rtm = (struct rt_msghdr *)p;
176855163Sshin			addrs = rtm->rtm_addrs;
176955163Sshin			q = (char *)(rtm + 1);
177055163Sshin			if (rtm->rtm_version != RTM_VERSION) {
177155163Sshin				trace(1, "unexpected rtmsg version %d "
177255163Sshin					"(should be %d)\n",
177355163Sshin					rtm->rtm_version, RTM_VERSION);
177455163Sshin				continue;
177555163Sshin			}
177655163Sshin			if (rtm->rtm_pid == pid) {
177755163Sshin#if 0
177855163Sshin				trace(1, "rtmsg looped back to me, ignored\n");
177955163Sshin#endif
178055163Sshin				continue;
178155163Sshin			}
178255163Sshin			break;
178355163Sshin		}
178455163Sshin		memset(&rta, 0, sizeof(rta));
178555163Sshin		for (i = 0; i < RTAX_MAX; i++) {
178655163Sshin			if (addrs & (1 << i)) {
178755163Sshin				rta[i] = (struct sockaddr_in6 *)q;
178855163Sshin				q += ROUNDUP(rta[i]->sin6_len);
178955163Sshin			}
179055163Sshin		}
179155163Sshin
179255163Sshin		trace(1, "rtsock: %s (addrs=%x)\n",
179355163Sshin			rttypes((struct rt_msghdr *)p), addrs);
179455163Sshin		if (dflag >= 2) {
179555163Sshin			for (i = 0;
179655163Sshin			     i < ((struct rt_msghdr *)p)->rtm_msglen;
179755163Sshin			     i++) {
179855163Sshin				fprintf(stderr, "%02x ", p[i] & 0xff);
179955163Sshin				if (i % 16 == 15) fprintf(stderr, "\n");
180055163Sshin			}
180155163Sshin			fprintf(stderr, "\n");
180255163Sshin		}
180355163Sshin
180455163Sshin		/*
180555163Sshin		 * Easy ones first.
180655163Sshin		 *
180755163Sshin		 * We may be able to optimize by using ifm->ifm_index or
180855163Sshin		 * ifam->ifam_index.  For simplicity we don't do that here.
180955163Sshin		 */
181055163Sshin		switch (((struct rt_msghdr *)p)->rtm_type) {
181155163Sshin		case RTM_NEWADDR:
181255163Sshin		case RTM_IFINFO:
181355163Sshin			iface++;
181455163Sshin			continue;
181555163Sshin		case RTM_ADD:
181655163Sshin			rtable++;
181755163Sshin			continue;
181855163Sshin		case RTM_LOSING:
181955163Sshin		case RTM_MISS:
182055163Sshin		case RTM_GET:
182155163Sshin		case RTM_LOCK:
182255163Sshin			/* nothing to be done here */
182355163Sshin			trace(1, "\tnothing to be done, ignored\n");
182455163Sshin			continue;
182555163Sshin		}
182655163Sshin
182755163Sshin#if 0
182855163Sshin		if (rta[RTAX_DST] == NULL) {
182955163Sshin			trace(1, "\tno destination, ignored\n");
183062607Sitojun			continue;
183155163Sshin		}
183255163Sshin		if (rta[RTAX_DST]->sin6_family != AF_INET6) {
183355163Sshin			trace(1, "\taf mismatch, ignored\n");
183455163Sshin			continue;
183555163Sshin		}
183655163Sshin		if (IN6_IS_ADDR_LINKLOCAL(&rta[RTAX_DST]->sin6_addr)) {
183755163Sshin			trace(1, "\tlinklocal destination, ignored\n");
183855163Sshin			continue;
183955163Sshin		}
184055163Sshin		if (IN6_ARE_ADDR_EQUAL(&rta[RTAX_DST]->sin6_addr, &in6addr_loopback)) {
184155163Sshin			trace(1, "\tloopback destination, ignored\n");
184255163Sshin			continue;		/* Loopback */
184355163Sshin		}
184455163Sshin		if (IN6_IS_ADDR_MULTICAST(&rta[RTAX_DST]->sin6_addr)) {
184555163Sshin			trace(1, "\tmulticast destination, ignored\n");
184655163Sshin			continue;
184755163Sshin		}
184855163Sshin#endif
184955163Sshin
185055163Sshin		/* hard ones */
185155163Sshin		switch (((struct rt_msghdr *)p)->rtm_type) {
185255163Sshin		case RTM_NEWADDR:
185355163Sshin		case RTM_IFINFO:
185455163Sshin		case RTM_ADD:
185555163Sshin		case RTM_LOSING:
185655163Sshin		case RTM_MISS:
185755163Sshin		case RTM_GET:
185855163Sshin		case RTM_LOCK:
185955163Sshin			/* should already be handled */
186055163Sshin			fatal("rtrecv: never reach here");
186178064Sume			/*NOTREACHED*/
186255163Sshin		case RTM_DELETE:
186378064Sume			if (!rta[RTAX_DST] || !rta[RTAX_GATEWAY]) {
186478064Sume				trace(1, "\tsome of dst/gw/netamsk are "
186578064Sume				    "unavailable, ignored\n");
186655163Sshin				break;
186755163Sshin			}
186878064Sume			if ((rtm->rtm_flags & RTF_HOST) != 0) {
186978064Sume				mask.sin6_len = sizeof(mask);
187078064Sume				memset(&mask.sin6_addr, 0xff,
187178064Sume				    sizeof(mask.sin6_addr));
187278064Sume				rta[RTAX_NETMASK] = &mask;
187378064Sume			} else if (!rta[RTAX_NETMASK]) {
187478064Sume				trace(1, "\tsome of dst/gw/netamsk are "
187578064Sume				    "unavailable, ignored\n");
187678064Sume				break;
187778064Sume			}
187878064Sume			if (rt_del(rta[RTAX_DST], rta[RTAX_GATEWAY],
187978064Sume			    rta[RTAX_NETMASK]) == 0) {
188055163Sshin				rtable++;	/*just to be sure*/
188155163Sshin			}
188255163Sshin			break;
188355163Sshin		case RTM_CHANGE:
188455163Sshin		case RTM_REDIRECT:
188555163Sshin			trace(1, "\tnot supported yet, ignored\n");
188655163Sshin			break;
188755163Sshin		case RTM_DELADDR:
188855163Sshin			if (!rta[RTAX_NETMASK] || !rta[RTAX_IFA]) {
188955163Sshin				trace(1, "\tno netmask or ifa given, ignored\n");
189055163Sshin				break;
189155163Sshin			}
189255163Sshin			if (ifam->ifam_index < nindex2ifc)
189355163Sshin				ifcp = index2ifc[ifam->ifam_index];
189455163Sshin			else
189555163Sshin				ifcp = NULL;
189655163Sshin			if (!ifcp) {
189755163Sshin				trace(1, "\tinvalid ifam_index %d, ignored\n",
189855163Sshin					ifam->ifam_index);
189955163Sshin				break;
190055163Sshin			}
190178064Sume			if (!rt_deladdr(ifcp, rta[RTAX_IFA], rta[RTAX_NETMASK]))
190278064Sume				iface++;
190355163Sshin			break;
190455163Sshin		case RTM_OLDADD:
190555163Sshin		case RTM_OLDDEL:
190655163Sshin			trace(1, "\tnot supported yet, ignored\n");
190755163Sshin			break;
190855163Sshin		}
190955163Sshin
191055163Sshin	}
191155163Sshin
191255163Sshin	if (iface) {
191355163Sshin		trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
191455163Sshin		ifconfig();
1915243232Shrs		TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
191678064Sume			if (ifcp->ifc_cflags & IFC_CHANGED) {
191778064Sume				if (ifrt(ifcp, 1)) {
1918243232Shrs					TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
191978064Sume						if (ifcp->ifc_index == ic->ifc_index)
192078064Sume							continue;
192178064Sume						if (ic->ifc_flags & IFF_UP)
192278064Sume							ripsend(ic, &ic->ifc_ripsin,
192378064Sume							RRTF_CHANGED);
192478064Sume					}
192578064Sume					/* Reset the flag */
1926243232Shrs					TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
192778064Sume						rrt->rrt_rflags &= ~RRTF_CHANGED;
1928243232Shrs					}
192978064Sume				}
193078064Sume				ifcp->ifc_cflags &= ~IFC_CHANGED;
193178064Sume			}
1932243232Shrs		}
193355163Sshin	}
193455163Sshin	if (rtable) {
193555163Sshin		trace(1, "rtsock: read routing table again\n");
193655163Sshin		krtread(1);
193755163Sshin	}
193855163Sshin}
193955163Sshin
194055163Sshin/*
194155163Sshin * remove specified route from the internal routing table.
194255163Sshin */
194355163Sshinint
1944243232Shrsrt_del(const struct sockaddr_in6 *sdst,
1945243232Shrs	const struct sockaddr_in6 *sgw,
1946243232Shrs	const struct sockaddr_in6 *smask)
194755163Sshin{
194855163Sshin	const struct in6_addr *dst = NULL;
194955163Sshin	const struct in6_addr *gw = NULL;
195055163Sshin	int prefix;
195155163Sshin	struct netinfo6 ni6;
195255163Sshin	struct riprt *rrt = NULL;
195355163Sshin	time_t t_lifetime;
195455163Sshin
195555163Sshin	if (sdst->sin6_family != AF_INET6) {
195655163Sshin		trace(1, "\tother AF, ignored\n");
195755163Sshin		return -1;
195855163Sshin	}
195955163Sshin	if (IN6_IS_ADDR_LINKLOCAL(&sdst->sin6_addr)
196055163Sshin	 || IN6_ARE_ADDR_EQUAL(&sdst->sin6_addr, &in6addr_loopback)
196155163Sshin	 || IN6_IS_ADDR_MULTICAST(&sdst->sin6_addr)) {
196255163Sshin		trace(1, "\taddress %s not interesting, ignored\n",
196355163Sshin			inet6_n2p(&sdst->sin6_addr));
196455163Sshin		return -1;
196555163Sshin	}
196655163Sshin	dst = &sdst->sin6_addr;
196778064Sume	if (sgw->sin6_family == AF_INET6) {
196855163Sshin		/* easy case */
196955163Sshin		gw = &sgw->sin6_addr;
197078064Sume		prefix = sin6mask2len(smask);
197155163Sshin	} else if (sgw->sin6_family == AF_LINK) {
197255163Sshin		/*
197355163Sshin		 * Interface route... a hard case.  We need to get the prefix
197455163Sshin		 * length from the kernel, but we now are parsing rtmsg.
197555163Sshin		 * We'll purge matching routes from my list, then get the
197655163Sshin		 * fresh list.
197755163Sshin		 */
197855163Sshin		struct riprt *longest;
1979108533Sschweikh		trace(1, "\t%s is an interface route, guessing prefixlen\n",
198055163Sshin			inet6_n2p(dst));
198155163Sshin		longest = NULL;
1982243232Shrs		TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
198355163Sshin			if (IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
198455163Sshin					&sdst->sin6_addr)
198555163Sshin			 && IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)) {
198655163Sshin				if (!longest
198755163Sshin				 || longest->rrt_info.rip6_plen <
198855163Sshin						 rrt->rrt_info.rip6_plen) {
198955163Sshin					longest = rrt;
199055163Sshin				}
199155163Sshin			}
199255163Sshin		}
199355163Sshin		rrt = longest;
199455163Sshin		if (!rrt) {
199555163Sshin			trace(1, "\tno matching interface route found\n");
199655163Sshin			return -1;
199755163Sshin		}
199855163Sshin		gw = &in6addr_loopback;
199955163Sshin		prefix = rrt->rrt_info.rip6_plen;
200055163Sshin	} else {
200178064Sume		trace(1, "\tunsupported af: (gw=%d)\n", sgw->sin6_family);
200255163Sshin		return -1;
200355163Sshin	}
200455163Sshin
200555163Sshin	trace(1, "\tdeleting %s/%d ", inet6_n2p(dst), prefix);
200655163Sshin	trace(1, "gw %s\n", inet6_n2p(gw));
200755163Sshin	t_lifetime = time(NULL) - RIP_LIFETIME;
200855163Sshin	/* age route for interface address */
200955163Sshin	memset(&ni6, 0, sizeof(ni6));
201055163Sshin	ni6.rip6_dest = *dst;
201155163Sshin	ni6.rip6_plen = prefix;
201255163Sshin	applyplen(&ni6.rip6_dest, ni6.rip6_plen);	/*to be sure*/
201355163Sshin	trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6.rip6_dest),
201455163Sshin		ni6.rip6_plen);
2015243232Shrs	if (!rrt && (rrt = rtsearch(&ni6)) == NULL) {
201655163Sshin		trace(1, "\tno route found\n");
201755163Sshin		return -1;
201855163Sshin	}
201978064Sume#if 0
202055163Sshin	if ((rrt->rrt_flags & RTF_STATIC) == 0) {
202155163Sshin		trace(1, "\tyou can delete static routes only\n");
202278064Sume	} else
202378064Sume#endif
202478064Sume	if (!IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, gw)) {
202555163Sshin		trace(1, "\tgw mismatch: %s <-> ",
202655163Sshin			inet6_n2p(&rrt->rrt_gw));
202755163Sshin		trace(1, "%s\n", inet6_n2p(gw));
202855163Sshin	} else {
202955163Sshin		trace(1, "\troute found, age it\n");
203055163Sshin		if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
203155163Sshin			rrt->rrt_t = t_lifetime;
203255163Sshin			rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
203355163Sshin		}
203455163Sshin	}
203555163Sshin	return 0;
203655163Sshin}
203755163Sshin
203855163Sshin/*
203955163Sshin * remove specified address from internal interface/routing table.
204055163Sshin */
204155163Sshinint
2042243232Shrsrt_deladdr(struct ifc *ifcp,
2043243232Shrs	const struct sockaddr_in6 *sifa,
2044243232Shrs	const struct sockaddr_in6 *smask)
204555163Sshin{
204655163Sshin	const struct in6_addr *addr = NULL;
204755163Sshin	int prefix;
2048243232Shrs	struct ifac *ifac = NULL;
204955163Sshin	struct netinfo6 ni6;
205055163Sshin	struct riprt *rrt = NULL;
205155163Sshin	time_t t_lifetime;
205255163Sshin	int updated = 0;
205355163Sshin
205478064Sume	if (sifa->sin6_family != AF_INET6) {
205555163Sshin		trace(1, "\tother AF, ignored\n");
205655163Sshin		return -1;
205755163Sshin	}
205855163Sshin	addr = &sifa->sin6_addr;
205978064Sume	prefix = sin6mask2len(smask);
206055163Sshin
206155163Sshin	trace(1, "\tdeleting %s/%d from %s\n",
206255163Sshin		inet6_n2p(addr), prefix, ifcp->ifc_name);
2063243232Shrs	ifac = ifa_match(ifcp, addr, prefix);
2064243232Shrs	if (!ifac) {
206555163Sshin		trace(1, "\tno matching ifa found for %s/%d on %s\n",
206655163Sshin			inet6_n2p(addr), prefix, ifcp->ifc_name);
206755163Sshin		return -1;
206855163Sshin	}
2069243232Shrs	if (ifac->ifac_ifc != ifcp) {
207055163Sshin		trace(1, "\taddress table corrupt: back pointer does not match "
207155163Sshin			"(%s != %s)\n",
2072243232Shrs			ifcp->ifc_name, ifac->ifac_ifc->ifc_name);
207355163Sshin		return -1;
207455163Sshin	}
2075243232Shrs	TAILQ_REMOVE(&ifcp->ifc_ifac_head, ifac, ifac_next);
207655163Sshin	t_lifetime = time(NULL) - RIP_LIFETIME;
207755163Sshin	/* age route for interface address */
207855163Sshin	memset(&ni6, 0, sizeof(ni6));
2079243232Shrs	ni6.rip6_dest = ifac->ifac_addr;
2080243232Shrs	ni6.rip6_plen = ifac->ifac_plen;
208155163Sshin	applyplen(&ni6.rip6_dest, ni6.rip6_plen);
208255163Sshin	trace(1, "\tfind interface route %s/%d on %d\n",
208355163Sshin		inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen, ifcp->ifc_index);
2084243232Shrs	if ((rrt = rtsearch(&ni6)) != NULL) {
208555163Sshin		struct in6_addr none;
208655163Sshin		memset(&none, 0, sizeof(none));
208778064Sume		if (rrt->rrt_index == ifcp->ifc_index &&
208878064Sume		    (IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &none) ||
208978064Sume		     IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw))) {
209055163Sshin			trace(1, "\troute found, age it\n");
209155163Sshin			if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
209255163Sshin				rrt->rrt_t = t_lifetime;
209355163Sshin				rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
209455163Sshin			}
209555163Sshin			updated++;
209655163Sshin		} else {
209755163Sshin			trace(1, "\tnon-interface route found: %s/%d on %d\n",
209855163Sshin				inet6_n2p(&rrt->rrt_info.rip6_dest),
209955163Sshin				rrt->rrt_info.rip6_plen,
210055163Sshin				rrt->rrt_index);
210155163Sshin		}
210255163Sshin	} else
210355163Sshin		trace(1, "\tno interface route found\n");
210455163Sshin	/* age route for p2p destination */
210555163Sshin	if (ifcp->ifc_flags & IFF_POINTOPOINT) {
210655163Sshin		memset(&ni6, 0, sizeof(ni6));
2107243232Shrs		ni6.rip6_dest = ifac->ifac_raddr;
210855163Sshin		ni6.rip6_plen = 128;
210955163Sshin		applyplen(&ni6.rip6_dest, ni6.rip6_plen);	/*to be sure*/
211055163Sshin		trace(1, "\tfind p2p route %s/%d on %d\n",
211155163Sshin			inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen,
211255163Sshin			ifcp->ifc_index);
2113243232Shrs		if ((rrt = rtsearch(&ni6)) != NULL) {
211478064Sume			if (rrt->rrt_index == ifcp->ifc_index &&
2115243232Shrs			    IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw,
2116243232Shrs			    &ifac->ifac_addr)) {
211755163Sshin				trace(1, "\troute found, age it\n");
211855163Sshin				if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
211955163Sshin					rrt->rrt_t = t_lifetime;
212055163Sshin					rrt->rrt_info.rip6_metric =
212178064Sume					    HOPCNT_INFINITY6;
212255163Sshin					updated++;
212355163Sshin				}
212455163Sshin			} else {
212555163Sshin				trace(1, "\tnon-p2p route found: %s/%d on %d\n",
212655163Sshin					inet6_n2p(&rrt->rrt_info.rip6_dest),
212755163Sshin					rrt->rrt_info.rip6_plen,
212855163Sshin					rrt->rrt_index);
212955163Sshin			}
213055163Sshin		} else
213155163Sshin			trace(1, "\tno p2p route found\n");
213255163Sshin	}
2133243232Shrs	free(ifac);
2134243232Shrs
2135243232Shrs	return ((updated) ? 0 : -1);
213655163Sshin}
213755163Sshin
213855163Sshin/*
213955163Sshin * Get each interface address and put those interface routes to the route
214055163Sshin * list.
214155163Sshin */
214278064Sumeint
2143243232Shrsifrt(struct ifc *ifcp, int again)
214455163Sshin{
2145243232Shrs	struct ifac *ifac;
2146243232Shrs	struct riprt *rrt = NULL, *search_rrt, *loop_rrt;
214762607Sitojun	struct netinfo6 *np;
214878064Sume	time_t t_lifetime;
214978064Sume	int need_trigger = 0;
215055163Sshin
2151122677Sume#if 0
215255163Sshin	if (ifcp->ifc_flags & IFF_LOOPBACK)
215378064Sume		return 0;			/* ignore loopback */
2154122677Sume#endif
2155122677Sume
215662607Sitojun	if (ifcp->ifc_flags & IFF_POINTOPOINT) {
215762607Sitojun		ifrt_p2p(ifcp, again);
215878064Sume		return 0;
215962607Sitojun	}
216062607Sitojun
2161243232Shrs	TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2162243232Shrs		if (IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
216362607Sitojun#if 0
216462607Sitojun			trace(1, "route: %s on %s: "
216562607Sitojun			    "skip linklocal interface address\n",
2166243232Shrs			    inet6_n2p(&ifac->ifac_addr), ifcp->ifc_name);
216762607Sitojun#endif
216862607Sitojun			continue;
216962607Sitojun		}
2170243232Shrs		if (IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_addr)) {
217162607Sitojun#if 0
217262607Sitojun			trace(1, "route: %s: skip unspec interface address\n",
217362607Sitojun			    ifcp->ifc_name);
217462607Sitojun#endif
217562607Sitojun			continue;
217662607Sitojun		}
2177243232Shrs		if (IN6_IS_ADDR_LOOPBACK(&ifac->ifac_addr)) {
2178122677Sume#if 0
2179122677Sume			trace(1, "route: %s: skip loopback address\n",
2180122677Sume			    ifcp->ifc_name);
2181122677Sume#endif
2182122677Sume			continue;
2183122677Sume		}
218478064Sume		if (ifcp->ifc_flags & IFF_UP) {
218578064Sume			if ((rrt = MALLOC(struct riprt)) == NULL)
218678064Sume				fatal("malloc: struct riprt");
218778064Sume			memset(rrt, 0, sizeof(*rrt));
218878064Sume			rrt->rrt_same = NULL;
218978064Sume			rrt->rrt_index = ifcp->ifc_index;
219078064Sume			rrt->rrt_t = 0;	/* don't age */
2191243232Shrs			rrt->rrt_info.rip6_dest = ifac->ifac_addr;
219278064Sume			rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
219378064Sume			rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2194243232Shrs			rrt->rrt_info.rip6_plen = ifac->ifac_plen;
2195186119Sqingli			rrt->rrt_flags = RTF_HOST;
219678064Sume			rrt->rrt_rflags |= RRTF_CHANGED;
2197243232Shrs			applyplen(&rrt->rrt_info.rip6_dest, ifac->ifac_plen);
219878064Sume			memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2199243232Shrs			rrt->rrt_gw = ifac->ifac_addr;
220078064Sume			np = &rrt->rrt_info;
2201243232Shrs			search_rrt = rtsearch(np);
220278064Sume			if (search_rrt != NULL) {
2203119042Sume				if (search_rrt->rrt_info.rip6_metric <=
220478064Sume				    rrt->rrt_info.rip6_metric) {
220578064Sume					/* Already have better route */
220678064Sume					if (!again) {
220778064Sume						trace(1, "route: %s/%d: "
220878064Sume						    "already registered (%s)\n",
220978064Sume						    inet6_n2p(&np->rip6_dest), np->rip6_plen,
221078064Sume						    ifcp->ifc_name);
221178064Sume					}
2212119042Sume					goto next;
221378064Sume				}
2214119042Sume
2215243232Shrs				TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
2216119042Sume				delroute(&rrt->rrt_info, &rrt->rrt_gw);
221778064Sume			}
221855163Sshin			/* Attach the route to the list */
221962607Sitojun			trace(1, "route: %s/%d: register route (%s)\n",
222062607Sitojun			    inet6_n2p(&np->rip6_dest), np->rip6_plen,
222162607Sitojun			    ifcp->ifc_name);
2222243232Shrs			TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
222378064Sume			addroute(rrt, &rrt->rrt_gw, ifcp);
2224119042Sume			rrt = NULL;
222578064Sume			sendrequest(ifcp);
222678064Sume			ripsend(ifcp, &ifcp->ifc_ripsin, 0);
222778064Sume			need_trigger = 1;
222855163Sshin		} else {
2229243232Shrs			TAILQ_FOREACH(loop_rrt, &riprt_head, rrt_next) {
223078064Sume				if (loop_rrt->rrt_index == ifcp->ifc_index) {
223178064Sume					t_lifetime = time(NULL) - RIP_LIFETIME;
223278064Sume					if (loop_rrt->rrt_t == 0 || loop_rrt->rrt_t > t_lifetime) {
223378064Sume						loop_rrt->rrt_t = t_lifetime;
223478064Sume						loop_rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
223578064Sume						loop_rrt->rrt_rflags |= RRTF_CHANGED;
223678064Sume						need_trigger = 1;
223778064Sume					}
223878064Sume				}
223955163Sshin			}
224078064Sume                }
2241119042Sume	next:
2242119042Sume		if (rrt)
2243119042Sume			free(rrt);
224462607Sitojun	}
224578064Sume	return need_trigger;
224662607Sitojun}
224755163Sshin
224862607Sitojun/*
224962607Sitojun * there are couple of p2p interface routing models.  "behavior" lets
225062607Sitojun * you pick one.  it looks that gated behavior fits best with BSDs,
2251119035Sume * since BSD kernels do not look at prefix length on p2p interfaces.
225262607Sitojun */
225362607Sitojunvoid
2254243232Shrsifrt_p2p(struct ifc *ifcp, int again)
225562607Sitojun{
2256243232Shrs	struct ifac *ifac;
2257243232Shrs	struct riprt *rrt, *orrt;
225862607Sitojun	struct netinfo6 *np;
225962607Sitojun	struct in6_addr addr, dest;
226062607Sitojun	int advert, ignore, i;
226162607Sitojun#define P2PADVERT_NETWORK	1
226262607Sitojun#define P2PADVERT_ADDR		2
226362607Sitojun#define P2PADVERT_DEST		4
226462607Sitojun#define P2PADVERT_MAX		4
226562607Sitojun	const enum { CISCO, GATED, ROUTE6D } behavior = GATED;
226678064Sume	const char *category = "";
226762607Sitojun	const char *noadv;
226862607Sitojun
2269243232Shrs	TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2270243232Shrs		addr = ifac->ifac_addr;
2271243232Shrs		dest = ifac->ifac_raddr;
2272243232Shrs		applyplen(&addr, ifac->ifac_plen);
2273243232Shrs		applyplen(&dest, ifac->ifac_plen);
227462607Sitojun		advert = ignore = 0;
227562607Sitojun		switch (behavior) {
227662607Sitojun		case CISCO:
227762607Sitojun			/*
227862607Sitojun			 * honor addr/plen, just like normal shared medium
227962607Sitojun			 * interface.  this may cause trouble if you reuse
228062607Sitojun			 * addr/plen on other interfaces.
228162607Sitojun			 *
228262607Sitojun			 * advertise addr/plen.
228362607Sitojun			 */
228462607Sitojun			advert |= P2PADVERT_NETWORK;
228562607Sitojun			break;
228662607Sitojun		case GATED:
228762607Sitojun			/*
228862607Sitojun			 * prefixlen on p2p interface is meaningless.
228962607Sitojun			 * advertise addr/128 and dest/128.
229062607Sitojun			 *
229162607Sitojun			 * do not install network route to route6d routing
229262607Sitojun			 * table (if we do, it would prevent route installation
229362607Sitojun			 * for other p2p interface that shares addr/plen).
229478064Sume			 *
229578064Sume			 * XXX what should we do if dest is ::?  it will not
229678064Sume			 * get announced anyways (see following filter),
229778064Sume			 * but we need to think.
229862607Sitojun			 */
229962607Sitojun			advert |= P2PADVERT_ADDR;
230062607Sitojun			advert |= P2PADVERT_DEST;
230162607Sitojun			ignore |= P2PADVERT_NETWORK;
230262607Sitojun			break;
230362607Sitojun		case ROUTE6D:
230462607Sitojun			/*
230578064Sume			 * just for testing.  actually the code is redundant
230678064Sume			 * given the current p2p interface address assignment
230778064Sume			 * rule for kame kernel.
230878064Sume			 *
230978064Sume			 * intent:
231078064Sume			 *	A/n -> announce A/n
231178064Sume			 *	A B/n, A and B share prefix -> A/n (= B/n)
231278064Sume			 *	A B/n, do not share prefix -> A/128 and B/128
231378064Sume			 * actually, A/64 and A B/128 are the only cases
231478064Sume			 * permitted by the kernel:
231578064Sume			 *	A/64 -> A/64
231678064Sume			 *	A B/128 -> A/128 and B/128
231762607Sitojun			 */
2318243232Shrs			if (!IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_raddr)) {
231978064Sume				if (IN6_ARE_ADDR_EQUAL(&addr, &dest))
232078064Sume					advert |= P2PADVERT_NETWORK;
232178064Sume				else {
232278064Sume					advert |= P2PADVERT_ADDR;
232378064Sume					advert |= P2PADVERT_DEST;
232478064Sume					ignore |= P2PADVERT_NETWORK;
232578064Sume				}
232678064Sume			} else
232762607Sitojun				advert |= P2PADVERT_NETWORK;
232862607Sitojun			break;
232962607Sitojun		}
233062607Sitojun
233162607Sitojun		for (i = 1; i <= P2PADVERT_MAX; i *= 2) {
233262607Sitojun			if ((ignore & i) != 0)
233362607Sitojun				continue;
233478064Sume			if ((rrt = MALLOC(struct riprt)) == NULL) {
233555163Sshin				fatal("malloc: struct riprt");
233678064Sume				/*NOTREACHED*/
233778064Sume			}
233862607Sitojun			memset(rrt, 0, sizeof(*rrt));
233955163Sshin			rrt->rrt_same = NULL;
234055163Sshin			rrt->rrt_index = ifcp->ifc_index;
234162607Sitojun			rrt->rrt_t = 0;	/* don't age */
234262607Sitojun			switch (i) {
234362607Sitojun			case P2PADVERT_NETWORK:
2344243232Shrs				rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2345243232Shrs				rrt->rrt_info.rip6_plen = ifac->ifac_plen;
234662607Sitojun				applyplen(&rrt->rrt_info.rip6_dest,
2347243232Shrs				    ifac->ifac_plen);
234862607Sitojun				category = "network";
234962607Sitojun				break;
235062607Sitojun			case P2PADVERT_ADDR:
2351243232Shrs				rrt->rrt_info.rip6_dest = ifac->ifac_addr;
235262607Sitojun				rrt->rrt_info.rip6_plen = 128;
235378064Sume				rrt->rrt_gw = in6addr_loopback;
235462607Sitojun				category = "addr";
235562607Sitojun				break;
235662607Sitojun			case P2PADVERT_DEST:
2357243232Shrs				rrt->rrt_info.rip6_dest = ifac->ifac_raddr;
235862607Sitojun				rrt->rrt_info.rip6_plen = 128;
2359243232Shrs				rrt->rrt_gw = ifac->ifac_addr;
236062607Sitojun				category = "dest";
236162607Sitojun				break;
236262607Sitojun			}
236362607Sitojun			if (IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_info.rip6_dest) ||
236462607Sitojun			    IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_info.rip6_dest)) {
236562607Sitojun#if 0
236662607Sitojun				trace(1, "route: %s: skip unspec/linklocal "
236762607Sitojun				    "(%s on %s)\n", category, ifcp->ifc_name);
236862607Sitojun#endif
236962607Sitojun				free(rrt);
237062607Sitojun				continue;
237162607Sitojun			}
237262607Sitojun			if ((advert & i) == 0) {
237362607Sitojun				rrt->rrt_rflags |= RRTF_NOADVERTISE;
237462607Sitojun				noadv = ", NO-ADV";
237562607Sitojun			} else
237662607Sitojun				noadv = "";
237755163Sshin			rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
237862607Sitojun			rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
237955163Sshin			np = &rrt->rrt_info;
2380243232Shrs			orrt = rtsearch(np);
238178064Sume			if (!orrt) {
238255163Sshin				/* Attach the route to the list */
238362607Sitojun				trace(1, "route: %s/%d: register route "
238462607Sitojun				    "(%s on %s%s)\n",
238562607Sitojun				    inet6_n2p(&np->rip6_dest), np->rip6_plen,
238662607Sitojun				    category, ifcp->ifc_name, noadv);
2387243232Shrs				TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
238878064Sume			} else if (rrt->rrt_index != orrt->rrt_index ||
238978064Sume			    rrt->rrt_info.rip6_metric != orrt->rrt_info.rip6_metric) {
2390243232Shrs				/* replace route */
2391243232Shrs				TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2392243232Shrs				TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
239378064Sume				free(orrt);
239478064Sume
239578064Sume				trace(1, "route: %s/%d: update (%s on %s%s)\n",
239678064Sume				    inet6_n2p(&np->rip6_dest), np->rip6_plen,
239778064Sume				    category, ifcp->ifc_name, noadv);
239855163Sshin			} else {
239955163Sshin				/* Already found */
240055163Sshin				if (!again) {
240162607Sitojun					trace(1, "route: %s/%d: "
240262607Sitojun					    "already registered (%s on %s%s)\n",
240362607Sitojun					    inet6_n2p(&np->rip6_dest),
240462607Sitojun					    np->rip6_plen, category,
240562607Sitojun					    ifcp->ifc_name, noadv);
240655163Sshin				}
240755163Sshin				free(rrt);
240855163Sshin			}
240955163Sshin		}
241055163Sshin	}
241162607Sitojun#undef P2PADVERT_NETWORK
241262607Sitojun#undef P2PADVERT_ADDR
241362607Sitojun#undef P2PADVERT_DEST
241462607Sitojun#undef P2PADVERT_MAX
241555163Sshin}
241655163Sshin
241755163Sshinint
2418243232Shrsgetifmtu(int ifindex)
241955163Sshin{
242055163Sshin	int	mib[6];
242155163Sshin	char	*buf;
242255163Sshin	size_t	msize;
242355163Sshin	struct	if_msghdr *ifm;
242455163Sshin	int	mtu;
242555163Sshin
242655163Sshin	mib[0] = CTL_NET;
242755163Sshin	mib[1] = PF_ROUTE;
242855163Sshin	mib[2] = 0;
242955163Sshin	mib[3] = AF_INET6;
243055163Sshin	mib[4] = NET_RT_IFLIST;
243155163Sshin	mib[5] = ifindex;
243278064Sume	if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
243355163Sshin		fatal("sysctl estimate NET_RT_IFLIST");
243478064Sume		/*NOTREACHED*/
243578064Sume	}
243678064Sume	if ((buf = malloc(msize)) == NULL) {
243755163Sshin		fatal("malloc");
243878064Sume		/*NOTREACHED*/
243978064Sume	}
244078064Sume	if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
244155163Sshin		fatal("sysctl NET_RT_IFLIST");
244278064Sume		/*NOTREACHED*/
244378064Sume	}
244455163Sshin	ifm = (struct if_msghdr *)buf;
244555163Sshin	mtu = ifm->ifm_data.ifi_mtu;
244678064Sume	if (ifindex != ifm->ifm_index) {
244755163Sshin		fatal("ifindex does not match with ifm_index");
244878064Sume		/*NOTREACHED*/
244978064Sume	}
245055163Sshin	free(buf);
245155163Sshin	return mtu;
245255163Sshin}
245355163Sshin
245455163Sshinconst char *
2455243232Shrsrttypes(struct rt_msghdr *rtm)
245655163Sshin{
245762607Sitojun#define	RTTYPE(s, f) \
245862607Sitojundo { \
245962607Sitojun	if (rtm->rtm_type == (f)) \
246062607Sitojun		return (s); \
246162607Sitojun} while (0)
246255163Sshin	RTTYPE("ADD", RTM_ADD);
246355163Sshin	RTTYPE("DELETE", RTM_DELETE);
246455163Sshin	RTTYPE("CHANGE", RTM_CHANGE);
246555163Sshin	RTTYPE("GET", RTM_GET);
246655163Sshin	RTTYPE("LOSING", RTM_LOSING);
246755163Sshin	RTTYPE("REDIRECT", RTM_REDIRECT);
246855163Sshin	RTTYPE("MISS", RTM_MISS);
246955163Sshin	RTTYPE("LOCK", RTM_LOCK);
247055163Sshin	RTTYPE("OLDADD", RTM_OLDADD);
247155163Sshin	RTTYPE("OLDDEL", RTM_OLDDEL);
247255163Sshin	RTTYPE("NEWADDR", RTM_NEWADDR);
247355163Sshin	RTTYPE("DELADDR", RTM_DELADDR);
247455163Sshin	RTTYPE("IFINFO", RTM_IFINFO);
247578064Sume#ifdef RTM_OLDADD
247678064Sume	RTTYPE("OLDADD", RTM_OLDADD);
247778064Sume#endif
247878064Sume#ifdef RTM_OLDDEL
247978064Sume	RTTYPE("OLDDEL", RTM_OLDDEL);
248078064Sume#endif
248178064Sume#ifdef RTM_OIFINFO
248278064Sume	RTTYPE("OIFINFO", RTM_OIFINFO);
248378064Sume#endif
248478064Sume#ifdef RTM_IFANNOUNCE
248578064Sume	RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE);
248678064Sume#endif
248778064Sume#ifdef RTM_NEWMADDR
248878064Sume	RTTYPE("NEWMADDR", RTM_NEWMADDR);
248978064Sume#endif
249078064Sume#ifdef RTM_DELMADDR
249178064Sume	RTTYPE("DELMADDR", RTM_DELMADDR);
249278064Sume#endif
249355163Sshin#undef RTTYPE
249455163Sshin	return NULL;
249555163Sshin}
249655163Sshin
249755163Sshinconst char *
2498243232Shrsrtflags(struct rt_msghdr *rtm)
249955163Sshin{
250055163Sshin	static char buf[BUFSIZ];
250155163Sshin
250278064Sume	/*
250378064Sume	 * letter conflict should be okay.  painful when *BSD diverges...
250478064Sume	 */
250578064Sume	strlcpy(buf, "", sizeof(buf));
250662607Sitojun#define	RTFLAG(s, f) \
250762607Sitojundo { \
250862607Sitojun	if (rtm->rtm_flags & (f)) \
250978064Sume		strlcat(buf, (s), sizeof(buf)); \
251062607Sitojun} while (0)
251155163Sshin	RTFLAG("U", RTF_UP);
251255163Sshin	RTFLAG("G", RTF_GATEWAY);
251355163Sshin	RTFLAG("H", RTF_HOST);
251455163Sshin	RTFLAG("R", RTF_REJECT);
251555163Sshin	RTFLAG("D", RTF_DYNAMIC);
251655163Sshin	RTFLAG("M", RTF_MODIFIED);
251755163Sshin	RTFLAG("d", RTF_DONE);
251855163Sshin#ifdef	RTF_MASK
251955163Sshin	RTFLAG("m", RTF_MASK);
252055163Sshin#endif
2521186119Sqingli#ifdef RTF_CLONING
252255163Sshin	RTFLAG("C", RTF_CLONING);
2523186119Sqingli#endif
252478064Sume#ifdef RTF_CLONED
252578064Sume	RTFLAG("c", RTF_CLONED);
252678064Sume#endif
252778064Sume#ifdef RTF_PRCLONING
252878064Sume	RTFLAG("c", RTF_PRCLONING);
252978064Sume#endif
253078064Sume#ifdef RTF_WASCLONED
253178064Sume	RTFLAG("W", RTF_WASCLONED);
253278064Sume#endif
253355163Sshin	RTFLAG("X", RTF_XRESOLVE);
2534186119Sqingli#ifdef RTF_LLINFO
253555163Sshin	RTFLAG("L", RTF_LLINFO);
2536186119Sqingli#endif
253755163Sshin	RTFLAG("S", RTF_STATIC);
253855163Sshin	RTFLAG("B", RTF_BLACKHOLE);
253978064Sume#ifdef RTF_PROTO3
254078064Sume	RTFLAG("3", RTF_PROTO3);
254178064Sume#endif
254255163Sshin	RTFLAG("2", RTF_PROTO2);
254355163Sshin	RTFLAG("1", RTF_PROTO1);
254478064Sume#ifdef RTF_BROADCAST
254578064Sume	RTFLAG("b", RTF_BROADCAST);
254678064Sume#endif
254778064Sume#ifdef RTF_DEFAULT
254878064Sume	RTFLAG("d", RTF_DEFAULT);
254978064Sume#endif
255078064Sume#ifdef RTF_ISAROUTER
255178064Sume	RTFLAG("r", RTF_ISAROUTER);
255278064Sume#endif
255378064Sume#ifdef RTF_TUNNEL
255478064Sume	RTFLAG("T", RTF_TUNNEL);
255578064Sume#endif
255678064Sume#ifdef RTF_AUTH
255778064Sume	RTFLAG("A", RTF_AUTH);
255878064Sume#endif
255978064Sume#ifdef RTF_CRYPT
256078064Sume	RTFLAG("E", RTF_CRYPT);
256178064Sume#endif
256255163Sshin#undef RTFLAG
256355163Sshin	return buf;
256455163Sshin}
256555163Sshin
256655163Sshinconst char *
2567243232Shrsifflags(int flags)
256855163Sshin{
256955163Sshin	static char buf[BUFSIZ];
257055163Sshin
257178064Sume	strlcpy(buf, "", sizeof(buf));
257262607Sitojun#define	IFFLAG(s, f) \
257362607Sitojundo { \
2574119040Sume	if (flags & (f)) { \
257562607Sitojun		if (buf[0]) \
257678064Sume			strlcat(buf, ",", sizeof(buf)); \
2577119040Sume		strlcat(buf, (s), sizeof(buf)); \
257862607Sitojun	} \
257962607Sitojun} while (0)
258055163Sshin	IFFLAG("UP", IFF_UP);
258155163Sshin	IFFLAG("BROADCAST", IFF_BROADCAST);
258255163Sshin	IFFLAG("DEBUG", IFF_DEBUG);
258355163Sshin	IFFLAG("LOOPBACK", IFF_LOOPBACK);
258455163Sshin	IFFLAG("POINTOPOINT", IFF_POINTOPOINT);
258555163Sshin#ifdef IFF_NOTRAILERS
258655163Sshin	IFFLAG("NOTRAILERS", IFF_NOTRAILERS);
258755163Sshin#endif
258878064Sume#ifdef IFF_SMART
258978064Sume	IFFLAG("SMART", IFF_SMART);
259078064Sume#endif
259155163Sshin	IFFLAG("RUNNING", IFF_RUNNING);
259255163Sshin	IFFLAG("NOARP", IFF_NOARP);
259355163Sshin	IFFLAG("PROMISC", IFF_PROMISC);
259455163Sshin	IFFLAG("ALLMULTI", IFF_ALLMULTI);
259555163Sshin	IFFLAG("OACTIVE", IFF_OACTIVE);
259655163Sshin	IFFLAG("SIMPLEX", IFF_SIMPLEX);
259755163Sshin	IFFLAG("LINK0", IFF_LINK0);
259855163Sshin	IFFLAG("LINK1", IFF_LINK1);
259955163Sshin	IFFLAG("LINK2", IFF_LINK2);
260055163Sshin	IFFLAG("MULTICAST", IFF_MULTICAST);
260155163Sshin#undef IFFLAG
260255163Sshin	return buf;
260355163Sshin}
260455163Sshin
260555163Sshinvoid
2606243232Shrskrtread(int again)
260755163Sshin{
260855163Sshin	int mib[6];
260955163Sshin	size_t msize;
261055163Sshin	char *buf, *p, *lim;
261155163Sshin	struct rt_msghdr *rtm;
261255163Sshin	int retry;
261355163Sshin	const char *errmsg;
261455163Sshin
261555163Sshin	retry = 0;
261655163Sshin	buf = NULL;
261755163Sshin	mib[0] = CTL_NET;
261855163Sshin	mib[1] = PF_ROUTE;
261955163Sshin	mib[2] = 0;
262055163Sshin	mib[3] = AF_INET6;	/* Address family */
262155163Sshin	mib[4] = NET_RT_DUMP;	/* Dump the kernel routing table */
262255163Sshin	mib[5] = 0;		/* No flags */
262355163Sshin	do {
2624243232Shrs		if (retry)
2625243232Shrs			sleep(1);
262655163Sshin		retry++;
262755163Sshin		errmsg = NULL;
262855163Sshin		if (buf)
262955163Sshin			free(buf);
263055163Sshin		if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
263155163Sshin			errmsg = "sysctl estimate";
263255163Sshin			continue;
263355163Sshin		}
263455163Sshin		if ((buf = malloc(msize)) == NULL) {
263555163Sshin			errmsg = "malloc";
263655163Sshin			continue;
263755163Sshin		}
263855163Sshin		if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
263955163Sshin			errmsg = "sysctl NET_RT_DUMP";
264055163Sshin			continue;
264155163Sshin		}
2642243233Shrs	} while (retry < RT_DUMP_MAXRETRY && errmsg != NULL);
264378064Sume	if (errmsg) {
264469279Sume		fatal("%s (with %d retries, msize=%lu)", errmsg, retry,
264569279Sume		    (u_long)msize);
264678064Sume		/*NOTREACHED*/
264778064Sume	} else if (1 < retry)
264855163Sshin		syslog(LOG_INFO, "NET_RT_DUMP %d retires", retry);
264955163Sshin
265055163Sshin	lim = buf + msize;
265155163Sshin	for (p = buf; p < lim; p += rtm->rtm_msglen) {
265255163Sshin		rtm = (struct rt_msghdr *)p;
265355163Sshin		rt_entry(rtm, again);
265455163Sshin	}
265555163Sshin	free(buf);
265655163Sshin}
265755163Sshin
265855163Sshinvoid
2659243232Shrsrt_entry(struct rt_msghdr *rtm, int again)
266055163Sshin{
266155163Sshin	struct	sockaddr_in6 *sin6_dst, *sin6_gw, *sin6_mask;
266255163Sshin	struct	sockaddr_in6 *sin6_genmask, *sin6_ifp;
266355163Sshin	char	*rtmp, *ifname = NULL;
266478064Sume	struct	riprt *rrt, *orrt;
266555163Sshin	struct	netinfo6 *np;
2666243232Shrs	int ifindex;
266755163Sshin
266855163Sshin	sin6_dst = sin6_gw = sin6_mask = sin6_genmask = sin6_ifp = 0;
266955163Sshin	if ((rtm->rtm_flags & RTF_UP) == 0 || rtm->rtm_flags &
2670186119Sqingli		(RTF_XRESOLVE|RTF_BLACKHOLE)) {
267155163Sshin		return;		/* not interested in the link route */
267262607Sitojun	}
267369279Sume	/* do not look at cloned routes */
267469279Sume#ifdef RTF_WASCLONED
267569279Sume	if (rtm->rtm_flags & RTF_WASCLONED)
267669279Sume		return;
267769279Sume#endif
267869279Sume#ifdef RTF_CLONED
267969279Sume	if (rtm->rtm_flags & RTF_CLONED)
268069279Sume		return;
268169279Sume#endif
2682243233Shrs	/* XXX: Ignore connected routes. */
2683243233Shrs	if (!(rtm->rtm_flags & (RTF_GATEWAY|RTF_HOST|RTF_STATIC)))
2684243233Shrs		return;
268569279Sume	/*
268669279Sume	 * do not look at dynamic routes.
268769279Sume	 * netbsd/openbsd cloned routes have UGHD.
268869279Sume	 */
268969279Sume	if (rtm->rtm_flags & RTF_DYNAMIC)
269069279Sume		return;
269155163Sshin	rtmp = (char *)(rtm + 1);
269255163Sshin	/* Destination */
269355163Sshin	if ((rtm->rtm_addrs & RTA_DST) == 0)
269455163Sshin		return;		/* ignore routes without destination address */
269555163Sshin	sin6_dst = (struct sockaddr_in6 *)rtmp;
269664631Sitojun	rtmp += ROUNDUP(sin6_dst->sin6_len);
269755163Sshin	if (rtm->rtm_addrs & RTA_GATEWAY) {
269855163Sshin		sin6_gw = (struct sockaddr_in6 *)rtmp;
269955163Sshin		rtmp += ROUNDUP(sin6_gw->sin6_len);
270055163Sshin	}
270155163Sshin	if (rtm->rtm_addrs & RTA_NETMASK) {
270255163Sshin		sin6_mask = (struct sockaddr_in6 *)rtmp;
270355163Sshin		rtmp += ROUNDUP(sin6_mask->sin6_len);
270455163Sshin	}
270555163Sshin	if (rtm->rtm_addrs & RTA_GENMASK) {
270655163Sshin		sin6_genmask = (struct sockaddr_in6 *)rtmp;
270755163Sshin		rtmp += ROUNDUP(sin6_genmask->sin6_len);
270855163Sshin	}
270955163Sshin	if (rtm->rtm_addrs & RTA_IFP) {
271055163Sshin		sin6_ifp = (struct sockaddr_in6 *)rtmp;
271155163Sshin		rtmp += ROUNDUP(sin6_ifp->sin6_len);
271255163Sshin	}
271355163Sshin
271455163Sshin	/* Destination */
271555163Sshin	if (sin6_dst->sin6_family != AF_INET6)
271655163Sshin		return;
271755163Sshin	if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst->sin6_addr))
271855163Sshin		return;		/* Link-local */
271955163Sshin	if (IN6_ARE_ADDR_EQUAL(&sin6_dst->sin6_addr, &in6addr_loopback))
272055163Sshin		return;		/* Loopback */
272155163Sshin	if (IN6_IS_ADDR_MULTICAST(&sin6_dst->sin6_addr))
272255163Sshin		return;
272355163Sshin
272478064Sume	if ((rrt = MALLOC(struct riprt)) == NULL) {
272555163Sshin		fatal("malloc: struct riprt");
272678064Sume		/*NOTREACHED*/
272778064Sume	}
272862607Sitojun	memset(rrt, 0, sizeof(*rrt));
272955163Sshin	np = &rrt->rrt_info;
273055163Sshin	rrt->rrt_same = NULL;
273155163Sshin	rrt->rrt_t = time(NULL);
273255163Sshin	if (aflag == 0 && (rtm->rtm_flags & RTF_STATIC))
273355163Sshin		rrt->rrt_t = 0;	/* Don't age static routes */
2734243233Shrs	if (rtm->rtm_flags & Pflag)
2735243233Shrs		rrt->rrt_t = 0;	/* Don't age PROTO[123] routes */
2736122677Sume	if ((rtm->rtm_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST)
2737122677Sume		rrt->rrt_t = 0;	/* Don't age non-gateway host routes */
273855163Sshin	np->rip6_tag = 0;
273955163Sshin	np->rip6_metric = rtm->rtm_rmx.rmx_hopcount;
274055163Sshin	if (np->rip6_metric < 1)
274155163Sshin		np->rip6_metric = 1;
274255163Sshin	rrt->rrt_flags = rtm->rtm_flags;
274355163Sshin	np->rip6_dest = sin6_dst->sin6_addr;
274455163Sshin
274555163Sshin	/* Mask or plen */
274655163Sshin	if (rtm->rtm_flags & RTF_HOST)
274755163Sshin		np->rip6_plen = 128;	/* Host route */
274878064Sume	else if (sin6_mask)
274978064Sume		np->rip6_plen = sin6mask2len(sin6_mask);
275078064Sume	else
275155163Sshin		np->rip6_plen = 0;
275255163Sshin
2753243232Shrs	orrt = rtsearch(np);
275478064Sume	if (orrt && orrt->rrt_info.rip6_metric != HOPCNT_INFINITY6) {
275555163Sshin		/* Already found */
275655163Sshin		if (!again) {
275755163Sshin			trace(1, "route: %s/%d flags %s: already registered\n",
275855163Sshin				inet6_n2p(&np->rip6_dest), np->rip6_plen,
275955163Sshin				rtflags(rtm));
276055163Sshin		}
276155163Sshin		free(rrt);
276255163Sshin		return;
276355163Sshin	}
276455163Sshin	/* Gateway */
276555163Sshin	if (!sin6_gw)
276655163Sshin		memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
276755163Sshin	else {
276855163Sshin		if (sin6_gw->sin6_family == AF_INET6)
276955163Sshin			rrt->rrt_gw = sin6_gw->sin6_addr;
277055163Sshin		else if (sin6_gw->sin6_family == AF_LINK) {
277155163Sshin			/* XXX in case ppp link? */
277255163Sshin			rrt->rrt_gw = in6addr_loopback;
277355163Sshin		} else
277455163Sshin			memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
277555163Sshin	}
277655163Sshin	trace(1, "route: %s/%d flags %s",
277755163Sshin		inet6_n2p(&np->rip6_dest), np->rip6_plen, rtflags(rtm));
277855163Sshin	trace(1, " gw %s", inet6_n2p(&rrt->rrt_gw));
277955163Sshin
278055163Sshin	/* Interface */
2781243232Shrs	ifindex = rtm->rtm_index;
2782243232Shrs	if ((unsigned int)ifindex < nindex2ifc && index2ifc[ifindex])
2783243232Shrs		ifname = index2ifc[ifindex]->ifc_name;
278458070Sshin	else {
278558070Sshin		trace(1, " not configured\n");
278662607Sitojun		free(rrt);
278758070Sshin		return;
278858070Sshin	}
2789243232Shrs	trace(1, " if %s sock %d", ifname, ifindex);
2790243232Shrs	rrt->rrt_index = ifindex;
279155163Sshin
279262607Sitojun	trace(1, "\n");
279362607Sitojun
279455163Sshin	/* Check gateway */
279555163Sshin	if (!IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_gw) &&
2796122677Sume	    !IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw) &&
2797122677Sume	    (rrt->rrt_flags & RTF_LOCAL) == 0) {
279855163Sshin		trace(0, "***** Gateway %s is not a link-local address.\n",
279955163Sshin			inet6_n2p(&rrt->rrt_gw));
280055163Sshin		trace(0, "*****     dest(%s) if(%s) -- Not optimized.\n",
280162607Sitojun			inet6_n2p(&rrt->rrt_info.rip6_dest), ifname);
280262607Sitojun		rrt->rrt_rflags |= RRTF_NH_NOT_LLADDR;
280355163Sshin	}
280455163Sshin
280555163Sshin	/* Put it to the route list */
280678064Sume	if (orrt && orrt->rrt_info.rip6_metric == HOPCNT_INFINITY6) {
280778064Sume		/* replace route list */
2808243232Shrs		TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2809243232Shrs		TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
2810243232Shrs
281178064Sume		trace(1, "route: %s/%d flags %s: replace new route\n",
281278064Sume		    inet6_n2p(&np->rip6_dest), np->rip6_plen,
281378064Sume		    rtflags(rtm));
2814243232Shrs		free(orrt);
2815243232Shrs	} else
2816243232Shrs		TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
281755163Sshin}
281855163Sshin
281955163Sshinint
2820243232Shrsaddroute(struct riprt *rrt,
2821243232Shrs	const struct in6_addr *gw,
2822243232Shrs	struct ifc *ifcp)
282355163Sshin{
282455163Sshin	struct	netinfo6 *np;
282555163Sshin	u_char	buf[BUFSIZ], buf1[BUFSIZ], buf2[BUFSIZ];
282655163Sshin	struct	rt_msghdr	*rtm;
2827119031Sume	struct	sockaddr_in6	*sin6;
282855163Sshin	int	len;
282955163Sshin
283055163Sshin	np = &rrt->rrt_info;
283178064Sume	inet_ntop(AF_INET6, (const void *)gw, (char *)buf1, sizeof(buf1));
283255163Sshin	inet_ntop(AF_INET6, (void *)&ifcp->ifc_mylladdr, (char *)buf2, sizeof(buf2));
283355163Sshin	tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n",
283455163Sshin		inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
283555163Sshin		np->rip6_metric - 1, buf2);
283655163Sshin	if (rtlog)
283755163Sshin		fprintf(rtlog, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(),
283855163Sshin			inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
283955163Sshin			np->rip6_metric - 1, buf2);
284055163Sshin	if (nflag)
284155163Sshin		return 0;
284255163Sshin
284355163Sshin	memset(buf, 0, sizeof(buf));
284455163Sshin	rtm = (struct rt_msghdr *)buf;
284555163Sshin	rtm->rtm_type = RTM_ADD;
284655163Sshin	rtm->rtm_version = RTM_VERSION;
284755163Sshin	rtm->rtm_seq = ++seq;
284855163Sshin	rtm->rtm_pid = pid;
284962607Sitojun	rtm->rtm_flags = rrt->rrt_flags;
2850243233Shrs	rtm->rtm_flags |= Qflag;
285155163Sshin	rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
285255163Sshin	rtm->rtm_rmx.rmx_hopcount = np->rip6_metric - 1;
285355163Sshin	rtm->rtm_inits = RTV_HOPCOUNT;
2854119031Sume	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
285555163Sshin	/* Destination */
2856119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2857119031Sume	sin6->sin6_family = AF_INET6;
2858119031Sume	sin6->sin6_addr = np->rip6_dest;
2859119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
286055163Sshin	/* Gateway */
2861119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2862119031Sume	sin6->sin6_family = AF_INET6;
2863119031Sume	sin6->sin6_addr = *gw;
2864119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
286555163Sshin	/* Netmask */
2866119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2867119031Sume	sin6->sin6_family = AF_INET6;
2868119031Sume	sin6->sin6_addr = *(plen2mask(np->rip6_plen));
2869119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
287055163Sshin
2871119031Sume	len = (char *)sin6 - (char *)buf;
287255163Sshin	rtm->rtm_msglen = len;
287355163Sshin	if (write(rtsock, buf, len) > 0)
287455163Sshin		return 0;
287555163Sshin
287655163Sshin	if (errno == EEXIST) {
287755163Sshin		trace(0, "ADD: Route already exists %s/%d gw %s\n",
2878119035Sume		    inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
287955163Sshin		if (rtlog)
288055163Sshin			fprintf(rtlog, "ADD: Route already exists %s/%d gw %s\n",
2881119035Sume			    inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
288255163Sshin	} else {
288355163Sshin		trace(0, "Can not write to rtsock (addroute): %s\n",
2884119035Sume		    strerror(errno));
288555163Sshin		if (rtlog)
288655163Sshin			fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2887119035Sume			    strerror(errno));
288855163Sshin	}
288955163Sshin	return -1;
289055163Sshin}
289155163Sshin
289255163Sshinint
2893243232Shrsdelroute(struct netinfo6 *np, struct in6_addr *gw)
289455163Sshin{
289555163Sshin	u_char	buf[BUFSIZ], buf2[BUFSIZ];
289655163Sshin	struct	rt_msghdr	*rtm;
2897119031Sume	struct	sockaddr_in6	*sin6;
289855163Sshin	int	len;
289955163Sshin
290055163Sshin	inet_ntop(AF_INET6, (void *)gw, (char *)buf2, sizeof(buf2));
290155163Sshin	tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np->rip6_dest),
290255163Sshin		np->rip6_plen, buf2);
290355163Sshin	if (rtlog)
290455163Sshin		fprintf(rtlog, "%s: DEL: %s/%d gw %s\n",
290555163Sshin			hms(), inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
290655163Sshin	if (nflag)
290755163Sshin		return 0;
290855163Sshin
290955163Sshin	memset(buf, 0, sizeof(buf));
291055163Sshin	rtm = (struct rt_msghdr *)buf;
291155163Sshin	rtm->rtm_type = RTM_DELETE;
291255163Sshin	rtm->rtm_version = RTM_VERSION;
291355163Sshin	rtm->rtm_seq = ++seq;
291455163Sshin	rtm->rtm_pid = pid;
291555163Sshin	rtm->rtm_flags = RTF_UP | RTF_GATEWAY;
2916243233Shrs	rtm->rtm_flags |= Qflag;
291778064Sume	if (np->rip6_plen == sizeof(struct in6_addr) * 8)
291878064Sume		rtm->rtm_flags |= RTF_HOST;
291955163Sshin	rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2920119031Sume	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
292155163Sshin	/* Destination */
2922119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2923119031Sume	sin6->sin6_family = AF_INET6;
2924119031Sume	sin6->sin6_addr = np->rip6_dest;
2925119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
292655163Sshin	/* Gateway */
2927119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2928119031Sume	sin6->sin6_family = AF_INET6;
2929119031Sume	sin6->sin6_addr = *gw;
2930119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
293155163Sshin	/* Netmask */
2932119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2933119031Sume	sin6->sin6_family = AF_INET6;
2934119031Sume	sin6->sin6_addr = *(plen2mask(np->rip6_plen));
2935119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
293655163Sshin
2937119031Sume	len = (char *)sin6 - (char *)buf;
293855163Sshin	rtm->rtm_msglen = len;
293955163Sshin	if (write(rtsock, buf, len) >= 0)
294055163Sshin		return 0;
294155163Sshin
294255163Sshin	if (errno == ESRCH) {
294355163Sshin		trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n",
2944119035Sume		    inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
294555163Sshin		if (rtlog)
294655163Sshin			fprintf(rtlog, "RTDEL: Route does not exist: %s/%d gw %s\n",
2947119035Sume			    inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
294855163Sshin	} else {
294955163Sshin		trace(0, "Can not write to rtsock (delroute): %s\n",
2950119035Sume		    strerror(errno));
295155163Sshin		if (rtlog)
295255163Sshin			fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2953119035Sume			    strerror(errno));
295455163Sshin	}
295555163Sshin	return -1;
295655163Sshin}
295755163Sshin
295855163Sshinstruct in6_addr *
2959243232Shrsgetroute(struct netinfo6 *np, struct in6_addr *gw)
296055163Sshin{
296155163Sshin	u_char buf[BUFSIZ];
2962119085Sume	int myseq;
296355163Sshin	int len;
296455163Sshin	struct rt_msghdr *rtm;
2965119031Sume	struct sockaddr_in6 *sin6;
296655163Sshin
296755163Sshin	rtm = (struct rt_msghdr *)buf;
296855163Sshin	len = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in6);
296955163Sshin	memset(rtm, 0, len);
297055163Sshin	rtm->rtm_type = RTM_GET;
297155163Sshin	rtm->rtm_version = RTM_VERSION;
297255163Sshin	myseq = ++seq;
297355163Sshin	rtm->rtm_seq = myseq;
297455163Sshin	rtm->rtm_addrs = RTA_DST;
297555163Sshin	rtm->rtm_msglen = len;
2976119031Sume	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2977119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2978119031Sume	sin6->sin6_family = AF_INET6;
2979119031Sume	sin6->sin6_addr = np->rip6_dest;
298055163Sshin	if (write(rtsock, buf, len) < 0) {
298155163Sshin		if (errno == ESRCH)	/* No such route found */
298255163Sshin			return NULL;
298355163Sshin		perror("write to rtsock");
298478064Sume		exit(1);
298555163Sshin	}
298655163Sshin	do {
298755163Sshin		if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
298855163Sshin			perror("read from rtsock");
298978064Sume			exit(1);
299055163Sshin		}
299155163Sshin		rtm = (struct rt_msghdr *)buf;
299255163Sshin	} while (rtm->rtm_seq != myseq || rtm->rtm_pid != pid);
2993119031Sume	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
299455163Sshin	if (rtm->rtm_addrs & RTA_DST) {
2995119031Sume		sin6 = (struct sockaddr_in6 *)
2996119031Sume			((char *)sin6 + ROUNDUP(sin6->sin6_len));
299755163Sshin	}
299855163Sshin	if (rtm->rtm_addrs & RTA_GATEWAY) {
2999119031Sume		*gw = sin6->sin6_addr;
300055163Sshin		return gw;
300155163Sshin	}
300255163Sshin	return NULL;
300355163Sshin}
300455163Sshin
300555163Sshinconst char *
3006243232Shrsinet6_n2p(const struct in6_addr *p)
300755163Sshin{
300855163Sshin	static char buf[BUFSIZ];
300955163Sshin
301078064Sume	return inet_ntop(AF_INET6, (const void *)p, buf, sizeof(buf));
301155163Sshin}
301255163Sshin
301355163Sshinvoid
3014243232Shrsifrtdump(int sig)
301555163Sshin{
301655163Sshin
301755163Sshin	ifdump(sig);
301855163Sshin	rtdump(sig);
301955163Sshin}
302055163Sshin
302155163Sshinvoid
3022243232Shrsifdump(int sig)
302355163Sshin{
302455163Sshin	struct ifc *ifcp;
302555163Sshin	FILE *dump;
3026243232Shrs	int nifc = 0;
302755163Sshin
302855163Sshin	if (sig == 0)
302955163Sshin		dump = stderr;
303055163Sshin	else
303155163Sshin		if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
303255163Sshin			dump = stderr;
303355163Sshin
303455163Sshin	fprintf(dump, "%s: Interface Table Dump\n", hms());
3035243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next)
3036243232Shrs		nifc++;
303755163Sshin	fprintf(dump, "  Number of interfaces: %d\n", nifc);
3038243232Shrs
3039243232Shrs	fprintf(dump, "  advertising interfaces:\n");
3040243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3041243232Shrs		if ((ifcp->ifc_flags & IFF_UP) == 0)
3042243232Shrs			continue;
3043243232Shrs		if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
3044243232Shrs			continue;
3045243232Shrs		ifdump0(dump, ifcp);
304655163Sshin	}
304755163Sshin	fprintf(dump, "\n");
3048243232Shrs	fprintf(dump, "  non-advertising interfaces:\n");
3049243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3050243232Shrs		if ((ifcp->ifc_flags & IFF_UP) &&
3051243232Shrs		    (iff_find(ifcp, IFIL_TYPE_N) == NULL))
3052243232Shrs			continue;
3053243232Shrs		ifdump0(dump, ifcp);
3054243232Shrs	}
3055243232Shrs	fprintf(dump, "\n");
305655163Sshin	if (dump != stderr)
305755163Sshin		fclose(dump);
305855163Sshin}
305955163Sshin
306055163Sshinvoid
3061243232Shrsifdump0(FILE *dump, const struct ifc *ifcp)
306255163Sshin{
3063243232Shrs	struct ifac *ifac;
306455163Sshin	struct iff *iffp;
306555163Sshin	char buf[BUFSIZ];
306655163Sshin	const char *ft;
306755163Sshin	int addr;
306855163Sshin
306955163Sshin	fprintf(dump, "    %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n",
307055163Sshin		ifcp->ifc_name, ifcp->ifc_index, ifflags(ifcp->ifc_flags),
307155163Sshin		inet6_n2p(&ifcp->ifc_mylladdr),
307255163Sshin		ifcp->ifc_mtu, ifcp->ifc_metric);
3073243232Shrs	TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
307455163Sshin		if (ifcp->ifc_flags & IFF_POINTOPOINT) {
3075243232Shrs			inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr,
307655163Sshin				buf, sizeof(buf));
307755163Sshin			fprintf(dump, "\t%s/%d -- %s\n",
3078243232Shrs				inet6_n2p(&ifac->ifac_addr),
3079243232Shrs				ifac->ifac_plen, buf);
308055163Sshin		} else {
308155163Sshin			fprintf(dump, "\t%s/%d\n",
3082243232Shrs				inet6_n2p(&ifac->ifac_addr),
3083243232Shrs				ifac->ifac_plen);
308455163Sshin		}
308555163Sshin	}
3086243232Shrs
3087243232Shrs	fprintf(dump, "\tFilter:\n");
3088243232Shrs	TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
3089243232Shrs		addr = 0;
3090243232Shrs		switch (iffp->iff_type) {
3091243232Shrs		case IFIL_TYPE_A:
3092243232Shrs			ft = "Aggregate"; addr++; break;
3093243232Shrs		case IFIL_TYPE_N:
3094243232Shrs			ft = "No-use"; break;
3095243232Shrs		case IFIL_TYPE_O:
3096243232Shrs			ft = "Advertise-only"; addr++; break;
3097243232Shrs		case IFIL_TYPE_T:
3098243232Shrs			ft = "Default-only"; break;
3099243232Shrs		case IFIL_TYPE_L:
3100243232Shrs			ft = "Listen-only"; addr++; break;
3101243232Shrs		default:
3102243232Shrs			snprintf(buf, sizeof(buf), "Unknown-%c", iffp->iff_type);
3103243232Shrs			ft = buf;
3104243232Shrs			addr++;
3105243232Shrs			break;
310655163Sshin		}
3107243232Shrs		fprintf(dump, "\t\t%s", ft);
3108243232Shrs		if (addr)
3109243232Shrs			fprintf(dump, "(%s/%d)", inet6_n2p(&iffp->iff_addr),
3110243232Shrs				iffp->iff_plen);
311155163Sshin		fprintf(dump, "\n");
311255163Sshin	}
3113243232Shrs	fprintf(dump, "\n");
311455163Sshin}
311555163Sshin
311655163Sshinvoid
3117243232Shrsrtdump(int sig)
311855163Sshin{
311955163Sshin	struct	riprt *rrt;
312055163Sshin	char	buf[BUFSIZ];
312155163Sshin	FILE	*dump;
312255163Sshin	time_t	t, age;
312355163Sshin
312455163Sshin	if (sig == 0)
312555163Sshin		dump = stderr;
312655163Sshin	else
312755163Sshin		if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
312855163Sshin			dump = stderr;
312955163Sshin
313055163Sshin	t = time(NULL);
313155163Sshin	fprintf(dump, "\n%s: Routing Table Dump\n", hms());
3132243232Shrs	TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
313355163Sshin		if (rrt->rrt_t == 0)
313455163Sshin			age = 0;
313555163Sshin		else
313655163Sshin			age = t - rrt->rrt_t;
313755163Sshin		inet_ntop(AF_INET6, (void *)&rrt->rrt_info.rip6_dest,
313855163Sshin			buf, sizeof(buf));
313955163Sshin		fprintf(dump, "    %s/%d if(%d:%s) gw(%s) [%d] age(%ld)",
314055163Sshin			buf, rrt->rrt_info.rip6_plen, rrt->rrt_index,
314155163Sshin			index2ifc[rrt->rrt_index]->ifc_name,
314255163Sshin			inet6_n2p(&rrt->rrt_gw),
314355163Sshin			rrt->rrt_info.rip6_metric, (long)age);
314455163Sshin		if (rrt->rrt_info.rip6_tag) {
314555163Sshin			fprintf(dump, " tag(0x%04x)",
314655163Sshin				ntohs(rrt->rrt_info.rip6_tag) & 0xffff);
314755163Sshin		}
314862607Sitojun		if (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)
314955163Sshin			fprintf(dump, " NOT-LL");
315062607Sitojun		if (rrt->rrt_rflags & RRTF_NOADVERTISE)
315155163Sshin			fprintf(dump, " NO-ADV");
315255163Sshin		fprintf(dump, "\n");
315355163Sshin	}
315455163Sshin	fprintf(dump, "\n");
315555163Sshin	if (dump != stderr)
315655163Sshin		fclose(dump);
315755163Sshin}
315855163Sshin
315955163Sshin/*
316055163Sshin * Parse the -A (and -O) options and put corresponding filter object to the
316178064Sume * specified interface structures.  Each of the -A/O option has the following
316255163Sshin * syntax:	-A 5f09:c400::/32,ef0,ef1  (aggregate)
316355163Sshin * 		-O 5f09:c400::/32,ef0,ef1  (only when match)
316455163Sshin */
316555163Sshinvoid
3166243232Shrsfilterconfig(void)
316755163Sshin{
316855163Sshin	int i;
3169119083Sume	char *p, *ap, *iflp, *ifname, *ep;
3170243232Shrs	struct iff iff, *iffp;
317178064Sume	struct ifc *ifcp;
317278064Sume	struct riprt *rrt;
317364631Sitojun#if 0
317478064Sume	struct in6_addr gw;
317564631Sitojun#endif
3176119083Sume	u_long plen;
317755163Sshin
317855163Sshin	for (i = 0; i < nfilter; i++) {
317955163Sshin		ap = filter[i];
318055163Sshin		iflp = NULL;
3181243232Shrs		iffp = &iff;
3182243232Shrs		memset(iffp, 0, sizeof(*iffp));
318355163Sshin		if (filtertype[i] == 'N' || filtertype[i] == 'T') {
318455163Sshin			iflp = ap;
318555163Sshin			goto ifonly;
318655163Sshin		}
3187119038Sume		if ((p = strchr(ap, ',')) != NULL) {
318855163Sshin			*p++ = '\0';
318955163Sshin			iflp = p;
319055163Sshin		}
3191119038Sume		if ((p = strchr(ap, '/')) == NULL) {
319255163Sshin			fatal("no prefixlen specified for '%s'", ap);
319378064Sume			/*NOTREACHED*/
319478064Sume		}
319555163Sshin		*p++ = '\0';
3196243232Shrs		if (inet_pton(AF_INET6, ap, &iffp->iff_addr) != 1) {
319755163Sshin			fatal("invalid prefix specified for '%s'", ap);
319878064Sume			/*NOTREACHED*/
319978064Sume		}
3200119083Sume		errno = 0;
3201119083Sume		ep = NULL;
3202119083Sume		plen = strtoul(p, &ep, 10);
3203243232Shrs		if (errno || !*p || *ep || plen > sizeof(iffp->iff_addr) * 8) {
3204119083Sume			fatal("invalid prefix length specified for '%s'", ap);
3205119083Sume			/*NOTREACHED*/
3206119083Sume		}
3207243232Shrs		iffp->iff_plen = plen;
3208243232Shrs		applyplen(&iffp->iff_addr, iffp->iff_plen);
320955163Sshinifonly:
3210243232Shrs		iffp->iff_type = filtertype[i];
321178064Sume		if (iflp == NULL || *iflp == '\0') {
321255163Sshin			fatal("no interface specified for '%s'", ap);
321378064Sume			/*NOTREACHED*/
321478064Sume		}
321555163Sshin		/* parse the interface listing portion */
321655163Sshin		while (iflp) {
321755163Sshin			ifname = iflp;
3218119038Sume			if ((iflp = strchr(iflp, ',')) != NULL)
321955163Sshin				*iflp++ = '\0';
3220243232Shrs
3221243232Shrs			TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3222243233Shrs				if (fnmatch(ifname, ifcp->ifc_name, 0) != 0)
3223243232Shrs					continue;
3224243232Shrs
3225243232Shrs				iffp = malloc(sizeof(*iffp));
3226243232Shrs				if (iffp == NULL) {
3227243232Shrs					fatal("malloc of iff");
3228243232Shrs					/*NOTREACHED*/
3229243232Shrs				}
3230243232Shrs				memcpy(iffp, &iff, sizeof(*iffp));
3231243233Shrs#if 0
3232243233Shrs				syslog(LOG_INFO, "Add filter: type %d, ifname %s.", iffp->iff_type, ifname);
3233243233Shrs#endif
3234243232Shrs				TAILQ_INSERT_HEAD(&ifcp->ifc_iff_head, iffp, iff_next);
323578064Sume			}
323655163Sshin		}
323778064Sume
323878064Sume		/*
323978064Sume		 * -A: aggregate configuration.
324078064Sume		 */
3241243232Shrs		if (filtertype[i] != IFIL_TYPE_A)
324255163Sshin			continue;
324355163Sshin		/* put the aggregate to the kernel routing table */
324455163Sshin		rrt = (struct riprt *)malloc(sizeof(struct riprt));
324578064Sume		if (rrt == NULL) {
324655163Sshin			fatal("malloc: rrt");
324778064Sume			/*NOTREACHED*/
324878064Sume		}
324955163Sshin		memset(rrt, 0, sizeof(struct riprt));
3250243232Shrs		rrt->rrt_info.rip6_dest = iff.iff_addr;
3251243232Shrs		rrt->rrt_info.rip6_plen = iff.iff_plen;
325255163Sshin		rrt->rrt_info.rip6_metric = 1;
325355163Sshin		rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
325455163Sshin		rrt->rrt_gw = in6addr_loopback;
325562607Sitojun		rrt->rrt_flags = RTF_UP | RTF_REJECT;
325662607Sitojun		rrt->rrt_rflags = RRTF_AGGREGATE;
325755163Sshin		rrt->rrt_t = 0;
3258119039Sume		rrt->rrt_index = loopifcp->ifc_index;
325964631Sitojun#if 0
326064631Sitojun		if (getroute(&rrt->rrt_info, &gw)) {
326164631Sitojun#if 0
326264631Sitojun			/*
326364631Sitojun			 * When the address has already been registered in the
326464631Sitojun			 * kernel routing table, it should be removed
326564631Sitojun			 */
326664631Sitojun			delroute(&rrt->rrt_info, &gw);
326764631Sitojun#else
326878064Sume			/* it is safer behavior */
326964631Sitojun			errno = EINVAL;
327064631Sitojun			fatal("%s/%u already in routing table, "
327164631Sitojun			    "cannot aggregate",
327264631Sitojun			    inet6_n2p(&rrt->rrt_info.rip6_dest),
327364631Sitojun			    rrt->rrt_info.rip6_plen);
327478064Sume			/*NOTREACHED*/
327564631Sitojun#endif
327664631Sitojun		}
327764631Sitojun#endif
327855163Sshin		/* Put the route to the list */
3279243232Shrs		TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
328055163Sshin		trace(1, "Aggregate: %s/%d for %s\n",
3281243232Shrs			inet6_n2p(&iff.iff_addr), iff.iff_plen,
3282243232Shrs			loopifcp->ifc_name);
328355163Sshin		/* Add this route to the kernel */
328455163Sshin		if (nflag) 	/* do not modify kernel routing table */
328555163Sshin			continue;
328655163Sshin		addroute(rrt, &in6addr_loopback, loopifcp);
328755163Sshin	}
328855163Sshin}
328955163Sshin
329055163Sshin/***************** utility functions *****************/
329155163Sshin
329255163Sshin/*
329355163Sshin * Returns a pointer to ifac whose address and prefix length matches
329455163Sshin * with the address and prefix length specified in the arguments.
329555163Sshin */
329655163Sshinstruct ifac *
3297243232Shrsifa_match(const struct ifc *ifcp,
3298243232Shrs	const struct in6_addr *ia,
3299243232Shrs	int plen)
330055163Sshin{
3301243232Shrs	struct ifac *ifac;
330255163Sshin
3303243232Shrs	TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
3304243232Shrs		if (IN6_ARE_ADDR_EQUAL(&ifac->ifac_addr, ia) &&
3305243232Shrs		    ifac->ifac_plen == plen)
330655163Sshin			break;
330755163Sshin	}
3308243232Shrs
3309243232Shrs	return (ifac);
331055163Sshin}
331155163Sshin
331255163Sshin/*
331355163Sshin * Return a pointer to riprt structure whose address and prefix length
331455163Sshin * matches with the address and prefix length found in the argument.
331578064Sume * Note: This is not a rtalloc().  Therefore exact match is necessary.
331655163Sshin */
331755163Sshinstruct riprt *
3318243232Shrsrtsearch(struct netinfo6 *np)
331955163Sshin{
332055163Sshin	struct	riprt	*rrt;
332155163Sshin
3322243232Shrs	TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
332355163Sshin		if (rrt->rrt_info.rip6_plen == np->rip6_plen &&
332455163Sshin		    IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
332555163Sshin				       &np->rip6_dest))
3326243232Shrs			break;
332755163Sshin	}
3328243232Shrs
3329243232Shrs	return (rrt);
333055163Sshin}
333155163Sshin
333255163Sshinint
3333243232Shrssin6mask2len(const struct sockaddr_in6 *sin6)
333478064Sume{
333578064Sume
333678064Sume	return mask2len(&sin6->sin6_addr,
333778064Sume	    sin6->sin6_len - offsetof(struct sockaddr_in6, sin6_addr));
333878064Sume}
333978064Sume
334078064Sumeint
3341243232Shrsmask2len(const struct in6_addr *addr, int lenlim)
334255163Sshin{
334355163Sshin	int i = 0, j;
334478064Sume	const u_char *p = (const u_char *)addr;
334562607Sitojun
334655163Sshin	for (j = 0; j < lenlim; j++, p++) {
334755163Sshin		if (*p != 0xff)
334855163Sshin			break;
334955163Sshin		i += 8;
335055163Sshin	}
335155163Sshin	if (j < lenlim) {
335255163Sshin		switch (*p) {
335362607Sitojun#define	MASKLEN(m, l)	case m: do { i += l; break; } while (0)
335462607Sitojun		MASKLEN(0xfe, 7); break;
335562607Sitojun		MASKLEN(0xfc, 6); break;
335662607Sitojun		MASKLEN(0xf8, 5); break;
335762607Sitojun		MASKLEN(0xf0, 4); break;
335862607Sitojun		MASKLEN(0xe0, 3); break;
335962607Sitojun		MASKLEN(0xc0, 2); break;
336062607Sitojun		MASKLEN(0x80, 1); break;
336155163Sshin#undef	MASKLEN
336255163Sshin		}
336355163Sshin	}
336455163Sshin	return i;
336555163Sshin}
336655163Sshin
336755163Sshinvoid
3368243232Shrsapplymask(struct in6_addr *addr, struct in6_addr *mask)
336955163Sshin{
337055163Sshin	int	i;
337155163Sshin	u_long	*p, *q;
337255163Sshin
337355163Sshin	p = (u_long *)addr; q = (u_long *)mask;
337455163Sshin	for (i = 0; i < 4; i++)
337555163Sshin		*p++ &= *q++;
337655163Sshin}
337755163Sshin
337855163Sshinstatic const u_char plent[8] = {
337955163Sshin	0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
338055163Sshin};
338155163Sshin
338255163Sshinvoid
3383243232Shrsapplyplen(struct in6_addr *ia, int plen)
338455163Sshin{
338555163Sshin	u_char	*p;
338655163Sshin	int	i;
338755163Sshin
338855163Sshin	p = ia->s6_addr;
338955163Sshin	for (i = 0; i < 16; i++) {
339055163Sshin		if (plen <= 0)
339155163Sshin			*p = 0;
339255163Sshin		else if (plen < 8)
339355163Sshin			*p &= plent[plen];
339455163Sshin		p++, plen -= 8;
339555163Sshin	}
339655163Sshin}
339755163Sshin
339855163Sshinstatic const int pl2m[9] = {
339955163Sshin	0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
340055163Sshin};
340155163Sshin
340255163Sshinstruct in6_addr *
3403243232Shrsplen2mask(int n)
340455163Sshin{
340555163Sshin	static struct in6_addr ia;
340655163Sshin	u_char	*p;
340755163Sshin	int	i;
340855163Sshin
340955163Sshin	memset(&ia, 0, sizeof(struct in6_addr));
341055163Sshin	p = (u_char *)&ia;
341155163Sshin	for (i = 0; i < 16; i++, p++, n -= 8) {
341255163Sshin		if (n >= 8) {
341355163Sshin			*p = 0xff;
341455163Sshin			continue;
341555163Sshin		}
341655163Sshin		*p = pl2m[n];
341755163Sshin		break;
341855163Sshin	}
341955163Sshin	return &ia;
342055163Sshin}
342155163Sshin
342255163Sshinchar *
3423243232Shrsallocopy(char *p)
342455163Sshin{
3425119033Sume	int len = strlen(p) + 1;
3426119033Sume	char *q = (char *)malloc(len);
342755163Sshin
3428119033Sume	if (!q) {
3429119033Sume		fatal("malloc");
3430119033Sume		/*NOTREACHED*/
3431119033Sume	}
3432119033Sume
3433119033Sume	strlcpy(q, p, len);
343455163Sshin	return q;
343555163Sshin}
343655163Sshin
343755163Sshinchar *
3438243232Shrshms(void)
343955163Sshin{
344055163Sshin	static char buf[BUFSIZ];
344155163Sshin	time_t t;
344255163Sshin	struct	tm *tm;
344355163Sshin
344455163Sshin	t = time(NULL);
344578064Sume	if ((tm = localtime(&t)) == 0) {
344655163Sshin		fatal("localtime");
344778064Sume		/*NOTREACHED*/
344878064Sume	}
344978064Sume	snprintf(buf, sizeof(buf), "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
345078064Sume	    tm->tm_sec);
345155163Sshin	return buf;
345255163Sshin}
345355163Sshin
345455163Sshin#define	RIPRANDDEV	1.0	/* 30 +- 15, max - min = 30 */
345555163Sshin
345655163Sshinint
3457243232Shrsripinterval(int timer)
345855163Sshin{
345955163Sshin	double r = rand();
346055163Sshin
346155163Sshin	interval = (int)(timer + timer * RIPRANDDEV * (r / RAND_MAX - 0.5));
346255163Sshin	nextalarm = time(NULL) + interval;
346355163Sshin	return interval;
346455163Sshin}
346555163Sshin
346655163Sshintime_t
3467243232Shrsripsuptrig(void)
346855163Sshin{
346955163Sshin	time_t t;
347055163Sshin
347155163Sshin	double r = rand();
347262607Sitojun	t  = (int)(RIP_TRIG_INT6_MIN +
347378064Sume		(RIP_TRIG_INT6_MAX - RIP_TRIG_INT6_MIN) * (r / RAND_MAX));
347455163Sshin	sup_trig_update = time(NULL) + t;
347555163Sshin	return t;
347655163Sshin}
347755163Sshin
347855163Sshinvoid
347955163Sshin#ifdef __STDC__
348055163Sshinfatal(const char *fmt, ...)
348155163Sshin#else
348255163Sshinfatal(fmt, va_alist)
348355163Sshin	char	*fmt;
348455163Sshin	va_dcl
348555163Sshin#endif
348655163Sshin{
348755163Sshin	va_list ap;
348855163Sshin	char buf[1024];
348955163Sshin
349055163Sshin#ifdef __STDC__
349155163Sshin	va_start(ap, fmt);
349255163Sshin#else
349355163Sshin	va_start(ap);
349455163Sshin#endif
349555163Sshin	vsnprintf(buf, sizeof(buf), fmt, ap);
3496119043Sume	va_end(ap);
349755163Sshin	perror(buf);
3498119043Sume	if (errno)
3499119043Sume		syslog(LOG_ERR, "%s: %s", buf, strerror(errno));
3500119043Sume	else
3501119043Sume		syslog(LOG_ERR, "%s", buf);
350278064Sume	rtdexit();
350355163Sshin}
350455163Sshin
350555163Sshinvoid
350655163Sshin#ifdef __STDC__
350755163Sshintracet(int level, const char *fmt, ...)
350855163Sshin#else
350955163Sshintracet(level, fmt, va_alist)
351055163Sshin	int level;
351155163Sshin	char *fmt;
351255163Sshin	va_dcl
351355163Sshin#endif
351455163Sshin{
351555163Sshin	va_list ap;
351655163Sshin
3517119043Sume	if (level <= dflag) {
351855163Sshin#ifdef __STDC__
3519119043Sume		va_start(ap, fmt);
352055163Sshin#else
3521119043Sume		va_start(ap);
352255163Sshin#endif
352355163Sshin		fprintf(stderr, "%s: ", hms());
352455163Sshin		vfprintf(stderr, fmt, ap);
3525119043Sume		va_end(ap);
352655163Sshin	}
352755163Sshin	if (dflag) {
3528119043Sume#ifdef __STDC__
3529119043Sume		va_start(ap, fmt);
3530119043Sume#else
3531119043Sume		va_start(ap);
3532119043Sume#endif
353355163Sshin		if (level > 0)
353455163Sshin			vsyslog(LOG_DEBUG, fmt, ap);
353555163Sshin		else
353655163Sshin			vsyslog(LOG_WARNING, fmt, ap);
3537119043Sume		va_end(ap);
353855163Sshin	}
353955163Sshin}
354055163Sshin
354155163Sshinvoid
354255163Sshin#ifdef __STDC__
354355163Sshintrace(int level, const char *fmt, ...)
354455163Sshin#else
354555163Sshintrace(level, fmt, va_alist)
354655163Sshin	int level;
354755163Sshin	char *fmt;
354855163Sshin	va_dcl
354955163Sshin#endif
355055163Sshin{
355155163Sshin	va_list ap;
355255163Sshin
3553119043Sume	if (level <= dflag) {
355455163Sshin#ifdef __STDC__
3555119043Sume		va_start(ap, fmt);
355655163Sshin#else
3557119043Sume		va_start(ap);
355855163Sshin#endif
355955163Sshin		vfprintf(stderr, fmt, ap);
3560119043Sume		va_end(ap);
3561119043Sume	}
356255163Sshin	if (dflag) {
3563119043Sume#ifdef __STDC__
3564119043Sume		va_start(ap, fmt);
3565119043Sume#else
3566119043Sume		va_start(ap);
3567119043Sume#endif
356855163Sshin		if (level > 0)
356955163Sshin			vsyslog(LOG_DEBUG, fmt, ap);
357055163Sshin		else
357155163Sshin			vsyslog(LOG_WARNING, fmt, ap);
3572119043Sume		va_end(ap);
357355163Sshin	}
357455163Sshin}
357555163Sshin
357655163Sshinunsigned int
3577243232Shrsif_maxindex(void)
357855163Sshin{
357955163Sshin	struct if_nameindex *p, *p0;
358055163Sshin	unsigned int max = 0;
358155163Sshin
358255163Sshin	p0 = if_nameindex();
358355163Sshin	for (p = p0; p && p->if_index && p->if_name; p++) {
358455163Sshin		if (max < p->if_index)
358555163Sshin			max = p->if_index;
358655163Sshin	}
358755163Sshin	if_freenameindex(p0);
358855163Sshin	return max;
358955163Sshin}
359055163Sshin
359155163Sshinstruct ifc *
3592243232Shrsifc_find(char *name)
359355163Sshin{
359455163Sshin	struct ifc *ifcp;
359555163Sshin
3596243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
359755163Sshin		if (strcmp(name, ifcp->ifc_name) == 0)
3598243232Shrs			break;
359955163Sshin	}
3600243232Shrs	return (ifcp);
360155163Sshin}
360255163Sshin
360355163Sshinstruct iff *
3604243232Shrsiff_find(struct ifc *ifcp, int type)
360555163Sshin{
360655163Sshin	struct iff *iffp;
360755163Sshin
3608243232Shrs	TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
3609243232Shrs		if (type == IFIL_TYPE_ANY ||
3610243232Shrs		    type == iffp->iff_type)
3611243232Shrs			break;
361255163Sshin	}
3613243232Shrs
3614243232Shrs	return (iffp);
361555163Sshin}
361655163Sshin
361755163Sshinvoid
3618243232Shrssetindex2ifc(int idx, struct ifc *ifcp)
361955163Sshin{
3620122677Sume	int n, nsize;
362162607Sitojun	struct ifc **p;
362255163Sshin
362355163Sshin	if (!index2ifc) {
362455163Sshin		nindex2ifc = 5;	/*initial guess*/
362555163Sshin		index2ifc = (struct ifc **)
362655163Sshin			malloc(sizeof(*index2ifc) * nindex2ifc);
362778064Sume		if (index2ifc == NULL) {
362855163Sshin			fatal("malloc");
362978064Sume			/*NOTREACHED*/
363078064Sume		}
363155163Sshin		memset(index2ifc, 0, sizeof(*index2ifc) * nindex2ifc);
363255163Sshin	}
363355163Sshin	n = nindex2ifc;
3634122677Sume	for (nsize = nindex2ifc; nsize <= idx; nsize *= 2)
3635122677Sume		;
3636122677Sume	if (n != nsize) {
363762607Sitojun		p = (struct ifc **)realloc(index2ifc,
3638122677Sume		    sizeof(*index2ifc) * nsize);
363978064Sume		if (p == NULL) {
364055163Sshin			fatal("realloc");
364178064Sume			/*NOTREACHED*/
364278064Sume		}
364378064Sume		memset(p + n, 0, sizeof(*index2ifc) * (nindex2ifc - n));
364462607Sitojun		index2ifc = p;
3645122677Sume		nindex2ifc = nsize;
364655163Sshin	}
364778064Sume	index2ifc[idx] = ifcp;
364855163Sshin}
3649