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