ipx_usrreq.c revision 25652
1/*
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.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
21 *
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
32 * SUCH DAMAGE.
33 *
34 *	@(#)ipx_usrreq.c
35 *
36 * $Id: ipx_usrreq.c,v 1.13 1997/05/01 06:21:30 jhay Exp $
37 */
38
39#include <sys/param.h>
40#include <sys/queue.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/malloc.h>
44#include <sys/mbuf.h>
45#include <sys/proc.h>
46#include <sys/protosw.h>
47#include <sys/socket.h>
48#include <sys/socketvar.h>
49#include <sys/errno.h>
50#include <sys/stat.h>
51#include <sys/sysctl.h>
52
53#include <net/if.h>
54#include <net/route.h>
55
56#include <netinet/in.h>
57
58#include <netipx/ipx.h>
59#include <netipx/ipx_pcb.h>
60#include <netipx/ipx_if.h>
61#include <netipx/ipx_var.h>
62#include <netipx/ipx_error.h>
63#include <netipx/ipx_ip.h>
64
65/*
66 * IPX protocol implementation.
67 */
68
69int ipxsendspace = IPXSNDQ;
70SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxsendspace, CTLFLAG_RW,
71            &ipxsendspace, 0, "");
72int ipxrecvspace = IPXRCVQ;
73SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxrecvspace, CTLFLAG_RW,
74            &ipxrecvspace, 0, "");
75
76static	int ipx_usr_abort(struct socket *so);
77static	int ipx_attach(struct socket *so, int proto, struct proc *p);
78static	int ipx_bind(struct socket *so, struct mbuf *nam, struct proc *p);
79static	int ipx_connect(struct socket *so, struct mbuf *nam, struct proc *p);
80static	int ipx_detach(struct socket *so);
81static	int ipx_disconnect(struct socket *so);
82static	int ipx_send(struct socket *so, int flags, struct mbuf *m,
83		     struct mbuf *addr, struct mbuf *control, struct proc *p);
84static	int ipx_shutdown(struct socket *so);
85static	int ripx_attach(struct socket *so, int proto, struct proc *p);
86static	int ipx_output(struct ipxpcb *ipxp, struct mbuf *m0);
87
88struct	pr_usrreqs ipx_usrreqs = {
89	ipx_usr_abort, pru_accept_notsupp, ipx_attach, ipx_bind,
90	ipx_connect, pru_connect2_notsupp, ipx_control, ipx_detach,
91	ipx_disconnect, pru_listen_notsupp, ipx_peeraddr, pru_rcvd_notsupp,
92	pru_rcvoob_notsupp, ipx_send, pru_sense_null, ipx_shutdown,
93	ipx_sockaddr, sosend, soreceive, soselect
94};
95
96struct	pr_usrreqs ripx_usrreqs = {
97	ipx_usr_abort, pru_accept_notsupp, ripx_attach, ipx_bind,
98	ipx_connect, pru_connect2_notsupp, ipx_control, ipx_detach,
99	ipx_disconnect, pru_listen_notsupp, ipx_peeraddr, pru_rcvd_notsupp,
100	pru_rcvoob_notsupp, ipx_send, pru_sense_null, ipx_shutdown,
101	ipx_sockaddr, sosend, soreceive, soselect
102};
103
104/*
105 *  This may also be called for raw listeners.
106 */
107void
108ipx_input(m, ipxp)
109	struct mbuf *m;
110	register struct ipxpcb *ipxp;
111{
112	register struct ipx *ipx = mtod(m, struct ipx *);
113	struct ifnet *ifp = m->m_pkthdr.rcvif;
114	struct sockaddr_ipx ipx_ipx;
115
116	if (ipxp == NULL)
117		panic("No ipxpcb");
118	/*
119	 * Construct sockaddr format source address.
120	 * Stuff source address and datagram in user buffer.
121	 */
122	ipx_ipx.sipx_len = sizeof(ipx_ipx);
123	ipx_ipx.sipx_family = AF_IPX;
124	ipx_ipx.sipx_addr = ipx->ipx_sna;
125	ipx_ipx.sipx_zero[0] = '\0';
126	ipx_ipx.sipx_zero[1] = '\0';
127	if (ipx_neteqnn(ipx->ipx_sna.x_net, ipx_zeronet) && ifp != NULL) {
128		register struct ifaddr *ifa;
129
130		for (ifa = ifp->if_addrhead.tqh_first; ifa != NULL;
131		     ifa = ifa->ifa_link.tqe_next) {
132			if (ifa->ifa_addr->sa_family == AF_IPX) {
133				ipx_ipx.sipx_addr.x_net =
134					IA_SIPX(ifa)->sipx_addr.x_net;
135				break;
136			}
137		}
138	}
139	ipxp->ipxp_rpt = ipx->ipx_pt;
140	if (!(ipxp->ipxp_flags & IPXP_RAWIN) ) {
141		m->m_len -= sizeof(struct ipx);
142		m->m_pkthdr.len -= sizeof(struct ipx);
143		m->m_data += sizeof(struct ipx);
144	}
145	if (sbappendaddr(&ipxp->ipxp_socket->so_rcv, (struct sockaddr *)&ipx_ipx,
146	    m, (struct mbuf *)NULL) == 0)
147		goto bad;
148	sorwakeup(ipxp->ipxp_socket);
149	return;
150bad:
151	m_freem(m);
152}
153
154void
155ipx_abort(ipxp)
156	struct ipxpcb *ipxp;
157{
158	struct socket *so = ipxp->ipxp_socket;
159
160	ipx_pcbdisconnect(ipxp);
161	soisdisconnected(so);
162}
163
164/*
165 * Drop connection, reporting
166 * the specified error.
167 */
168void
169ipx_drop(ipxp, errno)
170	register struct ipxpcb *ipxp;
171	int errno;
172{
173	struct socket *so = ipxp->ipxp_socket;
174
175	/*
176	 * someday, in the IPX world
177	 * we will generate error protocol packets
178	 * announcing that the socket has gone away.
179	 *
180	 * XXX Probably never. IPX does not have error packets.
181	 */
182	/*if (TCPS_HAVERCVDSYN(tp->t_state)) {
183		tp->t_state = TCPS_CLOSED;
184		tcp_output(tp);
185	}*/
186	so->so_error = errno;
187	ipx_pcbdisconnect(ipxp);
188	soisdisconnected(so);
189}
190
191static int
192ipx_output(ipxp, m0)
193	struct ipxpcb *ipxp;
194	struct mbuf *m0;
195{
196	register struct mbuf *m;
197	register struct ipx *ipx;
198	register struct socket *so;
199	register int len = 0;
200	register struct route *ro;
201	struct mbuf *mprev = NULL;
202
203	/*
204	 * Calculate data length.
205	 */
206	for (m = m0; m != NULL; m = m->m_next) {
207		mprev = m;
208		len += m->m_len;
209	}
210	/*
211	 * Make sure packet is actually of even length.
212	 */
213
214	if (len & 1) {
215		m = mprev;
216		if ((m->m_flags & M_EXT) == 0 &&
217			(m->m_len + m->m_data < &m->m_dat[MLEN])) {
218			m->m_len++;
219		} else {
220			struct mbuf *m1 = m_get(M_DONTWAIT, MT_DATA);
221
222			if (m1 == NULL) {
223				m_freem(m0);
224				return (ENOBUFS);
225			}
226			m1->m_len = 1;
227			* mtod(m1, char *) = 0;
228			m->m_next = m1;
229		}
230		m0->m_pkthdr.len++;
231	}
232
233	/*
234	 * Fill in mbuf with extended IPX header
235	 * and addresses and length put into network format.
236	 */
237	m = m0;
238	if (ipxp->ipxp_flags & IPXP_RAWOUT) {
239		ipx = mtod(m, struct ipx *);
240	} else {
241		M_PREPEND(m, sizeof(struct ipx), M_DONTWAIT);
242		if (m == NULL)
243			return (ENOBUFS);
244		ipx = mtod(m, struct ipx *);
245		ipx->ipx_tc = 0;
246		ipx->ipx_pt = ipxp->ipxp_dpt;
247		ipx->ipx_sna = ipxp->ipxp_laddr;
248		ipx->ipx_dna = ipxp->ipxp_faddr;
249		len += sizeof(struct ipx);
250	}
251
252	ipx->ipx_len = htons((u_short)len);
253
254	if (ipxcksum) {
255		ipx->ipx_sum = 0;
256		len = ((len - 1) | 1) + 1;
257		ipx->ipx_sum = ipx_cksum(m, len);
258	} else
259		ipx->ipx_sum = 0xffff;
260
261	/*
262	 * Output datagram.
263	 */
264	so = ipxp->ipxp_socket;
265	if (so->so_options & SO_DONTROUTE)
266		return (ipx_outputfl(m, (struct route *)NULL,
267		    (so->so_options & SO_BROADCAST) | IPX_ROUTETOIF));
268	/*
269	 * Use cached route for previous datagram if
270	 * possible.  If the previous net was the same
271	 * and the interface was a broadcast medium, or
272	 * if the previous destination was identical,
273	 * then we are ok.
274	 *
275	 * NB: We don't handle broadcasts because that
276	 *     would require 3 subroutine calls.
277	 */
278	ro = &ipxp->ipxp_route;
279#ifdef ancient_history
280	/*
281	 * I think that this will all be handled in ipx_pcbconnect!
282	 */
283	if (ro->ro_rt != NULL) {
284		if(ipx_neteq(ipxp->ipxp_lastdst, ipx->ipx_dna)) {
285			/*
286			 * This assumes we have no GH type routes
287			 */
288			if (ro->ro_rt->rt_flags & RTF_HOST) {
289				if (!ipx_hosteq(ipxp->ipxp_lastdst, ipx->ipx_dna))
290					goto re_route;
291
292			}
293			if ((ro->ro_rt->rt_flags & RTF_GATEWAY) == 0) {
294				register struct ipx_addr *dst =
295						&satoipx_addr(ro->ro_dst);
296				dst->x_host = ipx->ipx_dna.x_host;
297			}
298			/*
299			 * Otherwise, we go through the same gateway
300			 * and dst is already set up.
301			 */
302		} else {
303		re_route:
304			RTFREE(ro->ro_rt);
305			ro->ro_rt = NULL;
306		}
307	}
308	ipxp->ipxp_lastdst = ipx->ipx_dna;
309#endif /* ancient_history */
310	return (ipx_outputfl(m, ro, so->so_options & SO_BROADCAST));
311}
312
313int
314ipx_ctloutput(req, so, level, name, value, p)
315	int req, level;
316	struct socket *so;
317	int name;
318	struct mbuf **value;
319	struct proc *p;
320{
321	register struct mbuf *m;
322	struct ipxpcb *ipxp = sotoipxpcb(so);
323	int mask, error = 0;
324
325	if (ipxp == NULL)
326		return (EINVAL);
327
328	switch (req) {
329
330	case PRCO_GETOPT:
331		if (value == NULL)
332			return (EINVAL);
333		m = m_get(M_DONTWAIT, MT_DATA);
334		if (m == NULL)
335			return (ENOBUFS);
336		switch (name) {
337
338		case SO_ALL_PACKETS:
339			mask = IPXP_ALL_PACKETS;
340			goto get_flags;
341
342		case SO_HEADERS_ON_INPUT:
343			mask = IPXP_RAWIN;
344			goto get_flags;
345
346		case SO_HEADERS_ON_OUTPUT:
347			mask = IPXP_RAWOUT;
348		get_flags:
349			m->m_len = sizeof(short);
350			*mtod(m, short *) = ipxp->ipxp_flags & mask;
351			break;
352
353		case SO_DEFAULT_HEADERS:
354			m->m_len = sizeof(struct ipx);
355			{
356				register struct ipx *ipx = mtod(m, struct ipx *);
357				ipx->ipx_len = 0;
358				ipx->ipx_sum = 0;
359				ipx->ipx_tc = 0;
360				ipx->ipx_pt = ipxp->ipxp_dpt;
361				ipx->ipx_dna = ipxp->ipxp_faddr;
362				ipx->ipx_sna = ipxp->ipxp_laddr;
363			}
364			break;
365
366		case SO_SEQNO:
367			m->m_len = sizeof(long);
368			*mtod(m, long *) = ipx_pexseq++;
369			break;
370
371		default:
372			error = EINVAL;
373		}
374		*value = m;
375		break;
376
377	case PRCO_SETOPT:
378		switch (name) {
379			int *ok;
380
381		case SO_ALL_PACKETS:
382			mask = IPXP_ALL_PACKETS;
383			goto set_head;
384
385		case SO_HEADERS_ON_INPUT:
386			mask = IPXP_RAWIN;
387			goto set_head;
388
389		case SO_HEADERS_ON_OUTPUT:
390			mask = IPXP_RAWOUT;
391		set_head:
392			if (value && *value) {
393				ok = mtod(*value, int *);
394				if (*ok)
395					ipxp->ipxp_flags |= mask;
396				else
397					ipxp->ipxp_flags &= ~mask;
398			} else error = EINVAL;
399			break;
400
401		case SO_DEFAULT_HEADERS:
402			{
403				register struct ipx *ipx
404				    = mtod(*value, struct ipx *);
405				ipxp->ipxp_dpt = ipx->ipx_pt;
406			}
407			break;
408#ifdef IPXIP
409		case SO_IPXIP_ROUTE:
410			error = ipxip_route(so, *value, p);
411			break;
412#endif /* IPXIP */
413#ifdef IPXTUNNEL
414		case SO_IPXTUNNEL_ROUTE
415			error = ipxtun_route(so, *value, p);
416			break;
417#endif
418		default:
419			error = EINVAL;
420		}
421		if (value && *value)
422			m_freem(*value);
423		break;
424	}
425	return (error);
426}
427
428static int
429ipx_usr_abort(so)
430	struct socket *so;
431{
432	int s;
433	struct ipxpcb *ipxp = sotoipxpcb(so);
434
435	s = splnet();
436	ipx_pcbdetach(ipxp);
437	splx(s);
438	sofree(so);
439	soisdisconnected(so);
440	return (0);
441}
442
443static int
444ipx_attach(so, proto, p)
445	struct socket *so;
446	int proto;
447	struct proc *p;
448{
449	int error;
450	int s;
451	struct ipxpcb *ipxp = sotoipxpcb(so);
452
453	if (ipxp != NULL)
454		return (EINVAL);
455	s = splnet();
456	error = ipx_pcballoc(so, &ipxpcb, p);
457	splx(s);
458	if (error == 0)
459		error = soreserve(so, ipxsendspace, ipxrecvspace);
460	return (error);
461}
462
463static int
464ipx_bind(so, nam, p)
465	struct socket *so;
466	struct mbuf *nam;
467	struct proc *p;
468{
469	struct ipxpcb *ipxp = sotoipxpcb(so);
470
471	return (ipx_pcbbind(ipxp, nam, p));
472}
473
474static int
475ipx_connect(so, nam, p)
476	struct socket *so;
477	struct mbuf *nam;
478	struct proc *p;
479{
480	int error;
481	int s;
482	struct ipxpcb *ipxp = sotoipxpcb(so);
483
484	if (!ipx_nullhost(ipxp->ipxp_faddr))
485		return (EISCONN);
486	s = splnet();
487	error = ipx_pcbconnect(ipxp, nam, p);
488	splx(s);
489	if (error == 0)
490		soisconnected(so);
491	return (error);
492}
493
494static int
495ipx_detach(so)
496	struct socket *so;
497{
498	int s;
499	struct ipxpcb *ipxp = sotoipxpcb(so);
500
501	if (ipxp == NULL)
502		return (ENOTCONN);
503	s = splnet();
504	ipx_pcbdetach(ipxp);
505	splx(s);
506	return (0);
507}
508
509static int
510ipx_disconnect(so)
511	struct socket *so;
512{
513	int s;
514	struct ipxpcb *ipxp = sotoipxpcb(so);
515
516	if (ipx_nullhost(ipxp->ipxp_faddr))
517		return (ENOTCONN);
518	s = splnet();
519	ipx_pcbdisconnect(ipxp);
520	splx(s);
521	soisdisconnected(so);
522	return (0);
523}
524
525int
526ipx_peeraddr(so, nam)
527	struct socket *so;
528	struct mbuf *nam;
529{
530	struct ipxpcb *ipxp = sotoipxpcb(so);
531
532	ipx_setpeeraddr(ipxp, nam);
533	return (0);
534}
535
536static int
537ipx_send(so, flags, m, nam, control, p)
538	struct socket *so;
539	int flags;
540	struct mbuf *m;
541	struct mbuf *nam;
542	struct mbuf *control;
543	struct proc *p;
544{
545	int error;
546	struct ipxpcb *ipxp = sotoipxpcb(so);
547	struct ipx_addr laddr;
548	int s = 0;
549
550	if (nam != NULL) {
551		laddr = ipxp->ipxp_laddr;
552		if (!ipx_nullhost(ipxp->ipxp_faddr)) {
553			error = EISCONN;
554			goto send_release;
555		}
556		/*
557		 * Must block input while temporarily connected.
558		 */
559		s = splnet();
560		error = ipx_pcbconnect(ipxp, nam, p);
561		if (error) {
562			splx(s);
563			goto send_release;
564		}
565	} else {
566		if (ipx_nullhost(ipxp->ipxp_faddr)) {
567			error = ENOTCONN;
568			goto send_release;
569		}
570	}
571	error = ipx_output(ipxp, m);
572	m = NULL;
573	if (nam != NULL) {
574		ipx_pcbdisconnect(ipxp);
575		splx(s);
576		ipxp->ipxp_laddr.x_host = laddr.x_host;
577		ipxp->ipxp_laddr.x_port = laddr.x_port;
578	}
579
580send_release:
581	if (m != NULL)
582		m_freem(m);
583	return (error);
584}
585
586static int
587ipx_shutdown(so)
588	struct socket *so;
589{
590	socantsendmore(so);
591	return (0);
592}
593
594int
595ipx_sockaddr(so, nam)
596	struct socket *so;
597	struct mbuf *nam;
598{
599	struct ipxpcb *ipxp = sotoipxpcb(so);
600
601	ipx_setsockaddr(ipxp, nam);
602	return (0);
603}
604
605static int
606ripx_attach(so, proto, p)
607	struct socket *so;
608	int proto;
609	struct proc *p;
610{
611	int error = 0;
612	int s;
613	struct ipxpcb *ipxp = sotoipxpcb(so);
614
615	if (p != NULL && (error = suser(p->p_ucred, &p->p_acflag)) != 0)
616		return (error);
617	s = splnet();
618	error = ipx_pcballoc(so, &ipxrawpcb, p);
619	splx(s);
620	if (error)
621		return (error);
622	error = soreserve(so, ipxsendspace, ipxrecvspace);
623	if (error)
624		return (error);
625	ipxp = sotoipxpcb(so);
626	ipxp->ipxp_faddr.x_host = ipx_broadhost;
627	ipxp->ipxp_flags = IPXP_RAWIN | IPXP_RAWOUT;
628	return (error);
629}
630