#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/conf.h>
#include <sys/proc.h>
#include <sys/caps.h>
#include <sys/mbuf.h>
#include <sys/malloc.h>
#include <sys/fcntl.h>
#include <sys/tty.h>
#include <sys/ttycom.h>
#include <sys/syslog.h>
#include <sys/errno.h>
#include <netgraph/ng_message.h>
#include <netgraph/netgraph.h>
#include "ng_tty.h"
#define MAX_MBUFQ 3
#define NGT_HIWATER 400
struct ngt_sc {
struct tty *tp;
node_p node;
hook_p hook;
struct mbuf *m;
struct mbuf *qhead, **qtail;
short qlen;
short hotchar;
u_int flags;
struct callout ctimeout;
};
typedef struct ngt_sc *sc_p;
#define FLG_TIMEOUT 0x0001
#define FLG_DEBUG 0x0002
#ifdef INVARIANTS
#define QUEUECHECK(sc) \
do { \
struct mbuf **mp; \
int k; \
\
for (k = 0, mp = &sc->qhead; \
k <= MAX_MBUFQ && *mp; \
k++, mp = &(*mp)->m_nextpkt); \
if (k != sc->qlen || k > MAX_MBUFQ || *mp || mp != sc->qtail) \
panic("%s: queue", __func__); \
} while (0)
#else
#define QUEUECHECK(sc) do {} while (0)
#endif
static int ngt_open(cdev_t dev, struct tty *tp);
static int ngt_close(struct tty *tp, int flag);
static int ngt_read(struct tty *tp, struct uio *uio, int flag);
static int ngt_write(struct tty *tp, struct uio *uio, int flag);
static int ngt_tioctl(struct tty *tp,
u_long cmd, caddr_t data, int flag, struct ucred *cred);
static int ngt_input(int c, struct tty *tp);
static int ngt_start(struct tty *tp);
static ng_constructor_t ngt_constructor;
static ng_rcvmsg_t ngt_rcvmsg;
static ng_shutdown_t ngt_shutdown;
static ng_newhook_t ngt_newhook;
static ng_rcvdata_t ngt_rcvdata;
static ng_disconnect_t ngt_disconnect;
static int ngt_mod_event(module_t mod, int event, void *data);
static void ngt_timeout(void *arg);
#define ERROUT(x) do { error = (x); goto done; } while (0)
static struct linesw ngt_disc = {
ngt_open,
ngt_close,
ngt_read,
ngt_write,
ngt_tioctl,
ngt_input,
ngt_start,
ttymodem,
NG_TTY_DFL_HOTCHAR
};
static struct ng_type typestruct = {
NG_VERSION,
NG_TTY_NODE_TYPE,
ngt_mod_event,
ngt_constructor,
ngt_rcvmsg,
ngt_shutdown,
ngt_newhook,
NULL,
NULL,
ngt_rcvdata,
ngt_rcvdata,
ngt_disconnect,
NULL
};
NETGRAPH_INIT(tty, &typestruct);
static int ngt_unit;
static int ngt_nodeop_ok;
static int ngt_ldisc;
static int
ngt_open(cdev_t dev, struct tty *tp)
{
char name[sizeof(NG_TTY_NODE_TYPE) + 8];
sc_p sc;
int error;
if ((error = caps_priv_check_self(SYSCAP_RESTRICTEDROOT)))
return (error);
lwkt_gettoken(&tp->t_token);
if (tp->t_line == NETGRAPHDISC) {
sc = (sc_p) tp->t_sc;
if (sc != NULL && sc->tp == tp)
goto done;
}
sc = kmalloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
sc->tp = tp;
sc->hotchar = NG_TTY_DFL_HOTCHAR;
sc->qtail = &sc->qhead;
QUEUECHECK(sc);
callout_init_mp(&sc->ctimeout);
ngt_nodeop_ok = 1;
error = ng_make_node_common(&typestruct, &sc->node);
ngt_nodeop_ok = 0;
if (error) {
kfree(sc, M_NETGRAPH);
goto done;
}
ksnprintf(name, sizeof(name), "%s%d", typestruct.name, ngt_unit++);
sc->node->private = sc;
tp->t_sc = (caddr_t) sc;
if ((error = ng_name_node(sc->node, name))) {
log(LOG_ERR, "%s: node name exists?\n", name);
ngt_nodeop_ok = 1;
ng_rmnode(sc->node);
ngt_nodeop_ok = 0;
goto done;
}
ttyflush(tp, FREAD | FWRITE);
clist_alloc_cblocks(&tp->t_canq, 0);
clist_alloc_cblocks(&tp->t_rawq, 0);
clist_alloc_cblocks(&tp->t_outq, MLEN + NGT_HIWATER);
done:
lwkt_reltoken(&tp->t_token);
return (error);
}
static int
ngt_close(struct tty *tp, int flag)
{
const sc_p sc = (sc_p) tp->t_sc;
lwkt_gettoken(&tp->t_token);
ttyflush(tp, FREAD | FWRITE);
clist_free_cblocks(&tp->t_outq);
tp->t_line = 0;
if (sc != NULL) {
if (sc->flags & FLG_TIMEOUT) {
callout_stop(&sc->ctimeout);
sc->flags &= ~FLG_TIMEOUT;
}
ngt_nodeop_ok = 1;
ng_rmnode(sc->node);
ngt_nodeop_ok = 0;
tp->t_sc = NULL;
}
lwkt_reltoken(&tp->t_token);
return (0);
}
static int
ngt_read(struct tty *tp, struct uio *uio, int flag)
{
return (EIO);
}
static int
ngt_write(struct tty *tp, struct uio *uio, int flag)
{
return (EIO);
}
static int
ngt_tioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct ucred *cred)
{
const sc_p sc = (sc_p) tp->t_sc;
int error = 0;
lwkt_gettoken(&tp->t_token);
switch (cmd) {
case NGIOCGINFO:
{
struct nodeinfo *const ni = (struct nodeinfo *) data;
const node_p node = sc->node;
bzero(ni, sizeof(*ni));
if (node->name)
strncpy(ni->name, node->name, sizeof(ni->name) - 1);
strncpy(ni->type, node->type->name, sizeof(ni->type) - 1);
ni->id = (u_int32_t)(uintptr_t)node;
ni->hooks = node->numhooks;
break;
}
default:
ERROUT(ENOIOCTL);
}
done:
lwkt_reltoken(&tp->t_token);
return (error);
}
static int
ngt_input(int c, struct tty *tp)
{
const sc_p sc = (sc_p) tp->t_sc;
const node_p node = sc ? sc->node : NULL;
struct mbuf *m;
int error = 0;
lwkt_gettoken(&tp->t_token);
if (!sc || tp != sc->tp) {
lwkt_reltoken(&tp->t_token);
return (0);
}
if (!sc->hook)
ERROUT(0);
if ((tp->t_state & TS_CONNECTED) == 0) {
if (sc->flags & FLG_DEBUG)
log(LOG_DEBUG, "%s: no carrier\n", node->name);
ERROUT(0);
}
if (c & TTY_ERRORMASK) {
if (sc->flags & FLG_DEBUG)
log(LOG_DEBUG, "%s: line error %x\n",
node->name, c & TTY_ERRORMASK);
ERROUT(0);
}
c &= TTY_CHARMASK;
if (!(m = sc->m)) {
MGETHDR(m, M_NOWAIT, MT_DATA);
if (!m) {
if (sc->flags & FLG_DEBUG)
log(LOG_ERR,
"%s: can't get mbuf\n", node->name);
ERROUT(ENOBUFS);
}
m->m_len = m->m_pkthdr.len = 0;
m->m_pkthdr.rcvif = NULL;
sc->m = m;
}
*mtod(m, u_char *) = c;
m->m_data++;
m->m_len++;
m->m_pkthdr.len++;
if (sc->hotchar == -1 || c == sc->hotchar || m->m_len >= MHLEN) {
m->m_data = m->m_pktdat;
error = ng_queue_data(sc->hook, m, NULL);
sc->m = NULL;
}
done:
lwkt_reltoken(&tp->t_token);
return (error);
}
static int
ngt_start(struct tty *tp)
{
const sc_p sc = (sc_p) tp->t_sc;
lwkt_gettoken(&tp->t_token);
while (tp->t_outq.c_cc < NGT_HIWATER) {
struct mbuf *m = sc->qhead;
if (!m)
break;
if ((sc->qhead = m->m_nextpkt) == NULL)
sc->qtail = &sc->qhead;
sc->qlen--;
QUEUECHECK(sc);
while (m) {
int sent;
sent = m->m_len - clist_btoq(mtod(m, u_char *),
m->m_len, &tp->t_outq);
m->m_data += sent;
m->m_len -= sent;
if (m->m_len > 0)
break;
m = m_free(m);
}
if (m) {
m->m_nextpkt = sc->qhead;
sc->qhead = m;
if (sc->qtail == &sc->qhead)
sc->qtail = &m->m_nextpkt;
sc->qlen++;
QUEUECHECK(sc);
break;
}
}
if (tp->t_oproc != NULL)
(*tp->t_oproc) (tp);
if (sc->qhead && (sc->flags & FLG_TIMEOUT) == 0) {
callout_reset(&sc->ctimeout, 1, ngt_timeout, sc);
sc->flags |= FLG_TIMEOUT;
}
lwkt_reltoken(&tp->t_token);
return (0);
}
static void
ngt_timeout(void *arg)
{
const sc_p sc = (sc_p) arg;
struct tty *tp = sc->tp;
lwkt_gettoken(&tp->t_token);
sc->flags &= ~FLG_TIMEOUT;
ngt_start(tp);
lwkt_reltoken(&tp->t_token);
}
static int
ngt_constructor(node_p *nodep)
{
if (!ngt_nodeop_ok)
return (EOPNOTSUPP);
return (ng_make_node_common(&typestruct, nodep));
}
static int
ngt_newhook(node_p node, hook_p hook, const char *name)
{
const sc_p sc = node->private;
struct tty *tp = sc->tp;
int error = 0;
if (strcmp(name, NG_TTY_HOOK))
return (EINVAL);
lwkt_gettoken(&tp->t_token);
if (sc->hook)
ERROUT(EISCONN);
sc->hook = hook;
done:
lwkt_reltoken(&tp->t_token);
return (error);
}
static int
ngt_disconnect(hook_p hook)
{
const sc_p sc = hook->node->private;
struct tty *tp = sc->tp;
lwkt_gettoken(&tp->t_token);
if (hook != sc->hook)
panic(__func__);
sc->hook = NULL;
m_freem(sc->m);
sc->m = NULL;
lwkt_reltoken(&tp->t_token);
return (0);
}
static int
ngt_shutdown(node_p node)
{
const sc_p sc = node->private;
struct tty *tp = sc->tp;
if (!ngt_nodeop_ok)
return (EOPNOTSUPP);
lwkt_gettoken(&tp->t_token);
ng_unname(node);
ng_cutlinks(node);
node->private = NULL;
ng_unref(sc->node);
m_freem(sc->qhead);
m_freem(sc->m);
bzero(sc, sizeof(*sc));
kfree(sc, M_NETGRAPH);
lwkt_reltoken(&tp->t_token);
return (0);
}
static int
ngt_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
{
const sc_p sc = hook->node->private;
struct tty *tp = sc->tp;
int error = 0;
if (hook != sc->hook)
panic(__func__);
NG_FREE_META(meta);
lwkt_gettoken(&tp->t_token);
if (sc->qlen >= MAX_MBUFQ)
ERROUT(ENOBUFS);
m->m_nextpkt = NULL;
*sc->qtail = m;
sc->qtail = &m->m_nextpkt;
sc->qlen++;
QUEUECHECK(sc);
m = NULL;
if (sc->qlen == 1)
ngt_start(sc->tp);
done:
lwkt_reltoken(&tp->t_token);
if (m)
m_freem(m);
return (error);
}
static int
ngt_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
struct ng_mesg **rptr)
{
const sc_p sc = (sc_p) node->private;
struct tty *tp = sc->tp;
struct ng_mesg *resp = NULL;
int error = 0;
lwkt_gettoken(&tp->t_token);
switch (msg->header.typecookie) {
case NGM_TTY_COOKIE:
switch (msg->header.cmd) {
case NGM_TTY_SET_HOTCHAR:
{
int hotchar;
if (msg->header.arglen != sizeof(int))
ERROUT(EINVAL);
hotchar = *((int *) msg->data);
if (hotchar != (u_char) hotchar && hotchar != -1)
ERROUT(EINVAL);
sc->hotchar = hotchar;
break;
}
case NGM_TTY_GET_HOTCHAR:
NG_MKRESPONSE(resp, msg, sizeof(int), M_NOWAIT);
if (!resp)
ERROUT(ENOMEM);
*((int *) resp->data) = sc->hotchar;
break;
default:
ERROUT(EINVAL);
}
break;
default:
ERROUT(EINVAL);
}
if (rptr)
*rptr = resp;
else if (resp)
kfree(resp, M_NETGRAPH);
done:
kfree(msg, M_NETGRAPH);
lwkt_reltoken(&tp->t_token);
return (error);
}
static int
ngt_mod_event(module_t mod, int event, void *data)
{
int error = 0;
switch (event) {
case MOD_LOAD:
if ((ngt_ldisc = ldisc_register(NETGRAPHDISC, &ngt_disc)) < 0) {
log(LOG_ERR, "%s: can't register line discipline",
__func__);
return (EIO);
}
break;
case MOD_UNLOAD:
ldisc_deregister(ngt_ldisc);
break;
default:
error = EOPNOTSUPP;
break;
}
return (error);
}