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