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