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