xref: /netbsd-src/sys/net/if_ppp.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: if_ppp.c,v 1.132 2010/08/21 13:19:40 pgoyette 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) 1984-2000 Carnegie Mellon University. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * 3. The name "Carnegie Mellon University" must not be used to
22  *    endorse or promote products derived from this software without
23  *    prior written permission. For permission or any legal
24  *    details, please contact
25  *      Office of Technology Transfer
26  *      Carnegie Mellon University
27  *      5000 Forbes Avenue
28  *      Pittsburgh, PA  15213-3890
29  *      (412) 268-4387, fax: (412) 268-7395
30  *      tech-transfer@andrew.cmu.edu
31  *
32  * 4. Redistributions of any form whatsoever must retain the following
33  *    acknowledgment:
34  *    "This product includes software developed by Computing Services
35  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
36  *
37  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
38  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
39  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
40  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
41  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
42  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
43  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
44  *
45  * Based on:
46  *	@(#)if_sl.c	7.6.1.2 (Berkeley) 2/15/89
47  *
48  * Copyright (c) 1987 Regents of the University of California.
49  * All rights reserved.
50  *
51  * Redistribution and use in source and binary forms are permitted
52  * provided that the above copyright notice and this paragraph are
53  * duplicated in all such forms and that any documentation,
54  * advertising materials, and other materials related to such
55  * distribution and use acknowledge that the software was developed
56  * by the University of California, Berkeley.  The name of the
57  * University may not be used to endorse or promote products derived
58  * from this software without specific prior written permission.
59  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
60  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
61  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
62  *
63  * Serial Line interface
64  *
65  * Rick Adams
66  * Center for Seismic Studies
67  * 1300 N 17th Street, Suite 1450
68  * Arlington, Virginia 22209
69  * (703)276-7900
70  * rick@seismo.ARPA
71  * seismo!rick
72  *
73  * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
74  * Converted to 4.3BSD Beta by Chris Torek.
75  * Other changes made at Berkeley, based in part on code by Kirk Smith.
76  *
77  * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
78  * Added VJ tcp header compression; more unified ioctls
79  *
80  * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au).
81  * Cleaned up a lot of the mbuf-related code to fix bugs that
82  * caused system crashes and packet corruption.  Changed pppstart
83  * so that it doesn't just give up with a collision if the whole
84  * packet doesn't fit in the output ring buffer.
85  *
86  * Added priority queueing for interactive IP packets, following
87  * the model of if_sl.c, plus hooks for bpf.
88  * Paul Mackerras (paulus@cs.anu.edu.au).
89  */
90 
91 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
92 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
93 
94 /*
95  * XXX IMP ME HARDER
96  *
97  * This is an explanation of that comment.  This code used to use
98  * splimp() to block both network and tty interrupts.  However,
99  * that call is deprecated.  So, we have replaced the uses of
100  * splimp() with splhigh() in order to applomplish what it needs
101  * to accomplish, and added that happy little comment.
102  */
103 
104 #include <sys/cdefs.h>
105 __KERNEL_RCSID(0, "$NetBSD: if_ppp.c,v 1.132 2010/08/21 13:19:40 pgoyette Exp $");
106 
107 #include "ppp.h"
108 
109 #include "opt_inet.h"
110 #include "opt_gateway.h"
111 #include "opt_ppp.h"
112 
113 #ifdef INET
114 #define VJC
115 #endif
116 #define PPP_COMPRESS
117 
118 #include <sys/param.h>
119 #include <sys/proc.h>
120 #include <sys/mbuf.h>
121 #include <sys/socket.h>
122 #include <sys/ioctl.h>
123 #include <sys/kernel.h>
124 #include <sys/systm.h>
125 #include <sys/time.h>
126 #include <sys/malloc.h>
127 #include <sys/module.h>
128 #include <sys/mutex.h>
129 #include <sys/once.h>
130 #include <sys/conf.h>
131 #include <sys/kauth.h>
132 #include <sys/intr.h>
133 #include <sys/simplelock.h>
134 #include <sys/socketvar.h>
135 
136 #include <net/if.h>
137 #include <net/if_types.h>
138 #include <net/netisr.h>
139 #include <net/route.h>
140 #ifdef PPP_FILTER
141 #include <net/bpf.h>
142 #endif
143 
144 #include <netinet/in.h>
145 #include <netinet/in_systm.h>
146 #include <netinet/in_var.h>
147 #ifdef INET
148 #include <netinet/ip.h>
149 #endif
150 
151 #include <net/bpf.h>
152 
153 #include <net/slip.h>
154 
155 #ifdef VJC
156 #include <net/slcompress.h>
157 #endif
158 
159 #include <net/ppp_defs.h>
160 #include <net/if_ppp.h>
161 #include <net/if_pppvar.h>
162 #include <sys/cpu.h>
163 
164 #ifdef PPP_COMPRESS
165 #define PACKETPTR	struct mbuf *
166 #include <net/ppp-comp.h>
167 #endif
168 
169 static int	pppsioctl(struct ifnet *, u_long, void *);
170 static void	ppp_requeue(struct ppp_softc *);
171 static void	ppp_ccp(struct ppp_softc *, struct mbuf *m, int rcvd);
172 static void	ppp_ccp_closed(struct ppp_softc *);
173 static void	ppp_inproc(struct ppp_softc *, struct mbuf *);
174 static void	pppdumpm(struct mbuf *m0);
175 #ifdef ALTQ
176 static void	ppp_ifstart(struct ifnet *ifp);
177 #endif
178 
179 static void	pppintr(void *);
180 
181 /*
182  * Some useful mbuf macros not in mbuf.h.
183  */
184 #define M_IS_CLUSTER(m)	((m)->m_flags & M_EXT)
185 
186 #define M_DATASTART(m)	\
187 	(M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
188 	    (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
189 
190 #define M_DATASIZE(m)	\
191 	(M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
192 	    (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
193 
194 /*
195  * We define two link layer specific mbuf flags, to mark high-priority
196  * packets for output, and received packets following lost/corrupted
197  * packets.
198  */
199 #define	M_HIGHPRI	M_LINK0	/* output packet for sc_fastq */
200 #define	M_ERRMARK	M_LINK1	/* rx packet following lost/corrupted pkt */
201 
202 static int		ppp_clone_create(struct if_clone *, int);
203 static int		ppp_clone_destroy(struct ifnet *);
204 
205 static struct ppp_softc *ppp_create(const char *, int);
206 
207 static LIST_HEAD(, ppp_softc) ppp_softc_list;
208 
209 struct if_clone ppp_cloner =
210     IF_CLONE_INITIALIZER("ppp", ppp_clone_create, ppp_clone_destroy);
211 
212 static struct simplelock ppp_list_mutex = SIMPLELOCK_INITIALIZER;
213 
214 #ifdef PPP_COMPRESS
215 ONCE_DECL(ppp_compressor_mtx_init);
216 static LIST_HEAD(, compressor) ppp_compressors = { NULL };
217 static kmutex_t ppp_compressors_mtx;
218 
219 static int ppp_compressor_init(void);
220 static struct compressor *ppp_get_compressor(uint8_t);
221 static void ppp_compressor_rele(struct compressor *);
222 #endif /* PPP_COMPRESS */
223 
224 
225 /*
226  * Called from boot code to establish ppp interfaces.
227  */
228 void
229 pppattach(void)
230 {
231     extern struct linesw ppp_disc;
232 
233     if (ttyldisc_attach(&ppp_disc) != 0)
234     	panic("pppattach");
235     LIST_INIT(&ppp_softc_list);
236     if_clone_attach(&ppp_cloner);
237     RUN_ONCE(&ppp_compressor_mtx_init, ppp_compressor_init);
238 }
239 
240 static struct ppp_softc *
241 ppp_create(const char *name, int unit)
242 {
243     struct ppp_softc *sc, *sci, *scl = NULL;
244 
245     sc = malloc(sizeof(*sc), M_DEVBUF, M_WAIT|M_ZERO);
246 
247     simple_lock(&ppp_list_mutex);
248     if (unit == -1) {
249 	int i = 0;
250 	LIST_FOREACH(sci, &ppp_softc_list, sc_iflist) {
251 	    scl = sci;
252 	    if (i < sci->sc_unit) {
253 		unit = i;
254 		break;
255 	    } else {
256 #ifdef DIAGNOSTIC
257 		KASSERT(i == sci->sc_unit);
258 #endif
259 		i++;
260 	    }
261 	}
262 	if (unit == -1)
263 	    unit = i;
264     } else {
265 	LIST_FOREACH(sci, &ppp_softc_list, sc_iflist) {
266 	    scl = sci;
267 	    if (unit < sci->sc_unit)
268 		break;
269 	    else if (unit == sci->sc_unit) {
270 		free(sc, M_DEVBUF);
271 		return NULL;
272 	    }
273 	}
274     }
275 
276     if (sci != NULL)
277 	LIST_INSERT_BEFORE(sci, sc, sc_iflist);
278     else if (scl != NULL)
279 	LIST_INSERT_AFTER(scl, sc, sc_iflist);
280     else
281 	LIST_INSERT_HEAD(&ppp_softc_list, sc, sc_iflist);
282 
283     simple_unlock(&ppp_list_mutex);
284 
285     if_initname(&sc->sc_if, name, sc->sc_unit = unit);
286     callout_init(&sc->sc_timo_ch, 0);
287     sc->sc_if.if_softc = sc;
288     sc->sc_if.if_mtu = PPP_MTU;
289     sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
290     sc->sc_if.if_type = IFT_PPP;
291     sc->sc_if.if_hdrlen = PPP_HDRLEN;
292     sc->sc_if.if_dlt = DLT_NULL;
293     sc->sc_if.if_ioctl = pppsioctl;
294     sc->sc_if.if_output = pppoutput;
295 #ifdef ALTQ
296     sc->sc_if.if_start = ppp_ifstart;
297 #endif
298     IFQ_SET_MAXLEN(&sc->sc_if.if_snd, IFQ_MAXLEN);
299     sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
300     sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
301     sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
302     /* Ratio of 1:2 packets between the regular and the fast queue */
303     sc->sc_maxfastq = 2;
304     IFQ_SET_READY(&sc->sc_if.if_snd);
305     if_attach(&sc->sc_if);
306     if_alloc_sadl(&sc->sc_if);
307     bpf_attach(&sc->sc_if, DLT_NULL, 0);
308     return sc;
309 }
310 
311 static int
312 ppp_clone_create(struct if_clone *ifc, int unit)
313 {
314     return ppp_create(ifc->ifc_name, unit) == NULL ? EEXIST : 0;
315 }
316 
317 static int
318 ppp_clone_destroy(struct ifnet *ifp)
319 {
320     struct ppp_softc *sc = (struct ppp_softc *)ifp->if_softc;
321 
322     if (sc->sc_devp != NULL)
323 	return EBUSY; /* Not removing it */
324 
325     simple_lock(&ppp_list_mutex);
326     LIST_REMOVE(sc, sc_iflist);
327     simple_unlock(&ppp_list_mutex);
328 
329     bpf_detach(ifp);
330     if_detach(ifp);
331 
332     free(sc, M_DEVBUF);
333     return 0;
334 }
335 
336 /*
337  * Allocate a ppp interface unit and initialize it.
338  */
339 struct ppp_softc *
340 pppalloc(pid_t pid)
341 {
342     struct ppp_softc *sc = NULL, *scf;
343     int i;
344 
345     simple_lock(&ppp_list_mutex);
346     for (scf = LIST_FIRST(&ppp_softc_list); scf != NULL;
347 	scf = LIST_NEXT(scf, sc_iflist)) {
348 	if (scf->sc_xfer == pid) {
349 	    scf->sc_xfer = 0;
350 	    simple_unlock(&ppp_list_mutex);
351 	    return scf;
352 	}
353 	if (scf->sc_devp == NULL && sc == NULL)
354 	    sc = scf;
355     }
356     simple_unlock(&ppp_list_mutex);
357 
358     if (sc == NULL)
359 	sc = ppp_create(ppp_cloner.ifc_name, -1);
360 
361     sc->sc_si = softint_establish(SOFTINT_NET, pppintr, sc);
362     if (sc->sc_si == NULL) {
363 	printf("%s: unable to establish softintr\n", sc->sc_if.if_xname);
364 	return (NULL);
365     }
366     sc->sc_flags = 0;
367     sc->sc_mru = PPP_MRU;
368     sc->sc_relinq = NULL;
369     (void)memset(&sc->sc_stats, 0, sizeof(sc->sc_stats));
370 #ifdef VJC
371     sc->sc_comp = malloc(sizeof(struct slcompress), M_DEVBUF, M_NOWAIT);
372     if (sc->sc_comp)
373 	sl_compress_init(sc->sc_comp);
374 #endif
375 #ifdef PPP_COMPRESS
376     sc->sc_xc_state = NULL;
377     sc->sc_rc_state = NULL;
378 #endif /* PPP_COMPRESS */
379     for (i = 0; i < NUM_NP; ++i)
380 	sc->sc_npmode[i] = NPMODE_ERROR;
381     sc->sc_npqueue = NULL;
382     sc->sc_npqtail = &sc->sc_npqueue;
383     sc->sc_last_sent = sc->sc_last_recv = time_second;
384 
385     return sc;
386 }
387 
388 /*
389  * Deallocate a ppp unit.  Must be called at splsoftnet or higher.
390  */
391 void
392 pppdealloc(struct ppp_softc *sc)
393 {
394     struct mbuf *m;
395 
396     softint_disestablish(sc->sc_si);
397     if_down(&sc->sc_if);
398     sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
399     sc->sc_devp = NULL;
400     sc->sc_xfer = 0;
401     for (;;) {
402 	IF_DEQUEUE(&sc->sc_rawq, m);
403 	if (m == NULL)
404 	    break;
405 	m_freem(m);
406     }
407     for (;;) {
408 	IF_DEQUEUE(&sc->sc_inq, m);
409 	if (m == NULL)
410 	    break;
411 	m_freem(m);
412     }
413     for (;;) {
414 	IF_DEQUEUE(&sc->sc_fastq, m);
415 	if (m == NULL)
416 	    break;
417 	m_freem(m);
418     }
419     while ((m = sc->sc_npqueue) != NULL) {
420 	sc->sc_npqueue = m->m_nextpkt;
421 	m_freem(m);
422     }
423     if (sc->sc_togo != NULL) {
424 	m_freem(sc->sc_togo);
425 	sc->sc_togo = NULL;
426     }
427 #ifdef PPP_COMPRESS
428     ppp_ccp_closed(sc);
429     sc->sc_xc_state = NULL;
430     sc->sc_rc_state = NULL;
431 #endif /* PPP_COMPRESS */
432 #ifdef PPP_FILTER
433     if (sc->sc_pass_filt_in.bf_insns != 0) {
434 	free(sc->sc_pass_filt_in.bf_insns, M_DEVBUF);
435 	sc->sc_pass_filt_in.bf_insns = 0;
436 	sc->sc_pass_filt_in.bf_len = 0;
437     }
438     if (sc->sc_pass_filt_out.bf_insns != 0) {
439 	free(sc->sc_pass_filt_out.bf_insns, M_DEVBUF);
440 	sc->sc_pass_filt_out.bf_insns = 0;
441 	sc->sc_pass_filt_out.bf_len = 0;
442     }
443     if (sc->sc_active_filt_in.bf_insns != 0) {
444 	free(sc->sc_active_filt_in.bf_insns, M_DEVBUF);
445 	sc->sc_active_filt_in.bf_insns = 0;
446 	sc->sc_active_filt_in.bf_len = 0;
447     }
448     if (sc->sc_active_filt_out.bf_insns != 0) {
449 	free(sc->sc_active_filt_out.bf_insns, M_DEVBUF);
450 	sc->sc_active_filt_out.bf_insns = 0;
451 	sc->sc_active_filt_out.bf_len = 0;
452     }
453 #endif /* PPP_FILTER */
454 #ifdef VJC
455     if (sc->sc_comp != 0) {
456 	free(sc->sc_comp, M_DEVBUF);
457 	sc->sc_comp = 0;
458     }
459 #endif
460     (void)ppp_clone_destroy(&sc->sc_if);
461 }
462 
463 /*
464  * Ioctl routine for generic ppp devices.
465  */
466 int
467 pppioctl(struct ppp_softc *sc, u_long cmd, void *data, int flag,
468     struct lwp *l)
469 {
470     int s, error, flags, mru, npx;
471     u_int nb;
472     struct ppp_option_data *odp;
473     struct compressor *cp;
474     struct npioctl *npi;
475     time_t t;
476 #ifdef PPP_FILTER
477     struct bpf_program *bp, *nbp;
478     struct bpf_insn *newcode, *oldcode;
479     int newcodelen;
480 #endif /* PPP_FILTER */
481 #ifdef	PPP_COMPRESS
482     u_char ccp_option[CCP_MAX_OPTION_LENGTH];
483 #endif
484 
485     switch (cmd) {
486     case PPPIOCSFLAGS:
487     case PPPIOCSMRU:
488     case PPPIOCSMAXCID:
489     case PPPIOCSCOMPRESS:
490     case PPPIOCSNPMODE:
491 	if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
492 	    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, &sc->sc_if, KAUTH_ARG(cmd),
493 	    NULL) != 0)
494 		return (EPERM);
495 	break;
496     case PPPIOCXFERUNIT:
497 	/* XXX: Why is this privileged?! */
498 	if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
499 	    KAUTH_REQ_NETWORK_INTERFACE_GETPRIV, &sc->sc_if, KAUTH_ARG(cmd),
500 	    NULL) != 0)
501 		return (EPERM);
502 	break;
503     default:
504 	break;
505     }
506 
507     switch (cmd) {
508     case FIONREAD:
509 	*(int *)data = sc->sc_inq.ifq_len;
510 	break;
511 
512     case PPPIOCGUNIT:
513 	*(int *)data = sc->sc_unit;
514 	break;
515 
516     case PPPIOCGFLAGS:
517 	*(u_int *)data = sc->sc_flags;
518 	break;
519 
520     case PPPIOCGRAWIN:
521 	{
522 	    struct ppp_rawin *rwin = (struct ppp_rawin *)data;
523 	    u_char c, q = 0;
524 
525 	    for (c = sc->sc_rawin_start; c < sizeof(sc->sc_rawin.buf);)
526 		rwin->buf[q++] = sc->sc_rawin.buf[c++];
527 
528 	    for (c = 0; c < sc->sc_rawin_start;)
529 		rwin->buf[q++] = sc->sc_rawin.buf[c++];
530 
531 	    rwin->count = sc->sc_rawin.count;
532 	}
533 	break;
534 
535     case PPPIOCSFLAGS:
536 	flags = *(int *)data & SC_MASK;
537 	s = splsoftnet();
538 #ifdef PPP_COMPRESS
539 	if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
540 	    ppp_ccp_closed(sc);
541 #endif
542 	splhigh();	/* XXX IMP ME HARDER */
543 	sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
544 	splx(s);
545 	break;
546 
547     case PPPIOCSMRU:
548 	mru = *(int *)data;
549 	if (mru >= PPP_MINMRU && mru <= PPP_MAXMRU)
550 	    sc->sc_mru = mru;
551 	break;
552 
553     case PPPIOCGMRU:
554 	*(int *)data = sc->sc_mru;
555 	break;
556 
557 #ifdef VJC
558     case PPPIOCSMAXCID:
559 	if (sc->sc_comp) {
560 	    s = splsoftnet();
561 	    sl_compress_setup(sc->sc_comp, *(int *)data);
562 	    splx(s);
563 	}
564 	break;
565 #endif
566 
567     case PPPIOCXFERUNIT:
568 	sc->sc_xfer = l->l_proc->p_pid;
569 	break;
570 
571 #ifdef PPP_COMPRESS
572     case PPPIOCSCOMPRESS:
573 	odp = (struct ppp_option_data *) data;
574 	nb = odp->length;
575 	if (nb > sizeof(ccp_option))
576 	    nb = sizeof(ccp_option);
577 	if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
578 	    return (error);
579 	if (ccp_option[1] < 2)	/* preliminary check on the length byte */
580 	    return (EINVAL);
581 	cp = ppp_get_compressor(ccp_option[0]);
582 	if (cp == NULL) {
583 		if (sc->sc_flags & SC_DEBUG)
584 		    printf("%s: no compressor for [%x %x %x], %x\n",
585 			sc->sc_if.if_xname, ccp_option[0], ccp_option[1],
586 			ccp_option[2], nb);
587 		return (EINVAL);	/* no handler found */
588 	}
589 	/*
590 	 * Found a handler for the protocol - try to allocate
591 	 * a compressor or decompressor.
592 	 */
593 	error = 0;
594 	if (odp->transmit) {
595 	    s = splsoftnet();
596 	    if (sc->sc_xc_state != NULL) {
597 		(*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
598 		ppp_compressor_rele(sc->sc_xcomp);
599 	    }
600 	    sc->sc_xcomp = cp;
601 	    sc->sc_xc_state = cp->comp_alloc(ccp_option, nb);
602 	    if (sc->sc_xc_state == NULL) {
603 		if (sc->sc_flags & SC_DEBUG)
604 		    printf("%s: comp_alloc failed\n",
605 			sc->sc_if.if_xname);
606 		error = ENOBUFS;
607 	    }
608 	    splhigh();	/* XXX IMP ME HARDER */
609 	    sc->sc_flags &= ~SC_COMP_RUN;
610 	    splx(s);
611 	} else {
612 	    s = splsoftnet();
613 	    if (sc->sc_rc_state != NULL) {
614 		(*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
615 		ppp_compressor_rele(sc->sc_rcomp);
616 	    }
617 	    sc->sc_rcomp = cp;
618 	    sc->sc_rc_state = cp->decomp_alloc(ccp_option, nb);
619 	    if (sc->sc_rc_state == NULL) {
620 		if (sc->sc_flags & SC_DEBUG)
621 		    printf("%s: decomp_alloc failed\n",
622 			sc->sc_if.if_xname);
623 		error = ENOBUFS;
624 	    }
625 	    splhigh();	/* XXX IMP ME HARDER */
626 	    sc->sc_flags &= ~SC_DECOMP_RUN;
627 	    splx(s);
628 	}
629 	return (error);
630 #endif /* PPP_COMPRESS */
631 
632     case PPPIOCGNPMODE:
633     case PPPIOCSNPMODE:
634 	npi = (struct npioctl *) data;
635 	switch (npi->protocol) {
636 	case PPP_IP:
637 	    npx = NP_IP;
638 	    break;
639 	case PPP_IPV6:
640 	    npx = NP_IPV6;
641 	    break;
642 	default:
643 	    return EINVAL;
644 	}
645 	if (cmd == PPPIOCGNPMODE) {
646 	    npi->mode = sc->sc_npmode[npx];
647 	} else {
648 	    if (npi->mode != sc->sc_npmode[npx]) {
649 		s = splnet();
650 		sc->sc_npmode[npx] = npi->mode;
651 		if (npi->mode != NPMODE_QUEUE) {
652 		    ppp_requeue(sc);
653 		    ppp_restart(sc);
654 		}
655 		splx(s);
656 	    }
657 	}
658 	break;
659 
660     case PPPIOCGIDLE:
661 	s = splsoftnet();
662 	t = time_second;
663 	((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
664 	((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
665 	splx(s);
666 	break;
667 
668 #ifdef PPP_FILTER
669     case PPPIOCSPASS:
670     case PPPIOCSACTIVE:
671 	/* These are no longer supported. */
672 	return EOPNOTSUPP;
673 
674     case PPPIOCSIPASS:
675     case PPPIOCSOPASS:
676     case PPPIOCSIACTIVE:
677     case PPPIOCSOACTIVE:
678 	nbp = (struct bpf_program *) data;
679 	if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
680 	    return EINVAL;
681 	newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
682 	if (newcodelen != 0) {
683 	    newcode = malloc(newcodelen, M_DEVBUF, M_WAITOK);
684 	    /* WAITOK -- malloc() never fails. */
685 	    if ((error = copyin((void *)nbp->bf_insns, (void *)newcode,
686 			       newcodelen)) != 0) {
687 		free(newcode, M_DEVBUF);
688 		return error;
689 	    }
690 	    if (!bpf_validate(newcode, nbp->bf_len)) {
691 		free(newcode, M_DEVBUF);
692 		return EINVAL;
693 	    }
694 	} else
695 	    newcode = 0;
696 	switch (cmd) {
697 	case PPPIOCSIPASS:
698 	    bp = &sc->sc_pass_filt_in;
699 	    break;
700 
701 	case PPPIOCSOPASS:
702 	    bp = &sc->sc_pass_filt_out;
703 	    break;
704 
705 	case PPPIOCSIACTIVE:
706 	    bp = &sc->sc_active_filt_in;
707 	    break;
708 
709 	case PPPIOCSOACTIVE:
710 	    bp = &sc->sc_active_filt_out;
711 	    break;
712 	default:
713 	    free(newcode, M_DEVBUF);
714 	    return (EPASSTHROUGH);
715 	}
716 	oldcode = bp->bf_insns;
717 	s = splnet();
718 	bp->bf_len = nbp->bf_len;
719 	bp->bf_insns = newcode;
720 	splx(s);
721 	if (oldcode != 0)
722 	    free(oldcode, M_DEVBUF);
723 	break;
724 #endif /* PPP_FILTER */
725 
726     default:
727 	return (EPASSTHROUGH);
728     }
729     return (0);
730 }
731 
732 /*
733  * Process an ioctl request to the ppp network interface.
734  */
735 static int
736 pppsioctl(struct ifnet *ifp, u_long cmd, void *data)
737 {
738     struct lwp *l = curlwp;	/* XXX */
739     struct ppp_softc *sc = ifp->if_softc;
740     struct ifaddr *ifa = (struct ifaddr *)data;
741     struct ifreq *ifr = (struct ifreq *)data;
742     struct ppp_stats *psp;
743 #ifdef	PPP_COMPRESS
744     struct ppp_comp_stats *pcp;
745 #endif
746     int s = splnet(), error = 0;
747 
748     switch (cmd) {
749     case SIOCSIFFLAGS:
750 	if ((error = ifioctl_common(ifp, cmd, data)) != 0)
751 		break;
752 	if ((ifp->if_flags & IFF_RUNNING) == 0)
753 	    ifp->if_flags &= ~IFF_UP;
754 	break;
755 
756     case SIOCINITIFADDR:
757 	switch (ifa->ifa_addr->sa_family) {
758 #ifdef INET
759 	case AF_INET:
760 	    break;
761 #endif
762 #ifdef INET6
763 	case AF_INET6:
764 	    break;
765 #endif
766 	default:
767 	    error = EAFNOSUPPORT;
768 	    break;
769 	}
770 	break;
771 
772     case SIOCSIFDSTADDR:
773 	switch (ifa->ifa_addr->sa_family) {
774 #ifdef INET
775 	case AF_INET:
776 	    break;
777 #endif
778 #ifdef INET6
779 	case AF_INET6:
780 	    break;
781 #endif
782 	default:
783 	    error = EAFNOSUPPORT;
784 	    break;
785 	}
786 	break;
787 
788     case SIOCADDMULTI:
789     case SIOCDELMULTI:
790 	if (ifr == NULL) {
791 	    error = EAFNOSUPPORT;
792 	    break;
793 	}
794 	switch (ifreq_getaddr(cmd, ifr)->sa_family) {
795 #ifdef INET
796 	case AF_INET:
797 	    break;
798 #endif
799 #ifdef INET6
800 	case AF_INET6:
801 	    break;
802 #endif
803 	default:
804 	    error = EAFNOSUPPORT;
805 	    break;
806 	}
807 	break;
808 
809     case SIOCGPPPSTATS:
810 	psp = &((struct ifpppstatsreq *) data)->stats;
811 	memset(psp, 0, sizeof(*psp));
812 	psp->p = sc->sc_stats;
813 #if defined(VJC) && !defined(SL_NO_STATS)
814 	if (sc->sc_comp) {
815 	    psp->vj.vjs_packets = sc->sc_comp->sls_packets;
816 	    psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
817 	    psp->vj.vjs_searches = sc->sc_comp->sls_searches;
818 	    psp->vj.vjs_misses = sc->sc_comp->sls_misses;
819 	    psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
820 	    psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
821 	    psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
822 	    psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
823 	}
824 #endif /* VJC */
825 	break;
826 
827 #ifdef PPP_COMPRESS
828     case SIOCGPPPCSTATS:
829 	pcp = &((struct ifpppcstatsreq *) data)->stats;
830 	memset(pcp, 0, sizeof(*pcp));
831 	if (sc->sc_xc_state != NULL)
832 	    (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
833 	if (sc->sc_rc_state != NULL)
834 	    (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
835 	break;
836 #endif /* PPP_COMPRESS */
837 
838     case SIOCSIFMTU:
839 	if ((error = kauth_authorize_network(l->l_cred,
840 	    KAUTH_NETWORK_INTERFACE, KAUTH_REQ_NETWORK_INTERFACE_SETPRIV,
841 	    ifp, (void *)cmd, NULL) != 0))
842 	    break;
843 	/*FALLTHROUGH*/
844     default:
845 	if ((error = ifioctl_common(&sc->sc_if, cmd, data)) == ENETRESET)
846 		error = 0;
847 	break;
848     }
849     splx(s);
850     return (error);
851 }
852 
853 /*
854  * Queue a packet.  Start transmission if not active.
855  * Packet is placed in Information field of PPP frame.
856  */
857 int
858 pppoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
859     struct rtentry *rtp)
860 {
861     struct ppp_softc *sc = ifp->if_softc;
862     int protocol, address, control;
863     u_char *cp;
864     int s, error;
865 #ifdef INET
866     struct ip *ip;
867 #endif
868     struct ifqueue *ifq;
869     enum NPmode mode;
870     int len;
871     ALTQ_DECL(struct altq_pktattr pktattr;)
872 
873     if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
874 	|| ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
875 	error = ENETDOWN;	/* sort of */
876 	goto bad;
877     }
878 
879     IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family, &pktattr);
880 
881     /*
882      * Compute PPP header.
883      */
884     m0->m_flags &= ~M_HIGHPRI;
885     switch (dst->sa_family) {
886 #ifdef INET
887     case AF_INET:
888 	address = PPP_ALLSTATIONS;
889 	control = PPP_UI;
890 	protocol = PPP_IP;
891 	mode = sc->sc_npmode[NP_IP];
892 
893 	/*
894 	 * If this packet has the "low delay" bit set in the IP header,
895 	 * put it on the fastq instead.
896 	 */
897 	ip = mtod(m0, struct ip *);
898 	if (ip->ip_tos & IPTOS_LOWDELAY)
899 	    m0->m_flags |= M_HIGHPRI;
900 	break;
901 #endif
902 #ifdef INET6
903     case AF_INET6:
904 	address = PPP_ALLSTATIONS;	/*XXX*/
905 	control = PPP_UI;		/*XXX*/
906 	protocol = PPP_IPV6;
907 	mode = sc->sc_npmode[NP_IPV6];
908 
909 #if 0	/* XXX flowinfo/traffic class, maybe? */
910 	/*
911 	 * If this packet has the "low delay" bit set in the IP header,
912 	 * put it on the fastq instead.
913 	 */
914 	ip = mtod(m0, struct ip *);
915 	if (ip->ip_tos & IPTOS_LOWDELAY)
916 	    m0->m_flags |= M_HIGHPRI;
917 #endif
918 	break;
919 #endif
920     case AF_UNSPEC:
921 	address = PPP_ADDRESS(dst->sa_data);
922 	control = PPP_CONTROL(dst->sa_data);
923 	protocol = PPP_PROTOCOL(dst->sa_data);
924 	mode = NPMODE_PASS;
925 	break;
926     default:
927 	printf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
928 	error = EAFNOSUPPORT;
929 	goto bad;
930     }
931 
932     /*
933      * Drop this packet, or return an error, if necessary.
934      */
935     if (mode == NPMODE_ERROR) {
936 	error = ENETDOWN;
937 	goto bad;
938     }
939     if (mode == NPMODE_DROP) {
940 	error = 0;
941 	goto bad;
942     }
943 
944     /*
945      * Add PPP header.
946      */
947     M_PREPEND(m0, PPP_HDRLEN, M_DONTWAIT);
948     if (m0 == NULL) {
949 	error = ENOBUFS;
950 	goto bad;
951     }
952 
953     cp = mtod(m0, u_char *);
954     *cp++ = address;
955     *cp++ = control;
956     *cp++ = protocol >> 8;
957     *cp++ = protocol & 0xff;
958 
959     len = m_length(m0);
960 
961     if (sc->sc_flags & SC_LOG_OUTPKT) {
962 	printf("%s output: ", ifp->if_xname);
963 	pppdumpm(m0);
964     }
965 
966     if ((protocol & 0x8000) == 0) {
967 #ifdef PPP_FILTER
968 	/*
969 	 * Apply the pass and active filters to the packet,
970 	 * but only if it is a data packet.
971 	 */
972 	if (sc->sc_pass_filt_out.bf_insns != 0
973 	    && bpf_filter(sc->sc_pass_filt_out.bf_insns, (u_char *) m0,
974 			  len, 0) == 0) {
975 	    error = 0;		/* drop this packet */
976 	    goto bad;
977 	}
978 
979 	/*
980 	 * Update the time we sent the most recent packet.
981 	 */
982 	if (sc->sc_active_filt_out.bf_insns == 0
983 	    || bpf_filter(sc->sc_active_filt_out.bf_insns, (u_char *) m0,
984 	    		  len, 0))
985 	    sc->sc_last_sent = time_second;
986 #else
987 	/*
988 	 * Update the time we sent the most recent packet.
989 	 */
990 	sc->sc_last_sent = time_second;
991 #endif /* PPP_FILTER */
992     }
993 
994     /*
995      * See if bpf wants to look at the packet.
996      */
997     bpf_mtap(&sc->sc_if, m0);
998 
999     /*
1000      * Put the packet on the appropriate queue.
1001      */
1002     s = splnet();
1003     if (mode == NPMODE_QUEUE) {
1004 	/* XXX we should limit the number of packets on this queue */
1005 	*sc->sc_npqtail = m0;
1006 	m0->m_nextpkt = NULL;
1007 	sc->sc_npqtail = &m0->m_nextpkt;
1008     } else {
1009 	ifq = (m0->m_flags & M_HIGHPRI) ? &sc->sc_fastq : NULL;
1010 	if ((error = ifq_enqueue2(&sc->sc_if, ifq, m0
1011 		ALTQ_COMMA ALTQ_DECL(&pktattr))) != 0) {
1012 	    splx(s);
1013 	    sc->sc_if.if_oerrors++;
1014 	    sc->sc_stats.ppp_oerrors++;
1015 	    return (error);
1016 	}
1017 	ppp_restart(sc);
1018     }
1019     ifp->if_opackets++;
1020     ifp->if_obytes += len;
1021 
1022     splx(s);
1023     return (0);
1024 
1025 bad:
1026     m_freem(m0);
1027     return (error);
1028 }
1029 
1030 /*
1031  * After a change in the NPmode for some NP, move packets from the
1032  * npqueue to the send queue or the fast queue as appropriate.
1033  * Should be called at splnet, since we muck with the queues.
1034  */
1035 static void
1036 ppp_requeue(struct ppp_softc *sc)
1037 {
1038     struct mbuf *m, **mpp;
1039     struct ifqueue *ifq;
1040     enum NPmode mode;
1041     int error;
1042 
1043     for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
1044 	switch (PPP_PROTOCOL(mtod(m, u_char *))) {
1045 	case PPP_IP:
1046 	    mode = sc->sc_npmode[NP_IP];
1047 	    break;
1048 	case PPP_IPV6:
1049 	    mode = sc->sc_npmode[NP_IPV6];
1050 	    break;
1051 	default:
1052 	    mode = NPMODE_PASS;
1053 	}
1054 
1055 	switch (mode) {
1056 	case NPMODE_PASS:
1057 	    /*
1058 	     * This packet can now go on one of the queues to be sent.
1059 	     */
1060 	    *mpp = m->m_nextpkt;
1061 	    m->m_nextpkt = NULL;
1062 	    ifq = (m->m_flags & M_HIGHPRI) ? &sc->sc_fastq : NULL;
1063 	    if ((error = ifq_enqueue2(&sc->sc_if, ifq, m ALTQ_COMMA
1064 		ALTQ_DECL(NULL))) != 0) {
1065 		sc->sc_if.if_oerrors++;
1066 		sc->sc_stats.ppp_oerrors++;
1067 	    }
1068 	    break;
1069 
1070 	case NPMODE_DROP:
1071 	case NPMODE_ERROR:
1072 	    *mpp = m->m_nextpkt;
1073 	    m_freem(m);
1074 	    break;
1075 
1076 	case NPMODE_QUEUE:
1077 	    mpp = &m->m_nextpkt;
1078 	    break;
1079 	}
1080     }
1081     sc->sc_npqtail = mpp;
1082 }
1083 
1084 /*
1085  * Transmitter has finished outputting some stuff;
1086  * remember to call sc->sc_start later at splsoftnet.
1087  */
1088 void
1089 ppp_restart(struct ppp_softc *sc)
1090 {
1091     int s = splhigh();	/* XXX IMP ME HARDER */
1092 
1093     sc->sc_flags &= ~SC_TBUSY;
1094     softint_schedule(sc->sc_si);
1095     splx(s);
1096 }
1097 
1098 /*
1099  * Get a packet to send.  This procedure is intended to be called at
1100  * splsoftnet, since it may involve time-consuming operations such as
1101  * applying VJ compression, packet compression, address/control and/or
1102  * protocol field compression to the packet.
1103  */
1104 struct mbuf *
1105 ppp_dequeue(struct ppp_softc *sc)
1106 {
1107     struct mbuf *m, *mp;
1108     u_char *cp;
1109     int address, control, protocol;
1110     int s;
1111 
1112     /*
1113      * Grab a packet to send: first try the fast queue, then the
1114      * normal queue.
1115      */
1116     s = splnet();
1117     if (sc->sc_nfastq < sc->sc_maxfastq) {
1118 	IF_DEQUEUE(&sc->sc_fastq, m);
1119 	if (m != NULL)
1120 	    sc->sc_nfastq++;
1121 	else
1122 	    IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
1123     } else {
1124 	sc->sc_nfastq = 0;
1125 	IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
1126 	if (m == NULL) {
1127 	    IF_DEQUEUE(&sc->sc_fastq, m);
1128 	    if (m != NULL)
1129 		sc->sc_nfastq++;
1130 	}
1131     }
1132     splx(s);
1133 
1134     if (m == NULL)
1135 	return NULL;
1136 
1137     ++sc->sc_stats.ppp_opackets;
1138 
1139     /*
1140      * Extract the ppp header of the new packet.
1141      * The ppp header will be in one mbuf.
1142      */
1143     cp = mtod(m, u_char *);
1144     address = PPP_ADDRESS(cp);
1145     control = PPP_CONTROL(cp);
1146     protocol = PPP_PROTOCOL(cp);
1147 
1148     switch (protocol) {
1149     case PPP_IP:
1150 #ifdef VJC
1151 	/*
1152 	 * If the packet is a TCP/IP packet, see if we can compress it.
1153 	 */
1154 	if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
1155 	    struct ip *ip;
1156 	    int type;
1157 
1158 	    mp = m;
1159 	    ip = (struct ip *) (cp + PPP_HDRLEN);
1160 	    if (mp->m_len <= PPP_HDRLEN) {
1161 		mp = mp->m_next;
1162 		if (mp == NULL)
1163 		    break;
1164 		ip = mtod(mp, struct ip *);
1165 	    }
1166 	    /* this code assumes the IP/TCP header is in one non-shared mbuf */
1167 	    if (ip->ip_p == IPPROTO_TCP) {
1168 		type = sl_compress_tcp(mp, ip, sc->sc_comp,
1169 				       !(sc->sc_flags & SC_NO_TCP_CCID));
1170 		switch (type) {
1171 		case TYPE_UNCOMPRESSED_TCP:
1172 		    protocol = PPP_VJC_UNCOMP;
1173 		    break;
1174 		case TYPE_COMPRESSED_TCP:
1175 		    protocol = PPP_VJC_COMP;
1176 		    cp = mtod(m, u_char *);
1177 		    cp[0] = address;	/* header has moved */
1178 		    cp[1] = control;
1179 		    cp[2] = 0;
1180 		    break;
1181 		}
1182 		cp[3] = protocol;	/* update protocol in PPP header */
1183 	    }
1184 	}
1185 #endif	/* VJC */
1186 	break;
1187 
1188 #ifdef PPP_COMPRESS
1189     case PPP_CCP:
1190 	ppp_ccp(sc, m, 0);
1191 	break;
1192 #endif	/* PPP_COMPRESS */
1193     }
1194 
1195 #ifdef PPP_COMPRESS
1196     if (protocol != PPP_LCP && protocol != PPP_CCP
1197 	&& sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1198 	struct mbuf *mcomp = NULL;
1199 	int slen;
1200 
1201 	slen = 0;
1202 	for (mp = m; mp != NULL; mp = mp->m_next)
1203 	    slen += mp->m_len;
1204 	(*sc->sc_xcomp->compress)
1205 	    (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
1206 	if (mcomp != NULL) {
1207 	    if (sc->sc_flags & SC_CCP_UP) {
1208 		/* Send the compressed packet instead of the original. */
1209 		m_freem(m);
1210 		m = mcomp;
1211 		cp = mtod(m, u_char *);
1212 		protocol = cp[3];
1213 	    } else {
1214 		/* Can't transmit compressed packets until CCP is up. */
1215 		m_freem(mcomp);
1216 	    }
1217 	}
1218     }
1219 #endif	/* PPP_COMPRESS */
1220 
1221     /*
1222      * Compress the address/control and protocol, if possible.
1223      */
1224     if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1225 	control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1226 	protocol != PPP_LCP) {
1227 	/* can compress address/control */
1228 	m->m_data += 2;
1229 	m->m_len -= 2;
1230     }
1231     if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1232 	/* can compress protocol */
1233 	if (mtod(m, u_char *) == cp) {
1234 	    cp[2] = cp[1];	/* move address/control up */
1235 	    cp[1] = cp[0];
1236 	}
1237 	++m->m_data;
1238 	--m->m_len;
1239     }
1240 
1241     return m;
1242 }
1243 
1244 /*
1245  * Software interrupt routine, called at splsoftnet.
1246  */
1247 static void
1248 pppintr(void *arg)
1249 {
1250 	struct ppp_softc *sc = arg;
1251 	struct mbuf *m;
1252 	int s;
1253 
1254 	mutex_enter(softnet_lock);
1255 	if (!(sc->sc_flags & SC_TBUSY)
1256 	    && (IFQ_IS_EMPTY(&sc->sc_if.if_snd) == 0 || sc->sc_fastq.ifq_head
1257 		|| sc->sc_outm)) {
1258 		s = splhigh();	/* XXX IMP ME HARDER */
1259 		sc->sc_flags |= SC_TBUSY;
1260 		splx(s);
1261 		(*sc->sc_start)(sc);
1262 	}
1263 	for (;;) {
1264 		s = splnet();
1265 		IF_DEQUEUE(&sc->sc_rawq, m);
1266 		splx(s);
1267 		if (m == NULL)
1268 			break;
1269 		ppp_inproc(sc, m);
1270 	}
1271 	mutex_exit(softnet_lock);
1272 }
1273 
1274 #ifdef PPP_COMPRESS
1275 /*
1276  * Handle a CCP packet.  `rcvd' is 1 if the packet was received,
1277  * 0 if it is about to be transmitted.
1278  */
1279 static void
1280 ppp_ccp(struct ppp_softc *sc, struct mbuf *m, int rcvd)
1281 {
1282     u_char *dp, *ep;
1283     struct mbuf *mp;
1284     int slen, s;
1285 
1286     /*
1287      * Get a pointer to the data after the PPP header.
1288      */
1289     if (m->m_len <= PPP_HDRLEN) {
1290 	mp = m->m_next;
1291 	if (mp == NULL)
1292 	    return;
1293 	dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1294     } else {
1295 	mp = m;
1296 	dp = mtod(mp, u_char *) + PPP_HDRLEN;
1297     }
1298 
1299     ep = mtod(mp, u_char *) + mp->m_len;
1300     if (dp + CCP_HDRLEN > ep)
1301 	return;
1302     slen = CCP_LENGTH(dp);
1303     if (dp + slen > ep) {
1304 	if (sc->sc_flags & SC_DEBUG)
1305 	    printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1306 		dp, slen, mtod(mp, u_char *), mp->m_len);
1307 	return;
1308     }
1309 
1310     switch (CCP_CODE(dp)) {
1311     case CCP_CONFREQ:
1312     case CCP_TERMREQ:
1313     case CCP_TERMACK:
1314 	/* CCP must be going down - disable compression */
1315 	if (sc->sc_flags & SC_CCP_UP) {
1316 	    s = splhigh();	/* XXX IMP ME HARDER */
1317 	    sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1318 	    splx(s);
1319 	}
1320 	break;
1321 
1322     case CCP_CONFACK:
1323 	if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1324 	    && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1325 	    && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1326 	    if (!rcvd) {
1327 		/* we're agreeing to send compressed packets. */
1328 		if (sc->sc_xc_state != NULL
1329 		    && (*sc->sc_xcomp->comp_init)
1330 			(sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1331 			 sc->sc_unit, 0, sc->sc_flags & SC_DEBUG)) {
1332 		    s = splhigh();	/* XXX IMP ME HARDER */
1333 		    sc->sc_flags |= SC_COMP_RUN;
1334 		    splx(s);
1335 		}
1336 	    } else {
1337 		/* peer is agreeing to send compressed packets. */
1338 		if (sc->sc_rc_state != NULL
1339 		    && (*sc->sc_rcomp->decomp_init)
1340 			(sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1341 			 sc->sc_unit, 0, sc->sc_mru,
1342 			 sc->sc_flags & SC_DEBUG)) {
1343 		    s = splhigh();	/* XXX IMP ME HARDER */
1344 		    sc->sc_flags |= SC_DECOMP_RUN;
1345 		    sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1346 		    splx(s);
1347 		}
1348 	    }
1349 	}
1350 	break;
1351 
1352     case CCP_RESETACK:
1353 	if (sc->sc_flags & SC_CCP_UP) {
1354 	    if (!rcvd) {
1355 		if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1356 		    (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1357 	    } else {
1358 		if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1359 		    (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1360 		    s = splhigh();	/* XXX IMP ME HARDER */
1361 		    sc->sc_flags &= ~SC_DC_ERROR;
1362 		    splx(s);
1363 		}
1364 	    }
1365 	}
1366 	break;
1367     }
1368 }
1369 
1370 /*
1371  * CCP is down; free (de)compressor state if necessary.
1372  */
1373 static void
1374 ppp_ccp_closed(struct ppp_softc *sc)
1375 {
1376     if (sc->sc_xc_state) {
1377 	(*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1378 	ppp_compressor_rele(sc->sc_xcomp);
1379 	sc->sc_xc_state = NULL;
1380     }
1381     if (sc->sc_rc_state) {
1382 	(*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1383 	ppp_compressor_rele(sc->sc_rcomp);
1384 	sc->sc_rc_state = NULL;
1385     }
1386 }
1387 #endif /* PPP_COMPRESS */
1388 
1389 /*
1390  * PPP packet input routine.
1391  * The caller has checked and removed the FCS and has inserted
1392  * the address/control bytes and the protocol high byte if they
1393  * were omitted.
1394  */
1395 void
1396 ppppktin(struct ppp_softc *sc, struct mbuf *m, int lost)
1397 {
1398     int s = splhigh();	/* XXX IMP ME HARDER */
1399 
1400     if (lost)
1401 	m->m_flags |= M_ERRMARK;
1402     IF_ENQUEUE(&sc->sc_rawq, m);
1403     softint_schedule(sc->sc_si);
1404     splx(s);
1405 }
1406 
1407 /*
1408  * Process a received PPP packet, doing decompression as necessary.
1409  * Should be called at splsoftnet.
1410  */
1411 #define COMPTYPE(proto)	((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1412 			 TYPE_UNCOMPRESSED_TCP)
1413 
1414 static void
1415 ppp_inproc(struct ppp_softc *sc, struct mbuf *m)
1416 {
1417     struct ifnet *ifp = &sc->sc_if;
1418     struct ifqueue *inq;
1419     int s, ilen, proto, rv;
1420     u_char *cp, adrs, ctrl;
1421     struct mbuf *mp, *dmp = NULL;
1422 #ifdef VJC
1423     int xlen;
1424     u_char *iphdr;
1425     u_int hlen;
1426 #endif
1427 
1428     sc->sc_stats.ppp_ipackets++;
1429 
1430     if (sc->sc_flags & SC_LOG_INPKT) {
1431 	ilen = 0;
1432 	for (mp = m; mp != NULL; mp = mp->m_next)
1433 	    ilen += mp->m_len;
1434 	printf("%s: got %d bytes\n", ifp->if_xname, ilen);
1435 	pppdumpm(m);
1436     }
1437 
1438     cp = mtod(m, u_char *);
1439     adrs = PPP_ADDRESS(cp);
1440     ctrl = PPP_CONTROL(cp);
1441     proto = PPP_PROTOCOL(cp);
1442 
1443     if (m->m_flags & M_ERRMARK) {
1444 	m->m_flags &= ~M_ERRMARK;
1445 	s = splhigh();	/* XXX IMP ME HARDER */
1446 	sc->sc_flags |= SC_VJ_RESET;
1447 	splx(s);
1448     }
1449 
1450 #ifdef PPP_COMPRESS
1451     /*
1452      * Decompress this packet if necessary, update the receiver's
1453      * dictionary, or take appropriate action on a CCP packet.
1454      */
1455     if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1456 	&& !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1457 	/* decompress this packet */
1458 	rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1459 	if (rv == DECOMP_OK) {
1460 	    m_freem(m);
1461 	    if (dmp == NULL) {
1462 		/* no error, but no decompressed packet produced */
1463 		return;
1464 	    }
1465 	    m = dmp;
1466 	    cp = mtod(m, u_char *);
1467 	    proto = PPP_PROTOCOL(cp);
1468 
1469 	} else {
1470 	    /*
1471 	     * An error has occurred in decompression.
1472 	     * Pass the compressed packet up to pppd, which may take
1473 	     * CCP down or issue a Reset-Req.
1474 	     */
1475 	    if (sc->sc_flags & SC_DEBUG)
1476 		printf("%s: decompress failed %d\n", ifp->if_xname, rv);
1477 	    s = splhigh();	/* XXX IMP ME HARDER */
1478 	    sc->sc_flags |= SC_VJ_RESET;
1479 	    if (rv == DECOMP_ERROR)
1480 		sc->sc_flags |= SC_DC_ERROR;
1481 	    else
1482 		sc->sc_flags |= SC_DC_FERROR;
1483 	    splx(s);
1484 	}
1485 
1486     } else {
1487 	if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1488 	    (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1489 	}
1490 	if (proto == PPP_CCP) {
1491 	    ppp_ccp(sc, m, 1);
1492 	}
1493     }
1494 #endif
1495 
1496     ilen = 0;
1497     for (mp = m; mp != NULL; mp = mp->m_next)
1498 	ilen += mp->m_len;
1499 
1500 #ifdef VJC
1501     if (sc->sc_flags & SC_VJ_RESET) {
1502 	/*
1503 	 * If we've missed a packet, we must toss subsequent compressed
1504 	 * packets which don't have an explicit connection ID.
1505 	 */
1506 	if (sc->sc_comp)
1507 	    sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1508 	s = splhigh();	/* XXX IMP ME HARDER */
1509 	sc->sc_flags &= ~SC_VJ_RESET;
1510 	splx(s);
1511     }
1512 
1513     /*
1514      * See if we have a VJ-compressed packet to uncompress.
1515      */
1516     if (proto == PPP_VJC_COMP) {
1517 	if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1518 	    goto bad;
1519 
1520 	xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1521 				      ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1522 				      sc->sc_comp, &iphdr, &hlen);
1523 
1524 	if (xlen <= 0) {
1525 	    if (sc->sc_flags & SC_DEBUG)
1526 		printf("%s: VJ uncompress failed on type comp\n",
1527 		    ifp->if_xname);
1528 	    goto bad;
1529 	}
1530 
1531 	/* Copy the PPP and IP headers into a new mbuf. */
1532 	MGETHDR(mp, M_DONTWAIT, MT_DATA);
1533 	if (mp == NULL)
1534 	    goto bad;
1535 	mp->m_len = 0;
1536 	mp->m_next = NULL;
1537 	if (hlen + PPP_HDRLEN > MHLEN) {
1538 	    MCLGET(mp, M_DONTWAIT);
1539 	    if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1540 		m_freem(mp);
1541 		goto bad;	/* lose if big headers and no clusters */
1542 	    }
1543 	}
1544 	cp = mtod(mp, u_char *);
1545 	cp[0] = adrs;
1546 	cp[1] = ctrl;
1547 	cp[2] = 0;
1548 	cp[3] = PPP_IP;
1549 	proto = PPP_IP;
1550 	bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1551 	mp->m_len = hlen + PPP_HDRLEN;
1552 
1553 	/*
1554 	 * Trim the PPP and VJ headers off the old mbuf
1555 	 * and stick the new and old mbufs together.
1556 	 */
1557 	m->m_data += PPP_HDRLEN + xlen;
1558 	m->m_len -= PPP_HDRLEN + xlen;
1559 	if (m->m_len <= M_TRAILINGSPACE(mp)) {
1560 	    bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1561 	    mp->m_len += m->m_len;
1562 	    MFREE(m, mp->m_next);
1563 	} else
1564 	    mp->m_next = m;
1565 	m = mp;
1566 	ilen += hlen - xlen;
1567 
1568     } else if (proto == PPP_VJC_UNCOMP) {
1569 	if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1570 	    goto bad;
1571 
1572 	xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1573 				      ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1574 				      sc->sc_comp, &iphdr, &hlen);
1575 
1576 	if (xlen < 0) {
1577 	    if (sc->sc_flags & SC_DEBUG)
1578 		printf("%s: VJ uncompress failed on type uncomp\n",
1579 		    ifp->if_xname);
1580 	    goto bad;
1581 	}
1582 
1583 	proto = PPP_IP;
1584 	cp[3] = PPP_IP;
1585     }
1586 #endif /* VJC */
1587 
1588     /*
1589      * If the packet will fit in a header mbuf, don't waste a
1590      * whole cluster on it.
1591      */
1592     if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1593 	MGETHDR(mp, M_DONTWAIT, MT_DATA);
1594 	if (mp != NULL) {
1595 	    m_copydata(m, 0, ilen, mtod(mp, void *));
1596 	    m_freem(m);
1597 	    m = mp;
1598 	    m->m_len = ilen;
1599 	}
1600     }
1601     m->m_pkthdr.len = ilen;
1602     m->m_pkthdr.rcvif = ifp;
1603 
1604     if ((proto & 0x8000) == 0) {
1605 #ifdef PPP_FILTER
1606 	/*
1607 	 * See whether we want to pass this packet, and
1608 	 * if it counts as link activity.
1609 	 */
1610 	if (sc->sc_pass_filt_in.bf_insns != 0
1611 	    && bpf_filter(sc->sc_pass_filt_in.bf_insns, (u_char *) m,
1612 			  ilen, 0) == 0) {
1613 	    /* drop this packet */
1614 	    m_freem(m);
1615 	    return;
1616 	}
1617 	if (sc->sc_active_filt_in.bf_insns == 0
1618 	    || bpf_filter(sc->sc_active_filt_in.bf_insns, (u_char *) m,
1619 	    		  ilen, 0))
1620 	    sc->sc_last_recv = time_second;
1621 #else
1622 	/*
1623 	 * Record the time that we received this packet.
1624 	 */
1625 	sc->sc_last_recv = time_second;
1626 #endif /* PPP_FILTER */
1627     }
1628 
1629     /* See if bpf wants to look at the packet. */
1630     bpf_mtap(&sc->sc_if, m);
1631 
1632     rv = 0;
1633     switch (proto) {
1634 #ifdef INET
1635     case PPP_IP:
1636 	/*
1637 	 * IP packet - take off the ppp header and pass it up to IP.
1638 	 */
1639 	if ((ifp->if_flags & IFF_UP) == 0
1640 	    || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1641 	    /* interface is down - drop the packet. */
1642 	    m_freem(m);
1643 	    return;
1644 	}
1645 	m->m_pkthdr.len -= PPP_HDRLEN;
1646 	m->m_data += PPP_HDRLEN;
1647 	m->m_len -= PPP_HDRLEN;
1648 #ifdef GATEWAY
1649 	if (ipflow_fastforward(m))
1650 		return;
1651 #endif
1652 	schednetisr(NETISR_IP);
1653 	inq = &ipintrq;
1654 	break;
1655 #endif
1656 
1657 #ifdef INET6
1658     case PPP_IPV6:
1659 	/*
1660 	 * IPv6 packet - take off the ppp header and pass it up to IPv6.
1661 	 */
1662 	if ((ifp->if_flags & IFF_UP) == 0
1663 	    || sc->sc_npmode[NP_IPV6] != NPMODE_PASS) {
1664 	    /* interface is down - drop the packet. */
1665 	    m_freem(m);
1666 	    return;
1667 	}
1668 	m->m_pkthdr.len -= PPP_HDRLEN;
1669 	m->m_data += PPP_HDRLEN;
1670 	m->m_len -= PPP_HDRLEN;
1671 #ifdef GATEWAY
1672 	if (ip6flow_fastforward(m))
1673 		return;
1674 #endif
1675 	schednetisr(NETISR_IPV6);
1676 	inq = &ip6intrq;
1677 	break;
1678 #endif
1679 
1680     default:
1681 	/*
1682 	 * Some other protocol - place on input queue for read().
1683 	 */
1684 	inq = &sc->sc_inq;
1685 	rv = 1;
1686 	break;
1687     }
1688 
1689     /*
1690      * Put the packet on the appropriate input queue.
1691      */
1692     s = splnet();
1693     if (IF_QFULL(inq)) {
1694 	IF_DROP(inq);
1695 	splx(s);
1696 	if (sc->sc_flags & SC_DEBUG)
1697 	    printf("%s: input queue full\n", ifp->if_xname);
1698 	ifp->if_iqdrops++;
1699 	goto bad;
1700     }
1701     IF_ENQUEUE(inq, m);
1702     splx(s);
1703     ifp->if_ipackets++;
1704     ifp->if_ibytes += ilen;
1705 
1706     if (rv)
1707 	(*sc->sc_ctlp)(sc);
1708 
1709     return;
1710 
1711  bad:
1712     m_freem(m);
1713     sc->sc_if.if_ierrors++;
1714     sc->sc_stats.ppp_ierrors++;
1715 }
1716 
1717 #define MAX_DUMP_BYTES	128
1718 
1719 static void
1720 pppdumpm(struct mbuf *m0)
1721 {
1722     char buf[3*MAX_DUMP_BYTES+4];
1723     char *bp = buf;
1724     struct mbuf *m;
1725 
1726     for (m = m0; m; m = m->m_next) {
1727 	int l = m->m_len;
1728 	u_char *rptr = (u_char *)m->m_data;
1729 
1730 	while (l--) {
1731 	    if (bp > buf + sizeof(buf) - 4)
1732 		goto done;
1733 	    *bp++ = hexdigits[*rptr >> 4]; /* convert byte to ascii hex */
1734 	    *bp++ = hexdigits[*rptr++ & 0xf];
1735 	}
1736 
1737 	if (m->m_next) {
1738 	    if (bp > buf + sizeof(buf) - 3)
1739 		goto done;
1740 	    *bp++ = '|';
1741 	} else
1742 	    *bp++ = ' ';
1743     }
1744 done:
1745     if (m)
1746 	*bp++ = '>';
1747     *bp = 0;
1748     printf("%s\n", buf);
1749 }
1750 
1751 #ifdef ALTQ
1752 /*
1753  * a wrapper to transmit a packet from if_start since ALTQ uses
1754  * if_start to send a packet.
1755  */
1756 static void
1757 ppp_ifstart(struct ifnet *ifp)
1758 {
1759 	struct ppp_softc *sc;
1760 
1761 	sc = ifp->if_softc;
1762 	(*sc->sc_start)(sc);
1763 }
1764 #endif
1765 
1766 static const struct ppp_known_compressor {
1767 	uint8_t code;
1768 	const char *module;
1769 } ppp_known_compressors[] = {
1770 	{ CI_DEFLATE, "ppp_deflate" },
1771 	{ CI_DEFLATE_DRAFT, "ppp_deflate" },
1772 	{ CI_BSD_COMPRESS, "ppp_bsdcomp" },
1773 	{ CI_MPPE, "ppp_mppe" },
1774 	{ 0, NULL }
1775 };
1776 
1777 static int
1778 ppp_compressor_init(void)
1779 {
1780 
1781 	mutex_init(&ppp_compressors_mtx, MUTEX_DEFAULT, IPL_NONE);
1782 	return 0;
1783 }
1784 
1785 static void
1786 ppp_compressor_rele(struct compressor *cp)
1787 {
1788 
1789 	mutex_enter(&ppp_compressors_mtx);
1790 	--cp->comp_refcnt;
1791 	mutex_exit(&ppp_compressors_mtx);
1792 }
1793 
1794 static struct compressor *
1795 ppp_get_compressor_noload(uint8_t ci, bool hold)
1796 {
1797 	struct compressor *cp;
1798 
1799 	KASSERT(mutex_owned(&ppp_compressors_mtx));
1800 	LIST_FOREACH(cp, &ppp_compressors, comp_list) {
1801 		if (cp->compress_proto == ci) {
1802 			if (hold)
1803 				++cp->comp_refcnt;
1804 			return cp;
1805 		}
1806 	}
1807 
1808 	return NULL;
1809 }
1810 
1811 static struct compressor *
1812 ppp_get_compressor(uint8_t ci)
1813 {
1814 	struct compressor *cp = NULL;
1815 	const struct ppp_known_compressor *pkc;
1816 
1817 	mutex_enter(&ppp_compressors_mtx);
1818 	cp = ppp_get_compressor_noload(ci, true);
1819 	mutex_exit(&ppp_compressors_mtx);
1820 	if (cp != NULL)
1821 		return cp;
1822 
1823 	kernconfig_lock();
1824 	mutex_enter(&ppp_compressors_mtx);
1825 	cp = ppp_get_compressor_noload(ci, true);
1826 	mutex_exit(&ppp_compressors_mtx);
1827 	if (cp == NULL) {
1828 		/* Not found, so try to autoload a module */
1829 		for (pkc = ppp_known_compressors; pkc->module != NULL; pkc++) {
1830 			if (pkc->code == ci) {
1831 				if (module_autoload(pkc->module,
1832 				    MODULE_CLASS_MISC) != 0)
1833 					break;
1834 				mutex_enter(&ppp_compressors_mtx);
1835 				cp = ppp_get_compressor_noload(ci, true);
1836 				mutex_exit(&ppp_compressors_mtx);
1837 				break;
1838 			}
1839 		}
1840 	}
1841 	kernconfig_unlock();
1842 
1843 	return cp;
1844 }
1845 
1846 int
1847 ppp_register_compressor(struct compressor *pc, size_t ncomp)
1848 {
1849 	int error = 0;
1850 	size_t i;
1851 
1852 	RUN_ONCE(&ppp_compressor_mtx_init, ppp_compressor_init);
1853 
1854 	mutex_enter(&ppp_compressors_mtx);
1855 	for (i = 0; i < ncomp; i++) {
1856 		if (ppp_get_compressor_noload(pc[i].compress_proto,
1857 		    false) != NULL)
1858 			error = EEXIST;
1859 	}
1860 	if (!error) {
1861 		for (i = 0; i < ncomp; i++) {
1862 			pc[i].comp_refcnt = 0;
1863 			LIST_INSERT_HEAD(&ppp_compressors, &pc[i], comp_list);
1864 		}
1865 	}
1866 	mutex_exit(&ppp_compressors_mtx);
1867 
1868 	return error;
1869 }
1870 
1871 int
1872 ppp_unregister_compressor(struct compressor *pc, size_t ncomp)
1873 {
1874 	int error = 0;
1875 	size_t i;
1876 
1877 	mutex_enter(&ppp_compressors_mtx);
1878 	for (i = 0; i < ncomp; i++) {
1879 		if (ppp_get_compressor_noload(pc[i].compress_proto,
1880 		    false) != &pc[i])
1881 			error = ENOENT;
1882 		else if (pc[i].comp_refcnt != 0)
1883 			error = EBUSY;
1884 	}
1885 	if (!error) {
1886 		for (i = 0; i < ncomp; i++) {
1887 			LIST_REMOVE(&pc[i], comp_list);
1888 		}
1889 	}
1890 	mutex_exit(&ppp_compressors_mtx);
1891 
1892 	return error;
1893 }
1894