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