2 * Copyright (c) 1984, 1985, 1986, 1987, 1993
3 * The Regents of the University of California.
4 * Copyright (c) 2004-2006 Robert N. M. Watson
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 * 4. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Copyright (c) 1995, Mike Mitchell
32 * All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
70 #include <sys/param.h>
71 #include <sys/kernel.h>
75 #include <sys/protosw.h>
76 #include <sys/signalvar.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
80 #include <sys/sysctl.h>
81 #include <sys/systm.h>
84 #include <net/route.h>
86 #include <netinet/in.h>
88 #include <netipx/ipx.h>
89 #include <netipx/ipx_if.h>
90 #include <netipx/ipx_pcb.h>
91 #include <netipx/ipx_var.h>
93 #include <security/mac/mac_framework.h>
96 * IPX protocol implementation.
99 static int ipxsendspace = IPXSNDQ;
100 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxsendspace, CTLFLAG_RW,
101 &ipxsendspace, 0, "Send buffer space");
102 static int ipxrecvspace = IPXRCVQ;
103 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxrecvspace, CTLFLAG_RW,
104 &ipxrecvspace, 0, "Receive buffer space");
106 static void ipx_usr_abort(struct socket *so);
107 static int ipx_attach(struct socket *so, int proto, struct thread *td);
108 static int ipx_bind(struct socket *so, struct sockaddr *nam, struct thread *td);
109 static int ipx_connect(struct socket *so, struct sockaddr *nam,
111 static void ipx_detach(struct socket *so);
112 static int ipx_disconnect(struct socket *so);
113 static int ipx_send(struct socket *so, int flags, struct mbuf *m,
114 struct sockaddr *addr, struct mbuf *control,
116 static int ipx_shutdown(struct socket *so);
117 static int ripx_attach(struct socket *so, int proto, struct thread *td);
118 static int ipx_output(struct ipxpcb *ipxp, struct mbuf *m0);
119 static void ipx_usr_close(struct socket *so);
121 struct pr_usrreqs ipx_usrreqs = {
122 .pru_abort = ipx_usr_abort,
123 .pru_attach = ipx_attach,
124 .pru_bind = ipx_bind,
125 .pru_connect = ipx_connect,
126 .pru_control = ipx_control,
127 .pru_detach = ipx_detach,
128 .pru_disconnect = ipx_disconnect,
129 .pru_peeraddr = ipx_peeraddr,
130 .pru_send = ipx_send,
131 .pru_shutdown = ipx_shutdown,
132 .pru_sockaddr = ipx_sockaddr,
133 .pru_close = ipx_usr_close,
136 struct pr_usrreqs ripx_usrreqs = {
137 .pru_abort = ipx_usr_abort,
138 .pru_attach = ripx_attach,
139 .pru_bind = ipx_bind,
140 .pru_connect = ipx_connect,
141 .pru_control = ipx_control,
142 .pru_detach = ipx_detach,
143 .pru_disconnect = ipx_disconnect,
144 .pru_peeraddr = ipx_peeraddr,
145 .pru_send = ipx_send,
146 .pru_shutdown = ipx_shutdown,
147 .pru_sockaddr = ipx_sockaddr,
148 .pru_close = ipx_usr_close,
152 * This may also be called for raw listeners.
155 ipx_input(struct mbuf *m, struct ipxpcb *ipxp)
157 struct ipx *ipx = mtod(m, struct ipx *);
158 struct ifnet *ifp = m->m_pkthdr.rcvif;
159 struct sockaddr_ipx ipx_ipx;
161 KASSERT(ipxp != NULL, ("ipx_input: NULL ipxpcb"));
162 IPX_LOCK_ASSERT(ipxp);
164 * Construct sockaddr format source address.
165 * Stuff source address and datagram in user buffer.
167 ipx_ipx.sipx_len = sizeof(ipx_ipx);
168 ipx_ipx.sipx_family = AF_IPX;
169 ipx_ipx.sipx_addr = ipx->ipx_sna;
170 ipx_ipx.sipx_zero[0] = '\0';
171 ipx_ipx.sipx_zero[1] = '\0';
172 if (ipx_neteqnn(ipx->ipx_sna.x_net, ipx_zeronet) && ifp != NULL) {
175 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa != NULL;
176 ifa = TAILQ_NEXT(ifa, ifa_link)) {
177 if (ifa->ifa_addr->sa_family == AF_IPX) {
178 ipx_ipx.sipx_addr.x_net =
179 IA_SIPX(ifa)->sipx_addr.x_net;
184 ipxp->ipxp_rpt = ipx->ipx_pt;
185 if ((ipxp->ipxp_flags & IPXP_RAWIN) == 0) {
186 m->m_len -= sizeof(struct ipx);
187 m->m_pkthdr.len -= sizeof(struct ipx);
188 m->m_data += sizeof(struct ipx);
190 if (sbappendaddr(&ipxp->ipxp_socket->so_rcv,
191 (struct sockaddr *)&ipx_ipx, m, NULL) == 0)
194 sorwakeup(ipxp->ipxp_socket);
198 * Drop connection, reporting
199 * the specified error.
202 ipx_drop(struct ipxpcb *ipxp, int errno)
204 struct socket *so = ipxp->ipxp_socket;
206 IPX_LIST_LOCK_ASSERT();
207 IPX_LOCK_ASSERT(ipxp);
210 * someday, in the IPX world
211 * we will generate error protocol packets
212 * announcing that the socket has gone away.
214 * XXX Probably never. IPX does not have error packets.
216 /*if (TCPS_HAVERCVDSYN(tp->t_state)) {
217 tp->t_state = TCPS_CLOSED;
220 so->so_error = errno;
221 ipx_pcbdisconnect(ipxp);
222 soisdisconnected(so);
226 ipx_output(struct ipxpcb *ipxp, struct mbuf *m0)
233 struct mbuf *mprev = NULL;
235 IPX_LOCK_ASSERT(ipxp);
238 * Calculate data length.
240 for (m = m0; m != NULL; m = m->m_next) {
245 * Make sure packet is actually of even length.
250 if ((m->m_flags & M_EXT) == 0 &&
251 (m->m_len + m->m_data < &m->m_dat[MLEN])) {
252 mtod(m, char*)[m->m_len++] = 0;
254 struct mbuf *m1 = m_get(M_DONTWAIT, MT_DATA);
261 * mtod(m1, char *) = 0;
268 * Fill in mbuf with extended IPX header
269 * and addresses and length put into network format.
272 if (ipxp->ipxp_flags & IPXP_RAWOUT) {
273 ipx = mtod(m, struct ipx *);
275 M_PREPEND(m, sizeof(struct ipx), M_DONTWAIT);
278 ipx = mtod(m, struct ipx *);
280 ipx->ipx_pt = ipxp->ipxp_dpt;
281 ipx->ipx_sna = ipxp->ipxp_laddr;
282 ipx->ipx_dna = ipxp->ipxp_faddr;
283 len += sizeof(struct ipx);
286 ipx->ipx_len = htons((u_short)len);
288 if (ipxp->ipxp_flags & IPXP_CHECKSUM) {
289 ipx->ipx_sum = ipx_cksum(m, len);
291 ipx->ipx_sum = 0xffff;
296 so = ipxp->ipxp_socket;
297 if (so->so_options & SO_DONTROUTE)
298 return (ipx_outputfl(m, (struct route *)NULL,
299 (so->so_options & SO_BROADCAST) | IPX_ROUTETOIF));
301 * Use cached route for previous datagram if
302 * possible. If the previous net was the same
303 * and the interface was a broadcast medium, or
304 * if the previous destination was identical,
307 * NB: We don't handle broadcasts because that
308 * would require 3 subroutine calls.
310 ro = &ipxp->ipxp_route;
311 #ifdef ancient_history
313 * I think that this will all be handled in ipx_pcbconnect!
315 if (ro->ro_rt != NULL) {
316 if(ipx_neteq(ipxp->ipxp_lastdst, ipx->ipx_dna)) {
318 * This assumes we have no GH type routes
320 if (ro->ro_rt->rt_flags & RTF_HOST) {
321 if (!ipx_hosteq(ipxp->ipxp_lastdst, ipx->ipx_dna))
325 if ((ro->ro_rt->rt_flags & RTF_GATEWAY) == 0) {
326 struct ipx_addr *dst =
327 &satoipx_addr(ro->ro_dst);
328 dst->x_host = ipx->ipx_dna.x_host;
331 * Otherwise, we go through the same gateway
332 * and dst is already set up.
340 ipxp->ipxp_lastdst = ipx->ipx_dna;
341 #endif /* ancient_history */
342 return (ipx_outputfl(m, ro, so->so_options & SO_BROADCAST));
346 ipx_ctloutput(struct socket *so, struct sockopt *sopt)
348 struct ipxpcb *ipxp = sotoipxpcb(so);
349 int mask, error, optval;
354 KASSERT(ipxp != NULL, ("ipx_ctloutput: ipxp == NULL"));
357 switch (sopt->sopt_dir) {
359 switch (sopt->sopt_name) {
361 mask = IPXP_ALL_PACKETS;
364 case SO_HEADERS_ON_INPUT:
368 case SO_IPX_CHECKSUM:
369 mask = IPXP_CHECKSUM;
372 case SO_HEADERS_ON_OUTPUT:
376 soptval = ipxp->ipxp_flags & mask;
377 error = sooptcopyout(sopt, &soptval, sizeof soptval);
380 case SO_DEFAULT_HEADERS:
385 ioptval.ipx_pt = ipxp->ipxp_dpt;
386 ioptval.ipx_dna = ipxp->ipxp_faddr;
387 ioptval.ipx_sna = ipxp->ipxp_laddr;
389 error = sooptcopyout(sopt, &soptval, sizeof soptval);
397 error = sooptcopyout(sopt, &seq, sizeof seq);
406 switch (sopt->sopt_name) {
408 mask = IPXP_ALL_PACKETS;
411 case SO_HEADERS_ON_INPUT:
415 case SO_IPX_CHECKSUM:
416 mask = IPXP_CHECKSUM;
419 case SO_HEADERS_ON_OUTPUT:
422 error = sooptcopyin(sopt, &optval, sizeof optval,
428 ipxp->ipxp_flags |= mask;
430 ipxp->ipxp_flags &= ~mask;
434 case SO_DEFAULT_HEADERS:
435 error = sooptcopyin(sopt, &ioptval, sizeof ioptval,
439 /* Unlocked write. */
440 ipxp->ipxp_dpt = ioptval.ipx_pt;
451 ipx_usr_abort(struct socket *so)
454 /* XXXRW: Possibly ipx_disconnect() here? */
455 soisdisconnected(so);
459 ipx_attach(struct socket *so, int proto, struct thread *td)
462 struct ipxpcb *ipxp = sotoipxpcb(so);
466 KASSERT(ipxp == NULL, ("ipx_attach: ipxp != NULL"));
467 error = soreserve(so, ipxsendspace, ipxrecvspace);
471 error = ipx_pcballoc(so, &ipxpcb_list, td);
477 ipx_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
479 struct ipxpcb *ipxp = sotoipxpcb(so);
482 KASSERT(ipxp != NULL, ("ipx_bind: ipxp == NULL"));
485 error = ipx_pcbbind(ipxp, nam, td);
492 ipx_usr_close(struct socket *so)
495 /* XXXRW: Possibly ipx_disconnect() here? */
496 soisdisconnected(so);
500 ipx_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
502 struct ipxpcb *ipxp = sotoipxpcb(so);
505 KASSERT(ipxp != NULL, ("ipx_connect: ipxp == NULL"));
508 if (!ipx_nullhost(ipxp->ipxp_faddr)) {
512 error = ipx_pcbconnect(ipxp, nam, td);
522 ipx_detach(struct socket *so)
524 struct ipxpcb *ipxp = sotoipxpcb(so);
526 /* XXXRW: Should assert detached. */
527 KASSERT(ipxp != NULL, ("ipx_detach: ipxp == NULL"));
536 ipx_disconnect(struct socket *so)
538 struct ipxpcb *ipxp = sotoipxpcb(so);
541 KASSERT(ipxp != NULL, ("ipx_disconnect: ipxp == NULL"));
545 if (ipx_nullhost(ipxp->ipxp_faddr)) {
549 ipx_pcbdisconnect(ipxp);
550 soisdisconnected(so);
558 ipx_peeraddr(struct socket *so, struct sockaddr **nam)
560 struct ipxpcb *ipxp = sotoipxpcb(so);
562 KASSERT(ipxp != NULL, ("ipx_peeraddr: ipxp == NULL"));
563 ipx_getpeeraddr(ipxp, nam);
568 ipx_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
569 struct mbuf *control, struct thread *td)
572 struct ipxpcb *ipxp = sotoipxpcb(so);
573 struct ipx_addr laddr;
575 KASSERT(ipxp != NULL, ("ipxp_send: ipxp == NULL"));
577 * Attempt to only acquire the necessary locks: if the socket is
578 * already connected, we don't need to hold the IPX list lock to be
579 * used by ipx_pcbconnect() and ipx_pcbdisconnect(), just the IPX
583 mac_socket_create_mbuf(so, m);
588 laddr = ipxp->ipxp_laddr;
589 if (!ipx_nullhost(ipxp->ipxp_faddr)) {
596 * Must block input while temporarily connected.
598 error = ipx_pcbconnect(ipxp, nam, td);
606 if (ipx_nullhost(ipxp->ipxp_faddr)) {
612 error = ipx_output(ipxp, m);
615 ipx_pcbdisconnect(ipxp);
616 ipxp->ipxp_laddr = laddr;
633 KASSERT(so->so_pcb != NULL, ("ipx_shutdown: so_pcb == NULL"));
639 ipx_sockaddr(struct socket *so, struct sockaddr **nam)
641 struct ipxpcb *ipxp = sotoipxpcb(so);
643 KASSERT(ipxp != NULL, ("ipx_sockaddr: ipxp == NULL"));
644 ipx_getsockaddr(ipxp, nam);
649 ripx_attach(struct socket *so, int proto, struct thread *td)
652 struct ipxpcb *ipxp = sotoipxpcb(so);
654 KASSERT(ipxp == NULL, ("ripx_attach: ipxp != NULL"));
657 error = priv_check(td, PRIV_NETIPX_RAW);
663 * We hold the IPX list lock for the duration as address parameters
664 * of the IPX pcb are changed. Since no one else holds a reference
665 * to the ipxpcb yet, we don't need the ipxpcb lock here.
668 error = ipx_pcballoc(so, &ipxrawpcb_list, td);
671 ipxp = sotoipxpcb(so);
672 error = soreserve(so, ipxsendspace, ipxrecvspace);
675 ipxp->ipxp_faddr.x_host = ipx_broadhost;
676 ipxp->ipxp_flags = IPXP_RAWIN | IPXP_RAWOUT;