PageRenderTime 69ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/linux-source-2.6.32/net/socket.c

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