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