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