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.3 1996/01/01 22:48:39 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>
70 * The IP module may use this SAP value for IP packets.
73 #define ETHERTYPE_IP 0x800
77 #define PPP_MAXMTU 65535
83 * Private information; one per upper stream.
85 typedef struct upperstr {
86 minor_t mn; /* minor device number */
87 struct upperstr *nextmn; /* next minor device */
88 queue_t *q; /* read q associated with this upper stream */
89 int flags; /* flag bits, see below */
90 int state; /* current DLPI state */
91 int sap; /* service access point */
92 int req_sap; /* which SAP the DLPI client requested */
93 struct upperstr *ppa; /* control stream for our ppa */
94 struct upperstr *next; /* next stream for this ppa */
95 uint ioc_id; /* last ioctl ID for this stream */
96 enum NPmode npmode; /* what to do with packets on this SAP */
98 * There is exactly one control stream for each PPA.
99 * The following fields are only used for control streams.
102 queue_t *lowerq; /* write queue attached below this PPA */
103 struct upperstr *nextppa; /* next control stream */
106 struct pppstat stats; /* statistics */
107 time_t last_sent; /* time last NP packet sent */
108 time_t last_recv; /* time last NP packet rcvd */
110 kstat_t *kstats; /* stats for netstat */
114 char ifname[IFNAMSIZ];
115 struct ifstats ifstats;
119 /* Values for flags */
120 #define US_PRIV 1 /* stream was opened by superuser */
121 #define US_CONTROL 2 /* stream is a control stream */
122 #define US_BLOCKED 4 /* flow ctrl has blocked lower write stream */
123 #define US_LASTMOD 8 /* no PPP modules below us */
124 #define US_DBGLOG 0x10 /* log various occurrences */
126 static upperstr_t *minor_devs = NULL;
127 static upperstr_t *ppas = NULL;
130 static int pppopen __P((queue_t *, dev_t *, int, int, cred_t *));
131 static int pppclose __P((queue_t *, int, cred_t *));
133 static int pppopen __P((queue_t *, int, int, int));
134 static int pppclose __P((queue_t *, int));
136 static int pppuwput __P((queue_t *, mblk_t *));
137 static int pppursrv __P((queue_t *));
138 static int pppuwsrv __P((queue_t *));
139 static int ppplrput __P((queue_t *, mblk_t *));
140 static int ppplwput __P((queue_t *, mblk_t *));
141 static int ppplrsrv __P((queue_t *));
142 static int ppplwsrv __P((queue_t *));
144 static void dlpi_request __P((queue_t *, mblk_t *, upperstr_t *));
145 static void dlpi_error __P((queue_t *, int, int, int));
146 static void dlpi_ok __P((queue_t *, int));
148 static int send_data __P((mblk_t *, upperstr_t *));
149 static void new_ppa __P((queue_t *, mblk_t *));
150 static void attach_ppa __P((queue_t *, mblk_t *));
151 static void detach_ppa __P((queue_t *, mblk_t *));
152 static void debug_dump __P((queue_t *, mblk_t *));
153 static upperstr_t *find_dest __P((upperstr_t *, int));
154 static int putctl2 __P((queue_t *, int, int, int));
155 static int putctl4 __P((queue_t *, int, int, int));
157 static struct module_info ppp_info = {
158 0xb1a6, "ppp", 0, 512, 512, 128
161 static struct qinit pppurint = {
162 NULL, pppursrv, pppopen, pppclose, NULL, &ppp_info, NULL
165 static struct qinit pppuwint = {
166 pppuwput, pppuwsrv, NULL, NULL, NULL, &ppp_info, NULL
169 static struct qinit ppplrint = {
170 ppplrput, ppplrsrv, NULL, NULL, NULL, &ppp_info, NULL
173 static struct qinit ppplwint = {
174 ppplwput, ppplwsrv, NULL, NULL, NULL, &ppp_info, NULL
178 extern struct ifstats *ifstats;
182 struct streamtab pppinfo = {
183 &pppurint, &pppuwint,
190 * How we maintain statistics.
193 #define INCR_IPACKETS(ppa) \
194 if (ppa->kstats != 0) { \
195 KSTAT_NAMED_PTR(ppa->kstats)[0].value.ul++; \
197 #define INCR_IERRORS(ppa) \
198 if (ppa->kstats != 0) { \
199 KSTAT_NAMED_PTR(ppa->kstats)[1].value.ul++; \
201 #define INCR_OPACKETS(ppa) \
202 if (ppa->kstats != 0) { \
203 KSTAT_NAMED_PTR(ppa->kstats)[2].value.ul++; \
205 #define INCR_OERRORS(ppa) \
206 if (ppa->kstats != 0) { \
207 KSTAT_NAMED_PTR(ppa->kstats)[3].value.ul++; \
212 #define INCR_IPACKETS(ppa) ppa->ifstats.ifs_ipackets++;
213 #define INCR_IERRORS(ppa) ppa->ifstats.ifs_ierrors++;
214 #define INCR_OPACKETS(ppa) ppa->ifstats.ifs_opackets++;
215 #define INCR_OERRORS(ppa) ppa->ifstats.ifs_oerrors++;
219 * STREAMS driver entry points.
223 pppopen(q, devp, oflag, sflag, credp)
229 pppopen(q, dev, oflag, sflag)
231 int dev; /* really dev_t */
240 DRV_OPEN_OK(dev); /* device is already open */
242 if (sflag == CLONEOPEN) {
244 for (prevp = &minor_devs; (up = *prevp) != 0; prevp = &up->nextmn) {
251 mn = getminor(*devp);
255 for (prevp = &minor_devs; (up = *prevp) != 0; prevp = &up->nextmn) {
260 /* this can't happen */
261 q->q_ptr = WR(q)->q_ptr = (caddr_t) up;
267 * Construct a new minor node.
269 up = (upperstr_t *) ALLOC_SLEEP(sizeof(upperstr_t));
270 bzero((caddr_t) up, sizeof(upperstr_t));
272 DPRINT("pppopen: out of kernel memory\n");
279 *devp = makedevice(getmajor(*devp), mn);
283 up->flags |= US_PRIV;
285 up->state = DL_UNATTACHED;
288 up->ifflags = IFF_UP | IFF_POINTOPOINT;
291 up->last_sent = up->last_recv = time;
292 up->npmode = NPMODE_DROP;
293 q->q_ptr = (caddr_t) up;
294 WR(q)->q_ptr = (caddr_t) up;
299 DRV_OPEN_OK(makedev(major(dev), mn));
304 pppclose(q, flag, credp)
314 upperstr_t *up, **upp;
315 upperstr_t *as, *asnext;
320 up = (upperstr_t *) q->q_ptr;
321 if (up->flags & US_DBGLOG)
322 DPRINT2("ppp/%d: close, flags=%x\n", up->mn, up->flags);
325 if (up->flags & US_CONTROL) {
327 struct ifstats *ifp, *pifp;
330 * This stream represents a PPA:
331 * For all streams attached to the PPA, clear their
332 * references to this PPA.
333 * Then remove this PPA from the list of PPAs.
335 for (as = up->next; as != 0; as = asnext) {
339 if (as->flags & US_BLOCKED) {
340 as->flags &= ~US_BLOCKED;
341 flushq(WR(as->q), FLUSHDATA);
344 for (upp = &ppas; *upp != 0; upp = &(*upp)->nextppa)
350 /* Remove the statistics from the active list. */
351 for (ifp = ifstats, pifp = 0; ifp; ifp = ifp->ifs_next) {
352 if (ifp == &up->ifstats) {
354 pifp->ifs_next = ifp->ifs_next;
356 ifstats = ifp->ifs_next;
364 * If this stream is attached to a PPA,
365 * remove it from the PPA's list.
367 if ((as = up->ppa) != 0) {
368 for (; as->next != 0; as = as->next)
369 if (as->next == up) {
378 kstat_delete(up->kstats);
384 for (prevp = &minor_devs; *prevp != 0; prevp = &(*prevp)->nextmn) {
390 kmem_free(up, sizeof(upperstr_t));
397 * A message from on high. We do one of three things:
399 * - put the message on the lower write stream
400 * - queue it for our service routine
407 upperstr_t *us, *usnext, *ppa, *os, *nps;
417 struct ppp_idle *pip;
419 us = (upperstr_t *) q->q_ptr;
420 switch (mp->b_datap->db_type) {
424 dlpi_request(q, mp, us);
429 if (us->flags & US_DBGLOG)
430 DPRINT3("ppp/%d: uwput M_DATA len=%d flags=%x\n",
431 us->mn, msgdsize(mp), us->flags);
432 if (us->ppa == 0 || msgdsize(mp) > us->ppa->mtu + PPP_HDRLEN
434 || (us->flags & US_CONTROL) == 0
437 DPRINT1("pppuwput: junk data len=%d\n", msgdsize(mp));
442 if ((us->flags & US_CONTROL) == 0)
443 us->ppa->last_sent = time;
445 if (!send_data(mp, us))
450 iop = (struct iocblk *) mp->b_rptr;
452 if (us->flags & US_DBGLOG)
453 DPRINT3("ppp/%d: ioctl %x count=%d\n",
454 us->mn, iop->ioc_cmd, iop->ioc_count);
455 switch (iop->ioc_cmd) {
457 if ((us->flags & US_CONTROL) == 0 || us->lowerq != 0)
459 lb = (struct linkblk *) mp->b_cont->b_rptr;
460 us->lowerq = lq = lb->l_qbot;
461 lq->q_ptr = (caddr_t) us;
462 RD(lq)->q_ptr = (caddr_t) us;
464 flushq(RD(lq), FLUSHALL);
467 us->flags &= ~US_LASTMOD;
468 /* Unblock upper streams which now feed this lower stream. */
470 /* Send useful information down to the modules which
471 are now linked below us. */
472 putctl2(lq, M_CTL, PPPCTL_UNIT, us->ppa_id);
473 putctl4(lq, M_CTL, PPPCTL_MRU, us->mru);
474 putctl4(lq, M_CTL, PPPCTL_MTU, us->mtu);
478 lb = (struct linkblk *) mp->b_cont->b_rptr;
480 if (us->lowerq != lb->l_qbot)
481 DPRINT2("ppp unlink: lowerq=%x qbot=%x\n",
482 us->lowerq, lb->l_qbot);
487 /* Unblock streams which now feed back up the control stream. */
492 if (us->flags & US_CONTROL)
494 if ((us->flags & US_PRIV) == 0) {
498 /* Arrange to return an int */
499 if ((mq = mp->b_cont) == 0
500 || mq->b_datap->db_lim - mq->b_rptr < sizeof(int)) {
501 mq = allocb(sizeof(int), BPRI_HI);
511 iop->ioc_count = sizeof(int);
512 mq->b_wptr = mq->b_rptr + sizeof(int);
513 qwriter(q, mp, new_ppa, PERIM_OUTER);
518 /* like dlpi_attach, for programs which can't write to
519 the stream (like pppstats) */
520 if (iop->ioc_count != sizeof(int) || us->ppa != 0)
522 n = *(int *)mp->b_cont->b_rptr;
523 for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
524 if (ppa->ppa_id == n)
530 qwriter(q, mp, attach_ppa, PERIM_OUTER);
536 /* Attach to a given SAP. */
537 if (iop->ioc_count != sizeof(int) || us->ppa == 0)
539 n = *(int *)mp->b_cont->b_rptr;
540 /* n must be a valid PPP network protocol number. */
541 if (n < 0x21 || n > 0x3fff || (n & 0x101) != 1)
543 /* check that no other stream is bound to this sap already. */
544 for (os = us->ppa; os != 0; os = os->next)
556 if (iop->ioc_count != sizeof(int) || (us->flags & US_CONTROL) == 0)
558 n = *(int *)mp->b_cont->b_rptr;
559 if (n <= 0 || n > PPP_MAXMTU)
565 putctl4(us->lowerq, M_CTL, PPPCTL_MRU, n);
571 if (iop->ioc_count != sizeof(int) || (us->flags & US_CONTROL) == 0)
573 n = *(int *)mp->b_cont->b_rptr;
574 if (n <= 0 || n > PPP_MAXMTU)
580 us->ifstats.ifs_mtu = n;
583 putctl4(us->lowerq, M_CTL, PPPCTL_MTU, n);
589 us->flags |= US_LASTMOD;
594 if (iop->ioc_count != sizeof(int))
596 n = *(int *)mp->b_cont->b_rptr;
597 if (n == PPPDBG_DUMP + PPPDBG_DRIVER) {
598 qwriter(q, NULL, debug_dump, PERIM_OUTER);
601 } else if (n == PPPDBG_LOG + PPPDBG_DRIVER) {
602 DPRINT1("ppp/%d: debug log enabled\n", us->mn);
603 us->flags |= US_DBGLOG;
607 if (us->ppa == 0 || us->ppa->lowerq == 0)
609 putnext(us->ppa->lowerq, mp);
615 if (iop->ioc_count != 2 * sizeof(int))
617 if ((us->flags & US_CONTROL) == 0)
619 sap = ((int *)mp->b_cont->b_rptr)[0];
620 for (nps = us->next; nps != 0; nps = nps->next)
625 nps->npmode = (enum NPmode) ((int *)mp->b_cont->b_rptr)[1];
626 if (nps->npmode == NPMODE_DROP || nps->npmode == NPMODE_ERROR)
627 flushq(WR(nps->q), FLUSHDATA);
628 else if (nps->npmode == NPMODE_PASS && qsize(WR(nps->q)) > 0
629 && (nps->flags & US_BLOCKED) == 0)
636 if ((ppa = us->ppa) == 0)
638 mq = allocb(sizeof(struct ppp_idle), BPRI_HI);
647 pip = (struct ppp_idle *) mq->b_wptr;
648 pip->xmit_idle = time - ppa->last_sent;
649 pip->recv_idle = time - ppa->last_recv;
650 mq->b_wptr += sizeof(struct ppp_idle);
651 iop->ioc_count = sizeof(struct ppp_idle);
657 printf("SIOCSIFNAME\n");
658 /* Sent from IP down to us. Attach the ifstats structure. */
659 if (iop->ioc_count != sizeof(struct ifreq) || us->ppa == 0)
661 ifr = (struct ifreq *)mp->b_cont->b_rptr;
662 /* Find the unit number in the interface name. */
663 for (i = 0; i < IFNAMSIZ; i++) {
664 if (ifr->ifr_name[i] == 0 ||
665 (ifr->ifr_name[i] >= '0' &&
666 ifr->ifr_name[i] <= '9'))
669 us->ifname[i] = ifr->ifr_name[i];
673 /* Convert the unit number to binary. */
674 for (n = 0; i < IFNAMSIZ; i++) {
675 if (ifr->ifr_name[i] == 0) {
679 n = n * 10 + ifr->ifr_name[i] - '0';
683 /* Verify the ppa. */
684 if (us->ppa->ppa_id != n)
688 /* Set up the netstat block. */
689 strncpy (ppa->ifname, us->ifname, IFNAMSIZ);
691 ppa->ifstats.ifs_name = ppa->ifname;
692 ppa->ifstats.ifs_unit = n;
693 ppa->ifstats.ifs_active = us->state != DL_UNBOUND;
694 ppa->ifstats.ifs_mtu = ppa->mtu;
696 /* Link in statistics used by netstat. */
697 ppa->ifstats.ifs_next = ifstats;
698 ifstats = &ppa->ifstats;
705 printf("SIOCGIFFLAGS\n");
706 if (!(us->flags & US_CONTROL)) {
712 ((struct iocblk_in *)iop)->ioc_ifflags = us->ifflags;
717 printf("SIOCSIFFLAGS\n");
718 if (!(us->flags & US_CONTROL)) {
724 us->ifflags = ((struct iocblk_in *)iop)->ioc_ifflags;
729 printf("SIOCSIFADDR\n");
730 if (!(us->flags & US_CONTROL)) {
736 us->ifflags |= IFF_RUNNING;
737 ((struct iocblk_in *)iop)->ioc_ifflags |= IFF_RUNNING;
752 if (us->ppa == 0 || us->ppa->lowerq == 0)
754 us->ioc_id = iop->ioc_id;
756 switch (iop->ioc_cmd) {
759 if (us->flags & US_LASTMOD) {
763 putnext(us->ppa->lowerq, mp);
766 if (us->flags & US_PRIV)
767 putnext(us->ppa->lowerq, mp);
769 DPRINT1("ppp ioctl %x rejected\n", iop->ioc_cmd);
778 iop->ioc_error = error;
779 mp->b_datap->db_type = M_IOCNAK;
781 } else if (error == 0) {
782 mp->b_datap->db_type = M_IOCACK;
788 if (us->flags & US_DBGLOG)
789 DPRINT2("ppp/%d: flush %x\n", us->mn, *mp->b_rptr);
790 if (*mp->b_rptr & FLUSHW)
791 flushq(q, FLUSHDATA);
792 if (*mp->b_rptr & FLUSHR) {
793 *mp->b_rptr &= ~FLUSHW;
808 dlpi_request(q, mp, us)
813 union DL_primitives *d = (union DL_primitives *) mp->b_rptr;
814 int size = mp->b_wptr - mp->b_rptr;
816 upperstr_t *ppa, *os;
821 if (us->flags & US_DBGLOG)
822 cmn_err(CE_CONT, "ppp/%d: dlpi prim %x len=%d\n", us->mn,
823 d->dl_primitive, size);
824 switch (d->dl_primitive) {
826 if (size < sizeof(dl_info_req_t))
828 if ((reply = allocb(sizeof(dl_info_ack_t), BPRI_HI)) == 0)
829 break; /* should do bufcall */
830 reply->b_datap->db_type = M_PCPROTO;
831 info = (dl_info_ack_t *) reply->b_wptr;
832 reply->b_wptr += sizeof(dl_info_ack_t);
833 bzero((caddr_t) info, sizeof(dl_info_ack_t));
834 info->dl_primitive = DL_INFO_ACK;
835 info->dl_max_sdu = PPP_MAXMTU;
836 info->dl_min_sdu = 1;
837 info->dl_addr_length = sizeof(ulong);
839 info->dl_mac_type = DL_OTHER;
841 info->dl_mac_type = DL_HDLC; /* a lie */
843 info->dl_current_state = us->state;
844 info->dl_service_mode = DL_CLDLS;
845 info->dl_provider_style = DL_STYLE2;
846 #if DL_CURRENT_VERSION >= 2
847 info->dl_sap_length = sizeof(ulong);
848 info->dl_version = DL_CURRENT_VERSION;
854 if (size < sizeof(dl_attach_req_t))
856 if (us->state != DL_UNATTACHED || us->ppa != 0) {
857 dlpi_error(q, DL_ATTACH_REQ, DL_OUTSTATE, 0);
860 for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
861 if (ppa->ppa_id == d->attach_req.dl_ppa)
864 dlpi_error(q, DL_ATTACH_REQ, DL_BADPPA, 0);
868 qwriter(q, mp, attach_ppa, PERIM_OUTER);
872 if (size < sizeof(dl_detach_req_t))
874 if (us->state != DL_UNBOUND || us->ppa == 0) {
875 dlpi_error(q, DL_DETACH_REQ, DL_OUTSTATE, 0);
878 qwriter(q, mp, detach_ppa, PERIM_OUTER);
882 if (size < sizeof(dl_bind_req_t))
884 if (us->state != DL_UNBOUND || us->ppa == 0) {
885 dlpi_error(q, DL_BIND_REQ, DL_OUTSTATE, 0);
888 if (d->bind_req.dl_service_mode != DL_CLDLS) {
889 dlpi_error(q, DL_BIND_REQ, DL_UNSUPPORTED, 0);
893 /* saps must be valid PPP network protocol numbers,
894 except that we accept ETHERTYPE_IP in place of PPP_IP. */
895 sap = d->bind_req.dl_sap;
897 DPRINT1("ppp bind %x\n", sap);
898 if (sap == ETHERTYPE_IP)
900 if (sap < 0x21 || sap > 0x3fff || (sap & 0x101) != 1) {
901 dlpi_error(q, DL_BIND_REQ, DL_BADADDR, 0);
905 /* check that no other stream is bound to this sap already. */
906 for (os = us->ppa; os != 0; os = os->next)
910 dlpi_error(q, DL_BIND_REQ, DL_NOADDR, 0);
917 if ((reply = allocb(sizeof(dl_bind_ack_t) + sizeof(ulong),
919 break; /* should do bufcall */
920 ackp = (dl_bind_ack_t *) reply->b_wptr;
921 reply->b_wptr += sizeof(dl_bind_ack_t) + sizeof(ulong);
922 reply->b_datap->db_type = M_PCPROTO;
923 bzero((caddr_t) ackp, sizeof(dl_bind_ack_t));
924 ackp->dl_primitive = DL_BIND_ACK;
926 ackp->dl_addr_length = sizeof(ulong);
927 ackp->dl_addr_offset = sizeof(dl_bind_ack_t);
928 *(ulong *)(ackp+1) = sap;
933 if (size < sizeof(dl_unbind_req_t))
935 if (us->state != DL_IDLE) {
936 dlpi_error(q, DL_UNBIND_REQ, DL_OUTSTATE, 0);
940 us->state = DL_UNBOUND;
942 us->ppa->ifstats.ifs_active = 0;
944 dlpi_ok(q, DL_UNBIND_REQ);
947 case DL_UNITDATA_REQ:
948 if (size < sizeof(dl_unitdata_req_t))
950 if (us->state != DL_IDLE) {
951 dlpi_error(q, DL_UNITDATA_REQ, DL_OUTSTATE, 0);
954 if ((ppa = us->ppa) == 0) {
955 cmn_err(CE_CONT, "ppp: in state dl_idle but ppa == 0?\n");
958 len = mp->b_cont == 0? 0: msgdsize(mp->b_cont);
959 if (len > ppa->mtu) {
960 DPRINT2("dlpi data too large (%d > %d)\n", len, ppa->mtu);
963 ppa->last_sent = time;
964 /* this assumes PPP_HDRLEN <= sizeof(dl_unitdata_req_t) */
965 if (mp->b_datap->db_ref > 1) {
966 np = allocb(PPP_HDRLEN, BPRI_HI);
969 np->b_cont = mp->b_cont;
974 mp->b_datap->db_type = M_DATA;
975 /* XXX should use dl_dest_addr_offset/length here,
976 but we would have to translate ETHERTYPE_IP -> PPP_IP */
977 mp->b_wptr = mp->b_rptr + PPP_HDRLEN;
978 mp->b_rptr[0] = PPP_ALLSTATIONS;
979 mp->b_rptr[1] = PPP_UI;
980 mp->b_rptr[2] = us->sap >> 8;
981 mp->b_rptr[3] = us->sap;
982 if (!send_data(mp, us))
986 #if DL_CURRENT_VERSION >= 2
987 case DL_SUBS_BIND_REQ:
988 case DL_SUBS_UNBIND_REQ:
989 case DL_ENABMULTI_REQ:
990 case DL_DISABMULTI_REQ:
991 case DL_PROMISCON_REQ:
992 case DL_PROMISCOFF_REQ:
993 case DL_PHYS_ADDR_REQ:
994 case DL_SET_PHYS_ADDR_REQ:
997 case DL_REPLY_UPDATE_REQ:
999 case DL_DATA_ACK_REQ:
1001 case DL_CONNECT_REQ:
1003 dlpi_error(q, d->dl_primitive, DL_NOTSUPPORTED, 0);
1006 case DL_CONNECT_RES:
1007 case DL_DISCONNECT_REQ:
1010 dlpi_error(q, d->dl_primitive, DL_OUTSTATE, 0);
1014 dlpi_error(q, d->dl_primitive, DL_BADQOSTYPE, 0);
1017 #if DL_CURRENT_VERSION >= 2
1024 cmn_err(CE_CONT, "ppp: unknown dlpi prim 0x%x\n", d->dl_primitive);
1027 dlpi_error(q, d->dl_primitive, DL_BADPRIM, 0);
1034 dlpi_error(q, prim, err, uerr)
1036 int prim, err, uerr;
1039 dl_error_ack_t *errp;
1041 reply = allocb(sizeof(dl_error_ack_t), BPRI_HI);
1043 return; /* XXX should do bufcall */
1044 reply->b_datap->db_type = M_PCPROTO;
1045 errp = (dl_error_ack_t *) reply->b_wptr;
1046 reply->b_wptr += sizeof(dl_error_ack_t);
1047 errp->dl_primitive = DL_ERROR_ACK;
1048 errp->dl_error_primitive = prim;
1049 errp->dl_errno = err;
1050 errp->dl_unix_errno = uerr;
1062 reply = allocb(sizeof(dl_ok_ack_t), BPRI_HI);
1064 return; /* XXX should do bufcall */
1065 reply->b_datap->db_type = M_PCPROTO;
1066 okp = (dl_ok_ack_t *) reply->b_wptr;
1067 reply->b_wptr += sizeof(dl_ok_ack_t);
1068 okp->dl_primitive = DL_OK_ACK;
1069 okp->dl_correct_primitive = prim;
1072 #endif /* NO_DLPI */
1082 if (us->flags & US_BLOCKED || us->npmode == NPMODE_QUEUE)
1085 if (ppa == 0 || us->npmode == NPMODE_DROP || us->npmode == NPMODE_ERROR) {
1089 if ((q = ppa->lowerq) == 0) {
1090 /* try to send it up the control stream */
1091 if (canputnext(ppa->q)) {
1092 putnext(ppa->q, mp);
1096 if (canputnext(ppa->lowerq)) {
1098 * The lower write queue's put procedure just updates counters
1099 * and does a putnext. We call it so that on SMP systems, we
1100 * enter the lower queues' perimeter so that the counter
1101 * updates are serialized.
1103 put(ppa->lowerq, mp);
1107 us->flags |= US_BLOCKED;
1112 * Allocate a new PPA id and link this stream into the list of PPAs.
1113 * This procedure is called with an exclusive lock on all queues in
1121 upperstr_t *us, **usp;
1126 while ((us = *usp) != 0 && ppa_id == us->ppa_id) {
1130 us = (upperstr_t *) q->q_ptr;
1131 us->ppa_id = ppa_id;
1136 us->flags |= US_CONTROL;
1137 us->npmode = NPMODE_PASS;
1144 * Create a kstats record for our statistics, so netstat -i works.
1146 if (us->kstats == 0) {
1149 sprintf(unit, "ppp%d", us->ppa->ppa_id);
1150 us->kstats = kstat_create("ppp", us->ppa->ppa_id, unit,
1151 "net", KSTAT_TYPE_NAMED, 4, 0);
1152 if (us->kstats != 0) {
1153 kstat_named_t *kn = KSTAT_NAMED_PTR(us->kstats);
1155 strcpy(kn[0].name, "ipackets");
1156 kn[0].data_type = KSTAT_DATA_ULONG;
1157 strcpy(kn[1].name, "ierrors");
1158 kn[1].data_type = KSTAT_DATA_ULONG;
1159 strcpy(kn[2].name, "opackets");
1160 kn[2].data_type = KSTAT_DATA_ULONG;
1161 strcpy(kn[3].name, "oerrors");
1162 kn[3].data_type = KSTAT_DATA_ULONG;
1163 kstat_install(us->kstats);
1168 *(int *)mp->b_cont->b_rptr = ppa_id;
1169 mp->b_datap->db_type = M_IOCACK;
1180 us = (upperstr_t *) q->q_ptr;
1182 us->state = DL_UNBOUND;
1184 for (t = us->ppa; t->next != 0; t = t->next)
1188 if (mp->b_datap->db_type == M_IOCTL) {
1189 mp->b_datap->db_type = M_IOCACK;
1193 dlpi_ok(q, DL_ATTACH_REQ);
1205 us = (upperstr_t *) q->q_ptr;
1206 for (t = us->ppa; t->next != 0; t = t->next)
1207 if (t->next == us) {
1214 us->state = DL_UNATTACHED;
1215 dlpi_ok(q, DL_DETACH_REQ);
1224 struct lowerstr *ls;
1228 us = (upperstr_t *) q->q_ptr;
1229 us->flags &= ~US_BLOCKED;
1230 while ((mp = getq(q)) != 0) {
1231 if (!send_data(mp, us)) {
1246 ppa = (upperstr_t *) q->q_ptr;
1247 if (ppa != 0) { /* why wouldn't it? */
1248 ppa->stats.ppp_opackets++;
1249 ppa->stats.ppp_obytes += msgdsize(mp);
1250 #ifdef INCR_OPACKETS
1265 * Flow control has back-enabled this stream:
1266 * enable the write service procedures of all upper
1267 * streams feeding this lower stream.
1269 for (us = (upperstr_t *) q->q_ptr; us != NULL; us = us->next)
1270 if (us->flags & US_BLOCKED)
1279 upperstr_t *us, *as;
1282 dl_unitdata_ind_t *ud;
1286 us = (upperstr_t *) q->q_ptr;
1287 if (us->flags & US_CONTROL) {
1290 * If there is no lower queue attached, run the write service
1291 * routines of other upper streams attached to this PPA.
1293 if (us->lowerq == 0) {
1296 if (as->flags & US_BLOCKED)
1303 * A network protocol stream. Put a DLPI header on each
1304 * packet and send it on.
1305 * (Actually, it seems that the IP module will happily
1306 * accept M_DATA messages without the DL_UNITDATA_IND header.)
1308 while ((mp = getq(q)) != 0) {
1309 if (!canputnext(q)) {
1314 proto = PPP_PROTOCOL(mp->b_rptr);
1315 mp->b_rptr += PPP_HDRLEN;
1316 hdr = allocb(sizeof(dl_unitdata_ind_t) + 2 * sizeof(ulong),
1319 /* XXX should put it back and use bufcall */
1323 hdr->b_datap->db_type = M_PROTO;
1324 ud = (dl_unitdata_ind_t *) hdr->b_wptr;
1325 hdr->b_wptr += sizeof(dl_unitdata_ind_t) + 2 * sizeof(ulong);
1327 ud->dl_primitive = DL_UNITDATA_IND;
1328 ud->dl_dest_addr_length = sizeof(ulong);
1329 ud->dl_dest_addr_offset = sizeof(dl_unitdata_ind_t);
1330 ud->dl_src_addr_length = sizeof(ulong);
1331 ud->dl_src_addr_offset = ud->dl_dest_addr_offset + sizeof(ulong);
1332 #if DL_CURRENT_VERSION >= 2
1333 ud->dl_group_address = 0;
1335 /* Send the DLPI client the data with the SAP they requested,
1336 (e.g. ETHERTYPE_IP) rather than the PPP protocol number
1338 ((ulong *)(ud + 1))[0] = us->req_sap; /* dest SAP */
1339 ((ulong *)(ud + 1))[1] = us->req_sap; /* src SAP */
1343 #endif /* NO_DLPI */
1348 * If this stream is attached to a PPA with a lower queue pair,
1349 * enable the read queue's service routine if it has data queued.
1350 * XXX there is a possibility that packets could get out of order
1351 * if ppplrput now runs before ppplrsrv.
1353 if (us->ppa != 0 && us->ppa->lowerq != 0)
1354 qenable(RD(us->ppa->lowerq));
1360 find_dest(ppa, proto)
1366 for (us = ppa->next; us != 0; us = us->next)
1367 if (proto == us->sap)
1377 upperstr_t *ppa, *us;
1383 ppa = (upperstr_t *) q->q_ptr;
1385 DPRINT1("ppplrput: q = %x, ppa = 0??\n", q);
1389 switch (mp->b_datap->db_type) {
1391 if (*mp->b_rptr & FLUSHW) {
1392 *mp->b_rptr &= ~FLUSHR;
1399 switch (*mp->b_rptr) {
1404 ppa->stats.ppp_ierrors++;
1410 ppa->stats.ppp_oerrors++;
1419 * Attempt to match up the response with the stream
1420 * that the request came from.
1422 iop = (struct iocblk *) mp->b_rptr;
1423 for (us = ppa; us != 0; us = us->next)
1424 if (us->ioc_id == iop->ioc_id)
1433 if (mp->b_datap->db_type == M_DATA) {
1435 if (mp->b_wptr - mp->b_rptr < PPP_HDRLEN) {
1436 PULLUP(mp, PPP_HDRLEN);
1438 DPRINT1("ppp_lrput: msgpullup failed (len=%d)\n", len);
1442 ppa->stats.ppp_ipackets++;
1443 ppa->stats.ppp_ibytes += len;
1444 #ifdef INCR_IPACKETS
1447 proto = PPP_PROTOCOL(mp->b_rptr);
1448 if (proto < 0x8000 && (us = find_dest(ppa, proto)) != 0) {
1450 * A data packet for some network protocol.
1451 * Queue it on the upper stream for that protocol.
1457 ppa->last_recv = time;
1462 * A control frame, a frame for an unknown protocol,
1463 * or some other message type.
1464 * Send it up to pppd via the control stream.
1466 if (queclass(mp) == QPCTL || canputnext(ppa->q))
1467 putnext(ppa->q, mp);
1481 upperstr_t *ppa, *us;
1485 * Packets only get queued here for flow control reasons.
1487 ppa = (upperstr_t *) q->q_ptr;
1488 while ((mp = getq(q)) != 0) {
1489 if (mp->b_datap->db_type == M_DATA
1490 && (proto = PPP_PROTOCOL(mp->b_rptr)) < 0x8000
1491 && (us = find_dest(ppa, proto)) != 0) {
1499 if (canputnext(ppa->q))
1500 putnext(ppa->q, mp);
1511 putctl2(q, type, code, val)
1513 int type, code, val;
1517 mp = allocb(2, BPRI_HI);
1520 mp->b_datap->db_type = type;
1521 mp->b_wptr[0] = code;
1522 mp->b_wptr[1] = val;
1529 putctl4(q, type, code, val)
1531 int type, code, val;
1535 mp = allocb(4, BPRI_HI);
1538 mp->b_datap->db_type = type;
1539 mp->b_wptr[0] = code;
1540 ((short *)mp->b_wptr)[1] = val;
1548 queue_t *q; /* not used */
1549 mblk_t *mp; /* not used either */
1554 DPRINT("ppp upper streams:\n");
1555 for (us = minor_devs; us != 0; us = us->nextmn) {
1557 DPRINT3(" %d: q=%x rlev=%d",
1558 us->mn, uq, (uq? qsize(uq): 0));
1559 DPRINT3(" wlev=%d flags=0x%b", (uq? qsize(WR(uq)): 0),
1560 us->flags, "\020\1priv\2control\3blocked\4last");
1561 DPRINT3(" state=%x sap=%x req_sap=%x", us->state, us->sap,
1566 DPRINT1(" ppa=%d\n", us->ppa->ppa_id);
1567 if (us->flags & US_CONTROL) {
1569 DPRINT3(" control for %d lq=%x rlev=%d",
1570 us->ppa_id, lq, (lq? qsize(RD(lq)): 0));
1571 DPRINT3(" wlev=%d mru=%d mtu=%d\n",
1572 (lq? qsize(lq): 0), us->mru, us->mtu);