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