📄 if_sl.c
字号:
/* * Copyright (c) 1987, 1989, 1992, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)if_sl.c 8.6 (Berkeley) 2/1/94 *//* * Serial Line interface * * Rick Adams * Center for Seismic Studies * 1300 N 17th Street, Suite 1450 * Arlington, Virginia 22209 * (703)276-7900 * rick@seismo.ARPA * seismo!rick * * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris). * N.B.: this belongs in netinet, not net, the way it stands now. * Should have a link-layer type designation, but wouldn't be * backwards-compatible. * * Converted to 4.3BSD Beta by Chris Torek. * Other changes made at Berkeley, based in part on code by Kirk Smith. * W. Jolitz added slip abort. * * Hacked almost beyond recognition by Van Jacobson (van@helios.ee.lbl.gov). * Added priority queuing for "interactive" traffic; hooks for TCP * header compression; ICMP filtering (at 2400 baud, some cretin * pinging you can use up all your bandwidth). Made low clist behavior * more robust and slightly less likely to hang serial line. * Sped up a bunch of things. * * Note that splimp() is used throughout to block both (tty) input * interrupts and network activity; thus, splimp must be >= spltty. */#include "sl.h"#if NSL > 0#include "bpfilter.h"#include <sys/param.h>#include <sys/systm.h>#include <sys/proc.h>#include <sys/mbuf.h>#include <sys/buf.h>#include <sys/dkstat.h>#include <sys/socket.h>#include <sys/ioctl.h>#include <sys/file.h>#include <sys/signalvar.h>#include <sys/tty.h>#include <sys/clist.h>#include <sys/kernel.h>#include <sys/conf.h>#include <machine/cpu.h>#include <net/if.h>#include <net/if_types.h>#include <net/netisr.h>#include <net/route.h>#if INET#include <netinet/in.h>#include <netinet/in_systm.h>#include <netinet/in_var.h>#include <netinet/ip.h>#elseHuh? Slip without inet?#endif#include <net/slcompress.h>#include <net/if_slvar.h>#include <net/slip.h>#if NBPFILTER > 0#include <sys/time.h>#include <net/bpf.h>#endif/* * SLRMAX is a hard limit on input packet size. To simplify the code * and improve performance, we require that packets fit in an mbuf * cluster, and if we get a compressed packet, there's enough extra * room to expand the header into a max length tcp/ip header (128 * bytes). So, SLRMAX can be at most * MCLBYTES - 128 * * SLMTU is the default transmit MTU. The transmit MTU should be kept * small enough so that interactive use doesn't suffer, but large * enough to provide good performance. 552 is a good choice for SLMTU * because it is high enough to not fragment TCP packets being routed * through this host. Packet fragmentation is bad with SLIP because * fragment headers aren't compressed. The previous assumptions about * the best MTU value don't really hold when using modern modems with * BTLZ data compression because the modem buffers play a much larger * role in interactive performance than the MTU. The MTU can be changed * at any time to suit the specific environment with ifconfig(8), and * its maximum value is defined as SLTMAX. SLTMAX must not be so large * that it would overflow the stack if BPF is configured (XXX; if_ppp.c * handles this better). * * SLIP_HIWAT is the amount of data that will be queued 'downstream' * of us (i.e., in clists waiting to be picked up by the tty output * interrupt). If we queue a lot of data downstream, it's immune to * our t.o.s. queuing. * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed * telnet/ftp will see a 1 sec wait, independent of the mtu (the * wait is dependent on the ftp window size but that's typically * 1k - 4k). So, we want SLIP_HIWAT just big enough to amortize * the cost (in idle time on the wire) of the tty driver running * off the end of its clists & having to call back slstart for a * new packet. For a tty interface with any buffering at all, this * cost will be zero. Even with a totally brain dead interface (like * the one on a typical workstation), the cost will be <= 1 character * time. So, setting SLIP_HIWAT to ~100 guarantees that we'll lose * at most 1% while maintaining good interactive response. */#if NBPFILTER > 0#define BUFOFFSET (128+sizeof(struct ifnet **)+SLIP_HDRLEN)#else#define BUFOFFSET (128+sizeof(struct ifnet **))#endif#define SLRMAX (MCLBYTES - BUFOFFSET)#define SLBUFSIZE (SLRMAX + BUFOFFSET)#ifndef SLMTU#define SLMTU 552 /* default MTU */#endif#define SLTMAX 1500 /* maximum MTU */#define SLIP_HIWAT roundup(50,CBSIZE)#define CLISTRESERVE 1024 /* Can't let clists get too low *//* * SLIP ABORT ESCAPE MECHANISM: * (inspired by HAYES modem escape arrangement) * 1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape } * within window time signals a "soft" exit from slip mode by remote end * if the IFF_DEBUG flag is on. */#define ABT_ESC '\033' /* can't be t_intr - distant host must know it*/#define ABT_IDLE 1 /* in seconds - idle before an escape */#define ABT_COUNT 3 /* count of escapes for abort */#define ABT_WINDOW (ABT_COUNT*2+2) /* in seconds - time to count */struct sl_softc sl_softc[NSL];#define FRAME_END 0xc0 /* Frame End */#define FRAME_ESCAPE 0xdb /* Frame Esc */#define TRANS_FRAME_END 0xdc /* transposed frame end */#define TRANS_FRAME_ESCAPE 0xdd /* transposed frame esc */static int slinit __P((struct sl_softc *));static struct mbuf *sl_btom __P((struct sl_softc *, int));static timeout_t sl_keepalive;static timeout_t sl_outfill;#define ttyerrio ((int (*) __P((struct tty *, struct uio *, int)))enodev)static struct linesw slipdisc = { slopen, slclose, ttyerrio, ttyerrio, sltioctl, slinput, slstart, ttymodem };/* * Called from boot code to establish sl interfaces. */voidslattach(){ register struct sl_softc *sc; register int i = 0; linesw[SLIPDISC] = slipdisc; for (sc = sl_softc; i < NSL; sc++) { sc->sc_if.if_name = "sl"; sc->sc_if.if_next = NULL; sc->sc_if.if_unit = i++; sc->sc_if.if_mtu = SLMTU; sc->sc_if.if_flags = IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST; sc->sc_if.if_type = IFT_SLIP; sc->sc_if.if_ioctl = slioctl; sc->sc_if.if_output = sloutput; sc->sc_if.if_snd.ifq_maxlen = 50; sc->sc_fastq.ifq_maxlen = 32; if_attach(&sc->sc_if);#if NBPFILTER > 0 bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_SLIP, SLIP_HDRLEN);#endif }}PSEUDO_SET(slattach, if_sl);static intslinit(sc) register struct sl_softc *sc;{ register caddr_t p; if (sc->sc_ep == (u_char *) 0) { MCLALLOC(p, M_WAIT); if (p) sc->sc_ep = (u_char *)p + SLBUFSIZE; else { printf("sl%d: can't allocate buffer\n", sc - sl_softc); return (0); } } sc->sc_buf = sc->sc_ep - SLRMAX; sc->sc_mp = sc->sc_buf; sl_compress_init(&sc->sc_comp); return (1);}/* * Line specific open routine. * Attach the given tty to the first available sl unit. *//* ARGSUSED */intslopen(dev, tp) dev_t dev; register struct tty *tp;{ struct proc *p = curproc; /* XXX */ register struct sl_softc *sc; register int nsl; int s, error; error = suser(p->p_ucred, &p->p_acflag); if (error) return (error); if (tp->t_line == SLIPDISC) return (0); for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++) if (sc->sc_ttyp == NULL) { if (slinit(sc) == 0) return (ENOBUFS); tp->t_sc = (caddr_t)sc; sc->sc_ttyp = tp; sc->sc_if.if_baudrate = tp->t_ospeed; ttyflush(tp, FREAD | FWRITE); tp->t_line = SLIPDISC; /* * We don't use t_canq or t_rawq, so reduce their * cblock resources to 0. Reserve enough cblocks * for t_outq to guarantee that we can fit a full * packet if the SLIP_HIWAT check allows slstart() * to loop. Use the same value for the cblock * limit since the reserved blocks should always * be enough. Reserving cblocks probably makes * the CLISTRESERVE check unnecessary and wasteful. */ clist_alloc_cblocks(&tp->t_canq, 0, 0); clist_alloc_cblocks(&tp->t_outq, SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1, SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1); clist_alloc_cblocks(&tp->t_rawq, 0, 0); s = splnet(); if_up(&sc->sc_if); splx(s); return (0); } return (ENXIO);}/* * Line specific close routine. * Detach the tty from the sl unit. */intslclose(tp,flag) struct tty *tp; int flag;{ register struct sl_softc *sc; int s; ttyflush(tp, FREAD | FWRITE); /* * XXX the placement of the following spl is misleading. tty * interrupts must be blocked across line discipline switches * and throughout closes to avoid races. */ s = splimp(); /* actually, max(spltty, splnet) */ clist_free_cblocks(&tp->t_outq); tp->t_line = 0; sc = (struct sl_softc *)tp->t_sc; if (sc != NULL) { if (sc->sc_outfill) { sc->sc_outfill = 0; untimeout(sl_outfill, sc); } if (sc->sc_keepalive) { sc->sc_keepalive = 0; untimeout(sl_keepalive, sc); } if_down(&sc->sc_if); sc->sc_flags = 0; sc->sc_ttyp = NULL; tp->t_sc = NULL; MCLFREE((caddr_t)(sc->sc_ep - SLBUFSIZE)); sc->sc_ep = 0; sc->sc_mp = 0; sc->sc_buf = 0; } splx(s); return 0;}/* * Line specific (tty) ioctl routine. * Provide a way to get the sl unit number. *//* ARGSUSED */intsltioctl(tp, cmd, data, flag, p) struct tty *tp; int cmd; caddr_t data; int flag; struct proc *p;{ struct sl_softc *sc = (struct sl_softc *)tp->t_sc; int s; s = splimp(); switch (cmd) { case SLIOCGUNIT: *(int *)data = sc->sc_if.if_unit; break; case SLIOCSUNIT: sc->sc_if.if_unit = *(u_int *)data; break; case SLIOCSKEEPAL: sc->sc_keepalive = *(u_int *)data * hz; if (sc->sc_keepalive) { sc->sc_flags |= SC_KEEPALIVE; timeout(sl_keepalive, sc, sc->sc_keepalive); } else { sc->sc_flags &= ~SC_KEEPALIVE; untimeout(sl_keepalive, sc); } break; case SLIOCGKEEPAL: *(int *)data = sc->sc_keepalive / hz; break; case SLIOCSOUTFILL: sc->sc_outfill = *(u_int *)data * hz; if (sc->sc_outfill) { sc->sc_flags |= SC_OUTWAIT; timeout(sl_outfill, sc, sc->sc_outfill); } else { sc->sc_flags &= ~SC_OUTWAIT; untimeout(sl_outfill, sc); } break; case SLIOCGOUTFILL: *(int *)data = sc->sc_outfill / hz; break; default: splx(s); return (-1); } splx(s); return (0);}/* * Queue a packet. Start transmission if not active. * Compression happens in slstart; if we do it here, IP TOS * will cause us to not compress "background" packets, because * ordering gets trashed. It can be done for all packets in slstart. */intsloutput(ifp, m, dst, rtp) struct ifnet *ifp; register struct mbuf *m; struct sockaddr *dst; struct rtentry *rtp;{ register struct sl_softc *sc = &sl_softc[ifp->if_unit]; register struct ip *ip; register struct ifqueue *ifq; int s; /* * `Cannot happen' (see slioctl). Someday we will extend * the line protocol to support other address families. */ if (dst->sa_family != AF_INET) { printf("sl%d: af%d not supported\n", sc->sc_if.if_unit, dst->sa_family); m_freem(m); sc->sc_if.if_noproto++; return (EAFNOSUPPORT); } if (sc->sc_ttyp == NULL || !(ifp->if_flags & IFF_UP)) { m_freem(m); return (ENETDOWN); } if ((sc->sc_ttyp->t_state & TS_CONNECTED) == 0) { m_freem(m); return (EHOSTUNREACH); } ifq = &sc->sc_if.if_snd; ip = mtod(m, struct ip *); if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) { m_freem(m); return (ENETRESET); /* XXX ? */ } if (ip->ip_tos & IPTOS_LOWDELAY) ifq = &sc->sc_fastq; s = splimp(); if (IF_QFULL(ifq)) { IF_DROP(ifq); m_freem(m); splx(s); sc->sc_if.if_oerrors++; return (ENOBUFS); } IF_ENQUEUE(ifq, m); if (sc->sc_ttyp->t_outq.c_cc == 0) slstart(sc->sc_ttyp); splx(s); return (0);}/* * Start output on interface. Get another datagram * to send from the interface queue and map it to * the interface before starting output. */intslstart(tp) register struct tty *tp;{ register struct sl_softc *sc = (struct sl_softc *)tp->t_sc; register struct mbuf *m; register u_char *cp; register struct ip *ip; int s; struct mbuf *m2;#if NBPFILTER > 0 u_char bpfbuf[SLTMAX + SLIP_HDRLEN]; register int len = 0;#endif for (;;) { /* * Call output process whether or not there is more in the * output queue. We are being called in lieu of ttstart * and must do what it would. */ (*tp->t_oproc)(tp); if (tp->t_outq.c_cc != 0) { if (sc != NULL) sc->sc_flags &= ~SC_OUTWAIT; if (tp->t_outq.c_cc > SLIP_HIWAT) return 0; } /* * This happens briefly when the line shuts down. */ if (sc == NULL)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -