2 * Copyright (c) 2005-2006 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * Copyright (c) 1996 Charles D. Cranor and Washington University.
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
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.
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.
55 * $NetBSD: natm.c,v 1.5 1996/11/09 03:26:26 chuck Exp $
59 * natm.c: Native mode ATM access (both aal0 and aal5).
62 #include <sys/cdefs.h>
63 __FBSDID("$FreeBSD$");
65 #include <sys/param.h>
67 #include <sys/kernel.h>
69 #include <sys/malloc.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>
77 #include <sys/systm.h>
78 #include <sys/sysctl.h>
81 #include <net/if_var.h>
82 #include <net/if_atm.h>
83 #include <net/netisr.h>
85 #include <netinet/in.h>
87 #include <netnatm/natm.h>
89 static const u_long natm5_sendspace = 16*1024;
90 static const u_long natm5_recvspace = 16*1024;
92 static const u_long natm0_sendspace = 16*1024;
93 static const u_long natm0_recvspace = 16*1024;
96 * netnatm global subsystem lock, protects all global data structures in
102 * User socket requests.
104 static int natm_usr_attach(struct socket *, int, struct thread *);
105 static void natm_usr_detach(struct socket *);
106 static int natm_usr_connect(struct socket *, struct sockaddr *,
108 static int natm_usr_disconnect(struct socket *);
109 static int natm_usr_shutdown(struct socket *);
110 static int natm_usr_send(struct socket *, int, struct mbuf *,
111 struct sockaddr *, struct mbuf *, struct thread *);
112 static int natm_usr_peeraddr(struct socket *, struct sockaddr **);
113 static int natm_usr_control(struct socket *, u_long, caddr_t,
114 struct ifnet *, struct thread *);
115 static void natm_usr_abort(struct socket *);
116 static int natm_usr_bind(struct socket *, struct sockaddr *,
118 static int natm_usr_sockaddr(struct socket *, struct sockaddr **);
121 natm_usr_attach(struct socket *so, int proto, struct thread *p)
123 struct natmpcb *npcb;
126 npcb = (struct natmpcb *)so->so_pcb;
127 KASSERT(npcb == NULL, ("natm_usr_attach: so_pcb != NULL"));
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,
134 error = soreserve(so, natm0_sendspace,
139 so->so_pcb = npcb = npcb_alloc(M_WAITOK);
140 npcb->npcb_socket = so;
145 natm_usr_detach(struct socket *so)
147 struct natmpcb *npcb;
149 npcb = (struct natmpcb *)so->so_pcb;
150 KASSERT(npcb != NULL, ("natm_usr_detach: npcb == NULL"));
153 npcb_free(npcb, NPCB_DESTROY); /* drain */
159 natm_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *p)
161 struct natmpcb *npcb;
162 struct sockaddr_natm *snatm;
163 struct atmio_openvcc op;
166 int proto = so->so_proto->pr_protocol;
168 npcb = (struct natmpcb *)so->so_pcb;
169 KASSERT(npcb != NULL, ("natm_usr_connect: npcb == NULL"));
172 * Validate nam and npcb.
175 snatm = (struct sockaddr_natm *)nam;
176 if (snatm->snatm_len != sizeof(*snatm) ||
177 (npcb->npcb_flags & NPCB_FREE) == 0) {
181 if (snatm->snatm_family != AF_NATM) {
183 return (EAFNOSUPPORT);
186 snatm->snatm_if[IFNAMSIZ - 1] = '\0'; /* XXX ensure null termination
187 since ifunit() uses strcmp */
190 * Convert interface string to ifp, validate.
192 ifp = ifunit(snatm->snatm_if);
193 if (ifp == NULL || (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
197 if (ifp->if_output != atm_output) {
199 return (EAFNOSUPPORT);
203 * Register us with the NATM PCB layer.
205 if (npcb_add(npcb, ifp, snatm->snatm_vci, snatm->snatm_vpi) != npcb) {
213 * XXXRW: Eventually desirable to hold mutex over ioctl?
215 bzero(&op, sizeof(op));
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;
225 if (ifp->if_ioctl == NULL ||
226 ifp->if_ioctl(ifp, SIOCATMOPENVCC, (caddr_t)&op) != 0)
233 natm_usr_disconnect(struct socket *so)
235 struct natmpcb *npcb;
236 struct atmio_closevcc cl;
240 npcb = (struct natmpcb *)so->so_pcb;
241 KASSERT(npcb != NULL, ("natm_usr_disconnect: npcb == NULL"));
244 if ((npcb->npcb_flags & NPCB_CONNECTED) == 0) {
246 printf("natm: disconnected check\n");
249 ifp = npcb->npcb_ifp;
254 * XXXRW: Eventually desirable to hold mutex over ioctl?
256 cl.vpi = npcb->npcb_vpi;
257 cl.vci = npcb->npcb_vci;
259 if (ifp->if_ioctl != NULL)
260 ifp->if_ioctl(ifp, SIOCATMCLOSEVCC, (caddr_t)&cl);
261 soisdisconnected(so);
266 natm_usr_shutdown(struct socket *so)
274 natm_usr_send(struct socket *so, int flags, struct mbuf *m,
275 struct sockaddr *nam, struct mbuf *control, struct thread *p)
277 struct natmpcb *npcb;
278 struct atm_pseudohdr *aph;
280 int proto = so->so_proto->pr_protocol;
282 npcb = (struct natmpcb *)so->so_pcb;
283 KASSERT(npcb != NULL, ("natm_usr_send: npcb == NULL"));
286 if (control && control->m_len) {
294 * Send the data. We must put an atm_pseudohdr on first.
296 M_PREPEND(m, sizeof(*aph), M_NOWAIT);
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);
312 natm_usr_peeraddr(struct socket *so, struct sockaddr **nam)
314 struct natmpcb *npcb;
315 struct sockaddr_natm *snatm, ssnatm;
317 npcb = (struct natmpcb *)so->so_pcb;
318 KASSERT(npcb != NULL, ("natm_usr_peeraddr: npcb == NULL"));
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;
330 *nam = sodupsockaddr((struct sockaddr *)snatm, M_WAITOK);
335 natm_usr_control(struct socket *so, u_long cmd, caddr_t arg,
336 struct ifnet *ifp, struct thread *p)
338 struct natmpcb *npcb;
340 npcb = (struct natmpcb *)so->so_pcb;
341 KASSERT(npcb != NULL, ("natm_usr_control: npcb == NULL"));
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.
358 if (ifp == NULL || ifp->if_ioctl == NULL)
360 return ((*ifp->if_ioctl)(ifp, cmd, arg));
364 natm_usr_abort(struct socket *so)
370 natm_usr_close(struct socket *so)
376 natm_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *p)
383 natm_usr_sockaddr(struct socket *so, struct sockaddr **nam)
389 /* xxx - should be const */
390 struct 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,
406 * natmintr: interrupt
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
413 natmintr(struct mbuf *m)
416 struct natmpcb *npcb;
423 npcb = (struct natmpcb *)m->m_pkthdr.rcvif; /* XXX: overloaded */
424 so = npcb->npcb_socket;
428 if (npcb->npcb_flags & NPCB_DRAIN) {
429 if (npcb->npcb_inq == 0)
430 free(npcb, M_PCB); /* done! */
436 if (npcb->npcb_flags & NPCB_FREE) {
438 m_freem(m); /* drop */
442 #ifdef NEED_TO_RESTORE_IFP
443 m->m_pkthdr.rcvif = npcb->npcb_ifp;
446 m->m_pkthdr.rcvif = NULL; /* null it out to be safe */
450 if (sbspace(&so->so_rcv) > m->m_pkthdr.len) {
453 natm_sookbytes += m->m_pkthdr.len;
455 sbappendrecord(&so->so_rcv, m);
461 natm_sodropbytes += m->m_pkthdr.len;
469 * natm0_sysctl: not used, but here in case we want to add something
473 natm0_sysctl(SYSCTL_HANDLER_ARGS)
476 /* All sysctl names at this level are terminal. */
481 * natm5_sysctl: not used, but here in case we want to add something
485 natm5_sysctl(SYSCTL_HANDLER_ARGS)
488 /* All sysctl names at this level are terminal. */