1998-06-02 22:02:55 +04:00
|
|
|
/* $NetBSD: input.c,v 1.22 1998/06/02 18:02:55 thorpej Exp $ */
|
1995-03-18 17:54:19 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-05-13 12:02:16 +04:00
|
|
|
* Copyright (c) 1983, 1988, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03: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. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
|
|
|
|
*/
|
|
|
|
|
1996-09-24 20:24:12 +04:00
|
|
|
#if !defined(lint) && !defined(sgi) && !defined(__NetBSD__)
|
1995-03-18 17:54:19 +03:00
|
|
|
static char sccsid[] = "@(#)input.c 8.1 (Berkeley) 6/5/93";
|
1996-09-24 20:24:12 +04:00
|
|
|
#elif defined(__NetBSD__)
|
1997-09-15 14:38:11 +04:00
|
|
|
#include <sys/cdefs.h>
|
1998-06-02 22:02:55 +04:00
|
|
|
__RCSID("$NetBSD: input.c,v 1.22 1998/06/02 18:02:55 thorpej Exp $");
|
1995-03-18 17:54:19 +03:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
#include "defs.h"
|
|
|
|
|
1997-02-04 01:02:51 +03:00
|
|
|
static void input(struct sockaddr_in *, struct interface *, struct interface *,
|
|
|
|
struct rip *, int);
|
1998-06-02 22:02:55 +04:00
|
|
|
static void input_route(naddr, naddr, struct rt_spare *, struct netinfo *);
|
1997-02-04 01:02:51 +03:00
|
|
|
static int ck_passwd(struct interface *, struct rip *, void *,
|
|
|
|
naddr, struct msg_limit *);
|
1995-06-21 02:25:51 +04:00
|
|
|
|
1996-08-10 05:28:58 +04:00
|
|
|
|
|
|
|
/* process RIP input
|
1995-06-21 02:25:51 +04:00
|
|
|
*/
|
1996-08-10 05:28:58 +04:00
|
|
|
void
|
|
|
|
read_rip(int sock,
|
1997-02-04 01:02:51 +03:00
|
|
|
struct interface *sifp)
|
1995-06-21 02:25:51 +04:00
|
|
|
{
|
1996-08-10 05:28:58 +04:00
|
|
|
struct sockaddr_in from;
|
1997-02-04 01:02:51 +03:00
|
|
|
struct interface *aifp;
|
1996-08-10 05:28:58 +04:00
|
|
|
int fromlen, cc;
|
1997-02-04 01:02:51 +03:00
|
|
|
#ifdef USE_PASSIFNAME
|
|
|
|
static struct msg_limit bad_name;
|
|
|
|
struct {
|
|
|
|
char ifname[IFNAMSIZ];
|
|
|
|
union pkt_buf pbuf;
|
|
|
|
} inbuf;
|
|
|
|
#else
|
|
|
|
struct {
|
|
|
|
union pkt_buf pbuf;
|
|
|
|
} inbuf;
|
|
|
|
#endif
|
1995-06-21 02:25:51 +04:00
|
|
|
|
|
|
|
|
1996-08-10 05:28:58 +04:00
|
|
|
for (;;) {
|
|
|
|
fromlen = sizeof(from);
|
|
|
|
cc = recvfrom(sock, &inbuf, sizeof(inbuf), 0,
|
|
|
|
(struct sockaddr*)&from, &fromlen);
|
|
|
|
if (cc <= 0) {
|
|
|
|
if (cc < 0 && errno != EWOULDBLOCK)
|
|
|
|
LOGERR("recvfrom(rip)");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (fromlen != sizeof(struct sockaddr_in))
|
|
|
|
logbad(1,"impossible recvfrom(rip) fromlen=%d",
|
|
|
|
fromlen);
|
1995-06-21 02:25:51 +04:00
|
|
|
|
1997-02-04 01:02:51 +03:00
|
|
|
/* aifp is the "authenticated" interface via which the packet
|
|
|
|
* arrived. In fact, it is only the interface on which
|
|
|
|
* the packet should have arrived based on is source
|
|
|
|
* address.
|
|
|
|
* sifp is interface associated with the socket through which
|
|
|
|
* the packet was received.
|
|
|
|
*/
|
|
|
|
#ifdef USE_PASSIFNAME
|
|
|
|
if ((cc -= sizeof(inbuf.ifname)) < 0)
|
|
|
|
logbad(0,"missing USE_PASSIFNAME; only %d bytes",
|
|
|
|
cc+sizeof(inbuf.ifname));
|
|
|
|
|
|
|
|
/* check the remote interfaces first */
|
|
|
|
for (aifp = remote_if; aifp; aifp = aifp->int_rlink) {
|
|
|
|
if (aifp->int_addr == from.sin_addr.s_addr)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (aifp == 0) {
|
|
|
|
aifp = ifwithname(inbuf.ifname, 0);
|
|
|
|
if (aifp == 0) {
|
|
|
|
msglim(&bad_name, from.sin_addr.s_addr,
|
|
|
|
"impossible interface name %.*s",
|
|
|
|
IFNAMSIZ, inbuf.ifname);
|
|
|
|
} else if (((aifp->int_if_flags & IFF_POINTOPOINT)
|
|
|
|
&& aifp->int_dstaddr!=from.sin_addr.s_addr)
|
|
|
|
|| (!(aifp->int_if_flags & IFF_POINTOPOINT)
|
|
|
|
&& !on_net(from.sin_addr.s_addr,
|
|
|
|
aifp->int_net,
|
|
|
|
aifp->int_mask))) {
|
|
|
|
/* If it came via the wrong interface, do not
|
|
|
|
* trust it.
|
|
|
|
*/
|
|
|
|
aifp = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
aifp = iflookup(from.sin_addr.s_addr);
|
|
|
|
#endif
|
|
|
|
if (sifp == 0)
|
|
|
|
sifp = aifp;
|
|
|
|
|
|
|
|
input(&from, sifp, aifp, &inbuf.pbuf.rip, cc);
|
1996-08-10 05:28:58 +04:00
|
|
|
}
|
1995-06-21 02:25:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-08-10 05:28:58 +04:00
|
|
|
/* Process a RIP packet
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
1996-08-10 05:28:58 +04:00
|
|
|
static void
|
|
|
|
input(struct sockaddr_in *from, /* received from this IP address */
|
1997-02-04 01:02:51 +03:00
|
|
|
struct interface *sifp, /* interface of incoming socket */
|
|
|
|
struct interface *aifp, /* "authenticated" interface */
|
1996-08-10 05:28:58 +04:00
|
|
|
struct rip *rip,
|
1997-02-04 01:02:51 +03:00
|
|
|
int cc)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1996-08-10 05:28:58 +04:00
|
|
|
# define FROM_NADDR from->sin_addr.s_addr
|
1997-02-04 01:02:51 +03:00
|
|
|
static struct msg_limit use_auth, bad_len, bad_mask;
|
1998-06-02 22:02:55 +04:00
|
|
|
static struct msg_limit unk_router, bad_router, bad_nhop;
|
1996-08-10 05:28:58 +04:00
|
|
|
|
|
|
|
struct rt_entry *rt;
|
1998-06-02 22:02:55 +04:00
|
|
|
struct rt_spare new;
|
1996-08-10 05:28:58 +04:00
|
|
|
struct netinfo *n, *lim;
|
|
|
|
struct interface *ifp1;
|
1997-09-15 14:38:11 +04:00
|
|
|
naddr gate, mask, v1_mask, dst, ddst_h = 0;
|
1997-02-04 01:02:51 +03:00
|
|
|
struct auth *ap;
|
1998-06-02 22:02:55 +04:00
|
|
|
struct tgate *tg = 0;
|
|
|
|
struct tgate_net *tn;
|
|
|
|
int i, j;
|
1996-08-10 05:28:58 +04:00
|
|
|
|
1997-02-04 01:02:51 +03:00
|
|
|
/* Notice when we hear from a remote gateway
|
|
|
|
*/
|
|
|
|
if (aifp != 0
|
|
|
|
&& (aifp->int_state & IS_REMOTE))
|
|
|
|
aifp->int_act_time = now.tv_sec;
|
1996-08-10 05:28:58 +04:00
|
|
|
|
1997-02-04 01:02:51 +03:00
|
|
|
trace_rip("Recv", "from", from, sifp, rip, cc);
|
1996-08-10 05:28:58 +04:00
|
|
|
|
|
|
|
if (rip->rip_vers == 0) {
|
1997-02-04 01:02:51 +03:00
|
|
|
msglim(&bad_router, FROM_NADDR,
|
|
|
|
"RIP version 0, cmd %d, packet received from %s",
|
|
|
|
rip->rip_cmd, naddr_ntoa(FROM_NADDR));
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
1996-09-24 20:24:12 +04:00
|
|
|
} else if (rip->rip_vers > RIPv2) {
|
|
|
|
rip->rip_vers = RIPv2;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1997-02-04 01:02:51 +03:00
|
|
|
if (cc > OVER_MAXPACKETSIZE) {
|
|
|
|
msglim(&bad_router, FROM_NADDR,
|
|
|
|
"packet at least %d bytes too long received from %s",
|
|
|
|
cc-MAXPACKETSIZE, naddr_ntoa(FROM_NADDR));
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
}
|
1995-06-21 02:25:51 +04:00
|
|
|
|
1996-08-10 05:28:58 +04:00
|
|
|
n = rip->rip_nets;
|
1997-02-04 01:02:51 +03:00
|
|
|
lim = (struct netinfo *)((char*)rip + cc);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1996-08-10 05:28:58 +04:00
|
|
|
/* Notice authentication.
|
|
|
|
* As required by section 4.2 in RFC 1723, discard authenticated
|
|
|
|
* RIPv2 messages, but only if configured for that silliness.
|
|
|
|
*
|
1997-02-04 01:02:51 +03:00
|
|
|
* RIPv2 authentication is lame. Why authenticate queries?
|
1996-08-10 05:28:58 +04:00
|
|
|
* Why should a RIPv2 implementation with authentication disabled
|
|
|
|
* not be able to listen to RIPv2 packets with authenication, while
|
|
|
|
* RIPv1 systems will listen? Crazy!
|
|
|
|
*/
|
|
|
|
if (!auth_ok
|
1996-09-24 20:24:12 +04:00
|
|
|
&& rip->rip_vers == RIPv2
|
1996-08-10 05:28:58 +04:00
|
|
|
&& n < lim && n->n_family == RIP_AF_AUTH) {
|
1997-02-04 01:02:51 +03:00
|
|
|
msglim(&use_auth, FROM_NADDR,
|
|
|
|
"RIPv2 message with authentication from %s discarded",
|
|
|
|
naddr_ntoa(FROM_NADDR));
|
1996-08-10 05:28:58 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (rip->rip_cmd) {
|
1993-03-21 12:45:37 +03:00
|
|
|
case RIPCMD_REQUEST:
|
1997-02-04 01:02:51 +03:00
|
|
|
/* For mere requests, be a little sloppy about the source
|
1996-08-10 05:28:58 +04:00
|
|
|
*/
|
1997-02-04 01:02:51 +03:00
|
|
|
if (aifp == 0)
|
|
|
|
aifp = sifp;
|
|
|
|
|
|
|
|
/* Are we talking to ourself or a remote gateway?
|
|
|
|
*/
|
|
|
|
ifp1 = ifwithaddr(FROM_NADDR, 0, 1);
|
|
|
|
if (ifp1) {
|
|
|
|
if (ifp1->int_state & IS_REMOTE) {
|
|
|
|
/* remote gateway */
|
|
|
|
aifp = ifp1;
|
|
|
|
if (check_remote(aifp)) {
|
|
|
|
aifp->int_act_time = now.tv_sec;
|
|
|
|
(void)if_ok(aifp, "remote ");
|
|
|
|
}
|
|
|
|
} else if (from->sin_port == htons(RIP_PORT)) {
|
|
|
|
trace_pkt(" discard our own RIP request");
|
1996-08-10 05:28:58 +04:00
|
|
|
return;
|
|
|
|
}
|
1997-02-04 01:02:51 +03:00
|
|
|
}
|
1996-08-10 05:28:58 +04:00
|
|
|
|
1997-02-04 01:02:51 +03:00
|
|
|
/* did the request come from a router?
|
|
|
|
*/
|
|
|
|
if (from->sin_port == htons(RIP_PORT)) {
|
|
|
|
/* yes, ignore the request if RIP is off so that
|
|
|
|
* the router does not depend on us.
|
1996-08-10 05:28:58 +04:00
|
|
|
*/
|
1997-02-04 01:02:51 +03:00
|
|
|
if (rip_sock < 0
|
|
|
|
|| (aifp != 0
|
|
|
|
&& IS_RIP_OUT_OFF(aifp->int_state))) {
|
|
|
|
trace_pkt(" discard request while RIP off");
|
1996-08-10 05:28:58 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* According to RFC 1723, we should ignore unathenticated
|
|
|
|
* queries. That is too silly to bother with. Sheesh!
|
1997-02-04 01:02:51 +03:00
|
|
|
* Are forwarding tables supposed to be secret, when
|
|
|
|
* a bad guy can infer them with test traffic? When RIP
|
|
|
|
* is still the most common router-discovery protocol
|
|
|
|
* and so hosts need to send queries that will be answered?
|
|
|
|
* What about `rtquery`?
|
1996-08-10 05:28:58 +04:00
|
|
|
* Maybe on firewalls you'd care, but not enough to
|
|
|
|
* give up the diagnostic facilities of remote probing.
|
|
|
|
*/
|
|
|
|
|
1997-02-04 01:02:51 +03:00
|
|
|
if (n >= lim) {
|
|
|
|
msglim(&bad_len, FROM_NADDR, "empty request from %s",
|
|
|
|
naddr_ntoa(FROM_NADDR));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (cc%sizeof(*n) != sizeof(struct rip)%sizeof(*n)) {
|
|
|
|
msglim(&bad_len, FROM_NADDR,
|
|
|
|
"request of bad length (%d) from %s",
|
|
|
|
cc, naddr_ntoa(FROM_NADDR));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rip->rip_vers == RIPv2
|
|
|
|
&& (aifp == 0 || (aifp->int_state & IS_NO_RIPV1_OUT))) {
|
|
|
|
v12buf.buf->rip_vers = RIPv2;
|
|
|
|
/* If we have a secret but it is a cleartext secret,
|
|
|
|
* do not disclose our secret unless the other guy
|
|
|
|
* already knows it.
|
|
|
|
*/
|
|
|
|
ap = find_auth(aifp);
|
|
|
|
if (ap != 0 && ap->type == RIP_AUTH_PW
|
|
|
|
&& n->n_family == RIP_AF_AUTH
|
|
|
|
&& !ck_passwd(aifp,rip,lim,FROM_NADDR,&use_auth))
|
|
|
|
ap = 0;
|
|
|
|
} else {
|
|
|
|
v12buf.buf->rip_vers = RIPv1;
|
|
|
|
ap = 0;
|
1996-08-10 05:28:58 +04:00
|
|
|
}
|
1997-02-04 01:02:51 +03:00
|
|
|
clr_ws_buf(&v12buf, ap);
|
|
|
|
|
|
|
|
do {
|
|
|
|
NTOHL(n->n_metric);
|
1996-08-10 05:28:58 +04:00
|
|
|
|
|
|
|
/* A single entry with family RIP_AF_UNSPEC and
|
|
|
|
* metric HOPCNT_INFINITY means "all routes".
|
1993-03-21 12:45:37 +03:00
|
|
|
* We respond to routers only if we are acting
|
|
|
|
* as a supplier, or to anyone other than a router
|
1996-08-10 05:28:58 +04:00
|
|
|
* (i.e. a query).
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
1996-08-10 05:28:58 +04:00
|
|
|
if (n->n_family == RIP_AF_UNSPEC
|
1997-02-04 01:02:51 +03:00
|
|
|
&& n->n_metric == HOPCNT_INFINITY) {
|
1998-06-02 22:02:55 +04:00
|
|
|
/* Answer a query from a utility program
|
|
|
|
* with all we know.
|
|
|
|
*/
|
1996-08-10 05:28:58 +04:00
|
|
|
if (from->sin_port != htons(RIP_PORT)) {
|
1997-02-04 01:02:51 +03:00
|
|
|
supply(from, aifp, OUT_QUERY, 0,
|
|
|
|
rip->rip_vers, ap != 0);
|
1996-09-24 20:24:12 +04:00
|
|
|
return;
|
1996-08-10 05:28:58 +04:00
|
|
|
}
|
1997-02-04 01:02:51 +03:00
|
|
|
|
1996-09-24 20:24:12 +04:00
|
|
|
/* A router trying to prime its tables.
|
|
|
|
* Filter the answer in the about same way
|
|
|
|
* broadcasts are filtered.
|
|
|
|
*
|
|
|
|
* Only answer a router if we are a supplier
|
|
|
|
* to keep an unwary host that is just starting
|
1998-06-02 22:02:55 +04:00
|
|
|
* from picking us as a router.
|
1996-09-24 20:24:12 +04:00
|
|
|
*/
|
1997-02-04 01:02:51 +03:00
|
|
|
if (aifp == 0) {
|
|
|
|
trace_pkt("ignore distant router");
|
|
|
|
return;
|
|
|
|
}
|
1996-09-24 20:24:12 +04:00
|
|
|
if (!supplier
|
1997-02-04 01:02:51 +03:00
|
|
|
|| IS_RIP_OFF(aifp->int_state)) {
|
|
|
|
trace_pkt("ignore; not supplying");
|
1996-09-24 20:24:12 +04:00
|
|
|
return;
|
1997-02-04 01:02:51 +03:00
|
|
|
}
|
1996-09-24 20:24:12 +04:00
|
|
|
|
1998-06-02 22:02:55 +04:00
|
|
|
/* Do not answer a RIPv1 router if
|
|
|
|
* we are sending RIPv2. But do offer
|
|
|
|
* poor man's router discovery.
|
|
|
|
*/
|
|
|
|
if ((aifp->int_state & IS_NO_RIPV1_OUT)
|
|
|
|
&& rip->rip_vers == RIPv1) {
|
|
|
|
if (!(aifp->int_state & IS_PM_RDISC)) {
|
|
|
|
trace_pkt("ignore; sending RIPv2");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
v12buf.n->n_family = RIP_AF_INET;
|
|
|
|
v12buf.n->n_dst = RIP_DEFAULT;
|
|
|
|
i = aifp->int_d_metric;
|
|
|
|
if (0 != (rt = rtget(RIP_DEFAULT, 0)))
|
|
|
|
i = MIN(i, (rt->rt_metric
|
|
|
|
+aifp->int_metric+1));
|
|
|
|
v12buf.n->n_metric = htonl(i);
|
|
|
|
v12buf.n++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Respond with RIPv1 instead of RIPv2 if
|
|
|
|
* that is what we are broadcasting on the
|
|
|
|
* interface to keep the remote router from
|
|
|
|
* getting the wrong initial idea of the
|
|
|
|
* routes we send.
|
|
|
|
*/
|
1996-09-24 20:24:12 +04:00
|
|
|
supply(from, aifp, OUT_UNICAST, 0,
|
1998-06-02 22:02:55 +04:00
|
|
|
(aifp->int_state & IS_NO_RIPV1_OUT)
|
1997-02-04 01:02:51 +03:00
|
|
|
? RIPv2 : RIPv1,
|
|
|
|
ap != 0);
|
1996-08-10 05:28:58 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1997-02-04 01:02:51 +03:00
|
|
|
/* Ignore authentication */
|
|
|
|
if (n->n_family == RIP_AF_AUTH)
|
|
|
|
continue;
|
|
|
|
|
1996-08-10 05:28:58 +04:00
|
|
|
if (n->n_family != RIP_AF_INET) {
|
1997-02-04 01:02:51 +03:00
|
|
|
msglim(&bad_router, FROM_NADDR,
|
1998-06-02 22:02:55 +04:00
|
|
|
"request from %s for unsupported"
|
|
|
|
" (af %d) %s",
|
1997-02-04 01:02:51 +03:00
|
|
|
naddr_ntoa(FROM_NADDR),
|
|
|
|
ntohs(n->n_family),
|
|
|
|
naddr_ntoa(n->n_dst));
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
}
|
1996-08-10 05:28:58 +04:00
|
|
|
|
1997-02-04 01:02:51 +03:00
|
|
|
/* We are being asked about a specific destination.
|
|
|
|
*/
|
1996-08-10 05:28:58 +04:00
|
|
|
dst = n->n_dst;
|
|
|
|
if (!check_dst(dst)) {
|
1997-02-04 01:02:51 +03:00
|
|
|
msglim(&bad_router, FROM_NADDR,
|
|
|
|
"bad queried destination %s from %s",
|
|
|
|
naddr_ntoa(dst),
|
|
|
|
naddr_ntoa(FROM_NADDR));
|
1996-08-10 05:28:58 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1997-02-04 01:02:51 +03:00
|
|
|
/* decide what mask was intended */
|
1996-08-10 05:28:58 +04:00
|
|
|
if (rip->rip_vers == RIPv1
|
|
|
|
|| 0 == (mask = ntohl(n->n_mask))
|
|
|
|
|| 0 != (ntohl(dst) & ~mask))
|
1997-02-04 01:02:51 +03:00
|
|
|
mask = ripv1_mask_host(dst, aifp);
|
1996-08-10 05:28:58 +04:00
|
|
|
|
1997-02-04 01:02:51 +03:00
|
|
|
/* try to find the answer */
|
1996-08-10 05:28:58 +04:00
|
|
|
rt = rtget(dst, mask);
|
|
|
|
if (!rt && dst != RIP_DEFAULT)
|
|
|
|
rt = rtfind(n->n_dst);
|
|
|
|
|
1997-02-04 01:02:51 +03:00
|
|
|
if (v12buf.buf->rip_vers != RIPv1)
|
|
|
|
v12buf.n->n_mask = mask;
|
1996-08-10 05:28:58 +04:00
|
|
|
if (rt == 0) {
|
1997-02-04 01:02:51 +03:00
|
|
|
/* we do not have the answer */
|
|
|
|
v12buf.n->n_metric = HOPCNT_INFINITY;
|
1996-08-10 05:28:58 +04:00
|
|
|
} else {
|
1997-02-04 01:02:51 +03:00
|
|
|
/* we have the answer, so compute the
|
|
|
|
* right metric and next hop.
|
|
|
|
*/
|
|
|
|
v12buf.n->n_family = RIP_AF_INET;
|
|
|
|
v12buf.n->n_dst = dst;
|
|
|
|
v12buf.n->n_metric = (rt->rt_metric+1
|
|
|
|
+ ((aifp!=0)
|
|
|
|
? aifp->int_metric
|
|
|
|
: 1));
|
|
|
|
if (v12buf.n->n_metric > HOPCNT_INFINITY)
|
|
|
|
v12buf.n->n_metric = HOPCNT_INFINITY;
|
|
|
|
if (v12buf.buf->rip_vers != RIPv1) {
|
|
|
|
v12buf.n->n_tag = rt->rt_tag;
|
|
|
|
v12buf.n->n_mask = mask;
|
|
|
|
if (aifp != 0
|
1996-08-10 05:28:58 +04:00
|
|
|
&& on_net(rt->rt_gate,
|
1997-02-04 01:02:51 +03:00
|
|
|
aifp->int_net,
|
|
|
|
aifp->int_mask)
|
|
|
|
&& rt->rt_gate != aifp->int_addr)
|
|
|
|
v12buf.n->n_nhop = rt->rt_gate;
|
1996-08-10 05:28:58 +04:00
|
|
|
}
|
1995-06-21 02:25:51 +04:00
|
|
|
}
|
1997-02-04 01:02:51 +03:00
|
|
|
HTONL(v12buf.n->n_metric);
|
|
|
|
|
|
|
|
/* Stop paying attention if we fill the output buffer.
|
|
|
|
*/
|
|
|
|
if (++v12buf.n >= v12buf.lim)
|
|
|
|
break;
|
|
|
|
} while (++n < lim);
|
|
|
|
|
|
|
|
/* Send the answer about specific routes.
|
1996-08-10 05:28:58 +04:00
|
|
|
*/
|
1997-02-04 01:02:51 +03:00
|
|
|
if (ap != 0 && ap->type == RIP_AUTH_MD5)
|
|
|
|
end_md5_auth(&v12buf, ap);
|
|
|
|
|
1996-08-10 05:28:58 +04:00
|
|
|
if (from->sin_port != htons(RIP_PORT)) {
|
|
|
|
/* query */
|
1997-02-04 01:02:51 +03:00
|
|
|
(void)output(OUT_QUERY, from, aifp,
|
|
|
|
v12buf.buf,
|
|
|
|
((char *)v12buf.n - (char*)v12buf.buf));
|
1996-08-10 05:28:58 +04:00
|
|
|
} else if (supplier) {
|
1997-02-04 01:02:51 +03:00
|
|
|
(void)output(OUT_UNICAST, from, aifp,
|
|
|
|
v12buf.buf,
|
|
|
|
((char *)v12buf.n - (char*)v12buf.buf));
|
|
|
|
} else {
|
|
|
|
/* Only answer a router if we are a supplier
|
|
|
|
* to keep an unwary host that is just starting
|
|
|
|
* from picking us an a router.
|
|
|
|
*/
|
|
|
|
;
|
1996-08-10 05:28:58 +04:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
case RIPCMD_TRACEON:
|
|
|
|
case RIPCMD_TRACEOFF:
|
|
|
|
/* verify message came from a privileged port */
|
1996-08-10 05:28:58 +04:00
|
|
|
if (ntohs(from->sin_port) > IPPORT_RESERVED) {
|
|
|
|
msglog("trace command from untrusted port on %s",
|
|
|
|
naddr_ntoa(FROM_NADDR));
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
1996-08-10 05:28:58 +04:00
|
|
|
}
|
1996-09-24 20:24:12 +04:00
|
|
|
if (aifp == 0) {
|
1996-08-10 05:28:58 +04:00
|
|
|
msglog("trace command from unknown router %s",
|
|
|
|
naddr_ntoa(FROM_NADDR));
|
1995-05-24 19:22:52 +04:00
|
|
|
return;
|
1996-08-10 05:28:58 +04:00
|
|
|
}
|
|
|
|
if (rip->rip_cmd == RIPCMD_TRACEON) {
|
1997-02-04 01:02:51 +03:00
|
|
|
rip->rip_tracefile[cc-4] = '\0';
|
|
|
|
set_tracefile((char*)rip->rip_tracefile,
|
|
|
|
"trace command: %s\n", 0);
|
1996-08-10 05:28:58 +04:00
|
|
|
} else {
|
1997-02-04 01:02:51 +03:00
|
|
|
trace_off("tracing turned off by %s",
|
1996-08-10 05:28:58 +04:00
|
|
|
naddr_ntoa(FROM_NADDR));
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
case RIPCMD_RESPONSE:
|
1997-02-04 01:02:51 +03:00
|
|
|
if (cc%sizeof(*n) != sizeof(struct rip)%sizeof(*n)) {
|
|
|
|
msglim(&bad_len, FROM_NADDR,
|
|
|
|
"response of bad length (%d) from %s",
|
|
|
|
cc, naddr_ntoa(FROM_NADDR));
|
1996-08-10 05:28:58 +04:00
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/* verify message came from a router */
|
1996-08-10 05:28:58 +04:00
|
|
|
if (from->sin_port != ntohs(RIP_PORT)) {
|
1997-02-04 01:02:51 +03:00
|
|
|
msglim(&bad_router, FROM_NADDR,
|
|
|
|
" discard RIP response from unknown port"
|
|
|
|
" %d", from->sin_port);
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
1996-08-10 05:28:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rip_sock < 0) {
|
1997-02-04 01:02:51 +03:00
|
|
|
trace_pkt(" discard response while RIP off");
|
1996-08-10 05:28:58 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Are we talking to ourself or a remote gateway?
|
|
|
|
*/
|
|
|
|
ifp1 = ifwithaddr(FROM_NADDR, 0, 1);
|
|
|
|
if (ifp1) {
|
|
|
|
if (ifp1->int_state & IS_REMOTE) {
|
1997-02-04 01:02:51 +03:00
|
|
|
/* remote gateway */
|
|
|
|
aifp = ifp1;
|
|
|
|
if (check_remote(aifp)) {
|
|
|
|
aifp->int_act_time = now.tv_sec;
|
|
|
|
(void)if_ok(aifp, "remote ");
|
1996-08-10 05:28:58 +04:00
|
|
|
}
|
|
|
|
} else {
|
1997-02-04 01:02:51 +03:00
|
|
|
trace_pkt(" discard our own RIP response");
|
|
|
|
return;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
1996-08-10 05:28:58 +04:00
|
|
|
|
1997-02-04 01:02:51 +03:00
|
|
|
/* Accept routing packets from routers directly connected
|
|
|
|
* via broadcast or point-to-point networks, and from
|
1996-08-10 05:28:58 +04:00
|
|
|
* those listed in /etc/gateways.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
1997-02-04 01:02:51 +03:00
|
|
|
if (aifp == 0) {
|
|
|
|
msglim(&unk_router, FROM_NADDR,
|
|
|
|
" discard response from %s"
|
|
|
|
" via unexpected interface",
|
|
|
|
naddr_ntoa(FROM_NADDR));
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
1996-08-10 05:28:58 +04:00
|
|
|
}
|
1997-02-04 01:02:51 +03:00
|
|
|
if (IS_RIP_IN_OFF(aifp->int_state)) {
|
|
|
|
trace_pkt(" discard RIPv%d response"
|
|
|
|
" via disabled interface %s",
|
|
|
|
rip->rip_vers, aifp->int_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n >= lim) {
|
|
|
|
msglim(&bad_len, FROM_NADDR, "empty response from %s",
|
|
|
|
naddr_ntoa(FROM_NADDR));
|
1996-08-10 05:28:58 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1996-09-24 20:24:12 +04:00
|
|
|
if (((aifp->int_state & IS_NO_RIPV1_IN)
|
1996-08-10 05:28:58 +04:00
|
|
|
&& rip->rip_vers == RIPv1)
|
1996-09-24 20:24:12 +04:00
|
|
|
|| ((aifp->int_state & IS_NO_RIPV2_IN)
|
1996-08-10 05:28:58 +04:00
|
|
|
&& rip->rip_vers != RIPv1)) {
|
1997-02-04 01:02:51 +03:00
|
|
|
trace_pkt(" discard RIPv%d response",
|
1996-08-10 05:28:58 +04:00
|
|
|
rip->rip_vers);
|
|
|
|
return;
|
|
|
|
}
|
1995-06-21 02:25:51 +04:00
|
|
|
|
1996-08-10 05:28:58 +04:00
|
|
|
/* Ignore routes via dead interface.
|
|
|
|
*/
|
1996-09-24 20:24:12 +04:00
|
|
|
if (aifp->int_state & IS_BROKE) {
|
1998-06-02 22:02:55 +04:00
|
|
|
trace_pkt("discard response via broken interface %s",
|
1996-09-24 20:24:12 +04:00
|
|
|
aifp->int_name);
|
1996-08-10 05:28:58 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1997-02-04 01:02:51 +03:00
|
|
|
/* If the interface cares, ignore bad routers.
|
|
|
|
* Trace but do not log this problem, because where it
|
|
|
|
* happens, it happens frequently.
|
1996-08-10 05:28:58 +04:00
|
|
|
*/
|
1997-02-04 01:02:51 +03:00
|
|
|
if (aifp->int_state & IS_DISTRUST) {
|
1998-06-02 22:02:55 +04:00
|
|
|
tg = tgates;
|
1997-02-04 01:02:51 +03:00
|
|
|
while (tg->tgate_addr != FROM_NADDR) {
|
|
|
|
tg = tg->tgate_next;
|
|
|
|
if (tg == 0) {
|
|
|
|
trace_pkt(" discard RIP response"
|
|
|
|
" from untrusted router %s",
|
|
|
|
naddr_ntoa(FROM_NADDR));
|
|
|
|
return;
|
|
|
|
}
|
1996-09-24 20:24:12 +04:00
|
|
|
}
|
1996-08-10 05:28:58 +04:00
|
|
|
}
|
|
|
|
|
1997-02-04 01:02:51 +03:00
|
|
|
/* Authenticate the packet if we have a secret.
|
|
|
|
* If we do not have any secrets, ignore the error in
|
|
|
|
* RFC 1723 and accept it regardless.
|
|
|
|
*/
|
|
|
|
if (aifp->int_auth[0].type != RIP_AUTH_NONE
|
|
|
|
&& rip->rip_vers != RIPv1
|
|
|
|
&& !ck_passwd(aifp,rip,lim,FROM_NADDR,&use_auth))
|
|
|
|
return;
|
1996-08-10 05:28:58 +04:00
|
|
|
|
1997-02-04 01:02:51 +03:00
|
|
|
do {
|
1996-08-10 05:28:58 +04:00
|
|
|
if (n->n_family == RIP_AF_AUTH)
|
1995-06-21 02:25:51 +04:00
|
|
|
continue;
|
1996-08-10 05:28:58 +04:00
|
|
|
|
|
|
|
NTOHL(n->n_metric);
|
|
|
|
dst = n->n_dst;
|
|
|
|
if (n->n_family != RIP_AF_INET
|
|
|
|
&& (n->n_family != RIP_AF_UNSPEC
|
|
|
|
|| dst != RIP_DEFAULT)) {
|
1997-02-04 01:02:51 +03:00
|
|
|
msglim(&bad_router, FROM_NADDR,
|
|
|
|
"route from %s to unsupported"
|
|
|
|
" address family=%d destination=%s",
|
|
|
|
naddr_ntoa(FROM_NADDR),
|
|
|
|
n->n_family,
|
|
|
|
naddr_ntoa(dst));
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
}
|
1996-08-10 05:28:58 +04:00
|
|
|
if (!check_dst(dst)) {
|
1997-02-04 01:02:51 +03:00
|
|
|
msglim(&bad_router, FROM_NADDR,
|
|
|
|
"bad destination %s from %s",
|
|
|
|
naddr_ntoa(dst),
|
|
|
|
naddr_ntoa(FROM_NADDR));
|
1996-08-10 05:28:58 +04:00
|
|
|
return;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1996-08-10 05:28:58 +04:00
|
|
|
if (n->n_metric == 0
|
|
|
|
|| n->n_metric > HOPCNT_INFINITY) {
|
1997-02-04 01:02:51 +03:00
|
|
|
msglim(&bad_router, FROM_NADDR,
|
|
|
|
"bad metric %d from %s"
|
|
|
|
" for destination %s",
|
|
|
|
n->n_metric,
|
|
|
|
naddr_ntoa(FROM_NADDR),
|
|
|
|
naddr_ntoa(dst));
|
1996-08-10 05:28:58 +04:00
|
|
|
return;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1996-08-10 05:28:58 +04:00
|
|
|
|
|
|
|
/* Notice the next-hop.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
1997-02-04 01:02:51 +03:00
|
|
|
gate = FROM_NADDR;
|
1996-09-24 20:24:12 +04:00
|
|
|
if (n->n_nhop != 0) {
|
|
|
|
if (rip->rip_vers == RIPv2) {
|
|
|
|
n->n_nhop = 0;
|
1996-08-10 05:28:58 +04:00
|
|
|
} else {
|
1996-09-24 20:24:12 +04:00
|
|
|
/* Use it only if it is valid. */
|
|
|
|
if (on_net(n->n_nhop,
|
|
|
|
aifp->int_net, aifp->int_mask)
|
|
|
|
&& check_dst(n->n_nhop)) {
|
|
|
|
gate = n->n_nhop;
|
|
|
|
} else {
|
1997-02-04 01:02:51 +03:00
|
|
|
msglim(&bad_nhop, FROM_NADDR,
|
|
|
|
"router %s to %s"
|
|
|
|
" has bad next hop %s",
|
|
|
|
naddr_ntoa(FROM_NADDR),
|
|
|
|
naddr_ntoa(dst),
|
|
|
|
naddr_ntoa(n->n_nhop));
|
|
|
|
n->n_nhop = 0;
|
1996-09-24 20:24:12 +04:00
|
|
|
}
|
1996-08-10 05:28:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rip->rip_vers == RIPv1
|
|
|
|
|| 0 == (mask = ntohl(n->n_mask))) {
|
1996-09-24 20:24:12 +04:00
|
|
|
mask = ripv1_mask_host(dst,aifp);
|
1996-08-10 05:28:58 +04:00
|
|
|
} else if ((ntohl(dst) & ~mask) != 0) {
|
1997-02-04 01:02:51 +03:00
|
|
|
msglim(&bad_mask, FROM_NADDR,
|
|
|
|
"router %s sent bad netmask"
|
|
|
|
" %#x with %s",
|
|
|
|
naddr_ntoa(FROM_NADDR),
|
|
|
|
mask,
|
|
|
|
naddr_ntoa(dst));
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
}
|
1996-08-10 05:28:58 +04:00
|
|
|
if (rip->rip_vers == RIPv1)
|
|
|
|
n->n_tag = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1996-08-10 05:28:58 +04:00
|
|
|
/* Adjust metric according to incoming interface..
|
|
|
|
*/
|
1996-09-24 20:24:12 +04:00
|
|
|
n->n_metric += aifp->int_metric;
|
1996-08-10 05:28:58 +04:00
|
|
|
if (n->n_metric > HOPCNT_INFINITY)
|
|
|
|
n->n_metric = HOPCNT_INFINITY;
|
|
|
|
|
1998-06-02 22:02:55 +04:00
|
|
|
/* Should we trust this route from this router? */
|
|
|
|
if (tg && (tn = tg->tgate_nets)->mask != 0) {
|
|
|
|
for (i = 0; i < MAX_TGATE_NETS; i++, tn++) {
|
|
|
|
if (on_net(dst, tn->net, tn->mask)
|
|
|
|
&& tn->mask <= mask)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i >= MAX_TGATE_NETS || tn->mask == 0) {
|
|
|
|
trace_pkt(" ignored unauthorized %s",
|
|
|
|
addrname(dst,mask,0));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-08-10 05:28:58 +04:00
|
|
|
/* Recognize and ignore a default route we faked
|
|
|
|
* which is being sent back to us by a machine with
|
|
|
|
* broken split-horizon.
|
|
|
|
* Be a little more paranoid than that, and reject
|
|
|
|
* default routes with the same metric we advertised.
|
|
|
|
*/
|
1996-09-24 20:24:12 +04:00
|
|
|
if (aifp->int_d_metric != 0
|
1996-08-10 05:28:58 +04:00
|
|
|
&& dst == RIP_DEFAULT
|
1996-09-24 20:24:12 +04:00
|
|
|
&& n->n_metric >= aifp->int_d_metric)
|
1996-08-10 05:28:58 +04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* We can receive aggregated RIPv2 routes that must
|
|
|
|
* be broken down before they are transmitted by
|
|
|
|
* RIPv1 via an interface on a subnet.
|
|
|
|
* We might also receive the same routes aggregated
|
|
|
|
* via other RIPv2 interfaces.
|
|
|
|
* This could cause duplicate routes to be sent on
|
|
|
|
* the RIPv1 interfaces. "Longest matching variable
|
|
|
|
* length netmasks" lets RIPv2 listeners understand,
|
|
|
|
* but breaking down the aggregated routes for RIPv1
|
|
|
|
* listeners can produce duplicate routes.
|
|
|
|
*
|
|
|
|
* Breaking down aggregated routes here bloats
|
|
|
|
* the daemon table, but does not hurt the kernel
|
|
|
|
* table, since routes are always aggregated for
|
|
|
|
* the kernel.
|
|
|
|
*
|
|
|
|
* Notice that this does not break down network
|
|
|
|
* routes corresponding to subnets. This is part
|
|
|
|
* of the defense against RS_NET_SYN.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
1996-08-10 05:28:58 +04:00
|
|
|
if (have_ripv1_out
|
|
|
|
&& (((rt = rtget(dst,mask)) == 0
|
1997-02-04 01:02:51 +03:00
|
|
|
|| !(rt->rt_state & RS_NET_SYN)))
|
|
|
|
&& (v1_mask = ripv1_mask_net(dst,0)) > mask) {
|
1996-08-10 05:28:58 +04:00
|
|
|
ddst_h = v1_mask & -v1_mask;
|
|
|
|
i = (v1_mask & ~mask)/ddst_h;
|
1996-09-24 20:24:12 +04:00
|
|
|
if (i >= 511) {
|
1996-08-10 05:28:58 +04:00
|
|
|
/* Punt if we would have to generate
|
|
|
|
* an unreasonable number of routes.
|
|
|
|
*/
|
1998-06-02 22:02:55 +04:00
|
|
|
if (TRACECONTENTS)
|
|
|
|
trace_misc("accept %s-->%s as 1"
|
|
|
|
" instead of %d routes",
|
|
|
|
addrname(dst,mask,0),
|
|
|
|
naddr_ntoa(FROM_NADDR),
|
|
|
|
i+1);
|
1996-08-10 05:28:58 +04:00
|
|
|
i = 0;
|
|
|
|
} else {
|
|
|
|
mask = v1_mask;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
|
1998-06-02 22:02:55 +04:00
|
|
|
new.rts_gate = gate;
|
|
|
|
new.rts_router = FROM_NADDR;
|
|
|
|
new.rts_metric = n->n_metric;
|
|
|
|
new.rts_tag = n->n_tag;
|
|
|
|
new.rts_time = now.tv_sec;
|
|
|
|
new.rts_ifp = aifp;
|
|
|
|
new.rts_de_ag = i;
|
|
|
|
j = 0;
|
1996-08-10 05:28:58 +04:00
|
|
|
for (;;) {
|
1998-06-02 22:02:55 +04:00
|
|
|
input_route(dst, mask, &new, n);
|
|
|
|
if (++j > i)
|
1996-08-10 05:28:58 +04:00
|
|
|
break;
|
|
|
|
dst = htonl(ntohl(dst) + ddst_h);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1997-02-04 01:02:51 +03:00
|
|
|
} while (++n < lim);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
}
|
1997-02-04 01:02:51 +03:00
|
|
|
#undef FROM_NADDR
|
1996-08-10 05:28:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Process a single input route.
|
|
|
|
*/
|
|
|
|
static void
|
1998-06-02 22:02:55 +04:00
|
|
|
input_route(naddr dst, /* network order */
|
1996-08-10 05:28:58 +04:00
|
|
|
naddr mask,
|
1998-06-02 22:02:55 +04:00
|
|
|
struct rt_spare *new,
|
1996-08-10 05:28:58 +04:00
|
|
|
struct netinfo *n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct rt_entry *rt;
|
|
|
|
struct rt_spare *rts, *rts0;
|
|
|
|
struct interface *ifp1;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1996-08-10 05:28:58 +04:00
|
|
|
|
|
|
|
/* See if the other guy is telling us to send our packets to him.
|
|
|
|
* Sometimes network routes arrive over a point-to-point link for
|
|
|
|
* the network containing the address(es) of the link.
|
|
|
|
*
|
|
|
|
* If our interface is broken, switch to using the other guy.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
1996-08-10 05:28:58 +04:00
|
|
|
ifp1 = ifwithaddr(dst, 1, 1);
|
|
|
|
if (ifp1 != 0
|
1997-02-04 01:02:51 +03:00
|
|
|
&& (!(ifp1->int_state & IS_BROKE)
|
|
|
|
|| (ifp1->int_state & IS_PASSIVE)))
|
1996-08-10 05:28:58 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Look for the route in our table.
|
|
|
|
*/
|
|
|
|
rt = rtget(dst, mask);
|
|
|
|
|
|
|
|
/* Consider adding the route if we do not already have it.
|
|
|
|
*/
|
|
|
|
if (rt == 0) {
|
|
|
|
/* Ignore unknown routes being poisoned.
|
|
|
|
*/
|
1998-06-02 22:02:55 +04:00
|
|
|
if (new->rts_metric == HOPCNT_INFINITY)
|
1996-08-10 05:28:58 +04:00
|
|
|
return;
|
|
|
|
|
1996-09-24 20:24:12 +04:00
|
|
|
/* Ignore the route if it points to us */
|
|
|
|
if (n->n_nhop != 0
|
|
|
|
&& 0 != ifwithaddr(n->n_nhop, 1, 0))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* If something has not gone crazy and tried to fill
|
|
|
|
* our memory, accept the new route.
|
|
|
|
*/
|
|
|
|
if (total_routes < MAX_ROUTES)
|
1998-06-02 22:02:55 +04:00
|
|
|
rtadd(dst, mask, 0, new);
|
1996-08-10 05:28:58 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We already know about the route. Consider this update.
|
|
|
|
*
|
|
|
|
* If (rt->rt_state & RS_NET_SYN), then this route
|
|
|
|
* is the same as a network route we have inferred
|
|
|
|
* for subnets we know, in order to tell RIPv1 routers
|
|
|
|
* about the subnets.
|
|
|
|
*
|
|
|
|
* It is impossible to tell if the route is coming
|
|
|
|
* from a distant RIPv2 router with the standard
|
|
|
|
* netmask because that router knows about the entire
|
|
|
|
* network, or if it is a round-about echo of a
|
|
|
|
* synthetic, RIPv1 network route of our own.
|
|
|
|
* The worst is that both kinds of routes might be
|
|
|
|
* received, and the bad one might have the smaller
|
1996-09-24 20:24:12 +04:00
|
|
|
* metric. Partly solve this problem by never
|
|
|
|
* aggregating into such a route. Also keep it
|
1996-08-10 05:28:58 +04:00
|
|
|
* around as long as the interface exists.
|
|
|
|
*/
|
|
|
|
|
|
|
|
rts0 = rt->rt_spares;
|
|
|
|
for (rts = rts0, i = NUM_SPARES; i != 0; i--, rts++) {
|
1998-06-02 22:02:55 +04:00
|
|
|
if (rts->rts_router == new->rts_router)
|
1996-08-10 05:28:58 +04:00
|
|
|
break;
|
|
|
|
/* Note the worst slot to reuse,
|
|
|
|
* other than the current slot.
|
|
|
|
*/
|
|
|
|
if (rts0 == rt->rt_spares
|
|
|
|
|| BETTER_LINK(rt, rts0, rts))
|
|
|
|
rts0 = rts;
|
|
|
|
}
|
|
|
|
if (i != 0) {
|
1998-06-02 22:02:55 +04:00
|
|
|
/* Found a route from the router already in the table.
|
1996-08-10 05:28:58 +04:00
|
|
|
*/
|
1998-06-02 22:02:55 +04:00
|
|
|
|
|
|
|
/* If the new route is a route broken down from an
|
|
|
|
* aggregated route, and if the previous route is either
|
|
|
|
* not a broken down route or was broken down from a finer
|
|
|
|
* netmask, and if the previous route is current,
|
|
|
|
* then forget this one.
|
|
|
|
*/
|
|
|
|
if (new->rts_de_ag > rts->rts_de_ag
|
|
|
|
&& now_stale <= rts->rts_time)
|
|
|
|
return;
|
1996-08-10 05:28:58 +04:00
|
|
|
|
1996-09-24 20:24:12 +04:00
|
|
|
/* Keep poisoned routes around only long enough to pass
|
1998-06-02 22:02:55 +04:00
|
|
|
* the poison on. Use a new timestamp for good routes.
|
1996-08-10 05:28:58 +04:00
|
|
|
*/
|
1998-06-02 22:02:55 +04:00
|
|
|
if (rts->rts_metric == HOPCNT_INFINITY
|
|
|
|
&& new->rts_metric == HOPCNT_INFINITY)
|
|
|
|
new->rts_time = rts->rts_time;
|
1996-08-10 05:28:58 +04:00
|
|
|
|
|
|
|
/* If this is an update for the router we currently prefer,
|
|
|
|
* then note it.
|
|
|
|
*/
|
|
|
|
if (i == NUM_SPARES) {
|
1998-06-02 22:02:55 +04:00
|
|
|
rtchange(rt, rt->rt_state, new, 0);
|
1996-08-10 05:28:58 +04:00
|
|
|
/* If the route got worse, check for something better.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
1998-06-02 22:02:55 +04:00
|
|
|
if (new->rts_metric > rts->rts_metric)
|
1996-08-10 05:28:58 +04:00
|
|
|
rtswitch(rt, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is an update for a spare route.
|
|
|
|
* Finished if the route is unchanged.
|
|
|
|
*/
|
1998-06-02 22:02:55 +04:00
|
|
|
if (rts->rts_gate == new->rts_gate
|
|
|
|
&& rts->rts_metric == new->rts_metric
|
|
|
|
&& rts->rts_tag == new->rts_tag) {
|
|
|
|
trace_upslot(rt, rts, new);
|
|
|
|
*rts = *new;
|
1996-08-10 05:28:58 +04:00
|
|
|
return;
|
1998-06-02 22:02:55 +04:00
|
|
|
}
|
|
|
|
/* Forget it if it has gone bad.
|
|
|
|
*/
|
|
|
|
if (new->rts_metric == HOPCNT_INFINITY) {
|
1997-02-04 01:02:51 +03:00
|
|
|
rts_delete(rt, rts);
|
|
|
|
return;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1996-08-10 05:28:58 +04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
/* The update is for a route we know about,
|
|
|
|
* but not from a familiar router.
|
1996-09-24 20:24:12 +04:00
|
|
|
*
|
|
|
|
* Ignore the route if it points to us.
|
1996-08-10 05:28:58 +04:00
|
|
|
*/
|
1996-09-24 20:24:12 +04:00
|
|
|
if (n->n_nhop != 0
|
|
|
|
&& 0 != ifwithaddr(n->n_nhop, 1, 0))
|
|
|
|
return;
|
|
|
|
|
1998-06-02 22:02:55 +04:00
|
|
|
/* the loop above set rts0=worst spare */
|
1996-08-10 05:28:58 +04:00
|
|
|
rts = rts0;
|
|
|
|
|
|
|
|
/* Save the route as a spare only if it has
|
|
|
|
* a better metric than our worst spare.
|
|
|
|
* This also ignores poisoned routes (those
|
|
|
|
* received with metric HOPCNT_INFINITY).
|
|
|
|
*/
|
1998-06-02 22:02:55 +04:00
|
|
|
if (new->rts_metric >= rts->rts_metric)
|
1996-08-10 05:28:58 +04:00
|
|
|
return;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1996-08-10 05:28:58 +04:00
|
|
|
|
1998-06-02 22:02:55 +04:00
|
|
|
trace_upslot(rt, rts, new);
|
|
|
|
*rts = *new;
|
1996-08-10 05:28:58 +04:00
|
|
|
|
|
|
|
/* try to switch to a better route */
|
|
|
|
rtswitch(rt, rts);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1997-02-04 01:02:51 +03:00
|
|
|
|
|
|
|
|
|
|
|
static int /* 0 if bad */
|
|
|
|
ck_passwd(struct interface *aifp,
|
|
|
|
struct rip *rip,
|
|
|
|
void *lim,
|
|
|
|
naddr from,
|
|
|
|
struct msg_limit *use_authp)
|
|
|
|
{
|
|
|
|
# define NA (rip->rip_auths)
|
|
|
|
struct netauth *na2;
|
|
|
|
struct auth *ap;
|
|
|
|
MD5_CTX md5_ctx;
|
|
|
|
u_char hash[RIP_AUTH_PW_LEN];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
|
|
if ((void *)NA >= lim || NA->a_family != RIP_AF_AUTH) {
|
|
|
|
msglim(use_authp, from, "missing password from %s",
|
|
|
|
naddr_ntoa(from));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* accept any current (+/- 24 hours) password
|
|
|
|
*/
|
|
|
|
for (ap = aifp->int_auth, i = 0; i < MAX_AUTH_KEYS; i++, ap++) {
|
|
|
|
if (ap->type != NA->a_type
|
|
|
|
|| (u_long)ap->start > (u_long)clk.tv_sec+DAY
|
|
|
|
|| (u_long)ap->end+DAY < (u_long)clk.tv_sec)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (NA->a_type == RIP_AUTH_PW) {
|
1997-09-15 15:51:54 +04:00
|
|
|
if (!memcmp(ap->key, NA->au.au_pw, RIP_AUTH_PW_LEN))
|
1997-02-04 01:02:51 +03:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* accept MD5 secret with the right key ID
|
|
|
|
*/
|
|
|
|
if (NA->au.a_md5.md5_keyid != ap->keyid)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
na2 = (struct netauth *)((char *)(NA+1)
|
|
|
|
+ NA->au.a_md5.md5_pkt_len);
|
|
|
|
if (NA->au.a_md5.md5_pkt_len % sizeof(*NA) != 0
|
|
|
|
|| lim < (void *)(na2+1)) {
|
|
|
|
msglim(use_authp, from,
|
|
|
|
"bad MD5 RIP-II pkt length %d from %s",
|
|
|
|
NA->au.a_md5.md5_pkt_len,
|
|
|
|
naddr_ntoa(from));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
MD5Init(&md5_ctx);
|
|
|
|
MD5Update(&md5_ctx, (u_char *)NA,
|
|
|
|
(char *)na2->au.au_pw - (char *)NA);
|
|
|
|
MD5Update(&md5_ctx,
|
|
|
|
(u_char *)ap->key, sizeof(ap->key));
|
|
|
|
MD5Final(hash, &md5_ctx);
|
|
|
|
if (na2->a_family != RIP_AF_AUTH
|
|
|
|
|| na2->a_type != 1
|
|
|
|
|| NA->au.a_md5.md5_auth_len != RIP_AUTH_PW_LEN
|
1997-09-15 15:51:54 +04:00
|
|
|
|| memcmp(hash, na2->au.au_pw, sizeof(hash)))
|
1997-02-04 01:02:51 +03:00
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
msglim(use_authp, from, "bad password from %s",
|
|
|
|
naddr_ntoa(from));
|
|
|
|
return 0;
|
|
|
|
#undef NA
|
|
|
|
}
|