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