initial commit with v2.6.9
[linux-2.6.9-moxart.git] / net / compat.c
blob6080b6439b968474163806d8f0bfa8a4546d4c21
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/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>
26 #include <net/scm.h>
27 #include <net/sock.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,
33 int niov)
35 int tot_len = 0;
37 while(niov > 0) {
38 compat_uptr_t buf;
39 compat_size_t len;
41 if(get_user(len, &uiov32->iov_len) ||
42 get_user(buf, &uiov32->iov_base)) {
43 tot_len = -EFAULT;
44 break;
46 tot_len += len;
47 kiov->iov_base = compat_ptr(buf);
48 kiov->iov_len = (__kernel_size_t) len;
49 uiov32++;
50 kiov++;
51 niov--;
53 return tot_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))
68 return -EFAULT;
69 kmsg->msg_name = compat_ptr(tmp1);
70 kmsg->msg_iov = compat_ptr(tmp2);
71 kmsg->msg_control = compat_ptr(tmp3);
72 return 0;
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)
79 int tot_len;
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,
85 kern_address);
86 if(err < 0)
87 return err;
89 kern_msg->msg_name = kern_address;
90 } else
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),
95 GFP_KERNEL);
96 if(!kern_iov)
97 return -ENOMEM;
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);
103 if(tot_len >= 0)
104 kern_msg->msg_iov = kern_iov;
105 else if(kern_msg->msg_iovlen > UIO_FASTIOV)
106 kfree(kern_iov);
108 return tot_len;
111 /* Bleech... */
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) >
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,
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;
148 kcmlen = 0;
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))
153 return -EFAULT;
155 /* Catch bogons. */
156 if(CMSG_COMPAT_ALIGN(ucmlen) <
157 CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)))
158 return -EINVAL;
159 if((unsigned long)(((char __user *)ucmsg - (char __user *)kmsg->msg_control)
160 + ucmlen) > kmsg->msg_controllen)
161 return -EINVAL;
163 tmp = ((ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg))) +
164 CMSG_ALIGN(sizeof(struct cmsghdr)));
165 kcmlen += tmp;
166 ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
168 if(kcmlen == 0)
169 return -EINVAL;
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
174 * from the user.
176 if(kcmlen > stackbuf_size)
177 kcmsg_base = kcmsg = kmalloc(kcmlen, GFP_KERNEL);
178 if(kcmsg == NULL)
179 return -ENOBUFS;
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;
198 /* Advance. */
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;
206 return 0;
208 out_free_efault:
209 if(kcmsg_base != (struct cmsghdr *)stackbuf)
210 kfree(kcmsg_base);
211 return -EFAULT;
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;
219 int cmlen;
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;
230 data = &ctv;
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))
244 return -EFAULT;
245 if(copy_to_user(CMSG_COMPAT_DATA(cm), data, cmlen - sizeof(struct compat_cmsghdr)))
246 return -EFAULT;
247 cmlen = CMSG_COMPAT_SPACE(len);
248 kmsg->msg_control += cmlen;
249 kmsg->msg_controllen -= cmlen;
250 return 0;
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;
259 int __user *cmfptr;
260 int err = 0, i;
262 if (fdnum < fdmax)
263 fdmax = fdnum;
265 for (i = 0, cmfptr = (int __user *) CMSG_COMPAT_DATA(cm); i < fdmax; i++, cmfptr++) {
266 int new_fd;
267 err = get_unused_fd();
268 if (err < 0)
269 break;
270 new_fd = err;
271 err = put_user(new_fd, cmfptr);
272 if (err) {
273 put_unused_fd(new_fd);
274 break;
276 /* Bump the usage count and install the file. */
277 get_file(fp[i]);
278 fd_install(new_fd, fp[i]);
281 if (i > 0) {
282 int cmlen = CMSG_COMPAT_LEN(i * sizeof(int));
283 if (!err)
284 err = put_user(SOL_SOCKET, &cm->cmsg_level);
285 if (!err)
286 err = put_user(SCM_RIGHTS, &cm->cmsg_type);
287 if (!err)
288 err = put_user(cmlen, &cm->cmsg_len);
289 if (!err) {
290 cmlen = CMSG_COMPAT_SPACE(i * sizeof(int));
291 kmsg->msg_control += cmlen;
292 kmsg->msg_controllen -= cmlen;
295 if (i < fdnum)
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.
302 __scm_destroy(scm);
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];
311 u32 valid_hooks;
312 u32 num_entries;
313 u32 size;
314 u32 hook_entry[NF_IP_NUMHOOKS];
315 u32 underflow[NF_IP_NUMHOOKS];
316 u32 num_counters;
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;
329 int ret;
330 int i;
331 compat_uptr_t ucntrs;
333 urepl = (struct compat_ipt_replace __user *)optval;
334 if (get_user(origsize, &urepl->size))
335 return -EFAULT;
337 /* Hack: Causes ipchains to give correct error msg --RR */
338 if (optlen != sizeof(*urepl) + origsize)
339 return -ENOPROTOOPT;
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);
347 ret = -EFAULT;
348 if (put_user(origsize, &repl_nat->size))
349 goto out;
351 if (!access_ok(VERIFY_READ, urepl, optlen) ||
352 !access_ok(VERIFY_WRITE, repl_nat, optlen))
353 goto out;
355 if (__copy_from_user(name, urepl->name, sizeof(urepl->name)) ||
356 __copy_to_user(repl_nat->name, name, sizeof(repl_nat->name)))
357 goto out;
359 if (__get_user(tmp32, &urepl->valid_hooks) ||
360 __put_user(tmp32, &repl_nat->valid_hooks))
361 goto out;
363 if (__get_user(tmp32, &urepl->num_entries) ||
364 __put_user(tmp32, &repl_nat->num_entries))
365 goto out;
367 if (__get_user(num_counters, &urepl->num_counters) ||
368 __put_user(num_counters, &repl_nat->num_counters))
369 goto out;
371 if (__get_user(ucntrs, &urepl->counters) ||
372 __put_user(compat_ptr(ucntrs), &repl_nat->counters))
373 goto out;
375 if (__copy_in_user(&repl_nat->entries[0],
376 &urepl->entries[0],
377 origsize))
378 goto out;
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]))
385 goto out;
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
392 * not misaligned ...
394 if (!access_ok(VERIFY_WRITE, compat_ptr(ucntrs),
395 num_counters * sizeof(struct ipt_counters)))
396 goto out;
399 ret = sys_setsockopt(fd, level, optname,
400 (char __user *)repl_nat, repl_nat_size);
402 out:
403 return ret;
407 * A struct sock_filter is architecture independent.
409 struct compat_sock_fprog {
410 u16 len;
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));
419 compat_uptr_t ptr;
420 u16 len;
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))
428 return -EFAULT;
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;
438 mm_segment_t old_fs;
439 int err;
441 if (optlen < sizeof(*up))
442 return -EINVAL;
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))
446 return -EFAULT;
447 old_fs = get_fs();
448 set_fs(KERNEL_DS);
449 err = sys_setsockopt(fd, level, optname, (char *) &ktime, sizeof(ktime));
450 set_fs(old_fs);
452 return err;
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,
461 optval, optlen);
462 if (level == SOL_SOCKET && optname == SO_ATTACH_FILTER)
463 return do_set_attach_filter(fd, level, optname,
464 optval, optlen);
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;
477 mm_segment_t old_fs;
478 int len, err;
480 up = (struct compat_timeval __user *) optval;
481 if (get_user(len, optlen))
482 return -EFAULT;
483 if (len < sizeof(*up))
484 return -EINVAL;
485 len = sizeof(ktime);
486 old_fs = get_fs();
487 set_fs(KERNEL_DS);
488 err = sys_getsockopt(fd, level, optname, (char *) &ktime, &len);
489 set_fs(old_fs);
491 if (!err) {
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))
496 err = -EFAULT;
498 return err;
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)};
514 #undef AL
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)
528 int ret;
529 u32 a[6];
530 u32 a0, a1;
532 if (call < SYS_SOCKET || call > SYS_RECVMSG)
533 return -EINVAL;
534 if (copy_from_user(a, args, nas[call]))
535 return -EFAULT;
536 a0 = a[0];
537 a1 = a[1];
539 switch(call) {
540 case SYS_SOCKET:
541 ret = sys_socket(a0, a1, a[2]);
542 break;
543 case SYS_BIND:
544 ret = sys_bind(a0, compat_ptr(a1), a[2]);
545 break;
546 case SYS_CONNECT:
547 ret = sys_connect(a0, compat_ptr(a1), a[2]);
548 break;
549 case SYS_LISTEN:
550 ret = sys_listen(a0, a1);
551 break;
552 case SYS_ACCEPT:
553 ret = sys_accept(a0, compat_ptr(a1), compat_ptr(a[2]));
554 break;
555 case SYS_GETSOCKNAME:
556 ret = sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2]));
557 break;
558 case SYS_GETPEERNAME:
559 ret = sys_getpeername(a0, compat_ptr(a1), compat_ptr(a[2]));
560 break;
561 case SYS_SOCKETPAIR:
562 ret = sys_socketpair(a0, a1, a[2], compat_ptr(a[3]));
563 break;
564 case SYS_SEND:
565 ret = sys_send(a0, compat_ptr(a1), a[2], a[3]);
566 break;
567 case SYS_SENDTO:
568 ret = sys_sendto(a0, compat_ptr(a1), a[2], a[3], compat_ptr(a[4]), a[5]);
569 break;
570 case SYS_RECV:
571 ret = sys_recv(a0, compat_ptr(a1), a[2], a[3]);
572 break;
573 case SYS_RECVFROM:
574 ret = sys_recvfrom(a0, compat_ptr(a1), a[2], a[3], compat_ptr(a[4]), compat_ptr(a[5]));
575 break;
576 case SYS_SHUTDOWN:
577 ret = sys_shutdown(a0,a1);
578 break;
579 case SYS_SETSOCKOPT:
580 ret = compat_sys_setsockopt(a0, a1, a[2],
581 compat_ptr(a[3]), a[4]);
582 break;
583 case SYS_GETSOCKOPT:
584 ret = compat_sys_getsockopt(a0, a1, a[2],
585 compat_ptr(a[3]), compat_ptr(a[4]));
586 break;
587 case SYS_SENDMSG:
588 ret = compat_sys_sendmsg(a0, compat_ptr(a1), a[2]);
589 break;
590 case SYS_RECVMSG:
591 ret = compat_sys_recvmsg(a0, compat_ptr(a1), a[2]);
592 break;
593 default:
594 ret = -EINVAL;
595 break;
597 return ret;