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