2 * Copyright (c) 1987, 1989, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)if_sl.c 8.6 (Berkeley) 2/1/94
34 * $FreeBSD: src/sys/net/if_sl.c,v 1.84.2.2 2002/02/13 00:43:10 dillon Exp $
35 * $DragonFly: src/sys/net/sl/if_sl.c,v 1.32 2008/05/14 11:59:23 sephe Exp $
39 * Serial Line interface
42 * Center for Seismic Studies
43 * 1300 N 17th Street, Suite 1450
44 * Arlington, Virginia 22209
49 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
50 * N.B.: this belongs in netinet, not net, the way it stands now.
51 * Should have a link-layer type designation, but wouldn't be
52 * backwards-compatible.
54 * Converted to 4.3BSD Beta by Chris Torek.
55 * Other changes made at Berkeley, based in part on code by Kirk Smith.
56 * W. Jolitz added slip abort.
58 * Hacked almost beyond recognition by Van Jacobson (van@helios.ee.lbl.gov).
59 * Added priority queuing for "interactive" traffic; hooks for TCP
60 * header compression; ICMP filtering (at 2400 baud, some cretin
61 * pinging you can use up all your bandwidth). Made low clist behavior
62 * more robust and slightly less likely to hang serial line.
63 * Sped up a bunch of things.
69 #if !defined(KLD_MODULE)
72 #include <sys/param.h>
73 #include <sys/systm.h>
75 #include <sys/malloc.h>
77 #include <sys/dkstat.h>
78 #include <sys/socket.h>
79 #include <sys/sockio.h>
80 #include <sys/fcntl.h>
81 #include <sys/signalvar.h>
83 #include <sys/clist.h>
84 #include <sys/kernel.h>
86 #include <sys/thread2.h>
89 #include <net/if_types.h>
90 #include <net/ifq_var.h>
91 #include <net/netisr.h>
94 #include <netinet/in.h>
95 #include <netinet/in_systm.h>
96 #include <netinet/in_var.h>
97 #include <netinet/ip.h>
99 #error "Huh? Slip without inet?"
102 #include <net/slcompress.h>
103 #include "if_slvar.h"
104 #include <net/slip.h>
108 static void slattach (void *);
109 PSEUDO_SET(slattach
, if_sl
);
112 * SLRMAX is a hard limit on input packet size. To simplify the code
113 * and improve performance, we require that packets fit in an mbuf
114 * cluster, and if we get a compressed packet, there's enough extra
115 * room to expand the header into a max length tcp/ip header (128
116 * bytes). So, SLRMAX can be at most
119 * SLMTU is the default transmit MTU. The transmit MTU should be kept
120 * small enough so that interactive use doesn't suffer, but large
121 * enough to provide good performance. 552 is a good choice for SLMTU
122 * because it is high enough to not fragment TCP packets being routed
123 * through this host. Packet fragmentation is bad with SLIP because
124 * fragment headers aren't compressed. The previous assumptions about
125 * the best MTU value don't really hold when using modern modems with
126 * BTLZ data compression because the modem buffers play a much larger
127 * role in interactive performance than the MTU. The MTU can be changed
128 * at any time to suit the specific environment with ifconfig(8), and
129 * its maximum value is defined as SLTMAX. SLTMAX must not be so large
130 * that it would overflow the stack if BPF is configured (XXX; if_ppp.c
131 * handles this better).
133 * SLIP_HIWAT is the amount of data that will be queued 'downstream'
134 * of us (i.e., in clists waiting to be picked up by the tty output
135 * interrupt). If we queue a lot of data downstream, it's immune to
136 * our t.o.s. queuing.
137 * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed
138 * telnet/ftp will see a 1 sec wait, independent of the mtu (the
139 * wait is dependent on the ftp window size but that's typically
140 * 1k - 4k). So, we want SLIP_HIWAT just big enough to amortize
141 * the cost (in idle time on the wire) of the tty driver running
142 * off the end of its clists & having to call back slstart for a
143 * new packet. For a tty interface with any buffering at all, this
144 * cost will be zero. Even with a totally brain dead interface (like
145 * the one on a typical workstation), the cost will be <= 1 character
146 * time. So, setting SLIP_HIWAT to ~100 guarantees that we'll lose
147 * at most 1% while maintaining good interactive response.
149 #define BUFOFFSET (128+sizeof(struct ifnet **)+SLIP_HDRLEN)
150 #define SLRMAX (MCLBYTES - BUFOFFSET)
151 #define SLBUFSIZE (SLRMAX + BUFOFFSET)
153 #define SLMTU 552 /* default MTU */
155 #define SLTMAX 1500 /* maximum MTU */
156 #define SLIP_HIWAT roundup(50,CBSIZE)
157 #define CLISTRESERVE 1024 /* Can't let clists get too low */
160 * SLIP ABORT ESCAPE MECHANISM:
161 * (inspired by HAYES modem escape arrangement)
162 * 1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape }
163 * within window time signals a "soft" exit from slip mode by remote end
164 * if the IFF_DEBUG flag is on.
166 #define ABT_ESC '\033' /* can't be t_intr - distant host must know it*/
167 #define ABT_IDLE 1 /* in seconds - idle before an escape */
168 #define ABT_COUNT 3 /* count of escapes for abort */
169 #define ABT_WINDOW (ABT_COUNT*2+2) /* in seconds - time to count */
171 static struct sl_softc sl_softc
[NSL
];
173 #define FRAME_END 0xc0 /* Frame End */
174 #define FRAME_ESCAPE 0xdb /* Frame Esc */
175 #define TRANS_FRAME_END 0xdc /* transposed frame end */
176 #define TRANS_FRAME_ESCAPE 0xdd /* transposed frame esc */
178 static int slinit (struct sl_softc
*);
179 static struct mbuf
*sl_btom (struct sl_softc
*, int);
180 static timeout_t sl_keepalive
;
181 static timeout_t sl_outfill
;
182 static int slclose (struct tty
*,int);
183 static int slinput (int, struct tty
*);
184 static int slioctl (struct ifnet
*, u_long
, caddr_t
, struct ucred
*);
185 static int sltioctl (struct tty
*, u_long
, caddr_t
, int, struct ucred
*);
186 static int slopen (cdev_t
, struct tty
*);
187 static int sloutput (struct ifnet
*,
188 struct mbuf
*, struct sockaddr
*, struct rtentry
*);
189 static int slstart (struct tty
*);
191 static struct linesw slipdisc
= {
192 slopen
, slclose
, l_noread
, l_nowrite
,
193 sltioctl
, slinput
, slstart
, ttymodem
,
198 * Called from boot code to establish sl interfaces.
201 slattach(void *dummy
)
206 linesw
[SLIPDISC
] = slipdisc
;
208 for (sc
= sl_softc
; i
< NSL
; sc
++) {
209 if_initname(&(sc
->sc_if
), "sl", i
++);
210 sc
->sc_if
.if_mtu
= SLMTU
;
215 IFF_POINTOPOINT
| SC_AUTOCOMP
| IFF_MULTICAST
;
217 sc
->sc_if
.if_type
= IFT_SLIP
;
218 sc
->sc_if
.if_ioctl
= slioctl
;
219 sc
->sc_if
.if_output
= sloutput
;
220 ifq_set_maxlen(&sc
->sc_if
.if_snd
, 50);
221 ifq_set_ready(&sc
->sc_if
.if_snd
);
222 sc
->sc_fastq
.ifq_maxlen
= 32;
223 sc
->sc_if
.if_linkmib
= sc
;
224 sc
->sc_if
.if_linkmiblen
= sizeof *sc
;
225 callout_init(&sc
->sc_oftimeout
);
226 callout_init(&sc
->sc_katimeout
);
227 if_attach(&sc
->sc_if
, NULL
);
228 bpfattach(&sc
->sc_if
, DLT_SLIP
, SLIP_HDRLEN
);
233 slinit(struct sl_softc
*sc
)
235 if (sc
->sc_ep
== NULL
)
236 sc
->sc_ep
= kmalloc(SLBUFSIZE
, M_DEVBUF
, M_WAITOK
);
237 sc
->sc_buf
= sc
->sc_ep
+ SLBUFSIZE
- SLRMAX
;
238 sc
->sc_mp
= sc
->sc_buf
;
239 sl_compress_init(&sc
->sc_comp
, -1);
244 * Line specific open routine.
245 * Attach the given tty to the first available sl unit.
249 slopen(cdev_t dev
, struct tty
*tp
)
254 struct thread
*td
= curthread
; /* XXX */
260 if (tp
->t_line
== SLIPDISC
)
263 for (nsl
= NSL
, sc
= sl_softc
; --nsl
>= 0; sc
++)
264 if (sc
->sc_ttyp
== NULL
&& !(sc
->sc_flags
& SC_STATIC
)) {
267 tp
->t_sc
= (caddr_t
)sc
;
269 sc
->sc_if
.if_baudrate
= tp
->t_ospeed
;
270 ttyflush(tp
, FREAD
| FWRITE
);
272 tp
->t_line
= SLIPDISC
;
274 * We don't use t_canq or t_rawq, so reduce their
275 * cblock resources to 0. Reserve enough cblocks
276 * for t_outq to guarantee that we can fit a full
277 * packet if the SLIP_HIWAT check allows slstart()
278 * to loop. Use the same value for the cblock
279 * limit since the reserved blocks should always
280 * be enough. Reserving cblocks probably makes
281 * the CLISTRESERVE check unnecessary and wasteful.
283 clist_alloc_cblocks(&tp
->t_canq
, 0, 0);
284 clist_alloc_cblocks(&tp
->t_outq
,
285 SLIP_HIWAT
+ 2 * sc
->sc_if
.if_mtu
+ 1,
286 SLIP_HIWAT
+ 2 * sc
->sc_if
.if_mtu
+ 1);
287 clist_alloc_cblocks(&tp
->t_rawq
, 0, 0);
298 * Line specific close routine.
299 * Detach the tty from the sl unit.
302 slclose(struct tty
*tp
, int flag
)
306 ttyflush(tp
, FREAD
| FWRITE
);
309 clist_free_cblocks(&tp
->t_outq
);
311 sc
= (struct sl_softc
*)tp
->t_sc
;
313 if (sc
->sc_outfill
) {
315 callout_stop(&sc
->sc_oftimeout
);
317 if (sc
->sc_keepalive
) {
318 sc
->sc_keepalive
= 0;
319 callout_stop(&sc
->sc_katimeout
);
322 sc
->sc_flags
&= SC_STATIC
;
326 kfree(sc
->sc_ep
, M_DEVBUF
);
337 * Line specific (tty) ioctl routine.
338 * Provide a way to get the sl unit number.
342 sltioctl(struct tty
*tp
, u_long cmd
, caddr_t data
, int flag
, struct ucred
*cred
)
344 struct sl_softc
*sc
= (struct sl_softc
*)tp
->t_sc
, *nc
, *tmpnc
;
351 *(int *)data
= sc
->sc_if
.if_dunit
;
355 if (sc
->sc_if
.if_dunit
!= *(u_int
*)data
) {
356 for (nsl
= NSL
, nc
= sl_softc
; --nsl
>= 0; nc
++) {
357 if ( nc
->sc_if
.if_dunit
== *(u_int
*)data
358 && nc
->sc_ttyp
== NULL
360 tmpnc
= kmalloc(sizeof *tmpnc
, M_TEMP
,
364 nc
->sc_if
= tmpnc
->sc_if
;
365 tmpnc
->sc_if
= sc
->sc_if
;
367 kfree(tmpnc
, M_TEMP
);
368 if (sc
->sc_if
.if_flags
& IFF_UP
) {
370 if (!(nc
->sc_if
.if_flags
& IFF_UP
))
372 } else if (nc
->sc_if
.if_flags
& IFF_UP
)
374 sc
->sc_flags
&= ~SC_STATIC
;
375 sc
->sc_flags
|= (nc
->sc_flags
& SC_STATIC
);
377 clist_alloc_cblocks(&tp
->t_outq
,
378 SLIP_HIWAT
+ 2 * sc
->sc_if
.if_mtu
+ 1,
379 SLIP_HIWAT
+ 2 * sc
->sc_if
.if_mtu
+ 1);
380 sl_compress_init(&sc
->sc_comp
, -1);
388 sc
->sc_flags
|= SC_STATIC
;
392 sc
->sc_keepalive
= *(u_int
*)data
* hz
;
393 if (sc
->sc_keepalive
) {
394 sc
->sc_flags
|= SC_KEEPALIVE
;
395 callout_reset(&sc
->sc_katimeout
, sc
->sc_keepalive
,
398 if ((sc
->sc_flags
& SC_KEEPALIVE
) != 0) {
399 callout_stop(&sc
->sc_katimeout
);
400 sc
->sc_flags
&= ~SC_KEEPALIVE
;
406 *(int *)data
= sc
->sc_keepalive
/ hz
;
410 sc
->sc_outfill
= *(u_int
*)data
* hz
;
411 if (sc
->sc_outfill
) {
412 sc
->sc_flags
|= SC_OUTWAIT
;
413 callout_reset(&sc
->sc_oftimeout
, sc
->sc_outfill
,
416 if ((sc
->sc_flags
& SC_OUTWAIT
) != 0) {
417 callout_stop(&sc
->sc_oftimeout
);
418 sc
->sc_flags
&= ~SC_OUTWAIT
;
424 *(int *)data
= sc
->sc_outfill
/ hz
;
436 * Queue a packet. Start transmission if not active.
437 * Compression happens in slstart; if we do it here, IP TOS
438 * will cause us to not compress "background" packets, because
439 * ordering gets trashed. It can be done for all packets in slstart.
442 sloutput_serialized(struct ifnet
*ifp
, struct mbuf
*m
, struct sockaddr
*dst
,
445 struct sl_softc
*sc
= &sl_softc
[ifp
->if_dunit
];
448 struct altq_pktattr pktattr
;
450 ifq_classify(&ifp
->if_snd
, m
, dst
->sa_family
, &pktattr
);
453 * `Cannot happen' (see slioctl). Someday we will extend
454 * the line protocol to support other address families.
456 if (dst
->sa_family
!= AF_INET
) {
457 kprintf("%s: af%d not supported\n", sc
->sc_if
.if_xname
,
460 sc
->sc_if
.if_noproto
++;
461 return (EAFNOSUPPORT
);
464 if (sc
->sc_ttyp
== NULL
|| !(ifp
->if_flags
& IFF_UP
)) {
468 if ((sc
->sc_ttyp
->t_state
& TS_CONNECTED
) == 0) {
470 return (EHOSTUNREACH
);
472 ip
= mtod(m
, struct ip
*);
473 if (sc
->sc_if
.if_flags
& SC_NOICMP
&& ip
->ip_p
== IPPROTO_ICMP
) {
475 return (ENETRESET
); /* XXX ? */
480 if ((ip
->ip_tos
& IPTOS_LOWDELAY
) && !ifq_is_enabled(&sc
->sc_if
.if_snd
)) {
481 if (IF_QFULL(&sc
->sc_fastq
)) {
482 IF_DROP(&sc
->sc_fastq
);
486 IF_ENQUEUE(&sc
->sc_fastq
, m
);
490 error
= ifq_enqueue(&sc
->sc_if
.if_snd
, m
, &pktattr
);
493 sc
->sc_if
.if_oerrors
++;
497 if (sc
->sc_ttyp
->t_outq
.c_cc
== 0)
498 slstart(sc
->sc_ttyp
);
504 sloutput(struct ifnet
*ifp
, struct mbuf
*m
, struct sockaddr
*dst
,
509 lwkt_serialize_enter(ifp
->if_serializer
);
510 error
= sloutput_serialized(ifp
, m
, dst
, rtp
);
511 lwkt_serialize_exit(ifp
->if_serializer
);
517 * Start output on interface. Get another datagram
518 * to send from the interface queue and map it to
519 * the interface before starting output.
522 slstart(struct tty
*tp
)
524 struct sl_softc
*sc
= (struct sl_softc
*)tp
->t_sc
;
528 u_char bpfbuf
[SLTMAX
+ SLIP_HDRLEN
];
533 * Call output process whether or not there is more in the
534 * output queue. We are being called in lieu of ttstart
535 * and must do what it would.
539 if (tp
->t_outq
.c_cc
!= 0) {
541 sc
->sc_flags
&= ~SC_OUTWAIT
;
542 if (tp
->t_outq
.c_cc
> SLIP_HIWAT
)
547 * This happens briefly when the line shuts down.
553 * Get a packet and send it to the interface.
556 IF_DEQUEUE(&sc
->sc_fastq
, m
);
558 sc
->sc_if
.if_omcasts
++; /* XXX */
560 m
= ifq_dequeue(&sc
->sc_if
.if_snd
, NULL
);
566 * We do the header compression here rather than in sloutput
567 * because the packets will be out of order if we are using TOS
568 * queueing, and the connection id compression will get
569 * munged when this happens.
571 if (sc
->sc_if
.if_bpf
) {
573 * We need to save the TCP/IP header before it's
574 * compressed. To avoid complicated code, we just
575 * copy the entire packet into a stack buffer (since
576 * this is a serial line, packets should be short
577 * and/or the copy should be negligible cost compared
578 * to the packet transmission time).
581 u_char
*cp
= bpfbuf
+ SLIP_HDRLEN
;
585 int mlen
= m1
->m_len
;
587 bcopy(mtod(m1
, caddr_t
), cp
, mlen
);
590 } while ((m1
= m1
->m_next
) != NULL
);
592 ip
= mtod(m
, struct ip
*);
593 if (ip
->ip_v
== IPVERSION
&& ip
->ip_p
== IPPROTO_TCP
) {
594 if (sc
->sc_if
.if_flags
& SC_COMPRESS
)
595 *mtod(m
, u_char
*) |= sl_compress_tcp(m
, ip
,
598 if (sc
->sc_if
.if_bpf
) {
600 * Put the SLIP pseudo-"link header" in place. The
601 * compressed header is now at the beginning of the
604 bpfbuf
[SLX_DIR
] = SLIPDIR_OUT
;
605 bcopy(mtod(m
, caddr_t
), &bpfbuf
[SLX_CHDR
], CHDR_LEN
);
606 bpf_tap(sc
->sc_if
.if_bpf
, bpfbuf
, len
+ SLIP_HDRLEN
);
610 * If system is getting low on clists, just flush our
611 * output queue (if the stuff was important, it'll get
612 * retransmitted). Note that SLTMAX is used instead of
613 * the current if_mtu setting because connections that
614 * have already been established still use the original
615 * (possibly larger) mss.
617 if (cfreecount
< CLISTRESERVE
+ SLTMAX
) {
619 sc
->sc_if
.if_collisions
++;
623 sc
->sc_flags
&= ~SC_OUTWAIT
;
625 * The extra FRAME_END will start up a new packet, and thus
626 * will flush any accumulated garbage. We do this whenever
627 * the line may have been idle for some time.
629 if (tp
->t_outq
.c_cc
== 0) {
630 ++sc
->sc_if
.if_obytes
;
631 clist_putc(FRAME_END
, &tp
->t_outq
);
637 cp
= mtod(m
, u_char
*); ep
= cp
+ m
->m_len
;
640 * Find out how many bytes in the string we can
641 * handle without doing something special.
656 * Put n characters at once
657 * into the tty output queue.
659 if (b_to_q((char *)bp
, cp
- bp
,
662 sc
->sc_if
.if_obytes
+= cp
- bp
;
665 * If there are characters left in the mbuf,
666 * the first one must be special..
667 * Put it out in a different form.
670 if (clist_putc(FRAME_ESCAPE
, &tp
->t_outq
))
672 if (clist_putc(*cp
++ == FRAME_ESCAPE
?
673 TRANS_FRAME_ESCAPE
: TRANS_FRAME_END
,
675 clist_unputc(&tp
->t_outq
);
678 sc
->sc_if
.if_obytes
+= 2;
684 if (clist_putc(FRAME_END
, &tp
->t_outq
)) {
686 * Not enough room. Remove a char to make room
687 * and end the packet normally.
688 * If you get many collisions (more than one or two
689 * a day) you probably do not have enough clists
690 * and you should increase "nclist" in param.c.
692 clist_unputc(&tp
->t_outq
);
693 clist_putc(FRAME_END
, &tp
->t_outq
);
694 sc
->sc_if
.if_collisions
++;
696 ++sc
->sc_if
.if_obytes
;
697 sc
->sc_if
.if_opackets
++;
704 * Copy data buffer to mbuf chain; add ifnet pointer.
707 sl_btom(struct sl_softc
*sc
, int len
)
714 MGETHDR(m
, MB_DONTWAIT
, MT_DATA
);
719 * If we have more than MHLEN bytes, it's cheaper to
720 * queue the cluster we just filled & allocate a new one
721 * for the input buffer. Otherwise, fill the mbuf we
722 * allocated above. Note that code in the input routine
723 * guarantees that packet will fit in a cluster.
726 MCLGET(m
, MB_DONTWAIT
);
727 if ((m
->m_flags
& M_EXT
) == 0) {
729 * we couldn't get a cluster - if memory's this
730 * low, it's time to start dropping packets.
736 bcopy((caddr_t
)sc
->sc_buf
, mtod(m
, caddr_t
), len
);
738 m
->m_pkthdr
.len
= len
;
739 m
->m_pkthdr
.rcvif
= &sc
->sc_if
;
744 * tty interface receiver interrupt.
747 slinput(int c
, struct tty
*tp
)
752 u_char chdr
[CHDR_LEN
];
755 sc
= (struct sl_softc
*)tp
->t_sc
;
758 if (c
& TTY_ERRORMASK
|| (tp
->t_state
& TS_CONNECTED
) == 0) {
759 sc
->sc_flags
|= SC_ERROR
;
764 ++sc
->sc_if
.if_ibytes
;
766 if (sc
->sc_if
.if_flags
& IFF_DEBUG
) {
769 * If we have a previous abort, see whether
770 * this one is within the time limit.
772 if (sc
->sc_abortcount
&&
773 time_second
>= sc
->sc_starttime
+ ABT_WINDOW
)
774 sc
->sc_abortcount
= 0;
776 * If we see an abort after "idle" time, count it;
777 * record when the first abort escape arrived.
779 if (time_second
>= sc
->sc_lasttime
+ ABT_IDLE
) {
780 if (++sc
->sc_abortcount
== 1)
781 sc
->sc_starttime
= time_second
;
782 if (sc
->sc_abortcount
>= ABT_COUNT
) {
788 sc
->sc_abortcount
= 0;
789 sc
->sc_lasttime
= time_second
;
794 case TRANS_FRAME_ESCAPE
:
799 case TRANS_FRAME_END
:
809 sc
->sc_flags
&= ~SC_KEEPALIVE
;
810 if(sc
->sc_flags
& SC_ERROR
) {
811 sc
->sc_flags
&= ~SC_ERROR
;
814 len
= sc
->sc_mp
- sc
->sc_buf
;
816 /* less than min length packet - ignore */
819 if (sc
->sc_if
.if_bpf
) {
821 * Save the compressed header, so we
822 * can tack it on later. Note that we
823 * will end up copying garbage in some
824 * cases but this is okay. We remember
825 * where the buffer started so we can
826 * compute the new header length.
828 bcopy(sc
->sc_buf
, chdr
, CHDR_LEN
);
831 if ((c
= (*sc
->sc_buf
& 0xf0)) != (IPVERSION
<< 4)) {
833 c
= TYPE_COMPRESSED_TCP
;
834 else if (c
== TYPE_UNCOMPRESSED_TCP
)
835 *sc
->sc_buf
&= 0x4f; /* XXX */
837 * We've got something that's not an IP packet.
838 * If compression is enabled, try to decompress it.
839 * Otherwise, if `auto-enable' compression is on and
840 * it's a reasonable packet, decompress it and then
841 * enable compression. Otherwise, drop it.
843 if (sc
->sc_if
.if_flags
& SC_COMPRESS
) {
844 len
= sl_uncompress_tcp(&sc
->sc_buf
, len
,
845 (u_int
)c
, &sc
->sc_comp
);
848 } else if ((sc
->sc_if
.if_flags
& SC_AUTOCOMP
) &&
849 c
== TYPE_UNCOMPRESSED_TCP
&& len
>= 40) {
850 len
= sl_uncompress_tcp(&sc
->sc_buf
, len
,
851 (u_int
)c
, &sc
->sc_comp
);
854 sc
->sc_if
.if_flags
|= SC_COMPRESS
;
858 if (sc
->sc_if
.if_bpf
) {
860 * Put the SLIP pseudo-"link header" in place.
861 * We couldn't do this any earlier since
862 * decompression probably moved the buffer
863 * pointer. Then, invoke BPF.
865 u_char
*hp
= sc
->sc_buf
- SLIP_HDRLEN
;
867 hp
[SLX_DIR
] = SLIPDIR_IN
;
868 bcopy(chdr
, &hp
[SLX_CHDR
], CHDR_LEN
);
869 bpf_tap(sc
->sc_if
.if_bpf
, hp
, len
+ SLIP_HDRLEN
);
871 m
= sl_btom(sc
, len
);
875 sc
->sc_if
.if_ipackets
++;
877 if ((sc
->sc_if
.if_flags
& IFF_UP
) == 0) {
882 if (netisr_queue(NETISR_IP
, m
)) {
883 sc
->sc_if
.if_ierrors
++;
884 sc
->sc_if
.if_iqdrops
++;
889 if (sc
->sc_mp
< sc
->sc_ep
+ SLBUFSIZE
) {
895 /* can't put lower; would miss an extra frame */
896 sc
->sc_flags
|= SC_ERROR
;
899 sc
->sc_if
.if_ierrors
++;
901 sc
->sc_mp
= sc
->sc_buf
= sc
->sc_ep
+ SLBUFSIZE
- SLRMAX
;
907 * Process an ioctl request.
910 slioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct ucred
*cr
)
912 struct ifaddr
*ifa
= (struct ifaddr
*)data
;
913 struct ifreq
*ifr
= (struct ifreq
*)data
;
922 * if.c will set the interface up even if we
925 if (sl_softc
[ifp
->if_dunit
].sc_ttyp
== NULL
) {
926 ifp
->if_flags
&= ~IFF_UP
;
931 * This is "historical" - set the interface up when
932 * setting the address.
934 if (ifa
->ifa_addr
->sa_family
== AF_INET
) {
935 if (sl_softc
[ifp
->if_dunit
].sc_ttyp
!= NULL
)
936 ifp
->if_flags
|= IFF_UP
;
938 error
= EAFNOSUPPORT
;
943 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
944 error
= EAFNOSUPPORT
;
953 * Set the interface MTU.
955 if (ifr
->ifr_mtu
> SLTMAX
)
960 ifp
->if_mtu
= ifr
->ifr_mtu
;
961 tp
= sl_softc
[ifp
->if_dunit
].sc_ttyp
;
963 clist_alloc_cblocks(&tp
->t_outq
,
964 SLIP_HIWAT
+ 2 * ifp
->if_mtu
+ 1,
965 SLIP_HIWAT
+ 2 * ifp
->if_mtu
+ 1);
978 sl_keepalive(void *chan
)
980 struct sl_softc
*sc
= chan
;
982 if (sc
->sc_keepalive
) {
983 if (sc
->sc_flags
& SC_KEEPALIVE
)
984 pgsignal (sc
->sc_ttyp
->t_pgrp
, SIGURG
, 1);
986 sc
->sc_flags
|= SC_KEEPALIVE
;
987 callout_reset(&sc
->sc_katimeout
, sc
->sc_keepalive
,
990 sc
->sc_flags
&= ~SC_KEEPALIVE
;
995 sl_outfill(void *chan
)
997 struct sl_softc
*sc
= chan
;
998 struct tty
*tp
= sc
->sc_ttyp
;
1000 if (sc
->sc_outfill
&& tp
!= NULL
) {
1001 if (sc
->sc_flags
& SC_OUTWAIT
) {
1003 ++sc
->sc_if
.if_obytes
;
1004 clist_putc(FRAME_END
, &tp
->t_outq
);
1008 sc
->sc_flags
|= SC_OUTWAIT
;
1009 callout_reset(&sc
->sc_oftimeout
, sc
->sc_outfill
,
1012 sc
->sc_flags
&= ~SC_OUTWAIT
;