2 * Copyright (c) 1984, 1985, 1986, 1987, 1993
3 * The Regents of the University of California.
4 * Copyright (c) 1995, Mike Mitchell
5 * Copyright (c) 2004-2006 Robert N. M. Watson
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/kernel.h>
49 #include <sys/protosw.h>
50 #include <sys/signalvar.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
54 #include <sys/sysctl.h>
55 #include <sys/systm.h>
58 #include <net/route.h>
60 #include <netinet/in.h>
62 #include <netipx/ipx.h>
63 #include <netipx/ipx_if.h>
64 #include <netipx/ipx_ip.h>
65 #include <netipx/ipx_pcb.h>
66 #include <netipx/ipx_var.h>
69 * IPX protocol implementation.
72 static int ipxsendspace = IPXSNDQ;
73 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxsendspace, CTLFLAG_RW,
74 &ipxsendspace, 0, "");
75 static int ipxrecvspace = IPXRCVQ;
76 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxrecvspace, CTLFLAG_RW,
77 &ipxrecvspace, 0, "");
79 static void ipx_usr_abort(struct socket *so);
80 static int ipx_attach(struct socket *so, int proto, struct thread *td);
81 static int ipx_bind(struct socket *so, struct sockaddr *nam, struct thread *td);
82 static int ipx_connect(struct socket *so, struct sockaddr *nam,
84 static void ipx_detach(struct socket *so);
85 static int ipx_disconnect(struct socket *so);
86 static int ipx_send(struct socket *so, int flags, struct mbuf *m,
87 struct sockaddr *addr, struct mbuf *control,
89 static int ipx_shutdown(struct socket *so);
90 static int ripx_attach(struct socket *so, int proto, struct thread *td);
91 static int ipx_output(struct ipxpcb *ipxp, struct mbuf *m0);
92 static void ipx_usr_close(struct socket *so);
94 struct pr_usrreqs ipx_usrreqs = {
95 .pru_abort = ipx_usr_abort,
96 .pru_attach = ipx_attach,
98 .pru_connect = ipx_connect,
99 .pru_control = ipx_control,
100 .pru_detach = ipx_detach,
101 .pru_disconnect = ipx_disconnect,
102 .pru_peeraddr = ipx_peeraddr,
103 .pru_send = ipx_send,
104 .pru_shutdown = ipx_shutdown,
105 .pru_sockaddr = ipx_sockaddr,
106 .pru_close = ipx_usr_close,
109 struct pr_usrreqs ripx_usrreqs = {
110 .pru_abort = ipx_usr_abort,
111 .pru_attach = ripx_attach,
112 .pru_bind = ipx_bind,
113 .pru_connect = ipx_connect,
114 .pru_control = ipx_control,
115 .pru_detach = ipx_detach,
116 .pru_disconnect = ipx_disconnect,
117 .pru_peeraddr = ipx_peeraddr,
118 .pru_send = ipx_send,
119 .pru_shutdown = ipx_shutdown,
120 .pru_sockaddr = ipx_sockaddr,
121 .pru_close = ipx_usr_close,
125 * This may also be called for raw listeners.
130 register struct ipxpcb *ipxp;
132 register struct ipx *ipx = mtod(m, struct ipx *);
133 struct ifnet *ifp = m->m_pkthdr.rcvif;
134 struct sockaddr_ipx ipx_ipx;
136 KASSERT(ipxp != NULL, ("ipx_input: NULL ipxpcb"));
137 IPX_LOCK_ASSERT(ipxp);
139 * Construct sockaddr format source address.
140 * Stuff source address and datagram in user buffer.
142 ipx_ipx.sipx_len = sizeof(ipx_ipx);
143 ipx_ipx.sipx_family = AF_IPX;
144 ipx_ipx.sipx_addr = ipx->ipx_sna;
145 ipx_ipx.sipx_zero[0] = '\0';
146 ipx_ipx.sipx_zero[1] = '\0';
147 if (ipx_neteqnn(ipx->ipx_sna.x_net, ipx_zeronet) && ifp != NULL) {
148 register struct ifaddr *ifa;
150 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa != NULL;
151 ifa = TAILQ_NEXT(ifa, ifa_link)) {
152 if (ifa->ifa_addr->sa_family == AF_IPX) {
153 ipx_ipx.sipx_addr.x_net =
154 IA_SIPX(ifa)->sipx_addr.x_net;
159 ipxp->ipxp_rpt = ipx->ipx_pt;
160 if ((ipxp->ipxp_flags & IPXP_RAWIN) == 0) {
161 m->m_len -= sizeof(struct ipx);
162 m->m_pkthdr.len -= sizeof(struct ipx);
163 m->m_data += sizeof(struct ipx);
165 if (sbappendaddr(&ipxp->ipxp_socket->so_rcv,
166 (struct sockaddr *)&ipx_ipx, m, NULL) == 0)
169 sorwakeup(ipxp->ipxp_socket);
173 * Drop connection, reporting
174 * the specified error.
177 ipx_drop(ipxp, errno)
178 register struct ipxpcb *ipxp;
181 struct socket *so = ipxp->ipxp_socket;
183 IPX_LIST_LOCK_ASSERT();
184 IPX_LOCK_ASSERT(ipxp);
187 * someday, in the IPX world
188 * we will generate error protocol packets
189 * announcing that the socket has gone away.
191 * XXX Probably never. IPX does not have error packets.
193 /*if (TCPS_HAVERCVDSYN(tp->t_state)) {
194 tp->t_state = TCPS_CLOSED;
197 so->so_error = errno;
198 ipx_pcbdisconnect(ipxp);
199 soisdisconnected(so);
207 register struct ipx *ipx;
208 register struct socket *so;
209 register int len = 0;
210 register struct route *ro;
212 struct mbuf *mprev = NULL;
214 IPX_LOCK_ASSERT(ipxp);
217 * Calculate data length.
219 for (m = m0; m != NULL; m = m->m_next) {
224 * Make sure packet is actually of even length.
229 if ((m->m_flags & M_EXT) == 0 &&
230 (m->m_len + m->m_data < &m->m_dat[MLEN])) {
231 mtod(m, char*)[m->m_len++] = 0;
233 struct mbuf *m1 = m_get(M_DONTWAIT, MT_DATA);
240 * mtod(m1, char *) = 0;
247 * Fill in mbuf with extended IPX header
248 * and addresses and length put into network format.
251 if (ipxp->ipxp_flags & IPXP_RAWOUT) {
252 ipx = mtod(m, struct ipx *);
254 M_PREPEND(m, sizeof(struct ipx), M_DONTWAIT);
257 ipx = mtod(m, struct ipx *);
259 ipx->ipx_pt = ipxp->ipxp_dpt;
260 ipx->ipx_sna = ipxp->ipxp_laddr;
261 ipx->ipx_dna = ipxp->ipxp_faddr;
262 len += sizeof(struct ipx);
265 ipx->ipx_len = htons((u_short)len);
267 if (ipxp->ipxp_flags & IPXP_CHECKSUM) {
268 ipx->ipx_sum = ipx_cksum(m, len);
270 ipx->ipx_sum = 0xffff;
275 so = ipxp->ipxp_socket;
276 if (so->so_options & SO_DONTROUTE)
277 return (ipx_outputfl(m, (struct route *)NULL,
278 (so->so_options & SO_BROADCAST) | IPX_ROUTETOIF));
280 * Use cached route for previous datagram if
281 * possible. If the previous net was the same
282 * and the interface was a broadcast medium, or
283 * if the previous destination was identical,
286 * NB: We don't handle broadcasts because that
287 * would require 3 subroutine calls.
289 ro = &ipxp->ipxp_route;
290 #ifdef ancient_history
292 * I think that this will all be handled in ipx_pcbconnect!
294 if (ro->ro_rt != NULL) {
295 if(ipx_neteq(ipxp->ipxp_lastdst, ipx->ipx_dna)) {
297 * This assumes we have no GH type routes
299 if (ro->ro_rt->rt_flags & RTF_HOST) {
300 if (!ipx_hosteq(ipxp->ipxp_lastdst, ipx->ipx_dna))
304 if ((ro->ro_rt->rt_flags & RTF_GATEWAY) == 0) {
305 register struct ipx_addr *dst =
306 &satoipx_addr(ro->ro_dst);
307 dst->x_host = ipx->ipx_dna.x_host;
310 * Otherwise, we go through the same gateway
311 * and dst is already set up.
319 ipxp->ipxp_lastdst = ipx->ipx_dna;
320 #endif /* ancient_history */
321 return (ipx_outputfl(m, ro, so->so_options & SO_BROADCAST));
325 ipx_ctloutput(so, sopt)
327 struct sockopt *sopt;
329 struct ipxpcb *ipxp = sotoipxpcb(so);
330 int mask, error, optval;
335 KASSERT(ipxp != NULL, ("ipx_ctloutput: ipxp == NULL"));
338 switch (sopt->sopt_dir) {
340 switch (sopt->sopt_name) {
342 mask = IPXP_ALL_PACKETS;
345 case SO_HEADERS_ON_INPUT:
349 case SO_IPX_CHECKSUM:
350 mask = IPXP_CHECKSUM;
353 case SO_HEADERS_ON_OUTPUT:
357 soptval = ipxp->ipxp_flags & mask;
358 error = sooptcopyout(sopt, &soptval, sizeof soptval);
361 case SO_DEFAULT_HEADERS:
366 ioptval.ipx_pt = ipxp->ipxp_dpt;
367 ioptval.ipx_dna = ipxp->ipxp_faddr;
368 ioptval.ipx_sna = ipxp->ipxp_laddr;
370 error = sooptcopyout(sopt, &soptval, sizeof soptval);
378 error = sooptcopyout(sopt, &seq, sizeof seq);
387 switch (sopt->sopt_name) {
389 mask = IPXP_ALL_PACKETS;
392 case SO_HEADERS_ON_INPUT:
396 case SO_IPX_CHECKSUM:
397 mask = IPXP_CHECKSUM;
399 case SO_HEADERS_ON_OUTPUT:
402 error = sooptcopyin(sopt, &optval, sizeof optval,
408 ipxp->ipxp_flags |= mask;
410 ipxp->ipxp_flags &= ~mask;
414 case SO_DEFAULT_HEADERS:
415 error = sooptcopyin(sopt, &ioptval, sizeof ioptval,
419 /* Unlocked write. */
420 ipxp->ipxp_dpt = ioptval.ipx_pt;
424 error = ipxip_route(so, sopt);
440 /* XXXRW: Possibly ipx_disconnect() here? */
441 soisdisconnected(so);
445 ipx_attach(so, proto, td)
451 struct ipxpcb *ipxp = sotoipxpcb(so);
455 KASSERT(ipxp == NULL, ("ipx_attach: ipxp != NULL"));
456 error = soreserve(so, ipxsendspace, ipxrecvspace);
460 error = ipx_pcballoc(so, &ipxpcb_list, td);
466 ipx_bind(so, nam, td)
468 struct sockaddr *nam;
471 struct ipxpcb *ipxp = sotoipxpcb(so);
474 KASSERT(ipxp != NULL, ("ipx_bind: ipxp == NULL"));
477 error = ipx_pcbbind(ipxp, nam, td);
488 /* XXXRW: Possibly ipx_disconnect() here? */
489 soisdisconnected(so);
493 ipx_connect(so, nam, td)
495 struct sockaddr *nam;
498 struct ipxpcb *ipxp = sotoipxpcb(so);
501 KASSERT(ipxp != NULL, ("ipx_connect: ipxp == NULL"));
504 if (!ipx_nullhost(ipxp->ipxp_faddr)) {
508 error = ipx_pcbconnect(ipxp, nam, td);
521 struct ipxpcb *ipxp = sotoipxpcb(so);
523 /* XXXRW: Should assert detached. */
524 KASSERT(ipxp != NULL, ("ipx_detach: ipxp == NULL"));
536 struct ipxpcb *ipxp = sotoipxpcb(so);
539 KASSERT(ipxp != NULL, ("ipx_disconnect: ipxp == NULL"));
543 if (ipx_nullhost(ipxp->ipxp_faddr)) {
547 ipx_pcbdisconnect(ipxp);
548 soisdisconnected(so);
556 ipx_peeraddr(so, nam)
558 struct sockaddr **nam;
560 struct ipxpcb *ipxp = sotoipxpcb(so);
562 KASSERT(ipxp != NULL, ("ipx_peeraddr: ipxp == NULL"));
563 ipx_setpeeraddr(ipxp, nam);
568 ipx_send(so, flags, m, nam, control, td)
572 struct sockaddr *nam;
573 struct mbuf *control;
577 struct ipxpcb *ipxp = sotoipxpcb(so);
578 struct ipx_addr laddr;
580 KASSERT(ipxp != NULL, ("ipxp_send: ipxp == NULL"));
582 * Attempt to only acquire the necessary locks: if the socket is
583 * already connected, we don't need to hold the IPX list lock to be
584 * used by ipx_pcbconnect() and ipx_pcbdisconnect(), just the IPX
590 laddr = ipxp->ipxp_laddr;
591 if (!ipx_nullhost(ipxp->ipxp_faddr)) {
598 * Must block input while temporarily connected.
600 error = ipx_pcbconnect(ipxp, nam, td);
608 if (ipx_nullhost(ipxp->ipxp_faddr)) {
614 error = ipx_output(ipxp, m);
617 ipx_pcbdisconnect(ipxp);
618 ipxp->ipxp_laddr = laddr;
635 KASSERT(so->so_pcb != NULL, ("ipx_shutdown: so_pcb == NULL"));
641 ipx_sockaddr(so, nam)
643 struct sockaddr **nam;
645 struct ipxpcb *ipxp = sotoipxpcb(so);
647 KASSERT(ipxp != NULL, ("ipx_sockaddr: ipxp == NULL"));
648 ipx_setsockaddr(ipxp, nam);
653 ripx_attach(so, proto, 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;