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>
28 #include <asm/uaccess.h>
29 #include <net/compat.h>
31 static inline int iov_from_user_compat_to_kern(struct iovec
*kiov
,
32 struct compat_iovec __user
*uiov32
,
41 if(get_user(len
, &uiov32
->iov_len
) ||
42 get_user(buf
, &uiov32
->iov_base
)) {
47 kiov
->iov_base
= compat_ptr(buf
);
48 kiov
->iov_len
= (__kernel_size_t
) len
;
56 int get_compat_msghdr(struct msghdr
*kmsg
, struct compat_msghdr __user
*umsg
)
58 compat_uptr_t tmp1
, tmp2
, tmp3
;
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
))
69 kmsg
->msg_name
= compat_ptr(tmp1
);
70 kmsg
->msg_iov
= compat_ptr(tmp2
);
71 kmsg
->msg_control
= compat_ptr(tmp3
);
75 /* I've named the args so it is easy to tell whose space the pointers are in. */
76 int verify_compat_iovec(struct msghdr
*kern_msg
, struct iovec
*kern_iov
,
77 char *kern_address
, int mode
)
81 if(kern_msg
->msg_namelen
) {
82 if(mode
==VERIFY_READ
) {
83 int err
= move_addr_to_kernel(kern_msg
->msg_name
,
84 kern_msg
->msg_namelen
,
89 kern_msg
->msg_name
= kern_address
;
91 kern_msg
->msg_name
= NULL
;
93 if(kern_msg
->msg_iovlen
> UIO_FASTIOV
) {
94 kern_iov
= kmalloc(kern_msg
->msg_iovlen
* sizeof(struct iovec
),
100 tot_len
= iov_from_user_compat_to_kern(kern_iov
,
101 (struct compat_iovec __user
*)kern_msg
->msg_iov
,
102 kern_msg
->msg_iovlen
);
104 kern_msg
->msg_iov
= kern_iov
;
105 else if(kern_msg
->msg_iovlen
> UIO_FASTIOV
)
112 #define CMSG_COMPAT_ALIGN(len) ALIGN((len), sizeof(s32))
114 #define CMSG_COMPAT_DATA(cmsg) \
115 ((void __user *)((char __user *)(cmsg) + CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr))))
116 #define CMSG_COMPAT_SPACE(len) \
117 (CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)) + CMSG_COMPAT_ALIGN(len))
118 #define CMSG_COMPAT_LEN(len) \
119 (CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)) + (len))
121 #define CMSG_COMPAT_FIRSTHDR(msg) \
122 (((msg)->msg_controllen) >= sizeof(struct compat_cmsghdr) ? \
123 (struct compat_cmsghdr __user *)((msg)->msg_control) : \
124 (struct compat_cmsghdr __user *)NULL)
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
) >
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
,
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
;
149 kcmsg_base
= kcmsg
= (struct cmsghdr
*)stackbuf
;
150 ucmsg
= CMSG_COMPAT_FIRSTHDR(kmsg
);
151 while(ucmsg
!= NULL
) {
152 if(get_user(ucmlen
, &ucmsg
->cmsg_len
))
156 if(CMSG_COMPAT_ALIGN(ucmlen
) <
157 CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr
)))
159 if((unsigned long)(((char __user
*)ucmsg
- (char __user
*)kmsg
->msg_control
)
160 + ucmlen
) > kmsg
->msg_controllen
)
163 tmp
= ((ucmlen
- CMSG_COMPAT_ALIGN(sizeof(*ucmsg
))) +
164 CMSG_ALIGN(sizeof(struct cmsghdr
)));
166 ucmsg
= cmsg_compat_nxthdr(kmsg
, ucmsg
, ucmlen
);
171 /* The kcmlen holds the 64-bit version of the control length.
172 * It may not be modified as we do not stick it into the kmsg
173 * until we have successfully copied over all of the data
176 if(kcmlen
> stackbuf_size
)
177 kcmsg_base
= kcmsg
= kmalloc(kcmlen
, GFP_KERNEL
);
181 /* Now copy them over neatly. */
182 memset(kcmsg
, 0, kcmlen
);
183 ucmsg
= CMSG_COMPAT_FIRSTHDR(kmsg
);
184 while(ucmsg
!= NULL
) {
185 __get_user(ucmlen
, &ucmsg
->cmsg_len
);
186 tmp
= ((ucmlen
- CMSG_COMPAT_ALIGN(sizeof(*ucmsg
))) +
187 CMSG_ALIGN(sizeof(struct cmsghdr
)));
188 kcmsg
->cmsg_len
= tmp
;
189 __get_user(kcmsg
->cmsg_level
, &ucmsg
->cmsg_level
);
190 __get_user(kcmsg
->cmsg_type
, &ucmsg
->cmsg_type
);
192 /* Copy over the data. */
193 if(copy_from_user(CMSG_DATA(kcmsg
),
194 CMSG_COMPAT_DATA(ucmsg
),
195 (ucmlen
- CMSG_COMPAT_ALIGN(sizeof(*ucmsg
)))))
196 goto out_free_efault
;
199 kcmsg
= (struct cmsghdr
*)((char *)kcmsg
+ CMSG_ALIGN(tmp
));
200 ucmsg
= cmsg_compat_nxthdr(kmsg
, ucmsg
, ucmlen
);
203 /* Ok, looks like we made it. Hook it up and return success. */
204 kmsg
->msg_control
= kcmsg_base
;
205 kmsg
->msg_controllen
= kcmlen
;
209 if(kcmsg_base
!= (struct cmsghdr
*)stackbuf
)
214 int put_cmsg_compat(struct msghdr
*kmsg
, int level
, int type
, int len
, void *data
)
216 struct compat_timeval ctv
;
217 struct compat_cmsghdr __user
*cm
= (struct compat_cmsghdr __user
*) kmsg
->msg_control
;
218 struct compat_cmsghdr cmhdr
;
221 if(cm
== NULL
|| kmsg
->msg_controllen
< sizeof(*cm
)) {
222 kmsg
->msg_flags
|= MSG_CTRUNC
;
223 return 0; /* XXX: return error? check spec. */
226 if (level
== SOL_SOCKET
&& type
== SO_TIMESTAMP
) {
227 struct timeval
*tv
= (struct timeval
*)data
;
228 ctv
.tv_sec
= tv
->tv_sec
;
229 ctv
.tv_usec
= tv
->tv_usec
;
231 len
= sizeof(struct compat_timeval
);
234 cmlen
= CMSG_COMPAT_LEN(len
);
235 if(kmsg
->msg_controllen
< cmlen
) {
236 kmsg
->msg_flags
|= MSG_CTRUNC
;
237 cmlen
= kmsg
->msg_controllen
;
239 cmhdr
.cmsg_level
= level
;
240 cmhdr
.cmsg_type
= type
;
241 cmhdr
.cmsg_len
= cmlen
;
243 if(copy_to_user(cm
, &cmhdr
, sizeof cmhdr
))
245 if(copy_to_user(CMSG_COMPAT_DATA(cm
), data
, cmlen
- sizeof(struct compat_cmsghdr
)))
247 cmlen
= CMSG_COMPAT_SPACE(len
);
248 kmsg
->msg_control
+= cmlen
;
249 kmsg
->msg_controllen
-= cmlen
;
253 void scm_detach_fds_compat(struct msghdr
*kmsg
, struct scm_cookie
*scm
)
255 struct compat_cmsghdr __user
*cm
= (struct compat_cmsghdr __user
*) kmsg
->msg_control
;
256 int fdmax
= (kmsg
->msg_controllen
- sizeof(struct compat_cmsghdr
)) / sizeof(int);
257 int fdnum
= scm
->fp
->count
;
258 struct file
**fp
= scm
->fp
->fp
;
265 for (i
= 0, cmfptr
= (int __user
*) CMSG_COMPAT_DATA(cm
); i
< fdmax
; i
++, cmfptr
++) {
267 err
= get_unused_fd();
271 err
= put_user(new_fd
, cmfptr
);
273 put_unused_fd(new_fd
);
276 /* Bump the usage count and install the file. */
278 fd_install(new_fd
, fp
[i
]);
282 int cmlen
= CMSG_COMPAT_LEN(i
* sizeof(int));
284 err
= put_user(SOL_SOCKET
, &cm
->cmsg_level
);
286 err
= put_user(SCM_RIGHTS
, &cm
->cmsg_type
);
288 err
= put_user(cmlen
, &cm
->cmsg_len
);
290 cmlen
= CMSG_COMPAT_SPACE(i
* sizeof(int));
291 kmsg
->msg_control
+= cmlen
;
292 kmsg
->msg_controllen
-= cmlen
;
296 kmsg
->msg_flags
|= MSG_CTRUNC
;
299 * All of the files that fit in the message have had their
300 * usage counts incremented, so we just free the list.
306 * For now, we assume that the compatibility and native version
307 * of struct ipt_entry are the same - sfr. FIXME
309 struct compat_ipt_replace
{
310 char name
[IPT_TABLE_MAXNAMELEN
];
314 u32 hook_entry
[NF_IP_NUMHOOKS
];
315 u32 underflow
[NF_IP_NUMHOOKS
];
317 compat_uptr_t counters
; /* struct ipt_counters * */
318 struct ipt_entry entries
[0];
321 static int do_netfilter_replace(int fd
, int level
, int optname
,
322 char __user
*optval
, int optlen
)
324 struct compat_ipt_replace __user
*urepl
;
325 struct ipt_replace __user
*repl_nat
;
326 char name
[IPT_TABLE_MAXNAMELEN
];
327 u32 origsize
, tmp32
, num_counters
;
328 unsigned int repl_nat_size
;
331 compat_uptr_t ucntrs
;
333 urepl
= (struct compat_ipt_replace __user
*)optval
;
334 if (get_user(origsize
, &urepl
->size
))
337 /* Hack: Causes ipchains to give correct error msg --RR */
338 if (optlen
!= sizeof(*urepl
) + origsize
)
341 /* XXX Assumes that size of ipt_entry is the same both in
342 * native and compat environments.
344 repl_nat_size
= sizeof(*repl_nat
) + origsize
;
345 repl_nat
= compat_alloc_user_space(repl_nat_size
);
348 if (put_user(origsize
, &repl_nat
->size
))
351 if (!access_ok(VERIFY_READ
, urepl
, optlen
) ||
352 !access_ok(VERIFY_WRITE
, repl_nat
, optlen
))
355 if (__copy_from_user(name
, urepl
->name
, sizeof(urepl
->name
)) ||
356 __copy_to_user(repl_nat
->name
, name
, sizeof(repl_nat
->name
)))
359 if (__get_user(tmp32
, &urepl
->valid_hooks
) ||
360 __put_user(tmp32
, &repl_nat
->valid_hooks
))
363 if (__get_user(tmp32
, &urepl
->num_entries
) ||
364 __put_user(tmp32
, &repl_nat
->num_entries
))
367 if (__get_user(num_counters
, &urepl
->num_counters
) ||
368 __put_user(num_counters
, &repl_nat
->num_counters
))
371 if (__get_user(ucntrs
, &urepl
->counters
) ||
372 __put_user(compat_ptr(ucntrs
), &repl_nat
->counters
))
375 if (__copy_in_user(&repl_nat
->entries
[0],
380 for (i
= 0; i
< NF_IP_NUMHOOKS
; i
++) {
381 if (__get_user(tmp32
, &urepl
->hook_entry
[i
]) ||
382 __put_user(tmp32
, &repl_nat
->hook_entry
[i
]) ||
383 __get_user(tmp32
, &urepl
->underflow
[i
]) ||
384 __put_user(tmp32
, &repl_nat
->underflow
[i
]))
389 * Since struct ipt_counters just contains two u_int64_t members
390 * we can just do the access_ok check here and pass the (converted)
391 * pointer into the standard syscall. We hope that the pointer is
394 if (!access_ok(VERIFY_WRITE
, compat_ptr(ucntrs
),
395 num_counters
* sizeof(struct ipt_counters
)))
399 ret
= sys_setsockopt(fd
, level
, optname
,
400 (char __user
*)repl_nat
, repl_nat_size
);
407 * A struct sock_filter is architecture independent.
409 struct compat_sock_fprog
{
411 compat_uptr_t filter
; /* struct sock_filter * */
414 static int do_set_attach_filter(int fd
, int level
, int optname
,
415 char __user
*optval
, int optlen
)
417 struct compat_sock_fprog __user
*fprog32
= (struct compat_sock_fprog __user
*)optval
;
418 struct sock_fprog __user
*kfprog
= compat_alloc_user_space(sizeof(struct sock_fprog
));
422 if (!access_ok(VERIFY_READ
, fprog32
, sizeof(*fprog32
)) ||
423 !access_ok(VERIFY_WRITE
, kfprog
, sizeof(struct sock_fprog
)) ||
424 __get_user(len
, &fprog32
->len
) ||
425 __get_user(ptr
, &fprog32
->filter
) ||
426 __put_user(len
, &kfprog
->len
) ||
427 __put_user(compat_ptr(ptr
), &kfprog
->filter
))
430 return sys_setsockopt(fd
, level
, optname
, (char __user
*)kfprog
,
431 sizeof(struct sock_fprog
));
434 static int do_set_sock_timeout(int fd
, int level
, int optname
, char __user
*optval
, int optlen
)
436 struct compat_timeval __user
*up
= (struct compat_timeval __user
*) optval
;
437 struct timeval ktime
;
441 if (optlen
< sizeof(*up
))
443 if (!access_ok(VERIFY_READ
, up
, sizeof(*up
)) ||
444 __get_user(ktime
.tv_sec
, &up
->tv_sec
) ||
445 __get_user(ktime
.tv_usec
, &up
->tv_usec
))
449 err
= sys_setsockopt(fd
, level
, optname
, (char *) &ktime
, sizeof(ktime
));
455 asmlinkage
long compat_sys_setsockopt(int fd
, int level
, int optname
,
456 char __user
*optval
, int optlen
)
458 /* SO_SET_REPLACE seems to be the same in all levels */
459 if (optname
== IPT_SO_SET_REPLACE
)
460 return do_netfilter_replace(fd
, level
, optname
,
462 if (level
== SOL_SOCKET
&& optname
== SO_ATTACH_FILTER
)
463 return do_set_attach_filter(fd
, level
, optname
,
465 if (level
== SOL_SOCKET
&&
466 (optname
== SO_RCVTIMEO
|| optname
== SO_SNDTIMEO
))
467 return do_set_sock_timeout(fd
, level
, optname
, optval
, optlen
);
469 return sys_setsockopt(fd
, level
, optname
, optval
, optlen
);
472 static int do_get_sock_timeout(int fd
, int level
, int optname
,
473 char __user
*optval
, int __user
*optlen
)
475 struct compat_timeval __user
*up
;
476 struct timeval ktime
;
480 up
= (struct compat_timeval __user
*) optval
;
481 if (get_user(len
, optlen
))
483 if (len
< sizeof(*up
))
488 err
= sys_getsockopt(fd
, level
, optname
, (char *) &ktime
, &len
);
492 if (put_user(sizeof(*up
), optlen
) ||
493 !access_ok(VERIFY_WRITE
, up
, sizeof(*up
)) ||
494 __put_user(ktime
.tv_sec
, &up
->tv_sec
) ||
495 __put_user(ktime
.tv_usec
, &up
->tv_usec
))
501 asmlinkage
long compat_sys_getsockopt(int fd
, int level
, int optname
,
502 char __user
*optval
, int __user
*optlen
)
504 if (optname
== SO_RCVTIMEO
|| optname
== SO_SNDTIMEO
)
505 return do_get_sock_timeout(fd
, level
, optname
, optval
, optlen
);
506 return sys_getsockopt(fd
, level
, optname
, optval
, optlen
);
509 /* Argument list sizes for compat_sys_socketcall */
510 #define AL(x) ((x) * sizeof(u32))
511 static unsigned char nas
[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
512 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
513 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)};
516 asmlinkage
long compat_sys_sendmsg(int fd
, struct compat_msghdr __user
*msg
, unsigned flags
)
518 return sys_sendmsg(fd
, (struct msghdr __user
*)msg
, flags
| MSG_CMSG_COMPAT
);
521 asmlinkage
long compat_sys_recvmsg(int fd
, struct compat_msghdr __user
*msg
, unsigned int flags
)
523 return sys_recvmsg(fd
, (struct msghdr __user
*)msg
, flags
| MSG_CMSG_COMPAT
);
526 asmlinkage
long compat_sys_socketcall(int call
, u32 __user
*args
)
532 if (call
< SYS_SOCKET
|| call
> SYS_RECVMSG
)
534 if (copy_from_user(a
, args
, nas
[call
]))
541 ret
= sys_socket(a0
, a1
, a
[2]);
544 ret
= sys_bind(a0
, compat_ptr(a1
), a
[2]);
547 ret
= sys_connect(a0
, compat_ptr(a1
), a
[2]);
550 ret
= sys_listen(a0
, a1
);
553 ret
= sys_accept(a0
, compat_ptr(a1
), compat_ptr(a
[2]));
555 case SYS_GETSOCKNAME
:
556 ret
= sys_getsockname(a0
, compat_ptr(a1
), compat_ptr(a
[2]));
558 case SYS_GETPEERNAME
:
559 ret
= sys_getpeername(a0
, compat_ptr(a1
), compat_ptr(a
[2]));
562 ret
= sys_socketpair(a0
, a1
, a
[2], compat_ptr(a
[3]));
565 ret
= sys_send(a0
, compat_ptr(a1
), a
[2], a
[3]);
568 ret
= sys_sendto(a0
, compat_ptr(a1
), a
[2], a
[3], compat_ptr(a
[4]), a
[5]);
571 ret
= sys_recv(a0
, compat_ptr(a1
), a
[2], a
[3]);
574 ret
= sys_recvfrom(a0
, compat_ptr(a1
), a
[2], a
[3], compat_ptr(a
[4]), compat_ptr(a
[5]));
577 ret
= sys_shutdown(a0
,a1
);
580 ret
= compat_sys_setsockopt(a0
, a1
, a
[2],
581 compat_ptr(a
[3]), a
[4]);
584 ret
= compat_sys_getsockopt(a0
, a1
, a
[2],
585 compat_ptr(a
[3]), compat_ptr(a
[4]));
588 ret
= compat_sys_sendmsg(a0
, compat_ptr(a1
), a
[2]);
591 ret
= compat_sys_recvmsg(a0
, compat_ptr(a1
), a
[2]);