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