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/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>
27 #include <asm/uaccess.h>
28 #include <net/compat.h>
30 static inline int iov_from_user_compat_to_kern(struct iovec
*kiov
,
31 struct compat_iovec __user
*uiov32
,
40 if (get_user(len
, &uiov32
->iov_len
) ||
41 get_user(buf
, &uiov32
->iov_base
)) {
46 kiov
->iov_base
= compat_ptr(buf
);
47 kiov
->iov_len
= (__kernel_size_t
) len
;
55 int get_compat_msghdr(struct msghdr
*kmsg
, struct compat_msghdr __user
*umsg
)
57 compat_uptr_t tmp1
, tmp2
, tmp3
;
59 if (!access_ok(VERIFY_READ
, umsg
, sizeof(*umsg
)) ||
60 __get_user(tmp1
, &umsg
->msg_name
) ||
61 __get_user(kmsg
->msg_namelen
, &umsg
->msg_namelen
) ||
62 __get_user(tmp2
, &umsg
->msg_iov
) ||
63 __get_user(kmsg
->msg_iovlen
, &umsg
->msg_iovlen
) ||
64 __get_user(tmp3
, &umsg
->msg_control
) ||
65 __get_user(kmsg
->msg_controllen
, &umsg
->msg_controllen
) ||
66 __get_user(kmsg
->msg_flags
, &umsg
->msg_flags
))
68 kmsg
->msg_name
= compat_ptr(tmp1
);
69 kmsg
->msg_iov
= compat_ptr(tmp2
);
70 kmsg
->msg_control
= compat_ptr(tmp3
);
74 /* I've named the args so it is easy to tell whose space the pointers are in. */
75 int verify_compat_iovec(struct msghdr
*kern_msg
, struct iovec
*kern_iov
,
76 char *kern_address
, int mode
)
80 if (kern_msg
->msg_namelen
) {
81 if (mode
==VERIFY_READ
) {
82 int err
= move_addr_to_kernel(kern_msg
->msg_name
,
83 kern_msg
->msg_namelen
,
88 kern_msg
->msg_name
= kern_address
;
90 kern_msg
->msg_name
= NULL
;
92 tot_len
= iov_from_user_compat_to_kern(kern_iov
,
93 (struct compat_iovec __user
*)kern_msg
->msg_iov
,
94 kern_msg
->msg_iovlen
);
96 kern_msg
->msg_iov
= kern_iov
;
102 #define CMSG_COMPAT_ALIGN(len) ALIGN((len), sizeof(s32))
104 #define CMSG_COMPAT_DATA(cmsg) \
105 ((void __user *)((char __user *)(cmsg) + CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr))))
106 #define CMSG_COMPAT_SPACE(len) \
107 (CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)) + CMSG_COMPAT_ALIGN(len))
108 #define CMSG_COMPAT_LEN(len) \
109 (CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)) + (len))
111 #define CMSG_COMPAT_FIRSTHDR(msg) \
112 (((msg)->msg_controllen) >= sizeof(struct compat_cmsghdr) ? \
113 (struct compat_cmsghdr __user *)((msg)->msg_control) : \
114 (struct compat_cmsghdr __user *)NULL)
116 #define CMSG_COMPAT_OK(ucmlen, ucmsg, mhdr) \
117 ((ucmlen) >= sizeof(struct compat_cmsghdr) && \
118 (ucmlen) <= (unsigned long) \
119 ((mhdr)->msg_controllen - \
120 ((char *)(ucmsg) - (char *)(mhdr)->msg_control)))
122 static inline struct compat_cmsghdr __user
*cmsg_compat_nxthdr(struct msghdr
*msg
,
123 struct compat_cmsghdr __user
*cmsg
, int cmsg_len
)
125 char __user
*ptr
= (char __user
*)cmsg
+ CMSG_COMPAT_ALIGN(cmsg_len
);
126 if ((unsigned long)(ptr
+ 1 - (char __user
*)msg
->msg_control
) >
129 return (struct compat_cmsghdr __user
*)ptr
;
132 /* There is a lot of hair here because the alignment rules (and
133 * thus placement) of cmsg headers and length are different for
134 * 32-bit apps. -DaveM
136 int cmsghdr_from_user_compat_to_kern(struct msghdr
*kmsg
, struct sock
*sk
,
137 unsigned char *stackbuf
, int stackbuf_size
)
139 struct compat_cmsghdr __user
*ucmsg
;
140 struct cmsghdr
*kcmsg
, *kcmsg_base
;
141 compat_size_t ucmlen
;
142 __kernel_size_t kcmlen
, tmp
;
146 kcmsg_base
= kcmsg
= (struct cmsghdr
*)stackbuf
;
147 ucmsg
= CMSG_COMPAT_FIRSTHDR(kmsg
);
148 while (ucmsg
!= NULL
) {
149 if (get_user(ucmlen
, &ucmsg
->cmsg_len
))
153 if (!CMSG_COMPAT_OK(ucmlen
, ucmsg
, kmsg
))
156 tmp
= ((ucmlen
- CMSG_COMPAT_ALIGN(sizeof(*ucmsg
))) +
157 CMSG_ALIGN(sizeof(struct cmsghdr
)));
158 tmp
= CMSG_ALIGN(tmp
);
160 ucmsg
= cmsg_compat_nxthdr(kmsg
, ucmsg
, ucmlen
);
165 /* The kcmlen holds the 64-bit version of the control length.
166 * It may not be modified as we do not stick it into the kmsg
167 * until we have successfully copied over all of the data
170 if (kcmlen
> stackbuf_size
)
171 kcmsg_base
= kcmsg
= sock_kmalloc(sk
, kcmlen
, GFP_KERNEL
);
175 /* Now copy them over neatly. */
176 memset(kcmsg
, 0, kcmlen
);
177 ucmsg
= CMSG_COMPAT_FIRSTHDR(kmsg
);
178 while (ucmsg
!= NULL
) {
179 if (__get_user(ucmlen
, &ucmsg
->cmsg_len
))
181 if (!CMSG_COMPAT_OK(ucmlen
, ucmsg
, kmsg
))
183 tmp
= ((ucmlen
- CMSG_COMPAT_ALIGN(sizeof(*ucmsg
))) +
184 CMSG_ALIGN(sizeof(struct cmsghdr
)));
185 if ((char *)kcmsg_base
+ kcmlen
- (char *)kcmsg
< CMSG_ALIGN(tmp
))
187 kcmsg
->cmsg_len
= tmp
;
188 tmp
= CMSG_ALIGN(tmp
);
189 if (__get_user(kcmsg
->cmsg_level
, &ucmsg
->cmsg_level
) ||
190 __get_user(kcmsg
->cmsg_type
, &ucmsg
->cmsg_type
) ||
191 copy_from_user(CMSG_DATA(kcmsg
),
192 CMSG_COMPAT_DATA(ucmsg
),
193 (ucmlen
- CMSG_COMPAT_ALIGN(sizeof(*ucmsg
)))))
197 kcmsg
= (struct cmsghdr
*)((char *)kcmsg
+ tmp
);
198 ucmsg
= cmsg_compat_nxthdr(kmsg
, ucmsg
, ucmlen
);
201 /* Ok, looks like we made it. Hook it up and return success. */
202 kmsg
->msg_control
= kcmsg_base
;
203 kmsg
->msg_controllen
= kcmlen
;
209 if (kcmsg_base
!= (struct cmsghdr
*)stackbuf
)
210 sock_kfree_s(sk
, kcmsg_base
, kcmlen
);
214 int put_cmsg_compat(struct msghdr
*kmsg
, int level
, int type
, int len
, void *data
)
216 struct compat_timeval ctv
;
217 struct compat_timespec cts
;
218 struct compat_cmsghdr __user
*cm
= (struct compat_cmsghdr __user
*) kmsg
->msg_control
;
219 struct compat_cmsghdr cmhdr
;
222 if (cm
== NULL
|| kmsg
->msg_controllen
< sizeof(*cm
)) {
223 kmsg
->msg_flags
|= MSG_CTRUNC
;
224 return 0; /* XXX: return error? check spec. */
227 if (level
== SOL_SOCKET
&& type
== SO_TIMESTAMP
) {
228 struct timeval
*tv
= (struct timeval
*)data
;
229 ctv
.tv_sec
= tv
->tv_sec
;
230 ctv
.tv_usec
= tv
->tv_usec
;
234 if (level
== SOL_SOCKET
&& type
== SO_TIMESTAMPNS
) {
235 struct timespec
*ts
= (struct timespec
*)data
;
236 cts
.tv_sec
= ts
->tv_sec
;
237 cts
.tv_nsec
= ts
->tv_nsec
;
242 cmlen
= CMSG_COMPAT_LEN(len
);
243 if (kmsg
->msg_controllen
< cmlen
) {
244 kmsg
->msg_flags
|= MSG_CTRUNC
;
245 cmlen
= kmsg
->msg_controllen
;
247 cmhdr
.cmsg_level
= level
;
248 cmhdr
.cmsg_type
= type
;
249 cmhdr
.cmsg_len
= cmlen
;
251 if (copy_to_user(cm
, &cmhdr
, sizeof cmhdr
))
253 if (copy_to_user(CMSG_COMPAT_DATA(cm
), data
, cmlen
- sizeof(struct compat_cmsghdr
)))
255 cmlen
= CMSG_COMPAT_SPACE(len
);
256 if (kmsg
->msg_controllen
< cmlen
)
257 cmlen
= kmsg
->msg_controllen
;
258 kmsg
->msg_control
+= cmlen
;
259 kmsg
->msg_controllen
-= cmlen
;
263 void scm_detach_fds_compat(struct msghdr
*kmsg
, struct scm_cookie
*scm
)
265 struct compat_cmsghdr __user
*cm
= (struct compat_cmsghdr __user
*) kmsg
->msg_control
;
266 int fdmax
= (kmsg
->msg_controllen
- sizeof(struct compat_cmsghdr
)) / sizeof(int);
267 int fdnum
= scm
->fp
->count
;
268 struct file
**fp
= scm
->fp
->fp
;
275 for (i
= 0, cmfptr
= (int __user
*) CMSG_COMPAT_DATA(cm
); i
< fdmax
; i
++, cmfptr
++) {
277 err
= security_file_receive(fp
[i
]);
280 err
= get_unused_fd_flags(MSG_CMSG_CLOEXEC
& kmsg
->msg_flags
285 err
= put_user(new_fd
, cmfptr
);
287 put_unused_fd(new_fd
);
290 /* Bump the usage count and install the file. */
292 fd_install(new_fd
, fp
[i
]);
296 int cmlen
= CMSG_COMPAT_LEN(i
* sizeof(int));
297 err
= put_user(SOL_SOCKET
, &cm
->cmsg_level
);
299 err
= put_user(SCM_RIGHTS
, &cm
->cmsg_type
);
301 err
= put_user(cmlen
, &cm
->cmsg_len
);
303 cmlen
= CMSG_COMPAT_SPACE(i
* sizeof(int));
304 kmsg
->msg_control
+= cmlen
;
305 kmsg
->msg_controllen
-= cmlen
;
309 kmsg
->msg_flags
|= MSG_CTRUNC
;
312 * All of the files that fit in the message have had their
313 * usage counts incremented, so we just free the list.
319 * A struct sock_filter is architecture independent.
321 struct compat_sock_fprog
{
323 compat_uptr_t filter
; /* struct sock_filter * */
326 static int do_set_attach_filter(struct socket
*sock
, int level
, int optname
,
327 char __user
*optval
, int optlen
)
329 struct compat_sock_fprog __user
*fprog32
= (struct compat_sock_fprog __user
*)optval
;
330 struct sock_fprog __user
*kfprog
= compat_alloc_user_space(sizeof(struct sock_fprog
));
334 if (!access_ok(VERIFY_READ
, fprog32
, sizeof(*fprog32
)) ||
335 !access_ok(VERIFY_WRITE
, kfprog
, sizeof(struct sock_fprog
)) ||
336 __get_user(len
, &fprog32
->len
) ||
337 __get_user(ptr
, &fprog32
->filter
) ||
338 __put_user(len
, &kfprog
->len
) ||
339 __put_user(compat_ptr(ptr
), &kfprog
->filter
))
342 return sock_setsockopt(sock
, level
, optname
, (char __user
*)kfprog
,
343 sizeof(struct sock_fprog
));
346 static int do_set_sock_timeout(struct socket
*sock
, int level
,
347 int optname
, char __user
*optval
, int optlen
)
349 struct compat_timeval __user
*up
= (struct compat_timeval __user
*) optval
;
350 struct timeval ktime
;
354 if (optlen
< sizeof(*up
))
356 if (!access_ok(VERIFY_READ
, up
, sizeof(*up
)) ||
357 __get_user(ktime
.tv_sec
, &up
->tv_sec
) ||
358 __get_user(ktime
.tv_usec
, &up
->tv_usec
))
362 err
= sock_setsockopt(sock
, level
, optname
, (char *) &ktime
, sizeof(ktime
));
368 static int compat_sock_setsockopt(struct socket
*sock
, int level
, int optname
,
369 char __user
*optval
, int optlen
)
371 if (optname
== SO_ATTACH_FILTER
)
372 return do_set_attach_filter(sock
, level
, optname
,
374 if (optname
== SO_RCVTIMEO
|| optname
== SO_SNDTIMEO
)
375 return do_set_sock_timeout(sock
, level
, optname
, optval
, optlen
);
377 return sock_setsockopt(sock
, level
, optname
, optval
, optlen
);
380 asmlinkage
long compat_sys_setsockopt(int fd
, int level
, int optname
,
381 char __user
*optval
, int optlen
)
389 if ((sock
= sockfd_lookup(fd
, &err
))!=NULL
)
391 err
= security_socket_setsockopt(sock
,level
,optname
);
397 if (level
== SOL_SOCKET
)
398 err
= compat_sock_setsockopt(sock
, level
,
399 optname
, optval
, optlen
);
400 else if (sock
->ops
->compat_setsockopt
)
401 err
= sock
->ops
->compat_setsockopt(sock
, level
,
402 optname
, optval
, optlen
);
404 err
= sock
->ops
->setsockopt(sock
, level
,
405 optname
, optval
, optlen
);
411 static int do_get_sock_timeout(struct socket
*sock
, int level
, int optname
,
412 char __user
*optval
, int __user
*optlen
)
414 struct compat_timeval __user
*up
;
415 struct timeval ktime
;
419 up
= (struct compat_timeval __user
*) optval
;
420 if (get_user(len
, optlen
))
422 if (len
< sizeof(*up
))
427 err
= sock_getsockopt(sock
, level
, optname
, (char *) &ktime
, &len
);
431 if (put_user(sizeof(*up
), optlen
) ||
432 !access_ok(VERIFY_WRITE
, up
, sizeof(*up
)) ||
433 __put_user(ktime
.tv_sec
, &up
->tv_sec
) ||
434 __put_user(ktime
.tv_usec
, &up
->tv_usec
))
440 static int compat_sock_getsockopt(struct socket
*sock
, int level
, int optname
,
441 char __user
*optval
, int __user
*optlen
)
443 if (optname
== SO_RCVTIMEO
|| optname
== SO_SNDTIMEO
)
444 return do_get_sock_timeout(sock
, level
, optname
, optval
, optlen
);
445 return sock_getsockopt(sock
, level
, optname
, optval
, optlen
);
448 int compat_sock_get_timestamp(struct sock
*sk
, struct timeval __user
*userstamp
)
450 struct compat_timeval __user
*ctv
=
451 (struct compat_timeval __user
*) userstamp
;
455 if (!sock_flag(sk
, SOCK_TIMESTAMP
))
456 sock_enable_timestamp(sk
);
457 tv
= ktime_to_timeval(sk
->sk_stamp
);
460 if (tv
.tv_sec
== 0) {
461 sk
->sk_stamp
= ktime_get_real();
462 tv
= ktime_to_timeval(sk
->sk_stamp
);
465 if (put_user(tv
.tv_sec
, &ctv
->tv_sec
) ||
466 put_user(tv
.tv_usec
, &ctv
->tv_usec
))
470 EXPORT_SYMBOL(compat_sock_get_timestamp
);
472 int compat_sock_get_timestampns(struct sock
*sk
, struct timespec __user
*userstamp
)
474 struct compat_timespec __user
*ctv
=
475 (struct compat_timespec __user
*) userstamp
;
479 if (!sock_flag(sk
, SOCK_TIMESTAMP
))
480 sock_enable_timestamp(sk
);
481 ts
= ktime_to_timespec(sk
->sk_stamp
);
484 if (ts
.tv_sec
== 0) {
485 sk
->sk_stamp
= ktime_get_real();
486 ts
= ktime_to_timespec(sk
->sk_stamp
);
489 if (put_user(ts
.tv_sec
, &ctv
->tv_sec
) ||
490 put_user(ts
.tv_nsec
, &ctv
->tv_nsec
))
494 EXPORT_SYMBOL(compat_sock_get_timestampns
);
496 asmlinkage
long compat_sys_getsockopt(int fd
, int level
, int optname
,
497 char __user
*optval
, int __user
*optlen
)
502 if ((sock
= sockfd_lookup(fd
, &err
))!=NULL
)
504 err
= security_socket_getsockopt(sock
, level
,
511 if (level
== SOL_SOCKET
)
512 err
= compat_sock_getsockopt(sock
, level
,
513 optname
, optval
, optlen
);
514 else if (sock
->ops
->compat_getsockopt
)
515 err
= sock
->ops
->compat_getsockopt(sock
, level
,
516 optname
, optval
, optlen
);
518 err
= sock
->ops
->getsockopt(sock
, level
,
519 optname
, optval
, optlen
);
524 /* Argument list sizes for compat_sys_socketcall */
525 #define AL(x) ((x) * sizeof(u32))
526 static unsigned char nas
[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
527 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
528 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)};
531 asmlinkage
long compat_sys_sendmsg(int fd
, struct compat_msghdr __user
*msg
, unsigned flags
)
533 return sys_sendmsg(fd
, (struct msghdr __user
*)msg
, flags
| MSG_CMSG_COMPAT
);
536 asmlinkage
long compat_sys_recvmsg(int fd
, struct compat_msghdr __user
*msg
, unsigned int flags
)
538 return sys_recvmsg(fd
, (struct msghdr __user
*)msg
, flags
| MSG_CMSG_COMPAT
);
541 asmlinkage
long compat_sys_socketcall(int call
, u32 __user
*args
)
547 if (call
< SYS_SOCKET
|| call
> SYS_RECVMSG
)
549 if (copy_from_user(a
, args
, nas
[call
]))
556 ret
= sys_socket(a0
, a1
, a
[2]);
559 ret
= sys_bind(a0
, compat_ptr(a1
), a
[2]);
562 ret
= sys_connect(a0
, compat_ptr(a1
), a
[2]);
565 ret
= sys_listen(a0
, a1
);
568 ret
= sys_accept(a0
, compat_ptr(a1
), compat_ptr(a
[2]));
570 case SYS_GETSOCKNAME
:
571 ret
= sys_getsockname(a0
, compat_ptr(a1
), compat_ptr(a
[2]));
573 case SYS_GETPEERNAME
:
574 ret
= sys_getpeername(a0
, compat_ptr(a1
), compat_ptr(a
[2]));
577 ret
= sys_socketpair(a0
, a1
, a
[2], compat_ptr(a
[3]));
580 ret
= sys_send(a0
, compat_ptr(a1
), a
[2], a
[3]);
583 ret
= sys_sendto(a0
, compat_ptr(a1
), a
[2], a
[3], compat_ptr(a
[4]), a
[5]);
586 ret
= sys_recv(a0
, compat_ptr(a1
), a
[2], a
[3]);
589 ret
= sys_recvfrom(a0
, compat_ptr(a1
), a
[2], a
[3], compat_ptr(a
[4]), compat_ptr(a
[5]));
592 ret
= sys_shutdown(a0
,a1
);
595 ret
= compat_sys_setsockopt(a0
, a1
, a
[2],
596 compat_ptr(a
[3]), a
[4]);
599 ret
= compat_sys_getsockopt(a0
, a1
, a
[2],
600 compat_ptr(a
[3]), compat_ptr(a
[4]));
603 ret
= compat_sys_sendmsg(a0
, compat_ptr(a1
), a
[2]);
606 ret
= compat_sys_recvmsg(a0
, compat_ptr(a1
), a
[2]);