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