blob: 6e30949d9f7794a584c1503fe8dbf7a51d0e83b7 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * ioctl32.c: Conversion between 32bit and 64bit native ioctls.
4 *
5 * Copyright (C) 1997-2000 Jakub Jelinek (jakub@redhat.com)
6 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
7 * Copyright (C) 2001,2002 Andi Kleen, SuSE Labs
Pavel Macheka2531292010-07-18 14:27:13 +02008 * Copyright (C) 2003 Pavel Machek (pavel@ucw.cz)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * These routines maintain argument size conversion between 32bit and 64bit
11 * ioctls.
12 */
13
Akos Maroy3fee37c2008-01-06 11:15:55 +010014#include <linux/joystick.h>
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/types.h>
17#include <linux/compat.h>
18#include <linux/kernel.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080019#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/compiler.h>
21#include <linux/sched.h>
22#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/ioctl.h>
24#include <linux/if.h>
NeilBrownbff61972009-03-31 14:33:13 +110025#include <linux/raid/md_u.h>
Ankit Jain3e63cbb2009-06-19 14:28:07 -040026#include <linux/falloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/file.h>
Paul Mackerras4b32da2b2012-03-04 12:56:55 +000028#include <linux/ppp-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/if_pppox.h>
30#include <linux/mtio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/tty.h>
32#include <linux/vt_kern.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/raw.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/rtc.h>
36#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/syscalls.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/gfp.h>
Hans Verkuil594edf32015-11-11 08:26:12 -020041#include <linux/cec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Al Viro66cf1912016-01-07 09:53:30 -050043#include "internal.h"
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <net/bluetooth/bluetooth.h>
Marcel Holtmannf49daa82014-07-11 04:58:29 +020046#include <net/bluetooth/hci_sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <net/bluetooth/rfcomm.h>
48
49#include <linux/capi.h>
Hansjoerg Lipp5024ad42006-06-26 00:25:35 -070050#include <linux/gigaset_dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Randy Dunlap3c3622d2008-07-16 08:52:00 -050052#ifdef CONFIG_BLOCK
Johannes Stezenbach390192b2011-07-01 22:32:26 +020053#include <linux/cdrom.h>
54#include <linux/fd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <scsi/scsi_ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <scsi/sg.h>
Randy Dunlap3c3622d2008-07-16 08:52:00 -050058#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080060#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/watchdog.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/soundcard.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65#include <linux/hiddev.h>
66
David S. Miller6e87abd2005-11-16 00:52:57 -080067
Arnd Bergmann661f6272009-11-05 19:52:55 +010068#include <linux/sort.h>
69
David S. Miller81639042007-07-29 00:50:42 -070070#ifdef CONFIG_SPARC
Al Viro0976d4e2018-08-16 21:10:42 -040071#include <linux/fb.h>
David S. Miller81639042007-07-29 00:50:42 -070072#include <asm/fbio.h>
73#endif
74
Jann Horna7f61e892016-01-05 18:27:30 +010075#define convert_in_user(srcptr, dstptr) \
76({ \
77 typeof(*srcptr) val; \
78 \
79 get_user(val, srcptr) || put_user(val, dstptr); \
80})
81
Al Viro66cf1912016-01-07 09:53:30 -050082static int do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Jann Hornb4341722016-01-05 18:27:29 +010083{
84 int err;
85
86 err = security_file_ioctl(file, cmd, arg);
87 if (err)
88 return err;
89
Al Viro66cf1912016-01-07 09:53:30 -050090 return vfs_ioctl(file, cmd, arg);
Jann Hornb4341722016-01-05 18:27:29 +010091}
92
David Howells93614012006-09-30 20:45:40 +020093#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -070094typedef struct sg_io_hdr32 {
95 compat_int_t interface_id; /* [i] 'S' for SCSI generic (required) */
96 compat_int_t dxfer_direction; /* [i] data transfer direction */
97 unsigned char cmd_len; /* [i] SCSI command length ( <= 16 bytes) */
98 unsigned char mx_sb_len; /* [i] max length to write to sbp */
99 unsigned short iovec_count; /* [i] 0 implies no scatter gather */
100 compat_uint_t dxfer_len; /* [i] byte count of data transfer */
101 compat_uint_t dxferp; /* [i], [*io] points to data transfer memory
102 or scatter gather list */
103 compat_uptr_t cmdp; /* [i], [*i] points to command to perform */
104 compat_uptr_t sbp; /* [i], [*o] points to sense_buffer memory */
105 compat_uint_t timeout; /* [i] MAX_UINT->no timeout (unit: millisec) */
106 compat_uint_t flags; /* [i] 0 -> default, see SG_FLAG... */
107 compat_int_t pack_id; /* [i->o] unused internally (normally) */
108 compat_uptr_t usr_ptr; /* [i->o] unused internally */
109 unsigned char status; /* [o] scsi status */
110 unsigned char masked_status; /* [o] shifted, masked scsi status */
111 unsigned char msg_status; /* [o] messaging level data (optional) */
112 unsigned char sb_len_wr; /* [o] byte count actually written to sbp */
113 unsigned short host_status; /* [o] errors from host adapter */
114 unsigned short driver_status; /* [o] errors from software driver */
115 compat_int_t resid; /* [o] dxfer_len - actual_transferred */
116 compat_uint_t duration; /* [o] time taken by cmd (unit: millisec) */
117 compat_uint_t info; /* [o] auxiliary information */
118} sg_io_hdr32_t; /* 64 bytes long (on sparc32) */
119
120typedef struct sg_iovec32 {
121 compat_uint_t iov_base;
122 compat_uint_t iov_len;
123} sg_iovec32_t;
124
125static int sg_build_iovec(sg_io_hdr_t __user *sgio, void __user *dxferp, u16 iovec_count)
126{
127 sg_iovec_t __user *iov = (sg_iovec_t __user *) (sgio + 1);
128 sg_iovec32_t __user *iov32 = dxferp;
129 int i;
130
131 for (i = 0; i < iovec_count; i++) {
132 u32 base, len;
133
134 if (get_user(base, &iov32[i].iov_base) ||
135 get_user(len, &iov32[i].iov_len) ||
136 put_user(compat_ptr(base), &iov[i].iov_base) ||
137 put_user(len, &iov[i].iov_len))
138 return -EFAULT;
139 }
140
141 if (put_user(iov, &sgio->dxferp))
142 return -EFAULT;
143 return 0;
144}
145
Al Viro66cf1912016-01-07 09:53:30 -0500146static int sg_ioctl_trans(struct file *file, unsigned int cmd,
Arnd Bergmann43c6e7b92009-11-14 23:16:18 +0100147 sg_io_hdr32_t __user *sgio32)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 sg_io_hdr_t __user *sgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 u16 iovec_count;
151 u32 data;
152 void __user *dxferp;
153 int err;
FUJITA Tomonori84eb8fb2010-01-05 19:41:44 +0900154 int interface_id;
155
156 if (get_user(interface_id, &sgio32->interface_id))
157 return -EFAULT;
158 if (interface_id != 'S')
Al Viro66cf1912016-01-07 09:53:30 -0500159 return do_ioctl(file, cmd, (unsigned long)sgio32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (get_user(iovec_count, &sgio32->iovec_count))
162 return -EFAULT;
163
164 {
165 void __user *top = compat_alloc_user_space(0);
166 void __user *new = compat_alloc_user_space(sizeof(sg_io_hdr_t) +
167 (iovec_count * sizeof(sg_iovec_t)));
168 if (new > top)
169 return -EINVAL;
170
171 sgio = new;
172 }
173
174 /* Ok, now construct. */
175 if (copy_in_user(&sgio->interface_id, &sgio32->interface_id,
176 (2 * sizeof(int)) +
177 (2 * sizeof(unsigned char)) +
178 (1 * sizeof(unsigned short)) +
179 (1 * sizeof(unsigned int))))
180 return -EFAULT;
181
182 if (get_user(data, &sgio32->dxferp))
183 return -EFAULT;
184 dxferp = compat_ptr(data);
185 if (iovec_count) {
186 if (sg_build_iovec(sgio, dxferp, iovec_count))
187 return -EFAULT;
188 } else {
189 if (put_user(dxferp, &sgio->dxferp))
190 return -EFAULT;
191 }
192
193 {
194 unsigned char __user *cmdp;
195 unsigned char __user *sbp;
196
197 if (get_user(data, &sgio32->cmdp))
198 return -EFAULT;
199 cmdp = compat_ptr(data);
200
201 if (get_user(data, &sgio32->sbp))
202 return -EFAULT;
203 sbp = compat_ptr(data);
204
205 if (put_user(cmdp, &sgio->cmdp) ||
206 put_user(sbp, &sgio->sbp))
207 return -EFAULT;
208 }
209
210 if (copy_in_user(&sgio->timeout, &sgio32->timeout,
211 3 * sizeof(int)))
212 return -EFAULT;
213
214 if (get_user(data, &sgio32->usr_ptr))
215 return -EFAULT;
216 if (put_user(compat_ptr(data), &sgio->usr_ptr))
217 return -EFAULT;
218
Al Viro66cf1912016-01-07 09:53:30 -0500219 err = do_ioctl(file, cmd, (unsigned long) sgio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 if (err >= 0) {
222 void __user *datap;
223
224 if (copy_in_user(&sgio32->pack_id, &sgio->pack_id,
225 sizeof(int)) ||
226 get_user(datap, &sgio->usr_ptr) ||
227 put_user((u32)(unsigned long)datap,
228 &sgio32->usr_ptr) ||
229 copy_in_user(&sgio32->status, &sgio->status,
230 (4 * sizeof(unsigned char)) +
231 (2 * sizeof(unsigned short)) +
232 (3 * sizeof(int))))
233 err = -EFAULT;
234 }
235
236 return err;
237}
238
Andi Kleen29663872006-01-11 22:44:03 +0100239struct compat_sg_req_info { /* used by SG_GET_REQUEST_TABLE ioctl() */
240 char req_state;
241 char orphan;
242 char sg_io_owned;
243 char problem;
244 int pack_id;
245 compat_uptr_t usr_ptr;
246 unsigned int duration;
247 int unused;
248};
249
Al Viro66cf1912016-01-07 09:53:30 -0500250static int sg_grt_trans(struct file *file,
Jann Hornb4341722016-01-05 18:27:29 +0100251 unsigned int cmd, struct compat_sg_req_info __user *o)
Andi Kleen29663872006-01-11 22:44:03 +0100252{
253 int err, i;
Al Viro6b2b4e52006-02-01 06:33:33 -0500254 sg_req_info_t __user *r;
Andi Kleen29663872006-01-11 22:44:03 +0100255 r = compat_alloc_user_space(sizeof(sg_req_info_t)*SG_MAX_QUEUE);
Al Viro66cf1912016-01-07 09:53:30 -0500256 err = do_ioctl(file, cmd, (unsigned long)r);
Andi Kleen29663872006-01-11 22:44:03 +0100257 if (err < 0)
258 return err;
259 for (i = 0; i < SG_MAX_QUEUE; i++) {
260 void __user *ptr;
261 int d;
262
263 if (copy_in_user(o + i, r + i, offsetof(sg_req_info_t, usr_ptr)) ||
264 get_user(ptr, &r[i].usr_ptr) ||
265 get_user(d, &r[i].duration) ||
266 put_user((u32)(unsigned long)(ptr), &o[i].usr_ptr) ||
267 put_user(d, &o[i].duration))
268 return -EFAULT;
269 }
270 return err;
271}
David Howells93614012006-09-30 20:45:40 +0200272#endif /* CONFIG_BLOCK */
Andi Kleen29663872006-01-11 22:44:03 +0100273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274struct sock_fprog32 {
275 unsigned short len;
276 compat_caddr_t filter;
277};
278
279#define PPPIOCSPASS32 _IOW('t', 71, struct sock_fprog32)
280#define PPPIOCSACTIVE32 _IOW('t', 70, struct sock_fprog32)
281
Al Viro66cf1912016-01-07 09:53:30 -0500282static int ppp_sock_fprog_ioctl_trans(struct file *file,
Jann Hornb4341722016-01-05 18:27:29 +0100283 unsigned int cmd, struct sock_fprog32 __user *u_fprog32)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 struct sock_fprog __user *u_fprog64 = compat_alloc_user_space(sizeof(struct sock_fprog));
286 void __user *fptr64;
287 u32 fptr32;
288 u16 flen;
289
290 if (get_user(flen, &u_fprog32->len) ||
291 get_user(fptr32, &u_fprog32->filter))
292 return -EFAULT;
293
294 fptr64 = compat_ptr(fptr32);
295
296 if (put_user(flen, &u_fprog64->len) ||
297 put_user(fptr64, &u_fprog64->filter))
298 return -EFAULT;
299
300 if (cmd == PPPIOCSPASS32)
301 cmd = PPPIOCSPASS;
302 else
303 cmd = PPPIOCSACTIVE;
304
Al Viro66cf1912016-01-07 09:53:30 -0500305 return do_ioctl(file, cmd, (unsigned long) u_fprog64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308struct ppp_option_data32 {
309 compat_caddr_t ptr;
310 u32 length;
311 compat_int_t transmit;
312};
313#define PPPIOCSCOMPRESS32 _IOW('t', 77, struct ppp_option_data32)
314
315struct ppp_idle32 {
316 compat_time_t xmit_idle;
317 compat_time_t recv_idle;
318};
319#define PPPIOCGIDLE32 _IOR('t', 63, struct ppp_idle32)
320
Al Viro66cf1912016-01-07 09:53:30 -0500321static int ppp_gidle(struct file *file, unsigned int cmd,
Arnd Bergmann43c6e7b92009-11-14 23:16:18 +0100322 struct ppp_idle32 __user *idle32)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 struct ppp_idle __user *idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 __kernel_time_t xmit, recv;
326 int err;
327
328 idle = compat_alloc_user_space(sizeof(*idle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Al Viro66cf1912016-01-07 09:53:30 -0500330 err = do_ioctl(file, PPPIOCGIDLE, (unsigned long) idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 if (!err) {
333 if (get_user(xmit, &idle->xmit_idle) ||
334 get_user(recv, &idle->recv_idle) ||
335 put_user(xmit, &idle32->xmit_idle) ||
336 put_user(recv, &idle32->recv_idle))
337 err = -EFAULT;
338 }
339 return err;
340}
341
Al Viro66cf1912016-01-07 09:53:30 -0500342static int ppp_scompress(struct file *file, unsigned int cmd,
Arnd Bergmann43c6e7b92009-11-14 23:16:18 +0100343 struct ppp_option_data32 __user *odata32)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 struct ppp_option_data __user *odata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 __u32 data;
347 void __user *datap;
348
349 odata = compat_alloc_user_space(sizeof(*odata));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 if (get_user(data, &odata32->ptr))
352 return -EFAULT;
353
354 datap = compat_ptr(data);
355 if (put_user(datap, &odata->ptr))
356 return -EFAULT;
357
358 if (copy_in_user(&odata->length, &odata32->length,
359 sizeof(__u32) + sizeof(int)))
360 return -EFAULT;
361
Al Viro66cf1912016-01-07 09:53:30 -0500362 return do_ioctl(file, PPPIOCSCOMPRESS, (unsigned long) odata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
David Howells93614012006-09-30 20:45:40 +0200365#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366struct mtget32 {
367 compat_long_t mt_type;
368 compat_long_t mt_resid;
369 compat_long_t mt_dsreg;
370 compat_long_t mt_gstat;
371 compat_long_t mt_erreg;
372 compat_daddr_t mt_fileno;
373 compat_daddr_t mt_blkno;
374};
375#define MTIOCGET32 _IOR('m', 2, struct mtget32)
376
377struct mtpos32 {
378 compat_long_t mt_blkno;
379};
380#define MTIOCPOS32 _IOR('m', 3, struct mtpos32)
381
Al Viro66cf1912016-01-07 09:53:30 -0500382static int mt_ioctl_trans(struct file *file,
Jann Hornb4341722016-01-05 18:27:29 +0100383 unsigned int cmd, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Jann Horna7f61e892016-01-05 18:27:30 +0100385 /* NULL initialization to make gcc shut up */
386 struct mtget __user *get = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 struct mtget32 __user *umget32;
Jann Horna7f61e892016-01-05 18:27:30 +0100388 struct mtpos __user *pos = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 struct mtpos32 __user *upos32;
390 unsigned long kcmd;
391 void *karg;
392 int err = 0;
393
394 switch(cmd) {
395 case MTIOCPOS32:
396 kcmd = MTIOCPOS;
Jann Horna7f61e892016-01-05 18:27:30 +0100397 pos = compat_alloc_user_space(sizeof(*pos));
398 karg = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 break;
Andrew Morton45bf5cd2010-03-05 13:43:19 -0800400 default: /* MTIOCGET32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 kcmd = MTIOCGET;
Jann Horna7f61e892016-01-05 18:27:30 +0100402 get = compat_alloc_user_space(sizeof(*get));
403 karg = get;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
Jann Horna7f61e892016-01-05 18:27:30 +0100406 if (karg == NULL)
407 return -EFAULT;
Al Viro66cf1912016-01-07 09:53:30 -0500408 err = do_ioctl(file, kcmd, (unsigned long)karg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (err)
410 return err;
411 switch (cmd) {
412 case MTIOCPOS32:
Arnd Bergmann43c6e7b92009-11-14 23:16:18 +0100413 upos32 = argp;
Jann Horna7f61e892016-01-05 18:27:30 +0100414 err = convert_in_user(&pos->mt_blkno, &upos32->mt_blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 break;
416 case MTIOCGET32:
Arnd Bergmann43c6e7b92009-11-14 23:16:18 +0100417 umget32 = argp;
Jann Horna7f61e892016-01-05 18:27:30 +0100418 err = convert_in_user(&get->mt_type, &umget32->mt_type);
419 err |= convert_in_user(&get->mt_resid, &umget32->mt_resid);
420 err |= convert_in_user(&get->mt_dsreg, &umget32->mt_dsreg);
421 err |= convert_in_user(&get->mt_gstat, &umget32->mt_gstat);
422 err |= convert_in_user(&get->mt_erreg, &umget32->mt_erreg);
423 err |= convert_in_user(&get->mt_fileno, &umget32->mt_fileno);
424 err |= convert_in_user(&get->mt_blkno, &umget32->mt_blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 break;
426 }
427 return err ? -EFAULT: 0;
428}
429
David Howells93614012006-09-30 20:45:40 +0200430#endif /* CONFIG_BLOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432/* Bluetooth ioctls */
Johan Hedberg2cdf0962010-07-09 16:28:36 -0300433#define HCIUARTSETPROTO _IOW('U', 200, int)
434#define HCIUARTGETPROTO _IOR('U', 201, int)
435#define HCIUARTGETDEVICE _IOR('U', 202, int)
Johan Hedberg63c7d092010-07-12 11:37:04 -0300436#define HCIUARTSETFLAGS _IOW('U', 203, int)
437#define HCIUARTGETFLAGS _IOR('U', 204, int)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Christoph Hellwigec3cad92006-01-09 20:52:11 -0800439#define RTC_IRQP_READ32 _IOR('p', 0x0b, compat_ulong_t)
440#define RTC_IRQP_SET32 _IOW('p', 0x0c, compat_ulong_t)
441#define RTC_EPOCH_READ32 _IOR('p', 0x0d, compat_ulong_t)
442#define RTC_EPOCH_SET32 _IOW('p', 0x0e, compat_ulong_t)
443
Al Viro66cf1912016-01-07 09:53:30 -0500444static int rtc_ioctl(struct file *file,
Jann Hornb4341722016-01-05 18:27:29 +0100445 unsigned cmd, void __user *argp)
Christoph Hellwigec3cad92006-01-09 20:52:11 -0800446{
Jann Horna7f61e892016-01-05 18:27:30 +0100447 unsigned long __user *valp = compat_alloc_user_space(sizeof(*valp));
Christoph Hellwigec3cad92006-01-09 20:52:11 -0800448 int ret;
449
Jann Horna7f61e892016-01-05 18:27:30 +0100450 if (valp == NULL)
451 return -EFAULT;
Christoph Hellwigec3cad92006-01-09 20:52:11 -0800452 switch (cmd) {
453 case RTC_IRQP_READ32:
454 case RTC_EPOCH_READ32:
Al Viro66cf1912016-01-07 09:53:30 -0500455 ret = do_ioctl(file, (cmd == RTC_IRQP_READ32) ?
Christoph Hellwigec3cad92006-01-09 20:52:11 -0800456 RTC_IRQP_READ : RTC_EPOCH_READ,
Jann Horna7f61e892016-01-05 18:27:30 +0100457 (unsigned long)valp);
Christoph Hellwigec3cad92006-01-09 20:52:11 -0800458 if (ret)
459 return ret;
Jann Horna7f61e892016-01-05 18:27:30 +0100460 return convert_in_user(valp, (unsigned int __user *)argp);
Christoph Hellwigec3cad92006-01-09 20:52:11 -0800461 case RTC_IRQP_SET32:
Al Viro66cf1912016-01-07 09:53:30 -0500462 return do_ioctl(file, RTC_IRQP_SET, (unsigned long)argp);
Christoph Hellwigec3cad92006-01-09 20:52:11 -0800463 case RTC_EPOCH_SET32:
Al Viro66cf1912016-01-07 09:53:30 -0500464 return do_ioctl(file, RTC_EPOCH_SET, (unsigned long)argp);
Christoph Hellwigec3cad92006-01-09 20:52:11 -0800465 }
Arnd Bergmann43c6e7b92009-11-14 23:16:18 +0100466
467 return -ENOIOCTLCMD;
Christoph Hellwigec3cad92006-01-09 20:52:11 -0800468}
469
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400470/* on ia32 l_start is on a 32-bit boundary */
471#if defined(CONFIG_IA64) || defined(CONFIG_X86_64)
472struct space_resv_32 {
473 __s16 l_type;
474 __s16 l_whence;
475 __s64 l_start __attribute__((packed));
476 /* len == 0 means until end of file */
477 __s64 l_len __attribute__((packed));
478 __s32 l_sysid;
479 __u32 l_pid;
480 __s32 l_pad[4]; /* reserve area */
481};
482
483#define FS_IOC_RESVSP_32 _IOW ('X', 40, struct space_resv_32)
484#define FS_IOC_RESVSP64_32 _IOW ('X', 42, struct space_resv_32)
485
486/* just account for different alignment */
Arnd Bergmann43c6e7b92009-11-14 23:16:18 +0100487static int compat_ioctl_preallocate(struct file *file,
488 struct space_resv_32 __user *p32)
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400489{
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400490 struct space_resv __user *p = compat_alloc_user_space(sizeof(*p));
491
492 if (copy_in_user(&p->l_type, &p32->l_type, sizeof(s16)) ||
493 copy_in_user(&p->l_whence, &p32->l_whence, sizeof(s16)) ||
494 copy_in_user(&p->l_start, &p32->l_start, sizeof(s64)) ||
495 copy_in_user(&p->l_len, &p32->l_len, sizeof(s64)) ||
496 copy_in_user(&p->l_sysid, &p32->l_sysid, sizeof(s32)) ||
497 copy_in_user(&p->l_pid, &p32->l_pid, sizeof(u32)) ||
498 copy_in_user(&p->l_pad, &p32->l_pad, 4*sizeof(u32)))
499 return -EFAULT;
500
501 return ioctl_preallocate(file, p);
502}
503#endif
504
Arnd Bergmann661f6272009-11-05 19:52:55 +0100505/*
506 * simple reversible transform to make our table more evenly
507 * distributed after sorting.
508 */
509#define XFORM(i) (((i) ^ ((i) << 27) ^ ((i) << 17)) & 0xffffffff)
Christoph Hellwig6272e262007-05-08 00:29:21 -0700510
Mark Charlebois9280cdd2017-04-28 15:15:12 -0700511#define COMPATIBLE_IOCTL(cmd) XFORM((u32)cmd),
Andi Kleen421f0282007-05-02 19:27:20 +0200512/* ioctl should not be warned about even if it's not implemented.
513 Valid reasons to use this:
514 - It is implemented with ->compat_ioctl on some device, but programs
515 call it on others too.
516 - The ioctl is not implemented in the native kernel, but programs
517 call it commonly anyways.
518 Most other reasons are not valid. */
519#define IGNORE_IOCTL(cmd) COMPATIBLE_IOCTL(cmd)
Christoph Hellwige6a6d2e2006-01-09 20:52:14 -0800520
Arnd Bergmann661f6272009-11-05 19:52:55 +0100521static unsigned int ioctl_pointer[] = {
Christoph Hellwig644fd4f2007-05-08 00:29:07 -0700522/* compatible ioctls first */
Christoph Hellwig644fd4f2007-05-08 00:29:07 -0700523/* Little t */
Christoph Hellwig644fd4f2007-05-08 00:29:07 -0700524COMPATIBLE_IOCTL(TIOCOUTQ)
Christoph Hellwig644fd4f2007-05-08 00:29:07 -0700525/* Little f */
526COMPATIBLE_IOCTL(FIOCLEX)
527COMPATIBLE_IOCTL(FIONCLEX)
528COMPATIBLE_IOCTL(FIOASYNC)
529COMPATIBLE_IOCTL(FIONBIO)
530COMPATIBLE_IOCTL(FIONREAD) /* This is also TIOCINQ */
Eric Sandeen69130c72009-08-06 15:07:37 -0700531COMPATIBLE_IOCTL(FS_IOC_FIEMAP)
Christoph Hellwig644fd4f2007-05-08 00:29:07 -0700532/* 0x00 */
533COMPATIBLE_IOCTL(FIBMAP)
534COMPATIBLE_IOCTL(FIGETBSZ)
Christoph Hellwig5cf8cf42009-02-26 21:32:51 +0100535/* 'X' - originally XFS but some now in the VFS */
536COMPATIBLE_IOCTL(FIFREEZE)
537COMPATIBLE_IOCTL(FITHAW)
Mikulas Patocka9abea2d2015-07-09 18:05:15 +0200538COMPATIBLE_IOCTL(FITRIM)
Randy Dunlap3c3622d2008-07-16 08:52:00 -0500539#ifdef CONFIG_BLOCK
Christoph Hellwig644fd4f2007-05-08 00:29:07 -0700540/* Big S */
541COMPATIBLE_IOCTL(SCSI_IOCTL_GET_IDLUN)
542COMPATIBLE_IOCTL(SCSI_IOCTL_DOORLOCK)
543COMPATIBLE_IOCTL(SCSI_IOCTL_DOORUNLOCK)
544COMPATIBLE_IOCTL(SCSI_IOCTL_TEST_UNIT_READY)
545COMPATIBLE_IOCTL(SCSI_IOCTL_GET_BUS_NUMBER)
546COMPATIBLE_IOCTL(SCSI_IOCTL_SEND_COMMAND)
547COMPATIBLE_IOCTL(SCSI_IOCTL_PROBE_HOST)
548COMPATIBLE_IOCTL(SCSI_IOCTL_GET_PCI)
Randy Dunlap3c3622d2008-07-16 08:52:00 -0500549#endif
Atsushi Nemoto3f001712010-01-10 23:29:16 +0900550/* Big V (don't complain on serial console) */
551IGNORE_IOCTL(VT_OPENQRY)
552IGNORE_IOCTL(VT_GETMODE)
Christoph Hellwig644fd4f2007-05-08 00:29:07 -0700553/* Little p (/dev/rtc, /dev/envctrl, etc.) */
554COMPATIBLE_IOCTL(RTC_AIE_ON)
555COMPATIBLE_IOCTL(RTC_AIE_OFF)
556COMPATIBLE_IOCTL(RTC_UIE_ON)
557COMPATIBLE_IOCTL(RTC_UIE_OFF)
558COMPATIBLE_IOCTL(RTC_PIE_ON)
559COMPATIBLE_IOCTL(RTC_PIE_OFF)
560COMPATIBLE_IOCTL(RTC_WIE_ON)
561COMPATIBLE_IOCTL(RTC_WIE_OFF)
562COMPATIBLE_IOCTL(RTC_ALM_SET)
563COMPATIBLE_IOCTL(RTC_ALM_READ)
564COMPATIBLE_IOCTL(RTC_RD_TIME)
565COMPATIBLE_IOCTL(RTC_SET_TIME)
566COMPATIBLE_IOCTL(RTC_WKALM_SET)
567COMPATIBLE_IOCTL(RTC_WKALM_RD)
568/*
569 * These two are only for the sbus rtc driver, but
570 * hwclock tries them on every rtc device first when
571 * running on sparc. On other architectures the entries
572 * are useless but harmless.
573 */
574COMPATIBLE_IOCTL(_IOR('p', 20, int[7])) /* RTCGET */
575COMPATIBLE_IOCTL(_IOW('p', 21, int[7])) /* RTCSET */
576/* Little m */
577COMPATIBLE_IOCTL(MTIOCTOP)
578/* Socket level stuff */
579COMPATIBLE_IOCTL(FIOQSIZE)
Randy Dunlap3c3622d2008-07-16 08:52:00 -0500580#ifdef CONFIG_BLOCK
Arnd Bergmannf79f1182010-02-10 16:14:04 +0100581/* md calls this on random blockdevs */
582IGNORE_IOCTL(RAID_VERSION)
Johannes Stezenbach390192b2011-07-01 22:32:26 +0200583/* qemu/qemu-img might call these two on plain files for probing */
584IGNORE_IOCTL(CDROM_DRIVE_STATUS)
585IGNORE_IOCTL(FDGETPRM32)
Christoph Hellwig644fd4f2007-05-08 00:29:07 -0700586/* SG stuff */
587COMPATIBLE_IOCTL(SG_SET_TIMEOUT)
588COMPATIBLE_IOCTL(SG_GET_TIMEOUT)
589COMPATIBLE_IOCTL(SG_EMULATED_HOST)
Christoph Hellwig644fd4f2007-05-08 00:29:07 -0700590COMPATIBLE_IOCTL(SG_GET_TRANSFORM)
591COMPATIBLE_IOCTL(SG_SET_RESERVED_SIZE)
592COMPATIBLE_IOCTL(SG_GET_RESERVED_SIZE)
593COMPATIBLE_IOCTL(SG_GET_SCSI_ID)
594COMPATIBLE_IOCTL(SG_SET_FORCE_LOW_DMA)
595COMPATIBLE_IOCTL(SG_GET_LOW_DMA)
596COMPATIBLE_IOCTL(SG_SET_FORCE_PACK_ID)
597COMPATIBLE_IOCTL(SG_GET_PACK_ID)
598COMPATIBLE_IOCTL(SG_GET_NUM_WAITING)
599COMPATIBLE_IOCTL(SG_SET_DEBUG)
600COMPATIBLE_IOCTL(SG_GET_SG_TABLESIZE)
601COMPATIBLE_IOCTL(SG_GET_COMMAND_Q)
602COMPATIBLE_IOCTL(SG_SET_COMMAND_Q)
603COMPATIBLE_IOCTL(SG_GET_VERSION_NUM)
604COMPATIBLE_IOCTL(SG_NEXT_CMD_LEN)
605COMPATIBLE_IOCTL(SG_SCSI_RESET)
606COMPATIBLE_IOCTL(SG_GET_REQUEST_TABLE)
607COMPATIBLE_IOCTL(SG_SET_KEEP_ORPHAN)
608COMPATIBLE_IOCTL(SG_GET_KEEP_ORPHAN)
Randy Dunlap3c3622d2008-07-16 08:52:00 -0500609#endif
Christoph Hellwig644fd4f2007-05-08 00:29:07 -0700610/* PPP stuff */
611COMPATIBLE_IOCTL(PPPIOCGFLAGS)
612COMPATIBLE_IOCTL(PPPIOCSFLAGS)
613COMPATIBLE_IOCTL(PPPIOCGASYNCMAP)
614COMPATIBLE_IOCTL(PPPIOCSASYNCMAP)
615COMPATIBLE_IOCTL(PPPIOCGUNIT)
616COMPATIBLE_IOCTL(PPPIOCGRASYNCMAP)
617COMPATIBLE_IOCTL(PPPIOCSRASYNCMAP)
618COMPATIBLE_IOCTL(PPPIOCGMRU)
619COMPATIBLE_IOCTL(PPPIOCSMRU)
620COMPATIBLE_IOCTL(PPPIOCSMAXCID)
621COMPATIBLE_IOCTL(PPPIOCGXASYNCMAP)
622COMPATIBLE_IOCTL(PPPIOCSXASYNCMAP)
623COMPATIBLE_IOCTL(PPPIOCXFERUNIT)
624/* PPPIOCSCOMPRESS is translated */
625COMPATIBLE_IOCTL(PPPIOCGNPMODE)
626COMPATIBLE_IOCTL(PPPIOCSNPMODE)
627COMPATIBLE_IOCTL(PPPIOCGDEBUG)
628COMPATIBLE_IOCTL(PPPIOCSDEBUG)
629/* PPPIOCSPASS is translated */
630/* PPPIOCSACTIVE is translated */
631/* PPPIOCGIDLE is translated */
632COMPATIBLE_IOCTL(PPPIOCNEWUNIT)
633COMPATIBLE_IOCTL(PPPIOCATTACH)
634COMPATIBLE_IOCTL(PPPIOCDETACH)
635COMPATIBLE_IOCTL(PPPIOCSMRRU)
636COMPATIBLE_IOCTL(PPPIOCCONNECT)
637COMPATIBLE_IOCTL(PPPIOCDISCONN)
638COMPATIBLE_IOCTL(PPPIOCATTCHAN)
639COMPATIBLE_IOCTL(PPPIOCGCHAN)
Florian Westphal8bab6f12011-08-06 12:12:04 +0000640COMPATIBLE_IOCTL(PPPIOCGL2TPSTATS)
Christoph Hellwig644fd4f2007-05-08 00:29:07 -0700641/* PPPOX */
642COMPATIBLE_IOCTL(PPPOEIOCSFWD)
643COMPATIBLE_IOCTL(PPPOEIOCDFWD)
Christoph Hellwig644fd4f2007-05-08 00:29:07 -0700644/* Big A */
645/* sparc only */
646/* Big Q for sound/OSS */
647COMPATIBLE_IOCTL(SNDCTL_SEQ_RESET)
648COMPATIBLE_IOCTL(SNDCTL_SEQ_SYNC)
649COMPATIBLE_IOCTL(SNDCTL_SYNTH_INFO)
650COMPATIBLE_IOCTL(SNDCTL_SEQ_CTRLRATE)
651COMPATIBLE_IOCTL(SNDCTL_SEQ_GETOUTCOUNT)
652COMPATIBLE_IOCTL(SNDCTL_SEQ_GETINCOUNT)
653COMPATIBLE_IOCTL(SNDCTL_SEQ_PERCMODE)
654COMPATIBLE_IOCTL(SNDCTL_FM_LOAD_INSTR)
655COMPATIBLE_IOCTL(SNDCTL_SEQ_TESTMIDI)
656COMPATIBLE_IOCTL(SNDCTL_SEQ_RESETSAMPLES)
657COMPATIBLE_IOCTL(SNDCTL_SEQ_NRSYNTHS)
658COMPATIBLE_IOCTL(SNDCTL_SEQ_NRMIDIS)
659COMPATIBLE_IOCTL(SNDCTL_MIDI_INFO)
660COMPATIBLE_IOCTL(SNDCTL_SEQ_THRESHOLD)
661COMPATIBLE_IOCTL(SNDCTL_SYNTH_MEMAVL)
662COMPATIBLE_IOCTL(SNDCTL_FM_4OP_ENABLE)
663COMPATIBLE_IOCTL(SNDCTL_SEQ_PANIC)
664COMPATIBLE_IOCTL(SNDCTL_SEQ_OUTOFBAND)
665COMPATIBLE_IOCTL(SNDCTL_SEQ_GETTIME)
666COMPATIBLE_IOCTL(SNDCTL_SYNTH_ID)
667COMPATIBLE_IOCTL(SNDCTL_SYNTH_CONTROL)
668COMPATIBLE_IOCTL(SNDCTL_SYNTH_REMOVESAMPLE)
669/* Big T for sound/OSS */
670COMPATIBLE_IOCTL(SNDCTL_TMR_TIMEBASE)
671COMPATIBLE_IOCTL(SNDCTL_TMR_START)
672COMPATIBLE_IOCTL(SNDCTL_TMR_STOP)
673COMPATIBLE_IOCTL(SNDCTL_TMR_CONTINUE)
674COMPATIBLE_IOCTL(SNDCTL_TMR_TEMPO)
675COMPATIBLE_IOCTL(SNDCTL_TMR_SOURCE)
676COMPATIBLE_IOCTL(SNDCTL_TMR_METRONOME)
677COMPATIBLE_IOCTL(SNDCTL_TMR_SELECT)
678/* Little m for sound/OSS */
679COMPATIBLE_IOCTL(SNDCTL_MIDI_PRETIME)
680COMPATIBLE_IOCTL(SNDCTL_MIDI_MPUMODE)
681COMPATIBLE_IOCTL(SNDCTL_MIDI_MPUCMD)
682/* Big P for sound/OSS */
683COMPATIBLE_IOCTL(SNDCTL_DSP_RESET)
684COMPATIBLE_IOCTL(SNDCTL_DSP_SYNC)
685COMPATIBLE_IOCTL(SNDCTL_DSP_SPEED)
686COMPATIBLE_IOCTL(SNDCTL_DSP_STEREO)
687COMPATIBLE_IOCTL(SNDCTL_DSP_GETBLKSIZE)
688COMPATIBLE_IOCTL(SNDCTL_DSP_CHANNELS)
689COMPATIBLE_IOCTL(SOUND_PCM_WRITE_FILTER)
690COMPATIBLE_IOCTL(SNDCTL_DSP_POST)
691COMPATIBLE_IOCTL(SNDCTL_DSP_SUBDIVIDE)
692COMPATIBLE_IOCTL(SNDCTL_DSP_SETFRAGMENT)
693COMPATIBLE_IOCTL(SNDCTL_DSP_GETFMTS)
694COMPATIBLE_IOCTL(SNDCTL_DSP_SETFMT)
695COMPATIBLE_IOCTL(SNDCTL_DSP_GETOSPACE)
696COMPATIBLE_IOCTL(SNDCTL_DSP_GETISPACE)
697COMPATIBLE_IOCTL(SNDCTL_DSP_NONBLOCK)
698COMPATIBLE_IOCTL(SNDCTL_DSP_GETCAPS)
699COMPATIBLE_IOCTL(SNDCTL_DSP_GETTRIGGER)
700COMPATIBLE_IOCTL(SNDCTL_DSP_SETTRIGGER)
701COMPATIBLE_IOCTL(SNDCTL_DSP_GETIPTR)
702COMPATIBLE_IOCTL(SNDCTL_DSP_GETOPTR)
703/* SNDCTL_DSP_MAPINBUF, XXX needs translation */
704/* SNDCTL_DSP_MAPOUTBUF, XXX needs translation */
705COMPATIBLE_IOCTL(SNDCTL_DSP_SETSYNCRO)
706COMPATIBLE_IOCTL(SNDCTL_DSP_SETDUPLEX)
707COMPATIBLE_IOCTL(SNDCTL_DSP_GETODELAY)
708COMPATIBLE_IOCTL(SNDCTL_DSP_PROFILE)
709COMPATIBLE_IOCTL(SOUND_PCM_READ_RATE)
710COMPATIBLE_IOCTL(SOUND_PCM_READ_CHANNELS)
711COMPATIBLE_IOCTL(SOUND_PCM_READ_BITS)
712COMPATIBLE_IOCTL(SOUND_PCM_READ_FILTER)
713/* Big C for sound/OSS */
714COMPATIBLE_IOCTL(SNDCTL_COPR_RESET)
715COMPATIBLE_IOCTL(SNDCTL_COPR_LOAD)
716COMPATIBLE_IOCTL(SNDCTL_COPR_RDATA)
717COMPATIBLE_IOCTL(SNDCTL_COPR_RCODE)
718COMPATIBLE_IOCTL(SNDCTL_COPR_WDATA)
719COMPATIBLE_IOCTL(SNDCTL_COPR_WCODE)
720COMPATIBLE_IOCTL(SNDCTL_COPR_RUN)
721COMPATIBLE_IOCTL(SNDCTL_COPR_HALT)
722COMPATIBLE_IOCTL(SNDCTL_COPR_SENDMSG)
723COMPATIBLE_IOCTL(SNDCTL_COPR_RCVMSG)
724/* Big M for sound/OSS */
725COMPATIBLE_IOCTL(SOUND_MIXER_READ_VOLUME)
726COMPATIBLE_IOCTL(SOUND_MIXER_READ_BASS)
727COMPATIBLE_IOCTL(SOUND_MIXER_READ_TREBLE)
728COMPATIBLE_IOCTL(SOUND_MIXER_READ_SYNTH)
729COMPATIBLE_IOCTL(SOUND_MIXER_READ_PCM)
730COMPATIBLE_IOCTL(SOUND_MIXER_READ_SPEAKER)
731COMPATIBLE_IOCTL(SOUND_MIXER_READ_LINE)
732COMPATIBLE_IOCTL(SOUND_MIXER_READ_MIC)
733COMPATIBLE_IOCTL(SOUND_MIXER_READ_CD)
734COMPATIBLE_IOCTL(SOUND_MIXER_READ_IMIX)
735COMPATIBLE_IOCTL(SOUND_MIXER_READ_ALTPCM)
736COMPATIBLE_IOCTL(SOUND_MIXER_READ_RECLEV)
737COMPATIBLE_IOCTL(SOUND_MIXER_READ_IGAIN)
738COMPATIBLE_IOCTL(SOUND_MIXER_READ_OGAIN)
739COMPATIBLE_IOCTL(SOUND_MIXER_READ_LINE1)
740COMPATIBLE_IOCTL(SOUND_MIXER_READ_LINE2)
741COMPATIBLE_IOCTL(SOUND_MIXER_READ_LINE3)
742COMPATIBLE_IOCTL(MIXER_READ(SOUND_MIXER_DIGITAL1))
743COMPATIBLE_IOCTL(MIXER_READ(SOUND_MIXER_DIGITAL2))
744COMPATIBLE_IOCTL(MIXER_READ(SOUND_MIXER_DIGITAL3))
745COMPATIBLE_IOCTL(MIXER_READ(SOUND_MIXER_PHONEIN))
746COMPATIBLE_IOCTL(MIXER_READ(SOUND_MIXER_PHONEOUT))
747COMPATIBLE_IOCTL(MIXER_READ(SOUND_MIXER_VIDEO))
748COMPATIBLE_IOCTL(MIXER_READ(SOUND_MIXER_RADIO))
749COMPATIBLE_IOCTL(MIXER_READ(SOUND_MIXER_MONITOR))
750COMPATIBLE_IOCTL(SOUND_MIXER_READ_MUTE)
751/* SOUND_MIXER_READ_ENHANCE, same value as READ_MUTE */
752/* SOUND_MIXER_READ_LOUD, same value as READ_MUTE */
753COMPATIBLE_IOCTL(SOUND_MIXER_READ_RECSRC)
754COMPATIBLE_IOCTL(SOUND_MIXER_READ_DEVMASK)
755COMPATIBLE_IOCTL(SOUND_MIXER_READ_RECMASK)
756COMPATIBLE_IOCTL(SOUND_MIXER_READ_STEREODEVS)
757COMPATIBLE_IOCTL(SOUND_MIXER_READ_CAPS)
758COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_VOLUME)
759COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_BASS)
760COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_TREBLE)
761COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_SYNTH)
762COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_PCM)
763COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_SPEAKER)
764COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_LINE)
765COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_MIC)
766COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_CD)
767COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_IMIX)
768COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_ALTPCM)
769COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_RECLEV)
770COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_IGAIN)
771COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_OGAIN)
772COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_LINE1)
773COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_LINE2)
774COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_LINE3)
775COMPATIBLE_IOCTL(MIXER_WRITE(SOUND_MIXER_DIGITAL1))
776COMPATIBLE_IOCTL(MIXER_WRITE(SOUND_MIXER_DIGITAL2))
777COMPATIBLE_IOCTL(MIXER_WRITE(SOUND_MIXER_DIGITAL3))
778COMPATIBLE_IOCTL(MIXER_WRITE(SOUND_MIXER_PHONEIN))
779COMPATIBLE_IOCTL(MIXER_WRITE(SOUND_MIXER_PHONEOUT))
780COMPATIBLE_IOCTL(MIXER_WRITE(SOUND_MIXER_VIDEO))
781COMPATIBLE_IOCTL(MIXER_WRITE(SOUND_MIXER_RADIO))
782COMPATIBLE_IOCTL(MIXER_WRITE(SOUND_MIXER_MONITOR))
783COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_MUTE)
784/* SOUND_MIXER_WRITE_ENHANCE, same value as WRITE_MUTE */
785/* SOUND_MIXER_WRITE_LOUD, same value as WRITE_MUTE */
786COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_RECSRC)
787COMPATIBLE_IOCTL(SOUND_MIXER_INFO)
788COMPATIBLE_IOCTL(SOUND_OLD_MIXER_INFO)
789COMPATIBLE_IOCTL(SOUND_MIXER_ACCESS)
790COMPATIBLE_IOCTL(SOUND_MIXER_AGC)
791COMPATIBLE_IOCTL(SOUND_MIXER_3DSE)
792COMPATIBLE_IOCTL(SOUND_MIXER_PRIVATE1)
793COMPATIBLE_IOCTL(SOUND_MIXER_PRIVATE2)
794COMPATIBLE_IOCTL(SOUND_MIXER_PRIVATE3)
795COMPATIBLE_IOCTL(SOUND_MIXER_PRIVATE4)
796COMPATIBLE_IOCTL(SOUND_MIXER_PRIVATE5)
797COMPATIBLE_IOCTL(SOUND_MIXER_GETLEVELS)
798COMPATIBLE_IOCTL(SOUND_MIXER_SETLEVELS)
799COMPATIBLE_IOCTL(OSS_GETVERSION)
Christoph Hellwig644fd4f2007-05-08 00:29:07 -0700800/* Raw devices */
801COMPATIBLE_IOCTL(RAW_SETBIND)
802COMPATIBLE_IOCTL(RAW_GETBIND)
Christoph Hellwig644fd4f2007-05-08 00:29:07 -0700803/* Watchdog */
804COMPATIBLE_IOCTL(WDIOC_GETSUPPORT)
805COMPATIBLE_IOCTL(WDIOC_GETSTATUS)
806COMPATIBLE_IOCTL(WDIOC_GETBOOTSTATUS)
807COMPATIBLE_IOCTL(WDIOC_GETTEMP)
808COMPATIBLE_IOCTL(WDIOC_SETOPTIONS)
809COMPATIBLE_IOCTL(WDIOC_KEEPALIVE)
810COMPATIBLE_IOCTL(WDIOC_SETTIMEOUT)
811COMPATIBLE_IOCTL(WDIOC_GETTIMEOUT)
Wolfram Sang97beb3a2016-08-31 14:52:42 +0300812COMPATIBLE_IOCTL(WDIOC_SETPRETIMEOUT)
813COMPATIBLE_IOCTL(WDIOC_GETPRETIMEOUT)
Christoph Hellwig644fd4f2007-05-08 00:29:07 -0700814/* Big R */
815COMPATIBLE_IOCTL(RNDGETENTCNT)
816COMPATIBLE_IOCTL(RNDADDTOENTCNT)
817COMPATIBLE_IOCTL(RNDGETPOOL)
818COMPATIBLE_IOCTL(RNDADDENTROPY)
819COMPATIBLE_IOCTL(RNDZAPENTCNT)
820COMPATIBLE_IOCTL(RNDCLEARPOOL)
821/* Bluetooth */
822COMPATIBLE_IOCTL(HCIDEVUP)
823COMPATIBLE_IOCTL(HCIDEVDOWN)
824COMPATIBLE_IOCTL(HCIDEVRESET)
825COMPATIBLE_IOCTL(HCIDEVRESTAT)
826COMPATIBLE_IOCTL(HCIGETDEVLIST)
827COMPATIBLE_IOCTL(HCIGETDEVINFO)
828COMPATIBLE_IOCTL(HCIGETCONNLIST)
829COMPATIBLE_IOCTL(HCIGETCONNINFO)
Marcel Holtmann40be4922008-07-14 20:13:50 +0200830COMPATIBLE_IOCTL(HCIGETAUTHINFO)
Christoph Hellwig644fd4f2007-05-08 00:29:07 -0700831COMPATIBLE_IOCTL(HCISETRAW)
832COMPATIBLE_IOCTL(HCISETSCAN)
833COMPATIBLE_IOCTL(HCISETAUTH)
834COMPATIBLE_IOCTL(HCISETENCRYPT)
835COMPATIBLE_IOCTL(HCISETPTYPE)
836COMPATIBLE_IOCTL(HCISETLINKPOL)
837COMPATIBLE_IOCTL(HCISETLINKMODE)
838COMPATIBLE_IOCTL(HCISETACLMTU)
839COMPATIBLE_IOCTL(HCISETSCOMTU)
Johan Hedbergf0358562010-05-18 13:20:32 +0200840COMPATIBLE_IOCTL(HCIBLOCKADDR)
841COMPATIBLE_IOCTL(HCIUNBLOCKADDR)
Christoph Hellwig644fd4f2007-05-08 00:29:07 -0700842COMPATIBLE_IOCTL(HCIINQUIRY)
843COMPATIBLE_IOCTL(HCIUARTSETPROTO)
844COMPATIBLE_IOCTL(HCIUARTGETPROTO)
Marcel Holtmannd10d34a2016-01-25 08:44:03 +0100845COMPATIBLE_IOCTL(HCIUARTGETDEVICE)
846COMPATIBLE_IOCTL(HCIUARTSETFLAGS)
847COMPATIBLE_IOCTL(HCIUARTGETFLAGS)
Christoph Hellwig644fd4f2007-05-08 00:29:07 -0700848COMPATIBLE_IOCTL(RFCOMMCREATEDEV)
849COMPATIBLE_IOCTL(RFCOMMRELEASEDEV)
850COMPATIBLE_IOCTL(RFCOMMGETDEVLIST)
851COMPATIBLE_IOCTL(RFCOMMGETDEVINFO)
852COMPATIBLE_IOCTL(RFCOMMSTEALDLC)
Christoph Hellwig644fd4f2007-05-08 00:29:07 -0700853/* CAPI */
854COMPATIBLE_IOCTL(CAPI_REGISTER)
855COMPATIBLE_IOCTL(CAPI_GET_MANUFACTURER)
856COMPATIBLE_IOCTL(CAPI_GET_VERSION)
857COMPATIBLE_IOCTL(CAPI_GET_SERIAL)
858COMPATIBLE_IOCTL(CAPI_GET_PROFILE)
859COMPATIBLE_IOCTL(CAPI_MANUFACTURER_CMD)
860COMPATIBLE_IOCTL(CAPI_GET_ERRCODE)
861COMPATIBLE_IOCTL(CAPI_INSTALLED)
862COMPATIBLE_IOCTL(CAPI_GET_FLAGS)
863COMPATIBLE_IOCTL(CAPI_SET_FLAGS)
864COMPATIBLE_IOCTL(CAPI_CLR_FLAGS)
865COMPATIBLE_IOCTL(CAPI_NCCI_OPENCOUNT)
866COMPATIBLE_IOCTL(CAPI_NCCI_GETUNIT)
Christoph Hellwig644fd4f2007-05-08 00:29:07 -0700867/* Misc. */
868COMPATIBLE_IOCTL(0x41545900) /* ATYIO_CLKR */
869COMPATIBLE_IOCTL(0x41545901) /* ATYIO_CLKW */
870COMPATIBLE_IOCTL(PCIIOC_CONTROLLER)
871COMPATIBLE_IOCTL(PCIIOC_MMAP_IS_IO)
872COMPATIBLE_IOCTL(PCIIOC_MMAP_IS_MEM)
873COMPATIBLE_IOCTL(PCIIOC_WRITE_COMBINE)
Christoph Hellwig644fd4f2007-05-08 00:29:07 -0700874/* hiddev */
875COMPATIBLE_IOCTL(HIDIOCGVERSION)
876COMPATIBLE_IOCTL(HIDIOCAPPLICATION)
877COMPATIBLE_IOCTL(HIDIOCGDEVINFO)
878COMPATIBLE_IOCTL(HIDIOCGSTRING)
879COMPATIBLE_IOCTL(HIDIOCINITREPORT)
880COMPATIBLE_IOCTL(HIDIOCGREPORT)
881COMPATIBLE_IOCTL(HIDIOCSREPORT)
882COMPATIBLE_IOCTL(HIDIOCGREPORTINFO)
883COMPATIBLE_IOCTL(HIDIOCGFIELDINFO)
884COMPATIBLE_IOCTL(HIDIOCGUSAGE)
885COMPATIBLE_IOCTL(HIDIOCSUSAGE)
886COMPATIBLE_IOCTL(HIDIOCGUCODE)
887COMPATIBLE_IOCTL(HIDIOCGFLAG)
888COMPATIBLE_IOCTL(HIDIOCSFLAG)
889COMPATIBLE_IOCTL(HIDIOCGCOLLECTIONINDEX)
890COMPATIBLE_IOCTL(HIDIOCGCOLLECTIONINFO)
Akos Maroy3fee37c2008-01-06 11:15:55 +0100891/* joystick */
892COMPATIBLE_IOCTL(JSIOCGVERSION)
893COMPATIBLE_IOCTL(JSIOCGAXES)
894COMPATIBLE_IOCTL(JSIOCGBUTTONS)
895COMPATIBLE_IOCTL(JSIOCGNAME(0))
896
Andi Kleen2724b6d2007-05-02 19:27:21 +0200897/* fat 'r' ioctls. These are handled by fat with ->compat_ioctl,
898 but we don't want warnings on other file systems. So declare
899 them as compatible here. */
900#define VFAT_IOCTL_READDIR_BOTH32 _IOR('r', 1, struct compat_dirent[2])
901#define VFAT_IOCTL_READDIR_SHORT32 _IOR('r', 2, struct compat_dirent[2])
902
903IGNORE_IOCTL(VFAT_IOCTL_READDIR_BOTH32)
904IGNORE_IOCTL(VFAT_IOCTL_READDIR_SHORT32)
Geert Uytterhoeven98701dc2007-07-15 23:42:01 -0700905
David S. Miller81639042007-07-29 00:50:42 -0700906#ifdef CONFIG_SPARC
907/* Sparc framebuffers, handled in sbusfb_compat_ioctl() */
908IGNORE_IOCTL(FBIOGTYPE)
909IGNORE_IOCTL(FBIOSATTR)
910IGNORE_IOCTL(FBIOGATTR)
911IGNORE_IOCTL(FBIOSVIDEO)
912IGNORE_IOCTL(FBIOGVIDEO)
913IGNORE_IOCTL(FBIOSCURPOS)
914IGNORE_IOCTL(FBIOGCURPOS)
915IGNORE_IOCTL(FBIOGCURMAX)
916IGNORE_IOCTL(FBIOPUTCMAP32)
917IGNORE_IOCTL(FBIOGETCMAP32)
918IGNORE_IOCTL(FBIOSCURSOR32)
919IGNORE_IOCTL(FBIOGCURSOR32)
920#endif
Christoph Hellwige6a6d2e2006-01-09 20:52:14 -0800921};
David S. Miller6e87abd2005-11-16 00:52:57 -0800922
Arnd Bergmann5a07ea02009-05-21 22:04:16 +0000923/*
924 * Convert common ioctl arguments based on their command number
925 *
926 * Please do not add any code in here. Instead, implement
927 * a compat_ioctl operation in the place that handleѕ the
928 * ioctl for the native case.
929 */
Al Viro66cf1912016-01-07 09:53:30 -0500930static long do_ioctl_trans(unsigned int cmd,
Arnd Bergmann5a07ea02009-05-21 22:04:16 +0000931 unsigned long arg, struct file *file)
932{
Arnd Bergmann43c6e7b92009-11-14 23:16:18 +0100933 void __user *argp = compat_ptr(arg);
934
Arnd Bergmann5a07ea02009-05-21 22:04:16 +0000935 switch (cmd) {
936 case PPPIOCGIDLE32:
Al Viro66cf1912016-01-07 09:53:30 -0500937 return ppp_gidle(file, cmd, argp);
Arnd Bergmann5a07ea02009-05-21 22:04:16 +0000938 case PPPIOCSCOMPRESS32:
Al Viro66cf1912016-01-07 09:53:30 -0500939 return ppp_scompress(file, cmd, argp);
Arnd Bergmann5a07ea02009-05-21 22:04:16 +0000940 case PPPIOCSPASS32:
941 case PPPIOCSACTIVE32:
Al Viro66cf1912016-01-07 09:53:30 -0500942 return ppp_sock_fprog_ioctl_trans(file, cmd, argp);
Arnd Bergmann5a07ea02009-05-21 22:04:16 +0000943#ifdef CONFIG_BLOCK
944 case SG_IO:
Al Viro66cf1912016-01-07 09:53:30 -0500945 return sg_ioctl_trans(file, cmd, argp);
Arnd Bergmann5a07ea02009-05-21 22:04:16 +0000946 case SG_GET_REQUEST_TABLE:
Al Viro66cf1912016-01-07 09:53:30 -0500947 return sg_grt_trans(file, cmd, argp);
Arnd Bergmann5a07ea02009-05-21 22:04:16 +0000948 case MTIOCGET32:
949 case MTIOCPOS32:
Al Viro66cf1912016-01-07 09:53:30 -0500950 return mt_ioctl_trans(file, cmd, argp);
Arnd Bergmann5a07ea02009-05-21 22:04:16 +0000951#endif
Arnd Bergmann5a07ea02009-05-21 22:04:16 +0000952 /* Not implemented in the native kernel */
953 case RTC_IRQP_READ32:
954 case RTC_IRQP_SET32:
955 case RTC_EPOCH_READ32:
956 case RTC_EPOCH_SET32:
Al Viro66cf1912016-01-07 09:53:30 -0500957 return rtc_ioctl(file, cmd, argp);
Arnd Bergmann5a07ea02009-05-21 22:04:16 +0000958 }
Arnd Bergmann789f0f82009-11-05 19:13:51 +0100959
960 /*
961 * These take an integer instead of a pointer as 'arg',
962 * so we must not do a compat_ptr() translation.
963 */
964 switch (cmd) {
Arnd Bergmann789f0f82009-11-05 19:13:51 +0100965 /* RAID */
966 case HOT_REMOVE_DISK:
967 case HOT_ADD_DISK:
968 case SET_DISK_FAULTY:
969 case SET_BITMAP_FILE:
Al Viro66cf1912016-01-07 09:53:30 -0500970 return vfs_ioctl(file, cmd, arg);
Arnd Bergmann789f0f82009-11-05 19:13:51 +0100971 }
972
Arnd Bergmann5a07ea02009-05-21 22:04:16 +0000973 return -ENOIOCTLCMD;
974}
975
Arnd Bergmann661f6272009-11-05 19:52:55 +0100976static int compat_ioctl_check_table(unsigned int xcmd)
977{
978 int i;
979 const int max = ARRAY_SIZE(ioctl_pointer) - 1;
980
981 BUILD_BUG_ON(max >= (1 << 16));
982
983 /* guess initial offset into table, assuming a
984 normalized distribution */
985 i = ((xcmd >> 16) * max) >> 16;
986
987 /* do linear search up first, until greater or equal */
988 while (ioctl_pointer[i] < xcmd && i < max)
989 i++;
990
991 /* then do linear search down */
992 while (ioctl_pointer[i] > xcmd && i > 0)
993 i--;
994
995 return ioctl_pointer[i] == xcmd;
996}
997
Heiko Carstens932602e2014-03-04 16:07:52 +0100998COMPAT_SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd,
999 compat_ulong_t, arg32)
Christoph Hellwig6272e262007-05-08 00:29:21 -07001000{
Heiko Carstens932602e2014-03-04 16:07:52 +01001001 unsigned long arg = arg32;
Al Viro2903ff02012-08-28 12:52:22 -04001002 struct fd f = fdget(fd);
Christoph Hellwig6272e262007-05-08 00:29:21 -07001003 int error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001004 if (!f.file)
Christoph Hellwig6272e262007-05-08 00:29:21 -07001005 goto out;
1006
1007 /* RED-PEN how should LSM module know it's handling 32bit? */
Al Viro2903ff02012-08-28 12:52:22 -04001008 error = security_file_ioctl(f.file, cmd, arg);
Christoph Hellwig6272e262007-05-08 00:29:21 -07001009 if (error)
1010 goto out_fput;
1011
1012 /*
1013 * To allow the compat_ioctl handlers to be self contained
1014 * we need to check the common ioctls here first.
1015 * Just handle them with the standard handlers below.
1016 */
1017 switch (cmd) {
1018 case FIOCLEX:
1019 case FIONCLEX:
1020 case FIONBIO:
1021 case FIOASYNC:
1022 case FIOQSIZE:
1023 break;
1024
Ankit Jain3e63cbb2009-06-19 14:28:07 -04001025#if defined(CONFIG_IA64) || defined(CONFIG_X86_64)
1026 case FS_IOC_RESVSP_32:
1027 case FS_IOC_RESVSP64_32:
Al Viro2903ff02012-08-28 12:52:22 -04001028 error = compat_ioctl_preallocate(f.file, compat_ptr(arg));
Ankit Jain3e63cbb2009-06-19 14:28:07 -04001029 goto out_fput;
1030#else
1031 case FS_IOC_RESVSP:
1032 case FS_IOC_RESVSP64:
Al Viro2903ff02012-08-28 12:52:22 -04001033 error = ioctl_preallocate(f.file, compat_ptr(arg));
Ankit Jain3e63cbb2009-06-19 14:28:07 -04001034 goto out_fput;
1035#endif
1036
Darrick J. Wongd79bdd52015-12-19 00:55:52 -08001037 case FICLONE:
1038 case FICLONERANGE:
Darrick J. Wong54dbc152015-12-19 00:55:59 -08001039 case FIDEDUPERANGE:
Josef Bacikceac2042017-09-29 15:24:06 -04001040 case FS_IOC_FIEMAP:
Darrick J. Wongd79bdd52015-12-19 00:55:52 -08001041 goto do_ioctl;
1042
Christoph Hellwig6272e262007-05-08 00:29:21 -07001043 case FIBMAP:
1044 case FIGETBSZ:
1045 case FIONREAD:
Al Viro496ad9a2013-01-23 17:07:38 -05001046 if (S_ISREG(file_inode(f.file)->i_mode))
Christoph Hellwig6272e262007-05-08 00:29:21 -07001047 break;
1048 /*FALL THROUGH*/
1049
1050 default:
Al Viro72c2d532013-09-22 16:27:52 -04001051 if (f.file->f_op->compat_ioctl) {
Al Viro2903ff02012-08-28 12:52:22 -04001052 error = f.file->f_op->compat_ioctl(f.file, cmd, arg);
Christoph Hellwig6272e262007-05-08 00:29:21 -07001053 if (error != -ENOIOCTLCMD)
1054 goto out_fput;
1055 }
1056
Al Viro72c2d532013-09-22 16:27:52 -04001057 if (!f.file->f_op->unlocked_ioctl)
Christoph Hellwig6272e262007-05-08 00:29:21 -07001058 goto do_ioctl;
1059 break;
1060 }
1061
Arnd Bergmann661f6272009-11-05 19:52:55 +01001062 if (compat_ioctl_check_table(XFORM(cmd)))
1063 goto found_handler;
Christoph Hellwig6272e262007-05-08 00:29:21 -07001064
Al Viro66cf1912016-01-07 09:53:30 -05001065 error = do_ioctl_trans(cmd, arg, f.file);
Linus Torvalds07d106d2012-01-05 15:40:12 -08001066 if (error == -ENOIOCTLCMD)
1067 error = -ENOTTY;
Christoph Hellwig6272e262007-05-08 00:29:21 -07001068
1069 goto out_fput;
1070
1071 found_handler:
Arnd Bergmann789f0f82009-11-05 19:13:51 +01001072 arg = (unsigned long)compat_ptr(arg);
Christoph Hellwig6272e262007-05-08 00:29:21 -07001073 do_ioctl:
Al Viro2903ff02012-08-28 12:52:22 -04001074 error = do_vfs_ioctl(f.file, fd, cmd, arg);
Christoph Hellwig6272e262007-05-08 00:29:21 -07001075 out_fput:
Al Viro2903ff02012-08-28 12:52:22 -04001076 fdput(f);
Christoph Hellwig6272e262007-05-08 00:29:21 -07001077 out:
1078 return error;
1079}
1080
Arnd Bergmann661f6272009-11-05 19:52:55 +01001081static int __init init_sys32_ioctl_cmp(const void *p, const void *q)
Christoph Hellwig6272e262007-05-08 00:29:21 -07001082{
Arnd Bergmann661f6272009-11-05 19:52:55 +01001083 unsigned int a, b;
1084 a = *(unsigned int *)p;
1085 b = *(unsigned int *)q;
1086 if (a > b)
1087 return 1;
1088 if (a < b)
1089 return -1;
1090 return 0;
Christoph Hellwig6272e262007-05-08 00:29:21 -07001091}
1092
1093static int __init init_sys32_ioctl(void)
1094{
Arnd Bergmann661f6272009-11-05 19:52:55 +01001095 sort(ioctl_pointer, ARRAY_SIZE(ioctl_pointer), sizeof(*ioctl_pointer),
1096 init_sys32_ioctl_cmp, NULL);
Christoph Hellwig6272e262007-05-08 00:29:21 -07001097 return 0;
1098}
1099__initcall(init_sys32_ioctl);