if_ppp.c revision 1.161.2.2 1 /* $NetBSD: if_ppp.c,v 1.161.2.2 2020/04/08 14:08:57 martin 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.161.2.2 2020/04/08 14:08:57 martin 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 LIST_HEAD(ppp_sclist, ppp_softc);
210 static struct {
211 struct ppp_sclist list;
212 kmutex_t lock;
213 } ppp_softcs __cacheline_aligned;
214
215 struct if_clone ppp_cloner =
216 IF_CLONE_INITIALIZER("ppp", ppp_clone_create, ppp_clone_destroy);
217
218 #ifdef PPP_COMPRESS
219 static LIST_HEAD(, compressor) ppp_compressors = { NULL };
220 static kmutex_t ppp_compressors_mtx;
221
222 static int ppp_compressor_init(void);
223 static int ppp_compressor_destroy(void);
224 static struct compressor *ppp_get_compressor(uint8_t);
225 static void ppp_compressor_rele(struct compressor *);
226 #endif /* PPP_COMPRESS */
227
228
229 /*
230 * Called from boot code to establish ppp interfaces.
231 */
232 void
233 pppattach(int n __unused)
234 {
235
236 /*
237 * Nothing to do here, initialization is handled by the
238 * module initialization code in pppinit() below).
239 */
240 }
241
242 static void
243 pppinit(void)
244 {
245 /* Init the compressor sub-sub-system */
246 ppp_compressor_init();
247
248 if (ttyldisc_attach(&ppp_disc) != 0)
249 panic("%s", __func__);
250
251 mutex_init(&ppp_softcs.lock, MUTEX_DEFAULT, IPL_NONE);
252 LIST_INIT(&ppp_softcs.list);
253 if_clone_attach(&ppp_cloner);
254 }
255
256 static int
257 pppdetach(void)
258 {
259 int error = 0;
260
261 if (!LIST_EMPTY(&ppp_softcs.list))
262 error = EBUSY;
263
264 if (error == 0)
265 error = ttyldisc_detach(&ppp_disc);
266
267 if (error == 0) {
268 mutex_destroy(&ppp_softcs.lock);
269 if_clone_detach(&ppp_cloner);
270 ppp_compressor_destroy();
271 }
272
273 return error;
274 }
275
276 static struct ppp_softc *
277 ppp_create(const char *name, int unit)
278 {
279 struct ppp_softc *sc, *sci, *scl = NULL;
280
281 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAIT|M_ZERO);
282
283 mutex_enter(&ppp_softcs.lock);
284 if (unit == -1) {
285 int i = 0;
286 LIST_FOREACH(sci, &ppp_softcs.list, sc_iflist) {
287 scl = sci;
288 if (i < sci->sc_unit) {
289 unit = i;
290 break;
291 } else {
292 #ifdef DIAGNOSTIC
293 KASSERT(i == sci->sc_unit);
294 #endif
295 i++;
296 }
297 }
298 if (unit == -1)
299 unit = i;
300 } else {
301 LIST_FOREACH(sci, &ppp_softcs.list, sc_iflist) {
302 scl = sci;
303 if (unit < sci->sc_unit)
304 break;
305 else if (unit == sci->sc_unit) {
306 free(sc, M_DEVBUF);
307 mutex_exit(&ppp_softcs.lock);
308 return NULL;
309 }
310 }
311 }
312
313 if (sci != NULL)
314 LIST_INSERT_BEFORE(sci, sc, sc_iflist);
315 else if (scl != NULL)
316 LIST_INSERT_AFTER(scl, sc, sc_iflist);
317 else
318 LIST_INSERT_HEAD(&ppp_softcs.list, sc, sc_iflist);
319
320 mutex_exit(&ppp_softcs.lock);
321
322 if_initname(&sc->sc_if, name, sc->sc_unit = unit);
323 callout_init(&sc->sc_timo_ch, 0);
324 sc->sc_if.if_softc = sc;
325 sc->sc_if.if_mtu = PPP_MTU;
326 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
327 sc->sc_if.if_type = IFT_PPP;
328 sc->sc_if.if_hdrlen = PPP_HDRLEN;
329 sc->sc_if.if_dlt = DLT_NULL;
330 sc->sc_if.if_ioctl = pppsioctl;
331 sc->sc_if.if_output = pppoutput;
332 #ifdef ALTQ
333 sc->sc_if.if_start = ppp_ifstart;
334 #endif
335 IFQ_SET_MAXLEN(&sc->sc_if.if_snd, IFQ_MAXLEN);
336 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
337 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
338 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
339 /* Ratio of 1:2 packets between the regular and the fast queue */
340 sc->sc_maxfastq = 2;
341 IFQ_SET_READY(&sc->sc_if.if_snd);
342 if_attach(&sc->sc_if);
343 if_alloc_sadl(&sc->sc_if);
344 bpf_attach(&sc->sc_if, DLT_NULL, 0);
345 return sc;
346 }
347
348 static int
349 ppp_clone_create(struct if_clone *ifc, int unit)
350 {
351 return ppp_create(ifc->ifc_name, unit) == NULL ? EEXIST : 0;
352 }
353
354 static int
355 ppp_clone_destroy(struct ifnet *ifp)
356 {
357 struct ppp_softc *sc = (struct ppp_softc *)ifp->if_softc;
358
359 if (sc->sc_devp != NULL)
360 return EBUSY; /* Not removing it */
361
362 mutex_enter(&ppp_softcs.lock);
363 LIST_REMOVE(sc, sc_iflist);
364 mutex_exit(&ppp_softcs.lock);
365
366 bpf_detach(ifp);
367 if_detach(ifp);
368
369 free(sc, M_DEVBUF);
370 return 0;
371 }
372
373 /*
374 * Allocate a ppp interface unit and initialize it.
375 */
376 struct ppp_softc *
377 pppalloc(pid_t pid)
378 {
379 struct ppp_softc *sc = NULL, *scf;
380 int i;
381
382 mutex_enter(&ppp_softcs.lock);
383 LIST_FOREACH(scf, &ppp_softcs.list, sc_iflist) {
384 if (scf->sc_xfer == pid) {
385 scf->sc_xfer = 0;
386 mutex_exit(&ppp_softcs.lock);
387 return scf;
388 }
389 if (scf->sc_devp == NULL && sc == NULL)
390 sc = scf;
391 }
392 mutex_exit(&ppp_softcs.lock);
393
394 if (sc == NULL)
395 sc = ppp_create(ppp_cloner.ifc_name, -1);
396
397 sc->sc_si = softint_establish(SOFTINT_NET, pppintr, sc);
398 if (sc->sc_si == NULL) {
399 printf("%s: unable to establish softintr\n",
400 sc->sc_if.if_xname);
401 return (NULL);
402 }
403 sc->sc_flags = 0;
404 sc->sc_mru = PPP_MRU;
405 sc->sc_relinq = NULL;
406 (void)memset(&sc->sc_stats, 0, sizeof(sc->sc_stats));
407 #ifdef VJC
408 sc->sc_comp = malloc(sizeof(struct slcompress), M_DEVBUF, M_NOWAIT);
409 if (sc->sc_comp)
410 sl_compress_init(sc->sc_comp);
411 #endif
412 #ifdef PPP_COMPRESS
413 sc->sc_xc_state = NULL;
414 sc->sc_rc_state = NULL;
415 #endif /* PPP_COMPRESS */
416 for (i = 0; i < NUM_NP; ++i)
417 sc->sc_npmode[i] = NPMODE_ERROR;
418 sc->sc_npqueue = NULL;
419 sc->sc_npqtail = &sc->sc_npqueue;
420 sc->sc_last_sent = sc->sc_last_recv = time_second;
421
422 return sc;
423 }
424
425 /*
426 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
427 */
428 void
429 pppdealloc(struct ppp_softc *sc)
430 {
431 struct mbuf *m;
432
433 softint_disestablish(sc->sc_si);
434 if_down(&sc->sc_if);
435 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
436 sc->sc_devp = NULL;
437 sc->sc_xfer = 0;
438 for (;;) {
439 IF_DEQUEUE(&sc->sc_rawq, m);
440 if (m == NULL)
441 break;
442 m_freem(m);
443 }
444 for (;;) {
445 IF_DEQUEUE(&sc->sc_inq, m);
446 if (m == NULL)
447 break;
448 m_freem(m);
449 }
450 for (;;) {
451 IF_DEQUEUE(&sc->sc_fastq, m);
452 if (m == NULL)
453 break;
454 m_freem(m);
455 }
456 while ((m = sc->sc_npqueue) != NULL) {
457 sc->sc_npqueue = m->m_nextpkt;
458 m_freem(m);
459 }
460 if (sc->sc_togo != NULL) {
461 m_freem(sc->sc_togo);
462 sc->sc_togo = NULL;
463 }
464 #ifdef PPP_COMPRESS
465 ppp_ccp_closed(sc);
466 sc->sc_xc_state = NULL;
467 sc->sc_rc_state = NULL;
468 #endif /* PPP_COMPRESS */
469 #ifdef PPP_FILTER
470 if (sc->sc_pass_filt_in.bf_insns != 0) {
471 free(sc->sc_pass_filt_in.bf_insns, M_DEVBUF);
472 sc->sc_pass_filt_in.bf_insns = 0;
473 sc->sc_pass_filt_in.bf_len = 0;
474 }
475 if (sc->sc_pass_filt_out.bf_insns != 0) {
476 free(sc->sc_pass_filt_out.bf_insns, M_DEVBUF);
477 sc->sc_pass_filt_out.bf_insns = 0;
478 sc->sc_pass_filt_out.bf_len = 0;
479 }
480 if (sc->sc_active_filt_in.bf_insns != 0) {
481 free(sc->sc_active_filt_in.bf_insns, M_DEVBUF);
482 sc->sc_active_filt_in.bf_insns = 0;
483 sc->sc_active_filt_in.bf_len = 0;
484 }
485 if (sc->sc_active_filt_out.bf_insns != 0) {
486 free(sc->sc_active_filt_out.bf_insns, M_DEVBUF);
487 sc->sc_active_filt_out.bf_insns = 0;
488 sc->sc_active_filt_out.bf_len = 0;
489 }
490 #endif /* PPP_FILTER */
491 #ifdef VJC
492 if (sc->sc_comp != 0) {
493 free(sc->sc_comp, M_DEVBUF);
494 sc->sc_comp = 0;
495 }
496 #endif
497 (void)ppp_clone_destroy(&sc->sc_if);
498 }
499
500 /*
501 * Ioctl routine for generic ppp devices.
502 */
503 int
504 pppioctl(struct ppp_softc *sc, u_long cmd, void *data, int flag,
505 struct lwp *l)
506 {
507 int s, error, flags, mru, npx;
508 u_int nb;
509 struct ppp_option_data *odp;
510 struct compressor *cp;
511 struct npioctl *npi;
512 time_t t;
513 #ifdef PPP_FILTER
514 struct bpf_program *bp, *nbp;
515 struct bpf_insn *newcode, *oldcode;
516 int newcodelen;
517 #endif /* PPP_FILTER */
518 #ifdef PPP_COMPRESS
519 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
520 #endif
521
522 switch (cmd) {
523 case PPPIOCSFLAGS:
524 case PPPIOCSMRU:
525 case PPPIOCSMAXCID:
526 case PPPIOCSCOMPRESS:
527 case PPPIOCSNPMODE:
528 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
529 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, &sc->sc_if,
530 KAUTH_ARG(cmd), NULL) != 0)
531 return (EPERM);
532 break;
533 case PPPIOCXFERUNIT:
534 /* XXX: Why is this privileged?! */
535 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
536 KAUTH_REQ_NETWORK_INTERFACE_GETPRIV, &sc->sc_if,
537 KAUTH_ARG(cmd), NULL) != 0)
538 return (EPERM);
539 break;
540 default:
541 break;
542 }
543
544 switch (cmd) {
545 case FIONREAD:
546 *(int *)data = sc->sc_inq.ifq_len;
547 break;
548
549 case PPPIOCGUNIT:
550 *(int *)data = sc->sc_unit;
551 break;
552
553 case PPPIOCGFLAGS:
554 *(u_int *)data = sc->sc_flags;
555 break;
556
557 case PPPIOCGRAWIN:
558 {
559 struct ppp_rawin *rwin = (struct ppp_rawin *)data;
560 u_char c, q = 0;
561
562 for (c = sc->sc_rawin_start; c < sizeof(sc->sc_rawin.buf);)
563 rwin->buf[q++] = sc->sc_rawin.buf[c++];
564
565 for (c = 0; c < sc->sc_rawin_start;)
566 rwin->buf[q++] = sc->sc_rawin.buf[c++];
567
568 rwin->count = sc->sc_rawin.count;
569 }
570 break;
571
572 case PPPIOCSFLAGS:
573 flags = *(int *)data & SC_MASK;
574 s = splsoftnet();
575 #ifdef PPP_COMPRESS
576 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
577 ppp_ccp_closed(sc);
578 #endif
579 splhigh(); /* XXX IMP ME HARDER */
580 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
581 splx(s);
582 break;
583
584 case PPPIOCSMRU:
585 mru = *(int *)data;
586 if (mru >= PPP_MINMRU && mru <= PPP_MAXMRU)
587 sc->sc_mru = mru;
588 break;
589
590 case PPPIOCGMRU:
591 *(int *)data = sc->sc_mru;
592 break;
593
594 #ifdef VJC
595 case PPPIOCSMAXCID:
596 if (sc->sc_comp) {
597 s = splsoftnet();
598 sl_compress_setup(sc->sc_comp, *(int *)data);
599 splx(s);
600 }
601 break;
602 #endif
603
604 case PPPIOCXFERUNIT:
605 sc->sc_xfer = l->l_proc->p_pid;
606 break;
607
608 #ifdef PPP_COMPRESS
609 case PPPIOCSCOMPRESS:
610 odp = (struct ppp_option_data *) data;
611 nb = odp->length;
612 if (nb > sizeof(ccp_option))
613 nb = sizeof(ccp_option);
614 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
615 return (error);
616 /* preliminary check on the length byte */
617 if (ccp_option[1] < 2)
618 return (EINVAL);
619 cp = ppp_get_compressor(ccp_option[0]);
620 if (cp == NULL) {
621 if (sc->sc_flags & SC_DEBUG)
622 printf("%s: no compressor for [%x %x %x], %x\n",
623 sc->sc_if.if_xname, ccp_option[0],
624 ccp_option[1], ccp_option[2], nb);
625 return (EINVAL); /* no handler found */
626 }
627 /*
628 * Found a handler for the protocol - try to allocate
629 * a compressor or decompressor.
630 */
631 error = 0;
632 if (odp->transmit) {
633 s = splsoftnet();
634 if (sc->sc_xc_state != NULL) {
635 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
636 ppp_compressor_rele(sc->sc_xcomp);
637 }
638 sc->sc_xcomp = cp;
639 sc->sc_xc_state = cp->comp_alloc(ccp_option, nb);
640 if (sc->sc_xc_state == NULL) {
641 if (sc->sc_flags & SC_DEBUG)
642 printf("%s: comp_alloc failed\n",
643 sc->sc_if.if_xname);
644 error = ENOBUFS;
645 }
646 splhigh(); /* XXX IMP ME HARDER */
647 sc->sc_flags &= ~SC_COMP_RUN;
648 splx(s);
649 } else {
650 s = splsoftnet();
651 if (sc->sc_rc_state != NULL) {
652 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
653 ppp_compressor_rele(sc->sc_rcomp);
654 }
655 sc->sc_rcomp = cp;
656 sc->sc_rc_state = cp->decomp_alloc(ccp_option, nb);
657 if (sc->sc_rc_state == NULL) {
658 if (sc->sc_flags & SC_DEBUG)
659 printf("%s: decomp_alloc failed\n",
660 sc->sc_if.if_xname);
661 error = ENOBUFS;
662 }
663 splhigh(); /* XXX IMP ME HARDER */
664 sc->sc_flags &= ~SC_DECOMP_RUN;
665 splx(s);
666 }
667 return (error);
668 #endif /* PPP_COMPRESS */
669
670 case PPPIOCGNPMODE:
671 case PPPIOCSNPMODE:
672 npi = (struct npioctl *) data;
673 switch (npi->protocol) {
674 case PPP_IP:
675 npx = NP_IP;
676 break;
677 case PPP_IPV6:
678 npx = NP_IPV6;
679 break;
680 default:
681 return EINVAL;
682 }
683 if (cmd == PPPIOCGNPMODE) {
684 npi->mode = sc->sc_npmode[npx];
685 } else {
686 if (npi->mode != sc->sc_npmode[npx]) {
687 s = splnet();
688 sc->sc_npmode[npx] = npi->mode;
689 if (npi->mode != NPMODE_QUEUE) {
690 ppp_requeue(sc);
691 ppp_restart(sc);
692 }
693 splx(s);
694 }
695 }
696 break;
697
698 case PPPIOCGIDLE:
699 s = splsoftnet();
700 t = time_second;
701 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
702 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
703 splx(s);
704 break;
705
706 #ifdef PPP_FILTER
707 case PPPIOCSPASS:
708 case PPPIOCSACTIVE:
709 /* These are no longer supported. */
710 return EOPNOTSUPP;
711
712 case PPPIOCSIPASS:
713 case PPPIOCSOPASS:
714 case PPPIOCSIACTIVE:
715 case PPPIOCSOACTIVE:
716 nbp = (struct bpf_program *) data;
717 if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
718 return EINVAL;
719 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
720 if (newcodelen != 0) {
721 newcode = malloc(newcodelen, M_DEVBUF, M_WAITOK);
722 /* WAITOK -- malloc() never fails. */
723 if ((error = copyin((void *)nbp->bf_insns,
724 (void *)newcode, newcodelen)) != 0) {
725 free(newcode, M_DEVBUF);
726 return error;
727 }
728 if (!bpf_validate(newcode, nbp->bf_len)) {
729 free(newcode, M_DEVBUF);
730 return EINVAL;
731 }
732 } else
733 newcode = 0;
734 switch (cmd) {
735 case PPPIOCSIPASS:
736 bp = &sc->sc_pass_filt_in;
737 break;
738
739 case PPPIOCSOPASS:
740 bp = &sc->sc_pass_filt_out;
741 break;
742
743 case PPPIOCSIACTIVE:
744 bp = &sc->sc_active_filt_in;
745 break;
746
747 case PPPIOCSOACTIVE:
748 bp = &sc->sc_active_filt_out;
749 break;
750 default:
751 free(newcode, M_DEVBUF);
752 return (EPASSTHROUGH);
753 }
754 oldcode = bp->bf_insns;
755 s = splnet();
756 bp->bf_len = nbp->bf_len;
757 bp->bf_insns = newcode;
758 splx(s);
759 if (oldcode != 0)
760 free(oldcode, M_DEVBUF);
761 break;
762 #endif /* PPP_FILTER */
763
764 default:
765 return (EPASSTHROUGH);
766 }
767 return (0);
768 }
769
770 /*
771 * Process an ioctl request to the ppp network interface.
772 */
773 static int
774 pppsioctl(struct ifnet *ifp, u_long cmd, void *data)
775 {
776 struct ppp_softc *sc = ifp->if_softc;
777 struct ifaddr *ifa = (struct ifaddr *)data;
778 struct ifreq *ifr = (struct ifreq *)data;
779 struct ppp_stats *psp;
780 #ifdef PPP_COMPRESS
781 struct ppp_comp_stats *pcp;
782 #endif
783 int s = splnet(), error = 0;
784
785 switch (cmd) {
786 case SIOCSIFFLAGS:
787 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
788 break;
789 if ((ifp->if_flags & IFF_RUNNING) == 0)
790 ifp->if_flags &= ~IFF_UP;
791 break;
792
793 case SIOCINITIFADDR:
794 switch (ifa->ifa_addr->sa_family) {
795 #ifdef INET
796 case AF_INET:
797 break;
798 #endif
799 #ifdef INET6
800 case AF_INET6:
801 break;
802 #endif
803 default:
804 printf("%s: af%d not supported\n", ifp->if_xname,
805 ifa->ifa_addr->sa_family);
806 error = EAFNOSUPPORT;
807 break;
808 }
809 ifa->ifa_rtrequest = p2p_rtrequest;
810 break;
811
812 case SIOCADDMULTI:
813 case SIOCDELMULTI:
814 if (ifr == NULL) {
815 error = EAFNOSUPPORT;
816 break;
817 }
818 switch (ifreq_getaddr(cmd, ifr)->sa_family) {
819 #ifdef INET
820 case AF_INET:
821 break;
822 #endif
823 #ifdef INET6
824 case AF_INET6:
825 break;
826 #endif
827 default:
828 error = EAFNOSUPPORT;
829 break;
830 }
831 break;
832
833 case SIOCGPPPSTATS:
834 psp = &((struct ifpppstatsreq *) data)->stats;
835 memset(psp, 0, sizeof(*psp));
836 psp->p = sc->sc_stats;
837 #if defined(VJC) && !defined(SL_NO_STATS)
838 if (sc->sc_comp) {
839 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
840 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
841 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
842 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
843 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
844 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
845 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
846 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
847 }
848 #endif /* VJC */
849 break;
850
851 #ifdef PPP_COMPRESS
852 case SIOCGPPPCSTATS:
853 pcp = &((struct ifpppcstatsreq *) data)->stats;
854 memset(pcp, 0, sizeof(*pcp));
855 if (sc->sc_xc_state != NULL)
856 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
857 if (sc->sc_rc_state != NULL)
858 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
859 break;
860 #endif /* PPP_COMPRESS */
861
862 default:
863 if ((error = ifioctl_common(&sc->sc_if, cmd, data)) == ENETRESET)
864 error = 0;
865 break;
866 }
867 splx(s);
868 return (error);
869 }
870
871 /*
872 * Queue a packet. Start transmission if not active.
873 * Packet is placed in Information field of PPP frame.
874 */
875 int
876 pppoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
877 const struct rtentry *rtp)
878 {
879 struct ppp_softc *sc = ifp->if_softc;
880 int protocol, address, control;
881 u_char *cp;
882 int s, error;
883 #ifdef INET
884 struct ip *ip;
885 #endif
886 struct ifqueue *ifq;
887 enum NPmode mode;
888 int len;
889
890 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
891 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
892 error = ENETDOWN; /* sort of */
893 goto bad;
894 }
895
896 IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family);
897
898 /*
899 * Compute PPP header.
900 */
901 m0->m_flags &= ~M_HIGHPRI;
902 switch (dst->sa_family) {
903 #ifdef INET
904 case AF_INET:
905 address = PPP_ALLSTATIONS;
906 control = PPP_UI;
907 protocol = PPP_IP;
908 mode = sc->sc_npmode[NP_IP];
909
910 /*
911 * If this packet has the "low delay" bit set in the IP header,
912 * put it on the fastq instead.
913 */
914 ip = mtod(m0, struct ip *);
915 if (ip->ip_tos & IPTOS_LOWDELAY)
916 m0->m_flags |= M_HIGHPRI;
917 break;
918 #endif
919 #ifdef INET6
920 case AF_INET6:
921 address = PPP_ALLSTATIONS; /*XXX*/
922 control = PPP_UI; /*XXX*/
923 protocol = PPP_IPV6;
924 mode = sc->sc_npmode[NP_IPV6];
925
926 #if 0 /* XXX flowinfo/traffic class, maybe? */
927 /*
928 * If this packet has the "low delay" bit set in the IP header,
929 * put it on the fastq instead.
930 */
931 ip = mtod(m0, struct ip *);
932 if (ip->ip_tos & IPTOS_LOWDELAY)
933 m0->m_flags |= M_HIGHPRI;
934 #endif
935 break;
936 #endif
937 case AF_UNSPEC:
938 address = PPP_ADDRESS(dst->sa_data);
939 control = PPP_CONTROL(dst->sa_data);
940 protocol = PPP_PROTOCOL(dst->sa_data);
941 mode = NPMODE_PASS;
942 break;
943 default:
944 printf("%s: af%d not supported\n", ifp->if_xname,
945 dst->sa_family);
946 error = EAFNOSUPPORT;
947 goto bad;
948 }
949
950 /*
951 * Drop this packet, or return an error, if necessary.
952 */
953 if (mode == NPMODE_ERROR) {
954 error = ENETDOWN;
955 goto bad;
956 }
957 if (mode == NPMODE_DROP) {
958 error = 0;
959 goto bad;
960 }
961
962 /*
963 * Add PPP header.
964 */
965 M_PREPEND(m0, PPP_HDRLEN, M_DONTWAIT);
966 if (m0 == NULL) {
967 error = ENOBUFS;
968 goto bad;
969 }
970
971 cp = mtod(m0, u_char *);
972 *cp++ = address;
973 *cp++ = control;
974 *cp++ = protocol >> 8;
975 *cp++ = protocol & 0xff;
976
977 len = m_length(m0);
978
979 if (sc->sc_flags & SC_LOG_OUTPKT) {
980 printf("%s output: ", ifp->if_xname);
981 pppdumpm(m0);
982 }
983
984 if ((protocol & 0x8000) == 0) {
985 #ifdef PPP_FILTER
986 /*
987 * Apply the pass and active filters to the packet,
988 * but only if it is a data packet.
989 */
990 if (sc->sc_pass_filt_out.bf_insns != 0
991 && bpf_filter(sc->sc_pass_filt_out.bf_insns,
992 (u_char *)m0, len, 0) == 0) {
993 error = 0; /* drop this packet */
994 goto bad;
995 }
996
997 /*
998 * Update the time we sent the most recent packet.
999 */
1000 if (sc->sc_active_filt_out.bf_insns == 0
1001 || bpf_filter(sc->sc_active_filt_out.bf_insns,
1002 (u_char *)m0, len, 0))
1003 sc->sc_last_sent = time_second;
1004 #else
1005 /*
1006 * Update the time we sent the most recent packet.
1007 */
1008 sc->sc_last_sent = time_second;
1009 #endif /* PPP_FILTER */
1010 }
1011
1012 /*
1013 * See if bpf wants to look at the packet.
1014 */
1015 bpf_mtap(&sc->sc_if, m0, BPF_D_OUT);
1016
1017 /*
1018 * Put the packet on the appropriate queue.
1019 */
1020 s = splnet();
1021 if (mode == NPMODE_QUEUE) {
1022 /* XXX we should limit the number of packets on this queue */
1023 *sc->sc_npqtail = m0;
1024 m0->m_nextpkt = NULL;
1025 sc->sc_npqtail = &m0->m_nextpkt;
1026 } else {
1027 ifq = (m0->m_flags & M_HIGHPRI) ? &sc->sc_fastq : NULL;
1028 if ((error = ifq_enqueue2(&sc->sc_if, ifq, m0)) != 0) {
1029 splx(s);
1030 if_statinc(&sc->sc_if, if_oerrors);
1031 sc->sc_stats.ppp_oerrors++;
1032 return (error);
1033 }
1034 ppp_restart(sc);
1035 }
1036 if_statadd2(ifp, if_opackets, 1, 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 if_statinc(&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 if_statinc(ifp, if_iqdrops);
1732 goto bad;
1733 }
1734 if_statadd2(ifp, if_ipackets, 1, if_ibytes, ilen);
1735 splx(s);
1736 return;
1737 }
1738
1739 /* ifq: other protocol cases */
1740 if (!inq) {
1741 splx(s);
1742 goto bad;
1743 }
1744 if (IF_QFULL(inq)) {
1745 IF_DROP(inq);
1746 splx(s);
1747 if (sc->sc_flags & SC_DEBUG)
1748 printf("%s: input queue full\n", ifp->if_xname);
1749 if_statinc(ifp, if_iqdrops);
1750 goto bad;
1751 }
1752 IF_ENQUEUE(inq, m);
1753 splx(s);
1754 if_statadd2(ifp, if_ipackets, 1, if_ibytes, ilen);
1755
1756 (*sc->sc_ctlp)(sc);
1757
1758 return;
1759
1760 bad:
1761 m_freem(m);
1762 if_statinc(&sc->sc_if, if_ierrors);
1763 sc->sc_stats.ppp_ierrors++;
1764 }
1765
1766 #define MAX_DUMP_BYTES 128
1767
1768 static void
1769 pppdumpm(struct mbuf *m0)
1770 {
1771 char buf[3*MAX_DUMP_BYTES+4];
1772 char *bp = buf;
1773 struct mbuf *m;
1774
1775 for (m = m0; m; m = m->m_next) {
1776 int l = m->m_len;
1777 u_char *rptr = (u_char *)m->m_data;
1778
1779 while (l--) {
1780 if (bp > buf + sizeof(buf) - 4)
1781 goto done;
1782 /* Convert byte to ascii hex */
1783 *bp++ = hexdigits[*rptr >> 4];
1784 *bp++ = hexdigits[*rptr++ & 0xf];
1785 }
1786
1787 if (m->m_next) {
1788 if (bp > buf + sizeof(buf) - 3)
1789 goto done;
1790 *bp++ = '|';
1791 } else
1792 *bp++ = ' ';
1793 }
1794 done:
1795 if (m)
1796 *bp++ = '>';
1797 *bp = 0;
1798 printf("%s\n", buf);
1799 }
1800
1801 #ifdef ALTQ
1802 /*
1803 * A wrapper to transmit a packet from if_start since ALTQ uses
1804 * if_start to send a packet.
1805 */
1806 static void
1807 ppp_ifstart(struct ifnet *ifp)
1808 {
1809 struct ppp_softc *sc;
1810
1811 sc = ifp->if_softc;
1812 (*sc->sc_start)(sc);
1813 }
1814 #endif
1815
1816 static const struct ppp_known_compressor {
1817 uint8_t code;
1818 const char *module;
1819 } ppp_known_compressors[] = {
1820 { CI_DEFLATE, "ppp_deflate" },
1821 { CI_DEFLATE_DRAFT, "ppp_deflate" },
1822 { CI_BSD_COMPRESS, "ppp_bsdcomp" },
1823 { CI_MPPE, "ppp_mppe" },
1824 { 0, NULL }
1825 };
1826
1827 static int
1828 ppp_compressor_init(void)
1829 {
1830
1831 mutex_init(&ppp_compressors_mtx, MUTEX_DEFAULT, IPL_NONE);
1832 return 0;
1833 }
1834
1835 static int
1836 ppp_compressor_destroy(void)
1837 {
1838
1839 mutex_destroy(&ppp_compressors_mtx);
1840 return 0;
1841 }
1842
1843 static void
1844 ppp_compressor_rele(struct compressor *cp)
1845 {
1846
1847 mutex_enter(&ppp_compressors_mtx);
1848 --cp->comp_refcnt;
1849 mutex_exit(&ppp_compressors_mtx);
1850 }
1851
1852 static struct compressor *
1853 ppp_get_compressor_noload(uint8_t ci, bool hold)
1854 {
1855 struct compressor *cp;
1856
1857 KASSERT(mutex_owned(&ppp_compressors_mtx));
1858 LIST_FOREACH(cp, &ppp_compressors, comp_list) {
1859 if (cp->compress_proto == ci) {
1860 if (hold)
1861 ++cp->comp_refcnt;
1862 return cp;
1863 }
1864 }
1865
1866 return NULL;
1867 }
1868
1869 static struct compressor *
1870 ppp_get_compressor(uint8_t ci)
1871 {
1872 struct compressor *cp = NULL;
1873 const struct ppp_known_compressor *pkc;
1874
1875 mutex_enter(&ppp_compressors_mtx);
1876 cp = ppp_get_compressor_noload(ci, true);
1877 mutex_exit(&ppp_compressors_mtx);
1878 if (cp != NULL)
1879 return cp;
1880
1881 kernconfig_lock();
1882 mutex_enter(&ppp_compressors_mtx);
1883 cp = ppp_get_compressor_noload(ci, true);
1884 mutex_exit(&ppp_compressors_mtx);
1885 if (cp == NULL) {
1886 /* Not found, so try to autoload a module */
1887 for (pkc = ppp_known_compressors; pkc->module != NULL; pkc++) {
1888 if (pkc->code == ci) {
1889 if (module_autoload(pkc->module,
1890 MODULE_CLASS_MISC) != 0)
1891 break;
1892 mutex_enter(&ppp_compressors_mtx);
1893 cp = ppp_get_compressor_noload(ci, true);
1894 mutex_exit(&ppp_compressors_mtx);
1895 break;
1896 }
1897 }
1898 }
1899 kernconfig_unlock();
1900
1901 return cp;
1902 }
1903
1904 int
1905 ppp_register_compressor(struct compressor *pc, size_t ncomp)
1906 {
1907 int error = 0;
1908 size_t i;
1909
1910 mutex_enter(&ppp_compressors_mtx);
1911 for (i = 0; i < ncomp; i++) {
1912 if (ppp_get_compressor_noload(pc[i].compress_proto,
1913 false) != NULL)
1914 error = EEXIST;
1915 }
1916 if (!error) {
1917 for (i = 0; i < ncomp; i++) {
1918 pc[i].comp_refcnt = 0;
1919 LIST_INSERT_HEAD(&ppp_compressors, &pc[i], comp_list);
1920 }
1921 }
1922 mutex_exit(&ppp_compressors_mtx);
1923
1924 return error;
1925 }
1926
1927 int
1928 ppp_unregister_compressor(struct compressor *pc, size_t ncomp)
1929 {
1930 int error = 0;
1931 size_t i;
1932
1933 mutex_enter(&ppp_compressors_mtx);
1934 for (i = 0; i < ncomp; i++) {
1935 if (ppp_get_compressor_noload(pc[i].compress_proto,
1936 false) != &pc[i])
1937 error = ENOENT;
1938 else if (pc[i].comp_refcnt != 0)
1939 error = EBUSY;
1940 }
1941 if (!error) {
1942 for (i = 0; i < ncomp; i++) {
1943 LIST_REMOVE(&pc[i], comp_list);
1944 }
1945 }
1946 mutex_exit(&ppp_compressors_mtx);
1947
1948 return error;
1949 }
1950
1951 /*
1952 * Module infrastructure
1953 */
1954 #include "if_module.h"
1955
1956 #ifdef PPP_FILTER
1957 #define PPP_DEP "bpf_filter,"
1958 #else
1959 #define PPP_DEP
1960 #endif
1961
1962 IF_MODULE(MODULE_CLASS_DRIVER, ppp, PPP_DEP "slcompress")
1963