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 * 4. Neither the name of the University nor the names of its contributors 1418316Swollman * may be used to endorse or promote products derived from this software 1518316Swollman * without specific prior written permission. 1618316Swollman * 1718316Swollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1818316Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1918316Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2018316Swollman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2118316Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2218316Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2318316Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2418316Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2518316Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2618316Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2718316Swollman * SUCH DAMAGE. 2846303Smarkm * 2950476Speter * $FreeBSD$ 3018316Swollman */ 3118316Swollman 3246303Smarkm#include "defs.h" 3346303Smarkm 34126250Sbms#ifdef __NetBSD__ 3546303Smarkm__RCSID("$NetBSD$"); 36126250Sbms#elif defined(__FreeBSD__) 37126250Sbms__RCSID("$FreeBSD$"); 38126250Sbms#else 39126250Sbms__RCSID("$Revision: 2.27 $"); 40126250Sbms#ident "$Revision: 2.27 $" 4118316Swollman#endif 4218316Swollman 4318316Swollman 4446303Smarkmu_int update_seqno; 4518316Swollman 4618316Swollman 4718316Swollman/* walk the tree of routes with this for output 4818316Swollman */ 49190715Sphkstatic struct { 5018316Swollman struct sockaddr_in to; 5118316Swollman naddr to_mask; 5218316Swollman naddr to_net; 5318316Swollman naddr to_std_mask; 5418316Swollman naddr to_std_net; 5518316Swollman struct interface *ifp; /* usually output interface */ 5620339Swollman struct auth *a; 5718316Swollman char metric; /* adjust metrics by interface */ 5818316Swollman int npackets; 5918316Swollman int gen_limit; 6018316Swollman u_int state; 6118316Swollman#define WS_ST_FLASH 0x001 /* send only changed routes */ 6219880Swollman#define WS_ST_RIP2_ALL 0x002 /* send full featured RIPv2 */ 6319880Swollman#define WS_ST_AG 0x004 /* ok to aggregate subnets */ 6419880Swollman#define WS_ST_SUPER_AG 0x008 /* ok to aggregate networks */ 6546303Smarkm#define WS_ST_QUERY 0x010 /* responding to a query */ 6646303Smarkm#define WS_ST_TO_ON_NET 0x020 /* sending onto one of our nets */ 6746303Smarkm#define WS_ST_DEFAULT 0x040 /* faking a default */ 6818316Swollman} ws; 6918316Swollman 7018316Swollman/* A buffer for what can be heard by both RIPv1 and RIPv2 listeners */ 7119880Swollmanstruct ws_buf v12buf; 72190715Sphkstatic union pkt_buf ripv12_buf; 7318316Swollman 7418316Swollman/* Another for only RIPv2 listeners */ 75190715Sphkstatic struct ws_buf v2buf; 76190715Sphkstatic union pkt_buf rip_v2_buf; 7718316Swollman 7818316Swollman 7918316Swollman 8019880Swollmanvoid 8119880Swollmanbufinit(void) 8219880Swollman{ 8319880Swollman ripv12_buf.rip.rip_cmd = RIPCMD_RESPONSE; 8419880Swollman v12buf.buf = &ripv12_buf.rip; 8519880Swollman v12buf.base = &v12buf.buf->rip_nets[0]; 8619880Swollman 8719880Swollman rip_v2_buf.rip.rip_cmd = RIPCMD_RESPONSE; 8819880Swollman rip_v2_buf.rip.rip_vers = RIPv2; 8919880Swollman v2buf.buf = &rip_v2_buf.rip; 9019880Swollman v2buf.base = &v2buf.buf->rip_nets[0]; 9119880Swollman} 9219880Swollman 9319880Swollman 9418316Swollman/* Send the contents of the global buffer via the non-multicast socket 9518316Swollman */ 9618316Swollmanint /* <0 on failure */ 9718316Swollmanoutput(enum output_type type, 9818316Swollman struct sockaddr_in *dst, /* send to here */ 9918316Swollman struct interface *ifp, 10018316Swollman struct rip *buf, 10118316Swollman int size) /* this many bytes */ 10218316Swollman{ 103126250Sbms struct sockaddr_in osin; 10418316Swollman int flags; 10546303Smarkm const char *msg; 10618316Swollman int res; 10718316Swollman int soc; 10818316Swollman int serrno; 10918316Swollman 110190718Sphk assert(ifp != NULL); 111126250Sbms osin = *dst; 112126250Sbms if (osin.sin_port == 0) 113126250Sbms osin.sin_port = htons(RIP_PORT); 11418316Swollman#ifdef _HAVE_SIN_LEN 115126250Sbms if (osin.sin_len == 0) 116126250Sbms osin.sin_len = sizeof(osin); 11718316Swollman#endif 11818316Swollman 11918316Swollman soc = rip_sock; 12018316Swollman flags = 0; 12118316Swollman 12218316Swollman switch (type) { 12318316Swollman case OUT_QUERY: 12418316Swollman msg = "Answer Query"; 12518316Swollman if (soc < 0) 12618316Swollman soc = ifp->int_rip_sock; 12718316Swollman break; 12818316Swollman case OUT_UNICAST: 12918316Swollman msg = "Send"; 13018316Swollman if (soc < 0) 13118316Swollman soc = ifp->int_rip_sock; 13218316Swollman flags = MSG_DONTROUTE; 13318316Swollman break; 13418316Swollman case OUT_BROADCAST: 13518316Swollman if (ifp->int_if_flags & IFF_POINTOPOINT) { 13618316Swollman msg = "Send"; 13718316Swollman } else { 13818316Swollman msg = "Send bcast"; 13918316Swollman } 14018316Swollman flags = MSG_DONTROUTE; 14118316Swollman break; 14218316Swollman case OUT_MULTICAST: 143190712Sphk if ((ifp->int_if_flags & (IFF_POINTOPOINT|IFF_MULTICAST)) == 144190709Sphk IFF_POINTOPOINT) { 14518316Swollman msg = "Send pt-to-pt"; 14618316Swollman } else if (ifp->int_state & IS_DUP) { 14718316Swollman trace_act("abort multicast output via %s" 14819880Swollman " with duplicate address", 14918316Swollman ifp->int_name); 15018316Swollman return 0; 15118316Swollman } else { 15218316Swollman msg = "Send mcast"; 15318316Swollman if (rip_sock_mcast != ifp) { 154180993Sphk struct ip_mreqn mreqn; 155180993Sphk 156180993Sphk memset(&mreqn, 0, sizeof(struct ip_mreqn)); 157180993Sphk mreqn.imr_ifindex = ifp->int_index; 15818316Swollman if (0 > setsockopt(rip_sock, 159180993Sphk IPPROTO_IP, 160180993Sphk IP_MULTICAST_IF, 161180993Sphk &mreqn, 162180993Sphk sizeof(mreqn))) { 16318316Swollman serrno = errno; 164180993Sphk LOGERR("setsockopt(rip_sock, " 16518316Swollman "IP_MULTICAST_IF)"); 16618316Swollman errno = serrno; 16718316Swollman ifp = 0; 16818316Swollman return -1; 16918316Swollman } 17018316Swollman rip_sock_mcast = ifp; 17118316Swollman } 172126250Sbms osin.sin_addr.s_addr = htonl(INADDR_RIP_GROUP); 17318316Swollman } 17420339Swollman break; 17518316Swollman 17618316Swollman case NO_OUT_MULTICAST: 17718316Swollman case NO_OUT_RIPV2: 17820339Swollman default: 17920339Swollman#ifdef DEBUG 18020339Swollman abort(); 18120339Swollman#endif 18220339Swollman return -1; 18318316Swollman } 18418316Swollman 185126250Sbms trace_rip(msg, "to", &osin, ifp, buf, size); 18618316Swollman 18718316Swollman res = sendto(soc, buf, size, flags, 188126250Sbms (struct sockaddr *)&osin, sizeof(osin)); 18918316Swollman if (res < 0 19018316Swollman && (ifp == 0 || !(ifp->int_state & IS_BROKE))) { 19118316Swollman serrno = errno; 19218316Swollman msglog("%s sendto(%s%s%s.%d): %s", msg, 19318316Swollman ifp != 0 ? ifp->int_name : "", 19418316Swollman ifp != 0 ? ", " : "", 195126250Sbms inet_ntoa(osin.sin_addr), 196126250Sbms ntohs(osin.sin_port), 19718316Swollman strerror(errno)); 19818316Swollman errno = serrno; 19918316Swollman } 20018316Swollman 20118316Swollman return res; 20218316Swollman} 20318316Swollman 20418316Swollman 20520339Swollman/* Find the first key for a packet to send. 20637908Scharnier * Try for a key that is eligible and has not expired, but settle for 20719880Swollman * the last key if they have all expired. 20819880Swollman * If no key is ready yet, give up. 20918316Swollman */ 21020339Swollmanstruct auth * 21119880Swollmanfind_auth(struct interface *ifp) 21218316Swollman{ 21320339Swollman struct auth *ap, *res; 21419880Swollman int i; 21519880Swollman 21619880Swollman 21720339Swollman if (ifp == 0) 21819880Swollman return 0; 21920339Swollman 22019880Swollman res = 0; 22120339Swollman ap = ifp->int_auth; 22219880Swollman for (i = 0; i < MAX_AUTH_KEYS; i++, ap++) { 22320339Swollman /* stop looking after the last key */ 22420339Swollman if (ap->type == RIP_AUTH_NONE) 22520339Swollman break; 22646303Smarkm 22720339Swollman /* ignore keys that are not ready yet */ 22820339Swollman if ((u_long)ap->start > (u_long)clk.tv_sec) 22920339Swollman continue; 23020339Swollman 23120339Swollman if ((u_long)ap->end < (u_long)clk.tv_sec) { 23220339Swollman /* note best expired password as a fall-back */ 23320339Swollman if (res == 0 || (u_long)ap->end > (u_long)res->end) 23420339Swollman res = ap; 23520339Swollman continue; 23620339Swollman } 23720339Swollman 23820339Swollman /* note key with the best future */ 23920339Swollman if (res == 0 || (u_long)res->end < (u_long)ap->end) 24019880Swollman res = ap; 24118316Swollman } 24219880Swollman return res; 24318316Swollman} 24418316Swollman 24518316Swollman 24619880Swollmanvoid 24719880Swollmanclr_ws_buf(struct ws_buf *wb, 24820339Swollman struct auth *ap) 24919880Swollman{ 25019880Swollman struct netauth *na; 25119880Swollman 25219880Swollman wb->lim = wb->base + NETS_LEN; 25319880Swollman wb->n = wb->base; 25446303Smarkm memset(wb->n, 0, NETS_LEN*sizeof(*wb->n)); 25519880Swollman 25646303Smarkm /* (start to) install authentication if appropriate 25719880Swollman */ 25819880Swollman if (ap == 0) 25919880Swollman return; 26046303Smarkm 26119880Swollman na = (struct netauth*)wb->n; 26220339Swollman if (ap->type == RIP_AUTH_PW) { 26319880Swollman na->a_family = RIP_AF_AUTH; 26419880Swollman na->a_type = RIP_AUTH_PW; 26546303Smarkm memcpy(na->au.au_pw, ap->key, sizeof(na->au.au_pw)); 26619880Swollman wb->n++; 26719880Swollman 26820339Swollman } else if (ap->type == RIP_AUTH_MD5) { 26919880Swollman na->a_family = RIP_AF_AUTH; 27019880Swollman na->a_type = RIP_AUTH_MD5; 27119880Swollman na->au.a_md5.md5_keyid = ap->keyid; 272126250Sbms na->au.a_md5.md5_auth_len = RIP_AUTH_MD5_KEY_LEN; 27346303Smarkm na->au.a_md5.md5_seqno = htonl(clk.tv_sec); 27419880Swollman wb->n++; 27519880Swollman wb->lim--; /* make room for trailer */ 27619880Swollman } 27719880Swollman} 27819880Swollman 27919880Swollman 28019880Swollmanvoid 28119880Swollmanend_md5_auth(struct ws_buf *wb, 28220339Swollman struct auth *ap) 28319880Swollman{ 28419880Swollman struct netauth *na, *na2; 28519880Swollman MD5_CTX md5_ctx; 28646303Smarkm int len; 28719880Swollman 28819880Swollman 28919880Swollman na = (struct netauth*)wb->base; 29019880Swollman na2 = (struct netauth*)wb->n; 29146303Smarkm len = (char *)na2-(char *)wb->buf; 29219880Swollman na2->a_family = RIP_AF_AUTH; 29346303Smarkm na2->a_type = htons(1); 29446303Smarkm na->au.a_md5.md5_pkt_len = htons(len); 29519880Swollman MD5Init(&md5_ctx); 296126250Sbms MD5Update(&md5_ctx, (u_char *)wb->buf, len + RIP_AUTH_MD5_HASH_XTRA); 297126250Sbms MD5Update(&md5_ctx, ap->key, RIP_AUTH_MD5_KEY_LEN); 29819880Swollman MD5Final(na2->au.au_pw, &md5_ctx); 29919880Swollman wb->n++; 30019880Swollman} 30119880Swollman 30219880Swollman 30318316Swollman/* Send the buffer 30418316Swollman */ 30518316Swollmanstatic void 30618316Swollmansupply_write(struct ws_buf *wb) 30718316Swollman{ 30818316Swollman /* Output multicast only if legal. 30937908Scharnier * If we would multicast and it would be illegal, then discard the 31018316Swollman * packet. 31118316Swollman */ 31218316Swollman switch (wb->type) { 31318316Swollman case NO_OUT_MULTICAST: 31419880Swollman trace_pkt("skip multicast to %s because impossible", 31518316Swollman naddr_ntoa(ws.to.sin_addr.s_addr)); 31618316Swollman break; 31718316Swollman case NO_OUT_RIPV2: 31818316Swollman break; 31918316Swollman default: 32020339Swollman if (ws.a != 0 && ws.a->type == RIP_AUTH_MD5) 32119880Swollman end_md5_auth(wb,ws.a); 32218316Swollman if (output(wb->type, &ws.to, ws.ifp, wb->buf, 32318316Swollman ((char *)wb->n - (char*)wb->buf)) < 0 32418316Swollman && ws.ifp != 0) 32518316Swollman if_sick(ws.ifp); 32618316Swollman ws.npackets++; 32718316Swollman break; 32818316Swollman } 32918316Swollman 33020339Swollman clr_ws_buf(wb,ws.a); 33118316Swollman} 33218316Swollman 33318316Swollman 33418316Swollman/* put an entry into the packet 33518316Swollman */ 33618316Swollmanstatic void 33718316Swollmansupply_out(struct ag_info *ag) 33818316Swollman{ 33918316Swollman int i; 34020339Swollman naddr mask, v1_mask, dst_h, ddst_h = 0; 34118316Swollman struct ws_buf *wb; 34218316Swollman 34318316Swollman 34418316Swollman /* Skip this route if doing a flash update and it and the routes 34518316Swollman * it aggregates have not changed recently. 34618316Swollman */ 34718316Swollman if (ag->ag_seqno < update_seqno 34818316Swollman && (ws.state & WS_ST_FLASH)) 34918316Swollman return; 35018316Swollman 35118316Swollman dst_h = ag->ag_dst_h; 35218316Swollman mask = ag->ag_mask; 35318316Swollman v1_mask = ripv1_mask_host(htonl(dst_h), 35418316Swollman (ws.state & WS_ST_TO_ON_NET) ? ws.ifp : 0); 35518316Swollman i = 0; 35618316Swollman 35718316Swollman /* If we are sending RIPv2 packets that cannot (or must not) be 35818316Swollman * heard by RIPv1 listeners, do not worry about sub- or supernets. 35918316Swollman * Subnets (from other networks) can only be sent via multicast. 36018316Swollman * A pair of subnet routes might have been promoted so that they 36118316Swollman * are legal to send by RIPv1. 36219880Swollman * If RIPv1 is off, use the multicast buffer. 36318316Swollman */ 36419880Swollman if ((ws.state & WS_ST_RIP2_ALL) 36518316Swollman || ((ag->ag_state & AGS_RIPV2) && v1_mask != mask)) { 36618316Swollman /* use the RIPv2-only buffer */ 36719880Swollman wb = &v2buf; 36818316Swollman 36918316Swollman } else { 37018316Swollman /* use the RIPv1-or-RIPv2 buffer */ 37119880Swollman wb = &v12buf; 37218316Swollman 37318316Swollman /* Convert supernet route into corresponding set of network 37418316Swollman * routes for RIPv1, but leave non-contiguous netmasks 37518316Swollman * to ag_check(). 37618316Swollman */ 37718316Swollman if (v1_mask > mask 37818316Swollman && mask + (mask & -mask) == 0) { 37918316Swollman ddst_h = v1_mask & -v1_mask; 38018316Swollman i = (v1_mask & ~mask)/ddst_h; 38118316Swollman 38218316Swollman if (i > ws.gen_limit) { 38318316Swollman /* Punt if we would have to generate an 38418316Swollman * unreasonable number of routes. 38518316Swollman */ 38646303Smarkm if (TRACECONTENTS) 38746303Smarkm trace_misc("sending %s-->%s as 1" 38846303Smarkm " instead of %d routes", 38946303Smarkm addrname(htonl(dst_h), mask, 39046303Smarkm 1), 39146303Smarkm naddr_ntoa(ws.to.sin_addr 39246303Smarkm .s_addr), 39346303Smarkm i+1); 39418316Swollman i = 0; 39518316Swollman 39618316Swollman } else { 39718316Swollman mask = v1_mask; 39818316Swollman ws.gen_limit -= i; 39918316Swollman } 40018316Swollman } 40118316Swollman } 40218316Swollman 40318316Swollman do { 40418316Swollman wb->n->n_family = RIP_AF_INET; 40518316Swollman wb->n->n_dst = htonl(dst_h); 40618316Swollman /* If the route is from router-discovery or we are 40718316Swollman * shutting down, admit only a bad metric. 40818316Swollman */ 40918316Swollman wb->n->n_metric = ((stopint || ag->ag_metric < 1) 41018316Swollman ? HOPCNT_INFINITY 41118316Swollman : ag->ag_metric); 41290868Smike wb->n->n_metric = htonl(wb->n->n_metric); 41319880Swollman /* Any non-zero bits in the supposedly unused RIPv1 fields 41419880Swollman * cause the old `routed` to ignore the route. 41519880Swollman * That means the mask and so forth cannot be sent 41619880Swollman * in the hybrid RIPv1/RIPv2 mode. 41719880Swollman */ 41819880Swollman if (ws.state & WS_ST_RIP2_ALL) { 41918316Swollman if (ag->ag_nhop != 0 42018316Swollman && ((ws.state & WS_ST_QUERY) 42118316Swollman || (ag->ag_nhop != ws.ifp->int_addr 42218316Swollman && on_net(ag->ag_nhop, 42318316Swollman ws.ifp->int_net, 42418316Swollman ws.ifp->int_mask)))) 42518316Swollman wb->n->n_nhop = ag->ag_nhop; 42619880Swollman wb->n->n_mask = htonl(mask); 42718316Swollman wb->n->n_tag = ag->ag_tag; 42818316Swollman } 42918316Swollman dst_h += ddst_h; 43018316Swollman 43118316Swollman if (++wb->n >= wb->lim) 43218316Swollman supply_write(wb); 43318316Swollman } while (i-- != 0); 43418316Swollman} 43518316Swollman 43618316Swollman 43718316Swollman/* supply one route from the table 43818316Swollman */ 43918316Swollman/* ARGSUSED */ 44018316Swollmanstatic int 44118316Swollmanwalk_supply(struct radix_node *rn, 44246303Smarkm struct walkarg *argp UNUSED) 44318316Swollman{ 44418316Swollman#define RT ((struct rt_entry *)rn) 44518316Swollman u_short ags; 44618316Swollman char metric, pref; 44718316Swollman naddr dst, nhop; 44846303Smarkm struct rt_spare *rts; 44946303Smarkm int i; 45018316Swollman 45118316Swollman 45219880Swollman /* Do not advertise external remote interfaces or passive interfaces. 45318316Swollman */ 45418316Swollman if ((RT->rt_state & RS_IF) 45518316Swollman && RT->rt_ifp != 0 45664131Ssheldonh && (RT->rt_ifp->int_state & IS_PASSIVE) 45718316Swollman && !(RT->rt_state & RS_MHOME)) 45818316Swollman return 0; 45918316Swollman 46018316Swollman /* If being quiet about our ability to forward, then 46119880Swollman * do not say anything unless responding to a query, 46219880Swollman * except about our main interface. 46318316Swollman */ 46419880Swollman if (!supplier && !(ws.state & WS_ST_QUERY) 46519880Swollman && !(RT->rt_state & RS_MHOME)) 46618316Swollman return 0; 46718316Swollman 46818316Swollman dst = RT->rt_dst; 46918316Swollman 47018316Swollman /* do not collide with the fake default route */ 47118316Swollman if (dst == RIP_DEFAULT 47218316Swollman && (ws.state & WS_ST_DEFAULT)) 47318316Swollman return 0; 47418316Swollman 47518316Swollman if (RT->rt_state & RS_NET_SYN) { 47618316Swollman if (RT->rt_state & RS_NET_INT) { 47718316Swollman /* Do not send manual synthetic network routes 47818316Swollman * into the subnet. 47918316Swollman */ 48018316Swollman if (on_net(ws.to.sin_addr.s_addr, 48118316Swollman ntohl(dst), RT->rt_mask)) 48218316Swollman return 0; 48318316Swollman 48418316Swollman } else { 48518316Swollman /* Do not send automatic synthetic network routes 48637908Scharnier * if they are not needed because no RIPv1 listeners 48718316Swollman * can hear them. 48818316Swollman */ 48918316Swollman if (ws.state & WS_ST_RIP2_ALL) 49018316Swollman return 0; 49118316Swollman 49218316Swollman /* Do not send automatic synthetic network routes to 49318316Swollman * the real subnet. 49418316Swollman */ 49518316Swollman if (on_net(ws.to.sin_addr.s_addr, 49618316Swollman ntohl(dst), RT->rt_mask)) 49718316Swollman return 0; 49818316Swollman } 49918316Swollman nhop = 0; 50018316Swollman 50118316Swollman } else { 50218316Swollman /* Advertise the next hop if this is not a route for one 50318316Swollman * of our interfaces and the next hop is on the same 50418316Swollman * network as the target. 50546303Smarkm * The final determination is made by supply_out(). 50618316Swollman */ 50718316Swollman if (!(RT->rt_state & RS_IF) 50818316Swollman && RT->rt_gate != myaddr 50918316Swollman && RT->rt_gate != loopaddr) 51018316Swollman nhop = RT->rt_gate; 51118316Swollman else 51218316Swollman nhop = 0; 51318316Swollman } 51418316Swollman 51518316Swollman metric = RT->rt_metric; 51618316Swollman ags = 0; 51718316Swollman 51818316Swollman if (RT->rt_state & RS_MHOME) { 51918316Swollman /* retain host route of multi-homed servers */ 52018316Swollman ; 52118316Swollman 52218316Swollman } else if (RT_ISHOST(RT)) { 52346303Smarkm /* We should always suppress (into existing network routes) 52446303Smarkm * the host routes for the local end of our point-to-point 52546303Smarkm * links. 52618316Swollman * If we are suppressing host routes in general, then do so. 52718316Swollman * Avoid advertising host routes onto their own network, 52818316Swollman * where they should be handled by proxy-ARP. 52918316Swollman */ 53018316Swollman if ((RT->rt_state & RS_LOCAL) 53118316Swollman || ridhosts 53218316Swollman || on_net(dst, ws.to_net, ws.to_mask)) 53318316Swollman ags |= AGS_SUPPRESS; 53418316Swollman 53546303Smarkm /* Aggregate stray host routes into network routes if allowed. 53646303Smarkm * We cannot aggregate host routes into small network routes 53746303Smarkm * without confusing RIPv1 listeners into thinking the 53846303Smarkm * network routes are host routes. 53946303Smarkm */ 540126250Sbms if ((ws.state & WS_ST_AG) && (ws.state & WS_ST_RIP2_ALL)) 54146303Smarkm ags |= AGS_AGGREGATE; 54218316Swollman 54346303Smarkm } else { 54446303Smarkm /* Always suppress network routes into other, existing 54546303Smarkm * network routes 54618316Swollman */ 54718316Swollman ags |= AGS_SUPPRESS; 54818316Swollman 54918316Swollman /* Generate supernets if allowed. 55018316Swollman * If we can be heard by RIPv1 systems, we will 55118316Swollman * later convert back to ordinary nets. 55218316Swollman * This unifies dealing with received supernets. 55318316Swollman */ 55446303Smarkm if ((ws.state & WS_ST_AG) 55546303Smarkm && ((RT->rt_state & RS_SUBNET) 55646303Smarkm || (ws.state & WS_ST_SUPER_AG))) 55746303Smarkm ags |= AGS_AGGREGATE; 55818316Swollman } 55918316Swollman 56018316Swollman /* Do not send RIPv1 advertisements of subnets to other 56118316Swollman * networks. If possible, multicast them by RIPv2. 56218316Swollman */ 56318316Swollman if ((RT->rt_state & RS_SUBNET) 56418316Swollman && !(ws.state & WS_ST_RIP2_ALL) 56546303Smarkm && !on_net(dst, ws.to_std_net, ws.to_std_mask)) 56646303Smarkm ags |= AGS_RIPV2 | AGS_AGGREGATE; 56718316Swollman 56846303Smarkm 56918316Swollman /* Do not send a route back to where it came from, except in 57018316Swollman * response to a query. This is "split-horizon". That means not 57118316Swollman * advertising back to the same network and so via the same interface. 57218316Swollman * 57318316Swollman * We want to suppress routes that might have been fragmented 57418316Swollman * from this route by a RIPv1 router and sent back to us, and so we 57518316Swollman * cannot forget this route here. Let the split-horizon route 57646303Smarkm * suppress the fragmented routes and then itself be forgotten. 57718316Swollman * 57818316Swollman * Include the routes for both ends of point-to-point interfaces 57920339Swollman * among those suppressed by split-horizon, since the other side 58020339Swollman * should knows them as well as we do. 58146303Smarkm * 58246303Smarkm * Notice spare routes with the same metric that we are about to 58346303Smarkm * advertise, to split the horizon on redundant, inactive paths. 584126250Sbms * 585126250Sbms * Do not suppress advertisements of interface-related addresses on 586126250Sbms * non-point-to-point interfaces. This ensures that we have something 587126250Sbms * to say every 30 seconds to help detect broken Ethernets or 588126250Sbms * other interfaces where one packet every 30 seconds costs nothing. 58918316Swollman */ 59046303Smarkm if (ws.ifp != 0 59118316Swollman && !(ws.state & WS_ST_QUERY) 59218316Swollman && (ws.state & WS_ST_TO_ON_NET) 59318316Swollman && (!(RT->rt_state & RS_IF) 59418316Swollman || ws.ifp->int_if_flags & IFF_POINTOPOINT)) { 59546303Smarkm for (rts = RT->rt_spares, i = NUM_SPARES; i != 0; i--, rts++) { 59646303Smarkm if (rts->rts_metric > metric 59746303Smarkm || rts->rts_ifp != ws.ifp) 59846303Smarkm continue; 59946303Smarkm 60046303Smarkm /* If we do not mark the route with AGS_SPLIT_HZ here, 60146303Smarkm * it will be poisoned-reverse, or advertised back 60246303Smarkm * toward its source with an infinite metric. 60346303Smarkm * If we have recently advertised the route with a 60446303Smarkm * better metric than we now have, then we should 60546303Smarkm * poison-reverse the route before suppressing it for 60646303Smarkm * split-horizon. 60746303Smarkm * 60846303Smarkm * In almost all cases, if there is no spare for the 60946303Smarkm * route then it is either old and dead or a brand 61046303Smarkm * new route. If it is brand new, there is no need 61146303Smarkm * for poison-reverse. If it is old and dead, it 61246303Smarkm * is already poisoned. 61346303Smarkm */ 61446303Smarkm if (RT->rt_poison_time < now_expire 61546303Smarkm || RT->rt_poison_metric >= metric 61646303Smarkm || RT->rt_spares[1].rts_gate == 0) { 61746303Smarkm ags |= AGS_SPLIT_HZ; 61846303Smarkm ags &= ~AGS_SUPPRESS; 61946303Smarkm } 62046303Smarkm metric = HOPCNT_INFINITY; 62146303Smarkm break; 62218316Swollman } 62318316Swollman } 62418316Swollman 62546303Smarkm /* Keep track of the best metric with which the 62646303Smarkm * route has been advertised recently. 62746303Smarkm */ 62846303Smarkm if (RT->rt_poison_metric >= metric 62946303Smarkm || RT->rt_poison_time < now_expire) { 63046303Smarkm RT->rt_poison_time = now.tv_sec; 63146303Smarkm RT->rt_poison_metric = metric; 63246303Smarkm } 63346303Smarkm 63418316Swollman /* Adjust the outgoing metric by the cost of the link. 63546303Smarkm * Avoid aggregation when a route is counting to infinity. 63618316Swollman */ 63746303Smarkm pref = RT->rt_poison_metric + ws.metric; 63846303Smarkm metric += ws.metric; 63918316Swollman 64046303Smarkm /* Do not advertise stable routes that will be ignored, 64146303Smarkm * unless we are answering a query. 64246303Smarkm * If the route recently was advertised with a metric that 64346303Smarkm * would have been less than infinity through this interface, 64446303Smarkm * we need to continue to advertise it in order to poison it. 64546303Smarkm */ 64646303Smarkm if (metric >= HOPCNT_INFINITY) { 64719880Swollman if (!(ws.state & WS_ST_QUERY) 64819880Swollman && (pref >= HOPCNT_INFINITY 64919880Swollman || RT->rt_poison_time < now_garbage)) 65018316Swollman return 0; 65118316Swollman 65218316Swollman metric = HOPCNT_INFINITY; 65318316Swollman } 65418316Swollman 65518316Swollman ag_check(dst, RT->rt_mask, 0, nhop, metric, pref, 65618316Swollman RT->rt_seqno, RT->rt_tag, ags, supply_out); 65718316Swollman return 0; 65818316Swollman#undef RT 65918316Swollman} 66018316Swollman 66118316Swollman 66218316Swollman/* Supply dst with the contents of the routing tables. 66318316Swollman * If this won't fit in one packet, chop it up into several. 66418316Swollman */ 66518316Swollmanvoid 66618316Swollmansupply(struct sockaddr_in *dst, 66718316Swollman struct interface *ifp, /* output interface */ 66818316Swollman enum output_type type, 66918316Swollman int flash, /* 1=flash update */ 67019880Swollman int vers, /* RIP version */ 67119880Swollman int passwd_ok) /* OK to include cleartext password */ 67218316Swollman{ 67318316Swollman struct rt_entry *rt; 67419880Swollman int def_metric; 67518316Swollman 67618316Swollman ws.state = 0; 67718316Swollman ws.gen_limit = 1024; 67818316Swollman 67918316Swollman ws.to = *dst; 68018316Swollman ws.to_std_mask = std_mask(ws.to.sin_addr.s_addr); 68118316Swollman ws.to_std_net = ntohl(ws.to.sin_addr.s_addr) & ws.to_std_mask; 68218316Swollman 68318316Swollman if (ifp != 0) { 68418316Swollman ws.to_mask = ifp->int_mask; 68518316Swollman ws.to_net = ifp->int_net; 68618316Swollman if (on_net(ws.to.sin_addr.s_addr, ws.to_net, ws.to_mask)) 68718316Swollman ws.state |= WS_ST_TO_ON_NET; 68818316Swollman 68918316Swollman } else { 69018316Swollman ws.to_mask = ripv1_mask_net(ws.to.sin_addr.s_addr, 0); 69118316Swollman ws.to_net = ntohl(ws.to.sin_addr.s_addr) & ws.to_mask; 69218316Swollman rt = rtfind(dst->sin_addr.s_addr); 69318316Swollman if (rt) 69418316Swollman ifp = rt->rt_ifp; 69518316Swollman } 69618316Swollman 69718316Swollman ws.npackets = 0; 69818316Swollman if (flash) 69918316Swollman ws.state |= WS_ST_FLASH; 70018316Swollman 70118316Swollman if ((ws.ifp = ifp) == 0) { 70218316Swollman ws.metric = 1; 70318316Swollman } else { 70418316Swollman /* Adjust the advertised metric by the outgoing interface 70518316Swollman * metric. 70618316Swollman */ 707126250Sbms ws.metric = ifp->int_metric + 1 + ifp->int_adj_outmetric; 70818316Swollman } 70918316Swollman 71018316Swollman ripv12_buf.rip.rip_vers = vers; 71118316Swollman 71218316Swollman switch (type) { 71318316Swollman case OUT_MULTICAST: 71446303Smarkm if (ifp->int_if_flags & IFF_MULTICAST) 71546303Smarkm v2buf.type = OUT_MULTICAST; 71646303Smarkm else 71746303Smarkm v2buf.type = NO_OUT_MULTICAST; 71819880Swollman v12buf.type = OUT_BROADCAST; 71918316Swollman break; 72046303Smarkm 72146303Smarkm case OUT_QUERY: 72246303Smarkm ws.state |= WS_ST_QUERY; 723102411Scharnier /* FALLTHROUGH */ 72446303Smarkm case OUT_BROADCAST: 72518316Swollman case OUT_UNICAST: 72619880Swollman v2buf.type = (vers == RIPv2) ? type : NO_OUT_RIPV2; 72719880Swollman v12buf.type = type; 72818316Swollman break; 72946303Smarkm 73046303Smarkm case NO_OUT_MULTICAST: 73146303Smarkm case NO_OUT_RIPV2: 73246303Smarkm break; /* no output */ 73318316Swollman } 73418316Swollman 73518316Swollman if (vers == RIPv2) { 73618316Swollman /* full RIPv2 only if cannot be heard by RIPv1 listeners */ 73718316Swollman if (type != OUT_BROADCAST) 73818316Swollman ws.state |= WS_ST_RIP2_ALL; 73946303Smarkm if ((ws.state & WS_ST_QUERY) 74046303Smarkm || !(ws.state & WS_ST_TO_ON_NET)) { 74118316Swollman ws.state |= (WS_ST_AG | WS_ST_SUPER_AG); 74219880Swollman } else if (ifp == 0 || !(ifp->int_state & IS_NO_AG)) { 74318316Swollman ws.state |= WS_ST_AG; 74418316Swollman if (type != OUT_BROADCAST 74546303Smarkm && (ifp == 0 74646303Smarkm || !(ifp->int_state & IS_NO_SUPER_AG))) 74718316Swollman ws.state |= WS_ST_SUPER_AG; 74818316Swollman } 74918316Swollman } 75018316Swollman 75119880Swollman ws.a = (vers == RIPv2) ? find_auth(ifp) : 0; 75220339Swollman if (!passwd_ok && ws.a != 0 && ws.a->type == RIP_AUTH_PW) 75319880Swollman ws.a = 0; 75420339Swollman clr_ws_buf(&v12buf,ws.a); 75520339Swollman clr_ws_buf(&v2buf,ws.a); 75619880Swollman 75719880Swollman /* Fake a default route if asked and if there is not already 75819880Swollman * a better, real default route. 75919880Swollman */ 76019880Swollman if (supplier && (def_metric = ifp->int_d_metric) != 0) { 76119880Swollman if (0 == (rt = rtget(RIP_DEFAULT, 0)) 76219880Swollman || rt->rt_metric+ws.metric >= def_metric) { 76318316Swollman ws.state |= WS_ST_DEFAULT; 76419880Swollman ag_check(0, 0, 0, 0, def_metric, def_metric, 76518316Swollman 0, 0, 0, supply_out); 76619880Swollman } else { 76719880Swollman def_metric = rt->rt_metric+ws.metric; 76818316Swollman } 76919880Swollman 77019880Swollman /* If both RIPv2 and the poor-man's router discovery 77119880Swollman * kludge are on, arrange to advertise an extra 77219880Swollman * default route via RIPv1. 77319880Swollman */ 77418316Swollman if ((ws.state & WS_ST_RIP2_ALL) 77518316Swollman && (ifp->int_state & IS_PM_RDISC)) { 77618316Swollman ripv12_buf.rip.rip_vers = RIPv1; 77719880Swollman v12buf.n->n_family = RIP_AF_INET; 77819880Swollman v12buf.n->n_dst = htonl(RIP_DEFAULT); 77919880Swollman v12buf.n->n_metric = htonl(def_metric); 78019880Swollman v12buf.n++; 78118316Swollman } 78218316Swollman } 78318316Swollman 78418316Swollman (void)rn_walktree(rhead, walk_supply, 0); 78518316Swollman ag_flush(0,0,supply_out); 78618316Swollman 78718316Swollman /* Flush the packet buffers, provided they are not empty and 78818316Swollman * do not contain only the password. 78918316Swollman */ 79019880Swollman if (v12buf.n != v12buf.base 79119880Swollman && (v12buf.n > v12buf.base+1 79219880Swollman || v12buf.base->n_family != RIP_AF_AUTH)) 79319880Swollman supply_write(&v12buf); 79419880Swollman if (v2buf.n != v2buf.base 79519880Swollman && (v2buf.n > v2buf.base+1 79619880Swollman || v2buf.base->n_family != RIP_AF_AUTH)) 79719880Swollman supply_write(&v2buf); 79818316Swollman 79918316Swollman /* If we sent nothing and this is an answer to a query, send 80018316Swollman * an empty buffer. 80118316Swollman */ 80218316Swollman if (ws.npackets == 0 80318316Swollman && (ws.state & WS_ST_QUERY)) 80419880Swollman supply_write(&v12buf); 80518316Swollman} 80618316Swollman 80718316Swollman 80818316Swollman/* send all of the routing table or just do a flash update 80918316Swollman */ 81018316Swollmanvoid 81118316Swollmanrip_bcast(int flash) 81218316Swollman{ 81318316Swollman#ifdef _HAVE_SIN_LEN 81464131Ssheldonh static struct sockaddr_in dst = {sizeof(dst), AF_INET, 0, {0}, {0}}; 81518316Swollman#else 81618316Swollman static struct sockaddr_in dst = {AF_INET}; 81718316Swollman#endif 81818316Swollman struct interface *ifp; 81918316Swollman enum output_type type; 82018316Swollman int vers; 82118316Swollman struct timeval rtime; 82218316Swollman 82318316Swollman 82418316Swollman need_flash = 0; 82518316Swollman intvl_random(&rtime, MIN_WAITTIME, MAX_WAITTIME); 82618316Swollman no_flash = rtime; 82718316Swollman timevaladd(&no_flash, &now); 82818316Swollman 82918316Swollman if (rip_sock < 0) 83018316Swollman return; 83118316Swollman 83219880Swollman trace_act("send %s and inhibit dynamic updates for %.3f sec", 83318316Swollman flash ? "dynamic update" : "all routes", 83418316Swollman rtime.tv_sec + ((float)rtime.tv_usec)/1000000.0); 83518316Swollman 836190711Sphk LIST_FOREACH(ifp, &ifnet, int_list) { 83719880Swollman /* Skip interfaces not doing RIP. 83819880Swollman * Do try broken interfaces to see if they have healed. 83918316Swollman */ 84019880Swollman if (IS_RIP_OUT_OFF(ifp->int_state)) 84118316Swollman continue; 84218316Swollman 84318316Swollman /* skip turned off interfaces */ 84446303Smarkm if (!iff_up(ifp->int_if_flags)) 84518316Swollman continue; 84618316Swollman 84719880Swollman vers = (ifp->int_state & IS_NO_RIPV1_OUT) ? RIPv2 : RIPv1; 84818316Swollman 84918316Swollman if (ifp->int_if_flags & IFF_BROADCAST) { 85018316Swollman /* ordinary, hardware interface */ 85118316Swollman dst.sin_addr.s_addr = ifp->int_brdaddr; 85219880Swollman 85318316Swollman if (vers == RIPv2 85446303Smarkm && !(ifp->int_state & IS_NO_RIP_MCAST)) { 85518316Swollman type = OUT_MULTICAST; 85618316Swollman } else { 85718316Swollman type = OUT_BROADCAST; 85818316Swollman } 85918316Swollman 86018316Swollman } else if (ifp->int_if_flags & IFF_POINTOPOINT) { 86118316Swollman /* point-to-point hardware interface */ 86218316Swollman dst.sin_addr.s_addr = ifp->int_dstaddr; 863190709Sphk if (vers == RIPv2 && 864190709Sphk ifp->int_if_flags & IFF_MULTICAST && 865190709Sphk !(ifp->int_state & IS_NO_RIP_MCAST)) { 866190709Sphk type = OUT_MULTICAST; 867190709Sphk } else { 868190709Sphk type = OUT_UNICAST; 869190709Sphk } 87018316Swollman 87119880Swollman } else if (ifp->int_state & IS_REMOTE) { 87218316Swollman /* remote interface */ 87318316Swollman dst.sin_addr.s_addr = ifp->int_addr; 87418316Swollman type = OUT_UNICAST; 87519880Swollman 87619880Swollman } else { 87719880Swollman /* ATM, HIPPI, etc. */ 87819880Swollman continue; 87918316Swollman } 88018316Swollman 88119880Swollman supply(&dst, ifp, type, flash, vers, 1); 88218316Swollman } 88318316Swollman 88418316Swollman update_seqno++; /* all routes are up to date */ 88518316Swollman} 88618316Swollman 88718316Swollman 88818316Swollman/* Ask for routes 88918316Swollman * Do it only once to an interface, and not even after the interface 89018316Swollman * was broken and recovered. 89118316Swollman */ 89218316Swollmanvoid 89318316Swollmanrip_query(void) 89418316Swollman{ 89518316Swollman#ifdef _HAVE_SIN_LEN 89664131Ssheldonh static struct sockaddr_in dst = {sizeof(dst), AF_INET, 0, {0}, {0}}; 89718316Swollman#else 89818316Swollman static struct sockaddr_in dst = {AF_INET}; 89918316Swollman#endif 90018316Swollman struct interface *ifp; 90118316Swollman struct rip buf; 90218316Swollman enum output_type type; 90318316Swollman 90418316Swollman 90518316Swollman if (rip_sock < 0) 90618316Swollman return; 90718316Swollman 90846303Smarkm memset(&buf, 0, sizeof(buf)); 90918316Swollman 910190711Sphk LIST_FOREACH(ifp, &ifnet, int_list) { 91119880Swollman /* Skip interfaces those already queried. 91219880Swollman * Do not ask via interfaces through which we don't 91319880Swollman * accept input. Do not ask via interfaces that cannot 91419880Swollman * send RIP packets. 91519880Swollman * Do try broken interfaces to see if they have healed. 91618316Swollman */ 91719880Swollman if (IS_RIP_IN_OFF(ifp->int_state) 91819880Swollman || ifp->int_query_time != NEVER) 91918316Swollman continue; 92018316Swollman 92118316Swollman /* skip turned off interfaces */ 92246303Smarkm if (!iff_up(ifp->int_if_flags)) 92318316Swollman continue; 92418316Swollman 92519880Swollman buf.rip_vers = (ifp->int_state&IS_NO_RIPV1_OUT) ? RIPv2:RIPv1; 92618316Swollman buf.rip_cmd = RIPCMD_REQUEST; 92718316Swollman buf.rip_nets[0].n_family = RIP_AF_UNSPEC; 92818316Swollman buf.rip_nets[0].n_metric = htonl(HOPCNT_INFINITY); 92918316Swollman 93046303Smarkm /* Send a RIPv1 query only if allowed and if we will 93146303Smarkm * listen to RIPv1 routers. 93246303Smarkm */ 93346303Smarkm if ((ifp->int_state & IS_NO_RIPV1_OUT) 93446303Smarkm || (ifp->int_state & IS_NO_RIPV1_IN)) { 93546303Smarkm buf.rip_vers = RIPv2; 93646303Smarkm } else { 93746303Smarkm buf.rip_vers = RIPv1; 93846303Smarkm } 93946303Smarkm 94018316Swollman if (ifp->int_if_flags & IFF_BROADCAST) { 94118316Swollman /* ordinary, hardware interface */ 94218316Swollman dst.sin_addr.s_addr = ifp->int_brdaddr; 94346303Smarkm 94446303Smarkm /* Broadcast RIPv1 queries and RIPv2 queries 94546303Smarkm * when the hardware cannot multicast. 94618316Swollman */ 94718316Swollman if (buf.rip_vers == RIPv2 94846303Smarkm && (ifp->int_if_flags & IFF_MULTICAST) 94946303Smarkm && !(ifp->int_state & IS_NO_RIP_MCAST)) { 95018316Swollman type = OUT_MULTICAST; 95118316Swollman } else { 95218316Swollman type = OUT_BROADCAST; 95318316Swollman } 95418316Swollman 95518316Swollman } else if (ifp->int_if_flags & IFF_POINTOPOINT) { 95618316Swollman /* point-to-point hardware interface */ 95718316Swollman dst.sin_addr.s_addr = ifp->int_dstaddr; 95818316Swollman type = OUT_UNICAST; 95918316Swollman 96019880Swollman } else if (ifp->int_state & IS_REMOTE) { 96118316Swollman /* remote interface */ 96218316Swollman dst.sin_addr.s_addr = ifp->int_addr; 96318316Swollman type = OUT_UNICAST; 96419880Swollman 96519880Swollman } else { 96619880Swollman /* ATM, HIPPI, etc. */ 96719880Swollman continue; 96818316Swollman } 96918316Swollman 97019880Swollman ifp->int_query_time = now.tv_sec+SUPPLY_INTERVAL; 97118316Swollman if (output(type, &dst, ifp, &buf, sizeof(buf)) < 0) 97218316Swollman if_sick(ifp); 97318316Swollman } 97418316Swollman} 975