PageRenderTime 186ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 1ms

/net/socket.c

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