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