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