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