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.9 1997/03/04 03:31:35 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>
70 * Modifications marked with #ifdef PRIOQ are for priority queueing of
71 * interactive traffic, and are due to Marko Zec <zec@japa.tel.fer.hr>.
74 #include <netinet/in.h>
84 * The IP module may use this SAP value for IP packets.
87 #define ETHERTYPE_IP 0x800
91 #define PPP_MAXMTU 65535
97 * Private information; one per upper stream.
99 typedef struct upperstr {
100 minor_t mn; /* minor device number */
101 struct upperstr *nextmn; /* next minor device */
102 queue_t *q; /* read q associated with this upper stream */
103 int flags; /* flag bits, see below */
104 int state; /* current DLPI state */
105 int sap; /* service access point */
106 int req_sap; /* which SAP the DLPI client requested */
107 struct upperstr *ppa; /* control stream for our ppa */
108 struct upperstr *next; /* next stream for this ppa */
109 uint ioc_id; /* last ioctl ID for this stream */
110 enum NPmode npmode; /* what to do with packets on this SAP */
112 * There is exactly one control stream for each PPA.
113 * The following fields are only used for control streams.
116 queue_t *lowerq; /* write queue attached below this PPA */
117 struct upperstr *nextppa; /* next control stream */
120 struct pppstat stats; /* statistics */
121 time_t last_sent; /* time last NP packet sent */
122 time_t last_recv; /* time last NP packet rcvd */
124 kstat_t *kstats; /* stats for netstat */
128 char ifname[IFNAMSIZ];
129 struct ifstats ifstats;
133 /* Values for flags */
134 #define US_PRIV 1 /* stream was opened by superuser */
135 #define US_CONTROL 2 /* stream is a control stream */
136 #define US_BLOCKED 4 /* flow ctrl has blocked lower write stream */
137 #define US_LASTMOD 8 /* no PPP modules below us */
138 #define US_DBGLOG 0x10 /* log various occurrences */
142 static u_char max_band=0;
143 static u_char def_band=0;
145 #define IPPORT_DEFAULT 65535
148 * Port priority table
149 * Highest priority ports are listed first, lowest are listed last.
150 * ICMP & packets using unlisted ports will be treated as "default".
151 * If IPPORT_DEFAULT is not listed here, "default" packets will be
152 * assigned lowest priority.
153 * Each line should be terminated with "0".
154 * Line containing only "0" marks the end of the list.
157 static u_short prioq_table[]= {
159 22, 23, 513, 517, 518, 0,
163 20, 70, 80, 8001, 8008, 8080, 0, /* 8001,8008,8080 - common proxy ports */
169 static upperstr_t *minor_devs = NULL;
170 static upperstr_t *ppas = NULL;
173 static int pppopen __P((queue_t *, dev_t *, int, int, cred_t *));
174 static int pppclose __P((queue_t *, int, cred_t *));
176 static int pppopen __P((queue_t *, int, int, int));
177 static int pppclose __P((queue_t *, int));
179 static int pppuwput __P((queue_t *, mblk_t *));
180 static int pppursrv __P((queue_t *));
181 static int pppuwsrv __P((queue_t *));
182 static int ppplrput __P((queue_t *, mblk_t *));
183 static int ppplwput __P((queue_t *, mblk_t *));
184 static int ppplrsrv __P((queue_t *));
185 static int ppplwsrv __P((queue_t *));
187 static void dlpi_request __P((queue_t *, mblk_t *, upperstr_t *));
188 static void dlpi_error __P((queue_t *, int, int, int));
189 static void dlpi_ok __P((queue_t *, int));
191 static int send_data __P((mblk_t *, upperstr_t *));
192 static void new_ppa __P((queue_t *, mblk_t *));
193 static void attach_ppa __P((queue_t *, mblk_t *));
194 static void detach_ppa __P((queue_t *, mblk_t *));
195 static void debug_dump __P((queue_t *, mblk_t *));
196 static upperstr_t *find_dest __P((upperstr_t *, int));
197 static int putctl2 __P((queue_t *, int, int, int));
198 static int putctl4 __P((queue_t *, int, int, int));
199 static int pass_packet __P((upperstr_t *ppa, mblk_t *mp, int outbound));
201 #define PPP_ID 0xb1a6
202 static struct module_info ppp_info = {
204 PPP_ID, "ppp", 0, 512, 512, 384
206 PPP_ID, "ppp", 0, 512, 512, 128
210 static struct qinit pppurint = {
211 NULL, pppursrv, pppopen, pppclose, NULL, &ppp_info, NULL
214 static struct qinit pppuwint = {
215 pppuwput, pppuwsrv, NULL, NULL, NULL, &ppp_info, NULL
218 static struct qinit ppplrint = {
219 ppplrput, ppplrsrv, NULL, NULL, NULL, &ppp_info, NULL
222 static struct qinit ppplwint = {
223 ppplwput, ppplwsrv, NULL, NULL, NULL, &ppp_info, NULL
227 extern struct ifstats *ifstats;
231 struct streamtab pppinfo = {
232 &pppurint, &pppuwint,
239 * How we maintain statistics.
242 #define INCR_IPACKETS(ppa) \
243 if (ppa->kstats != 0) { \
244 KSTAT_NAMED_PTR(ppa->kstats)[0].value.ul++; \
246 #define INCR_IERRORS(ppa) \
247 if (ppa->kstats != 0) { \
248 KSTAT_NAMED_PTR(ppa->kstats)[1].value.ul++; \
250 #define INCR_OPACKETS(ppa) \
251 if (ppa->kstats != 0) { \
252 KSTAT_NAMED_PTR(ppa->kstats)[2].value.ul++; \
254 #define INCR_OERRORS(ppa) \
255 if (ppa->kstats != 0) { \
256 KSTAT_NAMED_PTR(ppa->kstats)[3].value.ul++; \
261 #define INCR_IPACKETS(ppa) ppa->ifstats.ifs_ipackets++;
262 #define INCR_IERRORS(ppa) ppa->ifstats.ifs_ierrors++;
263 #define INCR_OPACKETS(ppa) ppa->ifstats.ifs_opackets++;
264 #define INCR_OERRORS(ppa) ppa->ifstats.ifs_oerrors++;
268 * STREAMS driver entry points.
272 pppopen(q, devp, oflag, sflag, credp)
278 pppopen(q, dev, oflag, sflag)
280 int dev; /* really dev_t */
293 DRV_OPEN_OK(dev); /* device is already open */
296 /* Calculate max_bband & def_band from definitions in prioq.h
297 This colud be done at some more approtiate time (less often)
298 but this way it works well so I'll just leave it here */
306 if (*ptr++ == IPPORT_DEFAULT) {
310 max_band += new_band;
314 def_band = max_band - def_band;
318 if (sflag == CLONEOPEN) {
320 for (prevp = &minor_devs; (up = *prevp) != 0; prevp = &up->nextmn) {
327 mn = getminor(*devp);
331 for (prevp = &minor_devs; (up = *prevp) != 0; prevp = &up->nextmn) {
336 /* this can't happen */
337 q->q_ptr = WR(q)->q_ptr = (caddr_t) up;
343 * Construct a new minor node.
345 up = (upperstr_t *) ALLOC_SLEEP(sizeof(upperstr_t));
346 bzero((caddr_t) up, sizeof(upperstr_t));
348 DPRINT("pppopen: out of kernel memory\n");
355 *devp = makedevice(getmajor(*devp), mn);
359 up->flags |= US_PRIV;
361 up->state = DL_UNATTACHED;
364 up->ifflags = IFF_UP | IFF_POINTOPOINT;
367 up->last_sent = up->last_recv = time;
368 up->npmode = NPMODE_DROP;
369 q->q_ptr = (caddr_t) up;
370 WR(q)->q_ptr = (caddr_t) up;
375 DRV_OPEN_OK(makedev(major(dev), mn));
380 pppclose(q, flag, credp)
390 upperstr_t *up, **upp;
391 upperstr_t *as, *asnext;
396 up = (upperstr_t *) q->q_ptr;
397 if (up->flags & US_DBGLOG)
398 DPRINT2("ppp/%d: close, flags=%x\n", up->mn, up->flags);
401 if (up->flags & US_CONTROL) {
403 struct ifstats *ifp, *pifp;
406 * This stream represents a PPA:
407 * For all streams attached to the PPA, clear their
408 * references to this PPA.
409 * Then remove this PPA from the list of PPAs.
411 for (as = up->next; as != 0; as = asnext) {
415 if (as->flags & US_BLOCKED) {
416 as->flags &= ~US_BLOCKED;
417 flushq(WR(as->q), FLUSHDATA);
420 for (upp = &ppas; *upp != 0; upp = &(*upp)->nextppa)
426 /* Remove the statistics from the active list. */
427 for (ifp = ifstats, pifp = 0; ifp; ifp = ifp->ifs_next) {
428 if (ifp == &up->ifstats) {
430 pifp->ifs_next = ifp->ifs_next;
432 ifstats = ifp->ifs_next;
440 * If this stream is attached to a PPA,
441 * remove it from the PPA's list.
443 if ((as = up->ppa) != 0) {
444 for (; as->next != 0; as = as->next)
445 if (as->next == up) {
454 kstat_delete(up->kstats);
460 for (prevp = &minor_devs; *prevp != 0; prevp = &(*prevp)->nextmn) {
466 FREE(up, sizeof(upperstr_t));
473 * A message from on high. We do one of three things:
475 * - put the message on the lower write stream
476 * - queue it for our service routine
483 upperstr_t *us, *usnext, *ppa, *os, *nps;
493 struct ppp_idle *pip;
499 us = (upperstr_t *) q->q_ptr;
500 switch (mp->b_datap->db_type) {
504 dlpi_request(q, mp, us);
509 if (us->flags & US_DBGLOG)
510 DPRINT3("ppp/%d: uwput M_DATA len=%d flags=%x\n",
511 us->mn, msgdsize(mp), us->flags);
512 if (us->ppa == 0 || msgdsize(mp) > us->ppa->mtu + PPP_HDRLEN
514 || (us->flags & US_CONTROL) == 0
517 DPRINT1("pppuwput: junk data len=%d\n", msgdsize(mp));
522 if (!pass_packet(us->ppa, mp, 1)) {
527 if (!send_data(mp, us))
532 iop = (struct iocblk *) mp->b_rptr;
534 if (us->flags & US_DBGLOG)
535 DPRINT3("ppp/%d: ioctl %x count=%d\n",
536 us->mn, iop->ioc_cmd, iop->ioc_count);
537 switch (iop->ioc_cmd) {
539 if ((us->flags & US_CONTROL) == 0 || us->lowerq != 0)
541 lb = (struct linkblk *) mp->b_cont->b_rptr;
542 us->lowerq = lq = lb->l_qbot;
543 lq->q_ptr = (caddr_t) us;
544 RD(lq)->q_ptr = (caddr_t) us;
546 flushq(RD(lq), FLUSHALL);
549 us->flags &= ~US_LASTMOD;
550 /* Unblock upper streams which now feed this lower stream. */
552 /* Send useful information down to the modules which
553 are now linked below us. */
554 putctl2(lq, M_CTL, PPPCTL_UNIT, us->ppa_id);
555 putctl4(lq, M_CTL, PPPCTL_MRU, us->mru);
556 putctl4(lq, M_CTL, PPPCTL_MTU, us->mtu);
558 /* Lower tty driver's queue hiwat/lowat from default 4096/128
559 to 256/128 since we don't want queueing of data on
560 output to physical device */
563 for (tlq = lq; tlq->q_next != NULL; tlq = tlq->q_next)
565 strqset(tlq, QHIWAT, 0, 256);
566 strqset(tlq, QLOWAT, 0, 128);
572 lb = (struct linkblk *) mp->b_cont->b_rptr;
574 if (us->lowerq != lb->l_qbot)
575 DPRINT2("ppp unlink: lowerq=%x qbot=%x\n",
576 us->lowerq, lb->l_qbot);
581 /* Unblock streams which now feed back up the control stream. */
586 if (us->flags & US_CONTROL)
588 if ((us->flags & US_PRIV) == 0) {
592 /* Arrange to return an int */
593 if ((mq = mp->b_cont) == 0
594 || mq->b_datap->db_lim - mq->b_rptr < sizeof(int)) {
595 mq = allocb(sizeof(int), BPRI_HI);
605 iop->ioc_count = sizeof(int);
606 mq->b_wptr = mq->b_rptr + sizeof(int);
607 qwriter(q, mp, new_ppa, PERIM_OUTER);
612 /* like dlpi_attach, for programs which can't write to
613 the stream (like pppstats) */
614 if (iop->ioc_count != sizeof(int) || us->ppa != 0)
616 n = *(int *)mp->b_cont->b_rptr;
617 for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
618 if (ppa->ppa_id == n)
624 qwriter(q, mp, attach_ppa, PERIM_OUTER);
630 /* Attach to a given SAP. */
631 if (iop->ioc_count != sizeof(int) || us->ppa == 0)
633 n = *(int *)mp->b_cont->b_rptr;
634 /* n must be a valid PPP network protocol number. */
635 if (n < 0x21 || n > 0x3fff || (n & 0x101) != 1)
637 /* check that no other stream is bound to this sap already. */
638 for (os = us->ppa; os != 0; os = os->next)
650 if (iop->ioc_count != sizeof(int) || (us->flags & US_CONTROL) == 0)
652 n = *(int *)mp->b_cont->b_rptr;
653 if (n <= 0 || n > PPP_MAXMTU)
659 putctl4(us->lowerq, M_CTL, PPPCTL_MRU, n);
665 if (iop->ioc_count != sizeof(int) || (us->flags & US_CONTROL) == 0)
667 n = *(int *)mp->b_cont->b_rptr;
668 if (n <= 0 || n > PPP_MAXMTU)
674 us->ifstats.ifs_mtu = n;
677 putctl4(us->lowerq, M_CTL, PPPCTL_MTU, n);
683 us->flags |= US_LASTMOD;
688 if (iop->ioc_count != sizeof(int))
690 n = *(int *)mp->b_cont->b_rptr;
691 if (n == PPPDBG_DUMP + PPPDBG_DRIVER) {
692 qwriter(q, NULL, debug_dump, PERIM_OUTER);
695 } else if (n == PPPDBG_LOG + PPPDBG_DRIVER) {
696 DPRINT1("ppp/%d: debug log enabled\n", us->mn);
697 us->flags |= US_DBGLOG;
701 if (us->ppa == 0 || us->ppa->lowerq == 0)
703 putnext(us->ppa->lowerq, mp);
709 if (iop->ioc_count != 2 * sizeof(int))
711 if ((us->flags & US_CONTROL) == 0)
713 sap = ((int *)mp->b_cont->b_rptr)[0];
714 for (nps = us->next; nps != 0; nps = nps->next)
718 if (us->flags & US_DBGLOG)
719 DPRINT2("ppp/%d: no stream for sap %x\n", us->mn, sap);
722 nps->npmode = (enum NPmode) ((int *)mp->b_cont->b_rptr)[1];
723 if (nps->npmode == NPMODE_DROP || nps->npmode == NPMODE_ERROR)
724 flushq(WR(nps->q), FLUSHDATA);
725 else if (nps->npmode == NPMODE_PASS && qsize(WR(nps->q)) > 0
726 && (nps->flags & US_BLOCKED) == 0)
733 if ((ppa = us->ppa) == 0)
735 mq = allocb(sizeof(struct ppp_idle), BPRI_HI);
744 pip = (struct ppp_idle *) mq->b_wptr;
745 pip->xmit_idle = time - ppa->last_sent;
746 pip->recv_idle = time - ppa->last_recv;
747 mq->b_wptr += sizeof(struct ppp_idle);
748 iop->ioc_count = sizeof(struct ppp_idle);
754 /* Sent from IP down to us. Attach the ifstats structure. */
755 if (iop->ioc_count != sizeof(struct ifreq) || us->ppa == 0)
757 ifr = (struct ifreq *)mp->b_cont->b_rptr;
758 /* Find the unit number in the interface name. */
759 for (i = 0; i < IFNAMSIZ; i++) {
760 if (ifr->ifr_name[i] == 0 ||
761 (ifr->ifr_name[i] >= '0' &&
762 ifr->ifr_name[i] <= '9'))
765 us->ifname[i] = ifr->ifr_name[i];
769 /* Convert the unit number to binary. */
770 for (n = 0; i < IFNAMSIZ; i++) {
771 if (ifr->ifr_name[i] == 0) {
775 n = n * 10 + ifr->ifr_name[i] - '0';
779 /* Verify the ppa. */
780 if (us->ppa->ppa_id != n)
784 /* Set up the netstat block. */
785 strncpy (ppa->ifname, us->ifname, IFNAMSIZ);
787 ppa->ifstats.ifs_name = ppa->ifname;
788 ppa->ifstats.ifs_unit = n;
789 ppa->ifstats.ifs_active = us->state != DL_UNBOUND;
790 ppa->ifstats.ifs_mtu = ppa->mtu;
792 /* Link in statistics used by netstat. */
793 ppa->ifstats.ifs_next = ifstats;
794 ifstats = &ppa->ifstats;
801 if (!(us->flags & US_CONTROL)) {
807 ((struct iocblk_in *)iop)->ioc_ifflags = us->ifflags;
812 if (!(us->flags & US_CONTROL)) {
818 us->ifflags = ((struct iocblk_in *)iop)->ioc_ifflags;
823 if (!(us->flags & US_CONTROL)) {
829 us->ifflags |= IFF_RUNNING;
830 ((struct iocblk_in *)iop)->ioc_ifflags |= IFF_RUNNING;
845 if (us->ppa == 0 || us->ppa->lowerq == 0)
847 us->ioc_id = iop->ioc_id;
849 switch (iop->ioc_cmd) {
852 if (us->flags & US_LASTMOD) {
856 putnext(us->ppa->lowerq, mp);
859 if (us->flags & US_PRIV)
860 putnext(us->ppa->lowerq, mp);
862 DPRINT1("ppp ioctl %x rejected\n", iop->ioc_cmd);
871 iop->ioc_error = error;
872 mp->b_datap->db_type = M_IOCNAK;
874 } else if (error == 0) {
875 mp->b_datap->db_type = M_IOCACK;
881 if (us->flags & US_DBGLOG)
882 DPRINT2("ppp/%d: flush %x\n", us->mn, *mp->b_rptr);
883 if (*mp->b_rptr & FLUSHW)
884 flushq(q, FLUSHDATA);
885 if (*mp->b_rptr & FLUSHR) {
886 *mp->b_rptr &= ~FLUSHW;
901 dlpi_request(q, mp, us)
906 union DL_primitives *d = (union DL_primitives *) mp->b_rptr;
907 int size = mp->b_wptr - mp->b_rptr;
909 upperstr_t *ppa, *os;
914 if (us->flags & US_DBGLOG)
915 DPRINT3("ppp/%d: dlpi prim %x len=%d\n", us->mn,
916 d->dl_primitive, size);
917 switch (d->dl_primitive) {
919 if (size < sizeof(dl_info_req_t))
921 if ((reply = allocb(sizeof(dl_info_ack_t), BPRI_HI)) == 0)
922 break; /* should do bufcall */
923 reply->b_datap->db_type = M_PCPROTO;
924 info = (dl_info_ack_t *) reply->b_wptr;
925 reply->b_wptr += sizeof(dl_info_ack_t);
926 bzero((caddr_t) info, sizeof(dl_info_ack_t));
927 info->dl_primitive = DL_INFO_ACK;
928 info->dl_max_sdu = PPP_MAXMTU;
929 info->dl_min_sdu = 1;
930 info->dl_addr_length = sizeof(ulong);
932 info->dl_mac_type = DL_OTHER;
934 info->dl_mac_type = DL_HDLC; /* a lie */
936 info->dl_current_state = us->state;
937 info->dl_service_mode = DL_CLDLS;
938 info->dl_provider_style = DL_STYLE2;
939 #if DL_CURRENT_VERSION >= 2
940 info->dl_sap_length = sizeof(ulong);
941 info->dl_version = DL_CURRENT_VERSION;
947 if (size < sizeof(dl_attach_req_t))
949 if (us->state != DL_UNATTACHED || us->ppa != 0) {
950 dlpi_error(q, DL_ATTACH_REQ, DL_OUTSTATE, 0);
953 for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
954 if (ppa->ppa_id == d->attach_req.dl_ppa)
957 dlpi_error(q, DL_ATTACH_REQ, DL_BADPPA, 0);
961 qwriter(q, mp, attach_ppa, PERIM_OUTER);
965 if (size < sizeof(dl_detach_req_t))
967 if (us->state != DL_UNBOUND || us->ppa == 0) {
968 dlpi_error(q, DL_DETACH_REQ, DL_OUTSTATE, 0);
971 qwriter(q, mp, detach_ppa, PERIM_OUTER);
975 if (size < sizeof(dl_bind_req_t))
977 if (us->state != DL_UNBOUND || us->ppa == 0) {
978 dlpi_error(q, DL_BIND_REQ, DL_OUTSTATE, 0);
981 if (d->bind_req.dl_service_mode != DL_CLDLS) {
982 dlpi_error(q, DL_BIND_REQ, DL_UNSUPPORTED, 0);
986 /* saps must be valid PPP network protocol numbers,
987 except that we accept ETHERTYPE_IP in place of PPP_IP. */
988 sap = d->bind_req.dl_sap;
990 if (sap == ETHERTYPE_IP)
992 if (sap < 0x21 || sap > 0x3fff || (sap & 0x101) != 1) {
993 dlpi_error(q, DL_BIND_REQ, DL_BADADDR, 0);
997 /* check that no other stream is bound to this sap already. */
998 for (os = us->ppa; os != 0; os = os->next)
1002 dlpi_error(q, DL_BIND_REQ, DL_NOADDR, 0);
1007 us->state = DL_IDLE;
1009 if ((reply = allocb(sizeof(dl_bind_ack_t) + sizeof(ulong),
1011 break; /* should do bufcall */
1012 ackp = (dl_bind_ack_t *) reply->b_wptr;
1013 reply->b_wptr += sizeof(dl_bind_ack_t) + sizeof(ulong);
1014 reply->b_datap->db_type = M_PCPROTO;
1015 bzero((caddr_t) ackp, sizeof(dl_bind_ack_t));
1016 ackp->dl_primitive = DL_BIND_ACK;
1018 ackp->dl_addr_length = sizeof(ulong);
1019 ackp->dl_addr_offset = sizeof(dl_bind_ack_t);
1020 *(ulong *)(ackp+1) = sap;
1025 if (size < sizeof(dl_unbind_req_t))
1027 if (us->state != DL_IDLE) {
1028 dlpi_error(q, DL_UNBIND_REQ, DL_OUTSTATE, 0);
1032 us->state = DL_UNBOUND;
1034 us->ppa->ifstats.ifs_active = 0;
1036 dlpi_ok(q, DL_UNBIND_REQ);
1039 case DL_UNITDATA_REQ:
1040 if (size < sizeof(dl_unitdata_req_t))
1042 if (us->state != DL_IDLE) {
1043 dlpi_error(q, DL_UNITDATA_REQ, DL_OUTSTATE, 0);
1046 if ((ppa = us->ppa) == 0) {
1047 cmn_err(CE_CONT, "ppp: in state dl_idle but ppa == 0?\n");
1050 len = mp->b_cont == 0? 0: msgdsize(mp->b_cont);
1051 if (len > ppa->mtu) {
1052 DPRINT2("dlpi data too large (%d > %d)\n", len, ppa->mtu);
1057 /* Extract s_port & d_port from IP-packet, the code is a bit
1058 dirty here, but so am I, too... */
1059 if (mp->b_datap->db_type == M_PROTO && us->sap == PPP_IP
1060 && mp->b_cont != 0) {
1064 u_char band_unset, cur_band, syn;
1065 u_short s_port, d_port;
1067 bb = mp->b_cont->b_rptr; /* bb points to IP-header*/
1068 len = mp->b_cont->b_wptr - mp->b_cont->b_rptr;
1070 s_port = IPPORT_DEFAULT;
1071 d_port = IPPORT_DEFAULT;
1072 if (len >= 20) { /* 20 = minimum length of IP header */
1073 iphlen = (bb[0] & 0x0f) * 4;
1078 if (len >= 20) { /* min length of TCP header */
1079 s_port = (tlh[0] << 8) + tlh[1];
1080 d_port = (tlh[2] << 8) + tlh[3];
1081 syn = tlh[13] & 0x02;
1085 if (len >= 8) { /* min length of UDP header */
1086 s_port = (tlh[0] << 8) + tlh[1];
1087 d_port = (tlh[2] << 8) + tlh[3];
1094 * Now calculate b_band for this packet from the
1095 * port-priority table.
1098 cur_band = max_band;
1101 while (*ptr && band_unset)
1102 if (s_port == *ptr || d_port == *ptr++) {
1103 mp->b_band = cur_band;
1111 mp->b_band = def_band;
1112 /* It may be usable to urge SYN packets a bit */
1117 /* this assumes PPP_HDRLEN <= sizeof(dl_unitdata_req_t) */
1118 if (mp->b_datap->db_ref > 1) {
1119 np = allocb(PPP_HDRLEN, BPRI_HI);
1122 np->b_cont = mp->b_cont;
1127 mp->b_datap->db_type = M_DATA;
1128 /* XXX should use dl_dest_addr_offset/length here,
1129 but we would have to translate ETHERTYPE_IP -> PPP_IP */
1130 mp->b_wptr = mp->b_rptr + PPP_HDRLEN;
1131 mp->b_rptr[0] = PPP_ALLSTATIONS;
1132 mp->b_rptr[1] = PPP_UI;
1133 mp->b_rptr[2] = us->sap >> 8;
1134 mp->b_rptr[3] = us->sap;
1135 if (!pass_packet(ppa, mp, 1)) {
1138 if (!send_data(mp, us))
1143 #if DL_CURRENT_VERSION >= 2
1144 case DL_SUBS_BIND_REQ:
1145 case DL_SUBS_UNBIND_REQ:
1146 case DL_ENABMULTI_REQ:
1147 case DL_DISABMULTI_REQ:
1148 case DL_PROMISCON_REQ:
1149 case DL_PROMISCOFF_REQ:
1150 case DL_PHYS_ADDR_REQ:
1151 case DL_SET_PHYS_ADDR_REQ:
1154 case DL_REPLY_UPDATE_REQ:
1156 case DL_DATA_ACK_REQ:
1158 case DL_CONNECT_REQ:
1160 dlpi_error(q, d->dl_primitive, DL_NOTSUPPORTED, 0);
1163 case DL_CONNECT_RES:
1164 case DL_DISCONNECT_REQ:
1167 dlpi_error(q, d->dl_primitive, DL_OUTSTATE, 0);
1171 dlpi_error(q, d->dl_primitive, DL_BADQOSTYPE, 0);
1174 #if DL_CURRENT_VERSION >= 2
1181 cmn_err(CE_CONT, "ppp: unknown dlpi prim 0x%x\n", d->dl_primitive);
1184 dlpi_error(q, d->dl_primitive, DL_BADPRIM, 0);
1191 dlpi_error(q, prim, err, uerr)
1193 int prim, err, uerr;
1196 dl_error_ack_t *errp;
1198 reply = allocb(sizeof(dl_error_ack_t), BPRI_HI);
1200 return; /* XXX should do bufcall */
1201 reply->b_datap->db_type = M_PCPROTO;
1202 errp = (dl_error_ack_t *) reply->b_wptr;
1203 reply->b_wptr += sizeof(dl_error_ack_t);
1204 errp->dl_primitive = DL_ERROR_ACK;
1205 errp->dl_error_primitive = prim;
1206 errp->dl_errno = err;
1207 errp->dl_unix_errno = uerr;
1219 reply = allocb(sizeof(dl_ok_ack_t), BPRI_HI);
1221 return; /* XXX should do bufcall */
1222 reply->b_datap->db_type = M_PCPROTO;
1223 okp = (dl_ok_ack_t *) reply->b_wptr;
1224 reply->b_wptr += sizeof(dl_ok_ack_t);
1225 okp->dl_primitive = DL_OK_ACK;
1226 okp->dl_correct_primitive = prim;
1229 #endif /* NO_DLPI */
1232 pass_packet(ppa, mp, outbound)
1238 * Here is where we might, in future, decide whether to pass
1239 * or drop the packet, and whether it counts as link activity.
1242 ppa->last_sent = time;
1244 ppa->last_recv = time;
1256 if ((us->flags & US_BLOCKED) || us->npmode == NPMODE_QUEUE)
1259 if (ppa == 0 || us->npmode == NPMODE_DROP || us->npmode == NPMODE_ERROR) {
1260 if (us->flags & US_DBGLOG)
1261 DPRINT2("ppp/%d: dropping pkt (npmode=%d)\n", us->mn, us->npmode);
1265 if ((q = ppa->lowerq) == 0) {
1266 /* try to send it up the control stream */
1267 if (bcanputnext(ppa->q, mp->b_band)) {
1269 * The message seems to get corrupted for some reason if
1270 * we just send the message up as it is, so we send a copy.
1272 mblk_t *np = copymsg(mp);
1275 putnext(ppa->q, np);
1279 if (bcanputnext(ppa->lowerq, mp->b_band)) {
1281 * The lower write queue's put procedure just updates counters
1282 * and does a putnext. We call it so that on SMP systems, we
1283 * enter the lower queues' perimeter so that the counter
1284 * updates are serialized.
1286 put(ppa->lowerq, mp);
1290 us->flags |= US_BLOCKED;
1295 * Allocate a new PPA id and link this stream into the list of PPAs.
1296 * This procedure is called with an exclusive lock on all queues in
1304 upperstr_t *us, **usp;
1309 while ((us = *usp) != 0 && ppa_id == us->ppa_id) {
1313 us = (upperstr_t *) q->q_ptr;
1314 us->ppa_id = ppa_id;
1319 us->flags |= US_CONTROL;
1320 us->npmode = NPMODE_PASS;
1327 * Create a kstats record for our statistics, so netstat -i works.
1329 if (us->kstats == 0) {
1332 sprintf(unit, "ppp%d", us->ppa->ppa_id);
1333 us->kstats = kstat_create("ppp", us->ppa->ppa_id, unit,
1334 "net", KSTAT_TYPE_NAMED, 4, 0);
1335 if (us->kstats != 0) {
1336 kstat_named_t *kn = KSTAT_NAMED_PTR(us->kstats);
1338 strcpy(kn[0].name, "ipackets");
1339 kn[0].data_type = KSTAT_DATA_ULONG;
1340 strcpy(kn[1].name, "ierrors");
1341 kn[1].data_type = KSTAT_DATA_ULONG;
1342 strcpy(kn[2].name, "opackets");
1343 kn[2].data_type = KSTAT_DATA_ULONG;
1344 strcpy(kn[3].name, "oerrors");
1345 kn[3].data_type = KSTAT_DATA_ULONG;
1346 kstat_install(us->kstats);
1351 *(int *)mp->b_cont->b_rptr = ppa_id;
1352 mp->b_datap->db_type = M_IOCACK;
1363 us = (upperstr_t *) q->q_ptr;
1365 us->state = DL_UNBOUND;
1367 for (t = us->ppa; t->next != 0; t = t->next)
1371 if (mp->b_datap->db_type == M_IOCTL) {
1372 mp->b_datap->db_type = M_IOCACK;
1376 dlpi_ok(q, DL_ATTACH_REQ);
1388 us = (upperstr_t *) q->q_ptr;
1389 for (t = us->ppa; t->next != 0; t = t->next)
1390 if (t->next == us) {
1397 us->state = DL_UNATTACHED;
1398 dlpi_ok(q, DL_DETACH_REQ);
1407 struct lowerstr *ls;
1411 us = (upperstr_t *) q->q_ptr;
1412 us->flags &= ~US_BLOCKED;
1413 while ((mp = getq(q)) != 0) {
1414 if (!send_data(mp, us)) {
1429 ppa = (upperstr_t *) q->q_ptr;
1430 if (ppa != 0) { /* why wouldn't it? */
1431 ppa->stats.ppp_opackets++;
1432 ppa->stats.ppp_obytes += msgdsize(mp);
1433 #ifdef INCR_OPACKETS
1448 * Flow control has back-enabled this stream:
1449 * enable the write service procedures of all upper
1450 * streams feeding this lower stream.
1452 for (us = (upperstr_t *) q->q_ptr; us != NULL; us = us->next)
1453 if (us->flags & US_BLOCKED)
1462 upperstr_t *us, *as;
1465 dl_unitdata_ind_t *ud;
1469 us = (upperstr_t *) q->q_ptr;
1470 if (us->flags & US_CONTROL) {
1473 * If there is no lower queue attached, run the write service
1474 * routines of other upper streams attached to this PPA.
1476 if (us->lowerq == 0) {
1479 if (as->flags & US_BLOCKED)
1486 * A network protocol stream. Put a DLPI header on each
1487 * packet and send it on.
1488 * (Actually, it seems that the IP module will happily
1489 * accept M_DATA messages without the DL_UNITDATA_IND header.)
1491 while ((mp = getq(q)) != 0) {
1492 if (!canputnext(q)) {
1497 proto = PPP_PROTOCOL(mp->b_rptr);
1498 mp->b_rptr += PPP_HDRLEN;
1499 hdr = allocb(sizeof(dl_unitdata_ind_t) + 2 * sizeof(ulong),
1502 /* XXX should put it back and use bufcall */
1506 hdr->b_datap->db_type = M_PROTO;
1507 ud = (dl_unitdata_ind_t *) hdr->b_wptr;
1508 hdr->b_wptr += sizeof(dl_unitdata_ind_t) + 2 * sizeof(ulong);
1510 ud->dl_primitive = DL_UNITDATA_IND;
1511 ud->dl_dest_addr_length = sizeof(ulong);
1512 ud->dl_dest_addr_offset = sizeof(dl_unitdata_ind_t);
1513 ud->dl_src_addr_length = sizeof(ulong);
1514 ud->dl_src_addr_offset = ud->dl_dest_addr_offset + sizeof(ulong);
1515 #if DL_CURRENT_VERSION >= 2
1516 ud->dl_group_address = 0;
1518 /* Send the DLPI client the data with the SAP they requested,
1519 (e.g. ETHERTYPE_IP) rather than the PPP protocol number
1521 ((ulong *)(ud + 1))[0] = us->req_sap; /* dest SAP */
1522 ((ulong *)(ud + 1))[1] = us->req_sap; /* src SAP */
1526 #endif /* NO_DLPI */
1531 * If this stream is attached to a PPA with a lower queue pair,
1532 * enable the read queue's service routine if it has data queued.
1533 * XXX there is a possibility that packets could get out of order
1534 * if ppplrput now runs before ppplrsrv.
1536 if (us->ppa != 0 && us->ppa->lowerq != 0)
1537 qenable(RD(us->ppa->lowerq));
1543 find_dest(ppa, proto)
1549 for (us = ppa->next; us != 0; us = us->next)
1550 if (proto == us->sap)
1560 upperstr_t *ppa, *us;
1566 ppa = (upperstr_t *) q->q_ptr;
1568 DPRINT1("ppplrput: q = %x, ppa = 0??\n", q);
1572 switch (mp->b_datap->db_type) {
1574 if (*mp->b_rptr & FLUSHW) {
1575 *mp->b_rptr &= ~FLUSHR;
1582 switch (*mp->b_rptr) {
1587 ppa->stats.ppp_ierrors++;
1593 ppa->stats.ppp_oerrors++;
1602 * Attempt to match up the response with the stream
1603 * that the request came from.
1605 iop = (struct iocblk *) mp->b_rptr;
1606 for (us = ppa; us != 0; us = us->next)
1607 if (us->ioc_id == iop->ioc_id)
1617 * The serial device has hung up. We don't want to send
1618 * the M_HANGUP message up to pppd because that will stop
1619 * us from using the control stream any more. Instead we
1620 * send a zero-length message as an end-of-file indication.
1623 mp = allocb(1, BPRI_HI);
1625 DPRINT1("ppp/%d: couldn't allocate eof message!\n", ppa->mn);
1628 putnext(ppa->q, mp);
1632 if (mp->b_datap->db_type == M_DATA) {
1634 if (mp->b_wptr - mp->b_rptr < PPP_HDRLEN) {
1635 PULLUP(mp, PPP_HDRLEN);
1637 DPRINT1("ppp_lrput: msgpullup failed (len=%d)\n", len);
1641 ppa->stats.ppp_ipackets++;
1642 ppa->stats.ppp_ibytes += len;
1643 #ifdef INCR_IPACKETS
1646 if (!pass_packet(ppa, mp, 0)) {
1650 proto = PPP_PROTOCOL(mp->b_rptr);
1651 if (proto < 0x8000 && (us = find_dest(ppa, proto)) != 0) {
1653 * A data packet for some network protocol.
1654 * Queue it on the upper stream for that protocol.
1664 * A control frame, a frame for an unknown protocol,
1665 * or some other message type.
1666 * Send it up to pppd via the control stream.
1668 if (queclass(mp) == QPCTL || canputnext(ppa->q))
1669 putnext(ppa->q, mp);
1683 upperstr_t *ppa, *us;
1687 * Packets only get queued here for flow control reasons.
1689 ppa = (upperstr_t *) q->q_ptr;
1690 while ((mp = getq(q)) != 0) {
1691 if (mp->b_datap->db_type == M_DATA
1692 && (proto = PPP_PROTOCOL(mp->b_rptr)) < 0x8000
1693 && (us = find_dest(ppa, proto)) != 0) {
1701 if (canputnext(ppa->q))
1702 putnext(ppa->q, mp);
1713 putctl2(q, type, code, val)
1715 int type, code, val;
1719 mp = allocb(2, BPRI_HI);
1722 mp->b_datap->db_type = type;
1723 mp->b_wptr[0] = code;
1724 mp->b_wptr[1] = val;
1731 putctl4(q, type, code, val)
1733 int type, code, val;
1737 mp = allocb(4, BPRI_HI);
1740 mp->b_datap->db_type = type;
1741 mp->b_wptr[0] = code;
1742 ((short *)mp->b_wptr)[1] = val;
1750 queue_t *q; /* not used */
1751 mblk_t *mp; /* not used either */
1756 DPRINT("ppp upper streams:\n");
1757 for (us = minor_devs; us != 0; us = us->nextmn) {
1759 DPRINT3(" %d: q=%x rlev=%d",
1760 us->mn, uq, (uq? qsize(uq): 0));
1761 DPRINT3(" wlev=%d flags=0x%b", (uq? qsize(WR(uq)): 0),
1762 us->flags, "\020\1priv\2control\3blocked\4last");
1763 DPRINT3(" state=%x sap=%x req_sap=%x", us->state, us->sap,
1768 DPRINT1(" ppa=%d\n", us->ppa->ppa_id);
1769 if (us->flags & US_CONTROL) {
1771 DPRINT3(" control for %d lq=%x rlev=%d",
1772 us->ppa_id, lq, (lq? qsize(RD(lq)): 0));
1773 DPRINT3(" wlev=%d mru=%d mtu=%d\n",
1774 (lq? qsize(lq): 0), us->mru, us->mtu);