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 $ */
6 * Copyright (c) 1996 Charles D. Cranor and Washington University.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Charles D. Cranor and
20 * Washington University.
21 * 4. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * natm.c: native mode ATM access (both aal0 and aal5).
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/sockio.h>
44 #include <sys/protosw.h>
45 #include <sys/malloc.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
51 #include <net/if_atm.h>
52 #include <net/netisr.h>
54 #include <netinet/in.h>
56 #include <netnatm/natm.h>
58 static u_long natm5_sendspace = 16*1024;
59 static u_long natm5_recvspace = 16*1024;
61 static u_long natm0_sendspace = 16*1024;
62 static u_long natm0_recvspace = 16*1024;
67 #ifdef FREEBSD_USRREQS
69 * FreeBSD new usrreqs supersedes pr_usrreq.
71 static int natm_usr_attach __P((struct socket *, int, struct proc *));
72 static int natm_usr_detach __P((struct socket *));
73 static int natm_usr_connect __P((struct socket *, struct sockaddr *,
75 static int natm_usr_disconnect __P((struct socket *));
76 static int natm_usr_shutdown __P((struct socket *));
77 static int natm_usr_send __P((struct socket *, int, struct mbuf *,
78 struct sockaddr *, struct mbuf *,
80 static int natm_usr_peeraddr __P((struct socket *, struct sockaddr **));
81 static int natm_usr_control __P((struct socket *, u_long, caddr_t,
82 struct ifnet *, struct proc *));
83 static int natm_usr_abort __P((struct socket *));
84 static int natm_usr_bind __P((struct socket *, struct sockaddr *,
86 static int natm_usr_sockaddr __P((struct socket *, struct sockaddr **));
89 natm_usr_attach(struct socket *so, int proto, struct proc *p)
95 npcb = (struct natmpcb *) so->so_pcb;
102 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
103 if (proto == PROTO_NATMAAL5)
104 error = soreserve(so, natm5_sendspace, natm5_recvspace);
106 error = soreserve(so, natm0_sendspace, natm0_recvspace);
111 so->so_pcb = (caddr_t) (npcb = npcb_alloc(M_WAITOK));
112 npcb->npcb_socket = so;
119 natm_usr_detach(struct socket *so)
121 struct natmpcb *npcb;
123 int s = SPLSOFTNET();
125 npcb = (struct natmpcb *) so->so_pcb;
132 * we turn on 'drain' *before* we sofree.
134 npcb_free(npcb, NPCB_DESTROY); /* drain */
143 natm_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
145 struct natmpcb *npcb;
146 struct sockaddr_natm *snatm;
147 struct atm_pseudoioctl api;
150 int s2, s = SPLSOFTNET();
151 int proto = so->so_proto->pr_protocol;
153 npcb = (struct natmpcb *) so->so_pcb;
160 * 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.
181 ifp = ifunit(snatm->snatm_if);
182 if (ifp == NULL || (ifp->if_flags & IFF_RUNNING) == 0) {
186 if (ifp->if_output != atm_output) {
187 error = EAFNOSUPPORT;
192 * register us with the NATM PCB layer
195 if (npcb_add(npcb, ifp, snatm->snatm_vci, snatm->snatm_vpi) != npcb) {
204 ATM_PH_FLAGS(&api.aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0;
205 ATM_PH_VPI(&api.aph) = npcb->npcb_vpi;
206 ATM_PH_SETVCI(&api.aph, npcb->npcb_vci);
209 if (ifp->if_ioctl == NULL ||
210 ifp->if_ioctl(ifp, SIOCATMENA, (caddr_t) &api) != 0) {
212 npcb_free(npcb, NPCB_REMOVE);
226 natm_usr_disconnect(struct socket *so)
228 struct natmpcb *npcb;
229 struct atm_pseudoioctl api;
232 int s2, s = SPLSOFTNET();
234 npcb = (struct natmpcb *) so->so_pcb;
240 if ((npcb->npcb_flags & NPCB_CONNECTED) == 0) {
241 printf("natm: disconnected check\n");
245 ifp = npcb->npcb_ifp;
251 ATM_PH_FLAGS(&api.aph) = ATM_PH_AAL5;
252 ATM_PH_VPI(&api.aph) = npcb->npcb_vpi;
253 ATM_PH_SETVCI(&api.aph, npcb->npcb_vci);
256 if (ifp->if_ioctl != NULL)
257 ifp->if_ioctl(ifp, SIOCATMDIS, (caddr_t) &api);
260 npcb_free(npcb, NPCB_REMOVE);
261 soisdisconnected(so);
269 natm_usr_shutdown(struct socket *so)
276 natm_usr_send(struct socket *so, int flags, struct mbuf *m,
277 struct sockaddr *nam, struct mbuf *control, struct proc *p)
279 struct natmpcb *npcb;
280 struct atm_pseudohdr *aph;
282 int s = SPLSOFTNET();
283 int proto = so->so_proto->pr_protocol;
285 npcb = (struct natmpcb *) so->so_pcb;
291 if (control && control->m_len) {
299 * send the data. we must put an atm_pseudohdr on first
302 M_PREPEND(m, sizeof(*aph), M_WAITOK);
307 aph = mtod(m, struct atm_pseudohdr *);
308 ATM_PH_VPI(aph) = npcb->npcb_vpi;
309 ATM_PH_SETVCI(aph, npcb->npcb_vci);
310 ATM_PH_FLAGS(aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0;
312 error = atm_output(npcb->npcb_ifp, m, NULL, NULL);
320 natm_usr_peeraddr(struct socket *so, struct sockaddr **nam)
322 struct natmpcb *npcb;
323 struct sockaddr_natm *snatm, ssnatm;
325 int s = SPLSOFTNET();
327 npcb = (struct natmpcb *) so->so_pcb;
334 bzero(snatm, sizeof(*snatm));
335 snatm->snatm_len = sizeof(*snatm);
336 snatm->snatm_family = AF_NATM;
337 snprintf(snatm->snatm_if, sizeof(snatm->snatm_if),
338 "%s%d", npcb->npcb_ifp->if_name, npcb->npcb_ifp->if_unit);
339 snatm->snatm_vci = npcb->npcb_vci;
340 snatm->snatm_vpi = npcb->npcb_vpi;
341 *nam = dup_sockaddr((struct sockaddr *)snatm, 0);
349 natm_usr_control(struct socket *so, u_long cmd, caddr_t arg,
350 struct ifnet *ifp, struct proc *p)
352 struct natmpcb *npcb;
353 struct atm_rawioctl ario;
355 int s = SPLSOFTNET();
357 npcb = (struct natmpcb *) so->so_pcb;
364 * raw atm ioctl. comes in as a SIOCRAWATM. we convert it to
365 * SIOCXRAWATM and pass it to the driver.
367 if (cmd == SIOCRAWATM) {
368 if (npcb->npcb_ifp == NULL) {
373 ario.rawvalue = *((int *)arg);
374 error = npcb->npcb_ifp->if_ioctl(npcb->npcb_ifp,
375 SIOCXRAWATM, (caddr_t) &ario);
378 npcb->npcb_flags |= NPCB_RAW;
380 npcb->npcb_flags &= ~(NPCB_RAW);
392 natm_usr_abort(struct socket *so)
394 return natm_usr_shutdown(so);
398 natm_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
404 natm_usr_sockaddr(struct socket *so, struct sockaddr **nam)
409 /* xxx - should be const */
410 struct pr_usrreqs natm_usrreqs = {
411 natm_usr_abort, pru_accept_notsupp, natm_usr_attach, natm_usr_bind,
412 natm_usr_connect, pru_connect2_notsupp, natm_usr_control,
413 natm_usr_detach, natm_usr_disconnect, pru_listen_notsupp,
414 natm_usr_peeraddr, pru_rcvd_notsupp, pru_rcvoob_notsupp,
415 natm_usr_send, pru_sense_null, natm_usr_shutdown,
416 natm_usr_sockaddr, sosend, soreceive, sopoll
419 #else /* !FREEBSD_USRREQS */
421 #if defined(__NetBSD__) || defined(__OpenBSD__)
422 int natm_usrreq(so, req, m, nam, control, p)
423 #elif defined(__FreeBSD__)
424 int natm_usrreq(so, req, m, nam, control)
429 struct mbuf *m, *nam, *control;
430 #if defined(__NetBSD__) || defined(__OpenBSD__)
435 int error = 0, s, s2;
436 struct natmpcb *npcb;
437 struct sockaddr_natm *snatm;
438 struct atm_pseudoioctl api;
439 struct atm_pseudohdr *aph;
440 struct atm_rawioctl ario;
442 int proto = so->so_proto->pr_protocol;
446 npcb = (struct natmpcb *) so->so_pcb;
448 if (npcb == NULL && req != PRU_ATTACH) {
455 case PRU_ATTACH: /* attach protocol to up */
462 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
463 if (proto == PROTO_NATMAAL5)
464 error = soreserve(so, natm5_sendspace, natm5_recvspace);
466 error = soreserve(so, natm0_sendspace, natm0_recvspace);
471 so->so_pcb = (caddr_t) (npcb = npcb_alloc(M_WAITOK));
472 npcb->npcb_socket = so;
476 case PRU_DETACH: /* detach protocol from up */
479 * we turn on 'drain' *before* we sofree.
482 npcb_free(npcb, NPCB_DESTROY); /* drain */
488 case PRU_CONNECT: /* establish connection to peer */
491 * validate nam and npcb
494 if (nam->m_len != sizeof(*snatm)) {
498 snatm = mtod(nam, struct sockaddr_natm *);
499 if (snatm->snatm_len != sizeof(*snatm) ||
500 (npcb->npcb_flags & NPCB_FREE) == 0) {
504 if (snatm->snatm_family != AF_NATM) {
505 error = EAFNOSUPPORT;
509 snatm->snatm_if[IFNAMSIZ-1] = '\0'; /* XXX ensure null termination
510 since ifunit() uses strcmp */
513 * convert interface string to ifp, validate.
516 ifp = ifunit(snatm->snatm_if);
517 if (ifp == NULL || (ifp->if_flags & IFF_RUNNING) == 0) {
521 if (ifp->if_output != atm_output) {
522 error = EAFNOSUPPORT;
528 * register us with the NATM PCB layer
531 if (npcb_add(npcb, ifp, snatm->snatm_vci, snatm->snatm_vpi) != npcb) {
540 ATM_PH_FLAGS(&api.aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0;
541 ATM_PH_VPI(&api.aph) = npcb->npcb_vpi;
542 ATM_PH_SETVCI(&api.aph, npcb->npcb_vci);
545 if (ifp->if_ioctl == NULL ||
546 ifp->if_ioctl(ifp, SIOCATMENA, (caddr_t) &api) != 0) {
548 npcb_free(npcb, NPCB_REMOVE);
558 case PRU_DISCONNECT: /* disconnect from peer */
560 if ((npcb->npcb_flags & NPCB_CONNECTED) == 0) {
561 printf("natm: disconnected check\n");
565 ifp = npcb->npcb_ifp;
571 ATM_PH_FLAGS(&api.aph) = ATM_PH_AAL5;
572 ATM_PH_VPI(&api.aph) = npcb->npcb_vpi;
573 ATM_PH_SETVCI(&api.aph, npcb->npcb_vci);
576 if (ifp->if_ioctl != NULL)
577 ifp->if_ioctl(ifp, SIOCATMDIS, (caddr_t) &api);
580 npcb_free(npcb, NPCB_REMOVE);
581 soisdisconnected(so);
585 case PRU_SHUTDOWN: /* won't send any more data */
589 case PRU_SEND: /* send this data */
590 if (control && control->m_len) {
598 * send the data. we must put an atm_pseudohdr on first
601 M_PREPEND(m, sizeof(*aph), M_WAITOK);
606 aph = mtod(m, struct atm_pseudohdr *);
607 ATM_PH_VPI(aph) = npcb->npcb_vpi;
608 ATM_PH_SETVCI(aph, npcb->npcb_vci);
609 ATM_PH_FLAGS(aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0;
611 error = atm_output(npcb->npcb_ifp, m, NULL, NULL);
615 case PRU_SENSE: /* return status into m */
619 case PRU_PEERADDR: /* fetch peer's address */
620 snatm = mtod(nam, struct sockaddr_natm *);
621 bzero(snatm, sizeof(*snatm));
622 nam->m_len = snatm->snatm_len = sizeof(*snatm);
623 snatm->snatm_family = AF_NATM;
624 #if defined(__NetBSD__) || defined(__OpenBSD__)
625 bcopy(npcb->npcb_ifp->if_xname, snatm->snatm_if, sizeof(snatm->snatm_if));
626 #elif defined(__FreeBSD__)
627 snprintf(snatm->snatm_if, sizeof(snatm->snatm_if),
628 "%s%d", npcb->npcb_ifp->if_name, npcb->npcb_ifp->if_unit);
630 snatm->snatm_vci = npcb->npcb_vci;
631 snatm->snatm_vpi = npcb->npcb_vpi;
634 case PRU_CONTROL: /* control operations on protocol */
636 * raw atm ioctl. comes in as a SIOCRAWATM. we convert it to
637 * SIOCXRAWATM and pass it to the driver.
639 if ((u_long)m == SIOCRAWATM) {
640 if (npcb->npcb_ifp == NULL) {
645 ario.rawvalue = *((int *)nam);
646 error = npcb->npcb_ifp->if_ioctl(npcb->npcb_ifp,
647 SIOCXRAWATM, (caddr_t) &ario);
650 npcb->npcb_flags |= NPCB_RAW;
652 npcb->npcb_flags &= ~(NPCB_RAW);
661 case PRU_BIND: /* bind socket to address */
662 case PRU_LISTEN: /* listen for connection */
663 case PRU_ACCEPT: /* accept connection from peer */
664 case PRU_CONNECT2: /* connect two sockets */
665 case PRU_ABORT: /* abort (fast DISCONNECT, DETATCH) */
666 /* (only happens if LISTEN socket) */
667 case PRU_RCVD: /* have taken data; more room now */
668 case PRU_FASTTIMO: /* 200ms timeout */
669 case PRU_SLOWTIMO: /* 500ms timeout */
670 case PRU_RCVOOB: /* retrieve out of band data */
671 case PRU_SENDOOB: /* send out of band data */
672 case PRU_PROTORCV: /* receive from below */
673 case PRU_PROTOSEND: /* send to below */
674 case PRU_SOCKADDR: /* fetch socket's address */
676 printf("natm: PRU #%d unsupported\n", req);
681 default: panic("natm usrreq");
689 #endif /* !FREEBSD_USRREQS */
692 * natmintr: splsoftnet interrupt
694 * note: we expect a socket pointer in rcvif rather than an interface
695 * pointer. we can get the interface pointer from the so's PCB if
706 struct natmpcb *npcb;
710 IF_DEQUEUE(&natmintrq, m);
716 if ((m->m_flags & M_PKTHDR) == 0)
717 panic("natmintr no HDR");
720 npcb = (struct natmpcb *) m->m_pkthdr.rcvif; /* XXX: overloaded */
721 so = npcb->npcb_socket;
723 s = splimp(); /* could have atm devs @ different levels */
727 if (npcb->npcb_flags & NPCB_DRAIN) {
729 if (npcb->npcb_inq == 0)
730 FREE(npcb, M_PCB); /* done! */
734 if (npcb->npcb_flags & NPCB_FREE) {
735 m_freem(m); /* drop */
739 #ifdef NEED_TO_RESTORE_IFP
740 m->m_pkthdr.rcvif = npcb->npcb_ifp;
743 m->m_pkthdr.rcvif = NULL; /* null it out to be safe */
747 if (sbspace(&so->so_rcv) > m->m_pkthdr.len ||
748 ((npcb->npcb_flags & NPCB_RAW) != 0 && so->so_rcv.sb_cc < NPCB_RAWCC) ) {
751 natm_sookbytes += m->m_pkthdr.len;
753 sbappendrecord(&so->so_rcv, m);
758 natm_sodropbytes += m->m_pkthdr.len;
766 #if defined(__FreeBSD__)
768 netisr_natm_setup(void *dummy __unused)
771 register_netisr(NETISR_NATM, natmintr);
773 SYSINIT(natm_setup, SI_SUB_CPU, SI_ORDER_ANY, netisr_natm_setup, NULL);
778 * natm0_sysctl: not used, but here in case we want to add something
782 int natm0_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
792 /* All sysctl names at this level are terminal. */
795 return (ENOPROTOOPT);
799 * natm5_sysctl: not used, but here in case we want to add something
803 int natm5_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
813 /* All sysctl names at this level are terminal. */
816 return (ENOPROTOOPT);