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