PageRenderTime 66ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/net/socket.c

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

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