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