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