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