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