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