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