]> git.ozlabs.org Git - ppp.git/blob - modules/ppp.c
Previously, 64-bit compilation was dependent upon the version of the OS,
[ppp.git] / modules / ppp.c
1 /*
2  * ppp.c - STREAMS multiplexing pseudo-device driver for PPP.
3  *
4  * Copyright (c) 1994 The Australian National University.
5  * All rights reserved.
6  *
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
12  * any purpose.
13  *
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
18  * OF SUCH DAMAGE.
19  *
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,
25  * OR MODIFICATIONS.
26  *
27  * $Id: ppp.c,v 1.22 1999/09/17 05:59:00 paulus Exp $
28  */
29
30 /*
31  * This file is used under Solaris 2, SVR4, SunOS 4, and Digital UNIX.
32  */
33
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/stat.h>
37 #include <sys/stream.h>
38 #include <sys/stropts.h>
39 #include <sys/errno.h>
40 #ifdef __osf__
41 #include <sys/ioctl.h>
42 #include <sys/cmn_err.h>
43 #define queclass(mp)    ((mp)->b_band & QPCTL)
44 #else
45 #include <sys/ioccom.h>
46 #endif
47 #include <sys/time.h>
48 #ifdef SVR4
49 #include <sys/cmn_err.h>
50 #include <sys/conf.h>
51 #include <sys/dlpi.h>
52 #include <sys/ddi.h>
53 #ifdef SOL2
54 #include <sys/ksynch.h>
55 #include <sys/kstat.h>
56 #include <sys/sunddi.h>
57 #else
58 #include <sys/socket.h>
59 #include <sys/sockio.h>
60 #include <net/if.h>
61 #include <netinet/in.h>
62 #endif /* SOL2 */
63 #else /* not SVR4 */
64 #include <sys/user.h>
65 #endif /* SVR4 */
66 #include <net/ppp_defs.h>
67 #include <net/pppio.h>
68 #include "ppp_mod.h"
69
70 /*
71  * Modifications marked with #ifdef PRIOQ are for priority queueing of
72  * interactive traffic, and are due to Marko Zec <zec@japa.tel.fer.hr>.
73  */
74 #ifdef PRIOQ
75 #include <netinet/in.h>
76 #endif  /* PRIOQ */
77
78 #ifdef __STDC__
79 #define __P(x)  x
80 #else
81 #define __P(x)  ()
82 #endif
83
84 /*
85  * The IP module may use this SAP value for IP packets.
86  */
87 #ifndef ETHERTYPE_IP
88 #define ETHERTYPE_IP    0x800
89 #endif
90
91 extern time_t time;
92
93 #ifdef SOL2
94 /*
95  * We use this reader-writer lock to ensure that the lower streams
96  * stay connected to the upper streams while the lower-side put and
97  * service procedures are running.  Essentially it is an existence
98  * lock for the upper stream associated with each lower stream.
99  */
100 krwlock_t ppp_lower_lock;
101 #define LOCK_LOWER_W    rw_enter(&ppp_lower_lock, RW_WRITER)
102 #define LOCK_LOWER_R    rw_enter(&ppp_lower_lock, RW_READER)
103 #define TRYLOCK_LOWER_R rw_tryenter(&ppp_lower_lock, RW_READER)
104 #define UNLOCK_LOWER    rw_exit(&ppp_lower_lock)
105
106 #define MT_ENTER(x)     mutex_enter(x)
107 #define MT_EXIT(x)      mutex_exit(x)
108
109 /*
110  * Notes on multithreaded implementation for Solaris 2:
111  *
112  * We use an inner perimeter around each queue pair and an outer
113  * perimeter around the whole driver.  The inner perimeter is
114  * entered exclusively for all entry points (open, close, put,
115  * service).  The outer perimeter is entered exclusively for open
116  * and close and shared for put and service.  This is all done for
117  * us by the streams framework.
118  *
119  * I used to think that the perimeters were entered for the lower
120  * streams' put and service routines as well as for the upper streams'.
121  * Because of problems experienced by people, and after reading the
122  * documentation more closely, I now don't think that is true.  So we
123  * now use ppp_lower_lock to give us an existence guarantee on the
124  * upper stream controlling each lower stream.
125  *
126  * Shared entry to the outer perimeter protects the existence of all
127  * the upper streams and their upperstr_t structures, and guarantees
128  * that the following fields of any upperstr_t won't change:
129  * nextmn, next, nextppa.  It guarantees that the lowerq field of an
130  * upperstr_t won't go from non-zero to zero, that the global `ppas'
131  * won't change and that the no lower stream will get unlinked.
132  *
133  * Shared (reader) access to ppa_lower_lock guarantees that no lower
134  * stream will be unlinked and that the lowerq field of all upperstr_t
135  * structures won't change.
136  */
137
138 #else /* SOL2 */
139 #define LOCK_LOWER_W    0
140 #define LOCK_LOWER_R    0
141 #define TRYLOCK_LOWER_R 1
142 #define UNLOCK_LOWER    0
143 #define MT_ENTER(x)     0
144 #define MT_EXIT(x)      0
145
146 #endif /* SOL2 */
147
148 /*
149  * Private information; one per upper stream.
150  */
151 typedef struct upperstr {
152     minor_t mn;                 /* minor device number */
153     struct upperstr *nextmn;    /* next minor device */
154     queue_t *q;                 /* read q associated with this upper stream */
155     int flags;                  /* flag bits, see below */
156     int state;                  /* current DLPI state */
157     int sap;                    /* service access point */
158     int req_sap;                /* which SAP the DLPI client requested */
159     struct upperstr *ppa;       /* control stream for our ppa */
160     struct upperstr *next;      /* next stream for this ppa */
161     uint ioc_id;                /* last ioctl ID for this stream */
162     enum NPmode npmode;         /* what to do with packets on this SAP */
163     unsigned char rblocked;     /* flow control has blocked upper read strm */
164         /* N.B. rblocked is only changed by control stream's put/srv procs */
165     /*
166      * There is exactly one control stream for each PPA.
167      * The following fields are only used for control streams.
168      */
169     int ppa_id;
170     queue_t *lowerq;            /* write queue attached below this PPA */
171     struct upperstr *nextppa;   /* next control stream */
172     int mru;
173     int mtu;
174     struct pppstat stats;       /* statistics */
175     time_t last_sent;           /* time last NP packet sent */
176     time_t last_recv;           /* time last NP packet rcvd */
177 #ifdef SOL2
178     kmutex_t stats_lock;        /* lock for stats updates */
179     kstat_t *kstats;            /* stats for netstat */
180 #endif /* SOL2 */
181 #ifdef LACHTCP
182     int ifflags;
183     char ifname[IFNAMSIZ];
184     struct ifstats ifstats;
185 #endif /* LACHTCP */
186 } upperstr_t;
187
188 /* Values for flags */
189 #define US_PRIV         1       /* stream was opened by superuser */
190 #define US_CONTROL      2       /* stream is a control stream */
191 #define US_BLOCKED      4       /* flow ctrl has blocked lower write stream */
192 #define US_LASTMOD      8       /* no PPP modules below us */
193 #define US_DBGLOG       0x10    /* log various occurrences */
194 #define US_RBLOCKED     0x20    /* flow ctrl has blocked upper read stream */
195
196
197 #ifdef PRIOQ
198 static u_char max_band=0;
199 static u_char def_band=0;
200
201 #define IPPORT_DEFAULT          65535
202
203 /*
204  * Port priority table
205  * Highest priority ports are listed first, lowest are listed last.
206  * ICMP & packets using unlisted ports will be treated as "default".
207  * If IPPORT_DEFAULT is not listed here, "default" packets will be 
208  * assigned lowest priority.
209  * Each line should be terminated with "0".
210  * Line containing only "0" marks the end of the list.
211  */
212
213 static u_short prioq_table[]= {
214     113, 53, 0,
215     22, 23, 513, 517, 518, 0,
216     514, 21, 79, 111, 0,
217     25, 109, 110, 0,
218     IPPORT_DEFAULT, 0,
219     20, 70, 80, 8001, 8008, 8080, 0, /* 8001,8008,8080 - common proxy ports */
220 0 };
221
222 #endif  /* PRIOQ */
223
224
225 static upperstr_t *minor_devs = NULL;
226 static upperstr_t *ppas = NULL;
227
228 #ifdef SVR4
229 static int pppopen __P((queue_t *, dev_t *, int, int, cred_t *));
230 static int pppclose __P((queue_t *, int, cred_t *));
231 #else
232 static int pppopen __P((queue_t *, int, int, int));
233 static int pppclose __P((queue_t *, int));
234 #endif /* SVR4 */
235 static int pppurput __P((queue_t *, mblk_t *));
236 static int pppuwput __P((queue_t *, mblk_t *));
237 static int pppursrv __P((queue_t *));
238 static int pppuwsrv __P((queue_t *));
239 static int ppplrput __P((queue_t *, mblk_t *));
240 static int ppplwput __P((queue_t *, mblk_t *));
241 static int ppplrsrv __P((queue_t *));
242 static int ppplwsrv __P((queue_t *));
243 #ifndef NO_DLPI
244 static void dlpi_request __P((queue_t *, mblk_t *, upperstr_t *));
245 static void dlpi_error __P((queue_t *, upperstr_t *, int, int, int));
246 static void dlpi_ok __P((queue_t *, int));
247 #endif
248 static int send_data __P((mblk_t *, upperstr_t *));
249 static void new_ppa __P((queue_t *, mblk_t *));
250 static void attach_ppa __P((queue_t *, mblk_t *));
251 static void detach_ppa __P((queue_t *, mblk_t *));
252 static void detach_lower __P((queue_t *, mblk_t *));
253 static void debug_dump __P((queue_t *, mblk_t *));
254 static upperstr_t *find_dest __P((upperstr_t *, int));
255 static int putctl2 __P((queue_t *, int, int, int));
256 static int putctl4 __P((queue_t *, int, int, int));
257 static int pass_packet __P((upperstr_t *ppa, mblk_t *mp, int outbound));
258 #ifdef FILTER_PACKETS
259 static int ip_hard_filter __P((upperstr_t *ppa, mblk_t *mp, int outbound));
260 #endif /* FILTER_PACKETS */
261
262 #define PPP_ID 0xb1a6
263 static struct module_info ppp_info = {
264 #ifdef PRIOQ
265     PPP_ID, "ppp", 0, 512, 512, 384
266 #else
267     PPP_ID, "ppp", 0, 512, 512, 128
268 #endif  /* PRIOQ */
269 };
270
271 static struct qinit pppurint = {
272     pppurput, pppursrv, pppopen, pppclose, NULL, &ppp_info, NULL
273 };
274
275 static struct qinit pppuwint = {
276     pppuwput, pppuwsrv, NULL, NULL, NULL, &ppp_info, NULL
277 };
278
279 static struct qinit ppplrint = {
280     ppplrput, ppplrsrv, NULL, NULL, NULL, &ppp_info, NULL
281 };
282
283 static struct qinit ppplwint = {
284     ppplwput, ppplwsrv, NULL, NULL, NULL, &ppp_info, NULL
285 };
286
287 #ifdef LACHTCP
288 extern struct ifstats *ifstats;
289 int pppdevflag = 0;
290 #endif
291
292 struct streamtab pppinfo = {
293     &pppurint, &pppuwint,
294     &ppplrint, &ppplwint
295 };
296
297 int ppp_count;
298
299 /*
300  * How we maintain statistics.
301  */
302 #ifdef SOL2
303 #define INCR_IPACKETS(ppa)                              \
304         if (ppa->kstats != 0) {                         \
305             KSTAT_NAMED_PTR(ppa->kstats)[0].value.ul++; \
306         }
307 #define INCR_IERRORS(ppa)                               \
308         if (ppa->kstats != 0) {                         \
309             KSTAT_NAMED_PTR(ppa->kstats)[1].value.ul++; \
310         }
311 #define INCR_OPACKETS(ppa)                              \
312         if (ppa->kstats != 0) {                         \
313             KSTAT_NAMED_PTR(ppa->kstats)[2].value.ul++; \
314         }
315 #define INCR_OERRORS(ppa)                               \
316         if (ppa->kstats != 0) {                         \
317             KSTAT_NAMED_PTR(ppa->kstats)[3].value.ul++; \
318         }
319 #endif
320
321 #ifdef LACHTCP
322 #define INCR_IPACKETS(ppa)      ppa->ifstats.ifs_ipackets++;
323 #define INCR_IERRORS(ppa)       ppa->ifstats.ifs_ierrors++;
324 #define INCR_OPACKETS(ppa)      ppa->ifstats.ifs_opackets++;
325 #define INCR_OERRORS(ppa)       ppa->ifstats.ifs_oerrors++;
326 #endif
327
328 /*
329  * STREAMS driver entry points.
330  */
331 static int
332 #ifdef SVR4
333 pppopen(q, devp, oflag, sflag, credp)
334     queue_t *q;
335     dev_t *devp;
336     int oflag, sflag;
337     cred_t *credp;
338 #else
339 pppopen(q, dev, oflag, sflag)
340     queue_t *q;
341     int dev;                    /* really dev_t */
342     int oflag, sflag;
343 #endif
344 {
345     upperstr_t *up;
346     upperstr_t **prevp;
347     minor_t mn;
348 #ifdef PRIOQ
349     u_short *ptr;
350     u_char new_band;
351 #endif  /* PRIOQ */
352
353     if (q->q_ptr)
354         DRV_OPEN_OK(dev);       /* device is already open */
355
356 #ifdef PRIOQ
357     /* Calculate max_bband & def_band from definitions in prioq.h
358        This colud be done at some more approtiate time (less often)
359        but this way it works well so I'll just leave it here */
360
361     max_band = 1;
362     def_band = 0;
363     ptr = prioq_table;
364     while (*ptr) {
365         new_band = 1;
366         while (*ptr)
367             if (*ptr++ == IPPORT_DEFAULT) {
368                 new_band = 0;
369                 def_band = max_band;
370             }
371         max_band += new_band;
372         ptr++;
373     }
374     if (def_band)
375         def_band = max_band - def_band;
376     --max_band;
377 #endif  /* PRIOQ */
378
379     if (sflag == CLONEOPEN) {
380         mn = 0;
381         for (prevp = &minor_devs; (up = *prevp) != 0; prevp = &up->nextmn) {
382             if (up->mn != mn)
383                 break;
384             ++mn;
385         }
386     } else {
387 #ifdef SVR4
388         mn = getminor(*devp);
389 #else
390         mn = minor(dev);
391 #endif
392         for (prevp = &minor_devs; (up = *prevp) != 0; prevp = &up->nextmn) {
393             if (up->mn >= mn)
394                 break;
395         }
396         if (up->mn == mn) {
397             /* this can't happen */
398             q->q_ptr = WR(q)->q_ptr = (caddr_t) up;
399             DRV_OPEN_OK(dev);
400         }
401     }
402
403     /*
404      * Construct a new minor node.
405      */
406     up = (upperstr_t *) ALLOC_SLEEP(sizeof(upperstr_t));
407     bzero((caddr_t) up, sizeof(upperstr_t));
408     if (up == 0) {
409         DPRINT("pppopen: out of kernel memory\n");
410         OPEN_ERROR(ENXIO);
411     }
412     up->nextmn = *prevp;
413     *prevp = up;
414     up->mn = mn;
415 #ifdef SVR4
416     *devp = makedevice(getmajor(*devp), mn);
417 #endif
418     up->q = q;
419     if (NOTSUSER() == 0)
420         up->flags |= US_PRIV;
421 #ifndef NO_DLPI
422     up->state = DL_UNATTACHED;
423 #endif
424 #ifdef LACHTCP
425     up->ifflags = IFF_UP | IFF_POINTOPOINT;
426 #endif
427     up->sap = -1;
428     up->last_sent = up->last_recv = time;
429     up->npmode = NPMODE_DROP;
430     q->q_ptr = (caddr_t) up;
431     WR(q)->q_ptr = (caddr_t) up;
432     noenable(WR(q));
433 #ifdef SOL2
434     mutex_init(&up->stats_lock, NULL, MUTEX_DRIVER, NULL);
435 #endif
436     ++ppp_count;
437
438     qprocson(q);
439     DRV_OPEN_OK(makedev(major(dev), mn));
440 }
441
442 static int
443 #ifdef SVR4
444 pppclose(q, flag, credp)
445     queue_t *q;
446     int flag;
447     cred_t *credp;
448 #else
449 pppclose(q, flag)
450     queue_t *q;
451     int flag;
452 #endif
453 {
454     upperstr_t *up, **upp;
455     upperstr_t *as, *asnext;
456     upperstr_t **prevp;
457
458     qprocsoff(q);
459
460     up = (upperstr_t *) q->q_ptr;
461     if (up == 0) {
462         DPRINT("pppclose: q_ptr = 0\n");
463         return 0;
464     }
465     if (up->flags & US_DBGLOG)
466         DPRINT2("ppp/%d: close, flags=%x\n", up->mn, up->flags);
467     if (up->flags & US_CONTROL) {
468 #ifdef LACHTCP
469         struct ifstats *ifp, *pifp;
470 #endif
471         if (up->lowerq != 0) {
472             /* Gack! the lower stream should have be unlinked earlier! */
473             DPRINT1("ppp%d: lower stream still connected on close?\n",
474                     up->mn);
475             LOCK_LOWER_W;
476             up->lowerq->q_ptr = 0;
477             RD(up->lowerq)->q_ptr = 0;
478             up->lowerq = 0;
479             UNLOCK_LOWER;
480         }
481
482         /*
483          * This stream represents a PPA:
484          * For all streams attached to the PPA, clear their
485          * references to this PPA.
486          * Then remove this PPA from the list of PPAs.
487          */
488         for (as = up->next; as != 0; as = asnext) {
489             asnext = as->next;
490             as->next = 0;
491             as->ppa = 0;
492             if (as->flags & US_BLOCKED) {
493                 as->flags &= ~US_BLOCKED;
494                 flushq(WR(as->q), FLUSHDATA);
495             }
496         }
497         for (upp = &ppas; *upp != 0; upp = &(*upp)->nextppa)
498             if (*upp == up) {
499                 *upp = up->nextppa;
500                 break;
501             }
502 #ifdef LACHTCP
503         /* Remove the statistics from the active list.  */
504         for (ifp = ifstats, pifp = 0; ifp; ifp = ifp->ifs_next) {
505             if (ifp == &up->ifstats) {
506                 if (pifp)
507                     pifp->ifs_next = ifp->ifs_next;
508                 else
509                     ifstats = ifp->ifs_next;
510                 break;
511             }
512             pifp = ifp;
513         }
514 #endif
515     } else {
516         /*
517          * If this stream is attached to a PPA,
518          * remove it from the PPA's list.
519          */
520         if ((as = up->ppa) != 0) {
521             for (; as->next != 0; as = as->next)
522                 if (as->next == up) {
523                     as->next = up->next;
524                     break;
525                 }
526         }
527     }
528
529 #ifdef SOL2
530     if (up->kstats)
531         kstat_delete(up->kstats);
532     mutex_destroy(&up->stats_lock);
533 #endif
534
535     q->q_ptr = NULL;
536     WR(q)->q_ptr = NULL;
537
538     for (prevp = &minor_devs; *prevp != 0; prevp = &(*prevp)->nextmn) {
539         if (*prevp == up) {
540             *prevp = up->nextmn;
541             break;
542         }
543     }
544     FREE(up, sizeof(upperstr_t));
545     --ppp_count;
546
547     return 0;
548 }
549
550 /*
551  * A message from on high.  We do one of three things:
552  *      - qreply()
553  *      - put the message on the lower write stream
554  *      - queue it for our service routine
555  */
556 static int
557 pppuwput(q, mp)
558     queue_t *q;
559     mblk_t *mp;
560 {
561     upperstr_t *us, *ppa, *nps;
562     struct iocblk *iop;
563     struct linkblk *lb;
564 #ifdef LACHTCP
565     struct ifreq *ifr;
566     int i;
567 #endif
568     queue_t *lq;
569     int error, n, sap;
570     mblk_t *mq;
571     struct ppp_idle *pip;
572 #ifdef PRIOQ
573     queue_t *tlq;
574 #endif  /* PRIOQ */
575 #ifdef NO_DLPI
576     upperstr_t *os;
577 #endif
578
579     us = (upperstr_t *) q->q_ptr;
580     if (us == 0) {
581         DPRINT("pppuwput: q_ptr = 0!\n");
582         return 0;
583     }
584     if (mp == 0) {
585         DPRINT1("pppuwput/%d: mp = 0!\n", us->mn);
586         return 0;
587     }
588     if (mp->b_datap == 0) {
589         DPRINT1("pppuwput/%d: mp->b_datap = 0!\n", us->mn);
590         return 0;
591     }
592     switch (mp->b_datap->db_type) {
593 #ifndef NO_DLPI
594     case M_PCPROTO:
595     case M_PROTO:
596         dlpi_request(q, mp, us);
597         break;
598 #endif /* NO_DLPI */
599
600     case M_DATA:
601         if (us->flags & US_DBGLOG)
602             DPRINT3("ppp/%d: uwput M_DATA len=%d flags=%x\n",
603                     us->mn, msgdsize(mp), us->flags);
604         if (us->ppa == 0 || msgdsize(mp) > us->ppa->mtu + PPP_HDRLEN
605 #ifndef NO_DLPI
606             || (us->flags & US_CONTROL) == 0
607 #endif /* NO_DLPI */
608             ) {
609             DPRINT1("pppuwput: junk data len=%d\n", msgdsize(mp));
610             freemsg(mp);
611             break;
612         }
613 #ifdef NO_DLPI
614         if ((us->flags & US_CONTROL) == 0 && !pass_packet(us, mp, 1))
615             break;
616 #endif
617         if (!send_data(mp, us))
618             putq(q, mp);
619         break;
620
621     case M_IOCTL:
622         iop = (struct iocblk *) mp->b_rptr;
623         error = EINVAL;
624         if (us->flags & US_DBGLOG)
625             DPRINT3("ppp/%d: ioctl %x count=%d\n",
626                     us->mn, iop->ioc_cmd, iop->ioc_count);
627         switch (iop->ioc_cmd) {
628         case I_LINK:
629             if ((us->flags & US_CONTROL) == 0 || us->lowerq != 0)
630                 break;
631             if (mp->b_cont == 0) {
632                 DPRINT1("pppuwput/%d: ioctl I_LINK b_cont = 0!\n", us->mn);
633                 break;
634             }
635             lb = (struct linkblk *) mp->b_cont->b_rptr;
636             lq = lb->l_qbot;
637             if (lq == 0) {
638                 DPRINT1("pppuwput/%d: ioctl I_LINK l_qbot = 0!\n", us->mn);
639                 break;
640             }
641             LOCK_LOWER_W;
642             us->lowerq = lq;
643             lq->q_ptr = (caddr_t) q;
644             RD(lq)->q_ptr = (caddr_t) us->q;
645             UNLOCK_LOWER;
646             iop->ioc_count = 0;
647             error = 0;
648             us->flags &= ~US_LASTMOD;
649             /* Unblock upper streams which now feed this lower stream. */
650             qenable(q);
651             /* Send useful information down to the modules which
652                are now linked below us. */
653             putctl2(lq, M_CTL, PPPCTL_UNIT, us->ppa_id);
654             putctl4(lq, M_CTL, PPPCTL_MRU, us->mru);
655             putctl4(lq, M_CTL, PPPCTL_MTU, us->mtu);
656 #ifdef PRIOQ
657             /* Lower tty driver's queue hiwat/lowat from default 4096/128
658                to 256/128 since we don't want queueing of data on
659                output to physical device */
660
661             freezestr(lq);
662             for (tlq = lq; tlq->q_next != NULL; tlq = tlq->q_next)
663                 ;
664             strqset(tlq, QHIWAT, 0, 256);
665             strqset(tlq, QLOWAT, 0, 128);
666             unfreezestr(lq);
667 #endif  /* PRIOQ */
668             break;
669
670         case I_UNLINK:
671             if (mp->b_cont == 0) {
672                 DPRINT1("pppuwput/%d: ioctl I_UNLINK b_cont = 0!\n", us->mn);
673                 break;
674             }
675             lb = (struct linkblk *) mp->b_cont->b_rptr;
676 #if DEBUG
677             if (us->lowerq != lb->l_qbot) {
678                 DPRINT2("ppp unlink: lowerq=%x qbot=%x\n",
679                         us->lowerq, lb->l_qbot);
680                 break;
681             }
682 #endif
683             iop->ioc_count = 0;
684             qwriter(q, mp, detach_lower, PERIM_OUTER);
685             error = -1;
686             break;
687
688         case PPPIO_NEWPPA:
689             if (us->flags & US_CONTROL)
690                 break;
691             if ((us->flags & US_PRIV) == 0) {
692                 error = EPERM;
693                 break;
694             }
695             /* Arrange to return an int */
696             if ((mq = mp->b_cont) == 0
697                 || mq->b_datap->db_lim - mq->b_rptr < sizeof(int)) {
698                 mq = allocb(sizeof(int), BPRI_HI);
699                 if (mq == 0) {
700                     error = ENOSR;
701                     break;
702                 }
703                 if (mp->b_cont != 0)
704                     freemsg(mp->b_cont);
705                 mp->b_cont = mq;
706                 mq->b_cont = 0;
707             }
708             iop->ioc_count = sizeof(int);
709             mq->b_wptr = mq->b_rptr + sizeof(int);
710             qwriter(q, mp, new_ppa, PERIM_OUTER);
711             error = -1;
712             break;
713
714         case PPPIO_ATTACH:
715             /* like dlpi_attach, for programs which can't write to
716                the stream (like pppstats) */
717             if (iop->ioc_count != sizeof(int) || us->ppa != 0)
718                 break;
719             if (mp->b_cont == 0) {
720                 DPRINT1("pppuwput/%d: ioctl PPPIO_ATTACH b_cont = 0!\n", us->mn);
721                 break;
722             }
723             n = *(int *)mp->b_cont->b_rptr;
724             for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
725                 if (ppa->ppa_id == n)
726                     break;
727             if (ppa == 0)
728                 break;
729             us->ppa = ppa;
730             iop->ioc_count = 0;
731             qwriter(q, mp, attach_ppa, PERIM_OUTER);
732             error = -1;
733             break;
734
735 #ifdef NO_DLPI
736         case PPPIO_BIND:
737             /* Attach to a given SAP. */
738             if (iop->ioc_count != sizeof(int) || us->ppa == 0)
739                 break;
740             if (mp->b_cont == 0) {
741                 DPRINT1("pppuwput/%d: ioctl PPPIO_BIND b_cont = 0!\n", us->mn);
742                 break;
743             }
744             n = *(int *)mp->b_cont->b_rptr;
745             /* n must be a valid PPP network protocol number. */
746             if (n < 0x21 || n > 0x3fff || (n & 0x101) != 1)
747                 break;
748             /* check that no other stream is bound to this sap already. */
749             for (os = us->ppa; os != 0; os = os->next)
750                 if (os->sap == n)
751                     break;
752             if (os != 0)
753                 break;
754             us->sap = n;
755             iop->ioc_count = 0;
756             error = 0;
757             break;
758 #endif /* NO_DLPI */
759
760         case PPPIO_MRU:
761             if (iop->ioc_count != sizeof(int) || (us->flags & US_CONTROL) == 0)
762                 break;
763             if (mp->b_cont == 0) {
764                 DPRINT1("pppuwput/%d: ioctl PPPIO_MRU b_cont = 0!\n", us->mn);
765                 break;
766             }
767             n = *(int *)mp->b_cont->b_rptr;
768             if (n <= 0 || n > PPP_MAXMRU)
769                 break;
770             if (n < PPP_MRU)
771                 n = PPP_MRU;
772             us->mru = n;
773             if (us->lowerq)
774                 putctl4(us->lowerq, M_CTL, PPPCTL_MRU, n);
775             error = 0;
776             iop->ioc_count = 0;
777             break;
778
779         case PPPIO_MTU:
780             if (iop->ioc_count != sizeof(int) || (us->flags & US_CONTROL) == 0)
781                 break;
782             if (mp->b_cont == 0) {
783                 DPRINT1("pppuwput/%d: ioctl PPPIO_MTU b_cont = 0!\n", us->mn);
784                 break;
785             }
786             n = *(int *)mp->b_cont->b_rptr;
787             if (n <= 0 || n > PPP_MAXMTU)
788                 break;
789             us->mtu = n;
790 #ifdef LACHTCP
791             /* The MTU reported in netstat, not used as IP max packet size! */
792             us->ifstats.ifs_mtu = n;
793 #endif
794             if (us->lowerq)
795                 putctl4(us->lowerq, M_CTL, PPPCTL_MTU, n);
796             error = 0;
797             iop->ioc_count = 0;
798             break;
799
800         case PPPIO_LASTMOD:
801             us->flags |= US_LASTMOD;
802             error = 0;
803             break;
804
805         case PPPIO_DEBUG:
806             if (iop->ioc_count != sizeof(int))
807                 break;
808             if (mp->b_cont == 0) {
809                 DPRINT1("pppuwput/%d: ioctl PPPIO_DEBUG b_cont = 0!\n", us->mn);
810                 break;
811             }
812             n = *(int *)mp->b_cont->b_rptr;
813             if (n == PPPDBG_DUMP + PPPDBG_DRIVER) {
814                 qwriter(q, NULL, debug_dump, PERIM_OUTER);
815                 iop->ioc_count = 0;
816                 error = -1;
817             } else if (n == PPPDBG_LOG + PPPDBG_DRIVER) {
818                 DPRINT1("ppp/%d: debug log enabled\n", us->mn);
819                 us->flags |= US_DBGLOG;
820                 iop->ioc_count = 0;
821                 error = 0;
822             } else {
823                 if (us->ppa == 0 || us->ppa->lowerq == 0)
824                     break;
825                 putnext(us->ppa->lowerq, mp);
826                 error = -1;
827             }
828             break;
829
830         case PPPIO_NPMODE:
831             if (iop->ioc_count != 2 * sizeof(int))
832                 break;
833             if ((us->flags & US_CONTROL) == 0)
834                 break;
835             if (mp->b_cont == 0) {
836                 DPRINT1("pppuwput/%d: ioctl PPPIO_NPMODE b_cont = 0!\n", us->mn);
837                 break;
838             }
839             sap = ((int *)mp->b_cont->b_rptr)[0];
840             for (nps = us->next; nps != 0; nps = nps->next)
841                 if (nps->sap == sap)
842                     break;
843             if (nps == 0) {
844                 if (us->flags & US_DBGLOG)
845                     DPRINT2("ppp/%d: no stream for sap %x\n", us->mn, sap);
846                 break;
847             }
848             /* XXX possibly should use qwriter here */
849             nps->npmode = (enum NPmode) ((int *)mp->b_cont->b_rptr)[1];
850             if (nps->npmode != NPMODE_QUEUE && (nps->flags & US_BLOCKED) != 0)
851                 qenable(WR(nps->q));
852             iop->ioc_count = 0;
853             error = 0;
854             break;
855
856         case PPPIO_GIDLE:
857             if ((ppa = us->ppa) == 0)
858                 break;
859             mq = allocb(sizeof(struct ppp_idle), BPRI_HI);
860             if (mq == 0) {
861                 error = ENOSR;
862                 break;
863             }
864             if (mp->b_cont != 0)
865                 freemsg(mp->b_cont);
866             mp->b_cont = mq;
867             mq->b_cont = 0;
868             pip = (struct ppp_idle *) mq->b_wptr;
869             pip->xmit_idle = time - ppa->last_sent;
870             pip->recv_idle = time - ppa->last_recv;
871             mq->b_wptr += sizeof(struct ppp_idle);
872             iop->ioc_count = sizeof(struct ppp_idle);
873             error = 0;
874             break;
875
876 #ifdef LACHTCP
877         case SIOCSIFNAME:
878             /* Sent from IP down to us.  Attach the ifstats structure.  */
879             if (iop->ioc_count != sizeof(struct ifreq) || us->ppa == 0)
880                 break;
881             ifr = (struct ifreq *)mp->b_cont->b_rptr;
882             /* Find the unit number in the interface name.  */
883             for (i = 0; i < IFNAMSIZ; i++) {
884                 if (ifr->ifr_name[i] == 0 ||
885                     (ifr->ifr_name[i] >= '0' &&
886                      ifr->ifr_name[i] <= '9'))
887                     break;
888                 else
889                     us->ifname[i] = ifr->ifr_name[i];
890             }
891             us->ifname[i] = 0;
892
893             /* Convert the unit number to binary.  */
894             for (n = 0; i < IFNAMSIZ; i++) {
895                 if (ifr->ifr_name[i] == 0) {
896                     break;
897                 }
898                 else {
899                     n = n * 10 + ifr->ifr_name[i] - '0';
900                 }
901             }
902
903             /* Verify the ppa.  */
904             if (us->ppa->ppa_id != n)
905                 break;
906             ppa = us->ppa;
907
908             /* Set up the netstat block.  */
909             strncpy (ppa->ifname, us->ifname, IFNAMSIZ);
910
911             ppa->ifstats.ifs_name = ppa->ifname;
912             ppa->ifstats.ifs_unit = n;
913             ppa->ifstats.ifs_active = us->state != DL_UNBOUND;
914             ppa->ifstats.ifs_mtu = ppa->mtu;
915
916             /* Link in statistics used by netstat.  */
917             ppa->ifstats.ifs_next = ifstats;
918             ifstats = &ppa->ifstats;
919
920             iop->ioc_count = 0;
921             error = 0;
922             break;
923
924         case SIOCGIFFLAGS:
925             if (!(us->flags & US_CONTROL)) {
926                 if (us->ppa)
927                     us = us->ppa;
928                 else
929                     break;
930             }
931             ((struct iocblk_in *)iop)->ioc_ifflags = us->ifflags;
932             error = 0;
933             break;
934
935         case SIOCSIFFLAGS:
936             if (!(us->flags & US_CONTROL)) {
937                 if (us->ppa)
938                     us = us->ppa;
939                 else
940                     break;
941             }
942             us->ifflags = ((struct iocblk_in *)iop)->ioc_ifflags;
943             error = 0;
944             break;
945
946         case SIOCSIFADDR:
947             if (!(us->flags & US_CONTROL)) {
948                 if (us->ppa)
949                     us = us->ppa;
950                 else
951                     break;
952             }
953             us->ifflags |= IFF_RUNNING;
954             ((struct iocblk_in *)iop)->ioc_ifflags |= IFF_RUNNING;
955             error = 0;
956             break;
957
958         case SIOCSIFMTU:
959             /*
960              * Vanilla SVR4 systems don't handle SIOCSIFMTU, rather
961              * they take the MTU from the DL_INFO_ACK we sent in response
962              * to their DL_INFO_REQ.  Fortunately, they will update the
963              * MTU if we send an unsolicited DL_INFO_ACK up.
964              */
965             if ((mq = allocb(sizeof(dl_info_req_t), BPRI_HI)) == 0)
966                 break;          /* should do bufcall */
967             ((union DL_primitives *)mq->b_rptr)->dl_primitive = DL_INFO_REQ;
968             mq->b_wptr = mq->b_rptr + sizeof(dl_info_req_t);
969             dlpi_request(q, mq, us);
970             error = 0;
971             break;
972
973         case SIOCGIFNETMASK:
974         case SIOCSIFNETMASK:
975         case SIOCGIFADDR:
976         case SIOCGIFDSTADDR:
977         case SIOCSIFDSTADDR:
978         case SIOCGIFMETRIC:
979             error = 0;
980             break;
981 #endif /* LACHTCP */
982
983         default:
984             if (us->ppa == 0 || us->ppa->lowerq == 0)
985                 break;
986             us->ioc_id = iop->ioc_id;
987             error = -1;
988             switch (iop->ioc_cmd) {
989             case PPPIO_GETSTAT:
990             case PPPIO_GETCSTAT:
991                 if (us->flags & US_LASTMOD) {
992                     error = EINVAL;
993                     break;
994                 }
995                 putnext(us->ppa->lowerq, mp);
996                 break;
997             default:
998                 if (us->flags & US_PRIV)
999                     putnext(us->ppa->lowerq, mp);
1000                 else {
1001                     DPRINT1("ppp ioctl %x rejected\n", iop->ioc_cmd);
1002                     error = EPERM;
1003                 }
1004                 break;
1005             }
1006             break;
1007         }
1008
1009         if (error > 0) {
1010             iop->ioc_error = error;
1011             mp->b_datap->db_type = M_IOCNAK;
1012             qreply(q, mp);
1013         } else if (error == 0) {
1014             mp->b_datap->db_type = M_IOCACK;
1015             qreply(q, mp);
1016         }
1017         break;
1018
1019     case M_FLUSH:
1020         if (us->flags & US_DBGLOG)
1021             DPRINT2("ppp/%d: flush %x\n", us->mn, *mp->b_rptr);
1022         if (*mp->b_rptr & FLUSHW)
1023             flushq(q, FLUSHDATA);
1024         if (*mp->b_rptr & FLUSHR) {
1025             *mp->b_rptr &= ~FLUSHW;
1026             qreply(q, mp);
1027         } else
1028             freemsg(mp);
1029         break;
1030
1031     default:
1032         freemsg(mp);
1033         break;
1034     }
1035     return 0;
1036 }
1037
1038 #ifndef NO_DLPI
1039 static void
1040 dlpi_request(q, mp, us)
1041     queue_t *q;
1042     mblk_t *mp;
1043     upperstr_t *us;
1044 {
1045     union DL_primitives *d = (union DL_primitives *) mp->b_rptr;
1046     int size = mp->b_wptr - mp->b_rptr;
1047     mblk_t *reply, *np;
1048     upperstr_t *ppa, *os;
1049     int sap, len;
1050     dl_info_ack_t *info;
1051     dl_bind_ack_t *ackp;
1052
1053     if (us->flags & US_DBGLOG)
1054         DPRINT3("ppp/%d: dlpi prim %x len=%d\n", us->mn,
1055                 d->dl_primitive, size);
1056     switch (d->dl_primitive) {
1057     case DL_INFO_REQ:
1058         if (size < sizeof(dl_info_req_t))
1059             goto badprim;
1060         if ((reply = allocb(sizeof(dl_info_ack_t), BPRI_HI)) == 0)
1061             break;              /* should do bufcall */
1062         reply->b_datap->db_type = M_PCPROTO;
1063         info = (dl_info_ack_t *) reply->b_wptr;
1064         reply->b_wptr += sizeof(dl_info_ack_t);
1065         bzero((caddr_t) info, sizeof(dl_info_ack_t));
1066         info->dl_primitive = DL_INFO_ACK;
1067         info->dl_max_sdu = us->ppa? us->ppa->mtu: PPP_MAXMTU;
1068         info->dl_min_sdu = 1;
1069         info->dl_addr_length = sizeof(uint);
1070 #if 0
1071 #ifdef DL_OTHER
1072         info->dl_mac_type = DL_OTHER;
1073 #else
1074         info->dl_mac_type = DL_HDLC;    /* a lie */
1075 #endif
1076 #else
1077         info->dl_mac_type = DL_ETHER;   /* a bigger lie */
1078 #endif
1079         info->dl_current_state = us->state;
1080         info->dl_service_mode = DL_CLDLS;
1081         info->dl_provider_style = DL_STYLE2;
1082 #if DL_CURRENT_VERSION >= 2
1083         info->dl_sap_length = sizeof(uint);
1084         info->dl_version = DL_CURRENT_VERSION;
1085 #endif
1086         qreply(q, reply);
1087         break;
1088
1089     case DL_ATTACH_REQ:
1090         if (size < sizeof(dl_attach_req_t))
1091             goto badprim;
1092         if (us->state != DL_UNATTACHED || us->ppa != 0) {
1093             dlpi_error(q, us, DL_ATTACH_REQ, DL_OUTSTATE, 0);
1094             break;
1095         }
1096         for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
1097             if (ppa->ppa_id == d->attach_req.dl_ppa)
1098                 break;
1099         if (ppa == 0) {
1100             dlpi_error(q, us, DL_ATTACH_REQ, DL_BADPPA, 0);
1101             break;
1102         }
1103         us->ppa = ppa;
1104         qwriter(q, mp, attach_ppa, PERIM_OUTER);
1105         return;
1106
1107     case DL_DETACH_REQ:
1108         if (size < sizeof(dl_detach_req_t))
1109             goto badprim;
1110         if (us->state != DL_UNBOUND || us->ppa == 0) {
1111             dlpi_error(q, us, DL_DETACH_REQ, DL_OUTSTATE, 0);
1112             break;
1113         }
1114         qwriter(q, mp, detach_ppa, PERIM_OUTER);
1115         return;
1116
1117     case DL_BIND_REQ:
1118         if (size < sizeof(dl_bind_req_t))
1119             goto badprim;
1120         if (us->state != DL_UNBOUND || us->ppa == 0) {
1121             dlpi_error(q, us, DL_BIND_REQ, DL_OUTSTATE, 0);
1122             break;
1123         }
1124 #if 0
1125         /* apparently this test fails (unnecessarily?) on some systems */
1126         if (d->bind_req.dl_service_mode != DL_CLDLS) {
1127             dlpi_error(q, us, DL_BIND_REQ, DL_UNSUPPORTED, 0);
1128             break;
1129         }
1130 #endif
1131
1132         /* saps must be valid PPP network protocol numbers,
1133            except that we accept ETHERTYPE_IP in place of PPP_IP. */
1134         sap = d->bind_req.dl_sap;
1135         us->req_sap = sap;
1136
1137 #ifdef SOL2
1138         /* 
1139          * ip will send a sap value of 0 (post-Solaris 7), or
1140          * ETHERTYPE_IP (0x800) (pre-Solaris 8) due to how the
1141          * ppp DLPI provider declares its characteristics.
1142          * <adi.masputra@sun.com>
1143          */
1144         if (sap == 0)
1145             sap = ETHERTYPE_IP;
1146 #endif /* SOL2 */
1147
1148         if (sap == ETHERTYPE_IP)
1149             sap = PPP_IP;
1150         if (sap < 0x21 || sap > 0x3fff || (sap & 0x101) != 1) {
1151             dlpi_error(q, us, DL_BIND_REQ, DL_BADADDR, 0);
1152             break;
1153         }
1154
1155         /* check that no other stream is bound to this sap already. */
1156         for (os = us->ppa; os != 0; os = os->next)
1157             if (os->sap == sap)
1158                 break;
1159         if (os != 0) {
1160             dlpi_error(q, us, DL_BIND_REQ, DL_NOADDR, 0);
1161             break;
1162         }
1163
1164         us->sap = sap;
1165         us->state = DL_IDLE;
1166
1167         if ((reply = allocb(sizeof(dl_bind_ack_t) + sizeof(uint),
1168                             BPRI_HI)) == 0)
1169             break;              /* should do bufcall */
1170         ackp = (dl_bind_ack_t *) reply->b_wptr;
1171         reply->b_wptr += sizeof(dl_bind_ack_t) + sizeof(uint);
1172         reply->b_datap->db_type = M_PCPROTO;
1173         bzero((caddr_t) ackp, sizeof(dl_bind_ack_t));
1174         ackp->dl_primitive = DL_BIND_ACK;
1175         ackp->dl_sap = sap;
1176         ackp->dl_addr_length = sizeof(uint);
1177         ackp->dl_addr_offset = sizeof(dl_bind_ack_t);
1178         *(uint *)(ackp+1) = sap;
1179         qreply(q, reply);
1180         break;
1181
1182     case DL_UNBIND_REQ:
1183         if (size < sizeof(dl_unbind_req_t))
1184             goto badprim;
1185         if (us->state != DL_IDLE) {
1186             dlpi_error(q, us, DL_UNBIND_REQ, DL_OUTSTATE, 0);
1187             break;
1188         }
1189         us->sap = -1;
1190         us->state = DL_UNBOUND;
1191 #ifdef LACHTCP
1192         us->ppa->ifstats.ifs_active = 0;
1193 #endif
1194         dlpi_ok(q, DL_UNBIND_REQ);
1195         break;
1196
1197     case DL_UNITDATA_REQ:
1198         if (size < sizeof(dl_unitdata_req_t))
1199             goto badprim;
1200         if (us->state != DL_IDLE) {
1201             dlpi_error(q, us, DL_UNITDATA_REQ, DL_OUTSTATE, 0);
1202             break;
1203         }
1204         if ((ppa = us->ppa) == 0) {
1205             cmn_err(CE_CONT, "ppp: in state dl_idle but ppa == 0?\n");
1206             break;
1207         }
1208         len = mp->b_cont == 0? 0: msgdsize(mp->b_cont);
1209         if (len > ppa->mtu) {
1210             DPRINT2("dlpi data too large (%d > %d)\n", len, ppa->mtu);
1211             break;
1212         }
1213         mp->b_band = 0;
1214 #ifdef PRIOQ
1215         /* Extract s_port & d_port from IP-packet, the code is a bit
1216            dirty here, but so am I, too... */
1217         if (mp->b_datap->db_type == M_PROTO && us->sap == PPP_IP
1218             && mp->b_cont != 0) {
1219             u_char *bb, *tlh;
1220             int iphlen, len;
1221             u_short *ptr;
1222             u_char band_unset, cur_band, syn;
1223             u_short s_port, d_port;
1224
1225             bb = mp->b_cont->b_rptr; /* bb points to IP-header*/
1226             len = mp->b_cont->b_wptr - mp->b_cont->b_rptr;
1227             syn = 0;
1228             s_port = IPPORT_DEFAULT;
1229             d_port = IPPORT_DEFAULT;
1230             if (len >= 20) {    /* 20 = minimum length of IP header */
1231                 iphlen = (bb[0] & 0x0f) * 4;
1232                 tlh = bb + iphlen;
1233                 len -= iphlen;
1234                 switch (bb[9]) {
1235                 case IPPROTO_TCP:
1236                     if (len >= 20) {          /* min length of TCP header */
1237                         s_port = (tlh[0] << 8) + tlh[1];
1238                         d_port = (tlh[2] << 8) + tlh[3];
1239                         syn = tlh[13] & 0x02;
1240                     }
1241                     break;
1242                 case IPPROTO_UDP:
1243                     if (len >= 8) {           /* min length of UDP header */
1244                         s_port = (tlh[0] << 8) + tlh[1];
1245                         d_port = (tlh[2] << 8) + tlh[3];
1246                     }
1247                     break;
1248                 }
1249             }
1250
1251             /*
1252              * Now calculate b_band for this packet from the
1253              * port-priority table.
1254              */
1255             ptr = prioq_table;
1256             cur_band = max_band;
1257             band_unset = 1;
1258             while (*ptr) {
1259                 while (*ptr && band_unset)
1260                     if (s_port == *ptr || d_port == *ptr++) {
1261                         mp->b_band = cur_band;
1262                         band_unset = 0;
1263                         break;
1264                     }
1265                 ptr++;
1266                 cur_band--;
1267             }
1268             if (band_unset)
1269                 mp->b_band = def_band;
1270             /* It may be usable to urge SYN packets a bit */
1271             if (syn)
1272                 mp->b_band++;
1273         }
1274 #endif  /* PRIOQ */
1275         /* this assumes PPP_HDRLEN <= sizeof(dl_unitdata_req_t) */
1276         if (mp->b_datap->db_ref > 1) {
1277             np = allocb(PPP_HDRLEN, BPRI_HI);
1278             if (np == 0)
1279                 break;          /* gak! */
1280             np->b_cont = mp->b_cont;
1281             mp->b_cont = 0;
1282             freeb(mp);
1283             mp = np;
1284         } else
1285             mp->b_datap->db_type = M_DATA;
1286         /* XXX should use dl_dest_addr_offset/length here,
1287            but we would have to translate ETHERTYPE_IP -> PPP_IP */
1288         mp->b_wptr = mp->b_rptr + PPP_HDRLEN;
1289         mp->b_rptr[0] = PPP_ALLSTATIONS;
1290         mp->b_rptr[1] = PPP_UI;
1291         mp->b_rptr[2] = us->sap >> 8;
1292         mp->b_rptr[3] = us->sap;
1293         if (pass_packet(us, mp, 1)) {
1294             if (!send_data(mp, us))
1295                 putq(q, mp);
1296         }
1297         return;
1298
1299 #if DL_CURRENT_VERSION >= 2
1300     case DL_SUBS_BIND_REQ:
1301     case DL_SUBS_UNBIND_REQ:
1302     case DL_ENABMULTI_REQ:
1303     case DL_DISABMULTI_REQ:
1304     case DL_PROMISCON_REQ:
1305     case DL_PROMISCOFF_REQ:
1306     case DL_PHYS_ADDR_REQ:
1307     case DL_SET_PHYS_ADDR_REQ:
1308     case DL_XID_REQ:
1309     case DL_TEST_REQ:
1310     case DL_REPLY_UPDATE_REQ:
1311     case DL_REPLY_REQ:
1312     case DL_DATA_ACK_REQ:
1313 #endif
1314     case DL_CONNECT_REQ:
1315     case DL_TOKEN_REQ:
1316         dlpi_error(q, us, d->dl_primitive, DL_NOTSUPPORTED, 0);
1317         break;
1318
1319     case DL_CONNECT_RES:
1320     case DL_DISCONNECT_REQ:
1321     case DL_RESET_REQ:
1322     case DL_RESET_RES:
1323         dlpi_error(q, us, d->dl_primitive, DL_OUTSTATE, 0);
1324         break;
1325
1326     case DL_UDQOS_REQ:
1327         dlpi_error(q, us, d->dl_primitive, DL_BADQOSTYPE, 0);
1328         break;
1329
1330 #if DL_CURRENT_VERSION >= 2
1331     case DL_TEST_RES:
1332     case DL_XID_RES:
1333         break;
1334 #endif
1335
1336     default:
1337         cmn_err(CE_CONT, "ppp: unknown dlpi prim 0x%x\n", d->dl_primitive);
1338         /* fall through */
1339     badprim:
1340         dlpi_error(q, us, d->dl_primitive, DL_BADPRIM, 0);
1341         break;
1342     }
1343     freemsg(mp);
1344 }
1345
1346 static void
1347 dlpi_error(q, us, prim, err, uerr)
1348     queue_t *q;
1349     upperstr_t *us;
1350     int prim, err, uerr;
1351 {
1352     mblk_t *reply;
1353     dl_error_ack_t *errp;
1354
1355     if (us->flags & US_DBGLOG)
1356         DPRINT3("ppp/%d: dlpi error, prim=%x, err=%x\n", us->mn, prim, err);
1357     reply = allocb(sizeof(dl_error_ack_t), BPRI_HI);
1358     if (reply == 0)
1359         return;                 /* XXX should do bufcall */
1360     reply->b_datap->db_type = M_PCPROTO;
1361     errp = (dl_error_ack_t *) reply->b_wptr;
1362     reply->b_wptr += sizeof(dl_error_ack_t);
1363     errp->dl_primitive = DL_ERROR_ACK;
1364     errp->dl_error_primitive = prim;
1365     errp->dl_errno = err;
1366     errp->dl_unix_errno = uerr;
1367     qreply(q, reply);
1368 }
1369
1370 static void
1371 dlpi_ok(q, prim)
1372     queue_t *q;
1373     int prim;
1374 {
1375     mblk_t *reply;
1376     dl_ok_ack_t *okp;
1377
1378     reply = allocb(sizeof(dl_ok_ack_t), BPRI_HI);
1379     if (reply == 0)
1380         return;                 /* XXX should do bufcall */
1381     reply->b_datap->db_type = M_PCPROTO;
1382     okp = (dl_ok_ack_t *) reply->b_wptr;
1383     reply->b_wptr += sizeof(dl_ok_ack_t);
1384     okp->dl_primitive = DL_OK_ACK;
1385     okp->dl_correct_primitive = prim;
1386     qreply(q, reply);
1387 }
1388 #endif /* NO_DLPI */
1389
1390 static int
1391 pass_packet(us, mp, outbound)
1392     upperstr_t *us;
1393     mblk_t *mp;
1394     int outbound;
1395 {
1396     int pass;
1397     upperstr_t *ppa;
1398
1399     if ((ppa = us->ppa) == 0) {
1400         freemsg(mp);
1401         return 0;
1402     }
1403
1404 #ifdef FILTER_PACKETS
1405     pass = ip_hard_filter(us, mp, outbound);
1406 #else
1407     /*
1408      * Here is where we might, in future, decide whether to pass
1409      * or drop the packet, and whether it counts as link activity.
1410      */
1411     pass = 1;
1412 #endif /* FILTER_PACKETS */
1413
1414     if (pass < 0) {
1415         /* pass only if link already up, and don't update time */
1416         if (ppa->lowerq == 0) {
1417             freemsg(mp);
1418             return 0;
1419         }
1420         pass = 1;
1421     } else if (pass) {
1422         if (outbound)
1423             ppa->last_sent = time;
1424         else
1425             ppa->last_recv = time;
1426     }
1427
1428     return pass;
1429 }
1430
1431 /*
1432  * We have some data to send down to the lower stream (or up the
1433  * control stream, if we don't have a lower stream attached).
1434  * Returns 1 if the message was dealt with, 0 if it wasn't able
1435  * to be sent on and should therefore be queued up.
1436  */
1437 static int
1438 send_data(mp, us)
1439     mblk_t *mp;
1440     upperstr_t *us;
1441 {
1442     upperstr_t *ppa;
1443
1444     if ((us->flags & US_BLOCKED) || us->npmode == NPMODE_QUEUE)
1445         return 0;
1446     ppa = us->ppa;
1447     if (ppa == 0 || us->npmode == NPMODE_DROP || us->npmode == NPMODE_ERROR) {
1448         if (us->flags & US_DBGLOG)
1449             DPRINT2("ppp/%d: dropping pkt (npmode=%d)\n", us->mn, us->npmode);
1450         freemsg(mp);
1451         return 1;
1452     }
1453     if (ppa->lowerq == 0) {
1454         /* try to send it up the control stream */
1455         if (bcanputnext(ppa->q, mp->b_band)) {
1456             /*
1457              * The message seems to get corrupted for some reason if
1458              * we just send the message up as it is, so we send a copy.
1459              */
1460             mblk_t *np = copymsg(mp);
1461             freemsg(mp);
1462             if (np != 0)
1463                 putnext(ppa->q, np);
1464             return 1;
1465         }
1466     } else {
1467         if (bcanputnext(ppa->lowerq, mp->b_band)) {
1468             MT_ENTER(&ppa->stats_lock);
1469             ppa->stats.ppp_opackets++;
1470             ppa->stats.ppp_obytes += msgdsize(mp);
1471 #ifdef INCR_OPACKETS
1472             INCR_OPACKETS(ppa);
1473 #endif
1474             MT_EXIT(&ppa->stats_lock);
1475             /*
1476              * The lower queue is only ever detached while holding an
1477              * exclusive lock on the whole driver.  So we can be confident
1478              * that the lower queue is still there.
1479              */
1480             putnext(ppa->lowerq, mp);
1481             return 1;
1482         }
1483     }
1484     us->flags |= US_BLOCKED;
1485     return 0;
1486 }
1487
1488 /*
1489  * Allocate a new PPA id and link this stream into the list of PPAs.
1490  * This procedure is called with an exclusive lock on all queues in
1491  * this driver.
1492  */
1493 static void
1494 new_ppa(q, mp)
1495     queue_t *q;
1496     mblk_t *mp;
1497 {
1498     upperstr_t *us, *up, **usp;
1499     int ppa_id;
1500
1501     us = (upperstr_t *) q->q_ptr;
1502     if (us == 0) {
1503         DPRINT("new_ppa: q_ptr = 0!\n");
1504         return;
1505     }
1506
1507     usp = &ppas;
1508     ppa_id = 0;
1509     while ((up = *usp) != 0 && ppa_id == up->ppa_id) {
1510         ++ppa_id;
1511         usp = &up->nextppa;
1512     }
1513     us->ppa_id = ppa_id;
1514     us->ppa = us;
1515     us->next = 0;
1516     us->nextppa = *usp;
1517     *usp = us;
1518     us->flags |= US_CONTROL;
1519     us->npmode = NPMODE_PASS;
1520
1521     us->mtu = PPP_MTU;
1522     us->mru = PPP_MRU;
1523
1524 #ifdef SOL2
1525     /*
1526      * Create a kstats record for our statistics, so netstat -i works.
1527      */
1528     if (us->kstats == 0) {
1529         char unit[32];
1530
1531         sprintf(unit, "ppp%d", us->ppa->ppa_id);
1532         us->kstats = kstat_create("ppp", us->ppa->ppa_id, unit,
1533                                   "net", KSTAT_TYPE_NAMED, 4, 0);
1534         if (us->kstats != 0) {
1535             kstat_named_t *kn = KSTAT_NAMED_PTR(us->kstats);
1536
1537             strcpy(kn[0].name, "ipackets");
1538             kn[0].data_type = KSTAT_DATA_ULONG;
1539             strcpy(kn[1].name, "ierrors");
1540             kn[1].data_type = KSTAT_DATA_ULONG;
1541             strcpy(kn[2].name, "opackets");
1542             kn[2].data_type = KSTAT_DATA_ULONG;
1543             strcpy(kn[3].name, "oerrors");
1544             kn[3].data_type = KSTAT_DATA_ULONG;
1545             kstat_install(us->kstats);
1546         }
1547     }
1548 #endif /* SOL2 */
1549
1550     *(int *)mp->b_cont->b_rptr = ppa_id;
1551     mp->b_datap->db_type = M_IOCACK;
1552     qreply(q, mp);
1553 }
1554
1555 static void
1556 attach_ppa(q, mp)
1557     queue_t *q;
1558     mblk_t *mp;
1559 {
1560     upperstr_t *us, *t;
1561
1562     us = (upperstr_t *) q->q_ptr;
1563     if (us == 0) {
1564         DPRINT("attach_ppa: q_ptr = 0!\n");
1565         return;
1566     }
1567
1568 #ifndef NO_DLPI
1569     us->state = DL_UNBOUND;
1570 #endif
1571     for (t = us->ppa; t->next != 0; t = t->next)
1572         ;
1573     t->next = us;
1574     us->next = 0;
1575     if (mp->b_datap->db_type == M_IOCTL) {
1576         mp->b_datap->db_type = M_IOCACK;
1577         qreply(q, mp);
1578     } else {
1579 #ifndef NO_DLPI
1580         dlpi_ok(q, DL_ATTACH_REQ);
1581 #endif
1582     }
1583 }
1584
1585 static void
1586 detach_ppa(q, mp)
1587     queue_t *q;
1588     mblk_t *mp;
1589 {
1590     upperstr_t *us, *t;
1591
1592     us = (upperstr_t *) q->q_ptr;
1593     if (us == 0) {
1594         DPRINT("detach_ppa: q_ptr = 0!\n");
1595         return;
1596     }
1597
1598     for (t = us->ppa; t->next != 0; t = t->next)
1599         if (t->next == us) {
1600             t->next = us->next;
1601             break;
1602         }
1603     us->next = 0;
1604     us->ppa = 0;
1605 #ifndef NO_DLPI
1606     us->state = DL_UNATTACHED;
1607     dlpi_ok(q, DL_DETACH_REQ);
1608 #endif
1609 }
1610
1611 /*
1612  * We call this with qwriter in order to give the upper queue procedures
1613  * the guarantee that the lower queue is not going to go away while
1614  * they are executing.
1615  */
1616 static void
1617 detach_lower(q, mp)
1618     queue_t *q;
1619     mblk_t *mp;
1620 {
1621     upperstr_t *us;
1622
1623     us = (upperstr_t *) q->q_ptr;
1624     if (us == 0) {
1625         DPRINT("detach_lower: q_ptr = 0!\n");
1626         return;
1627     }
1628
1629     LOCK_LOWER_W;
1630     us->lowerq->q_ptr = 0;
1631     RD(us->lowerq)->q_ptr = 0;
1632     us->lowerq = 0;
1633     UNLOCK_LOWER;
1634
1635     /* Unblock streams which now feed back up the control stream. */
1636     qenable(us->q);
1637
1638     mp->b_datap->db_type = M_IOCACK;
1639     qreply(q, mp);
1640 }
1641
1642 static int
1643 pppuwsrv(q)
1644     queue_t *q;
1645 {
1646     upperstr_t *us, *as;
1647     mblk_t *mp;
1648
1649     us = (upperstr_t *) q->q_ptr;
1650     if (us == 0) {
1651         DPRINT("pppuwsrv: q_ptr = 0!\n");
1652         return 0;
1653     }
1654
1655     /*
1656      * If this is a control stream, then this service procedure
1657      * probably got enabled because of flow control in the lower
1658      * stream being enabled (or because of the lower stream going
1659      * away).  Therefore we enable the service procedure of all
1660      * attached upper streams.
1661      */
1662     if (us->flags & US_CONTROL) {
1663         for (as = us->next; as != 0; as = as->next)
1664             qenable(WR(as->q));
1665     }
1666
1667     /* Try to send on any data queued here. */
1668     us->flags &= ~US_BLOCKED;
1669     while ((mp = getq(q)) != 0) {
1670         if (!send_data(mp, us)) {
1671             putbq(q, mp);
1672             break;
1673         }
1674     }
1675
1676     return 0;
1677 }
1678
1679 /* should never get called... */
1680 static int
1681 ppplwput(q, mp)
1682     queue_t *q;
1683     mblk_t *mp;
1684 {
1685     putnext(q, mp);
1686     return 0;
1687 }
1688
1689 static int
1690 ppplwsrv(q)
1691     queue_t *q;
1692 {
1693     queue_t *uq;
1694
1695     /*
1696      * Flow control has back-enabled this stream:
1697      * enable the upper write service procedure for
1698      * the upper control stream for this lower stream.
1699      */
1700     LOCK_LOWER_R;
1701     uq = (queue_t *) q->q_ptr;
1702     if (uq != 0)
1703         qenable(uq);
1704     UNLOCK_LOWER;
1705     return 0;
1706 }
1707
1708 /*
1709  * This should only get called for control streams.
1710  */
1711 static int
1712 pppurput(q, mp)
1713     queue_t *q;
1714     mblk_t *mp;
1715 {
1716     upperstr_t *ppa, *us;
1717     int proto, len;
1718     struct iocblk *iop;
1719
1720     ppa = (upperstr_t *) q->q_ptr;
1721     if (ppa == 0) {
1722         DPRINT("pppurput: q_ptr = 0!\n");
1723         return 0;
1724     }
1725
1726     switch (mp->b_datap->db_type) {
1727     case M_CTL:
1728         MT_ENTER(&ppa->stats_lock);
1729         switch (*mp->b_rptr) {
1730         case PPPCTL_IERROR:
1731 #ifdef INCR_IERRORS
1732             INCR_IERRORS(ppa);
1733 #endif
1734             ppa->stats.ppp_ierrors++;
1735             break;
1736         case PPPCTL_OERROR:
1737 #ifdef INCR_OERRORS
1738             INCR_OERRORS(ppa);
1739 #endif
1740             ppa->stats.ppp_oerrors++;
1741             break;
1742         }
1743         MT_EXIT(&ppa->stats_lock);
1744         freemsg(mp);
1745         break;
1746
1747     case M_IOCACK:
1748     case M_IOCNAK:
1749         /*
1750          * Attempt to match up the response with the stream
1751          * that the request came from.
1752          */
1753         iop = (struct iocblk *) mp->b_rptr;
1754         for (us = ppa; us != 0; us = us->next)
1755             if (us->ioc_id == iop->ioc_id)
1756                 break;
1757         if (us == 0)
1758             freemsg(mp);
1759         else
1760             putnext(us->q, mp);
1761         break;
1762
1763     case M_HANGUP:
1764         /*
1765          * The serial device has hung up.  We don't want to send
1766          * the M_HANGUP message up to pppd because that will stop
1767          * us from using the control stream any more.  Instead we
1768          * send a zero-length message as an end-of-file indication.
1769          */
1770         freemsg(mp);
1771         mp = allocb(1, BPRI_HI);
1772         if (mp == 0) {
1773             DPRINT1("ppp/%d: couldn't allocate eof message!\n", ppa->mn);
1774             break;
1775         }
1776         putnext(ppa->q, mp);
1777         break;
1778
1779     default:
1780         if (mp->b_datap->db_type == M_DATA) {
1781             len = msgdsize(mp);
1782             if (mp->b_wptr - mp->b_rptr < PPP_HDRLEN) {
1783                 PULLUP(mp, PPP_HDRLEN);
1784                 if (mp == 0) {
1785                     DPRINT1("ppp_urput: msgpullup failed (len=%d)\n", len);
1786                     break;
1787                 }
1788             }
1789             MT_ENTER(&ppa->stats_lock);
1790             ppa->stats.ppp_ipackets++;
1791             ppa->stats.ppp_ibytes += len;
1792 #ifdef INCR_IPACKETS
1793             INCR_IPACKETS(ppa);
1794 #endif
1795             MT_EXIT(&ppa->stats_lock);
1796
1797             proto = PPP_PROTOCOL(mp->b_rptr);
1798             if (proto < 0x8000 && (us = find_dest(ppa, proto)) != 0) {
1799                 /*
1800                  * A data packet for some network protocol.
1801                  * Queue it on the upper stream for that protocol.
1802                  * XXX could we just putnext it?  (would require thought)
1803                  * The rblocked flag is there to ensure that we keep
1804                  * messages in order for each network protocol.
1805                  */
1806                 if (!pass_packet(us, mp, 0))
1807                     break;
1808                 if (!us->rblocked && !canput(us->q))
1809                     us->rblocked = 1;
1810                 if (!us->rblocked)
1811                     putq(us->q, mp);
1812                 else
1813                     putq(q, mp);
1814                 break;
1815             }
1816         }
1817         /*
1818          * A control frame, a frame for an unknown protocol,
1819          * or some other message type.
1820          * Send it up to pppd via the control stream.
1821          */
1822         if (queclass(mp) == QPCTL || canputnext(ppa->q))
1823             putnext(ppa->q, mp);
1824         else
1825             putq(q, mp);
1826         break;
1827     }
1828
1829     return 0;
1830 }
1831
1832 static int
1833 pppursrv(q)
1834     queue_t *q;
1835 {
1836     upperstr_t *us, *as;
1837     mblk_t *mp, *hdr;
1838 #ifndef NO_DLPI
1839     dl_unitdata_ind_t *ud;
1840 #endif
1841     int proto;
1842
1843     us = (upperstr_t *) q->q_ptr;
1844     if (us == 0) {
1845         DPRINT("pppursrv: q_ptr = 0!\n");
1846         return 0;
1847     }
1848
1849     if (us->flags & US_CONTROL) {
1850         /*
1851          * A control stream.
1852          * If there is no lower queue attached, run the write service
1853          * routines of other upper streams attached to this PPA.
1854          */
1855         if (us->lowerq == 0) {
1856             as = us;
1857             do {
1858                 if (as->flags & US_BLOCKED)
1859                     qenable(WR(as->q));
1860                 as = as->next;
1861             } while (as != 0);
1862         }
1863
1864         /*
1865          * Messages get queued on this stream's read queue if they
1866          * can't be queued on the read queue of the attached stream
1867          * that they are destined for.  This is for flow control -
1868          * when this queue fills up, the lower read put procedure will
1869          * queue messages there and the flow control will propagate
1870          * down from there.
1871          */
1872         while ((mp = getq(q)) != 0) {
1873             proto = PPP_PROTOCOL(mp->b_rptr);
1874             if (proto < 0x8000 && (as = find_dest(us, proto)) != 0) {
1875                 if (!canput(as->q))
1876                     break;
1877                 putq(as->q, mp);
1878             } else {
1879                 if (!canputnext(q))
1880                     break;
1881                 putnext(q, mp);
1882             }
1883         }
1884         if (mp) {
1885             putbq(q, mp);
1886         } else {
1887             /* can now put stuff directly on network protocol streams again */
1888             for (as = us->next; as != 0; as = as->next)
1889                 as->rblocked = 0;
1890         }
1891
1892         /*
1893          * If this stream has a lower stream attached,
1894          * enable the read queue's service routine.
1895          * XXX we should really only do this if the queue length
1896          * has dropped below the low-water mark.
1897          */
1898         if (us->lowerq != 0)
1899             qenable(RD(us->lowerq));
1900                 
1901     } else {
1902         /*
1903          * A network protocol stream.  Put a DLPI header on each
1904          * packet and send it on.
1905          * (Actually, it seems that the IP module will happily
1906          * accept M_DATA messages without the DL_UNITDATA_IND header.)
1907          */
1908         while ((mp = getq(q)) != 0) {
1909             if (!canputnext(q)) {
1910                 putbq(q, mp);
1911                 break;
1912             }
1913 #ifndef NO_DLPI
1914             proto = PPP_PROTOCOL(mp->b_rptr);
1915             mp->b_rptr += PPP_HDRLEN;
1916             hdr = allocb(sizeof(dl_unitdata_ind_t) + 2 * sizeof(uint),
1917                          BPRI_MED);
1918             if (hdr == 0) {
1919                 /* XXX should put it back and use bufcall */
1920                 freemsg(mp);
1921                 continue;
1922             }
1923             hdr->b_datap->db_type = M_PROTO;
1924             ud = (dl_unitdata_ind_t *) hdr->b_wptr;
1925             hdr->b_wptr += sizeof(dl_unitdata_ind_t) + 2 * sizeof(uint);
1926             hdr->b_cont = mp;
1927             ud->dl_primitive = DL_UNITDATA_IND;
1928             ud->dl_dest_addr_length = sizeof(uint);
1929             ud->dl_dest_addr_offset = sizeof(dl_unitdata_ind_t);
1930             ud->dl_src_addr_length = sizeof(uint);
1931             ud->dl_src_addr_offset = ud->dl_dest_addr_offset + sizeof(uint);
1932 #if DL_CURRENT_VERSION >= 2
1933             ud->dl_group_address = 0;
1934 #endif
1935             /* Send the DLPI client the data with the SAP they requested,
1936                (e.g. ETHERTYPE_IP) rather than the PPP protocol number
1937                (e.g. PPP_IP) */
1938             ((uint *)(ud + 1))[0] = us->req_sap;        /* dest SAP */
1939             ((uint *)(ud + 1))[1] = us->req_sap;        /* src SAP */
1940             putnext(q, hdr);
1941 #else /* NO_DLPI */
1942             putnext(q, mp);
1943 #endif /* NO_DLPI */
1944         }
1945         /*
1946          * Now that we have consumed some packets from this queue,
1947          * enable the control stream's read service routine so that we
1948          * can process any packets for us that might have got queued
1949          * there for flow control reasons.
1950          */
1951         if (us->ppa)
1952             qenable(us->ppa->q);
1953     }
1954
1955     return 0;
1956 }
1957
1958 static upperstr_t *
1959 find_dest(ppa, proto)
1960     upperstr_t *ppa;
1961     int proto;
1962 {
1963     upperstr_t *us;
1964
1965     for (us = ppa->next; us != 0; us = us->next)
1966         if (proto == us->sap)
1967             break;
1968     return us;
1969 }
1970
1971 /*
1972  * We simply put the message on to the associated upper control stream
1973  * (either here or in ppplrsrv).  That way we enter the perimeters
1974  * before looking through the list of attached streams to decide which
1975  * stream it should go up.
1976  */
1977 static int
1978 ppplrput(q, mp)
1979     queue_t *q;
1980     mblk_t *mp;
1981 {
1982     queue_t *uq;
1983     struct iocblk *iop;
1984
1985     switch (mp->b_datap->db_type) {
1986     case M_IOCTL:
1987         iop = (struct iocblk *) mp->b_rptr;
1988         iop->ioc_error = EINVAL;
1989         mp->b_datap->db_type = M_IOCNAK;
1990         qreply(q, mp);
1991         return 0;
1992     case M_FLUSH:
1993         if (*mp->b_rptr & FLUSHR)
1994             flushq(q, FLUSHDATA);
1995         if (*mp->b_rptr & FLUSHW) {
1996             *mp->b_rptr &= ~FLUSHR;
1997             qreply(q, mp);
1998         } else
1999             freemsg(mp);
2000         return 0;
2001     }
2002
2003     /*
2004      * If we can't get the lower lock straight away, queue this one
2005      * rather than blocking, to avoid the possibility of deadlock.
2006      */
2007     if (!TRYLOCK_LOWER_R) {
2008         putq(q, mp);
2009         return 0;
2010     }
2011
2012     /*
2013      * Check that we're still connected to the driver.
2014      */
2015     uq = (queue_t *) q->q_ptr;
2016     if (uq == 0) {
2017         UNLOCK_LOWER;
2018         DPRINT1("ppplrput: q = %x, uq = 0??\n", q);
2019         freemsg(mp);
2020         return 0;
2021     }
2022
2023     /*
2024      * Try to forward the message to the put routine for the upper
2025      * control stream for this lower stream.
2026      * If there are already messages queued here, queue this one so
2027      * they don't get out of order.
2028      */
2029     if (queclass(mp) == QPCTL || (qsize(q) == 0 && canput(uq)))
2030         put(uq, mp);
2031     else
2032         putq(q, mp);
2033
2034     UNLOCK_LOWER;
2035     return 0;
2036 }
2037
2038 static int
2039 ppplrsrv(q)
2040     queue_t *q;
2041 {
2042     mblk_t *mp;
2043     queue_t *uq;
2044
2045     /*
2046      * Packets get queued here for flow control reasons
2047      * or if the lrput routine couldn't get the lower lock
2048      * without blocking.
2049      */
2050     LOCK_LOWER_R;
2051     uq = (queue_t *) q->q_ptr;
2052     if (uq == 0) {
2053         UNLOCK_LOWER;
2054         flushq(q, FLUSHALL);
2055         DPRINT1("ppplrsrv: q = %x, uq = 0??\n", q);
2056         return 0;
2057     }
2058     while ((mp = getq(q)) != 0) {
2059         if (queclass(mp) == QPCTL || canput(uq))
2060             put(uq, mp);
2061         else {
2062             putbq(q, mp);
2063             break;
2064         }
2065     }
2066     UNLOCK_LOWER;
2067     return 0;
2068 }
2069
2070 static int
2071 putctl2(q, type, code, val)
2072     queue_t *q;
2073     int type, code, val;
2074 {
2075     mblk_t *mp;
2076
2077     mp = allocb(2, BPRI_HI);
2078     if (mp == 0)
2079         return 0;
2080     mp->b_datap->db_type = type;
2081     mp->b_wptr[0] = code;
2082     mp->b_wptr[1] = val;
2083     mp->b_wptr += 2;
2084     putnext(q, mp);
2085     return 1;
2086 }
2087
2088 static int
2089 putctl4(q, type, code, val)
2090     queue_t *q;
2091     int type, code, val;
2092 {
2093     mblk_t *mp;
2094
2095     mp = allocb(4, BPRI_HI);
2096     if (mp == 0)
2097         return 0;
2098     mp->b_datap->db_type = type;
2099     mp->b_wptr[0] = code;
2100     ((short *)mp->b_wptr)[1] = val;
2101     mp->b_wptr += 4;
2102     putnext(q, mp);
2103     return 1;
2104 }
2105
2106 static void
2107 debug_dump(q, mp)
2108     queue_t *q;
2109     mblk_t *mp;
2110 {
2111     upperstr_t *us;
2112     queue_t *uq, *lq;
2113
2114     DPRINT("ppp upper streams:\n");
2115     for (us = minor_devs; us != 0; us = us->nextmn) {
2116         uq = us->q;
2117         DPRINT3(" %d: q=%x rlev=%d",
2118                 us->mn, uq, (uq? qsize(uq): 0));
2119         DPRINT3(" wlev=%d flags=0x%b", (uq? qsize(WR(uq)): 0),
2120                 us->flags, "\020\1priv\2control\3blocked\4last");
2121         DPRINT3(" state=%x sap=%x req_sap=%x", us->state, us->sap,
2122                 us->req_sap);
2123         if (us->ppa == 0)
2124             DPRINT(" ppa=?\n");
2125         else
2126             DPRINT1(" ppa=%d\n", us->ppa->ppa_id);
2127         if (us->flags & US_CONTROL) {
2128             lq = us->lowerq;
2129             DPRINT3("    control for %d lq=%x rlev=%d",
2130                     us->ppa_id, lq, (lq? qsize(RD(lq)): 0));
2131             DPRINT3(" wlev=%d mru=%d mtu=%d\n",
2132                     (lq? qsize(lq): 0), us->mru, us->mtu);
2133         }
2134     }
2135     mp->b_datap->db_type = M_IOCACK;
2136     qreply(q, mp);
2137 }
2138
2139 #ifdef FILTER_PACKETS
2140 #include <netinet/in_systm.h>
2141 #include <netinet/ip.h>
2142 #include <netinet/udp.h>
2143 #include <netinet/tcp.h>
2144
2145 #define MAX_IPHDR    128     /* max TCP/IP header size */
2146
2147
2148 /* The following table contains a hard-coded list of protocol/port pairs.
2149  * Any matching packets are either discarded unconditionally, or, 
2150  * if ok_if_link_up is non-zero when a connection does not currently exist
2151  * (i.e., they go through if the connection is present, but never initiate
2152  * a dial-out).
2153  * This idea came from a post by dm@garage.uun.org (David Mazieres)
2154  */
2155 static struct pktfilt_tab { 
2156         int proto; 
2157         u_short port; 
2158         u_short ok_if_link_up; 
2159 } pktfilt_tab[] = {
2160         { IPPROTO_UDP,  520,    1 },    /* RIP, ok to pass if link is up */
2161         { IPPROTO_UDP,  123,    1 },    /* NTP, don't keep up the link for it */
2162         { -1,           0,      0 }     /* terminator entry has port == -1 */
2163 };
2164
2165
2166 static int
2167 ip_hard_filter(us, mp, outbound)
2168     upperstr_t *us;
2169     mblk_t *mp;
2170     int outbound;
2171 {
2172     struct ip *ip;
2173     struct pktfilt_tab *pft;
2174     mblk_t *temp_mp;
2175     int proto;
2176     int len, hlen;
2177
2178
2179     /* Note, the PPP header has already been pulled up in all cases */
2180     proto = PPP_PROTOCOL(mp->b_rptr);
2181     if (us->flags & US_DBGLOG)
2182         DPRINT3("ppp/%d: filter, proto=0x%x, out=%d\n", us->mn, proto, outbound);
2183
2184     switch (proto)
2185     {
2186     case PPP_IP:
2187         if ((mp->b_wptr - mp->b_rptr) == PPP_HDRLEN && mp->b_cont != 0) {
2188             temp_mp = mp->b_cont;
2189             len = msgdsize(temp_mp);
2190             hlen = (len < MAX_IPHDR) ? len : MAX_IPHDR;
2191             PULLUP(temp_mp, hlen);
2192             if (temp_mp == 0) {
2193                 DPRINT2("ppp/%d: filter, pullup next failed, len=%d\n", 
2194                         us->mn, hlen);
2195                 mp->b_cont = 0;         /* PULLUP() freed the rest */
2196                 freemsg(mp);
2197                 return 0;
2198             }
2199             ip = (struct ip *)mp->b_cont->b_rptr;
2200         }
2201         else {
2202             len = msgdsize(mp);
2203             hlen = (len < (PPP_HDRLEN+MAX_IPHDR)) ? len : (PPP_HDRLEN+MAX_IPHDR);
2204             PULLUP(mp, hlen);
2205             if (mp == 0) {
2206                 DPRINT2("ppp/%d: filter, pullup failed, len=%d\n", 
2207                         us->mn, hlen);
2208                 return 0;
2209             }
2210             ip = (struct ip *)(mp->b_rptr + PPP_HDRLEN);
2211         }
2212
2213         /* For IP traffic, certain packets (e.g., RIP) may be either
2214          *   1.  ignored - dropped completely
2215          *   2.  will not initiate a connection, but
2216          *       will be passed if a connection is currently up.
2217          */
2218         for (pft=pktfilt_tab; pft->proto != -1; pft++) {
2219             if (ip->ip_p == pft->proto) {
2220                 switch(pft->proto) {
2221                 case IPPROTO_UDP:
2222                     if (((struct udphdr *) &((int *)ip)[ip->ip_hl])->uh_dport
2223                                 == htons(pft->port)) goto endfor;
2224                     break;
2225                 case IPPROTO_TCP:
2226                     if (((struct tcphdr *) &((int *)ip)[ip->ip_hl])->th_dport
2227                                 == htons(pft->port)) goto endfor;
2228                     break;
2229                 }       
2230             }
2231         }
2232         endfor:
2233         if (pft->proto != -1) {
2234             if (us->flags & US_DBGLOG)
2235                 DPRINT3("ppp/%d: found IP pkt, proto=0x%x (%d)\n", 
2236                                 us->mn, pft->proto, pft->port);
2237             /* Discard if not connected, or if not pass_with_link_up */
2238             /* else, if link is up let go by, but don't update time */
2239             return pft->ok_if_link_up? -1: 0;
2240         }
2241         break;
2242     } /* end switch (proto) */
2243
2244     return 1;
2245 }
2246 #endif /* FILTER_PACKETS */
2247