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