#include "use_sl.h"
#include "opt_inet.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/caps.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/dkstat.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/fcntl.h>
#include <sys/signalvar.h>
#include <sys/tty.h>
#include <sys/kernel.h>
#include <sys/sysctl.h>
#include <sys/conf.h>
#include <net/if.h>
#include <net/if_types.h>
#include <net/ifq_var.h>
#include <net/netisr.h>
#if INET
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/in_var.h>
#include <netinet/ip.h>
#else
#error "Huh? Slip without inet?"
#endif
#include <net/slcompress.h>
#include "if_slvar.h"
#include <net/slip.h>
#include <net/bpf.h>
static void slattach (void *);
PSEUDO_SET(slattach, if_sl);
static int sliffopts = IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST;
SYSCTL_INT(_net, OID_AUTO, sliffopts, CTLFLAG_RW,
&sliffopts, 0, "");
TUNABLE_INT("net.sliffopts", &sliffopts);
#define BUFOFFSET (128+sizeof(struct ifnet **)+SLIP_HDRLEN)
#define SLRMAX (MCLBYTES - BUFOFFSET)
#define SLBUFSIZE (SLRMAX + BUFOFFSET)
#ifndef SLMTU
#define SLMTU 552
#endif
#define SLTMAX 1500
#define SLIP_HIWAT 50
#define CLISTRESERVE 1024
#define ABT_ESC '\033'
#define ABT_IDLE 1
#define ABT_COUNT 3
#define ABT_WINDOW (ABT_COUNT*2+2)
static struct sl_softc sl_softc[NSL];
#define FRAME_END 0xc0
#define FRAME_ESCAPE 0xdb
#define TRANS_FRAME_END 0xdc
#define TRANS_FRAME_ESCAPE 0xdd
static int slinit (struct sl_softc *);
static struct mbuf *sl_btom (struct sl_softc *, int);
static timeout_t sl_keepalive;
static timeout_t sl_outfill;
static int slclose (struct tty *,int);
static int slinput (int, struct tty *);
static int slioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
static int sltioctl (struct tty *, u_long, caddr_t, int, struct ucred *);
static int slopen (cdev_t, struct tty *);
static int sloutput (struct ifnet *,
struct mbuf *, struct sockaddr *, struct rtentry *);
static int slstart (struct tty *);
static struct linesw slipdisc = {
slopen, slclose, l_noread, l_nowrite,
sltioctl, slinput, slstart, ttymodem,
FRAME_END
};
static void
slattach(void *dummy)
{
struct sl_softc *sc;
int i = 0;
linesw[SLIPDISC] = slipdisc;
for (sc = sl_softc; i < NSL; sc++) {
if_initname(&(sc->sc_if), "sl", i++);
sc->sc_if.if_mtu = SLMTU;
sc->sc_if.if_flags = sliffopts;
sc->sc_if.if_type = IFT_SLIP;
sc->sc_if.if_ioctl = slioctl;
sc->sc_if.if_output = sloutput;
ifq_set_maxlen(&sc->sc_if.if_snd, 50);
ifq_set_ready(&sc->sc_if.if_snd);
sc->sc_fastq.ifq_maxlen = 32;
sc->sc_if.if_linkmib = sc;
sc->sc_if.if_linkmiblen = sizeof *sc;
callout_init_mp(&sc->sc_oftimeout);
callout_init_mp(&sc->sc_katimeout);
if_attach(&sc->sc_if, NULL);
bpfattach(&sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
}
}
static int
slinit(struct sl_softc *sc)
{
if (sc->sc_ep == NULL)
sc->sc_ep = kmalloc(SLBUFSIZE, M_DEVBUF, M_WAITOK);
sc->sc_buf = sc->sc_ep + SLBUFSIZE - SLRMAX;
sc->sc_mp = sc->sc_buf;
sl_compress_init(&sc->sc_comp, -1);
return (1);
}
static int
slopen(cdev_t dev, struct tty *tp)
{
struct sl_softc *sc;
int nsl;
int error;
error = caps_priv_check_self(SYSCAP_RESTRICTEDROOT);
if (error)
return (error);
lwkt_gettoken(&tty_token);
if (tp->t_line == SLIPDISC) {
lwkt_reltoken(&tty_token);
return (0);
}
for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++) {
if (sc->sc_ttyp == NULL && !(sc->sc_flags & SC_STATIC)) {
if (slinit(sc) == 0) {
lwkt_reltoken(&tty_token);
return (ENOBUFS);
}
tp->t_slsc = (caddr_t)sc;
sc->sc_ttyp = tp;
sc->sc_if.if_baudrate = tp->t_ospeed;
ttyflush(tp, FREAD | FWRITE);
tp->t_line = SLIPDISC;
clist_alloc_cblocks(&tp->t_canq, 0);
clist_alloc_cblocks(&tp->t_outq,
SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1);
clist_alloc_cblocks(&tp->t_rawq, 0);
if_up(&sc->sc_if);
lwkt_reltoken(&tty_token);
return (0);
}
}
lwkt_reltoken(&tty_token);
return (ENXIO);
}
static int
slclose(struct tty *tp, int flag)
{
struct sl_softc *sc;
lwkt_gettoken(&tty_token);
lwkt_gettoken(&tp->t_token);
ttyflush(tp, FREAD | FWRITE);
clist_free_cblocks(&tp->t_outq);
tp->t_line = 0;
sc = (struct sl_softc *)tp->t_slsc;
if (sc != NULL) {
if (sc->sc_outfill) {
sc->sc_outfill = 0;
callout_stop(&sc->sc_oftimeout);
}
if (sc->sc_keepalive) {
sc->sc_keepalive = 0;
callout_stop(&sc->sc_katimeout);
}
if_down(&sc->sc_if);
sc->sc_flags &= SC_STATIC;
sc->sc_ttyp = NULL;
tp->t_slsc = NULL;
if (sc->sc_ep) {
kfree(sc->sc_ep, M_DEVBUF);
sc->sc_ep = NULL;
}
sc->sc_mp = 0;
sc->sc_buf = 0;
}
lwkt_reltoken(&tp->t_token);
lwkt_reltoken(&tty_token);
return 0;
}
static int
sltioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct ucred *cred)
{
struct sl_softc *sc = (struct sl_softc *)tp->t_slsc, *nc, *tmpnc;
int nsl;
lwkt_gettoken(&tty_token);
lwkt_gettoken(&tp->t_token);
switch (cmd) {
case SLIOCGUNIT:
*(int *)data = sc->sc_if.if_dunit;
break;
case SLIOCSUNIT:
if (sc->sc_if.if_dunit != *(u_int *)data) {
for (nsl = NSL, nc = sl_softc; --nsl >= 0; nc++) {
if ( nc->sc_if.if_dunit == *(u_int *)data
&& nc->sc_ttyp == NULL
) {
tmpnc = kmalloc(sizeof *tmpnc, M_TEMP,
M_WAITOK);
*tmpnc = *nc;
*nc = *sc;
nc->sc_if = tmpnc->sc_if;
tmpnc->sc_if = sc->sc_if;
*sc = *tmpnc;
kfree(tmpnc, M_TEMP);
if (sc->sc_if.if_flags & IFF_UP) {
if_down(&sc->sc_if);
if (!(nc->sc_if.if_flags & IFF_UP))
if_up(&nc->sc_if);
} else if (nc->sc_if.if_flags & IFF_UP)
if_down(&nc->sc_if);
sc->sc_flags &= ~SC_STATIC;
sc->sc_flags |= (nc->sc_flags & SC_STATIC);
tp->t_slsc = sc = nc;
clist_alloc_cblocks(&tp->t_outq,
SLIP_HIWAT +
2 * sc->sc_if.if_mtu + 1);
sl_compress_init(&sc->sc_comp, -1);
goto slfound;
}
}
lwkt_reltoken(&tp->t_token);
lwkt_reltoken(&tty_token);
return (ENXIO);
}
slfound:
sc->sc_flags |= SC_STATIC;
break;
case SLIOCSKEEPAL:
sc->sc_keepalive = *(u_int *)data * hz;
if (sc->sc_keepalive) {
sc->sc_flags |= SC_KEEPALIVE;
callout_reset(&sc->sc_katimeout, sc->sc_keepalive,
sl_keepalive, sc);
} else {
if ((sc->sc_flags & SC_KEEPALIVE) != 0) {
callout_stop(&sc->sc_katimeout);
sc->sc_flags &= ~SC_KEEPALIVE;
}
}
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;
callout_reset(&sc->sc_oftimeout, sc->sc_outfill,
sl_outfill, sc);
} else {
if ((sc->sc_flags & SC_OUTWAIT) != 0) {
callout_stop(&sc->sc_oftimeout);
sc->sc_flags &= ~SC_OUTWAIT;
}
}
break;
case SLIOCGOUTFILL:
*(int *)data = sc->sc_outfill / hz;
break;
default:
lwkt_reltoken(&tp->t_token);
lwkt_reltoken(&tty_token);
return (ENOIOCTL);
}
lwkt_reltoken(&tp->t_token);
lwkt_reltoken(&tty_token);
return (0);
}
static int
sloutput_serialized(struct ifnet *ifp, struct ifaltq_subque *ifsq,
struct mbuf *m, struct sockaddr *dst, struct rtentry *rtp)
{
struct sl_softc *sc = &sl_softc[ifp->if_dunit];
struct ip *ip;
int error;
struct altq_pktattr pktattr;
ifq_classify(&ifp->if_snd, m, dst->sa_family, &pktattr);
if (dst->sa_family != AF_INET) {
kprintf("%s: af%d not supported\n", sc->sc_if.if_xname,
dst->sa_family);
m_freem(m);
IFNET_STAT_INC(&sc->sc_if, noproto, 1);
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);
}
ip = mtod(m, struct ip *);
if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
m_freem(m);
return (ENETRESET);
}
if ((ip->ip_tos & IPTOS_LOWDELAY) &&
!ifq_is_enabled(&sc->sc_if.if_snd)) {
if (IF_QFULL(&sc->sc_fastq)) {
IF_DROP(&sc->sc_fastq);
m_freem(m);
error = ENOBUFS;
} else {
IF_ENQUEUE(&sc->sc_fastq, m);
error = 0;
}
} else {
error = ifsq_enqueue(ifsq, m, &pktattr);
}
if (error) {
IFNET_STAT_INC(&sc->sc_if, oqdrops, 1);
return (error);
}
if (sc->sc_ttyp->t_outq.c_cc == 0)
slstart(sc->sc_ttyp);
return (0);
}
static int
sloutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
struct rtentry *rtp)
{
struct ifaltq_subque *ifsq = ifq_get_subq_default(&ifp->if_snd);
int error;
ifsq_serialize_hw(ifsq);
error = sloutput_serialized(ifp, ifsq, m, dst, rtp);
ifsq_deserialize_hw(ifsq);
return error;
}
static int
slstart(struct tty *tp)
{
struct sl_softc *sc = (struct sl_softc *)tp->t_slsc;
struct ifaltq_subque *ifsq = ifq_get_subq_default(&sc->sc_if.if_snd);
struct mbuf *m;
u_char *cp;
struct ip *ip;
u_char bpfbuf[SLTMAX + SLIP_HDRLEN];
int len = 0;
lwkt_gettoken(&tp->t_token);
for (;;) {
(*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) {
lwkt_reltoken(&tp->t_token);
return 0;
}
}
if (sc == NULL) {
lwkt_reltoken(&tp->t_token);
return 0;
}
IF_DEQUEUE(&sc->sc_fastq, m);
if (m)
IFNET_STAT_INC(&sc->sc_if, omcasts, 1);
else
m = ifsq_dequeue(ifsq);
if (m == NULL) {
lwkt_reltoken(&tp->t_token);
return 0;
}
if (sc->sc_if.if_bpf) {
struct mbuf *m1 = m;
u_char *cp = bpfbuf + SLIP_HDRLEN;
len = 0;
do {
int mlen = m1->m_len;
bcopy(mtod(m1, caddr_t), cp, mlen);
cp += mlen;
len += mlen;
} while ((m1 = m1->m_next) != NULL);
}
ip = mtod(m, struct ip *);
if (ip->ip_v == IPVERSION && ip->ip_p == IPPROTO_TCP) {
if (sc->sc_if.if_flags & SC_COMPRESS)
*mtod(m, u_char *) |= sl_compress_tcp(m, ip,
&sc->sc_comp, 1);
}
if (sc->sc_if.if_bpf) {
bpf_gettoken();
if (sc->sc_if.if_bpf) {
bpfbuf[SLX_DIR] = SLIPDIR_OUT;
bcopy(mtod(m, caddr_t), &bpfbuf[SLX_CHDR],
CHDR_LEN);
bpf_tap(sc->sc_if.if_bpf, bpfbuf,
len + SLIP_HDRLEN);
}
bpf_reltoken();
}
sc->sc_flags &= ~SC_OUTWAIT;
if (tp->t_outq.c_cc == 0) {
IFNET_STAT_INC(&sc->sc_if, obytes, 1);
clist_putc(FRAME_END, &tp->t_outq);
}
while (m) {
u_char *ep;
cp = mtod(m, u_char *); ep = cp + m->m_len;
while (cp < ep) {
u_char *bp = cp;
while (cp < ep) {
switch (*cp++) {
case FRAME_ESCAPE:
case FRAME_END:
--cp;
goto out;
}
}
out:
if (cp > bp) {
if (clist_btoq((char *)bp, cp - bp,
&tp->t_outq)) {
break;
}
IFNET_STAT_INC(&sc->sc_if, obytes,
cp - bp);
}
if (cp < ep) {
if (clist_putc(FRAME_ESCAPE, &tp->t_outq))
break;
if (clist_putc(*cp++ == FRAME_ESCAPE ?
TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
&tp->t_outq)) {
clist_unputc(&tp->t_outq);
break;
}
IFNET_STAT_INC(&sc->sc_if, obytes, 2);
}
}
m = m_free(m);
}
if (clist_putc(FRAME_END, &tp->t_outq)) {
clist_unputc(&tp->t_outq);
clist_putc(FRAME_END, &tp->t_outq);
IFNET_STAT_INC(&sc->sc_if, collisions, 1);
} else {
IFNET_STAT_INC(&sc->sc_if, obytes, 1);
IFNET_STAT_INC(&sc->sc_if, opackets, 1);
}
}
lwkt_reltoken(&tp->t_token);
return 0;
}
static struct mbuf *
sl_btom(struct sl_softc *sc, int len)
{
struct mbuf *m;
if (len >= MCLBYTES)
return (NULL);
MGETHDR(m, M_NOWAIT, MT_DATA);
if (m == NULL)
return (NULL);
if (len >= MHLEN) {
MCLGET(m, M_NOWAIT);
if ((m->m_flags & M_EXT) == 0) {
m_free(m);
return (NULL);
}
}
bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len);
m->m_len = len;
m->m_pkthdr.len = len;
m->m_pkthdr.rcvif = &sc->sc_if;
return (m);
}
static int
slinput(int c, struct tty *tp)
{
struct sl_softc *sc;
struct mbuf *m;
int len;
u_char chdr[CHDR_LEN];
#if 0
kprintf(" %02x", (unsigned char)c);
if ((unsigned char)c == 0xC0)
kprintf("\n");
#endif
lwkt_gettoken(&tp->t_token);
tk_nin++;
sc = (struct sl_softc *)tp->t_slsc;
if (sc == NULL) {
lwkt_reltoken(&tp->t_token);
#if 0
kprintf("X");
#endif
return 0;
}
if (c & TTY_ERRORMASK || (tp->t_state & TS_CONNECTED) == 0) {
sc->sc_flags |= SC_ERROR;
lwkt_reltoken(&tp->t_token);
#if 0
kprintf("Y");
#endif
return 0;
}
c &= TTY_CHARMASK;
IFNET_STAT_INC(&sc->sc_if, ibytes, 1);
if (sc->sc_if.if_flags & IFF_DEBUG) {
if (c == ABT_ESC) {
if (sc->sc_abortcount &&
time_uptime >= sc->sc_starttime + ABT_WINDOW)
sc->sc_abortcount = 0;
if (time_uptime >= sc->sc_lasttime + ABT_IDLE) {
if (++sc->sc_abortcount == 1)
sc->sc_starttime = time_uptime;
if (sc->sc_abortcount >= ABT_COUNT) {
slclose(tp,0);
lwkt_reltoken(&tp->t_token);
return 0;
}
}
} else
sc->sc_abortcount = 0;
sc->sc_lasttime = time_uptime;
}
switch (c) {
case TRANS_FRAME_ESCAPE:
if (sc->sc_escape)
c = FRAME_ESCAPE;
break;
case TRANS_FRAME_END:
if (sc->sc_escape)
c = FRAME_END;
break;
case FRAME_ESCAPE:
sc->sc_escape = 1;
lwkt_reltoken(&tp->t_token);
return 0;
case FRAME_END:
sc->sc_flags &= ~SC_KEEPALIVE;
if(sc->sc_flags & SC_ERROR) {
sc->sc_flags &= ~SC_ERROR;
goto newpack;
}
len = sc->sc_mp - sc->sc_buf;
if (len < 3)
goto newpack;
if (sc->sc_if.if_bpf) {
bcopy(sc->sc_buf, chdr, CHDR_LEN);
}
if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) {
if (c & 0x80)
c = TYPE_COMPRESSED_TCP;
else if (c == TYPE_UNCOMPRESSED_TCP)
*sc->sc_buf &= 0x4f;
if (sc->sc_if.if_flags & SC_COMPRESS) {
len = sl_uncompress_tcp(&sc->sc_buf, len,
(u_int)c, &sc->sc_comp);
if (len <= 0)
goto error;
} else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
len = sl_uncompress_tcp(&sc->sc_buf, len,
(u_int)c, &sc->sc_comp);
if (len <= 0)
goto error;
sc->sc_if.if_flags |= SC_COMPRESS;
} else
goto error;
}
if (sc->sc_if.if_bpf) {
bpf_gettoken();
if (sc->sc_if.if_bpf) {
u_char *hp = sc->sc_buf - SLIP_HDRLEN;
hp[SLX_DIR] = SLIPDIR_IN;
bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN);
bpf_tap(sc->sc_if.if_bpf, hp,
len + SLIP_HDRLEN);
}
bpf_reltoken();
}
m = sl_btom(sc, len);
if (m == NULL)
goto error;
IFNET_STAT_INC(&sc->sc_if, ipackets, 1);
if ((sc->sc_if.if_flags & IFF_UP) == 0) {
m_freem(m);
goto newpack;
}
if (netisr_queue(NETISR_IP, m)) {
IFNET_STAT_INC(&sc->sc_if, ierrors, 1);
IFNET_STAT_INC(&sc->sc_if, iqdrops, 1);
}
goto newpack;
}
if (sc->sc_mp < sc->sc_ep + SLBUFSIZE) {
*sc->sc_mp++ = c;
sc->sc_escape = 0;
lwkt_reltoken(&tp->t_token);
return 0;
}
sc->sc_flags |= SC_ERROR;
error:
IFNET_STAT_INC(&sc->sc_if, ierrors, 1);
newpack:
sc->sc_mp = sc->sc_buf = sc->sc_ep + SLBUFSIZE - SLRMAX;
sc->sc_escape = 0;
lwkt_reltoken(&tp->t_token);
return 0;
}
static int
slioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
{
struct ifaddr *ifa = (struct ifaddr *)data;
struct ifreq *ifr = (struct ifreq *)data;
int error = 0;
switch (cmd) {
case SIOCSIFFLAGS:
if (sl_softc[ifp->if_dunit].sc_ttyp == NULL) {
ifp->if_flags &= ~IFF_UP;
}
break;
case SIOCSIFADDR:
if (ifa->ifa_addr->sa_family == AF_INET) {
if (sl_softc[ifp->if_dunit].sc_ttyp != NULL)
ifp->if_flags |= IFF_UP;
} else {
error = EAFNOSUPPORT;
}
break;
case SIOCSIFDSTADDR:
if (ifa->ifa_addr->sa_family != AF_INET)
error = EAFNOSUPPORT;
break;
case SIOCADDMULTI:
case SIOCDELMULTI:
break;
case SIOCSIFMTU:
if (ifr->ifr_mtu > SLTMAX)
error = EINVAL;
else {
struct tty *tp;
ifp->if_mtu = ifr->ifr_mtu;
tp = sl_softc[ifp->if_dunit].sc_ttyp;
if (tp != NULL)
clist_alloc_cblocks(&tp->t_outq,
SLIP_HIWAT + 2 * ifp->if_mtu + 1);
}
break;
default:
error = EINVAL;
}
return (error);
}
static void
sl_keepalive(void *chan)
{
struct sl_softc *sc = chan;
struct tty *tp = sc->sc_ttyp;
struct pgrp *pg;
lwkt_gettoken(&tp->t_token);
if (sc->sc_keepalive) {
if (sc->sc_flags & SC_KEEPALIVE) {
pg = sc->sc_ttyp->t_pgrp;
if (pg) {
pgref(pg);
pgsignal (pg, SIGURG, 1);
pgrel(pg);
}
} else {
sc->sc_flags |= SC_KEEPALIVE;
}
callout_reset(&sc->sc_katimeout, sc->sc_keepalive,
sl_keepalive, sc);
} else {
sc->sc_flags &= ~SC_KEEPALIVE;
}
lwkt_reltoken(&tp->t_token);
}
static void
sl_outfill(void *chan)
{
struct sl_softc *sc = chan;
struct tty *tp = sc->sc_ttyp;
lwkt_gettoken(&tp->t_token);
if (sc->sc_outfill && tp != NULL) {
if (sc->sc_flags & SC_OUTWAIT) {
IFNET_STAT_INC(&sc->sc_if, obytes, 1);
clist_putc(FRAME_END, &tp->t_outq);
(*tp->t_oproc)(tp);
} else
sc->sc_flags |= SC_OUTWAIT;
callout_reset(&sc->sc_oftimeout, sc->sc_outfill,
sl_outfill, sc);
} else {
sc->sc_flags &= ~SC_OUTWAIT;
}
lwkt_reltoken(&tp->t_token);
}
MODULE_VERSION(if_sl, 1);