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
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
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
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
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.
43 * Carnegie Mellon University
45 * Pittsburgh, PA 15213
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
70 * Center for Seismic Studies
71 * 1300 N 17th Street, Suite 1450
72 * Arlington, Virginia 22209
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 */
107 static char sccsid
[] = "$Revision: 1.13 $ ($Date: 2002/12/06 09:49:15 $)";
111 #define KERNEL_FEATURES 1
116 #include <kernserv/clock_timer.h>
117 #include <kernserv/lock.h>
119 #include <kern/lock.h>
120 #endif /* NS_TARGET */
121 #endif /* NS_TARGET */
123 #include <sys/param.h>
125 typedef simple_lock_data_t lock_data_t
; /* XXX */
126 #endif /* NS_TARGET */
127 #include <sys/proc.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>
140 #include <driverkit/generalFuncs.h>
141 #import <kernserv/machine/spl.h>
143 #if defined(sparc) || defined(m68k)
144 #include <machine/psl.h>
146 #include <kernserv/kern_server_types.h>
149 #include <net/route.h>
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>
159 #include <net/ppp_defs.h>
161 #include <net/vjcompress.h>
163 #include <net/if_ppp.h>
164 #include "NeXT_Version.h"
165 #include "if_pppvar.h"
168 struct ppp_softc ppp_softc
[NUM_PPP
];
172 #define PACKETPTR NETBUF_T
173 #include <net/ppp-comp.h>
177 * The max number of NETBUF_Ts we wish to compress and cache for
180 #define COMPRESS_CACHE_LEN 1
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] = {
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
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] = {
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...
273 u_long ifru_asyncmap
;
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.
288 pppgetbuf(netif_t ifp
)
292 int len
= MAX(if_mtu(ifp
), PPP_MTU
) + PPP_HDRLEN
+ PPP_FCSLEN
;
296 NB_SHRINK_TOP(nb
, PPP_HDRLEN
);
297 NB_SHRINK_BOT(nb
, PPP_FCSLEN
); /* grown by pppstart() */
303 * Called from boot code to establish ppp interfaces.
308 register struct ppp_softc
*sc
;
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
);
334 IOLog("PPP Successfully Installed.\n\n");
340 struct ppp_softc
*sc
;
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");
366 * Allocate a ppp interface unit and initialize it.
373 struct ppp_softc
*sc
;
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
);
384 for (nppp
= 0, sc
= ppp_softc
; nppp
< NUM_PPP
; nppp
++, sc
++)
385 if (sc
->sc_devp
== NULL
)
391 sc
->sc_mru
= PPP_MRU
;
392 sc
->sc_relinq
= NULL
;
394 vj_compress_init(&sc
->sc_comp
, -1);
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... */
405 ns_time_to_timeval(clock_value(System
), &tv_time
);
406 sc
->sc_last_sent
= sc
->sc_last_recv
= tv_time
.tv_sec
;
408 sc
->sc_last_sent
= sc
->sc_last_recv
= time
.tv_sec
;
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
);
424 * Deallocate a ppp unit. Must be called at splnet or higher.
428 struct ppp_softc
*sc
;
431 if_flags_set(sc
->sc_if
, if_flags(sc
->sc_if
) & ~(IFF_UP
|IFF_RUNNING
));
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
);
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
;
460 struct proc
*p
= curproc
;
461 int s
, error
, flags
, mru
, nb
, npx
, oldflags
;
462 struct ppp_option_data
*odp
;
463 struct compressor
**cp
;
467 u_char ccp_option
[CCP_MAX_OPTION_LENGTH
];
470 #ifdef HAS_BROKEN_TIOCSPGRP
471 struct tty
*tp
= sc
->sc_devp
;
474 struct timeval tv_time
;
475 #endif /* NS_TARGET */
480 s
= splimp(); /* paranoid; splnet probably ok */
481 if ((m
= nbq_peek(&sc
->sc_inq
)) != NULL
)
482 *(int *)data
= NB_SIZE(m
);
489 *(int *)data
= if_unit(sc
->sc_if
);
493 *(u_int
*)data
= sc
->sc_flags
;
499 flags
= *(int *)data
& SC_MASK
;
502 if (sc
->sc_flags
& SC_CCP_OPEN
&& !(flags
& SC_CCP_OPEN
))
506 oldflags
= sc
->sc_flags
;
507 sc
->sc_flags
= (sc
->sc_flags
& ~SC_MASK
) | flags
;
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.
527 nbq_flush(&sc
->sc_freeq
); /* get rid of old buffers */
532 if (sc
->sc_ilen
!= 0)
533 sc
->sc_flags
|= SC_FLUSH
;
537 pppfillfreeq((void *) sc
); /* and make a queue of new ones */
543 *(int *)data
= sc
->sc_mru
;
551 vj_compress_init(&sc
->sc_comp
, *(int *)data
);
559 sc
->sc_xfer
= p
->p_pid
;
563 case PPPIOCSCOMPRESS
:
566 odp
= (struct ppp_option_data
*) data
;
568 if (nb
> sizeof(ccp_option
))
569 nb
= sizeof(ccp_option
);
570 if (error
= copyin(odp
->ptr
, ccp_option
, nb
))
572 if (ccp_option
[1] < 2) /* preliminary check on the length byte */
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.
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
));
592 sc
->sc_flags
&= ~SC_COMP_RUN
;
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
));
603 sc
->sc_flags
&= ~SC_DECOMP_RUN
;
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
615 tp
->t_pgrp
= *(int *)data
;
621 npi
= (struct npioctl
*) data
;
622 switch (npi
->protocol
) {
629 if (cmd
== PPPIOCGNPMODE
) {
630 npi
->mode
= sc
->sc_npmode
[npx
];
634 if (npi
->mode
!= sc
->sc_npmode
[npx
]) {
636 sc
->sc_npmode
[npx
] = npi
->mode
;
637 if (npi
->mode
!= NPMODE_QUEUE
) {
649 ns_time_to_timeval(clock_value(System
), &tv_time
);
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
;
666 pppcontrol(ifp
, cmd
, data
)
672 if (!strcmp(cmd
, IFCONTROL_UNIXIOCTL
)) {
673 if_ioctl_t
* ctl
= (if_ioctl_t
*)data
;
674 return pppsioctl(ifp
,
677 } else if (!strcmp(cmd
, IFCONTROL_SETADDR
)) {
678 struct sockaddr_in
*sin
= (struct sockaddr_in
*)data
;
679 if (sin
->sin_family
!= AF_INET
)
681 if_flags_set(ifp
, if_flags(ifp
) | IFF_UP
);
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
)
695 } else if (!strcmp(cmd
, IFCONTROL_SETFLAGS
)) {
696 register union ifr_ifru
*ifr
= (union ifr_ifru
*)data
;
699 if_flags_set(ifp
, ifr
->ifru_flags
);
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
)
712 IOLog("ppp%d: Invalid ppp control %s\n", if_unit(ifp
), cmd
);
718 * Process an ioctl request to the ppp network interface.
721 pppsioctl(ifp
, cmd
, data
)
722 register netif_t ifp
;
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
;
731 struct ppp_comp_stats
*pcp
;
733 int s
= splimp(), error
= 0;
737 IOLog("ppp%d: pppioctl: SIOCSIFFLAGS called!\n", if_unit(ifp
));
741 if (ifa
->ifa_addr
.sa_family
!= AF_INET
)
742 error
= EAFNOSUPPORT
;
746 if (ifa
->ifa_addr
.sa_family
!= AF_INET
)
747 error
= EAFNOSUPPORT
;
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 */
762 ifr
->ifr_mtu
= if_mtu(sc
->sc_if
);
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
);
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
;
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
);
795 #endif /* PPP_COMPRESS */
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
)
819 register struct ppp_softc
*sc
= &ppp_softc
[if_unit(ifp
)];
820 struct sockaddr
*dst
= (struct sockaddr
*) arg
;
821 int protocol
, address
, control
;
826 struct nb_queue
*ifq
;
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 */
840 * Compute PPP header.
843 switch (dst
->sa_family
) {
846 address
= PPP_ALLSTATIONS
;
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
)
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
;
867 address
= PPP_ALLSTATIONS
;
874 address
= PPP_ADDRESS(dst
->sa_data
);
875 control
= PPP_CONTROL(dst
->sa_data
);
876 protocol
= PPP_PROTOCOL(dst
->sa_data
);
880 IOLog("ppp%d: af%d not supported\n", if_unit(ifp
), dst
->sa_family
);
881 error
= EAFNOSUPPORT
;
886 * Drop this packet, or return an error, if necessary.
888 if (mode
== NPMODE_ERROR
) {
892 if (mode
== NPMODE_DROP
) {
900 NB_GROW_TOP(m0
, PPP_HDRLEN
);
902 cp
= mtod(m0
, u_char
*);
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 */
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? */
924 ifq
= (flags
& M_HIGHPRI
)? &sc
->sc_fastq
: &sc
->sc_slowq
;
925 if (nbq_full(ifq
) < 0) {
927 IOLog("ppp%d: output queue full\n", if_unit(sc
->sc_if
));
929 incr_cnt(sc
->sc_if
, if_oerrors
);
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;
949 pppintr_comp(sc
); /* Calls pppstart() */
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.
974 struct ppp_softc
*sc
;
977 struct nb_queue
*ifq
;
982 for (m
= sc
->sc_npq
.head
; m
; ) {
985 switch (PPP_PROTOCOL(mtod(m
, u_char
*))) {
987 mode
= sc
->sc_npmode
[NP_IP
];
996 * This packet can now go on one of the queues to be sent.
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
)) {
1007 incr_cnt(sc
->sc_if
, if_oerrors
);
1010 nbq_enqueue(ifq
, m
);
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.
1038 struct ppp_softc
*sc
;
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;
1054 IOLogDbg("ppp%d: compression callout failed returning %d\n",
1055 if_unit(sc
->sc_if
), error
);
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().
1073 struct ppp_softc
*sc
= (struct ppp_softc
*)arg
;
1077 if (nbq_low(&sc
->sc_freeq
))
1078 pppfillfreeq((void *) sc
);
1082 m
= nbq_dequeue(&sc
->sc_rawq
);
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.
1094 if (!nbq_empty(&sc
->sc_rawq
))
1101 sc
->sc_decompsched
= 0;
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.
1118 struct ppp_softc
*sc
= (struct ppp_softc
*)arg
;
1122 if (nbq_low(&sc
->sc_freeq
))
1123 pppfillfreeq((void *) sc
);
1126 while (!nbq_full(&sc
->sc_compq
) && !nbq_empty(&sc
->sc_fastq
))
1129 if (!nbq_full(&sc
->sc_compq
) && !nbq_empty(&sc
->sc_slowq
))
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.
1142 struct ppp_softc
*sc
;
1147 int address
, control
, protocol
;
1149 struct timeval tv_time
;
1153 * Grab a packet to send: first try the fast queue, then the
1156 m
= nbq_dequeue(&sc
->sc_fastq
);
1158 m
= nbq_dequeue(&sc
->sc_slowq
);
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
);
1172 ns_time_to_timeval(clock_value(System
), &tv_time
);
1173 #endif /* NS_TARGET */
1178 * Update the time we sent the most recent packet.
1181 sc
->sc_last_sent
= tv_time
.tv_sec
;
1183 sc
->sc_last_sent
= time
.tv_sec
;
1184 #endif /* NS_TARGET */
1188 * If the packet is a TCP/IP packet, see if we can compress it.
1190 if (sc
->sc_flags
& SC_COMP_TCP
) {
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
,
1200 !(sc
->sc_flags
& SC_NO_TCP_CCID
), &vjhdr
);
1202 case TYPE_UNCOMPRESSED_TCP
:
1203 protocol
= PPP_VJC_UNCOMP
;
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 */
1214 cp
[3] = protocol
; /* update protocol in PPP header */
1226 #endif /* PPP_COMPRESS */
1231 if (protocol
!= PPP_LCP
&& protocol
!= PPP_CCP
1232 && sc
->sc_xc_state
&& (sc
->sc_flags
& SC_COMP_RUN
)) {
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
) {
1251 cp
= mtod(m
, u_char
*);
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 */
1272 NB_SHRINK_TOP(m
, 1);
1277 nbq_enqueue(&sc
->sc_compq
, m
);
1278 (*sc
->sc_start
)(sc
);
1284 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1285 * 0 if it is about to be transmitted.
1288 ppp_ccp(sc
, m
, rcvd
)
1289 struct ppp_softc
*sc
;
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
)
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
));
1311 switch (CCP_CODE(dp
)) {
1315 /* CCP must be going down - disable compression */
1316 if (sc
->sc_flags
& SC_CCP_UP
) {
1318 sc
->sc_flags
&= ~(SC_CCP_UP
| SC_COMP_RUN
| SC_DECOMP_RUN
);
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
) {
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
)) {
1334 sc
->sc_flags
|= SC_COMP_RUN
;
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
,
1346 0, sc
->sc_mru
, sc
->sc_flags
& SC_DEBUG
)) {
1348 sc
->sc_flags
|= SC_DECOMP_RUN
;
1349 sc
->sc_flags
&= ~(SC_DC_ERROR
| SC_DC_FERROR
);
1357 if (sc
->sc_flags
& SC_CCP_UP
) {
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 */
1364 if (sc
->sc_rc_state
&& (sc
->sc_flags
& SC_DECOMP_RUN
)) {
1365 (*sc
->sc_rcomp
->decomp_reset
)(sc
->sc_rc_state
);
1367 sc
->sc_flags
&= ~SC_DC_ERROR
;
1377 * CCP is down; free (de)compressor state if necessary.
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
1401 ppppktin(sc
, m
, lost
)
1402 struct ppp_softc
*sc
;
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;
1417 IOLogDbg("ppp%d: decompression callout failed returning %d\n",
1418 if_unit(sc
->sc_if
), error
);
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)
1432 struct ppp_softc
*sc
;
1435 struct nb_queue
*inq
;
1436 int s
, ilen
, xlen
, proto
, rv
;
1438 u_char
*cp
, adrs
, ctrl
;
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
));
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
) {
1463 sc
->sc_flags
|= SC_VJ_RESET
;
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
){
1480 /* No error, but no decompressed packet returned */
1484 cp
= mtod(m
, u_char
*);
1485 proto
= PPP_PROTOCOL(cp
);
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
);
1494 sc
->sc_flags
|= SC_VJ_RESET
;
1496 if (rv
== DECOMP_ERROR
)
1497 sc
->sc_flags
|= SC_DC_ERROR
;
1499 sc
->sc_flags
|= SC_DC_FERROR
;
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
) {
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
);
1528 sc
->sc_flags
&= ~SC_VJ_RESET
;
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
)
1540 xlen
= vj_uncompress_tcp(cp
+ PPP_HDRLEN
, ilen
- PPP_HDRLEN
,
1542 &sc
->sc_comp
, &iphdr
, &hlen
);
1546 IOLogDbg("ppp%d: VJ uncompress failed on type comp\n",
1547 if_unit(sc->sc_if));
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
*);
1565 #define getip_hl(base) ((base).ip_hl)
1567 u_short
mytcpcksum (struct ip
*pip
);
1568 struct tcphdr
*ptcp
;
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
)
1582 set_newvj_error_mode();
1584 IOLog("NEWVJ: Warning... TCP checksum failed Received=%u, Calculated=%u)\n",
1585 (ptcp
->th_sum
)&0xffff, thecksum
&0xffff);
1597 ilen
+= hlen
- xlen
;
1599 } else if (proto
== PPP_VJC_UNCOMP
) {
1600 if (sc
->sc_flags
& SC_REJ_COMP_TCP
)
1604 vj_uncompress_uncomp(cp
+ PPP_HDRLEN
, ilen
-PPP_HDRLEN
, &sc
->sc_comp
);
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. */
1623 IOLogDbg("ppp%d: IP packed dropped (NPmode)\n", if_unit(sc
->sc_if
));
1626 NB_SHRINK_TOP(m
, PPP_HDRLEN
);
1627 inet_queue(sc
->sc_if
, NB_TO_nb(m
));
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 */
1633 sc
->sc_last_recv
= time
.tv_sec
; /* update time of last pkt rcvd */
1640 * Some other protocol - place on input queue for read().
1648 * Put the packet on the appropriate input queue.
1651 if (nbq_full(inq
)) {
1654 IOLog("ppp%d: input queue full\n", if_unit(sc
->sc_if
));
1657 nbq_enqueue(inq
, m
);
1667 incr_cnt(sc
->sc_if
, if_ierrors
);
1670 #define MAX_DUMP_BYTES 128
1676 char buf
[3*MAX_DUMP_BYTES
+4];
1678 static char digits
[] = "0123456789abcdef";
1679 int l
= NB_SIZE(m0
);
1680 u_char
*rptr
= mtod(m0
, u_char
*);
1683 if (bp
> buf
+ sizeof(buf
) - 4)
1685 *bp
++ = digits
[*rptr
>> 4]; /* convert byte to ascii hex */
1686 *bp
++ = digits
[*rptr
++ & 0xf];