PageRenderTime 65ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/net/socket.c

https://bitbucket.org/zarboz/ville-upstream-test-branch
C | 3385 lines | 2431 code | 488 blank | 466 comment | 378 complexity | e32ca468343846396f70ce384d0d5075 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  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_pseudo(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. void sock_release(struct socket *sock)
  448. {
  449. if (sock->ops) {
  450. struct module *owner = sock->ops->owner;
  451. sock->ops->release(sock);
  452. sock->ops = NULL;
  453. module_put(owner);
  454. }
  455. if (rcu_dereference_protected(sock->wq, 1)->fasync_list)
  456. printk(KERN_ERR "sock_release: fasync list not empty!\n");
  457. percpu_sub(sockets_in_use, 1);
  458. if (!sock->file) {
  459. iput(SOCK_INODE(sock));
  460. return;
  461. }
  462. sock->file = NULL;
  463. }
  464. EXPORT_SYMBOL(sock_release);
  465. int sock_tx_timestamp(struct sock *sk, __u8 *tx_flags)
  466. {
  467. *tx_flags = 0;
  468. if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
  469. *tx_flags |= SKBTX_HW_TSTAMP;
  470. if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
  471. *tx_flags |= SKBTX_SW_TSTAMP;
  472. return 0;
  473. }
  474. EXPORT_SYMBOL(sock_tx_timestamp);
  475. static inline int __sock_sendmsg_nosec(struct kiocb *iocb, struct socket *sock,
  476. struct msghdr *msg, size_t size)
  477. {
  478. struct sock_iocb *si = kiocb_to_siocb(iocb);
  479. sock_update_classid(sock->sk);
  480. si->sock = sock;
  481. si->scm = NULL;
  482. si->msg = msg;
  483. si->size = size;
  484. return sock->ops->sendmsg(iocb, sock, msg, size);
  485. }
  486. static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
  487. struct msghdr *msg, size_t size)
  488. {
  489. int err = security_socket_sendmsg(sock, msg, size);
  490. return err ?: __sock_sendmsg_nosec(iocb, sock, msg, size);
  491. }
  492. int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
  493. {
  494. struct kiocb iocb;
  495. struct sock_iocb siocb;
  496. int ret;
  497. init_sync_kiocb(&iocb, NULL);
  498. iocb.private = &siocb;
  499. ret = __sock_sendmsg(&iocb, sock, msg, size);
  500. if (-EIOCBQUEUED == ret)
  501. ret = wait_on_sync_kiocb(&iocb);
  502. return ret;
  503. }
  504. EXPORT_SYMBOL(sock_sendmsg);
  505. static int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg, size_t size)
  506. {
  507. struct kiocb iocb;
  508. struct sock_iocb siocb;
  509. int ret;
  510. init_sync_kiocb(&iocb, NULL);
  511. iocb.private = &siocb;
  512. ret = __sock_sendmsg_nosec(&iocb, sock, msg, size);
  513. if (-EIOCBQUEUED == ret)
  514. ret = wait_on_sync_kiocb(&iocb);
  515. return ret;
  516. }
  517. int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
  518. struct kvec *vec, size_t num, size_t size)
  519. {
  520. mm_segment_t oldfs = get_fs();
  521. int result;
  522. set_fs(KERNEL_DS);
  523. /*
  524. * the following is safe, since for compiler definitions of kvec and
  525. * iovec are identical, yielding the same in-core layout and alignment
  526. */
  527. msg->msg_iov = (struct iovec *)vec;
  528. msg->msg_iovlen = num;
  529. result = sock_sendmsg(sock, msg, size);
  530. set_fs(oldfs);
  531. return result;
  532. }
  533. EXPORT_SYMBOL(kernel_sendmsg);
  534. static int ktime2ts(ktime_t kt, struct timespec *ts)
  535. {
  536. if (kt.tv64) {
  537. *ts = ktime_to_timespec(kt);
  538. return 1;
  539. } else {
  540. return 0;
  541. }
  542. }
  543. /*
  544. * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
  545. */
  546. void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
  547. struct sk_buff *skb)
  548. {
  549. int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
  550. struct timespec ts[3];
  551. int empty = 1;
  552. struct skb_shared_hwtstamps *shhwtstamps =
  553. skb_hwtstamps(skb);
  554. /* Race occurred between timestamp enabling and packet
  555. receiving. Fill in the current time for now. */
  556. if (need_software_tstamp && skb->tstamp.tv64 == 0)
  557. __net_timestamp(skb);
  558. if (need_software_tstamp) {
  559. if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
  560. struct timeval tv;
  561. skb_get_timestamp(skb, &tv);
  562. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
  563. sizeof(tv), &tv);
  564. } else {
  565. skb_get_timestampns(skb, &ts[0]);
  566. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
  567. sizeof(ts[0]), &ts[0]);
  568. }
  569. }
  570. memset(ts, 0, sizeof(ts));
  571. if (skb->tstamp.tv64 &&
  572. sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE)) {
  573. skb_get_timestampns(skb, ts + 0);
  574. empty = 0;
  575. }
  576. if (shhwtstamps) {
  577. if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
  578. ktime2ts(shhwtstamps->syststamp, ts + 1))
  579. empty = 0;
  580. if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
  581. ktime2ts(shhwtstamps->hwtstamp, ts + 2))
  582. empty = 0;
  583. }
  584. if (!empty)
  585. put_cmsg(msg, SOL_SOCKET,
  586. SCM_TIMESTAMPING, sizeof(ts), &ts);
  587. }
  588. EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
  589. static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
  590. struct sk_buff *skb)
  591. {
  592. if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && skb->dropcount)
  593. put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
  594. sizeof(__u32), &skb->dropcount);
  595. }
  596. void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
  597. struct sk_buff *skb)
  598. {
  599. sock_recv_timestamp(msg, sk, skb);
  600. sock_recv_drops(msg, sk, skb);
  601. }
  602. EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
  603. static inline int __sock_recvmsg_nosec(struct kiocb *iocb, struct socket *sock,
  604. struct msghdr *msg, size_t size, int flags)
  605. {
  606. struct sock_iocb *si = kiocb_to_siocb(iocb);
  607. sock_update_classid(sock->sk);
  608. si->sock = sock;
  609. si->scm = NULL;
  610. si->msg = msg;
  611. si->size = size;
  612. si->flags = flags;
  613. return sock->ops->recvmsg(iocb, sock, msg, size, flags);
  614. }
  615. static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
  616. struct msghdr *msg, size_t size, int flags)
  617. {
  618. int err = security_socket_recvmsg(sock, msg, size, flags);
  619. return err ?: __sock_recvmsg_nosec(iocb, sock, msg, size, flags);
  620. }
  621. int sock_recvmsg(struct socket *sock, struct msghdr *msg,
  622. size_t size, int flags)
  623. {
  624. struct kiocb iocb;
  625. struct sock_iocb siocb;
  626. int ret;
  627. init_sync_kiocb(&iocb, NULL);
  628. iocb.private = &siocb;
  629. ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
  630. if (-EIOCBQUEUED == ret)
  631. ret = wait_on_sync_kiocb(&iocb);
  632. return ret;
  633. }
  634. EXPORT_SYMBOL(sock_recvmsg);
  635. static int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
  636. size_t size, int flags)
  637. {
  638. struct kiocb iocb;
  639. struct sock_iocb siocb;
  640. int ret;
  641. init_sync_kiocb(&iocb, NULL);
  642. iocb.private = &siocb;
  643. ret = __sock_recvmsg_nosec(&iocb, sock, msg, size, flags);
  644. if (-EIOCBQUEUED == ret)
  645. ret = wait_on_sync_kiocb(&iocb);
  646. return ret;
  647. }
  648. /**
  649. * kernel_recvmsg - Receive a message from a socket (kernel space)
  650. * @sock: The socket to receive the message from
  651. * @msg: Received message
  652. * @vec: Input s/g array for message data
  653. * @num: Size of input s/g array
  654. * @size: Number of bytes to read
  655. * @flags: Message flags (MSG_DONTWAIT, etc...)
  656. *
  657. * On return the msg structure contains the scatter/gather array passed in the
  658. * vec argument. The array is modified so that it consists of the unfilled
  659. * portion of the original array.
  660. *
  661. * The returned value is the total number of bytes received, or an error.
  662. */
  663. int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
  664. struct kvec *vec, size_t num, size_t size, int flags)
  665. {
  666. mm_segment_t oldfs = get_fs();
  667. int result;
  668. set_fs(KERNEL_DS);
  669. /*
  670. * the following is safe, since for compiler definitions of kvec and
  671. * iovec are identical, yielding the same in-core layout and alignment
  672. */
  673. msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
  674. result = sock_recvmsg(sock, msg, size, flags);
  675. set_fs(oldfs);
  676. return result;
  677. }
  678. EXPORT_SYMBOL(kernel_recvmsg);
  679. static void sock_aio_dtor(struct kiocb *iocb)
  680. {
  681. kfree(iocb->private);
  682. }
  683. static ssize_t sock_sendpage(struct file *file, struct page *page,
  684. int offset, size_t size, loff_t *ppos, int more)
  685. {
  686. struct socket *sock;
  687. int flags;
  688. sock = file->private_data;
  689. flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  690. /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
  691. flags |= more;
  692. return kernel_sendpage(sock, page, offset, size, flags);
  693. }
  694. static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
  695. struct pipe_inode_info *pipe, size_t len,
  696. unsigned int flags)
  697. {
  698. struct socket *sock = file->private_data;
  699. if (unlikely(!sock->ops->splice_read))
  700. return -EINVAL;
  701. sock_update_classid(sock->sk);
  702. return sock->ops->splice_read(sock, ppos, pipe, len, flags);
  703. }
  704. static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
  705. struct sock_iocb *siocb)
  706. {
  707. if (!is_sync_kiocb(iocb)) {
  708. siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
  709. if (!siocb)
  710. return NULL;
  711. iocb->ki_dtor = sock_aio_dtor;
  712. }
  713. siocb->kiocb = iocb;
  714. iocb->private = siocb;
  715. return siocb;
  716. }
  717. static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
  718. struct file *file, const struct iovec *iov,
  719. unsigned long nr_segs)
  720. {
  721. struct socket *sock = file->private_data;
  722. size_t size = 0;
  723. int i;
  724. for (i = 0; i < nr_segs; i++)
  725. size += iov[i].iov_len;
  726. msg->msg_name = NULL;
  727. msg->msg_namelen = 0;
  728. msg->msg_control = NULL;
  729. msg->msg_controllen = 0;
  730. msg->msg_iov = (struct iovec *)iov;
  731. msg->msg_iovlen = nr_segs;
  732. msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  733. return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
  734. }
  735. static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
  736. unsigned long nr_segs, loff_t pos)
  737. {
  738. struct sock_iocb siocb, *x;
  739. if (pos != 0)
  740. return -ESPIPE;
  741. if (iocb->ki_left == 0) /* Match SYS5 behaviour */
  742. return 0;
  743. x = alloc_sock_iocb(iocb, &siocb);
  744. if (!x)
  745. return -ENOMEM;
  746. return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
  747. }
  748. static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
  749. struct file *file, const struct iovec *iov,
  750. unsigned long nr_segs)
  751. {
  752. struct socket *sock = file->private_data;
  753. size_t size = 0;
  754. int i;
  755. for (i = 0; i < nr_segs; i++)
  756. size += iov[i].iov_len;
  757. msg->msg_name = NULL;
  758. msg->msg_namelen = 0;
  759. msg->msg_control = NULL;
  760. msg->msg_controllen = 0;
  761. msg->msg_iov = (struct iovec *)iov;
  762. msg->msg_iovlen = nr_segs;
  763. msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  764. if (sock->type == SOCK_SEQPACKET)
  765. msg->msg_flags |= MSG_EOR;
  766. return __sock_sendmsg(iocb, sock, msg, size);
  767. }
  768. static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
  769. unsigned long nr_segs, loff_t pos)
  770. {
  771. struct sock_iocb siocb, *x;
  772. if (pos != 0)
  773. return -ESPIPE;
  774. x = alloc_sock_iocb(iocb, &siocb);
  775. if (!x)
  776. return -ENOMEM;
  777. return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
  778. }
  779. /*
  780. * Atomic setting of ioctl hooks to avoid race
  781. * with module unload.
  782. */
  783. static DEFINE_MUTEX(br_ioctl_mutex);
  784. static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
  785. void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
  786. {
  787. mutex_lock(&br_ioctl_mutex);
  788. br_ioctl_hook = hook;
  789. mutex_unlock(&br_ioctl_mutex);
  790. }
  791. EXPORT_SYMBOL(brioctl_set);
  792. static DEFINE_MUTEX(vlan_ioctl_mutex);
  793. static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
  794. void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
  795. {
  796. mutex_lock(&vlan_ioctl_mutex);
  797. vlan_ioctl_hook = hook;
  798. mutex_unlock(&vlan_ioctl_mutex);
  799. }
  800. EXPORT_SYMBOL(vlan_ioctl_set);
  801. static DEFINE_MUTEX(dlci_ioctl_mutex);
  802. static int (*dlci_ioctl_hook) (unsigned int, void __user *);
  803. void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
  804. {
  805. mutex_lock(&dlci_ioctl_mutex);
  806. dlci_ioctl_hook = hook;
  807. mutex_unlock(&dlci_ioctl_mutex);
  808. }
  809. EXPORT_SYMBOL(dlci_ioctl_set);
  810. static long sock_do_ioctl(struct net *net, struct socket *sock,
  811. unsigned int cmd, unsigned long arg)
  812. {
  813. int err;
  814. void __user *argp = (void __user *)arg;
  815. err = sock->ops->ioctl(sock, cmd, arg);
  816. /*
  817. * If this ioctl is unknown try to hand it down
  818. * to the NIC driver.
  819. */
  820. if (err == -ENOIOCTLCMD)
  821. err = dev_ioctl(net, cmd, argp);
  822. return err;
  823. }
  824. /*
  825. * With an ioctl, arg may well be a user mode pointer, but we don't know
  826. * what to do with it - that's up to the protocol still.
  827. */
  828. static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  829. {
  830. struct socket *sock;
  831. struct sock *sk;
  832. void __user *argp = (void __user *)arg;
  833. int pid, err;
  834. struct net *net;
  835. sock = file->private_data;
  836. sk = sock->sk;
  837. net = sock_net(sk);
  838. if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
  839. err = dev_ioctl(net, cmd, argp);
  840. } else
  841. #ifdef CONFIG_WEXT_CORE
  842. if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
  843. err = dev_ioctl(net, cmd, argp);
  844. } else
  845. #endif
  846. switch (cmd) {
  847. case FIOSETOWN:
  848. case SIOCSPGRP:
  849. err = -EFAULT;
  850. if (get_user(pid, (int __user *)argp))
  851. break;
  852. err = f_setown(sock->file, pid, 1);
  853. break;
  854. case FIOGETOWN:
  855. case SIOCGPGRP:
  856. err = put_user(f_getown(sock->file),
  857. (int __user *)argp);
  858. break;
  859. case SIOCGIFBR:
  860. case SIOCSIFBR:
  861. case SIOCBRADDBR:
  862. case SIOCBRDELBR:
  863. err = -ENOPKG;
  864. if (!br_ioctl_hook)
  865. request_module("bridge");
  866. mutex_lock(&br_ioctl_mutex);
  867. if (br_ioctl_hook)
  868. err = br_ioctl_hook(net, cmd, argp);
  869. mutex_unlock(&br_ioctl_mutex);
  870. break;
  871. case SIOCGIFVLAN:
  872. case SIOCSIFVLAN:
  873. err = -ENOPKG;
  874. if (!vlan_ioctl_hook)
  875. request_module("8021q");
  876. mutex_lock(&vlan_ioctl_mutex);
  877. if (vlan_ioctl_hook)
  878. err = vlan_ioctl_hook(net, argp);
  879. mutex_unlock(&vlan_ioctl_mutex);
  880. break;
  881. case SIOCADDDLCI:
  882. case SIOCDELDLCI:
  883. err = -ENOPKG;
  884. if (!dlci_ioctl_hook)
  885. request_module("dlci");
  886. mutex_lock(&dlci_ioctl_mutex);
  887. if (dlci_ioctl_hook)
  888. err = dlci_ioctl_hook(cmd, argp);
  889. mutex_unlock(&dlci_ioctl_mutex);
  890. break;
  891. default:
  892. err = sock_do_ioctl(net, sock, cmd, arg);
  893. break;
  894. }
  895. return err;
  896. }
  897. int sock_create_lite(int family, int type, int protocol, struct socket **res)
  898. {
  899. int err;
  900. struct socket *sock = NULL;
  901. err = security_socket_create(family, type, protocol, 1);
  902. if (err)
  903. goto out;
  904. sock = sock_alloc();
  905. if (!sock) {
  906. err = -ENOMEM;
  907. goto out;
  908. }
  909. sock->type = type;
  910. err = security_socket_post_create(sock, family, type, protocol, 1);
  911. if (err)
  912. goto out_release;
  913. out:
  914. *res = sock;
  915. return err;
  916. out_release:
  917. sock_release(sock);
  918. sock = NULL;
  919. goto out;
  920. }
  921. EXPORT_SYMBOL(sock_create_lite);
  922. /* No kernel lock held - perfect */
  923. static unsigned int sock_poll(struct file *file, poll_table *wait)
  924. {
  925. struct socket *sock;
  926. /*
  927. * We can't return errors to poll, so it's either yes or no.
  928. */
  929. sock = file->private_data;
  930. return sock->ops->poll(file, sock, wait);
  931. }
  932. static int sock_mmap(struct file *file, struct vm_area_struct *vma)
  933. {
  934. struct socket *sock = file->private_data;
  935. return sock->ops->mmap(file, sock, vma);
  936. }
  937. static int sock_close(struct inode *inode, struct file *filp)
  938. {
  939. /*
  940. * It was possible the inode is NULL we were
  941. * closing an unfinished socket.
  942. */
  943. if (!inode) {
  944. printk(KERN_DEBUG "sock_close: NULL inode\n");
  945. return 0;
  946. }
  947. sock_release(SOCKET_I(inode));
  948. return 0;
  949. }
  950. /*
  951. * Update the socket async list
  952. *
  953. * Fasync_list locking strategy.
  954. *
  955. * 1. fasync_list is modified only under process context socket lock
  956. * i.e. under semaphore.
  957. * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
  958. * or under socket lock
  959. */
  960. static int sock_fasync(int fd, struct file *filp, int on)
  961. {
  962. struct socket *sock = filp->private_data;
  963. struct sock *sk = sock->sk;
  964. struct socket_wq *wq;
  965. if (sk == NULL)
  966. return -EINVAL;
  967. lock_sock(sk);
  968. wq = rcu_dereference_protected(sock->wq, sock_owned_by_user(sk));
  969. fasync_helper(fd, filp, on, &wq->fasync_list);
  970. if (!wq->fasync_list)
  971. sock_reset_flag(sk, SOCK_FASYNC);
  972. else
  973. sock_set_flag(sk, SOCK_FASYNC);
  974. release_sock(sk);
  975. return 0;
  976. }
  977. /* This function may be called only under socket lock or callback_lock or rcu_lock */
  978. int sock_wake_async(struct socket *sock, int how, int band)
  979. {
  980. struct socket_wq *wq;
  981. if (!sock)
  982. return -1;
  983. rcu_read_lock();
  984. wq = rcu_dereference(sock->wq);
  985. if (!wq || !wq->fasync_list) {
  986. rcu_read_unlock();
  987. return -1;
  988. }
  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(&wq->fasync_list, SIGIO, band);
  1001. break;
  1002. case SOCK_WAKE_URG:
  1003. kill_fasync(&wq->fasync_list, SIGURG, band);
  1004. }
  1005. rcu_read_unlock();
  1006. return 0;
  1007. }
  1008. EXPORT_SYMBOL(sock_wake_async);
  1009. int __sock_create(struct net *net, int family, int type, int protocol,
  1010. struct socket **res, int kern)
  1011. {
  1012. int err;
  1013. struct socket *sock;
  1014. const struct net_proto_family *pf;
  1015. /*
  1016. * Check protocol is in range
  1017. */
  1018. if (family < 0 || family >= NPROTO)
  1019. return -EAFNOSUPPORT;
  1020. if (type < 0 || type >= SOCK_MAX)
  1021. return -EINVAL;
  1022. /* Compatibility.
  1023. This uglymoron is moved from INET layer to here to avoid
  1024. deadlock in module load.
  1025. */
  1026. if (family == PF_INET && type == SOCK_PACKET) {
  1027. static int warned;
  1028. if (!warned) {
  1029. warned = 1;
  1030. printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
  1031. current->comm);
  1032. }
  1033. family = PF_PACKET;
  1034. }
  1035. err = security_socket_create(family, type, protocol, kern);
  1036. if (err)
  1037. return err;
  1038. /*
  1039. * Allocate the socket and allow the family to set things up. if
  1040. * the protocol is 0, the family is instructed to select an appropriate
  1041. * default.
  1042. */
  1043. sock = sock_alloc();
  1044. if (!sock) {
  1045. if (net_ratelimit())
  1046. printk(KERN_WARNING "socket: no more sockets\n");
  1047. return -ENFILE; /* Not exactly a match, but its the
  1048. closest posix thing */
  1049. }
  1050. sock->type = type;
  1051. #ifdef CONFIG_MODULES
  1052. /* Attempt to load a protocol module if the find failed.
  1053. *
  1054. * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
  1055. * requested real, full-featured networking support upon configuration.
  1056. * Otherwise module support will break!
  1057. */
  1058. if (rcu_access_pointer(net_families[family]) == NULL)
  1059. request_module("net-pf-%d", family);
  1060. #endif
  1061. rcu_read_lock();
  1062. pf = rcu_dereference(net_families[family]);
  1063. err = -EAFNOSUPPORT;
  1064. if (!pf)
  1065. goto out_release;
  1066. /*
  1067. * We will call the ->create function, that possibly is in a loadable
  1068. * module, so we have to bump that loadable module refcnt first.
  1069. */
  1070. if (!try_module_get(pf->owner))
  1071. goto out_release;
  1072. /* Now protected by module ref count */
  1073. rcu_read_unlock();
  1074. err = pf->create(net, sock, protocol, kern);
  1075. if (err < 0)
  1076. goto out_module_put;
  1077. /*
  1078. * Now to bump the refcnt of the [loadable] module that owns this
  1079. * socket at sock_release time we decrement its refcnt.
  1080. */
  1081. if (!try_module_get(sock->ops->owner))
  1082. goto out_module_busy;
  1083. /*
  1084. * Now that we're done with the ->create function, the [loadable]
  1085. * module can have its refcnt decremented
  1086. */
  1087. module_put(pf->owner);
  1088. err = security_socket_post_create(sock, family, type, protocol, kern);
  1089. if (err)
  1090. goto out_sock_release;
  1091. *res = sock;
  1092. return 0;
  1093. out_module_busy:
  1094. err = -EAFNOSUPPORT;
  1095. out_module_put:
  1096. sock->ops = NULL;
  1097. module_put(pf->owner);
  1098. out_sock_release:
  1099. sock_release(sock);
  1100. return err;
  1101. out_release:
  1102. rcu_read_unlock();
  1103. goto out_sock_release;
  1104. }
  1105. EXPORT_SYMBOL(__sock_create);
  1106. int sock_create(int family, int type, int protocol, struct socket **res)
  1107. {
  1108. return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
  1109. }
  1110. EXPORT_SYMBOL(sock_create);
  1111. int sock_create_kern(int family, int type, int protocol, struct socket **res)
  1112. {
  1113. return __sock_create(&init_net, family, type, protocol, res, 1);
  1114. }
  1115. EXPORT_SYMBOL(sock_create_kern);
  1116. SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
  1117. {
  1118. int retval;
  1119. struct socket *sock;
  1120. int flags;
  1121. /* Check the SOCK_* constants for consistency. */
  1122. BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
  1123. BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
  1124. BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
  1125. BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
  1126. flags = type & ~SOCK_TYPE_MASK;
  1127. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1128. return -EINVAL;
  1129. type &= SOCK_TYPE_MASK;
  1130. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1131. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1132. retval = sock_create(family, type, protocol, &sock);
  1133. if (retval < 0)
  1134. goto out;
  1135. retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
  1136. if (retval < 0)
  1137. goto out_release;
  1138. out:
  1139. /* It may be already another descriptor 8) Not kernel problem. */
  1140. return retval;
  1141. out_release:
  1142. sock_release(sock);
  1143. return retval;
  1144. }
  1145. /*
  1146. * Create a pair of connected sockets.
  1147. */
  1148. SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
  1149. int __user *, usockvec)
  1150. {
  1151. struct socket *sock1, *sock2;
  1152. int fd1, fd2, err;
  1153. struct file *newfile1, *newfile2;
  1154. int flags;
  1155. flags = type & ~SOCK_TYPE_MASK;
  1156. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1157. return -EINVAL;
  1158. type &= SOCK_TYPE_MASK;
  1159. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1160. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1161. /*
  1162. * Obtain the first socket and check if the underlying protocol
  1163. * supports the socketpair call.
  1164. */
  1165. err = sock_create(family, type, protocol, &sock1);
  1166. if (err < 0)
  1167. goto out;
  1168. err = sock_create(family, type, protocol, &sock2);
  1169. if (err < 0)
  1170. goto out_release_1;
  1171. err = sock1->ops->socketpair(sock1, sock2);
  1172. if (err < 0)
  1173. goto out_release_both;
  1174. fd1 = sock_alloc_file(sock1, &newfile1, flags);
  1175. if (unlikely(fd1 < 0)) {
  1176. err = fd1;
  1177. goto out_release_both;
  1178. }
  1179. fd2 = sock_alloc_file(sock2, &newfile2, flags);
  1180. if (unlikely(fd2 < 0)) {
  1181. err = fd2;
  1182. fput(newfile1);
  1183. put_unused_fd(fd1);
  1184. sock_release(sock2);
  1185. goto out;
  1186. }
  1187. audit_fd_pair(fd1, fd2);
  1188. fd_install(fd1, newfile1);
  1189. fd_install(fd2, newfile2);
  1190. /* fd1 and fd2 may be already another descriptors.
  1191. * Not kernel problem.
  1192. */
  1193. err = put_user(fd1, &usockvec[0]);
  1194. if (!err)
  1195. err = put_user(fd2, &usockvec[1]);
  1196. if (!err)
  1197. return 0;
  1198. sys_close(fd2);
  1199. sys_close(fd1);
  1200. return err;
  1201. out_release_both:
  1202. sock_release(sock2);
  1203. out_release_1:
  1204. sock_release(sock1);
  1205. out:
  1206. return err;
  1207. }
  1208. /*
  1209. * Bind a name to a socket. Nothing much to do here since it's
  1210. * the protocol's responsibility to handle the local address.
  1211. *
  1212. * We move the socket address to kernel space before we call
  1213. * the protocol layer (having also checked the address is ok).
  1214. */
  1215. SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
  1216. {
  1217. struct socket *sock;
  1218. struct sockaddr_storage address;
  1219. int err, fput_needed;
  1220. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1221. if (sock) {
  1222. err = move_addr_to_kernel(umyaddr, addrlen, (struct sockaddr *)&address);
  1223. if (err >= 0) {
  1224. err = security_socket_bind(sock,
  1225. (struct sockaddr *)&address,
  1226. addrlen);
  1227. if (!err)
  1228. err = sock->ops->bind(sock,
  1229. (struct sockaddr *)
  1230. &address, addrlen);
  1231. }
  1232. fput_light(sock->file, fput_needed);
  1233. }
  1234. return err;
  1235. }
  1236. /*
  1237. * Perform a listen. Basically, we allow the protocol to do anything
  1238. * necessary for a listen, and if that works, we mark the socket as
  1239. * ready for listening.
  1240. */
  1241. SYSCALL_DEFINE2(listen, int, fd, int, backlog)
  1242. {
  1243. struct socket *sock;
  1244. int err, fput_needed;
  1245. int somaxconn;
  1246. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1247. if (sock) {
  1248. somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
  1249. if ((unsigned)backlog > somaxconn)
  1250. backlog = somaxconn;
  1251. err = security_socket_listen(sock, backlog);
  1252. if (!err)
  1253. err = sock->ops->listen(sock, backlog);
  1254. fput_light(sock->file, fput_needed);
  1255. }
  1256. return err;
  1257. }
  1258. /*
  1259. * For accept, we attempt to create a new socket, set up the link
  1260. * with the client, wake up the client, then return the new
  1261. * connected fd. We collect the address of the connector in kernel
  1262. * space and move it to user at the very end. This is unclean because
  1263. * we open the socket then return an error.
  1264. *
  1265. * 1003.1g adds the ability to recvmsg() to query connection pending
  1266. * status to recvmsg. We need to add that support in a way thats
  1267. * clean when we restucture accept also.
  1268. */
  1269. SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
  1270. int __user *, upeer_addrlen, int, flags)
  1271. {
  1272. struct socket *sock, *newsock;
  1273. struct file *newfile;
  1274. int err, len, newfd, fput_needed;
  1275. struct sockaddr_storage address;
  1276. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1277. return -EINVAL;
  1278. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1279. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1280. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1281. if (!sock)
  1282. goto out;
  1283. err = -ENFILE;
  1284. newsock = sock_alloc();
  1285. if (!newsock)
  1286. goto out_put;
  1287. newsock->type = sock->type;
  1288. newsock->ops = sock->ops;
  1289. /*
  1290. * We don't need try_module_get here, as the listening socket (sock)
  1291. * has the protocol module (sock->ops->owner) held.
  1292. */
  1293. __module_get(newsock->ops->owner);
  1294. newfd = sock_alloc_file(newsock, &newfile, flags);
  1295. if (unlikely(newfd < 0)) {
  1296. err = newfd;
  1297. sock_release(newsock);
  1298. goto out_put;
  1299. }
  1300. err = security_socket_accept(sock, newsock);
  1301. if (err)
  1302. goto out_fd;
  1303. err = sock->ops->accept(sock, newsock, sock->file->f_flags);
  1304. if (err < 0)
  1305. goto out_fd;
  1306. if (upeer_sockaddr) {
  1307. if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
  1308. &len, 2) < 0) {
  1309. err = -ECONNABORTED;
  1310. goto out_fd;
  1311. }
  1312. err = move_addr_to_user((struct sockaddr *)&address,
  1313. len, upeer_sockaddr, upeer_addrlen);
  1314. if (err < 0)
  1315. goto out_fd;
  1316. }
  1317. /* File flags are not inherited via accept() unlike another OSes. */
  1318. fd_install(newfd, newfile);
  1319. err = newfd;
  1320. out_put:
  1321. fput_light(sock->file, fput_needed);
  1322. out:
  1323. return err;
  1324. out_fd:
  1325. fput(newfile);
  1326. put_unused_fd(newfd);
  1327. goto out_put;
  1328. }
  1329. SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
  1330. int __user *, upeer_addrlen)
  1331. {
  1332. return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
  1333. }
  1334. /*
  1335. * Attempt to connect to a socket with the server address. The address
  1336. * is in user space so we verify it is OK and move it to kernel space.
  1337. *
  1338. * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
  1339. * break bindings
  1340. *
  1341. * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
  1342. * other SEQPACKET protocols that take time to connect() as it doesn't
  1343. * include the -EINPROGRESS status for such sockets.
  1344. */
  1345. SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
  1346. int, addrlen)
  1347. {
  1348. struct socket *sock;
  1349. struct sockaddr_storage address;
  1350. int err, fput_needed;
  1351. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1352. if (!sock)
  1353. goto out;
  1354. err = move_addr_to_kernel(uservaddr, addrlen, (struct sockaddr *)&address);
  1355. if (err < 0)
  1356. goto out_put;
  1357. err =
  1358. security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
  1359. if (err)
  1360. goto out_put;
  1361. err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
  1362. sock->file->f_flags);
  1363. out_put:
  1364. fput_light(sock->file, fput_needed);
  1365. out:
  1366. return err;
  1367. }
  1368. /*
  1369. * Get the local address ('name') of a socket object. Move the obtained
  1370. * name to user space.
  1371. */
  1372. SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
  1373. int __user *, usockaddr_len)
  1374. {
  1375. struct socket *sock;
  1376. struct sockaddr_storage address;
  1377. int len, err, fput_needed;
  1378. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1379. if (!sock)
  1380. goto out;
  1381. err = security_socket_getsockname(sock);
  1382. if (err)
  1383. goto out_put;
  1384. err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
  1385. if (err)
  1386. goto out_put;
  1387. err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr, usockaddr_len);
  1388. out_put:
  1389. fput_light(sock->file, fput_needed);
  1390. out:
  1391. return err;
  1392. }
  1393. /*
  1394. * Get the remote address ('name') of a socket object. Move the obtained
  1395. * name to user space.
  1396. */
  1397. SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
  1398. int __user *, usockaddr_len)
  1399. {
  1400. struct socket *sock;
  1401. struct sockaddr_storage address;
  1402. int len, err, fput_needed;
  1403. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1404. if (sock != NULL) {
  1405. err = security_socket_getpeername(sock);
  1406. if (err) {
  1407. fput_light(sock->file, fput_needed);
  1408. return err;
  1409. }
  1410. err =
  1411. sock->ops->getname(sock, (struct sockaddr *)&address, &len,
  1412. 1);
  1413. if (!err)
  1414. err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr,
  1415. usockaddr_len);
  1416. fput_light(sock->file, fput_needed);
  1417. }
  1418. return err;
  1419. }
  1420. /*
  1421. * Send a datagram to a given address. We move the address into kernel
  1422. * space and check the user space data area is readable before invoking
  1423. * the protocol.
  1424. */
  1425. SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
  1426. unsigned, flags, struct sockaddr __user *, addr,
  1427. int, addr_len)
  1428. {
  1429. struct socket *sock;
  1430. struct sockaddr_storage address;
  1431. int err;
  1432. struct msghdr msg;
  1433. struct iovec iov;
  1434. int fput_needed;
  1435. if (len > INT_MAX)
  1436. len = INT_MAX;
  1437. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1438. if (!sock)
  1439. goto out;
  1440. iov.iov_base = buff;
  1441. iov.iov_len = len;
  1442. msg.msg_name = NULL;
  1443. msg.msg_iov = &iov;
  1444. msg.msg_iovlen = 1;
  1445. msg.msg_control = NULL;
  1446. msg.msg_controllen = 0;
  1447. msg.msg_namelen = 0;
  1448. if (addr) {
  1449. err = move_addr_to_kernel(addr, addr_len, (struct sockaddr *)&address);
  1450. if (err < 0)
  1451. goto out_put;
  1452. msg.msg_name = (struct sockaddr *)&address;
  1453. msg.msg_namelen = addr_len;
  1454. }
  1455. if (sock->file->f_flags & O_NONBLOCK)
  1456. flags |= MSG_DONTWAIT;
  1457. msg.msg_flags = flags;
  1458. err = sock_sendmsg(sock, &msg, len);
  1459. out_put:
  1460. fput_light(sock->file, fput_needed);
  1461. out:
  1462. return err;
  1463. }
  1464. /*
  1465. * Send a datagram down a socket.
  1466. */
  1467. SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
  1468. unsigned, flags)
  1469. {
  1470. return sys_sendto(fd, buff, len, flags, NULL, 0);
  1471. }
  1472. /*
  1473. * Receive a frame from the socket and optionally record the address of the
  1474. * sender. We verify the buffers are writable and if needed move the
  1475. * sender address from kernel to user space.
  1476. */
  1477. SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
  1478. unsigned, flags, struct sockaddr __user *, addr,
  1479. int __user *, addr_len)
  1480. {
  1481. struct socket *sock;
  1482. struct iovec iov;
  1483. struct msghdr msg;
  1484. struct sockaddr_storage address;
  1485. int err, err2;
  1486. int fput_needed;
  1487. if (size > INT_MAX)
  1488. size = INT_MAX;
  1489. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1490. if (!sock)
  1491. goto out;
  1492. msg.msg_control = NULL;
  1493. msg.msg_controllen = 0;
  1494. msg.msg_iovlen = 1;
  1495. msg.msg_iov = &iov;
  1496. iov.iov_len = size;
  1497. iov.iov_base = ubuf;
  1498. msg.msg_name = (struct sockaddr *)&address;
  1499. msg.msg_namelen = sizeof(address);
  1500. if (sock->file->f_flags & O_NONBLOCK)
  1501. flags |= MSG_DONTWAIT;
  1502. err = sock_recvmsg(sock, &msg, size, flags);
  1503. if (err >= 0 && addr != NULL) {
  1504. err2 = move_addr_to_user((struct sockaddr *)&address,
  1505. msg.msg_namelen, addr, addr_len);
  1506. if (err2 < 0)
  1507. err = err2;
  1508. }
  1509. fput_light(sock->file, fput_needed);
  1510. out:
  1511. return err;
  1512. }
  1513. /*
  1514. * Receive a datagram from a socket.
  1515. */
  1516. asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
  1517. unsigned flags)
  1518. {
  1519. return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
  1520. }
  1521. /*
  1522. * Set a socket option. Because we don't know the option lengths we have
  1523. * to pass the user mode parameter for the protocols to sort out.
  1524. */
  1525. SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
  1526. char __user *, optval, int, optlen)
  1527. {
  1528. int err, fput_needed;
  1529. struct socket *sock;
  1530. if (optlen < 0)
  1531. return -EINVAL;
  1532. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1533. if (sock != NULL) {
  1534. err = security_socket_setsockopt(sock, level, optname);
  1535. if (err)
  1536. goto out_put;
  1537. if (level == SOL_SOCKET)
  1538. err =
  1539. sock_setsockopt(sock, level, optname, optval,
  1540. optlen);
  1541. else
  1542. err =
  1543. sock->ops->setsockopt(sock, level, optname, optval,
  1544. optlen);
  1545. out_put:
  1546. fput_light(sock->file, fput_needed);
  1547. }
  1548. return err;
  1549. }
  1550. /*
  1551. * Get a socket option. Because we don't know the option lengths we have
  1552. * to pass a user mode parameter for the protocols to sort out.
  1553. */
  1554. SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
  1555. char __user *, optval, int __user *, optlen)
  1556. {
  1557. int err, fput_needed;
  1558. struct socket *sock;
  1559. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1560. if (sock != NULL) {
  1561. err = security_socket_getsockopt(sock, level, optname);
  1562. if (err)
  1563. goto out_put;
  1564. if (level == SOL_SOCKET)
  1565. err =
  1566. sock_getsockopt(sock, level, optname, optval,
  1567. optlen);
  1568. else
  1569. err =
  1570. sock->ops->getsockopt(sock, level, optname, optval,
  1571. optlen);
  1572. out_put:
  1573. fput_light(sock->file, fput_needed);
  1574. }
  1575. return err;
  1576. }
  1577. /*
  1578. * Shutdown a socket.
  1579. */
  1580. SYSCALL_DEFINE2(shutdown, int, fd, int, how)
  1581. {
  1582. int err, fput_needed;
  1583. struct socket *sock;
  1584. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1585. if (sock != NULL) {
  1586. err = security_socket_shutdown(sock, how);
  1587. if (!err)
  1588. err = sock->ops->shutdown(sock, how);
  1589. fput_light(sock->file, fput_needed);
  1590. }
  1591. return err;
  1592. }
  1593. /* A couple of helpful macros for getting the address of the 32/64 bit
  1594. * fields which are the same type (int / unsigned) on our platforms.
  1595. */
  1596. #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
  1597. #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
  1598. #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
  1599. struct used_address {
  1600. struct sockaddr_storage name;
  1601. unsigned int name_len;
  1602. };
  1603. static int __sys_sendmsg(struct socket *sock, struct msghdr __user *msg,
  1604. struct msghdr *msg_sys, unsigned flags,
  1605. struct used_address *used_address)
  1606. {
  1607. struct compat_msghdr __user *msg_compat =
  1608. (struct compat_msghdr __user *)msg;
  1609. struct sockaddr_storage address;
  1610. struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
  1611. unsigned char ctl[sizeof(struct cmsghdr) + 20]
  1612. __attribute__ ((aligned(sizeof(__kernel_size_t))));
  1613. /* 20 is size of ipv6_pktinfo */
  1614. unsigned char *ctl_buf = ctl;
  1615. int err, ctl_len, iov_size, total_len;
  1616. err = -EFAULT;
  1617. if (MSG_CMSG_COMPAT & flags) {
  1618. if (get_compat_msghdr(msg_sys, msg_compat))
  1619. return -EFAULT;
  1620. } else if (copy_from_user(msg_sys, msg, sizeof(struct msghdr)))
  1621. return -EFAULT;
  1622. /* do not move before msg_sys is valid */
  1623. err = -EMSGSIZE;
  1624. if (msg_sys->msg_iovlen > UIO_MAXIOV)
  1625. goto out;
  1626. /* Check whether to allocate the iovec area */
  1627. err = -ENOMEM;
  1628. iov_size = msg_sys->msg_iovlen * sizeof(struct iovec);
  1629. if (msg_sys->msg_iovlen > UIO_FASTIOV) {
  1630. iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
  1631. if (!iov)
  1632. goto out;
  1633. }
  1634. /* This will also move the address data into kernel space */
  1635. if (MSG_CMSG_COMPAT & flags) {
  1636. err = verify_compat_iovec(msg_sys, iov,
  1637. (struct sockaddr *)&address,
  1638. VERIFY_READ);
  1639. } else
  1640. err = verify_iovec(msg_sys, iov,
  1641. (struct sockaddr *)&address,
  1642. VERIFY_READ);
  1643. if (err < 0)
  1644. goto out_freeiov;
  1645. total_len = err;
  1646. err = -ENOBUFS;
  1647. if (msg_sys->msg_controllen > INT_MAX)
  1648. goto out_freeiov;
  1649. ctl_len = msg_sys->msg_controllen;
  1650. if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
  1651. err =
  1652. cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
  1653. sizeof(ctl));
  1654. if (err)
  1655. goto out_freeiov;
  1656. ctl_buf = msg_sys->msg_control;
  1657. ctl_len = msg_sys->msg_controllen;
  1658. } else if (ctl_len) {
  1659. if (ctl_len > sizeof(ctl)) {
  1660. ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
  1661. if (ctl_buf == NULL)
  1662. goto out_freeiov;
  1663. }
  1664. err = -EFAULT;
  1665. /*
  1666. * Careful! Before this, msg_sys->msg_control contains a user pointer.
  1667. * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
  1668. * checking falls down on this.
  1669. */
  1670. if (copy_from_user(ctl_buf,
  1671. (void __user __force *)msg_sys->msg_control,
  1672. ctl_len))
  1673. goto out_freectl;
  1674. msg_sys->msg_control = ctl_buf;
  1675. }
  1676. msg_sys->msg_flags = flags;
  1677. if (sock->file->f_flags & O_NONBLOCK)
  1678. msg_sys->msg_flags |= MSG_DONTWAIT;
  1679. /*
  1680. * If this is sendmmsg() and current destination address is same as
  1681. * previously succeeded address, omit asking LSM's decision.
  1682. * used_address->name_len is initialized to UINT_MAX so that the first
  1683. * destination address never matches.
  1684. */
  1685. if (used_address && msg_sys->msg_name &&
  1686. used_address->name_len == msg_sys->msg_namelen &&
  1687. !memcmp(&used_address->name, msg_sys->msg_name,
  1688. used_address->name_len)) {
  1689. err = sock_sendmsg_nosec(sock, msg_sys, total_len);
  1690. goto out_freectl;
  1691. }
  1692. err = sock_sendmsg(sock, msg_sys, total_len);
  1693. /*
  1694. * If this is sendmmsg() and sending to current destination address was
  1695. * successful, remember it.
  1696. */
  1697. if (used_address && err >= 0) {
  1698. used_address->name_len = msg_sys->msg_namelen;
  1699. if (msg_sys->msg_name)
  1700. memcpy(&used_address->name, msg_sys->msg_name,
  1701. used_address->name_len);
  1702. }
  1703. out_freectl:
  1704. if (ctl_buf != ctl)
  1705. sock_kfree_s(sock->sk, ctl_buf, ctl_len);
  1706. out_freeiov:
  1707. if (iov != iovstack)
  1708. sock_kfree_s(sock->sk, iov, iov_size);
  1709. out:
  1710. return err;
  1711. }
  1712. /*
  1713. * BSD sendmsg interface
  1714. */
  1715. SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned, flags)
  1716. {
  1717. int fput_needed, err;
  1718. struct msghdr msg_sys;
  1719. struct socket *sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1720. if (!sock)
  1721. goto out;
  1722. err = __sys_sendmsg(sock, msg, &msg_sys, flags, NULL);
  1723. fput_light(sock->file, fput_needed);
  1724. out:
  1725. return err;
  1726. }
  1727. /*
  1728. * Linux sendmmsg interface
  1729. */
  1730. int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
  1731. unsigned int flags)
  1732. {
  1733. int fput_needed, err, datagrams;
  1734. struct socket *sock;
  1735. struct mmsghdr __user *entry;
  1736. struct compat_mmsghdr __user *compat_entry;
  1737. struct msghdr msg_sys;
  1738. struct used_address used_address;
  1739. if (vlen > UIO_MAXIOV)
  1740. vlen = UIO_MAXIOV;
  1741. datagrams = 0;
  1742. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1743. if (!sock)
  1744. return err;
  1745. used_address.name_len = UINT_MAX;
  1746. entry = mmsg;
  1747. compat_entry = (struct compat_mmsghdr __user *)mmsg;
  1748. err = 0;
  1749. while (datagrams < vlen) {
  1750. if (MSG_CMSG_COMPAT & flags) {
  1751. err = __sys_sendmsg(sock, (struct msghdr __user *)compat_entry,
  1752. &msg_sys, flags, &used_address);
  1753. if (err < 0)
  1754. break;
  1755. err = __put_user(err, &compat_entry->msg_len);
  1756. ++compat_entry;
  1757. } else {
  1758. err = __sys_sendmsg(sock, (struct msghdr __user *)entry,
  1759. &msg_sys, flags, &used_address);
  1760. if (err < 0)
  1761. break;
  1762. err = put_user(err, &entry->msg_len);
  1763. ++entry;
  1764. }
  1765. if (err)
  1766. break;
  1767. ++datagrams;
  1768. }
  1769. fput_light(sock->file, fput_needed);
  1770. /* We only return an error if no datagrams were able to be sent */
  1771. if (datagrams != 0)
  1772. return datagrams;
  1773. return err;
  1774. }
  1775. SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
  1776. unsigned int, vlen, unsigned int, flags)
  1777. {
  1778. return __sys_sendmmsg(fd, mmsg, vlen, flags);
  1779. }
  1780. static int __sys_recvmsg(struct socket *sock, struct msghdr __user *msg,
  1781. struct msghdr *msg_sys, unsigned flags, int nosec)
  1782. {
  1783. struct compat_msghdr __user *msg_compat =
  1784. (struct compat_msghdr __user *)msg;
  1785. struct iovec iovstack[UIO_FASTIOV];
  1786. struct iovec *iov = iovstack;
  1787. unsigned long cmsg_ptr;
  1788. int err, iov_size, total_len, len;
  1789. /* kernel mode address */
  1790. struct sockaddr_storage addr;
  1791. /* user mode address pointers */
  1792. struct sockaddr __user *uaddr;
  1793. int __user *uaddr_len;
  1794. if (MSG_CMSG_COMPAT & flags) {
  1795. if (get_compat_msghdr(msg_sys, msg_compat))
  1796. return -EFAULT;
  1797. } else if (copy_from_user(msg_sys, msg, sizeof(struct msghdr)))
  1798. return -EFAULT;
  1799. err = -EMSGSIZE;
  1800. if (msg_sys->msg_iovlen > UIO_MAXIOV)
  1801. goto out;
  1802. /* Check whether to allocate the iovec area */
  1803. err = -ENOMEM;
  1804. iov_size = msg_sys->msg_iovlen * sizeof(struct iovec);
  1805. if (msg_sys->msg_iovlen > UIO_FASTIOV) {
  1806. iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
  1807. if (!iov)
  1808. goto out;
  1809. }
  1810. /*
  1811. * Save the user-mode address (verify_iovec will change the
  1812. * kernel msghdr to use the kernel address space)
  1813. */
  1814. uaddr = (__force void __user *)msg_sys->msg_name;
  1815. uaddr_len = COMPAT_NAMELEN(msg);
  1816. if (MSG_CMSG_COMPAT & flags) {
  1817. err = verify_compat_iovec(msg_sys, iov,
  1818. (struct sockaddr *)&addr,
  1819. VERIFY_WRITE);
  1820. } else
  1821. err = verify_iovec(msg_sys, iov,
  1822. (struct sockaddr *)&addr,
  1823. VERIFY_WRITE);
  1824. if (err < 0)
  1825. goto out_freeiov;
  1826. total_len = err;
  1827. cmsg_ptr = (unsigned long)msg_sys->msg_control;
  1828. msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
  1829. if (sock->file->f_flags & O_NONBLOCK)
  1830. flags |= MSG_DONTWAIT;
  1831. err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
  1832. total_len, flags);
  1833. if (err < 0)
  1834. goto out_freeiov;
  1835. len = err;
  1836. if (uaddr != NULL) {
  1837. err = move_addr_to_user((struct sockaddr *)&addr,
  1838. msg_sys->msg_namelen, uaddr,
  1839. uaddr_len);
  1840. if (err < 0)
  1841. goto out_freeiov;
  1842. }
  1843. err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
  1844. COMPAT_FLAGS(msg));
  1845. if (err)
  1846. goto out_freeiov;
  1847. if (MSG_CMSG_COMPAT & flags)
  1848. err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
  1849. &msg_compat->msg_controllen);
  1850. else
  1851. err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
  1852. &msg->msg_controllen);
  1853. if (err)
  1854. goto out_freeiov;
  1855. err = len;
  1856. out_freeiov:
  1857. if (iov != iovstack)
  1858. sock_kfree_s(sock->sk, iov, iov_size);
  1859. out:
  1860. return err;
  1861. }
  1862. /*
  1863. * BSD recvmsg interface
  1864. */
  1865. SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
  1866. unsigned int, flags)
  1867. {
  1868. int fput_needed, err;
  1869. struct msghdr msg_sys;
  1870. struct socket *sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1871. if (!sock)
  1872. goto out;
  1873. err = __sys_recvmsg(sock, msg, &msg_sys, flags, 0);
  1874. fput_light(sock->file, fput_needed);
  1875. out:
  1876. return err;
  1877. }
  1878. /*
  1879. * Linux recvmmsg interface
  1880. */
  1881. int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
  1882. unsigned int flags, struct timespec *timeout)
  1883. {
  1884. int fput_needed, err, datagrams;
  1885. struct socket *sock;
  1886. struct mmsghdr __user *entry;
  1887. struct compat_mmsghdr __user *compat_entry;
  1888. struct msghdr msg_sys;
  1889. struct timespec end_time;
  1890. if (timeout &&
  1891. poll_select_set_timeout(&end_time, timeout->tv_sec,
  1892. timeout->tv_nsec))
  1893. return -EINVAL;
  1894. datagrams = 0;
  1895. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1896. if (!sock)
  1897. return err;
  1898. err = sock_error(sock->sk);
  1899. if (err)
  1900. goto out_put;
  1901. entry = mmsg;
  1902. compat_entry = (struct compat_mmsghdr __user *)mmsg;
  1903. while (datagrams < vlen) {
  1904. /*
  1905. * No need to ask LSM for more than the first datagram.
  1906. */
  1907. if (MSG_CMSG_COMPAT & flags) {
  1908. err = __sys_recvmsg(sock, (struct msghdr __user *)compat_entry,
  1909. &msg_sys, flags & ~MSG_WAITFORONE,
  1910. datagrams);
  1911. if (err < 0)
  1912. break;
  1913. err = __put_user(err, &compat_entry->msg_len);
  1914. ++compat_entry;
  1915. } else {
  1916. err = __sys_recvmsg(sock, (struct msghdr __user *)entry,
  1917. &msg_sys, flags & ~MSG_WAITFORONE,
  1918. datagrams);
  1919. if (err < 0)
  1920. break;
  1921. err = put_user(err, &entry->msg_len);
  1922. ++entry;
  1923. }
  1924. if (err)
  1925. break;
  1926. ++datagrams;
  1927. /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
  1928. if (flags & MSG_WAITFORONE)
  1929. flags |= MSG_DONTWAIT;
  1930. if (timeout) {
  1931. ktime_get_ts(timeout);
  1932. *timeout = timespec_sub(end_time, *timeout);
  1933. if (timeout->tv_sec < 0) {
  1934. timeout->tv_sec = timeout->tv_nsec = 0;
  1935. break;
  1936. }
  1937. /* Timeout, return less than vlen datagrams */
  1938. if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
  1939. break;
  1940. }
  1941. /* Out of band data, return right away */
  1942. if (msg_sys.msg_flags & MSG_OOB)
  1943. break;
  1944. }
  1945. out_put:
  1946. fput_light(sock->file, fput_needed);
  1947. if (err == 0)
  1948. return datagrams;
  1949. if (datagrams != 0) {
  1950. /*
  1951. * We may return less entries than requested (vlen) if the
  1952. * sock is non block and there aren't enough datagrams...
  1953. */
  1954. if (err != -EAGAIN) {
  1955. /*
  1956. * ... or if recvmsg returns an error after we
  1957. * received some datagrams, where we record the
  1958. * error to return on the next call or if the
  1959. * app asks about it using getsockopt(SO_ERROR).
  1960. */
  1961. sock->sk->sk_err = -err;
  1962. }
  1963. return datagrams;
  1964. }
  1965. return err;
  1966. }
  1967. SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
  1968. unsigned int, vlen, unsigned int, flags,
  1969. struct timespec __user *, timeout)
  1970. {
  1971. int datagrams;
  1972. struct timespec timeout_sys;
  1973. if (!timeout)
  1974. return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
  1975. if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
  1976. return -EFAULT;
  1977. datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
  1978. if (datagrams > 0 &&
  1979. copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
  1980. datagrams = -EFAULT;
  1981. return datagrams;
  1982. }
  1983. #ifdef __ARCH_WANT_SYS_SOCKETCALL
  1984. /* Argument list sizes for sys_socketcall */
  1985. #define AL(x) ((x) * sizeof(unsigned long))
  1986. static const unsigned char nargs[21] = {
  1987. AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
  1988. AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
  1989. AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
  1990. AL(4), AL(5), AL(4)
  1991. };
  1992. #undef AL
  1993. /*
  1994. * System call vectors.
  1995. *
  1996. * Argument checking cleaned up. Saved 20% in size.
  1997. * This function doesn't need to set the kernel lock because
  1998. * it is set by the callees.
  1999. */
  2000. SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
  2001. {
  2002. unsigned long a[6];
  2003. unsigned long a0, a1;
  2004. int err;
  2005. unsigned int len;
  2006. if (call < 1 || call > SYS_SENDMMSG)
  2007. return -EINVAL;
  2008. len = nargs[call];
  2009. if (len > sizeof(a))
  2010. return -EINVAL;
  2011. /* copy_from_user should be SMP safe. */
  2012. if (copy_from_user(a, args, len))
  2013. return -EFAULT;
  2014. audit_socketcall(nargs[call] / sizeof(unsigned long), a);
  2015. a0 = a[0];
  2016. a1 = a[1];
  2017. switch (call) {
  2018. case SYS_SOCKET:
  2019. err = sys_socket(a0, a1, a[2]);
  2020. break;
  2021. case SYS_BIND:
  2022. err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
  2023. break;
  2024. case SYS_CONNECT:
  2025. err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
  2026. break;
  2027. case SYS_LISTEN:
  2028. err = sys_listen(a0, a1);
  2029. break;
  2030. case SYS_ACCEPT:
  2031. err = sys_accept4(a0, (struct sockaddr __user *)a1,
  2032. (int __user *)a[2], 0);
  2033. break;
  2034. case SYS_GETSOCKNAME:
  2035. err =
  2036. sys_getsockname(a0, (struct sockaddr __user *)a1,
  2037. (int __user *)a[2]);
  2038. break;
  2039. case SYS_GETPEERNAME:
  2040. err =
  2041. sys_getpeername(a0, (struct sockaddr __user *)a1,
  2042. (int __user *)a[2]);
  2043. break;
  2044. case SYS_SOCKETPAIR:
  2045. err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
  2046. break;
  2047. case SYS_SEND:
  2048. err = sys_send(a0, (void __user *)a1, a[2], a[3]);
  2049. break;
  2050. case SYS_SENDTO:
  2051. err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
  2052. (struct sockaddr __user *)a[4], a[5]);
  2053. break;
  2054. case SYS_RECV:
  2055. err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
  2056. break;
  2057. case SYS_RECVFROM:
  2058. err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
  2059. (struct sockaddr __user *)a[4],
  2060. (int __user *)a[5]);
  2061. break;
  2062. case SYS_SHUTDOWN:
  2063. err = sys_shutdown(a0, a1);
  2064. break;
  2065. case SYS_SETSOCKOPT:
  2066. err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
  2067. break;
  2068. case SYS_GETSOCKOPT:
  2069. err =
  2070. sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
  2071. (int __user *)a[4]);
  2072. break;
  2073. case SYS_SENDMSG:
  2074. err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
  2075. break;
  2076. case SYS_SENDMMSG:
  2077. err = sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3]);
  2078. break;
  2079. case SYS_RECVMSG:
  2080. err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
  2081. break;
  2082. case SYS_RECVMMSG:
  2083. err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
  2084. (struct timespec __user *)a[4]);
  2085. break;
  2086. case SYS_ACCEPT4:
  2087. err = sys_accept4(a0, (struct sockaddr __user *)a1,
  2088. (int __user *)a[2], a[3]);
  2089. break;
  2090. default:
  2091. err = -EINVAL;
  2092. break;
  2093. }
  2094. return err;
  2095. }
  2096. #endif /* __ARCH_WANT_SYS_SOCKETCALL */
  2097. /**
  2098. * sock_register - add a socket protocol handler
  2099. * @ops: description of protocol
  2100. *
  2101. * This function is called by a protocol handler that wants to
  2102. * advertise its address family, and have it linked into the
  2103. * socket interface. The value ops->family coresponds to the
  2104. * socket system call protocol family.
  2105. */
  2106. int sock_register(const struct net_proto_family *ops)
  2107. {
  2108. int err;
  2109. if (ops->family >= NPROTO) {
  2110. printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
  2111. NPROTO);
  2112. return -ENOBUFS;
  2113. }
  2114. spin_lock(&net_family_lock);
  2115. if (rcu_dereference_protected(net_families[ops->family],
  2116. lockdep_is_held(&net_family_lock)))
  2117. err = -EEXIST;
  2118. else {
  2119. rcu_assign_pointer(net_families[ops->family], ops);
  2120. err = 0;
  2121. }
  2122. spin_unlock(&net_family_lock);
  2123. printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
  2124. return err;
  2125. }
  2126. EXPORT_SYMBOL(sock_register);
  2127. /**
  2128. * sock_unregister - remove a protocol handler
  2129. * @family: protocol family to remove
  2130. *
  2131. * This function is called by a protocol handler that wants to
  2132. * remove its address family, and have it unlinked from the
  2133. * new socket creation.
  2134. *
  2135. * If protocol handler is a module, then it can use module reference
  2136. * counts to protect against new references. If protocol handler is not
  2137. * a module then it needs to provide its own protection in
  2138. * the ops->create routine.
  2139. */
  2140. void sock_unregister(int family)
  2141. {
  2142. BUG_ON(family < 0 || family >= NPROTO);
  2143. spin_lock(&net_family_lock);
  2144. RCU_INIT_POINTER(net_families[family], NULL);
  2145. spin_unlock(&net_family_lock);
  2146. synchronize_rcu();
  2147. printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
  2148. }
  2149. EXPORT_SYMBOL(sock_unregister);
  2150. static int __init sock_init(void)
  2151. {
  2152. int err;
  2153. /*
  2154. * Initialize sock SLAB cache.
  2155. */
  2156. sk_init();
  2157. /*
  2158. * Initialize skbuff SLAB cache
  2159. */
  2160. skb_init();
  2161. /*
  2162. * Initialize the protocols module.
  2163. */
  2164. init_inodecache();
  2165. err = register_filesystem(&sock_fs_type);
  2166. if (err)
  2167. goto out_fs;
  2168. sock_mnt = kern_mount(&sock_fs_type);
  2169. if (IS_ERR(sock_mnt)) {
  2170. err = PTR_ERR(sock_mnt);
  2171. goto out_mount;
  2172. }
  2173. /* The real protocol initialization is performed in later initcalls.
  2174. */
  2175. #ifdef CONFIG_NETFILTER
  2176. netfilter_init();
  2177. #endif
  2178. #ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
  2179. skb_timestamping_init();
  2180. #endif
  2181. out:
  2182. return err;
  2183. out_mount:
  2184. unregister_filesystem(&sock_fs_type);
  2185. out_fs:
  2186. goto out;
  2187. }
  2188. core_initcall(sock_init); /* early initcall */
  2189. #ifdef CONFIG_PROC_FS
  2190. void socket_seq_show(struct seq_file *seq)
  2191. {
  2192. int cpu;
  2193. int counter = 0;
  2194. for_each_possible_cpu(cpu)
  2195. counter += per_cpu(sockets_in_use, cpu);
  2196. /* It can be negative, by the way. 8) */
  2197. if (counter < 0)
  2198. counter = 0;
  2199. seq_printf(seq, "sockets: used %d\n", counter);
  2200. }
  2201. #endif /* CONFIG_PROC_FS */
  2202. #ifdef CONFIG_COMPAT
  2203. static int do_siocgstamp(struct net *net, struct socket *sock,
  2204. unsigned int cmd, struct compat_timeval __user *up)
  2205. {
  2206. mm_segment_t old_fs = get_fs();
  2207. struct timeval ktv;
  2208. int err;
  2209. set_fs(KERNEL_DS);
  2210. err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
  2211. set_fs(old_fs);
  2212. if (!err) {
  2213. err = put_user(ktv.tv_sec, &up->tv_sec);
  2214. err |= __put_user(ktv.tv_usec, &up->tv_usec);
  2215. }
  2216. return err;
  2217. }
  2218. static int do_siocgstampns(struct net *net, struct socket *sock,
  2219. unsigned int cmd, struct compat_timespec __user *up)
  2220. {
  2221. mm_segment_t old_fs = get_fs();
  2222. struct timespec kts;
  2223. int err;
  2224. set_fs(KERNEL_DS);
  2225. err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
  2226. set_fs(old_fs);
  2227. if (!err) {
  2228. err = put_user(kts.tv_sec, &up->tv_sec);
  2229. err |= __put_user(kts.tv_nsec, &up->tv_nsec);
  2230. }
  2231. return err;
  2232. }
  2233. static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
  2234. {
  2235. struct ifreq __user *uifr;
  2236. int err;
  2237. uifr = compat_alloc_user_space(sizeof(struct ifreq));
  2238. if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
  2239. return -EFAULT;
  2240. err = dev_ioctl(net, SIOCGIFNAME, uifr);
  2241. if (err)
  2242. return err;
  2243. if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
  2244. return -EFAULT;
  2245. return 0;
  2246. }
  2247. static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
  2248. {
  2249. struct compat_ifconf ifc32;
  2250. struct ifconf ifc;
  2251. struct ifconf __user *uifc;
  2252. struct compat_ifreq __user *ifr32;
  2253. struct ifreq __user *ifr;
  2254. unsigned int i, j;
  2255. int err;
  2256. if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
  2257. return -EFAULT;
  2258. memset(&ifc, 0, sizeof(ifc));
  2259. if (ifc32.ifcbuf == 0) {
  2260. ifc32.ifc_len = 0;
  2261. ifc.ifc_len = 0;
  2262. ifc.ifc_req = NULL;
  2263. uifc = compat_alloc_user_space(sizeof(struct ifconf));
  2264. } else {
  2265. size_t len = ((ifc32.ifc_len / sizeof(struct compat_ifreq)) + 1) *
  2266. sizeof(struct ifreq);
  2267. uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
  2268. ifc.ifc_len = len;
  2269. ifr = ifc.ifc_req = (void __user *)(uifc + 1);
  2270. ifr32 = compat_ptr(ifc32.ifcbuf);
  2271. for (i = 0; i < ifc32.ifc_len; i += sizeof(struct compat_ifreq)) {
  2272. if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
  2273. return -EFAULT;
  2274. ifr++;
  2275. ifr32++;
  2276. }
  2277. }
  2278. if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
  2279. return -EFAULT;
  2280. err = dev_ioctl(net, SIOCGIFCONF, uifc);
  2281. if (err)
  2282. return err;
  2283. if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
  2284. return -EFAULT;
  2285. ifr = ifc.ifc_req;
  2286. ifr32 = compat_ptr(ifc32.ifcbuf);
  2287. for (i = 0, j = 0;
  2288. i + sizeof(struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
  2289. i += sizeof(struct compat_ifreq), j += sizeof(struct ifreq)) {
  2290. if (copy_in_user(ifr32, ifr, sizeof(struct compat_ifreq)))
  2291. return -EFAULT;
  2292. ifr32++;
  2293. ifr++;
  2294. }
  2295. if (ifc32.ifcbuf == 0) {
  2296. /* Translate from 64-bit structure multiple to
  2297. * a 32-bit one.
  2298. */
  2299. i = ifc.ifc_len;
  2300. i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
  2301. ifc32.ifc_len = i;
  2302. } else {
  2303. ifc32.ifc_len = i;
  2304. }
  2305. if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
  2306. return -EFAULT;
  2307. return 0;
  2308. }
  2309. static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
  2310. {
  2311. struct compat_ethtool_rxnfc __user *compat_rxnfc;
  2312. bool convert_in = false, convert_out = false;
  2313. size_t buf_size = ALIGN(sizeof(struct ifreq), 8);
  2314. struct ethtool_rxnfc __user *rxnfc;
  2315. struct ifreq __user *ifr;
  2316. u32 rule_cnt = 0, actual_rule_cnt;
  2317. u32 ethcmd;
  2318. u32 data;
  2319. int ret;
  2320. if (get_user(data, &ifr32->ifr_ifru.ifru_data))
  2321. return -EFAULT;
  2322. compat_rxnfc = compat_ptr(data);
  2323. if (get_user(ethcmd, &compat_rxnfc->cmd))
  2324. return -EFAULT;
  2325. /* Most ethtool structures are defined without padding.
  2326. * Unfortunately struct ethtool_rxnfc is an exception.
  2327. */
  2328. switch (ethcmd) {
  2329. default:
  2330. break;
  2331. case ETHTOOL_GRXCLSRLALL:
  2332. /* Buffer size is variable */
  2333. if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
  2334. return -EFAULT;
  2335. if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
  2336. return -ENOMEM;
  2337. buf_size += rule_cnt * sizeof(u32);
  2338. /* fall through */
  2339. case ETHTOOL_GRXRINGS:
  2340. case ETHTOOL_GRXCLSRLCNT:
  2341. case ETHTOOL_GRXCLSRULE:
  2342. convert_out = true;
  2343. /* fall through */
  2344. case ETHTOOL_SRXCLSRLDEL:
  2345. case ETHTOOL_SRXCLSRLINS:
  2346. buf_size += sizeof(struct ethtool_rxnfc);
  2347. convert_in = true;
  2348. break;
  2349. }
  2350. ifr = compat_alloc_user_space(buf_size);
  2351. rxnfc = (void *)ifr + ALIGN(sizeof(struct ifreq), 8);
  2352. if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
  2353. return -EFAULT;
  2354. if (put_user(convert_in ? rxnfc : compat_ptr(data),
  2355. &ifr->ifr_ifru.ifru_data))
  2356. return -EFAULT;
  2357. if (convert_in) {
  2358. /* We expect there to be holes between fs.m_ext and
  2359. * fs.ring_cookie and at the end of fs, but nowhere else.
  2360. */
  2361. BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
  2362. sizeof(compat_rxnfc->fs.m_ext) !=
  2363. offsetof(struct ethtool_rxnfc, fs.m_ext) +
  2364. sizeof(rxnfc->fs.m_ext));
  2365. BUILD_BUG_ON(
  2366. offsetof(struct compat_ethtool_rxnfc, fs.location) -
  2367. offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
  2368. offsetof(struct ethtool_rxnfc, fs.location) -
  2369. offsetof(struct ethtool_rxnfc, fs.ring_cookie));
  2370. if (copy_in_user(rxnfc, compat_rxnfc,
  2371. (void *)(&rxnfc->fs.m_ext + 1) -
  2372. (void *)rxnfc) ||
  2373. copy_in_user(&rxnfc->fs.ring_cookie,
  2374. &compat_rxnfc->fs.ring_cookie,
  2375. (void *)(&rxnfc->fs.location + 1) -
  2376. (void *)&rxnfc->fs.ring_cookie) ||
  2377. copy_in_user(&rxnfc->rule_cnt, &compat_rxnfc->rule_cnt,
  2378. sizeof(rxnfc->rule_cnt)))
  2379. return -EFAULT;
  2380. }
  2381. ret = dev_ioctl(net, SIOCETHTOOL, ifr);
  2382. if (ret)
  2383. return ret;
  2384. if (convert_out) {
  2385. if (copy_in_user(compat_rxnfc, rxnfc,
  2386. (const void *)(&rxnfc->fs.m_ext + 1) -
  2387. (const void *)rxnfc) ||
  2388. copy_in_user(&compat_rxnfc->fs.ring_cookie,
  2389. &rxnfc->fs.ring_cookie,
  2390. (const void *)(&rxnfc->fs.location + 1) -
  2391. (const void *)&rxnfc->fs.ring_cookie) ||
  2392. copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
  2393. sizeof(rxnfc->rule_cnt)))
  2394. return -EFAULT;
  2395. if (ethcmd == ETHTOOL_GRXCLSRLALL) {
  2396. /* As an optimisation, we only copy the actual
  2397. * number of rules that the underlying
  2398. * function returned. Since Mallory might
  2399. * change the rule count in user memory, we
  2400. * check that it is less than the rule count
  2401. * originally given (as the user buffer size),
  2402. * which has been range-checked.
  2403. */
  2404. if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
  2405. return -EFAULT;
  2406. if (actual_rule_cnt < rule_cnt)
  2407. rule_cnt = actual_rule_cnt;
  2408. if (copy_in_user(&compat_rxnfc->rule_locs[0],
  2409. &rxnfc->rule_locs[0],
  2410. rule_cnt * sizeof(u32)))
  2411. return -EFAULT;
  2412. }
  2413. }
  2414. return 0;
  2415. }
  2416. static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
  2417. {
  2418. void __user *uptr;
  2419. compat_uptr_t uptr32;
  2420. struct ifreq __user *uifr;
  2421. uifr = compat_alloc_user_space(sizeof(*uifr));
  2422. if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
  2423. return -EFAULT;
  2424. if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
  2425. return -EFAULT;
  2426. uptr = compat_ptr(uptr32);
  2427. if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
  2428. return -EFAULT;
  2429. return dev_ioctl(net, SIOCWANDEV, uifr);
  2430. }
  2431. static int bond_ioctl(struct net *net, unsigned int cmd,
  2432. struct compat_ifreq __user *ifr32)
  2433. {
  2434. struct ifreq kifr;
  2435. struct ifreq __user *uifr;
  2436. mm_segment_t old_fs;
  2437. int err;
  2438. u32 data;
  2439. void __user *datap;
  2440. switch (cmd) {
  2441. case SIOCBONDENSLAVE:
  2442. case SIOCBONDRELEASE:
  2443. case SIOCBONDSETHWADDR:
  2444. case SIOCBONDCHANGEACTIVE:
  2445. if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
  2446. return -EFAULT;
  2447. old_fs = get_fs();
  2448. set_fs(KERNEL_DS);
  2449. err = dev_ioctl(net, cmd,
  2450. (struct ifreq __user __force *) &kifr);
  2451. set_fs(old_fs);
  2452. return err;
  2453. case SIOCBONDSLAVEINFOQUERY:
  2454. case SIOCBONDINFOQUERY:
  2455. uifr = compat_alloc_user_space(sizeof(*uifr));
  2456. if (copy_in_user(&uifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
  2457. return -EFAULT;
  2458. if (get_user(data, &ifr32->ifr_ifru.ifru_data))
  2459. return -EFAULT;
  2460. datap = compat_ptr(data);
  2461. if (put_user(datap, &uifr->ifr_ifru.ifru_data))
  2462. return -EFAULT;
  2463. return dev_ioctl(net, cmd, uifr);
  2464. default:
  2465. return -EINVAL;
  2466. }
  2467. }
  2468. static int siocdevprivate_ioctl(struct net *net, unsigned int cmd,
  2469. struct compat_ifreq __user *u_ifreq32)
  2470. {
  2471. struct ifreq __user *u_ifreq64;
  2472. char tmp_buf[IFNAMSIZ];
  2473. void __user *data64;
  2474. u32 data32;
  2475. if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
  2476. IFNAMSIZ))
  2477. return -EFAULT;
  2478. if (__get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
  2479. return -EFAULT;
  2480. data64 = compat_ptr(data32);
  2481. u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
  2482. /* Don't check these user accesses, just let that get trapped
  2483. * in the ioctl handler instead.
  2484. */
  2485. if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
  2486. IFNAMSIZ))
  2487. return -EFAULT;
  2488. if (__put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
  2489. return -EFAULT;
  2490. return dev_ioctl(net, cmd, u_ifreq64);
  2491. }
  2492. static int dev_ifsioc(struct net *net, struct socket *sock,
  2493. unsigned int cmd, struct compat_ifreq __user *uifr32)
  2494. {
  2495. struct ifreq __user *uifr;
  2496. int err;
  2497. uifr = compat_alloc_user_space(sizeof(*uifr));
  2498. if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
  2499. return -EFAULT;
  2500. err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
  2501. if (!err) {
  2502. switch (cmd) {
  2503. case SIOCGIFFLAGS:
  2504. case SIOCGIFMETRIC:
  2505. case SIOCGIFMTU:
  2506. case SIOCGIFMEM:
  2507. case SIOCGIFHWADDR:
  2508. case SIOCGIFINDEX:
  2509. case SIOCGIFADDR:
  2510. case SIOCGIFBRDADDR:
  2511. case SIOCGIFDSTADDR:
  2512. case SIOCGIFNETMASK:
  2513. case SIOCGIFPFLAGS:
  2514. case SIOCGIFTXQLEN:
  2515. case SIOCGMIIPHY:
  2516. case SIOCGMIIREG:
  2517. if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
  2518. err = -EFAULT;
  2519. break;
  2520. }
  2521. }
  2522. return err;
  2523. }
  2524. static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
  2525. struct compat_ifreq __user *uifr32)
  2526. {
  2527. struct ifreq ifr;
  2528. struct compat_ifmap __user *uifmap32;
  2529. mm_segment_t old_fs;
  2530. int err;
  2531. uifmap32 = &uifr32->ifr_ifru.ifru_map;
  2532. err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
  2533. err |= __get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
  2534. err |= __get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
  2535. err |= __get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
  2536. err |= __get_user(ifr.ifr_map.irq, &uifmap32->irq);
  2537. err |= __get_user(ifr.ifr_map.dma, &uifmap32->dma);
  2538. err |= __get_user(ifr.ifr_map.port, &uifmap32->port);
  2539. if (err)
  2540. return -EFAULT;
  2541. old_fs = get_fs();
  2542. set_fs(KERNEL_DS);
  2543. err = dev_ioctl(net, cmd, (void __user __force *)&ifr);
  2544. set_fs(old_fs);
  2545. if (cmd == SIOCGIFMAP && !err) {
  2546. err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
  2547. err |= __put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
  2548. err |= __put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
  2549. err |= __put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
  2550. err |= __put_user(ifr.ifr_map.irq, &uifmap32->irq);
  2551. err |= __put_user(ifr.ifr_map.dma, &uifmap32->dma);
  2552. err |= __put_user(ifr.ifr_map.port, &uifmap32->port);
  2553. if (err)
  2554. err = -EFAULT;
  2555. }
  2556. return err;
  2557. }
  2558. static int compat_siocshwtstamp(struct net *net, struct compat_ifreq __user *uifr32)
  2559. {
  2560. void __user *uptr;
  2561. compat_uptr_t uptr32;
  2562. struct ifreq __user *uifr;
  2563. uifr = compat_alloc_user_space(sizeof(*uifr));
  2564. if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
  2565. return -EFAULT;
  2566. if (get_user(uptr32, &uifr32->ifr_data))
  2567. return -EFAULT;
  2568. uptr = compat_ptr(uptr32);
  2569. if (put_user(uptr, &uifr->ifr_data))
  2570. return -EFAULT;
  2571. return dev_ioctl(net, SIOCSHWTSTAMP, uifr);
  2572. }
  2573. struct rtentry32 {
  2574. u32 rt_pad1;
  2575. struct sockaddr rt_dst; /* target address */
  2576. struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
  2577. struct sockaddr rt_genmask; /* target network mask (IP) */
  2578. unsigned short rt_flags;
  2579. short rt_pad2;
  2580. u32 rt_pad3;
  2581. unsigned char rt_tos;
  2582. unsigned char rt_class;
  2583. short rt_pad4;
  2584. short rt_metric; /* +1 for binary compatibility! */
  2585. /* char * */ u32 rt_dev; /* forcing the device at add */
  2586. u32 rt_mtu; /* per route MTU/Window */
  2587. u32 rt_window; /* Window clamping */
  2588. unsigned short rt_irtt; /* Initial RTT */
  2589. };
  2590. struct in6_rtmsg32 {
  2591. struct in6_addr rtmsg_dst;
  2592. struct in6_addr rtmsg_src;
  2593. struct in6_addr rtmsg_gateway;
  2594. u32 rtmsg_type;
  2595. u16 rtmsg_dst_len;
  2596. u16 rtmsg_src_len;
  2597. u32 rtmsg_metric;
  2598. u32 rtmsg_info;
  2599. u32 rtmsg_flags;
  2600. s32 rtmsg_ifindex;
  2601. };
  2602. static int routing_ioctl(struct net *net, struct socket *sock,
  2603. unsigned int cmd, void __user *argp)
  2604. {
  2605. int ret;
  2606. void *r = NULL;
  2607. struct in6_rtmsg r6;
  2608. struct rtentry r4;
  2609. char devname[16];
  2610. u32 rtdev;
  2611. mm_segment_t old_fs = get_fs();
  2612. if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
  2613. struct in6_rtmsg32 __user *ur6 = argp;
  2614. ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
  2615. 3 * sizeof(struct in6_addr));
  2616. ret |= __get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
  2617. ret |= __get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
  2618. ret |= __get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
  2619. ret |= __get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
  2620. ret |= __get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
  2621. ret |= __get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
  2622. ret |= __get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
  2623. r = (void *) &r6;
  2624. } else { /* ipv4 */
  2625. struct rtentry32 __user *ur4 = argp;
  2626. ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
  2627. 3 * sizeof(struct sockaddr));
  2628. ret |= __get_user(r4.rt_flags, &(ur4->rt_flags));
  2629. ret |= __get_user(r4.rt_metric, &(ur4->rt_metric));
  2630. ret |= __get_user(r4.rt_mtu, &(ur4->rt_mtu));
  2631. ret |= __get_user(r4.rt_window, &(ur4->rt_window));
  2632. ret |= __get_user(r4.rt_irtt, &(ur4->rt_irtt));
  2633. ret |= __get_user(rtdev, &(ur4->rt_dev));
  2634. if (rtdev) {
  2635. ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
  2636. r4.rt_dev = (char __user __force *)devname;
  2637. devname[15] = 0;
  2638. } else
  2639. r4.rt_dev = NULL;
  2640. r = (void *) &r4;
  2641. }
  2642. if (ret) {
  2643. ret = -EFAULT;
  2644. goto out;
  2645. }
  2646. set_fs(KERNEL_DS);
  2647. ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
  2648. set_fs(old_fs);
  2649. out:
  2650. return ret;
  2651. }
  2652. /* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
  2653. * for some operations; this forces use of the newer bridge-utils that
  2654. * use compatible ioctls
  2655. */
  2656. static int old_bridge_ioctl(compat_ulong_t __user *argp)
  2657. {
  2658. compat_ulong_t tmp;
  2659. if (get_user(tmp, argp))
  2660. return -EFAULT;
  2661. if (tmp == BRCTL_GET_VERSION)
  2662. return BRCTL_VERSION + 1;
  2663. return -EINVAL;
  2664. }
  2665. static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
  2666. unsigned int cmd, unsigned long arg)
  2667. {
  2668. void __user *argp = compat_ptr(arg);
  2669. struct sock *sk = sock->sk;
  2670. struct net *net = sock_net(sk);
  2671. if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
  2672. return siocdevprivate_ioctl(net, cmd, argp);
  2673. switch (cmd) {
  2674. case SIOCSIFBR:
  2675. case SIOCGIFBR:
  2676. return old_bridge_ioctl(argp);
  2677. case SIOCGIFNAME:
  2678. return dev_ifname32(net, argp);
  2679. case SIOCGIFCONF:
  2680. return dev_ifconf(net, argp);
  2681. case SIOCETHTOOL:
  2682. return ethtool_ioctl(net, argp);
  2683. case SIOCWANDEV:
  2684. return compat_siocwandev(net, argp);
  2685. case SIOCGIFMAP:
  2686. case SIOCSIFMAP:
  2687. return compat_sioc_ifmap(net, cmd, argp);
  2688. case SIOCBONDENSLAVE:
  2689. case SIOCBONDRELEASE:
  2690. case SIOCBONDSETHWADDR:
  2691. case SIOCBONDSLAVEINFOQUERY:
  2692. case SIOCBONDINFOQUERY:
  2693. case SIOCBONDCHANGEACTIVE:
  2694. return bond_ioctl(net, cmd, argp);
  2695. case SIOCADDRT:
  2696. case SIOCDELRT:
  2697. return routing_ioctl(net, sock, cmd, argp);
  2698. case SIOCGSTAMP:
  2699. return do_siocgstamp(net, sock, cmd, argp);
  2700. case SIOCGSTAMPNS:
  2701. return do_siocgstampns(net, sock, cmd, argp);
  2702. case SIOCSHWTSTAMP:
  2703. return compat_siocshwtstamp(net, argp);
  2704. case FIOSETOWN:
  2705. case SIOCSPGRP:
  2706. case FIOGETOWN:
  2707. case SIOCGPGRP:
  2708. case SIOCBRADDBR:
  2709. case SIOCBRDELBR:
  2710. case SIOCGIFVLAN:
  2711. case SIOCSIFVLAN:
  2712. case SIOCADDDLCI:
  2713. case SIOCDELDLCI:
  2714. return sock_ioctl(file, cmd, arg);
  2715. case SIOCGIFFLAGS:
  2716. case SIOCSIFFLAGS:
  2717. case SIOCGIFMETRIC:
  2718. case SIOCSIFMETRIC:
  2719. case SIOCGIFMTU:
  2720. case SIOCSIFMTU:
  2721. case SIOCGIFMEM:
  2722. case SIOCSIFMEM:
  2723. case SIOCGIFHWADDR:
  2724. case SIOCSIFHWADDR:
  2725. case SIOCADDMULTI:
  2726. case SIOCDELMULTI:
  2727. case SIOCGIFINDEX:
  2728. case SIOCGIFADDR:
  2729. case SIOCSIFADDR:
  2730. case SIOCSIFHWBROADCAST:
  2731. case SIOCDIFADDR:
  2732. case SIOCGIFBRDADDR:
  2733. case SIOCSIFBRDADDR:
  2734. case SIOCGIFDSTADDR:
  2735. case SIOCSIFDSTADDR:
  2736. case SIOCGIFNETMASK:
  2737. case SIOCSIFNETMASK:
  2738. case SIOCSIFPFLAGS:
  2739. case SIOCGIFPFLAGS:
  2740. case SIOCGIFTXQLEN:
  2741. case SIOCSIFTXQLEN:
  2742. case SIOCBRADDIF:
  2743. case SIOCBRDELIF:
  2744. case SIOCSIFNAME:
  2745. case SIOCGMIIPHY:
  2746. case SIOCGMIIREG:
  2747. case SIOCSMIIREG:
  2748. return dev_ifsioc(net, sock, cmd, argp);
  2749. case SIOCSARP:
  2750. case SIOCGARP:
  2751. case SIOCDARP:
  2752. case SIOCATMARK:
  2753. return sock_do_ioctl(net, sock, cmd, arg);
  2754. }
  2755. /* Prevent warning from compat_sys_ioctl, these always
  2756. * result in -EINVAL in the native case anyway. */
  2757. switch (cmd) {
  2758. case SIOCRTMSG:
  2759. case SIOCGIFCOUNT:
  2760. case SIOCSRARP:
  2761. case SIOCGRARP:
  2762. case SIOCDRARP:
  2763. case SIOCSIFLINK:
  2764. case SIOCGIFSLAVE:
  2765. case SIOCSIFSLAVE:
  2766. return -EINVAL;
  2767. }
  2768. return -ENOIOCTLCMD;
  2769. }
  2770. static long compat_sock_ioctl(struct file *file, unsigned cmd,
  2771. unsigned long arg)
  2772. {
  2773. struct socket *sock = file->private_data;
  2774. int ret = -ENOIOCTLCMD;
  2775. struct sock *sk;
  2776. struct net *net;
  2777. sk = sock->sk;
  2778. net = sock_net(sk);
  2779. if (sock->ops->compat_ioctl)
  2780. ret = sock->ops->compat_ioctl(sock, cmd, arg);
  2781. if (ret == -ENOIOCTLCMD &&
  2782. (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
  2783. ret = compat_wext_handle_ioctl(net, cmd, arg);
  2784. if (ret == -ENOIOCTLCMD)
  2785. ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
  2786. return ret;
  2787. }
  2788. #endif
  2789. int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
  2790. {
  2791. return sock->ops->bind(sock, addr, addrlen);
  2792. }
  2793. EXPORT_SYMBOL(kernel_bind);
  2794. int kernel_listen(struct socket *sock, int backlog)
  2795. {
  2796. return sock->ops->listen(sock, backlog);
  2797. }
  2798. EXPORT_SYMBOL(kernel_listen);
  2799. int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
  2800. {
  2801. struct sock *sk = sock->sk;
  2802. int err;
  2803. err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
  2804. newsock);
  2805. if (err < 0)
  2806. goto done;
  2807. err = sock->ops->accept(sock, *newsock, flags);
  2808. if (err < 0) {
  2809. sock_release(*newsock);
  2810. *newsock = NULL;
  2811. goto done;
  2812. }
  2813. (*newsock)->ops = sock->ops;
  2814. __module_get((*newsock)->ops->owner);
  2815. done:
  2816. return err;
  2817. }
  2818. EXPORT_SYMBOL(kernel_accept);
  2819. int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
  2820. int flags)
  2821. {
  2822. return sock->ops->connect(sock, addr, addrlen, flags);
  2823. }
  2824. EXPORT_SYMBOL(kernel_connect);
  2825. int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
  2826. int *addrlen)
  2827. {
  2828. return sock->ops->getname(sock, addr, addrlen, 0);
  2829. }
  2830. EXPORT_SYMBOL(kernel_getsockname);
  2831. int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
  2832. int *addrlen)
  2833. {
  2834. return sock->ops->getname(sock, addr, addrlen, 1);
  2835. }
  2836. EXPORT_SYMBOL(kernel_getpeername);
  2837. int kernel_getsockopt(struct socket *sock, int level, int optname,
  2838. char *optval, int *optlen)
  2839. {
  2840. mm_segment_t oldfs = get_fs();
  2841. char __user *uoptval;
  2842. int __user *uoptlen;
  2843. int err;
  2844. uoptval = (char __user __force *) optval;
  2845. uoptlen = (int __user __force *) optlen;
  2846. set_fs(KERNEL_DS);
  2847. if (level == SOL_SOCKET)
  2848. err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
  2849. else
  2850. err = sock->ops->getsockopt(sock, level, optname, uoptval,
  2851. uoptlen);
  2852. set_fs(oldfs);
  2853. return err;
  2854. }
  2855. EXPORT_SYMBOL(kernel_getsockopt);
  2856. int kernel_setsockopt(struct socket *sock, int level, int optname,
  2857. char *optval, unsigned int optlen)
  2858. {
  2859. mm_segment_t oldfs = get_fs();
  2860. char __user *uoptval;
  2861. int err;
  2862. uoptval = (char __user __force *) optval;
  2863. set_fs(KERNEL_DS);
  2864. if (level == SOL_SOCKET)
  2865. err = sock_setsockopt(sock, level, optname, uoptval, optlen);
  2866. else
  2867. err = sock->ops->setsockopt(sock, level, optname, uoptval,
  2868. optlen);
  2869. set_fs(oldfs);
  2870. return err;
  2871. }
  2872. EXPORT_SYMBOL(kernel_setsockopt);
  2873. int kernel_sendpage(struct socket *sock, struct page *page, int offset,
  2874. size_t size, int flags)
  2875. {
  2876. sock_update_classid(sock->sk);
  2877. if (sock->ops->sendpage)
  2878. return sock->ops->sendpage(sock, page, offset, size, flags);
  2879. return sock_no_sendpage(sock, page, offset, size, flags);
  2880. }
  2881. EXPORT_SYMBOL(kernel_sendpage);
  2882. int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
  2883. {
  2884. mm_segment_t oldfs = get_fs();
  2885. int err;
  2886. set_fs(KERNEL_DS);
  2887. err = sock->ops->ioctl(sock, cmd, arg);
  2888. set_fs(oldfs);
  2889. return err;
  2890. }
  2891. EXPORT_SYMBOL(kernel_sock_ioctl);
  2892. int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
  2893. {
  2894. return sock->ops->shutdown(sock, how);
  2895. }
  2896. EXPORT_SYMBOL(kernel_sock_shutdown);