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>
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>
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,
42 if(get_user(len, &uiov32->iov_len) ||
43 get_user(buf, &uiov32->iov_base)) {
48 kiov->iov_base = compat_ptr(buf);
49 kiov->iov_len = (__kernel_size_t) len;
57 int get_compat_msghdr(struct msghdr *kmsg, struct compat_msghdr __user *umsg)
59 compat_uptr_t tmp1, tmp2, tmp3;
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))
70 kmsg->msg_name = compat_ptr(tmp1);
71 kmsg->msg_iov = compat_ptr(tmp2);
72 kmsg->msg_control = compat_ptr(tmp3);
76 /* I've named the args so it is easy to tell whose space the pointers are in. */
77 int verify_compat_iovec(struct msghdr *kern_msg, struct iovec *kern_iov,
78 char *kern_address, int mode)
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,
90 kern_msg->msg_name = kern_address;
92 kern_msg->msg_name = NULL;
94 if(kern_msg->msg_iovlen > UIO_FASTIOV) {
95 kern_iov = kmalloc(kern_msg->msg_iovlen * sizeof(struct iovec),
101 tot_len = iov_from_user_compat_to_kern(kern_iov,
102 (struct compat_iovec __user *)kern_msg->msg_iov,
103 kern_msg->msg_iovlen);
105 kern_msg->msg_iov = kern_iov;
106 else if(kern_msg->msg_iovlen > UIO_FASTIOV)
113 #define CMSG_COMPAT_ALIGN(len) ALIGN((len), sizeof(s32))
115 #define CMSG_COMPAT_DATA(cmsg) \
116 ((void __user *)((char __user *)(cmsg) + CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr))))
117 #define CMSG_COMPAT_SPACE(len) \
118 (CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)) + CMSG_COMPAT_ALIGN(len))
119 #define CMSG_COMPAT_LEN(len) \
120 (CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)) + (len))
122 #define CMSG_COMPAT_FIRSTHDR(msg) \
123 (((msg)->msg_controllen) >= sizeof(struct compat_cmsghdr) ? \
124 (struct compat_cmsghdr __user *)((msg)->msg_control) : \
125 (struct compat_cmsghdr __user *)NULL)
127 #define CMSG_COMPAT_OK(ucmlen, ucmsg, mhdr) \
128 ((ucmlen) >= sizeof(struct compat_cmsghdr) && \
129 (ucmlen) <= (unsigned long) \
130 ((mhdr)->msg_controllen - \
131 ((char *)(ucmsg) - (char *)(mhdr)->msg_control)))
133 static inline struct compat_cmsghdr __user *cmsg_compat_nxthdr(struct msghdr *msg,
134 struct compat_cmsghdr __user *cmsg, int cmsg_len)
136 char __user *ptr = (char __user *)cmsg + CMSG_COMPAT_ALIGN(cmsg_len);
137 if ((unsigned long)(ptr + 1 - (char __user *)msg->msg_control) >
140 return (struct compat_cmsghdr __user *)ptr;
143 /* There is a lot of hair here because the alignment rules (and
144 * thus placement) of cmsg headers and length are different for
145 * 32-bit apps. -DaveM
147 int cmsghdr_from_user_compat_to_kern(struct msghdr *kmsg,
148 unsigned char *stackbuf, int stackbuf_size)
150 struct compat_cmsghdr __user *ucmsg;
151 struct cmsghdr *kcmsg, *kcmsg_base;
152 compat_size_t ucmlen;
153 __kernel_size_t kcmlen, tmp;
156 kcmsg_base = kcmsg = (struct cmsghdr *)stackbuf;
157 ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
158 while(ucmsg != NULL) {
159 if(get_user(ucmlen, &ucmsg->cmsg_len))
163 if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg))
166 tmp = ((ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg))) +
167 CMSG_ALIGN(sizeof(struct cmsghdr)));
169 ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
174 /* The kcmlen holds the 64-bit version of the control length.
175 * It may not be modified as we do not stick it into the kmsg
176 * until we have successfully copied over all of the data
179 if(kcmlen > stackbuf_size)
180 kcmsg_base = kcmsg = kmalloc(kcmlen, GFP_KERNEL);
184 /* Now copy them over neatly. */
185 memset(kcmsg, 0, kcmlen);
186 ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
187 while(ucmsg != NULL) {
188 __get_user(ucmlen, &ucmsg->cmsg_len);
189 tmp = ((ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg))) +
190 CMSG_ALIGN(sizeof(struct cmsghdr)));
191 kcmsg->cmsg_len = tmp;
192 __get_user(kcmsg->cmsg_level, &ucmsg->cmsg_level);
193 __get_user(kcmsg->cmsg_type, &ucmsg->cmsg_type);
195 /* Copy over the data. */
196 if(copy_from_user(CMSG_DATA(kcmsg),
197 CMSG_COMPAT_DATA(ucmsg),
198 (ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg)))))
199 goto out_free_efault;
202 kcmsg = (struct cmsghdr *)((char *)kcmsg + CMSG_ALIGN(tmp));
203 ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
206 /* Ok, looks like we made it. Hook it up and return success. */
207 kmsg->msg_control = kcmsg_base;
208 kmsg->msg_controllen = kcmlen;
212 if(kcmsg_base != (struct cmsghdr *)stackbuf)
217 int put_cmsg_compat(struct msghdr *kmsg, int level, int type, int len, void *data)
219 struct compat_timeval ctv;
220 struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control;
221 struct compat_cmsghdr cmhdr;
224 if(cm == NULL || kmsg->msg_controllen < sizeof(*cm)) {
225 kmsg->msg_flags |= MSG_CTRUNC;
226 return 0; /* XXX: return error? check spec. */
229 if (level == SOL_SOCKET && type == SO_TIMESTAMP) {
230 struct timeval *tv = (struct timeval *)data;
231 ctv.tv_sec = tv->tv_sec;
232 ctv.tv_usec = tv->tv_usec;
234 len = sizeof(struct compat_timeval);
237 cmlen = CMSG_COMPAT_LEN(len);
238 if(kmsg->msg_controllen < cmlen) {
239 kmsg->msg_flags |= MSG_CTRUNC;
240 cmlen = kmsg->msg_controllen;
242 cmhdr.cmsg_level = level;
243 cmhdr.cmsg_type = type;
244 cmhdr.cmsg_len = cmlen;
246 if(copy_to_user(cm, &cmhdr, sizeof cmhdr))
248 if(copy_to_user(CMSG_COMPAT_DATA(cm), data, cmlen - sizeof(struct compat_cmsghdr)))
250 cmlen = CMSG_COMPAT_SPACE(len);
251 kmsg->msg_control += cmlen;
252 kmsg->msg_controllen -= cmlen;
256 void scm_detach_fds_compat(struct msghdr *kmsg, struct scm_cookie *scm)
258 struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control;
259 int fdmax = (kmsg->msg_controllen - sizeof(struct compat_cmsghdr)) / sizeof(int);
260 int fdnum = scm->fp->count;
261 struct file **fp = scm->fp->fp;
268 for (i = 0, cmfptr = (int __user *) CMSG_COMPAT_DATA(cm); i < fdmax; i++, cmfptr++) {
270 err = security_file_receive(fp[i]);
273 err = get_unused_fd();
277 err = put_user(new_fd, cmfptr);
279 put_unused_fd(new_fd);
282 /* Bump the usage count and install the file. */
284 fd_install(new_fd, fp[i]);
288 int cmlen = CMSG_COMPAT_LEN(i * sizeof(int));
290 err = put_user(SOL_SOCKET, &cm->cmsg_level);
292 err = put_user(SCM_RIGHTS, &cm->cmsg_type);
294 err = put_user(cmlen, &cm->cmsg_len);
296 cmlen = CMSG_COMPAT_SPACE(i * sizeof(int));
297 kmsg->msg_control += cmlen;
298 kmsg->msg_controllen -= cmlen;
302 kmsg->msg_flags |= MSG_CTRUNC;
305 * All of the files that fit in the message have had their
306 * usage counts incremented, so we just free the list.
312 * For now, we assume that the compatibility and native version
313 * of struct ipt_entry are the same - sfr. FIXME
315 struct compat_ipt_replace {
316 char name[IPT_TABLE_MAXNAMELEN];
320 u32 hook_entry[NF_IP_NUMHOOKS];
321 u32 underflow[NF_IP_NUMHOOKS];
323 compat_uptr_t counters; /* struct ipt_counters * */
324 struct ipt_entry entries[0];
327 static int do_netfilter_replace(int fd, int level, int optname,
328 char __user *optval, int optlen)
330 struct compat_ipt_replace __user *urepl;
331 struct ipt_replace __user *repl_nat;
332 char name[IPT_TABLE_MAXNAMELEN];
333 u32 origsize, tmp32, num_counters;
334 unsigned int repl_nat_size;
337 compat_uptr_t ucntrs;
339 urepl = (struct compat_ipt_replace __user *)optval;
340 if (get_user(origsize, &urepl->size))
343 /* Hack: Causes ipchains to give correct error msg --RR */
344 if (optlen != sizeof(*urepl) + origsize)
347 /* XXX Assumes that size of ipt_entry is the same both in
348 * native and compat environments.
350 repl_nat_size = sizeof(*repl_nat) + origsize;
351 repl_nat = compat_alloc_user_space(repl_nat_size);
354 if (put_user(origsize, &repl_nat->size))
357 if (!access_ok(VERIFY_READ, urepl, optlen) ||
358 !access_ok(VERIFY_WRITE, repl_nat, optlen))
361 if (__copy_from_user(name, urepl->name, sizeof(urepl->name)) ||
362 __copy_to_user(repl_nat->name, name, sizeof(repl_nat->name)))
365 if (__get_user(tmp32, &urepl->valid_hooks) ||
366 __put_user(tmp32, &repl_nat->valid_hooks))
369 if (__get_user(tmp32, &urepl->num_entries) ||
370 __put_user(tmp32, &repl_nat->num_entries))
373 if (__get_user(num_counters, &urepl->num_counters) ||
374 __put_user(num_counters, &repl_nat->num_counters))
377 if (__get_user(ucntrs, &urepl->counters) ||
378 __put_user(compat_ptr(ucntrs), &repl_nat->counters))
381 if (__copy_in_user(&repl_nat->entries[0],
386 for (i = 0; i < NF_IP_NUMHOOKS; i++) {
387 if (__get_user(tmp32, &urepl->hook_entry[i]) ||
388 __put_user(tmp32, &repl_nat->hook_entry[i]) ||
389 __get_user(tmp32, &urepl->underflow[i]) ||
390 __put_user(tmp32, &repl_nat->underflow[i]))
395 * Since struct ipt_counters just contains two u_int64_t members
396 * we can just do the access_ok check here and pass the (converted)
397 * pointer into the standard syscall. We hope that the pointer is
400 if (!access_ok(VERIFY_WRITE, compat_ptr(ucntrs),
401 num_counters * sizeof(struct ipt_counters)))
405 ret = sys_setsockopt(fd, level, optname,
406 (char __user *)repl_nat, repl_nat_size);
413 * A struct sock_filter is architecture independent.
415 struct compat_sock_fprog {
417 compat_uptr_t filter; /* struct sock_filter * */
420 static int do_set_attach_filter(int fd, int level, int optname,
421 char __user *optval, int optlen)
423 struct compat_sock_fprog __user *fprog32 = (struct compat_sock_fprog __user *)optval;
424 struct sock_fprog __user *kfprog = compat_alloc_user_space(sizeof(struct sock_fprog));
428 if (!access_ok(VERIFY_READ, fprog32, sizeof(*fprog32)) ||
429 !access_ok(VERIFY_WRITE, kfprog, sizeof(struct sock_fprog)) ||
430 __get_user(len, &fprog32->len) ||
431 __get_user(ptr, &fprog32->filter) ||
432 __put_user(len, &kfprog->len) ||
433 __put_user(compat_ptr(ptr), &kfprog->filter))
436 return sys_setsockopt(fd, level, optname, (char __user *)kfprog,
437 sizeof(struct sock_fprog));
440 static int do_set_sock_timeout(int fd, int level, int optname, char __user *optval, int optlen)
442 struct compat_timeval __user *up = (struct compat_timeval __user *) optval;
443 struct timeval ktime;
447 if (optlen < sizeof(*up))
449 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
450 __get_user(ktime.tv_sec, &up->tv_sec) ||
451 __get_user(ktime.tv_usec, &up->tv_usec))
455 err = sys_setsockopt(fd, level, optname, (char *) &ktime, sizeof(ktime));
461 asmlinkage long compat_sys_setsockopt(int fd, int level, int optname,
462 char __user *optval, int optlen)
464 /* SO_SET_REPLACE seems to be the same in all levels */
465 if (optname == IPT_SO_SET_REPLACE)
466 return do_netfilter_replace(fd, level, optname,
468 if (level == SOL_SOCKET && optname == SO_ATTACH_FILTER)
469 return do_set_attach_filter(fd, level, optname,
471 if (level == SOL_SOCKET &&
472 (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO))
473 return do_set_sock_timeout(fd, level, optname, optval, optlen);
475 return sys_setsockopt(fd, level, optname, optval, optlen);
478 static int do_get_sock_timeout(int fd, int level, int optname,
479 char __user *optval, int __user *optlen)
481 struct compat_timeval __user *up;
482 struct timeval ktime;
486 up = (struct compat_timeval __user *) optval;
487 if (get_user(len, optlen))
489 if (len < sizeof(*up))
494 err = sys_getsockopt(fd, level, optname, (char *) &ktime, &len);
498 if (put_user(sizeof(*up), optlen) ||
499 !access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
500 __put_user(ktime.tv_sec, &up->tv_sec) ||
501 __put_user(ktime.tv_usec, &up->tv_usec))
507 asmlinkage long compat_sys_getsockopt(int fd, int level, int optname,
508 char __user *optval, int __user *optlen)
510 if (level == SOL_SOCKET &&
511 (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO))
512 return do_get_sock_timeout(fd, level, optname, optval, optlen);
513 return sys_getsockopt(fd, level, optname, optval, optlen);
516 /* Argument list sizes for compat_sys_socketcall */
517 #define AL(x) ((x) * sizeof(u32))
518 static unsigned char nas[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
519 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
520 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)};
523 asmlinkage long compat_sys_sendmsg(int fd, struct compat_msghdr __user *msg, unsigned flags)
525 return sys_sendmsg(fd, (struct msghdr __user *)msg, flags | MSG_CMSG_COMPAT);
528 asmlinkage long compat_sys_recvmsg(int fd, struct compat_msghdr __user *msg, unsigned int flags)
530 return sys_recvmsg(fd, (struct msghdr __user *)msg, flags | MSG_CMSG_COMPAT);
533 asmlinkage long compat_sys_socketcall(int call, u32 __user *args)
539 if (call < SYS_SOCKET || call > SYS_RECVMSG)
541 if (copy_from_user(a, args, nas[call]))
548 ret = sys_socket(a0, a1, a[2]);
551 ret = sys_bind(a0, compat_ptr(a1), a[2]);
554 ret = sys_connect(a0, compat_ptr(a1), a[2]);
557 ret = sys_listen(a0, a1);
560 ret = sys_accept(a0, compat_ptr(a1), compat_ptr(a[2]));
562 case SYS_GETSOCKNAME:
563 ret = sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2]));
565 case SYS_GETPEERNAME:
566 ret = sys_getpeername(a0, compat_ptr(a1), compat_ptr(a[2]));
569 ret = sys_socketpair(a0, a1, a[2], compat_ptr(a[3]));
572 ret = sys_send(a0, compat_ptr(a1), a[2], a[3]);
575 ret = sys_sendto(a0, compat_ptr(a1), a[2], a[3], compat_ptr(a[4]), a[5]);
578 ret = sys_recv(a0, compat_ptr(a1), a[2], a[3]);
581 ret = sys_recvfrom(a0, compat_ptr(a1), a[2], a[3], compat_ptr(a[4]), compat_ptr(a[5]));
584 ret = sys_shutdown(a0,a1);
587 ret = compat_sys_setsockopt(a0, a1, a[2],
588 compat_ptr(a[3]), a[4]);
591 ret = compat_sys_getsockopt(a0, a1, a[2],
592 compat_ptr(a[3]), compat_ptr(a[4]));
595 ret = compat_sys_sendmsg(a0, compat_ptr(a1), a[2]);
598 ret = compat_sys_recvmsg(a0, compat_ptr(a1), a[2]);