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