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