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