xref: /freebsd/sys/dev/snp/snp.c (revision ee41f1b1cf5e3d4f586cb85b46123b416275862c)
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 "opt_compat.h"
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/filio.h>
22 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
23 #include <sys/ioctl_compat.h>
24 #endif
25 #include <sys/malloc.h>
26 #include <sys/tty.h>
27 #include <sys/conf.h>
28 #include <sys/poll.h>
29 #include <sys/kernel.h>
30 #include <sys/snoop.h>
31 #include <sys/vnode.h>
32 
33 static	d_open_t	snpopen;
34 static	d_close_t	snpclose;
35 static	d_read_t	snpread;
36 static	d_write_t	snpwrite;
37 static	d_ioctl_t	snpioctl;
38 static	d_poll_t	snppoll;
39 
40 #define CDEV_MAJOR 53
41 static struct cdevsw snp_cdevsw = {
42 	/* open */	snpopen,
43 	/* close */	snpclose,
44 	/* read */	snpread,
45 	/* write */	snpwrite,
46 	/* ioctl */	snpioctl,
47 	/* poll */	snppoll,
48 	/* mmap */	nommap,
49 	/* strategy */	nostrategy,
50 	/* name */	"snp",
51 	/* maj */	CDEV_MAJOR,
52 	/* dump */	nodump,
53 	/* psize */	nopsize,
54 	/* flags */	0,
55 	/* bmaj */	-1
56 };
57 
58 
59 #ifndef MIN
60 #define MIN(a,b) (((a)<(b))?(a):(b))
61 #endif
62 
63 static MALLOC_DEFINE(M_SNP, "snp", "Snoop device data");
64 
65 static struct tty	*snpdevtotty __P((dev_t dev));
66 static int		snp_detach __P((struct snoop *snp));
67 
68 static struct tty *
69 snpdevtotty (dev)
70 	dev_t		dev;
71 {
72 	struct cdevsw	*cdp;
73 
74 	cdp = devsw(dev);
75 	if (cdp && cdp->d_flags & D_TTY)
76 		return (dev->si_tty);
77 	return (NULL);
78 }
79 
80 #define SNP_INPUT_BUF	5	/* This is even too much, the maximal
81 				 * interactive mode write is 3 bytes
82 				 * length for function keys...
83 				 */
84 
85 static	int
86 snpwrite(dev, uio, flag)
87 	dev_t           dev;
88 	struct uio     *uio;
89 	int             flag;
90 {
91 	int             len, i, error;
92 	struct snoop   *snp = dev->si_drv1;
93 	struct tty     *tp;
94 	char		c[SNP_INPUT_BUF];
95 
96 	if (snp->snp_tty == NULL)
97 		return (EIO);
98 
99 	tp = snp->snp_tty;
100 
101 	if ((tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
102 	    (tp->t_line == OTTYDISC || tp->t_line == NTTYDISC))
103 		goto tty_input;
104 
105 	printf("Snoop: attempt to write to bad tty.\n");
106 	return (EIO);
107 
108 tty_input:
109 	if (!(tp->t_state & TS_ISOPEN))
110 		return (EIO);
111 
112 	while (uio->uio_resid > 0) {
113 		len = MIN(uio->uio_resid, SNP_INPUT_BUF);
114 		if ((error = uiomove(c, len, uio)) != 0)
115 			return (error);
116 		for (i=0; i < len; i++) {
117 			if (ttyinput(c[i], tp))
118 				return (EIO);
119 		}
120 	}
121 	return 0;
122 
123 }
124 
125 
126 static	int
127 snpread(dev, uio, flag)
128 	dev_t           dev;
129 	struct uio     *uio;
130 	int             flag;
131 {
132 	struct snoop   *snp = dev->si_drv1;
133 	int             len, n, nblen, s, error = 0;
134 	caddr_t         from;
135 	char           *nbuf;
136 
137 	KASSERT(snp->snp_len + snp->snp_base <= snp->snp_blen,
138 	    ("snoop buffer error"));
139 
140 	if (snp->snp_tty == NULL)
141 		return (EIO);
142 
143 	snp->snp_flags &= ~SNOOP_RWAIT;
144 
145 	do {
146 		if (snp->snp_len == 0) {
147 			if (flag & IO_NDELAY)
148 				return (EWOULDBLOCK);
149 			snp->snp_flags |= SNOOP_RWAIT;
150 			tsleep((caddr_t)snp, (PZERO + 1) | PCATCH, "snprd", 0);
151 		}
152 	} while (snp->snp_len == 0);
153 
154 	n = snp->snp_len;
155 
156 	while (snp->snp_len > 0 && uio->uio_resid > 0 && error == 0) {
157 		len = MIN(uio->uio_resid, snp->snp_len);
158 		from = (caddr_t)(snp->snp_buf + snp->snp_base);
159 		if (len == 0)
160 			break;
161 
162 		error = uiomove(from, len, uio);
163 		snp->snp_base += len;
164 		snp->snp_len -= len;
165 	}
166 	if ((snp->snp_flags & SNOOP_OFLOW) && (n < snp->snp_len)) {
167 		snp->snp_flags &= ~SNOOP_OFLOW;
168 	}
169 	s = spltty();
170 	nblen = snp->snp_blen;
171 	if (((nblen / 2) >= SNOOP_MINLEN) && (nblen / 2) >= snp->snp_len) {
172 		while (((nblen / 2) >= snp->snp_len) && ((nblen / 2) >= SNOOP_MINLEN))
173 			nblen = nblen / 2;
174 		if ((nbuf = malloc(nblen, M_SNP, M_NOWAIT)) != NULL) {
175 			bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
176 			free(snp->snp_buf, M_SNP);
177 			snp->snp_buf = nbuf;
178 			snp->snp_blen = nblen;
179 			snp->snp_base = 0;
180 		}
181 	}
182 	splx(s);
183 
184 	return error;
185 }
186 
187 int
188 snpinc(struct snoop *snp, char c)
189 {
190         char    buf;
191 
192 	buf = c;
193         return (snpin(snp, &buf, 1));
194 }
195 
196 
197 int
198 snpin(snp, buf, n)
199 	struct snoop   *snp;
200 	char           *buf;
201 	int             n;
202 {
203 	int             s_free, s_tail;
204 	int             s, len, nblen;
205 	caddr_t         from, to;
206 	char           *nbuf;
207 
208 	KASSERT(n >= 0, ("negative snoop char count"));
209 
210 	if (n == 0)
211 		return 0;
212 
213 	if (snp->snp_flags & SNOOP_DOWN) {
214 		printf("Snoop: more data to down interface.\n");
215 		return 0;
216 	}
217 
218 	if (snp->snp_flags & SNOOP_OFLOW) {
219 		printf("Snoop: buffer overflow.\n");
220 		/*
221 		 * On overflow we just repeat the standart close
222 		 * procedure...yes , this is waste of space but.. Then next
223 		 * read from device will fail if one would recall he is
224 		 * snooping and retry...
225 		 */
226 
227 		return (snpdown(snp));
228 	}
229 	s_tail = snp->snp_blen - (snp->snp_len + snp->snp_base);
230 	s_free = snp->snp_blen - snp->snp_len;
231 
232 
233 	if (n > s_free) {
234 		s = spltty();
235 		nblen = snp->snp_blen;
236 		while ((n > s_free) && ((nblen * 2) <= SNOOP_MAXLEN)) {
237 			nblen = snp->snp_blen * 2;
238 			s_free = nblen - (snp->snp_len + snp->snp_base);
239 		}
240 		if ((n <= s_free) && (nbuf = malloc(nblen, M_SNP, M_NOWAIT))) {
241 			bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
242 			free(snp->snp_buf, M_SNP);
243 			snp->snp_buf = nbuf;
244 			snp->snp_blen = nblen;
245 			snp->snp_base = 0;
246 		} else {
247 			snp->snp_flags |= SNOOP_OFLOW;
248 			if (snp->snp_flags & SNOOP_RWAIT) {
249 				snp->snp_flags &= ~SNOOP_RWAIT;
250 				wakeup((caddr_t)snp);
251 			}
252 			splx(s);
253 			return 0;
254 		}
255 		splx(s);
256 	}
257 	if (n > s_tail) {
258 		from = (caddr_t)(snp->snp_buf + snp->snp_base);
259 		to = (caddr_t)(snp->snp_buf);
260 		len = snp->snp_len;
261 		bcopy(from, to, len);
262 		snp->snp_base = 0;
263 	}
264 	to = (caddr_t)(snp->snp_buf + snp->snp_base + snp->snp_len);
265 	bcopy(buf, to, n);
266 	snp->snp_len += n;
267 
268 	if (snp->snp_flags & SNOOP_RWAIT) {
269 		snp->snp_flags &= ~SNOOP_RWAIT;
270 		wakeup((caddr_t)snp);
271 	}
272 	selwakeup(&snp->snp_sel);
273 	snp->snp_sel.si_pid = 0;
274 
275 	return n;
276 }
277 
278 static	int
279 snpopen(dev, flag, mode, p)
280 	dev_t           dev;
281 	int             flag, mode;
282 	struct proc    *p;
283 {
284 	struct snoop   *snp;
285 	int error;
286 
287 	if ((error = suser(p)) != 0)
288 		return (error);
289 
290 	if (dev->si_drv1 == NULL) {
291 		int mynor = minor(dev);
292 
293 		dev->si_drv1 = snp = malloc(sizeof(*snp), M_SNP, M_WAITOK|M_ZERO);
294 		make_dev(&snp_cdevsw, mynor, 0, 0, 0600, "snp%d", mynor);
295 	} else
296 		return (EBUSY);
297 
298 	/*
299 	 * We intentionally do not OR flags with SNOOP_OPEN, but set them so
300 	 * all previous settings (especially SNOOP_OFLOW) will be cleared.
301 	 */
302 	snp->snp_flags = SNOOP_OPEN;
303 
304 	snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
305 	snp->snp_blen = SNOOP_MINLEN;
306 	snp->snp_base = 0;
307 	snp->snp_len = 0;
308 
309 	/*
310 	 * snp_tty == NULL  is for inactive snoop devices.
311 	 */
312 	snp->snp_tty = NULL;
313 	snp->snp_target = NODEV;
314 	return (0);
315 }
316 
317 
318 static int
319 snp_detach(snp)
320 	struct snoop   *snp;
321 {
322 	struct tty     *tp;
323 
324 	snp->snp_base = 0;
325 	snp->snp_len = 0;
326 
327 	/*
328 	 * If line disc. changed we do not touch this pointer, SLIP/PPP will
329 	 * change it anyway.
330 	 */
331 
332 	if (snp->snp_tty == NULL)
333 		goto detach_notty;
334 
335 	tp = snp->snp_tty;
336 
337 	if (tp && (tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
338 	    (tp->t_line == OTTYDISC || tp->t_line == NTTYDISC)) {
339 		tp->t_sc = NULL;
340 		tp->t_state &= ~TS_SNOOP;
341 	} else
342 		printf("Snoop: bad attached tty data.\n");
343 
344 	snp->snp_tty = NULL;
345 	snp->snp_target = NODEV;
346 
347 detach_notty:
348 	selwakeup(&snp->snp_sel);
349 	snp->snp_sel.si_pid = 0;
350 	if ((snp->snp_flags & SNOOP_OPEN) == 0)
351 		free(snp, M_SNP);
352 
353 	return (0);
354 }
355 
356 static	int
357 snpclose(dev, flags, fmt, p)
358 	dev_t           dev;
359 	int             flags;
360 	int             fmt;
361 	struct proc    *p;
362 {
363 	struct snoop   *snp = dev->si_drv1;
364 
365 	snp->snp_blen = 0;
366 	free(snp->snp_buf, M_SNP);
367 	snp->snp_flags &= ~SNOOP_OPEN;
368 	dev->si_drv1 = NULL;
369 
370 	return (snp_detach(snp));
371 }
372 
373 int
374 snpdown(snp)
375 	struct snoop	*snp;
376 {
377 
378 	if (snp->snp_blen != SNOOP_MINLEN) {
379 		free(snp->snp_buf, M_SNP);
380 		snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
381 		snp->snp_blen = SNOOP_MINLEN;
382 	}
383 	snp->snp_flags |= SNOOP_DOWN;
384 
385 	return (snp_detach(snp));
386 }
387 
388 
389 static	int
390 snpioctl(dev, cmd, data, flags, p)
391 	dev_t           dev;
392 	u_long          cmd;
393 	caddr_t         data;
394 	int             flags;
395 	struct proc    *p;
396 {
397 	dev_t		tdev;
398 	struct snoop   *snp = dev->si_drv1;
399 	struct tty     *tp, *tpo;
400 	int s;
401 
402 	switch (cmd) {
403 	case SNPSTTY:
404 		tdev = udev2dev(*((udev_t *)data), 0);
405 		if (tdev == NODEV)
406 			return (snpdown(snp));
407 
408 		tp = snpdevtotty(tdev);
409 		if (!tp)
410 			return (EINVAL);
411 
412 		if ((tp->t_sc != (caddr_t)snp) && (tp->t_state & TS_SNOOP))
413 			return (EBUSY);
414 
415 		if ((tp->t_line != OTTYDISC) && (tp->t_line != NTTYDISC))
416 			return (EBUSY);
417 
418 		s = spltty();
419 
420 		if (snp->snp_target == NODEV) {
421 			tpo = snp->snp_tty;
422 			if (tpo)
423 				tpo->t_state &= ~TS_SNOOP;
424 		}
425 
426 		tp->t_sc = (caddr_t)snp;
427 		tp->t_state |= TS_SNOOP;
428 		snp->snp_tty = tp;
429 		snp->snp_target = tdev;
430 
431 		/*
432 		 * Clean overflow and down flags -
433 		 * we'll have a chance to get them in the future :)))
434 		 */
435 		snp->snp_flags &= ~SNOOP_OFLOW;
436 		snp->snp_flags &= ~SNOOP_DOWN;
437 		splx(s);
438 		break;
439 
440 	case SNPGTTY:
441 		/*
442 		 * We keep snp_target field specially to make
443 		 * SNPGTTY happy, else we can't know what is device
444 		 * major/minor for tty.
445 		 */
446 		*((dev_t *)data) = snp->snp_target;
447 		break;
448 
449 	case FIONBIO:
450 		break;
451 
452 	case FIOASYNC:
453 		if (*(int *)data)
454 			snp->snp_flags |= SNOOP_ASYNC;
455 		else
456 			snp->snp_flags &= ~SNOOP_ASYNC;
457 		break;
458 
459 	case FIONREAD:
460 		s = spltty();
461 		if (snp->snp_tty != NULL)
462 			*(int *)data = snp->snp_len;
463 		else
464 			if (snp->snp_flags & SNOOP_DOWN) {
465 				if (snp->snp_flags & SNOOP_OFLOW)
466 					*(int *)data = SNP_OFLOW;
467 				else
468 					*(int *)data = SNP_TTYCLOSE;
469 			} else {
470 				*(int *)data = SNP_DETACH;
471 			}
472 		splx(s);
473 		break;
474 
475 	default:
476 		return (ENOTTY);
477 	}
478 	return (0);
479 }
480 
481 
482 static	int
483 snppoll(dev, events, p)
484 	dev_t           dev;
485 	int             events;
486 	struct proc    *p;
487 {
488 	struct snoop   *snp = dev->si_drv1;
489 	int		revents = 0;
490 
491 
492 	/*
493 	 * If snoop is down, we don't want to poll() forever so we return 1.
494 	 * Caller should see if we down via FIONREAD ioctl().  The last should
495 	 * return -1 to indicate down state.
496 	 */
497 	if (events & (POLLIN | POLLRDNORM)) {
498 		if (snp->snp_flags & SNOOP_DOWN || snp->snp_len > 0)
499 			revents |= events & (POLLIN | POLLRDNORM);
500 		else
501 			selrecord(p, &snp->snp_sel);
502 	}
503 	return (revents);
504 }
505 
506 static void snp_drvinit __P((void *unused));
507 
508 static void
509 snp_drvinit(unused)
510 	void *unused;
511 {
512 
513 	cdevsw_add(&snp_cdevsw);
514 }
515 
516 SYSINIT(snpdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,snp_drvinit,NULL)
517