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