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