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