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