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