]> git.ozlabs.org Git - ppp.git/blob - netbsd-1.1/if_ppp.c
Added packet filtering; ref. Deflate compressor; malloc VJ state;
[ppp.git] / netbsd-1.1 / if_ppp.c
1 /*      $Id: if_ppp.c,v 1.2 1996/04/04 03:21:05 paulus Exp $    */
2
3 /*
4  * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
5  *
6  * Copyright (c) 1989 Carnegie Mellon University.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms are permitted
10  * provided that the above copyright notice and this paragraph are
11  * duplicated in all such forms and that any documentation,
12  * advertising materials, and other materials related to such
13  * distribution and use acknowledge that the software was developed
14  * by Carnegie Mellon University.  The name of the
15  * University may not be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
19  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  *
21  * Drew D. Perkins
22  * Carnegie Mellon University
23  * 4910 Forbes Ave.
24  * Pittsburgh, PA 15213
25  * (412) 268-8576
26  * ddp@andrew.cmu.edu
27  *
28  * Based on:
29  *      @(#)if_sl.c     7.6.1.2 (Berkeley) 2/15/89
30  *
31  * Copyright (c) 1987 Regents of the University of California.
32  * All rights reserved.
33  *
34  * Redistribution and use in source and binary forms are permitted
35  * provided that the above copyright notice and this paragraph are
36  * duplicated in all such forms and that any documentation,
37  * advertising materials, and other materials related to such
38  * distribution and use acknowledge that the software was developed
39  * by the University of California, Berkeley.  The name of the
40  * University may not be used to endorse or promote products derived
41  * from this software without specific prior written permission.
42  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
43  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
44  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
45  *
46  * Serial Line interface
47  *
48  * Rick Adams
49  * Center for Seismic Studies
50  * 1300 N 17th Street, Suite 1450
51  * Arlington, Virginia 22209
52  * (703)276-7900
53  * rick@seismo.ARPA
54  * seismo!rick
55  *
56  * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
57  * Converted to 4.3BSD Beta by Chris Torek.
58  * Other changes made at Berkeley, based in part on code by Kirk Smith.
59  *
60  * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
61  * Added VJ tcp header compression; more unified ioctls
62  *
63  * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au).
64  * Cleaned up a lot of the mbuf-related code to fix bugs that
65  * caused system crashes and packet corruption.  Changed pppstart
66  * so that it doesn't just give up with a collision if the whole
67  * packet doesn't fit in the output ring buffer.
68  *
69  * Added priority queueing for interactive IP packets, following
70  * the model of if_sl.c, plus hooks for bpf.
71  * Paul Mackerras (paulus@cs.anu.edu.au).
72  */
73
74 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
75 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
76
77 #include "ppp.h"
78 #if NPPP > 0
79
80 #define VJC
81 #define PPP_COMPRESS
82
83 #include <sys/param.h>
84 #include <sys/proc.h>
85 #include <sys/mbuf.h>
86 #include <sys/socket.h>
87 #include <sys/ioctl.h>
88 #include <sys/kernel.h>
89 #include <sys/systm.h>
90 #include <sys/time.h>
91 #include <sys/malloc.h>
92
93 #include <net/if.h>
94 #include <net/if_types.h>
95 #include <net/netisr.h>
96 #include <net/route.h>
97 #include <net/bpf.h>
98
99 #if INET
100 #include <netinet/in.h>
101 #include <netinet/in_systm.h>
102 #include <netinet/in_var.h>
103 #include <netinet/ip.h>
104 #endif
105
106 #include "bpfilter.h"
107 #if NBPFILTER > 0
108 #include <sys/time.h>
109 #include <net/bpf.h>
110 #endif
111
112 #ifdef VJC
113 #include <net/slcompress.h>
114 #endif
115
116 #include <net/ppp_defs.h>
117 #include <net/if_ppp.h>
118 #include <net/if_pppvar.h>
119 #include <machine/cpu.h>
120
121 #ifdef PPP_COMPRESS
122 #define PACKETPTR       struct mbuf *
123 #include <net/ppp-comp.h>
124 #endif
125
126 static void     ppp_requeue __P((struct ppp_softc *));
127 static void     ppp_outpkt __P((struct ppp_softc *));
128 static void     ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
129 static void     ppp_ccp_closed __P((struct ppp_softc *));
130 static void     ppp_inproc __P((struct ppp_softc *, struct mbuf *));
131 static void     pppdumpm __P((struct mbuf *m0));
132
133 /*
134  * Some useful mbuf macros not in mbuf.h.
135  */
136 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
137
138 #define M_DATASTART(m)  \
139         (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
140             (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
141
142 #define M_DATASIZE(m)   \
143         (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
144             (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
145
146 /*
147  * We steal two bits in the mbuf m_flags, to mark high-priority packets
148  * for output, and received packets following lost/corrupted packets.
149  */
150 #define M_HIGHPRI       0x2000  /* output packet for sc_fastq */
151 #define M_ERRMARK       0x4000  /* steal a bit in mbuf m_flags */
152
153
154 #ifdef PPP_COMPRESS
155 /*
156  * List of compressors we know about.
157  * We leave some space so maybe we can modload compressors.
158  */
159
160 extern struct compressor ppp_bsd_compress;
161 extern struct compressor ppp_deflate;
162
163 struct compressor *ppp_compressors[8] = {
164 #if DO_BSD_COMPRESS
165     &ppp_bsd_compress,
166 #endif
167 #if DO_DEFLATE
168     &ppp_deflate,
169 #endif
170     NULL
171 };
172 #endif /* PPP_COMPRESS */
173
174 /*
175  * Called from boot code to establish ppp interfaces.
176  */
177 void
178 pppattach()
179 {
180     register struct ppp_softc *sc;
181     register int i = 0;
182
183     for (sc = ppp_softc; i < NPPP; sc++) {
184         sc->sc_if.if_name = "ppp";
185         sc->sc_if.if_unit = i++;
186         sc->sc_if.if_mtu = PPP_MTU;
187         sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
188         sc->sc_if.if_type = IFT_PPP;
189         sc->sc_if.if_hdrlen = PPP_HDRLEN;
190         sc->sc_if.if_ioctl = pppsioctl;
191         sc->sc_if.if_output = pppoutput;
192         sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
193         sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
194         sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
195         sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
196         if_attach(&sc->sc_if);
197 #if NBPFILTER > 0
198         bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN);
199 #endif
200     }
201 }
202
203 /*
204  * Allocate a ppp interface unit and initialize it.
205  */
206 struct ppp_softc *
207 pppalloc(pid)
208     pid_t pid;
209 {
210     int nppp, i;
211     struct ppp_softc *sc;
212
213     for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
214         if (sc->sc_xfer == pid) {
215             sc->sc_xfer = 0;
216             return sc;
217         }
218     for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
219         if (sc->sc_devp == NULL)
220             break;
221     if (nppp >= NPPP)
222         return NULL;
223
224     sc->sc_flags = 0;
225     sc->sc_mru = PPP_MRU;
226     sc->sc_relinq = NULL;
227     bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
228 #ifdef VJC
229     MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
230            M_DEVBUF, M_NOWAIT);
231     if (sc->sc_comp)
232         sl_compress_init(sc->sc_comp, -1);
233 #endif
234 #ifdef PPP_COMPRESS
235     sc->sc_xc_state = NULL;
236     sc->sc_rc_state = NULL;
237 #endif /* PPP_COMPRESS */
238     for (i = 0; i < NUM_NP; ++i)
239         sc->sc_npmode[i] = NPMODE_ERROR;
240     sc->sc_npqueue = NULL;
241     sc->sc_npqtail = &sc->sc_npqueue;
242     sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
243
244     return sc;
245 }
246
247 /*
248  * Deallocate a ppp unit.  Must be called at splsoftnet or higher.
249  */
250 void
251 pppdealloc(sc)
252     struct ppp_softc *sc;
253 {
254     struct mbuf *m;
255
256     if_down(&sc->sc_if);
257     sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
258     sc->sc_devp = NULL;
259     sc->sc_xfer = 0;
260     for (;;) {
261         IF_DEQUEUE(&sc->sc_rawq, m);
262         if (m == NULL)
263             break;
264         m_freem(m);
265     }
266     for (;;) {
267         IF_DEQUEUE(&sc->sc_inq, m);
268         if (m == NULL)
269             break;
270         m_freem(m);
271     }
272     for (;;) {
273         IF_DEQUEUE(&sc->sc_fastq, m);
274         if (m == NULL)
275             break;
276         m_freem(m);
277     }
278     while ((m = sc->sc_npqueue) != NULL) {
279         sc->sc_npqueue = m->m_nextpkt;
280         m_freem(m);
281     }
282     if (sc->sc_togo != NULL) {
283         m_freem(sc->sc_togo);
284         sc->sc_togo = NULL;
285     }
286 #ifdef PPP_COMPRESS
287     ppp_ccp_closed(sc);
288     sc->sc_xc_state = NULL;
289     sc->sc_rc_state = NULL;
290 #endif /* PPP_COMPRESS */
291     if (sc->sc_pass_filt.bf_insns != 0) {
292         FREE(sc->sc_pass_filt.bf_insns, M_DEVBUF);
293         sc->sc_pass_filt.bf_insns = 0;
294         sc->sc_pass_filt.bf_len = 0;
295     }
296     if (sc->sc_active_filt.bf_insns != 0) {
297         FREE(sc->sc_active_filt.bf_insns, M_DEVBUF);
298         sc->sc_active_filt.bf_insns = 0;
299         sc->sc_active_filt.bf_len = 0;
300     }
301 #ifdef VJC
302     if (sc->sc_comp != 0) {
303         FREE(sc->sc_comp, M_DEVBUF);
304         sc->sc_comp = 0;
305     }
306 #endif
307 }
308
309 /*
310  * Ioctl routine for generic ppp devices.
311  */
312 int
313 pppioctl(sc, cmd, data, flag, p)
314     struct ppp_softc *sc;
315     u_long cmd;
316     caddr_t data;
317     int flag;
318     struct proc *p;
319 {
320     int s, error, flags, mru, nb, npx;
321     struct ppp_option_data *odp;
322     struct compressor **cp;
323     struct npioctl *npi;
324     time_t t;
325     struct bpf_program *bp, *nbp;
326     struct bpf_insn *newcode, *oldcode;
327     int newcodelen;
328 #ifdef  PPP_COMPRESS
329     u_char ccp_option[CCP_MAX_OPTION_LENGTH];
330 #endif
331
332     switch (cmd) {
333     case FIONREAD:
334         *(int *)data = sc->sc_inq.ifq_len;
335         break;
336
337     case PPPIOCGUNIT:
338         *(int *)data = sc->sc_if.if_unit;
339         break;
340
341     case PPPIOCGFLAGS:
342         *(u_int *)data = sc->sc_flags;
343         break;
344
345     case PPPIOCSFLAGS:
346         if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
347             return (error);
348         flags = *(int *)data & SC_MASK;
349         s = splsoftnet();
350 #ifdef PPP_COMPRESS
351         if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
352             ppp_ccp_closed(sc);
353 #endif
354         splhigh();
355         sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
356         splx(s);
357         break;
358
359     case PPPIOCSMRU:
360         if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
361             return (error);
362         mru = *(int *)data;
363         if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
364             sc->sc_mru = mru;
365         break;
366
367     case PPPIOCGMRU:
368         *(int *)data = sc->sc_mru;
369         break;
370
371 #ifdef VJC
372     case PPPIOCSMAXCID:
373         if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
374             return (error);
375         if (sc->sc_comp) {
376             s = splsoftnet();
377             sl_compress_init(sc->sc_comp, *(int *)data);
378             splx(s);
379         }
380         break;
381 #endif
382
383     case PPPIOCXFERUNIT:
384         if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
385             return (error);
386         sc->sc_xfer = p->p_pid;
387         break;
388
389 #ifdef PPP_COMPRESS
390     case PPPIOCSCOMPRESS:
391         if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
392             return (error);
393         odp = (struct ppp_option_data *) data;
394         nb = odp->length;
395         if (nb > sizeof(ccp_option))
396             nb = sizeof(ccp_option);
397         if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
398             return (error);
399         if (ccp_option[1] < 2)  /* preliminary check on the length byte */
400             return (EINVAL);
401         for (cp = ppp_compressors; *cp != NULL; ++cp)
402             if ((*cp)->compress_proto == ccp_option[0]) {
403                 /*
404                  * Found a handler for the protocol - try to allocate
405                  * a compressor or decompressor.
406                  */
407                 error = 0;
408                 if (odp->transmit) {
409                     s = splsoftnet();
410                     if (sc->sc_xc_state != NULL)
411                         (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
412                     sc->sc_xcomp = *cp;
413                     sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
414                     if (sc->sc_xc_state == NULL) {
415                         if (sc->sc_flags & SC_DEBUG)
416                             printf("ppp%d: comp_alloc failed\n",
417                                sc->sc_if.if_unit);
418                         error = ENOBUFS;
419                     }
420                     splhigh();
421                     sc->sc_flags &= ~SC_COMP_RUN;
422                     splx(s);
423                 } else {
424                     s = splsoftnet();
425                     if (sc->sc_rc_state != NULL)
426                         (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
427                     sc->sc_rcomp = *cp;
428                     sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
429                     if (sc->sc_rc_state == NULL) {
430                         if (sc->sc_flags & SC_DEBUG)
431                             printf("ppp%d: decomp_alloc failed\n",
432                                sc->sc_if.if_unit);
433                         error = ENOBUFS;
434                     }
435                     splhigh();
436                     sc->sc_flags &= ~SC_DECOMP_RUN;
437                     splx(s);
438                 }
439                 return (error);
440             }
441         if (sc->sc_flags & SC_DEBUG)
442             printf("ppp%d: no compressor for [%x %x %x], %x\n",
443                    sc->sc_if.if_unit, ccp_option[0], ccp_option[1],
444                    ccp_option[2], nb);
445         return (EINVAL);        /* no handler found */
446 #endif /* PPP_COMPRESS */
447
448     case PPPIOCGNPMODE:
449     case PPPIOCSNPMODE:
450         npi = (struct npioctl *) data;
451         switch (npi->protocol) {
452         case PPP_IP:
453             npx = NP_IP;
454             break;
455         default:
456             return EINVAL;
457         }
458         if (cmd == PPPIOCGNPMODE) {
459             npi->mode = sc->sc_npmode[npx];
460         } else {
461             if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
462                 return (error);
463             if (npi->mode != sc->sc_npmode[npx]) {
464                 s = splsoftnet();
465                 sc->sc_npmode[npx] = npi->mode;
466                 if (npi->mode != NPMODE_QUEUE) {
467                     ppp_requeue(sc);
468                     (*sc->sc_start)(sc);
469                 }
470                 splx(s);
471             }
472         }
473         break;
474
475     case PPPIOCGIDLE:
476         s = splsoftnet();
477         t = time.tv_sec;
478         ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
479         ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
480         splx(s);
481         break;
482
483     case PPPIOCSPASS:
484     case PPPIOCSACTIVE:
485         nbp = (struct bpf_program *) data;
486         if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
487             return EINVAL;
488         newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
489         if (newcodelen != 0) {
490             MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
491             if (newcode == 0) {
492                 return EINVAL;          /* or sumpin */
493             }
494             if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
495                                newcodelen)) != 0) {
496                 FREE(newcode, M_DEVBUF);
497                 return error;
498             }
499             if (!bpf_validate(newcode, nbp->bf_len)) {
500                 FREE(newcode, M_DEVBUF);
501                 return EINVAL;
502             }
503         } else
504             newcode = 0;
505         bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
506         oldcode = bp->bf_insns;
507         s = splimp();
508         bp->bf_len = nbp->bf_len;
509         bp->bf_insns = newcode;
510         splx(s);
511         if (oldcode != 0)
512             FREE(oldcode, M_DEVBUF);
513         break;
514
515     default:
516         return (-1);
517     }
518     return (0);
519 }
520
521 /*
522  * Process an ioctl request to the ppp network interface.
523  */
524 int
525 pppsioctl(ifp, cmd, data)
526     register struct ifnet *ifp;
527     u_long cmd;
528     caddr_t data;
529 {
530     struct proc *p = curproc;   /* XXX */
531     register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
532     register struct ifaddr *ifa = (struct ifaddr *)data;
533     register struct ifreq *ifr = (struct ifreq *)data;
534     struct ppp_stats *psp;
535 #ifdef  PPP_COMPRESS
536     struct ppp_comp_stats *pcp;
537 #endif
538     int s = splimp(), error = 0;
539
540     switch (cmd) {
541     case SIOCSIFFLAGS:
542         if ((ifp->if_flags & IFF_RUNNING) == 0)
543             ifp->if_flags &= ~IFF_UP;
544         break;
545
546     case SIOCSIFADDR:
547         if (ifa->ifa_addr->sa_family != AF_INET)
548             error = EAFNOSUPPORT;
549         break;
550
551     case SIOCSIFDSTADDR:
552         if (ifa->ifa_addr->sa_family != AF_INET)
553             error = EAFNOSUPPORT;
554         break;
555
556     case SIOCSIFMTU:
557         if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
558             break;
559         sc->sc_if.if_mtu = ifr->ifr_mtu;
560         break;
561
562     case SIOCGIFMTU:
563         ifr->ifr_mtu = sc->sc_if.if_mtu;
564         break;
565
566     case SIOCADDMULTI:
567     case SIOCDELMULTI:
568         if (ifr == 0) {
569             error = EAFNOSUPPORT;
570             break;
571         }
572         switch(ifr->ifr_addr.sa_family) {
573 #ifdef INET
574         case AF_INET:
575             break;
576 #endif
577         default:
578             error = EAFNOSUPPORT;
579             break;
580         }
581         break;
582
583     case SIOCGPPPSTATS:
584         psp = &((struct ifpppstatsreq *) data)->stats;
585         bzero(psp, sizeof(*psp));
586         psp->p = sc->sc_stats;
587 #if defined(VJC) && !defined(SL_NO_STATS)
588         if (sc->sc_comp) {
589             psp->vj.vjs_packets = sc->sc_comp->sls_packets;
590             psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
591             psp->vj.vjs_searches = sc->sc_comp->sls_searches;
592             psp->vj.vjs_misses = sc->sc_comp->sls_misses;
593             psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
594             psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
595             psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
596             psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
597         }
598 #endif /* VJC */
599         break;
600
601 #ifdef PPP_COMPRESS
602     case SIOCGPPPCSTATS:
603         pcp = &((struct ifpppcstatsreq *) data)->stats;
604         bzero(pcp, sizeof(*pcp));
605         if (sc->sc_xc_state != NULL)
606             (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
607         if (sc->sc_rc_state != NULL)
608             (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
609         break;
610 #endif /* PPP_COMPRESS */
611
612     default:
613         error = EINVAL;
614     }
615     splx(s);
616     return (error);
617 }
618
619 /*
620  * Queue a packet.  Start transmission if not active.
621  * Packet is placed in Information field of PPP frame.
622  */
623 int
624 pppoutput(ifp, m0, dst, rtp)
625     struct ifnet *ifp;
626     struct mbuf *m0;
627     struct sockaddr *dst;
628     struct rtentry *rtp;
629 {
630     register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
631     int protocol, address, control;
632     u_char *cp;
633     int s, error;
634     struct ip *ip;
635     struct ifqueue *ifq;
636     enum NPmode mode;
637     int active, len;
638     struct mbuf *m;
639
640     if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
641         || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
642         error = ENETDOWN;       /* sort of */
643         goto bad;
644     }
645
646     /*
647      * Compute PPP header.
648      */
649     m0->m_flags &= ~M_HIGHPRI;
650     switch (dst->sa_family) {
651 #ifdef INET
652     case AF_INET:
653         address = PPP_ALLSTATIONS;
654         control = PPP_UI;
655         protocol = PPP_IP;
656         mode = sc->sc_npmode[NP_IP];
657
658         /*
659          * If this packet has the "low delay" bit set in the IP header,
660          * put it on the fastq instead.
661          */
662         ip = mtod(m0, struct ip *);
663         if (ip->ip_tos & IPTOS_LOWDELAY)
664             m0->m_flags |= M_HIGHPRI;
665         break;
666 #endif
667     case AF_UNSPEC:
668         address = PPP_ADDRESS(dst->sa_data);
669         control = PPP_CONTROL(dst->sa_data);
670         protocol = PPP_PROTOCOL(dst->sa_data);
671         mode = NPMODE_PASS;
672         break;
673     default:
674         printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
675         error = EAFNOSUPPORT;
676         goto bad;
677     }
678
679     /*
680      * Drop this packet, or return an error, if necessary.
681      */
682     if (mode == NPMODE_ERROR) {
683         error = ENETDOWN;
684         goto bad;
685     }
686     if (mode == NPMODE_DROP) {
687         error = 0;
688         goto bad;
689     }
690
691     /*
692      * Add PPP header.  If no space in first mbuf, allocate another.
693      * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
694      */
695     if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
696         m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
697         if (m0 == 0) {
698             error = ENOBUFS;
699             goto bad;
700         }
701         m0->m_len = 0;
702     } else
703         m0->m_data -= PPP_HDRLEN;
704
705     cp = mtod(m0, u_char *);
706     *cp++ = address;
707     *cp++ = control;
708     *cp++ = protocol >> 8;
709     *cp++ = protocol & 0xff;
710     m0->m_len += PPP_HDRLEN;
711
712     len = 0;
713     for (m = m0; m != 0; m = m->m_next)
714         len += m->m_len;
715
716     if (sc->sc_flags & SC_LOG_OUTPKT) {
717         printf("ppp%d output: ", ifp->if_unit);
718         pppdumpm(m0);
719     }
720
721     /*
722      * Apply the pass and active filters to the packet,
723      * but only if it is a data packet.
724      */
725     active = 0;
726     if ((protocol & 0x8000) == 0) {
727         *mtod(m0, u_char *) = 1;        /* indicates outbound */
728         if (sc->sc_pass_filt.bf_insns != 0
729             && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
730                           len, 0) == 0) {
731             error = 0;          /* drop this packet */
732             goto bad;
733         }
734
735         /*
736          * Update the time we sent the most recent packet.
737          */
738         if (sc->sc_active_filt.bf_insns == 0
739             || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
740             sc->sc_last_sent = time.tv_sec;
741
742         *mtod(m0, u_char *) = address;
743     }
744
745 #if NBPFILTER > 0
746     /*
747      * See if bpf wants to look at the packet.
748      */
749     if (sc->sc_bpf)
750         bpf_mtap(sc->sc_bpf, m0);
751 #endif
752
753     /*
754      * Put the packet on the appropriate queue.
755      */
756     s = splsoftnet();
757     if (mode == NPMODE_QUEUE) {
758         /* XXX we should limit the number of packets on this queue */
759         *sc->sc_npqtail = m0;
760         m0->m_nextpkt = NULL;
761         sc->sc_npqtail = &m0->m_nextpkt;
762     } else {
763         ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
764         if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
765             IF_DROP(ifq);
766             splx(s);
767             sc->sc_if.if_oerrors++;
768             sc->sc_stats.ppp_oerrors++;
769             error = ENOBUFS;
770             goto bad;
771         }
772         IF_ENQUEUE(ifq, m0);
773         (*sc->sc_start)(sc);
774     }
775     ifp->if_lastchange = time;
776     ifp->if_opackets++;
777     ifp->if_obytes += len;
778
779     splx(s);
780     return (0);
781
782 bad:
783     m_freem(m0);
784     return (error);
785 }
786
787 /*
788  * After a change in the NPmode for some NP, move packets from the
789  * npqueue to the send queue or the fast queue as appropriate.
790  * Should be called at splsoftnet.
791  */
792 static void
793 ppp_requeue(sc)
794     struct ppp_softc *sc;
795 {
796     struct mbuf *m, **mpp;
797     struct ifqueue *ifq;
798     enum NPmode mode;
799
800     for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
801         switch (PPP_PROTOCOL(mtod(m, u_char *))) {
802         case PPP_IP:
803             mode = sc->sc_npmode[NP_IP];
804             break;
805         default:
806             mode = NPMODE_PASS;
807         }
808
809         switch (mode) {
810         case NPMODE_PASS:
811             /*
812              * This packet can now go on one of the queues to be sent.
813              */
814             *mpp = m->m_nextpkt;
815             m->m_nextpkt = NULL;
816             ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
817             if (IF_QFULL(ifq)) {
818                 IF_DROP(ifq);
819                 sc->sc_if.if_oerrors++;
820                 sc->sc_stats.ppp_oerrors++;
821             } else
822                 IF_ENQUEUE(ifq, m);
823             break;
824
825         case NPMODE_DROP:
826         case NPMODE_ERROR:
827             *mpp = m->m_nextpkt;
828             m_freem(m);
829             break;
830
831         case NPMODE_QUEUE:
832             mpp = &m->m_nextpkt;
833             break;
834         }
835     }
836     sc->sc_npqtail = mpp;
837 }
838
839 /*
840  * Get a packet to send.  This procedure is intended to be called at
841  * spltty or splimp, so it takes little time.  If there isn't a packet
842  * waiting to go out, it schedules a software interrupt to prepare a
843  * new packet; the device start routine gets called again when a
844  * packet is ready.
845  */
846 struct mbuf *
847 ppp_dequeue(sc)
848     struct ppp_softc *sc;
849 {
850     struct mbuf *m;
851     int s = splhigh();
852
853     m = sc->sc_togo;
854     if (m) {
855         /*
856          * Had a packet waiting - send it.
857          */
858         sc->sc_togo = NULL;
859         sc->sc_flags |= SC_TBUSY;
860         splx(s);
861         return m;
862     }
863     /*
864      * Remember we wanted a packet and schedule a software interrupt.
865      */
866     sc->sc_flags &= ~SC_TBUSY;
867     schednetisr(NETISR_PPP);
868     splx(s);
869     return NULL;
870 }
871
872 /*
873  * Software interrupt routine, called at splsoftnet.
874  */
875 void
876 pppintr()
877 {
878     struct ppp_softc *sc;
879     int i, s;
880     struct mbuf *m;
881
882     sc = ppp_softc;
883     for (i = 0; i < NPPP; ++i, ++sc) {
884         if (!(sc->sc_flags & SC_TBUSY) && sc->sc_togo == NULL
885             && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head))
886             ppp_outpkt(sc);
887         for (;;) {
888             s = splhigh();
889             IF_DEQUEUE(&sc->sc_rawq, m);
890             splx(s);
891             if (m == NULL)
892                 break;
893             ppp_inproc(sc, m);
894         }
895     }
896 }
897
898 /*
899  * Grab another packet off a queue and apply VJ compression,
900  * packet compression, address/control and/or protocol compression
901  * if enabled.  Should be called at splsoftnet.
902  */
903 static void
904 ppp_outpkt(sc)
905     struct ppp_softc *sc;
906 {
907     struct mbuf *m, *mp;
908     u_char *cp;
909     int address, control, protocol;
910
911     /*
912      * Grab a packet to send: first try the fast queue, then the
913      * normal queue.
914      */
915     IF_DEQUEUE(&sc->sc_fastq, m);
916     if (m == NULL)
917         IF_DEQUEUE(&sc->sc_if.if_snd, m);
918     if (m == NULL)
919         return;
920
921     ++sc->sc_stats.ppp_opackets;
922
923     /*
924      * Extract the ppp header of the new packet.
925      * The ppp header will be in one mbuf.
926      */
927     cp = mtod(m, u_char *);
928     address = PPP_ADDRESS(cp);
929     control = PPP_CONTROL(cp);
930     protocol = PPP_PROTOCOL(cp);
931
932     switch (protocol) {
933     case PPP_IP:
934 #ifdef VJC
935         /*
936          * If the packet is a TCP/IP packet, see if we can compress it.
937          */
938         if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
939             struct ip *ip;
940             int type;
941
942             mp = m;
943             ip = (struct ip *) (cp + PPP_HDRLEN);
944             if (mp->m_len <= PPP_HDRLEN) {
945                 mp = mp->m_next;
946                 if (mp == NULL)
947                     break;
948                 ip = mtod(mp, struct ip *);
949             }
950             /* this code assumes the IP/TCP header is in one non-shared mbuf */
951             if (ip->ip_p == IPPROTO_TCP) {
952                 type = sl_compress_tcp(mp, ip, sc->sc_comp,
953                                        !(sc->sc_flags & SC_NO_TCP_CCID));
954                 switch (type) {
955                 case TYPE_UNCOMPRESSED_TCP:
956                     protocol = PPP_VJC_UNCOMP;
957                     break;
958                 case TYPE_COMPRESSED_TCP:
959                     protocol = PPP_VJC_COMP;
960                     cp = mtod(m, u_char *);
961                     cp[0] = address;    /* header has moved */
962                     cp[1] = control;
963                     cp[2] = 0;
964                     break;
965                 }
966                 cp[3] = protocol;       /* update protocol in PPP header */
967             }
968         }
969 #endif  /* VJC */
970         break;
971
972 #ifdef PPP_COMPRESS
973     case PPP_CCP:
974         ppp_ccp(sc, m, 0);
975         break;
976 #endif  /* PPP_COMPRESS */
977     }
978
979 #ifdef PPP_COMPRESS
980     if (protocol != PPP_LCP && protocol != PPP_CCP
981         && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
982         struct mbuf *mcomp = NULL;
983         int slen, clen;
984
985         slen = 0;
986         for (mp = m; mp != NULL; mp = mp->m_next)
987             slen += mp->m_len;
988         clen = (*sc->sc_xcomp->compress)
989             (sc->sc_xc_state, &mcomp, m, slen,
990              (sc->sc_flags & SC_CCP_UP? sc->sc_if.if_mtu: 0));
991         if (mcomp != NULL) {
992             m_freem(m);
993             m = mcomp;
994             cp = mtod(m, u_char *);
995             protocol = cp[3];
996         }
997     }
998 #endif  /* PPP_COMPRESS */
999
1000     /*
1001      * Compress the address/control and protocol, if possible.
1002      */
1003     if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1004         control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1005         protocol != PPP_LCP) {
1006         /* can compress address/control */
1007         m->m_data += 2;
1008         m->m_len -= 2;
1009     }
1010     if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1011         /* can compress protocol */
1012         if (mtod(m, u_char *) == cp) {
1013             cp[2] = cp[1];      /* move address/control up */
1014             cp[1] = cp[0];
1015         }
1016         ++m->m_data;
1017         --m->m_len;
1018     }
1019
1020     sc->sc_togo = m;
1021     (*sc->sc_start)(sc);
1022 }
1023
1024 #ifdef PPP_COMPRESS
1025 /*
1026  * Handle a CCP packet.  `rcvd' is 1 if the packet was received,
1027  * 0 if it is about to be transmitted.
1028  */
1029 static void
1030 ppp_ccp(sc, m, rcvd)
1031     struct ppp_softc *sc;
1032     struct mbuf *m;
1033     int rcvd;
1034 {
1035     u_char *dp, *ep;
1036     struct mbuf *mp;
1037     int slen, s;
1038
1039     /*
1040      * Get a pointer to the data after the PPP header.
1041      */
1042     if (m->m_len <= PPP_HDRLEN) {
1043         mp = m->m_next;
1044         if (mp == NULL)
1045             return;
1046         dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1047     } else {
1048         mp = m;
1049         dp = mtod(mp, u_char *) + PPP_HDRLEN;
1050     }
1051
1052     ep = mtod(mp, u_char *) + mp->m_len;
1053     if (dp + CCP_HDRLEN > ep)
1054         return;
1055     slen = CCP_LENGTH(dp);
1056     if (dp + slen > ep) {
1057         if (sc->sc_flags & SC_DEBUG)
1058             printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1059                    dp, slen, mtod(mp, u_char *), mp->m_len);
1060         return;
1061     }
1062
1063     switch (CCP_CODE(dp)) {
1064     case CCP_CONFREQ:
1065     case CCP_TERMREQ:
1066     case CCP_TERMACK:
1067         /* CCP must be going down - disable compression */
1068         if (sc->sc_flags & SC_CCP_UP) {
1069             s = splhigh();
1070             sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1071             splx(s);
1072         }
1073         break;
1074
1075     case CCP_CONFACK:
1076         if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1077             && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1078             && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1079             if (!rcvd) {
1080                 /* we're agreeing to send compressed packets. */
1081                 if (sc->sc_xc_state != NULL
1082                     && (*sc->sc_xcomp->comp_init)
1083                         (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1084                          sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) {
1085                     s = splhigh();
1086                     sc->sc_flags |= SC_COMP_RUN;
1087                     splx(s);
1088                 }
1089             } else {
1090                 /* peer is agreeing to send compressed packets. */
1091                 if (sc->sc_rc_state != NULL
1092                     && (*sc->sc_rcomp->decomp_init)
1093                         (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1094                          sc->sc_if.if_unit, 0, sc->sc_mru,
1095                          sc->sc_flags & SC_DEBUG)) {
1096                     s = splhigh();
1097                     sc->sc_flags |= SC_DECOMP_RUN;
1098                     sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1099                     splx(s);
1100                 }
1101             }
1102         }
1103         break;
1104
1105     case CCP_RESETACK:
1106         if (sc->sc_flags & SC_CCP_UP) {
1107             if (!rcvd) {
1108                 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1109                     (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1110             } else {
1111                 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1112                     (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1113                     s = splhigh();
1114                     sc->sc_flags &= ~SC_DC_ERROR;
1115                     splx(s);
1116                 }
1117             }
1118         }
1119         break;
1120     }
1121 }
1122
1123 /*
1124  * CCP is down; free (de)compressor state if necessary.
1125  */
1126 static void
1127 ppp_ccp_closed(sc)
1128     struct ppp_softc *sc;
1129 {
1130     if (sc->sc_xc_state) {
1131         (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1132         sc->sc_xc_state = NULL;
1133     }
1134     if (sc->sc_rc_state) {
1135         (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1136         sc->sc_rc_state = NULL;
1137     }
1138 }
1139 #endif /* PPP_COMPRESS */
1140
1141 /*
1142  * PPP packet input routine.
1143  * The caller has checked and removed the FCS and has inserted
1144  * the address/control bytes and the protocol high byte if they
1145  * were omitted.
1146  */
1147 void
1148 ppppktin(sc, m, lost)
1149     struct ppp_softc *sc;
1150     struct mbuf *m;
1151     int lost;
1152 {
1153     int s = splhigh();
1154
1155     if (lost)
1156         m->m_flags |= M_ERRMARK;
1157     IF_ENQUEUE(&sc->sc_rawq, m);
1158     schednetisr(NETISR_PPP);
1159     splx(s);
1160 }
1161
1162 /*
1163  * Process a received PPP packet, doing decompression as necessary.
1164  * Should be called at splsoftnet.
1165  */
1166 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1167                          TYPE_UNCOMPRESSED_TCP)
1168
1169 static void
1170 ppp_inproc(sc, m)
1171     struct ppp_softc *sc;
1172     struct mbuf *m;
1173 {
1174     struct ifnet *ifp = &sc->sc_if;
1175     struct ifqueue *inq;
1176     int s, ilen, xlen, proto, rv;
1177     u_char *cp, adrs, ctrl;
1178     struct mbuf *mp, *dmp = NULL;
1179     u_char *iphdr;
1180     u_int hlen;
1181
1182     sc->sc_stats.ppp_ipackets++;
1183
1184     if (sc->sc_flags & SC_LOG_INPKT) {
1185         ilen = 0;
1186         for (mp = m; mp != NULL; mp = mp->m_next)
1187             ilen += mp->m_len;
1188         printf("ppp%d: got %d bytes\n", ifp->if_unit, ilen);
1189         pppdumpm(m);
1190     }
1191
1192     cp = mtod(m, u_char *);
1193     adrs = PPP_ADDRESS(cp);
1194     ctrl = PPP_CONTROL(cp);
1195     proto = PPP_PROTOCOL(cp);
1196
1197     if (m->m_flags & M_ERRMARK) {
1198         m->m_flags &= ~M_ERRMARK;
1199         s = splhigh();
1200         sc->sc_flags |= SC_VJ_RESET;
1201         splx(s);
1202     }
1203
1204 #ifdef PPP_COMPRESS
1205     /*
1206      * Decompress this packet if necessary, update the receiver's
1207      * dictionary, or take appropriate action on a CCP packet.
1208      */
1209     if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1210         && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1211         /* decompress this packet */
1212         rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1213         if (rv == DECOMP_OK) {
1214             m_freem(m);
1215             if (dmp == NULL) {
1216                 /* no error, but no decompressed packet produced */
1217                 return;
1218             }
1219             m = dmp;
1220             cp = mtod(m, u_char *);
1221             proto = PPP_PROTOCOL(cp);
1222
1223         } else {
1224             /*
1225              * An error has occurred in decompression.
1226              * Pass the compressed packet up to pppd, which may take
1227              * CCP down or issue a Reset-Req.
1228              */
1229             if (sc->sc_flags & SC_DEBUG)
1230                 printf("ppp%d: decompress failed %d\n", ifp->if_unit, rv);
1231             s = splhigh();
1232             sc->sc_flags |= SC_VJ_RESET;
1233             if (rv == DECOMP_ERROR)
1234                 sc->sc_flags |= SC_DC_ERROR;
1235             else
1236                 sc->sc_flags |= SC_DC_FERROR;
1237             splx(s);
1238         }
1239
1240     } else {
1241         if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1242             (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1243         }
1244         if (proto == PPP_CCP) {
1245             ppp_ccp(sc, m, 1);
1246         }
1247     }
1248 #endif
1249
1250     ilen = 0;
1251     for (mp = m; mp != NULL; mp = mp->m_next)
1252         ilen += mp->m_len;
1253
1254 #ifdef VJC
1255     if (sc->sc_flags & SC_VJ_RESET) {
1256         /*
1257          * If we've missed a packet, we must toss subsequent compressed
1258          * packets which don't have an explicit connection ID.
1259          */
1260         if (sc->sc_comp)
1261             sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1262         s = splhigh();
1263         sc->sc_flags &= ~SC_VJ_RESET;
1264         splx(s);
1265     }
1266
1267     /*
1268      * See if we have a VJ-compressed packet to uncompress.
1269      */
1270     if (proto == PPP_VJC_COMP) {
1271         if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1272             goto bad;
1273
1274         xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1275                                       ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1276                                       sc->sc_comp, &iphdr, &hlen);
1277
1278         if (xlen <= 0) {
1279             if (sc->sc_flags & SC_DEBUG)
1280                 printf("ppp%d: VJ uncompress failed on type comp\n",
1281                         ifp->if_unit);
1282             goto bad;
1283         }
1284
1285         /* Copy the PPP and IP headers into a new mbuf. */
1286         MGETHDR(mp, M_DONTWAIT, MT_DATA);
1287         if (mp == NULL)
1288             goto bad;
1289         mp->m_len = 0;
1290         mp->m_next = NULL;
1291         if (hlen + PPP_HDRLEN > MHLEN) {
1292             MCLGET(mp, M_DONTWAIT);
1293             if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1294                 m_freem(mp);
1295                 goto bad;       /* lose if big headers and no clusters */
1296             }
1297         }
1298         cp = mtod(mp, u_char *);
1299         cp[0] = adrs;
1300         cp[1] = ctrl;
1301         cp[2] = 0;
1302         cp[3] = PPP_IP;
1303         proto = PPP_IP;
1304         bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1305         mp->m_len = hlen + PPP_HDRLEN;
1306
1307         /*
1308          * Trim the PPP and VJ headers off the old mbuf
1309          * and stick the new and old mbufs together.
1310          */
1311         m->m_data += PPP_HDRLEN + xlen;
1312         m->m_len -= PPP_HDRLEN + xlen;
1313         if (m->m_len <= M_TRAILINGSPACE(mp)) {
1314             bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1315             mp->m_len += m->m_len;
1316             MFREE(m, mp->m_next);
1317         } else
1318             mp->m_next = m;
1319         m = mp;
1320         ilen += hlen - xlen;
1321
1322     } else if (proto == PPP_VJC_UNCOMP) {
1323         if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1324             goto bad;
1325
1326         xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1327                                       ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1328                                       sc->sc_comp, &iphdr, &hlen);
1329
1330         if (xlen < 0) {
1331             if (sc->sc_flags & SC_DEBUG)
1332                 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1333                         ifp->if_unit);
1334             goto bad;
1335         }
1336
1337         proto = PPP_IP;
1338         cp[3] = PPP_IP;
1339     }
1340 #endif /* VJC */
1341
1342     /*
1343      * If the packet will fit in a header mbuf, don't waste a
1344      * whole cluster on it.
1345      */
1346     if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1347         MGETHDR(mp, M_DONTWAIT, MT_DATA);
1348         if (mp != NULL) {
1349             m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1350             m_freem(m);
1351             m = mp;
1352             m->m_len = ilen;
1353         }
1354     }
1355     m->m_pkthdr.len = ilen;
1356     m->m_pkthdr.rcvif = ifp;
1357
1358     /*
1359      * See whether we want to pass this packet, and
1360      * if it counts as link activity.
1361      */
1362     if ((proto & 0x8000) == 0) {
1363         adrs = *mtod(m, u_char *);      /* save address field */
1364         *mtod(m, u_char *) = 0;         /* indicate inbound */
1365         if (sc->sc_pass_filt.bf_insns != 0
1366             && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1367                           ilen, 0) == 0) {
1368             /* drop this packet */
1369             m_freem(m);
1370             return;
1371         }
1372         if (sc->sc_active_filt.bf_insns == 0
1373             || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1374             sc->sc_last_recv = time.tv_sec;
1375
1376         *mtod(m, u_char *) = adrs;
1377     }
1378
1379 #if NBPFILTER > 0
1380     /* See if bpf wants to look at the packet. */
1381     if (sc->sc_bpf)
1382         bpf_mtap(sc->sc_bpf, m);
1383 #endif
1384
1385     rv = 0;
1386     switch (proto) {
1387 #ifdef INET
1388     case PPP_IP:
1389         /*
1390          * IP packet - take off the ppp header and pass it up to IP.
1391          */
1392         if ((ifp->if_flags & IFF_UP) == 0
1393             || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1394             /* interface is down - drop the packet. */
1395             m_freem(m);
1396             return;
1397         }
1398         m->m_pkthdr.len -= PPP_HDRLEN;
1399         m->m_data += PPP_HDRLEN;
1400         m->m_len -= PPP_HDRLEN;
1401         schednetisr(NETISR_IP);
1402         inq = &ipintrq;
1403         break;
1404 #endif
1405
1406     default:
1407         /*
1408          * Some other protocol - place on input queue for read().
1409          */
1410         inq = &sc->sc_inq;
1411         rv = 1;
1412         break;
1413     }
1414
1415     /*
1416      * Put the packet on the appropriate input queue.
1417      */
1418     s = splhigh();
1419     if (IF_QFULL(inq)) {
1420         IF_DROP(inq);
1421         splx(s);
1422         if (sc->sc_flags & SC_DEBUG)
1423             printf("ppp%d: input queue full\n", ifp->if_unit);
1424         ifp->if_iqdrops++;
1425         goto bad;
1426     }
1427     IF_ENQUEUE(inq, m);
1428     splx(s);
1429     ifp->if_ipackets++;
1430     ifp->if_ibytes += ilen;
1431     ifp->if_lastchange = time;
1432
1433     if (rv)
1434         (*sc->sc_ctlp)(sc);
1435
1436     return;
1437
1438  bad:
1439     m_freem(m);
1440     sc->sc_if.if_ierrors++;
1441     sc->sc_stats.ppp_ierrors++;
1442 }
1443
1444 #define MAX_DUMP_BYTES  128
1445
1446 static void
1447 pppdumpm(m0)
1448     struct mbuf *m0;
1449 {
1450     char buf[3*MAX_DUMP_BYTES+4];
1451     char *bp = buf;
1452     struct mbuf *m;
1453     static char digits[] = "0123456789abcdef";
1454
1455     for (m = m0; m; m = m->m_next) {
1456         int l = m->m_len;
1457         u_char *rptr = (u_char *)m->m_data;
1458
1459         while (l--) {
1460             if (bp > buf + sizeof(buf) - 4)
1461                 goto done;
1462             *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1463             *bp++ = digits[*rptr++ & 0xf];
1464         }
1465
1466         if (m->m_next) {
1467             if (bp > buf + sizeof(buf) - 3)
1468                 goto done;
1469             *bp++ = '|';
1470         } else
1471             *bp++ = ' ';
1472     }
1473 done:
1474     if (m)
1475         *bp++ = '>';
1476     *bp = 0;
1477     printf("%s\n", buf);
1478 }
1479
1480 #endif  /* NPPP > 0 */