4 * Copyright (c) 1987, 1989, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * 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.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)if_sl.c 8.6 (Berkeley) 2/1/94
32 * $FreeBSD: src/sys/net/if_sl.c,v 1.84.2.2 2002/02/13 00:43:10 dillon Exp $
33 * $DragonFly: src/sys/net/sl/if_sl.c,v 1.32 2008/05/14 11:59:23 sephe Exp $
37 * Serial Line interface
40 * Center for Seismic Studies
41 * 1300 N 17th Street, Suite 1450
42 * Arlington, Virginia 22209
47 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
48 * N.B.: this belongs in netinet, not net, the way it stands now.
49 * Should have a link-layer type designation, but wouldn't be
50 * backwards-compatible.
52 * Converted to 4.3BSD Beta by Chris Torek.
53 * Other changes made at Berkeley, based in part on code by Kirk Smith.
54 * W. Jolitz added slip abort.
56 * Hacked almost beyond recognition by Van Jacobson (van@helios.ee.lbl.gov).
57 * Added priority queuing for "interactive" traffic; hooks for TCP
58 * header compression; ICMP filtering (at 2400 baud, some cretin
59 * pinging you can use up all your bandwidth). Made low clist behavior
60 * more robust and slightly less likely to hang serial line.
61 * Sped up a bunch of things.
67 #if !defined(KLD_MODULE)
70 #include <sys/param.h>
71 #include <sys/systm.h>
74 #include <sys/malloc.h>
76 #include <sys/dkstat.h>
77 #include <sys/socket.h>
78 #include <sys/sockio.h>
79 #include <sys/fcntl.h>
80 #include <sys/signalvar.h>
82 #include <sys/clist.h>
83 #include <sys/kernel.h>
85 #include <sys/thread2.h>
88 #include <net/if_types.h>
89 #include <net/ifq_var.h>
90 #include <net/netisr.h>
93 #include <netinet/in.h>
94 #include <netinet/in_systm.h>
95 #include <netinet/in_var.h>
96 #include <netinet/ip.h>
98 #error "Huh? Slip without inet?"
101 #include <net/slcompress.h>
102 #include "if_slvar.h"
103 #include <net/slip.h>
107 static void slattach (void *);
108 PSEUDO_SET(slattach, if_sl);
111 * SLRMAX is a hard limit on input packet size. To simplify the code
112 * and improve performance, we require that packets fit in an mbuf
113 * cluster, and if we get a compressed packet, there's enough extra
114 * room to expand the header into a max length tcp/ip header (128
115 * bytes). So, SLRMAX can be at most
118 * SLMTU is the default transmit MTU. The transmit MTU should be kept
119 * small enough so that interactive use doesn't suffer, but large
120 * enough to provide good performance. 552 is a good choice for SLMTU
121 * because it is high enough to not fragment TCP packets being routed
122 * through this host. Packet fragmentation is bad with SLIP because
123 * fragment headers aren't compressed. The previous assumptions about
124 * the best MTU value don't really hold when using modern modems with
125 * BTLZ data compression because the modem buffers play a much larger
126 * role in interactive performance than the MTU. The MTU can be changed
127 * at any time to suit the specific environment with ifconfig(8), and
128 * its maximum value is defined as SLTMAX. SLTMAX must not be so large
129 * that it would overflow the stack if BPF is configured (XXX; if_ppp.c
130 * handles this better).
132 * SLIP_HIWAT is the amount of data that will be queued 'downstream'
133 * of us (i.e., in clists waiting to be picked up by the tty output
134 * interrupt). If we queue a lot of data downstream, it's immune to
135 * our t.o.s. queuing.
136 * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed
137 * telnet/ftp will see a 1 sec wait, independent of the mtu (the
138 * wait is dependent on the ftp window size but that's typically
139 * 1k - 4k). So, we want SLIP_HIWAT just big enough to amortize
140 * the cost (in idle time on the wire) of the tty driver running
141 * off the end of its clists & having to call back slstart for a
142 * new packet. For a tty interface with any buffering at all, this
143 * cost will be zero. Even with a totally brain dead interface (like
144 * the one on a typical workstation), the cost will be <= 1 character
145 * time. So, setting SLIP_HIWAT to ~100 guarantees that we'll lose
146 * at most 1% while maintaining good interactive response.
148 #define BUFOFFSET (128+sizeof(struct ifnet **)+SLIP_HDRLEN)
149 #define SLRMAX (MCLBYTES - BUFOFFSET)
150 #define SLBUFSIZE (SLRMAX + BUFOFFSET)
152 #define SLMTU 552 /* default MTU */
154 #define SLTMAX 1500 /* maximum MTU */
155 #define SLIP_HIWAT roundup(50,CBSIZE)
156 #define CLISTRESERVE 1024 /* Can't let clists get too low */
159 * SLIP ABORT ESCAPE MECHANISM:
160 * (inspired by HAYES modem escape arrangement)
161 * 1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape }
162 * within window time signals a "soft" exit from slip mode by remote end
163 * if the IFF_DEBUG flag is on.
165 #define ABT_ESC '\033' /* can't be t_intr - distant host must know it*/
166 #define ABT_IDLE 1 /* in seconds - idle before an escape */
167 #define ABT_COUNT 3 /* count of escapes for abort */
168 #define ABT_WINDOW (ABT_COUNT*2+2) /* in seconds - time to count */
170 static struct sl_softc sl_softc[NSL];
172 #define FRAME_END 0xc0 /* Frame End */
173 #define FRAME_ESCAPE 0xdb /* Frame Esc */
174 #define TRANS_FRAME_END 0xdc /* transposed frame end */
175 #define TRANS_FRAME_ESCAPE 0xdd /* transposed frame esc */
177 static int slinit (struct sl_softc *);
178 static struct mbuf *sl_btom (struct sl_softc *, int);
179 static timeout_t sl_keepalive;
180 static timeout_t sl_outfill;
181 static int slclose (struct tty *,int);
182 static int slinput (int, struct tty *);
183 static int slioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
184 static int sltioctl (struct tty *, u_long, caddr_t, int, struct ucred *);
185 static int slopen (cdev_t, struct tty *);
186 static int sloutput (struct ifnet *,
187 struct mbuf *, struct sockaddr *, struct rtentry *);
188 static int slstart (struct tty *);
190 static struct linesw slipdisc = {
191 slopen, slclose, l_noread, l_nowrite,
192 sltioctl, slinput, slstart, ttymodem,
197 * Called from boot code to establish sl interfaces.
200 slattach(void *dummy)
205 lwkt_gettoken(&tty_token);
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_mp(&sc->sc_oftimeout);
226 callout_init_mp(&sc->sc_katimeout);
227 if_attach(&sc->sc_if, NULL);
228 bpfattach(&sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
230 lwkt_reltoken(&tty_token);
234 slinit(struct sl_softc *sc)
236 lwkt_gettoken(&tty_token);
237 if (sc->sc_ep == NULL)
238 sc->sc_ep = kmalloc(SLBUFSIZE, M_DEVBUF, M_WAITOK);
239 sc->sc_buf = sc->sc_ep + SLBUFSIZE - SLRMAX;
240 sc->sc_mp = sc->sc_buf;
241 sl_compress_init(&sc->sc_comp, -1);
242 lwkt_reltoken(&tty_token);
247 * Line specific open routine.
248 * Attach the given tty to the first available sl unit.
252 slopen(cdev_t dev, struct tty *tp)
257 struct thread *td = curthread; /* XXX */
259 error = priv_check(td, PRIV_ROOT);
263 lwkt_gettoken(&tty_token);
264 if (tp->t_line == SLIPDISC) {
265 lwkt_reltoken(&tty_token);
269 for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++)
270 if (sc->sc_ttyp == NULL && !(sc->sc_flags & SC_STATIC)) {
271 if (slinit(sc) == 0) {
272 lwkt_reltoken(&tty_token);
275 tp->t_sc = (caddr_t)sc;
277 sc->sc_if.if_baudrate = tp->t_ospeed;
278 ttyflush(tp, FREAD | FWRITE);
280 tp->t_line = SLIPDISC;
282 * We don't use t_canq or t_rawq, so reduce their
283 * cblock resources to 0. Reserve enough cblocks
284 * for t_outq to guarantee that we can fit a full
285 * packet if the SLIP_HIWAT check allows slstart()
286 * to loop. Use the same value for the cblock
287 * limit since the reserved blocks should always
288 * be enough. Reserving cblocks probably makes
289 * the CLISTRESERVE check unnecessary and wasteful.
291 clist_alloc_cblocks(&tp->t_canq, 0, 0);
292 clist_alloc_cblocks(&tp->t_outq,
293 SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1,
294 SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1);
295 clist_alloc_cblocks(&tp->t_rawq, 0, 0);
300 lwkt_reltoken(&tty_token);
303 lwkt_reltoken(&tty_token);
308 * Line specific close routine.
309 * Detach the tty from the sl unit.
312 slclose(struct tty *tp, int flag)
316 lwkt_gettoken(&tty_token);
317 ttyflush(tp, FREAD | FWRITE);
320 clist_free_cblocks(&tp->t_outq);
322 sc = (struct sl_softc *)tp->t_sc;
324 if (sc->sc_outfill) {
326 callout_stop(&sc->sc_oftimeout);
328 if (sc->sc_keepalive) {
329 sc->sc_keepalive = 0;
330 callout_stop(&sc->sc_katimeout);
333 sc->sc_flags &= SC_STATIC;
337 kfree(sc->sc_ep, M_DEVBUF);
344 lwkt_reltoken(&tty_token);
349 * Line specific (tty) ioctl routine.
350 * Provide a way to get the sl unit number.
354 sltioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct ucred *cred)
356 struct sl_softc *sc = (struct sl_softc *)tp->t_sc, *nc, *tmpnc;
360 lwkt_gettoken(&tty_token);
364 *(int *)data = sc->sc_if.if_dunit;
368 if (sc->sc_if.if_dunit != *(u_int *)data) {
369 for (nsl = NSL, nc = sl_softc; --nsl >= 0; nc++) {
370 if ( nc->sc_if.if_dunit == *(u_int *)data
371 && nc->sc_ttyp == NULL
373 tmpnc = kmalloc(sizeof *tmpnc, M_TEMP,
377 nc->sc_if = tmpnc->sc_if;
378 tmpnc->sc_if = sc->sc_if;
380 kfree(tmpnc, M_TEMP);
381 if (sc->sc_if.if_flags & IFF_UP) {
383 if (!(nc->sc_if.if_flags & IFF_UP))
385 } else if (nc->sc_if.if_flags & IFF_UP)
387 sc->sc_flags &= ~SC_STATIC;
388 sc->sc_flags |= (nc->sc_flags & SC_STATIC);
390 clist_alloc_cblocks(&tp->t_outq,
391 SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1,
392 SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1);
393 sl_compress_init(&sc->sc_comp, -1);
397 lwkt_reltoken(&tty_token);
402 sc->sc_flags |= SC_STATIC;
406 sc->sc_keepalive = *(u_int *)data * hz;
407 if (sc->sc_keepalive) {
408 sc->sc_flags |= SC_KEEPALIVE;
409 callout_reset(&sc->sc_katimeout, sc->sc_keepalive,
412 if ((sc->sc_flags & SC_KEEPALIVE) != 0) {
413 callout_stop(&sc->sc_katimeout);
414 sc->sc_flags &= ~SC_KEEPALIVE;
420 *(int *)data = sc->sc_keepalive / hz;
424 sc->sc_outfill = *(u_int *)data * hz;
425 if (sc->sc_outfill) {
426 sc->sc_flags |= SC_OUTWAIT;
427 callout_reset(&sc->sc_oftimeout, sc->sc_outfill,
430 if ((sc->sc_flags & SC_OUTWAIT) != 0) {
431 callout_stop(&sc->sc_oftimeout);
432 sc->sc_flags &= ~SC_OUTWAIT;
438 *(int *)data = sc->sc_outfill / hz;
442 lwkt_reltoken(&tty_token);
446 lwkt_reltoken(&tty_token);
452 * Queue a packet. Start transmission if not active.
453 * Compression happens in slstart; if we do it here, IP TOS
454 * will cause us to not compress "background" packets, because
455 * ordering gets trashed. It can be done for all packets in slstart.
458 sloutput_serialized(struct ifnet *ifp, struct ifaltq_subque *ifsq,
459 struct mbuf *m, struct sockaddr *dst, struct rtentry *rtp)
461 struct sl_softc *sc = &sl_softc[ifp->if_dunit];
464 struct altq_pktattr pktattr;
466 ifq_classify(&ifp->if_snd, m, dst->sa_family, &pktattr);
469 * `Cannot happen' (see slioctl). Someday we will extend
470 * the line protocol to support other address families.
472 if (dst->sa_family != AF_INET) {
473 kprintf("%s: af%d not supported\n", sc->sc_if.if_xname,
476 IFNET_STAT_INC(&sc->sc_if, noproto, 1);
477 return (EAFNOSUPPORT);
480 if (sc->sc_ttyp == NULL || !(ifp->if_flags & IFF_UP)) {
484 if ((sc->sc_ttyp->t_state & TS_CONNECTED) == 0) {
486 return (EHOSTUNREACH);
488 ip = mtod(m, struct ip *);
489 if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
491 return (ENETRESET); /* XXX ? */
496 if ((ip->ip_tos & IPTOS_LOWDELAY) && !ifq_is_enabled(&sc->sc_if.if_snd)) {
497 if (IF_QFULL(&sc->sc_fastq)) {
498 IF_DROP(&sc->sc_fastq);
502 IF_ENQUEUE(&sc->sc_fastq, m);
506 error = ifsq_enqueue(ifsq, m, &pktattr);
509 IFNET_STAT_INC(&sc->sc_if, oerrors, 1);
513 if (sc->sc_ttyp->t_outq.c_cc == 0)
514 slstart(sc->sc_ttyp);
520 sloutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
523 struct ifaltq_subque *ifsq = ifq_get_subq_default(&ifp->if_snd);
526 ifsq_serialize_hw(ifsq);
527 error = sloutput_serialized(ifp, ifsq, m, dst, rtp);
528 ifsq_deserialize_hw(ifsq);
534 * Start output on interface. Get another datagram
535 * to send from the interface queue and map it to
536 * the interface before starting output.
539 slstart(struct tty *tp)
541 struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
542 struct ifaltq_subque *ifsq = ifq_get_subq_default(&sc->sc_if.if_snd);
546 u_char bpfbuf[SLTMAX + SLIP_HDRLEN];
549 lwkt_gettoken(&tty_token);
552 * Call output process whether or not there is more in the
553 * output queue. We are being called in lieu of ttstart
554 * and must do what it would.
558 if (tp->t_outq.c_cc != 0) {
560 sc->sc_flags &= ~SC_OUTWAIT;
561 if (tp->t_outq.c_cc > SLIP_HIWAT) {
562 lwkt_reltoken(&tty_token);
568 * This happens briefly when the line shuts down.
571 lwkt_reltoken(&tty_token);
576 * Get a packet and send it to the interface.
579 IF_DEQUEUE(&sc->sc_fastq, m);
581 IFNET_STAT_INC(&sc->sc_if, omcasts, 1); /* XXX */
583 m = ifsq_dequeue(ifsq);
586 lwkt_reltoken(&tty_token);
591 * We do the header compression here rather than in sloutput
592 * because the packets will be out of order if we are using TOS
593 * queueing, and the connection id compression will get
594 * munged when this happens.
596 if (sc->sc_if.if_bpf) {
598 * We need to save the TCP/IP header before it's
599 * compressed. To avoid complicated code, we just
600 * copy the entire packet into a stack buffer (since
601 * this is a serial line, packets should be short
602 * and/or the copy should be negligible cost compared
603 * to the packet transmission time).
606 u_char *cp = bpfbuf + SLIP_HDRLEN;
610 int mlen = m1->m_len;
612 bcopy(mtod(m1, caddr_t), cp, mlen);
615 } while ((m1 = m1->m_next) != NULL);
617 ip = mtod(m, struct ip *);
618 if (ip->ip_v == IPVERSION && ip->ip_p == IPPROTO_TCP) {
619 if (sc->sc_if.if_flags & SC_COMPRESS)
620 *mtod(m, u_char *) |= sl_compress_tcp(m, ip,
623 if (sc->sc_if.if_bpf) {
626 if (sc->sc_if.if_bpf) {
628 * Put the SLIP pseudo-"link header" in place.
629 * The compressed header is now at the
630 * beginning of the mbuf.
632 bpfbuf[SLX_DIR] = SLIPDIR_OUT;
633 bcopy(mtod(m, caddr_t), &bpfbuf[SLX_CHDR],
635 bpf_tap(sc->sc_if.if_bpf, bpfbuf,
643 * If system is getting low on clists, just flush our
644 * output queue (if the stuff was important, it'll get
645 * retransmitted). Note that SLTMAX is used instead of
646 * the current if_mtu setting because connections that
647 * have already been established still use the original
648 * (possibly larger) mss.
650 if (cfreecount < CLISTRESERVE + SLTMAX) {
652 IFNET_STAT_INC(&sc->sc_if, collisions, 1);
656 sc->sc_flags &= ~SC_OUTWAIT;
658 * The extra FRAME_END will start up a new packet, and thus
659 * will flush any accumulated garbage. We do this whenever
660 * the line may have been idle for some time.
662 if (tp->t_outq.c_cc == 0) {
663 IFNET_STAT_INC(&sc->sc_if, obytes, 1);
664 clist_putc(FRAME_END, &tp->t_outq);
670 cp = mtod(m, u_char *); ep = cp + m->m_len;
673 * Find out how many bytes in the string we can
674 * handle without doing something special.
689 * Put n characters at once
690 * into the tty output queue.
692 if (b_to_q((char *)bp, cp - bp,
695 IFNET_STAT_INC(&sc->sc_if, obytes,
699 * If there are characters left in the mbuf,
700 * the first one must be special..
701 * Put it out in a different form.
704 if (clist_putc(FRAME_ESCAPE, &tp->t_outq))
706 if (clist_putc(*cp++ == FRAME_ESCAPE ?
707 TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
709 clist_unputc(&tp->t_outq);
712 IFNET_STAT_INC(&sc->sc_if, obytes, 2);
718 if (clist_putc(FRAME_END, &tp->t_outq)) {
720 * Not enough room. Remove a char to make room
721 * and end the packet normally.
722 * If you get many collisions (more than one or two
723 * a day) you probably do not have enough clists
724 * and you should increase "nclist" in param.c.
726 clist_unputc(&tp->t_outq);
727 clist_putc(FRAME_END, &tp->t_outq);
728 IFNET_STAT_INC(&sc->sc_if, collisions, 1);
730 IFNET_STAT_INC(&sc->sc_if, obytes, 1);
731 IFNET_STAT_INC(&sc->sc_if, opackets, 1);
734 lwkt_reltoken(&tty_token);
739 * Copy data buffer to mbuf chain; add ifnet pointer.
742 sl_btom(struct sl_softc *sc, int len)
749 MGETHDR(m, MB_DONTWAIT, MT_DATA);
754 * If we have more than MHLEN bytes, it's cheaper to
755 * queue the cluster we just filled & allocate a new one
756 * for the input buffer. Otherwise, fill the mbuf we
757 * allocated above. Note that code in the input routine
758 * guarantees that packet will fit in a cluster.
761 MCLGET(m, MB_DONTWAIT);
762 if ((m->m_flags & M_EXT) == 0) {
764 * we couldn't get a cluster - if memory's this
765 * low, it's time to start dropping packets.
771 bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len);
773 m->m_pkthdr.len = len;
774 m->m_pkthdr.rcvif = &sc->sc_if;
779 * tty interface receiver interrupt.
782 slinput(int c, struct tty *tp)
787 u_char chdr[CHDR_LEN];
789 lwkt_gettoken(&tty_token);
791 sc = (struct sl_softc *)tp->t_sc;
793 lwkt_reltoken(&tty_token);
796 if (c & TTY_ERRORMASK || (tp->t_state & TS_CONNECTED) == 0) {
797 sc->sc_flags |= SC_ERROR;
798 lwkt_reltoken(&tty_token);
803 IFNET_STAT_INC(&sc->sc_if, ibytes, 1);
805 if (sc->sc_if.if_flags & IFF_DEBUG) {
808 * If we have a previous abort, see whether
809 * this one is within the time limit.
811 if (sc->sc_abortcount &&
812 time_uptime >= sc->sc_starttime + ABT_WINDOW)
813 sc->sc_abortcount = 0;
815 * If we see an abort after "idle" time, count it;
816 * record when the first abort escape arrived.
818 if (time_uptime >= sc->sc_lasttime + ABT_IDLE) {
819 if (++sc->sc_abortcount == 1)
820 sc->sc_starttime = time_uptime;
821 if (sc->sc_abortcount >= ABT_COUNT) {
823 lwkt_reltoken(&tty_token);
828 sc->sc_abortcount = 0;
829 sc->sc_lasttime = time_uptime;
834 case TRANS_FRAME_ESCAPE:
839 case TRANS_FRAME_END:
846 lwkt_reltoken(&tty_token);
850 sc->sc_flags &= ~SC_KEEPALIVE;
851 if(sc->sc_flags & SC_ERROR) {
852 sc->sc_flags &= ~SC_ERROR;
855 len = sc->sc_mp - sc->sc_buf;
857 /* less than min length packet - ignore */
860 if (sc->sc_if.if_bpf) {
862 * Save the compressed header, so we
863 * can tack it on later. Note that we
864 * will end up copying garbage in some
865 * cases but this is okay. We remember
866 * where the buffer started so we can
867 * compute the new header length.
869 bcopy(sc->sc_buf, chdr, CHDR_LEN);
872 if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) {
874 c = TYPE_COMPRESSED_TCP;
875 else if (c == TYPE_UNCOMPRESSED_TCP)
876 *sc->sc_buf &= 0x4f; /* XXX */
878 * We've got something that's not an IP packet.
879 * If compression is enabled, try to decompress it.
880 * Otherwise, if `auto-enable' compression is on and
881 * it's a reasonable packet, decompress it and then
882 * enable compression. Otherwise, drop it.
884 if (sc->sc_if.if_flags & SC_COMPRESS) {
885 len = sl_uncompress_tcp(&sc->sc_buf, len,
886 (u_int)c, &sc->sc_comp);
889 } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
890 c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
891 len = sl_uncompress_tcp(&sc->sc_buf, len,
892 (u_int)c, &sc->sc_comp);
895 sc->sc_if.if_flags |= SC_COMPRESS;
899 if (sc->sc_if.if_bpf) {
902 if (sc->sc_if.if_bpf) {
904 * Put the SLIP pseudo-"link header" in place.
905 * We couldn't do this any earlier since
906 * decompression probably moved the buffer
907 * pointer. Then, invoke BPF.
909 u_char *hp = sc->sc_buf - SLIP_HDRLEN;
911 hp[SLX_DIR] = SLIPDIR_IN;
912 bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN);
913 bpf_tap(sc->sc_if.if_bpf, hp,
919 m = sl_btom(sc, len);
923 IFNET_STAT_INC(&sc->sc_if, ipackets, 1);
925 if ((sc->sc_if.if_flags & IFF_UP) == 0) {
930 if (netisr_queue(NETISR_IP, m)) {
931 IFNET_STAT_INC(&sc->sc_if, ierrors, 1);
932 IFNET_STAT_INC(&sc->sc_if, iqdrops, 1);
937 if (sc->sc_mp < sc->sc_ep + SLBUFSIZE) {
940 lwkt_reltoken(&tty_token);
944 /* can't put lower; would miss an extra frame */
945 sc->sc_flags |= SC_ERROR;
948 IFNET_STAT_INC(&sc->sc_if, ierrors, 1);
950 sc->sc_mp = sc->sc_buf = sc->sc_ep + SLBUFSIZE - SLRMAX;
952 lwkt_reltoken(&tty_token);
957 * Process an ioctl request.
960 slioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
962 struct ifaddr *ifa = (struct ifaddr *)data;
963 struct ifreq *ifr = (struct ifreq *)data;
972 * if.c will set the interface up even if we
975 if (sl_softc[ifp->if_dunit].sc_ttyp == NULL) {
976 ifp->if_flags &= ~IFF_UP;
981 * This is "historical" - set the interface up when
982 * setting the address.
984 if (ifa->ifa_addr->sa_family == AF_INET) {
985 if (sl_softc[ifp->if_dunit].sc_ttyp != NULL)
986 ifp->if_flags |= IFF_UP;
988 error = EAFNOSUPPORT;
993 if (ifa->ifa_addr->sa_family != AF_INET)
994 error = EAFNOSUPPORT;
1003 * Set the interface MTU.
1005 if (ifr->ifr_mtu > SLTMAX)
1010 ifp->if_mtu = ifr->ifr_mtu;
1011 tp = sl_softc[ifp->if_dunit].sc_ttyp;
1013 clist_alloc_cblocks(&tp->t_outq,
1014 SLIP_HIWAT + 2 * ifp->if_mtu + 1,
1015 SLIP_HIWAT + 2 * ifp->if_mtu + 1);
1028 sl_keepalive(void *chan)
1030 struct sl_softc *sc = chan;
1033 lwkt_gettoken(&tty_token);
1034 if (sc->sc_keepalive) {
1035 if (sc->sc_flags & SC_KEEPALIVE) {
1036 pg = sc->sc_ttyp->t_pgrp;
1039 pgsignal (pg, SIGURG, 1);
1043 sc->sc_flags |= SC_KEEPALIVE;
1045 callout_reset(&sc->sc_katimeout, sc->sc_keepalive,
1048 sc->sc_flags &= ~SC_KEEPALIVE;
1050 lwkt_reltoken(&tty_token);
1054 sl_outfill(void *chan)
1056 struct sl_softc *sc = chan;
1057 struct tty *tp = sc->sc_ttyp;
1059 lwkt_gettoken(&tty_token);
1061 if (sc->sc_outfill && tp != NULL) {
1062 if (sc->sc_flags & SC_OUTWAIT) {
1064 IFNET_STAT_INC(&sc->sc_if, obytes, 1);
1065 clist_putc(FRAME_END, &tp->t_outq);
1069 sc->sc_flags |= SC_OUTWAIT;
1070 callout_reset(&sc->sc_oftimeout, sc->sc_outfill,
1073 sc->sc_flags &= ~SC_OUTWAIT;
1075 lwkt_reltoken(&tty_token);