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