1/*	$NetBSD: clnp_raw.c,v 1.33 2008/12/17 20:51:38 cegger Exp $	*/
2
3/*-
4 * Copyright (c) 1991, 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. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 *	@(#)clnp_raw.c	8.1 (Berkeley) 6/10/93
32 */
33
34/***********************************************************
35				Copyright IBM Corporation 1987
36
37                      All Rights Reserved
38
39Permission to use, copy, modify, and distribute this software and its
40documentation for any purpose and without fee is hereby granted,
41provided that the above copyright notice appear in all copies and that
42both that copyright notice and this permission notice appear in
43supporting documentation, and that the name of IBM not be
44used in advertising or publicity pertaining to distribution of the
45software without specific, written prior permission.
46
47IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
48ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
49IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
50ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
51WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
52ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
53SOFTWARE.
54
55******************************************************************/
56
57/*
58 * ARGO Project, Computer Sciences Dept., University of Wisconsin - Madison
59 */
60
61#include <sys/cdefs.h>
62__KERNEL_RCSID(0, "$NetBSD: clnp_raw.c,v 1.33 2008/12/17 20:51:38 cegger Exp $");
63
64#include <sys/param.h>
65#include <sys/mbuf.h>
66#include <sys/domain.h>
67#include <sys/protosw.h>
68#include <sys/socket.h>
69#include <sys/socketvar.h>
70#include <sys/errno.h>
71#include <sys/time.h>
72#include <sys/systm.h>
73#include <sys/proc.h>
74
75#include <net/if.h>
76#include <net/route.h>
77#include <net/raw_cb.h>
78
79#include <netiso/iso.h>
80#include <netiso/iso_pcb.h>
81#include <netiso/clnp.h>
82#include <netiso/clnp_stat.h>
83#include <netiso/argo_debug.h>
84
85#include <netiso/tp_user.h>	/* XXX -- defines SOL_NETWORK */
86
87struct sockproto rclnp_proto = {PF_ISO, 0};
88
89/*
90 * FUNCTION:		rclnp_input
91 *
92 * PURPOSE:		Setup generic address and protocol structures for
93 *			raw input routine, then pass them along with the
94 *			mbuf chain.
95 *
96 * RETURNS:		none
97 *
98 * SIDE EFFECTS:
99 *
100 * NOTES:		The protocol field of rclnp_proto is set to zero
101 *			indicating no protocol.
102 */
103void
104rclnp_input(struct mbuf *m, ...)
105{
106	struct sockaddr_iso *src;	/* ptr to src address */
107	struct sockaddr_iso *dst;	/* ptr to dest address */
108	int             hdrlen;	/* length (in bytes) of clnp header */
109	va_list ap;
110
111	va_start(ap, m);
112	src = va_arg(ap, struct sockaddr_iso *);
113	dst = va_arg(ap, struct sockaddr_iso *);
114	hdrlen = va_arg(ap, int);
115	va_end(ap);
116#ifdef	TROLL
117	if (trollctl.tr_ops & TR_CHUCK) {
118		m_freem(m);
119		return;
120	}
121#endif				/* TROLL */
122
123	raw_input(m, &rclnp_proto, sisotosa(src), sisotosa(dst));
124}
125
126/*
127 * FUNCTION:		rclnp_output
128 *
129 * PURPOSE:			Prepare to send a raw clnp packet. Setup src and dest
130 *					addresses, count the number of bytes to send, and
131 *					call clnp_output.
132 *
133 * RETURNS:			success - 0
134 *					failure - an appropriate error code
135 *
136 * SIDE EFFECTS:
137 *
138 * NOTES:
139 */
140int
141rclnp_output(struct mbuf *m0, ...)
142{
143	struct socket  *so;	/* socket to send from */
144	struct rawisopcb *rp;	/* ptr to raw cb */
145	int             error;	/* return value of function */
146	int             flags;	/* flags for clnp_output */
147	va_list ap;
148
149	va_start(ap, m0);
150	so = va_arg(ap, struct socket *);
151	va_end(ap);
152	rp = sotorawisopcb(so);
153
154	if ((m0->m_flags & M_PKTHDR) == 0)
155		return (EINVAL);
156	/*
157	 * Set up src address. If user has bound socket to an address, use it.
158	 * Otherwise, do not specify src (clnp_output will fill it in).
159	 */
160	if (rp->risop_rcb.rcb_laddr) {
161		if (rp->risop_isop.isop_sladdr.siso_family != AF_ISO) {
162	bad:
163			m_freem(m0);
164			return (EAFNOSUPPORT);
165		}
166	}
167	/* set up dest address */
168	if (rp->risop_rcb.rcb_faddr == 0)
169		goto bad;
170	rp->risop_isop.isop_sfaddr = *satosiso(rp->risop_rcb.rcb_faddr);
171	rp->risop_isop.isop_faddr = &rp->risop_isop.isop_sfaddr;
172
173	/* get flags and ship it off */
174	flags = rp->risop_flags & CLNP_VFLAGS;
175
176	error = clnp_output(m0, &rp->risop_isop, m0->m_pkthdr.len,
177			    flags | CLNP_NOCACHE);
178
179	return (error);
180}
181
182/*
183 * FUNCTION:		rclnp_ctloutput
184 *
185 * PURPOSE:			Raw clnp socket option processing
186 *				All options are stored inside a sockopt.
187 *
188 * RETURNS:			success - 0
189 *					failure - unix error code
190 *
191 * SIDE EFFECTS:
192 *
193 * NOTES:
194 */
195int
196rclnp_ctloutput(
197	int             op,	/* type of operation */
198	struct socket  *so,	/* ptr to socket */
199	struct sockopt *sopt)	/* socket options */
200{
201	int             error = 0;
202	struct rawisopcb *rp = sotorawisopcb(so);	/* raw cb ptr */
203
204#ifdef ARGO_DEBUG
205	if (argo_debug[D_CTLOUTPUT]) {
206		printf("rclnp_ctloutput: op = x%x, level = x%x, name = x%x\n",
207		    op, sopt->sopt_level, sopt->sopt_name);
208		printf("rclnp_ctloutput: %zu bytes of data\n", sopt->sopt_size);
209		dump_buf(sopt->sopt_data, sopt->sopt_size);
210	}
211#endif
212
213#ifdef SOL_NETWORK
214	if (sopt->sopt_level != SOL_NETWORK)
215		return (EINVAL);
216#endif
217
218	switch (op) {
219	case PRCO_SETOPT:
220		switch (sopt->sopt_name) {
221		case CLNPOPT_FLAGS:{
222			u_short flags;
223
224			error = sockopt_get(sopt, &flags, sizeof(flags));
225			if (error)
226				break;
227
228			/*
229			 *	Don't allow invalid flags to be set
230			 */
231			if ((flags & (CLNP_VFLAGS)) != flags)
232				error = EINVAL;
233			else
234				rp->risop_flags |= flags;
235
236			break;
237			}
238
239		case CLNPOPT_OPTS: {
240			struct mbuf *m;
241
242			m = sockopt_getmbuf(sopt);
243			if (m == NULL) {
244				error = ENOBUFS;
245				break;
246			}
247
248			error = clnp_set_opts(&rp->risop_isop.isop_options, &m);
249			m_freem(m);
250			if (error)
251				break;
252			rp->risop_isop.isop_optindex = m_get(M_WAIT, MT_SOOPTS);
253			(void) clnp_opt_sanity(rp->risop_isop.isop_options,
254				 mtod(rp->risop_isop.isop_options, void *),
255					 rp->risop_isop.isop_options->m_len,
256					  mtod(rp->risop_isop.isop_optindex,
257					       struct clnp_optidx *));
258			break;
259			}
260		}
261		break;
262
263	case PRCO_GETOPT:
264#ifdef notdef
265		/* commented out to keep hi C quiet */
266		switch (sopt->sopt_name) {
267		default:
268			error = EINVAL;
269			break;
270		}
271#endif				/* notdef */
272		break;
273	default:
274		error = EINVAL;
275		break;
276	}
277	return error;
278}
279
280/* ARGSUSED */
281int
282clnp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
283	struct mbuf *control, struct lwp *l)
284{
285	int    error = 0;
286	struct rawisopcb *rp = sotorawisopcb(so);
287	struct proc *p;
288
289	p = l ? l->l_proc : NULL;
290	rp = sotorawisopcb(so);
291	switch (req) {
292
293	case PRU_ATTACH:
294		sosetlock(so);
295		if (rp != 0) {
296			error = EISCONN;
297			break;
298		}
299		rp = malloc(sizeof(*rp), M_PCB, M_WAITOK|M_ZERO);
300		if (rp == 0)
301			return (ENOBUFS);
302		so->so_pcb = rp;
303		break;
304
305	case PRU_DETACH:
306		if (rp->risop_isop.isop_options)
307			m_freem(rp->risop_isop.isop_options);
308		rtcache_free(&rp->risop_isop.isop_route);
309		if (rp->risop_rcb.rcb_laddr)
310			rp->risop_rcb.rcb_laddr = 0;
311		/* free clnp cached hdr if necessary */
312		if (rp->risop_isop.isop_clnpcache != NULL) {
313			struct clnp_cache *clcp =
314			mtod(rp->risop_isop.isop_clnpcache, struct clnp_cache *);
315			if (clcp->clc_hdr != NULL) {
316				m_free(clcp->clc_hdr);
317			}
318			m_free(rp->risop_isop.isop_clnpcache);
319		}
320		if (rp->risop_isop.isop_optindex != NULL)
321			m_free(rp->risop_isop.isop_optindex);
322
323		break;
324
325	case PRU_BIND:
326		{
327			struct sockaddr_iso *addr = mtod(nam, struct sockaddr_iso *);
328
329			if (nam->m_len != sizeof(*addr))
330				return (EINVAL);
331			if ((ifnet.tqh_first == 0) ||
332			    (addr->siso_family != AF_ISO) ||
333			    (addr->siso_addr.isoa_len &&
334			     ifa_ifwithaddr(sisotosa(addr)) == 0))
335				return (EADDRNOTAVAIL);
336			rp->risop_isop.isop_sladdr = *addr;
337			rp->risop_rcb.rcb_laddr = sisotosa(
338							   (rp->risop_isop.isop_laddr = &rp->risop_isop.isop_sladdr));
339			return (0);
340		}
341	case PRU_CONNECT:
342		{
343			struct sockaddr_iso *addr = mtod(nam, struct sockaddr_iso *);
344
345			if ((nam->m_len > sizeof(*addr)) || (addr->siso_len > sizeof(*addr)))
346				return (EINVAL);
347			if (ifnet.tqh_first == 0)
348				return (EADDRNOTAVAIL);
349
350			/* copy the address */
351			if (addr->siso_family != AF_ISO)
352				rp->risop_isop.isop_sfaddr = *addr;
353
354			/* initialize address pointers */
355			rp->risop_isop.isop_faddr = &rp->risop_isop.isop_sfaddr;
356			rp->risop_rcb.rcb_faddr = sisotosa(
357				rp->risop_isop.isop_faddr);
358
359			/* address setup, mark socket connected */
360			soisconnected(so);
361
362			return (0);
363		}
364	}
365	error = raw_usrreq(so, req, m, nam, control, l);
366
367	if (error && req == PRU_ATTACH && so->so_pcb)
368		free((void *) rp, M_PCB);
369	return (error);
370}
371