11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * NET An implementation of the SOCKET network access protocol. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Version: @(#)socket.c 1.1.93 18/02/95 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Authors: Orest Zborowski, <obz@Kodak.COM> 702c30a84SJesper Juhl * Ross Biro 81da177e4SLinus Torvalds * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * Fixes: 111da177e4SLinus Torvalds * Anonymous : NOTSOCK/BADF cleanup. Error fix in 121da177e4SLinus Torvalds * shutdown() 131da177e4SLinus Torvalds * Alan Cox : verify_area() fixes 141da177e4SLinus Torvalds * Alan Cox : Removed DDI 151da177e4SLinus Torvalds * Jonathan Kamens : SOCK_DGRAM reconnect bug 161da177e4SLinus Torvalds * Alan Cox : Moved a load of checks to the very 171da177e4SLinus Torvalds * top level. 181da177e4SLinus Torvalds * Alan Cox : Move address structures to/from user 191da177e4SLinus Torvalds * mode above the protocol layers. 201da177e4SLinus Torvalds * Rob Janssen : Allow 0 length sends. 211da177e4SLinus Torvalds * Alan Cox : Asynchronous I/O support (cribbed from the 221da177e4SLinus Torvalds * tty drivers). 231da177e4SLinus Torvalds * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style) 241da177e4SLinus Torvalds * Jeff Uphoff : Made max number of sockets command-line 251da177e4SLinus Torvalds * configurable. 261da177e4SLinus Torvalds * Matti Aarnio : Made the number of sockets dynamic, 271da177e4SLinus Torvalds * to be allocated when needed, and mr. 281da177e4SLinus Torvalds * Uphoff's max is used as max to be 291da177e4SLinus Torvalds * allowed to allocate. 301da177e4SLinus Torvalds * Linus : Argh. removed all the socket allocation 311da177e4SLinus Torvalds * altogether: it's in the inode now. 321da177e4SLinus Torvalds * Alan Cox : Made sock_alloc()/sock_release() public 331da177e4SLinus Torvalds * for NetROM and future kernel nfsd type 341da177e4SLinus Torvalds * stuff. 351da177e4SLinus Torvalds * Alan Cox : sendmsg/recvmsg basics. 361da177e4SLinus Torvalds * Tom Dyas : Export net symbols. 371da177e4SLinus Torvalds * Marcin Dalecki : Fixed problems with CONFIG_NET="n". 381da177e4SLinus Torvalds * Alan Cox : Added thread locking to sys_* calls 391da177e4SLinus Torvalds * for sockets. May have errors at the 401da177e4SLinus Torvalds * moment. 411da177e4SLinus Torvalds * Kevin Buhr : Fixed the dumb errors in the above. 421da177e4SLinus Torvalds * Andi Kleen : Some small cleanups, optimizations, 431da177e4SLinus Torvalds * and fixed a copy_from_user() bug. 441da177e4SLinus Torvalds * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0) 451da177e4SLinus Torvalds * Tigran Aivazian : Made listen(2) backlog sanity checks 461da177e4SLinus Torvalds * protocol-independent 471da177e4SLinus Torvalds * 481da177e4SLinus Torvalds * 491da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 501da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 511da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 521da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 531da177e4SLinus Torvalds * 541da177e4SLinus Torvalds * 551da177e4SLinus Torvalds * This module is effectively the top level interface to the BSD socket 561da177e4SLinus Torvalds * paradigm. 571da177e4SLinus Torvalds * 581da177e4SLinus Torvalds * Based upon Swansea University Computer Society NET3.039 591da177e4SLinus Torvalds */ 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds #include <linux/mm.h> 621da177e4SLinus Torvalds #include <linux/socket.h> 631da177e4SLinus Torvalds #include <linux/file.h> 641da177e4SLinus Torvalds #include <linux/net.h> 651da177e4SLinus Torvalds #include <linux/interrupt.h> 6655737fdaSStephen Hemminger #include <linux/rcupdate.h> 671da177e4SLinus Torvalds #include <linux/netdevice.h> 681da177e4SLinus Torvalds #include <linux/proc_fs.h> 691da177e4SLinus Torvalds #include <linux/seq_file.h> 704a3e2f71SArjan van de Ven #include <linux/mutex.h> 711da177e4SLinus Torvalds #include <linux/wanrouter.h> 721da177e4SLinus Torvalds #include <linux/if_bridge.h> 7320380731SArnaldo Carvalho de Melo #include <linux/if_frad.h> 7420380731SArnaldo Carvalho de Melo #include <linux/if_vlan.h> 751da177e4SLinus Torvalds #include <linux/init.h> 761da177e4SLinus Torvalds #include <linux/poll.h> 771da177e4SLinus Torvalds #include <linux/cache.h> 781da177e4SLinus Torvalds #include <linux/module.h> 791da177e4SLinus Torvalds #include <linux/highmem.h> 801da177e4SLinus Torvalds #include <linux/mount.h> 811da177e4SLinus Torvalds #include <linux/security.h> 821da177e4SLinus Torvalds #include <linux/syscalls.h> 831da177e4SLinus Torvalds #include <linux/compat.h> 841da177e4SLinus Torvalds #include <linux/kmod.h> 853ec3b2fbSDavid Woodhouse #include <linux/audit.h> 86d86b5e0eSAdrian Bunk #include <linux/wireless.h> 871b8d7ae4SEric W. Biederman #include <linux/nsproxy.h> 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds #include <asm/uaccess.h> 901da177e4SLinus Torvalds #include <asm/unistd.h> 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds #include <net/compat.h> 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds #include <net/sock.h> 951da177e4SLinus Torvalds #include <linux/netfilter.h> 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds static int sock_no_open(struct inode *irrelevant, struct file *dontcare); 98027445c3SBadari Pulavarty static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov, 99027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos); 100027445c3SBadari Pulavarty static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov, 101027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos); 1021da177e4SLinus Torvalds static int sock_mmap(struct file *file, struct vm_area_struct *vma); 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds static int sock_close(struct inode *inode, struct file *file); 1051da177e4SLinus Torvalds static unsigned int sock_poll(struct file *file, 1061da177e4SLinus Torvalds struct poll_table_struct *wait); 10789bddce5SStephen Hemminger static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 10889bbfc95SShaun Pereira #ifdef CONFIG_COMPAT 10989bbfc95SShaun Pereira static long compat_sock_ioctl(struct file *file, 11089bbfc95SShaun Pereira unsigned int cmd, unsigned long arg); 11189bbfc95SShaun Pereira #endif 1121da177e4SLinus Torvalds static int sock_fasync(int fd, struct file *filp, int on); 1131da177e4SLinus Torvalds static ssize_t sock_sendpage(struct file *file, struct page *page, 1141da177e4SLinus Torvalds int offset, size_t size, loff_t *ppos, int more); 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds /* 1171da177e4SLinus Torvalds * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear 1181da177e4SLinus Torvalds * in the operation structures but are done directly via the socketcall() multiplexor. 1191da177e4SLinus Torvalds */ 1201da177e4SLinus Torvalds 121da7071d7SArjan van de Ven static const struct file_operations socket_file_ops = { 1221da177e4SLinus Torvalds .owner = THIS_MODULE, 1231da177e4SLinus Torvalds .llseek = no_llseek, 1241da177e4SLinus Torvalds .aio_read = sock_aio_read, 1251da177e4SLinus Torvalds .aio_write = sock_aio_write, 1261da177e4SLinus Torvalds .poll = sock_poll, 1271da177e4SLinus Torvalds .unlocked_ioctl = sock_ioctl, 12889bbfc95SShaun Pereira #ifdef CONFIG_COMPAT 12989bbfc95SShaun Pereira .compat_ioctl = compat_sock_ioctl, 13089bbfc95SShaun Pereira #endif 1311da177e4SLinus Torvalds .mmap = sock_mmap, 1321da177e4SLinus Torvalds .open = sock_no_open, /* special open code to disallow open via /proc */ 1331da177e4SLinus Torvalds .release = sock_close, 1341da177e4SLinus Torvalds .fasync = sock_fasync, 1355274f052SJens Axboe .sendpage = sock_sendpage, 1365274f052SJens Axboe .splice_write = generic_splice_sendpage, 1371da177e4SLinus Torvalds }; 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds /* 1401da177e4SLinus Torvalds * The protocol list. Each protocol is registered in here. 1411da177e4SLinus Torvalds */ 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds static DEFINE_SPINLOCK(net_family_lock); 144f0fd27d4SStephen Hemminger static const struct net_proto_family *net_families[NPROTO] __read_mostly; 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds /* 1471da177e4SLinus Torvalds * Statistics counters of the socket lists 1481da177e4SLinus Torvalds */ 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds static DEFINE_PER_CPU(int, sockets_in_use) = 0; 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds /* 15389bddce5SStephen Hemminger * Support routines. 15489bddce5SStephen Hemminger * Move socket addresses back and forth across the kernel/user 1551da177e4SLinus Torvalds * divide and look after the messy bits. 1561da177e4SLinus Torvalds */ 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds #define MAX_SOCK_ADDR 128 /* 108 for Unix domain - 1591da177e4SLinus Torvalds 16 for IP, 16 for IPX, 1601da177e4SLinus Torvalds 24 for IPv6, 1611da177e4SLinus Torvalds about 80 for AX.25 1621da177e4SLinus Torvalds must be at least one bigger than 1631da177e4SLinus Torvalds the AF_UNIX size (see net/unix/af_unix.c 1641da177e4SLinus Torvalds :unix_mkname()). 1651da177e4SLinus Torvalds */ 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds /** 1681da177e4SLinus Torvalds * move_addr_to_kernel - copy a socket address into kernel space 1691da177e4SLinus Torvalds * @uaddr: Address in user space 1701da177e4SLinus Torvalds * @kaddr: Address in kernel space 1711da177e4SLinus Torvalds * @ulen: Length in user space 1721da177e4SLinus Torvalds * 1731da177e4SLinus Torvalds * The address is copied into kernel space. If the provided address is 1741da177e4SLinus Torvalds * too long an error code of -EINVAL is returned. If the copy gives 1751da177e4SLinus Torvalds * invalid addresses -EFAULT is returned. On a success 0 is returned. 1761da177e4SLinus Torvalds */ 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds int move_addr_to_kernel(void __user *uaddr, int ulen, void *kaddr) 1791da177e4SLinus Torvalds { 1801da177e4SLinus Torvalds if (ulen < 0 || ulen > MAX_SOCK_ADDR) 1811da177e4SLinus Torvalds return -EINVAL; 1821da177e4SLinus Torvalds if (ulen == 0) 1831da177e4SLinus Torvalds return 0; 1841da177e4SLinus Torvalds if (copy_from_user(kaddr, uaddr, ulen)) 1851da177e4SLinus Torvalds return -EFAULT; 1863ec3b2fbSDavid Woodhouse return audit_sockaddr(ulen, kaddr); 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds /** 1901da177e4SLinus Torvalds * move_addr_to_user - copy an address to user space 1911da177e4SLinus Torvalds * @kaddr: kernel space address 1921da177e4SLinus Torvalds * @klen: length of address in kernel 1931da177e4SLinus Torvalds * @uaddr: user space address 1941da177e4SLinus Torvalds * @ulen: pointer to user length field 1951da177e4SLinus Torvalds * 1961da177e4SLinus Torvalds * The value pointed to by ulen on entry is the buffer length available. 1971da177e4SLinus Torvalds * This is overwritten with the buffer space used. -EINVAL is returned 1981da177e4SLinus Torvalds * if an overlong buffer is specified or a negative buffer size. -EFAULT 1991da177e4SLinus Torvalds * is returned if either the buffer or the length field are not 2001da177e4SLinus Torvalds * accessible. 2011da177e4SLinus Torvalds * After copying the data up to the limit the user specifies, the true 2021da177e4SLinus Torvalds * length of the data is written over the length limit the user 2031da177e4SLinus Torvalds * specified. Zero is returned for a success. 2041da177e4SLinus Torvalds */ 2051da177e4SLinus Torvalds 20689bddce5SStephen Hemminger int move_addr_to_user(void *kaddr, int klen, void __user *uaddr, 20789bddce5SStephen Hemminger int __user *ulen) 2081da177e4SLinus Torvalds { 2091da177e4SLinus Torvalds int err; 2101da177e4SLinus Torvalds int len; 2111da177e4SLinus Torvalds 21289bddce5SStephen Hemminger err = get_user(len, ulen); 21389bddce5SStephen Hemminger if (err) 2141da177e4SLinus Torvalds return err; 2151da177e4SLinus Torvalds if (len > klen) 2161da177e4SLinus Torvalds len = klen; 2171da177e4SLinus Torvalds if (len < 0 || len > MAX_SOCK_ADDR) 2181da177e4SLinus Torvalds return -EINVAL; 21989bddce5SStephen Hemminger if (len) { 220d6fe3945SSteve Grubb if (audit_sockaddr(klen, kaddr)) 221d6fe3945SSteve Grubb return -ENOMEM; 2221da177e4SLinus Torvalds if (copy_to_user(uaddr, kaddr, len)) 2231da177e4SLinus Torvalds return -EFAULT; 2241da177e4SLinus Torvalds } 2251da177e4SLinus Torvalds /* 2261da177e4SLinus Torvalds * "fromlen shall refer to the value before truncation.." 2271da177e4SLinus Torvalds * 1003.1g 2281da177e4SLinus Torvalds */ 2291da177e4SLinus Torvalds return __put_user(klen, ulen); 2301da177e4SLinus Torvalds } 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds #define SOCKFS_MAGIC 0x534F434B 2331da177e4SLinus Torvalds 234e18b890bSChristoph Lameter static struct kmem_cache *sock_inode_cachep __read_mostly; 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds static struct inode *sock_alloc_inode(struct super_block *sb) 2371da177e4SLinus Torvalds { 2381da177e4SLinus Torvalds struct socket_alloc *ei; 23989bddce5SStephen Hemminger 240e94b1766SChristoph Lameter ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL); 2411da177e4SLinus Torvalds if (!ei) 2421da177e4SLinus Torvalds return NULL; 2431da177e4SLinus Torvalds init_waitqueue_head(&ei->socket.wait); 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds ei->socket.fasync_list = NULL; 2461da177e4SLinus Torvalds ei->socket.state = SS_UNCONNECTED; 2471da177e4SLinus Torvalds ei->socket.flags = 0; 2481da177e4SLinus Torvalds ei->socket.ops = NULL; 2491da177e4SLinus Torvalds ei->socket.sk = NULL; 2501da177e4SLinus Torvalds ei->socket.file = NULL; 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds return &ei->vfs_inode; 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds static void sock_destroy_inode(struct inode *inode) 2561da177e4SLinus Torvalds { 2571da177e4SLinus Torvalds kmem_cache_free(sock_inode_cachep, 2581da177e4SLinus Torvalds container_of(inode, struct socket_alloc, vfs_inode)); 2591da177e4SLinus Torvalds } 2601da177e4SLinus Torvalds 261e18b890bSChristoph Lameter static void init_once(void *foo, struct kmem_cache *cachep, unsigned long flags) 2621da177e4SLinus Torvalds { 2631da177e4SLinus Torvalds struct socket_alloc *ei = (struct socket_alloc *)foo; 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds inode_init_once(&ei->vfs_inode); 2661da177e4SLinus Torvalds } 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds static int init_inodecache(void) 2691da177e4SLinus Torvalds { 2701da177e4SLinus Torvalds sock_inode_cachep = kmem_cache_create("sock_inode_cache", 2711da177e4SLinus Torvalds sizeof(struct socket_alloc), 27289bddce5SStephen Hemminger 0, 27389bddce5SStephen Hemminger (SLAB_HWCACHE_ALIGN | 27489bddce5SStephen Hemminger SLAB_RECLAIM_ACCOUNT | 275fffb60f9SPaul Jackson SLAB_MEM_SPREAD), 27620c2df83SPaul Mundt init_once); 2771da177e4SLinus Torvalds if (sock_inode_cachep == NULL) 2781da177e4SLinus Torvalds return -ENOMEM; 2791da177e4SLinus Torvalds return 0; 2801da177e4SLinus Torvalds } 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds static struct super_operations sockfs_ops = { 2831da177e4SLinus Torvalds .alloc_inode = sock_alloc_inode, 2841da177e4SLinus Torvalds .destroy_inode =sock_destroy_inode, 2851da177e4SLinus Torvalds .statfs = simple_statfs, 2861da177e4SLinus Torvalds }; 2871da177e4SLinus Torvalds 288454e2398SDavid Howells static int sockfs_get_sb(struct file_system_type *fs_type, 28989bddce5SStephen Hemminger int flags, const char *dev_name, void *data, 29089bddce5SStephen Hemminger struct vfsmount *mnt) 2911da177e4SLinus Torvalds { 292454e2398SDavid Howells return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC, 293454e2398SDavid Howells mnt); 2941da177e4SLinus Torvalds } 2951da177e4SLinus Torvalds 296ba89966cSEric Dumazet static struct vfsmount *sock_mnt __read_mostly; 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds static struct file_system_type sock_fs_type = { 2991da177e4SLinus Torvalds .name = "sockfs", 3001da177e4SLinus Torvalds .get_sb = sockfs_get_sb, 3011da177e4SLinus Torvalds .kill_sb = kill_anon_super, 3021da177e4SLinus Torvalds }; 30389bddce5SStephen Hemminger 3041da177e4SLinus Torvalds static int sockfs_delete_dentry(struct dentry *dentry) 3051da177e4SLinus Torvalds { 306304e61e6SEric Dumazet /* 307304e61e6SEric Dumazet * At creation time, we pretended this dentry was hashed 308304e61e6SEric Dumazet * (by clearing DCACHE_UNHASHED bit in d_flags) 309304e61e6SEric Dumazet * At delete time, we restore the truth : not hashed. 310304e61e6SEric Dumazet * (so that dput() can proceed correctly) 311304e61e6SEric Dumazet */ 312304e61e6SEric Dumazet dentry->d_flags |= DCACHE_UNHASHED; 313304e61e6SEric Dumazet return 0; 3141da177e4SLinus Torvalds } 315c23fbb6bSEric Dumazet 316c23fbb6bSEric Dumazet /* 317c23fbb6bSEric Dumazet * sockfs_dname() is called from d_path(). 318c23fbb6bSEric Dumazet */ 319c23fbb6bSEric Dumazet static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen) 320c23fbb6bSEric Dumazet { 321c23fbb6bSEric Dumazet return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]", 322c23fbb6bSEric Dumazet dentry->d_inode->i_ino); 323c23fbb6bSEric Dumazet } 324c23fbb6bSEric Dumazet 3251da177e4SLinus Torvalds static struct dentry_operations sockfs_dentry_operations = { 3261da177e4SLinus Torvalds .d_delete = sockfs_delete_dentry, 327c23fbb6bSEric Dumazet .d_dname = sockfs_dname, 3281da177e4SLinus Torvalds }; 3291da177e4SLinus Torvalds 3301da177e4SLinus Torvalds /* 3311da177e4SLinus Torvalds * Obtains the first available file descriptor and sets it up for use. 3321da177e4SLinus Torvalds * 33339d8c1b6SDavid S. Miller * These functions create file structures and maps them to fd space 33439d8c1b6SDavid S. Miller * of the current process. On success it returns file descriptor 3351da177e4SLinus Torvalds * and file struct implicitly stored in sock->file. 3361da177e4SLinus Torvalds * Note that another thread may close file descriptor before we return 3371da177e4SLinus Torvalds * from this function. We use the fact that now we do not refer 3381da177e4SLinus Torvalds * to socket after mapping. If one day we will need it, this 3391da177e4SLinus Torvalds * function will increment ref. count on file by 1. 3401da177e4SLinus Torvalds * 3411da177e4SLinus Torvalds * In any case returned fd MAY BE not valid! 3421da177e4SLinus Torvalds * This race condition is unavoidable 3431da177e4SLinus Torvalds * with shared fd spaces, we cannot solve it inside kernel, 3441da177e4SLinus Torvalds * but we take care of internal coherence yet. 3451da177e4SLinus Torvalds */ 3461da177e4SLinus Torvalds 34739d8c1b6SDavid S. Miller static int sock_alloc_fd(struct file **filep) 3481da177e4SLinus Torvalds { 3491da177e4SLinus Torvalds int fd; 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds fd = get_unused_fd(); 35239d8c1b6SDavid S. Miller if (likely(fd >= 0)) { 3531da177e4SLinus Torvalds struct file *file = get_empty_filp(); 3541da177e4SLinus Torvalds 35539d8c1b6SDavid S. Miller *filep = file; 35639d8c1b6SDavid S. Miller if (unlikely(!file)) { 3571da177e4SLinus Torvalds put_unused_fd(fd); 35839d8c1b6SDavid S. Miller return -ENFILE; 3591da177e4SLinus Torvalds } 36039d8c1b6SDavid S. Miller } else 36139d8c1b6SDavid S. Miller *filep = NULL; 36239d8c1b6SDavid S. Miller return fd; 36339d8c1b6SDavid S. Miller } 36439d8c1b6SDavid S. Miller 36539d8c1b6SDavid S. Miller static int sock_attach_fd(struct socket *sock, struct file *file) 36639d8c1b6SDavid S. Miller { 367c23fbb6bSEric Dumazet struct qstr name = { .name = "" }; 3681da177e4SLinus Torvalds 369c23fbb6bSEric Dumazet file->f_path.dentry = d_alloc(sock_mnt->mnt_sb->s_root, &name); 3703126a42cSJosef Sipek if (unlikely(!file->f_path.dentry)) 37139d8c1b6SDavid S. Miller return -ENOMEM; 37239d8c1b6SDavid S. Miller 3733126a42cSJosef Sipek file->f_path.dentry->d_op = &sockfs_dentry_operations; 374304e61e6SEric Dumazet /* 375304e61e6SEric Dumazet * We dont want to push this dentry into global dentry hash table. 376304e61e6SEric Dumazet * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED 377304e61e6SEric Dumazet * This permits a working /proc/$pid/fd/XXX on sockets 378304e61e6SEric Dumazet */ 3793126a42cSJosef Sipek file->f_path.dentry->d_flags &= ~DCACHE_UNHASHED; 3803126a42cSJosef Sipek d_instantiate(file->f_path.dentry, SOCK_INODE(sock)); 3813126a42cSJosef Sipek file->f_path.mnt = mntget(sock_mnt); 3823126a42cSJosef Sipek file->f_mapping = file->f_path.dentry->d_inode->i_mapping; 3831da177e4SLinus Torvalds 3841da177e4SLinus Torvalds sock->file = file; 3851da177e4SLinus Torvalds file->f_op = SOCK_INODE(sock)->i_fop = &socket_file_ops; 3861da177e4SLinus Torvalds file->f_mode = FMODE_READ | FMODE_WRITE; 3871da177e4SLinus Torvalds file->f_flags = O_RDWR; 3881da177e4SLinus Torvalds file->f_pos = 0; 38907dc3f07SBenjamin LaHaise file->private_data = sock; 39039d8c1b6SDavid S. Miller 39139d8c1b6SDavid S. Miller return 0; 3921da177e4SLinus Torvalds } 3931da177e4SLinus Torvalds 39439d8c1b6SDavid S. Miller int sock_map_fd(struct socket *sock) 39539d8c1b6SDavid S. Miller { 39639d8c1b6SDavid S. Miller struct file *newfile; 39739d8c1b6SDavid S. Miller int fd = sock_alloc_fd(&newfile); 39839d8c1b6SDavid S. Miller 39939d8c1b6SDavid S. Miller if (likely(fd >= 0)) { 40039d8c1b6SDavid S. Miller int err = sock_attach_fd(sock, newfile); 40139d8c1b6SDavid S. Miller 40239d8c1b6SDavid S. Miller if (unlikely(err < 0)) { 40339d8c1b6SDavid S. Miller put_filp(newfile); 40439d8c1b6SDavid S. Miller put_unused_fd(fd); 40539d8c1b6SDavid S. Miller return err; 40639d8c1b6SDavid S. Miller } 40739d8c1b6SDavid S. Miller fd_install(fd, newfile); 40839d8c1b6SDavid S. Miller } 4091da177e4SLinus Torvalds return fd; 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds 4126cb153caSBenjamin LaHaise static struct socket *sock_from_file(struct file *file, int *err) 4136cb153caSBenjamin LaHaise { 4146cb153caSBenjamin LaHaise if (file->f_op == &socket_file_ops) 4156cb153caSBenjamin LaHaise return file->private_data; /* set in sock_map_fd */ 4166cb153caSBenjamin LaHaise 4176cb153caSBenjamin LaHaise *err = -ENOTSOCK; 4186cb153caSBenjamin LaHaise return NULL; 4196cb153caSBenjamin LaHaise } 4206cb153caSBenjamin LaHaise 4211da177e4SLinus Torvalds /** 4221da177e4SLinus Torvalds * sockfd_lookup - Go from a file number to its socket slot 4231da177e4SLinus Torvalds * @fd: file handle 4241da177e4SLinus Torvalds * @err: pointer to an error code return 4251da177e4SLinus Torvalds * 4261da177e4SLinus Torvalds * The file handle passed in is locked and the socket it is bound 4271da177e4SLinus Torvalds * too is returned. If an error occurs the err pointer is overwritten 4281da177e4SLinus Torvalds * with a negative errno code and NULL is returned. The function checks 4291da177e4SLinus Torvalds * for both invalid handles and passing a handle which is not a socket. 4301da177e4SLinus Torvalds * 4311da177e4SLinus Torvalds * On a success the socket object pointer is returned. 4321da177e4SLinus Torvalds */ 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds struct socket *sockfd_lookup(int fd, int *err) 4351da177e4SLinus Torvalds { 4361da177e4SLinus Torvalds struct file *file; 4371da177e4SLinus Torvalds struct socket *sock; 4381da177e4SLinus Torvalds 43989bddce5SStephen Hemminger file = fget(fd); 44089bddce5SStephen Hemminger if (!file) { 4411da177e4SLinus Torvalds *err = -EBADF; 4421da177e4SLinus Torvalds return NULL; 4431da177e4SLinus Torvalds } 44489bddce5SStephen Hemminger 4456cb153caSBenjamin LaHaise sock = sock_from_file(file, err); 4466cb153caSBenjamin LaHaise if (!sock) 4471da177e4SLinus Torvalds fput(file); 4486cb153caSBenjamin LaHaise return sock; 4491da177e4SLinus Torvalds } 4501da177e4SLinus Torvalds 4516cb153caSBenjamin LaHaise static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed) 4526cb153caSBenjamin LaHaise { 4536cb153caSBenjamin LaHaise struct file *file; 4546cb153caSBenjamin LaHaise struct socket *sock; 4556cb153caSBenjamin LaHaise 4563672558cSHua Zhong *err = -EBADF; 4576cb153caSBenjamin LaHaise file = fget_light(fd, fput_needed); 4586cb153caSBenjamin LaHaise if (file) { 4596cb153caSBenjamin LaHaise sock = sock_from_file(file, err); 4606cb153caSBenjamin LaHaise if (sock) 4611da177e4SLinus Torvalds return sock; 4626cb153caSBenjamin LaHaise fput_light(file, *fput_needed); 4636cb153caSBenjamin LaHaise } 4646cb153caSBenjamin LaHaise return NULL; 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds 4671da177e4SLinus Torvalds /** 4681da177e4SLinus Torvalds * sock_alloc - allocate a socket 4691da177e4SLinus Torvalds * 4701da177e4SLinus Torvalds * Allocate a new inode and socket object. The two are bound together 4711da177e4SLinus Torvalds * and initialised. The socket is then returned. If we are out of inodes 4721da177e4SLinus Torvalds * NULL is returned. 4731da177e4SLinus Torvalds */ 4741da177e4SLinus Torvalds 4751da177e4SLinus Torvalds static struct socket *sock_alloc(void) 4761da177e4SLinus Torvalds { 4771da177e4SLinus Torvalds struct inode *inode; 4781da177e4SLinus Torvalds struct socket *sock; 4791da177e4SLinus Torvalds 4801da177e4SLinus Torvalds inode = new_inode(sock_mnt->mnt_sb); 4811da177e4SLinus Torvalds if (!inode) 4821da177e4SLinus Torvalds return NULL; 4831da177e4SLinus Torvalds 4841da177e4SLinus Torvalds sock = SOCKET_I(inode); 4851da177e4SLinus Torvalds 4861da177e4SLinus Torvalds inode->i_mode = S_IFSOCK | S_IRWXUGO; 4871da177e4SLinus Torvalds inode->i_uid = current->fsuid; 4881da177e4SLinus Torvalds inode->i_gid = current->fsgid; 4891da177e4SLinus Torvalds 4901da177e4SLinus Torvalds get_cpu_var(sockets_in_use)++; 4911da177e4SLinus Torvalds put_cpu_var(sockets_in_use); 4921da177e4SLinus Torvalds return sock; 4931da177e4SLinus Torvalds } 4941da177e4SLinus Torvalds 4951da177e4SLinus Torvalds /* 4961da177e4SLinus Torvalds * In theory you can't get an open on this inode, but /proc provides 4971da177e4SLinus Torvalds * a back door. Remember to keep it shut otherwise you'll let the 4981da177e4SLinus Torvalds * creepy crawlies in. 4991da177e4SLinus Torvalds */ 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds static int sock_no_open(struct inode *irrelevant, struct file *dontcare) 5021da177e4SLinus Torvalds { 5031da177e4SLinus Torvalds return -ENXIO; 5041da177e4SLinus Torvalds } 5051da177e4SLinus Torvalds 5064b6f5d20SArjan van de Ven const struct file_operations bad_sock_fops = { 5071da177e4SLinus Torvalds .owner = THIS_MODULE, 5081da177e4SLinus Torvalds .open = sock_no_open, 5091da177e4SLinus Torvalds }; 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds /** 5121da177e4SLinus Torvalds * sock_release - close a socket 5131da177e4SLinus Torvalds * @sock: socket to close 5141da177e4SLinus Torvalds * 5151da177e4SLinus Torvalds * The socket is released from the protocol stack if it has a release 5161da177e4SLinus Torvalds * callback, and the inode is then released if the socket is bound to 5171da177e4SLinus Torvalds * an inode not a file. 5181da177e4SLinus Torvalds */ 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds void sock_release(struct socket *sock) 5211da177e4SLinus Torvalds { 5221da177e4SLinus Torvalds if (sock->ops) { 5231da177e4SLinus Torvalds struct module *owner = sock->ops->owner; 5241da177e4SLinus Torvalds 5251da177e4SLinus Torvalds sock->ops->release(sock); 5261da177e4SLinus Torvalds sock->ops = NULL; 5271da177e4SLinus Torvalds module_put(owner); 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds if (sock->fasync_list) 5311da177e4SLinus Torvalds printk(KERN_ERR "sock_release: fasync list not empty!\n"); 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds get_cpu_var(sockets_in_use)--; 5341da177e4SLinus Torvalds put_cpu_var(sockets_in_use); 5351da177e4SLinus Torvalds if (!sock->file) { 5361da177e4SLinus Torvalds iput(SOCK_INODE(sock)); 5371da177e4SLinus Torvalds return; 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds sock->file = NULL; 5401da177e4SLinus Torvalds } 5411da177e4SLinus Torvalds 5421da177e4SLinus Torvalds static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock, 5431da177e4SLinus Torvalds struct msghdr *msg, size_t size) 5441da177e4SLinus Torvalds { 5451da177e4SLinus Torvalds struct sock_iocb *si = kiocb_to_siocb(iocb); 5461da177e4SLinus Torvalds int err; 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds si->sock = sock; 5491da177e4SLinus Torvalds si->scm = NULL; 5501da177e4SLinus Torvalds si->msg = msg; 5511da177e4SLinus Torvalds si->size = size; 5521da177e4SLinus Torvalds 5531da177e4SLinus Torvalds err = security_socket_sendmsg(sock, msg, size); 5541da177e4SLinus Torvalds if (err) 5551da177e4SLinus Torvalds return err; 5561da177e4SLinus Torvalds 5571da177e4SLinus Torvalds return sock->ops->sendmsg(iocb, sock, msg, size); 5581da177e4SLinus Torvalds } 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) 5611da177e4SLinus Torvalds { 5621da177e4SLinus Torvalds struct kiocb iocb; 5631da177e4SLinus Torvalds struct sock_iocb siocb; 5641da177e4SLinus Torvalds int ret; 5651da177e4SLinus Torvalds 5661da177e4SLinus Torvalds init_sync_kiocb(&iocb, NULL); 5671da177e4SLinus Torvalds iocb.private = &siocb; 5681da177e4SLinus Torvalds ret = __sock_sendmsg(&iocb, sock, msg, size); 5691da177e4SLinus Torvalds if (-EIOCBQUEUED == ret) 5701da177e4SLinus Torvalds ret = wait_on_sync_kiocb(&iocb); 5711da177e4SLinus Torvalds return ret; 5721da177e4SLinus Torvalds } 5731da177e4SLinus Torvalds 5741da177e4SLinus Torvalds int kernel_sendmsg(struct socket *sock, struct msghdr *msg, 5751da177e4SLinus Torvalds struct kvec *vec, size_t num, size_t size) 5761da177e4SLinus Torvalds { 5771da177e4SLinus Torvalds mm_segment_t oldfs = get_fs(); 5781da177e4SLinus Torvalds int result; 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds set_fs(KERNEL_DS); 5811da177e4SLinus Torvalds /* 5821da177e4SLinus Torvalds * the following is safe, since for compiler definitions of kvec and 5831da177e4SLinus Torvalds * iovec are identical, yielding the same in-core layout and alignment 5841da177e4SLinus Torvalds */ 58589bddce5SStephen Hemminger msg->msg_iov = (struct iovec *)vec; 5861da177e4SLinus Torvalds msg->msg_iovlen = num; 5871da177e4SLinus Torvalds result = sock_sendmsg(sock, msg, size); 5881da177e4SLinus Torvalds set_fs(oldfs); 5891da177e4SLinus Torvalds return result; 5901da177e4SLinus Torvalds } 5911da177e4SLinus Torvalds 59292f37fd2SEric Dumazet /* 59392f37fd2SEric Dumazet * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP) 59492f37fd2SEric Dumazet */ 59592f37fd2SEric Dumazet void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk, 59692f37fd2SEric Dumazet struct sk_buff *skb) 59792f37fd2SEric Dumazet { 59892f37fd2SEric Dumazet ktime_t kt = skb->tstamp; 59992f37fd2SEric Dumazet 60092f37fd2SEric Dumazet if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) { 60192f37fd2SEric Dumazet struct timeval tv; 60292f37fd2SEric Dumazet /* Race occurred between timestamp enabling and packet 60392f37fd2SEric Dumazet receiving. Fill in the current time for now. */ 60492f37fd2SEric Dumazet if (kt.tv64 == 0) 60592f37fd2SEric Dumazet kt = ktime_get_real(); 60692f37fd2SEric Dumazet skb->tstamp = kt; 60792f37fd2SEric Dumazet tv = ktime_to_timeval(kt); 60892f37fd2SEric Dumazet put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP, sizeof(tv), &tv); 60992f37fd2SEric Dumazet } else { 61092f37fd2SEric Dumazet struct timespec ts; 61192f37fd2SEric Dumazet /* Race occurred between timestamp enabling and packet 61292f37fd2SEric Dumazet receiving. Fill in the current time for now. */ 61392f37fd2SEric Dumazet if (kt.tv64 == 0) 61492f37fd2SEric Dumazet kt = ktime_get_real(); 61592f37fd2SEric Dumazet skb->tstamp = kt; 61692f37fd2SEric Dumazet ts = ktime_to_timespec(kt); 61792f37fd2SEric Dumazet put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS, sizeof(ts), &ts); 61892f37fd2SEric Dumazet } 61992f37fd2SEric Dumazet } 62092f37fd2SEric Dumazet 6217c81fd8bSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(__sock_recv_timestamp); 6227c81fd8bSArnaldo Carvalho de Melo 6231da177e4SLinus Torvalds static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock, 6241da177e4SLinus Torvalds struct msghdr *msg, size_t size, int flags) 6251da177e4SLinus Torvalds { 6261da177e4SLinus Torvalds int err; 6271da177e4SLinus Torvalds struct sock_iocb *si = kiocb_to_siocb(iocb); 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds si->sock = sock; 6301da177e4SLinus Torvalds si->scm = NULL; 6311da177e4SLinus Torvalds si->msg = msg; 6321da177e4SLinus Torvalds si->size = size; 6331da177e4SLinus Torvalds si->flags = flags; 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds err = security_socket_recvmsg(sock, msg, size, flags); 6361da177e4SLinus Torvalds if (err) 6371da177e4SLinus Torvalds return err; 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds return sock->ops->recvmsg(iocb, sock, msg, size, flags); 6401da177e4SLinus Torvalds } 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds int sock_recvmsg(struct socket *sock, struct msghdr *msg, 6431da177e4SLinus Torvalds size_t size, int flags) 6441da177e4SLinus Torvalds { 6451da177e4SLinus Torvalds struct kiocb iocb; 6461da177e4SLinus Torvalds struct sock_iocb siocb; 6471da177e4SLinus Torvalds int ret; 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds init_sync_kiocb(&iocb, NULL); 6501da177e4SLinus Torvalds iocb.private = &siocb; 6511da177e4SLinus Torvalds ret = __sock_recvmsg(&iocb, sock, msg, size, flags); 6521da177e4SLinus Torvalds if (-EIOCBQUEUED == ret) 6531da177e4SLinus Torvalds ret = wait_on_sync_kiocb(&iocb); 6541da177e4SLinus Torvalds return ret; 6551da177e4SLinus Torvalds } 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds int kernel_recvmsg(struct socket *sock, struct msghdr *msg, 65889bddce5SStephen Hemminger struct kvec *vec, size_t num, size_t size, int flags) 6591da177e4SLinus Torvalds { 6601da177e4SLinus Torvalds mm_segment_t oldfs = get_fs(); 6611da177e4SLinus Torvalds int result; 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds set_fs(KERNEL_DS); 6641da177e4SLinus Torvalds /* 6651da177e4SLinus Torvalds * the following is safe, since for compiler definitions of kvec and 6661da177e4SLinus Torvalds * iovec are identical, yielding the same in-core layout and alignment 6671da177e4SLinus Torvalds */ 66889bddce5SStephen Hemminger msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num; 6691da177e4SLinus Torvalds result = sock_recvmsg(sock, msg, size, flags); 6701da177e4SLinus Torvalds set_fs(oldfs); 6711da177e4SLinus Torvalds return result; 6721da177e4SLinus Torvalds } 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds static void sock_aio_dtor(struct kiocb *iocb) 6751da177e4SLinus Torvalds { 6761da177e4SLinus Torvalds kfree(iocb->private); 6771da177e4SLinus Torvalds } 6781da177e4SLinus Torvalds 67920380731SArnaldo Carvalho de Melo static ssize_t sock_sendpage(struct file *file, struct page *page, 6801da177e4SLinus Torvalds int offset, size_t size, loff_t *ppos, int more) 6811da177e4SLinus Torvalds { 6821da177e4SLinus Torvalds struct socket *sock; 6831da177e4SLinus Torvalds int flags; 6841da177e4SLinus Torvalds 685b69aee04SEric Dumazet sock = file->private_data; 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT; 6881da177e4SLinus Torvalds if (more) 6891da177e4SLinus Torvalds flags |= MSG_MORE; 6901da177e4SLinus Torvalds 6911da177e4SLinus Torvalds return sock->ops->sendpage(sock, page, offset, size, flags); 6921da177e4SLinus Torvalds } 6931da177e4SLinus Torvalds 694ce1d4d3eSChristoph Hellwig static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb, 69589bddce5SStephen Hemminger struct sock_iocb *siocb) 696ce1d4d3eSChristoph Hellwig { 697ce1d4d3eSChristoph Hellwig if (!is_sync_kiocb(iocb)) { 698ce1d4d3eSChristoph Hellwig siocb = kmalloc(sizeof(*siocb), GFP_KERNEL); 699ce1d4d3eSChristoph Hellwig if (!siocb) 700ce1d4d3eSChristoph Hellwig return NULL; 701ce1d4d3eSChristoph Hellwig iocb->ki_dtor = sock_aio_dtor; 702ce1d4d3eSChristoph Hellwig } 703ce1d4d3eSChristoph Hellwig 704ce1d4d3eSChristoph Hellwig siocb->kiocb = iocb; 705ce1d4d3eSChristoph Hellwig iocb->private = siocb; 706ce1d4d3eSChristoph Hellwig return siocb; 707ce1d4d3eSChristoph Hellwig } 708ce1d4d3eSChristoph Hellwig 709ce1d4d3eSChristoph Hellwig static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb, 710027445c3SBadari Pulavarty struct file *file, const struct iovec *iov, 71189bddce5SStephen Hemminger unsigned long nr_segs) 712ce1d4d3eSChristoph Hellwig { 713ce1d4d3eSChristoph Hellwig struct socket *sock = file->private_data; 714ce1d4d3eSChristoph Hellwig size_t size = 0; 715ce1d4d3eSChristoph Hellwig int i; 716ce1d4d3eSChristoph Hellwig 717ce1d4d3eSChristoph Hellwig for (i = 0; i < nr_segs; i++) 718ce1d4d3eSChristoph Hellwig size += iov[i].iov_len; 719ce1d4d3eSChristoph Hellwig 720ce1d4d3eSChristoph Hellwig msg->msg_name = NULL; 721ce1d4d3eSChristoph Hellwig msg->msg_namelen = 0; 722ce1d4d3eSChristoph Hellwig msg->msg_control = NULL; 723ce1d4d3eSChristoph Hellwig msg->msg_controllen = 0; 724ce1d4d3eSChristoph Hellwig msg->msg_iov = (struct iovec *)iov; 725ce1d4d3eSChristoph Hellwig msg->msg_iovlen = nr_segs; 726ce1d4d3eSChristoph Hellwig msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0; 727ce1d4d3eSChristoph Hellwig 728ce1d4d3eSChristoph Hellwig return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags); 729ce1d4d3eSChristoph Hellwig } 730ce1d4d3eSChristoph Hellwig 731027445c3SBadari Pulavarty static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov, 732027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 733ce1d4d3eSChristoph Hellwig { 734ce1d4d3eSChristoph Hellwig struct sock_iocb siocb, *x; 735ce1d4d3eSChristoph Hellwig 736ce1d4d3eSChristoph Hellwig if (pos != 0) 737ce1d4d3eSChristoph Hellwig return -ESPIPE; 738027445c3SBadari Pulavarty 739027445c3SBadari Pulavarty if (iocb->ki_left == 0) /* Match SYS5 behaviour */ 740ce1d4d3eSChristoph Hellwig return 0; 741ce1d4d3eSChristoph Hellwig 742027445c3SBadari Pulavarty 743027445c3SBadari Pulavarty x = alloc_sock_iocb(iocb, &siocb); 744ce1d4d3eSChristoph Hellwig if (!x) 745ce1d4d3eSChristoph Hellwig return -ENOMEM; 746027445c3SBadari Pulavarty return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs); 747ce1d4d3eSChristoph Hellwig } 748ce1d4d3eSChristoph Hellwig 749ce1d4d3eSChristoph Hellwig static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb, 750027445c3SBadari Pulavarty struct file *file, const struct iovec *iov, 75189bddce5SStephen Hemminger unsigned long nr_segs) 752ce1d4d3eSChristoph Hellwig { 753ce1d4d3eSChristoph Hellwig struct socket *sock = file->private_data; 754ce1d4d3eSChristoph Hellwig size_t size = 0; 755ce1d4d3eSChristoph Hellwig int i; 756ce1d4d3eSChristoph Hellwig 757ce1d4d3eSChristoph Hellwig for (i = 0; i < nr_segs; i++) 758ce1d4d3eSChristoph Hellwig size += iov[i].iov_len; 759ce1d4d3eSChristoph Hellwig 760ce1d4d3eSChristoph Hellwig msg->msg_name = NULL; 761ce1d4d3eSChristoph Hellwig msg->msg_namelen = 0; 762ce1d4d3eSChristoph Hellwig msg->msg_control = NULL; 763ce1d4d3eSChristoph Hellwig msg->msg_controllen = 0; 764ce1d4d3eSChristoph Hellwig msg->msg_iov = (struct iovec *)iov; 765ce1d4d3eSChristoph Hellwig msg->msg_iovlen = nr_segs; 766ce1d4d3eSChristoph Hellwig msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0; 767ce1d4d3eSChristoph Hellwig if (sock->type == SOCK_SEQPACKET) 768ce1d4d3eSChristoph Hellwig msg->msg_flags |= MSG_EOR; 769ce1d4d3eSChristoph Hellwig 770ce1d4d3eSChristoph Hellwig return __sock_sendmsg(iocb, sock, msg, size); 771ce1d4d3eSChristoph Hellwig } 772ce1d4d3eSChristoph Hellwig 773027445c3SBadari Pulavarty static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov, 774027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 7751da177e4SLinus Torvalds { 776ce1d4d3eSChristoph Hellwig struct sock_iocb siocb, *x; 7771da177e4SLinus Torvalds 778ce1d4d3eSChristoph Hellwig if (pos != 0) 779ce1d4d3eSChristoph Hellwig return -ESPIPE; 780027445c3SBadari Pulavarty 781027445c3SBadari Pulavarty x = alloc_sock_iocb(iocb, &siocb); 782ce1d4d3eSChristoph Hellwig if (!x) 783ce1d4d3eSChristoph Hellwig return -ENOMEM; 784ce1d4d3eSChristoph Hellwig 785027445c3SBadari Pulavarty return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs); 7861da177e4SLinus Torvalds } 7871da177e4SLinus Torvalds 7881da177e4SLinus Torvalds /* 7891da177e4SLinus Torvalds * Atomic setting of ioctl hooks to avoid race 7901da177e4SLinus Torvalds * with module unload. 7911da177e4SLinus Torvalds */ 7921da177e4SLinus Torvalds 7934a3e2f71SArjan van de Ven static DEFINE_MUTEX(br_ioctl_mutex); 7941da177e4SLinus Torvalds static int (*br_ioctl_hook) (unsigned int cmd, void __user *arg) = NULL; 7951da177e4SLinus Torvalds 7961da177e4SLinus Torvalds void brioctl_set(int (*hook) (unsigned int, void __user *)) 7971da177e4SLinus Torvalds { 7984a3e2f71SArjan van de Ven mutex_lock(&br_ioctl_mutex); 7991da177e4SLinus Torvalds br_ioctl_hook = hook; 8004a3e2f71SArjan van de Ven mutex_unlock(&br_ioctl_mutex); 8011da177e4SLinus Torvalds } 80289bddce5SStephen Hemminger 8031da177e4SLinus Torvalds EXPORT_SYMBOL(brioctl_set); 8041da177e4SLinus Torvalds 8054a3e2f71SArjan van de Ven static DEFINE_MUTEX(vlan_ioctl_mutex); 8061da177e4SLinus Torvalds static int (*vlan_ioctl_hook) (void __user *arg); 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds void vlan_ioctl_set(int (*hook) (void __user *)) 8091da177e4SLinus Torvalds { 8104a3e2f71SArjan van de Ven mutex_lock(&vlan_ioctl_mutex); 8111da177e4SLinus Torvalds vlan_ioctl_hook = hook; 8124a3e2f71SArjan van de Ven mutex_unlock(&vlan_ioctl_mutex); 8131da177e4SLinus Torvalds } 81489bddce5SStephen Hemminger 8151da177e4SLinus Torvalds EXPORT_SYMBOL(vlan_ioctl_set); 8161da177e4SLinus Torvalds 8174a3e2f71SArjan van de Ven static DEFINE_MUTEX(dlci_ioctl_mutex); 8181da177e4SLinus Torvalds static int (*dlci_ioctl_hook) (unsigned int, void __user *); 8191da177e4SLinus Torvalds 8201da177e4SLinus Torvalds void dlci_ioctl_set(int (*hook) (unsigned int, void __user *)) 8211da177e4SLinus Torvalds { 8224a3e2f71SArjan van de Ven mutex_lock(&dlci_ioctl_mutex); 8231da177e4SLinus Torvalds dlci_ioctl_hook = hook; 8244a3e2f71SArjan van de Ven mutex_unlock(&dlci_ioctl_mutex); 8251da177e4SLinus Torvalds } 82689bddce5SStephen Hemminger 8271da177e4SLinus Torvalds EXPORT_SYMBOL(dlci_ioctl_set); 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds /* 8301da177e4SLinus Torvalds * With an ioctl, arg may well be a user mode pointer, but we don't know 8311da177e4SLinus Torvalds * what to do with it - that's up to the protocol still. 8321da177e4SLinus Torvalds */ 8331da177e4SLinus Torvalds 8341da177e4SLinus Torvalds static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg) 8351da177e4SLinus Torvalds { 8361da177e4SLinus Torvalds struct socket *sock; 8371da177e4SLinus Torvalds void __user *argp = (void __user *)arg; 8381da177e4SLinus Torvalds int pid, err; 8391da177e4SLinus Torvalds 840b69aee04SEric Dumazet sock = file->private_data; 8411da177e4SLinus Torvalds if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) { 8421da177e4SLinus Torvalds err = dev_ioctl(cmd, argp); 8431da177e4SLinus Torvalds } else 844d86b5e0eSAdrian Bunk #ifdef CONFIG_WIRELESS_EXT 8451da177e4SLinus Torvalds if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) { 8461da177e4SLinus Torvalds err = dev_ioctl(cmd, argp); 8471da177e4SLinus Torvalds } else 848d86b5e0eSAdrian Bunk #endif /* CONFIG_WIRELESS_EXT */ 8491da177e4SLinus Torvalds switch (cmd) { 8501da177e4SLinus Torvalds case FIOSETOWN: 8511da177e4SLinus Torvalds case SIOCSPGRP: 8521da177e4SLinus Torvalds err = -EFAULT; 8531da177e4SLinus Torvalds if (get_user(pid, (int __user *)argp)) 8541da177e4SLinus Torvalds break; 8551da177e4SLinus Torvalds err = f_setown(sock->file, pid, 1); 8561da177e4SLinus Torvalds break; 8571da177e4SLinus Torvalds case FIOGETOWN: 8581da177e4SLinus Torvalds case SIOCGPGRP: 859609d7fa9SEric W. Biederman err = put_user(f_getown(sock->file), 86089bddce5SStephen Hemminger (int __user *)argp); 8611da177e4SLinus Torvalds break; 8621da177e4SLinus Torvalds case SIOCGIFBR: 8631da177e4SLinus Torvalds case SIOCSIFBR: 8641da177e4SLinus Torvalds case SIOCBRADDBR: 8651da177e4SLinus Torvalds case SIOCBRDELBR: 8661da177e4SLinus Torvalds err = -ENOPKG; 8671da177e4SLinus Torvalds if (!br_ioctl_hook) 8681da177e4SLinus Torvalds request_module("bridge"); 8691da177e4SLinus Torvalds 8704a3e2f71SArjan van de Ven mutex_lock(&br_ioctl_mutex); 8711da177e4SLinus Torvalds if (br_ioctl_hook) 8721da177e4SLinus Torvalds err = br_ioctl_hook(cmd, argp); 8734a3e2f71SArjan van de Ven mutex_unlock(&br_ioctl_mutex); 8741da177e4SLinus Torvalds break; 8751da177e4SLinus Torvalds case SIOCGIFVLAN: 8761da177e4SLinus Torvalds case SIOCSIFVLAN: 8771da177e4SLinus Torvalds err = -ENOPKG; 8781da177e4SLinus Torvalds if (!vlan_ioctl_hook) 8791da177e4SLinus Torvalds request_module("8021q"); 8801da177e4SLinus Torvalds 8814a3e2f71SArjan van de Ven mutex_lock(&vlan_ioctl_mutex); 8821da177e4SLinus Torvalds if (vlan_ioctl_hook) 8831da177e4SLinus Torvalds err = vlan_ioctl_hook(argp); 8844a3e2f71SArjan van de Ven mutex_unlock(&vlan_ioctl_mutex); 8851da177e4SLinus Torvalds break; 8861da177e4SLinus Torvalds case SIOCADDDLCI: 8871da177e4SLinus Torvalds case SIOCDELDLCI: 8881da177e4SLinus Torvalds err = -ENOPKG; 8891da177e4SLinus Torvalds if (!dlci_ioctl_hook) 8901da177e4SLinus Torvalds request_module("dlci"); 8911da177e4SLinus Torvalds 8921da177e4SLinus Torvalds if (dlci_ioctl_hook) { 8934a3e2f71SArjan van de Ven mutex_lock(&dlci_ioctl_mutex); 8941da177e4SLinus Torvalds err = dlci_ioctl_hook(cmd, argp); 8954a3e2f71SArjan van de Ven mutex_unlock(&dlci_ioctl_mutex); 8961da177e4SLinus Torvalds } 8971da177e4SLinus Torvalds break; 8981da177e4SLinus Torvalds default: 8991da177e4SLinus Torvalds err = sock->ops->ioctl(sock, cmd, arg); 900b5e5fa5eSChristoph Hellwig 901b5e5fa5eSChristoph Hellwig /* 902b5e5fa5eSChristoph Hellwig * If this ioctl is unknown try to hand it down 903b5e5fa5eSChristoph Hellwig * to the NIC driver. 904b5e5fa5eSChristoph Hellwig */ 905b5e5fa5eSChristoph Hellwig if (err == -ENOIOCTLCMD) 906b5e5fa5eSChristoph Hellwig err = dev_ioctl(cmd, argp); 9071da177e4SLinus Torvalds break; 9081da177e4SLinus Torvalds } 9091da177e4SLinus Torvalds return err; 9101da177e4SLinus Torvalds } 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds int sock_create_lite(int family, int type, int protocol, struct socket **res) 9131da177e4SLinus Torvalds { 9141da177e4SLinus Torvalds int err; 9151da177e4SLinus Torvalds struct socket *sock = NULL; 9161da177e4SLinus Torvalds 9171da177e4SLinus Torvalds err = security_socket_create(family, type, protocol, 1); 9181da177e4SLinus Torvalds if (err) 9191da177e4SLinus Torvalds goto out; 9201da177e4SLinus Torvalds 9211da177e4SLinus Torvalds sock = sock_alloc(); 9221da177e4SLinus Torvalds if (!sock) { 9231da177e4SLinus Torvalds err = -ENOMEM; 9241da177e4SLinus Torvalds goto out; 9251da177e4SLinus Torvalds } 9261da177e4SLinus Torvalds 9271da177e4SLinus Torvalds sock->type = type; 9287420ed23SVenkat Yekkirala err = security_socket_post_create(sock, family, type, protocol, 1); 9297420ed23SVenkat Yekkirala if (err) 9307420ed23SVenkat Yekkirala goto out_release; 9317420ed23SVenkat Yekkirala 9321da177e4SLinus Torvalds out: 9331da177e4SLinus Torvalds *res = sock; 9341da177e4SLinus Torvalds return err; 9357420ed23SVenkat Yekkirala out_release: 9367420ed23SVenkat Yekkirala sock_release(sock); 9377420ed23SVenkat Yekkirala sock = NULL; 9387420ed23SVenkat Yekkirala goto out; 9391da177e4SLinus Torvalds } 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds /* No kernel lock held - perfect */ 9421da177e4SLinus Torvalds static unsigned int sock_poll(struct file *file, poll_table *wait) 9431da177e4SLinus Torvalds { 9441da177e4SLinus Torvalds struct socket *sock; 9451da177e4SLinus Torvalds 9461da177e4SLinus Torvalds /* 9471da177e4SLinus Torvalds * We can't return errors to poll, so it's either yes or no. 9481da177e4SLinus Torvalds */ 949b69aee04SEric Dumazet sock = file->private_data; 9501da177e4SLinus Torvalds return sock->ops->poll(file, sock, wait); 9511da177e4SLinus Torvalds } 9521da177e4SLinus Torvalds 9531da177e4SLinus Torvalds static int sock_mmap(struct file *file, struct vm_area_struct *vma) 9541da177e4SLinus Torvalds { 955b69aee04SEric Dumazet struct socket *sock = file->private_data; 9561da177e4SLinus Torvalds 9571da177e4SLinus Torvalds return sock->ops->mmap(file, sock, vma); 9581da177e4SLinus Torvalds } 9591da177e4SLinus Torvalds 96020380731SArnaldo Carvalho de Melo static int sock_close(struct inode *inode, struct file *filp) 9611da177e4SLinus Torvalds { 9621da177e4SLinus Torvalds /* 9631da177e4SLinus Torvalds * It was possible the inode is NULL we were 9641da177e4SLinus Torvalds * closing an unfinished socket. 9651da177e4SLinus Torvalds */ 9661da177e4SLinus Torvalds 96789bddce5SStephen Hemminger if (!inode) { 9681da177e4SLinus Torvalds printk(KERN_DEBUG "sock_close: NULL inode\n"); 9691da177e4SLinus Torvalds return 0; 9701da177e4SLinus Torvalds } 9711da177e4SLinus Torvalds sock_fasync(-1, filp, 0); 9721da177e4SLinus Torvalds sock_release(SOCKET_I(inode)); 9731da177e4SLinus Torvalds return 0; 9741da177e4SLinus Torvalds } 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds /* 9771da177e4SLinus Torvalds * Update the socket async list 9781da177e4SLinus Torvalds * 9791da177e4SLinus Torvalds * Fasync_list locking strategy. 9801da177e4SLinus Torvalds * 9811da177e4SLinus Torvalds * 1. fasync_list is modified only under process context socket lock 9821da177e4SLinus Torvalds * i.e. under semaphore. 9831da177e4SLinus Torvalds * 2. fasync_list is used under read_lock(&sk->sk_callback_lock) 9841da177e4SLinus Torvalds * or under socket lock. 9851da177e4SLinus Torvalds * 3. fasync_list can be used from softirq context, so that 9861da177e4SLinus Torvalds * modification under socket lock have to be enhanced with 9871da177e4SLinus Torvalds * write_lock_bh(&sk->sk_callback_lock). 9881da177e4SLinus Torvalds * --ANK (990710) 9891da177e4SLinus Torvalds */ 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds static int sock_fasync(int fd, struct file *filp, int on) 9921da177e4SLinus Torvalds { 9931da177e4SLinus Torvalds struct fasync_struct *fa, *fna = NULL, **prev; 9941da177e4SLinus Torvalds struct socket *sock; 9951da177e4SLinus Torvalds struct sock *sk; 9961da177e4SLinus Torvalds 99789bddce5SStephen Hemminger if (on) { 9988b3a7005SKris Katterjohn fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL); 9991da177e4SLinus Torvalds if (fna == NULL) 10001da177e4SLinus Torvalds return -ENOMEM; 10011da177e4SLinus Torvalds } 10021da177e4SLinus Torvalds 1003b69aee04SEric Dumazet sock = filp->private_data; 10041da177e4SLinus Torvalds 100589bddce5SStephen Hemminger sk = sock->sk; 100689bddce5SStephen Hemminger if (sk == NULL) { 10071da177e4SLinus Torvalds kfree(fna); 10081da177e4SLinus Torvalds return -EINVAL; 10091da177e4SLinus Torvalds } 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds lock_sock(sk); 10121da177e4SLinus Torvalds 10131da177e4SLinus Torvalds prev = &(sock->fasync_list); 10141da177e4SLinus Torvalds 10151da177e4SLinus Torvalds for (fa = *prev; fa != NULL; prev = &fa->fa_next, fa = *prev) 10161da177e4SLinus Torvalds if (fa->fa_file == filp) 10171da177e4SLinus Torvalds break; 10181da177e4SLinus Torvalds 101989bddce5SStephen Hemminger if (on) { 102089bddce5SStephen Hemminger if (fa != NULL) { 10211da177e4SLinus Torvalds write_lock_bh(&sk->sk_callback_lock); 10221da177e4SLinus Torvalds fa->fa_fd = fd; 10231da177e4SLinus Torvalds write_unlock_bh(&sk->sk_callback_lock); 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds kfree(fna); 10261da177e4SLinus Torvalds goto out; 10271da177e4SLinus Torvalds } 10281da177e4SLinus Torvalds fna->fa_file = filp; 10291da177e4SLinus Torvalds fna->fa_fd = fd; 10301da177e4SLinus Torvalds fna->magic = FASYNC_MAGIC; 10311da177e4SLinus Torvalds fna->fa_next = sock->fasync_list; 10321da177e4SLinus Torvalds write_lock_bh(&sk->sk_callback_lock); 10331da177e4SLinus Torvalds sock->fasync_list = fna; 10341da177e4SLinus Torvalds write_unlock_bh(&sk->sk_callback_lock); 103589bddce5SStephen Hemminger } else { 103689bddce5SStephen Hemminger if (fa != NULL) { 10371da177e4SLinus Torvalds write_lock_bh(&sk->sk_callback_lock); 10381da177e4SLinus Torvalds *prev = fa->fa_next; 10391da177e4SLinus Torvalds write_unlock_bh(&sk->sk_callback_lock); 10401da177e4SLinus Torvalds kfree(fa); 10411da177e4SLinus Torvalds } 10421da177e4SLinus Torvalds } 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds out: 10451da177e4SLinus Torvalds release_sock(sock->sk); 10461da177e4SLinus Torvalds return 0; 10471da177e4SLinus Torvalds } 10481da177e4SLinus Torvalds 10491da177e4SLinus Torvalds /* This function may be called only under socket lock or callback_lock */ 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds int sock_wake_async(struct socket *sock, int how, int band) 10521da177e4SLinus Torvalds { 10531da177e4SLinus Torvalds if (!sock || !sock->fasync_list) 10541da177e4SLinus Torvalds return -1; 105589bddce5SStephen Hemminger switch (how) { 10561da177e4SLinus Torvalds case 1: 10571da177e4SLinus Torvalds 10581da177e4SLinus Torvalds if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags)) 10591da177e4SLinus Torvalds break; 10601da177e4SLinus Torvalds goto call_kill; 10611da177e4SLinus Torvalds case 2: 10621da177e4SLinus Torvalds if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags)) 10631da177e4SLinus Torvalds break; 10641da177e4SLinus Torvalds /* fall through */ 10651da177e4SLinus Torvalds case 0: 10661da177e4SLinus Torvalds call_kill: 10671da177e4SLinus Torvalds __kill_fasync(sock->fasync_list, SIGIO, band); 10681da177e4SLinus Torvalds break; 10691da177e4SLinus Torvalds case 3: 10701da177e4SLinus Torvalds __kill_fasync(sock->fasync_list, SIGURG, band); 10711da177e4SLinus Torvalds } 10721da177e4SLinus Torvalds return 0; 10731da177e4SLinus Torvalds } 10741da177e4SLinus Torvalds 10751b8d7ae4SEric W. Biederman static int __sock_create(struct net *net, int family, int type, int protocol, 107689bddce5SStephen Hemminger struct socket **res, int kern) 10771da177e4SLinus Torvalds { 10781da177e4SLinus Torvalds int err; 10791da177e4SLinus Torvalds struct socket *sock; 108055737fdaSStephen Hemminger const struct net_proto_family *pf; 10811da177e4SLinus Torvalds 10821da177e4SLinus Torvalds /* 10831da177e4SLinus Torvalds * Check protocol is in range 10841da177e4SLinus Torvalds */ 10851da177e4SLinus Torvalds if (family < 0 || family >= NPROTO) 10861da177e4SLinus Torvalds return -EAFNOSUPPORT; 10871da177e4SLinus Torvalds if (type < 0 || type >= SOCK_MAX) 10881da177e4SLinus Torvalds return -EINVAL; 10891da177e4SLinus Torvalds 10901da177e4SLinus Torvalds /* Compatibility. 10911da177e4SLinus Torvalds 10921da177e4SLinus Torvalds This uglymoron is moved from INET layer to here to avoid 10931da177e4SLinus Torvalds deadlock in module load. 10941da177e4SLinus Torvalds */ 10951da177e4SLinus Torvalds if (family == PF_INET && type == SOCK_PACKET) { 10961da177e4SLinus Torvalds static int warned; 10971da177e4SLinus Torvalds if (!warned) { 10981da177e4SLinus Torvalds warned = 1; 109989bddce5SStephen Hemminger printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n", 110089bddce5SStephen Hemminger current->comm); 11011da177e4SLinus Torvalds } 11021da177e4SLinus Torvalds family = PF_PACKET; 11031da177e4SLinus Torvalds } 11041da177e4SLinus Torvalds 11051da177e4SLinus Torvalds err = security_socket_create(family, type, protocol, kern); 11061da177e4SLinus Torvalds if (err) 11071da177e4SLinus Torvalds return err; 11081da177e4SLinus Torvalds 110955737fdaSStephen Hemminger /* 111055737fdaSStephen Hemminger * Allocate the socket and allow the family to set things up. if 111155737fdaSStephen Hemminger * the protocol is 0, the family is instructed to select an appropriate 111255737fdaSStephen Hemminger * default. 111355737fdaSStephen Hemminger */ 111455737fdaSStephen Hemminger sock = sock_alloc(); 111555737fdaSStephen Hemminger if (!sock) { 111655737fdaSStephen Hemminger if (net_ratelimit()) 111755737fdaSStephen Hemminger printk(KERN_WARNING "socket: no more sockets\n"); 111855737fdaSStephen Hemminger return -ENFILE; /* Not exactly a match, but its the 111955737fdaSStephen Hemminger closest posix thing */ 112055737fdaSStephen Hemminger } 112155737fdaSStephen Hemminger 112255737fdaSStephen Hemminger sock->type = type; 112355737fdaSStephen Hemminger 11241da177e4SLinus Torvalds #if defined(CONFIG_KMOD) 11251da177e4SLinus Torvalds /* Attempt to load a protocol module if the find failed. 11261da177e4SLinus Torvalds * 11271da177e4SLinus Torvalds * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user 11281da177e4SLinus Torvalds * requested real, full-featured networking support upon configuration. 11291da177e4SLinus Torvalds * Otherwise module support will break! 11301da177e4SLinus Torvalds */ 113155737fdaSStephen Hemminger if (net_families[family] == NULL) 11321da177e4SLinus Torvalds request_module("net-pf-%d", family); 11331da177e4SLinus Torvalds #endif 11341da177e4SLinus Torvalds 113555737fdaSStephen Hemminger rcu_read_lock(); 113655737fdaSStephen Hemminger pf = rcu_dereference(net_families[family]); 11371da177e4SLinus Torvalds err = -EAFNOSUPPORT; 113855737fdaSStephen Hemminger if (!pf) 113955737fdaSStephen Hemminger goto out_release; 11401da177e4SLinus Torvalds 11411da177e4SLinus Torvalds /* 11421da177e4SLinus Torvalds * We will call the ->create function, that possibly is in a loadable 11431da177e4SLinus Torvalds * module, so we have to bump that loadable module refcnt first. 11441da177e4SLinus Torvalds */ 114555737fdaSStephen Hemminger if (!try_module_get(pf->owner)) 11461da177e4SLinus Torvalds goto out_release; 11471da177e4SLinus Torvalds 114855737fdaSStephen Hemminger /* Now protected by module ref count */ 114955737fdaSStephen Hemminger rcu_read_unlock(); 115055737fdaSStephen Hemminger 11511b8d7ae4SEric W. Biederman err = pf->create(net, sock, protocol); 115255737fdaSStephen Hemminger if (err < 0) 11531da177e4SLinus Torvalds goto out_module_put; 1154a79af59eSFrank Filz 11551da177e4SLinus Torvalds /* 11561da177e4SLinus Torvalds * Now to bump the refcnt of the [loadable] module that owns this 11571da177e4SLinus Torvalds * socket at sock_release time we decrement its refcnt. 11581da177e4SLinus Torvalds */ 115955737fdaSStephen Hemminger if (!try_module_get(sock->ops->owner)) 116055737fdaSStephen Hemminger goto out_module_busy; 116155737fdaSStephen Hemminger 11621da177e4SLinus Torvalds /* 11631da177e4SLinus Torvalds * Now that we're done with the ->create function, the [loadable] 11641da177e4SLinus Torvalds * module can have its refcnt decremented 11651da177e4SLinus Torvalds */ 116655737fdaSStephen Hemminger module_put(pf->owner); 11677420ed23SVenkat Yekkirala err = security_socket_post_create(sock, family, type, protocol, kern); 11687420ed23SVenkat Yekkirala if (err) 11693b185525SHerbert Xu goto out_sock_release; 117055737fdaSStephen Hemminger *res = sock; 11711da177e4SLinus Torvalds 117255737fdaSStephen Hemminger return 0; 117355737fdaSStephen Hemminger 117455737fdaSStephen Hemminger out_module_busy: 117555737fdaSStephen Hemminger err = -EAFNOSUPPORT; 11761da177e4SLinus Torvalds out_module_put: 117755737fdaSStephen Hemminger sock->ops = NULL; 117855737fdaSStephen Hemminger module_put(pf->owner); 117955737fdaSStephen Hemminger out_sock_release: 11801da177e4SLinus Torvalds sock_release(sock); 118155737fdaSStephen Hemminger return err; 118255737fdaSStephen Hemminger 118355737fdaSStephen Hemminger out_release: 118455737fdaSStephen Hemminger rcu_read_unlock(); 118555737fdaSStephen Hemminger goto out_sock_release; 11861da177e4SLinus Torvalds } 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds int sock_create(int family, int type, int protocol, struct socket **res) 11891da177e4SLinus Torvalds { 11901b8d7ae4SEric W. Biederman return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0); 11911da177e4SLinus Torvalds } 11921da177e4SLinus Torvalds 11931da177e4SLinus Torvalds int sock_create_kern(int family, int type, int protocol, struct socket **res) 11941da177e4SLinus Torvalds { 11951b8d7ae4SEric W. Biederman return __sock_create(&init_net, family, type, protocol, res, 1); 11961da177e4SLinus Torvalds } 11971da177e4SLinus Torvalds 11981da177e4SLinus Torvalds asmlinkage long sys_socket(int family, int type, int protocol) 11991da177e4SLinus Torvalds { 12001da177e4SLinus Torvalds int retval; 12011da177e4SLinus Torvalds struct socket *sock; 12021da177e4SLinus Torvalds 12031da177e4SLinus Torvalds retval = sock_create(family, type, protocol, &sock); 12041da177e4SLinus Torvalds if (retval < 0) 12051da177e4SLinus Torvalds goto out; 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds retval = sock_map_fd(sock); 12081da177e4SLinus Torvalds if (retval < 0) 12091da177e4SLinus Torvalds goto out_release; 12101da177e4SLinus Torvalds 12111da177e4SLinus Torvalds out: 12121da177e4SLinus Torvalds /* It may be already another descriptor 8) Not kernel problem. */ 12131da177e4SLinus Torvalds return retval; 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds out_release: 12161da177e4SLinus Torvalds sock_release(sock); 12171da177e4SLinus Torvalds return retval; 12181da177e4SLinus Torvalds } 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds /* 12211da177e4SLinus Torvalds * Create a pair of connected sockets. 12221da177e4SLinus Torvalds */ 12231da177e4SLinus Torvalds 122489bddce5SStephen Hemminger asmlinkage long sys_socketpair(int family, int type, int protocol, 122589bddce5SStephen Hemminger int __user *usockvec) 12261da177e4SLinus Torvalds { 12271da177e4SLinus Torvalds struct socket *sock1, *sock2; 12281da177e4SLinus Torvalds int fd1, fd2, err; 1229db349509SAl Viro struct file *newfile1, *newfile2; 12301da177e4SLinus Torvalds 12311da177e4SLinus Torvalds /* 12321da177e4SLinus Torvalds * Obtain the first socket and check if the underlying protocol 12331da177e4SLinus Torvalds * supports the socketpair call. 12341da177e4SLinus Torvalds */ 12351da177e4SLinus Torvalds 12361da177e4SLinus Torvalds err = sock_create(family, type, protocol, &sock1); 12371da177e4SLinus Torvalds if (err < 0) 12381da177e4SLinus Torvalds goto out; 12391da177e4SLinus Torvalds 12401da177e4SLinus Torvalds err = sock_create(family, type, protocol, &sock2); 12411da177e4SLinus Torvalds if (err < 0) 12421da177e4SLinus Torvalds goto out_release_1; 12431da177e4SLinus Torvalds 12441da177e4SLinus Torvalds err = sock1->ops->socketpair(sock1, sock2); 12451da177e4SLinus Torvalds if (err < 0) 12461da177e4SLinus Torvalds goto out_release_both; 12471da177e4SLinus Torvalds 1248db349509SAl Viro fd1 = sock_alloc_fd(&newfile1); 1249db349509SAl Viro if (unlikely(fd1 < 0)) 12501da177e4SLinus Torvalds goto out_release_both; 12511da177e4SLinus Torvalds 1252db349509SAl Viro fd2 = sock_alloc_fd(&newfile2); 1253db349509SAl Viro if (unlikely(fd2 < 0)) { 1254db349509SAl Viro put_filp(newfile1); 1255db349509SAl Viro put_unused_fd(fd1); 1256db349509SAl Viro goto out_release_both; 1257db349509SAl Viro } 12581da177e4SLinus Torvalds 1259db349509SAl Viro err = sock_attach_fd(sock1, newfile1); 1260db349509SAl Viro if (unlikely(err < 0)) { 1261db349509SAl Viro goto out_fd2; 1262db349509SAl Viro } 1263db349509SAl Viro 1264db349509SAl Viro err = sock_attach_fd(sock2, newfile2); 1265db349509SAl Viro if (unlikely(err < 0)) { 1266db349509SAl Viro fput(newfile1); 1267db349509SAl Viro goto out_fd1; 1268db349509SAl Viro } 1269db349509SAl Viro 1270db349509SAl Viro err = audit_fd_pair(fd1, fd2); 1271db349509SAl Viro if (err < 0) { 1272db349509SAl Viro fput(newfile1); 1273db349509SAl Viro fput(newfile2); 1274db349509SAl Viro goto out_fd; 1275db349509SAl Viro } 1276db349509SAl Viro 1277db349509SAl Viro fd_install(fd1, newfile1); 1278db349509SAl Viro fd_install(fd2, newfile2); 12791da177e4SLinus Torvalds /* fd1 and fd2 may be already another descriptors. 12801da177e4SLinus Torvalds * Not kernel problem. 12811da177e4SLinus Torvalds */ 12821da177e4SLinus Torvalds 12831da177e4SLinus Torvalds err = put_user(fd1, &usockvec[0]); 12841da177e4SLinus Torvalds if (!err) 12851da177e4SLinus Torvalds err = put_user(fd2, &usockvec[1]); 12861da177e4SLinus Torvalds if (!err) 12871da177e4SLinus Torvalds return 0; 12881da177e4SLinus Torvalds 12891da177e4SLinus Torvalds sys_close(fd2); 12901da177e4SLinus Torvalds sys_close(fd1); 12911da177e4SLinus Torvalds return err; 12921da177e4SLinus Torvalds 12931da177e4SLinus Torvalds out_release_both: 12941da177e4SLinus Torvalds sock_release(sock2); 12951da177e4SLinus Torvalds out_release_1: 12961da177e4SLinus Torvalds sock_release(sock1); 12971da177e4SLinus Torvalds out: 12981da177e4SLinus Torvalds return err; 1299db349509SAl Viro 1300db349509SAl Viro out_fd2: 1301db349509SAl Viro put_filp(newfile1); 1302db349509SAl Viro sock_release(sock1); 1303db349509SAl Viro out_fd1: 1304db349509SAl Viro put_filp(newfile2); 1305db349509SAl Viro sock_release(sock2); 1306db349509SAl Viro out_fd: 1307db349509SAl Viro put_unused_fd(fd1); 1308db349509SAl Viro put_unused_fd(fd2); 1309db349509SAl Viro goto out; 13101da177e4SLinus Torvalds } 13111da177e4SLinus Torvalds 13121da177e4SLinus Torvalds /* 13131da177e4SLinus Torvalds * Bind a name to a socket. Nothing much to do here since it's 13141da177e4SLinus Torvalds * the protocol's responsibility to handle the local address. 13151da177e4SLinus Torvalds * 13161da177e4SLinus Torvalds * We move the socket address to kernel space before we call 13171da177e4SLinus Torvalds * the protocol layer (having also checked the address is ok). 13181da177e4SLinus Torvalds */ 13191da177e4SLinus Torvalds 13201da177e4SLinus Torvalds asmlinkage long sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen) 13211da177e4SLinus Torvalds { 13221da177e4SLinus Torvalds struct socket *sock; 13231da177e4SLinus Torvalds char address[MAX_SOCK_ADDR]; 13246cb153caSBenjamin LaHaise int err, fput_needed; 13251da177e4SLinus Torvalds 132689bddce5SStephen Hemminger sock = sockfd_lookup_light(fd, &err, &fput_needed); 132789bddce5SStephen Hemminger if (sock) { 132889bddce5SStephen Hemminger err = move_addr_to_kernel(umyaddr, addrlen, address); 132989bddce5SStephen Hemminger if (err >= 0) { 133089bddce5SStephen Hemminger err = security_socket_bind(sock, 133189bddce5SStephen Hemminger (struct sockaddr *)address, 133289bddce5SStephen Hemminger addrlen); 13336cb153caSBenjamin LaHaise if (!err) 13346cb153caSBenjamin LaHaise err = sock->ops->bind(sock, 133589bddce5SStephen Hemminger (struct sockaddr *) 133689bddce5SStephen Hemminger address, addrlen); 13371da177e4SLinus Torvalds } 13386cb153caSBenjamin LaHaise fput_light(sock->file, fput_needed); 13391da177e4SLinus Torvalds } 13401da177e4SLinus Torvalds return err; 13411da177e4SLinus Torvalds } 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds /* 13441da177e4SLinus Torvalds * Perform a listen. Basically, we allow the protocol to do anything 13451da177e4SLinus Torvalds * necessary for a listen, and if that works, we mark the socket as 13461da177e4SLinus Torvalds * ready for listening. 13471da177e4SLinus Torvalds */ 13481da177e4SLinus Torvalds 13497a42c217SBrian Haley int sysctl_somaxconn __read_mostly = SOMAXCONN; 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds asmlinkage long sys_listen(int fd, int backlog) 13521da177e4SLinus Torvalds { 13531da177e4SLinus Torvalds struct socket *sock; 13546cb153caSBenjamin LaHaise int err, fput_needed; 13551da177e4SLinus Torvalds 135689bddce5SStephen Hemminger sock = sockfd_lookup_light(fd, &err, &fput_needed); 135789bddce5SStephen Hemminger if (sock) { 13581da177e4SLinus Torvalds if ((unsigned)backlog > sysctl_somaxconn) 13591da177e4SLinus Torvalds backlog = sysctl_somaxconn; 13601da177e4SLinus Torvalds 13611da177e4SLinus Torvalds err = security_socket_listen(sock, backlog); 13626cb153caSBenjamin LaHaise if (!err) 13631da177e4SLinus Torvalds err = sock->ops->listen(sock, backlog); 13646cb153caSBenjamin LaHaise 13656cb153caSBenjamin LaHaise fput_light(sock->file, fput_needed); 13661da177e4SLinus Torvalds } 13671da177e4SLinus Torvalds return err; 13681da177e4SLinus Torvalds } 13691da177e4SLinus Torvalds 13701da177e4SLinus Torvalds /* 13711da177e4SLinus Torvalds * For accept, we attempt to create a new socket, set up the link 13721da177e4SLinus Torvalds * with the client, wake up the client, then return the new 13731da177e4SLinus Torvalds * connected fd. We collect the address of the connector in kernel 13741da177e4SLinus Torvalds * space and move it to user at the very end. This is unclean because 13751da177e4SLinus Torvalds * we open the socket then return an error. 13761da177e4SLinus Torvalds * 13771da177e4SLinus Torvalds * 1003.1g adds the ability to recvmsg() to query connection pending 13781da177e4SLinus Torvalds * status to recvmsg. We need to add that support in a way thats 13791da177e4SLinus Torvalds * clean when we restucture accept also. 13801da177e4SLinus Torvalds */ 13811da177e4SLinus Torvalds 138289bddce5SStephen Hemminger asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr, 138389bddce5SStephen Hemminger int __user *upeer_addrlen) 13841da177e4SLinus Torvalds { 13851da177e4SLinus Torvalds struct socket *sock, *newsock; 138639d8c1b6SDavid S. Miller struct file *newfile; 13876cb153caSBenjamin LaHaise int err, len, newfd, fput_needed; 13881da177e4SLinus Torvalds char address[MAX_SOCK_ADDR]; 13891da177e4SLinus Torvalds 13906cb153caSBenjamin LaHaise sock = sockfd_lookup_light(fd, &err, &fput_needed); 13911da177e4SLinus Torvalds if (!sock) 13921da177e4SLinus Torvalds goto out; 13931da177e4SLinus Torvalds 13941da177e4SLinus Torvalds err = -ENFILE; 13951da177e4SLinus Torvalds if (!(newsock = sock_alloc())) 13961da177e4SLinus Torvalds goto out_put; 13971da177e4SLinus Torvalds 13981da177e4SLinus Torvalds newsock->type = sock->type; 13991da177e4SLinus Torvalds newsock->ops = sock->ops; 14001da177e4SLinus Torvalds 14011da177e4SLinus Torvalds /* 14021da177e4SLinus Torvalds * We don't need try_module_get here, as the listening socket (sock) 14031da177e4SLinus Torvalds * has the protocol module (sock->ops->owner) held. 14041da177e4SLinus Torvalds */ 14051da177e4SLinus Torvalds __module_get(newsock->ops->owner); 14061da177e4SLinus Torvalds 140739d8c1b6SDavid S. Miller newfd = sock_alloc_fd(&newfile); 140839d8c1b6SDavid S. Miller if (unlikely(newfd < 0)) { 140939d8c1b6SDavid S. Miller err = newfd; 14109a1875e6SDavid S. Miller sock_release(newsock); 14119a1875e6SDavid S. Miller goto out_put; 141239d8c1b6SDavid S. Miller } 141339d8c1b6SDavid S. Miller 141439d8c1b6SDavid S. Miller err = sock_attach_fd(newsock, newfile); 141539d8c1b6SDavid S. Miller if (err < 0) 141679f4f642SAlexey Dobriyan goto out_fd_simple; 141739d8c1b6SDavid S. Miller 1418a79af59eSFrank Filz err = security_socket_accept(sock, newsock); 1419a79af59eSFrank Filz if (err) 142039d8c1b6SDavid S. Miller goto out_fd; 1421a79af59eSFrank Filz 14221da177e4SLinus Torvalds err = sock->ops->accept(sock, newsock, sock->file->f_flags); 14231da177e4SLinus Torvalds if (err < 0) 142439d8c1b6SDavid S. Miller goto out_fd; 14251da177e4SLinus Torvalds 14261da177e4SLinus Torvalds if (upeer_sockaddr) { 142789bddce5SStephen Hemminger if (newsock->ops->getname(newsock, (struct sockaddr *)address, 142889bddce5SStephen Hemminger &len, 2) < 0) { 14291da177e4SLinus Torvalds err = -ECONNABORTED; 143039d8c1b6SDavid S. Miller goto out_fd; 14311da177e4SLinus Torvalds } 143289bddce5SStephen Hemminger err = move_addr_to_user(address, len, upeer_sockaddr, 143389bddce5SStephen Hemminger upeer_addrlen); 14341da177e4SLinus Torvalds if (err < 0) 143539d8c1b6SDavid S. Miller goto out_fd; 14361da177e4SLinus Torvalds } 14371da177e4SLinus Torvalds 14381da177e4SLinus Torvalds /* File flags are not inherited via accept() unlike another OSes. */ 14391da177e4SLinus Torvalds 144039d8c1b6SDavid S. Miller fd_install(newfd, newfile); 144139d8c1b6SDavid S. Miller err = newfd; 14421da177e4SLinus Torvalds 14431da177e4SLinus Torvalds security_socket_post_accept(sock, newsock); 14441da177e4SLinus Torvalds 14451da177e4SLinus Torvalds out_put: 14466cb153caSBenjamin LaHaise fput_light(sock->file, fput_needed); 14471da177e4SLinus Torvalds out: 14481da177e4SLinus Torvalds return err; 144979f4f642SAlexey Dobriyan out_fd_simple: 145079f4f642SAlexey Dobriyan sock_release(newsock); 145179f4f642SAlexey Dobriyan put_filp(newfile); 145279f4f642SAlexey Dobriyan put_unused_fd(newfd); 145379f4f642SAlexey Dobriyan goto out_put; 145439d8c1b6SDavid S. Miller out_fd: 14559606a216SDavid S. Miller fput(newfile); 145639d8c1b6SDavid S. Miller put_unused_fd(newfd); 14571da177e4SLinus Torvalds goto out_put; 14581da177e4SLinus Torvalds } 14591da177e4SLinus Torvalds 14601da177e4SLinus Torvalds /* 14611da177e4SLinus Torvalds * Attempt to connect to a socket with the server address. The address 14621da177e4SLinus Torvalds * is in user space so we verify it is OK and move it to kernel space. 14631da177e4SLinus Torvalds * 14641da177e4SLinus Torvalds * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to 14651da177e4SLinus Torvalds * break bindings 14661da177e4SLinus Torvalds * 14671da177e4SLinus Torvalds * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and 14681da177e4SLinus Torvalds * other SEQPACKET protocols that take time to connect() as it doesn't 14691da177e4SLinus Torvalds * include the -EINPROGRESS status for such sockets. 14701da177e4SLinus Torvalds */ 14711da177e4SLinus Torvalds 147289bddce5SStephen Hemminger asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr, 147389bddce5SStephen Hemminger int addrlen) 14741da177e4SLinus Torvalds { 14751da177e4SLinus Torvalds struct socket *sock; 14761da177e4SLinus Torvalds char address[MAX_SOCK_ADDR]; 14776cb153caSBenjamin LaHaise int err, fput_needed; 14781da177e4SLinus Torvalds 14796cb153caSBenjamin LaHaise sock = sockfd_lookup_light(fd, &err, &fput_needed); 14801da177e4SLinus Torvalds if (!sock) 14811da177e4SLinus Torvalds goto out; 14821da177e4SLinus Torvalds err = move_addr_to_kernel(uservaddr, addrlen, address); 14831da177e4SLinus Torvalds if (err < 0) 14841da177e4SLinus Torvalds goto out_put; 14851da177e4SLinus Torvalds 148689bddce5SStephen Hemminger err = 148789bddce5SStephen Hemminger security_socket_connect(sock, (struct sockaddr *)address, addrlen); 14881da177e4SLinus Torvalds if (err) 14891da177e4SLinus Torvalds goto out_put; 14901da177e4SLinus Torvalds 14911da177e4SLinus Torvalds err = sock->ops->connect(sock, (struct sockaddr *)address, addrlen, 14921da177e4SLinus Torvalds sock->file->f_flags); 14931da177e4SLinus Torvalds out_put: 14946cb153caSBenjamin LaHaise fput_light(sock->file, fput_needed); 14951da177e4SLinus Torvalds out: 14961da177e4SLinus Torvalds return err; 14971da177e4SLinus Torvalds } 14981da177e4SLinus Torvalds 14991da177e4SLinus Torvalds /* 15001da177e4SLinus Torvalds * Get the local address ('name') of a socket object. Move the obtained 15011da177e4SLinus Torvalds * name to user space. 15021da177e4SLinus Torvalds */ 15031da177e4SLinus Torvalds 150489bddce5SStephen Hemminger asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr, 150589bddce5SStephen Hemminger int __user *usockaddr_len) 15061da177e4SLinus Torvalds { 15071da177e4SLinus Torvalds struct socket *sock; 15081da177e4SLinus Torvalds char address[MAX_SOCK_ADDR]; 15096cb153caSBenjamin LaHaise int len, err, fput_needed; 15101da177e4SLinus Torvalds 15116cb153caSBenjamin LaHaise sock = sockfd_lookup_light(fd, &err, &fput_needed); 15121da177e4SLinus Torvalds if (!sock) 15131da177e4SLinus Torvalds goto out; 15141da177e4SLinus Torvalds 15151da177e4SLinus Torvalds err = security_socket_getsockname(sock); 15161da177e4SLinus Torvalds if (err) 15171da177e4SLinus Torvalds goto out_put; 15181da177e4SLinus Torvalds 15191da177e4SLinus Torvalds err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 0); 15201da177e4SLinus Torvalds if (err) 15211da177e4SLinus Torvalds goto out_put; 15221da177e4SLinus Torvalds err = move_addr_to_user(address, len, usockaddr, usockaddr_len); 15231da177e4SLinus Torvalds 15241da177e4SLinus Torvalds out_put: 15256cb153caSBenjamin LaHaise fput_light(sock->file, fput_needed); 15261da177e4SLinus Torvalds out: 15271da177e4SLinus Torvalds return err; 15281da177e4SLinus Torvalds } 15291da177e4SLinus Torvalds 15301da177e4SLinus Torvalds /* 15311da177e4SLinus Torvalds * Get the remote address ('name') of a socket object. Move the obtained 15321da177e4SLinus Torvalds * name to user space. 15331da177e4SLinus Torvalds */ 15341da177e4SLinus Torvalds 153589bddce5SStephen Hemminger asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr, 153689bddce5SStephen Hemminger int __user *usockaddr_len) 15371da177e4SLinus Torvalds { 15381da177e4SLinus Torvalds struct socket *sock; 15391da177e4SLinus Torvalds char address[MAX_SOCK_ADDR]; 15406cb153caSBenjamin LaHaise int len, err, fput_needed; 15411da177e4SLinus Torvalds 154289bddce5SStephen Hemminger sock = sockfd_lookup_light(fd, &err, &fput_needed); 154389bddce5SStephen Hemminger if (sock != NULL) { 15441da177e4SLinus Torvalds err = security_socket_getpeername(sock); 15451da177e4SLinus Torvalds if (err) { 15466cb153caSBenjamin LaHaise fput_light(sock->file, fput_needed); 15471da177e4SLinus Torvalds return err; 15481da177e4SLinus Torvalds } 15491da177e4SLinus Torvalds 155089bddce5SStephen Hemminger err = 155189bddce5SStephen Hemminger sock->ops->getname(sock, (struct sockaddr *)address, &len, 155289bddce5SStephen Hemminger 1); 15531da177e4SLinus Torvalds if (!err) 155489bddce5SStephen Hemminger err = move_addr_to_user(address, len, usockaddr, 155589bddce5SStephen Hemminger usockaddr_len); 15566cb153caSBenjamin LaHaise fput_light(sock->file, fput_needed); 15571da177e4SLinus Torvalds } 15581da177e4SLinus Torvalds return err; 15591da177e4SLinus Torvalds } 15601da177e4SLinus Torvalds 15611da177e4SLinus Torvalds /* 15621da177e4SLinus Torvalds * Send a datagram to a given address. We move the address into kernel 15631da177e4SLinus Torvalds * space and check the user space data area is readable before invoking 15641da177e4SLinus Torvalds * the protocol. 15651da177e4SLinus Torvalds */ 15661da177e4SLinus Torvalds 156789bddce5SStephen Hemminger asmlinkage long sys_sendto(int fd, void __user *buff, size_t len, 156889bddce5SStephen Hemminger unsigned flags, struct sockaddr __user *addr, 156989bddce5SStephen Hemminger int addr_len) 15701da177e4SLinus Torvalds { 15711da177e4SLinus Torvalds struct socket *sock; 15721da177e4SLinus Torvalds char address[MAX_SOCK_ADDR]; 15731da177e4SLinus Torvalds int err; 15741da177e4SLinus Torvalds struct msghdr msg; 15751da177e4SLinus Torvalds struct iovec iov; 15766cb153caSBenjamin LaHaise int fput_needed; 15776cb153caSBenjamin LaHaise struct file *sock_file; 15781da177e4SLinus Torvalds 15796cb153caSBenjamin LaHaise sock_file = fget_light(fd, &fput_needed); 15804387ff75SDavid S. Miller err = -EBADF; 15816cb153caSBenjamin LaHaise if (!sock_file) 15824387ff75SDavid S. Miller goto out; 15836cb153caSBenjamin LaHaise 15846cb153caSBenjamin LaHaise sock = sock_from_file(sock_file, &err); 15851da177e4SLinus Torvalds if (!sock) 15866cb153caSBenjamin LaHaise goto out_put; 15871da177e4SLinus Torvalds iov.iov_base = buff; 15881da177e4SLinus Torvalds iov.iov_len = len; 15891da177e4SLinus Torvalds msg.msg_name = NULL; 15901da177e4SLinus Torvalds msg.msg_iov = &iov; 15911da177e4SLinus Torvalds msg.msg_iovlen = 1; 15921da177e4SLinus Torvalds msg.msg_control = NULL; 15931da177e4SLinus Torvalds msg.msg_controllen = 0; 15941da177e4SLinus Torvalds msg.msg_namelen = 0; 15956cb153caSBenjamin LaHaise if (addr) { 15961da177e4SLinus Torvalds err = move_addr_to_kernel(addr, addr_len, address); 15971da177e4SLinus Torvalds if (err < 0) 15981da177e4SLinus Torvalds goto out_put; 15991da177e4SLinus Torvalds msg.msg_name = address; 16001da177e4SLinus Torvalds msg.msg_namelen = addr_len; 16011da177e4SLinus Torvalds } 16021da177e4SLinus Torvalds if (sock->file->f_flags & O_NONBLOCK) 16031da177e4SLinus Torvalds flags |= MSG_DONTWAIT; 16041da177e4SLinus Torvalds msg.msg_flags = flags; 16051da177e4SLinus Torvalds err = sock_sendmsg(sock, &msg, len); 16061da177e4SLinus Torvalds 16071da177e4SLinus Torvalds out_put: 16086cb153caSBenjamin LaHaise fput_light(sock_file, fput_needed); 16094387ff75SDavid S. Miller out: 16101da177e4SLinus Torvalds return err; 16111da177e4SLinus Torvalds } 16121da177e4SLinus Torvalds 16131da177e4SLinus Torvalds /* 16141da177e4SLinus Torvalds * Send a datagram down a socket. 16151da177e4SLinus Torvalds */ 16161da177e4SLinus Torvalds 16171da177e4SLinus Torvalds asmlinkage long sys_send(int fd, void __user *buff, size_t len, unsigned flags) 16181da177e4SLinus Torvalds { 16191da177e4SLinus Torvalds return sys_sendto(fd, buff, len, flags, NULL, 0); 16201da177e4SLinus Torvalds } 16211da177e4SLinus Torvalds 16221da177e4SLinus Torvalds /* 16231da177e4SLinus Torvalds * Receive a frame from the socket and optionally record the address of the 16241da177e4SLinus Torvalds * sender. We verify the buffers are writable and if needed move the 16251da177e4SLinus Torvalds * sender address from kernel to user space. 16261da177e4SLinus Torvalds */ 16271da177e4SLinus Torvalds 162889bddce5SStephen Hemminger asmlinkage long sys_recvfrom(int fd, void __user *ubuf, size_t size, 162989bddce5SStephen Hemminger unsigned flags, struct sockaddr __user *addr, 163089bddce5SStephen Hemminger int __user *addr_len) 16311da177e4SLinus Torvalds { 16321da177e4SLinus Torvalds struct socket *sock; 16331da177e4SLinus Torvalds struct iovec iov; 16341da177e4SLinus Torvalds struct msghdr msg; 16351da177e4SLinus Torvalds char address[MAX_SOCK_ADDR]; 16361da177e4SLinus Torvalds int err, err2; 16376cb153caSBenjamin LaHaise struct file *sock_file; 16386cb153caSBenjamin LaHaise int fput_needed; 16391da177e4SLinus Torvalds 16406cb153caSBenjamin LaHaise sock_file = fget_light(fd, &fput_needed); 16414387ff75SDavid S. Miller err = -EBADF; 16426cb153caSBenjamin LaHaise if (!sock_file) 16434387ff75SDavid S. Miller goto out; 16446cb153caSBenjamin LaHaise 16456cb153caSBenjamin LaHaise sock = sock_from_file(sock_file, &err); 16461da177e4SLinus Torvalds if (!sock) 16474387ff75SDavid S. Miller goto out_put; 16481da177e4SLinus Torvalds 16491da177e4SLinus Torvalds msg.msg_control = NULL; 16501da177e4SLinus Torvalds msg.msg_controllen = 0; 16511da177e4SLinus Torvalds msg.msg_iovlen = 1; 16521da177e4SLinus Torvalds msg.msg_iov = &iov; 16531da177e4SLinus Torvalds iov.iov_len = size; 16541da177e4SLinus Torvalds iov.iov_base = ubuf; 16551da177e4SLinus Torvalds msg.msg_name = address; 16561da177e4SLinus Torvalds msg.msg_namelen = MAX_SOCK_ADDR; 16571da177e4SLinus Torvalds if (sock->file->f_flags & O_NONBLOCK) 16581da177e4SLinus Torvalds flags |= MSG_DONTWAIT; 16591da177e4SLinus Torvalds err = sock_recvmsg(sock, &msg, size, flags); 16601da177e4SLinus Torvalds 166189bddce5SStephen Hemminger if (err >= 0 && addr != NULL) { 16621da177e4SLinus Torvalds err2 = move_addr_to_user(address, msg.msg_namelen, addr, addr_len); 16631da177e4SLinus Torvalds if (err2 < 0) 16641da177e4SLinus Torvalds err = err2; 16651da177e4SLinus Torvalds } 16664387ff75SDavid S. Miller out_put: 16676cb153caSBenjamin LaHaise fput_light(sock_file, fput_needed); 16684387ff75SDavid S. Miller out: 16691da177e4SLinus Torvalds return err; 16701da177e4SLinus Torvalds } 16711da177e4SLinus Torvalds 16721da177e4SLinus Torvalds /* 16731da177e4SLinus Torvalds * Receive a datagram from a socket. 16741da177e4SLinus Torvalds */ 16751da177e4SLinus Torvalds 167689bddce5SStephen Hemminger asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size, 167789bddce5SStephen Hemminger unsigned flags) 16781da177e4SLinus Torvalds { 16791da177e4SLinus Torvalds return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL); 16801da177e4SLinus Torvalds } 16811da177e4SLinus Torvalds 16821da177e4SLinus Torvalds /* 16831da177e4SLinus Torvalds * Set a socket option. Because we don't know the option lengths we have 16841da177e4SLinus Torvalds * to pass the user mode parameter for the protocols to sort out. 16851da177e4SLinus Torvalds */ 16861da177e4SLinus Torvalds 168789bddce5SStephen Hemminger asmlinkage long sys_setsockopt(int fd, int level, int optname, 168889bddce5SStephen Hemminger char __user *optval, int optlen) 16891da177e4SLinus Torvalds { 16906cb153caSBenjamin LaHaise int err, fput_needed; 16911da177e4SLinus Torvalds struct socket *sock; 16921da177e4SLinus Torvalds 16931da177e4SLinus Torvalds if (optlen < 0) 16941da177e4SLinus Torvalds return -EINVAL; 16951da177e4SLinus Torvalds 169689bddce5SStephen Hemminger sock = sockfd_lookup_light(fd, &err, &fput_needed); 169789bddce5SStephen Hemminger if (sock != NULL) { 16981da177e4SLinus Torvalds err = security_socket_setsockopt(sock, level, optname); 16996cb153caSBenjamin LaHaise if (err) 17006cb153caSBenjamin LaHaise goto out_put; 17011da177e4SLinus Torvalds 17021da177e4SLinus Torvalds if (level == SOL_SOCKET) 170389bddce5SStephen Hemminger err = 170489bddce5SStephen Hemminger sock_setsockopt(sock, level, optname, optval, 170589bddce5SStephen Hemminger optlen); 17061da177e4SLinus Torvalds else 170789bddce5SStephen Hemminger err = 170889bddce5SStephen Hemminger sock->ops->setsockopt(sock, level, optname, optval, 170989bddce5SStephen Hemminger optlen); 17106cb153caSBenjamin LaHaise out_put: 17116cb153caSBenjamin LaHaise fput_light(sock->file, fput_needed); 17121da177e4SLinus Torvalds } 17131da177e4SLinus Torvalds return err; 17141da177e4SLinus Torvalds } 17151da177e4SLinus Torvalds 17161da177e4SLinus Torvalds /* 17171da177e4SLinus Torvalds * Get a socket option. Because we don't know the option lengths we have 17181da177e4SLinus Torvalds * to pass a user mode parameter for the protocols to sort out. 17191da177e4SLinus Torvalds */ 17201da177e4SLinus Torvalds 172189bddce5SStephen Hemminger asmlinkage long sys_getsockopt(int fd, int level, int optname, 172289bddce5SStephen Hemminger char __user *optval, int __user *optlen) 17231da177e4SLinus Torvalds { 17246cb153caSBenjamin LaHaise int err, fput_needed; 17251da177e4SLinus Torvalds struct socket *sock; 17261da177e4SLinus Torvalds 172789bddce5SStephen Hemminger sock = sockfd_lookup_light(fd, &err, &fput_needed); 172889bddce5SStephen Hemminger if (sock != NULL) { 17296cb153caSBenjamin LaHaise err = security_socket_getsockopt(sock, level, optname); 17306cb153caSBenjamin LaHaise if (err) 17316cb153caSBenjamin LaHaise goto out_put; 17321da177e4SLinus Torvalds 17331da177e4SLinus Torvalds if (level == SOL_SOCKET) 173489bddce5SStephen Hemminger err = 173589bddce5SStephen Hemminger sock_getsockopt(sock, level, optname, optval, 173689bddce5SStephen Hemminger optlen); 17371da177e4SLinus Torvalds else 173889bddce5SStephen Hemminger err = 173989bddce5SStephen Hemminger sock->ops->getsockopt(sock, level, optname, optval, 174089bddce5SStephen Hemminger optlen); 17416cb153caSBenjamin LaHaise out_put: 17426cb153caSBenjamin LaHaise fput_light(sock->file, fput_needed); 17431da177e4SLinus Torvalds } 17441da177e4SLinus Torvalds return err; 17451da177e4SLinus Torvalds } 17461da177e4SLinus Torvalds 17471da177e4SLinus Torvalds /* 17481da177e4SLinus Torvalds * Shutdown a socket. 17491da177e4SLinus Torvalds */ 17501da177e4SLinus Torvalds 17511da177e4SLinus Torvalds asmlinkage long sys_shutdown(int fd, int how) 17521da177e4SLinus Torvalds { 17536cb153caSBenjamin LaHaise int err, fput_needed; 17541da177e4SLinus Torvalds struct socket *sock; 17551da177e4SLinus Torvalds 175689bddce5SStephen Hemminger sock = sockfd_lookup_light(fd, &err, &fput_needed); 175789bddce5SStephen Hemminger if (sock != NULL) { 17581da177e4SLinus Torvalds err = security_socket_shutdown(sock, how); 17596cb153caSBenjamin LaHaise if (!err) 17601da177e4SLinus Torvalds err = sock->ops->shutdown(sock, how); 17616cb153caSBenjamin LaHaise fput_light(sock->file, fput_needed); 17621da177e4SLinus Torvalds } 17631da177e4SLinus Torvalds return err; 17641da177e4SLinus Torvalds } 17651da177e4SLinus Torvalds 17661da177e4SLinus Torvalds /* A couple of helpful macros for getting the address of the 32/64 bit 17671da177e4SLinus Torvalds * fields which are the same type (int / unsigned) on our platforms. 17681da177e4SLinus Torvalds */ 17691da177e4SLinus Torvalds #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member) 17701da177e4SLinus Torvalds #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen) 17711da177e4SLinus Torvalds #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags) 17721da177e4SLinus Torvalds 17731da177e4SLinus Torvalds /* 17741da177e4SLinus Torvalds * BSD sendmsg interface 17751da177e4SLinus Torvalds */ 17761da177e4SLinus Torvalds 17771da177e4SLinus Torvalds asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags) 17781da177e4SLinus Torvalds { 177989bddce5SStephen Hemminger struct compat_msghdr __user *msg_compat = 178089bddce5SStephen Hemminger (struct compat_msghdr __user *)msg; 17811da177e4SLinus Torvalds struct socket *sock; 17821da177e4SLinus Torvalds char address[MAX_SOCK_ADDR]; 17831da177e4SLinus Torvalds struct iovec iovstack[UIO_FASTIOV], *iov = iovstack; 1784b9d717a7SAlex Williamson unsigned char ctl[sizeof(struct cmsghdr) + 20] 1785b9d717a7SAlex Williamson __attribute__ ((aligned(sizeof(__kernel_size_t)))); 1786b9d717a7SAlex Williamson /* 20 is size of ipv6_pktinfo */ 17871da177e4SLinus Torvalds unsigned char *ctl_buf = ctl; 17881da177e4SLinus Torvalds struct msghdr msg_sys; 17891da177e4SLinus Torvalds int err, ctl_len, iov_size, total_len; 17906cb153caSBenjamin LaHaise int fput_needed; 17911da177e4SLinus Torvalds 17921da177e4SLinus Torvalds err = -EFAULT; 17931da177e4SLinus Torvalds if (MSG_CMSG_COMPAT & flags) { 17941da177e4SLinus Torvalds if (get_compat_msghdr(&msg_sys, msg_compat)) 17951da177e4SLinus Torvalds return -EFAULT; 179689bddce5SStephen Hemminger } 179789bddce5SStephen Hemminger else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr))) 17981da177e4SLinus Torvalds return -EFAULT; 17991da177e4SLinus Torvalds 18006cb153caSBenjamin LaHaise sock = sockfd_lookup_light(fd, &err, &fput_needed); 18011da177e4SLinus Torvalds if (!sock) 18021da177e4SLinus Torvalds goto out; 18031da177e4SLinus Torvalds 18041da177e4SLinus Torvalds /* do not move before msg_sys is valid */ 18051da177e4SLinus Torvalds err = -EMSGSIZE; 18061da177e4SLinus Torvalds if (msg_sys.msg_iovlen > UIO_MAXIOV) 18071da177e4SLinus Torvalds goto out_put; 18081da177e4SLinus Torvalds 18091da177e4SLinus Torvalds /* Check whether to allocate the iovec area */ 18101da177e4SLinus Torvalds err = -ENOMEM; 18111da177e4SLinus Torvalds iov_size = msg_sys.msg_iovlen * sizeof(struct iovec); 18121da177e4SLinus Torvalds if (msg_sys.msg_iovlen > UIO_FASTIOV) { 18131da177e4SLinus Torvalds iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL); 18141da177e4SLinus Torvalds if (!iov) 18151da177e4SLinus Torvalds goto out_put; 18161da177e4SLinus Torvalds } 18171da177e4SLinus Torvalds 18181da177e4SLinus Torvalds /* This will also move the address data into kernel space */ 18191da177e4SLinus Torvalds if (MSG_CMSG_COMPAT & flags) { 18201da177e4SLinus Torvalds err = verify_compat_iovec(&msg_sys, iov, address, VERIFY_READ); 18211da177e4SLinus Torvalds } else 18221da177e4SLinus Torvalds err = verify_iovec(&msg_sys, iov, address, VERIFY_READ); 18231da177e4SLinus Torvalds if (err < 0) 18241da177e4SLinus Torvalds goto out_freeiov; 18251da177e4SLinus Torvalds total_len = err; 18261da177e4SLinus Torvalds 18271da177e4SLinus Torvalds err = -ENOBUFS; 18281da177e4SLinus Torvalds 18291da177e4SLinus Torvalds if (msg_sys.msg_controllen > INT_MAX) 18301da177e4SLinus Torvalds goto out_freeiov; 18311da177e4SLinus Torvalds ctl_len = msg_sys.msg_controllen; 18321da177e4SLinus Torvalds if ((MSG_CMSG_COMPAT & flags) && ctl_len) { 183389bddce5SStephen Hemminger err = 183489bddce5SStephen Hemminger cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl, 183589bddce5SStephen Hemminger sizeof(ctl)); 18361da177e4SLinus Torvalds if (err) 18371da177e4SLinus Torvalds goto out_freeiov; 18381da177e4SLinus Torvalds ctl_buf = msg_sys.msg_control; 18398920e8f9SAl Viro ctl_len = msg_sys.msg_controllen; 18401da177e4SLinus Torvalds } else if (ctl_len) { 184189bddce5SStephen Hemminger if (ctl_len > sizeof(ctl)) { 18421da177e4SLinus Torvalds ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL); 18431da177e4SLinus Torvalds if (ctl_buf == NULL) 18441da177e4SLinus Torvalds goto out_freeiov; 18451da177e4SLinus Torvalds } 18461da177e4SLinus Torvalds err = -EFAULT; 18471da177e4SLinus Torvalds /* 18481da177e4SLinus Torvalds * Careful! Before this, msg_sys.msg_control contains a user pointer. 18491da177e4SLinus Torvalds * Afterwards, it will be a kernel pointer. Thus the compiler-assisted 18501da177e4SLinus Torvalds * checking falls down on this. 18511da177e4SLinus Torvalds */ 185289bddce5SStephen Hemminger if (copy_from_user(ctl_buf, (void __user *)msg_sys.msg_control, 185389bddce5SStephen Hemminger ctl_len)) 18541da177e4SLinus Torvalds goto out_freectl; 18551da177e4SLinus Torvalds msg_sys.msg_control = ctl_buf; 18561da177e4SLinus Torvalds } 18571da177e4SLinus Torvalds msg_sys.msg_flags = flags; 18581da177e4SLinus Torvalds 18591da177e4SLinus Torvalds if (sock->file->f_flags & O_NONBLOCK) 18601da177e4SLinus Torvalds msg_sys.msg_flags |= MSG_DONTWAIT; 18611da177e4SLinus Torvalds err = sock_sendmsg(sock, &msg_sys, total_len); 18621da177e4SLinus Torvalds 18631da177e4SLinus Torvalds out_freectl: 18641da177e4SLinus Torvalds if (ctl_buf != ctl) 18651da177e4SLinus Torvalds sock_kfree_s(sock->sk, ctl_buf, ctl_len); 18661da177e4SLinus Torvalds out_freeiov: 18671da177e4SLinus Torvalds if (iov != iovstack) 18681da177e4SLinus Torvalds sock_kfree_s(sock->sk, iov, iov_size); 18691da177e4SLinus Torvalds out_put: 18706cb153caSBenjamin LaHaise fput_light(sock->file, fput_needed); 18711da177e4SLinus Torvalds out: 18721da177e4SLinus Torvalds return err; 18731da177e4SLinus Torvalds } 18741da177e4SLinus Torvalds 18751da177e4SLinus Torvalds /* 18761da177e4SLinus Torvalds * BSD recvmsg interface 18771da177e4SLinus Torvalds */ 18781da177e4SLinus Torvalds 187989bddce5SStephen Hemminger asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, 188089bddce5SStephen Hemminger unsigned int flags) 18811da177e4SLinus Torvalds { 188289bddce5SStephen Hemminger struct compat_msghdr __user *msg_compat = 188389bddce5SStephen Hemminger (struct compat_msghdr __user *)msg; 18841da177e4SLinus Torvalds struct socket *sock; 18851da177e4SLinus Torvalds struct iovec iovstack[UIO_FASTIOV]; 18861da177e4SLinus Torvalds struct iovec *iov = iovstack; 18871da177e4SLinus Torvalds struct msghdr msg_sys; 18881da177e4SLinus Torvalds unsigned long cmsg_ptr; 18891da177e4SLinus Torvalds int err, iov_size, total_len, len; 18906cb153caSBenjamin LaHaise int fput_needed; 18911da177e4SLinus Torvalds 18921da177e4SLinus Torvalds /* kernel mode address */ 18931da177e4SLinus Torvalds char addr[MAX_SOCK_ADDR]; 18941da177e4SLinus Torvalds 18951da177e4SLinus Torvalds /* user mode address pointers */ 18961da177e4SLinus Torvalds struct sockaddr __user *uaddr; 18971da177e4SLinus Torvalds int __user *uaddr_len; 18981da177e4SLinus Torvalds 18991da177e4SLinus Torvalds if (MSG_CMSG_COMPAT & flags) { 19001da177e4SLinus Torvalds if (get_compat_msghdr(&msg_sys, msg_compat)) 19011da177e4SLinus Torvalds return -EFAULT; 190289bddce5SStephen Hemminger } 190389bddce5SStephen Hemminger else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr))) 19041da177e4SLinus Torvalds return -EFAULT; 19051da177e4SLinus Torvalds 19066cb153caSBenjamin LaHaise sock = sockfd_lookup_light(fd, &err, &fput_needed); 19071da177e4SLinus Torvalds if (!sock) 19081da177e4SLinus Torvalds goto out; 19091da177e4SLinus Torvalds 19101da177e4SLinus Torvalds err = -EMSGSIZE; 19111da177e4SLinus Torvalds if (msg_sys.msg_iovlen > UIO_MAXIOV) 19121da177e4SLinus Torvalds goto out_put; 19131da177e4SLinus Torvalds 19141da177e4SLinus Torvalds /* Check whether to allocate the iovec area */ 19151da177e4SLinus Torvalds err = -ENOMEM; 19161da177e4SLinus Torvalds iov_size = msg_sys.msg_iovlen * sizeof(struct iovec); 19171da177e4SLinus Torvalds if (msg_sys.msg_iovlen > UIO_FASTIOV) { 19181da177e4SLinus Torvalds iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL); 19191da177e4SLinus Torvalds if (!iov) 19201da177e4SLinus Torvalds goto out_put; 19211da177e4SLinus Torvalds } 19221da177e4SLinus Torvalds 19231da177e4SLinus Torvalds /* 19241da177e4SLinus Torvalds * Save the user-mode address (verify_iovec will change the 19251da177e4SLinus Torvalds * kernel msghdr to use the kernel address space) 19261da177e4SLinus Torvalds */ 19271da177e4SLinus Torvalds 19281da177e4SLinus Torvalds uaddr = (void __user *)msg_sys.msg_name; 19291da177e4SLinus Torvalds uaddr_len = COMPAT_NAMELEN(msg); 19301da177e4SLinus Torvalds if (MSG_CMSG_COMPAT & flags) { 19311da177e4SLinus Torvalds err = verify_compat_iovec(&msg_sys, iov, addr, VERIFY_WRITE); 19321da177e4SLinus Torvalds } else 19331da177e4SLinus Torvalds err = verify_iovec(&msg_sys, iov, addr, VERIFY_WRITE); 19341da177e4SLinus Torvalds if (err < 0) 19351da177e4SLinus Torvalds goto out_freeiov; 19361da177e4SLinus Torvalds total_len = err; 19371da177e4SLinus Torvalds 19381da177e4SLinus Torvalds cmsg_ptr = (unsigned long)msg_sys.msg_control; 19394a19542eSUlrich Drepper msg_sys.msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT); 19401da177e4SLinus Torvalds 19411da177e4SLinus Torvalds if (sock->file->f_flags & O_NONBLOCK) 19421da177e4SLinus Torvalds flags |= MSG_DONTWAIT; 19431da177e4SLinus Torvalds err = sock_recvmsg(sock, &msg_sys, total_len, flags); 19441da177e4SLinus Torvalds if (err < 0) 19451da177e4SLinus Torvalds goto out_freeiov; 19461da177e4SLinus Torvalds len = err; 19471da177e4SLinus Torvalds 19481da177e4SLinus Torvalds if (uaddr != NULL) { 194989bddce5SStephen Hemminger err = move_addr_to_user(addr, msg_sys.msg_namelen, uaddr, 195089bddce5SStephen Hemminger uaddr_len); 19511da177e4SLinus Torvalds if (err < 0) 19521da177e4SLinus Torvalds goto out_freeiov; 19531da177e4SLinus Torvalds } 195437f7f421SDavid S. Miller err = __put_user((msg_sys.msg_flags & ~MSG_CMSG_COMPAT), 195537f7f421SDavid S. Miller COMPAT_FLAGS(msg)); 19561da177e4SLinus Torvalds if (err) 19571da177e4SLinus Torvalds goto out_freeiov; 19581da177e4SLinus Torvalds if (MSG_CMSG_COMPAT & flags) 19591da177e4SLinus Torvalds err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr, 19601da177e4SLinus Torvalds &msg_compat->msg_controllen); 19611da177e4SLinus Torvalds else 19621da177e4SLinus Torvalds err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr, 19631da177e4SLinus Torvalds &msg->msg_controllen); 19641da177e4SLinus Torvalds if (err) 19651da177e4SLinus Torvalds goto out_freeiov; 19661da177e4SLinus Torvalds err = len; 19671da177e4SLinus Torvalds 19681da177e4SLinus Torvalds out_freeiov: 19691da177e4SLinus Torvalds if (iov != iovstack) 19701da177e4SLinus Torvalds sock_kfree_s(sock->sk, iov, iov_size); 19711da177e4SLinus Torvalds out_put: 19726cb153caSBenjamin LaHaise fput_light(sock->file, fput_needed); 19731da177e4SLinus Torvalds out: 19741da177e4SLinus Torvalds return err; 19751da177e4SLinus Torvalds } 19761da177e4SLinus Torvalds 19771da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_SOCKETCALL 19781da177e4SLinus Torvalds 19791da177e4SLinus Torvalds /* Argument list sizes for sys_socketcall */ 19801da177e4SLinus Torvalds #define AL(x) ((x) * sizeof(unsigned long)) 198189bddce5SStephen Hemminger static const unsigned char nargs[18]={ 198289bddce5SStephen Hemminger AL(0),AL(3),AL(3),AL(3),AL(2),AL(3), 19831da177e4SLinus Torvalds AL(3),AL(3),AL(4),AL(4),AL(4),AL(6), 198489bddce5SStephen Hemminger AL(6),AL(2),AL(5),AL(5),AL(3),AL(3) 198589bddce5SStephen Hemminger }; 198689bddce5SStephen Hemminger 19871da177e4SLinus Torvalds #undef AL 19881da177e4SLinus Torvalds 19891da177e4SLinus Torvalds /* 19901da177e4SLinus Torvalds * System call vectors. 19911da177e4SLinus Torvalds * 19921da177e4SLinus Torvalds * Argument checking cleaned up. Saved 20% in size. 19931da177e4SLinus Torvalds * This function doesn't need to set the kernel lock because 19941da177e4SLinus Torvalds * it is set by the callees. 19951da177e4SLinus Torvalds */ 19961da177e4SLinus Torvalds 19971da177e4SLinus Torvalds asmlinkage long sys_socketcall(int call, unsigned long __user *args) 19981da177e4SLinus Torvalds { 19991da177e4SLinus Torvalds unsigned long a[6]; 20001da177e4SLinus Torvalds unsigned long a0, a1; 20011da177e4SLinus Torvalds int err; 20021da177e4SLinus Torvalds 20031da177e4SLinus Torvalds if (call < 1 || call > SYS_RECVMSG) 20041da177e4SLinus Torvalds return -EINVAL; 20051da177e4SLinus Torvalds 20061da177e4SLinus Torvalds /* copy_from_user should be SMP safe. */ 20071da177e4SLinus Torvalds if (copy_from_user(a, args, nargs[call])) 20081da177e4SLinus Torvalds return -EFAULT; 20091da177e4SLinus Torvalds 20104bcff1b3SDavid Woodhouse err = audit_socketcall(nargs[call] / sizeof(unsigned long), a); 20113ec3b2fbSDavid Woodhouse if (err) 20123ec3b2fbSDavid Woodhouse return err; 20133ec3b2fbSDavid Woodhouse 20141da177e4SLinus Torvalds a0 = a[0]; 20151da177e4SLinus Torvalds a1 = a[1]; 20161da177e4SLinus Torvalds 201789bddce5SStephen Hemminger switch (call) { 20181da177e4SLinus Torvalds case SYS_SOCKET: 20191da177e4SLinus Torvalds err = sys_socket(a0, a1, a[2]); 20201da177e4SLinus Torvalds break; 20211da177e4SLinus Torvalds case SYS_BIND: 20221da177e4SLinus Torvalds err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]); 20231da177e4SLinus Torvalds break; 20241da177e4SLinus Torvalds case SYS_CONNECT: 20251da177e4SLinus Torvalds err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]); 20261da177e4SLinus Torvalds break; 20271da177e4SLinus Torvalds case SYS_LISTEN: 20281da177e4SLinus Torvalds err = sys_listen(a0, a1); 20291da177e4SLinus Torvalds break; 20301da177e4SLinus Torvalds case SYS_ACCEPT: 203189bddce5SStephen Hemminger err = 203289bddce5SStephen Hemminger sys_accept(a0, (struct sockaddr __user *)a1, 203389bddce5SStephen Hemminger (int __user *)a[2]); 20341da177e4SLinus Torvalds break; 20351da177e4SLinus Torvalds case SYS_GETSOCKNAME: 203689bddce5SStephen Hemminger err = 203789bddce5SStephen Hemminger sys_getsockname(a0, (struct sockaddr __user *)a1, 203889bddce5SStephen Hemminger (int __user *)a[2]); 20391da177e4SLinus Torvalds break; 20401da177e4SLinus Torvalds case SYS_GETPEERNAME: 204189bddce5SStephen Hemminger err = 204289bddce5SStephen Hemminger sys_getpeername(a0, (struct sockaddr __user *)a1, 204389bddce5SStephen Hemminger (int __user *)a[2]); 20441da177e4SLinus Torvalds break; 20451da177e4SLinus Torvalds case SYS_SOCKETPAIR: 20461da177e4SLinus Torvalds err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]); 20471da177e4SLinus Torvalds break; 20481da177e4SLinus Torvalds case SYS_SEND: 20491da177e4SLinus Torvalds err = sys_send(a0, (void __user *)a1, a[2], a[3]); 20501da177e4SLinus Torvalds break; 20511da177e4SLinus Torvalds case SYS_SENDTO: 20521da177e4SLinus Torvalds err = sys_sendto(a0, (void __user *)a1, a[2], a[3], 20531da177e4SLinus Torvalds (struct sockaddr __user *)a[4], a[5]); 20541da177e4SLinus Torvalds break; 20551da177e4SLinus Torvalds case SYS_RECV: 20561da177e4SLinus Torvalds err = sys_recv(a0, (void __user *)a1, a[2], a[3]); 20571da177e4SLinus Torvalds break; 20581da177e4SLinus Torvalds case SYS_RECVFROM: 20591da177e4SLinus Torvalds err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3], 206089bddce5SStephen Hemminger (struct sockaddr __user *)a[4], 206189bddce5SStephen Hemminger (int __user *)a[5]); 20621da177e4SLinus Torvalds break; 20631da177e4SLinus Torvalds case SYS_SHUTDOWN: 20641da177e4SLinus Torvalds err = sys_shutdown(a0, a1); 20651da177e4SLinus Torvalds break; 20661da177e4SLinus Torvalds case SYS_SETSOCKOPT: 20671da177e4SLinus Torvalds err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]); 20681da177e4SLinus Torvalds break; 20691da177e4SLinus Torvalds case SYS_GETSOCKOPT: 207089bddce5SStephen Hemminger err = 207189bddce5SStephen Hemminger sys_getsockopt(a0, a1, a[2], (char __user *)a[3], 207289bddce5SStephen Hemminger (int __user *)a[4]); 20731da177e4SLinus Torvalds break; 20741da177e4SLinus Torvalds case SYS_SENDMSG: 20751da177e4SLinus Torvalds err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]); 20761da177e4SLinus Torvalds break; 20771da177e4SLinus Torvalds case SYS_RECVMSG: 20781da177e4SLinus Torvalds err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]); 20791da177e4SLinus Torvalds break; 20801da177e4SLinus Torvalds default: 20811da177e4SLinus Torvalds err = -EINVAL; 20821da177e4SLinus Torvalds break; 20831da177e4SLinus Torvalds } 20841da177e4SLinus Torvalds return err; 20851da177e4SLinus Torvalds } 20861da177e4SLinus Torvalds 20871da177e4SLinus Torvalds #endif /* __ARCH_WANT_SYS_SOCKETCALL */ 20881da177e4SLinus Torvalds 208955737fdaSStephen Hemminger /** 209055737fdaSStephen Hemminger * sock_register - add a socket protocol handler 209155737fdaSStephen Hemminger * @ops: description of protocol 209255737fdaSStephen Hemminger * 20931da177e4SLinus Torvalds * This function is called by a protocol handler that wants to 20941da177e4SLinus Torvalds * advertise its address family, and have it linked into the 209555737fdaSStephen Hemminger * socket interface. The value ops->family coresponds to the 209655737fdaSStephen Hemminger * socket system call protocol family. 20971da177e4SLinus Torvalds */ 2098f0fd27d4SStephen Hemminger int sock_register(const struct net_proto_family *ops) 20991da177e4SLinus Torvalds { 21001da177e4SLinus Torvalds int err; 21011da177e4SLinus Torvalds 21021da177e4SLinus Torvalds if (ops->family >= NPROTO) { 210389bddce5SStephen Hemminger printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family, 210489bddce5SStephen Hemminger NPROTO); 21051da177e4SLinus Torvalds return -ENOBUFS; 21061da177e4SLinus Torvalds } 210755737fdaSStephen Hemminger 210855737fdaSStephen Hemminger spin_lock(&net_family_lock); 210955737fdaSStephen Hemminger if (net_families[ops->family]) 21101da177e4SLinus Torvalds err = -EEXIST; 211155737fdaSStephen Hemminger else { 21121da177e4SLinus Torvalds net_families[ops->family] = ops; 21131da177e4SLinus Torvalds err = 0; 21141da177e4SLinus Torvalds } 211555737fdaSStephen Hemminger spin_unlock(&net_family_lock); 211655737fdaSStephen Hemminger 211789bddce5SStephen Hemminger printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family); 21181da177e4SLinus Torvalds return err; 21191da177e4SLinus Torvalds } 21201da177e4SLinus Torvalds 212155737fdaSStephen Hemminger /** 212255737fdaSStephen Hemminger * sock_unregister - remove a protocol handler 212355737fdaSStephen Hemminger * @family: protocol family to remove 212455737fdaSStephen Hemminger * 21251da177e4SLinus Torvalds * This function is called by a protocol handler that wants to 21261da177e4SLinus Torvalds * remove its address family, and have it unlinked from the 212755737fdaSStephen Hemminger * new socket creation. 212855737fdaSStephen Hemminger * 212955737fdaSStephen Hemminger * If protocol handler is a module, then it can use module reference 213055737fdaSStephen Hemminger * counts to protect against new references. If protocol handler is not 213155737fdaSStephen Hemminger * a module then it needs to provide its own protection in 213255737fdaSStephen Hemminger * the ops->create routine. 21331da177e4SLinus Torvalds */ 2134f0fd27d4SStephen Hemminger void sock_unregister(int family) 21351da177e4SLinus Torvalds { 2136f0fd27d4SStephen Hemminger BUG_ON(family < 0 || family >= NPROTO); 21371da177e4SLinus Torvalds 213855737fdaSStephen Hemminger spin_lock(&net_family_lock); 21391da177e4SLinus Torvalds net_families[family] = NULL; 214055737fdaSStephen Hemminger spin_unlock(&net_family_lock); 214155737fdaSStephen Hemminger 214255737fdaSStephen Hemminger synchronize_rcu(); 214355737fdaSStephen Hemminger 214489bddce5SStephen Hemminger printk(KERN_INFO "NET: Unregistered protocol family %d\n", family); 21451da177e4SLinus Torvalds } 21461da177e4SLinus Torvalds 214777d76ea3SAndi Kleen static int __init sock_init(void) 21481da177e4SLinus Torvalds { 21491da177e4SLinus Torvalds /* 21501da177e4SLinus Torvalds * Initialize sock SLAB cache. 21511da177e4SLinus Torvalds */ 21521da177e4SLinus Torvalds 21531da177e4SLinus Torvalds sk_init(); 21541da177e4SLinus Torvalds 21551da177e4SLinus Torvalds /* 21561da177e4SLinus Torvalds * Initialize skbuff SLAB cache 21571da177e4SLinus Torvalds */ 21581da177e4SLinus Torvalds skb_init(); 21591da177e4SLinus Torvalds 21601da177e4SLinus Torvalds /* 21611da177e4SLinus Torvalds * Initialize the protocols module. 21621da177e4SLinus Torvalds */ 21631da177e4SLinus Torvalds 21641da177e4SLinus Torvalds init_inodecache(); 21651da177e4SLinus Torvalds register_filesystem(&sock_fs_type); 21661da177e4SLinus Torvalds sock_mnt = kern_mount(&sock_fs_type); 216777d76ea3SAndi Kleen 216877d76ea3SAndi Kleen /* The real protocol initialization is performed in later initcalls. 21691da177e4SLinus Torvalds */ 21701da177e4SLinus Torvalds 21711da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER 21721da177e4SLinus Torvalds netfilter_init(); 21731da177e4SLinus Torvalds #endif 2174cbeb321aSDavid S. Miller 2175cbeb321aSDavid S. Miller return 0; 21761da177e4SLinus Torvalds } 21771da177e4SLinus Torvalds 217877d76ea3SAndi Kleen core_initcall(sock_init); /* early initcall */ 217977d76ea3SAndi Kleen 21801da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 21811da177e4SLinus Torvalds void socket_seq_show(struct seq_file *seq) 21821da177e4SLinus Torvalds { 21831da177e4SLinus Torvalds int cpu; 21841da177e4SLinus Torvalds int counter = 0; 21851da177e4SLinus Torvalds 21866f912042SKAMEZAWA Hiroyuki for_each_possible_cpu(cpu) 21871da177e4SLinus Torvalds counter += per_cpu(sockets_in_use, cpu); 21881da177e4SLinus Torvalds 21891da177e4SLinus Torvalds /* It can be negative, by the way. 8) */ 21901da177e4SLinus Torvalds if (counter < 0) 21911da177e4SLinus Torvalds counter = 0; 21921da177e4SLinus Torvalds 21931da177e4SLinus Torvalds seq_printf(seq, "sockets: used %d\n", counter); 21941da177e4SLinus Torvalds } 21951da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 21961da177e4SLinus Torvalds 219789bbfc95SShaun Pereira #ifdef CONFIG_COMPAT 219889bbfc95SShaun Pereira static long compat_sock_ioctl(struct file *file, unsigned cmd, 219989bbfc95SShaun Pereira unsigned long arg) 220089bbfc95SShaun Pereira { 220189bbfc95SShaun Pereira struct socket *sock = file->private_data; 220289bbfc95SShaun Pereira int ret = -ENOIOCTLCMD; 220389bbfc95SShaun Pereira 220489bbfc95SShaun Pereira if (sock->ops->compat_ioctl) 220589bbfc95SShaun Pereira ret = sock->ops->compat_ioctl(sock, cmd, arg); 220689bbfc95SShaun Pereira 220789bbfc95SShaun Pereira return ret; 220889bbfc95SShaun Pereira } 220989bbfc95SShaun Pereira #endif 221089bbfc95SShaun Pereira 2211ac5a488eSSridhar Samudrala int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen) 2212ac5a488eSSridhar Samudrala { 2213ac5a488eSSridhar Samudrala return sock->ops->bind(sock, addr, addrlen); 2214ac5a488eSSridhar Samudrala } 2215ac5a488eSSridhar Samudrala 2216ac5a488eSSridhar Samudrala int kernel_listen(struct socket *sock, int backlog) 2217ac5a488eSSridhar Samudrala { 2218ac5a488eSSridhar Samudrala return sock->ops->listen(sock, backlog); 2219ac5a488eSSridhar Samudrala } 2220ac5a488eSSridhar Samudrala 2221ac5a488eSSridhar Samudrala int kernel_accept(struct socket *sock, struct socket **newsock, int flags) 2222ac5a488eSSridhar Samudrala { 2223ac5a488eSSridhar Samudrala struct sock *sk = sock->sk; 2224ac5a488eSSridhar Samudrala int err; 2225ac5a488eSSridhar Samudrala 2226ac5a488eSSridhar Samudrala err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol, 2227ac5a488eSSridhar Samudrala newsock); 2228ac5a488eSSridhar Samudrala if (err < 0) 2229ac5a488eSSridhar Samudrala goto done; 2230ac5a488eSSridhar Samudrala 2231ac5a488eSSridhar Samudrala err = sock->ops->accept(sock, *newsock, flags); 2232ac5a488eSSridhar Samudrala if (err < 0) { 2233ac5a488eSSridhar Samudrala sock_release(*newsock); 2234ac5a488eSSridhar Samudrala goto done; 2235ac5a488eSSridhar Samudrala } 2236ac5a488eSSridhar Samudrala 2237ac5a488eSSridhar Samudrala (*newsock)->ops = sock->ops; 2238ac5a488eSSridhar Samudrala 2239ac5a488eSSridhar Samudrala done: 2240ac5a488eSSridhar Samudrala return err; 2241ac5a488eSSridhar Samudrala } 2242ac5a488eSSridhar Samudrala 2243ac5a488eSSridhar Samudrala int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen, 2244ac5a488eSSridhar Samudrala int flags) 2245ac5a488eSSridhar Samudrala { 2246ac5a488eSSridhar Samudrala return sock->ops->connect(sock, addr, addrlen, flags); 2247ac5a488eSSridhar Samudrala } 2248ac5a488eSSridhar Samudrala 2249ac5a488eSSridhar Samudrala int kernel_getsockname(struct socket *sock, struct sockaddr *addr, 2250ac5a488eSSridhar Samudrala int *addrlen) 2251ac5a488eSSridhar Samudrala { 2252ac5a488eSSridhar Samudrala return sock->ops->getname(sock, addr, addrlen, 0); 2253ac5a488eSSridhar Samudrala } 2254ac5a488eSSridhar Samudrala 2255ac5a488eSSridhar Samudrala int kernel_getpeername(struct socket *sock, struct sockaddr *addr, 2256ac5a488eSSridhar Samudrala int *addrlen) 2257ac5a488eSSridhar Samudrala { 2258ac5a488eSSridhar Samudrala return sock->ops->getname(sock, addr, addrlen, 1); 2259ac5a488eSSridhar Samudrala } 2260ac5a488eSSridhar Samudrala 2261ac5a488eSSridhar Samudrala int kernel_getsockopt(struct socket *sock, int level, int optname, 2262ac5a488eSSridhar Samudrala char *optval, int *optlen) 2263ac5a488eSSridhar Samudrala { 2264ac5a488eSSridhar Samudrala mm_segment_t oldfs = get_fs(); 2265ac5a488eSSridhar Samudrala int err; 2266ac5a488eSSridhar Samudrala 2267ac5a488eSSridhar Samudrala set_fs(KERNEL_DS); 2268ac5a488eSSridhar Samudrala if (level == SOL_SOCKET) 2269ac5a488eSSridhar Samudrala err = sock_getsockopt(sock, level, optname, optval, optlen); 2270ac5a488eSSridhar Samudrala else 2271ac5a488eSSridhar Samudrala err = sock->ops->getsockopt(sock, level, optname, optval, 2272ac5a488eSSridhar Samudrala optlen); 2273ac5a488eSSridhar Samudrala set_fs(oldfs); 2274ac5a488eSSridhar Samudrala return err; 2275ac5a488eSSridhar Samudrala } 2276ac5a488eSSridhar Samudrala 2277ac5a488eSSridhar Samudrala int kernel_setsockopt(struct socket *sock, int level, int optname, 2278ac5a488eSSridhar Samudrala char *optval, int optlen) 2279ac5a488eSSridhar Samudrala { 2280ac5a488eSSridhar Samudrala mm_segment_t oldfs = get_fs(); 2281ac5a488eSSridhar Samudrala int err; 2282ac5a488eSSridhar Samudrala 2283ac5a488eSSridhar Samudrala set_fs(KERNEL_DS); 2284ac5a488eSSridhar Samudrala if (level == SOL_SOCKET) 2285ac5a488eSSridhar Samudrala err = sock_setsockopt(sock, level, optname, optval, optlen); 2286ac5a488eSSridhar Samudrala else 2287ac5a488eSSridhar Samudrala err = sock->ops->setsockopt(sock, level, optname, optval, 2288ac5a488eSSridhar Samudrala optlen); 2289ac5a488eSSridhar Samudrala set_fs(oldfs); 2290ac5a488eSSridhar Samudrala return err; 2291ac5a488eSSridhar Samudrala } 2292ac5a488eSSridhar Samudrala 2293ac5a488eSSridhar Samudrala int kernel_sendpage(struct socket *sock, struct page *page, int offset, 2294ac5a488eSSridhar Samudrala size_t size, int flags) 2295ac5a488eSSridhar Samudrala { 2296ac5a488eSSridhar Samudrala if (sock->ops->sendpage) 2297ac5a488eSSridhar Samudrala return sock->ops->sendpage(sock, page, offset, size, flags); 2298ac5a488eSSridhar Samudrala 2299ac5a488eSSridhar Samudrala return sock_no_sendpage(sock, page, offset, size, flags); 2300ac5a488eSSridhar Samudrala } 2301ac5a488eSSridhar Samudrala 2302ac5a488eSSridhar Samudrala int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg) 2303ac5a488eSSridhar Samudrala { 2304ac5a488eSSridhar Samudrala mm_segment_t oldfs = get_fs(); 2305ac5a488eSSridhar Samudrala int err; 2306ac5a488eSSridhar Samudrala 2307ac5a488eSSridhar Samudrala set_fs(KERNEL_DS); 2308ac5a488eSSridhar Samudrala err = sock->ops->ioctl(sock, cmd, arg); 2309ac5a488eSSridhar Samudrala set_fs(oldfs); 2310ac5a488eSSridhar Samudrala 2311ac5a488eSSridhar Samudrala return err; 2312ac5a488eSSridhar Samudrala } 2313ac5a488eSSridhar Samudrala 23141da177e4SLinus Torvalds /* ABI emulation layers need these two */ 23151da177e4SLinus Torvalds EXPORT_SYMBOL(move_addr_to_kernel); 23161da177e4SLinus Torvalds EXPORT_SYMBOL(move_addr_to_user); 23171da177e4SLinus Torvalds EXPORT_SYMBOL(sock_create); 23181da177e4SLinus Torvalds EXPORT_SYMBOL(sock_create_kern); 23191da177e4SLinus Torvalds EXPORT_SYMBOL(sock_create_lite); 23201da177e4SLinus Torvalds EXPORT_SYMBOL(sock_map_fd); 23211da177e4SLinus Torvalds EXPORT_SYMBOL(sock_recvmsg); 23221da177e4SLinus Torvalds EXPORT_SYMBOL(sock_register); 23231da177e4SLinus Torvalds EXPORT_SYMBOL(sock_release); 23241da177e4SLinus Torvalds EXPORT_SYMBOL(sock_sendmsg); 23251da177e4SLinus Torvalds EXPORT_SYMBOL(sock_unregister); 23261da177e4SLinus Torvalds EXPORT_SYMBOL(sock_wake_async); 23271da177e4SLinus Torvalds EXPORT_SYMBOL(sockfd_lookup); 23281da177e4SLinus Torvalds EXPORT_SYMBOL(kernel_sendmsg); 23291da177e4SLinus Torvalds EXPORT_SYMBOL(kernel_recvmsg); 2330ac5a488eSSridhar Samudrala EXPORT_SYMBOL(kernel_bind); 2331ac5a488eSSridhar Samudrala EXPORT_SYMBOL(kernel_listen); 2332ac5a488eSSridhar Samudrala EXPORT_SYMBOL(kernel_accept); 2333ac5a488eSSridhar Samudrala EXPORT_SYMBOL(kernel_connect); 2334ac5a488eSSridhar Samudrala EXPORT_SYMBOL(kernel_getsockname); 2335ac5a488eSSridhar Samudrala EXPORT_SYMBOL(kernel_getpeername); 2336ac5a488eSSridhar Samudrala EXPORT_SYMBOL(kernel_getsockopt); 2337ac5a488eSSridhar Samudrala EXPORT_SYMBOL(kernel_setsockopt); 2338ac5a488eSSridhar Samudrala EXPORT_SYMBOL(kernel_sendpage); 2339ac5a488eSSridhar Samudrala EXPORT_SYMBOL(kernel_sock_ioctl); 2340