Split ifnet serialize step 1/many: Add if_{serialize,deserialize,tryserialize}()
[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
102 #include <net/if.h>
103 #include <net/if_types.h>
104 #include <net/ifq_var.h>
105 #include <net/netisr.h>
106 #include <net/bpf.h>
107
108 #ifdef INET
109 #include <netinet/in.h>
110 #include <netinet/in_systm.h>
111 #include <netinet/in_var.h>
112 #include <netinet/ip.h>
113 #endif
114
115 #ifdef IPX
116 #include <netproto/ipx/ipx.h>
117 #include <netproto/ipx/ipx_if.h>
118 #endif
119
120 #ifdef VJC
121 #include <net/slcompress.h>
122 #endif
123
124 #include "if_ppp.h"
125 #include "if_pppvar.h"
126
127 /* minimise diffs */
128 #ifndef splsoftnet
129 #define splsoftnet      splnet
130 #endif
131
132 #ifdef PPP_COMPRESS
133 #define PACKETPTR       struct mbuf *
134 #include <net/ppp_layer/ppp_comp.h>
135 #endif
136
137 struct ppp_softc ppp_softc[NPPP];
138
139 /* XXX layering violation */
140 extern void     pppasyncattach (void *);
141
142 static void     pppattach (void *);
143 PSEUDO_SET(pppattach, if_ppp);
144
145 static int      pppsioctl (struct ifnet *ifp, u_long cmd, caddr_t data,
146                            struct ucred *);
147
148 static void     ppp_requeue (struct ppp_softc *);
149 static void     ppp_ccp (struct ppp_softc *, struct mbuf *m, int rcvd);
150 static void     ppp_ccp_closed (struct ppp_softc *);
151 static void     ppp_inproc (struct ppp_softc *, struct mbuf *);
152 static void     pppdumpm (struct mbuf *m0);
153 static void     ppp_ifstart(struct ifnet *ifp);
154
155 /*
156  * Some useful mbuf macros not in mbuf.h.
157  */
158 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
159
160 #define M_DATASTART(m)  \
161         (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
162             (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
163
164 #define M_DATASIZE(m)   \
165         (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
166             (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
167
168 /*
169  * We steal two bits in the mbuf m_flags, to mark high-priority packets
170  * for output, and received packets following lost/corrupted packets.
171  */
172 #define M_HIGHPRI       M_PROTO2        /* output packet for sc_fastq */
173 #define M_ERRMARK       M_PROTO3        /* steal a bit in mbuf m_flags */
174
175
176 #ifdef PPP_COMPRESS
177 /*
178  * List of compressors we know about.
179  * We leave some space so maybe we can modload compressors.
180  */
181
182 extern struct compressor ppp_bsd_compress;
183 extern struct compressor ppp_deflate, ppp_deflate_draft;
184
185 static struct compressor *ppp_compressors[8] = {
186 #if DO_BSD_COMPRESS && defined(PPP_BSDCOMP)
187     &ppp_bsd_compress,
188 #endif
189 #if DO_DEFLATE && defined(PPP_DEFLATE)
190     &ppp_deflate,
191     &ppp_deflate_draft,
192 #endif
193     NULL
194 };
195 #endif /* PPP_COMPRESS */
196
197 /*
198  * Software interrupt routine, called at spl[soft]net.
199  */
200 static void
201 pppintr(struct netmsg *msg)
202 {
203     struct mbuf *m;
204     struct ppp_softc *sc;
205     int i;
206
207     /*
208      * Packets are never sent to this netisr so the message must always
209      * be replied.  Interlock processing and notification by replying
210      * the message first.
211      */
212     lwkt_replymsg(&msg->nm_lmsg, 0);
213
214     sc = ppp_softc;
215     for (i = 0; i < NPPP; ++i, ++sc) {
216         ifnet_serialize_all(&sc->sc_if);
217         if (!(sc->sc_flags & SC_TBUSY)
218             && (!ifq_is_empty(&sc->sc_if.if_snd) || !IF_QEMPTY(&sc->sc_fastq))) {
219             sc->sc_flags |= SC_TBUSY;
220             (*sc->sc_start)(sc);
221         } 
222         for (;;) {
223             IF_DEQUEUE(&sc->sc_rawq, m);
224             if (m == NULL)
225                 break;
226             ppp_inproc(sc, m);
227         }
228         ifnet_deserialize_all(&sc->sc_if);
229     }
230 }
231
232 /*
233  * Called from boot code to establish ppp interfaces.
234  */
235 static void
236 pppattach(void *dummy)
237 {
238     struct ppp_softc *sc;
239     int i = 0;
240
241     for (sc = ppp_softc; i < NPPP; sc++) {
242         if_initname(&(sc->sc_if), "ppp", i++);
243         sc->sc_if.if_softc = sc;
244         sc->sc_if.if_mtu = PPP_MTU;
245         sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
246         sc->sc_if.if_type = IFT_PPP;
247         sc->sc_if.if_hdrlen = PPP_HDRLEN;
248         sc->sc_if.if_ioctl = pppsioctl;
249         sc->sc_if.if_output = pppoutput;
250         sc->sc_if.if_start = ppp_ifstart;
251         ifq_set_maxlen(&sc->sc_if.if_snd, IFQ_MAXLEN);
252         ifq_set_ready(&sc->sc_if.if_snd);
253         sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
254         sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
255         sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
256         callout_init(&sc->sc_timeout);
257         if_attach(&sc->sc_if, NULL);
258         bpfattach(&sc->sc_if, DLT_PPP, PPP_HDRLEN);
259     }
260     netisr_register(NETISR_PPP, cpu0_portfn, pktinfo_portfn_cpu0,
261                     pppintr, NETISR_FLAG_NOTMPSAFE);
262     /*
263      * XXX layering violation - if_ppp can work over any lower level
264      * transport that cares to attach to it.
265      */
266     pppasyncattach(dummy);
267 }
268
269 /*
270  * Allocate a ppp interface unit and initialize it.
271  */
272 struct ppp_softc *
273 pppalloc(struct thread *td)
274 {
275     int nppp, i;
276     struct ppp_softc *sc;
277
278     for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
279         if (sc->sc_xfer == td) {
280             sc->sc_xfer = NULL;
281             return sc;
282         }
283     for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
284         if (sc->sc_devp == NULL)
285             break;
286     if (nppp >= NPPP)
287         return NULL;
288
289     sc->sc_flags = 0;
290     sc->sc_mru = PPP_MRU;
291     sc->sc_relinq = NULL;
292     bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
293 #ifdef VJC
294     MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
295            M_DEVBUF, M_WAITOK);
296     sl_compress_init(sc->sc_comp, -1);
297 #endif
298 #ifdef PPP_COMPRESS
299     sc->sc_xc_state = NULL;
300     sc->sc_rc_state = NULL;
301 #endif /* PPP_COMPRESS */
302     for (i = 0; i < NUM_NP; ++i)
303         sc->sc_npmode[i] = NPMODE_ERROR;
304     sc->sc_npqueue = NULL;
305     sc->sc_npqtail = &sc->sc_npqueue;
306     sc->sc_last_sent = sc->sc_last_recv = time_second;
307
308     return sc;
309 }
310
311 /*
312  * Deallocate a ppp unit.  Must be called at splsoftnet or higher.
313  */
314 void
315 pppdealloc(struct ppp_softc *sc)
316 {
317     struct mbuf *m;
318
319     if_down(&sc->sc_if);
320     sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
321     getmicrotime(&sc->sc_if.if_lastchange);
322     sc->sc_devp = NULL;
323     sc->sc_xfer = NULL;
324     for (;;) {
325         IF_DEQUEUE(&sc->sc_rawq, m);
326         if (m == NULL)
327             break;
328         m_freem(m);
329     }
330     for (;;) {
331         IF_DEQUEUE(&sc->sc_inq, m);
332         if (m == NULL)
333             break;
334         m_freem(m);
335     }
336     for (;;) {
337         IF_DEQUEUE(&sc->sc_fastq, m);
338         if (m == NULL)
339             break;
340         m_freem(m);
341     }
342     while ((m = sc->sc_npqueue) != NULL) {
343         sc->sc_npqueue = m->m_nextpkt;
344         m_freem(m);
345     }
346 #ifdef PPP_COMPRESS
347     ppp_ccp_closed(sc);
348     sc->sc_xc_state = NULL;
349     sc->sc_rc_state = NULL;
350 #endif /* PPP_COMPRESS */
351 #ifdef PPP_FILTER
352     if (sc->sc_pass_filt.bf_insns != 0) {
353         kfree(sc->sc_pass_filt.bf_insns, M_DEVBUF);
354         sc->sc_pass_filt.bf_insns = 0;
355         sc->sc_pass_filt.bf_len = 0;
356     }
357     if (sc->sc_active_filt.bf_insns != 0) {
358         kfree(sc->sc_active_filt.bf_insns, M_DEVBUF);
359         sc->sc_active_filt.bf_insns = 0;
360         sc->sc_active_filt.bf_len = 0;
361     }
362 #endif /* PPP_FILTER */
363 #ifdef VJC
364     if (sc->sc_comp != 0) {
365         kfree(sc->sc_comp, M_DEVBUF);
366         sc->sc_comp = 0;
367     }
368 #endif
369 }
370
371 /*
372  * Ioctl routine for generic ppp devices.
373  */
374 int
375 pppioctl(struct ppp_softc *sc, u_long cmd, caddr_t data,
376     int flag, struct ucred *cred)
377 {
378     int error, flags, mru, npx;
379     u_int nb;
380     struct ppp_option_data *odp;
381     struct compressor **cp;
382     struct npioctl *npi;
383     time_t t;
384 #ifdef PPP_FILTER
385     struct bpf_program *bp, *nbp;
386     struct bpf_insn *newcode, *oldcode;
387     int newcodelen;
388 #endif /* PPP_FILTER */
389 #ifdef  PPP_COMPRESS
390     u_char ccp_option[CCP_MAX_OPTION_LENGTH];
391 #endif
392
393     switch (cmd) {
394     case FIONREAD:
395         *(int *)data = sc->sc_inq.ifq_len;
396         break;
397
398     case PPPIOCGUNIT:
399         *(int *)data = sc->sc_if.if_dunit;
400         break;
401
402     case PPPIOCGFLAGS:
403         *(u_int *)data = sc->sc_flags;
404         break;
405
406     case PPPIOCSFLAGS:
407         if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
408             return (error);
409         flags = *(int *)data & SC_MASK;
410         crit_enter();
411 #ifdef PPP_COMPRESS
412         if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
413             ppp_ccp_closed(sc);
414 #endif
415         sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
416         crit_exit();
417         break;
418
419     case PPPIOCSMRU:
420         if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
421             return (error);
422         mru = *(int *)data;
423         if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
424             sc->sc_mru = mru;
425         break;
426
427     case PPPIOCGMRU:
428         *(int *)data = sc->sc_mru;
429         break;
430
431 #ifdef VJC
432     case PPPIOCSMAXCID:
433         if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
434             return (error);
435         if (sc->sc_comp) {
436             crit_enter();
437             sl_compress_init(sc->sc_comp, *(int *)data);
438             crit_exit();
439         }
440         break;
441 #endif
442
443     case PPPIOCXFERUNIT:
444         if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
445             return (error);
446         sc->sc_xfer = curthread;
447         break;
448
449 #ifdef PPP_COMPRESS
450     case PPPIOCSCOMPRESS:
451         if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
452             return (error);
453         odp = (struct ppp_option_data *) data;
454         nb = odp->length;
455         if (nb > sizeof(ccp_option))
456             nb = sizeof(ccp_option);
457         if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
458             return (error);
459         if (ccp_option[1] < 2)  /* preliminary check on the length byte */
460             return (EINVAL);
461         for (cp = ppp_compressors; *cp != NULL; ++cp)
462             if ((*cp)->compress_proto == ccp_option[0]) {
463                 /*
464                  * Found a handler for the protocol - try to allocate
465                  * a compressor or decompressor.
466                  */
467                 error = 0;
468                 if (odp->transmit) {
469                     crit_enter();
470                     if (sc->sc_xc_state != NULL)
471                         (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
472                     sc->sc_xcomp = *cp;
473                     sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
474                     if (sc->sc_xc_state == NULL) {
475                         if (sc->sc_flags & SC_DEBUG)
476                             kprintf("%s: comp_alloc failed\n",
477                                sc->sc_if.if_xname);
478                         error = ENOBUFS;
479                     }
480                     sc->sc_flags &= ~SC_COMP_RUN;
481                     crit_exit();
482                 } else {
483                     crit_enter();
484                     if (sc->sc_rc_state != NULL)
485                         (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
486                     sc->sc_rcomp = *cp;
487                     sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
488                     if (sc->sc_rc_state == NULL) {
489                         if (sc->sc_flags & SC_DEBUG)
490                             kprintf("%s: decomp_alloc failed\n",
491                                sc->sc_if.if_xname);
492                         error = ENOBUFS;
493                     }
494                     sc->sc_flags &= ~SC_DECOMP_RUN;
495                     crit_exit();
496                 }
497                 return (error);
498             }
499         if (sc->sc_flags & SC_DEBUG)
500             kprintf("%s: no compressor for [%x %x %x], %x\n",
501                    sc->sc_if.if_xname, ccp_option[0], ccp_option[1],
502                    ccp_option[2], nb);
503         return (EINVAL);        /* no handler found */
504 #endif /* PPP_COMPRESS */
505
506     case PPPIOCGNPMODE:
507     case PPPIOCSNPMODE:
508         npi = (struct npioctl *) data;
509         switch (npi->protocol) {
510         case PPP_IP:
511             npx = NP_IP;
512             break;
513         default:
514             return EINVAL;
515         }
516         if (cmd == PPPIOCGNPMODE) {
517             npi->mode = sc->sc_npmode[npx];
518         } else {
519             if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
520                 return (error);
521             if (npi->mode != sc->sc_npmode[npx]) {
522                 crit_enter();
523                 sc->sc_npmode[npx] = npi->mode;
524                 if (npi->mode != NPMODE_QUEUE) {
525                     ppp_requeue(sc);
526                     (*sc->sc_start)(sc);
527                 }
528                 crit_exit();
529             }
530         }
531         break;
532
533     case PPPIOCGIDLE:
534         crit_enter();
535         t = time_second;
536         ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
537         ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
538         crit_exit();
539         break;
540
541 #ifdef PPP_FILTER
542     case PPPIOCSPASS:
543     case PPPIOCSACTIVE:
544         nbp = (struct bpf_program *) data;
545         if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
546             return EINVAL;
547         newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
548         if (newcodelen != 0) {
549             MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
550             if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
551                                newcodelen)) != 0) {
552                 kfree(newcode, M_DEVBUF);
553                 return error;
554             }
555             if (!bpf_validate(newcode, nbp->bf_len)) {
556                 kfree(newcode, M_DEVBUF);
557                 return EINVAL;
558             }
559         } else
560             newcode = 0;
561         bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
562         oldcode = bp->bf_insns;
563         crit_enter();
564         bp->bf_len = nbp->bf_len;
565         bp->bf_insns = newcode;
566         crit_exit();
567         if (oldcode != 0)
568             kfree(oldcode, M_DEVBUF);
569         break;
570 #endif
571
572     default:
573         return (ENOIOCTL);
574     }
575     return (0);
576 }
577
578 /*
579  * Process an ioctl request to the ppp network interface.
580  */
581 static int
582 pppsioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
583 {
584     struct ppp_softc *sc = &ppp_softc[ifp->if_dunit];
585     struct ifaddr *ifa = (struct ifaddr *)data;
586     struct ifreq *ifr = (struct ifreq *)data;
587     struct ppp_stats *psp;
588 #ifdef  PPP_COMPRESS
589     struct ppp_comp_stats *pcp;
590 #endif
591     int error = 0;
592
593     crit_enter();
594
595     switch (cmd) {
596     case SIOCSIFFLAGS:
597         if ((ifp->if_flags & IFF_RUNNING) == 0)
598             ifp->if_flags &= ~IFF_UP;
599         break;
600
601     case SIOCSIFADDR:
602     case SIOCAIFADDR:
603         switch(ifa->ifa_addr->sa_family) {
604 #ifdef INET
605         case AF_INET:
606             break;
607 #endif
608 #ifdef IPX
609         case AF_IPX:
610             break;
611 #endif
612         default:
613             error = EAFNOSUPPORT;
614             break;
615         }
616         break;
617
618     case SIOCSIFDSTADDR:
619         switch(ifa->ifa_addr->sa_family) {
620 #ifdef INET
621         case AF_INET:
622             break;
623 #endif
624 #ifdef IPX
625         case AF_IPX:
626             break;
627 #endif
628         default:
629             error = EAFNOSUPPORT;
630             break;
631         }
632         break;
633
634     case SIOCSIFMTU:
635         if ((error = priv_check_cred(cr, PRIV_ROOT, 0)) != 0)
636             break;
637         if (ifr->ifr_mtu > PPP_MAXMTU)
638             error = EINVAL;
639         else {
640             sc->sc_if.if_mtu = ifr->ifr_mtu;
641             if (sc->sc_setmtu)
642                     (*sc->sc_setmtu)(sc);
643         }
644         break;
645
646     case SIOCGIFMTU:
647         ifr->ifr_mtu = sc->sc_if.if_mtu;
648         break;
649
650     case SIOCADDMULTI:
651     case SIOCDELMULTI:
652         if (ifr == 0) {
653             error = EAFNOSUPPORT;
654             break;
655         }
656         switch(ifr->ifr_addr.sa_family) {
657 #ifdef INET
658         case AF_INET:
659             break;
660 #endif
661         default:
662             error = EAFNOSUPPORT;
663             break;
664         }
665         break;
666
667     case SIOCGPPPSTATS:
668         psp = &((struct ifpppstatsreq *) data)->stats;
669         bzero(psp, sizeof(*psp));
670         psp->p = sc->sc_stats;
671 #if defined(VJC) && !defined(SL_NO_STATS)
672         if (sc->sc_comp) {
673             psp->vj.vjs_packets = sc->sc_comp->sls_packets;
674             psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
675             psp->vj.vjs_searches = sc->sc_comp->sls_searches;
676             psp->vj.vjs_misses = sc->sc_comp->sls_misses;
677             psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
678             psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
679             psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
680             psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
681         }
682 #endif /* VJC */
683         break;
684
685 #ifdef PPP_COMPRESS
686     case SIOCGPPPCSTATS:
687         pcp = &((struct ifpppcstatsreq *) data)->stats;
688         bzero(pcp, sizeof(*pcp));
689         if (sc->sc_xc_state != NULL)
690             (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
691         if (sc->sc_rc_state != NULL)
692             (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
693         break;
694 #endif /* PPP_COMPRESS */
695
696     default:
697         error = ENOTTY;
698     }
699     crit_exit();
700     return (error);
701 }
702
703 /*
704  * Queue a packet.  Start transmission if not active.
705  * Packet is placed in Information field of PPP frame.
706  * Called at splnet as the if->if_output handler.
707  * Called at splnet from pppwrite().
708  */
709 static int
710 pppoutput_serialized(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
711                      struct rtentry *rtp)
712 {
713     struct ppp_softc *sc = &ppp_softc[ifp->if_dunit];
714     int protocol, address, control;
715     u_char *cp;
716     int error;
717     struct ip *ip;
718     struct ifqueue *ifq;
719     enum NPmode mode;
720     int len;
721     struct mbuf *m;
722     struct altq_pktattr pktattr;
723
724     if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
725         || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
726         error = ENETDOWN;       /* sort of */
727         goto bad;
728     }
729
730     ifq_classify(&ifp->if_snd, m0, dst->sa_family, &pktattr);
731
732     /*
733      * Compute PPP header.
734      */
735     m0->m_flags &= ~M_HIGHPRI;
736     switch (dst->sa_family) {
737 #ifdef INET
738     case AF_INET:
739         address = PPP_ALLSTATIONS;
740         control = PPP_UI;
741         protocol = PPP_IP;
742         mode = sc->sc_npmode[NP_IP];
743
744         /*
745          * If this packet has the "low delay" bit set in the IP header,
746          * put it on the fastq instead.
747          */
748         ip = mtod(m0, struct ip *);
749         if (ip->ip_tos & IPTOS_LOWDELAY)
750             m0->m_flags |= M_HIGHPRI;
751         break;
752 #endif
753 #ifdef IPX
754     case AF_IPX:
755         /*
756          * This is pretty bogus.. We dont have an ipxcp module in pppd
757          * yet to configure the link parameters.  Sigh. I guess a
758          * manual ifconfig would do....  -Peter
759          */
760         address = PPP_ALLSTATIONS;
761         control = PPP_UI;
762         protocol = PPP_IPX;
763         mode = NPMODE_PASS;
764         break;
765 #endif
766     case AF_UNSPEC:
767         address = PPP_ADDRESS(dst->sa_data);
768         control = PPP_CONTROL(dst->sa_data);
769         protocol = PPP_PROTOCOL(dst->sa_data);
770         mode = NPMODE_PASS;
771         break;
772     default:
773         kprintf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
774         error = EAFNOSUPPORT;
775         goto bad;
776     }
777
778     /*
779      * Drop this packet, or return an error, if necessary.
780      */
781     if (mode == NPMODE_ERROR) {
782         error = ENETDOWN;
783         goto bad;
784     }
785     if (mode == NPMODE_DROP) {
786         error = 0;
787         goto bad;
788     }
789
790     /*
791      * Add PPP header.  If no space in first mbuf, allocate another.
792      * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
793      */
794     if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
795         m0 = m_prepend(m0, PPP_HDRLEN, MB_DONTWAIT);
796         if (m0 == 0) {
797             error = ENOBUFS;
798             goto bad;
799         }
800         m0->m_len = 0;
801     } else
802         m0->m_data -= PPP_HDRLEN;
803
804     cp = mtod(m0, u_char *);
805     *cp++ = address;
806     *cp++ = control;
807     *cp++ = protocol >> 8;
808     *cp++ = protocol & 0xff;
809     m0->m_len += PPP_HDRLEN;
810
811     len = 0;
812     for (m = m0; m != 0; m = m->m_next)
813         len += m->m_len;
814
815     if (sc->sc_flags & SC_LOG_OUTPKT) {
816         kprintf("%s output: ", ifp->if_xname);
817         pppdumpm(m0);
818     }
819
820     if ((protocol & 0x8000) == 0) {
821 #ifdef PPP_FILTER
822         /*
823          * Apply the pass and active filters to the packet,
824          * but only if it is a data packet.
825          */
826         *mtod(m0, u_char *) = 1;        /* indicates outbound */
827         if (sc->sc_pass_filt.bf_insns != 0
828             && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
829                           len, 0) == 0) {
830             error = 0;          /* drop this packet */
831             goto bad;
832         }
833
834         /*
835          * Update the time we sent the most recent packet.
836          */
837         if (sc->sc_active_filt.bf_insns == 0
838             || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
839             sc->sc_last_sent = time_second;
840
841         *mtod(m0, u_char *) = address;
842 #else
843         /*
844          * Update the time we sent the most recent data packet.
845          */
846         sc->sc_last_sent = time_second;
847 #endif /* PPP_FILTER */
848     }
849
850     BPF_MTAP(ifp, m0);
851
852     /*
853      * Put the packet on the appropriate queue.
854      */
855     crit_enter();
856     if (mode == NPMODE_QUEUE) {
857         /* XXX we should limit the number of packets on this queue */
858         *sc->sc_npqtail = m0;
859         m0->m_nextpkt = NULL;
860         sc->sc_npqtail = &m0->m_nextpkt;
861     } else {
862         /* fastq and if_snd are emptied at spl[soft]net now */
863         if ((m0->m_flags & M_HIGHPRI) && !ifq_is_enabled(&sc->sc_if.if_snd)) {
864             ifq = &sc->sc_fastq;
865             if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
866                 IF_DROP(ifq);
867                 m_freem(m0);
868                 error = ENOBUFS;
869             } else {
870                 IF_ENQUEUE(ifq, m0);
871                 error = 0;
872             }
873         } else {
874             ASSERT_SERIALIZED(sc->sc_if.if_serializer);
875             error = ifq_enqueue(&sc->sc_if.if_snd, m0, &pktattr);
876         }
877         if (error) {
878             crit_exit();
879             sc->sc_if.if_oerrors++;
880             sc->sc_stats.ppp_oerrors++;
881             return (error);
882         }
883         (*sc->sc_start)(sc);
884     }
885     getmicrotime(&ifp->if_lastchange);
886     ifp->if_opackets++;
887     ifp->if_obytes += len;
888
889     crit_exit();
890     return (0);
891
892 bad:
893     m_freem(m0);
894     return (error);
895 }
896
897 int
898 pppoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
899           struct rtentry *rtp)
900 {
901         int error;
902
903         ifnet_serialize_tx(ifp);
904         error = pppoutput_serialized(ifp, m0, dst, rtp);
905         ifnet_deserialize_tx(ifp);
906
907         return error;
908 }
909
910 /*
911  * After a change in the NPmode for some NP, move packets from the
912  * npqueue to the send queue or the fast queue as appropriate.
913  * Should be called at spl[soft]net.
914  */
915 static void
916 ppp_requeue(struct ppp_softc *sc)
917 {
918     struct mbuf *m, **mpp;
919     struct ifqueue *ifq;
920     enum NPmode mode;
921     int error;
922
923     for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
924         switch (PPP_PROTOCOL(mtod(m, u_char *))) {
925         case PPP_IP:
926             mode = sc->sc_npmode[NP_IP];
927             break;
928         default:
929             mode = NPMODE_PASS;
930         }
931
932         switch (mode) {
933         case NPMODE_PASS:
934             /*
935              * This packet can now go on one of the queues to be sent.
936              */
937             *mpp = m->m_nextpkt;
938             m->m_nextpkt = NULL;
939             if ((m->m_flags & M_HIGHPRI) && !ifq_is_enabled(&sc->sc_if.if_snd)) {
940                 ifq = &sc->sc_fastq;
941                 if (IF_QFULL(ifq)) {
942                     IF_DROP(ifq);
943                     error = ENOBUFS;
944                 } else {
945                     IF_ENQUEUE(ifq, m);
946                     error = 0;
947                 }
948             } else {
949                 error = ifq_enqueue(&sc->sc_if.if_snd, m, NULL);
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 }