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.15 1998/05/04 06:11: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
93 * Private information; one per upper stream.
95 typedef struct upperstr {
96 minor_t mn; /* minor device number */
97 struct upperstr *nextmn; /* next minor device */
98 queue_t *q; /* read q associated with this upper stream */
99 int flags; /* flag bits, see below */
100 int state; /* current DLPI state */
101 int sap; /* service access point */
102 int req_sap; /* which SAP the DLPI client requested */
103 struct upperstr *ppa; /* control stream for our ppa */
104 struct upperstr *next; /* next stream for this ppa */
105 uint ioc_id; /* last ioctl ID for this stream */
106 enum NPmode npmode; /* what to do with packets on this SAP */
108 * There is exactly one control stream for each PPA.
109 * The following fields are only used for control streams.
112 queue_t *lowerq; /* write queue attached below this PPA */
113 struct upperstr *nextppa; /* next control stream */
116 struct pppstat stats; /* statistics */
117 time_t last_sent; /* time last NP packet sent */
118 time_t last_recv; /* time last NP packet rcvd */
120 kstat_t *kstats; /* stats for netstat */
124 char ifname[IFNAMSIZ];
125 struct ifstats ifstats;
129 /* Values for flags */
130 #define US_PRIV 1 /* stream was opened by superuser */
131 #define US_CONTROL 2 /* stream is a control stream */
132 #define US_BLOCKED 4 /* flow ctrl has blocked lower write stream */
133 #define US_LASTMOD 8 /* no PPP modules below us */
134 #define US_DBGLOG 0x10 /* log various occurrences */
138 static u_char max_band=0;
139 static u_char def_band=0;
141 #define IPPORT_DEFAULT 65535
144 * Port priority table
145 * Highest priority ports are listed first, lowest are listed last.
146 * ICMP & packets using unlisted ports will be treated as "default".
147 * If IPPORT_DEFAULT is not listed here, "default" packets will be
148 * assigned lowest priority.
149 * Each line should be terminated with "0".
150 * Line containing only "0" marks the end of the list.
153 static u_short prioq_table[]= {
155 22, 23, 513, 517, 518, 0,
159 20, 70, 80, 8001, 8008, 8080, 0, /* 8001,8008,8080 - common proxy ports */
165 static upperstr_t *minor_devs = NULL;
166 static upperstr_t *ppas = NULL;
169 static int pppopen __P((queue_t *, dev_t *, int, int, cred_t *));
170 static int pppclose __P((queue_t *, int, cred_t *));
172 static int pppopen __P((queue_t *, int, int, int));
173 static int pppclose __P((queue_t *, int));
175 static int pppuwput __P((queue_t *, mblk_t *));
176 static int pppursrv __P((queue_t *));
177 static int pppuwsrv __P((queue_t *));
178 static int ppplrput __P((queue_t *, mblk_t *));
179 static int ppplwput __P((queue_t *, mblk_t *));
180 static int ppplrsrv __P((queue_t *));
181 static int ppplwsrv __P((queue_t *));
183 static void dlpi_request __P((queue_t *, mblk_t *, upperstr_t *));
184 static void dlpi_error __P((queue_t *, upperstr_t *, int, int, int));
185 static void dlpi_ok __P((queue_t *, int));
187 static int send_data __P((mblk_t *, upperstr_t *));
188 static void new_ppa __P((queue_t *, mblk_t *));
189 static void attach_ppa __P((queue_t *, mblk_t *));
190 static void detach_ppa __P((queue_t *, mblk_t *));
191 static void debug_dump __P((queue_t *, mblk_t *));
192 static upperstr_t *find_dest __P((upperstr_t *, int));
193 static int putctl2 __P((queue_t *, int, int, int));
194 static int putctl4 __P((queue_t *, int, int, int));
195 static int pass_packet __P((upperstr_t *ppa, mblk_t *mp, int outbound));
196 static int ip_hard_filter __P((upperstr_t *ppa, mblk_t *mp, int outbound));
198 #define PPP_ID 0xb1a6
199 static struct module_info ppp_info = {
201 PPP_ID, "ppp", 0, 512, 512, 384
203 PPP_ID, "ppp", 0, 512, 512, 128
207 static struct qinit pppurint = {
208 NULL, pppursrv, pppopen, pppclose, NULL, &ppp_info, NULL
211 static struct qinit pppuwint = {
212 pppuwput, pppuwsrv, NULL, NULL, NULL, &ppp_info, NULL
215 static struct qinit ppplrint = {
216 ppplrput, ppplrsrv, NULL, NULL, NULL, &ppp_info, NULL
219 static struct qinit ppplwint = {
220 ppplwput, ppplwsrv, NULL, NULL, NULL, &ppp_info, NULL
224 extern struct ifstats *ifstats;
228 struct streamtab pppinfo = {
229 &pppurint, &pppuwint,
236 * How we maintain statistics.
239 #define INCR_IPACKETS(ppa) \
240 if (ppa->kstats != 0) { \
241 KSTAT_NAMED_PTR(ppa->kstats)[0].value.ul++; \
243 #define INCR_IERRORS(ppa) \
244 if (ppa->kstats != 0) { \
245 KSTAT_NAMED_PTR(ppa->kstats)[1].value.ul++; \
247 #define INCR_OPACKETS(ppa) \
248 if (ppa->kstats != 0) { \
249 KSTAT_NAMED_PTR(ppa->kstats)[2].value.ul++; \
251 #define INCR_OERRORS(ppa) \
252 if (ppa->kstats != 0) { \
253 KSTAT_NAMED_PTR(ppa->kstats)[3].value.ul++; \
258 #define INCR_IPACKETS(ppa) ppa->ifstats.ifs_ipackets++;
259 #define INCR_IERRORS(ppa) ppa->ifstats.ifs_ierrors++;
260 #define INCR_OPACKETS(ppa) ppa->ifstats.ifs_opackets++;
261 #define INCR_OERRORS(ppa) ppa->ifstats.ifs_oerrors++;
265 * STREAMS driver entry points.
269 pppopen(q, devp, oflag, sflag, credp)
275 pppopen(q, dev, oflag, sflag)
277 int dev; /* really dev_t */
290 DRV_OPEN_OK(dev); /* device is already open */
293 /* Calculate max_bband & def_band from definitions in prioq.h
294 This colud be done at some more approtiate time (less often)
295 but this way it works well so I'll just leave it here */
303 if (*ptr++ == IPPORT_DEFAULT) {
307 max_band += new_band;
311 def_band = max_band - def_band;
315 if (sflag == CLONEOPEN) {
317 for (prevp = &minor_devs; (up = *prevp) != 0; prevp = &up->nextmn) {
324 mn = getminor(*devp);
328 for (prevp = &minor_devs; (up = *prevp) != 0; prevp = &up->nextmn) {
333 /* this can't happen */
334 q->q_ptr = WR(q)->q_ptr = (caddr_t) up;
340 * Construct a new minor node.
342 up = (upperstr_t *) ALLOC_SLEEP(sizeof(upperstr_t));
343 bzero((caddr_t) up, sizeof(upperstr_t));
345 DPRINT("pppopen: out of kernel memory\n");
352 *devp = makedevice(getmajor(*devp), mn);
356 up->flags |= US_PRIV;
358 up->state = DL_UNATTACHED;
361 up->ifflags = IFF_UP | IFF_POINTOPOINT;
364 up->last_sent = up->last_recv = time;
365 up->npmode = NPMODE_DROP;
366 q->q_ptr = (caddr_t) up;
367 WR(q)->q_ptr = (caddr_t) up;
372 DRV_OPEN_OK(makedev(major(dev), mn));
377 pppclose(q, flag, credp)
387 upperstr_t *up, **upp;
388 upperstr_t *as, *asnext;
393 up = (upperstr_t *) q->q_ptr;
395 DPRINT("pppclose: q_ptr = 0\n");
398 if (up->flags & US_DBGLOG)
399 DPRINT2("ppp/%d: close, flags=%x\n", up->mn, up->flags);
400 if (up->flags & US_CONTROL) {
402 struct ifstats *ifp, *pifp;
405 * This stream represents a PPA:
406 * For all streams attached to the PPA, clear their
407 * references to this PPA.
408 * Then remove this PPA from the list of PPAs.
410 for (as = up->next; as != 0; as = asnext) {
414 if (as->flags & US_BLOCKED) {
415 as->flags &= ~US_BLOCKED;
416 flushq(WR(as->q), FLUSHDATA);
419 for (upp = &ppas; *upp != 0; upp = &(*upp)->nextppa)
425 /* Remove the statistics from the active list. */
426 for (ifp = ifstats, pifp = 0; ifp; ifp = ifp->ifs_next) {
427 if (ifp == &up->ifstats) {
429 pifp->ifs_next = ifp->ifs_next;
431 ifstats = ifp->ifs_next;
439 * If this stream is attached to a PPA,
440 * remove it from the PPA's list.
442 if ((as = up->ppa) != 0) {
443 for (; as->next != 0; as = as->next)
444 if (as->next == up) {
453 kstat_delete(up->kstats);
459 for (prevp = &minor_devs; *prevp != 0; prevp = &(*prevp)->nextmn) {
465 FREE(up, sizeof(upperstr_t));
472 * A message from on high. We do one of three things:
474 * - put the message on the lower write stream
475 * - queue it for our service routine
482 upperstr_t *us, *usnext, *ppa, *os, *nps;
492 struct ppp_idle *pip;
498 us = (upperstr_t *) q->q_ptr;
500 DPRINT("pppuwput: q_ptr = 0!\n");
504 DPRINT1("pppuwput/%d: mp = 0!\n", us->mn);
507 if (mp->b_datap == 0) {
508 DPRINT1("pppuwput/%d: mp->b_datap = 0!\n", us->mn);
511 switch (mp->b_datap->db_type) {
515 dlpi_request(q, mp, us);
520 if (us->flags & US_DBGLOG)
521 DPRINT3("ppp/%d: uwput M_DATA len=%d flags=%x\n",
522 us->mn, msgdsize(mp), us->flags);
523 if (us->ppa == 0 || msgdsize(mp) > us->ppa->mtu + PPP_HDRLEN
525 || (us->flags & US_CONTROL) == 0
528 DPRINT1("pppuwput: junk data len=%d\n", msgdsize(mp));
533 if ((us->flags & US_CONTROL) == 0 && !pass_packet(us, mp, 1))
536 if (!send_data(mp, us))
541 iop = (struct iocblk *) mp->b_rptr;
543 if (us->flags & US_DBGLOG)
544 DPRINT3("ppp/%d: ioctl %x count=%d\n",
545 us->mn, iop->ioc_cmd, iop->ioc_count);
546 switch (iop->ioc_cmd) {
548 if ((us->flags & US_CONTROL) == 0 || us->lowerq != 0)
550 if (mp->b_cont == 0) {
551 DPRINT1("pppuwput/%d: ioctl I_LINK b_cont = 0!\n", us->mn);
554 lb = (struct linkblk *) mp->b_cont->b_rptr;
555 us->lowerq = lq = lb->l_qbot;
557 DPRINT1("pppuwput/%d: ioctl I_LINK l_qbot = 0!\n", us->mn);
560 lq->q_ptr = (caddr_t) us;
561 RD(lq)->q_ptr = (caddr_t) us;
563 flushq(RD(lq), FLUSHALL);
566 us->flags &= ~US_LASTMOD;
567 /* Unblock upper streams which now feed this lower stream. */
569 /* Send useful information down to the modules which
570 are now linked below us. */
571 putctl2(lq, M_CTL, PPPCTL_UNIT, us->ppa_id);
572 putctl4(lq, M_CTL, PPPCTL_MRU, us->mru);
573 putctl4(lq, M_CTL, PPPCTL_MTU, us->mtu);
575 /* Lower tty driver's queue hiwat/lowat from default 4096/128
576 to 256/128 since we don't want queueing of data on
577 output to physical device */
580 for (tlq = lq; tlq->q_next != NULL; tlq = tlq->q_next)
582 strqset(tlq, QHIWAT, 0, 256);
583 strqset(tlq, QLOWAT, 0, 128);
589 if (mp->b_cont == 0) {
590 DPRINT1("pppuwput/%d: ioctl I_UNLINK b_cont = 0!\n", us->mn);
593 lb = (struct linkblk *) mp->b_cont->b_rptr;
595 if (us->lowerq != lb->l_qbot) {
596 DPRINT2("ppp unlink: lowerq=%x qbot=%x\n",
597 us->lowerq, lb->l_qbot);
604 /* Unblock streams which now feed back up the control stream. */
609 if (us->flags & US_CONTROL)
611 if ((us->flags & US_PRIV) == 0) {
615 /* Arrange to return an int */
616 if ((mq = mp->b_cont) == 0
617 || mq->b_datap->db_lim - mq->b_rptr < sizeof(int)) {
618 mq = allocb(sizeof(int), BPRI_HI);
628 iop->ioc_count = sizeof(int);
629 mq->b_wptr = mq->b_rptr + sizeof(int);
630 qwriter(q, mp, new_ppa, PERIM_OUTER);
635 /* like dlpi_attach, for programs which can't write to
636 the stream (like pppstats) */
637 if (iop->ioc_count != sizeof(int) || us->ppa != 0)
639 if (mp->b_cont == 0) {
640 DPRINT1("pppuwput/%d: ioctl PPPIO_ATTACH b_cont = 0!\n", us->mn);
643 n = *(int *)mp->b_cont->b_rptr;
644 for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
645 if (ppa->ppa_id == n)
651 qwriter(q, mp, attach_ppa, PERIM_OUTER);
657 /* Attach to a given SAP. */
658 if (iop->ioc_count != sizeof(int) || us->ppa == 0)
660 if (mp->b_cont == 0) {
661 DPRINT1("pppuwput/%d: ioctl PPPIO_BIND b_cont = 0!\n", us->mn);
664 n = *(int *)mp->b_cont->b_rptr;
665 /* n must be a valid PPP network protocol number. */
666 if (n < 0x21 || n > 0x3fff || (n & 0x101) != 1)
668 /* check that no other stream is bound to this sap already. */
669 for (os = us->ppa; os != 0; os = os->next)
681 if (iop->ioc_count != sizeof(int) || (us->flags & US_CONTROL) == 0)
683 if (mp->b_cont == 0) {
684 DPRINT1("pppuwput/%d: ioctl PPPIO_MRU b_cont = 0!\n", us->mn);
687 n = *(int *)mp->b_cont->b_rptr;
688 if (n <= 0 || n > PPP_MAXMRU)
694 putctl4(us->lowerq, M_CTL, PPPCTL_MRU, n);
700 if (iop->ioc_count != sizeof(int) || (us->flags & US_CONTROL) == 0)
702 if (mp->b_cont == 0) {
703 DPRINT1("pppuwput/%d: ioctl PPPIO_MTU b_cont = 0!\n", us->mn);
706 n = *(int *)mp->b_cont->b_rptr;
707 if (n <= 0 || n > PPP_MAXMTU)
711 /* The MTU reported in netstat, not used as IP max packet size! */
712 us->ifstats.ifs_mtu = n;
715 putctl4(us->lowerq, M_CTL, PPPCTL_MTU, n);
721 us->flags |= US_LASTMOD;
726 if (iop->ioc_count != sizeof(int))
728 if (mp->b_cont == 0) {
729 DPRINT1("pppuwput/%d: ioctl PPPIO_DEBUG b_cont = 0!\n", us->mn);
732 n = *(int *)mp->b_cont->b_rptr;
733 if (n == PPPDBG_DUMP + PPPDBG_DRIVER) {
734 qwriter(q, NULL, debug_dump, PERIM_OUTER);
737 } else if (n == PPPDBG_LOG + PPPDBG_DRIVER) {
738 DPRINT1("ppp/%d: debug log enabled\n", us->mn);
739 us->flags |= US_DBGLOG;
743 if (us->ppa == 0 || us->ppa->lowerq == 0)
745 putnext(us->ppa->lowerq, mp);
751 if (iop->ioc_count != 2 * sizeof(int))
753 if ((us->flags & US_CONTROL) == 0)
755 if (mp->b_cont == 0) {
756 DPRINT1("pppuwput/%d: ioctl PPPIO_NPMODE b_cont = 0!\n", us->mn);
759 sap = ((int *)mp->b_cont->b_rptr)[0];
760 for (nps = us->next; nps != 0; nps = nps->next)
764 if (us->flags & US_DBGLOG)
765 DPRINT2("ppp/%d: no stream for sap %x\n", us->mn, sap);
768 nps->npmode = (enum NPmode) ((int *)mp->b_cont->b_rptr)[1];
769 if (nps->npmode == NPMODE_DROP || nps->npmode == NPMODE_ERROR)
770 flushq(WR(nps->q), FLUSHDATA);
771 else if (nps->npmode == NPMODE_PASS && qsize(WR(nps->q)) > 0
772 && (nps->flags & US_BLOCKED) == 0)
779 if ((ppa = us->ppa) == 0)
781 mq = allocb(sizeof(struct ppp_idle), BPRI_HI);
790 pip = (struct ppp_idle *) mq->b_wptr;
791 pip->xmit_idle = time - ppa->last_sent;
792 pip->recv_idle = time - ppa->last_recv;
793 mq->b_wptr += sizeof(struct ppp_idle);
794 iop->ioc_count = sizeof(struct ppp_idle);
800 /* Sent from IP down to us. Attach the ifstats structure. */
801 if (iop->ioc_count != sizeof(struct ifreq) || us->ppa == 0)
803 ifr = (struct ifreq *)mp->b_cont->b_rptr;
804 /* Find the unit number in the interface name. */
805 for (i = 0; i < IFNAMSIZ; i++) {
806 if (ifr->ifr_name[i] == 0 ||
807 (ifr->ifr_name[i] >= '0' &&
808 ifr->ifr_name[i] <= '9'))
811 us->ifname[i] = ifr->ifr_name[i];
815 /* Convert the unit number to binary. */
816 for (n = 0; i < IFNAMSIZ; i++) {
817 if (ifr->ifr_name[i] == 0) {
821 n = n * 10 + ifr->ifr_name[i] - '0';
825 /* Verify the ppa. */
826 if (us->ppa->ppa_id != n)
830 /* Set up the netstat block. */
831 strncpy (ppa->ifname, us->ifname, IFNAMSIZ);
833 ppa->ifstats.ifs_name = ppa->ifname;
834 ppa->ifstats.ifs_unit = n;
835 ppa->ifstats.ifs_active = us->state != DL_UNBOUND;
836 ppa->ifstats.ifs_mtu = ppa->mtu;
838 /* Link in statistics used by netstat. */
839 ppa->ifstats.ifs_next = ifstats;
840 ifstats = &ppa->ifstats;
847 if (!(us->flags & US_CONTROL)) {
853 ((struct iocblk_in *)iop)->ioc_ifflags = us->ifflags;
858 if (!(us->flags & US_CONTROL)) {
864 us->ifflags = ((struct iocblk_in *)iop)->ioc_ifflags;
869 if (!(us->flags & US_CONTROL)) {
875 us->ifflags |= IFF_RUNNING;
876 ((struct iocblk_in *)iop)->ioc_ifflags |= IFF_RUNNING;
882 * Vanilla SVR4 systems don't handle SIOCSIFMTU, rather
883 * they take the MTU from the DL_INFO_ACK we sent in response
884 * to their DL_INFO_REQ. Fortunately, they will update the
885 * MTU if we send an unsolicited DL_INFO_ACK up.
887 if ((mq = allocb(sizeof(dl_info_req_t), BPRI_HI)) == 0)
888 break; /* should do bufcall */
889 ((union DL_primitives *)mq->b_rptr)->dl_primitive = DL_INFO_REQ;
890 mq->b_wptr = mq->b_rptr + sizeof(dl_info_req_t);
891 dlpi_request(q, mq, us);
906 if (us->ppa == 0 || us->ppa->lowerq == 0)
908 us->ioc_id = iop->ioc_id;
910 switch (iop->ioc_cmd) {
913 if (us->flags & US_LASTMOD) {
917 putnext(us->ppa->lowerq, mp);
920 if (us->flags & US_PRIV)
921 putnext(us->ppa->lowerq, mp);
923 DPRINT1("ppp ioctl %x rejected\n", iop->ioc_cmd);
932 iop->ioc_error = error;
933 mp->b_datap->db_type = M_IOCNAK;
935 } else if (error == 0) {
936 mp->b_datap->db_type = M_IOCACK;
942 if (us->flags & US_DBGLOG)
943 DPRINT2("ppp/%d: flush %x\n", us->mn, *mp->b_rptr);
944 if (*mp->b_rptr & FLUSHW)
945 flushq(q, FLUSHDATA);
946 if (*mp->b_rptr & FLUSHR) {
947 *mp->b_rptr &= ~FLUSHW;
962 dlpi_request(q, mp, us)
967 union DL_primitives *d = (union DL_primitives *) mp->b_rptr;
968 int size = mp->b_wptr - mp->b_rptr;
970 upperstr_t *ppa, *os;
975 if (us->flags & US_DBGLOG)
976 DPRINT3("ppp/%d: dlpi prim %x len=%d\n", us->mn,
977 d->dl_primitive, size);
978 switch (d->dl_primitive) {
980 if (size < sizeof(dl_info_req_t))
982 if ((reply = allocb(sizeof(dl_info_ack_t), BPRI_HI)) == 0)
983 break; /* should do bufcall */
984 reply->b_datap->db_type = M_PCPROTO;
985 info = (dl_info_ack_t *) reply->b_wptr;
986 reply->b_wptr += sizeof(dl_info_ack_t);
987 bzero((caddr_t) info, sizeof(dl_info_ack_t));
988 info->dl_primitive = DL_INFO_ACK;
989 info->dl_max_sdu = us->ppa? us->ppa->mtu: PPP_MAXMTU;
990 info->dl_min_sdu = 1;
991 info->dl_addr_length = sizeof(uint);
993 info->dl_mac_type = DL_OTHER;
995 info->dl_mac_type = DL_HDLC; /* a lie */
997 info->dl_current_state = us->state;
998 info->dl_service_mode = DL_CLDLS;
999 info->dl_provider_style = DL_STYLE2;
1000 #if DL_CURRENT_VERSION >= 2
1001 info->dl_sap_length = sizeof(uint);
1002 info->dl_version = DL_CURRENT_VERSION;
1008 if (size < sizeof(dl_attach_req_t))
1010 if (us->state != DL_UNATTACHED || us->ppa != 0) {
1011 dlpi_error(q, us, DL_ATTACH_REQ, DL_OUTSTATE, 0);
1014 for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
1015 if (ppa->ppa_id == d->attach_req.dl_ppa)
1018 dlpi_error(q, us, DL_ATTACH_REQ, DL_BADPPA, 0);
1022 qwriter(q, mp, attach_ppa, PERIM_OUTER);
1026 if (size < sizeof(dl_detach_req_t))
1028 if (us->state != DL_UNBOUND || us->ppa == 0) {
1029 dlpi_error(q, us, DL_DETACH_REQ, DL_OUTSTATE, 0);
1032 qwriter(q, mp, detach_ppa, PERIM_OUTER);
1036 if (size < sizeof(dl_bind_req_t))
1038 if (us->state != DL_UNBOUND || us->ppa == 0) {
1039 dlpi_error(q, us, DL_BIND_REQ, DL_OUTSTATE, 0);
1043 /* apparently this test fails (unnecessarily?) on some systems */
1044 if (d->bind_req.dl_service_mode != DL_CLDLS) {
1045 dlpi_error(q, us, DL_BIND_REQ, DL_UNSUPPORTED, 0);
1050 /* saps must be valid PPP network protocol numbers,
1051 except that we accept ETHERTYPE_IP in place of PPP_IP. */
1052 sap = d->bind_req.dl_sap;
1054 if (sap == ETHERTYPE_IP)
1056 if (sap < 0x21 || sap > 0x3fff || (sap & 0x101) != 1) {
1057 dlpi_error(q, us, DL_BIND_REQ, DL_BADADDR, 0);
1061 /* check that no other stream is bound to this sap already. */
1062 for (os = us->ppa; os != 0; os = os->next)
1066 dlpi_error(q, us, DL_BIND_REQ, DL_NOADDR, 0);
1071 us->state = DL_IDLE;
1073 if ((reply = allocb(sizeof(dl_bind_ack_t) + sizeof(uint),
1075 break; /* should do bufcall */
1076 ackp = (dl_bind_ack_t *) reply->b_wptr;
1077 reply->b_wptr += sizeof(dl_bind_ack_t) + sizeof(uint);
1078 reply->b_datap->db_type = M_PCPROTO;
1079 bzero((caddr_t) ackp, sizeof(dl_bind_ack_t));
1080 ackp->dl_primitive = DL_BIND_ACK;
1082 ackp->dl_addr_length = sizeof(uint);
1083 ackp->dl_addr_offset = sizeof(dl_bind_ack_t);
1084 *(uint *)(ackp+1) = sap;
1089 if (size < sizeof(dl_unbind_req_t))
1091 if (us->state != DL_IDLE) {
1092 dlpi_error(q, us, DL_UNBIND_REQ, DL_OUTSTATE, 0);
1096 us->state = DL_UNBOUND;
1098 us->ppa->ifstats.ifs_active = 0;
1100 dlpi_ok(q, DL_UNBIND_REQ);
1103 case DL_UNITDATA_REQ:
1104 if (size < sizeof(dl_unitdata_req_t))
1106 if (us->state != DL_IDLE) {
1107 dlpi_error(q, us, DL_UNITDATA_REQ, DL_OUTSTATE, 0);
1110 if ((ppa = us->ppa) == 0) {
1111 cmn_err(CE_CONT, "ppp: in state dl_idle but ppa == 0?\n");
1114 len = mp->b_cont == 0? 0: msgdsize(mp->b_cont);
1115 if (len > ppa->mtu) {
1116 DPRINT2("dlpi data too large (%d > %d)\n", len, ppa->mtu);
1121 /* Extract s_port & d_port from IP-packet, the code is a bit
1122 dirty here, but so am I, too... */
1123 if (mp->b_datap->db_type == M_PROTO && us->sap == PPP_IP
1124 && mp->b_cont != 0) {
1128 u_char band_unset, cur_band, syn;
1129 u_short s_port, d_port;
1131 bb = mp->b_cont->b_rptr; /* bb points to IP-header*/
1132 len = mp->b_cont->b_wptr - mp->b_cont->b_rptr;
1134 s_port = IPPORT_DEFAULT;
1135 d_port = IPPORT_DEFAULT;
1136 if (len >= 20) { /* 20 = minimum length of IP header */
1137 iphlen = (bb[0] & 0x0f) * 4;
1142 if (len >= 20) { /* min length of TCP header */
1143 s_port = (tlh[0] << 8) + tlh[1];
1144 d_port = (tlh[2] << 8) + tlh[3];
1145 syn = tlh[13] & 0x02;
1149 if (len >= 8) { /* min length of UDP header */
1150 s_port = (tlh[0] << 8) + tlh[1];
1151 d_port = (tlh[2] << 8) + tlh[3];
1158 * Now calculate b_band for this packet from the
1159 * port-priority table.
1162 cur_band = max_band;
1165 while (*ptr && band_unset)
1166 if (s_port == *ptr || d_port == *ptr++) {
1167 mp->b_band = cur_band;
1175 mp->b_band = def_band;
1176 /* It may be usable to urge SYN packets a bit */
1181 /* this assumes PPP_HDRLEN <= sizeof(dl_unitdata_req_t) */
1182 if (mp->b_datap->db_ref > 1) {
1183 np = allocb(PPP_HDRLEN, BPRI_HI);
1186 np->b_cont = mp->b_cont;
1191 mp->b_datap->db_type = M_DATA;
1192 /* XXX should use dl_dest_addr_offset/length here,
1193 but we would have to translate ETHERTYPE_IP -> PPP_IP */
1194 mp->b_wptr = mp->b_rptr + PPP_HDRLEN;
1195 mp->b_rptr[0] = PPP_ALLSTATIONS;
1196 mp->b_rptr[1] = PPP_UI;
1197 mp->b_rptr[2] = us->sap >> 8;
1198 mp->b_rptr[3] = us->sap;
1199 if (pass_packet(us, mp, 1)) {
1200 if (!send_data(mp, us))
1205 #if DL_CURRENT_VERSION >= 2
1206 case DL_SUBS_BIND_REQ:
1207 case DL_SUBS_UNBIND_REQ:
1208 case DL_ENABMULTI_REQ:
1209 case DL_DISABMULTI_REQ:
1210 case DL_PROMISCON_REQ:
1211 case DL_PROMISCOFF_REQ:
1212 case DL_PHYS_ADDR_REQ:
1213 case DL_SET_PHYS_ADDR_REQ:
1216 case DL_REPLY_UPDATE_REQ:
1218 case DL_DATA_ACK_REQ:
1220 case DL_CONNECT_REQ:
1222 dlpi_error(q, us, d->dl_primitive, DL_NOTSUPPORTED, 0);
1225 case DL_CONNECT_RES:
1226 case DL_DISCONNECT_REQ:
1229 dlpi_error(q, us, d->dl_primitive, DL_OUTSTATE, 0);
1233 dlpi_error(q, us, d->dl_primitive, DL_BADQOSTYPE, 0);
1236 #if DL_CURRENT_VERSION >= 2
1243 cmn_err(CE_CONT, "ppp: unknown dlpi prim 0x%x\n", d->dl_primitive);
1246 dlpi_error(q, us, d->dl_primitive, DL_BADPRIM, 0);
1253 dlpi_error(q, us, prim, err, uerr)
1256 int prim, err, uerr;
1259 dl_error_ack_t *errp;
1261 if (us->flags & US_DBGLOG)
1262 DPRINT3("ppp/%d: dlpi error, prim=%x, err=%x\n", us->mn, prim, err);
1263 reply = allocb(sizeof(dl_error_ack_t), BPRI_HI);
1265 return; /* XXX should do bufcall */
1266 reply->b_datap->db_type = M_PCPROTO;
1267 errp = (dl_error_ack_t *) reply->b_wptr;
1268 reply->b_wptr += sizeof(dl_error_ack_t);
1269 errp->dl_primitive = DL_ERROR_ACK;
1270 errp->dl_error_primitive = prim;
1271 errp->dl_errno = err;
1272 errp->dl_unix_errno = uerr;
1284 reply = allocb(sizeof(dl_ok_ack_t), BPRI_HI);
1286 return; /* XXX should do bufcall */
1287 reply->b_datap->db_type = M_PCPROTO;
1288 okp = (dl_ok_ack_t *) reply->b_wptr;
1289 reply->b_wptr += sizeof(dl_ok_ack_t);
1290 okp->dl_primitive = DL_OK_ACK;
1291 okp->dl_correct_primitive = prim;
1294 #endif /* NO_DLPI */
1297 pass_packet(us, mp, outbound)
1305 if ((ppa = us->ppa) == 0) {
1310 #ifdef FILTER_PACKETS
1311 pass = ip_hard_filter(us, mp, outbound);
1314 * Here is where we might, in future, decide whether to pass
1315 * or drop the packet, and whether it counts as link activity.
1318 #endif /* FILTER_PACKETS */
1321 /* pass only if link already up, and don't update time */
1322 if (ppa->lowerq == 0) {
1329 ppa->last_sent = time;
1331 ppa->last_recv = time;
1345 if ((us->flags & US_BLOCKED) || us->npmode == NPMODE_QUEUE)
1348 if (ppa == 0 || us->npmode == NPMODE_DROP || us->npmode == NPMODE_ERROR) {
1349 if (us->flags & US_DBGLOG)
1350 DPRINT2("ppp/%d: dropping pkt (npmode=%d)\n", us->mn, us->npmode);
1354 if ((q = ppa->lowerq) == 0) {
1355 /* try to send it up the control stream */
1356 if (bcanputnext(ppa->q, mp->b_band)) {
1358 * The message seems to get corrupted for some reason if
1359 * we just send the message up as it is, so we send a copy.
1361 mblk_t *np = copymsg(mp);
1364 putnext(ppa->q, np);
1368 if (bcanputnext(ppa->lowerq, mp->b_band)) {
1370 * The lower write queue's put procedure just updates counters
1371 * and does a putnext. We call it so that on SMP systems, we
1372 * enter the lower queues' perimeter so that the counter
1373 * updates are serialized.
1375 put(ppa->lowerq, mp);
1379 us->flags |= US_BLOCKED;
1384 * Allocate a new PPA id and link this stream into the list of PPAs.
1385 * This procedure is called with an exclusive lock on all queues in
1393 upperstr_t *us, **usp;
1398 while ((us = *usp) != 0 && ppa_id == us->ppa_id) {
1402 us = (upperstr_t *) q->q_ptr;
1403 us->ppa_id = ppa_id;
1408 us->flags |= US_CONTROL;
1409 us->npmode = NPMODE_PASS;
1416 * Create a kstats record for our statistics, so netstat -i works.
1418 if (us->kstats == 0) {
1421 sprintf(unit, "ppp%d", us->ppa->ppa_id);
1422 us->kstats = kstat_create("ppp", us->ppa->ppa_id, unit,
1423 "net", KSTAT_TYPE_NAMED, 4, 0);
1424 if (us->kstats != 0) {
1425 kstat_named_t *kn = KSTAT_NAMED_PTR(us->kstats);
1427 strcpy(kn[0].name, "ipackets");
1428 kn[0].data_type = KSTAT_DATA_ULONG;
1429 strcpy(kn[1].name, "ierrors");
1430 kn[1].data_type = KSTAT_DATA_ULONG;
1431 strcpy(kn[2].name, "opackets");
1432 kn[2].data_type = KSTAT_DATA_ULONG;
1433 strcpy(kn[3].name, "oerrors");
1434 kn[3].data_type = KSTAT_DATA_ULONG;
1435 kstat_install(us->kstats);
1440 *(int *)mp->b_cont->b_rptr = ppa_id;
1441 mp->b_datap->db_type = M_IOCACK;
1452 us = (upperstr_t *) q->q_ptr;
1454 us->state = DL_UNBOUND;
1456 for (t = us->ppa; t->next != 0; t = t->next)
1460 if (mp->b_datap->db_type == M_IOCTL) {
1461 mp->b_datap->db_type = M_IOCACK;
1465 dlpi_ok(q, DL_ATTACH_REQ);
1477 us = (upperstr_t *) q->q_ptr;
1478 for (t = us->ppa; t->next != 0; t = t->next)
1479 if (t->next == us) {
1486 us->state = DL_UNATTACHED;
1487 dlpi_ok(q, DL_DETACH_REQ);
1496 struct lowerstr *ls;
1500 us = (upperstr_t *) q->q_ptr;
1501 us->flags &= ~US_BLOCKED;
1502 while ((mp = getq(q)) != 0) {
1503 if (!send_data(mp, us)) {
1518 ppa = (upperstr_t *) q->q_ptr;
1519 if (ppa != 0) { /* why wouldn't it? */
1520 ppa->stats.ppp_opackets++;
1521 ppa->stats.ppp_obytes += msgdsize(mp);
1522 #ifdef INCR_OPACKETS
1537 * Flow control has back-enabled this stream:
1538 * enable the write service procedures of all upper
1539 * streams feeding this lower stream.
1541 for (us = (upperstr_t *) q->q_ptr; us != NULL; us = us->next)
1542 if (us->flags & US_BLOCKED)
1551 upperstr_t *us, *as;
1554 dl_unitdata_ind_t *ud;
1558 us = (upperstr_t *) q->q_ptr;
1559 if (us->flags & US_CONTROL) {
1562 * If there is no lower queue attached, run the write service
1563 * routines of other upper streams attached to this PPA.
1565 if (us->lowerq == 0) {
1568 if (as->flags & US_BLOCKED)
1575 * A network protocol stream. Put a DLPI header on each
1576 * packet and send it on.
1577 * (Actually, it seems that the IP module will happily
1578 * accept M_DATA messages without the DL_UNITDATA_IND header.)
1580 while ((mp = getq(q)) != 0) {
1581 if (!canputnext(q)) {
1586 proto = PPP_PROTOCOL(mp->b_rptr);
1587 mp->b_rptr += PPP_HDRLEN;
1588 hdr = allocb(sizeof(dl_unitdata_ind_t) + 2 * sizeof(uint),
1591 /* XXX should put it back and use bufcall */
1595 hdr->b_datap->db_type = M_PROTO;
1596 ud = (dl_unitdata_ind_t *) hdr->b_wptr;
1597 hdr->b_wptr += sizeof(dl_unitdata_ind_t) + 2 * sizeof(uint);
1599 ud->dl_primitive = DL_UNITDATA_IND;
1600 ud->dl_dest_addr_length = sizeof(uint);
1601 ud->dl_dest_addr_offset = sizeof(dl_unitdata_ind_t);
1602 ud->dl_src_addr_length = sizeof(uint);
1603 ud->dl_src_addr_offset = ud->dl_dest_addr_offset + sizeof(uint);
1604 #if DL_CURRENT_VERSION >= 2
1605 ud->dl_group_address = 0;
1607 /* Send the DLPI client the data with the SAP they requested,
1608 (e.g. ETHERTYPE_IP) rather than the PPP protocol number
1610 ((uint *)(ud + 1))[0] = us->req_sap; /* dest SAP */
1611 ((uint *)(ud + 1))[1] = us->req_sap; /* src SAP */
1615 #endif /* NO_DLPI */
1620 * If this stream is attached to a PPA with a lower queue pair,
1621 * enable the read queue's service routine if it has data queued.
1622 * XXX there is a possibility that packets could get out of order
1623 * if ppplrput now runs before ppplrsrv.
1625 if (us->ppa != 0 && us->ppa->lowerq != 0)
1626 qenable(RD(us->ppa->lowerq));
1632 find_dest(ppa, proto)
1638 for (us = ppa->next; us != 0; us = us->next)
1639 if (proto == us->sap)
1649 upperstr_t *ppa, *us;
1655 ppa = (upperstr_t *) q->q_ptr;
1657 DPRINT1("ppplrput: q = %x, ppa = 0??\n", q);
1661 switch (mp->b_datap->db_type) {
1663 if (*mp->b_rptr & FLUSHW) {
1664 *mp->b_rptr &= ~FLUSHR;
1671 switch (*mp->b_rptr) {
1676 ppa->stats.ppp_ierrors++;
1682 ppa->stats.ppp_oerrors++;
1691 * Attempt to match up the response with the stream
1692 * that the request came from.
1694 iop = (struct iocblk *) mp->b_rptr;
1695 for (us = ppa; us != 0; us = us->next)
1696 if (us->ioc_id == iop->ioc_id)
1706 * The serial device has hung up. We don't want to send
1707 * the M_HANGUP message up to pppd because that will stop
1708 * us from using the control stream any more. Instead we
1709 * send a zero-length message as an end-of-file indication.
1712 mp = allocb(1, BPRI_HI);
1714 DPRINT1("ppp/%d: couldn't allocate eof message!\n", ppa->mn);
1717 putnext(ppa->q, mp);
1721 if (mp->b_datap->db_type == M_DATA) {
1723 if (mp->b_wptr - mp->b_rptr < PPP_HDRLEN) {
1724 PULLUP(mp, PPP_HDRLEN);
1726 DPRINT1("ppp_lrput: msgpullup failed (len=%d)\n", len);
1730 ppa->stats.ppp_ipackets++;
1731 ppa->stats.ppp_ibytes += len;
1732 #ifdef INCR_IPACKETS
1735 proto = PPP_PROTOCOL(mp->b_rptr);
1736 if (proto < 0x8000 && (us = find_dest(ppa, proto)) != 0) {
1738 * A data packet for some network protocol.
1739 * Queue it on the upper stream for that protocol.
1741 if (!pass_packet(us, mp, 0))
1751 * A control frame, a frame for an unknown protocol,
1752 * or some other message type.
1753 * Send it up to pppd via the control stream.
1755 if (queclass(mp) == QPCTL || canputnext(ppa->q))
1756 putnext(ppa->q, mp);
1770 upperstr_t *ppa, *us;
1774 * Packets only get queued here for flow control reasons.
1776 ppa = (upperstr_t *) q->q_ptr;
1777 while ((mp = getq(q)) != 0) {
1778 if (mp->b_datap->db_type == M_DATA
1779 && (proto = PPP_PROTOCOL(mp->b_rptr)) < 0x8000
1780 && (us = find_dest(ppa, proto)) != 0) {
1788 if (canputnext(ppa->q))
1789 putnext(ppa->q, mp);
1800 putctl2(q, type, code, val)
1802 int type, code, val;
1806 mp = allocb(2, BPRI_HI);
1809 mp->b_datap->db_type = type;
1810 mp->b_wptr[0] = code;
1811 mp->b_wptr[1] = val;
1818 putctl4(q, type, code, val)
1820 int type, code, val;
1824 mp = allocb(4, BPRI_HI);
1827 mp->b_datap->db_type = type;
1828 mp->b_wptr[0] = code;
1829 ((short *)mp->b_wptr)[1] = val;
1837 queue_t *q; /* not used */
1838 mblk_t *mp; /* not used either */
1843 DPRINT("ppp upper streams:\n");
1844 for (us = minor_devs; us != 0; us = us->nextmn) {
1846 DPRINT3(" %d: q=%x rlev=%d",
1847 us->mn, uq, (uq? qsize(uq): 0));
1848 DPRINT3(" wlev=%d flags=0x%b", (uq? qsize(WR(uq)): 0),
1849 us->flags, "\020\1priv\2control\3blocked\4last");
1850 DPRINT3(" state=%x sap=%x req_sap=%x", us->state, us->sap,
1855 DPRINT1(" ppa=%d\n", us->ppa->ppa_id);
1856 if (us->flags & US_CONTROL) {
1858 DPRINT3(" control for %d lq=%x rlev=%d",
1859 us->ppa_id, lq, (lq? qsize(RD(lq)): 0));
1860 DPRINT3(" wlev=%d mru=%d mtu=%d\n",
1861 (lq? qsize(lq): 0), us->mru, us->mtu);
1866 #ifdef FILTER_PACKETS
1867 #include <netinet/in_systm.h>
1868 #include <netinet/ip.h>
1869 #include <netinet/udp.h>
1870 #include <netinet/tcp.h>
1872 #define MAX_IPHDR 128 /* max TCP/IP header size */
1875 /* The following table contains a hard-coded list of protocol/port pairs.
1876 * Any matching packets are either discarded unconditionally, or,
1877 * if ok_if_link_up is non-zero when a connection does not currently exist
1878 * (i.e., they go through if the connection is present, but never initiate
1880 * This idea came from a post by dm@garage.uun.org (David Mazieres)
1882 static struct pktfilt_tab {
1885 u_short ok_if_link_up;
1887 { IPPROTO_UDP, 520, 1 }, /* RIP, ok to pass if link is up */
1888 { IPPROTO_UDP, 123, 1 }, /* NTP, don't keep up the link for it */
1889 { -1, 0, 0 } /* terminator entry has port == -1 */
1894 ip_hard_filter(us, mp, outbound)
1900 struct pktfilt_tab *pft;
1906 /* Note, the PPP header has already been pulled up in all cases */
1907 proto = PPP_PROTOCOL(mp->b_rptr);
1908 if (us->flags & US_DBGLOG)
1909 DPRINT3("ppp/%d: filter, proto=0x%x, out=%d\n", us->mn, proto, outbound);
1914 if ((mp->b_wptr - mp->b_rptr) == PPP_HDRLEN && mp->b_cont != 0) {
1915 temp_mp = mp->b_cont;
1916 len = msgdsize(temp_mp);
1917 hlen = (len < MAX_IPHDR) ? len : MAX_IPHDR;
1918 PULLUP(temp_mp, hlen);
1920 DPRINT2("ppp/%d: filter, pullup next failed, len=%d\n",
1922 mp->b_cont = 0; /* PULLUP() freed the rest */
1926 ip = (struct ip *)mp->b_cont->b_rptr;
1930 hlen = (len < (PPP_HDRLEN+MAX_IPHDR)) ? len : (PPP_HDRLEN+MAX_IPHDR);
1933 DPRINT2("ppp/%d: filter, pullup failed, len=%d\n",
1937 ip = (struct ip *)(mp->b_rptr + PPP_HDRLEN);
1940 /* For IP traffic, certain packets (e.g., RIP) may be either
1941 * 1. ignored - dropped completely
1942 * 2. will not initiate a connection, but
1943 * will be passed if a connection is currently up.
1945 for (pft=pktfilt_tab; pft->proto != -1; pft++) {
1946 if (ip->ip_p == pft->proto) {
1947 switch(pft->proto) {
1949 if (((struct udphdr *) &((int *)ip)[ip->ip_hl])->uh_dport
1950 == htons(pft->port)) goto endfor;
1953 if (((struct tcphdr *) &((int *)ip)[ip->ip_hl])->th_dport
1954 == htons(pft->port)) goto endfor;
1960 if (pft->proto != -1) {
1961 if (us->flags & US_DBGLOG)
1962 DPRINT3("ppp/%d: found IP pkt, proto=0x%x (%d)\n",
1963 us->mn, pft->proto, pft->port);
1964 /* Discard if not connected, or if not pass_with_link_up */
1965 /* else, if link is up let go by, but don't update time */
1966 return pft->ok_if_link_up? -1: 0;
1969 } /* end switch (proto) */
1973 #endif /* FILTER_PACKETS */