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