]> git.ozlabs.org Git - ppp.git/blob - freebsd-2.0/if_ppp.c
modified to look more like the netbsd-1.1 version
[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.8 1996/07/01 05:25:55 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,
894              (sc->sc_flags & SC_CCP_UP? sc->sc_if.if_mtu: 0));
895         if (mcomp != NULL) {
896             m_freem(m);
897             m = mcomp;
898             cp = mtod(m, u_char *);
899             protocol = cp[3];
900         }
901     }
902 #endif  /* PPP_COMPRESS */
903
904     /*
905      * Compress the address/control and protocol, if possible.
906      */
907     if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
908         control == PPP_UI && protocol != PPP_ALLSTATIONS &&
909         protocol != PPP_LCP) {
910         /* can compress address/control */
911         m->m_data += 2;
912         m->m_len -= 2;
913     }
914     if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
915         /* can compress protocol */
916         if (mtod(m, u_char *) == cp) {
917             cp[2] = cp[1];      /* move address/control up */
918             cp[1] = cp[0];
919         }
920         ++m->m_data;
921         --m->m_len;
922     }
923
924     return m;
925 }
926
927 /*
928  * Software interrupt routine, called at splsoftnet.
929  */
930 void
931 pppintr()
932 {
933     struct ppp_softc *sc;
934     int i, s, s2;
935     struct mbuf *m;
936
937     sc = ppp_softc;
938     s = splsoftnet();
939     for (i = 0; i < NPPP; ++i, ++sc) {
940         if (!(sc->sc_flags & SC_TBUSY)
941             && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
942             s2 = splimp();
943             sc->sc_flags |= SC_TBUSY;
944             splx(s2);
945             (*sc->sc_start)(sc);
946         }
947         for (;;) {
948             s2 = splimp();
949             IF_DEQUEUE(&sc->sc_rawq, m);
950             splx(s2);
951             if (m == NULL)
952                 break;
953             ppp_inproc(sc, m);
954         }
955     }
956     splx(s);
957 }
958
959 #ifdef PPP_COMPRESS
960 /*
961  * Handle a CCP packet.  `rcvd' is 1 if the packet was received,
962  * 0 if it is about to be transmitted.
963  */
964 static void
965 ppp_ccp(sc, m, rcvd)
966     struct ppp_softc *sc;
967     struct mbuf *m;
968     int rcvd;
969 {
970     u_char *dp, *ep;
971     struct mbuf *mp;
972     int slen, s;
973
974     /*
975      * Get a pointer to the data after the PPP header.
976      */
977     if (m->m_len <= PPP_HDRLEN) {
978         mp = m->m_next;
979         if (mp == NULL)
980             return;
981         dp = (mp != NULL)? mtod(mp, u_char *): NULL;
982     } else {
983         mp = m;
984         dp = mtod(mp, u_char *) + PPP_HDRLEN;
985     }
986
987     ep = mtod(mp, u_char *) + mp->m_len;
988     if (dp + CCP_HDRLEN > ep)
989         return;
990     slen = CCP_LENGTH(dp);
991     if (dp + slen > ep) {
992         if (sc->sc_flags & SC_DEBUG)
993             printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
994                    dp, slen, mtod(mp, u_char *), mp->m_len);
995         return;
996     }
997
998     switch (CCP_CODE(dp)) {
999     case CCP_CONFREQ:
1000     case CCP_TERMREQ:
1001     case CCP_TERMACK:
1002         /* CCP must be going down - disable compression */
1003         if (sc->sc_flags & SC_CCP_UP) {
1004             s = splimp();
1005             sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1006             splx(s);
1007         }
1008         break;
1009
1010     case CCP_CONFACK:
1011         if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1012             && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1013             && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1014             if (!rcvd) {
1015                 /* we're agreeing to send compressed packets. */
1016                 if (sc->sc_xc_state != NULL
1017                     && (*sc->sc_xcomp->comp_init)
1018                         (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1019                          sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) {
1020                     s = splimp();
1021                     sc->sc_flags |= SC_COMP_RUN;
1022                     splx(s);
1023                 }
1024             } else {
1025                 /* peer is agreeing to send compressed packets. */
1026                 if (sc->sc_rc_state != NULL
1027                     && (*sc->sc_rcomp->decomp_init)
1028                         (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1029                          sc->sc_if.if_unit, 0, sc->sc_mru,
1030                          sc->sc_flags & SC_DEBUG)) {
1031                     s = splimp();
1032                     sc->sc_flags |= SC_DECOMP_RUN;
1033                     sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1034                     splx(s);
1035                 }
1036             }
1037         }
1038         break;
1039
1040     case CCP_RESETACK:
1041         if (sc->sc_flags & SC_CCP_UP) {
1042             if (!rcvd) {
1043                 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1044                     (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1045             } else {
1046                 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1047                     (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1048                     s = splimp();
1049                     sc->sc_flags &= ~SC_DC_ERROR;
1050                     splx(s);
1051                 }
1052             }
1053         }
1054         break;
1055     }
1056 }
1057
1058 /*
1059  * CCP is down; free (de)compressor state if necessary.
1060  */
1061 static void
1062 ppp_ccp_closed(sc)
1063     struct ppp_softc *sc;
1064 {
1065     if (sc->sc_xc_state) {
1066         (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1067         sc->sc_xc_state = NULL;
1068     }
1069     if (sc->sc_rc_state) {
1070         (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1071         sc->sc_rc_state = NULL;
1072     }
1073 }
1074 #endif /* PPP_COMPRESS */
1075
1076 /*
1077  * PPP packet input routine.
1078  * The caller has checked and removed the FCS and has inserted
1079  * the address/control bytes and the protocol high byte if they
1080  * were omitted.
1081  */
1082 void
1083 ppppktin(sc, m, lost)
1084     struct ppp_softc *sc;
1085     struct mbuf *m;
1086     int lost;
1087 {
1088     int s = splimp();
1089
1090     if (lost)
1091         m->m_flags |= M_ERRMARK;
1092     IF_ENQUEUE(&sc->sc_rawq, m);
1093     schednetisr(NETISR_PPP);
1094     splx(s);
1095 }
1096
1097 /*
1098  * Process a received PPP packet, doing decompression as necessary.
1099  * Should be called at splsoftnet.
1100  */
1101 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1102                          TYPE_UNCOMPRESSED_TCP)
1103
1104 static void
1105 ppp_inproc(sc, m)
1106     struct ppp_softc *sc;
1107     struct mbuf *m;
1108 {
1109     struct ifnet *ifp = &sc->sc_if;
1110     struct ifqueue *inq;
1111     int s, ilen, xlen, proto, rv;
1112     u_char *cp, adrs, ctrl;
1113     struct mbuf *mp, *dmp = NULL;
1114     u_char *iphdr;
1115     u_int hlen;
1116
1117     sc->sc_stats.ppp_ipackets++;
1118
1119     if (sc->sc_flags & SC_LOG_INPKT) {
1120         ilen = 0;
1121         for (mp = m; mp != NULL; mp = mp->m_next)
1122             ilen += mp->m_len;
1123         printf("ppp%d: got %d bytes\n", ifp->if_unit, ilen);
1124         pppdumpm(m);
1125     }
1126
1127     cp = mtod(m, u_char *);
1128     adrs = PPP_ADDRESS(cp);
1129     ctrl = PPP_CONTROL(cp);
1130     proto = PPP_PROTOCOL(cp);
1131
1132     if (m->m_flags & M_ERRMARK) {
1133         m->m_flags &= ~M_ERRMARK;
1134         s = splimp();
1135         sc->sc_flags |= SC_VJ_RESET;
1136         splx(s);
1137     }
1138
1139 #ifdef PPP_COMPRESS
1140     /*
1141      * Decompress this packet if necessary, update the receiver's
1142      * dictionary, or take appropriate action on a CCP packet.
1143      */
1144     if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1145         && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1146         /* decompress this packet */
1147         rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1148         if (rv == DECOMP_OK) {
1149             m_freem(m);
1150             if (dmp == NULL) {
1151                 /* no error, but no decompressed packet produced */
1152                 return;
1153             }
1154             m = dmp;
1155             cp = mtod(m, u_char *);
1156             proto = PPP_PROTOCOL(cp);
1157
1158         } else {
1159             /*
1160              * An error has occurred in decompression.
1161              * Pass the compressed packet up to pppd, which may take
1162              * CCP down or issue a Reset-Req.
1163              */
1164             if (sc->sc_flags & SC_DEBUG)
1165                 printf("ppp%d: decompress failed %d\n", ifp->if_unit, rv);
1166             s = splimp();
1167             sc->sc_flags |= SC_VJ_RESET;
1168             if (rv == DECOMP_ERROR)
1169                 sc->sc_flags |= SC_DC_ERROR;
1170             else
1171                 sc->sc_flags |= SC_DC_FERROR;
1172             splx(s);
1173         }
1174
1175     } else {
1176         if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1177             (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1178         }
1179         if (proto == PPP_CCP) {
1180             ppp_ccp(sc, m, 1);
1181         }
1182     }
1183 #endif
1184
1185     ilen = 0;
1186     for (mp = m; mp != NULL; mp = mp->m_next)
1187         ilen += mp->m_len;
1188
1189 #ifdef VJC
1190     if (sc->sc_flags & SC_VJ_RESET) {
1191         /*
1192          * If we've missed a packet, we must toss subsequent compressed
1193          * packets which don't have an explicit connection ID.
1194          */
1195         if (sc->sc_comp)
1196             vj_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1197         s = splimp();
1198         sc->sc_flags &= ~SC_VJ_RESET;
1199         splx(s);
1200     }
1201
1202     /*
1203      * See if we have a VJ-compressed packet to uncompress.
1204      */
1205     if (proto == PPP_VJC_COMP) {
1206         if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1207             goto bad;
1208
1209         xlen = vj_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1210                                       ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1211                                       sc->sc_comp, &iphdr, &hlen);
1212
1213         if (xlen <= 0) {
1214             if (sc->sc_flags & SC_DEBUG)
1215                 printf("ppp%d: VJ uncompress failed on type comp\n",
1216                         ifp->if_unit);
1217             goto bad;
1218         }
1219
1220         /* Copy the PPP and IP headers into a new mbuf. */
1221         MGETHDR(mp, M_DONTWAIT, MT_DATA);
1222         if (mp == NULL)
1223             goto bad;
1224         mp->m_len = 0;
1225         mp->m_next = NULL;
1226         if (hlen + PPP_HDRLEN > MHLEN) {
1227             MCLGET(mp, M_DONTWAIT);
1228             if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1229                 m_freem(mp);
1230                 goto bad;       /* lose if big headers and no clusters */
1231             }
1232         }
1233         cp = mtod(mp, u_char *);
1234         cp[0] = adrs;
1235         cp[1] = ctrl;
1236         cp[2] = 0;
1237         cp[3] = PPP_IP;
1238         proto = PPP_IP;
1239         bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1240         mp->m_len = hlen + PPP_HDRLEN;
1241
1242         /*
1243          * Trim the PPP and VJ headers off the old mbuf
1244          * and stick the new and old mbufs together.
1245          */
1246         m->m_data += PPP_HDRLEN + xlen;
1247         m->m_len -= PPP_HDRLEN + xlen;
1248         if (m->m_len <= M_TRAILINGSPACE(mp)) {
1249             bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1250             mp->m_len += m->m_len;
1251             MFREE(m, mp->m_next);
1252         } else
1253             mp->m_next = m;
1254         m = mp;
1255         ilen += hlen - xlen;
1256
1257     } else if (proto == PPP_VJC_UNCOMP) {
1258         if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1259             goto bad;
1260
1261         xlen = vj_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1262                                       ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1263                                       sc->sc_comp, &iphdr, &hlen);
1264
1265         if (xlen < 0) {
1266             if (sc->sc_flags & SC_DEBUG)
1267                 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1268                         ifp->if_unit);
1269             goto bad;
1270         }
1271
1272         proto = PPP_IP;
1273         cp[3] = PPP_IP;
1274     }
1275 #endif /* VJC */
1276
1277     /*
1278      * If the packet will fit in a header mbuf, don't waste a
1279      * whole cluster on it.
1280      */
1281     if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1282         MGETHDR(mp, M_DONTWAIT, MT_DATA);
1283         if (mp != NULL) {
1284             m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1285             m_freem(m);
1286             m = mp;
1287             m->m_len = ilen;
1288         }
1289     }
1290     m->m_pkthdr.len = ilen;
1291     m->m_pkthdr.rcvif = ifp;
1292
1293     /*
1294      * Record the time that we received this packet.
1295      */
1296     if ((proto & 0x8000) == 0) {
1297         sc->sc_last_recv = time.tv_sec;
1298     }
1299
1300 #if NBPFILTER > 0
1301     /* See if bpf wants to look at the packet. */
1302     if (sc->sc_bpf)
1303         bpf_mtap(sc->sc_bpf, m);
1304 #endif
1305
1306     rv = 0;
1307     switch (proto) {
1308 #ifdef INET
1309     case PPP_IP:
1310         /*
1311          * IP packet - take off the ppp header and pass it up to IP.
1312          */
1313         if ((ifp->if_flags & IFF_UP) == 0
1314             || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1315             /* interface is down - drop the packet. */
1316             m_freem(m);
1317             return;
1318         }
1319         m->m_pkthdr.len -= PPP_HDRLEN;
1320         m->m_data += PPP_HDRLEN;
1321         m->m_len -= PPP_HDRLEN;
1322         schednetisr(NETISR_IP);
1323         inq = &ipintrq;
1324         break;
1325 #endif
1326
1327     default:
1328         /*
1329          * Some other protocol - place on input queue for read().
1330          */
1331         inq = &sc->sc_inq;
1332         rv = 1;
1333         break;
1334     }
1335
1336     /*
1337      * Put the packet on the appropriate input queue.
1338      */
1339     s = splimp();
1340     if (IF_QFULL(inq)) {
1341         IF_DROP(inq);
1342         splx(s);
1343         if (sc->sc_flags & SC_DEBUG)
1344             printf("ppp%d: input queue full\n", ifp->if_unit);
1345         ifp->if_iqdrops++;
1346         goto bad;
1347     }
1348     IF_ENQUEUE(inq, m);
1349     splx(s);
1350     ifp->if_ipackets++;
1351     ifp->if_ibytes += ilen;
1352     ifp->if_lastchange = time;
1353
1354     if (rv)
1355         (*sc->sc_ctlp)(sc);
1356
1357     return;
1358
1359  bad:
1360     m_freem(m);
1361     sc->sc_if.if_ierrors++;
1362     sc->sc_stats.ppp_ierrors++;
1363 }
1364
1365 #define MAX_DUMP_BYTES  128
1366
1367 static void
1368 pppdumpm(m0)
1369     struct mbuf *m0;
1370 {
1371     char buf[3*MAX_DUMP_BYTES+4];
1372     char *bp = buf;
1373     struct mbuf *m;
1374     static char digits[] = "0123456789abcdef";
1375
1376     for (m = m0; m; m = m->m_next) {
1377         int l = m->m_len;
1378         u_char *rptr = (u_char *)m->m_data;
1379
1380         while (l--) {
1381             if (bp > buf + sizeof(buf) - 4)
1382                 goto done;
1383             *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1384             *bp++ = digits[*rptr++ & 0xf];
1385         }
1386
1387         if (m->m_next) {
1388             if (bp > buf + sizeof(buf) - 3)
1389                 goto done;
1390             *bp++ = '|';
1391         } else
1392             *bp++ = ' ';
1393     }
1394 done:
1395     if (m)
1396         *bp++ = '>';
1397     *bp = 0;
1398     printf("%s\n", buf);
1399 }
1400
1401 #endif  /* NPPP > 0 */