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