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