rxrpc: Fix several cases where a padded len isn't checked in ticket decode
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / socket.c
1 /*
2 * NET An implementation of the SOCKET network access protocol.
3 *
4 * Version: @(#)socket.c 1.1.93 18/02/95
5 *
6 * Authors: Orest Zborowski, <obz@Kodak.COM>
7 * Ross Biro
8 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
9 *
10 * Fixes:
11 * Anonymous : NOTSOCK/BADF cleanup. Error fix in
12 * shutdown()
13 * Alan Cox : verify_area() fixes
14 * Alan Cox : Removed DDI
15 * Jonathan Kamens : SOCK_DGRAM reconnect bug
16 * Alan Cox : Moved a load of checks to the very
17 * top level.
18 * Alan Cox : Move address structures to/from user
19 * mode above the protocol layers.
20 * Rob Janssen : Allow 0 length sends.
21 * Alan Cox : Asynchronous I/O support (cribbed from the
22 * tty drivers).
23 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
24 * Jeff Uphoff : Made max number of sockets command-line
25 * configurable.
26 * Matti Aarnio : Made the number of sockets dynamic,
27 * to be allocated when needed, and mr.
28 * Uphoff's max is used as max to be
29 * allowed to allocate.
30 * Linus : Argh. removed all the socket allocation
31 * altogether: it's in the inode now.
32 * Alan Cox : Made sock_alloc()/sock_release() public
33 * for NetROM and future kernel nfsd type
34 * stuff.
35 * Alan Cox : sendmsg/recvmsg basics.
36 * Tom Dyas : Export net symbols.
37 * Marcin Dalecki : Fixed problems with CONFIG_NET="n".
38 * Alan Cox : Added thread locking to sys_* calls
39 * for sockets. May have errors at the
40 * moment.
41 * Kevin Buhr : Fixed the dumb errors in the above.
42 * Andi Kleen : Some small cleanups, optimizations,
43 * and fixed a copy_from_user() bug.
44 * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
45 * Tigran Aivazian : Made listen(2) backlog sanity checks
46 * protocol-independent
47 *
48 *
49 * This program is free software; you can redistribute it and/or
50 * modify it under the terms of the GNU General Public License
51 * as published by the Free Software Foundation; either version
52 * 2 of the License, or (at your option) any later version.
53 *
54 *
55 * This module is effectively the top level interface to the BSD socket
56 * paradigm.
57 *
58 * Based upon Swansea University Computer Society NET3.039
59 */
60
61 #include <linux/mm.h>
62 #include <linux/socket.h>
63 #include <linux/file.h>
64 #include <linux/net.h>
65 #include <linux/interrupt.h>
66 #include <linux/thread_info.h>
67 #include <linux/rcupdate.h>
68 #include <linux/netdevice.h>
69 #include <linux/proc_fs.h>
70 #include <linux/seq_file.h>
71 #include <linux/mutex.h>
72 #include <linux/if_bridge.h>
73 #include <linux/if_frad.h>
74 #include <linux/if_vlan.h>
75 #include <linux/init.h>
76 #include <linux/poll.h>
77 #include <linux/cache.h>
78 #include <linux/module.h>
79 #include <linux/highmem.h>
80 #include <linux/mount.h>
81 #include <linux/security.h>
82 #include <linux/syscalls.h>
83 #include <linux/compat.h>
84 #include <linux/kmod.h>
85 #include <linux/audit.h>
86 #include <linux/wireless.h>
87 #include <linux/nsproxy.h>
88 #include <linux/magic.h>
89 #include <linux/slab.h>
90 #include <linux/xattr.h>
91
92 #include <asm/uaccess.h>
93 #include <asm/unistd.h>
94
95 #include <net/compat.h>
96 #include <net/wext.h>
97 #include <net/cls_cgroup.h>
98
99 #include <net/sock.h>
100 #include <linux/netfilter.h>
101
102 #include <linux/if_tun.h>
103 #include <linux/ipv6_route.h>
104 #include <linux/route.h>
105 #include <linux/sockios.h>
106 #include <linux/atalk.h>
107
108 static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
109 static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
110 unsigned long nr_segs, loff_t pos);
111 static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
112 unsigned long nr_segs, loff_t pos);
113 static int sock_mmap(struct file *file, struct vm_area_struct *vma);
114
115 static int sock_close(struct inode *inode, struct file *file);
116 static unsigned int sock_poll(struct file *file,
117 struct poll_table_struct *wait);
118 static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
119 #ifdef CONFIG_COMPAT
120 static long compat_sock_ioctl(struct file *file,
121 unsigned int cmd, unsigned long arg);
122 #endif
123 static int sock_fasync(int fd, struct file *filp, int on);
124 static ssize_t sock_sendpage(struct file *file, struct page *page,
125 int offset, size_t size, loff_t *ppos, int more);
126 static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
127 struct pipe_inode_info *pipe, size_t len,
128 unsigned int flags);
129
130 /*
131 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
132 * in the operation structures but are done directly via the socketcall() multiplexor.
133 */
134
135 static const struct file_operations socket_file_ops = {
136 .owner = THIS_MODULE,
137 .llseek = no_llseek,
138 .aio_read = sock_aio_read,
139 .aio_write = sock_aio_write,
140 .poll = sock_poll,
141 .unlocked_ioctl = sock_ioctl,
142 #ifdef CONFIG_COMPAT
143 .compat_ioctl = compat_sock_ioctl,
144 #endif
145 .mmap = sock_mmap,
146 .open = sock_no_open, /* special open code to disallow open via /proc */
147 .release = sock_close,
148 .fasync = sock_fasync,
149 .sendpage = sock_sendpage,
150 .splice_write = generic_splice_sendpage,
151 .splice_read = sock_splice_read,
152 };
153
154 /*
155 * The protocol list. Each protocol is registered in here.
156 */
157
158 static DEFINE_SPINLOCK(net_family_lock);
159 static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
160
161 /*
162 * Statistics counters of the socket lists
163 */
164
165 static DEFINE_PER_CPU(int, sockets_in_use);
166
167 /*
168 * Support routines.
169 * Move socket addresses back and forth across the kernel/user
170 * divide and look after the messy bits.
171 */
172
173 /**
174 * move_addr_to_kernel - copy a socket address into kernel space
175 * @uaddr: Address in user space
176 * @kaddr: Address in kernel space
177 * @ulen: Length in user space
178 *
179 * The address is copied into kernel space. If the provided address is
180 * too long an error code of -EINVAL is returned. If the copy gives
181 * invalid addresses -EFAULT is returned. On a success 0 is returned.
182 */
183
184 int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
185 {
186 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
187 return -EINVAL;
188 if (ulen == 0)
189 return 0;
190 if (copy_from_user(kaddr, uaddr, ulen))
191 return -EFAULT;
192 return audit_sockaddr(ulen, kaddr);
193 }
194
195 /**
196 * move_addr_to_user - copy an address to user space
197 * @kaddr: kernel space address
198 * @klen: length of address in kernel
199 * @uaddr: user space address
200 * @ulen: pointer to user length field
201 *
202 * The value pointed to by ulen on entry is the buffer length available.
203 * This is overwritten with the buffer space used. -EINVAL is returned
204 * if an overlong buffer is specified or a negative buffer size. -EFAULT
205 * is returned if either the buffer or the length field are not
206 * accessible.
207 * After copying the data up to the limit the user specifies, the true
208 * length of the data is written over the length limit the user
209 * specified. Zero is returned for a success.
210 */
211
212 static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
213 void __user *uaddr, int __user *ulen)
214 {
215 int err;
216 int len;
217
218 BUG_ON(klen > sizeof(struct sockaddr_storage));
219 err = get_user(len, ulen);
220 if (err)
221 return err;
222 if (len > klen)
223 len = klen;
224 if (len < 0)
225 return -EINVAL;
226 if (len) {
227 if (audit_sockaddr(klen, kaddr))
228 return -ENOMEM;
229 if (copy_to_user(uaddr, kaddr, len))
230 return -EFAULT;
231 }
232 /*
233 * "fromlen shall refer to the value before truncation.."
234 * 1003.1g
235 */
236 return __put_user(klen, ulen);
237 }
238
239 static struct kmem_cache *sock_inode_cachep __read_mostly;
240
241 static struct inode *sock_alloc_inode(struct super_block *sb)
242 {
243 struct socket_alloc *ei;
244 struct socket_wq *wq;
245
246 ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
247 if (!ei)
248 return NULL;
249 wq = kmalloc(sizeof(*wq), GFP_KERNEL);
250 if (!wq) {
251 kmem_cache_free(sock_inode_cachep, ei);
252 return NULL;
253 }
254 init_waitqueue_head(&wq->wait);
255 wq->fasync_list = NULL;
256 RCU_INIT_POINTER(ei->socket.wq, wq);
257
258 ei->socket.state = SS_UNCONNECTED;
259 ei->socket.flags = 0;
260 ei->socket.ops = NULL;
261 ei->socket.sk = NULL;
262 ei->socket.file = NULL;
263
264 return &ei->vfs_inode;
265 }
266
267 static void sock_destroy_inode(struct inode *inode)
268 {
269 struct socket_alloc *ei;
270 struct socket_wq *wq;
271
272 ei = container_of(inode, struct socket_alloc, vfs_inode);
273 wq = rcu_dereference_protected(ei->socket.wq, 1);
274 kfree_rcu(wq, rcu);
275 kmem_cache_free(sock_inode_cachep, ei);
276 }
277
278 static void init_once(void *foo)
279 {
280 struct socket_alloc *ei = (struct socket_alloc *)foo;
281
282 inode_init_once(&ei->vfs_inode);
283 }
284
285 static int init_inodecache(void)
286 {
287 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
288 sizeof(struct socket_alloc),
289 0,
290 (SLAB_HWCACHE_ALIGN |
291 SLAB_RECLAIM_ACCOUNT |
292 SLAB_MEM_SPREAD),
293 init_once);
294 if (sock_inode_cachep == NULL)
295 return -ENOMEM;
296 return 0;
297 }
298
299 static const struct super_operations sockfs_ops = {
300 .alloc_inode = sock_alloc_inode,
301 .destroy_inode = sock_destroy_inode,
302 .statfs = simple_statfs,
303 };
304
305 /*
306 * sockfs_dname() is called from d_path().
307 */
308 static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
309 {
310 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
311 dentry->d_inode->i_ino);
312 }
313
314 static const struct dentry_operations sockfs_dentry_operations = {
315 .d_dname = sockfs_dname,
316 };
317
318 static struct dentry *sockfs_mount(struct file_system_type *fs_type,
319 int flags, const char *dev_name, void *data)
320 {
321 return mount_pseudo(fs_type, "socket:", &sockfs_ops,
322 &sockfs_dentry_operations, SOCKFS_MAGIC);
323 }
324
325 static struct vfsmount *sock_mnt __read_mostly;
326
327 static struct file_system_type sock_fs_type = {
328 .name = "sockfs",
329 .mount = sockfs_mount,
330 .kill_sb = kill_anon_super,
331 };
332
333 /*
334 * Obtains the first available file descriptor and sets it up for use.
335 *
336 * These functions create file structures and maps them to fd space
337 * of the current process. On success it returns file descriptor
338 * and file struct implicitly stored in sock->file.
339 * Note that another thread may close file descriptor before we return
340 * from this function. We use the fact that now we do not refer
341 * to socket after mapping. If one day we will need it, this
342 * function will increment ref. count on file by 1.
343 *
344 * In any case returned fd MAY BE not valid!
345 * This race condition is unavoidable
346 * with shared fd spaces, we cannot solve it inside kernel,
347 * but we take care of internal coherence yet.
348 */
349
350 struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
351 {
352 struct qstr name = { .name = "" };
353 struct path path;
354 struct file *file;
355
356 if (dname) {
357 name.name = dname;
358 name.len = strlen(name.name);
359 } else if (sock->sk) {
360 name.name = sock->sk->sk_prot_creator->name;
361 name.len = strlen(name.name);
362 }
363 path.dentry = d_alloc_pseudo(sock_mnt->mnt_sb, &name);
364 if (unlikely(!path.dentry))
365 return ERR_PTR(-ENOMEM);
366 path.mnt = mntget(sock_mnt);
367
368 d_instantiate(path.dentry, SOCK_INODE(sock));
369 SOCK_INODE(sock)->i_fop = &socket_file_ops;
370
371 file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
372 &socket_file_ops);
373 if (unlikely(IS_ERR(file))) {
374 /* drop dentry, keep inode */
375 ihold(path.dentry->d_inode);
376 path_put(&path);
377 return file;
378 }
379
380 sock->file = file;
381 file->f_flags = O_RDWR | (flags & O_NONBLOCK);
382 file->private_data = sock;
383 return file;
384 }
385 EXPORT_SYMBOL(sock_alloc_file);
386
387 static int sock_map_fd(struct socket *sock, int flags)
388 {
389 struct file *newfile;
390 int fd = get_unused_fd_flags(flags);
391 if (unlikely(fd < 0))
392 return fd;
393
394 newfile = sock_alloc_file(sock, flags, NULL);
395 if (likely(!IS_ERR(newfile))) {
396 fd_install(fd, newfile);
397 return fd;
398 }
399
400 put_unused_fd(fd);
401 return PTR_ERR(newfile);
402 }
403
404 struct socket *sock_from_file(struct file *file, int *err)
405 {
406 if (file->f_op == &socket_file_ops)
407 return file->private_data; /* set in sock_map_fd */
408
409 *err = -ENOTSOCK;
410 return NULL;
411 }
412 EXPORT_SYMBOL(sock_from_file);
413
414 /**
415 * sockfd_lookup - Go from a file number to its socket slot
416 * @fd: file handle
417 * @err: pointer to an error code return
418 *
419 * The file handle passed in is locked and the socket it is bound
420 * too is returned. If an error occurs the err pointer is overwritten
421 * with a negative errno code and NULL is returned. The function checks
422 * for both invalid handles and passing a handle which is not a socket.
423 *
424 * On a success the socket object pointer is returned.
425 */
426
427 struct socket *sockfd_lookup(int fd, int *err)
428 {
429 struct file *file;
430 struct socket *sock;
431
432 file = fget(fd);
433 if (!file) {
434 *err = -EBADF;
435 return NULL;
436 }
437
438 sock = sock_from_file(file, err);
439 if (!sock)
440 fput(file);
441 return sock;
442 }
443 EXPORT_SYMBOL(sockfd_lookup);
444
445 static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
446 {
447 struct file *file;
448 struct socket *sock;
449
450 *err = -EBADF;
451 file = fget_light(fd, fput_needed);
452 if (file) {
453 sock = sock_from_file(file, err);
454 if (sock)
455 return sock;
456 fput_light(file, *fput_needed);
457 }
458 return NULL;
459 }
460
461 #define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
462 #define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
463 #define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
464 static ssize_t sockfs_getxattr(struct dentry *dentry,
465 const char *name, void *value, size_t size)
466 {
467 const char *proto_name;
468 size_t proto_size;
469 int error;
470
471 error = -ENODATA;
472 if (!strncmp(name, XATTR_NAME_SOCKPROTONAME, XATTR_NAME_SOCKPROTONAME_LEN)) {
473 proto_name = dentry->d_name.name;
474 proto_size = strlen(proto_name);
475
476 if (value) {
477 error = -ERANGE;
478 if (proto_size + 1 > size)
479 goto out;
480
481 strncpy(value, proto_name, proto_size + 1);
482 }
483 error = proto_size + 1;
484 }
485
486 out:
487 return error;
488 }
489
490 static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
491 size_t size)
492 {
493 ssize_t len;
494 ssize_t used = 0;
495
496 len = security_inode_listsecurity(dentry->d_inode, buffer, size);
497 if (len < 0)
498 return len;
499 used += len;
500 if (buffer) {
501 if (size < used)
502 return -ERANGE;
503 buffer += len;
504 }
505
506 len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
507 used += len;
508 if (buffer) {
509 if (size < used)
510 return -ERANGE;
511 memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
512 buffer += len;
513 }
514
515 return used;
516 }
517
518 static const struct inode_operations sockfs_inode_ops = {
519 .getxattr = sockfs_getxattr,
520 .listxattr = sockfs_listxattr,
521 };
522
523 /**
524 * sock_alloc - allocate a socket
525 *
526 * Allocate a new inode and socket object. The two are bound together
527 * and initialised. The socket is then returned. If we are out of inodes
528 * NULL is returned.
529 */
530
531 static struct socket *sock_alloc(void)
532 {
533 struct inode *inode;
534 struct socket *sock;
535
536 inode = new_inode_pseudo(sock_mnt->mnt_sb);
537 if (!inode)
538 return NULL;
539
540 sock = SOCKET_I(inode);
541
542 kmemcheck_annotate_bitfield(sock, type);
543 inode->i_ino = get_next_ino();
544 inode->i_mode = S_IFSOCK | S_IRWXUGO;
545 inode->i_uid = current_fsuid();
546 inode->i_gid = current_fsgid();
547 inode->i_op = &sockfs_inode_ops;
548
549 this_cpu_add(sockets_in_use, 1);
550 return sock;
551 }
552
553 /*
554 * In theory you can't get an open on this inode, but /proc provides
555 * a back door. Remember to keep it shut otherwise you'll let the
556 * creepy crawlies in.
557 */
558
559 static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
560 {
561 return -ENXIO;
562 }
563
564 const struct file_operations bad_sock_fops = {
565 .owner = THIS_MODULE,
566 .open = sock_no_open,
567 .llseek = noop_llseek,
568 };
569
570 /**
571 * sock_release - close a socket
572 * @sock: socket to close
573 *
574 * The socket is released from the protocol stack if it has a release
575 * callback, and the inode is then released if the socket is bound to
576 * an inode not a file.
577 */
578
579 void sock_release(struct socket *sock)
580 {
581 if (sock->ops) {
582 struct module *owner = sock->ops->owner;
583
584 sock->ops->release(sock);
585 sock->ops = NULL;
586 module_put(owner);
587 }
588
589 if (rcu_dereference_protected(sock->wq, 1)->fasync_list)
590 printk(KERN_ERR "sock_release: fasync list not empty!\n");
591
592 if (test_bit(SOCK_EXTERNALLY_ALLOCATED, &sock->flags))
593 return;
594
595 this_cpu_sub(sockets_in_use, 1);
596 if (!sock->file) {
597 iput(SOCK_INODE(sock));
598 return;
599 }
600 sock->file = NULL;
601 }
602 EXPORT_SYMBOL(sock_release);
603
604 void sock_tx_timestamp(struct sock *sk, __u8 *tx_flags)
605 {
606 *tx_flags = 0;
607 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
608 *tx_flags |= SKBTX_HW_TSTAMP;
609 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
610 *tx_flags |= SKBTX_SW_TSTAMP;
611 if (sock_flag(sk, SOCK_WIFI_STATUS))
612 *tx_flags |= SKBTX_WIFI_STATUS;
613 }
614 EXPORT_SYMBOL(sock_tx_timestamp);
615
616 static inline int __sock_sendmsg_nosec(struct kiocb *iocb, struct socket *sock,
617 struct msghdr *msg, size_t size)
618 {
619 struct sock_iocb *si = kiocb_to_siocb(iocb);
620
621 si->sock = sock;
622 si->scm = NULL;
623 si->msg = msg;
624 si->size = size;
625
626 return sock->ops->sendmsg(iocb, sock, msg, size);
627 }
628
629 static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
630 struct msghdr *msg, size_t size)
631 {
632 int err = security_socket_sendmsg(sock, msg, size);
633
634 return err ?: __sock_sendmsg_nosec(iocb, sock, msg, size);
635 }
636
637 int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
638 {
639 struct kiocb iocb;
640 struct sock_iocb siocb;
641 int ret;
642
643 init_sync_kiocb(&iocb, NULL);
644 iocb.private = &siocb;
645 ret = __sock_sendmsg(&iocb, sock, msg, size);
646 if (-EIOCBQUEUED == ret)
647 ret = wait_on_sync_kiocb(&iocb);
648 return ret;
649 }
650 EXPORT_SYMBOL(sock_sendmsg);
651
652 static int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg, size_t size)
653 {
654 struct kiocb iocb;
655 struct sock_iocb siocb;
656 int ret;
657
658 init_sync_kiocb(&iocb, NULL);
659 iocb.private = &siocb;
660 ret = __sock_sendmsg_nosec(&iocb, sock, msg, size);
661 if (-EIOCBQUEUED == ret)
662 ret = wait_on_sync_kiocb(&iocb);
663 return ret;
664 }
665
666 int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
667 struct kvec *vec, size_t num, size_t size)
668 {
669 mm_segment_t oldfs = get_fs();
670 int result;
671
672 set_fs(KERNEL_DS);
673 /*
674 * the following is safe, since for compiler definitions of kvec and
675 * iovec are identical, yielding the same in-core layout and alignment
676 */
677 msg->msg_iov = (struct iovec *)vec;
678 msg->msg_iovlen = num;
679 result = sock_sendmsg(sock, msg, size);
680 set_fs(oldfs);
681 return result;
682 }
683 EXPORT_SYMBOL(kernel_sendmsg);
684
685 /*
686 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
687 */
688 void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
689 struct sk_buff *skb)
690 {
691 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
692 struct timespec ts[3];
693 int empty = 1;
694 struct skb_shared_hwtstamps *shhwtstamps =
695 skb_hwtstamps(skb);
696
697 /* Race occurred between timestamp enabling and packet
698 receiving. Fill in the current time for now. */
699 if (need_software_tstamp && skb->tstamp.tv64 == 0)
700 __net_timestamp(skb);
701
702 if (need_software_tstamp) {
703 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
704 struct timeval tv;
705 skb_get_timestamp(skb, &tv);
706 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
707 sizeof(tv), &tv);
708 } else {
709 skb_get_timestampns(skb, &ts[0]);
710 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
711 sizeof(ts[0]), &ts[0]);
712 }
713 }
714
715
716 memset(ts, 0, sizeof(ts));
717 if (sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE) &&
718 ktime_to_timespec_cond(skb->tstamp, ts + 0))
719 empty = 0;
720 if (shhwtstamps) {
721 if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
722 ktime_to_timespec_cond(shhwtstamps->syststamp, ts + 1))
723 empty = 0;
724 if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
725 ktime_to_timespec_cond(shhwtstamps->hwtstamp, ts + 2))
726 empty = 0;
727 }
728 if (!empty)
729 put_cmsg(msg, SOL_SOCKET,
730 SCM_TIMESTAMPING, sizeof(ts), &ts);
731 }
732 EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
733
734 void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
735 struct sk_buff *skb)
736 {
737 int ack;
738
739 if (!sock_flag(sk, SOCK_WIFI_STATUS))
740 return;
741 if (!skb->wifi_acked_valid)
742 return;
743
744 ack = skb->wifi_acked;
745
746 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
747 }
748 EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
749
750 static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
751 struct sk_buff *skb)
752 {
753 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && skb->dropcount)
754 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
755 sizeof(__u32), &skb->dropcount);
756 }
757
758 void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
759 struct sk_buff *skb)
760 {
761 sock_recv_timestamp(msg, sk, skb);
762 sock_recv_drops(msg, sk, skb);
763 }
764 EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
765
766 static inline int __sock_recvmsg_nosec(struct kiocb *iocb, struct socket *sock,
767 struct msghdr *msg, size_t size, int flags)
768 {
769 struct sock_iocb *si = kiocb_to_siocb(iocb);
770
771 si->sock = sock;
772 si->scm = NULL;
773 si->msg = msg;
774 si->size = size;
775 si->flags = flags;
776
777 return sock->ops->recvmsg(iocb, sock, msg, size, flags);
778 }
779
780 static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
781 struct msghdr *msg, size_t size, int flags)
782 {
783 int err = security_socket_recvmsg(sock, msg, size, flags);
784
785 return err ?: __sock_recvmsg_nosec(iocb, sock, msg, size, flags);
786 }
787
788 int sock_recvmsg(struct socket *sock, struct msghdr *msg,
789 size_t size, int flags)
790 {
791 struct kiocb iocb;
792 struct sock_iocb siocb;
793 int ret;
794
795 init_sync_kiocb(&iocb, NULL);
796 iocb.private = &siocb;
797 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
798 if (-EIOCBQUEUED == ret)
799 ret = wait_on_sync_kiocb(&iocb);
800 return ret;
801 }
802 EXPORT_SYMBOL(sock_recvmsg);
803
804 static int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
805 size_t size, int flags)
806 {
807 struct kiocb iocb;
808 struct sock_iocb siocb;
809 int ret;
810
811 init_sync_kiocb(&iocb, NULL);
812 iocb.private = &siocb;
813 ret = __sock_recvmsg_nosec(&iocb, sock, msg, size, flags);
814 if (-EIOCBQUEUED == ret)
815 ret = wait_on_sync_kiocb(&iocb);
816 return ret;
817 }
818
819 /**
820 * kernel_recvmsg - Receive a message from a socket (kernel space)
821 * @sock: The socket to receive the message from
822 * @msg: Received message
823 * @vec: Input s/g array for message data
824 * @num: Size of input s/g array
825 * @size: Number of bytes to read
826 * @flags: Message flags (MSG_DONTWAIT, etc...)
827 *
828 * On return the msg structure contains the scatter/gather array passed in the
829 * vec argument. The array is modified so that it consists of the unfilled
830 * portion of the original array.
831 *
832 * The returned value is the total number of bytes received, or an error.
833 */
834 int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
835 struct kvec *vec, size_t num, size_t size, int flags)
836 {
837 mm_segment_t oldfs = get_fs();
838 int result;
839
840 set_fs(KERNEL_DS);
841 /*
842 * the following is safe, since for compiler definitions of kvec and
843 * iovec are identical, yielding the same in-core layout and alignment
844 */
845 msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
846 result = sock_recvmsg(sock, msg, size, flags);
847 set_fs(oldfs);
848 return result;
849 }
850 EXPORT_SYMBOL(kernel_recvmsg);
851
852 static void sock_aio_dtor(struct kiocb *iocb)
853 {
854 kfree(iocb->private);
855 }
856
857 static ssize_t sock_sendpage(struct file *file, struct page *page,
858 int offset, size_t size, loff_t *ppos, int more)
859 {
860 struct socket *sock;
861 int flags;
862
863 sock = file->private_data;
864
865 flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
866 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
867 flags |= more;
868
869 return kernel_sendpage(sock, page, offset, size, flags);
870 }
871
872 static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
873 struct pipe_inode_info *pipe, size_t len,
874 unsigned int flags)
875 {
876 struct socket *sock = file->private_data;
877
878 if (unlikely(!sock->ops->splice_read))
879 return -EINVAL;
880
881 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
882 }
883
884 static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
885 struct sock_iocb *siocb)
886 {
887 if (!is_sync_kiocb(iocb)) {
888 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
889 if (!siocb)
890 return NULL;
891 iocb->ki_dtor = sock_aio_dtor;
892 }
893
894 siocb->kiocb = iocb;
895 iocb->private = siocb;
896 return siocb;
897 }
898
899 static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
900 struct file *file, const struct iovec *iov,
901 unsigned long nr_segs)
902 {
903 struct socket *sock = file->private_data;
904 size_t size = 0;
905 int i;
906
907 for (i = 0; i < nr_segs; i++)
908 size += iov[i].iov_len;
909
910 msg->msg_name = NULL;
911 msg->msg_namelen = 0;
912 msg->msg_control = NULL;
913 msg->msg_controllen = 0;
914 msg->msg_iov = (struct iovec *)iov;
915 msg->msg_iovlen = nr_segs;
916 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
917
918 return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
919 }
920
921 static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
922 unsigned long nr_segs, loff_t pos)
923 {
924 struct sock_iocb siocb, *x;
925
926 if (pos != 0)
927 return -ESPIPE;
928
929 if (iocb->ki_left == 0) /* Match SYS5 behaviour */
930 return 0;
931
932
933 x = alloc_sock_iocb(iocb, &siocb);
934 if (!x)
935 return -ENOMEM;
936 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
937 }
938
939 static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
940 struct file *file, const struct iovec *iov,
941 unsigned long nr_segs)
942 {
943 struct socket *sock = file->private_data;
944 size_t size = 0;
945 int i;
946
947 for (i = 0; i < nr_segs; i++)
948 size += iov[i].iov_len;
949
950 msg->msg_name = NULL;
951 msg->msg_namelen = 0;
952 msg->msg_control = NULL;
953 msg->msg_controllen = 0;
954 msg->msg_iov = (struct iovec *)iov;
955 msg->msg_iovlen = nr_segs;
956 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
957 if (sock->type == SOCK_SEQPACKET)
958 msg->msg_flags |= MSG_EOR;
959
960 return __sock_sendmsg(iocb, sock, msg, size);
961 }
962
963 static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
964 unsigned long nr_segs, loff_t pos)
965 {
966 struct sock_iocb siocb, *x;
967
968 if (pos != 0)
969 return -ESPIPE;
970
971 x = alloc_sock_iocb(iocb, &siocb);
972 if (!x)
973 return -ENOMEM;
974
975 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
976 }
977
978 /*
979 * Atomic setting of ioctl hooks to avoid race
980 * with module unload.
981 */
982
983 static DEFINE_MUTEX(br_ioctl_mutex);
984 static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
985
986 void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
987 {
988 mutex_lock(&br_ioctl_mutex);
989 br_ioctl_hook = hook;
990 mutex_unlock(&br_ioctl_mutex);
991 }
992 EXPORT_SYMBOL(brioctl_set);
993
994 static DEFINE_MUTEX(vlan_ioctl_mutex);
995 static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
996
997 void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
998 {
999 mutex_lock(&vlan_ioctl_mutex);
1000 vlan_ioctl_hook = hook;
1001 mutex_unlock(&vlan_ioctl_mutex);
1002 }
1003 EXPORT_SYMBOL(vlan_ioctl_set);
1004
1005 static DEFINE_MUTEX(dlci_ioctl_mutex);
1006 static int (*dlci_ioctl_hook) (unsigned int, void __user *);
1007
1008 void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
1009 {
1010 mutex_lock(&dlci_ioctl_mutex);
1011 dlci_ioctl_hook = hook;
1012 mutex_unlock(&dlci_ioctl_mutex);
1013 }
1014 EXPORT_SYMBOL(dlci_ioctl_set);
1015
1016 static long sock_do_ioctl(struct net *net, struct socket *sock,
1017 unsigned int cmd, unsigned long arg)
1018 {
1019 int err;
1020 void __user *argp = (void __user *)arg;
1021
1022 err = sock->ops->ioctl(sock, cmd, arg);
1023
1024 /*
1025 * If this ioctl is unknown try to hand it down
1026 * to the NIC driver.
1027 */
1028 if (err == -ENOIOCTLCMD)
1029 err = dev_ioctl(net, cmd, argp);
1030
1031 return err;
1032 }
1033
1034 /*
1035 * With an ioctl, arg may well be a user mode pointer, but we don't know
1036 * what to do with it - that's up to the protocol still.
1037 */
1038
1039 static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1040 {
1041 struct socket *sock;
1042 struct sock *sk;
1043 void __user *argp = (void __user *)arg;
1044 int pid, err;
1045 struct net *net;
1046
1047 sock = file->private_data;
1048 sk = sock->sk;
1049 net = sock_net(sk);
1050 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
1051 err = dev_ioctl(net, cmd, argp);
1052 } else
1053 #ifdef CONFIG_WEXT_CORE
1054 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
1055 err = dev_ioctl(net, cmd, argp);
1056 } else
1057 #endif
1058 switch (cmd) {
1059 case FIOSETOWN:
1060 case SIOCSPGRP:
1061 err = -EFAULT;
1062 if (get_user(pid, (int __user *)argp))
1063 break;
1064 err = f_setown(sock->file, pid, 1);
1065 break;
1066 case FIOGETOWN:
1067 case SIOCGPGRP:
1068 err = put_user(f_getown(sock->file),
1069 (int __user *)argp);
1070 break;
1071 case SIOCGIFBR:
1072 case SIOCSIFBR:
1073 case SIOCBRADDBR:
1074 case SIOCBRDELBR:
1075 err = -ENOPKG;
1076 if (!br_ioctl_hook)
1077 request_module("bridge");
1078
1079 mutex_lock(&br_ioctl_mutex);
1080 if (br_ioctl_hook)
1081 err = br_ioctl_hook(net, cmd, argp);
1082 mutex_unlock(&br_ioctl_mutex);
1083 break;
1084 case SIOCGIFVLAN:
1085 case SIOCSIFVLAN:
1086 err = -ENOPKG;
1087 if (!vlan_ioctl_hook)
1088 request_module("8021q");
1089
1090 mutex_lock(&vlan_ioctl_mutex);
1091 if (vlan_ioctl_hook)
1092 err = vlan_ioctl_hook(net, argp);
1093 mutex_unlock(&vlan_ioctl_mutex);
1094 break;
1095 case SIOCADDDLCI:
1096 case SIOCDELDLCI:
1097 err = -ENOPKG;
1098 if (!dlci_ioctl_hook)
1099 request_module("dlci");
1100
1101 mutex_lock(&dlci_ioctl_mutex);
1102 if (dlci_ioctl_hook)
1103 err = dlci_ioctl_hook(cmd, argp);
1104 mutex_unlock(&dlci_ioctl_mutex);
1105 break;
1106 default:
1107 err = sock_do_ioctl(net, sock, cmd, arg);
1108 break;
1109 }
1110 return err;
1111 }
1112
1113 int sock_create_lite(int family, int type, int protocol, struct socket **res)
1114 {
1115 int err;
1116 struct socket *sock = NULL;
1117
1118 err = security_socket_create(family, type, protocol, 1);
1119 if (err)
1120 goto out;
1121
1122 sock = sock_alloc();
1123 if (!sock) {
1124 err = -ENOMEM;
1125 goto out;
1126 }
1127
1128 sock->type = type;
1129 err = security_socket_post_create(sock, family, type, protocol, 1);
1130 if (err)
1131 goto out_release;
1132
1133 out:
1134 *res = sock;
1135 return err;
1136 out_release:
1137 sock_release(sock);
1138 sock = NULL;
1139 goto out;
1140 }
1141 EXPORT_SYMBOL(sock_create_lite);
1142
1143 /* No kernel lock held - perfect */
1144 static unsigned int sock_poll(struct file *file, poll_table *wait)
1145 {
1146 struct socket *sock;
1147
1148 /*
1149 * We can't return errors to poll, so it's either yes or no.
1150 */
1151 sock = file->private_data;
1152 return sock->ops->poll(file, sock, wait);
1153 }
1154
1155 static int sock_mmap(struct file *file, struct vm_area_struct *vma)
1156 {
1157 struct socket *sock = file->private_data;
1158
1159 return sock->ops->mmap(file, sock, vma);
1160 }
1161
1162 static int sock_close(struct inode *inode, struct file *filp)
1163 {
1164 sock_release(SOCKET_I(inode));
1165 return 0;
1166 }
1167
1168 /*
1169 * Update the socket async list
1170 *
1171 * Fasync_list locking strategy.
1172 *
1173 * 1. fasync_list is modified only under process context socket lock
1174 * i.e. under semaphore.
1175 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1176 * or under socket lock
1177 */
1178
1179 static int sock_fasync(int fd, struct file *filp, int on)
1180 {
1181 struct socket *sock = filp->private_data;
1182 struct sock *sk = sock->sk;
1183 struct socket_wq *wq;
1184
1185 if (sk == NULL)
1186 return -EINVAL;
1187
1188 lock_sock(sk);
1189 wq = rcu_dereference_protected(sock->wq, sock_owned_by_user(sk));
1190 fasync_helper(fd, filp, on, &wq->fasync_list);
1191
1192 if (!wq->fasync_list)
1193 sock_reset_flag(sk, SOCK_FASYNC);
1194 else
1195 sock_set_flag(sk, SOCK_FASYNC);
1196
1197 release_sock(sk);
1198 return 0;
1199 }
1200
1201 /* This function may be called only under socket lock or callback_lock or rcu_lock */
1202
1203 int sock_wake_async(struct socket *sock, int how, int band)
1204 {
1205 struct socket_wq *wq;
1206
1207 if (!sock)
1208 return -1;
1209 rcu_read_lock();
1210 wq = rcu_dereference(sock->wq);
1211 if (!wq || !wq->fasync_list) {
1212 rcu_read_unlock();
1213 return -1;
1214 }
1215 switch (how) {
1216 case SOCK_WAKE_WAITD:
1217 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1218 break;
1219 goto call_kill;
1220 case SOCK_WAKE_SPACE:
1221 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1222 break;
1223 /* fall through */
1224 case SOCK_WAKE_IO:
1225 call_kill:
1226 kill_fasync(&wq->fasync_list, SIGIO, band);
1227 break;
1228 case SOCK_WAKE_URG:
1229 kill_fasync(&wq->fasync_list, SIGURG, band);
1230 }
1231 rcu_read_unlock();
1232 return 0;
1233 }
1234 EXPORT_SYMBOL(sock_wake_async);
1235
1236 int __sock_create(struct net *net, int family, int type, int protocol,
1237 struct socket **res, int kern)
1238 {
1239 int err;
1240 struct socket *sock;
1241 const struct net_proto_family *pf;
1242
1243 /*
1244 * Check protocol is in range
1245 */
1246 if (family < 0 || family >= NPROTO)
1247 return -EAFNOSUPPORT;
1248 if (type < 0 || type >= SOCK_MAX)
1249 return -EINVAL;
1250
1251 /* Compatibility.
1252
1253 This uglymoron is moved from INET layer to here to avoid
1254 deadlock in module load.
1255 */
1256 if (family == PF_INET && type == SOCK_PACKET) {
1257 static int warned;
1258 if (!warned) {
1259 warned = 1;
1260 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1261 current->comm);
1262 }
1263 family = PF_PACKET;
1264 }
1265
1266 err = security_socket_create(family, type, protocol, kern);
1267 if (err)
1268 return err;
1269
1270 /*
1271 * Allocate the socket and allow the family to set things up. if
1272 * the protocol is 0, the family is instructed to select an appropriate
1273 * default.
1274 */
1275 sock = sock_alloc();
1276 if (!sock) {
1277 net_warn_ratelimited("socket: no more sockets\n");
1278 return -ENFILE; /* Not exactly a match, but its the
1279 closest posix thing */
1280 }
1281
1282 sock->type = type;
1283
1284 #ifdef CONFIG_MODULES
1285 /* Attempt to load a protocol module if the find failed.
1286 *
1287 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1288 * requested real, full-featured networking support upon configuration.
1289 * Otherwise module support will break!
1290 */
1291 if (rcu_access_pointer(net_families[family]) == NULL)
1292 request_module("net-pf-%d", family);
1293 #endif
1294
1295 rcu_read_lock();
1296 pf = rcu_dereference(net_families[family]);
1297 err = -EAFNOSUPPORT;
1298 if (!pf)
1299 goto out_release;
1300
1301 /*
1302 * We will call the ->create function, that possibly is in a loadable
1303 * module, so we have to bump that loadable module refcnt first.
1304 */
1305 if (!try_module_get(pf->owner))
1306 goto out_release;
1307
1308 /* Now protected by module ref count */
1309 rcu_read_unlock();
1310
1311 err = pf->create(net, sock, protocol, kern);
1312 if (err < 0)
1313 goto out_module_put;
1314
1315 /*
1316 * Now to bump the refcnt of the [loadable] module that owns this
1317 * socket at sock_release time we decrement its refcnt.
1318 */
1319 if (!try_module_get(sock->ops->owner))
1320 goto out_module_busy;
1321
1322 /*
1323 * Now that we're done with the ->create function, the [loadable]
1324 * module can have its refcnt decremented
1325 */
1326 module_put(pf->owner);
1327 err = security_socket_post_create(sock, family, type, protocol, kern);
1328 if (err)
1329 goto out_sock_release;
1330 *res = sock;
1331
1332 return 0;
1333
1334 out_module_busy:
1335 err = -EAFNOSUPPORT;
1336 out_module_put:
1337 sock->ops = NULL;
1338 module_put(pf->owner);
1339 out_sock_release:
1340 sock_release(sock);
1341 return err;
1342
1343 out_release:
1344 rcu_read_unlock();
1345 goto out_sock_release;
1346 }
1347 EXPORT_SYMBOL(__sock_create);
1348
1349 int sock_create(int family, int type, int protocol, struct socket **res)
1350 {
1351 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
1352 }
1353 EXPORT_SYMBOL(sock_create);
1354
1355 int sock_create_kern(int family, int type, int protocol, struct socket **res)
1356 {
1357 return __sock_create(&init_net, family, type, protocol, res, 1);
1358 }
1359 EXPORT_SYMBOL(sock_create_kern);
1360
1361 SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
1362 {
1363 int retval;
1364 struct socket *sock;
1365 int flags;
1366
1367 /* Check the SOCK_* constants for consistency. */
1368 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1369 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1370 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1371 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1372
1373 flags = type & ~SOCK_TYPE_MASK;
1374 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1375 return -EINVAL;
1376 type &= SOCK_TYPE_MASK;
1377
1378 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1379 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1380
1381 retval = sock_create(family, type, protocol, &sock);
1382 if (retval < 0)
1383 goto out;
1384
1385 retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
1386 if (retval < 0)
1387 goto out_release;
1388
1389 out:
1390 /* It may be already another descriptor 8) Not kernel problem. */
1391 return retval;
1392
1393 out_release:
1394 sock_release(sock);
1395 return retval;
1396 }
1397
1398 /*
1399 * Create a pair of connected sockets.
1400 */
1401
1402 SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1403 int __user *, usockvec)
1404 {
1405 struct socket *sock1, *sock2;
1406 int fd1, fd2, err;
1407 struct file *newfile1, *newfile2;
1408 int flags;
1409
1410 flags = type & ~SOCK_TYPE_MASK;
1411 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1412 return -EINVAL;
1413 type &= SOCK_TYPE_MASK;
1414
1415 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1416 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1417
1418 /*
1419 * Obtain the first socket and check if the underlying protocol
1420 * supports the socketpair call.
1421 */
1422
1423 err = sock_create(family, type, protocol, &sock1);
1424 if (err < 0)
1425 goto out;
1426
1427 err = sock_create(family, type, protocol, &sock2);
1428 if (err < 0)
1429 goto out_release_1;
1430
1431 err = sock1->ops->socketpair(sock1, sock2);
1432 if (err < 0)
1433 goto out_release_both;
1434
1435 fd1 = get_unused_fd_flags(flags);
1436 if (unlikely(fd1 < 0)) {
1437 err = fd1;
1438 goto out_release_both;
1439 }
1440 fd2 = get_unused_fd_flags(flags);
1441 if (unlikely(fd2 < 0)) {
1442 err = fd2;
1443 put_unused_fd(fd1);
1444 goto out_release_both;
1445 }
1446
1447 newfile1 = sock_alloc_file(sock1, flags, NULL);
1448 if (unlikely(IS_ERR(newfile1))) {
1449 err = PTR_ERR(newfile1);
1450 put_unused_fd(fd1);
1451 put_unused_fd(fd2);
1452 goto out_release_both;
1453 }
1454
1455 newfile2 = sock_alloc_file(sock2, flags, NULL);
1456 if (IS_ERR(newfile2)) {
1457 err = PTR_ERR(newfile2);
1458 fput(newfile1);
1459 put_unused_fd(fd1);
1460 put_unused_fd(fd2);
1461 sock_release(sock2);
1462 goto out;
1463 }
1464
1465 audit_fd_pair(fd1, fd2);
1466 fd_install(fd1, newfile1);
1467 fd_install(fd2, newfile2);
1468 /* fd1 and fd2 may be already another descriptors.
1469 * Not kernel problem.
1470 */
1471
1472 err = put_user(fd1, &usockvec[0]);
1473 if (!err)
1474 err = put_user(fd2, &usockvec[1]);
1475 if (!err)
1476 return 0;
1477
1478 sys_close(fd2);
1479 sys_close(fd1);
1480 return err;
1481
1482 out_release_both:
1483 sock_release(sock2);
1484 out_release_1:
1485 sock_release(sock1);
1486 out:
1487 return err;
1488 }
1489
1490 /*
1491 * Bind a name to a socket. Nothing much to do here since it's
1492 * the protocol's responsibility to handle the local address.
1493 *
1494 * We move the socket address to kernel space before we call
1495 * the protocol layer (having also checked the address is ok).
1496 */
1497
1498 SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
1499 {
1500 struct socket *sock;
1501 struct sockaddr_storage address;
1502 int err, fput_needed;
1503
1504 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1505 if (sock) {
1506 err = move_addr_to_kernel(umyaddr, addrlen, &address);
1507 if (err >= 0) {
1508 err = security_socket_bind(sock,
1509 (struct sockaddr *)&address,
1510 addrlen);
1511 if (!err)
1512 err = sock->ops->bind(sock,
1513 (struct sockaddr *)
1514 &address, addrlen);
1515 }
1516 fput_light(sock->file, fput_needed);
1517 }
1518 return err;
1519 }
1520
1521 /*
1522 * Perform a listen. Basically, we allow the protocol to do anything
1523 * necessary for a listen, and if that works, we mark the socket as
1524 * ready for listening.
1525 */
1526
1527 SYSCALL_DEFINE2(listen, int, fd, int, backlog)
1528 {
1529 struct socket *sock;
1530 int err, fput_needed;
1531 int somaxconn;
1532
1533 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1534 if (sock) {
1535 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
1536 if ((unsigned int)backlog > somaxconn)
1537 backlog = somaxconn;
1538
1539 err = security_socket_listen(sock, backlog);
1540 if (!err)
1541 err = sock->ops->listen(sock, backlog);
1542
1543 fput_light(sock->file, fput_needed);
1544 }
1545 return err;
1546 }
1547
1548 /*
1549 * For accept, we attempt to create a new socket, set up the link
1550 * with the client, wake up the client, then return the new
1551 * connected fd. We collect the address of the connector in kernel
1552 * space and move it to user at the very end. This is unclean because
1553 * we open the socket then return an error.
1554 *
1555 * 1003.1g adds the ability to recvmsg() to query connection pending
1556 * status to recvmsg. We need to add that support in a way thats
1557 * clean when we restucture accept also.
1558 */
1559
1560 SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1561 int __user *, upeer_addrlen, int, flags)
1562 {
1563 struct socket *sock, *newsock;
1564 struct file *newfile;
1565 int err, len, newfd, fput_needed;
1566 struct sockaddr_storage address;
1567
1568 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1569 return -EINVAL;
1570
1571 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1572 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1573
1574 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1575 if (!sock)
1576 goto out;
1577
1578 err = -ENFILE;
1579 newsock = sock_alloc();
1580 if (!newsock)
1581 goto out_put;
1582
1583 newsock->type = sock->type;
1584 newsock->ops = sock->ops;
1585
1586 /*
1587 * We don't need try_module_get here, as the listening socket (sock)
1588 * has the protocol module (sock->ops->owner) held.
1589 */
1590 __module_get(newsock->ops->owner);
1591
1592 newfd = get_unused_fd_flags(flags);
1593 if (unlikely(newfd < 0)) {
1594 err = newfd;
1595 sock_release(newsock);
1596 goto out_put;
1597 }
1598 newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
1599 if (unlikely(IS_ERR(newfile))) {
1600 err = PTR_ERR(newfile);
1601 put_unused_fd(newfd);
1602 sock_release(newsock);
1603 goto out_put;
1604 }
1605
1606 err = security_socket_accept(sock, newsock);
1607 if (err)
1608 goto out_fd;
1609
1610 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1611 if (err < 0)
1612 goto out_fd;
1613
1614 if (upeer_sockaddr) {
1615 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
1616 &len, 2) < 0) {
1617 err = -ECONNABORTED;
1618 goto out_fd;
1619 }
1620 err = move_addr_to_user(&address,
1621 len, upeer_sockaddr, upeer_addrlen);
1622 if (err < 0)
1623 goto out_fd;
1624 }
1625
1626 /* File flags are not inherited via accept() unlike another OSes. */
1627
1628 fd_install(newfd, newfile);
1629 err = newfd;
1630
1631 out_put:
1632 fput_light(sock->file, fput_needed);
1633 out:
1634 return err;
1635 out_fd:
1636 fput(newfile);
1637 put_unused_fd(newfd);
1638 goto out_put;
1639 }
1640
1641 SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1642 int __user *, upeer_addrlen)
1643 {
1644 return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
1645 }
1646
1647 /*
1648 * Attempt to connect to a socket with the server address. The address
1649 * is in user space so we verify it is OK and move it to kernel space.
1650 *
1651 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1652 * break bindings
1653 *
1654 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1655 * other SEQPACKET protocols that take time to connect() as it doesn't
1656 * include the -EINPROGRESS status for such sockets.
1657 */
1658
1659 SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1660 int, addrlen)
1661 {
1662 struct socket *sock;
1663 struct sockaddr_storage address;
1664 int err, fput_needed;
1665
1666 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1667 if (!sock)
1668 goto out;
1669 err = move_addr_to_kernel(uservaddr, addrlen, &address);
1670 if (err < 0)
1671 goto out_put;
1672
1673 err =
1674 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
1675 if (err)
1676 goto out_put;
1677
1678 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
1679 sock->file->f_flags);
1680 out_put:
1681 fput_light(sock->file, fput_needed);
1682 out:
1683 return err;
1684 }
1685
1686 /*
1687 * Get the local address ('name') of a socket object. Move the obtained
1688 * name to user space.
1689 */
1690
1691 SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1692 int __user *, usockaddr_len)
1693 {
1694 struct socket *sock;
1695 struct sockaddr_storage address;
1696 int len, err, fput_needed;
1697
1698 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1699 if (!sock)
1700 goto out;
1701
1702 err = security_socket_getsockname(sock);
1703 if (err)
1704 goto out_put;
1705
1706 err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
1707 if (err)
1708 goto out_put;
1709 err = move_addr_to_user(&address, len, usockaddr, usockaddr_len);
1710
1711 out_put:
1712 fput_light(sock->file, fput_needed);
1713 out:
1714 return err;
1715 }
1716
1717 /*
1718 * Get the remote address ('name') of a socket object. Move the obtained
1719 * name to user space.
1720 */
1721
1722 SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1723 int __user *, usockaddr_len)
1724 {
1725 struct socket *sock;
1726 struct sockaddr_storage address;
1727 int len, err, fput_needed;
1728
1729 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1730 if (sock != NULL) {
1731 err = security_socket_getpeername(sock);
1732 if (err) {
1733 fput_light(sock->file, fput_needed);
1734 return err;
1735 }
1736
1737 err =
1738 sock->ops->getname(sock, (struct sockaddr *)&address, &len,
1739 1);
1740 if (!err)
1741 err = move_addr_to_user(&address, len, usockaddr,
1742 usockaddr_len);
1743 fput_light(sock->file, fput_needed);
1744 }
1745 return err;
1746 }
1747
1748 /*
1749 * Send a datagram to a given address. We move the address into kernel
1750 * space and check the user space data area is readable before invoking
1751 * the protocol.
1752 */
1753
1754 SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1755 unsigned int, flags, struct sockaddr __user *, addr,
1756 int, addr_len)
1757 {
1758 struct socket *sock;
1759 struct sockaddr_storage address;
1760 int err;
1761 struct msghdr msg;
1762 struct iovec iov;
1763 int fput_needed;
1764
1765 if (len > INT_MAX)
1766 len = INT_MAX;
1767 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1768 if (!sock)
1769 goto out;
1770
1771 iov.iov_base = buff;
1772 iov.iov_len = len;
1773 msg.msg_name = NULL;
1774 msg.msg_iov = &iov;
1775 msg.msg_iovlen = 1;
1776 msg.msg_control = NULL;
1777 msg.msg_controllen = 0;
1778 msg.msg_namelen = 0;
1779 if (addr) {
1780 err = move_addr_to_kernel(addr, addr_len, &address);
1781 if (err < 0)
1782 goto out_put;
1783 msg.msg_name = (struct sockaddr *)&address;
1784 msg.msg_namelen = addr_len;
1785 }
1786 if (sock->file->f_flags & O_NONBLOCK)
1787 flags |= MSG_DONTWAIT;
1788 msg.msg_flags = flags;
1789 err = sock_sendmsg(sock, &msg, len);
1790
1791 out_put:
1792 fput_light(sock->file, fput_needed);
1793 out:
1794 return err;
1795 }
1796
1797 /*
1798 * Send a datagram down a socket.
1799 */
1800
1801 SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
1802 unsigned int, flags)
1803 {
1804 return sys_sendto(fd, buff, len, flags, NULL, 0);
1805 }
1806
1807 /*
1808 * Receive a frame from the socket and optionally record the address of the
1809 * sender. We verify the buffers are writable and if needed move the
1810 * sender address from kernel to user space.
1811 */
1812
1813 SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
1814 unsigned int, flags, struct sockaddr __user *, addr,
1815 int __user *, addr_len)
1816 {
1817 struct socket *sock;
1818 struct iovec iov;
1819 struct msghdr msg;
1820 struct sockaddr_storage address;
1821 int err, err2;
1822 int fput_needed;
1823
1824 if (size > INT_MAX)
1825 size = INT_MAX;
1826 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1827 if (!sock)
1828 goto out;
1829
1830 msg.msg_control = NULL;
1831 msg.msg_controllen = 0;
1832 msg.msg_iovlen = 1;
1833 msg.msg_iov = &iov;
1834 iov.iov_len = size;
1835 iov.iov_base = ubuf;
1836 /* Save some cycles and don't copy the address if not needed */
1837 msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
1838 /* We assume all kernel code knows the size of sockaddr_storage */
1839 msg.msg_namelen = 0;
1840 if (sock->file->f_flags & O_NONBLOCK)
1841 flags |= MSG_DONTWAIT;
1842 err = sock_recvmsg(sock, &msg, size, flags);
1843
1844 if (err >= 0 && addr != NULL) {
1845 err2 = move_addr_to_user(&address,
1846 msg.msg_namelen, addr, addr_len);
1847 if (err2 < 0)
1848 err = err2;
1849 }
1850
1851 fput_light(sock->file, fput_needed);
1852 out:
1853 return err;
1854 }
1855
1856 /*
1857 * Receive a datagram from a socket.
1858 */
1859
1860 asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
1861 unsigned int flags)
1862 {
1863 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1864 }
1865
1866 /*
1867 * Set a socket option. Because we don't know the option lengths we have
1868 * to pass the user mode parameter for the protocols to sort out.
1869 */
1870
1871 SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1872 char __user *, optval, int, optlen)
1873 {
1874 int err, fput_needed;
1875 struct socket *sock;
1876
1877 if (optlen < 0)
1878 return -EINVAL;
1879
1880 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1881 if (sock != NULL) {
1882 err = security_socket_setsockopt(sock, level, optname);
1883 if (err)
1884 goto out_put;
1885
1886 if (level == SOL_SOCKET)
1887 err =
1888 sock_setsockopt(sock, level, optname, optval,
1889 optlen);
1890 else
1891 err =
1892 sock->ops->setsockopt(sock, level, optname, optval,
1893 optlen);
1894 out_put:
1895 fput_light(sock->file, fput_needed);
1896 }
1897 return err;
1898 }
1899
1900 /*
1901 * Get a socket option. Because we don't know the option lengths we have
1902 * to pass a user mode parameter for the protocols to sort out.
1903 */
1904
1905 SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1906 char __user *, optval, int __user *, optlen)
1907 {
1908 int err, fput_needed;
1909 struct socket *sock;
1910
1911 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1912 if (sock != NULL) {
1913 err = security_socket_getsockopt(sock, level, optname);
1914 if (err)
1915 goto out_put;
1916
1917 if (level == SOL_SOCKET)
1918 err =
1919 sock_getsockopt(sock, level, optname, optval,
1920 optlen);
1921 else
1922 err =
1923 sock->ops->getsockopt(sock, level, optname, optval,
1924 optlen);
1925 out_put:
1926 fput_light(sock->file, fput_needed);
1927 }
1928 return err;
1929 }
1930
1931 /*
1932 * Shutdown a socket.
1933 */
1934
1935 SYSCALL_DEFINE2(shutdown, int, fd, int, how)
1936 {
1937 int err, fput_needed;
1938 struct socket *sock;
1939
1940 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1941 if (sock != NULL) {
1942 err = security_socket_shutdown(sock, how);
1943 if (!err)
1944 err = sock->ops->shutdown(sock, how);
1945 fput_light(sock->file, fput_needed);
1946 }
1947 return err;
1948 }
1949
1950 /* A couple of helpful macros for getting the address of the 32/64 bit
1951 * fields which are the same type (int / unsigned) on our platforms.
1952 */
1953 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1954 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1955 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1956
1957 struct used_address {
1958 struct sockaddr_storage name;
1959 unsigned int name_len;
1960 };
1961
1962 static int copy_msghdr_from_user(struct msghdr *kmsg,
1963 struct msghdr __user *umsg)
1964 {
1965 if (copy_from_user(kmsg, umsg, sizeof(struct msghdr)))
1966 return -EFAULT;
1967
1968 if (kmsg->msg_namelen < 0)
1969 return -EINVAL;
1970
1971 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
1972 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
1973 return 0;
1974 }
1975
1976 static int ___sys_sendmsg(struct socket *sock, struct msghdr __user *msg,
1977 struct msghdr *msg_sys, unsigned int flags,
1978 struct used_address *used_address)
1979 {
1980 struct compat_msghdr __user *msg_compat =
1981 (struct compat_msghdr __user *)msg;
1982 struct sockaddr_storage address;
1983 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1984 unsigned char ctl[sizeof(struct cmsghdr) + 20]
1985 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1986 /* 20 is size of ipv6_pktinfo */
1987 unsigned char *ctl_buf = ctl;
1988 int err, ctl_len, total_len;
1989
1990 err = -EFAULT;
1991 if (MSG_CMSG_COMPAT & flags)
1992 err = get_compat_msghdr(msg_sys, msg_compat);
1993 else
1994 err = copy_msghdr_from_user(msg_sys, msg);
1995 if (err)
1996 return err;
1997
1998 if (msg_sys->msg_iovlen > UIO_FASTIOV) {
1999 err = -EMSGSIZE;
2000 if (msg_sys->msg_iovlen > UIO_MAXIOV)
2001 goto out;
2002 err = -ENOMEM;
2003 iov = kmalloc(msg_sys->msg_iovlen * sizeof(struct iovec),
2004 GFP_KERNEL);
2005 if (!iov)
2006 goto out;
2007 }
2008
2009 /* This will also move the address data into kernel space */
2010 if (MSG_CMSG_COMPAT & flags) {
2011 err = verify_compat_iovec(msg_sys, iov, &address, VERIFY_READ);
2012 } else
2013 err = verify_iovec(msg_sys, iov, &address, VERIFY_READ);
2014 if (err < 0)
2015 goto out_freeiov;
2016 total_len = err;
2017
2018 err = -ENOBUFS;
2019
2020 if (msg_sys->msg_controllen > INT_MAX)
2021 goto out_freeiov;
2022 ctl_len = msg_sys->msg_controllen;
2023 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
2024 err =
2025 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
2026 sizeof(ctl));
2027 if (err)
2028 goto out_freeiov;
2029 ctl_buf = msg_sys->msg_control;
2030 ctl_len = msg_sys->msg_controllen;
2031 } else if (ctl_len) {
2032 if (ctl_len > sizeof(ctl)) {
2033 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
2034 if (ctl_buf == NULL)
2035 goto out_freeiov;
2036 }
2037 err = -EFAULT;
2038 /*
2039 * Careful! Before this, msg_sys->msg_control contains a user pointer.
2040 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
2041 * checking falls down on this.
2042 */
2043 if (copy_from_user(ctl_buf,
2044 (void __user __force *)msg_sys->msg_control,
2045 ctl_len))
2046 goto out_freectl;
2047 msg_sys->msg_control = ctl_buf;
2048 }
2049 msg_sys->msg_flags = flags;
2050
2051 if (sock->file->f_flags & O_NONBLOCK)
2052 msg_sys->msg_flags |= MSG_DONTWAIT;
2053 /*
2054 * If this is sendmmsg() and current destination address is same as
2055 * previously succeeded address, omit asking LSM's decision.
2056 * used_address->name_len is initialized to UINT_MAX so that the first
2057 * destination address never matches.
2058 */
2059 if (used_address && msg_sys->msg_name &&
2060 used_address->name_len == msg_sys->msg_namelen &&
2061 !memcmp(&used_address->name, msg_sys->msg_name,
2062 used_address->name_len)) {
2063 err = sock_sendmsg_nosec(sock, msg_sys, total_len);
2064 goto out_freectl;
2065 }
2066 err = sock_sendmsg(sock, msg_sys, total_len);
2067 /*
2068 * If this is sendmmsg() and sending to current destination address was
2069 * successful, remember it.
2070 */
2071 if (used_address && err >= 0) {
2072 used_address->name_len = msg_sys->msg_namelen;
2073 if (msg_sys->msg_name)
2074 memcpy(&used_address->name, msg_sys->msg_name,
2075 used_address->name_len);
2076 }
2077
2078 out_freectl:
2079 if (ctl_buf != ctl)
2080 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
2081 out_freeiov:
2082 if (iov != iovstack)
2083 kfree(iov);
2084 out:
2085 return err;
2086 }
2087
2088 /*
2089 * BSD sendmsg interface
2090 */
2091
2092 long __sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
2093 {
2094 int fput_needed, err;
2095 struct msghdr msg_sys;
2096 struct socket *sock;
2097
2098 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2099 if (!sock)
2100 goto out;
2101
2102 err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL);
2103
2104 fput_light(sock->file, fput_needed);
2105 out:
2106 return err;
2107 }
2108
2109 SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned int, flags)
2110 {
2111 if (flags & MSG_CMSG_COMPAT)
2112 return -EINVAL;
2113 return __sys_sendmsg(fd, msg, flags);
2114 }
2115
2116 /*
2117 * Linux sendmmsg interface
2118 */
2119
2120 int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2121 unsigned int flags)
2122 {
2123 int fput_needed, err, datagrams;
2124 struct socket *sock;
2125 struct mmsghdr __user *entry;
2126 struct compat_mmsghdr __user *compat_entry;
2127 struct msghdr msg_sys;
2128 struct used_address used_address;
2129
2130 if (vlen > UIO_MAXIOV)
2131 vlen = UIO_MAXIOV;
2132
2133 datagrams = 0;
2134
2135 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2136 if (!sock)
2137 return err;
2138
2139 used_address.name_len = UINT_MAX;
2140 entry = mmsg;
2141 compat_entry = (struct compat_mmsghdr __user *)mmsg;
2142 err = 0;
2143
2144 while (datagrams < vlen) {
2145 if (MSG_CMSG_COMPAT & flags) {
2146 err = ___sys_sendmsg(sock, (struct msghdr __user *)compat_entry,
2147 &msg_sys, flags, &used_address);
2148 if (err < 0)
2149 break;
2150 err = __put_user(err, &compat_entry->msg_len);
2151 ++compat_entry;
2152 } else {
2153 err = ___sys_sendmsg(sock,
2154 (struct msghdr __user *)entry,
2155 &msg_sys, flags, &used_address);
2156 if (err < 0)
2157 break;
2158 err = put_user(err, &entry->msg_len);
2159 ++entry;
2160 }
2161
2162 if (err)
2163 break;
2164 ++datagrams;
2165 }
2166
2167 fput_light(sock->file, fput_needed);
2168
2169 /* We only return an error if no datagrams were able to be sent */
2170 if (datagrams != 0)
2171 return datagrams;
2172
2173 return err;
2174 }
2175
2176 SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2177 unsigned int, vlen, unsigned int, flags)
2178 {
2179 if (flags & MSG_CMSG_COMPAT)
2180 return -EINVAL;
2181 return __sys_sendmmsg(fd, mmsg, vlen, flags);
2182 }
2183
2184 static int ___sys_recvmsg(struct socket *sock, struct msghdr __user *msg,
2185 struct msghdr *msg_sys, unsigned int flags, int nosec)
2186 {
2187 struct compat_msghdr __user *msg_compat =
2188 (struct compat_msghdr __user *)msg;
2189 struct iovec iovstack[UIO_FASTIOV];
2190 struct iovec *iov = iovstack;
2191 unsigned long cmsg_ptr;
2192 int err, total_len, len;
2193
2194 /* kernel mode address */
2195 struct sockaddr_storage addr;
2196
2197 /* user mode address pointers */
2198 struct sockaddr __user *uaddr;
2199 int __user *uaddr_len;
2200
2201 if (MSG_CMSG_COMPAT & flags)
2202 err = get_compat_msghdr(msg_sys, msg_compat);
2203 else
2204 err = copy_msghdr_from_user(msg_sys, msg);
2205 if (err)
2206 return err;
2207
2208 if (msg_sys->msg_iovlen > UIO_FASTIOV) {
2209 err = -EMSGSIZE;
2210 if (msg_sys->msg_iovlen > UIO_MAXIOV)
2211 goto out;
2212 err = -ENOMEM;
2213 iov = kmalloc(msg_sys->msg_iovlen * sizeof(struct iovec),
2214 GFP_KERNEL);
2215 if (!iov)
2216 goto out;
2217 }
2218
2219 /* Save the user-mode address (verify_iovec will change the
2220 * kernel msghdr to use the kernel address space)
2221 */
2222 uaddr = (__force void __user *)msg_sys->msg_name;
2223 uaddr_len = COMPAT_NAMELEN(msg);
2224 if (MSG_CMSG_COMPAT & flags)
2225 err = verify_compat_iovec(msg_sys, iov, &addr, VERIFY_WRITE);
2226 else
2227 err = verify_iovec(msg_sys, iov, &addr, VERIFY_WRITE);
2228 if (err < 0)
2229 goto out_freeiov;
2230 total_len = err;
2231
2232 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2233 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
2234
2235 /* We assume all kernel code knows the size of sockaddr_storage */
2236 msg_sys->msg_namelen = 0;
2237
2238 if (sock->file->f_flags & O_NONBLOCK)
2239 flags |= MSG_DONTWAIT;
2240 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
2241 total_len, flags);
2242 if (err < 0)
2243 goto out_freeiov;
2244 len = err;
2245
2246 if (uaddr != NULL) {
2247 err = move_addr_to_user(&addr,
2248 msg_sys->msg_namelen, uaddr,
2249 uaddr_len);
2250 if (err < 0)
2251 goto out_freeiov;
2252 }
2253 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
2254 COMPAT_FLAGS(msg));
2255 if (err)
2256 goto out_freeiov;
2257 if (MSG_CMSG_COMPAT & flags)
2258 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
2259 &msg_compat->msg_controllen);
2260 else
2261 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
2262 &msg->msg_controllen);
2263 if (err)
2264 goto out_freeiov;
2265 err = len;
2266
2267 out_freeiov:
2268 if (iov != iovstack)
2269 kfree(iov);
2270 out:
2271 return err;
2272 }
2273
2274 /*
2275 * BSD recvmsg interface
2276 */
2277
2278 long __sys_recvmsg(int fd, struct msghdr __user *msg, unsigned flags)
2279 {
2280 int fput_needed, err;
2281 struct msghdr msg_sys;
2282 struct socket *sock;
2283
2284 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2285 if (!sock)
2286 goto out;
2287
2288 err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
2289
2290 fput_light(sock->file, fput_needed);
2291 out:
2292 return err;
2293 }
2294
2295 SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
2296 unsigned int, flags)
2297 {
2298 if (flags & MSG_CMSG_COMPAT)
2299 return -EINVAL;
2300 return __sys_recvmsg(fd, msg, flags);
2301 }
2302
2303 /*
2304 * Linux recvmmsg interface
2305 */
2306
2307 int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2308 unsigned int flags, struct timespec *timeout)
2309 {
2310 int fput_needed, err, datagrams;
2311 struct socket *sock;
2312 struct mmsghdr __user *entry;
2313 struct compat_mmsghdr __user *compat_entry;
2314 struct msghdr msg_sys;
2315 struct timespec end_time;
2316
2317 if (timeout &&
2318 poll_select_set_timeout(&end_time, timeout->tv_sec,
2319 timeout->tv_nsec))
2320 return -EINVAL;
2321
2322 datagrams = 0;
2323
2324 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2325 if (!sock)
2326 return err;
2327
2328 err = sock_error(sock->sk);
2329 if (err) {
2330 datagrams = err;
2331 goto out_put;
2332 }
2333
2334 entry = mmsg;
2335 compat_entry = (struct compat_mmsghdr __user *)mmsg;
2336
2337 while (datagrams < vlen) {
2338 /*
2339 * No need to ask LSM for more than the first datagram.
2340 */
2341 if (MSG_CMSG_COMPAT & flags) {
2342 err = ___sys_recvmsg(sock, (struct msghdr __user *)compat_entry,
2343 &msg_sys, flags & ~MSG_WAITFORONE,
2344 datagrams);
2345 if (err < 0)
2346 break;
2347 err = __put_user(err, &compat_entry->msg_len);
2348 ++compat_entry;
2349 } else {
2350 err = ___sys_recvmsg(sock,
2351 (struct msghdr __user *)entry,
2352 &msg_sys, flags & ~MSG_WAITFORONE,
2353 datagrams);
2354 if (err < 0)
2355 break;
2356 err = put_user(err, &entry->msg_len);
2357 ++entry;
2358 }
2359
2360 if (err)
2361 break;
2362 ++datagrams;
2363
2364 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2365 if (flags & MSG_WAITFORONE)
2366 flags |= MSG_DONTWAIT;
2367
2368 if (timeout) {
2369 ktime_get_ts(timeout);
2370 *timeout = timespec_sub(end_time, *timeout);
2371 if (timeout->tv_sec < 0) {
2372 timeout->tv_sec = timeout->tv_nsec = 0;
2373 break;
2374 }
2375
2376 /* Timeout, return less than vlen datagrams */
2377 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2378 break;
2379 }
2380
2381 /* Out of band data, return right away */
2382 if (msg_sys.msg_flags & MSG_OOB)
2383 break;
2384 }
2385
2386 if (err == 0)
2387 goto out_put;
2388
2389 if (datagrams == 0) {
2390 datagrams = err;
2391 goto out_put;
2392 }
2393
2394 /*
2395 * We may return less entries than requested (vlen) if the
2396 * sock is non block and there aren't enough datagrams...
2397 */
2398 if (err != -EAGAIN) {
2399 /*
2400 * ... or if recvmsg returns an error after we
2401 * received some datagrams, where we record the
2402 * error to return on the next call or if the
2403 * app asks about it using getsockopt(SO_ERROR).
2404 */
2405 sock->sk->sk_err = -err;
2406 }
2407 out_put:
2408 fput_light(sock->file, fput_needed);
2409
2410 return datagrams;
2411 }
2412
2413 SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2414 unsigned int, vlen, unsigned int, flags,
2415 struct timespec __user *, timeout)
2416 {
2417 int datagrams;
2418 struct timespec timeout_sys;
2419
2420 if (flags & MSG_CMSG_COMPAT)
2421 return -EINVAL;
2422
2423 if (!timeout)
2424 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
2425
2426 if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
2427 return -EFAULT;
2428
2429 datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2430
2431 if (datagrams > 0 &&
2432 copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
2433 datagrams = -EFAULT;
2434
2435 return datagrams;
2436 }
2437
2438 #ifdef __ARCH_WANT_SYS_SOCKETCALL
2439 /* Argument list sizes for sys_socketcall */
2440 #define AL(x) ((x) * sizeof(unsigned long))
2441 static const unsigned char nargs[21] = {
2442 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2443 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2444 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
2445 AL(4), AL(5), AL(4)
2446 };
2447
2448 #undef AL
2449
2450 /*
2451 * System call vectors.
2452 *
2453 * Argument checking cleaned up. Saved 20% in size.
2454 * This function doesn't need to set the kernel lock because
2455 * it is set by the callees.
2456 */
2457
2458 SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
2459 {
2460 unsigned long a[AUDITSC_ARGS];
2461 unsigned long a0, a1;
2462 int err;
2463 unsigned int len;
2464
2465 if (call < 1 || call > SYS_SENDMMSG)
2466 return -EINVAL;
2467
2468 len = nargs[call];
2469 if (len > sizeof(a))
2470 return -EINVAL;
2471
2472 /* copy_from_user should be SMP safe. */
2473 if (copy_from_user(a, args, len))
2474 return -EFAULT;
2475
2476 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
2477 if (err)
2478 return err;
2479
2480 a0 = a[0];
2481 a1 = a[1];
2482
2483 switch (call) {
2484 case SYS_SOCKET:
2485 err = sys_socket(a0, a1, a[2]);
2486 break;
2487 case SYS_BIND:
2488 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2489 break;
2490 case SYS_CONNECT:
2491 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2492 break;
2493 case SYS_LISTEN:
2494 err = sys_listen(a0, a1);
2495 break;
2496 case SYS_ACCEPT:
2497 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2498 (int __user *)a[2], 0);
2499 break;
2500 case SYS_GETSOCKNAME:
2501 err =
2502 sys_getsockname(a0, (struct sockaddr __user *)a1,
2503 (int __user *)a[2]);
2504 break;
2505 case SYS_GETPEERNAME:
2506 err =
2507 sys_getpeername(a0, (struct sockaddr __user *)a1,
2508 (int __user *)a[2]);
2509 break;
2510 case SYS_SOCKETPAIR:
2511 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2512 break;
2513 case SYS_SEND:
2514 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2515 break;
2516 case SYS_SENDTO:
2517 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2518 (struct sockaddr __user *)a[4], a[5]);
2519 break;
2520 case SYS_RECV:
2521 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2522 break;
2523 case SYS_RECVFROM:
2524 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2525 (struct sockaddr __user *)a[4],
2526 (int __user *)a[5]);
2527 break;
2528 case SYS_SHUTDOWN:
2529 err = sys_shutdown(a0, a1);
2530 break;
2531 case SYS_SETSOCKOPT:
2532 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2533 break;
2534 case SYS_GETSOCKOPT:
2535 err =
2536 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2537 (int __user *)a[4]);
2538 break;
2539 case SYS_SENDMSG:
2540 err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
2541 break;
2542 case SYS_SENDMMSG:
2543 err = sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3]);
2544 break;
2545 case SYS_RECVMSG:
2546 err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
2547 break;
2548 case SYS_RECVMMSG:
2549 err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
2550 (struct timespec __user *)a[4]);
2551 break;
2552 case SYS_ACCEPT4:
2553 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2554 (int __user *)a[2], a[3]);
2555 break;
2556 default:
2557 err = -EINVAL;
2558 break;
2559 }
2560 return err;
2561 }
2562
2563 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
2564
2565 /**
2566 * sock_register - add a socket protocol handler
2567 * @ops: description of protocol
2568 *
2569 * This function is called by a protocol handler that wants to
2570 * advertise its address family, and have it linked into the
2571 * socket interface. The value ops->family coresponds to the
2572 * socket system call protocol family.
2573 */
2574 int sock_register(const struct net_proto_family *ops)
2575 {
2576 int err;
2577
2578 if (ops->family >= NPROTO) {
2579 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
2580 NPROTO);
2581 return -ENOBUFS;
2582 }
2583
2584 spin_lock(&net_family_lock);
2585 if (rcu_dereference_protected(net_families[ops->family],
2586 lockdep_is_held(&net_family_lock)))
2587 err = -EEXIST;
2588 else {
2589 rcu_assign_pointer(net_families[ops->family], ops);
2590 err = 0;
2591 }
2592 spin_unlock(&net_family_lock);
2593
2594 printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
2595 return err;
2596 }
2597 EXPORT_SYMBOL(sock_register);
2598
2599 /**
2600 * sock_unregister - remove a protocol handler
2601 * @family: protocol family to remove
2602 *
2603 * This function is called by a protocol handler that wants to
2604 * remove its address family, and have it unlinked from the
2605 * new socket creation.
2606 *
2607 * If protocol handler is a module, then it can use module reference
2608 * counts to protect against new references. If protocol handler is not
2609 * a module then it needs to provide its own protection in
2610 * the ops->create routine.
2611 */
2612 void sock_unregister(int family)
2613 {
2614 BUG_ON(family < 0 || family >= NPROTO);
2615
2616 spin_lock(&net_family_lock);
2617 RCU_INIT_POINTER(net_families[family], NULL);
2618 spin_unlock(&net_family_lock);
2619
2620 synchronize_rcu();
2621
2622 printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
2623 }
2624 EXPORT_SYMBOL(sock_unregister);
2625
2626 static int __init sock_init(void)
2627 {
2628 int err;
2629 /*
2630 * Initialize the network sysctl infrastructure.
2631 */
2632 err = net_sysctl_init();
2633 if (err)
2634 goto out;
2635
2636 /*
2637 * Initialize skbuff SLAB cache
2638 */
2639 skb_init();
2640
2641 /*
2642 * Initialize the protocols module.
2643 */
2644
2645 init_inodecache();
2646
2647 err = register_filesystem(&sock_fs_type);
2648 if (err)
2649 goto out_fs;
2650 sock_mnt = kern_mount(&sock_fs_type);
2651 if (IS_ERR(sock_mnt)) {
2652 err = PTR_ERR(sock_mnt);
2653 goto out_mount;
2654 }
2655
2656 /* The real protocol initialization is performed in later initcalls.
2657 */
2658
2659 #ifdef CONFIG_NETFILTER
2660 netfilter_init();
2661 #endif
2662
2663 #ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
2664 skb_timestamping_init();
2665 #endif
2666
2667 out:
2668 return err;
2669
2670 out_mount:
2671 unregister_filesystem(&sock_fs_type);
2672 out_fs:
2673 goto out;
2674 }
2675
2676 core_initcall(sock_init); /* early initcall */
2677
2678 #ifdef CONFIG_PROC_FS
2679 void socket_seq_show(struct seq_file *seq)
2680 {
2681 int cpu;
2682 int counter = 0;
2683
2684 for_each_possible_cpu(cpu)
2685 counter += per_cpu(sockets_in_use, cpu);
2686
2687 /* It can be negative, by the way. 8) */
2688 if (counter < 0)
2689 counter = 0;
2690
2691 seq_printf(seq, "sockets: used %d\n", counter);
2692 }
2693 #endif /* CONFIG_PROC_FS */
2694
2695 #ifdef CONFIG_COMPAT
2696 static int do_siocgstamp(struct net *net, struct socket *sock,
2697 unsigned int cmd, void __user *up)
2698 {
2699 mm_segment_t old_fs = get_fs();
2700 struct timeval ktv;
2701 int err;
2702
2703 set_fs(KERNEL_DS);
2704 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
2705 set_fs(old_fs);
2706 if (!err)
2707 err = compat_put_timeval(&ktv, up);
2708
2709 return err;
2710 }
2711
2712 static int do_siocgstampns(struct net *net, struct socket *sock,
2713 unsigned int cmd, void __user *up)
2714 {
2715 mm_segment_t old_fs = get_fs();
2716 struct timespec kts;
2717 int err;
2718
2719 set_fs(KERNEL_DS);
2720 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
2721 set_fs(old_fs);
2722 if (!err)
2723 err = compat_put_timespec(&kts, up);
2724
2725 return err;
2726 }
2727
2728 static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
2729 {
2730 struct ifreq __user *uifr;
2731 int err;
2732
2733 uifr = compat_alloc_user_space(sizeof(struct ifreq));
2734 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2735 return -EFAULT;
2736
2737 err = dev_ioctl(net, SIOCGIFNAME, uifr);
2738 if (err)
2739 return err;
2740
2741 if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
2742 return -EFAULT;
2743
2744 return 0;
2745 }
2746
2747 static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
2748 {
2749 struct compat_ifconf ifc32;
2750 struct ifconf ifc;
2751 struct ifconf __user *uifc;
2752 struct compat_ifreq __user *ifr32;
2753 struct ifreq __user *ifr;
2754 unsigned int i, j;
2755 int err;
2756
2757 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
2758 return -EFAULT;
2759
2760 memset(&ifc, 0, sizeof(ifc));
2761 if (ifc32.ifcbuf == 0) {
2762 ifc32.ifc_len = 0;
2763 ifc.ifc_len = 0;
2764 ifc.ifc_req = NULL;
2765 uifc = compat_alloc_user_space(sizeof(struct ifconf));
2766 } else {
2767 size_t len = ((ifc32.ifc_len / sizeof(struct compat_ifreq)) + 1) *
2768 sizeof(struct ifreq);
2769 uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
2770 ifc.ifc_len = len;
2771 ifr = ifc.ifc_req = (void __user *)(uifc + 1);
2772 ifr32 = compat_ptr(ifc32.ifcbuf);
2773 for (i = 0; i < ifc32.ifc_len; i += sizeof(struct compat_ifreq)) {
2774 if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
2775 return -EFAULT;
2776 ifr++;
2777 ifr32++;
2778 }
2779 }
2780 if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
2781 return -EFAULT;
2782
2783 err = dev_ioctl(net, SIOCGIFCONF, uifc);
2784 if (err)
2785 return err;
2786
2787 if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
2788 return -EFAULT;
2789
2790 ifr = ifc.ifc_req;
2791 ifr32 = compat_ptr(ifc32.ifcbuf);
2792 for (i = 0, j = 0;
2793 i + sizeof(struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
2794 i += sizeof(struct compat_ifreq), j += sizeof(struct ifreq)) {
2795 if (copy_in_user(ifr32, ifr, sizeof(struct compat_ifreq)))
2796 return -EFAULT;
2797 ifr32++;
2798 ifr++;
2799 }
2800
2801 if (ifc32.ifcbuf == 0) {
2802 /* Translate from 64-bit structure multiple to
2803 * a 32-bit one.
2804 */
2805 i = ifc.ifc_len;
2806 i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
2807 ifc32.ifc_len = i;
2808 } else {
2809 ifc32.ifc_len = i;
2810 }
2811 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
2812 return -EFAULT;
2813
2814 return 0;
2815 }
2816
2817 static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
2818 {
2819 struct compat_ethtool_rxnfc __user *compat_rxnfc;
2820 bool convert_in = false, convert_out = false;
2821 size_t buf_size = ALIGN(sizeof(struct ifreq), 8);
2822 struct ethtool_rxnfc __user *rxnfc;
2823 struct ifreq __user *ifr;
2824 u32 rule_cnt = 0, actual_rule_cnt;
2825 u32 ethcmd;
2826 u32 data;
2827 int ret;
2828
2829 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2830 return -EFAULT;
2831
2832 compat_rxnfc = compat_ptr(data);
2833
2834 if (get_user(ethcmd, &compat_rxnfc->cmd))
2835 return -EFAULT;
2836
2837 /* Most ethtool structures are defined without padding.
2838 * Unfortunately struct ethtool_rxnfc is an exception.
2839 */
2840 switch (ethcmd) {
2841 default:
2842 break;
2843 case ETHTOOL_GRXCLSRLALL:
2844 /* Buffer size is variable */
2845 if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
2846 return -EFAULT;
2847 if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
2848 return -ENOMEM;
2849 buf_size += rule_cnt * sizeof(u32);
2850 /* fall through */
2851 case ETHTOOL_GRXRINGS:
2852 case ETHTOOL_GRXCLSRLCNT:
2853 case ETHTOOL_GRXCLSRULE:
2854 case ETHTOOL_SRXCLSRLINS:
2855 convert_out = true;
2856 /* fall through */
2857 case ETHTOOL_SRXCLSRLDEL:
2858 buf_size += sizeof(struct ethtool_rxnfc);
2859 convert_in = true;
2860 break;
2861 }
2862
2863 ifr = compat_alloc_user_space(buf_size);
2864 rxnfc = (void __user *)ifr + ALIGN(sizeof(struct ifreq), 8);
2865
2866 if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2867 return -EFAULT;
2868
2869 if (put_user(convert_in ? rxnfc : compat_ptr(data),
2870 &ifr->ifr_ifru.ifru_data))
2871 return -EFAULT;
2872
2873 if (convert_in) {
2874 /* We expect there to be holes between fs.m_ext and
2875 * fs.ring_cookie and at the end of fs, but nowhere else.
2876 */
2877 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
2878 sizeof(compat_rxnfc->fs.m_ext) !=
2879 offsetof(struct ethtool_rxnfc, fs.m_ext) +
2880 sizeof(rxnfc->fs.m_ext));
2881 BUILD_BUG_ON(
2882 offsetof(struct compat_ethtool_rxnfc, fs.location) -
2883 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
2884 offsetof(struct ethtool_rxnfc, fs.location) -
2885 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
2886
2887 if (copy_in_user(rxnfc, compat_rxnfc,
2888 (void __user *)(&rxnfc->fs.m_ext + 1) -
2889 (void __user *)rxnfc) ||
2890 copy_in_user(&rxnfc->fs.ring_cookie,
2891 &compat_rxnfc->fs.ring_cookie,
2892 (void __user *)(&rxnfc->fs.location + 1) -
2893 (void __user *)&rxnfc->fs.ring_cookie) ||
2894 copy_in_user(&rxnfc->rule_cnt, &compat_rxnfc->rule_cnt,
2895 sizeof(rxnfc->rule_cnt)))
2896 return -EFAULT;
2897 }
2898
2899 ret = dev_ioctl(net, SIOCETHTOOL, ifr);
2900 if (ret)
2901 return ret;
2902
2903 if (convert_out) {
2904 if (copy_in_user(compat_rxnfc, rxnfc,
2905 (const void __user *)(&rxnfc->fs.m_ext + 1) -
2906 (const void __user *)rxnfc) ||
2907 copy_in_user(&compat_rxnfc->fs.ring_cookie,
2908 &rxnfc->fs.ring_cookie,
2909 (const void __user *)(&rxnfc->fs.location + 1) -
2910 (const void __user *)&rxnfc->fs.ring_cookie) ||
2911 copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
2912 sizeof(rxnfc->rule_cnt)))
2913 return -EFAULT;
2914
2915 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
2916 /* As an optimisation, we only copy the actual
2917 * number of rules that the underlying
2918 * function returned. Since Mallory might
2919 * change the rule count in user memory, we
2920 * check that it is less than the rule count
2921 * originally given (as the user buffer size),
2922 * which has been range-checked.
2923 */
2924 if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
2925 return -EFAULT;
2926 if (actual_rule_cnt < rule_cnt)
2927 rule_cnt = actual_rule_cnt;
2928 if (copy_in_user(&compat_rxnfc->rule_locs[0],
2929 &rxnfc->rule_locs[0],
2930 rule_cnt * sizeof(u32)))
2931 return -EFAULT;
2932 }
2933 }
2934
2935 return 0;
2936 }
2937
2938 static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
2939 {
2940 void __user *uptr;
2941 compat_uptr_t uptr32;
2942 struct ifreq __user *uifr;
2943
2944 uifr = compat_alloc_user_space(sizeof(*uifr));
2945 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2946 return -EFAULT;
2947
2948 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
2949 return -EFAULT;
2950
2951 uptr = compat_ptr(uptr32);
2952
2953 if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
2954 return -EFAULT;
2955
2956 return dev_ioctl(net, SIOCWANDEV, uifr);
2957 }
2958
2959 static int bond_ioctl(struct net *net, unsigned int cmd,
2960 struct compat_ifreq __user *ifr32)
2961 {
2962 struct ifreq kifr;
2963 struct ifreq __user *uifr;
2964 mm_segment_t old_fs;
2965 int err;
2966 u32 data;
2967 void __user *datap;
2968
2969 switch (cmd) {
2970 case SIOCBONDENSLAVE:
2971 case SIOCBONDRELEASE:
2972 case SIOCBONDSETHWADDR:
2973 case SIOCBONDCHANGEACTIVE:
2974 if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
2975 return -EFAULT;
2976
2977 old_fs = get_fs();
2978 set_fs(KERNEL_DS);
2979 err = dev_ioctl(net, cmd,
2980 (struct ifreq __user __force *) &kifr);
2981 set_fs(old_fs);
2982
2983 return err;
2984 case SIOCBONDSLAVEINFOQUERY:
2985 case SIOCBONDINFOQUERY:
2986 uifr = compat_alloc_user_space(sizeof(*uifr));
2987 if (copy_in_user(&uifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2988 return -EFAULT;
2989
2990 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2991 return -EFAULT;
2992
2993 datap = compat_ptr(data);
2994 if (put_user(datap, &uifr->ifr_ifru.ifru_data))
2995 return -EFAULT;
2996
2997 return dev_ioctl(net, cmd, uifr);
2998 default:
2999 return -ENOIOCTLCMD;
3000 }
3001 }
3002
3003 static int siocdevprivate_ioctl(struct net *net, unsigned int cmd,
3004 struct compat_ifreq __user *u_ifreq32)
3005 {
3006 struct ifreq __user *u_ifreq64;
3007 char tmp_buf[IFNAMSIZ];
3008 void __user *data64;
3009 u32 data32;
3010
3011 if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
3012 IFNAMSIZ))
3013 return -EFAULT;
3014 if (__get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
3015 return -EFAULT;
3016 data64 = compat_ptr(data32);
3017
3018 u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
3019
3020 /* Don't check these user accesses, just let that get trapped
3021 * in the ioctl handler instead.
3022 */
3023 if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
3024 IFNAMSIZ))
3025 return -EFAULT;
3026 if (__put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
3027 return -EFAULT;
3028
3029 return dev_ioctl(net, cmd, u_ifreq64);
3030 }
3031
3032 static int dev_ifsioc(struct net *net, struct socket *sock,
3033 unsigned int cmd, struct compat_ifreq __user *uifr32)
3034 {
3035 struct ifreq __user *uifr;
3036 int err;
3037
3038 uifr = compat_alloc_user_space(sizeof(*uifr));
3039 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
3040 return -EFAULT;
3041
3042 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
3043
3044 if (!err) {
3045 switch (cmd) {
3046 case SIOCGIFFLAGS:
3047 case SIOCGIFMETRIC:
3048 case SIOCGIFMTU:
3049 case SIOCGIFMEM:
3050 case SIOCGIFHWADDR:
3051 case SIOCGIFINDEX:
3052 case SIOCGIFADDR:
3053 case SIOCGIFBRDADDR:
3054 case SIOCGIFDSTADDR:
3055 case SIOCGIFNETMASK:
3056 case SIOCGIFPFLAGS:
3057 case SIOCGIFTXQLEN:
3058 case SIOCGMIIPHY:
3059 case SIOCGMIIREG:
3060 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
3061 err = -EFAULT;
3062 break;
3063 }
3064 }
3065 return err;
3066 }
3067
3068 static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
3069 struct compat_ifreq __user *uifr32)
3070 {
3071 struct ifreq ifr;
3072 struct compat_ifmap __user *uifmap32;
3073 mm_segment_t old_fs;
3074 int err;
3075
3076 uifmap32 = &uifr32->ifr_ifru.ifru_map;
3077 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
3078 err |= __get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3079 err |= __get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3080 err |= __get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3081 err |= __get_user(ifr.ifr_map.irq, &uifmap32->irq);
3082 err |= __get_user(ifr.ifr_map.dma, &uifmap32->dma);
3083 err |= __get_user(ifr.ifr_map.port, &uifmap32->port);
3084 if (err)
3085 return -EFAULT;
3086
3087 old_fs = get_fs();
3088 set_fs(KERNEL_DS);
3089 err = dev_ioctl(net, cmd, (void __user __force *)&ifr);
3090 set_fs(old_fs);
3091
3092 if (cmd == SIOCGIFMAP && !err) {
3093 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
3094 err |= __put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3095 err |= __put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3096 err |= __put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3097 err |= __put_user(ifr.ifr_map.irq, &uifmap32->irq);
3098 err |= __put_user(ifr.ifr_map.dma, &uifmap32->dma);
3099 err |= __put_user(ifr.ifr_map.port, &uifmap32->port);
3100 if (err)
3101 err = -EFAULT;
3102 }
3103 return err;
3104 }
3105
3106 static int compat_siocshwtstamp(struct net *net, struct compat_ifreq __user *uifr32)
3107 {
3108 void __user *uptr;
3109 compat_uptr_t uptr32;
3110 struct ifreq __user *uifr;
3111
3112 uifr = compat_alloc_user_space(sizeof(*uifr));
3113 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
3114 return -EFAULT;
3115
3116 if (get_user(uptr32, &uifr32->ifr_data))
3117 return -EFAULT;
3118
3119 uptr = compat_ptr(uptr32);
3120
3121 if (put_user(uptr, &uifr->ifr_data))
3122 return -EFAULT;
3123
3124 return dev_ioctl(net, SIOCSHWTSTAMP, uifr);
3125 }
3126
3127 struct rtentry32 {
3128 u32 rt_pad1;
3129 struct sockaddr rt_dst; /* target address */
3130 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
3131 struct sockaddr rt_genmask; /* target network mask (IP) */
3132 unsigned short rt_flags;
3133 short rt_pad2;
3134 u32 rt_pad3;
3135 unsigned char rt_tos;
3136 unsigned char rt_class;
3137 short rt_pad4;
3138 short rt_metric; /* +1 for binary compatibility! */
3139 /* char * */ u32 rt_dev; /* forcing the device at add */
3140 u32 rt_mtu; /* per route MTU/Window */
3141 u32 rt_window; /* Window clamping */
3142 unsigned short rt_irtt; /* Initial RTT */
3143 };
3144
3145 struct in6_rtmsg32 {
3146 struct in6_addr rtmsg_dst;
3147 struct in6_addr rtmsg_src;
3148 struct in6_addr rtmsg_gateway;
3149 u32 rtmsg_type;
3150 u16 rtmsg_dst_len;
3151 u16 rtmsg_src_len;
3152 u32 rtmsg_metric;
3153 u32 rtmsg_info;
3154 u32 rtmsg_flags;
3155 s32 rtmsg_ifindex;
3156 };
3157
3158 static int routing_ioctl(struct net *net, struct socket *sock,
3159 unsigned int cmd, void __user *argp)
3160 {
3161 int ret;
3162 void *r = NULL;
3163 struct in6_rtmsg r6;
3164 struct rtentry r4;
3165 char devname[16];
3166 u32 rtdev;
3167 mm_segment_t old_fs = get_fs();
3168
3169 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
3170 struct in6_rtmsg32 __user *ur6 = argp;
3171 ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
3172 3 * sizeof(struct in6_addr));
3173 ret |= __get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
3174 ret |= __get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
3175 ret |= __get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
3176 ret |= __get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
3177 ret |= __get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
3178 ret |= __get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
3179 ret |= __get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
3180
3181 r = (void *) &r6;
3182 } else { /* ipv4 */
3183 struct rtentry32 __user *ur4 = argp;
3184 ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
3185 3 * sizeof(struct sockaddr));
3186 ret |= __get_user(r4.rt_flags, &(ur4->rt_flags));
3187 ret |= __get_user(r4.rt_metric, &(ur4->rt_metric));
3188 ret |= __get_user(r4.rt_mtu, &(ur4->rt_mtu));
3189 ret |= __get_user(r4.rt_window, &(ur4->rt_window));
3190 ret |= __get_user(r4.rt_irtt, &(ur4->rt_irtt));
3191 ret |= __get_user(rtdev, &(ur4->rt_dev));
3192 if (rtdev) {
3193 ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
3194 r4.rt_dev = (char __user __force *)devname;
3195 devname[15] = 0;
3196 } else
3197 r4.rt_dev = NULL;
3198
3199 r = (void *) &r4;
3200 }
3201
3202 if (ret) {
3203 ret = -EFAULT;
3204 goto out;
3205 }
3206
3207 set_fs(KERNEL_DS);
3208 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
3209 set_fs(old_fs);
3210
3211 out:
3212 return ret;
3213 }
3214
3215 /* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3216 * for some operations; this forces use of the newer bridge-utils that
3217 * use compatible ioctls
3218 */
3219 static int old_bridge_ioctl(compat_ulong_t __user *argp)
3220 {
3221 compat_ulong_t tmp;
3222
3223 if (get_user(tmp, argp))
3224 return -EFAULT;
3225 if (tmp == BRCTL_GET_VERSION)
3226 return BRCTL_VERSION + 1;
3227 return -EINVAL;
3228 }
3229
3230 static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3231 unsigned int cmd, unsigned long arg)
3232 {
3233 void __user *argp = compat_ptr(arg);
3234 struct sock *sk = sock->sk;
3235 struct net *net = sock_net(sk);
3236
3237 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
3238 return siocdevprivate_ioctl(net, cmd, argp);
3239
3240 switch (cmd) {
3241 case SIOCSIFBR:
3242 case SIOCGIFBR:
3243 return old_bridge_ioctl(argp);
3244 case SIOCGIFNAME:
3245 return dev_ifname32(net, argp);
3246 case SIOCGIFCONF:
3247 return dev_ifconf(net, argp);
3248 case SIOCETHTOOL:
3249 return ethtool_ioctl(net, argp);
3250 case SIOCWANDEV:
3251 return compat_siocwandev(net, argp);
3252 case SIOCGIFMAP:
3253 case SIOCSIFMAP:
3254 return compat_sioc_ifmap(net, cmd, argp);
3255 case SIOCBONDENSLAVE:
3256 case SIOCBONDRELEASE:
3257 case SIOCBONDSETHWADDR:
3258 case SIOCBONDSLAVEINFOQUERY:
3259 case SIOCBONDINFOQUERY:
3260 case SIOCBONDCHANGEACTIVE:
3261 return bond_ioctl(net, cmd, argp);
3262 case SIOCADDRT:
3263 case SIOCDELRT:
3264 return routing_ioctl(net, sock, cmd, argp);
3265 case SIOCGSTAMP:
3266 return do_siocgstamp(net, sock, cmd, argp);
3267 case SIOCGSTAMPNS:
3268 return do_siocgstampns(net, sock, cmd, argp);
3269 case SIOCSHWTSTAMP:
3270 return compat_siocshwtstamp(net, argp);
3271
3272 case FIOSETOWN:
3273 case SIOCSPGRP:
3274 case FIOGETOWN:
3275 case SIOCGPGRP:
3276 case SIOCBRADDBR:
3277 case SIOCBRDELBR:
3278 case SIOCGIFVLAN:
3279 case SIOCSIFVLAN:
3280 case SIOCADDDLCI:
3281 case SIOCDELDLCI:
3282 return sock_ioctl(file, cmd, arg);
3283
3284 case SIOCGIFFLAGS:
3285 case SIOCSIFFLAGS:
3286 case SIOCGIFMETRIC:
3287 case SIOCSIFMETRIC:
3288 case SIOCGIFMTU:
3289 case SIOCSIFMTU:
3290 case SIOCGIFMEM:
3291 case SIOCSIFMEM:
3292 case SIOCGIFHWADDR:
3293 case SIOCSIFHWADDR:
3294 case SIOCADDMULTI:
3295 case SIOCDELMULTI:
3296 case SIOCGIFINDEX:
3297 case SIOCGIFADDR:
3298 case SIOCSIFADDR:
3299 case SIOCSIFHWBROADCAST:
3300 case SIOCDIFADDR:
3301 case SIOCGIFBRDADDR:
3302 case SIOCSIFBRDADDR:
3303 case SIOCGIFDSTADDR:
3304 case SIOCSIFDSTADDR:
3305 case SIOCGIFNETMASK:
3306 case SIOCSIFNETMASK:
3307 case SIOCSIFPFLAGS:
3308 case SIOCGIFPFLAGS:
3309 case SIOCGIFTXQLEN:
3310 case SIOCSIFTXQLEN:
3311 case SIOCBRADDIF:
3312 case SIOCBRDELIF:
3313 case SIOCSIFNAME:
3314 case SIOCGMIIPHY:
3315 case SIOCGMIIREG:
3316 case SIOCSMIIREG:
3317 return dev_ifsioc(net, sock, cmd, argp);
3318
3319 case SIOCSARP:
3320 case SIOCGARP:
3321 case SIOCDARP:
3322 case SIOCATMARK:
3323 return sock_do_ioctl(net, sock, cmd, arg);
3324 }
3325
3326 return -ENOIOCTLCMD;
3327 }
3328
3329 static long compat_sock_ioctl(struct file *file, unsigned int cmd,
3330 unsigned long arg)
3331 {
3332 struct socket *sock = file->private_data;
3333 int ret = -ENOIOCTLCMD;
3334 struct sock *sk;
3335 struct net *net;
3336
3337 sk = sock->sk;
3338 net = sock_net(sk);
3339
3340 if (sock->ops->compat_ioctl)
3341 ret = sock->ops->compat_ioctl(sock, cmd, arg);
3342
3343 if (ret == -ENOIOCTLCMD &&
3344 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3345 ret = compat_wext_handle_ioctl(net, cmd, arg);
3346
3347 if (ret == -ENOIOCTLCMD)
3348 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3349
3350 return ret;
3351 }
3352 #endif
3353
3354 int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3355 {
3356 return sock->ops->bind(sock, addr, addrlen);
3357 }
3358 EXPORT_SYMBOL(kernel_bind);
3359
3360 int kernel_listen(struct socket *sock, int backlog)
3361 {
3362 return sock->ops->listen(sock, backlog);
3363 }
3364 EXPORT_SYMBOL(kernel_listen);
3365
3366 int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3367 {
3368 struct sock *sk = sock->sk;
3369 int err;
3370
3371 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3372 newsock);
3373 if (err < 0)
3374 goto done;
3375
3376 err = sock->ops->accept(sock, *newsock, flags);
3377 if (err < 0) {
3378 sock_release(*newsock);
3379 *newsock = NULL;
3380 goto done;
3381 }
3382
3383 (*newsock)->ops = sock->ops;
3384 __module_get((*newsock)->ops->owner);
3385
3386 done:
3387 return err;
3388 }
3389 EXPORT_SYMBOL(kernel_accept);
3390
3391 int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
3392 int flags)
3393 {
3394 return sock->ops->connect(sock, addr, addrlen, flags);
3395 }
3396 EXPORT_SYMBOL(kernel_connect);
3397
3398 int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
3399 int *addrlen)
3400 {
3401 return sock->ops->getname(sock, addr, addrlen, 0);
3402 }
3403 EXPORT_SYMBOL(kernel_getsockname);
3404
3405 int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
3406 int *addrlen)
3407 {
3408 return sock->ops->getname(sock, addr, addrlen, 1);
3409 }
3410 EXPORT_SYMBOL(kernel_getpeername);
3411
3412 int kernel_getsockopt(struct socket *sock, int level, int optname,
3413 char *optval, int *optlen)
3414 {
3415 mm_segment_t oldfs = get_fs();
3416 char __user *uoptval;
3417 int __user *uoptlen;
3418 int err;
3419
3420 uoptval = (char __user __force *) optval;
3421 uoptlen = (int __user __force *) optlen;
3422
3423 set_fs(KERNEL_DS);
3424 if (level == SOL_SOCKET)
3425 err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
3426 else
3427 err = sock->ops->getsockopt(sock, level, optname, uoptval,
3428 uoptlen);
3429 set_fs(oldfs);
3430 return err;
3431 }
3432 EXPORT_SYMBOL(kernel_getsockopt);
3433
3434 int kernel_setsockopt(struct socket *sock, int level, int optname,
3435 char *optval, unsigned int optlen)
3436 {
3437 mm_segment_t oldfs = get_fs();
3438 char __user *uoptval;
3439 int err;
3440
3441 uoptval = (char __user __force *) optval;
3442
3443 set_fs(KERNEL_DS);
3444 if (level == SOL_SOCKET)
3445 err = sock_setsockopt(sock, level, optname, uoptval, optlen);
3446 else
3447 err = sock->ops->setsockopt(sock, level, optname, uoptval,
3448 optlen);
3449 set_fs(oldfs);
3450 return err;
3451 }
3452 EXPORT_SYMBOL(kernel_setsockopt);
3453
3454 int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3455 size_t size, int flags)
3456 {
3457 if (sock->ops->sendpage)
3458 return sock->ops->sendpage(sock, page, offset, size, flags);
3459
3460 return sock_no_sendpage(sock, page, offset, size, flags);
3461 }
3462 EXPORT_SYMBOL(kernel_sendpage);
3463
3464 int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
3465 {
3466 mm_segment_t oldfs = get_fs();
3467 int err;
3468
3469 set_fs(KERNEL_DS);
3470 err = sock->ops->ioctl(sock, cmd, arg);
3471 set_fs(oldfs);
3472
3473 return err;
3474 }
3475 EXPORT_SYMBOL(kernel_sock_ioctl);
3476
3477 int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3478 {
3479 return sock->ops->shutdown(sock, how);
3480 }
3481 EXPORT_SYMBOL(kernel_sock_shutdown);