2 * ppp.c - STREAMS multiplexing pseudo-device driver for PPP.
4 * Copyright (c) 1994 The Australian National University.
7 * Permission to use, copy, modify, and distribute this software and its
8 * documentation is hereby granted, provided that the above copyright
9 * notice appears in all copies. This software is provided without any
10 * warranty, express or implied. The Australian National University
11 * makes no representations about the suitability of this software for
14 * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
15 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
17 * THE AUSTRALIAN NATIONAL UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY
20 * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
21 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
23 * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
24 * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
27 * $Id: ppp.c,v 1.4 1996/04/04 02:45:29 paulus Exp $
31 * This file is used under Solaris 2, SVR4, and SunOS 4.
34 #include <sys/types.h>
35 #include <sys/param.h>
37 #include <sys/stream.h>
38 #include <sys/stropts.h>
39 #include <sys/errno.h>
40 #include <sys/ioccom.h>
43 #include <sys/cmn_err.h>
48 #include <sys/kstat.h>
49 #include <sys/sunddi.h>
51 #include <sys/socket.h>
52 #include <sys/sockio.h>
54 #include <netinet/in.h>
59 #include <net/ppp_defs.h>
60 #include <net/pppio.h>
71 * The IP module may use this SAP value for IP packets.
74 #define ETHERTYPE_IP 0x800
78 #define PPP_MAXMTU 65535
84 * Private information; one per upper stream.
86 typedef struct upperstr {
87 minor_t mn; /* minor device number */
88 struct upperstr *nextmn; /* next minor device */
89 queue_t *q; /* read q associated with this upper stream */
90 int flags; /* flag bits, see below */
91 int state; /* current DLPI state */
92 int sap; /* service access point */
93 int req_sap; /* which SAP the DLPI client requested */
94 struct upperstr *ppa; /* control stream for our ppa */
95 struct upperstr *next; /* next stream for this ppa */
96 uint ioc_id; /* last ioctl ID for this stream */
97 enum NPmode npmode; /* what to do with packets on this SAP */
99 * There is exactly one control stream for each PPA.
100 * The following fields are only used for control streams.
103 queue_t *lowerq; /* write queue attached below this PPA */
104 struct upperstr *nextppa; /* next control stream */
107 struct pppstat stats; /* statistics */
108 time_t last_sent; /* time last NP packet sent */
109 time_t last_recv; /* time last NP packet rcvd */
110 struct bpf_program active_f;/* filter for active packets */
111 struct bpf_program pass_f; /* filter for packets to pass */
113 kstat_t *kstats; /* stats for netstat */
117 char ifname[IFNAMSIZ];
118 struct ifstats ifstats;
122 /* Values for flags */
123 #define US_PRIV 1 /* stream was opened by superuser */
124 #define US_CONTROL 2 /* stream is a control stream */
125 #define US_BLOCKED 4 /* flow ctrl has blocked lower write stream */
126 #define US_LASTMOD 8 /* no PPP modules below us */
127 #define US_DBGLOG 0x10 /* log various occurrences */
129 static upperstr_t *minor_devs = NULL;
130 static upperstr_t *ppas = NULL;
133 static int pppopen __P((queue_t *, dev_t *, int, int, cred_t *));
134 static int pppclose __P((queue_t *, int, cred_t *));
136 static int pppopen __P((queue_t *, int, int, int));
137 static int pppclose __P((queue_t *, int));
139 static int pppuwput __P((queue_t *, mblk_t *));
140 static int pppursrv __P((queue_t *));
141 static int pppuwsrv __P((queue_t *));
142 static int ppplrput __P((queue_t *, mblk_t *));
143 static int ppplwput __P((queue_t *, mblk_t *));
144 static int ppplrsrv __P((queue_t *));
145 static int ppplwsrv __P((queue_t *));
147 static void dlpi_request __P((queue_t *, mblk_t *, upperstr_t *));
148 static void dlpi_error __P((queue_t *, int, int, int));
149 static void dlpi_ok __P((queue_t *, int));
151 static int send_data __P((mblk_t *, upperstr_t *));
152 static void new_ppa __P((queue_t *, mblk_t *));
153 static void attach_ppa __P((queue_t *, mblk_t *));
154 static void detach_ppa __P((queue_t *, mblk_t *));
155 static void debug_dump __P((queue_t *, mblk_t *));
156 static upperstr_t *find_dest __P((upperstr_t *, int));
157 static int putctl2 __P((queue_t *, int, int, int));
158 static int putctl4 __P((queue_t *, int, int, int));
160 static struct module_info ppp_info = {
161 0xb1a6, "ppp", 0, 512, 512, 128
164 static struct qinit pppurint = {
165 NULL, pppursrv, pppopen, pppclose, NULL, &ppp_info, NULL
168 static struct qinit pppuwint = {
169 pppuwput, pppuwsrv, NULL, NULL, NULL, &ppp_info, NULL
172 static struct qinit ppplrint = {
173 ppplrput, ppplrsrv, NULL, NULL, NULL, &ppp_info, NULL
176 static struct qinit ppplwint = {
177 ppplwput, ppplwsrv, NULL, NULL, NULL, &ppp_info, NULL
181 extern struct ifstats *ifstats;
185 struct streamtab pppinfo = {
186 &pppurint, &pppuwint,
193 * How we maintain statistics.
196 #define INCR_IPACKETS(ppa) \
197 if (ppa->kstats != 0) { \
198 KSTAT_NAMED_PTR(ppa->kstats)[0].value.ul++; \
200 #define INCR_IERRORS(ppa) \
201 if (ppa->kstats != 0) { \
202 KSTAT_NAMED_PTR(ppa->kstats)[1].value.ul++; \
204 #define INCR_OPACKETS(ppa) \
205 if (ppa->kstats != 0) { \
206 KSTAT_NAMED_PTR(ppa->kstats)[2].value.ul++; \
208 #define INCR_OERRORS(ppa) \
209 if (ppa->kstats != 0) { \
210 KSTAT_NAMED_PTR(ppa->kstats)[3].value.ul++; \
215 #define INCR_IPACKETS(ppa) ppa->ifstats.ifs_ipackets++;
216 #define INCR_IERRORS(ppa) ppa->ifstats.ifs_ierrors++;
217 #define INCR_OPACKETS(ppa) ppa->ifstats.ifs_opackets++;
218 #define INCR_OERRORS(ppa) ppa->ifstats.ifs_oerrors++;
222 * STREAMS driver entry points.
226 pppopen(q, devp, oflag, sflag, credp)
232 pppopen(q, dev, oflag, sflag)
234 int dev; /* really dev_t */
243 DRV_OPEN_OK(dev); /* device is already open */
245 if (sflag == CLONEOPEN) {
247 for (prevp = &minor_devs; (up = *prevp) != 0; prevp = &up->nextmn) {
254 mn = getminor(*devp);
258 for (prevp = &minor_devs; (up = *prevp) != 0; prevp = &up->nextmn) {
263 /* this can't happen */
264 q->q_ptr = WR(q)->q_ptr = (caddr_t) up;
270 * Construct a new minor node.
272 up = (upperstr_t *) ALLOC_SLEEP(sizeof(upperstr_t));
273 bzero((caddr_t) up, sizeof(upperstr_t));
275 DPRINT("pppopen: out of kernel memory\n");
282 *devp = makedevice(getmajor(*devp), mn);
286 up->flags |= US_PRIV;
288 up->state = DL_UNATTACHED;
291 up->ifflags = IFF_UP | IFF_POINTOPOINT;
294 up->last_sent = up->last_recv = time;
295 up->npmode = NPMODE_DROP;
296 q->q_ptr = (caddr_t) up;
297 WR(q)->q_ptr = (caddr_t) up;
302 DRV_OPEN_OK(makedev(major(dev), mn));
307 pppclose(q, flag, credp)
317 upperstr_t *up, **upp;
318 upperstr_t *as, *asnext;
323 up = (upperstr_t *) q->q_ptr;
324 if (up->flags & US_DBGLOG)
325 DPRINT2("ppp/%d: close, flags=%x\n", up->mn, up->flags);
328 if (up->flags & US_CONTROL) {
330 struct ifstats *ifp, *pifp;
333 * This stream represents a PPA:
334 * For all streams attached to the PPA, clear their
335 * references to this PPA.
336 * Then remove this PPA from the list of PPAs.
338 for (as = up->next; as != 0; as = asnext) {
342 if (as->flags & US_BLOCKED) {
343 as->flags &= ~US_BLOCKED;
344 flushq(WR(as->q), FLUSHDATA);
347 for (upp = &ppas; *upp != 0; upp = &(*upp)->nextppa)
353 /* Remove the statistics from the active list. */
354 for (ifp = ifstats, pifp = 0; ifp; ifp = ifp->ifs_next) {
355 if (ifp == &up->ifstats) {
357 pifp->ifs_next = ifp->ifs_next;
359 ifstats = ifp->ifs_next;
367 * If this stream is attached to a PPA,
368 * remove it from the PPA's list.
370 if ((as = up->ppa) != 0) {
371 for (; as->next != 0; as = as->next)
372 if (as->next == up) {
381 kstat_delete(up->kstats);
384 if (up->active_f.bf_insns) {
385 kmem_free(up->active_f.bf_insns, up->active_f.bf_len);
386 up->active_f.bf_insns = 0;
388 if (up->pass_f.bf_insns) {
389 kmem_free(up->pass_f.bf_insns, up->pass_f.bf_len);
390 up->pass_f.bf_insns = 0;
396 for (prevp = &minor_devs; *prevp != 0; prevp = &(*prevp)->nextmn) {
402 kmem_free(up, sizeof(upperstr_t));
409 * A message from on high. We do one of three things:
411 * - put the message on the lower write stream
412 * - queue it for our service routine
419 upperstr_t *us, *usnext, *ppa, *os, *nps;
429 struct ppp_idle *pip;
432 struct bpf_program *dest;
434 us = (upperstr_t *) q->q_ptr;
435 switch (mp->b_datap->db_type) {
439 dlpi_request(q, mp, us);
444 if (us->flags & US_DBGLOG)
445 DPRINT3("ppp/%d: uwput M_DATA len=%d flags=%x\n",
446 us->mn, msgdsize(mp), us->flags);
447 if (us->ppa == 0 || msgdsize(mp) > us->ppa->mtu + PPP_HDRLEN
449 || (us->flags & US_CONTROL) == 0
452 DPRINT1("pppuwput: junk data len=%d\n", msgdsize(mp));
457 if (!pass_packet(us->ppa, mp, 1)) {
462 if (!send_data(mp, us))
467 iop = (struct iocblk *) mp->b_rptr;
469 if (us->flags & US_DBGLOG)
470 DPRINT3("ppp/%d: ioctl %x count=%d\n",
471 us->mn, iop->ioc_cmd, iop->ioc_count);
472 switch (iop->ioc_cmd) {
474 if ((us->flags & US_CONTROL) == 0 || us->lowerq != 0)
476 lb = (struct linkblk *) mp->b_cont->b_rptr;
477 us->lowerq = lq = lb->l_qbot;
478 lq->q_ptr = (caddr_t) us;
479 RD(lq)->q_ptr = (caddr_t) us;
481 flushq(RD(lq), FLUSHALL);
484 us->flags &= ~US_LASTMOD;
485 /* Unblock upper streams which now feed this lower stream. */
487 /* Send useful information down to the modules which
488 are now linked below us. */
489 putctl2(lq, M_CTL, PPPCTL_UNIT, us->ppa_id);
490 putctl4(lq, M_CTL, PPPCTL_MRU, us->mru);
491 putctl4(lq, M_CTL, PPPCTL_MTU, us->mtu);
495 lb = (struct linkblk *) mp->b_cont->b_rptr;
497 if (us->lowerq != lb->l_qbot)
498 DPRINT2("ppp unlink: lowerq=%x qbot=%x\n",
499 us->lowerq, lb->l_qbot);
504 /* Unblock streams which now feed back up the control stream. */
509 if (us->flags & US_CONTROL)
511 if ((us->flags & US_PRIV) == 0) {
515 /* Arrange to return an int */
516 if ((mq = mp->b_cont) == 0
517 || mq->b_datap->db_lim - mq->b_rptr < sizeof(int)) {
518 mq = allocb(sizeof(int), BPRI_HI);
528 iop->ioc_count = sizeof(int);
529 mq->b_wptr = mq->b_rptr + sizeof(int);
530 qwriter(q, mp, new_ppa, PERIM_OUTER);
535 /* like dlpi_attach, for programs which can't write to
536 the stream (like pppstats) */
537 if (iop->ioc_count != sizeof(int) || us->ppa != 0)
539 n = *(int *)mp->b_cont->b_rptr;
540 for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
541 if (ppa->ppa_id == n)
547 qwriter(q, mp, attach_ppa, PERIM_OUTER);
553 /* Attach to a given SAP. */
554 if (iop->ioc_count != sizeof(int) || us->ppa == 0)
556 n = *(int *)mp->b_cont->b_rptr;
557 /* n must be a valid PPP network protocol number. */
558 if (n < 0x21 || n > 0x3fff || (n & 0x101) != 1)
560 /* check that no other stream is bound to this sap already. */
561 for (os = us->ppa; os != 0; os = os->next)
573 if (iop->ioc_count != sizeof(int) || (us->flags & US_CONTROL) == 0)
575 n = *(int *)mp->b_cont->b_rptr;
576 if (n <= 0 || n > PPP_MAXMTU)
582 putctl4(us->lowerq, M_CTL, PPPCTL_MRU, n);
588 if (iop->ioc_count != sizeof(int) || (us->flags & US_CONTROL) == 0)
590 n = *(int *)mp->b_cont->b_rptr;
591 if (n <= 0 || n > PPP_MAXMTU)
597 us->ifstats.ifs_mtu = n;
600 putctl4(us->lowerq, M_CTL, PPPCTL_MTU, n);
606 us->flags |= US_LASTMOD;
611 if (iop->ioc_count != sizeof(int))
613 n = *(int *)mp->b_cont->b_rptr;
614 if (n == PPPDBG_DUMP + PPPDBG_DRIVER) {
615 qwriter(q, NULL, debug_dump, PERIM_OUTER);
618 } else if (n == PPPDBG_LOG + PPPDBG_DRIVER) {
619 DPRINT1("ppp/%d: debug log enabled\n", us->mn);
620 us->flags |= US_DBGLOG;
624 if (us->ppa == 0 || us->ppa->lowerq == 0)
626 putnext(us->ppa->lowerq, mp);
632 if (iop->ioc_count != 2 * sizeof(int))
634 if ((us->flags & US_CONTROL) == 0)
636 sap = ((int *)mp->b_cont->b_rptr)[0];
637 for (nps = us->next; nps != 0; nps = nps->next)
642 nps->npmode = (enum NPmode) ((int *)mp->b_cont->b_rptr)[1];
643 if (nps->npmode == NPMODE_DROP || nps->npmode == NPMODE_ERROR)
644 flushq(WR(nps->q), FLUSHDATA);
645 else if (nps->npmode == NPMODE_PASS && qsize(WR(nps->q)) > 0
646 && (nps->flags & US_BLOCKED) == 0)
653 if ((ppa = us->ppa) == 0)
655 mq = allocb(sizeof(struct ppp_idle), BPRI_HI);
664 pip = (struct ppp_idle *) mq->b_wptr;
665 pip->xmit_idle = time - ppa->last_sent;
666 pip->recv_idle = time - ppa->last_recv;
667 mq->b_wptr += sizeof(struct ppp_idle);
668 iop->ioc_count = sizeof(struct ppp_idle);
673 case PPPIO_ACTIVEFILT:
674 if ((us->flags & US_CONTROL) == 0)
676 len = iop->ioc_count;
677 if (len > BPF_MAXINSNS * sizeof(struct bpf_insn)
678 || len % sizeof(struct bpf_insn) != 0)
681 if (!bpf_validate((struct bpf_insn *) mp->b_cont->b_rptr,
682 len / sizeof(struct bpf_insn)))
684 ip = (struct bpf_insn *) ALLOC_NOSLEEP(len);
689 bcopy((caddr_t)mp->b_cont->b_rptr, (caddr_t)ip, len);
692 dest = iop->ioc_cmd == PPPIO_ACTIVEFILT?
693 &us->active_f: &us->pass_f;
694 if (dest->bf_insns != 0)
695 kmem_free((caddr_t) dest->bf_insns, dest->bf_len);
702 printf("SIOCSIFNAME\n");
703 /* Sent from IP down to us. Attach the ifstats structure. */
704 if (iop->ioc_count != sizeof(struct ifreq) || us->ppa == 0)
706 ifr = (struct ifreq *)mp->b_cont->b_rptr;
707 /* Find the unit number in the interface name. */
708 for (i = 0; i < IFNAMSIZ; i++) {
709 if (ifr->ifr_name[i] == 0 ||
710 (ifr->ifr_name[i] >= '0' &&
711 ifr->ifr_name[i] <= '9'))
714 us->ifname[i] = ifr->ifr_name[i];
718 /* Convert the unit number to binary. */
719 for (n = 0; i < IFNAMSIZ; i++) {
720 if (ifr->ifr_name[i] == 0) {
724 n = n * 10 + ifr->ifr_name[i] - '0';
728 /* Verify the ppa. */
729 if (us->ppa->ppa_id != n)
733 /* Set up the netstat block. */
734 strncpy (ppa->ifname, us->ifname, IFNAMSIZ);
736 ppa->ifstats.ifs_name = ppa->ifname;
737 ppa->ifstats.ifs_unit = n;
738 ppa->ifstats.ifs_active = us->state != DL_UNBOUND;
739 ppa->ifstats.ifs_mtu = ppa->mtu;
741 /* Link in statistics used by netstat. */
742 ppa->ifstats.ifs_next = ifstats;
743 ifstats = &ppa->ifstats;
750 printf("SIOCGIFFLAGS\n");
751 if (!(us->flags & US_CONTROL)) {
757 ((struct iocblk_in *)iop)->ioc_ifflags = us->ifflags;
762 printf("SIOCSIFFLAGS\n");
763 if (!(us->flags & US_CONTROL)) {
769 us->ifflags = ((struct iocblk_in *)iop)->ioc_ifflags;
774 printf("SIOCSIFADDR\n");
775 if (!(us->flags & US_CONTROL)) {
781 us->ifflags |= IFF_RUNNING;
782 ((struct iocblk_in *)iop)->ioc_ifflags |= IFF_RUNNING;
797 if (us->ppa == 0 || us->ppa->lowerq == 0)
799 us->ioc_id = iop->ioc_id;
801 switch (iop->ioc_cmd) {
804 if (us->flags & US_LASTMOD) {
808 putnext(us->ppa->lowerq, mp);
811 if (us->flags & US_PRIV)
812 putnext(us->ppa->lowerq, mp);
814 DPRINT1("ppp ioctl %x rejected\n", iop->ioc_cmd);
823 iop->ioc_error = error;
824 mp->b_datap->db_type = M_IOCNAK;
826 } else if (error == 0) {
827 mp->b_datap->db_type = M_IOCACK;
833 if (us->flags & US_DBGLOG)
834 DPRINT2("ppp/%d: flush %x\n", us->mn, *mp->b_rptr);
835 if (*mp->b_rptr & FLUSHW)
836 flushq(q, FLUSHDATA);
837 if (*mp->b_rptr & FLUSHR) {
838 *mp->b_rptr &= ~FLUSHW;
853 dlpi_request(q, mp, us)
858 union DL_primitives *d = (union DL_primitives *) mp->b_rptr;
859 int size = mp->b_wptr - mp->b_rptr;
861 upperstr_t *ppa, *os;
866 if (us->flags & US_DBGLOG)
867 cmn_err(CE_CONT, "ppp/%d: dlpi prim %x len=%d\n", us->mn,
868 d->dl_primitive, size);
869 switch (d->dl_primitive) {
871 if (size < sizeof(dl_info_req_t))
873 if ((reply = allocb(sizeof(dl_info_ack_t), BPRI_HI)) == 0)
874 break; /* should do bufcall */
875 reply->b_datap->db_type = M_PCPROTO;
876 info = (dl_info_ack_t *) reply->b_wptr;
877 reply->b_wptr += sizeof(dl_info_ack_t);
878 bzero((caddr_t) info, sizeof(dl_info_ack_t));
879 info->dl_primitive = DL_INFO_ACK;
880 info->dl_max_sdu = PPP_MAXMTU;
881 info->dl_min_sdu = 1;
882 info->dl_addr_length = sizeof(ulong);
884 info->dl_mac_type = DL_OTHER;
886 info->dl_mac_type = DL_HDLC; /* a lie */
888 info->dl_current_state = us->state;
889 info->dl_service_mode = DL_CLDLS;
890 info->dl_provider_style = DL_STYLE2;
891 #if DL_CURRENT_VERSION >= 2
892 info->dl_sap_length = sizeof(ulong);
893 info->dl_version = DL_CURRENT_VERSION;
899 if (size < sizeof(dl_attach_req_t))
901 if (us->state != DL_UNATTACHED || us->ppa != 0) {
902 dlpi_error(q, DL_ATTACH_REQ, DL_OUTSTATE, 0);
905 for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
906 if (ppa->ppa_id == d->attach_req.dl_ppa)
909 dlpi_error(q, DL_ATTACH_REQ, DL_BADPPA, 0);
913 qwriter(q, mp, attach_ppa, PERIM_OUTER);
917 if (size < sizeof(dl_detach_req_t))
919 if (us->state != DL_UNBOUND || us->ppa == 0) {
920 dlpi_error(q, DL_DETACH_REQ, DL_OUTSTATE, 0);
923 qwriter(q, mp, detach_ppa, PERIM_OUTER);
927 if (size < sizeof(dl_bind_req_t))
929 if (us->state != DL_UNBOUND || us->ppa == 0) {
930 dlpi_error(q, DL_BIND_REQ, DL_OUTSTATE, 0);
933 if (d->bind_req.dl_service_mode != DL_CLDLS) {
934 dlpi_error(q, DL_BIND_REQ, DL_UNSUPPORTED, 0);
938 /* saps must be valid PPP network protocol numbers,
939 except that we accept ETHERTYPE_IP in place of PPP_IP. */
940 sap = d->bind_req.dl_sap;
942 DPRINT1("ppp bind %x\n", sap);
943 if (sap == ETHERTYPE_IP)
945 if (sap < 0x21 || sap > 0x3fff || (sap & 0x101) != 1) {
946 dlpi_error(q, DL_BIND_REQ, DL_BADADDR, 0);
950 /* check that no other stream is bound to this sap already. */
951 for (os = us->ppa; os != 0; os = os->next)
955 dlpi_error(q, DL_BIND_REQ, DL_NOADDR, 0);
962 if ((reply = allocb(sizeof(dl_bind_ack_t) + sizeof(ulong),
964 break; /* should do bufcall */
965 ackp = (dl_bind_ack_t *) reply->b_wptr;
966 reply->b_wptr += sizeof(dl_bind_ack_t) + sizeof(ulong);
967 reply->b_datap->db_type = M_PCPROTO;
968 bzero((caddr_t) ackp, sizeof(dl_bind_ack_t));
969 ackp->dl_primitive = DL_BIND_ACK;
971 ackp->dl_addr_length = sizeof(ulong);
972 ackp->dl_addr_offset = sizeof(dl_bind_ack_t);
973 *(ulong *)(ackp+1) = sap;
978 if (size < sizeof(dl_unbind_req_t))
980 if (us->state != DL_IDLE) {
981 dlpi_error(q, DL_UNBIND_REQ, DL_OUTSTATE, 0);
985 us->state = DL_UNBOUND;
987 us->ppa->ifstats.ifs_active = 0;
989 dlpi_ok(q, DL_UNBIND_REQ);
992 case DL_UNITDATA_REQ:
993 if (size < sizeof(dl_unitdata_req_t))
995 if (us->state != DL_IDLE) {
996 dlpi_error(q, DL_UNITDATA_REQ, DL_OUTSTATE, 0);
999 if ((ppa = us->ppa) == 0) {
1000 cmn_err(CE_CONT, "ppp: in state dl_idle but ppa == 0?\n");
1003 len = mp->b_cont == 0? 0: msgdsize(mp->b_cont);
1004 if (len > ppa->mtu) {
1005 DPRINT2("dlpi data too large (%d > %d)\n", len, ppa->mtu);
1008 /* this assumes PPP_HDRLEN <= sizeof(dl_unitdata_req_t) */
1009 if (mp->b_datap->db_ref > 1) {
1010 np = allocb(PPP_HDRLEN, BPRI_HI);
1013 np->b_cont = mp->b_cont;
1018 mp->b_datap->db_type = M_DATA;
1019 /* XXX should use dl_dest_addr_offset/length here,
1020 but we would have to translate ETHERTYPE_IP -> PPP_IP */
1021 mp->b_wptr = mp->b_rptr + PPP_HDRLEN;
1022 mp->b_rptr[0] = PPP_ALLSTATIONS;
1023 mp->b_rptr[1] = PPP_UI;
1024 mp->b_rptr[2] = us->sap >> 8;
1025 mp->b_rptr[3] = us->sap;
1026 if (!pass_packet(ppa, mp, 1))
1029 if (!send_data(mp, us))
1034 #if DL_CURRENT_VERSION >= 2
1035 case DL_SUBS_BIND_REQ:
1036 case DL_SUBS_UNBIND_REQ:
1037 case DL_ENABMULTI_REQ:
1038 case DL_DISABMULTI_REQ:
1039 case DL_PROMISCON_REQ:
1040 case DL_PROMISCOFF_REQ:
1041 case DL_PHYS_ADDR_REQ:
1042 case DL_SET_PHYS_ADDR_REQ:
1045 case DL_REPLY_UPDATE_REQ:
1047 case DL_DATA_ACK_REQ:
1049 case DL_CONNECT_REQ:
1051 dlpi_error(q, d->dl_primitive, DL_NOTSUPPORTED, 0);
1054 case DL_CONNECT_RES:
1055 case DL_DISCONNECT_REQ:
1058 dlpi_error(q, d->dl_primitive, DL_OUTSTATE, 0);
1062 dlpi_error(q, d->dl_primitive, DL_BADQOSTYPE, 0);
1065 #if DL_CURRENT_VERSION >= 2
1072 cmn_err(CE_CONT, "ppp: unknown dlpi prim 0x%x\n", d->dl_primitive);
1075 dlpi_error(q, d->dl_primitive, DL_BADPRIM, 0);
1082 dlpi_error(q, prim, err, uerr)
1084 int prim, err, uerr;
1087 dl_error_ack_t *errp;
1089 reply = allocb(sizeof(dl_error_ack_t), BPRI_HI);
1091 return; /* XXX should do bufcall */
1092 reply->b_datap->db_type = M_PCPROTO;
1093 errp = (dl_error_ack_t *) reply->b_wptr;
1094 reply->b_wptr += sizeof(dl_error_ack_t);
1095 errp->dl_primitive = DL_ERROR_ACK;
1096 errp->dl_error_primitive = prim;
1097 errp->dl_errno = err;
1098 errp->dl_unix_errno = uerr;
1110 reply = allocb(sizeof(dl_ok_ack_t), BPRI_HI);
1112 return; /* XXX should do bufcall */
1113 reply->b_datap->db_type = M_PCPROTO;
1114 okp = (dl_ok_ack_t *) reply->b_wptr;
1115 reply->b_wptr += sizeof(dl_ok_ack_t);
1116 okp->dl_primitive = DL_OK_ACK;
1117 okp->dl_correct_primitive = prim;
1120 #endif /* NO_DLPI */
1123 pass_packet(ppa, mp, outbound)
1130 if (PPP_PROTOCOL(mp->b_rptr) >= 0x8000
1131 || (ppa->pass_f.bf_insns == 0 && ppa->active_f.bf_insns == 0))
1135 *mp->b_rptr = outbound;
1136 pass = ppa->pass_f.bf_insns == 0
1137 || bpf_filter(ppa->pass_f.bf_insns, mp, len, 0);
1138 if (pass && (ppa->active_f.bf_insns == 0
1139 || bpf_filter(ppa->active_f.bf_insns, mp, len, 0))) {
1141 ppa->last_sent = time;
1143 ppa->last_recv = time;
1157 if (us->flags & US_BLOCKED || us->npmode == NPMODE_QUEUE)
1160 if (ppa == 0 || us->npmode == NPMODE_DROP || us->npmode == NPMODE_ERROR) {
1164 if ((q = ppa->lowerq) == 0) {
1165 /* try to send it up the control stream */
1166 if (canputnext(ppa->q)) {
1167 putnext(ppa->q, mp);
1171 if (canputnext(ppa->lowerq)) {
1173 * The lower write queue's put procedure just updates counters
1174 * and does a putnext. We call it so that on SMP systems, we
1175 * enter the lower queues' perimeter so that the counter
1176 * updates are serialized.
1178 put(ppa->lowerq, mp);
1182 us->flags |= US_BLOCKED;
1187 * Allocate a new PPA id and link this stream into the list of PPAs.
1188 * This procedure is called with an exclusive lock on all queues in
1196 upperstr_t *us, **usp;
1201 while ((us = *usp) != 0 && ppa_id == us->ppa_id) {
1205 us = (upperstr_t *) q->q_ptr;
1206 us->ppa_id = ppa_id;
1211 us->flags |= US_CONTROL;
1212 us->npmode = NPMODE_PASS;
1219 * Create a kstats record for our statistics, so netstat -i works.
1221 if (us->kstats == 0) {
1224 sprintf(unit, "ppp%d", us->ppa->ppa_id);
1225 us->kstats = kstat_create("ppp", us->ppa->ppa_id, unit,
1226 "net", KSTAT_TYPE_NAMED, 4, 0);
1227 if (us->kstats != 0) {
1228 kstat_named_t *kn = KSTAT_NAMED_PTR(us->kstats);
1230 strcpy(kn[0].name, "ipackets");
1231 kn[0].data_type = KSTAT_DATA_ULONG;
1232 strcpy(kn[1].name, "ierrors");
1233 kn[1].data_type = KSTAT_DATA_ULONG;
1234 strcpy(kn[2].name, "opackets");
1235 kn[2].data_type = KSTAT_DATA_ULONG;
1236 strcpy(kn[3].name, "oerrors");
1237 kn[3].data_type = KSTAT_DATA_ULONG;
1238 kstat_install(us->kstats);
1243 *(int *)mp->b_cont->b_rptr = ppa_id;
1244 mp->b_datap->db_type = M_IOCACK;
1255 us = (upperstr_t *) q->q_ptr;
1257 us->state = DL_UNBOUND;
1259 for (t = us->ppa; t->next != 0; t = t->next)
1263 if (mp->b_datap->db_type == M_IOCTL) {
1264 mp->b_datap->db_type = M_IOCACK;
1268 dlpi_ok(q, DL_ATTACH_REQ);
1280 us = (upperstr_t *) q->q_ptr;
1281 for (t = us->ppa; t->next != 0; t = t->next)
1282 if (t->next == us) {
1289 us->state = DL_UNATTACHED;
1290 dlpi_ok(q, DL_DETACH_REQ);
1299 struct lowerstr *ls;
1303 us = (upperstr_t *) q->q_ptr;
1304 us->flags &= ~US_BLOCKED;
1305 while ((mp = getq(q)) != 0) {
1306 if (!send_data(mp, us)) {
1321 ppa = (upperstr_t *) q->q_ptr;
1322 if (ppa != 0) { /* why wouldn't it? */
1323 ppa->stats.ppp_opackets++;
1324 ppa->stats.ppp_obytes += msgdsize(mp);
1325 #ifdef INCR_OPACKETS
1340 * Flow control has back-enabled this stream:
1341 * enable the write service procedures of all upper
1342 * streams feeding this lower stream.
1344 for (us = (upperstr_t *) q->q_ptr; us != NULL; us = us->next)
1345 if (us->flags & US_BLOCKED)
1354 upperstr_t *us, *as;
1357 dl_unitdata_ind_t *ud;
1361 us = (upperstr_t *) q->q_ptr;
1362 if (us->flags & US_CONTROL) {
1365 * If there is no lower queue attached, run the write service
1366 * routines of other upper streams attached to this PPA.
1368 if (us->lowerq == 0) {
1371 if (as->flags & US_BLOCKED)
1378 * A network protocol stream. Put a DLPI header on each
1379 * packet and send it on.
1380 * (Actually, it seems that the IP module will happily
1381 * accept M_DATA messages without the DL_UNITDATA_IND header.)
1383 while ((mp = getq(q)) != 0) {
1384 if (!canputnext(q)) {
1389 proto = PPP_PROTOCOL(mp->b_rptr);
1390 mp->b_rptr += PPP_HDRLEN;
1391 hdr = allocb(sizeof(dl_unitdata_ind_t) + 2 * sizeof(ulong),
1394 /* XXX should put it back and use bufcall */
1398 hdr->b_datap->db_type = M_PROTO;
1399 ud = (dl_unitdata_ind_t *) hdr->b_wptr;
1400 hdr->b_wptr += sizeof(dl_unitdata_ind_t) + 2 * sizeof(ulong);
1402 ud->dl_primitive = DL_UNITDATA_IND;
1403 ud->dl_dest_addr_length = sizeof(ulong);
1404 ud->dl_dest_addr_offset = sizeof(dl_unitdata_ind_t);
1405 ud->dl_src_addr_length = sizeof(ulong);
1406 ud->dl_src_addr_offset = ud->dl_dest_addr_offset + sizeof(ulong);
1407 #if DL_CURRENT_VERSION >= 2
1408 ud->dl_group_address = 0;
1410 /* Send the DLPI client the data with the SAP they requested,
1411 (e.g. ETHERTYPE_IP) rather than the PPP protocol number
1413 ((ulong *)(ud + 1))[0] = us->req_sap; /* dest SAP */
1414 ((ulong *)(ud + 1))[1] = us->req_sap; /* src SAP */
1418 #endif /* NO_DLPI */
1423 * If this stream is attached to a PPA with a lower queue pair,
1424 * enable the read queue's service routine if it has data queued.
1425 * XXX there is a possibility that packets could get out of order
1426 * if ppplrput now runs before ppplrsrv.
1428 if (us->ppa != 0 && us->ppa->lowerq != 0)
1429 qenable(RD(us->ppa->lowerq));
1435 find_dest(ppa, proto)
1441 for (us = ppa->next; us != 0; us = us->next)
1442 if (proto == us->sap)
1452 upperstr_t *ppa, *us;
1458 ppa = (upperstr_t *) q->q_ptr;
1460 DPRINT1("ppplrput: q = %x, ppa = 0??\n", q);
1464 switch (mp->b_datap->db_type) {
1466 if (*mp->b_rptr & FLUSHW) {
1467 *mp->b_rptr &= ~FLUSHR;
1474 switch (*mp->b_rptr) {
1479 ppa->stats.ppp_ierrors++;
1485 ppa->stats.ppp_oerrors++;
1494 * Attempt to match up the response with the stream
1495 * that the request came from.
1497 iop = (struct iocblk *) mp->b_rptr;
1498 for (us = ppa; us != 0; us = us->next)
1499 if (us->ioc_id == iop->ioc_id)
1508 if (mp->b_datap->db_type == M_DATA) {
1510 if (mp->b_wptr - mp->b_rptr < PPP_HDRLEN) {
1511 PULLUP(mp, PPP_HDRLEN);
1513 DPRINT1("ppp_lrput: msgpullup failed (len=%d)\n", len);
1517 ppa->stats.ppp_ipackets++;
1518 ppa->stats.ppp_ibytes += len;
1519 #ifdef INCR_IPACKETS
1522 if (!pass_packet(ppa, mp, 0)) {
1526 proto = PPP_PROTOCOL(mp->b_rptr);
1527 if (proto < 0x8000 && (us = find_dest(ppa, proto)) != 0) {
1529 * A data packet for some network protocol.
1530 * Queue it on the upper stream for that protocol.
1540 * A control frame, a frame for an unknown protocol,
1541 * or some other message type.
1542 * Send it up to pppd via the control stream.
1544 if (queclass(mp) == QPCTL || canputnext(ppa->q))
1545 putnext(ppa->q, mp);
1559 upperstr_t *ppa, *us;
1563 * Packets only get queued here for flow control reasons.
1565 ppa = (upperstr_t *) q->q_ptr;
1566 while ((mp = getq(q)) != 0) {
1567 if (mp->b_datap->db_type == M_DATA
1568 && (proto = PPP_PROTOCOL(mp->b_rptr)) < 0x8000
1569 && (us = find_dest(ppa, proto)) != 0) {
1577 if (canputnext(ppa->q))
1578 putnext(ppa->q, mp);
1589 putctl2(q, type, code, val)
1591 int type, code, val;
1595 mp = allocb(2, BPRI_HI);
1598 mp->b_datap->db_type = type;
1599 mp->b_wptr[0] = code;
1600 mp->b_wptr[1] = val;
1607 putctl4(q, type, code, val)
1609 int type, code, val;
1613 mp = allocb(4, BPRI_HI);
1616 mp->b_datap->db_type = type;
1617 mp->b_wptr[0] = code;
1618 ((short *)mp->b_wptr)[1] = val;
1626 queue_t *q; /* not used */
1627 mblk_t *mp; /* not used either */
1632 DPRINT("ppp upper streams:\n");
1633 for (us = minor_devs; us != 0; us = us->nextmn) {
1635 DPRINT3(" %d: q=%x rlev=%d",
1636 us->mn, uq, (uq? qsize(uq): 0));
1637 DPRINT3(" wlev=%d flags=0x%b", (uq? qsize(WR(uq)): 0),
1638 us->flags, "\020\1priv\2control\3blocked\4last");
1639 DPRINT3(" state=%x sap=%x req_sap=%x", us->state, us->sap,
1644 DPRINT1(" ppa=%d\n", us->ppa->ppa_id);
1645 if (us->flags & US_CONTROL) {
1647 DPRINT3(" control for %d lq=%x rlev=%d",
1648 us->ppa_id, lq, (lq? qsize(RD(lq)): 0));
1649 DPRINT3(" wlev=%d mru=%d mtu=%d\n",
1650 (lq? qsize(lq): 0), us->mru, us->mtu);