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