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