2 * Copyright (c) 1995, Mike Mitchell
3 * Copyright (c) 1984, 1985, 1986, 1987, 1993
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * $FreeBSD: src/sys/netipx/ipx_usrreq.c,v 1.26.2.1 2001/02/22 09:44:18 bp Exp $
37 * $DragonFly: src/sys/netproto/ipx/ipx_usrreq.c,v 1.10 2006/01/14 13:36:40 swildner Exp $
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/sysctl.h>
51 #include <sys/thread2.h>
54 #include <net/route.h>
56 #include <netinet/in.h>
65 * IPX protocol implementation.
68 static int ipxsendspace = IPXSNDQ;
69 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxsendspace, CTLFLAG_RW,
70 &ipxsendspace, 0, "");
71 static int ipxrecvspace = IPXRCVQ;
72 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxrecvspace, CTLFLAG_RW,
73 &ipxrecvspace, 0, "");
75 static int ipx_usr_abort(struct socket *so);
76 static int ipx_attach(struct socket *so, int proto,
77 struct pru_attach_info *ai);
78 static int ipx_bind(struct socket *so, struct sockaddr *nam,
80 static int ipx_connect(struct socket *so, struct sockaddr *nam,
82 static int ipx_detach(struct socket *so);
83 static int ipx_disconnect(struct socket *so);
84 static int ipx_send(struct socket *so, int flags, struct mbuf *m,
85 struct sockaddr *addr, struct mbuf *control,
87 static int ipx_shutdown(struct socket *so);
88 static int ripx_attach(struct socket *so, int proto,
89 struct pru_attach_info *ai);
90 static int ipx_output(struct ipxpcb *ipxp, struct mbuf *m0);
92 struct pr_usrreqs ipx_usrreqs = {
93 ipx_usr_abort, pru_accept_notsupp, ipx_attach, ipx_bind,
94 ipx_connect, pru_connect2_notsupp, ipx_control, ipx_detach,
95 ipx_disconnect, pru_listen_notsupp, ipx_peeraddr, pru_rcvd_notsupp,
96 pru_rcvoob_notsupp, ipx_send, pru_sense_null, ipx_shutdown,
97 ipx_sockaddr, sosend, soreceive, sopoll
100 struct pr_usrreqs ripx_usrreqs = {
101 ipx_usr_abort, pru_accept_notsupp, ripx_attach, ipx_bind,
102 ipx_connect, pru_connect2_notsupp, ipx_control, ipx_detach,
103 ipx_disconnect, pru_listen_notsupp, ipx_peeraddr, pru_rcvd_notsupp,
104 pru_rcvoob_notsupp, ipx_send, pru_sense_null, ipx_shutdown,
105 ipx_sockaddr, sosend, soreceive, sopoll
109 * This may also be called for raw listeners.
112 ipx_input(struct mbuf *m, struct ipxpcb *ipxp)
114 struct ipx *ipx = mtod(m, struct ipx *);
115 struct ifnet *ifp = m->m_pkthdr.rcvif;
116 struct sockaddr_ipx ipx_ipx;
121 * Construct sockaddr format source address.
122 * Stuff source address and datagram in user buffer.
124 ipx_ipx.sipx_len = sizeof(ipx_ipx);
125 ipx_ipx.sipx_family = AF_IPX;
126 ipx_ipx.sipx_addr = ipx->ipx_sna;
127 ipx_ipx.sipx_zero[0] = '\0';
128 ipx_ipx.sipx_zero[1] = '\0';
129 if (ipx_neteqnn(ipx->ipx_sna.x_net, ipx_zeronet) && ifp != NULL) {
132 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa != NULL;
133 ifa = TAILQ_NEXT(ifa, ifa_link)) {
134 if (ifa->ifa_addr->sa_family == AF_IPX) {
135 ipx_ipx.sipx_addr.x_net =
136 IA_SIPX(ifa)->sipx_addr.x_net;
141 ipxp->ipxp_rpt = ipx->ipx_pt;
142 if (!(ipxp->ipxp_flags & IPXP_RAWIN) ) {
143 m->m_len -= sizeof(struct ipx);
144 m->m_pkthdr.len -= sizeof(struct ipx);
145 m->m_data += sizeof(struct ipx);
147 if (sbappendaddr(&ipxp->ipxp_socket->so_rcv, (struct sockaddr *)&ipx_ipx,
148 m, (struct mbuf *)NULL) == 0)
150 sorwakeup(ipxp->ipxp_socket);
157 ipx_abort(struct ipxpcb *ipxp)
159 struct socket *so = ipxp->ipxp_socket;
161 ipx_pcbdisconnect(ipxp);
162 soisdisconnected(so);
166 * Drop connection, reporting
167 * the specified error.
170 ipx_drop(struct ipxpcb *ipxp, int errno)
172 struct socket *so = ipxp->ipxp_socket;
175 * someday, in the IPX world
176 * we will generate error protocol packets
177 * announcing that the socket has gone away.
179 * XXX Probably never. IPX does not have error packets.
181 /*if (TCPS_HAVERCVDSYN(tp->t_state)) {
182 tp->t_state = TCPS_CLOSED;
185 so->so_error = errno;
186 ipx_pcbdisconnect(ipxp);
187 soisdisconnected(so);
191 ipx_output(struct ipxpcb *ipxp, struct mbuf *m0)
198 struct mbuf *mprev = NULL;
201 * Calculate data length.
203 for (m = m0; m != NULL; m = m->m_next) {
208 * Make sure packet is actually of even length.
213 if ((m->m_flags & M_EXT) == 0 &&
214 (m->m_len + m->m_data < &m->m_dat[MLEN])) {
215 mtod(m, char*)[m->m_len++] = 0;
217 struct mbuf *m1 = m_get(MB_DONTWAIT, MT_DATA);
224 * mtod(m1, char *) = 0;
231 * Fill in mbuf with extended IPX header
232 * and addresses and length put into network format.
235 if (ipxp->ipxp_flags & IPXP_RAWOUT) {
236 ipx = mtod(m, struct ipx *);
238 M_PREPEND(m, sizeof(struct ipx), MB_DONTWAIT);
241 ipx = mtod(m, struct ipx *);
243 ipx->ipx_pt = ipxp->ipxp_dpt;
244 ipx->ipx_sna = ipxp->ipxp_laddr;
245 ipx->ipx_dna = ipxp->ipxp_faddr;
246 len += sizeof(struct ipx);
249 ipx->ipx_len = htons((u_short)len);
251 if (ipxp->ipxp_flags & IPXP_CHECKSUM) {
252 ipx->ipx_sum = ipx_cksum(m, len);
254 ipx->ipx_sum = 0xffff;
259 so = ipxp->ipxp_socket;
260 if (so->so_options & SO_DONTROUTE)
261 return (ipx_outputfl(m, (struct route *)NULL,
262 (so->so_options & SO_BROADCAST) | IPX_ROUTETOIF));
264 * Use cached route for previous datagram if
265 * possible. If the previous net was the same
266 * and the interface was a broadcast medium, or
267 * if the previous destination was identical,
270 * NB: We don't handle broadcasts because that
271 * would require 3 subroutine calls.
273 ro = &ipxp->ipxp_route;
274 #ifdef ancient_history
276 * I think that this will all be handled in ipx_pcbconnect!
278 if (ro->ro_rt != NULL) {
279 if(ipx_neteq(ipxp->ipxp_lastdst, ipx->ipx_dna)) {
281 * This assumes we have no GH type routes
283 if (ro->ro_rt->rt_flags & RTF_HOST) {
284 if (!ipx_hosteq(ipxp->ipxp_lastdst, ipx->ipx_dna))
288 if ((ro->ro_rt->rt_flags & RTF_GATEWAY) == 0) {
289 struct ipx_addr *dst =
290 &satoipx_addr(ro->ro_dst);
291 dst->x_host = ipx->ipx_dna.x_host;
294 * Otherwise, we go through the same gateway
295 * and dst is already set up.
303 ipxp->ipxp_lastdst = ipx->ipx_dna;
304 #endif /* ancient_history */
305 return (ipx_outputfl(m, ro, so->so_options & SO_BROADCAST));
309 ipx_ctloutput(struct socket *so, struct sockopt *sopt)
311 struct ipxpcb *ipxp = sotoipxpcb(so);
312 int mask, error, optval;
320 switch (sopt->sopt_dir) {
322 switch (sopt->sopt_name) {
324 mask = IPXP_ALL_PACKETS;
327 case SO_HEADERS_ON_INPUT:
331 case SO_IPX_CHECKSUM:
332 mask = IPXP_CHECKSUM;
335 case SO_HEADERS_ON_OUTPUT:
338 soptval = ipxp->ipxp_flags & mask;
339 error = sooptcopyout(sopt, &soptval, sizeof soptval);
342 case SO_DEFAULT_HEADERS:
346 ioptval.ipx_pt = ipxp->ipxp_dpt;
347 ioptval.ipx_dna = ipxp->ipxp_faddr;
348 ioptval.ipx_sna = ipxp->ipxp_laddr;
349 error = sooptcopyout(sopt, &soptval, sizeof soptval);
353 error = sooptcopyout(sopt, &ipx_pexseq,
364 switch (sopt->sopt_name) {
366 mask = IPXP_ALL_PACKETS;
369 case SO_HEADERS_ON_INPUT:
373 case SO_IPX_CHECKSUM:
374 mask = IPXP_CHECKSUM;
376 case SO_HEADERS_ON_OUTPUT:
379 error = sooptcopyin(sopt, &optval, sizeof optval,
384 ipxp->ipxp_flags |= mask;
386 ipxp->ipxp_flags &= ~mask;
389 case SO_DEFAULT_HEADERS:
390 error = sooptcopyin(sopt, &ioptval, sizeof ioptval,
394 ipxp->ipxp_dpt = ioptval.ipx_pt;
398 error = ipxip_route(so, sopt);
403 case SO_IPXTUNNEL_ROUTE:
404 error = ipxtun_route(so, sopt);
417 ipx_usr_abort(struct socket *so)
419 struct ipxpcb *ipxp = sotoipxpcb(so);
425 soisdisconnected(so);
430 ipx_attach(struct socket *so, int proto, struct pru_attach_info *ai)
433 struct ipxpcb *ipxp = sotoipxpcb(so);
438 error = ipx_pcballoc(so, &ipxpcb);
441 error = soreserve(so, ipxsendspace, ipxrecvspace,
447 ipx_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
449 struct ipxpcb *ipxp = sotoipxpcb(so);
451 return (ipx_pcbbind(ipxp, nam, td));
455 ipx_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
458 struct ipxpcb *ipxp = sotoipxpcb(so);
460 if (!ipx_nullhost(ipxp->ipxp_faddr))
463 error = ipx_pcbconnect(ipxp, nam, td);
471 ipx_detach(struct socket *so)
473 struct ipxpcb *ipxp = sotoipxpcb(so);
484 ipx_disconnect(struct socket *so)
486 struct ipxpcb *ipxp = sotoipxpcb(so);
488 if (ipx_nullhost(ipxp->ipxp_faddr))
491 ipx_pcbdisconnect(ipxp);
493 soisdisconnected(so);
498 ipx_peeraddr(struct socket *so, struct sockaddr **nam)
500 struct ipxpcb *ipxp = sotoipxpcb(so);
502 ipx_setpeeraddr(ipxp, nam);
507 ipx_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
508 struct mbuf *control, struct thread *td)
511 struct ipxpcb *ipxp = sotoipxpcb(so);
512 struct ipx_addr laddr;
516 laddr = ipxp->ipxp_laddr;
517 if (!ipx_nullhost(ipxp->ipxp_faddr)) {
522 * Must block input while temporarily connected.
524 error = ipx_pcbconnect(ipxp, nam, td);
529 if (ipx_nullhost(ipxp->ipxp_faddr)) {
534 error = ipx_output(ipxp, m);
537 ipx_pcbdisconnect(ipxp);
538 ipxp->ipxp_laddr = laddr;
548 ipx_shutdown(struct socket *so)
555 ipx_sockaddr(struct socket *so, struct sockaddr **nam)
557 struct ipxpcb *ipxp = sotoipxpcb(so);
559 ipx_setsockaddr(ipxp, nam); /* XXX what if alloc fails? */
564 ripx_attach(struct socket *so, int proto, struct pru_attach_info *ai)
567 struct ipxpcb *ipxp = sotoipxpcb(so);
569 if ((error = suser_cred(ai->p_ucred, NULL_CRED_OKAY)) != 0)
572 error = ipx_pcballoc(so, &ipxrawpcb);
576 error = soreserve(so, ipxsendspace, ipxrecvspace, ai->sb_rlimit);
579 ipxp = sotoipxpcb(so);
580 ipxp->ipxp_faddr.x_host = ipx_broadhost;
581 ipxp->ipxp_flags = IPXP_RAWIN | IPXP_RAWOUT;