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