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