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