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