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