Eliminate use of curthread in if_ioctl functions by passing down the
[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.13 2004/03/23 22:19:06 hsu 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                            struct ucred *);
143 static void     pppintr (struct netmsg *msg);
144
145 static void     ppp_requeue (struct ppp_softc *);
146 static void     ppp_ccp (struct ppp_softc *, struct mbuf *m, int rcvd);
147 static void     ppp_ccp_closed (struct ppp_softc *);
148 static void     ppp_inproc (struct ppp_softc *, struct mbuf *);
149 static void     pppdumpm (struct mbuf *m0);
150
151 /*
152  * Some useful mbuf macros not in mbuf.h.
153  */
154 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
155
156 #define M_DATASTART(m)  \
157         (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
158             (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
159
160 #define M_DATASIZE(m)   \
161         (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
162             (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
163
164 /*
165  * We steal two bits in the mbuf m_flags, to mark high-priority packets
166  * for output, and received packets following lost/corrupted packets.
167  */
168 #define M_HIGHPRI       0x2000  /* output packet for sc_fastq */
169 #define M_ERRMARK       0x4000  /* steal a bit in mbuf m_flags */
170
171
172 #ifdef PPP_COMPRESS
173 /*
174  * List of compressors we know about.
175  * We leave some space so maybe we can modload compressors.
176  */
177
178 extern struct compressor ppp_bsd_compress;
179 extern struct compressor ppp_deflate, ppp_deflate_draft;
180
181 static struct compressor *ppp_compressors[8] = {
182 #if DO_BSD_COMPRESS && defined(PPP_BSDCOMP)
183     &ppp_bsd_compress,
184 #endif
185 #if DO_DEFLATE && defined(PPP_DEFLATE)
186     &ppp_deflate,
187     &ppp_deflate_draft,
188 #endif
189     NULL
190 };
191 #endif /* PPP_COMPRESS */
192
193 /*
194  * Called from boot code to establish ppp interfaces.
195  */
196 static void
197 pppattach(dummy)
198     void *dummy;
199 {
200     struct ppp_softc *sc;
201     int i = 0;
202
203     for (sc = ppp_softc; i < NPPP; sc++) {
204         if_initname(&(sc->sc_if), "ppp", 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     netisr_register(NETISR_PPP, cpu0_portfn, 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_dunit;
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("%s: comp_alloc failed\n",
437                                sc->sc_if.if_xname);
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("%s: decomp_alloc failed\n",
452                                sc->sc_if.if_xname);
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("%s: no compressor for [%x %x %x], %x\n",
463                    sc->sc_if.if_xname, 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, cr)
548     struct ifnet *ifp;
549     u_long cmd;
550     caddr_t data;
551     struct ucred *cr;
552 {
553     struct thread *td = curthread;      /* XXX */
554     struct ppp_softc *sc = &ppp_softc[ifp->if_dunit];
555     struct ifaddr *ifa = (struct ifaddr *)data;
556     struct ifreq *ifr = (struct ifreq *)data;
557     struct ppp_stats *psp;
558 #ifdef  PPP_COMPRESS
559     struct ppp_comp_stats *pcp;
560 #endif
561     int s = splimp(), error = 0;
562
563     switch (cmd) {
564     case SIOCSIFFLAGS:
565         if ((ifp->if_flags & IFF_RUNNING) == 0)
566             ifp->if_flags &= ~IFF_UP;
567         break;
568
569     case SIOCSIFADDR:
570     case SIOCAIFADDR:
571         switch(ifa->ifa_addr->sa_family) {
572 #ifdef INET
573         case AF_INET:
574             break;
575 #endif
576 #ifdef IPX
577         case AF_IPX:
578             break;
579 #endif
580         default:
581             error = EAFNOSUPPORT;
582             break;
583         }
584         break;
585
586     case SIOCSIFDSTADDR:
587         switch(ifa->ifa_addr->sa_family) {
588 #ifdef INET
589         case AF_INET:
590             break;
591 #endif
592 #ifdef IPX
593         case AF_IPX:
594             break;
595 #endif
596         default:
597             error = EAFNOSUPPORT;
598             break;
599         }
600         break;
601
602     case SIOCSIFMTU:
603         if ((error = suser(td)) != 0)
604             break;
605         if (ifr->ifr_mtu > PPP_MAXMTU)
606             error = EINVAL;
607         else {
608             sc->sc_if.if_mtu = ifr->ifr_mtu;
609             if (sc->sc_setmtu)
610                     (*sc->sc_setmtu)(sc);
611         }
612         break;
613
614     case SIOCGIFMTU:
615         ifr->ifr_mtu = sc->sc_if.if_mtu;
616         break;
617
618     case SIOCADDMULTI:
619     case SIOCDELMULTI:
620         if (ifr == 0) {
621             error = EAFNOSUPPORT;
622             break;
623         }
624         switch(ifr->ifr_addr.sa_family) {
625 #ifdef INET
626         case AF_INET:
627             break;
628 #endif
629         default:
630             error = EAFNOSUPPORT;
631             break;
632         }
633         break;
634
635     case SIOCGPPPSTATS:
636         psp = &((struct ifpppstatsreq *) data)->stats;
637         bzero(psp, sizeof(*psp));
638         psp->p = sc->sc_stats;
639 #if defined(VJC) && !defined(SL_NO_STATS)
640         if (sc->sc_comp) {
641             psp->vj.vjs_packets = sc->sc_comp->sls_packets;
642             psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
643             psp->vj.vjs_searches = sc->sc_comp->sls_searches;
644             psp->vj.vjs_misses = sc->sc_comp->sls_misses;
645             psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
646             psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
647             psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
648             psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
649         }
650 #endif /* VJC */
651         break;
652
653 #ifdef PPP_COMPRESS
654     case SIOCGPPPCSTATS:
655         pcp = &((struct ifpppcstatsreq *) data)->stats;
656         bzero(pcp, sizeof(*pcp));
657         if (sc->sc_xc_state != NULL)
658             (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
659         if (sc->sc_rc_state != NULL)
660             (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
661         break;
662 #endif /* PPP_COMPRESS */
663
664     default:
665         error = ENOTTY;
666     }
667     splx(s);
668     return (error);
669 }
670
671 /*
672  * Queue a packet.  Start transmission if not active.
673  * Packet is placed in Information field of PPP frame.
674  * Called at splnet as the if->if_output handler.
675  * Called at splnet from pppwrite().
676  */
677 int
678 pppoutput(ifp, m0, dst, rtp)
679     struct ifnet *ifp;
680     struct mbuf *m0;
681     struct sockaddr *dst;
682     struct rtentry *rtp;
683 {
684     struct ppp_softc *sc = &ppp_softc[ifp->if_dunit];
685     int protocol, address, control;
686     u_char *cp;
687     int s, error;
688     struct ip *ip;
689     struct ifqueue *ifq;
690     enum NPmode mode;
691     int len;
692     struct mbuf *m;
693
694     if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
695         || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
696         error = ENETDOWN;       /* sort of */
697         goto bad;
698     }
699
700     /*
701      * Compute PPP header.
702      */
703     m0->m_flags &= ~M_HIGHPRI;
704     switch (dst->sa_family) {
705 #ifdef INET
706     case AF_INET:
707         address = PPP_ALLSTATIONS;
708         control = PPP_UI;
709         protocol = PPP_IP;
710         mode = sc->sc_npmode[NP_IP];
711
712         /*
713          * If this packet has the "low delay" bit set in the IP header,
714          * put it on the fastq instead.
715          */
716         ip = mtod(m0, struct ip *);
717         if (ip->ip_tos & IPTOS_LOWDELAY)
718             m0->m_flags |= M_HIGHPRI;
719         break;
720 #endif
721 #ifdef IPX
722     case AF_IPX:
723         /*
724          * This is pretty bogus.. We dont have an ipxcp module in pppd
725          * yet to configure the link parameters.  Sigh. I guess a
726          * manual ifconfig would do....  -Peter
727          */
728         address = PPP_ALLSTATIONS;
729         control = PPP_UI;
730         protocol = PPP_IPX;
731         mode = NPMODE_PASS;
732         break;
733 #endif
734     case AF_UNSPEC:
735         address = PPP_ADDRESS(dst->sa_data);
736         control = PPP_CONTROL(dst->sa_data);
737         protocol = PPP_PROTOCOL(dst->sa_data);
738         mode = NPMODE_PASS;
739         break;
740     default:
741         printf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
742         error = EAFNOSUPPORT;
743         goto bad;
744     }
745
746     /*
747      * Drop this packet, or return an error, if necessary.
748      */
749     if (mode == NPMODE_ERROR) {
750         error = ENETDOWN;
751         goto bad;
752     }
753     if (mode == NPMODE_DROP) {
754         error = 0;
755         goto bad;
756     }
757
758     /*
759      * Add PPP header.  If no space in first mbuf, allocate another.
760      * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
761      */
762     if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
763         m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
764         if (m0 == 0) {
765             error = ENOBUFS;
766             goto bad;
767         }
768         m0->m_len = 0;
769     } else
770         m0->m_data -= PPP_HDRLEN;
771
772     cp = mtod(m0, u_char *);
773     *cp++ = address;
774     *cp++ = control;
775     *cp++ = protocol >> 8;
776     *cp++ = protocol & 0xff;
777     m0->m_len += PPP_HDRLEN;
778
779     len = 0;
780     for (m = m0; m != 0; m = m->m_next)
781         len += m->m_len;
782
783     if (sc->sc_flags & SC_LOG_OUTPKT) {
784         printf("%s output: ", ifp->if_xname);
785         pppdumpm(m0);
786     }
787
788     if ((protocol & 0x8000) == 0) {
789 #ifdef PPP_FILTER
790         /*
791          * Apply the pass and active filters to the packet,
792          * but only if it is a data packet.
793          */
794         *mtod(m0, u_char *) = 1;        /* indicates outbound */
795         if (sc->sc_pass_filt.bf_insns != 0
796             && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
797                           len, 0) == 0) {
798             error = 0;          /* drop this packet */
799             goto bad;
800         }
801
802         /*
803          * Update the time we sent the most recent packet.
804          */
805         if (sc->sc_active_filt.bf_insns == 0
806             || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
807             sc->sc_last_sent = time_second;
808
809         *mtod(m0, u_char *) = address;
810 #else
811         /*
812          * Update the time we sent the most recent data packet.
813          */
814         sc->sc_last_sent = time_second;
815 #endif /* PPP_FILTER */
816     }
817
818     /*
819      * See if bpf wants to look at the packet.
820      */
821     if (ifp->if_bpf)
822         bpf_mtap(ifp, m0);
823
824     /*
825      * Put the packet on the appropriate queue.
826      */
827     s = splsoftnet();   /* redundant */
828     if (mode == NPMODE_QUEUE) {
829         /* XXX we should limit the number of packets on this queue */
830         *sc->sc_npqtail = m0;
831         m0->m_nextpkt = NULL;
832         sc->sc_npqtail = &m0->m_nextpkt;
833     } else {
834         /* fastq and if_snd are emptied at spl[soft]net now */
835         ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
836         if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
837             IF_DROP(ifq);
838             splx(s);
839             sc->sc_if.if_oerrors++;
840             sc->sc_stats.ppp_oerrors++;
841             error = ENOBUFS;
842             goto bad;
843         }
844         IF_ENQUEUE(ifq, m0);
845         (*sc->sc_start)(sc);
846     }
847     getmicrotime(&ifp->if_lastchange);
848     ifp->if_opackets++;
849     ifp->if_obytes += len;
850
851     splx(s);
852     return (0);
853
854 bad:
855     m_freem(m0);
856     return (error);
857 }
858
859 /*
860  * After a change in the NPmode for some NP, move packets from the
861  * npqueue to the send queue or the fast queue as appropriate.
862  * Should be called at spl[soft]net.
863  */
864 static void
865 ppp_requeue(sc)
866     struct ppp_softc *sc;
867 {
868     struct mbuf *m, **mpp;
869     struct ifqueue *ifq;
870     enum NPmode mode;
871
872     for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
873         switch (PPP_PROTOCOL(mtod(m, u_char *))) {
874         case PPP_IP:
875             mode = sc->sc_npmode[NP_IP];
876             break;
877         default:
878             mode = NPMODE_PASS;
879         }
880
881         switch (mode) {
882         case NPMODE_PASS:
883             /*
884              * This packet can now go on one of the queues to be sent.
885              */
886             *mpp = m->m_nextpkt;
887             m->m_nextpkt = NULL;
888             ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
889             if (IF_QFULL(ifq)) {
890                 IF_DROP(ifq);
891                 sc->sc_if.if_oerrors++;
892                 sc->sc_stats.ppp_oerrors++;
893             } else
894                 IF_ENQUEUE(ifq, m);
895             break;
896
897         case NPMODE_DROP:
898         case NPMODE_ERROR:
899             *mpp = m->m_nextpkt;
900             m_freem(m);
901             break;
902
903         case NPMODE_QUEUE:
904             mpp = &m->m_nextpkt;
905             break;
906         }
907     }
908     sc->sc_npqtail = mpp;
909 }
910
911 /*
912  * Transmitter has finished outputting some stuff;
913  * remember to call sc->sc_start later at splsoftnet.
914  */
915 void
916 ppp_restart(sc)
917     struct ppp_softc *sc;
918 {
919     int s = splimp();
920
921     sc->sc_flags &= ~SC_TBUSY;
922     schednetisr(NETISR_PPP);
923     splx(s);
924 }
925
926
927 /*
928  * Get a packet to send.  This procedure is intended to be called at
929  * splsoftnet, since it may involve time-consuming operations such as
930  * applying VJ compression, packet compression, address/control and/or
931  * protocol field compression to the packet.
932  */
933 struct mbuf *
934 ppp_dequeue(sc)
935     struct ppp_softc *sc;
936 {
937     struct mbuf *m, *mp;
938     u_char *cp;
939     int address, control, protocol;
940
941     /*
942      * Grab a packet to send: first try the fast queue, then the
943      * normal queue.
944      */
945     IF_DEQUEUE(&sc->sc_fastq, m);
946     if (m == NULL)
947         IF_DEQUEUE(&sc->sc_if.if_snd, m);
948     if (m == NULL)
949         return NULL;
950
951     ++sc->sc_stats.ppp_opackets;
952
953     /*
954      * Extract the ppp header of the new packet.
955      * The ppp header will be in one mbuf.
956      */
957     cp = mtod(m, u_char *);
958     address = PPP_ADDRESS(cp);
959     control = PPP_CONTROL(cp);
960     protocol = PPP_PROTOCOL(cp);
961
962     switch (protocol) {
963     case PPP_IP:
964 #ifdef VJC
965         /*
966          * If the packet is a TCP/IP packet, see if we can compress it.
967          */
968         if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
969             struct ip *ip;
970             int type;
971
972             mp = m;
973             ip = (struct ip *) (cp + PPP_HDRLEN);
974             if (mp->m_len <= PPP_HDRLEN) {
975                 mp = mp->m_next;
976                 if (mp == NULL)
977                     break;
978                 ip = mtod(mp, struct ip *);
979             }
980             /* this code assumes the IP/TCP header is in one non-shared mbuf */
981             if (ip->ip_p == IPPROTO_TCP) {
982                 type = sl_compress_tcp(mp, ip, sc->sc_comp,
983                                        !(sc->sc_flags & SC_NO_TCP_CCID));
984                 switch (type) {
985                 case TYPE_UNCOMPRESSED_TCP:
986                     protocol = PPP_VJC_UNCOMP;
987                     break;
988                 case TYPE_COMPRESSED_TCP:
989                     protocol = PPP_VJC_COMP;
990                     cp = mtod(m, u_char *);
991                     cp[0] = address;    /* header has moved */
992                     cp[1] = control;
993                     cp[2] = 0;
994                     break;
995                 }
996                 cp[3] = protocol;       /* update protocol in PPP header */
997             }
998         }
999 #endif  /* VJC */
1000         break;
1001
1002 #ifdef PPP_COMPRESS
1003     case PPP_CCP:
1004         ppp_ccp(sc, m, 0);
1005         break;
1006 #endif  /* PPP_COMPRESS */
1007     }
1008
1009 #ifdef PPP_COMPRESS
1010     if (protocol != PPP_LCP && protocol != PPP_CCP
1011         && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1012         struct mbuf *mcomp = NULL;
1013         int slen, clen;
1014
1015         slen = 0;
1016         for (mp = m; mp != NULL; mp = mp->m_next)
1017             slen += mp->m_len;
1018         clen = (*sc->sc_xcomp->compress)
1019             (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
1020         if (mcomp != NULL) {
1021             if (sc->sc_flags & SC_CCP_UP) {
1022                 /* Send the compressed packet instead of the original. */
1023                 m_freem(m);
1024                 m = mcomp;
1025                 cp = mtod(m, u_char *);
1026                 protocol = cp[3];
1027             } else {
1028                 /* Can't transmit compressed packets until CCP is up. */
1029                 m_freem(mcomp);
1030             }
1031         }
1032     }
1033 #endif  /* PPP_COMPRESS */
1034
1035     /*
1036      * Compress the address/control and protocol, if possible.
1037      */
1038     if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1039         control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1040         protocol != PPP_LCP) {
1041         /* can compress address/control */
1042         m->m_data += 2;
1043         m->m_len -= 2;
1044     }
1045     if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1046         /* can compress protocol */
1047         if (mtod(m, u_char *) == cp) {
1048             cp[2] = cp[1];      /* move address/control up */
1049             cp[1] = cp[0];
1050         }
1051         ++m->m_data;
1052         --m->m_len;
1053     }
1054
1055     return m;
1056 }
1057
1058 /*
1059  * Software interrupt routine, called at spl[soft]net.
1060  */
1061 static void
1062 pppintr(struct netmsg *msg)
1063 {
1064     struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
1065     struct ppp_softc *sc;
1066     int i, s;
1067
1068     sc = ppp_softc;
1069     for (i = 0; i < NPPP; ++i, ++sc) {
1070         s = splimp();
1071         if (!(sc->sc_flags & SC_TBUSY)
1072             && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
1073             sc->sc_flags |= SC_TBUSY;
1074             splx(s);
1075             (*sc->sc_start)(sc);
1076         } else
1077             splx(s);
1078         for (;;) {
1079             s = splimp();
1080             IF_DEQUEUE(&sc->sc_rawq, m);
1081             splx(s);
1082             if (m == NULL)
1083                 break;
1084             ppp_inproc(sc, m);
1085         }
1086     }
1087 }
1088
1089 #ifdef PPP_COMPRESS
1090 /*
1091  * Handle a CCP packet.  `rcvd' is 1 if the packet was received,
1092  * 0 if it is about to be transmitted.
1093  */
1094 static void
1095 ppp_ccp(sc, m, rcvd)
1096     struct ppp_softc *sc;
1097     struct mbuf *m;
1098     int rcvd;
1099 {
1100     u_char *dp, *ep;
1101     struct mbuf *mp;
1102     int slen, s;
1103
1104     /*
1105      * Get a pointer to the data after the PPP header.
1106      */
1107     if (m->m_len <= PPP_HDRLEN) {
1108         mp = m->m_next;
1109         if (mp == NULL)
1110             return;
1111         dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1112     } else {
1113         mp = m;
1114         dp = mtod(mp, u_char *) + PPP_HDRLEN;
1115     }
1116
1117     ep = mtod(mp, u_char *) + mp->m_len;
1118     if (dp + CCP_HDRLEN > ep)
1119         return;
1120     slen = CCP_LENGTH(dp);
1121     if (dp + slen > ep) {
1122         if (sc->sc_flags & SC_DEBUG)
1123             printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1124                    dp, slen, mtod(mp, u_char *), mp->m_len);
1125         return;
1126     }
1127
1128     switch (CCP_CODE(dp)) {
1129     case CCP_CONFREQ:
1130     case CCP_TERMREQ:
1131     case CCP_TERMACK:
1132         /* CCP must be going down - disable compression */
1133         if (sc->sc_flags & SC_CCP_UP) {
1134             s = splimp();
1135             sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1136             splx(s);
1137         }
1138         break;
1139
1140     case CCP_CONFACK:
1141         if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1142             && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1143             && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1144             if (!rcvd) {
1145                 /* we're agreeing to send compressed packets. */
1146                 if (sc->sc_xc_state != NULL
1147                     && (*sc->sc_xcomp->comp_init)
1148                         (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1149                          sc->sc_if.if_dunit, 0, sc->sc_flags & SC_DEBUG)) {
1150                     s = splimp();
1151                     sc->sc_flags |= SC_COMP_RUN;
1152                     splx(s);
1153                 }
1154             } else {
1155                 /* peer is agreeing to send compressed packets. */
1156                 if (sc->sc_rc_state != NULL
1157                     && (*sc->sc_rcomp->decomp_init)
1158                         (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1159                          sc->sc_if.if_dunit, 0, sc->sc_mru,
1160                          sc->sc_flags & SC_DEBUG)) {
1161                     s = splimp();
1162                     sc->sc_flags |= SC_DECOMP_RUN;
1163                     sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1164                     splx(s);
1165                 }
1166             }
1167         }
1168         break;
1169
1170     case CCP_RESETACK:
1171         if (sc->sc_flags & SC_CCP_UP) {
1172             if (!rcvd) {
1173                 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1174                     (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1175             } else {
1176                 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1177                     (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1178                     s = splimp();
1179                     sc->sc_flags &= ~SC_DC_ERROR;
1180                     splx(s);
1181                 }
1182             }
1183         }
1184         break;
1185     }
1186 }
1187
1188 /*
1189  * CCP is down; free (de)compressor state if necessary.
1190  */
1191 static void
1192 ppp_ccp_closed(sc)
1193     struct ppp_softc *sc;
1194 {
1195     if (sc->sc_xc_state) {
1196         (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1197         sc->sc_xc_state = NULL;
1198     }
1199     if (sc->sc_rc_state) {
1200         (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1201         sc->sc_rc_state = NULL;
1202     }
1203 }
1204 #endif /* PPP_COMPRESS */
1205
1206 /*
1207  * PPP packet input routine.
1208  * The caller has checked and removed the FCS and has inserted
1209  * the address/control bytes and the protocol high byte if they
1210  * were omitted.
1211  */
1212 void
1213 ppppktin(sc, m, lost)
1214     struct ppp_softc *sc;
1215     struct mbuf *m;
1216     int lost;
1217 {
1218     int s = splimp();
1219
1220     if (lost)
1221         m->m_flags |= M_ERRMARK;
1222     IF_ENQUEUE(&sc->sc_rawq, m);
1223     schednetisr(NETISR_PPP);
1224     splx(s);
1225 }
1226
1227 /*
1228  * Process a received PPP packet, doing decompression as necessary.
1229  * Should be called at splsoftnet.
1230  */
1231 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1232                          TYPE_UNCOMPRESSED_TCP)
1233
1234 static void
1235 ppp_inproc(sc, m)
1236     struct ppp_softc *sc;
1237     struct mbuf *m;
1238 {
1239     struct ifnet *ifp = &sc->sc_if;
1240     int isr;
1241     int s, ilen = 0, xlen, proto, rv;
1242     u_char *cp, adrs, ctrl;
1243     struct mbuf *mp, *dmp = NULL;
1244     u_char *iphdr;
1245     u_int hlen;
1246
1247     sc->sc_stats.ppp_ipackets++;
1248
1249     if (sc->sc_flags & SC_LOG_INPKT) {
1250         ilen = 0;
1251         for (mp = m; mp != NULL; mp = mp->m_next)
1252             ilen += mp->m_len;
1253         printf("%s: got %d bytes\n", ifp->if_xname, ilen);
1254         pppdumpm(m);
1255     }
1256
1257     cp = mtod(m, u_char *);
1258     adrs = PPP_ADDRESS(cp);
1259     ctrl = PPP_CONTROL(cp);
1260     proto = PPP_PROTOCOL(cp);
1261
1262     if (m->m_flags & M_ERRMARK) {
1263         m->m_flags &= ~M_ERRMARK;
1264         s = splimp();
1265         sc->sc_flags |= SC_VJ_RESET;
1266         splx(s);
1267     }
1268
1269 #ifdef PPP_COMPRESS
1270     /*
1271      * Decompress this packet if necessary, update the receiver's
1272      * dictionary, or take appropriate action on a CCP packet.
1273      */
1274     if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1275         && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1276         /* decompress this packet */
1277         rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1278         if (rv == DECOMP_OK) {
1279             m_freem(m);
1280             if (dmp == NULL) {
1281                 /* no error, but no decompressed packet produced */
1282                 return;
1283             }
1284             m = dmp;
1285             cp = mtod(m, u_char *);
1286             proto = PPP_PROTOCOL(cp);
1287
1288         } else {
1289             /*
1290              * An error has occurred in decompression.
1291              * Pass the compressed packet up to pppd, which may take
1292              * CCP down or issue a Reset-Req.
1293              */
1294             if (sc->sc_flags & SC_DEBUG)
1295                 printf("%s: decompress failed %d\n", ifp->if_xname, rv);
1296             s = splimp();
1297             sc->sc_flags |= SC_VJ_RESET;
1298             if (rv == DECOMP_ERROR)
1299                 sc->sc_flags |= SC_DC_ERROR;
1300             else
1301                 sc->sc_flags |= SC_DC_FERROR;
1302             splx(s);
1303         }
1304
1305     } else {
1306         if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1307             (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1308         }
1309         if (proto == PPP_CCP) {
1310             ppp_ccp(sc, m, 1);
1311         }
1312     }
1313 #endif
1314
1315     ilen = 0;
1316     for (mp = m; mp != NULL; mp = mp->m_next)
1317         ilen += mp->m_len;
1318
1319 #ifdef VJC
1320     if (sc->sc_flags & SC_VJ_RESET) {
1321         /*
1322          * If we've missed a packet, we must toss subsequent compressed
1323          * packets which don't have an explicit connection ID.
1324          */
1325         if (sc->sc_comp)
1326             sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1327         s = splimp();
1328         sc->sc_flags &= ~SC_VJ_RESET;
1329         splx(s);
1330     }
1331
1332     /*
1333      * See if we have a VJ-compressed packet to uncompress.
1334      */
1335     if (proto == PPP_VJC_COMP) {
1336         if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1337             goto bad;
1338
1339         xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1340                                       ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1341                                       sc->sc_comp, &iphdr, &hlen);
1342
1343         if (xlen <= 0) {
1344             if (sc->sc_flags & SC_DEBUG)
1345                 printf("%s: VJ uncompress failed on type comp\n",
1346                         ifp->if_xname);
1347             goto bad;
1348         }
1349
1350         /* Copy the PPP and IP headers into a new mbuf. */
1351         MGETHDR(mp, M_DONTWAIT, MT_DATA);
1352         if (mp == NULL)
1353             goto bad;
1354         mp->m_len = 0;
1355         mp->m_next = NULL;
1356         if (hlen + PPP_HDRLEN > MHLEN) {
1357             MCLGET(mp, M_DONTWAIT);
1358             if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1359                 m_freem(mp);
1360                 goto bad;       /* lose if big headers and no clusters */
1361             }
1362         }
1363         cp = mtod(mp, u_char *);
1364         cp[0] = adrs;
1365         cp[1] = ctrl;
1366         cp[2] = 0;
1367         cp[3] = PPP_IP;
1368         proto = PPP_IP;
1369         bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1370         mp->m_len = hlen + PPP_HDRLEN;
1371
1372         /*
1373          * Trim the PPP and VJ headers off the old mbuf
1374          * and stick the new and old mbufs together.
1375          */
1376         m->m_data += PPP_HDRLEN + xlen;
1377         m->m_len -= PPP_HDRLEN + xlen;
1378         if (m->m_len <= M_TRAILINGSPACE(mp)) {
1379             bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1380             mp->m_len += m->m_len;
1381             mp->m_next = m_free(m);
1382         } else {
1383             mp->m_next = m;
1384         }
1385         m = mp;
1386         ilen += hlen - xlen;
1387
1388     } else if (proto == PPP_VJC_UNCOMP) {
1389         if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1390             goto bad;
1391
1392         xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1393                                       ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1394                                       sc->sc_comp, &iphdr, &hlen);
1395
1396         if (xlen < 0) {
1397             if (sc->sc_flags & SC_DEBUG)
1398                 printf("%s: VJ uncompress failed on type uncomp\n",
1399                         ifp->if_xname);
1400             goto bad;
1401         }
1402
1403         proto = PPP_IP;
1404         cp[3] = PPP_IP;
1405     }
1406 #endif /* VJC */
1407
1408     /*
1409      * If the packet will fit in a header mbuf, don't waste a
1410      * whole cluster on it.
1411      */
1412     if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1413         MGETHDR(mp, M_DONTWAIT, MT_DATA);
1414         if (mp != NULL) {
1415             m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1416             m_freem(m);
1417             m = mp;
1418             m->m_len = ilen;
1419         }
1420     }
1421     m->m_pkthdr.len = ilen;
1422     m->m_pkthdr.rcvif = ifp;
1423
1424     if ((proto & 0x8000) == 0) {
1425 #ifdef PPP_FILTER
1426         /*
1427          * See whether we want to pass this packet, and
1428          * if it counts as link activity.
1429          */
1430         adrs = *mtod(m, u_char *);      /* save address field */
1431         *mtod(m, u_char *) = 0;         /* indicate inbound */
1432         if (sc->sc_pass_filt.bf_insns != 0
1433             && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1434                           ilen, 0) == 0) {
1435             /* drop this packet */
1436             m_freem(m);
1437             return;
1438         }
1439         if (sc->sc_active_filt.bf_insns == 0
1440             || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1441             sc->sc_last_recv = time_second;
1442
1443         *mtod(m, u_char *) = adrs;
1444 #else
1445         /*
1446          * Record the time that we received this packet.
1447          */
1448         sc->sc_last_recv = time_second;
1449 #endif /* PPP_FILTER */
1450     }
1451
1452     /* See if bpf wants to look at the packet. */
1453     if (sc->sc_if.if_bpf)
1454         bpf_mtap(&sc->sc_if, m);
1455
1456     isr = -1;
1457     switch (proto) {
1458 #ifdef INET
1459     case PPP_IP:
1460         /*
1461          * IP packet - take off the ppp header and pass it up to IP.
1462          */
1463         if ((ifp->if_flags & IFF_UP) == 0
1464             || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1465             /* interface is down - drop the packet. */
1466             m_freem(m);
1467             return;
1468         }
1469         m->m_pkthdr.len -= PPP_HDRLEN;
1470         m->m_data += PPP_HDRLEN;
1471         m->m_len -= PPP_HDRLEN;
1472         if (ipflow_fastforward(m))
1473             return;
1474         isr = NETISR_IP;
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         isr = NETISR_IPX;
1492         sc->sc_last_recv = time_second; /* update time of last pkt rcvd */
1493         break;
1494 #endif
1495
1496     default:
1497         /*
1498          * Some other protocol - place on input queue for read().
1499          */
1500         break;
1501     }
1502
1503     if (isr == -1)
1504         rv = IF_HANDOFF(&sc->sc_inq, m, NULL);
1505     else
1506         rv = (netisr_queue(isr, m) == 0);
1507     if (!rv) {
1508         if (sc->sc_flags & SC_DEBUG)
1509             printf("%s: input queue full\n", ifp->if_xname);
1510         ifp->if_iqdrops++;
1511         goto bad;
1512     }
1513
1514     ifp->if_ipackets++;
1515     ifp->if_ibytes += ilen;
1516     getmicrotime(&ifp->if_lastchange);
1517
1518     if (rv)
1519         (*sc->sc_ctlp)(sc);
1520
1521     return;
1522
1523  bad:
1524     m_freem(m);
1525     sc->sc_if.if_ierrors++;
1526     sc->sc_stats.ppp_ierrors++;
1527 }
1528
1529 #define MAX_DUMP_BYTES  128
1530
1531 static void
1532 pppdumpm(m0)
1533     struct mbuf *m0;
1534 {
1535     char buf[3*MAX_DUMP_BYTES+4];
1536     char *bp = buf;
1537     struct mbuf *m;
1538
1539     for (m = m0; m; m = m->m_next) {
1540         int l = m->m_len;
1541         u_char *rptr = (u_char *)m->m_data;
1542
1543         while (l--) {
1544             if (bp > buf + sizeof(buf) - 4)
1545                 goto done;
1546             *bp++ = hex2ascii(*rptr >> 4);
1547             *bp++ = hex2ascii(*rptr++ & 0xf);
1548         }
1549
1550         if (m->m_next) {
1551             if (bp > buf + sizeof(buf) - 3)
1552                 goto done;
1553             *bp++ = '|';
1554         } else
1555             *bp++ = ' ';
1556     }
1557 done:
1558     if (m)
1559         *bp++ = '>';
1560     *bp = 0;
1561     printf("%s\n", buf);
1562 }