10Sstevel@tonic-gate /*
2*5381Smeem * 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/output.c,v 1.7 2000/08/11 08:24:38 sheldonh 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>
43*5381Smeem #include <alloca.h>
440Sstevel@tonic-gate
450Sstevel@tonic-gate uint_t update_seqno;
460Sstevel@tonic-gate
470Sstevel@tonic-gate
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate * walk the tree of routes with this for output
500Sstevel@tonic-gate */
510Sstevel@tonic-gate static struct {
520Sstevel@tonic-gate struct sockaddr_in to;
530Sstevel@tonic-gate in_addr_t to_mask;
540Sstevel@tonic-gate in_addr_t to_net;
550Sstevel@tonic-gate in_addr_t to_std_mask;
560Sstevel@tonic-gate in_addr_t to_std_net;
570Sstevel@tonic-gate struct interface *ifp; /* usually output interface */
580Sstevel@tonic-gate struct auth *a;
590Sstevel@tonic-gate uint8_t metric; /* adjust metrics by interface */
600Sstevel@tonic-gate uint32_t npackets;
610Sstevel@tonic-gate uint32_t gen_limit;
620Sstevel@tonic-gate #define WS_GEN_LIMIT_MAX 1024
630Sstevel@tonic-gate uint16_t state;
640Sstevel@tonic-gate #define WS_ST_FLASH 0x001 /* send only changed routes */
650Sstevel@tonic-gate #define WS_ST_RIP2_ALL 0x002 /* send full featured RIPv2 */
660Sstevel@tonic-gate #define WS_ST_AG 0x004 /* ok to aggregate subnets */
670Sstevel@tonic-gate #define WS_ST_SUPER_AG 0x008 /* ok to aggregate networks */
680Sstevel@tonic-gate #define WS_ST_QUERY 0x010 /* responding to a query */
690Sstevel@tonic-gate #define WS_ST_TO_ON_NET 0x020 /* sending onto one of our nets */
700Sstevel@tonic-gate #define WS_ST_DEFAULT 0x040 /* faking a default */
710Sstevel@tonic-gate } ws;
720Sstevel@tonic-gate
730Sstevel@tonic-gate /* A buffer for what can be heard by both RIPv1 and RIPv2 listeners */
740Sstevel@tonic-gate struct ws_buf v12buf;
750Sstevel@tonic-gate static union pkt_buf ripv12_buf;
760Sstevel@tonic-gate
770Sstevel@tonic-gate /* Another for only RIPv2 listeners */
780Sstevel@tonic-gate static struct ws_buf v2buf;
790Sstevel@tonic-gate static union pkt_buf rip_v2_buf;
800Sstevel@tonic-gate
810Sstevel@tonic-gate
820Sstevel@tonic-gate
830Sstevel@tonic-gate void
bufinit(void)840Sstevel@tonic-gate bufinit(void)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate ripv12_buf.rip.rip_cmd = RIPCMD_RESPONSE;
870Sstevel@tonic-gate v12buf.buf = &ripv12_buf.rip;
880Sstevel@tonic-gate v12buf.base = &v12buf.buf->rip_nets[0];
890Sstevel@tonic-gate
900Sstevel@tonic-gate rip_v2_buf.rip.rip_cmd = RIPCMD_RESPONSE;
910Sstevel@tonic-gate rip_v2_buf.rip.rip_vers = RIPv2;
920Sstevel@tonic-gate v2buf.buf = &rip_v2_buf.rip;
930Sstevel@tonic-gate v2buf.base = &v2buf.buf->rip_nets[0];
940Sstevel@tonic-gate }
950Sstevel@tonic-gate
960Sstevel@tonic-gate
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate * Send the contents of the global buffer via the non-multicast socket
990Sstevel@tonic-gate */
1000Sstevel@tonic-gate int /* <0 on failure */
output(enum output_type type,struct sockaddr_in * dst,struct interface * ifp,struct rip * buf,int size)1010Sstevel@tonic-gate output(enum output_type type,
1020Sstevel@tonic-gate struct sockaddr_in *dst, /* send to here */
1030Sstevel@tonic-gate struct interface *ifp,
1040Sstevel@tonic-gate struct rip *buf,
1050Sstevel@tonic-gate int size) /* this many bytes */
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate struct sockaddr_in sin;
1080Sstevel@tonic-gate int flags;
1090Sstevel@tonic-gate const char *msg;
1100Sstevel@tonic-gate int res;
1110Sstevel@tonic-gate int ifindex;
1120Sstevel@tonic-gate struct in_addr addr;
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate sin = *dst;
1150Sstevel@tonic-gate if (sin.sin_port == 0)
1160Sstevel@tonic-gate sin.sin_port = htons(RIP_PORT);
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate flags = 0;
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate if (ifp == NULL && type == OUT_MULTICAST) {
1210Sstevel@tonic-gate msglog("Cannot send RIP message to %s",
1220Sstevel@tonic-gate inet_ntoa(sin.sin_addr));
1230Sstevel@tonic-gate return (-1);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate switch (type) {
1270Sstevel@tonic-gate case OUT_QUERY:
1280Sstevel@tonic-gate msg = "Answer Query";
1290Sstevel@tonic-gate break;
1300Sstevel@tonic-gate case OUT_UNICAST:
1310Sstevel@tonic-gate msg = "Send";
1320Sstevel@tonic-gate flags = MSG_DONTROUTE;
1330Sstevel@tonic-gate break;
1340Sstevel@tonic-gate case OUT_BROADCAST:
1350Sstevel@tonic-gate msg = "Send bcast";
1360Sstevel@tonic-gate break;
1370Sstevel@tonic-gate case OUT_MULTICAST:
1380Sstevel@tonic-gate msg = "Send mcast";
1390Sstevel@tonic-gate break;
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate case NO_OUT_MULTICAST:
1420Sstevel@tonic-gate case NO_OUT_RIPV2:
1430Sstevel@tonic-gate default:
1440Sstevel@tonic-gate #ifdef DEBUG
1450Sstevel@tonic-gate abort();
1460Sstevel@tonic-gate #endif
1470Sstevel@tonic-gate return (-1);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate /*
151*5381Smeem * IP_PKTINFO overrides IP_MULTICAST_IF, so we don't set ifindex
152*5381Smeem * for multicast traffic.
1530Sstevel@tonic-gate */
1540Sstevel@tonic-gate ifindex = (type != OUT_MULTICAST && type != OUT_QUERY &&
1550Sstevel@tonic-gate ifp != NULL && ifp->int_phys != NULL) ?
1560Sstevel@tonic-gate ifp->int_phys->phyi_index : 0;
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate if (rip_sock_interface != ifp) {
1590Sstevel@tonic-gate /*
1600Sstevel@tonic-gate * For multicast, we have to choose the source
1610Sstevel@tonic-gate * address. This is either the local address
1620Sstevel@tonic-gate * (non-point-to-point) or the remote address.
1630Sstevel@tonic-gate */
1640Sstevel@tonic-gate if (ifp != NULL) {
1650Sstevel@tonic-gate addr.s_addr = (ifp->int_if_flags & IFF_POINTOPOINT) ?
1660Sstevel@tonic-gate ifp->int_dstaddr : ifp->int_addr;
1670Sstevel@tonic-gate if (type == OUT_MULTICAST &&
1680Sstevel@tonic-gate setsockopt(rip_sock, IPPROTO_IP,
1690Sstevel@tonic-gate IP_MULTICAST_IF, &addr, sizeof (addr)) == -1) {
1700Sstevel@tonic-gate LOGERR("setsockopt(rip_sock, IP_MULTICAST_IF)");
1710Sstevel@tonic-gate return (-1);
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate rip_sock_interface = ifp;
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate trace_rip(msg, "to", &sin, ifp, buf, size);
1780Sstevel@tonic-gate
179*5381Smeem res = sendtoif(rip_sock, buf, size, flags, &sin, ifindex);
180*5381Smeem if (res < 0 && (ifp == NULL || !(ifp->int_state & IS_BROKE))) {
1810Sstevel@tonic-gate writelog(LOG_WARNING, "%s sendto(%s%s%s.%d): %s", msg,
1820Sstevel@tonic-gate ifp != NULL ? ifp->int_name : "",
1830Sstevel@tonic-gate ifp != NULL ? ", " : "",
1840Sstevel@tonic-gate inet_ntoa(sin.sin_addr),
1850Sstevel@tonic-gate ntohs(sin.sin_port),
1860Sstevel@tonic-gate rip_strerror(errno));
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate return (res);
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate
192*5381Smeem /*
193*5381Smeem * Semantically identical to sendto(), but sends the message through a
194*5381Smeem * specific interface (if ifindex is non-zero) using IP_PKTINFO.
195*5381Smeem */
196*5381Smeem int
sendtoif(int fd,const void * buf,uint_t bufsize,uint_t flags,struct sockaddr_in * sinp,uint_t ifindex)197*5381Smeem sendtoif(int fd, const void *buf, uint_t bufsize, uint_t flags,
198*5381Smeem struct sockaddr_in *sinp, uint_t ifindex)
199*5381Smeem {
200*5381Smeem struct iovec iov;
201*5381Smeem struct msghdr msg;
202*5381Smeem struct cmsghdr *cmsgp;
203*5381Smeem struct in_pktinfo *ipip;
204*5381Smeem
205*5381Smeem iov.iov_base = (void *)buf;
206*5381Smeem iov.iov_len = bufsize;
207*5381Smeem
208*5381Smeem (void) memset(&msg, 0, sizeof (struct msghdr));
209*5381Smeem msg.msg_name = (struct sockaddr *)sinp;
210*5381Smeem msg.msg_namelen = sizeof (struct sockaddr_in);
211*5381Smeem msg.msg_iov = &iov;
212*5381Smeem msg.msg_iovlen = 1;
213*5381Smeem
214*5381Smeem if (ifindex != 0) {
215*5381Smeem /*
216*5381Smeem * We can't precisely predict the alignment padding we'll
217*5381Smeem * need, so we allocate the maximum alignment and then
218*5381Smeem * use CMSG_NXTHDR() to fix it up at the end.
219*5381Smeem */
220*5381Smeem msg.msg_controllen = sizeof (*cmsgp) + _MAX_ALIGNMENT +
221*5381Smeem sizeof (*ipip) + _MAX_ALIGNMENT + sizeof (*cmsgp);
222*5381Smeem msg.msg_control = alloca(msg.msg_controllen);
223*5381Smeem
224*5381Smeem cmsgp = CMSG_FIRSTHDR(&msg);
225*5381Smeem ipip = (void *)CMSG_DATA(cmsgp);
226*5381Smeem (void) memset(ipip, 0, sizeof (struct in_pktinfo));
227*5381Smeem ipip->ipi_ifindex = ifindex;
228*5381Smeem cmsgp->cmsg_len = (caddr_t)(ipip + 1) - (caddr_t)cmsgp;
229*5381Smeem cmsgp->cmsg_type = IP_PKTINFO;
230*5381Smeem cmsgp->cmsg_level = IPPROTO_IP;
231*5381Smeem
232*5381Smeem /*
233*5381Smeem * Correct the control message length.
234*5381Smeem */
235*5381Smeem cmsgp = CMSG_NXTHDR(&msg, cmsgp);
236*5381Smeem msg.msg_controllen = (caddr_t)cmsgp - (caddr_t)msg.msg_control;
237*5381Smeem }
238*5381Smeem
239*5381Smeem return (sendmsg(fd, &msg, flags));
240*5381Smeem }
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate /*
2430Sstevel@tonic-gate * Find the first key for a packet to send.
2440Sstevel@tonic-gate * Try for a key that is eligible and has not expired, but settle for
2450Sstevel@tonic-gate * the last key if they have all expired.
2460Sstevel@tonic-gate * If no key is ready yet, give up.
2470Sstevel@tonic-gate */
2480Sstevel@tonic-gate struct auth *
find_auth(struct interface * ifp)2490Sstevel@tonic-gate find_auth(struct interface *ifp)
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate struct auth *ap, *res = NULL;
2520Sstevel@tonic-gate int i;
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate if (ifp == NULL)
2560Sstevel@tonic-gate return (NULL);
2570Sstevel@tonic-gate
2580Sstevel@tonic-gate if ((ap = ifp->int_auth) == NULL)
2590Sstevel@tonic-gate return (NULL);
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate for (i = 0; i < MAX_AUTH_KEYS; i++, ap++) {
2620Sstevel@tonic-gate /* stop looking after the last key */
2630Sstevel@tonic-gate if (ap->type == RIP_AUTH_NONE)
2640Sstevel@tonic-gate break;
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate /* ignore keys that are not ready yet */
2670Sstevel@tonic-gate if ((ulong_t)ap->start > (ulong_t)clk.tv_sec)
2680Sstevel@tonic-gate continue;
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate if ((ulong_t)ap->end < (ulong_t)clk.tv_sec) {
2710Sstevel@tonic-gate /* note best expired password as a fall-back */
2720Sstevel@tonic-gate if (res == NULL ||
2730Sstevel@tonic-gate (((ulong_t)ap->end > (ulong_t)res->end)) &&
2740Sstevel@tonic-gate ((ulong_t)res->end < (ulong_t)clk.tv_sec))
2750Sstevel@tonic-gate res = ap;
2760Sstevel@tonic-gate continue;
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate /* note key with the best future */
2800Sstevel@tonic-gate if (res == NULL || (ulong_t)res->end < (ulong_t)ap->end)
2810Sstevel@tonic-gate res = ap;
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate return (res);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate void
clr_ws_buf(struct ws_buf * wb,struct auth * ap)2880Sstevel@tonic-gate clr_ws_buf(struct ws_buf *wb, struct auth *ap)
2890Sstevel@tonic-gate {
2900Sstevel@tonic-gate struct netauth *na;
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate wb->lim = wb->base + NETS_LEN;
2930Sstevel@tonic-gate wb->n = wb->base;
2940Sstevel@tonic-gate (void) memset(wb->n, 0, NETS_LEN*sizeof (*wb->n));
2950Sstevel@tonic-gate
2960Sstevel@tonic-gate /*
2970Sstevel@tonic-gate * (start to) install authentication if appropriate
2980Sstevel@tonic-gate */
2990Sstevel@tonic-gate if (ap == NULL)
3000Sstevel@tonic-gate return;
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate na = (struct netauth *)wb->n;
3030Sstevel@tonic-gate if (ap->type == RIP_AUTH_PW) {
3040Sstevel@tonic-gate na->a_family = RIP_AF_AUTH;
3050Sstevel@tonic-gate na->a_type = RIP_AUTH_PW;
3060Sstevel@tonic-gate (void) memcpy(na->au.au_pw, ap->key, sizeof (na->au.au_pw));
3070Sstevel@tonic-gate wb->n++;
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate } else if (ap->type == RIP_AUTH_MD5) {
3100Sstevel@tonic-gate na->a_family = RIP_AF_AUTH;
3110Sstevel@tonic-gate na->a_type = RIP_AUTH_MD5;
3120Sstevel@tonic-gate na->au.a_md5.md5_keyid = ap->keyid;
3130Sstevel@tonic-gate na->au.a_md5.md5_auth_len = RIP_AUTH_MD5_LEN;
3140Sstevel@tonic-gate na->au.a_md5.md5_seqno = htonl(clk.tv_sec);
3150Sstevel@tonic-gate wb->n++;
3160Sstevel@tonic-gate wb->lim--; /* make room for trailer */
3170Sstevel@tonic-gate }
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate void
end_md5_auth(struct ws_buf * wb,struct auth * ap)3220Sstevel@tonic-gate end_md5_auth(struct ws_buf *wb, struct auth *ap)
3230Sstevel@tonic-gate {
3240Sstevel@tonic-gate struct netauth *na, *na2;
3250Sstevel@tonic-gate MD5_CTX md5_ctx;
3260Sstevel@tonic-gate int len;
3270Sstevel@tonic-gate
3280Sstevel@tonic-gate na = (struct netauth *)wb->base;
3290Sstevel@tonic-gate na2 = (struct netauth *)wb->n;
3300Sstevel@tonic-gate len = (char *)na2-(char *)wb->buf;
3310Sstevel@tonic-gate na2->a_family = RIP_AF_AUTH;
3320Sstevel@tonic-gate na2->a_type = RIP_AUTH_TRAILER;
3330Sstevel@tonic-gate na->au.a_md5.md5_pkt_len = htons(len);
3340Sstevel@tonic-gate MD5Init(&md5_ctx);
3350Sstevel@tonic-gate /* len+4 to include auth trailer's family/type in MD5 sum */
3360Sstevel@tonic-gate MD5Update(&md5_ctx, (uchar_t *)wb->buf, len + 4);
3370Sstevel@tonic-gate MD5Update(&md5_ctx, ap->key, RIP_AUTH_MD5_LEN);
3380Sstevel@tonic-gate MD5Final(na2->au.au_pw, &md5_ctx);
3390Sstevel@tonic-gate wb->n++;
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate /*
3440Sstevel@tonic-gate * Send the buffer
3450Sstevel@tonic-gate */
3460Sstevel@tonic-gate static void
supply_write(struct ws_buf * wb)3470Sstevel@tonic-gate supply_write(struct ws_buf *wb)
3480Sstevel@tonic-gate {
3490Sstevel@tonic-gate /*
3500Sstevel@tonic-gate * Output multicast only if legal.
3510Sstevel@tonic-gate * If we would multicast and it would be illegal, then discard the
3520Sstevel@tonic-gate * packet.
3530Sstevel@tonic-gate */
3540Sstevel@tonic-gate switch (wb->type) {
3550Sstevel@tonic-gate case NO_OUT_MULTICAST:
3560Sstevel@tonic-gate trace_pkt("skip multicast to %s because impossible",
3570Sstevel@tonic-gate naddr_ntoa(ws.to.sin_addr.s_addr));
3580Sstevel@tonic-gate break;
3590Sstevel@tonic-gate case NO_OUT_RIPV2:
3600Sstevel@tonic-gate break;
3610Sstevel@tonic-gate default:
3620Sstevel@tonic-gate if (ws.a != NULL && ws.a->type == RIP_AUTH_MD5)
3630Sstevel@tonic-gate end_md5_auth(wb, ws.a);
3640Sstevel@tonic-gate if (output(wb->type, &ws.to, ws.ifp, wb->buf,
3650Sstevel@tonic-gate ((char *)wb->n - (char *)wb->buf)) < 0 && ws.ifp != NULL)
3660Sstevel@tonic-gate if_sick(ws.ifp, _B_FALSE);
3670Sstevel@tonic-gate ws.npackets++;
3680Sstevel@tonic-gate break;
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate
3710Sstevel@tonic-gate clr_ws_buf(wb, ws.a);
3720Sstevel@tonic-gate }
3730Sstevel@tonic-gate
3740Sstevel@tonic-gate
3750Sstevel@tonic-gate /*
3760Sstevel@tonic-gate * Put an entry into the packet
3770Sstevel@tonic-gate */
3780Sstevel@tonic-gate static void
supply_out(struct ag_info * ag)3790Sstevel@tonic-gate supply_out(struct ag_info *ag)
3800Sstevel@tonic-gate {
3810Sstevel@tonic-gate uint32_t dstcount;
3820Sstevel@tonic-gate in_addr_t mask, v1_mask, dst_h, ddst_h = 0;
3830Sstevel@tonic-gate struct ws_buf *wb;
3840Sstevel@tonic-gate
3850Sstevel@tonic-gate
3860Sstevel@tonic-gate /*
3870Sstevel@tonic-gate * Skip this route if doing a flash update and it and the routes
3880Sstevel@tonic-gate * it aggregates have not changed recently.
3890Sstevel@tonic-gate */
3900Sstevel@tonic-gate if (ag->ag_seqno < update_seqno && (ws.state & WS_ST_FLASH))
3910Sstevel@tonic-gate return;
3920Sstevel@tonic-gate
3930Sstevel@tonic-gate dst_h = ag->ag_dst_h;
3940Sstevel@tonic-gate mask = ag->ag_mask;
3950Sstevel@tonic-gate v1_mask = ripv1_mask_host(htonl(dst_h),
3960Sstevel@tonic-gate (ws.state & WS_ST_TO_ON_NET) ? ws.ifp : NULL);
3970Sstevel@tonic-gate dstcount = 0;
3980Sstevel@tonic-gate
3990Sstevel@tonic-gate /*
4000Sstevel@tonic-gate * If we are sending RIPv2 packets that cannot (or must not) be
4010Sstevel@tonic-gate * heard by RIPv1 listeners, do not worry about sub- or supernets.
4020Sstevel@tonic-gate * Subnets (from other networks) can only be sent via multicast.
4030Sstevel@tonic-gate * A pair of subnet routes might have been promoted so that they
4040Sstevel@tonic-gate * are legal to send by RIPv1.
4050Sstevel@tonic-gate * If RIPv1 is off, use the multicast buffer.
4060Sstevel@tonic-gate */
4070Sstevel@tonic-gate if ((ws.state & WS_ST_RIP2_ALL) ||
4080Sstevel@tonic-gate ((ag->ag_state & AGS_RIPV2) && v1_mask != mask)) {
4090Sstevel@tonic-gate /* use the RIPv2-only buffer */
4100Sstevel@tonic-gate wb = &v2buf;
4110Sstevel@tonic-gate
4120Sstevel@tonic-gate } else {
4130Sstevel@tonic-gate /*
4140Sstevel@tonic-gate * use the RIPv1-or-RIPv2 buffer
4150Sstevel@tonic-gate */
4160Sstevel@tonic-gate wb = &v12buf;
4170Sstevel@tonic-gate
4180Sstevel@tonic-gate /*
4190Sstevel@tonic-gate * Convert supernet route into corresponding set of network
4200Sstevel@tonic-gate * routes for RIPv1, but leave non-contiguous netmasks
4210Sstevel@tonic-gate * to ag_check().
4220Sstevel@tonic-gate */
4230Sstevel@tonic-gate if (v1_mask > mask &&
4240Sstevel@tonic-gate mask + (mask & -mask) == 0) {
4250Sstevel@tonic-gate ddst_h = v1_mask & -v1_mask;
4260Sstevel@tonic-gate dstcount = (v1_mask & ~mask)/ddst_h;
4270Sstevel@tonic-gate
4280Sstevel@tonic-gate if (dstcount > ws.gen_limit) {
4290Sstevel@tonic-gate /*
4300Sstevel@tonic-gate * Punt if we would have to generate an
4310Sstevel@tonic-gate * unreasonable number of routes.
4320Sstevel@tonic-gate */
4330Sstevel@tonic-gate if (TRACECONTENTS)
4340Sstevel@tonic-gate trace_misc("sending %s-->%s as 1"
4350Sstevel@tonic-gate " instead of %d routes",
4360Sstevel@tonic-gate addrname(htonl(dst_h), mask, 1),
4370Sstevel@tonic-gate naddr_ntoa(ws.to.sin_addr.s_addr),
4380Sstevel@tonic-gate dstcount + 1);
4390Sstevel@tonic-gate dstcount = 0;
4400Sstevel@tonic-gate
4410Sstevel@tonic-gate } else {
4420Sstevel@tonic-gate mask = v1_mask;
4430Sstevel@tonic-gate ws.gen_limit -= dstcount;
4440Sstevel@tonic-gate }
4450Sstevel@tonic-gate }
4460Sstevel@tonic-gate }
4470Sstevel@tonic-gate
4480Sstevel@tonic-gate do {
4490Sstevel@tonic-gate wb->n->n_family = RIP_AF_INET;
4500Sstevel@tonic-gate wb->n->n_dst = htonl(dst_h);
4510Sstevel@tonic-gate /*
4520Sstevel@tonic-gate * If the route is from router-discovery or we are
4530Sstevel@tonic-gate * shutting down, or this is a broken/sick interface,
4540Sstevel@tonic-gate * admit only a bad metric.
4550Sstevel@tonic-gate */
4560Sstevel@tonic-gate wb->n->n_metric = ((stopint || ag->ag_metric < 1 ||
4570Sstevel@tonic-gate (ag->ag_ifp && (ag->ag_ifp->int_state &
4580Sstevel@tonic-gate (IS_BROKE|IS_SICK)))) ? HOPCNT_INFINITY : ag->ag_metric);
4590Sstevel@tonic-gate wb->n->n_metric = htonl(wb->n->n_metric);
4600Sstevel@tonic-gate /*
4610Sstevel@tonic-gate * Any non-zero bits in the supposedly unused RIPv1 fields
4620Sstevel@tonic-gate * cause the old `routed` to ignore the route.
4630Sstevel@tonic-gate * That means the mask and so forth cannot be sent
4640Sstevel@tonic-gate * in the hybrid RIPv1/RIPv2 mode.
4650Sstevel@tonic-gate */
4660Sstevel@tonic-gate if (ws.state & WS_ST_RIP2_ALL) {
4670Sstevel@tonic-gate if (ag->ag_nhop != 0 &&
4680Sstevel@tonic-gate ((ws.state & WS_ST_QUERY) ||
4690Sstevel@tonic-gate (ag->ag_nhop != ws.ifp->int_addr &&
4700Sstevel@tonic-gate on_net(ag->ag_nhop, ws.ifp->int_net,
4710Sstevel@tonic-gate ws.ifp->int_mask)) &&
4720Sstevel@tonic-gate ifwithaddr(ag->ag_nhop, _B_FALSE, _B_FALSE) ==
4730Sstevel@tonic-gate NULL))
4740Sstevel@tonic-gate wb->n->n_nhop = ag->ag_nhop;
4750Sstevel@tonic-gate wb->n->n_mask = htonl(mask);
4760Sstevel@tonic-gate wb->n->n_tag = ag->ag_tag;
4770Sstevel@tonic-gate }
4780Sstevel@tonic-gate dst_h += ddst_h;
4790Sstevel@tonic-gate
4800Sstevel@tonic-gate if (++wb->n >= wb->lim)
4810Sstevel@tonic-gate supply_write(wb);
4820Sstevel@tonic-gate } while (dstcount-- > 0);
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate
4850Sstevel@tonic-gate
4860Sstevel@tonic-gate /*
4870Sstevel@tonic-gate * Supply one route from the table
4880Sstevel@tonic-gate */
4890Sstevel@tonic-gate /* ARGSUSED */
4900Sstevel@tonic-gate static int
walk_supply(struct radix_node * rn,void * argp)4910Sstevel@tonic-gate walk_supply(struct radix_node *rn, void *argp)
4920Sstevel@tonic-gate {
4930Sstevel@tonic-gate #define RT ((struct rt_entry *)rn)
4940Sstevel@tonic-gate ushort_t ags;
4950Sstevel@tonic-gate uint8_t metric, pref;
4960Sstevel@tonic-gate in_addr_t dst, nhop;
4970Sstevel@tonic-gate struct rt_spare *rts;
4980Sstevel@tonic-gate uint_t sparecount;
4990Sstevel@tonic-gate
5000Sstevel@tonic-gate
5010Sstevel@tonic-gate /*
5020Sstevel@tonic-gate * Do not advertise external remote interfaces or passive interfaces.
5030Sstevel@tonic-gate */
5040Sstevel@tonic-gate if ((RT->rt_state & RS_IF) && RT->rt_ifp != NULL &&
5050Sstevel@tonic-gate (RT->rt_ifp->int_state & IS_PASSIVE) &&
5060Sstevel@tonic-gate !(RT->rt_state & RS_MHOME))
5070Sstevel@tonic-gate return (0);
5080Sstevel@tonic-gate /*
5090Sstevel@tonic-gate * Do not advertise routes learnt from /etc/gateways.
5100Sstevel@tonic-gate */
5110Sstevel@tonic-gate if (RT->rt_spares[0].rts_origin == RO_FILE)
5120Sstevel@tonic-gate return (0);
5130Sstevel@tonic-gate
5140Sstevel@tonic-gate /*
5150Sstevel@tonic-gate * Do not advertise routes which would lead to forwarding on a
5160Sstevel@tonic-gate * non-forwarding interface.
5170Sstevel@tonic-gate */
5180Sstevel@tonic-gate if (RT->rt_state & RS_NOPROPAGATE)
5190Sstevel@tonic-gate return (0);
5200Sstevel@tonic-gate
5210Sstevel@tonic-gate /*
5220Sstevel@tonic-gate * If being quiet about our ability to forward, then
5230Sstevel@tonic-gate * do not say anything unless responding to a query,
5240Sstevel@tonic-gate * except about our main interface.
5250Sstevel@tonic-gate */
5260Sstevel@tonic-gate if (!should_supply(NULL) && !(ws.state & WS_ST_QUERY) &&
5270Sstevel@tonic-gate !(RT->rt_state & RS_MHOME))
5280Sstevel@tonic-gate return (0);
5290Sstevel@tonic-gate
5300Sstevel@tonic-gate dst = RT->rt_dst;
5310Sstevel@tonic-gate
5320Sstevel@tonic-gate /*
5330Sstevel@tonic-gate * do not collide with the fake default route
5340Sstevel@tonic-gate */
5350Sstevel@tonic-gate if (dst == RIP_DEFAULT && (ws.state & WS_ST_DEFAULT))
5360Sstevel@tonic-gate return (0);
5370Sstevel@tonic-gate
5380Sstevel@tonic-gate if (RT->rt_state & RS_NET_SYN) {
5390Sstevel@tonic-gate if (RT->rt_state & RS_NET_INT) {
5400Sstevel@tonic-gate /*
5410Sstevel@tonic-gate * Do not send manual synthetic network routes
5420Sstevel@tonic-gate * into the subnet.
5430Sstevel@tonic-gate */
5440Sstevel@tonic-gate if (on_net(ws.to.sin_addr.s_addr,
5450Sstevel@tonic-gate ntohl(dst), RT->rt_mask))
5460Sstevel@tonic-gate return (0);
5470Sstevel@tonic-gate
5480Sstevel@tonic-gate } else {
5490Sstevel@tonic-gate /*
5500Sstevel@tonic-gate * Do not send automatic synthetic network routes
5510Sstevel@tonic-gate * if they are not needed because no RIPv1 listeners
5520Sstevel@tonic-gate * can hear them.
5530Sstevel@tonic-gate */
5540Sstevel@tonic-gate if (ws.state & WS_ST_RIP2_ALL)
5550Sstevel@tonic-gate return (0);
5560Sstevel@tonic-gate
5570Sstevel@tonic-gate /*
5580Sstevel@tonic-gate * Do not send automatic synthetic network routes to
5590Sstevel@tonic-gate * the real subnet.
5600Sstevel@tonic-gate */
5610Sstevel@tonic-gate if (on_net(ws.to.sin_addr.s_addr,
5620Sstevel@tonic-gate ntohl(dst), RT->rt_mask))
5630Sstevel@tonic-gate return (0);
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate nhop = 0;
5660Sstevel@tonic-gate
5670Sstevel@tonic-gate } else {
5680Sstevel@tonic-gate /*
5690Sstevel@tonic-gate * Advertise the next hop if this is not a route for one
5700Sstevel@tonic-gate * of our interfaces and the next hop is on the same
5710Sstevel@tonic-gate * network as the target.
5720Sstevel@tonic-gate * The final determination is made by supply_out().
5730Sstevel@tonic-gate */
5740Sstevel@tonic-gate if (!(RT->rt_state & RS_IF) && !(RT->rt_state & RS_MHOME) &&
5750Sstevel@tonic-gate RT->rt_gate != loopaddr)
5760Sstevel@tonic-gate nhop = RT->rt_gate;
5770Sstevel@tonic-gate else
5780Sstevel@tonic-gate nhop = 0;
5790Sstevel@tonic-gate }
5800Sstevel@tonic-gate
5810Sstevel@tonic-gate metric = RT->rt_metric;
5820Sstevel@tonic-gate ags = 0;
5830Sstevel@tonic-gate
5840Sstevel@tonic-gate if (!RT_ISHOST(RT)) {
5850Sstevel@tonic-gate /*
5860Sstevel@tonic-gate * Always suppress network routes into other, existing
5870Sstevel@tonic-gate * network routes
5880Sstevel@tonic-gate */
5890Sstevel@tonic-gate ags |= AGS_SUPPRESS;
5900Sstevel@tonic-gate
5910Sstevel@tonic-gate /*
5920Sstevel@tonic-gate * Generate supernets if allowed.
5930Sstevel@tonic-gate * If we can be heard by RIPv1 systems, we will
5940Sstevel@tonic-gate * later convert back to ordinary nets.
5950Sstevel@tonic-gate * This unifies dealing with received supernets.
5960Sstevel@tonic-gate */
5970Sstevel@tonic-gate if ((ws.state & WS_ST_AG) && ((RT->rt_state & RS_SUBNET) ||
5980Sstevel@tonic-gate (ws.state & WS_ST_SUPER_AG)))
5990Sstevel@tonic-gate ags |= AGS_AGGREGATE;
6000Sstevel@tonic-gate } else if (!(RT->rt_state & RS_MHOME)) {
6010Sstevel@tonic-gate /*
6020Sstevel@tonic-gate * We should always suppress (into existing network routes)
6030Sstevel@tonic-gate * the host routes for the local end of our point-to-point
6040Sstevel@tonic-gate * links.
6050Sstevel@tonic-gate * If we are suppressing host routes in general, then do so.
6060Sstevel@tonic-gate * Avoid advertising host routes onto their own network,
6070Sstevel@tonic-gate * where they should be handled by proxy-ARP.
6080Sstevel@tonic-gate */
6090Sstevel@tonic-gate if ((RT->rt_state & RS_LOCAL) || ridhosts ||
6100Sstevel@tonic-gate on_net(dst, ws.to_net, ws.to_mask))
6110Sstevel@tonic-gate ags |= AGS_SUPPRESS;
6120Sstevel@tonic-gate
6130Sstevel@tonic-gate /*
6140Sstevel@tonic-gate * Aggregate stray host routes into network routes if allowed.
6150Sstevel@tonic-gate * We cannot aggregate host routes into small network routes
6160Sstevel@tonic-gate * without confusing RIPv1 listeners into thinking the
6170Sstevel@tonic-gate * network routes are host routes.
6180Sstevel@tonic-gate */
6190Sstevel@tonic-gate if ((ws.state & WS_ST_AG) && (ws.state & WS_ST_RIP2_ALL))
6200Sstevel@tonic-gate ags |= AGS_AGGREGATE;
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate
6230Sstevel@tonic-gate /*
6240Sstevel@tonic-gate * Do not send RIPv1 advertisements of subnets to other
6250Sstevel@tonic-gate * networks. If possible, multicast them by RIPv2.
6260Sstevel@tonic-gate */
6270Sstevel@tonic-gate if ((RT->rt_state & RS_SUBNET) && !(ws.state & WS_ST_RIP2_ALL) &&
6280Sstevel@tonic-gate !on_net(dst, ws.to_std_net, ws.to_std_mask))
6290Sstevel@tonic-gate ags |= AGS_RIPV2 | AGS_AGGREGATE;
6300Sstevel@tonic-gate
6310Sstevel@tonic-gate
6320Sstevel@tonic-gate /*
6330Sstevel@tonic-gate * Do not send a route back to where it came from, except in
6340Sstevel@tonic-gate * response to a query. This is "split-horizon". That means not
6350Sstevel@tonic-gate * advertising back to the same network and so via the same interface.
6360Sstevel@tonic-gate *
6370Sstevel@tonic-gate * We want to suppress routes that might have been fragmented
6380Sstevel@tonic-gate * from this route by a RIPv1 router and sent back to us, and so we
6390Sstevel@tonic-gate * cannot forget this route here. Let the split-horizon route
6400Sstevel@tonic-gate * suppress the fragmented routes and then itself be forgotten.
6410Sstevel@tonic-gate *
6420Sstevel@tonic-gate * Include the routes for both ends of point-to-point interfaces
6430Sstevel@tonic-gate * among those suppressed by split-horizon, since the other side
6440Sstevel@tonic-gate * should knows them as well as we do.
6450Sstevel@tonic-gate *
6460Sstevel@tonic-gate * Notice spare routes with the same metric that we are about to
6470Sstevel@tonic-gate * advertise, to split the horizon on redundant, inactive paths.
6480Sstevel@tonic-gate */
6490Sstevel@tonic-gate if (ws.ifp != NULL && !(ws.state & WS_ST_QUERY) &&
6500Sstevel@tonic-gate (ws.state & WS_ST_TO_ON_NET) && (!(RT->rt_state & RS_IF) ||
6510Sstevel@tonic-gate (ws.ifp->int_if_flags & IFF_POINTOPOINT))) {
6520Sstevel@tonic-gate for (rts = RT->rt_spares, sparecount = 0;
6530Sstevel@tonic-gate sparecount < RT->rt_num_spares; sparecount++, rts++) {
6540Sstevel@tonic-gate if (rts->rts_metric > metric || rts->rts_ifp != ws.ifp)
6550Sstevel@tonic-gate continue;
6560Sstevel@tonic-gate
6570Sstevel@tonic-gate /*
6580Sstevel@tonic-gate * If we do not mark the route with AGS_SPLIT_HZ here,
6590Sstevel@tonic-gate * it will be poisoned-reverse, or advertised back
6600Sstevel@tonic-gate * toward its source with an infinite metric.
6610Sstevel@tonic-gate * If we have recently advertised the route with a
6620Sstevel@tonic-gate * better metric than we now have, then we should
6630Sstevel@tonic-gate * poison-reverse the route before suppressing it for
6640Sstevel@tonic-gate * split-horizon.
6650Sstevel@tonic-gate *
6660Sstevel@tonic-gate * In almost all cases, if there is no spare for the
6670Sstevel@tonic-gate * route then it is either old and dead or a brand
6680Sstevel@tonic-gate * new route. If it is brand new, there is no need
6690Sstevel@tonic-gate * for poison-reverse. If it is old and dead, it
6700Sstevel@tonic-gate * is already poisoned.
6710Sstevel@tonic-gate */
6720Sstevel@tonic-gate if (RT->rt_poison_time < now_expire ||
6730Sstevel@tonic-gate RT->rt_poison_metric >= metric ||
6740Sstevel@tonic-gate RT->rt_spares[1].rts_gate == 0) {
6750Sstevel@tonic-gate ags |= AGS_SPLIT_HZ;
6760Sstevel@tonic-gate ags &= ~AGS_SUPPRESS;
6770Sstevel@tonic-gate }
6780Sstevel@tonic-gate metric = HOPCNT_INFINITY;
6790Sstevel@tonic-gate break;
6800Sstevel@tonic-gate }
6810Sstevel@tonic-gate }
6820Sstevel@tonic-gate
6830Sstevel@tonic-gate /*
6840Sstevel@tonic-gate * Keep track of the best metric with which the
6850Sstevel@tonic-gate * route has been advertised recently.
6860Sstevel@tonic-gate */
6870Sstevel@tonic-gate if (RT->rt_poison_metric >= metric ||
6880Sstevel@tonic-gate RT->rt_poison_time < now_expire) {
6890Sstevel@tonic-gate RT->rt_poison_time = now.tv_sec;
6900Sstevel@tonic-gate RT->rt_poison_metric = metric;
6910Sstevel@tonic-gate }
6920Sstevel@tonic-gate
6930Sstevel@tonic-gate /*
6940Sstevel@tonic-gate * Adjust the outgoing metric by the cost of the link.
6950Sstevel@tonic-gate * Avoid aggregation when a route is counting to infinity.
6960Sstevel@tonic-gate */
6970Sstevel@tonic-gate pref = RT->rt_poison_metric + ws.metric;
6980Sstevel@tonic-gate metric += ws.metric;
6990Sstevel@tonic-gate
7000Sstevel@tonic-gate /*
7010Sstevel@tonic-gate * If this is a static route pointing to the same interface
7020Sstevel@tonic-gate * upon which we are sending out the RIP RESPONSE
7030Sstevel@tonic-gate * adjust the preference so that we don't aggregate into this
7040Sstevel@tonic-gate * route. Note that the maximum possible hop count on a route
7050Sstevel@tonic-gate * per RFC 2453 is 16 (HOPCNT_INFINITY)
7060Sstevel@tonic-gate */
7070Sstevel@tonic-gate if ((RT->rt_state & RS_STATIC) && (ws.ifp == RT->rt_ifp))
7080Sstevel@tonic-gate pref = (HOPCNT_INFINITY+1);
7090Sstevel@tonic-gate
7100Sstevel@tonic-gate /*
7110Sstevel@tonic-gate * Do not advertise stable routes that will be ignored,
7120Sstevel@tonic-gate * unless we are answering a query.
7130Sstevel@tonic-gate * If the route recently was advertised with a metric that
7140Sstevel@tonic-gate * would have been less than infinity through this interface,
7150Sstevel@tonic-gate * we need to continue to advertise it in order to poison it.
7160Sstevel@tonic-gate */
7170Sstevel@tonic-gate if (metric >= HOPCNT_INFINITY) {
7180Sstevel@tonic-gate if (!(ws.state & WS_ST_QUERY) && (pref >= HOPCNT_INFINITY ||
7190Sstevel@tonic-gate RT->rt_poison_time < now_garbage))
7200Sstevel@tonic-gate return (0);
7210Sstevel@tonic-gate
7220Sstevel@tonic-gate metric = HOPCNT_INFINITY;
7230Sstevel@tonic-gate }
7240Sstevel@tonic-gate
7250Sstevel@tonic-gate /*
7260Sstevel@tonic-gate * supply this route out on the wire- we only care about dest/mask
7270Sstevel@tonic-gate * and so can ignore all rt_spares[i] with i > 0
7280Sstevel@tonic-gate */
7290Sstevel@tonic-gate ag_check(dst, RT->rt_mask, 0, RT->rt_ifp, nhop, metric, pref,
7300Sstevel@tonic-gate RT->rt_seqno, RT->rt_tag, ags, supply_out);
7310Sstevel@tonic-gate return (0);
7320Sstevel@tonic-gate #undef RT
7330Sstevel@tonic-gate }
7340Sstevel@tonic-gate
7350Sstevel@tonic-gate
7360Sstevel@tonic-gate /*
7370Sstevel@tonic-gate * Supply dst with the contents of the routing tables.
7380Sstevel@tonic-gate * If this won't fit in one packet, chop it up into several.
7390Sstevel@tonic-gate */
7400Sstevel@tonic-gate void
supply(struct sockaddr_in * dst,struct interface * ifp,enum output_type type,int flash,int vers,boolean_t passwd_ok)7410Sstevel@tonic-gate supply(struct sockaddr_in *dst,
7420Sstevel@tonic-gate struct interface *ifp, /* output interface */
7430Sstevel@tonic-gate enum output_type type,
7440Sstevel@tonic-gate int flash, /* 1=flash update */
7450Sstevel@tonic-gate int vers, /* RIP version */
7460Sstevel@tonic-gate boolean_t passwd_ok) /* OK to include cleartext password */
7470Sstevel@tonic-gate {
7480Sstevel@tonic-gate struct rt_entry *rt;
7490Sstevel@tonic-gate uint8_t def_metric;
7500Sstevel@tonic-gate
7510Sstevel@tonic-gate
7520Sstevel@tonic-gate ws.state = 0;
7530Sstevel@tonic-gate ws.gen_limit = WS_GEN_LIMIT_MAX;
7540Sstevel@tonic-gate
7550Sstevel@tonic-gate ws.to = *dst;
7560Sstevel@tonic-gate ws.to_std_mask = std_mask(ws.to.sin_addr.s_addr);
7570Sstevel@tonic-gate ws.to_std_net = ntohl(ws.to.sin_addr.s_addr) & ws.to_std_mask;
7580Sstevel@tonic-gate
7590Sstevel@tonic-gate if (ifp != NULL) {
7600Sstevel@tonic-gate ws.to_mask = ifp->int_mask;
7610Sstevel@tonic-gate ws.to_net = ifp->int_net;
7620Sstevel@tonic-gate if (on_net(ws.to.sin_addr.s_addr, ws.to_net, ws.to_mask) ||
7630Sstevel@tonic-gate type == OUT_MULTICAST)
7640Sstevel@tonic-gate ws.state |= WS_ST_TO_ON_NET;
7650Sstevel@tonic-gate
7660Sstevel@tonic-gate } else {
7670Sstevel@tonic-gate ws.to_mask = ripv1_mask_net(ws.to.sin_addr.s_addr, NULL);
7680Sstevel@tonic-gate ws.to_net = ntohl(ws.to.sin_addr.s_addr) & ws.to_mask;
7690Sstevel@tonic-gate rt = rtfind(dst->sin_addr.s_addr);
7700Sstevel@tonic-gate if (rt != NULL)
7710Sstevel@tonic-gate ifp = rt->rt_ifp;
7720Sstevel@tonic-gate else
7730Sstevel@tonic-gate return;
7740Sstevel@tonic-gate }
7750Sstevel@tonic-gate
7760Sstevel@tonic-gate ws.npackets = 0;
7770Sstevel@tonic-gate if (flash)
7780Sstevel@tonic-gate ws.state |= WS_ST_FLASH;
7790Sstevel@tonic-gate
7800Sstevel@tonic-gate ws.ifp = ifp;
7810Sstevel@tonic-gate
7820Sstevel@tonic-gate /*
7830Sstevel@tonic-gate * Routes in the table were already adjusted by their respective
7840Sstevel@tonic-gate * destination interface costs (which are zero by default) on
7850Sstevel@tonic-gate * input. The following is the value by which each route's metric
7860Sstevel@tonic-gate * will be bumped up on output.
7870Sstevel@tonic-gate */
7880Sstevel@tonic-gate ws.metric = 1;
7890Sstevel@tonic-gate
7900Sstevel@tonic-gate ripv12_buf.rip.rip_vers = vers;
7910Sstevel@tonic-gate
7920Sstevel@tonic-gate switch (type) {
7930Sstevel@tonic-gate case OUT_MULTICAST:
7940Sstevel@tonic-gate if (ifp->int_if_flags & IFF_MULTICAST)
7950Sstevel@tonic-gate v2buf.type = OUT_MULTICAST;
7960Sstevel@tonic-gate else
7970Sstevel@tonic-gate v2buf.type = NO_OUT_MULTICAST;
7980Sstevel@tonic-gate v12buf.type = OUT_BROADCAST;
7990Sstevel@tonic-gate break;
8000Sstevel@tonic-gate
8010Sstevel@tonic-gate case OUT_QUERY:
8020Sstevel@tonic-gate ws.state |= WS_ST_QUERY;
8030Sstevel@tonic-gate /* FALLTHROUGH */
8040Sstevel@tonic-gate case OUT_BROADCAST:
8050Sstevel@tonic-gate case OUT_UNICAST:
8060Sstevel@tonic-gate v2buf.type = (vers == RIPv2) ? type : NO_OUT_RIPV2;
8070Sstevel@tonic-gate v12buf.type = type;
8080Sstevel@tonic-gate break;
8090Sstevel@tonic-gate
8100Sstevel@tonic-gate case NO_OUT_MULTICAST:
8110Sstevel@tonic-gate case NO_OUT_RIPV2:
8120Sstevel@tonic-gate return; /* no output */
8130Sstevel@tonic-gate }
8140Sstevel@tonic-gate
8150Sstevel@tonic-gate if (vers == RIPv2) {
8160Sstevel@tonic-gate /* full RIPv2 only if cannot be heard by RIPv1 listeners */
8170Sstevel@tonic-gate if (type != OUT_BROADCAST)
8180Sstevel@tonic-gate ws.state |= WS_ST_RIP2_ALL;
8190Sstevel@tonic-gate if ((ws.state & WS_ST_QUERY) || !(ws.state & WS_ST_TO_ON_NET)) {
8200Sstevel@tonic-gate ws.state |= (WS_ST_AG | WS_ST_SUPER_AG);
8210Sstevel@tonic-gate } else if (ifp == NULL || !(ifp->int_state & IS_NO_AG)) {
8220Sstevel@tonic-gate ws.state |= WS_ST_AG;
8230Sstevel@tonic-gate if (type != OUT_BROADCAST && (ifp == NULL ||
8240Sstevel@tonic-gate !(ifp->int_state & IS_NO_SUPER_AG)))
8250Sstevel@tonic-gate ws.state |= WS_ST_SUPER_AG;
8260Sstevel@tonic-gate }
8270Sstevel@tonic-gate
8280Sstevel@tonic-gate /* See if this packet needs authenticating */
8290Sstevel@tonic-gate ws.a = find_auth(ifp);
8300Sstevel@tonic-gate if (!passwd_ok && ws.a != NULL && ws.a->type == RIP_AUTH_PW)
8310Sstevel@tonic-gate ws.a = NULL;
8320Sstevel@tonic-gate if (ws.a != NULL && (ulong_t)ws.a->end < (ulong_t)clk.tv_sec &&
8330Sstevel@tonic-gate !ws.a->warnedflag) {
8340Sstevel@tonic-gate /*
8350Sstevel@tonic-gate * If the best key is an expired one, we may as
8360Sstevel@tonic-gate * well use it. Log this event.
8370Sstevel@tonic-gate */
8380Sstevel@tonic-gate writelog(LOG_WARNING,
8390Sstevel@tonic-gate "Using expired auth while transmitting to %s",
8400Sstevel@tonic-gate naddr_ntoa(ws.to.sin_addr.s_addr));
8410Sstevel@tonic-gate ws.a->warnedflag = 1;
8420Sstevel@tonic-gate }
8430Sstevel@tonic-gate } else {
8440Sstevel@tonic-gate ws.a = NULL;
8450Sstevel@tonic-gate }
8460Sstevel@tonic-gate
8470Sstevel@tonic-gate clr_ws_buf(&v12buf, ws.a);
8480Sstevel@tonic-gate clr_ws_buf(&v2buf, ws.a);
8490Sstevel@tonic-gate
8500Sstevel@tonic-gate /*
8510Sstevel@tonic-gate * Fake a default route if asked and if there is not already
8520Sstevel@tonic-gate * a better, real default route.
8530Sstevel@tonic-gate */
8540Sstevel@tonic-gate if (should_supply(NULL) && (def_metric = ifp->int_d_metric) != 0) {
8550Sstevel@tonic-gate if (NULL == (rt = rtget(RIP_DEFAULT, 0)) ||
8560Sstevel@tonic-gate rt->rt_metric+ws.metric >= def_metric) {
8570Sstevel@tonic-gate ws.state |= WS_ST_DEFAULT;
8580Sstevel@tonic-gate ag_check(0, 0, 0, NULL, 0, def_metric, def_metric,
8590Sstevel@tonic-gate 0, 0, 0, supply_out);
8600Sstevel@tonic-gate } else {
8610Sstevel@tonic-gate def_metric = rt->rt_metric+ws.metric;
8620Sstevel@tonic-gate }
8630Sstevel@tonic-gate
8640Sstevel@tonic-gate /*
8650Sstevel@tonic-gate * If both RIPv2 and the poor-man's router discovery
8660Sstevel@tonic-gate * kludge are on, arrange to advertise an extra
8670Sstevel@tonic-gate * default route via RIPv1.
8680Sstevel@tonic-gate */
8690Sstevel@tonic-gate if ((ws.state & WS_ST_RIP2_ALL) &&
8700Sstevel@tonic-gate (ifp->int_state & IS_PM_RDISC)) {
8710Sstevel@tonic-gate ripv12_buf.rip.rip_vers = RIPv1;
8720Sstevel@tonic-gate v12buf.n->n_family = RIP_AF_INET;
8730Sstevel@tonic-gate v12buf.n->n_dst = htonl(RIP_DEFAULT);
8740Sstevel@tonic-gate v12buf.n->n_metric = htonl(def_metric);
8750Sstevel@tonic-gate v12buf.n++;
8760Sstevel@tonic-gate }
8770Sstevel@tonic-gate }
8780Sstevel@tonic-gate
8790Sstevel@tonic-gate (void) rn_walktree(rhead, walk_supply, NULL);
8800Sstevel@tonic-gate ag_flush(0, 0, supply_out);
8810Sstevel@tonic-gate
8820Sstevel@tonic-gate /*
8830Sstevel@tonic-gate * Flush the packet buffers, provided they are not empty and
8840Sstevel@tonic-gate * do not contain only the password.
8850Sstevel@tonic-gate */
8860Sstevel@tonic-gate if (v12buf.n != v12buf.base &&
8870Sstevel@tonic-gate (v12buf.n > v12buf.base+1 ||
8880Sstevel@tonic-gate v12buf.base->n_family != RIP_AF_AUTH))
8890Sstevel@tonic-gate supply_write(&v12buf);
8900Sstevel@tonic-gate if (v2buf.n != v2buf.base && (v2buf.n > v2buf.base+1 ||
8910Sstevel@tonic-gate v2buf.base->n_family != RIP_AF_AUTH))
8920Sstevel@tonic-gate supply_write(&v2buf);
8930Sstevel@tonic-gate
8940Sstevel@tonic-gate /*
8950Sstevel@tonic-gate * If we sent nothing and this is an answer to a query, send
8960Sstevel@tonic-gate * an empty buffer.
8970Sstevel@tonic-gate */
8980Sstevel@tonic-gate if (ws.npackets == 0 && (ws.state & WS_ST_QUERY)) {
8990Sstevel@tonic-gate supply_write(&v2buf);
9000Sstevel@tonic-gate if (ws.npackets == 0)
9010Sstevel@tonic-gate supply_write(&v12buf);
9020Sstevel@tonic-gate }
9030Sstevel@tonic-gate }
9040Sstevel@tonic-gate
9050Sstevel@tonic-gate
9060Sstevel@tonic-gate /*
9070Sstevel@tonic-gate * send all of the routing table or just do a flash update
9080Sstevel@tonic-gate */
9090Sstevel@tonic-gate void
rip_bcast(int flash)9100Sstevel@tonic-gate rip_bcast(int flash)
9110Sstevel@tonic-gate {
9120Sstevel@tonic-gate static struct sockaddr_in dst = {AF_INET};
9130Sstevel@tonic-gate struct interface *ifp;
9140Sstevel@tonic-gate enum output_type type;
9150Sstevel@tonic-gate int vers;
9160Sstevel@tonic-gate struct timeval rtime;
9170Sstevel@tonic-gate
9180Sstevel@tonic-gate
9190Sstevel@tonic-gate need_flash = _B_FALSE;
9200Sstevel@tonic-gate intvl_random(&rtime, MIN_WAITTIME, MAX_WAITTIME);
9210Sstevel@tonic-gate no_flash = rtime;
9220Sstevel@tonic-gate timevaladd(&no_flash, &now);
9230Sstevel@tonic-gate
9240Sstevel@tonic-gate if (!rip_enabled)
9250Sstevel@tonic-gate return;
9260Sstevel@tonic-gate
9270Sstevel@tonic-gate trace_act("send %s and inhibit dynamic updates for %.3f sec",
9280Sstevel@tonic-gate flash ? "dynamic update" : "all routes",
9290Sstevel@tonic-gate rtime.tv_sec + ((double)rtime.tv_usec)/1000000.0);
9300Sstevel@tonic-gate
9310Sstevel@tonic-gate for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) {
9320Sstevel@tonic-gate /*
9330Sstevel@tonic-gate * Skip interfaces not doing RIP or for which IP
9340Sstevel@tonic-gate * forwarding isn't turned on. Skip duplicate
9350Sstevel@tonic-gate * interfaces, we don't want to generate duplicate
9360Sstevel@tonic-gate * packets. Do try broken interfaces to see if they
9370Sstevel@tonic-gate * have healed.
9380Sstevel@tonic-gate */
9390Sstevel@tonic-gate if (IS_RIP_OUT_OFF(ifp->int_state) ||
9400Sstevel@tonic-gate (ifp->int_state & IS_DUP) ||
9410Sstevel@tonic-gate !IS_IFF_ROUTING(ifp->int_if_flags))
9420Sstevel@tonic-gate continue;
9430Sstevel@tonic-gate
9440Sstevel@tonic-gate /* skip turned off interfaces */
9450Sstevel@tonic-gate if (!IS_IFF_UP(ifp->int_if_flags))
9460Sstevel@tonic-gate continue;
9470Sstevel@tonic-gate
9480Sstevel@tonic-gate /* skip interfaces we shouldn't use */
9490Sstevel@tonic-gate if (IS_IFF_QUIET(ifp->int_if_flags))
9500Sstevel@tonic-gate continue;
9510Sstevel@tonic-gate
9520Sstevel@tonic-gate vers = (ifp->int_state & IS_NO_RIPV1_OUT) ? RIPv2 : RIPv1;
9530Sstevel@tonic-gate dst.sin_addr.s_addr = ifp->int_ripout_addr;
9540Sstevel@tonic-gate
9550Sstevel@tonic-gate /*
9560Sstevel@tonic-gate * Ignore the interface if it's not broadcast,
9570Sstevel@tonic-gate * point-to-point, or remote. It must be non-broadcast
9580Sstevel@tonic-gate * multiaccess, and therefore unsupported.
9590Sstevel@tonic-gate */
9600Sstevel@tonic-gate if (!(ifp->int_if_flags & (IFF_BROADCAST | IFF_POINTOPOINT)) &&
9610Sstevel@tonic-gate !(ifp->int_state & IS_REMOTE))
9620Sstevel@tonic-gate continue;
9630Sstevel@tonic-gate
9640Sstevel@tonic-gate type = (ifp->int_if_flags & IFF_BROADCAST) ?
9650Sstevel@tonic-gate OUT_BROADCAST : OUT_UNICAST;
9660Sstevel@tonic-gate if (vers == RIPv2 && (ifp->int_if_flags & IFF_MULTICAST) &&
9670Sstevel@tonic-gate !(ifp->int_state & IS_NO_RIP_MCAST))
9680Sstevel@tonic-gate type = OUT_MULTICAST;
9690Sstevel@tonic-gate
9700Sstevel@tonic-gate supply(&dst, ifp, type, flash, vers, _B_TRUE);
9710Sstevel@tonic-gate }
9720Sstevel@tonic-gate
9730Sstevel@tonic-gate update_seqno++; /* all routes are up to date */
9740Sstevel@tonic-gate }
9750Sstevel@tonic-gate
9760Sstevel@tonic-gate
9770Sstevel@tonic-gate /*
9780Sstevel@tonic-gate * Ask for routes
9790Sstevel@tonic-gate * Do it only once to an interface, and not even after the interface
9800Sstevel@tonic-gate * was broken and recovered.
9810Sstevel@tonic-gate */
9820Sstevel@tonic-gate void
rip_query(void)9830Sstevel@tonic-gate rip_query(void)
9840Sstevel@tonic-gate {
9850Sstevel@tonic-gate static struct sockaddr_in dst = {AF_INET};
9860Sstevel@tonic-gate struct interface *ifp;
9870Sstevel@tonic-gate struct rip buf;
9880Sstevel@tonic-gate enum output_type type;
9890Sstevel@tonic-gate
9900Sstevel@tonic-gate
9910Sstevel@tonic-gate if (!rip_enabled)
9920Sstevel@tonic-gate return;
9930Sstevel@tonic-gate
9940Sstevel@tonic-gate (void) memset(&buf, 0, sizeof (buf));
9950Sstevel@tonic-gate
9960Sstevel@tonic-gate for (ifp = ifnet; ifp; ifp = ifp->int_next) {
9970Sstevel@tonic-gate /*
9980Sstevel@tonic-gate * Skip interfaces those already queried. Do not ask
9990Sstevel@tonic-gate * via interfaces through which we don't accept input.
10000Sstevel@tonic-gate * Do not ask via interfaces that cannot send RIP
10010Sstevel@tonic-gate * packets. Don't send queries on duplicate
10020Sstevel@tonic-gate * interfaces, that would generate duplicate packets
10030Sstevel@tonic-gate * on link. Do try broken interfaces to see if they
10040Sstevel@tonic-gate * have healed.
10050Sstevel@tonic-gate */
10060Sstevel@tonic-gate if (IS_RIP_IN_OFF(ifp->int_state) ||
10070Sstevel@tonic-gate (ifp->int_state & IS_DUP) ||
10080Sstevel@tonic-gate ifp->int_query_time != NEVER)
10090Sstevel@tonic-gate continue;
10100Sstevel@tonic-gate
10110Sstevel@tonic-gate /* skip turned off interfaces */
10120Sstevel@tonic-gate if (!IS_IFF_UP(ifp->int_if_flags))
10130Sstevel@tonic-gate continue;
10140Sstevel@tonic-gate
10150Sstevel@tonic-gate /* skip interfaces we shouldn't use */
10160Sstevel@tonic-gate if (IS_IFF_QUIET(ifp->int_if_flags))
10170Sstevel@tonic-gate continue;
10180Sstevel@tonic-gate
10190Sstevel@tonic-gate /*
10200Sstevel@tonic-gate * Ignore the interface if it's not broadcast,
10210Sstevel@tonic-gate * point-to-point, or remote. It must be non-broadcast
10220Sstevel@tonic-gate * multiaccess, and therefore unsupported.
10230Sstevel@tonic-gate */
10240Sstevel@tonic-gate if (!(ifp->int_if_flags & (IFF_BROADCAST | IFF_POINTOPOINT)) &&
10250Sstevel@tonic-gate !(ifp->int_state & IS_REMOTE))
10260Sstevel@tonic-gate continue;
10270Sstevel@tonic-gate
10280Sstevel@tonic-gate buf.rip_cmd = RIPCMD_REQUEST;
10290Sstevel@tonic-gate buf.rip_nets[0].n_family = RIP_AF_UNSPEC;
10300Sstevel@tonic-gate buf.rip_nets[0].n_metric = htonl(HOPCNT_INFINITY);
10310Sstevel@tonic-gate
10320Sstevel@tonic-gate /*
10330Sstevel@tonic-gate * Send a RIPv1 query only if allowed and if we will
10340Sstevel@tonic-gate * listen to RIPv1 routers.
10350Sstevel@tonic-gate */
10360Sstevel@tonic-gate if ((ifp->int_state & IS_NO_RIPV1_OUT) ||
10370Sstevel@tonic-gate (ifp->int_state & IS_NO_RIPV1_IN)) {
10380Sstevel@tonic-gate buf.rip_vers = RIPv2;
10390Sstevel@tonic-gate } else {
10400Sstevel@tonic-gate buf.rip_vers = RIPv1;
10410Sstevel@tonic-gate }
10420Sstevel@tonic-gate
10430Sstevel@tonic-gate dst.sin_addr.s_addr = ifp->int_ripout_addr;
10440Sstevel@tonic-gate
10450Sstevel@tonic-gate type = (ifp->int_if_flags & IFF_BROADCAST) ?
10460Sstevel@tonic-gate OUT_BROADCAST : OUT_UNICAST;
10470Sstevel@tonic-gate if (buf.rip_vers == RIPv2 &&
10480Sstevel@tonic-gate (ifp->int_if_flags & IFF_MULTICAST) &&
10490Sstevel@tonic-gate !(ifp->int_state & IS_NO_RIP_MCAST))
10500Sstevel@tonic-gate type = OUT_MULTICAST;
10510Sstevel@tonic-gate
10520Sstevel@tonic-gate ifp->int_query_time = now.tv_sec+SUPPLY_INTERVAL;
10530Sstevel@tonic-gate if (output(type, &dst, ifp, &buf, sizeof (buf)) < 0)
10540Sstevel@tonic-gate if_sick(ifp, _B_FALSE);
10550Sstevel@tonic-gate }
10560Sstevel@tonic-gate }
1057