PageRenderTime 70ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/net/socket.c

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