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 HAVE 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.2 1995/12/18 03:57:27 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 */
97 * There is exactly one control stream for each PPA.
98 * The following fields are only used for control streams.
101 queue_t *lowerq; /* write queue attached below this PPA */
102 struct upperstr *nextppa; /* next control stream */
105 struct pppstat stats; /* statistics */
106 time_t last_sent; /* time last NP packet sent */
107 time_t last_recv; /* time last NP packet rcvd */
109 kstat_t *kstats; /* stats for netstat */
113 char ifname[IFNAMSIZ];
114 struct ifstats ifstats;
118 /* Values for flags */
119 #define US_PRIV 1 /* stream was opened by superuser */
120 #define US_CONTROL 2 /* stream is a control stream */
121 #define US_BLOCKED 4 /* flow ctrl has blocked lower write stream */
122 #define US_LASTMOD 8 /* no PPP modules below us */
123 #define US_DBGLOG 0x10 /* log various occurrences */
125 static upperstr_t *minor_devs = NULL;
126 static upperstr_t *ppas = NULL;
129 static int pppopen __P((queue_t *, dev_t *, int, int, cred_t *));
130 static int pppclose __P((queue_t *, int, cred_t *));
132 static int pppopen __P((queue_t *, int, int, int));
133 static int pppclose __P((queue_t *, int));
135 static int pppuwput __P((queue_t *, mblk_t *));
136 static int pppursrv __P((queue_t *));
137 static int pppuwsrv __P((queue_t *));
138 static int ppplrput __P((queue_t *, mblk_t *));
139 static int ppplwput __P((queue_t *, mblk_t *));
140 static int ppplrsrv __P((queue_t *));
141 static int ppplwsrv __P((queue_t *));
143 static void dlpi_request __P((queue_t *, mblk_t *, upperstr_t *));
144 static void dlpi_error __P((queue_t *, int, int, int));
145 static void dlpi_ok __P((queue_t *, int));
147 static int send_data __P((mblk_t *, upperstr_t *));
148 static void new_ppa __P((queue_t *, mblk_t *));
149 static void attach_ppa __P((queue_t *, mblk_t *));
150 static void detach_ppa __P((queue_t *, mblk_t *));
151 static void debug_dump __P((queue_t *, mblk_t *));
152 static upperstr_t *find_dest __P((upperstr_t *, int));
153 static int putctl2 __P((queue_t *, int, int, int));
154 static int putctl4 __P((queue_t *, int, int, int));
156 static struct module_info ppp_info = {
157 0xb1a6, "ppp", 0, 512, 512, 128
160 static struct qinit pppurint = {
161 NULL, pppursrv, pppopen, pppclose, NULL, &ppp_info, NULL
164 static struct qinit pppuwint = {
165 pppuwput, pppuwsrv, NULL, NULL, NULL, &ppp_info, NULL
168 static struct qinit ppplrint = {
169 ppplrput, ppplrsrv, NULL, NULL, NULL, &ppp_info, NULL
172 static struct qinit ppplwint = {
173 ppplwput, ppplwsrv, NULL, NULL, NULL, &ppp_info, NULL
177 extern struct ifstats *ifstats;
181 struct streamtab pppinfo = {
182 &pppurint, &pppuwint,
189 * How we maintain statistics.
192 #define INCR_IPACKETS(ppa) \
193 if (ppa->kstats != 0) { \
194 KSTAT_NAMED_PTR(ppa->kstats)[0].value.ul++; \
196 #define INCR_IERRORS(ppa) \
197 if (ppa->kstats != 0) { \
198 KSTAT_NAMED_PTR(ppa->kstats)[1].value.ul++; \
200 #define INCR_OPACKETS(ppa) \
201 if (ppa->kstats != 0) { \
202 KSTAT_NAMED_PTR(ppa->kstats)[2].value.ul++; \
204 #define INCR_OERRORS(ppa) \
205 if (ppa->kstats != 0) { \
206 KSTAT_NAMED_PTR(ppa->kstats)[3].value.ul++; \
211 #define INCR_IPACKETS(ppa) ppa->ifstats.ifs_ipackets++;
212 #define INCR_IERRORS(ppa) ppa->ifstats.ifs_ierrors++;
213 #define INCR_OPACKETS(ppa) ppa->ifstats.ifs_opackets++;
214 #define INCR_OERRORS(ppa) ppa->ifstats.ifs_oerrors++;
218 * STREAMS driver entry points.
222 pppopen(q, devp, oflag, sflag, credp)
228 pppopen(q, dev, oflag, sflag)
230 int dev; /* really dev_t */
239 DRV_OPEN_OK(dev); /* device is already open */
241 if (sflag == CLONEOPEN) {
243 for (prevp = &minor_devs; (up = *prevp) != 0; prevp = &up->nextmn) {
250 mn = getminor(*devp);
254 for (prevp = &minor_devs; (up = *prevp) != 0; prevp = &up->nextmn) {
259 /* this can't happen */
260 q->q_ptr = WR(q)->q_ptr = (caddr_t) up;
266 * Construct a new minor node.
268 up = (upperstr_t *) ALLOC_SLEEP(sizeof(upperstr_t));
269 bzero((caddr_t) up, sizeof(upperstr_t));
271 DPRINT("pppopen: out of kernel memory\n");
278 *devp = makedevice(getmajor(*devp), mn);
282 up->flags |= US_PRIV;
284 up->state = DL_UNATTACHED;
287 up->ifflags = IFF_UP | IFF_POINTOPOINT;
290 up->last_sent = up->last_recv = time;
291 q->q_ptr = (caddr_t) up;
292 WR(q)->q_ptr = (caddr_t) up;
297 DRV_OPEN_OK(makedev(major(dev), mn));
302 pppclose(q, flag, credp)
312 upperstr_t *up, **upp;
313 upperstr_t *as, *asnext;
318 up = (upperstr_t *) q->q_ptr;
319 if (up->flags & US_DBGLOG)
320 DPRINT2("ppp/%d: close, flags=%x\n", up->mn, up->flags);
323 if (up->flags & US_CONTROL) {
325 struct ifstats *ifp, *pifp;
328 * This stream represents a PPA:
329 * For all streams attached to the PPA, clear their
330 * references to this PPA.
331 * Then remove this PPA from the list of PPAs.
333 for (as = up->next; as != 0; as = asnext) {
337 if (as->flags & US_BLOCKED) {
338 as->flags &= ~US_BLOCKED;
339 flushq(WR(as->q), FLUSHDATA);
342 for (upp = &ppas; *upp != 0; upp = &(*upp)->nextppa)
348 /* Remove the statistics from the active list. */
349 for (ifp = ifstats, pifp = 0; ifp; ifp = ifp->ifs_next) {
350 if (ifp == &up->ifstats) {
352 pifp->ifs_next = ifp->ifs_next;
354 ifstats = ifp->ifs_next;
362 * If this stream is attached to a PPA,
363 * remove it from the PPA's list.
365 if ((as = up->ppa) != 0) {
366 for (; as->next != 0; as = as->next)
367 if (as->next == up) {
376 kstat_delete(up->kstats);
382 for (prevp = &minor_devs; *prevp != 0; prevp = &(*prevp)->nextmn) {
388 kmem_free(up, sizeof(upperstr_t));
395 * A message from on high. We do one of three things:
397 * - put the message on the lower write stream
398 * - queue it for our service routine
405 upperstr_t *us, *usnext, *ppa, *os;
415 struct ppp_idle *pip;
417 us = (upperstr_t *) q->q_ptr;
418 switch (mp->b_datap->db_type) {
422 dlpi_request(q, mp, us);
427 if (us->flags & US_DBGLOG)
428 DPRINT3("ppp/%d: uwput M_DATA len=%d flags=%x\n",
429 us->mn, msgdsize(mp), us->flags);
430 if (us->ppa == 0 || msgdsize(mp) > us->ppa->mtu + PPP_HDRLEN
432 || (us->flags & US_CONTROL) == 0
435 DPRINT1("pppuwput: junk data len=%d\n", msgdsize(mp));
440 if ((us->flags & US_CONTROL) == 0)
441 us->ppa->last_sent = time;
443 if (!send_data(mp, us))
448 iop = (struct iocblk *) mp->b_rptr;
450 if (us->flags & US_DBGLOG)
451 DPRINT3("ppp/%d: ioctl %x count=%d\n",
452 us->mn, iop->ioc_cmd, iop->ioc_count);
453 switch (iop->ioc_cmd) {
455 if ((us->flags & US_CONTROL) == 0 || us->lowerq != 0)
457 lb = (struct linkblk *) mp->b_cont->b_rptr;
458 us->lowerq = lq = lb->l_qbot;
459 lq->q_ptr = (caddr_t) us;
460 RD(lq)->q_ptr = (caddr_t) us;
462 flushq(RD(lq), FLUSHALL);
465 us->flags &= ~US_LASTMOD;
466 /* Unblock upper streams which now feed this lower stream. */
468 /* Send useful information down to the modules which
469 are now linked below us. */
470 putctl2(lq, M_CTL, PPPCTL_UNIT, us->ppa_id);
471 putctl4(lq, M_CTL, PPPCTL_MRU, us->mru);
472 putctl4(lq, M_CTL, PPPCTL_MTU, us->mtu);
476 lb = (struct linkblk *) mp->b_cont->b_rptr;
478 if (us->lowerq != lb->l_qbot)
479 DPRINT2("ppp unlink: lowerq=%x qbot=%x\n",
480 us->lowerq, lb->l_qbot);
485 /* Unblock streams which now feed back up the control stream. */
490 if (us->flags & US_CONTROL)
492 if ((us->flags & US_PRIV) == 0) {
496 /* Arrange to return an int */
497 if ((mq = mp->b_cont) == 0
498 || mq->b_datap->db_lim - mq->b_rptr < sizeof(int)) {
499 mq = allocb(sizeof(int), BPRI_HI);
509 iop->ioc_count = sizeof(int);
510 mq->b_wptr = mq->b_rptr + sizeof(int);
511 qwriter(q, mp, new_ppa, PERIM_OUTER);
516 /* like dlpi_attach, for programs which can't write to
517 the stream (like pppstats) */
518 if (iop->ioc_count != sizeof(int) || us->ppa != 0)
520 n = *(int *)mp->b_cont->b_rptr;
521 for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
522 if (ppa->ppa_id == n)
528 qwriter(q, mp, attach_ppa, PERIM_OUTER);
534 /* Attach to a given SAP. */
535 if (iop->ioc_count != sizeof(int) || us->ppa == 0)
537 n = *(int *)mp->b_cont->b_rptr;
538 /* n must be a valid PPP network protocol number. */
539 if (n < 0x21 || n > 0x3fff || (n & 0x101) != 1)
541 /* check that no other stream is bound to this sap already. */
542 for (os = us->ppa; os != 0; os = os->next)
554 if (iop->ioc_count != sizeof(int) || (us->flags & US_CONTROL) == 0)
556 n = *(int *)mp->b_cont->b_rptr;
557 if (n <= 0 || n > PPP_MAXMTU)
563 putctl4(us->lowerq, M_CTL, PPPCTL_MRU, n);
569 if (iop->ioc_count != sizeof(int) || (us->flags & US_CONTROL) == 0)
571 n = *(int *)mp->b_cont->b_rptr;
572 if (n <= 0 || n > PPP_MAXMTU)
578 us->ifstats.ifs_mtu = n;
581 putctl4(us->lowerq, M_CTL, PPPCTL_MTU, n);
587 us->flags |= US_LASTMOD;
592 if (iop->ioc_count != sizeof(int))
594 n = *(int *)mp->b_cont->b_rptr;
595 if (n == PPPDBG_DUMP + PPPDBG_DRIVER) {
596 qwriter(q, NULL, debug_dump, PERIM_OUTER);
599 } else if (n == PPPDBG_LOG + PPPDBG_DRIVER) {
600 DPRINT1("ppp/%d: debug log enabled\n", us->mn);
601 us->flags |= US_DBGLOG;
605 if (us->ppa == 0 || us->ppa->lowerq == 0)
607 putnext(us->ppa->lowerq, mp);
613 if ((ppa = us->ppa) == 0)
615 mq = allocb(sizeof(struct ppp_idle), BPRI_HI);
624 pip = (struct ppp_idle *) mq->b_wptr;
625 pip->xmit_idle = time - ppa->last_sent;
626 pip->recv_idle = time - ppa->last_recv;
627 mq->b_wptr += sizeof(struct ppp_idle);
628 iop->ioc_count = sizeof(struct ppp_idle);
634 printf("SIOCSIFNAME\n");
635 /* Sent from IP down to us. Attach the ifstats structure. */
636 if (iop->ioc_count != sizeof(struct ifreq) || us->ppa == 0)
638 ifr = (struct ifreq *)mp->b_cont->b_rptr;
639 /* Find the unit number in the interface name. */
640 for (i = 0; i < IFNAMSIZ; i++) {
641 if (ifr->ifr_name[i] == 0 ||
642 (ifr->ifr_name[i] >= '0' &&
643 ifr->ifr_name[i] <= '9'))
646 us->ifname[i] = ifr->ifr_name[i];
650 /* Convert the unit number to binary. */
651 for (n = 0; i < IFNAMSIZ; i++) {
652 if (ifr->ifr_name[i] == 0) {
656 n = n * 10 + ifr->ifr_name[i] - '0';
660 /* Verify the ppa. */
661 if (us->ppa->ppa_id != n)
665 /* Set up the netstat block. */
666 strncpy (ppa->ifname, us->ifname, IFNAMSIZ);
668 ppa->ifstats.ifs_name = ppa->ifname;
669 ppa->ifstats.ifs_unit = n;
670 ppa->ifstats.ifs_active = us->state != DL_UNBOUND;
671 ppa->ifstats.ifs_mtu = ppa->mtu;
673 /* Link in statistics used by netstat. */
674 ppa->ifstats.ifs_next = ifstats;
675 ifstats = &ppa->ifstats;
682 printf("SIOCGIFFLAGS\n");
683 if (!(us->flags & US_CONTROL)) {
689 ((struct iocblk_in *)iop)->ioc_ifflags = us->ifflags;
694 printf("SIOCSIFFLAGS\n");
695 if (!(us->flags & US_CONTROL)) {
701 us->ifflags = ((struct iocblk_in *)iop)->ioc_ifflags;
706 printf("SIOCSIFADDR\n");
707 if (!(us->flags & US_CONTROL)) {
713 us->ifflags |= IFF_RUNNING;
714 ((struct iocblk_in *)iop)->ioc_ifflags |= IFF_RUNNING;
729 if (us->ppa == 0 || us->ppa->lowerq == 0)
731 us->ioc_id = iop->ioc_id;
733 switch (iop->ioc_cmd) {
736 if (us->flags & US_LASTMOD) {
740 putnext(us->ppa->lowerq, mp);
743 if (us->flags & US_PRIV)
744 putnext(us->ppa->lowerq, mp);
746 DPRINT1("ppp ioctl %x rejected\n", iop->ioc_cmd);
755 iop->ioc_error = error;
756 mp->b_datap->db_type = M_IOCNAK;
758 } else if (error == 0) {
759 mp->b_datap->db_type = M_IOCACK;
765 if (us->flags & US_DBGLOG)
766 DPRINT2("ppp/%d: flush %x\n", us->mn, *mp->b_rptr);
767 if (*mp->b_rptr & FLUSHW)
768 flushq(q, FLUSHDATA);
769 if (*mp->b_rptr & FLUSHR) {
770 *mp->b_rptr &= ~FLUSHW;
785 dlpi_request(q, mp, us)
790 union DL_primitives *d = (union DL_primitives *) mp->b_rptr;
791 int size = mp->b_wptr - mp->b_rptr;
793 upperstr_t *ppa, *os;
798 if (us->flags & US_DBGLOG)
799 cmn_err(CE_CONT, "ppp/%d: dlpi prim %x len=%d\n", us->mn,
800 d->dl_primitive, size);
801 switch (d->dl_primitive) {
803 if (size < sizeof(dl_info_req_t))
805 if ((reply = allocb(sizeof(dl_info_ack_t), BPRI_HI)) == 0)
806 break; /* should do bufcall */
807 reply->b_datap->db_type = M_PCPROTO;
808 info = (dl_info_ack_t *) reply->b_wptr;
809 reply->b_wptr += sizeof(dl_info_ack_t);
810 bzero((caddr_t) info, sizeof(dl_info_ack_t));
811 info->dl_primitive = DL_INFO_ACK;
812 info->dl_max_sdu = PPP_MAXMTU;
813 info->dl_min_sdu = 1;
814 info->dl_addr_length = sizeof(ulong);
816 info->dl_mac_type = DL_OTHER;
818 info->dl_mac_type = DL_HDLC; /* a lie */
820 info->dl_current_state = us->state;
821 info->dl_service_mode = DL_CLDLS;
822 info->dl_provider_style = DL_STYLE2;
823 #if DL_CURRENT_VERSION >= 2
824 info->dl_sap_length = sizeof(ulong);
825 info->dl_version = DL_CURRENT_VERSION;
831 if (size < sizeof(dl_attach_req_t))
833 if (us->state != DL_UNATTACHED || us->ppa != 0) {
834 dlpi_error(q, DL_ATTACH_REQ, DL_OUTSTATE, 0);
837 for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
838 if (ppa->ppa_id == d->attach_req.dl_ppa)
841 dlpi_error(q, DL_ATTACH_REQ, DL_BADPPA, 0);
845 qwriter(q, mp, attach_ppa, PERIM_OUTER);
849 if (size < sizeof(dl_detach_req_t))
851 if (us->state != DL_UNBOUND || us->ppa == 0) {
852 dlpi_error(q, DL_DETACH_REQ, DL_OUTSTATE, 0);
855 qwriter(q, mp, detach_ppa, PERIM_OUTER);
859 if (size < sizeof(dl_bind_req_t))
861 if (us->state != DL_UNBOUND || us->ppa == 0) {
862 dlpi_error(q, DL_BIND_REQ, DL_OUTSTATE, 0);
865 if (d->bind_req.dl_service_mode != DL_CLDLS) {
866 dlpi_error(q, DL_BIND_REQ, DL_UNSUPPORTED, 0);
870 /* saps must be valid PPP network protocol numbers,
871 except that we accept ETHERTYPE_IP in place of PPP_IP. */
872 sap = d->bind_req.dl_sap;
874 DPRINT1("ppp bind %x\n", sap);
875 if (sap == ETHERTYPE_IP)
877 if (sap < 0x21 || sap > 0x3fff || (sap & 0x101) != 1) {
878 dlpi_error(q, DL_BIND_REQ, DL_BADADDR, 0);
882 /* check that no other stream is bound to this sap already. */
883 for (os = us->ppa; os != 0; os = os->next)
887 dlpi_error(q, DL_BIND_REQ, DL_NOADDR, 0);
894 if ((reply = allocb(sizeof(dl_bind_ack_t) + sizeof(ulong),
896 break; /* should do bufcall */
897 ackp = (dl_bind_ack_t *) reply->b_wptr;
898 reply->b_wptr += sizeof(dl_bind_ack_t) + sizeof(ulong);
899 reply->b_datap->db_type = M_PCPROTO;
900 bzero((caddr_t) ackp, sizeof(dl_bind_ack_t));
901 ackp->dl_primitive = DL_BIND_ACK;
903 ackp->dl_addr_length = sizeof(ulong);
904 ackp->dl_addr_offset = sizeof(dl_bind_ack_t);
905 *(ulong *)(ackp+1) = sap;
910 if (size < sizeof(dl_unbind_req_t))
912 if (us->state != DL_IDLE) {
913 dlpi_error(q, DL_UNBIND_REQ, DL_OUTSTATE, 0);
917 us->state = DL_UNBOUND;
919 us->ppa->ifstats.ifs_active = 0;
921 dlpi_ok(q, DL_UNBIND_REQ);
924 case DL_UNITDATA_REQ:
925 if (size < sizeof(dl_unitdata_req_t))
927 if (us->state != DL_IDLE) {
928 dlpi_error(q, DL_UNITDATA_REQ, DL_OUTSTATE, 0);
931 if ((ppa = us->ppa) == 0) {
932 cmn_err(CE_CONT, "ppp: in state dl_idle but ppa == 0?\n");
935 len = mp->b_cont == 0? 0: msgdsize(mp->b_cont);
936 if (len > ppa->mtu) {
937 DPRINT2("dlpi data too large (%d > %d)\n", len, ppa->mtu);
940 ppa->last_sent = time;
941 /* this assumes PPP_HDRLEN <= sizeof(dl_unitdata_req_t) */
942 if (mp->b_datap->db_ref > 1) {
943 np = allocb(PPP_HDRLEN, BPRI_HI);
946 np->b_cont = mp->b_cont;
951 mp->b_datap->db_type = M_DATA;
952 /* XXX should use dl_dest_addr_offset/length here,
953 but we would have to translate ETHERTYPE_IP -> PPP_IP */
954 mp->b_wptr = mp->b_rptr + PPP_HDRLEN;
955 mp->b_rptr[0] = PPP_ALLSTATIONS;
956 mp->b_rptr[1] = PPP_UI;
957 mp->b_rptr[2] = us->sap >> 8;
958 mp->b_rptr[3] = us->sap;
959 if (!send_data(mp, us))
963 #if DL_CURRENT_VERSION >= 2
964 case DL_SUBS_BIND_REQ:
965 case DL_SUBS_UNBIND_REQ:
966 case DL_ENABMULTI_REQ:
967 case DL_DISABMULTI_REQ:
968 case DL_PROMISCON_REQ:
969 case DL_PROMISCOFF_REQ:
970 case DL_PHYS_ADDR_REQ:
971 case DL_SET_PHYS_ADDR_REQ:
974 case DL_REPLY_UPDATE_REQ:
976 case DL_DATA_ACK_REQ:
980 dlpi_error(q, d->dl_primitive, DL_NOTSUPPORTED, 0);
984 case DL_DISCONNECT_REQ:
987 dlpi_error(q, d->dl_primitive, DL_OUTSTATE, 0);
991 dlpi_error(q, d->dl_primitive, DL_BADQOSTYPE, 0);
994 #if DL_CURRENT_VERSION >= 2
1001 cmn_err(CE_CONT, "ppp: unknown dlpi prim 0x%x\n", d->dl_primitive);
1004 dlpi_error(q, d->dl_primitive, DL_BADPRIM, 0);
1011 dlpi_error(q, prim, err, uerr)
1013 int prim, err, uerr;
1016 dl_error_ack_t *errp;
1018 reply = allocb(sizeof(dl_error_ack_t), BPRI_HI);
1020 return; /* XXX should do bufcall */
1021 reply->b_datap->db_type = M_PCPROTO;
1022 errp = (dl_error_ack_t *) reply->b_wptr;
1023 reply->b_wptr += sizeof(dl_error_ack_t);
1024 errp->dl_primitive = DL_ERROR_ACK;
1025 errp->dl_error_primitive = prim;
1026 errp->dl_errno = err;
1027 errp->dl_unix_errno = uerr;
1039 reply = allocb(sizeof(dl_ok_ack_t), BPRI_HI);
1041 return; /* XXX should do bufcall */
1042 reply->b_datap->db_type = M_PCPROTO;
1043 okp = (dl_ok_ack_t *) reply->b_wptr;
1044 reply->b_wptr += sizeof(dl_ok_ack_t);
1045 okp->dl_primitive = DL_OK_ACK;
1046 okp->dl_correct_primitive = prim;
1049 #endif /* NO_DLPI */
1059 if (us->flags & US_BLOCKED)
1066 if ((q = ppa->lowerq) == 0) {
1067 /* try to send it up the control stream */
1068 if (canputnext(ppa->q)) {
1069 putnext(ppa->q, mp);
1073 if (canputnext(ppa->lowerq)) {
1075 * The lower write queue's put procedure just updates counters
1076 * and does a putnext. We call it so that on SMP systems, we
1077 * enter the lower queues' perimeter so that the counter
1078 * updates are serialized.
1080 put(ppa->lowerq, mp);
1084 us->flags |= US_BLOCKED;
1089 * Allocate a new PPA id and link this stream into the list of PPAs.
1090 * This procedure is called with an exclusive lock on all queues in
1098 upperstr_t *us, **usp;
1103 while ((us = *usp) != 0 && ppa_id == us->ppa_id) {
1107 us = (upperstr_t *) q->q_ptr;
1108 us->ppa_id = ppa_id;
1113 us->flags |= US_CONTROL;
1120 * Create a kstats record for our statistics, so netstat -i works.
1122 if (us->kstats == 0) {
1125 sprintf(unit, "ppp%d", us->ppa->ppa_id);
1126 us->kstats = kstat_create("ppp", us->ppa->ppa_id, unit,
1127 "net", KSTAT_TYPE_NAMED, 4, 0);
1128 if (us->kstats != 0) {
1129 kstat_named_t *kn = KSTAT_NAMED_PTR(us->kstats);
1131 strcpy(kn[0].name, "ipackets");
1132 kn[0].data_type = KSTAT_DATA_ULONG;
1133 strcpy(kn[1].name, "ierrors");
1134 kn[1].data_type = KSTAT_DATA_ULONG;
1135 strcpy(kn[2].name, "opackets");
1136 kn[2].data_type = KSTAT_DATA_ULONG;
1137 strcpy(kn[3].name, "oerrors");
1138 kn[3].data_type = KSTAT_DATA_ULONG;
1139 kstat_install(us->kstats);
1144 *(int *)mp->b_cont->b_rptr = ppa_id;
1145 mp->b_datap->db_type = M_IOCACK;
1156 us = (upperstr_t *) q->q_ptr;
1158 us->state = DL_UNBOUND;
1160 for (t = us->ppa; t->next != 0; t = t->next)
1164 if (mp->b_datap->db_type == M_IOCTL) {
1165 mp->b_datap->db_type = M_IOCACK;
1169 dlpi_ok(q, DL_ATTACH_REQ);
1181 us = (upperstr_t *) q->q_ptr;
1182 for (t = us->ppa; t->next != 0; t = t->next)
1183 if (t->next == us) {
1190 us->state = DL_UNATTACHED;
1191 dlpi_ok(q, DL_DETACH_REQ);
1200 struct lowerstr *ls;
1204 us = (upperstr_t *) q->q_ptr;
1205 us->flags &= ~US_BLOCKED;
1206 while ((mp = getq(q)) != 0) {
1207 if (!send_data(mp, us)) {
1222 ppa = (upperstr_t *) q->q_ptr;
1223 if (ppa != 0) { /* why wouldn't it? */
1224 ppa->stats.ppp_opackets++;
1225 ppa->stats.ppp_obytes += msgdsize(mp);
1226 #ifdef INCR_OPACKETS
1241 * Flow control has back-enabled this stream:
1242 * enable the write service procedures of all upper
1243 * streams feeding this lower stream.
1245 for (us = (upperstr_t *) q->q_ptr; us != NULL; us = us->next)
1246 if (us->flags & US_BLOCKED)
1255 upperstr_t *us, *as;
1258 dl_unitdata_ind_t *ud;
1262 us = (upperstr_t *) q->q_ptr;
1263 if (us->flags & US_CONTROL) {
1266 * If there is no lower queue attached, run the write service
1267 * routines of other upper streams attached to this PPA.
1269 if (us->lowerq == 0) {
1272 if (as->flags & US_BLOCKED)
1279 * A network protocol stream. Put a DLPI header on each
1280 * packet and send it on.
1281 * (Actually, it seems that the IP module will happily
1282 * accept M_DATA messages without the DL_UNITDATA_IND header.)
1284 while ((mp = getq(q)) != 0) {
1285 if (!canputnext(q)) {
1290 proto = PPP_PROTOCOL(mp->b_rptr);
1291 mp->b_rptr += PPP_HDRLEN;
1292 hdr = allocb(sizeof(dl_unitdata_ind_t) + 2 * sizeof(ulong),
1295 /* XXX should put it back and use bufcall */
1299 hdr->b_datap->db_type = M_PROTO;
1300 ud = (dl_unitdata_ind_t *) hdr->b_wptr;
1301 hdr->b_wptr += sizeof(dl_unitdata_ind_t) + 2 * sizeof(ulong);
1303 ud->dl_primitive = DL_UNITDATA_IND;
1304 ud->dl_dest_addr_length = sizeof(ulong);
1305 ud->dl_dest_addr_offset = sizeof(dl_unitdata_ind_t);
1306 ud->dl_src_addr_length = sizeof(ulong);
1307 ud->dl_src_addr_offset = ud->dl_dest_addr_offset + sizeof(ulong);
1308 #if DL_CURRENT_VERSION >= 2
1309 ud->dl_group_address = 0;
1311 /* Send the DLPI client the data with the SAP they requested,
1312 (e.g. ETHERTYPE_IP) rather than the PPP protocol number
1314 ((ulong *)(ud + 1))[0] = us->req_sap; /* dest SAP */
1315 ((ulong *)(ud + 1))[1] = us->req_sap; /* src SAP */
1319 #endif /* NO_DLPI */
1324 * If this stream is attached to a PPA with a lower queue pair,
1325 * enable the read queue's service routine if it has data queued.
1326 * XXX there is a possibility that packets could get out of order
1327 * if ppplrput now runs before ppplrsrv.
1329 if (us->ppa != 0 && us->ppa->lowerq != 0)
1330 qenable(RD(us->ppa->lowerq));
1336 find_dest(ppa, proto)
1342 for (us = ppa->next; us != 0; us = us->next)
1343 if (proto == us->sap)
1353 upperstr_t *ppa, *us;
1359 ppa = (upperstr_t *) q->q_ptr;
1361 DPRINT1("ppplrput: q = %x, ppa = 0??\n", q);
1365 switch (mp->b_datap->db_type) {
1367 if (*mp->b_rptr & FLUSHW) {
1368 *mp->b_rptr &= ~FLUSHR;
1375 switch (*mp->b_rptr) {
1380 ppa->stats.ppp_ierrors++;
1386 ppa->stats.ppp_oerrors++;
1395 * Attempt to match up the response with the stream
1396 * that the request came from.
1398 iop = (struct iocblk *) mp->b_rptr;
1399 for (us = ppa; us != 0; us = us->next)
1400 if (us->ioc_id == iop->ioc_id)
1409 if (mp->b_datap->db_type == M_DATA) {
1411 if (mp->b_wptr - mp->b_rptr < PPP_HDRLEN) {
1412 PULLUP(mp, PPP_HDRLEN);
1414 DPRINT1("ppp_lrput: msgpullup failed (len=%d)\n", len);
1418 ppa->stats.ppp_ipackets++;
1419 ppa->stats.ppp_ibytes += len;
1420 #ifdef INCR_IPACKETS
1423 proto = PPP_PROTOCOL(mp->b_rptr);
1424 if (proto < 0x8000 && (us = find_dest(ppa, proto)) != 0) {
1426 * A data packet for some network protocol.
1427 * Queue it on the upper stream for that protocol.
1433 ppa->last_recv = time;
1438 * A control frame, a frame for an unknown protocol,
1439 * or some other message type.
1440 * Send it up to pppd via the control stream.
1442 if (queclass(mp) == QPCTL || canputnext(ppa->q))
1443 putnext(ppa->q, mp);
1457 upperstr_t *ppa, *us;
1461 * Packets only get queued here for flow control reasons.
1463 ppa = (upperstr_t *) q->q_ptr;
1464 while ((mp = getq(q)) != 0) {
1465 if (mp->b_datap->db_type == M_DATA
1466 && (proto = PPP_PROTOCOL(mp->b_rptr)) < 0x8000
1467 && (us = find_dest(ppa, proto)) != 0) {
1475 if (canputnext(ppa->q))
1476 putnext(ppa->q, mp);
1487 putctl2(q, type, code, val)
1489 int type, code, val;
1493 mp = allocb(2, BPRI_HI);
1496 mp->b_datap->db_type = type;
1497 mp->b_wptr[0] = code;
1498 mp->b_wptr[1] = val;
1505 putctl4(q, type, code, val)
1507 int type, code, val;
1511 mp = allocb(4, BPRI_HI);
1514 mp->b_datap->db_type = type;
1515 mp->b_wptr[0] = code;
1516 ((short *)mp->b_wptr)[1] = val;
1524 queue_t *q; /* not used */
1525 mblk_t *mp; /* not used either */
1530 DPRINT("ppp upper streams:\n");
1531 for (us = minor_devs; us != 0; us = us->nextmn) {
1533 DPRINT3(" %d: q=%x rlev=%d",
1534 us->mn, uq, (uq? qsize(uq): 0));
1535 DPRINT3(" wlev=%d flags=0x%b", (uq? qsize(WR(uq)): 0),
1536 us->flags, "\020\1priv\2control\3blocked\4last");
1537 DPRINT3(" state=%x sap=%x req_sap=%x", us->state, us->sap,
1542 DPRINT1(" ppa=%d\n", us->ppa->ppa_id);
1543 if (us->flags & US_CONTROL) {
1545 DPRINT3(" control for %d lq=%x rlev=%d",
1546 us->ppa_id, lq, (lq? qsize(RD(lq)): 0));
1547 DPRINT3(" wlev=%d mru=%d mtu=%d\n",
1548 (lq? qsize(lq): 0), us->mru, us->mtu);