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