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