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.13 2004/03/24 01:58:01 hsu 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>
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 (struct socket *, int, struct pru_attach_info *);
73 static int natm_usr_detach (struct socket *);
74 static int natm_usr_connect (struct socket *, struct sockaddr *,
76 static int natm_usr_disconnect (struct socket *);
77 static int natm_usr_shutdown (struct socket *);
78 static int natm_usr_send (struct socket *, int, struct mbuf *,
79 struct sockaddr *, struct mbuf *,
81 static int natm_usr_peeraddr (struct socket *, struct sockaddr **);
82 static int natm_usr_control (struct socket *, u_long, caddr_t,
83 struct ifnet *, struct thread *);
84 static int natm_usr_abort (struct socket *);
85 static int natm_usr_bind (struct socket *, struct sockaddr *,
87 static int natm_usr_sockaddr (struct socket *, struct sockaddr **);
90 natm_usr_attach(struct socket *so, int proto, struct pru_attach_info *ai)
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,
108 error = soreserve(so, natm0_sendspace, natm0_recvspace,
114 so->so_pcb = (caddr_t) (npcb = npcb_alloc(M_WAITOK));
115 npcb->npcb_socket = so;
122 natm_usr_detach(struct socket *so)
124 struct natmpcb *npcb;
126 int s = SPLSOFTNET();
128 npcb = (struct natmpcb *) so->so_pcb;
135 * we turn on 'drain' *before* we sofree.
137 npcb_free(npcb, NPCB_DESTROY); /* drain */
146 natm_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
148 struct natmpcb *npcb;
149 struct sockaddr_natm *snatm;
150 struct atm_pseudoioctl api;
153 int s2, s = SPLSOFTNET();
154 int proto = so->so_proto->pr_protocol;
156 npcb = (struct natmpcb *) so->so_pcb;
163 * validate nam and npcb
166 snatm = (struct sockaddr_natm *)nam;
167 if (snatm->snatm_len != sizeof(*snatm) ||
168 (npcb->npcb_flags & NPCB_FREE) == 0) {
172 if (snatm->snatm_family != AF_NATM) {
173 error = EAFNOSUPPORT;
177 snatm->snatm_if[IFNAMSIZ-1] = '\0'; /* XXX ensure null termination
178 since ifunit() uses strcmp */
181 * convert interface string to ifp, validate.
184 ifp = ifunit(snatm->snatm_if);
185 if (ifp == NULL || (ifp->if_flags & IFF_RUNNING) == 0) {
189 if (ifp->if_output != atm_output) {
190 error = EAFNOSUPPORT;
195 * register us with the NATM PCB layer
198 if (npcb_add(npcb, ifp, snatm->snatm_vci, snatm->snatm_vpi) != npcb) {
207 ATM_PH_FLAGS(&api.aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0;
208 ATM_PH_VPI(&api.aph) = npcb->npcb_vpi;
209 ATM_PH_SETVCI(&api.aph, npcb->npcb_vci);
212 if (ifp->if_ioctl == NULL ||
213 ifp->if_ioctl(ifp, SIOCATMENA, (caddr_t) &api,
214 td->td_proc->p_ucred) != 0) {
216 npcb_free(npcb, NPCB_REMOVE);
230 natm_usr_disconnect(struct socket *so)
232 struct natmpcb *npcb;
233 struct atm_pseudoioctl api;
236 int s2, s = SPLSOFTNET();
238 npcb = (struct natmpcb *) so->so_pcb;
244 if ((npcb->npcb_flags & NPCB_CONNECTED) == 0) {
245 printf("natm: disconnected check\n");
249 ifp = npcb->npcb_ifp;
255 ATM_PH_FLAGS(&api.aph) = ATM_PH_AAL5;
256 ATM_PH_VPI(&api.aph) = npcb->npcb_vpi;
257 ATM_PH_SETVCI(&api.aph, npcb->npcb_vci);
260 if (ifp->if_ioctl != NULL)
261 ifp->if_ioctl(ifp, SIOCATMDIS, (caddr_t) &api, (struct ucred *)NULL);
264 npcb_free(npcb, NPCB_REMOVE);
265 soisdisconnected(so);
273 natm_usr_shutdown(struct socket *so)
280 natm_usr_send(struct socket *so, int flags, struct mbuf *m,
281 struct sockaddr *nam, struct mbuf *control, struct thread *td)
283 struct natmpcb *npcb;
284 struct atm_pseudohdr *aph;
286 int s = SPLSOFTNET();
287 int proto = so->so_proto->pr_protocol;
289 npcb = (struct natmpcb *) so->so_pcb;
295 if (control && control->m_len) {
303 * send the data. we must put an atm_pseudohdr on first
306 M_PREPEND(m, sizeof(*aph), M_WAITOK);
311 aph = mtod(m, struct atm_pseudohdr *);
312 ATM_PH_VPI(aph) = npcb->npcb_vpi;
313 ATM_PH_SETVCI(aph, npcb->npcb_vci);
314 ATM_PH_FLAGS(aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0;
316 error = atm_output(npcb->npcb_ifp, m, NULL, NULL);
324 natm_usr_peeraddr(struct socket *so, struct sockaddr **nam)
326 struct natmpcb *npcb;
327 struct sockaddr_natm *snatm, ssnatm;
329 int s = SPLSOFTNET();
331 npcb = (struct natmpcb *) so->so_pcb;
338 bzero(snatm, sizeof(*snatm));
339 snatm->snatm_len = sizeof(*snatm);
340 snatm->snatm_family = AF_NATM;
341 strlcpy(snatm->snatm_if, npcb->npcb_ifp->if_xname,
342 sizeof(snatm->snatm_if));
343 snatm->snatm_vci = npcb->npcb_vci;
344 snatm->snatm_vpi = npcb->npcb_vpi;
345 *nam = dup_sockaddr((struct sockaddr *)snatm, 0);
353 natm_usr_control(struct socket *so, u_long cmd, caddr_t arg,
354 struct ifnet *ifp, struct thread *td)
356 struct natmpcb *npcb;
357 struct atm_rawioctl ario;
359 int s = SPLSOFTNET();
361 npcb = (struct natmpcb *) so->so_pcb;
368 * raw atm ioctl. comes in as a SIOCRAWATM. we convert it to
369 * SIOCXRAWATM and pass it to the driver.
371 if (cmd == SIOCRAWATM) {
372 if (npcb->npcb_ifp == NULL) {
377 ario.rawvalue = *((int *)arg);
378 error = npcb->npcb_ifp->if_ioctl(npcb->npcb_ifp,
379 SIOCXRAWATM, (caddr_t) &ario,
380 td->td_proc->p_ucred);
383 npcb->npcb_flags |= NPCB_RAW;
385 npcb->npcb_flags &= ~(NPCB_RAW);
397 natm_usr_abort(struct socket *so)
399 return natm_usr_shutdown(so);
403 natm_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
409 natm_usr_sockaddr(struct socket *so, struct sockaddr **nam)
414 /* xxx - should be const */
415 struct pr_usrreqs natm_usrreqs = {
416 natm_usr_abort, pru_accept_notsupp, natm_usr_attach, natm_usr_bind,
417 natm_usr_connect, pru_connect2_notsupp, natm_usr_control,
418 natm_usr_detach, natm_usr_disconnect, pru_listen_notsupp,
419 natm_usr_peeraddr, pru_rcvd_notsupp, pru_rcvoob_notsupp,
420 natm_usr_send, pru_sense_null, natm_usr_shutdown,
421 natm_usr_sockaddr, sosend, soreceive, sopoll
424 #else /* !FREEBSD_USRREQS */
426 #if defined(__NetBSD__) || defined(__OpenBSD__)
427 int natm_usrreq(so, req, m, nam, control, p)
428 #elif defined(__DragonFly__)
429 int natm_usrreq(so, req, m, nam, control)
434 struct mbuf *m, *nam, *control;
435 #if defined(__NetBSD__) || defined(__OpenBSD__)
440 int error = 0, s, s2;
441 struct natmpcb *npcb;
442 struct sockaddr_natm *snatm;
443 struct atm_pseudoioctl api;
444 struct atm_pseudohdr *aph;
445 struct atm_rawioctl ario;
447 int proto = so->so_proto->pr_protocol;
451 npcb = (struct natmpcb *) so->so_pcb;
453 if (npcb == NULL && req != PRU_ATTACH) {
460 case PRU_ATTACH: /* attach protocol to up */
467 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
468 if (proto == PROTO_NATMAAL5)
469 error = soreserve(so, natm5_sendspace, natm5_recvspace);
471 error = soreserve(so, natm0_sendspace, natm0_recvspace);
476 so->so_pcb = (caddr_t) (npcb = npcb_alloc(M_WAITOK));
477 npcb->npcb_socket = so;
481 case PRU_DETACH: /* detach protocol from up */
484 * we turn on 'drain' *before* we sofree.
487 npcb_free(npcb, NPCB_DESTROY); /* drain */
493 case PRU_CONNECT: /* establish connection to peer */
496 * validate nam and npcb
499 if (nam->m_len != sizeof(*snatm)) {
503 snatm = mtod(nam, struct sockaddr_natm *);
504 if (snatm->snatm_len != sizeof(*snatm) ||
505 (npcb->npcb_flags & NPCB_FREE) == 0) {
509 if (snatm->snatm_family != AF_NATM) {
510 error = EAFNOSUPPORT;
514 snatm->snatm_if[IFNAMSIZ-1] = '\0'; /* XXX ensure null termination
515 since ifunit() uses strcmp */
518 * convert interface string to ifp, validate.
521 ifp = ifunit(snatm->snatm_if);
522 if (ifp == NULL || (ifp->if_flags & IFF_RUNNING) == 0) {
526 if (ifp->if_output != atm_output) {
527 error = EAFNOSUPPORT;
533 * register us with the NATM PCB layer
536 if (npcb_add(npcb, ifp, snatm->snatm_vci, snatm->snatm_vpi) != npcb) {
545 ATM_PH_FLAGS(&api.aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0;
546 ATM_PH_VPI(&api.aph) = npcb->npcb_vpi;
547 ATM_PH_SETVCI(&api.aph, npcb->npcb_vci);
550 if (ifp->if_ioctl == NULL ||
551 ifp->if_ioctl(ifp, SIOCATMENA, (caddr_t) &api,
552 (struct ucred *)NULL) != 0) {
554 npcb_free(npcb, NPCB_REMOVE);
564 case PRU_DISCONNECT: /* disconnect from peer */
566 if ((npcb->npcb_flags & NPCB_CONNECTED) == 0) {
567 printf("natm: disconnected check\n");
571 ifp = npcb->npcb_ifp;
577 ATM_PH_FLAGS(&api.aph) = ATM_PH_AAL5;
578 ATM_PH_VPI(&api.aph) = npcb->npcb_vpi;
579 ATM_PH_SETVCI(&api.aph, npcb->npcb_vci);
582 if (ifp->if_ioctl != NULL)
583 ifp->if_ioctl(ifp, SIOCATMDIS, (caddr_t) &api, (struct ucred *)NULL);
586 npcb_free(npcb, NPCB_REMOVE);
587 soisdisconnected(so);
591 case PRU_SHUTDOWN: /* won't send any more data */
595 case PRU_SEND: /* send this data */
596 if (control && control->m_len) {
604 * send the data. we must put an atm_pseudohdr on first
607 M_PREPEND(m, sizeof(*aph), M_WAITOK);
612 aph = mtod(m, struct atm_pseudohdr *);
613 ATM_PH_VPI(aph) = npcb->npcb_vpi;
614 ATM_PH_SETVCI(aph, npcb->npcb_vci);
615 ATM_PH_FLAGS(aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0;
617 error = atm_output(npcb->npcb_ifp, m, NULL, NULL);
621 case PRU_SENSE: /* return status into m */
625 case PRU_PEERADDR: /* fetch peer's address */
626 snatm = mtod(nam, struct sockaddr_natm *);
627 bzero(snatm, sizeof(*snatm));
628 nam->m_len = snatm->snatm_len = sizeof(*snatm);
629 snatm->snatm_family = AF_NATM;
630 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
631 bcopy(npcb->npcb_ifp->if_xname, snatm->snatm_if, sizeof(snatm->snatm_if));
632 #elif defined(__DragonFly__)
633 snprintf(snatm->snatm_if, sizeof(snatm->snatm_if),
634 "%s%d", npcb->npcb_ifp->if_name, npcb->npcb_ifp->if_unit);
636 snatm->snatm_vci = npcb->npcb_vci;
637 snatm->snatm_vpi = npcb->npcb_vpi;
640 case PRU_CONTROL: /* control operations on protocol */
642 * raw atm ioctl. comes in as a SIOCRAWATM. we convert it to
643 * SIOCXRAWATM and pass it to the driver.
645 if ((u_long)m == SIOCRAWATM) {
646 if (npcb->npcb_ifp == NULL) {
651 ario.rawvalue = *((int *)nam);
652 error = npcb->npcb_ifp->if_ioctl(npcb->npcb_ifp, SIOCXRAWATM,
653 (caddr_t) &ario, (struct ucred *)NULL);
656 npcb->npcb_flags |= NPCB_RAW;
658 npcb->npcb_flags &= ~(NPCB_RAW);
667 case PRU_BIND: /* bind socket to address */
668 case PRU_LISTEN: /* listen for connection */
669 case PRU_ACCEPT: /* accept connection from peer */
670 case PRU_CONNECT2: /* connect two sockets */
671 case PRU_ABORT: /* abort (fast DISCONNECT, DETATCH) */
672 /* (only happens if LISTEN socket) */
673 case PRU_RCVD: /* have taken data; more room now */
674 case PRU_FASTTIMO: /* 200ms timeout */
675 case PRU_SLOWTIMO: /* 500ms timeout */
676 case PRU_RCVOOB: /* retrieve out of band data */
677 case PRU_SENDOOB: /* send out of band data */
678 case PRU_PROTORCV: /* receive from below */
679 case PRU_PROTOSEND: /* send to below */
680 case PRU_SOCKADDR: /* fetch socket's address */
682 printf("natm: PRU #%d unsupported\n", req);
687 default: panic("natm usrreq");
695 #endif /* !FREEBSD_USRREQS */
698 * natm0_sysctl: not used, but here in case we want to add something
702 int natm0_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
703 void *newp, size_t newlen)
705 /* All sysctl names at this level are terminal. */
708 return (ENOPROTOOPT);
712 * natm5_sysctl: not used, but here in case we want to add something
716 int natm5_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
717 void *newp, size_t newlen)
719 /* All sysctl names at this level are terminal. */
722 return (ENOPROTOOPT);
725 static void natmintr(struct netmsg *);
727 #if defined(__DragonFly__)
729 netisr_natm_setup(void *dummy __unused)
732 netisr_register(NETISR_NATM, cpu0_portfn, natmintr);
734 SYSINIT(natm_setup, SI_SUB_CPU, SI_ORDER_ANY, netisr_natm_setup, NULL);
740 LIST_INIT(&natm_pcbs);
742 netisr_register(NETISR_NATM, cpu0_portfn, natmintr);
746 * natmintr: splsoftnet interrupt
748 * note: we expect a socket pointer in rcvif rather than an interface
749 * pointer. we can get the interface pointer from the so's PCB if
753 natmintr(struct netmsg *msg)
755 struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
758 struct natmpcb *npcb;
761 if ((m->m_flags & M_PKTHDR) == 0)
762 panic("natmintr no HDR");
765 npcb = (struct natmpcb *) m->m_pkthdr.rcvif; /* XXX: overloaded */
766 so = npcb->npcb_socket;
768 s = splimp(); /* could have atm devs @ different levels */
772 if (npcb->npcb_flags & NPCB_DRAIN) {
774 if (npcb->npcb_inq == 0)
775 FREE(npcb, M_PCB); /* done! */
779 if (npcb->npcb_flags & NPCB_FREE) {
780 m_freem(m); /* drop */
784 #ifdef NEED_TO_RESTORE_IFP
785 m->m_pkthdr.rcvif = npcb->npcb_ifp;
788 m->m_pkthdr.rcvif = NULL; /* null it out to be safe */
792 if (sbspace(&so->so_rcv) > m->m_pkthdr.len ||
793 ((npcb->npcb_flags & NPCB_RAW) != 0 && so->so_rcv.sb_cc < NPCB_RAWCC) ) {
796 natm_sookbytes += m->m_pkthdr.len;
798 sbappendrecord(&so->so_rcv, m);
803 natm_sodropbytes += m->m_pkthdr.len;