]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/if_ppp.c
This commit was generated by cvs2svn to compensate for changes in r162621,
[FreeBSD/FreeBSD.git] / sys / net / if_ppp.c
1 /*
2  * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
3  */
4
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 /* $FreeBSD$ */
75 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
76 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
77
78 #include "opt_inet.h"
79 #include "opt_ipx.h"
80 #include "opt_mac.h"
81 #include "opt_ppp.h"
82
83 #ifdef INET
84 #define VJC
85 #endif
86 #define PPP_COMPRESS
87
88 #include <sys/param.h>
89 #include <sys/systm.h>
90 #include <sys/proc.h>
91 #include <sys/mac.h>
92 #include <sys/mbuf.h>
93 #include <sys/socket.h>
94 #include <sys/filio.h>
95 #include <sys/sockio.h>
96 #include <sys/kernel.h>
97 #include <sys/time.h>
98 #include <sys/malloc.h>
99 #include <sys/module.h>
100
101 #include <net/if.h>
102 #include <net/if_clone.h>
103 #include <net/if_types.h>
104 #include <net/netisr.h>
105 #include <net/bpf.h>
106
107 #ifdef INET
108 #include <netinet/in.h>
109 #include <netinet/in_systm.h>
110 #include <netinet/in_var.h>
111 #include <netinet/ip.h>
112 #endif
113
114 #ifdef IPX
115 #include <netipx/ipx.h>
116 #include <netipx/ipx_if.h>
117 #endif
118
119 #ifdef VJC
120 #include <net/slcompress.h>
121 #endif
122
123 #include <net/if_ppp.h>
124 #include <net/if_pppvar.h>
125
126 /* minimise diffs */
127 #ifndef splsoftnet
128 #define splsoftnet      splnet
129 #endif
130
131 #ifdef PPP_COMPRESS
132 #define PACKETPTR       struct mbuf *
133 #include <net/ppp_comp.h>
134 #endif
135
136 #define PPPNAME         "ppp"
137 static MALLOC_DEFINE(M_PPP, PPPNAME, "PPP interface");
138
139 static struct mtx ppp_softc_list_mtx;
140 static LIST_HEAD(, ppp_softc) ppp_softc_list;
141
142 #define PPP_LIST_LOCK_INIT()    mtx_init(&ppp_softc_list_mtx,           \
143                                     "ppp_softc_list_mtx", NULL, MTX_DEF)
144 #define PPP_LIST_LOCK_DESTROY() mtx_destroy(&ppp_softc_list_mtx)
145 #define PPP_LIST_LOCK()         mtx_lock(&ppp_softc_list_mtx)
146 #define PPP_LIST_UNLOCK()       mtx_unlock(&ppp_softc_list_mtx)
147
148 /* XXX layering violation */
149 extern void     pppasyncattach(void *);
150 extern void     pppasyncdetach(void);
151
152 static int      pppsioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
153 static void     pppintr(void);
154
155 static void     ppp_requeue(struct ppp_softc *);
156 static void     ppp_ccp(struct ppp_softc *, struct mbuf *m, int rcvd);
157 static void     ppp_ccp_closed(struct ppp_softc *);
158 static void     ppp_inproc(struct ppp_softc *, struct mbuf *);
159 static void     pppdumpm(struct mbuf *m0);
160 static int      ppp_clone_create(struct if_clone *, int, caddr_t);
161 static void     ppp_clone_destroy(struct ifnet *);
162
163 IFC_SIMPLE_DECLARE(ppp, 0);
164
165 /*
166  * Some useful mbuf macros not in mbuf.h.
167  */
168 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
169
170 #define M_DATASTART(m)  \
171         (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
172             (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
173
174 #define M_DATASIZE(m)   \
175         (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
176             (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
177
178 /*
179  * We steal two bits in the mbuf m_flags, to mark high-priority packets
180  * for output, and received packets following lost/corrupted packets.
181  */
182 #define M_HIGHPRI       0x2000  /* output packet for sc_fastq */
183 #define M_ERRMARK       0x4000  /* steal a bit in mbuf m_flags */
184
185
186 #ifdef PPP_COMPRESS
187 /*
188  * List of compressors we know about.
189  * We leave some space so maybe we can modload compressors.
190  */
191
192 extern struct compressor ppp_bsd_compress;
193 extern struct compressor ppp_deflate, ppp_deflate_draft;
194
195 static struct compressor *ppp_compressors[8] = {
196 #if defined(PPP_BSDCOMP)
197     &ppp_bsd_compress,
198 #endif
199 #if defined(PPP_DEFLATE)
200     &ppp_deflate,
201     &ppp_deflate_draft,
202 #endif
203     NULL
204 };
205 #endif /* PPP_COMPRESS */
206
207 static int
208 ppp_clone_create(struct if_clone *ifc, int unit, caddr_t params)
209 {
210         struct ifnet            *ifp;
211         struct ppp_softc        *sc;
212
213         sc = malloc(sizeof(struct ppp_softc), M_PPP, M_WAITOK | M_ZERO);
214         ifp = sc->sc_ifp = if_alloc(IFT_PPP);
215         if (ifp == NULL) {
216                 free(sc, M_PPP);
217                 return (ENOSPC);
218         }
219
220         ifp->if_softc = sc;
221         if_initname(ifp, ifc->ifc_name, unit);
222         ifp->if_mtu = PPP_MTU;
223         ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST | IFF_NEEDSGIANT;
224         ifp->if_hdrlen = PPP_HDRLEN;
225         ifp->if_ioctl = pppsioctl;
226         ifp->if_output = pppoutput;
227         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
228         sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
229         sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
230         sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
231         mtx_init(&sc->sc_inq.ifq_mtx, "ppp_inq", NULL, MTX_DEF);
232         mtx_init(&sc->sc_fastq.ifq_mtx, "ppp_fastq", NULL, MTX_DEF);
233         mtx_init(&sc->sc_rawq.ifq_mtx, "ppp_rawq", NULL, MTX_DEF);
234         if_attach(ifp);
235         bpfattach(ifp, DLT_PPP, PPP_HDRLEN);
236
237         PPP_LIST_LOCK();
238         LIST_INSERT_HEAD(&ppp_softc_list, sc, sc_list);
239         PPP_LIST_UNLOCK();
240
241         return (0);
242 }
243
244 static void
245 ppp_clone_destroy(struct ifnet *ifp)
246 {
247         struct ppp_softc *sc;
248
249         sc = ifp->if_softc;
250         PPP_LIST_LOCK();
251         LIST_REMOVE(sc, sc_list);
252         PPP_LIST_UNLOCK();
253
254         bpfdetach(ifp);
255         if_detach(ifp);
256         if_free(ifp);
257         mtx_destroy(&sc->sc_rawq.ifq_mtx);
258         mtx_destroy(&sc->sc_fastq.ifq_mtx);
259         mtx_destroy(&sc->sc_inq.ifq_mtx);
260         free(sc, M_PPP);
261 }
262
263 static int
264 ppp_modevent(module_t mod, int type, void *data) 
265 {
266
267         switch (type) { 
268         case MOD_LOAD: 
269                 PPP_LIST_LOCK_INIT();
270                 LIST_INIT(&ppp_softc_list);
271                 if_clone_attach(&ppp_cloner);
272
273                 netisr_register(NETISR_PPP, (netisr_t *)pppintr, NULL, 0);
274                 /*
275                  * XXX layering violation - if_ppp can work over any lower
276                  * level transport that cares to attach to it.
277                  */
278                 pppasyncattach(NULL);
279                 break; 
280         case MOD_UNLOAD: 
281                 /* XXX: layering violation */
282                 pppasyncdetach();
283
284                 netisr_unregister(NETISR_PPP);
285
286                 if_clone_detach(&ppp_cloner);
287                 PPP_LIST_LOCK_DESTROY();
288                 break; 
289         default:
290                 return EOPNOTSUPP;
291         } 
292         return 0; 
293
294
295 static moduledata_t ppp_mod = { 
296         "if_ppp", 
297         ppp_modevent, 
298         0
299 }; 
300
301 DECLARE_MODULE(if_ppp, ppp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
302
303 /*
304  * Allocate a ppp interface unit and initialize it.
305  */
306 struct ppp_softc *
307 pppalloc(pid)
308     pid_t pid;
309 {
310     int i;
311     char tmpname[IFNAMSIZ];
312     struct ifnet *ifp;
313     struct ppp_softc *sc;
314
315     PPP_LIST_LOCK();
316     LIST_FOREACH(sc, &ppp_softc_list, sc_list) {
317         if (sc->sc_xfer == pid) {
318             sc->sc_xfer = 0;
319             PPP_LIST_UNLOCK();
320             return sc;
321         }
322     }
323     LIST_FOREACH(sc, &ppp_softc_list, sc_list) {
324         if (sc->sc_devp == NULL)
325             break;
326     }
327     PPP_LIST_UNLOCK();
328     /* Try to clone an interface if we don't have a free one */
329     if (sc == NULL) {
330         strcpy(tmpname, PPPNAME);
331         if (if_clone_create(tmpname, sizeof(tmpname), (caddr_t) 0) != 0)
332             return NULL;
333         ifp = ifunit(tmpname);
334         if (ifp == NULL)
335             return NULL;
336         sc = ifp->if_softc;
337     }
338     if (sc == NULL || sc->sc_devp != NULL)
339         return NULL;
340
341     sc->sc_flags = 0;
342     sc->sc_mru = PPP_MRU;
343     sc->sc_relinq = NULL;
344     bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
345 #ifdef VJC
346     MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
347            M_DEVBUF, M_NOWAIT);
348     if (sc->sc_comp)
349         sl_compress_init(sc->sc_comp, -1);
350 #endif
351 #ifdef PPP_COMPRESS
352     sc->sc_xc_state = NULL;
353     sc->sc_rc_state = NULL;
354 #endif /* PPP_COMPRESS */
355     for (i = 0; i < NUM_NP; ++i)
356         sc->sc_npmode[i] = NPMODE_ERROR;
357     sc->sc_npqueue = NULL;
358     sc->sc_npqtail = &sc->sc_npqueue;
359     sc->sc_last_sent = sc->sc_last_recv = time_uptime;
360
361     return sc;
362 }
363
364 /*
365  * Deallocate a ppp unit.  Must be called at splsoftnet or higher.
366  */
367 void
368 pppdealloc(sc)
369     struct ppp_softc *sc;
370 {
371     struct mbuf *m;
372
373     if_down(PPP2IFP(sc));
374     PPP2IFP(sc)->if_flags &= ~IFF_UP;
375     PPP2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
376     getmicrotime(&PPP2IFP(sc)->if_lastchange);
377     sc->sc_devp = NULL;
378     sc->sc_xfer = 0;
379     IF_DRAIN(&sc->sc_rawq);
380     IF_DRAIN(&sc->sc_inq);
381     IF_DRAIN(&sc->sc_fastq);
382     while ((m = sc->sc_npqueue) != NULL) {
383         sc->sc_npqueue = m->m_nextpkt;
384         m_freem(m);
385     }
386 #ifdef PPP_COMPRESS
387     ppp_ccp_closed(sc);
388     sc->sc_xc_state = NULL;
389     sc->sc_rc_state = NULL;
390 #endif /* PPP_COMPRESS */
391 #ifdef PPP_FILTER
392     if (sc->sc_pass_filt.bf_insns != 0) {
393         free(sc->sc_pass_filt.bf_insns, M_DEVBUF);
394         sc->sc_pass_filt.bf_insns = 0;
395         sc->sc_pass_filt.bf_len = 0;
396     }
397     if (sc->sc_active_filt.bf_insns != 0) {
398         free(sc->sc_active_filt.bf_insns, M_DEVBUF);
399         sc->sc_active_filt.bf_insns = 0;
400         sc->sc_active_filt.bf_len = 0;
401     }
402 #endif /* PPP_FILTER */
403 #ifdef VJC
404     if (sc->sc_comp != 0) {
405         free(sc->sc_comp, M_DEVBUF);
406         sc->sc_comp = 0;
407     }
408 #endif
409 }
410
411 /*
412  * Ioctl routine for generic ppp devices.
413  */
414 int
415 pppioctl(sc, cmd, data, flag, td)
416     struct ppp_softc *sc;
417     u_long cmd;
418     caddr_t data;
419     int flag;
420     struct thread *td;
421 {
422     struct proc *p = td->td_proc;
423     int s, flags, mru, npx;
424     u_int nb;
425     int error = 0;
426     struct ppp_option_data *odp;
427     struct compressor **cp;
428     struct npioctl *npi;
429     time_t t;
430 #ifdef PPP_FILTER
431     struct bpf_program *bp, *nbp;
432     struct bpf_insn *newcode, *oldcode;
433     int newcodelen;
434 #endif /* PPP_FILTER */
435 #ifdef  PPP_COMPRESS
436     u_char ccp_option[CCP_MAX_OPTION_LENGTH];
437 #endif
438
439     switch (cmd) {
440     case FIONREAD:
441         *(int *)data = sc->sc_inq.ifq_len;
442         break;
443
444     case PPPIOCGUNIT:
445         *(int *)data = PPP2IFP(sc)->if_dunit;
446         break;
447
448     case PPPIOCGFLAGS:
449         *(u_int *)data = sc->sc_flags;
450         break;
451
452     case PPPIOCSFLAGS:
453         if ((error = suser(td)) != 0)
454             break;
455         flags = *(int *)data & SC_MASK;
456         s = splsoftnet();
457 #ifdef PPP_COMPRESS
458         if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
459             ppp_ccp_closed(sc);
460 #endif
461         splimp();
462         sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
463         splx(s);
464         break;
465
466     case PPPIOCSMRU:
467         if ((error = suser(td)) != 0)
468             return (error);
469         mru = *(int *)data;
470         if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
471             sc->sc_mru = mru;
472         break;
473
474     case PPPIOCGMRU:
475         *(int *)data = sc->sc_mru;
476         break;
477
478 #ifdef VJC
479     case PPPIOCSMAXCID:
480         if ((error = suser(td)) != 0)
481             break;
482         if (sc->sc_comp) {
483             s = splsoftnet();
484             sl_compress_init(sc->sc_comp, *(int *)data);
485             splx(s);
486         }
487         break;
488 #endif
489
490     case PPPIOCXFERUNIT:
491         if ((error = suser(td)) != 0)
492             break;
493         sc->sc_xfer = p->p_pid;
494         break;
495
496 #ifdef PPP_COMPRESS
497     case PPPIOCSCOMPRESS:
498         if ((error = suser(td)) != 0)
499             break;
500         odp = (struct ppp_option_data *) data;
501         nb = odp->length;
502         if (nb > sizeof(ccp_option))
503             nb = sizeof(ccp_option);
504         if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
505             break;
506         if (ccp_option[1] < 2) {  /* preliminary check on the length byte */
507             error = EINVAL;
508             break;
509         }
510         for (cp = ppp_compressors; *cp != NULL; ++cp)
511             if ((*cp)->compress_proto == ccp_option[0]) {
512                 /*
513                  * Found a handler for the protocol - try to allocate
514                  * a compressor or decompressor.
515                  */
516                 error = 0;
517                 if (odp->transmit) {
518                     s = splsoftnet();
519                     if (sc->sc_xc_state != NULL)
520                         (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
521                     sc->sc_xcomp = *cp;
522                     sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
523                     if (sc->sc_xc_state == NULL) {
524                         if (sc->sc_flags & SC_DEBUG)
525                             if_printf(PPP2IFP(sc), "comp_alloc failed\n");
526                         error = ENOBUFS;
527                     }
528                     splimp();
529                     sc->sc_flags &= ~SC_COMP_RUN;
530                     splx(s);
531                 } else {
532                     s = splsoftnet();
533                     if (sc->sc_rc_state != NULL)
534                         (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
535                     sc->sc_rcomp = *cp;
536                     sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
537                     if (sc->sc_rc_state == NULL) {
538                         if (sc->sc_flags & SC_DEBUG)
539                             if_printf(PPP2IFP(sc), "decomp_alloc failed\n");
540                         error = ENOBUFS;
541                     }
542                     splimp();
543                     sc->sc_flags &= ~SC_DECOMP_RUN;
544                     splx(s);
545                 }
546                 break;
547             }
548         if (sc->sc_flags & SC_DEBUG)
549             if_printf(PPP2IFP(sc), "no compressor for [%x %x %x], %x\n",
550                    ccp_option[0], ccp_option[1], ccp_option[2], nb);
551         error = EINVAL;         /* no handler found */
552         break;
553 #endif /* PPP_COMPRESS */
554
555     case PPPIOCGNPMODE:
556     case PPPIOCSNPMODE:
557         npi = (struct npioctl *) data;
558         npx = 0;        /* XXX: quiet gcc */
559         switch (npi->protocol) {
560         case PPP_IP:
561             npx = NP_IP;
562             break;
563         default:
564             error = EINVAL;
565         }
566         if (error)
567             break;
568         if (cmd == PPPIOCGNPMODE) {
569             npi->mode = sc->sc_npmode[npx];
570         } else {
571             if ((error = suser(td)) != 0)
572                 break;
573             if (npi->mode != sc->sc_npmode[npx]) {
574                 s = splsoftnet();
575                 sc->sc_npmode[npx] = npi->mode;
576                 if (npi->mode != NPMODE_QUEUE) {
577                     ppp_requeue(sc);
578                     (*sc->sc_start)(sc);
579                 }
580                 splx(s);
581             }
582         }
583         break;
584
585     case PPPIOCGIDLE:
586         s = splsoftnet();
587         t = time_uptime;
588         ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
589         ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
590         splx(s);
591         break;
592
593 #ifdef PPP_FILTER
594     case PPPIOCSPASS:
595     case PPPIOCSACTIVE:
596         nbp = (struct bpf_program *) data;
597         if ((unsigned) nbp->bf_len > BPF_MAXINSNS) {
598             error = EINVAL;
599             break;
600         }
601         newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
602         if (newcodelen != 0) {
603             MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
604             if (newcode == 0) {
605                 error = EINVAL;         /* or sumpin */
606                 break;
607             }
608             if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
609                                newcodelen)) != 0) {
610                 free(newcode, M_DEVBUF);
611                 break;
612             }
613             if (!bpf_validate(newcode, nbp->bf_len)) {
614                 free(newcode, M_DEVBUF);
615                 error = EINVAL;
616                 break;
617             }
618         } else
619             newcode = 0;
620         bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
621         oldcode = bp->bf_insns;
622         s = splimp();
623         bp->bf_len = nbp->bf_len;
624         bp->bf_insns = newcode;
625         splx(s);
626         if (oldcode != 0)
627             free(oldcode, M_DEVBUF);
628         break;
629 #endif
630
631     default:
632         error = ENOIOCTL;
633         break;
634     }
635     return (error);
636 }
637
638 /*
639  * Process an ioctl request to the ppp network interface.
640  */
641 static int
642 pppsioctl(ifp, cmd, data)
643     register struct ifnet *ifp;
644     u_long cmd;
645     caddr_t data;
646 {
647     struct thread *td = curthread;      /* XXX */
648     register struct ppp_softc *sc = ifp->if_softc;
649     register struct ifaddr *ifa = (struct ifaddr *)data;
650     register struct ifreq *ifr = (struct ifreq *)data;
651     struct ppp_stats *psp;
652 #ifdef  PPP_COMPRESS
653     struct ppp_comp_stats *pcp;
654 #endif
655     int s = splimp(), error = 0;
656
657     switch (cmd) {
658     case SIOCSIFFLAGS:
659         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
660             ifp->if_flags &= ~IFF_UP;
661         break;
662
663     case SIOCSIFADDR:
664     case SIOCAIFADDR:
665         switch(ifa->ifa_addr->sa_family) {
666 #ifdef INET
667         case AF_INET:
668             break;
669 #endif
670 #ifdef IPX
671         case AF_IPX:
672             break;
673 #endif
674         default:
675             error = EAFNOSUPPORT;
676             break;
677         }
678         break;
679
680     case SIOCSIFDSTADDR:
681         switch(ifa->ifa_addr->sa_family) {
682 #ifdef INET
683         case AF_INET:
684             break;
685 #endif
686 #ifdef IPX
687         case AF_IPX:
688             break;
689 #endif
690         default:
691             error = EAFNOSUPPORT;
692             break;
693         }
694         break;
695
696     case SIOCSIFMTU:
697         if ((error = suser(td)) != 0)
698             break;
699         if (ifr->ifr_mtu > PPP_MAXMTU)
700             error = EINVAL;
701         else {
702             PPP2IFP(sc)->if_mtu = ifr->ifr_mtu;
703             if (sc->sc_setmtu)
704                     (*sc->sc_setmtu)(sc);
705         }
706         break;
707
708     case SIOCGIFMTU:
709         ifr->ifr_mtu = PPP2IFP(sc)->if_mtu;
710         break;
711
712     case SIOCADDMULTI:
713     case SIOCDELMULTI:
714         if (ifr == 0) {
715             error = EAFNOSUPPORT;
716             break;
717         }
718         switch(ifr->ifr_addr.sa_family) {
719 #ifdef INET
720         case AF_INET:
721             break;
722 #endif
723         default:
724             error = EAFNOSUPPORT;
725             break;
726         }
727         break;
728
729     case SIOCGPPPSTATS:
730         psp = &((struct ifpppstatsreq *) data)->stats;
731         bzero(psp, sizeof(*psp));
732         psp->p = sc->sc_stats;
733 #if defined(VJC) && !defined(SL_NO_STATS)
734         if (sc->sc_comp) {
735             psp->vj.vjs_packets = sc->sc_comp->sls_packets;
736             psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
737             psp->vj.vjs_searches = sc->sc_comp->sls_searches;
738             psp->vj.vjs_misses = sc->sc_comp->sls_misses;
739             psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
740             psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
741             psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
742             psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
743         }
744 #endif /* VJC */
745         break;
746
747 #ifdef PPP_COMPRESS
748     case SIOCGPPPCSTATS:
749         pcp = &((struct ifpppcstatsreq *) data)->stats;
750         bzero(pcp, sizeof(*pcp));
751         if (sc->sc_xc_state != NULL)
752             (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
753         if (sc->sc_rc_state != NULL)
754             (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
755         break;
756 #endif /* PPP_COMPRESS */
757
758     default:
759         error = ENOTTY;
760     }
761     splx(s);
762     return (error);
763 }
764
765 /*
766  * Queue a packet.  Start transmission if not active.
767  * Packet is placed in Information field of PPP frame.
768  * Called at splnet as the if->if_output handler.
769  * Called at splnet from pppwrite().
770  */
771 int
772 pppoutput(ifp, m0, dst, rtp)
773     struct ifnet *ifp;
774     struct mbuf *m0;
775     struct sockaddr *dst;
776     struct rtentry *rtp;
777 {
778     register struct ppp_softc *sc = ifp->if_softc;
779     int protocol, address, control;
780     u_char *cp;
781     int s, error;
782     struct ip *ip;
783     struct ifqueue *ifq;
784     enum NPmode mode;
785     int len;
786
787 #ifdef MAC
788     error = mac_check_ifnet_transmit(ifp, m0);
789     if (error)
790         goto bad;
791 #endif
792
793     if (sc->sc_devp == NULL || (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0
794         || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
795         error = ENETDOWN;       /* sort of */
796         goto bad;
797     }
798
799     /*
800      * Compute PPP header.
801      */
802     m0->m_flags &= ~M_HIGHPRI;
803     switch (dst->sa_family) {
804 #ifdef INET
805     case AF_INET:
806         address = PPP_ALLSTATIONS;
807         control = PPP_UI;
808         protocol = PPP_IP;
809         mode = sc->sc_npmode[NP_IP];
810
811         /*
812          * If this packet has the "low delay" bit set in the IP header,
813          * put it on the fastq instead.
814          */
815         ip = mtod(m0, struct ip *);
816         if (ip->ip_tos & IPTOS_LOWDELAY)
817             m0->m_flags |= M_HIGHPRI;
818         break;
819 #endif
820 #ifdef IPX
821     case AF_IPX:
822         /*
823          * This is pretty bogus.. We dont have an ipxcp module in pppd
824          * yet to configure the link parameters.  Sigh. I guess a
825          * manual ifconfig would do....  -Peter
826          */
827         address = PPP_ALLSTATIONS;
828         control = PPP_UI;
829         protocol = PPP_IPX;
830         mode = NPMODE_PASS;
831         break;
832 #endif
833     case AF_UNSPEC:
834         address = PPP_ADDRESS(dst->sa_data);
835         control = PPP_CONTROL(dst->sa_data);
836         protocol = PPP_PROTOCOL(dst->sa_data);
837         mode = NPMODE_PASS;
838         break;
839     default:
840         if_printf(ifp, "af%d not supported\n", dst->sa_family);
841         error = EAFNOSUPPORT;
842         goto bad;
843     }
844
845     /*
846      * Drop this packet, or return an error, if necessary.
847      */
848     if (mode == NPMODE_ERROR) {
849         error = ENETDOWN;
850         goto bad;
851     }
852     if (mode == NPMODE_DROP) {
853         error = 0;
854         goto bad;
855     }
856
857     /*
858      * Add PPP header.  If no space in first mbuf, allocate another.
859      * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
860      */
861     if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
862         m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
863         if (m0 == 0) {
864             error = ENOBUFS;
865             goto bad;
866         }
867         m0->m_len = 0;
868     } else
869         m0->m_data -= PPP_HDRLEN;
870
871     cp = mtod(m0, u_char *);
872     *cp++ = address;
873     *cp++ = control;
874     *cp++ = protocol >> 8;
875     *cp++ = protocol & 0xff;
876     m0->m_len += PPP_HDRLEN;
877
878     len = m_length(m0, NULL);
879
880     if (sc->sc_flags & SC_LOG_OUTPKT) {
881         printf("%s output: ", ifp->if_xname);
882         pppdumpm(m0);
883     }
884
885     if ((protocol & 0x8000) == 0) {
886 #ifdef PPP_FILTER
887         /*
888          * Apply the pass and active filters to the packet,
889          * but only if it is a data packet.
890          */
891         *mtod(m0, u_char *) = 1;        /* indicates outbound */
892         if (sc->sc_pass_filt.bf_insns != 0
893             && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
894                           len, 0) == 0) {
895             error = 0;          /* drop this packet */
896             goto bad;
897         }
898
899         /*
900          * Update the time we sent the most recent packet.
901          */
902         if (sc->sc_active_filt.bf_insns == 0
903             || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
904             sc->sc_last_sent = time_uptime;
905
906         *mtod(m0, u_char *) = address;
907 #else
908         /*
909          * Update the time we sent the most recent data packet.
910          */
911         sc->sc_last_sent = time_uptime;
912 #endif /* PPP_FILTER */
913     }
914
915     /*
916      * See if bpf wants to look at the packet.
917      */
918     BPF_MTAP(ifp, m0);
919
920     /*
921      * Put the packet on the appropriate queue.
922      */
923     s = splsoftnet();   /* redundant */
924     if (mode == NPMODE_QUEUE) {
925         /* XXX we should limit the number of packets on this queue */
926         *sc->sc_npqtail = m0;
927         m0->m_nextpkt = NULL;
928         sc->sc_npqtail = &m0->m_nextpkt;
929     } else {
930         /* fastq and if_snd are emptied at spl[soft]net now */
931         ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq:
932             (struct ifqueue *)&ifp->if_snd;
933         IF_LOCK(ifq);
934         if (_IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
935             _IF_DROP(ifq);
936             IF_UNLOCK(ifq);
937             PPP2IFP(sc)->if_oerrors++;
938             sc->sc_stats.ppp_oerrors++;
939             error = ENOBUFS;
940             goto bad;
941         }
942         _IF_ENQUEUE(ifq, m0);
943         IF_UNLOCK(ifq);
944         (*sc->sc_start)(sc);
945     }
946     getmicrotime(&ifp->if_lastchange);
947     ifp->if_opackets++;
948     ifp->if_obytes += len;
949
950     splx(s);
951     return (0);
952
953 bad:
954     m_freem(m0);
955     return (error);
956 }
957
958 /*
959  * After a change in the NPmode for some NP, move packets from the
960  * npqueue to the send queue or the fast queue as appropriate.
961  * Should be called at spl[soft]net.
962  */
963 static void
964 ppp_requeue(sc)
965     struct ppp_softc *sc;
966 {
967     struct mbuf *m, **mpp;
968     struct ifqueue *ifq;
969     enum NPmode mode;
970
971     for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
972         switch (PPP_PROTOCOL(mtod(m, u_char *))) {
973         case PPP_IP:
974             mode = sc->sc_npmode[NP_IP];
975             break;
976         default:
977             mode = NPMODE_PASS;
978         }
979
980         switch (mode) {
981         case NPMODE_PASS:
982             /*
983              * This packet can now go on one of the queues to be sent.
984              */
985             *mpp = m->m_nextpkt;
986             m->m_nextpkt = NULL;
987             ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq:
988                 (struct ifqueue *)&PPP2IFP(sc)->if_snd;
989             if (! IF_HANDOFF(ifq, m, NULL)) {
990                 PPP2IFP(sc)->if_oerrors++;
991                 sc->sc_stats.ppp_oerrors++;
992             }
993             break;
994
995         case NPMODE_DROP:
996         case NPMODE_ERROR:
997             *mpp = m->m_nextpkt;
998             m_freem(m);
999             break;
1000
1001         case NPMODE_QUEUE:
1002             mpp = &m->m_nextpkt;
1003             break;
1004         }
1005     }
1006     sc->sc_npqtail = mpp;
1007 }
1008
1009 /*
1010  * Transmitter has finished outputting some stuff;
1011  * remember to call sc->sc_start later at splsoftnet.
1012  */
1013 void
1014 ppp_restart(sc)
1015     struct ppp_softc *sc;
1016 {
1017     int s = splimp();
1018
1019     sc->sc_flags &= ~SC_TBUSY;
1020     schednetisr(NETISR_PPP);
1021     splx(s);
1022 }
1023
1024
1025 /*
1026  * Get a packet to send.  This procedure is intended to be called at
1027  * splsoftnet, since it may involve time-consuming operations such as
1028  * applying VJ compression, packet compression, address/control and/or
1029  * protocol field compression to the packet.
1030  */
1031 struct mbuf *
1032 ppp_dequeue(sc)
1033     struct ppp_softc *sc;
1034 {
1035     struct mbuf *m, *mp;
1036     u_char *cp;
1037     int address, control, protocol;
1038
1039     /*
1040      * Grab a packet to send: first try the fast queue, then the
1041      * normal queue.
1042      */
1043     IF_DEQUEUE(&sc->sc_fastq, m);
1044     if (m == NULL)
1045         IF_DEQUEUE(&PPP2IFP(sc)->if_snd, m);
1046     if (m == NULL)
1047         return NULL;
1048
1049     ++sc->sc_stats.ppp_opackets;
1050
1051     /*
1052      * Extract the ppp header of the new packet.
1053      * The ppp header will be in one mbuf.
1054      */
1055     cp = mtod(m, u_char *);
1056     address = PPP_ADDRESS(cp);
1057     control = PPP_CONTROL(cp);
1058     protocol = PPP_PROTOCOL(cp);
1059
1060     switch (protocol) {
1061     case PPP_IP:
1062 #ifdef VJC
1063         /*
1064          * If the packet is a TCP/IP packet, see if we can compress it.
1065          */
1066         if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
1067             struct ip *ip;
1068             int type;
1069
1070             mp = m;
1071             ip = (struct ip *) (cp + PPP_HDRLEN);
1072             if (mp->m_len <= PPP_HDRLEN) {
1073                 mp = mp->m_next;
1074                 if (mp == NULL)
1075                     break;
1076                 ip = mtod(mp, struct ip *);
1077             }
1078             /* this code assumes the IP/TCP header is in one non-shared mbuf */
1079             if (ip->ip_p == IPPROTO_TCP) {
1080                 type = sl_compress_tcp(mp, ip, sc->sc_comp,
1081                                        !(sc->sc_flags & SC_NO_TCP_CCID));
1082                 switch (type) {
1083                 case TYPE_UNCOMPRESSED_TCP:
1084                     protocol = PPP_VJC_UNCOMP;
1085                     break;
1086                 case TYPE_COMPRESSED_TCP:
1087                     protocol = PPP_VJC_COMP;
1088                     cp = mtod(m, u_char *);
1089                     cp[0] = address;    /* header has moved */
1090                     cp[1] = control;
1091                     cp[2] = 0;
1092                     break;
1093                 }
1094                 cp[3] = protocol;       /* update protocol in PPP header */
1095             }
1096         }
1097 #endif  /* VJC */
1098         break;
1099
1100 #ifdef PPP_COMPRESS
1101     case PPP_CCP:
1102         ppp_ccp(sc, m, 0);
1103         break;
1104 #endif  /* PPP_COMPRESS */
1105     }
1106
1107 #ifdef PPP_COMPRESS
1108     if (protocol != PPP_LCP && protocol != PPP_CCP
1109         && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1110         struct mbuf *mcomp = NULL;
1111         int slen, clen;
1112
1113         slen = m_length(m, NULL);
1114         clen = (*sc->sc_xcomp->compress)
1115             (sc->sc_xc_state, &mcomp, m, slen, PPP2IFP(sc)->if_mtu + PPP_HDRLEN);
1116         if (mcomp != NULL) {
1117             if (sc->sc_flags & SC_CCP_UP) {
1118                 /* Send the compressed packet instead of the original. */
1119                 m_freem(m);
1120                 m = mcomp;
1121                 cp = mtod(m, u_char *);
1122                 protocol = cp[3];
1123             } else {
1124                 /* Can't transmit compressed packets until CCP is up. */
1125                 m_freem(mcomp);
1126             }
1127         }
1128     }
1129 #endif  /* PPP_COMPRESS */
1130
1131     /*
1132      * Compress the address/control and protocol, if possible.
1133      */
1134     if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1135         control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1136         protocol != PPP_LCP) {
1137         /* can compress address/control */
1138         m->m_data += 2;
1139         m->m_len -= 2;
1140     }
1141     if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1142         /* can compress protocol */
1143         if (mtod(m, u_char *) == cp) {
1144             cp[2] = cp[1];      /* move address/control up */
1145             cp[1] = cp[0];
1146         }
1147         ++m->m_data;
1148         --m->m_len;
1149     }
1150
1151     return m;
1152 }
1153
1154 /*
1155  * Software interrupt routine, called at spl[soft]net.
1156  */
1157 static void
1158 pppintr()
1159 {
1160     struct ppp_softc *sc;
1161     int s;
1162     struct mbuf *m;
1163
1164     GIANT_REQUIRED;
1165
1166     PPP_LIST_LOCK();
1167     LIST_FOREACH(sc, &ppp_softc_list, sc_list) {
1168         s = splimp();
1169         if (!(sc->sc_flags & SC_TBUSY)
1170             && (PPP2IFP(sc)->if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
1171             sc->sc_flags |= SC_TBUSY;
1172             splx(s);
1173             (*sc->sc_start)(sc);
1174         } else
1175             splx(s);
1176         for (;;) {
1177             s = splimp();
1178             IF_DEQUEUE(&sc->sc_rawq, m);
1179             splx(s);
1180             if (m == NULL)
1181                 break;
1182 #ifdef MAC
1183             mac_create_mbuf_from_ifnet(PPP2IFP(sc), m);
1184 #endif
1185             ppp_inproc(sc, m);
1186         }
1187     }
1188     PPP_LIST_UNLOCK();
1189 }
1190
1191 #ifdef PPP_COMPRESS
1192 /*
1193  * Handle a CCP packet.  `rcvd' is 1 if the packet was received,
1194  * 0 if it is about to be transmitted.
1195  */
1196 static void
1197 ppp_ccp(sc, m, rcvd)
1198     struct ppp_softc *sc;
1199     struct mbuf *m;
1200     int rcvd;
1201 {
1202     u_char *dp, *ep;
1203     struct mbuf *mp;
1204     int slen, s;
1205
1206     /*
1207      * Get a pointer to the data after the PPP header.
1208      */
1209     if (m->m_len <= PPP_HDRLEN) {
1210         mp = m->m_next;
1211         if (mp == NULL)
1212             return;
1213         dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1214     } else {
1215         mp = m;
1216         dp = mtod(mp, u_char *) + PPP_HDRLEN;
1217     }
1218
1219     ep = mtod(mp, u_char *) + mp->m_len;
1220     if (dp + CCP_HDRLEN > ep)
1221         return;
1222     slen = CCP_LENGTH(dp);
1223     if (dp + slen > ep) {
1224         if (sc->sc_flags & SC_DEBUG)
1225             printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1226                    dp, slen, mtod(mp, u_char *), mp->m_len);
1227         return;
1228     }
1229
1230     switch (CCP_CODE(dp)) {
1231     case CCP_CONFREQ:
1232     case CCP_TERMREQ:
1233     case CCP_TERMACK:
1234         /* CCP must be going down - disable compression */
1235         if (sc->sc_flags & SC_CCP_UP) {
1236             s = splimp();
1237             sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1238             splx(s);
1239         }
1240         break;
1241
1242     case CCP_CONFACK:
1243         if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1244             && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1245             && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1246             if (!rcvd) {
1247                 /* we're agreeing to send compressed packets. */
1248                 if (sc->sc_xc_state != NULL
1249                     && (*sc->sc_xcomp->comp_init)
1250                         (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1251                          PPP2IFP(sc)->if_dunit, 0, sc->sc_flags & SC_DEBUG)) {
1252                     s = splimp();
1253                     sc->sc_flags |= SC_COMP_RUN;
1254                     splx(s);
1255                 }
1256             } else {
1257                 /* peer is agreeing to send compressed packets. */
1258                 if (sc->sc_rc_state != NULL
1259                     && (*sc->sc_rcomp->decomp_init)
1260                         (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1261                          PPP2IFP(sc)->if_dunit, 0, sc->sc_mru,
1262                          sc->sc_flags & SC_DEBUG)) {
1263                     s = splimp();
1264                     sc->sc_flags |= SC_DECOMP_RUN;
1265                     sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1266                     splx(s);
1267                 }
1268             }
1269         }
1270         break;
1271
1272     case CCP_RESETACK:
1273         if (sc->sc_flags & SC_CCP_UP) {
1274             if (!rcvd) {
1275                 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1276                     (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1277             } else {
1278                 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1279                     (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1280                     s = splimp();
1281                     sc->sc_flags &= ~SC_DC_ERROR;
1282                     splx(s);
1283                 }
1284             }
1285         }
1286         break;
1287     }
1288 }
1289
1290 /*
1291  * CCP is down; free (de)compressor state if necessary.
1292  */
1293 static void
1294 ppp_ccp_closed(sc)
1295     struct ppp_softc *sc;
1296 {
1297     if (sc->sc_xc_state) {
1298         (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1299         sc->sc_xc_state = NULL;
1300     }
1301     if (sc->sc_rc_state) {
1302         (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1303         sc->sc_rc_state = NULL;
1304     }
1305 }
1306 #endif /* PPP_COMPRESS */
1307
1308 /*
1309  * PPP packet input routine.
1310  * The caller has checked and removed the FCS and has inserted
1311  * the address/control bytes and the protocol high byte if they
1312  * were omitted.
1313  */
1314 void
1315 ppppktin(sc, m, lost)
1316     struct ppp_softc *sc;
1317     struct mbuf *m;
1318     int lost;
1319 {
1320     int s = splimp();
1321
1322     if (lost)
1323         m->m_flags |= M_ERRMARK;
1324     IF_ENQUEUE(&sc->sc_rawq, m);
1325     schednetisr(NETISR_PPP);
1326     splx(s);
1327 }
1328
1329 /*
1330  * Process a received PPP packet, doing decompression as necessary.
1331  * Should be called at splsoftnet.
1332  */
1333 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1334                          TYPE_UNCOMPRESSED_TCP)
1335
1336 static void
1337 ppp_inproc(sc, m)
1338     struct ppp_softc *sc;
1339     struct mbuf *m;
1340 {
1341     struct ifnet *ifp = PPP2IFP(sc);
1342     int isr;
1343     int s, ilen = 0, xlen, proto, rv;
1344     u_char *cp, adrs, ctrl;
1345     struct mbuf *mp, *dmp = NULL;
1346     u_char *iphdr;
1347     u_int hlen;
1348
1349     sc->sc_stats.ppp_ipackets++;
1350
1351     if (sc->sc_flags & SC_LOG_INPKT) {
1352         ilen = m_length(m, NULL);
1353         if_printf(ifp, "got %d bytes\n", ilen);
1354         pppdumpm(m);
1355     }
1356
1357     cp = mtod(m, u_char *);
1358     adrs = PPP_ADDRESS(cp);
1359     ctrl = PPP_CONTROL(cp);
1360     proto = PPP_PROTOCOL(cp);
1361
1362     if (m->m_flags & M_ERRMARK) {
1363         m->m_flags &= ~M_ERRMARK;
1364         s = splimp();
1365         sc->sc_flags |= SC_VJ_RESET;
1366         splx(s);
1367     }
1368
1369 #ifdef PPP_COMPRESS
1370     /*
1371      * Decompress this packet if necessary, update the receiver's
1372      * dictionary, or take appropriate action on a CCP packet.
1373      */
1374     if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1375         && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1376         /* decompress this packet */
1377         rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1378         if (rv == DECOMP_OK) {
1379             m_freem(m);
1380             if (dmp == NULL) {
1381                 /* no error, but no decompressed packet produced */
1382                 return;
1383             }
1384             m = dmp;
1385             cp = mtod(m, u_char *);
1386             proto = PPP_PROTOCOL(cp);
1387
1388         } else {
1389             /*
1390              * An error has occurred in decompression.
1391              * Pass the compressed packet up to pppd, which may take
1392              * CCP down or issue a Reset-Req.
1393              */
1394             if (sc->sc_flags & SC_DEBUG)
1395                 if_printf(ifp, "decompress failed %d\n", rv);
1396             s = splimp();
1397             sc->sc_flags |= SC_VJ_RESET;
1398             if (rv == DECOMP_ERROR)
1399                 sc->sc_flags |= SC_DC_ERROR;
1400             else
1401                 sc->sc_flags |= SC_DC_FERROR;
1402             splx(s);
1403         }
1404
1405     } else {
1406         if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1407             (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1408         }
1409         if (proto == PPP_CCP) {
1410             ppp_ccp(sc, m, 1);
1411         }
1412     }
1413 #endif
1414
1415     ilen = m_length(m, NULL);
1416
1417 #ifdef VJC
1418     if (sc->sc_flags & SC_VJ_RESET) {
1419         /*
1420          * If we've missed a packet, we must toss subsequent compressed
1421          * packets which don't have an explicit connection ID.
1422          */
1423         if (sc->sc_comp)
1424             sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1425         s = splimp();
1426         sc->sc_flags &= ~SC_VJ_RESET;
1427         splx(s);
1428     }
1429
1430     /*
1431      * See if we have a VJ-compressed packet to uncompress.
1432      */
1433     if (proto == PPP_VJC_COMP) {
1434         if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1435             goto bad;
1436
1437         xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1438                                       ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1439                                       sc->sc_comp, &iphdr, &hlen);
1440
1441         if (xlen <= 0) {
1442             if (sc->sc_flags & SC_DEBUG)
1443                 if_printf(ifp, "VJ uncompress failed on type comp\n");
1444             goto bad;
1445         }
1446
1447         /* Copy the PPP and IP headers into a new mbuf. */
1448         MGETHDR(mp, M_DONTWAIT, MT_DATA);
1449         if (mp == NULL)
1450             goto bad;
1451         mp->m_len = 0;
1452         mp->m_next = NULL;
1453         if (hlen + PPP_HDRLEN > MHLEN) {
1454             MCLGET(mp, M_DONTWAIT);
1455             if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1456                 m_freem(mp);
1457                 goto bad;       /* lose if big headers and no clusters */
1458             }
1459         }
1460 #ifdef MAC
1461         mac_copy_mbuf(m, mp);
1462 #endif
1463         cp = mtod(mp, u_char *);
1464         cp[0] = adrs;
1465         cp[1] = ctrl;
1466         cp[2] = 0;
1467         cp[3] = PPP_IP;
1468         proto = PPP_IP;
1469         bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1470         mp->m_len = hlen + PPP_HDRLEN;
1471
1472         /*
1473          * Trim the PPP and VJ headers off the old mbuf
1474          * and stick the new and old mbufs together.
1475          */
1476         m->m_data += PPP_HDRLEN + xlen;
1477         m->m_len -= PPP_HDRLEN + xlen;
1478         if (m->m_len <= M_TRAILINGSPACE(mp)) {
1479             bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1480             mp->m_len += m->m_len;
1481             mp->m_next = m_free(m);
1482         } else {
1483             mp->m_next = m;
1484         }
1485         m = mp;
1486         ilen += hlen - xlen;
1487
1488     } else if (proto == PPP_VJC_UNCOMP) {
1489         if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1490             goto bad;
1491
1492         xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1493                                       ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1494                                       sc->sc_comp, &iphdr, &hlen);
1495
1496         if (xlen < 0) {
1497             if (sc->sc_flags & SC_DEBUG)
1498                 if_printf(ifp, "VJ uncompress failed on type uncomp\n");
1499             goto bad;
1500         }
1501
1502         proto = PPP_IP;
1503         cp[3] = PPP_IP;
1504     }
1505 #endif /* VJC */
1506
1507     /*
1508      * If the packet will fit in a header mbuf, don't waste a
1509      * whole cluster on it.
1510      */
1511     if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1512         MGETHDR(mp, M_DONTWAIT, MT_DATA);
1513         if (mp != NULL) {
1514 #ifdef MAC
1515             mac_copy_mbuf(m, mp);
1516 #endif
1517             m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1518             m_freem(m);
1519             m = mp;
1520             m->m_len = ilen;
1521         }
1522     }
1523     m->m_pkthdr.len = ilen;
1524     m->m_pkthdr.rcvif = ifp;
1525
1526     if ((proto & 0x8000) == 0) {
1527 #ifdef PPP_FILTER
1528         /*
1529          * See whether we want to pass this packet, and
1530          * if it counts as link activity.
1531          */
1532         adrs = *mtod(m, u_char *);      /* save address field */
1533         *mtod(m, u_char *) = 0;         /* indicate inbound */
1534         if (sc->sc_pass_filt.bf_insns != 0
1535             && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1536                           ilen, 0) == 0) {
1537             /* drop this packet */
1538             m_freem(m);
1539             return;
1540         }
1541         if (sc->sc_active_filt.bf_insns == 0
1542             || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1543             sc->sc_last_recv = time_uptime;
1544
1545         *mtod(m, u_char *) = adrs;
1546 #else
1547         /*
1548          * Record the time that we received this packet.
1549          */
1550         sc->sc_last_recv = time_uptime;
1551 #endif /* PPP_FILTER */
1552     }
1553
1554     /* See if bpf wants to look at the packet. */
1555     BPF_MTAP(PPP2IFP(sc), m);
1556
1557     isr = -1;
1558     switch (proto) {
1559 #ifdef INET
1560     case PPP_IP:
1561         /*
1562          * IP packet - take off the ppp header and pass it up to IP.
1563          */
1564         if ((ifp->if_flags & IFF_UP) == 0
1565             || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1566             /* interface is down - drop the packet. */
1567             m_freem(m);
1568             return;
1569         }
1570         m->m_pkthdr.len -= PPP_HDRLEN;
1571         m->m_data += PPP_HDRLEN;
1572         m->m_len -= PPP_HDRLEN;
1573         if ((m = ip_fastforward(m)) == NULL)
1574             return;
1575         isr = NETISR_IP;
1576         break;
1577 #endif
1578 #ifdef IPX
1579     case PPP_IPX:
1580         /*
1581          * IPX packet - take off the ppp header and pass it up to IPX.
1582          */
1583         if ((PPP2IFP(sc)->if_flags & IFF_UP) == 0
1584             /* XXX: || sc->sc_npmode[NP_IPX] != NPMODE_PASS*/) {
1585             /* interface is down - drop the packet. */
1586             m_freem(m);
1587             return;
1588         }
1589         m->m_pkthdr.len -= PPP_HDRLEN;
1590         m->m_data += PPP_HDRLEN;
1591         m->m_len -= PPP_HDRLEN;
1592         isr = NETISR_IPX;
1593         sc->sc_last_recv = time_uptime; /* update time of last pkt rcvd */
1594         break;
1595 #endif
1596
1597     default:
1598         /*
1599          * Some other protocol - place on input queue for read().
1600          */
1601         break;
1602     }
1603
1604     if (isr == -1)
1605       rv = IF_HANDOFF(&sc->sc_inq, m, NULL);
1606     else
1607       rv = netisr_queue(isr, m);        /* (0) on success. */
1608     if ((isr == -1 && !rv) || (isr != -1 && rv)) {
1609         if (sc->sc_flags & SC_DEBUG)
1610             if_printf(ifp, "input queue full\n");
1611         ifp->if_iqdrops++;
1612         m = NULL;
1613         goto bad;
1614     }
1615     ifp->if_ipackets++;
1616     ifp->if_ibytes += ilen;
1617     getmicrotime(&ifp->if_lastchange);
1618
1619     if (isr == -1)
1620         (*sc->sc_ctlp)(sc);
1621
1622     return;
1623
1624  bad:
1625     if (m)
1626         m_freem(m);
1627     PPP2IFP(sc)->if_ierrors++;
1628     sc->sc_stats.ppp_ierrors++;
1629 }
1630
1631 #define MAX_DUMP_BYTES  128
1632
1633 static void
1634 pppdumpm(m0)
1635     struct mbuf *m0;
1636 {
1637     char buf[3*MAX_DUMP_BYTES+4];
1638     char *bp = buf;
1639     struct mbuf *m;
1640
1641     for (m = m0; m; m = m->m_next) {
1642         int l = m->m_len;
1643         u_char *rptr = (u_char *)m->m_data;
1644
1645         while (l--) {
1646             if (bp > buf + (sizeof(buf) - 4))
1647                 goto done;
1648             *bp++ = hex2ascii(*rptr >> 4);
1649             *bp++ = hex2ascii(*rptr++ & 0xf);
1650         }
1651
1652         if (m->m_next) {
1653             if (bp > buf + (sizeof(buf) - 3))
1654                 goto done;
1655             *bp++ = '|';
1656         } else
1657             *bp++ = ' ';
1658     }
1659 done:
1660     if (m)
1661         *bp++ = '>';
1662     *bp = 0;
1663     printf("%s\n", buf);
1664 }