]> git.ozlabs.org Git - ppp.git/blob - freebsd-2.0/if_ppp.c
Fix an fd leak on the discovery socket.
[ppp.git] / freebsd-2.0 / if_ppp.c
1 /*
2  * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
3  *
4  * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  *
18  * 3. The name "Carnegie Mellon University" must not be used to
19  *    endorse or promote products derived from this software without
20  *    prior written permission. For permission or any legal
21  *    details, please contact
22  *      Office of Technology Transfer
23  *      Carnegie Mellon University
24  *      5000 Forbes Avenue
25  *      Pittsburgh, PA  15213-3890
26  *      (412) 268-4387, fax: (412) 268-7395
27  *      tech-transfer@andrew.cmu.edu
28  *
29  * 4. Redistributions of any form whatsoever must retain the following
30  *    acknowledgment:
31  *    "This product includes software developed by Computing Services
32  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
33  *
34  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
35  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
36  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
37  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
38  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
39  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
40  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
41  *
42  * Drew D. Perkins
43  * Carnegie Mellon University
44  * 4910 Forbes Ave.
45  * Pittsburgh, PA 15213
46  * (412) 268-8576
47  * ddp@andrew.cmu.edu
48  *
49  * Based on:
50  *      @(#)if_sl.c     7.6.1.2 (Berkeley) 2/15/89
51  *
52  * Copyright (c) 1987 Regents of the University of California.
53  * All rights reserved.
54  *
55  * Redistribution and use in source and binary forms are permitted
56  * provided that the above copyright notice and this paragraph are
57  * duplicated in all such forms and that any documentation,
58  * advertising materials, and other materials related to such
59  * distribution and use acknowledge that the software was developed
60  * by the University of California, Berkeley.  The name of the
61  * University may not be used to endorse or promote products derived
62  * from this software without specific prior written permission.
63  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
64  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
65  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
66  *
67  * Serial Line interface
68  *
69  * Rick Adams
70  * Center for Seismic Studies
71  * 1300 N 17th Street, Suite 1450
72  * Arlington, Virginia 22209
73  * (703)276-7900
74  * rick@seismo.ARPA
75  * seismo!rick
76  *
77  * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
78  * Converted to 4.3BSD Beta by Chris Torek.
79  * Other changes made at Berkeley, based in part on code by Kirk Smith.
80  *
81  * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
82  * Added VJ tcp header compression; more unified ioctls
83  *
84  * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au).
85  * Cleaned up a lot of the mbuf-related code to fix bugs that
86  * caused system crashes and packet corruption.  Changed pppstart
87  * so that it doesn't just give up with a collision if the whole
88  * packet doesn't fit in the output ring buffer.
89  *
90  * Added priority queueing for interactive IP packets, following
91  * the model of if_sl.c, plus hooks for bpf.
92  * Paul Mackerras (paulus@cs.anu.edu.au).
93  */
94
95 /* $Id: if_ppp.c,v 1.15 2002/12/06 09:49:15 paulus Exp $ */
96 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
97 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
98
99 #include "ppp.h"
100 #if NPPP > 0
101
102 #define VJC
103 #define PPP_COMPRESS
104
105 #include <sys/param.h>
106 #include <sys/systm.h>
107 #include <sys/proc.h>
108 #include <sys/mbuf.h>
109 #include <sys/socket.h>
110 #include <sys/ioctl.h>
111 #include <sys/kernel.h>
112 #include <sys/time.h>
113 #include <sys/malloc.h>
114
115 #include <net/if.h>
116 #include <net/if_types.h>
117 #include <net/netisr.h>
118 #include <net/route.h>
119 #ifdef PPP_FILTER
120 #include <net/bpf.h>
121 #endif
122
123 #if INET
124 #include <netinet/in.h>
125 #include <netinet/in_systm.h>
126 #include <netinet/in_var.h>
127 #include <netinet/ip.h>
128 #endif
129
130 #include "bpfilter.h"
131 #if NBPFILTER > 0
132 #include <net/bpf.h>
133 #endif
134
135 #ifdef VJC
136 #include <net/pppcompress.h>
137 #endif
138
139 #include <net/ppp_defs.h>
140 #include <net/if_ppp.h>
141 #include <net/if_pppvar.h>
142 #include <machine/cpu.h>
143
144 #define splsoftnet      splnet
145
146 #ifndef NETISR_PPP
147 /* This definition should be moved to net/netisr.h */
148 #define NETISR_PPP      26      /* PPP software interrupt */
149 #endif
150
151 #ifdef PPP_COMPRESS
152 #define PACKETPTR       struct mbuf *
153 #include <net/ppp-comp.h>
154 #endif
155
156 static int      pppsioctl __P((struct ifnet *, int, caddr_t));
157 static void     ppp_requeue __P((struct ppp_softc *));
158 static void     ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
159 static void     ppp_ccp_closed __P((struct ppp_softc *));
160 static void     ppp_inproc __P((struct ppp_softc *, struct mbuf *));
161 static void     pppdumpm __P((struct mbuf *m0));
162
163 /*
164  * Some useful mbuf macros not in mbuf.h.
165  */
166 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
167
168 #define M_DATASTART(m)  \
169         (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
170             (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
171
172 #define M_DATASIZE(m)   \
173         (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
174             (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
175
176 /*
177  * We steal two bits in the mbuf m_flags, to mark high-priority packets
178  * for output, and received packets following lost/corrupted packets.
179  */
180 #define M_HIGHPRI       0x2000  /* output packet for sc_fastq */
181 #define M_ERRMARK       0x4000  /* steal a bit in mbuf m_flags */
182
183
184 #ifdef PPP_COMPRESS
185 /*
186  * List of compressors we know about.
187  * We leave some space so maybe we can modload compressors.
188  */
189
190 extern struct compressor ppp_bsd_compress;
191 extern struct compressor ppp_deflate, ppp_deflate_draft;
192
193 struct compressor *ppp_compressors[8] = {
194 #if DO_BSD_COMPRESS
195     &ppp_bsd_compress,
196 #endif
197 #if DO_DEFLATE
198     &ppp_deflate,
199     &ppp_deflate_draft,
200 #endif
201     NULL
202 };
203 #endif /* PPP_COMPRESS */
204
205 TEXT_SET(pseudo_set, pppattach);
206
207 /*
208  * Called from boot code to establish ppp interfaces.
209  */
210 void
211 pppattach()
212 {
213     register struct ppp_softc *sc;
214     register int i = 0;
215     extern void (*netisrs[])__P((void));
216
217     for (sc = ppp_softc; i < NPPP; sc++) {
218         sc->sc_if.if_name = "ppp";
219         sc->sc_if.if_unit = i++;
220         sc->sc_if.if_mtu = PPP_MTU;
221         sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
222         sc->sc_if.if_type = IFT_PPP;
223         sc->sc_if.if_hdrlen = PPP_HDRLEN;
224         sc->sc_if.if_ioctl = pppsioctl;
225         sc->sc_if.if_output = pppoutput;
226         sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
227         sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
228         sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
229         sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
230         if_attach(&sc->sc_if);
231 #if NBPFILTER > 0
232         bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN);
233 #endif
234     }
235     netisrs[NETISR_PPP] = pppintr;
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 vjcompress *, sizeof(struct vjcompress),
265            M_DEVBUF, M_NOWAIT);
266     if (sc->sc_comp)
267         vj_compress_init(sc->sc_comp, -1);
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     int 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_if.if_unit;
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             vj_compress_init(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("ppp%d: comp_alloc failed\n",
456                                sc->sc_if.if_unit);
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("ppp%d: decomp_alloc failed\n",
471                                sc->sc_if.if_unit);
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("ppp%d: no compressor for [%x %x %x], %x\n",
482                    sc->sc_if.if_unit, 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 = splsoftnet();
504                 sc->sc_npmode[npx] = npi->mode;
505                 if (npi->mode != NPMODE_QUEUE) {
506                     ppp_requeue(sc);
507                     (*sc->sc_start)(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     int cmd;
569     caddr_t data;
570 {
571     struct proc *p = curproc;   /* XXX */
572     register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
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 = &ppp_softc[ifp->if_unit];
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("ppp%d: af%d not supported\n", ifp->if_unit, 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("ppp%d output: ", ifp->if_unit);
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 data 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 = splsoftnet();
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         (*sc->sc_start)(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 splsoftnet.
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
915     /*
916      * Grab a packet to send: first try the fast queue, then the
917      * normal queue.
918      */
919     IF_DEQUEUE(&sc->sc_fastq, m);
920     if (m == NULL)
921         IF_DEQUEUE(&sc->sc_if.if_snd, m);
922     if (m == NULL)
923         return NULL;
924
925     ++sc->sc_stats.ppp_opackets;
926
927     /*
928      * Extract the ppp header of the new packet.
929      * The ppp header will be in one mbuf.
930      */
931     cp = mtod(m, u_char *);
932     address = PPP_ADDRESS(cp);
933     control = PPP_CONTROL(cp);
934     protocol = PPP_PROTOCOL(cp);
935
936     switch (protocol) {
937     case PPP_IP:
938 #ifdef VJC
939         /*
940          * If the packet is a TCP/IP packet, see if we can compress it.
941          */
942         if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
943             struct ip *ip;
944             int type;
945
946             mp = m;
947             ip = (struct ip *) (cp + PPP_HDRLEN);
948             if (mp->m_len <= PPP_HDRLEN) {
949                 mp = mp->m_next;
950                 if (mp == NULL)
951                     break;
952                 ip = mtod(mp, struct ip *);
953             }
954             /* this code assumes the IP/TCP header is in one non-shared mbuf */
955             if (ip->ip_p == IPPROTO_TCP) {
956                 type = vj_compress_tcp(mp, ip, sc->sc_comp,
957                                        !(sc->sc_flags & SC_NO_TCP_CCID));
958                 switch (type) {
959                 case TYPE_UNCOMPRESSED_TCP:
960                     protocol = PPP_VJC_UNCOMP;
961                     break;
962                 case TYPE_COMPRESSED_TCP:
963                     protocol = PPP_VJC_COMP;
964                     cp = mtod(m, u_char *);
965                     cp[0] = address;    /* header has moved */
966                     cp[1] = control;
967                     cp[2] = 0;
968                     break;
969                 }
970                 cp[3] = protocol;       /* update protocol in PPP header */
971             }
972         }
973 #endif  /* VJC */
974         break;
975
976 #ifdef PPP_COMPRESS
977     case PPP_CCP:
978         ppp_ccp(sc, m, 0);
979         break;
980 #endif  /* PPP_COMPRESS */
981     }
982
983 #ifdef PPP_COMPRESS
984     if (protocol != PPP_LCP && protocol != PPP_CCP
985         && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
986         struct mbuf *mcomp = NULL;
987         int slen, clen;
988
989         slen = 0;
990         for (mp = m; mp != NULL; mp = mp->m_next)
991             slen += mp->m_len;
992         clen = (*sc->sc_xcomp->compress)
993             (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
994         if (mcomp != NULL) {
995             if (sc->sc_flags & SC_CCP_UP) {
996                 /* Send the compressed packet instead of the original. */
997                 m_freem(m);
998                 m = mcomp;
999                 cp = mtod(m, u_char *);
1000                 protocol = cp[3];
1001             } else {
1002                 /* Can't transmit compressed packets until CCP is up. */
1003                 m_freem(mcomp);
1004             }
1005         }
1006     }
1007 #endif  /* PPP_COMPRESS */
1008
1009     /*
1010      * Compress the address/control and protocol, if possible.
1011      */
1012     if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1013         control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1014         protocol != PPP_LCP) {
1015         /* can compress address/control */
1016         m->m_data += 2;
1017         m->m_len -= 2;
1018     }
1019     if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1020         /* can compress protocol */
1021         if (mtod(m, u_char *) == cp) {
1022             cp[2] = cp[1];      /* move address/control up */
1023             cp[1] = cp[0];
1024         }
1025         ++m->m_data;
1026         --m->m_len;
1027     }
1028
1029     return m;
1030 }
1031
1032 /*
1033  * Software interrupt routine, called at splsoftnet.
1034  */
1035 void
1036 pppintr()
1037 {
1038     struct ppp_softc *sc;
1039     int i, s, s2;
1040     struct mbuf *m;
1041
1042     sc = ppp_softc;
1043     s = splsoftnet();
1044     for (i = 0; i < NPPP; ++i, ++sc) {
1045         if (!(sc->sc_flags & SC_TBUSY)
1046             && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
1047             s2 = splimp();
1048             sc->sc_flags |= SC_TBUSY;
1049             splx(s2);
1050             (*sc->sc_start)(sc);
1051         }
1052         for (;;) {
1053             s2 = splimp();
1054             IF_DEQUEUE(&sc->sc_rawq, m);
1055             splx(s2);
1056             if (m == NULL)
1057                 break;
1058             ppp_inproc(sc, m);
1059         }
1060     }
1061     splx(s);
1062 }
1063
1064 #ifdef PPP_COMPRESS
1065 /*
1066  * Handle a CCP packet.  `rcvd' is 1 if the packet was received,
1067  * 0 if it is about to be transmitted.
1068  */
1069 static void
1070 ppp_ccp(sc, m, rcvd)
1071     struct ppp_softc *sc;
1072     struct mbuf *m;
1073     int rcvd;
1074 {
1075     u_char *dp, *ep;
1076     struct mbuf *mp;
1077     int slen, s;
1078
1079     /*
1080      * Get a pointer to the data after the PPP header.
1081      */
1082     if (m->m_len <= PPP_HDRLEN) {
1083         mp = m->m_next;
1084         if (mp == NULL)
1085             return;
1086         dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1087     } else {
1088         mp = m;
1089         dp = mtod(mp, u_char *) + PPP_HDRLEN;
1090     }
1091
1092     ep = mtod(mp, u_char *) + mp->m_len;
1093     if (dp + CCP_HDRLEN > ep)
1094         return;
1095     slen = CCP_LENGTH(dp);
1096     if (dp + slen > ep) {
1097         if (sc->sc_flags & SC_DEBUG)
1098             printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1099                    dp, slen, mtod(mp, u_char *), mp->m_len);
1100         return;
1101     }
1102
1103     switch (CCP_CODE(dp)) {
1104     case CCP_CONFREQ:
1105     case CCP_TERMREQ:
1106     case CCP_TERMACK:
1107         /* CCP must be going down - disable compression */
1108         if (sc->sc_flags & SC_CCP_UP) {
1109             s = splimp();
1110             sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1111             splx(s);
1112         }
1113         break;
1114
1115     case CCP_CONFACK:
1116         if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1117             && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1118             && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1119             if (!rcvd) {
1120                 /* we're agreeing to send compressed packets. */
1121                 if (sc->sc_xc_state != NULL
1122                     && (*sc->sc_xcomp->comp_init)
1123                         (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1124                          sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) {
1125                     s = splimp();
1126                     sc->sc_flags |= SC_COMP_RUN;
1127                     splx(s);
1128                 }
1129             } else {
1130                 /* peer is agreeing to send compressed packets. */
1131                 if (sc->sc_rc_state != NULL
1132                     && (*sc->sc_rcomp->decomp_init)
1133                         (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1134                          sc->sc_if.if_unit, 0, sc->sc_mru,
1135                          sc->sc_flags & SC_DEBUG)) {
1136                     s = splimp();
1137                     sc->sc_flags |= SC_DECOMP_RUN;
1138                     sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1139                     splx(s);
1140                 }
1141             }
1142         }
1143         break;
1144
1145     case CCP_RESETACK:
1146         if (sc->sc_flags & SC_CCP_UP) {
1147             if (!rcvd) {
1148                 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1149                     (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1150             } else {
1151                 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1152                     (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1153                     s = splimp();
1154                     sc->sc_flags &= ~SC_DC_ERROR;
1155                     splx(s);
1156                 }
1157             }
1158         }
1159         break;
1160     }
1161 }
1162
1163 /*
1164  * CCP is down; free (de)compressor state if necessary.
1165  */
1166 static void
1167 ppp_ccp_closed(sc)
1168     struct ppp_softc *sc;
1169 {
1170     if (sc->sc_xc_state) {
1171         (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1172         sc->sc_xc_state = NULL;
1173     }
1174     if (sc->sc_rc_state) {
1175         (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1176         sc->sc_rc_state = NULL;
1177     }
1178 }
1179 #endif /* PPP_COMPRESS */
1180
1181 /*
1182  * PPP packet input routine.
1183  * The caller has checked and removed the FCS and has inserted
1184  * the address/control bytes and the protocol high byte if they
1185  * were omitted.
1186  */
1187 void
1188 ppppktin(sc, m, lost)
1189     struct ppp_softc *sc;
1190     struct mbuf *m;
1191     int lost;
1192 {
1193     int s = splimp();
1194
1195     if (lost)
1196         m->m_flags |= M_ERRMARK;
1197     IF_ENQUEUE(&sc->sc_rawq, m);
1198     schednetisr(NETISR_PPP);
1199     splx(s);
1200 }
1201
1202 /*
1203  * Process a received PPP packet, doing decompression as necessary.
1204  * Should be called at splsoftnet.
1205  */
1206 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1207                          TYPE_UNCOMPRESSED_TCP)
1208
1209 static void
1210 ppp_inproc(sc, m)
1211     struct ppp_softc *sc;
1212     struct mbuf *m;
1213 {
1214     struct ifnet *ifp = &sc->sc_if;
1215     struct ifqueue *inq;
1216     int s, ilen, xlen, proto, rv;
1217     u_char *cp, adrs, ctrl;
1218     struct mbuf *mp, *dmp = NULL;
1219     u_char *iphdr;
1220     u_int hlen;
1221
1222     sc->sc_stats.ppp_ipackets++;
1223
1224     if (sc->sc_flags & SC_LOG_INPKT) {
1225         ilen = 0;
1226         for (mp = m; mp != NULL; mp = mp->m_next)
1227             ilen += mp->m_len;
1228         printf("ppp%d: got %d bytes\n", ifp->if_unit, ilen);
1229         pppdumpm(m);
1230     }
1231
1232     cp = mtod(m, u_char *);
1233     adrs = PPP_ADDRESS(cp);
1234     ctrl = PPP_CONTROL(cp);
1235     proto = PPP_PROTOCOL(cp);
1236
1237     if (m->m_flags & M_ERRMARK) {
1238         m->m_flags &= ~M_ERRMARK;
1239         s = splimp();
1240         sc->sc_flags |= SC_VJ_RESET;
1241         splx(s);
1242     }
1243
1244 #ifdef PPP_COMPRESS
1245     /*
1246      * Decompress this packet if necessary, update the receiver's
1247      * dictionary, or take appropriate action on a CCP packet.
1248      */
1249     if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1250         && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1251         /* decompress this packet */
1252         rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1253         if (rv == DECOMP_OK) {
1254             m_freem(m);
1255             if (dmp == NULL) {
1256                 /* no error, but no decompressed packet produced */
1257                 return;
1258             }
1259             m = dmp;
1260             cp = mtod(m, u_char *);
1261             proto = PPP_PROTOCOL(cp);
1262
1263         } else {
1264             /*
1265              * An error has occurred in decompression.
1266              * Pass the compressed packet up to pppd, which may take
1267              * CCP down or issue a Reset-Req.
1268              */
1269             if (sc->sc_flags & SC_DEBUG)
1270                 printf("ppp%d: decompress failed %d\n", ifp->if_unit, rv);
1271             s = splimp();
1272             sc->sc_flags |= SC_VJ_RESET;
1273             if (rv == DECOMP_ERROR)
1274                 sc->sc_flags |= SC_DC_ERROR;
1275             else
1276                 sc->sc_flags |= SC_DC_FERROR;
1277             splx(s);
1278         }
1279
1280     } else {
1281         if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1282             (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1283         }
1284         if (proto == PPP_CCP) {
1285             ppp_ccp(sc, m, 1);
1286         }
1287     }
1288 #endif
1289
1290     ilen = 0;
1291     for (mp = m; mp != NULL; mp = mp->m_next)
1292         ilen += mp->m_len;
1293
1294 #ifdef VJC
1295     if (sc->sc_flags & SC_VJ_RESET) {
1296         /*
1297          * If we've missed a packet, we must toss subsequent compressed
1298          * packets which don't have an explicit connection ID.
1299          */
1300         if (sc->sc_comp)
1301             vj_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1302         s = splimp();
1303         sc->sc_flags &= ~SC_VJ_RESET;
1304         splx(s);
1305     }
1306
1307     /*
1308      * See if we have a VJ-compressed packet to uncompress.
1309      */
1310     if (proto == PPP_VJC_COMP) {
1311         if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1312             goto bad;
1313
1314         xlen = vj_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1315                                       ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1316                                       sc->sc_comp, &iphdr, &hlen);
1317
1318         if (xlen <= 0) {
1319             if (sc->sc_flags & SC_DEBUG)
1320                 printf("ppp%d: VJ uncompress failed on type comp\n",
1321                         ifp->if_unit);
1322             goto bad;
1323         }
1324
1325         /* Copy the PPP and IP headers into a new mbuf. */
1326         MGETHDR(mp, M_DONTWAIT, MT_DATA);
1327         if (mp == NULL)
1328             goto bad;
1329         mp->m_len = 0;
1330         mp->m_next = NULL;
1331         if (hlen + PPP_HDRLEN > MHLEN) {
1332             MCLGET(mp, M_DONTWAIT);
1333             if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1334                 m_freem(mp);
1335                 goto bad;       /* lose if big headers and no clusters */
1336             }
1337         }
1338         cp = mtod(mp, u_char *);
1339         cp[0] = adrs;
1340         cp[1] = ctrl;
1341         cp[2] = 0;
1342         cp[3] = PPP_IP;
1343         proto = PPP_IP;
1344         bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1345         mp->m_len = hlen + PPP_HDRLEN;
1346
1347         /*
1348          * Trim the PPP and VJ headers off the old mbuf
1349          * and stick the new and old mbufs together.
1350          */
1351         m->m_data += PPP_HDRLEN + xlen;
1352         m->m_len -= PPP_HDRLEN + xlen;
1353         if (m->m_len <= M_TRAILINGSPACE(mp)) {
1354             bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1355             mp->m_len += m->m_len;
1356             MFREE(m, mp->m_next);
1357         } else
1358             mp->m_next = m;
1359         m = mp;
1360         ilen += hlen - xlen;
1361
1362     } else if (proto == PPP_VJC_UNCOMP) {
1363         if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1364             goto bad;
1365
1366         xlen = vj_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1367                                       ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1368                                       sc->sc_comp, &iphdr, &hlen);
1369
1370         if (xlen < 0) {
1371             if (sc->sc_flags & SC_DEBUG)
1372                 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1373                         ifp->if_unit);
1374             goto bad;
1375         }
1376
1377         proto = PPP_IP;
1378         cp[3] = PPP_IP;
1379     }
1380 #endif /* VJC */
1381
1382     /*
1383      * If the packet will fit in a header mbuf, don't waste a
1384      * whole cluster on it.
1385      */
1386     if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1387         MGETHDR(mp, M_DONTWAIT, MT_DATA);
1388         if (mp != NULL) {
1389             m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1390             m_freem(m);
1391             m = mp;
1392             m->m_len = ilen;
1393         }
1394     }
1395     m->m_pkthdr.len = ilen;
1396     m->m_pkthdr.rcvif = ifp;
1397
1398     if ((proto & 0x8000) == 0) {
1399 #ifdef PPP_FILTER
1400         /*
1401          * See whether we want to pass this packet, and
1402          * if it counts as link activity.
1403          */
1404         adrs = *mtod(m, u_char *);      /* save address field */
1405         *mtod(m, u_char *) = 0;         /* indicate inbound */
1406         if (sc->sc_pass_filt.bf_insns != 0
1407             && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1408                           ilen, 0) == 0) {
1409             /* drop this packet */
1410             m_freem(m);
1411             return;
1412         }
1413         if (sc->sc_active_filt.bf_insns == 0
1414             || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1415             sc->sc_last_recv = time.tv_sec;
1416
1417         *mtod(m, u_char *) = adrs;
1418 #else
1419         /*
1420          * Record the time that we received this packet.
1421          */
1422         sc->sc_last_recv = time.tv_sec;
1423 #endif /* PPP_FILTER */
1424     }
1425
1426 #if NBPFILTER > 0
1427     /* See if bpf wants to look at the packet. */
1428     if (sc->sc_bpf)
1429         bpf_mtap(sc->sc_bpf, m);
1430 #endif
1431
1432     rv = 0;
1433     switch (proto) {
1434 #ifdef INET
1435     case PPP_IP:
1436         /*
1437          * IP packet - take off the ppp header and pass it up to IP.
1438          */
1439         if ((ifp->if_flags & IFF_UP) == 0
1440             || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1441             /* interface is down - drop the packet. */
1442             m_freem(m);
1443             return;
1444         }
1445         m->m_pkthdr.len -= PPP_HDRLEN;
1446         m->m_data += PPP_HDRLEN;
1447         m->m_len -= PPP_HDRLEN;
1448         schednetisr(NETISR_IP);
1449         inq = &ipintrq;
1450         break;
1451 #endif
1452
1453     default:
1454         /*
1455          * Some other protocol - place on input queue for read().
1456          */
1457         inq = &sc->sc_inq;
1458         rv = 1;
1459         break;
1460     }
1461
1462     /*
1463      * Put the packet on the appropriate input queue.
1464      */
1465     s = splimp();
1466     if (IF_QFULL(inq)) {
1467         IF_DROP(inq);
1468         splx(s);
1469         if (sc->sc_flags & SC_DEBUG)
1470             printf("ppp%d: input queue full\n", ifp->if_unit);
1471         ifp->if_iqdrops++;
1472         goto bad;
1473     }
1474     IF_ENQUEUE(inq, m);
1475     splx(s);
1476     ifp->if_ipackets++;
1477     ifp->if_ibytes += ilen;
1478     ifp->if_lastchange = time;
1479
1480     if (rv)
1481         (*sc->sc_ctlp)(sc);
1482
1483     return;
1484
1485  bad:
1486     m_freem(m);
1487     sc->sc_if.if_ierrors++;
1488     sc->sc_stats.ppp_ierrors++;
1489 }
1490
1491 #define MAX_DUMP_BYTES  128
1492
1493 static void
1494 pppdumpm(m0)
1495     struct mbuf *m0;
1496 {
1497     char buf[3*MAX_DUMP_BYTES+4];
1498     char *bp = buf;
1499     struct mbuf *m;
1500     static char digits[] = "0123456789abcdef";
1501
1502     for (m = m0; m; m = m->m_next) {
1503         int l = m->m_len;
1504         u_char *rptr = (u_char *)m->m_data;
1505
1506         while (l--) {
1507             if (bp > buf + sizeof(buf) - 4)
1508                 goto done;
1509             *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1510             *bp++ = digits[*rptr++ & 0xf];
1511         }
1512
1513         if (m->m_next) {
1514             if (bp > buf + sizeof(buf) - 3)
1515                 goto done;
1516             *bp++ = '|';
1517         } else
1518             *bp++ = ' ';
1519     }
1520 done:
1521     if (m)
1522         *bp++ = '>';
1523     *bp = 0;
1524     printf("%s\n", buf);
1525 }
1526
1527 #endif  /* NPPP > 0 */