output.c revision 1.1 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1983, 1988 Regents of the University of California.
3 1.1 cgd * 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.1 cgd static char sccsid[] = "@(#)output.c 5.15 (Berkeley) 2/28/91";
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd /*
39 1.1 cgd * Routing Table Management Daemon
40 1.1 cgd */
41 1.1 cgd #include "defs.h"
42 1.1 cgd
43 1.1 cgd /*
44 1.1 cgd * Apply the function "f" to all non-passive
45 1.1 cgd * interfaces. If the interface supports the
46 1.1 cgd * use of broadcasting use it, otherwise address
47 1.1 cgd * the output to the known router.
48 1.1 cgd */
49 1.1 cgd toall(f, rtstate, skipif)
50 1.1 cgd int (*f)();
51 1.1 cgd int rtstate;
52 1.1 cgd struct interface *skipif;
53 1.1 cgd {
54 1.1 cgd register struct interface *ifp;
55 1.1 cgd register struct sockaddr *dst;
56 1.1 cgd register int flags;
57 1.1 cgd extern struct interface *ifnet;
58 1.1 cgd
59 1.1 cgd for (ifp = ifnet; ifp; ifp = ifp->int_next) {
60 1.1 cgd if (ifp->int_flags & IFF_PASSIVE || ifp == skipif)
61 1.1 cgd continue;
62 1.1 cgd dst = ifp->int_flags & IFF_BROADCAST ? &ifp->int_broadaddr :
63 1.1 cgd ifp->int_flags & IFF_POINTOPOINT ? &ifp->int_dstaddr :
64 1.1 cgd &ifp->int_addr;
65 1.1 cgd flags = ifp->int_flags & IFF_INTERFACE ? MSG_DONTROUTE : 0;
66 1.1 cgd (*f)(dst, flags, ifp, rtstate);
67 1.1 cgd }
68 1.1 cgd }
69 1.1 cgd
70 1.1 cgd /*
71 1.1 cgd * Output a preformed packet.
72 1.1 cgd */
73 1.1 cgd /*ARGSUSED*/
74 1.1 cgd sndmsg(dst, flags, ifp, rtstate)
75 1.1 cgd struct sockaddr *dst;
76 1.1 cgd int flags;
77 1.1 cgd struct interface *ifp;
78 1.1 cgd int rtstate;
79 1.1 cgd {
80 1.1 cgd
81 1.1 cgd (*afswitch[dst->sa_family].af_output)(s, flags,
82 1.1 cgd dst, sizeof (struct rip));
83 1.1 cgd TRACE_OUTPUT(ifp, dst, sizeof (struct rip));
84 1.1 cgd }
85 1.1 cgd
86 1.1 cgd /*
87 1.1 cgd * Supply dst with the contents of the routing tables.
88 1.1 cgd * If this won't fit in one packet, chop it up into several.
89 1.1 cgd */
90 1.1 cgd supply(dst, flags, ifp, rtstate)
91 1.1 cgd struct sockaddr *dst;
92 1.1 cgd int flags;
93 1.1 cgd register struct interface *ifp;
94 1.1 cgd int rtstate;
95 1.1 cgd {
96 1.1 cgd register struct rt_entry *rt;
97 1.1 cgd register struct netinfo *n = msg->rip_nets;
98 1.1 cgd register struct rthash *rh;
99 1.1 cgd struct rthash *base = hosthash;
100 1.1 cgd int doinghost = 1, size;
101 1.1 cgd int (*output)() = afswitch[dst->sa_family].af_output;
102 1.1 cgd int (*sendroute)() = afswitch[dst->sa_family].af_sendroute;
103 1.1 cgd int npackets = 0;
104 1.1 cgd
105 1.1 cgd msg->rip_cmd = RIPCMD_RESPONSE;
106 1.1 cgd msg->rip_vers = RIPVERSION;
107 1.1 cgd bzero(msg->rip_res1, sizeof(msg->rip_res1));
108 1.1 cgd again:
109 1.1 cgd for (rh = base; rh < &base[ROUTEHASHSIZ]; rh++)
110 1.1 cgd for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
111 1.1 cgd /*
112 1.1 cgd * Don't resend the information on the network
113 1.1 cgd * from which it was received (unless sending
114 1.1 cgd * in response to a query).
115 1.1 cgd */
116 1.1 cgd if (ifp && rt->rt_ifp == ifp &&
117 1.1 cgd (rt->rt_state & RTS_INTERFACE) == 0)
118 1.1 cgd continue;
119 1.1 cgd if (rt->rt_state & RTS_EXTERNAL)
120 1.1 cgd continue;
121 1.1 cgd /*
122 1.1 cgd * For dynamic updates, limit update to routes
123 1.1 cgd * with the specified state.
124 1.1 cgd */
125 1.1 cgd if (rtstate && (rt->rt_state & rtstate) == 0)
126 1.1 cgd continue;
127 1.1 cgd /*
128 1.1 cgd * Limit the spread of subnet information
129 1.1 cgd * to those who are interested.
130 1.1 cgd */
131 1.1 cgd if (doinghost == 0 && rt->rt_state & RTS_SUBNET) {
132 1.1 cgd if (rt->rt_dst.sa_family != dst->sa_family)
133 1.1 cgd continue;
134 1.1 cgd if ((*sendroute)(rt, dst) == 0)
135 1.1 cgd continue;
136 1.1 cgd }
137 1.1 cgd size = (char *)n - packet;
138 1.1 cgd if (size > MAXPACKETSIZE - sizeof (struct netinfo)) {
139 1.1 cgd TRACE_OUTPUT(ifp, dst, size);
140 1.1 cgd (*output)(s, flags, dst, size);
141 1.1 cgd /*
142 1.1 cgd * If only sending to ourselves,
143 1.1 cgd * one packet is enough to monitor interface.
144 1.1 cgd */
145 1.1 cgd if (ifp && (ifp->int_flags &
146 1.1 cgd (IFF_BROADCAST | IFF_POINTOPOINT | IFF_REMOTE)) == 0)
147 1.1 cgd return;
148 1.1 cgd n = msg->rip_nets;
149 1.1 cgd npackets++;
150 1.1 cgd }
151 1.1 cgd n->rip_dst = rt->rt_dst;
152 1.1 cgd #if BSD < 198810
153 1.1 cgd if (sizeof(n->rip_dst.sa_family) > 1) /* XXX */
154 1.1 cgd n->rip_dst.sa_family = htons(n->rip_dst.sa_family);
155 1.1 cgd #else
156 1.1 cgd #define osa(x) ((struct osockaddr *)(&(x)))
157 1.1 cgd osa(n->rip_dst)->sa_family = htons(n->rip_dst.sa_family);
158 1.1 cgd #endif
159 1.1 cgd n->rip_metric = htonl(rt->rt_metric);
160 1.1 cgd n++;
161 1.1 cgd }
162 1.1 cgd if (doinghost) {
163 1.1 cgd doinghost = 0;
164 1.1 cgd base = nethash;
165 1.1 cgd goto again;
166 1.1 cgd }
167 1.1 cgd if (n != msg->rip_nets || (npackets == 0 && rtstate == 0)) {
168 1.1 cgd size = (char *)n - packet;
169 1.1 cgd TRACE_OUTPUT(ifp, dst, size);
170 1.1 cgd (*output)(s, flags, dst, size);
171 1.1 cgd }
172 1.1 cgd }
173