• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
4  *
5  * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
6  * which can be dynamically activated and de-activated by the line
7  * discipline handling modules (like SLIP).
8  */
9 
10 #include <linux/types.h>
11 #include <linux/termios.h>
12 #include <linux/errno.h>
13 #include <linux/sched/signal.h>
14 #include <linux/kernel.h>
15 #include <linux/major.h>
16 #include <linux/tty.h>
17 #include <linux/fcntl.h>
18 #include <linux/string.h>
19 #include <linux/mm.h>
20 #include <linux/module.h>
21 #include <linux/bitops.h>
22 #include <linux/mutex.h>
23 #include <linux/compat.h>
24 #include "tty.h"
25 
26 #include <asm/io.h>
27 #include <linux/uaccess.h>
28 
29 #undef TTY_DEBUG_WAIT_UNTIL_SENT
30 
31 #ifdef TTY_DEBUG_WAIT_UNTIL_SENT
32 # define tty_debug_wait_until_sent(tty, f, args...)    tty_debug(tty, f, ##args)
33 #else
34 # define tty_debug_wait_until_sent(tty, f, args...)    do {} while (0)
35 #endif
36 
37 #undef	DEBUG
38 
39 /*
40  * Internal flag options for termios setting behavior
41  */
42 #define TERMIOS_FLUSH	1
43 #define TERMIOS_WAIT	2
44 #define TERMIOS_TERMIO	4
45 #define TERMIOS_OLD	8
46 
47 
48 /**
49  *	tty_chars_in_buffer	-	characters pending
50  *	@tty: terminal
51  *
52  *	Return the number of bytes of data in the device private
53  *	output queue. If no private method is supplied there is assumed
54  *	to be no queue on the device.
55  */
56 
tty_chars_in_buffer(struct tty_struct * tty)57 int tty_chars_in_buffer(struct tty_struct *tty)
58 {
59 	if (tty->ops->chars_in_buffer)
60 		return tty->ops->chars_in_buffer(tty);
61 	else
62 		return 0;
63 }
64 EXPORT_SYMBOL(tty_chars_in_buffer);
65 
66 /**
67  *	tty_write_room		-	write queue space
68  *	@tty: terminal
69  *
70  *	Return the number of bytes that can be queued to this device
71  *	at the present time. The result should be treated as a guarantee
72  *	and the driver cannot offer a value it later shrinks by more than
73  *	the number of bytes written. If no method is provided 2K is always
74  *	returned and data may be lost as there will be no flow control.
75  */
76 
tty_write_room(struct tty_struct * tty)77 int tty_write_room(struct tty_struct *tty)
78 {
79 	if (tty->ops->write_room)
80 		return tty->ops->write_room(tty);
81 	return 2048;
82 }
83 EXPORT_SYMBOL(tty_write_room);
84 
85 /**
86  *	tty_driver_flush_buffer	-	discard internal buffer
87  *	@tty: terminal
88  *
89  *	Discard the internal output buffer for this device. If no method
90  *	is provided then either the buffer cannot be hardware flushed or
91  *	there is no buffer driver side.
92  */
tty_driver_flush_buffer(struct tty_struct * tty)93 void tty_driver_flush_buffer(struct tty_struct *tty)
94 {
95 	if (tty->ops->flush_buffer)
96 		tty->ops->flush_buffer(tty);
97 }
98 EXPORT_SYMBOL(tty_driver_flush_buffer);
99 
100 /**
101  *	tty_throttle		-	flow control
102  *	@tty: terminal
103  *
104  *	Indicate that a tty should stop transmitting data down the stack.
105  *	Takes the termios rwsem to protect against parallel throttle/unthrottle
106  *	and also to ensure the driver can consistently reference its own
107  *	termios data at this point when implementing software flow control.
108  */
109 
tty_throttle(struct tty_struct * tty)110 void tty_throttle(struct tty_struct *tty)
111 {
112 	down_write(&tty->termios_rwsem);
113 	/* check TTY_THROTTLED first so it indicates our state */
114 	if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
115 	    tty->ops->throttle)
116 		tty->ops->throttle(tty);
117 	tty->flow_change = 0;
118 	up_write(&tty->termios_rwsem);
119 }
120 EXPORT_SYMBOL(tty_throttle);
121 
122 /**
123  *	tty_unthrottle		-	flow control
124  *	@tty: terminal
125  *
126  *	Indicate that a tty may continue transmitting data down the stack.
127  *	Takes the termios rwsem to protect against parallel throttle/unthrottle
128  *	and also to ensure the driver can consistently reference its own
129  *	termios data at this point when implementing software flow control.
130  *
131  *	Drivers should however remember that the stack can issue a throttle,
132  *	then change flow control method, then unthrottle.
133  */
134 
tty_unthrottle(struct tty_struct * tty)135 void tty_unthrottle(struct tty_struct *tty)
136 {
137 	down_write(&tty->termios_rwsem);
138 	if (test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
139 	    tty->ops->unthrottle)
140 		tty->ops->unthrottle(tty);
141 	tty->flow_change = 0;
142 	up_write(&tty->termios_rwsem);
143 }
144 EXPORT_SYMBOL(tty_unthrottle);
145 
146 /**
147  *	tty_throttle_safe	-	flow control
148  *	@tty: terminal
149  *
150  *	Similar to tty_throttle() but will only attempt throttle
151  *	if tty->flow_change is TTY_THROTTLE_SAFE. Prevents an accidental
152  *	throttle due to race conditions when throttling is conditional
153  *	on factors evaluated prior to throttling.
154  *
155  *	Returns 0 if tty is throttled (or was already throttled)
156  */
157 
tty_throttle_safe(struct tty_struct * tty)158 int tty_throttle_safe(struct tty_struct *tty)
159 {
160 	int ret = 0;
161 
162 	mutex_lock(&tty->throttle_mutex);
163 	if (!tty_throttled(tty)) {
164 		if (tty->flow_change != TTY_THROTTLE_SAFE)
165 			ret = 1;
166 		else {
167 			set_bit(TTY_THROTTLED, &tty->flags);
168 			if (tty->ops->throttle)
169 				tty->ops->throttle(tty);
170 		}
171 	}
172 	mutex_unlock(&tty->throttle_mutex);
173 
174 	return ret;
175 }
176 
177 /**
178  *	tty_unthrottle_safe	-	flow control
179  *	@tty: terminal
180  *
181  *	Similar to tty_unthrottle() but will only attempt unthrottle
182  *	if tty->flow_change is TTY_UNTHROTTLE_SAFE. Prevents an accidental
183  *	unthrottle due to race conditions when unthrottling is conditional
184  *	on factors evaluated prior to unthrottling.
185  *
186  *	Returns 0 if tty is unthrottled (or was already unthrottled)
187  */
188 
tty_unthrottle_safe(struct tty_struct * tty)189 int tty_unthrottle_safe(struct tty_struct *tty)
190 {
191 	int ret = 0;
192 
193 	mutex_lock(&tty->throttle_mutex);
194 	if (tty_throttled(tty)) {
195 		if (tty->flow_change != TTY_UNTHROTTLE_SAFE)
196 			ret = 1;
197 		else {
198 			clear_bit(TTY_THROTTLED, &tty->flags);
199 			if (tty->ops->unthrottle)
200 				tty->ops->unthrottle(tty);
201 		}
202 	}
203 	mutex_unlock(&tty->throttle_mutex);
204 
205 	return ret;
206 }
207 
208 /**
209  *	tty_wait_until_sent	-	wait for I/O to finish
210  *	@tty: tty we are waiting for
211  *	@timeout: how long we will wait
212  *
213  *	Wait for characters pending in a tty driver to hit the wire, or
214  *	for a timeout to occur (eg due to flow control)
215  *
216  *	Locking: none
217  */
218 
tty_wait_until_sent(struct tty_struct * tty,long timeout)219 void tty_wait_until_sent(struct tty_struct *tty, long timeout)
220 {
221 	tty_debug_wait_until_sent(tty, "wait until sent, timeout=%ld\n", timeout);
222 
223 	if (!timeout)
224 		timeout = MAX_SCHEDULE_TIMEOUT;
225 
226 	timeout = wait_event_interruptible_timeout(tty->write_wait,
227 			!tty_chars_in_buffer(tty), timeout);
228 	if (timeout <= 0)
229 		return;
230 
231 	if (timeout == MAX_SCHEDULE_TIMEOUT)
232 		timeout = 0;
233 
234 	if (tty->ops->wait_until_sent)
235 		tty->ops->wait_until_sent(tty, timeout);
236 }
237 EXPORT_SYMBOL(tty_wait_until_sent);
238 
239 
240 /*
241  *		Termios Helper Methods
242  */
243 
unset_locked_termios(struct tty_struct * tty,struct ktermios * old)244 static void unset_locked_termios(struct tty_struct *tty, struct ktermios *old)
245 {
246 	struct ktermios *termios = &tty->termios;
247 	struct ktermios *locked  = &tty->termios_locked;
248 	int	i;
249 
250 #define NOSET_MASK(x, y, z) (x = ((x) & ~(z)) | ((y) & (z)))
251 
252 	NOSET_MASK(termios->c_iflag, old->c_iflag, locked->c_iflag);
253 	NOSET_MASK(termios->c_oflag, old->c_oflag, locked->c_oflag);
254 	NOSET_MASK(termios->c_cflag, old->c_cflag, locked->c_cflag);
255 	NOSET_MASK(termios->c_lflag, old->c_lflag, locked->c_lflag);
256 	termios->c_line = locked->c_line ? old->c_line : termios->c_line;
257 	for (i = 0; i < NCCS; i++)
258 		termios->c_cc[i] = locked->c_cc[i] ?
259 			old->c_cc[i] : termios->c_cc[i];
260 	/* FIXME: What should we do for i/ospeed */
261 }
262 
263 /**
264  *	tty_termios_copy_hw	-	copy hardware settings
265  *	@new: New termios
266  *	@old: Old termios
267  *
268  *	Propagate the hardware specific terminal setting bits from
269  *	the old termios structure to the new one. This is used in cases
270  *	where the hardware does not support reconfiguration or as a helper
271  *	in some cases where only minimal reconfiguration is supported
272  */
273 
tty_termios_copy_hw(struct ktermios * new,struct ktermios * old)274 void tty_termios_copy_hw(struct ktermios *new, struct ktermios *old)
275 {
276 	/* The bits a dumb device handles in software. Smart devices need
277 	   to always provide a set_termios method */
278 	new->c_cflag &= HUPCL | CREAD | CLOCAL;
279 	new->c_cflag |= old->c_cflag & ~(HUPCL | CREAD | CLOCAL);
280 	new->c_ispeed = old->c_ispeed;
281 	new->c_ospeed = old->c_ospeed;
282 }
283 EXPORT_SYMBOL(tty_termios_copy_hw);
284 
285 /**
286  *	tty_termios_hw_change	-	check for setting change
287  *	@a: termios
288  *	@b: termios to compare
289  *
290  *	Check if any of the bits that affect a dumb device have changed
291  *	between the two termios structures, or a speed change is needed.
292  */
293 
tty_termios_hw_change(const struct ktermios * a,const struct ktermios * b)294 int tty_termios_hw_change(const struct ktermios *a, const struct ktermios *b)
295 {
296 	if (a->c_ispeed != b->c_ispeed || a->c_ospeed != b->c_ospeed)
297 		return 1;
298 	if ((a->c_cflag ^ b->c_cflag) & ~(HUPCL | CREAD | CLOCAL))
299 		return 1;
300 	return 0;
301 }
302 EXPORT_SYMBOL(tty_termios_hw_change);
303 
304 /**
305  *	tty_set_termios		-	update termios values
306  *	@tty: tty to update
307  *	@new_termios: desired new value
308  *
309  *	Perform updates to the termios values set on this terminal.
310  *	A master pty's termios should never be set.
311  *
312  *	Locking: termios_rwsem
313  */
314 
tty_set_termios(struct tty_struct * tty,struct ktermios * new_termios)315 int tty_set_termios(struct tty_struct *tty, struct ktermios *new_termios)
316 {
317 	struct ktermios old_termios;
318 	struct tty_ldisc *ld;
319 
320 	WARN_ON(tty->driver->type == TTY_DRIVER_TYPE_PTY &&
321 		tty->driver->subtype == PTY_TYPE_MASTER);
322 	/*
323 	 *	Perform the actual termios internal changes under lock.
324 	 */
325 
326 
327 	/* FIXME: we need to decide on some locking/ordering semantics
328 	   for the set_termios notification eventually */
329 	down_write(&tty->termios_rwsem);
330 	old_termios = tty->termios;
331 	tty->termios = *new_termios;
332 	unset_locked_termios(tty, &old_termios);
333 
334 	if (tty->ops->set_termios)
335 		tty->ops->set_termios(tty, &old_termios);
336 	else
337 		tty_termios_copy_hw(&tty->termios, &old_termios);
338 
339 	ld = tty_ldisc_ref(tty);
340 	if (ld != NULL) {
341 		if (ld->ops->set_termios)
342 			ld->ops->set_termios(tty, &old_termios);
343 		tty_ldisc_deref(ld);
344 	}
345 	up_write(&tty->termios_rwsem);
346 	return 0;
347 }
348 EXPORT_SYMBOL_GPL(tty_set_termios);
349 
350 /**
351  *	set_termios		-	set termios values for a tty
352  *	@tty: terminal device
353  *	@arg: user data
354  *	@opt: option information
355  *
356  *	Helper function to prepare termios data and run necessary other
357  *	functions before using tty_set_termios to do the actual changes.
358  *
359  *	Locking:
360  *		Called functions take ldisc and termios_rwsem locks
361  */
362 
set_termios(struct tty_struct * tty,void __user * arg,int opt)363 static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
364 {
365 	struct ktermios tmp_termios;
366 	struct tty_ldisc *ld;
367 	int retval = tty_check_change(tty);
368 
369 	if (retval)
370 		return retval;
371 
372 	down_read(&tty->termios_rwsem);
373 	tmp_termios = tty->termios;
374 	up_read(&tty->termios_rwsem);
375 
376 	if (opt & TERMIOS_TERMIO) {
377 		if (user_termio_to_kernel_termios(&tmp_termios,
378 						(struct termio __user *)arg))
379 			return -EFAULT;
380 #ifdef TCGETS2
381 	} else if (opt & TERMIOS_OLD) {
382 		if (user_termios_to_kernel_termios_1(&tmp_termios,
383 						(struct termios __user *)arg))
384 			return -EFAULT;
385 	} else {
386 		if (user_termios_to_kernel_termios(&tmp_termios,
387 						(struct termios2 __user *)arg))
388 			return -EFAULT;
389 	}
390 #else
391 	} else if (user_termios_to_kernel_termios(&tmp_termios,
392 					(struct termios __user *)arg))
393 		return -EFAULT;
394 #endif
395 
396 	/* If old style Bfoo values are used then load c_ispeed/c_ospeed
397 	 * with the real speed so its unconditionally usable */
398 	tmp_termios.c_ispeed = tty_termios_input_baud_rate(&tmp_termios);
399 	tmp_termios.c_ospeed = tty_termios_baud_rate(&tmp_termios);
400 
401 	if (opt & (TERMIOS_FLUSH|TERMIOS_WAIT)) {
402 retry_write_wait:
403 		retval = wait_event_interruptible(tty->write_wait, !tty_chars_in_buffer(tty));
404 		if (retval < 0)
405 			return retval;
406 
407 		if (tty_write_lock(tty, 0) < 0)
408 			goto retry_write_wait;
409 
410 		/* Racing writer? */
411 		if (tty_chars_in_buffer(tty)) {
412 			tty_write_unlock(tty);
413 			goto retry_write_wait;
414 		}
415 
416 		ld = tty_ldisc_ref(tty);
417 		if (ld != NULL) {
418 			if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
419 				ld->ops->flush_buffer(tty);
420 			tty_ldisc_deref(ld);
421 		}
422 
423 		if ((opt & TERMIOS_WAIT) && tty->ops->wait_until_sent) {
424 			tty->ops->wait_until_sent(tty, 0);
425 			if (signal_pending(current)) {
426 				tty_write_unlock(tty);
427 				return -ERESTARTSYS;
428 			}
429 		}
430 
431 		tty_set_termios(tty, &tmp_termios);
432 
433 		tty_write_unlock(tty);
434 	} else {
435 		tty_set_termios(tty, &tmp_termios);
436 	}
437 
438 	/* FIXME: Arguably if tmp_termios == tty->termios AND the
439 	   actual requested termios was not tmp_termios then we may
440 	   want to return an error as no user requested change has
441 	   succeeded */
442 	return 0;
443 }
444 
copy_termios(struct tty_struct * tty,struct ktermios * kterm)445 static void copy_termios(struct tty_struct *tty, struct ktermios *kterm)
446 {
447 	down_read(&tty->termios_rwsem);
448 	*kterm = tty->termios;
449 	up_read(&tty->termios_rwsem);
450 }
451 
copy_termios_locked(struct tty_struct * tty,struct ktermios * kterm)452 static void copy_termios_locked(struct tty_struct *tty, struct ktermios *kterm)
453 {
454 	down_read(&tty->termios_rwsem);
455 	*kterm = tty->termios_locked;
456 	up_read(&tty->termios_rwsem);
457 }
458 
get_termio(struct tty_struct * tty,struct termio __user * termio)459 static int get_termio(struct tty_struct *tty, struct termio __user *termio)
460 {
461 	struct ktermios kterm;
462 	copy_termios(tty, &kterm);
463 	if (kernel_termios_to_user_termio(termio, &kterm))
464 		return -EFAULT;
465 	return 0;
466 }
467 
468 #ifdef TIOCGETP
469 /*
470  * These are deprecated, but there is limited support..
471  *
472  * The "sg_flags" translation is a joke..
473  */
get_sgflags(struct tty_struct * tty)474 static int get_sgflags(struct tty_struct *tty)
475 {
476 	int flags = 0;
477 
478 	if (!L_ICANON(tty)) {
479 		if (L_ISIG(tty))
480 			flags |= 0x02;		/* cbreak */
481 		else
482 			flags |= 0x20;		/* raw */
483 	}
484 	if (L_ECHO(tty))
485 		flags |= 0x08;			/* echo */
486 	if (O_OPOST(tty))
487 		if (O_ONLCR(tty))
488 			flags |= 0x10;		/* crmod */
489 	return flags;
490 }
491 
get_sgttyb(struct tty_struct * tty,struct sgttyb __user * sgttyb)492 static int get_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
493 {
494 	struct sgttyb tmp;
495 
496 	down_read(&tty->termios_rwsem);
497 	tmp.sg_ispeed = tty->termios.c_ispeed;
498 	tmp.sg_ospeed = tty->termios.c_ospeed;
499 	tmp.sg_erase = tty->termios.c_cc[VERASE];
500 	tmp.sg_kill = tty->termios.c_cc[VKILL];
501 	tmp.sg_flags = get_sgflags(tty);
502 	up_read(&tty->termios_rwsem);
503 
504 	return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0;
505 }
506 
set_sgflags(struct ktermios * termios,int flags)507 static void set_sgflags(struct ktermios *termios, int flags)
508 {
509 	termios->c_iflag = ICRNL | IXON;
510 	termios->c_oflag = 0;
511 	termios->c_lflag = ISIG | ICANON;
512 	if (flags & 0x02) {	/* cbreak */
513 		termios->c_iflag = 0;
514 		termios->c_lflag &= ~ICANON;
515 	}
516 	if (flags & 0x08) {		/* echo */
517 		termios->c_lflag |= ECHO | ECHOE | ECHOK |
518 				    ECHOCTL | ECHOKE | IEXTEN;
519 	}
520 	if (flags & 0x10) {		/* crmod */
521 		termios->c_oflag |= OPOST | ONLCR;
522 	}
523 	if (flags & 0x20) {	/* raw */
524 		termios->c_iflag = 0;
525 		termios->c_lflag &= ~(ISIG | ICANON);
526 	}
527 	if (!(termios->c_lflag & ICANON)) {
528 		termios->c_cc[VMIN] = 1;
529 		termios->c_cc[VTIME] = 0;
530 	}
531 }
532 
533 /**
534  *	set_sgttyb		-	set legacy terminal values
535  *	@tty: tty structure
536  *	@sgttyb: pointer to old style terminal structure
537  *
538  *	Updates a terminal from the legacy BSD style terminal information
539  *	structure.
540  *
541  *	Locking: termios_rwsem
542  */
543 
set_sgttyb(struct tty_struct * tty,struct sgttyb __user * sgttyb)544 static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
545 {
546 	int retval;
547 	struct sgttyb tmp;
548 	struct ktermios termios;
549 
550 	retval = tty_check_change(tty);
551 	if (retval)
552 		return retval;
553 
554 	if (copy_from_user(&tmp, sgttyb, sizeof(tmp)))
555 		return -EFAULT;
556 
557 	down_write(&tty->termios_rwsem);
558 	termios = tty->termios;
559 	termios.c_cc[VERASE] = tmp.sg_erase;
560 	termios.c_cc[VKILL] = tmp.sg_kill;
561 	set_sgflags(&termios, tmp.sg_flags);
562 	/* Try and encode into Bfoo format */
563 #ifdef BOTHER
564 	tty_termios_encode_baud_rate(&termios, termios.c_ispeed,
565 						termios.c_ospeed);
566 #endif
567 	up_write(&tty->termios_rwsem);
568 	tty_set_termios(tty, &termios);
569 	return 0;
570 }
571 #endif
572 
573 #ifdef TIOCGETC
get_tchars(struct tty_struct * tty,struct tchars __user * tchars)574 static int get_tchars(struct tty_struct *tty, struct tchars __user *tchars)
575 {
576 	struct tchars tmp;
577 
578 	down_read(&tty->termios_rwsem);
579 	tmp.t_intrc = tty->termios.c_cc[VINTR];
580 	tmp.t_quitc = tty->termios.c_cc[VQUIT];
581 	tmp.t_startc = tty->termios.c_cc[VSTART];
582 	tmp.t_stopc = tty->termios.c_cc[VSTOP];
583 	tmp.t_eofc = tty->termios.c_cc[VEOF];
584 	tmp.t_brkc = tty->termios.c_cc[VEOL2];	/* what is brkc anyway? */
585 	up_read(&tty->termios_rwsem);
586 	return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
587 }
588 
set_tchars(struct tty_struct * tty,struct tchars __user * tchars)589 static int set_tchars(struct tty_struct *tty, struct tchars __user *tchars)
590 {
591 	struct tchars tmp;
592 
593 	if (copy_from_user(&tmp, tchars, sizeof(tmp)))
594 		return -EFAULT;
595 	down_write(&tty->termios_rwsem);
596 	tty->termios.c_cc[VINTR] = tmp.t_intrc;
597 	tty->termios.c_cc[VQUIT] = tmp.t_quitc;
598 	tty->termios.c_cc[VSTART] = tmp.t_startc;
599 	tty->termios.c_cc[VSTOP] = tmp.t_stopc;
600 	tty->termios.c_cc[VEOF] = tmp.t_eofc;
601 	tty->termios.c_cc[VEOL2] = tmp.t_brkc;	/* what is brkc anyway? */
602 	up_write(&tty->termios_rwsem);
603 	return 0;
604 }
605 #endif
606 
607 #ifdef TIOCGLTC
get_ltchars(struct tty_struct * tty,struct ltchars __user * ltchars)608 static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
609 {
610 	struct ltchars tmp;
611 
612 	down_read(&tty->termios_rwsem);
613 	tmp.t_suspc = tty->termios.c_cc[VSUSP];
614 	/* what is dsuspc anyway? */
615 	tmp.t_dsuspc = tty->termios.c_cc[VSUSP];
616 	tmp.t_rprntc = tty->termios.c_cc[VREPRINT];
617 	/* what is flushc anyway? */
618 	tmp.t_flushc = tty->termios.c_cc[VEOL2];
619 	tmp.t_werasc = tty->termios.c_cc[VWERASE];
620 	tmp.t_lnextc = tty->termios.c_cc[VLNEXT];
621 	up_read(&tty->termios_rwsem);
622 	return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
623 }
624 
set_ltchars(struct tty_struct * tty,struct ltchars __user * ltchars)625 static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
626 {
627 	struct ltchars tmp;
628 
629 	if (copy_from_user(&tmp, ltchars, sizeof(tmp)))
630 		return -EFAULT;
631 
632 	down_write(&tty->termios_rwsem);
633 	tty->termios.c_cc[VSUSP] = tmp.t_suspc;
634 	/* what is dsuspc anyway? */
635 	tty->termios.c_cc[VEOL2] = tmp.t_dsuspc;
636 	tty->termios.c_cc[VREPRINT] = tmp.t_rprntc;
637 	/* what is flushc anyway? */
638 	tty->termios.c_cc[VEOL2] = tmp.t_flushc;
639 	tty->termios.c_cc[VWERASE] = tmp.t_werasc;
640 	tty->termios.c_cc[VLNEXT] = tmp.t_lnextc;
641 	up_write(&tty->termios_rwsem);
642 	return 0;
643 }
644 #endif
645 
646 /**
647  *	tty_change_softcar	-	carrier change ioctl helper
648  *	@tty: tty to update
649  *	@arg: enable/disable CLOCAL
650  *
651  *	Perform a change to the CLOCAL state and call into the driver
652  *	layer to make it visible. All done with the termios rwsem
653  */
654 
tty_change_softcar(struct tty_struct * tty,int arg)655 static int tty_change_softcar(struct tty_struct *tty, int arg)
656 {
657 	int ret = 0;
658 	int bit = arg ? CLOCAL : 0;
659 	struct ktermios old;
660 
661 	down_write(&tty->termios_rwsem);
662 	old = tty->termios;
663 	tty->termios.c_cflag &= ~CLOCAL;
664 	tty->termios.c_cflag |= bit;
665 	if (tty->ops->set_termios)
666 		tty->ops->set_termios(tty, &old);
667 	if (C_CLOCAL(tty) != bit)
668 		ret = -EINVAL;
669 	up_write(&tty->termios_rwsem);
670 	return ret;
671 }
672 
673 /**
674  *	tty_mode_ioctl		-	mode related ioctls
675  *	@tty: tty for the ioctl
676  *	@file: file pointer for the tty
677  *	@cmd: command
678  *	@arg: ioctl argument
679  *
680  *	Perform non line discipline specific mode control ioctls. This
681  *	is designed to be called by line disciplines to ensure they provide
682  *	consistent mode setting.
683  */
684 
tty_mode_ioctl(struct tty_struct * tty,struct file * file,unsigned int cmd,unsigned long arg)685 int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
686 			unsigned int cmd, unsigned long arg)
687 {
688 	struct tty_struct *real_tty;
689 	void __user *p = (void __user *)arg;
690 	int ret = 0;
691 	struct ktermios kterm;
692 
693 	BUG_ON(file == NULL);
694 
695 	if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
696 	    tty->driver->subtype == PTY_TYPE_MASTER)
697 		real_tty = tty->link;
698 	else
699 		real_tty = tty;
700 
701 	switch (cmd) {
702 #ifdef TIOCGETP
703 	case TIOCGETP:
704 		return get_sgttyb(real_tty, (struct sgttyb __user *) arg);
705 	case TIOCSETP:
706 	case TIOCSETN:
707 		return set_sgttyb(real_tty, (struct sgttyb __user *) arg);
708 #endif
709 #ifdef TIOCGETC
710 	case TIOCGETC:
711 		return get_tchars(real_tty, p);
712 	case TIOCSETC:
713 		return set_tchars(real_tty, p);
714 #endif
715 #ifdef TIOCGLTC
716 	case TIOCGLTC:
717 		return get_ltchars(real_tty, p);
718 	case TIOCSLTC:
719 		return set_ltchars(real_tty, p);
720 #endif
721 	case TCSETSF:
722 		return set_termios(real_tty, p,  TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_OLD);
723 	case TCSETSW:
724 		return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_OLD);
725 	case TCSETS:
726 		return set_termios(real_tty, p, TERMIOS_OLD);
727 #ifndef TCGETS2
728 	case TCGETS:
729 		copy_termios(real_tty, &kterm);
730 		if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
731 			ret = -EFAULT;
732 		return ret;
733 #else
734 	case TCGETS:
735 		copy_termios(real_tty, &kterm);
736 		if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
737 			ret = -EFAULT;
738 		return ret;
739 	case TCGETS2:
740 		copy_termios(real_tty, &kterm);
741 		if (kernel_termios_to_user_termios((struct termios2 __user *)arg, &kterm))
742 			ret = -EFAULT;
743 		return ret;
744 	case TCSETSF2:
745 		return set_termios(real_tty, p,  TERMIOS_FLUSH | TERMIOS_WAIT);
746 	case TCSETSW2:
747 		return set_termios(real_tty, p, TERMIOS_WAIT);
748 	case TCSETS2:
749 		return set_termios(real_tty, p, 0);
750 #endif
751 	case TCGETA:
752 		return get_termio(real_tty, p);
753 	case TCSETAF:
754 		return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO);
755 	case TCSETAW:
756 		return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO);
757 	case TCSETA:
758 		return set_termios(real_tty, p, TERMIOS_TERMIO);
759 #ifndef TCGETS2
760 	case TIOCGLCKTRMIOS:
761 		copy_termios_locked(real_tty, &kterm);
762 		if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
763 			ret = -EFAULT;
764 		return ret;
765 	case TIOCSLCKTRMIOS:
766 		if (!capable(CAP_SYS_ADMIN))
767 			return -EPERM;
768 		copy_termios_locked(real_tty, &kterm);
769 		if (user_termios_to_kernel_termios(&kterm,
770 					       (struct termios __user *) arg))
771 			return -EFAULT;
772 		down_write(&real_tty->termios_rwsem);
773 		real_tty->termios_locked = kterm;
774 		up_write(&real_tty->termios_rwsem);
775 		return 0;
776 #else
777 	case TIOCGLCKTRMIOS:
778 		copy_termios_locked(real_tty, &kterm);
779 		if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
780 			ret = -EFAULT;
781 		return ret;
782 	case TIOCSLCKTRMIOS:
783 		if (!capable(CAP_SYS_ADMIN))
784 			return -EPERM;
785 		copy_termios_locked(real_tty, &kterm);
786 		if (user_termios_to_kernel_termios_1(&kterm,
787 					       (struct termios __user *) arg))
788 			return -EFAULT;
789 		down_write(&real_tty->termios_rwsem);
790 		real_tty->termios_locked = kterm;
791 		up_write(&real_tty->termios_rwsem);
792 		return ret;
793 #endif
794 #ifdef TCGETX
795 	case TCGETX:
796 	case TCSETX:
797 	case TCSETXW:
798 	case TCSETXF:
799 		return -ENOTTY;
800 #endif
801 	case TIOCGSOFTCAR:
802 		copy_termios(real_tty, &kterm);
803 		ret = put_user((kterm.c_cflag & CLOCAL) ? 1 : 0,
804 						(int __user *)arg);
805 		return ret;
806 	case TIOCSSOFTCAR:
807 		if (get_user(arg, (unsigned int __user *) arg))
808 			return -EFAULT;
809 		return tty_change_softcar(real_tty, arg);
810 	default:
811 		return -ENOIOCTLCMD;
812 	}
813 }
814 EXPORT_SYMBOL_GPL(tty_mode_ioctl);
815 
816 
817 /* Caller guarantees ldisc reference is held */
__tty_perform_flush(struct tty_struct * tty,unsigned long arg)818 static int __tty_perform_flush(struct tty_struct *tty, unsigned long arg)
819 {
820 	struct tty_ldisc *ld = tty->ldisc;
821 
822 	switch (arg) {
823 	case TCIFLUSH:
824 		if (ld && ld->ops->flush_buffer) {
825 			ld->ops->flush_buffer(tty);
826 			tty_unthrottle(tty);
827 		}
828 		break;
829 	case TCIOFLUSH:
830 		if (ld && ld->ops->flush_buffer) {
831 			ld->ops->flush_buffer(tty);
832 			tty_unthrottle(tty);
833 		}
834 		fallthrough;
835 	case TCOFLUSH:
836 		tty_driver_flush_buffer(tty);
837 		break;
838 	default:
839 		return -EINVAL;
840 	}
841 	return 0;
842 }
843 
tty_perform_flush(struct tty_struct * tty,unsigned long arg)844 int tty_perform_flush(struct tty_struct *tty, unsigned long arg)
845 {
846 	struct tty_ldisc *ld;
847 	int retval = tty_check_change(tty);
848 	if (retval)
849 		return retval;
850 
851 	ld = tty_ldisc_ref_wait(tty);
852 	retval = __tty_perform_flush(tty, arg);
853 	if (ld)
854 		tty_ldisc_deref(ld);
855 	return retval;
856 }
857 EXPORT_SYMBOL_GPL(tty_perform_flush);
858 
n_tty_ioctl_helper(struct tty_struct * tty,struct file * file,unsigned int cmd,unsigned long arg)859 int n_tty_ioctl_helper(struct tty_struct *tty, struct file *file,
860 		       unsigned int cmd, unsigned long arg)
861 {
862 	int retval;
863 
864 	switch (cmd) {
865 	case TCXONC:
866 		retval = tty_check_change(tty);
867 		if (retval)
868 			return retval;
869 		switch (arg) {
870 		case TCOOFF:
871 			spin_lock_irq(&tty->flow_lock);
872 			if (!tty->flow_stopped) {
873 				tty->flow_stopped = 1;
874 				__stop_tty(tty);
875 			}
876 			spin_unlock_irq(&tty->flow_lock);
877 			break;
878 		case TCOON:
879 			spin_lock_irq(&tty->flow_lock);
880 			if (tty->flow_stopped) {
881 				tty->flow_stopped = 0;
882 				__start_tty(tty);
883 			}
884 			spin_unlock_irq(&tty->flow_lock);
885 			break;
886 		case TCIOFF:
887 			if (STOP_CHAR(tty) != __DISABLED_CHAR)
888 				retval = tty_send_xchar(tty, STOP_CHAR(tty));
889 			break;
890 		case TCION:
891 			if (START_CHAR(tty) != __DISABLED_CHAR)
892 				retval = tty_send_xchar(tty, START_CHAR(tty));
893 			break;
894 		default:
895 			return -EINVAL;
896 		}
897 		return retval;
898 	case TCFLSH:
899 		retval = tty_check_change(tty);
900 		if (retval)
901 			return retval;
902 		return __tty_perform_flush(tty, arg);
903 	default:
904 		/* Try the mode commands */
905 		return tty_mode_ioctl(tty, file, cmd, arg);
906 	}
907 }
908 EXPORT_SYMBOL(n_tty_ioctl_helper);
909