/net/socket.c

http://github.com/mirrors/linux · C · 3900 lines · 2673 code · 554 blank · 673 comment · 433 complexity · 84723dff9441b8f411429fc030df9f76 MD5 · raw file

Large files are truncated click here to view the full file

  1. // SPDX-License-Identifier: GPL-2.0-or-later
  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, <obz@Kodak.COM>
  8. * Ross Biro
  9. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  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. #include <linux/mm.h>
  55. #include <linux/socket.h>
  56. #include <linux/file.h>
  57. #include <linux/net.h>
  58. #include <linux/interrupt.h>
  59. #include <linux/thread_info.h>
  60. #include <linux/rcupdate.h>
  61. #include <linux/netdevice.h>
  62. #include <linux/proc_fs.h>
  63. #include <linux/seq_file.h>
  64. #include <linux/mutex.h>
  65. #include <linux/if_bridge.h>
  66. #include <linux/if_frad.h>
  67. #include <linux/if_vlan.h>
  68. #include <linux/ptp_classify.h>
  69. #include <linux/init.h>
  70. #include <linux/poll.h>
  71. #include <linux/cache.h>
  72. #include <linux/module.h>
  73. #include <linux/highmem.h>
  74. #include <linux/mount.h>
  75. #include <linux/pseudo_fs.h>
  76. #include <linux/security.h>
  77. #include <linux/syscalls.h>
  78. #include <linux/compat.h>
  79. #include <linux/kmod.h>
  80. #include <linux/audit.h>
  81. #include <linux/wireless.h>
  82. #include <linux/nsproxy.h>
  83. #include <linux/magic.h>
  84. #include <linux/slab.h>
  85. #include <linux/xattr.h>
  86. #include <linux/nospec.h>
  87. #include <linux/indirect_call_wrapper.h>
  88. #include <linux/uaccess.h>
  89. #include <asm/unistd.h>
  90. #include <net/compat.h>
  91. #include <net/wext.h>
  92. #include <net/cls_cgroup.h>
  93. #include <net/sock.h>
  94. #include <linux/netfilter.h>
  95. #include <linux/if_tun.h>
  96. #include <linux/ipv6_route.h>
  97. #include <linux/route.h>
  98. #include <linux/termios.h>
  99. #include <linux/sockios.h>
  100. #include <net/busy_poll.h>
  101. #include <linux/errqueue.h>
  102. #ifdef CONFIG_NET_RX_BUSY_POLL
  103. unsigned int sysctl_net_busy_read __read_mostly;
  104. unsigned int sysctl_net_busy_poll __read_mostly;
  105. #endif
  106. static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to);
  107. static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from);
  108. static int sock_mmap(struct file *file, struct vm_area_struct *vma);
  109. static int sock_close(struct inode *inode, struct file *file);
  110. static __poll_t sock_poll(struct file *file,
  111. struct poll_table_struct *wait);
  112. static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  113. #ifdef CONFIG_COMPAT
  114. static long compat_sock_ioctl(struct file *file,
  115. unsigned int cmd, unsigned long arg);
  116. #endif
  117. static int sock_fasync(int fd, struct file *filp, int on);
  118. static ssize_t sock_sendpage(struct file *file, struct page *page,
  119. int offset, size_t size, loff_t *ppos, int more);
  120. static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
  121. struct pipe_inode_info *pipe, size_t len,
  122. unsigned int flags);
  123. #ifdef CONFIG_PROC_FS
  124. static void sock_show_fdinfo(struct seq_file *m, struct file *f)
  125. {
  126. struct socket *sock = f->private_data;
  127. if (sock->ops->show_fdinfo)
  128. sock->ops->show_fdinfo(m, sock);
  129. }
  130. #else
  131. #define sock_show_fdinfo NULL
  132. #endif
  133. /*
  134. * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
  135. * in the operation structures but are done directly via the socketcall() multiplexor.
  136. */
  137. static const struct file_operations socket_file_ops = {
  138. .owner = THIS_MODULE,
  139. .llseek = no_llseek,
  140. .read_iter = sock_read_iter,
  141. .write_iter = sock_write_iter,
  142. .poll = sock_poll,
  143. .unlocked_ioctl = sock_ioctl,
  144. #ifdef CONFIG_COMPAT
  145. .compat_ioctl = compat_sock_ioctl,
  146. #endif
  147. .mmap = sock_mmap,
  148. .release = sock_close,
  149. .fasync = sock_fasync,
  150. .sendpage = sock_sendpage,
  151. .splice_write = generic_splice_sendpage,
  152. .splice_read = sock_splice_read,
  153. .show_fdinfo = sock_show_fdinfo,
  154. };
  155. /*
  156. * The protocol list. Each protocol is registered in here.
  157. */
  158. static DEFINE_SPINLOCK(net_family_lock);
  159. static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
  160. /*
  161. * Support routines.
  162. * Move socket addresses back and forth across the kernel/user
  163. * divide and look after the messy bits.
  164. */
  165. /**
  166. * move_addr_to_kernel - copy a socket address into kernel space
  167. * @uaddr: Address in user space
  168. * @kaddr: Address in kernel space
  169. * @ulen: Length in user space
  170. *
  171. * The address is copied into kernel space. If the provided address is
  172. * too long an error code of -EINVAL is returned. If the copy gives
  173. * invalid addresses -EFAULT is returned. On a success 0 is returned.
  174. */
  175. int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
  176. {
  177. if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
  178. return -EINVAL;
  179. if (ulen == 0)
  180. return 0;
  181. if (copy_from_user(kaddr, uaddr, ulen))
  182. return -EFAULT;
  183. return audit_sockaddr(ulen, kaddr);
  184. }
  185. /**
  186. * move_addr_to_user - copy an address to user space
  187. * @kaddr: kernel space address
  188. * @klen: length of address in kernel
  189. * @uaddr: user space address
  190. * @ulen: pointer to user length field
  191. *
  192. * The value pointed to by ulen on entry is the buffer length available.
  193. * This is overwritten with the buffer space used. -EINVAL is returned
  194. * if an overlong buffer is specified or a negative buffer size. -EFAULT
  195. * is returned if either the buffer or the length field are not
  196. * accessible.
  197. * After copying the data up to the limit the user specifies, the true
  198. * length of the data is written over the length limit the user
  199. * specified. Zero is returned for a success.
  200. */
  201. static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
  202. void __user *uaddr, int __user *ulen)
  203. {
  204. int err;
  205. int len;
  206. BUG_ON(klen > sizeof(struct sockaddr_storage));
  207. err = get_user(len, ulen);
  208. if (err)
  209. return err;
  210. if (len > klen)
  211. len = klen;
  212. if (len < 0)
  213. return -EINVAL;
  214. if (len) {
  215. if (audit_sockaddr(klen, kaddr))
  216. return -ENOMEM;
  217. if (copy_to_user(uaddr, kaddr, len))
  218. return -EFAULT;
  219. }
  220. /*
  221. * "fromlen shall refer to the value before truncation.."
  222. * 1003.1g
  223. */
  224. return __put_user(klen, ulen);
  225. }
  226. static struct kmem_cache *sock_inode_cachep __ro_after_init;
  227. static struct inode *sock_alloc_inode(struct super_block *sb)
  228. {
  229. struct socket_alloc *ei;
  230. ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
  231. if (!ei)
  232. return NULL;
  233. init_waitqueue_head(&ei->socket.wq.wait);
  234. ei->socket.wq.fasync_list = NULL;
  235. ei->socket.wq.flags = 0;
  236. ei->socket.state = SS_UNCONNECTED;
  237. ei->socket.flags = 0;
  238. ei->socket.ops = NULL;
  239. ei->socket.sk = NULL;
  240. ei->socket.file = NULL;
  241. return &ei->vfs_inode;
  242. }
  243. static void sock_free_inode(struct inode *inode)
  244. {
  245. struct socket_alloc *ei;
  246. ei = container_of(inode, struct socket_alloc, vfs_inode);
  247. kmem_cache_free(sock_inode_cachep, ei);
  248. }
  249. static void init_once(void *foo)
  250. {
  251. struct socket_alloc *ei = (struct socket_alloc *)foo;
  252. inode_init_once(&ei->vfs_inode);
  253. }
  254. static void init_inodecache(void)
  255. {
  256. sock_inode_cachep = kmem_cache_create("sock_inode_cache",
  257. sizeof(struct socket_alloc),
  258. 0,
  259. (SLAB_HWCACHE_ALIGN |
  260. SLAB_RECLAIM_ACCOUNT |
  261. SLAB_MEM_SPREAD | SLAB_ACCOUNT),
  262. init_once);
  263. BUG_ON(sock_inode_cachep == NULL);
  264. }
  265. static const struct super_operations sockfs_ops = {
  266. .alloc_inode = sock_alloc_inode,
  267. .free_inode = sock_free_inode,
  268. .statfs = simple_statfs,
  269. };
  270. /*
  271. * sockfs_dname() is called from d_path().
  272. */
  273. static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
  274. {
  275. return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
  276. d_inode(dentry)->i_ino);
  277. }
  278. static const struct dentry_operations sockfs_dentry_operations = {
  279. .d_dname = sockfs_dname,
  280. };
  281. static int sockfs_xattr_get(const struct xattr_handler *handler,
  282. struct dentry *dentry, struct inode *inode,
  283. const char *suffix, void *value, size_t size)
  284. {
  285. if (value) {
  286. if (dentry->d_name.len + 1 > size)
  287. return -ERANGE;
  288. memcpy(value, dentry->d_name.name, dentry->d_name.len + 1);
  289. }
  290. return dentry->d_name.len + 1;
  291. }
  292. #define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
  293. #define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
  294. #define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
  295. static const struct xattr_handler sockfs_xattr_handler = {
  296. .name = XATTR_NAME_SOCKPROTONAME,
  297. .get = sockfs_xattr_get,
  298. };
  299. static int sockfs_security_xattr_set(const struct xattr_handler *handler,
  300. struct dentry *dentry, struct inode *inode,
  301. const char *suffix, const void *value,
  302. size_t size, int flags)
  303. {
  304. /* Handled by LSM. */
  305. return -EAGAIN;
  306. }
  307. static const struct xattr_handler sockfs_security_xattr_handler = {
  308. .prefix = XATTR_SECURITY_PREFIX,
  309. .set = sockfs_security_xattr_set,
  310. };
  311. static const struct xattr_handler *sockfs_xattr_handlers[] = {
  312. &sockfs_xattr_handler,
  313. &sockfs_security_xattr_handler,
  314. NULL
  315. };
  316. static int sockfs_init_fs_context(struct fs_context *fc)
  317. {
  318. struct pseudo_fs_context *ctx = init_pseudo(fc, SOCKFS_MAGIC);
  319. if (!ctx)
  320. return -ENOMEM;
  321. ctx->ops = &sockfs_ops;
  322. ctx->dops = &sockfs_dentry_operations;
  323. ctx->xattr = sockfs_xattr_handlers;
  324. return 0;
  325. }
  326. static struct vfsmount *sock_mnt __read_mostly;
  327. static struct file_system_type sock_fs_type = {
  328. .name = "sockfs",
  329. .init_fs_context = sockfs_init_fs_context,
  330. .kill_sb = kill_anon_super,
  331. };
  332. /*
  333. * Obtains the first available file descriptor and sets it up for use.
  334. *
  335. * These functions create file structures and maps them to fd space
  336. * of the current process. On success it returns file descriptor
  337. * and file struct implicitly stored in sock->file.
  338. * Note that another thread may close file descriptor before we return
  339. * from this function. We use the fact that now we do not refer
  340. * to socket after mapping. If one day we will need it, this
  341. * function will increment ref. count on file by 1.
  342. *
  343. * In any case returned fd MAY BE not valid!
  344. * This race condition is unavoidable
  345. * with shared fd spaces, we cannot solve it inside kernel,
  346. * but we take care of internal coherence yet.
  347. */
  348. /**
  349. * sock_alloc_file - Bind a &socket to a &file
  350. * @sock: socket
  351. * @flags: file status flags
  352. * @dname: protocol name
  353. *
  354. * Returns the &file bound with @sock, implicitly storing it
  355. * in sock->file. If dname is %NULL, sets to "".
  356. * On failure the return is a ERR pointer (see linux/err.h).
  357. * This function uses GFP_KERNEL internally.
  358. */
  359. struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
  360. {
  361. struct file *file;
  362. if (!dname)
  363. dname = sock->sk ? sock->sk->sk_prot_creator->name : "";
  364. file = alloc_file_pseudo(SOCK_INODE(sock), sock_mnt, dname,
  365. O_RDWR | (flags & O_NONBLOCK),
  366. &socket_file_ops);
  367. if (IS_ERR(file)) {
  368. sock_release(sock);
  369. return file;
  370. }
  371. sock->file = file;
  372. file->private_data = sock;
  373. stream_open(SOCK_INODE(sock), file);
  374. return file;
  375. }
  376. EXPORT_SYMBOL(sock_alloc_file);
  377. static int sock_map_fd(struct socket *sock, int flags)
  378. {
  379. struct file *newfile;
  380. int fd = get_unused_fd_flags(flags);
  381. if (unlikely(fd < 0)) {
  382. sock_release(sock);
  383. return fd;
  384. }
  385. newfile = sock_alloc_file(sock, flags, NULL);
  386. if (!IS_ERR(newfile)) {
  387. fd_install(fd, newfile);
  388. return fd;
  389. }
  390. put_unused_fd(fd);
  391. return PTR_ERR(newfile);
  392. }
  393. /**
  394. * sock_from_file - Return the &socket bounded to @file.
  395. * @file: file
  396. * @err: pointer to an error code return
  397. *
  398. * On failure returns %NULL and assigns -ENOTSOCK to @err.
  399. */
  400. struct socket *sock_from_file(struct file *file, int *err)
  401. {
  402. if (file->f_op == &socket_file_ops)
  403. return file->private_data; /* set in sock_map_fd */
  404. *err = -ENOTSOCK;
  405. return NULL;
  406. }
  407. EXPORT_SYMBOL(sock_from_file);
  408. /**
  409. * sockfd_lookup - Go from a file number to its socket slot
  410. * @fd: file handle
  411. * @err: pointer to an error code return
  412. *
  413. * The file handle passed in is locked and the socket it is bound
  414. * to is returned. If an error occurs the err pointer is overwritten
  415. * with a negative errno code and NULL is returned. The function checks
  416. * for both invalid handles and passing a handle which is not a socket.
  417. *
  418. * On a success the socket object pointer is returned.
  419. */
  420. struct socket *sockfd_lookup(int fd, int *err)
  421. {
  422. struct file *file;
  423. struct socket *sock;
  424. file = fget(fd);
  425. if (!file) {
  426. *err = -EBADF;
  427. return NULL;
  428. }
  429. sock = sock_from_file(file, err);
  430. if (!sock)
  431. fput(file);
  432. return sock;
  433. }
  434. EXPORT_SYMBOL(sockfd_lookup);
  435. static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
  436. {
  437. struct fd f = fdget(fd);
  438. struct socket *sock;
  439. *err = -EBADF;
  440. if (f.file) {
  441. sock = sock_from_file(f.file, err);
  442. if (likely(sock)) {
  443. *fput_needed = f.flags;
  444. return sock;
  445. }
  446. fdput(f);
  447. }
  448. return NULL;
  449. }
  450. static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
  451. size_t size)
  452. {
  453. ssize_t len;
  454. ssize_t used = 0;
  455. len = security_inode_listsecurity(d_inode(dentry), buffer, size);
  456. if (len < 0)
  457. return len;
  458. used += len;
  459. if (buffer) {
  460. if (size < used)
  461. return -ERANGE;
  462. buffer += len;
  463. }
  464. len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
  465. used += len;
  466. if (buffer) {
  467. if (size < used)
  468. return -ERANGE;
  469. memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
  470. buffer += len;
  471. }
  472. return used;
  473. }
  474. static int sockfs_setattr(struct dentry *dentry, struct iattr *iattr)
  475. {
  476. int err = simple_setattr(dentry, iattr);
  477. if (!err && (iattr->ia_valid & ATTR_UID)) {
  478. struct socket *sock = SOCKET_I(d_inode(dentry));
  479. if (sock->sk)
  480. sock->sk->sk_uid = iattr->ia_uid;
  481. else
  482. err = -ENOENT;
  483. }
  484. return err;
  485. }
  486. static const struct inode_operations sockfs_inode_ops = {
  487. .listxattr = sockfs_listxattr,
  488. .setattr = sockfs_setattr,
  489. };
  490. /**
  491. * sock_alloc - allocate a socket
  492. *
  493. * Allocate a new inode and socket object. The two are bound together
  494. * and initialised. The socket is then returned. If we are out of inodes
  495. * NULL is returned. This functions uses GFP_KERNEL internally.
  496. */
  497. struct socket *sock_alloc(void)
  498. {
  499. struct inode *inode;
  500. struct socket *sock;
  501. inode = new_inode_pseudo(sock_mnt->mnt_sb);
  502. if (!inode)
  503. return NULL;
  504. sock = SOCKET_I(inode);
  505. inode->i_ino = get_next_ino();
  506. inode->i_mode = S_IFSOCK | S_IRWXUGO;
  507. inode->i_uid = current_fsuid();
  508. inode->i_gid = current_fsgid();
  509. inode->i_op = &sockfs_inode_ops;
  510. return sock;
  511. }
  512. EXPORT_SYMBOL(sock_alloc);
  513. /**
  514. * sock_release - close a socket
  515. * @sock: socket to close
  516. *
  517. * The socket is released from the protocol stack if it has a release
  518. * callback, and the inode is then released if the socket is bound to
  519. * an inode not a file.
  520. */
  521. static void __sock_release(struct socket *sock, struct inode *inode)
  522. {
  523. if (sock->ops) {
  524. struct module *owner = sock->ops->owner;
  525. if (inode)
  526. inode_lock(inode);
  527. sock->ops->release(sock);
  528. sock->sk = NULL;
  529. if (inode)
  530. inode_unlock(inode);
  531. sock->ops = NULL;
  532. module_put(owner);
  533. }
  534. if (sock->wq.fasync_list)
  535. pr_err("%s: fasync list not empty!\n", __func__);
  536. if (!sock->file) {
  537. iput(SOCK_INODE(sock));
  538. return;
  539. }
  540. sock->file = NULL;
  541. }
  542. void sock_release(struct socket *sock)
  543. {
  544. __sock_release(sock, NULL);
  545. }
  546. EXPORT_SYMBOL(sock_release);
  547. void __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags)
  548. {
  549. u8 flags = *tx_flags;
  550. if (tsflags & SOF_TIMESTAMPING_TX_HARDWARE)
  551. flags |= SKBTX_HW_TSTAMP;
  552. if (tsflags & SOF_TIMESTAMPING_TX_SOFTWARE)
  553. flags |= SKBTX_SW_TSTAMP;
  554. if (tsflags & SOF_TIMESTAMPING_TX_SCHED)
  555. flags |= SKBTX_SCHED_TSTAMP;
  556. *tx_flags = flags;
  557. }
  558. EXPORT_SYMBOL(__sock_tx_timestamp);
  559. INDIRECT_CALLABLE_DECLARE(int inet_sendmsg(struct socket *, struct msghdr *,
  560. size_t));
  561. INDIRECT_CALLABLE_DECLARE(int inet6_sendmsg(struct socket *, struct msghdr *,
  562. size_t));
  563. static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg)
  564. {
  565. int ret = INDIRECT_CALL_INET(sock->ops->sendmsg, inet6_sendmsg,
  566. inet_sendmsg, sock, msg,
  567. msg_data_left(msg));
  568. BUG_ON(ret == -EIOCBQUEUED);
  569. return ret;
  570. }
  571. /**
  572. * sock_sendmsg - send a message through @sock
  573. * @sock: socket
  574. * @msg: message to send
  575. *
  576. * Sends @msg through @sock, passing through LSM.
  577. * Returns the number of bytes sent, or an error code.
  578. */
  579. int sock_sendmsg(struct socket *sock, struct msghdr *msg)
  580. {
  581. int err = security_socket_sendmsg(sock, msg,
  582. msg_data_left(msg));
  583. return err ?: sock_sendmsg_nosec(sock, msg);
  584. }
  585. EXPORT_SYMBOL(sock_sendmsg);
  586. /**
  587. * kernel_sendmsg - send a message through @sock (kernel-space)
  588. * @sock: socket
  589. * @msg: message header
  590. * @vec: kernel vec
  591. * @num: vec array length
  592. * @size: total message data size
  593. *
  594. * Builds the message data with @vec and sends it through @sock.
  595. * Returns the number of bytes sent, or an error code.
  596. */
  597. int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
  598. struct kvec *vec, size_t num, size_t size)
  599. {
  600. iov_iter_kvec(&msg->msg_iter, WRITE, vec, num, size);
  601. return sock_sendmsg(sock, msg);
  602. }
  603. EXPORT_SYMBOL(kernel_sendmsg);
  604. /**
  605. * kernel_sendmsg_locked - send a message through @sock (kernel-space)
  606. * @sk: sock
  607. * @msg: message header
  608. * @vec: output s/g array
  609. * @num: output s/g array length
  610. * @size: total message data size
  611. *
  612. * Builds the message data with @vec and sends it through @sock.
  613. * Returns the number of bytes sent, or an error code.
  614. * Caller must hold @sk.
  615. */
  616. int kernel_sendmsg_locked(struct sock *sk, struct msghdr *msg,
  617. struct kvec *vec, size_t num, size_t size)
  618. {
  619. struct socket *sock = sk->sk_socket;
  620. if (!sock->ops->sendmsg_locked)
  621. return sock_no_sendmsg_locked(sk, msg, size);
  622. iov_iter_kvec(&msg->msg_iter, WRITE, vec, num, size);
  623. return sock->ops->sendmsg_locked(sk, msg, msg_data_left(msg));
  624. }
  625. EXPORT_SYMBOL(kernel_sendmsg_locked);
  626. static bool skb_is_err_queue(const struct sk_buff *skb)
  627. {
  628. /* pkt_type of skbs enqueued on the error queue are set to
  629. * PACKET_OUTGOING in skb_set_err_queue(). This is only safe to do
  630. * in recvmsg, since skbs received on a local socket will never
  631. * have a pkt_type of PACKET_OUTGOING.
  632. */
  633. return skb->pkt_type == PACKET_OUTGOING;
  634. }
  635. /* On transmit, software and hardware timestamps are returned independently.
  636. * As the two skb clones share the hardware timestamp, which may be updated
  637. * before the software timestamp is received, a hardware TX timestamp may be
  638. * returned only if there is no software TX timestamp. Ignore false software
  639. * timestamps, which may be made in the __sock_recv_timestamp() call when the
  640. * option SO_TIMESTAMP_OLD(NS) is enabled on the socket, even when the skb has a
  641. * hardware timestamp.
  642. */
  643. static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp)
  644. {
  645. return skb->tstamp && !false_tstamp && skb_is_err_queue(skb);
  646. }
  647. static void put_ts_pktinfo(struct msghdr *msg, struct sk_buff *skb)
  648. {
  649. struct scm_ts_pktinfo ts_pktinfo;
  650. struct net_device *orig_dev;
  651. if (!skb_mac_header_was_set(skb))
  652. return;
  653. memset(&ts_pktinfo, 0, sizeof(ts_pktinfo));
  654. rcu_read_lock();
  655. orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
  656. if (orig_dev)
  657. ts_pktinfo.if_index = orig_dev->ifindex;
  658. rcu_read_unlock();
  659. ts_pktinfo.pkt_length = skb->len - skb_mac_offset(skb);
  660. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_PKTINFO,
  661. sizeof(ts_pktinfo), &ts_pktinfo);
  662. }
  663. /*
  664. * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
  665. */
  666. void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
  667. struct sk_buff *skb)
  668. {
  669. int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
  670. int new_tstamp = sock_flag(sk, SOCK_TSTAMP_NEW);
  671. struct scm_timestamping_internal tss;
  672. int empty = 1, false_tstamp = 0;
  673. struct skb_shared_hwtstamps *shhwtstamps =
  674. skb_hwtstamps(skb);
  675. /* Race occurred between timestamp enabling and packet
  676. receiving. Fill in the current time for now. */
  677. if (need_software_tstamp && skb->tstamp == 0) {
  678. __net_timestamp(skb);
  679. false_tstamp = 1;
  680. }
  681. if (need_software_tstamp) {
  682. if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
  683. if (new_tstamp) {
  684. struct __kernel_sock_timeval tv;
  685. skb_get_new_timestamp(skb, &tv);
  686. put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_NEW,
  687. sizeof(tv), &tv);
  688. } else {
  689. struct __kernel_old_timeval tv;
  690. skb_get_timestamp(skb, &tv);
  691. put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_OLD,
  692. sizeof(tv), &tv);
  693. }
  694. } else {
  695. if (new_tstamp) {
  696. struct __kernel_timespec ts;
  697. skb_get_new_timestampns(skb, &ts);
  698. put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_NEW,
  699. sizeof(ts), &ts);
  700. } else {
  701. struct __kernel_old_timespec ts;
  702. skb_get_timestampns(skb, &ts);
  703. put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_OLD,
  704. sizeof(ts), &ts);
  705. }
  706. }
  707. }
  708. memset(&tss, 0, sizeof(tss));
  709. if ((sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
  710. ktime_to_timespec64_cond(skb->tstamp, tss.ts + 0))
  711. empty = 0;
  712. if (shhwtstamps &&
  713. (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
  714. !skb_is_swtx_tstamp(skb, false_tstamp) &&
  715. ktime_to_timespec64_cond(shhwtstamps->hwtstamp, tss.ts + 2)) {
  716. empty = 0;
  717. if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_PKTINFO) &&
  718. !skb_is_err_queue(skb))
  719. put_ts_pktinfo(msg, skb);
  720. }
  721. if (!empty) {
  722. if (sock_flag(sk, SOCK_TSTAMP_NEW))
  723. put_cmsg_scm_timestamping64(msg, &tss);
  724. else
  725. put_cmsg_scm_timestamping(msg, &tss);
  726. if (skb_is_err_queue(skb) && skb->len &&
  727. SKB_EXT_ERR(skb)->opt_stats)
  728. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_OPT_STATS,
  729. skb->len, skb->data);
  730. }
  731. }
  732. EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
  733. void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
  734. struct sk_buff *skb)
  735. {
  736. int ack;
  737. if (!sock_flag(sk, SOCK_WIFI_STATUS))
  738. return;
  739. if (!skb->wifi_acked_valid)
  740. return;
  741. ack = skb->wifi_acked;
  742. put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
  743. }
  744. EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
  745. static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
  746. struct sk_buff *skb)
  747. {
  748. if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && SOCK_SKB_CB(skb)->dropcount)
  749. put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
  750. sizeof(__u32), &SOCK_SKB_CB(skb)->dropcount);
  751. }
  752. void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
  753. struct sk_buff *skb)
  754. {
  755. sock_recv_timestamp(msg, sk, skb);
  756. sock_recv_drops(msg, sk, skb);
  757. }
  758. EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
  759. INDIRECT_CALLABLE_DECLARE(int inet_recvmsg(struct socket *, struct msghdr *,
  760. size_t, int));
  761. INDIRECT_CALLABLE_DECLARE(int inet6_recvmsg(struct socket *, struct msghdr *,
  762. size_t, int));
  763. static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
  764. int flags)
  765. {
  766. return INDIRECT_CALL_INET(sock->ops->recvmsg, inet6_recvmsg,
  767. inet_recvmsg, sock, msg, msg_data_left(msg),
  768. flags);
  769. }
  770. /**
  771. * sock_recvmsg - receive a message from @sock
  772. * @sock: socket
  773. * @msg: message to receive
  774. * @flags: message flags
  775. *
  776. * Receives @msg from @sock, passing through LSM. Returns the total number
  777. * of bytes received, or an error.
  778. */
  779. int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags)
  780. {
  781. int err = security_socket_recvmsg(sock, msg, msg_data_left(msg), flags);
  782. return err ?: sock_recvmsg_nosec(sock, msg, flags);
  783. }
  784. EXPORT_SYMBOL(sock_recvmsg);
  785. /**
  786. * kernel_recvmsg - Receive a message from a socket (kernel space)
  787. * @sock: The socket to receive the message from
  788. * @msg: Received message
  789. * @vec: Input s/g array for message data
  790. * @num: Size of input s/g array
  791. * @size: Number of bytes to read
  792. * @flags: Message flags (MSG_DONTWAIT, etc...)
  793. *
  794. * On return the msg structure contains the scatter/gather array passed in the
  795. * vec argument. The array is modified so that it consists of the unfilled
  796. * portion of the original array.
  797. *
  798. * The returned value is the total number of bytes received, or an error.
  799. */
  800. int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
  801. struct kvec *vec, size_t num, size_t size, int flags)
  802. {
  803. mm_segment_t oldfs = get_fs();
  804. int result;
  805. iov_iter_kvec(&msg->msg_iter, READ, vec, num, size);
  806. set_fs(KERNEL_DS);
  807. result = sock_recvmsg(sock, msg, flags);
  808. set_fs(oldfs);
  809. return result;
  810. }
  811. EXPORT_SYMBOL(kernel_recvmsg);
  812. static ssize_t sock_sendpage(struct file *file, struct page *page,
  813. int offset, size_t size, loff_t *ppos, int more)
  814. {
  815. struct socket *sock;
  816. int flags;
  817. sock = file->private_data;
  818. flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  819. /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
  820. flags |= more;
  821. return kernel_sendpage(sock, page, offset, size, flags);
  822. }
  823. static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
  824. struct pipe_inode_info *pipe, size_t len,
  825. unsigned int flags)
  826. {
  827. struct socket *sock = file->private_data;
  828. if (unlikely(!sock->ops->splice_read))
  829. return generic_file_splice_read(file, ppos, pipe, len, flags);
  830. return sock->ops->splice_read(sock, ppos, pipe, len, flags);
  831. }
  832. static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to)
  833. {
  834. struct file *file = iocb->ki_filp;
  835. struct socket *sock = file->private_data;
  836. struct msghdr msg = {.msg_iter = *to,
  837. .msg_iocb = iocb};
  838. ssize_t res;
  839. if (file->f_flags & O_NONBLOCK || (iocb->ki_flags & IOCB_NOWAIT))
  840. msg.msg_flags = MSG_DONTWAIT;
  841. if (iocb->ki_pos != 0)
  842. return -ESPIPE;
  843. if (!iov_iter_count(to)) /* Match SYS5 behaviour */
  844. return 0;
  845. res = sock_recvmsg(sock, &msg, msg.msg_flags);
  846. *to = msg.msg_iter;
  847. return res;
  848. }
  849. static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from)
  850. {
  851. struct file *file = iocb->ki_filp;
  852. struct socket *sock = file->private_data;
  853. struct msghdr msg = {.msg_iter = *from,
  854. .msg_iocb = iocb};
  855. ssize_t res;
  856. if (iocb->ki_pos != 0)
  857. return -ESPIPE;
  858. if (file->f_flags & O_NONBLOCK || (iocb->ki_flags & IOCB_NOWAIT))
  859. msg.msg_flags = MSG_DONTWAIT;
  860. if (sock->type == SOCK_SEQPACKET)
  861. msg.msg_flags |= MSG_EOR;
  862. res = sock_sendmsg(sock, &msg);
  863. *from = msg.msg_iter;
  864. return res;
  865. }
  866. /*
  867. * Atomic setting of ioctl hooks to avoid race
  868. * with module unload.
  869. */
  870. static DEFINE_MUTEX(br_ioctl_mutex);
  871. static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
  872. void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
  873. {
  874. mutex_lock(&br_ioctl_mutex);
  875. br_ioctl_hook = hook;
  876. mutex_unlock(&br_ioctl_mutex);
  877. }
  878. EXPORT_SYMBOL(brioctl_set);
  879. static DEFINE_MUTEX(vlan_ioctl_mutex);
  880. static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
  881. void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
  882. {
  883. mutex_lock(&vlan_ioctl_mutex);
  884. vlan_ioctl_hook = hook;
  885. mutex_unlock(&vlan_ioctl_mutex);
  886. }
  887. EXPORT_SYMBOL(vlan_ioctl_set);
  888. static DEFINE_MUTEX(dlci_ioctl_mutex);
  889. static int (*dlci_ioctl_hook) (unsigned int, void __user *);
  890. void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
  891. {
  892. mutex_lock(&dlci_ioctl_mutex);
  893. dlci_ioctl_hook = hook;
  894. mutex_unlock(&dlci_ioctl_mutex);
  895. }
  896. EXPORT_SYMBOL(dlci_ioctl_set);
  897. static long sock_do_ioctl(struct net *net, struct socket *sock,
  898. unsigned int cmd, unsigned long arg)
  899. {
  900. int err;
  901. void __user *argp = (void __user *)arg;
  902. err = sock->ops->ioctl(sock, cmd, arg);
  903. /*
  904. * If this ioctl is unknown try to hand it down
  905. * to the NIC driver.
  906. */
  907. if (err != -ENOIOCTLCMD)
  908. return err;
  909. if (cmd == SIOCGIFCONF) {
  910. struct ifconf ifc;
  911. if (copy_from_user(&ifc, argp, sizeof(struct ifconf)))
  912. return -EFAULT;
  913. rtnl_lock();
  914. err = dev_ifconf(net, &ifc, sizeof(struct ifreq));
  915. rtnl_unlock();
  916. if (!err && copy_to_user(argp, &ifc, sizeof(struct ifconf)))
  917. err = -EFAULT;
  918. } else {
  919. struct ifreq ifr;
  920. bool need_copyout;
  921. if (copy_from_user(&ifr, argp, sizeof(struct ifreq)))
  922. return -EFAULT;
  923. err = dev_ioctl(net, cmd, &ifr, &need_copyout);
  924. if (!err && need_copyout)
  925. if (copy_to_user(argp, &ifr, sizeof(struct ifreq)))
  926. return -EFAULT;
  927. }
  928. return err;
  929. }
  930. /*
  931. * With an ioctl, arg may well be a user mode pointer, but we don't know
  932. * what to do with it - that's up to the protocol still.
  933. */
  934. /**
  935. * get_net_ns - increment the refcount of the network namespace
  936. * @ns: common namespace (net)
  937. *
  938. * Returns the net's common namespace.
  939. */
  940. struct ns_common *get_net_ns(struct ns_common *ns)
  941. {
  942. return &get_net(container_of(ns, struct net, ns))->ns;
  943. }
  944. EXPORT_SYMBOL_GPL(get_net_ns);
  945. static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  946. {
  947. struct socket *sock;
  948. struct sock *sk;
  949. void __user *argp = (void __user *)arg;
  950. int pid, err;
  951. struct net *net;
  952. sock = file->private_data;
  953. sk = sock->sk;
  954. net = sock_net(sk);
  955. if (unlikely(cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))) {
  956. struct ifreq ifr;
  957. bool need_copyout;
  958. if (copy_from_user(&ifr, argp, sizeof(struct ifreq)))
  959. return -EFAULT;
  960. err = dev_ioctl(net, cmd, &ifr, &need_copyout);
  961. if (!err && need_copyout)
  962. if (copy_to_user(argp, &ifr, sizeof(struct ifreq)))
  963. return -EFAULT;
  964. } else
  965. #ifdef CONFIG_WEXT_CORE
  966. if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
  967. err = wext_handle_ioctl(net, cmd, argp);
  968. } else
  969. #endif
  970. switch (cmd) {
  971. case FIOSETOWN:
  972. case SIOCSPGRP:
  973. err = -EFAULT;
  974. if (get_user(pid, (int __user *)argp))
  975. break;
  976. err = f_setown(sock->file, pid, 1);
  977. break;
  978. case FIOGETOWN:
  979. case SIOCGPGRP:
  980. err = put_user(f_getown(sock->file),
  981. (int __user *)argp);
  982. break;
  983. case SIOCGIFBR:
  984. case SIOCSIFBR:
  985. case SIOCBRADDBR:
  986. case SIOCBRDELBR:
  987. err = -ENOPKG;
  988. if (!br_ioctl_hook)
  989. request_module("bridge");
  990. mutex_lock(&br_ioctl_mutex);
  991. if (br_ioctl_hook)
  992. err = br_ioctl_hook(net, cmd, argp);
  993. mutex_unlock(&br_ioctl_mutex);
  994. break;
  995. case SIOCGIFVLAN:
  996. case SIOCSIFVLAN:
  997. err = -ENOPKG;
  998. if (!vlan_ioctl_hook)
  999. request_module("8021q");
  1000. mutex_lock(&vlan_ioctl_mutex);
  1001. if (vlan_ioctl_hook)
  1002. err = vlan_ioctl_hook(net, argp);
  1003. mutex_unlock(&vlan_ioctl_mutex);
  1004. break;
  1005. case SIOCADDDLCI:
  1006. case SIOCDELDLCI:
  1007. err = -ENOPKG;
  1008. if (!dlci_ioctl_hook)
  1009. request_module("dlci");
  1010. mutex_lock(&dlci_ioctl_mutex);
  1011. if (dlci_ioctl_hook)
  1012. err = dlci_ioctl_hook(cmd, argp);
  1013. mutex_unlock(&dlci_ioctl_mutex);
  1014. break;
  1015. case SIOCGSKNS:
  1016. err = -EPERM;
  1017. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1018. break;
  1019. err = open_related_ns(&net->ns, get_net_ns);
  1020. break;
  1021. case SIOCGSTAMP_OLD:
  1022. case SIOCGSTAMPNS_OLD:
  1023. if (!sock->ops->gettstamp) {
  1024. err = -ENOIOCTLCMD;
  1025. break;
  1026. }
  1027. err = sock->ops->gettstamp(sock, argp,
  1028. cmd == SIOCGSTAMP_OLD,
  1029. !IS_ENABLED(CONFIG_64BIT));
  1030. break;
  1031. case SIOCGSTAMP_NEW:
  1032. case SIOCGSTAMPNS_NEW:
  1033. if (!sock->ops->gettstamp) {
  1034. err = -ENOIOCTLCMD;
  1035. break;
  1036. }
  1037. err = sock->ops->gettstamp(sock, argp,
  1038. cmd == SIOCGSTAMP_NEW,
  1039. false);
  1040. break;
  1041. default:
  1042. err = sock_do_ioctl(net, sock, cmd, arg);
  1043. break;
  1044. }
  1045. return err;
  1046. }
  1047. /**
  1048. * sock_create_lite - creates a socket
  1049. * @family: protocol family (AF_INET, ...)
  1050. * @type: communication type (SOCK_STREAM, ...)
  1051. * @protocol: protocol (0, ...)
  1052. * @res: new socket
  1053. *
  1054. * Creates a new socket and assigns it to @res, passing through LSM.
  1055. * The new socket initialization is not complete, see kernel_accept().
  1056. * Returns 0 or an error. On failure @res is set to %NULL.
  1057. * This function internally uses GFP_KERNEL.
  1058. */
  1059. int sock_create_lite(int family, int type, int protocol, struct socket **res)
  1060. {
  1061. int err;
  1062. struct socket *sock = NULL;
  1063. err = security_socket_create(family, type, protocol, 1);
  1064. if (err)
  1065. goto out;
  1066. sock = sock_alloc();
  1067. if (!sock) {
  1068. err = -ENOMEM;
  1069. goto out;
  1070. }
  1071. sock->type = type;
  1072. err = security_socket_post_create(sock, family, type, protocol, 1);
  1073. if (err)
  1074. goto out_release;
  1075. out:
  1076. *res = sock;
  1077. return err;
  1078. out_release:
  1079. sock_release(sock);
  1080. sock = NULL;
  1081. goto out;
  1082. }
  1083. EXPORT_SYMBOL(sock_create_lite);
  1084. /* No kernel lock held - perfect */
  1085. static __poll_t sock_poll(struct file *file, poll_table *wait)
  1086. {
  1087. struct socket *sock = file->private_data;
  1088. __poll_t events = poll_requested_events(wait), flag = 0;
  1089. if (!sock->ops->poll)
  1090. return 0;
  1091. if (sk_can_busy_loop(sock->sk)) {
  1092. /* poll once if requested by the syscall */
  1093. if (events & POLL_BUSY_LOOP)
  1094. sk_busy_loop(sock->sk, 1);
  1095. /* if this socket can poll_ll, tell the system call */
  1096. flag = POLL_BUSY_LOOP;
  1097. }
  1098. return sock->ops->poll(file, sock, wait) | flag;
  1099. }
  1100. static int sock_mmap(struct file *file, struct vm_area_struct *vma)
  1101. {
  1102. struct socket *sock = file->private_data;
  1103. return sock->ops->mmap(file, sock, vma);
  1104. }
  1105. static int sock_close(struct inode *inode, struct file *filp)
  1106. {
  1107. __sock_release(SOCKET_I(inode), inode);
  1108. return 0;
  1109. }
  1110. /*
  1111. * Update the socket async list
  1112. *
  1113. * Fasync_list locking strategy.
  1114. *
  1115. * 1. fasync_list is modified only under process context socket lock
  1116. * i.e. under semaphore.
  1117. * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
  1118. * or under socket lock
  1119. */
  1120. static int sock_fasync(int fd, struct file *filp, int on)
  1121. {
  1122. struct socket *sock = filp->private_data;
  1123. struct sock *sk = sock->sk;
  1124. struct socket_wq *wq = &sock->wq;
  1125. if (sk == NULL)
  1126. return -EINVAL;
  1127. lock_sock(sk);
  1128. fasync_helper(fd, filp, on, &wq->fasync_list);
  1129. if (!wq->fasync_list)
  1130. sock_reset_flag(sk, SOCK_FASYNC);
  1131. else
  1132. sock_set_flag(sk, SOCK_FASYNC);
  1133. release_sock(sk);
  1134. return 0;
  1135. }
  1136. /* This function may be called only under rcu_lock */
  1137. int sock_wake_async(struct socket_wq *wq, int how, int band)
  1138. {
  1139. if (!wq || !wq->fasync_list)
  1140. return -1;
  1141. switch (how) {
  1142. case SOCK_WAKE_WAITD:
  1143. if (test_bit(SOCKWQ_ASYNC_WAITDATA, &wq->flags))
  1144. break;
  1145. goto call_kill;
  1146. case SOCK_WAKE_SPACE:
  1147. if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags))
  1148. break;
  1149. /* fall through */
  1150. case SOCK_WAKE_IO:
  1151. call_kill:
  1152. kill_fasync(&wq->fasync_list, SIGIO, band);
  1153. break;
  1154. case SOCK_WAKE_URG:
  1155. kill_fasync(&wq->fasync_list, SIGURG, band);
  1156. }
  1157. return 0;
  1158. }
  1159. EXPORT_SYMBOL(sock_wake_async);
  1160. /**
  1161. * __sock_create - creates a socket
  1162. * @net: net namespace
  1163. * @family: protocol family (AF_INET, ...)
  1164. * @type: communication type (SOCK_STREAM, ...)
  1165. * @protocol: protocol (0, ...)
  1166. * @res: new socket
  1167. * @kern: boolean for kernel space sockets
  1168. *
  1169. * Creates a new socket and assigns it to @res, passing through LSM.
  1170. * Returns 0 or an error. On failure @res is set to %NULL. @kern must
  1171. * be set to true if the socket resides in kernel space.
  1172. * This function internally uses GFP_KERNEL.
  1173. */
  1174. int __sock_create(struct net *net, int family, int type, int protocol,
  1175. struct socket **res, int kern)
  1176. {
  1177. int err;
  1178. struct socket *sock;
  1179. const struct net_proto_family *pf;
  1180. /*
  1181. * Check protocol is in range
  1182. */
  1183. if (family < 0 || family >= NPROTO)
  1184. return -EAFNOSUPPORT;
  1185. if (type < 0 || type >= SOCK_MAX)
  1186. return -EINVAL;
  1187. /* Compatibility.
  1188. This uglymoron is moved from INET layer to here to avoid
  1189. deadlock in module load.
  1190. */
  1191. if (family == PF_INET && type == SOCK_PACKET) {
  1192. pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
  1193. current->comm);
  1194. family = PF_PACKET;
  1195. }
  1196. err = security_socket_create(family, type, protocol, kern);
  1197. if (err)
  1198. return err;
  1199. /*
  1200. * Allocate the socket and allow the family to set things up. if
  1201. * the protocol is 0, the family is instructed to select an appropriate
  1202. * default.
  1203. */
  1204. sock = sock_alloc();
  1205. if (!sock) {
  1206. net_warn_ratelimited("socket: no more sockets\n");
  1207. return -ENFILE; /* Not exactly a match, but its the
  1208. closest posix thing */
  1209. }
  1210. sock->type = type;
  1211. #ifdef CONFIG_MODULES
  1212. /* Attempt to load a protocol module if the find failed.
  1213. *
  1214. * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
  1215. * requested real, full-featured networking support upon configuration.
  1216. * Otherwise module support will break!
  1217. */
  1218. if (rcu_access_pointer(net_families[family]) == NULL)
  1219. request_module("net-pf-%d", family);
  1220. #endif
  1221. rcu_read_lock();
  1222. pf = rcu_dereference(net_families[family]);
  1223. err = -EAFNOSUPPORT;
  1224. if (!pf)
  1225. goto out_release;
  1226. /*
  1227. * We will call the ->create function, that possibly is in a loadable
  1228. * module, so we have to bump that loadable module refcnt first.
  1229. */
  1230. if (!try_module_get(pf->owner))
  1231. goto out_release;
  1232. /* Now protected by module ref count */
  1233. rcu_read_unlock();
  1234. err = pf->create(net, sock, protocol, kern);
  1235. if (err < 0)
  1236. goto out_module_put;
  1237. /*
  1238. * Now to bump the refcnt of the [loadable] module that owns this
  1239. * socket at sock_release time we decrement its refcnt.
  1240. */
  1241. if (!try_module_get(sock->ops->owner))
  1242. goto out_module_busy;
  1243. /*
  1244. * Now that we're done with the ->create function, the [loadable]
  1245. * module can have its refcnt decremented
  1246. */
  1247. module_put(pf->owner);
  1248. err = security_socket_post_create(sock, family, type, protocol, kern);
  1249. if (err)
  1250. goto out_sock_release;
  1251. *res = sock;
  1252. return 0;
  1253. out_module_busy:
  1254. err = -EAFNOSUPPORT;
  1255. out_module_put:
  1256. sock->ops = NULL;
  1257. module_put(pf->owner);
  1258. out_sock_release:
  1259. sock_release(sock);
  1260. return err;
  1261. out_release:
  1262. rcu_read_unlock();
  1263. goto out_sock_release;
  1264. }
  1265. EXPORT_SYMBOL(__sock_create);
  1266. /**
  1267. * sock_create - creates a socket
  1268. * @family: protocol family (AF_INET, ...)
  1269. * @type: communication type (SOCK_STREAM, ...)
  1270. * @protocol: protocol (0, ...)
  1271. * @res: new socket
  1272. *
  1273. * A wrapper around __sock_create().
  1274. * Returns 0 or an error. This function internally uses GFP_KERNEL.
  1275. */
  1276. int sock_create(int family, int type, int protocol, struct socket **res)
  1277. {
  1278. return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
  1279. }
  1280. EXPORT_SYMBOL(sock_create);
  1281. /**
  1282. * sock_create_kern - creates a socket (kernel space)
  1283. * @net: net namespace
  1284. * @family: protocol family (AF_INET, ...)
  1285. * @type: communication type (SOCK_STREAM, ...)
  1286. * @protocol: protocol (0, ...)
  1287. * @res: new socket
  1288. *
  1289. * A wrapper around __sock_create().
  1290. * Returns 0 or an error. This function internally uses GFP_KERNEL.
  1291. */
  1292. int sock_create_kern(struct net *net, int family, int type, int protocol, struct socket **res)
  1293. {
  1294. return __sock_create(net, family, type, protocol, res, 1);
  1295. }
  1296. EXPORT_SYMBOL(sock_create_kern);
  1297. int __sys_socket(int family, int type, int protocol)
  1298. {
  1299. int retval;
  1300. struct socket *sock;
  1301. int flags;
  1302. /* Check the SOCK_* constants for consistency. */
  1303. BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
  1304. BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
  1305. BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
  1306. BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
  1307. flags = type & ~SOCK_TYPE_MASK;
  1308. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1309. return -EINVAL;
  1310. type &= SOCK_TYPE_MASK;
  1311. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1312. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1313. retval = sock_create(family, type, protocol, &sock);
  1314. if (retval < 0)
  1315. return retval;
  1316. return sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
  1317. }
  1318. SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
  1319. {
  1320. return __sys_socket(family, type, protocol);
  1321. }
  1322. /*
  1323. * Create a pair of connected sockets.
  1324. */
  1325. int __sys_socketpair(int family, int type, int protocol, int __user *usockvec)
  1326. {
  1327. struct socket *sock1, *sock2;
  1328. int fd1, fd2, err;
  1329. struct file *newfile1, *newfile2;
  1330. int flags;
  1331. flags = type & ~SOCK_TYPE_MASK;
  1332. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1333. return -EINVAL;
  1334. type &= SOCK_TYPE_MASK;
  1335. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1336. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1337. /*
  1338. * reserve descriptors and make sure we won't fail
  1339. * to return them to userland.
  1340. */
  1341. fd1 = get_unused_fd_flags(flags);
  1342. if (unlikely(fd1 < 0))
  1343. return fd1;
  1344. fd2 = get_unused_fd_flags(flags);
  1345. if (unlikely(fd2 < 0)) {
  1346. put_unused_fd(fd1);
  1347. return fd2;
  1348. }
  1349. err = put_user(fd1, &usockvec[0]);
  1350. if (err)
  1351. goto out;
  1352. err = put_user(fd2, &usockvec[1]);
  1353. if (err)
  1354. goto out;
  1355. /*
  1356. * Obtain the first socket and check if the underlying protocol
  1357. * supports the socketpair call.
  1358. */
  1359. err = sock_create(family, type, protocol, &sock1);
  1360. if (unlikely(err < 0))
  1361. goto out;
  1362. err = sock_create(family, type, protocol, &sock2);
  1363. if (unlikely(err < 0)) {
  1364. sock_release(sock1);
  1365. goto out;
  1366. }
  1367. err = security_socket_socketpair(sock1, sock2);
  1368. if (unlikely(err)) {
  1369. sock_release(sock2);
  1370. sock_release(sock1);
  1371. goto out;
  1372. }
  1373. err = sock1->ops->socketpair(sock1, sock2);
  1374. if (unlikely(err < 0)) {
  1375. sock_release(sock2);
  1376. sock_release(sock1);
  1377. goto out;
  1378. }
  1379. newfile1 = sock_alloc_file(sock1, flags, NULL);
  1380. if (IS_ERR(newfile1)) {
  1381. err = PTR_ERR(newfile1);
  1382. sock_release(sock2);
  1383. goto out;
  1384. }
  1385. newfile2 = sock_alloc_file(sock2, flags, NULL);
  1386. if (IS_ERR(newfile2)) {
  1387. err = PTR_ERR(newfile2);
  1388. fput(newfile1);
  1389. goto out;
  1390. }
  1391. audit_fd_pair(fd1, fd2);
  1392. fd_install(fd1, newfile1);
  1393. fd_install(fd2, newfile2);
  1394. return 0;
  1395. out:
  1396. put_unused_fd(fd2);
  1397. put_unused_fd(fd1);
  1398. return err;
  1399. }
  1400. SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
  1401. int __user *, usockvec)
  1402. {
  1403. return __sys_socketpair(family, type, protocol, usockvec);
  1404. }
  1405. /*
  1406. * Bind a name to a socket. Nothing much to do here since it's
  1407. * the protocol's responsibility to handle the local address.
  1408. *
  1409. * We move the socket address to kernel space before we call
  1410. * the protocol layer (having also checked the address is ok).
  1411. */
  1412. int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
  1413. {
  1414. struct socket *sock;
  1415. struct sockaddr_storage address;
  1416. int err, fput_needed;
  1417. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1418. if (sock) {
  1419. err = move_addr_to_kernel(umyaddr, addrlen, &address);
  1420. if (!err) {
  1421. err = security_socket_bind(sock,
  1422. (struct sockaddr *)&address,
  1423. addrlen);
  1424. if (!err)
  1425. err = sock->ops->bind(sock,
  1426. (struct sockaddr *)
  1427. &address, addrlen);
  1428. }
  1429. fput_light(sock->file, fput_needed);
  1430. }
  1431. return err;
  1432. }
  1433. SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
  1434. {
  1435. return __sys_bind(fd, umyaddr, addrlen);
  1436. }
  1437. /*
  1438. * Perform a listen. Basically, we allow the protocol to do anything
  1439. * necessary for a listen, and if that works, we mark the socket as
  1440. * ready for listening.
  1441. */
  1442. int __sys_listen(int fd, int backlog)
  1443. {
  1444. struct socket *sock;
  1445. int err, fput_needed;
  1446. int somaxconn;
  1447. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1448. if (sock) {
  1449. somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
  1450. if ((unsigned int)backlog > somaxconn)
  1451. backlog = somaxconn;
  1452. err = security_socket_listen(sock, backlog);
  1453. if (!err)
  1454. err = sock->ops->listen(sock, backlog);
  1455. fput_light(sock->file, fput_needed);
  1456. }
  1457. return err;
  1458. }
  1459. SYSCALL_DEFINE2(listen, int, fd, int, backlog)
  1460. {
  1461. return __sys_listen(fd, backlog);
  1462. }
  1463. int __sys_accept4_file(struct file *file, unsigned file_flags,
  1464. struct sockaddr __user *upeer_sockaddr,
  1465. int __user *upeer_addrlen, int flags,
  1466. unsigned long nofile)
  1467. {
  1468. struct socket *sock, *newsock;
  1469. struct file *newfile;
  1470. int err, len, newfd;
  1471. struct sockaddr_storage address;
  1472. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1473. return -EINVAL;
  1474. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1475. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1476. sock = sock_from_file(file, &err);
  1477. if (!sock)
  1478. goto out;
  1479. err = -ENFILE;
  1480. newsock = sock_alloc();
  1481. if (!newsock)
  1482. goto out;
  1483. newsock->type = sock->type;
  1484. newsock->ops = sock->ops;
  1485. /*
  1486. * We don't need try_module_get here, as the listening socket (sock)
  1487. * has the protocol module (sock->ops->owner) held.
  1488. */
  1489. __module_get(newsock->ops->owner);
  1490. newfd = __get_unused_fd_flags(flags, nofile);
  1491. if (unlikely(newfd < 0)) {
  1492. err = newfd;
  1493. sock_release(newsock);
  1494. goto out;
  1495. }
  1496. newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
  1497. if (IS_ERR(newfile)) {
  1498. err = PTR_ERR(newfile);
  1499. put_unused_fd(newfd);
  1500. goto out;
  1501. }
  1502. err = security_socket_accept(sock, newsock);
  1503. if (err)
  1504. goto out_fd;
  1505. err = sock->ops->accept(sock, newsock, sock->file->f_flags | file_flags,
  1506. false);
  1507. if (err < 0)
  1508. goto out_fd;
  1509. if (upeer_sockaddr) {
  1510. len = newsock->ops->getname(newsock,
  1511. (struct sockaddr *)&address, 2);
  1512. if (len < 0) {
  1513. err = -ECONNABORTED;
  1514. goto out_fd;
  1515. }
  1516. err = move_addr_to_user(&address,
  1517. len, upeer_sockaddr, upeer_addrlen);
  1518. if (err < 0)
  1519. goto out_fd;
  1520. }
  1521. /* File flags are not inherited via accept() unlike another OSes. */
  1522. fd_install(newfd, newfile);
  1523. err = newfd;
  1524. out:
  1525. return err;
  1526. out_fd:
  1527. fput(newfile);
  1528. put_unused_fd(newfd);
  1529. goto out;
  1530. }
  1531. /*
  1532. * For accept, we attempt to create a new socket, set up the link
  1533. * with the client, wake up the client, then return the new
  1534. * connected fd. We collect the address of the connector in kernel
  1535. * space and move it to user at the very end. This is unclean because
  1536. * we open the socket then return an error.
  1537. *
  1538. * 1003.1g adds the ability to recvmsg() to query connection pending
  1539. * status to recvmsg. We need to add that support in a way thats
  1540. * clean when we restructure accept also.
  1541. */
  1542. int __sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr,
  1543. int __user *upeer_addrlen, int flags)
  1544. {
  1545. int ret = -EBADF;
  1546. struct fd f;
  1547. f = fdget(fd);
  1548. if (f.file) {
  1549. ret = __sys_accept4_file(f.file, 0, upeer_sockaddr,
  1550. upeer_addrlen, flags,
  1551. rlimit(RLIMIT_NOFILE));
  1552. if (f.flags)
  1553. fput(f.file);
  1554. }
  1555. return ret;
  1556. }
  1557. SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
  1558. int __user *, upeer_addrlen, int, flags)
  1559. {
  1560. return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, flags);
  1561. }
  1562. SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
  1563. int __user *, upeer_addrlen)
  1564. {
  1565. return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
  1566. }
  1567. /*
  1568. * Attempt to connect to a socket with the server address. The address
  1569. * is in user space so we verify it is OK and move it to kernel space.
  1570. *
  1571. * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
  1572. * break bindings
  1573. *
  1574. * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
  1575. * other SEQPACKET protocols that take time to connect() as it doesn't
  1576. * include the -EINPROGRESS status for such sockets.
  1577. */
  1578. int __sys_connect_file(struct file *file, struct sockaddr_storage *address,
  1579. int addrlen, int file_flags)
  1580. {
  1581. struct socket *sock;
  1582. int err;
  1583. sock = sock_from_file(file, &err);
  1584. if (!sock)
  1585. goto out;
  1586. err =
  1587. security_socket_connect(sock, (struct sockaddr *)address, addrlen);
  1588. if (err)
  1589. goto out;
  1590. err = sock->ops->connect(sock, (struct sockaddr *)address, addrlen,
  1591. sock->file->f_flags | file_flags);
  1592. out:
  1593. return err;
  1594. }
  1595. int __sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)
  1596. {
  1597. int ret = -EBADF;
  1598. struct fd f;
  1599. f = fdget(fd);
  1600. if (f.file) {
  1601. struct sockaddr_storage address;
  1602. ret = move_addr_to_kernel(uservaddr, addrlen, &address);
  1603. if (!ret)
  1604. ret = __sys_connect_file(f.file, &address, addrlen, 0);
  1605. if (f.flags)
  1606. fput(f.file);
  1607. }
  1608. return ret;
  1609. }
  1610. SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
  1611. int, addrlen)
  1612. {
  1613. return __sys_connect(fd, uservaddr, addrlen);
  1614. }
  1615. /*
  1616. * Get the local address ('name') of a socket object. Move the obtained
  1617. * name to user space.
  1618. */
  1619. int __sys_getsockname(int fd, struct sockaddr __user *usockaddr,
  1620. int __user *usockaddr_len)
  1621. {
  1622. struct socket *sock;
  1623. struct sockaddr_storage address;
  1624. int err, fput_needed;
  1625. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1626. if (!sock)
  1627. goto out;
  1628. err = security_socket_getsockname(sock);
  1629. if (err)
  1630. goto out_put;
  1631. err = sock->ops->getname(sock, (struct sockaddr *)&address, 0);
  1632. if (err < 0)
  1633. goto out_put;
  1634. /* "err" is actually length in this case */
  1635. err = move_addr_to_user(&address, err, usockaddr, usockaddr_len);
  1636. out_put:
  1637. fput_light(sock->file, fput_needed);
  1638. out:
  1639. return err;
  1640. }
  1641. SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
  1642. int __user *, usockaddr_len)
  1643. {
  1644. return __sys_getsockname(fd, usockaddr, usockaddr_len);
  1645. }
  1646. /*
  1647. * Get the remote address ('name') of a socket object. Move the obtained
  1648. * name to user space.
  1649. */
  1650. int __sys_getpeername(int fd, struct sockaddr __user *usockaddr,
  1651. int __user *usockaddr_len)
  1652. {
  1653. struct socket *sock;
  1654. struct sockaddr_storage address;
  1655. int err, fput_needed;
  1656. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1657. if (sock != NULL) {
  1658. err = security_socket_getpeername(sock);
  1659. if (err) {
  1660. fput_light(sock->file, fput_needed);
  1661. return err;
  1662. }
  1663. err = sock->ops->getname(sock, (struct sockaddr *)&address, 1);
  1664. if (err >= 0)
  1665. /* "err" is actually length in this case */
  1666. err = move_addr_to_user(&address, err, usockaddr,
  1667. usockaddr_len);
  1668. fput_light(sock->file, fput_needed);
  1669. }
  1670. return err;
  1671. }
  1672. SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
  1673. int __user *, usockaddr_len)
  1674. {
  1675. return __sys_getpeername(fd, usockaddr, usockaddr_len);
  1676. }
  1677. /*
  1678. * Send a datagram to a given address. We move the address into kernel
  1679. * space and check the user space data area is readable before invoking
  1680. * the protocol.
  1681. */
  1682. int __sys_sendto(int fd, void __user *buff, size_t len, unsigned int flags,
  1683. struct sockaddr __user *addr, in