--- /dev/null
+CFLAGS= -D_KERNEL -I../current
+CC= gcc
+
+all: ppp x ppp_ahdlc
+
+ppp: ppp.o
+ ld -r -o ppp ppp.o
+
+ppp_ahdlc: ppp_ahdlc.o
+ ld -r -o ppp_ahdlc ppp_ahdlc.o
+
+x: x.c
+ $(CC) -I../current -g -o x x.c
--- /dev/null
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/stream.h>
+#include <sys/stropts.h>
+#include <sys/errno.h>
+#include <sys/cmn_err.h>
+#include <sys/modctl.h>
+#include <sys/conf.h>
+#include <sys/ddi.h>
+#include <sys/sunddi.h>
+#include <sys/dlpi.h>
+#include <sys/ioccom.h>
+#include <net/ppp_defs.h>
+#include <net/pppio.h>
+
+#ifdef __STDC__
+#define __P(x) x
+#else
+#define __P(x) ()
+#endif
+
+/*
+ * The IP module uses this SAP value for IP packets.
+ */
+#ifndef ETHERTYPE_IP
+#define ETHERTYPE_IP 0x800
+#endif
+
+#ifndef PPP_MAXMTU
+#define PPP_MAXMTU 65536
+#endif
+
+/*
+ * Private information; one per upper stream.
+ */
+struct upperstr {
+ minor_t mn; /* minor device number */
+ queue_t *q; /* read q associated with this upper stream */
+ int flags; /* flag bits, see below */
+ int state; /* current DLPI state */
+ int sap; /* service access point */
+ struct upperstr *ppa; /* control stream for our ppa */
+ struct upperstr *next; /* next stream for this ppa */
+ /*
+ * There is exactly one control stream for each PPA.
+ * The following fields are only used for control streams.
+ */
+ int ppa_id;
+ queue_t *lowerq; /* write queue attached below this PPA */
+ struct upperstr *nextppa; /* next control stream */
+};
+
+/* Values for flags */
+#define US_PRIV 1 /* stream was opened by superuser */
+#define US_CONTROL 2 /* stream is a control stream */
+#define US_BLOCKED 4 /* flow ctrl has blocked lower stream */
+
+static void *upper_states;
+static struct upperstr *ppas;
+
+static int ppp_identify __P((dev_info_t *));
+static int ppp_attach __P((dev_info_t *, ddi_attach_cmd_t));
+static int ppp_detach __P((dev_info_t *, ddi_detach_cmd_t));
+static int ppp_devinfo __P((dev_info_t *, ddi_info_cmd_t, void *, void **));
+static int pppopen __P((queue_t *, dev_t *, int, int, cred_t *));
+static int pppclose __P((queue_t *, int, cred_t *));
+static int pppuwput __P((queue_t *, mblk_t *));
+static int pppursrv __P((queue_t *));
+static int pppuwsrv __P((queue_t *));
+static int ppplrput __P((queue_t *, mblk_t *));
+static int ppplrsrv __P((queue_t *));
+static int ppplwsrv __P((queue_t *));
+static void dlpi_request __P((queue_t *, mblk_t *, struct upperstr *));
+static void dlpi_error __P((queue_t *, int, int, int));
+static void dlpi_ok __P((queue_t *, int));
+static int send_data __P((mblk_t *, struct upperstr *));
+static void new_ppa __P((queue_t *, mblk_t *));
+static struct upperstr *find_dest __P((struct upperstr *, int));
+
+static struct module_info ppp_info = {
+ 0xb1a6, "ppp", 0, 512, 512, 128
+};
+
+static struct qinit pppurint = {
+ NULL, pppursrv, pppopen, pppclose, NULL, &ppp_info, NULL
+};
+
+static struct qinit pppuwint = {
+ pppuwput, pppuwsrv, NULL, NULL, NULL, &ppp_info, NULL
+};
+
+static struct qinit ppplrint = {
+ ppplrput, ppplrsrv, NULL, NULL, NULL, &ppp_info, NULL
+};
+
+static struct qinit ppplwint = {
+ NULL, ppplwsrv, NULL, NULL, NULL, &ppp_info, NULL
+};
+
+static struct streamtab pppinfo = {
+ &pppurint, &pppuwint,
+ &ppplrint, &ppplwint
+};
+
+static dev_info_t *ppp_dip;
+
+static struct cb_ops cb_ppp_ops = {
+ nulldev, nulldev, nodev, nodev, /* cb_open, ... */
+ nodev, nodev, nodev, nodev, /* cb_dump, ... */
+ nodev, nodev, nodev, nochpoll, /* cb_devmap, ... */
+ ddi_prop_op, /* cb_prop_op */
+ &pppinfo, /* cb_stream */
+ D_NEW|D_MP|D_MTQPAIR|D_MTOUTPERIM|D_MTOCEXCL /* cb_flag */
+};
+
+static struct dev_ops ppp_ops = {
+ DEVO_REV, /* devo_rev */
+ 0, /* devo_refcnt */
+ ppp_devinfo, /* devo_getinfo */
+ ppp_identify, /* devo_identify */
+ nulldev, /* devo_probe */
+ ppp_attach, /* devo_attach */
+ ppp_detach, /* devo_detach */
+ nodev, /* devo_reset */
+ &cb_ppp_ops, /* devo_cb_ops */
+ NULL /* devo_bus_ops */
+};
+
+/*
+ * Module linkage information
+ */
+
+static struct modldrv modldrv = {
+ &mod_driverops, /* says this is a pseudo driver */
+ "PPP-2.2 multiplexing driver",
+ &ppp_ops /* driver ops */
+};
+
+static struct modlinkage modlinkage = {
+ MODREV_1,
+ (void *) &modldrv,
+ NULL
+};
+
+int
+_init(void)
+{
+ int error;
+
+ error = ddi_soft_state_init(&upper_states, sizeof(struct upperstr), 4);
+ if (!error) {
+ error = mod_install(&modlinkage);
+ if (!error)
+ return 0;
+ ddi_soft_state_fini(&upper_states);
+ }
+ return error;
+}
+
+int
+_fini(void)
+{
+ int error;
+
+ error = mod_remove(&modlinkage);
+ if (error)
+ return error;
+ ddi_soft_state_fini(&upper_states);
+ return 0;
+}
+
+int
+_info(mip)
+ struct modinfo *mip;
+{
+ return mod_info(&modlinkage, mip);
+}
+
+static int
+ppp_identify(dip)
+ dev_info_t *dip;
+{
+ return strcmp(ddi_get_name(dip), "ppp") == 0? DDI_IDENTIFIED:
+ DDI_NOT_IDENTIFIED;
+}
+
+static int
+ppp_attach(dip, cmd)
+ dev_info_t *dip;
+ ddi_attach_cmd_t cmd;
+{
+
+ if (cmd != DDI_ATTACH)
+ return DDI_FAILURE;
+ if (ddi_create_minor_node(dip, "ppp", S_IFCHR, 0, DDI_PSEUDO, CLONE_DEV)
+ == DDI_FAILURE) {
+ ddi_remove_minor_node(dip, NULL);
+ return DDI_FAILURE;
+ }
+ return DDI_SUCCESS;
+}
+
+static int
+ppp_detach(dip, cmd)
+ dev_info_t *dip;
+ ddi_detach_cmd_t cmd;
+{
+ ddi_remove_minor_node(dip, NULL);
+ return DDI_SUCCESS;
+}
+
+static int
+ppp_devinfo(dip, cmd, arg, result)
+ dev_info_t *dip;
+ ddi_info_cmd_t cmd;
+ void *arg;
+ void **result;
+{
+ int error;
+
+ error = DDI_SUCCESS;
+ switch (cmd) {
+ case DDI_INFO_DEVT2DEVINFO:
+ if (ppp_dip == NULL)
+ error = DDI_FAILURE;
+ else
+ *result = (void *) ppp_dip;
+ break;
+ case DDI_INFO_DEVT2INSTANCE:
+ *result = NULL;
+ break;
+ default:
+ error = DDI_FAILURE;
+ }
+ return error;
+}
+
+static int
+pppopen(q, devp, oflag, sflag, credp)
+ queue_t *q;
+ dev_t *devp;
+ int oflag, sflag;
+ cred_t *credp;
+{
+ struct upperstr *up;
+ minor_t mn;
+
+ if (q->q_ptr)
+ return 0; /* device is already open */
+
+ if (sflag == CLONEOPEN) {
+ for (mn = 0; ddi_get_soft_state(upper_states, mn) != NULL; ++mn)
+ ;
+ } else {
+ mn = getminor(*devp);
+ }
+ cmn_err(CE_CONT, "pppopen: minor %d\n", mn);
+
+ /*
+ * Construct a new minor node.
+ */
+ if (ddi_soft_state_zalloc(upper_states, mn) != DDI_SUCCESS)
+ return ENXIO;
+ up = ddi_get_soft_state(upper_states, mn);
+ *devp = makedevice(getmajor(*devp), mn);
+ up->q = q;
+ up->mn = mn;
+ up->flags = 0;
+ if (drv_priv(credp) == 0)
+ up->flags |= US_PRIV;
+ up->state = DL_UNATTACHED;
+ up->sap = -1;
+ up->ppa = 0;
+ up->next = 0;
+ up->lowerq = 0;
+ q->q_ptr = up;
+ WR(q)->q_ptr = up;
+ noenable(WR(q));
+
+ qprocson(q);
+ return 0;
+}
+
+static int
+pppclose(q, flag, credp)
+ queue_t *q;
+ int flag;
+ cred_t *credp;
+{
+ struct upperstr *up, **upp;
+ struct upperstr *as, *asnext;
+ struct lowerstr *ls;
+
+ qprocsoff(q);
+
+ up = (struct upperstr *) q->q_ptr;
+ cmn_err(CE_CONT, "pppclose: minor %d\n", up->mn);
+ if (up->flags & US_CONTROL) {
+ /*
+ * This stream represents a PPA:
+ * For all streams attached to the PPA, clear their
+ * references to this PPA.
+ * Then remove this PPA from the list of PPAs.
+ */
+ for (as = up->next; as != 0; as = asnext) {
+ asnext = as->next;
+ as->next = 0;
+ as->ppa = 0;
+ if (as->flags & US_BLOCKED) {
+ as->flags &= ~US_BLOCKED;
+ flushq(WR(as->q), FLUSHDATA);
+ }
+ }
+ for (upp = &ppas; *upp != 0; upp = &(*upp)->nextppa)
+ if (*upp == up) {
+ *upp = up->nextppa;
+ break;
+ }
+
+ } else {
+ /*
+ * If this stream is attached to a PPA,
+ * remove it from the PPA's list.
+ */
+ if ((as = up->ppa) != 0) {
+ for (; as->next != 0; as = as->next)
+ if (as->next == up) {
+ as->next = up->next;
+ break;
+ }
+ }
+ }
+
+ q->q_ptr = NULL;
+ WR(q)->q_ptr = NULL;
+ ddi_soft_state_free(upper_states, up->mn);
+
+ return 0;
+}
+
+/*
+ * A message from on high. We do one of three things:
+ * - qreply()
+ * - put the message on the lower write stream
+ * - queue it for our service routine
+ */
+static int
+pppuwput(q, mp)
+ queue_t *q;
+ mblk_t *mp;
+{
+ struct upperstr *us, *usnext;
+ struct iocblk *iop;
+ struct linkblk *lb;
+ queue_t *lq;
+ int error;
+ mblk_t *mq;
+
+ us = (struct upperstr *) q->q_ptr;
+ switch (mp->b_datap->db_type) {
+ case M_PCPROTO:
+ case M_PROTO:
+ dlpi_request(q, mp, us);
+ break;
+
+ case M_DATA:
+ if (!send_data(mp, us))
+ putq(q, mp);
+ break;
+
+ case M_IOCTL:
+ iop = (struct iocblk *) mp->b_rptr;
+ cmn_err(CE_CONT, "ppp%d ioctl %x count=%d\n", us->mn, iop->ioc_cmd,
+ iop->ioc_count);
+ switch (iop->ioc_cmd) {
+ case I_LINK:
+ if ((us->flags & US_CONTROL) == 0 || us->lowerq != 0)
+ goto iocnak;
+ lb = (struct linkblk *) mp->b_cont->b_rptr;
+ us->lowerq = lq = lb->l_qbot;
+ lq->q_ptr = us;
+ WR(lq)->q_ptr = us;
+ iop->ioc_count = 0;
+ mp->b_datap->db_type = M_IOCACK;
+ qreply(q, mp);
+ /* Unblock upper streams which now feed this lower stream. */
+ qenable(lq);
+ break;
+
+ case I_UNLINK:
+ lb = (struct linkblk *) mp->b_cont->b_rptr;
+ if (us->lowerq != lb->l_qbot)
+ cmn_err(CE_CONT, "ppp unlink: lowerq=%x qbot=%x\n",
+ us->lowerq, lb->l_qbot);
+ us->lowerq = 0;
+ iop->ioc_count = 0;
+ mp->b_datap->db_type = M_IOCACK;
+ qreply(q, mp);
+ /* Unblock streams which now feed back up the control stream. */
+ qenable(us->q);
+ break;
+
+ case PPPIO_NEWPPA:
+ if (us->flags & US_CONTROL)
+ goto iocnak;
+ if ((us->flags & US_PRIV) == 0) {
+ iop->ioc_error = EPERM;
+ goto iocnak;
+ }
+ /* Arrange to return an int */
+ if ((mq = mp->b_cont) == 0
+ || mq->b_datap->db_lim - mq->b_rptr < sizeof(int)) {
+ mq = allocb(sizeof(int), BPRI_HI);
+ if (mq == 0) {
+ iop->ioc_error = ENOSR;
+ goto iocnak;
+ }
+ if (mp->b_cont != 0)
+ freemsg(mp->b_cont);
+ mp->b_cont = mq;
+ mq->b_cont = 0;
+ }
+ iop->ioc_count = sizeof(int);
+ mq->b_wptr = mq->b_rptr + sizeof(int);
+ qwriter(q, mp, new_ppa, PERIM_OUTER);
+ break;
+
+ default:
+ if (us->ppa == 0 || us->ppa->lowerq == 0)
+ goto iocnak;
+ switch (iop->ioc_cmd) {
+ case PPPIO_GETSTAT:
+ case PPPIO_GETCSTAT:
+ break;
+ default:
+ if ((us->flags & US_PRIV) == 0) {
+ iop->ioc_error = EPERM;
+ goto iocnak;
+ }
+ }
+ putnext(us->ppa->lowerq, mp);
+ break;
+
+ iocnak:
+ mp->b_datap->db_type = M_IOCNAK;
+ qreply(q, mp);
+ break;
+ }
+ break;
+
+ case M_FLUSH:
+ if (*mp->b_rptr & FLUSHW)
+ flushq(q, FLUSHDATA);
+ if (*mp->b_rptr & FLUSHR) {
+ *mp->b_rptr &= ~FLUSHW;
+ qreply(q, mp);
+ } else
+ freemsg(mp);
+ break;
+
+ default:
+ freemsg(mp);
+ break;
+ }
+ return 0;
+}
+
+static void
+dlpi_request(q, mp, us)
+ queue_t *q;
+ mblk_t *mp;
+ struct upperstr *us;
+{
+ union DL_primitives *d = (union DL_primitives *) mp->b_rptr;
+ int size = mp->b_wptr - mp->b_rptr;
+ mblk_t *reply, *np;
+ struct upperstr *t, *ppa;
+ int sap;
+ dl_info_ack_t *info;
+ dl_bind_ack_t *ackp;
+ dl_phys_addr_ack_t *adrp;
+
+ cmn_err(CE_CONT, "ppp%d dlpi prim %x, state=%x\n", us->mn,
+ d->dl_primitive, us->state);
+ switch (d->dl_primitive) {
+ case DL_INFO_REQ:
+ if (size < sizeof(dl_info_req_t))
+ goto badprim;
+ if ((reply = allocb(sizeof(dl_info_ack_t), BPRI_HI)) == 0)
+ break; /* should do bufcall */
+ reply->b_datap->db_type = M_PCPROTO;
+ info = (dl_info_ack_t *) reply->b_wptr;
+ reply->b_wptr += sizeof(dl_info_ack_t);
+ bzero((caddr_t) info, sizeof(dl_info_ack_t));
+ info->dl_primitive = DL_INFO_ACK;
+ info->dl_max_sdu = PPP_MAXMTU;
+ info->dl_min_sdu = 1;
+ info->dl_addr_length = sizeof(ulong);
+ info->dl_mac_type = DL_OTHER;
+ info->dl_current_state = us->state;
+ info->dl_sap_length = sizeof(ulong);
+ info->dl_service_mode = DL_CLDLS;
+ info->dl_provider_style = DL_STYLE2;
+ info->dl_version = DL_CURRENT_VERSION;
+ qreply(q, reply);
+ break;
+
+ case DL_ATTACH_REQ:
+ if (size < sizeof(dl_attach_req_t))
+ goto badprim;
+ if (us->state != DL_UNATTACHED || us->ppa != 0) {
+ dlpi_error(q, DL_ATTACH_REQ, DL_OUTSTATE, 0);
+ break;
+ }
+ for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
+ if (ppa->ppa_id == d->attach_req.dl_ppa)
+ break;
+ if (ppa == 0) {
+ dlpi_error(q, DL_ATTACH_REQ, DL_BADPPA, 0);
+ break;
+ }
+ us->ppa = ppa;
+ us->state = DL_UNBOUND;
+ for (t = ppa; t->next != 0; t = t->next)
+ ;
+ t->next = us;
+ us->next = 0;
+ dlpi_ok(q, DL_ATTACH_REQ);
+ break;
+
+ case DL_DETACH_REQ:
+ if (size < sizeof(dl_detach_req_t))
+ goto badprim;
+ if (us->state != DL_UNBOUND || us->ppa == 0) {
+ dlpi_error(q, DL_DETACH_REQ, DL_OUTSTATE, 0);
+ break;
+ }
+ for (t = us->ppa; t->next != 0; t = t->next)
+ if (t->next == us) {
+ t->next = us->next;
+ break;
+ }
+ us->next = 0;
+ us->ppa = 0;
+ us->state = DL_UNATTACHED;
+ dlpi_ok(q, DL_DETACH_REQ);
+ break;
+
+ case DL_BIND_REQ:
+ if (size < sizeof(dl_bind_req_t))
+ goto badprim;
+ if (us->state != DL_UNBOUND) {
+ dlpi_error(q, DL_BIND_REQ, DL_OUTSTATE, 0);
+ break;
+ }
+ if (d->bind_req.dl_service_mode != DL_CLDLS) {
+ dlpi_error(q, DL_BIND_REQ, DL_UNSUPPORTED, 0);
+ break;
+ }
+ /* saps must be valid PPP network protocol numbers */
+ sap = d->bind_req.dl_sap;
+ cmn_err(CE_CONT, "ppp bind %x\n", sap);
+ if (sap == ETHERTYPE_IP)
+ sap = PPP_IP;
+ if (sap < 0x21 || sap > 0x3fff
+ || (sap & 1) == 0 || (sap & 0x100) != 0) {
+ dlpi_error(q, DL_BIND_REQ, DL_BADADDR, 0);
+ break;
+ }
+ us->sap = sap;
+ us->state = DL_IDLE;
+ if ((reply = allocb(sizeof(dl_bind_ack_t) + sizeof(ulong),
+ BPRI_HI)) == 0)
+ break; /* should do bufcall */
+ ackp = (dl_bind_ack_t *) reply->b_wptr;
+ reply->b_wptr += sizeof(dl_bind_ack_t) + sizeof(ulong);
+ reply->b_datap->db_type = M_PCPROTO;
+ bzero((caddr_t) ackp, sizeof(dl_bind_ack_t));
+ ackp->dl_primitive = DL_BIND_ACK;
+ ackp->dl_sap = sap;
+ ackp->dl_addr_length = sizeof(ulong);
+ ackp->dl_addr_offset = sizeof(dl_bind_ack_t);
+ *(ulong *)(ackp+1) = sap;
+ qreply(q, reply);
+ break;
+
+ case DL_UNBIND_REQ:
+ if (size < sizeof(dl_unbind_req_t))
+ goto badprim;
+ if (us->state != DL_IDLE) {
+ dlpi_error(q, DL_UNBIND_REQ, DL_OUTSTATE, 0);
+ break;
+ }
+ us->sap = -1;
+ us->state = DL_UNBOUND;
+ dlpi_ok(q, DL_UNBIND_REQ);
+ break;
+
+ case DL_UNITDATA_REQ:
+ if (size < sizeof(dl_unitdata_req_t))
+ goto badprim;
+ if (us->state != DL_IDLE) {
+ dlpi_error(q, DL_UNITDATA_REQ, DL_OUTSTATE, 0);
+ break;
+ }
+ /* this assumes PPP_HDRLEN <= sizeof(dl_unitdata_req_t) */
+ if (mp->b_datap->db_ref > 1) {
+ np = allocb(PPP_HDRLEN, BPRI_HI);
+ if (np == 0)
+ break; /* gak! */
+ np->b_cont = mp->b_cont;
+ mp->b_cont = 0;
+ freeb(mp);
+ mp = np;
+ }
+ /* XXX should use dl_dest_addr_offset/length here */
+ mp->b_wptr = mp->b_rptr + PPP_HDRLEN;
+ mp->b_rptr[0] = PPP_ALLSTATIONS;
+ mp->b_rptr[1] = PPP_UI;
+ mp->b_rptr[2] = us->sap >> 8;
+ mp->b_rptr[3] = us->sap;
+ if (!send_data(mp, us))
+ putq(q, mp);
+ return;
+
+ case DL_SUBS_BIND_REQ:
+ case DL_SUBS_UNBIND_REQ:
+ case DL_ENABMULTI_REQ:
+ case DL_DISABMULTI_REQ:
+ case DL_PROMISCON_REQ:
+ case DL_PROMISCOFF_REQ:
+ case DL_PHYS_ADDR_REQ:
+ case DL_SET_PHYS_ADDR_REQ:
+ case DL_XID_REQ:
+ case DL_TEST_REQ:
+ case DL_CONNECT_REQ:
+ case DL_TOKEN_REQ:
+ case DL_REPLY_UPDATE_REQ:
+ case DL_GET_STATISTICS_REQ:
+ case DL_REPLY_REQ:
+ case DL_DATA_ACK_REQ:
+ dlpi_error(q, d->dl_primitive, DL_NOTSUPPORTED, 0);
+ break;
+
+ case DL_CONNECT_RES:
+ case DL_DISCONNECT_REQ:
+ case DL_RESET_REQ:
+ case DL_RESET_RES:
+ dlpi_error(q, d->dl_primitive, DL_OUTSTATE, 0);
+ break;
+
+ case DL_UDQOS_REQ:
+ dlpi_error(q, d->dl_primitive, DL_BADQOSTYPE, 0);
+ break;
+
+ case DL_TEST_RES:
+ case DL_XID_RES:
+ break;
+
+ default:
+ badprim:
+ dlpi_error(q, d->dl_primitive, DL_BADPRIM, 0);
+ break;
+ }
+ freemsg(mp);
+}
+
+static void
+dlpi_error(q, prim, err, uerr)
+ queue_t *q;
+ int prim, err, uerr;
+{
+ mblk_t *reply;
+ dl_error_ack_t *errp;
+
+ reply = allocb(sizeof(dl_error_ack_t), BPRI_HI);
+ if (reply == 0)
+ return; /* XXX should do bufcall */
+ reply->b_datap->db_type = M_PCPROTO;
+ errp = (dl_error_ack_t *) reply->b_wptr;
+ reply->b_wptr += sizeof(dl_error_ack_t);
+ errp->dl_primitive = DL_ERROR_ACK;
+ errp->dl_error_primitive = prim;
+ errp->dl_errno = err;
+ errp->dl_unix_errno = uerr;
+ qreply(q, reply);
+}
+
+static void
+dlpi_ok(q, prim)
+ queue_t *q;
+ int prim;
+{
+ mblk_t *reply;
+ dl_ok_ack_t *okp;
+
+ reply = allocb(sizeof(dl_ok_ack_t), BPRI_HI);
+ if (reply == 0)
+ return; /* XXX should do bufcall */
+ reply->b_datap->db_type = M_PCPROTO;
+ okp = (dl_ok_ack_t *) reply->b_wptr;
+ reply->b_wptr += sizeof(dl_ok_ack_t);
+ okp->dl_primitive = DL_OK_ACK;
+ okp->dl_correct_primitive = prim;
+ qreply(q, reply);
+}
+
+static int
+send_data(mp, us)
+ mblk_t *mp;
+ struct upperstr *us;
+{
+ queue_t *q;
+ struct upperstr *ppa;
+
+ if (us->flags & US_BLOCKED)
+ return 0;
+ ppa = us->ppa;
+ if (ppa == 0) {
+ freemsg(mp);
+ return 1;
+ }
+ if ((q = ppa->lowerq) == 0) {
+ /* try to send it up the control stream */
+ q = ppa->q;
+ }
+ if (canputnext(q)) {
+ putnext(q, mp);
+ return 1;
+ }
+ us->flags |= US_BLOCKED;
+ return 0;
+}
+
+static void
+new_ppa(q, mp)
+ queue_t *q;
+ mblk_t *mp;
+{
+ struct upperstr *us, **usp;
+ int ppa_id;
+
+ /*
+ * Allocate a new PPA id and link this stream into
+ * the list of PPAs.
+ */
+ usp = &ppas;
+ ppa_id = 0;
+ while ((us = *usp) != 0 && ppa_id == us->ppa_id) {
+ ++ppa_id;
+ usp = &us->nextppa;
+ }
+ us = (struct upperstr *) q->q_ptr;
+ us->ppa_id = ppa_id;
+ us->ppa = us;
+ us->next = 0;
+ us->nextppa = *usp;
+ *usp = us;
+ us->flags |= US_CONTROL;
+
+ *(int *)mp->b_cont->b_rptr = ppa_id;
+ mp->b_datap->db_type = M_IOCACK;
+ qreply(q, mp);
+}
+
+static int
+pppuwsrv(q)
+ queue_t *q;
+{
+ struct upperstr *us;
+ struct lowerstr *ls;
+ queue_t *lwq;
+ mblk_t *mp;
+
+ us = (struct upperstr *) q->q_ptr;
+ while ((mp = getq(q)) != 0) {
+ if (!send_data(mp, us)) {
+ putbq(q, mp);
+ break;
+ }
+ }
+ return 0;
+}
+
+static int
+ppplwsrv(q)
+ queue_t *q;
+{
+ struct upperstr *us;
+
+ /*
+ * Flow control has back-enabled this stream:
+ * enable the write service procedures of all upper
+ * streams feeding this lower stream.
+ */
+ for (us = (struct upperstr *) q->q_ptr; us != NULL; us = us->next)
+ if (us->flags & US_BLOCKED)
+ qenable(WR(us->q));
+ return 0;
+}
+
+static int
+pppursrv(q)
+ queue_t *q;
+{
+ struct upperstr *us, *as;
+ mblk_t *mp, *hdr;
+ dl_unitdata_ind_t *ud;
+ int proto;
+
+ /*
+ * If this is a control stream and we don't have a lower queue attached,
+ * run the write service routines of other streams attached to this PPA.
+ */
+ us = (struct upperstr *) q->q_ptr;
+ if (us->flags & US_CONTROL) {
+ /*
+ * A control stream.
+ * If there is no lower queue attached, run the write service
+ * routines of other upper streams attached to this PPA.
+ */
+ if (us->lowerq == 0) {
+ as = us;
+ do {
+ if (as->flags & US_BLOCKED)
+ qenable(WR(as->q));
+ as = as->next;
+ } while (as != 0);
+ }
+ } else {
+ /*
+ * A network protocol stream. Put a DLPI header on each
+ * packet and send it on.
+ */
+ while ((mp = getq(q)) != 0) {
+ if (!canputnext(q)) {
+ putbq(q, mp);
+ break;
+ }
+ proto = PPP_PROTOCOL(mp->b_rptr);
+ mp->b_rptr += PPP_HDRLEN;
+ hdr = allocb(sizeof(dl_unitdata_ind_t) + 2 * sizeof(ulong),
+ BPRI_MED);
+ if (hdr == 0) {
+ /* XXX should put it back and use bufcall */
+ freemsg(mp);
+ continue;
+ }
+ ud = (dl_unitdata_ind_t *) hdr->b_wptr;
+ hdr->b_wptr += sizeof(dl_unitdata_ind_t) + 2 * sizeof(ulong);
+ hdr->b_cont = mp;
+ ud->dl_primitive = DL_UNITDATA_IND;
+ ud->dl_dest_addr_length = sizeof(ulong);
+ ud->dl_dest_addr_offset = sizeof(dl_unitdata_ind_t);
+ ud->dl_src_addr_length = sizeof(ulong);
+ ud->dl_src_addr_offset = ud->dl_dest_addr_offset + sizeof(ulong);
+ ud->dl_group_address = 0;
+ ((ulong *)(ud + 1))[0] = proto; /* dest SAP */
+ ((ulong *)(ud + 1))[1] = proto; /* src SAP */
+ putnext(q, mp);
+ }
+ }
+ return 0;
+}
+
+static struct upperstr *
+find_dest(ppa, proto)
+ struct upperstr *ppa;
+ int proto;
+{
+ struct upperstr *us;
+
+ for (us = ppa->next; us != 0; us = us->next)
+ if (proto == us->sap)
+ return us;
+ return 0;
+}
+
+static int
+ppplrput(q, mp)
+ queue_t *q;
+ mblk_t *mp;
+{
+ struct upperstr *ppa, *us;
+ queue_t *uq;
+ int proto;
+
+ ppa = (struct upperstr *) q->q_ptr;
+ switch (mp->b_datap->db_type) {
+ case M_FLUSH:
+ if (*mp->b_rptr & FLUSHW) {
+ *mp->b_rptr &= ~FLUSHR;
+ qreply(q, mp);
+ } else
+ freemsg(mp);
+ break;
+
+ default:
+ if (mp->b_datap->db_type == M_DATA
+ && (proto = PPP_PROTOCOL(mp->b_rptr)) < 0x8000
+ && (us = find_dest(ppa, proto)) != 0) {
+ /*
+ * A data packet for some network protocol.
+ * Queue it on the upper stream for that protocol.
+ */
+ if (canput(us->q))
+ putq(us->q, mp);
+ else
+ putq(q, mp);
+ break;
+ } else {
+ /*
+ * A control frame, a frame for an unknown protocol,
+ * or some other message type.
+ * Send it up to pppd via the control stream.
+ */
+ if (mp->b_datap->db_type >= QPCTL || canputnext(ppa->q))
+ putnext(ppa->q, mp);
+ else
+ putq(q, mp);
+ }
+ break;
+ }
+
+ return 0;
+}
+
+static int
+ppplrsrv(q)
+ queue_t *q;
+{
+ mblk_t *mp;
+ struct upperstr *ppa, *us;
+ int proto;
+
+ /*
+ * Packets only get queued here for flow control reasons.
+ */
+ ppa = (struct upperstr *) q->q_ptr;
+ while ((mp = getq(q)) != 0) {
+ if (mp->b_datap->db_type == M_DATA
+ && (proto = PPP_PROTOCOL(mp->b_rptr)) < 0x8000
+ && (us = find_dest(ppa, proto)) != 0) {
+ if (canput(us->q))
+ putq(us->q, mp);
+ else {
+ putbq(q, mp);
+ break;
+ }
+ } else {
+ if (canputnext(ppa->q))
+ putnext(ppa->q, mp);
+ else {
+ putbq(q, mp);
+ break;
+ }
+ }
+ }
+ return 0;
+}
--- /dev/null
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/stream.h>
+#include <sys/modctl.h>
+#include <sys/conf.h>
+#include <sys/kmem.h>
+#include <sys/ddi.h>
+#include <sys/sunddi.h>
+#include <net/ppp_defs.h>
+#include <net/pppio.h>
+
+#define IFRAME_BSIZE 512 /* Block size to allocate for input */
+#define OFRAME_BSIZE 4096 /* Don't allocb more than this for output */
+
+static int ahdlc_open __P((queue_t *, dev_t *, int, int, cred_t *));
+static int ahdlc_close __P((queue_t *, int, cred_t *));
+static int ahdlc_wput __P((queue_t *, mblk_t *));
+static int ahdlc_rput __P((queue_t *, mblk_t *));
+static void stuff_frame __P((queue_t *, mblk_t *));
+static void unstuff_chars __P((queue_t *, mblk_t *));
+
+static struct module_info minfo = {
+ 0x7d23, "ppp_ahdl", 0, INFPSZ, 4096, 128
+};
+
+static struct qinit rinit = {
+ ahdlc_rput, NULL, ahdlc_open, ahdlc_close, NULL, &minfo, NULL
+};
+
+static struct qinit winit = {
+ ahdlc_wput, NULL, NULL, NULL, NULL, &minfo, NULL
+};
+
+static struct streamtab ahdlc_info = {
+ &rinit, &winit, NULL, NULL
+};
+
+static struct fmodsw fsw = {
+ "ppp_ahdl",
+ &ahdlc_info,
+ D_NEW | D_MP | D_MTQPAIR
+};
+
+extern struct mod_ops mod_strmodops;
+
+static struct modlstrmod modlstrmod = {
+ &mod_strmodops,
+ "PPP async HDLC module",
+ &fsw
+};
+
+static struct modlinkage modlinkage = {
+ MODREV_1,
+ (void *) &modlstrmod,
+ NULL
+};
+
+struct ahdlc_state {
+ int flags;
+ mblk_t *cur_frame;
+ mblk_t *cur_blk;
+ int inlen;
+ ushort infcs;
+ u_int32_t xaccm[8];
+ u_int32_t raccm;
+ int mtu;
+ int mru;
+};
+
+/* Values for flags */
+#define ESCAPED 1 /* last saw escape char on input */
+#define IFLUSH 2 /* flushing input due to error */
+
+/*
+ * FCS lookup table as calculated by genfcstab.
+ */
+static u_short fcstab[256] = {
+ 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
+ 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
+ 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
+ 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
+ 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
+ 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
+ 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
+ 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
+ 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
+ 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
+ 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
+ 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
+ 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
+ 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
+ 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
+ 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
+ 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
+ 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
+ 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
+ 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
+ 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
+ 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
+ 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
+ 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
+ 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
+ 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
+ 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
+ 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
+ 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
+ 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
+ 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
+ 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
+};
+
+/*
+ * Entry points for modloading.
+ */
+int
+_init(void)
+{
+ return mod_install(&modlinkage);
+}
+
+int
+_fini(void)
+{
+ return mod_remove(&modlinkage);
+}
+
+int
+_info(mip)
+ struct modinfo *mip;
+{
+ return mod_info(&modlinkage, mip);
+}
+
+/*
+ * STREAMS module entry points.
+ */
+static int
+ahdlc_open(q, devp, flag, sflag, credp)
+ queue_t *q;
+ dev_t *devp;
+ int flag, sflag;
+ cred_t *credp;
+{
+ struct ahdlc_state *sp;
+
+ if (q->q_ptr == 0) {
+ sp = (struct ahdlc_state *) kmem_zalloc(sizeof(struct ahdlc_state),
+ KM_SLEEP);
+ if (sp == 0)
+ return ENOSR;
+ q->q_ptr = sp;
+ WR(q)->q_ptr = sp;
+ sp->xaccm[0] = ~0;
+ sp->xaccm[3] = 0x60000000;
+ sp->mru = 1500;
+ qprocson(q);
+ }
+ return 0;
+}
+
+static int
+ahdlc_close(q, flag, credp)
+ queue_t *q;
+ int flag;
+ cred_t *credp;
+{
+ struct ahdlc_state *state;
+
+ qprocsoff(q);
+ if (q->q_ptr != 0) {
+ state = (struct ahdlc_state *) q->q_ptr;
+ if (state->cur_frame != 0) {
+ freemsg(state->cur_frame);
+ state->cur_frame = 0;
+ }
+ kmem_free(q->q_ptr, sizeof(struct ahdlc_state));
+ }
+ return 0;
+}
+
+static int
+ahdlc_wput(q, mp)
+ queue_t *q;
+ mblk_t *mp;
+{
+ struct ahdlc_state *state;
+ struct iocblk *iop;
+
+ state = (struct ahdlc_state *) q->q_ptr;
+ switch (mp->b_datap->db_type) {
+ case M_DATA:
+ /*
+ * A data packet - do character-stuffing and FCS, and
+ * send it onwards.
+ */
+ stuff_frame(q, mp);
+ freemsg(mp);
+ break;
+
+ case M_IOCTL:
+ iop = (struct iocblk *) mp->b_rptr;
+ switch (iop->ioc_cmd) {
+ case PPPIO_XACCM:
+ if (iop->ioc_count != sizeof(ext_accm))
+ goto iocnak;
+ bcopy(mp->b_cont->b_rptr, (caddr_t)state->xaccm, sizeof(ext_accm));
+ state->xaccm[2] &= 0x40000000; /* don't escape 0x5e */
+ state->xaccm[3] |= 0x60000000; /* do escape 0x7d, 0x7e */
+ goto iocack;
+
+ case PPPIO_RACCM:
+ if (iop->ioc_count != sizeof(u_int32_t))
+ goto iocnak;
+ state->raccm = *(u_int32_t *)mp->b_cont->b_rptr;
+ goto iocack;
+
+ default:
+ putnext(q, mp);
+ break;
+
+ iocack:
+ iop->ioc_count = 0;
+ mp->b_datap->db_type = M_IOCACK;
+ qreply(q, mp);
+ break;
+
+ iocnak:
+ mp->b_datap->db_type = M_IOCNAK;
+ qreply(q, mp);
+ break;
+ }
+
+ default:
+ putnext(q, mp);
+ }
+ return 0;
+}
+
+static int
+ahdlc_rput(q, mp)
+ queue_t *q;
+ mblk_t *mp;
+{
+ mblk_t *np;
+ uchar_t *cp;
+ struct ahdlc_state *state;
+
+ switch (mp->b_datap->db_type) {
+ case M_DATA:
+ unstuff_chars(q, mp);
+ freemsg(mp);
+ break;
+
+ case M_HANGUP:
+ state = (struct ahdlc_state *) q->q_ptr;
+ if (state->cur_frame != 0) {
+ /* XXX would like to send this up for debugging */
+ freemsg(state->cur_frame);
+ state->cur_frame = 0;
+ state->cur_blk = 0;
+ }
+ state->inlen = 0;
+ state->flags = IFLUSH;
+ putnext(q, mp);
+ break;
+
+ default:
+ putnext(q, mp);
+ }
+ return 0;
+}
+
+/* Extract bit c from map m, to determine if c needs to be escaped. */
+#define ESCAPE(c, m) ((m)[(c) >> 5] & (1 << ((c) & 0x1f)))
+
+static void
+stuff_frame(q, mp)
+ queue_t *q;
+ mblk_t *mp;
+{
+ struct ahdlc_state *state;
+ int ilen, olen, c, extra;
+ mblk_t *omsg, *np, *op;
+ uchar_t *sp, *sp0, *dp, *dp0, *spend;
+ ushort_t fcs;
+ u_int32_t *xaccm, lcp_xaccm[8];
+
+ /*
+ * We estimate the length of the output packet as
+ * 1.25 * input length + 16 (for initial flag, FCS, final flag, slop).
+ */
+ state = (struct ahdlc_state *) q->q_ptr;
+ ilen = msgdsize(mp);
+ olen = ilen + (ilen >> 2) + 16;
+ if (olen > OFRAME_BSIZE)
+ olen = OFRAME_BSIZE;
+ omsg = op = allocb(olen, BPRI_MED);
+ if (omsg == 0)
+ return;
+
+ /*
+ * Put in an initial flag for now. We'll remove it later
+ * if we decide we don't need it.
+ */
+ dp = op->b_wptr;
+ *dp++ = PPP_FLAG;
+ --olen;
+
+ /*
+ * For LCP packets, we must escape all control characters.
+ */
+ if (proto == PPP_LCP) {
+ bcopy(state->xaccm, lcp_xaccm, sizeof(lcp_xaccm));
+ lcp_xaccm[0] = ~0;
+ xaccm = lcp_xaccm;
+ } else
+ xaccm = state->xaccm;
+
+ sp = mp->b_rptr;
+ fcs = PPP_INITFCS;
+ for (;;) {
+ spend = mp->b_wptr;
+ extra = sp + olen - spend;
+ if (extra < 0) {
+ spend = sp + olen;
+ extra = 0;
+ }
+ /*
+ * We can safely process the input up to `spend'
+ * without overrunning the output, provided we don't
+ * hit more than `extra' characters which need to be escaped.
+ */
+ sp0 = sp;
+ dp0 = dp;
+ while (sp < spend) {
+ c = *sp;
+ if (ESCAPE(c, xaccm)) {
+ if (extra > 0)
+ --extra;
+ else if (sp < spend - 1)
+ --spend;
+ else
+ break;
+ fcs = PPP_FCS(fcs, c);
+ *dp++ = PPP_ESCAPE;
+ c ^= PPP_TRANS;
+ } else
+ fcs = PPP_FCS(fcs, c);
+ *dp++ = c;
+ ++sp;
+ }
+ ilen -= sp - sp0;
+ olen -= dp - dp0;
+
+ /*
+ * At this point, we have emptied an input block
+ * and/or filled an output block.
+ */
+ if (sp >= mp->b_wptr) {
+ /*
+ * We've emptied an input block. Advance to the next.
+ */
+ mp = mp->b_cont;
+ if (mp == 0)
+ break; /* all done */
+ sp = mp->b_rptr;
+ }
+ if (olen < 2) {
+ /*
+ * The output block is full. Allocate a new one.
+ */
+ op->b_wptr = dp;
+ olen = 2 * ilen + 5;
+ if (olen > OFRAME_BSIZE)
+ olen = OFRAME_BSIZE;
+ np = allocb(olen, BPRI_MED);
+ if (np == 0) {
+ freemsg(omsg);
+ return;
+ }
+ op->b_cont = np;
+ op = np;
+ dp = op->b_wptr;
+ }
+ }
+
+ /*
+ * Append the FCS and closing flag.
+ * This could require up to 5 characters.
+ */
+ if (olen < 5) {
+ /* Sigh. Need another block. */
+ op->b_wptr = dp;
+ np = allocb(5, BPRI_MED);
+ if (np == 0) {
+ freemsg(omsg);
+ return;
+ }
+ op->b_cont = np;
+ op = np;
+ dp = op->b_wptr;
+ }
+ c = ~fcs & 0xff;
+ if (ESCAPE(c, xaccm)) {
+ *dp++ = PPP_ESCAPE;
+ c ^= PPP_TRANS;
+ }
+ *dp++ = c;
+ c = (~fcs >> 8) & 0xff;
+ if (ESCAPE(c, xaccm)) {
+ *dp++ = PPP_ESCAPE;
+ c ^= PPP_TRANS;
+ }
+ *dp++ = c;
+ *dp++ = PPP_FLAG;
+ op->b_wptr = dp;
+
+ /*
+ * Remove the initial flag, if possible.
+ */
+ if (qsize(q->q_next) > 0)
+ ++omsg->b_rptr;
+
+ putnext(q, omsg);
+}
+
+static void
+unstuff_chars(q, mp)
+ queue_t *q;
+ mblk_t *mp;
+{
+ struct ahdlc_state *state;
+ mblk_t *om;
+ uchar_t *cp, *cpend, *dp, *dp0;
+ int c, len, extra, offset;
+ ushort_t fcs;
+
+ state = (struct ahdlc_state *) q->q_ptr;
+ cp = mp->b_rptr;
+ for (;;) {
+ /*
+ * Advance to next input block if necessary.
+ */
+ if (cp >= mp->b_wptr) {
+ mp = mp->b_cont;
+ if (mp == 0)
+ break;
+ cp = mp->b_rptr;
+ continue;
+ }
+
+ if ((state->flags & (IFLUSH|ESCAPED)) == 0
+ && state->inlen >= PPP_HDRLEN
+ && (om = state->cur_blk) != 0) {
+ /*
+ * Process bulk chars as quickly as possible.
+ */
+ dp = om->b_wptr;
+ len = om->b_datap->db_lim - dp; /* max # output bytes */
+ extra = (cpend - cp) - len; /* #input chars - #output bytes */
+ if (extra < 0) {
+ len += extra; /* we'll run out of input first */
+ extra = 0;
+ }
+ cpend = cp + len;
+ dp0 = dp;
+ fcs = state->infcs;
+ while (cp < cpend) {
+ c = *cp;
+ if (c == PPP_FLAG)
+ break;
+ ++cp;
+ if (c == PPP_ESCAPE) {
+ if (extra > 0)
+ --extra;
+ else if (len > 1)
+ --len;
+ else {
+ state->flags |= ESCAPED;
+ break;
+ }
+ c = *cp;
+ if (c == PPP_FLAG)
+ break;
+ ++cp;
+ c ^= PPP_TRANS;
+ }
+ *dp++ = c;
+ fcs = PPP_FCS(fcs, c);
+ }
+ state->inlen += dp - dp0;
+ state->infcs = fcs;
+ om->b_wptr = dp;
+ if (len <= 0)
+ continue; /* go back and check cp again */
+ }
+
+ c = *cp++;
+ if (c == PPP_FLAG) {
+ /*
+ * End of a frame.
+ * If the ESCAPE flag is set, the frame ended with
+ * the frame abort sequence "}~".
+ */
+ om = state->cur_frame;
+ len = state->inlen;
+ state->cur_frame = 0;
+ state->inlen = 0;
+ if (om == 0)
+ continue;
+ if (state->flags & (IFLUSH|ESCAPED) || len < PPP_FCSLEN) {
+ /* XXX should send up ctl message to notify VJ decomp */
+ freemsg(om);
+ continue;
+ }
+ if (state->infcs != PPP_GOODFCS) {
+ /* incr bad fcs stats */
+ /* would like to be able to pass this up for debugging */
+ /* XXX should send up ctl message to notify VJ decomp */
+ freemsg(om);
+ continue;
+ }
+ adjmsg(om, -PPP_FCSLEN); /* chop off fcs */
+ putnext(q, om); /* bombs away! */
+ continue;
+ }
+
+ if (state->flags & IFLUSH)
+ continue;
+ if (state->flags & ESCAPED) {
+ c ^= PPP_TRANS;
+ } else if (c == PPP_ESCAPE) {
+ state->flags |= ESCAPED;
+ continue;
+ }
+ if (state->inlen == 0) {
+ /*
+ * First byte of the frame: allocate the first message block.
+ */
+ om = allocb(IFRAME_BSIZE, BPRI_MED);
+ if (om == 0) {
+ state->flags |= IFLUSH;
+ continue;
+ }
+ state->cur_frame = om;
+ state->cur_blk = om;
+ state->infcs = PPP_INITFCS;
+ } else {
+ om = state->cur_blk;
+ if (om->b_wptr >= om->b_datap->db_lim) {
+ /*
+ * Current message block is full. Allocate another one.
+ */
+ om = allocb(IFRAME_BSIZE, BPRI_MED);
+ if (om == 0) {
+ freemsg(state->cur_frame);
+ state->flags |= IFLUSH;
+ continue;
+ }
+ state->cur_blk->b_cont = om;
+ state->cur_blk = om;
+ }
+ }
+ *om->b_wptr++ = c;
+ ++state->inlen;
+ state->infcs = PPP_FCS(state->infcs, c);
+
+ if (state->inlen == PPP_HDRLEN) {
+ /*
+ * We don't do address/control & protocol decompression here,
+ * but we do leave space for the decompressed fields and
+ * arrange for the info field to start on a word boundary.
+ */
+ cp = om->b_rptr;
+ if (*cp == PPP_ALLSTATIONS)
+ cp += 2;
+ if ((*cp & 1) == 0)
+ ++cp;
+ /* cp is now pointing at the last byte of the ppp protocol field */
+ offset = 3 - ((unsigned)cp & 3);
+ if (offset > 0) {
+ do {
+ cp[offset] = cp[0];
+ --cp;
+ } while (cp >= om->b_rptr);
+ om->b_rptr += offset;
+ om->b_wptr += offset;
+ }
+ }
+ }
+}
+
--- /dev/null
+/*
+ * ppp_comp.c - STREAMS module for kernel-level CCP support.
+ *
+ * Copyright (c) 1994 The Australian National University.
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation is hereby granted, provided that the above copyright
+ * notice appears in all copies. This software is provided without any
+ * warranty, express or implied. The Australian National University
+ * makes no representations about the suitability of this software for
+ * any purpose.
+ *
+ * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
+ * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
+ * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
+ * THE AUSTRALIAN NATIONAL UNIVERSITY HAVE BEEN ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
+ * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
+ * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
+ * OR MODIFICATIONS.
+ *
+ * $Id: ppp_comp.c,v 1.1 1995/05/10 01:38:47 paulus Exp $
+ */
+
+/*
+ * This file is used under SunOS 4.x, and OSF/1 on DEC Alpha.
+ *
+ * Beware that under OSF/1, the ioctl constants (SIOC*) end up
+ * as 64-bit (long) values, so an ioctl constant should be cast to
+ * int (32 bits) before being compared with the ioc_cmd field of
+ * an iocblk structure.
+ */
+
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/errno.h>
+#include <sys/stream.h>
+#include <sys/modctl.h>
+#include <sys/conf.h>
+#include <sys/kmem.h>
+#include <sys/ddi.h>
+#include <sys/sunddi.h>
+#include <net/ppp_defs.h>
+#include <net/ppp_str.h>
+
+#define ALLOCATE(n) kmem_zalloc((n), KM_NOSLEEP)
+#define FREE(p, n) kmem_free((p), (n))
+
+#define PACKETPTR mblk_t *
+#include <net/ppp-comp.h>
+
+static int ppp_comp_open __P((queue_t *, dev_t *, int, int, cred_t *));
+static int ppp_comp_close __P((queue_t *, int, cred_t *));
+static int ppp_comp_rput __P((queue_t *, mblk_t *));
+static int ppp_comp_wput __P((queue_t *, mblk_t *));
+static void ppp_comp_ccp __P((queue_t *, mblk_t *, int));
+
+static struct module_info minfo = {
+ 0xbadf, "ppp_compress", 0, INFPSZ, 16384, 4096,
+};
+
+static struct qinit r_init = {
+ ppp_comp_rput, NULL, ppp_comp_open, ppp_comp_close,
+ NULL, &minfo, NULL
+};
+
+static struct qinit w_init = {
+ ppp_comp_wput, NULL, NULL, NULL, NULL, &minfo, NULL
+};
+
+static struct streamtab ppp_compinfo = {
+ &r_init, &w_init, NULL, NULL
+};
+
+static struct fmodsw fsw = {
+ "ppp_comp",
+ &ppp_compinfo,
+ D_NEW | D_MP | D_MTQPAIR
+};
+
+extern struct mod_ops mod_strmodops;
+
+static struct modlstrmod modlstrmod = {
+ &mod_strmodops,
+ "PPP compression module",
+ &fsw
+};
+
+static struct modlinkage modlinkage = {
+ MODREV_1,
+ (void *) &modlstrmod,
+ NULL
+};
+
+struct ppp_comp_state {
+ int flags;
+ int mru;
+ int mtu;
+ struct compressor *xcomp;
+ void *xstate;
+ struct compressor *rcomp;
+ void *rstate;
+ struct vjcompress vj_comp;
+};
+
+/* Bits in flags are as defined in pppio.h. */
+#define CCP_ERR (CCP_ERROR | CCP_FATALERROR)
+
+#define MAX_IPHDR 128 /* max TCP/IP header size */
+#define MAX_VJHDR 20 /* max VJ compressed header size (?) */
+
+/*
+ * List of compressors we know about.
+ */
+
+extern struct compressor ppp_bsd_compress;
+
+struct compressor *ppp_compressors[] = {
+#if DO_BSD_COMPRESS
+ &ppp_bsd_compress,
+#endif
+ NULL
+};
+
+/*
+ * Entry points for modloading.
+ */
+int
+_init(void)
+{
+ return mod_install(&modlinkage);
+}
+
+int
+_fini(void)
+{
+ return mod_remove(&modlinkage);
+}
+
+int
+_info(mip)
+ struct modinfo *mip;
+{
+ return mod_info(&modlinkage, mip);
+}
+
+/*
+ * STREAMS module entry points.
+ */
+static int
+ppp_comp_open(q, dev, flag, sflag, credp)
+ queue_t *q;
+ dev_t dev;
+ int flag, sflag;
+ cred_t *credp;
+{
+ struct ppp_comp_state *cp;
+
+ if (q->q_ptr == NULL) {
+ cp = (struct ppp_comp_state *) ALLOCATE(sizeof(struct ppp_comp_state));
+ if (cp == NULL)
+ return ENOSR;
+ OTHERQ(q)->q_ptr = q->q_ptr = cp;
+ cp->flags = 0;
+ cp->mru = PPP_MRU;
+ cp->xstate = NULL;
+ cp->rstate = NULL;
+ }
+ return 0;
+}
+
+static int
+ppp_comp_close(q)
+ queue_t *q;
+{
+ struct ppp_comp_state *cp;
+
+ cp = (struct ppp_comp_state *) q->q_ptr;
+ if (cp != NULL) {
+ if (cp->xstate != NULL)
+ (*cp->xcomp->comp_free)(cp->xstate);
+ if (cp->rstate != NULL)
+ (*cp->rcomp->decomp_free)(cp->rstate);
+ FREE(cp, sizeof(struct ppp_comp_state));
+ q->q_ptr = NULL;
+ OTHERQ(q)->q_ptr = NULL;
+ }
+ return 0;
+}
+
+static int
+ppp_comp_wput(q, mp)
+ queue_t *q;
+ mblk_t *mp;
+{
+ struct iocblk *iop;
+ struct ppp_comp_state *cp;
+ mblk_t *cmp;
+ int error, len, proto, state;
+ struct ppp_option_data *odp;
+ struct compressor **comp;
+ struct ppp_comp_stats *pcp;
+
+ cp = (struct ppp_comp_state *) q->q_ptr;
+ switch (mp->b_datap->db_type) {
+
+ case M_DATA:
+ /* first find out what the protocol is */
+ if (mp->b_wptr - mp->b_rptr < PPP_HDRLEN
+ && !pullupmsg(mp, PPP_HDRLEN)) {
+ freemsg(mp); /* give up on it */
+ break;
+ }
+ proto = PPP_PROTOCOL(mp->b_rptr);
+
+ /*
+ * Do VJ compression if requested.
+ */
+ if (proto == PPP_IP && (cp->flags & COMP_VJC)) {
+ len = msgdsize(mp);
+ if (len > MAX_IPHDR + PPP_HDRLEN)
+ len = MAX_IPHDR + PPP_HDRLEN;
+ if (mp->b_wptr - mp->b_rptr >= len || pullupmsg(mp, len)) {
+ ip = (struct ip *) (mp->b_rptr + PPP_HDRLEN);
+ if (ip->ip_p == IPPROTO_TCP) {
+ type = vj_compress_tcp(ip, len - PPP_HDRLEN,
+ cp->vj_comp, (cp->flags & COMP_VJCCID),
+ &vjhdr);
+ switch (type) {
+ case TYPE_UNCOMPRESSED_TCP:
+ mp->b_rptr[3] = proto = PPP_VJC_UNCOMP;
+ break;
+ case TYPE_COMPRESSED_TCP:
+ dp = vjhdr - PPP_HDRLEN;
+ dp[1] = mp->b_rptr[1]; /* copy control field */
+ dp[0] = mp->b_rptr[0]; /* copy address field */
+ dp[2] = 0; /* set protocol field */
+ dp[3] = proto = PPP_VJC_COMP;
+ mp->b_rptr = dp;
+ break;
+ }
+ }
+ }
+ }
+
+ /*
+ * Do packet compression if enabled.
+ */
+ if (proto == PPP_CCP)
+ ppp_comp_ccp(q, mp, 0);
+ else if (proto != PPP_LCP && (cp->flags & CCP_COMP_RUN)
+ && cp->xstate != NULL) {
+ len = msgdsize(mp);
+ (*cp->xcomp->compress)(cp->xstate, &cmp, mp, len,
+ (cp->flags & CCP_ISUP? cp->mtu: 0));
+ if (cmp != NULL) {
+ freemsg(mp);
+ mp = cmp;
+ }
+ }
+
+ /*
+ * Do address/control and protocol compression if enabled.
+ */
+ if (proto != PPP_LCP && (cp->flags & COMP_AC)) {
+ mp->b_rptr += 2; /* drop the address & ctrl fields */
+ if (proto < 0x100 && (cp->flags & COMP_PROT))
+ ++mp->b_rptr; /* drop the high protocol byte */
+ } else if (proto < 0x100 && (cp->flags & COMP_PROT)) {
+ /* shuffle up the address & ctrl fields */
+ mp->b_rptr[2] = mp->b_rptr[1];
+ mp->b_rptr[1] = mp->b_rptr[0];
+ ++mp->b_rptr;
+ }
+
+ putnext(q, mp);
+ break;
+
+ case M_IOCTL:
+ iop = (struct iocblk *) mp->b_rptr;
+ error = -1;
+ switch (iop->ioc_cmd) {
+
+ case PPPIO_CFLAGS:
+ /* set CCP state */
+ if (iop->ioc_count != sizeof(int)) {
+ error = EINVAL;
+ break;
+ }
+ state = (*(int *) mp->b_cont->b_rptr) & (CCP_ISUP | CCP_ISOPEN);
+ if ((state & CCP_ISOPEN) == 0) {
+ if (cp->xstate != NULL) {
+ (*cp->xcomp->comp_free)(cp->xstate);
+ cp->xstate = NULL;
+ }
+ if (cp->rstate != NULL) {
+ (*cp->rcomp->decomp_free)(cp->rstate);
+ cp->rstate = NULL;
+ }
+ cp->flags = 0;
+ } else {
+ cp->flags = (cp->flags & ~CCP_ISUP) | state;
+ }
+ error = 0;
+ iop->ioc_count = 0;
+ break;
+
+ case SIOCGIFCOMP:
+ if ((mp->b_cont = allocb(sizeof(int), BPRI_MED)) == NULL) {
+ error = ENOSR;
+ break;
+ }
+ *(int *)mp->b_cont->b_wptr = cp->flags;
+ mp->b_cont->b_wptr += iop->ioc_count = sizeof(int);
+ break;
+
+ case PPPIO_COMPRESS:
+ error = EINVAL;
+ if (iop->ioc_count != sizeof(struct ppp_option_data))
+ break;
+ odp = (struct ppp_option_data *) mp->b_cont->b_rptr;
+ len = mp->b_cont->b_wptr - (unsigned char *) odp->opt_data;
+ if (len > odp->length)
+ len = odp->length;
+ if (odp->opt_data[1] < 2 || odp->opt_data[1] > len)
+ break;
+ for (comp = ppp_compressors; *comp != NULL; ++comp)
+ if ((*comp)->compress_proto == odp->opt_data[0]) {
+ /* here's the handler! */
+ error = 0;
+ if (odp->transmit) {
+ if (cp->xstate != NULL)
+ (*cp->xcomp->comp_free)(cp->xstate);
+ cp->xcomp = *comp;
+ cp->xstate = (*comp)->comp_alloc(odp->opt_data, len);
+ if (cp->xstate == NULL)
+ error = ENOSR;
+ } else {
+ if (cp->rstate != NULL)
+ (*cp->rcomp->decomp_free)(cp->rstate);
+ cp->rcomp = *comp;
+ cp->rstate = (*comp)->decomp_alloc(odp->opt_data, len);
+ if (cp->rstate == NULL)
+ error = ENOSR;
+ }
+ break;
+ }
+ iop->ioc_count = 0;
+ break;
+
+ case PPPIO_MRU:
+ /* remember this value */
+ if (iop->ioc_count == sizeof(int)) {
+ cp->mru = *(int *) mp->b_cont->b_rptr;
+ }
+ break;
+
+ }
+
+ if (error < 0)
+ putnext(q, mp);
+ else if (error == 0) {
+ mp->b_datap->db_type = M_IOCACK;
+ qreply(q, mp);
+ } else {
+ mp->b_datap->db_type = M_IOCNAK;
+ iop->ioc_count = 0;
+ qreply(q, mp);
+ }
+ break;
+
+ default:
+ putnext(q, mp);
+ }
+}
+
+static int
+ppp_comp_rput(q, mp)
+ queue_t *q;
+ mblk_t *mp;
+{
+ int proto, rv;
+ mblk_t *dmp;
+ struct ppp_comp_state *cp;
+
+ cp = (struct ppp_comp_state *) q->q_ptr;
+ switch (mp->b_datap->db_type) {
+
+ case M_DATA:
+ /*
+ * First do address/control and protocol "decompression".
+ */
+ len = msgdsize(mp);
+ if (len > PPP_HDRLEN)
+ len = PPP_HDRLEN;
+ if (mp->b_wptr - mp->b_rptr < len && !pullupmsg(mp, len)) {
+ /* XXX reset VJ */
+ freemsg(mp);
+ break;
+ }
+ dp = mp->b_rptr;
+ if (PPP_ADDRESS(dp) == PPP_ALLSTATIONS && PPP_CONTROL(dp) == PPP_UI)
+ dp += 2; /* skip address/control */
+ proto = 0;
+ if ((dp[0] & 1) == 0)
+ proto = *dp++ << 8; /* grab high byte of protocol */
+ proto += *dp++; /* grab low byte of protocol */
+ if (dp > mp->b_wptr) {
+ freemsg(mp); /* short/bogus packet */
+ break;
+ }
+ if ((dp -= PPP_HDRLEN) < mp->b_datap->db_base) {
+ /* yucko, need a new message block */
+ mp->b_rptr = dp;
+ np = allocb(PPP_HDRLEN, BPRI_MED);
+ if (np == 0) {
+ freemsg(mp);
+ break;
+ }
+ linkb(np, mp);
+ mp = np;
+ dp = mp->b_rptr;
+ mp->b_wptr = dp + PPP_HDRLEN;
+ } else
+ mp->b_rptr = dp;
+ dp[0] = PPP_ALLSTATIONS;
+ dp[1] = PPP_UI;
+ dp[2] = proto >> 8;
+ dp[3] = proto;
+
+ /*
+ * Now see if we have a compressed packet to decompress,
+ * or a CCP packet to take notice of.
+ */
+ proto = PPP_PROTOCOL(mp->b_rptr);
+ if (proto == PPP_CCP)
+ ppp_comp_ccp(q, mp, 1);
+ else if (proto == PPP_COMP) {
+ if ((cp->flags & CCP_ISUP)
+ && (cp->flags & CCP_DECOMP_RUN) && cp->rstate
+ && (cp->flags & CCP_ERR) == 0) {
+ rv = (*cp->rcomp->decompress)(cp->rstate, mp, &dmp);
+ if (dmp != NULL) {
+ freemsg(mp);
+ mp = dmp;
+ } else {
+ switch (rv) {
+ case DECOMP_OK:
+ /* no error, but no packet returned */
+ freemsg(mp);
+ mp = NULL;
+ break;
+ case DECOMP_ERROR:
+ cp->flags |= CCP_ERROR;
+ break;
+ case DECOMP_FATALERROR:
+ cp->flags |= CCP_FATALERROR;
+ break;
+ }
+ }
+ }
+ } else if (cp->rstate && (cp->flags & CCP_DECOMP_RUN)) {
+ (*cp->rcomp->incomp)(cp->rstate, mp);
+ }
+
+ /*
+ * Now do VJ decompression.
+ */
+
+ if (mp != NULL)
+ putnext(q, mp);
+ break;
+
+ default:
+ putnext(q, mp);
+ }
+}
+
+/*
+ * Handle a CCP packet being sent or received.
+ */
+static void
+ppp_comp_ccp(q, mp, rcvd)
+ queue_t *q;
+ mblk_t *mp;
+ int rcvd;
+{
+ int len, clen;
+ struct ppp_comp_state *cp;
+ unsigned char *dp;
+
+ len = msgdsize(mp);
+ if (len < PPP_HDRLEN + CCP_HDRLEN || !pullupmsg(mp, len))
+ return;
+ cp = (struct ppp_comp_state *) q->q_ptr;
+ dp = mp->b_rptr + PPP_HDRLEN;
+ len -= PPP_HDRLEN;
+ clen = CCP_LENGTH(dp);
+ if (clen > len)
+ return;
+
+ switch (CCP_CODE(dp)) {
+ case CCP_CONFREQ:
+ case CCP_TERMREQ:
+ case CCP_TERMACK:
+ cp->flags &= ~CCP_ISUP;
+ break;
+
+ case CCP_CONFACK:
+ if ((cp->flags & (CCP_ISOPEN | CCP_ISUP)) == CCP_ISOPEN
+ && clen >= CCP_HDRLEN + CCP_OPT_MINLEN
+ && clen >= CCP_HDRLEN + CCP_OPT_LENGTH(dp + CCP_HDRLEN)) {
+ if (!rcvd) {
+ if (cp->xstate != NULL
+ && (*cp->xcomp->comp_init)
+ (cp->xstate, dp + CCP_HDRLEN, clen - CCP_HDRLEN,
+ 0, /* XXX: should be unit */ 0, 0))
+ cp->flags |= CCP_COMP_RUN;
+ } else {
+ if (cp->rstate != NULL
+ && (*cp->rcomp->decomp_init)
+ (cp->rstate, dp + CCP_HDRLEN, clen - CCP_HDRLEN,
+ 0/* unit */, 0, cp->mru, 0))
+ cp->flags = (cp->flags & ~CCP_ERR)
+ | CCP_DECOMP_RUN;
+ }
+ }
+ break;
+
+ case CCP_RESETACK:
+ if (cp->flags & CCP_ISUP) {
+ if (!rcvd) {
+ if (cp->xstate && (cp->flags & CCP_COMP_RUN))
+ (*cp->xcomp->comp_reset)(cp->xstate);
+ } else {
+ if (cp->rstate && (cp->flags & CCP_DECOMP_RUN)) {
+ (*cp->rcomp->decomp_reset)(cp->rstate);
+ cp->flags &= ~CCP_ERROR;
+ }
+ }
+ }
+ break;
+ }
+
+}