1 /* $NetBSD: natm.c,v 1.5 1996/11/09 03:26:26 chuck Exp $ */
4 * Copyright (c) 1996 Charles D. Cranor and Washington University.
5 * Copyright (c) 2005-2006 Robert N. M. Watson
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Charles D. Cranor and
19 * Washington University.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * natm.c: Native mode ATM access (both aal0 and aal5).
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
48 #include <sys/protosw.h>
49 #include <sys/signalvar.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sockio.h>
54 #include <sys/systm.h>
55 #include <sys/sysctl.h>
58 #include <net/if_atm.h>
59 #include <net/netisr.h>
61 #include <netinet/in.h>
63 #include <netnatm/natm.h>
65 static const u_long natm5_sendspace = 16*1024;
66 static const u_long natm5_recvspace = 16*1024;
68 static const u_long natm0_sendspace = 16*1024;
69 static const u_long natm0_recvspace = 16*1024;
72 * netnatm global subsystem lock, protects all global data structures in
78 * User socket requests.
80 static int natm_usr_attach(struct socket *, int, d_thread_t *);
81 static void natm_usr_detach(struct socket *);
82 static int natm_usr_connect(struct socket *, struct sockaddr *,
84 static int natm_usr_disconnect(struct socket *);
85 static int natm_usr_shutdown(struct socket *);
86 static int natm_usr_send(struct socket *, int, struct mbuf *,
87 struct sockaddr *, struct mbuf *, d_thread_t *);
88 static int natm_usr_peeraddr(struct socket *, struct sockaddr **);
89 static int natm_usr_control(struct socket *, u_long, caddr_t,
90 struct ifnet *, d_thread_t *);
91 static void natm_usr_abort(struct socket *);
92 static int natm_usr_bind(struct socket *, struct sockaddr *,
94 static int natm_usr_sockaddr(struct socket *, struct sockaddr **);
97 natm_usr_attach(struct socket *so, int proto, d_thread_t *p)
102 npcb = (struct natmpcb *)so->so_pcb;
103 KASSERT(npcb == NULL, ("natm_usr_attach: so_pcb != NULL"));
105 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
106 if (proto == PROTO_NATMAAL5)
107 error = soreserve(so, natm5_sendspace,
110 error = soreserve(so, natm0_sendspace,
115 so->so_pcb = npcb = npcb_alloc(M_WAITOK);
116 npcb->npcb_socket = so;
121 natm_usr_detach(struct socket *so)
123 struct natmpcb *npcb;
125 npcb = (struct natmpcb *)so->so_pcb;
126 KASSERT(npcb != NULL, ("natm_usr_detach: npcb == NULL"));
129 npcb_free(npcb, NPCB_DESTROY); /* drain */
135 natm_usr_connect(struct socket *so, struct sockaddr *nam, d_thread_t *p)
137 struct natmpcb *npcb;
138 struct sockaddr_natm *snatm;
139 struct atmio_openvcc op;
142 int proto = so->so_proto->pr_protocol;
144 npcb = (struct natmpcb *)so->so_pcb;
145 KASSERT(npcb != NULL, ("natm_usr_connect: npcb == NULL"));
148 * Validate nam and npcb.
151 snatm = (struct sockaddr_natm *)nam;
152 if (snatm->snatm_len != sizeof(*snatm) ||
153 (npcb->npcb_flags & NPCB_FREE) == 0) {
157 if (snatm->snatm_family != AF_NATM) {
159 return (EAFNOSUPPORT);
162 snatm->snatm_if[IFNAMSIZ - 1] = '\0'; /* XXX ensure null termination
163 since ifunit() uses strcmp */
166 * Convert interface string to ifp, validate.
168 ifp = ifunit(snatm->snatm_if);
169 if (ifp == NULL || (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
173 if (ifp->if_output != atm_output) {
175 return (EAFNOSUPPORT);
179 * Register us with the NATM PCB layer.
181 if (npcb_add(npcb, ifp, snatm->snatm_vci, snatm->snatm_vpi) != npcb) {
189 * XXXRW: Eventually desirable to hold mutex over ioctl?
191 bzero(&op, sizeof(op));
193 op.param.flags = ATMIO_FLAG_PVC;
194 op.param.vpi = npcb->npcb_vpi;
195 op.param.vci = npcb->npcb_vci;
196 op.param.rmtu = op.param.tmtu = ifp->if_mtu;
197 op.param.aal = (proto == PROTO_NATMAAL5) ? ATMIO_AAL_5 : ATMIO_AAL_0;
198 op.param.traffic = ATMIO_TRAFFIC_UBR;
202 if (ifp->if_ioctl == NULL ||
203 ifp->if_ioctl(ifp, SIOCATMOPENVCC, (caddr_t)&op) != 0) {
204 IFF_UNLOCKGIANT(ifp);
207 IFF_UNLOCKGIANT(ifp);
213 natm_usr_disconnect(struct socket *so)
215 struct natmpcb *npcb;
216 struct atmio_closevcc cl;
220 npcb = (struct natmpcb *)so->so_pcb;
221 KASSERT(npcb != NULL, ("natm_usr_disconnect: npcb == NULL"));
224 if ((npcb->npcb_flags & NPCB_CONNECTED) == 0) {
226 printf("natm: disconnected check\n");
229 ifp = npcb->npcb_ifp;
234 * XXXRW: Eventually desirable to hold mutex over ioctl?
236 cl.vpi = npcb->npcb_vpi;
237 cl.vci = npcb->npcb_vci;
239 if (ifp->if_ioctl != NULL) {
241 ifp->if_ioctl(ifp, SIOCATMCLOSEVCC, (caddr_t)&cl);
242 IFF_UNLOCKGIANT(ifp);
244 soisdisconnected(so);
249 natm_usr_shutdown(struct socket *so)
257 natm_usr_send(struct socket *so, int flags, struct mbuf *m,
258 struct sockaddr *nam, struct mbuf *control, d_thread_t *p)
260 struct natmpcb *npcb;
261 struct atm_pseudohdr *aph;
263 int proto = so->so_proto->pr_protocol;
265 npcb = (struct natmpcb *)so->so_pcb;
266 KASSERT(npcb != NULL, ("natm_usr_send: npcb == NULL"));
269 if (control && control->m_len) {
277 * Send the data. We must put an atm_pseudohdr on first.
279 M_PREPEND(m, sizeof(*aph), M_DONTWAIT);
285 aph = mtod(m, struct atm_pseudohdr *);
286 ATM_PH_VPI(aph) = npcb->npcb_vpi;
287 ATM_PH_SETVCI(aph, npcb->npcb_vci);
288 ATM_PH_FLAGS(aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0;
289 error = atm_output(npcb->npcb_ifp, m, NULL, NULL);
295 natm_usr_peeraddr(struct socket *so, struct sockaddr **nam)
297 struct natmpcb *npcb;
298 struct sockaddr_natm *snatm, ssnatm;
300 npcb = (struct natmpcb *)so->so_pcb;
301 KASSERT(npcb != NULL, ("natm_usr_peeraddr: npcb == NULL"));
305 bzero(snatm, sizeof(*snatm));
306 snatm->snatm_len = sizeof(*snatm);
307 snatm->snatm_family = AF_NATM;
308 strlcpy(snatm->snatm_if, npcb->npcb_ifp->if_xname,
309 sizeof(snatm->snatm_if));
310 snatm->snatm_vci = npcb->npcb_vci;
311 snatm->snatm_vpi = npcb->npcb_vpi;
313 *nam = sodupsockaddr((struct sockaddr *)snatm, M_WAITOK);
318 natm_usr_control(struct socket *so, u_long cmd, caddr_t arg,
319 struct ifnet *ifp, d_thread_t *p)
321 struct natmpcb *npcb;
324 npcb = (struct natmpcb *)so->so_pcb;
325 KASSERT(npcb != NULL, ("natm_usr_control: npcb == NULL"));
327 if (ifp == NULL || ifp->if_ioctl == NULL)
330 error = ((*ifp->if_ioctl)(ifp, cmd, arg));
331 IFF_UNLOCKGIANT(ifp);
336 natm_usr_abort(struct socket *so)
343 natm_usr_bind(struct socket *so, struct sockaddr *nam, d_thread_t *p)
350 natm_usr_sockaddr(struct socket *so, struct sockaddr **nam)
356 /* xxx - should be const */
357 struct pr_usrreqs natm_usrreqs = {
358 .pru_abort = natm_usr_abort,
359 .pru_attach = natm_usr_attach,
360 .pru_bind = natm_usr_bind,
361 .pru_connect = natm_usr_connect,
362 .pru_control = natm_usr_control,
363 .pru_detach = natm_usr_detach,
364 .pru_disconnect = natm_usr_disconnect,
365 .pru_peeraddr = natm_usr_peeraddr,
366 .pru_send = natm_usr_send,
367 .pru_shutdown = natm_usr_shutdown,
368 .pru_sockaddr = natm_usr_sockaddr,
372 * natmintr: interrupt
374 * Note: we expect a socket pointer in rcvif rather than an interface
375 * pointer. We can get the interface pointer from the so's PCB if we really
379 natmintr(struct mbuf *m)
382 struct natmpcb *npcb;
389 npcb = (struct natmpcb *)m->m_pkthdr.rcvif; /* XXX: overloaded */
390 so = npcb->npcb_socket;
394 if (npcb->npcb_flags & NPCB_DRAIN) {
395 if (npcb->npcb_inq == 0)
396 FREE(npcb, M_PCB); /* done! */
402 if (npcb->npcb_flags & NPCB_FREE) {
404 m_freem(m); /* drop */
408 #ifdef NEED_TO_RESTORE_IFP
409 m->m_pkthdr.rcvif = npcb->npcb_ifp;
412 m->m_pkthdr.rcvif = NULL; /* null it out to be safe */
416 if (sbspace(&so->so_rcv) > m->m_pkthdr.len) {
419 natm_sookbytes += m->m_pkthdr.len;
421 sbappendrecord(&so->so_rcv, m);
427 natm_sodropbytes += m->m_pkthdr.len;
435 * natm0_sysctl: not used, but here in case we want to add something
439 natm0_sysctl(SYSCTL_HANDLER_ARGS)
442 /* All sysctl names at this level are terminal. */
447 * natm5_sysctl: not used, but here in case we want to add something
451 natm5_sysctl(SYSCTL_HANDLER_ARGS)
454 /* All sysctl names at this level are terminal. */