10Sstevel@tonic-gate /*
2*7738SRishi.Srivatsavai@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
30Sstevel@tonic-gate * Use is subject to license terms.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * Copyright (c) 1983, 1988, 1993
60Sstevel@tonic-gate * The Regents of the University of California. All rights reserved.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate * are met:
110Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
120Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
130Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
140Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
150Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
160Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
170Sstevel@tonic-gate * must display the following acknowledgment:
180Sstevel@tonic-gate * This product includes software developed by the University of
190Sstevel@tonic-gate * California, Berkeley and its contributors.
200Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors
210Sstevel@tonic-gate * may be used to endorse or promote products derived from this software
220Sstevel@tonic-gate * without specific prior written permission.
230Sstevel@tonic-gate *
240Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
250Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
260Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
270Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
280Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
290Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
300Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
310Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
320Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
330Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
340Sstevel@tonic-gate * SUCH DAMAGE.
350Sstevel@tonic-gate *
360Sstevel@tonic-gate * $FreeBSD: src/sbin/routed/input.c,v 1.9 2001/06/06 20:52:30 phk Exp $
370Sstevel@tonic-gate */
380Sstevel@tonic-gate
390Sstevel@tonic-gate #include "defs.h"
400Sstevel@tonic-gate #include <md5.h>
410Sstevel@tonic-gate
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate * The size of the control buffer passed to recvmsg() used to receive
440Sstevel@tonic-gate * ancillary data.
450Sstevel@tonic-gate */
460Sstevel@tonic-gate #define CONTROL_BUFSIZE 1024
470Sstevel@tonic-gate
480Sstevel@tonic-gate static void input(struct sockaddr_in *, struct interface *, struct rip *, int);
490Sstevel@tonic-gate static boolean_t ck_passwd(struct interface *, struct rip *, uint8_t *,
500Sstevel@tonic-gate in_addr_t, struct msg_limit *);
510Sstevel@tonic-gate
520Sstevel@tonic-gate
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate * Find the interface which received the given message.
550Sstevel@tonic-gate */
560Sstevel@tonic-gate struct interface *
receiving_interface(struct msghdr * msg,boolean_t findremote)570Sstevel@tonic-gate receiving_interface(struct msghdr *msg, boolean_t findremote)
580Sstevel@tonic-gate {
590Sstevel@tonic-gate struct interface *ifp, *ifp1, *ifp2;
600Sstevel@tonic-gate struct sockaddr_in *from;
610Sstevel@tonic-gate void *opt;
620Sstevel@tonic-gate uint_t ifindex;
630Sstevel@tonic-gate
640Sstevel@tonic-gate from = (struct sockaddr_in *)msg->msg_name;
650Sstevel@tonic-gate
660Sstevel@tonic-gate /* First see if this packet came from a remote gateway. */
670Sstevel@tonic-gate if (findremote && ((ifp = findremoteif(from->sin_addr.s_addr)) != NULL))
680Sstevel@tonic-gate return (ifp);
690Sstevel@tonic-gate
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate * It did not come from a remote gateway. Determine which
720Sstevel@tonic-gate * physical interface this packet was received on by
730Sstevel@tonic-gate * processing the message's ancillary data to find the
740Sstevel@tonic-gate * IP_RECVIF option we requested.
750Sstevel@tonic-gate */
760Sstevel@tonic-gate if ((opt = find_ancillary(msg, IP_RECVIF)) == NULL) {
770Sstevel@tonic-gate msglog("unable to retrieve IP_RECVIF");
780Sstevel@tonic-gate } else {
790Sstevel@tonic-gate ifindex = *(uint_t *)opt;
800Sstevel@tonic-gate if ((ifp = ifwithindex(ifindex, _B_TRUE)) != NULL) {
810Sstevel@tonic-gate /* Find the best match of the aliases */
820Sstevel@tonic-gate ifp2 = NULL;
830Sstevel@tonic-gate for (ifp1 = ifp; ifp1 != NULL;
840Sstevel@tonic-gate ifp1 = ifp1->int_ilist.hl_next) {
850Sstevel@tonic-gate if (ifp1->int_addr == from->sin_addr.s_addr)
860Sstevel@tonic-gate return (ifp1);
870Sstevel@tonic-gate if ((ifp2 == NULL ||
884513Skcpoon (ifp2->int_state & IS_ALIAS)) &&
890Sstevel@tonic-gate on_net(from->sin_addr.s_addr, ifp1->int_net,
904513Skcpoon ifp1->int_mask)) {
910Sstevel@tonic-gate ifp2 = ifp1;
924513Skcpoon }
930Sstevel@tonic-gate }
940Sstevel@tonic-gate if (ifp2 != NULL)
950Sstevel@tonic-gate ifp = ifp2;
960Sstevel@tonic-gate return (ifp);
970Sstevel@tonic-gate }
980Sstevel@tonic-gate }
990Sstevel@tonic-gate
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate * As a last resort (for some reason, ip didn't give us the
1020Sstevel@tonic-gate * IP_RECVIF index we requested), try to deduce the receiving
1030Sstevel@tonic-gate * interface based on the source address of the packet.
1040Sstevel@tonic-gate */
105*7738SRishi.Srivatsavai@Sun.COM return (iflookup(from->sin_addr.s_addr));
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate * Process RIP input on rip_sock. Returns 0 for success, -1 for failure.
1100Sstevel@tonic-gate */
1110Sstevel@tonic-gate int
read_rip()1120Sstevel@tonic-gate read_rip()
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate struct sockaddr_in from;
1150Sstevel@tonic-gate struct interface *ifp;
1160Sstevel@tonic-gate int cc;
1170Sstevel@tonic-gate union pkt_buf inbuf;
1180Sstevel@tonic-gate struct msghdr msg;
1190Sstevel@tonic-gate struct iovec iov;
1200Sstevel@tonic-gate uint8_t ancillary_data[CONTROL_BUFSIZE];
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate iov.iov_base = &inbuf;
1230Sstevel@tonic-gate iov.iov_len = sizeof (inbuf);
1240Sstevel@tonic-gate msg.msg_iov = &iov;
1250Sstevel@tonic-gate msg.msg_iovlen = 1;
1260Sstevel@tonic-gate msg.msg_name = &from;
1270Sstevel@tonic-gate msg.msg_control = &ancillary_data;
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate for (;;) {
1300Sstevel@tonic-gate msg.msg_namelen = sizeof (from);
1310Sstevel@tonic-gate msg.msg_controllen = sizeof (ancillary_data);
1320Sstevel@tonic-gate cc = recvmsg(rip_sock, &msg, 0);
1330Sstevel@tonic-gate if (cc == 0)
1340Sstevel@tonic-gate return (-1);
1350Sstevel@tonic-gate if (cc < 0) {
1360Sstevel@tonic-gate if (errno == EWOULDBLOCK || errno == EINTR)
1370Sstevel@tonic-gate return (0);
1380Sstevel@tonic-gate LOGERR("recvmsg(rip_sock)");
1390Sstevel@tonic-gate return (-1);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate /*
1430Sstevel@tonic-gate * ifp is the interface via which the packet arrived.
1440Sstevel@tonic-gate */
1450Sstevel@tonic-gate ifp = receiving_interface(&msg, _B_TRUE);
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate input(&from, ifp, &inbuf.rip, cc);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate /* Process a RIP packet */
1530Sstevel@tonic-gate static void
input(struct sockaddr_in * from,struct interface * ifp,struct rip * rip,int cc)1540Sstevel@tonic-gate input(struct sockaddr_in *from, /* received from this IP address */
1550Sstevel@tonic-gate struct interface *ifp, /* interface of incoming socket */
1560Sstevel@tonic-gate struct rip *rip,
1570Sstevel@tonic-gate int cc)
1580Sstevel@tonic-gate {
1590Sstevel@tonic-gate #define FROM_NADDR from->sin_addr.s_addr
1600Sstevel@tonic-gate static struct msg_limit use_auth, bad_len, bad_mask;
1610Sstevel@tonic-gate static struct msg_limit unk_router, bad_router, bad_nhop;
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate struct rt_entry *rt;
1640Sstevel@tonic-gate struct rt_spare new;
1650Sstevel@tonic-gate struct netinfo *n, *lim;
1660Sstevel@tonic-gate struct interface *ifp1;
1670Sstevel@tonic-gate in_addr_t gate, mask, v1_mask, dst, ddst_h = 0;
1680Sstevel@tonic-gate struct auth *ap;
1690Sstevel@tonic-gate struct tgate *tg = NULL;
1700Sstevel@tonic-gate struct tgate_net *tn;
1710Sstevel@tonic-gate int i, j;
1720Sstevel@tonic-gate boolean_t poll_answer = _B_FALSE; /* Set to _B_TRUE if RIPCMD_POLL */
1730Sstevel@tonic-gate uint16_t rt_state = 0; /* Extra route state to pass to input_route() */
1740Sstevel@tonic-gate uint8_t metric;
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate (void) memset(&new, 0, sizeof (new));
1770Sstevel@tonic-gate /* Notice when we hear from a remote gateway */
1780Sstevel@tonic-gate if (ifp != NULL && (ifp->int_state & IS_REMOTE))
1790Sstevel@tonic-gate ifp->int_act_time = now.tv_sec;
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate trace_rip("Recv", "from", from, ifp, rip, cc);
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate if (ifp != NULL && (ifp->int_if_flags & IFF_NORTEXCH)) {
1840Sstevel@tonic-gate trace_misc("discard RIP packet received over %s (IFF_NORTEXCH)",
1850Sstevel@tonic-gate ifp->int_name);
1860Sstevel@tonic-gate return;
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate gate = ntohl(FROM_NADDR);
1905577Ssangeeta if (IN_CLASSD(gate) || (gate >> IN_CLASSA_NSHIFT) == 0) {
1910Sstevel@tonic-gate msglim(&bad_router, FROM_NADDR, "source address %s unusable",
1920Sstevel@tonic-gate naddr_ntoa(FROM_NADDR));
1930Sstevel@tonic-gate return;
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate if (rip->rip_vers == 0) {
1970Sstevel@tonic-gate msglim(&bad_router, FROM_NADDR,
1980Sstevel@tonic-gate "RIP version 0, cmd %d, packet received from %s",
1990Sstevel@tonic-gate rip->rip_cmd, naddr_ntoa(FROM_NADDR));
2000Sstevel@tonic-gate return;
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate if (rip->rip_vers > RIPv2) {
2040Sstevel@tonic-gate msglim(&bad_router, FROM_NADDR,
2050Sstevel@tonic-gate "Treating RIP version %d packet received from %s as "
2060Sstevel@tonic-gate "version %d", rip->rip_vers, naddr_ntoa(FROM_NADDR),
2070Sstevel@tonic-gate RIPv2);
2080Sstevel@tonic-gate rip->rip_vers = RIPv2;
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate if (cc > (int)OVER_MAXPACKETSIZE) {
2120Sstevel@tonic-gate msglim(&bad_router, FROM_NADDR,
2130Sstevel@tonic-gate "packet at least %d bytes too long received from %s",
2140Sstevel@tonic-gate cc-MAXPACKETSIZE, naddr_ntoa(FROM_NADDR));
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate n = rip->rip_nets;
2180Sstevel@tonic-gate lim = n + (cc - 4) / sizeof (struct netinfo);
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate /*
2210Sstevel@tonic-gate * Notice authentication.
2220Sstevel@tonic-gate * As required by section 5.2 of RFC 2453, discard authenticated
2230Sstevel@tonic-gate * RIPv2 messages, but only if configured for that silliness.
2240Sstevel@tonic-gate *
2250Sstevel@tonic-gate * RIPv2 authentication is lame. Why authenticate queries?
2260Sstevel@tonic-gate * Why should a RIPv2 implementation with authentication disabled
2270Sstevel@tonic-gate * not be able to listen to RIPv2 packets with authentication, while
2280Sstevel@tonic-gate * RIPv1 systems will listen? Crazy!
2290Sstevel@tonic-gate */
2300Sstevel@tonic-gate if (!auth_ok && rip->rip_vers == RIPv2 && n < lim &&
2310Sstevel@tonic-gate n->n_family == RIP_AF_AUTH) {
2320Sstevel@tonic-gate msglim(&use_auth, FROM_NADDR,
2330Sstevel@tonic-gate "RIPv2 message with authentication from %s discarded",
2340Sstevel@tonic-gate naddr_ntoa(FROM_NADDR));
2350Sstevel@tonic-gate return;
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate switch (rip->rip_cmd) {
2390Sstevel@tonic-gate case RIPCMD_POLL:
2400Sstevel@tonic-gate /*
2410Sstevel@tonic-gate * Similar to RIPCMD_REQUEST, this command is used to
2420Sstevel@tonic-gate * request either a full-table or a set of entries. Both
2430Sstevel@tonic-gate * silent processes and routers can respond to this
2440Sstevel@tonic-gate * command.
2450Sstevel@tonic-gate */
2460Sstevel@tonic-gate poll_answer = _B_TRUE;
2470Sstevel@tonic-gate /* FALLTHRU */
2480Sstevel@tonic-gate case RIPCMD_REQUEST:
2490Sstevel@tonic-gate /* Are we talking to ourself or a remote gateway? */
2500Sstevel@tonic-gate ifp1 = ifwithaddr(FROM_NADDR, _B_FALSE, _B_TRUE);
2510Sstevel@tonic-gate if (ifp1 != NULL) {
2520Sstevel@tonic-gate if (ifp1->int_state & IS_REMOTE) {
2530Sstevel@tonic-gate /* remote gateway */
2540Sstevel@tonic-gate ifp = ifp1;
2550Sstevel@tonic-gate if (check_remote(ifp)) {
2560Sstevel@tonic-gate ifp->int_act_time = now.tv_sec;
2570Sstevel@tonic-gate if_ok(ifp, "remote ", _B_FALSE);
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate } else if (from->sin_port == htons(RIP_PORT)) {
2600Sstevel@tonic-gate trace_pkt(" discard our own RIP request");
2610Sstevel@tonic-gate return;
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate /* did the request come from a router? */
2660Sstevel@tonic-gate if (!poll_answer && (from->sin_port == htons(RIP_PORT))) {
2670Sstevel@tonic-gate /*
2680Sstevel@tonic-gate * yes, ignore the request if RIP is off so that
2690Sstevel@tonic-gate * the router does not depend on us.
2700Sstevel@tonic-gate */
2710Sstevel@tonic-gate if (ripout_interfaces == 0 ||
2720Sstevel@tonic-gate (ifp != NULL && (IS_RIP_OUT_OFF(ifp->int_state) ||
2730Sstevel@tonic-gate !IS_IFF_ROUTING(ifp->int_if_flags)))) {
2740Sstevel@tonic-gate trace_pkt(" discard request while RIP off");
2750Sstevel@tonic-gate return;
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate /*
2800Sstevel@tonic-gate * According to RFC 2453 section 5.2, we should ignore
2810Sstevel@tonic-gate * unauthenticated queries when authentication is
2820Sstevel@tonic-gate * configured. That is too silly to bother with. Sheesh!
2830Sstevel@tonic-gate * Are forwarding tables supposed to be secret even though
2840Sstevel@tonic-gate * a bad guy can infer them with test traffic? RIP is
2850Sstevel@tonic-gate * still the most common router-discovery protocol, so
2860Sstevel@tonic-gate * hosts need to send queries that will be answered. What
2870Sstevel@tonic-gate * about `rtquery`? Maybe on firewalls you'd care, but not
2880Sstevel@tonic-gate * enough to give up the diagnostic facilities of remote
2890Sstevel@tonic-gate * probing.
2900Sstevel@tonic-gate */
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate if (n >= lim) {
2930Sstevel@tonic-gate msglim(&bad_len, FROM_NADDR, "empty request from %s",
2940Sstevel@tonic-gate naddr_ntoa(FROM_NADDR));
2950Sstevel@tonic-gate return;
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate if (cc%sizeof (*n) != sizeof (struct rip)%sizeof (*n)) {
2980Sstevel@tonic-gate msglim(&bad_len, FROM_NADDR,
2990Sstevel@tonic-gate "request of bad length (%d) from %s",
3000Sstevel@tonic-gate cc, naddr_ntoa(FROM_NADDR));
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate
3030Sstevel@tonic-gate if (rip->rip_vers == RIPv2 && (ifp == NULL ||
3040Sstevel@tonic-gate (ifp->int_state & IS_NO_RIPV1_OUT))) {
3050Sstevel@tonic-gate v12buf.buf->rip_vers = RIPv2;
3060Sstevel@tonic-gate /*
3070Sstevel@tonic-gate * If we have a secret but it is a cleartext secret,
3080Sstevel@tonic-gate * do not disclose our secret unless the other guy
3090Sstevel@tonic-gate * already knows it.
3100Sstevel@tonic-gate */
3110Sstevel@tonic-gate ap = find_auth(ifp);
3120Sstevel@tonic-gate if (ap != NULL &&
3130Sstevel@tonic-gate (ulong_t)ap->end < (ulong_t)clk.tv_sec) {
3140Sstevel@tonic-gate /*
3150Sstevel@tonic-gate * Don't authenticate incoming packets
3160Sstevel@tonic-gate * using an expired key.
3170Sstevel@tonic-gate */
3180Sstevel@tonic-gate msglim(&use_auth, FROM_NADDR,
3190Sstevel@tonic-gate "%s attempting to authenticate using "
3200Sstevel@tonic-gate "an expired password.",
3210Sstevel@tonic-gate naddr_ntoa(FROM_NADDR));
3220Sstevel@tonic-gate ap = NULL;
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate if (ap != NULL && ap->type == RIP_AUTH_PW &&
3250Sstevel@tonic-gate (n->n_family != RIP_AF_AUTH ||
3260Sstevel@tonic-gate !ck_passwd(ifp, rip, (uint8_t *)lim, FROM_NADDR,
3270Sstevel@tonic-gate &use_auth)))
3280Sstevel@tonic-gate ap = NULL;
3290Sstevel@tonic-gate } else {
3300Sstevel@tonic-gate v12buf.buf->rip_vers = RIPv1;
3310Sstevel@tonic-gate ap = NULL;
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate clr_ws_buf(&v12buf, ap);
3340Sstevel@tonic-gate
3350Sstevel@tonic-gate do {
3360Sstevel@tonic-gate n->n_metric = ntohl(n->n_metric);
3370Sstevel@tonic-gate
3380Sstevel@tonic-gate /*
3390Sstevel@tonic-gate * A single entry with family RIP_AF_UNSPEC and
3400Sstevel@tonic-gate * metric HOPCNT_INFINITY means "all routes".
3410Sstevel@tonic-gate * We respond to routers only if we are acting
3420Sstevel@tonic-gate * as a supplier, or to anyone other than a router
3430Sstevel@tonic-gate * (i.e. a query).
3440Sstevel@tonic-gate */
3450Sstevel@tonic-gate if (n->n_family == RIP_AF_UNSPEC &&
3460Sstevel@tonic-gate n->n_metric == HOPCNT_INFINITY) {
3470Sstevel@tonic-gate /*
3480Sstevel@tonic-gate * Answer a full-table query from a utility
3490Sstevel@tonic-gate * program with all we know.
3500Sstevel@tonic-gate */
3510Sstevel@tonic-gate if (poll_answer ||
3520Sstevel@tonic-gate (from->sin_port != htons(RIP_PORT))) {
3530Sstevel@tonic-gate supply(from, ifp, OUT_QUERY, 0,
3540Sstevel@tonic-gate rip->rip_vers, ap != NULL);
3550Sstevel@tonic-gate return;
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate
3580Sstevel@tonic-gate /*
3590Sstevel@tonic-gate * A router is trying to prime its tables.
3600Sstevel@tonic-gate * Filter the answer in the same way
3610Sstevel@tonic-gate * broadcasts are filtered.
3620Sstevel@tonic-gate *
3630Sstevel@tonic-gate * Only answer a router if we are a supplier
3640Sstevel@tonic-gate * to keep an unwary host that is just starting
3650Sstevel@tonic-gate * from picking us as a router.
3660Sstevel@tonic-gate */
3670Sstevel@tonic-gate if (ifp == NULL) {
3680Sstevel@tonic-gate trace_pkt("ignore distant router");
3690Sstevel@tonic-gate return;
3700Sstevel@tonic-gate }
3710Sstevel@tonic-gate if (IS_RIP_OFF(ifp->int_state) ||
3720Sstevel@tonic-gate !should_supply(ifp)) {
3730Sstevel@tonic-gate trace_pkt("ignore; not supplying");
3740Sstevel@tonic-gate return;
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate
3770Sstevel@tonic-gate /*
3780Sstevel@tonic-gate * Do not answer a RIPv1 router if
3790Sstevel@tonic-gate * we are sending RIPv2. But do offer
3800Sstevel@tonic-gate * poor man's router discovery.
3810Sstevel@tonic-gate */
3820Sstevel@tonic-gate if ((ifp->int_state & IS_NO_RIPV1_OUT) &&
3830Sstevel@tonic-gate rip->rip_vers == RIPv1) {
3840Sstevel@tonic-gate if (!(ifp->int_state & IS_PM_RDISC)) {
3854513Skcpoon trace_pkt("ignore; sending "
3864513Skcpoon "RIPv2");
3874513Skcpoon return;
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate
3900Sstevel@tonic-gate v12buf.n->n_family = RIP_AF_INET;
3910Sstevel@tonic-gate v12buf.n->n_dst = RIP_DEFAULT;
3920Sstevel@tonic-gate metric = ifp->int_d_metric;
3930Sstevel@tonic-gate if (NULL !=
3940Sstevel@tonic-gate (rt = rtget(RIP_DEFAULT, 0)))
3950Sstevel@tonic-gate metric = MIN(metric,
3960Sstevel@tonic-gate (rt->rt_metric + 1));
3970Sstevel@tonic-gate v12buf.n->n_metric = htonl(metric);
3980Sstevel@tonic-gate v12buf.n++;
3990Sstevel@tonic-gate break;
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate
4020Sstevel@tonic-gate /*
4030Sstevel@tonic-gate * Respond with RIPv1 instead of RIPv2 if
4040Sstevel@tonic-gate * that is what we are broadcasting on the
4050Sstevel@tonic-gate * interface to keep the remote router from
4060Sstevel@tonic-gate * getting the wrong initial idea of the
4070Sstevel@tonic-gate * routes we send.
4080Sstevel@tonic-gate */
4090Sstevel@tonic-gate supply(from, ifp, OUT_UNICAST, 0,
4100Sstevel@tonic-gate (ifp->int_state & IS_NO_RIPV1_OUT)
4110Sstevel@tonic-gate ? RIPv2 : RIPv1,
4120Sstevel@tonic-gate ap != NULL);
4130Sstevel@tonic-gate return;
4140Sstevel@tonic-gate }
4150Sstevel@tonic-gate
4160Sstevel@tonic-gate /* Ignore authentication */
4170Sstevel@tonic-gate if (n->n_family == RIP_AF_AUTH)
4180Sstevel@tonic-gate continue;
4190Sstevel@tonic-gate
4200Sstevel@tonic-gate if (n->n_family != RIP_AF_INET) {
4210Sstevel@tonic-gate msglim(&bad_router, FROM_NADDR,
4220Sstevel@tonic-gate "request from %s for unsupported"
4230Sstevel@tonic-gate " (af %d) %s",
4240Sstevel@tonic-gate naddr_ntoa(FROM_NADDR),
4250Sstevel@tonic-gate ntohs(n->n_family),
4260Sstevel@tonic-gate naddr_ntoa(n->n_dst));
4270Sstevel@tonic-gate return;
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate
4300Sstevel@tonic-gate /* We are being asked about a specific destination. */
4310Sstevel@tonic-gate v12buf.n->n_dst = dst = n->n_dst;
4320Sstevel@tonic-gate v12buf.n->n_family = RIP_AF_INET;
4330Sstevel@tonic-gate if (!check_dst(dst)) {
4340Sstevel@tonic-gate msglim(&bad_router, FROM_NADDR,
4350Sstevel@tonic-gate "bad queried destination %s from %s",
4360Sstevel@tonic-gate naddr_ntoa(dst),
4370Sstevel@tonic-gate naddr_ntoa(FROM_NADDR));
4380Sstevel@tonic-gate v12buf.n->n_metric = HOPCNT_INFINITY;
4390Sstevel@tonic-gate goto rte_done;
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate
4420Sstevel@tonic-gate /* decide what mask was intended */
4430Sstevel@tonic-gate if (rip->rip_vers == RIPv1 ||
4440Sstevel@tonic-gate 0 == (mask = ntohl(n->n_mask)) ||
4450Sstevel@tonic-gate 0 != (ntohl(dst) & ~mask))
4460Sstevel@tonic-gate mask = ripv1_mask_host(dst, ifp);
4470Sstevel@tonic-gate
4480Sstevel@tonic-gate /*
4490Sstevel@tonic-gate * Try to find the answer. If we don't have an
4500Sstevel@tonic-gate * explicit route for the destination, use the best
4510Sstevel@tonic-gate * route to the destination.
4520Sstevel@tonic-gate */
4530Sstevel@tonic-gate rt = rtget(dst, mask);
4540Sstevel@tonic-gate if (rt == NULL && dst != RIP_DEFAULT)
4550Sstevel@tonic-gate rt = rtfind(n->n_dst);
4560Sstevel@tonic-gate
4570Sstevel@tonic-gate if (v12buf.buf->rip_vers != RIPv1)
4580Sstevel@tonic-gate v12buf.n->n_mask = htonl(mask);
4590Sstevel@tonic-gate if (rt == NULL) {
4600Sstevel@tonic-gate /* we do not have the answer */
4610Sstevel@tonic-gate v12buf.n->n_metric = HOPCNT_INFINITY;
4620Sstevel@tonic-gate goto rte_done;
4630Sstevel@tonic-gate }
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate /*
4660Sstevel@tonic-gate * we have the answer, so compute the right metric
4670Sstevel@tonic-gate * and next hop.
4680Sstevel@tonic-gate */
4690Sstevel@tonic-gate v12buf.n->n_metric = rt->rt_metric + 1;
4700Sstevel@tonic-gate if (v12buf.n->n_metric > HOPCNT_INFINITY)
4710Sstevel@tonic-gate v12buf.n->n_metric = HOPCNT_INFINITY;
4720Sstevel@tonic-gate if (v12buf.buf->rip_vers != RIPv1) {
4730Sstevel@tonic-gate v12buf.n->n_tag = rt->rt_tag;
4740Sstevel@tonic-gate if (ifp != NULL &&
4750Sstevel@tonic-gate on_net(rt->rt_gate, ifp->int_net,
4760Sstevel@tonic-gate ifp->int_mask) &&
4770Sstevel@tonic-gate rt->rt_gate != ifp->int_addr)
4780Sstevel@tonic-gate v12buf.n->n_nhop = rt->rt_gate;
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate rte_done:
4810Sstevel@tonic-gate v12buf.n->n_metric = htonl(v12buf.n->n_metric);
4820Sstevel@tonic-gate
4830Sstevel@tonic-gate /*
4840Sstevel@tonic-gate * Stop paying attention if we fill the output buffer.
4850Sstevel@tonic-gate */
4860Sstevel@tonic-gate if (++v12buf.n >= v12buf.lim)
4870Sstevel@tonic-gate break;
4880Sstevel@tonic-gate } while (++n < lim);
4890Sstevel@tonic-gate
4900Sstevel@tonic-gate /*
4910Sstevel@tonic-gate * If our response is authenticated with md5, complete the
4920Sstevel@tonic-gate * md5 computation.
4930Sstevel@tonic-gate */
4940Sstevel@tonic-gate if (ap != NULL && ap->type == RIP_AUTH_MD5)
4950Sstevel@tonic-gate end_md5_auth(&v12buf, ap);
4960Sstevel@tonic-gate
4970Sstevel@tonic-gate /*
4980Sstevel@tonic-gate * Diagnostic programs make specific requests
4990Sstevel@tonic-gate * from ports other than 520. Log other types
5000Sstevel@tonic-gate * of specific requests as suspicious.
5010Sstevel@tonic-gate */
5020Sstevel@tonic-gate if (!poll_answer && (from->sin_port == htons(RIP_PORT))) {
5030Sstevel@tonic-gate writelog(LOG_WARNING,
5040Sstevel@tonic-gate "Received suspicious request from %s port %d",
5050Sstevel@tonic-gate naddr_ntoa(FROM_NADDR), RIP_PORT);
5060Sstevel@tonic-gate }
5070Sstevel@tonic-gate if (poll_answer || (from->sin_port != htons(RIP_PORT))) {
5080Sstevel@tonic-gate /* query */
5090Sstevel@tonic-gate (void) output(OUT_QUERY, from, ifp, v12buf.buf,
5100Sstevel@tonic-gate ((char *)v12buf.n - (char *)v12buf.buf));
5110Sstevel@tonic-gate } else {
5120Sstevel@tonic-gate (void) output(OUT_UNICAST, from, ifp,
5130Sstevel@tonic-gate v12buf.buf, ((char *)v12buf.n -
5140Sstevel@tonic-gate (char *)v12buf.buf));
5150Sstevel@tonic-gate }
5160Sstevel@tonic-gate return;
5170Sstevel@tonic-gate
5180Sstevel@tonic-gate case RIPCMD_TRACEON:
5190Sstevel@tonic-gate case RIPCMD_TRACEOFF:
5200Sstevel@tonic-gate /*
5210Sstevel@tonic-gate * Notice that trace messages are turned off for all possible
5220Sstevel@tonic-gate * abuse if PATH_TRACE is undefined in pathnames.h.
5230Sstevel@tonic-gate * Notice also that because of the way the trace file is
5240Sstevel@tonic-gate * handled in trace.c, no abuse is plausible even if
5250Sstevel@tonic-gate * PATH_TRACE is defined.
5260Sstevel@tonic-gate *
5270Sstevel@tonic-gate * First verify message came from a privileged port.
5280Sstevel@tonic-gate */
5290Sstevel@tonic-gate if (ntohs(from->sin_port) > IPPORT_RESERVED) {
5300Sstevel@tonic-gate trace_pkt("trace command from untrusted port %d on %s",
5310Sstevel@tonic-gate ntohs(from->sin_port), naddr_ntoa(FROM_NADDR));
5320Sstevel@tonic-gate return;
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate if (ifp == NULL || !remote_address_ok(ifp, FROM_NADDR)) {
5350Sstevel@tonic-gate /*
5360Sstevel@tonic-gate * Use a message here to warn about strange
5370Sstevel@tonic-gate * messages from remote systems.
5380Sstevel@tonic-gate */
5390Sstevel@tonic-gate msglim(&bad_router, FROM_NADDR,
5400Sstevel@tonic-gate "trace command from non-local host %s",
5410Sstevel@tonic-gate naddr_ntoa(FROM_NADDR));
5420Sstevel@tonic-gate return;
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate if (ifp->int_state & IS_DISTRUST) {
5450Sstevel@tonic-gate tg = tgates;
5460Sstevel@tonic-gate while (tg->tgate_addr != FROM_NADDR) {
5470Sstevel@tonic-gate tg = tg->tgate_next;
5480Sstevel@tonic-gate if (tg == NULL) {
5490Sstevel@tonic-gate trace_pkt("trace command from "
5500Sstevel@tonic-gate "untrusted host %s",
5510Sstevel@tonic-gate naddr_ntoa(FROM_NADDR));
5520Sstevel@tonic-gate return;
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate }
5550Sstevel@tonic-gate }
5560Sstevel@tonic-gate if (ifp->int_auth[0].type != RIP_AUTH_NONE) {
5570Sstevel@tonic-gate /*
5580Sstevel@tonic-gate * Technically, it would be fairly easy to add
5590Sstevel@tonic-gate * standard authentication to the existing
5600Sstevel@tonic-gate * trace commands -- just bracket the payload
5610Sstevel@tonic-gate * with the authentication information.
5620Sstevel@tonic-gate * However, the tracing message behavior
5630Sstevel@tonic-gate * itself is marginal enough that we don't
5640Sstevel@tonic-gate * actually care. Just discard if
5650Sstevel@tonic-gate * authentication is needed.
5660Sstevel@tonic-gate */
5670Sstevel@tonic-gate trace_pkt("trace command unauthenticated from %s",
5680Sstevel@tonic-gate naddr_ntoa(FROM_NADDR));
5690Sstevel@tonic-gate return;
5700Sstevel@tonic-gate }
5710Sstevel@tonic-gate if (rip->rip_cmd == RIPCMD_TRACEON) {
5720Sstevel@tonic-gate rip->rip_tracefile[cc-4] = '\0';
5730Sstevel@tonic-gate set_tracefile(rip->rip_tracefile,
5740Sstevel@tonic-gate "trace command: %s\n", 0);
5750Sstevel@tonic-gate } else {
5760Sstevel@tonic-gate trace_off("tracing turned off by %s",
5770Sstevel@tonic-gate naddr_ntoa(FROM_NADDR));
5780Sstevel@tonic-gate }
5790Sstevel@tonic-gate return;
5800Sstevel@tonic-gate
5810Sstevel@tonic-gate case RIPCMD_RESPONSE:
5820Sstevel@tonic-gate if (ifp != NULL && (ifp->int_if_flags & IFF_NOXMIT)) {
5830Sstevel@tonic-gate trace_misc("discard RIP response received over %s "
5840Sstevel@tonic-gate "(IFF_NOXMIT)", ifp->int_name);
5850Sstevel@tonic-gate return;
5860Sstevel@tonic-gate }
5870Sstevel@tonic-gate
5880Sstevel@tonic-gate if (cc%sizeof (*n) != sizeof (struct rip)%sizeof (*n)) {
5890Sstevel@tonic-gate msglim(&bad_len, FROM_NADDR,
5900Sstevel@tonic-gate "response of bad length (%d) from %s",
5910Sstevel@tonic-gate cc, naddr_ntoa(FROM_NADDR));
5920Sstevel@tonic-gate }
5930Sstevel@tonic-gate
5944513Skcpoon if ((gate >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
5954513Skcpoon IN_LINKLOCAL(gate)) {
5960Sstevel@tonic-gate msglim(&bad_router, FROM_NADDR,
5970Sstevel@tonic-gate "discard RIP response from bad source address %s",
5980Sstevel@tonic-gate naddr_ntoa(FROM_NADDR));
5990Sstevel@tonic-gate return;
6000Sstevel@tonic-gate }
6010Sstevel@tonic-gate
6020Sstevel@tonic-gate /* verify message came from a router */
6030Sstevel@tonic-gate if (from->sin_port != htons(RIP_PORT)) {
6040Sstevel@tonic-gate msglim(&bad_router, FROM_NADDR,
6050Sstevel@tonic-gate " discard RIP response from unknown port"
6060Sstevel@tonic-gate " %d on host %s", ntohs(from->sin_port),
6070Sstevel@tonic-gate naddr_ntoa(FROM_NADDR));
6080Sstevel@tonic-gate return;
6090Sstevel@tonic-gate }
6100Sstevel@tonic-gate
6110Sstevel@tonic-gate if (!rip_enabled) {
6120Sstevel@tonic-gate trace_pkt(" discard response while RIP off");
6130Sstevel@tonic-gate return;
6140Sstevel@tonic-gate }
6150Sstevel@tonic-gate
6160Sstevel@tonic-gate /* Are we talking to ourself or a remote gateway? */
6170Sstevel@tonic-gate ifp1 = ifwithaddr(FROM_NADDR, _B_FALSE, _B_TRUE);
6180Sstevel@tonic-gate if (ifp1 != NULL) {
6190Sstevel@tonic-gate if (ifp1->int_state & IS_REMOTE) {
6200Sstevel@tonic-gate /* remote gateway */
6210Sstevel@tonic-gate ifp = ifp1;
6220Sstevel@tonic-gate if (check_remote(ifp)) {
6230Sstevel@tonic-gate ifp->int_act_time = now.tv_sec;
6240Sstevel@tonic-gate if_ok(ifp, "remote ", _B_FALSE);
6250Sstevel@tonic-gate }
6260Sstevel@tonic-gate } else {
6270Sstevel@tonic-gate trace_pkt(" discard our own RIP response");
6280Sstevel@tonic-gate return;
6290Sstevel@tonic-gate }
6300Sstevel@tonic-gate } else {
6310Sstevel@tonic-gate /*
6320Sstevel@tonic-gate * If it's not a remote gateway, then the
6330Sstevel@tonic-gate * remote address *must* be directly
6340Sstevel@tonic-gate * connected. Make sure that it is.
6350Sstevel@tonic-gate */
6360Sstevel@tonic-gate if (ifp != NULL &&
6370Sstevel@tonic-gate !remote_address_ok(ifp, FROM_NADDR)) {
6380Sstevel@tonic-gate msglim(&bad_router, FROM_NADDR,
6390Sstevel@tonic-gate "discard RIP response; source %s not on "
6400Sstevel@tonic-gate "interface %s", naddr_ntoa(FROM_NADDR),
6410Sstevel@tonic-gate ifp->int_name);
6420Sstevel@tonic-gate return;
6430Sstevel@tonic-gate }
6440Sstevel@tonic-gate }
6450Sstevel@tonic-gate
6460Sstevel@tonic-gate /*
6470Sstevel@tonic-gate * Accept routing packets from routers directly connected
6480Sstevel@tonic-gate * via broadcast or point-to-point networks, and from
6490Sstevel@tonic-gate * those listed in /etc/gateways.
6500Sstevel@tonic-gate */
6510Sstevel@tonic-gate if (ifp == NULL) {
6520Sstevel@tonic-gate msglim(&unk_router, FROM_NADDR,
6530Sstevel@tonic-gate " discard response from %s"
6540Sstevel@tonic-gate " via unexpected interface",
6550Sstevel@tonic-gate naddr_ntoa(FROM_NADDR));
6560Sstevel@tonic-gate return;
6570Sstevel@tonic-gate }
6580Sstevel@tonic-gate
6590Sstevel@tonic-gate if (IS_RIP_IN_OFF(ifp->int_state)) {
6600Sstevel@tonic-gate trace_pkt(" discard RIPv%d response"
6610Sstevel@tonic-gate " via disabled interface %s",
6620Sstevel@tonic-gate rip->rip_vers, ifp->int_name);
6630Sstevel@tonic-gate return;
6640Sstevel@tonic-gate }
6650Sstevel@tonic-gate
6660Sstevel@tonic-gate if (n >= lim) {
6670Sstevel@tonic-gate msglim(&bad_len, FROM_NADDR, "empty response from %s",
6680Sstevel@tonic-gate naddr_ntoa(FROM_NADDR));
6690Sstevel@tonic-gate return;
6700Sstevel@tonic-gate }
6710Sstevel@tonic-gate
6720Sstevel@tonic-gate if (((ifp->int_state & IS_NO_RIPV1_IN) &&
6730Sstevel@tonic-gate rip->rip_vers == RIPv1) ||
6740Sstevel@tonic-gate ((ifp->int_state & IS_NO_RIPV2_IN) &&
6750Sstevel@tonic-gate rip->rip_vers != RIPv1)) {
6760Sstevel@tonic-gate trace_pkt(" discard RIPv%d response",
6770Sstevel@tonic-gate rip->rip_vers);
6780Sstevel@tonic-gate return;
6790Sstevel@tonic-gate }
6800Sstevel@tonic-gate
6810Sstevel@tonic-gate /*
6820Sstevel@tonic-gate * Continue to listen to routes via broken interfaces
6830Sstevel@tonic-gate * which might be declared IS_BROKE because of
6840Sstevel@tonic-gate * device-driver idiosyncracies, but might otherwise
6850Sstevel@tonic-gate * be perfectly healthy.
6860Sstevel@tonic-gate */
6870Sstevel@tonic-gate if (ifp->int_state & IS_BROKE) {
6880Sstevel@tonic-gate trace_pkt("response via broken interface %s",
6890Sstevel@tonic-gate ifp->int_name);
6900Sstevel@tonic-gate }
6910Sstevel@tonic-gate
6920Sstevel@tonic-gate /*
6930Sstevel@tonic-gate * If the interface cares, ignore bad routers.
6940Sstevel@tonic-gate * Trace but do not log this problem, because where it
6950Sstevel@tonic-gate * happens, it happens frequently.
6960Sstevel@tonic-gate */
6970Sstevel@tonic-gate if (ifp->int_state & IS_DISTRUST) {
6980Sstevel@tonic-gate tg = tgates;
6990Sstevel@tonic-gate while (tg->tgate_addr != FROM_NADDR) {
7000Sstevel@tonic-gate tg = tg->tgate_next;
7010Sstevel@tonic-gate if (tg == NULL) {
7020Sstevel@tonic-gate trace_pkt(" discard RIP response"
7030Sstevel@tonic-gate " from untrusted router %s",
7040Sstevel@tonic-gate naddr_ntoa(FROM_NADDR));
7050Sstevel@tonic-gate return;
7060Sstevel@tonic-gate }
7070Sstevel@tonic-gate }
7080Sstevel@tonic-gate }
7090Sstevel@tonic-gate
7100Sstevel@tonic-gate /*
7110Sstevel@tonic-gate * Authenticate the packet if we have a secret.
7120Sstevel@tonic-gate * If we do not have any secrets, ignore the error in
7130Sstevel@tonic-gate * RFC 1723 and accept it regardless.
7140Sstevel@tonic-gate */
7150Sstevel@tonic-gate if (ifp->int_auth[0].type != RIP_AUTH_NONE &&
7160Sstevel@tonic-gate rip->rip_vers != RIPv1 &&
7170Sstevel@tonic-gate !ck_passwd(ifp, rip, (uint8_t *)lim, FROM_NADDR, &use_auth))
7180Sstevel@tonic-gate return;
7190Sstevel@tonic-gate
7200Sstevel@tonic-gate /*
7210Sstevel@tonic-gate * Do this only if we're supplying routes to *nobody*.
7220Sstevel@tonic-gate */
7230Sstevel@tonic-gate if (!should_supply(NULL) && save_space) {
7240Sstevel@tonic-gate /*
7250Sstevel@tonic-gate * "-S" option. Instead of entering all routes,
7260Sstevel@tonic-gate * only enter a default route for the sender of
7270Sstevel@tonic-gate * this RESPONSE message
7280Sstevel@tonic-gate */
7290Sstevel@tonic-gate
7300Sstevel@tonic-gate /* Should we trust this route from this router? */
7310Sstevel@tonic-gate if (tg != NULL && tg->tgate_nets->mask != 0) {
7320Sstevel@tonic-gate trace_pkt(" ignored unauthorized %s",
7330Sstevel@tonic-gate addrname(RIP_DEFAULT, 0, 0));
7340Sstevel@tonic-gate break;
7350Sstevel@tonic-gate }
7360Sstevel@tonic-gate
7370Sstevel@tonic-gate new.rts_gate = FROM_NADDR;
7380Sstevel@tonic-gate new.rts_router = FROM_NADDR;
7390Sstevel@tonic-gate new.rts_metric = HOPCNT_INFINITY-1;
7400Sstevel@tonic-gate new.rts_tag = n->n_tag;
7410Sstevel@tonic-gate new.rts_time = now.tv_sec;
7420Sstevel@tonic-gate new.rts_ifp = ifp;
7430Sstevel@tonic-gate new.rts_de_ag = 0;
7440Sstevel@tonic-gate new.rts_origin = RO_RIP;
7450Sstevel@tonic-gate /*
7460Sstevel@tonic-gate * Add the newly generated default route, but don't
7470Sstevel@tonic-gate * propagate the madness. Treat it the same way as
7480Sstevel@tonic-gate * default routes learned from Router Discovery.
7490Sstevel@tonic-gate */
7500Sstevel@tonic-gate input_route(RIP_DEFAULT, 0, &new, n, RS_NOPROPAGATE);
7510Sstevel@tonic-gate return;
7520Sstevel@tonic-gate }
7530Sstevel@tonic-gate
7540Sstevel@tonic-gate if (!IS_IFF_ROUTING(ifp->int_if_flags)) {
7550Sstevel@tonic-gate /*
7560Sstevel@tonic-gate * We don't want to propagate routes which would
7570Sstevel@tonic-gate * result in a black-hole.
7580Sstevel@tonic-gate */
7590Sstevel@tonic-gate rt_state = RS_NOPROPAGATE;
7600Sstevel@tonic-gate }
7610Sstevel@tonic-gate
7620Sstevel@tonic-gate do {
7630Sstevel@tonic-gate if (n->n_family == RIP_AF_AUTH)
7640Sstevel@tonic-gate continue;
7650Sstevel@tonic-gate
7660Sstevel@tonic-gate n->n_metric = ntohl(n->n_metric);
7670Sstevel@tonic-gate dst = n->n_dst;
7680Sstevel@tonic-gate if (n->n_family != RIP_AF_INET &&
7690Sstevel@tonic-gate (n->n_family != RIP_AF_UNSPEC ||
7700Sstevel@tonic-gate dst != RIP_DEFAULT)) {
7710Sstevel@tonic-gate msglim(&bad_router, FROM_NADDR,
7720Sstevel@tonic-gate "route from %s to unsupported"
7730Sstevel@tonic-gate " address family=%d destination=%s",
7740Sstevel@tonic-gate naddr_ntoa(FROM_NADDR), n->n_family,
7750Sstevel@tonic-gate naddr_ntoa(dst));
7760Sstevel@tonic-gate continue;
7770Sstevel@tonic-gate }
7780Sstevel@tonic-gate if (!check_dst(dst)) {
7790Sstevel@tonic-gate msglim(&bad_router, FROM_NADDR,
7800Sstevel@tonic-gate "bad destination %s from %s",
7810Sstevel@tonic-gate naddr_ntoa(dst),
7820Sstevel@tonic-gate naddr_ntoa(FROM_NADDR));
7830Sstevel@tonic-gate continue;
7840Sstevel@tonic-gate }
7850Sstevel@tonic-gate if (n->n_metric == 0 || n->n_metric > HOPCNT_INFINITY) {
7860Sstevel@tonic-gate msglim(&bad_router, FROM_NADDR,
7870Sstevel@tonic-gate "bad metric %d from %s"
7880Sstevel@tonic-gate " for destination %s",
7890Sstevel@tonic-gate n->n_metric, naddr_ntoa(FROM_NADDR),
7900Sstevel@tonic-gate naddr_ntoa(dst));
7910Sstevel@tonic-gate continue;
7920Sstevel@tonic-gate }
7930Sstevel@tonic-gate
7940Sstevel@tonic-gate /*
7950Sstevel@tonic-gate * Notice the next-hop.
7960Sstevel@tonic-gate */
7970Sstevel@tonic-gate gate = FROM_NADDR;
7980Sstevel@tonic-gate if (n->n_nhop != 0) {
7990Sstevel@tonic-gate if (rip->rip_vers == RIPv1) {
8000Sstevel@tonic-gate n->n_nhop = 0;
8010Sstevel@tonic-gate } else {
8020Sstevel@tonic-gate /* Use it only if it is valid. */
8030Sstevel@tonic-gate if (on_net(n->n_nhop,
8040Sstevel@tonic-gate ifp->int_net, ifp->int_mask) &&
8050Sstevel@tonic-gate check_dst(n->n_nhop)) {
8060Sstevel@tonic-gate gate = n->n_nhop;
8070Sstevel@tonic-gate } else {
8080Sstevel@tonic-gate msglim(&bad_nhop,
8090Sstevel@tonic-gate FROM_NADDR,
8100Sstevel@tonic-gate "router %s to %s"
8110Sstevel@tonic-gate " has bad next hop %s",
8120Sstevel@tonic-gate naddr_ntoa(FROM_NADDR),
8130Sstevel@tonic-gate naddr_ntoa(dst),
8140Sstevel@tonic-gate naddr_ntoa(n->n_nhop));
8150Sstevel@tonic-gate n->n_nhop = 0;
8160Sstevel@tonic-gate }
8170Sstevel@tonic-gate }
8180Sstevel@tonic-gate }
8190Sstevel@tonic-gate
8200Sstevel@tonic-gate if (rip->rip_vers == RIPv1 ||
8210Sstevel@tonic-gate 0 == (mask = ntohl(n->n_mask))) {
8220Sstevel@tonic-gate mask = ripv1_mask_host(dst, ifp);
8230Sstevel@tonic-gate } else if ((ntohl(dst) & ~mask) != 0) {
8240Sstevel@tonic-gate msglim(&bad_mask, FROM_NADDR,
8250Sstevel@tonic-gate "router %s sent bad netmask %s with %s",
8260Sstevel@tonic-gate naddr_ntoa(FROM_NADDR),
8270Sstevel@tonic-gate naddr_ntoa(htonl(mask)),
8280Sstevel@tonic-gate naddr_ntoa(dst));
8290Sstevel@tonic-gate continue;
8300Sstevel@tonic-gate }
8310Sstevel@tonic-gate
8320Sstevel@tonic-gate if (mask == HOST_MASK &&
8330Sstevel@tonic-gate (ifp->int_state & IS_NO_HOST)) {
8340Sstevel@tonic-gate trace_pkt(" ignored host route %s",
8350Sstevel@tonic-gate addrname(dst, mask, 0));
8360Sstevel@tonic-gate continue;
8370Sstevel@tonic-gate }
8380Sstevel@tonic-gate
8390Sstevel@tonic-gate if (rip->rip_vers == RIPv1)
8400Sstevel@tonic-gate n->n_tag = 0;
8410Sstevel@tonic-gate
8420Sstevel@tonic-gate /*
8430Sstevel@tonic-gate * Adjust metric according to incoming interface cost.
8440Sstevel@tonic-gate * We intentionally don't drop incoming routes with
8450Sstevel@tonic-gate * metric 15 on the floor even though they will
8460Sstevel@tonic-gate * not be advertised to other routers. We can use
8470Sstevel@tonic-gate * such routes locally, resulting in a network with
8480Sstevel@tonic-gate * a maximum width of 15 hops rather than 14.
8490Sstevel@tonic-gate */
8500Sstevel@tonic-gate n->n_metric += ifp->int_metric;
8510Sstevel@tonic-gate if (n->n_metric > HOPCNT_INFINITY)
8520Sstevel@tonic-gate n->n_metric = HOPCNT_INFINITY;
8530Sstevel@tonic-gate
8540Sstevel@tonic-gate /*
8550Sstevel@tonic-gate * Should we trust this route from this router?
8560Sstevel@tonic-gate */
8570Sstevel@tonic-gate if (tg != NULL && (tn = tg->tgate_nets)->mask != 0) {
8580Sstevel@tonic-gate for (i = 0; i < MAX_TGATE_NETS; i++, tn++) {
8590Sstevel@tonic-gate if (on_net(dst, tn->net, tn->mask) &&
8600Sstevel@tonic-gate tn->mask <= mask)
8610Sstevel@tonic-gate break;
8620Sstevel@tonic-gate }
8630Sstevel@tonic-gate if (i >= MAX_TGATE_NETS || tn->mask == 0) {
8640Sstevel@tonic-gate trace_pkt(" ignored unauthorized %s",
8650Sstevel@tonic-gate addrname(dst, mask, 0));
8660Sstevel@tonic-gate continue;
8670Sstevel@tonic-gate }
8680Sstevel@tonic-gate }
8690Sstevel@tonic-gate
8700Sstevel@tonic-gate /*
8710Sstevel@tonic-gate * Recognize and ignore a default route we faked
8720Sstevel@tonic-gate * which is being sent back to us by a machine with
8730Sstevel@tonic-gate * broken split-horizon. Be a little more paranoid
8740Sstevel@tonic-gate * than that, and reject default routes with the
8750Sstevel@tonic-gate * same metric we advertised.
8760Sstevel@tonic-gate */
8770Sstevel@tonic-gate if (ifp->int_d_metric != 0 && dst == RIP_DEFAULT &&
8780Sstevel@tonic-gate n->n_metric >= ifp->int_d_metric)
8790Sstevel@tonic-gate continue;
8800Sstevel@tonic-gate
8810Sstevel@tonic-gate /*
8820Sstevel@tonic-gate * We can receive aggregated RIPv2 routes that must
8830Sstevel@tonic-gate * be broken down before they are transmitted by
8840Sstevel@tonic-gate * RIPv1 via an interface on a subnet. We might
8850Sstevel@tonic-gate * also receive the same routes aggregated via
8860Sstevel@tonic-gate * other RIPv2 interfaces. This could cause
8870Sstevel@tonic-gate * duplicate routes to be sent on the RIPv1
8880Sstevel@tonic-gate * interfaces. "Longest matching variable length
8890Sstevel@tonic-gate * netmasks" lets RIPv2 listeners understand, but
8900Sstevel@tonic-gate * breaking down the aggregated routes for RIPv1
8910Sstevel@tonic-gate * listeners can produce duplicate routes.
8920Sstevel@tonic-gate *
8930Sstevel@tonic-gate * Breaking down aggregated routes here bloats the
8940Sstevel@tonic-gate * daemon table, but does not hurt the kernel
8950Sstevel@tonic-gate * table, since routes are always aggregated for
8960Sstevel@tonic-gate * the kernel.
8970Sstevel@tonic-gate *
8980Sstevel@tonic-gate * Notice that this does not break down network
8990Sstevel@tonic-gate * routes corresponding to subnets. This is part of
9000Sstevel@tonic-gate * the defense against RS_NET_SYN.
9010Sstevel@tonic-gate */
9020Sstevel@tonic-gate if (have_ripv1_out &&
9030Sstevel@tonic-gate (((rt = rtget(dst, mask)) == NULL ||
9040Sstevel@tonic-gate !(rt->rt_state & RS_NET_SYN))) &&
9050Sstevel@tonic-gate (v1_mask = ripv1_mask_net(dst, 0)) > mask) {
9060Sstevel@tonic-gate /* Get least significant set bit */
9070Sstevel@tonic-gate ddst_h = v1_mask & -v1_mask;
9080Sstevel@tonic-gate i = (v1_mask & ~mask)/ddst_h;
9090Sstevel@tonic-gate /*
9100Sstevel@tonic-gate * If you're going to make 512 or more
9110Sstevel@tonic-gate * routes, then that's just too many. The
9120Sstevel@tonic-gate * reason here is that breaking an old
9130Sstevel@tonic-gate * class B into /24 allocations is common
9140Sstevel@tonic-gate * enough that allowing for the creation of
9150Sstevel@tonic-gate * at least 256 deaggregated routes is
9160Sstevel@tonic-gate * good. The next power of 2 is 512.
9170Sstevel@tonic-gate */
9180Sstevel@tonic-gate if (i >= 511) {
9190Sstevel@tonic-gate /*
9200Sstevel@tonic-gate * Punt if we would have to
9210Sstevel@tonic-gate * generate an unreasonable number
9220Sstevel@tonic-gate * of routes.
9230Sstevel@tonic-gate */
9240Sstevel@tonic-gate if (TRACECONTENTS)
9250Sstevel@tonic-gate trace_misc("accept %s-->%s as 1"
9260Sstevel@tonic-gate " instead of %d routes",
9270Sstevel@tonic-gate addrname(dst, mask, 0),
9280Sstevel@tonic-gate naddr_ntoa(FROM_NADDR),
9290Sstevel@tonic-gate i + 1);
9300Sstevel@tonic-gate i = 0;
9310Sstevel@tonic-gate } else {
9320Sstevel@tonic-gate mask = v1_mask;
9330Sstevel@tonic-gate }
9340Sstevel@tonic-gate } else {
9350Sstevel@tonic-gate i = 0;
9360Sstevel@tonic-gate }
9370Sstevel@tonic-gate
9380Sstevel@tonic-gate new.rts_gate = gate;
9390Sstevel@tonic-gate new.rts_router = FROM_NADDR;
9400Sstevel@tonic-gate new.rts_metric = n->n_metric;
9410Sstevel@tonic-gate new.rts_tag = n->n_tag;
9420Sstevel@tonic-gate new.rts_time = now.tv_sec;
9430Sstevel@tonic-gate new.rts_ifp = ifp;
9440Sstevel@tonic-gate new.rts_de_ag = i;
9450Sstevel@tonic-gate new.rts_origin = RO_RIP;
9460Sstevel@tonic-gate j = 0;
9470Sstevel@tonic-gate for (;;) {
9480Sstevel@tonic-gate input_route(dst, mask, &new, n, rt_state);
9490Sstevel@tonic-gate if (++j > i)
9500Sstevel@tonic-gate break;
9510Sstevel@tonic-gate dst = htonl(ntohl(dst) + ddst_h);
9520Sstevel@tonic-gate }
9530Sstevel@tonic-gate } while (++n < lim);
9540Sstevel@tonic-gate return;
9550Sstevel@tonic-gate case RIPCMD_POLLENTRY:
9560Sstevel@tonic-gate /*
9570Sstevel@tonic-gate * With this command one can request a single entry.
9580Sstevel@tonic-gate * Both silent processes and routers can respond to this
9590Sstevel@tonic-gate * command
9600Sstevel@tonic-gate */
9610Sstevel@tonic-gate
9620Sstevel@tonic-gate if (n >= lim) {
9630Sstevel@tonic-gate msglim(&bad_len, FROM_NADDR, "empty request from %s",
9640Sstevel@tonic-gate naddr_ntoa(FROM_NADDR));
9650Sstevel@tonic-gate return;
9660Sstevel@tonic-gate }
9670Sstevel@tonic-gate if (cc%sizeof (*n) != sizeof (struct rip)%sizeof (*n)) {
9680Sstevel@tonic-gate msglim(&bad_len, FROM_NADDR,
9690Sstevel@tonic-gate "request of bad length (%d) from %s",
9700Sstevel@tonic-gate cc, naddr_ntoa(FROM_NADDR));
9710Sstevel@tonic-gate }
9720Sstevel@tonic-gate
9730Sstevel@tonic-gate if (rip->rip_vers == RIPv2 && (ifp == NULL ||
9740Sstevel@tonic-gate (ifp->int_state & IS_NO_RIPV1_OUT))) {
9750Sstevel@tonic-gate v12buf.buf->rip_vers = RIPv2;
9760Sstevel@tonic-gate } else {
9770Sstevel@tonic-gate v12buf.buf->rip_vers = RIPv1;
9780Sstevel@tonic-gate }
9790Sstevel@tonic-gate /* Dont bother with md5 authentication with POLLENTRY */
9800Sstevel@tonic-gate ap = NULL;
9810Sstevel@tonic-gate clr_ws_buf(&v12buf, ap);
9820Sstevel@tonic-gate
9830Sstevel@tonic-gate n->n_metric = ntohl(n->n_metric);
9840Sstevel@tonic-gate
9850Sstevel@tonic-gate if (n->n_family != RIP_AF_INET) {
9860Sstevel@tonic-gate msglim(&bad_router, FROM_NADDR,
9870Sstevel@tonic-gate "POLLENTRY request from %s for unsupported"
9880Sstevel@tonic-gate " (af %d) %s",
9890Sstevel@tonic-gate naddr_ntoa(FROM_NADDR),
9900Sstevel@tonic-gate ntohs(n->n_family),
9910Sstevel@tonic-gate naddr_ntoa(n->n_dst));
9920Sstevel@tonic-gate return;
9930Sstevel@tonic-gate }
9940Sstevel@tonic-gate
9950Sstevel@tonic-gate /* We are being asked about a specific destination. */
9960Sstevel@tonic-gate v12buf.n->n_dst = dst = n->n_dst;
9970Sstevel@tonic-gate v12buf.n->n_family = RIP_AF_INET;
9980Sstevel@tonic-gate if (!check_dst(dst)) {
9990Sstevel@tonic-gate msglim(&bad_router, FROM_NADDR,
10000Sstevel@tonic-gate "bad queried destination %s from %s",
10010Sstevel@tonic-gate naddr_ntoa(dst),
10020Sstevel@tonic-gate naddr_ntoa(FROM_NADDR));
10030Sstevel@tonic-gate v12buf.n->n_metric = HOPCNT_INFINITY;
10040Sstevel@tonic-gate goto pollentry_done;
10050Sstevel@tonic-gate }
10060Sstevel@tonic-gate
10070Sstevel@tonic-gate /* decide what mask was intended */
10080Sstevel@tonic-gate if (rip->rip_vers == RIPv1 ||
10090Sstevel@tonic-gate 0 == (mask = ntohl(n->n_mask)) ||
10100Sstevel@tonic-gate 0 != (ntohl(dst) & ~mask))
10110Sstevel@tonic-gate mask = ripv1_mask_host(dst, ifp);
10120Sstevel@tonic-gate
10130Sstevel@tonic-gate /* try to find the answer */
10140Sstevel@tonic-gate rt = rtget(dst, mask);
10150Sstevel@tonic-gate if (rt == NULL && dst != RIP_DEFAULT)
10160Sstevel@tonic-gate rt = rtfind(n->n_dst);
10170Sstevel@tonic-gate
10180Sstevel@tonic-gate if (v12buf.buf->rip_vers != RIPv1)
10190Sstevel@tonic-gate v12buf.n->n_mask = htonl(mask);
10200Sstevel@tonic-gate if (rt == NULL) {
10210Sstevel@tonic-gate /* we do not have the answer */
10220Sstevel@tonic-gate v12buf.n->n_metric = HOPCNT_INFINITY;
10230Sstevel@tonic-gate goto pollentry_done;
10240Sstevel@tonic-gate }
10250Sstevel@tonic-gate
10260Sstevel@tonic-gate
10270Sstevel@tonic-gate /*
10280Sstevel@tonic-gate * we have the answer, so compute the right metric and next
10290Sstevel@tonic-gate * hop.
10300Sstevel@tonic-gate */
10310Sstevel@tonic-gate v12buf.n->n_metric = rt->rt_metric + 1;
10320Sstevel@tonic-gate if (v12buf.n->n_metric > HOPCNT_INFINITY)
10330Sstevel@tonic-gate v12buf.n->n_metric = HOPCNT_INFINITY;
10340Sstevel@tonic-gate if (v12buf.buf->rip_vers != RIPv1) {
10350Sstevel@tonic-gate v12buf.n->n_tag = rt->rt_tag;
10360Sstevel@tonic-gate if (ifp != NULL &&
10370Sstevel@tonic-gate on_net(rt->rt_gate, ifp->int_net, ifp->int_mask) &&
10380Sstevel@tonic-gate rt->rt_gate != ifp->int_addr)
10390Sstevel@tonic-gate v12buf.n->n_nhop = rt->rt_gate;
10400Sstevel@tonic-gate }
10410Sstevel@tonic-gate pollentry_done:
10420Sstevel@tonic-gate v12buf.n->n_metric = htonl(v12buf.n->n_metric);
10430Sstevel@tonic-gate
10440Sstevel@tonic-gate /*
10450Sstevel@tonic-gate * Send the answer about specific routes.
10460Sstevel@tonic-gate */
10470Sstevel@tonic-gate (void) output(OUT_QUERY, from, ifp, v12buf.buf,
10480Sstevel@tonic-gate ((char *)v12buf.n - (char *)v12buf.buf));
10490Sstevel@tonic-gate break;
10500Sstevel@tonic-gate }
10510Sstevel@tonic-gate #undef FROM_NADDR
10520Sstevel@tonic-gate }
10530Sstevel@tonic-gate
10540Sstevel@tonic-gate
10550Sstevel@tonic-gate /*
10560Sstevel@tonic-gate * Process a single input route.
10570Sstevel@tonic-gate */
10580Sstevel@tonic-gate void
input_route(in_addr_t dst,in_addr_t mask,struct rt_spare * new,struct netinfo * n,uint16_t rt_state)10590Sstevel@tonic-gate input_route(in_addr_t dst, /* network order */
10600Sstevel@tonic-gate in_addr_t mask,
10610Sstevel@tonic-gate struct rt_spare *new,
10620Sstevel@tonic-gate struct netinfo *n,
10630Sstevel@tonic-gate uint16_t rt_state)
10640Sstevel@tonic-gate {
10650Sstevel@tonic-gate int i;
10660Sstevel@tonic-gate struct rt_entry *rt;
10670Sstevel@tonic-gate struct rt_spare *rts, *rts0;
10680Sstevel@tonic-gate struct interface *ifp1;
10690Sstevel@tonic-gate struct rt_spare *ptr;
10700Sstevel@tonic-gate size_t ptrsize;
10710Sstevel@tonic-gate
10720Sstevel@tonic-gate /*
10730Sstevel@tonic-gate * See if we can already get there by a working interface. Ignore
10740Sstevel@tonic-gate * if so.
10750Sstevel@tonic-gate */
10760Sstevel@tonic-gate ifp1 = ifwithaddr(dst, _B_TRUE, _B_FALSE);
10770Sstevel@tonic-gate if (ifp1 != NULL && (ifp1->int_state & IS_PASSIVE))
10780Sstevel@tonic-gate return;
10790Sstevel@tonic-gate
10800Sstevel@tonic-gate /*
10810Sstevel@tonic-gate * Look for the route in our table.
10820Sstevel@tonic-gate */
10830Sstevel@tonic-gate rt = rtget(dst, mask);
10840Sstevel@tonic-gate
10850Sstevel@tonic-gate /* Consider adding the route if we do not already have it. */
10860Sstevel@tonic-gate if (rt == NULL) {
10870Sstevel@tonic-gate /* Ignore unknown routes being poisoned. */
10880Sstevel@tonic-gate if (new->rts_metric == HOPCNT_INFINITY)
10890Sstevel@tonic-gate return;
10900Sstevel@tonic-gate
10910Sstevel@tonic-gate /* Ignore the route if it points to us */
10920Sstevel@tonic-gate if (n != NULL && n->n_nhop != 0 &&
10930Sstevel@tonic-gate NULL != ifwithaddr(n->n_nhop, _B_TRUE, _B_FALSE))
10940Sstevel@tonic-gate return;
10950Sstevel@tonic-gate
10960Sstevel@tonic-gate /*
10970Sstevel@tonic-gate * If something has not gone crazy and tried to fill
10980Sstevel@tonic-gate * our memory, accept the new route.
10990Sstevel@tonic-gate */
11000Sstevel@tonic-gate rtadd(dst, mask, rt_state, new);
11010Sstevel@tonic-gate return;
11020Sstevel@tonic-gate }
11030Sstevel@tonic-gate
11040Sstevel@tonic-gate /*
11050Sstevel@tonic-gate * We already know about the route. Consider this update.
11060Sstevel@tonic-gate *
11070Sstevel@tonic-gate * If (rt->rt_state & RS_NET_SYN), then this route
11080Sstevel@tonic-gate * is the same as a network route we have inferred
11090Sstevel@tonic-gate * for subnets we know, in order to tell RIPv1 routers
11100Sstevel@tonic-gate * about the subnets.
11110Sstevel@tonic-gate *
11120Sstevel@tonic-gate * It is impossible to tell if the route is coming
11130Sstevel@tonic-gate * from a distant RIPv2 router with the standard
11140Sstevel@tonic-gate * netmask because that router knows about the entire
11150Sstevel@tonic-gate * network, or if it is a round-about echo of a
11160Sstevel@tonic-gate * synthetic, RIPv1 network route of our own.
11170Sstevel@tonic-gate * The worst is that both kinds of routes might be
11180Sstevel@tonic-gate * received, and the bad one might have the smaller
11190Sstevel@tonic-gate * metric. Partly solve this problem by never
11200Sstevel@tonic-gate * aggregating into such a route. Also keep it
11210Sstevel@tonic-gate * around as long as the interface exists.
11220Sstevel@tonic-gate */
11230Sstevel@tonic-gate
11240Sstevel@tonic-gate rts0 = rt->rt_spares;
11250Sstevel@tonic-gate for (rts = rts0, i = rt->rt_num_spares; i != 0; i--, rts++) {
11260Sstevel@tonic-gate if (rts->rts_router == new->rts_router)
11270Sstevel@tonic-gate break;
11280Sstevel@tonic-gate /*
11290Sstevel@tonic-gate * Note the worst slot to reuse,
11300Sstevel@tonic-gate * other than the current slot.
11310Sstevel@tonic-gate */
11320Sstevel@tonic-gate if (BETTER_LINK(rt, rts0, rts))
11330Sstevel@tonic-gate rts0 = rts;
11340Sstevel@tonic-gate }
11350Sstevel@tonic-gate if (i != 0) {
11360Sstevel@tonic-gate /*
11370Sstevel@tonic-gate * Found a route from the router already in the table.
11380Sstevel@tonic-gate */
11390Sstevel@tonic-gate
11400Sstevel@tonic-gate /*
11410Sstevel@tonic-gate * If the new route is a route broken down from an
11420Sstevel@tonic-gate * aggregated route, and if the previous route is either
11430Sstevel@tonic-gate * not a broken down route or was broken down from a finer
11440Sstevel@tonic-gate * netmask, and if the previous route is current,
11450Sstevel@tonic-gate * then forget this one.
11460Sstevel@tonic-gate */
11470Sstevel@tonic-gate if (new->rts_de_ag > rts->rts_de_ag &&
11480Sstevel@tonic-gate now_stale <= rts->rts_time)
11490Sstevel@tonic-gate return;
11500Sstevel@tonic-gate
11510Sstevel@tonic-gate /*
11520Sstevel@tonic-gate * Keep poisoned routes around only long enough to pass
11530Sstevel@tonic-gate * the poison on. Use a new timestamp for good routes.
11540Sstevel@tonic-gate */
11550Sstevel@tonic-gate if (rts->rts_metric == HOPCNT_INFINITY &&
11560Sstevel@tonic-gate new->rts_metric == HOPCNT_INFINITY)
11570Sstevel@tonic-gate new->rts_time = rts->rts_time;
11580Sstevel@tonic-gate
11590Sstevel@tonic-gate /*
11600Sstevel@tonic-gate * If this is an update for the router we currently prefer,
11610Sstevel@tonic-gate * then note it.
11620Sstevel@tonic-gate */
11630Sstevel@tonic-gate if (i == rt->rt_num_spares) {
11643725Ssowmini uint8_t old_metric = rts->rts_metric;
11653725Ssowmini
11660Sstevel@tonic-gate rtchange(rt, rt->rt_state | rt_state, new, 0);
11670Sstevel@tonic-gate /*
11680Sstevel@tonic-gate * If the route got worse, check for something better.
11690Sstevel@tonic-gate */
11703725Ssowmini if (new->rts_metric != old_metric)
11710Sstevel@tonic-gate rtswitch(rt, 0);
11720Sstevel@tonic-gate return;
11730Sstevel@tonic-gate }
11740Sstevel@tonic-gate
11750Sstevel@tonic-gate /*
11760Sstevel@tonic-gate * This is an update for a spare route.
11770Sstevel@tonic-gate * Finished if the route is unchanged.
11780Sstevel@tonic-gate */
11790Sstevel@tonic-gate if (rts->rts_gate == new->rts_gate &&
11800Sstevel@tonic-gate rts->rts_metric == new->rts_metric &&
11810Sstevel@tonic-gate rts->rts_tag == new->rts_tag) {
11820Sstevel@tonic-gate if ((rt->rt_dst == RIP_DEFAULT) &&
11830Sstevel@tonic-gate (rts->rts_ifp != new->rts_ifp))
11840Sstevel@tonic-gate trace_misc("input_route update for spare");
11850Sstevel@tonic-gate trace_upslot(rt, rts, new);
11860Sstevel@tonic-gate *rts = *new;
11870Sstevel@tonic-gate return;
11880Sstevel@tonic-gate }
11890Sstevel@tonic-gate
11900Sstevel@tonic-gate /*
11910Sstevel@tonic-gate * Forget it if it has gone bad.
11920Sstevel@tonic-gate */
11930Sstevel@tonic-gate if (new->rts_metric == HOPCNT_INFINITY) {
11940Sstevel@tonic-gate rts_delete(rt, rts);
11950Sstevel@tonic-gate return;
11960Sstevel@tonic-gate }
11970Sstevel@tonic-gate
11980Sstevel@tonic-gate } else {
11990Sstevel@tonic-gate /*
12000Sstevel@tonic-gate * The update is for a route we know about,
12010Sstevel@tonic-gate * but not from a familiar router.
12020Sstevel@tonic-gate *
12030Sstevel@tonic-gate * Ignore the route if it points to us.
12040Sstevel@tonic-gate */
12050Sstevel@tonic-gate if (n != NULL && n->n_nhop != 0 &&
12060Sstevel@tonic-gate NULL != ifwithaddr(n->n_nhop, _B_TRUE, _B_FALSE))
12070Sstevel@tonic-gate return;
12080Sstevel@tonic-gate
12090Sstevel@tonic-gate /* the loop above set rts0=worst spare */
12100Sstevel@tonic-gate if (rts0->rts_metric < HOPCNT_INFINITY) {
12110Sstevel@tonic-gate ptrsize = (rt->rt_num_spares + SPARE_INC) *
12120Sstevel@tonic-gate sizeof (struct rt_spare);
12130Sstevel@tonic-gate ptr = realloc(rt->rt_spares, ptrsize);
12140Sstevel@tonic-gate if (ptr != NULL) {
12150Sstevel@tonic-gate
12160Sstevel@tonic-gate rt->rt_spares = ptr;
12170Sstevel@tonic-gate rts0 = &rt->rt_spares[rt->rt_num_spares];
12180Sstevel@tonic-gate (void) memset(rts0, 0,
12190Sstevel@tonic-gate SPARE_INC * sizeof (struct rt_spare));
12200Sstevel@tonic-gate rt->rt_num_spares += SPARE_INC;
12210Sstevel@tonic-gate for (rts = rts0, i = SPARE_INC;
12220Sstevel@tonic-gate i != 0; i--, rts++)
12230Sstevel@tonic-gate rts->rts_metric = HOPCNT_INFINITY;
12240Sstevel@tonic-gate }
12250Sstevel@tonic-gate }
12260Sstevel@tonic-gate rts = rts0;
12270Sstevel@tonic-gate
12280Sstevel@tonic-gate /*
12290Sstevel@tonic-gate * Save the route as a spare only if it has
12300Sstevel@tonic-gate * a better metric than our worst spare.
12310Sstevel@tonic-gate * This also ignores poisoned routes (those
12320Sstevel@tonic-gate * received with metric HOPCNT_INFINITY).
12330Sstevel@tonic-gate */
12340Sstevel@tonic-gate if (new->rts_metric >= rts->rts_metric)
12350Sstevel@tonic-gate return;
12360Sstevel@tonic-gate }
12370Sstevel@tonic-gate trace_upslot(rt, rts, new);
12380Sstevel@tonic-gate *rts = *new;
12390Sstevel@tonic-gate
12400Sstevel@tonic-gate /* try to switch to a better route */
12410Sstevel@tonic-gate rtswitch(rt, rts);
12420Sstevel@tonic-gate }
12430Sstevel@tonic-gate
12440Sstevel@tonic-gate /*
12450Sstevel@tonic-gate * Recorded information about peer's MD5 sequence numbers. This is
12460Sstevel@tonic-gate * used to validate that received sequence numbers are in
12470Sstevel@tonic-gate * non-decreasing order as per the RFC.
12480Sstevel@tonic-gate */
12490Sstevel@tonic-gate struct peer_hash {
12500Sstevel@tonic-gate struct peer_hash *ph_next;
12510Sstevel@tonic-gate in_addr_t ph_addr;
12520Sstevel@tonic-gate time_t ph_heard;
12530Sstevel@tonic-gate uint32_t ph_seqno;
12540Sstevel@tonic-gate };
12550Sstevel@tonic-gate
12560Sstevel@tonic-gate static struct peer_hash **peer_hashes;
12570Sstevel@tonic-gate static int ph_index;
12580Sstevel@tonic-gate static int ph_num_peers;
12590Sstevel@tonic-gate
12600Sstevel@tonic-gate /*
12610Sstevel@tonic-gate * Get a peer_hash structure from the hash of known peers. Create a
12620Sstevel@tonic-gate * new one if not found. Returns NULL on unrecoverable allocation
12630Sstevel@tonic-gate * failure.
12640Sstevel@tonic-gate */
12650Sstevel@tonic-gate static struct peer_hash *
get_peer_info(in_addr_t from)12660Sstevel@tonic-gate get_peer_info(in_addr_t from)
12670Sstevel@tonic-gate {
12680Sstevel@tonic-gate struct peer_hash *php;
12690Sstevel@tonic-gate struct peer_hash *pnhp;
12700Sstevel@tonic-gate struct peer_hash **ph_pp;
12710Sstevel@tonic-gate struct peer_hash **ph2_pp;
12720Sstevel@tonic-gate struct peer_hash **ph3_pp;
12730Sstevel@tonic-gate int i;
12740Sstevel@tonic-gate static uint_t failed_count;
12750Sstevel@tonic-gate
12760Sstevel@tonic-gate if (peer_hashes == NULL) {
12770Sstevel@tonic-gate peer_hashes = calloc(hash_table_sizes[0],
12780Sstevel@tonic-gate sizeof (peer_hashes[0]));
12790Sstevel@tonic-gate if (peer_hashes == NULL) {
12800Sstevel@tonic-gate if (++failed_count % 100 == 1)
12810Sstevel@tonic-gate msglog("no memory for peer hash");
12820Sstevel@tonic-gate return (NULL);
12830Sstevel@tonic-gate }
12840Sstevel@tonic-gate }
12850Sstevel@tonic-gate /* Search for peer in existing hash table */
12860Sstevel@tonic-gate ph_pp = peer_hashes + (from % hash_table_sizes[ph_index]);
12870Sstevel@tonic-gate for (php = ph_pp[0]; php != NULL; php = php->ph_next) {
12880Sstevel@tonic-gate if (php->ph_addr == from)
12890Sstevel@tonic-gate return (php);
12900Sstevel@tonic-gate }
12910Sstevel@tonic-gate /*
12920Sstevel@tonic-gate * Not found; we need to add this peer to the table. If there
12930Sstevel@tonic-gate * are already too many peers, then try to expand the table
12940Sstevel@tonic-gate * first. It's not a big deal if we can't expand the table
12950Sstevel@tonic-gate * right now due to memory constraints. We'll try again
12960Sstevel@tonic-gate * later.
12970Sstevel@tonic-gate */
12980Sstevel@tonic-gate if (ph_num_peers >= hash_table_sizes[ph_index] * 5 &&
12990Sstevel@tonic-gate hash_table_sizes[ph_index + 1] != 0 &&
13000Sstevel@tonic-gate (ph_pp = calloc(hash_table_sizes[ph_index + 1],
13014513Skcpoon sizeof (peer_hashes[0]))) != NULL) {
13020Sstevel@tonic-gate ph2_pp = peer_hashes;
13030Sstevel@tonic-gate for (i = hash_table_sizes[ph_index] - 1; i >= 0; i--) {
13040Sstevel@tonic-gate for (php = ph2_pp[i]; php != NULL; php = pnhp) {
13050Sstevel@tonic-gate pnhp = php->ph_next;
13060Sstevel@tonic-gate ph3_pp = ph_pp + (php->ph_addr %
13070Sstevel@tonic-gate hash_table_sizes[ph_index + 1]);
13080Sstevel@tonic-gate php->ph_next = ph3_pp[0];
13090Sstevel@tonic-gate ph3_pp[0] = php;
13100Sstevel@tonic-gate }
13110Sstevel@tonic-gate }
13120Sstevel@tonic-gate ph_index++;
13130Sstevel@tonic-gate free(peer_hashes);
13140Sstevel@tonic-gate peer_hashes = ph_pp;
13150Sstevel@tonic-gate ph_pp += from % hash_table_sizes[ph_index];
13160Sstevel@tonic-gate }
13170Sstevel@tonic-gate php = calloc(sizeof (*php), 1);
13180Sstevel@tonic-gate if (php == NULL) {
13190Sstevel@tonic-gate if (++failed_count % 100 == 1)
13200Sstevel@tonic-gate msglog("no memory for peer hash entry");
13210Sstevel@tonic-gate } else {
13220Sstevel@tonic-gate php->ph_addr = from;
13230Sstevel@tonic-gate php->ph_heard = now.tv_sec;
13240Sstevel@tonic-gate php->ph_next = ph_pp[0];
13250Sstevel@tonic-gate ph_pp[0] = php;
13260Sstevel@tonic-gate ph_num_peers++;
13270Sstevel@tonic-gate }
13280Sstevel@tonic-gate return (php);
13290Sstevel@tonic-gate }
13300Sstevel@tonic-gate
13310Sstevel@tonic-gate /*
13320Sstevel@tonic-gate * Age out entries in the peer table. This is called every time we do
13330Sstevel@tonic-gate * a normal 30 second broadcast.
13340Sstevel@tonic-gate */
13350Sstevel@tonic-gate void
age_peer_info(void)13360Sstevel@tonic-gate age_peer_info(void)
13370Sstevel@tonic-gate {
13380Sstevel@tonic-gate struct peer_hash *php;
13390Sstevel@tonic-gate struct peer_hash *next_ph;
13400Sstevel@tonic-gate struct peer_hash *prev_ph;
13410Sstevel@tonic-gate struct peer_hash **ph_pp;
13420Sstevel@tonic-gate int i;
13430Sstevel@tonic-gate
13440Sstevel@tonic-gate /*
13450Sstevel@tonic-gate * Scan through the list and remove peers that should not
13460Sstevel@tonic-gate * still have valid authenticated entries in the routing
13470Sstevel@tonic-gate * table.
13480Sstevel@tonic-gate */
13490Sstevel@tonic-gate if ((ph_pp = peer_hashes) == NULL || ph_num_peers == 0)
13500Sstevel@tonic-gate return;
13510Sstevel@tonic-gate for (i = hash_table_sizes[ph_index] - 1; i >= 0; i--) {
13520Sstevel@tonic-gate prev_ph = NULL;
13530Sstevel@tonic-gate for (php = ph_pp[i]; php != NULL; php = next_ph) {
13540Sstevel@tonic-gate next_ph = php->ph_next;
13550Sstevel@tonic-gate if (php->ph_heard <= now_expire) {
13560Sstevel@tonic-gate if (prev_ph == NULL)
13570Sstevel@tonic-gate ph_pp[i] = next_ph;
13580Sstevel@tonic-gate else
13590Sstevel@tonic-gate prev_ph->ph_next = next_ph;
13600Sstevel@tonic-gate free(php);
13610Sstevel@tonic-gate if (--ph_num_peers == 0)
13620Sstevel@tonic-gate return;
13630Sstevel@tonic-gate } else {
13640Sstevel@tonic-gate prev_ph = php;
13650Sstevel@tonic-gate }
13660Sstevel@tonic-gate }
13670Sstevel@tonic-gate }
13680Sstevel@tonic-gate }
13690Sstevel@tonic-gate
13700Sstevel@tonic-gate static boolean_t /* _B_FALSE if bad, _B_TRUE if good */
ck_passwd(struct interface * aifp,struct rip * rip,uint8_t * lim,in_addr_t from,struct msg_limit * use_authp)13710Sstevel@tonic-gate ck_passwd(struct interface *aifp,
13720Sstevel@tonic-gate struct rip *rip,
13730Sstevel@tonic-gate uint8_t *lim,
13740Sstevel@tonic-gate in_addr_t from,
13750Sstevel@tonic-gate struct msg_limit *use_authp)
13760Sstevel@tonic-gate {
13770Sstevel@tonic-gate #define NA (rip->rip_auths)
13780Sstevel@tonic-gate struct netauth *na2;
13790Sstevel@tonic-gate struct auth *ap;
13800Sstevel@tonic-gate MD5_CTX md5_ctx;
13810Sstevel@tonic-gate uchar_t hash[RIP_AUTH_PW_LEN];
13820Sstevel@tonic-gate int i, len;
13830Sstevel@tonic-gate struct peer_hash *php;
13840Sstevel@tonic-gate uint32_t seqno;
13850Sstevel@tonic-gate
13860Sstevel@tonic-gate if ((uint8_t *)NA >= lim || NA->a_family != RIP_AF_AUTH) {
13870Sstevel@tonic-gate msglim(use_authp, from, "missing auth data from %s",
13880Sstevel@tonic-gate naddr_ntoa(from));
13890Sstevel@tonic-gate return (_B_FALSE);
13900Sstevel@tonic-gate }
13910Sstevel@tonic-gate
13920Sstevel@tonic-gate /*
13930Sstevel@tonic-gate * Validate sequence number on RIPv2 responses using keyed MD5
13940Sstevel@tonic-gate * authentication per RFC 2082 section 3.2.2. Note that if we
13950Sstevel@tonic-gate * can't locate the peer information (due to transient
13960Sstevel@tonic-gate * allocation problems), then we don't do the test. Also note
13970Sstevel@tonic-gate * that we assume that all sequence numbers 0x80000000 or more
13980Sstevel@tonic-gate * away are "less than."
13990Sstevel@tonic-gate *
14000Sstevel@tonic-gate * We intentionally violate RFC 2082 with respect to one case:
14010Sstevel@tonic-gate * restablishing contact. The RFC says that you should
14020Sstevel@tonic-gate * continue to ignore old sequence numbers in this case but
14030Sstevel@tonic-gate * make a special allowance for 0. This is extremely foolish.
14040Sstevel@tonic-gate * The problem is that if the router has crashed, it's
14050Sstevel@tonic-gate * entirely possible that either we'll miss sequence zero (or
14060Sstevel@tonic-gate * that it might not even send it!) or that the peer doesn't
14070Sstevel@tonic-gate * remember what it last used for a sequence number. In
14080Sstevel@tonic-gate * either case, we'll create a failure state that persists
14090Sstevel@tonic-gate * until the sequence number happens to advance past the last
14100Sstevel@tonic-gate * one we saw. This is bad because it means that we may have
14110Sstevel@tonic-gate * to wait until the router has been up for at least as long
14120Sstevel@tonic-gate * as it was last time before we even pay attention to it.
14130Sstevel@tonic-gate * Meanwhile, other routers may listen to it if they hadn't
14140Sstevel@tonic-gate * seen it before (i.e., if they crashed in the meantime).
14150Sstevel@tonic-gate * This means -- perversely -- that stable systems that stay
14160Sstevel@tonic-gate * "up" for a long time pay a penalty for doing so.
14170Sstevel@tonic-gate */
14180Sstevel@tonic-gate if (rip->rip_cmd == RIPCMD_RESPONSE && NA->a_type == RIP_AUTH_MD5 &&
14190Sstevel@tonic-gate (php = get_peer_info(from)) != NULL) {
14200Sstevel@tonic-gate /*
14210Sstevel@tonic-gate * If the entry that we find has been updated
14220Sstevel@tonic-gate * recently enough that the routes are known
14230Sstevel@tonic-gate * to still be good, but the sequence number
14240Sstevel@tonic-gate * looks bad, then discard the packet.
14250Sstevel@tonic-gate */
14260Sstevel@tonic-gate seqno = ntohl(NA->au.a_md5.md5_seqno);
14270Sstevel@tonic-gate if (php->ph_heard > now_expire && php->ph_seqno != 0 &&
14280Sstevel@tonic-gate (seqno == 0 || ((seqno - php->ph_seqno) & 0x80000000ul))) {
14290Sstevel@tonic-gate msglim(use_authp, from,
14300Sstevel@tonic-gate "discarding sequence %x (older than %x)",
14310Sstevel@tonic-gate (unsigned)seqno, (unsigned)php->ph_seqno);
14320Sstevel@tonic-gate return (_B_FALSE);
14330Sstevel@tonic-gate }
14340Sstevel@tonic-gate php->ph_heard = now.tv_sec;
14350Sstevel@tonic-gate php->ph_seqno = seqno;
14360Sstevel@tonic-gate }
14370Sstevel@tonic-gate
14380Sstevel@tonic-gate /*
14390Sstevel@tonic-gate * accept any current (+/- 24 hours) password
14400Sstevel@tonic-gate */
14410Sstevel@tonic-gate for (ap = aifp->int_auth, i = 0; i < MAX_AUTH_KEYS; i++, ap++) {
14420Sstevel@tonic-gate if (ap->type != NA->a_type ||
14430Sstevel@tonic-gate (ulong_t)ap->start > (ulong_t)clk.tv_sec+DAY ||
14440Sstevel@tonic-gate (ulong_t)ap->end+DAY < (ulong_t)clk.tv_sec)
14450Sstevel@tonic-gate continue;
14460Sstevel@tonic-gate
14470Sstevel@tonic-gate if (NA->a_type == RIP_AUTH_PW) {
14480Sstevel@tonic-gate if (0 == memcmp(NA->au.au_pw, ap->key, RIP_AUTH_PW_LEN))
14490Sstevel@tonic-gate return (_B_TRUE);
14500Sstevel@tonic-gate
14510Sstevel@tonic-gate } else {
14520Sstevel@tonic-gate /*
14530Sstevel@tonic-gate * accept MD5 secret with the right key ID
14540Sstevel@tonic-gate */
14550Sstevel@tonic-gate if (NA->au.a_md5.md5_keyid != ap->keyid)
14560Sstevel@tonic-gate continue;
14570Sstevel@tonic-gate
14580Sstevel@tonic-gate len = ntohs(NA->au.a_md5.md5_pkt_len);
14590Sstevel@tonic-gate if ((len - sizeof (*rip)) % sizeof (*NA) != 0 ||
14600Sstevel@tonic-gate len > (lim - (uint8_t *)rip - sizeof (*NA))) {
14610Sstevel@tonic-gate msglim(use_authp, from,
14620Sstevel@tonic-gate "wrong MD5 RIPv2 packet length of %d"
14630Sstevel@tonic-gate " instead of %d from %s",
14640Sstevel@tonic-gate len, lim - (uint8_t *)rip - sizeof (*NA),
14650Sstevel@tonic-gate naddr_ntoa(from));
14660Sstevel@tonic-gate return (_B_FALSE);
14670Sstevel@tonic-gate }
14680Sstevel@tonic-gate na2 = (struct netauth *)(rip->rip_nets +
14690Sstevel@tonic-gate (len - 4) / sizeof (struct netinfo));
14700Sstevel@tonic-gate
14710Sstevel@tonic-gate /*
14720Sstevel@tonic-gate * Given a good hash value, these are not security
14730Sstevel@tonic-gate * problems so be generous and accept the routes,
14740Sstevel@tonic-gate * after complaining.
14750Sstevel@tonic-gate */
14760Sstevel@tonic-gate if (TRACEPACKETS) {
14770Sstevel@tonic-gate if (NA->au.a_md5.md5_auth_len !=
14780Sstevel@tonic-gate RIP_AUTH_MD5_LEN)
14790Sstevel@tonic-gate msglim(use_authp, from,
14800Sstevel@tonic-gate "unknown MD5 RIPv2 auth len %#x"
14810Sstevel@tonic-gate " instead of %#x from %s",
14820Sstevel@tonic-gate NA->au.a_md5.md5_auth_len,
14830Sstevel@tonic-gate RIP_AUTH_MD5_LEN,
14840Sstevel@tonic-gate naddr_ntoa(from));
14850Sstevel@tonic-gate if (na2->a_family != RIP_AF_AUTH)
14860Sstevel@tonic-gate msglim(use_authp, from,
14870Sstevel@tonic-gate "unknown MD5 RIPv2 family %#x"
14880Sstevel@tonic-gate " instead of %#x from %s",
14890Sstevel@tonic-gate na2->a_family, RIP_AF_AUTH,
14900Sstevel@tonic-gate naddr_ntoa(from));
14910Sstevel@tonic-gate if (na2->a_type != RIP_AUTH_TRAILER)
14920Sstevel@tonic-gate msglim(use_authp, from,
14930Sstevel@tonic-gate "MD5 RIPv2 hash has %#x"
14940Sstevel@tonic-gate " instead of %#x from %s",
14950Sstevel@tonic-gate ntohs(na2->a_type),
14960Sstevel@tonic-gate ntohs(RIP_AUTH_TRAILER),
14970Sstevel@tonic-gate naddr_ntoa(from));
14980Sstevel@tonic-gate }
14990Sstevel@tonic-gate
15000Sstevel@tonic-gate MD5Init(&md5_ctx);
15010Sstevel@tonic-gate /*
15020Sstevel@tonic-gate * len+4 to include auth trailer's family/type in
15030Sstevel@tonic-gate * MD5 sum
15040Sstevel@tonic-gate */
15050Sstevel@tonic-gate MD5Update(&md5_ctx, (uchar_t *)rip, len + 4);
15060Sstevel@tonic-gate MD5Update(&md5_ctx, ap->key, RIP_AUTH_MD5_LEN);
15070Sstevel@tonic-gate MD5Final(hash, &md5_ctx);
15080Sstevel@tonic-gate if (0 == memcmp(hash, na2->au.au_pw, sizeof (hash)))
15090Sstevel@tonic-gate return (_B_TRUE);
15100Sstevel@tonic-gate }
15110Sstevel@tonic-gate }
15120Sstevel@tonic-gate
15130Sstevel@tonic-gate msglim(use_authp, from, "bad auth data from %s",
15140Sstevel@tonic-gate naddr_ntoa(from));
15150Sstevel@tonic-gate return (_B_FALSE);
15160Sstevel@tonic-gate #undef NA
15170Sstevel@tonic-gate }
1518