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.8 1996/09/14 05:18:45 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));
162 static int pass_packet __P((upperstr_t *ppa, mblk_t *mp, int outbound));
164 #define PPP_ID 0xb1a6
165 static struct module_info ppp_info = {
166 PPP_ID, "ppp", 0, 512, 512, 128
169 static struct qinit pppurint = {
170 NULL, pppursrv, pppopen, pppclose, NULL, &ppp_info, NULL
173 static struct qinit pppuwint = {
174 pppuwput, pppuwsrv, NULL, NULL, NULL, &ppp_info, NULL
177 static struct qinit ppplrint = {
178 ppplrput, ppplrsrv, NULL, NULL, NULL, &ppp_info, NULL
181 static struct qinit ppplwint = {
182 ppplwput, ppplwsrv, NULL, NULL, NULL, &ppp_info, NULL
186 extern struct ifstats *ifstats;
190 struct streamtab pppinfo = {
191 &pppurint, &pppuwint,
198 * How we maintain statistics.
201 #define INCR_IPACKETS(ppa) \
202 if (ppa->kstats != 0) { \
203 KSTAT_NAMED_PTR(ppa->kstats)[0].value.ul++; \
205 #define INCR_IERRORS(ppa) \
206 if (ppa->kstats != 0) { \
207 KSTAT_NAMED_PTR(ppa->kstats)[1].value.ul++; \
209 #define INCR_OPACKETS(ppa) \
210 if (ppa->kstats != 0) { \
211 KSTAT_NAMED_PTR(ppa->kstats)[2].value.ul++; \
213 #define INCR_OERRORS(ppa) \
214 if (ppa->kstats != 0) { \
215 KSTAT_NAMED_PTR(ppa->kstats)[3].value.ul++; \
220 #define INCR_IPACKETS(ppa) ppa->ifstats.ifs_ipackets++;
221 #define INCR_IERRORS(ppa) ppa->ifstats.ifs_ierrors++;
222 #define INCR_OPACKETS(ppa) ppa->ifstats.ifs_opackets++;
223 #define INCR_OERRORS(ppa) ppa->ifstats.ifs_oerrors++;
227 * STREAMS driver entry points.
231 pppopen(q, devp, oflag, sflag, credp)
237 pppopen(q, dev, oflag, sflag)
239 int dev; /* really dev_t */
248 DRV_OPEN_OK(dev); /* device is already open */
250 if (sflag == CLONEOPEN) {
252 for (prevp = &minor_devs; (up = *prevp) != 0; prevp = &up->nextmn) {
259 mn = getminor(*devp);
263 for (prevp = &minor_devs; (up = *prevp) != 0; prevp = &up->nextmn) {
268 /* this can't happen */
269 q->q_ptr = WR(q)->q_ptr = (caddr_t) up;
275 * Construct a new minor node.
277 up = (upperstr_t *) ALLOC_SLEEP(sizeof(upperstr_t));
278 bzero((caddr_t) up, sizeof(upperstr_t));
280 DPRINT("pppopen: out of kernel memory\n");
287 *devp = makedevice(getmajor(*devp), mn);
291 up->flags |= US_PRIV;
293 up->state = DL_UNATTACHED;
296 up->ifflags = IFF_UP | IFF_POINTOPOINT;
299 up->last_sent = up->last_recv = time;
300 up->npmode = NPMODE_DROP;
301 q->q_ptr = (caddr_t) up;
302 WR(q)->q_ptr = (caddr_t) up;
307 DRV_OPEN_OK(makedev(major(dev), mn));
312 pppclose(q, flag, credp)
322 upperstr_t *up, **upp;
323 upperstr_t *as, *asnext;
328 up = (upperstr_t *) q->q_ptr;
329 if (up->flags & US_DBGLOG)
330 DPRINT2("ppp/%d: close, flags=%x\n", up->mn, up->flags);
333 if (up->flags & US_CONTROL) {
335 struct ifstats *ifp, *pifp;
338 * This stream represents a PPA:
339 * For all streams attached to the PPA, clear their
340 * references to this PPA.
341 * Then remove this PPA from the list of PPAs.
343 for (as = up->next; as != 0; as = asnext) {
347 if (as->flags & US_BLOCKED) {
348 as->flags &= ~US_BLOCKED;
349 flushq(WR(as->q), FLUSHDATA);
352 for (upp = &ppas; *upp != 0; upp = &(*upp)->nextppa)
358 /* Remove the statistics from the active list. */
359 for (ifp = ifstats, pifp = 0; ifp; ifp = ifp->ifs_next) {
360 if (ifp == &up->ifstats) {
362 pifp->ifs_next = ifp->ifs_next;
364 ifstats = ifp->ifs_next;
372 * If this stream is attached to a PPA,
373 * remove it from the PPA's list.
375 if ((as = up->ppa) != 0) {
376 for (; as->next != 0; as = as->next)
377 if (as->next == up) {
386 kstat_delete(up->kstats);
392 for (prevp = &minor_devs; *prevp != 0; prevp = &(*prevp)->nextmn) {
398 FREE(up, sizeof(upperstr_t));
405 * A message from on high. We do one of three things:
407 * - put the message on the lower write stream
408 * - queue it for our service routine
415 upperstr_t *us, *usnext, *ppa, *os, *nps;
425 struct ppp_idle *pip;
428 us = (upperstr_t *) q->q_ptr;
429 switch (mp->b_datap->db_type) {
433 dlpi_request(q, mp, us);
438 if (us->flags & US_DBGLOG)
439 DPRINT3("ppp/%d: uwput M_DATA len=%d flags=%x\n",
440 us->mn, msgdsize(mp), us->flags);
441 if (us->ppa == 0 || msgdsize(mp) > us->ppa->mtu + PPP_HDRLEN
443 || (us->flags & US_CONTROL) == 0
446 DPRINT1("pppuwput: junk data len=%d\n", msgdsize(mp));
451 if (!pass_packet(us->ppa, mp, 1)) {
456 if (!send_data(mp, us))
461 iop = (struct iocblk *) mp->b_rptr;
463 if (us->flags & US_DBGLOG)
464 DPRINT3("ppp/%d: ioctl %x count=%d\n",
465 us->mn, iop->ioc_cmd, iop->ioc_count);
466 switch (iop->ioc_cmd) {
468 if ((us->flags & US_CONTROL) == 0 || us->lowerq != 0)
470 lb = (struct linkblk *) mp->b_cont->b_rptr;
471 us->lowerq = lq = lb->l_qbot;
472 lq->q_ptr = (caddr_t) us;
473 RD(lq)->q_ptr = (caddr_t) us;
475 flushq(RD(lq), FLUSHALL);
478 us->flags &= ~US_LASTMOD;
479 /* Unblock upper streams which now feed this lower stream. */
481 /* Send useful information down to the modules which
482 are now linked below us. */
483 putctl2(lq, M_CTL, PPPCTL_UNIT, us->ppa_id);
484 putctl4(lq, M_CTL, PPPCTL_MRU, us->mru);
485 putctl4(lq, M_CTL, PPPCTL_MTU, us->mtu);
489 lb = (struct linkblk *) mp->b_cont->b_rptr;
491 if (us->lowerq != lb->l_qbot)
492 DPRINT2("ppp unlink: lowerq=%x qbot=%x\n",
493 us->lowerq, lb->l_qbot);
498 /* Unblock streams which now feed back up the control stream. */
503 if (us->flags & US_CONTROL)
505 if ((us->flags & US_PRIV) == 0) {
509 /* Arrange to return an int */
510 if ((mq = mp->b_cont) == 0
511 || mq->b_datap->db_lim - mq->b_rptr < sizeof(int)) {
512 mq = allocb(sizeof(int), BPRI_HI);
522 iop->ioc_count = sizeof(int);
523 mq->b_wptr = mq->b_rptr + sizeof(int);
524 qwriter(q, mp, new_ppa, PERIM_OUTER);
529 /* like dlpi_attach, for programs which can't write to
530 the stream (like pppstats) */
531 if (iop->ioc_count != sizeof(int) || us->ppa != 0)
533 n = *(int *)mp->b_cont->b_rptr;
534 for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
535 if (ppa->ppa_id == n)
541 qwriter(q, mp, attach_ppa, PERIM_OUTER);
547 /* Attach to a given SAP. */
548 if (iop->ioc_count != sizeof(int) || us->ppa == 0)
550 n = *(int *)mp->b_cont->b_rptr;
551 /* n must be a valid PPP network protocol number. */
552 if (n < 0x21 || n > 0x3fff || (n & 0x101) != 1)
554 /* check that no other stream is bound to this sap already. */
555 for (os = us->ppa; os != 0; os = os->next)
567 if (iop->ioc_count != sizeof(int) || (us->flags & US_CONTROL) == 0)
569 n = *(int *)mp->b_cont->b_rptr;
570 if (n <= 0 || n > PPP_MAXMTU)
576 putctl4(us->lowerq, M_CTL, PPPCTL_MRU, n);
582 if (iop->ioc_count != sizeof(int) || (us->flags & US_CONTROL) == 0)
584 n = *(int *)mp->b_cont->b_rptr;
585 if (n <= 0 || n > PPP_MAXMTU)
591 us->ifstats.ifs_mtu = n;
594 putctl4(us->lowerq, M_CTL, PPPCTL_MTU, n);
600 us->flags |= US_LASTMOD;
605 if (iop->ioc_count != sizeof(int))
607 n = *(int *)mp->b_cont->b_rptr;
608 if (n == PPPDBG_DUMP + PPPDBG_DRIVER) {
609 qwriter(q, NULL, debug_dump, PERIM_OUTER);
612 } else if (n == PPPDBG_LOG + PPPDBG_DRIVER) {
613 DPRINT1("ppp/%d: debug log enabled\n", us->mn);
614 us->flags |= US_DBGLOG;
618 if (us->ppa == 0 || us->ppa->lowerq == 0)
620 putnext(us->ppa->lowerq, mp);
626 if (iop->ioc_count != 2 * sizeof(int))
628 if ((us->flags & US_CONTROL) == 0)
630 sap = ((int *)mp->b_cont->b_rptr)[0];
631 for (nps = us->next; nps != 0; nps = nps->next)
635 if (us->flags & US_DBGLOG)
636 DPRINT2("ppp/%d: no stream for sap %x\n", us->mn, sap);
639 nps->npmode = (enum NPmode) ((int *)mp->b_cont->b_rptr)[1];
640 if (nps->npmode == NPMODE_DROP || nps->npmode == NPMODE_ERROR)
641 flushq(WR(nps->q), FLUSHDATA);
642 else if (nps->npmode == NPMODE_PASS && qsize(WR(nps->q)) > 0
643 && (nps->flags & US_BLOCKED) == 0)
650 if ((ppa = us->ppa) == 0)
652 mq = allocb(sizeof(struct ppp_idle), BPRI_HI);
661 pip = (struct ppp_idle *) mq->b_wptr;
662 pip->xmit_idle = time - ppa->last_sent;
663 pip->recv_idle = time - ppa->last_recv;
664 mq->b_wptr += sizeof(struct ppp_idle);
665 iop->ioc_count = sizeof(struct ppp_idle);
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 if (!(us->flags & US_CONTROL)) {
724 ((struct iocblk_in *)iop)->ioc_ifflags = us->ifflags;
729 if (!(us->flags & US_CONTROL)) {
735 us->ifflags = ((struct iocblk_in *)iop)->ioc_ifflags;
740 if (!(us->flags & US_CONTROL)) {
746 us->ifflags |= IFF_RUNNING;
747 ((struct iocblk_in *)iop)->ioc_ifflags |= IFF_RUNNING;
762 if (us->ppa == 0 || us->ppa->lowerq == 0)
764 us->ioc_id = iop->ioc_id;
766 switch (iop->ioc_cmd) {
769 if (us->flags & US_LASTMOD) {
773 putnext(us->ppa->lowerq, mp);
776 if (us->flags & US_PRIV)
777 putnext(us->ppa->lowerq, mp);
779 DPRINT1("ppp ioctl %x rejected\n", iop->ioc_cmd);
788 iop->ioc_error = error;
789 mp->b_datap->db_type = M_IOCNAK;
791 } else if (error == 0) {
792 mp->b_datap->db_type = M_IOCACK;
798 if (us->flags & US_DBGLOG)
799 DPRINT2("ppp/%d: flush %x\n", us->mn, *mp->b_rptr);
800 if (*mp->b_rptr & FLUSHW)
801 flushq(q, FLUSHDATA);
802 if (*mp->b_rptr & FLUSHR) {
803 *mp->b_rptr &= ~FLUSHW;
818 dlpi_request(q, mp, us)
823 union DL_primitives *d = (union DL_primitives *) mp->b_rptr;
824 int size = mp->b_wptr - mp->b_rptr;
826 upperstr_t *ppa, *os;
831 if (us->flags & US_DBGLOG)
832 DPRINT3("ppp/%d: dlpi prim %x len=%d\n", us->mn,
833 d->dl_primitive, size);
834 switch (d->dl_primitive) {
836 if (size < sizeof(dl_info_req_t))
838 if ((reply = allocb(sizeof(dl_info_ack_t), BPRI_HI)) == 0)
839 break; /* should do bufcall */
840 reply->b_datap->db_type = M_PCPROTO;
841 info = (dl_info_ack_t *) reply->b_wptr;
842 reply->b_wptr += sizeof(dl_info_ack_t);
843 bzero((caddr_t) info, sizeof(dl_info_ack_t));
844 info->dl_primitive = DL_INFO_ACK;
845 info->dl_max_sdu = PPP_MAXMTU;
846 info->dl_min_sdu = 1;
847 info->dl_addr_length = sizeof(ulong);
849 info->dl_mac_type = DL_OTHER;
851 info->dl_mac_type = DL_HDLC; /* a lie */
853 info->dl_current_state = us->state;
854 info->dl_service_mode = DL_CLDLS;
855 info->dl_provider_style = DL_STYLE2;
856 #if DL_CURRENT_VERSION >= 2
857 info->dl_sap_length = sizeof(ulong);
858 info->dl_version = DL_CURRENT_VERSION;
864 if (size < sizeof(dl_attach_req_t))
866 if (us->state != DL_UNATTACHED || us->ppa != 0) {
867 dlpi_error(q, DL_ATTACH_REQ, DL_OUTSTATE, 0);
870 for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
871 if (ppa->ppa_id == d->attach_req.dl_ppa)
874 dlpi_error(q, DL_ATTACH_REQ, DL_BADPPA, 0);
878 qwriter(q, mp, attach_ppa, PERIM_OUTER);
882 if (size < sizeof(dl_detach_req_t))
884 if (us->state != DL_UNBOUND || us->ppa == 0) {
885 dlpi_error(q, DL_DETACH_REQ, DL_OUTSTATE, 0);
888 qwriter(q, mp, detach_ppa, PERIM_OUTER);
892 if (size < sizeof(dl_bind_req_t))
894 if (us->state != DL_UNBOUND || us->ppa == 0) {
895 dlpi_error(q, DL_BIND_REQ, DL_OUTSTATE, 0);
898 if (d->bind_req.dl_service_mode != DL_CLDLS) {
899 dlpi_error(q, DL_BIND_REQ, DL_UNSUPPORTED, 0);
903 /* saps must be valid PPP network protocol numbers,
904 except that we accept ETHERTYPE_IP in place of PPP_IP. */
905 sap = d->bind_req.dl_sap;
907 if (sap == ETHERTYPE_IP)
909 if (sap < 0x21 || sap > 0x3fff || (sap & 0x101) != 1) {
910 dlpi_error(q, DL_BIND_REQ, DL_BADADDR, 0);
914 /* check that no other stream is bound to this sap already. */
915 for (os = us->ppa; os != 0; os = os->next)
919 dlpi_error(q, DL_BIND_REQ, DL_NOADDR, 0);
926 if ((reply = allocb(sizeof(dl_bind_ack_t) + sizeof(ulong),
928 break; /* should do bufcall */
929 ackp = (dl_bind_ack_t *) reply->b_wptr;
930 reply->b_wptr += sizeof(dl_bind_ack_t) + sizeof(ulong);
931 reply->b_datap->db_type = M_PCPROTO;
932 bzero((caddr_t) ackp, sizeof(dl_bind_ack_t));
933 ackp->dl_primitive = DL_BIND_ACK;
935 ackp->dl_addr_length = sizeof(ulong);
936 ackp->dl_addr_offset = sizeof(dl_bind_ack_t);
937 *(ulong *)(ackp+1) = sap;
942 if (size < sizeof(dl_unbind_req_t))
944 if (us->state != DL_IDLE) {
945 dlpi_error(q, DL_UNBIND_REQ, DL_OUTSTATE, 0);
949 us->state = DL_UNBOUND;
951 us->ppa->ifstats.ifs_active = 0;
953 dlpi_ok(q, DL_UNBIND_REQ);
956 case DL_UNITDATA_REQ:
957 if (size < sizeof(dl_unitdata_req_t))
959 if (us->state != DL_IDLE) {
960 dlpi_error(q, DL_UNITDATA_REQ, DL_OUTSTATE, 0);
963 if ((ppa = us->ppa) == 0) {
964 cmn_err(CE_CONT, "ppp: in state dl_idle but ppa == 0?\n");
967 len = mp->b_cont == 0? 0: msgdsize(mp->b_cont);
968 if (len > ppa->mtu) {
969 DPRINT2("dlpi data too large (%d > %d)\n", len, ppa->mtu);
972 /* this assumes PPP_HDRLEN <= sizeof(dl_unitdata_req_t) */
973 if (mp->b_datap->db_ref > 1) {
974 np = allocb(PPP_HDRLEN, BPRI_HI);
977 np->b_cont = mp->b_cont;
982 mp->b_datap->db_type = M_DATA;
983 /* XXX should use dl_dest_addr_offset/length here,
984 but we would have to translate ETHERTYPE_IP -> PPP_IP */
985 mp->b_wptr = mp->b_rptr + PPP_HDRLEN;
986 mp->b_rptr[0] = PPP_ALLSTATIONS;
987 mp->b_rptr[1] = PPP_UI;
988 mp->b_rptr[2] = us->sap >> 8;
989 mp->b_rptr[3] = us->sap;
990 if (!pass_packet(ppa, mp, 1)) {
993 if (!send_data(mp, us))
998 #if DL_CURRENT_VERSION >= 2
999 case DL_SUBS_BIND_REQ:
1000 case DL_SUBS_UNBIND_REQ:
1001 case DL_ENABMULTI_REQ:
1002 case DL_DISABMULTI_REQ:
1003 case DL_PROMISCON_REQ:
1004 case DL_PROMISCOFF_REQ:
1005 case DL_PHYS_ADDR_REQ:
1006 case DL_SET_PHYS_ADDR_REQ:
1009 case DL_REPLY_UPDATE_REQ:
1011 case DL_DATA_ACK_REQ:
1013 case DL_CONNECT_REQ:
1015 dlpi_error(q, d->dl_primitive, DL_NOTSUPPORTED, 0);
1018 case DL_CONNECT_RES:
1019 case DL_DISCONNECT_REQ:
1022 dlpi_error(q, d->dl_primitive, DL_OUTSTATE, 0);
1026 dlpi_error(q, d->dl_primitive, DL_BADQOSTYPE, 0);
1029 #if DL_CURRENT_VERSION >= 2
1036 cmn_err(CE_CONT, "ppp: unknown dlpi prim 0x%x\n", d->dl_primitive);
1039 dlpi_error(q, d->dl_primitive, DL_BADPRIM, 0);
1046 dlpi_error(q, prim, err, uerr)
1048 int prim, err, uerr;
1051 dl_error_ack_t *errp;
1053 reply = allocb(sizeof(dl_error_ack_t), BPRI_HI);
1055 return; /* XXX should do bufcall */
1056 reply->b_datap->db_type = M_PCPROTO;
1057 errp = (dl_error_ack_t *) reply->b_wptr;
1058 reply->b_wptr += sizeof(dl_error_ack_t);
1059 errp->dl_primitive = DL_ERROR_ACK;
1060 errp->dl_error_primitive = prim;
1061 errp->dl_errno = err;
1062 errp->dl_unix_errno = uerr;
1074 reply = allocb(sizeof(dl_ok_ack_t), BPRI_HI);
1076 return; /* XXX should do bufcall */
1077 reply->b_datap->db_type = M_PCPROTO;
1078 okp = (dl_ok_ack_t *) reply->b_wptr;
1079 reply->b_wptr += sizeof(dl_ok_ack_t);
1080 okp->dl_primitive = DL_OK_ACK;
1081 okp->dl_correct_primitive = prim;
1084 #endif /* NO_DLPI */
1087 pass_packet(ppa, mp, outbound)
1093 * Here is where we might, in future, decide whether to pass
1094 * or drop the packet, and whether it counts as link activity.
1097 ppa->last_sent = time;
1099 ppa->last_recv = time;
1111 if ((us->flags & US_BLOCKED) || us->npmode == NPMODE_QUEUE)
1114 if (ppa == 0 || us->npmode == NPMODE_DROP || us->npmode == NPMODE_ERROR) {
1115 if (us->flags & US_DBGLOG)
1116 DPRINT2("ppp/%d: dropping pkt (npmode=%d)\n", us->mn, us->npmode);
1120 if ((q = ppa->lowerq) == 0) {
1121 /* try to send it up the control stream */
1122 if (canputnext(ppa->q)) {
1124 * The message seems to get corrupted for some reason if
1125 * we just send the message up as it is, so we send a copy.
1127 mblk_t *np = copymsg(mp);
1130 putnext(ppa->q, np);
1134 if (canputnext(ppa->lowerq)) {
1136 * The lower write queue's put procedure just updates counters
1137 * and does a putnext. We call it so that on SMP systems, we
1138 * enter the lower queues' perimeter so that the counter
1139 * updates are serialized.
1141 put(ppa->lowerq, mp);
1145 us->flags |= US_BLOCKED;
1150 * Allocate a new PPA id and link this stream into the list of PPAs.
1151 * This procedure is called with an exclusive lock on all queues in
1159 upperstr_t *us, **usp;
1164 while ((us = *usp) != 0 && ppa_id == us->ppa_id) {
1168 us = (upperstr_t *) q->q_ptr;
1169 us->ppa_id = ppa_id;
1174 us->flags |= US_CONTROL;
1175 us->npmode = NPMODE_PASS;
1182 * Create a kstats record for our statistics, so netstat -i works.
1184 if (us->kstats == 0) {
1187 sprintf(unit, "ppp%d", us->ppa->ppa_id);
1188 us->kstats = kstat_create("ppp", us->ppa->ppa_id, unit,
1189 "net", KSTAT_TYPE_NAMED, 4, 0);
1190 if (us->kstats != 0) {
1191 kstat_named_t *kn = KSTAT_NAMED_PTR(us->kstats);
1193 strcpy(kn[0].name, "ipackets");
1194 kn[0].data_type = KSTAT_DATA_ULONG;
1195 strcpy(kn[1].name, "ierrors");
1196 kn[1].data_type = KSTAT_DATA_ULONG;
1197 strcpy(kn[2].name, "opackets");
1198 kn[2].data_type = KSTAT_DATA_ULONG;
1199 strcpy(kn[3].name, "oerrors");
1200 kn[3].data_type = KSTAT_DATA_ULONG;
1201 kstat_install(us->kstats);
1206 *(int *)mp->b_cont->b_rptr = ppa_id;
1207 mp->b_datap->db_type = M_IOCACK;
1218 us = (upperstr_t *) q->q_ptr;
1220 us->state = DL_UNBOUND;
1222 for (t = us->ppa; t->next != 0; t = t->next)
1226 if (mp->b_datap->db_type == M_IOCTL) {
1227 mp->b_datap->db_type = M_IOCACK;
1231 dlpi_ok(q, DL_ATTACH_REQ);
1243 us = (upperstr_t *) q->q_ptr;
1244 for (t = us->ppa; t->next != 0; t = t->next)
1245 if (t->next == us) {
1252 us->state = DL_UNATTACHED;
1253 dlpi_ok(q, DL_DETACH_REQ);
1262 struct lowerstr *ls;
1266 us = (upperstr_t *) q->q_ptr;
1267 us->flags &= ~US_BLOCKED;
1268 while ((mp = getq(q)) != 0) {
1269 if (!send_data(mp, us)) {
1284 ppa = (upperstr_t *) q->q_ptr;
1285 if (ppa != 0) { /* why wouldn't it? */
1286 ppa->stats.ppp_opackets++;
1287 ppa->stats.ppp_obytes += msgdsize(mp);
1288 #ifdef INCR_OPACKETS
1303 * Flow control has back-enabled this stream:
1304 * enable the write service procedures of all upper
1305 * streams feeding this lower stream.
1307 for (us = (upperstr_t *) q->q_ptr; us != NULL; us = us->next)
1308 if (us->flags & US_BLOCKED)
1317 upperstr_t *us, *as;
1320 dl_unitdata_ind_t *ud;
1324 us = (upperstr_t *) q->q_ptr;
1325 if (us->flags & US_CONTROL) {
1328 * If there is no lower queue attached, run the write service
1329 * routines of other upper streams attached to this PPA.
1331 if (us->lowerq == 0) {
1334 if (as->flags & US_BLOCKED)
1341 * A network protocol stream. Put a DLPI header on each
1342 * packet and send it on.
1343 * (Actually, it seems that the IP module will happily
1344 * accept M_DATA messages without the DL_UNITDATA_IND header.)
1346 while ((mp = getq(q)) != 0) {
1347 if (!canputnext(q)) {
1352 proto = PPP_PROTOCOL(mp->b_rptr);
1353 mp->b_rptr += PPP_HDRLEN;
1354 hdr = allocb(sizeof(dl_unitdata_ind_t) + 2 * sizeof(ulong),
1357 /* XXX should put it back and use bufcall */
1361 hdr->b_datap->db_type = M_PROTO;
1362 ud = (dl_unitdata_ind_t *) hdr->b_wptr;
1363 hdr->b_wptr += sizeof(dl_unitdata_ind_t) + 2 * sizeof(ulong);
1365 ud->dl_primitive = DL_UNITDATA_IND;
1366 ud->dl_dest_addr_length = sizeof(ulong);
1367 ud->dl_dest_addr_offset = sizeof(dl_unitdata_ind_t);
1368 ud->dl_src_addr_length = sizeof(ulong);
1369 ud->dl_src_addr_offset = ud->dl_dest_addr_offset + sizeof(ulong);
1370 #if DL_CURRENT_VERSION >= 2
1371 ud->dl_group_address = 0;
1373 /* Send the DLPI client the data with the SAP they requested,
1374 (e.g. ETHERTYPE_IP) rather than the PPP protocol number
1376 ((ulong *)(ud + 1))[0] = us->req_sap; /* dest SAP */
1377 ((ulong *)(ud + 1))[1] = us->req_sap; /* src SAP */
1381 #endif /* NO_DLPI */
1386 * If this stream is attached to a PPA with a lower queue pair,
1387 * enable the read queue's service routine if it has data queued.
1388 * XXX there is a possibility that packets could get out of order
1389 * if ppplrput now runs before ppplrsrv.
1391 if (us->ppa != 0 && us->ppa->lowerq != 0)
1392 qenable(RD(us->ppa->lowerq));
1398 find_dest(ppa, proto)
1404 for (us = ppa->next; us != 0; us = us->next)
1405 if (proto == us->sap)
1415 upperstr_t *ppa, *us;
1421 ppa = (upperstr_t *) q->q_ptr;
1423 DPRINT1("ppplrput: q = %x, ppa = 0??\n", q);
1427 switch (mp->b_datap->db_type) {
1429 if (*mp->b_rptr & FLUSHW) {
1430 *mp->b_rptr &= ~FLUSHR;
1437 switch (*mp->b_rptr) {
1442 ppa->stats.ppp_ierrors++;
1448 ppa->stats.ppp_oerrors++;
1457 * Attempt to match up the response with the stream
1458 * that the request came from.
1460 iop = (struct iocblk *) mp->b_rptr;
1461 for (us = ppa; us != 0; us = us->next)
1462 if (us->ioc_id == iop->ioc_id)
1472 * The serial device has hung up. We don't want to send
1473 * the M_HANGUP message up to pppd because that will stop
1474 * us from using the control stream any more. Instead we
1475 * send a zero-length message as an end-of-file indication.
1478 mp = allocb(1, BPRI_HI);
1480 DPRINT1("ppp/%d: couldn't allocate eof message!\n", ppa->mn);
1483 putnext(ppa->q, mp);
1487 if (mp->b_datap->db_type == M_DATA) {
1489 if (mp->b_wptr - mp->b_rptr < PPP_HDRLEN) {
1490 PULLUP(mp, PPP_HDRLEN);
1492 DPRINT1("ppp_lrput: msgpullup failed (len=%d)\n", len);
1496 ppa->stats.ppp_ipackets++;
1497 ppa->stats.ppp_ibytes += len;
1498 #ifdef INCR_IPACKETS
1501 if (!pass_packet(ppa, mp, 0)) {
1505 proto = PPP_PROTOCOL(mp->b_rptr);
1506 if (proto < 0x8000 && (us = find_dest(ppa, proto)) != 0) {
1508 * A data packet for some network protocol.
1509 * Queue it on the upper stream for that protocol.
1519 * A control frame, a frame for an unknown protocol,
1520 * or some other message type.
1521 * Send it up to pppd via the control stream.
1523 if (queclass(mp) == QPCTL || canputnext(ppa->q))
1524 putnext(ppa->q, mp);
1538 upperstr_t *ppa, *us;
1542 * Packets only get queued here for flow control reasons.
1544 ppa = (upperstr_t *) q->q_ptr;
1545 while ((mp = getq(q)) != 0) {
1546 if (mp->b_datap->db_type == M_DATA
1547 && (proto = PPP_PROTOCOL(mp->b_rptr)) < 0x8000
1548 && (us = find_dest(ppa, proto)) != 0) {
1556 if (canputnext(ppa->q))
1557 putnext(ppa->q, mp);
1568 putctl2(q, type, code, val)
1570 int type, code, val;
1574 mp = allocb(2, BPRI_HI);
1577 mp->b_datap->db_type = type;
1578 mp->b_wptr[0] = code;
1579 mp->b_wptr[1] = val;
1586 putctl4(q, type, code, val)
1588 int type, code, val;
1592 mp = allocb(4, BPRI_HI);
1595 mp->b_datap->db_type = type;
1596 mp->b_wptr[0] = code;
1597 ((short *)mp->b_wptr)[1] = val;
1605 queue_t *q; /* not used */
1606 mblk_t *mp; /* not used either */
1611 DPRINT("ppp upper streams:\n");
1612 for (us = minor_devs; us != 0; us = us->nextmn) {
1614 DPRINT3(" %d: q=%x rlev=%d",
1615 us->mn, uq, (uq? qsize(uq): 0));
1616 DPRINT3(" wlev=%d flags=0x%b", (uq? qsize(WR(uq)): 0),
1617 us->flags, "\020\1priv\2control\3blocked\4last");
1618 DPRINT3(" state=%x sap=%x req_sap=%x", us->state, us->sap,
1623 DPRINT1(" ppa=%d\n", us->ppa->ppa_id);
1624 if (us->flags & US_CONTROL) {
1626 DPRINT3(" control for %d lq=%x rlev=%d",
1627 us->ppa_id, lq, (lq? qsize(RD(lq)): 0));
1628 DPRINT3(" wlev=%d mru=%d mtu=%d\n",
1629 (lq? qsize(lq): 0), us->mru, us->mtu);