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