]> git.ozlabs.org Git - ppp.git/blob - modules/ppp.c
use uint instead of ulong, in case longs are 8 bytes
[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.15 1998/05/04 06:11:35 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 == 0) {
395         DPRINT("pppclose: q_ptr = 0\n");
396         return 0;
397     }
398     if (up->flags & US_DBGLOG)
399         DPRINT2("ppp/%d: close, flags=%x\n", up->mn, up->flags);
400     if (up->flags & US_CONTROL) {
401 #ifdef LACHTCP
402         struct ifstats *ifp, *pifp;
403 #endif
404         /*
405          * This stream represents a PPA:
406          * For all streams attached to the PPA, clear their
407          * references to this PPA.
408          * Then remove this PPA from the list of PPAs.
409          */
410         for (as = up->next; as != 0; as = asnext) {
411             asnext = as->next;
412             as->next = 0;
413             as->ppa = 0;
414             if (as->flags & US_BLOCKED) {
415                 as->flags &= ~US_BLOCKED;
416                 flushq(WR(as->q), FLUSHDATA);
417             }
418         }
419         for (upp = &ppas; *upp != 0; upp = &(*upp)->nextppa)
420             if (*upp == up) {
421                 *upp = up->nextppa;
422                 break;
423             }
424 #ifdef LACHTCP
425         /* Remove the statistics from the active list.  */
426         for (ifp = ifstats, pifp = 0; ifp; ifp = ifp->ifs_next) {
427             if (ifp == &up->ifstats) {
428                 if (pifp)
429                     pifp->ifs_next = ifp->ifs_next;
430                 else
431                     ifstats = ifp->ifs_next;
432                 break;
433             }
434             pifp = ifp;
435         }
436 #endif
437     } else {
438         /*
439          * If this stream is attached to a PPA,
440          * remove it from the PPA's list.
441          */
442         if ((as = up->ppa) != 0) {
443             for (; as->next != 0; as = as->next)
444                 if (as->next == up) {
445                     as->next = up->next;
446                     break;
447                 }
448         }
449     }
450
451 #ifdef SOL2
452     if (up->kstats)
453         kstat_delete(up->kstats);
454 #endif
455
456     q->q_ptr = NULL;
457     WR(q)->q_ptr = NULL;
458
459     for (prevp = &minor_devs; *prevp != 0; prevp = &(*prevp)->nextmn) {
460         if (*prevp == up) {
461             *prevp = up->nextmn;
462             break;
463         }
464     }
465     FREE(up, sizeof(upperstr_t));
466     --ppp_count;
467
468     return 0;
469 }
470
471 /*
472  * A message from on high.  We do one of three things:
473  *      - qreply()
474  *      - put the message on the lower write stream
475  *      - queue it for our service routine
476  */
477 static int
478 pppuwput(q, mp)
479     queue_t *q;
480     mblk_t *mp;
481 {
482     upperstr_t *us, *usnext, *ppa, *os, *nps;
483     struct iocblk *iop;
484     struct linkblk *lb;
485 #ifdef LACHTCP
486     struct ifreq *ifr;
487     int i;
488 #endif
489     queue_t *lq;
490     int error, n, sap;
491     mblk_t *mq;
492     struct ppp_idle *pip;
493     int len;
494 #ifdef PRIOQ
495     queue_t *tlq;
496 #endif  /* PRIOQ */
497
498     us = (upperstr_t *) q->q_ptr;
499     if (us == 0) {
500         DPRINT("pppuwput: q_ptr = 0!\n");
501         return 0;
502     }
503     if (mp == 0) {
504         DPRINT1("pppuwput/%d: mp = 0!\n", us->mn);
505         return 0;
506     }
507     if (mp->b_datap == 0) {
508         DPRINT1("pppuwput/%d: mp->b_datap = 0!\n", us->mn);
509         return 0;
510     }
511     switch (mp->b_datap->db_type) {
512 #ifndef NO_DLPI
513     case M_PCPROTO:
514     case M_PROTO:
515         dlpi_request(q, mp, us);
516         break;
517 #endif /* NO_DLPI */
518
519     case M_DATA:
520         if (us->flags & US_DBGLOG)
521             DPRINT3("ppp/%d: uwput M_DATA len=%d flags=%x\n",
522                     us->mn, msgdsize(mp), us->flags);
523         if (us->ppa == 0 || msgdsize(mp) > us->ppa->mtu + PPP_HDRLEN
524 #ifndef NO_DLPI
525             || (us->flags & US_CONTROL) == 0
526 #endif /* NO_DLPI */
527             ) {
528             DPRINT1("pppuwput: junk data len=%d\n", msgdsize(mp));
529             freemsg(mp);
530             break;
531         }
532 #ifdef NO_DLPI
533         if ((us->flags & US_CONTROL) == 0 && !pass_packet(us, mp, 1))
534             break;
535 #endif
536         if (!send_data(mp, us))
537             putq(q, mp);
538         break;
539
540     case M_IOCTL:
541         iop = (struct iocblk *) mp->b_rptr;
542         error = EINVAL;
543         if (us->flags & US_DBGLOG)
544             DPRINT3("ppp/%d: ioctl %x count=%d\n",
545                     us->mn, iop->ioc_cmd, iop->ioc_count);
546         switch (iop->ioc_cmd) {
547         case I_LINK:
548             if ((us->flags & US_CONTROL) == 0 || us->lowerq != 0)
549                 break;
550             if (mp->b_cont == 0) {
551                 DPRINT1("pppuwput/%d: ioctl I_LINK b_cont = 0!\n", us->mn);
552                 break;
553             }
554             lb = (struct linkblk *) mp->b_cont->b_rptr;
555             us->lowerq = lq = lb->l_qbot;
556             if (lq == 0) {
557                 DPRINT1("pppuwput/%d: ioctl I_LINK l_qbot = 0!\n", us->mn);
558                 break;
559             }
560             lq->q_ptr = (caddr_t) us;
561             RD(lq)->q_ptr = (caddr_t) us;
562             noenable(RD(lq));
563             flushq(RD(lq), FLUSHALL);
564             iop->ioc_count = 0;
565             error = 0;
566             us->flags &= ~US_LASTMOD;
567             /* Unblock upper streams which now feed this lower stream. */
568             qenable(lq);
569             /* Send useful information down to the modules which
570                are now linked below us. */
571             putctl2(lq, M_CTL, PPPCTL_UNIT, us->ppa_id);
572             putctl4(lq, M_CTL, PPPCTL_MRU, us->mru);
573             putctl4(lq, M_CTL, PPPCTL_MTU, us->mtu);
574 #ifdef PRIOQ
575             /* Lower tty driver's queue hiwat/lowat from default 4096/128
576                to 256/128 since we don't want queueing of data on
577                output to physical device */
578
579             freezestr(lq);
580             for (tlq = lq; tlq->q_next != NULL; tlq = tlq->q_next)
581                 ;
582             strqset(tlq, QHIWAT, 0, 256);
583             strqset(tlq, QLOWAT, 0, 128);
584             unfreezestr(lq);
585 #endif  /* PRIOQ */
586             break;
587
588         case I_UNLINK:
589             if (mp->b_cont == 0) {
590                 DPRINT1("pppuwput/%d: ioctl I_UNLINK b_cont = 0!\n", us->mn);
591                 break;
592             }
593             lb = (struct linkblk *) mp->b_cont->b_rptr;
594 #if DEBUG
595             if (us->lowerq != lb->l_qbot) {
596                 DPRINT2("ppp unlink: lowerq=%x qbot=%x\n",
597                         us->lowerq, lb->l_qbot);
598                 break;
599             }
600 #endif
601             us->lowerq = 0;
602             iop->ioc_count = 0;
603             error = 0;
604             /* Unblock streams which now feed back up the control stream. */
605             qenable(us->q);
606             break;
607
608         case PPPIO_NEWPPA:
609             if (us->flags & US_CONTROL)
610                 break;
611             if ((us->flags & US_PRIV) == 0) {
612                 error = EPERM;
613                 break;
614             }
615             /* Arrange to return an int */
616             if ((mq = mp->b_cont) == 0
617                 || mq->b_datap->db_lim - mq->b_rptr < sizeof(int)) {
618                 mq = allocb(sizeof(int), BPRI_HI);
619                 if (mq == 0) {
620                     error = ENOSR;
621                     break;
622                 }
623                 if (mp->b_cont != 0)
624                     freemsg(mp->b_cont);
625                 mp->b_cont = mq;
626                 mq->b_cont = 0;
627             }
628             iop->ioc_count = sizeof(int);
629             mq->b_wptr = mq->b_rptr + sizeof(int);
630             qwriter(q, mp, new_ppa, PERIM_OUTER);
631             error = -1;
632             break;
633
634         case PPPIO_ATTACH:
635             /* like dlpi_attach, for programs which can't write to
636                the stream (like pppstats) */
637             if (iop->ioc_count != sizeof(int) || us->ppa != 0)
638                 break;
639             if (mp->b_cont == 0) {
640                 DPRINT1("pppuwput/%d: ioctl PPPIO_ATTACH b_cont = 0!\n", us->mn);
641                 break;
642             }
643             n = *(int *)mp->b_cont->b_rptr;
644             for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
645                 if (ppa->ppa_id == n)
646                     break;
647             if (ppa == 0)
648                 break;
649             us->ppa = ppa;
650             iop->ioc_count = 0;
651             qwriter(q, mp, attach_ppa, PERIM_OUTER);
652             error = -1;
653             break;
654
655 #ifdef NO_DLPI
656         case PPPIO_BIND:
657             /* Attach to a given SAP. */
658             if (iop->ioc_count != sizeof(int) || us->ppa == 0)
659                 break;
660             if (mp->b_cont == 0) {
661                 DPRINT1("pppuwput/%d: ioctl PPPIO_BIND b_cont = 0!\n", us->mn);
662                 break;
663             }
664             n = *(int *)mp->b_cont->b_rptr;
665             /* n must be a valid PPP network protocol number. */
666             if (n < 0x21 || n > 0x3fff || (n & 0x101) != 1)
667                 break;
668             /* check that no other stream is bound to this sap already. */
669             for (os = us->ppa; os != 0; os = os->next)
670                 if (os->sap == n)
671                     break;
672             if (os != 0)
673                 break;
674             us->sap = n;
675             iop->ioc_count = 0;
676             error = 0;
677             break;
678 #endif /* NO_DLPI */
679
680         case PPPIO_MRU:
681             if (iop->ioc_count != sizeof(int) || (us->flags & US_CONTROL) == 0)
682                 break;
683             if (mp->b_cont == 0) {
684                 DPRINT1("pppuwput/%d: ioctl PPPIO_MRU b_cont = 0!\n", us->mn);
685                 break;
686             }
687             n = *(int *)mp->b_cont->b_rptr;
688             if (n <= 0 || n > PPP_MAXMRU)
689                 break;
690             if (n < PPP_MRU)
691                 n = PPP_MRU;
692             us->mru = n;
693             if (us->lowerq)
694                 putctl4(us->lowerq, M_CTL, PPPCTL_MRU, n);
695             error = 0;
696             iop->ioc_count = 0;
697             break;
698
699         case PPPIO_MTU:
700             if (iop->ioc_count != sizeof(int) || (us->flags & US_CONTROL) == 0)
701                 break;
702             if (mp->b_cont == 0) {
703                 DPRINT1("pppuwput/%d: ioctl PPPIO_MTU b_cont = 0!\n", us->mn);
704                 break;
705             }
706             n = *(int *)mp->b_cont->b_rptr;
707             if (n <= 0 || n > PPP_MAXMTU)
708                 break;
709             us->mtu = n;
710 #ifdef LACHTCP
711             /* The MTU reported in netstat, not used as IP max packet size! */
712             us->ifstats.ifs_mtu = n;
713 #endif
714             if (us->lowerq)
715                 putctl4(us->lowerq, M_CTL, PPPCTL_MTU, n);
716             error = 0;
717             iop->ioc_count = 0;
718             break;
719
720         case PPPIO_LASTMOD:
721             us->flags |= US_LASTMOD;
722             error = 0;
723             break;
724
725         case PPPIO_DEBUG:
726             if (iop->ioc_count != sizeof(int))
727                 break;
728             if (mp->b_cont == 0) {
729                 DPRINT1("pppuwput/%d: ioctl PPPIO_DEBUG b_cont = 0!\n", us->mn);
730                 break;
731             }
732             n = *(int *)mp->b_cont->b_rptr;
733             if (n == PPPDBG_DUMP + PPPDBG_DRIVER) {
734                 qwriter(q, NULL, debug_dump, PERIM_OUTER);
735                 iop->ioc_count = 0;
736                 error = 0;
737             } else if (n == PPPDBG_LOG + PPPDBG_DRIVER) {
738                 DPRINT1("ppp/%d: debug log enabled\n", us->mn);
739                 us->flags |= US_DBGLOG;
740                 iop->ioc_count = 0;
741                 error = 0;
742             } else {
743                 if (us->ppa == 0 || us->ppa->lowerq == 0)
744                     break;
745                 putnext(us->ppa->lowerq, mp);
746                 error = -1;
747             }
748             break;
749
750         case PPPIO_NPMODE:
751             if (iop->ioc_count != 2 * sizeof(int))
752                 break;
753             if ((us->flags & US_CONTROL) == 0)
754                 break;
755             if (mp->b_cont == 0) {
756                 DPRINT1("pppuwput/%d: ioctl PPPIO_NPMODE b_cont = 0!\n", us->mn);
757                 break;
758             }
759             sap = ((int *)mp->b_cont->b_rptr)[0];
760             for (nps = us->next; nps != 0; nps = nps->next)
761                 if (nps->sap == sap)
762                     break;
763             if (nps == 0) {
764                 if (us->flags & US_DBGLOG)
765                     DPRINT2("ppp/%d: no stream for sap %x\n", us->mn, sap);
766                 break;
767             }
768             nps->npmode = (enum NPmode) ((int *)mp->b_cont->b_rptr)[1];
769             if (nps->npmode == NPMODE_DROP || nps->npmode == NPMODE_ERROR)
770                 flushq(WR(nps->q), FLUSHDATA);
771             else if (nps->npmode == NPMODE_PASS && qsize(WR(nps->q)) > 0
772                      && (nps->flags & US_BLOCKED) == 0)
773                 qenable(WR(nps->q));
774             iop->ioc_count = 0;
775             error = 0;
776             break;
777
778         case PPPIO_GIDLE:
779             if ((ppa = us->ppa) == 0)
780                 break;
781             mq = allocb(sizeof(struct ppp_idle), BPRI_HI);
782             if (mq == 0) {
783                 error = ENOSR;
784                 break;
785             }
786             if (mp->b_cont != 0)
787                 freemsg(mp->b_cont);
788             mp->b_cont = mq;
789             mq->b_cont = 0;
790             pip = (struct ppp_idle *) mq->b_wptr;
791             pip->xmit_idle = time - ppa->last_sent;
792             pip->recv_idle = time - ppa->last_recv;
793             mq->b_wptr += sizeof(struct ppp_idle);
794             iop->ioc_count = sizeof(struct ppp_idle);
795             error = 0;
796             break;
797
798 #ifdef LACHTCP
799         case SIOCSIFNAME:
800             /* Sent from IP down to us.  Attach the ifstats structure.  */
801             if (iop->ioc_count != sizeof(struct ifreq) || us->ppa == 0)
802                 break;
803             ifr = (struct ifreq *)mp->b_cont->b_rptr;
804             /* Find the unit number in the interface name.  */
805             for (i = 0; i < IFNAMSIZ; i++) {
806                 if (ifr->ifr_name[i] == 0 ||
807                     (ifr->ifr_name[i] >= '0' &&
808                      ifr->ifr_name[i] <= '9'))
809                     break;
810                 else
811                     us->ifname[i] = ifr->ifr_name[i];
812             }
813             us->ifname[i] = 0;
814
815             /* Convert the unit number to binary.  */
816             for (n = 0; i < IFNAMSIZ; i++) {
817                 if (ifr->ifr_name[i] == 0) {
818                     break;
819                 }
820                 else {
821                     n = n * 10 + ifr->ifr_name[i] - '0';
822                 }
823             }
824
825             /* Verify the ppa.  */
826             if (us->ppa->ppa_id != n)
827                 break;
828             ppa = us->ppa;
829
830             /* Set up the netstat block.  */
831             strncpy (ppa->ifname, us->ifname, IFNAMSIZ);
832
833             ppa->ifstats.ifs_name = ppa->ifname;
834             ppa->ifstats.ifs_unit = n;
835             ppa->ifstats.ifs_active = us->state != DL_UNBOUND;
836             ppa->ifstats.ifs_mtu = ppa->mtu;
837
838             /* Link in statistics used by netstat.  */
839             ppa->ifstats.ifs_next = ifstats;
840             ifstats = &ppa->ifstats;
841
842             iop->ioc_count = 0;
843             error = 0;
844             break;
845
846         case SIOCGIFFLAGS:
847             if (!(us->flags & US_CONTROL)) {
848                 if (us->ppa)
849                     us = us->ppa;
850                 else
851                     break;
852             }
853             ((struct iocblk_in *)iop)->ioc_ifflags = us->ifflags;
854             error = 0;
855             break;
856
857         case SIOCSIFFLAGS:
858             if (!(us->flags & US_CONTROL)) {
859                 if (us->ppa)
860                     us = us->ppa;
861                 else
862                     break;
863             }
864             us->ifflags = ((struct iocblk_in *)iop)->ioc_ifflags;
865             error = 0;
866             break;
867
868         case SIOCSIFADDR:
869             if (!(us->flags & US_CONTROL)) {
870                 if (us->ppa)
871                     us = us->ppa;
872                 else
873                     break;
874             }
875             us->ifflags |= IFF_RUNNING;
876             ((struct iocblk_in *)iop)->ioc_ifflags |= IFF_RUNNING;
877             error = 0;
878             break;
879
880         case SIOCSIFMTU:
881             /*
882              * Vanilla SVR4 systems don't handle SIOCSIFMTU, rather
883              * they take the MTU from the DL_INFO_ACK we sent in response
884              * to their DL_INFO_REQ.  Fortunately, they will update the
885              * MTU if we send an unsolicited DL_INFO_ACK up.
886              */
887             if ((mq = allocb(sizeof(dl_info_req_t), BPRI_HI)) == 0)
888                 break;          /* should do bufcall */
889             ((union DL_primitives *)mq->b_rptr)->dl_primitive = DL_INFO_REQ;
890             mq->b_wptr = mq->b_rptr + sizeof(dl_info_req_t);
891             dlpi_request(q, mq, us);
892             error = 0;
893             break;
894
895         case SIOCGIFNETMASK:
896         case SIOCSIFNETMASK:
897         case SIOCGIFADDR:
898         case SIOCGIFDSTADDR:
899         case SIOCSIFDSTADDR:
900         case SIOCGIFMETRIC:
901             error = 0;
902             break;
903 #endif /* LACHTCP */
904
905         default:
906             if (us->ppa == 0 || us->ppa->lowerq == 0)
907                 break;
908             us->ioc_id = iop->ioc_id;
909             error = -1;
910             switch (iop->ioc_cmd) {
911             case PPPIO_GETSTAT:
912             case PPPIO_GETCSTAT:
913                 if (us->flags & US_LASTMOD) {
914                     error = EINVAL;
915                     break;
916                 }
917                 putnext(us->ppa->lowerq, mp);
918                 break;
919             default:
920                 if (us->flags & US_PRIV)
921                     putnext(us->ppa->lowerq, mp);
922                 else {
923                     DPRINT1("ppp ioctl %x rejected\n", iop->ioc_cmd);
924                     error = EPERM;
925                 }
926                 break;
927             }
928             break;
929         }
930
931         if (error > 0) {
932             iop->ioc_error = error;
933             mp->b_datap->db_type = M_IOCNAK;
934             qreply(q, mp);
935         } else if (error == 0) {
936             mp->b_datap->db_type = M_IOCACK;
937             qreply(q, mp);
938         }
939         break;
940
941     case M_FLUSH:
942         if (us->flags & US_DBGLOG)
943             DPRINT2("ppp/%d: flush %x\n", us->mn, *mp->b_rptr);
944         if (*mp->b_rptr & FLUSHW)
945             flushq(q, FLUSHDATA);
946         if (*mp->b_rptr & FLUSHR) {
947             *mp->b_rptr &= ~FLUSHW;
948             qreply(q, mp);
949         } else
950             freemsg(mp);
951         break;
952
953     default:
954         freemsg(mp);
955         break;
956     }
957     return 0;
958 }
959
960 #ifndef NO_DLPI
961 static void
962 dlpi_request(q, mp, us)
963     queue_t *q;
964     mblk_t *mp;
965     upperstr_t *us;
966 {
967     union DL_primitives *d = (union DL_primitives *) mp->b_rptr;
968     int size = mp->b_wptr - mp->b_rptr;
969     mblk_t *reply, *np;
970     upperstr_t *ppa, *os;
971     int sap, *ip, len;
972     dl_info_ack_t *info;
973     dl_bind_ack_t *ackp;
974
975     if (us->flags & US_DBGLOG)
976         DPRINT3("ppp/%d: dlpi prim %x len=%d\n", us->mn,
977                 d->dl_primitive, size);
978     switch (d->dl_primitive) {
979     case DL_INFO_REQ:
980         if (size < sizeof(dl_info_req_t))
981             goto badprim;
982         if ((reply = allocb(sizeof(dl_info_ack_t), BPRI_HI)) == 0)
983             break;              /* should do bufcall */
984         reply->b_datap->db_type = M_PCPROTO;
985         info = (dl_info_ack_t *) reply->b_wptr;
986         reply->b_wptr += sizeof(dl_info_ack_t);
987         bzero((caddr_t) info, sizeof(dl_info_ack_t));
988         info->dl_primitive = DL_INFO_ACK;
989         info->dl_max_sdu = us->ppa? us->ppa->mtu: PPP_MAXMTU;
990         info->dl_min_sdu = 1;
991         info->dl_addr_length = sizeof(uint);
992 #ifdef DL_OTHER
993         info->dl_mac_type = DL_OTHER;
994 #else
995         info->dl_mac_type = DL_HDLC;    /* a lie */
996 #endif
997         info->dl_current_state = us->state;
998         info->dl_service_mode = DL_CLDLS;
999         info->dl_provider_style = DL_STYLE2;
1000 #if DL_CURRENT_VERSION >= 2
1001         info->dl_sap_length = sizeof(uint);
1002         info->dl_version = DL_CURRENT_VERSION;
1003 #endif
1004         qreply(q, reply);
1005         break;
1006
1007     case DL_ATTACH_REQ:
1008         if (size < sizeof(dl_attach_req_t))
1009             goto badprim;
1010         if (us->state != DL_UNATTACHED || us->ppa != 0) {
1011             dlpi_error(q, us, DL_ATTACH_REQ, DL_OUTSTATE, 0);
1012             break;
1013         }
1014         for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
1015             if (ppa->ppa_id == d->attach_req.dl_ppa)
1016                 break;
1017         if (ppa == 0) {
1018             dlpi_error(q, us, DL_ATTACH_REQ, DL_BADPPA, 0);
1019             break;
1020         }
1021         us->ppa = ppa;
1022         qwriter(q, mp, attach_ppa, PERIM_OUTER);
1023         break;
1024
1025     case DL_DETACH_REQ:
1026         if (size < sizeof(dl_detach_req_t))
1027             goto badprim;
1028         if (us->state != DL_UNBOUND || us->ppa == 0) {
1029             dlpi_error(q, us, DL_DETACH_REQ, DL_OUTSTATE, 0);
1030             break;
1031         }
1032         qwriter(q, mp, detach_ppa, PERIM_OUTER);
1033         break;
1034
1035     case DL_BIND_REQ:
1036         if (size < sizeof(dl_bind_req_t))
1037             goto badprim;
1038         if (us->state != DL_UNBOUND || us->ppa == 0) {
1039             dlpi_error(q, us, DL_BIND_REQ, DL_OUTSTATE, 0);
1040             break;
1041         }
1042 #if 0
1043         /* apparently this test fails (unnecessarily?) on some systems */
1044         if (d->bind_req.dl_service_mode != DL_CLDLS) {
1045             dlpi_error(q, us, DL_BIND_REQ, DL_UNSUPPORTED, 0);
1046             break;
1047         }
1048 #endif
1049
1050         /* saps must be valid PPP network protocol numbers,
1051            except that we accept ETHERTYPE_IP in place of PPP_IP. */
1052         sap = d->bind_req.dl_sap;
1053         us->req_sap = sap;
1054         if (sap == ETHERTYPE_IP)
1055             sap = PPP_IP;
1056         if (sap < 0x21 || sap > 0x3fff || (sap & 0x101) != 1) {
1057             dlpi_error(q, us, DL_BIND_REQ, DL_BADADDR, 0);
1058             break;
1059         }
1060
1061         /* check that no other stream is bound to this sap already. */
1062         for (os = us->ppa; os != 0; os = os->next)
1063             if (os->sap == sap)
1064                 break;
1065         if (os != 0) {
1066             dlpi_error(q, us, DL_BIND_REQ, DL_NOADDR, 0);
1067             break;
1068         }
1069
1070         us->sap = sap;
1071         us->state = DL_IDLE;
1072
1073         if ((reply = allocb(sizeof(dl_bind_ack_t) + sizeof(uint),
1074                             BPRI_HI)) == 0)
1075             break;              /* should do bufcall */
1076         ackp = (dl_bind_ack_t *) reply->b_wptr;
1077         reply->b_wptr += sizeof(dl_bind_ack_t) + sizeof(uint);
1078         reply->b_datap->db_type = M_PCPROTO;
1079         bzero((caddr_t) ackp, sizeof(dl_bind_ack_t));
1080         ackp->dl_primitive = DL_BIND_ACK;
1081         ackp->dl_sap = sap;
1082         ackp->dl_addr_length = sizeof(uint);
1083         ackp->dl_addr_offset = sizeof(dl_bind_ack_t);
1084         *(uint *)(ackp+1) = sap;
1085         qreply(q, reply);
1086         break;
1087
1088     case DL_UNBIND_REQ:
1089         if (size < sizeof(dl_unbind_req_t))
1090             goto badprim;
1091         if (us->state != DL_IDLE) {
1092             dlpi_error(q, us, DL_UNBIND_REQ, DL_OUTSTATE, 0);
1093             break;
1094         }
1095         us->sap = -1;
1096         us->state = DL_UNBOUND;
1097 #ifdef LACHTCP
1098         us->ppa->ifstats.ifs_active = 0;
1099 #endif
1100         dlpi_ok(q, DL_UNBIND_REQ);
1101         break;
1102
1103     case DL_UNITDATA_REQ:
1104         if (size < sizeof(dl_unitdata_req_t))
1105             goto badprim;
1106         if (us->state != DL_IDLE) {
1107             dlpi_error(q, us, DL_UNITDATA_REQ, DL_OUTSTATE, 0);
1108             break;
1109         }
1110         if ((ppa = us->ppa) == 0) {
1111             cmn_err(CE_CONT, "ppp: in state dl_idle but ppa == 0?\n");
1112             break;
1113         }
1114         len = mp->b_cont == 0? 0: msgdsize(mp->b_cont);
1115         if (len > ppa->mtu) {
1116             DPRINT2("dlpi data too large (%d > %d)\n", len, ppa->mtu);
1117             break;
1118         }
1119         mp->b_band = 0;
1120 #ifdef PRIOQ
1121         /* Extract s_port & d_port from IP-packet, the code is a bit
1122            dirty here, but so am I, too... */
1123         if (mp->b_datap->db_type == M_PROTO && us->sap == PPP_IP
1124             && mp->b_cont != 0) {
1125             u_char *bb, *tlh;
1126             int iphlen, len;
1127             u_short *ptr;
1128             u_char band_unset, cur_band, syn;
1129             u_short s_port, d_port;
1130
1131             bb = mp->b_cont->b_rptr; /* bb points to IP-header*/
1132             len = mp->b_cont->b_wptr - mp->b_cont->b_rptr;
1133             syn = 0;
1134             s_port = IPPORT_DEFAULT;
1135             d_port = IPPORT_DEFAULT;
1136             if (len >= 20) {    /* 20 = minimum length of IP header */
1137                 iphlen = (bb[0] & 0x0f) * 4;
1138                 tlh = bb + iphlen;
1139                 len -= iphlen;
1140                 switch (bb[9]) {
1141                 case IPPROTO_TCP:
1142                     if (len >= 20) {          /* min length of TCP header */
1143                         s_port = (tlh[0] << 8) + tlh[1];
1144                         d_port = (tlh[2] << 8) + tlh[3];
1145                         syn = tlh[13] & 0x02;
1146                     }
1147                     break;
1148                 case IPPROTO_UDP:
1149                     if (len >= 8) {           /* min length of UDP header */
1150                         s_port = (tlh[0] << 8) + tlh[1];
1151                         d_port = (tlh[2] << 8) + tlh[3];
1152                     }
1153                     break;
1154                 }
1155             }
1156
1157             /*
1158              * Now calculate b_band for this packet from the
1159              * port-priority table.
1160              */
1161             ptr = prioq_table;
1162             cur_band = max_band;
1163             band_unset = 1;
1164             while (*ptr) {
1165                 while (*ptr && band_unset)
1166                     if (s_port == *ptr || d_port == *ptr++) {
1167                         mp->b_band = cur_band;
1168                         band_unset = 0;
1169                         break;
1170                     }
1171                 ptr++;
1172                 cur_band--;
1173             }
1174             if (band_unset)
1175                 mp->b_band = def_band;
1176             /* It may be usable to urge SYN packets a bit */
1177             if (syn)
1178                 mp->b_band++;
1179         }
1180 #endif  /* PRIOQ */
1181         /* this assumes PPP_HDRLEN <= sizeof(dl_unitdata_req_t) */
1182         if (mp->b_datap->db_ref > 1) {
1183             np = allocb(PPP_HDRLEN, BPRI_HI);
1184             if (np == 0)
1185                 break;          /* gak! */
1186             np->b_cont = mp->b_cont;
1187             mp->b_cont = 0;
1188             freeb(mp);
1189             mp = np;
1190         } else
1191             mp->b_datap->db_type = M_DATA;
1192         /* XXX should use dl_dest_addr_offset/length here,
1193            but we would have to translate ETHERTYPE_IP -> PPP_IP */
1194         mp->b_wptr = mp->b_rptr + PPP_HDRLEN;
1195         mp->b_rptr[0] = PPP_ALLSTATIONS;
1196         mp->b_rptr[1] = PPP_UI;
1197         mp->b_rptr[2] = us->sap >> 8;
1198         mp->b_rptr[3] = us->sap;
1199         if (pass_packet(us, mp, 1)) {
1200             if (!send_data(mp, us))
1201                 putq(q, mp);
1202         }
1203         return;
1204
1205 #if DL_CURRENT_VERSION >= 2
1206     case DL_SUBS_BIND_REQ:
1207     case DL_SUBS_UNBIND_REQ:
1208     case DL_ENABMULTI_REQ:
1209     case DL_DISABMULTI_REQ:
1210     case DL_PROMISCON_REQ:
1211     case DL_PROMISCOFF_REQ:
1212     case DL_PHYS_ADDR_REQ:
1213     case DL_SET_PHYS_ADDR_REQ:
1214     case DL_XID_REQ:
1215     case DL_TEST_REQ:
1216     case DL_REPLY_UPDATE_REQ:
1217     case DL_REPLY_REQ:
1218     case DL_DATA_ACK_REQ:
1219 #endif
1220     case DL_CONNECT_REQ:
1221     case DL_TOKEN_REQ:
1222         dlpi_error(q, us, d->dl_primitive, DL_NOTSUPPORTED, 0);
1223         break;
1224
1225     case DL_CONNECT_RES:
1226     case DL_DISCONNECT_REQ:
1227     case DL_RESET_REQ:
1228     case DL_RESET_RES:
1229         dlpi_error(q, us, d->dl_primitive, DL_OUTSTATE, 0);
1230         break;
1231
1232     case DL_UDQOS_REQ:
1233         dlpi_error(q, us, d->dl_primitive, DL_BADQOSTYPE, 0);
1234         break;
1235
1236 #if DL_CURRENT_VERSION >= 2
1237     case DL_TEST_RES:
1238     case DL_XID_RES:
1239         break;
1240 #endif
1241
1242     default:
1243         cmn_err(CE_CONT, "ppp: unknown dlpi prim 0x%x\n", d->dl_primitive);
1244         /* fall through */
1245     badprim:
1246         dlpi_error(q, us, d->dl_primitive, DL_BADPRIM, 0);
1247         break;
1248     }
1249     freemsg(mp);
1250 }
1251
1252 static void
1253 dlpi_error(q, us, prim, err, uerr)
1254     queue_t *q;
1255     upperstr_t *us;
1256     int prim, err, uerr;
1257 {
1258     mblk_t *reply;
1259     dl_error_ack_t *errp;
1260
1261     if (us->flags & US_DBGLOG)
1262         DPRINT3("ppp/%d: dlpi error, prim=%x, err=%x\n", us->mn, prim, err);
1263     reply = allocb(sizeof(dl_error_ack_t), BPRI_HI);
1264     if (reply == 0)
1265         return;                 /* XXX should do bufcall */
1266     reply->b_datap->db_type = M_PCPROTO;
1267     errp = (dl_error_ack_t *) reply->b_wptr;
1268     reply->b_wptr += sizeof(dl_error_ack_t);
1269     errp->dl_primitive = DL_ERROR_ACK;
1270     errp->dl_error_primitive = prim;
1271     errp->dl_errno = err;
1272     errp->dl_unix_errno = uerr;
1273     qreply(q, reply);
1274 }
1275
1276 static void
1277 dlpi_ok(q, prim)
1278     queue_t *q;
1279     int prim;
1280 {
1281     mblk_t *reply;
1282     dl_ok_ack_t *okp;
1283
1284     reply = allocb(sizeof(dl_ok_ack_t), BPRI_HI);
1285     if (reply == 0)
1286         return;                 /* XXX should do bufcall */
1287     reply->b_datap->db_type = M_PCPROTO;
1288     okp = (dl_ok_ack_t *) reply->b_wptr;
1289     reply->b_wptr += sizeof(dl_ok_ack_t);
1290     okp->dl_primitive = DL_OK_ACK;
1291     okp->dl_correct_primitive = prim;
1292     qreply(q, reply);
1293 }
1294 #endif /* NO_DLPI */
1295
1296 static int
1297 pass_packet(us, mp, outbound)
1298     upperstr_t *us;
1299     mblk_t *mp;
1300     int outbound;
1301 {
1302     int pass;
1303     upperstr_t *ppa;
1304
1305     if ((ppa = us->ppa) == 0) {
1306         freemsg(mp);
1307         return 0;
1308     }
1309
1310 #ifdef FILTER_PACKETS
1311     pass = ip_hard_filter(us, mp, outbound);
1312 #else
1313     /*
1314      * Here is where we might, in future, decide whether to pass
1315      * or drop the packet, and whether it counts as link activity.
1316      */
1317     pass = 1;
1318 #endif /* FILTER_PACKETS */
1319
1320     if (pass < 0) {
1321         /* pass only if link already up, and don't update time */
1322         if (ppa->lowerq == 0) {
1323             freemsg(mp);
1324             return 0;
1325         }
1326         pass = 1;
1327     } else if (pass) {
1328         if (outbound)
1329             ppa->last_sent = time;
1330         else
1331             ppa->last_recv = time;
1332     }
1333
1334     return pass;
1335 }
1336
1337 static int
1338 send_data(mp, us)
1339     mblk_t *mp;
1340     upperstr_t *us;
1341 {
1342     queue_t *q;
1343     upperstr_t *ppa;
1344
1345     if ((us->flags & US_BLOCKED) || us->npmode == NPMODE_QUEUE)
1346         return 0;
1347     ppa = us->ppa;
1348     if (ppa == 0 || us->npmode == NPMODE_DROP || us->npmode == NPMODE_ERROR) {
1349         if (us->flags & US_DBGLOG)
1350             DPRINT2("ppp/%d: dropping pkt (npmode=%d)\n", us->mn, us->npmode);
1351         freemsg(mp);
1352         return 1;
1353     }
1354     if ((q = ppa->lowerq) == 0) {
1355         /* try to send it up the control stream */
1356         if (bcanputnext(ppa->q, mp->b_band)) {
1357             /*
1358              * The message seems to get corrupted for some reason if
1359              * we just send the message up as it is, so we send a copy.
1360              */
1361             mblk_t *np = copymsg(mp);
1362             freemsg(mp);
1363             if (np != 0)
1364                 putnext(ppa->q, np);
1365             return 1;
1366         }
1367     } else {
1368         if (bcanputnext(ppa->lowerq, mp->b_band)) {
1369             /*
1370              * The lower write queue's put procedure just updates counters
1371              * and does a putnext.  We call it so that on SMP systems, we
1372              * enter the lower queues' perimeter so that the counter
1373              * updates are serialized.
1374              */
1375             put(ppa->lowerq, mp);
1376             return 1;
1377         }
1378     }
1379     us->flags |= US_BLOCKED;
1380     return 0;
1381 }
1382
1383 /*
1384  * Allocate a new PPA id and link this stream into the list of PPAs.
1385  * This procedure is called with an exclusive lock on all queues in
1386  * this driver.
1387  */
1388 static void
1389 new_ppa(q, mp)
1390     queue_t *q;
1391     mblk_t *mp;
1392 {
1393     upperstr_t *us, **usp;
1394     int ppa_id;
1395
1396     usp = &ppas;
1397     ppa_id = 0;
1398     while ((us = *usp) != 0 && ppa_id == us->ppa_id) {
1399         ++ppa_id;
1400         usp = &us->nextppa;
1401     }
1402     us = (upperstr_t *) q->q_ptr;
1403     us->ppa_id = ppa_id;
1404     us->ppa = us;
1405     us->next = 0;
1406     us->nextppa = *usp;
1407     *usp = us;
1408     us->flags |= US_CONTROL;
1409     us->npmode = NPMODE_PASS;
1410
1411     us->mtu = PPP_MTU;
1412     us->mru = PPP_MRU;
1413
1414 #ifdef SOL2
1415     /*
1416      * Create a kstats record for our statistics, so netstat -i works.
1417      */
1418     if (us->kstats == 0) {
1419         char unit[32];
1420
1421         sprintf(unit, "ppp%d", us->ppa->ppa_id);
1422         us->kstats = kstat_create("ppp", us->ppa->ppa_id, unit,
1423                                   "net", KSTAT_TYPE_NAMED, 4, 0);
1424         if (us->kstats != 0) {
1425             kstat_named_t *kn = KSTAT_NAMED_PTR(us->kstats);
1426
1427             strcpy(kn[0].name, "ipackets");
1428             kn[0].data_type = KSTAT_DATA_ULONG;
1429             strcpy(kn[1].name, "ierrors");
1430             kn[1].data_type = KSTAT_DATA_ULONG;
1431             strcpy(kn[2].name, "opackets");
1432             kn[2].data_type = KSTAT_DATA_ULONG;
1433             strcpy(kn[3].name, "oerrors");
1434             kn[3].data_type = KSTAT_DATA_ULONG;
1435             kstat_install(us->kstats);
1436         }
1437     }
1438 #endif /* SOL2 */
1439
1440     *(int *)mp->b_cont->b_rptr = ppa_id;
1441     mp->b_datap->db_type = M_IOCACK;
1442     qreply(q, mp);
1443 }
1444
1445 static void
1446 attach_ppa(q, mp)
1447     queue_t *q;
1448     mblk_t *mp;
1449 {
1450     upperstr_t *us, *t;
1451
1452     us = (upperstr_t *) q->q_ptr;
1453 #ifndef NO_DLPI
1454     us->state = DL_UNBOUND;
1455 #endif
1456     for (t = us->ppa; t->next != 0; t = t->next)
1457         ;
1458     t->next = us;
1459     us->next = 0;
1460     if (mp->b_datap->db_type == M_IOCTL) {
1461         mp->b_datap->db_type = M_IOCACK;
1462         qreply(q, mp);
1463     } else {
1464 #ifndef NO_DLPI
1465         dlpi_ok(q, DL_ATTACH_REQ);
1466 #endif
1467     }
1468 }
1469
1470 static void
1471 detach_ppa(q, mp)
1472     queue_t *q;
1473     mblk_t *mp;
1474 {
1475     upperstr_t *us, *t;
1476
1477     us = (upperstr_t *) q->q_ptr;
1478     for (t = us->ppa; t->next != 0; t = t->next)
1479         if (t->next == us) {
1480             t->next = us->next;
1481             break;
1482         }
1483     us->next = 0;
1484     us->ppa = 0;
1485 #ifndef NO_DLPI
1486     us->state = DL_UNATTACHED;
1487     dlpi_ok(q, DL_DETACH_REQ);
1488 #endif
1489 }
1490
1491 static int
1492 pppuwsrv(q)
1493     queue_t *q;
1494 {
1495     upperstr_t *us;
1496     struct lowerstr *ls;
1497     queue_t *lwq;
1498     mblk_t *mp;
1499
1500     us = (upperstr_t *) q->q_ptr;
1501     us->flags &= ~US_BLOCKED;
1502     while ((mp = getq(q)) != 0) {
1503         if (!send_data(mp, us)) {
1504             putbq(q, mp);
1505             break;
1506         }
1507     }
1508     return 0;
1509 }
1510
1511 static int
1512 ppplwput(q, mp)
1513     queue_t *q;
1514     mblk_t *mp;
1515 {
1516     upperstr_t *ppa;
1517
1518     ppa = (upperstr_t *) q->q_ptr;
1519     if (ppa != 0) {             /* why wouldn't it? */
1520         ppa->stats.ppp_opackets++;
1521         ppa->stats.ppp_obytes += msgdsize(mp);
1522 #ifdef INCR_OPACKETS
1523         INCR_OPACKETS(ppa);
1524 #endif
1525     }
1526     putnext(q, mp);
1527     return 0;
1528 }
1529
1530 static int
1531 ppplwsrv(q)
1532     queue_t *q;
1533 {
1534     upperstr_t *us;
1535
1536     /*
1537      * Flow control has back-enabled this stream:
1538      * enable the write service procedures of all upper
1539      * streams feeding this lower stream.
1540      */
1541     for (us = (upperstr_t *) q->q_ptr; us != NULL; us = us->next)
1542         if (us->flags & US_BLOCKED)
1543             qenable(WR(us->q));
1544     return 0;
1545 }
1546
1547 static int
1548 pppursrv(q)
1549     queue_t *q;
1550 {
1551     upperstr_t *us, *as;
1552     mblk_t *mp, *hdr;
1553 #ifndef NO_DLPI
1554     dl_unitdata_ind_t *ud;
1555 #endif
1556     int proto;
1557
1558     us = (upperstr_t *) q->q_ptr;
1559     if (us->flags & US_CONTROL) {
1560         /*
1561          * A control stream.
1562          * If there is no lower queue attached, run the write service
1563          * routines of other upper streams attached to this PPA.
1564          */
1565         if (us->lowerq == 0) {
1566             as = us;
1567             do {
1568                 if (as->flags & US_BLOCKED)
1569                     qenable(WR(as->q));
1570                 as = as->next;
1571             } while (as != 0);
1572         }
1573     } else {
1574         /*
1575          * A network protocol stream.  Put a DLPI header on each
1576          * packet and send it on.
1577          * (Actually, it seems that the IP module will happily
1578          * accept M_DATA messages without the DL_UNITDATA_IND header.)
1579          */
1580         while ((mp = getq(q)) != 0) {
1581             if (!canputnext(q)) {
1582                 putbq(q, mp);
1583                 break;
1584             }
1585 #ifndef NO_DLPI
1586             proto = PPP_PROTOCOL(mp->b_rptr);
1587             mp->b_rptr += PPP_HDRLEN;
1588             hdr = allocb(sizeof(dl_unitdata_ind_t) + 2 * sizeof(uint),
1589                          BPRI_MED);
1590             if (hdr == 0) {
1591                 /* XXX should put it back and use bufcall */
1592                 freemsg(mp);
1593                 continue;
1594             }
1595             hdr->b_datap->db_type = M_PROTO;
1596             ud = (dl_unitdata_ind_t *) hdr->b_wptr;
1597             hdr->b_wptr += sizeof(dl_unitdata_ind_t) + 2 * sizeof(uint);
1598             hdr->b_cont = mp;
1599             ud->dl_primitive = DL_UNITDATA_IND;
1600             ud->dl_dest_addr_length = sizeof(uint);
1601             ud->dl_dest_addr_offset = sizeof(dl_unitdata_ind_t);
1602             ud->dl_src_addr_length = sizeof(uint);
1603             ud->dl_src_addr_offset = ud->dl_dest_addr_offset + sizeof(uint);
1604 #if DL_CURRENT_VERSION >= 2
1605             ud->dl_group_address = 0;
1606 #endif
1607             /* Send the DLPI client the data with the SAP they requested,
1608                (e.g. ETHERTYPE_IP) rather than the PPP protocol number
1609                (e.g. PPP_IP) */
1610             ((uint *)(ud + 1))[0] = us->req_sap;        /* dest SAP */
1611             ((uint *)(ud + 1))[1] = us->req_sap;        /* src SAP */
1612             putnext(q, hdr);
1613 #else /* NO_DLPI */
1614             putnext(q, mp);
1615 #endif /* NO_DLPI */
1616         }
1617     }
1618
1619     /*
1620      * If this stream is attached to a PPA with a lower queue pair,
1621      * enable the read queue's service routine if it has data queued.
1622      * XXX there is a possibility that packets could get out of order
1623      * if ppplrput now runs before ppplrsrv.
1624      */
1625     if (us->ppa != 0 && us->ppa->lowerq != 0)
1626         qenable(RD(us->ppa->lowerq));
1627
1628     return 0;
1629 }
1630
1631 static upperstr_t *
1632 find_dest(ppa, proto)
1633     upperstr_t *ppa;
1634     int proto;
1635 {
1636     upperstr_t *us;
1637
1638     for (us = ppa->next; us != 0; us = us->next)
1639         if (proto == us->sap)
1640             break;
1641     return us;
1642 }
1643
1644 static int
1645 ppplrput(q, mp)
1646     queue_t *q;
1647     mblk_t *mp;
1648 {
1649     upperstr_t *ppa, *us;
1650     queue_t *uq;
1651     int proto, len;
1652     mblk_t *np;
1653     struct iocblk *iop;
1654
1655     ppa = (upperstr_t *) q->q_ptr;
1656     if (ppa == 0) {
1657         DPRINT1("ppplrput: q = %x, ppa = 0??\n", q);
1658         freemsg(mp);
1659         return 0;
1660     }
1661     switch (mp->b_datap->db_type) {
1662     case M_FLUSH:
1663         if (*mp->b_rptr & FLUSHW) {
1664             *mp->b_rptr &= ~FLUSHR;
1665             qreply(q, mp);
1666         } else
1667             freemsg(mp);
1668         break;
1669
1670     case M_CTL:
1671         switch (*mp->b_rptr) {
1672         case PPPCTL_IERROR:
1673 #ifdef INCR_IERRORS
1674             INCR_IERRORS(ppa);
1675 #endif
1676             ppa->stats.ppp_ierrors++;
1677             break;
1678         case PPPCTL_OERROR:
1679 #ifdef INCR_OERRORS
1680             INCR_OERRORS(ppa);
1681 #endif
1682             ppa->stats.ppp_oerrors++;
1683             break;
1684         }
1685         freemsg(mp);
1686         break;
1687
1688     case M_IOCACK:
1689     case M_IOCNAK:
1690         /*
1691          * Attempt to match up the response with the stream
1692          * that the request came from.
1693          */
1694         iop = (struct iocblk *) mp->b_rptr;
1695         for (us = ppa; us != 0; us = us->next)
1696             if (us->ioc_id == iop->ioc_id)
1697                 break;
1698         if (us == 0)
1699             freemsg(mp);
1700         else
1701             putnext(us->q, mp);
1702         break;
1703
1704     case M_HANGUP:
1705         /*
1706          * The serial device has hung up.  We don't want to send
1707          * the M_HANGUP message up to pppd because that will stop
1708          * us from using the control stream any more.  Instead we
1709          * send a zero-length message as an end-of-file indication.
1710          */
1711         freemsg(mp);
1712         mp = allocb(1, BPRI_HI);
1713         if (mp == 0) {
1714             DPRINT1("ppp/%d: couldn't allocate eof message!\n", ppa->mn);
1715             break;
1716         }
1717         putnext(ppa->q, mp);
1718         break;
1719
1720     default:
1721         if (mp->b_datap->db_type == M_DATA) {
1722             len = msgdsize(mp);
1723             if (mp->b_wptr - mp->b_rptr < PPP_HDRLEN) {
1724                 PULLUP(mp, PPP_HDRLEN);
1725                 if (mp == 0) {
1726                     DPRINT1("ppp_lrput: msgpullup failed (len=%d)\n", len);
1727                     break;
1728                 }
1729             }
1730             ppa->stats.ppp_ipackets++;
1731             ppa->stats.ppp_ibytes += len;
1732 #ifdef INCR_IPACKETS
1733             INCR_IPACKETS(ppa);
1734 #endif
1735             proto = PPP_PROTOCOL(mp->b_rptr);
1736             if (proto < 0x8000 && (us = find_dest(ppa, proto)) != 0) {
1737                 /*
1738                  * A data packet for some network protocol.
1739                  * Queue it on the upper stream for that protocol.
1740                  */
1741                 if (!pass_packet(us, mp, 0))
1742                     break;
1743                 if (canput(us->q))
1744                     putq(us->q, mp);
1745                 else
1746                     putq(q, mp);
1747                 break;
1748             }
1749         }
1750         /*
1751          * A control frame, a frame for an unknown protocol,
1752          * or some other message type.
1753          * Send it up to pppd via the control stream.
1754          */
1755         if (queclass(mp) == QPCTL || canputnext(ppa->q))
1756             putnext(ppa->q, mp);
1757         else
1758             putq(q, mp);
1759         break;
1760     }
1761
1762     return 0;
1763 }
1764
1765 static int
1766 ppplrsrv(q)
1767     queue_t *q;
1768 {
1769     mblk_t *mp;
1770     upperstr_t *ppa, *us;
1771     int proto;
1772
1773     /*
1774      * Packets only get queued here for flow control reasons.
1775      */
1776     ppa = (upperstr_t *) q->q_ptr;
1777     while ((mp = getq(q)) != 0) {
1778         if (mp->b_datap->db_type == M_DATA
1779             && (proto = PPP_PROTOCOL(mp->b_rptr)) < 0x8000
1780             && (us = find_dest(ppa, proto)) != 0) {
1781             if (canput(us->q))
1782                 putq(us->q, mp);
1783             else {
1784                 putbq(q, mp);
1785                 break;
1786             }
1787         } else {
1788             if (canputnext(ppa->q))
1789                 putnext(ppa->q, mp);
1790             else {
1791                 putbq(q, mp);
1792                 break;
1793             }
1794         }
1795     }
1796     return 0;
1797 }
1798
1799 static int
1800 putctl2(q, type, code, val)
1801     queue_t *q;
1802     int type, code, val;
1803 {
1804     mblk_t *mp;
1805
1806     mp = allocb(2, BPRI_HI);
1807     if (mp == 0)
1808         return 0;
1809     mp->b_datap->db_type = type;
1810     mp->b_wptr[0] = code;
1811     mp->b_wptr[1] = val;
1812     mp->b_wptr += 2;
1813     putnext(q, mp);
1814     return 1;
1815 }
1816
1817 static int
1818 putctl4(q, type, code, val)
1819     queue_t *q;
1820     int type, code, val;
1821 {
1822     mblk_t *mp;
1823
1824     mp = allocb(4, BPRI_HI);
1825     if (mp == 0)
1826         return 0;
1827     mp->b_datap->db_type = type;
1828     mp->b_wptr[0] = code;
1829     ((short *)mp->b_wptr)[1] = val;
1830     mp->b_wptr += 4;
1831     putnext(q, mp);
1832     return 1;
1833 }
1834
1835 static void
1836 debug_dump(q, mp)
1837     queue_t *q;                 /* not used */
1838     mblk_t *mp;                 /* not used either */
1839 {
1840     upperstr_t *us;
1841     queue_t *uq, *lq;
1842
1843     DPRINT("ppp upper streams:\n");
1844     for (us = minor_devs; us != 0; us = us->nextmn) {
1845         uq = us->q;
1846         DPRINT3(" %d: q=%x rlev=%d",
1847                 us->mn, uq, (uq? qsize(uq): 0));
1848         DPRINT3(" wlev=%d flags=0x%b", (uq? qsize(WR(uq)): 0),
1849                 us->flags, "\020\1priv\2control\3blocked\4last");
1850         DPRINT3(" state=%x sap=%x req_sap=%x", us->state, us->sap,
1851                 us->req_sap);
1852         if (us->ppa == 0)
1853             DPRINT(" ppa=?\n");
1854         else
1855             DPRINT1(" ppa=%d\n", us->ppa->ppa_id);
1856         if (us->flags & US_CONTROL) {
1857             lq = us->lowerq;
1858             DPRINT3("    control for %d lq=%x rlev=%d",
1859                     us->ppa_id, lq, (lq? qsize(RD(lq)): 0));
1860             DPRINT3(" wlev=%d mru=%d mtu=%d\n",
1861                     (lq? qsize(lq): 0), us->mru, us->mtu);
1862         }
1863     }
1864 }
1865
1866 #ifdef FILTER_PACKETS
1867 #include <netinet/in_systm.h>
1868 #include <netinet/ip.h>
1869 #include <netinet/udp.h>
1870 #include <netinet/tcp.h>
1871
1872 #define MAX_IPHDR    128     /* max TCP/IP header size */
1873
1874
1875 /* The following table contains a hard-coded list of protocol/port pairs.
1876  * Any matching packets are either discarded unconditionally, or, 
1877  * if ok_if_link_up is non-zero when a connection does not currently exist
1878  * (i.e., they go through if the connection is present, but never initiate
1879  * a dial-out).
1880  * This idea came from a post by dm@garage.uun.org (David Mazieres)
1881  */
1882 static struct pktfilt_tab { 
1883         int proto; 
1884         u_short port; 
1885         u_short ok_if_link_up; 
1886 } pktfilt_tab[] = {
1887         { IPPROTO_UDP,  520,    1 },    /* RIP, ok to pass if link is up */
1888         { IPPROTO_UDP,  123,    1 },    /* NTP, don't keep up the link for it */
1889         { -1,           0,      0 }     /* terminator entry has port == -1 */
1890 };
1891
1892
1893 static int
1894 ip_hard_filter(us, mp, outbound)
1895     upperstr_t *us;
1896     mblk_t *mp;
1897     int outbound;
1898 {
1899     struct ip *ip;
1900     struct pktfilt_tab *pft;
1901     mblk_t *temp_mp;
1902     int proto;
1903     int len, hlen;
1904
1905
1906     /* Note, the PPP header has already been pulled up in all cases */
1907     proto = PPP_PROTOCOL(mp->b_rptr);
1908     if (us->flags & US_DBGLOG)
1909         DPRINT3("ppp/%d: filter, proto=0x%x, out=%d\n", us->mn, proto, outbound);
1910
1911     switch (proto)
1912     {
1913     case PPP_IP:
1914         if ((mp->b_wptr - mp->b_rptr) == PPP_HDRLEN && mp->b_cont != 0) {
1915             temp_mp = mp->b_cont;
1916             len = msgdsize(temp_mp);
1917             hlen = (len < MAX_IPHDR) ? len : MAX_IPHDR;
1918             PULLUP(temp_mp, hlen);
1919             if (temp_mp == 0) {
1920                 DPRINT2("ppp/%d: filter, pullup next failed, len=%d\n", 
1921                         us->mn, hlen);
1922                 mp->b_cont = 0;         /* PULLUP() freed the rest */
1923                 freemsg(mp);
1924                 return 0;
1925             }
1926             ip = (struct ip *)mp->b_cont->b_rptr;
1927         }
1928         else {
1929             len = msgdsize(mp);
1930             hlen = (len < (PPP_HDRLEN+MAX_IPHDR)) ? len : (PPP_HDRLEN+MAX_IPHDR);
1931             PULLUP(mp, hlen);
1932             if (mp == 0) {
1933                 DPRINT2("ppp/%d: filter, pullup failed, len=%d\n", 
1934                         us->mn, hlen);
1935                 return 0;
1936             }
1937             ip = (struct ip *)(mp->b_rptr + PPP_HDRLEN);
1938         }
1939
1940         /* For IP traffic, certain packets (e.g., RIP) may be either
1941          *   1.  ignored - dropped completely
1942          *   2.  will not initiate a connection, but
1943          *       will be passed if a connection is currently up.
1944          */
1945         for (pft=pktfilt_tab; pft->proto != -1; pft++) {
1946             if (ip->ip_p == pft->proto) {
1947                 switch(pft->proto) {
1948                 case IPPROTO_UDP:
1949                     if (((struct udphdr *) &((int *)ip)[ip->ip_hl])->uh_dport
1950                                 == htons(pft->port)) goto endfor;
1951                     break;
1952                 case IPPROTO_TCP:
1953                     if (((struct tcphdr *) &((int *)ip)[ip->ip_hl])->th_dport
1954                                 == htons(pft->port)) goto endfor;
1955                     break;
1956                 }       
1957             }
1958         }
1959         endfor:
1960         if (pft->proto != -1) {
1961             if (us->flags & US_DBGLOG)
1962                 DPRINT3("ppp/%d: found IP pkt, proto=0x%x (%d)\n", 
1963                                 us->mn, pft->proto, pft->port);
1964             /* Discard if not connected, or if not pass_with_link_up */
1965             /* else, if link is up let go by, but don't update time */
1966             return pft->ok_if_link_up? -1: 0;
1967         }
1968         break;
1969     } /* end switch (proto) */
1970
1971     return 1;
1972 }
1973 #endif /* FILTER_PACKETS */
1974