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

/net/socket.c

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