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