ipx_usrreq.c revision 139823
1/*-
2 * Copyright (c) 2004-2005 Robert N. M. Watson
3 * Copyright (c) 1995, Mike Mitchell
4 * Copyright (c) 1984, 1985, 1986, 1987, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by the University of
18 *	California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 *	@(#)ipx_usrreq.c
36 */
37
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/sys/netipx/ipx_usrreq.c 139823 2005-01-07 01:45:51Z imp $");
40
41#include "opt_ipx.h"
42
43#include <sys/param.h>
44#include <sys/kernel.h>
45#include <sys/lock.h>
46#include <sys/mbuf.h>
47#include <sys/protosw.h>
48#include <sys/signalvar.h>
49#include <sys/socket.h>
50#include <sys/socketvar.h>
51#include <sys/sx.h>
52#include <sys/sysctl.h>
53#include <sys/systm.h>
54
55#include <net/if.h>
56#include <net/route.h>
57
58#include <netinet/in.h>
59
60#include <netipx/ipx.h>
61#include <netipx/ipx_if.h>
62#include <netipx/ipx_ip.h>
63#include <netipx/ipx_pcb.h>
64#include <netipx/ipx_var.h>
65
66/*
67 * IPX protocol implementation.
68 */
69
70static int ipxsendspace = IPXSNDQ;
71SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxsendspace, CTLFLAG_RW,
72            &ipxsendspace, 0, "");
73static int ipxrecvspace = IPXRCVQ;
74SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxrecvspace, CTLFLAG_RW,
75            &ipxrecvspace, 0, "");
76
77static	int ipx_usr_abort(struct socket *so);
78static	int ipx_attach(struct socket *so, int proto, struct thread *td);
79static	int ipx_bind(struct socket *so, struct sockaddr *nam, struct thread *td);
80static	int ipx_connect(struct socket *so, struct sockaddr *nam,
81			struct thread *td);
82static	int ipx_detach(struct socket *so);
83static	int ipx_disconnect(struct socket *so);
84static	int ipx_send(struct socket *so, int flags, struct mbuf *m,
85		     struct sockaddr *addr, struct mbuf *control,
86		     struct thread *td);
87static	int ipx_shutdown(struct socket *so);
88static	int ripx_attach(struct socket *so, int proto, struct thread *td);
89static	int ipx_output(struct ipxpcb *ipxp, struct mbuf *m0);
90
91struct	pr_usrreqs ipx_usrreqs = {
92	.pru_abort =		ipx_usr_abort,
93	.pru_attach =		ipx_attach,
94	.pru_bind =		ipx_bind,
95	.pru_connect =		ipx_connect,
96	.pru_control =		ipx_control,
97	.pru_detach =		ipx_detach,
98	.pru_disconnect =	ipx_disconnect,
99	.pru_peeraddr =		ipx_peeraddr,
100	.pru_send =		ipx_send,
101	.pru_shutdown =		ipx_shutdown,
102	.pru_sockaddr =		ipx_sockaddr,
103};
104
105struct	pr_usrreqs ripx_usrreqs = {
106	.pru_abort =		ipx_usr_abort,
107	.pru_attach =		ripx_attach,
108	.pru_bind =		ipx_bind,
109	.pru_connect =		ipx_connect,
110	.pru_control =		ipx_control,
111	.pru_detach =		ipx_detach,
112	.pru_disconnect =	ipx_disconnect,
113	.pru_peeraddr =		ipx_peeraddr,
114	.pru_send =		ipx_send,
115	.pru_shutdown =		ipx_shutdown,
116	.pru_sockaddr =		ipx_sockaddr,
117};
118
119/*
120 *  This may also be called for raw listeners.
121 */
122void
123ipx_input(m, ipxp)
124	struct mbuf *m;
125	register struct ipxpcb *ipxp;
126{
127	register struct ipx *ipx = mtod(m, struct ipx *);
128	struct ifnet *ifp = m->m_pkthdr.rcvif;
129	struct sockaddr_ipx ipx_ipx;
130
131	KASSERT(ipxp != NULL, ("ipx_input: NULL ipxpcb"));
132	/*
133	 * Construct sockaddr format source address.
134	 * Stuff source address and datagram in user buffer.
135	 */
136	ipx_ipx.sipx_len = sizeof(ipx_ipx);
137	ipx_ipx.sipx_family = AF_IPX;
138	ipx_ipx.sipx_addr = ipx->ipx_sna;
139	ipx_ipx.sipx_zero[0] = '\0';
140	ipx_ipx.sipx_zero[1] = '\0';
141	if (ipx_neteqnn(ipx->ipx_sna.x_net, ipx_zeronet) && ifp != NULL) {
142		register struct ifaddr *ifa;
143
144		for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa != NULL;
145		     ifa = TAILQ_NEXT(ifa, ifa_link)) {
146			if (ifa->ifa_addr->sa_family == AF_IPX) {
147				ipx_ipx.sipx_addr.x_net =
148					IA_SIPX(ifa)->sipx_addr.x_net;
149				break;
150			}
151		}
152	}
153	ipxp->ipxp_rpt = ipx->ipx_pt;
154	if ((ipxp->ipxp_flags & IPXP_RAWIN) == 0) {
155		m->m_len -= sizeof(struct ipx);
156		m->m_pkthdr.len -= sizeof(struct ipx);
157		m->m_data += sizeof(struct ipx);
158	}
159	if (sbappendaddr(&ipxp->ipxp_socket->so_rcv,
160	    (struct sockaddr *)&ipx_ipx, m, NULL) == 0)
161		m_freem(m);
162	else
163		sorwakeup(ipxp->ipxp_socket);
164}
165
166/*
167 * Drop connection, reporting
168 * the specified error.
169 */
170void
171ipx_drop(ipxp, errno)
172	register struct ipxpcb *ipxp;
173	int errno;
174{
175	struct socket *so = ipxp->ipxp_socket;
176
177	/*
178	 * someday, in the IPX world
179	 * we will generate error protocol packets
180	 * announcing that the socket has gone away.
181	 *
182	 * XXX Probably never. IPX does not have error packets.
183	 */
184	/*if (TCPS_HAVERCVDSYN(tp->t_state)) {
185		tp->t_state = TCPS_CLOSED;
186		tcp_output(tp);
187	}*/
188	so->so_error = errno;
189	ipx_pcbdisconnect(ipxp);
190	soisdisconnected(so);
191}
192
193static int
194ipx_output(ipxp, m0)
195	struct ipxpcb *ipxp;
196	struct mbuf *m0;
197{
198	register struct ipx *ipx;
199	register struct socket *so;
200	register int len = 0;
201	register struct route *ro;
202	struct mbuf *m;
203	struct mbuf *mprev = NULL;
204
205	/*
206	 * Calculate data length.
207	 */
208	for (m = m0; m != NULL; m = m->m_next) {
209		mprev = m;
210		len += m->m_len;
211	}
212	/*
213	 * Make sure packet is actually of even length.
214	 */
215
216	if (len & 1) {
217		m = mprev;
218		if ((m->m_flags & M_EXT) == 0 &&
219			(m->m_len + m->m_data < &m->m_dat[MLEN])) {
220			mtod(m, char*)[m->m_len++] = 0;
221		} else {
222			struct mbuf *m1 = m_get(M_DONTWAIT, MT_DATA);
223
224			if (m1 == NULL) {
225				m_freem(m0);
226				return (ENOBUFS);
227			}
228			m1->m_len = 1;
229			* mtod(m1, char *) = 0;
230			m->m_next = m1;
231		}
232		m0->m_pkthdr.len++;
233	}
234
235	/*
236	 * Fill in mbuf with extended IPX header
237	 * and addresses and length put into network format.
238	 */
239	m = m0;
240	if (ipxp->ipxp_flags & IPXP_RAWOUT) {
241		ipx = mtod(m, struct ipx *);
242	} else {
243		M_PREPEND(m, sizeof(struct ipx), M_DONTWAIT);
244		if (m == NULL)
245			return (ENOBUFS);
246		ipx = mtod(m, struct ipx *);
247		ipx->ipx_tc = 0;
248		ipx->ipx_pt = ipxp->ipxp_dpt;
249		ipx->ipx_sna = ipxp->ipxp_laddr;
250		ipx->ipx_dna = ipxp->ipxp_faddr;
251		len += sizeof(struct ipx);
252	}
253
254	ipx->ipx_len = htons((u_short)len);
255
256	if (ipxp->ipxp_flags & IPXP_CHECKSUM) {
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(so, sopt)
315	struct socket *so;
316	struct sockopt *sopt;
317{
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_IPX_CHECKSUM:
339			mask = IPXP_CHECKSUM;
340			goto get_flags;
341
342		case SO_HEADERS_ON_OUTPUT:
343			mask = IPXP_RAWOUT;
344		get_flags:
345			soptval = ipxp->ipxp_flags & mask;
346			error = sooptcopyout(sopt, &soptval, sizeof soptval);
347			break;
348
349		case SO_DEFAULT_HEADERS:
350			ioptval.ipx_len = 0;
351			ioptval.ipx_sum = 0;
352			ioptval.ipx_tc = 0;
353			ioptval.ipx_pt = ipxp->ipxp_dpt;
354			ioptval.ipx_dna = ipxp->ipxp_faddr;
355			ioptval.ipx_sna = ipxp->ipxp_laddr;
356			error = sooptcopyout(sopt, &soptval, sizeof soptval);
357			break;
358
359		case SO_SEQNO:
360			error = sooptcopyout(sopt, &ipx_pexseq,
361					     sizeof ipx_pexseq);
362			ipx_pexseq++;
363			break;
364
365		default:
366			error = EINVAL;
367		}
368		break;
369
370	case SOPT_SET:
371		switch (sopt->sopt_name) {
372		case SO_ALL_PACKETS:
373			mask = IPXP_ALL_PACKETS;
374			goto set_head;
375
376		case SO_HEADERS_ON_INPUT:
377			mask = IPXP_RAWIN;
378			goto set_head;
379
380		case SO_IPX_CHECKSUM:
381			mask = IPXP_CHECKSUM;
382
383		case SO_HEADERS_ON_OUTPUT:
384			mask = IPXP_RAWOUT;
385		set_head:
386			error = sooptcopyin(sopt, &optval, sizeof optval,
387					    sizeof optval);
388			if (error)
389				break;
390			if (optval)
391				ipxp->ipxp_flags |= mask;
392			else
393				ipxp->ipxp_flags &= ~mask;
394			break;
395
396		case SO_DEFAULT_HEADERS:
397			error = sooptcopyin(sopt, &ioptval, sizeof ioptval,
398					    sizeof ioptval);
399			if (error)
400				break;
401			ipxp->ipxp_dpt = ioptval.ipx_pt;
402			break;
403#ifdef IPXIP
404		case SO_IPXIP_ROUTE:
405			error = ipxip_route(so, sopt);
406			break;
407#endif /* IPXIP */
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	soisdisconnected(so);
427	ACCEPT_LOCK();
428	SOCK_LOCK(so);
429	sotryfree(so);
430	return (0);
431}
432
433static int
434ipx_attach(so, proto, td)
435	struct socket *so;
436	int proto;
437	struct thread *td;
438{
439	int error;
440	int s;
441	struct ipxpcb *ipxp = sotoipxpcb(so);
442
443	if (ipxp != NULL)
444		return (EINVAL);
445	s = splnet();
446	error = ipx_pcballoc(so, &ipxpcb_list, td);
447	splx(s);
448	if (error == 0)
449		error = soreserve(so, ipxsendspace, ipxrecvspace);
450	return (error);
451}
452
453static int
454ipx_bind(so, nam, td)
455	struct socket *so;
456	struct sockaddr *nam;
457	struct thread *td;
458{
459	struct ipxpcb *ipxp = sotoipxpcb(so);
460
461	return (ipx_pcbbind(ipxp, nam, td));
462}
463
464static int
465ipx_connect(so, nam, td)
466	struct socket *so;
467	struct sockaddr *nam;
468	struct thread *td;
469{
470	int error;
471	int s;
472	struct ipxpcb *ipxp = sotoipxpcb(so);
473
474	if (!ipx_nullhost(ipxp->ipxp_faddr))
475		return (EISCONN);
476	s = splnet();
477	error = ipx_pcbconnect(ipxp, nam, td);
478	splx(s);
479	if (error == 0)
480		soisconnected(so);
481	return (error);
482}
483
484static int
485ipx_detach(so)
486	struct socket *so;
487{
488	int s;
489	struct ipxpcb *ipxp = sotoipxpcb(so);
490
491	if (ipxp == NULL)
492		return (ENOTCONN);
493	s = splnet();
494	ipx_pcbdetach(ipxp);
495	splx(s);
496	return (0);
497}
498
499static int
500ipx_disconnect(so)
501	struct socket *so;
502{
503	int s;
504	struct ipxpcb *ipxp = sotoipxpcb(so);
505
506	if (ipx_nullhost(ipxp->ipxp_faddr))
507		return (ENOTCONN);
508	s = splnet();
509	ipx_pcbdisconnect(ipxp);
510	splx(s);
511	soisdisconnected(so);
512	return (0);
513}
514
515int
516ipx_peeraddr(so, nam)
517	struct socket *so;
518	struct sockaddr **nam;
519{
520	struct ipxpcb *ipxp = sotoipxpcb(so);
521
522	ipx_setpeeraddr(ipxp, nam);
523	return (0);
524}
525
526static int
527ipx_send(so, flags, m, nam, control, td)
528	struct socket *so;
529	int flags;
530	struct mbuf *m;
531	struct sockaddr *nam;
532	struct mbuf *control;
533	struct thread *td;
534{
535	int error;
536	struct ipxpcb *ipxp = sotoipxpcb(so);
537	struct ipx_addr laddr;
538	int s = 0;
539
540	if (nam != NULL) {
541		laddr = ipxp->ipxp_laddr;
542		if (!ipx_nullhost(ipxp->ipxp_faddr)) {
543			error = EISCONN;
544			goto send_release;
545		}
546		/*
547		 * Must block input while temporarily connected.
548		 */
549		s = splnet();
550		error = ipx_pcbconnect(ipxp, nam, td);
551		if (error) {
552			splx(s);
553			goto send_release;
554		}
555	} else {
556		if (ipx_nullhost(ipxp->ipxp_faddr)) {
557			error = ENOTCONN;
558			goto send_release;
559		}
560	}
561	error = ipx_output(ipxp, m);
562	m = NULL;
563	if (nam != NULL) {
564		ipx_pcbdisconnect(ipxp);
565		splx(s);
566		ipxp->ipxp_laddr = laddr;
567	}
568
569send_release:
570	if (m != NULL)
571		m_freem(m);
572	return (error);
573}
574
575static int
576ipx_shutdown(so)
577	struct socket *so;
578{
579	socantsendmore(so);
580	return (0);
581}
582
583int
584ipx_sockaddr(so, nam)
585	struct socket *so;
586	struct sockaddr **nam;
587{
588	struct ipxpcb *ipxp = sotoipxpcb(so);
589
590	ipx_setsockaddr(ipxp, nam);
591	return (0);
592}
593
594static int
595ripx_attach(so, proto, td)
596	struct socket *so;
597	int proto;
598	struct thread *td;
599{
600	int error = 0;
601	int s;
602	struct ipxpcb *ipxp = sotoipxpcb(so);
603
604	if (td != NULL && (error = suser(td)) != 0)
605		return (error);
606	s = splnet();
607	error = ipx_pcballoc(so, &ipxrawpcb_list, td);
608	splx(s);
609	if (error)
610		return (error);
611	error = soreserve(so, ipxsendspace, ipxrecvspace);
612	if (error)
613		return (error);
614	ipxp = sotoipxpcb(so);
615	ipxp->ipxp_faddr.x_host = ipx_broadhost;
616	ipxp->ipxp_flags = IPXP_RAWIN | IPXP_RAWOUT;
617	return (error);
618}
619