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>
48 #include <sys/protosw.h>
49 #include <sys/signalvar.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/systm.h>
57 #include <net/route.h>
59 #include <netinet/in.h>
61 #include <netipx/ipx.h>
62 #include <netipx/ipx_if.h>
63 #include <netipx/ipx_ip.h>
64 #include <netipx/ipx_pcb.h>
65 #include <netipx/ipx_var.h>
68 * IPX protocol implementation.
71 static int ipxsendspace = IPXSNDQ;
72 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxsendspace, CTLFLAG_RW,
73 &ipxsendspace, 0, "");
74 static int ipxrecvspace = IPXRCVQ;
75 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxrecvspace, CTLFLAG_RW,
76 &ipxrecvspace, 0, "");
78 static void ipx_usr_abort(struct socket *so);
79 static int ipx_attach(struct socket *so, int proto, struct thread *td);
80 static int ipx_bind(struct socket *so, struct sockaddr *nam, struct thread *td);
81 static int ipx_connect(struct socket *so, struct sockaddr *nam,
83 static void ipx_detach(struct socket *so);
84 static int ipx_disconnect(struct socket *so);
85 static int ipx_send(struct socket *so, int flags, struct mbuf *m,
86 struct sockaddr *addr, struct mbuf *control,
88 static int ipx_shutdown(struct socket *so);
89 static int ripx_attach(struct socket *so, int proto, struct thread *td);
90 static int ipx_output(struct ipxpcb *ipxp, struct mbuf *m0);
91 static void ipx_usr_close(struct socket *so);
93 struct pr_usrreqs ipx_usrreqs = {
94 .pru_abort = ipx_usr_abort,
95 .pru_attach = ipx_attach,
97 .pru_connect = ipx_connect,
98 .pru_control = ipx_control,
99 .pru_detach = ipx_detach,
100 .pru_disconnect = ipx_disconnect,
101 .pru_peeraddr = ipx_peeraddr,
102 .pru_send = ipx_send,
103 .pru_shutdown = ipx_shutdown,
104 .pru_sockaddr = ipx_sockaddr,
105 .pru_close = ipx_usr_close,
108 struct pr_usrreqs ripx_usrreqs = {
109 .pru_abort = ipx_usr_abort,
110 .pru_attach = ripx_attach,
111 .pru_bind = ipx_bind,
112 .pru_connect = ipx_connect,
113 .pru_control = ipx_control,
114 .pru_detach = ipx_detach,
115 .pru_disconnect = ipx_disconnect,
116 .pru_peeraddr = ipx_peeraddr,
117 .pru_send = ipx_send,
118 .pru_shutdown = ipx_shutdown,
119 .pru_sockaddr = ipx_sockaddr,
120 .pru_close = ipx_usr_close,
124 * This may also be called for raw listeners.
129 register struct ipxpcb *ipxp;
131 register struct ipx *ipx = mtod(m, struct ipx *);
132 struct ifnet *ifp = m->m_pkthdr.rcvif;
133 struct sockaddr_ipx ipx_ipx;
135 KASSERT(ipxp != NULL, ("ipx_input: NULL ipxpcb"));
136 IPX_LOCK_ASSERT(ipxp);
138 * Construct sockaddr format source address.
139 * Stuff source address and datagram in user buffer.
141 ipx_ipx.sipx_len = sizeof(ipx_ipx);
142 ipx_ipx.sipx_family = AF_IPX;
143 ipx_ipx.sipx_addr = ipx->ipx_sna;
144 ipx_ipx.sipx_zero[0] = '\0';
145 ipx_ipx.sipx_zero[1] = '\0';
146 if (ipx_neteqnn(ipx->ipx_sna.x_net, ipx_zeronet) && ifp != NULL) {
147 register struct ifaddr *ifa;
149 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa != NULL;
150 ifa = TAILQ_NEXT(ifa, ifa_link)) {
151 if (ifa->ifa_addr->sa_family == AF_IPX) {
152 ipx_ipx.sipx_addr.x_net =
153 IA_SIPX(ifa)->sipx_addr.x_net;
158 ipxp->ipxp_rpt = ipx->ipx_pt;
159 if ((ipxp->ipxp_flags & IPXP_RAWIN) == 0) {
160 m->m_len -= sizeof(struct ipx);
161 m->m_pkthdr.len -= sizeof(struct ipx);
162 m->m_data += sizeof(struct ipx);
164 if (sbappendaddr(&ipxp->ipxp_socket->so_rcv,
165 (struct sockaddr *)&ipx_ipx, m, NULL) == 0)
168 sorwakeup(ipxp->ipxp_socket);
172 * Drop connection, reporting
173 * the specified error.
176 ipx_drop(ipxp, errno)
177 register struct ipxpcb *ipxp;
180 struct socket *so = ipxp->ipxp_socket;
182 IPX_LIST_LOCK_ASSERT();
183 IPX_LOCK_ASSERT(ipxp);
186 * someday, in the IPX world
187 * we will generate error protocol packets
188 * announcing that the socket has gone away.
190 * XXX Probably never. IPX does not have error packets.
192 /*if (TCPS_HAVERCVDSYN(tp->t_state)) {
193 tp->t_state = TCPS_CLOSED;
196 so->so_error = errno;
197 ipx_pcbdisconnect(ipxp);
198 soisdisconnected(so);
206 register struct ipx *ipx;
207 register struct socket *so;
208 register int len = 0;
209 register struct route *ro;
211 struct mbuf *mprev = NULL;
213 IPX_LOCK_ASSERT(ipxp);
216 * Calculate data length.
218 for (m = m0; m != NULL; m = m->m_next) {
223 * Make sure packet is actually of even length.
228 if ((m->m_flags & M_EXT) == 0 &&
229 (m->m_len + m->m_data < &m->m_dat[MLEN])) {
230 mtod(m, char*)[m->m_len++] = 0;
232 struct mbuf *m1 = m_get(M_DONTWAIT, MT_DATA);
239 * mtod(m1, char *) = 0;
246 * Fill in mbuf with extended IPX header
247 * and addresses and length put into network format.
250 if (ipxp->ipxp_flags & IPXP_RAWOUT) {
251 ipx = mtod(m, struct ipx *);
253 M_PREPEND(m, sizeof(struct ipx), M_DONTWAIT);
256 ipx = mtod(m, struct ipx *);
258 ipx->ipx_pt = ipxp->ipxp_dpt;
259 ipx->ipx_sna = ipxp->ipxp_laddr;
260 ipx->ipx_dna = ipxp->ipxp_faddr;
261 len += sizeof(struct ipx);
264 ipx->ipx_len = htons((u_short)len);
266 if (ipxp->ipxp_flags & IPXP_CHECKSUM) {
267 ipx->ipx_sum = ipx_cksum(m, len);
269 ipx->ipx_sum = 0xffff;
274 so = ipxp->ipxp_socket;
275 if (so->so_options & SO_DONTROUTE)
276 return (ipx_outputfl(m, (struct route *)NULL,
277 (so->so_options & SO_BROADCAST) | IPX_ROUTETOIF));
279 * Use cached route for previous datagram if
280 * possible. If the previous net was the same
281 * and the interface was a broadcast medium, or
282 * if the previous destination was identical,
285 * NB: We don't handle broadcasts because that
286 * would require 3 subroutine calls.
288 ro = &ipxp->ipxp_route;
289 #ifdef ancient_history
291 * I think that this will all be handled in ipx_pcbconnect!
293 if (ro->ro_rt != NULL) {
294 if(ipx_neteq(ipxp->ipxp_lastdst, ipx->ipx_dna)) {
296 * This assumes we have no GH type routes
298 if (ro->ro_rt->rt_flags & RTF_HOST) {
299 if (!ipx_hosteq(ipxp->ipxp_lastdst, ipx->ipx_dna))
303 if ((ro->ro_rt->rt_flags & RTF_GATEWAY) == 0) {
304 register struct ipx_addr *dst =
305 &satoipx_addr(ro->ro_dst);
306 dst->x_host = ipx->ipx_dna.x_host;
309 * Otherwise, we go through the same gateway
310 * and dst is already set up.
318 ipxp->ipxp_lastdst = ipx->ipx_dna;
319 #endif /* ancient_history */
320 return (ipx_outputfl(m, ro, so->so_options & SO_BROADCAST));
324 ipx_ctloutput(so, sopt)
326 struct sockopt *sopt;
328 struct ipxpcb *ipxp = sotoipxpcb(so);
329 int mask, error, optval;
334 KASSERT(ipxp != NULL, ("ipx_ctloutput: ipxp == NULL"));
337 switch (sopt->sopt_dir) {
339 switch (sopt->sopt_name) {
341 mask = IPXP_ALL_PACKETS;
344 case SO_HEADERS_ON_INPUT:
348 case SO_IPX_CHECKSUM:
349 mask = IPXP_CHECKSUM;
352 case SO_HEADERS_ON_OUTPUT:
356 soptval = ipxp->ipxp_flags & mask;
357 error = sooptcopyout(sopt, &soptval, sizeof soptval);
360 case SO_DEFAULT_HEADERS:
365 ioptval.ipx_pt = ipxp->ipxp_dpt;
366 ioptval.ipx_dna = ipxp->ipxp_faddr;
367 ioptval.ipx_sna = ipxp->ipxp_laddr;
369 error = sooptcopyout(sopt, &soptval, sizeof soptval);
377 error = sooptcopyout(sopt, &seq, sizeof seq);
386 switch (sopt->sopt_name) {
388 mask = IPXP_ALL_PACKETS;
391 case SO_HEADERS_ON_INPUT:
395 case SO_IPX_CHECKSUM:
396 mask = IPXP_CHECKSUM;
398 case SO_HEADERS_ON_OUTPUT:
401 error = sooptcopyin(sopt, &optval, sizeof optval,
407 ipxp->ipxp_flags |= mask;
409 ipxp->ipxp_flags &= ~mask;
413 case SO_DEFAULT_HEADERS:
414 error = sooptcopyin(sopt, &ioptval, sizeof ioptval,
418 /* Unlocked write. */
419 ipxp->ipxp_dpt = ioptval.ipx_pt;
423 error = ipxip_route(so, sopt);
439 /* XXXRW: Possibly ipx_disconnect() here? */
440 soisdisconnected(so);
444 ipx_attach(so, proto, td)
450 struct ipxpcb *ipxp = sotoipxpcb(so);
454 KASSERT(ipxp == NULL, ("ipx_attach: ipxp != NULL"));
455 error = soreserve(so, ipxsendspace, ipxrecvspace);
459 error = ipx_pcballoc(so, &ipxpcb_list, td);
465 ipx_bind(so, nam, td)
467 struct sockaddr *nam;
470 struct ipxpcb *ipxp = sotoipxpcb(so);
473 KASSERT(ipxp != NULL, ("ipx_bind: ipxp == NULL"));
476 error = ipx_pcbbind(ipxp, nam, td);
487 /* XXXRW: Possibly ipx_disconnect() here? */
488 soisdisconnected(so);
492 ipx_connect(so, nam, td)
494 struct sockaddr *nam;
497 struct ipxpcb *ipxp = sotoipxpcb(so);
500 KASSERT(ipxp != NULL, ("ipx_connect: ipxp == NULL"));
503 if (!ipx_nullhost(ipxp->ipxp_faddr)) {
507 error = ipx_pcbconnect(ipxp, nam, td);
520 struct ipxpcb *ipxp = sotoipxpcb(so);
522 /* XXXRW: Should assert detached. */
523 KASSERT(ipxp != NULL, ("ipx_detach: ipxp == NULL"));
535 struct ipxpcb *ipxp = sotoipxpcb(so);
538 KASSERT(ipxp != NULL, ("ipx_disconnect: ipxp == NULL"));
542 if (ipx_nullhost(ipxp->ipxp_faddr)) {
546 ipx_pcbdisconnect(ipxp);
547 soisdisconnected(so);
555 ipx_peeraddr(so, nam)
557 struct sockaddr **nam;
559 struct ipxpcb *ipxp = sotoipxpcb(so);
561 KASSERT(ipxp != NULL, ("ipx_peeraddr: ipxp == NULL"));
562 ipx_setpeeraddr(ipxp, nam);
567 ipx_send(so, flags, m, nam, control, td)
571 struct sockaddr *nam;
572 struct mbuf *control;
576 struct ipxpcb *ipxp = sotoipxpcb(so);
577 struct ipx_addr laddr;
579 KASSERT(ipxp != NULL, ("ipxp_send: ipxp == NULL"));
581 * Attempt to only acquire the necessary locks: if the socket is
582 * already connected, we don't need to hold the IPX list lock to be
583 * used by ipx_pcbconnect() and ipx_pcbdisconnect(), just the IPX
589 laddr = ipxp->ipxp_laddr;
590 if (!ipx_nullhost(ipxp->ipxp_faddr)) {
597 * Must block input while temporarily connected.
599 error = ipx_pcbconnect(ipxp, nam, td);
607 if (ipx_nullhost(ipxp->ipxp_faddr)) {
613 error = ipx_output(ipxp, m);
616 ipx_pcbdisconnect(ipxp);
617 ipxp->ipxp_laddr = laddr;
634 KASSERT(so->so_pcb != NULL, ("ipx_shutdown: so_pcb == NULL"));
640 ipx_sockaddr(so, nam)
642 struct sockaddr **nam;
644 struct ipxpcb *ipxp = sotoipxpcb(so);
646 KASSERT(ipxp != NULL, ("ipx_sockaddr: ipxp == NULL"));
647 ipx_setsockaddr(ipxp, nam);
652 ripx_attach(so, proto, td)
658 struct ipxpcb *ipxp = sotoipxpcb(so);
660 KASSERT(ipxp == NULL, ("ripx_attach: ipxp != NULL"));
661 if (td != NULL && (error = suser(td)) != 0)
664 * We hold the IPX list lock for the duration as address parameters
665 * of the IPX pcb are changed. Since no one else holds a reference
666 * to the ipxpcb yet, we don't need the ipxpcb lock here.
669 error = ipx_pcballoc(so, &ipxrawpcb_list, td);
672 ipxp = sotoipxpcb(so);
673 error = soreserve(so, ipxsendspace, ipxrecvspace);
676 ipxp->ipxp_faddr.x_host = ipx_broadhost;
677 ipxp->ipxp_flags = IPXP_RAWIN | IPXP_RAWOUT;