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

/net/socket.c

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

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