PageRenderTime 87ms CodeModel.GetById 46ms RepoModel.GetById 1ms app.codeStats 0ms

/net/socket.c

https://bitbucket.org/bradfa/linux
C | 3456 lines | 2500 code | 495 blank | 461 comment | 395 complexity | 4f7ba4b21274e7f1c301fe32993b3f84 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file