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