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