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_atm.h>
82 #include <net/netisr.h>
84 #include <netinet/in.h>
86 #include <netnatm/natm.h>
88 static const u_long natm5_sendspace = 16*1024;
89 static const u_long natm5_recvspace = 16*1024;
91 static const u_long natm0_sendspace = 16*1024;
92 static const u_long natm0_recvspace = 16*1024;
95 * netnatm global subsystem lock, protects all global data structures in
101 * User socket requests.
103 static int natm_usr_attach(struct socket *, int, struct thread *);
104 static void natm_usr_detach(struct socket *);
105 static int natm_usr_connect(struct socket *, struct sockaddr *,
107 static int natm_usr_disconnect(struct socket *);
108 static int natm_usr_shutdown(struct socket *);
109 static int natm_usr_send(struct socket *, int, struct mbuf *,
110 struct sockaddr *, struct mbuf *, struct thread *);
111 static int natm_usr_peeraddr(struct socket *, struct sockaddr **);
112 static int natm_usr_control(struct socket *, u_long, caddr_t,
113 struct ifnet *, struct thread *);
114 static void natm_usr_abort(struct socket *);
115 static int natm_usr_bind(struct socket *, struct sockaddr *,
117 static int natm_usr_sockaddr(struct socket *, struct sockaddr **);
120 natm_usr_attach(struct socket *so, int proto, struct thread *p)
122 struct natmpcb *npcb;
125 npcb = (struct natmpcb *)so->so_pcb;
126 KASSERT(npcb == NULL, ("natm_usr_attach: so_pcb != NULL"));
128 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
129 if (proto == PROTO_NATMAAL5)
130 error = soreserve(so, natm5_sendspace,
133 error = soreserve(so, natm0_sendspace,
138 so->so_pcb = npcb = npcb_alloc(M_WAITOK);
139 npcb->npcb_socket = so;
144 natm_usr_detach(struct socket *so)
146 struct natmpcb *npcb;
148 npcb = (struct natmpcb *)so->so_pcb;
149 KASSERT(npcb != NULL, ("natm_usr_detach: npcb == NULL"));
152 npcb_free(npcb, NPCB_DESTROY); /* drain */
158 natm_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *p)
160 struct natmpcb *npcb;
161 struct sockaddr_natm *snatm;
162 struct atmio_openvcc op;
165 int proto = so->so_proto->pr_protocol;
167 npcb = (struct natmpcb *)so->so_pcb;
168 KASSERT(npcb != NULL, ("natm_usr_connect: npcb == NULL"));
171 * Validate nam and npcb.
174 snatm = (struct sockaddr_natm *)nam;
175 if (snatm->snatm_len != sizeof(*snatm) ||
176 (npcb->npcb_flags & NPCB_FREE) == 0) {
180 if (snatm->snatm_family != AF_NATM) {
182 return (EAFNOSUPPORT);
185 snatm->snatm_if[IFNAMSIZ - 1] = '\0'; /* XXX ensure null termination
186 since ifunit() uses strcmp */
189 * Convert interface string to ifp, validate.
191 ifp = ifunit(snatm->snatm_if);
192 if (ifp == NULL || (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
196 if (ifp->if_output != atm_output) {
198 return (EAFNOSUPPORT);
202 * Register us with the NATM PCB layer.
204 if (npcb_add(npcb, ifp, snatm->snatm_vci, snatm->snatm_vpi) != npcb) {
212 * XXXRW: Eventually desirable to hold mutex over ioctl?
214 bzero(&op, sizeof(op));
216 op.param.flags = ATMIO_FLAG_PVC;
217 op.param.vpi = npcb->npcb_vpi;
218 op.param.vci = npcb->npcb_vci;
219 op.param.rmtu = op.param.tmtu = ifp->if_mtu;
220 op.param.aal = (proto == PROTO_NATMAAL5) ? ATMIO_AAL_5 : ATMIO_AAL_0;
221 op.param.traffic = ATMIO_TRAFFIC_UBR;
224 if (ifp->if_ioctl == NULL ||
225 ifp->if_ioctl(ifp, SIOCATMOPENVCC, (caddr_t)&op) != 0)
232 natm_usr_disconnect(struct socket *so)
234 struct natmpcb *npcb;
235 struct atmio_closevcc cl;
239 npcb = (struct natmpcb *)so->so_pcb;
240 KASSERT(npcb != NULL, ("natm_usr_disconnect: npcb == NULL"));
243 if ((npcb->npcb_flags & NPCB_CONNECTED) == 0) {
245 printf("natm: disconnected check\n");
248 ifp = npcb->npcb_ifp;
253 * XXXRW: Eventually desirable to hold mutex over ioctl?
255 cl.vpi = npcb->npcb_vpi;
256 cl.vci = npcb->npcb_vci;
258 if (ifp->if_ioctl != NULL)
259 ifp->if_ioctl(ifp, SIOCATMCLOSEVCC, (caddr_t)&cl);
260 soisdisconnected(so);
265 natm_usr_shutdown(struct socket *so)
273 natm_usr_send(struct socket *so, int flags, struct mbuf *m,
274 struct sockaddr *nam, struct mbuf *control, struct thread *p)
276 struct natmpcb *npcb;
277 struct atm_pseudohdr *aph;
279 int proto = so->so_proto->pr_protocol;
281 npcb = (struct natmpcb *)so->so_pcb;
282 KASSERT(npcb != NULL, ("natm_usr_send: npcb == NULL"));
285 if (control && control->m_len) {
293 * Send the data. We must put an atm_pseudohdr on first.
295 M_PREPEND(m, sizeof(*aph), M_DONTWAIT);
301 aph = mtod(m, struct atm_pseudohdr *);
302 ATM_PH_VPI(aph) = npcb->npcb_vpi;
303 ATM_PH_SETVCI(aph, npcb->npcb_vci);
304 ATM_PH_FLAGS(aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0;
305 error = atm_output(npcb->npcb_ifp, m, NULL, NULL);
311 natm_usr_peeraddr(struct socket *so, struct sockaddr **nam)
313 struct natmpcb *npcb;
314 struct sockaddr_natm *snatm, ssnatm;
316 npcb = (struct natmpcb *)so->so_pcb;
317 KASSERT(npcb != NULL, ("natm_usr_peeraddr: npcb == NULL"));
321 bzero(snatm, sizeof(*snatm));
322 snatm->snatm_len = sizeof(*snatm);
323 snatm->snatm_family = AF_NATM;
324 strlcpy(snatm->snatm_if, npcb->npcb_ifp->if_xname,
325 sizeof(snatm->snatm_if));
326 snatm->snatm_vci = npcb->npcb_vci;
327 snatm->snatm_vpi = npcb->npcb_vpi;
329 *nam = sodupsockaddr((struct sockaddr *)snatm, M_WAITOK);
334 natm_usr_control(struct socket *so, u_long cmd, caddr_t arg,
335 struct ifnet *ifp, struct thread *p)
337 struct natmpcb *npcb;
339 npcb = (struct natmpcb *)so->so_pcb;
340 KASSERT(npcb != NULL, ("natm_usr_control: npcb == NULL"));
348 * Although we should pass any non-ATM ioctl requests
349 * down to driver, we filter some legacy INET requests.
350 * Drivers trust SIOCSIFADDR et al to come from an already
351 * privileged layer, and do not perform any credentials
352 * checks or input validation.
357 if (ifp == NULL || ifp->if_ioctl == NULL)
359 return ((*ifp->if_ioctl)(ifp, cmd, arg));
363 natm_usr_abort(struct socket *so)
369 natm_usr_close(struct socket *so)
375 natm_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *p)
382 natm_usr_sockaddr(struct socket *so, struct sockaddr **nam)
388 /* xxx - should be const */
389 struct pr_usrreqs natm_usrreqs = {
390 .pru_abort = natm_usr_abort,
391 .pru_attach = natm_usr_attach,
392 .pru_bind = natm_usr_bind,
393 .pru_connect = natm_usr_connect,
394 .pru_control = natm_usr_control,
395 .pru_detach = natm_usr_detach,
396 .pru_disconnect = natm_usr_disconnect,
397 .pru_peeraddr = natm_usr_peeraddr,
398 .pru_send = natm_usr_send,
399 .pru_shutdown = natm_usr_shutdown,
400 .pru_sockaddr = natm_usr_sockaddr,
401 .pru_close = natm_usr_close,
405 * natmintr: interrupt
407 * Note: we expect a socket pointer in rcvif rather than an interface
408 * pointer. We can get the interface pointer from the so's PCB if we really
412 natmintr(struct mbuf *m)
415 struct natmpcb *npcb;
422 npcb = (struct natmpcb *)m->m_pkthdr.rcvif; /* XXX: overloaded */
423 so = npcb->npcb_socket;
427 if (npcb->npcb_flags & NPCB_DRAIN) {
428 if (npcb->npcb_inq == 0)
429 free(npcb, M_PCB); /* done! */
435 if (npcb->npcb_flags & NPCB_FREE) {
437 m_freem(m); /* drop */
441 #ifdef NEED_TO_RESTORE_IFP
442 m->m_pkthdr.rcvif = npcb->npcb_ifp;
445 m->m_pkthdr.rcvif = NULL; /* null it out to be safe */
449 if (sbspace(&so->so_rcv) > m->m_pkthdr.len) {
452 natm_sookbytes += m->m_pkthdr.len;
454 sbappendrecord(&so->so_rcv, m);
460 natm_sodropbytes += m->m_pkthdr.len;
468 * natm0_sysctl: not used, but here in case we want to add something
472 natm0_sysctl(SYSCTL_HANDLER_ARGS)
475 /* All sysctl names at this level are terminal. */
480 * natm5_sysctl: not used, but here in case we want to add something
484 natm5_sysctl(SYSCTL_HANDLER_ARGS)
487 /* All sysctl names at this level are terminal. */