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 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;
76 static int natm_usr_attach(struct socket *, int, d_thread_t *);
77 static int natm_usr_detach(struct socket *);
78 static int natm_usr_connect(struct socket *, struct sockaddr *, d_thread_t *);
79 static int natm_usr_disconnect(struct socket *);
80 static int natm_usr_shutdown(struct socket *);
81 static int natm_usr_send(struct socket *, int, struct mbuf *,
82 struct sockaddr *, struct mbuf *, d_thread_t *);
83 static int natm_usr_peeraddr(struct socket *, struct sockaddr **);
84 static int natm_usr_control(struct socket *, u_long, caddr_t,
85 struct ifnet *, d_thread_t *);
86 static int natm_usr_abort(struct socket *);
87 static int natm_usr_bind(struct socket *, struct sockaddr *, d_thread_t *);
88 static int natm_usr_sockaddr(struct socket *, struct sockaddr **);
91 natm_usr_attach(struct socket *so, int proto, d_thread_t *p)
96 npcb = (struct natmpcb *)so->so_pcb;
98 KASSERT(npcb == NULL, ("natm_usr_attach: so_pcb != NULL"));
100 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
101 if (proto == PROTO_NATMAAL5)
102 error = soreserve(so, natm5_sendspace, natm5_recvspace);
104 error = soreserve(so, natm0_sendspace, natm0_recvspace);
109 so->so_pcb = (caddr_t) (npcb = npcb_alloc(M_WAITOK));
110 npcb->npcb_socket = so;
116 natm_usr_detach(struct socket *so)
118 struct natmpcb *npcb;
122 npcb = (struct natmpcb *)so->so_pcb;
124 /* XXXRW: Does this case ever actually happen? */
130 * we turn on 'drain' *before* we sofree.
132 npcb_free(npcb, NPCB_DESTROY); /* drain */
143 natm_usr_connect(struct socket *so, struct sockaddr *nam, d_thread_t *p)
145 struct natmpcb *npcb;
146 struct sockaddr_natm *snatm;
147 struct atmio_openvcc op;
150 int proto = so->so_proto->pr_protocol;
153 npcb = (struct natmpcb *)so->so_pcb;
155 /* XXXRW: Does this case ever actually happen? */
161 * validate nam and npcb
163 snatm = (struct sockaddr_natm *)nam;
164 if (snatm->snatm_len != sizeof(*snatm) ||
165 (npcb->npcb_flags & NPCB_FREE) == 0) {
169 if (snatm->snatm_family != AF_NATM) {
170 error = EAFNOSUPPORT;
174 snatm->snatm_if[IFNAMSIZ - 1] = '\0'; /* XXX ensure null termination
175 since ifunit() uses strcmp */
178 * convert interface string to ifp, validate.
180 ifp = ifunit(snatm->snatm_if);
181 if (ifp == NULL || (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
185 if (ifp->if_output != atm_output) {
186 error = EAFNOSUPPORT;
191 * register us with the NATM PCB layer
193 if (npcb_add(npcb, ifp, snatm->snatm_vci, snatm->snatm_vpi) != npcb) {
202 bzero(&op, sizeof(op));
204 op.param.flags = ATMIO_FLAG_PVC;
205 op.param.vpi = npcb->npcb_vpi;
206 op.param.vci = npcb->npcb_vci;
207 op.param.rmtu = op.param.tmtu = ifp->if_mtu;
208 op.param.aal = (proto == PROTO_NATMAAL5) ? ATMIO_AAL_5 : ATMIO_AAL_0;
209 op.param.traffic = ATMIO_TRAFFIC_UBR;
212 if (ifp->if_ioctl == NULL ||
213 ifp->if_ioctl(ifp, SIOCATMOPENVCC, (caddr_t)&op) != 0) {
214 IFF_UNLOCKGIANT(ifp);
216 npcb_free(npcb, NPCB_REMOVE);
220 IFF_UNLOCKGIANT(ifp);
231 natm_usr_disconnect(struct socket *so)
233 struct natmpcb *npcb;
234 struct atmio_closevcc cl;
239 npcb = (struct natmpcb *)so->so_pcb;
241 /* XXXRW: Does this case ever actually happen? */
246 if ((npcb->npcb_flags & NPCB_CONNECTED) == 0) {
247 printf("natm: disconnected check\n");
251 ifp = npcb->npcb_ifp;
256 cl.vpi = npcb->npcb_vpi;
257 cl.vci = npcb->npcb_vci;
259 if (ifp->if_ioctl != NULL) {
261 ifp->if_ioctl(ifp, SIOCATMCLOSEVCC, (caddr_t)&cl);
262 IFF_UNLOCKGIANT(ifp);
266 npcb_free(npcb, NPCB_REMOVE);
267 soisdisconnected(so);
275 natm_usr_shutdown(struct socket *so)
282 natm_usr_send(struct socket *so, int flags, struct mbuf *m,
283 struct sockaddr *nam, struct mbuf *control, d_thread_t *p)
285 struct natmpcb *npcb;
286 struct atm_pseudohdr *aph;
288 int proto = so->so_proto->pr_protocol;
291 npcb = (struct natmpcb *)so->so_pcb;
293 /* XXXRW: Does this case ever actually happen? */
298 if (control && control->m_len) {
306 * send the data. we must put an atm_pseudohdr on first
308 M_PREPEND(m, sizeof(*aph), M_DONTWAIT);
313 aph = mtod(m, struct atm_pseudohdr *);
314 ATM_PH_VPI(aph) = npcb->npcb_vpi;
315 ATM_PH_SETVCI(aph, npcb->npcb_vci);
316 ATM_PH_FLAGS(aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0;
318 error = atm_output(npcb->npcb_ifp, m, NULL, NULL);
326 natm_usr_peeraddr(struct socket *so, struct sockaddr **nam)
328 struct natmpcb *npcb;
329 struct sockaddr_natm *snatm, ssnatm;
332 npcb = (struct natmpcb *)so->so_pcb;
334 /* XXXRW: Does this case ever actually happen? */
340 bzero(snatm, sizeof(*snatm));
341 snatm->snatm_len = sizeof(*snatm);
342 snatm->snatm_family = AF_NATM;
343 strlcpy(snatm->snatm_if, npcb->npcb_ifp->if_xname,
344 sizeof(snatm->snatm_if));
345 snatm->snatm_vci = npcb->npcb_vci;
346 snatm->snatm_vpi = npcb->npcb_vpi;
348 *nam = sodupsockaddr((struct sockaddr *)snatm, M_WAITOK);
353 natm_usr_control(struct socket *so, u_long cmd, caddr_t arg,
354 struct ifnet *ifp, d_thread_t *p)
356 struct natmpcb *npcb;
360 * XXXRW: Does this case ever actually happen? And does it even matter
361 * given that npcb is unused?
363 npcb = (struct natmpcb *)so->so_pcb;
367 if (ifp == NULL || ifp->if_ioctl == NULL)
371 error = ((*ifp->if_ioctl)(ifp, cmd, arg));
372 IFF_UNLOCKGIANT(ifp);
377 natm_usr_abort(struct socket *so)
379 return (natm_usr_shutdown(so));
383 natm_usr_bind(struct socket *so, struct sockaddr *nam, d_thread_t *p)
389 natm_usr_sockaddr(struct socket *so, struct sockaddr **nam)
394 /* xxx - should be const */
395 struct pr_usrreqs natm_usrreqs = {
396 .pru_abort = natm_usr_abort,
397 .pru_attach = natm_usr_attach,
398 .pru_bind = natm_usr_bind,
399 .pru_connect = natm_usr_connect,
400 .pru_control = natm_usr_control,
401 .pru_detach = natm_usr_detach,
402 .pru_disconnect = natm_usr_disconnect,
403 .pru_peeraddr = natm_usr_peeraddr,
404 .pru_send = natm_usr_send,
405 .pru_shutdown = natm_usr_shutdown,
406 .pru_sockaddr = natm_usr_sockaddr,
410 * natmintr: interrupt
412 * note: we expect a socket pointer in rcvif rather than an interface
413 * pointer. we can get the interface pointer from the so's PCB if
417 natmintr(struct mbuf *m)
420 struct natmpcb *npcb;
427 npcb = (struct natmpcb *)m->m_pkthdr.rcvif; /* XXX: overloaded */
428 so = npcb->npcb_socket;
432 if (npcb->npcb_flags & NPCB_DRAIN) {
433 if (npcb->npcb_inq == 0)
434 FREE(npcb, M_PCB); /* done! */
440 if (npcb->npcb_flags & NPCB_FREE) {
442 m_freem(m); /* drop */
446 #ifdef NEED_TO_RESTORE_IFP
447 m->m_pkthdr.rcvif = npcb->npcb_ifp;
450 m->m_pkthdr.rcvif = NULL; /* null it out to be safe */
454 if (sbspace(&so->so_rcv) > m->m_pkthdr.len) {
457 natm_sookbytes += m->m_pkthdr.len;
459 sbappendrecord(&so->so_rcv, m);
465 natm_sodropbytes += m->m_pkthdr.len;
473 * natm0_sysctl: not used, but here in case we want to add something
477 natm0_sysctl(SYSCTL_HANDLER_ARGS)
479 /* All sysctl names at this level are terminal. */
484 * natm5_sysctl: not used, but here in case we want to add something
488 natm5_sysctl(SYSCTL_HANDLER_ARGS)
490 /* All sysctl names at this level are terminal. */