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