USB: oti6858: cleanup
[linux/fpc-iii.git] / net / compat.c
blob80013fb69a61e0a62bb05ee48643ad2466e2cef8
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 <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,
32 int niov)
34 int tot_len = 0;
36 while (niov > 0) {
37 compat_uptr_t buf;
38 compat_size_t len;
40 if (get_user(len, &uiov32->iov_len) ||
41 get_user(buf, &uiov32->iov_base)) {
42 tot_len = -EFAULT;
43 break;
45 tot_len += len;
46 kiov->iov_base = compat_ptr(buf);
47 kiov->iov_len = (__kernel_size_t) len;
48 uiov32++;
49 kiov++;
50 niov--;
52 return tot_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))
67 return -EFAULT;
68 kmsg->msg_name = compat_ptr(tmp1);
69 kmsg->msg_iov = compat_ptr(tmp2);
70 kmsg->msg_control = compat_ptr(tmp3);
71 return 0;
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)
78 int tot_len;
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,
84 kern_address);
85 if (err < 0)
86 return err;
88 kern_msg->msg_name = kern_address;
89 } else
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);
95 if (tot_len >= 0)
96 kern_msg->msg_iov = kern_iov;
98 return tot_len;
101 /* Bleech... */
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) >
127 msg->msg_controllen)
128 return NULL;
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;
143 int err = -EFAULT;
145 kcmlen = 0;
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))
150 return -EFAULT;
152 /* Catch bogons. */
153 if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg))
154 return -EINVAL;
156 tmp = ((ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg))) +
157 CMSG_ALIGN(sizeof(struct cmsghdr)));
158 tmp = CMSG_ALIGN(tmp);
159 kcmlen += tmp;
160 ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
162 if (kcmlen == 0)
163 return -EINVAL;
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
168 * from the user.
170 if (kcmlen > stackbuf_size)
171 kcmsg_base = kcmsg = sock_kmalloc(sk, kcmlen, GFP_KERNEL);
172 if (kcmsg == NULL)
173 return -ENOBUFS;
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))
180 goto Efault;
181 if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg))
182 goto Einval;
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))
186 goto Einval;
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)))))
194 goto Efault;
196 /* Advance. */
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;
204 return 0;
206 Einval:
207 err = -EINVAL;
208 Efault:
209 if (kcmsg_base != (struct cmsghdr *)stackbuf)
210 sock_kfree_s(sk, kcmsg_base, kcmlen);
211 return err;
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;
220 int cmlen;
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;
231 data = &ctv;
232 len = sizeof(ctv);
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;
238 data = &cts;
239 len = sizeof(cts);
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))
252 return -EFAULT;
253 if (copy_to_user(CMSG_COMPAT_DATA(cm), data, cmlen - sizeof(struct compat_cmsghdr)))
254 return -EFAULT;
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;
260 return 0;
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;
269 int __user *cmfptr;
270 int err = 0, i;
272 if (fdnum < fdmax)
273 fdmax = fdnum;
275 for (i = 0, cmfptr = (int __user *) CMSG_COMPAT_DATA(cm); i < fdmax; i++, cmfptr++) {
276 int new_fd;
277 err = security_file_receive(fp[i]);
278 if (err)
279 break;
280 err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & kmsg->msg_flags
281 ? O_CLOEXEC : 0);
282 if (err < 0)
283 break;
284 new_fd = err;
285 err = put_user(new_fd, cmfptr);
286 if (err) {
287 put_unused_fd(new_fd);
288 break;
290 /* Bump the usage count and install the file. */
291 get_file(fp[i]);
292 fd_install(new_fd, fp[i]);
295 if (i > 0) {
296 int cmlen = CMSG_COMPAT_LEN(i * sizeof(int));
297 err = put_user(SOL_SOCKET, &cm->cmsg_level);
298 if (!err)
299 err = put_user(SCM_RIGHTS, &cm->cmsg_type);
300 if (!err)
301 err = put_user(cmlen, &cm->cmsg_len);
302 if (!err) {
303 cmlen = CMSG_COMPAT_SPACE(i * sizeof(int));
304 kmsg->msg_control += cmlen;
305 kmsg->msg_controllen -= cmlen;
308 if (i < fdnum)
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.
315 __scm_destroy(scm);
319 * A struct sock_filter is architecture independent.
321 struct compat_sock_fprog {
322 u16 len;
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));
331 compat_uptr_t ptr;
332 u16 len;
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))
340 return -EFAULT;
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;
351 mm_segment_t old_fs;
352 int err;
354 if (optlen < sizeof(*up))
355 return -EINVAL;
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))
359 return -EFAULT;
360 old_fs = get_fs();
361 set_fs(KERNEL_DS);
362 err = sock_setsockopt(sock, level, optname, (char *) &ktime, sizeof(ktime));
363 set_fs(old_fs);
365 return err;
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,
373 optval, optlen);
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)
383 int err;
384 struct socket *sock;
386 if (optlen < 0)
387 return -EINVAL;
389 if ((sock = sockfd_lookup(fd, &err))!=NULL)
391 err = security_socket_setsockopt(sock,level,optname);
392 if (err) {
393 sockfd_put(sock);
394 return err;
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);
403 else
404 err = sock->ops->setsockopt(sock, level,
405 optname, optval, optlen);
406 sockfd_put(sock);
408 return err;
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;
416 mm_segment_t old_fs;
417 int len, err;
419 up = (struct compat_timeval __user *) optval;
420 if (get_user(len, optlen))
421 return -EFAULT;
422 if (len < sizeof(*up))
423 return -EINVAL;
424 len = sizeof(ktime);
425 old_fs = get_fs();
426 set_fs(KERNEL_DS);
427 err = sock_getsockopt(sock, level, optname, (char *) &ktime, &len);
428 set_fs(old_fs);
430 if (!err) {
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))
435 err = -EFAULT;
437 return err;
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;
452 int err = -ENOENT;
453 struct timeval tv;
455 if (!sock_flag(sk, SOCK_TIMESTAMP))
456 sock_enable_timestamp(sk);
457 tv = ktime_to_timeval(sk->sk_stamp);
458 if (tv.tv_sec == -1)
459 return err;
460 if (tv.tv_sec == 0) {
461 sk->sk_stamp = ktime_get_real();
462 tv = ktime_to_timeval(sk->sk_stamp);
464 err = 0;
465 if (put_user(tv.tv_sec, &ctv->tv_sec) ||
466 put_user(tv.tv_usec, &ctv->tv_usec))
467 err = -EFAULT;
468 return err;
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;
476 int err = -ENOENT;
477 struct timespec ts;
479 if (!sock_flag(sk, SOCK_TIMESTAMP))
480 sock_enable_timestamp(sk);
481 ts = ktime_to_timespec(sk->sk_stamp);
482 if (ts.tv_sec == -1)
483 return err;
484 if (ts.tv_sec == 0) {
485 sk->sk_stamp = ktime_get_real();
486 ts = ktime_to_timespec(sk->sk_stamp);
488 err = 0;
489 if (put_user(ts.tv_sec, &ctv->tv_sec) ||
490 put_user(ts.tv_nsec, &ctv->tv_nsec))
491 err = -EFAULT;
492 return err;
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)
499 int err;
500 struct socket *sock;
502 if ((sock = sockfd_lookup(fd, &err))!=NULL)
504 err = security_socket_getsockopt(sock, level,
505 optname);
506 if (err) {
507 sockfd_put(sock);
508 return err;
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);
517 else
518 err = sock->ops->getsockopt(sock, level,
519 optname, optval, optlen);
520 sockfd_put(sock);
522 return err;
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)};
529 #undef AL
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)
543 int ret;
544 u32 a[6];
545 u32 a0, a1;
547 if (call < SYS_SOCKET || call > SYS_RECVMSG)
548 return -EINVAL;
549 if (copy_from_user(a, args, nas[call]))
550 return -EFAULT;
551 a0 = a[0];
552 a1 = a[1];
554 switch (call) {
555 case SYS_SOCKET:
556 ret = sys_socket(a0, a1, a[2]);
557 break;
558 case SYS_BIND:
559 ret = sys_bind(a0, compat_ptr(a1), a[2]);
560 break;
561 case SYS_CONNECT:
562 ret = sys_connect(a0, compat_ptr(a1), a[2]);
563 break;
564 case SYS_LISTEN:
565 ret = sys_listen(a0, a1);
566 break;
567 case SYS_ACCEPT:
568 ret = sys_accept(a0, compat_ptr(a1), compat_ptr(a[2]));
569 break;
570 case SYS_GETSOCKNAME:
571 ret = sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2]));
572 break;
573 case SYS_GETPEERNAME:
574 ret = sys_getpeername(a0, compat_ptr(a1), compat_ptr(a[2]));
575 break;
576 case SYS_SOCKETPAIR:
577 ret = sys_socketpair(a0, a1, a[2], compat_ptr(a[3]));
578 break;
579 case SYS_SEND:
580 ret = sys_send(a0, compat_ptr(a1), a[2], a[3]);
581 break;
582 case SYS_SENDTO:
583 ret = sys_sendto(a0, compat_ptr(a1), a[2], a[3], compat_ptr(a[4]), a[5]);
584 break;
585 case SYS_RECV:
586 ret = sys_recv(a0, compat_ptr(a1), a[2], a[3]);
587 break;
588 case SYS_RECVFROM:
589 ret = sys_recvfrom(a0, compat_ptr(a1), a[2], a[3], compat_ptr(a[4]), compat_ptr(a[5]));
590 break;
591 case SYS_SHUTDOWN:
592 ret = sys_shutdown(a0,a1);
593 break;
594 case SYS_SETSOCKOPT:
595 ret = compat_sys_setsockopt(a0, a1, a[2],
596 compat_ptr(a[3]), a[4]);
597 break;
598 case SYS_GETSOCKOPT:
599 ret = compat_sys_getsockopt(a0, a1, a[2],
600 compat_ptr(a[3]), compat_ptr(a[4]));
601 break;
602 case SYS_SENDMSG:
603 ret = compat_sys_sendmsg(a0, compat_ptr(a1), a[2]);
604 break;
605 case SYS_RECVMSG:
606 ret = compat_sys_recvmsg(a0, compat_ptr(a1), a[2]);
607 break;
608 default:
609 ret = -EINVAL;
610 break;
612 return ret;