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