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