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