PageRenderTime 73ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/net/socket.c

https://bitbucket.org/codefirex/kernel_lge_geehrc
C | 3389 lines | 2435 code | 488 blank | 466 comment | 379 complexity | 4a720c5061cc603e74a82d0c95856089 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. 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 (ccs_socket_post_accept_permission(sock, newsock)) {
  1307. err = -EAGAIN; /* Hope less harmful than -EPERM. */
  1308. goto out_fd;
  1309. }
  1310. if (upeer_sockaddr) {
  1311. if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
  1312. &len, 2) < 0) {
  1313. err = -ECONNABORTED;
  1314. goto out_fd;
  1315. }
  1316. err = move_addr_to_user((struct sockaddr *)&address,
  1317. len, upeer_sockaddr, upeer_addrlen);
  1318. if (err < 0)
  1319. goto out_fd;
  1320. }
  1321. /* File flags are not inherited via accept() unlike another OSes. */
  1322. fd_install(newfd, newfile);
  1323. err = newfd;
  1324. out_put:
  1325. fput_light(sock->file, fput_needed);
  1326. out:
  1327. return err;
  1328. out_fd:
  1329. fput(newfile);
  1330. put_unused_fd(newfd);
  1331. goto out_put;
  1332. }
  1333. SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
  1334. int __user *, upeer_addrlen)
  1335. {
  1336. return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
  1337. }
  1338. /*
  1339. * Attempt to connect to a socket with the server address. The address
  1340. * is in user space so we verify it is OK and move it to kernel space.
  1341. *
  1342. * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
  1343. * break bindings
  1344. *
  1345. * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
  1346. * other SEQPACKET protocols that take time to connect() as it doesn't
  1347. * include the -EINPROGRESS status for such sockets.
  1348. */
  1349. SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
  1350. int, addrlen)
  1351. {
  1352. struct socket *sock;
  1353. struct sockaddr_storage address;
  1354. int err, fput_needed;
  1355. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1356. if (!sock)
  1357. goto out;
  1358. err = move_addr_to_kernel(uservaddr, addrlen, (struct sockaddr *)&address);
  1359. if (err < 0)
  1360. goto out_put;
  1361. err =
  1362. security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
  1363. if (err)
  1364. goto out_put;
  1365. err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
  1366. sock->file->f_flags);
  1367. out_put:
  1368. fput_light(sock->file, fput_needed);
  1369. out:
  1370. return err;
  1371. }
  1372. /*
  1373. * Get the local address ('name') of a socket object. Move the obtained
  1374. * name to user space.
  1375. */
  1376. SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
  1377. int __user *, usockaddr_len)
  1378. {
  1379. struct socket *sock;
  1380. struct sockaddr_storage address;
  1381. int len, err, fput_needed;
  1382. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1383. if (!sock)
  1384. goto out;
  1385. err = security_socket_getsockname(sock);
  1386. if (err)
  1387. goto out_put;
  1388. err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
  1389. if (err)
  1390. goto out_put;
  1391. err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr, usockaddr_len);
  1392. out_put:
  1393. fput_light(sock->file, fput_needed);
  1394. out:
  1395. return err;
  1396. }
  1397. /*
  1398. * Get the remote address ('name') of a socket object. Move the obtained
  1399. * name to user space.
  1400. */
  1401. SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
  1402. int __user *, usockaddr_len)
  1403. {
  1404. struct socket *sock;
  1405. struct sockaddr_storage address;
  1406. int len, err, fput_needed;
  1407. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1408. if (sock != NULL) {
  1409. err = security_socket_getpeername(sock);
  1410. if (err) {
  1411. fput_light(sock->file, fput_needed);
  1412. return err;
  1413. }
  1414. err =
  1415. sock->ops->getname(sock, (struct sockaddr *)&address, &len,
  1416. 1);
  1417. if (!err)
  1418. err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr,
  1419. usockaddr_len);
  1420. fput_light(sock->file, fput_needed);
  1421. }
  1422. return err;
  1423. }
  1424. /*
  1425. * Send a datagram to a given address. We move the address into kernel
  1426. * space and check the user space data area is readable before invoking
  1427. * the protocol.
  1428. */
  1429. SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
  1430. unsigned, flags, struct sockaddr __user *, addr,
  1431. int, addr_len)
  1432. {
  1433. struct socket *sock;
  1434. struct sockaddr_storage address;
  1435. int err;
  1436. struct msghdr msg;
  1437. struct iovec iov;
  1438. int fput_needed;
  1439. if (len > INT_MAX)
  1440. len = INT_MAX;
  1441. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1442. if (!sock)
  1443. goto out;
  1444. iov.iov_base = buff;
  1445. iov.iov_len = len;
  1446. msg.msg_name = NULL;
  1447. msg.msg_iov = &iov;
  1448. msg.msg_iovlen = 1;
  1449. msg.msg_control = NULL;
  1450. msg.msg_controllen = 0;
  1451. msg.msg_namelen = 0;
  1452. if (addr) {
  1453. err = move_addr_to_kernel(addr, addr_len, (struct sockaddr *)&address);
  1454. if (err < 0)
  1455. goto out_put;
  1456. msg.msg_name = (struct sockaddr *)&address;
  1457. msg.msg_namelen = addr_len;
  1458. }
  1459. if (sock->file->f_flags & O_NONBLOCK)
  1460. flags |= MSG_DONTWAIT;
  1461. msg.msg_flags = flags;
  1462. err = sock_sendmsg(sock, &msg, len);
  1463. out_put:
  1464. fput_light(sock->file, fput_needed);
  1465. out:
  1466. return err;
  1467. }
  1468. /*
  1469. * Send a datagram down a socket.
  1470. */
  1471. SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
  1472. unsigned, flags)
  1473. {
  1474. return sys_sendto(fd, buff, len, flags, NULL, 0);
  1475. }
  1476. /*
  1477. * Receive a frame from the socket and optionally record the address of the
  1478. * sender. We verify the buffers are writable and if needed move the
  1479. * sender address from kernel to user space.
  1480. */
  1481. SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
  1482. unsigned, flags, struct sockaddr __user *, addr,
  1483. int __user *, addr_len)
  1484. {
  1485. struct socket *sock;
  1486. struct iovec iov;
  1487. struct msghdr msg;
  1488. struct sockaddr_storage address;
  1489. int err, err2;
  1490. int fput_needed;
  1491. if (size > INT_MAX)
  1492. size = INT_MAX;
  1493. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1494. if (!sock)
  1495. goto out;
  1496. msg.msg_control = NULL;
  1497. msg.msg_controllen = 0;
  1498. msg.msg_iovlen = 1;
  1499. msg.msg_iov = &iov;
  1500. iov.iov_len = size;
  1501. iov.iov_base = ubuf;
  1502. msg.msg_name = (struct sockaddr *)&address;
  1503. msg.msg_namelen = sizeof(address);
  1504. if (sock->file->f_flags & O_NONBLOCK)
  1505. flags |= MSG_DONTWAIT;
  1506. err = sock_recvmsg(sock, &msg, size, flags);
  1507. if (err >= 0 && addr != NULL) {
  1508. err2 = move_addr_to_user((struct sockaddr *)&address,
  1509. msg.msg_namelen, addr, addr_len);
  1510. if (err2 < 0)
  1511. err = err2;
  1512. }
  1513. fput_light(sock->file, fput_needed);
  1514. out:
  1515. return err;
  1516. }
  1517. /*
  1518. * Receive a datagram from a socket.
  1519. */
  1520. asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
  1521. unsigned flags)
  1522. {
  1523. return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
  1524. }
  1525. /*
  1526. * Set a socket option. Because we don't know the option lengths we have
  1527. * to pass the user mode parameter for the protocols to sort out.
  1528. */
  1529. SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
  1530. char __user *, optval, int, optlen)
  1531. {
  1532. int err, fput_needed;
  1533. struct socket *sock;
  1534. if (optlen < 0)
  1535. return -EINVAL;
  1536. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1537. if (sock != NULL) {
  1538. err = security_socket_setsockopt(sock, level, optname);
  1539. if (err)
  1540. goto out_put;
  1541. if (level == SOL_SOCKET)
  1542. err =
  1543. sock_setsockopt(sock, level, optname, optval,
  1544. optlen);
  1545. else
  1546. err =
  1547. sock->ops->setsockopt(sock, level, optname, optval,
  1548. optlen);
  1549. out_put:
  1550. fput_light(sock->file, fput_needed);
  1551. }
  1552. return err;
  1553. }
  1554. /*
  1555. * Get a socket option. Because we don't know the option lengths we have
  1556. * to pass a user mode parameter for the protocols to sort out.
  1557. */
  1558. SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
  1559. char __user *, optval, int __user *, optlen)
  1560. {
  1561. int err, fput_needed;
  1562. struct socket *sock;
  1563. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1564. if (sock != NULL) {
  1565. err = security_socket_getsockopt(sock, level, optname);
  1566. if (err)
  1567. goto out_put;
  1568. if (level == SOL_SOCKET)
  1569. err =
  1570. sock_getsockopt(sock, level, optname, optval,
  1571. optlen);
  1572. else
  1573. err =
  1574. sock->ops->getsockopt(sock, level, optname, optval,
  1575. optlen);
  1576. out_put:
  1577. fput_light(sock->file, fput_needed);
  1578. }
  1579. return err;
  1580. }
  1581. /*
  1582. * Shutdown a socket.
  1583. */
  1584. SYSCALL_DEFINE2(shutdown, int, fd, int, how)
  1585. {
  1586. int err, fput_needed;
  1587. struct socket *sock;
  1588. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1589. if (sock != NULL) {
  1590. err = security_socket_shutdown(sock, how);
  1591. if (!err)
  1592. err = sock->ops->shutdown(sock, how);
  1593. fput_light(sock->file, fput_needed);
  1594. }
  1595. return err;
  1596. }
  1597. /* A couple of helpful macros for getting the address of the 32/64 bit
  1598. * fields which are the same type (int / unsigned) on our platforms.
  1599. */
  1600. #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
  1601. #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
  1602. #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
  1603. struct used_address {
  1604. struct sockaddr_storage name;
  1605. unsigned int name_len;
  1606. };
  1607. static int __sys_sendmsg(struct socket *sock, struct msghdr __user *msg,
  1608. struct msghdr *msg_sys, unsigned flags,
  1609. struct used_address *used_address)
  1610. {
  1611. struct compat_msghdr __user *msg_compat =
  1612. (struct compat_msghdr __user *)msg;
  1613. struct sockaddr_storage address;
  1614. struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
  1615. unsigned char ctl[sizeof(struct cmsghdr) + 20]
  1616. __attribute__ ((aligned(sizeof(__kernel_size_t))));
  1617. /* 20 is size of ipv6_pktinfo */
  1618. unsigned char *ctl_buf = ctl;
  1619. int err, ctl_len, iov_size, total_len;
  1620. err = -EFAULT;
  1621. if (MSG_CMSG_COMPAT & flags) {
  1622. if (get_compat_msghdr(msg_sys, msg_compat))
  1623. return -EFAULT;
  1624. } else if (copy_from_user(msg_sys, msg, sizeof(struct msghdr)))
  1625. return -EFAULT;
  1626. /* do not move before msg_sys is valid */
  1627. err = -EMSGSIZE;
  1628. if (msg_sys->msg_iovlen > UIO_MAXIOV)
  1629. goto out;
  1630. /* Check whether to allocate the iovec area */
  1631. err = -ENOMEM;
  1632. iov_size = msg_sys->msg_iovlen * sizeof(struct iovec);
  1633. if (msg_sys->msg_iovlen > UIO_FASTIOV) {
  1634. iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
  1635. if (!iov)
  1636. goto out;
  1637. }
  1638. /* This will also move the address data into kernel space */
  1639. if (MSG_CMSG_COMPAT & flags) {
  1640. err = verify_compat_iovec(msg_sys, iov,
  1641. (struct sockaddr *)&address,
  1642. VERIFY_READ);
  1643. } else
  1644. err = verify_iovec(msg_sys, iov,
  1645. (struct sockaddr *)&address,
  1646. VERIFY_READ);
  1647. if (err < 0)
  1648. goto out_freeiov;
  1649. total_len = err;
  1650. err = -ENOBUFS;
  1651. if (msg_sys->msg_controllen > INT_MAX)
  1652. goto out_freeiov;
  1653. ctl_len = msg_sys->msg_controllen;
  1654. if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
  1655. err =
  1656. cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
  1657. sizeof(ctl));
  1658. if (err)
  1659. goto out_freeiov;
  1660. ctl_buf = msg_sys->msg_control;
  1661. ctl_len = msg_sys->msg_controllen;
  1662. } else if (ctl_len) {
  1663. if (ctl_len > sizeof(ctl)) {
  1664. ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
  1665. if (ctl_buf == NULL)
  1666. goto out_freeiov;
  1667. }
  1668. err = -EFAULT;
  1669. /*
  1670. * Careful! Before this, msg_sys->msg_control contains a user pointer.
  1671. * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
  1672. * checking falls down on this.
  1673. */
  1674. if (copy_from_user(ctl_buf,
  1675. (void __user __force *)msg_sys->msg_control,
  1676. ctl_len))
  1677. goto out_freectl;
  1678. msg_sys->msg_control = ctl_buf;
  1679. }
  1680. msg_sys->msg_flags = flags;
  1681. if (sock->file->f_flags & O_NONBLOCK)
  1682. msg_sys->msg_flags |= MSG_DONTWAIT;
  1683. /*
  1684. * If this is sendmmsg() and current destination address is same as
  1685. * previously succeeded address, omit asking LSM's decision.
  1686. * used_address->name_len is initialized to UINT_MAX so that the first
  1687. * destination address never matches.
  1688. */
  1689. if (used_address && msg_sys->msg_name &&
  1690. used_address->name_len == msg_sys->msg_namelen &&
  1691. !memcmp(&used_address->name, msg_sys->msg_name,
  1692. used_address->name_len)) {
  1693. err = sock_sendmsg_nosec(sock, msg_sys, total_len);
  1694. goto out_freectl;
  1695. }
  1696. err = sock_sendmsg(sock, msg_sys, total_len);
  1697. /*
  1698. * If this is sendmmsg() and sending to current destination address was
  1699. * successful, remember it.
  1700. */
  1701. if (used_address && err >= 0) {
  1702. used_address->name_len = msg_sys->msg_namelen;
  1703. if (msg_sys->msg_name)
  1704. memcpy(&used_address->name, msg_sys->msg_name,
  1705. used_address->name_len);
  1706. }
  1707. out_freectl:
  1708. if (ctl_buf != ctl)
  1709. sock_kfree_s(sock->sk, ctl_buf, ctl_len);
  1710. out_freeiov:
  1711. if (iov != iovstack)
  1712. sock_kfree_s(sock->sk, iov, iov_size);
  1713. out:
  1714. return err;
  1715. }
  1716. /*
  1717. * BSD sendmsg interface
  1718. */
  1719. SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned, flags)
  1720. {
  1721. int fput_needed, err;
  1722. struct msghdr msg_sys;
  1723. struct socket *sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1724. if (!sock)
  1725. goto out;
  1726. err = __sys_sendmsg(sock, msg, &msg_sys, flags, NULL);
  1727. fput_light(sock->file, fput_needed);
  1728. out:
  1729. return err;
  1730. }
  1731. /*
  1732. * Linux sendmmsg interface
  1733. */
  1734. int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
  1735. unsigned int flags)
  1736. {
  1737. int fput_needed, err, datagrams;
  1738. struct socket *sock;
  1739. struct mmsghdr __user *entry;
  1740. struct compat_mmsghdr __user *compat_entry;
  1741. struct msghdr msg_sys;
  1742. struct used_address used_address;
  1743. if (vlen > UIO_MAXIOV)
  1744. vlen = UIO_MAXIOV;
  1745. datagrams = 0;
  1746. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1747. if (!sock)
  1748. return err;
  1749. used_address.name_len = UINT_MAX;
  1750. entry = mmsg;
  1751. compat_entry = (struct compat_mmsghdr __user *)mmsg;
  1752. err = 0;
  1753. while (datagrams < vlen) {
  1754. if (MSG_CMSG_COMPAT & flags) {
  1755. err = __sys_sendmsg(sock, (struct msghdr __user *)compat_entry,
  1756. &msg_sys, flags, &used_address);
  1757. if (err < 0)
  1758. break;
  1759. err = __put_user(err, &compat_entry->msg_len);
  1760. ++compat_entry;
  1761. } else {
  1762. err = __sys_sendmsg(sock, (struct msghdr __user *)entry,
  1763. &msg_sys, flags, &used_address);
  1764. if (err < 0)
  1765. break;
  1766. err = put_user(err, &entry->msg_len);
  1767. ++entry;
  1768. }
  1769. if (err)
  1770. break;
  1771. ++datagrams;
  1772. }
  1773. fput_light(sock->file, fput_needed);
  1774. /* We only return an error if no datagrams were able to be sent */
  1775. if (datagrams != 0)
  1776. return datagrams;
  1777. return err;
  1778. }
  1779. SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
  1780. unsigned int, vlen, unsigned int, flags)
  1781. {
  1782. return __sys_sendmmsg(fd, mmsg, vlen, flags);
  1783. }
  1784. static int __sys_recvmsg(struct socket *sock, struct msghdr __user *msg,
  1785. struct msghdr *msg_sys, unsigned flags, int nosec)
  1786. {
  1787. struct compat_msghdr __user *msg_compat =
  1788. (struct compat_msghdr __user *)msg;
  1789. struct iovec iovstack[UIO_FASTIOV];
  1790. struct iovec *iov = iovstack;
  1791. unsigned long cmsg_ptr;
  1792. int err, iov_size, total_len, len;
  1793. /* kernel mode address */
  1794. struct sockaddr_storage addr;
  1795. /* user mode address pointers */
  1796. struct sockaddr __user *uaddr;
  1797. int __user *uaddr_len;
  1798. if (MSG_CMSG_COMPAT & flags) {
  1799. if (get_compat_msghdr(msg_sys, msg_compat))
  1800. return -EFAULT;
  1801. } else if (copy_from_user(msg_sys, msg, sizeof(struct msghdr)))
  1802. return -EFAULT;
  1803. err = -EMSGSIZE;
  1804. if (msg_sys->msg_iovlen > UIO_MAXIOV)
  1805. goto out;
  1806. /* Check whether to allocate the iovec area */
  1807. err = -ENOMEM;
  1808. iov_size = msg_sys->msg_iovlen * sizeof(struct iovec);
  1809. if (msg_sys->msg_iovlen > UIO_FASTIOV) {
  1810. iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
  1811. if (!iov)
  1812. goto out;
  1813. }
  1814. /*
  1815. * Save the user-mode address (verify_iovec will change the
  1816. * kernel msghdr to use the kernel address space)
  1817. */
  1818. uaddr = (__force void __user *)msg_sys->msg_name;
  1819. uaddr_len = COMPAT_NAMELEN(msg);
  1820. if (MSG_CMSG_COMPAT & flags) {
  1821. err = verify_compat_iovec(msg_sys, iov,
  1822. (struct sockaddr *)&addr,
  1823. VERIFY_WRITE);
  1824. } else
  1825. err = verify_iovec(msg_sys, iov,
  1826. (struct sockaddr *)&addr,
  1827. VERIFY_WRITE);
  1828. if (err < 0)
  1829. goto out_freeiov;
  1830. total_len = err;
  1831. cmsg_ptr = (unsigned long)msg_sys->msg_control;
  1832. msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
  1833. if (sock->file->f_flags & O_NONBLOCK)
  1834. flags |= MSG_DONTWAIT;
  1835. err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
  1836. total_len, flags);
  1837. if (err < 0)
  1838. goto out_freeiov;
  1839. len = err;
  1840. if (uaddr != NULL) {
  1841. err = move_addr_to_user((struct sockaddr *)&addr,
  1842. msg_sys->msg_namelen, uaddr,
  1843. uaddr_len);
  1844. if (err < 0)
  1845. goto out_freeiov;
  1846. }
  1847. err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
  1848. COMPAT_FLAGS(msg));
  1849. if (err)
  1850. goto out_freeiov;
  1851. if (MSG_CMSG_COMPAT & flags)
  1852. err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
  1853. &msg_compat->msg_controllen);
  1854. else
  1855. err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
  1856. &msg->msg_controllen);
  1857. if (err)
  1858. goto out_freeiov;
  1859. err = len;
  1860. out_freeiov:
  1861. if (iov != iovstack)
  1862. sock_kfree_s(sock->sk, iov, iov_size);
  1863. out:
  1864. return err;
  1865. }
  1866. /*
  1867. * BSD recvmsg interface
  1868. */
  1869. SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
  1870. unsigned int, flags)
  1871. {
  1872. int fput_needed, err;
  1873. struct msghdr msg_sys;
  1874. struct socket *sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1875. if (!sock)
  1876. goto out;
  1877. err = __sys_recvmsg(sock, msg, &msg_sys, flags, 0);
  1878. fput_light(sock->file, fput_needed);
  1879. out:
  1880. return err;
  1881. }
  1882. /*
  1883. * Linux recvmmsg interface
  1884. */
  1885. int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
  1886. unsigned int flags, struct timespec *timeout)
  1887. {
  1888. int fput_needed, err, datagrams;
  1889. struct socket *sock;
  1890. struct mmsghdr __user *entry;
  1891. struct compat_mmsghdr __user *compat_entry;
  1892. struct msghdr msg_sys;
  1893. struct timespec end_time;
  1894. if (timeout &&
  1895. poll_select_set_timeout(&end_time, timeout->tv_sec,
  1896. timeout->tv_nsec))
  1897. return -EINVAL;
  1898. datagrams = 0;
  1899. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1900. if (!sock)
  1901. return err;
  1902. err = sock_error(sock->sk);
  1903. if (err)
  1904. goto out_put;
  1905. entry = mmsg;
  1906. compat_entry = (struct compat_mmsghdr __user *)mmsg;
  1907. while (datagrams < vlen) {
  1908. /*
  1909. * No need to ask LSM for more than the first datagram.
  1910. */
  1911. if (MSG_CMSG_COMPAT & flags) {
  1912. err = __sys_recvmsg(sock, (struct msghdr __user *)compat_entry,
  1913. &msg_sys, flags & ~MSG_WAITFORONE,
  1914. datagrams);
  1915. if (err < 0)
  1916. break;
  1917. err = __put_user(err, &compat_entry->msg_len);
  1918. ++compat_entry;
  1919. } else {
  1920. err = __sys_recvmsg(sock, (struct msghdr __user *)entry,
  1921. &msg_sys, flags & ~MSG_WAITFORONE,
  1922. datagrams);
  1923. if (err < 0)
  1924. break;
  1925. err = put_user(err, &entry->msg_len);
  1926. ++entry;
  1927. }
  1928. if (err)
  1929. break;
  1930. ++datagrams;
  1931. /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
  1932. if (flags & MSG_WAITFORONE)
  1933. flags |= MSG_DONTWAIT;
  1934. if (timeout) {
  1935. ktime_get_ts(timeout);
  1936. *timeout = timespec_sub(end_time, *timeout);
  1937. if (timeout->tv_sec < 0) {
  1938. timeout->tv_sec = timeout->tv_nsec = 0;
  1939. break;
  1940. }
  1941. /* Timeout, return less than vlen datagrams */
  1942. if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
  1943. break;
  1944. }
  1945. /* Out of band data, return right away */
  1946. if (msg_sys.msg_flags & MSG_OOB)
  1947. break;
  1948. }
  1949. out_put:
  1950. fput_light(sock->file, fput_needed);
  1951. if (err == 0)
  1952. return datagrams;
  1953. if (datagrams != 0) {
  1954. /*
  1955. * We may return less entries than requested (vlen) if the
  1956. * sock is non block and there aren't enough datagrams...
  1957. */
  1958. if (err != -EAGAIN) {
  1959. /*
  1960. * ... or if recvmsg returns an error after we
  1961. * received some datagrams, where we record the
  1962. * error to return on the next call or if the
  1963. * app asks about it using getsockopt(SO_ERROR).
  1964. */
  1965. sock->sk->sk_err = -err;
  1966. }
  1967. return datagrams;
  1968. }
  1969. return err;
  1970. }
  1971. SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
  1972. unsigned int, vlen, unsigned int, flags,
  1973. struct timespec __user *, timeout)
  1974. {
  1975. int datagrams;
  1976. struct timespec timeout_sys;
  1977. if (!timeout)
  1978. return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
  1979. if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
  1980. return -EFAULT;
  1981. datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
  1982. if (datagrams > 0 &&
  1983. copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
  1984. datagrams = -EFAULT;
  1985. return datagrams;
  1986. }
  1987. #ifdef __ARCH_WANT_SYS_SOCKETCALL
  1988. /* Argument list sizes for sys_socketcall */
  1989. #define AL(x) ((x) * sizeof(unsigned long))
  1990. static const unsigned char nargs[21] = {
  1991. AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
  1992. AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
  1993. AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
  1994. AL(4), AL(5), AL(4)
  1995. };
  1996. #undef AL
  1997. /*
  1998. * System call vectors.
  1999. *
  2000. * Argument checking cleaned up. Saved 20% in size.
  2001. * This function doesn't need to set the kernel lock because
  2002. * it is set by the callees.
  2003. */
  2004. SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
  2005. {
  2006. unsigned long a[6];
  2007. unsigned long a0, a1;
  2008. int err;
  2009. unsigned int len;
  2010. if (call < 1 || call > SYS_SENDMMSG)
  2011. return -EINVAL;
  2012. len = nargs[call];
  2013. if (len > sizeof(a))
  2014. return -EINVAL;
  2015. /* copy_from_user should be SMP safe. */
  2016. if (copy_from_user(a, args, len))
  2017. return -EFAULT;
  2018. audit_socketcall(nargs[call] / sizeof(unsigned long), a);
  2019. a0 = a[0];
  2020. a1 = a[1];
  2021. switch (call) {
  2022. case SYS_SOCKET:
  2023. err = sys_socket(a0, a1, a[2]);
  2024. break;
  2025. case SYS_BIND:
  2026. err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
  2027. break;
  2028. case SYS_CONNECT:
  2029. err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
  2030. break;
  2031. case SYS_LISTEN:
  2032. err = sys_listen(a0, a1);
  2033. break;
  2034. case SYS_ACCEPT:
  2035. err = sys_accept4(a0, (struct sockaddr __user *)a1,
  2036. (int __user *)a[2], 0);
  2037. break;
  2038. case SYS_GETSOCKNAME:
  2039. err =
  2040. sys_getsockname(a0, (struct sockaddr __user *)a1,
  2041. (int __user *)a[2]);
  2042. break;
  2043. case SYS_GETPEERNAME:
  2044. err =
  2045. sys_getpeername(a0, (struct sockaddr __user *)a1,
  2046. (int __user *)a[2]);
  2047. break;
  2048. case SYS_SOCKETPAIR:
  2049. err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
  2050. break;
  2051. case SYS_SEND:
  2052. err = sys_send(a0, (void __user *)a1, a[2], a[3]);
  2053. break;
  2054. case SYS_SENDTO:
  2055. err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
  2056. (struct sockaddr __user *)a[4], a[5]);
  2057. break;
  2058. case SYS_RECV:
  2059. err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
  2060. break;
  2061. case SYS_RECVFROM:
  2062. err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
  2063. (struct sockaddr __user *)a[4],
  2064. (int __user *)a[5]);
  2065. break;
  2066. case SYS_SHUTDOWN:
  2067. err = sys_shutdown(a0, a1);
  2068. break;
  2069. case SYS_SETSOCKOPT:
  2070. err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
  2071. break;
  2072. case SYS_GETSOCKOPT:
  2073. err =
  2074. sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
  2075. (int __user *)a[4]);
  2076. break;
  2077. case SYS_SENDMSG:
  2078. err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
  2079. break;
  2080. case SYS_SENDMMSG:
  2081. err = sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3]);
  2082. break;
  2083. case SYS_RECVMSG:
  2084. err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
  2085. break;
  2086. case SYS_RECVMMSG:
  2087. err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
  2088. (struct timespec __user *)a[4]);
  2089. break;
  2090. case SYS_ACCEPT4:
  2091. err = sys_accept4(a0, (struct sockaddr __user *)a1,
  2092. (int __user *)a[2], a[3]);
  2093. break;
  2094. default:
  2095. err = -EINVAL;
  2096. break;
  2097. }
  2098. return err;
  2099. }
  2100. #endif /* __ARCH_WANT_SYS_SOCKETCALL */
  2101. /**
  2102. * sock_register - add a socket protocol handler
  2103. * @ops: description of protocol
  2104. *
  2105. * This function is called by a protocol handler that wants to
  2106. * advertise its address family, and have it linked into the
  2107. * socket interface. The value ops->family coresponds to the
  2108. * socket system call protocol family.
  2109. */
  2110. int sock_register(const struct net_proto_family *ops)
  2111. {
  2112. int err;
  2113. if (ops->family >= NPROTO) {
  2114. printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
  2115. NPROTO);
  2116. return -ENOBUFS;
  2117. }
  2118. spin_lock(&net_family_lock);
  2119. if (rcu_dereference_protected(net_families[ops->family],
  2120. lockdep_is_held(&net_family_lock)))
  2121. err = -EEXIST;
  2122. else {
  2123. rcu_assign_pointer(net_families[ops->family], ops);
  2124. err = 0;
  2125. }
  2126. spin_unlock(&net_family_lock);
  2127. printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
  2128. return err;
  2129. }
  2130. EXPORT_SYMBOL(sock_register);
  2131. /**
  2132. * sock_unregister - remove a protocol handler
  2133. * @family: protocol family to remove
  2134. *
  2135. * This function is called by a protocol handler that wants to
  2136. * remove its address family, and have it unlinked from the
  2137. * new socket creation.
  2138. *
  2139. * If protocol handler is a module, then it can use module reference
  2140. * counts to protect against new references. If protocol handler is not
  2141. * a module then it needs to provide its own protection in
  2142. * the ops->create routine.
  2143. */
  2144. void sock_unregister(int family)
  2145. {
  2146. BUG_ON(family < 0 || family >= NPROTO);
  2147. spin_lock(&net_family_lock);
  2148. rcu_assign_pointer(net_families[family], NULL);
  2149. spin_unlock(&net_family_lock);
  2150. synchronize_rcu();
  2151. printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
  2152. }
  2153. EXPORT_SYMBOL(sock_unregister);
  2154. static int __init sock_init(void)
  2155. {
  2156. int err;
  2157. /*
  2158. * Initialize sock SLAB cache.
  2159. */
  2160. sk_init();
  2161. /*
  2162. * Initialize skbuff SLAB cache
  2163. */
  2164. skb_init();
  2165. /*
  2166. * Initialize the protocols module.
  2167. */
  2168. init_inodecache();
  2169. err = register_filesystem(&sock_fs_type);
  2170. if (err)
  2171. goto out_fs;
  2172. sock_mnt = kern_mount(&sock_fs_type);
  2173. if (IS_ERR(sock_mnt)) {
  2174. err = PTR_ERR(sock_mnt);
  2175. goto out_mount;
  2176. }
  2177. /* The real protocol initialization is performed in later initcalls.
  2178. */
  2179. #ifdef CONFIG_NETFILTER
  2180. netfilter_init();
  2181. #endif
  2182. #ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
  2183. skb_timestamping_init();
  2184. #endif
  2185. out:
  2186. return err;
  2187. out_mount:
  2188. unregister_filesystem(&sock_fs_type);
  2189. out_fs:
  2190. goto out;
  2191. }
  2192. core_initcall(sock_init); /* early initcall */
  2193. #ifdef CONFIG_PROC_FS
  2194. void socket_seq_show(struct seq_file *seq)
  2195. {
  2196. int cpu;
  2197. int counter = 0;
  2198. for_each_possible_cpu(cpu)
  2199. counter += per_cpu(sockets_in_use, cpu);
  2200. /* It can be negative, by the way. 8) */
  2201. if (counter < 0)
  2202. counter = 0;
  2203. seq_printf(seq, "sockets: used %d\n", counter);
  2204. }
  2205. #endif /* CONFIG_PROC_FS */
  2206. #ifdef CONFIG_COMPAT
  2207. static int do_siocgstamp(struct net *net, struct socket *sock,
  2208. unsigned int cmd, struct compat_timeval __user *up)
  2209. {
  2210. mm_segment_t old_fs = get_fs();
  2211. struct timeval ktv;
  2212. int err;
  2213. set_fs(KERNEL_DS);
  2214. err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
  2215. set_fs(old_fs);
  2216. if (!err) {
  2217. err = put_user(ktv.tv_sec, &up->tv_sec);
  2218. err |= __put_user(ktv.tv_usec, &up->tv_usec);
  2219. }
  2220. return err;
  2221. }
  2222. static int do_siocgstampns(struct net *net, struct socket *sock,
  2223. unsigned int cmd, struct compat_timespec __user *up)
  2224. {
  2225. mm_segment_t old_fs = get_fs();
  2226. struct timespec kts;
  2227. int err;
  2228. set_fs(KERNEL_DS);
  2229. err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
  2230. set_fs(old_fs);
  2231. if (!err) {
  2232. err = put_user(kts.tv_sec, &up->tv_sec);
  2233. err |= __put_user(kts.tv_nsec, &up->tv_nsec);
  2234. }
  2235. return err;
  2236. }
  2237. static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
  2238. {
  2239. struct ifreq __user *uifr;
  2240. int err;
  2241. uifr = compat_alloc_user_space(sizeof(struct ifreq));
  2242. if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
  2243. return -EFAULT;
  2244. err = dev_ioctl(net, SIOCGIFNAME, uifr);
  2245. if (err)
  2246. return err;
  2247. if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
  2248. return -EFAULT;
  2249. return 0;
  2250. }
  2251. static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
  2252. {
  2253. struct compat_ifconf ifc32;
  2254. struct ifconf ifc;
  2255. struct ifconf __user *uifc;
  2256. struct compat_ifreq __user *ifr32;
  2257. struct ifreq __user *ifr;
  2258. unsigned int i, j;
  2259. int err;
  2260. if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
  2261. return -EFAULT;
  2262. memset(&ifc, 0, sizeof(ifc));
  2263. if (ifc32.ifcbuf == 0) {
  2264. ifc32.ifc_len = 0;
  2265. ifc.ifc_len = 0;
  2266. ifc.ifc_req = NULL;
  2267. uifc = compat_alloc_user_space(sizeof(struct ifconf));
  2268. } else {
  2269. size_t len = ((ifc32.ifc_len / sizeof(struct compat_ifreq)) + 1) *
  2270. sizeof(struct ifreq);
  2271. uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
  2272. ifc.ifc_len = len;
  2273. ifr = ifc.ifc_req = (void __user *)(uifc + 1);
  2274. ifr32 = compat_ptr(ifc32.ifcbuf);
  2275. for (i = 0; i < ifc32.ifc_len; i += sizeof(struct compat_ifreq)) {
  2276. if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
  2277. return -EFAULT;
  2278. ifr++;
  2279. ifr32++;
  2280. }
  2281. }
  2282. if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
  2283. return -EFAULT;
  2284. err = dev_ioctl(net, SIOCGIFCONF, uifc);
  2285. if (err)
  2286. return err;
  2287. if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
  2288. return -EFAULT;
  2289. ifr = ifc.ifc_req;
  2290. ifr32 = compat_ptr(ifc32.ifcbuf);
  2291. for (i = 0, j = 0;
  2292. i + sizeof(struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
  2293. i += sizeof(struct compat_ifreq), j += sizeof(struct ifreq)) {
  2294. if (copy_in_user(ifr32, ifr, sizeof(struct compat_ifreq)))
  2295. return -EFAULT;
  2296. ifr32++;
  2297. ifr++;
  2298. }
  2299. if (ifc32.ifcbuf == 0) {
  2300. /* Translate from 64-bit structure multiple to
  2301. * a 32-bit one.
  2302. */
  2303. i = ifc.ifc_len;
  2304. i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
  2305. ifc32.ifc_len = i;
  2306. } else {
  2307. ifc32.ifc_len = i;
  2308. }
  2309. if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
  2310. return -EFAULT;
  2311. return 0;
  2312. }
  2313. static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
  2314. {
  2315. struct compat_ethtool_rxnfc __user *compat_rxnfc;
  2316. bool convert_in = false, convert_out = false;
  2317. size_t buf_size = ALIGN(sizeof(struct ifreq), 8);
  2318. struct ethtool_rxnfc __user *rxnfc;
  2319. struct ifreq __user *ifr;
  2320. u32 rule_cnt = 0, actual_rule_cnt;
  2321. u32 ethcmd;
  2322. u32 data;
  2323. int ret;
  2324. if (get_user(data, &ifr32->ifr_ifru.ifru_data))
  2325. return -EFAULT;
  2326. compat_rxnfc = compat_ptr(data);
  2327. if (get_user(ethcmd, &compat_rxnfc->cmd))
  2328. return -EFAULT;
  2329. /* Most ethtool structures are defined without padding.
  2330. * Unfortunately struct ethtool_rxnfc is an exception.
  2331. */
  2332. switch (ethcmd) {
  2333. default:
  2334. break;
  2335. case ETHTOOL_GRXCLSRLALL:
  2336. /* Buffer size is variable */
  2337. if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
  2338. return -EFAULT;
  2339. if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
  2340. return -ENOMEM;
  2341. buf_size += rule_cnt * sizeof(u32);
  2342. /* fall through */
  2343. case ETHTOOL_GRXRINGS:
  2344. case ETHTOOL_GRXCLSRLCNT:
  2345. case ETHTOOL_GRXCLSRULE:
  2346. convert_out = true;
  2347. /* fall through */
  2348. case ETHTOOL_SRXCLSRLDEL:
  2349. case ETHTOOL_SRXCLSRLINS:
  2350. buf_size += sizeof(struct ethtool_rxnfc);
  2351. convert_in = true;
  2352. break;
  2353. }
  2354. ifr = compat_alloc_user_space(buf_size);
  2355. rxnfc = (void *)ifr + ALIGN(sizeof(struct ifreq), 8);
  2356. if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
  2357. return -EFAULT;
  2358. if (put_user(convert_in ? rxnfc : compat_ptr(data),
  2359. &ifr->ifr_ifru.ifru_data))
  2360. return -EFAULT;
  2361. if (convert_in) {
  2362. /* We expect there to be holes between fs.m_ext and
  2363. * fs.ring_cookie and at the end of fs, but nowhere else.
  2364. */
  2365. BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
  2366. sizeof(compat_rxnfc->fs.m_ext) !=
  2367. offsetof(struct ethtool_rxnfc, fs.m_ext) +
  2368. sizeof(rxnfc->fs.m_ext));
  2369. BUILD_BUG_ON(
  2370. offsetof(struct compat_ethtool_rxnfc, fs.location) -
  2371. offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
  2372. offsetof(struct ethtool_rxnfc, fs.location) -
  2373. offsetof(struct ethtool_rxnfc, fs.ring_cookie));
  2374. if (copy_in_user(rxnfc, compat_rxnfc,
  2375. (void *)(&rxnfc->fs.m_ext + 1) -
  2376. (void *)rxnfc) ||
  2377. copy_in_user(&rxnfc->fs.ring_cookie,
  2378. &compat_rxnfc->fs.ring_cookie,
  2379. (void *)(&rxnfc->fs.location + 1) -
  2380. (void *)&rxnfc->fs.ring_cookie) ||
  2381. copy_in_user(&rxnfc->rule_cnt, &compat_rxnfc->rule_cnt,
  2382. sizeof(rxnfc->rule_cnt)))
  2383. return -EFAULT;
  2384. }
  2385. ret = dev_ioctl(net, SIOCETHTOOL, ifr);
  2386. if (ret)
  2387. return ret;
  2388. if (convert_out) {
  2389. if (copy_in_user(compat_rxnfc, rxnfc,
  2390. (const void *)(&rxnfc->fs.m_ext + 1) -
  2391. (const void *)rxnfc) ||
  2392. copy_in_user(&compat_rxnfc->fs.ring_cookie,
  2393. &rxnfc->fs.ring_cookie,
  2394. (const void *)(&rxnfc->fs.location + 1) -
  2395. (const void *)&rxnfc->fs.ring_cookie) ||
  2396. copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
  2397. sizeof(rxnfc->rule_cnt)))
  2398. return -EFAULT;
  2399. if (ethcmd == ETHTOOL_GRXCLSRLALL) {
  2400. /* As an optimisation, we only copy the actual
  2401. * number of rules that the underlying
  2402. * function returned. Since Mallory might
  2403. * change the rule count in user memory, we
  2404. * check that it is less than the rule count
  2405. * originally given (as the user buffer size),
  2406. * which has been range-checked.
  2407. */
  2408. if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
  2409. return -EFAULT;
  2410. if (actual_rule_cnt < rule_cnt)
  2411. rule_cnt = actual_rule_cnt;
  2412. if (copy_in_user(&compat_rxnfc->rule_locs[0],
  2413. &rxnfc->rule_locs[0],
  2414. rule_cnt * sizeof(u32)))
  2415. return -EFAULT;
  2416. }
  2417. }
  2418. return 0;
  2419. }
  2420. static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
  2421. {
  2422. void __user *uptr;
  2423. compat_uptr_t uptr32;
  2424. struct ifreq __user *uifr;
  2425. uifr = compat_alloc_user_space(sizeof(*uifr));
  2426. if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
  2427. return -EFAULT;
  2428. if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
  2429. return -EFAULT;
  2430. uptr = compat_ptr(uptr32);
  2431. if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
  2432. return -EFAULT;
  2433. return dev_ioctl(net, SIOCWANDEV, uifr);
  2434. }
  2435. static int bond_ioctl(struct net *net, unsigned int cmd,
  2436. struct compat_ifreq __user *ifr32)
  2437. {
  2438. struct ifreq kifr;
  2439. struct ifreq __user *uifr;
  2440. mm_segment_t old_fs;
  2441. int err;
  2442. u32 data;
  2443. void __user *datap;
  2444. switch (cmd) {
  2445. case SIOCBONDENSLAVE:
  2446. case SIOCBONDRELEASE:
  2447. case SIOCBONDSETHWADDR:
  2448. case SIOCBONDCHANGEACTIVE:
  2449. if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
  2450. return -EFAULT;
  2451. old_fs = get_fs();
  2452. set_fs(KERNEL_DS);
  2453. err = dev_ioctl(net, cmd,
  2454. (struct ifreq __user __force *) &kifr);
  2455. set_fs(old_fs);
  2456. return err;
  2457. case SIOCBONDSLAVEINFOQUERY:
  2458. case SIOCBONDINFOQUERY:
  2459. uifr = compat_alloc_user_space(sizeof(*uifr));
  2460. if (copy_in_user(&uifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
  2461. return -EFAULT;
  2462. if (get_user(data, &ifr32->ifr_ifru.ifru_data))
  2463. return -EFAULT;
  2464. datap = compat_ptr(data);
  2465. if (put_user(datap, &uifr->ifr_ifru.ifru_data))
  2466. return -EFAULT;
  2467. return dev_ioctl(net, cmd, uifr);
  2468. default:
  2469. return -EINVAL;
  2470. }
  2471. }
  2472. static int siocdevprivate_ioctl(struct net *net, unsigned int cmd,
  2473. struct compat_ifreq __user *u_ifreq32)
  2474. {
  2475. struct ifreq __user *u_ifreq64;
  2476. char tmp_buf[IFNAMSIZ];
  2477. void __user *data64;
  2478. u32 data32;
  2479. if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
  2480. IFNAMSIZ))
  2481. return -EFAULT;
  2482. if (__get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
  2483. return -EFAULT;
  2484. data64 = compat_ptr(data32);
  2485. u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
  2486. /* Don't check these user accesses, just let that get trapped
  2487. * in the ioctl handler instead.
  2488. */
  2489. if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
  2490. IFNAMSIZ))
  2491. return -EFAULT;
  2492. if (__put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
  2493. return -EFAULT;
  2494. return dev_ioctl(net, cmd, u_ifreq64);
  2495. }
  2496. static int dev_ifsioc(struct net *net, struct socket *sock,
  2497. unsigned int cmd, struct compat_ifreq __user *uifr32)
  2498. {
  2499. struct ifreq __user *uifr;
  2500. int err;
  2501. uifr = compat_alloc_user_space(sizeof(*uifr));
  2502. if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
  2503. return -EFAULT;
  2504. err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
  2505. if (!err) {
  2506. switch (cmd) {
  2507. case SIOCGIFFLAGS:
  2508. case SIOCGIFMETRIC:
  2509. case SIOCGIFMTU:
  2510. case SIOCGIFMEM:
  2511. case SIOCGIFHWADDR:
  2512. case SIOCGIFINDEX:
  2513. case SIOCGIFADDR:
  2514. case SIOCGIFBRDADDR:
  2515. case SIOCGIFDSTADDR:
  2516. case SIOCGIFNETMASK:
  2517. case SIOCGIFPFLAGS:
  2518. case SIOCGIFTXQLEN:
  2519. case SIOCGMIIPHY:
  2520. case SIOCGMIIREG:
  2521. if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
  2522. err = -EFAULT;
  2523. break;
  2524. }
  2525. }
  2526. return err;
  2527. }
  2528. static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
  2529. struct compat_ifreq __user *uifr32)
  2530. {
  2531. struct ifreq ifr;
  2532. struct compat_ifmap __user *uifmap32;
  2533. mm_segment_t old_fs;
  2534. int err;
  2535. uifmap32 = &uifr32->ifr_ifru.ifru_map;
  2536. err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
  2537. err |= __get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
  2538. err |= __get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
  2539. err |= __get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
  2540. err |= __get_user(ifr.ifr_map.irq, &uifmap32->irq);
  2541. err |= __get_user(ifr.ifr_map.dma, &uifmap32->dma);
  2542. err |= __get_user(ifr.ifr_map.port, &uifmap32->port);
  2543. if (err)
  2544. return -EFAULT;
  2545. old_fs = get_fs();
  2546. set_fs(KERNEL_DS);
  2547. err = dev_ioctl(net, cmd, (void __user __force *)&ifr);
  2548. set_fs(old_fs);
  2549. if (cmd == SIOCGIFMAP && !err) {
  2550. err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
  2551. err |= __put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
  2552. err |= __put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
  2553. err |= __put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
  2554. err |= __put_user(ifr.ifr_map.irq, &uifmap32->irq);
  2555. err |= __put_user(ifr.ifr_map.dma, &uifmap32->dma);
  2556. err |= __put_user(ifr.ifr_map.port, &uifmap32->port);
  2557. if (err)
  2558. err = -EFAULT;
  2559. }
  2560. return err;
  2561. }
  2562. static int compat_siocshwtstamp(struct net *net, struct compat_ifreq __user *uifr32)
  2563. {
  2564. void __user *uptr;
  2565. compat_uptr_t uptr32;
  2566. struct ifreq __user *uifr;
  2567. uifr = compat_alloc_user_space(sizeof(*uifr));
  2568. if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
  2569. return -EFAULT;
  2570. if (get_user(uptr32, &uifr32->ifr_data))
  2571. return -EFAULT;
  2572. uptr = compat_ptr(uptr32);
  2573. if (put_user(uptr, &uifr->ifr_data))
  2574. return -EFAULT;
  2575. return dev_ioctl(net, SIOCSHWTSTAMP, uifr);
  2576. }
  2577. struct rtentry32 {
  2578. u32 rt_pad1;
  2579. struct sockaddr rt_dst; /* target address */
  2580. struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
  2581. struct sockaddr rt_genmask; /* target network mask (IP) */
  2582. unsigned short rt_flags;
  2583. short rt_pad2;
  2584. u32 rt_pad3;
  2585. unsigned char rt_tos;
  2586. unsigned char rt_class;
  2587. short rt_pad4;
  2588. short rt_metric; /* +1 for binary compatibility! */
  2589. /* char * */ u32 rt_dev; /* forcing the device at add */
  2590. u32 rt_mtu; /* per route MTU/Window */
  2591. u32 rt_window; /* Window clamping */
  2592. unsigned short rt_irtt; /* Initial RTT */
  2593. };
  2594. struct in6_rtmsg32 {
  2595. struct in6_addr rtmsg_dst;
  2596. struct in6_addr rtmsg_src;
  2597. struct in6_addr rtmsg_gateway;
  2598. u32 rtmsg_type;
  2599. u16 rtmsg_dst_len;
  2600. u16 rtmsg_src_len;
  2601. u32 rtmsg_metric;
  2602. u32 rtmsg_info;
  2603. u32 rtmsg_flags;
  2604. s32 rtmsg_ifindex;
  2605. };
  2606. static int routing_ioctl(struct net *net, struct socket *sock,
  2607. unsigned int cmd, void __user *argp)
  2608. {
  2609. int ret;
  2610. void *r = NULL;
  2611. struct in6_rtmsg r6;
  2612. struct rtentry r4;
  2613. char devname[16];
  2614. u32 rtdev;
  2615. mm_segment_t old_fs = get_fs();
  2616. if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
  2617. struct in6_rtmsg32 __user *ur6 = argp;
  2618. ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
  2619. 3 * sizeof(struct in6_addr));
  2620. ret |= __get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
  2621. ret |= __get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
  2622. ret |= __get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
  2623. ret |= __get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
  2624. ret |= __get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
  2625. ret |= __get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
  2626. ret |= __get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
  2627. r = (void *) &r6;
  2628. } else { /* ipv4 */
  2629. struct rtentry32 __user *ur4 = argp;
  2630. ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
  2631. 3 * sizeof(struct sockaddr));
  2632. ret |= __get_user(r4.rt_flags, &(ur4->rt_flags));
  2633. ret |= __get_user(r4.rt_metric, &(ur4->rt_metric));
  2634. ret |= __get_user(r4.rt_mtu, &(ur4->rt_mtu));
  2635. ret |= __get_user(r4.rt_window, &(ur4->rt_window));
  2636. ret |= __get_user(r4.rt_irtt, &(ur4->rt_irtt));
  2637. ret |= __get_user(rtdev, &(ur4->rt_dev));
  2638. if (rtdev) {
  2639. ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
  2640. r4.rt_dev = (char __user __force *)devname;
  2641. devname[15] = 0;
  2642. } else
  2643. r4.rt_dev = NULL;
  2644. r = (void *) &r4;
  2645. }
  2646. if (ret) {
  2647. ret = -EFAULT;
  2648. goto out;
  2649. }
  2650. set_fs(KERNEL_DS);
  2651. ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
  2652. set_fs(old_fs);
  2653. out:
  2654. return ret;
  2655. }
  2656. /* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
  2657. * for some operations; this forces use of the newer bridge-utils that
  2658. * use compatible ioctls
  2659. */
  2660. static int old_bridge_ioctl(compat_ulong_t __user *argp)
  2661. {
  2662. compat_ulong_t tmp;
  2663. if (get_user(tmp, argp))
  2664. return -EFAULT;
  2665. if (tmp == BRCTL_GET_VERSION)
  2666. return BRCTL_VERSION + 1;
  2667. return -EINVAL;
  2668. }
  2669. static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
  2670. unsigned int cmd, unsigned long arg)
  2671. {
  2672. void __user *argp = compat_ptr(arg);
  2673. struct sock *sk = sock->sk;
  2674. struct net *net = sock_net(sk);
  2675. if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
  2676. return siocdevprivate_ioctl(net, cmd, argp);
  2677. switch (cmd) {
  2678. case SIOCSIFBR:
  2679. case SIOCGIFBR:
  2680. return old_bridge_ioctl(argp);
  2681. case SIOCGIFNAME:
  2682. return dev_ifname32(net, argp);
  2683. case SIOCGIFCONF:
  2684. return dev_ifconf(net, argp);
  2685. case SIOCETHTOOL:
  2686. return ethtool_ioctl(net, argp);
  2687. case SIOCWANDEV:
  2688. return compat_siocwandev(net, argp);
  2689. case SIOCGIFMAP:
  2690. case SIOCSIFMAP:
  2691. return compat_sioc_ifmap(net, cmd, argp);
  2692. case SIOCBONDENSLAVE:
  2693. case SIOCBONDRELEASE:
  2694. case SIOCBONDSETHWADDR:
  2695. case SIOCBONDSLAVEINFOQUERY:
  2696. case SIOCBONDINFOQUERY:
  2697. case SIOCBONDCHANGEACTIVE:
  2698. return bond_ioctl(net, cmd, argp);
  2699. case SIOCADDRT:
  2700. case SIOCDELRT:
  2701. return routing_ioctl(net, sock, cmd, argp);
  2702. case SIOCGSTAMP:
  2703. return do_siocgstamp(net, sock, cmd, argp);
  2704. case SIOCGSTAMPNS:
  2705. return do_siocgstampns(net, sock, cmd, argp);
  2706. case SIOCSHWTSTAMP:
  2707. return compat_siocshwtstamp(net, argp);
  2708. case FIOSETOWN:
  2709. case SIOCSPGRP:
  2710. case FIOGETOWN:
  2711. case SIOCGPGRP:
  2712. case SIOCBRADDBR:
  2713. case SIOCBRDELBR:
  2714. case SIOCGIFVLAN:
  2715. case SIOCSIFVLAN:
  2716. case SIOCADDDLCI:
  2717. case SIOCDELDLCI:
  2718. return sock_ioctl(file, cmd, arg);
  2719. case SIOCGIFFLAGS:
  2720. case SIOCSIFFLAGS:
  2721. case SIOCGIFMETRIC:
  2722. case SIOCSIFMETRIC:
  2723. case SIOCGIFMTU:
  2724. case SIOCSIFMTU:
  2725. case SIOCGIFMEM:
  2726. case SIOCSIFMEM:
  2727. case SIOCGIFHWADDR:
  2728. case SIOCSIFHWADDR:
  2729. case SIOCADDMULTI:
  2730. case SIOCDELMULTI:
  2731. case SIOCGIFINDEX:
  2732. case SIOCGIFADDR:
  2733. case SIOCSIFADDR:
  2734. case SIOCSIFHWBROADCAST:
  2735. case SIOCDIFADDR:
  2736. case SIOCGIFBRDADDR:
  2737. case SIOCSIFBRDADDR:
  2738. case SIOCGIFDSTADDR:
  2739. case SIOCSIFDSTADDR:
  2740. case SIOCGIFNETMASK:
  2741. case SIOCSIFNETMASK:
  2742. case SIOCSIFPFLAGS:
  2743. case SIOCGIFPFLAGS:
  2744. case SIOCGIFTXQLEN:
  2745. case SIOCSIFTXQLEN:
  2746. case SIOCBRADDIF:
  2747. case SIOCBRDELIF:
  2748. case SIOCSIFNAME:
  2749. case SIOCGMIIPHY:
  2750. case SIOCGMIIREG:
  2751. case SIOCSMIIREG:
  2752. return dev_ifsioc(net, sock, cmd, argp);
  2753. case SIOCSARP:
  2754. case SIOCGARP:
  2755. case SIOCDARP:
  2756. case SIOCATMARK:
  2757. return sock_do_ioctl(net, sock, cmd, arg);
  2758. }
  2759. /* Prevent warning from compat_sys_ioctl, these always
  2760. * result in -EINVAL in the native case anyway. */
  2761. switch (cmd) {
  2762. case SIOCRTMSG:
  2763. case SIOCGIFCOUNT:
  2764. case SIOCSRARP:
  2765. case SIOCGRARP:
  2766. case SIOCDRARP:
  2767. case SIOCSIFLINK:
  2768. case SIOCGIFSLAVE:
  2769. case SIOCSIFSLAVE:
  2770. return -EINVAL;
  2771. }
  2772. return -ENOIOCTLCMD;
  2773. }
  2774. static long compat_sock_ioctl(struct file *file, unsigned cmd,
  2775. unsigned long arg)
  2776. {
  2777. struct socket *sock = file->private_data;
  2778. int ret = -ENOIOCTLCMD;
  2779. struct sock *sk;
  2780. struct net *net;
  2781. sk = sock->sk;
  2782. net = sock_net(sk);
  2783. if (sock->ops->compat_ioctl)
  2784. ret = sock->ops->compat_ioctl(sock, cmd, arg);
  2785. if (ret == -ENOIOCTLCMD &&
  2786. (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
  2787. ret = compat_wext_handle_ioctl(net, cmd, arg);
  2788. if (ret == -ENOIOCTLCMD)
  2789. ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
  2790. return ret;
  2791. }
  2792. #endif
  2793. int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
  2794. {
  2795. return sock->ops->bind(sock, addr, addrlen);
  2796. }
  2797. EXPORT_SYMBOL(kernel_bind);
  2798. int kernel_listen(struct socket *sock, int backlog)
  2799. {
  2800. return sock->ops->listen(sock, backlog);
  2801. }
  2802. EXPORT_SYMBOL(kernel_listen);
  2803. int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
  2804. {
  2805. struct sock *sk = sock->sk;
  2806. int err;
  2807. err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
  2808. newsock);
  2809. if (err < 0)
  2810. goto done;
  2811. err = sock->ops->accept(sock, *newsock, flags);
  2812. if (err < 0) {
  2813. sock_release(*newsock);
  2814. *newsock = NULL;
  2815. goto done;
  2816. }
  2817. (*newsock)->ops = sock->ops;
  2818. __module_get((*newsock)->ops->owner);
  2819. done:
  2820. return err;
  2821. }
  2822. EXPORT_SYMBOL(kernel_accept);
  2823. int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
  2824. int flags)
  2825. {
  2826. return sock->ops->connect(sock, addr, addrlen, flags);
  2827. }
  2828. EXPORT_SYMBOL(kernel_connect);
  2829. int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
  2830. int *addrlen)
  2831. {
  2832. return sock->ops->getname(sock, addr, addrlen, 0);
  2833. }
  2834. EXPORT_SYMBOL(kernel_getsockname);
  2835. int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
  2836. int *addrlen)
  2837. {
  2838. return sock->ops->getname(sock, addr, addrlen, 1);
  2839. }
  2840. EXPORT_SYMBOL(kernel_getpeername);
  2841. int kernel_getsockopt(struct socket *sock, int level, int optname,
  2842. char *optval, int *optlen)
  2843. {
  2844. mm_segment_t oldfs = get_fs();
  2845. char __user *uoptval;
  2846. int __user *uoptlen;
  2847. int err;
  2848. uoptval = (char __user __force *) optval;
  2849. uoptlen = (int __user __force *) optlen;
  2850. set_fs(KERNEL_DS);
  2851. if (level == SOL_SOCKET)
  2852. err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
  2853. else
  2854. err = sock->ops->getsockopt(sock, level, optname, uoptval,
  2855. uoptlen);
  2856. set_fs(oldfs);
  2857. return err;
  2858. }
  2859. EXPORT_SYMBOL(kernel_getsockopt);
  2860. int kernel_setsockopt(struct socket *sock, int level, int optname,
  2861. char *optval, unsigned int optlen)
  2862. {
  2863. mm_segment_t oldfs = get_fs();
  2864. char __user *uoptval;
  2865. int err;
  2866. uoptval = (char __user __force *) optval;
  2867. set_fs(KERNEL_DS);
  2868. if (level == SOL_SOCKET)
  2869. err = sock_setsockopt(sock, level, optname, uoptval, optlen);
  2870. else
  2871. err = sock->ops->setsockopt(sock, level, optname, uoptval,
  2872. optlen);
  2873. set_fs(oldfs);
  2874. return err;
  2875. }
  2876. EXPORT_SYMBOL(kernel_setsockopt);
  2877. int kernel_sendpage(struct socket *sock, struct page *page, int offset,
  2878. size_t size, int flags)
  2879. {
  2880. sock_update_classid(sock->sk);
  2881. if (sock->ops->sendpage)
  2882. return sock->ops->sendpage(sock, page, offset, size, flags);
  2883. return sock_no_sendpage(sock, page, offset, size, flags);
  2884. }
  2885. EXPORT_SYMBOL(kernel_sendpage);
  2886. int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
  2887. {
  2888. mm_segment_t oldfs = get_fs();
  2889. int err;
  2890. set_fs(KERNEL_DS);
  2891. err = sock->ops->ioctl(sock, cmd, arg);
  2892. set_fs(oldfs);
  2893. return err;
  2894. }
  2895. EXPORT_SYMBOL(kernel_sock_ioctl);
  2896. int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
  2897. {
  2898. return sock->ops->shutdown(sock, how);
  2899. }
  2900. EXPORT_SYMBOL(kernel_sock_shutdown);