PageRenderTime 75ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/linux-2.6.21.x/net/socket.c

https://bitbucket.org/altlc/wive-rtnl-ralink-rt305x-routers-firmware-amod
C | 2310 lines | 1571 code | 338 blank | 401 comment | 235 complexity | 12fbaea5fca7b66219c4d7e1adbb5627 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception, GPL-2.0, GPL-3.0, LGPL-3.0, 0BSD, AGPL-1.0, LGPL-2.1, LGPL-2.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/rcupdate.h>
  66. #include <linux/netdevice.h>
  67. #include <linux/proc_fs.h>
  68. #include <linux/seq_file.h>
  69. #include <linux/mutex.h>
  70. #include <linux/wanrouter.h>
  71. #include <linux/if_bridge.h>
  72. #include <linux/if_frad.h>
  73. #include <linux/if_vlan.h>
  74. #include <linux/init.h>
  75. #include <linux/poll.h>
  76. #include <linux/cache.h>
  77. #include <linux/module.h>
  78. #include <linux/highmem.h>
  79. #include <linux/mount.h>
  80. #include <linux/security.h>
  81. #include <linux/syscalls.h>
  82. #include <linux/compat.h>
  83. #include <linux/kmod.h>
  84. #include <linux/audit.h>
  85. #include <linux/wireless.h>
  86. #include <asm/uaccess.h>
  87. #include <asm/unistd.h>
  88. #include <net/compat.h>
  89. #include <net/sock.h>
  90. #include <linux/netfilter.h>
  91. static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
  92. static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
  93. unsigned long nr_segs, loff_t pos);
  94. static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
  95. unsigned long nr_segs, loff_t pos);
  96. static int sock_mmap(struct file *file, struct vm_area_struct *vma);
  97. static int sock_close(struct inode *inode, struct file *file);
  98. static unsigned int sock_poll(struct file *file,
  99. struct poll_table_struct *wait);
  100. static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  101. #ifdef CONFIG_COMPAT
  102. static long compat_sock_ioctl(struct file *file,
  103. unsigned int cmd, unsigned long arg);
  104. #endif
  105. static int sock_fasync(int fd, struct file *filp, int on);
  106. static ssize_t sock_sendpage(struct file *file, struct page *page,
  107. int offset, size_t size, loff_t *ppos, int more);
  108. /*
  109. * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
  110. * in the operation structures but are done directly via the socketcall() multiplexor.
  111. */
  112. static const struct file_operations socket_file_ops = {
  113. .owner = THIS_MODULE,
  114. .llseek = no_llseek,
  115. .aio_read = sock_aio_read,
  116. .aio_write = sock_aio_write,
  117. .poll = sock_poll,
  118. .unlocked_ioctl = sock_ioctl,
  119. #ifdef CONFIG_COMPAT
  120. .compat_ioctl = compat_sock_ioctl,
  121. #endif
  122. .mmap = sock_mmap,
  123. .open = sock_no_open, /* special open code to disallow open via /proc */
  124. .release = sock_close,
  125. .fasync = sock_fasync,
  126. .sendpage = sock_sendpage,
  127. .splice_write = generic_splice_sendpage,
  128. };
  129. /*
  130. * The protocol list. Each protocol is registered in here.
  131. */
  132. static DEFINE_SPINLOCK(net_family_lock);
  133. static const struct net_proto_family *net_families[NPROTO] __read_mostly;
  134. /*
  135. * Statistics counters of the socket lists
  136. */
  137. static DEFINE_PER_CPU(int, sockets_in_use) = 0;
  138. /*
  139. * Support routines.
  140. * Move socket addresses back and forth across the kernel/user
  141. * divide and look after the messy bits.
  142. */
  143. #define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
  144. 16 for IP, 16 for IPX,
  145. 24 for IPv6,
  146. about 80 for AX.25
  147. must be at least one bigger than
  148. the AF_UNIX size (see net/unix/af_unix.c
  149. :unix_mkname()).
  150. */
  151. /**
  152. * move_addr_to_kernel - copy a socket address into kernel space
  153. * @uaddr: Address in user space
  154. * @kaddr: Address in kernel space
  155. * @ulen: Length in user space
  156. *
  157. * The address is copied into kernel space. If the provided address is
  158. * too long an error code of -EINVAL is returned. If the copy gives
  159. * invalid addresses -EFAULT is returned. On a success 0 is returned.
  160. */
  161. int move_addr_to_kernel(void __user *uaddr, int ulen, void *kaddr)
  162. {
  163. if (ulen < 0 || ulen > MAX_SOCK_ADDR)
  164. return -EINVAL;
  165. if (ulen == 0)
  166. return 0;
  167. if (copy_from_user(kaddr, uaddr, ulen))
  168. return -EFAULT;
  169. return audit_sockaddr(ulen, kaddr);
  170. }
  171. /**
  172. * move_addr_to_user - copy an address to user space
  173. * @kaddr: kernel space address
  174. * @klen: length of address in kernel
  175. * @uaddr: user space address
  176. * @ulen: pointer to user length field
  177. *
  178. * The value pointed to by ulen on entry is the buffer length available.
  179. * This is overwritten with the buffer space used. -EINVAL is returned
  180. * if an overlong buffer is specified or a negative buffer size. -EFAULT
  181. * is returned if either the buffer or the length field are not
  182. * accessible.
  183. * After copying the data up to the limit the user specifies, the true
  184. * length of the data is written over the length limit the user
  185. * specified. Zero is returned for a success.
  186. */
  187. int move_addr_to_user(void *kaddr, int klen, void __user *uaddr,
  188. int __user *ulen)
  189. {
  190. int err;
  191. int len;
  192. err = get_user(len, ulen);
  193. if (err)
  194. return err;
  195. if (len > klen)
  196. len = klen;
  197. if (len < 0 || len > MAX_SOCK_ADDR)
  198. return -EINVAL;
  199. if (len) {
  200. if (audit_sockaddr(klen, kaddr))
  201. return -ENOMEM;
  202. if (copy_to_user(uaddr, kaddr, len))
  203. return -EFAULT;
  204. }
  205. /*
  206. * "fromlen shall refer to the value before truncation.."
  207. * 1003.1g
  208. */
  209. return __put_user(klen, ulen);
  210. }
  211. #define SOCKFS_MAGIC 0x534F434B
  212. static struct kmem_cache *sock_inode_cachep __read_mostly;
  213. static struct inode *sock_alloc_inode(struct super_block *sb)
  214. {
  215. struct socket_alloc *ei;
  216. ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
  217. if (!ei)
  218. return NULL;
  219. init_waitqueue_head(&ei->socket.wait);
  220. ei->socket.fasync_list = NULL;
  221. ei->socket.state = SS_UNCONNECTED;
  222. ei->socket.flags = 0;
  223. ei->socket.ops = NULL;
  224. ei->socket.sk = NULL;
  225. ei->socket.file = NULL;
  226. return &ei->vfs_inode;
  227. }
  228. static void sock_destroy_inode(struct inode *inode)
  229. {
  230. kmem_cache_free(sock_inode_cachep,
  231. container_of(inode, struct socket_alloc, vfs_inode));
  232. }
  233. static void init_once(void *foo, struct kmem_cache *cachep, unsigned long flags)
  234. {
  235. struct socket_alloc *ei = (struct socket_alloc *)foo;
  236. if (flags & SLAB_CTOR_CONSTRUCTOR)
  237. inode_init_once(&ei->vfs_inode);
  238. }
  239. static int init_inodecache(void)
  240. {
  241. sock_inode_cachep = kmem_cache_create("sock_inode_cache",
  242. sizeof(struct socket_alloc),
  243. 0,
  244. (SLAB_HWCACHE_ALIGN |
  245. SLAB_RECLAIM_ACCOUNT |
  246. SLAB_MEM_SPREAD),
  247. init_once,
  248. NULL);
  249. if (sock_inode_cachep == NULL)
  250. return -ENOMEM;
  251. return 0;
  252. }
  253. static struct super_operations sockfs_ops = {
  254. .alloc_inode = sock_alloc_inode,
  255. .destroy_inode =sock_destroy_inode,
  256. .statfs = simple_statfs,
  257. };
  258. static int sockfs_get_sb(struct file_system_type *fs_type,
  259. int flags, const char *dev_name, void *data,
  260. struct vfsmount *mnt)
  261. {
  262. return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,
  263. mnt);
  264. }
  265. static struct vfsmount *sock_mnt __read_mostly;
  266. static struct file_system_type sock_fs_type = {
  267. .name = "sockfs",
  268. .get_sb = sockfs_get_sb,
  269. .kill_sb = kill_anon_super,
  270. };
  271. static int sockfs_delete_dentry(struct dentry *dentry)
  272. {
  273. /*
  274. * At creation time, we pretended this dentry was hashed
  275. * (by clearing DCACHE_UNHASHED bit in d_flags)
  276. * At delete time, we restore the truth : not hashed.
  277. * (so that dput() can proceed correctly)
  278. */
  279. dentry->d_flags |= DCACHE_UNHASHED;
  280. return 0;
  281. }
  282. static struct dentry_operations sockfs_dentry_operations = {
  283. .d_delete = sockfs_delete_dentry,
  284. };
  285. /*
  286. * Obtains the first available file descriptor and sets it up for use.
  287. *
  288. * These functions create file structures and maps them to fd space
  289. * of the current process. On success it returns file descriptor
  290. * and file struct implicitly stored in sock->file.
  291. * Note that another thread may close file descriptor before we return
  292. * from this function. We use the fact that now we do not refer
  293. * to socket after mapping. If one day we will need it, this
  294. * function will increment ref. count on file by 1.
  295. *
  296. * In any case returned fd MAY BE not valid!
  297. * This race condition is unavoidable
  298. * with shared fd spaces, we cannot solve it inside kernel,
  299. * but we take care of internal coherence yet.
  300. */
  301. static int sock_alloc_fd(struct file **filep)
  302. {
  303. int fd;
  304. fd = get_unused_fd();
  305. if (likely(fd >= 0)) {
  306. struct file *file = get_empty_filp();
  307. *filep = file;
  308. if (unlikely(!file)) {
  309. put_unused_fd(fd);
  310. return -ENFILE;
  311. }
  312. } else
  313. *filep = NULL;
  314. return fd;
  315. }
  316. static int sock_attach_fd(struct socket *sock, struct file *file)
  317. {
  318. struct qstr this;
  319. char name[32];
  320. this.len = sprintf(name, "[%lu]", SOCK_INODE(sock)->i_ino);
  321. this.name = (unsigned char *)name;
  322. this.hash = 0;
  323. file->f_path.dentry = d_alloc(sock_mnt->mnt_sb->s_root, &this);
  324. if (unlikely(!file->f_path.dentry))
  325. return -ENOMEM;
  326. file->f_path.dentry->d_op = &sockfs_dentry_operations;
  327. /*
  328. * We dont want to push this dentry into global dentry hash table.
  329. * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
  330. * This permits a working /proc/$pid/fd/XXX on sockets
  331. */
  332. file->f_path.dentry->d_flags &= ~DCACHE_UNHASHED;
  333. d_instantiate(file->f_path.dentry, SOCK_INODE(sock));
  334. file->f_path.mnt = mntget(sock_mnt);
  335. file->f_mapping = file->f_path.dentry->d_inode->i_mapping;
  336. sock->file = file;
  337. file->f_op = SOCK_INODE(sock)->i_fop = &socket_file_ops;
  338. file->f_mode = FMODE_READ | FMODE_WRITE;
  339. file->f_flags = O_RDWR;
  340. file->f_pos = 0;
  341. file->private_data = sock;
  342. return 0;
  343. }
  344. int sock_map_fd(struct socket *sock)
  345. {
  346. struct file *newfile;
  347. int fd = sock_alloc_fd(&newfile);
  348. if (likely(fd >= 0)) {
  349. int err = sock_attach_fd(sock, newfile);
  350. if (unlikely(err < 0)) {
  351. put_filp(newfile);
  352. put_unused_fd(fd);
  353. return err;
  354. }
  355. fd_install(fd, newfile);
  356. }
  357. return fd;
  358. }
  359. static struct socket *sock_from_file(struct file *file, int *err)
  360. {
  361. if (file->f_op == &socket_file_ops)
  362. return file->private_data; /* set in sock_map_fd */
  363. *err = -ENOTSOCK;
  364. return NULL;
  365. }
  366. /**
  367. * sockfd_lookup - Go from a file number to its socket slot
  368. * @fd: file handle
  369. * @err: pointer to an error code return
  370. *
  371. * The file handle passed in is locked and the socket it is bound
  372. * too is returned. If an error occurs the err pointer is overwritten
  373. * with a negative errno code and NULL is returned. The function checks
  374. * for both invalid handles and passing a handle which is not a socket.
  375. *
  376. * On a success the socket object pointer is returned.
  377. */
  378. struct socket *sockfd_lookup(int fd, int *err)
  379. {
  380. struct file *file;
  381. struct socket *sock;
  382. file = fget(fd);
  383. if (!file) {
  384. *err = -EBADF;
  385. return NULL;
  386. }
  387. sock = sock_from_file(file, err);
  388. if (!sock)
  389. fput(file);
  390. return sock;
  391. }
  392. static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
  393. {
  394. struct file *file;
  395. struct socket *sock;
  396. *err = -EBADF;
  397. file = fget_light(fd, fput_needed);
  398. if (file) {
  399. sock = sock_from_file(file, err);
  400. if (sock)
  401. return sock;
  402. fput_light(file, *fput_needed);
  403. }
  404. return NULL;
  405. }
  406. /**
  407. * sock_alloc - allocate a socket
  408. *
  409. * Allocate a new inode and socket object. The two are bound together
  410. * and initialised. The socket is then returned. If we are out of inodes
  411. * NULL is returned.
  412. */
  413. static struct socket *sock_alloc(void)
  414. {
  415. struct inode *inode;
  416. struct socket *sock;
  417. inode = new_inode(sock_mnt->mnt_sb);
  418. if (!inode)
  419. return NULL;
  420. sock = SOCKET_I(inode);
  421. inode->i_mode = S_IFSOCK | S_IRWXUGO;
  422. inode->i_uid = current->fsuid;
  423. inode->i_gid = current->fsgid;
  424. get_cpu_var(sockets_in_use)++;
  425. put_cpu_var(sockets_in_use);
  426. return sock;
  427. }
  428. /*
  429. * In theory you can't get an open on this inode, but /proc provides
  430. * a back door. Remember to keep it shut otherwise you'll let the
  431. * creepy crawlies in.
  432. */
  433. static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
  434. {
  435. return -ENXIO;
  436. }
  437. const struct file_operations bad_sock_fops = {
  438. .owner = THIS_MODULE,
  439. .open = sock_no_open,
  440. };
  441. /**
  442. * sock_release - close a socket
  443. * @sock: socket to close
  444. *
  445. * The socket is released from the protocol stack if it has a release
  446. * callback, and the inode is then released if the socket is bound to
  447. * an inode not a file.
  448. */
  449. void sock_release(struct socket *sock)
  450. {
  451. if (sock->ops) {
  452. struct module *owner = sock->ops->owner;
  453. sock->ops->release(sock);
  454. sock->ops = NULL;
  455. module_put(owner);
  456. }
  457. if (sock->fasync_list)
  458. printk(KERN_ERR "sock_release: fasync list not empty!\n");
  459. get_cpu_var(sockets_in_use)--;
  460. put_cpu_var(sockets_in_use);
  461. if (!sock->file) {
  462. iput(SOCK_INODE(sock));
  463. return;
  464. }
  465. sock->file = NULL;
  466. }
  467. static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
  468. struct msghdr *msg, size_t size)
  469. {
  470. struct sock_iocb *si = kiocb_to_siocb(iocb);
  471. int err;
  472. si->sock = sock;
  473. si->scm = NULL;
  474. si->msg = msg;
  475. si->size = size;
  476. err = security_socket_sendmsg(sock, msg, size);
  477. if (err)
  478. return err;
  479. err = sock->ops->sendmsg(iocb, sock, msg, size);
  480. return err;
  481. }
  482. int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
  483. {
  484. struct kiocb iocb;
  485. struct sock_iocb siocb;
  486. int ret;
  487. init_sync_kiocb(&iocb, NULL);
  488. iocb.private = &siocb;
  489. ret = __sock_sendmsg(&iocb, sock, msg, size);
  490. if (-EIOCBQUEUED == ret)
  491. ret = wait_on_sync_kiocb(&iocb);
  492. return ret;
  493. }
  494. int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
  495. struct kvec *vec, size_t num, size_t size)
  496. {
  497. mm_segment_t oldfs = get_fs();
  498. int result;
  499. set_fs(KERNEL_DS);
  500. /*
  501. * the following is safe, since for compiler definitions of kvec and
  502. * iovec are identical, yielding the same in-core layout and alignment
  503. */
  504. msg->msg_iov = (struct iovec *)vec;
  505. msg->msg_iovlen = num;
  506. result = sock_sendmsg(sock, msg, size);
  507. set_fs(oldfs);
  508. return result;
  509. }
  510. static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
  511. struct msghdr *msg, size_t size, int flags)
  512. {
  513. int err;
  514. struct sock_iocb *si = kiocb_to_siocb(iocb);
  515. si->sock = sock;
  516. si->scm = NULL;
  517. si->msg = msg;
  518. si->size = size;
  519. si->flags = flags;
  520. err = security_socket_recvmsg(sock, msg, size, flags);
  521. if (err)
  522. return err;
  523. err = sock->ops->recvmsg(iocb, sock, msg, size, flags);
  524. return err;
  525. }
  526. int sock_recvmsg(struct socket *sock, struct msghdr *msg,
  527. size_t size, int flags)
  528. {
  529. struct kiocb iocb;
  530. struct sock_iocb siocb;
  531. int ret;
  532. init_sync_kiocb(&iocb, NULL);
  533. iocb.private = &siocb;
  534. ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
  535. if (-EIOCBQUEUED == ret)
  536. ret = wait_on_sync_kiocb(&iocb);
  537. return ret;
  538. }
  539. int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
  540. struct kvec *vec, size_t num, size_t size, int flags)
  541. {
  542. mm_segment_t oldfs = get_fs();
  543. int result;
  544. set_fs(KERNEL_DS);
  545. /*
  546. * the following is safe, since for compiler definitions of kvec and
  547. * iovec are identical, yielding the same in-core layout and alignment
  548. */
  549. msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
  550. result = sock_recvmsg(sock, msg, size, flags);
  551. set_fs(oldfs);
  552. return result;
  553. }
  554. static void sock_aio_dtor(struct kiocb *iocb)
  555. {
  556. kfree(iocb->private);
  557. }
  558. static ssize_t sock_sendpage(struct file *file, struct page *page,
  559. int offset, size_t size, loff_t *ppos, int more)
  560. {
  561. struct socket *sock;
  562. int flags;
  563. sock = file->private_data;
  564. flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
  565. if (more)
  566. flags |= MSG_MORE;
  567. return kernel_sendpage(sock, page, offset, size, flags);
  568. }
  569. static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
  570. struct sock_iocb *siocb)
  571. {
  572. if (!is_sync_kiocb(iocb)) {
  573. siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
  574. if (!siocb)
  575. return NULL;
  576. iocb->ki_dtor = sock_aio_dtor;
  577. }
  578. siocb->kiocb = iocb;
  579. iocb->private = siocb;
  580. return siocb;
  581. }
  582. static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
  583. struct file *file, const struct iovec *iov,
  584. unsigned long nr_segs)
  585. {
  586. struct socket *sock = file->private_data;
  587. size_t size = 0;
  588. int i;
  589. for (i = 0; i < nr_segs; i++)
  590. size += iov[i].iov_len;
  591. msg->msg_name = NULL;
  592. msg->msg_namelen = 0;
  593. msg->msg_control = NULL;
  594. msg->msg_controllen = 0;
  595. msg->msg_iov = (struct iovec *)iov;
  596. msg->msg_iovlen = nr_segs;
  597. msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  598. return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
  599. }
  600. static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
  601. unsigned long nr_segs, loff_t pos)
  602. {
  603. struct sock_iocb siocb, *x;
  604. if (pos != 0)
  605. return -ESPIPE;
  606. if (iocb->ki_left == 0) /* Match SYS5 behaviour */
  607. return 0;
  608. x = alloc_sock_iocb(iocb, &siocb);
  609. if (!x)
  610. return -ENOMEM;
  611. return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
  612. }
  613. static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
  614. struct file *file, const struct iovec *iov,
  615. unsigned long nr_segs)
  616. {
  617. struct socket *sock = file->private_data;
  618. size_t size = 0;
  619. int i;
  620. for (i = 0; i < nr_segs; i++)
  621. size += iov[i].iov_len;
  622. msg->msg_name = NULL;
  623. msg->msg_namelen = 0;
  624. msg->msg_control = NULL;
  625. msg->msg_controllen = 0;
  626. msg->msg_iov = (struct iovec *)iov;
  627. msg->msg_iovlen = nr_segs;
  628. msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  629. if (sock->type == SOCK_SEQPACKET)
  630. msg->msg_flags |= MSG_EOR;
  631. return __sock_sendmsg(iocb, sock, msg, size);
  632. }
  633. static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
  634. unsigned long nr_segs, loff_t pos)
  635. {
  636. struct sock_iocb siocb, *x;
  637. if (pos != 0)
  638. return -ESPIPE;
  639. x = alloc_sock_iocb(iocb, &siocb);
  640. if (!x)
  641. return -ENOMEM;
  642. return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
  643. }
  644. /*
  645. * Atomic setting of ioctl hooks to avoid race
  646. * with module unload.
  647. */
  648. static DEFINE_MUTEX(br_ioctl_mutex);
  649. static int (*br_ioctl_hook) (unsigned int cmd, void __user *arg) = NULL;
  650. void brioctl_set(int (*hook) (unsigned int, void __user *))
  651. {
  652. mutex_lock(&br_ioctl_mutex);
  653. br_ioctl_hook = hook;
  654. mutex_unlock(&br_ioctl_mutex);
  655. }
  656. EXPORT_SYMBOL(brioctl_set);
  657. static DEFINE_MUTEX(vlan_ioctl_mutex);
  658. static int (*vlan_ioctl_hook) (void __user *arg);
  659. void vlan_ioctl_set(int (*hook) (void __user *))
  660. {
  661. mutex_lock(&vlan_ioctl_mutex);
  662. vlan_ioctl_hook = hook;
  663. mutex_unlock(&vlan_ioctl_mutex);
  664. }
  665. EXPORT_SYMBOL(vlan_ioctl_set);
  666. static DEFINE_MUTEX(dlci_ioctl_mutex);
  667. static int (*dlci_ioctl_hook) (unsigned int, void __user *);
  668. void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
  669. {
  670. mutex_lock(&dlci_ioctl_mutex);
  671. dlci_ioctl_hook = hook;
  672. mutex_unlock(&dlci_ioctl_mutex);
  673. }
  674. EXPORT_SYMBOL(dlci_ioctl_set);
  675. /*
  676. * With an ioctl, arg may well be a user mode pointer, but we don't know
  677. * what to do with it - that's up to the protocol still.
  678. */
  679. static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  680. {
  681. struct socket *sock;
  682. void __user *argp = (void __user *)arg;
  683. int pid, err;
  684. sock = file->private_data;
  685. if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
  686. err = dev_ioctl(cmd, argp);
  687. } else
  688. #ifdef CONFIG_WIRELESS_EXT
  689. if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
  690. err = dev_ioctl(cmd, argp);
  691. } else
  692. #endif /* CONFIG_WIRELESS_EXT */
  693. switch (cmd) {
  694. case FIOSETOWN:
  695. case SIOCSPGRP:
  696. err = -EFAULT;
  697. if (get_user(pid, (int __user *)argp))
  698. break;
  699. err = f_setown(sock->file, pid, 1);
  700. break;
  701. case FIOGETOWN:
  702. case SIOCGPGRP:
  703. err = put_user(f_getown(sock->file),
  704. (int __user *)argp);
  705. break;
  706. case SIOCGIFBR:
  707. case SIOCSIFBR:
  708. case SIOCBRADDBR:
  709. case SIOCBRDELBR:
  710. err = -ENOPKG;
  711. if (!br_ioctl_hook)
  712. request_module("bridge");
  713. mutex_lock(&br_ioctl_mutex);
  714. if (br_ioctl_hook)
  715. err = br_ioctl_hook(cmd, argp);
  716. mutex_unlock(&br_ioctl_mutex);
  717. break;
  718. case SIOCGIFVLAN:
  719. case SIOCSIFVLAN:
  720. err = -ENOPKG;
  721. if (!vlan_ioctl_hook)
  722. request_module("8021q");
  723. mutex_lock(&vlan_ioctl_mutex);
  724. if (vlan_ioctl_hook)
  725. err = vlan_ioctl_hook(argp);
  726. mutex_unlock(&vlan_ioctl_mutex);
  727. break;
  728. case SIOCADDDLCI:
  729. case SIOCDELDLCI:
  730. err = -ENOPKG;
  731. if (!dlci_ioctl_hook)
  732. request_module("dlci");
  733. if (dlci_ioctl_hook) {
  734. mutex_lock(&dlci_ioctl_mutex);
  735. err = dlci_ioctl_hook(cmd, argp);
  736. mutex_unlock(&dlci_ioctl_mutex);
  737. }
  738. break;
  739. default:
  740. err = sock->ops->ioctl(sock, cmd, arg);
  741. /*
  742. * If this ioctl is unknown try to hand it down
  743. * to the NIC driver.
  744. */
  745. if (err == -ENOIOCTLCMD)
  746. err = dev_ioctl(cmd, argp);
  747. break;
  748. }
  749. return err;
  750. }
  751. int sock_create_lite(int family, int type, int protocol, struct socket **res)
  752. {
  753. int err;
  754. struct socket *sock = NULL;
  755. err = security_socket_create(family, type, protocol, 1);
  756. if (err)
  757. goto out;
  758. sock = sock_alloc();
  759. if (!sock) {
  760. err = -ENOMEM;
  761. goto out;
  762. }
  763. sock->type = type;
  764. err = security_socket_post_create(sock, family, type, protocol, 1);
  765. if (err)
  766. goto out_release;
  767. out:
  768. *res = sock;
  769. return err;
  770. out_release:
  771. sock_release(sock);
  772. sock = NULL;
  773. goto out;
  774. }
  775. /* No kernel lock held - perfect */
  776. static unsigned int sock_poll(struct file *file, poll_table *wait)
  777. {
  778. struct socket *sock;
  779. /*
  780. * We can't return errors to poll, so it's either yes or no.
  781. */
  782. sock = file->private_data;
  783. return sock->ops->poll(file, sock, wait);
  784. }
  785. static int sock_mmap(struct file *file, struct vm_area_struct *vma)
  786. {
  787. struct socket *sock = file->private_data;
  788. return sock->ops->mmap(file, sock, vma);
  789. }
  790. static int sock_close(struct inode *inode, struct file *filp)
  791. {
  792. /*
  793. * It was possible the inode is NULL we were
  794. * closing an unfinished socket.
  795. */
  796. if (!inode) {
  797. printk(KERN_DEBUG "sock_close: NULL inode\n");
  798. return 0;
  799. }
  800. sock_fasync(-1, filp, 0);
  801. sock_release(SOCKET_I(inode));
  802. return 0;
  803. }
  804. /*
  805. * Update the socket async list
  806. *
  807. * Fasync_list locking strategy.
  808. *
  809. * 1. fasync_list is modified only under process context socket lock
  810. * i.e. under semaphore.
  811. * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
  812. * or under socket lock.
  813. * 3. fasync_list can be used from softirq context, so that
  814. * modification under socket lock have to be enhanced with
  815. * write_lock_bh(&sk->sk_callback_lock).
  816. * --ANK (990710)
  817. */
  818. static int sock_fasync(int fd, struct file *filp, int on)
  819. {
  820. struct fasync_struct *fa, *fna = NULL, **prev;
  821. struct socket *sock;
  822. struct sock *sk;
  823. if (on) {
  824. fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
  825. if (fna == NULL)
  826. return -ENOMEM;
  827. }
  828. sock = filp->private_data;
  829. sk = sock->sk;
  830. if (sk == NULL) {
  831. kfree(fna);
  832. return -EINVAL;
  833. }
  834. lock_sock(sk);
  835. prev = &(sock->fasync_list);
  836. for (fa = *prev; fa != NULL; prev = &fa->fa_next, fa = *prev)
  837. if (fa->fa_file == filp)
  838. break;
  839. if (on) {
  840. if (fa != NULL) {
  841. write_lock_bh(&sk->sk_callback_lock);
  842. fa->fa_fd = fd;
  843. write_unlock_bh(&sk->sk_callback_lock);
  844. kfree(fna);
  845. goto out;
  846. }
  847. fna->fa_file = filp;
  848. fna->fa_fd = fd;
  849. fna->magic = FASYNC_MAGIC;
  850. fna->fa_next = sock->fasync_list;
  851. write_lock_bh(&sk->sk_callback_lock);
  852. sock->fasync_list = fna;
  853. sock_set_flag(sk, SOCK_FASYNC);
  854. write_unlock_bh(&sk->sk_callback_lock);
  855. } else {
  856. if (fa != NULL) {
  857. write_lock_bh(&sk->sk_callback_lock);
  858. *prev = fa->fa_next;
  859. if (!sock->fasync_list)
  860. sock_reset_flag(sk, SOCK_FASYNC);
  861. write_unlock_bh(&sk->sk_callback_lock);
  862. kfree(fa);
  863. }
  864. }
  865. out:
  866. release_sock(sock->sk);
  867. return 0;
  868. }
  869. /* This function may be called only under socket lock or callback_lock */
  870. int sock_wake_async(struct socket *sock, int how, int band)
  871. {
  872. if (!sock || !sock->fasync_list)
  873. return -1;
  874. switch (how) {
  875. case SOCK_WAKE_WAITD:
  876. if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
  877. break;
  878. goto call_kill;
  879. case SOCK_WAKE_SPACE:
  880. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
  881. break;
  882. /* fall through */
  883. case SOCK_WAKE_IO:
  884. call_kill:
  885. __kill_fasync(sock->fasync_list, SIGIO, band);
  886. break;
  887. case SOCK_WAKE_URG:
  888. __kill_fasync(sock->fasync_list, SIGURG, band);
  889. }
  890. return 0;
  891. }
  892. static int __sock_create(int family, int type, int protocol,
  893. struct socket **res, int kern)
  894. {
  895. int err;
  896. struct socket *sock;
  897. const struct net_proto_family *pf;
  898. /*
  899. * Check protocol is in range
  900. */
  901. if (family < 0 || family >= NPROTO)
  902. return -EAFNOSUPPORT;
  903. if (type < 0 || type >= SOCK_MAX)
  904. return -EINVAL;
  905. /* Compatibility.
  906. This uglymoron is moved from INET layer to here to avoid
  907. deadlock in module load.
  908. */
  909. if (family == PF_INET && type == SOCK_PACKET) {
  910. static int warned;
  911. if (!warned) {
  912. warned = 1;
  913. printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
  914. current->comm);
  915. }
  916. family = PF_PACKET;
  917. }
  918. err = security_socket_create(family, type, protocol, kern);
  919. if (err)
  920. return err;
  921. /*
  922. * Allocate the socket and allow the family to set things up. if
  923. * the protocol is 0, the family is instructed to select an appropriate
  924. * default.
  925. */
  926. sock = sock_alloc();
  927. if (!sock) {
  928. if (net_ratelimit())
  929. printk(KERN_WARNING "socket: no more sockets\n");
  930. return -ENFILE; /* Not exactly a match, but its the
  931. closest posix thing */
  932. }
  933. sock->type = type;
  934. #if defined(CONFIG_KMOD)
  935. /* Attempt to load a protocol module if the find failed.
  936. *
  937. * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
  938. * requested real, full-featured networking support upon configuration.
  939. * Otherwise module support will break!
  940. */
  941. if (net_families[family] == NULL)
  942. request_module("net-pf-%d", family);
  943. #endif
  944. rcu_read_lock();
  945. pf = rcu_dereference(net_families[family]);
  946. err = -EAFNOSUPPORT;
  947. if (!pf)
  948. goto out_release;
  949. /*
  950. * We will call the ->create function, that possibly is in a loadable
  951. * module, so we have to bump that loadable module refcnt first.
  952. */
  953. if (!try_module_get(pf->owner))
  954. goto out_release;
  955. /* Now protected by module ref count */
  956. rcu_read_unlock();
  957. err = pf->create(sock, protocol);
  958. if (err < 0)
  959. goto out_module_put;
  960. /*
  961. * Now to bump the refcnt of the [loadable] module that owns this
  962. * socket at sock_release time we decrement its refcnt.
  963. */
  964. if (!try_module_get(sock->ops->owner))
  965. goto out_module_busy;
  966. /*
  967. * Now that we're done with the ->create function, the [loadable]
  968. * module can have its refcnt decremented
  969. */
  970. module_put(pf->owner);
  971. err = security_socket_post_create(sock, family, type, protocol, kern);
  972. if (err)
  973. goto out_release;
  974. *res = sock;
  975. return 0;
  976. out_module_busy:
  977. err = -EAFNOSUPPORT;
  978. out_module_put:
  979. sock->ops = NULL;
  980. module_put(pf->owner);
  981. out_sock_release:
  982. sock_release(sock);
  983. return err;
  984. out_release:
  985. rcu_read_unlock();
  986. goto out_sock_release;
  987. }
  988. int sock_create(int family, int type, int protocol, struct socket **res)
  989. {
  990. return __sock_create(family, type, protocol, res, 0);
  991. }
  992. int sock_create_kern(int family, int type, int protocol, struct socket **res)
  993. {
  994. return __sock_create(family, type, protocol, res, 1);
  995. }
  996. asmlinkage long sys_socket(int family, int type, int protocol)
  997. {
  998. int retval;
  999. struct socket *sock;
  1000. retval = sock_create(family, type, protocol, &sock);
  1001. if (retval < 0)
  1002. goto out;
  1003. retval = sock_map_fd(sock);
  1004. if (retval < 0)
  1005. goto out_release;
  1006. out:
  1007. /* It may be already another descriptor 8) Not kernel problem. */
  1008. return retval;
  1009. out_release:
  1010. sock_release(sock);
  1011. return retval;
  1012. }
  1013. /*
  1014. * Create a pair of connected sockets.
  1015. */
  1016. asmlinkage long sys_socketpair(int family, int type, int protocol,
  1017. int __user *usockvec)
  1018. {
  1019. struct socket *sock1, *sock2;
  1020. int fd1, fd2, err;
  1021. struct file *newfile1, *newfile2;
  1022. /*
  1023. * Obtain the first socket and check if the underlying protocol
  1024. * supports the socketpair call.
  1025. */
  1026. err = sock_create(family, type, protocol, &sock1);
  1027. if (err < 0)
  1028. goto out;
  1029. err = sock_create(family, type, protocol, &sock2);
  1030. if (err < 0)
  1031. goto out_release_1;
  1032. err = sock1->ops->socketpair(sock1, sock2);
  1033. if (err < 0)
  1034. goto out_release_both;
  1035. fd1 = sock_alloc_fd(&newfile1);
  1036. if (unlikely(fd1 < 0)) {
  1037. err = fd1;
  1038. goto out_release_both;
  1039. }
  1040. fd2 = sock_alloc_fd(&newfile2);
  1041. if (unlikely(fd2 < 0)) {
  1042. err = fd2;
  1043. put_filp(newfile1);
  1044. put_unused_fd(fd1);
  1045. goto out_release_both;
  1046. }
  1047. err = sock_attach_fd(sock1, newfile1);
  1048. if (unlikely(err < 0)) {
  1049. goto out_fd2;
  1050. }
  1051. err = sock_attach_fd(sock2, newfile2);
  1052. if (unlikely(err < 0)) {
  1053. fput(newfile1);
  1054. goto out_fd1;
  1055. }
  1056. err = audit_fd_pair(fd1, fd2);
  1057. if (err < 0) {
  1058. fput(newfile1);
  1059. fput(newfile2);
  1060. goto out_fd;
  1061. }
  1062. fd_install(fd1, newfile1);
  1063. fd_install(fd2, newfile2);
  1064. /* fd1 and fd2 may be already another descriptors.
  1065. * Not kernel problem.
  1066. */
  1067. err = put_user(fd1, &usockvec[0]);
  1068. if (!err)
  1069. err = put_user(fd2, &usockvec[1]);
  1070. if (!err)
  1071. return 0;
  1072. sys_close(fd2);
  1073. sys_close(fd1);
  1074. return err;
  1075. out_release_both:
  1076. sock_release(sock2);
  1077. out_release_1:
  1078. sock_release(sock1);
  1079. out:
  1080. return err;
  1081. out_fd2:
  1082. put_filp(newfile1);
  1083. sock_release(sock1);
  1084. out_fd1:
  1085. put_filp(newfile2);
  1086. sock_release(sock2);
  1087. out_fd:
  1088. put_unused_fd(fd1);
  1089. put_unused_fd(fd2);
  1090. goto out;
  1091. }
  1092. /*
  1093. * Bind a name to a socket. Nothing much to do here since it's
  1094. * the protocol's responsibility to handle the local address.
  1095. *
  1096. * We move the socket address to kernel space before we call
  1097. * the protocol layer (having also checked the address is ok).
  1098. */
  1099. asmlinkage long sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
  1100. {
  1101. struct socket *sock;
  1102. char address[MAX_SOCK_ADDR];
  1103. int err, fput_needed;
  1104. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1105. if(sock) {
  1106. err = move_addr_to_kernel(umyaddr, addrlen, address);
  1107. if (err >= 0) {
  1108. err = security_socket_bind(sock,
  1109. (struct sockaddr *)address,
  1110. addrlen);
  1111. if (!err)
  1112. err = sock->ops->bind(sock,
  1113. (struct sockaddr *)
  1114. address, addrlen);
  1115. }
  1116. fput_light(sock->file, fput_needed);
  1117. }
  1118. return err;
  1119. }
  1120. /*
  1121. * Perform a listen. Basically, we allow the protocol to do anything
  1122. * necessary for a listen, and if that works, we mark the socket as
  1123. * ready for listening.
  1124. */
  1125. int sysctl_somaxconn __read_mostly = SOMAXCONN;
  1126. asmlinkage long sys_listen(int fd, int backlog)
  1127. {
  1128. struct socket *sock;
  1129. int err, fput_needed;
  1130. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1131. if (sock) {
  1132. if ((unsigned)backlog > sysctl_somaxconn)
  1133. backlog = sysctl_somaxconn;
  1134. err = security_socket_listen(sock, backlog);
  1135. if (!err)
  1136. err = sock->ops->listen(sock, backlog);
  1137. fput_light(sock->file, fput_needed);
  1138. }
  1139. return err;
  1140. }
  1141. /*
  1142. * For accept, we attempt to create a new socket, set up the link
  1143. * with the client, wake up the client, then return the new
  1144. * connected fd. We collect the address of the connector in kernel
  1145. * space and move it to user at the very end. This is unclean because
  1146. * we open the socket then return an error.
  1147. *
  1148. * 1003.1g adds the ability to recvmsg() to query connection pending
  1149. * status to recvmsg. We need to add that support in a way thats
  1150. * clean when we restucture accept also.
  1151. */
  1152. asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr,
  1153. int __user *upeer_addrlen)
  1154. {
  1155. struct socket *sock, *newsock;
  1156. struct file *newfile;
  1157. int err, len, newfd, fput_needed;
  1158. char address[MAX_SOCK_ADDR];
  1159. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1160. if (!sock)
  1161. goto out;
  1162. err = -ENFILE;
  1163. if (!(newsock = sock_alloc()))
  1164. goto out_put;
  1165. newsock->type = sock->type;
  1166. newsock->ops = sock->ops;
  1167. /*
  1168. * We don't need try_module_get here, as the listening socket (sock)
  1169. * has the protocol module (sock->ops->owner) held.
  1170. */
  1171. __module_get(newsock->ops->owner);
  1172. newfd = sock_alloc_fd(&newfile);
  1173. if (unlikely(newfd < 0)) {
  1174. err = newfd;
  1175. sock_release(newsock);
  1176. goto out_put;
  1177. }
  1178. err = sock_attach_fd(newsock, newfile);
  1179. if (err < 0)
  1180. goto out_fd_simple;
  1181. err = security_socket_accept(sock, newsock);
  1182. if (err)
  1183. goto out_fd;
  1184. err = sock->ops->accept(sock, newsock, sock->file->f_flags);
  1185. if (err < 0)
  1186. goto out_fd;
  1187. if (upeer_sockaddr) {
  1188. if (newsock->ops->getname(newsock, (struct sockaddr *)address,
  1189. &len, 2) < 0) {
  1190. err = -ECONNABORTED;
  1191. goto out_fd;
  1192. }
  1193. err = move_addr_to_user(address, len, upeer_sockaddr,
  1194. upeer_addrlen);
  1195. if (err < 0)
  1196. goto out_fd;
  1197. }
  1198. /* File flags are not inherited via accept() unlike another OSes. */
  1199. fd_install(newfd, newfile);
  1200. err = newfd;
  1201. out_put:
  1202. fput_light(sock->file, fput_needed);
  1203. out:
  1204. return err;
  1205. out_fd_simple:
  1206. sock_release(newsock);
  1207. put_filp(newfile);
  1208. put_unused_fd(newfd);
  1209. goto out_put;
  1210. out_fd:
  1211. fput(newfile);
  1212. put_unused_fd(newfd);
  1213. goto out_put;
  1214. }
  1215. /*
  1216. * Attempt to connect to a socket with the server address. The address
  1217. * is in user space so we verify it is OK and move it to kernel space.
  1218. *
  1219. * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
  1220. * break bindings
  1221. *
  1222. * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
  1223. * other SEQPACKET protocols that take time to connect() as it doesn't
  1224. * include the -EINPROGRESS status for such sockets.
  1225. */
  1226. asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr,
  1227. int addrlen)
  1228. {
  1229. struct socket *sock;
  1230. char address[MAX_SOCK_ADDR];
  1231. int err, fput_needed;
  1232. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1233. if (!sock)
  1234. goto out;
  1235. err = move_addr_to_kernel(uservaddr, addrlen, address);
  1236. if (err < 0)
  1237. goto out_put;
  1238. err =
  1239. security_socket_connect(sock, (struct sockaddr *)address, addrlen);
  1240. if (err)
  1241. goto out_put;
  1242. err = sock->ops->connect(sock, (struct sockaddr *)address, addrlen,
  1243. sock->file->f_flags);
  1244. out_put:
  1245. fput_light(sock->file, fput_needed);
  1246. out:
  1247. return err;
  1248. }
  1249. /*
  1250. * Get the local address ('name') of a socket object. Move the obtained
  1251. * name to user space.
  1252. */
  1253. asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr,
  1254. int __user *usockaddr_len)
  1255. {
  1256. struct socket *sock;
  1257. char address[MAX_SOCK_ADDR];
  1258. int len, err, fput_needed;
  1259. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1260. if (!sock)
  1261. goto out;
  1262. err = security_socket_getsockname(sock);
  1263. if (err)
  1264. goto out_put;
  1265. err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 0);
  1266. if (err)
  1267. goto out_put;
  1268. err = move_addr_to_user(address, len, usockaddr, usockaddr_len);
  1269. out_put:
  1270. fput_light(sock->file, fput_needed);
  1271. out:
  1272. return err;
  1273. }
  1274. /*
  1275. * Get the remote address ('name') of a socket object. Move the obtained
  1276. * name to user space.
  1277. */
  1278. asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr,
  1279. int __user *usockaddr_len)
  1280. {
  1281. struct socket *sock;
  1282. char address[MAX_SOCK_ADDR];
  1283. int len, err, fput_needed;
  1284. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1285. if (sock != NULL) {
  1286. err = security_socket_getpeername(sock);
  1287. if (err) {
  1288. fput_light(sock->file, fput_needed);
  1289. return err;
  1290. }
  1291. err =
  1292. sock->ops->getname(sock, (struct sockaddr *)address, &len,
  1293. 1);
  1294. if (!err)
  1295. err = move_addr_to_user(address, len, usockaddr,
  1296. usockaddr_len);
  1297. fput_light(sock->file, fput_needed);
  1298. }
  1299. return err;
  1300. }
  1301. /*
  1302. * Send a datagram to a given address. We move the address into kernel
  1303. * space and check the user space data area is readable before invoking
  1304. * the protocol.
  1305. */
  1306. asmlinkage long sys_sendto(int fd, void __user *buff, size_t len,
  1307. unsigned flags, struct sockaddr __user *addr,
  1308. int addr_len)
  1309. {
  1310. struct socket *sock;
  1311. char address[MAX_SOCK_ADDR];
  1312. int err;
  1313. struct msghdr msg;
  1314. struct iovec iov;
  1315. int fput_needed;
  1316. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1317. if (!sock)
  1318. goto out;
  1319. iov.iov_base = buff;
  1320. iov.iov_len = len;
  1321. msg.msg_name = NULL;
  1322. msg.msg_iov = &iov;
  1323. msg.msg_iovlen = 1;
  1324. msg.msg_control = NULL;
  1325. msg.msg_controllen = 0;
  1326. msg.msg_namelen = 0;
  1327. if (addr) {
  1328. err = move_addr_to_kernel(addr, addr_len, address);
  1329. if (err < 0)
  1330. goto out_put;
  1331. msg.msg_name = address;
  1332. msg.msg_namelen = addr_len;
  1333. }
  1334. if (sock->file->f_flags & O_NONBLOCK)
  1335. flags |= MSG_DONTWAIT;
  1336. msg.msg_flags = flags;
  1337. err = sock_sendmsg(sock, &msg, len);
  1338. out_put:
  1339. fput_light(sock->file, fput_needed);
  1340. out:
  1341. return err;
  1342. }
  1343. /*
  1344. * Send a datagram down a socket.
  1345. */
  1346. asmlinkage long sys_send(int fd, void __user *buff, size_t len, unsigned flags)
  1347. {
  1348. return sys_sendto(fd, buff, len, flags, NULL, 0);
  1349. }
  1350. /*
  1351. * Receive a frame from the socket and optionally record the address of the
  1352. * sender. We verify the buffers are writable and if needed move the
  1353. * sender address from kernel to user space.
  1354. */
  1355. asmlinkage long sys_recvfrom(int fd, void __user *ubuf, size_t size,
  1356. unsigned flags, struct sockaddr __user *addr,
  1357. int __user *addr_len)
  1358. {
  1359. struct socket *sock;
  1360. struct iovec iov;
  1361. struct msghdr msg;
  1362. char address[MAX_SOCK_ADDR];
  1363. int err, err2;
  1364. int fput_needed;
  1365. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1366. if (!sock)
  1367. goto out;
  1368. msg.msg_control = NULL;
  1369. msg.msg_controllen = 0;
  1370. msg.msg_iovlen = 1;
  1371. msg.msg_iov = &iov;
  1372. iov.iov_len = size;
  1373. iov.iov_base = ubuf;
  1374. msg.msg_name = address;
  1375. msg.msg_namelen = MAX_SOCK_ADDR;
  1376. if (sock->file->f_flags & O_NONBLOCK)
  1377. flags |= MSG_DONTWAIT;
  1378. err = sock_recvmsg(sock, &msg, size, flags);
  1379. if (err >= 0 && addr != NULL) {
  1380. err2 = move_addr_to_user(address, msg.msg_namelen, addr, addr_len);
  1381. if (err2 < 0)
  1382. err = err2;
  1383. }
  1384. fput_light(sock->file, fput_needed);
  1385. out:
  1386. return err;
  1387. }
  1388. /*
  1389. * Receive a datagram from a socket.
  1390. */
  1391. asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
  1392. unsigned flags)
  1393. {
  1394. return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
  1395. }
  1396. /*
  1397. * Set a socket option. Because we don't know the option lengths we have
  1398. * to pass the user mode parameter for the protocols to sort out.
  1399. */
  1400. asmlinkage long sys_setsockopt(int fd, int level, int optname,
  1401. char __user *optval, int optlen)
  1402. {
  1403. int err, fput_needed;
  1404. struct socket *sock;
  1405. if (optlen < 0)
  1406. return -EINVAL;
  1407. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1408. if (sock != NULL) {
  1409. err = security_socket_setsockopt(sock, level, optname);
  1410. if (err)
  1411. goto out_put;
  1412. if (level == SOL_SOCKET)
  1413. err =
  1414. sock_setsockopt(sock, level, optname, optval,
  1415. optlen);
  1416. else
  1417. err =
  1418. sock->ops->setsockopt(sock, level, optname, optval,
  1419. optlen);
  1420. out_put:
  1421. fput_light(sock->file, fput_needed);
  1422. }
  1423. return err;
  1424. }
  1425. /*
  1426. * Get a socket option. Because we don't know the option lengths we have
  1427. * to pass a user mode parameter for the protocols to sort out.
  1428. */
  1429. asmlinkage long sys_getsockopt(int fd, int level, int optname,
  1430. char __user *optval, int __user *optlen)
  1431. {
  1432. int err, fput_needed;
  1433. struct socket *sock;
  1434. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1435. if (sock != NULL) {
  1436. err = security_socket_getsockopt(sock, level, optname);
  1437. if (err)
  1438. goto out_put;
  1439. if (level == SOL_SOCKET)
  1440. err =
  1441. sock_getsockopt(sock, level, optname, optval,
  1442. optlen);
  1443. else
  1444. err =
  1445. sock->ops->getsockopt(sock, level, optname, optval,
  1446. optlen);
  1447. out_put:
  1448. fput_light(sock->file, fput_needed);
  1449. }
  1450. return err;
  1451. }
  1452. /*
  1453. * Shutdown a socket.
  1454. */
  1455. asmlinkage long sys_shutdown(int fd, int how)
  1456. {
  1457. int err, fput_needed;
  1458. struct socket *sock;
  1459. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1460. if (sock != NULL) {
  1461. err = security_socket_shutdown(sock, how);
  1462. if (!err)
  1463. err = sock->ops->shutdown(sock, how);
  1464. fput_light(sock->file, fput_needed);
  1465. }
  1466. return err;
  1467. }
  1468. /* A couple of helpful macros for getting the address of the 32/64 bit
  1469. * fields which are the same type (int / unsigned) on our platforms.
  1470. */
  1471. #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
  1472. #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
  1473. #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
  1474. /*
  1475. * BSD sendmsg interface
  1476. */
  1477. asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
  1478. {
  1479. struct compat_msghdr __user *msg_compat =
  1480. (struct compat_msghdr __user *)msg;
  1481. struct socket *sock;
  1482. char address[MAX_SOCK_ADDR];
  1483. struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
  1484. unsigned char ctl[sizeof(struct cmsghdr) + 20]
  1485. __attribute__ ((aligned(sizeof(__kernel_size_t))));
  1486. /* 20 is size of ipv6_pktinfo */
  1487. unsigned char *ctl_buf = ctl;
  1488. struct msghdr msg_sys;
  1489. int err, ctl_len, iov_size, total_len;
  1490. int fput_needed;
  1491. err = -EFAULT;
  1492. if (MSG_CMSG_COMPAT & flags) {
  1493. if (get_compat_msghdr(&msg_sys, msg_compat))
  1494. return -EFAULT;
  1495. }
  1496. else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
  1497. return -EFAULT;
  1498. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1499. if (!sock)
  1500. goto out;
  1501. /* do not move before msg_sys is valid */
  1502. err = -EMSGSIZE;
  1503. if (msg_sys.msg_iovlen > UIO_MAXIOV)
  1504. goto out_put;
  1505. /* Check whether to allocate the iovec area */
  1506. err = -ENOMEM;
  1507. iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
  1508. if (msg_sys.msg_iovlen > UIO_FASTIOV) {
  1509. iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
  1510. if (!iov)
  1511. goto out_put;
  1512. }
  1513. /* This will also move the address data into kernel space */
  1514. if (MSG_CMSG_COMPAT & flags) {
  1515. err = verify_compat_iovec(&msg_sys, iov, address, VERIFY_READ);
  1516. } else
  1517. err = verify_iovec(&msg_sys, iov, address, VERIFY_READ);
  1518. if (err < 0)
  1519. goto out_freeiov;
  1520. total_len = err;
  1521. err = -ENOBUFS;
  1522. if (msg_sys.msg_controllen > INT_MAX)
  1523. goto out_freeiov;
  1524. ctl_len = msg_sys.msg_controllen;
  1525. if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
  1526. err =
  1527. cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl,
  1528. sizeof(ctl));
  1529. if (err)
  1530. goto out_freeiov;
  1531. ctl_buf = msg_sys.msg_control;
  1532. ctl_len = msg_sys.msg_controllen;
  1533. } else if (ctl_len) {
  1534. if (ctl_len > sizeof(ctl)) {
  1535. ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
  1536. if (ctl_buf == NULL)
  1537. goto out_freeiov;
  1538. }
  1539. err = -EFAULT;
  1540. /*
  1541. * Careful! Before this, msg_sys.msg_control contains a user pointer.
  1542. * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
  1543. * checking falls down on this.
  1544. */
  1545. if (copy_from_user(ctl_buf,
  1546. (void __user __force *)msg_sys.msg_control,
  1547. ctl_len))
  1548. goto out_freectl;
  1549. msg_sys.msg_control = ctl_buf;
  1550. }
  1551. msg_sys.msg_flags = flags;
  1552. if (sock->file->f_flags & O_NONBLOCK)
  1553. msg_sys.msg_flags |= MSG_DONTWAIT;
  1554. err = sock_sendmsg(sock, &msg_sys, total_len);
  1555. out_freectl:
  1556. if (ctl_buf != ctl)
  1557. sock_kfree_s(sock->sk, ctl_buf, ctl_len);
  1558. out_freeiov:
  1559. if (iov != iovstack)
  1560. sock_kfree_s(sock->sk, iov, iov_size);
  1561. out_put:
  1562. fput_light(sock->file, fput_needed);
  1563. out:
  1564. return err;
  1565. }
  1566. /*
  1567. * BSD recvmsg interface
  1568. */
  1569. asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg,
  1570. unsigned int flags)
  1571. {
  1572. struct compat_msghdr __user *msg_compat =
  1573. (struct compat_msghdr __user *)msg;
  1574. struct socket *sock;
  1575. struct iovec iovstack[UIO_FASTIOV];
  1576. struct iovec *iov = iovstack;
  1577. struct msghdr msg_sys;
  1578. unsigned long cmsg_ptr;
  1579. int err, iov_size, total_len, len;
  1580. int fput_needed;
  1581. /* kernel mode address */
  1582. char addr[MAX_SOCK_ADDR];
  1583. /* user mode address pointers */
  1584. struct sockaddr __user *uaddr;
  1585. int __user *uaddr_len;
  1586. if (MSG_CMSG_COMPAT & flags) {
  1587. if (get_compat_msghdr(&msg_sys, msg_compat))
  1588. return -EFAULT;
  1589. }
  1590. else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
  1591. return -EFAULT;
  1592. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1593. if (!sock)
  1594. goto out;
  1595. err = -EMSGSIZE;
  1596. if (msg_sys.msg_iovlen > UIO_MAXIOV)
  1597. goto out_put;
  1598. /* Check whether to allocate the iovec area */
  1599. err = -ENOMEM;
  1600. iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
  1601. if (msg_sys.msg_iovlen > UIO_FASTIOV) {
  1602. iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
  1603. if (!iov)
  1604. goto out_put;
  1605. }
  1606. /*
  1607. * Save the user-mode address (verify_iovec will change the
  1608. * kernel msghdr to use the kernel address space)
  1609. */
  1610. uaddr = (__force void __user *)msg_sys.msg_name;
  1611. uaddr_len = COMPAT_NAMELEN(msg);
  1612. if (MSG_CMSG_COMPAT & flags) {
  1613. err = verify_compat_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
  1614. } else
  1615. err = verify_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
  1616. if (err < 0)
  1617. goto out_freeiov;
  1618. total_len = err;
  1619. cmsg_ptr = (unsigned long)msg_sys.msg_control;
  1620. msg_sys.msg_flags = 0;
  1621. if (MSG_CMSG_COMPAT & flags)
  1622. msg_sys.msg_flags = MSG_CMSG_COMPAT;
  1623. if (sock->file->f_flags & O_NONBLOCK)
  1624. flags |= MSG_DONTWAIT;
  1625. err = sock_recvmsg(sock, &msg_sys, total_len, flags);
  1626. if (err < 0)
  1627. goto out_freeiov;
  1628. len = err;
  1629. if (uaddr != NULL) {
  1630. err = move_addr_to_user(addr, msg_sys.msg_namelen, uaddr,
  1631. uaddr_len);
  1632. if (err < 0)
  1633. goto out_freeiov;
  1634. }
  1635. err = __put_user((msg_sys.msg_flags & ~MSG_CMSG_COMPAT),
  1636. COMPAT_FLAGS(msg));
  1637. if (err)
  1638. goto out_freeiov;
  1639. if (MSG_CMSG_COMPAT & flags)
  1640. err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
  1641. &msg_compat->msg_controllen);
  1642. else
  1643. err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
  1644. &msg->msg_controllen);
  1645. if (err)
  1646. goto out_freeiov;
  1647. err = len;
  1648. out_freeiov:
  1649. if (iov != iovstack)
  1650. sock_kfree_s(sock->sk, iov, iov_size);
  1651. out_put:
  1652. fput_light(sock->file, fput_needed);
  1653. out:
  1654. return err;
  1655. }
  1656. #ifdef __ARCH_WANT_SYS_SOCKETCALL
  1657. /* Argument list sizes for sys_socketcall */
  1658. #define AL(x) ((x) * sizeof(unsigned long))
  1659. static const unsigned char nargs[18]={
  1660. AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
  1661. AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
  1662. AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)
  1663. };
  1664. #undef AL
  1665. /*
  1666. * System call vectors.
  1667. *
  1668. * Argument checking cleaned up. Saved 20% in size.
  1669. * This function doesn't need to set the kernel lock because
  1670. * it is set by the callees.
  1671. */
  1672. asmlinkage long sys_socketcall(int call, unsigned long __user *args)
  1673. {
  1674. unsigned long a[6];
  1675. unsigned long a0, a1;
  1676. int err;
  1677. if (call < 1 || call > SYS_RECVMSG)
  1678. return -ENOSYS;
  1679. /* copy_from_user should be SMP safe. */
  1680. if (copy_from_user(a, args, nargs[call]))
  1681. return -EFAULT;
  1682. err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
  1683. if (err)
  1684. return err;
  1685. a0 = a[0];
  1686. a1 = a[1];
  1687. switch (call) {
  1688. case SYS_SOCKET:
  1689. err = sys_socket(a0, a1, a[2]);
  1690. break;
  1691. case SYS_BIND:
  1692. err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
  1693. break;
  1694. case SYS_CONNECT:
  1695. err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
  1696. break;
  1697. case SYS_LISTEN:
  1698. err = sys_listen(a0, a1);
  1699. break;
  1700. case SYS_ACCEPT:
  1701. err =
  1702. sys_accept(a0, (struct sockaddr __user *)a1,
  1703. (int __user *)a[2]);
  1704. break;
  1705. case SYS_GETSOCKNAME:
  1706. err =
  1707. sys_getsockname(a0, (struct sockaddr __user *)a1,
  1708. (int __user *)a[2]);
  1709. break;
  1710. case SYS_GETPEERNAME:
  1711. err =
  1712. sys_getpeername(a0, (struct sockaddr __user *)a1,
  1713. (int __user *)a[2]);
  1714. break;
  1715. case SYS_SOCKETPAIR:
  1716. err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
  1717. break;
  1718. case SYS_SEND:
  1719. err = sys_send(a0, (void __user *)a1, a[2], a[3]);
  1720. break;
  1721. case SYS_SENDTO:
  1722. err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
  1723. (struct sockaddr __user *)a[4], a[5]);
  1724. break;
  1725. case SYS_RECV:
  1726. err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
  1727. break;
  1728. case SYS_RECVFROM:
  1729. err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
  1730. (struct sockaddr __user *)a[4],
  1731. (int __user *)a[5]);
  1732. break;
  1733. case SYS_SHUTDOWN:
  1734. err = sys_shutdown(a0, a1);
  1735. break;
  1736. case SYS_SETSOCKOPT:
  1737. err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
  1738. break;
  1739. case SYS_GETSOCKOPT:
  1740. err =
  1741. sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
  1742. (int __user *)a[4]);
  1743. break;
  1744. case SYS_SENDMSG:
  1745. err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
  1746. break;
  1747. case SYS_RECVMSG:
  1748. err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
  1749. break;
  1750. default:
  1751. err = -ENOSYS;
  1752. break;
  1753. }
  1754. return err;
  1755. }
  1756. #endif /* __ARCH_WANT_SYS_SOCKETCALL */
  1757. /**
  1758. * sock_register - add a socket protocol handler
  1759. * @ops: description of protocol
  1760. *
  1761. * This function is called by a protocol handler that wants to
  1762. * advertise its address family, and have it linked into the
  1763. * socket interface. The value ops->family coresponds to the
  1764. * socket system call protocol family.
  1765. */
  1766. int sock_register(const struct net_proto_family *ops)
  1767. {
  1768. int err;
  1769. if (ops->family >= NPROTO) {
  1770. printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
  1771. NPROTO);
  1772. return -ENOBUFS;
  1773. }
  1774. spin_lock(&net_family_lock);
  1775. if (net_families[ops->family])
  1776. err = -EEXIST;
  1777. else {
  1778. net_families[ops->family] = ops;
  1779. err = 0;
  1780. }
  1781. spin_unlock(&net_family_lock);
  1782. printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
  1783. return err;
  1784. }
  1785. /**
  1786. * sock_unregister - remove a protocol handler
  1787. * @family: protocol family to remove
  1788. *
  1789. * This function is called by a protocol handler that wants to
  1790. * remove its address family, and have it unlinked from the
  1791. * new socket creation.
  1792. *
  1793. * If protocol handler is a module, then it can use module reference
  1794. * counts to protect against new references. If protocol handler is not
  1795. * a module then it needs to provide its own protection in
  1796. * the ops->create routine.
  1797. */
  1798. void sock_unregister(int family)
  1799. {
  1800. BUG_ON(family < 0 || family >= NPROTO);
  1801. spin_lock(&net_family_lock);
  1802. net_families[family] = NULL;
  1803. spin_unlock(&net_family_lock);
  1804. synchronize_rcu();
  1805. printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
  1806. }
  1807. static int __init sock_init(void)
  1808. {
  1809. /*
  1810. * Initialize sock SLAB cache.
  1811. */
  1812. sk_init();
  1813. /*
  1814. * Initialize skbuff SLAB cache
  1815. */
  1816. skb_init();
  1817. /*
  1818. * Initialize the protocols module.
  1819. */
  1820. init_inodecache();
  1821. register_filesystem(&sock_fs_type);
  1822. sock_mnt = kern_mount(&sock_fs_type);
  1823. /* The real protocol initialization is performed in later initcalls.
  1824. */
  1825. #ifdef CONFIG_NETFILTER
  1826. netfilter_init();
  1827. #endif
  1828. return 0;
  1829. }
  1830. core_initcall(sock_init); /* early initcall */
  1831. #ifdef CONFIG_PROC_FS
  1832. void socket_seq_show(struct seq_file *seq)
  1833. {
  1834. int cpu;
  1835. int counter = 0;
  1836. for_each_possible_cpu(cpu)
  1837. counter += per_cpu(sockets_in_use, cpu);
  1838. /* It can be negative, by the way. 8) */
  1839. if (counter < 0)
  1840. counter = 0;
  1841. seq_printf(seq, "sockets: used %d\n", counter);
  1842. }
  1843. #endif /* CONFIG_PROC_FS */
  1844. #ifdef CONFIG_COMPAT
  1845. static long compat_sock_ioctl(struct file *file, unsigned cmd,
  1846. unsigned long arg)
  1847. {
  1848. struct socket *sock = file->private_data;
  1849. int ret = -ENOIOCTLCMD;
  1850. if (sock->ops->compat_ioctl)
  1851. ret = sock->ops->compat_ioctl(sock, cmd, arg);
  1852. return ret;
  1853. }
  1854. #endif
  1855. int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
  1856. {
  1857. return sock->ops->bind(sock, addr, addrlen);
  1858. }
  1859. int kernel_listen(struct socket *sock, int backlog)
  1860. {
  1861. return sock->ops->listen(sock, backlog);
  1862. }
  1863. int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
  1864. {
  1865. struct sock *sk = sock->sk;
  1866. int err;
  1867. err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
  1868. newsock);
  1869. if (err < 0)
  1870. goto done;
  1871. err = sock->ops->accept(sock, *newsock, flags);
  1872. if (err < 0) {
  1873. sock_release(*newsock);
  1874. *newsock = NULL;
  1875. goto done;
  1876. }
  1877. (*newsock)->ops = sock->ops;
  1878. done:
  1879. return err;
  1880. }
  1881. int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
  1882. int flags)
  1883. {
  1884. return sock->ops->connect(sock, addr, addrlen, flags);
  1885. }
  1886. int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
  1887. int *addrlen)
  1888. {
  1889. return sock->ops->getname(sock, addr, addrlen, 0);
  1890. }
  1891. int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
  1892. int *addrlen)
  1893. {
  1894. return sock->ops->getname(sock, addr, addrlen, 1);
  1895. }
  1896. int kernel_getsockopt(struct socket *sock, int level, int optname,
  1897. char *optval, int *optlen)
  1898. {
  1899. mm_segment_t oldfs = get_fs();
  1900. char __user *uoptval;
  1901. int __user *uoptlen;
  1902. int err;
  1903. uoptval = (char __user __force *) optval;
  1904. uoptlen = (int __user __force *) optlen;
  1905. set_fs(KERNEL_DS);
  1906. if (level == SOL_SOCKET)
  1907. err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
  1908. else
  1909. err = sock->ops->getsockopt(sock, level, optname, uoptval,
  1910. uoptlen);
  1911. set_fs(oldfs);
  1912. return err;
  1913. }
  1914. int kernel_setsockopt(struct socket *sock, int level, int optname,
  1915. char *optval, int optlen)
  1916. {
  1917. mm_segment_t oldfs = get_fs();
  1918. char __user *uoptval;
  1919. int err;
  1920. uoptval = (char __user __force *) optval;
  1921. set_fs(KERNEL_DS);
  1922. if (level == SOL_SOCKET)
  1923. err = sock_setsockopt(sock, level, optname, uoptval, optlen);
  1924. else
  1925. err = sock->ops->setsockopt(sock, level, optname, uoptval,
  1926. optlen);
  1927. set_fs(oldfs);
  1928. return err;
  1929. }
  1930. int kernel_sendpage(struct socket *sock, struct page *page, int offset,
  1931. size_t size, int flags)
  1932. {
  1933. if (sock->ops->sendpage)
  1934. return sock->ops->sendpage(sock, page, offset, size, flags);
  1935. return sock_no_sendpage(sock, page, offset, size, flags);
  1936. }
  1937. int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
  1938. {
  1939. mm_segment_t oldfs = get_fs();
  1940. int err;
  1941. set_fs(KERNEL_DS);
  1942. err = sock->ops->ioctl(sock, cmd, arg);
  1943. set_fs(oldfs);
  1944. return err;
  1945. }
  1946. /* ABI emulation layers need these two */
  1947. EXPORT_SYMBOL(move_addr_to_kernel);
  1948. EXPORT_SYMBOL(move_addr_to_user);
  1949. EXPORT_SYMBOL(sock_create);
  1950. EXPORT_SYMBOL(sock_create_kern);
  1951. EXPORT_SYMBOL(sock_create_lite);
  1952. EXPORT_SYMBOL(sock_map_fd);
  1953. EXPORT_SYMBOL(sock_recvmsg);
  1954. EXPORT_SYMBOL(sock_register);
  1955. EXPORT_SYMBOL(sock_release);
  1956. EXPORT_SYMBOL(sock_sendmsg);
  1957. EXPORT_SYMBOL(sock_unregister);
  1958. EXPORT_SYMBOL(sock_wake_async);
  1959. EXPORT_SYMBOL(sockfd_lookup);
  1960. EXPORT_SYMBOL(kernel_sendmsg);
  1961. EXPORT_SYMBOL(kernel_recvmsg);
  1962. EXPORT_SYMBOL(kernel_bind);
  1963. EXPORT_SYMBOL(kernel_listen);
  1964. EXPORT_SYMBOL(kernel_accept);
  1965. EXPORT_SYMBOL(kernel_connect);
  1966. EXPORT_SYMBOL(kernel_getsockname);
  1967. EXPORT_SYMBOL(kernel_getpeername);
  1968. EXPORT_SYMBOL(kernel_getsockopt);
  1969. EXPORT_SYMBOL(kernel_setsockopt);
  1970. EXPORT_SYMBOL(kernel_sendpage);
  1971. EXPORT_SYMBOL(kernel_sock_ioctl);