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