blob: 377e560ab5c98bb99cc5d83340082c926b2c39de [file] [log] [blame]
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 32bit Socket syscall emulation. Based on arch/sparc64/kernel/sys_sparc32.c.
3 *
4 * Copyright (C) 2000 VA Linux Co
5 * Copyright (C) 2000 Don Dugger <n0ano@valinux.com>
6 * Copyright (C) 1999 Arun Sharma <arun.sharma@intel.com>
7 * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
8 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
9 * Copyright (C) 2000 Hewlett-Packard Co.
10 * Copyright (C) 2000 David Mosberger-Tang <davidm@hpl.hp.com>
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +090011 * Copyright (C) 2000,2001 Andi Kleen, SuSE Labs
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
14#include <linux/kernel.h>
15#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/types.h>
17#include <linux/file.h>
18#include <linux/icmpv6.h>
19#include <linux/socket.h>
20#include <linux/syscalls.h>
21#include <linux/filter.h>
22#include <linux/compat.h>
23#include <linux/netfilter_ipv4/ip_tables.h>
24#include <linux/security.h>
25
26#include <net/scm.h>
27#include <net/sock.h>
28#include <asm/uaccess.h>
29#include <net/compat.h>
30
31static inline int iov_from_user_compat_to_kern(struct iovec *kiov,
32 struct compat_iovec __user *uiov32,
33 int niov)
34{
35 int tot_len = 0;
36
Stephen Hemmingere71a4782007-04-10 20:10:33 -070037 while (niov > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 compat_uptr_t buf;
39 compat_size_t len;
40
Stephen Hemmingere71a4782007-04-10 20:10:33 -070041 if (get_user(len, &uiov32->iov_len) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 get_user(buf, &uiov32->iov_base)) {
43 tot_len = -EFAULT;
44 break;
45 }
46 tot_len += len;
47 kiov->iov_base = compat_ptr(buf);
48 kiov->iov_len = (__kernel_size_t) len;
49 uiov32++;
50 kiov++;
51 niov--;
52 }
53 return tot_len;
54}
55
56int get_compat_msghdr(struct msghdr *kmsg, struct compat_msghdr __user *umsg)
57{
58 compat_uptr_t tmp1, tmp2, tmp3;
59
60 if (!access_ok(VERIFY_READ, umsg, sizeof(*umsg)) ||
61 __get_user(tmp1, &umsg->msg_name) ||
62 __get_user(kmsg->msg_namelen, &umsg->msg_namelen) ||
63 __get_user(tmp2, &umsg->msg_iov) ||
64 __get_user(kmsg->msg_iovlen, &umsg->msg_iovlen) ||
65 __get_user(tmp3, &umsg->msg_control) ||
66 __get_user(kmsg->msg_controllen, &umsg->msg_controllen) ||
67 __get_user(kmsg->msg_flags, &umsg->msg_flags))
68 return -EFAULT;
69 kmsg->msg_name = compat_ptr(tmp1);
70 kmsg->msg_iov = compat_ptr(tmp2);
71 kmsg->msg_control = compat_ptr(tmp3);
72 return 0;
73}
74
75/* I've named the args so it is easy to tell whose space the pointers are in. */
76int verify_compat_iovec(struct msghdr *kern_msg, struct iovec *kern_iov,
77 char *kern_address, int mode)
78{
79 int tot_len;
80
Stephen Hemmingere71a4782007-04-10 20:10:33 -070081 if (kern_msg->msg_namelen) {
82 if (mode==VERIFY_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 int err = move_addr_to_kernel(kern_msg->msg_name,
84 kern_msg->msg_namelen,
85 kern_address);
Stephen Hemmingere71a4782007-04-10 20:10:33 -070086 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return err;
88 }
89 kern_msg->msg_name = kern_address;
90 } else
91 kern_msg->msg_name = NULL;
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 tot_len = iov_from_user_compat_to_kern(kern_iov,
94 (struct compat_iovec __user *)kern_msg->msg_iov,
95 kern_msg->msg_iovlen);
Stephen Hemmingere71a4782007-04-10 20:10:33 -070096 if (tot_len >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 kern_msg->msg_iov = kern_iov;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 return tot_len;
100}
101
102/* Bleech... */
103#define CMSG_COMPAT_ALIGN(len) ALIGN((len), sizeof(s32))
104
105#define CMSG_COMPAT_DATA(cmsg) \
106 ((void __user *)((char __user *)(cmsg) + CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr))))
107#define CMSG_COMPAT_SPACE(len) \
108 (CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)) + CMSG_COMPAT_ALIGN(len))
109#define CMSG_COMPAT_LEN(len) \
110 (CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)) + (len))
111
112#define CMSG_COMPAT_FIRSTHDR(msg) \
113 (((msg)->msg_controllen) >= sizeof(struct compat_cmsghdr) ? \
114 (struct compat_cmsghdr __user *)((msg)->msg_control) : \
115 (struct compat_cmsghdr __user *)NULL)
116
117#define CMSG_COMPAT_OK(ucmlen, ucmsg, mhdr) \
118 ((ucmlen) >= sizeof(struct compat_cmsghdr) && \
119 (ucmlen) <= (unsigned long) \
120 ((mhdr)->msg_controllen - \
121 ((char *)(ucmsg) - (char *)(mhdr)->msg_control)))
122
123static inline struct compat_cmsghdr __user *cmsg_compat_nxthdr(struct msghdr *msg,
124 struct compat_cmsghdr __user *cmsg, int cmsg_len)
125{
126 char __user *ptr = (char __user *)cmsg + CMSG_COMPAT_ALIGN(cmsg_len);
127 if ((unsigned long)(ptr + 1 - (char __user *)msg->msg_control) >
128 msg->msg_controllen)
129 return NULL;
130 return (struct compat_cmsghdr __user *)ptr;
131}
132
133/* There is a lot of hair here because the alignment rules (and
134 * thus placement) of cmsg headers and length are different for
135 * 32-bit apps. -DaveM
136 */
Al Viro8920e8f2005-09-07 18:28:51 -0700137int cmsghdr_from_user_compat_to_kern(struct msghdr *kmsg, struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 unsigned char *stackbuf, int stackbuf_size)
139{
140 struct compat_cmsghdr __user *ucmsg;
141 struct cmsghdr *kcmsg, *kcmsg_base;
142 compat_size_t ucmlen;
143 __kernel_size_t kcmlen, tmp;
Al Viro8920e8f2005-09-07 18:28:51 -0700144 int err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 kcmlen = 0;
147 kcmsg_base = kcmsg = (struct cmsghdr *)stackbuf;
148 ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700149 while (ucmsg != NULL) {
150 if (get_user(ucmlen, &ucmsg->cmsg_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return -EFAULT;
152
153 /* Catch bogons. */
154 if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg))
155 return -EINVAL;
156
157 tmp = ((ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg))) +
158 CMSG_ALIGN(sizeof(struct cmsghdr)));
Al Viro8920e8f2005-09-07 18:28:51 -0700159 tmp = CMSG_ALIGN(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 kcmlen += tmp;
161 ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
162 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700163 if (kcmlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return -EINVAL;
165
166 /* The kcmlen holds the 64-bit version of the control length.
167 * It may not be modified as we do not stick it into the kmsg
168 * until we have successfully copied over all of the data
169 * from the user.
170 */
Al Viro8920e8f2005-09-07 18:28:51 -0700171 if (kcmlen > stackbuf_size)
172 kcmsg_base = kcmsg = sock_kmalloc(sk, kcmlen, GFP_KERNEL);
173 if (kcmsg == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return -ENOBUFS;
175
176 /* Now copy them over neatly. */
177 memset(kcmsg, 0, kcmlen);
178 ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700179 while (ucmsg != NULL) {
Al Viro8920e8f2005-09-07 18:28:51 -0700180 if (__get_user(ucmlen, &ucmsg->cmsg_len))
181 goto Efault;
182 if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg))
183 goto Einval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 tmp = ((ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg))) +
185 CMSG_ALIGN(sizeof(struct cmsghdr)));
Al Viro8920e8f2005-09-07 18:28:51 -0700186 if ((char *)kcmsg_base + kcmlen - (char *)kcmsg < CMSG_ALIGN(tmp))
187 goto Einval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 kcmsg->cmsg_len = tmp;
Al Viro8920e8f2005-09-07 18:28:51 -0700189 tmp = CMSG_ALIGN(tmp);
190 if (__get_user(kcmsg->cmsg_level, &ucmsg->cmsg_level) ||
191 __get_user(kcmsg->cmsg_type, &ucmsg->cmsg_type) ||
192 copy_from_user(CMSG_DATA(kcmsg),
193 CMSG_COMPAT_DATA(ucmsg),
194 (ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg)))))
195 goto Efault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 /* Advance. */
Al Viro8920e8f2005-09-07 18:28:51 -0700198 kcmsg = (struct cmsghdr *)((char *)kcmsg + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
200 }
201
202 /* Ok, looks like we made it. Hook it up and return success. */
203 kmsg->msg_control = kcmsg_base;
204 kmsg->msg_controllen = kcmlen;
205 return 0;
206
Al Viro8920e8f2005-09-07 18:28:51 -0700207Einval:
208 err = -EINVAL;
209Efault:
210 if (kcmsg_base != (struct cmsghdr *)stackbuf)
211 sock_kfree_s(sk, kcmsg_base, kcmlen);
212 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
215int put_cmsg_compat(struct msghdr *kmsg, int level, int type, int len, void *data)
216{
217 struct compat_timeval ctv;
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700218 struct compat_timespec cts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control;
220 struct compat_cmsghdr cmhdr;
221 int cmlen;
222
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700223 if (cm == NULL || kmsg->msg_controllen < sizeof(*cm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 kmsg->msg_flags |= MSG_CTRUNC;
225 return 0; /* XXX: return error? check spec. */
226 }
227
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +0900228 if (level == SOL_SOCKET && type == SO_TIMESTAMP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 struct timeval *tv = (struct timeval *)data;
230 ctv.tv_sec = tv->tv_sec;
231 ctv.tv_usec = tv->tv_usec;
232 data = &ctv;
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700233 len = sizeof(ctv);
234 }
235 if (level == SOL_SOCKET && type == SO_TIMESTAMPNS) {
236 struct timespec *ts = (struct timespec *)data;
237 cts.tv_sec = ts->tv_sec;
238 cts.tv_nsec = ts->tv_nsec;
239 data = &cts;
240 len = sizeof(cts);
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +0900241 }
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 cmlen = CMSG_COMPAT_LEN(len);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700244 if (kmsg->msg_controllen < cmlen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 kmsg->msg_flags |= MSG_CTRUNC;
246 cmlen = kmsg->msg_controllen;
247 }
248 cmhdr.cmsg_level = level;
249 cmhdr.cmsg_type = type;
250 cmhdr.cmsg_len = cmlen;
251
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700252 if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return -EFAULT;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700254 if (copy_to_user(CMSG_COMPAT_DATA(cm), data, cmlen - sizeof(struct compat_cmsghdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return -EFAULT;
256 cmlen = CMSG_COMPAT_SPACE(len);
Wei Yongjun1ac70e72007-12-20 14:36:44 -0800257 if (kmsg->msg_controllen < cmlen)
258 cmlen = kmsg->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 kmsg->msg_control += cmlen;
260 kmsg->msg_controllen -= cmlen;
261 return 0;
262}
263
264void scm_detach_fds_compat(struct msghdr *kmsg, struct scm_cookie *scm)
265{
266 struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control;
267 int fdmax = (kmsg->msg_controllen - sizeof(struct compat_cmsghdr)) / sizeof(int);
268 int fdnum = scm->fp->count;
269 struct file **fp = scm->fp->fp;
270 int __user *cmfptr;
271 int err = 0, i;
272
273 if (fdnum < fdmax)
274 fdmax = fdnum;
275
276 for (i = 0, cmfptr = (int __user *) CMSG_COMPAT_DATA(cm); i < fdmax; i++, cmfptr++) {
277 int new_fd;
278 err = security_file_receive(fp[i]);
279 if (err)
280 break;
Ulrich Drepper4a195422007-07-15 23:40:34 -0700281 err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & kmsg->msg_flags
282 ? O_CLOEXEC : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (err < 0)
284 break;
285 new_fd = err;
286 err = put_user(new_fd, cmfptr);
287 if (err) {
288 put_unused_fd(new_fd);
289 break;
290 }
291 /* Bump the usage count and install the file. */
292 get_file(fp[i]);
293 fd_install(new_fd, fp[i]);
294 }
295
296 if (i > 0) {
297 int cmlen = CMSG_COMPAT_LEN(i * sizeof(int));
Miklos Szeredieffee6a2006-10-09 21:42:14 -0700298 err = put_user(SOL_SOCKET, &cm->cmsg_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (!err)
300 err = put_user(SCM_RIGHTS, &cm->cmsg_type);
301 if (!err)
302 err = put_user(cmlen, &cm->cmsg_len);
303 if (!err) {
304 cmlen = CMSG_COMPAT_SPACE(i * sizeof(int));
305 kmsg->msg_control += cmlen;
306 kmsg->msg_controllen -= cmlen;
307 }
308 }
309 if (i < fdnum)
310 kmsg->msg_flags |= MSG_CTRUNC;
311
312 /*
313 * All of the files that fit in the message have had their
314 * usage counts incremented, so we just free the list.
315 */
316 __scm_destroy(scm);
317}
318
319/*
320 * For now, we assume that the compatibility and native version
321 * of struct ipt_entry are the same - sfr. FIXME
322 */
323struct compat_ipt_replace {
324 char name[IPT_TABLE_MAXNAMELEN];
325 u32 valid_hooks;
326 u32 num_entries;
327 u32 size;
328 u32 hook_entry[NF_IP_NUMHOOKS];
329 u32 underflow[NF_IP_NUMHOOKS];
330 u32 num_counters;
331 compat_uptr_t counters; /* struct ipt_counters * */
332 struct ipt_entry entries[0];
333};
334
335static int do_netfilter_replace(int fd, int level, int optname,
336 char __user *optval, int optlen)
337{
338 struct compat_ipt_replace __user *urepl;
339 struct ipt_replace __user *repl_nat;
340 char name[IPT_TABLE_MAXNAMELEN];
341 u32 origsize, tmp32, num_counters;
342 unsigned int repl_nat_size;
343 int ret;
344 int i;
345 compat_uptr_t ucntrs;
346
347 urepl = (struct compat_ipt_replace __user *)optval;
348 if (get_user(origsize, &urepl->size))
349 return -EFAULT;
350
351 /* Hack: Causes ipchains to give correct error msg --RR */
352 if (optlen != sizeof(*urepl) + origsize)
353 return -ENOPROTOOPT;
354
355 /* XXX Assumes that size of ipt_entry is the same both in
356 * native and compat environments.
357 */
358 repl_nat_size = sizeof(*repl_nat) + origsize;
359 repl_nat = compat_alloc_user_space(repl_nat_size);
360
361 ret = -EFAULT;
362 if (put_user(origsize, &repl_nat->size))
363 goto out;
364
365 if (!access_ok(VERIFY_READ, urepl, optlen) ||
366 !access_ok(VERIFY_WRITE, repl_nat, optlen))
367 goto out;
368
369 if (__copy_from_user(name, urepl->name, sizeof(urepl->name)) ||
370 __copy_to_user(repl_nat->name, name, sizeof(repl_nat->name)))
371 goto out;
372
373 if (__get_user(tmp32, &urepl->valid_hooks) ||
374 __put_user(tmp32, &repl_nat->valid_hooks))
375 goto out;
376
377 if (__get_user(tmp32, &urepl->num_entries) ||
378 __put_user(tmp32, &repl_nat->num_entries))
379 goto out;
380
381 if (__get_user(num_counters, &urepl->num_counters) ||
382 __put_user(num_counters, &repl_nat->num_counters))
383 goto out;
384
385 if (__get_user(ucntrs, &urepl->counters) ||
386 __put_user(compat_ptr(ucntrs), &repl_nat->counters))
387 goto out;
388
389 if (__copy_in_user(&repl_nat->entries[0],
390 &urepl->entries[0],
391 origsize))
392 goto out;
393
394 for (i = 0; i < NF_IP_NUMHOOKS; i++) {
395 if (__get_user(tmp32, &urepl->hook_entry[i]) ||
396 __put_user(tmp32, &repl_nat->hook_entry[i]) ||
397 __get_user(tmp32, &urepl->underflow[i]) ||
398 __put_user(tmp32, &repl_nat->underflow[i]))
399 goto out;
400 }
401
402 /*
403 * Since struct ipt_counters just contains two u_int64_t members
404 * we can just do the access_ok check here and pass the (converted)
405 * pointer into the standard syscall. We hope that the pointer is
406 * not misaligned ...
407 */
408 if (!access_ok(VERIFY_WRITE, compat_ptr(ucntrs),
409 num_counters * sizeof(struct ipt_counters)))
410 goto out;
411
412
413 ret = sys_setsockopt(fd, level, optname,
414 (char __user *)repl_nat, repl_nat_size);
415
416out:
417 return ret;
418}
419
420/*
421 * A struct sock_filter is architecture independent.
422 */
423struct compat_sock_fprog {
424 u16 len;
425 compat_uptr_t filter; /* struct sock_filter * */
426};
427
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800428static int do_set_attach_filter(struct socket *sock, int level, int optname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 char __user *optval, int optlen)
430{
431 struct compat_sock_fprog __user *fprog32 = (struct compat_sock_fprog __user *)optval;
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +0900432 struct sock_fprog __user *kfprog = compat_alloc_user_space(sizeof(struct sock_fprog));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 compat_uptr_t ptr;
434 u16 len;
435
436 if (!access_ok(VERIFY_READ, fprog32, sizeof(*fprog32)) ||
437 !access_ok(VERIFY_WRITE, kfprog, sizeof(struct sock_fprog)) ||
438 __get_user(len, &fprog32->len) ||
439 __get_user(ptr, &fprog32->filter) ||
440 __put_user(len, &kfprog->len) ||
441 __put_user(compat_ptr(ptr), &kfprog->filter))
442 return -EFAULT;
443
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800444 return sock_setsockopt(sock, level, optname, (char __user *)kfprog,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 sizeof(struct sock_fprog));
446}
447
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800448static int do_set_sock_timeout(struct socket *sock, int level,
449 int optname, char __user *optval, int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 struct compat_timeval __user *up = (struct compat_timeval __user *) optval;
452 struct timeval ktime;
453 mm_segment_t old_fs;
454 int err;
455
456 if (optlen < sizeof(*up))
457 return -EINVAL;
458 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
459 __get_user(ktime.tv_sec, &up->tv_sec) ||
460 __get_user(ktime.tv_usec, &up->tv_usec))
461 return -EFAULT;
462 old_fs = get_fs();
463 set_fs(KERNEL_DS);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800464 err = sock_setsockopt(sock, level, optname, (char *) &ktime, sizeof(ktime));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 set_fs(old_fs);
466
467 return err;
468}
469
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800470static int compat_sock_setsockopt(struct socket *sock, int level, int optname,
471 char __user *optval, int optlen)
472{
473 if (optname == SO_ATTACH_FILTER)
474 return do_set_attach_filter(sock, level, optname,
475 optval, optlen);
476 if (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)
477 return do_set_sock_timeout(sock, level, optname, optval, optlen);
478
479 return sock_setsockopt(sock, level, optname, optval, optlen);
480}
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482asmlinkage long compat_sys_setsockopt(int fd, int level, int optname,
483 char __user *optval, int optlen)
484{
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800485 int err;
486 struct socket *sock;
487
Dmitry Mishin27229712006-04-01 02:25:19 -0800488 if (level == SOL_IPV6 && optname == IPT_SO_SET_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return do_netfilter_replace(fd, level, optname,
490 optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800492 if (optlen < 0)
493 return -EINVAL;
494
495 if ((sock = sockfd_lookup(fd, &err))!=NULL)
496 {
497 err = security_socket_setsockopt(sock,level,optname);
498 if (err) {
499 sockfd_put(sock);
500 return err;
501 }
502
503 if (level == SOL_SOCKET)
504 err = compat_sock_setsockopt(sock, level,
505 optname, optval, optlen);
506 else if (sock->ops->compat_setsockopt)
507 err = sock->ops->compat_setsockopt(sock, level,
508 optname, optval, optlen);
509 else
510 err = sock->ops->setsockopt(sock, level,
511 optname, optval, optlen);
512 sockfd_put(sock);
513 }
514 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800517static int do_get_sock_timeout(struct socket *sock, int level, int optname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 char __user *optval, int __user *optlen)
519{
520 struct compat_timeval __user *up;
521 struct timeval ktime;
522 mm_segment_t old_fs;
523 int len, err;
524
525 up = (struct compat_timeval __user *) optval;
526 if (get_user(len, optlen))
527 return -EFAULT;
528 if (len < sizeof(*up))
529 return -EINVAL;
530 len = sizeof(ktime);
531 old_fs = get_fs();
532 set_fs(KERNEL_DS);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800533 err = sock_getsockopt(sock, level, optname, (char *) &ktime, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 set_fs(old_fs);
535
536 if (!err) {
537 if (put_user(sizeof(*up), optlen) ||
538 !access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
539 __put_user(ktime.tv_sec, &up->tv_sec) ||
540 __put_user(ktime.tv_usec, &up->tv_usec))
541 err = -EFAULT;
542 }
543 return err;
544}
545
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800546static int compat_sock_getsockopt(struct socket *sock, int level, int optname,
547 char __user *optval, int __user *optlen)
548{
549 if (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)
550 return do_get_sock_timeout(sock, level, optname, optval, optlen);
551 return sock_getsockopt(sock, level, optname, optval, optlen);
552}
553
Shaun Pereiraf0ac2612006-03-21 23:59:39 -0800554int compat_sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
555{
556 struct compat_timeval __user *ctv =
557 (struct compat_timeval __user*) userstamp;
558 int err = -ENOENT;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700559 struct timeval tv;
Shaun Pereiraf0ac2612006-03-21 23:59:39 -0800560
561 if (!sock_flag(sk, SOCK_TIMESTAMP))
562 sock_enable_timestamp(sk);
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700563 tv = ktime_to_timeval(sk->sk_stamp);
564 if (tv.tv_sec == -1)
Shaun Pereiraf0ac2612006-03-21 23:59:39 -0800565 return err;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700566 if (tv.tv_sec == 0) {
567 sk->sk_stamp = ktime_get_real();
568 tv = ktime_to_timeval(sk->sk_stamp);
569 }
570 err = 0;
571 if (put_user(tv.tv_sec, &ctv->tv_sec) ||
572 put_user(tv.tv_usec, &ctv->tv_usec))
Shaun Pereiraf0ac2612006-03-21 23:59:39 -0800573 err = -EFAULT;
574 return err;
575}
576EXPORT_SYMBOL(compat_sock_get_timestamp);
577
Eric Dumazetae40eb12007-03-18 17:33:16 -0700578int compat_sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
579{
580 struct compat_timespec __user *ctv =
581 (struct compat_timespec __user*) userstamp;
582 int err = -ENOENT;
583 struct timespec ts;
584
585 if (!sock_flag(sk, SOCK_TIMESTAMP))
586 sock_enable_timestamp(sk);
587 ts = ktime_to_timespec(sk->sk_stamp);
588 if (ts.tv_sec == -1)
589 return err;
590 if (ts.tv_sec == 0) {
591 sk->sk_stamp = ktime_get_real();
592 ts = ktime_to_timespec(sk->sk_stamp);
593 }
594 err = 0;
595 if (put_user(ts.tv_sec, &ctv->tv_sec) ||
596 put_user(ts.tv_nsec, &ctv->tv_nsec))
597 err = -EFAULT;
598 return err;
599}
600EXPORT_SYMBOL(compat_sock_get_timestampns);
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602asmlinkage long compat_sys_getsockopt(int fd, int level, int optname,
603 char __user *optval, int __user *optlen)
604{
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800605 int err;
606 struct socket *sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800608 if ((sock = sockfd_lookup(fd, &err))!=NULL)
609 {
610 err = security_socket_getsockopt(sock, level,
611 optname);
612 if (err) {
613 sockfd_put(sock);
614 return err;
615 }
616
617 if (level == SOL_SOCKET)
618 err = compat_sock_getsockopt(sock, level,
619 optname, optval, optlen);
620 else if (sock->ops->compat_getsockopt)
621 err = sock->ops->compat_getsockopt(sock, level,
622 optname, optval, optlen);
623 else
624 err = sock->ops->getsockopt(sock, level,
625 optname, optval, optlen);
626 sockfd_put(sock);
627 }
628 return err;
629}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630/* Argument list sizes for compat_sys_socketcall */
631#define AL(x) ((x) * sizeof(u32))
632static unsigned char nas[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
633 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
634 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)};
635#undef AL
636
637asmlinkage long compat_sys_sendmsg(int fd, struct compat_msghdr __user *msg, unsigned flags)
638{
639 return sys_sendmsg(fd, (struct msghdr __user *)msg, flags | MSG_CMSG_COMPAT);
640}
641
642asmlinkage long compat_sys_recvmsg(int fd, struct compat_msghdr __user *msg, unsigned int flags)
643{
644 return sys_recvmsg(fd, (struct msghdr __user *)msg, flags | MSG_CMSG_COMPAT);
645}
646
647asmlinkage long compat_sys_socketcall(int call, u32 __user *args)
648{
649 int ret;
650 u32 a[6];
651 u32 a0, a1;
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +0900652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (call < SYS_SOCKET || call > SYS_RECVMSG)
654 return -EINVAL;
655 if (copy_from_user(a, args, nas[call]))
656 return -EFAULT;
657 a0 = a[0];
658 a1 = a[1];
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +0900659
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700660 switch (call) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 case SYS_SOCKET:
662 ret = sys_socket(a0, a1, a[2]);
663 break;
664 case SYS_BIND:
665 ret = sys_bind(a0, compat_ptr(a1), a[2]);
666 break;
667 case SYS_CONNECT:
668 ret = sys_connect(a0, compat_ptr(a1), a[2]);
669 break;
670 case SYS_LISTEN:
671 ret = sys_listen(a0, a1);
672 break;
673 case SYS_ACCEPT:
674 ret = sys_accept(a0, compat_ptr(a1), compat_ptr(a[2]));
675 break;
676 case SYS_GETSOCKNAME:
677 ret = sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2]));
678 break;
679 case SYS_GETPEERNAME:
680 ret = sys_getpeername(a0, compat_ptr(a1), compat_ptr(a[2]));
681 break;
682 case SYS_SOCKETPAIR:
683 ret = sys_socketpair(a0, a1, a[2], compat_ptr(a[3]));
684 break;
685 case SYS_SEND:
686 ret = sys_send(a0, compat_ptr(a1), a[2], a[3]);
687 break;
688 case SYS_SENDTO:
689 ret = sys_sendto(a0, compat_ptr(a1), a[2], a[3], compat_ptr(a[4]), a[5]);
690 break;
691 case SYS_RECV:
692 ret = sys_recv(a0, compat_ptr(a1), a[2], a[3]);
693 break;
694 case SYS_RECVFROM:
695 ret = sys_recvfrom(a0, compat_ptr(a1), a[2], a[3], compat_ptr(a[4]), compat_ptr(a[5]));
696 break;
697 case SYS_SHUTDOWN:
698 ret = sys_shutdown(a0,a1);
699 break;
700 case SYS_SETSOCKOPT:
701 ret = compat_sys_setsockopt(a0, a1, a[2],
702 compat_ptr(a[3]), a[4]);
703 break;
704 case SYS_GETSOCKOPT:
705 ret = compat_sys_getsockopt(a0, a1, a[2],
706 compat_ptr(a[3]), compat_ptr(a[4]));
707 break;
708 case SYS_SENDMSG:
709 ret = compat_sys_sendmsg(a0, compat_ptr(a1), a[2]);
710 break;
711 case SYS_RECVMSG:
712 ret = compat_sys_recvmsg(a0, compat_ptr(a1), a[2]);
713 break;
714 default:
715 ret = -EINVAL;
716 break;
717 }
718 return ret;
719}