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