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