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