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