PageRenderTime 63ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/net/socket.c

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

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