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