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