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.10 1997/04/30 05:44:43 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;
394 if (up->flags & US_DBGLOG)
395 DPRINT2("ppp/%d: close, flags=%x\n", up->mn, up->flags);
398 if (up->flags & US_CONTROL) {
400 struct ifstats *ifp, *pifp;
403 * This stream represents a PPA:
404 * For all streams attached to the PPA, clear their
405 * references to this PPA.
406 * Then remove this PPA from the list of PPAs.
408 for (as = up->next; as != 0; as = asnext) {
412 if (as->flags & US_BLOCKED) {
413 as->flags &= ~US_BLOCKED;
414 flushq(WR(as->q), FLUSHDATA);
417 for (upp = &ppas; *upp != 0; upp = &(*upp)->nextppa)
423 /* Remove the statistics from the active list. */
424 for (ifp = ifstats, pifp = 0; ifp; ifp = ifp->ifs_next) {
425 if (ifp == &up->ifstats) {
427 pifp->ifs_next = ifp->ifs_next;
429 ifstats = ifp->ifs_next;
437 * If this stream is attached to a PPA,
438 * remove it from the PPA's list.
440 if ((as = up->ppa) != 0) {
441 for (; as->next != 0; as = as->next)
442 if (as->next == up) {
451 kstat_delete(up->kstats);
457 for (prevp = &minor_devs; *prevp != 0; prevp = &(*prevp)->nextmn) {
463 FREE(up, sizeof(upperstr_t));
470 * A message from on high. We do one of three things:
472 * - put the message on the lower write stream
473 * - queue it for our service routine
480 upperstr_t *us, *usnext, *ppa, *os, *nps;
490 struct ppp_idle *pip;
496 us = (upperstr_t *) q->q_ptr;
497 switch (mp->b_datap->db_type) {
501 dlpi_request(q, mp, us);
506 if (us->flags & US_DBGLOG)
507 DPRINT3("ppp/%d: uwput M_DATA len=%d flags=%x\n",
508 us->mn, msgdsize(mp), us->flags);
509 if (us->ppa == 0 || msgdsize(mp) > us->ppa->mtu + PPP_HDRLEN
511 || (us->flags & US_CONTROL) == 0
514 DPRINT1("pppuwput: junk data len=%d\n", msgdsize(mp));
519 if ((us->flags & US_CONTROL) == 0 && !pass_packet(us, mp, 1))
522 if (!send_data(mp, us))
527 iop = (struct iocblk *) mp->b_rptr;
529 if (us->flags & US_DBGLOG)
530 DPRINT3("ppp/%d: ioctl %x count=%d\n",
531 us->mn, iop->ioc_cmd, iop->ioc_count);
532 switch (iop->ioc_cmd) {
534 if ((us->flags & US_CONTROL) == 0 || us->lowerq != 0)
536 lb = (struct linkblk *) mp->b_cont->b_rptr;
537 us->lowerq = lq = lb->l_qbot;
538 lq->q_ptr = (caddr_t) us;
539 RD(lq)->q_ptr = (caddr_t) us;
541 flushq(RD(lq), FLUSHALL);
544 us->flags &= ~US_LASTMOD;
545 /* Unblock upper streams which now feed this lower stream. */
547 /* Send useful information down to the modules which
548 are now linked below us. */
549 putctl2(lq, M_CTL, PPPCTL_UNIT, us->ppa_id);
550 putctl4(lq, M_CTL, PPPCTL_MRU, us->mru);
551 putctl4(lq, M_CTL, PPPCTL_MTU, us->mtu);
553 /* Lower tty driver's queue hiwat/lowat from default 4096/128
554 to 256/128 since we don't want queueing of data on
555 output to physical device */
558 for (tlq = lq; tlq->q_next != NULL; tlq = tlq->q_next)
560 strqset(tlq, QHIWAT, 0, 256);
561 strqset(tlq, QLOWAT, 0, 128);
567 lb = (struct linkblk *) mp->b_cont->b_rptr;
569 if (us->lowerq != lb->l_qbot)
570 DPRINT2("ppp unlink: lowerq=%x qbot=%x\n",
571 us->lowerq, lb->l_qbot);
576 /* Unblock streams which now feed back up the control stream. */
581 if (us->flags & US_CONTROL)
583 if ((us->flags & US_PRIV) == 0) {
587 /* Arrange to return an int */
588 if ((mq = mp->b_cont) == 0
589 || mq->b_datap->db_lim - mq->b_rptr < sizeof(int)) {
590 mq = allocb(sizeof(int), BPRI_HI);
600 iop->ioc_count = sizeof(int);
601 mq->b_wptr = mq->b_rptr + sizeof(int);
602 qwriter(q, mp, new_ppa, PERIM_OUTER);
607 /* like dlpi_attach, for programs which can't write to
608 the stream (like pppstats) */
609 if (iop->ioc_count != sizeof(int) || us->ppa != 0)
611 n = *(int *)mp->b_cont->b_rptr;
612 for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
613 if (ppa->ppa_id == n)
619 qwriter(q, mp, attach_ppa, PERIM_OUTER);
625 /* Attach to a given SAP. */
626 if (iop->ioc_count != sizeof(int) || us->ppa == 0)
628 n = *(int *)mp->b_cont->b_rptr;
629 /* n must be a valid PPP network protocol number. */
630 if (n < 0x21 || n > 0x3fff || (n & 0x101) != 1)
632 /* check that no other stream is bound to this sap already. */
633 for (os = us->ppa; os != 0; os = os->next)
645 if (iop->ioc_count != sizeof(int) || (us->flags & US_CONTROL) == 0)
647 n = *(int *)mp->b_cont->b_rptr;
648 if (n <= 0 || n > PPP_MAXMRU)
654 putctl4(us->lowerq, M_CTL, PPPCTL_MRU, n);
660 if (iop->ioc_count != sizeof(int) || (us->flags & US_CONTROL) == 0)
662 n = *(int *)mp->b_cont->b_rptr;
663 if (n <= 0 || n > PPP_MAXMTU)
667 /* The MTU reported in netstat, not used as IP max packet size! */
668 us->ifstats.ifs_mtu = n;
671 putctl4(us->lowerq, M_CTL, PPPCTL_MTU, n);
677 us->flags |= US_LASTMOD;
682 if (iop->ioc_count != sizeof(int))
684 n = *(int *)mp->b_cont->b_rptr;
685 if (n == PPPDBG_DUMP + PPPDBG_DRIVER) {
686 qwriter(q, NULL, debug_dump, PERIM_OUTER);
689 } else if (n == PPPDBG_LOG + PPPDBG_DRIVER) {
690 DPRINT1("ppp/%d: debug log enabled\n", us->mn);
691 us->flags |= US_DBGLOG;
695 if (us->ppa == 0 || us->ppa->lowerq == 0)
697 putnext(us->ppa->lowerq, mp);
703 if (iop->ioc_count != 2 * sizeof(int))
705 if ((us->flags & US_CONTROL) == 0)
707 sap = ((int *)mp->b_cont->b_rptr)[0];
708 for (nps = us->next; nps != 0; nps = nps->next)
712 if (us->flags & US_DBGLOG)
713 DPRINT2("ppp/%d: no stream for sap %x\n", us->mn, sap);
716 nps->npmode = (enum NPmode) ((int *)mp->b_cont->b_rptr)[1];
717 if (nps->npmode == NPMODE_DROP || nps->npmode == NPMODE_ERROR)
718 flushq(WR(nps->q), FLUSHDATA);
719 else if (nps->npmode == NPMODE_PASS && qsize(WR(nps->q)) > 0
720 && (nps->flags & US_BLOCKED) == 0)
727 if ((ppa = us->ppa) == 0)
729 mq = allocb(sizeof(struct ppp_idle), BPRI_HI);
738 pip = (struct ppp_idle *) mq->b_wptr;
739 pip->xmit_idle = time - ppa->last_sent;
740 pip->recv_idle = time - ppa->last_recv;
741 mq->b_wptr += sizeof(struct ppp_idle);
742 iop->ioc_count = sizeof(struct ppp_idle);
748 /* Sent from IP down to us. Attach the ifstats structure. */
749 if (iop->ioc_count != sizeof(struct ifreq) || us->ppa == 0)
751 ifr = (struct ifreq *)mp->b_cont->b_rptr;
752 /* Find the unit number in the interface name. */
753 for (i = 0; i < IFNAMSIZ; i++) {
754 if (ifr->ifr_name[i] == 0 ||
755 (ifr->ifr_name[i] >= '0' &&
756 ifr->ifr_name[i] <= '9'))
759 us->ifname[i] = ifr->ifr_name[i];
763 /* Convert the unit number to binary. */
764 for (n = 0; i < IFNAMSIZ; i++) {
765 if (ifr->ifr_name[i] == 0) {
769 n = n * 10 + ifr->ifr_name[i] - '0';
773 /* Verify the ppa. */
774 if (us->ppa->ppa_id != n)
778 /* Set up the netstat block. */
779 strncpy (ppa->ifname, us->ifname, IFNAMSIZ);
781 ppa->ifstats.ifs_name = ppa->ifname;
782 ppa->ifstats.ifs_unit = n;
783 ppa->ifstats.ifs_active = us->state != DL_UNBOUND;
784 ppa->ifstats.ifs_mtu = ppa->mtu;
786 /* Link in statistics used by netstat. */
787 ppa->ifstats.ifs_next = ifstats;
788 ifstats = &ppa->ifstats;
795 if (!(us->flags & US_CONTROL)) {
801 ((struct iocblk_in *)iop)->ioc_ifflags = us->ifflags;
806 if (!(us->flags & US_CONTROL)) {
812 us->ifflags = ((struct iocblk_in *)iop)->ioc_ifflags;
817 if (!(us->flags & US_CONTROL)) {
823 us->ifflags |= IFF_RUNNING;
824 ((struct iocblk_in *)iop)->ioc_ifflags |= IFF_RUNNING;
830 * Vanilla SVR4 systems don't handle SIOCSIFMTU, rather
831 * they take the MTU from the DL_INFO_ACK we sent in response
832 * to their DL_INFO_REQ. Fortunately, they will update the
833 * MTU if we send an unsolicited DL_INFO_ACK up.
835 if ((mq = allocb(sizeof(dl_info_req_t), BPRI_HI)) == 0)
836 break; /* should do bufcall */
837 ((union DL_primitives *)mq->b_rptr)->dl_primitive = DL_INFO_REQ;
838 mq->b_wptr = mq->b_rptr + sizeof(dl_info_req_t);
839 dlpi_request(q, mq, us);
854 if (us->ppa == 0 || us->ppa->lowerq == 0)
856 us->ioc_id = iop->ioc_id;
858 switch (iop->ioc_cmd) {
861 if (us->flags & US_LASTMOD) {
865 putnext(us->ppa->lowerq, mp);
868 if (us->flags & US_PRIV)
869 putnext(us->ppa->lowerq, mp);
871 DPRINT1("ppp ioctl %x rejected\n", iop->ioc_cmd);
880 iop->ioc_error = error;
881 mp->b_datap->db_type = M_IOCNAK;
883 } else if (error == 0) {
884 mp->b_datap->db_type = M_IOCACK;
890 if (us->flags & US_DBGLOG)
891 DPRINT2("ppp/%d: flush %x\n", us->mn, *mp->b_rptr);
892 if (*mp->b_rptr & FLUSHW)
893 flushq(q, FLUSHDATA);
894 if (*mp->b_rptr & FLUSHR) {
895 *mp->b_rptr &= ~FLUSHW;
910 dlpi_request(q, mp, us)
915 union DL_primitives *d = (union DL_primitives *) mp->b_rptr;
916 int size = mp->b_wptr - mp->b_rptr;
918 upperstr_t *ppa, *os;
923 if (us->flags & US_DBGLOG)
924 DPRINT3("ppp/%d: dlpi prim %x len=%d\n", us->mn,
925 d->dl_primitive, size);
926 switch (d->dl_primitive) {
928 if (size < sizeof(dl_info_req_t))
930 if ((reply = allocb(sizeof(dl_info_ack_t), BPRI_HI)) == 0)
931 break; /* should do bufcall */
932 reply->b_datap->db_type = M_PCPROTO;
933 info = (dl_info_ack_t *) reply->b_wptr;
934 reply->b_wptr += sizeof(dl_info_ack_t);
935 bzero((caddr_t) info, sizeof(dl_info_ack_t));
936 info->dl_primitive = DL_INFO_ACK;
937 info->dl_max_sdu = us->ppa->mtu;
938 info->dl_min_sdu = 1;
939 info->dl_addr_length = sizeof(ulong);
941 info->dl_mac_type = DL_OTHER;
943 info->dl_mac_type = DL_HDLC; /* a lie */
945 info->dl_current_state = us->state;
946 info->dl_service_mode = DL_CLDLS;
947 info->dl_provider_style = DL_STYLE2;
948 #if DL_CURRENT_VERSION >= 2
949 info->dl_sap_length = sizeof(ulong);
950 info->dl_version = DL_CURRENT_VERSION;
956 if (size < sizeof(dl_attach_req_t))
958 if (us->state != DL_UNATTACHED || us->ppa != 0) {
959 dlpi_error(q, us, DL_ATTACH_REQ, DL_OUTSTATE, 0);
962 for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
963 if (ppa->ppa_id == d->attach_req.dl_ppa)
966 dlpi_error(q, us, DL_ATTACH_REQ, DL_BADPPA, 0);
970 qwriter(q, mp, attach_ppa, PERIM_OUTER);
974 if (size < sizeof(dl_detach_req_t))
976 if (us->state != DL_UNBOUND || us->ppa == 0) {
977 dlpi_error(q, us, DL_DETACH_REQ, DL_OUTSTATE, 0);
980 qwriter(q, mp, detach_ppa, PERIM_OUTER);
984 if (size < sizeof(dl_bind_req_t))
986 if (us->state != DL_UNBOUND || us->ppa == 0) {
987 dlpi_error(q, us, DL_BIND_REQ, DL_OUTSTATE, 0);
991 /* apparently this test fails (unnecessarily?) on some systems */
992 if (d->bind_req.dl_service_mode != DL_CLDLS) {
993 dlpi_error(q, us, DL_BIND_REQ, DL_UNSUPPORTED, 0);
998 /* saps must be valid PPP network protocol numbers,
999 except that we accept ETHERTYPE_IP in place of PPP_IP. */
1000 sap = d->bind_req.dl_sap;
1002 if (sap == ETHERTYPE_IP)
1004 if (sap < 0x21 || sap > 0x3fff || (sap & 0x101) != 1) {
1005 dlpi_error(q, us, DL_BIND_REQ, DL_BADADDR, 0);
1009 /* check that no other stream is bound to this sap already. */
1010 for (os = us->ppa; os != 0; os = os->next)
1014 dlpi_error(q, us, DL_BIND_REQ, DL_NOADDR, 0);
1019 us->state = DL_IDLE;
1021 if ((reply = allocb(sizeof(dl_bind_ack_t) + sizeof(ulong),
1023 break; /* should do bufcall */
1024 ackp = (dl_bind_ack_t *) reply->b_wptr;
1025 reply->b_wptr += sizeof(dl_bind_ack_t) + sizeof(ulong);
1026 reply->b_datap->db_type = M_PCPROTO;
1027 bzero((caddr_t) ackp, sizeof(dl_bind_ack_t));
1028 ackp->dl_primitive = DL_BIND_ACK;
1030 ackp->dl_addr_length = sizeof(ulong);
1031 ackp->dl_addr_offset = sizeof(dl_bind_ack_t);
1032 *(ulong *)(ackp+1) = sap;
1037 if (size < sizeof(dl_unbind_req_t))
1039 if (us->state != DL_IDLE) {
1040 dlpi_error(q, us, DL_UNBIND_REQ, DL_OUTSTATE, 0);
1044 us->state = DL_UNBOUND;
1046 us->ppa->ifstats.ifs_active = 0;
1048 dlpi_ok(q, DL_UNBIND_REQ);
1051 case DL_UNITDATA_REQ:
1052 if (size < sizeof(dl_unitdata_req_t))
1054 if (us->state != DL_IDLE) {
1055 dlpi_error(q, us, DL_UNITDATA_REQ, DL_OUTSTATE, 0);
1058 if ((ppa = us->ppa) == 0) {
1059 cmn_err(CE_CONT, "ppp: in state dl_idle but ppa == 0?\n");
1062 len = mp->b_cont == 0? 0: msgdsize(mp->b_cont);
1063 if (len > ppa->mtu) {
1064 DPRINT2("dlpi data too large (%d > %d)\n", len, ppa->mtu);
1069 /* Extract s_port & d_port from IP-packet, the code is a bit
1070 dirty here, but so am I, too... */
1071 if (mp->b_datap->db_type == M_PROTO && us->sap == PPP_IP
1072 && mp->b_cont != 0) {
1076 u_char band_unset, cur_band, syn;
1077 u_short s_port, d_port;
1079 bb = mp->b_cont->b_rptr; /* bb points to IP-header*/
1080 len = mp->b_cont->b_wptr - mp->b_cont->b_rptr;
1082 s_port = IPPORT_DEFAULT;
1083 d_port = IPPORT_DEFAULT;
1084 if (len >= 20) { /* 20 = minimum length of IP header */
1085 iphlen = (bb[0] & 0x0f) * 4;
1090 if (len >= 20) { /* min length of TCP header */
1091 s_port = (tlh[0] << 8) + tlh[1];
1092 d_port = (tlh[2] << 8) + tlh[3];
1093 syn = tlh[13] & 0x02;
1097 if (len >= 8) { /* min length of UDP header */
1098 s_port = (tlh[0] << 8) + tlh[1];
1099 d_port = (tlh[2] << 8) + tlh[3];
1106 * Now calculate b_band for this packet from the
1107 * port-priority table.
1110 cur_band = max_band;
1113 while (*ptr && band_unset)
1114 if (s_port == *ptr || d_port == *ptr++) {
1115 mp->b_band = cur_band;
1123 mp->b_band = def_band;
1124 /* It may be usable to urge SYN packets a bit */
1129 /* this assumes PPP_HDRLEN <= sizeof(dl_unitdata_req_t) */
1130 if (mp->b_datap->db_ref > 1) {
1131 np = allocb(PPP_HDRLEN, BPRI_HI);
1134 np->b_cont = mp->b_cont;
1139 mp->b_datap->db_type = M_DATA;
1140 /* XXX should use dl_dest_addr_offset/length here,
1141 but we would have to translate ETHERTYPE_IP -> PPP_IP */
1142 mp->b_wptr = mp->b_rptr + PPP_HDRLEN;
1143 mp->b_rptr[0] = PPP_ALLSTATIONS;
1144 mp->b_rptr[1] = PPP_UI;
1145 mp->b_rptr[2] = us->sap >> 8;
1146 mp->b_rptr[3] = us->sap;
1147 if (pass_packet(us, mp, 1)) {
1148 if (!send_data(mp, us))
1153 #if DL_CURRENT_VERSION >= 2
1154 case DL_SUBS_BIND_REQ:
1155 case DL_SUBS_UNBIND_REQ:
1156 case DL_ENABMULTI_REQ:
1157 case DL_DISABMULTI_REQ:
1158 case DL_PROMISCON_REQ:
1159 case DL_PROMISCOFF_REQ:
1160 case DL_PHYS_ADDR_REQ:
1161 case DL_SET_PHYS_ADDR_REQ:
1164 case DL_REPLY_UPDATE_REQ:
1166 case DL_DATA_ACK_REQ:
1168 case DL_CONNECT_REQ:
1170 dlpi_error(q, us, d->dl_primitive, DL_NOTSUPPORTED, 0);
1173 case DL_CONNECT_RES:
1174 case DL_DISCONNECT_REQ:
1177 dlpi_error(q, us, d->dl_primitive, DL_OUTSTATE, 0);
1181 dlpi_error(q, us, d->dl_primitive, DL_BADQOSTYPE, 0);
1184 #if DL_CURRENT_VERSION >= 2
1191 cmn_err(CE_CONT, "ppp: unknown dlpi prim 0x%x\n", d->dl_primitive);
1194 dlpi_error(q, us, d->dl_primitive, DL_BADPRIM, 0);
1201 dlpi_error(q, us, prim, err, uerr)
1204 int prim, err, uerr;
1207 dl_error_ack_t *errp;
1209 if (us->flags & US_DBGLOG)
1210 DPRINT3("ppp/%d: dlpi error, prim=%x, err=%x\n", us->mn, prim, err);
1211 reply = allocb(sizeof(dl_error_ack_t), BPRI_HI);
1213 return; /* XXX should do bufcall */
1214 reply->b_datap->db_type = M_PCPROTO;
1215 errp = (dl_error_ack_t *) reply->b_wptr;
1216 reply->b_wptr += sizeof(dl_error_ack_t);
1217 errp->dl_primitive = DL_ERROR_ACK;
1218 errp->dl_error_primitive = prim;
1219 errp->dl_errno = err;
1220 errp->dl_unix_errno = uerr;
1232 reply = allocb(sizeof(dl_ok_ack_t), BPRI_HI);
1234 return; /* XXX should do bufcall */
1235 reply->b_datap->db_type = M_PCPROTO;
1236 okp = (dl_ok_ack_t *) reply->b_wptr;
1237 reply->b_wptr += sizeof(dl_ok_ack_t);
1238 okp->dl_primitive = DL_OK_ACK;
1239 okp->dl_correct_primitive = prim;
1242 #endif /* NO_DLPI */
1245 pass_packet(us, mp, outbound)
1258 #ifdef FILTER_PACKETS
1259 pass = ip_hard_filter(us, mp, outbound);
1262 * Here is where we might, in future, decide whether to pass
1263 * or drop the packet, and whether it counts as link activity.
1266 #endif /* FILTER_PACKETS */
1269 /* pass only if link already up, and don't update time */
1270 if (us->ppa->lowerq == 0) {
1277 ppa->last_sent = time;
1279 ppa->last_recv = time;
1293 if ((us->flags & US_BLOCKED) || us->npmode == NPMODE_QUEUE)
1296 if (ppa == 0 || us->npmode == NPMODE_DROP || us->npmode == NPMODE_ERROR) {
1297 if (us->flags & US_DBGLOG)
1298 DPRINT2("ppp/%d: dropping pkt (npmode=%d)\n", us->mn, us->npmode);
1302 if ((q = ppa->lowerq) == 0) {
1303 /* try to send it up the control stream */
1304 if (bcanputnext(ppa->q, mp->b_band)) {
1306 * The message seems to get corrupted for some reason if
1307 * we just send the message up as it is, so we send a copy.
1309 mblk_t *np = copymsg(mp);
1312 putnext(ppa->q, np);
1316 if (bcanputnext(ppa->lowerq, mp->b_band)) {
1318 * The lower write queue's put procedure just updates counters
1319 * and does a putnext. We call it so that on SMP systems, we
1320 * enter the lower queues' perimeter so that the counter
1321 * updates are serialized.
1323 put(ppa->lowerq, mp);
1327 us->flags |= US_BLOCKED;
1332 * Allocate a new PPA id and link this stream into the list of PPAs.
1333 * This procedure is called with an exclusive lock on all queues in
1341 upperstr_t *us, **usp;
1346 while ((us = *usp) != 0 && ppa_id == us->ppa_id) {
1350 us = (upperstr_t *) q->q_ptr;
1351 us->ppa_id = ppa_id;
1356 us->flags |= US_CONTROL;
1357 us->npmode = NPMODE_PASS;
1364 * Create a kstats record for our statistics, so netstat -i works.
1366 if (us->kstats == 0) {
1369 sprintf(unit, "ppp%d", us->ppa->ppa_id);
1370 us->kstats = kstat_create("ppp", us->ppa->ppa_id, unit,
1371 "net", KSTAT_TYPE_NAMED, 4, 0);
1372 if (us->kstats != 0) {
1373 kstat_named_t *kn = KSTAT_NAMED_PTR(us->kstats);
1375 strcpy(kn[0].name, "ipackets");
1376 kn[0].data_type = KSTAT_DATA_ULONG;
1377 strcpy(kn[1].name, "ierrors");
1378 kn[1].data_type = KSTAT_DATA_ULONG;
1379 strcpy(kn[2].name, "opackets");
1380 kn[2].data_type = KSTAT_DATA_ULONG;
1381 strcpy(kn[3].name, "oerrors");
1382 kn[3].data_type = KSTAT_DATA_ULONG;
1383 kstat_install(us->kstats);
1388 *(int *)mp->b_cont->b_rptr = ppa_id;
1389 mp->b_datap->db_type = M_IOCACK;
1400 us = (upperstr_t *) q->q_ptr;
1402 us->state = DL_UNBOUND;
1404 for (t = us->ppa; t->next != 0; t = t->next)
1408 if (mp->b_datap->db_type == M_IOCTL) {
1409 mp->b_datap->db_type = M_IOCACK;
1413 dlpi_ok(q, DL_ATTACH_REQ);
1425 us = (upperstr_t *) q->q_ptr;
1426 for (t = us->ppa; t->next != 0; t = t->next)
1427 if (t->next == us) {
1434 us->state = DL_UNATTACHED;
1435 dlpi_ok(q, DL_DETACH_REQ);
1444 struct lowerstr *ls;
1448 us = (upperstr_t *) q->q_ptr;
1449 us->flags &= ~US_BLOCKED;
1450 while ((mp = getq(q)) != 0) {
1451 if (!send_data(mp, us)) {
1466 ppa = (upperstr_t *) q->q_ptr;
1467 if (ppa != 0) { /* why wouldn't it? */
1468 ppa->stats.ppp_opackets++;
1469 ppa->stats.ppp_obytes += msgdsize(mp);
1470 #ifdef INCR_OPACKETS
1485 * Flow control has back-enabled this stream:
1486 * enable the write service procedures of all upper
1487 * streams feeding this lower stream.
1489 for (us = (upperstr_t *) q->q_ptr; us != NULL; us = us->next)
1490 if (us->flags & US_BLOCKED)
1499 upperstr_t *us, *as;
1502 dl_unitdata_ind_t *ud;
1506 us = (upperstr_t *) q->q_ptr;
1507 if (us->flags & US_CONTROL) {
1510 * If there is no lower queue attached, run the write service
1511 * routines of other upper streams attached to this PPA.
1513 if (us->lowerq == 0) {
1516 if (as->flags & US_BLOCKED)
1523 * A network protocol stream. Put a DLPI header on each
1524 * packet and send it on.
1525 * (Actually, it seems that the IP module will happily
1526 * accept M_DATA messages without the DL_UNITDATA_IND header.)
1528 while ((mp = getq(q)) != 0) {
1529 if (!canputnext(q)) {
1534 proto = PPP_PROTOCOL(mp->b_rptr);
1535 mp->b_rptr += PPP_HDRLEN;
1536 hdr = allocb(sizeof(dl_unitdata_ind_t) + 2 * sizeof(ulong),
1539 /* XXX should put it back and use bufcall */
1543 hdr->b_datap->db_type = M_PROTO;
1544 ud = (dl_unitdata_ind_t *) hdr->b_wptr;
1545 hdr->b_wptr += sizeof(dl_unitdata_ind_t) + 2 * sizeof(ulong);
1547 ud->dl_primitive = DL_UNITDATA_IND;
1548 ud->dl_dest_addr_length = sizeof(ulong);
1549 ud->dl_dest_addr_offset = sizeof(dl_unitdata_ind_t);
1550 ud->dl_src_addr_length = sizeof(ulong);
1551 ud->dl_src_addr_offset = ud->dl_dest_addr_offset + sizeof(ulong);
1552 #if DL_CURRENT_VERSION >= 2
1553 ud->dl_group_address = 0;
1555 /* Send the DLPI client the data with the SAP they requested,
1556 (e.g. ETHERTYPE_IP) rather than the PPP protocol number
1558 ((ulong *)(ud + 1))[0] = us->req_sap; /* dest SAP */
1559 ((ulong *)(ud + 1))[1] = us->req_sap; /* src SAP */
1563 #endif /* NO_DLPI */
1568 * If this stream is attached to a PPA with a lower queue pair,
1569 * enable the read queue's service routine if it has data queued.
1570 * XXX there is a possibility that packets could get out of order
1571 * if ppplrput now runs before ppplrsrv.
1573 if (us->ppa != 0 && us->ppa->lowerq != 0)
1574 qenable(RD(us->ppa->lowerq));
1580 find_dest(ppa, proto)
1586 for (us = ppa->next; us != 0; us = us->next)
1587 if (proto == us->sap)
1597 upperstr_t *ppa, *us;
1603 ppa = (upperstr_t *) q->q_ptr;
1605 DPRINT1("ppplrput: q = %x, ppa = 0??\n", q);
1609 switch (mp->b_datap->db_type) {
1611 if (*mp->b_rptr & FLUSHW) {
1612 *mp->b_rptr &= ~FLUSHR;
1619 switch (*mp->b_rptr) {
1624 ppa->stats.ppp_ierrors++;
1630 ppa->stats.ppp_oerrors++;
1639 * Attempt to match up the response with the stream
1640 * that the request came from.
1642 iop = (struct iocblk *) mp->b_rptr;
1643 for (us = ppa; us != 0; us = us->next)
1644 if (us->ioc_id == iop->ioc_id)
1654 * The serial device has hung up. We don't want to send
1655 * the M_HANGUP message up to pppd because that will stop
1656 * us from using the control stream any more. Instead we
1657 * send a zero-length message as an end-of-file indication.
1660 mp = allocb(1, BPRI_HI);
1662 DPRINT1("ppp/%d: couldn't allocate eof message!\n", ppa->mn);
1665 putnext(ppa->q, mp);
1669 if (mp->b_datap->db_type == M_DATA) {
1671 if (mp->b_wptr - mp->b_rptr < PPP_HDRLEN) {
1672 PULLUP(mp, PPP_HDRLEN);
1674 DPRINT1("ppp_lrput: msgpullup failed (len=%d)\n", len);
1678 ppa->stats.ppp_ipackets++;
1679 ppa->stats.ppp_ibytes += len;
1680 #ifdef INCR_IPACKETS
1683 proto = PPP_PROTOCOL(mp->b_rptr);
1684 if (proto < 0x8000 && (us = find_dest(ppa, proto)) != 0) {
1686 * A data packet for some network protocol.
1687 * Queue it on the upper stream for that protocol.
1689 if (!pass_packet(us, mp, 0))
1699 * A control frame, a frame for an unknown protocol,
1700 * or some other message type.
1701 * Send it up to pppd via the control stream.
1703 if (queclass(mp) == QPCTL || canputnext(ppa->q))
1704 putnext(ppa->q, mp);
1718 upperstr_t *ppa, *us;
1722 * Packets only get queued here for flow control reasons.
1724 ppa = (upperstr_t *) q->q_ptr;
1725 while ((mp = getq(q)) != 0) {
1726 if (mp->b_datap->db_type == M_DATA
1727 && (proto = PPP_PROTOCOL(mp->b_rptr)) < 0x8000
1728 && (us = find_dest(ppa, proto)) != 0) {
1736 if (canputnext(ppa->q))
1737 putnext(ppa->q, mp);
1748 putctl2(q, type, code, val)
1750 int type, code, val;
1754 mp = allocb(2, BPRI_HI);
1757 mp->b_datap->db_type = type;
1758 mp->b_wptr[0] = code;
1759 mp->b_wptr[1] = val;
1766 putctl4(q, type, code, val)
1768 int type, code, val;
1772 mp = allocb(4, BPRI_HI);
1775 mp->b_datap->db_type = type;
1776 mp->b_wptr[0] = code;
1777 ((short *)mp->b_wptr)[1] = val;
1785 queue_t *q; /* not used */
1786 mblk_t *mp; /* not used either */
1791 DPRINT("ppp upper streams:\n");
1792 for (us = minor_devs; us != 0; us = us->nextmn) {
1794 DPRINT3(" %d: q=%x rlev=%d",
1795 us->mn, uq, (uq? qsize(uq): 0));
1796 DPRINT3(" wlev=%d flags=0x%b", (uq? qsize(WR(uq)): 0),
1797 us->flags, "\020\1priv\2control\3blocked\4last");
1798 DPRINT3(" state=%x sap=%x req_sap=%x", us->state, us->sap,
1803 DPRINT1(" ppa=%d\n", us->ppa->ppa_id);
1804 if (us->flags & US_CONTROL) {
1806 DPRINT3(" control for %d lq=%x rlev=%d",
1807 us->ppa_id, lq, (lq? qsize(RD(lq)): 0));
1808 DPRINT3(" wlev=%d mru=%d mtu=%d\n",
1809 (lq? qsize(lq): 0), us->mru, us->mtu);
1814 #ifdef FILTER_PACKETS
1815 #include <netinet/in_systm.h>
1816 #include <netinet/ip.h>
1817 #include <netinet/udp.h>
1818 #include <netinet/tcp.h>
1820 #define MAX_IPHDR 128 /* max TCP/IP header size */
1823 /* The following table contains a hard-coded list of protocol/port pairs.
1824 * Any matching packets are either discarded unconditionally, or,
1825 * if ok_if_link_up is non-zero when a connection does not currently exist
1826 * (i.e., they go through if the connection is present, but never initiate
1828 * This idea came from a post by dm@garage.uun.org (David Mazieres)
1830 static struct pktfilt_tab {
1833 u_short ok_if_link_up;
1835 { IPPROTO_UDP, 520, 1 }, /* RIP, ok to pass if link is up */
1836 { IPPROTO_UDP, 123, 1 }, /* NTP, don't keep up the link for it */
1837 { -1, 0, 0 } /* terminator entry has port == -1 */
1842 ip_hard_filter(us, mp, outbound)
1848 struct pktfilt_tab *pft;
1854 /* Note, the PPP header has already been pulled up in all cases */
1855 proto = PPP_PROTOCOL(mp->b_rptr);
1856 if (us->flags & US_DBGLOG)
1857 DPRINT3("ppp/%d: filter, proto=0x%x, out=%d\n", us->mn, proto, outbound);
1862 if ((mp->b_wptr - mp->b_rptr) == PPP_HDRLEN) {
1863 temp_mp = mp->b_cont;
1864 len = msgdsize(temp_mp);
1865 hlen = (len < MAX_IPHDR) ? len : MAX_IPHDR;
1866 PULLUP(temp_mp, hlen);
1868 DPRINT2("ppp/%d: filter, pullup next failed, len=%d\n",
1870 mp->b_cont = 0; /* PULLUP() freed the rest */
1874 ip = (struct ip *)mp->b_cont->b_rptr;
1878 hlen = (len < (PPP_HDRLEN+MAX_IPHDR)) ? len : (PPP_HDRLEN+MAX_IPHDR);
1881 DPRINT2("ppp/%d: filter, pullup failed, len=%d\n",
1885 ip = (struct ip *)(mp->b_rptr + PPP_HDRLEN);
1888 /* For IP traffic, certain packets (e.g., RIP) may be either
1889 * 1. ignored - dropped completely
1890 * 2. will not initiate a connection, but
1891 * will be passed if a connection is currently up.
1893 for (pft=pktfilt_tab; pft->proto != -1; pft++) {
1894 if (ip->ip_p == pft->proto) {
1895 switch(pft->proto) {
1897 if (((struct udphdr *) &((int *)ip)[ip->ip_hl])->uh_dport
1898 == htons(pft->port)) goto endfor;
1901 if (((struct tcphdr *) &((int *)ip)[ip->ip_hl])->th_dport
1902 == htons(pft->port)) goto endfor;
1908 if (pft->proto != -1) {
1909 if (us->flags & US_DBGLOG)
1910 DPRINT3("ppp/%d: found IP pkt, proto=0x%x (%d)\n",
1911 us->mn, pft->proto, pft->port);
1912 /* Discard if not connected, or if not pass_with_link_up */
1913 /* else, if link is up let go by, but don't update time */
1914 return pft->ok_if_link_up? -1: 0;
1917 } /* end switch (proto) */
1921 #endif /* FILTER_PACKETS */