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