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