1 /*
2 * Copyright (c) 1995, Mike Mitchell
3 * Copyright (c) 1984, 1985, 1986, 1987, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)ipx_outputfl.c
35 *
36 * $FreeBSD: src/sys/netipx/ipx_outputfl.c,v 1.4.4.2 1999/09/05 08:19:00 peter Exp $
37 */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/mbuf.h>
42 #include <sys/socket.h>
43
44 #include <net/if.h>
45 #include <net/route.h>
46
47 #include <netipx/ipx.h>
48 #include <netipx/ipx_if.h>
49 #include <netipx/ipx_var.h>
50
51 #ifdef vax
52 #include <machine/mtpr.h>
53 #endif
54
55 int ipx_copy_output = 0;
56
57 int
58 ipx_outputfl(m0, ro, flags)
59 struct mbuf *m0;
60 struct route *ro;
61 int flags;
62 {
63 register struct ipx *ipx = mtod(m0, struct ipx *);
64 register struct ifnet *ifp = NULL;
65 int error = 0;
66 struct sockaddr_ipx *dst;
67 struct route ipxroute;
68
69 /*
70 * Route packet.
71 */
72 if (ro == NULL) {
73 ro = &ipxroute;
74 bzero((caddr_t)ro, sizeof(*ro));
75 }
76 dst = (struct sockaddr_ipx *)&ro->ro_dst;
77 if (ro->ro_rt == NULL) {
78 dst->sipx_family = AF_IPX;
79 dst->sipx_len = sizeof(*dst);
80 dst->sipx_addr = ipx->ipx_dna;
81 dst->sipx_addr.x_port = 0;
82 /*
83 * If routing to interface only,
84 * short circuit routing lookup.
85 */
86 if (flags & IPX_ROUTETOIF) {
87 struct ipx_ifaddr *ia = ipx_iaonnetof(&ipx->ipx_dna);
88
89 if (ia == NULL) {
90 ipxstat.ipxs_noroute++;
91 error = ENETUNREACH;
92 goto bad;
93 }
94 ifp = ia->ia_ifp;
95 goto gotif;
96 }
97 rtalloc(ro);
98 } else if ((ro->ro_rt->rt_flags & RTF_UP) == 0) {
99 /*
100 * The old route has gone away; try for a new one.
101 */
102 rtfree(ro->ro_rt);
103 ro->ro_rt = NULL;
104 rtalloc(ro);
105 }
106 if (ro->ro_rt == NULL || (ifp = ro->ro_rt->rt_ifp) == NULL) {
107 ipxstat.ipxs_noroute++;
108 error = ENETUNREACH;
109 goto bad;
110 }
111 ro->ro_rt->rt_use++;
112 if (ro->ro_rt->rt_flags & (RTF_GATEWAY|RTF_HOST))
113 dst = (struct sockaddr_ipx *)ro->ro_rt->rt_gateway;
114 gotif:
115 /*
116 * Look for multicast addresses and
117 * and verify user is allowed to send
118 * such a packet.
119 */
120 if (dst->sipx_addr.x_host.c_host[0]&1) {
121 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
122 error = EADDRNOTAVAIL;
123 goto bad;
124 }
125 if ((flags & IPX_ALLOWBROADCAST) == 0) {
126 error = EACCES;
127 goto bad;
128 }
129 }
130
131 if (htons(ipx->ipx_len) <= ifp->if_mtu) {
132 ipxstat.ipxs_localout++;
133 if (ipx_copy_output) {
134 ipx_watch_output(m0, ifp);
135 }
136 error = (*ifp->if_output)(ifp, m0,
137 (struct sockaddr *)dst, ro->ro_rt);
138 goto done;
139 } else {
140 ipxstat.ipxs_mtutoosmall++;
141 error = EMSGSIZE;
142 }
143 bad:
144 if (ipx_copy_output) {
145 ipx_watch_output(m0, ifp);
146 }
147 m_freem(m0);
148 done:
149 if (ro == &ipxroute && (flags & IPX_ROUTETOIF) == 0 &&
150 ro->ro_rt != NULL) {
151 RTFREE(ro->ro_rt);
152 ro->ro_rt = NULL;
153 }
154 return (error);
155 }
156
157 /*
158 * This will broadcast the type 20 (Netbios) packet to all the interfaces
159 * that have ipx configured and isn't in the list yet.
160 */
161 int
162 ipx_output_type20(m)
163 struct mbuf *m;
164 {
165 register struct ipx *ipx;
166 union ipx_net *nbnet = (union ipx_net *)(ipx + 1);
167 struct ipx_ifaddr *ia, *tia = NULL;
168 int error = 0;
169 struct mbuf *m1;
170 int i;
171 struct ifnet *ifp;
172 struct sockaddr_ipx dst;
173
174 /*
175 * We have to get to the 32 bytes after the ipx header also, so
176 * that we can fill in the network address of the receiving
177 * interface.
178 */
179 if ((m->m_flags & M_EXT || m->m_len < (sizeof(struct ipx) + 32)) &&
180 (m = m_pullup(m, sizeof(struct ipx) + 32)) == NULL) {
181 ipxstat.ipxs_toosmall++;
182 return (0);
183 }
184 ipx = mtod(m, struct ipx *);
185 nbnet = (union ipx_net *)(ipx + 1);
186
187 if (ipx->ipx_tc >= 8)
188 goto bad;
189 /*
190 * Now see if we have already seen this.
191 */
192 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
193 if(ia->ia_ifa.ifa_ifp == m->m_pkthdr.rcvif) {
194 if(tia == NULL)
195 tia = ia;
196
197 for (i=0;i<ipx->ipx_tc;i++,nbnet++)
198 if(ipx_neteqnn(ia->ia_addr.sipx_addr.x_net,
199 *nbnet))
200 goto bad;
201 }
202 /*
203 * Don't route the packet if the interface where it come from
204 * does not have an IPX address.
205 */
206 if(tia == NULL)
207 goto bad;
208
209 /*
210 * Add our receiving interface to the list.
211 */
212 nbnet = (union ipx_net *)(ipx + 1);
213 nbnet += ipx->ipx_tc;
214 *nbnet = tia->ia_addr.sipx_addr.x_net;
215
216 /*
217 * Increment the hop count.
218 */
219 ipx->ipx_tc++;
220 ipxstat.ipxs_forward++;
221
222 /*
223 * Send to all directly connected ifaces not in list and
224 * not to the one it came from.
225 */
226 m->m_flags &= ~M_BCAST;
227 bzero(&dst, sizeof(dst));
228 dst.sipx_family = AF_IPX;
229 dst.sipx_len = 12;
230 dst.sipx_addr.x_host = ipx_broadhost;
231
232 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
233 if(ia->ia_ifa.ifa_ifp != m->m_pkthdr.rcvif) {
234 nbnet = (union ipx_net *)(ipx + 1);
235 for (i=0;i<ipx->ipx_tc;i++,nbnet++)
236 if(ipx_neteqnn(ia->ia_addr.sipx_addr.x_net,
237 *nbnet))
238 goto skip_this;
239
240 /*
241 * Insert the net address of the dest net and
242 * calculate the new checksum if needed.
243 */
244 ifp = ia->ia_ifa.ifa_ifp;
245 dst.sipx_addr.x_net = ia->ia_addr.sipx_addr.x_net;
246 ipx->ipx_dna.x_net = dst.sipx_addr.x_net;
247 m1 = m_copym(m, 0, M_COPYALL, M_DONTWAIT);
248
249 if(ipx->ipx_sum != 0xffff) {
250 int len = ntohs(ipx->ipx_len);
251 ipx->ipx_sum = 0;
252 len = ((len - 1) | 1) + 1;
253 ipx->ipx_sum = ipx_cksum(m, len);
254 }
255 if(m1) {
256 error = (*ifp->if_output)(ifp, m1,
257 (struct sockaddr *)&dst, NULL);
258 /* XXX ipxstat.ipxs_localout++; */
259 }
260 skip_this:
261 }
262
263 bad:
264 m_freem(m);
265 return (error);
266 }
Cache object: a89ca8618b858738cb775aa1243da5ea
|