PageRenderTime 72ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/net/socket.c

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