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