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