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