xref: /freebsd/sys/dev/snp/snp.c (revision 1b6c76a2fe091c74f08427e6c870851025a9cf67)
1 /*
2  * Copyright (c) 1995 Ugen J.S.Antsilevich
3  *
4  * Redistribution and use in source forms, with and without modification,
5  * are permitted provided that this entire comment appears intact.
6  *
7  * Redistribution in binary form may occur without any restrictions.
8  * Obviously, it would be nice if you gave credit where credit is due
9  * but requiring it would be too onerous.
10  *
11  * This software is provided ``AS IS'' without any warranties of any kind.
12  *
13  * Snoop stuff.
14  *
15  * $FreeBSD$
16  */
17 
18 #include <sys/param.h>
19 #include <sys/systm.h>
20 #include <sys/filio.h>
21 #include <sys/malloc.h>
22 #include <sys/tty.h>
23 #include <sys/conf.h>
24 #include <sys/poll.h>
25 #include <sys/kernel.h>
26 #include <sys/snoop.h>
27 #include <sys/vnode.h>
28 
29 static	l_close_t	snplclose;
30 static	l_write_t	snplwrite;
31 static	d_open_t	snpopen;
32 static	d_close_t	snpclose;
33 static	d_read_t	snpread;
34 static	d_write_t	snpwrite;
35 static	d_ioctl_t	snpioctl;
36 static	d_poll_t	snppoll;
37 
38 #define CDEV_MAJOR 53
39 static struct cdevsw snp_cdevsw = {
40 	/* open */	snpopen,
41 	/* close */	snpclose,
42 	/* read */	snpread,
43 	/* write */	snpwrite,
44 	/* ioctl */	snpioctl,
45 	/* poll */	snppoll,
46 	/* mmap */	nommap,
47 	/* strategy */	nostrategy,
48 	/* name */	"snp",
49 	/* maj */	CDEV_MAJOR,
50 	/* dump */	nodump,
51 	/* psize */	nopsize,
52 	/* flags */	0,
53 };
54 
55 static struct linesw snpdisc = {
56 	ttyopen,	snplclose,	ttread,		snplwrite,
57 	l_nullioctl,	ttyinput,	ttstart,	ttymodem
58 };
59 
60 static MALLOC_DEFINE(M_SNP, "snp", "Snoop device data");
61 /*
62  * The number of the "snoop" line discipline.  This gets determined at
63  * module load time.
64  */
65 static int snooplinedisc;
66 
67 static struct tty	*snpdevtotty __P((dev_t dev));
68 static void		snp_clone __P((void *arg, char *name,
69 			    int namelen, dev_t *dev));
70 static int		snp_detach __P((struct snoop *snp));
71 static int		snp_down __P((struct snoop *snp));
72 static int		snp_in __P((struct snoop *snp, char *buf, int n));
73 static int		snp_modevent __P((module_t mod, int what, void *arg));
74 
75 static int
76 snplclose(tp, flag)
77 	struct tty *tp;
78 	int flag;
79 {
80 	struct snoop *snp;
81 	int error;
82 
83 	snp = tp->t_sc;
84 	error = snp_down(snp);
85 	if (error != 0)
86 		return (error);
87 	error = ttylclose(tp, flag);
88 	return (error);
89 }
90 
91 static int
92 snplwrite(tp, uio, flag)
93 	struct tty *tp;
94 	struct uio *uio;
95 	int flag;
96 {
97 	struct iovec iov;
98 	struct uio uio2;
99 	struct snoop *snp;
100 	int error, ilen;
101 	char ibuf[512];
102 
103 	snp = tp->t_sc;
104 	while (uio->uio_resid > 0) {
105 		ilen = imin(sizeof(ibuf), uio->uio_resid);
106 		error = uiomove(ibuf, ilen, uio);
107 		if (error != 0)
108 			return (error);
109 		snp_in(snp, ibuf, ilen);
110 		/* Hackish, but probably the least of all evils. */
111 		iov.iov_base = ibuf;
112 		iov.iov_len = ilen;
113 		uio2.uio_iov = &iov;
114 		uio2.uio_iovcnt = 1;
115 		uio2.uio_offset = 0;
116 		uio2.uio_resid = ilen;
117 		uio2.uio_segflg = UIO_SYSSPACE;
118 		uio2.uio_rw = UIO_WRITE;
119 		uio2.uio_procp = uio->uio_procp;
120 		error = ttwrite(tp, &uio2, flag);
121 		if (error != 0)
122 			return (error);
123 	}
124 	return (0);
125 }
126 
127 static struct tty *
128 snpdevtotty(dev)
129 	dev_t dev;
130 {
131 	struct cdevsw *cdp;
132 
133 	cdp = devsw(dev);
134 	if (cdp == NULL || (cdp->d_flags & D_TTY) == 0)
135 		return (NULL);
136 	return (dev->si_tty);
137 }
138 
139 #define SNP_INPUT_BUF	5	/* This is even too much, the maximal
140 				 * interactive mode write is 3 bytes
141 				 * length for function keys...
142 				 */
143 
144 static int
145 snpwrite(dev, uio, flag)
146 	dev_t dev;
147 	struct uio *uio;
148 	int flag;
149 {
150 	struct snoop *snp;
151 	struct tty *tp;
152 	int error, i, len;
153 	char c[SNP_INPUT_BUF];
154 
155 	snp = dev->si_drv1;
156 	tp = snp->snp_tty;
157 	if (tp == NULL)
158 		return (EIO);
159 	if ((tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
160 	    tp->t_line == snooplinedisc)
161 		goto tty_input;
162 
163 	printf("Snoop: attempt to write to bad tty.\n");
164 	return (EIO);
165 
166 tty_input:
167 	if (!(tp->t_state & TS_ISOPEN))
168 		return (EIO);
169 
170 	while (uio->uio_resid > 0) {
171 		len = imin(uio->uio_resid, SNP_INPUT_BUF);
172 		if ((error = uiomove(c, len, uio)) != 0)
173 			return (error);
174 		for (i=0; i < len; i++) {
175 			if (ttyinput(c[i], tp))
176 				return (EIO);
177 		}
178 	}
179 	return (0);
180 }
181 
182 
183 static int
184 snpread(dev, uio, flag)
185 	dev_t dev;
186 	struct uio *uio;
187 	int flag;
188 {
189 	struct snoop *snp;
190 	int error, len, n, nblen, s;
191 	caddr_t from;
192 	char *nbuf;
193 
194 	snp = dev->si_drv1;
195 	KASSERT(snp->snp_len + snp->snp_base <= snp->snp_blen,
196 	    ("snoop buffer error"));
197 
198 	if (snp->snp_tty == NULL)
199 		return (EIO);
200 
201 	snp->snp_flags &= ~SNOOP_RWAIT;
202 
203 	do {
204 		if (snp->snp_len == 0) {
205 			if (flag & IO_NDELAY)
206 				return (EWOULDBLOCK);
207 			snp->snp_flags |= SNOOP_RWAIT;
208 			tsleep((caddr_t)snp, (PZERO + 1) | PCATCH, "snprd", 0);
209 		}
210 	} while (snp->snp_len == 0);
211 
212 	n = snp->snp_len;
213 
214 	error = 0;
215 	while (snp->snp_len > 0 && uio->uio_resid > 0 && error == 0) {
216 		len = min((unsigned)uio->uio_resid, snp->snp_len);
217 		from = (caddr_t)(snp->snp_buf + snp->snp_base);
218 		if (len == 0)
219 			break;
220 
221 		error = uiomove(from, len, uio);
222 		snp->snp_base += len;
223 		snp->snp_len -= len;
224 	}
225 	if ((snp->snp_flags & SNOOP_OFLOW) && (n < snp->snp_len)) {
226 		snp->snp_flags &= ~SNOOP_OFLOW;
227 	}
228 	s = spltty();
229 	nblen = snp->snp_blen;
230 	if (((nblen / 2) >= SNOOP_MINLEN) && (nblen / 2) >= snp->snp_len) {
231 		while (nblen / 2 >= snp->snp_len && nblen / 2 >= SNOOP_MINLEN)
232 			nblen = nblen / 2;
233 		if ((nbuf = malloc(nblen, M_SNP, M_NOWAIT)) != NULL) {
234 			bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
235 			free(snp->snp_buf, M_SNP);
236 			snp->snp_buf = nbuf;
237 			snp->snp_blen = nblen;
238 			snp->snp_base = 0;
239 		}
240 	}
241 	splx(s);
242 
243 	return (error);
244 }
245 
246 static int
247 snp_in(snp, buf, n)
248 	struct snoop *snp;
249 	char *buf;
250 	int n;
251 {
252 	int s_free, s_tail;
253 	int s, len, nblen;
254 	caddr_t from, to;
255 	char *nbuf;
256 
257 	KASSERT(n >= 0, ("negative snoop char count"));
258 
259 	if (n == 0)
260 		return (0);
261 
262 	if (snp->snp_flags & SNOOP_DOWN) {
263 		printf("Snoop: more data to down interface.\n");
264 		return (0);
265 	}
266 
267 	if (snp->snp_flags & SNOOP_OFLOW) {
268 		printf("Snoop: buffer overflow.\n");
269 		/*
270 		 * On overflow we just repeat the standart close
271 		 * procedure...yes , this is waste of space but.. Then next
272 		 * read from device will fail if one would recall he is
273 		 * snooping and retry...
274 		 */
275 
276 		return (snp_down(snp));
277 	}
278 	s_tail = snp->snp_blen - (snp->snp_len + snp->snp_base);
279 	s_free = snp->snp_blen - snp->snp_len;
280 
281 
282 	if (n > s_free) {
283 		s = spltty();
284 		nblen = snp->snp_blen;
285 		while ((n > s_free) && ((nblen * 2) <= SNOOP_MAXLEN)) {
286 			nblen = snp->snp_blen * 2;
287 			s_free = nblen - (snp->snp_len + snp->snp_base);
288 		}
289 		if ((n <= s_free) && (nbuf = malloc(nblen, M_SNP, M_NOWAIT))) {
290 			bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
291 			free(snp->snp_buf, M_SNP);
292 			snp->snp_buf = nbuf;
293 			snp->snp_blen = nblen;
294 			snp->snp_base = 0;
295 		} else {
296 			snp->snp_flags |= SNOOP_OFLOW;
297 			if (snp->snp_flags & SNOOP_RWAIT) {
298 				snp->snp_flags &= ~SNOOP_RWAIT;
299 				wakeup((caddr_t)snp);
300 			}
301 			splx(s);
302 			return (0);
303 		}
304 		splx(s);
305 	}
306 	if (n > s_tail) {
307 		from = (caddr_t)(snp->snp_buf + snp->snp_base);
308 		to = (caddr_t)(snp->snp_buf);
309 		len = snp->snp_len;
310 		bcopy(from, to, len);
311 		snp->snp_base = 0;
312 	}
313 	to = (caddr_t)(snp->snp_buf + snp->snp_base + snp->snp_len);
314 	bcopy(buf, to, n);
315 	snp->snp_len += n;
316 
317 	if (snp->snp_flags & SNOOP_RWAIT) {
318 		snp->snp_flags &= ~SNOOP_RWAIT;
319 		wakeup((caddr_t)snp);
320 	}
321 	selwakeup(&snp->snp_sel);
322 	snp->snp_sel.si_pid = 0;
323 
324 	return (n);
325 }
326 
327 static int
328 snpopen(dev, flag, mode, p)
329 	dev_t dev;
330 	int flag, mode;
331 	struct proc *p;
332 {
333 	struct snoop *snp;
334 	int error;
335 
336 	if ((error = suser(p)) != 0)
337 		return (error);
338 
339 	if (dev->si_drv1 == NULL) {
340 		if (!(dev->si_flags & SI_NAMED))
341 			make_dev(&snp_cdevsw, minor(dev), UID_ROOT, GID_WHEEL,
342 			    0600, "snp%d", dev2unit(dev));
343 		dev->si_drv1 = snp = malloc(sizeof(*snp), M_SNP,
344 		    M_WAITOK | M_ZERO);
345 	} else
346 		return (EBUSY);
347 
348 	/*
349 	 * We intentionally do not OR flags with SNOOP_OPEN, but set them so
350 	 * all previous settings (especially SNOOP_OFLOW) will be cleared.
351 	 */
352 	snp->snp_flags = SNOOP_OPEN;
353 
354 	snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
355 	snp->snp_blen = SNOOP_MINLEN;
356 	snp->snp_base = 0;
357 	snp->snp_len = 0;
358 
359 	/*
360 	 * snp_tty == NULL  is for inactive snoop devices.
361 	 */
362 	snp->snp_tty = NULL;
363 	snp->snp_target = NODEV;
364 	return (0);
365 }
366 
367 
368 static int
369 snp_detach(snp)
370 	struct snoop *snp;
371 {
372 	struct tty *tp;
373 
374 	snp->snp_base = 0;
375 	snp->snp_len = 0;
376 
377 	/*
378 	 * If line disc. changed we do not touch this pointer, SLIP/PPP will
379 	 * change it anyway.
380 	 */
381 	tp = snp->snp_tty;
382 	if (tp == NULL)
383 		goto detach_notty;
384 
385 	if (tp && (tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
386 	    tp->t_line == snooplinedisc) {
387 		tp->t_sc = NULL;
388 		tp->t_state &= ~TS_SNOOP;
389 		tp->t_line = snp->snp_olddisc;
390 	} else
391 		printf("Snoop: bad attached tty data.\n");
392 
393 	snp->snp_tty = NULL;
394 	snp->snp_target = NODEV;
395 
396 detach_notty:
397 	selwakeup(&snp->snp_sel);
398 	snp->snp_sel.si_pid = 0;
399 	if ((snp->snp_flags & SNOOP_OPEN) == 0)
400 		free(snp, M_SNP);
401 
402 	return (0);
403 }
404 
405 static int
406 snpclose(dev, flags, fmt, p)
407 	dev_t dev;
408 	int flags;
409 	int fmt;
410 	struct proc *p;
411 {
412 	struct snoop *snp;
413 
414 	snp = dev->si_drv1;
415 	snp->snp_blen = 0;
416 	free(snp->snp_buf, M_SNP);
417 	snp->snp_flags &= ~SNOOP_OPEN;
418 	dev->si_drv1 = NULL;
419 	destroy_dev(dev);
420 
421 	return (snp_detach(snp));
422 }
423 
424 static int
425 snp_down(snp)
426 	struct snoop *snp;
427 {
428 
429 	if (snp->snp_blen != SNOOP_MINLEN) {
430 		free(snp->snp_buf, M_SNP);
431 		snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
432 		snp->snp_blen = SNOOP_MINLEN;
433 	}
434 	snp->snp_flags |= SNOOP_DOWN;
435 
436 	return (snp_detach(snp));
437 }
438 
439 static int
440 snpioctl(dev, cmd, data, flags, p)
441 	dev_t dev;
442 	u_long cmd;
443 	caddr_t data;
444 	int flags;
445 	struct proc *p;
446 {
447 	struct snoop *snp;
448 	struct tty *tp, *tpo;
449 	dev_t tdev;
450 	int s;
451 
452 	snp = dev->si_drv1;
453 	switch (cmd) {
454 	case SNPSTTY:
455 		tdev = udev2dev(*((udev_t *)data), 0);
456 		if (tdev == NODEV)
457 			return (snp_down(snp));
458 
459 		tp = snpdevtotty(tdev);
460 		if (!tp)
461 			return (EINVAL);
462 
463 		s = spltty();
464 
465 		if (snp->snp_target == NODEV) {
466 			tpo = snp->snp_tty;
467 			if (tpo)
468 				tpo->t_state &= ~TS_SNOOP;
469 		}
470 
471 		tp->t_sc = (caddr_t)snp;
472 		tp->t_state |= TS_SNOOP;
473 		snp->snp_olddisc = tp->t_line;
474 		tp->t_line = snooplinedisc;
475 		snp->snp_tty = tp;
476 		snp->snp_target = tdev;
477 
478 		/*
479 		 * Clean overflow and down flags -
480 		 * we'll have a chance to get them in the future :)))
481 		 */
482 		snp->snp_flags &= ~SNOOP_OFLOW;
483 		snp->snp_flags &= ~SNOOP_DOWN;
484 		splx(s);
485 		break;
486 
487 	case SNPGTTY:
488 		/*
489 		 * We keep snp_target field specially to make
490 		 * SNPGTTY happy, else we can't know what is device
491 		 * major/minor for tty.
492 		 */
493 		*((dev_t *)data) = snp->snp_target;
494 		break;
495 
496 	case FIONBIO:
497 		break;
498 
499 	case FIOASYNC:
500 		if (*(int *)data)
501 			snp->snp_flags |= SNOOP_ASYNC;
502 		else
503 			snp->snp_flags &= ~SNOOP_ASYNC;
504 		break;
505 
506 	case FIONREAD:
507 		s = spltty();
508 		if (snp->snp_tty != NULL)
509 			*(int *)data = snp->snp_len;
510 		else
511 			if (snp->snp_flags & SNOOP_DOWN) {
512 				if (snp->snp_flags & SNOOP_OFLOW)
513 					*(int *)data = SNP_OFLOW;
514 				else
515 					*(int *)data = SNP_TTYCLOSE;
516 			} else {
517 				*(int *)data = SNP_DETACH;
518 			}
519 		splx(s);
520 		break;
521 
522 	default:
523 		return (ENOTTY);
524 	}
525 	return (0);
526 }
527 
528 static int
529 snppoll(dev, events, p)
530 	dev_t dev;
531 	int events;
532 	struct proc *p;
533 {
534 	struct snoop *snp;
535 	int revents;
536 
537 	snp = dev->si_drv1;
538 	revents = 0;
539 	/*
540 	 * If snoop is down, we don't want to poll() forever so we return 1.
541 	 * Caller should see if we down via FIONREAD ioctl().  The last should
542 	 * return -1 to indicate down state.
543 	 */
544 	if (events & (POLLIN | POLLRDNORM)) {
545 		if (snp->snp_flags & SNOOP_DOWN || snp->snp_len > 0)
546 			revents |= events & (POLLIN | POLLRDNORM);
547 		else
548 			selrecord(p, &snp->snp_sel);
549 	}
550 	return (revents);
551 }
552 
553 static void
554 snp_clone(arg, name, namelen, dev)
555 	void *arg;
556 	char *name;
557 	int namelen;
558 	dev_t *dev;
559 {
560 	int u;
561 
562 	if (*dev != NODEV)
563 		return;
564 	if (dev_stdclone(name, NULL, "snp", &u) != 1)
565 		return;
566 	*dev = make_dev(&snp_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
567 	    "snp%d", u);
568 }
569 
570 static int
571 snp_modevent(mod, type, data)
572 	module_t mod;
573 	int type;
574 	void *data;
575 {
576 	static eventhandler_tag eh_tag;
577 
578 	switch (type) {
579 	case MOD_LOAD:
580 		/* XXX error checking. */
581 		eh_tag = EVENTHANDLER_REGISTER(dev_clone, snp_clone, 0, 1000);
582 		snooplinedisc = ldisc_register(LDISC_LOAD, &snpdisc);
583 		cdevsw_add(&snp_cdevsw);
584 		break;
585 	case MOD_UNLOAD:
586 		/* XXX don't unload if busy. */
587 		EVENTHANDLER_DEREGISTER(dev_clone, eh_tag);
588 		ldisc_deregister(snooplinedisc);
589 		cdevsw_remove(&snp_cdevsw);
590 		break;
591 	default:
592 		break;
593 	}
594 	return (0);
595 }
596 
597 static moduledata_t snp_mod = {
598         "snp",
599         snp_modevent,
600         NULL
601 };
602 DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR);
603