blob: 52d32f1bc7281c6da3276ae78a0cad1e63aa4426 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * 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>
11 * Copyright (C) 2000,2001 Andi Kleen, SuSE Labs
12 */
13
14#include <linux/kernel.h>
15#include <linux/fs.h>
16#include <linux/sched.h>
17#include <linux/types.h>
18#include <linux/file.h>
19#include <linux/icmpv6.h>
20#include <linux/socket.h>
21#include <linux/syscalls.h>
22#include <linux/filter.h>
23#include <linux/compat.h>
24#include <linux/netfilter_ipv4/ip_tables.h>
25#include <linux/security.h>
26
27#include <net/scm.h>
28#include <net/sock.h>
29#include <asm/uaccess.h>
30#include <net/compat.h>
31
32static inline int iov_from_user_compat_to_kern(struct iovec *kiov,
33 struct compat_iovec __user *uiov32,
34 int niov)
35{
36 int tot_len = 0;
37
38 while(niov > 0) {
39 compat_uptr_t buf;
40 compat_size_t len;
41
42 if(get_user(len, &uiov32->iov_len) ||
43 get_user(buf, &uiov32->iov_base)) {
44 tot_len = -EFAULT;
45 break;
46 }
47 tot_len += len;
48 kiov->iov_base = compat_ptr(buf);
49 kiov->iov_len = (__kernel_size_t) len;
50 uiov32++;
51 kiov++;
52 niov--;
53 }
54 return tot_len;
55}
56
57int get_compat_msghdr(struct msghdr *kmsg, struct compat_msghdr __user *umsg)
58{
59 compat_uptr_t tmp1, tmp2, tmp3;
60
61 if (!access_ok(VERIFY_READ, umsg, sizeof(*umsg)) ||
62 __get_user(tmp1, &umsg->msg_name) ||
63 __get_user(kmsg->msg_namelen, &umsg->msg_namelen) ||
64 __get_user(tmp2, &umsg->msg_iov) ||
65 __get_user(kmsg->msg_iovlen, &umsg->msg_iovlen) ||
66 __get_user(tmp3, &umsg->msg_control) ||
67 __get_user(kmsg->msg_controllen, &umsg->msg_controllen) ||
68 __get_user(kmsg->msg_flags, &umsg->msg_flags))
69 return -EFAULT;
70 kmsg->msg_name = compat_ptr(tmp1);
71 kmsg->msg_iov = compat_ptr(tmp2);
72 kmsg->msg_control = compat_ptr(tmp3);
73 return 0;
74}
75
76/* I've named the args so it is easy to tell whose space the pointers are in. */
77int verify_compat_iovec(struct msghdr *kern_msg, struct iovec *kern_iov,
78 char *kern_address, int mode)
79{
80 int tot_len;
81
82 if(kern_msg->msg_namelen) {
83 if(mode==VERIFY_READ) {
84 int err = move_addr_to_kernel(kern_msg->msg_name,
85 kern_msg->msg_namelen,
86 kern_address);
87 if(err < 0)
88 return err;
89 }
90 kern_msg->msg_name = kern_address;
91 } else
92 kern_msg->msg_name = NULL;
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 tot_len = iov_from_user_compat_to_kern(kern_iov,
95 (struct compat_iovec __user *)kern_msg->msg_iov,
96 kern_msg->msg_iovlen);
97 if(tot_len >= 0)
98 kern_msg->msg_iov = kern_iov;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 return tot_len;
101}
102
103/* Bleech... */
104#define CMSG_COMPAT_ALIGN(len) ALIGN((len), sizeof(s32))
105
106#define CMSG_COMPAT_DATA(cmsg) \
107 ((void __user *)((char __user *)(cmsg) + CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr))))
108#define CMSG_COMPAT_SPACE(len) \
109 (CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)) + CMSG_COMPAT_ALIGN(len))
110#define CMSG_COMPAT_LEN(len) \
111 (CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)) + (len))
112
113#define CMSG_COMPAT_FIRSTHDR(msg) \
114 (((msg)->msg_controllen) >= sizeof(struct compat_cmsghdr) ? \
115 (struct compat_cmsghdr __user *)((msg)->msg_control) : \
116 (struct compat_cmsghdr __user *)NULL)
117
118#define CMSG_COMPAT_OK(ucmlen, ucmsg, mhdr) \
119 ((ucmlen) >= sizeof(struct compat_cmsghdr) && \
120 (ucmlen) <= (unsigned long) \
121 ((mhdr)->msg_controllen - \
122 ((char *)(ucmsg) - (char *)(mhdr)->msg_control)))
123
124static inline struct compat_cmsghdr __user *cmsg_compat_nxthdr(struct msghdr *msg,
125 struct compat_cmsghdr __user *cmsg, int cmsg_len)
126{
127 char __user *ptr = (char __user *)cmsg + CMSG_COMPAT_ALIGN(cmsg_len);
128 if ((unsigned long)(ptr + 1 - (char __user *)msg->msg_control) >
129 msg->msg_controllen)
130 return NULL;
131 return (struct compat_cmsghdr __user *)ptr;
132}
133
134/* There is a lot of hair here because the alignment rules (and
135 * thus placement) of cmsg headers and length are different for
136 * 32-bit apps. -DaveM
137 */
Al Viro8920e8f2005-09-07 18:28:51 -0700138int cmsghdr_from_user_compat_to_kern(struct msghdr *kmsg, struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 unsigned char *stackbuf, int stackbuf_size)
140{
141 struct compat_cmsghdr __user *ucmsg;
142 struct cmsghdr *kcmsg, *kcmsg_base;
143 compat_size_t ucmlen;
144 __kernel_size_t kcmlen, tmp;
Al Viro8920e8f2005-09-07 18:28:51 -0700145 int err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 kcmlen = 0;
148 kcmsg_base = kcmsg = (struct cmsghdr *)stackbuf;
149 ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
150 while(ucmsg != NULL) {
151 if(get_user(ucmlen, &ucmsg->cmsg_len))
152 return -EFAULT;
153
154 /* Catch bogons. */
155 if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg))
156 return -EINVAL;
157
158 tmp = ((ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg))) +
159 CMSG_ALIGN(sizeof(struct cmsghdr)));
Al Viro8920e8f2005-09-07 18:28:51 -0700160 tmp = CMSG_ALIGN(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 kcmlen += tmp;
162 ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
163 }
164 if(kcmlen == 0)
165 return -EINVAL;
166
167 /* The kcmlen holds the 64-bit version of the control length.
168 * It may not be modified as we do not stick it into the kmsg
169 * until we have successfully copied over all of the data
170 * from the user.
171 */
Al Viro8920e8f2005-09-07 18:28:51 -0700172 if (kcmlen > stackbuf_size)
173 kcmsg_base = kcmsg = sock_kmalloc(sk, kcmlen, GFP_KERNEL);
174 if (kcmsg == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return -ENOBUFS;
176
177 /* Now copy them over neatly. */
178 memset(kcmsg, 0, kcmlen);
179 ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
180 while(ucmsg != NULL) {
Al Viro8920e8f2005-09-07 18:28:51 -0700181 if (__get_user(ucmlen, &ucmsg->cmsg_len))
182 goto Efault;
183 if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg))
184 goto Einval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 tmp = ((ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg))) +
186 CMSG_ALIGN(sizeof(struct cmsghdr)));
Al Viro8920e8f2005-09-07 18:28:51 -0700187 if ((char *)kcmsg_base + kcmlen - (char *)kcmsg < CMSG_ALIGN(tmp))
188 goto Einval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 kcmsg->cmsg_len = tmp;
Al Viro8920e8f2005-09-07 18:28:51 -0700190 tmp = CMSG_ALIGN(tmp);
191 if (__get_user(kcmsg->cmsg_level, &ucmsg->cmsg_level) ||
192 __get_user(kcmsg->cmsg_type, &ucmsg->cmsg_type) ||
193 copy_from_user(CMSG_DATA(kcmsg),
194 CMSG_COMPAT_DATA(ucmsg),
195 (ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg)))))
196 goto Efault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 /* Advance. */
Al Viro8920e8f2005-09-07 18:28:51 -0700199 kcmsg = (struct cmsghdr *)((char *)kcmsg + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
201 }
202
203 /* Ok, looks like we made it. Hook it up and return success. */
204 kmsg->msg_control = kcmsg_base;
205 kmsg->msg_controllen = kcmlen;
206 return 0;
207
Al Viro8920e8f2005-09-07 18:28:51 -0700208Einval:
209 err = -EINVAL;
210Efault:
211 if (kcmsg_base != (struct cmsghdr *)stackbuf)
212 sock_kfree_s(sk, kcmsg_base, kcmlen);
213 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
216int put_cmsg_compat(struct msghdr *kmsg, int level, int type, int len, void *data)
217{
218 struct compat_timeval ctv;
219 struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control;
220 struct compat_cmsghdr cmhdr;
221 int cmlen;
222
223 if(cm == NULL || kmsg->msg_controllen < sizeof(*cm)) {
224 kmsg->msg_flags |= MSG_CTRUNC;
225 return 0; /* XXX: return error? check spec. */
226 }
227
228 if (level == SOL_SOCKET && type == SO_TIMESTAMP) {
229 struct timeval *tv = (struct timeval *)data;
230 ctv.tv_sec = tv->tv_sec;
231 ctv.tv_usec = tv->tv_usec;
232 data = &ctv;
233 len = sizeof(struct compat_timeval);
234 }
235
236 cmlen = CMSG_COMPAT_LEN(len);
237 if(kmsg->msg_controllen < cmlen) {
238 kmsg->msg_flags |= MSG_CTRUNC;
239 cmlen = kmsg->msg_controllen;
240 }
241 cmhdr.cmsg_level = level;
242 cmhdr.cmsg_type = type;
243 cmhdr.cmsg_len = cmlen;
244
245 if(copy_to_user(cm, &cmhdr, sizeof cmhdr))
246 return -EFAULT;
247 if(copy_to_user(CMSG_COMPAT_DATA(cm), data, cmlen - sizeof(struct compat_cmsghdr)))
248 return -EFAULT;
249 cmlen = CMSG_COMPAT_SPACE(len);
250 kmsg->msg_control += cmlen;
251 kmsg->msg_controllen -= cmlen;
252 return 0;
253}
254
255void scm_detach_fds_compat(struct msghdr *kmsg, struct scm_cookie *scm)
256{
257 struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control;
258 int fdmax = (kmsg->msg_controllen - sizeof(struct compat_cmsghdr)) / sizeof(int);
259 int fdnum = scm->fp->count;
260 struct file **fp = scm->fp->fp;
261 int __user *cmfptr;
262 int err = 0, i;
263
264 if (fdnum < fdmax)
265 fdmax = fdnum;
266
267 for (i = 0, cmfptr = (int __user *) CMSG_COMPAT_DATA(cm); i < fdmax; i++, cmfptr++) {
268 int new_fd;
269 err = security_file_receive(fp[i]);
270 if (err)
271 break;
272 err = get_unused_fd();
273 if (err < 0)
274 break;
275 new_fd = err;
276 err = put_user(new_fd, cmfptr);
277 if (err) {
278 put_unused_fd(new_fd);
279 break;
280 }
281 /* Bump the usage count and install the file. */
282 get_file(fp[i]);
283 fd_install(new_fd, fp[i]);
284 }
285
286 if (i > 0) {
287 int cmlen = CMSG_COMPAT_LEN(i * sizeof(int));
Miklos Szeredieffee6a2006-10-09 21:42:14 -0700288 err = put_user(SOL_SOCKET, &cm->cmsg_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (!err)
290 err = put_user(SCM_RIGHTS, &cm->cmsg_type);
291 if (!err)
292 err = put_user(cmlen, &cm->cmsg_len);
293 if (!err) {
294 cmlen = CMSG_COMPAT_SPACE(i * sizeof(int));
295 kmsg->msg_control += cmlen;
296 kmsg->msg_controllen -= cmlen;
297 }
298 }
299 if (i < fdnum)
300 kmsg->msg_flags |= MSG_CTRUNC;
301
302 /*
303 * All of the files that fit in the message have had their
304 * usage counts incremented, so we just free the list.
305 */
306 __scm_destroy(scm);
307}
308
309/*
310 * For now, we assume that the compatibility and native version
311 * of struct ipt_entry are the same - sfr. FIXME
312 */
313struct compat_ipt_replace {
314 char name[IPT_TABLE_MAXNAMELEN];
315 u32 valid_hooks;
316 u32 num_entries;
317 u32 size;
318 u32 hook_entry[NF_IP_NUMHOOKS];
319 u32 underflow[NF_IP_NUMHOOKS];
320 u32 num_counters;
321 compat_uptr_t counters; /* struct ipt_counters * */
322 struct ipt_entry entries[0];
323};
324
325static int do_netfilter_replace(int fd, int level, int optname,
326 char __user *optval, int optlen)
327{
328 struct compat_ipt_replace __user *urepl;
329 struct ipt_replace __user *repl_nat;
330 char name[IPT_TABLE_MAXNAMELEN];
331 u32 origsize, tmp32, num_counters;
332 unsigned int repl_nat_size;
333 int ret;
334 int i;
335 compat_uptr_t ucntrs;
336
337 urepl = (struct compat_ipt_replace __user *)optval;
338 if (get_user(origsize, &urepl->size))
339 return -EFAULT;
340
341 /* Hack: Causes ipchains to give correct error msg --RR */
342 if (optlen != sizeof(*urepl) + origsize)
343 return -ENOPROTOOPT;
344
345 /* XXX Assumes that size of ipt_entry is the same both in
346 * native and compat environments.
347 */
348 repl_nat_size = sizeof(*repl_nat) + origsize;
349 repl_nat = compat_alloc_user_space(repl_nat_size);
350
351 ret = -EFAULT;
352 if (put_user(origsize, &repl_nat->size))
353 goto out;
354
355 if (!access_ok(VERIFY_READ, urepl, optlen) ||
356 !access_ok(VERIFY_WRITE, repl_nat, optlen))
357 goto out;
358
359 if (__copy_from_user(name, urepl->name, sizeof(urepl->name)) ||
360 __copy_to_user(repl_nat->name, name, sizeof(repl_nat->name)))
361 goto out;
362
363 if (__get_user(tmp32, &urepl->valid_hooks) ||
364 __put_user(tmp32, &repl_nat->valid_hooks))
365 goto out;
366
367 if (__get_user(tmp32, &urepl->num_entries) ||
368 __put_user(tmp32, &repl_nat->num_entries))
369 goto out;
370
371 if (__get_user(num_counters, &urepl->num_counters) ||
372 __put_user(num_counters, &repl_nat->num_counters))
373 goto out;
374
375 if (__get_user(ucntrs, &urepl->counters) ||
376 __put_user(compat_ptr(ucntrs), &repl_nat->counters))
377 goto out;
378
379 if (__copy_in_user(&repl_nat->entries[0],
380 &urepl->entries[0],
381 origsize))
382 goto out;
383
384 for (i = 0; i < NF_IP_NUMHOOKS; i++) {
385 if (__get_user(tmp32, &urepl->hook_entry[i]) ||
386 __put_user(tmp32, &repl_nat->hook_entry[i]) ||
387 __get_user(tmp32, &urepl->underflow[i]) ||
388 __put_user(tmp32, &repl_nat->underflow[i]))
389 goto out;
390 }
391
392 /*
393 * Since struct ipt_counters just contains two u_int64_t members
394 * we can just do the access_ok check here and pass the (converted)
395 * pointer into the standard syscall. We hope that the pointer is
396 * not misaligned ...
397 */
398 if (!access_ok(VERIFY_WRITE, compat_ptr(ucntrs),
399 num_counters * sizeof(struct ipt_counters)))
400 goto out;
401
402
403 ret = sys_setsockopt(fd, level, optname,
404 (char __user *)repl_nat, repl_nat_size);
405
406out:
407 return ret;
408}
409
410/*
411 * A struct sock_filter is architecture independent.
412 */
413struct compat_sock_fprog {
414 u16 len;
415 compat_uptr_t filter; /* struct sock_filter * */
416};
417
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800418static int do_set_attach_filter(struct socket *sock, int level, int optname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 char __user *optval, int optlen)
420{
421 struct compat_sock_fprog __user *fprog32 = (struct compat_sock_fprog __user *)optval;
422 struct sock_fprog __user *kfprog = compat_alloc_user_space(sizeof(struct sock_fprog));
423 compat_uptr_t ptr;
424 u16 len;
425
426 if (!access_ok(VERIFY_READ, fprog32, sizeof(*fprog32)) ||
427 !access_ok(VERIFY_WRITE, kfprog, sizeof(struct sock_fprog)) ||
428 __get_user(len, &fprog32->len) ||
429 __get_user(ptr, &fprog32->filter) ||
430 __put_user(len, &kfprog->len) ||
431 __put_user(compat_ptr(ptr), &kfprog->filter))
432 return -EFAULT;
433
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800434 return sock_setsockopt(sock, level, optname, (char __user *)kfprog,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 sizeof(struct sock_fprog));
436}
437
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800438static int do_set_sock_timeout(struct socket *sock, int level,
439 int optname, char __user *optval, int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 struct compat_timeval __user *up = (struct compat_timeval __user *) optval;
442 struct timeval ktime;
443 mm_segment_t old_fs;
444 int err;
445
446 if (optlen < sizeof(*up))
447 return -EINVAL;
448 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
449 __get_user(ktime.tv_sec, &up->tv_sec) ||
450 __get_user(ktime.tv_usec, &up->tv_usec))
451 return -EFAULT;
452 old_fs = get_fs();
453 set_fs(KERNEL_DS);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800454 err = sock_setsockopt(sock, level, optname, (char *) &ktime, sizeof(ktime));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 set_fs(old_fs);
456
457 return err;
458}
459
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800460static int compat_sock_setsockopt(struct socket *sock, int level, int optname,
461 char __user *optval, int optlen)
462{
463 if (optname == SO_ATTACH_FILTER)
464 return do_set_attach_filter(sock, level, optname,
465 optval, optlen);
466 if (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)
467 return do_set_sock_timeout(sock, level, optname, optval, optlen);
468
469 return sock_setsockopt(sock, level, optname, optval, optlen);
470}
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472asmlinkage long compat_sys_setsockopt(int fd, int level, int optname,
473 char __user *optval, int optlen)
474{
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800475 int err;
476 struct socket *sock;
477
Dmitry Mishin27229712006-04-01 02:25:19 -0800478 if (level == SOL_IPV6 && optname == IPT_SO_SET_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return do_netfilter_replace(fd, level, optname,
480 optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800482 if (optlen < 0)
483 return -EINVAL;
484
485 if ((sock = sockfd_lookup(fd, &err))!=NULL)
486 {
487 err = security_socket_setsockopt(sock,level,optname);
488 if (err) {
489 sockfd_put(sock);
490 return err;
491 }
492
493 if (level == SOL_SOCKET)
494 err = compat_sock_setsockopt(sock, level,
495 optname, optval, optlen);
496 else if (sock->ops->compat_setsockopt)
497 err = sock->ops->compat_setsockopt(sock, level,
498 optname, optval, optlen);
499 else
500 err = sock->ops->setsockopt(sock, level,
501 optname, optval, optlen);
502 sockfd_put(sock);
503 }
504 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800507static int do_get_sock_timeout(struct socket *sock, int level, int optname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 char __user *optval, int __user *optlen)
509{
510 struct compat_timeval __user *up;
511 struct timeval ktime;
512 mm_segment_t old_fs;
513 int len, err;
514
515 up = (struct compat_timeval __user *) optval;
516 if (get_user(len, optlen))
517 return -EFAULT;
518 if (len < sizeof(*up))
519 return -EINVAL;
520 len = sizeof(ktime);
521 old_fs = get_fs();
522 set_fs(KERNEL_DS);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800523 err = sock_getsockopt(sock, level, optname, (char *) &ktime, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 set_fs(old_fs);
525
526 if (!err) {
527 if (put_user(sizeof(*up), optlen) ||
528 !access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
529 __put_user(ktime.tv_sec, &up->tv_sec) ||
530 __put_user(ktime.tv_usec, &up->tv_usec))
531 err = -EFAULT;
532 }
533 return err;
534}
535
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800536static int compat_sock_getsockopt(struct socket *sock, int level, int optname,
537 char __user *optval, int __user *optlen)
538{
539 if (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)
540 return do_get_sock_timeout(sock, level, optname, optval, optlen);
541 return sock_getsockopt(sock, level, optname, optval, optlen);
542}
543
Shaun Pereiraf0ac2612006-03-21 23:59:39 -0800544int compat_sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
545{
546 struct compat_timeval __user *ctv =
547 (struct compat_timeval __user*) userstamp;
548 int err = -ENOENT;
549
550 if (!sock_flag(sk, SOCK_TIMESTAMP))
551 sock_enable_timestamp(sk);
552 if (sk->sk_stamp.tv_sec == -1)
553 return err;
554 if (sk->sk_stamp.tv_sec == 0)
555 do_gettimeofday(&sk->sk_stamp);
556 if (put_user(sk->sk_stamp.tv_sec, &ctv->tv_sec) ||
557 put_user(sk->sk_stamp.tv_usec, &ctv->tv_usec))
558 err = -EFAULT;
559 return err;
560}
561EXPORT_SYMBOL(compat_sock_get_timestamp);
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563asmlinkage long compat_sys_getsockopt(int fd, int level, int optname,
564 char __user *optval, int __user *optlen)
565{
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800566 int err;
567 struct socket *sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800569 if ((sock = sockfd_lookup(fd, &err))!=NULL)
570 {
571 err = security_socket_getsockopt(sock, level,
572 optname);
573 if (err) {
574 sockfd_put(sock);
575 return err;
576 }
577
578 if (level == SOL_SOCKET)
579 err = compat_sock_getsockopt(sock, level,
580 optname, optval, optlen);
581 else if (sock->ops->compat_getsockopt)
582 err = sock->ops->compat_getsockopt(sock, level,
583 optname, optval, optlen);
584 else
585 err = sock->ops->getsockopt(sock, level,
586 optname, optval, optlen);
587 sockfd_put(sock);
588 }
589 return err;
590}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591/* Argument list sizes for compat_sys_socketcall */
592#define AL(x) ((x) * sizeof(u32))
593static unsigned char nas[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
594 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
595 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)};
596#undef AL
597
598asmlinkage long compat_sys_sendmsg(int fd, struct compat_msghdr __user *msg, unsigned flags)
599{
600 return sys_sendmsg(fd, (struct msghdr __user *)msg, flags | MSG_CMSG_COMPAT);
601}
602
603asmlinkage long compat_sys_recvmsg(int fd, struct compat_msghdr __user *msg, unsigned int flags)
604{
605 return sys_recvmsg(fd, (struct msghdr __user *)msg, flags | MSG_CMSG_COMPAT);
606}
607
608asmlinkage long compat_sys_socketcall(int call, u32 __user *args)
609{
610 int ret;
611 u32 a[6];
612 u32 a0, a1;
613
614 if (call < SYS_SOCKET || call > SYS_RECVMSG)
615 return -EINVAL;
616 if (copy_from_user(a, args, nas[call]))
617 return -EFAULT;
618 a0 = a[0];
619 a1 = a[1];
620
621 switch(call) {
622 case SYS_SOCKET:
623 ret = sys_socket(a0, a1, a[2]);
624 break;
625 case SYS_BIND:
626 ret = sys_bind(a0, compat_ptr(a1), a[2]);
627 break;
628 case SYS_CONNECT:
629 ret = sys_connect(a0, compat_ptr(a1), a[2]);
630 break;
631 case SYS_LISTEN:
632 ret = sys_listen(a0, a1);
633 break;
634 case SYS_ACCEPT:
635 ret = sys_accept(a0, compat_ptr(a1), compat_ptr(a[2]));
636 break;
637 case SYS_GETSOCKNAME:
638 ret = sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2]));
639 break;
640 case SYS_GETPEERNAME:
641 ret = sys_getpeername(a0, compat_ptr(a1), compat_ptr(a[2]));
642 break;
643 case SYS_SOCKETPAIR:
644 ret = sys_socketpair(a0, a1, a[2], compat_ptr(a[3]));
645 break;
646 case SYS_SEND:
647 ret = sys_send(a0, compat_ptr(a1), a[2], a[3]);
648 break;
649 case SYS_SENDTO:
650 ret = sys_sendto(a0, compat_ptr(a1), a[2], a[3], compat_ptr(a[4]), a[5]);
651 break;
652 case SYS_RECV:
653 ret = sys_recv(a0, compat_ptr(a1), a[2], a[3]);
654 break;
655 case SYS_RECVFROM:
656 ret = sys_recvfrom(a0, compat_ptr(a1), a[2], a[3], compat_ptr(a[4]), compat_ptr(a[5]));
657 break;
658 case SYS_SHUTDOWN:
659 ret = sys_shutdown(a0,a1);
660 break;
661 case SYS_SETSOCKOPT:
662 ret = compat_sys_setsockopt(a0, a1, a[2],
663 compat_ptr(a[3]), a[4]);
664 break;
665 case SYS_GETSOCKOPT:
666 ret = compat_sys_getsockopt(a0, a1, a[2],
667 compat_ptr(a[3]), compat_ptr(a[4]));
668 break;
669 case SYS_SENDMSG:
670 ret = compat_sys_sendmsg(a0, compat_ptr(a1), a[2]);
671 break;
672 case SYS_RECVMSG:
673 ret = compat_sys_recvmsg(a0, compat_ptr(a1), a[2]);
674 break;
675 default:
676 ret = -EINVAL;
677 break;
678 }
679 return ret;
680}