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