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/if_var.h>
85 #include <net/route.h>
87 #include <netinet/in.h>
89 #include <netipx/ipx.h>
90 #include <netipx/ipx_if.h>
91 #include <netipx/ipx_pcb.h>
92 #include <netipx/ipx_var.h>
94 #include <security/mac/mac_framework.h>
97 * IPX protocol implementation.
100 static int ipxsendspace = IPXSNDQ;
101 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxsendspace, CTLFLAG_RW,
102 &ipxsendspace, 0, "Send buffer space");
103 static int ipxrecvspace = IPXRCVQ;
104 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxrecvspace, CTLFLAG_RW,
105 &ipxrecvspace, 0, "Receive buffer space");
107 static void ipx_usr_abort(struct socket *so);
108 static int ipx_attach(struct socket *so, int proto, struct thread *td);
109 static int ipx_bind(struct socket *so, struct sockaddr *nam, struct thread *td);
110 static int ipx_connect(struct socket *so, struct sockaddr *nam,
112 static void ipx_detach(struct socket *so);
113 static int ipx_disconnect(struct socket *so);
114 static int ipx_send(struct socket *so, int flags, struct mbuf *m,
115 struct sockaddr *addr, struct mbuf *control,
117 static int ipx_shutdown(struct socket *so);
118 static int ripx_attach(struct socket *so, int proto, struct thread *td);
119 static int ipx_output(struct ipxpcb *ipxp, struct mbuf *m0);
120 static void ipx_usr_close(struct socket *so);
122 struct pr_usrreqs ipx_usrreqs = {
123 .pru_abort = ipx_usr_abort,
124 .pru_attach = ipx_attach,
125 .pru_bind = ipx_bind,
126 .pru_connect = ipx_connect,
127 .pru_control = ipx_control,
128 .pru_detach = ipx_detach,
129 .pru_disconnect = ipx_disconnect,
130 .pru_peeraddr = ipx_peeraddr,
131 .pru_send = ipx_send,
132 .pru_shutdown = ipx_shutdown,
133 .pru_sockaddr = ipx_sockaddr,
134 .pru_close = ipx_usr_close,
137 struct pr_usrreqs ripx_usrreqs = {
138 .pru_abort = ipx_usr_abort,
139 .pru_attach = ripx_attach,
140 .pru_bind = ipx_bind,
141 .pru_connect = ipx_connect,
142 .pru_control = ipx_control,
143 .pru_detach = ipx_detach,
144 .pru_disconnect = ipx_disconnect,
145 .pru_peeraddr = ipx_peeraddr,
146 .pru_send = ipx_send,
147 .pru_shutdown = ipx_shutdown,
148 .pru_sockaddr = ipx_sockaddr,
149 .pru_close = ipx_usr_close,
153 * This may also be called for raw listeners.
156 ipx_input(struct mbuf *m, struct ipxpcb *ipxp)
158 struct ipx *ipx = mtod(m, struct ipx *);
159 struct ifnet *ifp = m->m_pkthdr.rcvif;
160 struct sockaddr_ipx ipx_ipx;
162 KASSERT(ipxp != NULL, ("ipx_input: NULL ipxpcb"));
163 IPX_LOCK_ASSERT(ipxp);
165 * Construct sockaddr format source address.
166 * Stuff source address and datagram in user buffer.
168 ipx_ipx.sipx_len = sizeof(ipx_ipx);
169 ipx_ipx.sipx_family = AF_IPX;
170 ipx_ipx.sipx_addr = ipx->ipx_sna;
171 ipx_ipx.sipx_zero[0] = '\0';
172 ipx_ipx.sipx_zero[1] = '\0';
173 if (ipx_neteqnn(ipx->ipx_sna.x_net, ipx_zeronet) && ifp != NULL) {
176 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa != NULL;
177 ifa = TAILQ_NEXT(ifa, ifa_link)) {
178 if (ifa->ifa_addr->sa_family == AF_IPX) {
179 ipx_ipx.sipx_addr.x_net =
180 IA_SIPX(ifa)->sipx_addr.x_net;
185 ipxp->ipxp_rpt = ipx->ipx_pt;
186 if ((ipxp->ipxp_flags & IPXP_RAWIN) == 0) {
187 m->m_len -= sizeof(struct ipx);
188 m->m_pkthdr.len -= sizeof(struct ipx);
189 m->m_data += sizeof(struct ipx);
192 if (mac_socket_check_deliver(ipxp->ipxp_socket, m) != 0) {
197 if (sbappendaddr(&ipxp->ipxp_socket->so_rcv,
198 (struct sockaddr *)&ipx_ipx, m, NULL) == 0)
201 sorwakeup(ipxp->ipxp_socket);
205 * Drop connection, reporting
206 * the specified error.
209 ipx_drop(struct ipxpcb *ipxp, int errno)
211 struct socket *so = ipxp->ipxp_socket;
213 IPX_LIST_LOCK_ASSERT();
214 IPX_LOCK_ASSERT(ipxp);
217 * someday, in the IPX world
218 * we will generate error protocol packets
219 * announcing that the socket has gone away.
221 * XXX Probably never. IPX does not have error packets.
223 /*if (TCPS_HAVERCVDSYN(tp->t_state)) {
224 tp->t_state = TCPS_CLOSED;
227 so->so_error = errno;
228 ipx_pcbdisconnect(ipxp);
229 soisdisconnected(so);
233 ipx_output(struct ipxpcb *ipxp, struct mbuf *m0)
240 struct mbuf *mprev = NULL;
242 IPX_LOCK_ASSERT(ipxp);
245 * Calculate data length.
247 for (m = m0; m != NULL; m = m->m_next) {
252 * Make sure packet is actually of even length.
257 if ((m->m_flags & M_EXT) == 0 &&
258 (m->m_len + m->m_data < &m->m_dat[MLEN])) {
259 mtod(m, char*)[m->m_len++] = 0;
261 struct mbuf *m1 = m_get(M_NOWAIT, MT_DATA);
268 * mtod(m1, char *) = 0;
275 * Fill in mbuf with extended IPX header
276 * and addresses and length put into network format.
279 if (ipxp->ipxp_flags & IPXP_RAWOUT) {
280 ipx = mtod(m, struct ipx *);
282 M_PREPEND(m, sizeof(struct ipx), M_NOWAIT);
285 ipx = mtod(m, struct ipx *);
287 ipx->ipx_pt = ipxp->ipxp_dpt;
288 ipx->ipx_sna = ipxp->ipxp_laddr;
289 ipx->ipx_dna = ipxp->ipxp_faddr;
290 len += sizeof(struct ipx);
293 ipx->ipx_len = htons((u_short)len);
295 if (ipxp->ipxp_flags & IPXP_CHECKSUM) {
296 ipx->ipx_sum = ipx_cksum(m, len);
298 ipx->ipx_sum = 0xffff;
303 so = ipxp->ipxp_socket;
304 if (so->so_options & SO_DONTROUTE)
305 return (ipx_outputfl(m, (struct route *)NULL,
306 (so->so_options & SO_BROADCAST) | IPX_ROUTETOIF));
308 * Use cached route for previous datagram if
309 * possible. If the previous net was the same
310 * and the interface was a broadcast medium, or
311 * if the previous destination was identical,
314 * NB: We don't handle broadcasts because that
315 * would require 3 subroutine calls.
317 ro = &ipxp->ipxp_route;
318 #ifdef ancient_history
320 * I think that this will all be handled in ipx_pcbconnect!
322 if (ro->ro_rt != NULL) {
323 if(ipx_neteq(ipxp->ipxp_lastdst, ipx->ipx_dna)) {
325 * This assumes we have no GH type routes
327 if (ro->ro_rt->rt_flags & RTF_HOST) {
328 if (!ipx_hosteq(ipxp->ipxp_lastdst, ipx->ipx_dna))
332 if ((ro->ro_rt->rt_flags & RTF_GATEWAY) == 0) {
333 struct ipx_addr *dst =
334 &satoipx_addr(ro->ro_dst);
335 dst->x_host = ipx->ipx_dna.x_host;
338 * Otherwise, we go through the same gateway
339 * and dst is already set up.
347 ipxp->ipxp_lastdst = ipx->ipx_dna;
348 #endif /* ancient_history */
349 return (ipx_outputfl(m, ro, so->so_options & SO_BROADCAST));
353 ipx_ctloutput(struct socket *so, struct sockopt *sopt)
355 struct ipxpcb *ipxp = sotoipxpcb(so);
356 int mask, error, optval;
361 KASSERT(ipxp != NULL, ("ipx_ctloutput: ipxp == NULL"));
364 switch (sopt->sopt_dir) {
366 switch (sopt->sopt_name) {
368 mask = IPXP_ALL_PACKETS;
371 case SO_HEADERS_ON_INPUT:
375 case SO_IPX_CHECKSUM:
376 mask = IPXP_CHECKSUM;
379 case SO_HEADERS_ON_OUTPUT:
383 soptval = ipxp->ipxp_flags & mask;
384 error = sooptcopyout(sopt, &soptval, sizeof soptval);
387 case SO_DEFAULT_HEADERS:
392 ioptval.ipx_pt = ipxp->ipxp_dpt;
393 ioptval.ipx_dna = ipxp->ipxp_faddr;
394 ioptval.ipx_sna = ipxp->ipxp_laddr;
396 error = sooptcopyout(sopt, &soptval, sizeof soptval);
404 error = sooptcopyout(sopt, &seq, sizeof seq);
413 switch (sopt->sopt_name) {
415 mask = IPXP_ALL_PACKETS;
418 case SO_HEADERS_ON_INPUT:
422 case SO_IPX_CHECKSUM:
423 mask = IPXP_CHECKSUM;
426 case SO_HEADERS_ON_OUTPUT:
429 error = sooptcopyin(sopt, &optval, sizeof optval,
435 ipxp->ipxp_flags |= mask;
437 ipxp->ipxp_flags &= ~mask;
441 case SO_DEFAULT_HEADERS:
442 error = sooptcopyin(sopt, &ioptval, sizeof ioptval,
446 /* Unlocked write. */
447 ipxp->ipxp_dpt = ioptval.ipx_pt;
458 ipx_usr_abort(struct socket *so)
461 /* XXXRW: Possibly ipx_disconnect() here? */
462 soisdisconnected(so);
466 ipx_attach(struct socket *so, int proto, struct thread *td)
469 struct ipxpcb *ipxp = sotoipxpcb(so);
473 KASSERT(ipxp == NULL, ("ipx_attach: ipxp != NULL"));
474 error = soreserve(so, ipxsendspace, ipxrecvspace);
478 error = ipx_pcballoc(so, &ipxpcb_list, td);
484 ipx_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
486 struct ipxpcb *ipxp = sotoipxpcb(so);
489 KASSERT(ipxp != NULL, ("ipx_bind: ipxp == NULL"));
492 error = ipx_pcbbind(ipxp, nam, td);
499 ipx_usr_close(struct socket *so)
502 /* XXXRW: Possibly ipx_disconnect() here? */
503 soisdisconnected(so);
507 ipx_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
509 struct ipxpcb *ipxp = sotoipxpcb(so);
512 KASSERT(ipxp != NULL, ("ipx_connect: ipxp == NULL"));
515 if (!ipx_nullhost(ipxp->ipxp_faddr)) {
519 error = ipx_pcbconnect(ipxp, nam, td);
529 ipx_detach(struct socket *so)
531 struct ipxpcb *ipxp = sotoipxpcb(so);
533 /* XXXRW: Should assert detached. */
534 KASSERT(ipxp != NULL, ("ipx_detach: ipxp == NULL"));
543 ipx_disconnect(struct socket *so)
545 struct ipxpcb *ipxp = sotoipxpcb(so);
548 KASSERT(ipxp != NULL, ("ipx_disconnect: ipxp == NULL"));
552 if (ipx_nullhost(ipxp->ipxp_faddr)) {
556 ipx_pcbdisconnect(ipxp);
557 soisdisconnected(so);
565 ipx_peeraddr(struct socket *so, struct sockaddr **nam)
567 struct ipxpcb *ipxp = sotoipxpcb(so);
569 KASSERT(ipxp != NULL, ("ipx_peeraddr: ipxp == NULL"));
570 ipx_getpeeraddr(ipxp, nam);
575 ipx_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
576 struct mbuf *control, struct thread *td)
579 struct ipxpcb *ipxp = sotoipxpcb(so);
580 struct ipx_addr laddr;
582 KASSERT(ipxp != NULL, ("ipxp_send: ipxp == NULL"));
584 * Attempt to only acquire the necessary locks: if the socket is
585 * already connected, we don't need to hold the IPX list lock to be
586 * used by ipx_pcbconnect() and ipx_pcbdisconnect(), just the IPX
590 mac_socket_create_mbuf(so, m);
595 laddr = ipxp->ipxp_laddr;
596 if (!ipx_nullhost(ipxp->ipxp_faddr)) {
603 * Must block input while temporarily connected.
605 error = ipx_pcbconnect(ipxp, nam, td);
613 if (ipx_nullhost(ipxp->ipxp_faddr)) {
619 error = ipx_output(ipxp, m);
622 ipx_pcbdisconnect(ipxp);
623 ipxp->ipxp_laddr = laddr;
640 KASSERT(so->so_pcb != NULL, ("ipx_shutdown: so_pcb == NULL"));
646 ipx_sockaddr(struct socket *so, struct sockaddr **nam)
648 struct ipxpcb *ipxp = sotoipxpcb(so);
650 KASSERT(ipxp != NULL, ("ipx_sockaddr: ipxp == NULL"));
651 ipx_getsockaddr(ipxp, nam);
656 ripx_attach(struct socket *so, int proto, struct thread *td)
659 struct ipxpcb *ipxp = sotoipxpcb(so);
661 KASSERT(ipxp == NULL, ("ripx_attach: ipxp != NULL"));
664 error = priv_check(td, PRIV_NETIPX_RAW);
670 * We hold the IPX list lock for the duration as address parameters
671 * of the IPX pcb are changed. Since no one else holds a reference
672 * to the ipxpcb yet, we don't need the ipxpcb lock here.
675 error = ipx_pcballoc(so, &ipxrawpcb_list, td);
678 ipxp = sotoipxpcb(so);
679 error = soreserve(so, ipxsendspace, ipxrecvspace);
682 ipxp->ipxp_faddr.x_host = ipx_broadhost;
683 ipxp->ipxp_flags = IPXP_RAWIN | IPXP_RAWOUT;