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