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

/net/socket.c

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