386BSD 0.1 development
[unix-history] / usr / src / sbin / routed / output.c
CommitLineData
13bd785c
WJ
1/*
2 * Copyright (c) 1983, 1988 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char sccsid[] = "@(#)output.c 5.15 (Berkeley) 2/28/91";
36#endif /* not lint */
37
38/*
39 * Routing Table Management Daemon
40 */
41#include "defs.h"
42
43/*
44 * Apply the function "f" to all non-passive
45 * interfaces. If the interface supports the
46 * use of broadcasting use it, otherwise address
47 * the output to the known router.
48 */
49toall(f, rtstate, skipif)
50 int (*f)();
51 int rtstate;
52 struct interface *skipif;
53{
54 register struct interface *ifp;
55 register struct sockaddr *dst;
56 register int flags;
57 extern struct interface *ifnet;
58
59 for (ifp = ifnet; ifp; ifp = ifp->int_next) {
60 if (ifp->int_flags & IFF_PASSIVE || ifp == skipif)
61 continue;
62 dst = ifp->int_flags & IFF_BROADCAST ? &ifp->int_broadaddr :
63 ifp->int_flags & IFF_POINTOPOINT ? &ifp->int_dstaddr :
64 &ifp->int_addr;
65 flags = ifp->int_flags & IFF_INTERFACE ? MSG_DONTROUTE : 0;
66 (*f)(dst, flags, ifp, rtstate);
67 }
68}
69
70/*
71 * Output a preformed packet.
72 */
73/*ARGSUSED*/
74sndmsg(dst, flags, ifp, rtstate)
75 struct sockaddr *dst;
76 int flags;
77 struct interface *ifp;
78 int rtstate;
79{
80
81 (*afswitch[dst->sa_family].af_output)(s, flags,
82 dst, sizeof (struct rip));
83 TRACE_OUTPUT(ifp, dst, sizeof (struct rip));
84}
85
86/*
87 * Supply dst with the contents of the routing tables.
88 * If this won't fit in one packet, chop it up into several.
89 */
90supply(dst, flags, ifp, rtstate)
91 struct sockaddr *dst;
92 int flags;
93 register struct interface *ifp;
94 int rtstate;
95{
96 register struct rt_entry *rt;
97 register struct netinfo *n = msg->rip_nets;
98 register struct rthash *rh;
99 struct rthash *base = hosthash;
100 int doinghost = 1, size;
101 int (*output)() = afswitch[dst->sa_family].af_output;
102 int (*sendroute)() = afswitch[dst->sa_family].af_sendroute;
103 int npackets = 0;
104
105 msg->rip_cmd = RIPCMD_RESPONSE;
106 msg->rip_vers = RIPVERSION;
107 bzero(msg->rip_res1, sizeof(msg->rip_res1));
108again:
109 for (rh = base; rh < &base[ROUTEHASHSIZ]; rh++)
110 for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
111 /*
112 * Don't resend the information on the network
113 * from which it was received (unless sending
114 * in response to a query).
115 */
116 if (ifp && rt->rt_ifp == ifp &&
117 (rt->rt_state & RTS_INTERFACE) == 0)
118 continue;
119 if (rt->rt_state & RTS_EXTERNAL)
120 continue;
121 /*
122 * For dynamic updates, limit update to routes
123 * with the specified state.
124 */
125 if (rtstate && (rt->rt_state & rtstate) == 0)
126 continue;
127 /*
128 * Limit the spread of subnet information
129 * to those who are interested.
130 */
131 if (doinghost == 0 && rt->rt_state & RTS_SUBNET) {
132 if (rt->rt_dst.sa_family != dst->sa_family)
133 continue;
134 if ((*sendroute)(rt, dst) == 0)
135 continue;
136 }
137 size = (char *)n - packet;
138 if (size > MAXPACKETSIZE - sizeof (struct netinfo)) {
139 TRACE_OUTPUT(ifp, dst, size);
140 (*output)(s, flags, dst, size);
141 /*
142 * If only sending to ourselves,
143 * one packet is enough to monitor interface.
144 */
145 if (ifp && (ifp->int_flags &
146 (IFF_BROADCAST | IFF_POINTOPOINT | IFF_REMOTE)) == 0)
147 return;
148 n = msg->rip_nets;
149 npackets++;
150 }
151 n->rip_dst = rt->rt_dst;
152#if BSD < 198810
153 if (sizeof(n->rip_dst.sa_family) > 1) /* XXX */
154 n->rip_dst.sa_family = htons(n->rip_dst.sa_family);
155#else
156#define osa(x) ((struct osockaddr *)(&(x)))
157 osa(n->rip_dst)->sa_family = htons(n->rip_dst.sa_family);
158#endif
159 n->rip_metric = htonl(rt->rt_metric);
160 n++;
161 }
162 if (doinghost) {
163 doinghost = 0;
164 base = nethash;
165 goto again;
166 }
167 if (n != msg->rip_nets || (npackets == 0 && rtstate == 0)) {
168 size = (char *)n - packet;
169 TRACE_OUTPUT(ifp, dst, size);
170 (*output)(s, flags, dst, size);
171 }
172}