if_ppp.c revision 1.168 1 /* $NetBSD: if_ppp.c,v 1.168 2022/07/06 08:06:59 riastradh 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.168 2022/07/06 08:06:59 riastradh 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 ||
877 (ifp->if_flags & IFF_RUNNING) == 0 ||
878 ((ifp->if_flags & IFF_UP) == 0 &&
879 dst->sa_family != AF_UNSPEC)) {
880 error = ENETDOWN; /* sort of */
881 goto bad;
882 }
883
884 IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family);
885
886 /*
887 * Compute PPP header.
888 */
889 m0->m_flags &= ~M_HIGHPRI;
890 switch (dst->sa_family) {
891 #ifdef INET
892 case AF_INET:
893 address = PPP_ALLSTATIONS;
894 control = PPP_UI;
895 protocol = PPP_IP;
896 mode = sc->sc_npmode[NP_IP];
897
898 /*
899 * If this packet has the "low delay" bit set in the IP header,
900 * put it on the fastq instead.
901 */
902 ip = mtod(m0, struct ip *);
903 if (ip->ip_tos & IPTOS_LOWDELAY)
904 m0->m_flags |= M_HIGHPRI;
905 break;
906 #endif
907 #ifdef INET6
908 case AF_INET6:
909 address = PPP_ALLSTATIONS; /*XXX*/
910 control = PPP_UI; /*XXX*/
911 protocol = PPP_IPV6;
912 mode = sc->sc_npmode[NP_IPV6];
913
914 #if 0 /* XXX flowinfo/traffic class, maybe? */
915 /*
916 * If this packet has the "low delay" bit set in the IP header,
917 * put it on the fastq instead.
918 */
919 ip = mtod(m0, struct ip *);
920 if (ip->ip_tos & IPTOS_LOWDELAY)
921 m0->m_flags |= M_HIGHPRI;
922 #endif
923 break;
924 #endif
925 case AF_UNSPEC:
926 address = PPP_ADDRESS(dst->sa_data);
927 control = PPP_CONTROL(dst->sa_data);
928 protocol = PPP_PROTOCOL(dst->sa_data);
929 mode = NPMODE_PASS;
930 break;
931 default:
932 printf("%s: af%d not supported\n", ifp->if_xname,
933 dst->sa_family);
934 error = EAFNOSUPPORT;
935 goto bad;
936 }
937
938 /*
939 * Drop this packet, or return an error, if necessary.
940 */
941 if (mode == NPMODE_ERROR) {
942 error = ENETDOWN;
943 goto bad;
944 }
945 if (mode == NPMODE_DROP) {
946 error = 0;
947 goto bad;
948 }
949
950 /*
951 * Add PPP header.
952 */
953 M_PREPEND(m0, PPP_HDRLEN, M_DONTWAIT);
954 if (m0 == NULL) {
955 error = ENOBUFS;
956 goto bad;
957 }
958
959 cp = mtod(m0, u_char *);
960 *cp++ = address;
961 *cp++ = control;
962 *cp++ = protocol >> 8;
963 *cp++ = protocol & 0xff;
964
965 len = m_length(m0);
966
967 if (sc->sc_flags & SC_LOG_OUTPKT) {
968 printf("%s output: ", ifp->if_xname);
969 pppdumpm(m0);
970 }
971
972 if ((protocol & 0x8000) == 0) {
973 #ifdef PPP_FILTER
974 /*
975 * Apply the pass and active filters to the packet,
976 * but only if it is a data packet.
977 */
978 if (sc->sc_pass_filt_out.bf_insns != 0 &&
979 bpf_filter(sc->sc_pass_filt_out.bf_insns,
980 (u_char *)m0, len, 0) == 0) {
981 error = 0; /* drop this packet */
982 goto bad;
983 }
984
985 /*
986 * Update the time we sent the most recent packet.
987 */
988 if (sc->sc_active_filt_out.bf_insns == 0 ||
989 bpf_filter(sc->sc_active_filt_out.bf_insns,
990 (u_char *)m0, len, 0))
991 sc->sc_last_sent = time_second;
992 #else
993 /*
994 * Update the time we sent the most recent packet.
995 */
996 sc->sc_last_sent = time_second;
997 #endif /* PPP_FILTER */
998 }
999
1000 /*
1001 * See if bpf wants to look at the packet.
1002 */
1003 bpf_mtap(&sc->sc_if, m0, BPF_D_OUT);
1004
1005 /*
1006 * Put the packet on the appropriate queue.
1007 */
1008 s = splnet();
1009 if (mode == NPMODE_QUEUE) {
1010 /* XXX we should limit the number of packets on this queue */
1011 *sc->sc_npqtail = m0;
1012 m0->m_nextpkt = NULL;
1013 sc->sc_npqtail = &m0->m_nextpkt;
1014 } else {
1015 ifq = (m0->m_flags & M_HIGHPRI) ? &sc->sc_fastq : NULL;
1016 if ((error = ifq_enqueue2(&sc->sc_if, ifq, m0)) != 0) {
1017 splx(s);
1018 if_statinc(&sc->sc_if, if_oerrors);
1019 sc->sc_stats.ppp_oerrors++;
1020 return error;
1021 }
1022 ppp_restart(sc);
1023 }
1024 if_statadd2(ifp, if_opackets, 1, if_obytes, len);
1025
1026 splx(s);
1027 return 0;
1028
1029 bad:
1030 m_freem(m0);
1031 return error;
1032 }
1033
1034 /*
1035 * After a change in the NPmode for some NP, move packets from the
1036 * npqueue to the send queue or the fast queue as appropriate.
1037 * Should be called at splnet, since we muck with the queues.
1038 */
1039 static void
1040 ppp_requeue(struct ppp_softc *sc)
1041 {
1042 struct mbuf *m, **mpp;
1043 struct ifqueue *ifq;
1044 enum NPmode mode;
1045 int error;
1046
1047 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
1048 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
1049 case PPP_IP:
1050 mode = sc->sc_npmode[NP_IP];
1051 break;
1052 case PPP_IPV6:
1053 mode = sc->sc_npmode[NP_IPV6];
1054 break;
1055 default:
1056 mode = NPMODE_PASS;
1057 }
1058
1059 switch (mode) {
1060 case NPMODE_PASS:
1061 /*
1062 * This packet can now go on one of the queues to
1063 * be sent.
1064 */
1065 *mpp = m->m_nextpkt;
1066 m->m_nextpkt = NULL;
1067 ifq = (m->m_flags & M_HIGHPRI) ? &sc->sc_fastq : NULL;
1068 if ((error = ifq_enqueue2(&sc->sc_if, ifq, m)) != 0) {
1069 if_statinc(&sc->sc_if, if_oerrors);
1070 sc->sc_stats.ppp_oerrors++;
1071 }
1072 break;
1073
1074 case NPMODE_DROP:
1075 case NPMODE_ERROR:
1076 *mpp = m->m_nextpkt;
1077 m_freem(m);
1078 break;
1079
1080 case NPMODE_QUEUE:
1081 mpp = &m->m_nextpkt;
1082 break;
1083 }
1084 }
1085 sc->sc_npqtail = mpp;
1086 }
1087
1088 /*
1089 * Transmitter has finished outputting some stuff;
1090 * remember to call sc->sc_start later at splsoftnet.
1091 */
1092 void
1093 ppp_restart(struct ppp_softc *sc)
1094 {
1095 int s = splhigh(); /* XXX IMP ME HARDER */
1096
1097 sc->sc_flags &= ~SC_TBUSY;
1098 softint_schedule(sc->sc_si);
1099 splx(s);
1100 }
1101
1102 /*
1103 * Get a packet to send. This procedure is intended to be called at
1104 * splsoftnet, since it may involve time-consuming operations such as
1105 * applying VJ compression, packet compression, address/control and/or
1106 * protocol field compression to the packet.
1107 */
1108 struct mbuf *
1109 ppp_dequeue(struct ppp_softc *sc)
1110 {
1111 struct mbuf *m, *mp;
1112 u_char *cp;
1113 int address, control, protocol;
1114 int s;
1115
1116 /*
1117 * Grab a packet to send: first try the fast queue, then the
1118 * normal queue.
1119 */
1120 s = splnet();
1121 if (sc->sc_nfastq < sc->sc_maxfastq) {
1122 IF_DEQUEUE(&sc->sc_fastq, m);
1123 if (m != NULL)
1124 sc->sc_nfastq++;
1125 else
1126 IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
1127 } else {
1128 sc->sc_nfastq = 0;
1129 IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
1130 if (m == NULL) {
1131 IF_DEQUEUE(&sc->sc_fastq, m);
1132 if (m != NULL)
1133 sc->sc_nfastq++;
1134 }
1135 }
1136 splx(s);
1137
1138 if (m == NULL)
1139 return NULL;
1140
1141 ++sc->sc_stats.ppp_opackets;
1142
1143 /*
1144 * Extract the ppp header of the new packet.
1145 * The ppp header will be in one mbuf.
1146 */
1147 cp = mtod(m, u_char *);
1148 address = PPP_ADDRESS(cp);
1149 control = PPP_CONTROL(cp);
1150 protocol = PPP_PROTOCOL(cp);
1151
1152 switch (protocol) {
1153 case PPP_IP:
1154 #ifdef VJC
1155 /*
1156 * If the packet is a TCP/IP packet, see if we can compress it.
1157 */
1158 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
1159 struct ip *ip;
1160 int type;
1161
1162 mp = m;
1163 ip = (struct ip *)(cp + PPP_HDRLEN);
1164 if (mp->m_len <= PPP_HDRLEN) {
1165 mp = mp->m_next;
1166 if (mp == NULL)
1167 break;
1168 ip = mtod(mp, struct ip *);
1169 }
1170 /*
1171 * This code assumes the IP/TCP header is in one
1172 * non-shared mbuf
1173 */
1174 if (ip->ip_p == IPPROTO_TCP) {
1175 type = sl_compress_tcp(mp, ip, sc->sc_comp,
1176 !(sc->sc_flags & SC_NO_TCP_CCID));
1177 switch (type) {
1178 case TYPE_UNCOMPRESSED_TCP:
1179 protocol = PPP_VJC_UNCOMP;
1180 break;
1181 case TYPE_COMPRESSED_TCP:
1182 protocol = PPP_VJC_COMP;
1183 cp = mtod(m, u_char *);
1184 cp[0] = address; /* Header has moved */
1185 cp[1] = control;
1186 cp[2] = 0;
1187 break;
1188 }
1189 /* Update protocol in PPP header */
1190 cp[3] = protocol;
1191 }
1192 }
1193 #endif /* VJC */
1194 break;
1195
1196 #ifdef PPP_COMPRESS
1197 case PPP_CCP:
1198 ppp_ccp(sc, m, 0);
1199 break;
1200 #endif /* PPP_COMPRESS */
1201 }
1202
1203 #ifdef PPP_COMPRESS
1204 if (protocol != PPP_LCP && protocol != PPP_CCP &&
1205 sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1206 struct mbuf *mcomp = NULL;
1207 int slen;
1208
1209 slen = 0;
1210 for (mp = m; mp != NULL; mp = mp->m_next)
1211 slen += mp->m_len;
1212 (*sc->sc_xcomp->compress)
1213 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
1214 if (mcomp != NULL) {
1215 if (sc->sc_flags & SC_CCP_UP) {
1216 /*
1217 * Send the compressed packet instead of the
1218 * original.
1219 */
1220 m_freem(m);
1221 m = mcomp;
1222 cp = mtod(m, u_char *);
1223 protocol = cp[3];
1224 } else {
1225 /*
1226 * Can't transmit compressed packets until CCP
1227 * is up.
1228 */
1229 m_freem(mcomp);
1230 }
1231 }
1232 }
1233 #endif /* PPP_COMPRESS */
1234
1235 /*
1236 * Compress the address/control and protocol, if possible.
1237 */
1238 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1239 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1240 protocol != PPP_LCP) {
1241 /* can compress address/control */
1242 m->m_data += 2;
1243 m->m_len -= 2;
1244 }
1245 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1246 /* can compress protocol */
1247 if (mtod(m, u_char *) == cp) {
1248 cp[2] = cp[1]; /* move address/control up */
1249 cp[1] = cp[0];
1250 }
1251 ++m->m_data;
1252 --m->m_len;
1253 }
1254
1255 return m;
1256 }
1257
1258 /*
1259 * Software interrupt routine, called at splsoftnet.
1260 */
1261 static void
1262 pppintr(void *arg)
1263 {
1264 struct ppp_softc *sc = arg;
1265 struct mbuf *m;
1266 int s;
1267
1268 mutex_enter(softnet_lock);
1269 if (!(sc->sc_flags & SC_TBUSY) &&
1270 (IFQ_IS_EMPTY(&sc->sc_if.if_snd) == 0 ||
1271 sc->sc_fastq.ifq_head ||
1272 sc->sc_outm)) {
1273 s = splhigh(); /* XXX IMP ME HARDER */
1274 sc->sc_flags |= SC_TBUSY;
1275 splx(s);
1276 (*sc->sc_start)(sc);
1277 }
1278 for (;;) {
1279 s = splnet();
1280 IF_DEQUEUE(&sc->sc_rawq, m);
1281 splx(s);
1282 if (m == NULL)
1283 break;
1284 ppp_inproc(sc, m);
1285 }
1286 mutex_exit(softnet_lock);
1287 }
1288
1289 #ifdef PPP_COMPRESS
1290 /*
1291 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1292 * 0 if it is about to be transmitted.
1293 */
1294 static void
1295 ppp_ccp(struct ppp_softc *sc, struct mbuf *m, int rcvd)
1296 {
1297 u_char *dp, *ep;
1298 struct mbuf *mp;
1299 int slen, s;
1300
1301 /*
1302 * Get a pointer to the data after the PPP header.
1303 */
1304 if (m->m_len <= PPP_HDRLEN) {
1305 mp = m->m_next;
1306 if (mp == NULL)
1307 return;
1308 dp = mtod(mp, u_char *);
1309 } else {
1310 mp = m;
1311 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1312 }
1313
1314 ep = mtod(mp, u_char *) + mp->m_len;
1315 if (dp + CCP_HDRLEN > ep)
1316 return;
1317 slen = CCP_LENGTH(dp);
1318 if (dp + slen > ep) {
1319 if (sc->sc_flags & SC_DEBUG)
1320 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1321 dp, slen, mtod(mp, u_char *), mp->m_len);
1322 return;
1323 }
1324
1325 switch (CCP_CODE(dp)) {
1326 case CCP_CONFREQ:
1327 case CCP_TERMREQ:
1328 case CCP_TERMACK:
1329 /* CCP must be going down - disable compression */
1330 if (sc->sc_flags & SC_CCP_UP) {
1331 s = splhigh(); /* XXX IMP ME HARDER */
1332 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1333 splx(s);
1334 }
1335 break;
1336
1337 case CCP_CONFACK:
1338 if (sc->sc_flags & SC_CCP_OPEN &&
1339 !(sc->sc_flags & SC_CCP_UP) &&
1340 slen >= CCP_HDRLEN + CCP_OPT_MINLEN &&
1341 slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1342 if (!rcvd) {
1343 /* We're agreeing to send compressed packets. */
1344 if (sc->sc_xc_state != NULL &&
1345 (*sc->sc_xcomp->comp_init)(sc->sc_xc_state,
1346 dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1347 sc->sc_unit, 0,
1348 sc->sc_flags & SC_DEBUG)) {
1349 s = splhigh(); /* XXX IMP ME HARDER */
1350 sc->sc_flags |= SC_COMP_RUN;
1351 splx(s);
1352 }
1353 } else {
1354 /*
1355 * Peer is agreeing to send compressed
1356 * packets.
1357 */
1358 if (sc->sc_rc_state != NULL &&
1359 (*sc->sc_rcomp->decomp_init)(
1360 sc->sc_rc_state,
1361 dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1362 sc->sc_unit, 0, sc->sc_mru,
1363 sc->sc_flags & SC_DEBUG)) {
1364 s = splhigh(); /* XXX IMP ME HARDER */
1365 sc->sc_flags |= SC_DECOMP_RUN;
1366 sc->sc_flags &=
1367 ~(SC_DC_ERROR | SC_DC_FERROR);
1368 splx(s);
1369 }
1370 }
1371 }
1372 break;
1373
1374 case CCP_RESETACK:
1375 if (sc->sc_flags & SC_CCP_UP) {
1376 if (!rcvd) {
1377 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1378 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1379 } else {
1380 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1381 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1382 s = splhigh(); /* XXX IMP ME HARDER */
1383 sc->sc_flags &= ~SC_DC_ERROR;
1384 splx(s);
1385 }
1386 }
1387 }
1388 break;
1389 }
1390 }
1391
1392 /*
1393 * CCP is down; free (de)compressor state if necessary.
1394 */
1395 static void
1396 ppp_ccp_closed(struct ppp_softc *sc)
1397 {
1398 if (sc->sc_xc_state) {
1399 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1400 ppp_compressor_rele(sc->sc_xcomp);
1401 sc->sc_xc_state = NULL;
1402 }
1403 if (sc->sc_rc_state) {
1404 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1405 ppp_compressor_rele(sc->sc_rcomp);
1406 sc->sc_rc_state = NULL;
1407 }
1408 }
1409 #endif /* PPP_COMPRESS */
1410
1411 /*
1412 * PPP packet input routine.
1413 * The caller has checked and removed the FCS and has inserted
1414 * the address/control bytes and the protocol high byte if they
1415 * were omitted.
1416 */
1417 void
1418 ppppktin(struct ppp_softc *sc, struct mbuf *m, int lost)
1419 {
1420 int s = splhigh(); /* XXX IMP ME HARDER */
1421
1422 if (lost)
1423 m->m_flags |= M_ERRMARK;
1424 IF_ENQUEUE(&sc->sc_rawq, m);
1425 softint_schedule(sc->sc_si);
1426 splx(s);
1427 }
1428
1429 /*
1430 * Process a received PPP packet, doing decompression as necessary.
1431 * Should be called at splsoftnet.
1432 */
1433 #define COMPTYPE(proto) \
1434 ((proto) == PPP_VJC_COMP ? TYPE_COMPRESSED_TCP \
1435 : TYPE_UNCOMPRESSED_TCP)
1436
1437 static void
1438 ppp_inproc(struct ppp_softc *sc, struct mbuf *m)
1439 {
1440 struct ifnet *ifp = &sc->sc_if;
1441 pktqueue_t *pktq = NULL;
1442 struct ifqueue *inq = NULL;
1443 int s, ilen, proto, rv;
1444 u_char *cp, adrs, ctrl;
1445 struct mbuf *mp, *dmp = NULL;
1446 #ifdef VJC
1447 int xlen;
1448 u_char *iphdr;
1449 u_int hlen;
1450 #endif
1451
1452 sc->sc_stats.ppp_ipackets++;
1453
1454 if (sc->sc_flags & SC_LOG_INPKT) {
1455 ilen = 0;
1456 for (mp = m; mp != NULL; mp = mp->m_next)
1457 ilen += mp->m_len;
1458 printf("%s: got %d bytes\n", ifp->if_xname, ilen);
1459 pppdumpm(m);
1460 }
1461
1462 cp = mtod(m, u_char *);
1463 adrs = PPP_ADDRESS(cp);
1464 ctrl = PPP_CONTROL(cp);
1465 proto = PPP_PROTOCOL(cp);
1466
1467 if (m->m_flags & M_ERRMARK) {
1468 m->m_flags &= ~M_ERRMARK;
1469 s = splhigh(); /* XXX IMP ME HARDER */
1470 sc->sc_flags |= SC_VJ_RESET;
1471 splx(s);
1472 }
1473
1474 #ifdef PPP_COMPRESS
1475 /*
1476 * Decompress this packet if necessary, update the receiver's
1477 * dictionary, or take appropriate action on a CCP packet.
1478 */
1479 if (proto == PPP_COMP &&
1480 sc->sc_rc_state &&
1481 (sc->sc_flags & SC_DECOMP_RUN) &&
1482 !(sc->sc_flags & SC_DC_ERROR) &&
1483 !(sc->sc_flags & SC_DC_FERROR)) {
1484 /* Decompress this packet */
1485 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1486 if (rv == DECOMP_OK) {
1487 m_freem(m);
1488 if (dmp == NULL) {
1489 /*
1490 * No error, but no decompressed packet
1491 * produced
1492 */
1493 return;
1494 }
1495 m = dmp;
1496 cp = mtod(m, u_char *);
1497 proto = PPP_PROTOCOL(cp);
1498
1499 } else {
1500 /*
1501 * An error has occurred in decompression.
1502 * Pass the compressed packet up to pppd, which may
1503 * take CCP down or issue a Reset-Req.
1504 */
1505 if (sc->sc_flags & SC_DEBUG)
1506 printf("%s: decompress failed %d\n",
1507 ifp->if_xname, rv);
1508 s = splhigh(); /* XXX IMP ME HARDER */
1509 sc->sc_flags |= SC_VJ_RESET;
1510 if (rv == DECOMP_ERROR)
1511 sc->sc_flags |= SC_DC_ERROR;
1512 else
1513 sc->sc_flags |= SC_DC_FERROR;
1514 splx(s);
1515 }
1516
1517 } else {
1518 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN))
1519 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1520 if (proto == PPP_CCP)
1521 ppp_ccp(sc, m, 1);
1522 }
1523 #endif
1524
1525 ilen = 0;
1526 for (mp = m; mp != NULL; mp = mp->m_next)
1527 ilen += mp->m_len;
1528
1529 #ifdef VJC
1530 if (sc->sc_flags & SC_VJ_RESET) {
1531 /*
1532 * If we've missed a packet, we must toss subsequent compressed
1533 * packets which don't have an explicit connection ID.
1534 */
1535 if (sc->sc_comp)
1536 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1537 s = splhigh(); /* XXX IMP ME HARDER */
1538 sc->sc_flags &= ~SC_VJ_RESET;
1539 splx(s);
1540 }
1541
1542 /*
1543 * See if we have a VJ-compressed packet to uncompress.
1544 */
1545 if (proto == PPP_VJC_COMP) {
1546 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1547 goto bad;
1548
1549 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN,
1550 m->m_len - PPP_HDRLEN, ilen - PPP_HDRLEN,
1551 TYPE_COMPRESSED_TCP, sc->sc_comp, &iphdr, &hlen);
1552 if (xlen <= 0) {
1553 if (sc->sc_flags & SC_DEBUG) {
1554 printf("%s: VJ uncompress failed"
1555 " on type comp\n",
1556 ifp->if_xname);
1557 }
1558 goto bad;
1559 }
1560
1561 /* Copy the PPP and IP headers into a new mbuf. */
1562 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1563 if (mp == NULL)
1564 goto bad;
1565 mp->m_len = 0;
1566 mp->m_next = NULL;
1567 if (hlen + PPP_HDRLEN > MHLEN) {
1568 MCLGET(mp, M_DONTWAIT);
1569 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1570 /* Lose if big headers and no clusters */
1571 m_freem(mp);
1572 goto bad;
1573 }
1574 }
1575 cp = mtod(mp, u_char *);
1576 cp[0] = adrs;
1577 cp[1] = ctrl;
1578 cp[2] = 0;
1579 cp[3] = PPP_IP;
1580 proto = PPP_IP;
1581 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1582 mp->m_len = hlen + PPP_HDRLEN;
1583
1584 /*
1585 * Trim the PPP and VJ headers off the old mbuf
1586 * and stick the new and old mbufs together.
1587 */
1588 m->m_data += PPP_HDRLEN + xlen;
1589 m->m_len -= PPP_HDRLEN + xlen;
1590 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1591 bcopy(mtod(m, u_char *),
1592 mtod(mp, u_char *) + mp->m_len, m->m_len);
1593 mp->m_len += m->m_len;
1594 mp->m_next = m_free(m);
1595 } else
1596 mp->m_next = m;
1597 m = mp;
1598 ilen += hlen - xlen;
1599
1600 } else if (proto == PPP_VJC_UNCOMP) {
1601 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1602 goto bad;
1603
1604 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN,
1605 m->m_len - PPP_HDRLEN, ilen - PPP_HDRLEN,
1606 TYPE_UNCOMPRESSED_TCP, sc->sc_comp, &iphdr, &hlen);
1607 if (xlen < 0) {
1608 if (sc->sc_flags & SC_DEBUG) {
1609 printf("%s: VJ uncompress failed"
1610 " on type uncomp\n",
1611 ifp->if_xname);
1612 }
1613 goto bad;
1614 }
1615
1616 proto = PPP_IP;
1617 cp[3] = PPP_IP;
1618 }
1619 #endif /* VJC */
1620
1621 /*
1622 * If the packet will fit in a header mbuf, don't waste a
1623 * whole cluster on it.
1624 */
1625 if (ilen <= MHLEN && (m->m_flags & M_EXT)) {
1626 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1627 if (mp != NULL) {
1628 m_copydata(m, 0, ilen, mtod(mp, void *));
1629 m_freem(m);
1630 m = mp;
1631 m->m_len = ilen;
1632 }
1633 }
1634 m->m_pkthdr.len = ilen;
1635 m_set_rcvif(m, ifp);
1636
1637 if ((proto & 0x8000) == 0) {
1638 #ifdef PPP_FILTER
1639 /*
1640 * See whether we want to pass this packet, and
1641 * if it counts as link activity.
1642 */
1643 if (sc->sc_pass_filt_in.bf_insns != 0 &&
1644 bpf_filter(sc->sc_pass_filt_in.bf_insns,
1645 (u_char *)m, ilen, 0) == 0) {
1646 /* drop this packet */
1647 m_freem(m);
1648 return;
1649 }
1650 if (sc->sc_active_filt_in.bf_insns == 0 ||
1651 bpf_filter(sc->sc_active_filt_in.bf_insns,
1652 (u_char *)m, ilen, 0))
1653 sc->sc_last_recv = time_second;
1654 #else
1655 /*
1656 * Record the time that we received this packet.
1657 */
1658 sc->sc_last_recv = time_second;
1659 #endif /* PPP_FILTER */
1660 }
1661
1662 /* See if bpf wants to look at the packet. */
1663 bpf_mtap(&sc->sc_if, m, BPF_D_IN);
1664
1665 switch (proto) {
1666 #ifdef INET
1667 case PPP_IP:
1668 /*
1669 * IP packet - take off the ppp header and pass it up to IP.
1670 */
1671 if ((ifp->if_flags & IFF_UP) == 0 ||
1672 sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1673 /* Interface is down - drop the packet. */
1674 m_freem(m);
1675 return;
1676 }
1677 m->m_pkthdr.len -= PPP_HDRLEN;
1678 m->m_data += PPP_HDRLEN;
1679 m->m_len -= PPP_HDRLEN;
1680 #ifdef GATEWAY
1681 if (ipflow_fastforward(m))
1682 return;
1683 #endif
1684 pktq = ip_pktq;
1685 break;
1686 #endif
1687
1688 #ifdef INET6
1689 case PPP_IPV6:
1690 /*
1691 * IPv6 packet - take off the ppp header and pass it up to
1692 * IPv6.
1693 */
1694 if ((ifp->if_flags & IFF_UP) == 0 ||
1695 sc->sc_npmode[NP_IPV6] != NPMODE_PASS) {
1696 /* interface is down - drop the packet. */
1697 m_freem(m);
1698 return;
1699 }
1700 m->m_pkthdr.len -= PPP_HDRLEN;
1701 m->m_data += PPP_HDRLEN;
1702 m->m_len -= PPP_HDRLEN;
1703 #ifdef GATEWAY
1704 if (ip6flow_fastforward(&m))
1705 return;
1706 #endif
1707 pktq = ip6_pktq;
1708 break;
1709 #endif
1710
1711 default:
1712 /*
1713 * Some other protocol - place on input queue for read().
1714 */
1715 inq = &sc->sc_inq;
1716 pktq = NULL;
1717 break;
1718 }
1719
1720 /*
1721 * Put the packet on the appropriate input queue.
1722 */
1723 s = splnet();
1724
1725 /* pktq: inet or inet6 cases */
1726 if (__predict_true(pktq)) {
1727 if (__predict_false(!pktq_enqueue(pktq, m, 0))) {
1728 splx(s);
1729 if_statinc(ifp, if_iqdrops);
1730 goto bad;
1731 }
1732 if_statadd2(ifp, if_ipackets, 1, if_ibytes, ilen);
1733 splx(s);
1734 return;
1735 }
1736
1737 /* ifq: other protocol cases */
1738 if (!inq) {
1739 splx(s);
1740 goto bad;
1741 }
1742 if (IF_QFULL(inq)) {
1743 IF_DROP(inq);
1744 splx(s);
1745 if (sc->sc_flags & SC_DEBUG)
1746 printf("%s: input queue full\n", ifp->if_xname);
1747 if_statinc(ifp, if_iqdrops);
1748 goto bad;
1749 }
1750 IF_ENQUEUE(inq, m);
1751 splx(s);
1752 if_statadd2(ifp, if_ipackets, 1, if_ibytes, ilen);
1753
1754 (*sc->sc_ctlp)(sc);
1755
1756 return;
1757
1758 bad:
1759 m_freem(m);
1760 if_statinc(&sc->sc_if, if_ierrors);
1761 sc->sc_stats.ppp_ierrors++;
1762 }
1763
1764 #define MAX_DUMP_BYTES 128
1765
1766 static void
1767 pppdumpm(struct mbuf *m0)
1768 {
1769 char buf[3*MAX_DUMP_BYTES+4];
1770 char *bp = buf;
1771 struct mbuf *m;
1772
1773 for (m = m0; m; m = m->m_next) {
1774 int l = m->m_len;
1775 u_char *rptr = (u_char *)m->m_data;
1776
1777 while (l--) {
1778 if (bp > buf + sizeof(buf) - 4)
1779 goto done;
1780 /* Convert byte to ascii hex */
1781 *bp++ = hexdigits[*rptr >> 4];
1782 *bp++ = hexdigits[*rptr++ & 0xf];
1783 }
1784
1785 if (m->m_next) {
1786 if (bp > buf + sizeof(buf) - 3)
1787 goto done;
1788 *bp++ = '|';
1789 } else
1790 *bp++ = ' ';
1791 }
1792 done:
1793 if (m)
1794 *bp++ = '>';
1795 *bp = 0;
1796 printf("%s\n", buf);
1797 }
1798
1799 #ifdef ALTQ
1800 /*
1801 * A wrapper to transmit a packet from if_start since ALTQ uses
1802 * if_start to send a packet.
1803 */
1804 static void
1805 ppp_ifstart(struct ifnet *ifp)
1806 {
1807 struct ppp_softc *sc;
1808
1809 sc = ifp->if_softc;
1810 (*sc->sc_start)(sc);
1811 }
1812 #endif
1813
1814 static const struct ppp_known_compressor {
1815 uint8_t code;
1816 const char *module;
1817 } ppp_known_compressors[] = {
1818 { CI_DEFLATE, "ppp_deflate" },
1819 { CI_DEFLATE_DRAFT, "ppp_deflate" },
1820 { CI_BSD_COMPRESS, "ppp_bsdcomp" },
1821 { CI_MPPE, "ppp_mppe" },
1822 { 0, NULL }
1823 };
1824
1825 static int
1826 ppp_compressor_init(void)
1827 {
1828
1829 mutex_init(&ppp_compressors_mtx, MUTEX_DEFAULT, IPL_NONE);
1830 return 0;
1831 }
1832
1833 static int
1834 ppp_compressor_destroy(void)
1835 {
1836
1837 mutex_destroy(&ppp_compressors_mtx);
1838 return 0;
1839 }
1840
1841 static void
1842 ppp_compressor_rele(struct compressor *cp)
1843 {
1844
1845 mutex_enter(&ppp_compressors_mtx);
1846 --cp->comp_refcnt;
1847 mutex_exit(&ppp_compressors_mtx);
1848 }
1849
1850 static struct compressor *
1851 ppp_get_compressor_noload(uint8_t ci, bool hold)
1852 {
1853 struct compressor *cp;
1854
1855 KASSERT(mutex_owned(&ppp_compressors_mtx));
1856 LIST_FOREACH(cp, &ppp_compressors, comp_list) {
1857 if (cp->compress_proto == ci) {
1858 if (hold)
1859 ++cp->comp_refcnt;
1860 return cp;
1861 }
1862 }
1863
1864 return NULL;
1865 }
1866
1867 static struct compressor *
1868 ppp_get_compressor(uint8_t ci)
1869 {
1870 struct compressor *cp = NULL;
1871 const struct ppp_known_compressor *pkc;
1872
1873 mutex_enter(&ppp_compressors_mtx);
1874 cp = ppp_get_compressor_noload(ci, true);
1875 mutex_exit(&ppp_compressors_mtx);
1876 if (cp != NULL)
1877 return cp;
1878
1879 kernconfig_lock();
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 /* Not found, so try to autoload a module */
1885 for (pkc = ppp_known_compressors; pkc->module != NULL; pkc++) {
1886 if (pkc->code == ci) {
1887 if (module_autoload(pkc->module,
1888 MODULE_CLASS_MISC) != 0)
1889 break;
1890 mutex_enter(&ppp_compressors_mtx);
1891 cp = ppp_get_compressor_noload(ci, true);
1892 mutex_exit(&ppp_compressors_mtx);
1893 break;
1894 }
1895 }
1896 }
1897 kernconfig_unlock();
1898
1899 return cp;
1900 }
1901
1902 int
1903 ppp_register_compressor(struct compressor *pc, size_t ncomp)
1904 {
1905 int error = 0;
1906 size_t i;
1907
1908 mutex_enter(&ppp_compressors_mtx);
1909 for (i = 0; i < ncomp; i++) {
1910 if (ppp_get_compressor_noload(pc[i].compress_proto,
1911 false) != NULL)
1912 error = EEXIST;
1913 }
1914 if (!error) {
1915 for (i = 0; i < ncomp; i++) {
1916 pc[i].comp_refcnt = 0;
1917 LIST_INSERT_HEAD(&ppp_compressors, &pc[i], comp_list);
1918 }
1919 }
1920 mutex_exit(&ppp_compressors_mtx);
1921
1922 return error;
1923 }
1924
1925 int
1926 ppp_unregister_compressor(struct compressor *pc, size_t ncomp)
1927 {
1928 int error = 0;
1929 size_t i;
1930
1931 mutex_enter(&ppp_compressors_mtx);
1932 for (i = 0; i < ncomp; i++) {
1933 if (ppp_get_compressor_noload(pc[i].compress_proto,
1934 false) != &pc[i])
1935 error = ENOENT;
1936 else if (pc[i].comp_refcnt != 0)
1937 error = EBUSY;
1938 }
1939 if (!error) {
1940 for (i = 0; i < ncomp; i++) {
1941 LIST_REMOVE(&pc[i], comp_list);
1942 }
1943 }
1944 mutex_exit(&ppp_compressors_mtx);
1945
1946 return error;
1947 }
1948
1949 /*
1950 * Module infrastructure
1951 */
1952 #include "if_module.h"
1953
1954 #ifdef PPP_FILTER
1955 #define PPP_DEP "bpf_filter,"
1956 #else
1957 #define PPP_DEP
1958 #endif
1959
1960 IF_MODULE(MODULE_CLASS_DRIVER, ppp, PPP_DEP "slcompress")
1961