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