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.6 1996/06/26 00:53:38 paulus Exp $
31 * This file is used under Solaris 2, SVR4, SunOS 4, and Digital UNIX.
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>
41 #include <sys/ioctl.h>
42 #include <sys/cmn_err.h>
43 #define queclass(mp) ((mp)->b_band & QPCTL)
45 #include <sys/ioccom.h>
49 #include <sys/cmn_err.h>
54 #include <sys/kstat.h>
55 #include <sys/sunddi.h>
57 #include <sys/socket.h>
58 #include <sys/sockio.h>
60 #include <netinet/in.h>
65 #include <net/ppp_defs.h>
66 #include <net/pppio.h>
76 * The IP module may use this SAP value for IP packets.
79 #define ETHERTYPE_IP 0x800
83 #define PPP_MAXMTU 65535
89 * Private information; one per upper stream.
91 typedef struct upperstr {
92 minor_t mn; /* minor device number */
93 struct upperstr *nextmn; /* next minor device */
94 queue_t *q; /* read q associated with this upper stream */
95 int flags; /* flag bits, see below */
96 int state; /* current DLPI state */
97 int sap; /* service access point */
98 int req_sap; /* which SAP the DLPI client requested */
99 struct upperstr *ppa; /* control stream for our ppa */
100 struct upperstr *next; /* next stream for this ppa */
101 uint ioc_id; /* last ioctl ID for this stream */
102 enum NPmode npmode; /* what to do with packets on this SAP */
104 * There is exactly one control stream for each PPA.
105 * The following fields are only used for control streams.
108 queue_t *lowerq; /* write queue attached below this PPA */
109 struct upperstr *nextppa; /* next control stream */
112 struct pppstat stats; /* statistics */
113 time_t last_sent; /* time last NP packet sent */
114 time_t last_recv; /* time last NP packet rcvd */
116 kstat_t *kstats; /* stats for netstat */
120 char ifname[IFNAMSIZ];
121 struct ifstats ifstats;
125 /* Values for flags */
126 #define US_PRIV 1 /* stream was opened by superuser */
127 #define US_CONTROL 2 /* stream is a control stream */
128 #define US_BLOCKED 4 /* flow ctrl has blocked lower write stream */
129 #define US_LASTMOD 8 /* no PPP modules below us */
130 #define US_DBGLOG 0x10 /* log various occurrences */
132 static upperstr_t *minor_devs = NULL;
133 static upperstr_t *ppas = NULL;
136 static int pppopen __P((queue_t *, dev_t *, int, int, cred_t *));
137 static int pppclose __P((queue_t *, int, cred_t *));
139 static int pppopen __P((queue_t *, int, int, int));
140 static int pppclose __P((queue_t *, int));
142 static int pppuwput __P((queue_t *, mblk_t *));
143 static int pppursrv __P((queue_t *));
144 static int pppuwsrv __P((queue_t *));
145 static int ppplrput __P((queue_t *, mblk_t *));
146 static int ppplwput __P((queue_t *, mblk_t *));
147 static int ppplrsrv __P((queue_t *));
148 static int ppplwsrv __P((queue_t *));
150 static void dlpi_request __P((queue_t *, mblk_t *, upperstr_t *));
151 static void dlpi_error __P((queue_t *, int, int, int));
152 static void dlpi_ok __P((queue_t *, int));
154 static int send_data __P((mblk_t *, upperstr_t *));
155 static void new_ppa __P((queue_t *, mblk_t *));
156 static void attach_ppa __P((queue_t *, mblk_t *));
157 static void detach_ppa __P((queue_t *, mblk_t *));
158 static void debug_dump __P((queue_t *, mblk_t *));
159 static upperstr_t *find_dest __P((upperstr_t *, int));
160 static int putctl2 __P((queue_t *, int, int, int));
161 static int putctl4 __P((queue_t *, int, int, int));
163 #define PPP_ID 0xb1a6
164 static struct module_info ppp_info = {
165 PPP_ID, "ppp", 0, 512, 512, 128
168 static struct qinit pppurint = {
169 NULL, pppursrv, pppopen, pppclose, NULL, &ppp_info, NULL
172 static struct qinit pppuwint = {
173 pppuwput, pppuwsrv, NULL, NULL, NULL, &ppp_info, NULL
176 static struct qinit ppplrint = {
177 ppplrput, ppplrsrv, NULL, NULL, NULL, &ppp_info, NULL
180 static struct qinit ppplwint = {
181 ppplwput, ppplwsrv, NULL, NULL, NULL, &ppp_info, NULL
185 extern struct ifstats *ifstats;
189 struct streamtab pppinfo = {
190 &pppurint, &pppuwint,
197 * How we maintain statistics.
200 #define INCR_IPACKETS(ppa) \
201 if (ppa->kstats != 0) { \
202 KSTAT_NAMED_PTR(ppa->kstats)[0].value.ul++; \
204 #define INCR_IERRORS(ppa) \
205 if (ppa->kstats != 0) { \
206 KSTAT_NAMED_PTR(ppa->kstats)[1].value.ul++; \
208 #define INCR_OPACKETS(ppa) \
209 if (ppa->kstats != 0) { \
210 KSTAT_NAMED_PTR(ppa->kstats)[2].value.ul++; \
212 #define INCR_OERRORS(ppa) \
213 if (ppa->kstats != 0) { \
214 KSTAT_NAMED_PTR(ppa->kstats)[3].value.ul++; \
219 #define INCR_IPACKETS(ppa) ppa->ifstats.ifs_ipackets++;
220 #define INCR_IERRORS(ppa) ppa->ifstats.ifs_ierrors++;
221 #define INCR_OPACKETS(ppa) ppa->ifstats.ifs_opackets++;
222 #define INCR_OERRORS(ppa) ppa->ifstats.ifs_oerrors++;
226 * STREAMS driver entry points.
230 pppopen(q, devp, oflag, sflag, credp)
236 pppopen(q, dev, oflag, sflag)
238 int dev; /* really dev_t */
247 DRV_OPEN_OK(dev); /* device is already open */
249 if (sflag == CLONEOPEN) {
251 for (prevp = &minor_devs; (up = *prevp) != 0; prevp = &up->nextmn) {
258 mn = getminor(*devp);
262 for (prevp = &minor_devs; (up = *prevp) != 0; prevp = &up->nextmn) {
267 /* this can't happen */
268 q->q_ptr = WR(q)->q_ptr = (caddr_t) up;
274 * Construct a new minor node.
276 up = (upperstr_t *) ALLOC_SLEEP(sizeof(upperstr_t));
277 bzero((caddr_t) up, sizeof(upperstr_t));
279 DPRINT("pppopen: out of kernel memory\n");
286 *devp = makedevice(getmajor(*devp), mn);
290 up->flags |= US_PRIV;
292 up->state = DL_UNATTACHED;
295 up->ifflags = IFF_UP | IFF_POINTOPOINT;
298 up->last_sent = up->last_recv = time;
299 up->npmode = NPMODE_DROP;
300 q->q_ptr = (caddr_t) up;
301 WR(q)->q_ptr = (caddr_t) up;
306 DRV_OPEN_OK(makedev(major(dev), mn));
311 pppclose(q, flag, credp)
321 upperstr_t *up, **upp;
322 upperstr_t *as, *asnext;
327 up = (upperstr_t *) q->q_ptr;
328 if (up->flags & US_DBGLOG)
329 DPRINT2("ppp/%d: close, flags=%x\n", up->mn, up->flags);
332 if (up->flags & US_CONTROL) {
334 struct ifstats *ifp, *pifp;
337 * This stream represents a PPA:
338 * For all streams attached to the PPA, clear their
339 * references to this PPA.
340 * Then remove this PPA from the list of PPAs.
342 for (as = up->next; as != 0; as = asnext) {
346 if (as->flags & US_BLOCKED) {
347 as->flags &= ~US_BLOCKED;
348 flushq(WR(as->q), FLUSHDATA);
351 for (upp = &ppas; *upp != 0; upp = &(*upp)->nextppa)
357 /* Remove the statistics from the active list. */
358 for (ifp = ifstats, pifp = 0; ifp; ifp = ifp->ifs_next) {
359 if (ifp == &up->ifstats) {
361 pifp->ifs_next = ifp->ifs_next;
363 ifstats = ifp->ifs_next;
371 * If this stream is attached to a PPA,
372 * remove it from the PPA's list.
374 if ((as = up->ppa) != 0) {
375 for (; as->next != 0; as = as->next)
376 if (as->next == up) {
385 kstat_delete(up->kstats);
391 for (prevp = &minor_devs; *prevp != 0; prevp = &(*prevp)->nextmn) {
397 FREE(up, sizeof(upperstr_t));
404 * A message from on high. We do one of three things:
406 * - put the message on the lower write stream
407 * - queue it for our service routine
414 upperstr_t *us, *usnext, *ppa, *os, *nps;
424 struct ppp_idle *pip;
427 us = (upperstr_t *) q->q_ptr;
428 switch (mp->b_datap->db_type) {
432 dlpi_request(q, mp, us);
437 if (us->flags & US_DBGLOG)
438 DPRINT3("ppp/%d: uwput M_DATA len=%d flags=%x\n",
439 us->mn, msgdsize(mp), us->flags);
440 if (us->ppa == 0 || msgdsize(mp) > us->ppa->mtu + PPP_HDRLEN
442 || (us->flags & US_CONTROL) == 0
445 DPRINT1("pppuwput: junk data len=%d\n", msgdsize(mp));
450 if (!pass_packet(us->ppa, mp, 1)) {
455 if (!send_data(mp, us))
460 iop = (struct iocblk *) mp->b_rptr;
462 if (us->flags & US_DBGLOG)
463 DPRINT3("ppp/%d: ioctl %x count=%d\n",
464 us->mn, iop->ioc_cmd, iop->ioc_count);
465 switch (iop->ioc_cmd) {
467 if ((us->flags & US_CONTROL) == 0 || us->lowerq != 0)
469 lb = (struct linkblk *) mp->b_cont->b_rptr;
470 us->lowerq = lq = lb->l_qbot;
471 lq->q_ptr = (caddr_t) us;
472 RD(lq)->q_ptr = (caddr_t) us;
474 flushq(RD(lq), FLUSHALL);
477 us->flags &= ~US_LASTMOD;
478 /* Unblock upper streams which now feed this lower stream. */
480 /* Send useful information down to the modules which
481 are now linked below us. */
482 putctl2(lq, M_CTL, PPPCTL_UNIT, us->ppa_id);
483 putctl4(lq, M_CTL, PPPCTL_MRU, us->mru);
484 putctl4(lq, M_CTL, PPPCTL_MTU, us->mtu);
488 lb = (struct linkblk *) mp->b_cont->b_rptr;
490 if (us->lowerq != lb->l_qbot)
491 DPRINT2("ppp unlink: lowerq=%x qbot=%x\n",
492 us->lowerq, lb->l_qbot);
497 /* Unblock streams which now feed back up the control stream. */
502 if (us->flags & US_CONTROL)
504 if ((us->flags & US_PRIV) == 0) {
508 /* Arrange to return an int */
509 if ((mq = mp->b_cont) == 0
510 || mq->b_datap->db_lim - mq->b_rptr < sizeof(int)) {
511 mq = allocb(sizeof(int), BPRI_HI);
521 iop->ioc_count = sizeof(int);
522 mq->b_wptr = mq->b_rptr + sizeof(int);
523 qwriter(q, mp, new_ppa, PERIM_OUTER);
528 /* like dlpi_attach, for programs which can't write to
529 the stream (like pppstats) */
530 if (iop->ioc_count != sizeof(int) || us->ppa != 0)
532 n = *(int *)mp->b_cont->b_rptr;
533 for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
534 if (ppa->ppa_id == n)
540 qwriter(q, mp, attach_ppa, PERIM_OUTER);
546 /* Attach to a given SAP. */
547 if (iop->ioc_count != sizeof(int) || us->ppa == 0)
549 n = *(int *)mp->b_cont->b_rptr;
550 /* n must be a valid PPP network protocol number. */
551 if (n < 0x21 || n > 0x3fff || (n & 0x101) != 1)
553 /* check that no other stream is bound to this sap already. */
554 for (os = us->ppa; os != 0; os = os->next)
566 if (iop->ioc_count != sizeof(int) || (us->flags & US_CONTROL) == 0)
568 n = *(int *)mp->b_cont->b_rptr;
569 if (n <= 0 || n > PPP_MAXMTU)
575 putctl4(us->lowerq, M_CTL, PPPCTL_MRU, n);
581 if (iop->ioc_count != sizeof(int) || (us->flags & US_CONTROL) == 0)
583 n = *(int *)mp->b_cont->b_rptr;
584 if (n <= 0 || n > PPP_MAXMTU)
590 us->ifstats.ifs_mtu = n;
593 putctl4(us->lowerq, M_CTL, PPPCTL_MTU, n);
599 us->flags |= US_LASTMOD;
604 if (iop->ioc_count != sizeof(int))
606 n = *(int *)mp->b_cont->b_rptr;
607 if (n == PPPDBG_DUMP + PPPDBG_DRIVER) {
608 qwriter(q, NULL, debug_dump, PERIM_OUTER);
611 } else if (n == PPPDBG_LOG + PPPDBG_DRIVER) {
612 DPRINT1("ppp/%d: debug log enabled\n", us->mn);
613 us->flags |= US_DBGLOG;
617 if (us->ppa == 0 || us->ppa->lowerq == 0)
619 putnext(us->ppa->lowerq, mp);
625 if (iop->ioc_count != 2 * sizeof(int))
627 if ((us->flags & US_CONTROL) == 0)
629 sap = ((int *)mp->b_cont->b_rptr)[0];
630 for (nps = us->next; nps != 0; nps = nps->next)
634 if (us->flags & US_DBGLOG)
635 DPRINT2("ppp/%d: no stream for sap %x\n", us->mn, sap);
638 nps->npmode = (enum NPmode) ((int *)mp->b_cont->b_rptr)[1];
639 if (nps->npmode == NPMODE_DROP || nps->npmode == NPMODE_ERROR)
640 flushq(WR(nps->q), FLUSHDATA);
641 else if (nps->npmode == NPMODE_PASS && qsize(WR(nps->q)) > 0
642 && (nps->flags & US_BLOCKED) == 0)
649 if ((ppa = us->ppa) == 0)
651 mq = allocb(sizeof(struct ppp_idle), BPRI_HI);
660 pip = (struct ppp_idle *) mq->b_wptr;
661 pip->xmit_idle = time - ppa->last_sent;
662 pip->recv_idle = time - ppa->last_recv;
663 mq->b_wptr += sizeof(struct ppp_idle);
664 iop->ioc_count = sizeof(struct ppp_idle);
670 printf("SIOCSIFNAME\n");
671 /* Sent from IP down to us. Attach the ifstats structure. */
672 if (iop->ioc_count != sizeof(struct ifreq) || us->ppa == 0)
674 ifr = (struct ifreq *)mp->b_cont->b_rptr;
675 /* Find the unit number in the interface name. */
676 for (i = 0; i < IFNAMSIZ; i++) {
677 if (ifr->ifr_name[i] == 0 ||
678 (ifr->ifr_name[i] >= '0' &&
679 ifr->ifr_name[i] <= '9'))
682 us->ifname[i] = ifr->ifr_name[i];
686 /* Convert the unit number to binary. */
687 for (n = 0; i < IFNAMSIZ; i++) {
688 if (ifr->ifr_name[i] == 0) {
692 n = n * 10 + ifr->ifr_name[i] - '0';
696 /* Verify the ppa. */
697 if (us->ppa->ppa_id != n)
701 /* Set up the netstat block. */
702 strncpy (ppa->ifname, us->ifname, IFNAMSIZ);
704 ppa->ifstats.ifs_name = ppa->ifname;
705 ppa->ifstats.ifs_unit = n;
706 ppa->ifstats.ifs_active = us->state != DL_UNBOUND;
707 ppa->ifstats.ifs_mtu = ppa->mtu;
709 /* Link in statistics used by netstat. */
710 ppa->ifstats.ifs_next = ifstats;
711 ifstats = &ppa->ifstats;
718 printf("SIOCGIFFLAGS\n");
719 if (!(us->flags & US_CONTROL)) {
725 ((struct iocblk_in *)iop)->ioc_ifflags = us->ifflags;
730 printf("SIOCSIFFLAGS\n");
731 if (!(us->flags & US_CONTROL)) {
737 us->ifflags = ((struct iocblk_in *)iop)->ioc_ifflags;
742 printf("SIOCSIFADDR\n");
743 if (!(us->flags & US_CONTROL)) {
749 us->ifflags |= IFF_RUNNING;
750 ((struct iocblk_in *)iop)->ioc_ifflags |= IFF_RUNNING;
765 if (us->ppa == 0 || us->ppa->lowerq == 0)
767 us->ioc_id = iop->ioc_id;
769 switch (iop->ioc_cmd) {
772 if (us->flags & US_LASTMOD) {
776 putnext(us->ppa->lowerq, mp);
779 if (us->flags & US_PRIV)
780 putnext(us->ppa->lowerq, mp);
782 DPRINT1("ppp ioctl %x rejected\n", iop->ioc_cmd);
791 iop->ioc_error = error;
792 mp->b_datap->db_type = M_IOCNAK;
794 } else if (error == 0) {
795 mp->b_datap->db_type = M_IOCACK;
801 if (us->flags & US_DBGLOG)
802 DPRINT2("ppp/%d: flush %x\n", us->mn, *mp->b_rptr);
803 if (*mp->b_rptr & FLUSHW)
804 flushq(q, FLUSHDATA);
805 if (*mp->b_rptr & FLUSHR) {
806 *mp->b_rptr &= ~FLUSHW;
821 dlpi_request(q, mp, us)
826 union DL_primitives *d = (union DL_primitives *) mp->b_rptr;
827 int size = mp->b_wptr - mp->b_rptr;
829 upperstr_t *ppa, *os;
834 if (us->flags & US_DBGLOG)
835 DPRINT3("ppp/%d: dlpi prim %x len=%d\n", us->mn,
836 d->dl_primitive, size);
837 switch (d->dl_primitive) {
839 if (size < sizeof(dl_info_req_t))
841 if ((reply = allocb(sizeof(dl_info_ack_t), BPRI_HI)) == 0)
842 break; /* should do bufcall */
843 reply->b_datap->db_type = M_PCPROTO;
844 info = (dl_info_ack_t *) reply->b_wptr;
845 reply->b_wptr += sizeof(dl_info_ack_t);
846 bzero((caddr_t) info, sizeof(dl_info_ack_t));
847 info->dl_primitive = DL_INFO_ACK;
848 info->dl_max_sdu = PPP_MAXMTU;
849 info->dl_min_sdu = 1;
850 info->dl_addr_length = sizeof(ulong);
852 info->dl_mac_type = DL_OTHER;
854 info->dl_mac_type = DL_HDLC; /* a lie */
856 info->dl_current_state = us->state;
857 info->dl_service_mode = DL_CLDLS;
858 info->dl_provider_style = DL_STYLE2;
859 #if DL_CURRENT_VERSION >= 2
860 info->dl_sap_length = sizeof(ulong);
861 info->dl_version = DL_CURRENT_VERSION;
867 if (size < sizeof(dl_attach_req_t))
869 if (us->state != DL_UNATTACHED || us->ppa != 0) {
870 dlpi_error(q, DL_ATTACH_REQ, DL_OUTSTATE, 0);
873 for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
874 if (ppa->ppa_id == d->attach_req.dl_ppa)
877 dlpi_error(q, DL_ATTACH_REQ, DL_BADPPA, 0);
881 qwriter(q, mp, attach_ppa, PERIM_OUTER);
885 if (size < sizeof(dl_detach_req_t))
887 if (us->state != DL_UNBOUND || us->ppa == 0) {
888 dlpi_error(q, DL_DETACH_REQ, DL_OUTSTATE, 0);
891 qwriter(q, mp, detach_ppa, PERIM_OUTER);
895 if (size < sizeof(dl_bind_req_t))
897 if (us->state != DL_UNBOUND || us->ppa == 0) {
898 dlpi_error(q, DL_BIND_REQ, DL_OUTSTATE, 0);
901 if (d->bind_req.dl_service_mode != DL_CLDLS) {
902 dlpi_error(q, DL_BIND_REQ, DL_UNSUPPORTED, 0);
906 /* saps must be valid PPP network protocol numbers,
907 except that we accept ETHERTYPE_IP in place of PPP_IP. */
908 sap = d->bind_req.dl_sap;
910 if (sap == ETHERTYPE_IP)
912 if (sap < 0x21 || sap > 0x3fff || (sap & 0x101) != 1) {
913 dlpi_error(q, DL_BIND_REQ, DL_BADADDR, 0);
917 /* check that no other stream is bound to this sap already. */
918 for (os = us->ppa; os != 0; os = os->next)
922 dlpi_error(q, DL_BIND_REQ, DL_NOADDR, 0);
929 if ((reply = allocb(sizeof(dl_bind_ack_t) + sizeof(ulong),
931 break; /* should do bufcall */
932 ackp = (dl_bind_ack_t *) reply->b_wptr;
933 reply->b_wptr += sizeof(dl_bind_ack_t) + sizeof(ulong);
934 reply->b_datap->db_type = M_PCPROTO;
935 bzero((caddr_t) ackp, sizeof(dl_bind_ack_t));
936 ackp->dl_primitive = DL_BIND_ACK;
938 ackp->dl_addr_length = sizeof(ulong);
939 ackp->dl_addr_offset = sizeof(dl_bind_ack_t);
940 *(ulong *)(ackp+1) = sap;
945 if (size < sizeof(dl_unbind_req_t))
947 if (us->state != DL_IDLE) {
948 dlpi_error(q, DL_UNBIND_REQ, DL_OUTSTATE, 0);
952 us->state = DL_UNBOUND;
954 us->ppa->ifstats.ifs_active = 0;
956 dlpi_ok(q, DL_UNBIND_REQ);
959 case DL_UNITDATA_REQ:
960 if (size < sizeof(dl_unitdata_req_t))
962 if (us->state != DL_IDLE) {
963 dlpi_error(q, DL_UNITDATA_REQ, DL_OUTSTATE, 0);
966 if ((ppa = us->ppa) == 0) {
967 cmn_err(CE_CONT, "ppp: in state dl_idle but ppa == 0?\n");
970 len = mp->b_cont == 0? 0: msgdsize(mp->b_cont);
971 if (len > ppa->mtu) {
972 DPRINT2("dlpi data too large (%d > %d)\n", len, ppa->mtu);
975 /* this assumes PPP_HDRLEN <= sizeof(dl_unitdata_req_t) */
976 if (mp->b_datap->db_ref > 1) {
977 np = allocb(PPP_HDRLEN, BPRI_HI);
980 np->b_cont = mp->b_cont;
985 mp->b_datap->db_type = M_DATA;
986 /* XXX should use dl_dest_addr_offset/length here,
987 but we would have to translate ETHERTYPE_IP -> PPP_IP */
988 mp->b_wptr = mp->b_rptr + PPP_HDRLEN;
989 mp->b_rptr[0] = PPP_ALLSTATIONS;
990 mp->b_rptr[1] = PPP_UI;
991 mp->b_rptr[2] = us->sap >> 8;
992 mp->b_rptr[3] = us->sap;
993 if (!pass_packet(ppa, mp, 1)) {
996 if (!send_data(mp, us))
1001 #if DL_CURRENT_VERSION >= 2
1002 case DL_SUBS_BIND_REQ:
1003 case DL_SUBS_UNBIND_REQ:
1004 case DL_ENABMULTI_REQ:
1005 case DL_DISABMULTI_REQ:
1006 case DL_PROMISCON_REQ:
1007 case DL_PROMISCOFF_REQ:
1008 case DL_PHYS_ADDR_REQ:
1009 case DL_SET_PHYS_ADDR_REQ:
1012 case DL_REPLY_UPDATE_REQ:
1014 case DL_DATA_ACK_REQ:
1016 case DL_CONNECT_REQ:
1018 dlpi_error(q, d->dl_primitive, DL_NOTSUPPORTED, 0);
1021 case DL_CONNECT_RES:
1022 case DL_DISCONNECT_REQ:
1025 dlpi_error(q, d->dl_primitive, DL_OUTSTATE, 0);
1029 dlpi_error(q, d->dl_primitive, DL_BADQOSTYPE, 0);
1032 #if DL_CURRENT_VERSION >= 2
1039 cmn_err(CE_CONT, "ppp: unknown dlpi prim 0x%x\n", d->dl_primitive);
1042 dlpi_error(q, d->dl_primitive, DL_BADPRIM, 0);
1049 dlpi_error(q, prim, err, uerr)
1051 int prim, err, uerr;
1054 dl_error_ack_t *errp;
1056 reply = allocb(sizeof(dl_error_ack_t), BPRI_HI);
1058 return; /* XXX should do bufcall */
1059 reply->b_datap->db_type = M_PCPROTO;
1060 errp = (dl_error_ack_t *) reply->b_wptr;
1061 reply->b_wptr += sizeof(dl_error_ack_t);
1062 errp->dl_primitive = DL_ERROR_ACK;
1063 errp->dl_error_primitive = prim;
1064 errp->dl_errno = err;
1065 errp->dl_unix_errno = uerr;
1077 reply = allocb(sizeof(dl_ok_ack_t), BPRI_HI);
1079 return; /* XXX should do bufcall */
1080 reply->b_datap->db_type = M_PCPROTO;
1081 okp = (dl_ok_ack_t *) reply->b_wptr;
1082 reply->b_wptr += sizeof(dl_ok_ack_t);
1083 okp->dl_primitive = DL_OK_ACK;
1084 okp->dl_correct_primitive = prim;
1087 #endif /* NO_DLPI */
1090 pass_packet(ppa, mp, outbound)
1096 * Here is where we might, in future, decide whether to pass
1097 * or drop the packet, and whether it counts as link activity.
1100 ppa->last_sent = time;
1102 ppa->last_recv = time;
1114 if ((us->flags & US_BLOCKED) || us->npmode == NPMODE_QUEUE)
1117 if (ppa == 0 || us->npmode == NPMODE_DROP || us->npmode == NPMODE_ERROR) {
1118 if (us->flags & US_DBGLOG)
1119 DPRINT2("ppp/%d: dropping pkt (npmode=%d)\n", us->mn, us->npmode);
1123 if ((q = ppa->lowerq) == 0) {
1124 /* try to send it up the control stream */
1125 if (canputnext(ppa->q)) {
1126 putnext(ppa->q, mp);
1130 if (canputnext(ppa->lowerq)) {
1132 * The lower write queue's put procedure just updates counters
1133 * and does a putnext. We call it so that on SMP systems, we
1134 * enter the lower queues' perimeter so that the counter
1135 * updates are serialized.
1137 put(ppa->lowerq, mp);
1141 us->flags |= US_BLOCKED;
1146 * Allocate a new PPA id and link this stream into the list of PPAs.
1147 * This procedure is called with an exclusive lock on all queues in
1155 upperstr_t *us, **usp;
1160 while ((us = *usp) != 0 && ppa_id == us->ppa_id) {
1164 us = (upperstr_t *) q->q_ptr;
1165 us->ppa_id = ppa_id;
1170 us->flags |= US_CONTROL;
1171 us->npmode = NPMODE_PASS;
1178 * Create a kstats record for our statistics, so netstat -i works.
1180 if (us->kstats == 0) {
1183 sprintf(unit, "ppp%d", us->ppa->ppa_id);
1184 us->kstats = kstat_create("ppp", us->ppa->ppa_id, unit,
1185 "net", KSTAT_TYPE_NAMED, 4, 0);
1186 if (us->kstats != 0) {
1187 kstat_named_t *kn = KSTAT_NAMED_PTR(us->kstats);
1189 strcpy(kn[0].name, "ipackets");
1190 kn[0].data_type = KSTAT_DATA_ULONG;
1191 strcpy(kn[1].name, "ierrors");
1192 kn[1].data_type = KSTAT_DATA_ULONG;
1193 strcpy(kn[2].name, "opackets");
1194 kn[2].data_type = KSTAT_DATA_ULONG;
1195 strcpy(kn[3].name, "oerrors");
1196 kn[3].data_type = KSTAT_DATA_ULONG;
1197 kstat_install(us->kstats);
1202 *(int *)mp->b_cont->b_rptr = ppa_id;
1203 mp->b_datap->db_type = M_IOCACK;
1214 us = (upperstr_t *) q->q_ptr;
1216 us->state = DL_UNBOUND;
1218 for (t = us->ppa; t->next != 0; t = t->next)
1222 if (mp->b_datap->db_type == M_IOCTL) {
1223 mp->b_datap->db_type = M_IOCACK;
1227 dlpi_ok(q, DL_ATTACH_REQ);
1239 us = (upperstr_t *) q->q_ptr;
1240 for (t = us->ppa; t->next != 0; t = t->next)
1241 if (t->next == us) {
1248 us->state = DL_UNATTACHED;
1249 dlpi_ok(q, DL_DETACH_REQ);
1258 struct lowerstr *ls;
1262 us = (upperstr_t *) q->q_ptr;
1263 us->flags &= ~US_BLOCKED;
1264 while ((mp = getq(q)) != 0) {
1265 if (!send_data(mp, us)) {
1280 ppa = (upperstr_t *) q->q_ptr;
1281 if (ppa != 0) { /* why wouldn't it? */
1282 ppa->stats.ppp_opackets++;
1283 ppa->stats.ppp_obytes += msgdsize(mp);
1284 #ifdef INCR_OPACKETS
1299 * Flow control has back-enabled this stream:
1300 * enable the write service procedures of all upper
1301 * streams feeding this lower stream.
1303 for (us = (upperstr_t *) q->q_ptr; us != NULL; us = us->next)
1304 if (us->flags & US_BLOCKED)
1313 upperstr_t *us, *as;
1316 dl_unitdata_ind_t *ud;
1320 us = (upperstr_t *) q->q_ptr;
1321 if (us->flags & US_CONTROL) {
1324 * If there is no lower queue attached, run the write service
1325 * routines of other upper streams attached to this PPA.
1327 if (us->lowerq == 0) {
1330 if (as->flags & US_BLOCKED)
1337 * A network protocol stream. Put a DLPI header on each
1338 * packet and send it on.
1339 * (Actually, it seems that the IP module will happily
1340 * accept M_DATA messages without the DL_UNITDATA_IND header.)
1342 while ((mp = getq(q)) != 0) {
1343 if (!canputnext(q)) {
1348 proto = PPP_PROTOCOL(mp->b_rptr);
1349 mp->b_rptr += PPP_HDRLEN;
1350 hdr = allocb(sizeof(dl_unitdata_ind_t) + 2 * sizeof(ulong),
1353 /* XXX should put it back and use bufcall */
1357 hdr->b_datap->db_type = M_PROTO;
1358 ud = (dl_unitdata_ind_t *) hdr->b_wptr;
1359 hdr->b_wptr += sizeof(dl_unitdata_ind_t) + 2 * sizeof(ulong);
1361 ud->dl_primitive = DL_UNITDATA_IND;
1362 ud->dl_dest_addr_length = sizeof(ulong);
1363 ud->dl_dest_addr_offset = sizeof(dl_unitdata_ind_t);
1364 ud->dl_src_addr_length = sizeof(ulong);
1365 ud->dl_src_addr_offset = ud->dl_dest_addr_offset + sizeof(ulong);
1366 #if DL_CURRENT_VERSION >= 2
1367 ud->dl_group_address = 0;
1369 /* Send the DLPI client the data with the SAP they requested,
1370 (e.g. ETHERTYPE_IP) rather than the PPP protocol number
1372 ((ulong *)(ud + 1))[0] = us->req_sap; /* dest SAP */
1373 ((ulong *)(ud + 1))[1] = us->req_sap; /* src SAP */
1377 #endif /* NO_DLPI */
1382 * If this stream is attached to a PPA with a lower queue pair,
1383 * enable the read queue's service routine if it has data queued.
1384 * XXX there is a possibility that packets could get out of order
1385 * if ppplrput now runs before ppplrsrv.
1387 if (us->ppa != 0 && us->ppa->lowerq != 0)
1388 qenable(RD(us->ppa->lowerq));
1394 find_dest(ppa, proto)
1400 for (us = ppa->next; us != 0; us = us->next)
1401 if (proto == us->sap)
1411 upperstr_t *ppa, *us;
1417 ppa = (upperstr_t *) q->q_ptr;
1419 DPRINT1("ppplrput: q = %x, ppa = 0??\n", q);
1423 switch (mp->b_datap->db_type) {
1425 if (*mp->b_rptr & FLUSHW) {
1426 *mp->b_rptr &= ~FLUSHR;
1433 switch (*mp->b_rptr) {
1438 ppa->stats.ppp_ierrors++;
1444 ppa->stats.ppp_oerrors++;
1453 * Attempt to match up the response with the stream
1454 * that the request came from.
1456 iop = (struct iocblk *) mp->b_rptr;
1457 for (us = ppa; us != 0; us = us->next)
1458 if (us->ioc_id == iop->ioc_id)
1468 * The serial device has hung up. We don't want to send
1469 * the M_HANGUP message up to pppd because that will stop
1470 * us from using the control stream any more. Instead we
1471 * send a zero-length message as an end-of-file indication.
1474 mp = allocb(1, BPRI_HI);
1476 DPRINT1("ppp/%d: couldn't allocate eof message!\n", ppa->mn);
1479 putnext(ppa->q, mp);
1483 if (mp->b_datap->db_type == M_DATA) {
1485 if (mp->b_wptr - mp->b_rptr < PPP_HDRLEN) {
1486 PULLUP(mp, PPP_HDRLEN);
1488 DPRINT1("ppp_lrput: msgpullup failed (len=%d)\n", len);
1492 ppa->stats.ppp_ipackets++;
1493 ppa->stats.ppp_ibytes += len;
1494 #ifdef INCR_IPACKETS
1497 if (!pass_packet(ppa, mp, 0)) {
1501 proto = PPP_PROTOCOL(mp->b_rptr);
1502 if (proto < 0x8000 && (us = find_dest(ppa, proto)) != 0) {
1504 * A data packet for some network protocol.
1505 * Queue it on the upper stream for that protocol.
1515 * A control frame, a frame for an unknown protocol,
1516 * or some other message type.
1517 * Send it up to pppd via the control stream.
1519 if (queclass(mp) == QPCTL || canputnext(ppa->q))
1520 putnext(ppa->q, mp);
1534 upperstr_t *ppa, *us;
1538 * Packets only get queued here for flow control reasons.
1540 ppa = (upperstr_t *) q->q_ptr;
1541 while ((mp = getq(q)) != 0) {
1542 if (mp->b_datap->db_type == M_DATA
1543 && (proto = PPP_PROTOCOL(mp->b_rptr)) < 0x8000
1544 && (us = find_dest(ppa, proto)) != 0) {
1552 if (canputnext(ppa->q))
1553 putnext(ppa->q, mp);
1564 putctl2(q, type, code, val)
1566 int type, code, val;
1570 mp = allocb(2, BPRI_HI);
1573 mp->b_datap->db_type = type;
1574 mp->b_wptr[0] = code;
1575 mp->b_wptr[1] = val;
1582 putctl4(q, type, code, val)
1584 int type, code, val;
1588 mp = allocb(4, BPRI_HI);
1591 mp->b_datap->db_type = type;
1592 mp->b_wptr[0] = code;
1593 ((short *)mp->b_wptr)[1] = val;
1601 queue_t *q; /* not used */
1602 mblk_t *mp; /* not used either */
1607 DPRINT("ppp upper streams:\n");
1608 for (us = minor_devs; us != 0; us = us->nextmn) {
1610 DPRINT3(" %d: q=%x rlev=%d",
1611 us->mn, uq, (uq? qsize(uq): 0));
1612 DPRINT3(" wlev=%d flags=0x%b", (uq? qsize(WR(uq)): 0),
1613 us->flags, "\020\1priv\2control\3blocked\4last");
1614 DPRINT3(" state=%x sap=%x req_sap=%x", us->state, us->sap,
1619 DPRINT1(" ppa=%d\n", us->ppa->ppa_id);
1620 if (us->flags & US_CONTROL) {
1622 DPRINT3(" control for %d lq=%x rlev=%d",
1623 us->ppa_id, lq, (lq? qsize(RD(lq)): 0));
1624 DPRINT3(" wlev=%d mru=%d mtu=%d\n",
1625 (lq? qsize(lq): 0), us->mru, us->mtu);