#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/caps.h>
#include <sys/socket.h>
#include <sys/syslog.h>
#include <sys/tty.h>
#include <sys/ttycom.h>
#include <net/if.h>
#include <net/if_var.h>
#include <netgraph7/netgraph.h>
#include <netgraph7/ng_message.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 ifqueue outq;
struct mbuf *m;
short hotchar;
u_int flags;
struct callout chand;
};
typedef struct ngt_sc *sc_p;
#define FLG_DEBUG 0x0002
#define FLG_DIE 0x0004
static int ngt_open(struct cdev *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_connect_t ngt_connect;
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(node_p node, hook_p hook, void *arg1, int arg2);
#define ERROUT(x) do { error = (x); goto done; } while (0)
static struct linesw ngt_disc = {
.l_open = ngt_open,
.l_close = ngt_close,
.l_read = ngt_read,
.l_write = ngt_write,
.l_ioctl = ngt_tioctl,
.l_rint = ngt_input,
.l_start = ngt_start,
.l_modem = ttymodem,
};
static struct ng_type typestruct = {
.version = NG_ABI_VERSION,
.name = NG_TTY_NODE_TYPE,
.mod_event = ngt_mod_event,
.constructor = ngt_constructor,
.rcvmsg = ngt_rcvmsg,
.shutdown = ngt_shutdown,
.newhook = ngt_newhook,
.connect = ngt_connect,
.rcvdata = ngt_rcvdata,
.disconnect = ngt_disconnect,
};
NETGRAPH_INIT(tty, &typestruct);
static int ngt_unit;
static int ngt_ldisc;
static int
ngt_open(struct cdev *dev, struct tty *tp)
{
char name[sizeof(NG_TTY_NODE_TYPE) + 8];
sc_p sc;
int error;
error = caps_priv_check_self(SYSCAP_NONET_NETGRAPH);
if (error)
return (error);
sc = kmalloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
lwkt_gettoken(&tp->t_token);
sc->tp = tp;
sc->hotchar = NG_TTY_DFL_HOTCHAR;
sc->outq.ifq_maxlen = MAX_MBUFQ;
error = ng_make_node_common(&typestruct, &sc->node);
if (error) {
kfree(sc, M_NETGRAPH);
lwkt_reltoken(&tp->t_token);
return (error);
}
atomic_add_int(&ngt_unit, 1);
ksnprintf(name, sizeof(name), "%s%d", typestruct.name, ngt_unit);
if ((error = ng_name_node(sc->node, name))) {
sc->flags |= FLG_DIE;
NG_NODE_UNREF(sc->node);
log(LOG_ERR, "%s: node name exists?\n", name);
lwkt_reltoken(&tp->t_token);
return (error);
}
NG_NODE_SET_PRIVATE(sc->node, sc);
tp->t_sc = sc;
callout_init_mp(&sc->chand);
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);
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);
if (sc != NULL) {
if (callout_pending(&sc->chand))
ng_uncallout(&sc->chand, sc->node);
tp->t_sc = NULL;
sc->flags |= FLG_DIE;
ng_rmnode_self(sc->node);
}
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;
if (sc == NULL) {
return (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 (NG_NODE_HAS_NAME(node))
strncpy(ni->name, NG_NODE_NAME(node), sizeof(ni->name) - 1);
strncpy(ni->type, node->nd_type->name, sizeof(ni->type) - 1);
ni->id = (u_int32_t) ng_node2ID(node);
ni->hooks = NG_NODE_NUMHOOKS(node);
break;
}
default:
lwkt_reltoken(&tp->t_token);
return (ENOIOCTL);
}
lwkt_reltoken(&tp->t_token);
return (0);
}
static int
ngt_input(int c, struct tty *tp)
{
sc_p sc;
node_p node;
struct mbuf *m;
int error = 0;
lwkt_gettoken(&tp->t_token);
sc = (sc_p) tp->t_sc;
if (sc == NULL) {
lwkt_reltoken(&tp->t_token);
return (0);
}
node = sc->node;
if (tp != sc->tp)
panic("ngt_input");
if ((tp->t_state & TS_CONNECTED) == 0) {
if (sc->flags & FLG_DEBUG)
log(LOG_DEBUG, "%s: no carrier\n", NG_NODE_NAME(node));
lwkt_reltoken(&tp->t_token);
return (0);
}
if (c & TTY_ERRORMASK) {
if (sc->flags & FLG_DEBUG)
log(LOG_DEBUG, "%s: line error %x\n",
NG_NODE_NAME(node), c & TTY_ERRORMASK);
lwkt_reltoken(&tp->t_token);
return (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", NG_NODE_NAME(node));
lwkt_reltoken(&tp->t_token);
return (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;
sc->m = NULL;
if (sc->hook == NULL) {
m_freem(m);
lwkt_reltoken(&tp->t_token);
return (0);
}
NG_SEND_DATA_ONLY(error, sc->hook, m);
}
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;
IF_DEQUEUE(&sc->outq, m);
if (m == NULL)
break;
while (m != NULL) {
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 != NULL) {
IF_PREPEND(&sc->outq, m);
break;
}
}
if (tp->t_oproc != NULL)
(*tp->t_oproc) (tp);
if (!IF_QEMPTY(&sc->outq) && !callout_pending(&sc->chand))
ng_callout(&sc->chand, sc->node, NULL, 1, ngt_timeout, NULL, 0);
lwkt_reltoken(&tp->t_token);
return (0);
}
static void
ngt_timeout(node_p node, hook_p hook, void *arg1, int arg2)
{
const sc_p sc = NG_NODE_PRIVATE(node);
struct tty *tp = sc->tp;
lwkt_gettoken(&tp->t_token);
ngt_start(sc->tp);
lwkt_reltoken(&tp->t_token);
}
static int
ngt_constructor(node_p node)
{
return (EOPNOTSUPP);
}
static int
ngt_newhook(node_p node, hook_p hook, const char *name)
{
const sc_p sc = NG_NODE_PRIVATE(node);
struct tty *tp = sc->tp;
if (strcmp(name, NG_TTY_HOOK))
return (EINVAL);
lwkt_gettoken(&tp->t_token);
if (sc->hook) {
lwkt_reltoken(&tp->t_token);
return (EISCONN);
}
sc->hook = hook;
lwkt_reltoken(&tp->t_token);
return (0);
}
static int
ngt_connect(hook_p hook)
{
NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
NG_HOOK_FORCE_QUEUE(hook);
return (0);
}
static int
ngt_disconnect(hook_p hook)
{
const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
struct tty *tp = sc->tp;
lwkt_gettoken(&tp->t_token);
if (hook != sc->hook)
panic(__func__);
sc->hook = NULL;
lwkt_reltoken(&tp->t_token);
return (0);
}
static int
ngt_shutdown(node_p node)
{
const sc_p sc = NG_NODE_PRIVATE(node);
struct tty *tp = sc->tp;
lwkt_gettoken(&tp->t_token);
if (!(sc->flags & FLG_DIE)) {
lwkt_reltoken(&tp->t_token);
return (EOPNOTSUPP);
}
IF_DRAIN(&sc->outq);
m_freem(sc->m);
kfree(sc, M_NETGRAPH);
lwkt_reltoken(&tp->t_token);
return (0);
}
static int
ngt_rcvdata(hook_p hook, item_p item)
{
const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
struct tty *tp = sc->tp;
struct mbuf *m;
int qlen;
if (hook != sc->hook)
panic(__func__);
NGI_GET_M(item, m);
NG_FREE_ITEM(item);
if (IF_QFULL(&sc->outq)) {
IF_DROP(&sc->outq);
NG_FREE_M(m);
return (ENOBUFS);
}
IF_ENQUEUE(&sc->outq, m);
qlen = sc->outq.ifq_len;
if (qlen == 1) {
lwkt_gettoken(&tp->t_token);
ngt_start(sc->tp);
lwkt_reltoken(&tp->t_token);
}
return (0);
}
static int
ngt_rcvmsg(node_p node, item_p item, hook_p lasthook)
{
const sc_p sc = NG_NODE_PRIVATE(node);
struct ng_mesg *msg, *resp = NULL;
int error = 0;
NGI_GET_MSG(item, msg);
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_WAITOK | M_NULLOK);
if (!resp)
ERROUT(ENOMEM);
*((int *) resp->data) = sc->hotchar;
break;
default:
ERROUT(EINVAL);
}
break;
default:
ERROUT(EINVAL);
}
done:
NG_RESPOND_MSG(error, node, item, resp);
NG_FREE_MSG(msg);
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);
}