* Copyright (c) 1987, 1989 Regents of the University of California.
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* @(#)if_sl.c 7.16 (Berkeley) %G%
* Center for Seismic Studies
* 1300 N 17th Street, Suite 1450
* Arlington, Virginia 22209
* Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
* N.B.: this belongs in netinet, not net, the way it stands now.
* Should have a link-layer type designation, but wouldn't be
* Converted to 4.3BSD Beta by Chris Torek.
* Other changes made at Berkeley, based in part on code by Kirk Smith.
* W. Jolitz added slip abort.
* Hacked almost beyond recognition by Van Jacobson (van@helios.ee.lbl.gov).
* Added priority queuing for "interactive" traffic; hooks for TCP
* header compression; ICMP filtering (at 2400 baud, some cretin
* pinging you can use up all your bandwidth). Made low clist behavior
* more robust and slightly less likely to hang serial line.
* Sped up a bunch of things.
* Note that splimp() is used throughout to block both (tty) input
* interrupts and network activity; thus, splimp must be >= spltty.
/* $Header: if_sl.c,v 1.7 89/05/31 02:24:52 van Exp $ */
/* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
#include "../netinet/in.h"
#include "../netinet/in_systm.h"
#include "../netinet/in_var.h"
#include "../netinet/ip.h"
#include "machine/mtpr.h"
* SLMTU is a hard limit on input packet size. To simplify the code
* and improve performance, we require that packets fit in an mbuf
* cluster, that there be enough extra room for the ifnet pointer that
* IP input requires and, if we get a compressed packet, there's
* enough extra room to expand the header into a max length tcp/ip
* header (128 bytes). So, SLMTU can be at most
* MCLBYTES - sizeof(struct ifnet *) - 128
* To insure we get good interactive response, the MTU wants to be
* the smallest size that amortizes the header cost. (Remember
* that even with type-of-service queuing, we have to wait for any
* in-progress packet to finish. I.e., we wait, on the average,
* 1/2 * mtu / cps, where cps is the line speed in characters per
* second. E.g., 533ms wait for a 1024 byte MTU on a 9600 baud
* line. The average compressed header size is 6-8 bytes so any
* MTU > 90 bytes will give us 90% of the line bandwidth. A 100ms
* wait is tolerable (500ms is not), so want an MTU around 256.
* (Since TCP will send 212 byte segments (to allow for 40 byte
* headers), the typical packet size on the wire will be around 220
* bytes). In 4.3tahoe+ systems, we can set an MTU in a route
* so we do that & leave the interface MTU relatively high (so we
* don't IP fragment when acting as a gateway to someone using a
#define BUFOFFSET (128+sizeof(struct ifnet **))
#define SLIP_HIWAT 1024 /* don't start a new packet if HIWAT on queue */
#define CLISTRESERVE 1024 /* Can't let clists get too low */
* SLIP ABORT ESCAPE MECHANISM:
* (inspired by HAYES modem escape arrangement)
* 1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape }
* signals a "soft" exit from slip mode by usermode process
#define ABT_ESC '\033' /* can't be t_intr - distant host must know it*/
#define ABT_WAIT 1 /* in seconds - idle before an escape & after */
#define ABT_RECYCLE (5*2+2) /* in seconds - time window processing abort */
#define ABT_SOFT 3 /* count of escapes */
* The following disgusting hack gets around the problem that IP TOS
* can't be set in BSD/Sun OS yet. We want to put "interactive"
* traffic on a high priority queue. To decide if traffic is
* interactive, we check that a) it is TCP and b) one of it's ports
* if telnet, rlogin or ftp control.
static u_short interactive_ports
[8] = {
#define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
struct sl_softc sl_softc
[NSL
];
#define FRAME_END 0xc0 /* Frame End */
#define FRAME_ESCAPE 0xdb /* Frame Esc */
#define TRANS_FRAME_END 0xdc /* transposed frame end */
#define TRANS_FRAME_ESCAPE 0xdd /* transposed frame esc */
int sloutput(), slioctl(), ttrstrt();
* Called from boot code to establish sl interfaces.
register struct sl_softc
*sc
;
for (sc
= sl_softc
; i
< NSL
; sc
++) {
sc
->sc_if
.if_name
= "sl";
sc
->sc_if
.if_mtu
= SLMTU
;
sc
->sc_if
.if_flags
= IFF_POINTOPOINT
;
sc
->sc_if
.if_ioctl
= slioctl
;
sc
->sc_if
.if_output
= sloutput
;
sc
->sc_if
.if_snd
.ifq_maxlen
= 50;
sc
->sc_fastq
.ifq_maxlen
= 32;
register struct sl_softc
*sc
;
if (sc
->sc_ep
== (u_char
*) 0) {
sc
->sc_ep
= (u_char
*)p
+ (BUFOFFSET
+ SLMTU
);
printf("sl%d: can't allocate buffer\n", sc
- sl_softc
);
sc
->sc_if
.if_flags
&= ~IFF_UP
;
sc
->sc_buf
= sc
->sc_ep
- SLMTU
;
sl_compress_init(&sc
->sc_comp
);
* Line specific open routine.
* Attach the given tty to the first available sl unit.
register struct sl_softc
*sc
;
if (error
= suser(u
.u_cred
, &u
.u_acflag
))
if (tp
->t_line
== SLIPDISC
)
for (nsl
= NSL
, sc
= sl_softc
; --nsl
>= 0; sc
++)
if (sc
->sc_ttyp
== NULL
) {
ttyflush(tp
, FREAD
| FWRITE
);
* Line specific close routine.
* Detach the tty from the sl unit.
* Mimics part of ttyclose().
register struct sl_softc
*sc
;
s
= splimp(); /* actually, max(spltty, splnet) */
sc
= (struct sl_softc
*)tp
->t_sc
;
MCLFREE((struct mbuf
*)(sc
->sc_ep
- (SLMTU
+ BUFOFFSET
)));
* Line specific (tty) ioctl routine.
* Provide a way to get the sl unit number.
sltioctl(tp
, cmd
, data
, flag
)
struct sl_softc
*sc
= (struct sl_softc
*)tp
->t_sc
;
*(int *)data
= sc
->sc_if
.if_unit
;
*(int *)data
= sc
->sc_flags
;
#define SC_MASK (SC_COMPRESS|SC_NOICMP)
(sc
->sc_flags
&~ SC_MASK
) | ((*(int *)data
) & SC_MASK
);
* Queue a packet. Start transmission if not active.
register struct ifnet
*ifp
;
register struct sl_softc
*sc
;
register struct ifqueue
*ifq
;
* `Cannot happen' (see slioctl). Someday we will extend
* the line protocol to support other address families.
if (dst
->sa_family
!= AF_INET
) {
printf("sl%d: af%d not supported\n", ifp
->if_unit
,
sc
= &sl_softc
[ifp
->if_unit
];
if (sc
->sc_ttyp
== NULL
) {
return (ENETDOWN
); /* sort of */
if ((sc
->sc_ttyp
->t_state
& TS_CARR_ON
) == 0) {
if ((ip
= mtod(m
, struct ip
*))->ip_p
== IPPROTO_TCP
) {
register int p
= ((int *)ip
)[ip
->ip_hl
];
if (INTERACTIVE(p
& 0xffff) || INTERACTIVE(p
>> 16))
if (sc
->sc_flags
& SC_COMPRESS
) {
/* if two copies of sl_compress_tcp are running
* for the same line, the compression state can
* get screwed up. We're assuming that sloutput
* was invoked at splnet so this isn't possible
* (this assumption is correct for 4.xbsd, x<=4).
* In a multi-threaded kernel, a lockout might
p
= sl_compress_tcp(m
, ip
, &sc
->sc_comp
);
} else if (sc
->sc_flags
& SC_NOICMP
&& ip
->ip_p
== IPPROTO_ICMP
) {
if (sc
->sc_ttyp
->t_outq
.c_cc
== 0) {
* Start output on interface. Get another datagram
* to send from the interface queue and map it to
* the interface before starting output.
register struct sl_softc
*sc
= (struct sl_softc
*)tp
->t_sc
;
* If there is more in the output queue, just send it now.
* We are being called in lieu of ttstart and must do what
if (tp
->t_outq
.c_cc
!= 0) {
if (tp
->t_outq
.c_cc
> SLIP_HIWAT
)
* This happens briefly when the line shuts down.
* Get a packet and send it to the interface.
IF_DEQUEUE(&sc
->sc_fastq
, m
);
IF_DEQUEUE(&sc
->sc_if
.if_snd
, m
);
* If system is getting low on clists, just flush our
* output queue (if the stuff was important, it'll get
if (cfreecount
< CLISTRESERVE
+ SLMTU
) {
sc
->sc_if
.if_collisions
++;
* The extra FRAME_END will start up a new packet, and thus
* will flush any accumulated garbage. We do this whenever
* the line may have been idle for some time.
if (tp
->t_outq
.c_cc
== 0) {
(void) putc(FRAME_END
, &tp
->t_outq
);
cp
= mtod(m
, u_char
*); ep
= cp
+ m
->m_len
;
* Find out how many bytes in the string we can
* handle without doing something special.
register u_char
*bp
= cp
;
* Put n characters at once
* into the tty output queue.
if (b_to_q((char *)bp
, cp
- bp
, &tp
->t_outq
))
sc
->sc_bytessent
+= cp
- bp
;
* If there are characters left in the mbuf,
* the first one must be special..
* Put it out in a different form.
if (putc(FRAME_ESCAPE
, &tp
->t_outq
))
if (putc(*cp
++ == FRAME_ESCAPE
?
TRANS_FRAME_ESCAPE
: TRANS_FRAME_END
,
(void) unputc(&tp
->t_outq
);
if (putc(FRAME_END
, &tp
->t_outq
)) {
* Not enough room. Remove a char to make room
* and end the packet normally.
* If you get many collisions (more than one or two
* a day) you probably do not have enough clists
* and you should increase "nclist" in param.c.
(void) unputc(&tp
->t_outq
);
(void) putc(FRAME_END
, &tp
->t_outq
);
sc
->sc_if
.if_collisions
++;
* Copy data buffer to mbuf chain; add ifnet pointer.
register struct sl_softc
*sc
;
MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
* If we have more than MLEN bytes, it's cheaper to
* queue the cluster we just filled & allocate a new one
* for the input buffer. Otherwise, fill the mbuf we
* allocated above. Note that code in the input routine
* guarantees that packet will fit in a cluster.
if ((m
->m_flags
& M_EXT
) == 0) {
/* we couldn't get a cluster - if memory's this
* low, it's time to start dropping packets. */
sc
->sc_ep
= mtod(m
, u_char
*) + (BUFOFFSET
+ SLMTU
);
bcopy((caddr_t
)cp
, mtod(m
, caddr_t
), len
);
m
->m_pkthdr
.rcvif
= &sc
->sc_if
;
* tty interface receiver interrupt.
register struct sl_softc
*sc
;
sc
= (struct sl_softc
*)tp
->t_sc
;
if (!(tp
->t_state
&TS_CARR_ON
)) /* XXX */
if (sc
->sc_flags
& SC_ABORT
) {
/* if we see an abort after "idle" time, count it */
if (c
== ABT_ESC
&& time
.tv_sec
>= sc
->sc_lasttime
+ ABT_WAIT
) {
/* record when the first abort escape arrived */
if (sc
->sc_abortcount
== 1)
sc
->sc_starttime
= time
.tv_sec
;
* if we have an abort, see that we have not run out of time,
* or that we have an "idle" time after the complete escape
if (time
.tv_sec
>= sc
->sc_starttime
+ ABT_RECYCLE
)
if (sc
->sc_abortcount
>= ABT_SOFT
&&
time
.tv_sec
>= sc
->sc_lasttime
+ ABT_WAIT
) {
sc
->sc_lasttime
= time
.tv_sec
;
len
= sc
->sc_mp
- sc
->sc_buf
;
/* less than min length packet - ignore */
if ((c
= (*sc
->sc_buf
& 0xf0)) != (IPVERSION
<< 4)) {
else if (c
== TYPE_UNCOMPRESSED_TCP
)
*sc
->sc_buf
&= 0x4f; /* XXX */
len
= sl_uncompress_tcp(&sc
->sc_buf
, len
, (u_int
)c
,
if (IF_QFULL(&ipintrq
)) {
if (sc
->sc_mp
< sc
->sc_ep
) {
sc
->sc_mp
= sc
->sc_buf
= sc
->sc_ep
- SLMTU
;
* Process an ioctl request.
register struct ifnet
*ifp
;
register struct ifaddr
*ifa
= (struct ifaddr
*)data
;
int s
= splimp(), error
= 0;
if (ifa
->ifa_addr
->sa_family
== AF_INET
)
if (ifa
->ifa_addr
->sa_family
!= AF_INET
)