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