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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#)if_sl.c 8.6 (Berkeley) 2/1/94
36 * $FreeBSD: src/sys/net/if_sl.c,v 1.84.2.2 2002/02/13 00:43:10 dillon Exp $
37 * $DragonFly: src/sys/net/sl/if_sl.c,v 1.32 2008/05/14 11:59:23 sephe Exp $
41 * Serial Line interface
44 * Center for Seismic Studies
45 * 1300 N 17th Street, Suite 1450
46 * Arlington, Virginia 22209
51 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
52 * N.B.: this belongs in netinet, not net, the way it stands now.
53 * Should have a link-layer type designation, but wouldn't be
54 * backwards-compatible.
56 * Converted to 4.3BSD Beta by Chris Torek.
57 * Other changes made at Berkeley, based in part on code by Kirk Smith.
58 * W. Jolitz added slip abort.
60 * Hacked almost beyond recognition by Van Jacobson (van@helios.ee.lbl.gov).
61 * Added priority queuing for "interactive" traffic; hooks for TCP
62 * header compression; ICMP filtering (at 2400 baud, some cretin
63 * pinging you can use up all your bandwidth). Made low clist behavior
64 * more robust and slightly less likely to hang serial line.
65 * Sped up a bunch of things.
71 #if !defined(KLD_MODULE)
74 #include <sys/param.h>
75 #include <sys/systm.h>
78 #include <sys/malloc.h>
80 #include <sys/dkstat.h>
81 #include <sys/socket.h>
82 #include <sys/sockio.h>
83 #include <sys/fcntl.h>
84 #include <sys/signalvar.h>
86 #include <sys/clist.h>
87 #include <sys/kernel.h>
89 #include <sys/thread2.h>
92 #include <net/if_types.h>
93 #include <net/ifq_var.h>
94 #include <net/netisr.h>
97 #include <netinet/in.h>
98 #include <netinet/in_systm.h>
99 #include <netinet/in_var.h>
100 #include <netinet/ip.h>
102 #error "Huh? Slip without inet?"
105 #include <net/slcompress.h>
106 #include "if_slvar.h"
107 #include <net/slip.h>
111 static void slattach (void *);
112 PSEUDO_SET(slattach, if_sl);
115 * SLRMAX is a hard limit on input packet size. To simplify the code
116 * and improve performance, we require that packets fit in an mbuf
117 * cluster, and if we get a compressed packet, there's enough extra
118 * room to expand the header into a max length tcp/ip header (128
119 * bytes). So, SLRMAX can be at most
122 * SLMTU is the default transmit MTU. The transmit MTU should be kept
123 * small enough so that interactive use doesn't suffer, but large
124 * enough to provide good performance. 552 is a good choice for SLMTU
125 * because it is high enough to not fragment TCP packets being routed
126 * through this host. Packet fragmentation is bad with SLIP because
127 * fragment headers aren't compressed. The previous assumptions about
128 * the best MTU value don't really hold when using modern modems with
129 * BTLZ data compression because the modem buffers play a much larger
130 * role in interactive performance than the MTU. The MTU can be changed
131 * at any time to suit the specific environment with ifconfig(8), and
132 * its maximum value is defined as SLTMAX. SLTMAX must not be so large
133 * that it would overflow the stack if BPF is configured (XXX; if_ppp.c
134 * handles this better).
136 * SLIP_HIWAT is the amount of data that will be queued 'downstream'
137 * of us (i.e., in clists waiting to be picked up by the tty output
138 * interrupt). If we queue a lot of data downstream, it's immune to
139 * our t.o.s. queuing.
140 * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed
141 * telnet/ftp will see a 1 sec wait, independent of the mtu (the
142 * wait is dependent on the ftp window size but that's typically
143 * 1k - 4k). So, we want SLIP_HIWAT just big enough to amortize
144 * the cost (in idle time on the wire) of the tty driver running
145 * off the end of its clists & having to call back slstart for a
146 * new packet. For a tty interface with any buffering at all, this
147 * cost will be zero. Even with a totally brain dead interface (like
148 * the one on a typical workstation), the cost will be <= 1 character
149 * time. So, setting SLIP_HIWAT to ~100 guarantees that we'll lose
150 * at most 1% while maintaining good interactive response.
152 #define BUFOFFSET (128+sizeof(struct ifnet **)+SLIP_HDRLEN)
153 #define SLRMAX (MCLBYTES - BUFOFFSET)
154 #define SLBUFSIZE (SLRMAX + BUFOFFSET)
156 #define SLMTU 552 /* default MTU */
158 #define SLTMAX 1500 /* maximum MTU */
159 #define SLIP_HIWAT roundup(50,CBSIZE)
160 #define CLISTRESERVE 1024 /* Can't let clists get too low */
163 * SLIP ABORT ESCAPE MECHANISM:
164 * (inspired by HAYES modem escape arrangement)
165 * 1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape }
166 * within window time signals a "soft" exit from slip mode by remote end
167 * if the IFF_DEBUG flag is on.
169 #define ABT_ESC '\033' /* can't be t_intr - distant host must know it*/
170 #define ABT_IDLE 1 /* in seconds - idle before an escape */
171 #define ABT_COUNT 3 /* count of escapes for abort */
172 #define ABT_WINDOW (ABT_COUNT*2+2) /* in seconds - time to count */
174 static struct sl_softc sl_softc[NSL];
176 #define FRAME_END 0xc0 /* Frame End */
177 #define FRAME_ESCAPE 0xdb /* Frame Esc */
178 #define TRANS_FRAME_END 0xdc /* transposed frame end */
179 #define TRANS_FRAME_ESCAPE 0xdd /* transposed frame esc */
181 static int slinit (struct sl_softc *);
182 static struct mbuf *sl_btom (struct sl_softc *, int);
183 static timeout_t sl_keepalive;
184 static timeout_t sl_outfill;
185 static int slclose (struct tty *,int);
186 static int slinput (int, struct tty *);
187 static int slioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
188 static int sltioctl (struct tty *, u_long, caddr_t, int, struct ucred *);
189 static int slopen (cdev_t, struct tty *);
190 static int sloutput (struct ifnet *,
191 struct mbuf *, struct sockaddr *, struct rtentry *);
192 static int slstart (struct tty *);
194 static struct linesw slipdisc = {
195 slopen, slclose, l_noread, l_nowrite,
196 sltioctl, slinput, slstart, ttymodem,
201 * Called from boot code to establish sl interfaces.
204 slattach(void *dummy)
209 lwkt_gettoken(&tty_token);
210 linesw[SLIPDISC] = slipdisc;
212 for (sc = sl_softc; i < NSL; sc++) {
213 if_initname(&(sc->sc_if), "sl", i++);
214 sc->sc_if.if_mtu = SLMTU;
219 IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST;
221 sc->sc_if.if_type = IFT_SLIP;
222 sc->sc_if.if_ioctl = slioctl;
223 sc->sc_if.if_output = sloutput;
224 ifq_set_maxlen(&sc->sc_if.if_snd, 50);
225 ifq_set_ready(&sc->sc_if.if_snd);
226 sc->sc_fastq.ifq_maxlen = 32;
227 sc->sc_if.if_linkmib = sc;
228 sc->sc_if.if_linkmiblen = sizeof *sc;
229 callout_init_mp(&sc->sc_oftimeout);
230 callout_init_mp(&sc->sc_katimeout);
231 if_attach(&sc->sc_if, NULL);
232 bpfattach(&sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
234 lwkt_reltoken(&tty_token);
238 slinit(struct sl_softc *sc)
240 lwkt_gettoken(&tty_token);
241 if (sc->sc_ep == NULL)
242 sc->sc_ep = kmalloc(SLBUFSIZE, M_DEVBUF, M_WAITOK);
243 sc->sc_buf = sc->sc_ep + SLBUFSIZE - SLRMAX;
244 sc->sc_mp = sc->sc_buf;
245 sl_compress_init(&sc->sc_comp, -1);
246 lwkt_reltoken(&tty_token);
251 * Line specific open routine.
252 * Attach the given tty to the first available sl unit.
256 slopen(cdev_t dev, struct tty *tp)
261 struct thread *td = curthread; /* XXX */
263 error = priv_check(td, PRIV_ROOT);
267 lwkt_gettoken(&tty_token);
268 if (tp->t_line == SLIPDISC) {
269 lwkt_reltoken(&tty_token);
273 for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++)
274 if (sc->sc_ttyp == NULL && !(sc->sc_flags & SC_STATIC)) {
275 if (slinit(sc) == 0) {
276 lwkt_reltoken(&tty_token);
279 tp->t_sc = (caddr_t)sc;
281 sc->sc_if.if_baudrate = tp->t_ospeed;
282 ttyflush(tp, FREAD | FWRITE);
284 tp->t_line = SLIPDISC;
286 * We don't use t_canq or t_rawq, so reduce their
287 * cblock resources to 0. Reserve enough cblocks
288 * for t_outq to guarantee that we can fit a full
289 * packet if the SLIP_HIWAT check allows slstart()
290 * to loop. Use the same value for the cblock
291 * limit since the reserved blocks should always
292 * be enough. Reserving cblocks probably makes
293 * the CLISTRESERVE check unnecessary and wasteful.
295 clist_alloc_cblocks(&tp->t_canq, 0, 0);
296 clist_alloc_cblocks(&tp->t_outq,
297 SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1,
298 SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1);
299 clist_alloc_cblocks(&tp->t_rawq, 0, 0);
304 lwkt_reltoken(&tty_token);
307 lwkt_reltoken(&tty_token);
312 * Line specific close routine.
313 * Detach the tty from the sl unit.
316 slclose(struct tty *tp, int flag)
320 lwkt_gettoken(&tty_token);
321 ttyflush(tp, FREAD | FWRITE);
324 clist_free_cblocks(&tp->t_outq);
326 sc = (struct sl_softc *)tp->t_sc;
328 if (sc->sc_outfill) {
330 callout_stop(&sc->sc_oftimeout);
332 if (sc->sc_keepalive) {
333 sc->sc_keepalive = 0;
334 callout_stop(&sc->sc_katimeout);
337 sc->sc_flags &= SC_STATIC;
341 kfree(sc->sc_ep, M_DEVBUF);
348 lwkt_reltoken(&tty_token);
353 * Line specific (tty) ioctl routine.
354 * Provide a way to get the sl unit number.
358 sltioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct ucred *cred)
360 struct sl_softc *sc = (struct sl_softc *)tp->t_sc, *nc, *tmpnc;
364 lwkt_gettoken(&tty_token);
368 *(int *)data = sc->sc_if.if_dunit;
372 if (sc->sc_if.if_dunit != *(u_int *)data) {
373 for (nsl = NSL, nc = sl_softc; --nsl >= 0; nc++) {
374 if ( nc->sc_if.if_dunit == *(u_int *)data
375 && nc->sc_ttyp == NULL
377 tmpnc = kmalloc(sizeof *tmpnc, M_TEMP,
381 nc->sc_if = tmpnc->sc_if;
382 tmpnc->sc_if = sc->sc_if;
384 kfree(tmpnc, M_TEMP);
385 if (sc->sc_if.if_flags & IFF_UP) {
387 if (!(nc->sc_if.if_flags & IFF_UP))
389 } else if (nc->sc_if.if_flags & IFF_UP)
391 sc->sc_flags &= ~SC_STATIC;
392 sc->sc_flags |= (nc->sc_flags & SC_STATIC);
394 clist_alloc_cblocks(&tp->t_outq,
395 SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1,
396 SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1);
397 sl_compress_init(&sc->sc_comp, -1);
401 lwkt_reltoken(&tty_token);
406 sc->sc_flags |= SC_STATIC;
410 sc->sc_keepalive = *(u_int *)data * hz;
411 if (sc->sc_keepalive) {
412 sc->sc_flags |= SC_KEEPALIVE;
413 callout_reset(&sc->sc_katimeout, sc->sc_keepalive,
416 if ((sc->sc_flags & SC_KEEPALIVE) != 0) {
417 callout_stop(&sc->sc_katimeout);
418 sc->sc_flags &= ~SC_KEEPALIVE;
424 *(int *)data = sc->sc_keepalive / hz;
428 sc->sc_outfill = *(u_int *)data * hz;
429 if (sc->sc_outfill) {
430 sc->sc_flags |= SC_OUTWAIT;
431 callout_reset(&sc->sc_oftimeout, sc->sc_outfill,
434 if ((sc->sc_flags & SC_OUTWAIT) != 0) {
435 callout_stop(&sc->sc_oftimeout);
436 sc->sc_flags &= ~SC_OUTWAIT;
442 *(int *)data = sc->sc_outfill / hz;
446 lwkt_reltoken(&tty_token);
450 lwkt_reltoken(&tty_token);
456 * Queue a packet. Start transmission if not active.
457 * Compression happens in slstart; if we do it here, IP TOS
458 * will cause us to not compress "background" packets, because
459 * ordering gets trashed. It can be done for all packets in slstart.
462 sloutput_serialized(struct ifnet *ifp, struct ifaltq_subque *ifsq,
463 struct mbuf *m, struct sockaddr *dst, struct rtentry *rtp)
465 struct sl_softc *sc = &sl_softc[ifp->if_dunit];
468 struct altq_pktattr pktattr;
470 ifq_classify(&ifp->if_snd, m, dst->sa_family, &pktattr);
473 * `Cannot happen' (see slioctl). Someday we will extend
474 * the line protocol to support other address families.
476 if (dst->sa_family != AF_INET) {
477 kprintf("%s: af%d not supported\n", sc->sc_if.if_xname,
480 sc->sc_if.if_noproto++;
481 return (EAFNOSUPPORT);
484 if (sc->sc_ttyp == NULL || !(ifp->if_flags & IFF_UP)) {
488 if ((sc->sc_ttyp->t_state & TS_CONNECTED) == 0) {
490 return (EHOSTUNREACH);
492 ip = mtod(m, struct ip *);
493 if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
495 return (ENETRESET); /* XXX ? */
500 if ((ip->ip_tos & IPTOS_LOWDELAY) && !ifq_is_enabled(&sc->sc_if.if_snd)) {
501 if (IF_QFULL(&sc->sc_fastq)) {
502 IF_DROP(&sc->sc_fastq);
506 IF_ENQUEUE(&sc->sc_fastq, m);
510 error = ifsq_enqueue(ifsq, m, &pktattr);
513 sc->sc_if.if_oerrors++;
517 if (sc->sc_ttyp->t_outq.c_cc == 0)
518 slstart(sc->sc_ttyp);
524 sloutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
527 struct ifaltq_subque *ifsq = ifq_get_subq_default(&ifp->if_snd);
530 ifnet_serialize_tx(ifp, ifsq);
531 error = sloutput_serialized(ifp, ifsq, m, dst, rtp);
532 ifnet_deserialize_tx(ifp, ifsq);
538 * Start output on interface. Get another datagram
539 * to send from the interface queue and map it to
540 * the interface before starting output.
543 slstart(struct tty *tp)
545 struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
546 struct ifaltq_subque *ifsq = ifq_get_subq_default(&sc->sc_if.if_snd);
550 u_char bpfbuf[SLTMAX + SLIP_HDRLEN];
553 lwkt_gettoken(&tty_token);
556 * Call output process whether or not there is more in the
557 * output queue. We are being called in lieu of ttstart
558 * and must do what it would.
562 if (tp->t_outq.c_cc != 0) {
564 sc->sc_flags &= ~SC_OUTWAIT;
565 if (tp->t_outq.c_cc > SLIP_HIWAT) {
566 lwkt_reltoken(&tty_token);
572 * This happens briefly when the line shuts down.
575 lwkt_reltoken(&tty_token);
580 * Get a packet and send it to the interface.
583 IF_DEQUEUE(&sc->sc_fastq, m);
585 sc->sc_if.if_omcasts++; /* XXX */
587 m = ifsq_dequeue(ifsq, NULL);
590 lwkt_reltoken(&tty_token);
595 * We do the header compression here rather than in sloutput
596 * because the packets will be out of order if we are using TOS
597 * queueing, and the connection id compression will get
598 * munged when this happens.
600 if (sc->sc_if.if_bpf) {
602 * We need to save the TCP/IP header before it's
603 * compressed. To avoid complicated code, we just
604 * copy the entire packet into a stack buffer (since
605 * this is a serial line, packets should be short
606 * and/or the copy should be negligible cost compared
607 * to the packet transmission time).
610 u_char *cp = bpfbuf + SLIP_HDRLEN;
614 int mlen = m1->m_len;
616 bcopy(mtod(m1, caddr_t), cp, mlen);
619 } while ((m1 = m1->m_next) != NULL);
621 ip = mtod(m, struct ip *);
622 if (ip->ip_v == IPVERSION && ip->ip_p == IPPROTO_TCP) {
623 if (sc->sc_if.if_flags & SC_COMPRESS)
624 *mtod(m, u_char *) |= sl_compress_tcp(m, ip,
627 if (sc->sc_if.if_bpf) {
630 if (sc->sc_if.if_bpf) {
632 * Put the SLIP pseudo-"link header" in place.
633 * The compressed header is now at the
634 * beginning of the mbuf.
636 bpfbuf[SLX_DIR] = SLIPDIR_OUT;
637 bcopy(mtod(m, caddr_t), &bpfbuf[SLX_CHDR],
639 bpf_tap(sc->sc_if.if_bpf, bpfbuf,
647 * If system is getting low on clists, just flush our
648 * output queue (if the stuff was important, it'll get
649 * retransmitted). Note that SLTMAX is used instead of
650 * the current if_mtu setting because connections that
651 * have already been established still use the original
652 * (possibly larger) mss.
654 if (cfreecount < CLISTRESERVE + SLTMAX) {
656 sc->sc_if.if_collisions++;
660 sc->sc_flags &= ~SC_OUTWAIT;
662 * The extra FRAME_END will start up a new packet, and thus
663 * will flush any accumulated garbage. We do this whenever
664 * the line may have been idle for some time.
666 if (tp->t_outq.c_cc == 0) {
667 ++sc->sc_if.if_obytes;
668 clist_putc(FRAME_END, &tp->t_outq);
674 cp = mtod(m, u_char *); ep = cp + m->m_len;
677 * Find out how many bytes in the string we can
678 * handle without doing something special.
693 * Put n characters at once
694 * into the tty output queue.
696 if (b_to_q((char *)bp, cp - bp,
699 sc->sc_if.if_obytes += cp - bp;
702 * If there are characters left in the mbuf,
703 * the first one must be special..
704 * Put it out in a different form.
707 if (clist_putc(FRAME_ESCAPE, &tp->t_outq))
709 if (clist_putc(*cp++ == FRAME_ESCAPE ?
710 TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
712 clist_unputc(&tp->t_outq);
715 sc->sc_if.if_obytes += 2;
721 if (clist_putc(FRAME_END, &tp->t_outq)) {
723 * Not enough room. Remove a char to make room
724 * and end the packet normally.
725 * If you get many collisions (more than one or two
726 * a day) you probably do not have enough clists
727 * and you should increase "nclist" in param.c.
729 clist_unputc(&tp->t_outq);
730 clist_putc(FRAME_END, &tp->t_outq);
731 sc->sc_if.if_collisions++;
733 ++sc->sc_if.if_obytes;
734 sc->sc_if.if_opackets++;
737 lwkt_reltoken(&tty_token);
742 * Copy data buffer to mbuf chain; add ifnet pointer.
745 sl_btom(struct sl_softc *sc, int len)
752 MGETHDR(m, MB_DONTWAIT, MT_DATA);
757 * If we have more than MHLEN bytes, it's cheaper to
758 * queue the cluster we just filled & allocate a new one
759 * for the input buffer. Otherwise, fill the mbuf we
760 * allocated above. Note that code in the input routine
761 * guarantees that packet will fit in a cluster.
764 MCLGET(m, MB_DONTWAIT);
765 if ((m->m_flags & M_EXT) == 0) {
767 * we couldn't get a cluster - if memory's this
768 * low, it's time to start dropping packets.
774 bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len);
776 m->m_pkthdr.len = len;
777 m->m_pkthdr.rcvif = &sc->sc_if;
782 * tty interface receiver interrupt.
785 slinput(int c, struct tty *tp)
790 u_char chdr[CHDR_LEN];
792 lwkt_gettoken(&tty_token);
794 sc = (struct sl_softc *)tp->t_sc;
796 lwkt_reltoken(&tty_token);
799 if (c & TTY_ERRORMASK || (tp->t_state & TS_CONNECTED) == 0) {
800 sc->sc_flags |= SC_ERROR;
801 lwkt_reltoken(&tty_token);
806 ++sc->sc_if.if_ibytes;
808 if (sc->sc_if.if_flags & IFF_DEBUG) {
811 * If we have a previous abort, see whether
812 * this one is within the time limit.
814 if (sc->sc_abortcount &&
815 time_second >= sc->sc_starttime + ABT_WINDOW)
816 sc->sc_abortcount = 0;
818 * If we see an abort after "idle" time, count it;
819 * record when the first abort escape arrived.
821 if (time_second >= sc->sc_lasttime + ABT_IDLE) {
822 if (++sc->sc_abortcount == 1)
823 sc->sc_starttime = time_second;
824 if (sc->sc_abortcount >= ABT_COUNT) {
826 lwkt_reltoken(&tty_token);
831 sc->sc_abortcount = 0;
832 sc->sc_lasttime = time_second;
837 case TRANS_FRAME_ESCAPE:
842 case TRANS_FRAME_END:
849 lwkt_reltoken(&tty_token);
853 sc->sc_flags &= ~SC_KEEPALIVE;
854 if(sc->sc_flags & SC_ERROR) {
855 sc->sc_flags &= ~SC_ERROR;
858 len = sc->sc_mp - sc->sc_buf;
860 /* less than min length packet - ignore */
863 if (sc->sc_if.if_bpf) {
865 * Save the compressed header, so we
866 * can tack it on later. Note that we
867 * will end up copying garbage in some
868 * cases but this is okay. We remember
869 * where the buffer started so we can
870 * compute the new header length.
872 bcopy(sc->sc_buf, chdr, CHDR_LEN);
875 if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) {
877 c = TYPE_COMPRESSED_TCP;
878 else if (c == TYPE_UNCOMPRESSED_TCP)
879 *sc->sc_buf &= 0x4f; /* XXX */
881 * We've got something that's not an IP packet.
882 * If compression is enabled, try to decompress it.
883 * Otherwise, if `auto-enable' compression is on and
884 * it's a reasonable packet, decompress it and then
885 * enable compression. Otherwise, drop it.
887 if (sc->sc_if.if_flags & SC_COMPRESS) {
888 len = sl_uncompress_tcp(&sc->sc_buf, len,
889 (u_int)c, &sc->sc_comp);
892 } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
893 c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
894 len = sl_uncompress_tcp(&sc->sc_buf, len,
895 (u_int)c, &sc->sc_comp);
898 sc->sc_if.if_flags |= SC_COMPRESS;
902 if (sc->sc_if.if_bpf) {
905 if (sc->sc_if.if_bpf) {
907 * Put the SLIP pseudo-"link header" in place.
908 * We couldn't do this any earlier since
909 * decompression probably moved the buffer
910 * pointer. Then, invoke BPF.
912 u_char *hp = sc->sc_buf - SLIP_HDRLEN;
914 hp[SLX_DIR] = SLIPDIR_IN;
915 bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN);
916 bpf_tap(sc->sc_if.if_bpf, hp,
922 m = sl_btom(sc, len);
926 sc->sc_if.if_ipackets++;
928 if ((sc->sc_if.if_flags & IFF_UP) == 0) {
933 if (netisr_queue(NETISR_IP, m)) {
934 sc->sc_if.if_ierrors++;
935 sc->sc_if.if_iqdrops++;
940 if (sc->sc_mp < sc->sc_ep + SLBUFSIZE) {
943 lwkt_reltoken(&tty_token);
947 /* can't put lower; would miss an extra frame */
948 sc->sc_flags |= SC_ERROR;
951 sc->sc_if.if_ierrors++;
953 sc->sc_mp = sc->sc_buf = sc->sc_ep + SLBUFSIZE - SLRMAX;
955 lwkt_reltoken(&tty_token);
960 * Process an ioctl request.
963 slioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
965 struct ifaddr *ifa = (struct ifaddr *)data;
966 struct ifreq *ifr = (struct ifreq *)data;
975 * if.c will set the interface up even if we
978 if (sl_softc[ifp->if_dunit].sc_ttyp == NULL) {
979 ifp->if_flags &= ~IFF_UP;
984 * This is "historical" - set the interface up when
985 * setting the address.
987 if (ifa->ifa_addr->sa_family == AF_INET) {
988 if (sl_softc[ifp->if_dunit].sc_ttyp != NULL)
989 ifp->if_flags |= IFF_UP;
991 error = EAFNOSUPPORT;
996 if (ifa->ifa_addr->sa_family != AF_INET)
997 error = EAFNOSUPPORT;
1006 * Set the interface MTU.
1008 if (ifr->ifr_mtu > SLTMAX)
1013 ifp->if_mtu = ifr->ifr_mtu;
1014 tp = sl_softc[ifp->if_dunit].sc_ttyp;
1016 clist_alloc_cblocks(&tp->t_outq,
1017 SLIP_HIWAT + 2 * ifp->if_mtu + 1,
1018 SLIP_HIWAT + 2 * ifp->if_mtu + 1);
1031 sl_keepalive(void *chan)
1033 struct sl_softc *sc = chan;
1035 lwkt_gettoken(&tty_token);
1036 lwkt_gettoken(&proc_token);
1037 if (sc->sc_keepalive) {
1038 if (sc->sc_flags & SC_KEEPALIVE)
1039 pgsignal (sc->sc_ttyp->t_pgrp, SIGURG, 1);
1041 sc->sc_flags |= SC_KEEPALIVE;
1042 callout_reset(&sc->sc_katimeout, sc->sc_keepalive,
1045 sc->sc_flags &= ~SC_KEEPALIVE;
1047 lwkt_reltoken(&proc_token);
1048 lwkt_reltoken(&tty_token);
1052 sl_outfill(void *chan)
1054 struct sl_softc *sc = chan;
1055 struct tty *tp = sc->sc_ttyp;
1057 lwkt_gettoken(&tty_token);
1059 if (sc->sc_outfill && tp != NULL) {
1060 if (sc->sc_flags & SC_OUTWAIT) {
1062 ++sc->sc_if.if_obytes;
1063 clist_putc(FRAME_END, &tp->t_outq);
1067 sc->sc_flags |= SC_OUTWAIT;
1068 callout_reset(&sc->sc_oftimeout, sc->sc_outfill,
1071 sc->sc_flags &= ~SC_OUTWAIT;
1073 lwkt_reltoken(&tty_token);