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