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