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