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