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: releng/5.0/sys/netipx/ipx_outputfl.c 85958 2001-11-03 08:24:00Z peter $
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 static int ipx_copy_output = 0;
52
53 int
54 ipx_outputfl(m0, ro, flags)
55 struct mbuf *m0;
56 struct route *ro;
57 int flags;
58 {
59 register struct ipx *ipx = mtod(m0, struct ipx *);
60 register struct ifnet *ifp = NULL;
61 int error = 0;
62 struct sockaddr_ipx *dst;
63 struct route ipxroute;
64
65 /*
66 * Route packet.
67 */
68 if (ro == NULL) {
69 ro = &ipxroute;
70 bzero((caddr_t)ro, sizeof(*ro));
71 }
72 dst = (struct sockaddr_ipx *)&ro->ro_dst;
73 if (ro->ro_rt == NULL) {
74 dst->sipx_family = AF_IPX;
75 dst->sipx_len = sizeof(*dst);
76 dst->sipx_addr = ipx->ipx_dna;
77 dst->sipx_addr.x_port = 0;
78 /*
79 * If routing to interface only,
80 * short circuit routing lookup.
81 */
82 if (flags & IPX_ROUTETOIF) {
83 struct ipx_ifaddr *ia = ipx_iaonnetof(&ipx->ipx_dna);
84
85 if (ia == NULL) {
86 ipxstat.ipxs_noroute++;
87 error = ENETUNREACH;
88 goto bad;
89 }
90 ifp = ia->ia_ifp;
91 goto gotif;
92 }
93 rtalloc(ro);
94 } else if ((ro->ro_rt->rt_flags & RTF_UP) == 0) {
95 /*
96 * The old route has gone away; try for a new one.
97 */
98 rtfree(ro->ro_rt);
99 ro->ro_rt = NULL;
100 rtalloc(ro);
101 }
102 if (ro->ro_rt == NULL || (ifp = ro->ro_rt->rt_ifp) == NULL) {
103 ipxstat.ipxs_noroute++;
104 error = ENETUNREACH;
105 goto bad;
106 }
107 ro->ro_rt->rt_use++;
108 if (ro->ro_rt->rt_flags & (RTF_GATEWAY|RTF_HOST))
109 dst = (struct sockaddr_ipx *)ro->ro_rt->rt_gateway;
110 gotif:
111 /*
112 * Look for multicast addresses and
113 * and verify user is allowed to send
114 * such a packet.
115 */
116 if (dst->sipx_addr.x_host.c_host[0]&1) {
117 if ((ifp->if_flags & (IFF_BROADCAST | IFF_LOOPBACK)) == 0) {
118 error = EADDRNOTAVAIL;
119 goto bad;
120 }
121 if ((flags & IPX_ALLOWBROADCAST) == 0) {
122 error = EACCES;
123 goto bad;
124 }
125 m0->m_flags |= M_BCAST;
126 }
127
128 if (htons(ipx->ipx_len) <= ifp->if_mtu) {
129 ipxstat.ipxs_localout++;
130 if (ipx_copy_output) {
131 ipx_watch_output(m0, ifp);
132 }
133 error = (*ifp->if_output)(ifp, m0,
134 (struct sockaddr *)dst, ro->ro_rt);
135 goto done;
136 } else {
137 ipxstat.ipxs_mtutoosmall++;
138 error = EMSGSIZE;
139 }
140 bad:
141 if (ipx_copy_output) {
142 ipx_watch_output(m0, ifp);
143 }
144 m_freem(m0);
145 done:
146 if (ro == &ipxroute && (flags & IPX_ROUTETOIF) == 0 &&
147 ro->ro_rt != NULL) {
148 RTFREE(ro->ro_rt);
149 ro->ro_rt = NULL;
150 }
151 return (error);
152 }
153
154 /*
155 * This will broadcast the type 20 (Netbios) packet to all the interfaces
156 * that have ipx configured and isn't in the list yet.
157 */
158 int
159 ipx_output_type20(m)
160 struct mbuf *m;
161 {
162 register struct ipx *ipx;
163 union ipx_net *nbnet;
164 struct ipx_ifaddr *ia, *tia = NULL;
165 int error = 0;
166 struct mbuf *m1;
167 int i;
168 struct ifnet *ifp;
169 struct sockaddr_ipx dst;
170
171 /*
172 * We have to get to the 32 bytes after the ipx header also, so
173 * that we can fill in the network address of the receiving
174 * interface.
175 */
176 if ((m->m_flags & M_EXT || m->m_len < (sizeof(struct ipx) + 32)) &&
177 (m = m_pullup(m, sizeof(struct ipx) + 32)) == NULL) {
178 ipxstat.ipxs_toosmall++;
179 return (0);
180 }
181 ipx = mtod(m, struct ipx *);
182 nbnet = (union ipx_net *)(ipx + 1);
183
184 if (ipx->ipx_tc >= 8)
185 goto bad;
186 /*
187 * Now see if we have already seen this.
188 */
189 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
190 if(ia->ia_ifa.ifa_ifp == m->m_pkthdr.rcvif) {
191 if(tia == NULL)
192 tia = ia;
193
194 for (i=0;i<ipx->ipx_tc;i++,nbnet++)
195 if(ipx_neteqnn(ia->ia_addr.sipx_addr.x_net,
196 *nbnet))
197 goto bad;
198 }
199 /*
200 * Don't route the packet if the interface where it come from
201 * does not have an IPX address.
202 */
203 if(tia == NULL)
204 goto bad;
205
206 /*
207 * Add our receiving interface to the list.
208 */
209 nbnet = (union ipx_net *)(ipx + 1);
210 nbnet += ipx->ipx_tc;
211 *nbnet = tia->ia_addr.sipx_addr.x_net;
212
213 /*
214 * Increment the hop count.
215 */
216 ipx->ipx_tc++;
217 ipxstat.ipxs_forward++;
218
219 /*
220 * Send to all directly connected ifaces not in list and
221 * not to the one it came from.
222 */
223 m->m_flags &= ~M_BCAST;
224 bzero(&dst, sizeof(dst));
225 dst.sipx_family = AF_IPX;
226 dst.sipx_len = 12;
227 dst.sipx_addr.x_host = ipx_broadhost;
228
229 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
230 if(ia->ia_ifa.ifa_ifp != m->m_pkthdr.rcvif) {
231 nbnet = (union ipx_net *)(ipx + 1);
232 for (i=0;i<ipx->ipx_tc;i++,nbnet++)
233 if(ipx_neteqnn(ia->ia_addr.sipx_addr.x_net,
234 *nbnet))
235 goto skip_this;
236
237 /*
238 * Insert the net address of the dest net and
239 * calculate the new checksum if needed.
240 */
241 ifp = ia->ia_ifa.ifa_ifp;
242 dst.sipx_addr.x_net = ia->ia_addr.sipx_addr.x_net;
243 ipx->ipx_dna.x_net = dst.sipx_addr.x_net;
244 if(ipx->ipx_sum != 0xffff)
245 ipx->ipx_sum = ipx_cksum(m, ntohs(ipx->ipx_len));
246
247 m1 = m_copym(m, 0, M_COPYALL, M_DONTWAIT);
248 if(m1) {
249 error = (*ifp->if_output)(ifp, m1,
250 (struct sockaddr *)&dst, NULL);
251 /* XXX ipxstat.ipxs_localout++; */
252 }
253 skip_this: ;
254 }
255
256 bad:
257 m_freem(m);
258 return (error);
259 }
Cache object: 55a33d9370e986febb60b42fed593469
|