route6d.c revision 299491
162607Sitojun/*	$FreeBSD: head/usr.sbin/route6d/route6d.c 299491 2016-05-11 22:33:20Z cem $	*/
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/route.h>
6755163Sshin#include <netinet/in.h>
6855163Sshin#include <netinet/in_var.h>
6955163Sshin#include <netinet/ip6.h>
7055163Sshin#include <netinet/udp.h>
7155163Sshin#include <netdb.h>
7262607Sitojun#include <ifaddrs.h>
7355163Sshin
7455163Sshin#include <arpa/inet.h>
7555163Sshin
7655163Sshin#include "route6d.h"
7755163Sshin
7855163Sshin#define	MAXFILTER	40
79243233Shrs#define RT_DUMP_MAXRETRY	15
8055163Sshin
8155163Sshin#ifdef	DEBUG
8255163Sshin#define	INIT_INTERVAL6	6
8355163Sshin#else
84119039Sume#define	INIT_INTERVAL6	10	/* Wait to submit an initial riprequest */
8555163Sshin#endif
8655163Sshin
8755163Sshin/* alignment constraint for routing socket */
8862607Sitojun#define ROUNDUP(a) \
8955163Sshin	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
9062607Sitojun#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
9155163Sshin
92243232Shrsstruct ifc {			/* Configuration of an interface */
93243232Shrs	TAILQ_ENTRY(ifc) ifc_next;
94243232Shrs
95243232Shrs	char	ifc_name[IFNAMSIZ];		/* if name */
9655163Sshin	int	ifc_index;			/* if index */
9755163Sshin	int	ifc_mtu;			/* if mtu */
9855163Sshin	int	ifc_metric;			/* if metric */
9978064Sume	u_int	ifc_flags;			/* flags */
10078064Sume	short	ifc_cflags;			/* IFC_XXX */
10155163Sshin	struct	in6_addr ifc_mylladdr;		/* my link-local address */
10255163Sshin	struct	sockaddr_in6 ifc_ripsin;	/* rip multicast address */
103243232Shrs	TAILQ_HEAD(, ifac) ifc_ifac_head;	/* list of AF_INET6 addrs */
104243232Shrs	TAILQ_HEAD(, iff) ifc_iff_head;		/* list of filters */
10555163Sshin	int	ifc_joined;			/* joined to ff02::9 */
10655163Sshin};
107243232ShrsTAILQ_HEAD(, ifc) ifc_head = TAILQ_HEAD_INITIALIZER(ifc_head);
10855163Sshin
109243232Shrsstruct ifac {			/* Adddress associated to an interface */
110243232Shrs	TAILQ_ENTRY(ifac) ifac_next;
111243232Shrs
112243232Shrs	struct	ifc *ifac_ifc;		/* back pointer */
113243232Shrs	struct	in6_addr ifac_addr;	/* address */
114243232Shrs	struct	in6_addr ifac_raddr;	/* remote address, valid in p2p */
115243232Shrs	int	ifac_scope_id;		/* scope id */
116243232Shrs	int	ifac_plen;		/* prefix length */
11755163Sshin};
11855163Sshin
119243232Shrsstruct iff {			/* Filters for an interface */
120243232Shrs	TAILQ_ENTRY(iff) iff_next;
121243232Shrs
12255163Sshin	int	iff_type;
12355163Sshin	struct	in6_addr iff_addr;
12455163Sshin	int	iff_plen;
12555163Sshin};
12655163Sshin
12755163Sshinstruct	ifc **index2ifc;
128243232Shrsunsigned int	nindex2ifc;
12955163Sshinstruct	ifc *loopifcp = NULL;	/* pointing to loopback */
130119076Sume#ifdef HAVE_POLL_H
131119076Sumestruct	pollfd set[2];
132119076Sume#else
133119070Sumefd_set	*sockvecp;	/* vector to select() for receiving */
134119070Sumefd_set	*recvecp;
135119070Sumeint	fdmasks;
136119070Sumeint	maxfd;		/* maximum fd for select() */
137119076Sume#endif
13855163Sshinint	rtsock;		/* the routing socket */
13955163Sshinint	ripsock;	/* socket to send/receive RIP datagram */
14055163Sshin
14155163Sshinstruct	rip6 *ripbuf;	/* packet buffer for sending */
14255163Sshin
14355163Sshin/*
14478064Sume * Maintain the routes in a linked list.  When the number of the routes
14555163Sshin * grows, somebody would like to introduce a hash based or a radix tree
14678064Sume * based structure.  I believe the number of routes handled by RIP is
14755163Sshin * limited and I don't have to manage a complex data structure, however.
14855163Sshin *
14955163Sshin * One of the major drawbacks of the linear linked list is the difficulty
15078064Sume * of representing the relationship between a couple of routes.  This may
15155163Sshin * be a significant problem when we have to support route aggregation with
152228990Suqs * suppressing the specifics covered by the aggregate.
15355163Sshin */
15455163Sshin
155243232Shrsstruct riprt {
156243232Shrs	TAILQ_ENTRY(riprt) rrt_next;	/* next destination */
157243232Shrs
15855163Sshin	struct	riprt *rrt_same;	/* same destination - future use */
15955163Sshin	struct	netinfo6 rrt_info;	/* network info */
16055163Sshin	struct	in6_addr rrt_gw;	/* gateway */
16162607Sitojun	u_long	rrt_flags;		/* kernel routing table flags */
16262607Sitojun	u_long	rrt_rflags;		/* route6d routing table flags */
16355163Sshin	time_t	rrt_t;			/* when the route validated */
16455163Sshin	int	rrt_index;		/* ifindex from which this route got */
16555163Sshin};
166243232ShrsTAILQ_HEAD(, riprt) riprt_head = TAILQ_HEAD_INITIALIZER(riprt_head);
16755163Sshin
16855163Sshinint	dflag = 0;	/* debug flag */
16955163Sshinint	qflag = 0;	/* quiet flag */
17055163Sshinint	nflag = 0;	/* don't update kernel routing table */
17155163Sshinint	aflag = 0;	/* age out even the statically defined routes */
17255163Sshinint	hflag = 0;	/* don't split horizon */
17355163Sshinint	lflag = 0;	/* exchange site local routes */
174243233Shrsint	Pflag = 0;	/* don't age out routes with RTF_PROTO[123] */
175243233Shrsint	Qflag = RTF_PROTO2;	/* set RTF_PROTO[123] flag to routes by RIPng */
17655163Sshinint	sflag = 0;	/* announce static routes w/ split horizon */
17755163Sshinint	Sflag = 0;	/* announce static routes to every interface */
17862607Sitojununsigned long routetag = 0;	/* route tag attached on originating case */
17955163Sshin
18055163Sshinchar	*filter[MAXFILTER];
18155163Sshinint	filtertype[MAXFILTER];
18255163Sshinint	nfilter = 0;
18355163Sshin
18455163Sshinpid_t	pid;
18555163Sshin
18655163Sshinstruct	sockaddr_storage ripsin;
18755163Sshin
18855163Sshinint	interval = 1;
18955163Sshintime_t	nextalarm = 0;
19055163Sshintime_t	sup_trig_update = 0;
19155163Sshin
19255163SshinFILE	*rtlog = NULL;
19355163Sshin
19455163Sshinint logopened = 0;
19555163Sshin
196119085Sumestatic	int	seq = 0;
19755163Sshin
19878064Sumevolatile sig_atomic_t seenalrm;
19978064Sumevolatile sig_atomic_t seenquit;
20078064Sumevolatile sig_atomic_t seenusr1;
20178064Sume
20262607Sitojun#define	RRTF_AGGREGATE		0x08000000
20362607Sitojun#define	RRTF_NOADVERTISE	0x10000000
20462607Sitojun#define	RRTF_NH_NOT_LLADDR	0x20000000
20562607Sitojun#define RRTF_SENDANYWAY		0x40000000
20662607Sitojun#define	RRTF_CHANGED		0x80000000
20755163Sshin
208173412Skevloint main(int, char **);
209173412Skevlovoid sighandler(int);
210173412Skevlovoid ripalarm(void);
211173412Skevlovoid riprecv(void);
212173412Skevlovoid ripsend(struct ifc *, struct sockaddr_in6 *, int);
213173412Skevloint out_filter(struct riprt *, struct ifc *);
214173412Skevlovoid init(void);
215173412Skevlovoid sockopt(struct ifc *);
216173412Skevlovoid ifconfig(void);
217243232Shrsint ifconfig1(const char *, const struct sockaddr *, struct ifc *, int);
218173412Skevlovoid rtrecv(void);
219173412Skevloint rt_del(const struct sockaddr_in6 *, const struct sockaddr_in6 *,
220173412Skevlo	const struct sockaddr_in6 *);
221173412Skevloint rt_deladdr(struct ifc *, const struct sockaddr_in6 *,
222173412Skevlo	const struct sockaddr_in6 *);
223173412Skevlovoid filterconfig(void);
224173412Skevloint getifmtu(int);
225173412Skevloconst char *rttypes(struct rt_msghdr *);
226173412Skevloconst char *rtflags(struct rt_msghdr *);
227173412Skevloconst char *ifflags(int);
228173412Skevloint ifrt(struct ifc *, int);
229173412Skevlovoid ifrt_p2p(struct ifc *, int);
230173412Skevlovoid applymask(struct in6_addr *, struct in6_addr *);
231173412Skevlovoid applyplen(struct in6_addr *, int);
232173412Skevlovoid ifrtdump(int);
233173412Skevlovoid ifdump(int);
234173412Skevlovoid ifdump0(FILE *, const struct ifc *);
235243233Shrsvoid ifremove(int);
236173412Skevlovoid rtdump(int);
237173412Skevlovoid rt_entry(struct rt_msghdr *, int);
238173412Skevlovoid rtdexit(void);
239173412Skevlovoid riprequest(struct ifc *, struct netinfo6 *, int,
240173412Skevlo	struct sockaddr_in6 *);
241243232Shrsvoid ripflush(struct ifc *, struct sockaddr_in6 *, int, struct netinfo6 *np);
242173412Skevlovoid sendrequest(struct ifc *);
243173412Skevloint sin6mask2len(const struct sockaddr_in6 *);
244173412Skevloint mask2len(const struct in6_addr *, int);
245173412Skevloint sendpacket(struct sockaddr_in6 *, int);
246173412Skevloint addroute(struct riprt *, const struct in6_addr *, struct ifc *);
247173412Skevloint delroute(struct netinfo6 *, struct in6_addr *);
248173412Skevlostruct in6_addr *getroute(struct netinfo6 *, struct in6_addr *);
249173412Skevlovoid krtread(int);
250173412Skevloint tobeadv(struct riprt *, struct ifc *);
251173412Skevlochar *allocopy(char *);
252173412Skevlochar *hms(void);
253173412Skevloconst char *inet6_n2p(const struct in6_addr *);
254173412Skevlostruct ifac *ifa_match(const struct ifc *, const struct in6_addr *, int);
255173412Skevlostruct in6_addr *plen2mask(int);
256243232Shrsstruct riprt *rtsearch(struct netinfo6 *);
257173412Skevloint ripinterval(int);
258173412Skevlotime_t ripsuptrig(void);
259173412Skevlovoid fatal(const char *, ...)
26066807Skris	__attribute__((__format__(__printf__, 1, 2)));
261173412Skevlovoid trace(int, const char *, ...)
26266807Skris	__attribute__((__format__(__printf__, 2, 3)));
263173412Skevlovoid tracet(int, const char *, ...)
26466807Skris	__attribute__((__format__(__printf__, 2, 3)));
265173412Skevlounsigned int if_maxindex(void);
266173412Skevlostruct ifc *ifc_find(char *);
267173412Skevlostruct iff *iff_find(struct ifc *, int);
268173412Skevlovoid setindex2ifc(int, struct ifc *);
26955163Sshin
27055163Sshin#define	MALLOC(type)	((type *)malloc(sizeof(type)))
27155163Sshin
272243232Shrs#define IFIL_TYPE_ANY	0x0
273243232Shrs#define IFIL_TYPE_A	'A'
274243232Shrs#define IFIL_TYPE_N	'N'
275243232Shrs#define IFIL_TYPE_T	'T'
276243232Shrs#define IFIL_TYPE_O	'O'
277243232Shrs#define IFIL_TYPE_L	'L'
278243232Shrs
27955163Sshinint
280243232Shrsmain(int argc, char *argv[])
28155163Sshin{
28255163Sshin	int	ch;
28355163Sshin	int	error = 0;
284243233Shrs	unsigned long proto;
28555163Sshin	struct	ifc *ifcp;
28655163Sshin	sigset_t mask, omask;
287243233Shrs	const char *pidfile = ROUTE6D_PID;
288243233Shrs	FILE *pidfh;
28955163Sshin	char *progname;
29062607Sitojun	char *ep;
29155163Sshin
29255163Sshin	progname = strrchr(*argv, '/');
29355163Sshin	if (progname)
29455163Sshin		progname++;
29555163Sshin	else
29655163Sshin		progname = *argv;
29755163Sshin
29855163Sshin	pid = getpid();
299243233Shrs	while ((ch = getopt(argc, argv, "A:N:O:R:T:L:t:adDhlnp:P:Q:qsS")) != -1) {
30055163Sshin		switch (ch) {
30155163Sshin		case 'A':
30255163Sshin		case 'N':
30355163Sshin		case 'O':
30455163Sshin		case 'T':
30555163Sshin		case 'L':
30662607Sitojun			if (nfilter >= MAXFILTER) {
30755163Sshin				fatal("Exceeds MAXFILTER");
30862607Sitojun				/*NOTREACHED*/
30962607Sitojun			}
31055163Sshin			filtertype[nfilter] = ch;
31155163Sshin			filter[nfilter++] = allocopy(optarg);
31255163Sshin			break;
31355163Sshin		case 't':
31462607Sitojun			ep = NULL;
31562607Sitojun			routetag = strtoul(optarg, &ep, 0);
31662607Sitojun			if (!ep || *ep != '\0' || (routetag & ~0xffff) != 0) {
31755163Sshin				fatal("invalid route tag");
31855163Sshin				/*NOTREACHED*/
31955163Sshin			}
32055163Sshin			break;
321243233Shrs		case 'p':
322243233Shrs			pidfile = optarg;
323243233Shrs			break;
324243233Shrs		case 'P':
325243233Shrs			ep = NULL;
326243233Shrs			proto = strtoul(optarg, &ep, 0);
327243233Shrs			if (!ep || *ep != '\0' || 3 < proto) {
328243233Shrs				fatal("invalid P flag");
329243233Shrs				/*NOTREACHED*/
330243233Shrs			}
331243233Shrs			if (proto == 0)
332243233Shrs				Pflag = 0;
333243233Shrs			if (proto == 1)
334243233Shrs				Pflag |= RTF_PROTO1;
335243233Shrs			if (proto == 2)
336243233Shrs				Pflag |= RTF_PROTO2;
337243233Shrs			if (proto == 3)
338243233Shrs				Pflag |= RTF_PROTO3;
339243233Shrs			break;
340243233Shrs		case 'Q':
341243233Shrs			ep = NULL;
342243233Shrs			proto = strtoul(optarg, &ep, 0);
343243233Shrs			if (!ep || *ep != '\0' || 3 < proto) {
344243233Shrs				fatal("invalid Q flag");
345243233Shrs				/*NOTREACHED*/
346243233Shrs			}
347243233Shrs			if (proto == 0)
348243233Shrs				Qflag = 0;
349243233Shrs			if (proto == 1)
350243233Shrs				Qflag |= RTF_PROTO1;
351243233Shrs			if (proto == 2)
352243233Shrs				Qflag |= RTF_PROTO2;
353243233Shrs			if (proto == 3)
354243233Shrs				Qflag |= RTF_PROTO3;
355243233Shrs			break;
35655163Sshin		case 'R':
35762607Sitojun			if ((rtlog = fopen(optarg, "w")) == NULL) {
35855163Sshin				fatal("Can not write to routelog");
35962607Sitojun				/*NOTREACHED*/
36062607Sitojun			}
36155163Sshin			break;
36262607Sitojun#define	FLAG(c, flag, n)	case c: do { flag = n; break; } while(0)
36362607Sitojun		FLAG('a', aflag, 1); break;
36462607Sitojun		FLAG('d', dflag, 1); break;
36562607Sitojun		FLAG('D', dflag, 2); break;
36662607Sitojun		FLAG('h', hflag, 1); break;
36762607Sitojun		FLAG('l', lflag, 1); break;
36862607Sitojun		FLAG('n', nflag, 1); break;
36962607Sitojun		FLAG('q', qflag, 1); break;
37062607Sitojun		FLAG('s', sflag, 1); break;
37162607Sitojun		FLAG('S', Sflag, 1); break;
37255163Sshin#undef	FLAG
37355163Sshin		default:
37455163Sshin			fatal("Invalid option specified, terminating");
37562607Sitojun			/*NOTREACHED*/
37655163Sshin		}
37755163Sshin	}
37855163Sshin	argc -= optind;
37955163Sshin	argv += optind;
38078064Sume	if (argc > 0) {
38155163Sshin		fatal("bogus extra arguments");
38278064Sume		/*NOTREACHED*/
38378064Sume	}
38455163Sshin
38555163Sshin	if (geteuid()) {
38655163Sshin		nflag = 1;
38755163Sshin		fprintf(stderr, "No kernel update is allowed\n");
38855163Sshin	}
389119037Sume
390119037Sume	if (dflag == 0) {
391119037Sume		if (daemon(0, 0) < 0) {
392119037Sume			fatal("daemon");
393119037Sume			/*NOTREACHED*/
394119037Sume		}
395119037Sume	}
396119037Sume
39755163Sshin	openlog(progname, LOG_NDELAY|LOG_PID, LOG_DAEMON);
39855163Sshin	logopened++;
39978064Sume
40078064Sume	if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL)
40178064Sume		fatal("malloc");
40278064Sume	memset(ripbuf, 0, RIP6_MAXMTU);
40378064Sume	ripbuf->rip6_cmd = RIP6_RESPONSE;
40478064Sume	ripbuf->rip6_vers = RIP6_VERSION;
40578064Sume	ripbuf->rip6_res1[0] = 0;
40678064Sume	ripbuf->rip6_res1[1] = 0;
40778064Sume
40855163Sshin	init();
40955163Sshin	ifconfig();
410243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
41155163Sshin		if (ifcp->ifc_index < 0) {
412243232Shrs			fprintf(stderr, "No ifindex found at %s "
413243232Shrs			    "(no link-local address?)\n", ifcp->ifc_name);
41455163Sshin			error++;
41555163Sshin		}
41655163Sshin	}
41755163Sshin	if (error)
41855163Sshin		exit(1);
41978064Sume	if (loopifcp == NULL) {
42055163Sshin		fatal("No loopback found");
42178064Sume		/*NOTREACHED*/
42278064Sume	}
423243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
42455163Sshin		ifrt(ifcp, 0);
425243232Shrs	}
42655163Sshin	filterconfig();
42755163Sshin	krtread(0);
42855163Sshin	if (dflag)
42955163Sshin		ifrtdump(0);
43055163Sshin
43155163Sshin	pid = getpid();
432243233Shrs	if ((pidfh = fopen(pidfile, "w")) != NULL) {
433243233Shrs		fprintf(pidfh, "%d\n", pid);
434243233Shrs		fclose(pidfh);
43555163Sshin	}
43655163Sshin
43778064Sume	if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL) {
43855163Sshin		fatal("malloc");
43978064Sume		/*NOTREACHED*/
44078064Sume	}
44162607Sitojun	memset(ripbuf, 0, RIP6_MAXMTU);
44255163Sshin	ripbuf->rip6_cmd = RIP6_RESPONSE;
44355163Sshin	ripbuf->rip6_vers = RIP6_VERSION;
44455163Sshin	ripbuf->rip6_res1[0] = 0;
44555163Sshin	ripbuf->rip6_res1[1] = 0;
44655163Sshin
44778064Sume	if (signal(SIGALRM, sighandler) == SIG_ERR ||
44878064Sume	    signal(SIGQUIT, sighandler) == SIG_ERR ||
44978064Sume	    signal(SIGTERM, sighandler) == SIG_ERR ||
45078064Sume	    signal(SIGUSR1, sighandler) == SIG_ERR ||
45178064Sume	    signal(SIGHUP, sighandler) == SIG_ERR ||
45278064Sume	    signal(SIGINT, sighandler) == SIG_ERR) {
45378064Sume		fatal("signal");
45478064Sume		/*NOTREACHED*/
45578064Sume	}
45655163Sshin	/*
45755163Sshin	 * To avoid rip packet congestion (not on a cable but in this
45855163Sshin	 * process), wait for a moment to send the first RIP6_RESPONSE
45955163Sshin	 * packets.
46055163Sshin	 */
46155163Sshin	alarm(ripinterval(INIT_INTERVAL6));
46255163Sshin
463243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
464243232Shrs		if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
465119041Sume			continue;
46655163Sshin		if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
46755163Sshin			sendrequest(ifcp);
46855163Sshin	}
46955163Sshin
47055163Sshin	syslog(LOG_INFO, "**** Started ****");
47155163Sshin	sigemptyset(&mask);
47255163Sshin	sigaddset(&mask, SIGALRM);
47355163Sshin	while (1) {
47478064Sume		if (seenalrm) {
47578064Sume			ripalarm();
47678064Sume			seenalrm = 0;
47778064Sume			continue;
47878064Sume		}
47978064Sume		if (seenquit) {
48078064Sume			rtdexit();
48178064Sume			seenquit = 0;
48278064Sume			continue;
48378064Sume		}
48478064Sume		if (seenusr1) {
48578064Sume			ifrtdump(SIGUSR1);
48678064Sume			seenusr1 = 0;
48778064Sume			continue;
48878064Sume		}
48978064Sume
490119076Sume#ifdef HAVE_POLL_H
491119076Sume		switch (poll(set, 2, INFTIM))
492119076Sume#else
493119070Sume		memcpy(recvecp, sockvecp, fdmasks);
494119076Sume		switch (select(maxfd + 1, recvecp, 0, 0, 0))
495119076Sume#endif
496119076Sume		{
49755163Sshin		case -1:
49878064Sume			if (errno != EINTR) {
49978064Sume				fatal("select");
50078064Sume				/*NOTREACHED*/
50178064Sume			}
50278064Sume			continue;
50355163Sshin		case 0:
50455163Sshin			continue;
50555163Sshin		default:
506119076Sume#ifdef HAVE_POLL_H
507119076Sume			if (set[0].revents & POLLIN)
508119076Sume#else
509119076Sume			if (FD_ISSET(ripsock, recvecp))
510119076Sume#endif
511119076Sume			{
51255163Sshin				sigprocmask(SIG_BLOCK, &mask, &omask);
51355163Sshin				riprecv();
51455163Sshin				sigprocmask(SIG_SETMASK, &omask, NULL);
51555163Sshin			}
516119076Sume#ifdef HAVE_POLL_H
517119076Sume			if (set[1].revents & POLLIN)
518119076Sume#else
519119076Sume			if (FD_ISSET(rtsock, recvecp))
520119076Sume#endif
521119076Sume			{
52255163Sshin				sigprocmask(SIG_BLOCK, &mask, &omask);
52355163Sshin				rtrecv();
52455163Sshin				sigprocmask(SIG_SETMASK, &omask, NULL);
52555163Sshin			}
52655163Sshin		}
52755163Sshin	}
52855163Sshin}
52955163Sshin
53078064Sumevoid
531243232Shrssighandler(int signo)
53278064Sume{
53378064Sume
53478064Sume	switch (signo) {
53578064Sume	case SIGALRM:
53678064Sume		seenalrm++;
53778064Sume		break;
53878064Sume	case SIGQUIT:
53978064Sume	case SIGTERM:
54078064Sume		seenquit++;
54178064Sume		break;
54278064Sume	case SIGUSR1:
54378064Sume	case SIGHUP:
54478064Sume	case SIGINT:
54578064Sume		seenusr1++;
54678064Sume		break;
54778064Sume	}
54878064Sume}
54978064Sume
55055163Sshin/*
55155163Sshin * gracefully exits after resetting sockopts.
55255163Sshin */
55355163Sshin/* ARGSUSED */
55455163Sshinvoid
555243232Shrsrtdexit(void)
55655163Sshin{
55755163Sshin	struct	riprt *rrt;
55855163Sshin
55955163Sshin	alarm(0);
560243232Shrs	TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
56162607Sitojun		if (rrt->rrt_rflags & RRTF_AGGREGATE) {
56255163Sshin			delroute(&rrt->rrt_info, &rrt->rrt_gw);
56355163Sshin		}
56455163Sshin	}
56555163Sshin	close(ripsock);
56655163Sshin	close(rtsock);
56755163Sshin	syslog(LOG_INFO, "**** Terminated ****");
56855163Sshin	closelog();
56955163Sshin	exit(1);
57055163Sshin}
57155163Sshin
57255163Sshin/*
57355163Sshin * Called periodically:
57455163Sshin *	1. age out the learned route. remove it if necessary.
57555163Sshin *	2. submit RIP6_RESPONSE packets.
57678064Sume * Invoked in every SUPPLY_INTERVAL6 (30) seconds.  I believe we don't have
57755163Sshin * to invoke this function in every 1 or 5 or 10 seconds only to age the
57855163Sshin * routes more precisely.
57955163Sshin */
58055163Sshin/* ARGSUSED */
58155163Sshinvoid
582243232Shrsripalarm(void)
58355163Sshin{
58455163Sshin	struct	ifc *ifcp;
585243232Shrs	struct	riprt *rrt, *rrt_tmp;
58655163Sshin	time_t	t_lifetime, t_holddown;
58755163Sshin
58855163Sshin	/* age the RIP routes */
58955163Sshin	t_lifetime = time(NULL) - RIP_LIFETIME;
59055163Sshin	t_holddown = t_lifetime - RIP_HOLDDOWN;
591243232Shrs	TAILQ_FOREACH_SAFE(rrt, &riprt_head, rrt_next, rrt_tmp) {
592243232Shrs		if (rrt->rrt_t == 0)
59355163Sshin			continue;
594243232Shrs		else if (rrt->rrt_t < t_holddown) {
595243232Shrs			TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
59655163Sshin			delroute(&rrt->rrt_info, &rrt->rrt_gw);
59755163Sshin			free(rrt);
598243232Shrs		} else if (rrt->rrt_t < t_lifetime)
59955163Sshin			rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
60055163Sshin	}
60155163Sshin	/* Supply updates */
602243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
60355163Sshin		if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
60455163Sshin			ripsend(ifcp, &ifcp->ifc_ripsin, 0);
60555163Sshin	}
60655163Sshin	alarm(ripinterval(SUPPLY_INTERVAL6));
60755163Sshin}
60855163Sshin
60955163Sshinvoid
610243232Shrsinit(void)
61155163Sshin{
612119034Sume	int	error;
613119034Sume	const int int0 = 0, int1 = 1, int255 = 255;
61455163Sshin	struct	addrinfo hints, *res;
615119081Sume	char	port[NI_MAXSERV];
61655163Sshin
617243232Shrs	TAILQ_INIT(&ifc_head);
61855163Sshin	nindex2ifc = 0;	/*initial guess*/
61955163Sshin	index2ifc = NULL;
620119081Sume	snprintf(port, sizeof(port), "%u", RIP6_PORT);
62155163Sshin
62255163Sshin	memset(&hints, 0, sizeof(hints));
62355163Sshin	hints.ai_family = PF_INET6;
62455163Sshin	hints.ai_socktype = SOCK_DGRAM;
625119080Sume	hints.ai_protocol = IPPROTO_UDP;
62655163Sshin	hints.ai_flags = AI_PASSIVE;
62755163Sshin	error = getaddrinfo(NULL, port, &hints, &res);
62878064Sume	if (error) {
62966807Skris		fatal("%s", gai_strerror(error));
63078064Sume		/*NOTREACHED*/
63178064Sume	}
63278064Sume	if (res->ai_next) {
63355163Sshin		fatal(":: resolved to multiple address");
63478064Sume		/*NOTREACHED*/
63578064Sume	}
63655163Sshin
63755163Sshin	ripsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
63878064Sume	if (ripsock < 0) {
63955163Sshin		fatal("rip socket");
64078064Sume		/*NOTREACHED*/
64178064Sume	}
642119034Sume#ifdef IPV6_V6ONLY
643119034Sume	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_V6ONLY,
644119034Sume	    &int1, sizeof(int1)) < 0) {
645119034Sume		fatal("rip IPV6_V6ONLY");
646119034Sume		/*NOTREACHED*/
647119034Sume	}
648119034Sume#endif
64978064Sume	if (bind(ripsock, res->ai_addr, res->ai_addrlen) < 0) {
65055163Sshin		fatal("rip bind");
65178064Sume		/*NOTREACHED*/
65278064Sume	}
65355163Sshin	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
65478064Sume	    &int255, sizeof(int255)) < 0) {
65555163Sshin		fatal("rip IPV6_MULTICAST_HOPS");
65678064Sume		/*NOTREACHED*/
65778064Sume	}
65855163Sshin	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
65978064Sume	    &int0, sizeof(int0)) < 0) {
66055163Sshin		fatal("rip IPV6_MULTICAST_LOOP");
66178064Sume		/*NOTREACHED*/
66278064Sume	}
66362921Sume
66462607Sitojun#ifdef IPV6_RECVPKTINFO
665119034Sume	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVPKTINFO,
666119034Sume	    &int1, sizeof(int1)) < 0) {
66762607Sitojun		fatal("rip IPV6_RECVPKTINFO");
66878064Sume		/*NOTREACHED*/
66978064Sume	}
67062607Sitojun#else  /* old adv. API */
671119034Sume	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_PKTINFO,
672119034Sume	    &int1, sizeof(int1)) < 0) {
67355163Sshin		fatal("rip IPV6_PKTINFO");
67478064Sume		/*NOTREACHED*/
67578064Sume	}
67662607Sitojun#endif
67755163Sshin
678164339Ssuz#ifdef IPV6_RECVPKTINFO
679164339Ssuz	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
680164339Ssuz	    &int1, sizeof(int1)) < 0) {
681164339Ssuz		fatal("rip IPV6_RECVHOPLIMIT");
682164339Ssuz		/*NOTREACHED*/
683164339Ssuz	}
684164339Ssuz#else  /* old adv. API */
685164339Ssuz	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_HOPLIMIT,
686164339Ssuz	    &int1, sizeof(int1)) < 0) {
687164339Ssuz		fatal("rip IPV6_HOPLIMIT");
688164339Ssuz		/*NOTREACHED*/
689164339Ssuz	}
690164339Ssuz#endif
691164339Ssuz
69255163Sshin	memset(&hints, 0, sizeof(hints));
69355163Sshin	hints.ai_family = PF_INET6;
69455163Sshin	hints.ai_socktype = SOCK_DGRAM;
695119080Sume	hints.ai_protocol = IPPROTO_UDP;
69655163Sshin	error = getaddrinfo(RIP6_DEST, port, &hints, &res);
69778064Sume	if (error) {
69866807Skris		fatal("%s", gai_strerror(error));
69978064Sume		/*NOTREACHED*/
70078064Sume	}
70178064Sume	if (res->ai_next) {
70255163Sshin		fatal("%s resolved to multiple address", RIP6_DEST);
70378064Sume		/*NOTREACHED*/
70478064Sume	}
70555163Sshin	memcpy(&ripsin, res->ai_addr, res->ai_addrlen);
70655163Sshin
707119076Sume#ifdef HAVE_POLL_H
708119076Sume	set[0].fd = ripsock;
709119076Sume	set[0].events = POLLIN;
710119076Sume#else
711119070Sume	maxfd = ripsock;
712119076Sume#endif
71355163Sshin
71455163Sshin	if (nflag == 0) {
71578064Sume		if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
71655163Sshin			fatal("route socket");
71778064Sume			/*NOTREACHED*/
71878064Sume		}
719119076Sume#ifdef HAVE_POLL_H
720119076Sume		set[1].fd = rtsock;
721119076Sume		set[1].events = POLLIN;
722119076Sume#else
723119070Sume		if (rtsock > maxfd)
724119070Sume			maxfd = rtsock;
725119076Sume#endif
726119076Sume	} else {
727119076Sume#ifdef HAVE_POLL_H
728119076Sume		set[1].fd = -1;
729119076Sume#else
73055163Sshin		rtsock = -1;	/*just for safety */
731119076Sume#endif
732119076Sume	}
733119070Sume
734119076Sume#ifndef HAVE_POLL_H
735119070Sume	fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
736119070Sume	if ((sockvecp = malloc(fdmasks)) == NULL) {
737119070Sume		fatal("malloc");
738119070Sume		/*NOTREACHED*/
739119070Sume	}
740119070Sume	if ((recvecp = malloc(fdmasks)) == NULL) {
741119070Sume		fatal("malloc");
742119070Sume		/*NOTREACHED*/
743119070Sume	}
744119070Sume	memset(sockvecp, 0, fdmasks);
745119070Sume	FD_SET(ripsock, sockvecp);
746119070Sume	if (rtsock >= 0)
747119070Sume		FD_SET(rtsock, sockvecp);
748119076Sume#endif
74955163Sshin}
75055163Sshin
75162607Sitojun#define	RIPSIZE(n) \
75262607Sitojun	(sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6))
75355163Sshin
75455163Sshin/*
75555163Sshin * ripflush flushes the rip datagram stored in the rip buffer
75655163Sshin */
75755163Sshinvoid
758243232Shrsripflush(struct ifc *ifcp, struct sockaddr_in6 *sin6, int nrt, struct netinfo6 *np)
75955163Sshin{
76055163Sshin	int i;
76155163Sshin	int error;
76255163Sshin
76355163Sshin	if (ifcp)
76455163Sshin		tracet(1, "Send(%s): info(%d) to %s.%d\n",
76555163Sshin			ifcp->ifc_name, nrt,
766119031Sume			inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
76755163Sshin	else
76855163Sshin		tracet(1, "Send: info(%d) to %s.%d\n",
769119031Sume			nrt, inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
77055163Sshin	if (dflag >= 2) {
77155163Sshin		np = ripbuf->rip6_nets;
77255163Sshin		for (i = 0; i < nrt; i++, np++) {
77355163Sshin			if (np->rip6_metric == NEXTHOP_METRIC) {
77455163Sshin				if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest))
77562607Sitojun					trace(2, "    NextHop reset");
77655163Sshin				else {
77755163Sshin					trace(2, "    NextHop %s",
77855163Sshin						inet6_n2p(&np->rip6_dest));
77955163Sshin				}
78055163Sshin			} else {
78155163Sshin				trace(2, "    %s/%d[%d]",
78255163Sshin					inet6_n2p(&np->rip6_dest),
78355163Sshin					np->rip6_plen, np->rip6_metric);
78455163Sshin			}
78555163Sshin			if (np->rip6_tag) {
78655163Sshin				trace(2, "  tag=0x%04x",
78755163Sshin					ntohs(np->rip6_tag) & 0xffff);
78855163Sshin			}
78955163Sshin			trace(2, "\n");
79055163Sshin		}
79155163Sshin	}
792119031Sume	error = sendpacket(sin6, RIPSIZE(nrt));
79355163Sshin	if (error == EAFNOSUPPORT) {
79455163Sshin		/* Protocol not supported */
79555163Sshin		tracet(1, "Could not send info to %s (%s): "
79655163Sshin			"set IFF_UP to 0\n",
79755163Sshin			ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
79855163Sshin		ifcp->ifc_flags &= ~IFF_UP;	/* As if down for AF_INET6 */
79955163Sshin	}
80055163Sshin}
80155163Sshin
80255163Sshin/*
80355163Sshin * Generate RIP6_RESPONSE packets and send them.
80455163Sshin */
80555163Sshinvoid
806243232Shrsripsend(struct	ifc *ifcp, struct sockaddr_in6 *sin6, int flag)
80755163Sshin{
80855163Sshin	struct	riprt *rrt;
80955163Sshin	struct	in6_addr *nh;	/* next hop */
810243232Shrs	struct netinfo6 *np;
81178064Sume	int	maxrte;
812243232Shrs	int nrt;
81355163Sshin
814119084Sume	if (qflag)
815119084Sume		return;
816119084Sume
81755163Sshin	if (ifcp == NULL) {
81855163Sshin		/*
81955163Sshin		 * Request from non-link local address is not
82055163Sshin		 * a regular route6d update.
82155163Sshin		 */
82262607Sitojun		maxrte = (IFMINMTU - sizeof(struct ip6_hdr) -
82362607Sitojun				sizeof(struct udphdr) -
82455163Sshin				sizeof(struct rip6) + sizeof(struct netinfo6)) /
82555163Sshin				sizeof(struct netinfo6);
826243232Shrs		nh = NULL;
827243232Shrs		nrt = 0;
828243232Shrs		np = ripbuf->rip6_nets;
829243232Shrs		TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
83062607Sitojun			if (rrt->rrt_rflags & RRTF_NOADVERTISE)
83155163Sshin				continue;
83255163Sshin			/* Put the route to the buffer */
83355163Sshin			*np = rrt->rrt_info;
83455163Sshin			np++; nrt++;
83555163Sshin			if (nrt == maxrte) {
836243232Shrs				ripflush(NULL, sin6, nrt, np);
83755163Sshin				nh = NULL;
838243232Shrs				nrt = 0;
839243232Shrs				np = ripbuf->rip6_nets;
84055163Sshin			}
84155163Sshin		}
84255163Sshin		if (nrt)	/* Send last packet */
843243232Shrs			ripflush(NULL, sin6, nrt, np);
84455163Sshin		return;
84555163Sshin	}
84655163Sshin
84762607Sitojun	if ((flag & RRTF_SENDANYWAY) == 0 &&
84855163Sshin	    (qflag || (ifcp->ifc_flags & IFF_LOOPBACK)))
84955163Sshin		return;
85078064Sume
85178064Sume	/* -N: no use */
852243232Shrs	if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
85355163Sshin		return;
85478064Sume
85578064Sume	/* -T: generate default route only */
856243232Shrs	if (iff_find(ifcp, IFIL_TYPE_T) != NULL) {
85755163Sshin		struct netinfo6 rrt_info;
85855163Sshin		memset(&rrt_info, 0, sizeof(struct netinfo6));
85955163Sshin		rrt_info.rip6_dest = in6addr_any;
86055163Sshin		rrt_info.rip6_plen = 0;
86155163Sshin		rrt_info.rip6_metric = 1;
86278064Sume		rrt_info.rip6_metric += ifcp->ifc_metric;
86355163Sshin		rrt_info.rip6_tag = htons(routetag & 0xffff);
86455163Sshin		np = ripbuf->rip6_nets;
86555163Sshin		*np = rrt_info;
86655163Sshin		nrt = 1;
867243232Shrs		ripflush(ifcp, sin6, nrt, np);
86855163Sshin		return;
86955163Sshin	}
87078064Sume
87162607Sitojun	maxrte = (ifcp->ifc_mtu - sizeof(struct ip6_hdr) -
87262607Sitojun			sizeof(struct udphdr) -
87355163Sshin			sizeof(struct rip6) + sizeof(struct netinfo6)) /
87455163Sshin			sizeof(struct netinfo6);
87578064Sume
87655163Sshin	nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
877243232Shrs	TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
87862607Sitojun		if (rrt->rrt_rflags & RRTF_NOADVERTISE)
87955163Sshin			continue;
88078064Sume
88178064Sume		/* Need to check filter here */
88278064Sume		if (out_filter(rrt, ifcp) == 0)
88355163Sshin			continue;
88478064Sume
88555163Sshin		/* Check split horizon and other conditions */
88655163Sshin		if (tobeadv(rrt, ifcp) == 0)
88755163Sshin			continue;
88878064Sume
88955163Sshin		/* Only considers the routes with flag if specified */
89062607Sitojun		if ((flag & RRTF_CHANGED) &&
89162607Sitojun		    (rrt->rrt_rflags & RRTF_CHANGED) == 0)
89255163Sshin			continue;
89378064Sume
89455163Sshin		/* Check nexthop */
89555163Sshin		if (rrt->rrt_index == ifcp->ifc_index &&
89655163Sshin		    !IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_gw) &&
89762607Sitojun		    (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR) == 0) {
89855163Sshin			if (nh == NULL || !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw)) {
899243232Shrs				if (nrt == maxrte - 2) {
900243232Shrs					ripflush(ifcp, sin6, nrt, np);
901243232Shrs					nh = NULL;
902243232Shrs					nrt = 0;
903243232Shrs					np = ripbuf->rip6_nets;
904243232Shrs				}
905243232Shrs
90655163Sshin				np->rip6_dest = rrt->rrt_gw;
90755163Sshin				np->rip6_plen = 0;
90855163Sshin				np->rip6_tag = 0;
90955163Sshin				np->rip6_metric = NEXTHOP_METRIC;
91055163Sshin				nh = &rrt->rrt_gw;
91155163Sshin				np++; nrt++;
91255163Sshin			}
91355163Sshin		} else if (nh && (rrt->rrt_index != ifcp->ifc_index ||
91455163Sshin			          !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw) ||
91562607Sitojun				  rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)) {
91655163Sshin			/* Reset nexthop */
917243232Shrs			if (nrt == maxrte - 2) {
918243232Shrs				ripflush(ifcp, sin6, nrt, np);
919243232Shrs				nh = NULL;
920243232Shrs				nrt = 0;
921243232Shrs				np = ripbuf->rip6_nets;
922243232Shrs			}
92355163Sshin			memset(np, 0, sizeof(struct netinfo6));
92455163Sshin			np->rip6_metric = NEXTHOP_METRIC;
92555163Sshin			nh = NULL;
92655163Sshin			np++; nrt++;
92755163Sshin		}
92878064Sume
92955163Sshin		/* Put the route to the buffer */
93055163Sshin		*np = rrt->rrt_info;
93155163Sshin		np++; nrt++;
93255163Sshin		if (nrt == maxrte) {
933243232Shrs			ripflush(ifcp, sin6, nrt, np);
93455163Sshin			nh = NULL;
935243232Shrs			nrt = 0;
936243232Shrs			np = ripbuf->rip6_nets;
93755163Sshin		}
93855163Sshin	}
93955163Sshin	if (nrt)	/* Send last packet */
940243232Shrs		ripflush(ifcp, sin6, nrt, np);
94155163Sshin}
94255163Sshin
94355163Sshin/*
94478064Sume * outbound filter logic, per-route/interface.
94578064Sume */
94678064Sumeint
947243232Shrsout_filter(struct riprt *rrt, struct ifc *ifcp)
94878064Sume{
94978064Sume	struct iff *iffp;
95078064Sume	struct in6_addr ia;
95178064Sume	int ok;
95278064Sume
95378064Sume	/*
95478064Sume	 * -A: filter out less specific routes, if we have aggregated
95578064Sume	 * route configured.
95678064Sume	 */
957243232Shrs	TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
95878064Sume		if (iffp->iff_type != 'A')
95978064Sume			continue;
96078064Sume		if (rrt->rrt_info.rip6_plen <= iffp->iff_plen)
96178064Sume			continue;
96278064Sume		ia = rrt->rrt_info.rip6_dest;
96378064Sume		applyplen(&ia, iffp->iff_plen);
96478064Sume		if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr))
96578064Sume			return 0;
96678064Sume	}
96778064Sume
96878064Sume	/*
96978064Sume	 * if it is an aggregated route, advertise it only to the
97078064Sume	 * interfaces specified on -A.
97178064Sume	 */
97278064Sume	if ((rrt->rrt_rflags & RRTF_AGGREGATE) != 0) {
97378064Sume		ok = 0;
974243232Shrs		TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
97578064Sume			if (iffp->iff_type != 'A')
97678064Sume				continue;
97778064Sume			if (rrt->rrt_info.rip6_plen == iffp->iff_plen &&
97878064Sume			    IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
97978064Sume			    &iffp->iff_addr)) {
98078064Sume				ok = 1;
98178064Sume				break;
98278064Sume			}
98378064Sume		}
98478064Sume		if (!ok)
98578064Sume			return 0;
98678064Sume	}
98778064Sume
98878064Sume	/*
98978064Sume	 * -O: advertise only if prefix matches the configured prefix.
99078064Sume	 */
991243232Shrs	if (iff_find(ifcp, IFIL_TYPE_O) != NULL) {
99278064Sume		ok = 0;
993243232Shrs		TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
99478064Sume			if (iffp->iff_type != 'O')
99578064Sume				continue;
99678064Sume			if (rrt->rrt_info.rip6_plen < iffp->iff_plen)
99778064Sume				continue;
99878064Sume			ia = rrt->rrt_info.rip6_dest;
99978064Sume			applyplen(&ia, iffp->iff_plen);
100078064Sume			if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
100178064Sume				ok = 1;
100278064Sume				break;
100378064Sume			}
100478064Sume		}
100578064Sume		if (!ok)
100678064Sume			return 0;
100778064Sume	}
100878064Sume
100978064Sume	/* the prefix should be advertised */
101078064Sume	return 1;
101178064Sume}
101278064Sume
101378064Sume/*
101455163Sshin * Determine if the route is to be advertised on the specified interface.
101555163Sshin * It checks options specified in the arguments and the split horizon rule.
101655163Sshin */
101755163Sshinint
1018243232Shrstobeadv(struct riprt *rrt, struct ifc *ifcp)
101955163Sshin{
102055163Sshin
102155163Sshin	/* Special care for static routes */
102255163Sshin	if (rrt->rrt_flags & RTF_STATIC) {
102362607Sitojun		/* XXX don't advertise reject/blackhole routes */
102462607Sitojun		if (rrt->rrt_flags & (RTF_REJECT | RTF_BLACKHOLE))
102562607Sitojun			return 0;
102662607Sitojun
102755163Sshin		if (Sflag)	/* Yes, advertise it anyway */
102855163Sshin			return 1;
102955163Sshin		if (sflag && rrt->rrt_index != ifcp->ifc_index)
103055163Sshin			return 1;
103155163Sshin		return 0;
103255163Sshin	}
103355163Sshin	/* Regular split horizon */
103455163Sshin	if (hflag == 0 && rrt->rrt_index == ifcp->ifc_index)
103555163Sshin		return 0;
103655163Sshin	return 1;
103755163Sshin}
103855163Sshin
103955163Sshin/*
104055163Sshin * Send a rip packet actually.
104155163Sshin */
104255163Sshinint
1043243232Shrssendpacket(struct sockaddr_in6 *sin6, int len)
104455163Sshin{
104555163Sshin	struct msghdr m;
104655163Sshin	struct cmsghdr *cm;
104755163Sshin	struct iovec iov[2];
104855163Sshin	u_char cmsgbuf[256];
104955163Sshin	struct in6_pktinfo *pi;
105078064Sume	int idx;
105155163Sshin	struct sockaddr_in6 sincopy;
105255163Sshin
105355163Sshin	/* do not overwrite the given sin */
1054119031Sume	sincopy = *sin6;
1055119031Sume	sin6 = &sincopy;
105655163Sshin
1057119035Sume	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
1058243231Shrs	    IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1059243231Shrs		idx = sin6->sin6_scope_id;
1060243231Shrs	else
106178064Sume		idx = 0;
106255163Sshin
1063119031Sume	m.msg_name = (caddr_t)sin6;
1064119031Sume	m.msg_namelen = sizeof(*sin6);
106555163Sshin	iov[0].iov_base = (caddr_t)ripbuf;
106655163Sshin	iov[0].iov_len = len;
106755163Sshin	m.msg_iov = iov;
106855163Sshin	m.msg_iovlen = 1;
106978064Sume	if (!idx) {
107055163Sshin		m.msg_control = NULL;
107155163Sshin		m.msg_controllen = 0;
107255163Sshin	} else {
107355163Sshin		memset(cmsgbuf, 0, sizeof(cmsgbuf));
107455163Sshin		cm = (struct cmsghdr *)cmsgbuf;
107555163Sshin		m.msg_control = (caddr_t)cm;
107655163Sshin		m.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
107755163Sshin
107855163Sshin		cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
107955163Sshin		cm->cmsg_level = IPPROTO_IPV6;
108055163Sshin		cm->cmsg_type = IPV6_PKTINFO;
108155163Sshin		pi = (struct in6_pktinfo *)CMSG_DATA(cm);
108255163Sshin		memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*::*/
108378064Sume		pi->ipi6_ifindex = idx;
108455163Sshin	}
108555163Sshin
108655163Sshin	if (sendmsg(ripsock, &m, 0 /*MSG_DONTROUTE*/) < 0) {
108755163Sshin		trace(1, "sendmsg: %s\n", strerror(errno));
108855163Sshin		return errno;
108955163Sshin	}
109062921Sume
109155163Sshin	return 0;
109255163Sshin}
109355163Sshin
109455163Sshin/*
109578064Sume * Receive and process RIP packets.  Update the routes/kernel forwarding
109655163Sshin * table if necessary.
109755163Sshin */
109855163Sshinvoid
1099243232Shrsriprecv(void)
110055163Sshin{
110155163Sshin	struct	ifc *ifcp, *ic;
110255163Sshin	struct	sockaddr_in6 fsock;
110355163Sshin	struct	in6_addr nh;	/* next hop */
110455163Sshin	struct	rip6 *rp;
110555163Sshin	struct	netinfo6 *np, *nq;
110655163Sshin	struct	riprt *rrt;
1107122677Sume	ssize_t	len, nn;
1108122677Sume	unsigned int need_trigger, idx;
110955163Sshin	char	buf[4 * RIP6_MAXMTU];
111055163Sshin	time_t	t;
111155163Sshin	struct msghdr m;
111255163Sshin	struct cmsghdr *cm;
111355163Sshin	struct iovec iov[2];
111455163Sshin	u_char cmsgbuf[256];
1115164339Ssuz	struct in6_pktinfo *pi = NULL;
1116164339Ssuz	int *hlimp = NULL;
111755163Sshin	struct iff *iffp;
111855163Sshin	struct in6_addr ia;
111955163Sshin	int ok;
112078064Sume	time_t t_half_lifetime;
112155163Sshin
112255163Sshin	need_trigger = 0;
112362921Sume
112455163Sshin	m.msg_name = (caddr_t)&fsock;
112555163Sshin	m.msg_namelen = sizeof(fsock);
112655163Sshin	iov[0].iov_base = (caddr_t)buf;
112755163Sshin	iov[0].iov_len = sizeof(buf);
112855163Sshin	m.msg_iov = iov;
112955163Sshin	m.msg_iovlen = 1;
113055163Sshin	cm = (struct cmsghdr *)cmsgbuf;
113155163Sshin	m.msg_control = (caddr_t)cm;
113255163Sshin	m.msg_controllen = sizeof(cmsgbuf);
113378064Sume	if ((len = recvmsg(ripsock, &m, 0)) < 0) {
113455163Sshin		fatal("recvmsg");
113578064Sume		/*NOTREACHED*/
113678064Sume	}
113778064Sume	idx = 0;
113855163Sshin	for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&m);
113955163Sshin	     cm;
114055163Sshin	     cm = (struct cmsghdr *)CMSG_NXTHDR(&m, cm)) {
1141164339Ssuz		if (cm->cmsg_level != IPPROTO_IPV6)
1142164339Ssuz		    continue;
1143164339Ssuz		switch (cm->cmsg_type) {
1144164339Ssuz		case IPV6_PKTINFO:
1145164339Ssuz			if (cm->cmsg_len != CMSG_LEN(sizeof(*pi))) {
1146164339Ssuz				trace(1,
1147164339Ssuz				    "invalid cmsg length for IPV6_PKTINFO\n");
1148164339Ssuz				return;
1149164339Ssuz			}
115055163Sshin			pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
115178064Sume			idx = pi->ipi6_ifindex;
115255163Sshin			break;
1153164339Ssuz		case IPV6_HOPLIMIT:
1154164339Ssuz			if (cm->cmsg_len != CMSG_LEN(sizeof(int))) {
1155164339Ssuz				trace(1,
1156164339Ssuz				    "invalid cmsg length for IPV6_HOPLIMIT\n");
1157164339Ssuz				return;
1158164339Ssuz			}
1159164339Ssuz			hlimp = (int *)CMSG_DATA(cm);
1160164339Ssuz			break;
116155163Sshin		}
116255163Sshin	}
116355163Sshin
1164243232Shrs	if ((size_t)len < sizeof(struct rip6)) {
1165121779Ssuz		trace(1, "Packet too short\n");
1166121779Ssuz		return;
1167121779Ssuz	}
1168121779Ssuz
1169164339Ssuz	if (pi == NULL || hlimp == NULL) {
1170164339Ssuz		/*
1171164339Ssuz		 * This can happen when the kernel failed to allocate memory
1172164339Ssuz		 * for the ancillary data.  Although we might be able to handle
1173164339Ssuz		 * some cases without this info, those are minor and not so
1174164339Ssuz		 * important, so it's better to discard the packet for safer
1175164339Ssuz		 * operation.
1176164339Ssuz		 */
1177164339Ssuz		trace(1, "IPv6 packet information cannot be retrieved\n");
1178164339Ssuz		return;
1179164339Ssuz	}
1180164339Ssuz
118155163Sshin	nh = fsock.sin6_addr;
118255163Sshin	nn = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
118355163Sshin		sizeof(struct netinfo6);
118455163Sshin	rp = (struct rip6 *)buf;
118555163Sshin	np = rp->rip6_nets;
118655163Sshin
1187119035Sume	if (rp->rip6_vers != RIP6_VERSION) {
118855163Sshin		trace(1, "Incorrect RIP version %d\n", rp->rip6_vers);
118955163Sshin		return;
119055163Sshin	}
119155163Sshin	if (rp->rip6_cmd == RIP6_REQUEST) {
119278064Sume		if (idx && idx < nindex2ifc) {
119378064Sume			ifcp = index2ifc[idx];
119455163Sshin			riprequest(ifcp, np, nn, &fsock);
119555163Sshin		} else {
119655163Sshin			riprequest(NULL, np, nn, &fsock);
119755163Sshin		}
119862607Sitojun		return;
119962607Sitojun	}
120055163Sshin
120155163Sshin	if (!IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr)) {
1202164339Ssuz		trace(1, "Response from non-ll addr: %s\n",
120378064Sume		    inet6_n2p(&fsock.sin6_addr));
120455163Sshin		return;		/* Ignore packets from non-link-local addr */
120555163Sshin	}
1206164339Ssuz	if (ntohs(fsock.sin6_port) != RIP6_PORT) {
1207164339Ssuz		trace(1, "Response from non-rip port from %s\n",
1208164339Ssuz		    inet6_n2p(&fsock.sin6_addr));
1209164339Ssuz		return;
1210164339Ssuz	}
1211164339Ssuz	if (IN6_IS_ADDR_MULTICAST(&pi->ipi6_addr) && *hlimp != 255) {
1212164339Ssuz		trace(1,
1213164339Ssuz		    "Response packet with a smaller hop limit (%d) from %s\n",
1214164339Ssuz		    *hlimp, inet6_n2p(&fsock.sin6_addr));
1215164339Ssuz		return;
1216164339Ssuz	}
1217164339Ssuz	/*
1218164339Ssuz	 * Further validation: since this program does not send off-link
1219164339Ssuz	 * requests, an incoming response must always come from an on-link
1220164339Ssuz	 * node.  Although this is normally ensured by the source address
1221164339Ssuz	 * check above, it may not 100% be safe because there are router
1222164339Ssuz	 * implementations that (invalidly) allow a packet with a link-local
1223164339Ssuz	 * source address to be forwarded to a different link.
1224164339Ssuz	 * So we also check whether the destination address is a link-local
1225164339Ssuz	 * address or the hop limit is 255.  Note that RFC2080 does not require
1226164339Ssuz	 * the specific hop limit for a unicast response, so we cannot assume
1227164339Ssuz	 * the limitation.
1228164339Ssuz	 */
1229164339Ssuz	if (!IN6_IS_ADDR_LINKLOCAL(&pi->ipi6_addr) && *hlimp != 255) {
1230164339Ssuz		trace(1,
1231164339Ssuz		    "Response packet possibly from an off-link node: "
1232164339Ssuz		    "from %s to %s hlim=%d\n",
1233164339Ssuz		    inet6_n2p(&fsock.sin6_addr),
1234164339Ssuz		    inet6_n2p(&pi->ipi6_addr), *hlimp);
1235164339Ssuz		return;
1236164339Ssuz	}
1237164339Ssuz
1238243231Shrs	idx = fsock.sin6_scope_id;
123978064Sume	ifcp = (idx < nindex2ifc) ? index2ifc[idx] : NULL;
124055163Sshin	if (!ifcp) {
124178064Sume		trace(1, "Packets to unknown interface index %d\n", idx);
124255163Sshin		return;		/* Ignore it */
124355163Sshin	}
124455163Sshin	if (IN6_ARE_ADDR_EQUAL(&ifcp->ifc_mylladdr, &fsock.sin6_addr))
124555163Sshin		return;		/* The packet is from me; ignore */
124655163Sshin	if (rp->rip6_cmd != RIP6_RESPONSE) {
124755163Sshin		trace(1, "Invalid command %d\n", rp->rip6_cmd);
124862607Sitojun		return;
124955163Sshin	}
125078064Sume
125178064Sume	/* -N: no use */
1252243232Shrs	if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
125355163Sshin		return;
125478064Sume
1255228674Sdim	tracet(1, "Recv(%s): from %s.%d info(%zd)\n",
125678064Sume	    ifcp->ifc_name, inet6_n2p(&nh), ntohs(fsock.sin6_port), nn);
125755163Sshin
125855163Sshin	t = time(NULL);
125978064Sume	t_half_lifetime = t - (RIP_LIFETIME/2);
126055163Sshin	for (; nn; nn--, np++) {
126155163Sshin		if (np->rip6_metric == NEXTHOP_METRIC) {
126255163Sshin			/* modify neighbor address */
126355163Sshin			if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
126455163Sshin				nh = np->rip6_dest;
126555163Sshin				trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
126655163Sshin			} else if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest)) {
126755163Sshin				nh = fsock.sin6_addr;
126855163Sshin				trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
126955163Sshin			} else {
127055163Sshin				nh = fsock.sin6_addr;
127155163Sshin				trace(1, "\tInvalid Nexthop: %s\n",
127278064Sume				    inet6_n2p(&np->rip6_dest));
127355163Sshin			}
127455163Sshin			continue;
127555163Sshin		}
127655163Sshin		if (IN6_IS_ADDR_MULTICAST(&np->rip6_dest)) {
127755163Sshin			trace(1, "\tMulticast netinfo6: %s/%d [%d]\n",
127855163Sshin				inet6_n2p(&np->rip6_dest),
127955163Sshin				np->rip6_plen, np->rip6_metric);
128055163Sshin			continue;
128155163Sshin		}
128255163Sshin		if (IN6_IS_ADDR_LOOPBACK(&np->rip6_dest)) {
128355163Sshin			trace(1, "\tLoopback netinfo6: %s/%d [%d]\n",
128455163Sshin				inet6_n2p(&np->rip6_dest),
128555163Sshin				np->rip6_plen, np->rip6_metric);
128655163Sshin			continue;
128755163Sshin		}
128855163Sshin		if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
128955163Sshin			trace(1, "\tLink Local netinfo6: %s/%d [%d]\n",
129055163Sshin				inet6_n2p(&np->rip6_dest),
129155163Sshin				np->rip6_plen, np->rip6_metric);
129255163Sshin			continue;
129355163Sshin		}
129455163Sshin		/* may need to pass sitelocal prefix in some case, however*/
129555163Sshin		if (IN6_IS_ADDR_SITELOCAL(&np->rip6_dest) && !lflag) {
129655163Sshin			trace(1, "\tSite Local netinfo6: %s/%d [%d]\n",
129755163Sshin				inet6_n2p(&np->rip6_dest),
129855163Sshin				np->rip6_plen, np->rip6_metric);
129955163Sshin			continue;
130055163Sshin		}
130155163Sshin		trace(2, "\tnetinfo6: %s/%d [%d]",
130255163Sshin			inet6_n2p(&np->rip6_dest),
130355163Sshin			np->rip6_plen, np->rip6_metric);
130455163Sshin		if (np->rip6_tag)
130555163Sshin			trace(2, "  tag=0x%04x", ntohs(np->rip6_tag) & 0xffff);
130662607Sitojun		if (dflag >= 2) {
130762607Sitojun			ia = np->rip6_dest;
130862607Sitojun			applyplen(&ia, np->rip6_plen);
130962607Sitojun			if (!IN6_ARE_ADDR_EQUAL(&ia, &np->rip6_dest))
131062607Sitojun				trace(2, " [junk outside prefix]");
131162607Sitojun		}
131255163Sshin
131378064Sume		/*
131478064Sume		 * -L: listen only if the prefix matches the configuration
131578064Sume		 */
1316243232Shrs                ok = 1;	/* if there's no L filter, it is ok */
1317243232Shrs                TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
1318243232Shrs                        if (iffp->iff_type != IFIL_TYPE_L)
1319243232Shrs                                continue;
1320243232Shrs                        ok = 0;
1321243232Shrs                        if (np->rip6_plen < iffp->iff_plen)
1322243232Shrs                                continue;
1323243232Shrs                        /* special rule: ::/0 means default, not "in /0" */
1324243232Shrs                        if (iffp->iff_plen == 0 && np->rip6_plen > 0)
1325243232Shrs                                continue;
1326243232Shrs                        ia = np->rip6_dest;
1327243232Shrs                        applyplen(&ia, iffp->iff_plen);
1328243232Shrs                        if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1329243232Shrs                                ok = 1;
1330243232Shrs                                break;
1331243232Shrs                        }
1332243232Shrs                }
133355163Sshin		if (!ok) {
133455163Sshin			trace(2, "  (filtered)\n");
133555163Sshin			continue;
133655163Sshin		}
133755163Sshin
133855163Sshin		trace(2, "\n");
133955163Sshin		np->rip6_metric++;
134055163Sshin		np->rip6_metric += ifcp->ifc_metric;
134155163Sshin		if (np->rip6_metric > HOPCNT_INFINITY6)
134255163Sshin			np->rip6_metric = HOPCNT_INFINITY6;
134355163Sshin
134455163Sshin		applyplen(&np->rip6_dest, np->rip6_plen);
1345243232Shrs		if ((rrt = rtsearch(np)) != NULL) {
134655163Sshin			if (rrt->rrt_t == 0)
134755163Sshin				continue;	/* Intf route has priority */
134855163Sshin			nq = &rrt->rrt_info;
134955163Sshin			if (nq->rip6_metric > np->rip6_metric) {
135055163Sshin				if (rrt->rrt_index == ifcp->ifc_index &&
135155163Sshin				    IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
135255163Sshin					/* Small metric from the same gateway */
135355163Sshin					nq->rip6_metric = np->rip6_metric;
135455163Sshin				} else {
135555163Sshin					/* Better route found */
135655163Sshin					rrt->rrt_index = ifcp->ifc_index;
135755163Sshin					/* Update routing table */
135855163Sshin					delroute(nq, &rrt->rrt_gw);
135955163Sshin					rrt->rrt_gw = nh;
136055163Sshin					*nq = *np;
136155163Sshin					addroute(rrt, &nh, ifcp);
136255163Sshin				}
136362607Sitojun				rrt->rrt_rflags |= RRTF_CHANGED;
136455163Sshin				rrt->rrt_t = t;
136555163Sshin				need_trigger = 1;
136655163Sshin			} else if (nq->rip6_metric < np->rip6_metric &&
136755163Sshin				   rrt->rrt_index == ifcp->ifc_index &&
136855163Sshin				   IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
136955163Sshin				/* Got worse route from same gw */
137055163Sshin				nq->rip6_metric = np->rip6_metric;
137155163Sshin				rrt->rrt_t = t;
137262607Sitojun				rrt->rrt_rflags |= RRTF_CHANGED;
137355163Sshin				need_trigger = 1;
137455163Sshin			} else if (nq->rip6_metric == np->rip6_metric &&
137555163Sshin				   np->rip6_metric < HOPCNT_INFINITY6) {
137678064Sume				if (rrt->rrt_index == ifcp->ifc_index &&
137778064Sume				   IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
137878064Sume					/* same metric, same route from same gw */
137978064Sume					rrt->rrt_t = t;
138078064Sume				} else if (rrt->rrt_t < t_half_lifetime) {
138178064Sume					/* Better route found */
138278064Sume					rrt->rrt_index = ifcp->ifc_index;
138378064Sume					/* Update routing table */
138478064Sume					delroute(nq, &rrt->rrt_gw);
138578064Sume					rrt->rrt_gw = nh;
138678064Sume					*nq = *np;
138778064Sume					addroute(rrt, &nh, ifcp);
138878064Sume					rrt->rrt_rflags |= RRTF_CHANGED;
138978064Sume					rrt->rrt_t = t;
139078064Sume				}
139155163Sshin			}
139262607Sitojun			/*
139355163Sshin			 * if nq->rip6_metric == HOPCNT_INFINITY6 then
139478064Sume			 * do not update age value.  Do nothing.
139555163Sshin			 */
139655163Sshin		} else if (np->rip6_metric < HOPCNT_INFINITY6) {
139755163Sshin			/* Got a new valid route */
139878064Sume			if ((rrt = MALLOC(struct riprt)) == NULL) {
139955163Sshin				fatal("malloc: struct riprt");
140078064Sume				/*NOTREACHED*/
140178064Sume			}
140262607Sitojun			memset(rrt, 0, sizeof(*rrt));
140355163Sshin			nq = &rrt->rrt_info;
140455163Sshin
140555163Sshin			rrt->rrt_same = NULL;
140655163Sshin			rrt->rrt_index = ifcp->ifc_index;
140755163Sshin			rrt->rrt_flags = RTF_UP|RTF_GATEWAY;
140855163Sshin			rrt->rrt_gw = nh;
140955163Sshin			*nq = *np;
141055163Sshin			applyplen(&nq->rip6_dest, nq->rip6_plen);
141155163Sshin			if (nq->rip6_plen == sizeof(struct in6_addr) * 8)
141255163Sshin				rrt->rrt_flags |= RTF_HOST;
141355163Sshin
141455163Sshin			/* Update routing table */
141555163Sshin			addroute(rrt, &nh, ifcp);
141662607Sitojun			rrt->rrt_rflags |= RRTF_CHANGED;
141755163Sshin			need_trigger = 1;
141855163Sshin			rrt->rrt_t = t;
1419243232Shrs
1420243232Shrs			/* Put the route to the list */
1421243232Shrs			TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
142255163Sshin		}
142355163Sshin	}
142455163Sshin	/* XXX need to care the interval between triggered updates */
142555163Sshin	if (need_trigger) {
142655163Sshin		if (nextalarm > time(NULL) + RIP_TRIG_INT6_MAX) {
1427243232Shrs			TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
142855163Sshin				if (ifcp->ifc_index == ic->ifc_index)
142955163Sshin					continue;
143055163Sshin				if (ic->ifc_flags & IFF_UP)
143155163Sshin					ripsend(ic, &ic->ifc_ripsin,
143262607Sitojun						RRTF_CHANGED);
143355163Sshin			}
143455163Sshin		}
143555163Sshin		/* Reset the flag */
1436243232Shrs		TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
143762607Sitojun			rrt->rrt_rflags &= ~RRTF_CHANGED;
1438243232Shrs		}
143955163Sshin	}
144055163Sshin}
144155163Sshin
144255163Sshin/*
144355163Sshin * Send all routes request packet to the specified interface.
144455163Sshin */
144555163Sshinvoid
1446243232Shrssendrequest(struct ifc *ifcp)
144755163Sshin{
144855163Sshin	struct netinfo6 *np;
144955163Sshin	int error;
145055163Sshin
145155163Sshin	if (ifcp->ifc_flags & IFF_LOOPBACK)
145255163Sshin		return;
145355163Sshin	ripbuf->rip6_cmd = RIP6_REQUEST;
145455163Sshin	np = ripbuf->rip6_nets;
145555163Sshin	memset(np, 0, sizeof(struct netinfo6));
145655163Sshin	np->rip6_metric = HOPCNT_INFINITY6;
145755163Sshin	tracet(1, "Send rtdump Request to %s (%s)\n",
145855163Sshin		ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
145955163Sshin	error = sendpacket(&ifcp->ifc_ripsin, RIPSIZE(1));
146055163Sshin	if (error == EAFNOSUPPORT) {
146155163Sshin		/* Protocol not supported */
146255163Sshin		tracet(1, "Could not send rtdump Request to %s (%s): "
146355163Sshin			"set IFF_UP to 0\n",
146455163Sshin			ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
146555163Sshin		ifcp->ifc_flags &= ~IFF_UP;	/* As if down for AF_INET6 */
146655163Sshin	}
146755163Sshin	ripbuf->rip6_cmd = RIP6_RESPONSE;
146855163Sshin}
146955163Sshin
147055163Sshin/*
147155163Sshin * Process a RIP6_REQUEST packet.
147255163Sshin */
147355163Sshinvoid
1474243232Shrsriprequest(struct ifc *ifcp,
1475243232Shrs	struct netinfo6 *np,
1476243232Shrs	int nn,
1477243232Shrs	struct sockaddr_in6 *sin6)
147855163Sshin{
147955163Sshin	int i;
148055163Sshin	struct riprt *rrt;
148155163Sshin
148255163Sshin	if (!(nn == 1 && IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest) &&
148355163Sshin	      np->rip6_plen == 0 && np->rip6_metric == HOPCNT_INFINITY6)) {
148455163Sshin		/* Specific response, don't split-horizon */
148555163Sshin		trace(1, "\tRIP Request\n");
148655163Sshin		for (i = 0; i < nn; i++, np++) {
1487243232Shrs			rrt = rtsearch(np);
148855163Sshin			if (rrt)
148955163Sshin				np->rip6_metric = rrt->rrt_info.rip6_metric;
149055163Sshin			else
149155163Sshin				np->rip6_metric = HOPCNT_INFINITY6;
149255163Sshin		}
1493119031Sume		(void)sendpacket(sin6, RIPSIZE(nn));
149455163Sshin		return;
149555163Sshin	}
149655163Sshin	/* Whole routing table dump */
149755163Sshin	trace(1, "\tRIP Request -- whole routing table\n");
1498119031Sume	ripsend(ifcp, sin6, RRTF_SENDANYWAY);
149955163Sshin}
150055163Sshin
150155163Sshin/*
150255163Sshin * Get information of each interface.
150355163Sshin */
150455163Sshinvoid
1505243232Shrsifconfig(void)
150655163Sshin{
150762607Sitojun	struct ifaddrs *ifap, *ifa;
150862607Sitojun	struct ifc *ifcp;
150962607Sitojun	struct ipv6_mreq mreq;
151062607Sitojun	int s;
151162607Sitojun
151278064Sume	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
151362607Sitojun		fatal("socket");
151478064Sume		/*NOTREACHED*/
151578064Sume	}
151662607Sitojun
151778064Sume	if (getifaddrs(&ifap) != 0) {
151862607Sitojun		fatal("getifaddrs");
151978064Sume		/*NOTREACHED*/
152078064Sume	}
152162607Sitojun
152262607Sitojun	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
152362607Sitojun		if (ifa->ifa_addr->sa_family != AF_INET6)
152462607Sitojun			continue;
152562607Sitojun		ifcp = ifc_find(ifa->ifa_name);
152662607Sitojun		/* we are interested in multicast-capable interfaces */
152762607Sitojun		if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
152862607Sitojun			continue;
152962607Sitojun		if (!ifcp) {
153062607Sitojun			/* new interface */
153178064Sume			if ((ifcp = MALLOC(struct ifc)) == NULL) {
153262607Sitojun				fatal("malloc: struct ifc");
153378064Sume				/*NOTREACHED*/
153478064Sume			}
153562607Sitojun			memset(ifcp, 0, sizeof(*ifcp));
1536243232Shrs
153762607Sitojun			ifcp->ifc_index = -1;
1538243232Shrs			strlcpy(ifcp->ifc_name, ifa->ifa_name,
1539243232Shrs			    sizeof(ifcp->ifc_name));
1540243232Shrs			TAILQ_INIT(&ifcp->ifc_ifac_head);
1541243232Shrs			TAILQ_INIT(&ifcp->ifc_iff_head);
154262607Sitojun			ifcp->ifc_flags = ifa->ifa_flags;
1543243232Shrs			TAILQ_INSERT_HEAD(&ifc_head, ifcp, ifc_next);
154462607Sitojun			trace(1, "newif %s <%s>\n", ifcp->ifc_name,
154562607Sitojun				ifflags(ifcp->ifc_flags));
154662607Sitojun			if (!strcmp(ifcp->ifc_name, LOOPBACK_IF))
154762607Sitojun				loopifcp = ifcp;
154862607Sitojun		} else {
154962607Sitojun			/* update flag, this may be up again */
155062607Sitojun			if (ifcp->ifc_flags != ifa->ifa_flags) {
155162607Sitojun				trace(1, "%s: <%s> -> ", ifcp->ifc_name,
155262607Sitojun					ifflags(ifcp->ifc_flags));
155362607Sitojun				trace(1, "<%s>\n", ifflags(ifa->ifa_flags));
155478064Sume				ifcp->ifc_cflags |= IFC_CHANGED;
155562607Sitojun			}
155662607Sitojun			ifcp->ifc_flags = ifa->ifa_flags;
155762607Sitojun		}
1558243232Shrs		if (ifconfig1(ifa->ifa_name, ifa->ifa_addr, ifcp, s) < 0) {
1559243232Shrs			/* maybe temporary failure */
1560243232Shrs			continue;
1561243232Shrs		}
156262607Sitojun		if ((ifcp->ifc_flags & (IFF_LOOPBACK | IFF_UP)) == IFF_UP
156362607Sitojun		 && 0 < ifcp->ifc_index && !ifcp->ifc_joined) {
156462607Sitojun			mreq.ipv6mr_multiaddr = ifcp->ifc_ripsin.sin6_addr;
156562607Sitojun			mreq.ipv6mr_interface = ifcp->ifc_index;
156678064Sume			if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
156778064Sume			    &mreq, sizeof(mreq)) < 0) {
156862607Sitojun				fatal("IPV6_JOIN_GROUP");
156978064Sume				/*NOTREACHED*/
157078064Sume			}
157162607Sitojun			trace(1, "join %s %s\n", ifcp->ifc_name, RIP6_DEST);
157262607Sitojun			ifcp->ifc_joined++;
157362607Sitojun		}
157462607Sitojun	}
157562607Sitojun	close(s);
157662607Sitojun	freeifaddrs(ifap);
157755163Sshin}
157855163Sshin
1579243232Shrsint
1580243232Shrsifconfig1(const char *name,
1581243232Shrs	const struct sockaddr *sa,
1582243232Shrs	struct ifc *ifcp,
1583243232Shrs	int s)
158455163Sshin{
158555163Sshin	struct	in6_ifreq ifr;
1586119031Sume	const struct sockaddr_in6 *sin6;
1587243232Shrs	struct	ifac *ifac;
158855163Sshin	int	plen;
158955163Sshin	char	buf[BUFSIZ];
159055163Sshin
1591119031Sume	sin6 = (const struct sockaddr_in6 *)sa;
1592122677Sume	if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && !lflag)
1593243232Shrs		return (-1);
1594119031Sume	ifr.ifr_addr = *sin6;
1595119032Sume	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
159678064Sume	if (ioctl(s, SIOCGIFNETMASK_IN6, (char *)&ifr) < 0) {
1597243232Shrs		syslog(LOG_INFO, "ioctl: SIOCGIFNETMASK_IN6");
1598243232Shrs		return (-1);
159978064Sume	}
160078064Sume	plen = sin6mask2len(&ifr.ifr_addr);
1601243232Shrs	if ((ifac = ifa_match(ifcp, &sin6->sin6_addr, plen)) != NULL) {
160255163Sshin		/* same interface found */
160355163Sshin		/* need check if something changed */
160455163Sshin		/* XXX not yet implemented */
1605243232Shrs		return (-1);
160655163Sshin	}
160755163Sshin	/*
160855163Sshin	 * New address is found
160955163Sshin	 */
1610243232Shrs	if ((ifac = MALLOC(struct ifac)) == NULL) {
161155163Sshin		fatal("malloc: struct ifac");
161278064Sume		/*NOTREACHED*/
161378064Sume	}
1614243232Shrs	memset(ifac, 0, sizeof(*ifac));
1615243232Shrs
1616243232Shrs	ifac->ifac_ifc = ifcp;
1617243232Shrs	ifac->ifac_addr = sin6->sin6_addr;
1618243232Shrs	ifac->ifac_plen = plen;
1619243232Shrs	ifac->ifac_scope_id = sin6->sin6_scope_id;
162055163Sshin	if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1621119031Sume		ifr.ifr_addr = *sin6;
162278064Sume		if (ioctl(s, SIOCGIFDSTADDR_IN6, (char *)&ifr) < 0) {
162355163Sshin			fatal("ioctl: SIOCGIFDSTADDR_IN6");
162478064Sume			/*NOTREACHED*/
162578064Sume		}
1626243232Shrs		ifac->ifac_raddr = ifr.ifr_dstaddr.sin6_addr;
1627243232Shrs		inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr, buf,
1628243232Shrs		    sizeof(buf));
162955163Sshin		trace(1, "found address %s/%d -- %s\n",
1630243232Shrs			inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen, buf);
163155163Sshin	} else {
163255163Sshin		trace(1, "found address %s/%d\n",
1633243232Shrs			inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen);
163455163Sshin	}
1635243232Shrs	if (ifcp->ifc_index < 0 && IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
1636243232Shrs		ifcp->ifc_mylladdr = ifac->ifac_addr;
1637243232Shrs		ifcp->ifc_index = ifac->ifac_scope_id;
163855163Sshin		memcpy(&ifcp->ifc_ripsin, &ripsin, ripsin.ss_len);
1639243231Shrs		ifcp->ifc_ripsin.sin6_scope_id = ifcp->ifc_index;
164055163Sshin		setindex2ifc(ifcp->ifc_index, ifcp);
164155163Sshin		ifcp->ifc_mtu = getifmtu(ifcp->ifc_index);
164255163Sshin		if (ifcp->ifc_mtu > RIP6_MAXMTU)
164355163Sshin			ifcp->ifc_mtu = RIP6_MAXMTU;
164478064Sume		if (ioctl(s, SIOCGIFMETRIC, (char *)&ifr) < 0) {
164555163Sshin			fatal("ioctl: SIOCGIFMETRIC");
164678064Sume			/*NOTREACHED*/
164778064Sume		}
164855163Sshin		ifcp->ifc_metric = ifr.ifr_metric;
164955163Sshin		trace(1, "\tindex: %d, mtu: %d, metric: %d\n",
165055163Sshin			ifcp->ifc_index, ifcp->ifc_mtu, ifcp->ifc_metric);
165178064Sume	} else
165278064Sume		ifcp->ifc_cflags |= IFC_CHANGED;
1653243232Shrs
1654243232Shrs	TAILQ_INSERT_HEAD(&ifcp->ifc_ifac_head, ifac, ifac_next);
1655243232Shrs
1656243232Shrs	return 0;
165755163Sshin}
165855163Sshin
1659243233Shrsvoid
1660243233Shrsifremove(int ifindex)
1661243233Shrs{
1662243233Shrs	struct ifc *ifcp;
1663243233Shrs	struct riprt *rrt;
1664243233Shrs
1665243233Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
1666243233Shrs		if (ifcp->ifc_index == ifindex)
1667243233Shrs			break;
1668243233Shrs	}
1669243233Shrs	if (ifcp == NULL)
1670243233Shrs		return;
1671243233Shrs
1672243233Shrs	tracet(1, "ifremove: %s is departed.\n", ifcp->ifc_name);
1673243233Shrs	TAILQ_REMOVE(&ifc_head, ifcp, ifc_next);
1674243233Shrs
1675243233Shrs	TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1676243233Shrs		if (rrt->rrt_index == ifcp->ifc_index &&
1677243233Shrs		    rrt->rrt_rflags & RRTF_AGGREGATE)
1678243233Shrs			delroute(&rrt->rrt_info, &rrt->rrt_gw);
1679243233Shrs	}
1680243233Shrs	free(ifcp);
1681243233Shrs}
1682243233Shrs
168355163Sshin/*
168455163Sshin * Receive and process routing messages.
168555163Sshin * Update interface information as necesssary.
168655163Sshin */
168755163Sshinvoid
1688243232Shrsrtrecv(void)
168955163Sshin{
169055163Sshin	char buf[BUFSIZ];
1691243232Shrs	char *p, *q = NULL;
169255163Sshin	struct rt_msghdr *rtm;
169355163Sshin	struct ifa_msghdr *ifam;
169455163Sshin	struct if_msghdr *ifm;
1695243233Shrs	struct if_announcemsghdr *ifan;
169655163Sshin	int len;
169778064Sume	struct ifc *ifcp, *ic;
169855163Sshin	int iface = 0, rtable = 0;
169955163Sshin	struct sockaddr_in6 *rta[RTAX_MAX];
170078064Sume	struct sockaddr_in6 mask;
1701243232Shrs	int i, addrs = 0;
170278064Sume	struct riprt *rrt;
170355163Sshin
170455163Sshin	if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
170555163Sshin		perror("read from rtsock");
170678064Sume		exit(1);
170755163Sshin	}
1708243232Shrs	if (len == 0)
1709243232Shrs		return;
1710243232Shrs#if 0
171155163Sshin	if (len < sizeof(*rtm)) {
171269279Sume		trace(1, "short read from rtsock: %d (should be > %lu)\n",
171369279Sume			len, (u_long)sizeof(*rtm));
171455163Sshin		return;
171555163Sshin	}
1716243232Shrs#endif
1717119042Sume	if (dflag >= 2) {
1718119042Sume		fprintf(stderr, "rtmsg:\n");
1719119042Sume		for (i = 0; i < len; i++) {
1720119042Sume			fprintf(stderr, "%02x ", buf[i] & 0xff);
1721119042Sume			if (i % 16 == 15) fprintf(stderr, "\n");
1722119042Sume		}
1723119042Sume		fprintf(stderr, "\n");
1724119042Sume	}
172555163Sshin
172655163Sshin	for (p = buf; p - buf < len; p += ((struct rt_msghdr *)p)->rtm_msglen) {
1727243232Shrs		if (((struct rt_msghdr *)p)->rtm_version != RTM_VERSION)
1728243232Shrs			continue;
1729243232Shrs
173055163Sshin		/* safety against bogus message */
173155163Sshin		if (((struct rt_msghdr *)p)->rtm_msglen <= 0) {
173255163Sshin			trace(1, "bogus rtmsg: length=%d\n",
173355163Sshin				((struct rt_msghdr *)p)->rtm_msglen);
173455163Sshin			break;
173555163Sshin		}
173655163Sshin		rtm = NULL;
173755163Sshin		ifam = NULL;
173855163Sshin		ifm = NULL;
173955163Sshin		switch (((struct rt_msghdr *)p)->rtm_type) {
174055163Sshin		case RTM_NEWADDR:
174155163Sshin		case RTM_DELADDR:
174255163Sshin			ifam = (struct ifa_msghdr *)p;
174355163Sshin			addrs = ifam->ifam_addrs;
174455163Sshin			q = (char *)(ifam + 1);
174555163Sshin			break;
174655163Sshin		case RTM_IFINFO:
174755163Sshin			ifm = (struct if_msghdr *)p;
174855163Sshin			addrs = ifm->ifm_addrs;
174955163Sshin			q = (char *)(ifm + 1);
175055163Sshin			break;
1751243233Shrs		case RTM_IFANNOUNCE:
1752243233Shrs			ifan = (struct if_announcemsghdr *)p;
1753243233Shrs			switch (ifan->ifan_what) {
1754243233Shrs			case IFAN_ARRIVAL:
1755243233Shrs				iface++;
1756243233Shrs				break;
1757243233Shrs			case IFAN_DEPARTURE:
1758243233Shrs				ifremove(ifan->ifan_index);
1759243233Shrs				iface++;
1760243233Shrs				break;
1761243233Shrs			}
1762243233Shrs			break;
176355163Sshin		default:
176455163Sshin			rtm = (struct rt_msghdr *)p;
176555163Sshin			addrs = rtm->rtm_addrs;
176655163Sshin			q = (char *)(rtm + 1);
176755163Sshin			if (rtm->rtm_version != RTM_VERSION) {
176855163Sshin				trace(1, "unexpected rtmsg version %d "
176955163Sshin					"(should be %d)\n",
177055163Sshin					rtm->rtm_version, RTM_VERSION);
177155163Sshin				continue;
177255163Sshin			}
177355163Sshin			if (rtm->rtm_pid == pid) {
177455163Sshin#if 0
177555163Sshin				trace(1, "rtmsg looped back to me, ignored\n");
177655163Sshin#endif
177755163Sshin				continue;
177855163Sshin			}
177955163Sshin			break;
178055163Sshin		}
178155163Sshin		memset(&rta, 0, sizeof(rta));
178255163Sshin		for (i = 0; i < RTAX_MAX; i++) {
178355163Sshin			if (addrs & (1 << i)) {
178455163Sshin				rta[i] = (struct sockaddr_in6 *)q;
178555163Sshin				q += ROUNDUP(rta[i]->sin6_len);
178655163Sshin			}
178755163Sshin		}
178855163Sshin
178955163Sshin		trace(1, "rtsock: %s (addrs=%x)\n",
179055163Sshin			rttypes((struct rt_msghdr *)p), addrs);
179155163Sshin		if (dflag >= 2) {
179255163Sshin			for (i = 0;
179355163Sshin			     i < ((struct rt_msghdr *)p)->rtm_msglen;
179455163Sshin			     i++) {
179555163Sshin				fprintf(stderr, "%02x ", p[i] & 0xff);
179655163Sshin				if (i % 16 == 15) fprintf(stderr, "\n");
179755163Sshin			}
179855163Sshin			fprintf(stderr, "\n");
179955163Sshin		}
180055163Sshin
180155163Sshin		/*
180255163Sshin		 * Easy ones first.
180355163Sshin		 *
180455163Sshin		 * We may be able to optimize by using ifm->ifm_index or
180555163Sshin		 * ifam->ifam_index.  For simplicity we don't do that here.
180655163Sshin		 */
180755163Sshin		switch (((struct rt_msghdr *)p)->rtm_type) {
180855163Sshin		case RTM_NEWADDR:
180955163Sshin		case RTM_IFINFO:
181055163Sshin			iface++;
181155163Sshin			continue;
181255163Sshin		case RTM_ADD:
181355163Sshin			rtable++;
181455163Sshin			continue;
181555163Sshin		case RTM_LOSING:
181655163Sshin		case RTM_MISS:
181755163Sshin		case RTM_GET:
181855163Sshin		case RTM_LOCK:
181955163Sshin			/* nothing to be done here */
182055163Sshin			trace(1, "\tnothing to be done, ignored\n");
182155163Sshin			continue;
182255163Sshin		}
182355163Sshin
182455163Sshin#if 0
182555163Sshin		if (rta[RTAX_DST] == NULL) {
182655163Sshin			trace(1, "\tno destination, ignored\n");
182762607Sitojun			continue;
182855163Sshin		}
182955163Sshin		if (rta[RTAX_DST]->sin6_family != AF_INET6) {
183055163Sshin			trace(1, "\taf mismatch, ignored\n");
183155163Sshin			continue;
183255163Sshin		}
183355163Sshin		if (IN6_IS_ADDR_LINKLOCAL(&rta[RTAX_DST]->sin6_addr)) {
183455163Sshin			trace(1, "\tlinklocal destination, ignored\n");
183555163Sshin			continue;
183655163Sshin		}
183755163Sshin		if (IN6_ARE_ADDR_EQUAL(&rta[RTAX_DST]->sin6_addr, &in6addr_loopback)) {
183855163Sshin			trace(1, "\tloopback destination, ignored\n");
183955163Sshin			continue;		/* Loopback */
184055163Sshin		}
184155163Sshin		if (IN6_IS_ADDR_MULTICAST(&rta[RTAX_DST]->sin6_addr)) {
184255163Sshin			trace(1, "\tmulticast destination, ignored\n");
184355163Sshin			continue;
184455163Sshin		}
184555163Sshin#endif
184655163Sshin
184755163Sshin		/* hard ones */
184855163Sshin		switch (((struct rt_msghdr *)p)->rtm_type) {
184955163Sshin		case RTM_NEWADDR:
185055163Sshin		case RTM_IFINFO:
185155163Sshin		case RTM_ADD:
185255163Sshin		case RTM_LOSING:
185355163Sshin		case RTM_MISS:
185455163Sshin		case RTM_GET:
185555163Sshin		case RTM_LOCK:
185655163Sshin			/* should already be handled */
185755163Sshin			fatal("rtrecv: never reach here");
185878064Sume			/*NOTREACHED*/
185955163Sshin		case RTM_DELETE:
186078064Sume			if (!rta[RTAX_DST] || !rta[RTAX_GATEWAY]) {
186178064Sume				trace(1, "\tsome of dst/gw/netamsk are "
186278064Sume				    "unavailable, ignored\n");
186355163Sshin				break;
186455163Sshin			}
186578064Sume			if ((rtm->rtm_flags & RTF_HOST) != 0) {
186678064Sume				mask.sin6_len = sizeof(mask);
186778064Sume				memset(&mask.sin6_addr, 0xff,
186878064Sume				    sizeof(mask.sin6_addr));
186978064Sume				rta[RTAX_NETMASK] = &mask;
187078064Sume			} else if (!rta[RTAX_NETMASK]) {
187178064Sume				trace(1, "\tsome of dst/gw/netamsk are "
187278064Sume				    "unavailable, ignored\n");
187378064Sume				break;
187478064Sume			}
187578064Sume			if (rt_del(rta[RTAX_DST], rta[RTAX_GATEWAY],
187678064Sume			    rta[RTAX_NETMASK]) == 0) {
187755163Sshin				rtable++;	/*just to be sure*/
187855163Sshin			}
187955163Sshin			break;
188055163Sshin		case RTM_CHANGE:
188155163Sshin		case RTM_REDIRECT:
188255163Sshin			trace(1, "\tnot supported yet, ignored\n");
188355163Sshin			break;
188455163Sshin		case RTM_DELADDR:
188555163Sshin			if (!rta[RTAX_NETMASK] || !rta[RTAX_IFA]) {
188655163Sshin				trace(1, "\tno netmask or ifa given, ignored\n");
188755163Sshin				break;
188855163Sshin			}
188955163Sshin			if (ifam->ifam_index < nindex2ifc)
189055163Sshin				ifcp = index2ifc[ifam->ifam_index];
189155163Sshin			else
189255163Sshin				ifcp = NULL;
189355163Sshin			if (!ifcp) {
189455163Sshin				trace(1, "\tinvalid ifam_index %d, ignored\n",
189555163Sshin					ifam->ifam_index);
189655163Sshin				break;
189755163Sshin			}
189878064Sume			if (!rt_deladdr(ifcp, rta[RTAX_IFA], rta[RTAX_NETMASK]))
189978064Sume				iface++;
190055163Sshin			break;
190155163Sshin		}
190255163Sshin
190355163Sshin	}
190455163Sshin
190555163Sshin	if (iface) {
190655163Sshin		trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
190755163Sshin		ifconfig();
1908243232Shrs		TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
190978064Sume			if (ifcp->ifc_cflags & IFC_CHANGED) {
191078064Sume				if (ifrt(ifcp, 1)) {
1911243232Shrs					TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
191278064Sume						if (ifcp->ifc_index == ic->ifc_index)
191378064Sume							continue;
191478064Sume						if (ic->ifc_flags & IFF_UP)
191578064Sume							ripsend(ic, &ic->ifc_ripsin,
191678064Sume							RRTF_CHANGED);
191778064Sume					}
191878064Sume					/* Reset the flag */
1919243232Shrs					TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
192078064Sume						rrt->rrt_rflags &= ~RRTF_CHANGED;
1921243232Shrs					}
192278064Sume				}
192378064Sume				ifcp->ifc_cflags &= ~IFC_CHANGED;
192478064Sume			}
1925243232Shrs		}
192655163Sshin	}
192755163Sshin	if (rtable) {
192855163Sshin		trace(1, "rtsock: read routing table again\n");
192955163Sshin		krtread(1);
193055163Sshin	}
193155163Sshin}
193255163Sshin
193355163Sshin/*
193455163Sshin * remove specified route from the internal routing table.
193555163Sshin */
193655163Sshinint
1937243232Shrsrt_del(const struct sockaddr_in6 *sdst,
1938243232Shrs	const struct sockaddr_in6 *sgw,
1939243232Shrs	const struct sockaddr_in6 *smask)
194055163Sshin{
194155163Sshin	const struct in6_addr *dst = NULL;
194255163Sshin	const struct in6_addr *gw = NULL;
194355163Sshin	int prefix;
194455163Sshin	struct netinfo6 ni6;
194555163Sshin	struct riprt *rrt = NULL;
194655163Sshin	time_t t_lifetime;
194755163Sshin
194855163Sshin	if (sdst->sin6_family != AF_INET6) {
194955163Sshin		trace(1, "\tother AF, ignored\n");
195055163Sshin		return -1;
195155163Sshin	}
195255163Sshin	if (IN6_IS_ADDR_LINKLOCAL(&sdst->sin6_addr)
195355163Sshin	 || IN6_ARE_ADDR_EQUAL(&sdst->sin6_addr, &in6addr_loopback)
195455163Sshin	 || IN6_IS_ADDR_MULTICAST(&sdst->sin6_addr)) {
195555163Sshin		trace(1, "\taddress %s not interesting, ignored\n",
195655163Sshin			inet6_n2p(&sdst->sin6_addr));
195755163Sshin		return -1;
195855163Sshin	}
195955163Sshin	dst = &sdst->sin6_addr;
196078064Sume	if (sgw->sin6_family == AF_INET6) {
196155163Sshin		/* easy case */
196255163Sshin		gw = &sgw->sin6_addr;
196378064Sume		prefix = sin6mask2len(smask);
196455163Sshin	} else if (sgw->sin6_family == AF_LINK) {
196555163Sshin		/*
196655163Sshin		 * Interface route... a hard case.  We need to get the prefix
196755163Sshin		 * length from the kernel, but we now are parsing rtmsg.
196855163Sshin		 * We'll purge matching routes from my list, then get the
196955163Sshin		 * fresh list.
197055163Sshin		 */
197155163Sshin		struct riprt *longest;
1972108533Sschweikh		trace(1, "\t%s is an interface route, guessing prefixlen\n",
197355163Sshin			inet6_n2p(dst));
197455163Sshin		longest = NULL;
1975243232Shrs		TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
197655163Sshin			if (IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
197755163Sshin					&sdst->sin6_addr)
197855163Sshin			 && IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)) {
197955163Sshin				if (!longest
198055163Sshin				 || longest->rrt_info.rip6_plen <
198155163Sshin						 rrt->rrt_info.rip6_plen) {
198255163Sshin					longest = rrt;
198355163Sshin				}
198455163Sshin			}
198555163Sshin		}
198655163Sshin		rrt = longest;
198755163Sshin		if (!rrt) {
198855163Sshin			trace(1, "\tno matching interface route found\n");
198955163Sshin			return -1;
199055163Sshin		}
199155163Sshin		gw = &in6addr_loopback;
199255163Sshin		prefix = rrt->rrt_info.rip6_plen;
199355163Sshin	} else {
199478064Sume		trace(1, "\tunsupported af: (gw=%d)\n", sgw->sin6_family);
199555163Sshin		return -1;
199655163Sshin	}
199755163Sshin
199855163Sshin	trace(1, "\tdeleting %s/%d ", inet6_n2p(dst), prefix);
199955163Sshin	trace(1, "gw %s\n", inet6_n2p(gw));
200055163Sshin	t_lifetime = time(NULL) - RIP_LIFETIME;
200155163Sshin	/* age route for interface address */
200255163Sshin	memset(&ni6, 0, sizeof(ni6));
200355163Sshin	ni6.rip6_dest = *dst;
200455163Sshin	ni6.rip6_plen = prefix;
200555163Sshin	applyplen(&ni6.rip6_dest, ni6.rip6_plen);	/*to be sure*/
200655163Sshin	trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6.rip6_dest),
200755163Sshin		ni6.rip6_plen);
2008243232Shrs	if (!rrt && (rrt = rtsearch(&ni6)) == NULL) {
200955163Sshin		trace(1, "\tno route found\n");
201055163Sshin		return -1;
201155163Sshin	}
201278064Sume#if 0
201355163Sshin	if ((rrt->rrt_flags & RTF_STATIC) == 0) {
201455163Sshin		trace(1, "\tyou can delete static routes only\n");
201578064Sume	} else
201678064Sume#endif
201778064Sume	if (!IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, gw)) {
201855163Sshin		trace(1, "\tgw mismatch: %s <-> ",
201955163Sshin			inet6_n2p(&rrt->rrt_gw));
202055163Sshin		trace(1, "%s\n", inet6_n2p(gw));
202155163Sshin	} else {
202255163Sshin		trace(1, "\troute found, age it\n");
202355163Sshin		if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
202455163Sshin			rrt->rrt_t = t_lifetime;
202555163Sshin			rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
202655163Sshin		}
202755163Sshin	}
202855163Sshin	return 0;
202955163Sshin}
203055163Sshin
203155163Sshin/*
203255163Sshin * remove specified address from internal interface/routing table.
203355163Sshin */
203455163Sshinint
2035243232Shrsrt_deladdr(struct ifc *ifcp,
2036243232Shrs	const struct sockaddr_in6 *sifa,
2037243232Shrs	const struct sockaddr_in6 *smask)
203855163Sshin{
203955163Sshin	const struct in6_addr *addr = NULL;
204055163Sshin	int prefix;
2041243232Shrs	struct ifac *ifac = NULL;
204255163Sshin	struct netinfo6 ni6;
204355163Sshin	struct riprt *rrt = NULL;
204455163Sshin	time_t t_lifetime;
204555163Sshin	int updated = 0;
204655163Sshin
204778064Sume	if (sifa->sin6_family != AF_INET6) {
204855163Sshin		trace(1, "\tother AF, ignored\n");
204955163Sshin		return -1;
205055163Sshin	}
205155163Sshin	addr = &sifa->sin6_addr;
205278064Sume	prefix = sin6mask2len(smask);
205355163Sshin
205455163Sshin	trace(1, "\tdeleting %s/%d from %s\n",
205555163Sshin		inet6_n2p(addr), prefix, ifcp->ifc_name);
2056243232Shrs	ifac = ifa_match(ifcp, addr, prefix);
2057243232Shrs	if (!ifac) {
205855163Sshin		trace(1, "\tno matching ifa found for %s/%d on %s\n",
205955163Sshin			inet6_n2p(addr), prefix, ifcp->ifc_name);
206055163Sshin		return -1;
206155163Sshin	}
2062243232Shrs	if (ifac->ifac_ifc != ifcp) {
206355163Sshin		trace(1, "\taddress table corrupt: back pointer does not match "
206455163Sshin			"(%s != %s)\n",
2065243232Shrs			ifcp->ifc_name, ifac->ifac_ifc->ifc_name);
206655163Sshin		return -1;
206755163Sshin	}
2068243232Shrs	TAILQ_REMOVE(&ifcp->ifc_ifac_head, ifac, ifac_next);
206955163Sshin	t_lifetime = time(NULL) - RIP_LIFETIME;
207055163Sshin	/* age route for interface address */
207155163Sshin	memset(&ni6, 0, sizeof(ni6));
2072243232Shrs	ni6.rip6_dest = ifac->ifac_addr;
2073243232Shrs	ni6.rip6_plen = ifac->ifac_plen;
207455163Sshin	applyplen(&ni6.rip6_dest, ni6.rip6_plen);
207555163Sshin	trace(1, "\tfind interface route %s/%d on %d\n",
207655163Sshin		inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen, ifcp->ifc_index);
2077243232Shrs	if ((rrt = rtsearch(&ni6)) != NULL) {
207855163Sshin		struct in6_addr none;
207955163Sshin		memset(&none, 0, sizeof(none));
208078064Sume		if (rrt->rrt_index == ifcp->ifc_index &&
208178064Sume		    (IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &none) ||
208278064Sume		     IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw))) {
208355163Sshin			trace(1, "\troute found, age it\n");
208455163Sshin			if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
208555163Sshin				rrt->rrt_t = t_lifetime;
208655163Sshin				rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
208755163Sshin			}
208855163Sshin			updated++;
208955163Sshin		} else {
209055163Sshin			trace(1, "\tnon-interface route found: %s/%d on %d\n",
209155163Sshin				inet6_n2p(&rrt->rrt_info.rip6_dest),
209255163Sshin				rrt->rrt_info.rip6_plen,
209355163Sshin				rrt->rrt_index);
209455163Sshin		}
209555163Sshin	} else
209655163Sshin		trace(1, "\tno interface route found\n");
209755163Sshin	/* age route for p2p destination */
209855163Sshin	if (ifcp->ifc_flags & IFF_POINTOPOINT) {
209955163Sshin		memset(&ni6, 0, sizeof(ni6));
2100243232Shrs		ni6.rip6_dest = ifac->ifac_raddr;
210155163Sshin		ni6.rip6_plen = 128;
210255163Sshin		applyplen(&ni6.rip6_dest, ni6.rip6_plen);	/*to be sure*/
210355163Sshin		trace(1, "\tfind p2p route %s/%d on %d\n",
210455163Sshin			inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen,
210555163Sshin			ifcp->ifc_index);
2106243232Shrs		if ((rrt = rtsearch(&ni6)) != NULL) {
210778064Sume			if (rrt->rrt_index == ifcp->ifc_index &&
2108243232Shrs			    IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw,
2109243232Shrs			    &ifac->ifac_addr)) {
211055163Sshin				trace(1, "\troute found, age it\n");
211155163Sshin				if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
211255163Sshin					rrt->rrt_t = t_lifetime;
211355163Sshin					rrt->rrt_info.rip6_metric =
211478064Sume					    HOPCNT_INFINITY6;
211555163Sshin					updated++;
211655163Sshin				}
211755163Sshin			} else {
211855163Sshin				trace(1, "\tnon-p2p route found: %s/%d on %d\n",
211955163Sshin					inet6_n2p(&rrt->rrt_info.rip6_dest),
212055163Sshin					rrt->rrt_info.rip6_plen,
212155163Sshin					rrt->rrt_index);
212255163Sshin			}
212355163Sshin		} else
212455163Sshin			trace(1, "\tno p2p route found\n");
212555163Sshin	}
2126243232Shrs	free(ifac);
2127243232Shrs
2128243232Shrs	return ((updated) ? 0 : -1);
212955163Sshin}
213055163Sshin
213155163Sshin/*
213255163Sshin * Get each interface address and put those interface routes to the route
213355163Sshin * list.
213455163Sshin */
213578064Sumeint
2136243232Shrsifrt(struct ifc *ifcp, int again)
213755163Sshin{
2138243232Shrs	struct ifac *ifac;
2139243232Shrs	struct riprt *rrt = NULL, *search_rrt, *loop_rrt;
214062607Sitojun	struct netinfo6 *np;
214178064Sume	time_t t_lifetime;
214278064Sume	int need_trigger = 0;
214355163Sshin
2144122677Sume#if 0
214555163Sshin	if (ifcp->ifc_flags & IFF_LOOPBACK)
214678064Sume		return 0;			/* ignore loopback */
2147122677Sume#endif
2148122677Sume
214962607Sitojun	if (ifcp->ifc_flags & IFF_POINTOPOINT) {
215062607Sitojun		ifrt_p2p(ifcp, again);
215178064Sume		return 0;
215262607Sitojun	}
215362607Sitojun
2154243232Shrs	TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2155243232Shrs		if (IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
215662607Sitojun#if 0
215762607Sitojun			trace(1, "route: %s on %s: "
215862607Sitojun			    "skip linklocal interface address\n",
2159243232Shrs			    inet6_n2p(&ifac->ifac_addr), ifcp->ifc_name);
216062607Sitojun#endif
216162607Sitojun			continue;
216262607Sitojun		}
2163243232Shrs		if (IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_addr)) {
216462607Sitojun#if 0
216562607Sitojun			trace(1, "route: %s: skip unspec interface address\n",
216662607Sitojun			    ifcp->ifc_name);
216762607Sitojun#endif
216862607Sitojun			continue;
216962607Sitojun		}
2170243232Shrs		if (IN6_IS_ADDR_LOOPBACK(&ifac->ifac_addr)) {
2171122677Sume#if 0
2172122677Sume			trace(1, "route: %s: skip loopback address\n",
2173122677Sume			    ifcp->ifc_name);
2174122677Sume#endif
2175122677Sume			continue;
2176122677Sume		}
217778064Sume		if (ifcp->ifc_flags & IFF_UP) {
217878064Sume			if ((rrt = MALLOC(struct riprt)) == NULL)
217978064Sume				fatal("malloc: struct riprt");
218078064Sume			memset(rrt, 0, sizeof(*rrt));
218178064Sume			rrt->rrt_same = NULL;
218278064Sume			rrt->rrt_index = ifcp->ifc_index;
218378064Sume			rrt->rrt_t = 0;	/* don't age */
2184243232Shrs			rrt->rrt_info.rip6_dest = ifac->ifac_addr;
218578064Sume			rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
218678064Sume			rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2187243232Shrs			rrt->rrt_info.rip6_plen = ifac->ifac_plen;
2188186119Sqingli			rrt->rrt_flags = RTF_HOST;
218978064Sume			rrt->rrt_rflags |= RRTF_CHANGED;
2190243232Shrs			applyplen(&rrt->rrt_info.rip6_dest, ifac->ifac_plen);
219178064Sume			memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2192243232Shrs			rrt->rrt_gw = ifac->ifac_addr;
219378064Sume			np = &rrt->rrt_info;
2194243232Shrs			search_rrt = rtsearch(np);
219578064Sume			if (search_rrt != NULL) {
2196119042Sume				if (search_rrt->rrt_info.rip6_metric <=
219778064Sume				    rrt->rrt_info.rip6_metric) {
219878064Sume					/* Already have better route */
219978064Sume					if (!again) {
220078064Sume						trace(1, "route: %s/%d: "
220178064Sume						    "already registered (%s)\n",
220278064Sume						    inet6_n2p(&np->rip6_dest), np->rip6_plen,
220378064Sume						    ifcp->ifc_name);
220478064Sume					}
2205119042Sume					goto next;
220678064Sume				}
2207119042Sume
2208243232Shrs				TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
2209119042Sume				delroute(&rrt->rrt_info, &rrt->rrt_gw);
221078064Sume			}
221155163Sshin			/* Attach the route to the list */
221262607Sitojun			trace(1, "route: %s/%d: register route (%s)\n",
221362607Sitojun			    inet6_n2p(&np->rip6_dest), np->rip6_plen,
221462607Sitojun			    ifcp->ifc_name);
2215243232Shrs			TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
221678064Sume			addroute(rrt, &rrt->rrt_gw, ifcp);
2217119042Sume			rrt = NULL;
221878064Sume			sendrequest(ifcp);
221978064Sume			ripsend(ifcp, &ifcp->ifc_ripsin, 0);
222078064Sume			need_trigger = 1;
222155163Sshin		} else {
2222243232Shrs			TAILQ_FOREACH(loop_rrt, &riprt_head, rrt_next) {
222378064Sume				if (loop_rrt->rrt_index == ifcp->ifc_index) {
222478064Sume					t_lifetime = time(NULL) - RIP_LIFETIME;
222578064Sume					if (loop_rrt->rrt_t == 0 || loop_rrt->rrt_t > t_lifetime) {
222678064Sume						loop_rrt->rrt_t = t_lifetime;
222778064Sume						loop_rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
222878064Sume						loop_rrt->rrt_rflags |= RRTF_CHANGED;
222978064Sume						need_trigger = 1;
223078064Sume					}
223178064Sume				}
223255163Sshin			}
223378064Sume                }
2234119042Sume	next:
2235119042Sume		if (rrt)
2236119042Sume			free(rrt);
223762607Sitojun	}
223878064Sume	return need_trigger;
223962607Sitojun}
224055163Sshin
224162607Sitojun/*
224262607Sitojun * there are couple of p2p interface routing models.  "behavior" lets
224362607Sitojun * you pick one.  it looks that gated behavior fits best with BSDs,
2244119035Sume * since BSD kernels do not look at prefix length on p2p interfaces.
224562607Sitojun */
224662607Sitojunvoid
2247243232Shrsifrt_p2p(struct ifc *ifcp, int again)
224862607Sitojun{
2249243232Shrs	struct ifac *ifac;
2250243232Shrs	struct riprt *rrt, *orrt;
225162607Sitojun	struct netinfo6 *np;
225262607Sitojun	struct in6_addr addr, dest;
225362607Sitojun	int advert, ignore, i;
225462607Sitojun#define P2PADVERT_NETWORK	1
225562607Sitojun#define P2PADVERT_ADDR		2
225662607Sitojun#define P2PADVERT_DEST		4
225762607Sitojun#define P2PADVERT_MAX		4
225862607Sitojun	const enum { CISCO, GATED, ROUTE6D } behavior = GATED;
225978064Sume	const char *category = "";
226062607Sitojun	const char *noadv;
226162607Sitojun
2262243232Shrs	TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2263243232Shrs		addr = ifac->ifac_addr;
2264243232Shrs		dest = ifac->ifac_raddr;
2265243232Shrs		applyplen(&addr, ifac->ifac_plen);
2266243232Shrs		applyplen(&dest, ifac->ifac_plen);
226762607Sitojun		advert = ignore = 0;
226862607Sitojun		switch (behavior) {
226962607Sitojun		case CISCO:
227062607Sitojun			/*
227162607Sitojun			 * honor addr/plen, just like normal shared medium
227262607Sitojun			 * interface.  this may cause trouble if you reuse
227362607Sitojun			 * addr/plen on other interfaces.
227462607Sitojun			 *
227562607Sitojun			 * advertise addr/plen.
227662607Sitojun			 */
227762607Sitojun			advert |= P2PADVERT_NETWORK;
227862607Sitojun			break;
227962607Sitojun		case GATED:
228062607Sitojun			/*
228162607Sitojun			 * prefixlen on p2p interface is meaningless.
228262607Sitojun			 * advertise addr/128 and dest/128.
228362607Sitojun			 *
228462607Sitojun			 * do not install network route to route6d routing
228562607Sitojun			 * table (if we do, it would prevent route installation
228662607Sitojun			 * for other p2p interface that shares addr/plen).
228778064Sume			 *
228878064Sume			 * XXX what should we do if dest is ::?  it will not
228978064Sume			 * get announced anyways (see following filter),
229078064Sume			 * but we need to think.
229162607Sitojun			 */
229262607Sitojun			advert |= P2PADVERT_ADDR;
229362607Sitojun			advert |= P2PADVERT_DEST;
229462607Sitojun			ignore |= P2PADVERT_NETWORK;
229562607Sitojun			break;
229662607Sitojun		case ROUTE6D:
229762607Sitojun			/*
229878064Sume			 * just for testing.  actually the code is redundant
229978064Sume			 * given the current p2p interface address assignment
230078064Sume			 * rule for kame kernel.
230178064Sume			 *
230278064Sume			 * intent:
230378064Sume			 *	A/n -> announce A/n
230478064Sume			 *	A B/n, A and B share prefix -> A/n (= B/n)
230578064Sume			 *	A B/n, do not share prefix -> A/128 and B/128
230678064Sume			 * actually, A/64 and A B/128 are the only cases
230778064Sume			 * permitted by the kernel:
230878064Sume			 *	A/64 -> A/64
230978064Sume			 *	A B/128 -> A/128 and B/128
231062607Sitojun			 */
2311243232Shrs			if (!IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_raddr)) {
231278064Sume				if (IN6_ARE_ADDR_EQUAL(&addr, &dest))
231378064Sume					advert |= P2PADVERT_NETWORK;
231478064Sume				else {
231578064Sume					advert |= P2PADVERT_ADDR;
231678064Sume					advert |= P2PADVERT_DEST;
231778064Sume					ignore |= P2PADVERT_NETWORK;
231878064Sume				}
231978064Sume			} else
232062607Sitojun				advert |= P2PADVERT_NETWORK;
232162607Sitojun			break;
232262607Sitojun		}
232362607Sitojun
232462607Sitojun		for (i = 1; i <= P2PADVERT_MAX; i *= 2) {
232562607Sitojun			if ((ignore & i) != 0)
232662607Sitojun				continue;
232778064Sume			if ((rrt = MALLOC(struct riprt)) == NULL) {
232855163Sshin				fatal("malloc: struct riprt");
232978064Sume				/*NOTREACHED*/
233078064Sume			}
233162607Sitojun			memset(rrt, 0, sizeof(*rrt));
233255163Sshin			rrt->rrt_same = NULL;
233355163Sshin			rrt->rrt_index = ifcp->ifc_index;
233462607Sitojun			rrt->rrt_t = 0;	/* don't age */
233562607Sitojun			switch (i) {
233662607Sitojun			case P2PADVERT_NETWORK:
2337243232Shrs				rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2338243232Shrs				rrt->rrt_info.rip6_plen = ifac->ifac_plen;
233962607Sitojun				applyplen(&rrt->rrt_info.rip6_dest,
2340243232Shrs				    ifac->ifac_plen);
234162607Sitojun				category = "network";
234262607Sitojun				break;
234362607Sitojun			case P2PADVERT_ADDR:
2344243232Shrs				rrt->rrt_info.rip6_dest = ifac->ifac_addr;
234562607Sitojun				rrt->rrt_info.rip6_plen = 128;
234678064Sume				rrt->rrt_gw = in6addr_loopback;
234762607Sitojun				category = "addr";
234862607Sitojun				break;
234962607Sitojun			case P2PADVERT_DEST:
2350243232Shrs				rrt->rrt_info.rip6_dest = ifac->ifac_raddr;
235162607Sitojun				rrt->rrt_info.rip6_plen = 128;
2352243232Shrs				rrt->rrt_gw = ifac->ifac_addr;
235362607Sitojun				category = "dest";
235462607Sitojun				break;
235562607Sitojun			}
235662607Sitojun			if (IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_info.rip6_dest) ||
235762607Sitojun			    IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_info.rip6_dest)) {
235862607Sitojun#if 0
235962607Sitojun				trace(1, "route: %s: skip unspec/linklocal "
236062607Sitojun				    "(%s on %s)\n", category, ifcp->ifc_name);
236162607Sitojun#endif
236262607Sitojun				free(rrt);
236362607Sitojun				continue;
236462607Sitojun			}
236562607Sitojun			if ((advert & i) == 0) {
236662607Sitojun				rrt->rrt_rflags |= RRTF_NOADVERTISE;
236762607Sitojun				noadv = ", NO-ADV";
236862607Sitojun			} else
236962607Sitojun				noadv = "";
237055163Sshin			rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
237162607Sitojun			rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
237255163Sshin			np = &rrt->rrt_info;
2373243232Shrs			orrt = rtsearch(np);
237478064Sume			if (!orrt) {
237555163Sshin				/* Attach the route to the list */
237662607Sitojun				trace(1, "route: %s/%d: register route "
237762607Sitojun				    "(%s on %s%s)\n",
237862607Sitojun				    inet6_n2p(&np->rip6_dest), np->rip6_plen,
237962607Sitojun				    category, ifcp->ifc_name, noadv);
2380243232Shrs				TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
238178064Sume			} else if (rrt->rrt_index != orrt->rrt_index ||
238278064Sume			    rrt->rrt_info.rip6_metric != orrt->rrt_info.rip6_metric) {
2383243232Shrs				/* replace route */
2384243232Shrs				TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2385243232Shrs				TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
238678064Sume				free(orrt);
238778064Sume
238878064Sume				trace(1, "route: %s/%d: update (%s on %s%s)\n",
238978064Sume				    inet6_n2p(&np->rip6_dest), np->rip6_plen,
239078064Sume				    category, ifcp->ifc_name, noadv);
239155163Sshin			} else {
239255163Sshin				/* Already found */
239355163Sshin				if (!again) {
239462607Sitojun					trace(1, "route: %s/%d: "
239562607Sitojun					    "already registered (%s on %s%s)\n",
239662607Sitojun					    inet6_n2p(&np->rip6_dest),
239762607Sitojun					    np->rip6_plen, category,
239862607Sitojun					    ifcp->ifc_name, noadv);
239955163Sshin				}
240055163Sshin				free(rrt);
240155163Sshin			}
240255163Sshin		}
240355163Sshin	}
240462607Sitojun#undef P2PADVERT_NETWORK
240562607Sitojun#undef P2PADVERT_ADDR
240662607Sitojun#undef P2PADVERT_DEST
240762607Sitojun#undef P2PADVERT_MAX
240855163Sshin}
240955163Sshin
241055163Sshinint
2411243232Shrsgetifmtu(int ifindex)
241255163Sshin{
241355163Sshin	int	mib[6];
241455163Sshin	char	*buf;
241555163Sshin	size_t	msize;
241655163Sshin	struct	if_msghdr *ifm;
241755163Sshin	int	mtu;
241855163Sshin
241955163Sshin	mib[0] = CTL_NET;
242055163Sshin	mib[1] = PF_ROUTE;
242155163Sshin	mib[2] = 0;
242255163Sshin	mib[3] = AF_INET6;
242355163Sshin	mib[4] = NET_RT_IFLIST;
242455163Sshin	mib[5] = ifindex;
242578064Sume	if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
242655163Sshin		fatal("sysctl estimate NET_RT_IFLIST");
242778064Sume		/*NOTREACHED*/
242878064Sume	}
242978064Sume	if ((buf = malloc(msize)) == NULL) {
243055163Sshin		fatal("malloc");
243178064Sume		/*NOTREACHED*/
243278064Sume	}
243378064Sume	if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
243455163Sshin		fatal("sysctl NET_RT_IFLIST");
243578064Sume		/*NOTREACHED*/
243678064Sume	}
243755163Sshin	ifm = (struct if_msghdr *)buf;
243855163Sshin	mtu = ifm->ifm_data.ifi_mtu;
243978064Sume	if (ifindex != ifm->ifm_index) {
244055163Sshin		fatal("ifindex does not match with ifm_index");
244178064Sume		/*NOTREACHED*/
244278064Sume	}
244355163Sshin	free(buf);
244455163Sshin	return mtu;
244555163Sshin}
244655163Sshin
244755163Sshinconst char *
2448243232Shrsrttypes(struct rt_msghdr *rtm)
244955163Sshin{
245062607Sitojun#define	RTTYPE(s, f) \
245162607Sitojundo { \
245262607Sitojun	if (rtm->rtm_type == (f)) \
245362607Sitojun		return (s); \
245462607Sitojun} while (0)
245555163Sshin	RTTYPE("ADD", RTM_ADD);
245655163Sshin	RTTYPE("DELETE", RTM_DELETE);
245755163Sshin	RTTYPE("CHANGE", RTM_CHANGE);
245855163Sshin	RTTYPE("GET", RTM_GET);
245955163Sshin	RTTYPE("LOSING", RTM_LOSING);
246055163Sshin	RTTYPE("REDIRECT", RTM_REDIRECT);
246155163Sshin	RTTYPE("MISS", RTM_MISS);
246255163Sshin	RTTYPE("LOCK", RTM_LOCK);
246355163Sshin	RTTYPE("NEWADDR", RTM_NEWADDR);
246455163Sshin	RTTYPE("DELADDR", RTM_DELADDR);
246555163Sshin	RTTYPE("IFINFO", RTM_IFINFO);
246678064Sume#ifdef RTM_OIFINFO
246778064Sume	RTTYPE("OIFINFO", RTM_OIFINFO);
246878064Sume#endif
246978064Sume#ifdef RTM_IFANNOUNCE
247078064Sume	RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE);
247178064Sume#endif
247278064Sume#ifdef RTM_NEWMADDR
247378064Sume	RTTYPE("NEWMADDR", RTM_NEWMADDR);
247478064Sume#endif
247578064Sume#ifdef RTM_DELMADDR
247678064Sume	RTTYPE("DELMADDR", RTM_DELMADDR);
247778064Sume#endif
247855163Sshin#undef RTTYPE
247955163Sshin	return NULL;
248055163Sshin}
248155163Sshin
248255163Sshinconst char *
2483243232Shrsrtflags(struct rt_msghdr *rtm)
248455163Sshin{
248555163Sshin	static char buf[BUFSIZ];
248655163Sshin
248778064Sume	/*
248878064Sume	 * letter conflict should be okay.  painful when *BSD diverges...
248978064Sume	 */
249078064Sume	strlcpy(buf, "", sizeof(buf));
249162607Sitojun#define	RTFLAG(s, f) \
249262607Sitojundo { \
249362607Sitojun	if (rtm->rtm_flags & (f)) \
249478064Sume		strlcat(buf, (s), sizeof(buf)); \
249562607Sitojun} while (0)
249655163Sshin	RTFLAG("U", RTF_UP);
249755163Sshin	RTFLAG("G", RTF_GATEWAY);
249855163Sshin	RTFLAG("H", RTF_HOST);
249955163Sshin	RTFLAG("R", RTF_REJECT);
250055163Sshin	RTFLAG("D", RTF_DYNAMIC);
250155163Sshin	RTFLAG("M", RTF_MODIFIED);
250255163Sshin	RTFLAG("d", RTF_DONE);
250355163Sshin#ifdef	RTF_MASK
250455163Sshin	RTFLAG("m", RTF_MASK);
250555163Sshin#endif
250678064Sume#ifdef RTF_CLONED
250778064Sume	RTFLAG("c", RTF_CLONED);
250878064Sume#endif
250955163Sshin	RTFLAG("X", RTF_XRESOLVE);
2510186119Sqingli#ifdef RTF_LLINFO
251155163Sshin	RTFLAG("L", RTF_LLINFO);
2512186119Sqingli#endif
251355163Sshin	RTFLAG("S", RTF_STATIC);
251455163Sshin	RTFLAG("B", RTF_BLACKHOLE);
251578064Sume#ifdef RTF_PROTO3
251678064Sume	RTFLAG("3", RTF_PROTO3);
251778064Sume#endif
251855163Sshin	RTFLAG("2", RTF_PROTO2);
251955163Sshin	RTFLAG("1", RTF_PROTO1);
252078064Sume#ifdef RTF_BROADCAST
252178064Sume	RTFLAG("b", RTF_BROADCAST);
252278064Sume#endif
252378064Sume#ifdef RTF_DEFAULT
252478064Sume	RTFLAG("d", RTF_DEFAULT);
252578064Sume#endif
252678064Sume#ifdef RTF_ISAROUTER
252778064Sume	RTFLAG("r", RTF_ISAROUTER);
252878064Sume#endif
252978064Sume#ifdef RTF_TUNNEL
253078064Sume	RTFLAG("T", RTF_TUNNEL);
253178064Sume#endif
253278064Sume#ifdef RTF_AUTH
253378064Sume	RTFLAG("A", RTF_AUTH);
253478064Sume#endif
253578064Sume#ifdef RTF_CRYPT
253678064Sume	RTFLAG("E", RTF_CRYPT);
253778064Sume#endif
253855163Sshin#undef RTFLAG
253955163Sshin	return buf;
254055163Sshin}
254155163Sshin
254255163Sshinconst char *
2543243232Shrsifflags(int flags)
254455163Sshin{
254555163Sshin	static char buf[BUFSIZ];
254655163Sshin
254778064Sume	strlcpy(buf, "", sizeof(buf));
254862607Sitojun#define	IFFLAG(s, f) \
254962607Sitojundo { \
2550119040Sume	if (flags & (f)) { \
255162607Sitojun		if (buf[0]) \
255278064Sume			strlcat(buf, ",", sizeof(buf)); \
2553119040Sume		strlcat(buf, (s), sizeof(buf)); \
255462607Sitojun	} \
255562607Sitojun} while (0)
255655163Sshin	IFFLAG("UP", IFF_UP);
255755163Sshin	IFFLAG("BROADCAST", IFF_BROADCAST);
255855163Sshin	IFFLAG("DEBUG", IFF_DEBUG);
255955163Sshin	IFFLAG("LOOPBACK", IFF_LOOPBACK);
256055163Sshin	IFFLAG("POINTOPOINT", IFF_POINTOPOINT);
256155163Sshin#ifdef IFF_NOTRAILERS
256255163Sshin	IFFLAG("NOTRAILERS", IFF_NOTRAILERS);
256355163Sshin#endif
256455163Sshin	IFFLAG("RUNNING", IFF_RUNNING);
256555163Sshin	IFFLAG("NOARP", IFF_NOARP);
256655163Sshin	IFFLAG("PROMISC", IFF_PROMISC);
256755163Sshin	IFFLAG("ALLMULTI", IFF_ALLMULTI);
256855163Sshin	IFFLAG("OACTIVE", IFF_OACTIVE);
256955163Sshin	IFFLAG("SIMPLEX", IFF_SIMPLEX);
257055163Sshin	IFFLAG("LINK0", IFF_LINK0);
257155163Sshin	IFFLAG("LINK1", IFF_LINK1);
257255163Sshin	IFFLAG("LINK2", IFF_LINK2);
257355163Sshin	IFFLAG("MULTICAST", IFF_MULTICAST);
257455163Sshin#undef IFFLAG
257555163Sshin	return buf;
257655163Sshin}
257755163Sshin
257855163Sshinvoid
2579243232Shrskrtread(int again)
258055163Sshin{
258155163Sshin	int mib[6];
258255163Sshin	size_t msize;
258355163Sshin	char *buf, *p, *lim;
258455163Sshin	struct rt_msghdr *rtm;
258555163Sshin	int retry;
258655163Sshin	const char *errmsg;
258755163Sshin
258855163Sshin	retry = 0;
258955163Sshin	buf = NULL;
259055163Sshin	mib[0] = CTL_NET;
259155163Sshin	mib[1] = PF_ROUTE;
259255163Sshin	mib[2] = 0;
259355163Sshin	mib[3] = AF_INET6;	/* Address family */
259455163Sshin	mib[4] = NET_RT_DUMP;	/* Dump the kernel routing table */
259555163Sshin	mib[5] = 0;		/* No flags */
259655163Sshin	do {
2597243232Shrs		if (retry)
2598243232Shrs			sleep(1);
259955163Sshin		retry++;
260055163Sshin		errmsg = NULL;
2601299491Scem		if (buf) {
260255163Sshin			free(buf);
2603299491Scem			buf = NULL;
2604299491Scem		}
260555163Sshin		if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
260655163Sshin			errmsg = "sysctl estimate";
260755163Sshin			continue;
260855163Sshin		}
260955163Sshin		if ((buf = malloc(msize)) == NULL) {
261055163Sshin			errmsg = "malloc";
261155163Sshin			continue;
261255163Sshin		}
261355163Sshin		if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
261455163Sshin			errmsg = "sysctl NET_RT_DUMP";
261555163Sshin			continue;
261655163Sshin		}
2617243233Shrs	} while (retry < RT_DUMP_MAXRETRY && errmsg != NULL);
261878064Sume	if (errmsg) {
261969279Sume		fatal("%s (with %d retries, msize=%lu)", errmsg, retry,
262069279Sume		    (u_long)msize);
262178064Sume		/*NOTREACHED*/
262278064Sume	} else if (1 < retry)
262355163Sshin		syslog(LOG_INFO, "NET_RT_DUMP %d retires", retry);
262455163Sshin
262555163Sshin	lim = buf + msize;
262655163Sshin	for (p = buf; p < lim; p += rtm->rtm_msglen) {
262755163Sshin		rtm = (struct rt_msghdr *)p;
262855163Sshin		rt_entry(rtm, again);
262955163Sshin	}
263055163Sshin	free(buf);
263155163Sshin}
263255163Sshin
263355163Sshinvoid
2634243232Shrsrt_entry(struct rt_msghdr *rtm, int again)
263555163Sshin{
263655163Sshin	struct	sockaddr_in6 *sin6_dst, *sin6_gw, *sin6_mask;
263755163Sshin	struct	sockaddr_in6 *sin6_genmask, *sin6_ifp;
263855163Sshin	char	*rtmp, *ifname = NULL;
263978064Sume	struct	riprt *rrt, *orrt;
264055163Sshin	struct	netinfo6 *np;
2641243232Shrs	int ifindex;
264255163Sshin
264355163Sshin	sin6_dst = sin6_gw = sin6_mask = sin6_genmask = sin6_ifp = 0;
264455163Sshin	if ((rtm->rtm_flags & RTF_UP) == 0 || rtm->rtm_flags &
2645186119Sqingli		(RTF_XRESOLVE|RTF_BLACKHOLE)) {
264655163Sshin		return;		/* not interested in the link route */
264762607Sitojun	}
264869279Sume	/* do not look at cloned routes */
264969279Sume#ifdef RTF_WASCLONED
265069279Sume	if (rtm->rtm_flags & RTF_WASCLONED)
265169279Sume		return;
265269279Sume#endif
265369279Sume#ifdef RTF_CLONED
265469279Sume	if (rtm->rtm_flags & RTF_CLONED)
265569279Sume		return;
265669279Sume#endif
2657243233Shrs	/* XXX: Ignore connected routes. */
2658243233Shrs	if (!(rtm->rtm_flags & (RTF_GATEWAY|RTF_HOST|RTF_STATIC)))
2659243233Shrs		return;
266069279Sume	/*
266169279Sume	 * do not look at dynamic routes.
266269279Sume	 * netbsd/openbsd cloned routes have UGHD.
266369279Sume	 */
266469279Sume	if (rtm->rtm_flags & RTF_DYNAMIC)
266569279Sume		return;
266655163Sshin	rtmp = (char *)(rtm + 1);
266755163Sshin	/* Destination */
266855163Sshin	if ((rtm->rtm_addrs & RTA_DST) == 0)
266955163Sshin		return;		/* ignore routes without destination address */
267055163Sshin	sin6_dst = (struct sockaddr_in6 *)rtmp;
267164631Sitojun	rtmp += ROUNDUP(sin6_dst->sin6_len);
267255163Sshin	if (rtm->rtm_addrs & RTA_GATEWAY) {
267355163Sshin		sin6_gw = (struct sockaddr_in6 *)rtmp;
267455163Sshin		rtmp += ROUNDUP(sin6_gw->sin6_len);
267555163Sshin	}
267655163Sshin	if (rtm->rtm_addrs & RTA_NETMASK) {
267755163Sshin		sin6_mask = (struct sockaddr_in6 *)rtmp;
267855163Sshin		rtmp += ROUNDUP(sin6_mask->sin6_len);
267955163Sshin	}
268055163Sshin	if (rtm->rtm_addrs & RTA_GENMASK) {
268155163Sshin		sin6_genmask = (struct sockaddr_in6 *)rtmp;
268255163Sshin		rtmp += ROUNDUP(sin6_genmask->sin6_len);
268355163Sshin	}
268455163Sshin	if (rtm->rtm_addrs & RTA_IFP) {
268555163Sshin		sin6_ifp = (struct sockaddr_in6 *)rtmp;
268655163Sshin		rtmp += ROUNDUP(sin6_ifp->sin6_len);
268755163Sshin	}
268855163Sshin
268955163Sshin	/* Destination */
269055163Sshin	if (sin6_dst->sin6_family != AF_INET6)
269155163Sshin		return;
269255163Sshin	if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst->sin6_addr))
269355163Sshin		return;		/* Link-local */
269455163Sshin	if (IN6_ARE_ADDR_EQUAL(&sin6_dst->sin6_addr, &in6addr_loopback))
269555163Sshin		return;		/* Loopback */
269655163Sshin	if (IN6_IS_ADDR_MULTICAST(&sin6_dst->sin6_addr))
269755163Sshin		return;
269855163Sshin
269978064Sume	if ((rrt = MALLOC(struct riprt)) == NULL) {
270055163Sshin		fatal("malloc: struct riprt");
270178064Sume		/*NOTREACHED*/
270278064Sume	}
270362607Sitojun	memset(rrt, 0, sizeof(*rrt));
270455163Sshin	np = &rrt->rrt_info;
270555163Sshin	rrt->rrt_same = NULL;
270655163Sshin	rrt->rrt_t = time(NULL);
270755163Sshin	if (aflag == 0 && (rtm->rtm_flags & RTF_STATIC))
270855163Sshin		rrt->rrt_t = 0;	/* Don't age static routes */
2709243233Shrs	if (rtm->rtm_flags & Pflag)
2710243233Shrs		rrt->rrt_t = 0;	/* Don't age PROTO[123] routes */
2711122677Sume	if ((rtm->rtm_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST)
2712122677Sume		rrt->rrt_t = 0;	/* Don't age non-gateway host routes */
271355163Sshin	np->rip6_tag = 0;
271455163Sshin	np->rip6_metric = rtm->rtm_rmx.rmx_hopcount;
271555163Sshin	if (np->rip6_metric < 1)
271655163Sshin		np->rip6_metric = 1;
271755163Sshin	rrt->rrt_flags = rtm->rtm_flags;
271855163Sshin	np->rip6_dest = sin6_dst->sin6_addr;
271955163Sshin
272055163Sshin	/* Mask or plen */
272155163Sshin	if (rtm->rtm_flags & RTF_HOST)
272255163Sshin		np->rip6_plen = 128;	/* Host route */
272378064Sume	else if (sin6_mask)
272478064Sume		np->rip6_plen = sin6mask2len(sin6_mask);
272578064Sume	else
272655163Sshin		np->rip6_plen = 0;
272755163Sshin
2728243232Shrs	orrt = rtsearch(np);
272978064Sume	if (orrt && orrt->rrt_info.rip6_metric != HOPCNT_INFINITY6) {
273055163Sshin		/* Already found */
273155163Sshin		if (!again) {
273255163Sshin			trace(1, "route: %s/%d flags %s: already registered\n",
273355163Sshin				inet6_n2p(&np->rip6_dest), np->rip6_plen,
273455163Sshin				rtflags(rtm));
273555163Sshin		}
273655163Sshin		free(rrt);
273755163Sshin		return;
273855163Sshin	}
273955163Sshin	/* Gateway */
274055163Sshin	if (!sin6_gw)
274155163Sshin		memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
274255163Sshin	else {
274355163Sshin		if (sin6_gw->sin6_family == AF_INET6)
274455163Sshin			rrt->rrt_gw = sin6_gw->sin6_addr;
274555163Sshin		else if (sin6_gw->sin6_family == AF_LINK) {
274655163Sshin			/* XXX in case ppp link? */
274755163Sshin			rrt->rrt_gw = in6addr_loopback;
274855163Sshin		} else
274955163Sshin			memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
275055163Sshin	}
275155163Sshin	trace(1, "route: %s/%d flags %s",
275255163Sshin		inet6_n2p(&np->rip6_dest), np->rip6_plen, rtflags(rtm));
275355163Sshin	trace(1, " gw %s", inet6_n2p(&rrt->rrt_gw));
275455163Sshin
275555163Sshin	/* Interface */
2756243232Shrs	ifindex = rtm->rtm_index;
2757243232Shrs	if ((unsigned int)ifindex < nindex2ifc && index2ifc[ifindex])
2758243232Shrs		ifname = index2ifc[ifindex]->ifc_name;
275958070Sshin	else {
276058070Sshin		trace(1, " not configured\n");
276162607Sitojun		free(rrt);
276258070Sshin		return;
276358070Sshin	}
2764243232Shrs	trace(1, " if %s sock %d", ifname, ifindex);
2765243232Shrs	rrt->rrt_index = ifindex;
276655163Sshin
276762607Sitojun	trace(1, "\n");
276862607Sitojun
276955163Sshin	/* Check gateway */
277055163Sshin	if (!IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_gw) &&
2771122677Sume	    !IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw) &&
2772122677Sume	    (rrt->rrt_flags & RTF_LOCAL) == 0) {
277355163Sshin		trace(0, "***** Gateway %s is not a link-local address.\n",
277455163Sshin			inet6_n2p(&rrt->rrt_gw));
277555163Sshin		trace(0, "*****     dest(%s) if(%s) -- Not optimized.\n",
277662607Sitojun			inet6_n2p(&rrt->rrt_info.rip6_dest), ifname);
277762607Sitojun		rrt->rrt_rflags |= RRTF_NH_NOT_LLADDR;
277855163Sshin	}
277955163Sshin
278055163Sshin	/* Put it to the route list */
278178064Sume	if (orrt && orrt->rrt_info.rip6_metric == HOPCNT_INFINITY6) {
278278064Sume		/* replace route list */
2783243232Shrs		TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2784243232Shrs		TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
2785243232Shrs
278678064Sume		trace(1, "route: %s/%d flags %s: replace new route\n",
278778064Sume		    inet6_n2p(&np->rip6_dest), np->rip6_plen,
278878064Sume		    rtflags(rtm));
2789243232Shrs		free(orrt);
2790243232Shrs	} else
2791243232Shrs		TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
279255163Sshin}
279355163Sshin
279455163Sshinint
2795243232Shrsaddroute(struct riprt *rrt,
2796243232Shrs	const struct in6_addr *gw,
2797243232Shrs	struct ifc *ifcp)
279855163Sshin{
279955163Sshin	struct	netinfo6 *np;
280055163Sshin	u_char	buf[BUFSIZ], buf1[BUFSIZ], buf2[BUFSIZ];
280155163Sshin	struct	rt_msghdr	*rtm;
2802119031Sume	struct	sockaddr_in6	*sin6;
280355163Sshin	int	len;
280455163Sshin
280555163Sshin	np = &rrt->rrt_info;
280678064Sume	inet_ntop(AF_INET6, (const void *)gw, (char *)buf1, sizeof(buf1));
280755163Sshin	inet_ntop(AF_INET6, (void *)&ifcp->ifc_mylladdr, (char *)buf2, sizeof(buf2));
280855163Sshin	tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n",
280955163Sshin		inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
281055163Sshin		np->rip6_metric - 1, buf2);
281155163Sshin	if (rtlog)
281255163Sshin		fprintf(rtlog, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(),
281355163Sshin			inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
281455163Sshin			np->rip6_metric - 1, buf2);
281555163Sshin	if (nflag)
281655163Sshin		return 0;
281755163Sshin
281855163Sshin	memset(buf, 0, sizeof(buf));
281955163Sshin	rtm = (struct rt_msghdr *)buf;
282055163Sshin	rtm->rtm_type = RTM_ADD;
282155163Sshin	rtm->rtm_version = RTM_VERSION;
282255163Sshin	rtm->rtm_seq = ++seq;
282355163Sshin	rtm->rtm_pid = pid;
282462607Sitojun	rtm->rtm_flags = rrt->rrt_flags;
2825243233Shrs	rtm->rtm_flags |= Qflag;
282655163Sshin	rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
282755163Sshin	rtm->rtm_rmx.rmx_hopcount = np->rip6_metric - 1;
282855163Sshin	rtm->rtm_inits = RTV_HOPCOUNT;
2829119031Sume	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
283055163Sshin	/* Destination */
2831119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2832119031Sume	sin6->sin6_family = AF_INET6;
2833119031Sume	sin6->sin6_addr = np->rip6_dest;
2834119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
283555163Sshin	/* Gateway */
2836119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2837119031Sume	sin6->sin6_family = AF_INET6;
2838119031Sume	sin6->sin6_addr = *gw;
2839270234Shrs	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
2840270234Shrs		sin6->sin6_scope_id = ifcp->ifc_index;
2841119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
284255163Sshin	/* Netmask */
2843119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2844119031Sume	sin6->sin6_family = AF_INET6;
2845119031Sume	sin6->sin6_addr = *(plen2mask(np->rip6_plen));
2846119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
284755163Sshin
2848119031Sume	len = (char *)sin6 - (char *)buf;
284955163Sshin	rtm->rtm_msglen = len;
285055163Sshin	if (write(rtsock, buf, len) > 0)
285155163Sshin		return 0;
285255163Sshin
285355163Sshin	if (errno == EEXIST) {
285455163Sshin		trace(0, "ADD: Route already exists %s/%d gw %s\n",
2855119035Sume		    inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
285655163Sshin		if (rtlog)
285755163Sshin			fprintf(rtlog, "ADD: Route already exists %s/%d gw %s\n",
2858119035Sume			    inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
285955163Sshin	} else {
286055163Sshin		trace(0, "Can not write to rtsock (addroute): %s\n",
2861119035Sume		    strerror(errno));
286255163Sshin		if (rtlog)
286355163Sshin			fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2864119035Sume			    strerror(errno));
286555163Sshin	}
286655163Sshin	return -1;
286755163Sshin}
286855163Sshin
286955163Sshinint
2870243232Shrsdelroute(struct netinfo6 *np, struct in6_addr *gw)
287155163Sshin{
287255163Sshin	u_char	buf[BUFSIZ], buf2[BUFSIZ];
287355163Sshin	struct	rt_msghdr	*rtm;
2874119031Sume	struct	sockaddr_in6	*sin6;
287555163Sshin	int	len;
287655163Sshin
287755163Sshin	inet_ntop(AF_INET6, (void *)gw, (char *)buf2, sizeof(buf2));
287855163Sshin	tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np->rip6_dest),
287955163Sshin		np->rip6_plen, buf2);
288055163Sshin	if (rtlog)
288155163Sshin		fprintf(rtlog, "%s: DEL: %s/%d gw %s\n",
288255163Sshin			hms(), inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
288355163Sshin	if (nflag)
288455163Sshin		return 0;
288555163Sshin
288655163Sshin	memset(buf, 0, sizeof(buf));
288755163Sshin	rtm = (struct rt_msghdr *)buf;
288855163Sshin	rtm->rtm_type = RTM_DELETE;
288955163Sshin	rtm->rtm_version = RTM_VERSION;
289055163Sshin	rtm->rtm_seq = ++seq;
289155163Sshin	rtm->rtm_pid = pid;
289255163Sshin	rtm->rtm_flags = RTF_UP | RTF_GATEWAY;
2893243233Shrs	rtm->rtm_flags |= Qflag;
289478064Sume	if (np->rip6_plen == sizeof(struct in6_addr) * 8)
289578064Sume		rtm->rtm_flags |= RTF_HOST;
289655163Sshin	rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2897119031Sume	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
289855163Sshin	/* Destination */
2899119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2900119031Sume	sin6->sin6_family = AF_INET6;
2901119031Sume	sin6->sin6_addr = np->rip6_dest;
2902119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
290355163Sshin	/* Gateway */
2904119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2905119031Sume	sin6->sin6_family = AF_INET6;
2906119031Sume	sin6->sin6_addr = *gw;
2907119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
290855163Sshin	/* Netmask */
2909119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2910119031Sume	sin6->sin6_family = AF_INET6;
2911119031Sume	sin6->sin6_addr = *(plen2mask(np->rip6_plen));
2912119031Sume	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
291355163Sshin
2914119031Sume	len = (char *)sin6 - (char *)buf;
291555163Sshin	rtm->rtm_msglen = len;
291655163Sshin	if (write(rtsock, buf, len) >= 0)
291755163Sshin		return 0;
291855163Sshin
291955163Sshin	if (errno == ESRCH) {
292055163Sshin		trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n",
2921119035Sume		    inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
292255163Sshin		if (rtlog)
292355163Sshin			fprintf(rtlog, "RTDEL: Route does not exist: %s/%d gw %s\n",
2924119035Sume			    inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
292555163Sshin	} else {
292655163Sshin		trace(0, "Can not write to rtsock (delroute): %s\n",
2927119035Sume		    strerror(errno));
292855163Sshin		if (rtlog)
292955163Sshin			fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2930119035Sume			    strerror(errno));
293155163Sshin	}
293255163Sshin	return -1;
293355163Sshin}
293455163Sshin
293555163Sshinstruct in6_addr *
2936243232Shrsgetroute(struct netinfo6 *np, struct in6_addr *gw)
293755163Sshin{
293855163Sshin	u_char buf[BUFSIZ];
2939119085Sume	int myseq;
294055163Sshin	int len;
294155163Sshin	struct rt_msghdr *rtm;
2942119031Sume	struct sockaddr_in6 *sin6;
294355163Sshin
294455163Sshin	rtm = (struct rt_msghdr *)buf;
294555163Sshin	len = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in6);
294655163Sshin	memset(rtm, 0, len);
294755163Sshin	rtm->rtm_type = RTM_GET;
294855163Sshin	rtm->rtm_version = RTM_VERSION;
294955163Sshin	myseq = ++seq;
295055163Sshin	rtm->rtm_seq = myseq;
295155163Sshin	rtm->rtm_addrs = RTA_DST;
295255163Sshin	rtm->rtm_msglen = len;
2953119031Sume	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
2954119031Sume	sin6->sin6_len = sizeof(struct sockaddr_in6);
2955119031Sume	sin6->sin6_family = AF_INET6;
2956119031Sume	sin6->sin6_addr = np->rip6_dest;
295755163Sshin	if (write(rtsock, buf, len) < 0) {
295855163Sshin		if (errno == ESRCH)	/* No such route found */
295955163Sshin			return NULL;
296055163Sshin		perror("write to rtsock");
296178064Sume		exit(1);
296255163Sshin	}
296355163Sshin	do {
296455163Sshin		if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
296555163Sshin			perror("read from rtsock");
296678064Sume			exit(1);
296755163Sshin		}
296855163Sshin		rtm = (struct rt_msghdr *)buf;
296955163Sshin	} while (rtm->rtm_seq != myseq || rtm->rtm_pid != pid);
2970119031Sume	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
297155163Sshin	if (rtm->rtm_addrs & RTA_DST) {
2972119031Sume		sin6 = (struct sockaddr_in6 *)
2973119031Sume			((char *)sin6 + ROUNDUP(sin6->sin6_len));
297455163Sshin	}
297555163Sshin	if (rtm->rtm_addrs & RTA_GATEWAY) {
2976119031Sume		*gw = sin6->sin6_addr;
297755163Sshin		return gw;
297855163Sshin	}
297955163Sshin	return NULL;
298055163Sshin}
298155163Sshin
298255163Sshinconst char *
2983243232Shrsinet6_n2p(const struct in6_addr *p)
298455163Sshin{
298555163Sshin	static char buf[BUFSIZ];
298655163Sshin
298778064Sume	return inet_ntop(AF_INET6, (const void *)p, buf, sizeof(buf));
298855163Sshin}
298955163Sshin
299055163Sshinvoid
2991243232Shrsifrtdump(int sig)
299255163Sshin{
299355163Sshin
299455163Sshin	ifdump(sig);
299555163Sshin	rtdump(sig);
299655163Sshin}
299755163Sshin
299855163Sshinvoid
2999243232Shrsifdump(int sig)
300055163Sshin{
300155163Sshin	struct ifc *ifcp;
300255163Sshin	FILE *dump;
3003243232Shrs	int nifc = 0;
300455163Sshin
300555163Sshin	if (sig == 0)
300655163Sshin		dump = stderr;
300755163Sshin	else
300855163Sshin		if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
300955163Sshin			dump = stderr;
301055163Sshin
301155163Sshin	fprintf(dump, "%s: Interface Table Dump\n", hms());
3012243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next)
3013243232Shrs		nifc++;
301455163Sshin	fprintf(dump, "  Number of interfaces: %d\n", nifc);
3015243232Shrs
3016243232Shrs	fprintf(dump, "  advertising interfaces:\n");
3017243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3018243232Shrs		if ((ifcp->ifc_flags & IFF_UP) == 0)
3019243232Shrs			continue;
3020243232Shrs		if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
3021243232Shrs			continue;
3022243232Shrs		ifdump0(dump, ifcp);
302355163Sshin	}
302455163Sshin	fprintf(dump, "\n");
3025243232Shrs	fprintf(dump, "  non-advertising interfaces:\n");
3026243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3027243232Shrs		if ((ifcp->ifc_flags & IFF_UP) &&
3028243232Shrs		    (iff_find(ifcp, IFIL_TYPE_N) == NULL))
3029243232Shrs			continue;
3030243232Shrs		ifdump0(dump, ifcp);
3031243232Shrs	}
3032243232Shrs	fprintf(dump, "\n");
303355163Sshin	if (dump != stderr)
303455163Sshin		fclose(dump);
303555163Sshin}
303655163Sshin
303755163Sshinvoid
3038243232Shrsifdump0(FILE *dump, const struct ifc *ifcp)
303955163Sshin{
3040243232Shrs	struct ifac *ifac;
304155163Sshin	struct iff *iffp;
304255163Sshin	char buf[BUFSIZ];
304355163Sshin	const char *ft;
304455163Sshin	int addr;
304555163Sshin
304655163Sshin	fprintf(dump, "    %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n",
304755163Sshin		ifcp->ifc_name, ifcp->ifc_index, ifflags(ifcp->ifc_flags),
304855163Sshin		inet6_n2p(&ifcp->ifc_mylladdr),
304955163Sshin		ifcp->ifc_mtu, ifcp->ifc_metric);
3050243232Shrs	TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
305155163Sshin		if (ifcp->ifc_flags & IFF_POINTOPOINT) {
3052243232Shrs			inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr,
305355163Sshin				buf, sizeof(buf));
305455163Sshin			fprintf(dump, "\t%s/%d -- %s\n",
3055243232Shrs				inet6_n2p(&ifac->ifac_addr),
3056243232Shrs				ifac->ifac_plen, buf);
305755163Sshin		} else {
305855163Sshin			fprintf(dump, "\t%s/%d\n",
3059243232Shrs				inet6_n2p(&ifac->ifac_addr),
3060243232Shrs				ifac->ifac_plen);
306155163Sshin		}
306255163Sshin	}
3063243232Shrs
3064243232Shrs	fprintf(dump, "\tFilter:\n");
3065243232Shrs	TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
3066243232Shrs		addr = 0;
3067243232Shrs		switch (iffp->iff_type) {
3068243232Shrs		case IFIL_TYPE_A:
3069243232Shrs			ft = "Aggregate"; addr++; break;
3070243232Shrs		case IFIL_TYPE_N:
3071243232Shrs			ft = "No-use"; break;
3072243232Shrs		case IFIL_TYPE_O:
3073243232Shrs			ft = "Advertise-only"; addr++; break;
3074243232Shrs		case IFIL_TYPE_T:
3075243232Shrs			ft = "Default-only"; break;
3076243232Shrs		case IFIL_TYPE_L:
3077243232Shrs			ft = "Listen-only"; addr++; break;
3078243232Shrs		default:
3079243232Shrs			snprintf(buf, sizeof(buf), "Unknown-%c", iffp->iff_type);
3080243232Shrs			ft = buf;
3081243232Shrs			addr++;
3082243232Shrs			break;
308355163Sshin		}
3084243232Shrs		fprintf(dump, "\t\t%s", ft);
3085243232Shrs		if (addr)
3086243232Shrs			fprintf(dump, "(%s/%d)", inet6_n2p(&iffp->iff_addr),
3087243232Shrs				iffp->iff_plen);
308855163Sshin		fprintf(dump, "\n");
308955163Sshin	}
3090243232Shrs	fprintf(dump, "\n");
309155163Sshin}
309255163Sshin
309355163Sshinvoid
3094243232Shrsrtdump(int sig)
309555163Sshin{
309655163Sshin	struct	riprt *rrt;
309755163Sshin	char	buf[BUFSIZ];
309855163Sshin	FILE	*dump;
309955163Sshin	time_t	t, age;
310055163Sshin
310155163Sshin	if (sig == 0)
310255163Sshin		dump = stderr;
310355163Sshin	else
310455163Sshin		if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
310555163Sshin			dump = stderr;
310655163Sshin
310755163Sshin	t = time(NULL);
310855163Sshin	fprintf(dump, "\n%s: Routing Table Dump\n", hms());
3109243232Shrs	TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
311055163Sshin		if (rrt->rrt_t == 0)
311155163Sshin			age = 0;
311255163Sshin		else
311355163Sshin			age = t - rrt->rrt_t;
311455163Sshin		inet_ntop(AF_INET6, (void *)&rrt->rrt_info.rip6_dest,
311555163Sshin			buf, sizeof(buf));
311655163Sshin		fprintf(dump, "    %s/%d if(%d:%s) gw(%s) [%d] age(%ld)",
311755163Sshin			buf, rrt->rrt_info.rip6_plen, rrt->rrt_index,
311855163Sshin			index2ifc[rrt->rrt_index]->ifc_name,
311955163Sshin			inet6_n2p(&rrt->rrt_gw),
312055163Sshin			rrt->rrt_info.rip6_metric, (long)age);
312155163Sshin		if (rrt->rrt_info.rip6_tag) {
312255163Sshin			fprintf(dump, " tag(0x%04x)",
312355163Sshin				ntohs(rrt->rrt_info.rip6_tag) & 0xffff);
312455163Sshin		}
312562607Sitojun		if (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)
312655163Sshin			fprintf(dump, " NOT-LL");
312762607Sitojun		if (rrt->rrt_rflags & RRTF_NOADVERTISE)
312855163Sshin			fprintf(dump, " NO-ADV");
312955163Sshin		fprintf(dump, "\n");
313055163Sshin	}
313155163Sshin	fprintf(dump, "\n");
313255163Sshin	if (dump != stderr)
313355163Sshin		fclose(dump);
313455163Sshin}
313555163Sshin
313655163Sshin/*
313755163Sshin * Parse the -A (and -O) options and put corresponding filter object to the
313878064Sume * specified interface structures.  Each of the -A/O option has the following
313955163Sshin * syntax:	-A 5f09:c400::/32,ef0,ef1  (aggregate)
314055163Sshin * 		-O 5f09:c400::/32,ef0,ef1  (only when match)
314155163Sshin */
314255163Sshinvoid
3143243232Shrsfilterconfig(void)
314455163Sshin{
314555163Sshin	int i;
3146119083Sume	char *p, *ap, *iflp, *ifname, *ep;
3147243232Shrs	struct iff iff, *iffp;
314878064Sume	struct ifc *ifcp;
314978064Sume	struct riprt *rrt;
315064631Sitojun#if 0
315178064Sume	struct in6_addr gw;
315264631Sitojun#endif
3153119083Sume	u_long plen;
315455163Sshin
315555163Sshin	for (i = 0; i < nfilter; i++) {
315655163Sshin		ap = filter[i];
315755163Sshin		iflp = NULL;
3158243232Shrs		iffp = &iff;
3159243232Shrs		memset(iffp, 0, sizeof(*iffp));
316055163Sshin		if (filtertype[i] == 'N' || filtertype[i] == 'T') {
316155163Sshin			iflp = ap;
316255163Sshin			goto ifonly;
316355163Sshin		}
3164119038Sume		if ((p = strchr(ap, ',')) != NULL) {
316555163Sshin			*p++ = '\0';
316655163Sshin			iflp = p;
316755163Sshin		}
3168119038Sume		if ((p = strchr(ap, '/')) == NULL) {
316955163Sshin			fatal("no prefixlen specified for '%s'", ap);
317078064Sume			/*NOTREACHED*/
317178064Sume		}
317255163Sshin		*p++ = '\0';
3173243232Shrs		if (inet_pton(AF_INET6, ap, &iffp->iff_addr) != 1) {
317455163Sshin			fatal("invalid prefix specified for '%s'", ap);
317578064Sume			/*NOTREACHED*/
317678064Sume		}
3177119083Sume		errno = 0;
3178119083Sume		ep = NULL;
3179119083Sume		plen = strtoul(p, &ep, 10);
3180243232Shrs		if (errno || !*p || *ep || plen > sizeof(iffp->iff_addr) * 8) {
3181119083Sume			fatal("invalid prefix length specified for '%s'", ap);
3182119083Sume			/*NOTREACHED*/
3183119083Sume		}
3184243232Shrs		iffp->iff_plen = plen;
3185243232Shrs		applyplen(&iffp->iff_addr, iffp->iff_plen);
318655163Sshinifonly:
3187243232Shrs		iffp->iff_type = filtertype[i];
318878064Sume		if (iflp == NULL || *iflp == '\0') {
318955163Sshin			fatal("no interface specified for '%s'", ap);
319078064Sume			/*NOTREACHED*/
319178064Sume		}
319255163Sshin		/* parse the interface listing portion */
319355163Sshin		while (iflp) {
319455163Sshin			ifname = iflp;
3195119038Sume			if ((iflp = strchr(iflp, ',')) != NULL)
319655163Sshin				*iflp++ = '\0';
3197243232Shrs
3198243232Shrs			TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3199243233Shrs				if (fnmatch(ifname, ifcp->ifc_name, 0) != 0)
3200243232Shrs					continue;
3201243232Shrs
3202243232Shrs				iffp = malloc(sizeof(*iffp));
3203243232Shrs				if (iffp == NULL) {
3204243232Shrs					fatal("malloc of iff");
3205243232Shrs					/*NOTREACHED*/
3206243232Shrs				}
3207243232Shrs				memcpy(iffp, &iff, sizeof(*iffp));
3208243233Shrs#if 0
3209243233Shrs				syslog(LOG_INFO, "Add filter: type %d, ifname %s.", iffp->iff_type, ifname);
3210243233Shrs#endif
3211243232Shrs				TAILQ_INSERT_HEAD(&ifcp->ifc_iff_head, iffp, iff_next);
321278064Sume			}
321355163Sshin		}
321478064Sume
321578064Sume		/*
321678064Sume		 * -A: aggregate configuration.
321778064Sume		 */
3218243232Shrs		if (filtertype[i] != IFIL_TYPE_A)
321955163Sshin			continue;
322055163Sshin		/* put the aggregate to the kernel routing table */
322155163Sshin		rrt = (struct riprt *)malloc(sizeof(struct riprt));
322278064Sume		if (rrt == NULL) {
322355163Sshin			fatal("malloc: rrt");
322478064Sume			/*NOTREACHED*/
322578064Sume		}
322655163Sshin		memset(rrt, 0, sizeof(struct riprt));
3227243232Shrs		rrt->rrt_info.rip6_dest = iff.iff_addr;
3228243232Shrs		rrt->rrt_info.rip6_plen = iff.iff_plen;
322955163Sshin		rrt->rrt_info.rip6_metric = 1;
323055163Sshin		rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
323155163Sshin		rrt->rrt_gw = in6addr_loopback;
323262607Sitojun		rrt->rrt_flags = RTF_UP | RTF_REJECT;
323362607Sitojun		rrt->rrt_rflags = RRTF_AGGREGATE;
323455163Sshin		rrt->rrt_t = 0;
3235119039Sume		rrt->rrt_index = loopifcp->ifc_index;
323664631Sitojun#if 0
323764631Sitojun		if (getroute(&rrt->rrt_info, &gw)) {
323864631Sitojun#if 0
323964631Sitojun			/*
324064631Sitojun			 * When the address has already been registered in the
324164631Sitojun			 * kernel routing table, it should be removed
324264631Sitojun			 */
324364631Sitojun			delroute(&rrt->rrt_info, &gw);
324464631Sitojun#else
324578064Sume			/* it is safer behavior */
324664631Sitojun			errno = EINVAL;
324764631Sitojun			fatal("%s/%u already in routing table, "
324864631Sitojun			    "cannot aggregate",
324964631Sitojun			    inet6_n2p(&rrt->rrt_info.rip6_dest),
325064631Sitojun			    rrt->rrt_info.rip6_plen);
325178064Sume			/*NOTREACHED*/
325264631Sitojun#endif
325364631Sitojun		}
325464631Sitojun#endif
325555163Sshin		/* Put the route to the list */
3256243232Shrs		TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
325755163Sshin		trace(1, "Aggregate: %s/%d for %s\n",
3258243232Shrs			inet6_n2p(&iff.iff_addr), iff.iff_plen,
3259243232Shrs			loopifcp->ifc_name);
326055163Sshin		/* Add this route to the kernel */
326155163Sshin		if (nflag) 	/* do not modify kernel routing table */
326255163Sshin			continue;
326355163Sshin		addroute(rrt, &in6addr_loopback, loopifcp);
326455163Sshin	}
326555163Sshin}
326655163Sshin
326755163Sshin/***************** utility functions *****************/
326855163Sshin
326955163Sshin/*
327055163Sshin * Returns a pointer to ifac whose address and prefix length matches
327155163Sshin * with the address and prefix length specified in the arguments.
327255163Sshin */
327355163Sshinstruct ifac *
3274243232Shrsifa_match(const struct ifc *ifcp,
3275243232Shrs	const struct in6_addr *ia,
3276243232Shrs	int plen)
327755163Sshin{
3278243232Shrs	struct ifac *ifac;
327955163Sshin
3280243232Shrs	TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
3281243232Shrs		if (IN6_ARE_ADDR_EQUAL(&ifac->ifac_addr, ia) &&
3282243232Shrs		    ifac->ifac_plen == plen)
328355163Sshin			break;
328455163Sshin	}
3285243232Shrs
3286243232Shrs	return (ifac);
328755163Sshin}
328855163Sshin
328955163Sshin/*
329055163Sshin * Return a pointer to riprt structure whose address and prefix length
329155163Sshin * matches with the address and prefix length found in the argument.
329278064Sume * Note: This is not a rtalloc().  Therefore exact match is necessary.
329355163Sshin */
329455163Sshinstruct riprt *
3295243232Shrsrtsearch(struct netinfo6 *np)
329655163Sshin{
329755163Sshin	struct	riprt	*rrt;
329855163Sshin
3299243232Shrs	TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
330055163Sshin		if (rrt->rrt_info.rip6_plen == np->rip6_plen &&
330155163Sshin		    IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
330255163Sshin				       &np->rip6_dest))
3303243232Shrs			break;
330455163Sshin	}
3305243232Shrs
3306243232Shrs	return (rrt);
330755163Sshin}
330855163Sshin
330955163Sshinint
3310243232Shrssin6mask2len(const struct sockaddr_in6 *sin6)
331178064Sume{
331278064Sume
331378064Sume	return mask2len(&sin6->sin6_addr,
331478064Sume	    sin6->sin6_len - offsetof(struct sockaddr_in6, sin6_addr));
331578064Sume}
331678064Sume
331778064Sumeint
3318243232Shrsmask2len(const struct in6_addr *addr, int lenlim)
331955163Sshin{
332055163Sshin	int i = 0, j;
332178064Sume	const u_char *p = (const u_char *)addr;
332262607Sitojun
332355163Sshin	for (j = 0; j < lenlim; j++, p++) {
332455163Sshin		if (*p != 0xff)
332555163Sshin			break;
332655163Sshin		i += 8;
332755163Sshin	}
332855163Sshin	if (j < lenlim) {
332955163Sshin		switch (*p) {
333062607Sitojun#define	MASKLEN(m, l)	case m: do { i += l; break; } while (0)
333162607Sitojun		MASKLEN(0xfe, 7); break;
333262607Sitojun		MASKLEN(0xfc, 6); break;
333362607Sitojun		MASKLEN(0xf8, 5); break;
333462607Sitojun		MASKLEN(0xf0, 4); break;
333562607Sitojun		MASKLEN(0xe0, 3); break;
333662607Sitojun		MASKLEN(0xc0, 2); break;
333762607Sitojun		MASKLEN(0x80, 1); break;
333855163Sshin#undef	MASKLEN
333955163Sshin		}
334055163Sshin	}
334155163Sshin	return i;
334255163Sshin}
334355163Sshin
334455163Sshinvoid
3345243232Shrsapplymask(struct in6_addr *addr, struct in6_addr *mask)
334655163Sshin{
334755163Sshin	int	i;
334855163Sshin	u_long	*p, *q;
334955163Sshin
335055163Sshin	p = (u_long *)addr; q = (u_long *)mask;
335155163Sshin	for (i = 0; i < 4; i++)
335255163Sshin		*p++ &= *q++;
335355163Sshin}
335455163Sshin
335555163Sshinstatic const u_char plent[8] = {
335655163Sshin	0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
335755163Sshin};
335855163Sshin
335955163Sshinvoid
3360243232Shrsapplyplen(struct in6_addr *ia, int plen)
336155163Sshin{
336255163Sshin	u_char	*p;
336355163Sshin	int	i;
336455163Sshin
336555163Sshin	p = ia->s6_addr;
336655163Sshin	for (i = 0; i < 16; i++) {
336755163Sshin		if (plen <= 0)
336855163Sshin			*p = 0;
336955163Sshin		else if (plen < 8)
337055163Sshin			*p &= plent[plen];
337155163Sshin		p++, plen -= 8;
337255163Sshin	}
337355163Sshin}
337455163Sshin
337555163Sshinstatic const int pl2m[9] = {
337655163Sshin	0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
337755163Sshin};
337855163Sshin
337955163Sshinstruct in6_addr *
3380243232Shrsplen2mask(int n)
338155163Sshin{
338255163Sshin	static struct in6_addr ia;
338355163Sshin	u_char	*p;
338455163Sshin	int	i;
338555163Sshin
338655163Sshin	memset(&ia, 0, sizeof(struct in6_addr));
338755163Sshin	p = (u_char *)&ia;
338855163Sshin	for (i = 0; i < 16; i++, p++, n -= 8) {
338955163Sshin		if (n >= 8) {
339055163Sshin			*p = 0xff;
339155163Sshin			continue;
339255163Sshin		}
339355163Sshin		*p = pl2m[n];
339455163Sshin		break;
339555163Sshin	}
339655163Sshin	return &ia;
339755163Sshin}
339855163Sshin
339955163Sshinchar *
3400243232Shrsallocopy(char *p)
340155163Sshin{
3402119033Sume	int len = strlen(p) + 1;
3403119033Sume	char *q = (char *)malloc(len);
340455163Sshin
3405119033Sume	if (!q) {
3406119033Sume		fatal("malloc");
3407119033Sume		/*NOTREACHED*/
3408119033Sume	}
3409119033Sume
3410119033Sume	strlcpy(q, p, len);
341155163Sshin	return q;
341255163Sshin}
341355163Sshin
341455163Sshinchar *
3415243232Shrshms(void)
341655163Sshin{
341755163Sshin	static char buf[BUFSIZ];
341855163Sshin	time_t t;
341955163Sshin	struct	tm *tm;
342055163Sshin
342155163Sshin	t = time(NULL);
342278064Sume	if ((tm = localtime(&t)) == 0) {
342355163Sshin		fatal("localtime");
342478064Sume		/*NOTREACHED*/
342578064Sume	}
342678064Sume	snprintf(buf, sizeof(buf), "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
342778064Sume	    tm->tm_sec);
342855163Sshin	return buf;
342955163Sshin}
343055163Sshin
343155163Sshin#define	RIPRANDDEV	1.0	/* 30 +- 15, max - min = 30 */
343255163Sshin
343355163Sshinint
3434243232Shrsripinterval(int timer)
343555163Sshin{
343655163Sshin	double r = rand();
343755163Sshin
343855163Sshin	interval = (int)(timer + timer * RIPRANDDEV * (r / RAND_MAX - 0.5));
343955163Sshin	nextalarm = time(NULL) + interval;
344055163Sshin	return interval;
344155163Sshin}
344255163Sshin
344355163Sshintime_t
3444243232Shrsripsuptrig(void)
344555163Sshin{
344655163Sshin	time_t t;
344755163Sshin
344855163Sshin	double r = rand();
344962607Sitojun	t  = (int)(RIP_TRIG_INT6_MIN +
345078064Sume		(RIP_TRIG_INT6_MAX - RIP_TRIG_INT6_MIN) * (r / RAND_MAX));
345155163Sshin	sup_trig_update = time(NULL) + t;
345255163Sshin	return t;
345355163Sshin}
345455163Sshin
345555163Sshinvoid
345655163Sshin#ifdef __STDC__
345755163Sshinfatal(const char *fmt, ...)
345855163Sshin#else
345955163Sshinfatal(fmt, va_alist)
346055163Sshin	char	*fmt;
346155163Sshin	va_dcl
346255163Sshin#endif
346355163Sshin{
346455163Sshin	va_list ap;
346555163Sshin	char buf[1024];
346655163Sshin
346755163Sshin#ifdef __STDC__
346855163Sshin	va_start(ap, fmt);
346955163Sshin#else
347055163Sshin	va_start(ap);
347155163Sshin#endif
347255163Sshin	vsnprintf(buf, sizeof(buf), fmt, ap);
3473119043Sume	va_end(ap);
347455163Sshin	perror(buf);
3475119043Sume	if (errno)
3476119043Sume		syslog(LOG_ERR, "%s: %s", buf, strerror(errno));
3477119043Sume	else
3478119043Sume		syslog(LOG_ERR, "%s", buf);
347978064Sume	rtdexit();
348055163Sshin}
348155163Sshin
348255163Sshinvoid
348355163Sshin#ifdef __STDC__
348455163Sshintracet(int level, const char *fmt, ...)
348555163Sshin#else
348655163Sshintracet(level, fmt, va_alist)
348755163Sshin	int level;
348855163Sshin	char *fmt;
348955163Sshin	va_dcl
349055163Sshin#endif
349155163Sshin{
349255163Sshin	va_list ap;
349355163Sshin
3494119043Sume	if (level <= dflag) {
349555163Sshin#ifdef __STDC__
3496119043Sume		va_start(ap, fmt);
349755163Sshin#else
3498119043Sume		va_start(ap);
349955163Sshin#endif
350055163Sshin		fprintf(stderr, "%s: ", hms());
350155163Sshin		vfprintf(stderr, fmt, ap);
3502119043Sume		va_end(ap);
350355163Sshin	}
350455163Sshin	if (dflag) {
3505119043Sume#ifdef __STDC__
3506119043Sume		va_start(ap, fmt);
3507119043Sume#else
3508119043Sume		va_start(ap);
3509119043Sume#endif
351055163Sshin		if (level > 0)
351155163Sshin			vsyslog(LOG_DEBUG, fmt, ap);
351255163Sshin		else
351355163Sshin			vsyslog(LOG_WARNING, fmt, ap);
3514119043Sume		va_end(ap);
351555163Sshin	}
351655163Sshin}
351755163Sshin
351855163Sshinvoid
351955163Sshin#ifdef __STDC__
352055163Sshintrace(int level, const char *fmt, ...)
352155163Sshin#else
352255163Sshintrace(level, fmt, va_alist)
352355163Sshin	int level;
352455163Sshin	char *fmt;
352555163Sshin	va_dcl
352655163Sshin#endif
352755163Sshin{
352855163Sshin	va_list ap;
352955163Sshin
3530119043Sume	if (level <= dflag) {
353155163Sshin#ifdef __STDC__
3532119043Sume		va_start(ap, fmt);
353355163Sshin#else
3534119043Sume		va_start(ap);
353555163Sshin#endif
353655163Sshin		vfprintf(stderr, fmt, ap);
3537119043Sume		va_end(ap);
3538119043Sume	}
353955163Sshin	if (dflag) {
3540119043Sume#ifdef __STDC__
3541119043Sume		va_start(ap, fmt);
3542119043Sume#else
3543119043Sume		va_start(ap);
3544119043Sume#endif
354555163Sshin		if (level > 0)
354655163Sshin			vsyslog(LOG_DEBUG, fmt, ap);
354755163Sshin		else
354855163Sshin			vsyslog(LOG_WARNING, fmt, ap);
3549119043Sume		va_end(ap);
355055163Sshin	}
355155163Sshin}
355255163Sshin
355355163Sshinunsigned int
3554243232Shrsif_maxindex(void)
355555163Sshin{
355655163Sshin	struct if_nameindex *p, *p0;
355755163Sshin	unsigned int max = 0;
355855163Sshin
355955163Sshin	p0 = if_nameindex();
356055163Sshin	for (p = p0; p && p->if_index && p->if_name; p++) {
356155163Sshin		if (max < p->if_index)
356255163Sshin			max = p->if_index;
356355163Sshin	}
356455163Sshin	if_freenameindex(p0);
356555163Sshin	return max;
356655163Sshin}
356755163Sshin
356855163Sshinstruct ifc *
3569243232Shrsifc_find(char *name)
357055163Sshin{
357155163Sshin	struct ifc *ifcp;
357255163Sshin
3573243232Shrs	TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
357455163Sshin		if (strcmp(name, ifcp->ifc_name) == 0)
3575243232Shrs			break;
357655163Sshin	}
3577243232Shrs	return (ifcp);
357855163Sshin}
357955163Sshin
358055163Sshinstruct iff *
3581243232Shrsiff_find(struct ifc *ifcp, int type)
358255163Sshin{
358355163Sshin	struct iff *iffp;
358455163Sshin
3585243232Shrs	TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
3586243232Shrs		if (type == IFIL_TYPE_ANY ||
3587243232Shrs		    type == iffp->iff_type)
3588243232Shrs			break;
358955163Sshin	}
3590243232Shrs
3591243232Shrs	return (iffp);
359255163Sshin}
359355163Sshin
359455163Sshinvoid
3595243232Shrssetindex2ifc(int idx, struct ifc *ifcp)
359655163Sshin{
3597122677Sume	int n, nsize;
359862607Sitojun	struct ifc **p;
359955163Sshin
360055163Sshin	if (!index2ifc) {
360155163Sshin		nindex2ifc = 5;	/*initial guess*/
360255163Sshin		index2ifc = (struct ifc **)
360355163Sshin			malloc(sizeof(*index2ifc) * nindex2ifc);
360478064Sume		if (index2ifc == NULL) {
360555163Sshin			fatal("malloc");
360678064Sume			/*NOTREACHED*/
360778064Sume		}
360855163Sshin		memset(index2ifc, 0, sizeof(*index2ifc) * nindex2ifc);
360955163Sshin	}
361055163Sshin	n = nindex2ifc;
3611122677Sume	for (nsize = nindex2ifc; nsize <= idx; nsize *= 2)
3612122677Sume		;
3613122677Sume	if (n != nsize) {
361462607Sitojun		p = (struct ifc **)realloc(index2ifc,
3615122677Sume		    sizeof(*index2ifc) * nsize);
361678064Sume		if (p == NULL) {
361755163Sshin			fatal("realloc");
361878064Sume			/*NOTREACHED*/
361978064Sume		}
362078064Sume		memset(p + n, 0, sizeof(*index2ifc) * (nindex2ifc - n));
362162607Sitojun		index2ifc = p;
3622122677Sume		nindex2ifc = nsize;
362355163Sshin	}
362478064Sume	index2ifc[idx] = ifcp;
362555163Sshin}
3626