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 $
36 * Serial Line interface
39 * Center for Seismic Studies
40 * 1300 N 17th Street, Suite 1450
41 * Arlington, Virginia 22209
46 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
47 * N.B.: this belongs in netinet, not net, the way it stands now.
48 * Should have a link-layer type designation, but wouldn't be
49 * backwards-compatible.
51 * Converted to 4.3BSD Beta by Chris Torek.
52 * Other changes made at Berkeley, based in part on code by Kirk Smith.
53 * W. Jolitz added slip abort.
55 * Hacked almost beyond recognition by Van Jacobson (van@helios.ee.lbl.gov).
56 * Added priority queuing for "interactive" traffic; hooks for TCP
57 * header compression; ICMP filtering (at 2400 baud, some cretin
58 * pinging you can use up all your bandwidth). Made low clist behavior
59 * more robust and slightly less likely to hang serial line.
60 * Sped up a bunch of things.
66 #include <sys/param.h>
67 #include <sys/systm.h>
70 #include <sys/malloc.h>
72 #include <sys/dkstat.h>
73 #include <sys/socket.h>
74 #include <sys/sockio.h>
75 #include <sys/fcntl.h>
76 #include <sys/signalvar.h>
78 #include <sys/kernel.h>
79 #include <sys/sysctl.h>
83 #include <net/if_types.h>
84 #include <net/ifq_var.h>
85 #include <net/netisr.h>
88 #include <netinet/in.h>
89 #include <netinet/in_systm.h>
90 #include <netinet/in_var.h>
91 #include <netinet/ip.h>
93 #error "Huh? Slip without inet?"
96 #include <net/slcompress.h>
102 static void slattach (void *);
103 PSEUDO_SET(slattach, if_sl);
105 /* Set to 0xC002 for broadcast instead of p-to-p */
106 static int sliffopts = IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST;
107 SYSCTL_INT(_net, OID_AUTO, sliffopts, CTLFLAG_RW,
109 TUNABLE_INT("net.sliffopts", &sliffopts);
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 50
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;
211 sc->sc_if.if_flags = sliffopts;
212 sc->sc_if.if_type = IFT_SLIP;
213 sc->sc_if.if_ioctl = slioctl;
214 sc->sc_if.if_output = sloutput;
215 ifq_set_maxlen(&sc->sc_if.if_snd, 50);
216 ifq_set_ready(&sc->sc_if.if_snd);
217 sc->sc_fastq.ifq_maxlen = 32;
218 sc->sc_if.if_linkmib = sc;
219 sc->sc_if.if_linkmiblen = sizeof *sc;
220 callout_init_mp(&sc->sc_oftimeout);
221 callout_init_mp(&sc->sc_katimeout);
222 if_attach(&sc->sc_if, NULL);
223 bpfattach(&sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
228 slinit(struct sl_softc *sc)
230 if (sc->sc_ep == NULL)
231 sc->sc_ep = kmalloc(SLBUFSIZE, M_DEVBUF, M_WAITOK);
232 sc->sc_buf = sc->sc_ep + SLBUFSIZE - SLRMAX;
233 sc->sc_mp = sc->sc_buf;
234 sl_compress_init(&sc->sc_comp, -1);
240 * Line specific open routine.
241 * Attach the given tty to the first available sl unit.
245 slopen(cdev_t dev, struct tty *tp)
251 error = caps_priv_check_self(SYSCAP_RESTRICTEDROOT);
255 lwkt_gettoken(&tty_token);
256 if (tp->t_line == SLIPDISC) {
257 lwkt_reltoken(&tty_token);
261 for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++) {
262 if (sc->sc_ttyp == NULL && !(sc->sc_flags & SC_STATIC)) {
263 if (slinit(sc) == 0) {
264 lwkt_reltoken(&tty_token);
267 tp->t_slsc = (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);
284 clist_alloc_cblocks(&tp->t_outq,
285 SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1);
286 clist_alloc_cblocks(&tp->t_rawq, 0);
289 lwkt_reltoken(&tty_token);
293 lwkt_reltoken(&tty_token);
299 * Line specific close routine.
300 * Detach the tty from the sl unit.
303 slclose(struct tty *tp, int flag)
307 lwkt_gettoken(&tty_token);
308 lwkt_gettoken(&tp->t_token);
309 ttyflush(tp, FREAD | FWRITE);
311 clist_free_cblocks(&tp->t_outq);
313 sc = (struct sl_softc *)tp->t_slsc;
315 if (sc->sc_outfill) {
317 callout_stop(&sc->sc_oftimeout);
319 if (sc->sc_keepalive) {
320 sc->sc_keepalive = 0;
321 callout_stop(&sc->sc_katimeout);
324 sc->sc_flags &= SC_STATIC;
328 kfree(sc->sc_ep, M_DEVBUF);
334 lwkt_reltoken(&tp->t_token);
335 lwkt_reltoken(&tty_token);
341 * Line specific (tty) ioctl routine.
342 * Provide a way to get the sl unit number.
346 sltioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct ucred *cred)
348 struct sl_softc *sc = (struct sl_softc *)tp->t_slsc, *nc, *tmpnc;
351 lwkt_gettoken(&tty_token);
352 lwkt_gettoken(&tp->t_token);
356 *(int *)data = sc->sc_if.if_dunit;
360 if (sc->sc_if.if_dunit != *(u_int *)data) {
361 for (nsl = NSL, nc = sl_softc; --nsl >= 0; nc++) {
362 if ( nc->sc_if.if_dunit == *(u_int *)data
363 && nc->sc_ttyp == NULL
365 tmpnc = kmalloc(sizeof *tmpnc, M_TEMP,
369 nc->sc_if = tmpnc->sc_if;
370 tmpnc->sc_if = sc->sc_if;
372 kfree(tmpnc, M_TEMP);
373 if (sc->sc_if.if_flags & IFF_UP) {
375 if (!(nc->sc_if.if_flags & IFF_UP))
377 } else if (nc->sc_if.if_flags & IFF_UP)
379 sc->sc_flags &= ~SC_STATIC;
380 sc->sc_flags |= (nc->sc_flags & SC_STATIC);
381 tp->t_slsc = sc = nc;
382 clist_alloc_cblocks(&tp->t_outq,
384 2 * sc->sc_if.if_mtu + 1);
385 sl_compress_init(&sc->sc_comp, -1);
389 lwkt_reltoken(&tp->t_token);
390 lwkt_reltoken(&tty_token);
394 sc->sc_flags |= SC_STATIC;
398 sc->sc_keepalive = *(u_int *)data * hz;
399 if (sc->sc_keepalive) {
400 sc->sc_flags |= SC_KEEPALIVE;
401 callout_reset(&sc->sc_katimeout, sc->sc_keepalive,
404 if ((sc->sc_flags & SC_KEEPALIVE) != 0) {
405 callout_stop(&sc->sc_katimeout);
406 sc->sc_flags &= ~SC_KEEPALIVE;
412 *(int *)data = sc->sc_keepalive / hz;
416 sc->sc_outfill = *(u_int *)data * hz;
417 if (sc->sc_outfill) {
418 sc->sc_flags |= SC_OUTWAIT;
419 callout_reset(&sc->sc_oftimeout, sc->sc_outfill,
422 if ((sc->sc_flags & SC_OUTWAIT) != 0) {
423 callout_stop(&sc->sc_oftimeout);
424 sc->sc_flags &= ~SC_OUTWAIT;
430 *(int *)data = sc->sc_outfill / hz;
434 lwkt_reltoken(&tp->t_token);
435 lwkt_reltoken(&tty_token);
438 lwkt_reltoken(&tp->t_token);
439 lwkt_reltoken(&tty_token);
445 * Queue a packet. Start transmission if not active.
446 * Compression happens in slstart; if we do it here, IP TOS
447 * will cause us to not compress "background" packets, because
448 * ordering gets trashed. It can be done for all packets in slstart.
451 sloutput_serialized(struct ifnet *ifp, struct ifaltq_subque *ifsq,
452 struct mbuf *m, struct sockaddr *dst, struct rtentry *rtp)
454 struct sl_softc *sc = &sl_softc[ifp->if_dunit];
457 struct altq_pktattr pktattr;
459 ifq_classify(&ifp->if_snd, m, dst->sa_family, &pktattr);
462 * `Cannot happen' (see slioctl). Someday we will extend
463 * the line protocol to support other address families.
465 if (dst->sa_family != AF_INET) {
466 kprintf("%s: af%d not supported\n", sc->sc_if.if_xname,
469 IFNET_STAT_INC(&sc->sc_if, noproto, 1);
470 return (EAFNOSUPPORT);
473 if (sc->sc_ttyp == NULL || !(ifp->if_flags & IFF_UP)) {
477 if ((sc->sc_ttyp->t_state & TS_CONNECTED) == 0) {
479 return (EHOSTUNREACH);
481 ip = mtod(m, struct ip *);
482 if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
484 return (ENETRESET); /* XXX ? */
487 if ((ip->ip_tos & IPTOS_LOWDELAY) &&
488 !ifq_is_enabled(&sc->sc_if.if_snd)) {
489 if (IF_QFULL(&sc->sc_fastq)) {
490 IF_DROP(&sc->sc_fastq);
494 IF_ENQUEUE(&sc->sc_fastq, m);
498 error = ifsq_enqueue(ifsq, m, &pktattr);
501 IFNET_STAT_INC(&sc->sc_if, oqdrops, 1);
504 if (sc->sc_ttyp->t_outq.c_cc == 0)
505 slstart(sc->sc_ttyp);
510 sloutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
513 struct ifaltq_subque *ifsq = ifq_get_subq_default(&ifp->if_snd);
516 ifsq_serialize_hw(ifsq);
517 error = sloutput_serialized(ifp, ifsq, m, dst, rtp);
518 ifsq_deserialize_hw(ifsq);
524 * Start output on interface. Get another datagram
525 * to send from the interface queue and map it to
526 * the interface before starting output.
529 slstart(struct tty *tp)
531 struct sl_softc *sc = (struct sl_softc *)tp->t_slsc;
532 struct ifaltq_subque *ifsq = ifq_get_subq_default(&sc->sc_if.if_snd);
536 u_char bpfbuf[SLTMAX + SLIP_HDRLEN];
539 lwkt_gettoken(&tp->t_token);
542 * Call output process whether or not there is more in the
543 * output queue. We are being called in lieu of ttstart
544 * and must do what it would.
548 if (tp->t_outq.c_cc != 0) {
550 sc->sc_flags &= ~SC_OUTWAIT;
551 if (tp->t_outq.c_cc > SLIP_HIWAT) {
552 lwkt_reltoken(&tp->t_token);
558 * This happens briefly when the line shuts down.
561 lwkt_reltoken(&tp->t_token);
566 * Get a packet and send it to the interface.
568 IF_DEQUEUE(&sc->sc_fastq, m);
570 IFNET_STAT_INC(&sc->sc_if, omcasts, 1); /* XXX */
572 m = ifsq_dequeue(ifsq);
574 lwkt_reltoken(&tp->t_token);
579 * We do the header compression here rather than in sloutput
580 * because the packets will be out of order if we are using TOS
581 * queueing, and the connection id compression will get
582 * munged when this happens.
584 if (sc->sc_if.if_bpf) {
586 * We need to save the TCP/IP header before it's
587 * compressed. To avoid complicated code, we just
588 * copy the entire packet into a stack buffer (since
589 * this is a serial line, packets should be short
590 * and/or the copy should be negligible cost compared
591 * to the packet transmission time).
594 u_char *cp = bpfbuf + SLIP_HDRLEN;
598 int mlen = m1->m_len;
600 bcopy(mtod(m1, caddr_t), cp, mlen);
603 } while ((m1 = m1->m_next) != NULL);
605 ip = mtod(m, struct ip *);
606 if (ip->ip_v == IPVERSION && ip->ip_p == IPPROTO_TCP) {
607 if (sc->sc_if.if_flags & SC_COMPRESS)
608 *mtod(m, u_char *) |= sl_compress_tcp(m, ip,
611 if (sc->sc_if.if_bpf) {
614 if (sc->sc_if.if_bpf) {
616 * Put the SLIP pseudo-"link header" in place.
617 * The compressed header is now at the
618 * beginning of the mbuf.
620 bpfbuf[SLX_DIR] = SLIPDIR_OUT;
621 bcopy(mtod(m, caddr_t), &bpfbuf[SLX_CHDR],
623 bpf_tap(sc->sc_if.if_bpf, bpfbuf,
630 sc->sc_flags &= ~SC_OUTWAIT;
633 * The extra FRAME_END will start up a new packet, and thus
634 * will flush any accumulated garbage. We do this whenever
635 * the line may have been idle for some time.
637 if (tp->t_outq.c_cc == 0) {
638 IFNET_STAT_INC(&sc->sc_if, obytes, 1);
639 clist_putc(FRAME_END, &tp->t_outq);
645 cp = mtod(m, u_char *); ep = cp + m->m_len;
648 * Find out how many bytes in the string we can
649 * handle without doing something special.
664 * Put n characters at once
665 * into the tty output queue.
667 if (clist_btoq((char *)bp, cp - bp,
671 IFNET_STAT_INC(&sc->sc_if, obytes,
675 * If there are characters left in the mbuf,
676 * the first one must be special..
677 * Put it out in a different form.
680 if (clist_putc(FRAME_ESCAPE, &tp->t_outq))
682 if (clist_putc(*cp++ == FRAME_ESCAPE ?
683 TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
685 clist_unputc(&tp->t_outq);
688 IFNET_STAT_INC(&sc->sc_if, obytes, 2);
694 if (clist_putc(FRAME_END, &tp->t_outq)) {
696 * Not enough room. Remove a char to make room
697 * and end the packet normally.
698 * If you get many collisions (more than one or two
699 * a day) you probably do not have enough clists
700 * and you should increase "nclist" in param.c.
702 clist_unputc(&tp->t_outq);
703 clist_putc(FRAME_END, &tp->t_outq);
704 IFNET_STAT_INC(&sc->sc_if, collisions, 1);
706 IFNET_STAT_INC(&sc->sc_if, obytes, 1);
707 IFNET_STAT_INC(&sc->sc_if, opackets, 1);
710 lwkt_reltoken(&tp->t_token);
716 * Copy data buffer to mbuf chain; add ifnet pointer.
719 sl_btom(struct sl_softc *sc, int len)
726 MGETHDR(m, M_NOWAIT, MT_DATA);
731 * If we have more than MHLEN bytes, it's cheaper to
732 * queue the cluster we just filled & allocate a new one
733 * for the input buffer. Otherwise, fill the mbuf we
734 * allocated above. Note that code in the input routine
735 * guarantees that packet will fit in a cluster.
739 if ((m->m_flags & M_EXT) == 0) {
741 * we couldn't get a cluster - if memory's this
742 * low, it's time to start dropping packets.
748 bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len);
750 m->m_pkthdr.len = len;
751 m->m_pkthdr.rcvif = &sc->sc_if;
756 * tty interface receiver interrupt.
759 slinput(int c, struct tty *tp)
764 u_char chdr[CHDR_LEN];
767 kprintf(" %02x", (unsigned char)c);
768 if ((unsigned char)c == 0xC0)
772 lwkt_gettoken(&tp->t_token);
774 sc = (struct sl_softc *)tp->t_slsc;
776 lwkt_reltoken(&tp->t_token);
782 if (c & TTY_ERRORMASK || (tp->t_state & TS_CONNECTED) == 0) {
783 sc->sc_flags |= SC_ERROR;
784 lwkt_reltoken(&tp->t_token);
792 IFNET_STAT_INC(&sc->sc_if, ibytes, 1);
794 if (sc->sc_if.if_flags & IFF_DEBUG) {
797 * If we have a previous abort, see whether
798 * this one is within the time limit.
800 if (sc->sc_abortcount &&
801 time_uptime >= sc->sc_starttime + ABT_WINDOW)
802 sc->sc_abortcount = 0;
804 * If we see an abort after "idle" time, count it;
805 * record when the first abort escape arrived.
807 if (time_uptime >= sc->sc_lasttime + ABT_IDLE) {
808 if (++sc->sc_abortcount == 1)
809 sc->sc_starttime = time_uptime;
810 if (sc->sc_abortcount >= ABT_COUNT) {
812 lwkt_reltoken(&tp->t_token);
817 sc->sc_abortcount = 0;
818 sc->sc_lasttime = time_uptime;
823 case TRANS_FRAME_ESCAPE:
828 case TRANS_FRAME_END:
835 lwkt_reltoken(&tp->t_token);
839 sc->sc_flags &= ~SC_KEEPALIVE;
840 if(sc->sc_flags & SC_ERROR) {
841 sc->sc_flags &= ~SC_ERROR;
844 len = sc->sc_mp - sc->sc_buf;
846 /* less than min length packet - ignore */
849 if (sc->sc_if.if_bpf) {
851 * Save the compressed header, so we
852 * can tack it on later. Note that we
853 * will end up copying garbage in some
854 * cases but this is okay. We remember
855 * where the buffer started so we can
856 * compute the new header length.
858 bcopy(sc->sc_buf, chdr, CHDR_LEN);
861 if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) {
863 c = TYPE_COMPRESSED_TCP;
864 else if (c == TYPE_UNCOMPRESSED_TCP)
865 *sc->sc_buf &= 0x4f; /* XXX */
867 * We've got something that's not an IP packet.
868 * If compression is enabled, try to decompress it.
869 * Otherwise, if `auto-enable' compression is on and
870 * it's a reasonable packet, decompress it and then
871 * enable compression. Otherwise, drop it.
873 if (sc->sc_if.if_flags & SC_COMPRESS) {
874 len = sl_uncompress_tcp(&sc->sc_buf, len,
875 (u_int)c, &sc->sc_comp);
878 } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
879 c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
880 len = sl_uncompress_tcp(&sc->sc_buf, len,
881 (u_int)c, &sc->sc_comp);
884 sc->sc_if.if_flags |= SC_COMPRESS;
888 if (sc->sc_if.if_bpf) {
891 if (sc->sc_if.if_bpf) {
893 * Put the SLIP pseudo-"link header" in place.
894 * We couldn't do this any earlier since
895 * decompression probably moved the buffer
896 * pointer. Then, invoke BPF.
898 u_char *hp = sc->sc_buf - SLIP_HDRLEN;
900 hp[SLX_DIR] = SLIPDIR_IN;
901 bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN);
902 bpf_tap(sc->sc_if.if_bpf, hp,
908 m = sl_btom(sc, len);
912 IFNET_STAT_INC(&sc->sc_if, ipackets, 1);
914 if ((sc->sc_if.if_flags & IFF_UP) == 0) {
919 if (netisr_queue(NETISR_IP, m)) {
920 IFNET_STAT_INC(&sc->sc_if, ierrors, 1);
921 IFNET_STAT_INC(&sc->sc_if, iqdrops, 1);
926 if (sc->sc_mp < sc->sc_ep + SLBUFSIZE) {
929 lwkt_reltoken(&tp->t_token);
933 /* can't put lower; would miss an extra frame */
934 sc->sc_flags |= SC_ERROR;
937 IFNET_STAT_INC(&sc->sc_if, ierrors, 1);
939 sc->sc_mp = sc->sc_buf = sc->sc_ep + SLBUFSIZE - SLRMAX;
941 lwkt_reltoken(&tp->t_token);
947 * Process an ioctl request.
950 slioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
952 struct ifaddr *ifa = (struct ifaddr *)data;
953 struct ifreq *ifr = (struct ifreq *)data;
960 * if.c will set the interface up even if we
963 if (sl_softc[ifp->if_dunit].sc_ttyp == NULL) {
964 ifp->if_flags &= ~IFF_UP;
969 * This is "historical" - set the interface up when
970 * setting the address.
972 if (ifa->ifa_addr->sa_family == AF_INET) {
973 if (sl_softc[ifp->if_dunit].sc_ttyp != NULL)
974 ifp->if_flags |= IFF_UP;
976 error = EAFNOSUPPORT;
981 if (ifa->ifa_addr->sa_family != AF_INET)
982 error = EAFNOSUPPORT;
991 * Set the interface MTU.
993 if (ifr->ifr_mtu > SLTMAX)
998 ifp->if_mtu = ifr->ifr_mtu;
999 tp = sl_softc[ifp->if_dunit].sc_ttyp;
1001 clist_alloc_cblocks(&tp->t_outq,
1002 SLIP_HIWAT + 2 * ifp->if_mtu + 1);
1013 sl_keepalive(void *chan)
1015 struct sl_softc *sc = chan;
1016 struct tty *tp = sc->sc_ttyp;
1019 lwkt_gettoken(&tp->t_token);
1020 if (sc->sc_keepalive) {
1021 if (sc->sc_flags & SC_KEEPALIVE) {
1022 pg = sc->sc_ttyp->t_pgrp;
1025 pgsignal (pg, SIGURG, 1);
1029 sc->sc_flags |= SC_KEEPALIVE;
1031 callout_reset(&sc->sc_katimeout, sc->sc_keepalive,
1034 sc->sc_flags &= ~SC_KEEPALIVE;
1036 lwkt_reltoken(&tp->t_token);
1040 sl_outfill(void *chan)
1042 struct sl_softc *sc = chan;
1043 struct tty *tp = sc->sc_ttyp;
1045 lwkt_gettoken(&tp->t_token);
1047 if (sc->sc_outfill && tp != NULL) {
1048 if (sc->sc_flags & SC_OUTWAIT) {
1049 IFNET_STAT_INC(&sc->sc_if, obytes, 1);
1050 clist_putc(FRAME_END, &tp->t_outq);
1053 sc->sc_flags |= SC_OUTWAIT;
1054 callout_reset(&sc->sc_oftimeout, sc->sc_outfill,
1057 sc->sc_flags &= ~SC_OUTWAIT;
1059 lwkt_reltoken(&tp->t_token);
1062 MODULE_VERSION(if_sl, 1);