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);
92 struct pr_usrreqs ipx_usrreqs = {
93 .pru_abort = ipx_usr_abort,
94 .pru_attach = ipx_attach,
96 .pru_connect = ipx_connect,
97 .pru_control = ipx_control,
98 .pru_detach = ipx_detach,
99 .pru_disconnect = ipx_disconnect,
100 .pru_peeraddr = ipx_peeraddr,
101 .pru_send = ipx_send,
102 .pru_shutdown = ipx_shutdown,
103 .pru_sockaddr = ipx_sockaddr,
106 struct pr_usrreqs ripx_usrreqs = {
107 .pru_abort = ipx_usr_abort,
108 .pru_attach = ripx_attach,
109 .pru_bind = ipx_bind,
110 .pru_connect = ipx_connect,
111 .pru_control = ipx_control,
112 .pru_detach = ipx_detach,
113 .pru_disconnect = ipx_disconnect,
114 .pru_peeraddr = ipx_peeraddr,
115 .pru_send = ipx_send,
116 .pru_shutdown = ipx_shutdown,
117 .pru_sockaddr = ipx_sockaddr,
121 * This may also be called for raw listeners.
126 register struct ipxpcb *ipxp;
128 register struct ipx *ipx = mtod(m, struct ipx *);
129 struct ifnet *ifp = m->m_pkthdr.rcvif;
130 struct sockaddr_ipx ipx_ipx;
132 KASSERT(ipxp != NULL, ("ipx_input: NULL ipxpcb"));
133 IPX_LOCK_ASSERT(ipxp);
135 * Construct sockaddr format source address.
136 * Stuff source address and datagram in user buffer.
138 ipx_ipx.sipx_len = sizeof(ipx_ipx);
139 ipx_ipx.sipx_family = AF_IPX;
140 ipx_ipx.sipx_addr = ipx->ipx_sna;
141 ipx_ipx.sipx_zero[0] = '\0';
142 ipx_ipx.sipx_zero[1] = '\0';
143 if (ipx_neteqnn(ipx->ipx_sna.x_net, ipx_zeronet) && ifp != NULL) {
144 register struct ifaddr *ifa;
146 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa != NULL;
147 ifa = TAILQ_NEXT(ifa, ifa_link)) {
148 if (ifa->ifa_addr->sa_family == AF_IPX) {
149 ipx_ipx.sipx_addr.x_net =
150 IA_SIPX(ifa)->sipx_addr.x_net;
155 ipxp->ipxp_rpt = ipx->ipx_pt;
156 if ((ipxp->ipxp_flags & IPXP_RAWIN) == 0) {
157 m->m_len -= sizeof(struct ipx);
158 m->m_pkthdr.len -= sizeof(struct ipx);
159 m->m_data += sizeof(struct ipx);
161 if (sbappendaddr(&ipxp->ipxp_socket->so_rcv,
162 (struct sockaddr *)&ipx_ipx, m, NULL) == 0)
165 sorwakeup(ipxp->ipxp_socket);
169 * Drop connection, reporting
170 * the specified error.
173 ipx_drop(ipxp, errno)
174 register struct ipxpcb *ipxp;
177 struct socket *so = ipxp->ipxp_socket;
179 IPX_LIST_LOCK_ASSERT();
180 IPX_LOCK_ASSERT(ipxp);
183 * someday, in the IPX world
184 * we will generate error protocol packets
185 * announcing that the socket has gone away.
187 * XXX Probably never. IPX does not have error packets.
189 /*if (TCPS_HAVERCVDSYN(tp->t_state)) {
190 tp->t_state = TCPS_CLOSED;
193 so->so_error = errno;
194 ipx_pcbdisconnect(ipxp);
195 soisdisconnected(so);
203 register struct ipx *ipx;
204 register struct socket *so;
205 register int len = 0;
206 register struct route *ro;
208 struct mbuf *mprev = NULL;
210 IPX_LOCK_ASSERT(ipxp);
213 * Calculate data length.
215 for (m = m0; m != NULL; m = m->m_next) {
220 * Make sure packet is actually of even length.
225 if ((m->m_flags & M_EXT) == 0 &&
226 (m->m_len + m->m_data < &m->m_dat[MLEN])) {
227 mtod(m, char*)[m->m_len++] = 0;
229 struct mbuf *m1 = m_get(M_DONTWAIT, MT_DATA);
236 * mtod(m1, char *) = 0;
243 * Fill in mbuf with extended IPX header
244 * and addresses and length put into network format.
247 if (ipxp->ipxp_flags & IPXP_RAWOUT) {
248 ipx = mtod(m, struct ipx *);
250 M_PREPEND(m, sizeof(struct ipx), M_DONTWAIT);
253 ipx = mtod(m, struct ipx *);
255 ipx->ipx_pt = ipxp->ipxp_dpt;
256 ipx->ipx_sna = ipxp->ipxp_laddr;
257 ipx->ipx_dna = ipxp->ipxp_faddr;
258 len += sizeof(struct ipx);
261 ipx->ipx_len = htons((u_short)len);
263 if (ipxp->ipxp_flags & IPXP_CHECKSUM) {
264 ipx->ipx_sum = ipx_cksum(m, len);
266 ipx->ipx_sum = 0xffff;
271 so = ipxp->ipxp_socket;
272 if (so->so_options & SO_DONTROUTE)
273 return (ipx_outputfl(m, (struct route *)NULL,
274 (so->so_options & SO_BROADCAST) | IPX_ROUTETOIF));
276 * Use cached route for previous datagram if
277 * possible. If the previous net was the same
278 * and the interface was a broadcast medium, or
279 * if the previous destination was identical,
282 * NB: We don't handle broadcasts because that
283 * would require 3 subroutine calls.
285 ro = &ipxp->ipxp_route;
286 #ifdef ancient_history
288 * I think that this will all be handled in ipx_pcbconnect!
290 if (ro->ro_rt != NULL) {
291 if(ipx_neteq(ipxp->ipxp_lastdst, ipx->ipx_dna)) {
293 * This assumes we have no GH type routes
295 if (ro->ro_rt->rt_flags & RTF_HOST) {
296 if (!ipx_hosteq(ipxp->ipxp_lastdst, ipx->ipx_dna))
300 if ((ro->ro_rt->rt_flags & RTF_GATEWAY) == 0) {
301 register struct ipx_addr *dst =
302 &satoipx_addr(ro->ro_dst);
303 dst->x_host = ipx->ipx_dna.x_host;
306 * Otherwise, we go through the same gateway
307 * and dst is already set up.
315 ipxp->ipxp_lastdst = ipx->ipx_dna;
316 #endif /* ancient_history */
317 return (ipx_outputfl(m, ro, so->so_options & SO_BROADCAST));
321 ipx_ctloutput(so, sopt)
323 struct sockopt *sopt;
325 struct ipxpcb *ipxp = sotoipxpcb(so);
326 int mask, error, optval;
331 KASSERT(ipxp != NULL, ("ipx_ctloutput: ipxp == NULL"));
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);
437 KASSERT(ipxp != NULL, ("ipx_usr_abort: ipxp == NULL"));
443 soisdisconnected(so);
447 ipx_attach(so, proto, td)
453 struct ipxpcb *ipxp = sotoipxpcb(so);
457 KASSERT(ipxp == NULL, ("ipx_attach: ipxp != NULL"));
458 error = soreserve(so, ipxsendspace, ipxrecvspace);
462 error = ipx_pcballoc(so, &ipxpcb_list, td);
468 ipx_bind(so, nam, td)
470 struct sockaddr *nam;
473 struct ipxpcb *ipxp = sotoipxpcb(so);
476 KASSERT(ipxp != NULL, ("ipx_bind: ipxp == NULL"));
479 error = ipx_pcbbind(ipxp, nam, td);
486 ipx_connect(so, nam, td)
488 struct sockaddr *nam;
491 struct ipxpcb *ipxp = sotoipxpcb(so);
494 KASSERT(ipxp != NULL, ("ipx_connect: ipxp == NULL"));
497 if (!ipx_nullhost(ipxp->ipxp_faddr)) {
501 error = ipx_pcbconnect(ipxp, nam, td);
514 struct ipxpcb *ipxp = sotoipxpcb(so);
516 KASSERT(ipxp != NULL, ("ipx_detach: ipxp == NULL"));
528 struct ipxpcb *ipxp = sotoipxpcb(so);
531 KASSERT(ipxp != NULL, ("ipx_disconnect: ipxp == NULL"));
535 if (ipx_nullhost(ipxp->ipxp_faddr)) {
539 ipx_pcbdisconnect(ipxp);
540 soisdisconnected(so);
548 ipx_peeraddr(so, nam)
550 struct sockaddr **nam;
552 struct ipxpcb *ipxp = sotoipxpcb(so);
554 KASSERT(ipxp != NULL, ("ipx_peeraddr: ipxp == NULL"));
555 ipx_setpeeraddr(ipxp, nam);
560 ipx_send(so, flags, m, nam, control, td)
564 struct sockaddr *nam;
565 struct mbuf *control;
569 struct ipxpcb *ipxp = sotoipxpcb(so);
570 struct ipx_addr laddr;
572 KASSERT(ipxp != NULL, ("ipxp_send: ipxp == NULL"));
574 * Attempt to only acquire the necessary locks: if the socket is
575 * already connected, we don't need to hold the IPX list lock to be
576 * used by ipx_pcbconnect() and ipx_pcbdisconnect(), just the IPX
582 laddr = ipxp->ipxp_laddr;
583 if (!ipx_nullhost(ipxp->ipxp_faddr)) {
590 * Must block input while temporarily connected.
592 error = ipx_pcbconnect(ipxp, nam, td);
600 if (ipx_nullhost(ipxp->ipxp_faddr)) {
606 error = ipx_output(ipxp, m);
609 ipx_pcbdisconnect(ipxp);
610 ipxp->ipxp_laddr = laddr;
627 KASSERT(so->so_pcb != NULL, ("ipx_shutdown: so_pcb == NULL"));
633 ipx_sockaddr(so, nam)
635 struct sockaddr **nam;
637 struct ipxpcb *ipxp = sotoipxpcb(so);
639 KASSERT(ipxp != NULL, ("ipx_sockaddr: ipxp == NULL"));
640 ipx_setsockaddr(ipxp, nam);
645 ripx_attach(so, proto, td)
651 struct ipxpcb *ipxp = sotoipxpcb(so);
653 KASSERT(ipxp == NULL, ("ripx_attach: ipxp != NULL"));
654 if (td != NULL && (error = suser(td)) != 0)
657 * We hold the IPX list lock for the duration as address parameters
658 * of the IPX pcb are changed. Since no one else holds a reference
659 * to the ipxpcb yet, we don't need the ipxpcb lock here.
662 error = ipx_pcballoc(so, &ipxrawpcb_list, td);
665 ipxp = sotoipxpcb(so);
666 error = soreserve(so, ipxsendspace, ipxrecvspace);
669 ipxp->ipxp_faddr.x_host = ipx_broadhost;
670 ipxp->ipxp_flags = IPXP_RAWIN | IPXP_RAWOUT;