if_ppp.c revision 1.58.4.1 1 /* $NetBSD: if_ppp.c,v 1.58.4.1 2001/08/16 16:52:39 tv 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) 1989 Carnegie Mellon University.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms are permitted
11 * provided that the above copyright notice and this paragraph are
12 * duplicated in all such forms and that any documentation,
13 * advertising materials, and other materials related to such
14 * distribution and use acknowledge that the software was developed
15 * by Carnegie Mellon University. The name of the
16 * University may not be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 *
22 * Drew D. Perkins
23 * Carnegie Mellon University
24 * 4910 Forbes Ave.
25 * Pittsburgh, PA 15213
26 * (412) 268-8576
27 * ddp (at) andrew.cmu.edu
28 *
29 * Based on:
30 * @(#)if_sl.c 7.6.1.2 (Berkeley) 2/15/89
31 *
32 * Copyright (c) 1987 Regents of the University of California.
33 * All rights reserved.
34 *
35 * Redistribution and use in source and binary forms are permitted
36 * provided that the above copyright notice and this paragraph are
37 * duplicated in all such forms and that any documentation,
38 * advertising materials, and other materials related to such
39 * distribution and use acknowledge that the software was developed
40 * by the University of California, Berkeley. The name of the
41 * University may not be used to endorse or promote products derived
42 * from this software without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
44 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
45 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46 *
47 * Serial Line interface
48 *
49 * Rick Adams
50 * Center for Seismic Studies
51 * 1300 N 17th Street, Suite 1450
52 * Arlington, Virginia 22209
53 * (703)276-7900
54 * rick (at) seismo.ARPA
55 * seismo!rick
56 *
57 * Pounded on heavily by Chris Torek (chris (at) mimsy.umd.edu, umcp-cs!chris).
58 * Converted to 4.3BSD Beta by Chris Torek.
59 * Other changes made at Berkeley, based in part on code by Kirk Smith.
60 *
61 * Converted to 4.3BSD+ 386BSD by Brad Parker (brad (at) cayman.com)
62 * Added VJ tcp header compression; more unified ioctls
63 *
64 * Extensively modified by Paul Mackerras (paulus (at) cs.anu.edu.au).
65 * Cleaned up a lot of the mbuf-related code to fix bugs that
66 * caused system crashes and packet corruption. Changed pppstart
67 * so that it doesn't just give up with a collision if the whole
68 * packet doesn't fit in the output ring buffer.
69 *
70 * Added priority queueing for interactive IP packets, following
71 * the model of if_sl.c, plus hooks for bpf.
72 * Paul Mackerras (paulus (at) cs.anu.edu.au).
73 */
74
75 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
76 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
77
78 #include "ppp.h"
79 #if NPPP > 0
80
81 #define VJC
82 #define PPP_COMPRESS
83
84 #include "opt_inet.h"
85 #include "opt_gateway.h"
86 #include "opt_ppp.h"
87
88 #include <sys/param.h>
89 #include <sys/proc.h>
90 #include <sys/mbuf.h>
91 #include <sys/socket.h>
92 #include <sys/ioctl.h>
93 #include <sys/kernel.h>
94 #include <sys/systm.h>
95 #include <sys/time.h>
96 #include <sys/malloc.h>
97
98 #include <net/if.h>
99 #include <net/if_types.h>
100 #include <net/netisr.h>
101 #include <net/route.h>
102 #ifdef PPP_FILTER
103 #include <net/bpf.h>
104 #endif
105
106 #ifdef INET
107 #include <netinet/in.h>
108 #include <netinet/in_systm.h>
109 #include <netinet/in_var.h>
110 #include <netinet/ip.h>
111 #else
112 #ifdef _KERNEL
113 #ifdef VJC
114 #error ppp device with VJC assumes INET
115 #endif
116 #endif
117 #endif
118
119 #include "bpfilter.h"
120 #if NBPFILTER > 0
121 #include <sys/time.h>
122 #include <net/bpf.h>
123 #endif
124
125 #if defined(PPP_FILTER) || NBPFILTER > 0
126 #include <net/slip.h>
127 #endif
128
129 #ifdef VJC
130 #include <net/slcompress.h>
131 #endif
132
133 #include <net/ppp_defs.h>
134 #include <net/if_ppp.h>
135 #include <net/if_pppvar.h>
136 #include <machine/cpu.h>
137
138 #ifdef PPP_COMPRESS
139 #define PACKETPTR struct mbuf *
140 #include <net/ppp-comp.h>
141 #endif
142
143 static int pppsioctl __P((struct ifnet *, u_long, caddr_t));
144 static void ppp_requeue __P((struct ppp_softc *));
145 static void ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
146 static void ppp_ccp_closed __P((struct ppp_softc *));
147 static void ppp_inproc __P((struct ppp_softc *, struct mbuf *));
148 static void pppdumpm __P((struct mbuf *m0));
149
150 /*
151 * Some useful mbuf macros not in mbuf.h.
152 */
153 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
154
155 #define M_DATASTART(m) \
156 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
157 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
158
159 #define M_DATASIZE(m) \
160 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
161 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
162
163 /*
164 * We define two link layer specific mbuf flags, to mark high-priority
165 * packets for output, and received packets following lost/corrupted
166 * packets.
167 */
168 #define M_HIGHPRI M_LINK0 /* output packet for sc_fastq */
169 #define M_ERRMARK M_LINK1 /* rx packet following lost/corrupted pkt */
170
171 #ifdef PPP_COMPRESS
172 /*
173 * List of compressors we know about.
174 * We leave some space so maybe we can modload compressors.
175 */
176
177 extern struct compressor ppp_bsd_compress;
178 extern struct compressor ppp_deflate, ppp_deflate_draft;
179
180 struct compressor *ppp_compressors[8] = {
181 #if DO_BSD_COMPRESS && defined(PPP_BSDCOMP)
182 &ppp_bsd_compress,
183 #endif
184 #if DO_DEFLATE && defined(PPP_DEFLATE)
185 &ppp_deflate,
186 &ppp_deflate_draft,
187 #endif
188 NULL
189 };
190 #endif /* PPP_COMPRESS */
191
192
193 /*
194 * Called from boot code to establish ppp interfaces.
195 */
196 void
197 pppattach()
198 {
199 struct ppp_softc *sc;
200 int i = 0;
201
202 for (sc = ppp_softc; i < NPPP; sc++) {
203 sc->sc_unit = i; /* XXX */
204 sprintf(sc->sc_if.if_xname, "ppp%d", i++);
205 callout_init(&sc->sc_timo_ch);
206 sc->sc_if.if_softc = sc;
207 sc->sc_if.if_mtu = PPP_MTU;
208 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
209 sc->sc_if.if_type = IFT_PPP;
210 sc->sc_if.if_hdrlen = PPP_HDRLEN;
211 sc->sc_if.if_ioctl = pppsioctl;
212 sc->sc_if.if_output = pppoutput;
213 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
214 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
215 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
216 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
217 if_attach(&sc->sc_if);
218 #if NBPFILTER > 0
219 bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_NULL, 0);
220 #endif
221 }
222 }
223
224 /*
225 * Allocate a ppp interface unit and initialize it.
226 */
227 struct ppp_softc *
228 pppalloc(pid)
229 pid_t pid;
230 {
231 int nppp, i;
232 struct ppp_softc *sc;
233
234 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
235 if (sc->sc_xfer == pid) {
236 sc->sc_xfer = 0;
237 return sc;
238 }
239 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
240 if (sc->sc_devp == NULL)
241 break;
242 if (nppp >= NPPP)
243 return NULL;
244
245 sc->sc_flags = 0;
246 sc->sc_mru = PPP_MRU;
247 sc->sc_relinq = NULL;
248 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
249 #ifdef VJC
250 MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
251 M_DEVBUF, M_NOWAIT);
252 if (sc->sc_comp)
253 sl_compress_init(sc->sc_comp);
254 #endif
255 #ifdef PPP_COMPRESS
256 sc->sc_xc_state = NULL;
257 sc->sc_rc_state = NULL;
258 #endif /* PPP_COMPRESS */
259 for (i = 0; i < NUM_NP; ++i)
260 sc->sc_npmode[i] = NPMODE_ERROR;
261 sc->sc_npqueue = NULL;
262 sc->sc_npqtail = &sc->sc_npqueue;
263 sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
264
265 return sc;
266 }
267
268 /*
269 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
270 */
271 void
272 pppdealloc(sc)
273 struct ppp_softc *sc;
274 {
275 struct mbuf *m;
276
277 if_down(&sc->sc_if);
278 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
279 sc->sc_devp = NULL;
280 sc->sc_xfer = 0;
281 for (;;) {
282 IF_DEQUEUE(&sc->sc_rawq, m);
283 if (m == NULL)
284 break;
285 m_freem(m);
286 }
287 for (;;) {
288 IF_DEQUEUE(&sc->sc_inq, m);
289 if (m == NULL)
290 break;
291 m_freem(m);
292 }
293 for (;;) {
294 IF_DEQUEUE(&sc->sc_fastq, m);
295 if (m == NULL)
296 break;
297 m_freem(m);
298 }
299 while ((m = sc->sc_npqueue) != NULL) {
300 sc->sc_npqueue = m->m_nextpkt;
301 m_freem(m);
302 }
303 if (sc->sc_togo != NULL) {
304 m_freem(sc->sc_togo);
305 sc->sc_togo = NULL;
306 }
307 #ifdef PPP_COMPRESS
308 ppp_ccp_closed(sc);
309 sc->sc_xc_state = NULL;
310 sc->sc_rc_state = NULL;
311 #endif /* PPP_COMPRESS */
312 #ifdef PPP_FILTER
313 if (sc->sc_pass_filt_in.bf_insns != 0) {
314 FREE(sc->sc_pass_filt_in.bf_insns, M_DEVBUF);
315 sc->sc_pass_filt_in.bf_insns = 0;
316 sc->sc_pass_filt_in.bf_len = 0;
317 }
318 if (sc->sc_pass_filt_out.bf_insns != 0) {
319 FREE(sc->sc_pass_filt_out.bf_insns, M_DEVBUF);
320 sc->sc_pass_filt_out.bf_insns = 0;
321 sc->sc_pass_filt_out.bf_len = 0;
322 }
323 if (sc->sc_active_filt_in.bf_insns != 0) {
324 FREE(sc->sc_active_filt_in.bf_insns, M_DEVBUF);
325 sc->sc_active_filt_in.bf_insns = 0;
326 sc->sc_active_filt_in.bf_len = 0;
327 }
328 if (sc->sc_active_filt_out.bf_insns != 0) {
329 FREE(sc->sc_active_filt_out.bf_insns, M_DEVBUF);
330 sc->sc_active_filt_out.bf_insns = 0;
331 sc->sc_active_filt_out.bf_len = 0;
332 }
333 #endif /* PPP_FILTER */
334 #ifdef VJC
335 if (sc->sc_comp != 0) {
336 FREE(sc->sc_comp, M_DEVBUF);
337 sc->sc_comp = 0;
338 }
339 #endif
340 }
341
342 /*
343 * Ioctl routine for generic ppp devices.
344 */
345 int
346 pppioctl(sc, cmd, data, flag, p)
347 struct ppp_softc *sc;
348 u_long cmd;
349 caddr_t data;
350 int flag;
351 struct proc *p;
352 {
353 int s, error, flags, mru, npx;
354 u_int nb;
355 struct ppp_option_data *odp;
356 struct compressor **cp;
357 struct npioctl *npi;
358 time_t t;
359 #ifdef PPP_FILTER
360 struct bpf_program *bp, *nbp;
361 struct bpf_insn *newcode, *oldcode;
362 int newcodelen;
363 #endif /* PPP_FILTER */
364 #ifdef PPP_COMPRESS
365 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
366 #endif
367
368 switch (cmd) {
369 case FIONREAD:
370 *(int *)data = sc->sc_inq.ifq_len;
371 break;
372
373 case PPPIOCGUNIT:
374 *(int *)data = sc->sc_unit; /* XXX */
375 break;
376
377 case PPPIOCGFLAGS:
378 *(u_int *)data = sc->sc_flags;
379 break;
380
381 case PPPIOCSFLAGS:
382 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
383 return (error);
384 flags = *(int *)data & SC_MASK;
385 s = splsoftnet();
386 #ifdef PPP_COMPRESS
387 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
388 ppp_ccp_closed(sc);
389 #endif
390 splimp();
391 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
392 splx(s);
393 break;
394
395 case PPPIOCSMRU:
396 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
397 return (error);
398 mru = *(int *)data;
399 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
400 sc->sc_mru = mru;
401 break;
402
403 case PPPIOCGMRU:
404 *(int *)data = sc->sc_mru;
405 break;
406
407 #ifdef VJC
408 case PPPIOCSMAXCID:
409 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
410 return (error);
411 if (sc->sc_comp) {
412 s = splsoftnet();
413 sl_compress_setup(sc->sc_comp, *(int *)data);
414 splx(s);
415 }
416 break;
417 #endif
418
419 case PPPIOCXFERUNIT:
420 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
421 return (error);
422 sc->sc_xfer = p->p_pid;
423 break;
424
425 #ifdef PPP_COMPRESS
426 case PPPIOCSCOMPRESS:
427 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
428 return (error);
429 odp = (struct ppp_option_data *) data;
430 nb = odp->length;
431 if (nb > sizeof(ccp_option))
432 nb = sizeof(ccp_option);
433 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
434 return (error);
435 if (ccp_option[1] < 2) /* preliminary check on the length byte */
436 return (EINVAL);
437 for (cp = ppp_compressors; *cp != NULL; ++cp)
438 if ((*cp)->compress_proto == ccp_option[0]) {
439 /*
440 * Found a handler for the protocol - try to allocate
441 * a compressor or decompressor.
442 */
443 error = 0;
444 if (odp->transmit) {
445 s = splsoftnet();
446 if (sc->sc_xc_state != NULL)
447 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
448 sc->sc_xcomp = *cp;
449 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
450 if (sc->sc_xc_state == NULL) {
451 if (sc->sc_flags & SC_DEBUG)
452 printf("%s: comp_alloc failed\n",
453 sc->sc_if.if_xname);
454 error = ENOBUFS;
455 }
456 splimp();
457 sc->sc_flags &= ~SC_COMP_RUN;
458 splx(s);
459 } else {
460 s = splsoftnet();
461 if (sc->sc_rc_state != NULL)
462 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
463 sc->sc_rcomp = *cp;
464 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
465 if (sc->sc_rc_state == NULL) {
466 if (sc->sc_flags & SC_DEBUG)
467 printf("%s: decomp_alloc failed\n",
468 sc->sc_if.if_xname);
469 error = ENOBUFS;
470 }
471 splimp();
472 sc->sc_flags &= ~SC_DECOMP_RUN;
473 splx(s);
474 }
475 return (error);
476 }
477 if (sc->sc_flags & SC_DEBUG)
478 printf("%s: no compressor for [%x %x %x], %x\n",
479 sc->sc_if.if_xname, ccp_option[0], ccp_option[1],
480 ccp_option[2], nb);
481 return (EINVAL); /* no handler found */
482 #endif /* PPP_COMPRESS */
483
484 case PPPIOCGNPMODE:
485 case PPPIOCSNPMODE:
486 npi = (struct npioctl *) data;
487 switch (npi->protocol) {
488 case PPP_IP:
489 npx = NP_IP;
490 break;
491 case PPP_IPV6:
492 npx = NP_IPV6;
493 break;
494 default:
495 return EINVAL;
496 }
497 if (cmd == PPPIOCGNPMODE) {
498 npi->mode = sc->sc_npmode[npx];
499 } else {
500 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
501 return (error);
502 if (npi->mode != sc->sc_npmode[npx]) {
503 s = splimp();
504 sc->sc_npmode[npx] = npi->mode;
505 if (npi->mode != NPMODE_QUEUE) {
506 ppp_requeue(sc);
507 ppp_restart(sc);
508 }
509 splx(s);
510 }
511 }
512 break;
513
514 case PPPIOCGIDLE:
515 s = splsoftnet();
516 t = time.tv_sec;
517 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
518 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
519 splx(s);
520 break;
521
522 #ifdef PPP_FILTER
523 case PPPIOCSPASS:
524 case PPPIOCSACTIVE:
525 /* These are no longer supported. */
526 return EOPNOTSUPP;
527
528 case PPPIOCSIPASS:
529 case PPPIOCSOPASS:
530 case PPPIOCSIACTIVE:
531 case PPPIOCSOACTIVE:
532 nbp = (struct bpf_program *) data;
533 if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
534 return EINVAL;
535 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
536 if (newcodelen != 0) {
537 newcode = malloc(newcodelen, M_DEVBUF, M_WAITOK);
538 /* WAITOK -- malloc() never fails. */
539 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
540 newcodelen)) != 0) {
541 free(newcode, M_DEVBUF);
542 return error;
543 }
544 if (!bpf_validate(newcode, nbp->bf_len)) {
545 free(newcode, M_DEVBUF);
546 return EINVAL;
547 }
548 } else
549 newcode = 0;
550 switch (cmd) {
551 case PPPIOCSIPASS:
552 bp = &sc->sc_pass_filt_in;
553 break;
554
555 case PPPIOCSOPASS:
556 bp = &sc->sc_pass_filt_out;
557 break;
558
559 case PPPIOCSIACTIVE:
560 bp = &sc->sc_active_filt_in;
561 break;
562
563 case PPPIOCSOACTIVE:
564 bp = &sc->sc_active_filt_out;
565 break;
566 }
567 oldcode = bp->bf_insns;
568 s = splimp();
569 bp->bf_len = nbp->bf_len;
570 bp->bf_insns = newcode;
571 splx(s);
572 if (oldcode != 0)
573 free(oldcode, M_DEVBUF);
574 break;
575 #endif /* PPP_FILTER */
576
577 default:
578 return (-1);
579 }
580 return (0);
581 }
582
583 /*
584 * Process an ioctl request to the ppp network interface.
585 */
586 static int
587 pppsioctl(ifp, cmd, data)
588 struct ifnet *ifp;
589 u_long cmd;
590 caddr_t data;
591 {
592 struct proc *p = curproc; /* XXX */
593 struct ppp_softc *sc = ifp->if_softc;
594 struct ifaddr *ifa = (struct ifaddr *)data;
595 struct ifreq *ifr = (struct ifreq *)data;
596 struct ppp_stats *psp;
597 #ifdef PPP_COMPRESS
598 struct ppp_comp_stats *pcp;
599 #endif
600 int s = splimp(), error = 0;
601
602 switch (cmd) {
603 case SIOCSIFFLAGS:
604 if ((ifp->if_flags & IFF_RUNNING) == 0)
605 ifp->if_flags &= ~IFF_UP;
606 break;
607
608 case SIOCSIFADDR:
609 switch (ifa->ifa_addr->sa_family) {
610 #ifdef INET
611 case AF_INET:
612 break;
613 #endif
614 #ifdef INET6
615 case AF_INET6:
616 break;
617 #endif
618 default:
619 error = EAFNOSUPPORT;
620 break;
621 }
622 break;
623
624 case SIOCSIFDSTADDR:
625 switch (ifa->ifa_addr->sa_family) {
626 #ifdef INET
627 case AF_INET:
628 break;
629 #endif
630 #ifdef INET6
631 case AF_INET6:
632 break;
633 #endif
634 default:
635 error = EAFNOSUPPORT;
636 break;
637 }
638 break;
639
640 case SIOCSIFMTU:
641 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
642 break;
643 sc->sc_if.if_mtu = ifr->ifr_mtu;
644 break;
645
646 case SIOCGIFMTU:
647 ifr->ifr_mtu = sc->sc_if.if_mtu;
648 break;
649
650 case SIOCADDMULTI:
651 case SIOCDELMULTI:
652 if (ifr == 0) {
653 error = EAFNOSUPPORT;
654 break;
655 }
656 switch(ifr->ifr_addr.sa_family) {
657 #ifdef INET
658 case AF_INET:
659 break;
660 #endif
661 #ifdef INET6
662 case AF_INET6:
663 break;
664 #endif
665 default:
666 error = EAFNOSUPPORT;
667 break;
668 }
669 break;
670
671 case SIOCGPPPSTATS:
672 psp = &((struct ifpppstatsreq *) data)->stats;
673 bzero(psp, sizeof(*psp));
674 psp->p = sc->sc_stats;
675 #if defined(VJC) && !defined(SL_NO_STATS)
676 if (sc->sc_comp) {
677 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
678 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
679 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
680 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
681 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
682 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
683 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
684 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
685 }
686 #endif /* VJC */
687 break;
688
689 #ifdef PPP_COMPRESS
690 case SIOCGPPPCSTATS:
691 pcp = &((struct ifpppcstatsreq *) data)->stats;
692 bzero(pcp, sizeof(*pcp));
693 if (sc->sc_xc_state != NULL)
694 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
695 if (sc->sc_rc_state != NULL)
696 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
697 break;
698 #endif /* PPP_COMPRESS */
699
700 default:
701 error = EINVAL;
702 }
703 splx(s);
704 return (error);
705 }
706
707 /*
708 * Queue a packet. Start transmission if not active.
709 * Packet is placed in Information field of PPP frame.
710 */
711 int
712 pppoutput(ifp, m0, dst, rtp)
713 struct ifnet *ifp;
714 struct mbuf *m0;
715 struct sockaddr *dst;
716 struct rtentry *rtp;
717 {
718 struct ppp_softc *sc = ifp->if_softc;
719 int protocol, address, control;
720 u_char *cp;
721 int s, error;
722 struct ip *ip;
723 struct ifqueue *ifq;
724 enum NPmode mode;
725 int len;
726 struct mbuf *m;
727
728 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
729 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
730 error = ENETDOWN; /* sort of */
731 goto bad;
732 }
733
734 /*
735 * Compute PPP header.
736 */
737 m0->m_flags &= ~M_HIGHPRI;
738 switch (dst->sa_family) {
739 #ifdef INET
740 case AF_INET:
741 address = PPP_ALLSTATIONS;
742 control = PPP_UI;
743 protocol = PPP_IP;
744 mode = sc->sc_npmode[NP_IP];
745
746 /*
747 * If this packet has the "low delay" bit set in the IP header,
748 * put it on the fastq instead.
749 */
750 ip = mtod(m0, struct ip *);
751 if (ip->ip_tos & IPTOS_LOWDELAY)
752 m0->m_flags |= M_HIGHPRI;
753 break;
754 #endif
755 #ifdef INET6
756 case AF_INET6:
757 address = PPP_ALLSTATIONS; /*XXX*/
758 control = PPP_UI; /*XXX*/
759 protocol = PPP_IPV6;
760 mode = sc->sc_npmode[NP_IPV6];
761
762 #if 0 /* XXX flowinfo/traffic class, maybe? */
763 /*
764 * If this packet has the "low delay" bit set in the IP header,
765 * put it on the fastq instead.
766 */
767 ip = mtod(m0, struct ip *);
768 if (ip->ip_tos & IPTOS_LOWDELAY)
769 m0->m_flags |= M_HIGHPRI;
770 #endif
771 break;
772 #endif
773 case AF_UNSPEC:
774 address = PPP_ADDRESS(dst->sa_data);
775 control = PPP_CONTROL(dst->sa_data);
776 protocol = PPP_PROTOCOL(dst->sa_data);
777 mode = NPMODE_PASS;
778 break;
779 default:
780 printf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
781 error = EAFNOSUPPORT;
782 goto bad;
783 }
784
785 /*
786 * Drop this packet, or return an error, if necessary.
787 */
788 if (mode == NPMODE_ERROR) {
789 error = ENETDOWN;
790 goto bad;
791 }
792 if (mode == NPMODE_DROP) {
793 error = 0;
794 goto bad;
795 }
796
797 /*
798 * Add PPP header. If no space in first mbuf, allocate another.
799 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
800 */
801 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
802 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
803 if (m0 == 0) {
804 error = ENOBUFS;
805 goto bad;
806 }
807 m0->m_len = 0;
808 } else
809 m0->m_data -= PPP_HDRLEN;
810
811 cp = mtod(m0, u_char *);
812 *cp++ = address;
813 *cp++ = control;
814 *cp++ = protocol >> 8;
815 *cp++ = protocol & 0xff;
816 m0->m_len += PPP_HDRLEN;
817
818 len = 0;
819 for (m = m0; m != 0; m = m->m_next)
820 len += m->m_len;
821
822 if (sc->sc_flags & SC_LOG_OUTPKT) {
823 printf("%s output: ", ifp->if_xname);
824 pppdumpm(m0);
825 }
826
827 if ((protocol & 0x8000) == 0) {
828 #ifdef PPP_FILTER
829 /*
830 * Apply the pass and active filters to the packet,
831 * but only if it is a data packet.
832 */
833 if (sc->sc_pass_filt_out.bf_insns != 0
834 && bpf_filter(sc->sc_pass_filt_out.bf_insns, (u_char *) m0,
835 len, 0) == 0) {
836 error = 0; /* drop this packet */
837 goto bad;
838 }
839
840 /*
841 * Update the time we sent the most recent packet.
842 */
843 if (sc->sc_active_filt_out.bf_insns == 0
844 || bpf_filter(sc->sc_active_filt_out.bf_insns, (u_char *) m0,
845 len, 0))
846 sc->sc_last_sent = time.tv_sec;
847 #else
848 /*
849 * Update the time we sent the most recent packet.
850 */
851 sc->sc_last_sent = time.tv_sec;
852 #endif /* PPP_FILTER */
853 }
854
855 #if NBPFILTER > 0
856 /*
857 * See if bpf wants to look at the packet.
858 */
859 if (sc->sc_bpf)
860 bpf_mtap(sc->sc_bpf, m0);
861 #endif
862
863 /*
864 * Put the packet on the appropriate queue.
865 */
866 s = splimp();
867 if (mode == NPMODE_QUEUE) {
868 /* XXX we should limit the number of packets on this queue */
869 *sc->sc_npqtail = m0;
870 m0->m_nextpkt = NULL;
871 sc->sc_npqtail = &m0->m_nextpkt;
872 } else {
873 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
874 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
875 IF_DROP(ifq);
876 splx(s);
877 sc->sc_if.if_oerrors++;
878 sc->sc_stats.ppp_oerrors++;
879 error = ENOBUFS;
880 goto bad;
881 }
882 IF_ENQUEUE(ifq, m0);
883 ppp_restart(sc);
884 }
885 ifp->if_lastchange = time;
886 ifp->if_opackets++;
887 ifp->if_obytes += len;
888
889 splx(s);
890 return (0);
891
892 bad:
893 m_freem(m0);
894 return (error);
895 }
896
897 /*
898 * After a change in the NPmode for some NP, move packets from the
899 * npqueue to the send queue or the fast queue as appropriate.
900 * Should be called at splimp, since we muck with the queues.
901 */
902 static void
903 ppp_requeue(sc)
904 struct ppp_softc *sc;
905 {
906 struct mbuf *m, **mpp;
907 struct ifqueue *ifq;
908 enum NPmode mode;
909
910 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
911 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
912 case PPP_IP:
913 mode = sc->sc_npmode[NP_IP];
914 break;
915 case PPP_IPV6:
916 mode = sc->sc_npmode[NP_IPV6];
917 break;
918 default:
919 mode = NPMODE_PASS;
920 }
921
922 switch (mode) {
923 case NPMODE_PASS:
924 /*
925 * This packet can now go on one of the queues to be sent.
926 */
927 *mpp = m->m_nextpkt;
928 m->m_nextpkt = NULL;
929 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
930 if (IF_QFULL(ifq)) {
931 IF_DROP(ifq);
932 sc->sc_if.if_oerrors++;
933 sc->sc_stats.ppp_oerrors++;
934 } else
935 IF_ENQUEUE(ifq, m);
936 break;
937
938 case NPMODE_DROP:
939 case NPMODE_ERROR:
940 *mpp = m->m_nextpkt;
941 m_freem(m);
942 break;
943
944 case NPMODE_QUEUE:
945 mpp = &m->m_nextpkt;
946 break;
947 }
948 }
949 sc->sc_npqtail = mpp;
950 }
951
952 /*
953 * Transmitter has finished outputting some stuff;
954 * remember to call sc->sc_start later at splsoftnet.
955 */
956 void
957 ppp_restart(sc)
958 struct ppp_softc *sc;
959 {
960 int s = splimp();
961
962 sc->sc_flags &= ~SC_TBUSY;
963 schednetisr(NETISR_PPP);
964 splx(s);
965 }
966
967 /*
968 * Get a packet to send. This procedure is intended to be called at
969 * splsoftnet, since it may involve time-consuming operations such as
970 * applying VJ compression, packet compression, address/control and/or
971 * protocol field compression to the packet.
972 */
973 struct mbuf *
974 ppp_dequeue(sc)
975 struct ppp_softc *sc;
976 {
977 struct mbuf *m, *mp;
978 u_char *cp;
979 int address, control, protocol;
980 int s;
981
982 /*
983 * Grab a packet to send: first try the fast queue, then the
984 * normal queue.
985 */
986 s = splimp();
987 IF_DEQUEUE(&sc->sc_fastq, m);
988 if (m == NULL)
989 IF_DEQUEUE(&sc->sc_if.if_snd, m);
990 splx(s);
991
992 if (m == NULL)
993 return NULL;
994
995 ++sc->sc_stats.ppp_opackets;
996
997 /*
998 * Extract the ppp header of the new packet.
999 * The ppp header will be in one mbuf.
1000 */
1001 cp = mtod(m, u_char *);
1002 address = PPP_ADDRESS(cp);
1003 control = PPP_CONTROL(cp);
1004 protocol = PPP_PROTOCOL(cp);
1005
1006 switch (protocol) {
1007 case PPP_IP:
1008 #ifdef VJC
1009 /*
1010 * If the packet is a TCP/IP packet, see if we can compress it.
1011 */
1012 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
1013 struct ip *ip;
1014 int type;
1015
1016 mp = m;
1017 ip = (struct ip *) (cp + PPP_HDRLEN);
1018 if (mp->m_len <= PPP_HDRLEN) {
1019 mp = mp->m_next;
1020 if (mp == NULL)
1021 break;
1022 ip = mtod(mp, struct ip *);
1023 }
1024 /* this code assumes the IP/TCP header is in one non-shared mbuf */
1025 if (ip->ip_p == IPPROTO_TCP) {
1026 type = sl_compress_tcp(mp, ip, sc->sc_comp,
1027 !(sc->sc_flags & SC_NO_TCP_CCID));
1028 switch (type) {
1029 case TYPE_UNCOMPRESSED_TCP:
1030 protocol = PPP_VJC_UNCOMP;
1031 break;
1032 case TYPE_COMPRESSED_TCP:
1033 protocol = PPP_VJC_COMP;
1034 cp = mtod(m, u_char *);
1035 cp[0] = address; /* header has moved */
1036 cp[1] = control;
1037 cp[2] = 0;
1038 break;
1039 }
1040 cp[3] = protocol; /* update protocol in PPP header */
1041 }
1042 }
1043 #endif /* VJC */
1044 break;
1045
1046 #ifdef PPP_COMPRESS
1047 case PPP_CCP:
1048 ppp_ccp(sc, m, 0);
1049 break;
1050 #endif /* PPP_COMPRESS */
1051 }
1052
1053 #ifdef PPP_COMPRESS
1054 if (protocol != PPP_LCP && protocol != PPP_CCP
1055 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1056 struct mbuf *mcomp = NULL;
1057 int slen, clen;
1058
1059 slen = 0;
1060 for (mp = m; mp != NULL; mp = mp->m_next)
1061 slen += mp->m_len;
1062 clen = (*sc->sc_xcomp->compress)
1063 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
1064 if (mcomp != NULL) {
1065 if (sc->sc_flags & SC_CCP_UP) {
1066 /* Send the compressed packet instead of the original. */
1067 m_freem(m);
1068 m = mcomp;
1069 cp = mtod(m, u_char *);
1070 protocol = cp[3];
1071 } else {
1072 /* Can't transmit compressed packets until CCP is up. */
1073 m_freem(mcomp);
1074 }
1075 }
1076 }
1077 #endif /* PPP_COMPRESS */
1078
1079 /*
1080 * Compress the address/control and protocol, if possible.
1081 */
1082 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1083 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1084 protocol != PPP_LCP) {
1085 /* can compress address/control */
1086 m->m_data += 2;
1087 m->m_len -= 2;
1088 }
1089 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1090 /* can compress protocol */
1091 if (mtod(m, u_char *) == cp) {
1092 cp[2] = cp[1]; /* move address/control up */
1093 cp[1] = cp[0];
1094 }
1095 ++m->m_data;
1096 --m->m_len;
1097 }
1098
1099 return m;
1100 }
1101
1102 /*
1103 * Software interrupt routine, called at splsoftnet.
1104 */
1105 void
1106 pppintr()
1107 {
1108 struct ppp_softc *sc;
1109 int i, s, s2;
1110 struct mbuf *m;
1111
1112 sc = ppp_softc;
1113 s = splsoftnet();
1114 for (i = 0; i < NPPP; ++i, ++sc) {
1115 if (!(sc->sc_flags & SC_TBUSY)
1116 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head
1117 || sc->sc_outm)) {
1118 s2 = splimp();
1119 sc->sc_flags |= SC_TBUSY;
1120 splx(s2);
1121 (*sc->sc_start)(sc);
1122 }
1123 for (;;) {
1124 s2 = splimp();
1125 IF_DEQUEUE(&sc->sc_rawq, m);
1126 splx(s2);
1127 if (m == NULL)
1128 break;
1129 ppp_inproc(sc, m);
1130 }
1131 }
1132 splx(s);
1133 }
1134
1135 #ifdef PPP_COMPRESS
1136 /*
1137 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1138 * 0 if it is about to be transmitted.
1139 */
1140 static void
1141 ppp_ccp(sc, m, rcvd)
1142 struct ppp_softc *sc;
1143 struct mbuf *m;
1144 int rcvd;
1145 {
1146 u_char *dp, *ep;
1147 struct mbuf *mp;
1148 int slen, s;
1149
1150 /*
1151 * Get a pointer to the data after the PPP header.
1152 */
1153 if (m->m_len <= PPP_HDRLEN) {
1154 mp = m->m_next;
1155 if (mp == NULL)
1156 return;
1157 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1158 } else {
1159 mp = m;
1160 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1161 }
1162
1163 ep = mtod(mp, u_char *) + mp->m_len;
1164 if (dp + CCP_HDRLEN > ep)
1165 return;
1166 slen = CCP_LENGTH(dp);
1167 if (dp + slen > ep) {
1168 if (sc->sc_flags & SC_DEBUG)
1169 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1170 dp, slen, mtod(mp, u_char *), mp->m_len);
1171 return;
1172 }
1173
1174 switch (CCP_CODE(dp)) {
1175 case CCP_CONFREQ:
1176 case CCP_TERMREQ:
1177 case CCP_TERMACK:
1178 /* CCP must be going down - disable compression */
1179 if (sc->sc_flags & SC_CCP_UP) {
1180 s = splimp();
1181 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1182 splx(s);
1183 }
1184 break;
1185
1186 case CCP_CONFACK:
1187 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1188 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1189 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1190 if (!rcvd) {
1191 /* we're agreeing to send compressed packets. */
1192 if (sc->sc_xc_state != NULL
1193 && (*sc->sc_xcomp->comp_init)
1194 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1195 sc->sc_unit, 0, sc->sc_flags & SC_DEBUG)) {
1196 s = splimp();
1197 sc->sc_flags |= SC_COMP_RUN;
1198 splx(s);
1199 }
1200 } else {
1201 /* peer is agreeing to send compressed packets. */
1202 if (sc->sc_rc_state != NULL
1203 && (*sc->sc_rcomp->decomp_init)
1204 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1205 sc->sc_unit, 0, sc->sc_mru,
1206 sc->sc_flags & SC_DEBUG)) {
1207 s = splimp();
1208 sc->sc_flags |= SC_DECOMP_RUN;
1209 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1210 splx(s);
1211 }
1212 }
1213 }
1214 break;
1215
1216 case CCP_RESETACK:
1217 if (sc->sc_flags & SC_CCP_UP) {
1218 if (!rcvd) {
1219 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1220 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1221 } else {
1222 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1223 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1224 s = splimp();
1225 sc->sc_flags &= ~SC_DC_ERROR;
1226 splx(s);
1227 }
1228 }
1229 }
1230 break;
1231 }
1232 }
1233
1234 /*
1235 * CCP is down; free (de)compressor state if necessary.
1236 */
1237 static void
1238 ppp_ccp_closed(sc)
1239 struct ppp_softc *sc;
1240 {
1241 if (sc->sc_xc_state) {
1242 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1243 sc->sc_xc_state = NULL;
1244 }
1245 if (sc->sc_rc_state) {
1246 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1247 sc->sc_rc_state = NULL;
1248 }
1249 }
1250 #endif /* PPP_COMPRESS */
1251
1252 /*
1253 * PPP packet input routine.
1254 * The caller has checked and removed the FCS and has inserted
1255 * the address/control bytes and the protocol high byte if they
1256 * were omitted.
1257 */
1258 void
1259 ppppktin(sc, m, lost)
1260 struct ppp_softc *sc;
1261 struct mbuf *m;
1262 int lost;
1263 {
1264 int s = splimp();
1265
1266 if (lost)
1267 m->m_flags |= M_ERRMARK;
1268 IF_ENQUEUE(&sc->sc_rawq, m);
1269 schednetisr(NETISR_PPP);
1270 splx(s);
1271 }
1272
1273 /*
1274 * Process a received PPP packet, doing decompression as necessary.
1275 * Should be called at splsoftnet.
1276 */
1277 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1278 TYPE_UNCOMPRESSED_TCP)
1279
1280 static void
1281 ppp_inproc(sc, m)
1282 struct ppp_softc *sc;
1283 struct mbuf *m;
1284 {
1285 struct ifnet *ifp = &sc->sc_if;
1286 struct ifqueue *inq;
1287 int s, ilen, xlen, proto, rv;
1288 u_char *cp, adrs, ctrl;
1289 struct mbuf *mp, *dmp = NULL;
1290 u_char *iphdr;
1291 u_int hlen;
1292
1293 sc->sc_stats.ppp_ipackets++;
1294
1295 if (sc->sc_flags & SC_LOG_INPKT) {
1296 ilen = 0;
1297 for (mp = m; mp != NULL; mp = mp->m_next)
1298 ilen += mp->m_len;
1299 printf("%s: got %d bytes\n", ifp->if_xname, ilen);
1300 pppdumpm(m);
1301 }
1302
1303 cp = mtod(m, u_char *);
1304 adrs = PPP_ADDRESS(cp);
1305 ctrl = PPP_CONTROL(cp);
1306 proto = PPP_PROTOCOL(cp);
1307
1308 if (m->m_flags & M_ERRMARK) {
1309 m->m_flags &= ~M_ERRMARK;
1310 s = splimp();
1311 sc->sc_flags |= SC_VJ_RESET;
1312 splx(s);
1313 }
1314
1315 #ifdef PPP_COMPRESS
1316 /*
1317 * Decompress this packet if necessary, update the receiver's
1318 * dictionary, or take appropriate action on a CCP packet.
1319 */
1320 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1321 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1322 /* decompress this packet */
1323 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1324 if (rv == DECOMP_OK) {
1325 m_freem(m);
1326 if (dmp == NULL) {
1327 /* no error, but no decompressed packet produced */
1328 return;
1329 }
1330 m = dmp;
1331 cp = mtod(m, u_char *);
1332 proto = PPP_PROTOCOL(cp);
1333
1334 } else {
1335 /*
1336 * An error has occurred in decompression.
1337 * Pass the compressed packet up to pppd, which may take
1338 * CCP down or issue a Reset-Req.
1339 */
1340 if (sc->sc_flags & SC_DEBUG)
1341 printf("%s: decompress failed %d\n", ifp->if_xname, rv);
1342 s = splimp();
1343 sc->sc_flags |= SC_VJ_RESET;
1344 if (rv == DECOMP_ERROR)
1345 sc->sc_flags |= SC_DC_ERROR;
1346 else
1347 sc->sc_flags |= SC_DC_FERROR;
1348 splx(s);
1349 }
1350
1351 } else {
1352 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1353 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1354 }
1355 if (proto == PPP_CCP) {
1356 ppp_ccp(sc, m, 1);
1357 }
1358 }
1359 #endif
1360
1361 ilen = 0;
1362 for (mp = m; mp != NULL; mp = mp->m_next)
1363 ilen += mp->m_len;
1364
1365 #ifdef VJC
1366 if (sc->sc_flags & SC_VJ_RESET) {
1367 /*
1368 * If we've missed a packet, we must toss subsequent compressed
1369 * packets which don't have an explicit connection ID.
1370 */
1371 if (sc->sc_comp)
1372 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1373 s = splimp();
1374 sc->sc_flags &= ~SC_VJ_RESET;
1375 splx(s);
1376 }
1377
1378 /*
1379 * See if we have a VJ-compressed packet to uncompress.
1380 */
1381 if (proto == PPP_VJC_COMP) {
1382 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1383 goto bad;
1384
1385 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1386 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1387 sc->sc_comp, &iphdr, &hlen);
1388
1389 if (xlen <= 0) {
1390 if (sc->sc_flags & SC_DEBUG)
1391 printf("%s: VJ uncompress failed on type comp\n",
1392 ifp->if_xname);
1393 goto bad;
1394 }
1395
1396 /* Copy the PPP and IP headers into a new mbuf. */
1397 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1398 if (mp == NULL)
1399 goto bad;
1400 mp->m_len = 0;
1401 mp->m_next = NULL;
1402 if (hlen + PPP_HDRLEN > MHLEN) {
1403 MCLGET(mp, M_DONTWAIT);
1404 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1405 m_freem(mp);
1406 goto bad; /* lose if big headers and no clusters */
1407 }
1408 }
1409 cp = mtod(mp, u_char *);
1410 cp[0] = adrs;
1411 cp[1] = ctrl;
1412 cp[2] = 0;
1413 cp[3] = PPP_IP;
1414 proto = PPP_IP;
1415 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1416 mp->m_len = hlen + PPP_HDRLEN;
1417
1418 /*
1419 * Trim the PPP and VJ headers off the old mbuf
1420 * and stick the new and old mbufs together.
1421 */
1422 m->m_data += PPP_HDRLEN + xlen;
1423 m->m_len -= PPP_HDRLEN + xlen;
1424 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1425 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1426 mp->m_len += m->m_len;
1427 MFREE(m, mp->m_next);
1428 } else
1429 mp->m_next = m;
1430 m = mp;
1431 ilen += hlen - xlen;
1432
1433 } else if (proto == PPP_VJC_UNCOMP) {
1434 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1435 goto bad;
1436
1437 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1438 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1439 sc->sc_comp, &iphdr, &hlen);
1440
1441 if (xlen < 0) {
1442 if (sc->sc_flags & SC_DEBUG)
1443 printf("%s: VJ uncompress failed on type uncomp\n",
1444 ifp->if_xname);
1445 goto bad;
1446 }
1447
1448 proto = PPP_IP;
1449 cp[3] = PPP_IP;
1450 }
1451 #endif /* VJC */
1452
1453 /*
1454 * If the packet will fit in a header mbuf, don't waste a
1455 * whole cluster on it.
1456 */
1457 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1458 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1459 if (mp != NULL) {
1460 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1461 m_freem(m);
1462 m = mp;
1463 m->m_len = ilen;
1464 }
1465 }
1466 m->m_pkthdr.len = ilen;
1467 m->m_pkthdr.rcvif = ifp;
1468
1469 if ((proto & 0x8000) == 0) {
1470 #ifdef PPP_FILTER
1471 /*
1472 * See whether we want to pass this packet, and
1473 * if it counts as link activity.
1474 */
1475 if (sc->sc_pass_filt_in.bf_insns != 0
1476 && bpf_filter(sc->sc_pass_filt_in.bf_insns, (u_char *) m,
1477 ilen, 0) == 0) {
1478 /* drop this packet */
1479 m_freem(m);
1480 return;
1481 }
1482 if (sc->sc_active_filt_in.bf_insns == 0
1483 || bpf_filter(sc->sc_active_filt_in.bf_insns, (u_char *) m,
1484 ilen, 0))
1485 sc->sc_last_recv = time.tv_sec;
1486 #else
1487 /*
1488 * Record the time that we received this packet.
1489 */
1490 sc->sc_last_recv = time.tv_sec;
1491 #endif /* PPP_FILTER */
1492 }
1493
1494 #if NBPFILTER > 0
1495 /* See if bpf wants to look at the packet. */
1496 if (sc->sc_bpf)
1497 bpf_mtap(sc->sc_bpf, m);
1498 #endif
1499
1500 rv = 0;
1501 switch (proto) {
1502 #ifdef INET
1503 case PPP_IP:
1504 /*
1505 * IP packet - take off the ppp header and pass it up to IP.
1506 */
1507 if ((ifp->if_flags & IFF_UP) == 0
1508 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1509 /* interface is down - drop the packet. */
1510 m_freem(m);
1511 return;
1512 }
1513 m->m_pkthdr.len -= PPP_HDRLEN;
1514 m->m_data += PPP_HDRLEN;
1515 m->m_len -= PPP_HDRLEN;
1516 #ifdef GATEWAY
1517 if (ipflow_fastforward(m))
1518 return;
1519 #endif
1520 schednetisr(NETISR_IP);
1521 inq = &ipintrq;
1522 break;
1523 #endif
1524
1525 #ifdef INET6
1526 case PPP_IPV6:
1527 /*
1528 * IPv6 packet - take off the ppp header and pass it up to IPv6.
1529 */
1530 if ((ifp->if_flags & IFF_UP) == 0
1531 || sc->sc_npmode[NP_IPV6] != NPMODE_PASS) {
1532 /* interface is down - drop the packet. */
1533 m_freem(m);
1534 return;
1535 }
1536 m->m_pkthdr.len -= PPP_HDRLEN;
1537 m->m_data += PPP_HDRLEN;
1538 m->m_len -= PPP_HDRLEN;
1539 schednetisr(NETISR_IPV6);
1540 inq = &ip6intrq;
1541 break;
1542 #endif
1543
1544 default:
1545 /*
1546 * Some other protocol - place on input queue for read().
1547 */
1548 inq = &sc->sc_inq;
1549 rv = 1;
1550 break;
1551 }
1552
1553 /*
1554 * Put the packet on the appropriate input queue.
1555 */
1556 s = splimp();
1557 if (IF_QFULL(inq)) {
1558 IF_DROP(inq);
1559 splx(s);
1560 if (sc->sc_flags & SC_DEBUG)
1561 printf("%s: input queue full\n", ifp->if_xname);
1562 ifp->if_iqdrops++;
1563 goto bad;
1564 }
1565 IF_ENQUEUE(inq, m);
1566 splx(s);
1567 ifp->if_ipackets++;
1568 ifp->if_ibytes += ilen;
1569 ifp->if_lastchange = time;
1570
1571 if (rv)
1572 (*sc->sc_ctlp)(sc);
1573
1574 return;
1575
1576 bad:
1577 m_freem(m);
1578 sc->sc_if.if_ierrors++;
1579 sc->sc_stats.ppp_ierrors++;
1580 }
1581
1582 #define MAX_DUMP_BYTES 128
1583
1584 static void
1585 pppdumpm(m0)
1586 struct mbuf *m0;
1587 {
1588 char buf[3*MAX_DUMP_BYTES+4];
1589 char *bp = buf;
1590 struct mbuf *m;
1591 static char digits[] = "0123456789abcdef";
1592
1593 for (m = m0; m; m = m->m_next) {
1594 int l = m->m_len;
1595 u_char *rptr = (u_char *)m->m_data;
1596
1597 while (l--) {
1598 if (bp > buf + sizeof(buf) - 4)
1599 goto done;
1600 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1601 *bp++ = digits[*rptr++ & 0xf];
1602 }
1603
1604 if (m->m_next) {
1605 if (bp > buf + sizeof(buf) - 3)
1606 goto done;
1607 *bp++ = '|';
1608 } else
1609 *bp++ = ' ';
1610 }
1611 done:
1612 if (m)
1613 *bp++ = '>';
1614 *bp = 0;
1615 printf("%s\n", buf);
1616 }
1617
1618 #endif /* NPPP > 0 */
1619