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