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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)if_sl.c 8.6 (Berkeley) 2/1/94
34 * Serial Line interface
37 * Center for Seismic Studies
38 * 1300 N 17th Street, Suite 1450
39 * Arlington, Virginia 22209
44 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
45 * N.B.: this belongs in netinet, not net, the way it stands now.
46 * Should have a link-layer type designation, but wouldn't be
47 * backwards-compatible.
49 * Converted to 4.3BSD Beta by Chris Torek.
50 * Other changes made at Berkeley, based in part on code by Kirk Smith.
51 * W. Jolitz added slip abort.
53 * Hacked almost beyond recognition by Van Jacobson (van@helios.ee.lbl.gov).
54 * Added priority queuing for "interactive" traffic; hooks for TCP
55 * header compression; ICMP filtering (at 2400 baud, some cretin
56 * pinging you can use up all your bandwidth). Made low clist behavior
57 * more robust and slightly less likely to hang serial line.
58 * Sped up a bunch of things.
60 * Note that splimp() is used throughout to block both (tty) input
61 * interrupts and network activity; thus, splimp must be >= spltty.
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/malloc.h>
72 #include <sys/socket.h>
73 #include <sys/sockio.h>
74 #include <sys/fcntl.h>
75 #include <sys/signalvar.h>
77 #include <sys/clist.h>
78 #include <sys/kernel.h>
80 #include <sys/module.h>
84 #include <net/if_types.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>
97 #include <net/if_slvar.h>
102 static MALLOC_DEFINE(M_SL, "sl", "SLIP Interface");
105 * SLRMAX is a hard limit on input packet size. To simplify the code
106 * and improve performance, we require that packets fit in an mbuf
107 * cluster, and if we get a compressed packet, there's enough extra
108 * room to expand the header into a max length tcp/ip header (128
109 * bytes). So, SLRMAX can be at most
112 * SLMTU is the default transmit MTU. The transmit MTU should be kept
113 * small enough so that interactive use doesn't suffer, but large
114 * enough to provide good performance. 552 is a good choice for SLMTU
115 * because it is high enough to not fragment TCP packets being routed
116 * through this host. Packet fragmentation is bad with SLIP because
117 * fragment headers aren't compressed. The previous assumptions about
118 * the best MTU value don't really hold when using modern modems with
119 * BTLZ data compression because the modem buffers play a much larger
120 * role in interactive performance than the MTU. The MTU can be changed
121 * at any time to suit the specific environment with ifconfig(8), and
122 * its maximum value is defined as SLTMAX. SLTMAX must not be so large
123 * that it would overflow the stack if BPF is configured (XXX; if_ppp.c
124 * handles this better).
126 * SLIP_HIWAT is the amount of data that will be queued 'downstream'
127 * of us (i.e., in clists waiting to be picked up by the tty output
128 * interrupt). If we queue a lot of data downstream, it's immune to
129 * our t.o.s. queuing.
130 * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed
131 * telnet/ftp will see a 1 sec wait, independent of the mtu (the
132 * wait is dependent on the ftp window size but that's typically
133 * 1k - 4k). So, we want SLIP_HIWAT just big enough to amortize
134 * the cost (in idle time on the wire) of the tty driver running
135 * off the end of its clists & having to call back slstart for a
136 * new packet. For a tty interface with any buffering at all, this
137 * cost will be zero. Even with a totally brain dead interface (like
138 * the one on a typical workstation), the cost will be <= 1 character
139 * time. So, setting SLIP_HIWAT to ~100 guarantees that we'll lose
140 * at most 1% while maintaining good interactive response.
142 #define BUFOFFSET (128+sizeof(struct ifnet **)+SLIP_HDRLEN)
143 #define SLRMAX (MCLBYTES - BUFOFFSET)
144 #define SLBUFSIZE (SLRMAX + BUFOFFSET)
146 #define SLMTU 552 /* default MTU */
148 #define SLTMAX 1500 /* maximum MTU */
149 #define SLIP_HIWAT roundup(50,CBSIZE)
150 #define CLISTRESERVE 1024 /* Can't let clists get too low */
153 * SLIP ABORT ESCAPE MECHANISM:
154 * (inspired by HAYES modem escape arrangement)
155 * 1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape }
156 * within window time signals a "soft" exit from slip mode by remote end
157 * if the IFF_DEBUG flag is on.
159 #define ABT_ESC '\033' /* can't be t_intr - distant host must know it*/
160 #define ABT_IDLE 1 /* in seconds - idle before an escape */
161 #define ABT_COUNT 3 /* count of escapes for abort */
162 #define ABT_WINDOW (ABT_COUNT*2+2) /* in seconds - time to count */
164 static LIST_HEAD(sl_list, sl_softc) sl_list;
166 #define FRAME_END 0xc0 /* Frame End */
167 #define FRAME_ESCAPE 0xdb /* Frame Esc */
168 #define TRANS_FRAME_END 0xdc /* transposed frame end */
169 #define TRANS_FRAME_ESCAPE 0xdd /* transposed frame esc */
171 static int slisstatic(int);
172 static void slmarkstatic(int);
173 static struct sl_softc *slcreate(void);
174 static void sldestroy(struct sl_softc *sc);
175 static struct mbuf *sl_btom(struct sl_softc *, int);
176 static timeout_t sl_keepalive;
177 static timeout_t sl_outfill;
178 static l_close_t slclose;
179 static l_rint_t slinput;
180 static l_ioctl_t sltioctl;
181 static l_start_t sltstart;
182 static int slioctl(struct ifnet *, u_long, caddr_t);
183 static int slopen(struct cdev *, struct tty *);
184 static int sloutput(struct ifnet *,
185 struct mbuf *, struct sockaddr *, struct rtentry *);
186 static void slstart(struct ifnet *);
188 static struct linesw slipdisc = {
192 .l_write = l_nowrite,
200 * Called from boot code to establish sl interfaces.
203 sl_modevent(module_t mod, int type, void *data)
207 ldisc_register(SLIPDISC, &slipdisc);
211 ldisc_deregister(SLIPDISC);
212 printf("if_sl module unload - not possible for this module type\n");
220 static moduledata_t sl_mod = {
226 DECLARE_MODULE(if_sl, sl_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
228 static int *st_unit_list;
229 static size_t st_unit_max = 0;
232 slisunitfree(int unit)
236 LIST_FOREACH(nc, &sl_list, sl_next) {
237 if (nc->sc_if.if_dunit == unit)
243 static struct sl_softc *
244 sl_for_tty(struct tty *tp)
248 LIST_FOREACH(nc, &sl_list, sl_next) {
249 if (nc->sc_ttyp == tp)
259 for (i = 0; i < st_unit_max; i++)
260 if (st_unit_list[i] == unit)
266 slmarkstatic(int unit)
270 if (slisstatic(unit))
273 MALLOC(t, int *, sizeof(int) * (st_unit_max+1), M_SL, M_NOWAIT);
278 bcopy(st_unit_list, t, sizeof(int) * st_unit_max);
279 free(st_unit_list, M_SL);
282 st_unit_list[st_unit_max] = unit;
286 static struct sl_softc *
293 MALLOC(sc, struct sl_softc *, sizeof(*sc), M_SL, M_WAITOK | M_ZERO);
295 m = m_gethdr(M_TRYWAIT, MT_DATA);
297 MCLGET(m, M_TRYWAIT);
298 if ((m->m_flags & M_EXT) == 0) {
305 printf("sl: can't allocate buffer\n");
310 sc->sc_ep = mtod(m, u_char *) + SLBUFSIZE;
312 sc->sc_buf = sc->sc_ep - SLRMAX;
313 sc->sc_mp = sc->sc_buf;
314 sl_compress_init(&sc->sc_comp, -1);
316 sc->sc_if.if_softc = sc;
317 sc->sc_if.if_mtu = SLMTU;
322 IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST | IFF_NEEDSGIANT;
324 sc->sc_if.if_type = IFT_SLIP;
325 sc->sc_if.if_ioctl = slioctl;
326 sc->sc_if.if_output = sloutput;
327 sc->sc_if.if_start = slstart;
328 sc->sc_if.if_snd.ifq_maxlen = 50;
329 sc->sc_fastq.ifq_maxlen = 32;
330 sc->sc_if.if_linkmib = sc;
331 sc->sc_if.if_linkmiblen = sizeof *sc;
332 mtx_init(&sc->sc_fastq.ifq_mtx, "sl_fastq", NULL, MTX_DEF);
335 * Find a suitable unit number.
337 for (unit=0; ; unit++) {
338 if (slisstatic(unit))
340 if (!slisunitfree(unit))
344 if_initname(&sc->sc_if, "sl", unit);
345 LIST_INSERT_HEAD(&sl_list, sc, sl_next);
347 if_attach(&sc->sc_if);
348 bpfattach(&sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
355 * Line specific open routine.
356 * Attach the given tty to the first available sl unit.
360 slopen(struct cdev *dev, register struct tty *tp)
362 register struct sl_softc *sc;
365 error = suser(curthread);
369 if ((sc = slcreate()) == NULL)
372 tp->t_hotchar = FRAME_END;
374 sc->sc_if.if_baudrate = tp->t_ospeed;
375 ttyflush(tp, FREAD | FWRITE);
378 * We don't use t_canq or t_rawq, so reduce their
379 * cblock resources to 0. Reserve enough cblocks
380 * for t_outq to guarantee that we can fit a full
381 * packet if the SLIP_HIWAT check allows slstart()
382 * to loop. Use the same value for the cblock
383 * limit since the reserved blocks should always
384 * be enough. Reserving cblocks probably makes
385 * the CLISTRESERVE check unnecessary and wasteful.
387 clist_alloc_cblocks(&tp->t_canq, 0, 0);
388 clist_alloc_cblocks(&tp->t_outq,
389 SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1,
390 SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1);
391 clist_alloc_cblocks(&tp->t_rawq, 0, 0);
400 sldestroy(struct sl_softc *sc)
402 bpfdetach(&sc->sc_if);
403 if_detach(&sc->sc_if);
404 LIST_REMOVE(sc, sl_next);
406 mtx_destroy(&sc->sc_fastq.ifq_mtx);
408 free(sc->bpfbuf, M_SL);
413 * Line specific close routine.
414 * Detach the tty from the sl unit.
417 slclose(struct tty *tp, int flag)
419 register struct sl_softc *sc;
422 ttyflush(tp, FREAD | FWRITE);
424 * XXX the placement of the following spl is misleading. tty
425 * interrupts must be blocked across line discipline switches
426 * and throughout closes to avoid races.
428 s = splimp(); /* actually, max(spltty, splnet) */
429 clist_free_cblocks(&tp->t_outq);
432 if (sc->sc_outfill) {
434 untimeout(sl_outfill, sc, sc->sc_ofhandle);
436 if (sc->sc_keepalive) {
437 sc->sc_keepalive = 0;
438 untimeout(sl_keepalive, sc, sc->sc_kahandle);
449 * Line specific (tty) ioctl routine.
450 * Provide a way to get the sl unit number.
454 sltioctl(struct tty *tp, u_long cmd, caddr_t data, int flag,
457 struct sl_softc *sc = sl_for_tty(tp);
463 *(int *)data = sc->sc_if.if_dunit;
467 unit = *(u_int *)data;
472 if (sc->sc_if.if_dunit != unit) {
473 if (!slisunitfree(unit)) {
478 wasup = sc->sc_if.if_flags & IFF_UP;
479 bpfdetach(&sc->sc_if);
480 if_detach(&sc->sc_if);
481 LIST_REMOVE(sc, sl_next);
482 if_initname(&sc->sc_if, "sl", unit);
483 LIST_INSERT_HEAD(&sl_list, sc, sl_next);
484 if_attach(&sc->sc_if);
485 bpfattach(&sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
490 clist_alloc_cblocks(&tp->t_outq,
491 SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1,
492 SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1);
498 sc->sc_keepalive = *(u_int *)data * hz;
499 if (sc->sc_keepalive) {
500 sc->sc_flags |= SC_KEEPALIVE;
501 sc->sc_kahandle = timeout(sl_keepalive, sc,
504 if ((sc->sc_flags & SC_KEEPALIVE) != 0) {
505 untimeout(sl_keepalive, sc, sc->sc_kahandle);
506 sc->sc_flags &= ~SC_KEEPALIVE;
512 *(int *)data = sc->sc_keepalive / hz;
516 sc->sc_outfill = *(u_int *)data * hz;
517 if (sc->sc_outfill) {
518 sc->sc_flags |= SC_OUTWAIT;
519 sc->sc_ofhandle = timeout(sl_outfill, sc,
522 if ((sc->sc_flags & SC_OUTWAIT) != 0) {
523 untimeout(sl_outfill, sc, sc->sc_ofhandle);
524 sc->sc_flags &= ~SC_OUTWAIT;
530 *(int *)data = sc->sc_outfill / hz;
542 * Queue a packet. Start transmission if not active.
543 * Compression happens in slstart; if we do it here, IP TOS
544 * will cause us to not compress "background" packets, because
545 * ordering gets trashed. It can be done for all packets in slstart.
548 sloutput(struct ifnet *ifp, register struct mbuf *m, struct sockaddr *dst,
551 register struct sl_softc *sc = ifp->if_softc;
552 register struct ip *ip;
556 * `Cannot happen' (see slioctl). Someday we will extend
557 * the line protocol to support other address families.
559 if (dst->sa_family != AF_INET) {
560 if_printf(ifp, "af%d not supported\n", dst->sa_family);
562 sc->sc_if.if_noproto++;
563 return (EAFNOSUPPORT);
566 if (sc->sc_ttyp == NULL || !(ifp->if_flags & IFF_UP)) {
570 if ((sc->sc_ttyp->t_state & TS_CONNECTED) == 0) {
572 return (EHOSTUNREACH);
574 ip = mtod(m, struct ip *);
575 if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
577 return (ENETRESET); /* XXX ? */
579 if (ip->ip_tos & IPTOS_LOWDELAY &&
580 !ALTQ_IS_ENABLED(&sc->sc_if.if_snd))
581 error = !(IF_HANDOFF(&sc->sc_fastq, m, &sc->sc_if));
583 IFQ_HANDOFF(&sc->sc_if, m, error);
585 sc->sc_if.if_oerrors++;
592 slstart(struct ifnet *ifp)
594 struct sl_softc *sc = ifp->if_softc;
598 if (sc->sc_ttyp->t_outq.c_cc == 0)
599 sltstart(sc->sc_ttyp);
604 * Start output on interface. Get another datagram
605 * to send from the interface queue and map it to
606 * the interface before starting output.
609 sltstart(struct tty *tp)
611 register struct sl_softc *sc = sl_for_tty(tp);
612 register struct mbuf *m;
614 register struct ip *ip;
616 register int len = 0;
618 GIANT_REQUIRED; /* tty */
622 * Call output process whether or not there is more in the
623 * output queue. We are being called in lieu of ttstart
624 * and must do what it would.
628 if (tp->t_outq.c_cc != 0) {
630 sc->sc_flags &= ~SC_OUTWAIT;
631 if (tp->t_outq.c_cc > SLIP_HIWAT)
636 * This happens briefly when the line shuts down.
642 * Get a packet and send it to the interface.
645 IF_DEQUEUE(&sc->sc_fastq, m);
647 sc->sc_if.if_omcasts++; /* XXX */
649 IF_DEQUEUE(&sc->sc_if.if_snd, m);
655 * We do the header compression here rather than in sloutput
656 * because the packets will be out of order if we are using TOS
657 * queueing, and the connection id compression will get
658 * munged when this happens.
660 if (sc->sc_if.if_bpf) {
662 * We need to save the TCP/IP header before it's
663 * compressed. To avoid complicated code, we just
664 * copy the entire packet into a stack buffer (since
665 * this is a serial line, packets should be short
666 * and/or the copy should be negligible cost compared
667 * to the packet transmission time).
669 register struct mbuf *m1 = m;
672 if (sc->bpfbuf == NULL)
673 MALLOC(sc->bpfbuf, u_char *,
674 SLTMAX + SLIP_HDRLEN, M_SL, M_NOWAIT);
677 cp = sc->bpfbuf + SLIP_HDRLEN;
680 register int mlen = m1->m_len;
682 bcopy(mtod(m1, caddr_t), cp, mlen);
685 } while ((m1 = m1->m_next) != NULL);
688 ip = mtod(m, struct ip *);
689 if (ip->ip_v == IPVERSION && ip->ip_p == IPPROTO_TCP) {
690 if (sc->sc_if.if_flags & SC_COMPRESS)
691 *mtod(m, u_char *) |= sl_compress_tcp(m, ip,
694 if (sc->sc_if.if_bpf && sc->bpfbuf) {
696 * Put the SLIP pseudo-"link header" in place. The
697 * compressed header is now at the beginning of the
700 sc->bpfbuf[SLX_DIR] = SLIPDIR_OUT;
701 bcopy(mtod(m, caddr_t), &sc->bpfbuf[SLX_CHDR], CHDR_LEN);
702 BPF_TAP(&sc->sc_if, sc->bpfbuf, len + SLIP_HDRLEN);
706 * If system is getting low on clists, just flush our
707 * output queue (if the stuff was important, it'll get
708 * retransmitted). Note that SLTMAX is used instead of
709 * the current if_mtu setting because connections that
710 * have already been established still use the original
711 * (possibly larger) mss.
713 if (cfreecount < CLISTRESERVE + SLTMAX) {
715 sc->sc_if.if_collisions++;
719 sc->sc_flags &= ~SC_OUTWAIT;
721 * The extra FRAME_END will start up a new packet, and thus
722 * will flush any accumulated garbage. We do this whenever
723 * the line may have been idle for some time.
725 if (tp->t_outq.c_cc == 0) {
726 ++sc->sc_if.if_obytes;
727 (void) putc(FRAME_END, &tp->t_outq);
733 cp = mtod(m, u_char *); ep = cp + m->m_len;
736 * Find out how many bytes in the string we can
737 * handle without doing something special.
739 register u_char *bp = cp;
752 * Put n characters at once
753 * into the tty output queue.
755 if (b_to_q((char *)bp, cp - bp,
758 sc->sc_if.if_obytes += cp - bp;
761 * If there are characters left in the mbuf,
762 * the first one must be special..
763 * Put it out in a different form.
766 if (putc(FRAME_ESCAPE, &tp->t_outq))
768 if (putc(*cp++ == FRAME_ESCAPE ?
769 TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
771 (void) unputc(&tp->t_outq);
774 sc->sc_if.if_obytes += 2;
780 if (putc(FRAME_END, &tp->t_outq)) {
782 * Not enough room. Remove a char to make room
783 * and end the packet normally.
784 * If you get many collisions (more than one or two
785 * a day) you probably do not have enough clists
786 * and you should increase "nclist" in param.c.
788 (void) unputc(&tp->t_outq);
789 (void) putc(FRAME_END, &tp->t_outq);
790 sc->sc_if.if_collisions++;
792 ++sc->sc_if.if_obytes;
793 sc->sc_if.if_opackets++;
800 * Copy data buffer to mbuf chain; add ifnet pointer.
803 sl_btom(struct sl_softc *sc, register int len)
805 struct mbuf *m, *newm;
807 MGETHDR(m, M_DONTWAIT, MT_DATA);
812 * If we have more than MHLEN bytes, it's cheaper to
813 * queue the cluster we just filled & allocate a new one
814 * for the input buffer. Otherwise, fill the mbuf we
815 * allocated above. Note that code in the input routine
816 * guarantees that packet will fit in a cluster.
819 MCLGET(m, M_DONTWAIT);
820 if ((m->m_flags & M_EXT) == 0) {
822 * we couldn't get a cluster - if memory's this
823 * low, it's time to start dropping packets.
828 /* Swap the new and old clusters */
832 sc->sc_ep = mtod(newm, u_char *) + SLBUFSIZE;
834 m->m_data = (caddr_t)sc->sc_buf;
836 bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len);
839 m->m_pkthdr.len = len;
840 m->m_pkthdr.rcvif = &sc->sc_if;
845 * tty interface receiver interrupt.
848 slinput(int c, struct tty *tp)
850 register struct sl_softc *sc;
851 register struct mbuf *m;
853 u_char chdr[CHDR_LEN];
859 if (c & TTY_ERRORMASK || (tp->t_state & TS_CONNECTED) == 0) {
860 sc->sc_flags |= SC_ERROR;
865 ++sc->sc_if.if_ibytes;
867 if (sc->sc_if.if_flags & IFF_DEBUG) {
870 * If we have a previous abort, see whether
871 * this one is within the time limit.
873 if (sc->sc_abortcount &&
874 time_second >= sc->sc_starttime + ABT_WINDOW)
875 sc->sc_abortcount = 0;
877 * If we see an abort after "idle" time, count it;
878 * record when the first abort escape arrived.
880 if (time_second >= sc->sc_lasttime + ABT_IDLE) {
881 if (++sc->sc_abortcount == 1)
882 sc->sc_starttime = time_second;
883 if (sc->sc_abortcount >= ABT_COUNT) {
889 sc->sc_abortcount = 0;
890 sc->sc_lasttime = time_second;
895 case TRANS_FRAME_ESCAPE:
900 case TRANS_FRAME_END:
910 sc->sc_flags &= ~SC_KEEPALIVE;
911 if(sc->sc_flags & SC_ERROR) {
912 sc->sc_flags &= ~SC_ERROR;
915 len = sc->sc_mp - sc->sc_buf;
917 /* less than min length packet - ignore */
920 if (sc->sc_if.if_bpf) {
922 * Save the compressed header, so we
923 * can tack it on later. Note that we
924 * will end up copying garbage in some
925 * cases but this is okay. We remember
926 * where the buffer started so we can
927 * compute the new header length.
929 bcopy(sc->sc_buf, chdr, CHDR_LEN);
932 if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) {
934 c = TYPE_COMPRESSED_TCP;
935 else if (c == TYPE_UNCOMPRESSED_TCP)
936 *sc->sc_buf &= 0x4f; /* XXX */
938 * We've got something that's not an IP packet.
939 * If compression is enabled, try to decompress it.
940 * Otherwise, if `auto-enable' compression is on and
941 * it's a reasonable packet, decompress it and then
942 * enable compression. Otherwise, drop it.
944 if (sc->sc_if.if_flags & SC_COMPRESS) {
945 len = sl_uncompress_tcp(&sc->sc_buf, len,
946 (u_int)c, &sc->sc_comp);
949 } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
950 c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
951 len = sl_uncompress_tcp(&sc->sc_buf, len,
952 (u_int)c, &sc->sc_comp);
955 sc->sc_if.if_flags |= SC_COMPRESS;
959 if (sc->sc_if.if_bpf) {
961 * Put the SLIP pseudo-"link header" in place.
962 * We couldn't do this any earlier since
963 * decompression probably moved the buffer
964 * pointer. Then, invoke BPF.
966 register u_char *hp = sc->sc_buf - SLIP_HDRLEN;
968 hp[SLX_DIR] = SLIPDIR_IN;
969 bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN);
970 BPF_TAP(&sc->sc_if, hp, len + SLIP_HDRLEN);
972 m = sl_btom(sc, len);
976 sc->sc_if.if_ipackets++;
978 if ((sc->sc_if.if_flags & IFF_UP) == 0) {
982 if (netisr_queue(NETISR_IP, m)) { /* (0) on success. */
983 sc->sc_if.if_ierrors++;
984 sc->sc_if.if_iqdrops++;
988 if (sc->sc_mp < sc->sc_ep) {
994 /* can't put lower; would miss an extra frame */
995 sc->sc_flags |= SC_ERROR;
998 sc->sc_if.if_ierrors++;
1000 sc->sc_mp = sc->sc_buf = sc->sc_ep - SLRMAX;
1006 * Process an ioctl request.
1009 slioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1011 register struct ifaddr *ifa = (struct ifaddr *)data;
1012 register struct ifreq *ifr = (struct ifreq *)data;
1013 register int s, error = 0;
1014 struct sl_softc *sc = ifp->if_softc;
1022 * if.c will set the interface up even if we
1025 if (sc->sc_ttyp == NULL) {
1026 ifp->if_flags &= ~IFF_UP;
1031 * This is "historical" - set the interface up when
1032 * setting the address.
1034 if (ifa->ifa_addr->sa_family == AF_INET) {
1035 if (sc->sc_ttyp != NULL)
1036 ifp->if_flags |= IFF_UP;
1038 error = EAFNOSUPPORT;
1042 case SIOCSIFDSTADDR:
1043 if (ifa->ifa_addr->sa_family != AF_INET)
1044 error = EAFNOSUPPORT;
1053 * Set the interface MTU.
1055 if (ifr->ifr_mtu > SLTMAX)
1060 ifp->if_mtu = ifr->ifr_mtu;
1063 clist_alloc_cblocks(&tp->t_outq,
1064 SLIP_HIWAT + 2 * ifp->if_mtu + 1,
1065 SLIP_HIWAT + 2 * ifp->if_mtu + 1);
1077 sl_keepalive(void *chan)
1079 struct sl_softc *sc = chan;
1081 if (sc->sc_keepalive) {
1082 if (sc->sc_flags & SC_KEEPALIVE) {
1083 if (sc->sc_ttyp->t_pgrp != NULL) {
1084 PGRP_LOCK(sc->sc_ttyp->t_pgrp);
1085 pgsignal (sc->sc_ttyp->t_pgrp, SIGURG, 1);
1086 PGRP_UNLOCK(sc->sc_ttyp->t_pgrp);
1089 sc->sc_flags |= SC_KEEPALIVE;
1090 sc->sc_kahandle = timeout(sl_keepalive, sc, sc->sc_keepalive);
1092 sc->sc_flags &= ~SC_KEEPALIVE;
1097 sl_outfill(void *chan)
1099 struct sl_softc *sc = chan;
1100 register struct tty *tp = sc->sc_ttyp;
1103 if (sc->sc_outfill && tp != NULL) {
1104 if (sc->sc_flags & SC_OUTWAIT) {
1106 ++sc->sc_if.if_obytes;
1107 (void) putc(FRAME_END, &tp->t_outq);
1111 sc->sc_flags |= SC_OUTWAIT;
1112 sc->sc_ofhandle = timeout(sl_outfill, sc, sc->sc_outfill);
1114 sc->sc_flags &= ~SC_OUTWAIT;