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