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