PageRenderTime 175ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 1ms

/net/socket.c

https://bitbucket.org/teamblackout/vivo-blackout-edition
C | 3404 lines | 2442 code | 490 blank | 472 comment | 386 complexity | 0d0a4e9afa5e2a1f522d20c2d495f48e MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0

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

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

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