]>
Commit | Line | Data |
---|---|---|
4768fbcb | 1 | /* |
1da177e4 LT |
2 | * 32bit Socket syscall emulation. Based on arch/sparc64/kernel/sys_sparc32.c. |
3 | * | |
4 | * Copyright (C) 2000 VA Linux Co | |
5 | * Copyright (C) 2000 Don Dugger <[email protected]> | |
6 | * Copyright (C) 1999 Arun Sharma <[email protected]> | |
7 | * Copyright (C) 1997,1998 Jakub Jelinek ([email protected]) | |
8 | * Copyright (C) 1997 David S. Miller ([email protected]) | |
9 | * Copyright (C) 2000 Hewlett-Packard Co. | |
10 | * Copyright (C) 2000 David Mosberger-Tang <[email protected]> | |
4768fbcb | 11 | * Copyright (C) 2000,2001 Andi Kleen, SuSE Labs |
1da177e4 LT |
12 | */ |
13 | ||
14 | #include <linux/kernel.h> | |
15 | #include <linux/fs.h> | |
1da177e4 LT |
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/netfilter_ipv4/ip_tables.h> | |
24 | #include <linux/security.h> | |
25 | ||
26 | #include <net/scm.h> | |
27 | #include <net/sock.h> | |
28 | #include <asm/uaccess.h> | |
29 | #include <net/compat.h> | |
30 | ||
31 | static inline int iov_from_user_compat_to_kern(struct iovec *kiov, | |
32 | struct compat_iovec __user *uiov32, | |
33 | int niov) | |
34 | { | |
35 | int tot_len = 0; | |
36 | ||
e71a4783 | 37 | while (niov > 0) { |
1da177e4 LT |
38 | compat_uptr_t buf; |
39 | compat_size_t len; | |
40 | ||
e71a4783 | 41 | if (get_user(len, &uiov32->iov_len) || |
1da177e4 LT |
42 | get_user(buf, &uiov32->iov_base)) { |
43 | tot_len = -EFAULT; | |
44 | break; | |
45 | } | |
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--; | |
52 | } | |
53 | return tot_len; | |
54 | } | |
55 | ||
56 | int get_compat_msghdr(struct msghdr *kmsg, struct compat_msghdr __user *umsg) | |
57 | { | |
58 | compat_uptr_t tmp1, tmp2, tmp3; | |
59 | ||
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; | |
73 | } | |
74 | ||
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) | |
78 | { | |
79 | int tot_len; | |
80 | ||
e71a4783 SH |
81 | if (kern_msg->msg_namelen) { |
82 | if (mode==VERIFY_READ) { | |
1da177e4 LT |
83 | int err = move_addr_to_kernel(kern_msg->msg_name, |
84 | kern_msg->msg_namelen, | |
85 | kern_address); | |
e71a4783 | 86 | if (err < 0) |
1da177e4 LT |
87 | return err; |
88 | } | |
89 | kern_msg->msg_name = kern_address; | |
90 | } else | |
91 | kern_msg->msg_name = NULL; | |
92 | ||
1da177e4 LT |
93 | tot_len = iov_from_user_compat_to_kern(kern_iov, |
94 | (struct compat_iovec __user *)kern_msg->msg_iov, | |
95 | kern_msg->msg_iovlen); | |
e71a4783 | 96 | if (tot_len >= 0) |
1da177e4 | 97 | kern_msg->msg_iov = kern_iov; |
1da177e4 LT |
98 | |
99 | return tot_len; | |
100 | } | |
101 | ||
102 | /* Bleech... */ | |
103 | #define CMSG_COMPAT_ALIGN(len) ALIGN((len), sizeof(s32)) | |
104 | ||
105 | #define CMSG_COMPAT_DATA(cmsg) \ | |
106 | ((void __user *)((char __user *)(cmsg) + CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)))) | |
107 | #define CMSG_COMPAT_SPACE(len) \ | |
108 | (CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)) + CMSG_COMPAT_ALIGN(len)) | |
109 | #define CMSG_COMPAT_LEN(len) \ | |
110 | (CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)) + (len)) | |
111 | ||
112 | #define CMSG_COMPAT_FIRSTHDR(msg) \ | |
113 | (((msg)->msg_controllen) >= sizeof(struct compat_cmsghdr) ? \ | |
114 | (struct compat_cmsghdr __user *)((msg)->msg_control) : \ | |
115 | (struct compat_cmsghdr __user *)NULL) | |
116 | ||
117 | #define CMSG_COMPAT_OK(ucmlen, ucmsg, mhdr) \ | |
118 | ((ucmlen) >= sizeof(struct compat_cmsghdr) && \ | |
119 | (ucmlen) <= (unsigned long) \ | |
120 | ((mhdr)->msg_controllen - \ | |
121 | ((char *)(ucmsg) - (char *)(mhdr)->msg_control))) | |
122 | ||
123 | static inline struct compat_cmsghdr __user *cmsg_compat_nxthdr(struct msghdr *msg, | |
124 | struct compat_cmsghdr __user *cmsg, int cmsg_len) | |
125 | { | |
126 | char __user *ptr = (char __user *)cmsg + CMSG_COMPAT_ALIGN(cmsg_len); | |
127 | if ((unsigned long)(ptr + 1 - (char __user *)msg->msg_control) > | |
128 | msg->msg_controllen) | |
129 | return NULL; | |
130 | return (struct compat_cmsghdr __user *)ptr; | |
131 | } | |
132 | ||
133 | /* There is a lot of hair here because the alignment rules (and | |
134 | * thus placement) of cmsg headers and length are different for | |
135 | * 32-bit apps. -DaveM | |
136 | */ | |
8920e8f9 | 137 | int cmsghdr_from_user_compat_to_kern(struct msghdr *kmsg, struct sock *sk, |
1da177e4 LT |
138 | unsigned char *stackbuf, int stackbuf_size) |
139 | { | |
140 | struct compat_cmsghdr __user *ucmsg; | |
141 | struct cmsghdr *kcmsg, *kcmsg_base; | |
142 | compat_size_t ucmlen; | |
143 | __kernel_size_t kcmlen, tmp; | |
8920e8f9 | 144 | int err = -EFAULT; |
1da177e4 LT |
145 | |
146 | kcmlen = 0; | |
147 | kcmsg_base = kcmsg = (struct cmsghdr *)stackbuf; | |
148 | ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg); | |
e71a4783 SH |
149 | while (ucmsg != NULL) { |
150 | if (get_user(ucmlen, &ucmsg->cmsg_len)) | |
1da177e4 LT |
151 | return -EFAULT; |
152 | ||
153 | /* Catch bogons. */ | |
154 | if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg)) | |
155 | return -EINVAL; | |
156 | ||
157 | tmp = ((ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg))) + | |
158 | CMSG_ALIGN(sizeof(struct cmsghdr))); | |
8920e8f9 | 159 | tmp = CMSG_ALIGN(tmp); |
1da177e4 LT |
160 | kcmlen += tmp; |
161 | ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen); | |
162 | } | |
e71a4783 | 163 | if (kcmlen == 0) |
1da177e4 LT |
164 | return -EINVAL; |
165 | ||
166 | /* The kcmlen holds the 64-bit version of the control length. | |
167 | * It may not be modified as we do not stick it into the kmsg | |
168 | * until we have successfully copied over all of the data | |
169 | * from the user. | |
170 | */ | |
8920e8f9 AV |
171 | if (kcmlen > stackbuf_size) |
172 | kcmsg_base = kcmsg = sock_kmalloc(sk, kcmlen, GFP_KERNEL); | |
173 | if (kcmsg == NULL) | |
1da177e4 LT |
174 | return -ENOBUFS; |
175 | ||
176 | /* Now copy them over neatly. */ | |
177 | memset(kcmsg, 0, kcmlen); | |
178 | ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg); | |
e71a4783 | 179 | while (ucmsg != NULL) { |
8920e8f9 AV |
180 | if (__get_user(ucmlen, &ucmsg->cmsg_len)) |
181 | goto Efault; | |
182 | if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg)) | |
183 | goto Einval; | |
1da177e4 LT |
184 | tmp = ((ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg))) + |
185 | CMSG_ALIGN(sizeof(struct cmsghdr))); | |
8920e8f9 AV |
186 | if ((char *)kcmsg_base + kcmlen - (char *)kcmsg < CMSG_ALIGN(tmp)) |
187 | goto Einval; | |
1da177e4 | 188 | kcmsg->cmsg_len = tmp; |
8920e8f9 AV |
189 | tmp = CMSG_ALIGN(tmp); |
190 | if (__get_user(kcmsg->cmsg_level, &ucmsg->cmsg_level) || | |
191 | __get_user(kcmsg->cmsg_type, &ucmsg->cmsg_type) || | |
192 | copy_from_user(CMSG_DATA(kcmsg), | |
193 | CMSG_COMPAT_DATA(ucmsg), | |
194 | (ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg))))) | |
195 | goto Efault; | |
1da177e4 LT |
196 | |
197 | /* Advance. */ | |
8920e8f9 | 198 | kcmsg = (struct cmsghdr *)((char *)kcmsg + tmp); |
1da177e4 LT |
199 | ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen); |
200 | } | |
201 | ||
202 | /* Ok, looks like we made it. Hook it up and return success. */ | |
203 | kmsg->msg_control = kcmsg_base; | |
204 | kmsg->msg_controllen = kcmlen; | |
205 | return 0; | |
206 | ||
8920e8f9 AV |
207 | Einval: |
208 | err = -EINVAL; | |
209 | Efault: | |
210 | if (kcmsg_base != (struct cmsghdr *)stackbuf) | |
211 | sock_kfree_s(sk, kcmsg_base, kcmlen); | |
212 | return err; | |
1da177e4 LT |
213 | } |
214 | ||
215 | int put_cmsg_compat(struct msghdr *kmsg, int level, int type, int len, void *data) | |
216 | { | |
217 | struct compat_timeval ctv; | |
92f37fd2 | 218 | struct compat_timespec cts; |
1da177e4 LT |
219 | struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control; |
220 | struct compat_cmsghdr cmhdr; | |
221 | int cmlen; | |
222 | ||
e71a4783 | 223 | if (cm == NULL || kmsg->msg_controllen < sizeof(*cm)) { |
1da177e4 LT |
224 | kmsg->msg_flags |= MSG_CTRUNC; |
225 | return 0; /* XXX: return error? check spec. */ | |
226 | } | |
227 | ||
4768fbcb | 228 | if (level == SOL_SOCKET && type == SO_TIMESTAMP) { |
1da177e4 LT |
229 | struct timeval *tv = (struct timeval *)data; |
230 | ctv.tv_sec = tv->tv_sec; | |
231 | ctv.tv_usec = tv->tv_usec; | |
232 | data = &ctv; | |
92f37fd2 ED |
233 | len = sizeof(ctv); |
234 | } | |
235 | if (level == SOL_SOCKET && type == SO_TIMESTAMPNS) { | |
236 | struct timespec *ts = (struct timespec *)data; | |
237 | cts.tv_sec = ts->tv_sec; | |
238 | cts.tv_nsec = ts->tv_nsec; | |
239 | data = &cts; | |
240 | len = sizeof(cts); | |
4768fbcb YH |
241 | } |
242 | ||
1da177e4 | 243 | cmlen = CMSG_COMPAT_LEN(len); |
e71a4783 | 244 | if (kmsg->msg_controllen < cmlen) { |
1da177e4 LT |
245 | kmsg->msg_flags |= MSG_CTRUNC; |
246 | cmlen = kmsg->msg_controllen; | |
247 | } | |
248 | cmhdr.cmsg_level = level; | |
249 | cmhdr.cmsg_type = type; | |
250 | cmhdr.cmsg_len = cmlen; | |
251 | ||
e71a4783 | 252 | if (copy_to_user(cm, &cmhdr, sizeof cmhdr)) |
1da177e4 | 253 | return -EFAULT; |
e71a4783 | 254 | if (copy_to_user(CMSG_COMPAT_DATA(cm), data, cmlen - sizeof(struct compat_cmsghdr))) |
1da177e4 LT |
255 | return -EFAULT; |
256 | cmlen = CMSG_COMPAT_SPACE(len); | |
257 | kmsg->msg_control += cmlen; | |
258 | kmsg->msg_controllen -= cmlen; | |
259 | return 0; | |
260 | } | |
261 | ||
262 | void scm_detach_fds_compat(struct msghdr *kmsg, struct scm_cookie *scm) | |
263 | { | |
264 | struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control; | |
265 | int fdmax = (kmsg->msg_controllen - sizeof(struct compat_cmsghdr)) / sizeof(int); | |
266 | int fdnum = scm->fp->count; | |
267 | struct file **fp = scm->fp->fp; | |
268 | int __user *cmfptr; | |
269 | int err = 0, i; | |
270 | ||
271 | if (fdnum < fdmax) | |
272 | fdmax = fdnum; | |
273 | ||
274 | for (i = 0, cmfptr = (int __user *) CMSG_COMPAT_DATA(cm); i < fdmax; i++, cmfptr++) { | |
275 | int new_fd; | |
276 | err = security_file_receive(fp[i]); | |
277 | if (err) | |
278 | break; | |
4a19542e UD |
279 | err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & kmsg->msg_flags |
280 | ? O_CLOEXEC : 0); | |
1da177e4 LT |
281 | if (err < 0) |
282 | break; | |
283 | new_fd = err; | |
284 | err = put_user(new_fd, cmfptr); | |
285 | if (err) { | |
286 | put_unused_fd(new_fd); | |
287 | break; | |
288 | } | |
289 | /* Bump the usage count and install the file. */ | |
290 | get_file(fp[i]); | |
291 | fd_install(new_fd, fp[i]); | |
292 | } | |
293 | ||
294 | if (i > 0) { | |
295 | int cmlen = CMSG_COMPAT_LEN(i * sizeof(int)); | |
effee6a0 | 296 | err = put_user(SOL_SOCKET, &cm->cmsg_level); |
1da177e4 LT |
297 | if (!err) |
298 | err = put_user(SCM_RIGHTS, &cm->cmsg_type); | |
299 | if (!err) | |
300 | err = put_user(cmlen, &cm->cmsg_len); | |
301 | if (!err) { | |
302 | cmlen = CMSG_COMPAT_SPACE(i * sizeof(int)); | |
303 | kmsg->msg_control += cmlen; | |
304 | kmsg->msg_controllen -= cmlen; | |
305 | } | |
306 | } | |
307 | if (i < fdnum) | |
308 | kmsg->msg_flags |= MSG_CTRUNC; | |
309 | ||
310 | /* | |
311 | * All of the files that fit in the message have had their | |
312 | * usage counts incremented, so we just free the list. | |
313 | */ | |
314 | __scm_destroy(scm); | |
315 | } | |
316 | ||
317 | /* | |
318 | * For now, we assume that the compatibility and native version | |
319 | * of struct ipt_entry are the same - sfr. FIXME | |
320 | */ | |
321 | struct compat_ipt_replace { | |
322 | char name[IPT_TABLE_MAXNAMELEN]; | |
323 | u32 valid_hooks; | |
324 | u32 num_entries; | |
325 | u32 size; | |
326 | u32 hook_entry[NF_IP_NUMHOOKS]; | |
327 | u32 underflow[NF_IP_NUMHOOKS]; | |
328 | u32 num_counters; | |
329 | compat_uptr_t counters; /* struct ipt_counters * */ | |
330 | struct ipt_entry entries[0]; | |
331 | }; | |
332 | ||
333 | static int do_netfilter_replace(int fd, int level, int optname, | |
334 | char __user *optval, int optlen) | |
335 | { | |
336 | struct compat_ipt_replace __user *urepl; | |
337 | struct ipt_replace __user *repl_nat; | |
338 | char name[IPT_TABLE_MAXNAMELEN]; | |
339 | u32 origsize, tmp32, num_counters; | |
340 | unsigned int repl_nat_size; | |
341 | int ret; | |
342 | int i; | |
343 | compat_uptr_t ucntrs; | |
344 | ||
345 | urepl = (struct compat_ipt_replace __user *)optval; | |
346 | if (get_user(origsize, &urepl->size)) | |
347 | return -EFAULT; | |
348 | ||
349 | /* Hack: Causes ipchains to give correct error msg --RR */ | |
350 | if (optlen != sizeof(*urepl) + origsize) | |
351 | return -ENOPROTOOPT; | |
352 | ||
353 | /* XXX Assumes that size of ipt_entry is the same both in | |
354 | * native and compat environments. | |
355 | */ | |
356 | repl_nat_size = sizeof(*repl_nat) + origsize; | |
357 | repl_nat = compat_alloc_user_space(repl_nat_size); | |
358 | ||
359 | ret = -EFAULT; | |
360 | if (put_user(origsize, &repl_nat->size)) | |
361 | goto out; | |
362 | ||
363 | if (!access_ok(VERIFY_READ, urepl, optlen) || | |
364 | !access_ok(VERIFY_WRITE, repl_nat, optlen)) | |
365 | goto out; | |
366 | ||
367 | if (__copy_from_user(name, urepl->name, sizeof(urepl->name)) || | |
368 | __copy_to_user(repl_nat->name, name, sizeof(repl_nat->name))) | |
369 | goto out; | |
370 | ||
371 | if (__get_user(tmp32, &urepl->valid_hooks) || | |
372 | __put_user(tmp32, &repl_nat->valid_hooks)) | |
373 | goto out; | |
374 | ||
375 | if (__get_user(tmp32, &urepl->num_entries) || | |
376 | __put_user(tmp32, &repl_nat->num_entries)) | |
377 | goto out; | |
378 | ||
379 | if (__get_user(num_counters, &urepl->num_counters) || | |
380 | __put_user(num_counters, &repl_nat->num_counters)) | |
381 | goto out; | |
382 | ||
383 | if (__get_user(ucntrs, &urepl->counters) || | |
384 | __put_user(compat_ptr(ucntrs), &repl_nat->counters)) | |
385 | goto out; | |
386 | ||
387 | if (__copy_in_user(&repl_nat->entries[0], | |
388 | &urepl->entries[0], | |
389 | origsize)) | |
390 | goto out; | |
391 | ||
392 | for (i = 0; i < NF_IP_NUMHOOKS; i++) { | |
393 | if (__get_user(tmp32, &urepl->hook_entry[i]) || | |
394 | __put_user(tmp32, &repl_nat->hook_entry[i]) || | |
395 | __get_user(tmp32, &urepl->underflow[i]) || | |
396 | __put_user(tmp32, &repl_nat->underflow[i])) | |
397 | goto out; | |
398 | } | |
399 | ||
400 | /* | |
401 | * Since struct ipt_counters just contains two u_int64_t members | |
402 | * we can just do the access_ok check here and pass the (converted) | |
403 | * pointer into the standard syscall. We hope that the pointer is | |
404 | * not misaligned ... | |
405 | */ | |
406 | if (!access_ok(VERIFY_WRITE, compat_ptr(ucntrs), | |
407 | num_counters * sizeof(struct ipt_counters))) | |
408 | goto out; | |
409 | ||
410 | ||
411 | ret = sys_setsockopt(fd, level, optname, | |
412 | (char __user *)repl_nat, repl_nat_size); | |
413 | ||
414 | out: | |
415 | return ret; | |
416 | } | |
417 | ||
418 | /* | |
419 | * A struct sock_filter is architecture independent. | |
420 | */ | |
421 | struct compat_sock_fprog { | |
422 | u16 len; | |
423 | compat_uptr_t filter; /* struct sock_filter * */ | |
424 | }; | |
425 | ||
3fdadf7d | 426 | static int do_set_attach_filter(struct socket *sock, int level, int optname, |
1da177e4 LT |
427 | char __user *optval, int optlen) |
428 | { | |
429 | struct compat_sock_fprog __user *fprog32 = (struct compat_sock_fprog __user *)optval; | |
4768fbcb | 430 | struct sock_fprog __user *kfprog = compat_alloc_user_space(sizeof(struct sock_fprog)); |
1da177e4 LT |
431 | compat_uptr_t ptr; |
432 | u16 len; | |
433 | ||
434 | if (!access_ok(VERIFY_READ, fprog32, sizeof(*fprog32)) || | |
435 | !access_ok(VERIFY_WRITE, kfprog, sizeof(struct sock_fprog)) || | |
436 | __get_user(len, &fprog32->len) || | |
437 | __get_user(ptr, &fprog32->filter) || | |
438 | __put_user(len, &kfprog->len) || | |
439 | __put_user(compat_ptr(ptr), &kfprog->filter)) | |
440 | return -EFAULT; | |
441 | ||
3fdadf7d | 442 | return sock_setsockopt(sock, level, optname, (char __user *)kfprog, |
1da177e4 LT |
443 | sizeof(struct sock_fprog)); |
444 | } | |
445 | ||
3fdadf7d DM |
446 | static int do_set_sock_timeout(struct socket *sock, int level, |
447 | int optname, char __user *optval, int optlen) | |
1da177e4 LT |
448 | { |
449 | struct compat_timeval __user *up = (struct compat_timeval __user *) optval; | |
450 | struct timeval ktime; | |
451 | mm_segment_t old_fs; | |
452 | int err; | |
453 | ||
454 | if (optlen < sizeof(*up)) | |
455 | return -EINVAL; | |
456 | if (!access_ok(VERIFY_READ, up, sizeof(*up)) || | |
457 | __get_user(ktime.tv_sec, &up->tv_sec) || | |
458 | __get_user(ktime.tv_usec, &up->tv_usec)) | |
459 | return -EFAULT; | |
460 | old_fs = get_fs(); | |
461 | set_fs(KERNEL_DS); | |
3fdadf7d | 462 | err = sock_setsockopt(sock, level, optname, (char *) &ktime, sizeof(ktime)); |
1da177e4 LT |
463 | set_fs(old_fs); |
464 | ||
465 | return err; | |
466 | } | |
467 | ||
3fdadf7d DM |
468 | static int compat_sock_setsockopt(struct socket *sock, int level, int optname, |
469 | char __user *optval, int optlen) | |
470 | { | |
471 | if (optname == SO_ATTACH_FILTER) | |
472 | return do_set_attach_filter(sock, level, optname, | |
473 | optval, optlen); | |
474 | if (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO) | |
475 | return do_set_sock_timeout(sock, level, optname, optval, optlen); | |
476 | ||
477 | return sock_setsockopt(sock, level, optname, optval, optlen); | |
478 | } | |
479 | ||
1da177e4 LT |
480 | asmlinkage long compat_sys_setsockopt(int fd, int level, int optname, |
481 | char __user *optval, int optlen) | |
482 | { | |
3fdadf7d DM |
483 | int err; |
484 | struct socket *sock; | |
485 | ||
2722971c | 486 | if (level == SOL_IPV6 && optname == IPT_SO_SET_REPLACE) |
1da177e4 LT |
487 | return do_netfilter_replace(fd, level, optname, |
488 | optval, optlen); | |
1da177e4 | 489 | |
3fdadf7d DM |
490 | if (optlen < 0) |
491 | return -EINVAL; | |
492 | ||
493 | if ((sock = sockfd_lookup(fd, &err))!=NULL) | |
494 | { | |
495 | err = security_socket_setsockopt(sock,level,optname); | |
496 | if (err) { | |
497 | sockfd_put(sock); | |
498 | return err; | |
499 | } | |
500 | ||
501 | if (level == SOL_SOCKET) | |
502 | err = compat_sock_setsockopt(sock, level, | |
503 | optname, optval, optlen); | |
504 | else if (sock->ops->compat_setsockopt) | |
505 | err = sock->ops->compat_setsockopt(sock, level, | |
506 | optname, optval, optlen); | |
507 | else | |
508 | err = sock->ops->setsockopt(sock, level, | |
509 | optname, optval, optlen); | |
510 | sockfd_put(sock); | |
511 | } | |
512 | return err; | |
1da177e4 LT |
513 | } |
514 | ||
3fdadf7d | 515 | static int do_get_sock_timeout(struct socket *sock, int level, int optname, |
1da177e4 LT |
516 | char __user *optval, int __user *optlen) |
517 | { | |
518 | struct compat_timeval __user *up; | |
519 | struct timeval ktime; | |
520 | mm_segment_t old_fs; | |
521 | int len, err; | |
522 | ||
523 | up = (struct compat_timeval __user *) optval; | |
524 | if (get_user(len, optlen)) | |
525 | return -EFAULT; | |
526 | if (len < sizeof(*up)) | |
527 | return -EINVAL; | |
528 | len = sizeof(ktime); | |
529 | old_fs = get_fs(); | |
530 | set_fs(KERNEL_DS); | |
3fdadf7d | 531 | err = sock_getsockopt(sock, level, optname, (char *) &ktime, &len); |
1da177e4 LT |
532 | set_fs(old_fs); |
533 | ||
534 | if (!err) { | |
535 | if (put_user(sizeof(*up), optlen) || | |
536 | !access_ok(VERIFY_WRITE, up, sizeof(*up)) || | |
537 | __put_user(ktime.tv_sec, &up->tv_sec) || | |
538 | __put_user(ktime.tv_usec, &up->tv_usec)) | |
539 | err = -EFAULT; | |
540 | } | |
541 | return err; | |
542 | } | |
543 | ||
3fdadf7d | 544 | static int compat_sock_getsockopt(struct socket *sock, int level, int optname, |
1da177e4 LT |
545 | char __user *optval, int __user *optlen) |
546 | { | |
3fdadf7d DM |
547 | if (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO) |
548 | return do_get_sock_timeout(sock, level, optname, optval, optlen); | |
549 | return sock_getsockopt(sock, level, optname, optval, optlen); | |
1da177e4 LT |
550 | } |
551 | ||
f0ac2614 SP |
552 | int compat_sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp) |
553 | { | |
554 | struct compat_timeval __user *ctv = | |
555 | (struct compat_timeval __user*) userstamp; | |
556 | int err = -ENOENT; | |
b7aa0bf7 | 557 | struct timeval tv; |
f0ac2614 SP |
558 | |
559 | if (!sock_flag(sk, SOCK_TIMESTAMP)) | |
560 | sock_enable_timestamp(sk); | |
b7aa0bf7 ED |
561 | tv = ktime_to_timeval(sk->sk_stamp); |
562 | if (tv.tv_sec == -1) | |
f0ac2614 | 563 | return err; |
b7aa0bf7 ED |
564 | if (tv.tv_sec == 0) { |
565 | sk->sk_stamp = ktime_get_real(); | |
566 | tv = ktime_to_timeval(sk->sk_stamp); | |
567 | } | |
568 | err = 0; | |
569 | if (put_user(tv.tv_sec, &ctv->tv_sec) || | |
570 | put_user(tv.tv_usec, &ctv->tv_usec)) | |
f0ac2614 SP |
571 | err = -EFAULT; |
572 | return err; | |
573 | } | |
574 | EXPORT_SYMBOL(compat_sock_get_timestamp); | |
575 | ||
ae40eb1e ED |
576 | int compat_sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp) |
577 | { | |
578 | struct compat_timespec __user *ctv = | |
579 | (struct compat_timespec __user*) userstamp; | |
580 | int err = -ENOENT; | |
581 | struct timespec ts; | |
582 | ||
583 | if (!sock_flag(sk, SOCK_TIMESTAMP)) | |
584 | sock_enable_timestamp(sk); | |
585 | ts = ktime_to_timespec(sk->sk_stamp); | |
586 | if (ts.tv_sec == -1) | |
587 | return err; | |
588 | if (ts.tv_sec == 0) { | |
589 | sk->sk_stamp = ktime_get_real(); | |
590 | ts = ktime_to_timespec(sk->sk_stamp); | |
591 | } | |
592 | err = 0; | |
593 | if (put_user(ts.tv_sec, &ctv->tv_sec) || | |
594 | put_user(ts.tv_nsec, &ctv->tv_nsec)) | |
595 | err = -EFAULT; | |
596 | return err; | |
597 | } | |
598 | EXPORT_SYMBOL(compat_sock_get_timestampns); | |
599 | ||
3fdadf7d DM |
600 | asmlinkage long compat_sys_getsockopt(int fd, int level, int optname, |
601 | char __user *optval, int __user *optlen) | |
602 | { | |
603 | int err; | |
604 | struct socket *sock; | |
605 | ||
606 | if ((sock = sockfd_lookup(fd, &err))!=NULL) | |
607 | { | |
608 | err = security_socket_getsockopt(sock, level, | |
609 | optname); | |
610 | if (err) { | |
611 | sockfd_put(sock); | |
612 | return err; | |
613 | } | |
614 | ||
615 | if (level == SOL_SOCKET) | |
616 | err = compat_sock_getsockopt(sock, level, | |
617 | optname, optval, optlen); | |
618 | else if (sock->ops->compat_getsockopt) | |
619 | err = sock->ops->compat_getsockopt(sock, level, | |
620 | optname, optval, optlen); | |
621 | else | |
622 | err = sock->ops->getsockopt(sock, level, | |
623 | optname, optval, optlen); | |
624 | sockfd_put(sock); | |
625 | } | |
626 | return err; | |
627 | } | |
1da177e4 LT |
628 | /* Argument list sizes for compat_sys_socketcall */ |
629 | #define AL(x) ((x) * sizeof(u32)) | |
630 | static unsigned char nas[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3), | |
631 | AL(3),AL(3),AL(4),AL(4),AL(4),AL(6), | |
632 | AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)}; | |
633 | #undef AL | |
634 | ||
635 | asmlinkage long compat_sys_sendmsg(int fd, struct compat_msghdr __user *msg, unsigned flags) | |
636 | { | |
637 | return sys_sendmsg(fd, (struct msghdr __user *)msg, flags | MSG_CMSG_COMPAT); | |
638 | } | |
639 | ||
640 | asmlinkage long compat_sys_recvmsg(int fd, struct compat_msghdr __user *msg, unsigned int flags) | |
641 | { | |
642 | return sys_recvmsg(fd, (struct msghdr __user *)msg, flags | MSG_CMSG_COMPAT); | |
643 | } | |
644 | ||
645 | asmlinkage long compat_sys_socketcall(int call, u32 __user *args) | |
646 | { | |
647 | int ret; | |
648 | u32 a[6]; | |
649 | u32 a0, a1; | |
4768fbcb | 650 | |
1da177e4 LT |
651 | if (call < SYS_SOCKET || call > SYS_RECVMSG) |
652 | return -EINVAL; | |
653 | if (copy_from_user(a, args, nas[call])) | |
654 | return -EFAULT; | |
655 | a0 = a[0]; | |
656 | a1 = a[1]; | |
4768fbcb | 657 | |
e71a4783 | 658 | switch (call) { |
1da177e4 LT |
659 | case SYS_SOCKET: |
660 | ret = sys_socket(a0, a1, a[2]); | |
661 | break; | |
662 | case SYS_BIND: | |
663 | ret = sys_bind(a0, compat_ptr(a1), a[2]); | |
664 | break; | |
665 | case SYS_CONNECT: | |
666 | ret = sys_connect(a0, compat_ptr(a1), a[2]); | |
667 | break; | |
668 | case SYS_LISTEN: | |
669 | ret = sys_listen(a0, a1); | |
670 | break; | |
671 | case SYS_ACCEPT: | |
672 | ret = sys_accept(a0, compat_ptr(a1), compat_ptr(a[2])); | |
673 | break; | |
674 | case SYS_GETSOCKNAME: | |
675 | ret = sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2])); | |
676 | break; | |
677 | case SYS_GETPEERNAME: | |
678 | ret = sys_getpeername(a0, compat_ptr(a1), compat_ptr(a[2])); | |
679 | break; | |
680 | case SYS_SOCKETPAIR: | |
681 | ret = sys_socketpair(a0, a1, a[2], compat_ptr(a[3])); | |
682 | break; | |
683 | case SYS_SEND: | |
684 | ret = sys_send(a0, compat_ptr(a1), a[2], a[3]); | |
685 | break; | |
686 | case SYS_SENDTO: | |
687 | ret = sys_sendto(a0, compat_ptr(a1), a[2], a[3], compat_ptr(a[4]), a[5]); | |
688 | break; | |
689 | case SYS_RECV: | |
690 | ret = sys_recv(a0, compat_ptr(a1), a[2], a[3]); | |
691 | break; | |
692 | case SYS_RECVFROM: | |
693 | ret = sys_recvfrom(a0, compat_ptr(a1), a[2], a[3], compat_ptr(a[4]), compat_ptr(a[5])); | |
694 | break; | |
695 | case SYS_SHUTDOWN: | |
696 | ret = sys_shutdown(a0,a1); | |
697 | break; | |
698 | case SYS_SETSOCKOPT: | |
699 | ret = compat_sys_setsockopt(a0, a1, a[2], | |
700 | compat_ptr(a[3]), a[4]); | |
701 | break; | |
702 | case SYS_GETSOCKOPT: | |
703 | ret = compat_sys_getsockopt(a0, a1, a[2], | |
704 | compat_ptr(a[3]), compat_ptr(a[4])); | |
705 | break; | |
706 | case SYS_SENDMSG: | |
707 | ret = compat_sys_sendmsg(a0, compat_ptr(a1), a[2]); | |
708 | break; | |
709 | case SYS_RECVMSG: | |
710 | ret = compat_sys_recvmsg(a0, compat_ptr(a1), a[2]); | |
711 | break; | |
712 | default: | |
713 | ret = -EINVAL; | |
714 | break; | |
715 | } | |
716 | return ret; | |
717 | } |