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