jme: Fix device PM wakeup API usage
[linux/fpc-iii.git] / net / compat.c
blob53e933eb78b8b2e4269317ea2176210c2ac3fe3e
1 /*
2 * 32bit Socket syscall emulation. Based on arch/sparc64/kernel/sys_sparc32.c.
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
14 #include <linux/kernel.h>
15 #include <linux/gfp.h>
16 #include <linux/fs.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/security.h>
25 #include <linux/export.h>
27 #include <net/scm.h>
28 #include <net/sock.h>
29 #include <net/ip.h>
30 #include <net/ipv6.h>
31 #include <asm/uaccess.h>
32 #include <net/compat.h>
34 static inline int iov_from_user_compat_to_kern(struct iovec *kiov,
35 struct compat_iovec __user *uiov32,
36 int niov)
38 int tot_len = 0;
40 while (niov > 0) {
41 compat_uptr_t buf;
42 compat_size_t len;
44 if (get_user(len, &uiov32->iov_len) ||
45 get_user(buf, &uiov32->iov_base))
46 return -EFAULT;
48 if (len > INT_MAX - tot_len)
49 len = INT_MAX - tot_len;
51 tot_len += len;
52 kiov->iov_base = compat_ptr(buf);
53 kiov->iov_len = (__kernel_size_t) len;
54 uiov32++;
55 kiov++;
56 niov--;
58 return tot_len;
61 int get_compat_msghdr(struct msghdr *kmsg, struct compat_msghdr __user *umsg)
63 compat_uptr_t tmp1, tmp2, tmp3;
65 if (!access_ok(VERIFY_READ, umsg, sizeof(*umsg)) ||
66 __get_user(tmp1, &umsg->msg_name) ||
67 __get_user(kmsg->msg_namelen, &umsg->msg_namelen) ||
68 __get_user(tmp2, &umsg->msg_iov) ||
69 __get_user(kmsg->msg_iovlen, &umsg->msg_iovlen) ||
70 __get_user(tmp3, &umsg->msg_control) ||
71 __get_user(kmsg->msg_controllen, &umsg->msg_controllen) ||
72 __get_user(kmsg->msg_flags, &umsg->msg_flags))
73 return -EFAULT;
75 if (!tmp1)
76 kmsg->msg_namelen = 0;
78 if (kmsg->msg_namelen < 0)
79 return -EINVAL;
81 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
82 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
83 kmsg->msg_name = compat_ptr(tmp1);
84 kmsg->msg_iov = compat_ptr(tmp2);
85 kmsg->msg_control = compat_ptr(tmp3);
86 return 0;
89 /* I've named the args so it is easy to tell whose space the pointers are in. */
90 int verify_compat_iovec(struct msghdr *kern_msg, struct iovec *kern_iov,
91 struct sockaddr_storage *kern_address, int mode)
93 int tot_len;
95 if (kern_msg->msg_name && kern_msg->msg_namelen) {
96 if (mode == VERIFY_READ) {
97 int err = move_addr_to_kernel(kern_msg->msg_name,
98 kern_msg->msg_namelen,
99 kern_address);
100 if (err < 0)
101 return err;
103 kern_msg->msg_name = kern_address;
104 } else {
105 kern_msg->msg_name = NULL;
106 kern_msg->msg_namelen = 0;
109 tot_len = iov_from_user_compat_to_kern(kern_iov,
110 (struct compat_iovec __user *)kern_msg->msg_iov,
111 kern_msg->msg_iovlen);
112 if (tot_len >= 0)
113 kern_msg->msg_iov = kern_iov;
115 return tot_len;
118 /* Bleech... */
119 #define CMSG_COMPAT_ALIGN(len) ALIGN((len), sizeof(s32))
121 #define CMSG_COMPAT_DATA(cmsg) \
122 ((void __user *)((char __user *)(cmsg) + CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr))))
123 #define CMSG_COMPAT_SPACE(len) \
124 (CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)) + CMSG_COMPAT_ALIGN(len))
125 #define CMSG_COMPAT_LEN(len) \
126 (CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)) + (len))
128 #define CMSG_COMPAT_FIRSTHDR(msg) \
129 (((msg)->msg_controllen) >= sizeof(struct compat_cmsghdr) ? \
130 (struct compat_cmsghdr __user *)((msg)->msg_control) : \
131 (struct compat_cmsghdr __user *)NULL)
133 #define CMSG_COMPAT_OK(ucmlen, ucmsg, mhdr) \
134 ((ucmlen) >= sizeof(struct compat_cmsghdr) && \
135 (ucmlen) <= (unsigned long) \
136 ((mhdr)->msg_controllen - \
137 ((char *)(ucmsg) - (char *)(mhdr)->msg_control)))
139 static inline struct compat_cmsghdr __user *cmsg_compat_nxthdr(struct msghdr *msg,
140 struct compat_cmsghdr __user *cmsg, int cmsg_len)
142 char __user *ptr = (char __user *)cmsg + CMSG_COMPAT_ALIGN(cmsg_len);
143 if ((unsigned long)(ptr + 1 - (char __user *)msg->msg_control) >
144 msg->msg_controllen)
145 return NULL;
146 return (struct compat_cmsghdr __user *)ptr;
149 /* There is a lot of hair here because the alignment rules (and
150 * thus placement) of cmsg headers and length are different for
151 * 32-bit apps. -DaveM
153 int cmsghdr_from_user_compat_to_kern(struct msghdr *kmsg, struct sock *sk,
154 unsigned char *stackbuf, int stackbuf_size)
156 struct compat_cmsghdr __user *ucmsg;
157 struct cmsghdr *kcmsg, *kcmsg_base;
158 compat_size_t ucmlen;
159 __kernel_size_t kcmlen, tmp;
160 int err = -EFAULT;
162 kcmlen = 0;
163 kcmsg_base = kcmsg = (struct cmsghdr *)stackbuf;
164 ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
165 while (ucmsg != NULL) {
166 if (get_user(ucmlen, &ucmsg->cmsg_len))
167 return -EFAULT;
169 /* Catch bogons. */
170 if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg))
171 return -EINVAL;
173 tmp = ((ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg))) +
174 CMSG_ALIGN(sizeof(struct cmsghdr)));
175 tmp = CMSG_ALIGN(tmp);
176 kcmlen += tmp;
177 ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
179 if (kcmlen == 0)
180 return -EINVAL;
182 /* The kcmlen holds the 64-bit version of the control length.
183 * It may not be modified as we do not stick it into the kmsg
184 * until we have successfully copied over all of the data
185 * from the user.
187 if (kcmlen > stackbuf_size)
188 kcmsg_base = kcmsg = sock_kmalloc(sk, kcmlen, GFP_KERNEL);
189 if (kcmsg == NULL)
190 return -ENOBUFS;
192 /* Now copy them over neatly. */
193 memset(kcmsg, 0, kcmlen);
194 ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
195 while (ucmsg != NULL) {
196 if (__get_user(ucmlen, &ucmsg->cmsg_len))
197 goto Efault;
198 if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg))
199 goto Einval;
200 tmp = ((ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg))) +
201 CMSG_ALIGN(sizeof(struct cmsghdr)));
202 if ((char *)kcmsg_base + kcmlen - (char *)kcmsg < CMSG_ALIGN(tmp))
203 goto Einval;
204 kcmsg->cmsg_len = tmp;
205 tmp = CMSG_ALIGN(tmp);
206 if (__get_user(kcmsg->cmsg_level, &ucmsg->cmsg_level) ||
207 __get_user(kcmsg->cmsg_type, &ucmsg->cmsg_type) ||
208 copy_from_user(CMSG_DATA(kcmsg),
209 CMSG_COMPAT_DATA(ucmsg),
210 (ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg)))))
211 goto Efault;
213 /* Advance. */
214 kcmsg = (struct cmsghdr *)((char *)kcmsg + tmp);
215 ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
218 /* Ok, looks like we made it. Hook it up and return success. */
219 kmsg->msg_control = kcmsg_base;
220 kmsg->msg_controllen = kcmlen;
221 return 0;
223 Einval:
224 err = -EINVAL;
225 Efault:
226 if (kcmsg_base != (struct cmsghdr *)stackbuf)
227 sock_kfree_s(sk, kcmsg_base, kcmlen);
228 return err;
231 int put_cmsg_compat(struct msghdr *kmsg, int level, int type, int len, void *data)
233 struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control;
234 struct compat_cmsghdr cmhdr;
235 struct compat_timeval ctv;
236 struct compat_timespec cts[3];
237 int cmlen;
239 if (cm == NULL || kmsg->msg_controllen < sizeof(*cm)) {
240 kmsg->msg_flags |= MSG_CTRUNC;
241 return 0; /* XXX: return error? check spec. */
244 if (!COMPAT_USE_64BIT_TIME) {
245 if (level == SOL_SOCKET && type == SCM_TIMESTAMP) {
246 struct timeval *tv = (struct timeval *)data;
247 ctv.tv_sec = tv->tv_sec;
248 ctv.tv_usec = tv->tv_usec;
249 data = &ctv;
250 len = sizeof(ctv);
252 if (level == SOL_SOCKET &&
253 (type == SCM_TIMESTAMPNS || type == SCM_TIMESTAMPING)) {
254 int count = type == SCM_TIMESTAMPNS ? 1 : 3;
255 int i;
256 struct timespec *ts = (struct timespec *)data;
257 for (i = 0; i < count; i++) {
258 cts[i].tv_sec = ts[i].tv_sec;
259 cts[i].tv_nsec = ts[i].tv_nsec;
261 data = &cts;
262 len = sizeof(cts[0]) * count;
266 cmlen = CMSG_COMPAT_LEN(len);
267 if (kmsg->msg_controllen < cmlen) {
268 kmsg->msg_flags |= MSG_CTRUNC;
269 cmlen = kmsg->msg_controllen;
271 cmhdr.cmsg_level = level;
272 cmhdr.cmsg_type = type;
273 cmhdr.cmsg_len = cmlen;
275 if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
276 return -EFAULT;
277 if (copy_to_user(CMSG_COMPAT_DATA(cm), data, cmlen - sizeof(struct compat_cmsghdr)))
278 return -EFAULT;
279 cmlen = CMSG_COMPAT_SPACE(len);
280 if (kmsg->msg_controllen < cmlen)
281 cmlen = kmsg->msg_controllen;
282 kmsg->msg_control += cmlen;
283 kmsg->msg_controllen -= cmlen;
284 return 0;
287 void scm_detach_fds_compat(struct msghdr *kmsg, struct scm_cookie *scm)
289 struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control;
290 int fdmax = (kmsg->msg_controllen - sizeof(struct compat_cmsghdr)) / sizeof(int);
291 int fdnum = scm->fp->count;
292 struct file **fp = scm->fp->fp;
293 int __user *cmfptr;
294 int err = 0, i;
296 if (fdnum < fdmax)
297 fdmax = fdnum;
299 for (i = 0, cmfptr = (int __user *) CMSG_COMPAT_DATA(cm); i < fdmax; i++, cmfptr++) {
300 int new_fd;
301 err = security_file_receive(fp[i]);
302 if (err)
303 break;
304 err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & kmsg->msg_flags
305 ? O_CLOEXEC : 0);
306 if (err < 0)
307 break;
308 new_fd = err;
309 err = put_user(new_fd, cmfptr);
310 if (err) {
311 put_unused_fd(new_fd);
312 break;
314 /* Bump the usage count and install the file. */
315 fd_install(new_fd, get_file(fp[i]));
318 if (i > 0) {
319 int cmlen = CMSG_COMPAT_LEN(i * sizeof(int));
320 err = put_user(SOL_SOCKET, &cm->cmsg_level);
321 if (!err)
322 err = put_user(SCM_RIGHTS, &cm->cmsg_type);
323 if (!err)
324 err = put_user(cmlen, &cm->cmsg_len);
325 if (!err) {
326 cmlen = CMSG_COMPAT_SPACE(i * sizeof(int));
327 kmsg->msg_control += cmlen;
328 kmsg->msg_controllen -= cmlen;
331 if (i < fdnum)
332 kmsg->msg_flags |= MSG_CTRUNC;
335 * All of the files that fit in the message have had their
336 * usage counts incremented, so we just free the list.
338 __scm_destroy(scm);
341 static int do_set_attach_filter(struct socket *sock, int level, int optname,
342 char __user *optval, unsigned int optlen)
344 struct compat_sock_fprog __user *fprog32 = (struct compat_sock_fprog __user *)optval;
345 struct sock_fprog __user *kfprog = compat_alloc_user_space(sizeof(struct sock_fprog));
346 compat_uptr_t ptr;
347 u16 len;
349 if (!access_ok(VERIFY_READ, fprog32, sizeof(*fprog32)) ||
350 !access_ok(VERIFY_WRITE, kfprog, sizeof(struct sock_fprog)) ||
351 __get_user(len, &fprog32->len) ||
352 __get_user(ptr, &fprog32->filter) ||
353 __put_user(len, &kfprog->len) ||
354 __put_user(compat_ptr(ptr), &kfprog->filter))
355 return -EFAULT;
357 return sock_setsockopt(sock, level, optname, (char __user *)kfprog,
358 sizeof(struct sock_fprog));
361 static int do_set_sock_timeout(struct socket *sock, int level,
362 int optname, char __user *optval, unsigned int optlen)
364 struct compat_timeval __user *up = (struct compat_timeval __user *)optval;
365 struct timeval ktime;
366 mm_segment_t old_fs;
367 int err;
369 if (optlen < sizeof(*up))
370 return -EINVAL;
371 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
372 __get_user(ktime.tv_sec, &up->tv_sec) ||
373 __get_user(ktime.tv_usec, &up->tv_usec))
374 return -EFAULT;
375 old_fs = get_fs();
376 set_fs(KERNEL_DS);
377 err = sock_setsockopt(sock, level, optname, (char *)&ktime, sizeof(ktime));
378 set_fs(old_fs);
380 return err;
383 static int compat_sock_setsockopt(struct socket *sock, int level, int optname,
384 char __user *optval, unsigned int optlen)
386 if (optname == SO_ATTACH_FILTER)
387 return do_set_attach_filter(sock, level, optname,
388 optval, optlen);
389 if (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)
390 return do_set_sock_timeout(sock, level, optname, optval, optlen);
392 return sock_setsockopt(sock, level, optname, optval, optlen);
395 COMPAT_SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
396 char __user *, optval, unsigned int, optlen)
398 int err;
399 struct socket *sock = sockfd_lookup(fd, &err);
401 if (sock) {
402 err = security_socket_setsockopt(sock, level, optname);
403 if (err) {
404 sockfd_put(sock);
405 return err;
408 if (level == SOL_SOCKET)
409 err = compat_sock_setsockopt(sock, level,
410 optname, optval, optlen);
411 else if (sock->ops->compat_setsockopt)
412 err = sock->ops->compat_setsockopt(sock, level,
413 optname, optval, optlen);
414 else
415 err = sock->ops->setsockopt(sock, level,
416 optname, optval, optlen);
417 sockfd_put(sock);
419 return err;
422 static int do_get_sock_timeout(struct socket *sock, int level, int optname,
423 char __user *optval, int __user *optlen)
425 struct compat_timeval __user *up;
426 struct timeval ktime;
427 mm_segment_t old_fs;
428 int len, err;
430 up = (struct compat_timeval __user *) optval;
431 if (get_user(len, optlen))
432 return -EFAULT;
433 if (len < sizeof(*up))
434 return -EINVAL;
435 len = sizeof(ktime);
436 old_fs = get_fs();
437 set_fs(KERNEL_DS);
438 err = sock_getsockopt(sock, level, optname, (char *) &ktime, &len);
439 set_fs(old_fs);
441 if (!err) {
442 if (put_user(sizeof(*up), optlen) ||
443 !access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
444 __put_user(ktime.tv_sec, &up->tv_sec) ||
445 __put_user(ktime.tv_usec, &up->tv_usec))
446 err = -EFAULT;
448 return err;
451 static int compat_sock_getsockopt(struct socket *sock, int level, int optname,
452 char __user *optval, int __user *optlen)
454 if (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)
455 return do_get_sock_timeout(sock, level, optname, optval, optlen);
456 return sock_getsockopt(sock, level, optname, optval, optlen);
459 int compat_sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
461 struct compat_timeval __user *ctv;
462 int err;
463 struct timeval tv;
465 if (COMPAT_USE_64BIT_TIME)
466 return sock_get_timestamp(sk, userstamp);
468 ctv = (struct compat_timeval __user *) userstamp;
469 err = -ENOENT;
470 if (!sock_flag(sk, SOCK_TIMESTAMP))
471 sock_enable_timestamp(sk, SOCK_TIMESTAMP);
472 tv = ktime_to_timeval(sk->sk_stamp);
473 if (tv.tv_sec == -1)
474 return err;
475 if (tv.tv_sec == 0) {
476 sk->sk_stamp = ktime_get_real();
477 tv = ktime_to_timeval(sk->sk_stamp);
479 err = 0;
480 if (put_user(tv.tv_sec, &ctv->tv_sec) ||
481 put_user(tv.tv_usec, &ctv->tv_usec))
482 err = -EFAULT;
483 return err;
485 EXPORT_SYMBOL(compat_sock_get_timestamp);
487 int compat_sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
489 struct compat_timespec __user *ctv;
490 int err;
491 struct timespec ts;
493 if (COMPAT_USE_64BIT_TIME)
494 return sock_get_timestampns (sk, userstamp);
496 ctv = (struct compat_timespec __user *) userstamp;
497 err = -ENOENT;
498 if (!sock_flag(sk, SOCK_TIMESTAMP))
499 sock_enable_timestamp(sk, SOCK_TIMESTAMP);
500 ts = ktime_to_timespec(sk->sk_stamp);
501 if (ts.tv_sec == -1)
502 return err;
503 if (ts.tv_sec == 0) {
504 sk->sk_stamp = ktime_get_real();
505 ts = ktime_to_timespec(sk->sk_stamp);
507 err = 0;
508 if (put_user(ts.tv_sec, &ctv->tv_sec) ||
509 put_user(ts.tv_nsec, &ctv->tv_nsec))
510 err = -EFAULT;
511 return err;
513 EXPORT_SYMBOL(compat_sock_get_timestampns);
515 COMPAT_SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
516 char __user *, optval, int __user *, optlen)
518 int err;
519 struct socket *sock = sockfd_lookup(fd, &err);
521 if (sock) {
522 err = security_socket_getsockopt(sock, level, optname);
523 if (err) {
524 sockfd_put(sock);
525 return err;
528 if (level == SOL_SOCKET)
529 err = compat_sock_getsockopt(sock, level,
530 optname, optval, optlen);
531 else if (sock->ops->compat_getsockopt)
532 err = sock->ops->compat_getsockopt(sock, level,
533 optname, optval, optlen);
534 else
535 err = sock->ops->getsockopt(sock, level,
536 optname, optval, optlen);
537 sockfd_put(sock);
539 return err;
542 struct compat_group_req {
543 __u32 gr_interface;
544 struct __kernel_sockaddr_storage gr_group
545 __attribute__ ((aligned(4)));
546 } __packed;
548 struct compat_group_source_req {
549 __u32 gsr_interface;
550 struct __kernel_sockaddr_storage gsr_group
551 __attribute__ ((aligned(4)));
552 struct __kernel_sockaddr_storage gsr_source
553 __attribute__ ((aligned(4)));
554 } __packed;
556 struct compat_group_filter {
557 __u32 gf_interface;
558 struct __kernel_sockaddr_storage gf_group
559 __attribute__ ((aligned(4)));
560 __u32 gf_fmode;
561 __u32 gf_numsrc;
562 struct __kernel_sockaddr_storage gf_slist[1]
563 __attribute__ ((aligned(4)));
564 } __packed;
566 #define __COMPAT_GF0_SIZE (sizeof(struct compat_group_filter) - \
567 sizeof(struct __kernel_sockaddr_storage))
570 int compat_mc_setsockopt(struct sock *sock, int level, int optname,
571 char __user *optval, unsigned int optlen,
572 int (*setsockopt)(struct sock *, int, int, char __user *, unsigned int))
574 char __user *koptval = optval;
575 int koptlen = optlen;
577 switch (optname) {
578 case MCAST_JOIN_GROUP:
579 case MCAST_LEAVE_GROUP:
581 struct compat_group_req __user *gr32 = (void *)optval;
582 struct group_req __user *kgr =
583 compat_alloc_user_space(sizeof(struct group_req));
584 u32 interface;
586 if (!access_ok(VERIFY_READ, gr32, sizeof(*gr32)) ||
587 !access_ok(VERIFY_WRITE, kgr, sizeof(struct group_req)) ||
588 __get_user(interface, &gr32->gr_interface) ||
589 __put_user(interface, &kgr->gr_interface) ||
590 copy_in_user(&kgr->gr_group, &gr32->gr_group,
591 sizeof(kgr->gr_group)))
592 return -EFAULT;
593 koptval = (char __user *)kgr;
594 koptlen = sizeof(struct group_req);
595 break;
597 case MCAST_JOIN_SOURCE_GROUP:
598 case MCAST_LEAVE_SOURCE_GROUP:
599 case MCAST_BLOCK_SOURCE:
600 case MCAST_UNBLOCK_SOURCE:
602 struct compat_group_source_req __user *gsr32 = (void *)optval;
603 struct group_source_req __user *kgsr = compat_alloc_user_space(
604 sizeof(struct group_source_req));
605 u32 interface;
607 if (!access_ok(VERIFY_READ, gsr32, sizeof(*gsr32)) ||
608 !access_ok(VERIFY_WRITE, kgsr,
609 sizeof(struct group_source_req)) ||
610 __get_user(interface, &gsr32->gsr_interface) ||
611 __put_user(interface, &kgsr->gsr_interface) ||
612 copy_in_user(&kgsr->gsr_group, &gsr32->gsr_group,
613 sizeof(kgsr->gsr_group)) ||
614 copy_in_user(&kgsr->gsr_source, &gsr32->gsr_source,
615 sizeof(kgsr->gsr_source)))
616 return -EFAULT;
617 koptval = (char __user *)kgsr;
618 koptlen = sizeof(struct group_source_req);
619 break;
621 case MCAST_MSFILTER:
623 struct compat_group_filter __user *gf32 = (void *)optval;
624 struct group_filter __user *kgf;
625 u32 interface, fmode, numsrc;
627 if (!access_ok(VERIFY_READ, gf32, __COMPAT_GF0_SIZE) ||
628 __get_user(interface, &gf32->gf_interface) ||
629 __get_user(fmode, &gf32->gf_fmode) ||
630 __get_user(numsrc, &gf32->gf_numsrc))
631 return -EFAULT;
632 koptlen = optlen + sizeof(struct group_filter) -
633 sizeof(struct compat_group_filter);
634 if (koptlen < GROUP_FILTER_SIZE(numsrc))
635 return -EINVAL;
636 kgf = compat_alloc_user_space(koptlen);
637 if (!access_ok(VERIFY_WRITE, kgf, koptlen) ||
638 __put_user(interface, &kgf->gf_interface) ||
639 __put_user(fmode, &kgf->gf_fmode) ||
640 __put_user(numsrc, &kgf->gf_numsrc) ||
641 copy_in_user(&kgf->gf_group, &gf32->gf_group,
642 sizeof(kgf->gf_group)) ||
643 (numsrc && copy_in_user(kgf->gf_slist, gf32->gf_slist,
644 numsrc * sizeof(kgf->gf_slist[0]))))
645 return -EFAULT;
646 koptval = (char __user *)kgf;
647 break;
650 default:
651 break;
653 return setsockopt(sock, level, optname, koptval, koptlen);
655 EXPORT_SYMBOL(compat_mc_setsockopt);
657 int compat_mc_getsockopt(struct sock *sock, int level, int optname,
658 char __user *optval, int __user *optlen,
659 int (*getsockopt)(struct sock *, int, int, char __user *, int __user *))
661 struct compat_group_filter __user *gf32 = (void *)optval;
662 struct group_filter __user *kgf;
663 int __user *koptlen;
664 u32 interface, fmode, numsrc;
665 int klen, ulen, err;
667 if (optname != MCAST_MSFILTER)
668 return getsockopt(sock, level, optname, optval, optlen);
670 koptlen = compat_alloc_user_space(sizeof(*koptlen));
671 if (!access_ok(VERIFY_READ, optlen, sizeof(*optlen)) ||
672 __get_user(ulen, optlen))
673 return -EFAULT;
675 /* adjust len for pad */
676 klen = ulen + sizeof(*kgf) - sizeof(*gf32);
678 if (klen < GROUP_FILTER_SIZE(0))
679 return -EINVAL;
681 if (!access_ok(VERIFY_WRITE, koptlen, sizeof(*koptlen)) ||
682 __put_user(klen, koptlen))
683 return -EFAULT;
685 /* have to allow space for previous compat_alloc_user_space, too */
686 kgf = compat_alloc_user_space(klen+sizeof(*optlen));
688 if (!access_ok(VERIFY_READ, gf32, __COMPAT_GF0_SIZE) ||
689 __get_user(interface, &gf32->gf_interface) ||
690 __get_user(fmode, &gf32->gf_fmode) ||
691 __get_user(numsrc, &gf32->gf_numsrc) ||
692 __put_user(interface, &kgf->gf_interface) ||
693 __put_user(fmode, &kgf->gf_fmode) ||
694 __put_user(numsrc, &kgf->gf_numsrc) ||
695 copy_in_user(&kgf->gf_group, &gf32->gf_group, sizeof(kgf->gf_group)))
696 return -EFAULT;
698 err = getsockopt(sock, level, optname, (char __user *)kgf, koptlen);
699 if (err)
700 return err;
702 if (!access_ok(VERIFY_READ, koptlen, sizeof(*koptlen)) ||
703 __get_user(klen, koptlen))
704 return -EFAULT;
706 ulen = klen - (sizeof(*kgf)-sizeof(*gf32));
708 if (!access_ok(VERIFY_WRITE, optlen, sizeof(*optlen)) ||
709 __put_user(ulen, optlen))
710 return -EFAULT;
712 if (!access_ok(VERIFY_READ, kgf, klen) ||
713 !access_ok(VERIFY_WRITE, gf32, ulen) ||
714 __get_user(interface, &kgf->gf_interface) ||
715 __get_user(fmode, &kgf->gf_fmode) ||
716 __get_user(numsrc, &kgf->gf_numsrc) ||
717 __put_user(interface, &gf32->gf_interface) ||
718 __put_user(fmode, &gf32->gf_fmode) ||
719 __put_user(numsrc, &gf32->gf_numsrc))
720 return -EFAULT;
721 if (numsrc) {
722 int copylen;
724 klen -= GROUP_FILTER_SIZE(0);
725 copylen = numsrc * sizeof(gf32->gf_slist[0]);
726 if (copylen > klen)
727 copylen = klen;
728 if (copy_in_user(gf32->gf_slist, kgf->gf_slist, copylen))
729 return -EFAULT;
731 return err;
733 EXPORT_SYMBOL(compat_mc_getsockopt);
736 /* Argument list sizes for compat_sys_socketcall */
737 #define AL(x) ((x) * sizeof(u32))
738 static unsigned char nas[21] = {
739 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
740 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
741 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
742 AL(4), AL(5), AL(4)
744 #undef AL
746 COMPAT_SYSCALL_DEFINE3(sendmsg, int, fd, struct compat_msghdr __user *, msg, unsigned int, flags)
748 return __sys_sendmsg(fd, (struct msghdr __user *)msg, flags | MSG_CMSG_COMPAT);
751 COMPAT_SYSCALL_DEFINE4(sendmmsg, int, fd, struct compat_mmsghdr __user *, mmsg,
752 unsigned int, vlen, unsigned int, flags)
754 return __sys_sendmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
755 flags | MSG_CMSG_COMPAT);
758 COMPAT_SYSCALL_DEFINE3(recvmsg, int, fd, struct compat_msghdr __user *, msg, unsigned int, flags)
760 return __sys_recvmsg(fd, (struct msghdr __user *)msg, flags | MSG_CMSG_COMPAT);
763 COMPAT_SYSCALL_DEFINE4(recv, int, fd, void __user *, buf, compat_size_t, len, unsigned int, flags)
765 return sys_recv(fd, buf, len, flags | MSG_CMSG_COMPAT);
768 COMPAT_SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, buf, compat_size_t, len,
769 unsigned int, flags, struct sockaddr __user *, addr,
770 int __user *, addrlen)
772 return sys_recvfrom(fd, buf, len, flags | MSG_CMSG_COMPAT, addr, addrlen);
775 COMPAT_SYSCALL_DEFINE5(recvmmsg, int, fd, struct compat_mmsghdr __user *, mmsg,
776 unsigned int, vlen, unsigned int, flags,
777 struct compat_timespec __user *, timeout)
779 int datagrams;
780 struct timespec ktspec;
782 if (timeout == NULL)
783 return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
784 flags | MSG_CMSG_COMPAT, NULL);
786 if (compat_get_timespec(&ktspec, timeout))
787 return -EFAULT;
789 datagrams = __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
790 flags | MSG_CMSG_COMPAT, &ktspec);
791 if (datagrams > 0 && compat_put_timespec(&ktspec, timeout))
792 datagrams = -EFAULT;
794 return datagrams;
797 COMPAT_SYSCALL_DEFINE2(socketcall, int, call, u32 __user *, args)
799 int ret;
800 u32 a[6];
801 u32 a0, a1;
803 if (call < SYS_SOCKET || call > SYS_SENDMMSG)
804 return -EINVAL;
805 if (copy_from_user(a, args, nas[call]))
806 return -EFAULT;
807 a0 = a[0];
808 a1 = a[1];
810 switch (call) {
811 case SYS_SOCKET:
812 ret = sys_socket(a0, a1, a[2]);
813 break;
814 case SYS_BIND:
815 ret = sys_bind(a0, compat_ptr(a1), a[2]);
816 break;
817 case SYS_CONNECT:
818 ret = sys_connect(a0, compat_ptr(a1), a[2]);
819 break;
820 case SYS_LISTEN:
821 ret = sys_listen(a0, a1);
822 break;
823 case SYS_ACCEPT:
824 ret = sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), 0);
825 break;
826 case SYS_GETSOCKNAME:
827 ret = sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2]));
828 break;
829 case SYS_GETPEERNAME:
830 ret = sys_getpeername(a0, compat_ptr(a1), compat_ptr(a[2]));
831 break;
832 case SYS_SOCKETPAIR:
833 ret = sys_socketpair(a0, a1, a[2], compat_ptr(a[3]));
834 break;
835 case SYS_SEND:
836 ret = sys_send(a0, compat_ptr(a1), a[2], a[3]);
837 break;
838 case SYS_SENDTO:
839 ret = sys_sendto(a0, compat_ptr(a1), a[2], a[3], compat_ptr(a[4]), a[5]);
840 break;
841 case SYS_RECV:
842 ret = compat_sys_recv(a0, compat_ptr(a1), a[2], a[3]);
843 break;
844 case SYS_RECVFROM:
845 ret = compat_sys_recvfrom(a0, compat_ptr(a1), a[2], a[3],
846 compat_ptr(a[4]), compat_ptr(a[5]));
847 break;
848 case SYS_SHUTDOWN:
849 ret = sys_shutdown(a0, a1);
850 break;
851 case SYS_SETSOCKOPT:
852 ret = compat_sys_setsockopt(a0, a1, a[2],
853 compat_ptr(a[3]), a[4]);
854 break;
855 case SYS_GETSOCKOPT:
856 ret = compat_sys_getsockopt(a0, a1, a[2],
857 compat_ptr(a[3]), compat_ptr(a[4]));
858 break;
859 case SYS_SENDMSG:
860 ret = compat_sys_sendmsg(a0, compat_ptr(a1), a[2]);
861 break;
862 case SYS_SENDMMSG:
863 ret = compat_sys_sendmmsg(a0, compat_ptr(a1), a[2], a[3]);
864 break;
865 case SYS_RECVMSG:
866 ret = compat_sys_recvmsg(a0, compat_ptr(a1), a[2]);
867 break;
868 case SYS_RECVMMSG:
869 ret = compat_sys_recvmmsg(a0, compat_ptr(a1), a[2], a[3],
870 compat_ptr(a[4]));
871 break;
872 case SYS_ACCEPT4:
873 ret = sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), a[3]);
874 break;
875 default:
876 ret = -EINVAL;
877 break;
879 return ret;