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