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