2 * ppp.c - STREAMS multiplexing pseudo-device driver for PPP.
4 * Copyright (c) 1994 Paul Mackerras. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * 3. The name(s) of the authors of this software must not be used to
19 * endorse or promote products derived from this software without
20 * prior written permission.
22 * 4. Redistributions of any form whatsoever must retain the following
24 * "This product includes software developed by Paul Mackerras
25 * <paulus@samba.org>".
27 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
28 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
29 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
30 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
31 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
32 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
33 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
37 * This file is used under Solaris 2, SVR4, SunOS 4, and Digital UNIX.
40 #include <sys/types.h>
41 #include <sys/param.h>
43 #include <sys/stream.h>
44 #include <sys/stropts.h>
45 #include <sys/errno.h>
47 #include <sys/ioctl.h>
48 #include <sys/cmn_err.h>
49 #define queclass(mp) ((mp)->b_band & QPCTL)
51 #include <sys/ioccom.h>
55 #include <sys/cmn_err.h>
60 #include <sys/ksynch.h>
61 #include <sys/kstat.h>
62 #include <sys/sunddi.h>
63 #include <sys/ethernet.h>
65 #include <sys/socket.h>
66 #include <sys/sockio.h>
68 #include <netinet/in.h>
73 #include <net/ppp_defs.h>
74 #include <net/pppio.h>
78 * Modifications marked with #ifdef PRIOQ are for priority queueing of
79 * interactive traffic, and are due to Marko Zec <zec@japa.tel.fer.hr>.
84 #include <netinet/in.h> /* leave this outside of PRIOQ for htons */
87 * The IP module may use this SAP value for IP packets.
90 #define ETHERTYPE_IP 0x800
93 #if !defined(ETHERTYPE_IPV6)
94 #define ETHERTYPE_IPV6 0x86dd
95 #endif /* !defined(ETHERTYPE_IPV6) */
97 #if !defined(ETHERTYPE_ALLSAP) && defined(SOL2)
98 #define ETHERTYPE_ALLSAP 0
99 #endif /* !defined(ETHERTYPE_ALLSAP) && defined(SOL2) */
101 #if !defined(PPP_ALLSAP) && defined(SOL2)
102 #define PPP_ALLSAP PPP_ALLSTATIONS
103 #endif /* !defined(PPP_ALLSAP) && defined(SOL2) */
109 * We use this reader-writer lock to ensure that the lower streams
110 * stay connected to the upper streams while the lower-side put and
111 * service procedures are running. Essentially it is an existence
112 * lock for the upper stream associated with each lower stream.
114 krwlock_t ppp_lower_lock;
115 #define LOCK_LOWER_W rw_enter(&ppp_lower_lock, RW_WRITER)
116 #define LOCK_LOWER_R rw_enter(&ppp_lower_lock, RW_READER)
117 #define TRYLOCK_LOWER_R rw_tryenter(&ppp_lower_lock, RW_READER)
118 #define UNLOCK_LOWER rw_exit(&ppp_lower_lock)
120 #define MT_ENTER(x) mutex_enter(x)
121 #define MT_EXIT(x) mutex_exit(x)
124 * Notes on multithreaded implementation for Solaris 2:
126 * We use an inner perimeter around each queue pair and an outer
127 * perimeter around the whole driver. The inner perimeter is
128 * entered exclusively for all entry points (open, close, put,
129 * service). The outer perimeter is entered exclusively for open
130 * and close and shared for put and service. This is all done for
131 * us by the streams framework.
133 * I used to think that the perimeters were entered for the lower
134 * streams' put and service routines as well as for the upper streams'.
135 * Because of problems experienced by people, and after reading the
136 * documentation more closely, I now don't think that is true. So we
137 * now use ppp_lower_lock to give us an existence guarantee on the
138 * upper stream controlling each lower stream.
140 * Shared entry to the outer perimeter protects the existence of all
141 * the upper streams and their upperstr_t structures, and guarantees
142 * that the following fields of any upperstr_t won't change:
143 * nextmn, next, nextppa. It guarantees that the lowerq field of an
144 * upperstr_t won't go from non-zero to zero, that the global `ppas'
145 * won't change and that the no lower stream will get unlinked.
147 * Shared (reader) access to ppa_lower_lock guarantees that no lower
148 * stream will be unlinked and that the lowerq field of all upperstr_t
149 * structures won't change.
153 #define LOCK_LOWER_W 0
154 #define LOCK_LOWER_R 0
155 #define TRYLOCK_LOWER_R 1
156 #define UNLOCK_LOWER 0
157 #define MT_ENTER(x) 0
163 * Private information; one per upper stream.
165 typedef struct upperstr {
166 minor_t mn; /* minor device number */
167 struct upperstr *nextmn; /* next minor device */
168 queue_t *q; /* read q associated with this upper stream */
169 int flags; /* flag bits, see below */
170 int state; /* current DLPI state */
171 int sap; /* service access point */
172 int req_sap; /* which SAP the DLPI client requested */
173 struct upperstr *ppa; /* control stream for our ppa */
174 struct upperstr *next; /* next stream for this ppa */
175 uint ioc_id; /* last ioctl ID for this stream */
176 enum NPmode npmode; /* what to do with packets on this SAP */
177 unsigned char rblocked; /* flow control has blocked upper read strm */
178 /* N.B. rblocked is only changed by control stream's put/srv procs */
180 * There is exactly one control stream for each PPA.
181 * The following fields are only used for control streams.
184 queue_t *lowerq; /* write queue attached below this PPA */
185 struct upperstr *nextppa; /* next control stream */
188 struct pppstat stats; /* statistics */
189 time_t last_sent; /* time last NP packet sent */
190 time_t last_recv; /* time last NP packet rcvd */
192 kmutex_t stats_lock; /* lock for stats updates */
193 kstat_t *kstats; /* stats for netstat */
197 char ifname[IFNAMSIZ];
198 struct ifstats ifstats;
202 /* Values for flags */
203 #define US_PRIV 1 /* stream was opened by superuser */
204 #define US_CONTROL 2 /* stream is a control stream */
205 #define US_BLOCKED 4 /* flow ctrl has blocked lower write stream */
206 #define US_LASTMOD 8 /* no PPP modules below us */
207 #define US_DBGLOG 0x10 /* log various occurrences */
208 #define US_RBLOCKED 0x20 /* flow ctrl has blocked upper read stream */
211 #if DL_CURRENT_VERSION >= 2
212 #define US_PROMISC 0x40 /* stream is promiscuous */
213 #endif /* DL_CURRENT_VERSION >= 2 */
214 #define US_RAWDATA 0x80 /* raw M_DATA, no DLPI header */
215 #endif /* defined(SOL2) */
218 static u_char max_band=0;
219 static u_char def_band=0;
221 #define IPPORT_DEFAULT 65535
224 * Port priority table
225 * Highest priority ports are listed first, lowest are listed last.
226 * ICMP & packets using unlisted ports will be treated as "default".
227 * If IPPORT_DEFAULT is not listed here, "default" packets will be
228 * assigned lowest priority.
229 * Each line should be terminated with "0".
230 * Line containing only "0" marks the end of the list.
233 static u_short prioq_table[]= {
235 22, 23, 513, 517, 518, 0,
239 20, 70, 80, 8001, 8008, 8080, 0, /* 8001,8008,8080 - common proxy ports */
245 static upperstr_t *minor_devs = NULL;
246 static upperstr_t *ppas = NULL;
249 static int pppopen(queue_t *, dev_t *, int, int, cred_t *);
250 static int pppclose(queue_t *, int, cred_t *);
252 static int pppopen(queue_t *, int, int, int);
253 static int pppclose(queue_t *, int);
255 static int pppurput(queue_t *, mblk_t *);
256 static int pppuwput(queue_t *, mblk_t *);
257 static int pppursrv(queue_t *);
258 static int pppuwsrv(queue_t *);
259 static int ppplrput(queue_t *, mblk_t *);
260 static int ppplwput(queue_t *, mblk_t *);
261 static int ppplrsrv(queue_t *);
262 static int ppplwsrv(queue_t *);
264 static void dlpi_request(queue_t *, mblk_t *, upperstr_t *);
265 static void dlpi_error(queue_t *, upperstr_t *, int, int, int);
266 static void dlpi_ok(queue_t *, int);
268 static int send_data(mblk_t *, upperstr_t *);
269 static void new_ppa(queue_t *, mblk_t *);
270 static void attach_ppa(queue_t *, mblk_t *);
272 static void detach_ppa(queue_t *, mblk_t *);
274 static void detach_lower(queue_t *, mblk_t *);
275 static void debug_dump(queue_t *, mblk_t *);
276 static upperstr_t *find_dest(upperstr_t *, int);
278 static upperstr_t *find_promisc(upperstr_t *, int);
279 static mblk_t *prepend_ether(upperstr_t *, mblk_t *, int);
280 static mblk_t *prepend_udind(upperstr_t *, mblk_t *, int);
281 static void promisc_sendup(upperstr_t *, mblk_t *, int, int);
282 #endif /* defined(SOL2) */
283 static int putctl2(queue_t *, int, int, int);
284 static int putctl4(queue_t *, int, int, int);
285 static int pass_packet(upperstr_t *ppa, mblk_t *mp, int outbound);
286 #ifdef FILTER_PACKETS
287 static int ip_hard_filter(upperstr_t *ppa, mblk_t *mp, int outbound);
288 #endif /* FILTER_PACKETS */
290 #define PPP_ID 0xb1a6
291 static struct module_info ppp_info = {
293 PPP_ID, "ppp", 0, 512, 512, 384
295 PPP_ID, "ppp", 0, 512, 512, 128
299 static struct qinit pppurint = {
300 pppurput, pppursrv, pppopen, pppclose, NULL, &ppp_info, NULL
303 static struct qinit pppuwint = {
304 pppuwput, pppuwsrv, NULL, NULL, NULL, &ppp_info, NULL
307 static struct qinit ppplrint = {
308 ppplrput, ppplrsrv, NULL, NULL, NULL, &ppp_info, NULL
311 static struct qinit ppplwint = {
312 ppplwput, ppplwsrv, NULL, NULL, NULL, &ppp_info, NULL
316 extern struct ifstats *ifstats;
320 struct streamtab pppinfo = {
321 &pppurint, &pppuwint,
328 * How we maintain statistics.
331 #define INCR_IPACKETS(ppa) \
332 if (ppa->kstats != 0) { \
333 KSTAT_NAMED_PTR(ppa->kstats)[0].value.ul++; \
335 #define INCR_IERRORS(ppa) \
336 if (ppa->kstats != 0) { \
337 KSTAT_NAMED_PTR(ppa->kstats)[1].value.ul++; \
339 #define INCR_OPACKETS(ppa) \
340 if (ppa->kstats != 0) { \
341 KSTAT_NAMED_PTR(ppa->kstats)[2].value.ul++; \
343 #define INCR_OERRORS(ppa) \
344 if (ppa->kstats != 0) { \
345 KSTAT_NAMED_PTR(ppa->kstats)[3].value.ul++; \
350 #define INCR_IPACKETS(ppa) ppa->ifstats.ifs_ipackets++;
351 #define INCR_IERRORS(ppa) ppa->ifstats.ifs_ierrors++;
352 #define INCR_OPACKETS(ppa) ppa->ifstats.ifs_opackets++;
353 #define INCR_OERRORS(ppa) ppa->ifstats.ifs_oerrors++;
357 * STREAMS driver entry points.
361 pppopen(q, devp, oflag, sflag, credp)
367 pppopen(q, dev, oflag, sflag)
369 int dev; /* really dev_t */
382 DRV_OPEN_OK(dev); /* device is already open */
385 /* Calculate max_bband & def_band from definitions in prioq.h
386 This colud be done at some more approtiate time (less often)
387 but this way it works well so I'll just leave it here */
395 if (*ptr++ == IPPORT_DEFAULT) {
399 max_band += new_band;
403 def_band = max_band - def_band;
407 if (sflag == CLONEOPEN) {
409 for (prevp = &minor_devs; (up = *prevp) != 0; prevp = &up->nextmn) {
416 mn = getminor(*devp);
420 for (prevp = &minor_devs; (up = *prevp) != 0; prevp = &up->nextmn) {
425 /* this can't happen */
426 q->q_ptr = WR(q)->q_ptr = (caddr_t) up;
432 * Construct a new minor node.
434 up = (upperstr_t *) ALLOC_SLEEP(sizeof(upperstr_t));
435 bzero((caddr_t) up, sizeof(upperstr_t));
437 DPRINT("pppopen: out of kernel memory\n");
444 *devp = makedevice(getmajor(*devp), mn);
448 up->flags |= US_PRIV;
450 up->state = DL_UNATTACHED;
453 up->ifflags = IFF_UP | IFF_POINTOPOINT;
456 up->last_sent = up->last_recv = time;
457 up->npmode = NPMODE_DROP;
458 q->q_ptr = (caddr_t) up;
459 WR(q)->q_ptr = (caddr_t) up;
462 mutex_init(&up->stats_lock, NULL, MUTEX_DRIVER, NULL);
467 DRV_OPEN_OK(makedev(major(dev), mn));
472 pppclose(q, flag, credp)
482 upperstr_t *up, **upp;
483 upperstr_t *as, *asnext;
488 up = (upperstr_t *) q->q_ptr;
490 DPRINT("pppclose: q_ptr = 0\n");
493 if (up->flags & US_DBGLOG)
494 DPRINT2("ppp/%d: close, flags=%x\n", up->mn, up->flags);
495 if (up->flags & US_CONTROL) {
497 struct ifstats *ifp, *pifp;
499 if (up->lowerq != 0) {
500 /* Gack! the lower stream should have be unlinked earlier! */
501 DPRINT1("ppp%d: lower stream still connected on close?\n",
504 up->lowerq->q_ptr = 0;
505 RD(up->lowerq)->q_ptr = 0;
511 * This stream represents a PPA:
512 * For all streams attached to the PPA, clear their
513 * references to this PPA.
514 * Then remove this PPA from the list of PPAs.
516 for (as = up->next; as != 0; as = asnext) {
520 if (as->flags & US_BLOCKED) {
521 as->flags &= ~US_BLOCKED;
522 flushq(WR(as->q), FLUSHDATA);
525 for (upp = &ppas; *upp != 0; upp = &(*upp)->nextppa)
531 /* Remove the statistics from the active list. */
532 for (ifp = ifstats, pifp = 0; ifp; ifp = ifp->ifs_next) {
533 if (ifp == &up->ifstats) {
535 pifp->ifs_next = ifp->ifs_next;
537 ifstats = ifp->ifs_next;
545 * If this stream is attached to a PPA,
546 * remove it from the PPA's list.
548 if ((as = up->ppa) != 0) {
549 for (; as->next != 0; as = as->next)
550 if (as->next == up) {
559 kstat_delete(up->kstats);
560 mutex_destroy(&up->stats_lock);
566 for (prevp = &minor_devs; *prevp != 0; prevp = &(*prevp)->nextmn) {
572 FREE(up, sizeof(upperstr_t));
579 * A message from on high. We do one of three things:
581 * - put the message on the lower write stream
582 * - queue it for our service routine
589 upperstr_t *us, *ppa, *nps;
599 struct ppp_idle *pip;
607 us = (upperstr_t *) q->q_ptr;
609 DPRINT("pppuwput: q_ptr = 0!\n");
613 DPRINT1("pppuwput/%d: mp = 0!\n", us->mn);
616 if (mp->b_datap == 0) {
617 DPRINT1("pppuwput/%d: mp->b_datap = 0!\n", us->mn);
620 switch (mp->b_datap->db_type) {
624 dlpi_request(q, mp, us);
629 if (us->flags & US_DBGLOG)
630 DPRINT3("ppp/%d: uwput M_DATA len=%d flags=%x\n",
631 us->mn, msgdsize(mp), us->flags);
632 if (us->ppa == 0 || msgdsize(mp) > us->ppa->mtu + PPP_HDRLEN
634 || (us->flags & US_CONTROL) == 0
637 DPRINT1("pppuwput: junk data len=%d\n", msgdsize(mp));
642 /* pass_packet frees the packet on returning 0 */
643 if ((us->flags & US_CONTROL) == 0 && !pass_packet(us, mp, 1))
646 if (!send_data(mp, us) && !putq(q, mp))
651 iop = (struct iocblk *) mp->b_rptr;
653 if (us->flags & US_DBGLOG)
654 DPRINT3("ppp/%d: ioctl %x count=%d\n",
655 us->mn, iop->ioc_cmd, iop->ioc_count);
656 switch (iop->ioc_cmd) {
658 case DLIOCRAW: /* raw M_DATA mode */
659 us->flags |= US_RAWDATA;
662 #endif /* defined(SOL2) */
664 if ((us->flags & US_CONTROL) == 0 || us->lowerq != 0)
666 if (mp->b_cont == 0) {
667 DPRINT1("pppuwput/%d: ioctl I_LINK b_cont = 0!\n", us->mn);
670 lb = (struct linkblk *) mp->b_cont->b_rptr;
673 DPRINT1("pppuwput/%d: ioctl I_LINK l_qbot = 0!\n", us->mn);
678 lq->q_ptr = (caddr_t) q;
679 RD(lq)->q_ptr = (caddr_t) us->q;
683 us->flags &= ~US_LASTMOD;
684 /* Unblock upper streams which now feed this lower stream. */
686 /* Send useful information down to the modules which
687 are now linked below us. */
688 putctl2(lq, M_CTL, PPPCTL_UNIT, us->ppa_id);
689 putctl4(lq, M_CTL, PPPCTL_MRU, us->mru);
690 putctl4(lq, M_CTL, PPPCTL_MTU, us->mtu);
692 /* Lower tty driver's queue hiwat/lowat from default 4096/128
693 to 256/128 since we don't want queueing of data on
694 output to physical device */
697 for (tlq = lq; tlq->q_next != NULL; tlq = tlq->q_next)
699 strqset(tlq, QHIWAT, 0, 256);
700 strqset(tlq, QLOWAT, 0, 128);
706 if (mp->b_cont == 0) {
707 DPRINT1("pppuwput/%d: ioctl I_UNLINK b_cont = 0!\n", us->mn);
710 lb = (struct linkblk *) mp->b_cont->b_rptr;
712 if (us->lowerq != lb->l_qbot) {
713 DPRINT2("ppp unlink: lowerq=%x qbot=%x\n",
714 us->lowerq, lb->l_qbot);
719 qwriter(q, mp, detach_lower, PERIM_OUTER);
725 if (us->flags & US_CONTROL)
727 if ((us->flags & US_PRIV) == 0) {
731 /* Arrange to return an int */
732 if ((mq = mp->b_cont) == 0
733 || mq->b_datap->db_lim - mq->b_rptr < sizeof(int)) {
734 mq = allocb(sizeof(int), BPRI_HI);
744 iop->ioc_count = sizeof(int);
745 mq->b_wptr = mq->b_rptr + sizeof(int);
746 qwriter(q, mp, new_ppa, PERIM_OUTER);
752 /* like dlpi_attach, for programs which can't write to
753 the stream (like pppstats) */
754 if (iop->ioc_count != sizeof(int) || us->ppa != 0)
756 if (mp->b_cont == 0) {
757 DPRINT1("pppuwput/%d: ioctl PPPIO_ATTACH b_cont = 0!\n", us->mn);
760 n = *(int *)mp->b_cont->b_rptr;
761 for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
762 if (ppa->ppa_id == n)
768 qwriter(q, mp, attach_ppa, PERIM_OUTER);
775 /* Attach to a given SAP. */
776 if (iop->ioc_count != sizeof(int) || us->ppa == 0)
778 if (mp->b_cont == 0) {
779 DPRINT1("pppuwput/%d: ioctl PPPIO_BIND b_cont = 0!\n", us->mn);
782 n = *(int *)mp->b_cont->b_rptr;
783 /* n must be a valid PPP network protocol number. */
784 if (n < 0x21 || n > 0x3fff || (n & 0x101) != 1)
786 /* check that no other stream is bound to this sap already. */
787 for (os = us->ppa; os != 0; os = os->next)
799 if (iop->ioc_count != sizeof(int) || (us->flags & US_CONTROL) == 0)
801 if (mp->b_cont == 0) {
802 DPRINT1("pppuwput/%d: ioctl PPPIO_MRU b_cont = 0!\n", us->mn);
805 n = *(int *)mp->b_cont->b_rptr;
806 if (n <= 0 || n > PPP_MAXMRU)
812 putctl4(us->lowerq, M_CTL, PPPCTL_MRU, n);
818 if (iop->ioc_count != sizeof(int) || (us->flags & US_CONTROL) == 0)
820 if (mp->b_cont == 0) {
821 DPRINT1("pppuwput/%d: ioctl PPPIO_MTU b_cont = 0!\n", us->mn);
824 n = *(int *)mp->b_cont->b_rptr;
825 if (n <= 0 || n > PPP_MAXMTU)
829 /* The MTU reported in netstat, not used as IP max packet size! */
830 us->ifstats.ifs_mtu = n;
833 putctl4(us->lowerq, M_CTL, PPPCTL_MTU, n);
839 us->flags |= US_LASTMOD;
844 if (iop->ioc_count != sizeof(int))
846 if (mp->b_cont == 0) {
847 DPRINT1("pppuwput/%d: ioctl PPPIO_DEBUG b_cont = 0!\n", us->mn);
850 n = *(int *)mp->b_cont->b_rptr;
851 if (n == PPPDBG_DUMP + PPPDBG_DRIVER) {
852 qwriter(q, mp, debug_dump, PERIM_OUTER);
855 } else if (n == PPPDBG_LOG + PPPDBG_DRIVER) {
856 DPRINT1("ppp/%d: debug log enabled\n", us->mn);
857 us->flags |= US_DBGLOG;
861 if (us->ppa == 0 || us->ppa->lowerq == 0)
863 putnext(us->ppa->lowerq, mp);
870 if (iop->ioc_count != 2 * sizeof(int))
872 if ((us->flags & US_CONTROL) == 0)
874 if (mp->b_cont == 0) {
875 DPRINT1("pppuwput/%d: ioctl PPPIO_NPMODE b_cont = 0!\n", us->mn);
878 sap = ((int *)mp->b_cont->b_rptr)[0];
879 for (nps = us->next; nps != 0; nps = nps->next) {
880 if (us->flags & US_DBGLOG)
881 DPRINT2("us = 0x%x, us->next->sap = 0x%x\n", nps, nps->sap);
886 if (us->flags & US_DBGLOG)
887 DPRINT2("ppp/%d: no stream for sap %x\n", us->mn, sap);
890 /* XXX possibly should use qwriter here */
891 nps->npmode = (enum NPmode) ((int *)mp->b_cont->b_rptr)[1];
892 if (nps->npmode != NPMODE_QUEUE && (nps->flags & US_BLOCKED) != 0)
899 if ((ppa = us->ppa) == 0)
901 mq = allocb(sizeof(struct ppp_idle), BPRI_HI);
910 pip = (struct ppp_idle *) mq->b_wptr;
911 pip->xmit_idle = time - ppa->last_sent;
912 pip->recv_idle = time - ppa->last_recv;
913 mq->b_wptr += sizeof(struct ppp_idle);
914 iop->ioc_count = sizeof(struct ppp_idle);
920 /* Sent from IP down to us. Attach the ifstats structure. */
921 if (iop->ioc_count != sizeof(struct ifreq) || us->ppa == 0)
923 ifr = (struct ifreq *)mp->b_cont->b_rptr;
924 /* Find the unit number in the interface name. */
925 for (i = 0; i < IFNAMSIZ; i++) {
926 if (ifr->ifr_name[i] == 0 ||
927 (ifr->ifr_name[i] >= '0' &&
928 ifr->ifr_name[i] <= '9'))
931 us->ifname[i] = ifr->ifr_name[i];
935 /* Convert the unit number to binary. */
936 for (n = 0; i < IFNAMSIZ; i++) {
937 if (ifr->ifr_name[i] == 0) {
941 n = n * 10 + ifr->ifr_name[i] - '0';
945 /* Verify the ppa. */
946 if (us->ppa->ppa_id != n)
950 /* Set up the netstat block. */
951 strncpy (ppa->ifname, us->ifname, IFNAMSIZ);
953 ppa->ifstats.ifs_name = ppa->ifname;
954 ppa->ifstats.ifs_unit = n;
955 ppa->ifstats.ifs_active = us->state != DL_UNBOUND;
956 ppa->ifstats.ifs_mtu = ppa->mtu;
958 /* Link in statistics used by netstat. */
959 ppa->ifstats.ifs_next = ifstats;
960 ifstats = &ppa->ifstats;
967 if (!(us->flags & US_CONTROL)) {
973 ((struct iocblk_in *)iop)->ioc_ifflags = us->ifflags;
978 if (!(us->flags & US_CONTROL)) {
984 us->ifflags = ((struct iocblk_in *)iop)->ioc_ifflags;
989 if (!(us->flags & US_CONTROL)) {
995 us->ifflags |= IFF_RUNNING;
996 ((struct iocblk_in *)iop)->ioc_ifflags |= IFF_RUNNING;
1002 * Vanilla SVR4 systems don't handle SIOCSIFMTU, rather
1003 * they take the MTU from the DL_INFO_ACK we sent in response
1004 * to their DL_INFO_REQ. Fortunately, they will update the
1005 * MTU if we send an unsolicited DL_INFO_ACK up.
1007 if ((mq = allocb(sizeof(dl_info_req_t), BPRI_HI)) == 0)
1008 break; /* should do bufcall */
1009 ((union DL_primitives *)mq->b_rptr)->dl_primitive = DL_INFO_REQ;
1010 mq->b_wptr = mq->b_rptr + sizeof(dl_info_req_t);
1011 dlpi_request(q, mq, us);
1012 /* mp is now gone */
1016 case SIOCGIFNETMASK:
1017 case SIOCSIFNETMASK:
1019 case SIOCGIFDSTADDR:
1020 case SIOCSIFDSTADDR:
1024 #endif /* LACHTCP */
1027 if (us->ppa == 0 || us->ppa->lowerq == 0)
1029 us->ioc_id = iop->ioc_id;
1031 switch (iop->ioc_cmd) {
1033 case PPPIO_GETCSTAT:
1034 if (us->flags & US_LASTMOD) {
1038 putnext(us->ppa->lowerq, mp);
1041 if (us->flags & US_PRIV)
1042 putnext(us->ppa->lowerq, mp);
1044 DPRINT1("ppp ioctl %x rejected\n", iop->ioc_cmd);
1053 iop->ioc_error = error;
1054 mp->b_datap->db_type = M_IOCNAK;
1056 } else if (error == 0) {
1057 mp->b_datap->db_type = M_IOCACK;
1063 if (us->flags & US_DBGLOG)
1064 DPRINT2("ppp/%d: flush %x\n", us->mn, *mp->b_rptr);
1065 if (*mp->b_rptr & FLUSHW)
1066 flushq(q, FLUSHDATA);
1067 if (*mp->b_rptr & FLUSHR) {
1068 *mp->b_rptr &= ~FLUSHW;
1083 dlpi_request(q, mp, us)
1088 union DL_primitives *d = (union DL_primitives *) mp->b_rptr;
1089 int size = mp->b_wptr - mp->b_rptr;
1091 upperstr_t *ppa, *os;
1093 dl_info_ack_t *info;
1094 dl_bind_ack_t *ackp;
1095 #if DL_CURRENT_VERSION >= 2
1096 dl_phys_addr_ack_t *paddrack;
1097 static struct ether_addr eaddr = {0};
1100 if (us->flags & US_DBGLOG)
1101 DPRINT3("ppp/%d: dlpi prim %x len=%d\n", us->mn,
1102 d->dl_primitive, size);
1103 switch (d->dl_primitive) {
1105 if (size < sizeof(dl_info_req_t))
1107 if ((reply = allocb(sizeof(dl_info_ack_t), BPRI_HI)) == 0)
1108 break; /* should do bufcall */
1109 reply->b_datap->db_type = M_PCPROTO;
1110 info = (dl_info_ack_t *) reply->b_wptr;
1111 reply->b_wptr += sizeof(dl_info_ack_t);
1112 bzero((caddr_t) info, sizeof(dl_info_ack_t));
1113 info->dl_primitive = DL_INFO_ACK;
1114 info->dl_max_sdu = us->ppa? us->ppa->mtu: PPP_MAXMTU;
1115 info->dl_min_sdu = 1;
1116 info->dl_addr_length = sizeof(uint);
1117 info->dl_mac_type = DL_ETHER; /* a bigger lie */
1118 info->dl_current_state = us->state;
1119 info->dl_service_mode = DL_CLDLS;
1120 info->dl_provider_style = DL_STYLE2;
1121 #if DL_CURRENT_VERSION >= 2
1122 info->dl_sap_length = sizeof(uint);
1123 info->dl_version = DL_CURRENT_VERSION;
1129 if (size < sizeof(dl_attach_req_t))
1131 if (us->state != DL_UNATTACHED || us->ppa != 0) {
1132 dlpi_error(q, us, DL_ATTACH_REQ, DL_OUTSTATE, 0);
1135 for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
1136 if (ppa->ppa_id == d->attach_req.dl_ppa)
1139 dlpi_error(q, us, DL_ATTACH_REQ, DL_BADPPA, 0);
1143 qwriter(q, mp, attach_ppa, PERIM_OUTER);
1147 if (size < sizeof(dl_detach_req_t))
1149 if (us->state != DL_UNBOUND || us->ppa == 0) {
1150 dlpi_error(q, us, DL_DETACH_REQ, DL_OUTSTATE, 0);
1153 qwriter(q, mp, detach_ppa, PERIM_OUTER);
1157 if (size < sizeof(dl_bind_req_t))
1159 if (us->state != DL_UNBOUND || us->ppa == 0) {
1160 dlpi_error(q, us, DL_BIND_REQ, DL_OUTSTATE, 0);
1164 /* apparently this test fails (unnecessarily?) on some systems */
1165 if (d->bind_req.dl_service_mode != DL_CLDLS) {
1166 dlpi_error(q, us, DL_BIND_REQ, DL_UNSUPPORTED, 0);
1171 /* saps must be valid PPP network protocol numbers,
1172 except that we accept ETHERTYPE_IP in place of PPP_IP. */
1173 sap = d->bind_req.dl_sap;
1177 if (us->flags & US_DBGLOG)
1178 DPRINT2("DL_BIND_REQ: ip gives sap = 0x%x, us = 0x%x", sap, us);
1180 if (sap == ETHERTYPE_IP) /* normal IFF_IPV4 */
1182 else if (sap == ETHERTYPE_IPV6) /* when IFF_IPV6 is set */
1184 else if (sap == ETHERTYPE_ALLSAP) /* snoop gives sap of 0 */
1187 DPRINT2("DL_BIND_REQ: unrecognized sap = 0x%x, us = 0x%x", sap, us);
1188 dlpi_error(q, us, DL_BIND_REQ, DL_BADADDR, 0);
1192 if (sap == ETHERTYPE_IP)
1194 if (sap < 0x21 || sap > 0x3fff || (sap & 0x101) != 1) {
1195 dlpi_error(q, us, DL_BIND_REQ, DL_BADADDR, 0);
1198 #endif /* defined(SOL2) */
1200 /* check that no other stream is bound to this sap already. */
1201 for (os = us->ppa; os != 0; os = os->next)
1205 dlpi_error(q, us, DL_BIND_REQ, DL_NOADDR, 0);
1210 us->state = DL_IDLE;
1212 if ((reply = allocb(sizeof(dl_bind_ack_t) + sizeof(uint),
1214 break; /* should do bufcall */
1215 ackp = (dl_bind_ack_t *) reply->b_wptr;
1216 reply->b_wptr += sizeof(dl_bind_ack_t) + sizeof(uint);
1217 reply->b_datap->db_type = M_PCPROTO;
1218 bzero((caddr_t) ackp, sizeof(dl_bind_ack_t));
1219 ackp->dl_primitive = DL_BIND_ACK;
1221 ackp->dl_addr_length = sizeof(uint);
1222 ackp->dl_addr_offset = sizeof(dl_bind_ack_t);
1223 *(uint *)(ackp+1) = sap;
1228 if (size < sizeof(dl_unbind_req_t))
1230 if (us->state != DL_IDLE) {
1231 dlpi_error(q, us, DL_UNBIND_REQ, DL_OUTSTATE, 0);
1235 us->state = DL_UNBOUND;
1237 us->ppa->ifstats.ifs_active = 0;
1239 dlpi_ok(q, DL_UNBIND_REQ);
1242 case DL_UNITDATA_REQ:
1243 if (size < sizeof(dl_unitdata_req_t))
1245 if (us->state != DL_IDLE) {
1246 dlpi_error(q, us, DL_UNITDATA_REQ, DL_OUTSTATE, 0);
1249 if ((ppa = us->ppa) == 0) {
1250 cmn_err(CE_CONT, "ppp: in state dl_idle but ppa == 0?\n");
1253 len = mp->b_cont == 0? 0: msgdsize(mp->b_cont);
1254 if (len > ppa->mtu) {
1255 DPRINT2("dlpi data too large (%d > %d)\n", len, ppa->mtu);
1261 * Should there be any promiscuous stream(s), send the data
1262 * up for each promiscuous stream that we recognize.
1265 promisc_sendup(ppa, mp->b_cont, us->sap, 0);
1266 #endif /* defined(SOL2) */
1270 /* Extract s_port & d_port from IP-packet, the code is a bit
1271 dirty here, but so am I, too... */
1272 if (mp->b_datap->db_type == M_PROTO && us->sap == PPP_IP
1273 && mp->b_cont != 0) {
1277 u_char band_unset, cur_band, syn;
1278 u_short s_port, d_port;
1280 bb = mp->b_cont->b_rptr; /* bb points to IP-header*/
1281 len = mp->b_cont->b_wptr - mp->b_cont->b_rptr;
1283 s_port = IPPORT_DEFAULT;
1284 d_port = IPPORT_DEFAULT;
1285 if (len >= 20) { /* 20 = minimum length of IP header */
1286 iphlen = (bb[0] & 0x0f) * 4;
1291 if (len >= 20) { /* min length of TCP header */
1292 s_port = (tlh[0] << 8) + tlh[1];
1293 d_port = (tlh[2] << 8) + tlh[3];
1294 syn = tlh[13] & 0x02;
1298 if (len >= 8) { /* min length of UDP header */
1299 s_port = (tlh[0] << 8) + tlh[1];
1300 d_port = (tlh[2] << 8) + tlh[3];
1307 * Now calculate b_band for this packet from the
1308 * port-priority table.
1311 cur_band = max_band;
1314 while (*ptr && band_unset)
1315 if (s_port == *ptr || d_port == *ptr++) {
1316 mp->b_band = cur_band;
1324 mp->b_band = def_band;
1325 /* It may be usable to urge SYN packets a bit */
1330 /* this assumes PPP_HDRLEN <= sizeof(dl_unitdata_req_t) */
1331 if (mp->b_datap->db_ref > 1) {
1332 np = allocb(PPP_HDRLEN, BPRI_HI);
1335 np->b_cont = mp->b_cont;
1340 mp->b_datap->db_type = M_DATA;
1341 /* XXX should use dl_dest_addr_offset/length here,
1342 but we would have to translate ETHERTYPE_IP -> PPP_IP */
1343 mp->b_wptr = mp->b_rptr + PPP_HDRLEN;
1344 mp->b_rptr[0] = PPP_ALLSTATIONS;
1345 mp->b_rptr[1] = PPP_UI;
1346 mp->b_rptr[2] = us->sap >> 8;
1347 mp->b_rptr[3] = us->sap;
1348 /* pass_packet frees the packet on returning 0 */
1349 if (pass_packet(us, mp, 1)) {
1350 if (!send_data(mp, us) && !putq(q, mp))
1355 #if DL_CURRENT_VERSION >= 2
1356 case DL_PHYS_ADDR_REQ:
1357 if (size < sizeof(dl_phys_addr_req_t))
1361 * Don't check state because ifconfig sends this one down too
1364 if ((reply = allocb(sizeof(dl_phys_addr_ack_t)+ETHERADDRL,
1366 break; /* should do bufcall */
1367 reply->b_datap->db_type = M_PCPROTO;
1368 paddrack = (dl_phys_addr_ack_t *) reply->b_wptr;
1369 reply->b_wptr += sizeof(dl_phys_addr_ack_t);
1370 bzero((caddr_t) paddrack, sizeof(dl_phys_addr_ack_t)+ETHERADDRL);
1371 paddrack->dl_primitive = DL_PHYS_ADDR_ACK;
1372 paddrack->dl_addr_length = ETHERADDRL;
1373 paddrack->dl_addr_offset = sizeof(dl_phys_addr_ack_t);
1374 bcopy(&eaddr, reply->b_wptr, ETHERADDRL);
1375 reply->b_wptr += ETHERADDRL;
1380 case DL_PROMISCON_REQ:
1381 if (size < sizeof(dl_promiscon_req_t))
1383 us->flags |= US_PROMISC;
1384 dlpi_ok(q, DL_PROMISCON_REQ);
1387 case DL_PROMISCOFF_REQ:
1388 if (size < sizeof(dl_promiscoff_req_t))
1390 us->flags &= ~US_PROMISC;
1391 dlpi_ok(q, DL_PROMISCOFF_REQ);
1394 case DL_PROMISCON_REQ: /* fall thru */
1395 case DL_PROMISCOFF_REQ: /* fall thru */
1396 #endif /* defined(SOL2) */
1397 #endif /* DL_CURRENT_VERSION >= 2 */
1399 #if DL_CURRENT_VERSION >= 2
1400 case DL_SET_PHYS_ADDR_REQ:
1401 case DL_SUBS_BIND_REQ:
1402 case DL_SUBS_UNBIND_REQ:
1403 case DL_ENABMULTI_REQ:
1404 case DL_DISABMULTI_REQ:
1407 case DL_REPLY_UPDATE_REQ:
1409 case DL_DATA_ACK_REQ:
1411 case DL_CONNECT_REQ:
1413 dlpi_error(q, us, d->dl_primitive, DL_NOTSUPPORTED, 0);
1416 case DL_CONNECT_RES:
1417 case DL_DISCONNECT_REQ:
1420 dlpi_error(q, us, d->dl_primitive, DL_OUTSTATE, 0);
1424 dlpi_error(q, us, d->dl_primitive, DL_BADQOSTYPE, 0);
1427 #if DL_CURRENT_VERSION >= 2
1434 if (us->flags & US_DBGLOG)
1435 DPRINT1("ppp: unknown dlpi prim 0x%x\n", d->dl_primitive);
1438 dlpi_error(q, us, d->dl_primitive, DL_BADPRIM, 0);
1445 dlpi_error(q, us, prim, err, uerr)
1448 int prim, err, uerr;
1451 dl_error_ack_t *errp;
1453 if (us->flags & US_DBGLOG)
1454 DPRINT3("ppp/%d: dlpi error, prim=%x, err=%x\n", us->mn, prim, err);
1455 reply = allocb(sizeof(dl_error_ack_t), BPRI_HI);
1457 return; /* XXX should do bufcall */
1458 reply->b_datap->db_type = M_PCPROTO;
1459 errp = (dl_error_ack_t *) reply->b_wptr;
1460 reply->b_wptr += sizeof(dl_error_ack_t);
1461 errp->dl_primitive = DL_ERROR_ACK;
1462 errp->dl_error_primitive = prim;
1463 errp->dl_errno = err;
1464 errp->dl_unix_errno = uerr;
1476 reply = allocb(sizeof(dl_ok_ack_t), BPRI_HI);
1478 return; /* XXX should do bufcall */
1479 reply->b_datap->db_type = M_PCPROTO;
1480 okp = (dl_ok_ack_t *) reply->b_wptr;
1481 reply->b_wptr += sizeof(dl_ok_ack_t);
1482 okp->dl_primitive = DL_OK_ACK;
1483 okp->dl_correct_primitive = prim;
1486 #endif /* NO_DLPI */
1489 * If return value is 0, then the packet has already been freed.
1492 pass_packet(us, mp, outbound)
1500 if ((ppa = us->ppa) == 0) {
1505 #ifdef FILTER_PACKETS
1506 pass = ip_hard_filter(us, mp, outbound);
1509 * Here is where we might, in future, decide whether to pass
1510 * or drop the packet, and whether it counts as link activity.
1513 #endif /* FILTER_PACKETS */
1516 /* pass only if link already up, and don't update time */
1517 if (ppa->lowerq == 0) {
1524 ppa->last_sent = time;
1526 ppa->last_recv = time;
1533 * We have some data to send down to the lower stream (or up the
1534 * control stream, if we don't have a lower stream attached).
1535 * Returns 1 if the message was dealt with, 0 if it wasn't able
1536 * to be sent on and should therefore be queued up.
1545 if ((us->flags & US_BLOCKED) || us->npmode == NPMODE_QUEUE)
1548 if (ppa == 0 || us->npmode == NPMODE_DROP || us->npmode == NPMODE_ERROR) {
1549 if (us->flags & US_DBGLOG)
1550 DPRINT2("ppp/%d: dropping pkt (npmode=%d)\n", us->mn, us->npmode);
1554 if (ppa->lowerq == 0) {
1555 /* try to send it up the control stream */
1556 if (bcanputnext(ppa->q, mp->b_band)) {
1558 * The message seems to get corrupted for some reason if
1559 * we just send the message up as it is, so we send a copy.
1561 mblk_t *np = copymsg(mp);
1564 putnext(ppa->q, np);
1568 if (bcanputnext(ppa->lowerq, mp->b_band)) {
1569 MT_ENTER(&ppa->stats_lock);
1570 ppa->stats.ppp_opackets++;
1571 ppa->stats.ppp_obytes += msgdsize(mp);
1572 #ifdef INCR_OPACKETS
1575 MT_EXIT(&ppa->stats_lock);
1577 * The lower queue is only ever detached while holding an
1578 * exclusive lock on the whole driver. So we can be confident
1579 * that the lower queue is still there.
1581 putnext(ppa->lowerq, mp);
1585 us->flags |= US_BLOCKED;
1590 * Allocate a new PPA id and link this stream into the list of PPAs.
1591 * This procedure is called with an exclusive lock on all queues in
1599 upperstr_t *us, *up, **usp;
1602 us = (upperstr_t *) q->q_ptr;
1604 DPRINT("new_ppa: q_ptr = 0!\n");
1610 while ((up = *usp) != 0 && ppa_id == up->ppa_id) {
1614 us->ppa_id = ppa_id;
1619 us->flags |= US_CONTROL;
1620 us->npmode = NPMODE_PASS;
1627 * Create a kstats record for our statistics, so netstat -i works.
1629 if (us->kstats == 0) {
1632 sprintf(unit, "ppp%d", us->ppa->ppa_id);
1633 us->kstats = kstat_create("ppp", us->ppa->ppa_id, unit,
1634 "net", KSTAT_TYPE_NAMED, 4, 0);
1635 if (us->kstats != 0) {
1636 kstat_named_t *kn = KSTAT_NAMED_PTR(us->kstats);
1638 strcpy(kn[0].name, "ipackets");
1639 kn[0].data_type = KSTAT_DATA_ULONG;
1640 strcpy(kn[1].name, "ierrors");
1641 kn[1].data_type = KSTAT_DATA_ULONG;
1642 strcpy(kn[2].name, "opackets");
1643 kn[2].data_type = KSTAT_DATA_ULONG;
1644 strcpy(kn[3].name, "oerrors");
1645 kn[3].data_type = KSTAT_DATA_ULONG;
1646 kstat_install(us->kstats);
1651 *(int *)mp->b_cont->b_rptr = ppa_id;
1652 mp->b_datap->db_type = M_IOCACK;
1663 us = (upperstr_t *) q->q_ptr;
1665 DPRINT("attach_ppa: q_ptr = 0!\n");
1670 us->state = DL_UNBOUND;
1672 for (t = us->ppa; t->next != 0; t = t->next)
1676 if (mp->b_datap->db_type == M_IOCTL) {
1677 mp->b_datap->db_type = M_IOCACK;
1681 dlpi_ok(q, DL_ATTACH_REQ);
1695 us = (upperstr_t *) q->q_ptr;
1697 DPRINT("detach_ppa: q_ptr = 0!\n");
1701 for (t = us->ppa; t->next != 0; t = t->next)
1702 if (t->next == us) {
1708 us->state = DL_UNATTACHED;
1709 dlpi_ok(q, DL_DETACH_REQ);
1715 * We call this with qwriter in order to give the upper queue procedures
1716 * the guarantee that the lower queue is not going to go away while
1717 * they are executing.
1726 us = (upperstr_t *) q->q_ptr;
1728 DPRINT("detach_lower: q_ptr = 0!\n");
1733 us->lowerq->q_ptr = 0;
1734 RD(us->lowerq)->q_ptr = 0;
1738 /* Unblock streams which now feed back up the control stream. */
1741 mp->b_datap->db_type = M_IOCACK;
1749 upperstr_t *us, *as;
1752 us = (upperstr_t *) q->q_ptr;
1754 DPRINT("pppuwsrv: q_ptr = 0!\n");
1759 * If this is a control stream, then this service procedure
1760 * probably got enabled because of flow control in the lower
1761 * stream being enabled (or because of the lower stream going
1762 * away). Therefore we enable the service procedure of all
1763 * attached upper streams.
1765 if (us->flags & US_CONTROL) {
1766 for (as = us->next; as != 0; as = as->next)
1770 /* Try to send on any data queued here. */
1771 us->flags &= ~US_BLOCKED;
1772 while ((mp = getq(q)) != 0) {
1773 if (!send_data(mp, us)) {
1782 /* should never get called... */
1799 * Flow control has back-enabled this stream:
1800 * enable the upper write service procedure for
1801 * the upper control stream for this lower stream.
1804 uq = (queue_t *) q->q_ptr;
1812 * This should only get called for control streams.
1819 upperstr_t *ppa, *us;
1823 ppa = (upperstr_t *) q->q_ptr;
1825 DPRINT("pppurput: q_ptr = 0!\n");
1829 switch (mp->b_datap->db_type) {
1831 MT_ENTER(&ppa->stats_lock);
1832 switch (*mp->b_rptr) {
1837 ppa->stats.ppp_ierrors++;
1843 ppa->stats.ppp_oerrors++;
1846 MT_EXIT(&ppa->stats_lock);
1853 * Attempt to match up the response with the stream
1854 * that the request came from.
1856 iop = (struct iocblk *) mp->b_rptr;
1857 for (us = ppa; us != 0; us = us->next)
1858 if (us->ioc_id == iop->ioc_id)
1868 * The serial device has hung up. We don't want to send
1869 * the M_HANGUP message up to pppd because that will stop
1870 * us from using the control stream any more. Instead we
1871 * send a zero-length message as an end-of-file indication.
1874 mp = allocb(1, BPRI_HI);
1876 DPRINT1("ppp/%d: couldn't allocate eof message!\n", ppa->mn);
1879 putnext(ppa->q, mp);
1884 if (mp->b_wptr - mp->b_rptr < PPP_HDRLEN) {
1885 PULLUP(mp, PPP_HDRLEN);
1887 DPRINT1("ppp_urput: msgpullup failed (len=%d)\n", len);
1891 MT_ENTER(&ppa->stats_lock);
1892 ppa->stats.ppp_ipackets++;
1893 ppa->stats.ppp_ibytes += len;
1894 #ifdef INCR_IPACKETS
1897 MT_EXIT(&ppa->stats_lock);
1899 proto = PPP_PROTOCOL(mp->b_rptr);
1903 * Should there be any promiscuous stream(s), send the data
1904 * up for each promiscuous stream that we recognize.
1906 promisc_sendup(ppa, mp, proto, 1);
1907 #endif /* defined(SOL2) */
1909 if (proto < 0x8000 && (us = find_dest(ppa, proto)) != 0) {
1911 * A data packet for some network protocol.
1912 * Queue it on the upper stream for that protocol.
1913 * XXX could we just putnext it? (would require thought)
1914 * The rblocked flag is there to ensure that we keep
1915 * messages in order for each network protocol.
1917 /* pass_packet frees the packet on returning 0 */
1918 if (!pass_packet(us, mp, 0))
1920 if (!us->rblocked && !canput(us->q))
1922 if (!putq(us->rblocked ? q : us->q, mp))
1931 * A control frame, a frame for an unknown protocol,
1932 * or some other message type.
1933 * Send it up to pppd via the control stream.
1935 if (queclass(mp) == QPCTL || canputnext(ppa->q))
1936 putnext(ppa->q, mp);
1937 else if (!putq(q, mp))
1949 upperstr_t *us, *as;
1952 dl_unitdata_ind_t *ud;
1956 us = (upperstr_t *) q->q_ptr;
1958 DPRINT("pppursrv: q_ptr = 0!\n");
1962 if (us->flags & US_CONTROL) {
1965 * If there is no lower queue attached, run the write service
1966 * routines of other upper streams attached to this PPA.
1968 if (us->lowerq == 0) {
1971 if (as->flags & US_BLOCKED)
1978 * Messages get queued on this stream's read queue if they
1979 * can't be queued on the read queue of the attached stream
1980 * that they are destined for. This is for flow control -
1981 * when this queue fills up, the lower read put procedure will
1982 * queue messages there and the flow control will propagate
1985 while ((mp = getq(q)) != 0) {
1986 proto = PPP_PROTOCOL(mp->b_rptr);
1987 if (proto < 0x8000 && (as = find_dest(us, proto)) != 0) {
1990 if (!putq(as->q, mp))
2001 /* can now put stuff directly on network protocol streams again */
2002 for (as = us->next; as != 0; as = as->next)
2007 * If this stream has a lower stream attached,
2008 * enable the read queue's service routine.
2009 * XXX we should really only do this if the queue length
2010 * has dropped below the low-water mark.
2012 if (us->lowerq != 0)
2013 qenable(RD(us->lowerq));
2017 * A network protocol stream. Put a DLPI header on each
2018 * packet and send it on.
2019 * (Actually, it seems that the IP module will happily
2020 * accept M_DATA messages without the DL_UNITDATA_IND header.)
2022 while ((mp = getq(q)) != 0) {
2023 if (!canputnext(q)) {
2028 proto = PPP_PROTOCOL(mp->b_rptr);
2029 mp->b_rptr += PPP_HDRLEN;
2030 hdr = allocb(sizeof(dl_unitdata_ind_t) + 2 * sizeof(uint),
2033 /* XXX should put it back and use bufcall */
2037 hdr->b_datap->db_type = M_PROTO;
2038 ud = (dl_unitdata_ind_t *) hdr->b_wptr;
2039 hdr->b_wptr += sizeof(dl_unitdata_ind_t) + 2 * sizeof(uint);
2041 ud->dl_primitive = DL_UNITDATA_IND;
2042 ud->dl_dest_addr_length = sizeof(uint);
2043 ud->dl_dest_addr_offset = sizeof(dl_unitdata_ind_t);
2044 ud->dl_src_addr_length = sizeof(uint);
2045 ud->dl_src_addr_offset = ud->dl_dest_addr_offset + sizeof(uint);
2046 #if DL_CURRENT_VERSION >= 2
2047 ud->dl_group_address = 0;
2049 /* Send the DLPI client the data with the SAP they requested,
2050 (e.g. ETHERTYPE_IP) rather than the PPP protocol number
2052 ((uint *)(ud + 1))[0] = us->req_sap; /* dest SAP */
2053 ((uint *)(ud + 1))[1] = us->req_sap; /* src SAP */
2057 #endif /* NO_DLPI */
2060 * Now that we have consumed some packets from this queue,
2061 * enable the control stream's read service routine so that we
2062 * can process any packets for us that might have got queued
2063 * there for flow control reasons.
2066 qenable(us->ppa->q);
2073 find_dest(ppa, proto)
2079 for (us = ppa->next; us != 0; us = us->next)
2080 if (proto == us->sap)
2087 * Test upstream promiscuous conditions. As of now, only pass IPv4 and
2088 * Ipv6 packets upstream (let PPP packets be decoded elsewhere).
2091 find_promisc(us, proto)
2096 if ((proto != PPP_IP) && (proto != PPP_IPV6))
2097 return (upperstr_t *)0;
2099 for ( ; us; us = us->next) {
2100 if ((us->flags & US_PROMISC) && (us->state == DL_IDLE))
2104 return (upperstr_t *)0;
2108 * Prepend an empty Ethernet header to msg for snoop, et al.
2111 prepend_ether(us, mp, proto)
2119 if ((eh = allocb(sizeof(struct ether_header), BPRI_HI)) == 0) {
2124 if (proto == PPP_IP)
2125 type = ETHERTYPE_IP;
2126 else if (proto == PPP_IPV6)
2127 type = ETHERTYPE_IPV6;
2129 type = proto; /* What else? Let decoder decide */
2131 eh->b_wptr += sizeof(struct ether_header);
2132 bzero((caddr_t)eh->b_rptr, sizeof(struct ether_header));
2133 ((struct ether_header *)eh->b_rptr)->ether_type = htons((short)type);
2139 * Prepend DL_UNITDATA_IND mblk to msg
2142 prepend_udind(us, mp, proto)
2147 dl_unitdata_ind_t *dlu;
2151 size = sizeof(dl_unitdata_ind_t);
2152 if ((dh = allocb(size, BPRI_MED)) == 0) {
2157 dh->b_datap->db_type = M_PROTO;
2158 dh->b_wptr = dh->b_datap->db_lim;
2159 dh->b_rptr = dh->b_wptr - size;
2161 dlu = (dl_unitdata_ind_t *)dh->b_rptr;
2162 dlu->dl_primitive = DL_UNITDATA_IND;
2163 dlu->dl_dest_addr_length = 0;
2164 dlu->dl_dest_addr_offset = sizeof(dl_unitdata_ind_t);
2165 dlu->dl_src_addr_length = 0;
2166 dlu->dl_src_addr_offset = sizeof(dl_unitdata_ind_t);
2167 dlu->dl_group_address = 0;
2174 * For any recognized promiscuous streams, send data upstream
2177 promisc_sendup(ppa, mp, proto, skip)
2182 mblk_t *dup_mp, *dup_dup_mp;
2183 upperstr_t *prus, *nprus;
2185 if ((prus = find_promisc(ppa, proto)) != 0) {
2186 if (dup_mp = dupmsg(mp)) {
2189 dup_mp->b_rptr += PPP_HDRLEN;
2191 for ( ; nprus = find_promisc(prus->next, proto);
2194 if (dup_dup_mp = dupmsg(dup_mp)) {
2195 if (canputnext(prus->q)) {
2196 if (prus->flags & US_RAWDATA) {
2197 dup_dup_mp = prepend_ether(prus, dup_dup_mp, proto);
2199 dup_dup_mp = prepend_udind(prus, dup_dup_mp, proto);
2201 if (dup_dup_mp == 0)
2203 putnext(prus->q, dup_dup_mp);
2205 DPRINT("ppp_urput: data to promisc q dropped\n");
2206 freemsg(dup_dup_mp);
2211 if (canputnext(prus->q)) {
2212 if (prus->flags & US_RAWDATA) {
2213 dup_mp = prepend_ether(prus, dup_mp, proto);
2215 dup_mp = prepend_udind(prus, dup_mp, proto);
2218 putnext(prus->q, dup_mp);
2220 DPRINT("ppp_urput: data to promisc q dropped\n");
2226 #endif /* defined(SOL2) */
2229 * We simply put the message on to the associated upper control stream
2230 * (either here or in ppplrsrv). That way we enter the perimeters
2231 * before looking through the list of attached streams to decide which
2232 * stream it should go up.
2242 switch (mp->b_datap->db_type) {
2244 iop = (struct iocblk *) mp->b_rptr;
2245 iop->ioc_error = EINVAL;
2246 mp->b_datap->db_type = M_IOCNAK;
2250 if (*mp->b_rptr & FLUSHR)
2251 flushq(q, FLUSHDATA);
2252 if (*mp->b_rptr & FLUSHW) {
2253 *mp->b_rptr &= ~FLUSHR;
2261 * If we can't get the lower lock straight away, queue this one
2262 * rather than blocking, to avoid the possibility of deadlock.
2264 if (!TRYLOCK_LOWER_R) {
2271 * Check that we're still connected to the driver.
2273 uq = (queue_t *) q->q_ptr;
2276 DPRINT1("ppplrput: q = %x, uq = 0??\n", q);
2282 * Try to forward the message to the put routine for the upper
2283 * control stream for this lower stream.
2284 * If there are already messages queued here, queue this one so
2285 * they don't get out of order.
2287 if (queclass(mp) == QPCTL || (qsize(q) == 0 && canput(uq)))
2289 else if (!putq(q, mp))
2304 * Packets get queued here for flow control reasons
2305 * or if the lrput routine couldn't get the lower lock
2309 uq = (queue_t *) q->q_ptr;
2312 flushq(q, FLUSHALL);
2313 DPRINT1("ppplrsrv: q = %x, uq = 0??\n", q);
2316 while ((mp = getq(q)) != 0) {
2317 if (queclass(mp) == QPCTL || canput(uq))
2329 putctl2(q, type, code, val)
2331 int type, code, val;
2335 mp = allocb(2, BPRI_HI);
2338 mp->b_datap->db_type = type;
2339 mp->b_wptr[0] = code;
2340 mp->b_wptr[1] = val;
2347 putctl4(q, type, code, val)
2349 int type, code, val;
2353 mp = allocb(4, BPRI_HI);
2356 mp->b_datap->db_type = type;
2357 mp->b_wptr[0] = code;
2358 ((short *)mp->b_wptr)[1] = val;
2372 DPRINT("ppp upper streams:\n");
2373 for (us = minor_devs; us != 0; us = us->nextmn) {
2375 DPRINT3(" %d: q=%x rlev=%d",
2376 us->mn, uq, (uq? qsize(uq): 0));
2377 DPRINT3(" wlev=%d flags=0x%b", (uq? qsize(WR(uq)): 0),
2378 us->flags, "\020\1priv\2control\3blocked\4last");
2379 DPRINT3(" state=%x sap=%x req_sap=%x", us->state, us->sap,
2384 DPRINT1(" ppa=%d\n", us->ppa->ppa_id);
2385 if (us->flags & US_CONTROL) {
2387 DPRINT3(" control for %d lq=%x rlev=%d",
2388 us->ppa_id, lq, (lq? qsize(RD(lq)): 0));
2389 DPRINT3(" wlev=%d mru=%d mtu=%d\n",
2390 (lq? qsize(lq): 0), us->mru, us->mtu);
2393 mp->b_datap->db_type = M_IOCACK;
2397 #ifdef FILTER_PACKETS
2398 #include <netinet/in_systm.h>
2399 #include <netinet/ip.h>
2400 #include <netinet/udp.h>
2401 #include <netinet/tcp.h>
2403 #define MAX_IPHDR 128 /* max TCP/IP header size */
2406 /* The following table contains a hard-coded list of protocol/port pairs.
2407 * Any matching packets are either discarded unconditionally, or,
2408 * if ok_if_link_up is non-zero when a connection does not currently exist
2409 * (i.e., they go through if the connection is present, but never initiate
2411 * This idea came from a post by dm@garage.uun.org (David Mazieres)
2413 static struct pktfilt_tab {
2416 u_short ok_if_link_up;
2418 { IPPROTO_UDP, 520, 1 }, /* RIP, ok to pass if link is up */
2419 { IPPROTO_UDP, 123, 1 }, /* NTP, don't keep up the link for it */
2420 { -1, 0, 0 } /* terminator entry has port == -1 */
2425 * Packet has already been freed if return value is 0.
2428 ip_hard_filter(us, mp, outbound)
2434 struct pktfilt_tab *pft;
2440 /* Note, the PPP header has already been pulled up in all cases */
2441 proto = PPP_PROTOCOL(mp->b_rptr);
2442 if (us->flags & US_DBGLOG)
2443 DPRINT3("ppp/%d: filter, proto=0x%x, out=%d\n", us->mn, proto, outbound);
2448 if ((mp->b_wptr - mp->b_rptr) == PPP_HDRLEN && mp->b_cont != 0) {
2449 temp_mp = mp->b_cont;
2450 len = msgdsize(temp_mp);
2451 hlen = (len < MAX_IPHDR) ? len : MAX_IPHDR;
2452 PULLUP(temp_mp, hlen);
2454 DPRINT2("ppp/%d: filter, pullup next failed, len=%d\n",
2456 mp->b_cont = 0; /* PULLUP() freed the rest */
2460 ip = (struct ip *)mp->b_cont->b_rptr;
2464 hlen = (len < (PPP_HDRLEN+MAX_IPHDR)) ? len : (PPP_HDRLEN+MAX_IPHDR);
2467 DPRINT2("ppp/%d: filter, pullup failed, len=%d\n",
2471 ip = (struct ip *)(mp->b_rptr + PPP_HDRLEN);
2474 /* For IP traffic, certain packets (e.g., RIP) may be either
2475 * 1. ignored - dropped completely
2476 * 2. will not initiate a connection, but
2477 * will be passed if a connection is currently up.
2479 for (pft=pktfilt_tab; pft->proto != -1; pft++) {
2480 if (ip->ip_p == pft->proto) {
2481 switch(pft->proto) {
2483 if (((struct udphdr *) &((int *)ip)[ip->ip_hl])->uh_dport
2484 == htons(pft->port)) goto endfor;
2487 if (((struct tcphdr *) &((int *)ip)[ip->ip_hl])->th_dport
2488 == htons(pft->port)) goto endfor;
2494 if (pft->proto != -1) {
2495 if (us->flags & US_DBGLOG)
2496 DPRINT3("ppp/%d: found IP pkt, proto=0x%x (%d)\n",
2497 us->mn, pft->proto, pft->port);
2498 /* Discard if not connected, or if not pass_with_link_up */
2499 /* else, if link is up let go by, but don't update time */
2500 if (pft->ok_if_link_up)
2506 } /* end switch (proto) */
2510 #endif /* FILTER_PACKETS */