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