]> git.ozlabs.org Git - ppp.git/blob - NeXT/if_ppp.c
check that VJ-unc header doesn't overflow buffer
[ppp.git] / NeXT / if_ppp.c
1 /*
2  * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
3  *
4  * Copyright (c) 1989 Carnegie Mellon University.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms are permitted
8  * provided that the above copyright notice and this paragraph are
9  * duplicated in all such forms and that any documentation,
10  * advertising materials, and other materials related to such
11  * distribution and use acknowledge that the software was developed
12  * by Carnegie Mellon University.  The name of the
13  * University may not be used to endorse or promote products derived
14  * from this software without specific prior written permission.
15  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18  *
19  * Drew D. Perkins
20  * Carnegie Mellon University
21  * 4910 Forbes Ave.
22  * Pittsburgh, PA 15213
23  * (412) 268-8576
24  * ddp@andrew.cmu.edu
25  *
26  * Based on:
27  *      @(#)if_sl.c     7.6.1.2 (Berkeley) 2/15/89
28  *
29  * Copyright (c) 1987 Regents of the University of California.
30  * All rights reserved.
31  *
32  * Redistribution and use in source and binary forms are permitted
33  * provided that the above copyright notice and this paragraph are
34  * duplicated in all such forms and that any documentation,
35  * advertising materials, and other materials related to such
36  * distribution and use acknowledge that the software was developed
37  * by the University of California, Berkeley.  The name of the
38  * University may not be used to endorse or promote products derived
39  * from this software without specific prior written permission.
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
41  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
42  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
43  *
44  * Serial Line interface
45  *
46  * Rick Adams
47  * Center for Seismic Studies
48  * 1300 N 17th Street, Suite 1450
49  * Arlington, Virginia 22209
50  * (703)276-7900
51  * rick@seismo.ARPA
52  * seismo!rick
53  *
54  * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
55  * Converted to 4.3BSD Beta by Chris Torek.
56  * Other changes made at Berkeley, based in part on code by Kirk Smith.
57  *
58  * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
59  * Added VJ tcp header compression; more unified ioctls
60  *
61  * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au).
62  * Cleaned up a lot of the mbuf-related code to fix bugs that
63  * caused system crashes and packet corruption.  Changed pppstart
64  * so that it doesn't just give up with a collision if the whole
65  * packet doesn't fit in the output ring buffer.
66  *
67  * Added priority queueing for interactive IP packets, following
68  * the model of if_sl.c, plus hooks for bpf.
69  * Paul Mackerras (paulus@cs.anu.edu.au).
70  *
71  * Rewritten for NextStep's funky kernel functions, I/O threads,
72  * and netbufs (instead of real mbufs).  Also, ifnets don't install
73  * into the kernel under NS as they do under BSD.  We have tried to
74  * make the code remain as similar to the NetBSD version without
75  * incurring too much hassle.  This code is the merge of 
76  * Philip Prindeville's <philipp@res.enst.fr>/Pete French's <pete@ohm.york.ac.uk>
77  * and Stephen Perkins'  <perkins@cps.msu.edu> independent ports.
78  *
79  */
80
81 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
82
83 #if !defined(lint)
84 static char sccsid[] = "$Revision: 1.1 $ ($Date: 1995/12/18 03:30:02 $)";
85 #endif /* not lint*/
86
87 #define KERNEL 1
88 #define KERNEL_FEATURES 1
89 #define INET 1
90
91 /* #include "ppp.h" */
92
93 #include <sys/param.h>
94 #include <sys/proc.h>
95 #include "netbuf.h"
96 #include <sys/socket.h>
97 #include <sys/conf.h>
98 #include <sys/time.h>
99 #include <sys/ioctl.h>
100 #include <sys/errno.h>
101 #include <kernserv/prototypes.h>
102 #if defined(m68k)
103 #import "spl.h"
104 #else
105 #include <driverkit/generalFuncs.h>
106 #import <kernserv/machine/spl.h>
107 #endif
108 #if defined(sparc) || defined(m68k)
109 #include <machine/psl.h>
110 #endif
111 #include <kernserv/kern_server_types.h>
112
113 #include <net/if.h>
114 #include <net/route.h>
115
116 #if INET
117 #include <netinet/in.h>
118 #include <netinet/in_systm.h>
119 #include <netinet/in_var.h>
120 #include <netinet/ip.h>
121 #endif
122
123
124 #include <net/ppp_defs.h>
125 #ifdef  VJC
126 #include <net/vjcompress.h>
127 #endif
128 #include <net/if_ppp.h>
129 #include "if_pppvar.h"
130
131 struct  ppp_softc ppp_softc[NUM_PPP];
132
133 #ifdef  PPP_COMPRESS
134 #define PACKETPTR       netbuf_t
135 #include <net/ppp-comp.h>
136 #endif
137
138 #ifdef NBPFILTER
139 #include <bpf/bpf.h>
140 /*
141  * We store the address of necessary BPF functions
142  * here.
143  */
144 struct bpf_fns fnarg;
145 #endif
146
147 /*
148  * The max number of netbuf_ts we wish to compress and cache for
149  * sending.
150  */
151 #define COMPRESS_CACHE_LEN 5
152
153 #include "inlines.h"
154
155 /*
156  * Necessary to avoid redefinition warnings or bogus type casts later.
157  */
158 int     pppoutput __P((netif_t ifp, netbuf_t m, void *arg));
159 int     pppsioctl __P((netif_t ifp, int cmd, caddr_t data));
160 int     pppcontrol __P((netif_t ifp, const char *cmd, void *data));
161 void    pppintr_comp __P((void *arg));
162 void    pppintr_decomp __P((void *arg));
163 void    pppfillfreeq __P((void *arg));
164 void    pppgetm __P((register struct ppp_softc *sc));
165
166 static void     ppp_requeue __P((struct ppp_softc *));
167 static void     ppp_outpkt __P((struct ppp_softc *));
168 static void     ppp_ccp __P((struct ppp_softc *, netbuf_t, int rcvd));
169 static void     ppp_ccp_closed __P((struct ppp_softc *));
170 static void     ppp_inproc __P((struct ppp_softc *, netbuf_t));
171 static void     pppdumpm __P((netbuf_t));
172
173 extern int      install_ppp_ld __P((void));
174 extern int      tty_ld_remove __P((int));
175
176
177 /*
178  * We steal two bits in the mbuf m_flags, to mark high-priority packets
179  * for output, and received packets following lost/corrupted packets.
180  */
181 #define M_HIGHPRI       0x2000  /* output packet for sc_fastq */
182 #define M_ERRMARK       0x4000  /* steal a bit in mbuf m_flags */
183
184 /*
185  * The following disgusting hack gets around the problem that IP TOS
186  * can't be set yet.  We want to put "interactive" traffic on a high
187  * priority queue.  To decide if traffic is interactive, we check that
188  * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
189  */
190 static u_short interactive_ports[8] = {
191         0,      513,    0,      0,
192         0,      21,     0,      23,
193 };
194
195 enum { QFREE, QRAW, QFAST, QSLOW, QIN, QNP, QCACHE };
196
197 static struct qparms qparms[] = {
198         {20, 40, 50, "free"},           /* freeq */
199         {5, 20, 25, "fast"},            /* rawq */
200         {5, 20, 25, "slow"},            /* fastq */
201         {5, 20, 25, "in"},              /* slowq */
202         {5, 20, 25, "out"},             /* inq */
203         {5, 20, 25, "np"},              /* npq */
204         {0, COMPRESS_CACHE_LEN, COMPRESS_CACHE_LEN, "cache"}    /* cache */
205 };
206
207 #define INTERACTIVE(p)  (interactive_ports[(p) & 7] == (p))
208
209 #ifndef IPTOS_LOWDELAY
210 #define IPTOS_LOWDELAY  0x10
211 #endif
212
213 #ifdef PPP_COMPRESS
214 /*
215  * List of compressors we know about.
216  * We leave some space so maybe we can modload compressors.
217  */
218
219 extern struct compressor ppp_bsd_compress;
220
221 struct compressor *ppp_compressors[8] = {
222 #if DO_BSD_COMPRESS
223     &ppp_bsd_compress,
224 #endif
225     NULL
226 };
227 #endif /* PPP_COMPRESS */
228
229 /* yeah, we sometimes have to change the MTU after having created the
230  * device.  Let's hope this doesn't break anything!!!
231  */
232 #define if_mtu_set(ifn,mtu)  (((struct ifnet *)ifn)->if_mtu = mtu)
233
234 extern int ipforwarding;
235 extern int ipsendredirects;
236
237 kern_server_t instance;
238
239 /*
240  * Sigh.  Should be defined in <net/if.h> but isn't...
241  */
242 union ifr_ifru {
243         short   ifru_flags;
244         short   ifru_mtu;
245         u_long  ifru_asyncmap;
246         int     ifru_metric;
247         caddr_t ifru_data;
248 };
249
250
251 netbuf_t
252 pppgetbuf(netif_t ifp)
253 {
254     netbuf_t nb;
255
256     int len = MAX(if_mtu(ifp), PPP_MTU) + PPP_HDRLEN + PPP_FCSLEN;
257     nb = ppp_nb_alloc(len);
258     if (nb != NULL)
259       {
260         ppp_nb_shrink_top(nb, PPP_HDRLEN);
261         nb_shrink_bot(nb, PPP_FCSLEN);          /* grown by pppstart() */
262       }
263     return nb;
264 }
265
266 /*
267  * Called from boot code to establish ppp interfaces.
268  */
269 int
270 pppattach()
271 {
272     register struct ppp_softc *sc;
273     register int i = 0;
274
275     IOLog("\nPPP version 2.2 released for NS 3.2 and 3.3\n");
276     IOLog("LKS: %s\n", sccsid);
277     IOLog("by  Stephen Perkins, Philip Prindeville, and Pete French\n");
278
279     IOLog("Installing PPP on Line Discipline %d\n", PPPDISC);
280     if (install_ppp_ld() < 0) {
281         IOLog("ppp: Could not install line discipline\n");
282         return 0;
283     }
284     
285
286     IOLog("Installing interfaces:\n");
287     for (sc = ppp_softc; i < NUM_PPP; sc++, i++) {
288         sc->sc_if = if_attach(NULL, NULL, pppoutput, 
289                               pppgetbuf, pppcontrol, "ppp", i, "Serial line PPP", 
290                               PPP_MTU, IFF_POINTOPOINT, NETIFCLASS_VIRTUAL, (void *) sc);
291         IOLog("     Initializing ppp%d\n", i);
292         nbq_init(&sc->sc_freeq, &qparms[QFREE]);
293         nbq_init(&sc->sc_rawq, &qparms[QRAW]);
294         nbq_init(&sc->sc_fastq, &qparms[QFAST]);
295         nbq_init(&sc->sc_slowq, &qparms[QSLOW]);
296         nbq_init(&sc->sc_inq, &qparms[QIN]);
297         nbq_init(&sc->sc_npq, &qparms[QNP]);
298         nbq_init(&sc->sc_compq, &qparms[QCACHE]);
299         IOLog("     ppp%d successfully attached.\n", i);
300 #ifdef NBPFILTER
301         bpfattach((caddr_t *) &sc->sc_bpf, sc->sc_if, DLT_PPP, PPP_HDRLEN);
302 #endif
303     }
304
305     ipforwarding = 1;
306     ipsendredirects = 1;
307     IOLog("PPP-2.2 Successfully Installed.\n\n");
308     return 1;
309 }
310
311 int
312 pppdetach()
313 {
314     struct ppp_softc *sc;
315     int i;
316
317     IOLog("Removing PPP on Line Discipline %d\n", PPPDISC);
318     if (!tty_ld_remove(PPPDISC))
319         IOLog("ppp: Could not remove line discipline\n");
320
321     IOLog("Removing interfaces:\n");
322     for (sc = ppp_softc, i = 0; i < NUM_PPP; sc++, i++) {
323         nbq_free(&sc->sc_freeq);
324         nbq_free(&sc->sc_rawq);
325         nbq_free(&sc->sc_fastq);
326         nbq_free(&sc->sc_slowq);
327         nbq_free(&sc->sc_inq);
328         nbq_free(&sc->sc_npq);
329         nbq_free(&sc->sc_compq);
330         if_detach(sc->sc_if);
331         /* no idea why we need this, but... */
332         bzero(sc->sc_if, sizeof(netif_t));
333         IOLog("     ppp%d successfully detached.\n", i);
334     }
335     IOLog("PPP-2.2 Successfully Removed.\n\n");
336     return 0;
337 }
338
339 /*
340  * Allocate a ppp interface unit and initialize it.
341  */
342 struct ppp_softc *
343 pppalloc(pid)
344     pid_t pid;
345 {
346     int nppp, i;
347     struct ppp_softc *sc;
348
349     for (nppp = 0, sc = ppp_softc; nppp < NUM_PPP; nppp++, sc++)
350         if (sc->sc_xfer == pid) {
351             IOLogDbg("ppp%d: alloc'ing unit %d to proc %d\n", nppp, nppp, pid);
352             sc->sc_xfer = 0;
353             return sc;
354         }
355     for (nppp = 0, sc = ppp_softc; nppp < NUM_PPP; nppp++, sc++)
356         if (sc->sc_devp == NULL)
357             break;
358     if (nppp >= NUM_PPP)
359         return NULL;
360
361     sc->sc_flags = 0;
362     sc->sc_mru = PPP_MRU;
363     sc->sc_relinq = NULL;
364 #ifdef VJC
365     vj_compress_init(&sc->sc_comp, -1);
366 #endif
367 #ifdef PPP_COMPRESS
368     sc->sc_xc_state = NULL;
369     sc->sc_rc_state = NULL;
370 #endif /* PPP_COMPRESS */
371     for (i = 0; i < NUM_NP; ++i)
372         sc->sc_npmode[i] = NPMODE_ERROR;
373     /* XXX - I'm not sure why the npqueue was zapped here... */
374     sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
375     sc->sc_compsched = 0;
376     sc->sc_decompsched = 0;
377
378 #if 0
379     /*
380      * Clear PPP stats information for the new session
381      */
382     sc->sc_bytessent = 0;
383     sc->sc_bytesrcvd = 0;
384     if_opackets_set(sc->sc_if, 0);
385     if_ipackets_set(sc->sc_if, 0);
386     if_oerrors_set(sc->sc_if, 0);
387     if_ierrors_set(sc->sc_if, 0);
388 #endif
389
390     /*
391      * XXX -- We need to get packets here, and we don't care if we do block...
392      * We do this after we set the sc_mru.
393      */
394     pppfillfreeq((void *) sc);
395
396     return sc;
397 }
398
399 /*
400  * Deallocate a ppp unit.  Must be called at splnet or higher.
401  */
402 void
403 pppdealloc(sc)
404     struct ppp_softc *sc;
405 {
406
407     if_flags_set(sc->sc_if, if_flags(sc->sc_if) & ~(IFF_UP|IFF_RUNNING));
408     sc->sc_devp = NULL;
409     sc->sc_xfer = 0;
410     nbq_flush(&sc->sc_freeq);
411     nbq_flush(&sc->sc_rawq);
412     nbq_flush(&sc->sc_inq);
413     nbq_flush(&sc->sc_fastq);
414     nbq_flush(&sc->sc_slowq);
415     nbq_flush(&sc->sc_npq);
416     nbq_flush(&sc->sc_compq);
417 #ifdef PPP_COMPRESS
418     ppp_ccp_closed(sc);
419     sc->sc_xc_state = NULL;
420     sc->sc_rc_state = NULL;
421 #endif /* PPP_COMPRESS */
422
423
424 }
425
426 /*
427  * Ioctl routine for generic ppp devices.
428  */
429 int
430 pppioctl(sc, cmd, data, flag)
431     struct ppp_softc *sc;
432     void *data;
433     u_long cmd;
434     int flag;
435 {
436     struct proc *p = curproc;
437     int s, error, flags, mru, nb, npx, oldflags;
438     struct ppp_option_data *odp;
439     struct compressor **cp;
440     struct npioctl *npi;
441     time_t t;
442 #ifdef  PPP_COMPRESS
443     u_char ccp_option[CCP_MAX_OPTION_LENGTH];
444 #endif
445     netbuf_t m;
446 #ifdef  HAS_BROKEN_TIOCSPGRP
447     struct tty *tp = sc->sc_devp;
448 #endif
449
450     switch (cmd) {
451     case FIONREAD:
452         s = splimp();           /* paranoid; splnet probably ok */
453         if ((m = nbq_peek(&sc->sc_inq)) != NULL)
454             *(int *)data = nb_size(m);
455         else
456             *(int *)data = 0;
457         splx(s);
458         break;
459
460     case PPPIOCGUNIT:
461         *(int *)data = if_unit(sc->sc_if);
462 #if 0
463         IOLogDbg("ppp%d: unit gets %d\n", if_unit(sc->sc_if), *(int *)data);
464 #endif
465         break;
466
467      case PPPIOCGFLAGS:
468         *(u_int *)data = sc->sc_flags;
469 #if 0
470         IOLogDbg("ppp%d: flags gets 0x%x\n", if_unit(sc->sc_if), *(u_int *)data);
471 #endif
472         break;
473
474     case PPPIOCSFLAGS:
475         if (! suser())
476             return EPERM;
477         flags = *(int *)data & SC_MASK;
478         s = splnet();
479 #ifdef PPP_COMPRESS
480         if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
481             ppp_ccp_closed(sc);
482 #endif
483         splimp();
484         oldflags = sc->sc_flags;
485         sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
486         splx(s);
487 #if 0
488         IOLogDbg("ppp%d: data 0x%x, flags: old 0x%x new 0x%x\n",
489                  if_unit(sc->sc_if), flags, oldflags, sc->sc_flags);
490 #endif
491         break;
492
493     case PPPIOCSMRU:
494         if (! suser())
495             return EPERM;
496         mru = *(int *)data;
497 #if 0
498         IOLogDbg("ppp%d: setting mru %d\n", if_unit(sc->sc_if), mru);
499 #endif
500         if (mru >= PPP_MRU && mru <= PPP_MAXMRU) {
501
502             /* To make sure we handle the received packet
503              * correctly, we do two things.  First, we
504              * empty out the free_q.  We then remove
505              * the current input buffer, set the input length
506              * to zero, and set the flush flag.
507              */
508             s = splimp();
509             nbq_flush(&sc->sc_freeq);   /* get rid of old buffers */
510             sc->sc_mru = mru;
511             if (sc->sc_m){
512               nb_free(sc->sc_m);
513               sc->sc_m = NULL;
514               if (sc->sc_ilen != 0)
515                 sc->sc_flags |= SC_FLUSH;
516             }
517             sc->sc_ilen = 0;
518             splx(s);
519             pppfillfreeq((void *) sc);  /* and make a queue of new ones */
520             pppgetm(sc);
521         }
522         break;
523
524     case PPPIOCGMRU:
525         *(int *)data = sc->sc_mru;
526 #if 0
527         IOLogDbg("ppp%d: mru gets 0x%x\n", if_unit(sc->sc_if), *(int *)data);
528 #endif
529         break;
530
531 #ifdef  VJC
532     case PPPIOCSMAXCID:
533         if (! suser())
534             return EPERM;
535 #if 0
536         IOLogDbg("ppp%d: setting max cid %d\n", if_unit(sc->sc_if), *(int *)data);
537 #endif
538         s = splnet();
539         vj_compress_init(&sc->sc_comp, *(int *)data);
540         splx(s);
541         break;
542 #endif
543
544     case PPPIOCXFERUNIT:
545         if (! suser())
546             return EPERM;
547         sc->sc_xfer = p->p_pid;
548 #if 0
549         IOLogDbg("ppp%d: setting pid %d\n", if_unit(sc->sc_if), sc->sc_xfer);
550 #endif
551         break;
552
553 #ifdef PPP_COMPRESS
554     case PPPIOCSCOMPRESS:
555         if (! suser())
556             return EPERM;
557         odp = (struct ppp_option_data *) data;
558         nb = odp->length;
559         if (nb > sizeof(ccp_option))
560             nb = sizeof(ccp_option);
561         if (error = copyin(odp->ptr, ccp_option, nb))
562             return (error);
563         if (ccp_option[1] < 2)  /* preliminary check on the length byte */
564             return (EINVAL);
565         for (cp = ppp_compressors; *cp != NULL; ++cp)
566             if ((*cp)->compress_proto == ccp_option[0]) {
567                 /*
568                  * Found a handler for the protocol - try to allocate
569                  * a compressor or decompressor.
570                  */
571                 error = 0;
572                 s = splnet();
573                 if (odp->transmit) {
574                     if (sc->sc_xc_state != NULL)
575                         (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
576                     sc->sc_xcomp = *cp;
577                     sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
578                     if (sc->sc_xc_state == NULL) {
579                         IOLogDbg("ppp%d: comp_alloc failed", if_unit(sc->sc_if));
580                         error = ENOBUFS;
581                     }
582                     splimp();
583                     sc->sc_flags &= ~SC_COMP_RUN;
584                 } else {
585                     if (sc->sc_rc_state != NULL)
586                         (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
587                     sc->sc_rcomp = *cp;
588                     sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
589                     if (sc->sc_rc_state == NULL) {
590                         IOLogDbg("ppp%d: decomp_alloc failed", if_unit(sc->sc_if));
591                         error = ENOBUFS;
592                     }
593                     splimp();
594                     sc->sc_flags &= ~SC_DECOMP_RUN;
595                 }
596                 splx(s);
597                 return (error);
598             }
599         IOLogDbg("ppp%d: no compressor for [%x %x %x], %x", if_unit(sc->sc_if),
600                  ccp_option[0], ccp_option[1], ccp_option[2], nb);
601         return (EINVAL);        /* no handler found */
602 #endif /* PPP_COMPRESS */
603
604 #ifdef  HAS_BROKEN_TIOCSPGRP
605     case TIOCSPGRP:
606 #if 0
607         IOLogDbg("ppp%d: pgrp was %d, setting pgrp %d, flags 0x%x, state 0x%x\n",
608                  if_unit(sc->sc_if), tp->t_pgrp, *(int *)data,
609                  tp->t_flags, tp->t_state);
610 #endif
611         tp->t_pgrp = *(int *)data;
612         break;
613 #endif
614
615     case PPPIOCGNPMODE:
616     case PPPIOCSNPMODE:
617         npi = (struct npioctl *) data;
618         switch (npi->protocol) {
619         case PPP_IP:
620             npx = NP_IP;
621             break;
622         default:
623             return EINVAL;
624         }
625         if (cmd == PPPIOCGNPMODE) {
626             npi->mode = sc->sc_npmode[npx];
627         } else {
628             if (! suser())
629                 return EPERM;
630 #if 0
631             IOLogDbg("ppp%d: setting protocol %d to mode %d\n", if_unit(sc->sc_if), npx, npi->mode);
632 #endif
633             if (npi->mode != sc->sc_npmode[npx]) {
634                 s = splimp();
635                 sc->sc_npmode[npx] = npi->mode;
636                 if (npi->mode != NPMODE_QUEUE) {
637                     ppp_requeue(sc);
638                     (*sc->sc_start)(sc);
639                 }
640                 splx(s);
641             }
642         }
643         break;
644
645     case PPPIOCGIDLE:
646         s = splimp();
647         t = time.tv_sec;
648         ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
649         ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
650         splx(s);
651         break;
652
653     default:
654         return (-1);
655     }
656     return (0);
657 }
658
659 int
660 pppcontrol(ifp, cmd, data)
661     netif_t ifp;
662     const char *cmd;
663     void *data;
664 {
665 #if 0
666     register struct ppp_softc *sc = &ppp_softc[if_unit(ifp)];
667 #endif
668
669     if (!strcmp(cmd, IFCONTROL_UNIXIOCTL)) {
670         if_ioctl_t* ctl = (if_ioctl_t*)data;
671         return pppsioctl(ifp,
672                         ctl->ioctl_command,
673                         ctl->ioctl_data);
674     } else if (!strcmp(cmd, IFCONTROL_SETADDR)) {
675         struct sockaddr_in *sin = (struct sockaddr_in *)data;
676         if (sin->sin_family != AF_INET)
677                 return EAFNOSUPPORT;
678 #if 0
679         IOLogDbg("ppp%d: setting address, and bringing up interface\n",
680                  if_unit(ifp));
681 #endif
682         if_flags_set(ifp, if_flags(ifp) | IFF_UP);
683         return 0;
684     }
685     /*
686      * We implement this to allow iftab
687      * to contain -AUTOMATIC- entries
688      * without generating errors at boot time.
689      * We do not, however, mark it as UP.
690      */
691     else if (!strcmp(cmd, IFCONTROL_AUTOADDR)) {
692         struct sockaddr_in *sin = (struct sockaddr_in *) data;
693         if (sin->sin_family != AF_INET)
694             return EAFNOSUPPORT;
695 #if 0
696         IOLogDbg("ppp%d: probing for interface address\n", if_unit(ifp));
697 #endif
698         return 0;
699     } else if (!strcmp(cmd, IFCONTROL_SETFLAGS)) {
700         register union ifr_ifru *ifr = (union ifr_ifru *)data;
701         if (!suser())
702             return EPERM;
703 #if 0
704         IOLogDbg("ppp%d: old flags 0x%x, new flags 0x%x\n", if_unit(ifp),
705                  if_flags(ifp), ifr->ifru_flags);
706 #endif
707         if_flags_set(ifp, ifr->ifru_flags);
708         return 0;
709     }
710     /*
711      * Under 3.2 developer, I don't know the symbol for this
712      * new 3.3 command.  So it is a constant for now. I don't
713      * believe I need to do anything to support this at the moment.
714      */
715     else if (strcmp(cmd, "add-multicast") == 0) {
716       struct sockaddr_in *sin = (struct sockaddr_in *) data;
717       if (sin->sin_family != AF_INET)
718         return EAFNOSUPPORT;
719     } else {
720         IOLog("ppp%d: Invalid ppp control %s\n", if_unit(ifp), cmd);
721         return EINVAL;
722     }
723 }
724
725 /*
726  * Process an ioctl request to the ppp network interface.
727  */
728 int
729 pppsioctl(ifp, cmd, data)
730     register netif_t ifp;
731     int cmd;
732     caddr_t data;
733 {
734     register struct ppp_softc *sc = &ppp_softc[if_unit(ifp)];
735     register struct ifaddr *ifa = (struct ifaddr *)data;
736     register struct ifreq *ifr = (struct ifreq *)data;
737     struct ppp_stats *psp;
738 #ifdef  PPP_COMPRESS
739     struct ppp_comp_stats *pcp;
740 #endif
741     int s = splimp(), error = 0;
742
743     switch (cmd) {
744     case SIOCSIFFLAGS:
745         IOLog("ppp%d: pppioctl: SIOCSIFFLAGS called!\n", if_unit(ifp));
746 #if 0
747         if_flags_set(ifp, (if_flags(ifp) & IFF_CANTCHANGE)
748                    | (ifr->ifr_flags & ~IFF_CANTCHANGE));
749 #endif
750         break;
751
752     case SIOCSIFADDR:
753         if (ifa->ifa_addr.sa_family != AF_INET)
754             error = EAFNOSUPPORT;
755         break;
756
757     case SIOCSIFDSTADDR:
758         if (ifa->ifa_addr.sa_family != AF_INET)
759             error = EAFNOSUPPORT;
760         break;
761
762     case SIOCSIFMTU:
763         if (!suser()) {
764             error = EPERM;
765             break;
766         }
767         if_mtu_set(sc->sc_if, ifr->ifr_mtu);
768         nbq_flush(&sc->sc_freeq);               /* get rid of old buffers */
769         pppsched(pppfillfreeq, sc);             /* and make a queue of new ones */
770         pppgetm(sc);
771         break;
772
773     case SIOCGIFMTU:
774         ifr->ifr_mtu = if_mtu(sc->sc_if);
775         break;
776
777     case SIOCGPPPSTATS:
778         psp = &((struct ifpppstatsreq *) data)->stats;
779         bzero(psp, sizeof(*psp));
780         psp->p.ppp_ibytes = sc->sc_bytesrcvd;
781         psp->p.ppp_ipackets = if_ipackets(sc->sc_if);
782         psp->p.ppp_ierrors = if_ierrors(sc->sc_if);
783         psp->p.ppp_obytes = sc->sc_bytessent;
784         psp->p.ppp_opackets = if_opackets(sc->sc_if);
785         psp->p.ppp_oerrors = if_oerrors(sc->sc_if);
786 #ifdef VJC
787         psp->vj.vjs_packets = sc->sc_comp.stats.vjs_packets;
788         psp->vj.vjs_compressed = sc->sc_comp.stats.vjs_compressed;
789         psp->vj.vjs_searches = sc->sc_comp.stats.vjs_searches;
790         psp->vj.vjs_misses = sc->sc_comp.stats.vjs_misses;
791         psp->vj.vjs_uncompressedin = sc->sc_comp.stats.vjs_uncompressedin;
792         psp->vj.vjs_compressedin = sc->sc_comp.stats.vjs_compressedin;
793         psp->vj.vjs_errorin = sc->sc_comp.stats.vjs_errorin;
794         psp->vj.vjs_tossed = sc->sc_comp.stats.vjs_tossed;
795 #endif /* VJC */
796         break;
797
798 #ifdef PPP_COMPRESS
799     case SIOCGPPPCSTATS:
800         pcp = &((struct ifpppcstatsreq *) data)->stats;
801         bzero(pcp, sizeof(*pcp));
802         if (sc->sc_xc_state != NULL)
803             (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
804         if (sc->sc_rc_state != NULL)
805             (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
806         break;
807 #endif /* PPP_COMPRESS */
808
809     default:
810         error = EINVAL;
811     }
812     splx(s);
813     return (error);
814 }
815
816 /*
817  * Queue a packet.  Start transmission if not active.
818  * Packet is placed in Information field of PPP frame.
819  */
820 int
821 pppoutput(ifp, m0, arg)
822     netif_t ifp;
823     netbuf_t m0;
824     void *arg;
825 {
826     register struct ppp_softc *sc = &ppp_softc[if_unit(ifp)];
827     struct sockaddr *dst = (struct sockaddr *) arg;
828     int protocol, address, control;
829     u_char *cp;
830     int s, error;
831     mark_t flags = 0;
832     struct ip *ip;
833     struct nb_queue *ifq;
834     enum NPmode mode;
835
836     if (sc->sc_devp == NULL || (if_flags(ifp) & IFF_RUNNING) == 0
837         || (if_flags(ifp) & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC) {
838         error = ENETDOWN;       /* sort of */
839         goto bad;
840     }
841
842     /*
843      * Compute PPP header.
844      */
845     flags &= ~M_HIGHPRI;
846     switch (dst->sa_family) {
847 #ifdef INET
848     case AF_INET:
849         address = PPP_ALLSTATIONS;
850         control = PPP_UI;
851         protocol = PPP_IP;
852         mode = sc->sc_npmode[NP_IP];
853
854         /*
855          * If this packet has the "low delay" bit set in the IP header,
856          * or TCP and to an interactive port, put it on the fastq instead
857          */
858         ip = mtod(m0, struct ip *);
859         if (ip->ip_tos & IPTOS_LOWDELAY || ip->ip_p == IPPROTO_ICMP)
860             goto urgent;
861         else if (ip->ip_p == IPPROTO_TCP) {
862             register u_short *p = (u_short *) &(((caddr_t) ip)[ip->ip_hl << 2]);
863             if (INTERACTIVE(ntohs(p[0])) || INTERACTIVE(ntohs(p[1])))
864 urgent:         flags |= M_HIGHPRI;
865         }
866         break;
867 #endif
868 #ifdef NS
869     case AF_NS:
870         address = PPP_ALLSTATIONS;
871         control = PPP_UI;
872         protocol = PPP_XNS;
873         mode = NPMODE_PASS;
874         break;
875 #endif
876     case AF_UNSPEC:
877         address = PPP_ADDRESS(dst->sa_data);
878         control = PPP_CONTROL(dst->sa_data);
879         protocol = PPP_PROTOCOL(dst->sa_data);
880         mode = NPMODE_PASS;
881         break;
882     default:
883         IOLog("ppp%d: af%d not supported\n", if_unit(ifp), dst->sa_family);
884         error = EAFNOSUPPORT;
885         goto bad;
886     }
887
888     /*
889      * Drop this packet, or return an error, if necessary.
890      */
891     if (mode == NPMODE_ERROR) {
892         error = ENETDOWN;
893         goto bad;
894     }
895     if (mode == NPMODE_DROP) {
896         error = 0;
897         goto bad;
898     }
899
900     /*
901      * Add PPP header.
902      */
903     ppp_nb_grow_top(m0, PPP_HDRLEN);
904
905     cp = mtod(m0, u_char *);
906     *cp++ = address;
907     *cp++ = control;
908     *cp++ = protocol >> 8;
909     *cp++ = protocol & 0xff;
910
911     if (sc->sc_flags & SC_LOG_OUTPKT) {
912         IOLog("ppp%d: output:\n", if_unit(ifp));        /* XXX */
913         pppdumpm(m0);
914     }
915
916 #ifdef NBPFILTER
917     /*
918      * See if bpf wants to look at the packet.
919      */
920     if (sc->sc_bpf)
921         bpf_tap(sc->sc_bpf, nb_map(m0), nb_size(m0));
922 #endif
923
924     /*
925      * Put the packet on the appropriate queue.
926      */
927     s = splimp();               /* splnet should be OK now */
928     if (mode == NPMODE_QUEUE) {
929         nb_set_mark(m0,flags);                  /* save priority */
930         /* XXX we should limit the number of packets on this queue */
931         nbq_enqueue(&sc->sc_npq, m0);           /* XXX is this correct? */
932     } else {
933         ifq = (flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_slowq;
934         if (nbq_full(ifq) < 0) {
935             nbq_drop(ifq);
936             IOLog("ppp%d: output queue full\n", if_unit(sc->sc_if));
937             splx(s);
938             incr_cnt(sc->sc_if, if_oerrors);
939             error = ENOBUFS;
940             goto bad;
941         }
942         nbq_enqueue(ifq, m0);
943     }
944
945     /*
946      * If we don't have some compressed packets already
947      * and we are not at interrupt priority, then do some compression. 
948      *
949      * We need to be especially careful here.  pppouput() is typically
950      * called at 2 different priority levels.  On a NeXT, neither of these
951      * is the interrupt priority level.  However, on Intel, one of them is.
952      * I don't know about HPPA or Sparc. Simple fix is to just check.
953      */
954     
955     if(!sc->sc_compsched && s != ipltospl(IPLIMP)) {
956         sc->sc_compsched = 1;
957         splx(s);
958         pppintr_comp(sc);    /* Calls pppstart() */
959     }
960     else {
961       (*sc->sc_start)(sc);
962       splx(s);
963     }
964
965     return (0);
966
967 bad:
968     nb_free(m0);
969     return (error);
970 }
971
972 /*
973  * After a change in the NPmode for some NP, move packets from the
974  * npqueue to the send queue or the fast queue as appropriate.
975  * Should be called at splimp (actually splnet would probably suffice).
976  * Due to some of the uglies in the packet queueing system I have
977  * implemented this without the mpp stuff.
978  * PCF
979  */
980
981 static void
982 ppp_requeue(sc)
983     struct ppp_softc *sc;
984 {
985     netbuf_t m, lm, nm;
986     struct nb_queue *ifq;
987     enum NPmode mode;
988     mark_t flags;
989
990     lm = nm = NULL;
991     for (m = sc->sc_npq.head; m; ) {
992         nb_get_next(m,&nm);
993
994         switch (PPP_PROTOCOL(mtod(m, u_char *))) {
995         case PPP_IP:
996             mode = sc->sc_npmode[NP_IP];
997             break;
998         default:
999             mode = NPMODE_PASS;
1000         }
1001
1002         switch (mode) {
1003         case NPMODE_PASS:
1004             /*
1005              * This packet can now go on one of the queues to be sent.
1006              */
1007             if(lm)
1008                 nb_set_next(lm,nm);
1009             else
1010                 sc->sc_npq.head = nm;
1011             nb_set_next(m,NULL);
1012             nb_get_mark(m,&flags);
1013             ifq = (flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_slowq;
1014             if (nbq_full(ifq)) {
1015                 nbq_drop(ifq);
1016                 incr_cnt(sc->sc_if, if_oerrors);
1017                 nb_free(m);
1018             } else 
1019                 nbq_enqueue(ifq, m);
1020             sc->sc_npq.len--;
1021             break;
1022
1023         case NPMODE_DROP:
1024         case NPMODE_ERROR:
1025             sc->sc_npq.len--;
1026             nb_free(m);
1027             break;
1028
1029         case NPMODE_QUEUE:
1030             lm = m;
1031             break;
1032         }
1033         m = nm;
1034     }
1035     sc->sc_npq.tail = lm;       /*  anything further on has been sent ! */
1036 }
1037
1038 /*
1039  * Get a packet to send.  This procedure is intended to be called
1040  * at spltty()/splimp(), so it takes little time.  If there isn't
1041  * a packet waiting to go out, it schedules a software interrupt
1042  * to prepare a new packet; the device start routine gets called
1043  * again when a packet is ready.
1044  */
1045 netbuf_t
1046 ppp_dequeue(sc)
1047     struct ppp_softc *sc;
1048 {
1049   netbuf_t m;
1050   int error;
1051   
1052   m = nbq_dequeue(&sc->sc_compq);
1053
1054
1055   if (!sc->sc_compsched && 
1056       (! nbq_empty(&sc->sc_slowq) || ! nbq_empty(&sc->sc_fastq)))
1057     {
1058       
1059       if ((error = pppsched(pppintr_comp, sc)) == KERN_SUCCESS)
1060         sc->sc_compsched = 1;
1061       else
1062         {
1063           IOLogDbg("ppp%d: compression callout failed returning %d\n",
1064                    if_unit(sc->sc_if), error);
1065         }
1066     }
1067   
1068   return m;
1069 }
1070
1071 /*
1072  * Takes all received input packets and uncompresses/hands_off.
1073  * Must not be reentrant and is called at normal priority.
1074  * Guaranteed Non-Reentrancy means we don't need to be at splnet().
1075  *
1076  */
1077
1078 void
1079 pppintr_decomp(arg)
1080     void *arg;
1081 {
1082     struct ppp_softc *sc = (struct ppp_softc *)arg;
1083     int s;
1084     netbuf_t m;
1085
1086     if (nbq_low(&sc->sc_freeq))
1087       pppfillfreeq((void *) sc);
1088
1089   decomp:
1090     for (;;) {
1091       m = nbq_dequeue(&sc->sc_rawq);
1092       if (m == NULL)
1093         break;
1094       ppp_inproc(sc, m);
1095     }
1096
1097   /*
1098    * Now we have aparently emptied the queue.  So, we try to reset the
1099    * synchronization flag that schedules callbacks.  We check for the
1100    * possibility that an interrupt occurred before we finish this check.
1101    */
1102   s = splimp();
1103   if (!nbq_empty(&sc->sc_rawq))
1104     {
1105       splx(s);
1106       goto decomp;
1107     }
1108   else
1109     {
1110       sc->sc_decompsched = 0;
1111       splx(s);
1112     }
1113 }
1114
1115
1116
1117 /*
1118  * Readies the next few output packet from
1119  * the sc_fastq/sc_slowq.  Will try to
1120  * precompress all packets on the fast
1121  * queue and at most one from the slow queue.
1122  */
1123 void
1124 pppintr_comp(arg)
1125     void *arg;
1126 {
1127   struct ppp_softc *sc = (struct ppp_softc *)arg;
1128   int s;
1129   netbuf_t m;
1130   
1131       if (nbq_low(&sc->sc_freeq))
1132         pppfillfreeq((void *) sc);
1133
1134   
1135   while (!nbq_full(&sc->sc_compq) && !nbq_empty(&sc->sc_fastq))
1136     ppp_outpkt(sc);
1137
1138         if (!nbq_full(&sc->sc_compq) && !nbq_empty(&sc->sc_slowq))
1139           ppp_outpkt(sc);
1140       
1141       sc->sc_compsched = 0;
1142 }
1143
1144 /*
1145  * Grab another packet off a queue and apply VJ compression,
1146  * packet compression, address/control and/or protocol compression
1147  * if enabled.  Should be called at splnet.
1148  */
1149 static void
1150 ppp_outpkt(sc)
1151     struct ppp_softc *sc;
1152 {
1153     int s;
1154     netbuf_t m;
1155     u_char *cp;
1156     int address, control, protocol;
1157
1158     /*
1159      * Grab a packet to send: first try the fast queue, then the
1160      * normal queue.
1161      */
1162     m = nbq_dequeue(&sc->sc_fastq);
1163     if (m == NULL)
1164         m = nbq_dequeue(&sc->sc_slowq);
1165     if (m == NULL)
1166         return;
1167
1168     /*
1169      * Extract the ppp header of the new packet.
1170      * The ppp header will be in one netbuf.
1171      */
1172     cp = mtod(m, u_char *);
1173     address = PPP_ADDRESS(cp);
1174     control = PPP_CONTROL(cp);
1175     protocol = PPP_PROTOCOL(cp);
1176
1177     switch (protocol) {
1178     case PPP_IP:
1179         /*
1180          * Update the time we sent the most recent packet.
1181          */
1182         sc->sc_last_sent = time.tv_sec;
1183
1184 #ifdef VJC
1185         /*
1186          * If the packet is a TCP/IP packet, see if we can compress it.
1187          */
1188         if (sc->sc_flags & SC_COMP_TCP) {
1189             struct ip *ip;
1190             int type;
1191             u_char *vjhdr;
1192
1193             ip = (struct ip *) (cp + PPP_HDRLEN);
1194             /* this code assumes the IP/TCP header is in one netbuf */
1195             if (ip->ip_p == IPPROTO_TCP) {
1196                 type = vj_compress_tcp(ip, nb_size(m) - PPP_HDRLEN,
1197                                        &sc->sc_comp,
1198                                        !(sc->sc_flags & SC_NO_TCP_CCID), &vjhdr);
1199                 switch (type) {
1200                 case TYPE_UNCOMPRESSED_TCP:
1201                     protocol = PPP_VJC_UNCOMP;
1202                     break;
1203                 case TYPE_COMPRESSED_TCP:
1204                     ppp_nb_shrink_top(m, vjhdr - (u_char *) ip);
1205                     protocol = PPP_VJC_COMP;
1206                     cp = mtod(m, u_char *);
1207                     cp[0] = address;    /* header has moved */
1208                     cp[1] = control;
1209                     cp[2] = 0;
1210                     break;
1211                 }
1212                 cp[3] = protocol;       /* update protocol in PPP header */
1213             }
1214         }
1215 #endif  /* VJC */
1216         break;
1217
1218 #ifdef PPP_COMPRESS
1219     case PPP_CCP:
1220         ppp_ccp(sc, m, 0);
1221         break;
1222 #endif  /* PPP_COMPRESS */
1223     }
1224
1225 #ifdef PPP_COMPRESS
1226     if (protocol != PPP_LCP && protocol != PPP_CCP
1227         && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1228         netbuf_t mcomp;
1229         int slen, clen;
1230
1231         slen = nb_size(m);
1232         clen = (*sc->sc_xcomp->compress)
1233             (sc->sc_xc_state, &mcomp, m, slen,
1234              sc->sc_flags & SC_CCP_UP? if_mtu(sc->sc_if): 0);
1235         if (mcomp != NULL) {
1236             nb_free(m);
1237             m = mcomp;
1238             cp = mtod(m, u_char *);
1239             protocol = cp[3];
1240         }
1241     }
1242 #endif  /* PPP_COMPRESS */
1243
1244     /*
1245      * Compress the address/control and protocol, if possible.
1246      */
1247     if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1248         control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1249         protocol != PPP_LCP) {
1250         /* can compress address/control */
1251         ppp_nb_shrink_top(m, 2);
1252     }
1253     if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1254         /* can compress protocol */
1255         if (mtod(m, u_char *) == cp) {
1256             cp[2] = cp[1];      /* move address/control up */
1257             cp[1] = cp[0];
1258         }
1259         ppp_nb_shrink_top(m, 1);
1260     }
1261
1262     s = splimp();
1263     nbq_enqueue(&sc->sc_compq, m);
1264     (*sc->sc_start)(sc);
1265     splx(s);
1266 }
1267
1268 #ifdef PPP_COMPRESS
1269 /*
1270  * Handle a CCP packet.  `rcvd' is 1 if the packet was received,
1271  * 0 if it is about to be transmitted.
1272  */
1273 static void
1274 ppp_ccp(sc, m, rcvd)
1275     struct ppp_softc *sc;
1276     netbuf_t m;
1277     int rcvd;
1278 {
1279     u_char *dp, *ep;
1280     int slen, s;
1281
1282     /*
1283      * Get a pointer to the data after the PPP header.
1284      */
1285     dp = mtod(m, u_char *) + PPP_HDRLEN;
1286
1287     ep = mtod(m, u_char *) + nb_size(m);
1288     if (dp + CCP_HDRLEN > ep)
1289         return;
1290     slen = CCP_LENGTH(dp);
1291     if (dp + slen > ep) {
1292         IOLogDbg("ppp%d: ccp: not enough data in netbuf (%x+%x > %x+%x)\n",
1293                  if_unit(sc->sc_if), dp, slen, mtod(m, u_char *), nb_size(m));
1294         return;
1295     }
1296
1297     switch (CCP_CODE(dp)) {
1298     case CCP_CONFREQ:
1299     case CCP_TERMREQ:
1300     case CCP_TERMACK:
1301         /* CCP must be going down - disable compression */
1302         if (sc->sc_flags & SC_CCP_UP) {
1303             s = splimp();
1304             sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1305             splx(s);
1306         }
1307         break;
1308
1309     case CCP_CONFACK:
1310         if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1311             && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1312             && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1313             if (!rcvd) {
1314                 /* we're agreeing to send compressed packets. */
1315                 if (sc->sc_xc_state != NULL
1316                     && (*sc->sc_xcomp->comp_init)
1317                         (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1318                          if_unit(sc->sc_if), 0, sc->sc_flags & SC_DEBUG)) {
1319                     s = splimp();
1320                     sc->sc_flags |= SC_COMP_RUN;
1321                     splx(s);
1322                 }
1323             } else {
1324                 /* peer is agreeing to send compressed packets. */
1325                 if (sc->sc_rc_state != NULL
1326                     && (*sc->sc_rcomp->decomp_init)
1327                         (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1328                          if_unit(sc->sc_if),
1329 #ifdef VJC
1330                          VJ_HDRLEN +
1331 #endif
1332                          0, sc->sc_mru, sc->sc_flags & SC_DEBUG)) {
1333                     s = splimp();
1334                     sc->sc_flags |= SC_DECOMP_RUN;
1335                     sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1336                     splx(s);
1337                 }
1338             }
1339         }
1340         break;
1341
1342     case CCP_RESETACK:
1343         if (sc->sc_flags & SC_CCP_UP) {
1344             if (!rcvd) {
1345                 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1346                     (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1347                     nbq_flush(&sc->sc_compq);  /* Flush pre-compressed packets */
1348                   }
1349             } else {
1350                 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1351                     (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1352                     s = splimp();
1353                     sc->sc_flags &= ~SC_DC_ERROR;
1354                     splx(s);
1355                 }
1356             }
1357         }
1358         break;
1359     }
1360 }
1361
1362 /*
1363  * CCP is down; free (de)compressor state if necessary.
1364  */
1365 static void
1366 ppp_ccp_closed(sc)
1367     struct ppp_softc *sc;
1368 {
1369     if (sc->sc_xc_state) {
1370         (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1371         sc->sc_xc_state = NULL;
1372     }
1373     if (sc->sc_rc_state) {
1374         (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1375         sc->sc_rc_state = NULL;
1376     }
1377 }
1378 #endif /* PPP_COMPRESS */
1379
1380 /*
1381  * PPP packet input routine.
1382  * The caller has checked and removed the FCS and has inserted
1383  * the address/control bytes and the protocol high byte if they
1384  * were omitted.
1385  */
1386 void
1387 ppppktin(sc, m, lost)
1388     struct ppp_softc *sc;
1389     netbuf_t m;
1390     int lost;
1391 {
1392   int error, s = splimp();
1393   
1394   nb_set_mark(m,(lost ? M_ERRMARK : 0));
1395   
1396   /* XXX - we should check for the raw queue overflowing... */
1397   nbq_enqueue(&sc->sc_rawq, m);
1398   if (!sc->sc_decompsched)
1399     {
1400       if ((error = pppsched(pppintr_decomp, sc)) == KERN_SUCCESS)
1401         sc->sc_decompsched = 1;
1402       else
1403         IOLogDbg("ppp%d: decompression callout failed returning %d\n",
1404                  if_unit(sc->sc_if), error);
1405     }
1406   
1407   splx(s);
1408 }
1409
1410 /*
1411  * Process a received PPP packet, doing decompression as necessary.
1412  */
1413 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1414                          TYPE_UNCOMPRESSED_TCP)
1415
1416 static void
1417 ppp_inproc(sc, m)
1418     struct ppp_softc *sc;
1419     netbuf_t m;
1420 {
1421     struct nb_queue *inq;
1422     int s, ilen, xlen, proto, rv;
1423     mark_t flags;
1424     u_char *cp, adrs, ctrl;
1425     netbuf_t dmp;
1426     u_char *iphdr;
1427     u_int hlen;
1428
1429     incr_cnt(sc->sc_if, if_ipackets);
1430
1431     nb_get_mark(m,&flags);
1432
1433     if (sc->sc_flags & SC_LOG_INPKT) {
1434         IOLog("ppp%d: got %d bytes\n", if_unit(sc->sc_if), nb_size(m));
1435         pppdumpm(m);
1436     }
1437
1438     cp = mtod(m, u_char *);
1439     adrs = PPP_ADDRESS(cp);
1440     ctrl = PPP_CONTROL(cp);
1441     proto = PPP_PROTOCOL(cp);
1442
1443     if (flags & M_ERRMARK) {
1444         s = splimp();
1445         sc->sc_flags |= SC_VJ_RESET;
1446         splx(s);
1447     }
1448
1449 #ifdef PPP_COMPRESS
1450     /*
1451      * Decompress this packet if necessary, update the receiver's
1452      * dictionary, or take appropriate action on a CCP packet.
1453      */
1454     if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1455         && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1456         /* decompress this packet */
1457         rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1458         if (rv == DECOMP_OK){
1459             nb_free(m);
1460             if (dmp == NULL){
1461                 /* No error, but no decompressed packet returned */
1462                 return;
1463               }
1464             m = dmp;
1465             cp = mtod(m, u_char *);
1466             proto = PPP_PROTOCOL(cp);
1467
1468         } else {
1469             /*
1470              * An error has occurred in decompression.
1471              * Pass the compressed packet up to pppd, which may take
1472              * CCP down or issue a Reset-Req.
1473              */
1474             IOLogDbg("ppp%d: decompress failed %d\n", if_unit(sc->sc_if), rv);
1475             s = splimp();
1476             sc->sc_flags |= SC_VJ_RESET;
1477             if (rv == DECOMP_ERROR)
1478               sc->sc_flags |= SC_DC_ERROR;
1479             else
1480               sc->sc_flags |= SC_DC_FERROR;
1481             splx(s);
1482         }
1483
1484     } else {
1485         if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1486             (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1487         }
1488         if (proto == PPP_CCP) {
1489             ppp_ccp(sc, m, 1);
1490         }
1491     }
1492 #endif
1493
1494     ilen = nb_size(m);
1495
1496 #ifdef VJC
1497     if (sc->sc_flags & SC_VJ_RESET) {
1498         /*
1499          * If we've missed a packet, we must toss subsequent compressed
1500          * packets which don't have an explicit connection ID.
1501          */
1502         vj_uncompress_err(&sc->sc_comp);
1503         s = splimp();
1504         sc->sc_flags &= ~SC_VJ_RESET;
1505         splx(s);
1506     }
1507
1508     /*
1509      * See if we have a VJ-compressed packet to uncompress.
1510      */
1511     if (proto == PPP_VJC_COMP) {
1512         if (sc->sc_flags & SC_REJ_COMP_TCP)
1513             goto bad;
1514
1515         xlen = vj_uncompress_tcp(cp + PPP_HDRLEN, ilen - PPP_HDRLEN,
1516                                  ilen - PPP_HDRLEN,
1517                                  &sc->sc_comp, &iphdr, &hlen);
1518
1519         if (xlen <= 0) {
1520             IOLogDbg("ppp%d: VJ uncompress failed on type comp\n",
1521                         if_unit(sc->sc_if));
1522             goto bad;
1523         }
1524
1525         ppp_nb_grow_top(m, hlen - xlen);
1526         nb_write(m, PPP_HDRLEN, hlen, iphdr);
1527
1528         cp = mtod(m, u_char *);
1529         cp[0] = adrs;
1530         cp[1] = ctrl;
1531         cp[2] = 0;
1532         cp[3] = PPP_IP;
1533         proto = PPP_IP;
1534
1535         ilen += hlen - xlen;
1536
1537     } else if (proto == PPP_VJC_UNCOMP) {
1538         if (sc->sc_flags & SC_REJ_COMP_TCP)
1539             goto bad;
1540
1541         vj_uncompress_uncomp(cp + PPP_HDRLEN, &sc->sc_comp);
1542
1543         proto = PPP_IP;
1544         cp[3] = PPP_IP;
1545     }
1546 #endif /* VJC */
1547
1548 #ifdef NBPFILTER
1549     /* See if bpf wants to look at the packet. */
1550     if (sc->sc_bpf)
1551         bpf_tap(sc->sc_bpf, nb_map(m), nb_size(m));
1552 #endif
1553
1554     rv = 0;
1555     switch (proto) {
1556 #ifdef INET
1557     case PPP_IP:
1558         /*
1559          * IP packet - take off the ppp header and pass it up to IP.
1560          */
1561         if ((if_flags(sc->sc_if) & IFF_UP) == 0
1562             || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1563             /* interface is down - drop the packet. */
1564             nb_free(m);
1565             IOLogDbg("ppp%d: IP packed dropped (NPmode)\n", if_unit(sc->sc_if));
1566             return;
1567         }
1568         ppp_nb_shrink_top(m, PPP_HDRLEN);
1569         inet_queue(sc->sc_if, m);
1570         sc->sc_last_recv = time.tv_sec; /* update time of last pkt rcvd */
1571         return;
1572 #endif
1573
1574     default:
1575         /*
1576          * Some other protocol - place on input queue for read().
1577          */
1578         inq = &sc->sc_inq;
1579         rv = 1;
1580         break;
1581     }
1582
1583     /*
1584      * Put the packet on the appropriate input queue.
1585      */
1586     s = splimp();
1587     if (nbq_full(inq)) {
1588         nbq_drop(inq);
1589         splx(s);
1590         IOLog("ppp%d: input queue full\n", if_unit(sc->sc_if));
1591         goto bad;
1592     }
1593     nbq_enqueue(inq, m);
1594     splx(s);
1595
1596     if (rv)
1597         (*sc->sc_ctlp)(sc);
1598
1599     return;
1600
1601  bad:
1602     nb_free(m);
1603     incr_cnt(sc->sc_if, if_ierrors);
1604 }
1605
1606 #define MAX_DUMP_BYTES  128
1607
1608 static void
1609 pppdumpm(m0)
1610     netbuf_t m0;
1611 {
1612     char buf[3*MAX_DUMP_BYTES+4];
1613     char *bp = buf;
1614     static char digits[] = "0123456789abcdef";
1615     int l = nb_size(m0);
1616     u_char *rptr = mtod(m0, u_char *);
1617
1618     while (l--) {
1619         if (bp > buf + sizeof(buf) - 4)
1620             goto done;
1621         *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1622         *bp++ = digits[*rptr++ & 0xf];
1623     }
1624
1625     *bp++ = ' ';
1626 done:
1627     if (l)
1628         *bp++ = '>';
1629     *bp = 0;
1630     IOLog("%s\n", buf);
1631 }
1632
1633