PageRenderTime 59ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/net/socket.c

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

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