PageRenderTime 121ms CodeModel.GetById 39ms RepoModel.GetById 1ms app.codeStats 0ms

/net/socket.c

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

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