1 /* $NetBSD: natm.c,v 1.5 1996/11/09 03:26:26 chuck Exp $ */
2 /* $FreeBSD: src/sys/netnatm/natm.c,v 1.12 2000/02/13 03:32:03 peter Exp $ */
3 /* $DragonFly: src/sys/netproto/natm/natm.c,v 1.3 2003/07/21 07:57:51 dillon Exp $ */
7 * Copyright (c) 1996 Charles D. Cranor and Washington University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Charles D. Cranor and
21 * Washington University.
22 * 4. The name of the author may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 * natm.c: native mode ATM access (both aal0 and aal5).
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/sockio.h>
45 #include <sys/protosw.h>
46 #include <sys/malloc.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
52 #include <net/if_atm.h>
53 #include <net/netisr.h>
55 #include <netinet/in.h>
57 #include <netnatm/natm.h>
59 static u_long natm5_sendspace = 16*1024;
60 static u_long natm5_recvspace = 16*1024;
62 static u_long natm0_sendspace = 16*1024;
63 static u_long natm0_recvspace = 16*1024;
68 #ifdef FREEBSD_USRREQS
70 * FreeBSD new usrreqs supersedes pr_usrreq.
72 static int natm_usr_attach __P((struct socket *, int, struct thread *));
73 static int natm_usr_detach __P((struct socket *));
74 static int natm_usr_connect __P((struct socket *, struct sockaddr *,
76 static int natm_usr_disconnect __P((struct socket *));
77 static int natm_usr_shutdown __P((struct socket *));
78 static int natm_usr_send __P((struct socket *, int, struct mbuf *,
79 struct sockaddr *, struct mbuf *,
81 static int natm_usr_peeraddr __P((struct socket *, struct sockaddr **));
82 static int natm_usr_control __P((struct socket *, u_long, caddr_t,
83 struct ifnet *, struct thread *));
84 static int natm_usr_abort __P((struct socket *));
85 static int natm_usr_bind __P((struct socket *, struct sockaddr *,
87 static int natm_usr_sockaddr __P((struct socket *, struct sockaddr **));
90 natm_usr_attach(struct socket *so, int proto, struct thread *td)
96 npcb = (struct natmpcb *) so->so_pcb;
103 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
104 if (proto == PROTO_NATMAAL5)
105 error = soreserve(so, natm5_sendspace, natm5_recvspace);
107 error = soreserve(so, natm0_sendspace, natm0_recvspace);
112 so->so_pcb = (caddr_t) (npcb = npcb_alloc(M_WAITOK));
113 npcb->npcb_socket = so;
120 natm_usr_detach(struct socket *so)
122 struct natmpcb *npcb;
124 int s = SPLSOFTNET();
126 npcb = (struct natmpcb *) so->so_pcb;
133 * we turn on 'drain' *before* we sofree.
135 npcb_free(npcb, NPCB_DESTROY); /* drain */
144 natm_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
146 struct natmpcb *npcb;
147 struct sockaddr_natm *snatm;
148 struct atm_pseudoioctl api;
151 int s2, s = SPLSOFTNET();
152 int proto = so->so_proto->pr_protocol;
154 npcb = (struct natmpcb *) so->so_pcb;
161 * validate nam and npcb
164 snatm = (struct sockaddr_natm *)nam;
165 if (snatm->snatm_len != sizeof(*snatm) ||
166 (npcb->npcb_flags & NPCB_FREE) == 0) {
170 if (snatm->snatm_family != AF_NATM) {
171 error = EAFNOSUPPORT;
175 snatm->snatm_if[IFNAMSIZ-1] = '\0'; /* XXX ensure null termination
176 since ifunit() uses strcmp */
179 * convert interface string to ifp, validate.
182 ifp = ifunit(snatm->snatm_if);
183 if (ifp == NULL || (ifp->if_flags & IFF_RUNNING) == 0) {
187 if (ifp->if_output != atm_output) {
188 error = EAFNOSUPPORT;
193 * register us with the NATM PCB layer
196 if (npcb_add(npcb, ifp, snatm->snatm_vci, snatm->snatm_vpi) != npcb) {
205 ATM_PH_FLAGS(&api.aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0;
206 ATM_PH_VPI(&api.aph) = npcb->npcb_vpi;
207 ATM_PH_SETVCI(&api.aph, npcb->npcb_vci);
210 if (ifp->if_ioctl == NULL ||
211 ifp->if_ioctl(ifp, SIOCATMENA, (caddr_t) &api) != 0) {
213 npcb_free(npcb, NPCB_REMOVE);
227 natm_usr_disconnect(struct socket *so)
229 struct natmpcb *npcb;
230 struct atm_pseudoioctl api;
233 int s2, s = SPLSOFTNET();
235 npcb = (struct natmpcb *) so->so_pcb;
241 if ((npcb->npcb_flags & NPCB_CONNECTED) == 0) {
242 printf("natm: disconnected check\n");
246 ifp = npcb->npcb_ifp;
252 ATM_PH_FLAGS(&api.aph) = ATM_PH_AAL5;
253 ATM_PH_VPI(&api.aph) = npcb->npcb_vpi;
254 ATM_PH_SETVCI(&api.aph, npcb->npcb_vci);
257 if (ifp->if_ioctl != NULL)
258 ifp->if_ioctl(ifp, SIOCATMDIS, (caddr_t) &api);
261 npcb_free(npcb, NPCB_REMOVE);
262 soisdisconnected(so);
270 natm_usr_shutdown(struct socket *so)
277 natm_usr_send(struct socket *so, int flags, struct mbuf *m,
278 struct sockaddr *nam, struct mbuf *control, struct thread *td)
280 struct natmpcb *npcb;
281 struct atm_pseudohdr *aph;
283 int s = SPLSOFTNET();
284 int proto = so->so_proto->pr_protocol;
286 npcb = (struct natmpcb *) so->so_pcb;
292 if (control && control->m_len) {
300 * send the data. we must put an atm_pseudohdr on first
303 M_PREPEND(m, sizeof(*aph), M_WAITOK);
308 aph = mtod(m, struct atm_pseudohdr *);
309 ATM_PH_VPI(aph) = npcb->npcb_vpi;
310 ATM_PH_SETVCI(aph, npcb->npcb_vci);
311 ATM_PH_FLAGS(aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0;
313 error = atm_output(npcb->npcb_ifp, m, NULL, NULL);
321 natm_usr_peeraddr(struct socket *so, struct sockaddr **nam)
323 struct natmpcb *npcb;
324 struct sockaddr_natm *snatm, ssnatm;
326 int s = SPLSOFTNET();
328 npcb = (struct natmpcb *) so->so_pcb;
335 bzero(snatm, sizeof(*snatm));
336 snatm->snatm_len = sizeof(*snatm);
337 snatm->snatm_family = AF_NATM;
338 snprintf(snatm->snatm_if, sizeof(snatm->snatm_if),
339 "%s%d", npcb->npcb_ifp->if_name, npcb->npcb_ifp->if_unit);
340 snatm->snatm_vci = npcb->npcb_vci;
341 snatm->snatm_vpi = npcb->npcb_vpi;
342 *nam = dup_sockaddr((struct sockaddr *)snatm, 0);
350 natm_usr_control(struct socket *so, u_long cmd, caddr_t arg,
351 struct ifnet *ifp, struct thread *td)
353 struct natmpcb *npcb;
354 struct atm_rawioctl ario;
356 int s = SPLSOFTNET();
358 npcb = (struct natmpcb *) so->so_pcb;
365 * raw atm ioctl. comes in as a SIOCRAWATM. we convert it to
366 * SIOCXRAWATM and pass it to the driver.
368 if (cmd == SIOCRAWATM) {
369 if (npcb->npcb_ifp == NULL) {
374 ario.rawvalue = *((int *)arg);
375 error = npcb->npcb_ifp->if_ioctl(npcb->npcb_ifp,
376 SIOCXRAWATM, (caddr_t) &ario);
379 npcb->npcb_flags |= NPCB_RAW;
381 npcb->npcb_flags &= ~(NPCB_RAW);
393 natm_usr_abort(struct socket *so)
395 return natm_usr_shutdown(so);
399 natm_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
405 natm_usr_sockaddr(struct socket *so, struct sockaddr **nam)
410 /* xxx - should be const */
411 struct pr_usrreqs natm_usrreqs = {
412 natm_usr_abort, pru_accept_notsupp, natm_usr_attach, natm_usr_bind,
413 natm_usr_connect, pru_connect2_notsupp, natm_usr_control,
414 natm_usr_detach, natm_usr_disconnect, pru_listen_notsupp,
415 natm_usr_peeraddr, pru_rcvd_notsupp, pru_rcvoob_notsupp,
416 natm_usr_send, pru_sense_null, natm_usr_shutdown,
417 natm_usr_sockaddr, sosend, soreceive, sopoll
420 #else /* !FREEBSD_USRREQS */
422 #if defined(__NetBSD__) || defined(__OpenBSD__)
423 int natm_usrreq(so, req, m, nam, control, p)
424 #elif defined(__FreeBSD__)
425 int natm_usrreq(so, req, m, nam, control)
430 struct mbuf *m, *nam, *control;
431 #if defined(__NetBSD__) || defined(__OpenBSD__)
436 int error = 0, s, s2;
437 struct natmpcb *npcb;
438 struct sockaddr_natm *snatm;
439 struct atm_pseudoioctl api;
440 struct atm_pseudohdr *aph;
441 struct atm_rawioctl ario;
443 int proto = so->so_proto->pr_protocol;
447 npcb = (struct natmpcb *) so->so_pcb;
449 if (npcb == NULL && req != PRU_ATTACH) {
456 case PRU_ATTACH: /* attach protocol to up */
463 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
464 if (proto == PROTO_NATMAAL5)
465 error = soreserve(so, natm5_sendspace, natm5_recvspace);
467 error = soreserve(so, natm0_sendspace, natm0_recvspace);
472 so->so_pcb = (caddr_t) (npcb = npcb_alloc(M_WAITOK));
473 npcb->npcb_socket = so;
477 case PRU_DETACH: /* detach protocol from up */
480 * we turn on 'drain' *before* we sofree.
483 npcb_free(npcb, NPCB_DESTROY); /* drain */
489 case PRU_CONNECT: /* establish connection to peer */
492 * validate nam and npcb
495 if (nam->m_len != sizeof(*snatm)) {
499 snatm = mtod(nam, struct sockaddr_natm *);
500 if (snatm->snatm_len != sizeof(*snatm) ||
501 (npcb->npcb_flags & NPCB_FREE) == 0) {
505 if (snatm->snatm_family != AF_NATM) {
506 error = EAFNOSUPPORT;
510 snatm->snatm_if[IFNAMSIZ-1] = '\0'; /* XXX ensure null termination
511 since ifunit() uses strcmp */
514 * convert interface string to ifp, validate.
517 ifp = ifunit(snatm->snatm_if);
518 if (ifp == NULL || (ifp->if_flags & IFF_RUNNING) == 0) {
522 if (ifp->if_output != atm_output) {
523 error = EAFNOSUPPORT;
529 * register us with the NATM PCB layer
532 if (npcb_add(npcb, ifp, snatm->snatm_vci, snatm->snatm_vpi) != npcb) {
541 ATM_PH_FLAGS(&api.aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0;
542 ATM_PH_VPI(&api.aph) = npcb->npcb_vpi;
543 ATM_PH_SETVCI(&api.aph, npcb->npcb_vci);
546 if (ifp->if_ioctl == NULL ||
547 ifp->if_ioctl(ifp, SIOCATMENA, (caddr_t) &api) != 0) {
549 npcb_free(npcb, NPCB_REMOVE);
559 case PRU_DISCONNECT: /* disconnect from peer */
561 if ((npcb->npcb_flags & NPCB_CONNECTED) == 0) {
562 printf("natm: disconnected check\n");
566 ifp = npcb->npcb_ifp;
572 ATM_PH_FLAGS(&api.aph) = ATM_PH_AAL5;
573 ATM_PH_VPI(&api.aph) = npcb->npcb_vpi;
574 ATM_PH_SETVCI(&api.aph, npcb->npcb_vci);
577 if (ifp->if_ioctl != NULL)
578 ifp->if_ioctl(ifp, SIOCATMDIS, (caddr_t) &api);
581 npcb_free(npcb, NPCB_REMOVE);
582 soisdisconnected(so);
586 case PRU_SHUTDOWN: /* won't send any more data */
590 case PRU_SEND: /* send this data */
591 if (control && control->m_len) {
599 * send the data. we must put an atm_pseudohdr on first
602 M_PREPEND(m, sizeof(*aph), M_WAITOK);
607 aph = mtod(m, struct atm_pseudohdr *);
608 ATM_PH_VPI(aph) = npcb->npcb_vpi;
609 ATM_PH_SETVCI(aph, npcb->npcb_vci);
610 ATM_PH_FLAGS(aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0;
612 error = atm_output(npcb->npcb_ifp, m, NULL, NULL);
616 case PRU_SENSE: /* return status into m */
620 case PRU_PEERADDR: /* fetch peer's address */
621 snatm = mtod(nam, struct sockaddr_natm *);
622 bzero(snatm, sizeof(*snatm));
623 nam->m_len = snatm->snatm_len = sizeof(*snatm);
624 snatm->snatm_family = AF_NATM;
625 #if defined(__NetBSD__) || defined(__OpenBSD__)
626 bcopy(npcb->npcb_ifp->if_xname, snatm->snatm_if, sizeof(snatm->snatm_if));
627 #elif defined(__FreeBSD__)
628 snprintf(snatm->snatm_if, sizeof(snatm->snatm_if),
629 "%s%d", npcb->npcb_ifp->if_name, npcb->npcb_ifp->if_unit);
631 snatm->snatm_vci = npcb->npcb_vci;
632 snatm->snatm_vpi = npcb->npcb_vpi;
635 case PRU_CONTROL: /* control operations on protocol */
637 * raw atm ioctl. comes in as a SIOCRAWATM. we convert it to
638 * SIOCXRAWATM and pass it to the driver.
640 if ((u_long)m == SIOCRAWATM) {
641 if (npcb->npcb_ifp == NULL) {
646 ario.rawvalue = *((int *)nam);
647 error = npcb->npcb_ifp->if_ioctl(npcb->npcb_ifp,
648 SIOCXRAWATM, (caddr_t) &ario);
651 npcb->npcb_flags |= NPCB_RAW;
653 npcb->npcb_flags &= ~(NPCB_RAW);
662 case PRU_BIND: /* bind socket to address */
663 case PRU_LISTEN: /* listen for connection */
664 case PRU_ACCEPT: /* accept connection from peer */
665 case PRU_CONNECT2: /* connect two sockets */
666 case PRU_ABORT: /* abort (fast DISCONNECT, DETATCH) */
667 /* (only happens if LISTEN socket) */
668 case PRU_RCVD: /* have taken data; more room now */
669 case PRU_FASTTIMO: /* 200ms timeout */
670 case PRU_SLOWTIMO: /* 500ms timeout */
671 case PRU_RCVOOB: /* retrieve out of band data */
672 case PRU_SENDOOB: /* send out of band data */
673 case PRU_PROTORCV: /* receive from below */
674 case PRU_PROTOSEND: /* send to below */
675 case PRU_SOCKADDR: /* fetch socket's address */
677 printf("natm: PRU #%d unsupported\n", req);
682 default: panic("natm usrreq");
690 #endif /* !FREEBSD_USRREQS */
693 * natmintr: splsoftnet interrupt
695 * note: we expect a socket pointer in rcvif rather than an interface
696 * pointer. we can get the interface pointer from the so's PCB if
707 struct natmpcb *npcb;
711 IF_DEQUEUE(&natmintrq, m);
717 if ((m->m_flags & M_PKTHDR) == 0)
718 panic("natmintr no HDR");
721 npcb = (struct natmpcb *) m->m_pkthdr.rcvif; /* XXX: overloaded */
722 so = npcb->npcb_socket;
724 s = splimp(); /* could have atm devs @ different levels */
728 if (npcb->npcb_flags & NPCB_DRAIN) {
730 if (npcb->npcb_inq == 0)
731 FREE(npcb, M_PCB); /* done! */
735 if (npcb->npcb_flags & NPCB_FREE) {
736 m_freem(m); /* drop */
740 #ifdef NEED_TO_RESTORE_IFP
741 m->m_pkthdr.rcvif = npcb->npcb_ifp;
744 m->m_pkthdr.rcvif = NULL; /* null it out to be safe */
748 if (sbspace(&so->so_rcv) > m->m_pkthdr.len ||
749 ((npcb->npcb_flags & NPCB_RAW) != 0 && so->so_rcv.sb_cc < NPCB_RAWCC) ) {
752 natm_sookbytes += m->m_pkthdr.len;
754 sbappendrecord(&so->so_rcv, m);
759 natm_sodropbytes += m->m_pkthdr.len;
767 #if defined(__FreeBSD__)
769 netisr_natm_setup(void *dummy __unused)
772 register_netisr(NETISR_NATM, natmintr);
774 SYSINIT(natm_setup, SI_SUB_CPU, SI_ORDER_ANY, netisr_natm_setup, NULL);
779 * natm0_sysctl: not used, but here in case we want to add something
783 int natm0_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
793 /* All sysctl names at this level are terminal. */
796 return (ENOPROTOOPT);
800 * natm5_sysctl: not used, but here in case we want to add something
804 int natm5_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
814 /* All sysctl names at this level are terminal. */
817 return (ENOPROTOOPT);