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