2 * Copyright (c) 2004-2005 Robert N. M. Watson
3 * Copyright (c) 1995, Mike Mitchell
4 * Copyright (c) 1984, 1985, 1986, 1987, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/kernel.h>
47 #include <sys/protosw.h>
48 #include <sys/signalvar.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/systm.h>
56 #include <net/route.h>
58 #include <netinet/in.h>
60 #include <netipx/ipx.h>
61 #include <netipx/ipx_if.h>
62 #include <netipx/ipx_ip.h>
63 #include <netipx/ipx_pcb.h>
64 #include <netipx/ipx_var.h>
67 * IPX protocol implementation.
70 static int ipxsendspace = IPXSNDQ;
71 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxsendspace, CTLFLAG_RW,
72 &ipxsendspace, 0, "");
73 static int ipxrecvspace = IPXRCVQ;
74 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxrecvspace, CTLFLAG_RW,
75 &ipxrecvspace, 0, "");
77 static int ipx_usr_abort(struct socket *so);
78 static int ipx_attach(struct socket *so, int proto, struct thread *td);
79 static int ipx_bind(struct socket *so, struct sockaddr *nam, struct thread *td);
80 static int ipx_connect(struct socket *so, struct sockaddr *nam,
82 static int ipx_detach(struct socket *so);
83 static int ipx_disconnect(struct socket *so);
84 static int ipx_send(struct socket *so, int flags, struct mbuf *m,
85 struct sockaddr *addr, struct mbuf *control,
87 static int ipx_shutdown(struct socket *so);
88 static int ripx_attach(struct socket *so, int proto, struct thread *td);
89 static int ipx_output(struct ipxpcb *ipxp, struct mbuf *m0);
91 struct pr_usrreqs ipx_usrreqs = {
92 .pru_abort = ipx_usr_abort,
93 .pru_attach = ipx_attach,
95 .pru_connect = ipx_connect,
96 .pru_control = ipx_control,
97 .pru_detach = ipx_detach,
98 .pru_disconnect = ipx_disconnect,
99 .pru_peeraddr = ipx_peeraddr,
100 .pru_send = ipx_send,
101 .pru_shutdown = ipx_shutdown,
102 .pru_sockaddr = ipx_sockaddr,
105 struct pr_usrreqs ripx_usrreqs = {
106 .pru_abort = ipx_usr_abort,
107 .pru_attach = ripx_attach,
108 .pru_bind = ipx_bind,
109 .pru_connect = ipx_connect,
110 .pru_control = ipx_control,
111 .pru_detach = ipx_detach,
112 .pru_disconnect = ipx_disconnect,
113 .pru_peeraddr = ipx_peeraddr,
114 .pru_send = ipx_send,
115 .pru_shutdown = ipx_shutdown,
116 .pru_sockaddr = ipx_sockaddr,
120 * This may also be called for raw listeners.
125 register struct ipxpcb *ipxp;
127 register struct ipx *ipx = mtod(m, struct ipx *);
128 struct ifnet *ifp = m->m_pkthdr.rcvif;
129 struct sockaddr_ipx ipx_ipx;
131 KASSERT(ipxp != NULL, ("ipx_input: NULL ipxpcb"));
132 IPX_LOCK_ASSERT(ipxp);
134 * Construct sockaddr format source address.
135 * Stuff source address and datagram in user buffer.
137 ipx_ipx.sipx_len = sizeof(ipx_ipx);
138 ipx_ipx.sipx_family = AF_IPX;
139 ipx_ipx.sipx_addr = ipx->ipx_sna;
140 ipx_ipx.sipx_zero[0] = '\0';
141 ipx_ipx.sipx_zero[1] = '\0';
142 if (ipx_neteqnn(ipx->ipx_sna.x_net, ipx_zeronet) && ifp != NULL) {
143 register struct ifaddr *ifa;
145 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa != NULL;
146 ifa = TAILQ_NEXT(ifa, ifa_link)) {
147 if (ifa->ifa_addr->sa_family == AF_IPX) {
148 ipx_ipx.sipx_addr.x_net =
149 IA_SIPX(ifa)->sipx_addr.x_net;
154 ipxp->ipxp_rpt = ipx->ipx_pt;
155 if ((ipxp->ipxp_flags & IPXP_RAWIN) == 0) {
156 m->m_len -= sizeof(struct ipx);
157 m->m_pkthdr.len -= sizeof(struct ipx);
158 m->m_data += sizeof(struct ipx);
160 if (sbappendaddr(&ipxp->ipxp_socket->so_rcv,
161 (struct sockaddr *)&ipx_ipx, m, NULL) == 0)
164 sorwakeup(ipxp->ipxp_socket);
168 * Drop connection, reporting
169 * the specified error.
172 ipx_drop(ipxp, errno)
173 register struct ipxpcb *ipxp;
176 struct socket *so = ipxp->ipxp_socket;
178 IPX_LIST_LOCK_ASSERT();
179 IPX_LOCK_ASSERT(ipxp);
182 * someday, in the IPX world
183 * we will generate error protocol packets
184 * announcing that the socket has gone away.
186 * XXX Probably never. IPX does not have error packets.
188 /*if (TCPS_HAVERCVDSYN(tp->t_state)) {
189 tp->t_state = TCPS_CLOSED;
192 so->so_error = errno;
193 ipx_pcbdisconnect(ipxp);
194 soisdisconnected(so);
202 register struct ipx *ipx;
203 register struct socket *so;
204 register int len = 0;
205 register struct route *ro;
207 struct mbuf *mprev = NULL;
209 IPX_LOCK_ASSERT(ipxp);
212 * Calculate data length.
214 for (m = m0; m != NULL; m = m->m_next) {
219 * Make sure packet is actually of even length.
224 if ((m->m_flags & M_EXT) == 0 &&
225 (m->m_len + m->m_data < &m->m_dat[MLEN])) {
226 mtod(m, char*)[m->m_len++] = 0;
228 struct mbuf *m1 = m_get(M_DONTWAIT, MT_DATA);
235 * mtod(m1, char *) = 0;
242 * Fill in mbuf with extended IPX header
243 * and addresses and length put into network format.
246 if (ipxp->ipxp_flags & IPXP_RAWOUT) {
247 ipx = mtod(m, struct ipx *);
249 M_PREPEND(m, sizeof(struct ipx), M_DONTWAIT);
252 ipx = mtod(m, struct ipx *);
254 ipx->ipx_pt = ipxp->ipxp_dpt;
255 ipx->ipx_sna = ipxp->ipxp_laddr;
256 ipx->ipx_dna = ipxp->ipxp_faddr;
257 len += sizeof(struct ipx);
260 ipx->ipx_len = htons((u_short)len);
262 if (ipxp->ipxp_flags & IPXP_CHECKSUM) {
263 ipx->ipx_sum = ipx_cksum(m, len);
265 ipx->ipx_sum = 0xffff;
270 so = ipxp->ipxp_socket;
271 if (so->so_options & SO_DONTROUTE)
272 return (ipx_outputfl(m, (struct route *)NULL,
273 (so->so_options & SO_BROADCAST) | IPX_ROUTETOIF));
275 * Use cached route for previous datagram if
276 * possible. If the previous net was the same
277 * and the interface was a broadcast medium, or
278 * if the previous destination was identical,
281 * NB: We don't handle broadcasts because that
282 * would require 3 subroutine calls.
284 ro = &ipxp->ipxp_route;
285 #ifdef ancient_history
287 * I think that this will all be handled in ipx_pcbconnect!
289 if (ro->ro_rt != NULL) {
290 if(ipx_neteq(ipxp->ipxp_lastdst, ipx->ipx_dna)) {
292 * This assumes we have no GH type routes
294 if (ro->ro_rt->rt_flags & RTF_HOST) {
295 if (!ipx_hosteq(ipxp->ipxp_lastdst, ipx->ipx_dna))
299 if ((ro->ro_rt->rt_flags & RTF_GATEWAY) == 0) {
300 register struct ipx_addr *dst =
301 &satoipx_addr(ro->ro_dst);
302 dst->x_host = ipx->ipx_dna.x_host;
305 * Otherwise, we go through the same gateway
306 * and dst is already set up.
314 ipxp->ipxp_lastdst = ipx->ipx_dna;
315 #endif /* ancient_history */
316 return (ipx_outputfl(m, ro, so->so_options & SO_BROADCAST));
320 ipx_ctloutput(so, sopt)
322 struct sockopt *sopt;
324 struct ipxpcb *ipxp = sotoipxpcb(so);
325 int mask, error, optval;
334 switch (sopt->sopt_dir) {
336 switch (sopt->sopt_name) {
338 mask = IPXP_ALL_PACKETS;
341 case SO_HEADERS_ON_INPUT:
345 case SO_IPX_CHECKSUM:
346 mask = IPXP_CHECKSUM;
349 case SO_HEADERS_ON_OUTPUT:
353 soptval = ipxp->ipxp_flags & mask;
354 error = sooptcopyout(sopt, &soptval, sizeof soptval);
357 case SO_DEFAULT_HEADERS:
362 ioptval.ipx_pt = ipxp->ipxp_dpt;
363 ioptval.ipx_dna = ipxp->ipxp_faddr;
364 ioptval.ipx_sna = ipxp->ipxp_laddr;
366 error = sooptcopyout(sopt, &soptval, sizeof soptval);
374 error = sooptcopyout(sopt, &seq, sizeof seq);
383 switch (sopt->sopt_name) {
385 mask = IPXP_ALL_PACKETS;
388 case SO_HEADERS_ON_INPUT:
392 case SO_IPX_CHECKSUM:
393 mask = IPXP_CHECKSUM;
395 case SO_HEADERS_ON_OUTPUT:
398 error = sooptcopyin(sopt, &optval, sizeof optval,
404 ipxp->ipxp_flags |= mask;
406 ipxp->ipxp_flags &= ~mask;
410 case SO_DEFAULT_HEADERS:
411 error = sooptcopyin(sopt, &ioptval, sizeof ioptval,
415 /* Unlocked write. */
416 ipxp->ipxp_dpt = ioptval.ipx_pt;
420 error = ipxip_route(so, sopt);
435 struct ipxpcb *ipxp = sotoipxpcb(so);
441 soisdisconnected(so);
449 ipx_attach(so, proto, td)
454 struct ipxpcb *ipxp = sotoipxpcb(so);
460 error = ipx_pcballoc(so, &ipxpcb_list, td);
463 error = soreserve(so, ipxsendspace, ipxrecvspace);
468 ipx_bind(so, nam, td)
470 struct sockaddr *nam;
473 struct ipxpcb *ipxp = sotoipxpcb(so);
478 error = ipx_pcbbind(ipxp, nam, td);
485 ipx_connect(so, nam, td)
487 struct sockaddr *nam;
490 struct ipxpcb *ipxp = sotoipxpcb(so);
495 if (!ipx_nullhost(ipxp->ipxp_faddr)) {
499 error = ipx_pcbconnect(ipxp, nam, td);
512 struct ipxpcb *ipxp = sotoipxpcb(so);
527 struct ipxpcb *ipxp = sotoipxpcb(so);
533 if (ipx_nullhost(ipxp->ipxp_faddr)) {
537 ipx_pcbdisconnect(ipxp);
538 soisdisconnected(so);
546 ipx_peeraddr(so, nam)
548 struct sockaddr **nam;
550 struct ipxpcb *ipxp = sotoipxpcb(so);
552 ipx_setpeeraddr(ipxp, nam);
557 ipx_send(so, flags, m, nam, control, td)
561 struct sockaddr *nam;
562 struct mbuf *control;
566 struct ipxpcb *ipxp = sotoipxpcb(so);
567 struct ipx_addr laddr;
570 * Attempt to only acquire the necessary locks: if the socket is
571 * already connected, we don't need to hold the IPX list lock to be
572 * used by ipx_pcbconnect() and ipx_pcbdisconnect(), just the IPX
578 laddr = ipxp->ipxp_laddr;
579 if (!ipx_nullhost(ipxp->ipxp_faddr)) {
586 * Must block input while temporarily connected.
588 error = ipx_pcbconnect(ipxp, nam, td);
596 if (ipx_nullhost(ipxp->ipxp_faddr)) {
602 error = ipx_output(ipxp, m);
605 ipx_pcbdisconnect(ipxp);
606 ipxp->ipxp_laddr = laddr;
627 ipx_sockaddr(so, nam)
629 struct sockaddr **nam;
631 struct ipxpcb *ipxp = sotoipxpcb(so);
633 ipx_setsockaddr(ipxp, nam);
638 ripx_attach(so, proto, td)
644 struct ipxpcb *ipxp = sotoipxpcb(so);
646 if (td != NULL && (error = suser(td)) != 0)
649 * We hold the IPX list lock for the duration as address parameters
650 * of the IPX pcb are changed. Since no one else holds a reference
651 * to the ipxpcb yet, we don't need the ipxpcb lock here.
654 error = ipx_pcballoc(so, &ipxrawpcb_list, td);
657 ipxp = sotoipxpcb(so);
658 error = soreserve(so, ipxsendspace, ipxrecvspace);
661 ipxp->ipxp_faddr.x_host = ipx_broadhost;
662 ipxp->ipxp_flags = IPXP_RAWIN | IPXP_RAWOUT;