ipx_usrreq.c revision 29366
111819Sjulian/*
211819Sjulian * Copyright (c) 1995, Mike Mitchell
311819Sjulian * Copyright (c) 1984, 1985, 1986, 1987, 1993
411819Sjulian *	The Regents of the University of California.  All rights reserved.
511819Sjulian *
611819Sjulian * Redistribution and use in source and binary forms, with or without
711819Sjulian * modification, are permitted provided that the following conditions
811819Sjulian * are met:
911819Sjulian * 1. Redistributions of source code must retain the above copyright
1011819Sjulian *    notice, this list of conditions and the following disclaimer.
1111819Sjulian * 2. Redistributions in binary form must reproduce the above copyright
1211819Sjulian *    notice, this list of conditions and the following disclaimer in the
1311819Sjulian *    documentation and/or other materials provided with the distribution.
1411819Sjulian * 3. All advertising materials mentioning features or use of this software
1511819Sjulian *    must display the following acknowledgement:
1611819Sjulian *	This product includes software developed by the University of
1711819Sjulian *	California, Berkeley and its contributors.
1811819Sjulian * 4. Neither the name of the University nor the names of its contributors
1911819Sjulian *    may be used to endorse or promote products derived from this software
2011819Sjulian *    without specific prior written permission.
2111819Sjulian *
2211819Sjulian * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2311819Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2411819Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2511819Sjulian * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2611819Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2711819Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2811819Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2911819Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3011819Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3111819Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3211819Sjulian * SUCH DAMAGE.
3311819Sjulian *
3412057Sjulian *	@(#)ipx_usrreq.c
3512057Sjulian *
3629366Speter * $Id: ipx_usrreq.c,v 1.16 1997/08/16 19:15:45 wollman Exp $
3711819Sjulian */
3811819Sjulian
3911819Sjulian#include <sys/param.h>
4011819Sjulian#include <sys/systm.h>
4119947Sjhay#include <sys/kernel.h>
4211819Sjulian#include <sys/mbuf.h>
4325345Sjhay#include <sys/proc.h>
4411819Sjulian#include <sys/protosw.h>
4511819Sjulian#include <sys/socket.h>
4611819Sjulian#include <sys/socketvar.h>
4719947Sjhay#include <sys/sysctl.h>
4811819Sjulian
4911819Sjulian#include <net/if.h>
5011819Sjulian#include <net/route.h>
5111819Sjulian
5211947Sjulian#include <netinet/in.h>
5311947Sjulian
5411819Sjulian#include <netipx/ipx.h>
5511819Sjulian#include <netipx/ipx_pcb.h>
5611819Sjulian#include <netipx/ipx_if.h>
5711819Sjulian#include <netipx/ipx_var.h>
5811947Sjulian#include <netipx/ipx_ip.h>
5911819Sjulian
6011819Sjulian/*
6111819Sjulian * IPX protocol implementation.
6211819Sjulian */
6311819Sjulian
6419947Sjhayint ipxsendspace = IPXSNDQ;
6519947SjhaySYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxsendspace, CTLFLAG_RW,
6619947Sjhay            &ipxsendspace, 0, "");
6719947Sjhayint ipxrecvspace = IPXRCVQ;
6819947SjhaySYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxrecvspace, CTLFLAG_RW,
6919947Sjhay            &ipxrecvspace, 0, "");
7019947Sjhay
7124659Sjhaystatic	int ipx_usr_abort(struct socket *so);
7225345Sjhaystatic	int ipx_attach(struct socket *so, int proto, struct proc *p);
7328270Swollmanstatic	int ipx_bind(struct socket *so, struct sockaddr *nam, struct proc *p);
7428270Swollmanstatic	int ipx_connect(struct socket *so, struct sockaddr *nam,
7528270Swollman			struct proc *p);
7624659Sjhaystatic	int ipx_detach(struct socket *so);
7724659Sjhaystatic	int ipx_disconnect(struct socket *so);
7824659Sjhaystatic	int ipx_send(struct socket *so, int flags, struct mbuf *m,
7928270Swollman		     struct sockaddr *addr, struct mbuf *control,
8028270Swollman		     struct proc *p);
8124659Sjhaystatic	int ipx_shutdown(struct socket *so);
8225345Sjhaystatic	int ripx_attach(struct socket *so, int proto, struct proc *p);
8325652Sjhaystatic	int ipx_output(struct ipxpcb *ipxp, struct mbuf *m0);
8424659Sjhay
8525652Sjhaystruct	pr_usrreqs ipx_usrreqs = {
8624659Sjhay	ipx_usr_abort, pru_accept_notsupp, ipx_attach, ipx_bind,
8724659Sjhay	ipx_connect, pru_connect2_notsupp, ipx_control, ipx_detach,
8824659Sjhay	ipx_disconnect, pru_listen_notsupp, ipx_peeraddr, pru_rcvd_notsupp,
8924659Sjhay	pru_rcvoob_notsupp, ipx_send, pru_sense_null, ipx_shutdown,
9029366Speter	ipx_sockaddr, sosend, soreceive, sopoll
9124659Sjhay};
9224659Sjhay
9325652Sjhaystruct	pr_usrreqs ripx_usrreqs = {
9424659Sjhay	ipx_usr_abort, pru_accept_notsupp, ripx_attach, ipx_bind,
9524659Sjhay	ipx_connect, pru_connect2_notsupp, ipx_control, ipx_detach,
9624659Sjhay	ipx_disconnect, pru_listen_notsupp, ipx_peeraddr, pru_rcvd_notsupp,
9724659Sjhay	pru_rcvoob_notsupp, ipx_send, pru_sense_null, ipx_shutdown,
9829366Speter	ipx_sockaddr, sosend, soreceive, sopoll
9924659Sjhay};
10024659Sjhay
10111819Sjulian/*
10211819Sjulian *  This may also be called for raw listeners.
10311819Sjulian */
10411819Sjulianvoid
10511819Sjulianipx_input(m, ipxp)
10611819Sjulian	struct mbuf *m;
10711819Sjulian	register struct ipxpcb *ipxp;
10811819Sjulian{
10911819Sjulian	register struct ipx *ipx = mtod(m, struct ipx *);
11011819Sjulian	struct ifnet *ifp = m->m_pkthdr.rcvif;
11115245Sjhay	struct sockaddr_ipx ipx_ipx;
11211819Sjulian
11325652Sjhay	if (ipxp == NULL)
11411819Sjulian		panic("No ipxpcb");
11511819Sjulian	/*
11611819Sjulian	 * Construct sockaddr format source address.
11711819Sjulian	 * Stuff source address and datagram in user buffer.
11811819Sjulian	 */
11915245Sjhay	ipx_ipx.sipx_len = sizeof(ipx_ipx);
12015245Sjhay	ipx_ipx.sipx_family = AF_IPX;
12111819Sjulian	ipx_ipx.sipx_addr = ipx->ipx_sna;
12215245Sjhay	ipx_ipx.sipx_zero[0] = '\0';
12315245Sjhay	ipx_ipx.sipx_zero[1] = '\0';
12425652Sjhay	if (ipx_neteqnn(ipx->ipx_sna.x_net, ipx_zeronet) && ifp != NULL) {
12511819Sjulian		register struct ifaddr *ifa;
12611819Sjulian
12725652Sjhay		for (ifa = ifp->if_addrhead.tqh_first; ifa != NULL;
12820407Swollman		     ifa = ifa->ifa_link.tqe_next) {
12911819Sjulian			if (ifa->ifa_addr->sa_family == AF_IPX) {
13011819Sjulian				ipx_ipx.sipx_addr.x_net =
13111819Sjulian					IA_SIPX(ifa)->sipx_addr.x_net;
13211819Sjulian				break;
13311819Sjulian			}
13411819Sjulian		}
13511819Sjulian	}
13611819Sjulian	ipxp->ipxp_rpt = ipx->ipx_pt;
13725652Sjhay	if (!(ipxp->ipxp_flags & IPXP_RAWIN) ) {
13825652Sjhay		m->m_len -= sizeof(struct ipx);
13925652Sjhay		m->m_pkthdr.len -= sizeof(struct ipx);
14025652Sjhay		m->m_data += sizeof(struct ipx);
14111819Sjulian	}
14211819Sjulian	if (sbappendaddr(&ipxp->ipxp_socket->so_rcv, (struct sockaddr *)&ipx_ipx,
14325652Sjhay	    m, (struct mbuf *)NULL) == 0)
14411819Sjulian		goto bad;
14511819Sjulian	sorwakeup(ipxp->ipxp_socket);
14611819Sjulian	return;
14711819Sjulianbad:
14811819Sjulian	m_freem(m);
14911819Sjulian}
15011819Sjulian
15111819Sjulianvoid
15211819Sjulianipx_abort(ipxp)
15311819Sjulian	struct ipxpcb *ipxp;
15411819Sjulian{
15511819Sjulian	struct socket *so = ipxp->ipxp_socket;
15611819Sjulian
15711819Sjulian	ipx_pcbdisconnect(ipxp);
15811819Sjulian	soisdisconnected(so);
15911819Sjulian}
16025652Sjhay
16111819Sjulian/*
16211819Sjulian * Drop connection, reporting
16311819Sjulian * the specified error.
16411819Sjulian */
16511819Sjulianvoid
16611819Sjulianipx_drop(ipxp, errno)
16711819Sjulian	register struct ipxpcb *ipxp;
16811819Sjulian	int errno;
16911819Sjulian{
17011819Sjulian	struct socket *so = ipxp->ipxp_socket;
17111819Sjulian
17211819Sjulian	/*
17325652Sjhay	 * someday, in the IPX world
17411819Sjulian	 * we will generate error protocol packets
17511819Sjulian	 * announcing that the socket has gone away.
17625652Sjhay	 *
17725652Sjhay	 * XXX Probably never. IPX does not have error packets.
17811819Sjulian	 */
17911819Sjulian	/*if (TCPS_HAVERCVDSYN(tp->t_state)) {
18011819Sjulian		tp->t_state = TCPS_CLOSED;
18125652Sjhay		tcp_output(tp);
18211819Sjulian	}*/
18311819Sjulian	so->so_error = errno;
18411819Sjulian	ipx_pcbdisconnect(ipxp);
18511819Sjulian	soisdisconnected(so);
18611819Sjulian}
18711819Sjulian
18825652Sjhaystatic int
18911819Sjulianipx_output(ipxp, m0)
19011819Sjulian	struct ipxpcb *ipxp;
19111819Sjulian	struct mbuf *m0;
19211819Sjulian{
19311819Sjulian	register struct mbuf *m;
19411819Sjulian	register struct ipx *ipx;
19511819Sjulian	register struct socket *so;
19611819Sjulian	register int len = 0;
19711819Sjulian	register struct route *ro;
19811819Sjulian	struct mbuf *mprev = NULL;
19911819Sjulian
20011819Sjulian	/*
20111819Sjulian	 * Calculate data length.
20211819Sjulian	 */
20325652Sjhay	for (m = m0; m != NULL; m = m->m_next) {
20411819Sjulian		mprev = m;
20511819Sjulian		len += m->m_len;
20611819Sjulian	}
20711819Sjulian	/*
20811819Sjulian	 * Make sure packet is actually of even length.
20911819Sjulian	 */
21011819Sjulian
21111819Sjulian	if (len & 1) {
21211819Sjulian		m = mprev;
21311819Sjulian		if ((m->m_flags & M_EXT) == 0 &&
21411819Sjulian			(m->m_len + m->m_data < &m->m_dat[MLEN])) {
21511819Sjulian			m->m_len++;
21611819Sjulian		} else {
21711819Sjulian			struct mbuf *m1 = m_get(M_DONTWAIT, MT_DATA);
21811819Sjulian
21925652Sjhay			if (m1 == NULL) {
22011819Sjulian				m_freem(m0);
22111819Sjulian				return (ENOBUFS);
22211819Sjulian			}
22311819Sjulian			m1->m_len = 1;
22411819Sjulian			* mtod(m1, char *) = 0;
22511819Sjulian			m->m_next = m1;
22611819Sjulian		}
22711819Sjulian		m0->m_pkthdr.len++;
22811819Sjulian	}
22911819Sjulian
23011819Sjulian	/*
23111819Sjulian	 * Fill in mbuf with extended IPX header
23211819Sjulian	 * and addresses and length put into network format.
23311819Sjulian	 */
23411819Sjulian	m = m0;
23511819Sjulian	if (ipxp->ipxp_flags & IPXP_RAWOUT) {
23611819Sjulian		ipx = mtod(m, struct ipx *);
23711819Sjulian	} else {
23825652Sjhay		M_PREPEND(m, sizeof(struct ipx), M_DONTWAIT);
23925652Sjhay		if (m == NULL)
24011819Sjulian			return (ENOBUFS);
24111819Sjulian		ipx = mtod(m, struct ipx *);
24211819Sjulian		ipx->ipx_tc = 0;
24311819Sjulian		ipx->ipx_pt = ipxp->ipxp_dpt;
24411819Sjulian		ipx->ipx_sna = ipxp->ipxp_laddr;
24511819Sjulian		ipx->ipx_dna = ipxp->ipxp_faddr;
24625652Sjhay		len += sizeof(struct ipx);
24711819Sjulian	}
24811819Sjulian
24911819Sjulian	ipx->ipx_len = htons((u_short)len);
25011819Sjulian
25111819Sjulian	if (ipxcksum) {
25211819Sjulian		ipx->ipx_sum = 0;
25311819Sjulian		len = ((len - 1) | 1) + 1;
25411819Sjulian		ipx->ipx_sum = ipx_cksum(m, len);
25511819Sjulian	} else
25611819Sjulian		ipx->ipx_sum = 0xffff;
25711819Sjulian
25811819Sjulian	/*
25911819Sjulian	 * Output datagram.
26011819Sjulian	 */
26111819Sjulian	so = ipxp->ipxp_socket;
26211819Sjulian	if (so->so_options & SO_DONTROUTE)
26325652Sjhay		return (ipx_outputfl(m, (struct route *)NULL,
26411819Sjulian		    (so->so_options & SO_BROADCAST) | IPX_ROUTETOIF));
26511819Sjulian	/*
26611819Sjulian	 * Use cached route for previous datagram if
26711819Sjulian	 * possible.  If the previous net was the same
26811819Sjulian	 * and the interface was a broadcast medium, or
26911819Sjulian	 * if the previous destination was identical,
27011819Sjulian	 * then we are ok.
27111819Sjulian	 *
27211819Sjulian	 * NB: We don't handle broadcasts because that
27311819Sjulian	 *     would require 3 subroutine calls.
27411819Sjulian	 */
27511819Sjulian	ro = &ipxp->ipxp_route;
27611819Sjulian#ifdef ancient_history
27711819Sjulian	/*
27811819Sjulian	 * I think that this will all be handled in ipx_pcbconnect!
27911819Sjulian	 */
28025652Sjhay	if (ro->ro_rt != NULL) {
28111819Sjulian		if(ipx_neteq(ipxp->ipxp_lastdst, ipx->ipx_dna)) {
28211819Sjulian			/*
28311819Sjulian			 * This assumes we have no GH type routes
28411819Sjulian			 */
28511819Sjulian			if (ro->ro_rt->rt_flags & RTF_HOST) {
28611819Sjulian				if (!ipx_hosteq(ipxp->ipxp_lastdst, ipx->ipx_dna))
28711819Sjulian					goto re_route;
28811819Sjulian
28911819Sjulian			}
29011819Sjulian			if ((ro->ro_rt->rt_flags & RTF_GATEWAY) == 0) {
29111819Sjulian				register struct ipx_addr *dst =
29211819Sjulian						&satoipx_addr(ro->ro_dst);
29311819Sjulian				dst->x_host = ipx->ipx_dna.x_host;
29411819Sjulian			}
29511819Sjulian			/*
29611819Sjulian			 * Otherwise, we go through the same gateway
29711819Sjulian			 * and dst is already set up.
29811819Sjulian			 */
29911819Sjulian		} else {
30011819Sjulian		re_route:
30111819Sjulian			RTFREE(ro->ro_rt);
30225652Sjhay			ro->ro_rt = NULL;
30311819Sjulian		}
30411819Sjulian	}
30511819Sjulian	ipxp->ipxp_lastdst = ipx->ipx_dna;
30611819Sjulian#endif /* ancient_history */
30711819Sjulian	return (ipx_outputfl(m, ro, so->so_options & SO_BROADCAST));
30811819Sjulian}
30919947Sjhay
31011819Sjulianint
31125345Sjhayipx_ctloutput(req, so, level, name, value, p)
31211819Sjulian	int req, level;
31311819Sjulian	struct socket *so;
31411819Sjulian	int name;
31511819Sjulian	struct mbuf **value;
31625345Sjhay	struct proc *p;
31711819Sjulian{
31811819Sjulian	register struct mbuf *m;
31911819Sjulian	struct ipxpcb *ipxp = sotoipxpcb(so);
32011819Sjulian	int mask, error = 0;
32111819Sjulian
32211819Sjulian	if (ipxp == NULL)
32311819Sjulian		return (EINVAL);
32411819Sjulian
32511819Sjulian	switch (req) {
32611819Sjulian
32711819Sjulian	case PRCO_GETOPT:
32825652Sjhay		if (value == NULL)
32911819Sjulian			return (EINVAL);
33011819Sjulian		m = m_get(M_DONTWAIT, MT_DATA);
33125652Sjhay		if (m == NULL)
33211819Sjulian			return (ENOBUFS);
33311819Sjulian		switch (name) {
33411819Sjulian
33511819Sjulian		case SO_ALL_PACKETS:
33611819Sjulian			mask = IPXP_ALL_PACKETS;
33711819Sjulian			goto get_flags;
33811819Sjulian
33911819Sjulian		case SO_HEADERS_ON_INPUT:
34011819Sjulian			mask = IPXP_RAWIN;
34111819Sjulian			goto get_flags;
34211819Sjulian
34311819Sjulian		case SO_HEADERS_ON_OUTPUT:
34411819Sjulian			mask = IPXP_RAWOUT;
34511819Sjulian		get_flags:
34611819Sjulian			m->m_len = sizeof(short);
34711819Sjulian			*mtod(m, short *) = ipxp->ipxp_flags & mask;
34811819Sjulian			break;
34911819Sjulian
35011819Sjulian		case SO_DEFAULT_HEADERS:
35111819Sjulian			m->m_len = sizeof(struct ipx);
35211819Sjulian			{
35311819Sjulian				register struct ipx *ipx = mtod(m, struct ipx *);
35411819Sjulian				ipx->ipx_len = 0;
35511819Sjulian				ipx->ipx_sum = 0;
35611819Sjulian				ipx->ipx_tc = 0;
35711819Sjulian				ipx->ipx_pt = ipxp->ipxp_dpt;
35811819Sjulian				ipx->ipx_dna = ipxp->ipxp_faddr;
35911819Sjulian				ipx->ipx_sna = ipxp->ipxp_laddr;
36011819Sjulian			}
36111819Sjulian			break;
36211819Sjulian
36311819Sjulian		case SO_SEQNO:
36411819Sjulian			m->m_len = sizeof(long);
36511819Sjulian			*mtod(m, long *) = ipx_pexseq++;
36611819Sjulian			break;
36711819Sjulian
36811819Sjulian		default:
36911819Sjulian			error = EINVAL;
37011819Sjulian		}
37111819Sjulian		*value = m;
37211819Sjulian		break;
37311819Sjulian
37411819Sjulian	case PRCO_SETOPT:
37511819Sjulian		switch (name) {
37611819Sjulian			int *ok;
37711819Sjulian
37811819Sjulian		case SO_ALL_PACKETS:
37911819Sjulian			mask = IPXP_ALL_PACKETS;
38011819Sjulian			goto set_head;
38111819Sjulian
38211819Sjulian		case SO_HEADERS_ON_INPUT:
38311819Sjulian			mask = IPXP_RAWIN;
38411819Sjulian			goto set_head;
38511819Sjulian
38611819Sjulian		case SO_HEADERS_ON_OUTPUT:
38711819Sjulian			mask = IPXP_RAWOUT;
38811819Sjulian		set_head:
38911819Sjulian			if (value && *value) {
39011819Sjulian				ok = mtod(*value, int *);
39111819Sjulian				if (*ok)
39211819Sjulian					ipxp->ipxp_flags |= mask;
39311819Sjulian				else
39411819Sjulian					ipxp->ipxp_flags &= ~mask;
39511819Sjulian			} else error = EINVAL;
39611819Sjulian			break;
39711819Sjulian
39811819Sjulian		case SO_DEFAULT_HEADERS:
39911819Sjulian			{
40011819Sjulian				register struct ipx *ipx
40111819Sjulian				    = mtod(*value, struct ipx *);
40211819Sjulian				ipxp->ipxp_dpt = ipx->ipx_pt;
40311819Sjulian			}
40411819Sjulian			break;
40511819Sjulian#ifdef IPXIP
40611819Sjulian		case SO_IPXIP_ROUTE:
40725345Sjhay			error = ipxip_route(so, *value, p);
40811819Sjulian			break;
40911819Sjulian#endif /* IPXIP */
41011819Sjulian#ifdef IPXTUNNEL
41111819Sjulian		case SO_IPXTUNNEL_ROUTE
41225345Sjhay			error = ipxtun_route(so, *value, p);
41311819Sjulian			break;
41411819Sjulian#endif
41511819Sjulian		default:
41611819Sjulian			error = EINVAL;
41711819Sjulian		}
41811819Sjulian		if (value && *value)
41911819Sjulian			m_freem(*value);
42011819Sjulian		break;
42111819Sjulian	}
42211819Sjulian	return (error);
42311819Sjulian}
42411819Sjulian
42524659Sjhaystatic int
42624659Sjhayipx_usr_abort(so)
42711819Sjulian	struct socket *so;
42811819Sjulian{
42924659Sjhay	int s;
43011819Sjulian	struct ipxpcb *ipxp = sotoipxpcb(so);
43111819Sjulian
43224659Sjhay	s = splnet();
43324659Sjhay	ipx_pcbdetach(ipxp);
43424659Sjhay	splx(s);
43524659Sjhay	sofree(so);
43624659Sjhay	soisdisconnected(so);
43724659Sjhay	return (0);
43824659Sjhay}
43911819Sjulian
44024659Sjhaystatic int
44125345Sjhayipx_attach(so, proto, p)
44224659Sjhay	struct socket *so;
44324659Sjhay	int proto;
44425345Sjhay	struct proc *p;
44524659Sjhay{
44624659Sjhay	int error;
44724659Sjhay	int s;
44824659Sjhay	struct ipxpcb *ipxp = sotoipxpcb(so);
44924659Sjhay
45024659Sjhay	if (ipxp != NULL)
45124659Sjhay		return (EINVAL);
45224659Sjhay	s = splnet();
45325345Sjhay	error = ipx_pcballoc(so, &ipxpcb, p);
45424659Sjhay	splx(s);
45524659Sjhay	if (error == 0)
45619947Sjhay		error = soreserve(so, ipxsendspace, ipxrecvspace);
45724659Sjhay	return (error);
45824659Sjhay}
45911819Sjulian
46024659Sjhaystatic int
46125345Sjhayipx_bind(so, nam, p)
46224659Sjhay	struct socket *so;
46328270Swollman	struct sockaddr *nam;
46425345Sjhay	struct proc *p;
46524659Sjhay{
46624659Sjhay	struct ipxpcb *ipxp = sotoipxpcb(so);
46711819Sjulian
46825345Sjhay	return (ipx_pcbbind(ipxp, nam, p));
46924659Sjhay}
47011819Sjulian
47124659Sjhaystatic int
47225345Sjhayipx_connect(so, nam, p)
47324659Sjhay	struct socket *so;
47428270Swollman	struct sockaddr *nam;
47525345Sjhay	struct proc *p;
47624659Sjhay{
47724659Sjhay	int error;
47824659Sjhay	int s;
47924659Sjhay	struct ipxpcb *ipxp = sotoipxpcb(so);
48011819Sjulian
48124659Sjhay	if (!ipx_nullhost(ipxp->ipxp_faddr))
48224659Sjhay		return (EISCONN);
48324659Sjhay	s = splnet();
48425345Sjhay	error = ipx_pcbconnect(ipxp, nam, p);
48524659Sjhay	splx(s);
48624659Sjhay	if (error == 0)
48724659Sjhay		soisconnected(so);
48824659Sjhay	return (error);
48924659Sjhay}
49011819Sjulian
49124659Sjhaystatic int
49224659Sjhayipx_detach(so)
49324659Sjhay	struct socket *so;
49424659Sjhay{
49524659Sjhay	int s;
49624659Sjhay	struct ipxpcb *ipxp = sotoipxpcb(so);
49711819Sjulian
49824659Sjhay	if (ipxp == NULL)
49924659Sjhay		return (ENOTCONN);
50024659Sjhay	s = splnet();
50124659Sjhay	ipx_pcbdetach(ipxp);
50224659Sjhay	splx(s);
50324659Sjhay	return (0);
50424659Sjhay}
50511819Sjulian
50624659Sjhaystatic int
50724659Sjhayipx_disconnect(so)
50824659Sjhay	struct socket *so;
50924659Sjhay{
51024659Sjhay	int s;
51124659Sjhay	struct ipxpcb *ipxp = sotoipxpcb(so);
51211819Sjulian
51324659Sjhay	if (ipx_nullhost(ipxp->ipxp_faddr))
51424659Sjhay		return (ENOTCONN);
51524659Sjhay	s = splnet();
51624659Sjhay	ipx_pcbdisconnect(ipxp);
51724659Sjhay	splx(s);
51824659Sjhay	soisdisconnected(so);
51924659Sjhay	return (0);
52024659Sjhay}
52111819Sjulian
52224659Sjhayint
52324659Sjhayipx_peeraddr(so, nam)
52424659Sjhay	struct socket *so;
52528270Swollman	struct sockaddr **nam;
52624659Sjhay{
52724659Sjhay	struct ipxpcb *ipxp = sotoipxpcb(so);
52811819Sjulian
52928270Swollman	ipx_setpeeraddr(ipxp, nam); /* XXX what if alloc fails? */
53024659Sjhay	return (0);
53124659Sjhay}
53224659Sjhay
53324659Sjhaystatic int
53425345Sjhayipx_send(so, flags, m, nam, control, p)
53524659Sjhay	struct socket *so;
53624659Sjhay	int flags;
53724659Sjhay	struct mbuf *m;
53828270Swollman	struct sockaddr *nam;
53924659Sjhay	struct mbuf *control;
54025345Sjhay	struct proc *p;
54124659Sjhay{
54224659Sjhay	int error;
54324659Sjhay	struct ipxpcb *ipxp = sotoipxpcb(so);
54424659Sjhay	struct ipx_addr laddr;
54524659Sjhay	int s = 0;
54624659Sjhay
54725652Sjhay	if (nam != NULL) {
54824659Sjhay		laddr = ipxp->ipxp_laddr;
54924659Sjhay		if (!ipx_nullhost(ipxp->ipxp_faddr)) {
55024659Sjhay			error = EISCONN;
55124659Sjhay			goto send_release;
55211819Sjulian		}
55324659Sjhay		/*
55424659Sjhay		 * Must block input while temporarily connected.
55524659Sjhay		 */
55624659Sjhay		s = splnet();
55725345Sjhay		error = ipx_pcbconnect(ipxp, nam, p);
55824659Sjhay		if (error) {
55911819Sjulian			splx(s);
56024659Sjhay			goto send_release;
56111819Sjulian		}
56224659Sjhay	} else {
56324659Sjhay		if (ipx_nullhost(ipxp->ipxp_faddr)) {
56424659Sjhay			error = ENOTCONN;
56524659Sjhay			goto send_release;
56624659Sjhay		}
56711819Sjulian	}
56824659Sjhay	error = ipx_output(ipxp, m);
56924659Sjhay	m = NULL;
57025652Sjhay	if (nam != NULL) {
57124659Sjhay		ipx_pcbdisconnect(ipxp);
57224659Sjhay		splx(s);
57324659Sjhay		ipxp->ipxp_laddr.x_host = laddr.x_host;
57424659Sjhay		ipxp->ipxp_laddr.x_port = laddr.x_port;
57524659Sjhay	}
57611819Sjulian
57724659Sjhaysend_release:
57811819Sjulian	if (m != NULL)
57911819Sjulian		m_freem(m);
58011819Sjulian	return (error);
58111819Sjulian}
58219947Sjhay
58324659Sjhaystatic int
58424659Sjhayipx_shutdown(so)
58524659Sjhay	struct socket *so;
58624659Sjhay{
58724659Sjhay	socantsendmore(so);
58824659Sjhay	return (0);
58924659Sjhay}
59024659Sjhay
59111819Sjulianint
59224659Sjhayipx_sockaddr(so, nam)
59311819Sjulian	struct socket *so;
59428270Swollman	struct sockaddr **nam;
59511819Sjulian{
59611819Sjulian	struct ipxpcb *ipxp = sotoipxpcb(so);
59711819Sjulian
59828270Swollman	ipx_setsockaddr(ipxp, nam); /* XXX what if alloc fails? */
59924659Sjhay	return (0);
60024659Sjhay}
60111819Sjulian
60224659Sjhaystatic int
60325345Sjhayripx_attach(so, proto, p)
60424659Sjhay	struct socket *so;
60524659Sjhay	int proto;
60625345Sjhay	struct proc *p;
60724659Sjhay{
60824659Sjhay	int error = 0;
60924659Sjhay	int s;
61024659Sjhay	struct ipxpcb *ipxp = sotoipxpcb(so);
61111819Sjulian
61225652Sjhay	if (p != NULL && (error = suser(p->p_ucred, &p->p_acflag)) != 0)
61325345Sjhay		return (error);
61424659Sjhay	s = splnet();
61525345Sjhay	error = ipx_pcballoc(so, &ipxrawpcb, p);
61624659Sjhay	splx(s);
61724659Sjhay	if (error)
61824659Sjhay		return (error);
61924659Sjhay	error = soreserve(so, ipxsendspace, ipxrecvspace);
62024659Sjhay	if (error)
62124659Sjhay		return (error);
62224659Sjhay	ipxp = sotoipxpcb(so);
62324659Sjhay	ipxp->ipxp_faddr.x_host = ipx_broadhost;
62424659Sjhay	ipxp->ipxp_flags = IPXP_RAWIN | IPXP_RAWOUT;
62511819Sjulian	return (error);
62611819Sjulian}
627