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