output.c revision 90868
118316Swollman/* 218316Swollman * Copyright (c) 1983, 1988, 1993 318316Swollman * The Regents of the University of California. All rights reserved. 418316Swollman * 518316Swollman * Redistribution and use in source and binary forms, with or without 618316Swollman * modification, are permitted provided that the following conditions 718316Swollman * are met: 818316Swollman * 1. Redistributions of source code must retain the above copyright 918316Swollman * notice, this list of conditions and the following disclaimer. 1018316Swollman * 2. Redistributions in binary form must reproduce the above copyright 1118316Swollman * notice, this list of conditions and the following disclaimer in the 1218316Swollman * documentation and/or other materials provided with the distribution. 1318316Swollman * 3. All advertising materials mentioning features or use of this software 1446303Smarkm * must display the following acknowledgment: 1518316Swollman * This product includes software developed by the University of 1618316Swollman * California, Berkeley and its contributors. 1718316Swollman * 4. Neither the name of the University nor the names of its contributors 1818316Swollman * may be used to endorse or promote products derived from this software 1918316Swollman * without specific prior written permission. 2018316Swollman * 2118316Swollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2218316Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2318316Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2418316Swollman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2518316Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2618316Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2718316Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2818316Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2918316Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3018316Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3118316Swollman * SUCH DAMAGE. 3246303Smarkm * 3350476Speter * $FreeBSD: head/sbin/routed/output.c 90868 2002-02-18 20:35:27Z mike $ 3418316Swollman */ 3518316Swollman 3646303Smarkm#include "defs.h" 3746303Smarkm 3846303Smarkm#if !defined(sgi) && !defined(__NetBSD__) 3946303Smarkmstatic char sccsid[] __attribute__((unused)) = "@(#)output.c 8.1 (Berkeley) 6/5/93"; 4046303Smarkm#elif defined(__NetBSD__) 4146303Smarkm__RCSID("$NetBSD$"); 4218316Swollman#endif 4350969Speter#ident "$FreeBSD: head/sbin/routed/output.c 90868 2002-02-18 20:35:27Z mike $" 4418316Swollman 4518316Swollman 4646303Smarkmu_int update_seqno; 4718316Swollman 4818316Swollman 4918316Swollman/* walk the tree of routes with this for output 5018316Swollman */ 5118316Swollmanstruct { 5218316Swollman struct sockaddr_in to; 5318316Swollman naddr to_mask; 5418316Swollman naddr to_net; 5518316Swollman naddr to_std_mask; 5618316Swollman naddr to_std_net; 5718316Swollman struct interface *ifp; /* usually output interface */ 5820339Swollman struct auth *a; 5918316Swollman char metric; /* adjust metrics by interface */ 6018316Swollman int npackets; 6118316Swollman int gen_limit; 6218316Swollman u_int state; 6318316Swollman#define WS_ST_FLASH 0x001 /* send only changed routes */ 6419880Swollman#define WS_ST_RIP2_ALL 0x002 /* send full featured RIPv2 */ 6519880Swollman#define WS_ST_AG 0x004 /* ok to aggregate subnets */ 6619880Swollman#define WS_ST_SUPER_AG 0x008 /* ok to aggregate networks */ 6746303Smarkm#define WS_ST_QUERY 0x010 /* responding to a query */ 6846303Smarkm#define WS_ST_TO_ON_NET 0x020 /* sending onto one of our nets */ 6946303Smarkm#define WS_ST_DEFAULT 0x040 /* faking a default */ 7018316Swollman} ws; 7118316Swollman 7218316Swollman/* A buffer for what can be heard by both RIPv1 and RIPv2 listeners */ 7319880Swollmanstruct ws_buf v12buf; 7418316Swollmanunion pkt_buf ripv12_buf; 7518316Swollman 7618316Swollman/* Another for only RIPv2 listeners */ 7719880Swollmanstruct ws_buf v2buf; 7818316Swollmanunion pkt_buf rip_v2_buf; 7918316Swollman 8018316Swollman 8118316Swollman 8219880Swollmanvoid 8319880Swollmanbufinit(void) 8419880Swollman{ 8519880Swollman ripv12_buf.rip.rip_cmd = RIPCMD_RESPONSE; 8619880Swollman v12buf.buf = &ripv12_buf.rip; 8719880Swollman v12buf.base = &v12buf.buf->rip_nets[0]; 8819880Swollman 8919880Swollman rip_v2_buf.rip.rip_cmd = RIPCMD_RESPONSE; 9019880Swollman rip_v2_buf.rip.rip_vers = RIPv2; 9119880Swollman v2buf.buf = &rip_v2_buf.rip; 9219880Swollman v2buf.base = &v2buf.buf->rip_nets[0]; 9319880Swollman} 9419880Swollman 9519880Swollman 9618316Swollman/* Send the contents of the global buffer via the non-multicast socket 9718316Swollman */ 9818316Swollmanint /* <0 on failure */ 9918316Swollmanoutput(enum output_type type, 10018316Swollman struct sockaddr_in *dst, /* send to here */ 10118316Swollman struct interface *ifp, 10218316Swollman struct rip *buf, 10318316Swollman int size) /* this many bytes */ 10418316Swollman{ 10518316Swollman struct sockaddr_in sin; 10618316Swollman int flags; 10746303Smarkm const char *msg; 10818316Swollman int res; 10918316Swollman naddr tgt_mcast; 11018316Swollman int soc; 11118316Swollman int serrno; 11218316Swollman 11318316Swollman sin = *dst; 11418316Swollman if (sin.sin_port == 0) 11518316Swollman sin.sin_port = htons(RIP_PORT); 11618316Swollman#ifdef _HAVE_SIN_LEN 11718316Swollman if (sin.sin_len == 0) 11818316Swollman sin.sin_len = sizeof(sin); 11918316Swollman#endif 12018316Swollman 12118316Swollman soc = rip_sock; 12218316Swollman flags = 0; 12318316Swollman 12418316Swollman switch (type) { 12518316Swollman case OUT_QUERY: 12618316Swollman msg = "Answer Query"; 12718316Swollman if (soc < 0) 12818316Swollman soc = ifp->int_rip_sock; 12918316Swollman break; 13018316Swollman case OUT_UNICAST: 13118316Swollman msg = "Send"; 13218316Swollman if (soc < 0) 13318316Swollman soc = ifp->int_rip_sock; 13418316Swollman flags = MSG_DONTROUTE; 13518316Swollman break; 13618316Swollman case OUT_BROADCAST: 13718316Swollman if (ifp->int_if_flags & IFF_POINTOPOINT) { 13818316Swollman msg = "Send"; 13918316Swollman } else { 14018316Swollman msg = "Send bcast"; 14118316Swollman } 14218316Swollman flags = MSG_DONTROUTE; 14318316Swollman break; 14418316Swollman case OUT_MULTICAST: 14518316Swollman if (ifp->int_if_flags & IFF_POINTOPOINT) { 14618316Swollman msg = "Send pt-to-pt"; 14718316Swollman } else if (ifp->int_state & IS_DUP) { 14818316Swollman trace_act("abort multicast output via %s" 14919880Swollman " with duplicate address", 15018316Swollman ifp->int_name); 15118316Swollman return 0; 15218316Swollman } else { 15318316Swollman msg = "Send mcast"; 15418316Swollman if (rip_sock_mcast != ifp) { 15518316Swollman#ifdef MCAST_PPP_BUG 15637908Scharnier /* Do not specify the primary interface 15718316Swollman * explicitly if we have the multicast 15818316Swollman * point-to-point kernel bug, since the 15918316Swollman * kernel will do the wrong thing if the 16018316Swollman * local address of a point-to-point link 16118316Swollman * is the same as the address of an ordinary 16218316Swollman * interface. 16318316Swollman */ 16418316Swollman if (ifp->int_addr == myaddr) { 16518316Swollman tgt_mcast = 0; 16618316Swollman } else 16718316Swollman#endif 16818316Swollman tgt_mcast = ifp->int_addr; 16918316Swollman if (0 > setsockopt(rip_sock, 17018316Swollman IPPROTO_IP, IP_MULTICAST_IF, 17118316Swollman &tgt_mcast, 17218316Swollman sizeof(tgt_mcast))) { 17318316Swollman serrno = errno; 17418316Swollman LOGERR("setsockopt(rip_sock," 17518316Swollman "IP_MULTICAST_IF)"); 17618316Swollman errno = serrno; 17718316Swollman ifp = 0; 17818316Swollman return -1; 17918316Swollman } 18018316Swollman rip_sock_mcast = ifp; 18118316Swollman } 18218316Swollman sin.sin_addr.s_addr = htonl(INADDR_RIP_GROUP); 18318316Swollman } 18420339Swollman break; 18518316Swollman 18618316Swollman case NO_OUT_MULTICAST: 18718316Swollman case NO_OUT_RIPV2: 18820339Swollman default: 18920339Swollman#ifdef DEBUG 19020339Swollman abort(); 19120339Swollman#endif 19220339Swollman return -1; 19318316Swollman } 19418316Swollman 19518316Swollman trace_rip(msg, "to", &sin, ifp, buf, size); 19618316Swollman 19718316Swollman res = sendto(soc, buf, size, flags, 19818316Swollman (struct sockaddr *)&sin, sizeof(sin)); 19918316Swollman if (res < 0 20018316Swollman && (ifp == 0 || !(ifp->int_state & IS_BROKE))) { 20118316Swollman serrno = errno; 20218316Swollman msglog("%s sendto(%s%s%s.%d): %s", msg, 20318316Swollman ifp != 0 ? ifp->int_name : "", 20418316Swollman ifp != 0 ? ", " : "", 20518316Swollman inet_ntoa(sin.sin_addr), 20618316Swollman ntohs(sin.sin_port), 20718316Swollman strerror(errno)); 20818316Swollman errno = serrno; 20918316Swollman } 21018316Swollman 21118316Swollman return res; 21218316Swollman} 21318316Swollman 21418316Swollman 21520339Swollman/* Find the first key for a packet to send. 21637908Scharnier * Try for a key that is eligible and has not expired, but settle for 21719880Swollman * the last key if they have all expired. 21819880Swollman * If no key is ready yet, give up. 21918316Swollman */ 22020339Swollmanstruct auth * 22119880Swollmanfind_auth(struct interface *ifp) 22218316Swollman{ 22320339Swollman struct auth *ap, *res; 22419880Swollman int i; 22519880Swollman 22619880Swollman 22720339Swollman if (ifp == 0) 22819880Swollman return 0; 22920339Swollman 23019880Swollman res = 0; 23120339Swollman ap = ifp->int_auth; 23219880Swollman for (i = 0; i < MAX_AUTH_KEYS; i++, ap++) { 23320339Swollman /* stop looking after the last key */ 23420339Swollman if (ap->type == RIP_AUTH_NONE) 23520339Swollman break; 23646303Smarkm 23720339Swollman /* ignore keys that are not ready yet */ 23820339Swollman if ((u_long)ap->start > (u_long)clk.tv_sec) 23920339Swollman continue; 24020339Swollman 24120339Swollman if ((u_long)ap->end < (u_long)clk.tv_sec) { 24220339Swollman /* note best expired password as a fall-back */ 24320339Swollman if (res == 0 || (u_long)ap->end > (u_long)res->end) 24420339Swollman res = ap; 24520339Swollman continue; 24620339Swollman } 24720339Swollman 24820339Swollman /* note key with the best future */ 24920339Swollman if (res == 0 || (u_long)res->end < (u_long)ap->end) 25019880Swollman res = ap; 25118316Swollman } 25219880Swollman return res; 25318316Swollman} 25418316Swollman 25518316Swollman 25619880Swollmanvoid 25719880Swollmanclr_ws_buf(struct ws_buf *wb, 25820339Swollman struct auth *ap) 25919880Swollman{ 26019880Swollman struct netauth *na; 26119880Swollman 26219880Swollman wb->lim = wb->base + NETS_LEN; 26319880Swollman wb->n = wb->base; 26446303Smarkm memset(wb->n, 0, NETS_LEN*sizeof(*wb->n)); 26519880Swollman 26646303Smarkm /* (start to) install authentication if appropriate 26719880Swollman */ 26819880Swollman if (ap == 0) 26919880Swollman return; 27046303Smarkm 27119880Swollman na = (struct netauth*)wb->n; 27220339Swollman if (ap->type == RIP_AUTH_PW) { 27319880Swollman na->a_family = RIP_AF_AUTH; 27419880Swollman na->a_type = RIP_AUTH_PW; 27546303Smarkm memcpy(na->au.au_pw, ap->key, sizeof(na->au.au_pw)); 27619880Swollman wb->n++; 27719880Swollman 27820339Swollman } else if (ap->type == RIP_AUTH_MD5) { 27919880Swollman na->a_family = RIP_AF_AUTH; 28019880Swollman na->a_type = RIP_AUTH_MD5; 28119880Swollman na->au.a_md5.md5_keyid = ap->keyid; 28246303Smarkm na->au.a_md5.md5_auth_len = RIP_AUTH_MD5_LEN; 28346303Smarkm na->au.a_md5.md5_seqno = htonl(clk.tv_sec); 28419880Swollman wb->n++; 28519880Swollman wb->lim--; /* make room for trailer */ 28619880Swollman } 28719880Swollman} 28819880Swollman 28919880Swollman 29019880Swollmanvoid 29119880Swollmanend_md5_auth(struct ws_buf *wb, 29220339Swollman struct auth *ap) 29319880Swollman{ 29419880Swollman struct netauth *na, *na2; 29519880Swollman MD5_CTX md5_ctx; 29646303Smarkm int len; 29719880Swollman 29819880Swollman 29919880Swollman na = (struct netauth*)wb->base; 30019880Swollman na2 = (struct netauth*)wb->n; 30146303Smarkm len = (char *)na2-(char *)wb->buf; 30219880Swollman na2->a_family = RIP_AF_AUTH; 30346303Smarkm na2->a_type = htons(1); 30446303Smarkm na->au.a_md5.md5_pkt_len = htons(len); 30519880Swollman MD5Init(&md5_ctx); 30646303Smarkm MD5Update(&md5_ctx, (u_char *)wb->buf, len); 30746303Smarkm MD5Update(&md5_ctx, ap->key, RIP_AUTH_MD5_LEN); 30819880Swollman MD5Final(na2->au.au_pw, &md5_ctx); 30919880Swollman wb->n++; 31019880Swollman} 31119880Swollman 31219880Swollman 31318316Swollman/* Send the buffer 31418316Swollman */ 31518316Swollmanstatic void 31618316Swollmansupply_write(struct ws_buf *wb) 31718316Swollman{ 31818316Swollman /* Output multicast only if legal. 31937908Scharnier * If we would multicast and it would be illegal, then discard the 32018316Swollman * packet. 32118316Swollman */ 32218316Swollman switch (wb->type) { 32318316Swollman case NO_OUT_MULTICAST: 32419880Swollman trace_pkt("skip multicast to %s because impossible", 32518316Swollman naddr_ntoa(ws.to.sin_addr.s_addr)); 32618316Swollman break; 32718316Swollman case NO_OUT_RIPV2: 32818316Swollman break; 32918316Swollman default: 33020339Swollman if (ws.a != 0 && ws.a->type == RIP_AUTH_MD5) 33119880Swollman end_md5_auth(wb,ws.a); 33218316Swollman if (output(wb->type, &ws.to, ws.ifp, wb->buf, 33318316Swollman ((char *)wb->n - (char*)wb->buf)) < 0 33418316Swollman && ws.ifp != 0) 33518316Swollman if_sick(ws.ifp); 33618316Swollman ws.npackets++; 33718316Swollman break; 33818316Swollman } 33918316Swollman 34020339Swollman clr_ws_buf(wb,ws.a); 34118316Swollman} 34218316Swollman 34318316Swollman 34418316Swollman/* put an entry into the packet 34518316Swollman */ 34618316Swollmanstatic void 34718316Swollmansupply_out(struct ag_info *ag) 34818316Swollman{ 34918316Swollman int i; 35020339Swollman naddr mask, v1_mask, dst_h, ddst_h = 0; 35118316Swollman struct ws_buf *wb; 35218316Swollman 35318316Swollman 35418316Swollman /* Skip this route if doing a flash update and it and the routes 35518316Swollman * it aggregates have not changed recently. 35618316Swollman */ 35718316Swollman if (ag->ag_seqno < update_seqno 35818316Swollman && (ws.state & WS_ST_FLASH)) 35918316Swollman return; 36018316Swollman 36118316Swollman dst_h = ag->ag_dst_h; 36218316Swollman mask = ag->ag_mask; 36318316Swollman v1_mask = ripv1_mask_host(htonl(dst_h), 36418316Swollman (ws.state & WS_ST_TO_ON_NET) ? ws.ifp : 0); 36518316Swollman i = 0; 36618316Swollman 36718316Swollman /* If we are sending RIPv2 packets that cannot (or must not) be 36818316Swollman * heard by RIPv1 listeners, do not worry about sub- or supernets. 36918316Swollman * Subnets (from other networks) can only be sent via multicast. 37018316Swollman * A pair of subnet routes might have been promoted so that they 37118316Swollman * are legal to send by RIPv1. 37219880Swollman * If RIPv1 is off, use the multicast buffer. 37318316Swollman */ 37419880Swollman if ((ws.state & WS_ST_RIP2_ALL) 37518316Swollman || ((ag->ag_state & AGS_RIPV2) && v1_mask != mask)) { 37618316Swollman /* use the RIPv2-only buffer */ 37719880Swollman wb = &v2buf; 37818316Swollman 37918316Swollman } else { 38018316Swollman /* use the RIPv1-or-RIPv2 buffer */ 38119880Swollman wb = &v12buf; 38218316Swollman 38318316Swollman /* Convert supernet route into corresponding set of network 38418316Swollman * routes for RIPv1, but leave non-contiguous netmasks 38518316Swollman * to ag_check(). 38618316Swollman */ 38718316Swollman if (v1_mask > mask 38818316Swollman && mask + (mask & -mask) == 0) { 38918316Swollman ddst_h = v1_mask & -v1_mask; 39018316Swollman i = (v1_mask & ~mask)/ddst_h; 39118316Swollman 39218316Swollman if (i > ws.gen_limit) { 39318316Swollman /* Punt if we would have to generate an 39418316Swollman * unreasonable number of routes. 39518316Swollman */ 39646303Smarkm if (TRACECONTENTS) 39746303Smarkm trace_misc("sending %s-->%s as 1" 39846303Smarkm " instead of %d routes", 39946303Smarkm addrname(htonl(dst_h), mask, 40046303Smarkm 1), 40146303Smarkm naddr_ntoa(ws.to.sin_addr 40246303Smarkm .s_addr), 40346303Smarkm i+1); 40418316Swollman i = 0; 40518316Swollman 40618316Swollman } else { 40718316Swollman mask = v1_mask; 40818316Swollman ws.gen_limit -= i; 40918316Swollman } 41018316Swollman } 41118316Swollman } 41218316Swollman 41318316Swollman do { 41418316Swollman wb->n->n_family = RIP_AF_INET; 41518316Swollman wb->n->n_dst = htonl(dst_h); 41618316Swollman /* If the route is from router-discovery or we are 41718316Swollman * shutting down, admit only a bad metric. 41818316Swollman */ 41918316Swollman wb->n->n_metric = ((stopint || ag->ag_metric < 1) 42018316Swollman ? HOPCNT_INFINITY 42118316Swollman : ag->ag_metric); 42290868Smike wb->n->n_metric = htonl(wb->n->n_metric); 42319880Swollman /* Any non-zero bits in the supposedly unused RIPv1 fields 42419880Swollman * cause the old `routed` to ignore the route. 42519880Swollman * That means the mask and so forth cannot be sent 42619880Swollman * in the hybrid RIPv1/RIPv2 mode. 42719880Swollman */ 42819880Swollman if (ws.state & WS_ST_RIP2_ALL) { 42918316Swollman if (ag->ag_nhop != 0 43018316Swollman && ((ws.state & WS_ST_QUERY) 43118316Swollman || (ag->ag_nhop != ws.ifp->int_addr 43218316Swollman && on_net(ag->ag_nhop, 43318316Swollman ws.ifp->int_net, 43418316Swollman ws.ifp->int_mask)))) 43518316Swollman wb->n->n_nhop = ag->ag_nhop; 43619880Swollman wb->n->n_mask = htonl(mask); 43718316Swollman wb->n->n_tag = ag->ag_tag; 43818316Swollman } 43918316Swollman dst_h += ddst_h; 44018316Swollman 44118316Swollman if (++wb->n >= wb->lim) 44218316Swollman supply_write(wb); 44318316Swollman } while (i-- != 0); 44418316Swollman} 44518316Swollman 44618316Swollman 44718316Swollman/* supply one route from the table 44818316Swollman */ 44918316Swollman/* ARGSUSED */ 45018316Swollmanstatic int 45118316Swollmanwalk_supply(struct radix_node *rn, 45246303Smarkm struct walkarg *argp UNUSED) 45318316Swollman{ 45418316Swollman#define RT ((struct rt_entry *)rn) 45518316Swollman u_short ags; 45618316Swollman char metric, pref; 45718316Swollman naddr dst, nhop; 45846303Smarkm struct rt_spare *rts; 45946303Smarkm int i; 46018316Swollman 46118316Swollman 46219880Swollman /* Do not advertise external remote interfaces or passive interfaces. 46318316Swollman */ 46418316Swollman if ((RT->rt_state & RS_IF) 46518316Swollman && RT->rt_ifp != 0 46664131Ssheldonh && (RT->rt_ifp->int_state & IS_PASSIVE) 46718316Swollman && !(RT->rt_state & RS_MHOME)) 46818316Swollman return 0; 46918316Swollman 47018316Swollman /* If being quiet about our ability to forward, then 47119880Swollman * do not say anything unless responding to a query, 47219880Swollman * except about our main interface. 47318316Swollman */ 47419880Swollman if (!supplier && !(ws.state & WS_ST_QUERY) 47519880Swollman && !(RT->rt_state & RS_MHOME)) 47618316Swollman return 0; 47718316Swollman 47818316Swollman dst = RT->rt_dst; 47918316Swollman 48018316Swollman /* do not collide with the fake default route */ 48118316Swollman if (dst == RIP_DEFAULT 48218316Swollman && (ws.state & WS_ST_DEFAULT)) 48318316Swollman return 0; 48418316Swollman 48518316Swollman if (RT->rt_state & RS_NET_SYN) { 48618316Swollman if (RT->rt_state & RS_NET_INT) { 48718316Swollman /* Do not send manual synthetic network routes 48818316Swollman * into the subnet. 48918316Swollman */ 49018316Swollman if (on_net(ws.to.sin_addr.s_addr, 49118316Swollman ntohl(dst), RT->rt_mask)) 49218316Swollman return 0; 49318316Swollman 49418316Swollman } else { 49518316Swollman /* Do not send automatic synthetic network routes 49637908Scharnier * if they are not needed because no RIPv1 listeners 49718316Swollman * can hear them. 49818316Swollman */ 49918316Swollman if (ws.state & WS_ST_RIP2_ALL) 50018316Swollman return 0; 50118316Swollman 50218316Swollman /* Do not send automatic synthetic network routes to 50318316Swollman * the real subnet. 50418316Swollman */ 50518316Swollman if (on_net(ws.to.sin_addr.s_addr, 50618316Swollman ntohl(dst), RT->rt_mask)) 50718316Swollman return 0; 50818316Swollman } 50918316Swollman nhop = 0; 51018316Swollman 51118316Swollman } else { 51218316Swollman /* Advertise the next hop if this is not a route for one 51318316Swollman * of our interfaces and the next hop is on the same 51418316Swollman * network as the target. 51546303Smarkm * The final determination is made by supply_out(). 51618316Swollman */ 51718316Swollman if (!(RT->rt_state & RS_IF) 51818316Swollman && RT->rt_gate != myaddr 51918316Swollman && RT->rt_gate != loopaddr) 52018316Swollman nhop = RT->rt_gate; 52118316Swollman else 52218316Swollman nhop = 0; 52318316Swollman } 52418316Swollman 52518316Swollman metric = RT->rt_metric; 52618316Swollman ags = 0; 52718316Swollman 52818316Swollman if (RT->rt_state & RS_MHOME) { 52918316Swollman /* retain host route of multi-homed servers */ 53018316Swollman ; 53118316Swollman 53218316Swollman } else if (RT_ISHOST(RT)) { 53346303Smarkm /* We should always suppress (into existing network routes) 53446303Smarkm * the host routes for the local end of our point-to-point 53546303Smarkm * links. 53618316Swollman * If we are suppressing host routes in general, then do so. 53718316Swollman * Avoid advertising host routes onto their own network, 53818316Swollman * where they should be handled by proxy-ARP. 53918316Swollman */ 54018316Swollman if ((RT->rt_state & RS_LOCAL) 54118316Swollman || ridhosts 54218316Swollman || on_net(dst, ws.to_net, ws.to_mask)) 54318316Swollman ags |= AGS_SUPPRESS; 54418316Swollman 54546303Smarkm /* Aggregate stray host routes into network routes if allowed. 54646303Smarkm * We cannot aggregate host routes into small network routes 54746303Smarkm * without confusing RIPv1 listeners into thinking the 54846303Smarkm * network routes are host routes. 54946303Smarkm */ 55046303Smarkm if ((ws.state & WS_ST_AG) 55146303Smarkm && !(ws.state & WS_ST_RIP2_ALL)) 55246303Smarkm ags |= AGS_AGGREGATE; 55318316Swollman 55446303Smarkm } else { 55546303Smarkm /* Always suppress network routes into other, existing 55646303Smarkm * network routes 55718316Swollman */ 55818316Swollman ags |= AGS_SUPPRESS; 55918316Swollman 56018316Swollman /* Generate supernets if allowed. 56118316Swollman * If we can be heard by RIPv1 systems, we will 56218316Swollman * later convert back to ordinary nets. 56318316Swollman * This unifies dealing with received supernets. 56418316Swollman */ 56546303Smarkm if ((ws.state & WS_ST_AG) 56646303Smarkm && ((RT->rt_state & RS_SUBNET) 56746303Smarkm || (ws.state & WS_ST_SUPER_AG))) 56846303Smarkm ags |= AGS_AGGREGATE; 56918316Swollman } 57018316Swollman 57118316Swollman /* Do not send RIPv1 advertisements of subnets to other 57218316Swollman * networks. If possible, multicast them by RIPv2. 57318316Swollman */ 57418316Swollman if ((RT->rt_state & RS_SUBNET) 57518316Swollman && !(ws.state & WS_ST_RIP2_ALL) 57646303Smarkm && !on_net(dst, ws.to_std_net, ws.to_std_mask)) 57746303Smarkm ags |= AGS_RIPV2 | AGS_AGGREGATE; 57818316Swollman 57946303Smarkm 58018316Swollman /* Do not send a route back to where it came from, except in 58118316Swollman * response to a query. This is "split-horizon". That means not 58218316Swollman * advertising back to the same network and so via the same interface. 58318316Swollman * 58418316Swollman * We want to suppress routes that might have been fragmented 58518316Swollman * from this route by a RIPv1 router and sent back to us, and so we 58618316Swollman * cannot forget this route here. Let the split-horizon route 58746303Smarkm * suppress the fragmented routes and then itself be forgotten. 58818316Swollman * 58918316Swollman * Include the routes for both ends of point-to-point interfaces 59020339Swollman * among those suppressed by split-horizon, since the other side 59120339Swollman * should knows them as well as we do. 59246303Smarkm * 59346303Smarkm * Notice spare routes with the same metric that we are about to 59446303Smarkm * advertise, to split the horizon on redundant, inactive paths. 59518316Swollman */ 59646303Smarkm if (ws.ifp != 0 59718316Swollman && !(ws.state & WS_ST_QUERY) 59818316Swollman && (ws.state & WS_ST_TO_ON_NET) 59918316Swollman && (!(RT->rt_state & RS_IF) 60018316Swollman || ws.ifp->int_if_flags & IFF_POINTOPOINT)) { 60146303Smarkm for (rts = RT->rt_spares, i = NUM_SPARES; i != 0; i--, rts++) { 60246303Smarkm if (rts->rts_metric > metric 60346303Smarkm || rts->rts_ifp != ws.ifp) 60446303Smarkm continue; 60546303Smarkm 60646303Smarkm /* If we do not mark the route with AGS_SPLIT_HZ here, 60746303Smarkm * it will be poisoned-reverse, or advertised back 60846303Smarkm * toward its source with an infinite metric. 60946303Smarkm * If we have recently advertised the route with a 61046303Smarkm * better metric than we now have, then we should 61146303Smarkm * poison-reverse the route before suppressing it for 61246303Smarkm * split-horizon. 61346303Smarkm * 61446303Smarkm * In almost all cases, if there is no spare for the 61546303Smarkm * route then it is either old and dead or a brand 61646303Smarkm * new route. If it is brand new, there is no need 61746303Smarkm * for poison-reverse. If it is old and dead, it 61846303Smarkm * is already poisoned. 61946303Smarkm */ 62046303Smarkm if (RT->rt_poison_time < now_expire 62146303Smarkm || RT->rt_poison_metric >= metric 62246303Smarkm || RT->rt_spares[1].rts_gate == 0) { 62346303Smarkm ags |= AGS_SPLIT_HZ; 62446303Smarkm ags &= ~AGS_SUPPRESS; 62546303Smarkm } 62646303Smarkm metric = HOPCNT_INFINITY; 62746303Smarkm break; 62818316Swollman } 62918316Swollman } 63018316Swollman 63146303Smarkm /* Keep track of the best metric with which the 63246303Smarkm * route has been advertised recently. 63346303Smarkm */ 63446303Smarkm if (RT->rt_poison_metric >= metric 63546303Smarkm || RT->rt_poison_time < now_expire) { 63646303Smarkm RT->rt_poison_time = now.tv_sec; 63746303Smarkm RT->rt_poison_metric = metric; 63846303Smarkm } 63946303Smarkm 64018316Swollman /* Adjust the outgoing metric by the cost of the link. 64146303Smarkm * Avoid aggregation when a route is counting to infinity. 64218316Swollman */ 64346303Smarkm pref = RT->rt_poison_metric + ws.metric; 64446303Smarkm metric += ws.metric; 64518316Swollman 64646303Smarkm /* Do not advertise stable routes that will be ignored, 64746303Smarkm * unless we are answering a query. 64846303Smarkm * If the route recently was advertised with a metric that 64946303Smarkm * would have been less than infinity through this interface, 65046303Smarkm * we need to continue to advertise it in order to poison it. 65146303Smarkm */ 65246303Smarkm if (metric >= HOPCNT_INFINITY) { 65319880Swollman if (!(ws.state & WS_ST_QUERY) 65419880Swollman && (pref >= HOPCNT_INFINITY 65519880Swollman || RT->rt_poison_time < now_garbage)) 65618316Swollman return 0; 65718316Swollman 65818316Swollman metric = HOPCNT_INFINITY; 65918316Swollman } 66018316Swollman 66118316Swollman ag_check(dst, RT->rt_mask, 0, nhop, metric, pref, 66218316Swollman RT->rt_seqno, RT->rt_tag, ags, supply_out); 66318316Swollman return 0; 66418316Swollman#undef RT 66518316Swollman} 66618316Swollman 66718316Swollman 66818316Swollman/* Supply dst with the contents of the routing tables. 66918316Swollman * If this won't fit in one packet, chop it up into several. 67018316Swollman */ 67118316Swollmanvoid 67218316Swollmansupply(struct sockaddr_in *dst, 67318316Swollman struct interface *ifp, /* output interface */ 67418316Swollman enum output_type type, 67518316Swollman int flash, /* 1=flash update */ 67619880Swollman int vers, /* RIP version */ 67719880Swollman int passwd_ok) /* OK to include cleartext password */ 67818316Swollman{ 67918316Swollman struct rt_entry *rt; 68019880Swollman int def_metric; 68118316Swollman 68218316Swollman 68318316Swollman ws.state = 0; 68418316Swollman ws.gen_limit = 1024; 68518316Swollman 68618316Swollman ws.to = *dst; 68718316Swollman ws.to_std_mask = std_mask(ws.to.sin_addr.s_addr); 68818316Swollman ws.to_std_net = ntohl(ws.to.sin_addr.s_addr) & ws.to_std_mask; 68918316Swollman 69018316Swollman if (ifp != 0) { 69118316Swollman ws.to_mask = ifp->int_mask; 69218316Swollman ws.to_net = ifp->int_net; 69318316Swollman if (on_net(ws.to.sin_addr.s_addr, ws.to_net, ws.to_mask)) 69418316Swollman ws.state |= WS_ST_TO_ON_NET; 69518316Swollman 69618316Swollman } else { 69718316Swollman ws.to_mask = ripv1_mask_net(ws.to.sin_addr.s_addr, 0); 69818316Swollman ws.to_net = ntohl(ws.to.sin_addr.s_addr) & ws.to_mask; 69918316Swollman rt = rtfind(dst->sin_addr.s_addr); 70018316Swollman if (rt) 70118316Swollman ifp = rt->rt_ifp; 70218316Swollman } 70318316Swollman 70418316Swollman ws.npackets = 0; 70518316Swollman if (flash) 70618316Swollman ws.state |= WS_ST_FLASH; 70718316Swollman 70818316Swollman if ((ws.ifp = ifp) == 0) { 70918316Swollman ws.metric = 1; 71018316Swollman } else { 71118316Swollman /* Adjust the advertised metric by the outgoing interface 71218316Swollman * metric. 71318316Swollman */ 71418316Swollman ws.metric = ifp->int_metric+1; 71518316Swollman } 71618316Swollman 71718316Swollman ripv12_buf.rip.rip_vers = vers; 71818316Swollman 71918316Swollman switch (type) { 72018316Swollman case OUT_MULTICAST: 72146303Smarkm if (ifp->int_if_flags & IFF_MULTICAST) 72246303Smarkm v2buf.type = OUT_MULTICAST; 72346303Smarkm else 72446303Smarkm v2buf.type = NO_OUT_MULTICAST; 72519880Swollman v12buf.type = OUT_BROADCAST; 72618316Swollman break; 72746303Smarkm 72846303Smarkm case OUT_QUERY: 72946303Smarkm ws.state |= WS_ST_QUERY; 73046303Smarkm /* fall through */ 73146303Smarkm case OUT_BROADCAST: 73218316Swollman case OUT_UNICAST: 73319880Swollman v2buf.type = (vers == RIPv2) ? type : NO_OUT_RIPV2; 73419880Swollman v12buf.type = type; 73518316Swollman break; 73646303Smarkm 73746303Smarkm case NO_OUT_MULTICAST: 73846303Smarkm case NO_OUT_RIPV2: 73946303Smarkm break; /* no output */ 74018316Swollman } 74118316Swollman 74218316Swollman if (vers == RIPv2) { 74318316Swollman /* full RIPv2 only if cannot be heard by RIPv1 listeners */ 74418316Swollman if (type != OUT_BROADCAST) 74518316Swollman ws.state |= WS_ST_RIP2_ALL; 74646303Smarkm if ((ws.state & WS_ST_QUERY) 74746303Smarkm || !(ws.state & WS_ST_TO_ON_NET)) { 74818316Swollman ws.state |= (WS_ST_AG | WS_ST_SUPER_AG); 74919880Swollman } else if (ifp == 0 || !(ifp->int_state & IS_NO_AG)) { 75018316Swollman ws.state |= WS_ST_AG; 75118316Swollman if (type != OUT_BROADCAST 75246303Smarkm && (ifp == 0 75346303Smarkm || !(ifp->int_state & IS_NO_SUPER_AG))) 75418316Swollman ws.state |= WS_ST_SUPER_AG; 75518316Swollman } 75618316Swollman } 75718316Swollman 75819880Swollman ws.a = (vers == RIPv2) ? find_auth(ifp) : 0; 75920339Swollman if (!passwd_ok && ws.a != 0 && ws.a->type == RIP_AUTH_PW) 76019880Swollman ws.a = 0; 76120339Swollman clr_ws_buf(&v12buf,ws.a); 76220339Swollman clr_ws_buf(&v2buf,ws.a); 76319880Swollman 76419880Swollman /* Fake a default route if asked and if there is not already 76519880Swollman * a better, real default route. 76619880Swollman */ 76719880Swollman if (supplier && (def_metric = ifp->int_d_metric) != 0) { 76819880Swollman if (0 == (rt = rtget(RIP_DEFAULT, 0)) 76919880Swollman || rt->rt_metric+ws.metric >= def_metric) { 77018316Swollman ws.state |= WS_ST_DEFAULT; 77119880Swollman ag_check(0, 0, 0, 0, def_metric, def_metric, 77218316Swollman 0, 0, 0, supply_out); 77319880Swollman } else { 77419880Swollman def_metric = rt->rt_metric+ws.metric; 77518316Swollman } 77619880Swollman 77719880Swollman /* If both RIPv2 and the poor-man's router discovery 77819880Swollman * kludge are on, arrange to advertise an extra 77919880Swollman * default route via RIPv1. 78019880Swollman */ 78118316Swollman if ((ws.state & WS_ST_RIP2_ALL) 78218316Swollman && (ifp->int_state & IS_PM_RDISC)) { 78318316Swollman ripv12_buf.rip.rip_vers = RIPv1; 78419880Swollman v12buf.n->n_family = RIP_AF_INET; 78519880Swollman v12buf.n->n_dst = htonl(RIP_DEFAULT); 78619880Swollman v12buf.n->n_metric = htonl(def_metric); 78719880Swollman v12buf.n++; 78818316Swollman } 78918316Swollman } 79018316Swollman 79118316Swollman (void)rn_walktree(rhead, walk_supply, 0); 79218316Swollman ag_flush(0,0,supply_out); 79318316Swollman 79418316Swollman /* Flush the packet buffers, provided they are not empty and 79518316Swollman * do not contain only the password. 79618316Swollman */ 79719880Swollman if (v12buf.n != v12buf.base 79819880Swollman && (v12buf.n > v12buf.base+1 79919880Swollman || v12buf.base->n_family != RIP_AF_AUTH)) 80019880Swollman supply_write(&v12buf); 80119880Swollman if (v2buf.n != v2buf.base 80219880Swollman && (v2buf.n > v2buf.base+1 80319880Swollman || v2buf.base->n_family != RIP_AF_AUTH)) 80419880Swollman supply_write(&v2buf); 80518316Swollman 80618316Swollman /* If we sent nothing and this is an answer to a query, send 80718316Swollman * an empty buffer. 80818316Swollman */ 80918316Swollman if (ws.npackets == 0 81018316Swollman && (ws.state & WS_ST_QUERY)) 81119880Swollman supply_write(&v12buf); 81218316Swollman} 81318316Swollman 81418316Swollman 81518316Swollman/* send all of the routing table or just do a flash update 81618316Swollman */ 81718316Swollmanvoid 81818316Swollmanrip_bcast(int flash) 81918316Swollman{ 82018316Swollman#ifdef _HAVE_SIN_LEN 82164131Ssheldonh static struct sockaddr_in dst = {sizeof(dst), AF_INET, 0, {0}, {0}}; 82218316Swollman#else 82318316Swollman static struct sockaddr_in dst = {AF_INET}; 82418316Swollman#endif 82518316Swollman struct interface *ifp; 82618316Swollman enum output_type type; 82718316Swollman int vers; 82818316Swollman struct timeval rtime; 82918316Swollman 83018316Swollman 83118316Swollman need_flash = 0; 83218316Swollman intvl_random(&rtime, MIN_WAITTIME, MAX_WAITTIME); 83318316Swollman no_flash = rtime; 83418316Swollman timevaladd(&no_flash, &now); 83518316Swollman 83618316Swollman if (rip_sock < 0) 83718316Swollman return; 83818316Swollman 83919880Swollman trace_act("send %s and inhibit dynamic updates for %.3f sec", 84018316Swollman flash ? "dynamic update" : "all routes", 84118316Swollman rtime.tv_sec + ((float)rtime.tv_usec)/1000000.0); 84218316Swollman 84318316Swollman for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) { 84419880Swollman /* Skip interfaces not doing RIP. 84519880Swollman * Do try broken interfaces to see if they have healed. 84618316Swollman */ 84719880Swollman if (IS_RIP_OUT_OFF(ifp->int_state)) 84818316Swollman continue; 84918316Swollman 85018316Swollman /* skip turned off interfaces */ 85146303Smarkm if (!iff_up(ifp->int_if_flags)) 85218316Swollman continue; 85318316Swollman 85419880Swollman vers = (ifp->int_state & IS_NO_RIPV1_OUT) ? RIPv2 : RIPv1; 85518316Swollman 85618316Swollman if (ifp->int_if_flags & IFF_BROADCAST) { 85718316Swollman /* ordinary, hardware interface */ 85818316Swollman dst.sin_addr.s_addr = ifp->int_brdaddr; 85919880Swollman 86018316Swollman if (vers == RIPv2 86146303Smarkm && !(ifp->int_state & IS_NO_RIP_MCAST)) { 86218316Swollman type = OUT_MULTICAST; 86318316Swollman } else { 86418316Swollman type = OUT_BROADCAST; 86518316Swollman } 86618316Swollman 86718316Swollman } else if (ifp->int_if_flags & IFF_POINTOPOINT) { 86818316Swollman /* point-to-point hardware interface */ 86918316Swollman dst.sin_addr.s_addr = ifp->int_dstaddr; 87018316Swollman type = OUT_UNICAST; 87118316Swollman 87219880Swollman } else if (ifp->int_state & IS_REMOTE) { 87318316Swollman /* remote interface */ 87418316Swollman dst.sin_addr.s_addr = ifp->int_addr; 87518316Swollman type = OUT_UNICAST; 87619880Swollman 87719880Swollman } else { 87819880Swollman /* ATM, HIPPI, etc. */ 87919880Swollman continue; 88018316Swollman } 88118316Swollman 88219880Swollman supply(&dst, ifp, type, flash, vers, 1); 88318316Swollman } 88418316Swollman 88518316Swollman update_seqno++; /* all routes are up to date */ 88618316Swollman} 88718316Swollman 88818316Swollman 88918316Swollman/* Ask for routes 89018316Swollman * Do it only once to an interface, and not even after the interface 89118316Swollman * was broken and recovered. 89218316Swollman */ 89318316Swollmanvoid 89418316Swollmanrip_query(void) 89518316Swollman{ 89618316Swollman#ifdef _HAVE_SIN_LEN 89764131Ssheldonh static struct sockaddr_in dst = {sizeof(dst), AF_INET, 0, {0}, {0}}; 89818316Swollman#else 89918316Swollman static struct sockaddr_in dst = {AF_INET}; 90018316Swollman#endif 90118316Swollman struct interface *ifp; 90218316Swollman struct rip buf; 90318316Swollman enum output_type type; 90418316Swollman 90518316Swollman 90618316Swollman if (rip_sock < 0) 90718316Swollman return; 90818316Swollman 90946303Smarkm memset(&buf, 0, sizeof(buf)); 91018316Swollman 91118316Swollman for (ifp = ifnet; ifp; ifp = ifp->int_next) { 91219880Swollman /* Skip interfaces those already queried. 91319880Swollman * Do not ask via interfaces through which we don't 91419880Swollman * accept input. Do not ask via interfaces that cannot 91519880Swollman * send RIP packets. 91619880Swollman * Do try broken interfaces to see if they have healed. 91718316Swollman */ 91819880Swollman if (IS_RIP_IN_OFF(ifp->int_state) 91919880Swollman || ifp->int_query_time != NEVER) 92018316Swollman continue; 92118316Swollman 92218316Swollman /* skip turned off interfaces */ 92346303Smarkm if (!iff_up(ifp->int_if_flags)) 92418316Swollman continue; 92518316Swollman 92619880Swollman buf.rip_vers = (ifp->int_state&IS_NO_RIPV1_OUT) ? RIPv2:RIPv1; 92718316Swollman buf.rip_cmd = RIPCMD_REQUEST; 92818316Swollman buf.rip_nets[0].n_family = RIP_AF_UNSPEC; 92918316Swollman buf.rip_nets[0].n_metric = htonl(HOPCNT_INFINITY); 93018316Swollman 93146303Smarkm /* Send a RIPv1 query only if allowed and if we will 93246303Smarkm * listen to RIPv1 routers. 93346303Smarkm */ 93446303Smarkm if ((ifp->int_state & IS_NO_RIPV1_OUT) 93546303Smarkm || (ifp->int_state & IS_NO_RIPV1_IN)) { 93646303Smarkm buf.rip_vers = RIPv2; 93746303Smarkm } else { 93846303Smarkm buf.rip_vers = RIPv1; 93946303Smarkm } 94046303Smarkm 94118316Swollman if (ifp->int_if_flags & IFF_BROADCAST) { 94218316Swollman /* ordinary, hardware interface */ 94318316Swollman dst.sin_addr.s_addr = ifp->int_brdaddr; 94446303Smarkm 94546303Smarkm /* Broadcast RIPv1 queries and RIPv2 queries 94646303Smarkm * when the hardware cannot multicast. 94718316Swollman */ 94818316Swollman if (buf.rip_vers == RIPv2 94946303Smarkm && (ifp->int_if_flags & IFF_MULTICAST) 95046303Smarkm && !(ifp->int_state & IS_NO_RIP_MCAST)) { 95118316Swollman type = OUT_MULTICAST; 95218316Swollman } else { 95318316Swollman type = OUT_BROADCAST; 95418316Swollman } 95518316Swollman 95618316Swollman } else if (ifp->int_if_flags & IFF_POINTOPOINT) { 95718316Swollman /* point-to-point hardware interface */ 95818316Swollman dst.sin_addr.s_addr = ifp->int_dstaddr; 95918316Swollman type = OUT_UNICAST; 96018316Swollman 96119880Swollman } else if (ifp->int_state & IS_REMOTE) { 96218316Swollman /* remote interface */ 96318316Swollman dst.sin_addr.s_addr = ifp->int_addr; 96418316Swollman type = OUT_UNICAST; 96519880Swollman 96619880Swollman } else { 96719880Swollman /* ATM, HIPPI, etc. */ 96819880Swollman continue; 96918316Swollman } 97018316Swollman 97119880Swollman ifp->int_query_time = now.tv_sec+SUPPLY_INTERVAL; 97218316Swollman if (output(type, &dst, ifp, &buf, sizeof(buf)) < 0) 97318316Swollman if_sick(ifp); 97418316Swollman } 97518316Swollman} 976