2000-07-05 02:14:16 +00:00
|
|
|
/* $FreeBSD$ */
|
2003-11-14 17:16:50 +00:00
|
|
|
/* $KAME: route6d.c,v 1.104 2003/10/31 00:30:20 itojun Exp $ */
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2017-11-20 19:49:47 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
1999-12-28 02:37:14 +00:00
|
|
|
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
|
|
|
|
* All rights reserved.
|
2017-01-09 00:33:27 +00:00
|
|
|
*
|
1999-12-28 02:37:14 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the project nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
2017-01-09 00:33:27 +00:00
|
|
|
*
|
1999-12-28 02:37:14 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
2012-11-18 15:37:27 +00:00
|
|
|
static const char _rcsid[] = "$KAME: route6d.c,v 1.104 2003/10/31 00:30:20 itojun Exp $";
|
1999-12-28 02:37:14 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/ioctl.h>
|
2018-05-11 00:01:43 +00:00
|
|
|
#include <sys/queue.h>
|
2017-01-09 00:37:09 +00:00
|
|
|
#include <sys/socket.h>
|
1999-12-28 02:37:14 +00:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/uio.h>
|
2017-01-09 00:37:09 +00:00
|
|
|
#include <arpa/inet.h>
|
1999-12-28 02:37:14 +00:00
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_var.h>
|
|
|
|
#include <netinet/ip6.h>
|
|
|
|
#include <netinet/udp.h>
|
2017-01-09 00:37:09 +00:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fnmatch.h>
|
2000-07-05 02:14:16 +00:00
|
|
|
#include <ifaddrs.h>
|
2017-01-09 00:37:09 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
#include <poll.h>
|
|
|
|
#endif
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
#include "route6d.h"
|
|
|
|
|
|
|
|
#define MAXFILTER 40
|
2012-11-18 15:48:02 +00:00
|
|
|
#define RT_DUMP_MAXRETRY 15
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
#define INIT_INTERVAL6 6
|
|
|
|
#else
|
2003-08-17 17:40:47 +00:00
|
|
|
#define INIT_INTERVAL6 10 /* Wait to submit an initial riprequest */
|
1999-12-28 02:37:14 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* alignment constraint for routing socket */
|
2000-07-05 02:14:16 +00:00
|
|
|
#define ROUNDUP(a) \
|
1999-12-28 02:37:14 +00:00
|
|
|
((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
|
2000-07-05 02:14:16 +00:00
|
|
|
#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2012-11-18 15:37:27 +00:00
|
|
|
struct ifc { /* Configuration of an interface */
|
|
|
|
TAILQ_ENTRY(ifc) ifc_next;
|
|
|
|
|
|
|
|
char ifc_name[IFNAMSIZ]; /* if name */
|
1999-12-28 02:37:14 +00:00
|
|
|
int ifc_index; /* if index */
|
|
|
|
int ifc_mtu; /* if mtu */
|
|
|
|
int ifc_metric; /* if metric */
|
2001-06-11 12:39:29 +00:00
|
|
|
u_int ifc_flags; /* flags */
|
|
|
|
short ifc_cflags; /* IFC_XXX */
|
1999-12-28 02:37:14 +00:00
|
|
|
struct in6_addr ifc_mylladdr; /* my link-local address */
|
|
|
|
struct sockaddr_in6 ifc_ripsin; /* rip multicast address */
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_HEAD(, ifac) ifc_ifac_head; /* list of AF_INET6 addrs */
|
|
|
|
TAILQ_HEAD(, iff) ifc_iff_head; /* list of filters */
|
1999-12-28 02:37:14 +00:00
|
|
|
int ifc_joined; /* joined to ff02::9 */
|
|
|
|
};
|
2017-01-13 06:22:49 +00:00
|
|
|
static TAILQ_HEAD(, ifc) ifc_head = TAILQ_HEAD_INITIALIZER(ifc_head);
|
2012-11-18 15:37:27 +00:00
|
|
|
|
2017-01-09 00:33:27 +00:00
|
|
|
struct ifac { /* Adddress associated to an interface */
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_ENTRY(ifac) ifac_next;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2012-11-18 15:37:27 +00:00
|
|
|
struct ifc *ifac_ifc; /* back pointer */
|
|
|
|
struct in6_addr ifac_addr; /* address */
|
|
|
|
struct in6_addr ifac_raddr; /* remote address, valid in p2p */
|
|
|
|
int ifac_scope_id; /* scope id */
|
|
|
|
int ifac_plen; /* prefix length */
|
1999-12-28 02:37:14 +00:00
|
|
|
};
|
|
|
|
|
2012-11-18 15:37:27 +00:00
|
|
|
struct iff { /* Filters for an interface */
|
|
|
|
TAILQ_ENTRY(iff) iff_next;
|
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
int iff_type;
|
|
|
|
struct in6_addr iff_addr;
|
|
|
|
int iff_plen;
|
|
|
|
};
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static struct ifc **index2ifc;
|
|
|
|
static unsigned int nindex2ifc;
|
|
|
|
static struct ifc *loopifcp = NULL; /* pointing to loopback */
|
2003-08-18 15:35:33 +00:00
|
|
|
#ifdef HAVE_POLL_H
|
2017-01-13 06:22:49 +00:00
|
|
|
static struct pollfd set[2];
|
2003-08-18 15:35:33 +00:00
|
|
|
#else
|
2017-01-13 06:22:49 +00:00
|
|
|
static fd_set *sockvecp; /* vector to select() for receiving */
|
|
|
|
static fd_set *recvecp;
|
|
|
|
static int fdmasks;
|
|
|
|
static int maxfd; /* maximum fd for select() */
|
2003-08-18 15:35:33 +00:00
|
|
|
#endif
|
2017-01-13 06:22:49 +00:00
|
|
|
static int rtsock; /* the routing socket */
|
|
|
|
static int ripsock; /* socket to send/receive RIP datagram */
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static struct rip6 *ripbuf; /* packet buffer for sending */
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
/*
|
2001-06-11 12:39:29 +00:00
|
|
|
* Maintain the routes in a linked list. When the number of the routes
|
1999-12-28 02:37:14 +00:00
|
|
|
* grows, somebody would like to introduce a hash based or a radix tree
|
2001-06-11 12:39:29 +00:00
|
|
|
* based structure. I believe the number of routes handled by RIP is
|
1999-12-28 02:37:14 +00:00
|
|
|
* limited and I don't have to manage a complex data structure, however.
|
|
|
|
*
|
|
|
|
* One of the major drawbacks of the linear linked list is the difficulty
|
2001-06-11 12:39:29 +00:00
|
|
|
* of representing the relationship between a couple of routes. This may
|
1999-12-28 02:37:14 +00:00
|
|
|
* be a significant problem when we have to support route aggregation with
|
2011-12-30 10:58:14 +00:00
|
|
|
* suppressing the specifics covered by the aggregate.
|
1999-12-28 02:37:14 +00:00
|
|
|
*/
|
|
|
|
|
2012-11-18 15:37:27 +00:00
|
|
|
struct riprt {
|
|
|
|
TAILQ_ENTRY(riprt) rrt_next; /* next destination */
|
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
struct riprt *rrt_same; /* same destination - future use */
|
|
|
|
struct netinfo6 rrt_info; /* network info */
|
|
|
|
struct in6_addr rrt_gw; /* gateway */
|
2000-07-05 02:14:16 +00:00
|
|
|
u_long rrt_flags; /* kernel routing table flags */
|
|
|
|
u_long rrt_rflags; /* route6d routing table flags */
|
1999-12-28 02:37:14 +00:00
|
|
|
time_t rrt_t; /* when the route validated */
|
|
|
|
int rrt_index; /* ifindex from which this route got */
|
|
|
|
};
|
2017-01-13 06:22:49 +00:00
|
|
|
static TAILQ_HEAD(, riprt) riprt_head = TAILQ_HEAD_INITIALIZER(riprt_head);
|
|
|
|
|
|
|
|
static int dflag = 0; /* debug flag */
|
|
|
|
static int qflag = 0; /* quiet flag */
|
|
|
|
static int nflag = 0; /* don't update kernel routing table */
|
|
|
|
static int aflag = 0; /* age out even the statically defined routes */
|
|
|
|
static int hflag = 0; /* don't split horizon */
|
|
|
|
static int lflag = 0; /* exchange site local routes */
|
|
|
|
static int Pflag = 0; /* don't age out routes with RTF_PROTO[123] */
|
|
|
|
static int Qflag = RTF_PROTO2; /* set RTF_PROTO[123] flag to routes by RIPng */
|
|
|
|
static int sflag = 0; /* announce static routes w/ split horizon */
|
|
|
|
static int Sflag = 0; /* announce static routes to every interface */
|
|
|
|
static unsigned long routetag = 0; /* route tag attached on originating case */
|
|
|
|
|
|
|
|
static char *filter[MAXFILTER];
|
|
|
|
static int filtertype[MAXFILTER];
|
|
|
|
static int nfilter = 0;
|
|
|
|
|
|
|
|
static pid_t pid;
|
|
|
|
|
|
|
|
static struct sockaddr_storage ripsin;
|
|
|
|
|
|
|
|
static int interval = 1;
|
|
|
|
static time_t nextalarm = 0;
|
|
|
|
#if 0
|
|
|
|
static time_t sup_trig_update = 0;
|
|
|
|
#endif
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static FILE *rtlog = NULL;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static int logopened = 0;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2003-08-18 16:20:30 +00:00
|
|
|
static int seq = 0;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static volatile sig_atomic_t seenalrm;
|
|
|
|
static volatile sig_atomic_t seenquit;
|
|
|
|
static volatile sig_atomic_t seenusr1;
|
2001-06-11 12:39:29 +00:00
|
|
|
|
2000-07-05 02:14:16 +00:00
|
|
|
#define RRTF_AGGREGATE 0x08000000
|
|
|
|
#define RRTF_NOADVERTISE 0x10000000
|
|
|
|
#define RRTF_NH_NOT_LLADDR 0x20000000
|
|
|
|
#define RRTF_SENDANYWAY 0x40000000
|
|
|
|
#define RRTF_CHANGED 0x80000000
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static void sighandler(int);
|
|
|
|
static void ripalarm(void);
|
|
|
|
static void riprecv(void);
|
|
|
|
static void ripsend(struct ifc *, struct sockaddr_in6 *, int);
|
|
|
|
static int out_filter(struct riprt *, struct ifc *);
|
|
|
|
static void init(void);
|
|
|
|
static void ifconfig(void);
|
|
|
|
static int ifconfig1(const char *, const struct sockaddr *, struct ifc *, int);
|
|
|
|
static void rtrecv(void);
|
|
|
|
static int rt_del(const struct sockaddr_in6 *, const struct sockaddr_in6 *,
|
2007-11-07 10:53:41 +00:00
|
|
|
const struct sockaddr_in6 *);
|
2017-01-13 06:22:49 +00:00
|
|
|
static int rt_deladdr(struct ifc *, const struct sockaddr_in6 *,
|
2007-11-07 10:53:41 +00:00
|
|
|
const struct sockaddr_in6 *);
|
2017-01-13 06:22:49 +00:00
|
|
|
static void filterconfig(void);
|
|
|
|
static int getifmtu(int);
|
|
|
|
static const char *rttypes(struct rt_msghdr *);
|
|
|
|
static const char *rtflags(struct rt_msghdr *);
|
|
|
|
static const char *ifflags(int);
|
|
|
|
static int ifrt(struct ifc *, int);
|
|
|
|
static void ifrt_p2p(struct ifc *, int);
|
|
|
|
static void applyplen(struct in6_addr *, int);
|
|
|
|
static void ifrtdump(int);
|
|
|
|
static void ifdump(int);
|
|
|
|
static void ifdump0(FILE *, const struct ifc *);
|
|
|
|
static void ifremove(int);
|
|
|
|
static void rtdump(int);
|
|
|
|
static void rt_entry(struct rt_msghdr *, int);
|
|
|
|
static void rtdexit(void);
|
|
|
|
static void riprequest(struct ifc *, struct netinfo6 *, int,
|
2007-11-07 10:53:41 +00:00
|
|
|
struct sockaddr_in6 *);
|
2017-01-13 06:22:49 +00:00
|
|
|
static void ripflush(struct ifc *, struct sockaddr_in6 *, int, struct netinfo6 *np);
|
|
|
|
static void sendrequest(struct ifc *);
|
|
|
|
static int sin6mask2len(const struct sockaddr_in6 *);
|
|
|
|
static int mask2len(const struct in6_addr *, int);
|
|
|
|
static int sendpacket(struct sockaddr_in6 *, int);
|
|
|
|
static int addroute(struct riprt *, const struct in6_addr *, struct ifc *);
|
|
|
|
static int delroute(struct netinfo6 *, struct in6_addr *);
|
|
|
|
#if 0
|
|
|
|
static struct in6_addr *getroute(struct netinfo6 *, struct in6_addr *);
|
|
|
|
#endif
|
|
|
|
static void krtread(int);
|
|
|
|
static int tobeadv(struct riprt *, struct ifc *);
|
|
|
|
static char *allocopy(char *);
|
|
|
|
static char *hms(void);
|
|
|
|
static const char *inet6_n2p(const struct in6_addr *);
|
|
|
|
static struct ifac *ifa_match(const struct ifc *, const struct in6_addr *, int);
|
|
|
|
static struct in6_addr *plen2mask(int);
|
|
|
|
static struct riprt *rtsearch(struct netinfo6 *);
|
|
|
|
static int ripinterval(int);
|
|
|
|
#if 0
|
|
|
|
static time_t ripsuptrig(void);
|
|
|
|
#endif
|
|
|
|
static void fatal(const char *, ...)
|
2000-10-08 07:43:38 +00:00
|
|
|
__attribute__((__format__(__printf__, 1, 2)));
|
2017-01-13 06:22:49 +00:00
|
|
|
static void trace(int, const char *, ...)
|
2000-10-08 07:43:38 +00:00
|
|
|
__attribute__((__format__(__printf__, 2, 3)));
|
2017-01-13 06:22:49 +00:00
|
|
|
static void tracet(int, const char *, ...)
|
2000-10-08 07:43:38 +00:00
|
|
|
__attribute__((__format__(__printf__, 2, 3)));
|
2017-01-13 06:22:49 +00:00
|
|
|
static struct ifc *ifc_find(char *);
|
|
|
|
static struct iff *iff_find(struct ifc *, int);
|
|
|
|
static void setindex2ifc(int, struct ifc *);
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
#define MALLOC(type) ((type *)malloc(sizeof(type)))
|
|
|
|
|
2012-11-18 15:37:27 +00:00
|
|
|
#define IFIL_TYPE_ANY 0x0
|
|
|
|
#define IFIL_TYPE_A 'A'
|
|
|
|
#define IFIL_TYPE_N 'N'
|
|
|
|
#define IFIL_TYPE_T 'T'
|
|
|
|
#define IFIL_TYPE_O 'O'
|
|
|
|
#define IFIL_TYPE_L 'L'
|
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
int
|
2012-11-18 15:37:27 +00:00
|
|
|
main(int argc, char *argv[])
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
int error = 0;
|
2012-11-18 15:48:02 +00:00
|
|
|
unsigned long proto;
|
1999-12-28 02:37:14 +00:00
|
|
|
struct ifc *ifcp;
|
|
|
|
sigset_t mask, omask;
|
2012-11-18 15:48:02 +00:00
|
|
|
const char *pidfile = ROUTE6D_PID;
|
|
|
|
FILE *pidfh;
|
1999-12-28 02:37:14 +00:00
|
|
|
char *progname;
|
2000-07-05 02:14:16 +00:00
|
|
|
char *ep;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
progname = strrchr(*argv, '/');
|
|
|
|
if (progname)
|
|
|
|
progname++;
|
|
|
|
else
|
|
|
|
progname = *argv;
|
|
|
|
|
|
|
|
pid = getpid();
|
2012-11-18 15:48:02 +00:00
|
|
|
while ((ch = getopt(argc, argv, "A:N:O:R:T:L:t:adDhlnp:P:Q:qsS")) != -1) {
|
1999-12-28 02:37:14 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'A':
|
|
|
|
case 'N':
|
|
|
|
case 'O':
|
|
|
|
case 'T':
|
|
|
|
case 'L':
|
2000-07-05 02:14:16 +00:00
|
|
|
if (nfilter >= MAXFILTER) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("Exceeds MAXFILTER");
|
2000-07-05 02:14:16 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
filtertype[nfilter] = ch;
|
|
|
|
filter[nfilter++] = allocopy(optarg);
|
|
|
|
break;
|
|
|
|
case 't':
|
2000-07-05 02:14:16 +00:00
|
|
|
ep = NULL;
|
|
|
|
routetag = strtoul(optarg, &ep, 0);
|
|
|
|
if (!ep || *ep != '\0' || (routetag & ~0xffff) != 0) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("invalid route tag");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
break;
|
2012-11-18 15:48:02 +00:00
|
|
|
case 'p':
|
|
|
|
pidfile = optarg;
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
ep = NULL;
|
|
|
|
proto = strtoul(optarg, &ep, 0);
|
|
|
|
if (!ep || *ep != '\0' || 3 < proto) {
|
|
|
|
fatal("invalid P flag");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
if (proto == 0)
|
|
|
|
Pflag = 0;
|
|
|
|
if (proto == 1)
|
|
|
|
Pflag |= RTF_PROTO1;
|
|
|
|
if (proto == 2)
|
|
|
|
Pflag |= RTF_PROTO2;
|
|
|
|
if (proto == 3)
|
|
|
|
Pflag |= RTF_PROTO3;
|
|
|
|
break;
|
|
|
|
case 'Q':
|
|
|
|
ep = NULL;
|
|
|
|
proto = strtoul(optarg, &ep, 0);
|
|
|
|
if (!ep || *ep != '\0' || 3 < proto) {
|
|
|
|
fatal("invalid Q flag");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
if (proto == 0)
|
|
|
|
Qflag = 0;
|
|
|
|
if (proto == 1)
|
|
|
|
Qflag |= RTF_PROTO1;
|
|
|
|
if (proto == 2)
|
|
|
|
Qflag |= RTF_PROTO2;
|
|
|
|
if (proto == 3)
|
|
|
|
Qflag |= RTF_PROTO3;
|
|
|
|
break;
|
1999-12-28 02:37:14 +00:00
|
|
|
case 'R':
|
2000-07-05 02:14:16 +00:00
|
|
|
if ((rtlog = fopen(optarg, "w")) == NULL) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("Can not write to routelog");
|
2000-07-05 02:14:16 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
break;
|
2000-07-05 02:14:16 +00:00
|
|
|
#define FLAG(c, flag, n) case c: do { flag = n; break; } while(0)
|
|
|
|
FLAG('a', aflag, 1); break;
|
|
|
|
FLAG('d', dflag, 1); break;
|
|
|
|
FLAG('D', dflag, 2); break;
|
|
|
|
FLAG('h', hflag, 1); break;
|
|
|
|
FLAG('l', lflag, 1); break;
|
|
|
|
FLAG('n', nflag, 1); break;
|
|
|
|
FLAG('q', qflag, 1); break;
|
|
|
|
FLAG('s', sflag, 1); break;
|
|
|
|
FLAG('S', Sflag, 1); break;
|
1999-12-28 02:37:14 +00:00
|
|
|
#undef FLAG
|
|
|
|
default:
|
|
|
|
fatal("Invalid option specified, terminating");
|
2000-07-05 02:14:16 +00:00
|
|
|
/*NOTREACHED*/
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2001-06-11 12:39:29 +00:00
|
|
|
if (argc > 0) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("bogus extra arguments");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
if (geteuid()) {
|
|
|
|
nflag = 1;
|
|
|
|
fprintf(stderr, "No kernel update is allowed\n");
|
|
|
|
}
|
2003-08-17 17:29:54 +00:00
|
|
|
|
|
|
|
if (dflag == 0) {
|
|
|
|
if (daemon(0, 0) < 0) {
|
|
|
|
fatal("daemon");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
openlog(progname, LOG_NDELAY|LOG_PID, LOG_DAEMON);
|
|
|
|
logopened++;
|
2001-06-11 12:39:29 +00:00
|
|
|
|
|
|
|
if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL)
|
|
|
|
fatal("malloc");
|
|
|
|
memset(ripbuf, 0, RIP6_MAXMTU);
|
|
|
|
ripbuf->rip6_cmd = RIP6_RESPONSE;
|
|
|
|
ripbuf->rip6_vers = RIP6_VERSION;
|
|
|
|
ripbuf->rip6_res1[0] = 0;
|
|
|
|
ripbuf->rip6_res1[1] = 0;
|
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
init();
|
|
|
|
ifconfig();
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
|
1999-12-28 02:37:14 +00:00
|
|
|
if (ifcp->ifc_index < 0) {
|
2012-11-18 15:37:27 +00:00
|
|
|
fprintf(stderr, "No ifindex found at %s "
|
|
|
|
"(no link-local address?)\n", ifcp->ifc_name);
|
1999-12-28 02:37:14 +00:00
|
|
|
error++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (error)
|
|
|
|
exit(1);
|
2001-06-11 12:39:29 +00:00
|
|
|
if (loopifcp == NULL) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("No loopback found");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
|
1999-12-28 02:37:14 +00:00
|
|
|
ifrt(ifcp, 0);
|
2012-11-18 15:37:27 +00:00
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
filterconfig();
|
|
|
|
krtread(0);
|
|
|
|
if (dflag)
|
|
|
|
ifrtdump(0);
|
|
|
|
|
|
|
|
pid = getpid();
|
2012-11-18 15:48:02 +00:00
|
|
|
if ((pidfh = fopen(pidfile, "w")) != NULL) {
|
|
|
|
fprintf(pidfh, "%d\n", pid);
|
|
|
|
fclose(pidfh);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
2001-06-11 12:39:29 +00:00
|
|
|
if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("malloc");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2000-07-05 02:14:16 +00:00
|
|
|
memset(ripbuf, 0, RIP6_MAXMTU);
|
1999-12-28 02:37:14 +00:00
|
|
|
ripbuf->rip6_cmd = RIP6_RESPONSE;
|
|
|
|
ripbuf->rip6_vers = RIP6_VERSION;
|
|
|
|
ripbuf->rip6_res1[0] = 0;
|
|
|
|
ripbuf->rip6_res1[1] = 0;
|
|
|
|
|
2001-06-11 12:39:29 +00:00
|
|
|
if (signal(SIGALRM, sighandler) == SIG_ERR ||
|
|
|
|
signal(SIGQUIT, sighandler) == SIG_ERR ||
|
|
|
|
signal(SIGTERM, sighandler) == SIG_ERR ||
|
|
|
|
signal(SIGUSR1, sighandler) == SIG_ERR ||
|
|
|
|
signal(SIGHUP, sighandler) == SIG_ERR ||
|
|
|
|
signal(SIGINT, sighandler) == SIG_ERR) {
|
|
|
|
fatal("signal");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
/*
|
|
|
|
* To avoid rip packet congestion (not on a cable but in this
|
|
|
|
* process), wait for a moment to send the first RIP6_RESPONSE
|
|
|
|
* packets.
|
|
|
|
*/
|
|
|
|
alarm(ripinterval(INIT_INTERVAL6));
|
|
|
|
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
|
|
|
|
if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
|
2003-08-17 17:51:30 +00:00
|
|
|
continue;
|
1999-12-28 02:37:14 +00:00
|
|
|
if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
|
|
|
|
sendrequest(ifcp);
|
|
|
|
}
|
|
|
|
|
|
|
|
syslog(LOG_INFO, "**** Started ****");
|
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGALRM);
|
|
|
|
while (1) {
|
2001-06-11 12:39:29 +00:00
|
|
|
if (seenalrm) {
|
|
|
|
ripalarm();
|
|
|
|
seenalrm = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (seenquit) {
|
|
|
|
rtdexit();
|
|
|
|
seenquit = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (seenusr1) {
|
|
|
|
ifrtdump(SIGUSR1);
|
|
|
|
seenusr1 = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2003-08-18 15:35:33 +00:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
switch (poll(set, 2, INFTIM))
|
|
|
|
#else
|
2003-08-18 15:24:32 +00:00
|
|
|
memcpy(recvecp, sockvecp, fdmasks);
|
2003-08-18 15:35:33 +00:00
|
|
|
switch (select(maxfd + 1, recvecp, 0, 0, 0))
|
|
|
|
#endif
|
|
|
|
{
|
1999-12-28 02:37:14 +00:00
|
|
|
case -1:
|
2001-06-11 12:39:29 +00:00
|
|
|
if (errno != EINTR) {
|
|
|
|
fatal("select");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
continue;
|
1999-12-28 02:37:14 +00:00
|
|
|
case 0:
|
|
|
|
continue;
|
|
|
|
default:
|
2003-08-18 15:35:33 +00:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
if (set[0].revents & POLLIN)
|
|
|
|
#else
|
|
|
|
if (FD_ISSET(ripsock, recvecp))
|
|
|
|
#endif
|
|
|
|
{
|
1999-12-28 02:37:14 +00:00
|
|
|
sigprocmask(SIG_BLOCK, &mask, &omask);
|
|
|
|
riprecv();
|
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
|
|
|
}
|
2003-08-18 15:35:33 +00:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
if (set[1].revents & POLLIN)
|
|
|
|
#else
|
|
|
|
if (FD_ISSET(rtsock, recvecp))
|
|
|
|
#endif
|
|
|
|
{
|
1999-12-28 02:37:14 +00:00
|
|
|
sigprocmask(SIG_BLOCK, &mask, &omask);
|
|
|
|
rtrecv();
|
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static void
|
2012-11-18 15:37:27 +00:00
|
|
|
sighandler(int signo)
|
2001-06-11 12:39:29 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
switch (signo) {
|
|
|
|
case SIGALRM:
|
|
|
|
seenalrm++;
|
|
|
|
break;
|
|
|
|
case SIGQUIT:
|
|
|
|
case SIGTERM:
|
|
|
|
seenquit++;
|
|
|
|
break;
|
|
|
|
case SIGUSR1:
|
|
|
|
case SIGHUP:
|
|
|
|
case SIGINT:
|
|
|
|
seenusr1++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
/*
|
|
|
|
* gracefully exits after resetting sockopts.
|
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
2017-01-13 06:22:49 +00:00
|
|
|
static void
|
2012-11-18 15:37:27 +00:00
|
|
|
rtdexit(void)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
struct riprt *rrt;
|
|
|
|
|
|
|
|
alarm(0);
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
|
2000-07-05 02:14:16 +00:00
|
|
|
if (rrt->rrt_rflags & RRTF_AGGREGATE) {
|
1999-12-28 02:37:14 +00:00
|
|
|
delroute(&rrt->rrt_info, &rrt->rrt_gw);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(ripsock);
|
|
|
|
close(rtsock);
|
|
|
|
syslog(LOG_INFO, "**** Terminated ****");
|
|
|
|
closelog();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called periodically:
|
|
|
|
* 1. age out the learned route. remove it if necessary.
|
|
|
|
* 2. submit RIP6_RESPONSE packets.
|
2001-06-11 12:39:29 +00:00
|
|
|
* Invoked in every SUPPLY_INTERVAL6 (30) seconds. I believe we don't have
|
1999-12-28 02:37:14 +00:00
|
|
|
* to invoke this function in every 1 or 5 or 10 seconds only to age the
|
|
|
|
* routes more precisely.
|
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
2017-01-13 06:22:49 +00:00
|
|
|
static void
|
2012-11-18 15:37:27 +00:00
|
|
|
ripalarm(void)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
struct ifc *ifcp;
|
2012-11-18 15:37:27 +00:00
|
|
|
struct riprt *rrt, *rrt_tmp;
|
1999-12-28 02:37:14 +00:00
|
|
|
time_t t_lifetime, t_holddown;
|
|
|
|
|
|
|
|
/* age the RIP routes */
|
|
|
|
t_lifetime = time(NULL) - RIP_LIFETIME;
|
|
|
|
t_holddown = t_lifetime - RIP_HOLDDOWN;
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_FOREACH_SAFE(rrt, &riprt_head, rrt_next, rrt_tmp) {
|
|
|
|
if (rrt->rrt_t == 0)
|
1999-12-28 02:37:14 +00:00
|
|
|
continue;
|
2012-11-18 15:37:27 +00:00
|
|
|
else if (rrt->rrt_t < t_holddown) {
|
|
|
|
TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
|
1999-12-28 02:37:14 +00:00
|
|
|
delroute(&rrt->rrt_info, &rrt->rrt_gw);
|
|
|
|
free(rrt);
|
2012-11-18 15:37:27 +00:00
|
|
|
} else if (rrt->rrt_t < t_lifetime)
|
1999-12-28 02:37:14 +00:00
|
|
|
rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
|
|
|
|
}
|
|
|
|
/* Supply updates */
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
|
1999-12-28 02:37:14 +00:00
|
|
|
if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
|
|
|
|
ripsend(ifcp, &ifcp->ifc_ripsin, 0);
|
|
|
|
}
|
|
|
|
alarm(ripinterval(SUPPLY_INTERVAL6));
|
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static void
|
2012-11-18 15:37:27 +00:00
|
|
|
init(void)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
2003-08-17 16:08:56 +00:00
|
|
|
int error;
|
|
|
|
const int int0 = 0, int1 = 1, int255 = 255;
|
1999-12-28 02:37:14 +00:00
|
|
|
struct addrinfo hints, *res;
|
2003-08-18 15:58:23 +00:00
|
|
|
char port[NI_MAXSERV];
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_INIT(&ifc_head);
|
1999-12-28 02:37:14 +00:00
|
|
|
nindex2ifc = 0; /*initial guess*/
|
|
|
|
index2ifc = NULL;
|
2003-08-18 15:58:23 +00:00
|
|
|
snprintf(port, sizeof(port), "%u", RIP6_PORT);
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = PF_INET6;
|
|
|
|
hints.ai_socktype = SOCK_DGRAM;
|
2003-08-18 15:54:08 +00:00
|
|
|
hints.ai_protocol = IPPROTO_UDP;
|
1999-12-28 02:37:14 +00:00
|
|
|
hints.ai_flags = AI_PASSIVE;
|
|
|
|
error = getaddrinfo(NULL, port, &hints, &res);
|
2001-06-11 12:39:29 +00:00
|
|
|
if (error) {
|
2000-10-08 07:43:38 +00:00
|
|
|
fatal("%s", gai_strerror(error));
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
if (res->ai_next) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal(":: resolved to multiple address");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
ripsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
2001-06-11 12:39:29 +00:00
|
|
|
if (ripsock < 0) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("rip socket");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2003-08-17 16:08:56 +00:00
|
|
|
#ifdef IPV6_V6ONLY
|
|
|
|
if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_V6ONLY,
|
|
|
|
&int1, sizeof(int1)) < 0) {
|
|
|
|
fatal("rip IPV6_V6ONLY");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
#endif
|
2001-06-11 12:39:29 +00:00
|
|
|
if (bind(ripsock, res->ai_addr, res->ai_addrlen) < 0) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("rip bind");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
|
2001-06-11 12:39:29 +00:00
|
|
|
&int255, sizeof(int255)) < 0) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("rip IPV6_MULTICAST_HOPS");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
|
2001-06-11 12:39:29 +00:00
|
|
|
&int0, sizeof(int0)) < 0) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("rip IPV6_MULTICAST_LOOP");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2000-07-10 18:27:55 +00:00
|
|
|
|
2000-07-05 02:14:16 +00:00
|
|
|
#ifdef IPV6_RECVPKTINFO
|
2003-08-17 16:08:56 +00:00
|
|
|
if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVPKTINFO,
|
|
|
|
&int1, sizeof(int1)) < 0) {
|
2000-07-05 02:14:16 +00:00
|
|
|
fatal("rip IPV6_RECVPKTINFO");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2000-07-05 02:14:16 +00:00
|
|
|
#else /* old adv. API */
|
2003-08-17 16:08:56 +00:00
|
|
|
if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_PKTINFO,
|
|
|
|
&int1, sizeof(int1)) < 0) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("rip IPV6_PKTINFO");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2017-01-09 00:33:27 +00:00
|
|
|
#endif
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2006-11-16 19:03:03 +00:00
|
|
|
#ifdef IPV6_RECVPKTINFO
|
|
|
|
if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
|
|
|
|
&int1, sizeof(int1)) < 0) {
|
|
|
|
fatal("rip IPV6_RECVHOPLIMIT");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
#else /* old adv. API */
|
|
|
|
if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_HOPLIMIT,
|
|
|
|
&int1, sizeof(int1)) < 0) {
|
|
|
|
fatal("rip IPV6_HOPLIMIT");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
#endif
|
2017-01-12 18:44:58 +00:00
|
|
|
freeaddrinfo(res);
|
2006-11-16 19:03:03 +00:00
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = PF_INET6;
|
|
|
|
hints.ai_socktype = SOCK_DGRAM;
|
2003-08-18 15:54:08 +00:00
|
|
|
hints.ai_protocol = IPPROTO_UDP;
|
1999-12-28 02:37:14 +00:00
|
|
|
error = getaddrinfo(RIP6_DEST, port, &hints, &res);
|
2001-06-11 12:39:29 +00:00
|
|
|
if (error) {
|
2000-10-08 07:43:38 +00:00
|
|
|
fatal("%s", gai_strerror(error));
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
if (res->ai_next) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("%s resolved to multiple address", RIP6_DEST);
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
memcpy(&ripsin, res->ai_addr, res->ai_addrlen);
|
2017-01-12 18:44:58 +00:00
|
|
|
freeaddrinfo(res);
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2003-08-18 15:35:33 +00:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
set[0].fd = ripsock;
|
|
|
|
set[0].events = POLLIN;
|
|
|
|
#else
|
2003-08-18 15:24:32 +00:00
|
|
|
maxfd = ripsock;
|
2003-08-18 15:35:33 +00:00
|
|
|
#endif
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
if (nflag == 0) {
|
2001-06-11 12:39:29 +00:00
|
|
|
if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("route socket");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2003-08-18 15:35:33 +00:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
set[1].fd = rtsock;
|
|
|
|
set[1].events = POLLIN;
|
|
|
|
#else
|
2003-08-18 15:24:32 +00:00
|
|
|
if (rtsock > maxfd)
|
|
|
|
maxfd = rtsock;
|
2003-08-18 15:35:33 +00:00
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
set[1].fd = -1;
|
|
|
|
#else
|
1999-12-28 02:37:14 +00:00
|
|
|
rtsock = -1; /*just for safety */
|
2003-08-18 15:35:33 +00:00
|
|
|
#endif
|
|
|
|
}
|
2003-08-18 15:24:32 +00:00
|
|
|
|
2003-08-18 15:35:33 +00:00
|
|
|
#ifndef HAVE_POLL_H
|
2003-08-18 15:24:32 +00:00
|
|
|
fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
|
|
|
|
if ((sockvecp = malloc(fdmasks)) == NULL) {
|
|
|
|
fatal("malloc");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
if ((recvecp = malloc(fdmasks)) == NULL) {
|
|
|
|
fatal("malloc");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
memset(sockvecp, 0, fdmasks);
|
|
|
|
FD_SET(ripsock, sockvecp);
|
|
|
|
if (rtsock >= 0)
|
|
|
|
FD_SET(rtsock, sockvecp);
|
2003-08-18 15:35:33 +00:00
|
|
|
#endif
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
2000-07-05 02:14:16 +00:00
|
|
|
#define RIPSIZE(n) \
|
|
|
|
(sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6))
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ripflush flushes the rip datagram stored in the rip buffer
|
|
|
|
*/
|
2017-01-13 06:22:49 +00:00
|
|
|
static void
|
2012-11-18 15:37:27 +00:00
|
|
|
ripflush(struct ifc *ifcp, struct sockaddr_in6 *sin6, int nrt, struct netinfo6 *np)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if (ifcp)
|
|
|
|
tracet(1, "Send(%s): info(%d) to %s.%d\n",
|
|
|
|
ifcp->ifc_name, nrt,
|
2003-08-17 16:05:49 +00:00
|
|
|
inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
|
1999-12-28 02:37:14 +00:00
|
|
|
else
|
|
|
|
tracet(1, "Send: info(%d) to %s.%d\n",
|
2003-08-17 16:05:49 +00:00
|
|
|
nrt, inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
|
1999-12-28 02:37:14 +00:00
|
|
|
if (dflag >= 2) {
|
|
|
|
np = ripbuf->rip6_nets;
|
|
|
|
for (i = 0; i < nrt; i++, np++) {
|
|
|
|
if (np->rip6_metric == NEXTHOP_METRIC) {
|
|
|
|
if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest))
|
2000-07-05 02:14:16 +00:00
|
|
|
trace(2, " NextHop reset");
|
1999-12-28 02:37:14 +00:00
|
|
|
else {
|
|
|
|
trace(2, " NextHop %s",
|
|
|
|
inet6_n2p(&np->rip6_dest));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
trace(2, " %s/%d[%d]",
|
|
|
|
inet6_n2p(&np->rip6_dest),
|
|
|
|
np->rip6_plen, np->rip6_metric);
|
|
|
|
}
|
|
|
|
if (np->rip6_tag) {
|
|
|
|
trace(2, " tag=0x%04x",
|
|
|
|
ntohs(np->rip6_tag) & 0xffff);
|
|
|
|
}
|
|
|
|
trace(2, "\n");
|
|
|
|
}
|
|
|
|
}
|
2003-08-17 16:05:49 +00:00
|
|
|
error = sendpacket(sin6, RIPSIZE(nrt));
|
1999-12-28 02:37:14 +00:00
|
|
|
if (error == EAFNOSUPPORT) {
|
|
|
|
/* Protocol not supported */
|
2017-01-12 18:44:58 +00:00
|
|
|
if (ifcp != NULL) {
|
|
|
|
tracet(1, "Could not send info to %s (%s): "
|
|
|
|
"set IFF_UP to 0\n",
|
|
|
|
ifcp->ifc_name,
|
|
|
|
inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
|
|
|
|
/* As if down for AF_INET6 */
|
|
|
|
ifcp->ifc_flags &= ~IFF_UP;
|
|
|
|
} else {
|
|
|
|
tracet(1, "Could not send info to %s\n",
|
|
|
|
inet6_n2p(&sin6->sin6_addr));
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate RIP6_RESPONSE packets and send them.
|
|
|
|
*/
|
2017-01-13 06:22:49 +00:00
|
|
|
static void
|
2012-11-18 15:37:27 +00:00
|
|
|
ripsend(struct ifc *ifcp, struct sockaddr_in6 *sin6, int flag)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
struct riprt *rrt;
|
|
|
|
struct in6_addr *nh; /* next hop */
|
2012-11-18 15:37:27 +00:00
|
|
|
struct netinfo6 *np;
|
2001-06-11 12:39:29 +00:00
|
|
|
int maxrte;
|
2012-11-18 15:37:27 +00:00
|
|
|
int nrt;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2003-08-18 16:15:30 +00:00
|
|
|
if (qflag)
|
|
|
|
return;
|
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
if (ifcp == NULL) {
|
|
|
|
/*
|
|
|
|
* Request from non-link local address is not
|
|
|
|
* a regular route6d update.
|
|
|
|
*/
|
2017-01-09 00:33:27 +00:00
|
|
|
maxrte = (IFMINMTU - sizeof(struct ip6_hdr) -
|
|
|
|
sizeof(struct udphdr) -
|
1999-12-28 02:37:14 +00:00
|
|
|
sizeof(struct rip6) + sizeof(struct netinfo6)) /
|
|
|
|
sizeof(struct netinfo6);
|
2012-11-18 15:37:27 +00:00
|
|
|
nh = NULL;
|
|
|
|
nrt = 0;
|
|
|
|
np = ripbuf->rip6_nets;
|
|
|
|
TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
|
2000-07-05 02:14:16 +00:00
|
|
|
if (rrt->rrt_rflags & RRTF_NOADVERTISE)
|
1999-12-28 02:37:14 +00:00
|
|
|
continue;
|
|
|
|
/* Put the route to the buffer */
|
|
|
|
*np = rrt->rrt_info;
|
|
|
|
np++; nrt++;
|
|
|
|
if (nrt == maxrte) {
|
2012-11-18 15:37:27 +00:00
|
|
|
ripflush(NULL, sin6, nrt, np);
|
1999-12-28 02:37:14 +00:00
|
|
|
nh = NULL;
|
2012-11-18 15:37:27 +00:00
|
|
|
nrt = 0;
|
|
|
|
np = ripbuf->rip6_nets;
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nrt) /* Send last packet */
|
2012-11-18 15:37:27 +00:00
|
|
|
ripflush(NULL, sin6, nrt, np);
|
1999-12-28 02:37:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-07-05 02:14:16 +00:00
|
|
|
if ((flag & RRTF_SENDANYWAY) == 0 &&
|
1999-12-28 02:37:14 +00:00
|
|
|
(qflag || (ifcp->ifc_flags & IFF_LOOPBACK)))
|
|
|
|
return;
|
2001-06-11 12:39:29 +00:00
|
|
|
|
|
|
|
/* -N: no use */
|
2012-11-18 15:37:27 +00:00
|
|
|
if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
|
1999-12-28 02:37:14 +00:00
|
|
|
return;
|
2001-06-11 12:39:29 +00:00
|
|
|
|
|
|
|
/* -T: generate default route only */
|
2012-11-18 15:37:27 +00:00
|
|
|
if (iff_find(ifcp, IFIL_TYPE_T) != NULL) {
|
1999-12-28 02:37:14 +00:00
|
|
|
struct netinfo6 rrt_info;
|
|
|
|
memset(&rrt_info, 0, sizeof(struct netinfo6));
|
|
|
|
rrt_info.rip6_dest = in6addr_any;
|
|
|
|
rrt_info.rip6_plen = 0;
|
|
|
|
rrt_info.rip6_metric = 1;
|
2001-06-11 12:39:29 +00:00
|
|
|
rrt_info.rip6_metric += ifcp->ifc_metric;
|
1999-12-28 02:37:14 +00:00
|
|
|
rrt_info.rip6_tag = htons(routetag & 0xffff);
|
|
|
|
np = ripbuf->rip6_nets;
|
|
|
|
*np = rrt_info;
|
|
|
|
nrt = 1;
|
2012-11-18 15:37:27 +00:00
|
|
|
ripflush(ifcp, sin6, nrt, np);
|
1999-12-28 02:37:14 +00:00
|
|
|
return;
|
|
|
|
}
|
2001-06-11 12:39:29 +00:00
|
|
|
|
2017-01-09 00:33:27 +00:00
|
|
|
maxrte = (ifcp->ifc_mtu - sizeof(struct ip6_hdr) -
|
|
|
|
sizeof(struct udphdr) -
|
1999-12-28 02:37:14 +00:00
|
|
|
sizeof(struct rip6) + sizeof(struct netinfo6)) /
|
|
|
|
sizeof(struct netinfo6);
|
2001-06-11 12:39:29 +00:00
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
|
2000-07-05 02:14:16 +00:00
|
|
|
if (rrt->rrt_rflags & RRTF_NOADVERTISE)
|
1999-12-28 02:37:14 +00:00
|
|
|
continue;
|
2001-06-11 12:39:29 +00:00
|
|
|
|
|
|
|
/* Need to check filter here */
|
|
|
|
if (out_filter(rrt, ifcp) == 0)
|
1999-12-28 02:37:14 +00:00
|
|
|
continue;
|
2001-06-11 12:39:29 +00:00
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
/* Check split horizon and other conditions */
|
|
|
|
if (tobeadv(rrt, ifcp) == 0)
|
|
|
|
continue;
|
2001-06-11 12:39:29 +00:00
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
/* Only considers the routes with flag if specified */
|
2000-07-05 02:14:16 +00:00
|
|
|
if ((flag & RRTF_CHANGED) &&
|
|
|
|
(rrt->rrt_rflags & RRTF_CHANGED) == 0)
|
1999-12-28 02:37:14 +00:00
|
|
|
continue;
|
2001-06-11 12:39:29 +00:00
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
/* Check nexthop */
|
|
|
|
if (rrt->rrt_index == ifcp->ifc_index &&
|
|
|
|
!IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_gw) &&
|
2000-07-05 02:14:16 +00:00
|
|
|
(rrt->rrt_rflags & RRTF_NH_NOT_LLADDR) == 0) {
|
1999-12-28 02:37:14 +00:00
|
|
|
if (nh == NULL || !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw)) {
|
2012-11-18 15:37:27 +00:00
|
|
|
if (nrt == maxrte - 2) {
|
|
|
|
ripflush(ifcp, sin6, nrt, np);
|
|
|
|
nh = NULL;
|
|
|
|
nrt = 0;
|
|
|
|
np = ripbuf->rip6_nets;
|
|
|
|
}
|
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
np->rip6_dest = rrt->rrt_gw;
|
|
|
|
np->rip6_plen = 0;
|
|
|
|
np->rip6_tag = 0;
|
|
|
|
np->rip6_metric = NEXTHOP_METRIC;
|
|
|
|
nh = &rrt->rrt_gw;
|
|
|
|
np++; nrt++;
|
|
|
|
}
|
|
|
|
} else if (nh && (rrt->rrt_index != ifcp->ifc_index ||
|
|
|
|
!IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw) ||
|
2000-07-05 02:14:16 +00:00
|
|
|
rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)) {
|
1999-12-28 02:37:14 +00:00
|
|
|
/* Reset nexthop */
|
2012-11-18 15:37:27 +00:00
|
|
|
if (nrt == maxrte - 2) {
|
|
|
|
ripflush(ifcp, sin6, nrt, np);
|
|
|
|
nh = NULL;
|
|
|
|
nrt = 0;
|
|
|
|
np = ripbuf->rip6_nets;
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
memset(np, 0, sizeof(struct netinfo6));
|
|
|
|
np->rip6_metric = NEXTHOP_METRIC;
|
|
|
|
nh = NULL;
|
|
|
|
np++; nrt++;
|
|
|
|
}
|
2001-06-11 12:39:29 +00:00
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
/* Put the route to the buffer */
|
|
|
|
*np = rrt->rrt_info;
|
|
|
|
np++; nrt++;
|
|
|
|
if (nrt == maxrte) {
|
2012-11-18 15:37:27 +00:00
|
|
|
ripflush(ifcp, sin6, nrt, np);
|
1999-12-28 02:37:14 +00:00
|
|
|
nh = NULL;
|
2012-11-18 15:37:27 +00:00
|
|
|
nrt = 0;
|
|
|
|
np = ripbuf->rip6_nets;
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nrt) /* Send last packet */
|
2012-11-18 15:37:27 +00:00
|
|
|
ripflush(ifcp, sin6, nrt, np);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
2001-06-11 12:39:29 +00:00
|
|
|
/*
|
|
|
|
* outbound filter logic, per-route/interface.
|
|
|
|
*/
|
2017-01-13 06:22:49 +00:00
|
|
|
static int
|
2012-11-18 15:37:27 +00:00
|
|
|
out_filter(struct riprt *rrt, struct ifc *ifcp)
|
2001-06-11 12:39:29 +00:00
|
|
|
{
|
|
|
|
struct iff *iffp;
|
|
|
|
struct in6_addr ia;
|
|
|
|
int ok;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* -A: filter out less specific routes, if we have aggregated
|
|
|
|
* route configured.
|
2017-01-09 00:33:27 +00:00
|
|
|
*/
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
|
2001-06-11 12:39:29 +00:00
|
|
|
if (iffp->iff_type != 'A')
|
|
|
|
continue;
|
|
|
|
if (rrt->rrt_info.rip6_plen <= iffp->iff_plen)
|
|
|
|
continue;
|
2017-01-09 00:33:27 +00:00
|
|
|
ia = rrt->rrt_info.rip6_dest;
|
2001-06-11 12:39:29 +00:00
|
|
|
applyplen(&ia, iffp->iff_plen);
|
|
|
|
if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if it is an aggregated route, advertise it only to the
|
|
|
|
* interfaces specified on -A.
|
|
|
|
*/
|
|
|
|
if ((rrt->rrt_rflags & RRTF_AGGREGATE) != 0) {
|
|
|
|
ok = 0;
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
|
2001-06-11 12:39:29 +00:00
|
|
|
if (iffp->iff_type != 'A')
|
|
|
|
continue;
|
|
|
|
if (rrt->rrt_info.rip6_plen == iffp->iff_plen &&
|
|
|
|
IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
|
|
|
|
&iffp->iff_addr)) {
|
|
|
|
ok = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ok)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* -O: advertise only if prefix matches the configured prefix.
|
|
|
|
*/
|
2012-11-18 15:37:27 +00:00
|
|
|
if (iff_find(ifcp, IFIL_TYPE_O) != NULL) {
|
2001-06-11 12:39:29 +00:00
|
|
|
ok = 0;
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
|
2001-06-11 12:39:29 +00:00
|
|
|
if (iffp->iff_type != 'O')
|
|
|
|
continue;
|
|
|
|
if (rrt->rrt_info.rip6_plen < iffp->iff_plen)
|
|
|
|
continue;
|
2017-01-09 00:33:27 +00:00
|
|
|
ia = rrt->rrt_info.rip6_dest;
|
2001-06-11 12:39:29 +00:00
|
|
|
applyplen(&ia, iffp->iff_plen);
|
|
|
|
if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
|
|
|
|
ok = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ok)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the prefix should be advertised */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
/*
|
|
|
|
* Determine if the route is to be advertised on the specified interface.
|
|
|
|
* It checks options specified in the arguments and the split horizon rule.
|
|
|
|
*/
|
2017-01-13 06:22:49 +00:00
|
|
|
static int
|
2012-11-18 15:37:27 +00:00
|
|
|
tobeadv(struct riprt *rrt, struct ifc *ifcp)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
/* Special care for static routes */
|
|
|
|
if (rrt->rrt_flags & RTF_STATIC) {
|
2000-07-05 02:14:16 +00:00
|
|
|
/* XXX don't advertise reject/blackhole routes */
|
|
|
|
if (rrt->rrt_flags & (RTF_REJECT | RTF_BLACKHOLE))
|
|
|
|
return 0;
|
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
if (Sflag) /* Yes, advertise it anyway */
|
|
|
|
return 1;
|
|
|
|
if (sflag && rrt->rrt_index != ifcp->ifc_index)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Regular split horizon */
|
|
|
|
if (hflag == 0 && rrt->rrt_index == ifcp->ifc_index)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send a rip packet actually.
|
|
|
|
*/
|
2017-01-13 06:22:49 +00:00
|
|
|
static int
|
2012-11-18 15:37:27 +00:00
|
|
|
sendpacket(struct sockaddr_in6 *sin6, int len)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
struct msghdr m;
|
|
|
|
struct cmsghdr *cm;
|
|
|
|
struct iovec iov[2];
|
|
|
|
struct in6_pktinfo *pi;
|
2017-01-13 06:22:49 +00:00
|
|
|
u_char cmsgbuf[256];
|
2001-06-11 12:39:29 +00:00
|
|
|
int idx;
|
1999-12-28 02:37:14 +00:00
|
|
|
struct sockaddr_in6 sincopy;
|
|
|
|
|
|
|
|
/* do not overwrite the given sin */
|
2003-08-17 16:05:49 +00:00
|
|
|
sincopy = *sin6;
|
|
|
|
sin6 = &sincopy;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2003-08-17 16:26:35 +00:00
|
|
|
if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
|
2012-11-18 15:11:47 +00:00
|
|
|
IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
|
|
|
|
idx = sin6->sin6_scope_id;
|
|
|
|
else
|
2001-06-11 12:39:29 +00:00
|
|
|
idx = 0;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2003-08-17 16:05:49 +00:00
|
|
|
m.msg_name = (caddr_t)sin6;
|
|
|
|
m.msg_namelen = sizeof(*sin6);
|
1999-12-28 02:37:14 +00:00
|
|
|
iov[0].iov_base = (caddr_t)ripbuf;
|
|
|
|
iov[0].iov_len = len;
|
|
|
|
m.msg_iov = iov;
|
|
|
|
m.msg_iovlen = 1;
|
2017-01-12 16:22:28 +00:00
|
|
|
m.msg_flags = 0;
|
2001-06-11 12:39:29 +00:00
|
|
|
if (!idx) {
|
1999-12-28 02:37:14 +00:00
|
|
|
m.msg_control = NULL;
|
|
|
|
m.msg_controllen = 0;
|
|
|
|
} else {
|
|
|
|
memset(cmsgbuf, 0, sizeof(cmsgbuf));
|
2017-01-13 06:22:49 +00:00
|
|
|
cm = (struct cmsghdr *)(void *)cmsgbuf;
|
1999-12-28 02:37:14 +00:00
|
|
|
m.msg_control = (caddr_t)cm;
|
|
|
|
m.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
|
|
|
|
|
|
|
|
cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
|
|
|
|
cm->cmsg_level = IPPROTO_IPV6;
|
|
|
|
cm->cmsg_type = IPV6_PKTINFO;
|
2017-01-13 06:22:49 +00:00
|
|
|
pi = (struct in6_pktinfo *)(void *)CMSG_DATA(cm);
|
1999-12-28 02:37:14 +00:00
|
|
|
memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*::*/
|
2001-06-11 12:39:29 +00:00
|
|
|
pi->ipi6_ifindex = idx;
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sendmsg(ripsock, &m, 0 /*MSG_DONTROUTE*/) < 0) {
|
|
|
|
trace(1, "sendmsg: %s\n", strerror(errno));
|
|
|
|
return errno;
|
|
|
|
}
|
2000-07-10 18:27:55 +00:00
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-06-11 12:39:29 +00:00
|
|
|
* Receive and process RIP packets. Update the routes/kernel forwarding
|
1999-12-28 02:37:14 +00:00
|
|
|
* table if necessary.
|
|
|
|
*/
|
2017-01-13 06:22:49 +00:00
|
|
|
static void
|
2012-11-18 15:37:27 +00:00
|
|
|
riprecv(void)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
struct ifc *ifcp, *ic;
|
|
|
|
struct sockaddr_in6 fsock;
|
|
|
|
struct in6_addr nh; /* next hop */
|
|
|
|
struct rip6 *rp;
|
|
|
|
struct netinfo6 *np, *nq;
|
|
|
|
struct riprt *rrt;
|
2003-11-14 17:16:50 +00:00
|
|
|
ssize_t len, nn;
|
|
|
|
unsigned int need_trigger, idx;
|
1999-12-28 02:37:14 +00:00
|
|
|
char buf[4 * RIP6_MAXMTU];
|
|
|
|
time_t t;
|
|
|
|
struct msghdr m;
|
|
|
|
struct cmsghdr *cm;
|
|
|
|
struct iovec iov[2];
|
|
|
|
u_char cmsgbuf[256];
|
2006-11-16 19:03:03 +00:00
|
|
|
struct in6_pktinfo *pi = NULL;
|
|
|
|
int *hlimp = NULL;
|
1999-12-28 02:37:14 +00:00
|
|
|
struct iff *iffp;
|
|
|
|
struct in6_addr ia;
|
|
|
|
int ok;
|
2001-06-11 12:39:29 +00:00
|
|
|
time_t t_half_lifetime;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
need_trigger = 0;
|
2000-07-10 18:27:55 +00:00
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
m.msg_name = (caddr_t)&fsock;
|
|
|
|
m.msg_namelen = sizeof(fsock);
|
|
|
|
iov[0].iov_base = (caddr_t)buf;
|
|
|
|
iov[0].iov_len = sizeof(buf);
|
|
|
|
m.msg_iov = iov;
|
|
|
|
m.msg_iovlen = 1;
|
2017-01-13 06:22:49 +00:00
|
|
|
cm = (struct cmsghdr *)(void *)cmsgbuf;
|
1999-12-28 02:37:14 +00:00
|
|
|
m.msg_control = (caddr_t)cm;
|
|
|
|
m.msg_controllen = sizeof(cmsgbuf);
|
2017-01-12 16:22:28 +00:00
|
|
|
m.msg_flags = 0;
|
2001-06-11 12:39:29 +00:00
|
|
|
if ((len = recvmsg(ripsock, &m, 0)) < 0) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("recvmsg");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
idx = 0;
|
1999-12-28 02:37:14 +00:00
|
|
|
for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&m);
|
|
|
|
cm;
|
|
|
|
cm = (struct cmsghdr *)CMSG_NXTHDR(&m, cm)) {
|
2006-11-16 19:03:03 +00:00
|
|
|
if (cm->cmsg_level != IPPROTO_IPV6)
|
|
|
|
continue;
|
|
|
|
switch (cm->cmsg_type) {
|
|
|
|
case IPV6_PKTINFO:
|
|
|
|
if (cm->cmsg_len != CMSG_LEN(sizeof(*pi))) {
|
|
|
|
trace(1,
|
|
|
|
"invalid cmsg length for IPV6_PKTINFO\n");
|
|
|
|
return;
|
|
|
|
}
|
2017-01-13 06:22:49 +00:00
|
|
|
pi = (struct in6_pktinfo *)(void *)CMSG_DATA(cm);
|
2001-06-11 12:39:29 +00:00
|
|
|
idx = pi->ipi6_ifindex;
|
1999-12-28 02:37:14 +00:00
|
|
|
break;
|
2006-11-16 19:03:03 +00:00
|
|
|
case IPV6_HOPLIMIT:
|
|
|
|
if (cm->cmsg_len != CMSG_LEN(sizeof(int))) {
|
|
|
|
trace(1,
|
|
|
|
"invalid cmsg length for IPV6_HOPLIMIT\n");
|
|
|
|
return;
|
|
|
|
}
|
2017-01-13 06:22:49 +00:00
|
|
|
hlimp = (int *)(void *)CMSG_DATA(cm);
|
2006-11-16 19:03:03 +00:00
|
|
|
break;
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-18 15:37:27 +00:00
|
|
|
if ((size_t)len < sizeof(struct rip6)) {
|
2003-10-31 03:18:45 +00:00
|
|
|
trace(1, "Packet too short\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-11-16 19:03:03 +00:00
|
|
|
if (pi == NULL || hlimp == NULL) {
|
|
|
|
/*
|
|
|
|
* This can happen when the kernel failed to allocate memory
|
|
|
|
* for the ancillary data. Although we might be able to handle
|
|
|
|
* some cases without this info, those are minor and not so
|
|
|
|
* important, so it's better to discard the packet for safer
|
|
|
|
* operation.
|
|
|
|
*/
|
|
|
|
trace(1, "IPv6 packet information cannot be retrieved\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
nh = fsock.sin6_addr;
|
|
|
|
nn = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
|
|
|
|
sizeof(struct netinfo6);
|
2017-01-13 06:22:49 +00:00
|
|
|
rp = (struct rip6 *)(void *)buf;
|
1999-12-28 02:37:14 +00:00
|
|
|
np = rp->rip6_nets;
|
|
|
|
|
2003-08-17 16:26:35 +00:00
|
|
|
if (rp->rip6_vers != RIP6_VERSION) {
|
1999-12-28 02:37:14 +00:00
|
|
|
trace(1, "Incorrect RIP version %d\n", rp->rip6_vers);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (rp->rip6_cmd == RIP6_REQUEST) {
|
2001-06-11 12:39:29 +00:00
|
|
|
if (idx && idx < nindex2ifc) {
|
|
|
|
ifcp = index2ifc[idx];
|
1999-12-28 02:37:14 +00:00
|
|
|
riprequest(ifcp, np, nn, &fsock);
|
|
|
|
} else {
|
|
|
|
riprequest(NULL, np, nn, &fsock);
|
|
|
|
}
|
2017-01-09 00:33:27 +00:00
|
|
|
return;
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
if (!IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr)) {
|
2006-11-16 19:03:03 +00:00
|
|
|
trace(1, "Response from non-ll addr: %s\n",
|
2001-06-11 12:39:29 +00:00
|
|
|
inet6_n2p(&fsock.sin6_addr));
|
1999-12-28 02:37:14 +00:00
|
|
|
return; /* Ignore packets from non-link-local addr */
|
|
|
|
}
|
2006-11-16 19:03:03 +00:00
|
|
|
if (ntohs(fsock.sin6_port) != RIP6_PORT) {
|
|
|
|
trace(1, "Response from non-rip port from %s\n",
|
|
|
|
inet6_n2p(&fsock.sin6_addr));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (IN6_IS_ADDR_MULTICAST(&pi->ipi6_addr) && *hlimp != 255) {
|
|
|
|
trace(1,
|
|
|
|
"Response packet with a smaller hop limit (%d) from %s\n",
|
|
|
|
*hlimp, inet6_n2p(&fsock.sin6_addr));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Further validation: since this program does not send off-link
|
|
|
|
* requests, an incoming response must always come from an on-link
|
|
|
|
* node. Although this is normally ensured by the source address
|
|
|
|
* check above, it may not 100% be safe because there are router
|
|
|
|
* implementations that (invalidly) allow a packet with a link-local
|
|
|
|
* source address to be forwarded to a different link.
|
|
|
|
* So we also check whether the destination address is a link-local
|
|
|
|
* address or the hop limit is 255. Note that RFC2080 does not require
|
2017-01-09 00:33:27 +00:00
|
|
|
* the specific hop limit for a unicast response, so we cannot assume
|
2006-11-16 19:03:03 +00:00
|
|
|
* the limitation.
|
|
|
|
*/
|
|
|
|
if (!IN6_IS_ADDR_LINKLOCAL(&pi->ipi6_addr) && *hlimp != 255) {
|
|
|
|
trace(1,
|
|
|
|
"Response packet possibly from an off-link node: "
|
|
|
|
"from %s to %s hlim=%d\n",
|
|
|
|
inet6_n2p(&fsock.sin6_addr),
|
|
|
|
inet6_n2p(&pi->ipi6_addr), *hlimp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-18 15:11:47 +00:00
|
|
|
idx = fsock.sin6_scope_id;
|
2001-06-11 12:39:29 +00:00
|
|
|
ifcp = (idx < nindex2ifc) ? index2ifc[idx] : NULL;
|
1999-12-28 02:37:14 +00:00
|
|
|
if (!ifcp) {
|
2001-06-11 12:39:29 +00:00
|
|
|
trace(1, "Packets to unknown interface index %d\n", idx);
|
1999-12-28 02:37:14 +00:00
|
|
|
return; /* Ignore it */
|
|
|
|
}
|
|
|
|
if (IN6_ARE_ADDR_EQUAL(&ifcp->ifc_mylladdr, &fsock.sin6_addr))
|
|
|
|
return; /* The packet is from me; ignore */
|
|
|
|
if (rp->rip6_cmd != RIP6_RESPONSE) {
|
|
|
|
trace(1, "Invalid command %d\n", rp->rip6_cmd);
|
2017-01-09 00:33:27 +00:00
|
|
|
return;
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
2001-06-11 12:39:29 +00:00
|
|
|
|
|
|
|
/* -N: no use */
|
2012-11-18 15:37:27 +00:00
|
|
|
if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
|
1999-12-28 02:37:14 +00:00
|
|
|
return;
|
2001-06-11 12:39:29 +00:00
|
|
|
|
2011-12-18 00:03:03 +00:00
|
|
|
tracet(1, "Recv(%s): from %s.%d info(%zd)\n",
|
2001-06-11 12:39:29 +00:00
|
|
|
ifcp->ifc_name, inet6_n2p(&nh), ntohs(fsock.sin6_port), nn);
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
t = time(NULL);
|
2001-06-11 12:39:29 +00:00
|
|
|
t_half_lifetime = t - (RIP_LIFETIME/2);
|
1999-12-28 02:37:14 +00:00
|
|
|
for (; nn; nn--, np++) {
|
|
|
|
if (np->rip6_metric == NEXTHOP_METRIC) {
|
|
|
|
/* modify neighbor address */
|
|
|
|
if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
|
|
|
|
nh = np->rip6_dest;
|
|
|
|
trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
|
|
|
|
} else if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest)) {
|
|
|
|
nh = fsock.sin6_addr;
|
|
|
|
trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
|
|
|
|
} else {
|
|
|
|
nh = fsock.sin6_addr;
|
|
|
|
trace(1, "\tInvalid Nexthop: %s\n",
|
2001-06-11 12:39:29 +00:00
|
|
|
inet6_n2p(&np->rip6_dest));
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (IN6_IS_ADDR_MULTICAST(&np->rip6_dest)) {
|
|
|
|
trace(1, "\tMulticast netinfo6: %s/%d [%d]\n",
|
|
|
|
inet6_n2p(&np->rip6_dest),
|
|
|
|
np->rip6_plen, np->rip6_metric);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (IN6_IS_ADDR_LOOPBACK(&np->rip6_dest)) {
|
|
|
|
trace(1, "\tLoopback netinfo6: %s/%d [%d]\n",
|
|
|
|
inet6_n2p(&np->rip6_dest),
|
|
|
|
np->rip6_plen, np->rip6_metric);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
|
|
|
|
trace(1, "\tLink Local netinfo6: %s/%d [%d]\n",
|
|
|
|
inet6_n2p(&np->rip6_dest),
|
|
|
|
np->rip6_plen, np->rip6_metric);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* may need to pass sitelocal prefix in some case, however*/
|
|
|
|
if (IN6_IS_ADDR_SITELOCAL(&np->rip6_dest) && !lflag) {
|
|
|
|
trace(1, "\tSite Local netinfo6: %s/%d [%d]\n",
|
|
|
|
inet6_n2p(&np->rip6_dest),
|
|
|
|
np->rip6_plen, np->rip6_metric);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
trace(2, "\tnetinfo6: %s/%d [%d]",
|
|
|
|
inet6_n2p(&np->rip6_dest),
|
|
|
|
np->rip6_plen, np->rip6_metric);
|
|
|
|
if (np->rip6_tag)
|
|
|
|
trace(2, " tag=0x%04x", ntohs(np->rip6_tag) & 0xffff);
|
2000-07-05 02:14:16 +00:00
|
|
|
if (dflag >= 2) {
|
|
|
|
ia = np->rip6_dest;
|
|
|
|
applyplen(&ia, np->rip6_plen);
|
|
|
|
if (!IN6_ARE_ADDR_EQUAL(&ia, &np->rip6_dest))
|
|
|
|
trace(2, " [junk outside prefix]");
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2001-06-11 12:39:29 +00:00
|
|
|
/*
|
|
|
|
* -L: listen only if the prefix matches the configuration
|
|
|
|
*/
|
2012-11-18 15:37:27 +00:00
|
|
|
ok = 1; /* if there's no L filter, it is ok */
|
|
|
|
TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
|
|
|
|
if (iffp->iff_type != IFIL_TYPE_L)
|
|
|
|
continue;
|
|
|
|
ok = 0;
|
|
|
|
if (np->rip6_plen < iffp->iff_plen)
|
|
|
|
continue;
|
|
|
|
/* special rule: ::/0 means default, not "in /0" */
|
|
|
|
if (iffp->iff_plen == 0 && np->rip6_plen > 0)
|
|
|
|
continue;
|
2017-01-09 00:33:27 +00:00
|
|
|
ia = np->rip6_dest;
|
2012-11-18 15:37:27 +00:00
|
|
|
applyplen(&ia, iffp->iff_plen);
|
|
|
|
if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
|
|
|
|
ok = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
if (!ok) {
|
|
|
|
trace(2, " (filtered)\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace(2, "\n");
|
|
|
|
np->rip6_metric++;
|
|
|
|
np->rip6_metric += ifcp->ifc_metric;
|
|
|
|
if (np->rip6_metric > HOPCNT_INFINITY6)
|
|
|
|
np->rip6_metric = HOPCNT_INFINITY6;
|
|
|
|
|
|
|
|
applyplen(&np->rip6_dest, np->rip6_plen);
|
2012-11-18 15:37:27 +00:00
|
|
|
if ((rrt = rtsearch(np)) != NULL) {
|
1999-12-28 02:37:14 +00:00
|
|
|
if (rrt->rrt_t == 0)
|
|
|
|
continue; /* Intf route has priority */
|
|
|
|
nq = &rrt->rrt_info;
|
|
|
|
if (nq->rip6_metric > np->rip6_metric) {
|
|
|
|
if (rrt->rrt_index == ifcp->ifc_index &&
|
|
|
|
IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
|
|
|
|
/* Small metric from the same gateway */
|
|
|
|
nq->rip6_metric = np->rip6_metric;
|
|
|
|
} else {
|
|
|
|
/* Better route found */
|
|
|
|
rrt->rrt_index = ifcp->ifc_index;
|
|
|
|
/* Update routing table */
|
|
|
|
delroute(nq, &rrt->rrt_gw);
|
|
|
|
rrt->rrt_gw = nh;
|
|
|
|
*nq = *np;
|
|
|
|
addroute(rrt, &nh, ifcp);
|
|
|
|
}
|
2000-07-05 02:14:16 +00:00
|
|
|
rrt->rrt_rflags |= RRTF_CHANGED;
|
1999-12-28 02:37:14 +00:00
|
|
|
rrt->rrt_t = t;
|
|
|
|
need_trigger = 1;
|
|
|
|
} else if (nq->rip6_metric < np->rip6_metric &&
|
|
|
|
rrt->rrt_index == ifcp->ifc_index &&
|
|
|
|
IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
|
|
|
|
/* Got worse route from same gw */
|
|
|
|
nq->rip6_metric = np->rip6_metric;
|
|
|
|
rrt->rrt_t = t;
|
2000-07-05 02:14:16 +00:00
|
|
|
rrt->rrt_rflags |= RRTF_CHANGED;
|
1999-12-28 02:37:14 +00:00
|
|
|
need_trigger = 1;
|
|
|
|
} else if (nq->rip6_metric == np->rip6_metric &&
|
|
|
|
np->rip6_metric < HOPCNT_INFINITY6) {
|
2001-06-11 12:39:29 +00:00
|
|
|
if (rrt->rrt_index == ifcp->ifc_index &&
|
2017-01-09 00:33:27 +00:00
|
|
|
IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
|
2001-06-11 12:39:29 +00:00
|
|
|
/* same metric, same route from same gw */
|
|
|
|
rrt->rrt_t = t;
|
|
|
|
} else if (rrt->rrt_t < t_half_lifetime) {
|
|
|
|
/* Better route found */
|
|
|
|
rrt->rrt_index = ifcp->ifc_index;
|
|
|
|
/* Update routing table */
|
|
|
|
delroute(nq, &rrt->rrt_gw);
|
|
|
|
rrt->rrt_gw = nh;
|
|
|
|
*nq = *np;
|
|
|
|
addroute(rrt, &nh, ifcp);
|
|
|
|
rrt->rrt_rflags |= RRTF_CHANGED;
|
|
|
|
rrt->rrt_t = t;
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
2017-01-09 00:33:27 +00:00
|
|
|
/*
|
1999-12-28 02:37:14 +00:00
|
|
|
* if nq->rip6_metric == HOPCNT_INFINITY6 then
|
2001-06-11 12:39:29 +00:00
|
|
|
* do not update age value. Do nothing.
|
1999-12-28 02:37:14 +00:00
|
|
|
*/
|
|
|
|
} else if (np->rip6_metric < HOPCNT_INFINITY6) {
|
|
|
|
/* Got a new valid route */
|
2001-06-11 12:39:29 +00:00
|
|
|
if ((rrt = MALLOC(struct riprt)) == NULL) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("malloc: struct riprt");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2000-07-05 02:14:16 +00:00
|
|
|
memset(rrt, 0, sizeof(*rrt));
|
1999-12-28 02:37:14 +00:00
|
|
|
nq = &rrt->rrt_info;
|
|
|
|
|
|
|
|
rrt->rrt_same = NULL;
|
|
|
|
rrt->rrt_index = ifcp->ifc_index;
|
|
|
|
rrt->rrt_flags = RTF_UP|RTF_GATEWAY;
|
|
|
|
rrt->rrt_gw = nh;
|
|
|
|
*nq = *np;
|
|
|
|
applyplen(&nq->rip6_dest, nq->rip6_plen);
|
|
|
|
if (nq->rip6_plen == sizeof(struct in6_addr) * 8)
|
|
|
|
rrt->rrt_flags |= RTF_HOST;
|
|
|
|
|
|
|
|
/* Update routing table */
|
|
|
|
addroute(rrt, &nh, ifcp);
|
2000-07-05 02:14:16 +00:00
|
|
|
rrt->rrt_rflags |= RRTF_CHANGED;
|
1999-12-28 02:37:14 +00:00
|
|
|
need_trigger = 1;
|
|
|
|
rrt->rrt_t = t;
|
2012-11-18 15:37:27 +00:00
|
|
|
|
|
|
|
/* Put the route to the list */
|
|
|
|
TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* XXX need to care the interval between triggered updates */
|
|
|
|
if (need_trigger) {
|
|
|
|
if (nextalarm > time(NULL) + RIP_TRIG_INT6_MAX) {
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
|
1999-12-28 02:37:14 +00:00
|
|
|
if (ifcp->ifc_index == ic->ifc_index)
|
|
|
|
continue;
|
|
|
|
if (ic->ifc_flags & IFF_UP)
|
|
|
|
ripsend(ic, &ic->ifc_ripsin,
|
2000-07-05 02:14:16 +00:00
|
|
|
RRTF_CHANGED);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Reset the flag */
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
|
2000-07-05 02:14:16 +00:00
|
|
|
rrt->rrt_rflags &= ~RRTF_CHANGED;
|
2012-11-18 15:37:27 +00:00
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send all routes request packet to the specified interface.
|
|
|
|
*/
|
2017-01-13 06:22:49 +00:00
|
|
|
static void
|
2012-11-18 15:37:27 +00:00
|
|
|
sendrequest(struct ifc *ifcp)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
struct netinfo6 *np;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if (ifcp->ifc_flags & IFF_LOOPBACK)
|
|
|
|
return;
|
|
|
|
ripbuf->rip6_cmd = RIP6_REQUEST;
|
|
|
|
np = ripbuf->rip6_nets;
|
|
|
|
memset(np, 0, sizeof(struct netinfo6));
|
|
|
|
np->rip6_metric = HOPCNT_INFINITY6;
|
|
|
|
tracet(1, "Send rtdump Request to %s (%s)\n",
|
|
|
|
ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
|
|
|
|
error = sendpacket(&ifcp->ifc_ripsin, RIPSIZE(1));
|
|
|
|
if (error == EAFNOSUPPORT) {
|
|
|
|
/* Protocol not supported */
|
|
|
|
tracet(1, "Could not send rtdump Request to %s (%s): "
|
|
|
|
"set IFF_UP to 0\n",
|
|
|
|
ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
|
|
|
|
ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
|
|
|
|
}
|
|
|
|
ripbuf->rip6_cmd = RIP6_RESPONSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process a RIP6_REQUEST packet.
|
|
|
|
*/
|
2017-01-13 06:22:49 +00:00
|
|
|
static void
|
2012-11-18 15:37:27 +00:00
|
|
|
riprequest(struct ifc *ifcp,
|
|
|
|
struct netinfo6 *np,
|
|
|
|
int nn,
|
|
|
|
struct sockaddr_in6 *sin6)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct riprt *rrt;
|
|
|
|
|
|
|
|
if (!(nn == 1 && IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest) &&
|
|
|
|
np->rip6_plen == 0 && np->rip6_metric == HOPCNT_INFINITY6)) {
|
|
|
|
/* Specific response, don't split-horizon */
|
|
|
|
trace(1, "\tRIP Request\n");
|
|
|
|
for (i = 0; i < nn; i++, np++) {
|
2012-11-18 15:37:27 +00:00
|
|
|
rrt = rtsearch(np);
|
1999-12-28 02:37:14 +00:00
|
|
|
if (rrt)
|
|
|
|
np->rip6_metric = rrt->rrt_info.rip6_metric;
|
|
|
|
else
|
|
|
|
np->rip6_metric = HOPCNT_INFINITY6;
|
|
|
|
}
|
2003-08-17 16:05:49 +00:00
|
|
|
(void)sendpacket(sin6, RIPSIZE(nn));
|
1999-12-28 02:37:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Whole routing table dump */
|
|
|
|
trace(1, "\tRIP Request -- whole routing table\n");
|
2003-08-17 16:05:49 +00:00
|
|
|
ripsend(ifcp, sin6, RRTF_SENDANYWAY);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get information of each interface.
|
|
|
|
*/
|
2017-01-13 06:22:49 +00:00
|
|
|
static void
|
2012-11-18 15:37:27 +00:00
|
|
|
ifconfig(void)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
2000-07-05 02:14:16 +00:00
|
|
|
struct ifaddrs *ifap, *ifa;
|
|
|
|
struct ifc *ifcp;
|
|
|
|
struct ipv6_mreq mreq;
|
|
|
|
int s;
|
|
|
|
|
2001-06-11 12:39:29 +00:00
|
|
|
if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
|
2000-07-05 02:14:16 +00:00
|
|
|
fatal("socket");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2000-07-05 02:14:16 +00:00
|
|
|
|
2001-06-11 12:39:29 +00:00
|
|
|
if (getifaddrs(&ifap) != 0) {
|
2000-07-05 02:14:16 +00:00
|
|
|
fatal("getifaddrs");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2000-07-05 02:14:16 +00:00
|
|
|
|
|
|
|
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
|
|
|
|
if (ifa->ifa_addr->sa_family != AF_INET6)
|
|
|
|
continue;
|
|
|
|
ifcp = ifc_find(ifa->ifa_name);
|
|
|
|
/* we are interested in multicast-capable interfaces */
|
|
|
|
if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
|
|
|
|
continue;
|
|
|
|
if (!ifcp) {
|
|
|
|
/* new interface */
|
2001-06-11 12:39:29 +00:00
|
|
|
if ((ifcp = MALLOC(struct ifc)) == NULL) {
|
2000-07-05 02:14:16 +00:00
|
|
|
fatal("malloc: struct ifc");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2000-07-05 02:14:16 +00:00
|
|
|
memset(ifcp, 0, sizeof(*ifcp));
|
2012-11-18 15:37:27 +00:00
|
|
|
|
2000-07-05 02:14:16 +00:00
|
|
|
ifcp->ifc_index = -1;
|
2012-11-18 15:37:27 +00:00
|
|
|
strlcpy(ifcp->ifc_name, ifa->ifa_name,
|
|
|
|
sizeof(ifcp->ifc_name));
|
|
|
|
TAILQ_INIT(&ifcp->ifc_ifac_head);
|
|
|
|
TAILQ_INIT(&ifcp->ifc_iff_head);
|
2000-07-05 02:14:16 +00:00
|
|
|
ifcp->ifc_flags = ifa->ifa_flags;
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_INSERT_HEAD(&ifc_head, ifcp, ifc_next);
|
2000-07-05 02:14:16 +00:00
|
|
|
trace(1, "newif %s <%s>\n", ifcp->ifc_name,
|
|
|
|
ifflags(ifcp->ifc_flags));
|
|
|
|
if (!strcmp(ifcp->ifc_name, LOOPBACK_IF))
|
|
|
|
loopifcp = ifcp;
|
|
|
|
} else {
|
|
|
|
/* update flag, this may be up again */
|
|
|
|
if (ifcp->ifc_flags != ifa->ifa_flags) {
|
|
|
|
trace(1, "%s: <%s> -> ", ifcp->ifc_name,
|
|
|
|
ifflags(ifcp->ifc_flags));
|
|
|
|
trace(1, "<%s>\n", ifflags(ifa->ifa_flags));
|
2001-06-11 12:39:29 +00:00
|
|
|
ifcp->ifc_cflags |= IFC_CHANGED;
|
2000-07-05 02:14:16 +00:00
|
|
|
}
|
|
|
|
ifcp->ifc_flags = ifa->ifa_flags;
|
|
|
|
}
|
2012-11-18 15:37:27 +00:00
|
|
|
if (ifconfig1(ifa->ifa_name, ifa->ifa_addr, ifcp, s) < 0) {
|
|
|
|
/* maybe temporary failure */
|
|
|
|
continue;
|
|
|
|
}
|
2000-07-05 02:14:16 +00:00
|
|
|
if ((ifcp->ifc_flags & (IFF_LOOPBACK | IFF_UP)) == IFF_UP
|
|
|
|
&& 0 < ifcp->ifc_index && !ifcp->ifc_joined) {
|
|
|
|
mreq.ipv6mr_multiaddr = ifcp->ifc_ripsin.sin6_addr;
|
|
|
|
mreq.ipv6mr_interface = ifcp->ifc_index;
|
2001-06-11 12:39:29 +00:00
|
|
|
if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
|
|
|
|
&mreq, sizeof(mreq)) < 0) {
|
2000-07-05 02:14:16 +00:00
|
|
|
fatal("IPV6_JOIN_GROUP");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
trace(1, "join %s %s\n", ifcp->ifc_name, RIP6_DEST);
|
|
|
|
ifcp->ifc_joined++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(s);
|
2001-06-11 12:39:29 +00:00
|
|
|
freeifaddrs(ifap);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static int
|
2012-11-18 15:37:27 +00:00
|
|
|
ifconfig1(const char *name,
|
|
|
|
const struct sockaddr *sa,
|
|
|
|
struct ifc *ifcp,
|
|
|
|
int s)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
struct in6_ifreq ifr;
|
2003-08-17 16:05:49 +00:00
|
|
|
const struct sockaddr_in6 *sin6;
|
2012-11-18 15:37:27 +00:00
|
|
|
struct ifac *ifac;
|
1999-12-28 02:37:14 +00:00
|
|
|
int plen;
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
sin6 = (const struct sockaddr_in6 *)(const void *)sa;
|
2003-11-14 17:16:50 +00:00
|
|
|
if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && !lflag)
|
2012-11-18 15:37:27 +00:00
|
|
|
return (-1);
|
2003-08-17 16:05:49 +00:00
|
|
|
ifr.ifr_addr = *sin6;
|
2016-05-15 22:31:03 +00:00
|
|
|
strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
|
2001-06-11 12:39:29 +00:00
|
|
|
if (ioctl(s, SIOCGIFNETMASK_IN6, (char *)&ifr) < 0) {
|
2012-11-18 15:37:27 +00:00
|
|
|
syslog(LOG_INFO, "ioctl: SIOCGIFNETMASK_IN6");
|
|
|
|
return (-1);
|
2001-06-11 12:39:29 +00:00
|
|
|
}
|
|
|
|
plen = sin6mask2len(&ifr.ifr_addr);
|
2012-11-18 15:37:27 +00:00
|
|
|
if ((ifac = ifa_match(ifcp, &sin6->sin6_addr, plen)) != NULL) {
|
1999-12-28 02:37:14 +00:00
|
|
|
/* same interface found */
|
|
|
|
/* need check if something changed */
|
|
|
|
/* XXX not yet implemented */
|
2012-11-18 15:37:27 +00:00
|
|
|
return (-1);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* New address is found
|
|
|
|
*/
|
2012-11-18 15:37:27 +00:00
|
|
|
if ((ifac = MALLOC(struct ifac)) == NULL) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("malloc: struct ifac");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2012-11-18 15:37:27 +00:00
|
|
|
memset(ifac, 0, sizeof(*ifac));
|
|
|
|
|
|
|
|
ifac->ifac_ifc = ifcp;
|
|
|
|
ifac->ifac_addr = sin6->sin6_addr;
|
|
|
|
ifac->ifac_plen = plen;
|
|
|
|
ifac->ifac_scope_id = sin6->sin6_scope_id;
|
1999-12-28 02:37:14 +00:00
|
|
|
if (ifcp->ifc_flags & IFF_POINTOPOINT) {
|
2003-08-17 16:05:49 +00:00
|
|
|
ifr.ifr_addr = *sin6;
|
2001-06-11 12:39:29 +00:00
|
|
|
if (ioctl(s, SIOCGIFDSTADDR_IN6, (char *)&ifr) < 0) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("ioctl: SIOCGIFDSTADDR_IN6");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2012-11-18 15:37:27 +00:00
|
|
|
ifac->ifac_raddr = ifr.ifr_dstaddr.sin6_addr;
|
|
|
|
inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr, buf,
|
|
|
|
sizeof(buf));
|
1999-12-28 02:37:14 +00:00
|
|
|
trace(1, "found address %s/%d -- %s\n",
|
2012-11-18 15:37:27 +00:00
|
|
|
inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen, buf);
|
1999-12-28 02:37:14 +00:00
|
|
|
} else {
|
|
|
|
trace(1, "found address %s/%d\n",
|
2012-11-18 15:37:27 +00:00
|
|
|
inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
2012-11-18 15:37:27 +00:00
|
|
|
if (ifcp->ifc_index < 0 && IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
|
|
|
|
ifcp->ifc_mylladdr = ifac->ifac_addr;
|
|
|
|
ifcp->ifc_index = ifac->ifac_scope_id;
|
1999-12-28 02:37:14 +00:00
|
|
|
memcpy(&ifcp->ifc_ripsin, &ripsin, ripsin.ss_len);
|
2012-11-18 15:11:47 +00:00
|
|
|
ifcp->ifc_ripsin.sin6_scope_id = ifcp->ifc_index;
|
1999-12-28 02:37:14 +00:00
|
|
|
setindex2ifc(ifcp->ifc_index, ifcp);
|
|
|
|
ifcp->ifc_mtu = getifmtu(ifcp->ifc_index);
|
|
|
|
if (ifcp->ifc_mtu > RIP6_MAXMTU)
|
|
|
|
ifcp->ifc_mtu = RIP6_MAXMTU;
|
2001-06-11 12:39:29 +00:00
|
|
|
if (ioctl(s, SIOCGIFMETRIC, (char *)&ifr) < 0) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("ioctl: SIOCGIFMETRIC");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
ifcp->ifc_metric = ifr.ifr_metric;
|
|
|
|
trace(1, "\tindex: %d, mtu: %d, metric: %d\n",
|
|
|
|
ifcp->ifc_index, ifcp->ifc_mtu, ifcp->ifc_metric);
|
2001-06-11 12:39:29 +00:00
|
|
|
} else
|
|
|
|
ifcp->ifc_cflags |= IFC_CHANGED;
|
2012-11-18 15:37:27 +00:00
|
|
|
|
|
|
|
TAILQ_INSERT_HEAD(&ifcp->ifc_ifac_head, ifac, ifac_next);
|
|
|
|
|
|
|
|
return 0;
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static void
|
2012-11-18 15:48:02 +00:00
|
|
|
ifremove(int ifindex)
|
|
|
|
{
|
|
|
|
struct ifc *ifcp;
|
|
|
|
struct riprt *rrt;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
|
|
|
|
if (ifcp->ifc_index == ifindex)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ifcp == NULL)
|
2017-01-09 00:33:27 +00:00
|
|
|
return;
|
2012-11-18 15:48:02 +00:00
|
|
|
|
|
|
|
tracet(1, "ifremove: %s is departed.\n", ifcp->ifc_name);
|
|
|
|
TAILQ_REMOVE(&ifc_head, ifcp, ifc_next);
|
|
|
|
|
|
|
|
TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
|
|
|
|
if (rrt->rrt_index == ifcp->ifc_index &&
|
|
|
|
rrt->rrt_rflags & RRTF_AGGREGATE)
|
|
|
|
delroute(&rrt->rrt_info, &rrt->rrt_gw);
|
|
|
|
}
|
|
|
|
free(ifcp);
|
|
|
|
}
|
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
/*
|
|
|
|
* Receive and process routing messages.
|
|
|
|
* Update interface information as necesssary.
|
|
|
|
*/
|
2017-01-13 06:22:49 +00:00
|
|
|
static void
|
2012-11-18 15:37:27 +00:00
|
|
|
rtrecv(void)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
char buf[BUFSIZ];
|
2012-11-18 15:37:27 +00:00
|
|
|
char *p, *q = NULL;
|
1999-12-28 02:37:14 +00:00
|
|
|
struct rt_msghdr *rtm;
|
|
|
|
struct ifa_msghdr *ifam;
|
|
|
|
struct if_msghdr *ifm;
|
2012-11-18 15:48:02 +00:00
|
|
|
struct if_announcemsghdr *ifan;
|
1999-12-28 02:37:14 +00:00
|
|
|
int len;
|
2001-06-11 12:39:29 +00:00
|
|
|
struct ifc *ifcp, *ic;
|
1999-12-28 02:37:14 +00:00
|
|
|
int iface = 0, rtable = 0;
|
|
|
|
struct sockaddr_in6 *rta[RTAX_MAX];
|
2001-06-11 12:39:29 +00:00
|
|
|
struct sockaddr_in6 mask;
|
2012-11-18 15:37:27 +00:00
|
|
|
int i, addrs = 0;
|
2001-06-11 12:39:29 +00:00
|
|
|
struct riprt *rrt;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
|
|
|
|
perror("read from rtsock");
|
2001-06-11 12:39:29 +00:00
|
|
|
exit(1);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
2012-11-18 15:37:27 +00:00
|
|
|
if (len == 0)
|
|
|
|
return;
|
|
|
|
#if 0
|
1999-12-28 02:37:14 +00:00
|
|
|
if (len < sizeof(*rtm)) {
|
2000-11-27 17:44:53 +00:00
|
|
|
trace(1, "short read from rtsock: %d (should be > %lu)\n",
|
|
|
|
len, (u_long)sizeof(*rtm));
|
1999-12-28 02:37:14 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-11-18 15:37:27 +00:00
|
|
|
#endif
|
2003-08-17 18:12:11 +00:00
|
|
|
if (dflag >= 2) {
|
|
|
|
fprintf(stderr, "rtmsg:\n");
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
fprintf(stderr, "%02x ", buf[i] & 0xff);
|
|
|
|
if (i % 16 == 15) fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
for (p = buf; p - buf < len; p +=
|
|
|
|
((struct rt_msghdr *)(void *)p)->rtm_msglen) {
|
|
|
|
if (((struct rt_msghdr *)(void *)p)->rtm_version != RTM_VERSION)
|
2012-11-18 15:37:27 +00:00
|
|
|
continue;
|
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
/* safety against bogus message */
|
2017-01-13 06:22:49 +00:00
|
|
|
if (((struct rt_msghdr *)(void *)p)->rtm_msglen <= 0) {
|
1999-12-28 02:37:14 +00:00
|
|
|
trace(1, "bogus rtmsg: length=%d\n",
|
2017-01-13 06:22:49 +00:00
|
|
|
((struct rt_msghdr *)(void *)p)->rtm_msglen);
|
1999-12-28 02:37:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
rtm = NULL;
|
|
|
|
ifam = NULL;
|
|
|
|
ifm = NULL;
|
2017-01-13 06:22:49 +00:00
|
|
|
switch (((struct rt_msghdr *)(void *)p)->rtm_type) {
|
1999-12-28 02:37:14 +00:00
|
|
|
case RTM_NEWADDR:
|
|
|
|
case RTM_DELADDR:
|
2017-01-13 06:22:49 +00:00
|
|
|
ifam = (struct ifa_msghdr *)(void *)p;
|
1999-12-28 02:37:14 +00:00
|
|
|
addrs = ifam->ifam_addrs;
|
|
|
|
q = (char *)(ifam + 1);
|
|
|
|
break;
|
|
|
|
case RTM_IFINFO:
|
2017-01-13 06:22:49 +00:00
|
|
|
ifm = (struct if_msghdr *)(void *)p;
|
1999-12-28 02:37:14 +00:00
|
|
|
addrs = ifm->ifm_addrs;
|
|
|
|
q = (char *)(ifm + 1);
|
|
|
|
break;
|
2012-11-18 15:48:02 +00:00
|
|
|
case RTM_IFANNOUNCE:
|
2017-01-13 06:22:49 +00:00
|
|
|
ifan = (struct if_announcemsghdr *)(void *)p;
|
2012-11-18 15:48:02 +00:00
|
|
|
switch (ifan->ifan_what) {
|
|
|
|
case IFAN_ARRIVAL:
|
|
|
|
iface++;
|
|
|
|
break;
|
|
|
|
case IFAN_DEPARTURE:
|
|
|
|
ifremove(ifan->ifan_index);
|
|
|
|
iface++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
1999-12-28 02:37:14 +00:00
|
|
|
default:
|
2017-01-13 06:22:49 +00:00
|
|
|
rtm = (struct rt_msghdr *)(void *)p;
|
1999-12-28 02:37:14 +00:00
|
|
|
if (rtm->rtm_version != RTM_VERSION) {
|
|
|
|
trace(1, "unexpected rtmsg version %d "
|
|
|
|
"(should be %d)\n",
|
|
|
|
rtm->rtm_version, RTM_VERSION);
|
|
|
|
continue;
|
|
|
|
}
|
2017-04-16 19:17:10 +00:00
|
|
|
/*
|
|
|
|
* Only messages that use the struct rt_msghdr
|
|
|
|
* format are allowed beyond this point.
|
|
|
|
*/
|
|
|
|
if (rtm->rtm_type > RTM_RESOLVE) {
|
|
|
|
trace(1, "rtmsg type %d ignored\n",
|
|
|
|
rtm->rtm_type);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
addrs = rtm->rtm_addrs;
|
|
|
|
q = (char *)(rtm + 1);
|
1999-12-28 02:37:14 +00:00
|
|
|
if (rtm->rtm_pid == pid) {
|
|
|
|
#if 0
|
|
|
|
trace(1, "rtmsg looped back to me, ignored\n");
|
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
memset(&rta, 0, sizeof(rta));
|
|
|
|
for (i = 0; i < RTAX_MAX; i++) {
|
|
|
|
if (addrs & (1 << i)) {
|
2017-01-13 06:22:49 +00:00
|
|
|
rta[i] = (struct sockaddr_in6 *)(void *)q;
|
1999-12-28 02:37:14 +00:00
|
|
|
q += ROUNDUP(rta[i]->sin6_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trace(1, "rtsock: %s (addrs=%x)\n",
|
2017-01-13 06:22:49 +00:00
|
|
|
rttypes((struct rt_msghdr *)(void *)p), addrs);
|
1999-12-28 02:37:14 +00:00
|
|
|
if (dflag >= 2) {
|
|
|
|
for (i = 0;
|
2017-01-13 06:22:49 +00:00
|
|
|
i < ((struct rt_msghdr *)(void *)p)->rtm_msglen;
|
1999-12-28 02:37:14 +00:00
|
|
|
i++) {
|
|
|
|
fprintf(stderr, "%02x ", p[i] & 0xff);
|
|
|
|
if (i % 16 == 15) fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Easy ones first.
|
|
|
|
*
|
|
|
|
* We may be able to optimize by using ifm->ifm_index or
|
|
|
|
* ifam->ifam_index. For simplicity we don't do that here.
|
|
|
|
*/
|
2017-01-13 06:22:49 +00:00
|
|
|
switch (((struct rt_msghdr *)(void *)p)->rtm_type) {
|
1999-12-28 02:37:14 +00:00
|
|
|
case RTM_NEWADDR:
|
|
|
|
case RTM_IFINFO:
|
|
|
|
iface++;
|
|
|
|
continue;
|
|
|
|
case RTM_ADD:
|
|
|
|
rtable++;
|
|
|
|
continue;
|
|
|
|
case RTM_LOSING:
|
|
|
|
case RTM_MISS:
|
|
|
|
case RTM_GET:
|
|
|
|
case RTM_LOCK:
|
|
|
|
/* nothing to be done here */
|
|
|
|
trace(1, "\tnothing to be done, ignored\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
if (rta[RTAX_DST] == NULL) {
|
|
|
|
trace(1, "\tno destination, ignored\n");
|
2017-01-09 00:33:27 +00:00
|
|
|
continue;
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
if (rta[RTAX_DST]->sin6_family != AF_INET6) {
|
|
|
|
trace(1, "\taf mismatch, ignored\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (IN6_IS_ADDR_LINKLOCAL(&rta[RTAX_DST]->sin6_addr)) {
|
|
|
|
trace(1, "\tlinklocal destination, ignored\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (IN6_ARE_ADDR_EQUAL(&rta[RTAX_DST]->sin6_addr, &in6addr_loopback)) {
|
|
|
|
trace(1, "\tloopback destination, ignored\n");
|
|
|
|
continue; /* Loopback */
|
|
|
|
}
|
|
|
|
if (IN6_IS_ADDR_MULTICAST(&rta[RTAX_DST]->sin6_addr)) {
|
|
|
|
trace(1, "\tmulticast destination, ignored\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* hard ones */
|
2017-01-13 06:22:49 +00:00
|
|
|
switch (((struct rt_msghdr *)(void *)p)->rtm_type) {
|
1999-12-28 02:37:14 +00:00
|
|
|
case RTM_NEWADDR:
|
|
|
|
case RTM_IFINFO:
|
|
|
|
case RTM_ADD:
|
|
|
|
case RTM_LOSING:
|
|
|
|
case RTM_MISS:
|
|
|
|
case RTM_GET:
|
|
|
|
case RTM_LOCK:
|
|
|
|
/* should already be handled */
|
|
|
|
fatal("rtrecv: never reach here");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
1999-12-28 02:37:14 +00:00
|
|
|
case RTM_DELETE:
|
2001-06-11 12:39:29 +00:00
|
|
|
if (!rta[RTAX_DST] || !rta[RTAX_GATEWAY]) {
|
|
|
|
trace(1, "\tsome of dst/gw/netamsk are "
|
|
|
|
"unavailable, ignored\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((rtm->rtm_flags & RTF_HOST) != 0) {
|
|
|
|
mask.sin6_len = sizeof(mask);
|
|
|
|
memset(&mask.sin6_addr, 0xff,
|
|
|
|
sizeof(mask.sin6_addr));
|
|
|
|
rta[RTAX_NETMASK] = &mask;
|
|
|
|
} else if (!rta[RTAX_NETMASK]) {
|
|
|
|
trace(1, "\tsome of dst/gw/netamsk are "
|
|
|
|
"unavailable, ignored\n");
|
1999-12-28 02:37:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-06-11 12:39:29 +00:00
|
|
|
if (rt_del(rta[RTAX_DST], rta[RTAX_GATEWAY],
|
|
|
|
rta[RTAX_NETMASK]) == 0) {
|
1999-12-28 02:37:14 +00:00
|
|
|
rtable++; /*just to be sure*/
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RTM_CHANGE:
|
|
|
|
case RTM_REDIRECT:
|
|
|
|
trace(1, "\tnot supported yet, ignored\n");
|
|
|
|
break;
|
|
|
|
case RTM_DELADDR:
|
|
|
|
if (!rta[RTAX_NETMASK] || !rta[RTAX_IFA]) {
|
|
|
|
trace(1, "\tno netmask or ifa given, ignored\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ifam->ifam_index < nindex2ifc)
|
|
|
|
ifcp = index2ifc[ifam->ifam_index];
|
|
|
|
else
|
|
|
|
ifcp = NULL;
|
|
|
|
if (!ifcp) {
|
|
|
|
trace(1, "\tinvalid ifam_index %d, ignored\n",
|
|
|
|
ifam->ifam_index);
|
|
|
|
break;
|
|
|
|
}
|
2001-06-11 12:39:29 +00:00
|
|
|
if (!rt_deladdr(ifcp, rta[RTAX_IFA], rta[RTAX_NETMASK]))
|
|
|
|
iface++;
|
1999-12-28 02:37:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iface) {
|
|
|
|
trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
|
|
|
|
ifconfig();
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
|
2001-06-11 12:39:29 +00:00
|
|
|
if (ifcp->ifc_cflags & IFC_CHANGED) {
|
|
|
|
if (ifrt(ifcp, 1)) {
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
|
2001-06-11 12:39:29 +00:00
|
|
|
if (ifcp->ifc_index == ic->ifc_index)
|
|
|
|
continue;
|
|
|
|
if (ic->ifc_flags & IFF_UP)
|
|
|
|
ripsend(ic, &ic->ifc_ripsin,
|
|
|
|
RRTF_CHANGED);
|
|
|
|
}
|
|
|
|
/* Reset the flag */
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
|
2001-06-11 12:39:29 +00:00
|
|
|
rrt->rrt_rflags &= ~RRTF_CHANGED;
|
2012-11-18 15:37:27 +00:00
|
|
|
}
|
2001-06-11 12:39:29 +00:00
|
|
|
}
|
|
|
|
ifcp->ifc_cflags &= ~IFC_CHANGED;
|
|
|
|
}
|
2012-11-18 15:37:27 +00:00
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
if (rtable) {
|
|
|
|
trace(1, "rtsock: read routing table again\n");
|
|
|
|
krtread(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* remove specified route from the internal routing table.
|
|
|
|
*/
|
2017-01-13 06:22:49 +00:00
|
|
|
static int
|
2012-11-18 15:37:27 +00:00
|
|
|
rt_del(const struct sockaddr_in6 *sdst,
|
|
|
|
const struct sockaddr_in6 *sgw,
|
|
|
|
const struct sockaddr_in6 *smask)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
const struct in6_addr *dst = NULL;
|
|
|
|
const struct in6_addr *gw = NULL;
|
|
|
|
int prefix;
|
|
|
|
struct netinfo6 ni6;
|
|
|
|
struct riprt *rrt = NULL;
|
|
|
|
time_t t_lifetime;
|
|
|
|
|
|
|
|
if (sdst->sin6_family != AF_INET6) {
|
|
|
|
trace(1, "\tother AF, ignored\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (IN6_IS_ADDR_LINKLOCAL(&sdst->sin6_addr)
|
|
|
|
|| IN6_ARE_ADDR_EQUAL(&sdst->sin6_addr, &in6addr_loopback)
|
|
|
|
|| IN6_IS_ADDR_MULTICAST(&sdst->sin6_addr)) {
|
|
|
|
trace(1, "\taddress %s not interesting, ignored\n",
|
|
|
|
inet6_n2p(&sdst->sin6_addr));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
dst = &sdst->sin6_addr;
|
2001-06-11 12:39:29 +00:00
|
|
|
if (sgw->sin6_family == AF_INET6) {
|
1999-12-28 02:37:14 +00:00
|
|
|
/* easy case */
|
|
|
|
gw = &sgw->sin6_addr;
|
2001-06-11 12:39:29 +00:00
|
|
|
prefix = sin6mask2len(smask);
|
1999-12-28 02:37:14 +00:00
|
|
|
} else if (sgw->sin6_family == AF_LINK) {
|
|
|
|
/*
|
|
|
|
* Interface route... a hard case. We need to get the prefix
|
|
|
|
* length from the kernel, but we now are parsing rtmsg.
|
|
|
|
* We'll purge matching routes from my list, then get the
|
|
|
|
* fresh list.
|
|
|
|
*/
|
|
|
|
struct riprt *longest;
|
2003-01-01 18:49:04 +00:00
|
|
|
trace(1, "\t%s is an interface route, guessing prefixlen\n",
|
1999-12-28 02:37:14 +00:00
|
|
|
inet6_n2p(dst));
|
|
|
|
longest = NULL;
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
|
1999-12-28 02:37:14 +00:00
|
|
|
if (IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
|
|
|
|
&sdst->sin6_addr)
|
|
|
|
&& IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)) {
|
|
|
|
if (!longest
|
|
|
|
|| longest->rrt_info.rip6_plen <
|
|
|
|
rrt->rrt_info.rip6_plen) {
|
|
|
|
longest = rrt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rrt = longest;
|
|
|
|
if (!rrt) {
|
|
|
|
trace(1, "\tno matching interface route found\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
gw = &in6addr_loopback;
|
|
|
|
prefix = rrt->rrt_info.rip6_plen;
|
|
|
|
} else {
|
2001-06-11 12:39:29 +00:00
|
|
|
trace(1, "\tunsupported af: (gw=%d)\n", sgw->sin6_family);
|
1999-12-28 02:37:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace(1, "\tdeleting %s/%d ", inet6_n2p(dst), prefix);
|
|
|
|
trace(1, "gw %s\n", inet6_n2p(gw));
|
|
|
|
t_lifetime = time(NULL) - RIP_LIFETIME;
|
|
|
|
/* age route for interface address */
|
|
|
|
memset(&ni6, 0, sizeof(ni6));
|
|
|
|
ni6.rip6_dest = *dst;
|
|
|
|
ni6.rip6_plen = prefix;
|
|
|
|
applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
|
|
|
|
trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6.rip6_dest),
|
|
|
|
ni6.rip6_plen);
|
2012-11-18 15:37:27 +00:00
|
|
|
if (!rrt && (rrt = rtsearch(&ni6)) == NULL) {
|
1999-12-28 02:37:14 +00:00
|
|
|
trace(1, "\tno route found\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2001-06-11 12:39:29 +00:00
|
|
|
#if 0
|
1999-12-28 02:37:14 +00:00
|
|
|
if ((rrt->rrt_flags & RTF_STATIC) == 0) {
|
|
|
|
trace(1, "\tyou can delete static routes only\n");
|
2001-06-11 12:39:29 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
if (!IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, gw)) {
|
1999-12-28 02:37:14 +00:00
|
|
|
trace(1, "\tgw mismatch: %s <-> ",
|
|
|
|
inet6_n2p(&rrt->rrt_gw));
|
|
|
|
trace(1, "%s\n", inet6_n2p(gw));
|
|
|
|
} else {
|
|
|
|
trace(1, "\troute found, age it\n");
|
|
|
|
if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
|
|
|
|
rrt->rrt_t = t_lifetime;
|
|
|
|
rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* remove specified address from internal interface/routing table.
|
|
|
|
*/
|
2017-01-13 06:22:49 +00:00
|
|
|
static int
|
2012-11-18 15:37:27 +00:00
|
|
|
rt_deladdr(struct ifc *ifcp,
|
|
|
|
const struct sockaddr_in6 *sifa,
|
|
|
|
const struct sockaddr_in6 *smask)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
const struct in6_addr *addr = NULL;
|
|
|
|
int prefix;
|
2012-11-18 15:37:27 +00:00
|
|
|
struct ifac *ifac = NULL;
|
1999-12-28 02:37:14 +00:00
|
|
|
struct netinfo6 ni6;
|
|
|
|
struct riprt *rrt = NULL;
|
|
|
|
time_t t_lifetime;
|
|
|
|
int updated = 0;
|
|
|
|
|
2001-06-11 12:39:29 +00:00
|
|
|
if (sifa->sin6_family != AF_INET6) {
|
1999-12-28 02:37:14 +00:00
|
|
|
trace(1, "\tother AF, ignored\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
addr = &sifa->sin6_addr;
|
2001-06-11 12:39:29 +00:00
|
|
|
prefix = sin6mask2len(smask);
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
trace(1, "\tdeleting %s/%d from %s\n",
|
|
|
|
inet6_n2p(addr), prefix, ifcp->ifc_name);
|
2012-11-18 15:37:27 +00:00
|
|
|
ifac = ifa_match(ifcp, addr, prefix);
|
|
|
|
if (!ifac) {
|
1999-12-28 02:37:14 +00:00
|
|
|
trace(1, "\tno matching ifa found for %s/%d on %s\n",
|
|
|
|
inet6_n2p(addr), prefix, ifcp->ifc_name);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-18 15:37:27 +00:00
|
|
|
if (ifac->ifac_ifc != ifcp) {
|
1999-12-28 02:37:14 +00:00
|
|
|
trace(1, "\taddress table corrupt: back pointer does not match "
|
|
|
|
"(%s != %s)\n",
|
2012-11-18 15:37:27 +00:00
|
|
|
ifcp->ifc_name, ifac->ifac_ifc->ifc_name);
|
1999-12-28 02:37:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_REMOVE(&ifcp->ifc_ifac_head, ifac, ifac_next);
|
1999-12-28 02:37:14 +00:00
|
|
|
t_lifetime = time(NULL) - RIP_LIFETIME;
|
|
|
|
/* age route for interface address */
|
|
|
|
memset(&ni6, 0, sizeof(ni6));
|
2012-11-18 15:37:27 +00:00
|
|
|
ni6.rip6_dest = ifac->ifac_addr;
|
|
|
|
ni6.rip6_plen = ifac->ifac_plen;
|
1999-12-28 02:37:14 +00:00
|
|
|
applyplen(&ni6.rip6_dest, ni6.rip6_plen);
|
|
|
|
trace(1, "\tfind interface route %s/%d on %d\n",
|
|
|
|
inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen, ifcp->ifc_index);
|
2012-11-18 15:37:27 +00:00
|
|
|
if ((rrt = rtsearch(&ni6)) != NULL) {
|
1999-12-28 02:37:14 +00:00
|
|
|
struct in6_addr none;
|
|
|
|
memset(&none, 0, sizeof(none));
|
2001-06-11 12:39:29 +00:00
|
|
|
if (rrt->rrt_index == ifcp->ifc_index &&
|
|
|
|
(IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &none) ||
|
|
|
|
IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw))) {
|
1999-12-28 02:37:14 +00:00
|
|
|
trace(1, "\troute found, age it\n");
|
|
|
|
if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
|
|
|
|
rrt->rrt_t = t_lifetime;
|
|
|
|
rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
|
|
|
|
}
|
|
|
|
updated++;
|
|
|
|
} else {
|
|
|
|
trace(1, "\tnon-interface route found: %s/%d on %d\n",
|
|
|
|
inet6_n2p(&rrt->rrt_info.rip6_dest),
|
|
|
|
rrt->rrt_info.rip6_plen,
|
|
|
|
rrt->rrt_index);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
trace(1, "\tno interface route found\n");
|
|
|
|
/* age route for p2p destination */
|
|
|
|
if (ifcp->ifc_flags & IFF_POINTOPOINT) {
|
|
|
|
memset(&ni6, 0, sizeof(ni6));
|
2012-11-18 15:37:27 +00:00
|
|
|
ni6.rip6_dest = ifac->ifac_raddr;
|
1999-12-28 02:37:14 +00:00
|
|
|
ni6.rip6_plen = 128;
|
|
|
|
applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
|
|
|
|
trace(1, "\tfind p2p route %s/%d on %d\n",
|
|
|
|
inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen,
|
|
|
|
ifcp->ifc_index);
|
2012-11-18 15:37:27 +00:00
|
|
|
if ((rrt = rtsearch(&ni6)) != NULL) {
|
2001-06-11 12:39:29 +00:00
|
|
|
if (rrt->rrt_index == ifcp->ifc_index &&
|
2012-11-18 15:37:27 +00:00
|
|
|
IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw,
|
|
|
|
&ifac->ifac_addr)) {
|
1999-12-28 02:37:14 +00:00
|
|
|
trace(1, "\troute found, age it\n");
|
|
|
|
if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
|
|
|
|
rrt->rrt_t = t_lifetime;
|
|
|
|
rrt->rrt_info.rip6_metric =
|
2001-06-11 12:39:29 +00:00
|
|
|
HOPCNT_INFINITY6;
|
1999-12-28 02:37:14 +00:00
|
|
|
updated++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
trace(1, "\tnon-p2p route found: %s/%d on %d\n",
|
|
|
|
inet6_n2p(&rrt->rrt_info.rip6_dest),
|
|
|
|
rrt->rrt_info.rip6_plen,
|
|
|
|
rrt->rrt_index);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
trace(1, "\tno p2p route found\n");
|
|
|
|
}
|
2012-11-18 15:37:27 +00:00
|
|
|
free(ifac);
|
|
|
|
|
|
|
|
return ((updated) ? 0 : -1);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get each interface address and put those interface routes to the route
|
|
|
|
* list.
|
|
|
|
*/
|
2017-01-13 06:22:49 +00:00
|
|
|
static int
|
2012-11-18 15:37:27 +00:00
|
|
|
ifrt(struct ifc *ifcp, int again)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
2012-11-18 15:37:27 +00:00
|
|
|
struct ifac *ifac;
|
|
|
|
struct riprt *rrt = NULL, *search_rrt, *loop_rrt;
|
2000-07-05 02:14:16 +00:00
|
|
|
struct netinfo6 *np;
|
2001-06-11 12:39:29 +00:00
|
|
|
time_t t_lifetime;
|
|
|
|
int need_trigger = 0;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2003-11-14 17:16:50 +00:00
|
|
|
#if 0
|
1999-12-28 02:37:14 +00:00
|
|
|
if (ifcp->ifc_flags & IFF_LOOPBACK)
|
2001-06-11 12:39:29 +00:00
|
|
|
return 0; /* ignore loopback */
|
2003-11-14 17:16:50 +00:00
|
|
|
#endif
|
|
|
|
|
2000-07-05 02:14:16 +00:00
|
|
|
if (ifcp->ifc_flags & IFF_POINTOPOINT) {
|
|
|
|
ifrt_p2p(ifcp, again);
|
2001-06-11 12:39:29 +00:00
|
|
|
return 0;
|
2000-07-05 02:14:16 +00:00
|
|
|
}
|
|
|
|
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
|
|
|
|
if (IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
|
2000-07-05 02:14:16 +00:00
|
|
|
#if 0
|
|
|
|
trace(1, "route: %s on %s: "
|
|
|
|
"skip linklocal interface address\n",
|
2012-11-18 15:37:27 +00:00
|
|
|
inet6_n2p(&ifac->ifac_addr), ifcp->ifc_name);
|
2000-07-05 02:14:16 +00:00
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
2012-11-18 15:37:27 +00:00
|
|
|
if (IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_addr)) {
|
2000-07-05 02:14:16 +00:00
|
|
|
#if 0
|
|
|
|
trace(1, "route: %s: skip unspec interface address\n",
|
|
|
|
ifcp->ifc_name);
|
2003-11-14 17:16:50 +00:00
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
2012-11-18 15:37:27 +00:00
|
|
|
if (IN6_IS_ADDR_LOOPBACK(&ifac->ifac_addr)) {
|
2003-11-14 17:16:50 +00:00
|
|
|
#if 0
|
|
|
|
trace(1, "route: %s: skip loopback address\n",
|
|
|
|
ifcp->ifc_name);
|
2000-07-05 02:14:16 +00:00
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
2001-06-11 12:39:29 +00:00
|
|
|
if (ifcp->ifc_flags & IFF_UP) {
|
|
|
|
if ((rrt = MALLOC(struct riprt)) == NULL)
|
|
|
|
fatal("malloc: struct riprt");
|
|
|
|
memset(rrt, 0, sizeof(*rrt));
|
|
|
|
rrt->rrt_same = NULL;
|
|
|
|
rrt->rrt_index = ifcp->ifc_index;
|
|
|
|
rrt->rrt_t = 0; /* don't age */
|
2012-11-18 15:37:27 +00:00
|
|
|
rrt->rrt_info.rip6_dest = ifac->ifac_addr;
|
2001-06-11 12:39:29 +00:00
|
|
|
rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
|
|
|
|
rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
|
2012-11-18 15:37:27 +00:00
|
|
|
rrt->rrt_info.rip6_plen = ifac->ifac_plen;
|
This main goals of this project are:
1. separating L2 tables (ARP, NDP) from the L3 routing tables
2. removing as much locking dependencies among these layers as
possible to allow for some parallelism in the search operations
3. simplify the logic in the routing code,
The most notable end result is the obsolescent of the route
cloning (RTF_CLONING) concept, which translated into code reduction
in both IPv4 ARP and IPv6 NDP related modules, and size reduction in
struct rtentry{}. The change in design obsoletes the semantics of
RTF_CLONING, RTF_WASCLONE and RTF_LLINFO routing flags. The userland
applications such as "arp" and "ndp" have been modified to reflect
those changes. The output from "netstat -r" shows only the routing
entries.
Quite a few developers have contributed to this project in the
past: Glebius Smirnoff, Luigi Rizzo, Alessandro Cerri, and
Andre Oppermann. And most recently:
- Kip Macy revised the locking code completely, thus completing
the last piece of the puzzle, Kip has also been conducting
active functional testing
- Sam Leffler has helped me improving/refactoring the code, and
provided valuable reviews
- Julian Elischer setup the perforce tree for me and has helped
me maintaining that branch before the svn conversion
2008-12-15 06:10:57 +00:00
|
|
|
rrt->rrt_flags = RTF_HOST;
|
2001-06-11 12:39:29 +00:00
|
|
|
rrt->rrt_rflags |= RRTF_CHANGED;
|
2012-11-18 15:37:27 +00:00
|
|
|
applyplen(&rrt->rrt_info.rip6_dest, ifac->ifac_plen);
|
2001-06-11 12:39:29 +00:00
|
|
|
memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
|
2012-11-18 15:37:27 +00:00
|
|
|
rrt->rrt_gw = ifac->ifac_addr;
|
2001-06-11 12:39:29 +00:00
|
|
|
np = &rrt->rrt_info;
|
2012-11-18 15:37:27 +00:00
|
|
|
search_rrt = rtsearch(np);
|
2001-06-11 12:39:29 +00:00
|
|
|
if (search_rrt != NULL) {
|
2003-08-17 18:12:11 +00:00
|
|
|
if (search_rrt->rrt_info.rip6_metric <=
|
2001-06-11 12:39:29 +00:00
|
|
|
rrt->rrt_info.rip6_metric) {
|
|
|
|
/* Already have better route */
|
|
|
|
if (!again) {
|
|
|
|
trace(1, "route: %s/%d: "
|
|
|
|
"already registered (%s)\n",
|
|
|
|
inet6_n2p(&np->rip6_dest), np->rip6_plen,
|
|
|
|
ifcp->ifc_name);
|
|
|
|
}
|
2003-08-17 18:12:11 +00:00
|
|
|
goto next;
|
2001-06-11 12:39:29 +00:00
|
|
|
}
|
2003-08-17 18:12:11 +00:00
|
|
|
|
2018-08-08 20:15:40 +00:00
|
|
|
TAILQ_REMOVE(&riprt_head, search_rrt, rrt_next);
|
|
|
|
delroute(&search_rrt->rrt_info,
|
|
|
|
&search_rrt->rrt_gw);
|
|
|
|
free(search_rrt);
|
2001-06-11 12:39:29 +00:00
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
/* Attach the route to the list */
|
2000-07-05 02:14:16 +00:00
|
|
|
trace(1, "route: %s/%d: register route (%s)\n",
|
|
|
|
inet6_n2p(&np->rip6_dest), np->rip6_plen,
|
|
|
|
ifcp->ifc_name);
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
|
2001-06-11 12:39:29 +00:00
|
|
|
addroute(rrt, &rrt->rrt_gw, ifcp);
|
2003-08-17 18:12:11 +00:00
|
|
|
rrt = NULL;
|
2001-06-11 12:39:29 +00:00
|
|
|
sendrequest(ifcp);
|
|
|
|
ripsend(ifcp, &ifcp->ifc_ripsin, 0);
|
|
|
|
need_trigger = 1;
|
1999-12-28 02:37:14 +00:00
|
|
|
} else {
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_FOREACH(loop_rrt, &riprt_head, rrt_next) {
|
2001-06-11 12:39:29 +00:00
|
|
|
if (loop_rrt->rrt_index == ifcp->ifc_index) {
|
|
|
|
t_lifetime = time(NULL) - RIP_LIFETIME;
|
|
|
|
if (loop_rrt->rrt_t == 0 || loop_rrt->rrt_t > t_lifetime) {
|
|
|
|
loop_rrt->rrt_t = t_lifetime;
|
|
|
|
loop_rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
|
|
|
|
loop_rrt->rrt_rflags |= RRTF_CHANGED;
|
|
|
|
need_trigger = 1;
|
|
|
|
}
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
2001-06-11 12:39:29 +00:00
|
|
|
}
|
2003-08-17 18:12:11 +00:00
|
|
|
next:
|
|
|
|
if (rrt)
|
|
|
|
free(rrt);
|
2000-07-05 02:14:16 +00:00
|
|
|
}
|
2001-06-11 12:39:29 +00:00
|
|
|
return need_trigger;
|
2000-07-05 02:14:16 +00:00
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2000-07-05 02:14:16 +00:00
|
|
|
/*
|
|
|
|
* there are couple of p2p interface routing models. "behavior" lets
|
|
|
|
* you pick one. it looks that gated behavior fits best with BSDs,
|
2003-08-17 16:26:35 +00:00
|
|
|
* since BSD kernels do not look at prefix length on p2p interfaces.
|
2000-07-05 02:14:16 +00:00
|
|
|
*/
|
2017-01-13 06:22:49 +00:00
|
|
|
static void
|
2012-11-18 15:37:27 +00:00
|
|
|
ifrt_p2p(struct ifc *ifcp, int again)
|
2000-07-05 02:14:16 +00:00
|
|
|
{
|
2012-11-18 15:37:27 +00:00
|
|
|
struct ifac *ifac;
|
|
|
|
struct riprt *rrt, *orrt;
|
2000-07-05 02:14:16 +00:00
|
|
|
struct netinfo6 *np;
|
|
|
|
struct in6_addr addr, dest;
|
|
|
|
int advert, ignore, i;
|
|
|
|
#define P2PADVERT_NETWORK 1
|
|
|
|
#define P2PADVERT_ADDR 2
|
|
|
|
#define P2PADVERT_DEST 4
|
|
|
|
#define P2PADVERT_MAX 4
|
|
|
|
const enum { CISCO, GATED, ROUTE6D } behavior = GATED;
|
2001-06-11 12:39:29 +00:00
|
|
|
const char *category = "";
|
2000-07-05 02:14:16 +00:00
|
|
|
const char *noadv;
|
|
|
|
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
|
|
|
|
addr = ifac->ifac_addr;
|
|
|
|
dest = ifac->ifac_raddr;
|
|
|
|
applyplen(&addr, ifac->ifac_plen);
|
|
|
|
applyplen(&dest, ifac->ifac_plen);
|
2000-07-05 02:14:16 +00:00
|
|
|
advert = ignore = 0;
|
|
|
|
switch (behavior) {
|
|
|
|
case CISCO:
|
|
|
|
/*
|
|
|
|
* honor addr/plen, just like normal shared medium
|
|
|
|
* interface. this may cause trouble if you reuse
|
|
|
|
* addr/plen on other interfaces.
|
|
|
|
*
|
|
|
|
* advertise addr/plen.
|
|
|
|
*/
|
|
|
|
advert |= P2PADVERT_NETWORK;
|
|
|
|
break;
|
|
|
|
case GATED:
|
|
|
|
/*
|
|
|
|
* prefixlen on p2p interface is meaningless.
|
|
|
|
* advertise addr/128 and dest/128.
|
|
|
|
*
|
|
|
|
* do not install network route to route6d routing
|
|
|
|
* table (if we do, it would prevent route installation
|
|
|
|
* for other p2p interface that shares addr/plen).
|
2001-06-11 12:39:29 +00:00
|
|
|
*
|
|
|
|
* XXX what should we do if dest is ::? it will not
|
|
|
|
* get announced anyways (see following filter),
|
|
|
|
* but we need to think.
|
2000-07-05 02:14:16 +00:00
|
|
|
*/
|
|
|
|
advert |= P2PADVERT_ADDR;
|
|
|
|
advert |= P2PADVERT_DEST;
|
|
|
|
ignore |= P2PADVERT_NETWORK;
|
|
|
|
break;
|
|
|
|
case ROUTE6D:
|
|
|
|
/*
|
2001-06-11 12:39:29 +00:00
|
|
|
* just for testing. actually the code is redundant
|
|
|
|
* given the current p2p interface address assignment
|
|
|
|
* rule for kame kernel.
|
|
|
|
*
|
|
|
|
* intent:
|
|
|
|
* A/n -> announce A/n
|
|
|
|
* A B/n, A and B share prefix -> A/n (= B/n)
|
|
|
|
* A B/n, do not share prefix -> A/128 and B/128
|
|
|
|
* actually, A/64 and A B/128 are the only cases
|
|
|
|
* permitted by the kernel:
|
|
|
|
* A/64 -> A/64
|
|
|
|
* A B/128 -> A/128 and B/128
|
2000-07-05 02:14:16 +00:00
|
|
|
*/
|
2012-11-18 15:37:27 +00:00
|
|
|
if (!IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_raddr)) {
|
2001-06-11 12:39:29 +00:00
|
|
|
if (IN6_ARE_ADDR_EQUAL(&addr, &dest))
|
|
|
|
advert |= P2PADVERT_NETWORK;
|
|
|
|
else {
|
|
|
|
advert |= P2PADVERT_ADDR;
|
|
|
|
advert |= P2PADVERT_DEST;
|
|
|
|
ignore |= P2PADVERT_NETWORK;
|
|
|
|
}
|
|
|
|
} else
|
2000-07-05 02:14:16 +00:00
|
|
|
advert |= P2PADVERT_NETWORK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i <= P2PADVERT_MAX; i *= 2) {
|
|
|
|
if ((ignore & i) != 0)
|
|
|
|
continue;
|
2001-06-11 12:39:29 +00:00
|
|
|
if ((rrt = MALLOC(struct riprt)) == NULL) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("malloc: struct riprt");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2000-07-05 02:14:16 +00:00
|
|
|
memset(rrt, 0, sizeof(*rrt));
|
1999-12-28 02:37:14 +00:00
|
|
|
rrt->rrt_same = NULL;
|
|
|
|
rrt->rrt_index = ifcp->ifc_index;
|
2000-07-05 02:14:16 +00:00
|
|
|
rrt->rrt_t = 0; /* don't age */
|
|
|
|
switch (i) {
|
|
|
|
case P2PADVERT_NETWORK:
|
2012-11-18 15:37:27 +00:00
|
|
|
rrt->rrt_info.rip6_dest = ifac->ifac_addr;
|
|
|
|
rrt->rrt_info.rip6_plen = ifac->ifac_plen;
|
2000-07-05 02:14:16 +00:00
|
|
|
applyplen(&rrt->rrt_info.rip6_dest,
|
2012-11-18 15:37:27 +00:00
|
|
|
ifac->ifac_plen);
|
2000-07-05 02:14:16 +00:00
|
|
|
category = "network";
|
|
|
|
break;
|
|
|
|
case P2PADVERT_ADDR:
|
2012-11-18 15:37:27 +00:00
|
|
|
rrt->rrt_info.rip6_dest = ifac->ifac_addr;
|
2000-07-05 02:14:16 +00:00
|
|
|
rrt->rrt_info.rip6_plen = 128;
|
2001-06-11 12:39:29 +00:00
|
|
|
rrt->rrt_gw = in6addr_loopback;
|
2000-07-05 02:14:16 +00:00
|
|
|
category = "addr";
|
|
|
|
break;
|
|
|
|
case P2PADVERT_DEST:
|
2012-11-18 15:37:27 +00:00
|
|
|
rrt->rrt_info.rip6_dest = ifac->ifac_raddr;
|
2000-07-05 02:14:16 +00:00
|
|
|
rrt->rrt_info.rip6_plen = 128;
|
2012-11-18 15:37:27 +00:00
|
|
|
rrt->rrt_gw = ifac->ifac_addr;
|
2000-07-05 02:14:16 +00:00
|
|
|
category = "dest";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_info.rip6_dest) ||
|
|
|
|
IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_info.rip6_dest)) {
|
|
|
|
#if 0
|
|
|
|
trace(1, "route: %s: skip unspec/linklocal "
|
|
|
|
"(%s on %s)\n", category, ifcp->ifc_name);
|
|
|
|
#endif
|
|
|
|
free(rrt);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((advert & i) == 0) {
|
|
|
|
rrt->rrt_rflags |= RRTF_NOADVERTISE;
|
|
|
|
noadv = ", NO-ADV";
|
|
|
|
} else
|
|
|
|
noadv = "";
|
1999-12-28 02:37:14 +00:00
|
|
|
rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
|
2000-07-05 02:14:16 +00:00
|
|
|
rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
|
1999-12-28 02:37:14 +00:00
|
|
|
np = &rrt->rrt_info;
|
2012-11-18 15:37:27 +00:00
|
|
|
orrt = rtsearch(np);
|
2001-06-11 12:39:29 +00:00
|
|
|
if (!orrt) {
|
1999-12-28 02:37:14 +00:00
|
|
|
/* Attach the route to the list */
|
2000-07-05 02:14:16 +00:00
|
|
|
trace(1, "route: %s/%d: register route "
|
|
|
|
"(%s on %s%s)\n",
|
|
|
|
inet6_n2p(&np->rip6_dest), np->rip6_plen,
|
|
|
|
category, ifcp->ifc_name, noadv);
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
|
2001-06-11 12:39:29 +00:00
|
|
|
} else if (rrt->rrt_index != orrt->rrt_index ||
|
|
|
|
rrt->rrt_info.rip6_metric != orrt->rrt_info.rip6_metric) {
|
2012-11-18 15:37:27 +00:00
|
|
|
/* replace route */
|
|
|
|
TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
|
|
|
|
TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
|
2001-06-11 12:39:29 +00:00
|
|
|
free(orrt);
|
|
|
|
|
|
|
|
trace(1, "route: %s/%d: update (%s on %s%s)\n",
|
|
|
|
inet6_n2p(&np->rip6_dest), np->rip6_plen,
|
|
|
|
category, ifcp->ifc_name, noadv);
|
1999-12-28 02:37:14 +00:00
|
|
|
} else {
|
|
|
|
/* Already found */
|
|
|
|
if (!again) {
|
2000-07-05 02:14:16 +00:00
|
|
|
trace(1, "route: %s/%d: "
|
|
|
|
"already registered (%s on %s%s)\n",
|
|
|
|
inet6_n2p(&np->rip6_dest),
|
|
|
|
np->rip6_plen, category,
|
|
|
|
ifcp->ifc_name, noadv);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
free(rrt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-07-05 02:14:16 +00:00
|
|
|
#undef P2PADVERT_NETWORK
|
|
|
|
#undef P2PADVERT_ADDR
|
|
|
|
#undef P2PADVERT_DEST
|
|
|
|
#undef P2PADVERT_MAX
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static int
|
2012-11-18 15:37:27 +00:00
|
|
|
getifmtu(int ifindex)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
int mib[6];
|
|
|
|
char *buf;
|
|
|
|
size_t msize;
|
|
|
|
struct if_msghdr *ifm;
|
|
|
|
int mtu;
|
|
|
|
|
|
|
|
mib[0] = CTL_NET;
|
|
|
|
mib[1] = PF_ROUTE;
|
|
|
|
mib[2] = 0;
|
|
|
|
mib[3] = AF_INET6;
|
|
|
|
mib[4] = NET_RT_IFLIST;
|
|
|
|
mib[5] = ifindex;
|
2017-01-09 00:38:19 +00:00
|
|
|
if (sysctl(mib, nitems(mib), NULL, &msize, NULL, 0) < 0) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("sysctl estimate NET_RT_IFLIST");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
if ((buf = malloc(msize)) == NULL) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("malloc");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2017-01-09 00:38:19 +00:00
|
|
|
if (sysctl(mib, nitems(mib), buf, &msize, NULL, 0) < 0) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("sysctl NET_RT_IFLIST");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2017-01-13 06:22:49 +00:00
|
|
|
ifm = (struct if_msghdr *)(void *)buf;
|
1999-12-28 02:37:14 +00:00
|
|
|
mtu = ifm->ifm_data.ifi_mtu;
|
2001-06-11 12:39:29 +00:00
|
|
|
if (ifindex != ifm->ifm_index) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("ifindex does not match with ifm_index");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
free(buf);
|
|
|
|
return mtu;
|
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static const char *
|
2012-11-18 15:37:27 +00:00
|
|
|
rttypes(struct rt_msghdr *rtm)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
2000-07-05 02:14:16 +00:00
|
|
|
#define RTTYPE(s, f) \
|
|
|
|
do { \
|
|
|
|
if (rtm->rtm_type == (f)) \
|
|
|
|
return (s); \
|
|
|
|
} while (0)
|
1999-12-28 02:37:14 +00:00
|
|
|
RTTYPE("ADD", RTM_ADD);
|
|
|
|
RTTYPE("DELETE", RTM_DELETE);
|
|
|
|
RTTYPE("CHANGE", RTM_CHANGE);
|
|
|
|
RTTYPE("GET", RTM_GET);
|
|
|
|
RTTYPE("LOSING", RTM_LOSING);
|
|
|
|
RTTYPE("REDIRECT", RTM_REDIRECT);
|
|
|
|
RTTYPE("MISS", RTM_MISS);
|
|
|
|
RTTYPE("LOCK", RTM_LOCK);
|
|
|
|
RTTYPE("NEWADDR", RTM_NEWADDR);
|
|
|
|
RTTYPE("DELADDR", RTM_DELADDR);
|
|
|
|
RTTYPE("IFINFO", RTM_IFINFO);
|
2001-06-11 12:39:29 +00:00
|
|
|
#ifdef RTM_OIFINFO
|
|
|
|
RTTYPE("OIFINFO", RTM_OIFINFO);
|
|
|
|
#endif
|
|
|
|
#ifdef RTM_IFANNOUNCE
|
|
|
|
RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE);
|
|
|
|
#endif
|
|
|
|
#ifdef RTM_NEWMADDR
|
|
|
|
RTTYPE("NEWMADDR", RTM_NEWMADDR);
|
|
|
|
#endif
|
|
|
|
#ifdef RTM_DELMADDR
|
|
|
|
RTTYPE("DELMADDR", RTM_DELMADDR);
|
|
|
|
#endif
|
1999-12-28 02:37:14 +00:00
|
|
|
#undef RTTYPE
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static const char *
|
2012-11-18 15:37:27 +00:00
|
|
|
rtflags(struct rt_msghdr *rtm)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
static char buf[BUFSIZ];
|
|
|
|
|
2001-06-11 12:39:29 +00:00
|
|
|
/*
|
|
|
|
* letter conflict should be okay. painful when *BSD diverges...
|
|
|
|
*/
|
|
|
|
strlcpy(buf, "", sizeof(buf));
|
2000-07-05 02:14:16 +00:00
|
|
|
#define RTFLAG(s, f) \
|
|
|
|
do { \
|
|
|
|
if (rtm->rtm_flags & (f)) \
|
2001-06-11 12:39:29 +00:00
|
|
|
strlcat(buf, (s), sizeof(buf)); \
|
2000-07-05 02:14:16 +00:00
|
|
|
} while (0)
|
1999-12-28 02:37:14 +00:00
|
|
|
RTFLAG("U", RTF_UP);
|
|
|
|
RTFLAG("G", RTF_GATEWAY);
|
|
|
|
RTFLAG("H", RTF_HOST);
|
|
|
|
RTFLAG("R", RTF_REJECT);
|
|
|
|
RTFLAG("D", RTF_DYNAMIC);
|
|
|
|
RTFLAG("M", RTF_MODIFIED);
|
|
|
|
RTFLAG("d", RTF_DONE);
|
|
|
|
#ifdef RTF_MASK
|
|
|
|
RTFLAG("m", RTF_MASK);
|
|
|
|
#endif
|
2001-06-11 12:39:29 +00:00
|
|
|
#ifdef RTF_CLONED
|
|
|
|
RTFLAG("c", RTF_CLONED);
|
|
|
|
#endif
|
1999-12-28 02:37:14 +00:00
|
|
|
RTFLAG("X", RTF_XRESOLVE);
|
This main goals of this project are:
1. separating L2 tables (ARP, NDP) from the L3 routing tables
2. removing as much locking dependencies among these layers as
possible to allow for some parallelism in the search operations
3. simplify the logic in the routing code,
The most notable end result is the obsolescent of the route
cloning (RTF_CLONING) concept, which translated into code reduction
in both IPv4 ARP and IPv6 NDP related modules, and size reduction in
struct rtentry{}. The change in design obsoletes the semantics of
RTF_CLONING, RTF_WASCLONE and RTF_LLINFO routing flags. The userland
applications such as "arp" and "ndp" have been modified to reflect
those changes. The output from "netstat -r" shows only the routing
entries.
Quite a few developers have contributed to this project in the
past: Glebius Smirnoff, Luigi Rizzo, Alessandro Cerri, and
Andre Oppermann. And most recently:
- Kip Macy revised the locking code completely, thus completing
the last piece of the puzzle, Kip has also been conducting
active functional testing
- Sam Leffler has helped me improving/refactoring the code, and
provided valuable reviews
- Julian Elischer setup the perforce tree for me and has helped
me maintaining that branch before the svn conversion
2008-12-15 06:10:57 +00:00
|
|
|
#ifdef RTF_LLINFO
|
1999-12-28 02:37:14 +00:00
|
|
|
RTFLAG("L", RTF_LLINFO);
|
This main goals of this project are:
1. separating L2 tables (ARP, NDP) from the L3 routing tables
2. removing as much locking dependencies among these layers as
possible to allow for some parallelism in the search operations
3. simplify the logic in the routing code,
The most notable end result is the obsolescent of the route
cloning (RTF_CLONING) concept, which translated into code reduction
in both IPv4 ARP and IPv6 NDP related modules, and size reduction in
struct rtentry{}. The change in design obsoletes the semantics of
RTF_CLONING, RTF_WASCLONE and RTF_LLINFO routing flags. The userland
applications such as "arp" and "ndp" have been modified to reflect
those changes. The output from "netstat -r" shows only the routing
entries.
Quite a few developers have contributed to this project in the
past: Glebius Smirnoff, Luigi Rizzo, Alessandro Cerri, and
Andre Oppermann. And most recently:
- Kip Macy revised the locking code completely, thus completing
the last piece of the puzzle, Kip has also been conducting
active functional testing
- Sam Leffler has helped me improving/refactoring the code, and
provided valuable reviews
- Julian Elischer setup the perforce tree for me and has helped
me maintaining that branch before the svn conversion
2008-12-15 06:10:57 +00:00
|
|
|
#endif
|
1999-12-28 02:37:14 +00:00
|
|
|
RTFLAG("S", RTF_STATIC);
|
|
|
|
RTFLAG("B", RTF_BLACKHOLE);
|
2001-06-11 12:39:29 +00:00
|
|
|
#ifdef RTF_PROTO3
|
|
|
|
RTFLAG("3", RTF_PROTO3);
|
|
|
|
#endif
|
1999-12-28 02:37:14 +00:00
|
|
|
RTFLAG("2", RTF_PROTO2);
|
|
|
|
RTFLAG("1", RTF_PROTO1);
|
2001-06-11 12:39:29 +00:00
|
|
|
#ifdef RTF_BROADCAST
|
|
|
|
RTFLAG("b", RTF_BROADCAST);
|
|
|
|
#endif
|
|
|
|
#ifdef RTF_DEFAULT
|
|
|
|
RTFLAG("d", RTF_DEFAULT);
|
|
|
|
#endif
|
|
|
|
#ifdef RTF_ISAROUTER
|
|
|
|
RTFLAG("r", RTF_ISAROUTER);
|
|
|
|
#endif
|
|
|
|
#ifdef RTF_TUNNEL
|
|
|
|
RTFLAG("T", RTF_TUNNEL);
|
|
|
|
#endif
|
|
|
|
#ifdef RTF_AUTH
|
|
|
|
RTFLAG("A", RTF_AUTH);
|
|
|
|
#endif
|
|
|
|
#ifdef RTF_CRYPT
|
|
|
|
RTFLAG("E", RTF_CRYPT);
|
|
|
|
#endif
|
1999-12-28 02:37:14 +00:00
|
|
|
#undef RTFLAG
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static const char *
|
2012-11-18 15:37:27 +00:00
|
|
|
ifflags(int flags)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
static char buf[BUFSIZ];
|
|
|
|
|
2001-06-11 12:39:29 +00:00
|
|
|
strlcpy(buf, "", sizeof(buf));
|
2000-07-05 02:14:16 +00:00
|
|
|
#define IFFLAG(s, f) \
|
|
|
|
do { \
|
2003-08-17 17:47:22 +00:00
|
|
|
if (flags & (f)) { \
|
2000-07-05 02:14:16 +00:00
|
|
|
if (buf[0]) \
|
2001-06-11 12:39:29 +00:00
|
|
|
strlcat(buf, ",", sizeof(buf)); \
|
2003-08-17 17:47:22 +00:00
|
|
|
strlcat(buf, (s), sizeof(buf)); \
|
2000-07-05 02:14:16 +00:00
|
|
|
} \
|
|
|
|
} while (0)
|
1999-12-28 02:37:14 +00:00
|
|
|
IFFLAG("UP", IFF_UP);
|
|
|
|
IFFLAG("BROADCAST", IFF_BROADCAST);
|
|
|
|
IFFLAG("DEBUG", IFF_DEBUG);
|
|
|
|
IFFLAG("LOOPBACK", IFF_LOOPBACK);
|
|
|
|
IFFLAG("POINTOPOINT", IFF_POINTOPOINT);
|
|
|
|
#ifdef IFF_NOTRAILERS
|
|
|
|
IFFLAG("NOTRAILERS", IFF_NOTRAILERS);
|
2001-06-11 12:39:29 +00:00
|
|
|
#endif
|
1999-12-28 02:37:14 +00:00
|
|
|
IFFLAG("RUNNING", IFF_RUNNING);
|
|
|
|
IFFLAG("NOARP", IFF_NOARP);
|
|
|
|
IFFLAG("PROMISC", IFF_PROMISC);
|
|
|
|
IFFLAG("ALLMULTI", IFF_ALLMULTI);
|
|
|
|
IFFLAG("OACTIVE", IFF_OACTIVE);
|
|
|
|
IFFLAG("SIMPLEX", IFF_SIMPLEX);
|
|
|
|
IFFLAG("LINK0", IFF_LINK0);
|
|
|
|
IFFLAG("LINK1", IFF_LINK1);
|
|
|
|
IFFLAG("LINK2", IFF_LINK2);
|
|
|
|
IFFLAG("MULTICAST", IFF_MULTICAST);
|
|
|
|
#undef IFFLAG
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static void
|
2012-11-18 15:37:27 +00:00
|
|
|
krtread(int again)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
int mib[6];
|
|
|
|
size_t msize;
|
|
|
|
char *buf, *p, *lim;
|
|
|
|
struct rt_msghdr *rtm;
|
|
|
|
int retry;
|
|
|
|
const char *errmsg;
|
|
|
|
|
|
|
|
retry = 0;
|
|
|
|
buf = NULL;
|
|
|
|
mib[0] = CTL_NET;
|
|
|
|
mib[1] = PF_ROUTE;
|
|
|
|
mib[2] = 0;
|
|
|
|
mib[3] = AF_INET6; /* Address family */
|
|
|
|
mib[4] = NET_RT_DUMP; /* Dump the kernel routing table */
|
|
|
|
mib[5] = 0; /* No flags */
|
|
|
|
do {
|
2012-11-18 15:37:27 +00:00
|
|
|
if (retry)
|
|
|
|
sleep(1);
|
1999-12-28 02:37:14 +00:00
|
|
|
retry++;
|
|
|
|
errmsg = NULL;
|
2016-05-11 22:33:20 +00:00
|
|
|
if (buf) {
|
1999-12-28 02:37:14 +00:00
|
|
|
free(buf);
|
2016-05-11 22:33:20 +00:00
|
|
|
buf = NULL;
|
|
|
|
}
|
2017-01-09 00:38:19 +00:00
|
|
|
if (sysctl(mib, nitems(mib), NULL, &msize, NULL, 0) < 0) {
|
1999-12-28 02:37:14 +00:00
|
|
|
errmsg = "sysctl estimate";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((buf = malloc(msize)) == NULL) {
|
|
|
|
errmsg = "malloc";
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-09 00:38:19 +00:00
|
|
|
if (sysctl(mib, nitems(mib), buf, &msize, NULL, 0) < 0) {
|
1999-12-28 02:37:14 +00:00
|
|
|
errmsg = "sysctl NET_RT_DUMP";
|
|
|
|
continue;
|
|
|
|
}
|
2012-11-18 15:48:02 +00:00
|
|
|
} while (retry < RT_DUMP_MAXRETRY && errmsg != NULL);
|
2001-06-11 12:39:29 +00:00
|
|
|
if (errmsg) {
|
2000-11-27 17:44:53 +00:00
|
|
|
fatal("%s (with %d retries, msize=%lu)", errmsg, retry,
|
|
|
|
(u_long)msize);
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
} else if (1 < retry)
|
1999-12-28 02:37:14 +00:00
|
|
|
syslog(LOG_INFO, "NET_RT_DUMP %d retires", retry);
|
|
|
|
|
|
|
|
lim = buf + msize;
|
|
|
|
for (p = buf; p < lim; p += rtm->rtm_msglen) {
|
2017-01-13 06:22:49 +00:00
|
|
|
rtm = (struct rt_msghdr *)(void *)p;
|
1999-12-28 02:37:14 +00:00
|
|
|
rt_entry(rtm, again);
|
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static void
|
2012-11-18 15:37:27 +00:00
|
|
|
rt_entry(struct rt_msghdr *rtm, int again)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_in6 *sin6_dst, *sin6_gw, *sin6_mask;
|
|
|
|
struct sockaddr_in6 *sin6_genmask, *sin6_ifp;
|
|
|
|
char *rtmp, *ifname = NULL;
|
2001-06-11 12:39:29 +00:00
|
|
|
struct riprt *rrt, *orrt;
|
1999-12-28 02:37:14 +00:00
|
|
|
struct netinfo6 *np;
|
2012-11-18 15:37:27 +00:00
|
|
|
int ifindex;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
sin6_dst = sin6_gw = sin6_mask = sin6_genmask = sin6_ifp = 0;
|
|
|
|
if ((rtm->rtm_flags & RTF_UP) == 0 || rtm->rtm_flags &
|
This main goals of this project are:
1. separating L2 tables (ARP, NDP) from the L3 routing tables
2. removing as much locking dependencies among these layers as
possible to allow for some parallelism in the search operations
3. simplify the logic in the routing code,
The most notable end result is the obsolescent of the route
cloning (RTF_CLONING) concept, which translated into code reduction
in both IPv4 ARP and IPv6 NDP related modules, and size reduction in
struct rtentry{}. The change in design obsoletes the semantics of
RTF_CLONING, RTF_WASCLONE and RTF_LLINFO routing flags. The userland
applications such as "arp" and "ndp" have been modified to reflect
those changes. The output from "netstat -r" shows only the routing
entries.
Quite a few developers have contributed to this project in the
past: Glebius Smirnoff, Luigi Rizzo, Alessandro Cerri, and
Andre Oppermann. And most recently:
- Kip Macy revised the locking code completely, thus completing
the last piece of the puzzle, Kip has also been conducting
active functional testing
- Sam Leffler has helped me improving/refactoring the code, and
provided valuable reviews
- Julian Elischer setup the perforce tree for me and has helped
me maintaining that branch before the svn conversion
2008-12-15 06:10:57 +00:00
|
|
|
(RTF_XRESOLVE|RTF_BLACKHOLE)) {
|
1999-12-28 02:37:14 +00:00
|
|
|
return; /* not interested in the link route */
|
2000-07-05 02:14:16 +00:00
|
|
|
}
|
2000-11-27 17:44:53 +00:00
|
|
|
/* do not look at cloned routes */
|
|
|
|
#ifdef RTF_WASCLONED
|
|
|
|
if (rtm->rtm_flags & RTF_WASCLONED)
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
#ifdef RTF_CLONED
|
|
|
|
if (rtm->rtm_flags & RTF_CLONED)
|
|
|
|
return;
|
|
|
|
#endif
|
2012-11-18 15:48:02 +00:00
|
|
|
/* XXX: Ignore connected routes. */
|
|
|
|
if (!(rtm->rtm_flags & (RTF_GATEWAY|RTF_HOST|RTF_STATIC)))
|
|
|
|
return;
|
2000-11-27 17:44:53 +00:00
|
|
|
/*
|
|
|
|
* do not look at dynamic routes.
|
|
|
|
* netbsd/openbsd cloned routes have UGHD.
|
|
|
|
*/
|
|
|
|
if (rtm->rtm_flags & RTF_DYNAMIC)
|
|
|
|
return;
|
1999-12-28 02:37:14 +00:00
|
|
|
rtmp = (char *)(rtm + 1);
|
|
|
|
/* Destination */
|
|
|
|
if ((rtm->rtm_addrs & RTA_DST) == 0)
|
|
|
|
return; /* ignore routes without destination address */
|
2017-01-13 06:22:49 +00:00
|
|
|
sin6_dst = (struct sockaddr_in6 *)(void *)rtmp;
|
2000-08-14 04:54:58 +00:00
|
|
|
rtmp += ROUNDUP(sin6_dst->sin6_len);
|
1999-12-28 02:37:14 +00:00
|
|
|
if (rtm->rtm_addrs & RTA_GATEWAY) {
|
2017-01-13 06:22:49 +00:00
|
|
|
sin6_gw = (struct sockaddr_in6 *)(void *)rtmp;
|
1999-12-28 02:37:14 +00:00
|
|
|
rtmp += ROUNDUP(sin6_gw->sin6_len);
|
|
|
|
}
|
|
|
|
if (rtm->rtm_addrs & RTA_NETMASK) {
|
2017-01-13 06:22:49 +00:00
|
|
|
sin6_mask = (struct sockaddr_in6 *)(void *)rtmp;
|
1999-12-28 02:37:14 +00:00
|
|
|
rtmp += ROUNDUP(sin6_mask->sin6_len);
|
|
|
|
}
|
|
|
|
if (rtm->rtm_addrs & RTA_GENMASK) {
|
2017-01-13 06:22:49 +00:00
|
|
|
sin6_genmask = (struct sockaddr_in6 *)(void *)rtmp;
|
1999-12-28 02:37:14 +00:00
|
|
|
rtmp += ROUNDUP(sin6_genmask->sin6_len);
|
|
|
|
}
|
|
|
|
if (rtm->rtm_addrs & RTA_IFP) {
|
2017-01-13 06:22:49 +00:00
|
|
|
sin6_ifp = (struct sockaddr_in6 *)(void *)rtmp;
|
1999-12-28 02:37:14 +00:00
|
|
|
rtmp += ROUNDUP(sin6_ifp->sin6_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Destination */
|
|
|
|
if (sin6_dst->sin6_family != AF_INET6)
|
|
|
|
return;
|
|
|
|
if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst->sin6_addr))
|
|
|
|
return; /* Link-local */
|
|
|
|
if (IN6_ARE_ADDR_EQUAL(&sin6_dst->sin6_addr, &in6addr_loopback))
|
|
|
|
return; /* Loopback */
|
|
|
|
if (IN6_IS_ADDR_MULTICAST(&sin6_dst->sin6_addr))
|
|
|
|
return;
|
|
|
|
|
2001-06-11 12:39:29 +00:00
|
|
|
if ((rrt = MALLOC(struct riprt)) == NULL) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("malloc: struct riprt");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2000-07-05 02:14:16 +00:00
|
|
|
memset(rrt, 0, sizeof(*rrt));
|
1999-12-28 02:37:14 +00:00
|
|
|
np = &rrt->rrt_info;
|
|
|
|
rrt->rrt_same = NULL;
|
|
|
|
rrt->rrt_t = time(NULL);
|
|
|
|
if (aflag == 0 && (rtm->rtm_flags & RTF_STATIC))
|
|
|
|
rrt->rrt_t = 0; /* Don't age static routes */
|
2012-11-18 15:48:02 +00:00
|
|
|
if (rtm->rtm_flags & Pflag)
|
|
|
|
rrt->rrt_t = 0; /* Don't age PROTO[123] routes */
|
2003-11-14 17:16:50 +00:00
|
|
|
if ((rtm->rtm_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST)
|
|
|
|
rrt->rrt_t = 0; /* Don't age non-gateway host routes */
|
1999-12-28 02:37:14 +00:00
|
|
|
np->rip6_tag = 0;
|
|
|
|
np->rip6_metric = rtm->rtm_rmx.rmx_hopcount;
|
|
|
|
if (np->rip6_metric < 1)
|
|
|
|
np->rip6_metric = 1;
|
|
|
|
rrt->rrt_flags = rtm->rtm_flags;
|
|
|
|
np->rip6_dest = sin6_dst->sin6_addr;
|
|
|
|
|
|
|
|
/* Mask or plen */
|
|
|
|
if (rtm->rtm_flags & RTF_HOST)
|
|
|
|
np->rip6_plen = 128; /* Host route */
|
2001-06-11 12:39:29 +00:00
|
|
|
else if (sin6_mask)
|
|
|
|
np->rip6_plen = sin6mask2len(sin6_mask);
|
|
|
|
else
|
1999-12-28 02:37:14 +00:00
|
|
|
np->rip6_plen = 0;
|
|
|
|
|
2012-11-18 15:37:27 +00:00
|
|
|
orrt = rtsearch(np);
|
2001-06-11 12:39:29 +00:00
|
|
|
if (orrt && orrt->rrt_info.rip6_metric != HOPCNT_INFINITY6) {
|
1999-12-28 02:37:14 +00:00
|
|
|
/* Already found */
|
|
|
|
if (!again) {
|
|
|
|
trace(1, "route: %s/%d flags %s: already registered\n",
|
|
|
|
inet6_n2p(&np->rip6_dest), np->rip6_plen,
|
|
|
|
rtflags(rtm));
|
|
|
|
}
|
|
|
|
free(rrt);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Gateway */
|
|
|
|
if (!sin6_gw)
|
|
|
|
memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
|
|
|
|
else {
|
|
|
|
if (sin6_gw->sin6_family == AF_INET6)
|
|
|
|
rrt->rrt_gw = sin6_gw->sin6_addr;
|
|
|
|
else if (sin6_gw->sin6_family == AF_LINK) {
|
|
|
|
/* XXX in case ppp link? */
|
|
|
|
rrt->rrt_gw = in6addr_loopback;
|
|
|
|
} else
|
|
|
|
memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
|
|
|
|
}
|
|
|
|
trace(1, "route: %s/%d flags %s",
|
|
|
|
inet6_n2p(&np->rip6_dest), np->rip6_plen, rtflags(rtm));
|
|
|
|
trace(1, " gw %s", inet6_n2p(&rrt->rrt_gw));
|
|
|
|
|
|
|
|
/* Interface */
|
2012-11-18 15:37:27 +00:00
|
|
|
ifindex = rtm->rtm_index;
|
|
|
|
if ((unsigned int)ifindex < nindex2ifc && index2ifc[ifindex])
|
|
|
|
ifname = index2ifc[ifindex]->ifc_name;
|
2000-03-14 18:17:55 +00:00
|
|
|
else {
|
|
|
|
trace(1, " not configured\n");
|
2000-07-05 02:14:16 +00:00
|
|
|
free(rrt);
|
2000-03-14 18:17:55 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-11-18 15:37:27 +00:00
|
|
|
trace(1, " if %s sock %d", ifname, ifindex);
|
|
|
|
rrt->rrt_index = ifindex;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2000-07-05 02:14:16 +00:00
|
|
|
trace(1, "\n");
|
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
/* Check gateway */
|
|
|
|
if (!IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_gw) &&
|
2003-11-14 17:16:50 +00:00
|
|
|
!IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw) &&
|
|
|
|
(rrt->rrt_flags & RTF_LOCAL) == 0) {
|
1999-12-28 02:37:14 +00:00
|
|
|
trace(0, "***** Gateway %s is not a link-local address.\n",
|
|
|
|
inet6_n2p(&rrt->rrt_gw));
|
|
|
|
trace(0, "***** dest(%s) if(%s) -- Not optimized.\n",
|
2000-07-05 02:14:16 +00:00
|
|
|
inet6_n2p(&rrt->rrt_info.rip6_dest), ifname);
|
|
|
|
rrt->rrt_rflags |= RRTF_NH_NOT_LLADDR;
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Put it to the route list */
|
2001-06-11 12:39:29 +00:00
|
|
|
if (orrt && orrt->rrt_info.rip6_metric == HOPCNT_INFINITY6) {
|
|
|
|
/* replace route list */
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
|
|
|
|
TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
|
|
|
|
|
2001-06-11 12:39:29 +00:00
|
|
|
trace(1, "route: %s/%d flags %s: replace new route\n",
|
|
|
|
inet6_n2p(&np->rip6_dest), np->rip6_plen,
|
|
|
|
rtflags(rtm));
|
2012-11-18 15:37:27 +00:00
|
|
|
free(orrt);
|
|
|
|
} else
|
|
|
|
TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static int
|
2012-11-18 15:37:27 +00:00
|
|
|
addroute(struct riprt *rrt,
|
|
|
|
const struct in6_addr *gw,
|
|
|
|
struct ifc *ifcp)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
struct netinfo6 *np;
|
|
|
|
u_char buf[BUFSIZ], buf1[BUFSIZ], buf2[BUFSIZ];
|
|
|
|
struct rt_msghdr *rtm;
|
2003-08-17 16:05:49 +00:00
|
|
|
struct sockaddr_in6 *sin6;
|
1999-12-28 02:37:14 +00:00
|
|
|
int len;
|
|
|
|
|
|
|
|
np = &rrt->rrt_info;
|
2001-06-11 12:39:29 +00:00
|
|
|
inet_ntop(AF_INET6, (const void *)gw, (char *)buf1, sizeof(buf1));
|
1999-12-28 02:37:14 +00:00
|
|
|
inet_ntop(AF_INET6, (void *)&ifcp->ifc_mylladdr, (char *)buf2, sizeof(buf2));
|
|
|
|
tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n",
|
|
|
|
inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
|
|
|
|
np->rip6_metric - 1, buf2);
|
|
|
|
if (rtlog)
|
|
|
|
fprintf(rtlog, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(),
|
|
|
|
inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
|
|
|
|
np->rip6_metric - 1, buf2);
|
|
|
|
if (nflag)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
2017-01-13 06:22:49 +00:00
|
|
|
rtm = (struct rt_msghdr *)(void *)buf;
|
1999-12-28 02:37:14 +00:00
|
|
|
rtm->rtm_type = RTM_ADD;
|
|
|
|
rtm->rtm_version = RTM_VERSION;
|
|
|
|
rtm->rtm_seq = ++seq;
|
|
|
|
rtm->rtm_pid = pid;
|
2000-07-05 02:14:16 +00:00
|
|
|
rtm->rtm_flags = rrt->rrt_flags;
|
2012-11-18 15:48:02 +00:00
|
|
|
rtm->rtm_flags |= Qflag;
|
1999-12-28 02:37:14 +00:00
|
|
|
rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
|
|
|
|
rtm->rtm_rmx.rmx_hopcount = np->rip6_metric - 1;
|
|
|
|
rtm->rtm_inits = RTV_HOPCOUNT;
|
2017-01-13 06:22:49 +00:00
|
|
|
sin6 = (struct sockaddr_in6 *)(void *)&buf[sizeof(struct rt_msghdr)];
|
1999-12-28 02:37:14 +00:00
|
|
|
/* Destination */
|
2003-08-17 16:05:49 +00:00
|
|
|
sin6->sin6_len = sizeof(struct sockaddr_in6);
|
|
|
|
sin6->sin6_family = AF_INET6;
|
|
|
|
sin6->sin6_addr = np->rip6_dest;
|
2017-01-13 06:22:49 +00:00
|
|
|
sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
|
1999-12-28 02:37:14 +00:00
|
|
|
/* Gateway */
|
2003-08-17 16:05:49 +00:00
|
|
|
sin6->sin6_len = sizeof(struct sockaddr_in6);
|
|
|
|
sin6->sin6_family = AF_INET6;
|
|
|
|
sin6->sin6_addr = *gw;
|
2014-08-20 17:27:15 +00:00
|
|
|
if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
|
|
|
|
sin6->sin6_scope_id = ifcp->ifc_index;
|
2017-01-13 06:22:49 +00:00
|
|
|
sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
|
1999-12-28 02:37:14 +00:00
|
|
|
/* Netmask */
|
2003-08-17 16:05:49 +00:00
|
|
|
sin6->sin6_len = sizeof(struct sockaddr_in6);
|
|
|
|
sin6->sin6_family = AF_INET6;
|
|
|
|
sin6->sin6_addr = *(plen2mask(np->rip6_plen));
|
2017-01-13 06:22:49 +00:00
|
|
|
sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2003-08-17 16:05:49 +00:00
|
|
|
len = (char *)sin6 - (char *)buf;
|
1999-12-28 02:37:14 +00:00
|
|
|
rtm->rtm_msglen = len;
|
|
|
|
if (write(rtsock, buf, len) > 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (errno == EEXIST) {
|
|
|
|
trace(0, "ADD: Route already exists %s/%d gw %s\n",
|
2003-08-17 16:26:35 +00:00
|
|
|
inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
|
1999-12-28 02:37:14 +00:00
|
|
|
if (rtlog)
|
|
|
|
fprintf(rtlog, "ADD: Route already exists %s/%d gw %s\n",
|
2003-08-17 16:26:35 +00:00
|
|
|
inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
|
1999-12-28 02:37:14 +00:00
|
|
|
} else {
|
|
|
|
trace(0, "Can not write to rtsock (addroute): %s\n",
|
2003-08-17 16:26:35 +00:00
|
|
|
strerror(errno));
|
1999-12-28 02:37:14 +00:00
|
|
|
if (rtlog)
|
|
|
|
fprintf(rtlog, "\tCan not write to rtsock: %s\n",
|
2003-08-17 16:26:35 +00:00
|
|
|
strerror(errno));
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static int
|
2012-11-18 15:37:27 +00:00
|
|
|
delroute(struct netinfo6 *np, struct in6_addr *gw)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
u_char buf[BUFSIZ], buf2[BUFSIZ];
|
|
|
|
struct rt_msghdr *rtm;
|
2003-08-17 16:05:49 +00:00
|
|
|
struct sockaddr_in6 *sin6;
|
1999-12-28 02:37:14 +00:00
|
|
|
int len;
|
|
|
|
|
|
|
|
inet_ntop(AF_INET6, (void *)gw, (char *)buf2, sizeof(buf2));
|
|
|
|
tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np->rip6_dest),
|
|
|
|
np->rip6_plen, buf2);
|
|
|
|
if (rtlog)
|
|
|
|
fprintf(rtlog, "%s: DEL: %s/%d gw %s\n",
|
|
|
|
hms(), inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
|
|
|
|
if (nflag)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
2017-01-13 06:22:49 +00:00
|
|
|
rtm = (struct rt_msghdr *)(void *)buf;
|
1999-12-28 02:37:14 +00:00
|
|
|
rtm->rtm_type = RTM_DELETE;
|
|
|
|
rtm->rtm_version = RTM_VERSION;
|
|
|
|
rtm->rtm_seq = ++seq;
|
|
|
|
rtm->rtm_pid = pid;
|
|
|
|
rtm->rtm_flags = RTF_UP | RTF_GATEWAY;
|
2012-11-18 15:48:02 +00:00
|
|
|
rtm->rtm_flags |= Qflag;
|
2001-06-11 12:39:29 +00:00
|
|
|
if (np->rip6_plen == sizeof(struct in6_addr) * 8)
|
|
|
|
rtm->rtm_flags |= RTF_HOST;
|
1999-12-28 02:37:14 +00:00
|
|
|
rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
|
2017-01-13 06:22:49 +00:00
|
|
|
sin6 = (struct sockaddr_in6 *)(void *)&buf[sizeof(struct rt_msghdr)];
|
1999-12-28 02:37:14 +00:00
|
|
|
/* Destination */
|
2003-08-17 16:05:49 +00:00
|
|
|
sin6->sin6_len = sizeof(struct sockaddr_in6);
|
|
|
|
sin6->sin6_family = AF_INET6;
|
|
|
|
sin6->sin6_addr = np->rip6_dest;
|
2017-01-13 06:22:49 +00:00
|
|
|
sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
|
1999-12-28 02:37:14 +00:00
|
|
|
/* Gateway */
|
2003-08-17 16:05:49 +00:00
|
|
|
sin6->sin6_len = sizeof(struct sockaddr_in6);
|
|
|
|
sin6->sin6_family = AF_INET6;
|
|
|
|
sin6->sin6_addr = *gw;
|
2017-01-13 06:22:49 +00:00
|
|
|
sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
|
1999-12-28 02:37:14 +00:00
|
|
|
/* Netmask */
|
2003-08-17 16:05:49 +00:00
|
|
|
sin6->sin6_len = sizeof(struct sockaddr_in6);
|
|
|
|
sin6->sin6_family = AF_INET6;
|
|
|
|
sin6->sin6_addr = *(plen2mask(np->rip6_plen));
|
2017-01-13 06:22:49 +00:00
|
|
|
sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2003-08-17 16:05:49 +00:00
|
|
|
len = (char *)sin6 - (char *)buf;
|
1999-12-28 02:37:14 +00:00
|
|
|
rtm->rtm_msglen = len;
|
|
|
|
if (write(rtsock, buf, len) >= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (errno == ESRCH) {
|
|
|
|
trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n",
|
2003-08-17 16:26:35 +00:00
|
|
|
inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
|
1999-12-28 02:37:14 +00:00
|
|
|
if (rtlog)
|
|
|
|
fprintf(rtlog, "RTDEL: Route does not exist: %s/%d gw %s\n",
|
2003-08-17 16:26:35 +00:00
|
|
|
inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
|
1999-12-28 02:37:14 +00:00
|
|
|
} else {
|
|
|
|
trace(0, "Can not write to rtsock (delroute): %s\n",
|
2003-08-17 16:26:35 +00:00
|
|
|
strerror(errno));
|
1999-12-28 02:37:14 +00:00
|
|
|
if (rtlog)
|
|
|
|
fprintf(rtlog, "\tCan not write to rtsock: %s\n",
|
2003-08-17 16:26:35 +00:00
|
|
|
strerror(errno));
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
#if 0
|
|
|
|
static struct in6_addr *
|
2012-11-18 15:37:27 +00:00
|
|
|
getroute(struct netinfo6 *np, struct in6_addr *gw)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
u_char buf[BUFSIZ];
|
2003-08-18 16:20:30 +00:00
|
|
|
int myseq;
|
1999-12-28 02:37:14 +00:00
|
|
|
int len;
|
|
|
|
struct rt_msghdr *rtm;
|
2003-08-17 16:05:49 +00:00
|
|
|
struct sockaddr_in6 *sin6;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
rtm = (struct rt_msghdr *)(void *)buf;
|
1999-12-28 02:37:14 +00:00
|
|
|
len = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in6);
|
|
|
|
memset(rtm, 0, len);
|
|
|
|
rtm->rtm_type = RTM_GET;
|
|
|
|
rtm->rtm_version = RTM_VERSION;
|
|
|
|
myseq = ++seq;
|
|
|
|
rtm->rtm_seq = myseq;
|
|
|
|
rtm->rtm_addrs = RTA_DST;
|
|
|
|
rtm->rtm_msglen = len;
|
2017-01-13 06:22:49 +00:00
|
|
|
sin6 = (struct sockaddr_in6 *)(void *)&buf[sizeof(struct rt_msghdr)];
|
2003-08-17 16:05:49 +00:00
|
|
|
sin6->sin6_len = sizeof(struct sockaddr_in6);
|
|
|
|
sin6->sin6_family = AF_INET6;
|
|
|
|
sin6->sin6_addr = np->rip6_dest;
|
1999-12-28 02:37:14 +00:00
|
|
|
if (write(rtsock, buf, len) < 0) {
|
|
|
|
if (errno == ESRCH) /* No such route found */
|
|
|
|
return NULL;
|
|
|
|
perror("write to rtsock");
|
2001-06-11 12:39:29 +00:00
|
|
|
exit(1);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
do {
|
|
|
|
if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
|
|
|
|
perror("read from rtsock");
|
2001-06-11 12:39:29 +00:00
|
|
|
exit(1);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
2017-01-13 06:22:49 +00:00
|
|
|
rtm = (struct rt_msghdr *)(void *)buf;
|
2017-04-16 19:17:10 +00:00
|
|
|
} while (rtm->rtm_type != RTM_GET || rtm->rtm_seq != myseq ||
|
|
|
|
rtm->rtm_pid != pid);
|
2017-01-13 06:22:49 +00:00
|
|
|
sin6 = (struct sockaddr_in6 *)(void *)&buf[sizeof(struct rt_msghdr)];
|
1999-12-28 02:37:14 +00:00
|
|
|
if (rtm->rtm_addrs & RTA_DST) {
|
2017-01-13 06:22:49 +00:00
|
|
|
sin6 = (struct sockaddr_in6 *)(void *)
|
2003-08-17 16:05:49 +00:00
|
|
|
((char *)sin6 + ROUNDUP(sin6->sin6_len));
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
if (rtm->rtm_addrs & RTA_GATEWAY) {
|
2003-08-17 16:05:49 +00:00
|
|
|
*gw = sin6->sin6_addr;
|
1999-12-28 02:37:14 +00:00
|
|
|
return gw;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-01-13 06:22:49 +00:00
|
|
|
#endif
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static const char *
|
2012-11-18 15:37:27 +00:00
|
|
|
inet6_n2p(const struct in6_addr *p)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
static char buf[BUFSIZ];
|
|
|
|
|
2001-06-11 12:39:29 +00:00
|
|
|
return inet_ntop(AF_INET6, (const void *)p, buf, sizeof(buf));
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static void
|
2012-11-18 15:37:27 +00:00
|
|
|
ifrtdump(int sig)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
ifdump(sig);
|
|
|
|
rtdump(sig);
|
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static void
|
2012-11-18 15:37:27 +00:00
|
|
|
ifdump(int sig)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
struct ifc *ifcp;
|
|
|
|
FILE *dump;
|
2012-11-18 15:37:27 +00:00
|
|
|
int nifc = 0;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
if (sig == 0)
|
|
|
|
dump = stderr;
|
|
|
|
else
|
|
|
|
if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
|
|
|
|
dump = stderr;
|
|
|
|
|
|
|
|
fprintf(dump, "%s: Interface Table Dump\n", hms());
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_FOREACH(ifcp, &ifc_head, ifc_next)
|
|
|
|
nifc++;
|
1999-12-28 02:37:14 +00:00
|
|
|
fprintf(dump, " Number of interfaces: %d\n", nifc);
|
2012-11-18 15:37:27 +00:00
|
|
|
|
|
|
|
fprintf(dump, " advertising interfaces:\n");
|
|
|
|
TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
|
|
|
|
if ((ifcp->ifc_flags & IFF_UP) == 0)
|
|
|
|
continue;
|
|
|
|
if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
|
|
|
|
continue;
|
|
|
|
ifdump0(dump, ifcp);
|
|
|
|
}
|
|
|
|
fprintf(dump, "\n");
|
|
|
|
fprintf(dump, " non-advertising interfaces:\n");
|
|
|
|
TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
|
|
|
|
if ((ifcp->ifc_flags & IFF_UP) &&
|
|
|
|
(iff_find(ifcp, IFIL_TYPE_N) == NULL))
|
|
|
|
continue;
|
|
|
|
ifdump0(dump, ifcp);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
fprintf(dump, "\n");
|
|
|
|
if (dump != stderr)
|
|
|
|
fclose(dump);
|
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static void
|
2012-11-18 15:37:27 +00:00
|
|
|
ifdump0(FILE *dump, const struct ifc *ifcp)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
2012-11-18 15:37:27 +00:00
|
|
|
struct ifac *ifac;
|
1999-12-28 02:37:14 +00:00
|
|
|
struct iff *iffp;
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
const char *ft;
|
|
|
|
int addr;
|
|
|
|
|
|
|
|
fprintf(dump, " %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n",
|
|
|
|
ifcp->ifc_name, ifcp->ifc_index, ifflags(ifcp->ifc_flags),
|
|
|
|
inet6_n2p(&ifcp->ifc_mylladdr),
|
|
|
|
ifcp->ifc_mtu, ifcp->ifc_metric);
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
|
1999-12-28 02:37:14 +00:00
|
|
|
if (ifcp->ifc_flags & IFF_POINTOPOINT) {
|
2012-11-18 15:37:27 +00:00
|
|
|
inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr,
|
1999-12-28 02:37:14 +00:00
|
|
|
buf, sizeof(buf));
|
|
|
|
fprintf(dump, "\t%s/%d -- %s\n",
|
2012-11-18 15:37:27 +00:00
|
|
|
inet6_n2p(&ifac->ifac_addr),
|
|
|
|
ifac->ifac_plen, buf);
|
1999-12-28 02:37:14 +00:00
|
|
|
} else {
|
|
|
|
fprintf(dump, "\t%s/%d\n",
|
2012-11-18 15:37:27 +00:00
|
|
|
inet6_n2p(&ifac->ifac_addr),
|
|
|
|
ifac->ifac_plen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(dump, "\tFilter:\n");
|
|
|
|
TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
|
|
|
|
addr = 0;
|
|
|
|
switch (iffp->iff_type) {
|
|
|
|
case IFIL_TYPE_A:
|
|
|
|
ft = "Aggregate"; addr++; break;
|
|
|
|
case IFIL_TYPE_N:
|
|
|
|
ft = "No-use"; break;
|
|
|
|
case IFIL_TYPE_O:
|
|
|
|
ft = "Advertise-only"; addr++; break;
|
|
|
|
case IFIL_TYPE_T:
|
|
|
|
ft = "Default-only"; break;
|
|
|
|
case IFIL_TYPE_L:
|
|
|
|
ft = "Listen-only"; addr++; break;
|
|
|
|
default:
|
|
|
|
snprintf(buf, sizeof(buf), "Unknown-%c", iffp->iff_type);
|
|
|
|
ft = buf;
|
|
|
|
addr++;
|
|
|
|
break;
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
2012-11-18 15:37:27 +00:00
|
|
|
fprintf(dump, "\t\t%s", ft);
|
|
|
|
if (addr)
|
|
|
|
fprintf(dump, "(%s/%d)", inet6_n2p(&iffp->iff_addr),
|
|
|
|
iffp->iff_plen);
|
1999-12-28 02:37:14 +00:00
|
|
|
fprintf(dump, "\n");
|
|
|
|
}
|
2012-11-18 15:37:27 +00:00
|
|
|
fprintf(dump, "\n");
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static void
|
2012-11-18 15:37:27 +00:00
|
|
|
rtdump(int sig)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
struct riprt *rrt;
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
FILE *dump;
|
|
|
|
time_t t, age;
|
|
|
|
|
|
|
|
if (sig == 0)
|
|
|
|
dump = stderr;
|
|
|
|
else
|
|
|
|
if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
|
|
|
|
dump = stderr;
|
|
|
|
|
|
|
|
t = time(NULL);
|
|
|
|
fprintf(dump, "\n%s: Routing Table Dump\n", hms());
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
|
1999-12-28 02:37:14 +00:00
|
|
|
if (rrt->rrt_t == 0)
|
|
|
|
age = 0;
|
|
|
|
else
|
|
|
|
age = t - rrt->rrt_t;
|
|
|
|
inet_ntop(AF_INET6, (void *)&rrt->rrt_info.rip6_dest,
|
|
|
|
buf, sizeof(buf));
|
|
|
|
fprintf(dump, " %s/%d if(%d:%s) gw(%s) [%d] age(%ld)",
|
|
|
|
buf, rrt->rrt_info.rip6_plen, rrt->rrt_index,
|
|
|
|
index2ifc[rrt->rrt_index]->ifc_name,
|
|
|
|
inet6_n2p(&rrt->rrt_gw),
|
|
|
|
rrt->rrt_info.rip6_metric, (long)age);
|
|
|
|
if (rrt->rrt_info.rip6_tag) {
|
|
|
|
fprintf(dump, " tag(0x%04x)",
|
|
|
|
ntohs(rrt->rrt_info.rip6_tag) & 0xffff);
|
|
|
|
}
|
2000-07-05 02:14:16 +00:00
|
|
|
if (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)
|
1999-12-28 02:37:14 +00:00
|
|
|
fprintf(dump, " NOT-LL");
|
2000-07-05 02:14:16 +00:00
|
|
|
if (rrt->rrt_rflags & RRTF_NOADVERTISE)
|
1999-12-28 02:37:14 +00:00
|
|
|
fprintf(dump, " NO-ADV");
|
|
|
|
fprintf(dump, "\n");
|
|
|
|
}
|
|
|
|
fprintf(dump, "\n");
|
|
|
|
if (dump != stderr)
|
|
|
|
fclose(dump);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse the -A (and -O) options and put corresponding filter object to the
|
2001-06-11 12:39:29 +00:00
|
|
|
* specified interface structures. Each of the -A/O option has the following
|
1999-12-28 02:37:14 +00:00
|
|
|
* syntax: -A 5f09:c400::/32,ef0,ef1 (aggregate)
|
|
|
|
* -O 5f09:c400::/32,ef0,ef1 (only when match)
|
|
|
|
*/
|
2017-01-13 06:22:49 +00:00
|
|
|
static void
|
2012-11-18 15:37:27 +00:00
|
|
|
filterconfig(void)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
int i;
|
2003-08-18 16:09:59 +00:00
|
|
|
char *p, *ap, *iflp, *ifname, *ep;
|
2012-11-18 15:37:27 +00:00
|
|
|
struct iff iff, *iffp;
|
2001-06-11 12:39:29 +00:00
|
|
|
struct ifc *ifcp;
|
|
|
|
struct riprt *rrt;
|
2000-08-14 04:54:58 +00:00
|
|
|
#if 0
|
2001-06-11 12:39:29 +00:00
|
|
|
struct in6_addr gw;
|
2000-08-14 04:54:58 +00:00
|
|
|
#endif
|
2003-08-18 16:09:59 +00:00
|
|
|
u_long plen;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
for (i = 0; i < nfilter; i++) {
|
|
|
|
ap = filter[i];
|
|
|
|
iflp = NULL;
|
2012-11-18 15:37:27 +00:00
|
|
|
iffp = ⇔
|
|
|
|
memset(iffp, 0, sizeof(*iffp));
|
1999-12-28 02:37:14 +00:00
|
|
|
if (filtertype[i] == 'N' || filtertype[i] == 'T') {
|
|
|
|
iflp = ap;
|
|
|
|
goto ifonly;
|
|
|
|
}
|
2003-08-17 17:34:09 +00:00
|
|
|
if ((p = strchr(ap, ',')) != NULL) {
|
1999-12-28 02:37:14 +00:00
|
|
|
*p++ = '\0';
|
|
|
|
iflp = p;
|
|
|
|
}
|
2003-08-17 17:34:09 +00:00
|
|
|
if ((p = strchr(ap, '/')) == NULL) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("no prefixlen specified for '%s'", ap);
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
*p++ = '\0';
|
2012-11-18 15:37:27 +00:00
|
|
|
if (inet_pton(AF_INET6, ap, &iffp->iff_addr) != 1) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("invalid prefix specified for '%s'", ap);
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2003-08-18 16:09:59 +00:00
|
|
|
errno = 0;
|
|
|
|
ep = NULL;
|
|
|
|
plen = strtoul(p, &ep, 10);
|
2012-11-18 15:37:27 +00:00
|
|
|
if (errno || !*p || *ep || plen > sizeof(iffp->iff_addr) * 8) {
|
2003-08-18 16:09:59 +00:00
|
|
|
fatal("invalid prefix length specified for '%s'", ap);
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2012-11-18 15:37:27 +00:00
|
|
|
iffp->iff_plen = plen;
|
|
|
|
applyplen(&iffp->iff_addr, iffp->iff_plen);
|
1999-12-28 02:37:14 +00:00
|
|
|
ifonly:
|
2012-11-18 15:37:27 +00:00
|
|
|
iffp->iff_type = filtertype[i];
|
2001-06-11 12:39:29 +00:00
|
|
|
if (iflp == NULL || *iflp == '\0') {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("no interface specified for '%s'", ap);
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
/* parse the interface listing portion */
|
|
|
|
while (iflp) {
|
|
|
|
ifname = iflp;
|
2003-08-17 17:34:09 +00:00
|
|
|
if ((iflp = strchr(iflp, ',')) != NULL)
|
1999-12-28 02:37:14 +00:00
|
|
|
*iflp++ = '\0';
|
2012-11-18 15:37:27 +00:00
|
|
|
|
|
|
|
TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
|
2012-11-18 15:48:02 +00:00
|
|
|
if (fnmatch(ifname, ifcp->ifc_name, 0) != 0)
|
2012-11-18 15:37:27 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
iffp = malloc(sizeof(*iffp));
|
|
|
|
if (iffp == NULL) {
|
|
|
|
fatal("malloc of iff");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
memcpy(iffp, &iff, sizeof(*iffp));
|
2012-11-18 15:48:02 +00:00
|
|
|
#if 0
|
|
|
|
syslog(LOG_INFO, "Add filter: type %d, ifname %s.", iffp->iff_type, ifname);
|
|
|
|
#endif
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_INSERT_HEAD(&ifcp->ifc_iff_head, iffp, iff_next);
|
2001-06-11 12:39:29 +00:00
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
2001-06-11 12:39:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* -A: aggregate configuration.
|
|
|
|
*/
|
2012-11-18 15:37:27 +00:00
|
|
|
if (filtertype[i] != IFIL_TYPE_A)
|
1999-12-28 02:37:14 +00:00
|
|
|
continue;
|
|
|
|
/* put the aggregate to the kernel routing table */
|
|
|
|
rrt = (struct riprt *)malloc(sizeof(struct riprt));
|
2001-06-11 12:39:29 +00:00
|
|
|
if (rrt == NULL) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("malloc: rrt");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
memset(rrt, 0, sizeof(struct riprt));
|
2012-11-18 15:37:27 +00:00
|
|
|
rrt->rrt_info.rip6_dest = iff.iff_addr;
|
|
|
|
rrt->rrt_info.rip6_plen = iff.iff_plen;
|
1999-12-28 02:37:14 +00:00
|
|
|
rrt->rrt_info.rip6_metric = 1;
|
|
|
|
rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
|
|
|
|
rrt->rrt_gw = in6addr_loopback;
|
2000-07-05 02:14:16 +00:00
|
|
|
rrt->rrt_flags = RTF_UP | RTF_REJECT;
|
|
|
|
rrt->rrt_rflags = RRTF_AGGREGATE;
|
1999-12-28 02:37:14 +00:00
|
|
|
rrt->rrt_t = 0;
|
2003-08-17 17:40:47 +00:00
|
|
|
rrt->rrt_index = loopifcp->ifc_index;
|
2000-08-14 04:54:58 +00:00
|
|
|
#if 0
|
|
|
|
if (getroute(&rrt->rrt_info, &gw)) {
|
|
|
|
#if 0
|
|
|
|
/*
|
|
|
|
* When the address has already been registered in the
|
2017-01-09 00:33:27 +00:00
|
|
|
* kernel routing table, it should be removed
|
2000-08-14 04:54:58 +00:00
|
|
|
*/
|
|
|
|
delroute(&rrt->rrt_info, &gw);
|
|
|
|
#else
|
2001-06-11 12:39:29 +00:00
|
|
|
/* it is safer behavior */
|
2000-08-14 04:54:58 +00:00
|
|
|
errno = EINVAL;
|
|
|
|
fatal("%s/%u already in routing table, "
|
|
|
|
"cannot aggregate",
|
|
|
|
inet6_n2p(&rrt->rrt_info.rip6_dest),
|
|
|
|
rrt->rrt_info.rip6_plen);
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
2000-08-14 04:54:58 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
1999-12-28 02:37:14 +00:00
|
|
|
/* Put the route to the list */
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
|
1999-12-28 02:37:14 +00:00
|
|
|
trace(1, "Aggregate: %s/%d for %s\n",
|
2012-11-18 15:37:27 +00:00
|
|
|
inet6_n2p(&iff.iff_addr), iff.iff_plen,
|
|
|
|
loopifcp->ifc_name);
|
1999-12-28 02:37:14 +00:00
|
|
|
/* Add this route to the kernel */
|
|
|
|
if (nflag) /* do not modify kernel routing table */
|
|
|
|
continue;
|
|
|
|
addroute(rrt, &in6addr_loopback, loopifcp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************** utility functions *****************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns a pointer to ifac whose address and prefix length matches
|
|
|
|
* with the address and prefix length specified in the arguments.
|
|
|
|
*/
|
2017-01-13 06:22:49 +00:00
|
|
|
static struct ifac *
|
2012-11-18 15:37:27 +00:00
|
|
|
ifa_match(const struct ifc *ifcp,
|
|
|
|
const struct in6_addr *ia,
|
|
|
|
int plen)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
2012-11-18 15:37:27 +00:00
|
|
|
struct ifac *ifac;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
|
|
|
|
if (IN6_ARE_ADDR_EQUAL(&ifac->ifac_addr, ia) &&
|
|
|
|
ifac->ifac_plen == plen)
|
1999-12-28 02:37:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-11-18 15:37:27 +00:00
|
|
|
|
|
|
|
return (ifac);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a pointer to riprt structure whose address and prefix length
|
|
|
|
* matches with the address and prefix length found in the argument.
|
2001-06-11 12:39:29 +00:00
|
|
|
* Note: This is not a rtalloc(). Therefore exact match is necessary.
|
1999-12-28 02:37:14 +00:00
|
|
|
*/
|
2017-01-13 06:22:49 +00:00
|
|
|
static struct riprt *
|
2012-11-18 15:37:27 +00:00
|
|
|
rtsearch(struct netinfo6 *np)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
struct riprt *rrt;
|
|
|
|
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
|
1999-12-28 02:37:14 +00:00
|
|
|
if (rrt->rrt_info.rip6_plen == np->rip6_plen &&
|
|
|
|
IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
|
|
|
|
&np->rip6_dest))
|
2012-11-18 15:37:27 +00:00
|
|
|
break;
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
2012-11-18 15:37:27 +00:00
|
|
|
|
|
|
|
return (rrt);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static int
|
2012-11-18 15:37:27 +00:00
|
|
|
sin6mask2len(const struct sockaddr_in6 *sin6)
|
2001-06-11 12:39:29 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
return mask2len(&sin6->sin6_addr,
|
|
|
|
sin6->sin6_len - offsetof(struct sockaddr_in6, sin6_addr));
|
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static int
|
2012-11-18 15:37:27 +00:00
|
|
|
mask2len(const struct in6_addr *addr, int lenlim)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
int i = 0, j;
|
2001-06-11 12:39:29 +00:00
|
|
|
const u_char *p = (const u_char *)addr;
|
2017-01-09 00:33:27 +00:00
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
for (j = 0; j < lenlim; j++, p++) {
|
|
|
|
if (*p != 0xff)
|
|
|
|
break;
|
|
|
|
i += 8;
|
|
|
|
}
|
|
|
|
if (j < lenlim) {
|
|
|
|
switch (*p) {
|
2000-07-05 02:14:16 +00:00
|
|
|
#define MASKLEN(m, l) case m: do { i += l; break; } while (0)
|
|
|
|
MASKLEN(0xfe, 7); break;
|
|
|
|
MASKLEN(0xfc, 6); break;
|
|
|
|
MASKLEN(0xf8, 5); break;
|
|
|
|
MASKLEN(0xf0, 4); break;
|
|
|
|
MASKLEN(0xe0, 3); break;
|
|
|
|
MASKLEN(0xc0, 2); break;
|
|
|
|
MASKLEN(0x80, 1); break;
|
1999-12-28 02:37:14 +00:00
|
|
|
#undef MASKLEN
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const u_char plent[8] = {
|
|
|
|
0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
|
|
|
|
};
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static void
|
2012-11-18 15:37:27 +00:00
|
|
|
applyplen(struct in6_addr *ia, int plen)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
u_char *p;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
p = ia->s6_addr;
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
if (plen <= 0)
|
|
|
|
*p = 0;
|
|
|
|
else if (plen < 8)
|
|
|
|
*p &= plent[plen];
|
|
|
|
p++, plen -= 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const int pl2m[9] = {
|
|
|
|
0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
|
|
|
|
};
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static struct in6_addr *
|
2012-11-18 15:37:27 +00:00
|
|
|
plen2mask(int n)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
static struct in6_addr ia;
|
|
|
|
u_char *p;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memset(&ia, 0, sizeof(struct in6_addr));
|
|
|
|
p = (u_char *)&ia;
|
|
|
|
for (i = 0; i < 16; i++, p++, n -= 8) {
|
|
|
|
if (n >= 8) {
|
|
|
|
*p = 0xff;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*p = pl2m[n];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return &ia;
|
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static char *
|
2012-11-18 15:37:27 +00:00
|
|
|
allocopy(char *p)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
2003-08-17 16:07:57 +00:00
|
|
|
int len = strlen(p) + 1;
|
|
|
|
char *q = (char *)malloc(len);
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2003-08-17 16:07:57 +00:00
|
|
|
if (!q) {
|
|
|
|
fatal("malloc");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
|
|
|
strlcpy(q, p, len);
|
1999-12-28 02:37:14 +00:00
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static char *
|
2012-11-18 15:37:27 +00:00
|
|
|
hms(void)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
static char buf[BUFSIZ];
|
|
|
|
time_t t;
|
|
|
|
struct tm *tm;
|
|
|
|
|
|
|
|
t = time(NULL);
|
2001-06-11 12:39:29 +00:00
|
|
|
if ((tm = localtime(&t)) == 0) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("localtime");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
snprintf(buf, sizeof(buf), "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
|
|
|
|
tm->tm_sec);
|
1999-12-28 02:37:14 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define RIPRANDDEV 1.0 /* 30 +- 15, max - min = 30 */
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static int
|
2012-11-18 15:37:27 +00:00
|
|
|
ripinterval(int timer)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
double r = rand();
|
|
|
|
|
|
|
|
interval = (int)(timer + timer * RIPRANDDEV * (r / RAND_MAX - 0.5));
|
|
|
|
nextalarm = time(NULL) + interval;
|
|
|
|
return interval;
|
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
#if 0
|
|
|
|
static time_t
|
2012-11-18 15:37:27 +00:00
|
|
|
ripsuptrig(void)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
time_t t;
|
|
|
|
|
|
|
|
double r = rand();
|
2017-01-09 00:33:27 +00:00
|
|
|
t = (int)(RIP_TRIG_INT6_MIN +
|
2001-06-11 12:39:29 +00:00
|
|
|
(RIP_TRIG_INT6_MAX - RIP_TRIG_INT6_MIN) * (r / RAND_MAX));
|
1999-12-28 02:37:14 +00:00
|
|
|
sup_trig_update = time(NULL) + t;
|
|
|
|
return t;
|
|
|
|
}
|
2017-01-13 06:22:49 +00:00
|
|
|
#endif
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static void
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char buf[1024];
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(buf, sizeof(buf), fmt, ap);
|
2003-08-17 18:35:56 +00:00
|
|
|
va_end(ap);
|
1999-12-28 02:37:14 +00:00
|
|
|
perror(buf);
|
2003-08-17 18:35:56 +00:00
|
|
|
if (errno)
|
|
|
|
syslog(LOG_ERR, "%s: %s", buf, strerror(errno));
|
|
|
|
else
|
|
|
|
syslog(LOG_ERR, "%s", buf);
|
2001-06-11 12:39:29 +00:00
|
|
|
rtdexit();
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static void
|
1999-12-28 02:37:14 +00:00
|
|
|
tracet(int level, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
2003-08-17 18:35:56 +00:00
|
|
|
if (level <= dflag) {
|
|
|
|
va_start(ap, fmt);
|
1999-12-28 02:37:14 +00:00
|
|
|
fprintf(stderr, "%s: ", hms());
|
|
|
|
vfprintf(stderr, fmt, ap);
|
2003-08-17 18:35:56 +00:00
|
|
|
va_end(ap);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
if (dflag) {
|
2003-08-17 18:35:56 +00:00
|
|
|
va_start(ap, fmt);
|
1999-12-28 02:37:14 +00:00
|
|
|
if (level > 0)
|
|
|
|
vsyslog(LOG_DEBUG, fmt, ap);
|
|
|
|
else
|
|
|
|
vsyslog(LOG_WARNING, fmt, ap);
|
2003-08-17 18:35:56 +00:00
|
|
|
va_end(ap);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static void
|
1999-12-28 02:37:14 +00:00
|
|
|
trace(int level, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
2003-08-17 18:35:56 +00:00
|
|
|
if (level <= dflag) {
|
|
|
|
va_start(ap, fmt);
|
1999-12-28 02:37:14 +00:00
|
|
|
vfprintf(stderr, fmt, ap);
|
2003-08-17 18:35:56 +00:00
|
|
|
va_end(ap);
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
if (dflag) {
|
2003-08-17 18:35:56 +00:00
|
|
|
va_start(ap, fmt);
|
1999-12-28 02:37:14 +00:00
|
|
|
if (level > 0)
|
|
|
|
vsyslog(LOG_DEBUG, fmt, ap);
|
|
|
|
else
|
|
|
|
vsyslog(LOG_WARNING, fmt, ap);
|
2003-08-17 18:35:56 +00:00
|
|
|
va_end(ap);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static struct ifc *
|
2012-11-18 15:37:27 +00:00
|
|
|
ifc_find(char *name)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
struct ifc *ifcp;
|
|
|
|
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
|
1999-12-28 02:37:14 +00:00
|
|
|
if (strcmp(name, ifcp->ifc_name) == 0)
|
2012-11-18 15:37:27 +00:00
|
|
|
break;
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
2012-11-18 15:37:27 +00:00
|
|
|
return (ifcp);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static struct iff *
|
2012-11-18 15:37:27 +00:00
|
|
|
iff_find(struct ifc *ifcp, int type)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
struct iff *iffp;
|
|
|
|
|
2012-11-18 15:37:27 +00:00
|
|
|
TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
|
|
|
|
if (type == IFIL_TYPE_ANY ||
|
|
|
|
type == iffp->iff_type)
|
|
|
|
break;
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
2012-11-18 15:37:27 +00:00
|
|
|
|
|
|
|
return (iffp);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 06:22:49 +00:00
|
|
|
static void
|
2012-11-18 15:37:27 +00:00
|
|
|
setindex2ifc(int idx, struct ifc *ifcp)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
2003-11-14 17:16:50 +00:00
|
|
|
int n, nsize;
|
2000-07-05 02:14:16 +00:00
|
|
|
struct ifc **p;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
if (!index2ifc) {
|
|
|
|
nindex2ifc = 5; /*initial guess*/
|
|
|
|
index2ifc = (struct ifc **)
|
|
|
|
malloc(sizeof(*index2ifc) * nindex2ifc);
|
2001-06-11 12:39:29 +00:00
|
|
|
if (index2ifc == NULL) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("malloc");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
memset(index2ifc, 0, sizeof(*index2ifc) * nindex2ifc);
|
|
|
|
}
|
|
|
|
n = nindex2ifc;
|
2003-11-14 17:16:50 +00:00
|
|
|
for (nsize = nindex2ifc; nsize <= idx; nsize *= 2)
|
|
|
|
;
|
|
|
|
if (n != nsize) {
|
2000-07-05 02:14:16 +00:00
|
|
|
p = (struct ifc **)realloc(index2ifc,
|
2003-11-14 17:16:50 +00:00
|
|
|
sizeof(*index2ifc) * nsize);
|
2001-06-11 12:39:29 +00:00
|
|
|
if (p == NULL) {
|
1999-12-28 02:37:14 +00:00
|
|
|
fatal("realloc");
|
2001-06-11 12:39:29 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
memset(p + n, 0, sizeof(*index2ifc) * (nindex2ifc - n));
|
2000-07-05 02:14:16 +00:00
|
|
|
index2ifc = p;
|
2003-11-14 17:16:50 +00:00
|
|
|
nindex2ifc = nsize;
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
2001-06-11 12:39:29 +00:00
|
|
|
index2ifc[idx] = ifcp;
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|