1/*-
2 * Copyright (c) 2005-2006 Robert N. M. Watson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * Copyright (c) 1996 Charles D. Cranor and Washington University.
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 * 1. Redistributions of source code must retain the above copyright
33 *    notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 *    notice, this list of conditions and the following disclaimer in the
36 *    documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 *    must display the following acknowledgement:
39 *      This product includes software developed by Charles D. Cranor and
40 *      Washington University.
41 * 4. The name of the author may not be used to endorse or promote products
42 *    derived from this software without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
45 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
48 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
49 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
53 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 *
55 * $NetBSD: natm.c,v 1.5 1996/11/09 03:26:26 chuck Exp $
56 */
57
58/*
59 * natm.c: Native mode ATM access (both aal0 and aal5).
60 */
61
62#include <sys/cdefs.h>
63__FBSDID("$FreeBSD$");
64
65#include <sys/param.h>
66#include <sys/conf.h>
67#include <sys/kernel.h>
68#include <sys/lock.h>
69#include <sys/malloc.h>
70#include <sys/mbuf.h>
71#include <sys/protosw.h>
72#include <sys/signalvar.h>
73#include <sys/socket.h>
74#include <sys/socketvar.h>
75#include <sys/sockio.h>
76#include <sys/sx.h>
77#include <sys/systm.h>
78#include <sys/sysctl.h>
79
80#include <net/if.h>
81#include <net/if_var.h>
82#include <net/if_atm.h>
83#include <net/netisr.h>
84
85#include <netinet/in.h>
86
87#include <netnatm/natm.h>
88
89static const u_long	natm5_sendspace = 16*1024;
90static const u_long	natm5_recvspace = 16*1024;
91
92static const u_long	natm0_sendspace = 16*1024;
93static const u_long	natm0_recvspace = 16*1024;
94
95/*
96 * netnatm global subsystem lock, protects all global data structures in
97 * netnatm.
98 */
99struct mtx	natm_mtx;
100
101/*
102 * User socket requests.
103 */
104static int	natm_usr_attach(struct socket *, int, struct thread *);
105static void	natm_usr_detach(struct socket *);
106static int	natm_usr_connect(struct socket *, struct sockaddr *,
107		    struct thread *);
108static int	natm_usr_disconnect(struct socket *);
109static int	natm_usr_shutdown(struct socket *);
110static int	natm_usr_send(struct socket *, int, struct mbuf *,
111		    struct sockaddr *, struct mbuf *, struct thread *);
112static int	natm_usr_peeraddr(struct socket *, struct sockaddr **);
113static int	natm_usr_control(struct socket *, u_long, caddr_t,
114		    struct ifnet *, struct thread *);
115static void	natm_usr_abort(struct socket *);
116static int	natm_usr_bind(struct socket *, struct sockaddr *,
117		    struct thread *);
118static int	natm_usr_sockaddr(struct socket *, struct sockaddr **);
119
120static int
121natm_usr_attach(struct socket *so, int proto, struct thread *p)
122{
123	struct natmpcb *npcb;
124	int error = 0;
125
126	npcb = (struct natmpcb *)so->so_pcb;
127	KASSERT(npcb == NULL, ("natm_usr_attach: so_pcb != NULL"));
128
129	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
130		if (proto == PROTO_NATMAAL5)
131			error = soreserve(so, natm5_sendspace,
132			    natm5_recvspace);
133		else
134			error = soreserve(so, natm0_sendspace,
135			    natm0_recvspace);
136		if (error)
137			return (error);
138	}
139	so->so_pcb = npcb = npcb_alloc(M_WAITOK);
140	npcb->npcb_socket = so;
141	return (error);
142}
143
144static void
145natm_usr_detach(struct socket *so)
146{
147	struct natmpcb *npcb;
148
149	npcb = (struct natmpcb *)so->so_pcb;
150	KASSERT(npcb != NULL, ("natm_usr_detach: npcb == NULL"));
151
152	NATM_LOCK();
153	npcb_free(npcb, NPCB_DESTROY);	/* drain */
154	so->so_pcb = NULL;
155	NATM_UNLOCK();
156}
157
158static int
159natm_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *p)
160{
161	struct natmpcb *npcb;
162	struct sockaddr_natm *snatm;
163	struct atmio_openvcc op;
164	struct ifnet *ifp;
165	int error = 0;
166	int proto = so->so_proto->pr_protocol;
167
168	npcb = (struct natmpcb *)so->so_pcb;
169	KASSERT(npcb != NULL, ("natm_usr_connect: npcb == NULL"));
170
171	/*
172	 * Validate nam and npcb.
173	 */
174	NATM_LOCK();
175	snatm = (struct sockaddr_natm *)nam;
176	if (snatm->snatm_len != sizeof(*snatm) ||
177		(npcb->npcb_flags & NPCB_FREE) == 0) {
178		NATM_UNLOCK();
179		return (EINVAL);
180	}
181	if (snatm->snatm_family != AF_NATM) {
182		NATM_UNLOCK();
183		return (EAFNOSUPPORT);
184	}
185
186	snatm->snatm_if[IFNAMSIZ - 1] = '\0';	/* XXX ensure null termination
187						   since ifunit() uses strcmp */
188
189	/*
190	 * Convert interface string to ifp, validate.
191	 */
192	ifp = ifunit(snatm->snatm_if);
193	if (ifp == NULL || (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
194		NATM_UNLOCK();
195		return (ENXIO);
196	}
197	if (ifp->if_output != atm_output) {
198		NATM_UNLOCK();
199		return (EAFNOSUPPORT);
200	}
201
202	/*
203	 * Register us with the NATM PCB layer.
204	 */
205	if (npcb_add(npcb, ifp, snatm->snatm_vci, snatm->snatm_vpi) != npcb) {
206		NATM_UNLOCK();
207		return (EADDRINUSE);
208	}
209
210	/*
211	 * Open the channel.
212	 *
213	 * XXXRW: Eventually desirable to hold mutex over ioctl?
214	 */
215	bzero(&op, sizeof(op));
216	op.rxhand = npcb;
217	op.param.flags = ATMIO_FLAG_PVC;
218	op.param.vpi = npcb->npcb_vpi;
219	op.param.vci = npcb->npcb_vci;
220	op.param.rmtu = op.param.tmtu = ifp->if_mtu;
221	op.param.aal = (proto == PROTO_NATMAAL5) ? ATMIO_AAL_5 : ATMIO_AAL_0;
222	op.param.traffic = ATMIO_TRAFFIC_UBR;
223	NATM_UNLOCK();
224
225	if (ifp->if_ioctl == NULL ||
226	    ifp->if_ioctl(ifp, SIOCATMOPENVCC, (caddr_t)&op) != 0)
227		return (EIO);
228	soisconnected(so);
229	return (error);
230}
231
232static int
233natm_usr_disconnect(struct socket *so)
234{
235	struct natmpcb *npcb;
236	struct atmio_closevcc cl;
237	struct ifnet *ifp;
238	int error = 0;
239
240	npcb = (struct natmpcb *)so->so_pcb;
241	KASSERT(npcb != NULL, ("natm_usr_disconnect: npcb == NULL"));
242
243	NATM_LOCK();
244	if ((npcb->npcb_flags & NPCB_CONNECTED) == 0) {
245		NATM_UNLOCK();
246		printf("natm: disconnected check\n");
247		return (EIO);
248	}
249	ifp = npcb->npcb_ifp;
250
251	/*
252	 * Disable rx.
253	 *
254	 * XXXRW: Eventually desirable to hold mutex over ioctl?
255	 */
256	cl.vpi = npcb->npcb_vpi;
257	cl.vci = npcb->npcb_vci;
258	NATM_UNLOCK();
259	if (ifp->if_ioctl != NULL)
260		ifp->if_ioctl(ifp, SIOCATMCLOSEVCC, (caddr_t)&cl);
261	soisdisconnected(so);
262	return (error);
263}
264
265static int
266natm_usr_shutdown(struct socket *so)
267{
268
269	socantsendmore(so);
270	return (0);
271}
272
273static int
274natm_usr_send(struct socket *so, int flags, struct mbuf *m,
275	struct sockaddr *nam, struct mbuf *control, struct thread *p)
276{
277	struct natmpcb *npcb;
278	struct atm_pseudohdr *aph;
279	int error = 0;
280	int proto = so->so_proto->pr_protocol;
281
282	npcb = (struct natmpcb *)so->so_pcb;
283	KASSERT(npcb != NULL, ("natm_usr_send: npcb == NULL"));
284
285	NATM_LOCK();
286	if (control && control->m_len) {
287		NATM_UNLOCK();
288		m_freem(control);
289		m_freem(m);
290		return (EINVAL);
291	}
292
293	/*
294	 * Send the data.  We must put an atm_pseudohdr on first.
295	 */
296	M_PREPEND(m, sizeof(*aph), M_NOWAIT);
297	if (m == NULL) {
298		NATM_UNLOCK();
299		m_freem(control);
300		return (ENOBUFS);
301	}
302	aph = mtod(m, struct atm_pseudohdr *);
303	ATM_PH_VPI(aph) = npcb->npcb_vpi;
304	ATM_PH_SETVCI(aph, npcb->npcb_vci);
305	ATM_PH_FLAGS(aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0;
306	error = atm_output(npcb->npcb_ifp, m, NULL, NULL);
307	NATM_UNLOCK();
308	return (error);
309}
310
311static int
312natm_usr_peeraddr(struct socket *so, struct sockaddr **nam)
313{
314	struct natmpcb *npcb;
315	struct sockaddr_natm *snatm, ssnatm;
316
317	npcb = (struct natmpcb *)so->so_pcb;
318	KASSERT(npcb != NULL, ("natm_usr_peeraddr: npcb == NULL"));
319
320	NATM_LOCK();
321	snatm = &ssnatm;
322	bzero(snatm, sizeof(*snatm));
323	snatm->snatm_len = sizeof(*snatm);
324	snatm->snatm_family = AF_NATM;
325	strlcpy(snatm->snatm_if, npcb->npcb_ifp->if_xname,
326	    sizeof(snatm->snatm_if));
327	snatm->snatm_vci = npcb->npcb_vci;
328	snatm->snatm_vpi = npcb->npcb_vpi;
329	NATM_UNLOCK();
330	*nam = sodupsockaddr((struct sockaddr *)snatm, M_WAITOK);
331	return (0);
332}
333
334static int
335natm_usr_control(struct socket *so, u_long cmd, caddr_t arg,
336	struct ifnet *ifp, struct thread *p)
337{
338	struct natmpcb *npcb;
339
340	npcb = (struct natmpcb *)so->so_pcb;
341	KASSERT(npcb != NULL, ("natm_usr_control: npcb == NULL"));
342
343	switch (cmd) {
344	case SIOCSIFADDR:
345	case SIOCSIFBRDADDR:
346	case SIOCSIFDSTADDR:
347	case SIOCSIFNETMASK:
348		/*
349		 * Although we should pass any non-ATM ioctl requests
350		 * down to driver, we filter some legacy INET requests.
351		 * Drivers trust SIOCSIFADDR et al to come from an already
352		 * privileged layer, and do not perform any credentials
353		 * checks or input validation.
354		 */
355		return (EINVAL);
356	}
357
358	if (ifp == NULL || ifp->if_ioctl == NULL)
359		return (EOPNOTSUPP);
360	return ((*ifp->if_ioctl)(ifp, cmd, arg));
361}
362
363static void
364natm_usr_abort(struct socket *so)
365{
366
367}
368
369static void
370natm_usr_close(struct socket *so)
371{
372
373}
374
375static int
376natm_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *p)
377{
378
379	return (EOPNOTSUPP);
380}
381
382static int
383natm_usr_sockaddr(struct socket *so, struct sockaddr **nam)
384{
385
386	return (EOPNOTSUPP);
387}
388
389/* xxx - should be const */
390struct pr_usrreqs natm_usrreqs = {
391	.pru_abort =		natm_usr_abort,
392	.pru_attach =		natm_usr_attach,
393	.pru_bind =		natm_usr_bind,
394	.pru_connect =		natm_usr_connect,
395	.pru_control =		natm_usr_control,
396	.pru_detach =		natm_usr_detach,
397	.pru_disconnect =	natm_usr_disconnect,
398	.pru_peeraddr =		natm_usr_peeraddr,
399	.pru_send =		natm_usr_send,
400	.pru_shutdown =		natm_usr_shutdown,
401	.pru_sockaddr =		natm_usr_sockaddr,
402	.pru_close =		natm_usr_close,
403};
404
405/*
406 * natmintr: interrupt
407 *
408 * Note: we expect a socket pointer in rcvif rather than an interface
409 * pointer.  We can get the interface pointer from the so's PCB if we really
410 * need it.
411 */
412void
413natmintr(struct mbuf *m)
414{
415	struct socket *so;
416	struct natmpcb *npcb;
417
418#ifdef DIAGNOSTIC
419	M_ASSERTPKTHDR(m);
420#endif
421
422	NATM_LOCK();
423	npcb = (struct natmpcb *)m->m_pkthdr.rcvif;	/* XXX: overloaded */
424	so = npcb->npcb_socket;
425
426	npcb->npcb_inq--;
427
428	if (npcb->npcb_flags & NPCB_DRAIN) {
429		if (npcb->npcb_inq == 0)
430			free(npcb, M_PCB);			/* done! */
431		NATM_UNLOCK();
432		m_freem(m);
433		return;
434	}
435
436	if (npcb->npcb_flags & NPCB_FREE) {
437		NATM_UNLOCK();
438		m_freem(m);					/* drop */
439		return;
440	}
441
442#ifdef NEED_TO_RESTORE_IFP
443	m->m_pkthdr.rcvif = npcb->npcb_ifp;
444#else
445#ifdef DIAGNOSTIC
446	m->m_pkthdr.rcvif = NULL;	/* null it out to be safe */
447#endif
448#endif
449
450	if (sbspace(&so->so_rcv) > m->m_pkthdr.len) {
451#ifdef NATM_STAT
452		natm_sookcnt++;
453		natm_sookbytes += m->m_pkthdr.len;
454#endif
455		sbappendrecord(&so->so_rcv, m);
456		sorwakeup(so);
457		NATM_UNLOCK();
458	} else {
459#ifdef NATM_STAT
460		natm_sodropcnt++;
461		natm_sodropbytes += m->m_pkthdr.len;
462#endif
463		NATM_UNLOCK();
464		m_freem(m);
465	}
466}
467
468/*
469 * natm0_sysctl: not used, but here in case we want to add something
470 * later...
471 */
472int
473natm0_sysctl(SYSCTL_HANDLER_ARGS)
474{
475
476	/* All sysctl names at this level are terminal. */
477	return (ENOENT);
478}
479
480/*
481 * natm5_sysctl: not used, but here in case we want to add something
482 * later...
483 */
484int
485natm5_sysctl(SYSCTL_HANDLER_ARGS)
486{
487
488	/* All sysctl names at this level are terminal. */
489	return (ENOENT);
490}
491