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