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