Home | History | Annotate | Line # | Download | only in route6d
route6d.c revision 1.36
      1  1.36     wiz /*	$NetBSD: route6d.c,v 1.36 2002/08/01 00:17:31 wiz Exp $	*/
      2  1.34  itojun /*	$KAME: route6d.c,v 1.85 2002/06/07 16:39:41 itojun Exp $	*/
      3   1.1  itojun 
      4   1.1  itojun /*
      5   1.1  itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6   1.1  itojun  * All rights reserved.
      7   1.1  itojun  *
      8   1.1  itojun  * Redistribution and use in source and binary forms, with or without
      9   1.1  itojun  * modification, are permitted provided that the following conditions
     10   1.1  itojun  * are met:
     11   1.1  itojun  * 1. Redistributions of source code must retain the above copyright
     12   1.1  itojun  *    notice, this list of conditions and the following disclaimer.
     13   1.1  itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1  itojun  *    notice, this list of conditions and the following disclaimer in the
     15   1.1  itojun  *    documentation and/or other materials provided with the distribution.
     16   1.1  itojun  * 3. Neither the name of the project nor the names of its contributors
     17   1.1  itojun  *    may be used to endorse or promote products derived from this software
     18   1.1  itojun  *    without specific prior written permission.
     19   1.1  itojun  *
     20   1.1  itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21   1.1  itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22   1.1  itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23   1.1  itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24   1.1  itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25   1.1  itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26   1.1  itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27   1.1  itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28   1.1  itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29   1.1  itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30   1.1  itojun  * SUCH DAMAGE.
     31   1.1  itojun  */
     32   1.1  itojun 
     33   1.2  itojun #include <sys/cdefs.h>
     34   1.1  itojun #ifndef	lint
     35  1.36     wiz __RCSID("$NetBSD: route6d.c,v 1.36 2002/08/01 00:17:31 wiz Exp $");
     36   1.1  itojun #endif
     37   1.1  itojun 
     38   1.1  itojun #include <stdio.h>
     39   1.1  itojun 
     40   1.1  itojun #include <time.h>
     41   1.1  itojun #include <unistd.h>
     42   1.1  itojun #include <stdlib.h>
     43   1.1  itojun #include <string.h>
     44   1.1  itojun #include <signal.h>
     45   1.1  itojun #include <stdarg.h>
     46   1.1  itojun #include <syslog.h>
     47   1.1  itojun #include <stddef.h>
     48  1.10  itojun #include <errno.h>
     49   1.9  itojun #include <err.h>
     50  1.33  itojun #include <util.h>
     51   1.1  itojun 
     52   1.1  itojun #include <sys/types.h>
     53   1.1  itojun #include <sys/param.h>
     54   1.1  itojun #include <sys/file.h>
     55   1.1  itojun #include <sys/socket.h>
     56   1.1  itojun #include <sys/ioctl.h>
     57   1.1  itojun #include <sys/sysctl.h>
     58   1.1  itojun #include <sys/uio.h>
     59   1.1  itojun #include <net/if.h>
     60   1.1  itojun #if defined(__FreeBSD__) && __FreeBSD__ >= 3
     61   1.1  itojun #include <net/if_var.h>
     62   1.1  itojun #endif /* __FreeBSD__ >= 3 */
     63   1.1  itojun #define	KERNEL	1
     64  1.10  itojun #define	_KERNEL	1
     65   1.1  itojun #include <net/route.h>
     66   1.1  itojun #undef KERNEL
     67  1.10  itojun #undef _KERNEL
     68   1.1  itojun #include <netinet/in.h>
     69   1.1  itojun #include <netinet/in_var.h>
     70   1.1  itojun #include <netinet/ip6.h>
     71   1.1  itojun #include <netinet/udp.h>
     72   1.1  itojun #include <netdb.h>
     73  1.10  itojun #include <ifaddrs.h>
     74   1.1  itojun 
     75   1.1  itojun #include <arpa/inet.h>
     76   1.1  itojun 
     77   1.1  itojun #include "route6d.h"
     78   1.1  itojun 
     79   1.1  itojun #define	MAXFILTER	40
     80   1.1  itojun 
     81   1.1  itojun #ifdef	DEBUG
     82   1.1  itojun #define	INIT_INTERVAL6	6
     83   1.1  itojun #else
     84   1.1  itojun #define	INIT_INTERVAL6	10	/* Wait to submit a initial riprequest */
     85   1.1  itojun #endif
     86   1.1  itojun 
     87   1.8  itojun /* alignment constraint for routing socket */
     88   1.8  itojun #define ROUNDUP(a) \
     89   1.8  itojun 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
     90   1.8  itojun #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
     91   1.8  itojun 
     92   1.1  itojun /*
     93   1.1  itojun  * Following two macros are highly depending on KAME Release
     94   1.1  itojun  */
     95   1.1  itojun #define	IN6_LINKLOCAL_IFINDEX(addr) \
     96   1.8  itojun 	((addr).s6_addr[2] << 8 | (addr).s6_addr[3])
     97   1.1  itojun 
     98   1.1  itojun #define	SET_IN6_LINKLOCAL_IFINDEX(addr, index) \
     99   1.8  itojun 	do { \
    100   1.8  itojun 		(addr).s6_addr[2] = ((index) >> 8) & 0xff; \
    101   1.8  itojun 		(addr).s6_addr[3] = (index) & 0xff; \
    102   1.8  itojun 	} while (0)
    103   1.1  itojun 
    104   1.1  itojun struct	ifc {			/* Configuration of an interface */
    105   1.1  itojun 	char	*ifc_name;			/* if name */
    106   1.1  itojun 	struct	ifc *ifc_next;
    107   1.1  itojun 	int	ifc_index;			/* if index */
    108   1.1  itojun 	int	ifc_mtu;			/* if mtu */
    109   1.1  itojun 	int	ifc_metric;			/* if metric */
    110  1.24  itojun 	u_int	ifc_flags;			/* flags */
    111  1.24  itojun 	short	ifc_cflags;			/* IFC_XXX */
    112   1.1  itojun 	struct	in6_addr ifc_mylladdr;		/* my link-local address */
    113   1.1  itojun 	struct	sockaddr_in6 ifc_ripsin;	/* rip multicast address */
    114   1.1  itojun 	struct	iff *ifc_filter;		/* filter structure */
    115   1.1  itojun 	struct	ifac *ifc_addr;			/* list of AF_INET6 addresses */
    116   1.1  itojun 	int	ifc_joined;			/* joined to ff02::9 */
    117   1.1  itojun };
    118   1.1  itojun 
    119   1.1  itojun struct	ifac {			/* Adddress associated to an interface */
    120   1.1  itojun 	struct	ifc *ifa_conf;		/* back pointer */
    121   1.1  itojun 	struct	ifac *ifa_next;
    122   1.1  itojun 	struct	in6_addr ifa_addr;	/* address */
    123   1.1  itojun 	struct	in6_addr ifa_raddr;	/* remote address, valid in p2p */
    124   1.1  itojun 	int	ifa_plen;		/* prefix length */
    125   1.1  itojun };
    126   1.1  itojun 
    127   1.1  itojun struct	iff {
    128   1.1  itojun 	int	iff_type;
    129   1.1  itojun 	struct	in6_addr iff_addr;
    130   1.1  itojun 	int	iff_plen;
    131   1.1  itojun 	struct	iff *iff_next;
    132   1.1  itojun };
    133   1.1  itojun 
    134   1.1  itojun struct	ifc *ifc;
    135   1.1  itojun int	nifc;		/* number of valid ifc's */
    136   1.1  itojun struct	ifc **index2ifc;
    137   1.1  itojun int	nindex2ifc;
    138   1.1  itojun struct	ifc *loopifcp = NULL;	/* pointing to loopback */
    139  1.34  itojun fd_set	*sockvecp;	/* vector to select() for receiving */
    140  1.34  itojun fd_set	*recvecp;
    141  1.34  itojun int	fdmasks;
    142   1.1  itojun int	rtsock;		/* the routing socket */
    143   1.1  itojun int	ripsock;	/* socket to send/receive RIP datagram */
    144  1.29  itojun int	maxfd;		/* maximum fd for select() */
    145   1.1  itojun 
    146   1.1  itojun struct	rip6 *ripbuf;	/* packet buffer for sending */
    147   1.1  itojun 
    148   1.1  itojun /*
    149  1.24  itojun  * Maintain the routes in a linked list.  When the number of the routes
    150   1.1  itojun  * grows, somebody would like to introduce a hash based or a radix tree
    151  1.24  itojun  * based structure.  I believe the number of routes handled by RIP is
    152   1.1  itojun  * limited and I don't have to manage a complex data structure, however.
    153   1.1  itojun  *
    154   1.1  itojun  * One of the major drawbacks of the linear linked list is the difficulty
    155  1.24  itojun  * of representing the relationship between a couple of routes.  This may
    156   1.1  itojun  * be a significant problem when we have to support route aggregation with
    157   1.1  itojun  * supressing the specifices covered by the aggregate.
    158   1.1  itojun  */
    159   1.1  itojun 
    160   1.1  itojun struct	riprt {
    161   1.1  itojun 	struct	riprt *rrt_next;	/* next destination */
    162   1.1  itojun 	struct	riprt *rrt_same;	/* same destination - future use */
    163   1.1  itojun 	struct	netinfo6 rrt_info;	/* network info */
    164   1.1  itojun 	struct	in6_addr rrt_gw;	/* gateway */
    165  1.13  itojun 	u_long	rrt_flags;		/* kernel routing table flags */
    166  1.13  itojun 	u_long	rrt_rflags;		/* route6d routing table flags */
    167   1.1  itojun 	time_t	rrt_t;			/* when the route validated */
    168   1.1  itojun 	int	rrt_index;		/* ifindex from which this route got */
    169   1.1  itojun };
    170   1.1  itojun 
    171   1.1  itojun struct	riprt *riprt = 0;
    172   1.1  itojun 
    173   1.1  itojun int	dflag = 0;	/* debug flag */
    174   1.1  itojun int	qflag = 0;	/* quiet flag */
    175   1.1  itojun int	nflag = 0;	/* don't update kernel routing table */
    176   1.1  itojun int	aflag = 0;	/* age out even the statically defined routes */
    177   1.1  itojun int	hflag = 0;	/* don't split horizon */
    178   1.1  itojun int	lflag = 0;	/* exchange site local routes */
    179   1.1  itojun int	sflag = 0;	/* announce static routes w/ split horizon */
    180   1.1  itojun int	Sflag = 0;	/* announce static routes to every interface */
    181  1.14  itojun unsigned long routetag = 0;	/* route tag attached on originating case */
    182   1.1  itojun 
    183   1.1  itojun char	*filter[MAXFILTER];
    184   1.1  itojun int	filtertype[MAXFILTER];
    185   1.1  itojun int	nfilter = 0;
    186   1.1  itojun 
    187   1.1  itojun pid_t	pid;
    188   1.1  itojun 
    189   1.1  itojun struct	sockaddr_storage ripsin;
    190   1.1  itojun 
    191   1.1  itojun struct	rtentry rtentry;
    192   1.1  itojun 
    193   1.1  itojun int	interval = 1;
    194   1.1  itojun time_t	nextalarm = 0;
    195   1.1  itojun time_t	sup_trig_update = 0;
    196   1.1  itojun 
    197   1.1  itojun FILE	*rtlog = NULL;
    198   1.1  itojun 
    199   1.9  itojun int logopened = 0;
    200   1.9  itojun 
    201  1.33  itojun static	int	seq = 0;
    202   1.1  itojun 
    203  1.24  itojun volatile sig_atomic_t seenalrm;
    204  1.24  itojun volatile sig_atomic_t seenquit;
    205  1.24  itojun volatile sig_atomic_t seenusr1;
    206  1.21  itojun 
    207  1.13  itojun #define	RRTF_AGGREGATE		0x08000000
    208  1.13  itojun #define	RRTF_NOADVERTISE	0x10000000
    209  1.13  itojun #define	RRTF_NH_NOT_LLADDR	0x20000000
    210  1.13  itojun #define RRTF_SENDANYWAY		0x40000000
    211  1.13  itojun #define	RRTF_CHANGED		0x80000000
    212   1.1  itojun 
    213   1.1  itojun int main __P((int, char **));
    214  1.21  itojun void sighandler __P((int));
    215  1.24  itojun void ripalarm __P((void));
    216   1.1  itojun void riprecv __P((void));
    217   1.1  itojun void ripsend __P((struct ifc *, struct sockaddr_in6 *, int));
    218  1.24  itojun int out_filter __P((struct riprt *, struct ifc *));
    219   1.1  itojun void init __P((void));
    220   1.1  itojun void sockopt __P((struct ifc *));
    221   1.1  itojun void ifconfig __P((void));
    222  1.10  itojun void ifconfig1 __P((const char *, const struct sockaddr *, struct ifc *, int));
    223   1.1  itojun void rtrecv __P((void));
    224   1.1  itojun int rt_del __P((const struct sockaddr_in6 *, const struct sockaddr_in6 *,
    225   1.1  itojun 	const struct sockaddr_in6 *));
    226   1.1  itojun int rt_deladdr __P((struct ifc *, const struct sockaddr_in6 *,
    227   1.1  itojun 	const struct sockaddr_in6 *));
    228   1.1  itojun void filterconfig __P((void));
    229   1.1  itojun int getifmtu __P((int));
    230  1.24  itojun const char *rttypes __P((struct rt_msghdr *));
    231  1.24  itojun const char *rtflags __P((struct rt_msghdr *));
    232  1.24  itojun const char *ifflags __P((int));
    233  1.24  itojun int ifrt __P((struct ifc *, int));
    234  1.12  itojun void ifrt_p2p __P((struct ifc *, int));
    235   1.1  itojun void applymask __P((struct in6_addr *, struct in6_addr *));
    236   1.1  itojun void applyplen __P((struct in6_addr *, int));
    237   1.1  itojun void ifrtdump __P((int));
    238   1.1  itojun void ifdump __P((int));
    239   1.1  itojun void ifdump0 __P((FILE *, const struct ifc *));
    240   1.1  itojun void rtdump __P((int));
    241   1.1  itojun void rt_entry __P((struct rt_msghdr *, int));
    242  1.24  itojun void rtdexit __P((void));
    243  1.24  itojun void riprequest __P((struct ifc *, struct netinfo6 *, int,
    244  1.24  itojun 	struct sockaddr_in6 *));
    245   1.1  itojun void ripflush __P((struct ifc *, struct sockaddr_in6 *));
    246   1.1  itojun void sendrequest __P((struct ifc *));
    247  1.24  itojun int sin6mask2len __P((const struct sockaddr_in6 *));
    248   1.1  itojun int mask2len __P((const struct in6_addr *, int));
    249   1.1  itojun int sendpacket __P((struct sockaddr_in6 *, int));
    250   1.1  itojun int addroute __P((struct riprt *, const struct in6_addr *, struct ifc *));
    251   1.1  itojun int delroute __P((struct netinfo6 *, struct in6_addr *));
    252   1.1  itojun struct in6_addr *getroute __P((struct netinfo6 *, struct in6_addr *));
    253   1.1  itojun void krtread __P((int));
    254   1.1  itojun int tobeadv __P((struct riprt *, struct ifc *));
    255   1.1  itojun char *allocopy __P((char *));
    256   1.1  itojun char *hms __P((void));
    257   1.1  itojun const char *inet6_n2p __P((const struct in6_addr *));
    258   1.1  itojun struct ifac *ifa_match __P((const struct ifc *, const struct in6_addr *, int));
    259   1.1  itojun struct in6_addr *plen2mask __P((int));
    260  1.24  itojun struct riprt *rtsearch __P((struct netinfo6 *, struct riprt **));
    261   1.1  itojun int ripinterval __P((int));
    262   1.1  itojun time_t ripsuptrig __P((void));
    263  1.18  itojun void fatal __P((const char *, ...))
    264  1.18  itojun 	__attribute__((__format__(__printf__, 1, 2)));
    265  1.18  itojun void trace __P((int, const char *, ...))
    266  1.18  itojun 	__attribute__((__format__(__printf__, 2, 3)));
    267  1.18  itojun void tracet __P((int, const char *, ...))
    268  1.18  itojun 	__attribute__((__format__(__printf__, 2, 3)));
    269   1.1  itojun unsigned int if_maxindex __P((void));
    270   1.1  itojun struct ifc *ifc_find __P((char *));
    271   1.1  itojun struct iff *iff_find __P((struct ifc *, int));
    272   1.1  itojun void setindex2ifc __P((int, struct ifc *));
    273   1.1  itojun 
    274   1.1  itojun #define	MALLOC(type)	((type *)malloc(sizeof(type)))
    275   1.1  itojun 
    276   1.7   itohy int
    277  1.36     wiz main(int argc, char **argv)
    278   1.1  itojun {
    279   1.3  itojun 	int	ch;
    280   1.1  itojun 	int	error = 0;
    281   1.1  itojun 	struct	ifc *ifcp;
    282   1.1  itojun 	sigset_t mask, omask;
    283   1.1  itojun 	char *progname;
    284  1.14  itojun 	char *ep;
    285   1.1  itojun 
    286   1.9  itojun 	progname = strrchr(*argv, '/');
    287   1.9  itojun 	if (progname)
    288   1.9  itojun 		progname++;
    289   1.9  itojun 	else
    290   1.9  itojun 		progname = *argv;
    291   1.9  itojun 
    292   1.1  itojun 	pid = getpid();
    293  1.27  kleink 	while ((ch = getopt(argc, argv, "A:N:O:R:T:L:t:adDhlnqsS")) != -1) {
    294   1.1  itojun 		switch (ch) {
    295   1.1  itojun 		case 'A':
    296   1.1  itojun 		case 'N':
    297   1.1  itojun 		case 'O':
    298   1.1  itojun 		case 'T':
    299   1.1  itojun 		case 'L':
    300  1.14  itojun 			if (nfilter >= MAXFILTER) {
    301   1.1  itojun 				fatal("Exceeds MAXFILTER");
    302  1.14  itojun 				/*NOTREACHED*/
    303  1.14  itojun 			}
    304   1.1  itojun 			filtertype[nfilter] = ch;
    305   1.1  itojun 			filter[nfilter++] = allocopy(optarg);
    306   1.1  itojun 			break;
    307   1.1  itojun 		case 't':
    308  1.14  itojun 			ep = NULL;
    309  1.14  itojun 			routetag = strtoul(optarg, &ep, 0);
    310  1.14  itojun 			if (!ep || *ep != '\0' || (routetag & ~0xffff) != 0) {
    311   1.1  itojun 				fatal("invalid route tag");
    312   1.1  itojun 				/*NOTREACHED*/
    313   1.1  itojun 			}
    314   1.1  itojun 			break;
    315   1.1  itojun 		case 'R':
    316  1.14  itojun 			if ((rtlog = fopen(optarg, "w")) == NULL) {
    317   1.1  itojun 				fatal("Can not write to routelog");
    318  1.14  itojun 				/*NOTREACHED*/
    319  1.14  itojun 			}
    320   1.1  itojun 			break;
    321  1.14  itojun #define	FLAG(c, flag, n)	case c: do { flag = n; break; } while(0)
    322  1.14  itojun 		FLAG('a', aflag, 1); break;
    323  1.14  itojun 		FLAG('d', dflag, 1); break;
    324  1.14  itojun 		FLAG('D', dflag, 2); break;
    325  1.14  itojun 		FLAG('h', hflag, 1); break;
    326  1.14  itojun 		FLAG('l', lflag, 1); break;
    327  1.14  itojun 		FLAG('n', nflag, 1); break;
    328  1.14  itojun 		FLAG('q', qflag, 1); break;
    329  1.14  itojun 		FLAG('s', sflag, 1); break;
    330  1.14  itojun 		FLAG('S', Sflag, 1); break;
    331   1.1  itojun #undef	FLAG
    332   1.1  itojun 		default:
    333   1.9  itojun 			fatal("Invalid option specified, terminating");
    334  1.14  itojun 			/*NOTREACHED*/
    335   1.1  itojun 		}
    336   1.1  itojun 	}
    337   1.9  itojun 	argc -= optind;
    338   1.9  itojun 	argv += optind;
    339  1.21  itojun 	if (argc > 0) {
    340   1.9  itojun 		fatal("bogus extra arguments");
    341  1.21  itojun 		/*NOTREACHED*/
    342  1.21  itojun 	}
    343   1.9  itojun 
    344   1.1  itojun 	if (geteuid()) {
    345   1.1  itojun 		nflag = 1;
    346   1.1  itojun 		fprintf(stderr, "No kernel update is allowed\n");
    347   1.1  itojun 	}
    348  1.31  itojun 
    349  1.31  itojun 	if (dflag == 0) {
    350  1.31  itojun 		if (daemon(0, 0) < 0) {
    351  1.31  itojun 			fatal("daemon");
    352  1.31  itojun 			/*NOTREACHED*/
    353  1.31  itojun 		}
    354  1.31  itojun 	}
    355  1.31  itojun 
    356   1.1  itojun 	openlog(progname, LOG_NDELAY|LOG_PID, LOG_DAEMON);
    357   1.9  itojun 	logopened++;
    358  1.24  itojun 
    359  1.24  itojun 	if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL)
    360  1.24  itojun 		fatal("malloc");
    361  1.24  itojun 	memset(ripbuf, 0, RIP6_MAXMTU);
    362  1.24  itojun 	ripbuf->rip6_cmd = RIP6_RESPONSE;
    363  1.24  itojun 	ripbuf->rip6_vers = RIP6_VERSION;
    364  1.24  itojun 	ripbuf->rip6_res1[0] = 0;
    365  1.24  itojun 	ripbuf->rip6_res1[1] = 0;
    366  1.24  itojun 
    367   1.1  itojun 	init();
    368   1.1  itojun 	ifconfig();
    369   1.1  itojun 	for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
    370   1.1  itojun 		if (ifcp->ifc_index < 0) {
    371   1.1  itojun 			fprintf(stderr,
    372   1.1  itojun "No ifindex found at %s (no link-local address?)\n",
    373   1.1  itojun 				ifcp->ifc_name);
    374   1.1  itojun 			error++;
    375   1.1  itojun 		}
    376   1.1  itojun 	}
    377   1.1  itojun 	if (error)
    378   1.1  itojun 		exit(1);
    379  1.21  itojun 	if (loopifcp == NULL) {
    380   1.1  itojun 		fatal("No loopback found");
    381  1.21  itojun 		/*NOTREACHED*/
    382  1.21  itojun 	}
    383   1.1  itojun 	for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next)
    384   1.1  itojun 		ifrt(ifcp, 0);
    385   1.1  itojun 	filterconfig();
    386   1.1  itojun 	krtread(0);
    387   1.1  itojun 	if (dflag)
    388   1.1  itojun 		ifrtdump(0);
    389   1.1  itojun 
    390  1.33  itojun 	pidfile(NULL);
    391   1.1  itojun 
    392  1.21  itojun 	if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL) {
    393   1.1  itojun 		fatal("malloc");
    394  1.21  itojun 		/*NOTREACHED*/
    395  1.21  itojun 	}
    396  1.12  itojun 	memset(ripbuf, 0, RIP6_MAXMTU);
    397   1.1  itojun 	ripbuf->rip6_cmd = RIP6_RESPONSE;
    398   1.1  itojun 	ripbuf->rip6_vers = RIP6_VERSION;
    399   1.1  itojun 	ripbuf->rip6_res1[0] = 0;
    400   1.1  itojun 	ripbuf->rip6_res1[1] = 0;
    401   1.1  itojun 
    402  1.21  itojun 	if (signal(SIGALRM, sighandler) == SIG_ERR ||
    403  1.21  itojun 	    signal(SIGQUIT, sighandler) == SIG_ERR ||
    404  1.21  itojun 	    signal(SIGTERM, sighandler) == SIG_ERR ||
    405  1.21  itojun 	    signal(SIGUSR1, sighandler) == SIG_ERR ||
    406  1.21  itojun 	    signal(SIGHUP, sighandler) == SIG_ERR ||
    407  1.21  itojun 	    signal(SIGINT, sighandler) == SIG_ERR) {
    408  1.21  itojun 		fatal("signal");
    409  1.21  itojun 		/*NOTREACHED*/
    410  1.21  itojun 	}
    411   1.1  itojun 	/*
    412   1.1  itojun 	 * To avoid rip packet congestion (not on a cable but in this
    413   1.1  itojun 	 * process), wait for a moment to send the first RIP6_RESPONSE
    414   1.1  itojun 	 * packets.
    415   1.1  itojun 	 */
    416   1.1  itojun 	alarm(ripinterval(INIT_INTERVAL6));
    417   1.1  itojun 
    418   1.1  itojun 	for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
    419  1.29  itojun 		if (iff_find(ifcp, 'N'))
    420  1.29  itojun 			continue;
    421   1.1  itojun 		if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
    422   1.1  itojun 			sendrequest(ifcp);
    423   1.1  itojun 	}
    424   1.1  itojun 
    425   1.1  itojun 	syslog(LOG_INFO, "**** Started ****");
    426   1.1  itojun 	sigemptyset(&mask);
    427   1.1  itojun 	sigaddset(&mask, SIGALRM);
    428   1.1  itojun 	while (1) {
    429  1.24  itojun 		if (seenalrm) {
    430  1.24  itojun 			ripalarm();
    431  1.24  itojun 			seenalrm = 0;
    432  1.24  itojun 			continue;
    433  1.24  itojun 		}
    434  1.24  itojun 		if (seenquit) {
    435  1.24  itojun 			rtdexit();
    436  1.24  itojun 			seenquit = 0;
    437  1.24  itojun 			continue;
    438  1.24  itojun 		}
    439  1.24  itojun 		if (seenusr1) {
    440  1.24  itojun 			ifrtdump(SIGUSR1);
    441  1.24  itojun 			seenusr1 = 0;
    442  1.24  itojun 			continue;
    443  1.24  itojun 		}
    444  1.24  itojun 
    445  1.34  itojun 		memcpy(recvecp, sockvecp, fdmasks);
    446  1.34  itojun 		switch (select(maxfd + 1, recvecp, 0, 0, 0)) {
    447   1.1  itojun 		case -1:
    448  1.21  itojun 			if (errno != EINTR) {
    449  1.21  itojun 				fatal("select");
    450  1.21  itojun 				/*NOTREACHED*/
    451  1.21  itojun 			}
    452  1.21  itojun 			continue;
    453   1.1  itojun 		case 0:
    454   1.1  itojun 			continue;
    455   1.1  itojun 		default:
    456  1.34  itojun 			if (FD_ISSET(ripsock, recvecp)) {
    457   1.1  itojun 				sigprocmask(SIG_BLOCK, &mask, &omask);
    458   1.1  itojun 				riprecv();
    459   1.1  itojun 				sigprocmask(SIG_SETMASK, &omask, NULL);
    460   1.1  itojun 			}
    461  1.34  itojun 			if (FD_ISSET(rtsock, recvecp)) {
    462   1.1  itojun 				sigprocmask(SIG_BLOCK, &mask, &omask);
    463   1.1  itojun 				rtrecv();
    464   1.1  itojun 				sigprocmask(SIG_SETMASK, &omask, NULL);
    465   1.1  itojun 			}
    466   1.1  itojun 		}
    467   1.1  itojun 	}
    468   1.1  itojun }
    469   1.1  itojun 
    470  1.21  itojun void
    471  1.36     wiz sighandler(int signo)
    472  1.21  itojun {
    473  1.24  itojun 
    474  1.24  itojun 	switch (signo) {
    475  1.24  itojun 	case SIGALRM:
    476  1.24  itojun 		seenalrm++;
    477  1.24  itojun 		break;
    478  1.24  itojun 	case SIGQUIT:
    479  1.24  itojun 	case SIGTERM:
    480  1.24  itojun 		seenquit++;
    481  1.24  itojun 		break;
    482  1.24  itojun 	case SIGUSR1:
    483  1.24  itojun 	case SIGHUP:
    484  1.24  itojun 	case SIGINT:
    485  1.24  itojun 		seenusr1++;
    486  1.24  itojun 		break;
    487  1.24  itojun 	}
    488  1.21  itojun }
    489  1.21  itojun 
    490   1.1  itojun /*
    491   1.1  itojun  * gracefully exits after resetting sockopts.
    492   1.1  itojun  */
    493   1.7   itohy /* ARGSUSED */
    494   1.7   itohy void
    495  1.36     wiz rtdexit(void)
    496   1.1  itojun {
    497   1.1  itojun 	struct	riprt *rrt;
    498   1.1  itojun 
    499   1.1  itojun 	alarm(0);
    500   1.1  itojun 	for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
    501  1.13  itojun 		if (rrt->rrt_rflags & RRTF_AGGREGATE) {
    502   1.1  itojun 			delroute(&rrt->rrt_info, &rrt->rrt_gw);
    503   1.1  itojun 		}
    504   1.1  itojun 	}
    505   1.1  itojun 	close(ripsock);
    506   1.1  itojun 	close(rtsock);
    507   1.1  itojun 	syslog(LOG_INFO, "**** Terminated ****");
    508   1.1  itojun 	closelog();
    509   1.1  itojun 	exit(1);
    510   1.1  itojun }
    511   1.1  itojun 
    512   1.1  itojun /*
    513   1.1  itojun  * Called periodically:
    514   1.1  itojun  *	1. age out the learned route. remove it if necessary.
    515   1.1  itojun  *	2. submit RIP6_RESPONSE packets.
    516  1.24  itojun  * Invoked in every SUPPLY_INTERVAL6 (30) seconds.  I believe we don't have
    517   1.1  itojun  * to invoke this function in every 1 or 5 or 10 seconds only to age the
    518   1.1  itojun  * routes more precisely.
    519   1.1  itojun  */
    520   1.7   itohy /* ARGSUSED */
    521   1.7   itohy void
    522  1.36     wiz ripalarm(void)
    523   1.1  itojun {
    524   1.1  itojun 	struct	ifc *ifcp;
    525   1.1  itojun 	struct	riprt *rrt, *rrt_prev, *rrt_next;
    526   1.1  itojun 	time_t	t_lifetime, t_holddown;
    527   1.1  itojun 
    528   1.1  itojun 	/* age the RIP routes */
    529   1.1  itojun 	rrt_prev = 0;
    530   1.1  itojun 	t_lifetime = time(NULL) - RIP_LIFETIME;
    531   1.1  itojun 	t_holddown = t_lifetime - RIP_HOLDDOWN;
    532   1.1  itojun 	for (rrt = riprt; rrt; rrt = rrt_next) {
    533   1.1  itojun 		rrt_next = rrt->rrt_next;
    534   1.1  itojun 
    535   1.1  itojun 		if (rrt->rrt_t == 0) {
    536   1.1  itojun 			rrt_prev = rrt;
    537   1.1  itojun 			continue;
    538   1.1  itojun 		}
    539   1.1  itojun 		if (rrt->rrt_t < t_holddown) {
    540   1.1  itojun 			if (rrt_prev) {
    541   1.1  itojun 				rrt_prev->rrt_next = rrt->rrt_next;
    542   1.1  itojun 			} else {
    543   1.1  itojun 				riprt = rrt->rrt_next;
    544   1.1  itojun 			}
    545   1.1  itojun 			delroute(&rrt->rrt_info, &rrt->rrt_gw);
    546   1.1  itojun 			free(rrt);
    547   1.1  itojun 			continue;
    548   1.1  itojun 		}
    549   1.1  itojun 		if (rrt->rrt_t < t_lifetime)
    550   1.1  itojun 			rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
    551   1.1  itojun 		rrt_prev = rrt;
    552   1.1  itojun 	}
    553   1.1  itojun 	/* Supply updates */
    554   1.1  itojun 	for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
    555   1.1  itojun 		if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
    556   1.1  itojun 			ripsend(ifcp, &ifcp->ifc_ripsin, 0);
    557   1.1  itojun 	}
    558   1.1  itojun 	alarm(ripinterval(SUPPLY_INTERVAL6));
    559   1.1  itojun }
    560   1.1  itojun 
    561   1.7   itohy void
    562  1.36     wiz init(void)
    563   1.1  itojun {
    564  1.15  itojun 	int	i, int0, int255, error;
    565   1.1  itojun 	struct	addrinfo hints, *res;
    566   1.1  itojun 	char	port[10];
    567   1.1  itojun 
    568   1.1  itojun 	ifc = (struct ifc *)NULL;
    569   1.1  itojun 	nifc = 0;
    570   1.1  itojun 	nindex2ifc = 0;	/*initial guess*/
    571   1.1  itojun 	index2ifc = NULL;
    572   1.1  itojun 	snprintf(port, sizeof(port), "%d", RIP6_PORT);
    573   1.1  itojun 
    574   1.1  itojun 	memset(&hints, 0, sizeof(hints));
    575   1.1  itojun 	hints.ai_family = PF_INET6;
    576   1.1  itojun 	hints.ai_socktype = SOCK_DGRAM;
    577   1.1  itojun 	hints.ai_flags = AI_PASSIVE;
    578   1.1  itojun 	error = getaddrinfo(NULL, port, &hints, &res);
    579  1.21  itojun 	if (error) {
    580  1.18  itojun 		fatal("%s", gai_strerror(error));
    581  1.21  itojun 		/*NOTREACHED*/
    582  1.21  itojun 	}
    583  1.21  itojun 	if (res->ai_next) {
    584   1.1  itojun 		fatal(":: resolved to multiple address");
    585  1.21  itojun 		/*NOTREACHED*/
    586  1.21  itojun 	}
    587   1.1  itojun 
    588   1.1  itojun 	int0 = 0; int255 = 255;
    589   1.1  itojun 	ripsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    590  1.21  itojun 	if (ripsock < 0) {
    591   1.1  itojun 		fatal("rip socket");
    592  1.21  itojun 		/*NOTREACHED*/
    593  1.21  itojun 	}
    594  1.21  itojun 	if (bind(ripsock, res->ai_addr, res->ai_addrlen) < 0) {
    595   1.1  itojun 		fatal("rip bind");
    596  1.21  itojun 		/*NOTREACHED*/
    597  1.21  itojun 	}
    598   1.1  itojun 	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
    599  1.21  itojun 	    &int255, sizeof(int255)) < 0) {
    600   1.1  itojun 		fatal("rip IPV6_MULTICAST_HOPS");
    601  1.21  itojun 		/*NOTREACHED*/
    602  1.21  itojun 	}
    603   1.1  itojun 	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
    604  1.21  itojun 	    &int0, sizeof(int0)) < 0) {
    605   1.1  itojun 		fatal("rip IPV6_MULTICAST_LOOP");
    606  1.21  itojun 		/*NOTREACHED*/
    607  1.21  itojun 	}
    608  1.15  itojun 
    609   1.1  itojun 	i = 1;
    610  1.10  itojun #ifdef IPV6_RECVPKTINFO
    611  1.10  itojun 	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &i,
    612  1.21  itojun 	    sizeof(i)) < 0) {
    613  1.10  itojun 		fatal("rip IPV6_RECVPKTINFO");
    614  1.21  itojun 		/*NOTREACHED*/
    615  1.21  itojun 	}
    616  1.10  itojun #else  /* old adv. API */
    617  1.10  itojun 	if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_PKTINFO, &i,
    618  1.21  itojun 	    sizeof(i)) < 0) {
    619   1.1  itojun 		fatal("rip IPV6_PKTINFO");
    620  1.21  itojun 		/*NOTREACHED*/
    621  1.21  itojun 	}
    622  1.10  itojun #endif
    623   1.1  itojun 
    624   1.1  itojun 	memset(&hints, 0, sizeof(hints));
    625   1.1  itojun 	hints.ai_family = PF_INET6;
    626   1.1  itojun 	hints.ai_socktype = SOCK_DGRAM;
    627   1.1  itojun 	error = getaddrinfo(RIP6_DEST, port, &hints, &res);
    628  1.21  itojun 	if (error) {
    629  1.18  itojun 		fatal("%s", gai_strerror(error));
    630  1.21  itojun 		/*NOTREACHED*/
    631  1.21  itojun 	}
    632  1.21  itojun 	if (res->ai_next) {
    633   1.1  itojun 		fatal("%s resolved to multiple address", RIP6_DEST);
    634  1.21  itojun 		/*NOTREACHED*/
    635  1.21  itojun 	}
    636   1.1  itojun 	memcpy(&ripsin, res->ai_addr, res->ai_addrlen);
    637   1.1  itojun 
    638  1.29  itojun 	maxfd = ripsock;
    639   1.1  itojun 
    640   1.1  itojun 	if (nflag == 0) {
    641  1.21  itojun 		if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
    642   1.1  itojun 			fatal("route socket");
    643  1.21  itojun 			/*NOTREACHED*/
    644  1.21  itojun 		}
    645  1.29  itojun 		if (rtsock > maxfd)
    646  1.29  itojun 			maxfd = rtsock;
    647   1.1  itojun 	} else
    648   1.1  itojun 		rtsock = -1;	/*just for safety */
    649  1.34  itojun 
    650  1.34  itojun 	fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
    651  1.34  itojun 	if ((sockvecp = malloc(fdmasks)) == NULL) {
    652  1.34  itojun 		fatal("malloc");
    653  1.34  itojun 		/*NOTREACHED*/
    654  1.34  itojun 	}
    655  1.34  itojun 	if ((recvecp = malloc(fdmasks)) == NULL) {
    656  1.34  itojun 		fatal("malloc");
    657  1.34  itojun 		/*NOTREACHED*/
    658  1.34  itojun 	}
    659  1.34  itojun 	memset(sockvecp, 0, fdmasks);
    660  1.34  itojun 	FD_SET(ripsock, sockvecp);
    661  1.34  itojun 	if (rtsock >= 0)
    662  1.34  itojun 		FD_SET(rtsock, sockvecp);
    663   1.1  itojun }
    664   1.1  itojun 
    665  1.14  itojun #define	RIPSIZE(n) \
    666  1.14  itojun 	(sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6))
    667   1.1  itojun 
    668   1.1  itojun /*
    669   1.1  itojun  * ripflush flushes the rip datagram stored in the rip buffer
    670   1.1  itojun  */
    671   1.1  itojun static int nrt;
    672   1.1  itojun static struct netinfo6 *np;
    673   1.1  itojun 
    674   1.7   itohy void
    675  1.36     wiz ripflush(struct ifc *ifcp, struct sockaddr_in6 *sin6)
    676   1.1  itojun {
    677   1.1  itojun 	int i;
    678   1.1  itojun 	int error;
    679   1.1  itojun 
    680   1.1  itojun 	if (ifcp)
    681   1.1  itojun 		tracet(1, "Send(%s): info(%d) to %s.%d\n",
    682   1.1  itojun 			ifcp->ifc_name, nrt,
    683  1.29  itojun 			inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
    684   1.1  itojun 	else
    685   1.1  itojun 		tracet(1, "Send: info(%d) to %s.%d\n",
    686  1.29  itojun 			nrt, inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
    687   1.1  itojun 	if (dflag >= 2) {
    688   1.1  itojun 		np = ripbuf->rip6_nets;
    689   1.1  itojun 		for (i = 0; i < nrt; i++, np++) {
    690   1.1  itojun 			if (np->rip6_metric == NEXTHOP_METRIC) {
    691   1.8  itojun 				if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest))
    692  1.14  itojun 					trace(2, "    NextHop reset");
    693   1.1  itojun 				else {
    694   1.1  itojun 					trace(2, "    NextHop %s",
    695   1.1  itojun 						inet6_n2p(&np->rip6_dest));
    696   1.1  itojun 				}
    697   1.1  itojun 			} else {
    698   1.1  itojun 				trace(2, "    %s/%d[%d]",
    699   1.1  itojun 					inet6_n2p(&np->rip6_dest),
    700   1.1  itojun 					np->rip6_plen, np->rip6_metric);
    701   1.1  itojun 			}
    702   1.1  itojun 			if (np->rip6_tag) {
    703   1.1  itojun 				trace(2, "  tag=0x%04x",
    704   1.1  itojun 					ntohs(np->rip6_tag) & 0xffff);
    705   1.1  itojun 			}
    706   1.1  itojun 			trace(2, "\n");
    707   1.1  itojun 		}
    708   1.1  itojun 	}
    709  1.29  itojun 	error = sendpacket(sin6, RIPSIZE(nrt));
    710   1.1  itojun 	if (error == EAFNOSUPPORT) {
    711   1.1  itojun 		/* Protocol not supported */
    712   1.1  itojun 		tracet(1, "Could not send info to %s (%s): "
    713   1.1  itojun 			"set IFF_UP to 0\n",
    714   1.1  itojun 			ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
    715   1.1  itojun 		ifcp->ifc_flags &= ~IFF_UP;	/* As if down for AF_INET6 */
    716   1.1  itojun 	}
    717   1.1  itojun 	nrt = 0; np = ripbuf->rip6_nets;
    718   1.1  itojun }
    719   1.1  itojun 
    720   1.1  itojun /*
    721   1.1  itojun  * Generate RIP6_RESPONSE packets and send them.
    722   1.1  itojun  */
    723   1.7   itohy void
    724  1.36     wiz ripsend(struct ifc *ifcp, struct sockaddr_in6 *sin6, int flag)
    725   1.1  itojun {
    726   1.1  itojun 	struct	riprt *rrt;
    727   1.1  itojun 	struct	in6_addr *nh;	/* next hop */
    728  1.24  itojun 	int	maxrte;
    729   1.1  itojun 
    730   1.1  itojun 	if (ifcp == NULL) {
    731   1.1  itojun 		/*
    732   1.1  itojun 		 * Request from non-link local address is not
    733   1.1  itojun 		 * a regular route6d update.
    734   1.1  itojun 		 */
    735   1.1  itojun 		maxrte = (IFMINMTU - sizeof(struct ip6_hdr) -
    736   1.1  itojun 				sizeof(struct udphdr) -
    737   1.1  itojun 				sizeof(struct rip6) + sizeof(struct netinfo6)) /
    738   1.1  itojun 				sizeof(struct netinfo6);
    739   1.1  itojun 		nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
    740   1.1  itojun 		for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
    741  1.13  itojun 			if (rrt->rrt_rflags & RRTF_NOADVERTISE)
    742   1.1  itojun 				continue;
    743   1.1  itojun 			/* Put the route to the buffer */
    744   1.1  itojun 			*np = rrt->rrt_info;
    745   1.1  itojun 			np++; nrt++;
    746   1.1  itojun 			if (nrt == maxrte) {
    747  1.29  itojun 				ripflush(NULL, sin6);
    748   1.1  itojun 				nh = NULL;
    749   1.1  itojun 			}
    750   1.1  itojun 		}
    751   1.1  itojun 		if (nrt)	/* Send last packet */
    752  1.29  itojun 			ripflush(NULL, sin6);
    753   1.1  itojun 		return;
    754   1.1  itojun 	}
    755   1.1  itojun 
    756  1.13  itojun 	if ((flag & RRTF_SENDANYWAY) == 0 &&
    757   1.1  itojun 	    (qflag || (ifcp->ifc_flags & IFF_LOOPBACK)))
    758   1.1  itojun 		return;
    759  1.24  itojun 
    760  1.24  itojun 	/* -N: no use */
    761   1.1  itojun 	if (iff_find(ifcp, 'N') != NULL)
    762   1.1  itojun 		return;
    763  1.24  itojun 
    764  1.24  itojun 	/* -T: generate default route only */
    765   1.1  itojun 	if (iff_find(ifcp, 'T') != NULL) {
    766   1.1  itojun 		struct netinfo6 rrt_info;
    767   1.1  itojun 		memset(&rrt_info, 0, sizeof(struct netinfo6));
    768   1.1  itojun 		rrt_info.rip6_dest = in6addr_any;
    769   1.1  itojun 		rrt_info.rip6_plen = 0;
    770   1.1  itojun 		rrt_info.rip6_metric = 1;
    771  1.29  itojun 		rrt_info.rip6_metric += ifcp->ifc_metric;
    772   1.1  itojun 		rrt_info.rip6_tag = htons(routetag & 0xffff);
    773   1.1  itojun 		np = ripbuf->rip6_nets;
    774   1.1  itojun 		*np = rrt_info;
    775   1.1  itojun 		nrt = 1;
    776  1.29  itojun 		ripflush(ifcp, sin6);
    777   1.1  itojun 		return;
    778   1.1  itojun 	}
    779  1.24  itojun 
    780   1.1  itojun 	maxrte = (ifcp->ifc_mtu - sizeof(struct ip6_hdr) -
    781   1.1  itojun 			sizeof(struct udphdr) -
    782   1.1  itojun 			sizeof(struct rip6) + sizeof(struct netinfo6)) /
    783   1.1  itojun 			sizeof(struct netinfo6);
    784  1.24  itojun 
    785   1.1  itojun 	nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
    786   1.1  itojun 	for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
    787  1.13  itojun 		if (rrt->rrt_rflags & RRTF_NOADVERTISE)
    788   1.1  itojun 			continue;
    789  1.24  itojun 
    790  1.24  itojun 		/* Need to check filter here */
    791  1.24  itojun 		if (out_filter(rrt, ifcp) == 0)
    792   1.1  itojun 			continue;
    793  1.24  itojun 
    794   1.1  itojun 		/* Check split horizon and other conditions */
    795   1.1  itojun 		if (tobeadv(rrt, ifcp) == 0)
    796   1.1  itojun 			continue;
    797  1.24  itojun 
    798   1.1  itojun 		/* Only considers the routes with flag if specified */
    799  1.13  itojun 		if ((flag & RRTF_CHANGED) &&
    800  1.13  itojun 		    (rrt->rrt_rflags & RRTF_CHANGED) == 0)
    801   1.1  itojun 			continue;
    802  1.24  itojun 
    803   1.1  itojun 		/* Check nexthop */
    804   1.1  itojun 		if (rrt->rrt_index == ifcp->ifc_index &&
    805   1.8  itojun 		    !IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_gw) &&
    806  1.13  itojun 		    (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR) == 0) {
    807   1.1  itojun 			if (nh == NULL || !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw)) {
    808   1.1  itojun 				if (nrt == maxrte - 2)
    809  1.29  itojun 					ripflush(ifcp, sin6);
    810   1.1  itojun 				np->rip6_dest = rrt->rrt_gw;
    811   1.1  itojun 				if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest))
    812   1.1  itojun 					SET_IN6_LINKLOCAL_IFINDEX(np->rip6_dest, 0);
    813   1.1  itojun 				np->rip6_plen = 0;
    814   1.1  itojun 				np->rip6_tag = 0;
    815   1.1  itojun 				np->rip6_metric = NEXTHOP_METRIC;
    816   1.1  itojun 				nh = &rrt->rrt_gw;
    817   1.1  itojun 				np++; nrt++;
    818   1.1  itojun 			}
    819   1.1  itojun 		} else if (nh && (rrt->rrt_index != ifcp->ifc_index ||
    820   1.1  itojun 			          !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw) ||
    821  1.13  itojun 				  rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)) {
    822   1.1  itojun 			/* Reset nexthop */
    823   1.1  itojun 			if (nrt == maxrte - 2)
    824  1.29  itojun 				ripflush(ifcp, sin6);
    825   1.1  itojun 			memset(np, 0, sizeof(struct netinfo6));
    826   1.1  itojun 			np->rip6_metric = NEXTHOP_METRIC;
    827   1.1  itojun 			nh = NULL;
    828   1.1  itojun 			np++; nrt++;
    829   1.1  itojun 		}
    830  1.24  itojun 
    831   1.1  itojun 		/* Put the route to the buffer */
    832   1.1  itojun 		*np = rrt->rrt_info;
    833   1.1  itojun 		np++; nrt++;
    834   1.1  itojun 		if (nrt == maxrte) {
    835  1.29  itojun 			ripflush(ifcp, sin6);
    836   1.1  itojun 			nh = NULL;
    837   1.1  itojun 		}
    838   1.1  itojun 	}
    839   1.1  itojun 	if (nrt)	/* Send last packet */
    840  1.29  itojun 		ripflush(ifcp, sin6);
    841   1.1  itojun }
    842   1.1  itojun 
    843   1.1  itojun /*
    844  1.24  itojun  * outbound filter logic, per-route/interface.
    845  1.24  itojun  */
    846  1.24  itojun int
    847  1.36     wiz out_filter(struct riprt *rrt, struct ifc *ifcp)
    848  1.24  itojun {
    849  1.24  itojun 	struct iff *iffp;
    850  1.24  itojun 	struct in6_addr ia;
    851  1.24  itojun 	int ok;
    852  1.24  itojun 
    853  1.24  itojun 	/*
    854  1.24  itojun 	 * -A: filter out less specific routes, if we have aggregated
    855  1.24  itojun 	 * route configured.
    856  1.24  itojun 	 */
    857  1.24  itojun 	for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
    858  1.24  itojun 		if (iffp->iff_type != 'A')
    859  1.24  itojun 			continue;
    860  1.24  itojun 		if (rrt->rrt_info.rip6_plen <= iffp->iff_plen)
    861  1.24  itojun 			continue;
    862  1.24  itojun 		ia = rrt->rrt_info.rip6_dest;
    863  1.24  itojun 		applyplen(&ia, iffp->iff_plen);
    864  1.24  itojun 		if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr))
    865  1.24  itojun 			return 0;
    866  1.24  itojun 	}
    867  1.24  itojun 
    868  1.24  itojun 	/*
    869  1.24  itojun 	 * if it is an aggregated route, advertise it only to the
    870  1.24  itojun 	 * interfaces specified on -A.
    871  1.24  itojun 	 */
    872  1.24  itojun 	if ((rrt->rrt_rflags & RRTF_AGGREGATE) != 0) {
    873  1.24  itojun 		ok = 0;
    874  1.24  itojun 		for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
    875  1.24  itojun 			if (iffp->iff_type != 'A')
    876  1.24  itojun 				continue;
    877  1.24  itojun 			if (rrt->rrt_info.rip6_plen == iffp->iff_plen &&
    878  1.24  itojun 			    IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
    879  1.24  itojun 			    &iffp->iff_addr)) {
    880  1.24  itojun 				ok = 1;
    881  1.24  itojun 				break;
    882  1.24  itojun 			}
    883  1.24  itojun 		}
    884  1.24  itojun 		if (!ok)
    885  1.24  itojun 			return 0;
    886  1.24  itojun 	}
    887  1.24  itojun 
    888  1.24  itojun 	/*
    889  1.24  itojun 	 * -O: advertise only if prefix matches the configured prefix.
    890  1.24  itojun 	 */
    891  1.24  itojun 	if (iff_find(ifcp, 'O')) {
    892  1.24  itojun 		ok = 0;
    893  1.24  itojun 		for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
    894  1.24  itojun 			if (iffp->iff_type != 'O')
    895  1.24  itojun 				continue;
    896  1.24  itojun 			if (rrt->rrt_info.rip6_plen < iffp->iff_plen)
    897  1.24  itojun 				continue;
    898  1.24  itojun 			ia = rrt->rrt_info.rip6_dest;
    899  1.24  itojun 			applyplen(&ia, iffp->iff_plen);
    900  1.24  itojun 			if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
    901  1.24  itojun 				ok = 1;
    902  1.24  itojun 				break;
    903  1.24  itojun 			}
    904  1.24  itojun 		}
    905  1.24  itojun 		if (!ok)
    906  1.24  itojun 			return 0;
    907  1.24  itojun 	}
    908  1.24  itojun 
    909  1.24  itojun 	/* the prefix should be advertised */
    910  1.24  itojun 	return 1;
    911  1.24  itojun }
    912  1.24  itojun 
    913  1.24  itojun /*
    914   1.1  itojun  * Determine if the route is to be advertised on the specified interface.
    915   1.1  itojun  * It checks options specified in the arguments and the split horizon rule.
    916   1.1  itojun  */
    917   1.7   itohy int
    918  1.36     wiz tobeadv(struct riprt *rrt, struct ifc *ifcp)
    919   1.1  itojun {
    920   1.7   itohy 
    921   1.1  itojun 	/* Special care for static routes */
    922   1.1  itojun 	if (rrt->rrt_flags & RTF_STATIC) {
    923  1.11  itojun 		/* XXX don't advertise reject/blackhole routes */
    924  1.11  itojun 		if (rrt->rrt_flags & (RTF_REJECT | RTF_BLACKHOLE))
    925  1.11  itojun 			return 0;
    926  1.11  itojun 
    927   1.1  itojun 		if (Sflag)	/* Yes, advertise it anyway */
    928   1.1  itojun 			return 1;
    929   1.1  itojun 		if (sflag && rrt->rrt_index != ifcp->ifc_index)
    930   1.1  itojun 			return 1;
    931   1.1  itojun 		return 0;
    932   1.1  itojun 	}
    933   1.1  itojun 	/* Regular split horizon */
    934   1.1  itojun 	if (hflag == 0 && rrt->rrt_index == ifcp->ifc_index)
    935   1.1  itojun 		return 0;
    936   1.1  itojun 	return 1;
    937   1.1  itojun }
    938   1.1  itojun 
    939   1.1  itojun /*
    940   1.1  itojun  * Send a rip packet actually.
    941   1.1  itojun  */
    942   1.7   itohy int
    943  1.36     wiz sendpacket(struct sockaddr_in6 *sin6, int len)
    944   1.1  itojun {
    945   1.1  itojun 	struct msghdr m;
    946   1.1  itojun 	struct cmsghdr *cm;
    947   1.1  itojun 	struct iovec iov[2];
    948   1.1  itojun 	u_char cmsgbuf[256];
    949   1.1  itojun 	struct in6_pktinfo *pi;
    950  1.24  itojun 	int idx;
    951   1.1  itojun 	struct sockaddr_in6 sincopy;
    952   1.1  itojun 
    953   1.1  itojun 	/* do not overwrite the given sin */
    954  1.29  itojun 	sincopy = *sin6;
    955  1.29  itojun 	sin6 = &sincopy;
    956   1.1  itojun 
    957  1.32  itojun 	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
    958  1.32  itojun 	    IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
    959  1.32  itojun 		/* XXX: do not mix the interface index and link index */
    960  1.29  itojun 		idx = IN6_LINKLOCAL_IFINDEX(sin6->sin6_addr);
    961  1.29  itojun 		SET_IN6_LINKLOCAL_IFINDEX(sin6->sin6_addr, 0);
    962  1.32  itojun 		sin6->sin6_scope_id = idx;
    963   1.1  itojun 	} else
    964  1.24  itojun 		idx = 0;
    965   1.1  itojun 
    966  1.29  itojun 	m.msg_name = (caddr_t)sin6;
    967  1.29  itojun 	m.msg_namelen = sizeof(*sin6);
    968   1.1  itojun 	iov[0].iov_base = (caddr_t)ripbuf;
    969   1.1  itojun 	iov[0].iov_len = len;
    970   1.1  itojun 	m.msg_iov = iov;
    971   1.1  itojun 	m.msg_iovlen = 1;
    972  1.24  itojun 	if (!idx) {
    973   1.1  itojun 		m.msg_control = NULL;
    974   1.1  itojun 		m.msg_controllen = 0;
    975   1.1  itojun 	} else {
    976   1.1  itojun 		memset(cmsgbuf, 0, sizeof(cmsgbuf));
    977   1.1  itojun 		cm = (struct cmsghdr *)cmsgbuf;
    978   1.1  itojun 		m.msg_control = (caddr_t)cm;
    979   1.1  itojun 		m.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
    980   1.1  itojun 
    981   1.1  itojun 		cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
    982   1.1  itojun 		cm->cmsg_level = IPPROTO_IPV6;
    983   1.1  itojun 		cm->cmsg_type = IPV6_PKTINFO;
    984   1.1  itojun 		pi = (struct in6_pktinfo *)CMSG_DATA(cm);
    985   1.1  itojun 		memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*::*/
    986  1.24  itojun 		pi->ipi6_ifindex = idx;
    987   1.1  itojun 	}
    988   1.1  itojun 
    989   1.1  itojun 	if (sendmsg(ripsock, &m, 0 /*MSG_DONTROUTE*/) < 0) {
    990   1.1  itojun 		trace(1, "sendmsg: %s\n", strerror(errno));
    991   1.1  itojun 		return errno;
    992   1.1  itojun 	}
    993  1.15  itojun 
    994   1.1  itojun 	return 0;
    995   1.1  itojun }
    996   1.1  itojun 
    997   1.1  itojun /*
    998  1.24  itojun  * Receive and process RIP packets.  Update the routes/kernel forwarding
    999   1.1  itojun  * table if necessary.
   1000   1.1  itojun  */
   1001   1.7   itohy void
   1002  1.36     wiz riprecv(void)
   1003   1.1  itojun {
   1004   1.1  itojun 	struct	ifc *ifcp, *ic;
   1005   1.1  itojun 	struct	sockaddr_in6 fsock;
   1006   1.1  itojun 	struct	in6_addr nh;	/* next hop */
   1007   1.1  itojun 	struct	rip6 *rp;
   1008   1.1  itojun 	struct	netinfo6 *np, *nq;
   1009   1.1  itojun 	struct	riprt *rrt;
   1010  1.24  itojun 	int	len, nn, need_trigger, idx;
   1011   1.1  itojun 	char	buf[4 * RIP6_MAXMTU];
   1012   1.1  itojun 	time_t	t;
   1013   1.1  itojun 	struct msghdr m;
   1014   1.1  itojun 	struct cmsghdr *cm;
   1015   1.1  itojun 	struct iovec iov[2];
   1016   1.1  itojun 	u_char cmsgbuf[256];
   1017   1.1  itojun 	struct in6_pktinfo *pi;
   1018   1.1  itojun 	struct iff *iffp;
   1019   1.1  itojun 	struct in6_addr ia;
   1020   1.1  itojun 	int ok;
   1021  1.24  itojun 	time_t t_half_lifetime;
   1022   1.1  itojun 
   1023   1.1  itojun 	need_trigger = 0;
   1024  1.15  itojun 
   1025   1.1  itojun 	m.msg_name = (caddr_t)&fsock;
   1026   1.1  itojun 	m.msg_namelen = sizeof(fsock);
   1027   1.1  itojun 	iov[0].iov_base = (caddr_t)buf;
   1028   1.1  itojun 	iov[0].iov_len = sizeof(buf);
   1029   1.1  itojun 	m.msg_iov = iov;
   1030   1.1  itojun 	m.msg_iovlen = 1;
   1031   1.1  itojun 	cm = (struct cmsghdr *)cmsgbuf;
   1032   1.1  itojun 	m.msg_control = (caddr_t)cm;
   1033   1.1  itojun 	m.msg_controllen = sizeof(cmsgbuf);
   1034  1.21  itojun 	if ((len = recvmsg(ripsock, &m, 0)) < 0) {
   1035   1.1  itojun 		fatal("recvmsg");
   1036  1.21  itojun 		/*NOTREACHED*/
   1037  1.21  itojun 	}
   1038  1.24  itojun 	idx = 0;
   1039   1.1  itojun 	for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&m);
   1040   1.1  itojun 	     cm;
   1041   1.1  itojun 	     cm = (struct cmsghdr *)CMSG_NXTHDR(&m, cm)) {
   1042  1.24  itojun 		if (cm->cmsg_level == IPPROTO_IPV6 &&
   1043  1.24  itojun 		    cm->cmsg_type == IPV6_PKTINFO) {
   1044   1.1  itojun 			pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
   1045  1.24  itojun 			idx = pi->ipi6_ifindex;
   1046   1.1  itojun 			break;
   1047   1.1  itojun 		}
   1048   1.1  itojun 	}
   1049  1.24  itojun 	if (idx && IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr))
   1050  1.24  itojun 		SET_IN6_LINKLOCAL_IFINDEX(fsock.sin6_addr, idx);
   1051   1.1  itojun 
   1052   1.1  itojun 	nh = fsock.sin6_addr;
   1053   1.1  itojun 	nn = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
   1054   1.1  itojun 		sizeof(struct netinfo6);
   1055   1.1  itojun 	rp = (struct rip6 *)buf;
   1056   1.1  itojun 	np = rp->rip6_nets;
   1057   1.1  itojun 
   1058   1.1  itojun 	if (rp->rip6_vers !=  RIP6_VERSION) {
   1059   1.1  itojun 		trace(1, "Incorrect RIP version %d\n", rp->rip6_vers);
   1060   1.1  itojun 		return;
   1061   1.1  itojun 	}
   1062   1.1  itojun 	if (rp->rip6_cmd == RIP6_REQUEST) {
   1063  1.24  itojun 		if (idx && idx < nindex2ifc) {
   1064  1.24  itojun 			ifcp = index2ifc[idx];
   1065   1.1  itojun 			riprequest(ifcp, np, nn, &fsock);
   1066   1.1  itojun 		} else {
   1067   1.1  itojun 			riprequest(NULL, np, nn, &fsock);
   1068   1.1  itojun 		}
   1069   1.1  itojun 		return;
   1070   1.1  itojun 	}
   1071   1.1  itojun 
   1072   1.1  itojun 	if (!IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr)) {
   1073   1.1  itojun 		trace(1, "Packets from non-ll addr: %s\n",
   1074  1.24  itojun 		    inet6_n2p(&fsock.sin6_addr));
   1075   1.1  itojun 		return;		/* Ignore packets from non-link-local addr */
   1076   1.1  itojun 	}
   1077  1.24  itojun 	idx = IN6_LINKLOCAL_IFINDEX(fsock.sin6_addr);
   1078  1.24  itojun 	ifcp = (idx < nindex2ifc) ? index2ifc[idx] : NULL;
   1079   1.1  itojun 	if (!ifcp) {
   1080  1.24  itojun 		trace(1, "Packets to unknown interface index %d\n", idx);
   1081   1.1  itojun 		return;		/* Ignore it */
   1082   1.1  itojun 	}
   1083   1.1  itojun 	if (IN6_ARE_ADDR_EQUAL(&ifcp->ifc_mylladdr, &fsock.sin6_addr))
   1084   1.1  itojun 		return;		/* The packet is from me; ignore */
   1085   1.1  itojun 	if (rp->rip6_cmd != RIP6_RESPONSE) {
   1086   1.1  itojun 		trace(1, "Invalid command %d\n", rp->rip6_cmd);
   1087   1.1  itojun 		return;
   1088   1.1  itojun 	}
   1089  1.24  itojun 
   1090  1.24  itojun 	/* -N: no use */
   1091   1.1  itojun 	if (iff_find(ifcp, 'N') != NULL)
   1092   1.1  itojun 		return;
   1093  1.24  itojun 
   1094   1.1  itojun 	tracet(1, "Recv(%s): from %s.%d info(%d)\n",
   1095  1.24  itojun 	    ifcp->ifc_name, inet6_n2p(&nh), ntohs(fsock.sin6_port), nn);
   1096   1.1  itojun 
   1097   1.1  itojun 	t = time(NULL);
   1098  1.24  itojun 	t_half_lifetime = t - (RIP_LIFETIME/2);
   1099   1.1  itojun 	for (; nn; nn--, np++) {
   1100   1.1  itojun 		if (np->rip6_metric == NEXTHOP_METRIC) {
   1101   1.1  itojun 			/* modify neighbor address */
   1102   1.1  itojun 			if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
   1103   1.1  itojun 				nh = np->rip6_dest;
   1104  1.24  itojun 				SET_IN6_LINKLOCAL_IFINDEX(nh, idx);
   1105   1.1  itojun 				trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
   1106   1.8  itojun 			} else if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest)) {
   1107   1.1  itojun 				nh = fsock.sin6_addr;
   1108   1.1  itojun 				trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
   1109   1.1  itojun 			} else {
   1110   1.1  itojun 				nh = fsock.sin6_addr;
   1111   1.1  itojun 				trace(1, "\tInvalid Nexthop: %s\n",
   1112  1.24  itojun 				    inet6_n2p(&np->rip6_dest));
   1113   1.1  itojun 			}
   1114   1.1  itojun 			continue;
   1115   1.1  itojun 		}
   1116   1.1  itojun 		if (IN6_IS_ADDR_MULTICAST(&np->rip6_dest)) {
   1117   1.1  itojun 			trace(1, "\tMulticast netinfo6: %s/%d [%d]\n",
   1118   1.1  itojun 				inet6_n2p(&np->rip6_dest),
   1119   1.1  itojun 				np->rip6_plen, np->rip6_metric);
   1120   1.1  itojun 			continue;
   1121   1.1  itojun 		}
   1122   1.1  itojun 		if (IN6_IS_ADDR_LOOPBACK(&np->rip6_dest)) {
   1123   1.1  itojun 			trace(1, "\tLoopback netinfo6: %s/%d [%d]\n",
   1124   1.1  itojun 				inet6_n2p(&np->rip6_dest),
   1125   1.1  itojun 				np->rip6_plen, np->rip6_metric);
   1126   1.1  itojun 			continue;
   1127   1.1  itojun 		}
   1128   1.1  itojun 		if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
   1129   1.1  itojun 			trace(1, "\tLink Local netinfo6: %s/%d [%d]\n",
   1130   1.1  itojun 				inet6_n2p(&np->rip6_dest),
   1131   1.1  itojun 				np->rip6_plen, np->rip6_metric);
   1132   1.1  itojun 			continue;
   1133   1.1  itojun 		}
   1134   1.1  itojun 		/* may need to pass sitelocal prefix in some case, however*/
   1135   1.1  itojun 		if (IN6_IS_ADDR_SITELOCAL(&np->rip6_dest) && !lflag) {
   1136   1.1  itojun 			trace(1, "\tSite Local netinfo6: %s/%d [%d]\n",
   1137   1.1  itojun 				inet6_n2p(&np->rip6_dest),
   1138   1.1  itojun 				np->rip6_plen, np->rip6_metric);
   1139   1.1  itojun 			continue;
   1140   1.1  itojun 		}
   1141   1.1  itojun 		trace(2, "\tnetinfo6: %s/%d [%d]",
   1142   1.1  itojun 			inet6_n2p(&np->rip6_dest),
   1143   1.1  itojun 			np->rip6_plen, np->rip6_metric);
   1144   1.1  itojun 		if (np->rip6_tag)
   1145   1.1  itojun 			trace(2, "  tag=0x%04x", ntohs(np->rip6_tag) & 0xffff);
   1146  1.14  itojun 		if (dflag >= 2) {
   1147  1.14  itojun 			ia = np->rip6_dest;
   1148  1.14  itojun 			applyplen(&ia, np->rip6_plen);
   1149  1.14  itojun 			if (!IN6_ARE_ADDR_EQUAL(&ia, &np->rip6_dest))
   1150  1.14  itojun 				trace(2, " [junk outside prefix]");
   1151  1.14  itojun 		}
   1152   1.1  itojun 
   1153  1.24  itojun 		/*
   1154  1.24  itojun 		 * -L: listen only if the prefix matches the configuration
   1155  1.24  itojun 		 */
   1156   1.1  itojun 		ok = 1;		/* if there's no L filter, it is ok */
   1157   1.1  itojun 		for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
   1158   1.1  itojun 			if (iffp->iff_type != 'L')
   1159   1.1  itojun 				continue;
   1160   1.1  itojun 			ok = 0;
   1161   1.1  itojun 			if (np->rip6_plen < iffp->iff_plen)
   1162   1.1  itojun 				continue;
   1163   1.1  itojun 			/* special rule: ::/0 means default, not "in /0" */
   1164   1.1  itojun 			if (iffp->iff_plen == 0 && np->rip6_plen > 0)
   1165   1.1  itojun 				continue;
   1166   1.1  itojun 			ia = np->rip6_dest;
   1167   1.1  itojun 			applyplen(&ia, iffp->iff_plen);
   1168   1.1  itojun 			if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
   1169   1.1  itojun 				ok = 1;
   1170   1.1  itojun 				break;
   1171   1.1  itojun 			}
   1172   1.1  itojun 		}
   1173   1.1  itojun 		if (!ok) {
   1174   1.1  itojun 			trace(2, "  (filtered)\n");
   1175   1.1  itojun 			continue;
   1176   1.1  itojun 		}
   1177   1.1  itojun 
   1178   1.1  itojun 		trace(2, "\n");
   1179   1.1  itojun 		np->rip6_metric++;
   1180   1.1  itojun 		np->rip6_metric += ifcp->ifc_metric;
   1181   1.1  itojun 		if (np->rip6_metric > HOPCNT_INFINITY6)
   1182   1.1  itojun 			np->rip6_metric = HOPCNT_INFINITY6;
   1183   1.1  itojun 
   1184   1.1  itojun 		applyplen(&np->rip6_dest, np->rip6_plen);
   1185  1.24  itojun 		if ((rrt = rtsearch(np, NULL)) != NULL) {
   1186   1.1  itojun 			if (rrt->rrt_t == 0)
   1187   1.1  itojun 				continue;	/* Intf route has priority */
   1188   1.1  itojun 			nq = &rrt->rrt_info;
   1189   1.1  itojun 			if (nq->rip6_metric > np->rip6_metric) {
   1190   1.1  itojun 				if (rrt->rrt_index == ifcp->ifc_index &&
   1191   1.1  itojun 				    IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
   1192   1.1  itojun 					/* Small metric from the same gateway */
   1193   1.1  itojun 					nq->rip6_metric = np->rip6_metric;
   1194   1.1  itojun 				} else {
   1195   1.1  itojun 					/* Better route found */
   1196   1.1  itojun 					rrt->rrt_index = ifcp->ifc_index;
   1197   1.1  itojun 					/* Update routing table */
   1198   1.1  itojun 					delroute(nq, &rrt->rrt_gw);
   1199   1.1  itojun 					rrt->rrt_gw = nh;
   1200   1.1  itojun 					*nq = *np;
   1201   1.1  itojun 					addroute(rrt, &nh, ifcp);
   1202   1.1  itojun 				}
   1203  1.13  itojun 				rrt->rrt_rflags |= RRTF_CHANGED;
   1204   1.1  itojun 				rrt->rrt_t = t;
   1205   1.1  itojun 				need_trigger = 1;
   1206   1.1  itojun 			} else if (nq->rip6_metric < np->rip6_metric &&
   1207   1.1  itojun 				   rrt->rrt_index == ifcp->ifc_index &&
   1208   1.1  itojun 				   IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
   1209   1.1  itojun 				/* Got worse route from same gw */
   1210   1.1  itojun 				nq->rip6_metric = np->rip6_metric;
   1211   1.1  itojun 				rrt->rrt_t = t;
   1212  1.13  itojun 				rrt->rrt_rflags |= RRTF_CHANGED;
   1213   1.1  itojun 				need_trigger = 1;
   1214   1.1  itojun 			} else if (nq->rip6_metric == np->rip6_metric &&
   1215   1.1  itojun 				   np->rip6_metric < HOPCNT_INFINITY6) {
   1216  1.24  itojun 				if (rrt->rrt_index == ifcp->ifc_index &&
   1217  1.24  itojun 				   IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
   1218  1.24  itojun 					/* same metric, same route from same gw */
   1219  1.24  itojun 					rrt->rrt_t = t;
   1220  1.24  itojun 				} else if (rrt->rrt_t < t_half_lifetime) {
   1221  1.24  itojun 					/* Better route found */
   1222  1.24  itojun 					rrt->rrt_index = ifcp->ifc_index;
   1223  1.24  itojun 					/* Update routing table */
   1224  1.24  itojun 					delroute(nq, &rrt->rrt_gw);
   1225  1.24  itojun 					rrt->rrt_gw = nh;
   1226  1.24  itojun 					*nq = *np;
   1227  1.24  itojun 					addroute(rrt, &nh, ifcp);
   1228  1.24  itojun 					rrt->rrt_rflags |= RRTF_CHANGED;
   1229  1.24  itojun 					rrt->rrt_t = t;
   1230  1.24  itojun 				}
   1231   1.1  itojun 			}
   1232   1.1  itojun 			/*
   1233   1.1  itojun 			 * if nq->rip6_metric == HOPCNT_INFINITY6 then
   1234  1.24  itojun 			 * do not update age value.  Do nothing.
   1235   1.1  itojun 			 */
   1236   1.1  itojun 		} else if (np->rip6_metric < HOPCNT_INFINITY6) {
   1237   1.1  itojun 			/* Got a new valid route */
   1238  1.21  itojun 			if ((rrt = MALLOC(struct riprt)) == NULL) {
   1239   1.1  itojun 				fatal("malloc: struct riprt");
   1240  1.21  itojun 				/*NOTREACHED*/
   1241  1.21  itojun 			}
   1242  1.12  itojun 			memset(rrt, 0, sizeof(*rrt));
   1243   1.1  itojun 			nq = &rrt->rrt_info;
   1244   1.1  itojun 
   1245   1.1  itojun 			rrt->rrt_same = NULL;
   1246   1.1  itojun 			rrt->rrt_index = ifcp->ifc_index;
   1247   1.1  itojun 			rrt->rrt_flags = RTF_UP|RTF_GATEWAY;
   1248   1.1  itojun 			rrt->rrt_gw = nh;
   1249   1.1  itojun 			*nq = *np;
   1250   1.1  itojun 			applyplen(&nq->rip6_dest, nq->rip6_plen);
   1251   1.6  itojun 			if (nq->rip6_plen == sizeof(struct in6_addr) * 8)
   1252   1.1  itojun 				rrt->rrt_flags |= RTF_HOST;
   1253   1.1  itojun 
   1254   1.1  itojun 			/* Put the route to the list */
   1255   1.1  itojun 			rrt->rrt_next = riprt;
   1256   1.1  itojun 			riprt = rrt;
   1257   1.1  itojun 			/* Update routing table */
   1258   1.1  itojun 			addroute(rrt, &nh, ifcp);
   1259  1.13  itojun 			rrt->rrt_rflags |= RRTF_CHANGED;
   1260   1.1  itojun 			need_trigger = 1;
   1261   1.1  itojun 			rrt->rrt_t = t;
   1262   1.1  itojun 		}
   1263   1.1  itojun 	}
   1264   1.1  itojun 	/* XXX need to care the interval between triggered updates */
   1265   1.1  itojun 	if (need_trigger) {
   1266   1.1  itojun 		if (nextalarm > time(NULL) + RIP_TRIG_INT6_MAX) {
   1267   1.1  itojun 			for (ic = ifc; ic; ic = ic->ifc_next) {
   1268   1.1  itojun 				if (ifcp->ifc_index == ic->ifc_index)
   1269   1.1  itojun 					continue;
   1270   1.1  itojun 				if (ic->ifc_flags & IFF_UP)
   1271   1.1  itojun 					ripsend(ic, &ic->ifc_ripsin,
   1272  1.13  itojun 						RRTF_CHANGED);
   1273   1.1  itojun 			}
   1274   1.1  itojun 		}
   1275   1.1  itojun 		/* Reset the flag */
   1276   1.1  itojun 		for (rrt = riprt; rrt; rrt = rrt->rrt_next)
   1277  1.13  itojun 			rrt->rrt_rflags &= ~RRTF_CHANGED;
   1278   1.1  itojun 	}
   1279   1.1  itojun }
   1280   1.1  itojun 
   1281   1.1  itojun /*
   1282   1.1  itojun  * Send all routes request packet to the specified interface.
   1283   1.1  itojun  */
   1284   1.7   itohy void
   1285  1.36     wiz sendrequest(struct ifc *ifcp)
   1286   1.1  itojun {
   1287   1.1  itojun 	struct netinfo6 *np;
   1288   1.1  itojun 	int error;
   1289   1.1  itojun 
   1290   1.1  itojun 	if (ifcp->ifc_flags & IFF_LOOPBACK)
   1291   1.1  itojun 		return;
   1292   1.1  itojun 	ripbuf->rip6_cmd = RIP6_REQUEST;
   1293   1.1  itojun 	np = ripbuf->rip6_nets;
   1294   1.1  itojun 	memset(np, 0, sizeof(struct netinfo6));
   1295   1.1  itojun 	np->rip6_metric = HOPCNT_INFINITY6;
   1296   1.1  itojun 	tracet(1, "Send rtdump Request to %s (%s)\n",
   1297   1.1  itojun 		ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
   1298   1.1  itojun 	error = sendpacket(&ifcp->ifc_ripsin, RIPSIZE(1));
   1299   1.1  itojun 	if (error == EAFNOSUPPORT) {
   1300   1.1  itojun 		/* Protocol not supported */
   1301   1.1  itojun 		tracet(1, "Could not send rtdump Request to %s (%s): "
   1302   1.1  itojun 			"set IFF_UP to 0\n",
   1303   1.1  itojun 			ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
   1304   1.1  itojun 		ifcp->ifc_flags &= ~IFF_UP;	/* As if down for AF_INET6 */
   1305   1.1  itojun 	}
   1306   1.1  itojun 	ripbuf->rip6_cmd = RIP6_RESPONSE;
   1307   1.1  itojun }
   1308   1.1  itojun 
   1309   1.1  itojun /*
   1310   1.1  itojun  * Process a RIP6_REQUEST packet.
   1311   1.1  itojun  */
   1312   1.7   itohy void
   1313  1.36     wiz riprequest(struct ifc *ifcp, struct netinfo6 *np, int nn,
   1314  1.36     wiz 	   struct sockaddr_in6 *sin6)
   1315   1.1  itojun {
   1316   1.1  itojun 	int i;
   1317   1.1  itojun 	struct riprt *rrt;
   1318   1.1  itojun 
   1319   1.8  itojun 	if (!(nn == 1 && IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest) &&
   1320   1.1  itojun 	      np->rip6_plen == 0 && np->rip6_metric == HOPCNT_INFINITY6)) {
   1321   1.1  itojun 		/* Specific response, don't split-horizon */
   1322   1.1  itojun 		trace(1, "\tRIP Request\n");
   1323   1.1  itojun 		for (i = 0; i < nn; i++, np++) {
   1324  1.24  itojun 			rrt = rtsearch(np, NULL);
   1325   1.1  itojun 			if (rrt)
   1326   1.1  itojun 				np->rip6_metric = rrt->rrt_info.rip6_metric;
   1327   1.1  itojun 			else
   1328   1.1  itojun 				np->rip6_metric = HOPCNT_INFINITY6;
   1329   1.1  itojun 		}
   1330  1.29  itojun 		(void)sendpacket(sin6, RIPSIZE(nn));
   1331   1.1  itojun 		return;
   1332   1.1  itojun 	}
   1333   1.1  itojun 	/* Whole routing table dump */
   1334   1.1  itojun 	trace(1, "\tRIP Request -- whole routing table\n");
   1335  1.29  itojun 	ripsend(ifcp, sin6, RRTF_SENDANYWAY);
   1336   1.1  itojun }
   1337   1.1  itojun 
   1338   1.1  itojun /*
   1339   1.1  itojun  * Get information of each interface.
   1340   1.1  itojun  */
   1341   1.7   itohy void
   1342  1.36     wiz ifconfig(void)
   1343   1.1  itojun {
   1344  1.10  itojun 	struct ifaddrs *ifap, *ifa;
   1345  1.10  itojun 	struct ifc *ifcp;
   1346  1.10  itojun 	struct ipv6_mreq mreq;
   1347  1.10  itojun 	int s;
   1348  1.10  itojun 
   1349  1.21  itojun 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
   1350  1.10  itojun 		fatal("socket");
   1351  1.21  itojun 		/*NOTREACHED*/
   1352  1.21  itojun 	}
   1353  1.10  itojun 
   1354  1.21  itojun 	if (getifaddrs(&ifap) != 0) {
   1355  1.10  itojun 		fatal("getifaddrs");
   1356  1.21  itojun 		/*NOTREACHED*/
   1357  1.21  itojun 	}
   1358  1.10  itojun 
   1359  1.10  itojun 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
   1360  1.10  itojun 		if (ifa->ifa_addr->sa_family != AF_INET6)
   1361  1.10  itojun 			continue;
   1362  1.10  itojun 		ifcp = ifc_find(ifa->ifa_name);
   1363  1.10  itojun 		/* we are interested in multicast-capable interfaces */
   1364  1.10  itojun 		if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
   1365  1.10  itojun 			continue;
   1366  1.10  itojun 		if (!ifcp) {
   1367  1.10  itojun 			/* new interface */
   1368  1.21  itojun 			if ((ifcp = MALLOC(struct ifc)) == NULL) {
   1369  1.12  itojun 				fatal("malloc: struct ifc");
   1370  1.21  itojun 				/*NOTREACHED*/
   1371  1.21  itojun 			}
   1372  1.10  itojun 			memset(ifcp, 0, sizeof(*ifcp));
   1373  1.10  itojun 			ifcp->ifc_index = -1;
   1374  1.10  itojun 			ifcp->ifc_next = ifc;
   1375  1.10  itojun 			ifc = ifcp;
   1376  1.10  itojun 			nifc++;
   1377  1.10  itojun 			ifcp->ifc_name = allocopy(ifa->ifa_name);
   1378  1.10  itojun 			ifcp->ifc_addr = 0;
   1379  1.10  itojun 			ifcp->ifc_filter = 0;
   1380  1.10  itojun 			ifcp->ifc_flags = ifa->ifa_flags;
   1381  1.10  itojun 			trace(1, "newif %s <%s>\n", ifcp->ifc_name,
   1382  1.10  itojun 				ifflags(ifcp->ifc_flags));
   1383  1.10  itojun 			if (!strcmp(ifcp->ifc_name, LOOPBACK_IF))
   1384  1.10  itojun 				loopifcp = ifcp;
   1385  1.10  itojun 		} else {
   1386  1.10  itojun 			/* update flag, this may be up again */
   1387  1.10  itojun 			if (ifcp->ifc_flags != ifa->ifa_flags) {
   1388  1.10  itojun 				trace(1, "%s: <%s> -> ", ifcp->ifc_name,
   1389  1.10  itojun 					ifflags(ifcp->ifc_flags));
   1390  1.10  itojun 				trace(1, "<%s>\n", ifflags(ifa->ifa_flags));
   1391  1.24  itojun 				ifcp->ifc_cflags |= IFC_CHANGED;
   1392  1.10  itojun 			}
   1393  1.10  itojun 			ifcp->ifc_flags = ifa->ifa_flags;
   1394  1.10  itojun 		}
   1395  1.10  itojun 		ifconfig1(ifa->ifa_name, ifa->ifa_addr, ifcp, s);
   1396  1.10  itojun 		if ((ifcp->ifc_flags & (IFF_LOOPBACK | IFF_UP)) == IFF_UP
   1397  1.10  itojun 		 && 0 < ifcp->ifc_index && !ifcp->ifc_joined) {
   1398  1.10  itojun 			mreq.ipv6mr_multiaddr = ifcp->ifc_ripsin.sin6_addr;
   1399  1.10  itojun 			mreq.ipv6mr_interface = ifcp->ifc_index;
   1400  1.21  itojun 			if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
   1401  1.21  itojun 			    &mreq, sizeof(mreq)) < 0) {
   1402  1.10  itojun 				fatal("IPV6_JOIN_GROUP");
   1403  1.21  itojun 				/*NOTREACHED*/
   1404  1.21  itojun 			}
   1405  1.10  itojun 			trace(1, "join %s %s\n", ifcp->ifc_name, RIP6_DEST);
   1406  1.10  itojun 			ifcp->ifc_joined++;
   1407  1.10  itojun 		}
   1408  1.10  itojun 	}
   1409  1.10  itojun 	close(s);
   1410  1.10  itojun 	freeifaddrs(ifap);
   1411   1.1  itojun }
   1412   1.1  itojun 
   1413   1.7   itohy void
   1414  1.36     wiz ifconfig1(const char *name, const struct sockaddr *sa, struct ifc *ifcp, int s)
   1415   1.1  itojun {
   1416   1.1  itojun 	struct	in6_ifreq ifr;
   1417  1.29  itojun 	const struct sockaddr_in6 *sin6;
   1418   1.1  itojun 	struct	ifac *ifa;
   1419   1.1  itojun 	int	plen;
   1420   1.1  itojun 	char	buf[BUFSIZ];
   1421   1.1  itojun 
   1422  1.29  itojun 	sin6 = (const struct sockaddr_in6 *)sa;
   1423  1.29  itojun 	if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && !lflag)
   1424  1.29  itojun 		return;
   1425  1.29  itojun 	ifr.ifr_addr = *sin6;
   1426  1.32  itojun 	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1427  1.21  itojun 	if (ioctl(s, SIOCGIFNETMASK_IN6, (char *)&ifr) < 0) {
   1428   1.1  itojun 		fatal("ioctl: SIOCGIFNETMASK_IN6");
   1429  1.21  itojun 		/*NOTREACHED*/
   1430  1.21  itojun 	}
   1431  1.24  itojun 	plen = sin6mask2len(&ifr.ifr_addr);
   1432  1.29  itojun 	if ((ifa = ifa_match(ifcp, &sin6->sin6_addr, plen)) != NULL) {
   1433   1.1  itojun 		/* same interface found */
   1434   1.1  itojun 		/* need check if something changed */
   1435   1.1  itojun 		/* XXX not yet implemented */
   1436   1.1  itojun 		return;
   1437   1.1  itojun 	}
   1438   1.1  itojun 	/*
   1439   1.1  itojun 	 * New address is found
   1440   1.1  itojun 	 */
   1441  1.21  itojun 	if ((ifa = MALLOC(struct ifac)) == NULL) {
   1442   1.1  itojun 		fatal("malloc: struct ifac");
   1443  1.21  itojun 		/*NOTREACHED*/
   1444  1.21  itojun 	}
   1445  1.12  itojun 	memset(ifa, 0, sizeof(*ifa));
   1446   1.1  itojun 	ifa->ifa_conf = ifcp;
   1447   1.1  itojun 	ifa->ifa_next = ifcp->ifc_addr;
   1448   1.1  itojun 	ifcp->ifc_addr = ifa;
   1449  1.29  itojun 	ifa->ifa_addr = sin6->sin6_addr;
   1450   1.1  itojun 	ifa->ifa_plen = plen;
   1451   1.1  itojun 	if (ifcp->ifc_flags & IFF_POINTOPOINT) {
   1452  1.29  itojun 		ifr.ifr_addr = *sin6;
   1453  1.21  itojun 		if (ioctl(s, SIOCGIFDSTADDR_IN6, (char *)&ifr) < 0) {
   1454   1.1  itojun 			fatal("ioctl: SIOCGIFDSTADDR_IN6");
   1455  1.21  itojun 			/*NOTREACHED*/
   1456  1.21  itojun 		}
   1457   1.1  itojun 		ifa->ifa_raddr = ifr.ifr_dstaddr.sin6_addr;
   1458   1.1  itojun 		inet_ntop(AF_INET6, (void *)&ifa->ifa_raddr, buf, sizeof(buf));
   1459   1.1  itojun 		trace(1, "found address %s/%d -- %s\n",
   1460   1.1  itojun 			inet6_n2p(&ifa->ifa_addr), ifa->ifa_plen, buf);
   1461   1.1  itojun 	} else {
   1462   1.1  itojun 		trace(1, "found address %s/%d\n",
   1463   1.1  itojun 			inet6_n2p(&ifa->ifa_addr), ifa->ifa_plen);
   1464   1.1  itojun 	}
   1465   1.1  itojun 	if (ifcp->ifc_index < 0 && IN6_IS_ADDR_LINKLOCAL(&ifa->ifa_addr)) {
   1466   1.1  itojun 		ifcp->ifc_mylladdr = ifa->ifa_addr;
   1467   1.1  itojun 		ifcp->ifc_index = IN6_LINKLOCAL_IFINDEX(ifa->ifa_addr);
   1468   1.1  itojun 		memcpy(&ifcp->ifc_ripsin, &ripsin, ripsin.ss_len);
   1469   1.1  itojun 		SET_IN6_LINKLOCAL_IFINDEX(ifcp->ifc_ripsin.sin6_addr,
   1470   1.1  itojun 			ifcp->ifc_index);
   1471   1.1  itojun 		setindex2ifc(ifcp->ifc_index, ifcp);
   1472   1.1  itojun 		ifcp->ifc_mtu = getifmtu(ifcp->ifc_index);
   1473   1.1  itojun 		if (ifcp->ifc_mtu > RIP6_MAXMTU)
   1474   1.1  itojun 			ifcp->ifc_mtu = RIP6_MAXMTU;
   1475  1.21  itojun 		if (ioctl(s, SIOCGIFMETRIC, (char *)&ifr) < 0) {
   1476   1.1  itojun 			fatal("ioctl: SIOCGIFMETRIC");
   1477  1.21  itojun 			/*NOTREACHED*/
   1478  1.21  itojun 		}
   1479   1.1  itojun 		ifcp->ifc_metric = ifr.ifr_metric;
   1480   1.1  itojun 		trace(1, "\tindex: %d, mtu: %d, metric: %d\n",
   1481   1.1  itojun 			ifcp->ifc_index, ifcp->ifc_mtu, ifcp->ifc_metric);
   1482  1.29  itojun 	} else
   1483  1.29  itojun 		ifcp->ifc_cflags |= IFC_CHANGED;
   1484   1.1  itojun }
   1485   1.1  itojun 
   1486   1.1  itojun /*
   1487   1.1  itojun  * Receive and process routing messages.
   1488   1.1  itojun  * Update interface information as necesssary.
   1489   1.1  itojun  */
   1490   1.7   itohy void
   1491  1.36     wiz rtrecv(void)
   1492   1.1  itojun {
   1493   1.1  itojun 	char buf[BUFSIZ];
   1494   1.1  itojun 	char *p, *q;
   1495   1.1  itojun 	struct rt_msghdr *rtm;
   1496   1.1  itojun 	struct ifa_msghdr *ifam;
   1497   1.1  itojun 	struct if_msghdr *ifm;
   1498   1.1  itojun 	int len;
   1499  1.24  itojun 	struct ifc *ifcp, *ic;
   1500   1.1  itojun 	int iface = 0, rtable = 0;
   1501   1.1  itojun 	struct sockaddr_in6 *rta[RTAX_MAX];
   1502  1.29  itojun 	struct sockaddr_in6 mask;
   1503   1.1  itojun 	int i, addrs;
   1504  1.24  itojun 	struct riprt *rrt;
   1505   1.1  itojun 
   1506   1.1  itojun 	if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
   1507   1.1  itojun 		perror("read from rtsock");
   1508  1.26     wiz 		exit(1);
   1509   1.1  itojun 	}
   1510   1.1  itojun 	if (len < sizeof(*rtm)) {
   1511  1.19  itojun 		trace(1, "short read from rtsock: %d (should be > %lu)\n",
   1512  1.19  itojun 			len, (u_long)sizeof(*rtm));
   1513   1.1  itojun 		return;
   1514   1.1  itojun 	}
   1515  1.29  itojun 	if (dflag >= 2) {
   1516  1.29  itojun 		fprintf(stderr, "rtmsg:\n");
   1517  1.29  itojun 		for (i = 0; i < len; i++) {
   1518  1.29  itojun 			fprintf(stderr, "%02x ", buf[i] & 0xff);
   1519  1.29  itojun 			if (i % 16 == 15) fprintf(stderr, "\n");
   1520  1.29  itojun 		}
   1521  1.29  itojun 		fprintf(stderr, "\n");
   1522  1.29  itojun 	}
   1523   1.1  itojun 
   1524   1.1  itojun 	for (p = buf; p - buf < len; p += ((struct rt_msghdr *)p)->rtm_msglen) {
   1525   1.1  itojun 		/* safety against bogus message */
   1526   1.1  itojun 		if (((struct rt_msghdr *)p)->rtm_msglen <= 0) {
   1527   1.1  itojun 			trace(1, "bogus rtmsg: length=%d\n",
   1528   1.1  itojun 				((struct rt_msghdr *)p)->rtm_msglen);
   1529   1.1  itojun 			break;
   1530   1.1  itojun 		}
   1531   1.1  itojun 		rtm = NULL;
   1532   1.1  itojun 		ifam = NULL;
   1533   1.1  itojun 		ifm = NULL;
   1534   1.1  itojun 		switch (((struct rt_msghdr *)p)->rtm_type) {
   1535   1.1  itojun 		case RTM_NEWADDR:
   1536   1.1  itojun 		case RTM_DELADDR:
   1537   1.1  itojun 			ifam = (struct ifa_msghdr *)p;
   1538   1.1  itojun 			addrs = ifam->ifam_addrs;
   1539   1.1  itojun 			q = (char *)(ifam + 1);
   1540   1.1  itojun 			break;
   1541   1.1  itojun 		case RTM_IFINFO:
   1542   1.1  itojun 			ifm = (struct if_msghdr *)p;
   1543   1.1  itojun 			addrs = ifm->ifm_addrs;
   1544   1.1  itojun 			q = (char *)(ifm + 1);
   1545   1.1  itojun 			break;
   1546   1.1  itojun 		default:
   1547   1.1  itojun 			rtm = (struct rt_msghdr *)p;
   1548   1.1  itojun 			addrs = rtm->rtm_addrs;
   1549   1.1  itojun 			q = (char *)(rtm + 1);
   1550   1.1  itojun 			if (rtm->rtm_version != RTM_VERSION) {
   1551   1.1  itojun 				trace(1, "unexpected rtmsg version %d "
   1552   1.1  itojun 					"(should be %d)\n",
   1553   1.1  itojun 					rtm->rtm_version, RTM_VERSION);
   1554   1.1  itojun 				continue;
   1555   1.1  itojun 			}
   1556   1.1  itojun 			if (rtm->rtm_pid == pid) {
   1557   1.1  itojun #if 0
   1558   1.1  itojun 				trace(1, "rtmsg looped back to me, ignored\n");
   1559   1.1  itojun #endif
   1560   1.1  itojun 				continue;
   1561   1.1  itojun 			}
   1562   1.1  itojun 			break;
   1563   1.1  itojun 		}
   1564   1.1  itojun 		memset(&rta, 0, sizeof(rta));
   1565   1.1  itojun 		for (i = 0; i < RTAX_MAX; i++) {
   1566   1.1  itojun 			if (addrs & (1 << i)) {
   1567   1.1  itojun 				rta[i] = (struct sockaddr_in6 *)q;
   1568   1.8  itojun 				q += ROUNDUP(rta[i]->sin6_len);
   1569   1.1  itojun 			}
   1570   1.1  itojun 		}
   1571   1.1  itojun 
   1572   1.1  itojun 		trace(1, "rtsock: %s (addrs=%x)\n",
   1573   1.1  itojun 			rttypes((struct rt_msghdr *)p), addrs);
   1574   1.1  itojun 		if (dflag >= 2) {
   1575   1.1  itojun 			for (i = 0;
   1576   1.1  itojun 			     i < ((struct rt_msghdr *)p)->rtm_msglen;
   1577   1.1  itojun 			     i++) {
   1578   1.1  itojun 				fprintf(stderr, "%02x ", p[i] & 0xff);
   1579   1.1  itojun 				if (i % 16 == 15) fprintf(stderr, "\n");
   1580   1.1  itojun 			}
   1581   1.1  itojun 			fprintf(stderr, "\n");
   1582   1.1  itojun 		}
   1583   1.1  itojun 
   1584   1.1  itojun 		/*
   1585   1.1  itojun 		 * Easy ones first.
   1586   1.1  itojun 		 *
   1587   1.1  itojun 		 * We may be able to optimize by using ifm->ifm_index or
   1588   1.1  itojun 		 * ifam->ifam_index.  For simplicity we don't do that here.
   1589   1.1  itojun 		 */
   1590   1.1  itojun 		switch (((struct rt_msghdr *)p)->rtm_type) {
   1591   1.1  itojun 		case RTM_NEWADDR:
   1592   1.1  itojun 		case RTM_IFINFO:
   1593   1.1  itojun 			iface++;
   1594   1.1  itojun 			continue;
   1595   1.1  itojun 		case RTM_ADD:
   1596   1.1  itojun 			rtable++;
   1597   1.1  itojun 			continue;
   1598   1.1  itojun 		case RTM_LOSING:
   1599   1.1  itojun 		case RTM_MISS:
   1600   1.1  itojun 		case RTM_RESOLVE:
   1601   1.1  itojun 		case RTM_GET:
   1602   1.1  itojun 		case RTM_LOCK:
   1603   1.1  itojun 			/* nothing to be done here */
   1604   1.1  itojun 			trace(1, "\tnothing to be done, ignored\n");
   1605   1.1  itojun 			continue;
   1606   1.1  itojun 		}
   1607   1.1  itojun 
   1608   1.1  itojun #if 0
   1609   1.1  itojun 		if (rta[RTAX_DST] == NULL) {
   1610   1.1  itojun 			trace(1, "\tno destination, ignored\n");
   1611   1.1  itojun 			continue;
   1612   1.1  itojun 		}
   1613   1.1  itojun 		if (rta[RTAX_DST]->sin6_family != AF_INET6) {
   1614   1.1  itojun 			trace(1, "\taf mismatch, ignored\n");
   1615   1.1  itojun 			continue;
   1616   1.1  itojun 		}
   1617   1.1  itojun 		if (IN6_IS_ADDR_LINKLOCAL(&rta[RTAX_DST]->sin6_addr)) {
   1618   1.1  itojun 			trace(1, "\tlinklocal destination, ignored\n");
   1619   1.1  itojun 			continue;
   1620   1.1  itojun 		}
   1621   1.1  itojun 		if (IN6_ARE_ADDR_EQUAL(&rta[RTAX_DST]->sin6_addr, &in6addr_loopback)) {
   1622   1.1  itojun 			trace(1, "\tloopback destination, ignored\n");
   1623   1.1  itojun 			continue;		/* Loopback */
   1624   1.1  itojun 		}
   1625   1.1  itojun 		if (IN6_IS_ADDR_MULTICAST(&rta[RTAX_DST]->sin6_addr)) {
   1626   1.1  itojun 			trace(1, "\tmulticast destination, ignored\n");
   1627   1.1  itojun 			continue;
   1628   1.1  itojun 		}
   1629   1.1  itojun #endif
   1630   1.1  itojun 
   1631   1.1  itojun 		/* hard ones */
   1632   1.1  itojun 		switch (((struct rt_msghdr *)p)->rtm_type) {
   1633   1.1  itojun 		case RTM_NEWADDR:
   1634   1.1  itojun 		case RTM_IFINFO:
   1635   1.1  itojun 		case RTM_ADD:
   1636   1.1  itojun 		case RTM_LOSING:
   1637   1.1  itojun 		case RTM_MISS:
   1638   1.1  itojun 		case RTM_RESOLVE:
   1639   1.1  itojun 		case RTM_GET:
   1640   1.1  itojun 		case RTM_LOCK:
   1641   1.1  itojun 			/* should already be handled */
   1642   1.1  itojun 			fatal("rtrecv: never reach here");
   1643  1.21  itojun 			/*NOTREACHED*/
   1644   1.1  itojun 		case RTM_DELETE:
   1645  1.29  itojun 			if (!rta[RTAX_DST] || !rta[RTAX_GATEWAY]) {
   1646  1.29  itojun 				trace(1, "\tsome of dst/gw/netamsk are "
   1647  1.29  itojun 				    "unavailable, ignored\n");
   1648  1.29  itojun 				break;
   1649  1.29  itojun 			}
   1650  1.29  itojun 			if ((rtm->rtm_flags & RTF_HOST) != 0) {
   1651  1.29  itojun 				mask.sin6_len = sizeof(mask);
   1652  1.29  itojun 				memset(&mask.sin6_addr, 0xff,
   1653  1.29  itojun 				    sizeof(mask.sin6_addr));
   1654  1.29  itojun 				rta[RTAX_NETMASK] = &mask;
   1655  1.29  itojun 			} else if (!rta[RTAX_NETMASK]) {
   1656  1.29  itojun 				trace(1, "\tsome of dst/gw/netamsk are "
   1657  1.29  itojun 				    "unavailable, ignored\n");
   1658   1.1  itojun 				break;
   1659   1.1  itojun 			}
   1660  1.29  itojun 			if (rt_del(rta[RTAX_DST], rta[RTAX_GATEWAY],
   1661  1.29  itojun 			    rta[RTAX_NETMASK]) == 0) {
   1662   1.1  itojun 				rtable++;	/*just to be sure*/
   1663   1.1  itojun 			}
   1664   1.1  itojun 			break;
   1665   1.1  itojun 		case RTM_CHANGE:
   1666   1.1  itojun 		case RTM_REDIRECT:
   1667   1.1  itojun 			trace(1, "\tnot supported yet, ignored\n");
   1668   1.1  itojun 			break;
   1669   1.1  itojun 		case RTM_DELADDR:
   1670   1.1  itojun 			if (!rta[RTAX_NETMASK] || !rta[RTAX_IFA]) {
   1671   1.1  itojun 				trace(1, "\tno netmask or ifa given, ignored\n");
   1672   1.1  itojun 				break;
   1673   1.1  itojun 			}
   1674   1.1  itojun 			if (ifam->ifam_index < nindex2ifc)
   1675   1.1  itojun 				ifcp = index2ifc[ifam->ifam_index];
   1676   1.1  itojun 			else
   1677   1.1  itojun 				ifcp = NULL;
   1678   1.1  itojun 			if (!ifcp) {
   1679   1.1  itojun 				trace(1, "\tinvalid ifam_index %d, ignored\n",
   1680   1.1  itojun 					ifam->ifam_index);
   1681   1.1  itojun 				break;
   1682   1.1  itojun 			}
   1683  1.29  itojun 			if (!rt_deladdr(ifcp, rta[RTAX_IFA], rta[RTAX_NETMASK]))
   1684  1.29  itojun 				iface++;
   1685   1.1  itojun 			break;
   1686   1.1  itojun 		case RTM_OLDADD:
   1687   1.1  itojun 		case RTM_OLDDEL:
   1688   1.1  itojun 			trace(1, "\tnot supported yet, ignored\n");
   1689   1.1  itojun 			break;
   1690   1.1  itojun 		}
   1691   1.1  itojun 
   1692   1.1  itojun 	}
   1693   1.1  itojun 
   1694   1.1  itojun 	if (iface) {
   1695   1.1  itojun 		trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
   1696   1.1  itojun 		ifconfig();
   1697   1.1  itojun 		for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next)
   1698  1.24  itojun 			if (ifcp->ifc_cflags & IFC_CHANGED) {
   1699  1.24  itojun 				if (ifrt(ifcp, 1)) {
   1700  1.24  itojun 					for (ic = ifc; ic; ic = ic->ifc_next) {
   1701  1.24  itojun 						if (ifcp->ifc_index == ic->ifc_index)
   1702  1.24  itojun 							continue;
   1703  1.24  itojun 						if (ic->ifc_flags & IFF_UP)
   1704  1.24  itojun 							ripsend(ic, &ic->ifc_ripsin,
   1705  1.24  itojun 							RRTF_CHANGED);
   1706  1.24  itojun 					}
   1707  1.24  itojun 					/* Reset the flag */
   1708  1.24  itojun 					for (rrt = riprt; rrt; rrt = rrt->rrt_next)
   1709  1.24  itojun 						rrt->rrt_rflags &= ~RRTF_CHANGED;
   1710  1.24  itojun 				}
   1711  1.24  itojun 				ifcp->ifc_cflags &= ~IFC_CHANGED;
   1712  1.24  itojun 			}
   1713   1.1  itojun 	}
   1714   1.1  itojun 	if (rtable) {
   1715   1.1  itojun 		trace(1, "rtsock: read routing table again\n");
   1716   1.1  itojun 		krtread(1);
   1717   1.1  itojun 	}
   1718   1.1  itojun }
   1719   1.1  itojun 
   1720   1.1  itojun /*
   1721   1.1  itojun  * remove specified route from the internal routing table.
   1722   1.1  itojun  */
   1723   1.7   itohy int
   1724  1.36     wiz rt_del(const struct sockaddr_in6 *sdst, const struct sockaddr_in6 *sgw,
   1725  1.36     wiz        const struct sockaddr_in6 *smask)
   1726   1.1  itojun {
   1727   1.1  itojun 	const struct in6_addr *dst = NULL;
   1728   1.1  itojun 	const struct in6_addr *gw = NULL;
   1729   1.1  itojun 	int prefix;
   1730   1.1  itojun 	struct netinfo6 ni6;
   1731   1.1  itojun 	struct riprt *rrt = NULL;
   1732   1.1  itojun 	time_t t_lifetime;
   1733   1.1  itojun 
   1734   1.1  itojun 	if (sdst->sin6_family != AF_INET6) {
   1735   1.1  itojun 		trace(1, "\tother AF, ignored\n");
   1736   1.1  itojun 		return -1;
   1737   1.1  itojun 	}
   1738   1.1  itojun 	if (IN6_IS_ADDR_LINKLOCAL(&sdst->sin6_addr)
   1739   1.1  itojun 	 || IN6_ARE_ADDR_EQUAL(&sdst->sin6_addr, &in6addr_loopback)
   1740   1.1  itojun 	 || IN6_IS_ADDR_MULTICAST(&sdst->sin6_addr)) {
   1741   1.1  itojun 		trace(1, "\taddress %s not interesting, ignored\n",
   1742   1.1  itojun 			inet6_n2p(&sdst->sin6_addr));
   1743   1.1  itojun 		return -1;
   1744   1.1  itojun 	}
   1745   1.1  itojun 	dst = &sdst->sin6_addr;
   1746  1.24  itojun 	if (sgw->sin6_family == AF_INET6) {
   1747   1.1  itojun 		/* easy case */
   1748   1.1  itojun 		gw = &sgw->sin6_addr;
   1749  1.24  itojun 		prefix = sin6mask2len(smask);
   1750   1.1  itojun 	} else if (sgw->sin6_family == AF_LINK) {
   1751   1.1  itojun 		/*
   1752   1.1  itojun 		 * Interface route... a hard case.  We need to get the prefix
   1753   1.1  itojun 		 * length from the kernel, but we now are parsing rtmsg.
   1754   1.1  itojun 		 * We'll purge matching routes from my list, then get the
   1755   1.1  itojun 		 * fresh list.
   1756   1.1  itojun 		 */
   1757   1.1  itojun 		struct riprt *longest;
   1758   1.1  itojun 		trace(1, "\t%s is a interface route, guessing prefixlen\n",
   1759   1.1  itojun 			inet6_n2p(dst));
   1760   1.1  itojun 		longest = NULL;
   1761   1.1  itojun 		for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
   1762   1.1  itojun 			if (IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
   1763   1.1  itojun 					&sdst->sin6_addr)
   1764   1.1  itojun 			 && IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)) {
   1765   1.1  itojun 				if (!longest
   1766   1.1  itojun 				 || longest->rrt_info.rip6_plen <
   1767   1.1  itojun 						 rrt->rrt_info.rip6_plen) {
   1768   1.1  itojun 					longest = rrt;
   1769   1.1  itojun 				}
   1770   1.1  itojun 			}
   1771   1.1  itojun 		}
   1772   1.1  itojun 		rrt = longest;
   1773   1.1  itojun 		if (!rrt) {
   1774   1.1  itojun 			trace(1, "\tno matching interface route found\n");
   1775   1.1  itojun 			return -1;
   1776   1.1  itojun 		}
   1777   1.1  itojun 		gw = &in6addr_loopback;
   1778   1.1  itojun 		prefix = rrt->rrt_info.rip6_plen;
   1779   1.1  itojun 	} else {
   1780  1.29  itojun 		trace(1, "\tunsupported af: (gw=%d)\n", sgw->sin6_family);
   1781   1.1  itojun 		return -1;
   1782   1.1  itojun 	}
   1783   1.1  itojun 
   1784   1.1  itojun 	trace(1, "\tdeleting %s/%d ", inet6_n2p(dst), prefix);
   1785   1.1  itojun 	trace(1, "gw %s\n", inet6_n2p(gw));
   1786   1.1  itojun 	t_lifetime = time(NULL) - RIP_LIFETIME;
   1787   1.1  itojun 	/* age route for interface address */
   1788   1.1  itojun 	memset(&ni6, 0, sizeof(ni6));
   1789   1.1  itojun 	ni6.rip6_dest = *dst;
   1790   1.1  itojun 	ni6.rip6_plen = prefix;
   1791   1.1  itojun 	applyplen(&ni6.rip6_dest, ni6.rip6_plen);	/*to be sure*/
   1792   1.1  itojun 	trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6.rip6_dest),
   1793   1.1  itojun 		ni6.rip6_plen);
   1794  1.24  itojun 	if (!rrt && (rrt = rtsearch(&ni6, NULL)) == NULL) {
   1795   1.1  itojun 		trace(1, "\tno route found\n");
   1796   1.1  itojun 		return -1;
   1797   1.1  itojun 	}
   1798  1.29  itojun #if 0
   1799   1.1  itojun 	if ((rrt->rrt_flags & RTF_STATIC) == 0) {
   1800   1.1  itojun 		trace(1, "\tyou can delete static routes only\n");
   1801  1.29  itojun 	} else
   1802  1.29  itojun #endif
   1803  1.29  itojun 	if (!IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, gw)) {
   1804   1.1  itojun 		trace(1, "\tgw mismatch: %s <-> ",
   1805   1.1  itojun 			inet6_n2p(&rrt->rrt_gw));
   1806   1.1  itojun 		trace(1, "%s\n", inet6_n2p(gw));
   1807   1.1  itojun 	} else {
   1808   1.1  itojun 		trace(1, "\troute found, age it\n");
   1809   1.1  itojun 		if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
   1810   1.1  itojun 			rrt->rrt_t = t_lifetime;
   1811   1.1  itojun 			rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
   1812   1.1  itojun 		}
   1813   1.1  itojun 	}
   1814   1.1  itojun 	return 0;
   1815   1.1  itojun }
   1816   1.1  itojun 
   1817   1.1  itojun /*
   1818   1.1  itojun  * remove specified address from internal interface/routing table.
   1819   1.1  itojun  */
   1820   1.7   itohy int
   1821  1.36     wiz rt_deladdr(struct ifc *ifcp, const struct sockaddr_in6 *sifa,
   1822  1.36     wiz 	   const struct sockaddr_in6 *smask)
   1823   1.1  itojun {
   1824   1.1  itojun 	const struct in6_addr *addr = NULL;
   1825   1.1  itojun 	int prefix;
   1826   1.1  itojun 	struct ifac *ifa = NULL;
   1827   1.1  itojun 	struct netinfo6 ni6;
   1828   1.1  itojun 	struct riprt *rrt = NULL;
   1829   1.1  itojun 	time_t t_lifetime;
   1830   1.1  itojun 	int updated = 0;
   1831   1.1  itojun 
   1832  1.24  itojun 	if (sifa->sin6_family != AF_INET6) {
   1833   1.1  itojun 		trace(1, "\tother AF, ignored\n");
   1834   1.1  itojun 		return -1;
   1835   1.1  itojun 	}
   1836   1.1  itojun 	addr = &sifa->sin6_addr;
   1837  1.24  itojun 	prefix = sin6mask2len(smask);
   1838   1.1  itojun 
   1839   1.1  itojun 	trace(1, "\tdeleting %s/%d from %s\n",
   1840   1.1  itojun 		inet6_n2p(addr), prefix, ifcp->ifc_name);
   1841   1.1  itojun 	ifa = ifa_match(ifcp, addr, prefix);
   1842   1.1  itojun 	if (!ifa) {
   1843   1.1  itojun 		trace(1, "\tno matching ifa found for %s/%d on %s\n",
   1844   1.1  itojun 			inet6_n2p(addr), prefix, ifcp->ifc_name);
   1845   1.1  itojun 		return -1;
   1846   1.1  itojun 	}
   1847   1.1  itojun 	if (ifa->ifa_conf != ifcp) {
   1848   1.1  itojun 		trace(1, "\taddress table corrupt: back pointer does not match "
   1849   1.1  itojun 			"(%s != %s)\n",
   1850   1.1  itojun 			ifcp->ifc_name, ifa->ifa_conf->ifc_name);
   1851   1.1  itojun 		return -1;
   1852   1.1  itojun 	}
   1853   1.1  itojun 	/* remove ifa from interface */
   1854   1.1  itojun 	if (ifcp->ifc_addr == ifa)
   1855   1.1  itojun 		ifcp->ifc_addr = ifa->ifa_next;
   1856   1.1  itojun 	else {
   1857   1.1  itojun 		struct ifac *p;
   1858   1.1  itojun 		for (p = ifcp->ifc_addr; p; p = p->ifa_next) {
   1859   1.1  itojun 			if (p->ifa_next == ifa) {
   1860   1.1  itojun 				p->ifa_next = ifa->ifa_next;
   1861   1.1  itojun 				break;
   1862   1.1  itojun 			}
   1863   1.1  itojun 		}
   1864   1.1  itojun 	}
   1865   1.1  itojun 	ifa->ifa_next = NULL;
   1866   1.1  itojun 	ifa->ifa_conf = NULL;
   1867   1.1  itojun 	t_lifetime = time(NULL) - RIP_LIFETIME;
   1868   1.1  itojun 	/* age route for interface address */
   1869   1.1  itojun 	memset(&ni6, 0, sizeof(ni6));
   1870   1.1  itojun 	ni6.rip6_dest = ifa->ifa_addr;
   1871   1.1  itojun 	ni6.rip6_plen = ifa->ifa_plen;
   1872   1.1  itojun 	applyplen(&ni6.rip6_dest, ni6.rip6_plen);
   1873   1.1  itojun 	trace(1, "\tfind interface route %s/%d on %d\n",
   1874   1.1  itojun 		inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen, ifcp->ifc_index);
   1875  1.24  itojun 	if ((rrt = rtsearch(&ni6, NULL)) != NULL) {
   1876   1.1  itojun 		struct in6_addr none;
   1877   1.1  itojun 		memset(&none, 0, sizeof(none));
   1878  1.29  itojun 		if (rrt->rrt_index == ifcp->ifc_index &&
   1879  1.29  itojun 		    (IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &none) ||
   1880  1.29  itojun 		     IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw))) {
   1881   1.1  itojun 			trace(1, "\troute found, age it\n");
   1882   1.1  itojun 			if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
   1883   1.1  itojun 				rrt->rrt_t = t_lifetime;
   1884   1.1  itojun 				rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
   1885   1.1  itojun 			}
   1886   1.1  itojun 			updated++;
   1887   1.1  itojun 		} else {
   1888   1.1  itojun 			trace(1, "\tnon-interface route found: %s/%d on %d\n",
   1889   1.1  itojun 				inet6_n2p(&rrt->rrt_info.rip6_dest),
   1890   1.1  itojun 				rrt->rrt_info.rip6_plen,
   1891   1.1  itojun 				rrt->rrt_index);
   1892   1.1  itojun 		}
   1893   1.1  itojun 	} else
   1894   1.1  itojun 		trace(1, "\tno interface route found\n");
   1895   1.1  itojun 	/* age route for p2p destination */
   1896   1.1  itojun 	if (ifcp->ifc_flags & IFF_POINTOPOINT) {
   1897   1.1  itojun 		memset(&ni6, 0, sizeof(ni6));
   1898   1.1  itojun 		ni6.rip6_dest = ifa->ifa_raddr;
   1899   1.1  itojun 		ni6.rip6_plen = 128;
   1900   1.1  itojun 		applyplen(&ni6.rip6_dest, ni6.rip6_plen);	/*to be sure*/
   1901   1.1  itojun 		trace(1, "\tfind p2p route %s/%d on %d\n",
   1902   1.1  itojun 			inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen,
   1903   1.1  itojun 			ifcp->ifc_index);
   1904  1.24  itojun 		if ((rrt = rtsearch(&ni6, NULL)) != NULL) {
   1905  1.29  itojun 			if (rrt->rrt_index == ifcp->ifc_index &&
   1906  1.29  itojun 			    IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &ifa->ifa_addr)) {
   1907   1.1  itojun 				trace(1, "\troute found, age it\n");
   1908   1.1  itojun 				if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
   1909   1.1  itojun 					rrt->rrt_t = t_lifetime;
   1910   1.1  itojun 					rrt->rrt_info.rip6_metric =
   1911  1.29  itojun 					    HOPCNT_INFINITY6;
   1912   1.1  itojun 					updated++;
   1913   1.1  itojun 				}
   1914   1.1  itojun 			} else {
   1915   1.1  itojun 				trace(1, "\tnon-p2p route found: %s/%d on %d\n",
   1916   1.1  itojun 					inet6_n2p(&rrt->rrt_info.rip6_dest),
   1917   1.1  itojun 					rrt->rrt_info.rip6_plen,
   1918   1.1  itojun 					rrt->rrt_index);
   1919   1.1  itojun 			}
   1920   1.1  itojun 		} else
   1921   1.1  itojun 			trace(1, "\tno p2p route found\n");
   1922   1.1  itojun 	}
   1923   1.1  itojun 	return updated ? 0 : -1;
   1924   1.1  itojun }
   1925   1.1  itojun 
   1926   1.1  itojun /*
   1927   1.1  itojun  * Get each interface address and put those interface routes to the route
   1928   1.1  itojun  * list.
   1929   1.1  itojun  */
   1930  1.24  itojun int
   1931  1.36     wiz ifrt(struct ifc *ifcp, int again)
   1932   1.1  itojun {
   1933  1.12  itojun 	struct ifac *ifa;
   1934  1.29  itojun 	struct riprt *rrt = NULL, *search_rrt, *prev_rrt, *loop_rrt;
   1935  1.12  itojun 	struct netinfo6 *np;
   1936  1.24  itojun 	time_t t_lifetime;
   1937  1.24  itojun 	int need_trigger = 0;
   1938   1.1  itojun 
   1939  1.32  itojun #if 0
   1940   1.1  itojun 	if (ifcp->ifc_flags & IFF_LOOPBACK)
   1941  1.24  itojun 		return 0;			/* ignore loopback */
   1942  1.32  itojun #endif
   1943  1.32  itojun 
   1944  1.12  itojun 	if (ifcp->ifc_flags & IFF_POINTOPOINT) {
   1945  1.12  itojun 		ifrt_p2p(ifcp, again);
   1946  1.24  itojun 		return 0;
   1947  1.12  itojun 	}
   1948  1.12  itojun 
   1949   1.1  itojun 	for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
   1950  1.12  itojun 		if (IN6_IS_ADDR_LINKLOCAL(&ifa->ifa_addr)) {
   1951  1.12  itojun #if 0
   1952  1.12  itojun 			trace(1, "route: %s on %s: "
   1953  1.12  itojun 			    "skip linklocal interface address\n",
   1954  1.12  itojun 			    inet6_n2p(&ifa->ifa_addr), ifcp->ifc_name);
   1955  1.12  itojun #endif
   1956  1.12  itojun 			continue;
   1957  1.12  itojun 		}
   1958  1.12  itojun 		if (IN6_IS_ADDR_UNSPECIFIED(&ifa->ifa_addr)) {
   1959  1.12  itojun #if 0
   1960  1.12  itojun 			trace(1, "route: %s: skip unspec interface address\n",
   1961  1.12  itojun 			    ifcp->ifc_name);
   1962  1.12  itojun #endif
   1963  1.12  itojun 			continue;
   1964  1.12  itojun 		}
   1965  1.32  itojun 		if (IN6_IS_ADDR_LOOPBACK(&ifa->ifa_addr)) {
   1966  1.32  itojun #if 0
   1967  1.32  itojun 			trace(1, "route: %s: skip loopback address\n",
   1968  1.32  itojun 			    ifcp->ifc_name);
   1969  1.32  itojun #endif
   1970  1.32  itojun 			continue;
   1971  1.32  itojun 		}
   1972  1.24  itojun 		if (ifcp->ifc_flags & IFF_UP) {
   1973  1.24  itojun 			if ((rrt = MALLOC(struct riprt)) == NULL)
   1974  1.24  itojun 				fatal("malloc: struct riprt");
   1975  1.24  itojun 			memset(rrt, 0, sizeof(*rrt));
   1976  1.24  itojun 			rrt->rrt_same = NULL;
   1977  1.24  itojun 			rrt->rrt_index = ifcp->ifc_index;
   1978  1.24  itojun 			rrt->rrt_t = 0;	/* don't age */
   1979  1.24  itojun 			rrt->rrt_info.rip6_dest = ifa->ifa_addr;
   1980  1.24  itojun 			rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
   1981  1.24  itojun 			rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
   1982  1.24  itojun 			rrt->rrt_info.rip6_plen = ifa->ifa_plen;
   1983  1.32  itojun 			if (ifa->ifa_plen == 128)
   1984  1.32  itojun 				rrt->rrt_flags = RTF_HOST;
   1985  1.32  itojun 			else
   1986  1.32  itojun 				rrt->rrt_flags = RTF_CLONING;
   1987  1.24  itojun 			rrt->rrt_rflags |= RRTF_CHANGED;
   1988  1.24  itojun 			applyplen(&rrt->rrt_info.rip6_dest, ifa->ifa_plen);
   1989  1.24  itojun 			memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
   1990  1.24  itojun 			rrt->rrt_gw = ifa->ifa_addr;
   1991  1.24  itojun 			np = &rrt->rrt_info;
   1992  1.24  itojun 			search_rrt = rtsearch(np, &prev_rrt);
   1993  1.24  itojun 			if (search_rrt != NULL) {
   1994  1.29  itojun 				if (search_rrt->rrt_info.rip6_metric <=
   1995  1.24  itojun 				    rrt->rrt_info.rip6_metric) {
   1996  1.24  itojun 					/* Already have better route */
   1997  1.24  itojun 					if (!again) {
   1998  1.24  itojun 						trace(1, "route: %s/%d: "
   1999  1.24  itojun 						    "already registered (%s)\n",
   2000  1.24  itojun 						    inet6_n2p(&np->rip6_dest), np->rip6_plen,
   2001  1.24  itojun 						    ifcp->ifc_name);
   2002  1.24  itojun 					}
   2003  1.29  itojun 					goto next;
   2004  1.24  itojun 				}
   2005  1.29  itojun 
   2006  1.29  itojun 				if (prev_rrt)
   2007  1.29  itojun 					prev_rrt->rrt_next = rrt->rrt_next;
   2008  1.29  itojun 				else
   2009  1.29  itojun 					riprt = rrt->rrt_next;
   2010  1.29  itojun 				delroute(&rrt->rrt_info, &rrt->rrt_gw);
   2011  1.24  itojun 			}
   2012   1.1  itojun 			/* Attach the route to the list */
   2013  1.12  itojun 			trace(1, "route: %s/%d: register route (%s)\n",
   2014  1.12  itojun 			    inet6_n2p(&np->rip6_dest), np->rip6_plen,
   2015  1.12  itojun 			    ifcp->ifc_name);
   2016   1.1  itojun 			rrt->rrt_next = riprt;
   2017   1.1  itojun 			riprt = rrt;
   2018  1.24  itojun 			addroute(rrt, &rrt->rrt_gw, ifcp);
   2019  1.29  itojun 			rrt = NULL;
   2020  1.24  itojun 			sendrequest(ifcp);
   2021  1.24  itojun 			ripsend(ifcp, &ifcp->ifc_ripsin, 0);
   2022  1.24  itojun 			need_trigger = 1;
   2023   1.1  itojun 		} else {
   2024  1.24  itojun 			for (loop_rrt = riprt; loop_rrt; loop_rrt = loop_rrt->rrt_next) {
   2025  1.24  itojun 				if (loop_rrt->rrt_index == ifcp->ifc_index) {
   2026  1.24  itojun 					t_lifetime = time(NULL) - RIP_LIFETIME;
   2027  1.24  itojun 					if (loop_rrt->rrt_t == 0 || loop_rrt->rrt_t > t_lifetime) {
   2028  1.24  itojun 						loop_rrt->rrt_t = t_lifetime;
   2029  1.24  itojun 						loop_rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
   2030  1.24  itojun 						loop_rrt->rrt_rflags |= RRTF_CHANGED;
   2031  1.24  itojun 						need_trigger = 1;
   2032  1.24  itojun 					}
   2033  1.24  itojun 				}
   2034   1.1  itojun 			}
   2035  1.24  itojun                 }
   2036  1.29  itojun 	next:
   2037  1.29  itojun 		if (rrt)
   2038  1.29  itojun 			free(rrt);
   2039  1.12  itojun 	}
   2040  1.24  itojun 	return need_trigger;
   2041  1.12  itojun }
   2042   1.1  itojun 
   2043  1.12  itojun /*
   2044  1.12  itojun  * there are couple of p2p interface routing models.  "behavior" lets
   2045  1.13  itojun  * you pick one.  it looks that gated behavior fits best with BSDs,
   2046  1.32  itojun  * since BSD kernels do not look at prefix length on p2p interfaces.
   2047  1.12  itojun  */
   2048  1.12  itojun void
   2049  1.36     wiz ifrt_p2p(struct ifc *ifcp, int again)
   2050  1.12  itojun {
   2051  1.12  itojun 	struct ifac *ifa;
   2052  1.29  itojun 	struct riprt *rrt, *orrt, *prevrrt;
   2053  1.12  itojun 	struct netinfo6 *np;
   2054  1.12  itojun 	struct in6_addr addr, dest;
   2055  1.13  itojun 	int advert, ignore, i;
   2056  1.12  itojun #define P2PADVERT_NETWORK	1
   2057  1.12  itojun #define P2PADVERT_ADDR		2
   2058  1.12  itojun #define P2PADVERT_DEST		4
   2059  1.12  itojun #define P2PADVERT_MAX		4
   2060  1.12  itojun 	const enum { CISCO, GATED, ROUTE6D } behavior = GATED;
   2061  1.24  itojun 	const char *category = "";
   2062  1.12  itojun 	const char *noadv;
   2063  1.12  itojun 
   2064  1.12  itojun 	for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
   2065  1.12  itojun 		addr = ifa->ifa_addr;
   2066  1.12  itojun 		dest = ifa->ifa_raddr;
   2067  1.12  itojun 		applyplen(&addr, ifa->ifa_plen);
   2068  1.12  itojun 		applyplen(&dest, ifa->ifa_plen);
   2069  1.13  itojun 		advert = ignore = 0;
   2070  1.12  itojun 		switch (behavior) {
   2071  1.12  itojun 		case CISCO:
   2072  1.12  itojun 			/*
   2073  1.13  itojun 			 * honor addr/plen, just like normal shared medium
   2074  1.13  itojun 			 * interface.  this may cause trouble if you reuse
   2075  1.13  itojun 			 * addr/plen on other interfaces.
   2076  1.13  itojun 			 *
   2077  1.13  itojun 			 * advertise addr/plen.
   2078  1.12  itojun 			 */
   2079  1.12  itojun 			advert |= P2PADVERT_NETWORK;
   2080  1.12  itojun 			break;
   2081  1.12  itojun 		case GATED:
   2082  1.12  itojun 			/*
   2083  1.13  itojun 			 * prefixlen on p2p interface is meaningless.
   2084  1.13  itojun 			 * advertise addr/128 and dest/128.
   2085  1.13  itojun 			 *
   2086  1.13  itojun 			 * do not install network route to route6d routing
   2087  1.13  itojun 			 * table (if we do, it would prevent route installation
   2088  1.13  itojun 			 * for other p2p interface that shares addr/plen).
   2089  1.25  itojun 			 *
   2090  1.25  itojun 			 * XXX what should we do if dest is ::?  it will not
   2091  1.25  itojun 			 * get announced anyways (see following filter),
   2092  1.25  itojun 			 * but we need to think.
   2093  1.12  itojun 			 */
   2094  1.13  itojun 			advert |= P2PADVERT_ADDR;
   2095  1.12  itojun 			advert |= P2PADVERT_DEST;
   2096  1.13  itojun 			ignore |= P2PADVERT_NETWORK;
   2097  1.12  itojun 			break;
   2098  1.12  itojun 		case ROUTE6D:
   2099  1.12  itojun 			/*
   2100  1.25  itojun 			 * just for testing.  actually the code is redundant
   2101  1.25  itojun 			 * given the current p2p interface address assignment
   2102  1.25  itojun 			 * rule for kame kernel.
   2103  1.25  itojun 			 *
   2104  1.25  itojun 			 * intent:
   2105  1.25  itojun 			 *	A/n -> announce A/n
   2106  1.25  itojun 			 *	A B/n, A and B share prefix -> A/n (= B/n)
   2107  1.25  itojun 			 *	A B/n, do not share prefix -> A/128 and B/128
   2108  1.25  itojun 			 * actually, A/64 and A B/128 are the only cases
   2109  1.25  itojun 			 * permitted by the kernel:
   2110  1.25  itojun 			 *	A/64 -> A/64
   2111  1.25  itojun 			 *	A B/128 -> A/128 and B/128
   2112  1.12  itojun 			 */
   2113  1.25  itojun 			if (!IN6_IS_ADDR_UNSPECIFIED(&ifa->ifa_raddr)) {
   2114  1.25  itojun 				if (IN6_ARE_ADDR_EQUAL(&addr, &dest))
   2115  1.25  itojun 					advert |= P2PADVERT_NETWORK;
   2116  1.25  itojun 				else {
   2117  1.25  itojun 					advert |= P2PADVERT_ADDR;
   2118  1.25  itojun 					advert |= P2PADVERT_DEST;
   2119  1.25  itojun 					ignore |= P2PADVERT_NETWORK;
   2120  1.25  itojun 				}
   2121  1.25  itojun 			} else
   2122  1.12  itojun 				advert |= P2PADVERT_NETWORK;
   2123  1.12  itojun 			break;
   2124  1.12  itojun 		}
   2125  1.12  itojun 
   2126  1.12  itojun 		for (i = 1; i <= P2PADVERT_MAX; i *= 2) {
   2127  1.13  itojun 			if ((ignore & i) != 0)
   2128  1.13  itojun 				continue;
   2129  1.21  itojun 			if ((rrt = MALLOC(struct riprt)) == NULL) {
   2130   1.1  itojun 				fatal("malloc: struct riprt");
   2131  1.21  itojun 				/*NOTREACHED*/
   2132  1.21  itojun 			}
   2133  1.12  itojun 			memset(rrt, 0, sizeof(*rrt));
   2134   1.1  itojun 			rrt->rrt_same = NULL;
   2135   1.1  itojun 			rrt->rrt_index = ifcp->ifc_index;
   2136  1.12  itojun 			rrt->rrt_t = 0;	/* don't age */
   2137  1.12  itojun 			switch (i) {
   2138  1.12  itojun 			case P2PADVERT_NETWORK:
   2139  1.12  itojun 				rrt->rrt_info.rip6_dest = ifa->ifa_addr;
   2140  1.12  itojun 				rrt->rrt_info.rip6_plen = ifa->ifa_plen;
   2141  1.12  itojun 				applyplen(&rrt->rrt_info.rip6_dest,
   2142  1.12  itojun 				    ifa->ifa_plen);
   2143  1.12  itojun 				category = "network";
   2144  1.12  itojun 				break;
   2145  1.12  itojun 			case P2PADVERT_ADDR:
   2146  1.12  itojun 				rrt->rrt_info.rip6_dest = ifa->ifa_addr;
   2147  1.12  itojun 				rrt->rrt_info.rip6_plen = 128;
   2148  1.29  itojun 				rrt->rrt_gw = in6addr_loopback;
   2149  1.12  itojun 				category = "addr";
   2150  1.12  itojun 				break;
   2151  1.12  itojun 			case P2PADVERT_DEST:
   2152  1.12  itojun 				rrt->rrt_info.rip6_dest = ifa->ifa_raddr;
   2153  1.12  itojun 				rrt->rrt_info.rip6_plen = 128;
   2154  1.29  itojun 				rrt->rrt_gw = ifa->ifa_addr;
   2155  1.12  itojun 				category = "dest";
   2156  1.12  itojun 				break;
   2157  1.12  itojun 			}
   2158  1.12  itojun 			if (IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_info.rip6_dest) ||
   2159  1.12  itojun 			    IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_info.rip6_dest)) {
   2160  1.12  itojun #if 0
   2161  1.12  itojun 				trace(1, "route: %s: skip unspec/linklocal "
   2162  1.12  itojun 				    "(%s on %s)\n", category, ifcp->ifc_name);
   2163  1.12  itojun #endif
   2164  1.12  itojun 				free(rrt);
   2165  1.12  itojun 				continue;
   2166  1.12  itojun 			}
   2167  1.12  itojun 			if ((advert & i) == 0) {
   2168  1.13  itojun 				rrt->rrt_rflags |= RRTF_NOADVERTISE;
   2169  1.12  itojun 				noadv = ", NO-ADV";
   2170  1.12  itojun 			} else
   2171  1.12  itojun 				noadv = "";
   2172   1.1  itojun 			rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
   2173  1.12  itojun 			rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
   2174   1.1  itojun 			np = &rrt->rrt_info;
   2175  1.29  itojun 			orrt = rtsearch(np, &prevrrt);
   2176  1.29  itojun 			if (!orrt) {
   2177   1.1  itojun 				/* Attach the route to the list */
   2178  1.12  itojun 				trace(1, "route: %s/%d: register route "
   2179  1.12  itojun 				    "(%s on %s%s)\n",
   2180  1.12  itojun 				    inet6_n2p(&np->rip6_dest), np->rip6_plen,
   2181  1.12  itojun 				    category, ifcp->ifc_name, noadv);
   2182   1.1  itojun 				rrt->rrt_next = riprt;
   2183   1.1  itojun 				riprt = rrt;
   2184  1.29  itojun 			} else if (rrt->rrt_index != orrt->rrt_index ||
   2185  1.29  itojun 			    rrt->rrt_info.rip6_metric != orrt->rrt_info.rip6_metric) {
   2186  1.29  itojun 				/* swap route */
   2187  1.29  itojun 				rrt->rrt_next = orrt->rrt_next;
   2188  1.29  itojun 				if (prevrrt)
   2189  1.29  itojun 					prevrrt->rrt_next = rrt;
   2190  1.29  itojun 				else
   2191  1.29  itojun 					riprt = rrt;
   2192  1.29  itojun 				free(orrt);
   2193  1.29  itojun 
   2194  1.29  itojun 				trace(1, "route: %s/%d: update (%s on %s%s)\n",
   2195  1.29  itojun 				    inet6_n2p(&np->rip6_dest), np->rip6_plen,
   2196  1.29  itojun 				    category, ifcp->ifc_name, noadv);
   2197   1.1  itojun 			} else {
   2198   1.1  itojun 				/* Already found */
   2199   1.1  itojun 				if (!again) {
   2200  1.12  itojun 					trace(1, "route: %s/%d: "
   2201  1.12  itojun 					    "already registered (%s on %s%s)\n",
   2202  1.12  itojun 					    inet6_n2p(&np->rip6_dest),
   2203  1.12  itojun 					    np->rip6_plen, category,
   2204  1.12  itojun 					    ifcp->ifc_name, noadv);
   2205   1.1  itojun 				}
   2206   1.1  itojun 				free(rrt);
   2207   1.1  itojun 			}
   2208   1.1  itojun 		}
   2209   1.1  itojun 	}
   2210  1.12  itojun #undef P2PADVERT_NETWORK
   2211  1.12  itojun #undef P2PADVERT_ADDR
   2212  1.12  itojun #undef P2PADVERT_DEST
   2213  1.12  itojun #undef P2PADVERT_MAX
   2214   1.1  itojun }
   2215   1.1  itojun 
   2216   1.7   itohy int
   2217  1.36     wiz getifmtu(int ifindex)
   2218   1.1  itojun {
   2219   1.1  itojun 	int	mib[6];
   2220   1.1  itojun 	char	*buf;
   2221   1.1  itojun 	size_t	msize;
   2222   1.1  itojun 	struct	if_msghdr *ifm;
   2223   1.1  itojun 	int	mtu;
   2224   1.1  itojun 
   2225   1.1  itojun 	mib[0] = CTL_NET;
   2226   1.1  itojun 	mib[1] = PF_ROUTE;
   2227   1.1  itojun 	mib[2] = 0;
   2228   1.1  itojun 	mib[3] = AF_INET6;
   2229   1.1  itojun 	mib[4] = NET_RT_IFLIST;
   2230   1.1  itojun 	mib[5] = ifindex;
   2231  1.21  itojun 	if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
   2232   1.1  itojun 		fatal("sysctl estimate NET_RT_IFLIST");
   2233  1.21  itojun 		/*NOTREACHED*/
   2234  1.21  itojun 	}
   2235  1.21  itojun 	if ((buf = malloc(msize)) == NULL) {
   2236   1.1  itojun 		fatal("malloc");
   2237  1.21  itojun 		/*NOTREACHED*/
   2238  1.21  itojun 	}
   2239  1.21  itojun 	if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
   2240   1.1  itojun 		fatal("sysctl NET_RT_IFLIST");
   2241  1.21  itojun 		/*NOTREACHED*/
   2242  1.21  itojun 	}
   2243   1.1  itojun 	ifm = (struct if_msghdr *)buf;
   2244   1.1  itojun 	mtu = ifm->ifm_data.ifi_mtu;
   2245  1.24  itojun #ifdef __FreeBSD__
   2246  1.21  itojun 	if (ifindex != ifm->ifm_index) {
   2247   1.1  itojun 		fatal("ifindex does not match with ifm_index");
   2248  1.21  itojun 		/*NOTREACHED*/
   2249  1.21  itojun 	}
   2250  1.24  itojun #endif
   2251   1.1  itojun 	free(buf);
   2252   1.1  itojun 	return mtu;
   2253   1.1  itojun }
   2254   1.1  itojun 
   2255   1.7   itohy const char *
   2256  1.36     wiz rttypes(struct rt_msghdr *rtm)
   2257   1.1  itojun {
   2258  1.14  itojun #define	RTTYPE(s, f) \
   2259  1.14  itojun do { \
   2260  1.14  itojun 	if (rtm->rtm_type == (f)) \
   2261  1.14  itojun 		return (s); \
   2262  1.14  itojun } while (0)
   2263   1.1  itojun 	RTTYPE("ADD", RTM_ADD);
   2264   1.1  itojun 	RTTYPE("DELETE", RTM_DELETE);
   2265   1.1  itojun 	RTTYPE("CHANGE", RTM_CHANGE);
   2266   1.1  itojun 	RTTYPE("GET", RTM_GET);
   2267   1.1  itojun 	RTTYPE("LOSING", RTM_LOSING);
   2268   1.1  itojun 	RTTYPE("REDIRECT", RTM_REDIRECT);
   2269   1.1  itojun 	RTTYPE("MISS", RTM_MISS);
   2270   1.1  itojun 	RTTYPE("LOCK", RTM_LOCK);
   2271   1.1  itojun 	RTTYPE("OLDADD", RTM_OLDADD);
   2272   1.1  itojun 	RTTYPE("OLDDEL", RTM_OLDDEL);
   2273   1.1  itojun 	RTTYPE("RESOLVE", RTM_RESOLVE);
   2274   1.1  itojun 	RTTYPE("NEWADDR", RTM_NEWADDR);
   2275   1.1  itojun 	RTTYPE("DELADDR", RTM_DELADDR);
   2276   1.1  itojun 	RTTYPE("IFINFO", RTM_IFINFO);
   2277  1.24  itojun #ifdef RTM_OLDADD
   2278  1.24  itojun 	RTTYPE("OLDADD", RTM_OLDADD);
   2279  1.24  itojun #endif
   2280  1.24  itojun #ifdef RTM_OLDDEL
   2281  1.24  itojun 	RTTYPE("OLDDEL", RTM_OLDDEL);
   2282  1.24  itojun #endif
   2283  1.24  itojun #ifdef RTM_OIFINFO
   2284  1.24  itojun 	RTTYPE("OIFINFO", RTM_OIFINFO);
   2285  1.24  itojun #endif
   2286  1.24  itojun #ifdef RTM_IFANNOUNCE
   2287  1.24  itojun 	RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE);
   2288  1.24  itojun #endif
   2289  1.24  itojun #ifdef RTM_NEWMADDR
   2290  1.24  itojun 	RTTYPE("NEWMADDR", RTM_NEWMADDR);
   2291  1.24  itojun #endif
   2292  1.24  itojun #ifdef RTM_DELMADDR
   2293  1.24  itojun 	RTTYPE("DELMADDR", RTM_DELMADDR);
   2294  1.24  itojun #endif
   2295   1.1  itojun #undef RTTYPE
   2296   1.1  itojun 	return NULL;
   2297   1.1  itojun }
   2298   1.1  itojun 
   2299   1.7   itohy const char *
   2300  1.36     wiz rtflags(struct rt_msghdr *rtm)
   2301   1.1  itojun {
   2302   1.1  itojun 	static char buf[BUFSIZ];
   2303   1.1  itojun 
   2304  1.24  itojun 	/*
   2305  1.24  itojun 	 * letter conflict should be okay.  painful when *BSD diverges...
   2306  1.24  itojun 	 */
   2307  1.24  itojun 	strlcpy(buf, "", sizeof(buf));
   2308  1.14  itojun #define	RTFLAG(s, f) \
   2309  1.14  itojun do { \
   2310  1.14  itojun 	if (rtm->rtm_flags & (f)) \
   2311  1.24  itojun 		strlcat(buf, (s), sizeof(buf)); \
   2312  1.14  itojun } while (0)
   2313   1.1  itojun 	RTFLAG("U", RTF_UP);
   2314   1.1  itojun 	RTFLAG("G", RTF_GATEWAY);
   2315   1.1  itojun 	RTFLAG("H", RTF_HOST);
   2316   1.1  itojun 	RTFLAG("R", RTF_REJECT);
   2317   1.1  itojun 	RTFLAG("D", RTF_DYNAMIC);
   2318   1.1  itojun 	RTFLAG("M", RTF_MODIFIED);
   2319   1.1  itojun 	RTFLAG("d", RTF_DONE);
   2320   1.1  itojun #ifdef	RTF_MASK
   2321   1.1  itojun 	RTFLAG("m", RTF_MASK);
   2322   1.1  itojun #endif
   2323   1.1  itojun 	RTFLAG("C", RTF_CLONING);
   2324  1.20  itojun #ifdef RTF_CLONED
   2325  1.20  itojun 	RTFLAG("c", RTF_CLONED);
   2326  1.20  itojun #endif
   2327  1.20  itojun #ifdef RTF_PRCLONING
   2328  1.20  itojun 	RTFLAG("c", RTF_PRCLONING);
   2329  1.20  itojun #endif
   2330  1.20  itojun #ifdef RTF_WASCLONED
   2331  1.20  itojun 	RTFLAG("W", RTF_WASCLONED);
   2332  1.20  itojun #endif
   2333   1.1  itojun 	RTFLAG("X", RTF_XRESOLVE);
   2334   1.1  itojun 	RTFLAG("L", RTF_LLINFO);
   2335   1.1  itojun 	RTFLAG("S", RTF_STATIC);
   2336   1.1  itojun 	RTFLAG("B", RTF_BLACKHOLE);
   2337  1.20  itojun #ifdef RTF_PROTO3
   2338  1.20  itojun 	RTFLAG("3", RTF_PROTO3);
   2339  1.20  itojun #endif
   2340   1.1  itojun 	RTFLAG("2", RTF_PROTO2);
   2341   1.1  itojun 	RTFLAG("1", RTF_PROTO1);
   2342  1.24  itojun #ifdef RTF_BROADCAST
   2343  1.24  itojun 	RTFLAG("b", RTF_BROADCAST);
   2344  1.24  itojun #endif
   2345  1.24  itojun #ifdef RTF_DEFAULT
   2346  1.24  itojun 	RTFLAG("d", RTF_DEFAULT);
   2347  1.24  itojun #endif
   2348  1.24  itojun #ifdef RTF_ISAROUTER
   2349  1.24  itojun 	RTFLAG("r", RTF_ISAROUTER);
   2350  1.24  itojun #endif
   2351  1.24  itojun #ifdef RTF_TUNNEL
   2352  1.24  itojun 	RTFLAG("T", RTF_TUNNEL);
   2353  1.24  itojun #endif
   2354  1.24  itojun #ifdef RTF_AUTH
   2355  1.24  itojun 	RTFLAG("A", RTF_AUTH);
   2356  1.24  itojun #endif
   2357  1.24  itojun #ifdef RTF_CRYPT
   2358  1.24  itojun 	RTFLAG("E", RTF_CRYPT);
   2359  1.24  itojun #endif
   2360   1.1  itojun #undef RTFLAG
   2361   1.1  itojun 	return buf;
   2362   1.1  itojun }
   2363   1.1  itojun 
   2364   1.7   itohy const char *
   2365  1.36     wiz ifflags(int flags)
   2366   1.1  itojun {
   2367   1.1  itojun 	static char buf[BUFSIZ];
   2368   1.1  itojun 
   2369  1.24  itojun 	strlcpy(buf, "", sizeof(buf));
   2370  1.14  itojun #define	IFFLAG(s, f) \
   2371  1.14  itojun do { \
   2372  1.29  itojun 	if (flags & (f)) { \
   2373  1.14  itojun 		if (buf[0]) \
   2374  1.24  itojun 			strlcat(buf, ",", sizeof(buf)); \
   2375  1.29  itojun 		strlcat(buf, (s), sizeof(buf)); \
   2376  1.14  itojun 	} \
   2377  1.14  itojun } while (0)
   2378   1.1  itojun 	IFFLAG("UP", IFF_UP);
   2379   1.1  itojun 	IFFLAG("BROADCAST", IFF_BROADCAST);
   2380   1.1  itojun 	IFFLAG("DEBUG", IFF_DEBUG);
   2381   1.1  itojun 	IFFLAG("LOOPBACK", IFF_LOOPBACK);
   2382   1.1  itojun 	IFFLAG("POINTOPOINT", IFF_POINTOPOINT);
   2383   1.1  itojun #ifdef IFF_NOTRAILERS
   2384   1.1  itojun 	IFFLAG("NOTRAILERS", IFF_NOTRAILERS);
   2385   1.1  itojun #endif
   2386  1.24  itojun #ifdef IFF_SMART
   2387  1.24  itojun 	IFFLAG("SMART", IFF_SMART);
   2388  1.24  itojun #endif
   2389   1.1  itojun 	IFFLAG("RUNNING", IFF_RUNNING);
   2390   1.1  itojun 	IFFLAG("NOARP", IFF_NOARP);
   2391   1.1  itojun 	IFFLAG("PROMISC", IFF_PROMISC);
   2392   1.1  itojun 	IFFLAG("ALLMULTI", IFF_ALLMULTI);
   2393   1.1  itojun 	IFFLAG("OACTIVE", IFF_OACTIVE);
   2394   1.1  itojun 	IFFLAG("SIMPLEX", IFF_SIMPLEX);
   2395   1.1  itojun 	IFFLAG("LINK0", IFF_LINK0);
   2396   1.1  itojun 	IFFLAG("LINK1", IFF_LINK1);
   2397   1.1  itojun 	IFFLAG("LINK2", IFF_LINK2);
   2398   1.1  itojun 	IFFLAG("MULTICAST", IFF_MULTICAST);
   2399   1.1  itojun #undef IFFLAG
   2400   1.1  itojun 	return buf;
   2401   1.1  itojun }
   2402   1.1  itojun 
   2403   1.7   itohy void
   2404  1.36     wiz krtread(int again)
   2405   1.1  itojun {
   2406   1.1  itojun 	int mib[6];
   2407   1.1  itojun 	size_t msize;
   2408   1.1  itojun 	char *buf, *p, *lim;
   2409   1.1  itojun 	struct rt_msghdr *rtm;
   2410   1.1  itojun 	int retry;
   2411   1.7   itohy 	const char *errmsg;
   2412   1.1  itojun 
   2413   1.1  itojun 	retry = 0;
   2414   1.1  itojun 	buf = NULL;
   2415   1.1  itojun 	mib[0] = CTL_NET;
   2416   1.1  itojun 	mib[1] = PF_ROUTE;
   2417   1.1  itojun 	mib[2] = 0;
   2418   1.1  itojun 	mib[3] = AF_INET6;	/* Address family */
   2419   1.1  itojun 	mib[4] = NET_RT_DUMP;	/* Dump the kernel routing table */
   2420   1.1  itojun 	mib[5] = 0;		/* No flags */
   2421   1.1  itojun 	do {
   2422   1.1  itojun 		retry++;
   2423   1.1  itojun 		errmsg = NULL;
   2424   1.1  itojun 		if (buf)
   2425   1.1  itojun 			free(buf);
   2426   1.1  itojun 		if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
   2427   1.1  itojun 			errmsg = "sysctl estimate";
   2428   1.1  itojun 			continue;
   2429   1.1  itojun 		}
   2430   1.1  itojun 		if ((buf = malloc(msize)) == NULL) {
   2431   1.1  itojun 			errmsg = "malloc";
   2432   1.1  itojun 			continue;
   2433   1.1  itojun 		}
   2434   1.1  itojun 		if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
   2435   1.1  itojun 			errmsg = "sysctl NET_RT_DUMP";
   2436   1.1  itojun 			continue;
   2437   1.1  itojun 		}
   2438   1.1  itojun 	} while (retry < 5 && errmsg != NULL);
   2439  1.21  itojun 	if (errmsg) {
   2440  1.19  itojun 		fatal("%s (with %d retries, msize=%lu)", errmsg, retry,
   2441  1.19  itojun 		    (u_long)msize);
   2442  1.21  itojun 		/*NOTREACHED*/
   2443  1.21  itojun 	} else if (1 < retry)
   2444   1.1  itojun 		syslog(LOG_INFO, "NET_RT_DUMP %d retires", retry);
   2445   1.1  itojun 
   2446   1.1  itojun 	lim = buf + msize;
   2447   1.1  itojun 	for (p = buf; p < lim; p += rtm->rtm_msglen) {
   2448   1.1  itojun 		rtm = (struct rt_msghdr *)p;
   2449   1.1  itojun 		rt_entry(rtm, again);
   2450   1.1  itojun 	}
   2451   1.1  itojun 	free(buf);
   2452   1.1  itojun }
   2453   1.1  itojun 
   2454   1.7   itohy void
   2455  1.36     wiz rt_entry(struct rt_msghdr *rtm, int again)
   2456   1.1  itojun {
   2457   1.1  itojun 	struct	sockaddr_in6 *sin6_dst, *sin6_gw, *sin6_mask;
   2458   1.1  itojun 	struct	sockaddr_in6 *sin6_genmask, *sin6_ifp;
   2459   1.1  itojun 	char	*rtmp, *ifname = NULL;
   2460  1.29  itojun 	struct	riprt *rrt, *orrt;
   2461   1.1  itojun 	struct	netinfo6 *np;
   2462   1.1  itojun 	int	s;
   2463   1.1  itojun 
   2464   1.1  itojun 	sin6_dst = sin6_gw = sin6_mask = sin6_genmask = sin6_ifp = 0;
   2465   1.1  itojun 	if ((rtm->rtm_flags & RTF_UP) == 0 || rtm->rtm_flags &
   2466  1.12  itojun 		(RTF_CLONING|RTF_XRESOLVE|RTF_LLINFO|RTF_BLACKHOLE)) {
   2467   1.1  itojun 		return;		/* not interested in the link route */
   2468  1.12  itojun 	}
   2469  1.20  itojun 	/* do not look at cloned routes */
   2470  1.20  itojun #ifdef RTF_WASCLONED
   2471  1.20  itojun 	if (rtm->rtm_flags & RTF_WASCLONED)
   2472  1.20  itojun 		return;
   2473  1.20  itojun #endif
   2474  1.20  itojun #ifdef RTF_CLONED
   2475  1.20  itojun 	if (rtm->rtm_flags & RTF_CLONED)
   2476  1.20  itojun 		return;
   2477  1.20  itojun #endif
   2478  1.20  itojun 	/*
   2479  1.20  itojun 	 * do not look at dynamic routes.
   2480  1.20  itojun 	 * netbsd/openbsd cloned routes have UGHD.
   2481  1.20  itojun 	 */
   2482  1.20  itojun 	if (rtm->rtm_flags & RTF_DYNAMIC)
   2483  1.20  itojun 		return;
   2484   1.1  itojun 	rtmp = (char *)(rtm + 1);
   2485   1.1  itojun 	/* Destination */
   2486   1.1  itojun 	if ((rtm->rtm_addrs & RTA_DST) == 0)
   2487   1.1  itojun 		return;		/* ignore routes without destination address */
   2488   1.1  itojun 	sin6_dst = (struct sockaddr_in6 *)rtmp;
   2489  1.17  itojun 	rtmp += ROUNDUP(sin6_dst->sin6_len);
   2490   1.1  itojun 	if (rtm->rtm_addrs & RTA_GATEWAY) {
   2491   1.1  itojun 		sin6_gw = (struct sockaddr_in6 *)rtmp;
   2492   1.8  itojun 		rtmp += ROUNDUP(sin6_gw->sin6_len);
   2493   1.1  itojun 	}
   2494   1.1  itojun 	if (rtm->rtm_addrs & RTA_NETMASK) {
   2495   1.1  itojun 		sin6_mask = (struct sockaddr_in6 *)rtmp;
   2496   1.8  itojun 		rtmp += ROUNDUP(sin6_mask->sin6_len);
   2497   1.1  itojun 	}
   2498   1.1  itojun 	if (rtm->rtm_addrs & RTA_GENMASK) {
   2499   1.1  itojun 		sin6_genmask = (struct sockaddr_in6 *)rtmp;
   2500   1.8  itojun 		rtmp += ROUNDUP(sin6_genmask->sin6_len);
   2501   1.1  itojun 	}
   2502   1.1  itojun 	if (rtm->rtm_addrs & RTA_IFP) {
   2503   1.1  itojun 		sin6_ifp = (struct sockaddr_in6 *)rtmp;
   2504   1.8  itojun 		rtmp += ROUNDUP(sin6_ifp->sin6_len);
   2505   1.1  itojun 	}
   2506   1.1  itojun 
   2507   1.1  itojun 	/* Destination */
   2508   1.1  itojun 	if (sin6_dst->sin6_family != AF_INET6)
   2509   1.1  itojun 		return;
   2510   1.1  itojun 	if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst->sin6_addr))
   2511   1.1  itojun 		return;		/* Link-local */
   2512   1.1  itojun 	if (IN6_ARE_ADDR_EQUAL(&sin6_dst->sin6_addr, &in6addr_loopback))
   2513   1.1  itojun 		return;		/* Loopback */
   2514   1.1  itojun 	if (IN6_IS_ADDR_MULTICAST(&sin6_dst->sin6_addr))
   2515   1.1  itojun 		return;
   2516   1.1  itojun 
   2517  1.21  itojun 	if ((rrt = MALLOC(struct riprt)) == NULL) {
   2518   1.1  itojun 		fatal("malloc: struct riprt");
   2519  1.21  itojun 		/*NOTREACHED*/
   2520  1.21  itojun 	}
   2521  1.12  itojun 	memset(rrt, 0, sizeof(*rrt));
   2522   1.1  itojun 	np = &rrt->rrt_info;
   2523   1.1  itojun 	rrt->rrt_same = NULL;
   2524   1.1  itojun 	rrt->rrt_t = time(NULL);
   2525   1.1  itojun 	if (aflag == 0 && (rtm->rtm_flags & RTF_STATIC))
   2526   1.1  itojun 		rrt->rrt_t = 0;	/* Don't age static routes */
   2527   1.1  itojun 	np->rip6_tag = 0;
   2528   1.1  itojun 	np->rip6_metric = rtm->rtm_rmx.rmx_hopcount;
   2529   1.1  itojun 	if (np->rip6_metric < 1)
   2530   1.1  itojun 		np->rip6_metric = 1;
   2531   1.1  itojun 	rrt->rrt_flags = rtm->rtm_flags;
   2532   1.1  itojun 	np->rip6_dest = sin6_dst->sin6_addr;
   2533   1.1  itojun 
   2534   1.1  itojun 	/* Mask or plen */
   2535   1.1  itojun 	if (rtm->rtm_flags & RTF_HOST)
   2536   1.1  itojun 		np->rip6_plen = 128;	/* Host route */
   2537  1.24  itojun 	else if (sin6_mask)
   2538  1.24  itojun 		np->rip6_plen = sin6mask2len(sin6_mask);
   2539  1.24  itojun 	else
   2540   1.1  itojun 		np->rip6_plen = 0;
   2541   1.1  itojun 
   2542  1.29  itojun 	orrt = rtsearch(np, NULL);
   2543  1.29  itojun 	if (orrt && orrt->rrt_info.rip6_metric != HOPCNT_INFINITY6) {
   2544   1.1  itojun 		/* Already found */
   2545   1.1  itojun 		if (!again) {
   2546   1.1  itojun 			trace(1, "route: %s/%d flags %s: already registered\n",
   2547   1.1  itojun 				inet6_n2p(&np->rip6_dest), np->rip6_plen,
   2548   1.1  itojun 				rtflags(rtm));
   2549   1.1  itojun 		}
   2550   1.1  itojun 		free(rrt);
   2551   1.1  itojun 		return;
   2552   1.1  itojun 	}
   2553   1.1  itojun 	/* Gateway */
   2554   1.1  itojun 	if (!sin6_gw)
   2555   1.1  itojun 		memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
   2556   1.1  itojun 	else {
   2557   1.1  itojun 		if (sin6_gw->sin6_family == AF_INET6)
   2558   1.1  itojun 			rrt->rrt_gw = sin6_gw->sin6_addr;
   2559   1.1  itojun 		else if (sin6_gw->sin6_family == AF_LINK) {
   2560   1.1  itojun 			/* XXX in case ppp link? */
   2561   1.1  itojun 			rrt->rrt_gw = in6addr_loopback;
   2562   1.1  itojun 		} else
   2563   1.1  itojun 			memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
   2564   1.1  itojun 	}
   2565   1.1  itojun 	trace(1, "route: %s/%d flags %s",
   2566   1.1  itojun 		inet6_n2p(&np->rip6_dest), np->rip6_plen, rtflags(rtm));
   2567   1.1  itojun 	trace(1, " gw %s", inet6_n2p(&rrt->rrt_gw));
   2568   1.1  itojun 
   2569   1.1  itojun 	/* Interface */
   2570   1.1  itojun 	s = rtm->rtm_index;
   2571   1.1  itojun 	if (s < nindex2ifc && index2ifc[s])
   2572   1.1  itojun 		ifname = index2ifc[s]->ifc_name;
   2573  1.11  itojun 	else {
   2574  1.11  itojun 		trace(1, " not configured\n");
   2575  1.12  itojun 		free(rrt);
   2576  1.11  itojun 		return;
   2577  1.11  itojun 	}
   2578  1.12  itojun 	trace(1, " if %s sock %d", ifname, s);
   2579   1.1  itojun 	rrt->rrt_index = s;
   2580   1.1  itojun 
   2581  1.12  itojun 	trace(1, "\n");
   2582  1.12  itojun 
   2583   1.1  itojun 	/* Check gateway */
   2584   1.1  itojun 	if (!IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_gw) &&
   2585   1.1  itojun 	    !IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)
   2586   1.1  itojun #ifdef __FreeBSD__
   2587   1.1  itojun 	 && (rrt->rrt_flags & RTF_LOCAL) == 0
   2588   1.1  itojun #endif
   2589   1.1  itojun 	    ) {
   2590   1.1  itojun 		trace(0, "***** Gateway %s is not a link-local address.\n",
   2591   1.1  itojun 			inet6_n2p(&rrt->rrt_gw));
   2592   1.1  itojun 		trace(0, "*****     dest(%s) if(%s) -- Not optimized.\n",
   2593  1.12  itojun 			inet6_n2p(&rrt->rrt_info.rip6_dest), ifname);
   2594  1.13  itojun 		rrt->rrt_rflags |= RRTF_NH_NOT_LLADDR;
   2595   1.1  itojun 	}
   2596   1.1  itojun 
   2597   1.1  itojun 	/* Put it to the route list */
   2598  1.29  itojun 	if (orrt && orrt->rrt_info.rip6_metric == HOPCNT_INFINITY6) {
   2599  1.29  itojun 		/* replace route list */
   2600  1.29  itojun 		rrt->rrt_next = orrt->rrt_next;
   2601  1.29  itojun 		*orrt = *rrt;
   2602  1.29  itojun 		trace(1, "route: %s/%d flags %s: replace new route\n",
   2603  1.29  itojun 		    inet6_n2p(&np->rip6_dest), np->rip6_plen,
   2604  1.29  itojun 		    rtflags(rtm));
   2605  1.29  itojun 		free(rrt);
   2606  1.29  itojun 	} else {
   2607  1.29  itojun 		rrt->rrt_next = riprt;
   2608  1.29  itojun 		riprt = rrt;
   2609  1.29  itojun 	}
   2610   1.1  itojun }
   2611   1.1  itojun 
   2612   1.7   itohy int
   2613  1.36     wiz addroute(struct riprt *rrt, const struct in6_addr *gw, struct ifc *ifcp)
   2614   1.1  itojun {
   2615   1.1  itojun 	struct	netinfo6 *np;
   2616   1.1  itojun 	u_char	buf[BUFSIZ], buf1[BUFSIZ], buf2[BUFSIZ];
   2617   1.1  itojun 	struct	rt_msghdr	*rtm;
   2618  1.29  itojun 	struct	sockaddr_in6	*sin6;
   2619   1.1  itojun 	int	len;
   2620   1.1  itojun 
   2621   1.1  itojun 	np = &rrt->rrt_info;
   2622  1.24  itojun 	inet_ntop(AF_INET6, (const void *)gw, (char *)buf1, sizeof(buf1));
   2623   1.1  itojun 	inet_ntop(AF_INET6, (void *)&ifcp->ifc_mylladdr, (char *)buf2, sizeof(buf2));
   2624   1.1  itojun 	tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n",
   2625   1.1  itojun 		inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
   2626   1.1  itojun 		np->rip6_metric - 1, buf2);
   2627   1.1  itojun 	if (rtlog)
   2628   1.1  itojun 		fprintf(rtlog, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(),
   2629   1.1  itojun 			inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
   2630   1.1  itojun 			np->rip6_metric - 1, buf2);
   2631   1.1  itojun 	if (nflag)
   2632   1.1  itojun 		return 0;
   2633   1.1  itojun 
   2634   1.8  itojun 	memset(buf, 0, sizeof(buf));
   2635   1.1  itojun 	rtm = (struct rt_msghdr *)buf;
   2636   1.1  itojun 	rtm->rtm_type = RTM_ADD;
   2637   1.1  itojun 	rtm->rtm_version = RTM_VERSION;
   2638   1.1  itojun 	rtm->rtm_seq = ++seq;
   2639   1.1  itojun 	rtm->rtm_pid = pid;
   2640  1.13  itojun 	rtm->rtm_flags = rrt->rrt_flags;
   2641   1.1  itojun 	rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
   2642   1.1  itojun 	rtm->rtm_rmx.rmx_hopcount = np->rip6_metric - 1;
   2643   1.1  itojun 	rtm->rtm_inits = RTV_HOPCOUNT;
   2644  1.29  itojun 	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
   2645   1.1  itojun 	/* Destination */
   2646  1.29  itojun 	sin6->sin6_len = sizeof(struct sockaddr_in6);
   2647  1.29  itojun 	sin6->sin6_family = AF_INET6;
   2648  1.29  itojun 	sin6->sin6_addr = np->rip6_dest;
   2649  1.29  itojun 	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
   2650   1.1  itojun 	/* Gateway */
   2651  1.29  itojun 	sin6->sin6_len = sizeof(struct sockaddr_in6);
   2652  1.29  itojun 	sin6->sin6_family = AF_INET6;
   2653  1.29  itojun 	sin6->sin6_addr = *gw;
   2654  1.29  itojun 	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
   2655   1.1  itojun 	/* Netmask */
   2656  1.29  itojun 	sin6->sin6_len = sizeof(struct sockaddr_in6);
   2657  1.29  itojun 	sin6->sin6_family = AF_INET6;
   2658  1.29  itojun 	sin6->sin6_addr = *(plen2mask(np->rip6_plen));
   2659  1.29  itojun 	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
   2660   1.8  itojun 
   2661  1.29  itojun 	len = (char *)sin6 - (char *)buf;
   2662   1.8  itojun 	rtm->rtm_msglen = len;
   2663   1.1  itojun 	if (write(rtsock, buf, len) > 0)
   2664   1.1  itojun 		return 0;
   2665   1.1  itojun 
   2666   1.1  itojun 	if (errno == EEXIST) {
   2667   1.1  itojun 		trace(0, "ADD: Route already exists %s/%d gw %s\n",
   2668   1.1  itojun 			inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
   2669   1.1  itojun 		if (rtlog)
   2670   1.1  itojun 			fprintf(rtlog, "ADD: Route already exists %s/%d gw %s\n",
   2671   1.1  itojun 				inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
   2672   1.1  itojun 	} else {
   2673   1.1  itojun 		trace(0, "Can not write to rtsock (addroute): %s\n",
   2674   1.1  itojun 			strerror(errno));
   2675   1.1  itojun 		if (rtlog)
   2676   1.1  itojun 			fprintf(rtlog, "\tCan not write to rtsock: %s\n",
   2677   1.1  itojun 				strerror(errno));
   2678   1.1  itojun 	}
   2679   1.1  itojun 	return -1;
   2680   1.1  itojun }
   2681   1.1  itojun 
   2682   1.7   itohy int
   2683  1.36     wiz delroute(struct netinfo6 *np, struct in6_addr *gw)
   2684   1.1  itojun {
   2685   1.1  itojun 	u_char	buf[BUFSIZ], buf2[BUFSIZ];
   2686   1.1  itojun 	struct	rt_msghdr	*rtm;
   2687  1.29  itojun 	struct	sockaddr_in6	*sin6;
   2688   1.1  itojun 	int	len;
   2689   1.1  itojun 
   2690   1.1  itojun 	inet_ntop(AF_INET6, (void *)gw, (char *)buf2, sizeof(buf2));
   2691   1.1  itojun 	tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np->rip6_dest),
   2692   1.1  itojun 		np->rip6_plen, buf2);
   2693   1.1  itojun 	if (rtlog)
   2694   1.1  itojun 		fprintf(rtlog, "%s: DEL: %s/%d gw %s\n",
   2695   1.1  itojun 			hms(), inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
   2696   1.1  itojun 	if (nflag)
   2697   1.1  itojun 		return 0;
   2698   1.1  itojun 
   2699   1.8  itojun 	memset(buf, 0, sizeof(buf));
   2700   1.1  itojun 	rtm = (struct rt_msghdr *)buf;
   2701   1.1  itojun 	rtm->rtm_type = RTM_DELETE;
   2702   1.1  itojun 	rtm->rtm_version = RTM_VERSION;
   2703   1.1  itojun 	rtm->rtm_seq = ++seq;
   2704   1.1  itojun 	rtm->rtm_pid = pid;
   2705   1.1  itojun 	rtm->rtm_flags = RTF_UP | RTF_GATEWAY;
   2706  1.25  itojun 	if (np->rip6_plen == sizeof(struct in6_addr) * 8)
   2707  1.25  itojun 		rtm->rtm_flags |= RTF_HOST;
   2708   1.1  itojun 	rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
   2709  1.29  itojun 	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
   2710   1.1  itojun 	/* Destination */
   2711  1.29  itojun 	sin6->sin6_len = sizeof(struct sockaddr_in6);
   2712  1.29  itojun 	sin6->sin6_family = AF_INET6;
   2713  1.29  itojun 	sin6->sin6_addr = np->rip6_dest;
   2714  1.29  itojun 	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
   2715   1.1  itojun 	/* Gateway */
   2716  1.29  itojun 	sin6->sin6_len = sizeof(struct sockaddr_in6);
   2717  1.29  itojun 	sin6->sin6_family = AF_INET6;
   2718  1.29  itojun 	sin6->sin6_addr = *gw;
   2719  1.29  itojun 	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
   2720   1.1  itojun 	/* Netmask */
   2721  1.29  itojun 	sin6->sin6_len = sizeof(struct sockaddr_in6);
   2722  1.29  itojun 	sin6->sin6_family = AF_INET6;
   2723  1.29  itojun 	sin6->sin6_addr = *(plen2mask(np->rip6_plen));
   2724  1.29  itojun 	sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
   2725   1.8  itojun 
   2726  1.29  itojun 	len = (char *)sin6 - (char *)buf;
   2727   1.8  itojun 	rtm->rtm_msglen = len;
   2728   1.1  itojun 	if (write(rtsock, buf, len) >= 0)
   2729   1.1  itojun 		return 0;
   2730   1.1  itojun 
   2731   1.1  itojun 	if (errno == ESRCH) {
   2732   1.1  itojun 		trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n",
   2733   1.1  itojun 			inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
   2734   1.1  itojun 		if (rtlog)
   2735   1.1  itojun 			fprintf(rtlog, "RTDEL: Route does not exist: %s/%d gw %s\n",
   2736   1.1  itojun 				inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
   2737   1.1  itojun 	} else {
   2738   1.1  itojun 		trace(0, "Can not write to rtsock (delroute): %s\n",
   2739   1.1  itojun 			strerror(errno));
   2740   1.1  itojun 		if (rtlog)
   2741   1.1  itojun 			fprintf(rtlog, "\tCan not write to rtsock: %s\n",
   2742   1.1  itojun 				strerror(errno));
   2743   1.1  itojun 	}
   2744   1.1  itojun 	return -1;
   2745   1.1  itojun }
   2746   1.1  itojun 
   2747   1.1  itojun struct in6_addr *
   2748  1.36     wiz getroute(struct netinfo6 *np, struct in6_addr *gw)
   2749   1.1  itojun {
   2750   1.1  itojun 	u_char buf[BUFSIZ];
   2751  1.33  itojun 	int myseq;
   2752   1.1  itojun 	int len;
   2753   1.1  itojun 	struct rt_msghdr *rtm;
   2754  1.29  itojun 	struct sockaddr_in6 *sin6;
   2755   1.1  itojun 
   2756   1.1  itojun 	rtm = (struct rt_msghdr *)buf;
   2757   1.1  itojun 	len = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in6);
   2758   1.1  itojun 	memset(rtm, 0, len);
   2759   1.1  itojun 	rtm->rtm_type = RTM_GET;
   2760   1.1  itojun 	rtm->rtm_version = RTM_VERSION;
   2761   1.1  itojun 	myseq = ++seq;
   2762   1.1  itojun 	rtm->rtm_seq = myseq;
   2763   1.1  itojun 	rtm->rtm_addrs = RTA_DST;
   2764   1.1  itojun 	rtm->rtm_msglen = len;
   2765  1.29  itojun 	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
   2766  1.29  itojun 	sin6->sin6_len = sizeof(struct sockaddr_in6);
   2767  1.29  itojun 	sin6->sin6_family = AF_INET6;
   2768  1.29  itojun 	sin6->sin6_addr = np->rip6_dest;
   2769   1.1  itojun 	if (write(rtsock, buf, len) < 0) {
   2770   1.1  itojun 		if (errno == ESRCH)	/* No such route found */
   2771   1.1  itojun 			return NULL;
   2772   1.1  itojun 		perror("write to rtsock");
   2773  1.26     wiz 		exit(1);
   2774   1.1  itojun 	}
   2775   1.1  itojun 	do {
   2776   1.1  itojun 		if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
   2777   1.1  itojun 			perror("read from rtsock");
   2778  1.26     wiz 			exit(1);
   2779   1.1  itojun 		}
   2780   1.1  itojun 		rtm = (struct rt_msghdr *)buf;
   2781   1.1  itojun 	} while (rtm->rtm_seq != myseq || rtm->rtm_pid != pid);
   2782  1.29  itojun 	sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
   2783   1.8  itojun 	if (rtm->rtm_addrs & RTA_DST) {
   2784  1.29  itojun 		sin6 = (struct sockaddr_in6 *)
   2785  1.29  itojun 			((char *)sin6 + ROUNDUP(sin6->sin6_len));
   2786   1.8  itojun 	}
   2787   1.1  itojun 	if (rtm->rtm_addrs & RTA_GATEWAY) {
   2788  1.29  itojun 		*gw = sin6->sin6_addr;
   2789   1.1  itojun 		return gw;
   2790   1.1  itojun 	}
   2791   1.1  itojun 	return NULL;
   2792   1.1  itojun }
   2793   1.1  itojun 
   2794   1.7   itohy const char *
   2795  1.36     wiz inet6_n2p(const struct in6_addr *p)
   2796   1.1  itojun {
   2797   1.1  itojun 	static char buf[BUFSIZ];
   2798   1.1  itojun 
   2799  1.24  itojun 	return inet_ntop(AF_INET6, (const void *)p, buf, sizeof(buf));
   2800   1.1  itojun }
   2801   1.1  itojun 
   2802   1.7   itohy void
   2803  1.36     wiz ifrtdump(int sig)
   2804   1.1  itojun {
   2805   1.7   itohy 
   2806   1.1  itojun 	ifdump(sig);
   2807   1.1  itojun 	rtdump(sig);
   2808   1.1  itojun }
   2809   1.1  itojun 
   2810   1.7   itohy void
   2811  1.36     wiz ifdump(int sig)
   2812   1.1  itojun {
   2813   1.1  itojun 	struct ifc *ifcp;
   2814   1.1  itojun 	FILE *dump;
   2815   1.1  itojun 	int i;
   2816   1.1  itojun 
   2817   1.1  itojun 	if (sig == 0)
   2818   1.1  itojun 		dump = stderr;
   2819   1.1  itojun 	else
   2820   1.1  itojun 		if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
   2821   1.1  itojun 			dump = stderr;
   2822   1.1  itojun 
   2823   1.1  itojun 	fprintf(dump, "%s: Interface Table Dump\n", hms());
   2824   1.1  itojun 	fprintf(dump, "  Number of interfaces: %d\n", nifc);
   2825   1.1  itojun 	for (i = 0; i < 2; i++) {
   2826   1.1  itojun 		fprintf(dump, "  %sadvertising interfaces:\n", i ? "non-" : "");
   2827   1.1  itojun 		for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
   2828   1.1  itojun 			if (i == 0) {
   2829   1.1  itojun 				if ((ifcp->ifc_flags & IFF_UP) == 0)
   2830   1.1  itojun 					continue;
   2831   1.1  itojun 				if (iff_find(ifcp, 'N') != NULL)
   2832   1.1  itojun 					continue;
   2833   1.1  itojun 			} else {
   2834   1.1  itojun 				if (ifcp->ifc_flags & IFF_UP)
   2835   1.1  itojun 					continue;
   2836   1.1  itojun 			}
   2837   1.1  itojun 			ifdump0(dump, ifcp);
   2838   1.1  itojun 		}
   2839   1.1  itojun 	}
   2840   1.1  itojun 	fprintf(dump, "\n");
   2841   1.1  itojun 	if (dump != stderr)
   2842   1.1  itojun 		fclose(dump);
   2843   1.1  itojun }
   2844   1.1  itojun 
   2845   1.7   itohy void
   2846  1.36     wiz ifdump0(FILE *dump, const struct ifc *ifcp)
   2847   1.1  itojun {
   2848   1.1  itojun 	struct ifac *ifa;
   2849   1.1  itojun 	struct iff *iffp;
   2850   1.1  itojun 	char buf[BUFSIZ];
   2851   1.7   itohy 	const char *ft;
   2852   1.1  itojun 	int addr;
   2853   1.1  itojun 
   2854   1.1  itojun 	fprintf(dump, "    %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n",
   2855   1.1  itojun 		ifcp->ifc_name, ifcp->ifc_index, ifflags(ifcp->ifc_flags),
   2856   1.1  itojun 		inet6_n2p(&ifcp->ifc_mylladdr),
   2857   1.1  itojun 		ifcp->ifc_mtu, ifcp->ifc_metric);
   2858   1.1  itojun 	for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
   2859   1.1  itojun 		if (ifcp->ifc_flags & IFF_POINTOPOINT) {
   2860   1.1  itojun 			inet_ntop(AF_INET6, (void *)&ifa->ifa_raddr,
   2861   1.1  itojun 				buf, sizeof(buf));
   2862   1.1  itojun 			fprintf(dump, "\t%s/%d -- %s\n",
   2863   1.1  itojun 				inet6_n2p(&ifa->ifa_addr),
   2864   1.1  itojun 				ifa->ifa_plen, buf);
   2865   1.1  itojun 		} else {
   2866   1.1  itojun 			fprintf(dump, "\t%s/%d\n",
   2867   1.1  itojun 				inet6_n2p(&ifa->ifa_addr),
   2868   1.1  itojun 				ifa->ifa_plen);
   2869   1.1  itojun 		}
   2870   1.1  itojun 	}
   2871   1.1  itojun 	if (ifcp->ifc_filter) {
   2872   1.1  itojun 		fprintf(dump, "\tFilter:");
   2873   1.1  itojun 		for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
   2874   1.1  itojun 			addr = 0;
   2875   1.1  itojun 			switch (iffp->iff_type) {
   2876   1.1  itojun 			case 'A':
   2877   1.1  itojun 				ft = "Aggregate"; addr++; break;
   2878   1.1  itojun 			case 'N':
   2879  1.24  itojun 				ft = "No-use"; break;
   2880   1.1  itojun 			case 'O':
   2881   1.1  itojun 				ft = "Advertise-only"; addr++; break;
   2882   1.1  itojun 			case 'T':
   2883   1.1  itojun 				ft = "Default-only"; break;
   2884   1.1  itojun 			case 'L':
   2885   1.1  itojun 				ft = "Listen-only"; addr++; break;
   2886   1.1  itojun 			default:
   2887   1.1  itojun 				snprintf(buf, sizeof(buf), "Unknown-%c", iffp->iff_type);
   2888   1.1  itojun 				ft = buf;
   2889   1.1  itojun 				addr++;
   2890   1.1  itojun 				break;
   2891   1.1  itojun 			}
   2892   1.1  itojun 			fprintf(dump, " %s", ft);
   2893   1.1  itojun 			if (addr) {
   2894   1.1  itojun 				fprintf(dump, "(%s/%d)", inet6_n2p(&iffp->iff_addr),
   2895   1.1  itojun 					iffp->iff_plen);
   2896   1.1  itojun 			}
   2897   1.1  itojun 		}
   2898   1.1  itojun 		fprintf(dump, "\n");
   2899   1.1  itojun 	}
   2900   1.1  itojun }
   2901   1.1  itojun 
   2902   1.7   itohy void
   2903  1.36     wiz rtdump(int sig)
   2904   1.1  itojun {
   2905   1.1  itojun 	struct	riprt *rrt;
   2906   1.1  itojun 	char	buf[BUFSIZ];
   2907   1.1  itojun 	FILE	*dump;
   2908   1.1  itojun 	time_t	t, age;
   2909   1.1  itojun 
   2910   1.1  itojun 	if (sig == 0)
   2911   1.1  itojun 		dump = stderr;
   2912   1.1  itojun 	else
   2913   1.1  itojun 		if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
   2914   1.1  itojun 			dump = stderr;
   2915   1.1  itojun 
   2916   1.1  itojun 	t = time(NULL);
   2917   1.1  itojun 	fprintf(dump, "\n%s: Routing Table Dump\n", hms());
   2918   1.1  itojun 	for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
   2919   1.1  itojun 		if (rrt->rrt_t == 0)
   2920   1.1  itojun 			age = 0;
   2921   1.1  itojun 		else
   2922   1.1  itojun 			age = t - rrt->rrt_t;
   2923   1.1  itojun 		inet_ntop(AF_INET6, (void *)&rrt->rrt_info.rip6_dest,
   2924   1.1  itojun 			buf, sizeof(buf));
   2925   1.1  itojun 		fprintf(dump, "    %s/%d if(%d:%s) gw(%s) [%d] age(%ld)",
   2926   1.1  itojun 			buf, rrt->rrt_info.rip6_plen, rrt->rrt_index,
   2927   1.1  itojun 			index2ifc[rrt->rrt_index]->ifc_name,
   2928   1.1  itojun 			inet6_n2p(&rrt->rrt_gw),
   2929   1.1  itojun 			rrt->rrt_info.rip6_metric, (long)age);
   2930   1.1  itojun 		if (rrt->rrt_info.rip6_tag) {
   2931   1.1  itojun 			fprintf(dump, " tag(0x%04x)",
   2932   1.1  itojun 				ntohs(rrt->rrt_info.rip6_tag) & 0xffff);
   2933   1.1  itojun 		}
   2934  1.13  itojun 		if (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)
   2935   1.1  itojun 			fprintf(dump, " NOT-LL");
   2936  1.13  itojun 		if (rrt->rrt_rflags & RRTF_NOADVERTISE)
   2937   1.1  itojun 			fprintf(dump, " NO-ADV");
   2938   1.1  itojun 		fprintf(dump, "\n");
   2939   1.1  itojun 	}
   2940   1.1  itojun 	fprintf(dump, "\n");
   2941   1.1  itojun 	if (dump != stderr)
   2942   1.1  itojun 		fclose(dump);
   2943   1.1  itojun }
   2944   1.1  itojun 
   2945   1.1  itojun /*
   2946   1.1  itojun  * Parse the -A (and -O) options and put corresponding filter object to the
   2947  1.24  itojun  * specified interface structures.  Each of the -A/O option has the following
   2948   1.1  itojun  * syntax:	-A 5f09:c400::/32,ef0,ef1  (aggregate)
   2949   1.1  itojun  * 		-O 5f09:c400::/32,ef0,ef1  (only when match)
   2950   1.1  itojun  */
   2951   1.7   itohy void
   2952  1.36     wiz filterconfig(void)
   2953   1.1  itojun {
   2954   1.1  itojun 	int i;
   2955   1.1  itojun 	char *p, *ap, *iflp, *ifname;
   2956  1.24  itojun 	struct iff ftmp, *iff_obj;
   2957  1.24  itojun 	struct ifc *ifcp;
   2958  1.24  itojun 	struct riprt *rrt;
   2959  1.16  itojun #if 0
   2960  1.24  itojun 	struct in6_addr gw;
   2961  1.16  itojun #endif
   2962   1.1  itojun 
   2963   1.1  itojun 	for (i = 0; i < nfilter; i++) {
   2964   1.1  itojun 		ap = filter[i];
   2965   1.1  itojun 		iflp = NULL;
   2966   1.1  itojun 		ifcp = NULL;
   2967   1.1  itojun 		if (filtertype[i] == 'N' || filtertype[i] == 'T') {
   2968   1.1  itojun 			iflp = ap;
   2969   1.1  itojun 			goto ifonly;
   2970   1.1  itojun 		}
   2971  1.35  itojun 		if ((p = strchr(ap, ',')) != NULL) {
   2972   1.1  itojun 			*p++ = '\0';
   2973   1.1  itojun 			iflp = p;
   2974   1.1  itojun 		}
   2975  1.35  itojun 		if ((p = strchr(ap, '/')) == NULL) {
   2976   1.1  itojun 			fatal("no prefixlen specified for '%s'", ap);
   2977  1.21  itojun 			/*NOTREACHED*/
   2978  1.21  itojun 		}
   2979   1.1  itojun 		*p++ = '\0';
   2980  1.21  itojun 		if (inet_pton(AF_INET6, ap, &ftmp.iff_addr) != 1) {
   2981   1.1  itojun 			fatal("invalid prefix specified for '%s'", ap);
   2982  1.21  itojun 			/*NOTREACHED*/
   2983  1.21  itojun 		}
   2984   1.1  itojun 		ftmp.iff_plen = atoi(p);
   2985   1.1  itojun 		ftmp.iff_next = NULL;
   2986   1.1  itojun 		applyplen(&ftmp.iff_addr, ftmp.iff_plen);
   2987   1.1  itojun ifonly:
   2988   1.1  itojun 		ftmp.iff_type = filtertype[i];
   2989  1.21  itojun 		if (iflp == NULL || *iflp == '\0') {
   2990   1.1  itojun 			fatal("no interface specified for '%s'", ap);
   2991  1.21  itojun 			/*NOTREACHED*/
   2992  1.21  itojun 		}
   2993   1.1  itojun 		/* parse the interface listing portion */
   2994   1.1  itojun 		while (iflp) {
   2995   1.1  itojun 			ifname = iflp;
   2996  1.35  itojun 			if ((iflp = strchr(iflp, ',')) != NULL)
   2997   1.1  itojun 				*iflp++ = '\0';
   2998   1.1  itojun 			ifcp = ifc_find(ifname);
   2999  1.21  itojun 			if (ifcp == NULL) {
   3000   1.1  itojun 				fatal("no interface %s exists", ifname);
   3001  1.21  itojun 				/*NOTREACHED*/
   3002  1.21  itojun 			}
   3003   1.1  itojun 			iff_obj = (struct iff *)malloc(sizeof(struct iff));
   3004  1.21  itojun 			if (iff_obj == NULL) {
   3005   1.1  itojun 				fatal("malloc of iff_obj");
   3006  1.21  itojun 				/*NOTREACHED*/
   3007  1.21  itojun 			}
   3008   1.1  itojun 			memcpy((void *)iff_obj, (void *)&ftmp,
   3009  1.21  itojun 			    sizeof(struct iff));
   3010   1.1  itojun 			/* link it to the interface filter */
   3011   1.1  itojun 			iff_obj->iff_next = ifcp->ifc_filter;
   3012   1.1  itojun 			ifcp->ifc_filter = iff_obj;
   3013   1.1  itojun 		}
   3014  1.24  itojun 
   3015  1.24  itojun 		/*
   3016  1.24  itojun 		 * -A: aggregate configuration.
   3017  1.24  itojun 		 */
   3018   1.1  itojun 		if (filtertype[i] != 'A')
   3019   1.1  itojun 			continue;
   3020   1.1  itojun 		/* put the aggregate to the kernel routing table */
   3021   1.1  itojun 		rrt = (struct riprt *)malloc(sizeof(struct riprt));
   3022  1.21  itojun 		if (rrt == NULL) {
   3023   1.1  itojun 			fatal("malloc: rrt");
   3024  1.21  itojun 			/*NOTREACHED*/
   3025  1.21  itojun 		}
   3026   1.1  itojun 		memset(rrt, 0, sizeof(struct riprt));
   3027   1.1  itojun 		rrt->rrt_info.rip6_dest = ftmp.iff_addr;
   3028   1.1  itojun 		rrt->rrt_info.rip6_plen = ftmp.iff_plen;
   3029   1.1  itojun 		rrt->rrt_info.rip6_metric = 1;
   3030   1.1  itojun 		rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
   3031   1.1  itojun 		rrt->rrt_gw = in6addr_loopback;
   3032  1.13  itojun 		rrt->rrt_flags = RTF_UP | RTF_REJECT;
   3033  1.13  itojun 		rrt->rrt_rflags = RRTF_AGGREGATE;
   3034   1.1  itojun 		rrt->rrt_t = 0;
   3035  1.32  itojun 		rrt->rrt_index = loopifcp->ifc_index;
   3036  1.16  itojun #if 0
   3037  1.15  itojun 		if (getroute(&rrt->rrt_info, &gw)) {
   3038  1.15  itojun #if 0
   3039  1.15  itojun 			/*
   3040  1.15  itojun 			 * When the address has already been registered in the
   3041  1.15  itojun 			 * kernel routing table, it should be removed
   3042  1.15  itojun 			 */
   3043  1.15  itojun 			delroute(&rrt->rrt_info, &gw);
   3044  1.15  itojun #else
   3045  1.24  itojun 			/* it is safer behavior */
   3046  1.15  itojun 			errno = EINVAL;
   3047  1.15  itojun 			fatal("%s/%u already in routing table, "
   3048  1.15  itojun 			    "cannot aggregate",
   3049  1.15  itojun 			    inet6_n2p(&rrt->rrt_info.rip6_dest),
   3050  1.15  itojun 			    rrt->rrt_info.rip6_plen);
   3051  1.21  itojun 			/*NOTREACHED*/
   3052  1.15  itojun #endif
   3053  1.15  itojun 		}
   3054  1.16  itojun #endif
   3055   1.1  itojun 		/* Put the route to the list */
   3056   1.1  itojun 		rrt->rrt_next = riprt;
   3057   1.1  itojun 		riprt = rrt;
   3058   1.1  itojun 		trace(1, "Aggregate: %s/%d for %s\n",
   3059   1.1  itojun 			inet6_n2p(&ftmp.iff_addr), ftmp.iff_plen,
   3060   1.1  itojun 			ifcp->ifc_name);
   3061   1.1  itojun 		/* Add this route to the kernel */
   3062   1.1  itojun 		if (nflag) 	/* do not modify kernel routing table */
   3063   1.1  itojun 			continue;
   3064   1.1  itojun 		addroute(rrt, &in6addr_loopback, loopifcp);
   3065   1.1  itojun 	}
   3066   1.1  itojun }
   3067   1.1  itojun 
   3068   1.1  itojun /***************** utility functions *****************/
   3069   1.1  itojun 
   3070   1.1  itojun /*
   3071   1.1  itojun  * Returns a pointer to ifac whose address and prefix length matches
   3072   1.1  itojun  * with the address and prefix length specified in the arguments.
   3073   1.1  itojun  */
   3074   1.7   itohy struct ifac *
   3075  1.36     wiz ifa_match(const struct ifc *ifcp, const struct in6_addr *ia, int plen)
   3076   1.1  itojun {
   3077   1.1  itojun 	struct ifac *ifa;
   3078   1.1  itojun 
   3079   1.1  itojun 	for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
   3080   1.1  itojun 		if (IN6_ARE_ADDR_EQUAL(&ifa->ifa_addr, ia) &&
   3081   1.1  itojun 		    ifa->ifa_plen == plen)
   3082   1.1  itojun 			break;
   3083   1.1  itojun 	}
   3084   1.1  itojun 	return ifa;
   3085   1.1  itojun }
   3086   1.1  itojun 
   3087   1.1  itojun /*
   3088   1.1  itojun  * Return a pointer to riprt structure whose address and prefix length
   3089   1.1  itojun  * matches with the address and prefix length found in the argument.
   3090  1.24  itojun  * Note: This is not a rtalloc().  Therefore exact match is necessary.
   3091   1.1  itojun  */
   3092   1.7   itohy struct riprt *
   3093  1.36     wiz rtsearch(struct netinfo6 *np, struct riprt **prev_rrt)
   3094   1.1  itojun {
   3095   1.1  itojun 	struct	riprt	*rrt;
   3096   1.1  itojun 
   3097  1.24  itojun 	if (prev_rrt)
   3098  1.24  itojun 		*prev_rrt = NULL;
   3099   1.1  itojun 	for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
   3100   1.1  itojun 		if (rrt->rrt_info.rip6_plen == np->rip6_plen &&
   3101   1.1  itojun 		    IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
   3102   1.1  itojun 				       &np->rip6_dest))
   3103   1.1  itojun 			return rrt;
   3104  1.24  itojun 		if (prev_rrt)
   3105  1.24  itojun 			*prev_rrt = rrt;
   3106   1.1  itojun 	}
   3107  1.24  itojun 	if (prev_rrt)
   3108  1.24  itojun 		*prev_rrt = NULL;
   3109   1.1  itojun 	return 0;
   3110   1.1  itojun }
   3111   1.1  itojun 
   3112   1.7   itohy int
   3113  1.36     wiz sin6mask2len(const struct sockaddr_in6 *sin6)
   3114  1.24  itojun {
   3115  1.24  itojun 
   3116  1.24  itojun 	return mask2len(&sin6->sin6_addr,
   3117  1.24  itojun 	    sin6->sin6_len - offsetof(struct sockaddr_in6, sin6_addr));
   3118  1.24  itojun }
   3119  1.24  itojun 
   3120  1.24  itojun int
   3121  1.36     wiz mask2len(const struct in6_addr *addr, int lenlim)
   3122   1.1  itojun {
   3123   1.1  itojun 	int i = 0, j;
   3124  1.24  itojun 	const u_char *p = (const u_char *)addr;
   3125   1.1  itojun 
   3126   1.1  itojun 	for (j = 0; j < lenlim; j++, p++) {
   3127   1.1  itojun 		if (*p != 0xff)
   3128   1.1  itojun 			break;
   3129   1.1  itojun 		i += 8;
   3130   1.1  itojun 	}
   3131   1.1  itojun 	if (j < lenlim) {
   3132   1.1  itojun 		switch (*p) {
   3133  1.14  itojun #define	MASKLEN(m, l)	case m: do { i += l; break; } while (0)
   3134  1.14  itojun 		MASKLEN(0xfe, 7); break;
   3135  1.14  itojun 		MASKLEN(0xfc, 6); break;
   3136  1.14  itojun 		MASKLEN(0xf8, 5); break;
   3137  1.14  itojun 		MASKLEN(0xf0, 4); break;
   3138  1.14  itojun 		MASKLEN(0xe0, 3); break;
   3139  1.14  itojun 		MASKLEN(0xc0, 2); break;
   3140  1.14  itojun 		MASKLEN(0x80, 1); break;
   3141   1.1  itojun #undef	MASKLEN
   3142   1.1  itojun 		}
   3143   1.1  itojun 	}
   3144   1.1  itojun 	return i;
   3145   1.1  itojun }
   3146   1.1  itojun 
   3147   1.7   itohy void
   3148  1.36     wiz applymask(struct in6_addr *addr, struct in6_addr *mask)
   3149   1.1  itojun {
   3150   1.1  itojun 	int	i;
   3151   1.1  itojun 	u_long	*p, *q;
   3152   1.1  itojun 
   3153   1.1  itojun 	p = (u_long *)addr; q = (u_long *)mask;
   3154   1.1  itojun 	for (i = 0; i < 4; i++)
   3155   1.1  itojun 		*p++ &= *q++;
   3156   1.1  itojun }
   3157   1.1  itojun 
   3158   1.7   itohy static const u_char plent[8] = {
   3159   1.7   itohy 	0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
   3160   1.7   itohy };
   3161   1.1  itojun 
   3162   1.7   itohy void
   3163  1.36     wiz applyplen(struct in6_addr *ia, int plen)
   3164   1.1  itojun {
   3165   1.1  itojun 	u_char	*p;
   3166   1.1  itojun 	int	i;
   3167   1.1  itojun 
   3168   1.8  itojun 	p = ia->s6_addr;
   3169   1.1  itojun 	for (i = 0; i < 16; i++) {
   3170   1.1  itojun 		if (plen <= 0)
   3171   1.1  itojun 			*p = 0;
   3172   1.1  itojun 		else if (plen < 8)
   3173   1.1  itojun 			*p &= plent[plen];
   3174   1.1  itojun 		p++, plen -= 8;
   3175   1.1  itojun 	}
   3176   1.1  itojun }
   3177   1.1  itojun 
   3178   1.7   itohy static const int pl2m[9] = {
   3179   1.7   itohy 	0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
   3180   1.7   itohy };
   3181   1.1  itojun 
   3182   1.7   itohy struct in6_addr *
   3183  1.36     wiz plen2mask(int n)
   3184   1.1  itojun {
   3185   1.1  itojun 	static struct in6_addr ia;
   3186   1.1  itojun 	u_char	*p;
   3187   1.1  itojun 	int	i;
   3188   1.1  itojun 
   3189   1.1  itojun 	memset(&ia, 0, sizeof(struct in6_addr));
   3190   1.1  itojun 	p = (u_char *)&ia;
   3191   1.1  itojun 	for (i = 0; i < 16; i++, p++, n -= 8) {
   3192   1.1  itojun 		if (n >= 8) {
   3193   1.1  itojun 			*p = 0xff;
   3194   1.1  itojun 			continue;
   3195   1.1  itojun 		}
   3196   1.1  itojun 		*p = pl2m[n];
   3197   1.1  itojun 		break;
   3198   1.1  itojun 	}
   3199   1.1  itojun 	return &ia;
   3200   1.1  itojun }
   3201   1.1  itojun 
   3202   1.7   itohy char *
   3203  1.36     wiz allocopy(char *p)
   3204   1.1  itojun {
   3205  1.33  itojun 	int len = strlen(p) + 1;
   3206  1.33  itojun 	char *q = (char *)malloc(len);
   3207  1.32  itojun 
   3208  1.32  itojun 	if (!q) {
   3209  1.32  itojun 		fatal("malloc");
   3210  1.32  itojun 		/*NOTREACHED*/
   3211  1.32  itojun 	}
   3212   1.7   itohy 
   3213  1.33  itojun 	strlcpy(q, p, len);
   3214   1.1  itojun 	return q;
   3215   1.1  itojun }
   3216   1.1  itojun 
   3217   1.7   itohy char *
   3218  1.36     wiz hms(void)
   3219   1.1  itojun {
   3220   1.1  itojun 	static char buf[BUFSIZ];
   3221   1.1  itojun 	time_t t;
   3222   1.1  itojun 	struct	tm *tm;
   3223   1.1  itojun 
   3224   1.1  itojun 	t = time(NULL);
   3225  1.21  itojun 	if ((tm = localtime(&t)) == 0) {
   3226   1.1  itojun 		fatal("localtime");
   3227  1.21  itojun 		/*NOTREACHED*/
   3228  1.21  itojun 	}
   3229  1.24  itojun 	snprintf(buf, sizeof(buf), "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
   3230  1.24  itojun 	    tm->tm_sec);
   3231   1.1  itojun 	return buf;
   3232   1.1  itojun }
   3233   1.1  itojun 
   3234   1.1  itojun #define	RIPRANDDEV	1.0	/* 30 +- 15, max - min = 30 */
   3235   1.1  itojun 
   3236   1.7   itohy int
   3237  1.36     wiz ripinterval(int timer)
   3238   1.1  itojun {
   3239   1.1  itojun 	double r = rand();
   3240   1.7   itohy 
   3241   1.1  itojun 	interval = (int)(timer + timer * RIPRANDDEV * (r / RAND_MAX - 0.5));
   3242   1.1  itojun 	nextalarm = time(NULL) + interval;
   3243   1.1  itojun 	return interval;
   3244   1.1  itojun }
   3245   1.1  itojun 
   3246   1.7   itohy time_t
   3247  1.36     wiz ripsuptrig(void)
   3248   1.1  itojun {
   3249   1.1  itojun 	time_t t;
   3250   1.1  itojun 
   3251   1.1  itojun 	double r = rand();
   3252   1.1  itojun 	t  = (int)(RIP_TRIG_INT6_MIN +
   3253  1.24  itojun 		(RIP_TRIG_INT6_MAX - RIP_TRIG_INT6_MIN) * (r / RAND_MAX));
   3254   1.1  itojun 	sup_trig_update = time(NULL) + t;
   3255   1.1  itojun 	return t;
   3256   1.1  itojun }
   3257   1.1  itojun 
   3258   1.1  itojun void
   3259   1.7   itohy fatal(const char *fmt, ...)
   3260   1.1  itojun {
   3261   1.1  itojun 	va_list ap;
   3262   1.1  itojun 	char buf[1024];
   3263   1.1  itojun 
   3264   1.1  itojun 	va_start(ap, fmt);
   3265   1.1  itojun 	vsnprintf(buf, sizeof(buf), fmt, ap);
   3266  1.30     wiz 	va_end(ap);
   3267   1.1  itojun 	perror(buf);
   3268   1.1  itojun 	syslog(LOG_ERR, "%s: %s", buf, strerror(errno));
   3269  1.24  itojun 	rtdexit();
   3270   1.1  itojun }
   3271   1.1  itojun 
   3272   1.1  itojun void
   3273   1.7   itohy tracet(int level, const char *fmt, ...)
   3274   1.1  itojun {
   3275   1.1  itojun 	va_list ap;
   3276   1.1  itojun 
   3277  1.28  itojun 	if (level <= dflag) {
   3278  1.28  itojun 		va_start(ap, fmt);
   3279   1.1  itojun 		fprintf(stderr, "%s: ", hms());
   3280   1.1  itojun 		vfprintf(stderr, fmt, ap);
   3281  1.28  itojun 		va_end(ap);
   3282   1.1  itojun 	}
   3283   1.1  itojun 	if (dflag) {
   3284  1.28  itojun 		va_start(ap, fmt);
   3285   1.1  itojun 		if (level > 0)
   3286   1.1  itojun 			vsyslog(LOG_DEBUG, fmt, ap);
   3287   1.1  itojun 		else
   3288   1.1  itojun 			vsyslog(LOG_WARNING, fmt, ap);
   3289  1.28  itojun 		va_end(ap);
   3290   1.1  itojun 	}
   3291   1.1  itojun }
   3292   1.1  itojun 
   3293   1.1  itojun void
   3294   1.7   itohy trace(int level, const char *fmt, ...)
   3295   1.1  itojun {
   3296   1.1  itojun 	va_list ap;
   3297   1.1  itojun 
   3298  1.28  itojun 	if (level <= dflag) {
   3299  1.28  itojun 		va_start(ap, fmt);
   3300   1.1  itojun 		vfprintf(stderr, fmt, ap);
   3301  1.28  itojun 		va_end(ap);
   3302  1.28  itojun 	}
   3303   1.1  itojun 	if (dflag) {
   3304  1.28  itojun 		va_start(ap, fmt);
   3305   1.1  itojun 		if (level > 0)
   3306   1.1  itojun 			vsyslog(LOG_DEBUG, fmt, ap);
   3307   1.1  itojun 		else
   3308   1.1  itojun 			vsyslog(LOG_WARNING, fmt, ap);
   3309  1.28  itojun 		va_end(ap);
   3310   1.1  itojun 	}
   3311   1.1  itojun }
   3312   1.1  itojun 
   3313   1.7   itohy unsigned int
   3314  1.36     wiz if_maxindex(void)
   3315   1.1  itojun {
   3316   1.1  itojun 	struct if_nameindex *p, *p0;
   3317   1.1  itojun 	unsigned int max = 0;
   3318   1.1  itojun 
   3319   1.1  itojun 	p0 = if_nameindex();
   3320   1.1  itojun 	for (p = p0; p && p->if_index && p->if_name; p++) {
   3321   1.1  itojun 		if (max < p->if_index)
   3322   1.1  itojun 			max = p->if_index;
   3323   1.1  itojun 	}
   3324   1.1  itojun 	if_freenameindex(p0);
   3325   1.1  itojun 	return max;
   3326   1.1  itojun }
   3327   1.1  itojun 
   3328   1.7   itohy struct ifc *
   3329  1.36     wiz ifc_find(char *name)
   3330   1.1  itojun {
   3331   1.1  itojun 	struct ifc *ifcp;
   3332   1.1  itojun 
   3333   1.1  itojun 	for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
   3334   1.1  itojun 		if (strcmp(name, ifcp->ifc_name) == 0)
   3335   1.1  itojun 			return ifcp;
   3336   1.1  itojun 	}
   3337   1.1  itojun 	return (struct ifc *)NULL;
   3338   1.1  itojun }
   3339   1.1  itojun 
   3340   1.7   itohy struct iff *
   3341  1.36     wiz iff_find(struct ifc *ifcp, int type)
   3342   1.1  itojun {
   3343   1.1  itojun 	struct iff *iffp;
   3344   1.7   itohy 
   3345   1.1  itojun 	for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
   3346   1.1  itojun 		if (iffp->iff_type == type)
   3347   1.1  itojun 			return iffp;
   3348   1.1  itojun 	}
   3349   1.1  itojun 	return NULL;
   3350   1.1  itojun }
   3351   1.1  itojun 
   3352   1.7   itohy void
   3353  1.36     wiz setindex2ifc(int idx, struct ifc *ifcp)
   3354   1.1  itojun {
   3355   1.1  itojun 	int n;
   3356  1.14  itojun 	struct ifc **p;
   3357   1.1  itojun 
   3358   1.1  itojun 	if (!index2ifc) {
   3359   1.1  itojun 		nindex2ifc = 5;	/*initial guess*/
   3360   1.1  itojun 		index2ifc = (struct ifc **)
   3361   1.1  itojun 			malloc(sizeof(*index2ifc) * nindex2ifc);
   3362  1.21  itojun 		if (index2ifc == NULL) {
   3363   1.1  itojun 			fatal("malloc");
   3364  1.21  itojun 			/*NOTREACHED*/
   3365  1.21  itojun 		}
   3366   1.1  itojun 		memset(index2ifc, 0, sizeof(*index2ifc) * nindex2ifc);
   3367   1.1  itojun 	}
   3368   1.1  itojun 	n = nindex2ifc;
   3369  1.24  itojun 	while (nindex2ifc <= idx)
   3370   1.1  itojun 		nindex2ifc *= 2;
   3371   1.1  itojun 	if (n != nindex2ifc) {
   3372  1.14  itojun 		p = (struct ifc **)realloc(index2ifc,
   3373  1.14  itojun 		    sizeof(*index2ifc) * nindex2ifc);
   3374  1.21  itojun 		if (p == NULL) {
   3375   1.1  itojun 			fatal("realloc");
   3376  1.21  itojun 			/*NOTREACHED*/
   3377  1.21  itojun 		}
   3378  1.23      pk 		memset(p + n, 0, sizeof(*index2ifc) * (nindex2ifc - n));
   3379  1.14  itojun 		index2ifc = p;
   3380   1.1  itojun 	}
   3381  1.24  itojun 	index2ifc[idx] = ifcp;
   3382   1.1  itojun }
   3383