summaryrefslogtreecommitdiff
path: root/sys
diff options
context:
space:
mode:
authorchuck <chuck@cvs.openbsd.org>1996-06-30 21:40:15 +0000
committerchuck <chuck@cvs.openbsd.org>1996-06-30 21:40:15 +0000
commit0bd8979f0d2784bc7077fd1945cc90274a74526e (patch)
treea698185538f06dedc3b081f3d9c2580f2764fbb7 /sys
parent80560ff2896efb39dd8b04dd6c1557d69f5f8b04 (diff)
new: protocol layer to provide you with native mode ATM access.
you can open raw aal5 and aal0 vcs with this protocol layer.
Diffstat (limited to 'sys')
-rw-r--r--sys/netnatm/natm.c439
-rw-r--r--sys/netnatm/natm.h133
-rw-r--r--sys/netnatm/natm_pcb.c190
-rw-r--r--sys/netnatm/natm_proto.c92
4 files changed, 854 insertions, 0 deletions
diff --git a/sys/netnatm/natm.c b/sys/netnatm/natm.c
new file mode 100644
index 00000000000..51da47eb433
--- /dev/null
+++ b/sys/netnatm/natm.c
@@ -0,0 +1,439 @@
+/* $OpenBSD: natm.c,v 1.1 1996/06/30 21:40:12 chuck Exp $ */
+
+/*
+ *
+ * Copyright (c) 1996 Charles D. Cranor and Washington University.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by Charles D. Cranor and
+ * Washington University.
+ * 4. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * natm.c: native mode ATM access (both aal0 and aal5).
+ */
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/domain.h>
+#include <sys/ioctl.h>
+#include <sys/proc.h>
+#include <sys/protosw.h>
+#include <sys/mbuf.h>
+#include <sys/socket.h>
+#include <sys/socketvar.h>
+
+#include <net/if.h>
+#include <net/if_atm.h>
+#include <net/radix.h>
+#include <net/route.h>
+
+#include <netinet/in.h>
+
+#include <netnatm/natm.h>
+
+u_long natm5_sendspace = 16*1024;
+u_long natm5_recvspace = 16*1024;
+
+u_long natm0_sendspace = 16*1024;
+u_long natm0_recvspace = 16*1024;
+
+/*
+ * user requests
+ */
+
+int natm_usrreq(so, req, m, nam, control, p)
+
+struct socket *so;
+int req;
+struct mbuf *m, *nam, *control;
+struct proc *p;
+
+{
+ int error = 0, s, s2;
+ struct natmpcb *npcb;
+ struct sockaddr_natm *snatm;
+ struct atm_pseudoioctl api;
+ struct atm_pseudohdr *aph;
+ struct atm_rawioctl ario;
+ struct ifnet *ifp;
+ int proto = so->so_proto->pr_protocol;
+
+ s = splsoftnet();
+
+ npcb = (struct natmpcb *) so->so_pcb;
+
+ if (npcb == NULL && req != PRU_ATTACH) {
+ error = EINVAL;
+ goto done;
+ }
+
+
+ switch (req) {
+ case PRU_ATTACH: /* attach protocol to up */
+
+ if (npcb) {
+ error = EISCONN;
+ break;
+ }
+
+ if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
+ if (proto == PROTO_NATMAAL5)
+ error = soreserve(so, natm5_sendspace, natm5_recvspace);
+ else
+ error = soreserve(so, natm0_sendspace, natm0_recvspace);
+ if (error)
+ break;
+ }
+
+ so->so_pcb = npcb = npcb_alloc(M_WAITOK);
+ npcb->npcb_socket = so;
+
+ break;
+
+ case PRU_DETACH: /* detach protocol from up */
+
+ /*
+ * we turn on 'drain' *before* we sofree.
+ */
+
+ npcb_free(npcb, NPCB_DESTROY); /* drain */
+ so->so_pcb = NULL;
+ sofree(so);
+
+ break;
+
+ case PRU_CONNECT: /* establish connection to peer */
+
+ /*
+ * validate nam and npcb
+ */
+
+ if (nam->m_len != sizeof(*snatm)) {
+ error = EINVAL;
+ break;
+ }
+ snatm = mtod(nam, struct sockaddr_natm *);
+ if (snatm->snatm_len != sizeof(*snatm) ||
+ (npcb->npcb_flags & NPCB_FREE) == 0) {
+ error = EINVAL;
+ break;
+ }
+ if (snatm->snatm_family != AF_NATM) {
+ error = EAFNOSUPPORT;
+ break;
+ }
+
+ snatm->snatm_if[IFNAMSIZ-1] = '\0'; /* XXX ensure null termination
+ since ifunit() uses strcmp */
+
+ /*
+ * convert interface string to ifp, validate.
+ */
+
+ ifp = ifunit(snatm->snatm_if);
+ if (ifp == NULL || (ifp->if_flags & IFF_RUNNING) == 0) {
+ error = ENXIO;
+ break;
+ }
+ if (ifp->if_output != atm_output) {
+ error = EAFNOSUPPORT;
+ break;
+ }
+
+
+ /*
+ * register us with the NATM PCB layer
+ */
+
+ if (npcb_add(npcb, ifp, snatm->snatm_vci, snatm->snatm_vpi) != npcb) {
+ error = EADDRINUSE;
+ break;
+ }
+
+ /*
+ * enable rx
+ */
+
+ ATM_PH_FLAGS(&api.aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0;
+ ATM_PH_VPI(&api.aph) = npcb->npcb_vpi;
+ ATM_PH_SETVCI(&api.aph, npcb->npcb_vci);
+ api.rxhand = npcb;
+ s2 = splimp();
+ if (ifp->if_ioctl == NULL ||
+ ifp->if_ioctl(ifp, SIOCATMENA, (caddr_t) &api) != 0) {
+ splx(s2);
+ npcb_free(npcb, NPCB_REMOVE);
+ error = EIO;
+ break;
+ }
+ splx(s2);
+
+ soisconnected(so);
+
+ break;
+
+ case PRU_DISCONNECT: /* disconnect from peer */
+
+ if ((npcb->npcb_flags & NPCB_CONNECTED) == 0) {
+ printf("natm: disconnected check\n");
+ error = EIO;
+ break;
+ }
+ ifp = npcb->npcb_ifp;
+
+ /*
+ * disable rx
+ */
+
+ ATM_PH_FLAGS(&api.aph) = ATM_PH_AAL5;
+ ATM_PH_VPI(&api.aph) = npcb->npcb_vpi;
+ ATM_PH_SETVCI(&api.aph, npcb->npcb_vci);
+ api.rxhand = npcb;
+ s2 = splimp();
+ if (ifp->if_ioctl != NULL)
+ ifp->if_ioctl(ifp, SIOCATMDIS, (caddr_t) &api);
+ splx(s);
+
+ npcb_free(npcb, NPCB_REMOVE);
+ soisdisconnected(so);
+
+ break;
+
+ case PRU_SHUTDOWN: /* won't send any more data */
+ socantsendmore(so);
+ break;
+
+ case PRU_SEND: /* send this data */
+ if (control && control->m_len) {
+ m_freem(control);
+ m_freem(m);
+ error = EINVAL;
+ break;
+ }
+
+ /*
+ * send the data. we must put an atm_pseudohdr on first
+ */
+
+ M_PREPEND(m, sizeof(*aph), M_WAITOK);
+ if (m == NULL) {
+ error = ENOBUFS;
+ break;
+ }
+ aph = mtod(m, struct atm_pseudohdr *);
+ ATM_PH_VPI(aph) = npcb->npcb_vpi;
+ ATM_PH_SETVCI(aph, npcb->npcb_vci);
+ ATM_PH_FLAGS(aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0;
+
+ error = atm_output(npcb->npcb_ifp, m, NULL, NULL);
+
+ break;
+
+ case PRU_SENSE: /* return status into m */
+ /* return zero? */
+ break;
+
+ case PRU_PEERADDR: /* fetch peer's address */
+ snatm = mtod(nam, struct sockaddr_natm *);
+ nam->m_len = snatm->snatm_len = sizeof(*snatm);
+ snatm->snatm_family = AF_NATM;
+ bcopy(npcb->npcb_ifp->if_xname, snatm->snatm_if, sizeof(snatm->snatm_if));
+ snatm->snatm_vci = npcb->npcb_vci;
+ snatm->snatm_vpi = npcb->npcb_vpi;
+ break;
+
+ case PRU_CONTROL: /* control operations on protocol */
+ /*
+ * raw atm ioctl. comes in as a SIOCRAWATM. we convert it to
+ * SIOCXRAWATM and pass it to the driver.
+ */
+ if ((u_long)m == SIOCRAWATM) {
+ if (npcb->npcb_ifp == NULL) {
+ error = ENOTCONN;
+ break;
+ }
+ ario.npcb = npcb;
+ ario.rawvalue = *((int *)nam);
+ error = npcb->npcb_ifp->if_ioctl(npcb->npcb_ifp,
+ SIOCXRAWATM, (caddr_t) &ario);
+ if (!error) {
+ if (ario.rawvalue)
+ npcb->npcb_flags |= NPCB_RAW;
+ else
+ npcb->npcb_flags &= ~(NPCB_RAW);
+ }
+
+ break;
+ }
+
+ error = EOPNOTSUPP;
+ break;
+
+ case PRU_BIND: /* bind socket to address */
+ case PRU_LISTEN: /* listen for connection */
+ case PRU_ACCEPT: /* accept connection from peer */
+ case PRU_CONNECT2: /* connect two sockets */
+ case PRU_ABORT: /* abort (fast DISCONNECT, DETATCH) */
+ /* (only happens if LISTEN socket) */
+ case PRU_RCVD: /* have taken data; more room now */
+ case PRU_FASTTIMO: /* 200ms timeout */
+ case PRU_SLOWTIMO: /* 500ms timeout */
+ case PRU_RCVOOB: /* retrieve out of band data */
+ case PRU_SENDOOB: /* send out of band data */
+ case PRU_PROTORCV: /* receive from below */
+ case PRU_PROTOSEND: /* send to below */
+ case PRU_SOCKADDR: /* fetch socket's address */
+#ifdef DIAGNOSTIC
+ printf("natm: PRU #%d unsupported\n", req);
+#endif
+ error = EOPNOTSUPP;
+ break;
+
+ default: panic("natm usrreq");
+ }
+
+done:
+ splx(s);
+ return(error);
+}
+
+/*
+ * natmintr: splsoftnet interrupt
+ *
+ * note: we expect a socket pointer in rcvif rather than an interface
+ * pointer. we can get the interface pointer from the so's PCB if
+ * we really need it.
+ */
+
+void
+natmintr()
+
+{
+ int s;
+ struct mbuf *m;
+ struct socket *so;
+ struct natmpcb *npcb;
+
+next:
+ s = splimp();
+ IF_DEQUEUE(&natmintrq, m);
+ splx(s);
+ if (m == NULL)
+ return;
+
+#ifdef DIAGNOSTIC
+ if ((m->m_flags & M_PKTHDR) == 0)
+ panic("ipintr no HDR");
+#endif
+
+ npcb = (struct natmpcb *) m->m_pkthdr.rcvif; /* XXX: overloaded */
+ so = npcb->npcb_socket;
+
+ s = splimp(); /* could have atm devs @ different levels */
+ npcb->npcb_inq--;
+ splx(s);
+
+ if (npcb->npcb_flags & NPCB_DRAIN) {
+ m_freem(m);
+ if (npcb->npcb_inq == 0)
+ FREE(npcb, M_PCB); /* done! */
+ goto next;
+ }
+
+ if (npcb->npcb_flags & NPCB_FREE) {
+ m_freem(m); /* drop */
+ goto next;
+ }
+
+#ifdef NEED_TO_RESTORE_IFP
+ m->m_pkthdr.rcvif = npcb->npcb_ifp;
+#else
+#ifdef DIAGNOSTIC
+m->m_pkthdr.rcvif = NULL; /* null it out to be safe */
+#endif
+#endif
+
+ if (sbspace(&so->so_rcv) > m->m_pkthdr.len ||
+ ((npcb->npcb_flags & NPCB_RAW) != 0 && so->so_rcv.sb_cc < NPCB_RAWCC) ) {
+#ifdef NATM_STAT
+ natm_sookcnt++;
+ natm_sookbytes += m->m_pkthdr.len;
+#endif
+ sbappend(&so->so_rcv, m);
+ sorwakeup(so);
+ } else {
+#ifdef NATM_STAT
+ natm_sodropcnt++;
+ natm_sodropbytes += m->m_pkthdr.len;
+#endif
+ m_freem(m);
+ }
+
+ goto next;
+}
+
+/*
+ * natm0_sysctl: not used, but here in case we want to add something
+ * later...
+ */
+
+int natm0_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
+
+int *name;
+u_int namelen;
+void *oldp;
+size_t *oldlenp;
+void *newp;
+size_t newlen;
+
+{
+ /* All sysctl names at this level are terminal. */
+ if (namelen != 1)
+ return (ENOTDIR);
+ return (ENOPROTOOPT);
+}
+
+/*
+ * natm5_sysctl: not used, but here in case we want to add something
+ * later...
+ */
+
+int natm5_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
+
+int *name;
+u_int namelen;
+void *oldp;
+size_t *oldlenp;
+void *newp;
+size_t newlen;
+
+{
+ /* All sysctl names at this level are terminal. */
+ if (namelen != 1)
+ return (ENOTDIR);
+ return (ENOPROTOOPT);
+}
diff --git a/sys/netnatm/natm.h b/sys/netnatm/natm.h
new file mode 100644
index 00000000000..143d1623cc4
--- /dev/null
+++ b/sys/netnatm/natm.h
@@ -0,0 +1,133 @@
+/* $OpenBSD: natm.h,v 1.1 1996/06/30 21:40:14 chuck Exp $ */
+
+/*
+ *
+ * Copyright (c) 1996 Charles D. Cranor and Washington University.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by Charles D. Cranor and
+ * Washington University.
+ * 4. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * natm.h: native mode atm
+ */
+
+
+/*
+ * supported protocols
+ */
+
+#define PROTO_NATMAAL0 1
+#define PROTO_NATMAAL5 2
+
+/*
+ * sockaddr_natm
+ */
+
+struct sockaddr_natm {
+ u_int8_t snatm_len; /* length */
+ u_int8_t snatm_family; /* AF_NATM */
+ char snatm_if[IFNAMSIZ]; /* interface name */
+ u_int16_t snatm_vci; /* vci */
+ u_int8_t snatm_vpi; /* vpi */
+};
+
+
+#ifdef _KERNEL
+
+/*
+ * natm protocol control block
+ */
+
+struct natmpcb {
+ LIST_ENTRY(natmpcb) pcblist; /* list pointers */
+ u_int npcb_inq; /* # of our pkts in proto q */
+ struct socket *npcb_socket; /* backpointer to socket */
+ struct ifnet *npcb_ifp; /* pointer to hardware */
+ struct in_addr ipaddr; /* remote IP address, if APCB_IP */
+ u_int16_t npcb_vci; /* VCI */
+ u_int8_t npcb_vpi; /* VPI */
+ u_int8_t npcb_flags; /* flags */
+};
+
+/* flags */
+#define NPCB_FREE 0x01 /* free (not on any list) */
+#define NPCB_CONNECTED 0x02 /* connected */
+#define NPCB_IP 0x04 /* used by IP */
+#define NPCB_DRAIN 0x08 /* destory as soon as inq == 0 */
+#define NPCB_RAW 0x10 /* in 'raw' mode? */
+
+/* flag arg to npcb_free */
+#define NPCB_REMOVE 0 /* remove from global list */
+#define NPCB_DESTROY 1 /* destroy and be free */
+
+/*
+ * NPCB_RAWCC is a hack which applies to connections in 'raw' mode. it
+ * is used to override the sbspace() macro when you *really* don't want
+ * to drop rcv data. the recv socket buffer size is raised to this value.
+ *
+ * XXX: socket buffering needs to be looked at.
+ */
+
+#define NPCB_RAWCC (1024*1024) /* 1MB */
+
+LIST_HEAD(npcblist, natmpcb);
+
+/* global data structures */
+
+struct npcblist natm_pcbs; /* global list of pcbs */
+extern struct ifqueue natmintrq; /* natm packet input queue */
+#define NATM_STAT
+#ifdef NATM_STAT
+extern u_int natm_sodropcnt,
+ natm_sodropbytes; /* account of droppage */
+extern u_int natm_sookcnt,
+ natm_sookbytes; /* account of ok */
+#endif
+
+/* atm_rawioctl: kernel's version of SIOCRAWATM [for internal use only!] */
+struct atm_rawioctl {
+ struct natmpcb *npcb;
+ int rawvalue;
+};
+#define SIOCXRAWATM _IOWR('a', 125, struct atm_rawioctl)
+
+/* external functions */
+
+/* natm_pcb.c */
+struct natmpcb *npcb_alloc __P((int));
+void npcb_free __P((struct natmpcb *, int));
+struct natmpcb *npcb_add __P((struct natmpcb *, struct ifnet *, int, int));
+
+/* natm.c */
+int natm_usrreq __P((struct socket *, int, struct mbuf *,
+ struct mbuf *, struct mbuf *, struct proc *));
+int natm0_sysctl __P((int *, u_int, void *, size_t *, void *, size_t));
+int natm5_sysctl __P((int *, u_int, void *, size_t *, void *, size_t));
+void natmintr __P((void));
+
+#endif
diff --git a/sys/netnatm/natm_pcb.c b/sys/netnatm/natm_pcb.c
new file mode 100644
index 00000000000..3be08a0c920
--- /dev/null
+++ b/sys/netnatm/natm_pcb.c
@@ -0,0 +1,190 @@
+/* $OpenBSD: natm_pcb.c,v 1.1 1996/06/30 21:40:13 chuck Exp $ */
+
+/*
+ *
+ * Copyright (c) 1996 Charles D. Cranor and Washington University.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by Charles D. Cranor and
+ * Washington University.
+ * 4. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * atm_pcb.c: manage atm protocol control blocks and keep IP and NATM
+ * from trying to use each other's VCs.
+ */
+
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/protosw.h>
+#include <sys/domain.h>
+#include <sys/mbuf.h>
+#include <sys/malloc.h>
+
+#include <net/if.h>
+#include <net/radix.h>
+#include <net/route.h>
+
+#include <netinet/in.h>
+
+#include <netnatm/natm.h>
+
+/*
+ * npcb_alloc: allocate a npcb [in the free state]
+ */
+
+struct natmpcb *npcb_alloc(wait)
+
+int wait;
+
+{
+ struct natmpcb *npcb;
+
+ MALLOC(npcb, struct natmpcb *, sizeof(*npcb), M_PCB, wait);
+
+#ifdef DIAGNOSTIC
+ if (wait == M_WAITOK && npcb == NULL) panic("npcb_alloc: malloc didn't wait");
+#endif
+
+ if (npcb) {
+ bzero(npcb, sizeof(*npcb));
+ npcb->npcb_flags = NPCB_FREE;
+ }
+ return(npcb);
+}
+
+
+/*
+ * npcb_free: free a npcb
+ */
+
+void npcb_free(npcb, op)
+
+struct natmpcb *npcb;
+int op;
+
+{
+ int s = splimp();
+
+ if ((npcb->npcb_flags & NPCB_FREE) == 0) {
+ LIST_REMOVE(npcb, pcblist);
+ npcb->npcb_flags = NPCB_FREE;
+ }
+ if (op == NPCB_DESTROY) {
+ if (npcb->npcb_inq) {
+ npcb->npcb_flags = NPCB_DRAIN; /* flag for distruction */
+ } else {
+ FREE(npcb, M_PCB); /* kill it! */
+ }
+ }
+
+ splx(s);
+}
+
+
+/*
+ * npcb_add: add or remove npcb from main list
+ * returns npcb if ok
+ */
+
+struct natmpcb *npcb_add(npcb, ifp, vci, vpi)
+
+struct natmpcb *npcb;
+struct ifnet *ifp;
+u_int16_t vci;
+u_int8_t vpi;
+
+{
+ struct natmpcb *cpcb = NULL; /* current pcb */
+ int s = splimp();
+
+
+ /*
+ * lookup required
+ */
+
+ for (cpcb = natm_pcbs.lh_first ; cpcb != NULL ;
+ cpcb = cpcb->pcblist.le_next) {
+ if (ifp == cpcb->npcb_ifp && vci == cpcb->npcb_vci && vpi == cpcb->npcb_vpi)
+ break;
+ }
+
+ /*
+ * add & something already there?
+ */
+
+ if (cpcb) {
+ cpcb = NULL;
+ goto done; /* fail */
+ }
+
+ /*
+ * need to allocate a pcb?
+ */
+
+ if (npcb == NULL) {
+ cpcb = npcb_alloc(M_NOWAIT); /* could be called from lower half */
+ if (cpcb == NULL)
+ goto done; /* fail */
+ } else {
+ cpcb = npcb;
+ }
+
+ cpcb->npcb_ifp = ifp;
+ cpcb->ipaddr.s_addr = 0;
+ cpcb->npcb_vci = vci;
+ cpcb->npcb_vpi = vpi;
+ cpcb->npcb_flags = NPCB_CONNECTED;
+
+ LIST_INSERT_HEAD(&natm_pcbs, cpcb, pcblist);
+
+done:
+ splx(s);
+ return(cpcb);
+}
+
+
+
+#ifdef DDB
+
+int npcb_dump()
+
+{
+ struct natmpcb *cpcb;
+
+ printf("npcb dump:\n");
+ for (cpcb = natm_pcbs.lh_first ; cpcb != NULL ;
+ cpcb = cpcb->pcblist.le_next) {
+ printf("if=%s, vci=%d, vpi=%d, IP=0x%x, sock=0x%x, flags=0x%x, inq=%d\n",
+ cpcb->npcb_ifp->if_xname, cpcb->npcb_vci, cpcb->npcb_vpi,
+ cpcb->ipaddr.s_addr, cpcb->npcb_socket,
+ cpcb->npcb_flags, cpcb->npcb_inq);
+ }
+ printf("done\n");
+ return(0);
+}
+
+#endif
diff --git a/sys/netnatm/natm_proto.c b/sys/netnatm/natm_proto.c
new file mode 100644
index 00000000000..33eaf6b0f97
--- /dev/null
+++ b/sys/netnatm/natm_proto.c
@@ -0,0 +1,92 @@
+/* $OpenBSD: natm_proto.c,v 1.1 1996/06/30 21:40:14 chuck Exp $ */
+
+/*
+ *
+ * Copyright (c) 1996 Charles D. Cranor and Washington University.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by Charles D. Cranor and
+ * Washington University.
+ * 4. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * protocol layer for access to native mode ATM
+ */
+
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/protosw.h>
+#include <sys/domain.h>
+#include <sys/mbuf.h>
+
+#include <net/if.h>
+#include <net/radix.h>
+#include <net/route.h>
+
+#include <netinet/in.h>
+
+#include <netnatm/natm.h>
+
+extern struct domain natmdomain;
+
+static void natm_init __P((void));
+
+struct protosw natmsw[] = {
+{ SOCK_STREAM, &natmdomain, PROTO_NATMAAL5, PR_CONNREQUIRED,
+ 0, 0, 0, 0,
+ natm_usrreq,
+ 0, 0, 0, 0, natm5_sysctl
+},
+{ SOCK_STREAM, &natmdomain, PROTO_NATMAAL0, PR_CONNREQUIRED,
+ 0, 0, 0, 0,
+ natm_usrreq,
+ 0, 0, 0, 0, natm0_sysctl
+},
+};
+
+struct domain natmdomain =
+ { AF_NATM, "natm", natm_init, 0, 0,
+ natmsw, &natmsw[sizeof(natmsw)/sizeof(natmsw[0])], 0,
+ 0, 0, 0};
+
+struct ifqueue natmintrq; /* natm packet input queue */
+int natmqmaxlen = IFQ_MAXLEN; /* max # of packets on queue */
+#ifdef NATM_STAT
+u_int natm_sodropcnt = 0; /* # mbufs dropped due to full sb */
+u_int natm_sodropbytes = 0; /* # of bytes dropped */
+u_int natm_sookcnt = 0; /* # mbufs ok */
+u_int natm_sookbytes = 0; /* # of bytes ok */
+#endif
+
+
+
+void natm_init()
+
+{
+ LIST_INIT(&natm_pcbs);
+ bzero(&natmintrq, sizeof(natmintrq));
+ natmintrq.ifq_maxlen = natmqmaxlen;
+}