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