PageRenderTime 76ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/net/socket.c

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