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