1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2008 Ed Schouten <[email protected]>
5 * All rights reserved.
6 *
7 * Portions of this software were developed under sponsorship from Snow
8 * B.V., the Netherlands.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/queue.h>
37 #include <sys/sysctl.h>
38 #include <sys/systm.h>
39 #include <sys/tty.h>
40 #include <sys/uio.h>
41
42 #include <vm/uma.h>
43
44 /*
45 * TTY input queue buffering.
46 *
47 * Unlike the output queue, the input queue has more features that are
48 * needed to properly implement various features offered by the TTY
49 * interface:
50 *
51 * - Data can be removed from the tail of the queue, which is used to
52 * implement backspace.
53 * - Once in a while, input has to be `canonicalized'. When ICANON is
54 * turned on, this will be done after a CR has been inserted.
55 * Otherwise, it should be done after any character has been inserted.
56 * - The input queue can store one bit per byte, called the quoting bit.
57 * This bit is used by TTYDISC to make backspace work on quoted
58 * characters.
59 *
60 * In most cases, there is probably less input than output, so unlike
61 * the outq, we'll stick to 128 byte blocks here.
62 */
63
64 static int ttyinq_flush_secure = 1;
65 SYSCTL_INT(_kern, OID_AUTO, tty_inq_flush_secure, CTLFLAG_RW,
66 &ttyinq_flush_secure, 0, "Zero buffers while flushing");
67
68 #define TTYINQ_QUOTESIZE (TTYINQ_DATASIZE / BMSIZE)
69 #define BMSIZE 32
70 #define GETBIT(tib,boff) \
71 ((tib)->tib_quotes[(boff) / BMSIZE] & (1 << ((boff) % BMSIZE)))
72 #define SETBIT(tib,boff) \
73 ((tib)->tib_quotes[(boff) / BMSIZE] |= (1 << ((boff) % BMSIZE)))
74 #define CLRBIT(tib,boff) \
75 ((tib)->tib_quotes[(boff) / BMSIZE] &= ~(1 << ((boff) % BMSIZE)))
76
77 struct ttyinq_block {
78 struct ttyinq_block *tib_prev;
79 struct ttyinq_block *tib_next;
80 uint32_t tib_quotes[TTYINQ_QUOTESIZE];
81 char tib_data[TTYINQ_DATASIZE];
82 };
83
84 static uma_zone_t ttyinq_zone;
85
86 #define TTYINQ_INSERT_TAIL(ti, tib) do { \
87 if (ti->ti_end == 0) { \
88 tib->tib_prev = NULL; \
89 tib->tib_next = ti->ti_firstblock; \
90 ti->ti_firstblock = tib; \
91 } else { \
92 tib->tib_prev = ti->ti_lastblock; \
93 tib->tib_next = ti->ti_lastblock->tib_next; \
94 ti->ti_lastblock->tib_next = tib; \
95 } \
96 if (tib->tib_next != NULL) \
97 tib->tib_next->tib_prev = tib; \
98 ti->ti_nblocks++; \
99 } while (0)
100
101 #define TTYINQ_REMOVE_HEAD(ti) do { \
102 ti->ti_firstblock = ti->ti_firstblock->tib_next; \
103 if (ti->ti_firstblock != NULL) \
104 ti->ti_firstblock->tib_prev = NULL; \
105 ti->ti_nblocks--; \
106 } while (0)
107
108 #define TTYINQ_RECYCLE(ti, tib) do { \
109 if (ti->ti_quota <= ti->ti_nblocks) \
110 uma_zfree(ttyinq_zone, tib); \
111 else \
112 TTYINQ_INSERT_TAIL(ti, tib); \
113 } while (0)
114
115 int
ttyinq_setsize(struct ttyinq * ti,struct tty * tp,size_t size)116 ttyinq_setsize(struct ttyinq *ti, struct tty *tp, size_t size)
117 {
118 struct ttyinq_block *tib;
119
120 ti->ti_quota = howmany(size, TTYINQ_DATASIZE);
121
122 while (ti->ti_quota > ti->ti_nblocks) {
123 /*
124 * List is getting bigger.
125 * Add new blocks to the tail of the list.
126 *
127 * We must unlock the TTY temporarily, because we need
128 * to allocate memory. This won't be a problem, because
129 * in the worst case, another thread ends up here, which
130 * may cause us to allocate too many blocks, but this
131 * will be caught by the loop below.
132 */
133 tty_unlock(tp);
134 tib = uma_zalloc(ttyinq_zone, M_WAITOK);
135 tty_lock(tp);
136
137 if (tty_gone(tp)) {
138 uma_zfree(ttyinq_zone, tib);
139 return (ENXIO);
140 }
141
142 TTYINQ_INSERT_TAIL(ti, tib);
143 }
144 return (0);
145 }
146
147 void
ttyinq_free(struct ttyinq * ti)148 ttyinq_free(struct ttyinq *ti)
149 {
150 struct ttyinq_block *tib;
151
152 ttyinq_flush(ti);
153 ti->ti_quota = 0;
154
155 while ((tib = ti->ti_firstblock) != NULL) {
156 TTYINQ_REMOVE_HEAD(ti);
157 uma_zfree(ttyinq_zone, tib);
158 }
159
160 MPASS(ti->ti_nblocks == 0);
161 }
162
163 int
ttyinq_read_uio(struct ttyinq * ti,struct tty * tp,struct uio * uio,size_t rlen,size_t flen)164 ttyinq_read_uio(struct ttyinq *ti, struct tty *tp, struct uio *uio,
165 size_t rlen, size_t flen)
166 {
167
168 /* rlen includes flen, flen bytes will be trimmed from the end. */
169 MPASS(rlen - flen <= uio->uio_resid);
170
171 while (rlen > 0) {
172 int error;
173 struct ttyinq_block *tib;
174 size_t cbegin, cend, clen;
175
176 /* See if there still is data. */
177 if (ti->ti_begin == ti->ti_linestart)
178 return (0);
179 tib = ti->ti_firstblock;
180 if (tib == NULL)
181 return (0);
182
183 /*
184 * The end address should be the lowest of these three:
185 * - The write pointer
186 * - The blocksize - we can't read beyond the block
187 * - The end address if we could perform the full read
188 */
189 cbegin = ti->ti_begin;
190 cend = MIN(MIN(ti->ti_linestart, ti->ti_begin + rlen),
191 TTYINQ_DATASIZE);
192 clen = cend - cbegin;
193 MPASS(clen >= flen);
194 rlen -= clen;
195
196 /*
197 * Caller shouldn't request that we trim anything if we might be
198 * reading across blocks. We could handle it, but today we do
199 * not.
200 */
201 if (flen > 0)
202 MPASS(rlen == 0);
203
204 /*
205 * We can prevent buffering in some cases:
206 * - We need to read the block until the end.
207 * - We don't need to read the block until the end, but
208 * there is no data beyond it, which allows us to move
209 * the write pointer to a new block.
210 */
211 if (cend == TTYINQ_DATASIZE || cend == ti->ti_end) {
212 /*
213 * Fast path: zero copy. Remove the first block,
214 * so we can unlock the TTY temporarily.
215 */
216 TTYINQ_REMOVE_HEAD(ti);
217 ti->ti_begin = 0;
218
219 /*
220 * Because we remove the first block, we must
221 * fix up the block offsets.
222 */
223 #define CORRECT_BLOCK(t) do { \
224 if (t <= TTYINQ_DATASIZE) \
225 t = 0; \
226 else \
227 t -= TTYINQ_DATASIZE; \
228 } while (0)
229 CORRECT_BLOCK(ti->ti_linestart);
230 CORRECT_BLOCK(ti->ti_reprint);
231 CORRECT_BLOCK(ti->ti_end);
232 #undef CORRECT_BLOCK
233
234 /*
235 * Temporary unlock and copy the data to
236 * userspace. We may need to flush trailing
237 * bytes, like EOF characters.
238 */
239 tty_unlock(tp);
240 error = uiomove(tib->tib_data + cbegin,
241 clen - flen, uio);
242 tty_lock(tp);
243
244 /* Block can now be readded to the list. */
245 TTYINQ_RECYCLE(ti, tib);
246 } else {
247 char ob[TTYINQ_DATASIZE - 1];
248
249 /*
250 * Slow path: store data in a temporary buffer.
251 */
252 memcpy(ob, tib->tib_data + cbegin, clen - flen);
253 ti->ti_begin += clen;
254 MPASS(ti->ti_begin < TTYINQ_DATASIZE);
255
256 /* Temporary unlock and copy the data to userspace. */
257 tty_unlock(tp);
258 error = uiomove(ob, clen - flen, uio);
259 tty_lock(tp);
260 }
261
262 if (error != 0)
263 return (error);
264 if (tty_gone(tp))
265 return (ENXIO);
266 }
267
268 return (0);
269 }
270
271 static __inline void
ttyinq_set_quotes(struct ttyinq_block * tib,size_t offset,size_t length,int value)272 ttyinq_set_quotes(struct ttyinq_block *tib, size_t offset,
273 size_t length, int value)
274 {
275
276 if (value) {
277 /* Set the bits. */
278 for (; length > 0; length--, offset++)
279 SETBIT(tib, offset);
280 } else {
281 /* Unset the bits. */
282 for (; length > 0; length--, offset++)
283 CLRBIT(tib, offset);
284 }
285 }
286
287 size_t
ttyinq_write(struct ttyinq * ti,const void * buf,size_t nbytes,int quote)288 ttyinq_write(struct ttyinq *ti, const void *buf, size_t nbytes, int quote)
289 {
290 const char *cbuf = buf;
291 struct ttyinq_block *tib;
292 unsigned int boff;
293 size_t l;
294
295 while (nbytes > 0) {
296 boff = ti->ti_end % TTYINQ_DATASIZE;
297
298 if (ti->ti_end == 0) {
299 /* First time we're being used or drained. */
300 MPASS(ti->ti_begin == 0);
301 tib = ti->ti_firstblock;
302 if (tib == NULL) {
303 /* Queue has no blocks. */
304 break;
305 }
306 ti->ti_lastblock = tib;
307 } else if (boff == 0) {
308 /* We reached the end of this block on last write. */
309 tib = ti->ti_lastblock->tib_next;
310 if (tib == NULL) {
311 /* We've reached the watermark. */
312 break;
313 }
314 ti->ti_lastblock = tib;
315 } else {
316 tib = ti->ti_lastblock;
317 }
318
319 /* Don't copy more than was requested. */
320 l = MIN(nbytes, TTYINQ_DATASIZE - boff);
321 MPASS(l > 0);
322 memcpy(tib->tib_data + boff, cbuf, l);
323
324 /* Set the quoting bits for the proper region. */
325 ttyinq_set_quotes(tib, boff, l, quote);
326
327 cbuf += l;
328 nbytes -= l;
329 ti->ti_end += l;
330 }
331
332 return (cbuf - (const char *)buf);
333 }
334
335 int
ttyinq_write_nofrag(struct ttyinq * ti,const void * buf,size_t nbytes,int quote)336 ttyinq_write_nofrag(struct ttyinq *ti, const void *buf, size_t nbytes, int quote)
337 {
338 size_t ret __unused;
339
340 if (ttyinq_bytesleft(ti) < nbytes)
341 return (-1);
342
343 /* We should always be able to write it back. */
344 ret = ttyinq_write(ti, buf, nbytes, quote);
345 MPASS(ret == nbytes);
346
347 return (0);
348 }
349
350 void
ttyinq_canonicalize(struct ttyinq * ti)351 ttyinq_canonicalize(struct ttyinq *ti)
352 {
353
354 ti->ti_linestart = ti->ti_reprint = ti->ti_end;
355 ti->ti_startblock = ti->ti_reprintblock = ti->ti_lastblock;
356 }
357
358 /*
359 * Canonicalize at one of the break characters; we'll work backwards from the
360 * lastblock to firstblock to try and find the latest one.
361 */
362 void
ttyinq_canonicalize_break(struct ttyinq * ti,const char * breakc)363 ttyinq_canonicalize_break(struct ttyinq *ti, const char *breakc)
364 {
365 struct ttyinq_block *tib = ti->ti_lastblock;
366 unsigned int canon, off;
367 unsigned int boff;
368
369 /* No block, no change needed. */
370 if (tib == NULL || ti->ti_end == 0)
371 return;
372
373 /* Start just past the end... */
374 off = ti->ti_end;
375 canon = ti->ti_begin;
376
377 while (off > ti->ti_begin) {
378 off--;
379 boff = off % TTYINQ_DATASIZE;
380
381 if (strchr(breakc, tib->tib_data[boff]) && !GETBIT(tib, boff)) {
382 canon = off + 1;
383 break;
384 }
385
386 if (off != ti->ti_begin && boff == 0)
387 tib = tib->tib_prev;
388 }
389
390 MPASS(canon > ti->ti_begin || off == ti->ti_begin);
391
392 /*
393 * We should only be able to hit canon == ti_begin if we walked
394 * everything we have and didn't find any of the break characters, so
395 * if canon == ti_begin then tib is already the correct block and we
396 * should avoid touching it.
397 *
398 * For all other scenarios, if canon lies on a block boundary then tib
399 * has already advanced to the previous block.
400 */
401 if (canon != ti->ti_begin && (canon % TTYINQ_DATASIZE) == 0)
402 tib = tib->tib_next;
403 ti->ti_linestart = ti->ti_reprint = canon;
404 ti->ti_startblock = ti->ti_reprintblock = tib;
405 }
406
407 size_t
ttyinq_findchar(struct ttyinq * ti,const char * breakc,size_t maxlen,char * lastc)408 ttyinq_findchar(struct ttyinq *ti, const char *breakc, size_t maxlen,
409 char *lastc)
410 {
411 struct ttyinq_block *tib = ti->ti_firstblock;
412 unsigned int boff = ti->ti_begin;
413 unsigned int bend = MIN(MIN(TTYINQ_DATASIZE, ti->ti_linestart),
414 ti->ti_begin + maxlen);
415
416 MPASS(maxlen > 0);
417
418 if (tib == NULL)
419 return (0);
420
421 while (boff < bend) {
422 if (strchr(breakc, tib->tib_data[boff]) && !GETBIT(tib, boff)) {
423 *lastc = tib->tib_data[boff];
424 return (boff - ti->ti_begin + 1);
425 }
426 boff++;
427 }
428
429 /* Not found - just process the entire block. */
430 return (bend - ti->ti_begin);
431 }
432
433 void
ttyinq_flush(struct ttyinq * ti)434 ttyinq_flush(struct ttyinq *ti)
435 {
436 struct ttyinq_block *tib;
437
438 ti->ti_begin = 0;
439 ti->ti_linestart = 0;
440 ti->ti_reprint = 0;
441 ti->ti_end = 0;
442
443 /* Zero all data in the input queue to get rid of passwords. */
444 if (ttyinq_flush_secure) {
445 for (tib = ti->ti_firstblock; tib != NULL; tib = tib->tib_next)
446 bzero(&tib->tib_data, sizeof tib->tib_data);
447 }
448 }
449
450 int
ttyinq_peekchar(struct ttyinq * ti,char * c,int * quote)451 ttyinq_peekchar(struct ttyinq *ti, char *c, int *quote)
452 {
453 unsigned int boff;
454 struct ttyinq_block *tib = ti->ti_lastblock;
455
456 if (ti->ti_linestart == ti->ti_end)
457 return (-1);
458
459 MPASS(ti->ti_end > 0);
460 boff = (ti->ti_end - 1) % TTYINQ_DATASIZE;
461
462 *c = tib->tib_data[boff];
463 *quote = GETBIT(tib, boff);
464
465 return (0);
466 }
467
468 void
ttyinq_unputchar(struct ttyinq * ti)469 ttyinq_unputchar(struct ttyinq *ti)
470 {
471
472 MPASS(ti->ti_linestart < ti->ti_end);
473
474 if (--ti->ti_end % TTYINQ_DATASIZE == 0) {
475 /* Roll back to the previous block. */
476 ti->ti_lastblock = ti->ti_lastblock->tib_prev;
477 /*
478 * This can only fail if we are unputchar()'ing the
479 * first character in the queue.
480 */
481 MPASS((ti->ti_lastblock == NULL) == (ti->ti_end == 0));
482 }
483 }
484
485 void
ttyinq_reprintpos_set(struct ttyinq * ti)486 ttyinq_reprintpos_set(struct ttyinq *ti)
487 {
488
489 ti->ti_reprint = ti->ti_end;
490 ti->ti_reprintblock = ti->ti_lastblock;
491 }
492
493 void
ttyinq_reprintpos_reset(struct ttyinq * ti)494 ttyinq_reprintpos_reset(struct ttyinq *ti)
495 {
496
497 ti->ti_reprint = ti->ti_linestart;
498 ti->ti_reprintblock = ti->ti_startblock;
499 }
500
501 static void
ttyinq_line_iterate(struct ttyinq * ti,ttyinq_line_iterator_t * iterator,void * data,unsigned int offset,struct ttyinq_block * tib)502 ttyinq_line_iterate(struct ttyinq *ti,
503 ttyinq_line_iterator_t *iterator, void *data,
504 unsigned int offset, struct ttyinq_block *tib)
505 {
506 unsigned int boff;
507
508 /* Use the proper block when we're at the queue head. */
509 if (offset == 0)
510 tib = ti->ti_firstblock;
511
512 /* Iterate all characters and call the iterator function. */
513 for (; offset < ti->ti_end; offset++) {
514 boff = offset % TTYINQ_DATASIZE;
515 MPASS(tib != NULL);
516
517 /* Call back the iterator function. */
518 iterator(data, tib->tib_data[boff], GETBIT(tib, boff));
519
520 /* Last byte iterated - go to the next block. */
521 if (boff == TTYINQ_DATASIZE - 1)
522 tib = tib->tib_next;
523 }
524 }
525
526 void
ttyinq_line_iterate_from_linestart(struct ttyinq * ti,ttyinq_line_iterator_t * iterator,void * data)527 ttyinq_line_iterate_from_linestart(struct ttyinq *ti,
528 ttyinq_line_iterator_t *iterator, void *data)
529 {
530
531 ttyinq_line_iterate(ti, iterator, data,
532 ti->ti_linestart, ti->ti_startblock);
533 }
534
535 void
ttyinq_line_iterate_from_reprintpos(struct ttyinq * ti,ttyinq_line_iterator_t * iterator,void * data)536 ttyinq_line_iterate_from_reprintpos(struct ttyinq *ti,
537 ttyinq_line_iterator_t *iterator, void *data)
538 {
539
540 ttyinq_line_iterate(ti, iterator, data,
541 ti->ti_reprint, ti->ti_reprintblock);
542 }
543
544 static void
ttyinq_startup(void * dummy)545 ttyinq_startup(void *dummy)
546 {
547
548 ttyinq_zone = uma_zcreate("ttyinq", sizeof(struct ttyinq_block),
549 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
550 }
551
552 SYSINIT(ttyinq, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyinq_startup, NULL);
553