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