#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/ioccom.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/priv.h>
#include <sys/socket.h>
#include <sys/syslog.h>
#include <sys/tty.h>
#include <sys/ttycom.h>
#include <sys/proc.h>
#include <net/if.h>
#include <net/if_var.h>
#include <netgraph/ng_message.h>
#include <netgraph/netgraph.h>
#include <netgraph/ng_tty.h>
struct ngt_softc {
struct tty *tp;
node_p node;
hook_p hook;
struct ifqueue outq;
size_t outqlen;
struct mbuf *m;
short hotchar;
u_int flags;
};
typedef struct ngt_softc *sc_p;
#define FLG_DEBUG 0x0002
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;
#define ERROUT(x) do { error = (x); goto done; } while (0)
static th_getc_inject_t ngt_getc_inject;
static th_getc_poll_t ngt_getc_poll;
static th_rint_t ngt_rint;
static th_rint_bypass_t ngt_rint_bypass;
static th_rint_poll_t ngt_rint_poll;
static struct ttyhook ngt_hook = {
.th_getc_inject = ngt_getc_inject,
.th_getc_poll = ngt_getc_poll,
.th_rint = ngt_rint,
.th_rint_bypass = ngt_rint_bypass,
.th_rint_poll = ngt_rint_poll,
};
static struct ng_type typestruct = {
.version = NG_ABI_VERSION,
.name = NG_TTY_NODE_TYPE,
.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);
#define NGTLOCK(sc) IF_LOCK(&sc->outq)
#define NGTUNLOCK(sc) IF_UNLOCK(&sc->outq)
static int
ngt_constructor(node_p node)
{
sc_p sc;
sc = malloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
NG_NODE_SET_PRIVATE(node, sc);
sc->node = node;
mtx_init(&sc->outq.ifq_mtx, "ng_tty node+queue", NULL, MTX_DEF);
IFQ_SET_MAXLEN(&sc->outq, ifqmaxlen);
return (0);
}
static int
ngt_newhook(node_p node, hook_p hook, const char *name)
{
const sc_p sc = NG_NODE_PRIVATE(node);
if (strcmp(name, NG_TTY_HOOK))
return (EINVAL);
if (sc->hook)
return (EISCONN);
NGTLOCK(sc);
sc->hook = hook;
NGTUNLOCK(sc);
return (0);
}
static int
ngt_connect(hook_p 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));
if (hook != sc->hook)
panic("%s", __func__);
NGTLOCK(sc);
sc->hook = NULL;
NGTUNLOCK(sc);
return (0);
}
static int
ngt_shutdown(node_p node)
{
const sc_p sc = NG_NODE_PRIVATE(node);
struct tty *tp;
tp = sc->tp;
if (tp != NULL) {
tty_lock(tp);
ttyhook_unregister(tp);
}
IF_DRAIN(&sc->outq);
mtx_destroy(&(sc)->outq.ifq_mtx);
NG_NODE_UNREF(sc->node);
free(sc, M_NETGRAPH);
return (0);
}
static int
ngt_rcvmsg(node_p node, item_p item, hook_p lasthook)
{
struct proc *p;
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_TTY:
if (sc->tp != NULL)
return (EBUSY);
p = pfind(((int *)msg->data)[0]);
if (p == NULL || (p->p_flag & P_WEXIT))
return (ESRCH);
_PHOLD(p);
PROC_UNLOCK(p);
error = ttyhook_register(&sc->tp, p, ((int *)msg->data)[1],
&ngt_hook, sc);
PRELE(p);
if (error != 0)
return (error);
break;
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);
}
done:
NG_RESPOND_MSG(error, node, item, resp);
NG_FREE_MSG(msg);
return (error);
}
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;
if (hook != sc->hook)
panic("%s", __func__);
NGI_GET_M(item, m);
NG_FREE_ITEM(item);
if (tp == NULL) {
NG_FREE_M(m);
return (ENXIO);
}
IF_LOCK(&sc->outq);
if (_IF_QFULL(&sc->outq)) {
IF_UNLOCK(&sc->outq);
NG_FREE_M(m);
return (ENOBUFS);
}
_IF_ENQUEUE(&sc->outq, m);
sc->outqlen += m->m_pkthdr.len;
IF_UNLOCK(&sc->outq);
tty_lock(tp);
if (!tty_gone(tp))
ttydevsw_outwakeup(tp);
tty_unlock(tp);
return (0);
}
static size_t
ngt_getc_inject(struct tty *tp, void *buf, size_t len)
{
sc_p sc = ttyhook_softc(tp);
size_t total = 0;
int length;
while (len) {
struct mbuf *m;
IF_DEQUEUE(&sc->outq, m);
if (m == NULL)
break;
while (m != NULL) {
length = min(m->m_len, len);
memcpy((char *)buf + total, mtod(m, char *), length);
m->m_data += length;
m->m_len -= length;
total += length;
len -= length;
if (m->m_len > 0)
break;
m = m_free(m);
}
if (m != NULL) {
IF_PREPEND(&sc->outq, m);
break;
}
}
IF_LOCK(&sc->outq);
sc->outqlen -= total;
IF_UNLOCK(&sc->outq);
MPASS(sc->outqlen >= 0);
return (total);
}
static size_t
ngt_getc_poll(struct tty *tp)
{
sc_p sc = ttyhook_softc(tp);
return (sc->outqlen);
}
static size_t
ngt_rint_bypass(struct tty *tp, const void *buf, size_t len)
{
sc_p sc = ttyhook_softc(tp);
node_p node = sc->node;
struct mbuf *m, *mb;
size_t total = 0;
int error = 0, length;
tty_assert_locked(tp);
if (sc->hook == NULL)
return (0);
m = m_getm2(NULL, len, M_NOWAIT, MT_DATA, M_PKTHDR);
if (m == NULL) {
if (sc->flags & FLG_DEBUG)
log(LOG_ERR,
"%s: can't get mbuf\n", NG_NODE_NAME(node));
return (0);
}
m->m_pkthdr.rcvif = NULL;
for (mb = m; mb != NULL; mb = mb->m_next) {
length = min(M_TRAILINGSPACE(mb), len - total);
memcpy(mtod(mb, char *), (const char *)buf + total, length);
mb->m_len = length;
total += length;
m->m_pkthdr.len += length;
}
if (sc->m != NULL) {
NG_SEND_DATA_ONLY(error, sc->hook, sc->m);
sc->m = NULL;
}
NG_SEND_DATA_ONLY(error, sc->hook, m);
return (total);
}
static int
ngt_rint(struct tty *tp, char c, int flags)
{
sc_p sc = ttyhook_softc(tp);
node_p node = sc->node;
struct mbuf *m;
int error = 0;
tty_assert_locked(tp);
if (sc->hook == NULL)
return (0);
if (flags != 0) {
if (sc->flags & FLG_DEBUG)
log(LOG_DEBUG, "%s: line error %x\n",
NG_NODE_NAME(node), flags);
return (0);
}
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));
return (ENOBUFS);
}
m->m_len = m->m_pkthdr.len = 0;
m->m_pkthdr.rcvif = NULL;
sc->m = m;
}
*(u_char *)mtodo(m, m->m_len++) = c;
m->m_pkthdr.len++;
if (sc->hotchar == -1 || (u_char)c == sc->hotchar ||
m->m_len >= MHLEN) {
sc->m = NULL;
NG_SEND_DATA_ONLY(error, sc->hook, m);
}
return (error);
}
static size_t
ngt_rint_poll(struct tty *tp)
{
return (1);
}