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.11 2004/03/06 01:58:57 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) != 0) {
215 npcb_free(npcb, NPCB_REMOVE);
229 natm_usr_disconnect(struct socket *so)
231 struct natmpcb *npcb;
232 struct atm_pseudoioctl api;
235 int s2, s = SPLSOFTNET();
237 npcb = (struct natmpcb *) so->so_pcb;
243 if ((npcb->npcb_flags & NPCB_CONNECTED) == 0) {
244 printf("natm: disconnected check\n");
248 ifp = npcb->npcb_ifp;
254 ATM_PH_FLAGS(&api.aph) = ATM_PH_AAL5;
255 ATM_PH_VPI(&api.aph) = npcb->npcb_vpi;
256 ATM_PH_SETVCI(&api.aph, npcb->npcb_vci);
259 if (ifp->if_ioctl != NULL)
260 ifp->if_ioctl(ifp, SIOCATMDIS, (caddr_t) &api);
263 npcb_free(npcb, NPCB_REMOVE);
264 soisdisconnected(so);
272 natm_usr_shutdown(struct socket *so)
279 natm_usr_send(struct socket *so, int flags, struct mbuf *m,
280 struct sockaddr *nam, struct mbuf *control, struct thread *td)
282 struct natmpcb *npcb;
283 struct atm_pseudohdr *aph;
285 int s = SPLSOFTNET();
286 int proto = so->so_proto->pr_protocol;
288 npcb = (struct natmpcb *) so->so_pcb;
294 if (control && control->m_len) {
302 * send the data. we must put an atm_pseudohdr on first
305 M_PREPEND(m, sizeof(*aph), M_WAITOK);
310 aph = mtod(m, struct atm_pseudohdr *);
311 ATM_PH_VPI(aph) = npcb->npcb_vpi;
312 ATM_PH_SETVCI(aph, npcb->npcb_vci);
313 ATM_PH_FLAGS(aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0;
315 error = atm_output(npcb->npcb_ifp, m, NULL, NULL);
323 natm_usr_peeraddr(struct socket *so, struct sockaddr **nam)
325 struct natmpcb *npcb;
326 struct sockaddr_natm *snatm, ssnatm;
328 int s = SPLSOFTNET();
330 npcb = (struct natmpcb *) so->so_pcb;
337 bzero(snatm, sizeof(*snatm));
338 snatm->snatm_len = sizeof(*snatm);
339 snatm->snatm_family = AF_NATM;
340 strlcpy(snatm->snatm_if, npcb->npcb_ifp->if_xname,
341 sizeof(snatm->snatm_if));
342 snatm->snatm_vci = npcb->npcb_vci;
343 snatm->snatm_vpi = npcb->npcb_vpi;
344 *nam = dup_sockaddr((struct sockaddr *)snatm, 0);
352 natm_usr_control(struct socket *so, u_long cmd, caddr_t arg,
353 struct ifnet *ifp, struct thread *td)
355 struct natmpcb *npcb;
356 struct atm_rawioctl ario;
358 int s = SPLSOFTNET();
360 npcb = (struct natmpcb *) so->so_pcb;
367 * raw atm ioctl. comes in as a SIOCRAWATM. we convert it to
368 * SIOCXRAWATM and pass it to the driver.
370 if (cmd == SIOCRAWATM) {
371 if (npcb->npcb_ifp == NULL) {
376 ario.rawvalue = *((int *)arg);
377 error = npcb->npcb_ifp->if_ioctl(npcb->npcb_ifp,
378 SIOCXRAWATM, (caddr_t) &ario);
381 npcb->npcb_flags |= NPCB_RAW;
383 npcb->npcb_flags &= ~(NPCB_RAW);
395 natm_usr_abort(struct socket *so)
397 return natm_usr_shutdown(so);
401 natm_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
407 natm_usr_sockaddr(struct socket *so, struct sockaddr **nam)
412 /* xxx - should be const */
413 struct pr_usrreqs natm_usrreqs = {
414 natm_usr_abort, pru_accept_notsupp, natm_usr_attach, natm_usr_bind,
415 natm_usr_connect, pru_connect2_notsupp, natm_usr_control,
416 natm_usr_detach, natm_usr_disconnect, pru_listen_notsupp,
417 natm_usr_peeraddr, pru_rcvd_notsupp, pru_rcvoob_notsupp,
418 natm_usr_send, pru_sense_null, natm_usr_shutdown,
419 natm_usr_sockaddr, sosend, soreceive, sopoll
422 #else /* !FREEBSD_USRREQS */
424 #if defined(__NetBSD__) || defined(__OpenBSD__)
425 int natm_usrreq(so, req, m, nam, control, p)
426 #elif defined(__DragonFly__)
427 int natm_usrreq(so, req, m, nam, control)
432 struct mbuf *m, *nam, *control;
433 #if defined(__NetBSD__) || defined(__OpenBSD__)
438 int error = 0, s, s2;
439 struct natmpcb *npcb;
440 struct sockaddr_natm *snatm;
441 struct atm_pseudoioctl api;
442 struct atm_pseudohdr *aph;
443 struct atm_rawioctl ario;
445 int proto = so->so_proto->pr_protocol;
449 npcb = (struct natmpcb *) so->so_pcb;
451 if (npcb == NULL && req != PRU_ATTACH) {
458 case PRU_ATTACH: /* attach protocol to up */
465 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
466 if (proto == PROTO_NATMAAL5)
467 error = soreserve(so, natm5_sendspace, natm5_recvspace);
469 error = soreserve(so, natm0_sendspace, natm0_recvspace);
474 so->so_pcb = (caddr_t) (npcb = npcb_alloc(M_WAITOK));
475 npcb->npcb_socket = so;
479 case PRU_DETACH: /* detach protocol from up */
482 * we turn on 'drain' *before* we sofree.
485 npcb_free(npcb, NPCB_DESTROY); /* drain */
491 case PRU_CONNECT: /* establish connection to peer */
494 * validate nam and npcb
497 if (nam->m_len != sizeof(*snatm)) {
501 snatm = mtod(nam, struct sockaddr_natm *);
502 if (snatm->snatm_len != sizeof(*snatm) ||
503 (npcb->npcb_flags & NPCB_FREE) == 0) {
507 if (snatm->snatm_family != AF_NATM) {
508 error = EAFNOSUPPORT;
512 snatm->snatm_if[IFNAMSIZ-1] = '\0'; /* XXX ensure null termination
513 since ifunit() uses strcmp */
516 * convert interface string to ifp, validate.
519 ifp = ifunit(snatm->snatm_if);
520 if (ifp == NULL || (ifp->if_flags & IFF_RUNNING) == 0) {
524 if (ifp->if_output != atm_output) {
525 error = EAFNOSUPPORT;
531 * register us with the NATM PCB layer
534 if (npcb_add(npcb, ifp, snatm->snatm_vci, snatm->snatm_vpi) != npcb) {
543 ATM_PH_FLAGS(&api.aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0;
544 ATM_PH_VPI(&api.aph) = npcb->npcb_vpi;
545 ATM_PH_SETVCI(&api.aph, npcb->npcb_vci);
548 if (ifp->if_ioctl == NULL ||
549 ifp->if_ioctl(ifp, SIOCATMENA, (caddr_t) &api) != 0) {
551 npcb_free(npcb, NPCB_REMOVE);
561 case PRU_DISCONNECT: /* disconnect from peer */
563 if ((npcb->npcb_flags & NPCB_CONNECTED) == 0) {
564 printf("natm: disconnected check\n");
568 ifp = npcb->npcb_ifp;
574 ATM_PH_FLAGS(&api.aph) = ATM_PH_AAL5;
575 ATM_PH_VPI(&api.aph) = npcb->npcb_vpi;
576 ATM_PH_SETVCI(&api.aph, npcb->npcb_vci);
579 if (ifp->if_ioctl != NULL)
580 ifp->if_ioctl(ifp, SIOCATMDIS, (caddr_t) &api);
583 npcb_free(npcb, NPCB_REMOVE);
584 soisdisconnected(so);
588 case PRU_SHUTDOWN: /* won't send any more data */
592 case PRU_SEND: /* send this data */
593 if (control && control->m_len) {
601 * send the data. we must put an atm_pseudohdr on first
604 M_PREPEND(m, sizeof(*aph), M_WAITOK);
609 aph = mtod(m, struct atm_pseudohdr *);
610 ATM_PH_VPI(aph) = npcb->npcb_vpi;
611 ATM_PH_SETVCI(aph, npcb->npcb_vci);
612 ATM_PH_FLAGS(aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0;
614 error = atm_output(npcb->npcb_ifp, m, NULL, NULL);
618 case PRU_SENSE: /* return status into m */
622 case PRU_PEERADDR: /* fetch peer's address */
623 snatm = mtod(nam, struct sockaddr_natm *);
624 bzero(snatm, sizeof(*snatm));
625 nam->m_len = snatm->snatm_len = sizeof(*snatm);
626 snatm->snatm_family = AF_NATM;
627 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
628 bcopy(npcb->npcb_ifp->if_xname, snatm->snatm_if, sizeof(snatm->snatm_if));
629 #elif defined(__DragonFly__)
630 snprintf(snatm->snatm_if, sizeof(snatm->snatm_if),
631 "%s%d", npcb->npcb_ifp->if_name, npcb->npcb_ifp->if_unit);
633 snatm->snatm_vci = npcb->npcb_vci;
634 snatm->snatm_vpi = npcb->npcb_vpi;
637 case PRU_CONTROL: /* control operations on protocol */
639 * raw atm ioctl. comes in as a SIOCRAWATM. we convert it to
640 * SIOCXRAWATM and pass it to the driver.
642 if ((u_long)m == SIOCRAWATM) {
643 if (npcb->npcb_ifp == NULL) {
648 ario.rawvalue = *((int *)nam);
649 error = npcb->npcb_ifp->if_ioctl(npcb->npcb_ifp,
650 SIOCXRAWATM, (caddr_t) &ario);
653 npcb->npcb_flags |= NPCB_RAW;
655 npcb->npcb_flags &= ~(NPCB_RAW);
664 case PRU_BIND: /* bind socket to address */
665 case PRU_LISTEN: /* listen for connection */
666 case PRU_ACCEPT: /* accept connection from peer */
667 case PRU_CONNECT2: /* connect two sockets */
668 case PRU_ABORT: /* abort (fast DISCONNECT, DETATCH) */
669 /* (only happens if LISTEN socket) */
670 case PRU_RCVD: /* have taken data; more room now */
671 case PRU_FASTTIMO: /* 200ms timeout */
672 case PRU_SLOWTIMO: /* 500ms timeout */
673 case PRU_RCVOOB: /* retrieve out of band data */
674 case PRU_SENDOOB: /* send out of band data */
675 case PRU_PROTORCV: /* receive from below */
676 case PRU_PROTOSEND: /* send to below */
677 case PRU_SOCKADDR: /* fetch socket's address */
679 printf("natm: PRU #%d unsupported\n", req);
684 default: panic("natm usrreq");
692 #endif /* !FREEBSD_USRREQS */
695 * natm0_sysctl: not used, but here in case we want to add something
699 int natm0_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
700 void *newp, size_t newlen)
702 /* All sysctl names at this level are terminal. */
705 return (ENOPROTOOPT);
709 * natm5_sysctl: not used, but here in case we want to add something
713 int natm5_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
714 void *newp, size_t newlen)
716 /* All sysctl names at this level are terminal. */
719 return (ENOPROTOOPT);
722 static void natmintr(struct netmsg *);
724 #if defined(__DragonFly__)
726 netisr_natm_setup(void *dummy __unused)
729 netisr_register(NETISR_NATM, cpu0_portfn, natmintr);
731 SYSINIT(natm_setup, SI_SUB_CPU, SI_ORDER_ANY, netisr_natm_setup, NULL);
737 LIST_INIT(&natm_pcbs);
739 netisr_register(NETISR_NATM, cpu0_portfn, natmintr);
743 * natmintr: splsoftnet interrupt
745 * note: we expect a socket pointer in rcvif rather than an interface
746 * pointer. we can get the interface pointer from the so's PCB if
750 natmintr(struct netmsg *msg)
752 struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
755 struct natmpcb *npcb;
758 if ((m->m_flags & M_PKTHDR) == 0)
759 panic("natmintr no HDR");
762 npcb = (struct natmpcb *) m->m_pkthdr.rcvif; /* XXX: overloaded */
763 so = npcb->npcb_socket;
765 s = splimp(); /* could have atm devs @ different levels */
769 if (npcb->npcb_flags & NPCB_DRAIN) {
771 if (npcb->npcb_inq == 0)
772 FREE(npcb, M_PCB); /* done! */
776 if (npcb->npcb_flags & NPCB_FREE) {
777 m_freem(m); /* drop */
781 #ifdef NEED_TO_RESTORE_IFP
782 m->m_pkthdr.rcvif = npcb->npcb_ifp;
785 m->m_pkthdr.rcvif = NULL; /* null it out to be safe */
789 if (sbspace(&so->so_rcv) > m->m_pkthdr.len ||
790 ((npcb->npcb_flags & NPCB_RAW) != 0 && so->so_rcv.sb_cc < NPCB_RAWCC) ) {
793 natm_sookbytes += m->m_pkthdr.len;
795 sbappendrecord(&so->so_rcv, m);
800 natm_sodropbytes += m->m_pkthdr.len;