]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netipx/ipx_outputfl.c
This commit was generated by cvs2svn to compensate for changes in r168616,
[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/route.h>
72
73 #include <netipx/ipx.h>
74 #include <netipx/ipx_if.h>
75 #include <netipx/ipx_var.h>
76
77 static int ipx_copy_output = 0;
78
79 int
80 ipx_outputfl(m0, ro, flags)
81         struct mbuf *m0;
82         struct route *ro;
83         int flags;
84 {
85         register struct ipx *ipx = mtod(m0, struct ipx *);
86         register struct ifnet *ifp = NULL;
87         int error = 0;
88         struct sockaddr_ipx *dst;
89         struct route ipxroute;
90
91         /*
92          * Route packet.
93          */
94         if (ro == NULL) {
95                 ro = &ipxroute;
96                 bzero((caddr_t)ro, sizeof(*ro));
97         }
98         dst = (struct sockaddr_ipx *)&ro->ro_dst;
99         if (ro->ro_rt == NULL) {
100                 dst->sipx_family = AF_IPX;
101                 dst->sipx_len = sizeof(*dst);
102                 dst->sipx_addr = ipx->ipx_dna;
103                 dst->sipx_addr.x_port = 0;
104                 /*
105                  * If routing to interface only,
106                  * short circuit routing lookup.
107                  */
108                 if (flags & IPX_ROUTETOIF) {
109                         struct ipx_ifaddr *ia = ipx_iaonnetof(&ipx->ipx_dna);
110
111                         if (ia == NULL) {
112                                 ipxstat.ipxs_noroute++;
113                                 error = ENETUNREACH;
114                                 goto bad;
115                         }
116                         ifp = ia->ia_ifp;
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                 if (ipx_copy_output) {
157                         ipx_watch_output(m0, ifp);
158                 }
159                 error = (*ifp->if_output)(ifp, m0,
160                                         (struct sockaddr *)dst, ro->ro_rt);
161                 goto done;
162         } else {
163                 ipxstat.ipxs_mtutoosmall++;
164                 error = EMSGSIZE;
165         }
166 bad:
167         if (ipx_copy_output) {
168                 ipx_watch_output(m0, ifp);
169         }
170         m_freem(m0);
171 done:
172         if (ro == &ipxroute && (flags & IPX_ROUTETOIF) == 0 &&
173             ro->ro_rt != NULL) {
174                 RTFREE(ro->ro_rt);
175                 ro->ro_rt = NULL;
176         }
177         return (error);
178 }
179
180 /*
181  * This will broadcast the type 20 (Netbios) packet to all the interfaces
182  * that have ipx configured and isn't in the list yet.
183  */
184 int
185 ipx_output_type20(m)
186         struct mbuf *m;
187 {
188         register struct ipx *ipx;
189         union ipx_net *nbnet;
190         struct ipx_ifaddr *ia, *tia = NULL;
191         int error = 0;
192         struct mbuf *m1;
193         int i;
194         struct ifnet *ifp;
195         struct sockaddr_ipx dst;
196
197         /*
198          * We have to get to the 32 bytes after the ipx header also, so
199          * that we can fill in the network address of the receiving
200          * interface.
201          */
202         if ((m->m_flags & M_EXT || m->m_len < (sizeof(struct ipx) + 32)) &&
203             (m = m_pullup(m, sizeof(struct ipx) + 32)) == NULL) {
204                 ipxstat.ipxs_toosmall++;
205                 return (0);
206         }
207         ipx = mtod(m, struct ipx *);
208         nbnet = (union ipx_net *)(ipx + 1);
209
210         if (ipx->ipx_tc >= 8)
211                 goto bad;
212         /*
213          * Now see if we have already seen this.
214          */
215         for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
216                 if(ia->ia_ifa.ifa_ifp == m->m_pkthdr.rcvif) {
217                         if(tia == NULL)
218                                 tia = ia;
219
220                         for (i=0;i<ipx->ipx_tc;i++,nbnet++)
221                                 if(ipx_neteqnn(ia->ia_addr.sipx_addr.x_net,
222                                                         *nbnet))
223                                         goto bad;
224                 }
225         /*
226          * Don't route the packet if the interface where it come from
227          * does not have an IPX address.
228          */
229         if(tia == NULL)
230                 goto bad;
231
232         /*
233          * Add our receiving interface to the list.
234          */
235         nbnet = (union ipx_net *)(ipx + 1);
236         nbnet += ipx->ipx_tc;
237         *nbnet = tia->ia_addr.sipx_addr.x_net;
238
239         /*
240          * Increment the hop count.
241          */
242         ipx->ipx_tc++;
243         ipxstat.ipxs_forward++;
244
245         /*
246          * Send to all directly connected ifaces not in list and
247          * not to the one it came from.
248          */
249         m->m_flags &= ~M_BCAST;
250         bzero(&dst, sizeof(dst));
251         dst.sipx_family = AF_IPX;
252         dst.sipx_len = 12;
253         dst.sipx_addr.x_host = ipx_broadhost;
254
255         for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
256                 if(ia->ia_ifa.ifa_ifp != m->m_pkthdr.rcvif) {
257                         nbnet = (union ipx_net *)(ipx + 1);
258                         for (i=0;i<ipx->ipx_tc;i++,nbnet++)
259                                 if(ipx_neteqnn(ia->ia_addr.sipx_addr.x_net,
260                                                         *nbnet))
261                                         goto skip_this;
262
263                         /*
264                          * Insert the net address of the dest net and
265                          * calculate the new checksum if needed.
266                          */
267                         ifp = ia->ia_ifa.ifa_ifp;
268                         dst.sipx_addr.x_net = ia->ia_addr.sipx_addr.x_net;
269                         ipx->ipx_dna.x_net = dst.sipx_addr.x_net;
270                         if(ipx->ipx_sum != 0xffff)
271                                 ipx->ipx_sum = ipx_cksum(m, ntohs(ipx->ipx_len));
272
273                         m1 = m_copym(m, 0, M_COPYALL, M_DONTWAIT);
274                         if(m1) {
275                                 error = (*ifp->if_output)(ifp, m1,
276                                         (struct sockaddr *)&dst, NULL);
277                                 /* XXX ipxstat.ipxs_localout++; */
278                         }
279 skip_this: ;
280                 }
281
282 bad:
283         m_freem(m);
284         return (error);
285 }