cnic: Improve NETDEV_UP event handling
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / compat.c
blob9559afc639c138a52d0d9ce8d49b83c7ce8abfaa
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/fs.h>
16 #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/security.h>
25 #include <net/scm.h>
26 #include <net/sock.h>
27 #include <net/ip.h>
28 #include <net/ipv6.h>
29 #include <asm/uaccess.h>
30 #include <net/compat.h>
32 static inline int iov_from_user_compat_to_kern(struct iovec *kiov,
33 struct compat_iovec __user *uiov32,
34 int niov)
36 int tot_len = 0;
38 while (niov > 0) {
39 compat_uptr_t buf;
40 compat_size_t len;
42 if (get_user(len, &uiov32->iov_len) ||
43 get_user(buf, &uiov32->iov_base))
44 return -EFAULT;
46 if (len > INT_MAX - tot_len)
47 len = INT_MAX - tot_len;
49 tot_len += len;
50 kiov->iov_base = compat_ptr(buf);
51 kiov->iov_len = (__kernel_size_t) len;
52 uiov32++;
53 kiov++;
54 niov--;
56 return tot_len;
59 int get_compat_msghdr(struct msghdr *kmsg, struct compat_msghdr __user *umsg)
61 compat_uptr_t tmp1, tmp2, tmp3;
63 if (!access_ok(VERIFY_READ, umsg, sizeof(*umsg)) ||
64 __get_user(tmp1, &umsg->msg_name) ||
65 __get_user(kmsg->msg_namelen, &umsg->msg_namelen) ||
66 __get_user(tmp2, &umsg->msg_iov) ||
67 __get_user(kmsg->msg_iovlen, &umsg->msg_iovlen) ||
68 __get_user(tmp3, &umsg->msg_control) ||
69 __get_user(kmsg->msg_controllen, &umsg->msg_controllen) ||
70 __get_user(kmsg->msg_flags, &umsg->msg_flags))
71 return -EFAULT;
72 kmsg->msg_name = compat_ptr(tmp1);
73 kmsg->msg_iov = compat_ptr(tmp2);
74 kmsg->msg_control = compat_ptr(tmp3);
75 return 0;
78 /* I've named the args so it is easy to tell whose space the pointers are in. */
79 int verify_compat_iovec(struct msghdr *kern_msg, struct iovec *kern_iov,
80 struct sockaddr *kern_address, int mode)
82 int tot_len;
84 if (kern_msg->msg_namelen) {
85 if (mode==VERIFY_READ) {
86 int err = move_addr_to_kernel(kern_msg->msg_name,
87 kern_msg->msg_namelen,
88 kern_address);
89 if (err < 0)
90 return err;
92 kern_msg->msg_name = kern_address;
93 } else
94 kern_msg->msg_name = NULL;
96 tot_len = iov_from_user_compat_to_kern(kern_iov,
97 (struct compat_iovec __user *)kern_msg->msg_iov,
98 kern_msg->msg_iovlen);
99 if (tot_len >= 0)
100 kern_msg->msg_iov = kern_iov;
102 return tot_len;
105 /* Bleech... */
106 #define CMSG_COMPAT_ALIGN(len) ALIGN((len), sizeof(s32))
108 #define CMSG_COMPAT_DATA(cmsg) \
109 ((void __user *)((char __user *)(cmsg) + CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr))))
110 #define CMSG_COMPAT_SPACE(len) \
111 (CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)) + CMSG_COMPAT_ALIGN(len))
112 #define CMSG_COMPAT_LEN(len) \
113 (CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)) + (len))
115 #define CMSG_COMPAT_FIRSTHDR(msg) \
116 (((msg)->msg_controllen) >= sizeof(struct compat_cmsghdr) ? \
117 (struct compat_cmsghdr __user *)((msg)->msg_control) : \
118 (struct compat_cmsghdr __user *)NULL)
120 #define CMSG_COMPAT_OK(ucmlen, ucmsg, mhdr) \
121 ((ucmlen) >= sizeof(struct compat_cmsghdr) && \
122 (ucmlen) <= (unsigned long) \
123 ((mhdr)->msg_controllen - \
124 ((char *)(ucmsg) - (char *)(mhdr)->msg_control)))
126 static inline struct compat_cmsghdr __user *cmsg_compat_nxthdr(struct msghdr *msg,
127 struct compat_cmsghdr __user *cmsg, int cmsg_len)
129 char __user *ptr = (char __user *)cmsg + CMSG_COMPAT_ALIGN(cmsg_len);
130 if ((unsigned long)(ptr + 1 - (char __user *)msg->msg_control) >
131 msg->msg_controllen)
132 return NULL;
133 return (struct compat_cmsghdr __user *)ptr;
136 /* There is a lot of hair here because the alignment rules (and
137 * thus placement) of cmsg headers and length are different for
138 * 32-bit apps. -DaveM
140 int cmsghdr_from_user_compat_to_kern(struct msghdr *kmsg, struct sock *sk,
141 unsigned char *stackbuf, int stackbuf_size)
143 struct compat_cmsghdr __user *ucmsg;
144 struct cmsghdr *kcmsg, *kcmsg_base;
145 compat_size_t ucmlen;
146 __kernel_size_t kcmlen, tmp;
147 int err = -EFAULT;
149 kcmlen = 0;
150 kcmsg_base = kcmsg = (struct cmsghdr *)stackbuf;
151 ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
152 while (ucmsg != NULL) {
153 if (get_user(ucmlen, &ucmsg->cmsg_len))
154 return -EFAULT;
156 /* Catch bogons. */
157 if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg))
158 return -EINVAL;
160 tmp = ((ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg))) +
161 CMSG_ALIGN(sizeof(struct cmsghdr)));
162 tmp = CMSG_ALIGN(tmp);
163 kcmlen += tmp;
164 ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
166 if (kcmlen == 0)
167 return -EINVAL;
169 /* The kcmlen holds the 64-bit version of the control length.
170 * It may not be modified as we do not stick it into the kmsg
171 * until we have successfully copied over all of the data
172 * from the user.
174 if (kcmlen > stackbuf_size)
175 kcmsg_base = kcmsg = sock_kmalloc(sk, kcmlen, GFP_KERNEL);
176 if (kcmsg == NULL)
177 return -ENOBUFS;
179 /* Now copy them over neatly. */
180 memset(kcmsg, 0, kcmlen);
181 ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
182 while (ucmsg != NULL) {
183 if (__get_user(ucmlen, &ucmsg->cmsg_len))
184 goto Efault;
185 if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg))
186 goto Einval;
187 tmp = ((ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg))) +
188 CMSG_ALIGN(sizeof(struct cmsghdr)));
189 if ((char *)kcmsg_base + kcmlen - (char *)kcmsg < CMSG_ALIGN(tmp))
190 goto Einval;
191 kcmsg->cmsg_len = tmp;
192 tmp = CMSG_ALIGN(tmp);
193 if (__get_user(kcmsg->cmsg_level, &ucmsg->cmsg_level) ||
194 __get_user(kcmsg->cmsg_type, &ucmsg->cmsg_type) ||
195 copy_from_user(CMSG_DATA(kcmsg),
196 CMSG_COMPAT_DATA(ucmsg),
197 (ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg)))))
198 goto Efault;
200 /* Advance. */
201 kcmsg = (struct cmsghdr *)((char *)kcmsg + tmp);
202 ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
205 /* Ok, looks like we made it. Hook it up and return success. */
206 kmsg->msg_control = kcmsg_base;
207 kmsg->msg_controllen = kcmlen;
208 return 0;
210 Einval:
211 err = -EINVAL;
212 Efault:
213 if (kcmsg_base != (struct cmsghdr *)stackbuf)
214 sock_kfree_s(sk, kcmsg_base, kcmlen);
215 return err;
218 int put_cmsg_compat(struct msghdr *kmsg, int level, int type, int len, void *data)
220 struct compat_timeval ctv;
221 struct compat_timespec cts[3];
222 struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control;
223 struct compat_cmsghdr cmhdr;
224 int cmlen;
226 if (cm == NULL || kmsg->msg_controllen < sizeof(*cm)) {
227 kmsg->msg_flags |= MSG_CTRUNC;
228 return 0; /* XXX: return error? check spec. */
231 if (level == SOL_SOCKET && type == SCM_TIMESTAMP) {
232 struct timeval *tv = (struct timeval *)data;
233 ctv.tv_sec = tv->tv_sec;
234 ctv.tv_usec = tv->tv_usec;
235 data = &ctv;
236 len = sizeof(ctv);
238 if (level == SOL_SOCKET &&
239 (type == SCM_TIMESTAMPNS || type == SCM_TIMESTAMPING)) {
240 int count = type == SCM_TIMESTAMPNS ? 1 : 3;
241 int i;
242 struct timespec *ts = (struct timespec *)data;
243 for (i = 0; i < count; i++) {
244 cts[i].tv_sec = ts[i].tv_sec;
245 cts[i].tv_nsec = ts[i].tv_nsec;
247 data = &cts;
248 len = sizeof(cts[0]) * count;
251 cmlen = CMSG_COMPAT_LEN(len);
252 if (kmsg->msg_controllen < cmlen) {
253 kmsg->msg_flags |= MSG_CTRUNC;
254 cmlen = kmsg->msg_controllen;
256 cmhdr.cmsg_level = level;
257 cmhdr.cmsg_type = type;
258 cmhdr.cmsg_len = cmlen;
260 if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
261 return -EFAULT;
262 if (copy_to_user(CMSG_COMPAT_DATA(cm), data, cmlen - sizeof(struct compat_cmsghdr)))
263 return -EFAULT;
264 cmlen = CMSG_COMPAT_SPACE(len);
265 if (kmsg->msg_controllen < cmlen)
266 cmlen = kmsg->msg_controllen;
267 kmsg->msg_control += cmlen;
268 kmsg->msg_controllen -= cmlen;
269 return 0;
272 void scm_detach_fds_compat(struct msghdr *kmsg, struct scm_cookie *scm)
274 struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control;
275 int fdmax = (kmsg->msg_controllen - sizeof(struct compat_cmsghdr)) / sizeof(int);
276 int fdnum = scm->fp->count;
277 struct file **fp = scm->fp->fp;
278 int __user *cmfptr;
279 int err = 0, i;
281 if (fdnum < fdmax)
282 fdmax = fdnum;
284 for (i = 0, cmfptr = (int __user *) CMSG_COMPAT_DATA(cm); i < fdmax; i++, cmfptr++) {
285 int new_fd;
286 err = security_file_receive(fp[i]);
287 if (err)
288 break;
289 err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & kmsg->msg_flags
290 ? O_CLOEXEC : 0);
291 if (err < 0)
292 break;
293 new_fd = err;
294 err = put_user(new_fd, cmfptr);
295 if (err) {
296 put_unused_fd(new_fd);
297 break;
299 /* Bump the usage count and install the file. */
300 get_file(fp[i]);
301 fd_install(new_fd, fp[i]);
304 if (i > 0) {
305 int cmlen = CMSG_COMPAT_LEN(i * sizeof(int));
306 err = put_user(SOL_SOCKET, &cm->cmsg_level);
307 if (!err)
308 err = put_user(SCM_RIGHTS, &cm->cmsg_type);
309 if (!err)
310 err = put_user(cmlen, &cm->cmsg_len);
311 if (!err) {
312 cmlen = CMSG_COMPAT_SPACE(i * sizeof(int));
313 kmsg->msg_control += cmlen;
314 kmsg->msg_controllen -= cmlen;
317 if (i < fdnum)
318 kmsg->msg_flags |= MSG_CTRUNC;
321 * All of the files that fit in the message have had their
322 * usage counts incremented, so we just free the list.
324 __scm_destroy(scm);
328 * A struct sock_filter is architecture independent.
330 struct compat_sock_fprog {
331 u16 len;
332 compat_uptr_t filter; /* struct sock_filter * */
335 static int do_set_attach_filter(struct socket *sock, int level, int optname,
336 char __user *optval, unsigned int optlen)
338 struct compat_sock_fprog __user *fprog32 = (struct compat_sock_fprog __user *)optval;
339 struct sock_fprog __user *kfprog = compat_alloc_user_space(sizeof(struct sock_fprog));
340 compat_uptr_t ptr;
341 u16 len;
343 if (!access_ok(VERIFY_READ, fprog32, sizeof(*fprog32)) ||
344 !access_ok(VERIFY_WRITE, kfprog, sizeof(struct sock_fprog)) ||
345 __get_user(len, &fprog32->len) ||
346 __get_user(ptr, &fprog32->filter) ||
347 __put_user(len, &kfprog->len) ||
348 __put_user(compat_ptr(ptr), &kfprog->filter))
349 return -EFAULT;
351 return sock_setsockopt(sock, level, optname, (char __user *)kfprog,
352 sizeof(struct sock_fprog));
355 static int do_set_sock_timeout(struct socket *sock, int level,
356 int optname, char __user *optval, unsigned int optlen)
358 struct compat_timeval __user *up = (struct compat_timeval __user *) optval;
359 struct timeval ktime;
360 mm_segment_t old_fs;
361 int err;
363 if (optlen < sizeof(*up))
364 return -EINVAL;
365 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
366 __get_user(ktime.tv_sec, &up->tv_sec) ||
367 __get_user(ktime.tv_usec, &up->tv_usec))
368 return -EFAULT;
369 old_fs = get_fs();
370 set_fs(KERNEL_DS);
371 err = sock_setsockopt(sock, level, optname, (char *) &ktime, sizeof(ktime));
372 set_fs(old_fs);
374 return err;
377 static int compat_sock_setsockopt(struct socket *sock, int level, int optname,
378 char __user *optval, unsigned int optlen)
380 if (optname == SO_ATTACH_FILTER)
381 return do_set_attach_filter(sock, level, optname,
382 optval, optlen);
383 if (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)
384 return do_set_sock_timeout(sock, level, optname, optval, optlen);
386 return sock_setsockopt(sock, level, optname, optval, optlen);
389 asmlinkage long compat_sys_setsockopt(int fd, int level, int optname,
390 char __user *optval, unsigned int optlen)
392 int err;
393 struct socket *sock;
395 if (optlen < 0)
396 return -EINVAL;
398 if ((sock = sockfd_lookup(fd, &err))!=NULL)
400 err = security_socket_setsockopt(sock,level,optname);
401 if (err) {
402 sockfd_put(sock);
403 return err;
406 if (level == SOL_SOCKET)
407 err = compat_sock_setsockopt(sock, level,
408 optname, optval, optlen);
409 else if (sock->ops->compat_setsockopt)
410 err = sock->ops->compat_setsockopt(sock, level,
411 optname, optval, optlen);
412 else
413 err = sock->ops->setsockopt(sock, level,
414 optname, optval, optlen);
415 sockfd_put(sock);
417 return err;
420 static int do_get_sock_timeout(struct socket *sock, int level, int optname,
421 char __user *optval, int __user *optlen)
423 struct compat_timeval __user *up;
424 struct timeval ktime;
425 mm_segment_t old_fs;
426 int len, err;
428 up = (struct compat_timeval __user *) optval;
429 if (get_user(len, optlen))
430 return -EFAULT;
431 if (len < sizeof(*up))
432 return -EINVAL;
433 len = sizeof(ktime);
434 old_fs = get_fs();
435 set_fs(KERNEL_DS);
436 err = sock_getsockopt(sock, level, optname, (char *) &ktime, &len);
437 set_fs(old_fs);
439 if (!err) {
440 if (put_user(sizeof(*up), optlen) ||
441 !access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
442 __put_user(ktime.tv_sec, &up->tv_sec) ||
443 __put_user(ktime.tv_usec, &up->tv_usec))
444 err = -EFAULT;
446 return err;
449 static int compat_sock_getsockopt(struct socket *sock, int level, int optname,
450 char __user *optval, int __user *optlen)
452 if (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)
453 return do_get_sock_timeout(sock, level, optname, optval, optlen);
454 return sock_getsockopt(sock, level, optname, optval, optlen);
457 int compat_sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
459 struct compat_timeval __user *ctv =
460 (struct compat_timeval __user*) userstamp;
461 int err = -ENOENT;
462 struct timeval tv;
464 if (!sock_flag(sk, SOCK_TIMESTAMP))
465 sock_enable_timestamp(sk, SOCK_TIMESTAMP);
466 tv = ktime_to_timeval(sk->sk_stamp);
467 if (tv.tv_sec == -1)
468 return err;
469 if (tv.tv_sec == 0) {
470 sk->sk_stamp = ktime_get_real();
471 tv = ktime_to_timeval(sk->sk_stamp);
473 err = 0;
474 if (put_user(tv.tv_sec, &ctv->tv_sec) ||
475 put_user(tv.tv_usec, &ctv->tv_usec))
476 err = -EFAULT;
477 return err;
479 EXPORT_SYMBOL(compat_sock_get_timestamp);
481 int compat_sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
483 struct compat_timespec __user *ctv =
484 (struct compat_timespec __user*) userstamp;
485 int err = -ENOENT;
486 struct timespec ts;
488 if (!sock_flag(sk, SOCK_TIMESTAMP))
489 sock_enable_timestamp(sk, SOCK_TIMESTAMP);
490 ts = ktime_to_timespec(sk->sk_stamp);
491 if (ts.tv_sec == -1)
492 return err;
493 if (ts.tv_sec == 0) {
494 sk->sk_stamp = ktime_get_real();
495 ts = ktime_to_timespec(sk->sk_stamp);
497 err = 0;
498 if (put_user(ts.tv_sec, &ctv->tv_sec) ||
499 put_user(ts.tv_nsec, &ctv->tv_nsec))
500 err = -EFAULT;
501 return err;
503 EXPORT_SYMBOL(compat_sock_get_timestampns);
505 asmlinkage long compat_sys_getsockopt(int fd, int level, int optname,
506 char __user *optval, int __user *optlen)
508 int err;
509 struct socket *sock;
511 if ((sock = sockfd_lookup(fd, &err))!=NULL)
513 err = security_socket_getsockopt(sock, level,
514 optname);
515 if (err) {
516 sockfd_put(sock);
517 return err;
520 if (level == SOL_SOCKET)
521 err = compat_sock_getsockopt(sock, level,
522 optname, optval, optlen);
523 else if (sock->ops->compat_getsockopt)
524 err = sock->ops->compat_getsockopt(sock, level,
525 optname, optval, optlen);
526 else
527 err = sock->ops->getsockopt(sock, level,
528 optname, optval, optlen);
529 sockfd_put(sock);
531 return err;
534 struct compat_group_req {
535 __u32 gr_interface;
536 struct __kernel_sockaddr_storage gr_group
537 __attribute__ ((aligned(4)));
538 } __attribute__ ((packed));
540 struct compat_group_source_req {
541 __u32 gsr_interface;
542 struct __kernel_sockaddr_storage gsr_group
543 __attribute__ ((aligned(4)));
544 struct __kernel_sockaddr_storage gsr_source
545 __attribute__ ((aligned(4)));
546 } __attribute__ ((packed));
548 struct compat_group_filter {
549 __u32 gf_interface;
550 struct __kernel_sockaddr_storage gf_group
551 __attribute__ ((aligned(4)));
552 __u32 gf_fmode;
553 __u32 gf_numsrc;
554 struct __kernel_sockaddr_storage gf_slist[1]
555 __attribute__ ((aligned(4)));
556 } __attribute__ ((packed));
558 #define __COMPAT_GF0_SIZE (sizeof(struct compat_group_filter) - \
559 sizeof(struct __kernel_sockaddr_storage))
562 int compat_mc_setsockopt(struct sock *sock, int level, int optname,
563 char __user *optval, unsigned int optlen,
564 int (*setsockopt)(struct sock *,int,int,char __user *,unsigned int))
566 char __user *koptval = optval;
567 int koptlen = optlen;
569 switch (optname) {
570 case MCAST_JOIN_GROUP:
571 case MCAST_LEAVE_GROUP:
573 struct compat_group_req __user *gr32 = (void *)optval;
574 struct group_req __user *kgr =
575 compat_alloc_user_space(sizeof(struct group_req));
576 u32 interface;
578 if (!access_ok(VERIFY_READ, gr32, sizeof(*gr32)) ||
579 !access_ok(VERIFY_WRITE, kgr, sizeof(struct group_req)) ||
580 __get_user(interface, &gr32->gr_interface) ||
581 __put_user(interface, &kgr->gr_interface) ||
582 copy_in_user(&kgr->gr_group, &gr32->gr_group,
583 sizeof(kgr->gr_group)))
584 return -EFAULT;
585 koptval = (char __user *)kgr;
586 koptlen = sizeof(struct group_req);
587 break;
589 case MCAST_JOIN_SOURCE_GROUP:
590 case MCAST_LEAVE_SOURCE_GROUP:
591 case MCAST_BLOCK_SOURCE:
592 case MCAST_UNBLOCK_SOURCE:
594 struct compat_group_source_req __user *gsr32 = (void *)optval;
595 struct group_source_req __user *kgsr = compat_alloc_user_space(
596 sizeof(struct group_source_req));
597 u32 interface;
599 if (!access_ok(VERIFY_READ, gsr32, sizeof(*gsr32)) ||
600 !access_ok(VERIFY_WRITE, kgsr,
601 sizeof(struct group_source_req)) ||
602 __get_user(interface, &gsr32->gsr_interface) ||
603 __put_user(interface, &kgsr->gsr_interface) ||
604 copy_in_user(&kgsr->gsr_group, &gsr32->gsr_group,
605 sizeof(kgsr->gsr_group)) ||
606 copy_in_user(&kgsr->gsr_source, &gsr32->gsr_source,
607 sizeof(kgsr->gsr_source)))
608 return -EFAULT;
609 koptval = (char __user *)kgsr;
610 koptlen = sizeof(struct group_source_req);
611 break;
613 case MCAST_MSFILTER:
615 struct compat_group_filter __user *gf32 = (void *)optval;
616 struct group_filter __user *kgf;
617 u32 interface, fmode, numsrc;
619 if (!access_ok(VERIFY_READ, gf32, __COMPAT_GF0_SIZE) ||
620 __get_user(interface, &gf32->gf_interface) ||
621 __get_user(fmode, &gf32->gf_fmode) ||
622 __get_user(numsrc, &gf32->gf_numsrc))
623 return -EFAULT;
624 koptlen = optlen + sizeof(struct group_filter) -
625 sizeof(struct compat_group_filter);
626 if (koptlen < GROUP_FILTER_SIZE(numsrc))
627 return -EINVAL;
628 kgf = compat_alloc_user_space(koptlen);
629 if (!access_ok(VERIFY_WRITE, kgf, koptlen) ||
630 __put_user(interface, &kgf->gf_interface) ||
631 __put_user(fmode, &kgf->gf_fmode) ||
632 __put_user(numsrc, &kgf->gf_numsrc) ||
633 copy_in_user(&kgf->gf_group, &gf32->gf_group,
634 sizeof(kgf->gf_group)) ||
635 (numsrc && copy_in_user(kgf->gf_slist, gf32->gf_slist,
636 numsrc * sizeof(kgf->gf_slist[0]))))
637 return -EFAULT;
638 koptval = (char __user *)kgf;
639 break;
642 default:
643 break;
645 return setsockopt(sock, level, optname, koptval, koptlen);
648 EXPORT_SYMBOL(compat_mc_setsockopt);
650 int compat_mc_getsockopt(struct sock *sock, int level, int optname,
651 char __user *optval, int __user *optlen,
652 int (*getsockopt)(struct sock *,int,int,char __user *,int __user *))
654 struct compat_group_filter __user *gf32 = (void *)optval;
655 struct group_filter __user *kgf;
656 int __user *koptlen;
657 u32 interface, fmode, numsrc;
658 int klen, ulen, err;
660 if (optname != MCAST_MSFILTER)
661 return getsockopt(sock, level, optname, optval, optlen);
663 koptlen = compat_alloc_user_space(sizeof(*koptlen));
664 if (!access_ok(VERIFY_READ, optlen, sizeof(*optlen)) ||
665 __get_user(ulen, optlen))
666 return -EFAULT;
668 /* adjust len for pad */
669 klen = ulen + sizeof(*kgf) - sizeof(*gf32);
671 if (klen < GROUP_FILTER_SIZE(0))
672 return -EINVAL;
674 if (!access_ok(VERIFY_WRITE, koptlen, sizeof(*koptlen)) ||
675 __put_user(klen, koptlen))
676 return -EFAULT;
678 /* have to allow space for previous compat_alloc_user_space, too */
679 kgf = compat_alloc_user_space(klen+sizeof(*optlen));
681 if (!access_ok(VERIFY_READ, gf32, __COMPAT_GF0_SIZE) ||
682 __get_user(interface, &gf32->gf_interface) ||
683 __get_user(fmode, &gf32->gf_fmode) ||
684 __get_user(numsrc, &gf32->gf_numsrc) ||
685 __put_user(interface, &kgf->gf_interface) ||
686 __put_user(fmode, &kgf->gf_fmode) ||
687 __put_user(numsrc, &kgf->gf_numsrc) ||
688 copy_in_user(&kgf->gf_group,&gf32->gf_group,sizeof(kgf->gf_group)))
689 return -EFAULT;
691 err = getsockopt(sock, level, optname, (char __user *)kgf, koptlen);
692 if (err)
693 return err;
695 if (!access_ok(VERIFY_READ, koptlen, sizeof(*koptlen)) ||
696 __get_user(klen, koptlen))
697 return -EFAULT;
699 ulen = klen - (sizeof(*kgf)-sizeof(*gf32));
701 if (!access_ok(VERIFY_WRITE, optlen, sizeof(*optlen)) ||
702 __put_user(ulen, optlen))
703 return -EFAULT;
705 if (!access_ok(VERIFY_READ, kgf, klen) ||
706 !access_ok(VERIFY_WRITE, gf32, ulen) ||
707 __get_user(interface, &kgf->gf_interface) ||
708 __get_user(fmode, &kgf->gf_fmode) ||
709 __get_user(numsrc, &kgf->gf_numsrc) ||
710 __put_user(interface, &gf32->gf_interface) ||
711 __put_user(fmode, &gf32->gf_fmode) ||
712 __put_user(numsrc, &gf32->gf_numsrc))
713 return -EFAULT;
714 if (numsrc) {
715 int copylen;
717 klen -= GROUP_FILTER_SIZE(0);
718 copylen = numsrc * sizeof(gf32->gf_slist[0]);
719 if (copylen > klen)
720 copylen = klen;
721 if (copy_in_user(gf32->gf_slist, kgf->gf_slist, copylen))
722 return -EFAULT;
724 return err;
727 EXPORT_SYMBOL(compat_mc_getsockopt);
730 /* Argument list sizes for compat_sys_socketcall */
731 #define AL(x) ((x) * sizeof(u32))
732 static unsigned char nas[19]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
733 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
734 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3),
735 AL(4)};
736 #undef AL
738 asmlinkage long compat_sys_sendmsg(int fd, struct compat_msghdr __user *msg, unsigned flags)
740 return sys_sendmsg(fd, (struct msghdr __user *)msg, flags | MSG_CMSG_COMPAT);
743 asmlinkage long compat_sys_recvmsg(int fd, struct compat_msghdr __user *msg, unsigned int flags)
745 return sys_recvmsg(fd, (struct msghdr __user *)msg, flags | MSG_CMSG_COMPAT);
748 asmlinkage long compat_sys_recv(int fd, void __user *buf, size_t len, unsigned flags)
750 return sys_recv(fd, buf, len, flags | MSG_CMSG_COMPAT);
753 asmlinkage long compat_sys_recvfrom(int fd, void __user *buf, size_t len,
754 unsigned flags, struct sockaddr __user *addr,
755 int __user *addrlen)
757 return sys_recvfrom(fd, buf, len, flags | MSG_CMSG_COMPAT, addr, addrlen);
760 asmlinkage long compat_sys_socketcall(int call, u32 __user *args)
762 int ret;
763 u32 a[6];
764 u32 a0, a1;
766 if (call < SYS_SOCKET || call > SYS_ACCEPT4)
767 return -EINVAL;
768 if (copy_from_user(a, args, nas[call]))
769 return -EFAULT;
770 a0 = a[0];
771 a1 = a[1];
773 switch (call) {
774 case SYS_SOCKET:
775 ret = sys_socket(a0, a1, a[2]);
776 break;
777 case SYS_BIND:
778 ret = sys_bind(a0, compat_ptr(a1), a[2]);
779 break;
780 case SYS_CONNECT:
781 ret = sys_connect(a0, compat_ptr(a1), a[2]);
782 break;
783 case SYS_LISTEN:
784 ret = sys_listen(a0, a1);
785 break;
786 case SYS_ACCEPT:
787 ret = sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), 0);
788 break;
789 case SYS_GETSOCKNAME:
790 ret = sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2]));
791 break;
792 case SYS_GETPEERNAME:
793 ret = sys_getpeername(a0, compat_ptr(a1), compat_ptr(a[2]));
794 break;
795 case SYS_SOCKETPAIR:
796 ret = sys_socketpair(a0, a1, a[2], compat_ptr(a[3]));
797 break;
798 case SYS_SEND:
799 ret = sys_send(a0, compat_ptr(a1), a[2], a[3]);
800 break;
801 case SYS_SENDTO:
802 ret = sys_sendto(a0, compat_ptr(a1), a[2], a[3], compat_ptr(a[4]), a[5]);
803 break;
804 case SYS_RECV:
805 ret = compat_sys_recv(a0, compat_ptr(a1), a[2], a[3]);
806 break;
807 case SYS_RECVFROM:
808 ret = compat_sys_recvfrom(a0, compat_ptr(a1), a[2], a[3],
809 compat_ptr(a[4]), compat_ptr(a[5]));
810 break;
811 case SYS_SHUTDOWN:
812 ret = sys_shutdown(a0,a1);
813 break;
814 case SYS_SETSOCKOPT:
815 ret = compat_sys_setsockopt(a0, a1, a[2],
816 compat_ptr(a[3]), a[4]);
817 break;
818 case SYS_GETSOCKOPT:
819 ret = compat_sys_getsockopt(a0, a1, a[2],
820 compat_ptr(a[3]), compat_ptr(a[4]));
821 break;
822 case SYS_SENDMSG:
823 ret = compat_sys_sendmsg(a0, compat_ptr(a1), a[2]);
824 break;
825 case SYS_RECVMSG:
826 ret = compat_sys_recvmsg(a0, compat_ptr(a1), a[2]);
827 break;
828 case SYS_ACCEPT4:
829 ret = sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), a[3]);
830 break;
831 default:
832 ret = -EINVAL;
833 break;
835 return ret;