1995-02-14 21:21:26 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1995 Ugen J.S.Antsilevich
|
|
|
|
*
|
|
|
|
* Redistribution and use in source forms, with and without modification,
|
|
|
|
* are permitted provided that this entire comment appears intact.
|
|
|
|
*
|
|
|
|
* Redistribution in binary form may occur without any restrictions.
|
|
|
|
* Obviously, it would be nice if you gave credit where credit is due
|
|
|
|
* but requiring it would be too onerous.
|
|
|
|
*
|
|
|
|
* This software is provided ``AS IS'' without any warranties of any kind.
|
|
|
|
*
|
|
|
|
* Snoop stuff.
|
1999-06-17 23:42:45 +00:00
|
|
|
*
|
1995-02-14 21:21:26 +00:00
|
|
|
*/
|
|
|
|
|
2003-08-24 18:03:45 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1995-02-14 21:21:26 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
1997-03-24 11:37:53 +00:00
|
|
|
#include <sys/filio.h>
|
1997-10-12 20:26:33 +00:00
|
|
|
#include <sys/malloc.h>
|
1995-02-14 21:21:26 +00:00
|
|
|
#include <sys/tty.h>
|
|
|
|
#include <sys/conf.h>
|
1997-09-14 02:42:03 +00:00
|
|
|
#include <sys/poll.h>
|
1995-02-14 21:21:26 +00:00
|
|
|
#include <sys/kernel.h>
|
2001-07-18 13:39:43 +00:00
|
|
|
#include <sys/queue.h>
|
1995-12-08 11:19:42 +00:00
|
|
|
#include <sys/snoop.h>
|
1997-11-18 16:12:51 +00:00
|
|
|
#include <sys/vnode.h>
|
1995-12-08 11:19:42 +00:00
|
|
|
|
2001-06-05 05:07:53 +00:00
|
|
|
static l_close_t snplclose;
|
2001-06-05 05:00:17 +00:00
|
|
|
static l_write_t snplwrite;
|
1995-12-08 11:19:42 +00:00
|
|
|
static d_open_t snpopen;
|
|
|
|
static d_close_t snpclose;
|
|
|
|
static d_read_t snpread;
|
|
|
|
static d_write_t snpwrite;
|
|
|
|
static d_ioctl_t snpioctl;
|
1997-09-14 02:42:03 +00:00
|
|
|
static d_poll_t snppoll;
|
1995-12-08 11:19:42 +00:00
|
|
|
|
1999-05-30 16:53:49 +00:00
|
|
|
static struct cdevsw snp_cdevsw = {
|
2003-03-03 12:15:54 +00:00
|
|
|
.d_open = snpopen,
|
|
|
|
.d_close = snpclose,
|
|
|
|
.d_read = snpread,
|
|
|
|
.d_write = snpwrite,
|
|
|
|
.d_ioctl = snpioctl,
|
|
|
|
.d_poll = snppoll,
|
|
|
|
.d_name = "snp",
|
2004-02-21 20:29:52 +00:00
|
|
|
.d_flags = D_PSEUDO,
|
1999-05-30 16:53:49 +00:00
|
|
|
};
|
1995-12-08 11:19:42 +00:00
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
static struct linesw snpdisc = {
|
2001-06-05 05:07:53 +00:00
|
|
|
ttyopen, snplclose, ttread, snplwrite,
|
2001-06-05 05:00:17 +00:00
|
|
|
l_nullioctl, ttyinput, ttstart, ttymodem
|
|
|
|
};
|
1995-11-29 10:49:16 +00:00
|
|
|
|
2001-07-25 14:47:56 +00:00
|
|
|
/*
|
|
|
|
* This is the main snoop per-device structure.
|
|
|
|
*/
|
|
|
|
struct snoop {
|
|
|
|
LIST_ENTRY(snoop) snp_list; /* List glue. */
|
2002-03-11 03:48:32 +00:00
|
|
|
int snp_unit; /* Device number. */
|
2001-07-25 14:47:56 +00:00
|
|
|
dev_t snp_target; /* Target tty device. */
|
|
|
|
struct tty *snp_tty; /* Target tty pointer. */
|
|
|
|
u_long snp_len; /* Possible length. */
|
|
|
|
u_long snp_base; /* Data base. */
|
|
|
|
u_long snp_blen; /* Used length. */
|
|
|
|
caddr_t snp_buf; /* Allocation pointer. */
|
|
|
|
int snp_flags; /* Flags. */
|
|
|
|
struct selinfo snp_sel; /* Select info. */
|
|
|
|
int snp_olddisc; /* Old line discipline. */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Possible flags.
|
|
|
|
*/
|
|
|
|
#define SNOOP_ASYNC 0x0002
|
|
|
|
#define SNOOP_OPEN 0x0004
|
|
|
|
#define SNOOP_RWAIT 0x0008
|
|
|
|
#define SNOOP_OFLOW 0x0010
|
|
|
|
#define SNOOP_DOWN 0x0020
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Other constants.
|
|
|
|
*/
|
|
|
|
#define SNOOP_MINLEN (4*1024) /* This should be power of 2.
|
|
|
|
* 4K tested to be the minimum
|
|
|
|
* for which on normal tty
|
|
|
|
* usage there is no need to
|
|
|
|
* allocate more.
|
|
|
|
*/
|
|
|
|
#define SNOOP_MAXLEN (64*1024) /* This one also,64K enough
|
|
|
|
* If we grow more,something
|
|
|
|
* really bad in this world..
|
|
|
|
*/
|
|
|
|
|
2000-12-08 20:09:00 +00:00
|
|
|
static MALLOC_DEFINE(M_SNP, "snp", "Snoop device data");
|
2001-05-22 22:13:14 +00:00
|
|
|
/*
|
|
|
|
* The number of the "snoop" line discipline. This gets determined at
|
|
|
|
* module load time.
|
|
|
|
*/
|
2001-06-05 05:00:17 +00:00
|
|
|
static int snooplinedisc;
|
2001-07-18 13:39:43 +00:00
|
|
|
|
|
|
|
static LIST_HEAD(, snoop) snp_sclist = LIST_HEAD_INITIALIZER(&snp_sclist);
|
2004-02-21 20:29:52 +00:00
|
|
|
static struct clonedevs *snpclones;
|
2001-07-18 13:39:43 +00:00
|
|
|
|
2002-03-20 02:08:01 +00:00
|
|
|
static struct tty *snpdevtotty(dev_t dev);
|
|
|
|
static void snp_clone(void *arg, char *name,
|
|
|
|
int namelen, dev_t *dev);
|
|
|
|
static int snp_detach(struct snoop *snp);
|
|
|
|
static int snp_down(struct snoop *snp);
|
|
|
|
static int snp_in(struct snoop *snp, char *buf, int n);
|
|
|
|
static int snp_modevent(module_t mod, int what, void *arg);
|
2001-05-22 22:13:14 +00:00
|
|
|
|
2001-06-05 05:07:53 +00:00
|
|
|
static int
|
|
|
|
snplclose(tp, flag)
|
|
|
|
struct tty *tp;
|
|
|
|
int flag;
|
|
|
|
{
|
|
|
|
struct snoop *snp;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
snp = tp->t_sc;
|
|
|
|
error = snp_down(snp);
|
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
|
|
|
error = ttylclose(tp, flag);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2001-05-22 22:13:14 +00:00
|
|
|
static int
|
2001-06-05 05:00:17 +00:00
|
|
|
snplwrite(tp, uio, flag)
|
|
|
|
struct tty *tp;
|
|
|
|
struct uio *uio;
|
|
|
|
int flag;
|
2001-05-22 22:13:14 +00:00
|
|
|
{
|
|
|
|
struct iovec iov;
|
|
|
|
struct uio uio2;
|
2001-06-05 05:00:17 +00:00
|
|
|
struct snoop *snp;
|
|
|
|
int error, ilen;
|
2001-08-16 06:00:57 +00:00
|
|
|
char *ibuf;
|
2001-05-22 22:13:14 +00:00
|
|
|
|
2001-08-16 06:00:57 +00:00
|
|
|
error = 0;
|
|
|
|
ibuf = NULL;
|
2001-06-05 05:00:17 +00:00
|
|
|
snp = tp->t_sc;
|
2001-06-03 05:17:39 +00:00
|
|
|
while (uio->uio_resid > 0) {
|
2001-08-16 06:00:57 +00:00
|
|
|
ilen = imin(512, uio->uio_resid);
|
2003-02-19 05:47:46 +00:00
|
|
|
ibuf = malloc(ilen, M_SNP, M_WAITOK);
|
2001-05-22 22:13:14 +00:00
|
|
|
error = uiomove(ibuf, ilen, uio);
|
2001-06-05 05:00:17 +00:00
|
|
|
if (error != 0)
|
2001-08-16 06:00:57 +00:00
|
|
|
break;
|
2001-06-05 05:00:17 +00:00
|
|
|
snp_in(snp, ibuf, ilen);
|
|
|
|
/* Hackish, but probably the least of all evils. */
|
2001-05-22 22:13:14 +00:00
|
|
|
iov.iov_base = ibuf;
|
|
|
|
iov.iov_len = ilen;
|
|
|
|
uio2.uio_iov = &iov;
|
|
|
|
uio2.uio_iovcnt = 1;
|
|
|
|
uio2.uio_offset = 0;
|
|
|
|
uio2.uio_resid = ilen;
|
|
|
|
uio2.uio_segflg = UIO_SYSSPACE;
|
|
|
|
uio2.uio_rw = UIO_WRITE;
|
2001-09-12 08:38:13 +00:00
|
|
|
uio2.uio_td = uio->uio_td;
|
2001-05-22 22:13:14 +00:00
|
|
|
error = ttwrite(tp, &uio2, flag);
|
2001-06-05 05:00:17 +00:00
|
|
|
if (error != 0)
|
2001-08-16 06:00:57 +00:00
|
|
|
break;
|
|
|
|
free(ibuf, M_SNP);
|
|
|
|
ibuf = NULL;
|
2001-05-22 22:13:14 +00:00
|
|
|
}
|
2001-08-16 06:00:57 +00:00
|
|
|
if (ibuf != NULL)
|
|
|
|
free(ibuf, M_SNP);
|
|
|
|
return (error);
|
2001-05-22 22:13:14 +00:00
|
|
|
}
|
|
|
|
|
1995-02-25 20:09:44 +00:00
|
|
|
static struct tty *
|
2001-06-05 05:00:17 +00:00
|
|
|
snpdevtotty(dev)
|
|
|
|
dev_t dev;
|
1995-02-25 20:09:44 +00:00
|
|
|
{
|
2001-06-05 05:00:17 +00:00
|
|
|
struct cdevsw *cdp;
|
1997-03-16 19:11:40 +00:00
|
|
|
|
1999-05-08 06:40:31 +00:00
|
|
|
cdp = devsw(dev);
|
2001-06-03 05:17:39 +00:00
|
|
|
if (cdp == NULL || (cdp->d_flags & D_TTY) == 0)
|
|
|
|
return (NULL);
|
|
|
|
return (dev->si_tty);
|
1995-02-25 20:09:44 +00:00
|
|
|
}
|
|
|
|
|
2000-04-02 00:35:37 +00:00
|
|
|
#define SNP_INPUT_BUF 5 /* This is even too much, the maximal
|
1995-02-27 19:47:53 +00:00
|
|
|
* interactive mode write is 3 bytes
|
|
|
|
* length for function keys...
|
|
|
|
*/
|
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
static int
|
1995-02-27 19:47:53 +00:00
|
|
|
snpwrite(dev, uio, flag)
|
2001-06-05 05:00:17 +00:00
|
|
|
dev_t dev;
|
|
|
|
struct uio *uio;
|
|
|
|
int flag;
|
1995-02-27 19:47:53 +00:00
|
|
|
{
|
2001-06-05 05:00:17 +00:00
|
|
|
struct snoop *snp;
|
|
|
|
struct tty *tp;
|
|
|
|
int error, i, len;
|
2002-04-10 03:51:49 +00:00
|
|
|
unsigned char c[SNP_INPUT_BUF];
|
1995-02-27 19:47:53 +00:00
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
snp = dev->si_drv1;
|
1995-02-27 19:47:53 +00:00
|
|
|
tp = snp->snp_tty;
|
2001-06-05 05:00:17 +00:00
|
|
|
if (tp == NULL)
|
|
|
|
return (EIO);
|
1995-02-27 19:47:53 +00:00
|
|
|
if ((tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
|
2001-06-05 05:00:17 +00:00
|
|
|
tp->t_line == snooplinedisc)
|
1995-02-27 19:47:53 +00:00
|
|
|
goto tty_input;
|
|
|
|
|
2002-03-11 03:48:32 +00:00
|
|
|
printf("snp%d: attempt to write to bad tty\n", snp->snp_unit);
|
1995-02-27 19:47:53 +00:00
|
|
|
return (EIO);
|
|
|
|
|
|
|
|
tty_input:
|
1995-05-30 08:16:23 +00:00
|
|
|
if (!(tp->t_state & TS_ISOPEN))
|
1995-02-27 19:47:53 +00:00
|
|
|
return (EIO);
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1995-02-27 19:47:53 +00:00
|
|
|
while (uio->uio_resid > 0) {
|
2001-06-03 05:17:39 +00:00
|
|
|
len = imin(uio->uio_resid, SNP_INPUT_BUF);
|
1995-02-27 19:47:53 +00:00
|
|
|
if ((error = uiomove(c, len, uio)) != 0)
|
|
|
|
return (error);
|
2000-04-02 00:35:37 +00:00
|
|
|
for (i=0; i < len; i++) {
|
|
|
|
if (ttyinput(c[i], tp))
|
1995-02-27 19:47:53 +00:00
|
|
|
return (EIO);
|
|
|
|
}
|
|
|
|
}
|
2001-06-05 05:00:17 +00:00
|
|
|
return (0);
|
1995-02-27 19:47:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
static int
|
1995-02-14 21:21:26 +00:00
|
|
|
snpread(dev, uio, flag)
|
2001-06-05 05:00:17 +00:00
|
|
|
dev_t dev;
|
|
|
|
struct uio *uio;
|
|
|
|
int flag;
|
1995-02-14 21:21:26 +00:00
|
|
|
{
|
2001-06-05 05:00:17 +00:00
|
|
|
struct snoop *snp;
|
|
|
|
int error, len, n, nblen, s;
|
|
|
|
caddr_t from;
|
|
|
|
char *nbuf;
|
1995-02-15 16:35:38 +00:00
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
snp = dev->si_drv1;
|
1999-01-08 17:31:30 +00:00
|
|
|
KASSERT(snp->snp_len + snp->snp_base <= snp->snp_blen,
|
1999-01-10 01:58:29 +00:00
|
|
|
("snoop buffer error"));
|
1995-02-25 20:09:44 +00:00
|
|
|
|
1995-02-27 19:47:53 +00:00
|
|
|
if (snp->snp_tty == NULL)
|
1995-02-14 21:21:26 +00:00
|
|
|
return (EIO);
|
|
|
|
|
1995-02-15 16:35:38 +00:00
|
|
|
snp->snp_flags &= ~SNOOP_RWAIT;
|
1995-02-14 21:21:26 +00:00
|
|
|
|
|
|
|
do {
|
1995-02-15 16:35:38 +00:00
|
|
|
if (snp->snp_len == 0) {
|
1997-11-18 16:12:51 +00:00
|
|
|
if (flag & IO_NDELAY)
|
|
|
|
return (EWOULDBLOCK);
|
1995-02-15 16:35:38 +00:00
|
|
|
snp->snp_flags |= SNOOP_RWAIT;
|
2003-03-02 16:54:40 +00:00
|
|
|
error = tsleep(snp, (PZERO + 1) | PCATCH,
|
2001-11-24 15:59:46 +00:00
|
|
|
"snprd", 0);
|
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
1995-02-14 21:21:26 +00:00
|
|
|
}
|
|
|
|
} while (snp->snp_len == 0);
|
|
|
|
|
1995-02-15 16:35:38 +00:00
|
|
|
n = snp->snp_len;
|
1995-02-14 21:21:26 +00:00
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
error = 0;
|
1995-02-15 16:35:38 +00:00
|
|
|
while (snp->snp_len > 0 && uio->uio_resid > 0 && error == 0) {
|
2001-06-03 05:17:39 +00:00
|
|
|
len = min((unsigned)uio->uio_resid, snp->snp_len);
|
2000-04-02 00:35:37 +00:00
|
|
|
from = (caddr_t)(snp->snp_buf + snp->snp_base);
|
1995-02-14 21:21:26 +00:00
|
|
|
if (len == 0)
|
|
|
|
break;
|
|
|
|
|
1995-02-15 16:35:38 +00:00
|
|
|
error = uiomove(from, len, uio);
|
|
|
|
snp->snp_base += len;
|
|
|
|
snp->snp_len -= len;
|
1995-02-14 21:21:26 +00:00
|
|
|
}
|
1995-02-15 16:35:38 +00:00
|
|
|
if ((snp->snp_flags & SNOOP_OFLOW) && (n < snp->snp_len)) {
|
1995-02-14 21:21:26 +00:00
|
|
|
snp->snp_flags &= ~SNOOP_OFLOW;
|
1995-02-15 16:35:38 +00:00
|
|
|
}
|
|
|
|
s = spltty();
|
|
|
|
nblen = snp->snp_blen;
|
|
|
|
if (((nblen / 2) >= SNOOP_MINLEN) && (nblen / 2) >= snp->snp_len) {
|
2001-06-05 05:00:17 +00:00
|
|
|
while (nblen / 2 >= snp->snp_len && nblen / 2 >= SNOOP_MINLEN)
|
1995-02-15 16:35:38 +00:00
|
|
|
nblen = nblen / 2;
|
2000-04-02 00:35:37 +00:00
|
|
|
if ((nbuf = malloc(nblen, M_SNP, M_NOWAIT)) != NULL) {
|
1995-02-15 16:35:38 +00:00
|
|
|
bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
|
2000-04-02 00:35:37 +00:00
|
|
|
free(snp->snp_buf, M_SNP);
|
1995-02-15 16:35:38 +00:00
|
|
|
snp->snp_buf = nbuf;
|
|
|
|
snp->snp_blen = nblen;
|
|
|
|
snp->snp_base = 0;
|
1995-02-14 21:21:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
return (error);
|
1995-02-14 21:21:26 +00:00
|
|
|
}
|
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
static int
|
|
|
|
snp_in(snp, buf, n)
|
|
|
|
struct snoop *snp;
|
|
|
|
char *buf;
|
|
|
|
int n;
|
1995-02-14 21:21:26 +00:00
|
|
|
{
|
2001-06-05 05:00:17 +00:00
|
|
|
int s_free, s_tail;
|
|
|
|
int s, len, nblen;
|
|
|
|
caddr_t from, to;
|
|
|
|
char *nbuf;
|
1995-02-14 21:21:26 +00:00
|
|
|
|
1999-01-10 01:58:29 +00:00
|
|
|
KASSERT(n >= 0, ("negative snoop char count"));
|
1995-02-14 21:21:26 +00:00
|
|
|
|
1995-02-15 16:35:38 +00:00
|
|
|
if (n == 0)
|
2001-06-05 05:00:17 +00:00
|
|
|
return (0);
|
1995-02-14 21:21:26 +00:00
|
|
|
|
1995-02-15 16:35:38 +00:00
|
|
|
if (snp->snp_flags & SNOOP_DOWN) {
|
2002-03-11 03:48:32 +00:00
|
|
|
printf("snp%d: more data to down interface\n", snp->snp_unit);
|
2001-06-05 05:00:17 +00:00
|
|
|
return (0);
|
1995-02-15 16:35:38 +00:00
|
|
|
}
|
1995-02-15 18:41:57 +00:00
|
|
|
|
1995-02-15 16:35:38 +00:00
|
|
|
if (snp->snp_flags & SNOOP_OFLOW) {
|
2002-03-11 03:48:32 +00:00
|
|
|
printf("snp%d: buffer overflow\n", snp->snp_unit);
|
1995-02-15 16:35:38 +00:00
|
|
|
/*
|
|
|
|
* On overflow we just repeat the standart close
|
|
|
|
* procedure...yes , this is waste of space but.. Then next
|
|
|
|
* read from device will fail if one would recall he is
|
|
|
|
* snooping and retry...
|
|
|
|
*/
|
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
return (snp_down(snp));
|
1995-02-15 16:35:38 +00:00
|
|
|
}
|
|
|
|
s_tail = snp->snp_blen - (snp->snp_len + snp->snp_base);
|
|
|
|
s_free = snp->snp_blen - snp->snp_len;
|
1995-02-14 21:21:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (n > s_free) {
|
1995-02-15 16:35:38 +00:00
|
|
|
s = spltty();
|
|
|
|
nblen = snp->snp_blen;
|
|
|
|
while ((n > s_free) && ((nblen * 2) <= SNOOP_MAXLEN)) {
|
|
|
|
nblen = snp->snp_blen * 2;
|
|
|
|
s_free = nblen - (snp->snp_len + snp->snp_base);
|
1995-02-14 21:21:26 +00:00
|
|
|
}
|
2000-04-02 00:35:37 +00:00
|
|
|
if ((n <= s_free) && (nbuf = malloc(nblen, M_SNP, M_NOWAIT))) {
|
1995-02-15 16:35:38 +00:00
|
|
|
bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
|
2000-04-02 00:35:37 +00:00
|
|
|
free(snp->snp_buf, M_SNP);
|
1995-02-15 16:35:38 +00:00
|
|
|
snp->snp_buf = nbuf;
|
|
|
|
snp->snp_blen = nblen;
|
|
|
|
snp->snp_base = 0;
|
1995-02-14 21:21:26 +00:00
|
|
|
} else {
|
1995-02-15 16:35:38 +00:00
|
|
|
snp->snp_flags |= SNOOP_OFLOW;
|
1995-02-14 21:21:26 +00:00
|
|
|
if (snp->snp_flags & SNOOP_RWAIT) {
|
1995-02-15 16:35:38 +00:00
|
|
|
snp->snp_flags &= ~SNOOP_RWAIT;
|
2003-03-02 16:54:40 +00:00
|
|
|
wakeup(snp);
|
1995-02-15 16:35:38 +00:00
|
|
|
}
|
1995-02-14 21:21:26 +00:00
|
|
|
splx(s);
|
2001-06-05 05:00:17 +00:00
|
|
|
return (0);
|
1995-02-14 21:21:26 +00:00
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
if (n > s_tail) {
|
2000-04-02 00:35:37 +00:00
|
|
|
from = (caddr_t)(snp->snp_buf + snp->snp_base);
|
|
|
|
to = (caddr_t)(snp->snp_buf);
|
1995-02-15 16:35:38 +00:00
|
|
|
len = snp->snp_len;
|
|
|
|
bcopy(from, to, len);
|
|
|
|
snp->snp_base = 0;
|
1995-02-14 21:21:26 +00:00
|
|
|
}
|
2000-04-02 00:35:37 +00:00
|
|
|
to = (caddr_t)(snp->snp_buf + snp->snp_base + snp->snp_len);
|
1995-02-15 16:35:38 +00:00
|
|
|
bcopy(buf, to, n);
|
|
|
|
snp->snp_len += n;
|
1995-02-14 21:21:26 +00:00
|
|
|
|
|
|
|
if (snp->snp_flags & SNOOP_RWAIT) {
|
1995-02-15 16:35:38 +00:00
|
|
|
snp->snp_flags &= ~SNOOP_RWAIT;
|
2003-03-02 16:54:40 +00:00
|
|
|
wakeup(snp);
|
1995-02-15 16:35:38 +00:00
|
|
|
}
|
2003-11-09 09:17:26 +00:00
|
|
|
selwakeuppri(&snp->snp_sel, PZERO + 1);
|
1995-02-14 21:21:26 +00:00
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
return (n);
|
1995-02-14 21:21:26 +00:00
|
|
|
}
|
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
snpopen(dev, flag, mode, td)
|
2001-06-05 05:00:17 +00:00
|
|
|
dev_t dev;
|
|
|
|
int flag, mode;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *td;
|
1995-02-14 21:21:26 +00:00
|
|
|
{
|
2001-06-05 05:00:17 +00:00
|
|
|
struct snoop *snp;
|
1995-02-14 21:21:26 +00:00
|
|
|
|
2000-04-02 00:35:37 +00:00
|
|
|
if (dev->si_drv1 == NULL) {
|
2004-02-21 20:29:52 +00:00
|
|
|
dev->si_flags &= ~SI_CHEAPCLONE;
|
2001-06-05 05:00:17 +00:00
|
|
|
dev->si_drv1 = snp = malloc(sizeof(*snp), M_SNP,
|
2003-02-19 05:47:46 +00:00
|
|
|
M_WAITOK | M_ZERO);
|
2002-03-11 03:48:32 +00:00
|
|
|
snp->snp_unit = dev2unit(dev);
|
2000-04-02 00:35:37 +00:00
|
|
|
} else
|
|
|
|
return (EBUSY);
|
1995-02-25 20:09:44 +00:00
|
|
|
|
1995-02-15 16:35:38 +00:00
|
|
|
/*
|
2000-04-02 00:35:37 +00:00
|
|
|
* We intentionally do not OR flags with SNOOP_OPEN, but set them so
|
1995-02-15 16:35:38 +00:00
|
|
|
* all previous settings (especially SNOOP_OFLOW) will be cleared.
|
|
|
|
*/
|
|
|
|
snp->snp_flags = SNOOP_OPEN;
|
1995-02-14 21:21:26 +00:00
|
|
|
|
2003-02-19 05:47:46 +00:00
|
|
|
snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
|
1995-02-15 16:35:38 +00:00
|
|
|
snp->snp_blen = SNOOP_MINLEN;
|
|
|
|
snp->snp_base = 0;
|
|
|
|
snp->snp_len = 0;
|
1995-02-14 21:21:26 +00:00
|
|
|
|
|
|
|
/*
|
1995-02-27 19:47:53 +00:00
|
|
|
* snp_tty == NULL is for inactive snoop devices.
|
1995-02-14 21:21:26 +00:00
|
|
|
*/
|
1995-02-27 19:47:53 +00:00
|
|
|
snp->snp_tty = NULL;
|
1999-05-10 18:10:08 +00:00
|
|
|
snp->snp_target = NODEV;
|
2001-07-18 13:39:43 +00:00
|
|
|
|
|
|
|
LIST_INSERT_HEAD(&snp_sclist, snp, snp_list);
|
1995-02-14 21:21:26 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1995-02-15 16:35:38 +00:00
|
|
|
|
1995-12-14 08:32:45 +00:00
|
|
|
static int
|
1995-02-15 16:35:38 +00:00
|
|
|
snp_detach(snp)
|
2001-06-05 05:00:17 +00:00
|
|
|
struct snoop *snp;
|
1995-02-14 21:21:26 +00:00
|
|
|
{
|
2001-06-05 05:00:17 +00:00
|
|
|
struct tty *tp;
|
1995-02-14 21:21:26 +00:00
|
|
|
|
|
|
|
snp->snp_base = 0;
|
|
|
|
snp->snp_len = 0;
|
|
|
|
|
|
|
|
/*
|
2000-04-02 00:35:37 +00:00
|
|
|
* If line disc. changed we do not touch this pointer, SLIP/PPP will
|
1995-02-15 16:35:38 +00:00
|
|
|
* change it anyway.
|
1995-02-14 21:21:26 +00:00
|
|
|
*/
|
1995-02-27 19:47:53 +00:00
|
|
|
tp = snp->snp_tty;
|
2001-06-05 05:00:17 +00:00
|
|
|
if (tp == NULL)
|
|
|
|
goto detach_notty;
|
1995-02-27 19:47:53 +00:00
|
|
|
|
1995-02-25 20:09:44 +00:00
|
|
|
if (tp && (tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
|
2001-06-05 05:00:17 +00:00
|
|
|
tp->t_line == snooplinedisc) {
|
1995-02-14 21:21:26 +00:00
|
|
|
tp->t_sc = NULL;
|
1995-02-15 16:35:38 +00:00
|
|
|
tp->t_state &= ~TS_SNOOP;
|
2001-05-22 22:13:14 +00:00
|
|
|
tp->t_line = snp->snp_olddisc;
|
1995-02-15 16:35:38 +00:00
|
|
|
} else
|
2002-03-11 03:48:32 +00:00
|
|
|
printf("snp%d: bad attached tty data\n", snp->snp_unit);
|
1995-02-14 21:21:26 +00:00
|
|
|
|
1995-02-27 19:47:53 +00:00
|
|
|
snp->snp_tty = NULL;
|
1999-05-10 18:10:08 +00:00
|
|
|
snp->snp_target = NODEV;
|
1995-02-15 16:35:38 +00:00
|
|
|
|
1995-02-15 18:41:57 +00:00
|
|
|
detach_notty:
|
2003-11-09 09:17:26 +00:00
|
|
|
selwakeuppri(&snp->snp_sel, PZERO + 1);
|
2000-04-02 00:35:37 +00:00
|
|
|
if ((snp->snp_flags & SNOOP_OPEN) == 0)
|
|
|
|
free(snp, M_SNP);
|
1995-02-14 21:21:26 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
snpclose(dev, flags, fmt, td)
|
2001-06-05 05:00:17 +00:00
|
|
|
dev_t dev;
|
|
|
|
int flags;
|
|
|
|
int fmt;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *td;
|
1995-02-15 16:35:38 +00:00
|
|
|
{
|
2001-06-05 05:00:17 +00:00
|
|
|
struct snoop *snp;
|
1995-02-15 16:35:38 +00:00
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
snp = dev->si_drv1;
|
1995-02-15 16:35:38 +00:00
|
|
|
snp->snp_blen = 0;
|
2001-07-18 13:39:43 +00:00
|
|
|
LIST_REMOVE(snp, snp_list);
|
2000-04-02 00:35:37 +00:00
|
|
|
free(snp->snp_buf, M_SNP);
|
1995-02-15 16:35:38 +00:00
|
|
|
snp->snp_flags &= ~SNOOP_OPEN;
|
2000-04-02 00:35:37 +00:00
|
|
|
dev->si_drv1 = NULL;
|
2004-02-21 20:29:52 +00:00
|
|
|
destroy_dev(dev);
|
1995-02-15 16:35:38 +00:00
|
|
|
|
|
|
|
return (snp_detach(snp));
|
|
|
|
}
|
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
static int
|
|
|
|
snp_down(snp)
|
|
|
|
struct snoop *snp;
|
1995-02-15 18:41:57 +00:00
|
|
|
{
|
2000-04-02 00:35:37 +00:00
|
|
|
|
|
|
|
if (snp->snp_blen != SNOOP_MINLEN) {
|
|
|
|
free(snp->snp_buf, M_SNP);
|
2003-02-19 05:47:46 +00:00
|
|
|
snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
|
2000-04-02 00:35:37 +00:00
|
|
|
snp->snp_blen = SNOOP_MINLEN;
|
|
|
|
}
|
1995-02-15 18:41:57 +00:00
|
|
|
snp->snp_flags |= SNOOP_DOWN;
|
1995-02-14 21:21:26 +00:00
|
|
|
|
1995-02-15 18:41:57 +00:00
|
|
|
return (snp_detach(snp));
|
|
|
|
}
|
1995-02-14 21:21:26 +00:00
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
snpioctl(dev, cmd, data, flags, td)
|
2001-06-05 05:00:17 +00:00
|
|
|
dev_t dev;
|
|
|
|
u_long cmd;
|
|
|
|
caddr_t data;
|
|
|
|
int flags;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *td;
|
1995-02-14 21:21:26 +00:00
|
|
|
{
|
2001-06-05 05:00:17 +00:00
|
|
|
struct snoop *snp;
|
|
|
|
struct tty *tp, *tpo;
|
|
|
|
dev_t tdev;
|
2000-04-02 00:35:37 +00:00
|
|
|
int s;
|
1995-02-14 21:21:26 +00:00
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
snp = dev->si_drv1;
|
1995-02-14 21:21:26 +00:00
|
|
|
switch (cmd) {
|
|
|
|
case SNPSTTY:
|
2000-04-02 00:35:37 +00:00
|
|
|
tdev = udev2dev(*((udev_t *)data), 0);
|
1999-05-10 18:10:08 +00:00
|
|
|
if (tdev == NODEV)
|
2001-06-05 05:00:17 +00:00
|
|
|
return (snp_down(snp));
|
1995-02-15 18:41:57 +00:00
|
|
|
|
1997-03-16 19:11:40 +00:00
|
|
|
tp = snpdevtotty(tdev);
|
1995-02-25 20:09:44 +00:00
|
|
|
if (!tp)
|
1995-02-14 21:21:26 +00:00
|
|
|
return (EINVAL);
|
2001-11-24 15:34:18 +00:00
|
|
|
if (tp->t_state & TS_SNOOP)
|
|
|
|
return (EBUSY);
|
1995-02-14 21:21:26 +00:00
|
|
|
|
1995-02-15 16:35:38 +00:00
|
|
|
s = spltty();
|
1995-02-25 20:09:44 +00:00
|
|
|
|
1999-05-10 18:10:08 +00:00
|
|
|
if (snp->snp_target == NODEV) {
|
1995-02-27 19:47:53 +00:00
|
|
|
tpo = snp->snp_tty;
|
1995-02-25 20:09:44 +00:00
|
|
|
if (tpo)
|
|
|
|
tpo->t_state &= ~TS_SNOOP;
|
1995-02-14 21:21:26 +00:00
|
|
|
}
|
1995-02-25 20:09:44 +00:00
|
|
|
|
2000-04-02 00:35:37 +00:00
|
|
|
tp->t_sc = (caddr_t)snp;
|
1995-02-14 21:21:26 +00:00
|
|
|
tp->t_state |= TS_SNOOP;
|
2001-05-22 22:13:14 +00:00
|
|
|
snp->snp_olddisc = tp->t_line;
|
2001-06-05 05:00:17 +00:00
|
|
|
tp->t_line = snooplinedisc;
|
1995-02-27 19:47:53 +00:00
|
|
|
snp->snp_tty = tp;
|
1995-02-25 20:09:44 +00:00
|
|
|
snp->snp_target = tdev;
|
|
|
|
|
1995-02-15 18:41:57 +00:00
|
|
|
/*
|
|
|
|
* Clean overflow and down flags -
|
|
|
|
* we'll have a chance to get them in the future :)))
|
|
|
|
*/
|
|
|
|
snp->snp_flags &= ~SNOOP_OFLOW;
|
1995-02-15 16:35:38 +00:00
|
|
|
snp->snp_flags &= ~SNOOP_DOWN;
|
1995-02-14 21:21:26 +00:00
|
|
|
splx(s);
|
|
|
|
break;
|
1995-02-25 20:09:44 +00:00
|
|
|
|
1995-02-14 21:21:26 +00:00
|
|
|
case SNPGTTY:
|
1995-02-27 19:47:53 +00:00
|
|
|
/*
|
|
|
|
* We keep snp_target field specially to make
|
2000-04-02 00:35:37 +00:00
|
|
|
* SNPGTTY happy, else we can't know what is device
|
1995-02-27 19:47:53 +00:00
|
|
|
* major/minor for tty.
|
|
|
|
*/
|
2002-11-11 10:45:31 +00:00
|
|
|
*((udev_t *)data) = dev2udev(snp->snp_target);
|
1995-02-14 21:21:26 +00:00
|
|
|
break;
|
1995-02-15 16:35:38 +00:00
|
|
|
|
1995-02-14 21:21:26 +00:00
|
|
|
case FIONBIO:
|
|
|
|
break;
|
1995-02-25 20:09:44 +00:00
|
|
|
|
1995-02-14 21:21:26 +00:00
|
|
|
case FIOASYNC:
|
2000-04-02 00:35:37 +00:00
|
|
|
if (*(int *)data)
|
1995-02-14 21:21:26 +00:00
|
|
|
snp->snp_flags |= SNOOP_ASYNC;
|
|
|
|
else
|
|
|
|
snp->snp_flags &= ~SNOOP_ASYNC;
|
|
|
|
break;
|
1995-02-25 20:09:44 +00:00
|
|
|
|
1995-02-14 21:21:26 +00:00
|
|
|
case FIONREAD:
|
|
|
|
s = spltty();
|
1995-02-27 19:47:53 +00:00
|
|
|
if (snp->snp_tty != NULL)
|
2000-04-02 00:35:37 +00:00
|
|
|
*(int *)data = snp->snp_len;
|
1995-05-30 08:16:23 +00:00
|
|
|
else
|
1995-02-25 20:09:44 +00:00
|
|
|
if (snp->snp_flags & SNOOP_DOWN) {
|
|
|
|
if (snp->snp_flags & SNOOP_OFLOW)
|
2000-04-02 00:35:37 +00:00
|
|
|
*(int *)data = SNP_OFLOW;
|
1995-02-15 18:41:57 +00:00
|
|
|
else
|
2000-04-02 00:35:37 +00:00
|
|
|
*(int *)data = SNP_TTYCLOSE;
|
1995-02-15 18:41:57 +00:00
|
|
|
} else {
|
2000-04-02 00:35:37 +00:00
|
|
|
*(int *)data = SNP_DETACH;
|
1995-02-15 18:41:57 +00:00
|
|
|
}
|
1995-02-14 21:21:26 +00:00
|
|
|
splx(s);
|
|
|
|
break;
|
1995-02-25 20:09:44 +00:00
|
|
|
|
1995-02-14 21:21:26 +00:00
|
|
|
default:
|
|
|
|
return (ENOTTY);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
snppoll(dev, events, td)
|
2001-06-05 05:00:17 +00:00
|
|
|
dev_t dev;
|
|
|
|
int events;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *td;
|
1995-02-14 21:21:26 +00:00
|
|
|
{
|
2001-06-05 05:00:17 +00:00
|
|
|
struct snoop *snp;
|
|
|
|
int revents;
|
1995-02-25 20:09:44 +00:00
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
snp = dev->si_drv1;
|
|
|
|
revents = 0;
|
1995-02-15 16:35:38 +00:00
|
|
|
/*
|
2000-04-02 00:35:37 +00:00
|
|
|
* If snoop is down, we don't want to poll() forever so we return 1.
|
|
|
|
* Caller should see if we down via FIONREAD ioctl(). The last should
|
1995-02-15 16:35:38 +00:00
|
|
|
* return -1 to indicate down state.
|
|
|
|
*/
|
1999-05-06 18:13:11 +00:00
|
|
|
if (events & (POLLIN | POLLRDNORM)) {
|
1997-09-14 02:42:03 +00:00
|
|
|
if (snp->snp_flags & SNOOP_DOWN || snp->snp_len > 0)
|
|
|
|
revents |= events & (POLLIN | POLLRDNORM);
|
|
|
|
else
|
2001-09-12 08:38:13 +00:00
|
|
|
selrecord(td, &snp->snp_sel);
|
1999-05-06 18:13:11 +00:00
|
|
|
}
|
1997-09-14 02:42:03 +00:00
|
|
|
return (revents);
|
1995-02-14 21:21:26 +00:00
|
|
|
}
|
|
|
|
|
2001-04-17 20:53:11 +00:00
|
|
|
static void
|
2001-06-05 05:00:17 +00:00
|
|
|
snp_clone(arg, name, namelen, dev)
|
|
|
|
void *arg;
|
|
|
|
char *name;
|
|
|
|
int namelen;
|
|
|
|
dev_t *dev;
|
2001-04-17 20:53:11 +00:00
|
|
|
{
|
2004-02-21 20:29:52 +00:00
|
|
|
int u, i;
|
2001-04-17 20:53:11 +00:00
|
|
|
|
|
|
|
if (*dev != NODEV)
|
|
|
|
return;
|
|
|
|
if (dev_stdclone(name, NULL, "snp", &u) != 1)
|
|
|
|
return;
|
2004-02-21 20:29:52 +00:00
|
|
|
i = clone_create(&snpclones, &snp_cdevsw, &u, dev, 0);
|
|
|
|
if (i)
|
|
|
|
*dev = make_dev(&snp_cdevsw, unit2minor(u),
|
|
|
|
UID_ROOT, GID_WHEEL, 0600, "snp%d", u);
|
|
|
|
if (*dev != NULL)
|
2001-11-24 15:10:53 +00:00
|
|
|
(*dev)->si_flags |= SI_CHEAPCLONE;
|
2001-04-17 20:53:11 +00:00
|
|
|
}
|
|
|
|
|
2001-05-22 22:13:14 +00:00
|
|
|
static int
|
2001-06-05 05:00:17 +00:00
|
|
|
snp_modevent(mod, type, data)
|
|
|
|
module_t mod;
|
|
|
|
int type;
|
|
|
|
void *data;
|
1995-11-29 10:49:16 +00:00
|
|
|
{
|
2001-06-05 05:00:17 +00:00
|
|
|
static eventhandler_tag eh_tag;
|
1995-11-29 10:49:16 +00:00
|
|
|
|
2001-05-22 22:13:14 +00:00
|
|
|
switch (type) {
|
|
|
|
case MOD_LOAD:
|
2001-06-05 05:00:17 +00:00
|
|
|
/* XXX error checking. */
|
2001-05-22 22:13:14 +00:00
|
|
|
eh_tag = EVENTHANDLER_REGISTER(dev_clone, snp_clone, 0, 1000);
|
2001-06-05 05:00:17 +00:00
|
|
|
snooplinedisc = ldisc_register(LDISC_LOAD, &snpdisc);
|
2001-05-22 22:13:14 +00:00
|
|
|
break;
|
|
|
|
case MOD_UNLOAD:
|
2001-07-18 13:39:43 +00:00
|
|
|
if (!LIST_EMPTY(&snp_sclist))
|
|
|
|
return (EBUSY);
|
2001-05-22 22:13:14 +00:00
|
|
|
EVENTHANDLER_DEREGISTER(dev_clone, eh_tag);
|
2004-02-21 20:29:52 +00:00
|
|
|
clone_cleanup(&snpclones);
|
2001-06-05 05:00:17 +00:00
|
|
|
ldisc_deregister(snooplinedisc);
|
2001-05-22 22:13:14 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2001-06-05 05:00:17 +00:00
|
|
|
return (0);
|
1995-11-29 10:49:16 +00:00
|
|
|
}
|
|
|
|
|
2001-05-22 22:13:14 +00:00
|
|
|
static moduledata_t snp_mod = {
|
|
|
|
"snp",
|
|
|
|
snp_modevent,
|
|
|
|
NULL
|
|
|
|
};
|
2004-02-21 20:29:52 +00:00
|
|
|
DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
|