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