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