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