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> 66aaca0bdcSUlrich Drepper #include <linux/thread_info.h> 6755737fdaSStephen Hemminger #include <linux/rcupdate.h> 681da177e4SLinus Torvalds #include <linux/netdevice.h> 691da177e4SLinus Torvalds #include <linux/proc_fs.h> 701da177e4SLinus Torvalds #include <linux/seq_file.h> 714a3e2f71SArjan van de Ven #include <linux/mutex.h> 721da177e4SLinus Torvalds #include <linux/wanrouter.h> 731da177e4SLinus Torvalds #include <linux/if_bridge.h> 7420380731SArnaldo Carvalho de Melo #include <linux/if_frad.h> 7520380731SArnaldo Carvalho de Melo #include <linux/if_vlan.h> 761da177e4SLinus Torvalds #include <linux/init.h> 771da177e4SLinus Torvalds #include <linux/poll.h> 781da177e4SLinus Torvalds #include <linux/cache.h> 791da177e4SLinus Torvalds #include <linux/module.h> 801da177e4SLinus Torvalds #include <linux/highmem.h> 811da177e4SLinus Torvalds #include <linux/mount.h> 821da177e4SLinus Torvalds #include <linux/security.h> 831da177e4SLinus Torvalds #include <linux/syscalls.h> 841da177e4SLinus Torvalds #include <linux/compat.h> 851da177e4SLinus Torvalds #include <linux/kmod.h> 863ec3b2fbSDavid Woodhouse #include <linux/audit.h> 87d86b5e0eSAdrian Bunk #include <linux/wireless.h> 881b8d7ae4SEric W. Biederman #include <linux/nsproxy.h> 891fd7317dSNick Black #include <linux/magic.h> 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds #include <asm/uaccess.h> 921da177e4SLinus Torvalds #include <asm/unistd.h> 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds #include <net/compat.h> 9587de87d5SDavid S. Miller #include <net/wext.h> 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds #include <net/sock.h> 981da177e4SLinus Torvalds #include <linux/netfilter.h> 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds static int sock_no_open(struct inode *irrelevant, struct file *dontcare); 101027445c3SBadari Pulavarty static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov, 102027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos); 103027445c3SBadari Pulavarty static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov, 104027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos); 1051da177e4SLinus Torvalds static int sock_mmap(struct file *file, struct vm_area_struct *vma); 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds static int sock_close(struct inode *inode, struct file *file); 1081da177e4SLinus Torvalds static unsigned int sock_poll(struct file *file, 1091da177e4SLinus Torvalds struct poll_table_struct *wait); 11089bddce5SStephen Hemminger static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 11189bbfc95SShaun Pereira #ifdef CONFIG_COMPAT 11289bbfc95SShaun Pereira static long compat_sock_ioctl(struct file *file, 11389bbfc95SShaun Pereira unsigned int cmd, unsigned long arg); 11489bbfc95SShaun Pereira #endif 1151da177e4SLinus Torvalds static int sock_fasync(int fd, struct file *filp, int on); 1161da177e4SLinus Torvalds static ssize_t sock_sendpage(struct file *file, struct page *page, 1171da177e4SLinus Torvalds int offset, size_t size, loff_t *ppos, int more); 1189c55e01cSJens Axboe static ssize_t sock_splice_read(struct file *file, loff_t *ppos, 1199c55e01cSJens Axboe struct pipe_inode_info *pipe, size_t len, 1209c55e01cSJens Axboe unsigned int flags); 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds /* 1231da177e4SLinus Torvalds * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear 1241da177e4SLinus Torvalds * in the operation structures but are done directly via the socketcall() multiplexor. 1251da177e4SLinus Torvalds */ 1261da177e4SLinus Torvalds 127da7071d7SArjan van de Ven static const struct file_operations socket_file_ops = { 1281da177e4SLinus Torvalds .owner = THIS_MODULE, 1291da177e4SLinus Torvalds .llseek = no_llseek, 1301da177e4SLinus Torvalds .aio_read = sock_aio_read, 1311da177e4SLinus Torvalds .aio_write = sock_aio_write, 1321da177e4SLinus Torvalds .poll = sock_poll, 1331da177e4SLinus Torvalds .unlocked_ioctl = sock_ioctl, 13489bbfc95SShaun Pereira #ifdef CONFIG_COMPAT 13589bbfc95SShaun Pereira .compat_ioctl = compat_sock_ioctl, 13689bbfc95SShaun Pereira #endif 1371da177e4SLinus Torvalds .mmap = sock_mmap, 1381da177e4SLinus Torvalds .open = sock_no_open, /* special open code to disallow open via /proc */ 1391da177e4SLinus Torvalds .release = sock_close, 1401da177e4SLinus Torvalds .fasync = sock_fasync, 1415274f052SJens Axboe .sendpage = sock_sendpage, 1425274f052SJens Axboe .splice_write = generic_splice_sendpage, 1439c55e01cSJens Axboe .splice_read = sock_splice_read, 1441da177e4SLinus Torvalds }; 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds /* 1471da177e4SLinus Torvalds * The protocol list. Each protocol is registered in here. 1481da177e4SLinus Torvalds */ 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds static DEFINE_SPINLOCK(net_family_lock); 151f0fd27d4SStephen Hemminger static const struct net_proto_family *net_families[NPROTO] __read_mostly; 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds /* 1541da177e4SLinus Torvalds * Statistics counters of the socket lists 1551da177e4SLinus Torvalds */ 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds static DEFINE_PER_CPU(int, sockets_in_use) = 0; 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds /* 16089bddce5SStephen Hemminger * Support routines. 16189bddce5SStephen Hemminger * Move socket addresses back and forth across the kernel/user 1621da177e4SLinus Torvalds * divide and look after the messy bits. 1631da177e4SLinus Torvalds */ 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds #define MAX_SOCK_ADDR 128 /* 108 for Unix domain - 1661da177e4SLinus Torvalds 16 for IP, 16 for IPX, 1671da177e4SLinus Torvalds 24 for IPv6, 1681da177e4SLinus Torvalds about 80 for AX.25 1691da177e4SLinus Torvalds must be at least one bigger than 1701da177e4SLinus Torvalds the AF_UNIX size (see net/unix/af_unix.c 1711da177e4SLinus Torvalds :unix_mkname()). 1721da177e4SLinus Torvalds */ 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds /** 1751da177e4SLinus Torvalds * move_addr_to_kernel - copy a socket address into kernel space 1761da177e4SLinus Torvalds * @uaddr: Address in user space 1771da177e4SLinus Torvalds * @kaddr: Address in kernel space 1781da177e4SLinus Torvalds * @ulen: Length in user space 1791da177e4SLinus Torvalds * 1801da177e4SLinus Torvalds * The address is copied into kernel space. If the provided address is 1811da177e4SLinus Torvalds * too long an error code of -EINVAL is returned. If the copy gives 1821da177e4SLinus Torvalds * invalid addresses -EFAULT is returned. On a success 0 is returned. 1831da177e4SLinus Torvalds */ 1841da177e4SLinus Torvalds 185230b1839SYOSHIFUJI Hideaki int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr *kaddr) 1861da177e4SLinus Torvalds { 187230b1839SYOSHIFUJI Hideaki if (ulen < 0 || ulen > sizeof(struct sockaddr_storage)) 1881da177e4SLinus Torvalds return -EINVAL; 1891da177e4SLinus Torvalds if (ulen == 0) 1901da177e4SLinus Torvalds return 0; 1911da177e4SLinus Torvalds if (copy_from_user(kaddr, uaddr, ulen)) 1921da177e4SLinus Torvalds return -EFAULT; 1933ec3b2fbSDavid Woodhouse return audit_sockaddr(ulen, kaddr); 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds /** 1971da177e4SLinus Torvalds * move_addr_to_user - copy an address to user space 1981da177e4SLinus Torvalds * @kaddr: kernel space address 1991da177e4SLinus Torvalds * @klen: length of address in kernel 2001da177e4SLinus Torvalds * @uaddr: user space address 2011da177e4SLinus Torvalds * @ulen: pointer to user length field 2021da177e4SLinus Torvalds * 2031da177e4SLinus Torvalds * The value pointed to by ulen on entry is the buffer length available. 2041da177e4SLinus Torvalds * This is overwritten with the buffer space used. -EINVAL is returned 2051da177e4SLinus Torvalds * if an overlong buffer is specified or a negative buffer size. -EFAULT 2061da177e4SLinus Torvalds * is returned if either the buffer or the length field are not 2071da177e4SLinus Torvalds * accessible. 2081da177e4SLinus Torvalds * After copying the data up to the limit the user specifies, the true 2091da177e4SLinus Torvalds * length of the data is written over the length limit the user 2101da177e4SLinus Torvalds * specified. Zero is returned for a success. 2111da177e4SLinus Torvalds */ 2121da177e4SLinus Torvalds 213230b1839SYOSHIFUJI Hideaki int move_addr_to_user(struct sockaddr *kaddr, int klen, void __user *uaddr, 21489bddce5SStephen Hemminger int __user *ulen) 2151da177e4SLinus Torvalds { 2161da177e4SLinus Torvalds int err; 2171da177e4SLinus Torvalds int len; 2181da177e4SLinus Torvalds 21989bddce5SStephen Hemminger err = get_user(len, ulen); 22089bddce5SStephen Hemminger if (err) 2211da177e4SLinus Torvalds return err; 2221da177e4SLinus Torvalds if (len > klen) 2231da177e4SLinus Torvalds len = klen; 224230b1839SYOSHIFUJI Hideaki if (len < 0 || len > sizeof(struct sockaddr_storage)) 2251da177e4SLinus Torvalds return -EINVAL; 22689bddce5SStephen Hemminger if (len) { 227d6fe3945SSteve Grubb if (audit_sockaddr(klen, kaddr)) 228d6fe3945SSteve Grubb return -ENOMEM; 2291da177e4SLinus Torvalds if (copy_to_user(uaddr, kaddr, len)) 2301da177e4SLinus Torvalds return -EFAULT; 2311da177e4SLinus Torvalds } 2321da177e4SLinus Torvalds /* 2331da177e4SLinus Torvalds * "fromlen shall refer to the value before truncation.." 2341da177e4SLinus Torvalds * 1003.1g 2351da177e4SLinus Torvalds */ 2361da177e4SLinus Torvalds return __put_user(klen, ulen); 2371da177e4SLinus Torvalds } 2381da177e4SLinus Torvalds 239e18b890bSChristoph Lameter static struct kmem_cache *sock_inode_cachep __read_mostly; 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds static struct inode *sock_alloc_inode(struct super_block *sb) 2421da177e4SLinus Torvalds { 2431da177e4SLinus Torvalds struct socket_alloc *ei; 24489bddce5SStephen Hemminger 245e94b1766SChristoph Lameter ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL); 2461da177e4SLinus Torvalds if (!ei) 2471da177e4SLinus Torvalds return NULL; 2481da177e4SLinus Torvalds init_waitqueue_head(&ei->socket.wait); 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds ei->socket.fasync_list = NULL; 2511da177e4SLinus Torvalds ei->socket.state = SS_UNCONNECTED; 2521da177e4SLinus Torvalds ei->socket.flags = 0; 2531da177e4SLinus Torvalds ei->socket.ops = NULL; 2541da177e4SLinus Torvalds ei->socket.sk = NULL; 2551da177e4SLinus Torvalds ei->socket.file = NULL; 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds return &ei->vfs_inode; 2581da177e4SLinus Torvalds } 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds static void sock_destroy_inode(struct inode *inode) 2611da177e4SLinus Torvalds { 2621da177e4SLinus Torvalds kmem_cache_free(sock_inode_cachep, 2631da177e4SLinus Torvalds container_of(inode, struct socket_alloc, vfs_inode)); 2641da177e4SLinus Torvalds } 2651da177e4SLinus Torvalds 26651cc5068SAlexey Dobriyan static void init_once(void *foo) 2671da177e4SLinus Torvalds { 2681da177e4SLinus Torvalds struct socket_alloc *ei = (struct socket_alloc *)foo; 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds inode_init_once(&ei->vfs_inode); 2711da177e4SLinus Torvalds } 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds static int init_inodecache(void) 2741da177e4SLinus Torvalds { 2751da177e4SLinus Torvalds sock_inode_cachep = kmem_cache_create("sock_inode_cache", 2761da177e4SLinus Torvalds sizeof(struct socket_alloc), 27789bddce5SStephen Hemminger 0, 27889bddce5SStephen Hemminger (SLAB_HWCACHE_ALIGN | 27989bddce5SStephen Hemminger SLAB_RECLAIM_ACCOUNT | 280fffb60f9SPaul Jackson SLAB_MEM_SPREAD), 28120c2df83SPaul Mundt init_once); 2821da177e4SLinus Torvalds if (sock_inode_cachep == NULL) 2831da177e4SLinus Torvalds return -ENOMEM; 2841da177e4SLinus Torvalds return 0; 2851da177e4SLinus Torvalds } 2861da177e4SLinus Torvalds 287b87221deSAlexey Dobriyan static const struct super_operations sockfs_ops = { 2881da177e4SLinus Torvalds .alloc_inode = sock_alloc_inode, 2891da177e4SLinus Torvalds .destroy_inode =sock_destroy_inode, 2901da177e4SLinus Torvalds .statfs = simple_statfs, 2911da177e4SLinus Torvalds }; 2921da177e4SLinus Torvalds 293454e2398SDavid Howells static int sockfs_get_sb(struct file_system_type *fs_type, 29489bddce5SStephen Hemminger int flags, const char *dev_name, void *data, 29589bddce5SStephen Hemminger struct vfsmount *mnt) 2961da177e4SLinus Torvalds { 297454e2398SDavid Howells return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC, 298454e2398SDavid Howells mnt); 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds 301ba89966cSEric Dumazet static struct vfsmount *sock_mnt __read_mostly; 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds static struct file_system_type sock_fs_type = { 3041da177e4SLinus Torvalds .name = "sockfs", 3051da177e4SLinus Torvalds .get_sb = sockfs_get_sb, 3061da177e4SLinus Torvalds .kill_sb = kill_anon_super, 3071da177e4SLinus Torvalds }; 30889bddce5SStephen Hemminger 3091da177e4SLinus Torvalds static int sockfs_delete_dentry(struct dentry *dentry) 3101da177e4SLinus Torvalds { 311304e61e6SEric Dumazet /* 312304e61e6SEric Dumazet * At creation time, we pretended this dentry was hashed 313304e61e6SEric Dumazet * (by clearing DCACHE_UNHASHED bit in d_flags) 314304e61e6SEric Dumazet * At delete time, we restore the truth : not hashed. 315304e61e6SEric Dumazet * (so that dput() can proceed correctly) 316304e61e6SEric Dumazet */ 317304e61e6SEric Dumazet dentry->d_flags |= DCACHE_UNHASHED; 318304e61e6SEric Dumazet return 0; 3191da177e4SLinus Torvalds } 320c23fbb6bSEric Dumazet 321c23fbb6bSEric Dumazet /* 322c23fbb6bSEric Dumazet * sockfs_dname() is called from d_path(). 323c23fbb6bSEric Dumazet */ 324c23fbb6bSEric Dumazet static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen) 325c23fbb6bSEric Dumazet { 326c23fbb6bSEric Dumazet return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]", 327c23fbb6bSEric Dumazet dentry->d_inode->i_ino); 328c23fbb6bSEric Dumazet } 329c23fbb6bSEric Dumazet 3303ba13d17SAl Viro static const struct dentry_operations sockfs_dentry_operations = { 3311da177e4SLinus Torvalds .d_delete = sockfs_delete_dentry, 332c23fbb6bSEric Dumazet .d_dname = sockfs_dname, 3331da177e4SLinus Torvalds }; 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds /* 3361da177e4SLinus Torvalds * Obtains the first available file descriptor and sets it up for use. 3371da177e4SLinus Torvalds * 33839d8c1b6SDavid S. Miller * These functions create file structures and maps them to fd space 33939d8c1b6SDavid S. Miller * of the current process. On success it returns file descriptor 3401da177e4SLinus Torvalds * and file struct implicitly stored in sock->file. 3411da177e4SLinus Torvalds * Note that another thread may close file descriptor before we return 3421da177e4SLinus Torvalds * from this function. We use the fact that now we do not refer 3431da177e4SLinus Torvalds * to socket after mapping. If one day we will need it, this 3441da177e4SLinus Torvalds * function will increment ref. count on file by 1. 3451da177e4SLinus Torvalds * 3461da177e4SLinus Torvalds * In any case returned fd MAY BE not valid! 3471da177e4SLinus Torvalds * This race condition is unavoidable 3481da177e4SLinus Torvalds * with shared fd spaces, we cannot solve it inside kernel, 3491da177e4SLinus Torvalds * but we take care of internal coherence yet. 3501da177e4SLinus Torvalds */ 3511da177e4SLinus Torvalds 352a677a039SUlrich Drepper static int sock_alloc_fd(struct file **filep, int flags) 3531da177e4SLinus Torvalds { 3541da177e4SLinus Torvalds int fd; 3551da177e4SLinus Torvalds 356a677a039SUlrich Drepper fd = get_unused_fd_flags(flags); 35739d8c1b6SDavid S. Miller if (likely(fd >= 0)) { 3581da177e4SLinus Torvalds struct file *file = get_empty_filp(); 3591da177e4SLinus Torvalds 36039d8c1b6SDavid S. Miller *filep = file; 36139d8c1b6SDavid S. Miller if (unlikely(!file)) { 3621da177e4SLinus Torvalds put_unused_fd(fd); 36339d8c1b6SDavid S. Miller return -ENFILE; 3641da177e4SLinus Torvalds } 36539d8c1b6SDavid S. Miller } else 36639d8c1b6SDavid S. Miller *filep = NULL; 36739d8c1b6SDavid S. Miller return fd; 36839d8c1b6SDavid S. Miller } 36939d8c1b6SDavid S. Miller 37077d27200SUlrich Drepper static int sock_attach_fd(struct socket *sock, struct file *file, int flags) 37139d8c1b6SDavid S. Miller { 372ce8d2cdfSDave Hansen struct dentry *dentry; 373c23fbb6bSEric Dumazet struct qstr name = { .name = "" }; 3741da177e4SLinus Torvalds 375ce8d2cdfSDave Hansen dentry = d_alloc(sock_mnt->mnt_sb->s_root, &name); 376ce8d2cdfSDave Hansen if (unlikely(!dentry)) 37739d8c1b6SDavid S. Miller return -ENOMEM; 37839d8c1b6SDavid S. Miller 379ce8d2cdfSDave Hansen dentry->d_op = &sockfs_dentry_operations; 380304e61e6SEric Dumazet /* 381304e61e6SEric Dumazet * We dont want to push this dentry into global dentry hash table. 382304e61e6SEric Dumazet * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED 383304e61e6SEric Dumazet * This permits a working /proc/$pid/fd/XXX on sockets 384304e61e6SEric Dumazet */ 385ce8d2cdfSDave Hansen dentry->d_flags &= ~DCACHE_UNHASHED; 386ce8d2cdfSDave Hansen d_instantiate(dentry, SOCK_INODE(sock)); 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds sock->file = file; 389ce8d2cdfSDave Hansen init_file(file, sock_mnt, dentry, FMODE_READ | FMODE_WRITE, 390ce8d2cdfSDave Hansen &socket_file_ops); 391ce8d2cdfSDave Hansen SOCK_INODE(sock)->i_fop = &socket_file_ops; 39277d27200SUlrich Drepper file->f_flags = O_RDWR | (flags & O_NONBLOCK); 3931da177e4SLinus Torvalds file->f_pos = 0; 39407dc3f07SBenjamin LaHaise file->private_data = sock; 39539d8c1b6SDavid S. Miller 39639d8c1b6SDavid S. Miller return 0; 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds 399a677a039SUlrich Drepper int sock_map_fd(struct socket *sock, int flags) 40039d8c1b6SDavid S. Miller { 40139d8c1b6SDavid S. Miller struct file *newfile; 402a677a039SUlrich Drepper int fd = sock_alloc_fd(&newfile, flags); 40339d8c1b6SDavid S. Miller 40439d8c1b6SDavid S. Miller if (likely(fd >= 0)) { 40577d27200SUlrich Drepper int err = sock_attach_fd(sock, newfile, flags); 40639d8c1b6SDavid S. Miller 40739d8c1b6SDavid S. Miller if (unlikely(err < 0)) { 40839d8c1b6SDavid S. Miller put_filp(newfile); 40939d8c1b6SDavid S. Miller put_unused_fd(fd); 41039d8c1b6SDavid S. Miller return err; 41139d8c1b6SDavid S. Miller } 41239d8c1b6SDavid S. Miller fd_install(fd, newfile); 41339d8c1b6SDavid S. Miller } 4141da177e4SLinus Torvalds return fd; 4151da177e4SLinus Torvalds } 4161da177e4SLinus Torvalds 4176cb153caSBenjamin LaHaise static struct socket *sock_from_file(struct file *file, int *err) 4186cb153caSBenjamin LaHaise { 4196cb153caSBenjamin LaHaise if (file->f_op == &socket_file_ops) 4206cb153caSBenjamin LaHaise return file->private_data; /* set in sock_map_fd */ 4216cb153caSBenjamin LaHaise 4226cb153caSBenjamin LaHaise *err = -ENOTSOCK; 4236cb153caSBenjamin LaHaise return NULL; 4246cb153caSBenjamin LaHaise } 4256cb153caSBenjamin LaHaise 4261da177e4SLinus Torvalds /** 4271da177e4SLinus Torvalds * sockfd_lookup - Go from a file number to its socket slot 4281da177e4SLinus Torvalds * @fd: file handle 4291da177e4SLinus Torvalds * @err: pointer to an error code return 4301da177e4SLinus Torvalds * 4311da177e4SLinus Torvalds * The file handle passed in is locked and the socket it is bound 4321da177e4SLinus Torvalds * too is returned. If an error occurs the err pointer is overwritten 4331da177e4SLinus Torvalds * with a negative errno code and NULL is returned. The function checks 4341da177e4SLinus Torvalds * for both invalid handles and passing a handle which is not a socket. 4351da177e4SLinus Torvalds * 4361da177e4SLinus Torvalds * On a success the socket object pointer is returned. 4371da177e4SLinus Torvalds */ 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds struct socket *sockfd_lookup(int fd, int *err) 4401da177e4SLinus Torvalds { 4411da177e4SLinus Torvalds struct file *file; 4421da177e4SLinus Torvalds struct socket *sock; 4431da177e4SLinus Torvalds 44489bddce5SStephen Hemminger file = fget(fd); 44589bddce5SStephen Hemminger if (!file) { 4461da177e4SLinus Torvalds *err = -EBADF; 4471da177e4SLinus Torvalds return NULL; 4481da177e4SLinus Torvalds } 44989bddce5SStephen Hemminger 4506cb153caSBenjamin LaHaise sock = sock_from_file(file, err); 4516cb153caSBenjamin LaHaise if (!sock) 4521da177e4SLinus Torvalds fput(file); 4536cb153caSBenjamin LaHaise return sock; 4541da177e4SLinus Torvalds } 4551da177e4SLinus Torvalds 4566cb153caSBenjamin LaHaise static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed) 4576cb153caSBenjamin LaHaise { 4586cb153caSBenjamin LaHaise struct file *file; 4596cb153caSBenjamin LaHaise struct socket *sock; 4606cb153caSBenjamin LaHaise 4613672558cSHua Zhong *err = -EBADF; 4626cb153caSBenjamin LaHaise file = fget_light(fd, fput_needed); 4636cb153caSBenjamin LaHaise if (file) { 4646cb153caSBenjamin LaHaise sock = sock_from_file(file, err); 4656cb153caSBenjamin LaHaise if (sock) 4661da177e4SLinus Torvalds return sock; 4676cb153caSBenjamin LaHaise fput_light(file, *fput_needed); 4686cb153caSBenjamin LaHaise } 4696cb153caSBenjamin LaHaise return NULL; 4701da177e4SLinus Torvalds } 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds /** 4731da177e4SLinus Torvalds * sock_alloc - allocate a socket 4741da177e4SLinus Torvalds * 4751da177e4SLinus Torvalds * Allocate a new inode and socket object. The two are bound together 4761da177e4SLinus Torvalds * and initialised. The socket is then returned. If we are out of inodes 4771da177e4SLinus Torvalds * NULL is returned. 4781da177e4SLinus Torvalds */ 4791da177e4SLinus Torvalds 4801da177e4SLinus Torvalds static struct socket *sock_alloc(void) 4811da177e4SLinus Torvalds { 4821da177e4SLinus Torvalds struct inode *inode; 4831da177e4SLinus Torvalds struct socket *sock; 4841da177e4SLinus Torvalds 4851da177e4SLinus Torvalds inode = new_inode(sock_mnt->mnt_sb); 4861da177e4SLinus Torvalds if (!inode) 4871da177e4SLinus Torvalds return NULL; 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds sock = SOCKET_I(inode); 4901da177e4SLinus Torvalds 49129a020d3SEric Dumazet kmemcheck_annotate_bitfield(sock, type); 4921da177e4SLinus Torvalds inode->i_mode = S_IFSOCK | S_IRWXUGO; 4938192b0c4SDavid Howells inode->i_uid = current_fsuid(); 4948192b0c4SDavid Howells inode->i_gid = current_fsgid(); 4951da177e4SLinus Torvalds 4964e69489aSEric Dumazet percpu_add(sockets_in_use, 1); 4971da177e4SLinus Torvalds return sock; 4981da177e4SLinus Torvalds } 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds /* 5011da177e4SLinus Torvalds * In theory you can't get an open on this inode, but /proc provides 5021da177e4SLinus Torvalds * a back door. Remember to keep it shut otherwise you'll let the 5031da177e4SLinus Torvalds * creepy crawlies in. 5041da177e4SLinus Torvalds */ 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds static int sock_no_open(struct inode *irrelevant, struct file *dontcare) 5071da177e4SLinus Torvalds { 5081da177e4SLinus Torvalds return -ENXIO; 5091da177e4SLinus Torvalds } 5101da177e4SLinus Torvalds 5114b6f5d20SArjan van de Ven const struct file_operations bad_sock_fops = { 5121da177e4SLinus Torvalds .owner = THIS_MODULE, 5131da177e4SLinus Torvalds .open = sock_no_open, 5141da177e4SLinus Torvalds }; 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds /** 5171da177e4SLinus Torvalds * sock_release - close a socket 5181da177e4SLinus Torvalds * @sock: socket to close 5191da177e4SLinus Torvalds * 5201da177e4SLinus Torvalds * The socket is released from the protocol stack if it has a release 5211da177e4SLinus Torvalds * callback, and the inode is then released if the socket is bound to 5221da177e4SLinus Torvalds * an inode not a file. 5231da177e4SLinus Torvalds */ 5241da177e4SLinus Torvalds 5251da177e4SLinus Torvalds void sock_release(struct socket *sock) 5261da177e4SLinus Torvalds { 5271da177e4SLinus Torvalds if (sock->ops) { 5281da177e4SLinus Torvalds struct module *owner = sock->ops->owner; 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds sock->ops->release(sock); 5311da177e4SLinus Torvalds sock->ops = NULL; 5321da177e4SLinus Torvalds module_put(owner); 5331da177e4SLinus Torvalds } 5341da177e4SLinus Torvalds 5351da177e4SLinus Torvalds if (sock->fasync_list) 5361da177e4SLinus Torvalds printk(KERN_ERR "sock_release: fasync list not empty!\n"); 5371da177e4SLinus Torvalds 5384e69489aSEric Dumazet percpu_sub(sockets_in_use, 1); 5391da177e4SLinus Torvalds if (!sock->file) { 5401da177e4SLinus Torvalds iput(SOCK_INODE(sock)); 5411da177e4SLinus Torvalds return; 5421da177e4SLinus Torvalds } 5431da177e4SLinus Torvalds sock->file = NULL; 5441da177e4SLinus Torvalds } 5451da177e4SLinus Torvalds 54620d49473SPatrick Ohly int sock_tx_timestamp(struct msghdr *msg, struct sock *sk, 54720d49473SPatrick Ohly union skb_shared_tx *shtx) 54820d49473SPatrick Ohly { 54920d49473SPatrick Ohly shtx->flags = 0; 55020d49473SPatrick Ohly if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE)) 55120d49473SPatrick Ohly shtx->hardware = 1; 55220d49473SPatrick Ohly if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE)) 55320d49473SPatrick Ohly shtx->software = 1; 55420d49473SPatrick Ohly return 0; 55520d49473SPatrick Ohly } 55620d49473SPatrick Ohly EXPORT_SYMBOL(sock_tx_timestamp); 55720d49473SPatrick Ohly 5581da177e4SLinus Torvalds static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock, 5591da177e4SLinus Torvalds struct msghdr *msg, size_t size) 5601da177e4SLinus Torvalds { 5611da177e4SLinus Torvalds struct sock_iocb *si = kiocb_to_siocb(iocb); 5621da177e4SLinus Torvalds int err; 5631da177e4SLinus Torvalds 5641da177e4SLinus Torvalds si->sock = sock; 5651da177e4SLinus Torvalds si->scm = NULL; 5661da177e4SLinus Torvalds si->msg = msg; 5671da177e4SLinus Torvalds si->size = size; 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds err = security_socket_sendmsg(sock, msg, size); 5701da177e4SLinus Torvalds if (err) 5711da177e4SLinus Torvalds return err; 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds return sock->ops->sendmsg(iocb, sock, msg, size); 5741da177e4SLinus Torvalds } 5751da177e4SLinus Torvalds 5761da177e4SLinus Torvalds int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) 5771da177e4SLinus Torvalds { 5781da177e4SLinus Torvalds struct kiocb iocb; 5791da177e4SLinus Torvalds struct sock_iocb siocb; 5801da177e4SLinus Torvalds int ret; 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds init_sync_kiocb(&iocb, NULL); 5831da177e4SLinus Torvalds iocb.private = &siocb; 5841da177e4SLinus Torvalds ret = __sock_sendmsg(&iocb, sock, msg, size); 5851da177e4SLinus Torvalds if (-EIOCBQUEUED == ret) 5861da177e4SLinus Torvalds ret = wait_on_sync_kiocb(&iocb); 5871da177e4SLinus Torvalds return ret; 5881da177e4SLinus Torvalds } 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds int kernel_sendmsg(struct socket *sock, struct msghdr *msg, 5911da177e4SLinus Torvalds struct kvec *vec, size_t num, size_t size) 5921da177e4SLinus Torvalds { 5931da177e4SLinus Torvalds mm_segment_t oldfs = get_fs(); 5941da177e4SLinus Torvalds int result; 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds set_fs(KERNEL_DS); 5971da177e4SLinus Torvalds /* 5981da177e4SLinus Torvalds * the following is safe, since for compiler definitions of kvec and 5991da177e4SLinus Torvalds * iovec are identical, yielding the same in-core layout and alignment 6001da177e4SLinus Torvalds */ 60189bddce5SStephen Hemminger msg->msg_iov = (struct iovec *)vec; 6021da177e4SLinus Torvalds msg->msg_iovlen = num; 6031da177e4SLinus Torvalds result = sock_sendmsg(sock, msg, size); 6041da177e4SLinus Torvalds set_fs(oldfs); 6051da177e4SLinus Torvalds return result; 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds 60820d49473SPatrick Ohly static int ktime2ts(ktime_t kt, struct timespec *ts) 60920d49473SPatrick Ohly { 61020d49473SPatrick Ohly if (kt.tv64) { 61120d49473SPatrick Ohly *ts = ktime_to_timespec(kt); 61220d49473SPatrick Ohly return 1; 61320d49473SPatrick Ohly } else { 61420d49473SPatrick Ohly return 0; 61520d49473SPatrick Ohly } 61620d49473SPatrick Ohly } 61720d49473SPatrick Ohly 61892f37fd2SEric Dumazet /* 61992f37fd2SEric Dumazet * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP) 62092f37fd2SEric Dumazet */ 62192f37fd2SEric Dumazet void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk, 62292f37fd2SEric Dumazet struct sk_buff *skb) 62392f37fd2SEric Dumazet { 62420d49473SPatrick Ohly int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP); 62520d49473SPatrick Ohly struct timespec ts[3]; 62620d49473SPatrick Ohly int empty = 1; 62720d49473SPatrick Ohly struct skb_shared_hwtstamps *shhwtstamps = 62820d49473SPatrick Ohly skb_hwtstamps(skb); 62992f37fd2SEric Dumazet 63020d49473SPatrick Ohly /* Race occurred between timestamp enabling and packet 63120d49473SPatrick Ohly receiving. Fill in the current time for now. */ 63220d49473SPatrick Ohly if (need_software_tstamp && skb->tstamp.tv64 == 0) 63320d49473SPatrick Ohly __net_timestamp(skb); 63420d49473SPatrick Ohly 63520d49473SPatrick Ohly if (need_software_tstamp) { 63692f37fd2SEric Dumazet if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) { 63792f37fd2SEric Dumazet struct timeval tv; 63820d49473SPatrick Ohly skb_get_timestamp(skb, &tv); 63920d49473SPatrick Ohly put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP, 64020d49473SPatrick Ohly sizeof(tv), &tv); 64192f37fd2SEric Dumazet } else { 64292f37fd2SEric Dumazet struct timespec ts; 64320d49473SPatrick Ohly skb_get_timestampns(skb, &ts); 64420d49473SPatrick Ohly put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS, 64520d49473SPatrick Ohly sizeof(ts), &ts); 64692f37fd2SEric Dumazet } 64792f37fd2SEric Dumazet } 64892f37fd2SEric Dumazet 64920d49473SPatrick Ohly 65020d49473SPatrick Ohly memset(ts, 0, sizeof(ts)); 65120d49473SPatrick Ohly if (skb->tstamp.tv64 && 65220d49473SPatrick Ohly sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE)) { 65320d49473SPatrick Ohly skb_get_timestampns(skb, ts + 0); 65420d49473SPatrick Ohly empty = 0; 65520d49473SPatrick Ohly } 65620d49473SPatrick Ohly if (shhwtstamps) { 65720d49473SPatrick Ohly if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) && 65820d49473SPatrick Ohly ktime2ts(shhwtstamps->syststamp, ts + 1)) 65920d49473SPatrick Ohly empty = 0; 66020d49473SPatrick Ohly if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) && 66120d49473SPatrick Ohly ktime2ts(shhwtstamps->hwtstamp, ts + 2)) 66220d49473SPatrick Ohly empty = 0; 66320d49473SPatrick Ohly } 66420d49473SPatrick Ohly if (!empty) 66520d49473SPatrick Ohly put_cmsg(msg, SOL_SOCKET, 66620d49473SPatrick Ohly SCM_TIMESTAMPING, sizeof(ts), &ts); 66720d49473SPatrick Ohly } 66820d49473SPatrick Ohly 6697c81fd8bSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(__sock_recv_timestamp); 6707c81fd8bSArnaldo Carvalho de Melo 6713b885787SNeil Horman inline void sock_recv_drops(struct msghdr *msg, struct sock *sk, struct sk_buff *skb) 6723b885787SNeil Horman { 6733b885787SNeil Horman if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && skb->dropcount) 6743b885787SNeil Horman put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL, 6753b885787SNeil Horman sizeof(__u32), &skb->dropcount); 6763b885787SNeil Horman } 6773b885787SNeil Horman 6783b885787SNeil Horman void sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk, 6793b885787SNeil Horman struct sk_buff *skb) 6803b885787SNeil Horman { 6813b885787SNeil Horman sock_recv_timestamp(msg, sk, skb); 6823b885787SNeil Horman sock_recv_drops(msg, sk, skb); 6833b885787SNeil Horman } 6843b885787SNeil Horman EXPORT_SYMBOL_GPL(sock_recv_ts_and_drops); 6853b885787SNeil Horman 6861da177e4SLinus Torvalds static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock, 6871da177e4SLinus Torvalds struct msghdr *msg, size_t size, int flags) 6881da177e4SLinus Torvalds { 6891da177e4SLinus Torvalds int err; 6901da177e4SLinus Torvalds struct sock_iocb *si = kiocb_to_siocb(iocb); 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds si->sock = sock; 6931da177e4SLinus Torvalds si->scm = NULL; 6941da177e4SLinus Torvalds si->msg = msg; 6951da177e4SLinus Torvalds si->size = size; 6961da177e4SLinus Torvalds si->flags = flags; 6971da177e4SLinus Torvalds 6981da177e4SLinus Torvalds err = security_socket_recvmsg(sock, msg, size, flags); 6991da177e4SLinus Torvalds if (err) 7001da177e4SLinus Torvalds return err; 7011da177e4SLinus Torvalds 7021da177e4SLinus Torvalds return sock->ops->recvmsg(iocb, sock, msg, size, flags); 7031da177e4SLinus Torvalds } 7041da177e4SLinus Torvalds 7051da177e4SLinus Torvalds int sock_recvmsg(struct socket *sock, struct msghdr *msg, 7061da177e4SLinus Torvalds size_t size, int flags) 7071da177e4SLinus Torvalds { 7081da177e4SLinus Torvalds struct kiocb iocb; 7091da177e4SLinus Torvalds struct sock_iocb siocb; 7101da177e4SLinus Torvalds int ret; 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds init_sync_kiocb(&iocb, NULL); 7131da177e4SLinus Torvalds iocb.private = &siocb; 7141da177e4SLinus Torvalds ret = __sock_recvmsg(&iocb, sock, msg, size, flags); 7151da177e4SLinus Torvalds if (-EIOCBQUEUED == ret) 7161da177e4SLinus Torvalds ret = wait_on_sync_kiocb(&iocb); 7171da177e4SLinus Torvalds return ret; 7181da177e4SLinus Torvalds } 7191da177e4SLinus Torvalds 7201da177e4SLinus Torvalds int kernel_recvmsg(struct socket *sock, struct msghdr *msg, 72189bddce5SStephen Hemminger struct kvec *vec, size_t num, size_t size, int flags) 7221da177e4SLinus Torvalds { 7231da177e4SLinus Torvalds mm_segment_t oldfs = get_fs(); 7241da177e4SLinus Torvalds int result; 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds set_fs(KERNEL_DS); 7271da177e4SLinus Torvalds /* 7281da177e4SLinus Torvalds * the following is safe, since for compiler definitions of kvec and 7291da177e4SLinus Torvalds * iovec are identical, yielding the same in-core layout and alignment 7301da177e4SLinus Torvalds */ 73189bddce5SStephen Hemminger msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num; 7321da177e4SLinus Torvalds result = sock_recvmsg(sock, msg, size, flags); 7331da177e4SLinus Torvalds set_fs(oldfs); 7341da177e4SLinus Torvalds return result; 7351da177e4SLinus Torvalds } 7361da177e4SLinus Torvalds 7371da177e4SLinus Torvalds static void sock_aio_dtor(struct kiocb *iocb) 7381da177e4SLinus Torvalds { 7391da177e4SLinus Torvalds kfree(iocb->private); 7401da177e4SLinus Torvalds } 7411da177e4SLinus Torvalds 74220380731SArnaldo Carvalho de Melo static ssize_t sock_sendpage(struct file *file, struct page *page, 7431da177e4SLinus Torvalds int offset, size_t size, loff_t *ppos, int more) 7441da177e4SLinus Torvalds { 7451da177e4SLinus Torvalds struct socket *sock; 7461da177e4SLinus Torvalds int flags; 7471da177e4SLinus Torvalds 748b69aee04SEric Dumazet sock = file->private_data; 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT; 7511da177e4SLinus Torvalds if (more) 7521da177e4SLinus Torvalds flags |= MSG_MORE; 7531da177e4SLinus Torvalds 754e6949583SLinus Torvalds return kernel_sendpage(sock, page, offset, size, flags); 7551da177e4SLinus Torvalds } 7561da177e4SLinus Torvalds 7579c55e01cSJens Axboe static ssize_t sock_splice_read(struct file *file, loff_t *ppos, 7589c55e01cSJens Axboe struct pipe_inode_info *pipe, size_t len, 7599c55e01cSJens Axboe unsigned int flags) 7609c55e01cSJens Axboe { 7619c55e01cSJens Axboe struct socket *sock = file->private_data; 7629c55e01cSJens Axboe 763997b37daSRémi Denis-Courmont if (unlikely(!sock->ops->splice_read)) 764997b37daSRémi Denis-Courmont return -EINVAL; 765997b37daSRémi Denis-Courmont 7669c55e01cSJens Axboe return sock->ops->splice_read(sock, ppos, pipe, len, flags); 7679c55e01cSJens Axboe } 7689c55e01cSJens Axboe 769ce1d4d3eSChristoph Hellwig static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb, 77089bddce5SStephen Hemminger struct sock_iocb *siocb) 771ce1d4d3eSChristoph Hellwig { 772ce1d4d3eSChristoph Hellwig if (!is_sync_kiocb(iocb)) { 773ce1d4d3eSChristoph Hellwig siocb = kmalloc(sizeof(*siocb), GFP_KERNEL); 774ce1d4d3eSChristoph Hellwig if (!siocb) 775ce1d4d3eSChristoph Hellwig return NULL; 776ce1d4d3eSChristoph Hellwig iocb->ki_dtor = sock_aio_dtor; 777ce1d4d3eSChristoph Hellwig } 778ce1d4d3eSChristoph Hellwig 779ce1d4d3eSChristoph Hellwig siocb->kiocb = iocb; 780ce1d4d3eSChristoph Hellwig iocb->private = siocb; 781ce1d4d3eSChristoph Hellwig return siocb; 782ce1d4d3eSChristoph Hellwig } 783ce1d4d3eSChristoph Hellwig 784ce1d4d3eSChristoph Hellwig static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb, 785027445c3SBadari Pulavarty struct file *file, const struct iovec *iov, 78689bddce5SStephen Hemminger unsigned long nr_segs) 787ce1d4d3eSChristoph Hellwig { 788ce1d4d3eSChristoph Hellwig struct socket *sock = file->private_data; 789ce1d4d3eSChristoph Hellwig size_t size = 0; 790ce1d4d3eSChristoph Hellwig int i; 791ce1d4d3eSChristoph Hellwig 792ce1d4d3eSChristoph Hellwig for (i = 0; i < nr_segs; i++) 793ce1d4d3eSChristoph Hellwig size += iov[i].iov_len; 794ce1d4d3eSChristoph Hellwig 795ce1d4d3eSChristoph Hellwig msg->msg_name = NULL; 796ce1d4d3eSChristoph Hellwig msg->msg_namelen = 0; 797ce1d4d3eSChristoph Hellwig msg->msg_control = NULL; 798ce1d4d3eSChristoph Hellwig msg->msg_controllen = 0; 799ce1d4d3eSChristoph Hellwig msg->msg_iov = (struct iovec *)iov; 800ce1d4d3eSChristoph Hellwig msg->msg_iovlen = nr_segs; 801ce1d4d3eSChristoph Hellwig msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0; 802ce1d4d3eSChristoph Hellwig 803ce1d4d3eSChristoph Hellwig return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags); 804ce1d4d3eSChristoph Hellwig } 805ce1d4d3eSChristoph Hellwig 806027445c3SBadari Pulavarty static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov, 807027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 808ce1d4d3eSChristoph Hellwig { 809ce1d4d3eSChristoph Hellwig struct sock_iocb siocb, *x; 810ce1d4d3eSChristoph Hellwig 811ce1d4d3eSChristoph Hellwig if (pos != 0) 812ce1d4d3eSChristoph Hellwig return -ESPIPE; 813027445c3SBadari Pulavarty 814027445c3SBadari Pulavarty if (iocb->ki_left == 0) /* Match SYS5 behaviour */ 815ce1d4d3eSChristoph Hellwig return 0; 816ce1d4d3eSChristoph Hellwig 817027445c3SBadari Pulavarty 818027445c3SBadari Pulavarty x = alloc_sock_iocb(iocb, &siocb); 819ce1d4d3eSChristoph Hellwig if (!x) 820ce1d4d3eSChristoph Hellwig return -ENOMEM; 821027445c3SBadari Pulavarty return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs); 822ce1d4d3eSChristoph Hellwig } 823ce1d4d3eSChristoph Hellwig 824ce1d4d3eSChristoph Hellwig static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb, 825027445c3SBadari Pulavarty struct file *file, const struct iovec *iov, 82689bddce5SStephen Hemminger unsigned long nr_segs) 827ce1d4d3eSChristoph Hellwig { 828ce1d4d3eSChristoph Hellwig struct socket *sock = file->private_data; 829ce1d4d3eSChristoph Hellwig size_t size = 0; 830ce1d4d3eSChristoph Hellwig int i; 831ce1d4d3eSChristoph Hellwig 832ce1d4d3eSChristoph Hellwig for (i = 0; i < nr_segs; i++) 833ce1d4d3eSChristoph Hellwig size += iov[i].iov_len; 834ce1d4d3eSChristoph Hellwig 835ce1d4d3eSChristoph Hellwig msg->msg_name = NULL; 836ce1d4d3eSChristoph Hellwig msg->msg_namelen = 0; 837ce1d4d3eSChristoph Hellwig msg->msg_control = NULL; 838ce1d4d3eSChristoph Hellwig msg->msg_controllen = 0; 839ce1d4d3eSChristoph Hellwig msg->msg_iov = (struct iovec *)iov; 840ce1d4d3eSChristoph Hellwig msg->msg_iovlen = nr_segs; 841ce1d4d3eSChristoph Hellwig msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0; 842ce1d4d3eSChristoph Hellwig if (sock->type == SOCK_SEQPACKET) 843ce1d4d3eSChristoph Hellwig msg->msg_flags |= MSG_EOR; 844ce1d4d3eSChristoph Hellwig 845ce1d4d3eSChristoph Hellwig return __sock_sendmsg(iocb, sock, msg, size); 846ce1d4d3eSChristoph Hellwig } 847ce1d4d3eSChristoph Hellwig 848027445c3SBadari Pulavarty static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov, 849027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 8501da177e4SLinus Torvalds { 851ce1d4d3eSChristoph Hellwig struct sock_iocb siocb, *x; 8521da177e4SLinus Torvalds 853ce1d4d3eSChristoph Hellwig if (pos != 0) 854ce1d4d3eSChristoph Hellwig return -ESPIPE; 855027445c3SBadari Pulavarty 856027445c3SBadari Pulavarty x = alloc_sock_iocb(iocb, &siocb); 857ce1d4d3eSChristoph Hellwig if (!x) 858ce1d4d3eSChristoph Hellwig return -ENOMEM; 859ce1d4d3eSChristoph Hellwig 860027445c3SBadari Pulavarty return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs); 8611da177e4SLinus Torvalds } 8621da177e4SLinus Torvalds 8631da177e4SLinus Torvalds /* 8641da177e4SLinus Torvalds * Atomic setting of ioctl hooks to avoid race 8651da177e4SLinus Torvalds * with module unload. 8661da177e4SLinus Torvalds */ 8671da177e4SLinus Torvalds 8684a3e2f71SArjan van de Ven static DEFINE_MUTEX(br_ioctl_mutex); 869881d966bSEric W. Biederman static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg) = NULL; 8701da177e4SLinus Torvalds 871881d966bSEric W. Biederman void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *)) 8721da177e4SLinus Torvalds { 8734a3e2f71SArjan van de Ven mutex_lock(&br_ioctl_mutex); 8741da177e4SLinus Torvalds br_ioctl_hook = hook; 8754a3e2f71SArjan van de Ven mutex_unlock(&br_ioctl_mutex); 8761da177e4SLinus Torvalds } 87789bddce5SStephen Hemminger 8781da177e4SLinus Torvalds EXPORT_SYMBOL(brioctl_set); 8791da177e4SLinus Torvalds 8804a3e2f71SArjan van de Ven static DEFINE_MUTEX(vlan_ioctl_mutex); 881881d966bSEric W. Biederman static int (*vlan_ioctl_hook) (struct net *, void __user *arg); 8821da177e4SLinus Torvalds 883881d966bSEric W. Biederman void vlan_ioctl_set(int (*hook) (struct net *, void __user *)) 8841da177e4SLinus Torvalds { 8854a3e2f71SArjan van de Ven mutex_lock(&vlan_ioctl_mutex); 8861da177e4SLinus Torvalds vlan_ioctl_hook = hook; 8874a3e2f71SArjan van de Ven mutex_unlock(&vlan_ioctl_mutex); 8881da177e4SLinus Torvalds } 88989bddce5SStephen Hemminger 8901da177e4SLinus Torvalds EXPORT_SYMBOL(vlan_ioctl_set); 8911da177e4SLinus Torvalds 8924a3e2f71SArjan van de Ven static DEFINE_MUTEX(dlci_ioctl_mutex); 8931da177e4SLinus Torvalds static int (*dlci_ioctl_hook) (unsigned int, void __user *); 8941da177e4SLinus Torvalds 8951da177e4SLinus Torvalds void dlci_ioctl_set(int (*hook) (unsigned int, void __user *)) 8961da177e4SLinus Torvalds { 8974a3e2f71SArjan van de Ven mutex_lock(&dlci_ioctl_mutex); 8981da177e4SLinus Torvalds dlci_ioctl_hook = hook; 8994a3e2f71SArjan van de Ven mutex_unlock(&dlci_ioctl_mutex); 9001da177e4SLinus Torvalds } 90189bddce5SStephen Hemminger 9021da177e4SLinus Torvalds EXPORT_SYMBOL(dlci_ioctl_set); 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds /* 9051da177e4SLinus Torvalds * With an ioctl, arg may well be a user mode pointer, but we don't know 9061da177e4SLinus Torvalds * what to do with it - that's up to the protocol still. 9071da177e4SLinus Torvalds */ 9081da177e4SLinus Torvalds 9091da177e4SLinus Torvalds static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg) 9101da177e4SLinus Torvalds { 9111da177e4SLinus Torvalds struct socket *sock; 912881d966bSEric W. Biederman struct sock *sk; 9131da177e4SLinus Torvalds void __user *argp = (void __user *)arg; 9141da177e4SLinus Torvalds int pid, err; 915881d966bSEric W. Biederman struct net *net; 9161da177e4SLinus Torvalds 917b69aee04SEric Dumazet sock = file->private_data; 918881d966bSEric W. Biederman sk = sock->sk; 9193b1e0a65SYOSHIFUJI Hideaki net = sock_net(sk); 9201da177e4SLinus Torvalds if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) { 921881d966bSEric W. Biederman err = dev_ioctl(net, cmd, argp); 9221da177e4SLinus Torvalds } else 9233d23e349SJohannes Berg #ifdef CONFIG_WEXT_CORE 9241da177e4SLinus Torvalds if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) { 925881d966bSEric W. Biederman err = dev_ioctl(net, cmd, argp); 9261da177e4SLinus Torvalds } else 9273d23e349SJohannes Berg #endif 9281da177e4SLinus Torvalds switch (cmd) { 9291da177e4SLinus Torvalds case FIOSETOWN: 9301da177e4SLinus Torvalds case SIOCSPGRP: 9311da177e4SLinus Torvalds err = -EFAULT; 9321da177e4SLinus Torvalds if (get_user(pid, (int __user *)argp)) 9331da177e4SLinus Torvalds break; 9341da177e4SLinus Torvalds err = f_setown(sock->file, pid, 1); 9351da177e4SLinus Torvalds break; 9361da177e4SLinus Torvalds case FIOGETOWN: 9371da177e4SLinus Torvalds case SIOCGPGRP: 938609d7fa9SEric W. Biederman err = put_user(f_getown(sock->file), 93989bddce5SStephen Hemminger (int __user *)argp); 9401da177e4SLinus Torvalds break; 9411da177e4SLinus Torvalds case SIOCGIFBR: 9421da177e4SLinus Torvalds case SIOCSIFBR: 9431da177e4SLinus Torvalds case SIOCBRADDBR: 9441da177e4SLinus Torvalds case SIOCBRDELBR: 9451da177e4SLinus Torvalds err = -ENOPKG; 9461da177e4SLinus Torvalds if (!br_ioctl_hook) 9471da177e4SLinus Torvalds request_module("bridge"); 9481da177e4SLinus Torvalds 9494a3e2f71SArjan van de Ven mutex_lock(&br_ioctl_mutex); 9501da177e4SLinus Torvalds if (br_ioctl_hook) 951881d966bSEric W. Biederman err = br_ioctl_hook(net, cmd, argp); 9524a3e2f71SArjan van de Ven mutex_unlock(&br_ioctl_mutex); 9531da177e4SLinus Torvalds break; 9541da177e4SLinus Torvalds case SIOCGIFVLAN: 9551da177e4SLinus Torvalds case SIOCSIFVLAN: 9561da177e4SLinus Torvalds err = -ENOPKG; 9571da177e4SLinus Torvalds if (!vlan_ioctl_hook) 9581da177e4SLinus Torvalds request_module("8021q"); 9591da177e4SLinus Torvalds 9604a3e2f71SArjan van de Ven mutex_lock(&vlan_ioctl_mutex); 9611da177e4SLinus Torvalds if (vlan_ioctl_hook) 962881d966bSEric W. Biederman err = vlan_ioctl_hook(net, argp); 9634a3e2f71SArjan van de Ven mutex_unlock(&vlan_ioctl_mutex); 9641da177e4SLinus Torvalds break; 9651da177e4SLinus Torvalds case SIOCADDDLCI: 9661da177e4SLinus Torvalds case SIOCDELDLCI: 9671da177e4SLinus Torvalds err = -ENOPKG; 9681da177e4SLinus Torvalds if (!dlci_ioctl_hook) 9691da177e4SLinus Torvalds request_module("dlci"); 9701da177e4SLinus Torvalds 9714a3e2f71SArjan van de Ven mutex_lock(&dlci_ioctl_mutex); 9727512cbf6SPavel Emelyanov if (dlci_ioctl_hook) 9731da177e4SLinus Torvalds err = dlci_ioctl_hook(cmd, argp); 9744a3e2f71SArjan van de Ven mutex_unlock(&dlci_ioctl_mutex); 9751da177e4SLinus Torvalds break; 9761da177e4SLinus Torvalds default: 9771da177e4SLinus Torvalds err = sock->ops->ioctl(sock, cmd, arg); 978b5e5fa5eSChristoph Hellwig 979b5e5fa5eSChristoph Hellwig /* 980b5e5fa5eSChristoph Hellwig * If this ioctl is unknown try to hand it down 981b5e5fa5eSChristoph Hellwig * to the NIC driver. 982b5e5fa5eSChristoph Hellwig */ 983b5e5fa5eSChristoph Hellwig if (err == -ENOIOCTLCMD) 984881d966bSEric W. Biederman err = dev_ioctl(net, cmd, argp); 9851da177e4SLinus Torvalds break; 9861da177e4SLinus Torvalds } 9871da177e4SLinus Torvalds return err; 9881da177e4SLinus Torvalds } 9891da177e4SLinus Torvalds 9901da177e4SLinus Torvalds int sock_create_lite(int family, int type, int protocol, struct socket **res) 9911da177e4SLinus Torvalds { 9921da177e4SLinus Torvalds int err; 9931da177e4SLinus Torvalds struct socket *sock = NULL; 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds err = security_socket_create(family, type, protocol, 1); 9961da177e4SLinus Torvalds if (err) 9971da177e4SLinus Torvalds goto out; 9981da177e4SLinus Torvalds 9991da177e4SLinus Torvalds sock = sock_alloc(); 10001da177e4SLinus Torvalds if (!sock) { 10011da177e4SLinus Torvalds err = -ENOMEM; 10021da177e4SLinus Torvalds goto out; 10031da177e4SLinus Torvalds } 10041da177e4SLinus Torvalds 10051da177e4SLinus Torvalds sock->type = type; 10067420ed23SVenkat Yekkirala err = security_socket_post_create(sock, family, type, protocol, 1); 10077420ed23SVenkat Yekkirala if (err) 10087420ed23SVenkat Yekkirala goto out_release; 10097420ed23SVenkat Yekkirala 10101da177e4SLinus Torvalds out: 10111da177e4SLinus Torvalds *res = sock; 10121da177e4SLinus Torvalds return err; 10137420ed23SVenkat Yekkirala out_release: 10147420ed23SVenkat Yekkirala sock_release(sock); 10157420ed23SVenkat Yekkirala sock = NULL; 10167420ed23SVenkat Yekkirala goto out; 10171da177e4SLinus Torvalds } 10181da177e4SLinus Torvalds 10191da177e4SLinus Torvalds /* No kernel lock held - perfect */ 10201da177e4SLinus Torvalds static unsigned int sock_poll(struct file *file, poll_table *wait) 10211da177e4SLinus Torvalds { 10221da177e4SLinus Torvalds struct socket *sock; 10231da177e4SLinus Torvalds 10241da177e4SLinus Torvalds /* 10251da177e4SLinus Torvalds * We can't return errors to poll, so it's either yes or no. 10261da177e4SLinus Torvalds */ 1027b69aee04SEric Dumazet sock = file->private_data; 10281da177e4SLinus Torvalds return sock->ops->poll(file, sock, wait); 10291da177e4SLinus Torvalds } 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds static int sock_mmap(struct file *file, struct vm_area_struct *vma) 10321da177e4SLinus Torvalds { 1033b69aee04SEric Dumazet struct socket *sock = file->private_data; 10341da177e4SLinus Torvalds 10351da177e4SLinus Torvalds return sock->ops->mmap(file, sock, vma); 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds 103820380731SArnaldo Carvalho de Melo static int sock_close(struct inode *inode, struct file *filp) 10391da177e4SLinus Torvalds { 10401da177e4SLinus Torvalds /* 10411da177e4SLinus Torvalds * It was possible the inode is NULL we were 10421da177e4SLinus Torvalds * closing an unfinished socket. 10431da177e4SLinus Torvalds */ 10441da177e4SLinus Torvalds 104589bddce5SStephen Hemminger if (!inode) { 10461da177e4SLinus Torvalds printk(KERN_DEBUG "sock_close: NULL inode\n"); 10471da177e4SLinus Torvalds return 0; 10481da177e4SLinus Torvalds } 10491da177e4SLinus Torvalds sock_release(SOCKET_I(inode)); 10501da177e4SLinus Torvalds return 0; 10511da177e4SLinus Torvalds } 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds /* 10541da177e4SLinus Torvalds * Update the socket async list 10551da177e4SLinus Torvalds * 10561da177e4SLinus Torvalds * Fasync_list locking strategy. 10571da177e4SLinus Torvalds * 10581da177e4SLinus Torvalds * 1. fasync_list is modified only under process context socket lock 10591da177e4SLinus Torvalds * i.e. under semaphore. 10601da177e4SLinus Torvalds * 2. fasync_list is used under read_lock(&sk->sk_callback_lock) 10611da177e4SLinus Torvalds * or under socket lock. 10621da177e4SLinus Torvalds * 3. fasync_list can be used from softirq context, so that 10631da177e4SLinus Torvalds * modification under socket lock have to be enhanced with 10641da177e4SLinus Torvalds * write_lock_bh(&sk->sk_callback_lock). 10651da177e4SLinus Torvalds * --ANK (990710) 10661da177e4SLinus Torvalds */ 10671da177e4SLinus Torvalds 10681da177e4SLinus Torvalds static int sock_fasync(int fd, struct file *filp, int on) 10691da177e4SLinus Torvalds { 10701da177e4SLinus Torvalds struct fasync_struct *fa, *fna = NULL, **prev; 10711da177e4SLinus Torvalds struct socket *sock; 10721da177e4SLinus Torvalds struct sock *sk; 10731da177e4SLinus Torvalds 107489bddce5SStephen Hemminger if (on) { 10758b3a7005SKris Katterjohn fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL); 10761da177e4SLinus Torvalds if (fna == NULL) 10771da177e4SLinus Torvalds return -ENOMEM; 10781da177e4SLinus Torvalds } 10791da177e4SLinus Torvalds 1080b69aee04SEric Dumazet sock = filp->private_data; 10811da177e4SLinus Torvalds 108289bddce5SStephen Hemminger sk = sock->sk; 108389bddce5SStephen Hemminger if (sk == NULL) { 10841da177e4SLinus Torvalds kfree(fna); 10851da177e4SLinus Torvalds return -EINVAL; 10861da177e4SLinus Torvalds } 10871da177e4SLinus Torvalds 10881da177e4SLinus Torvalds lock_sock(sk); 10891da177e4SLinus Torvalds 109076398425SJonathan Corbet spin_lock(&filp->f_lock); 109176398425SJonathan Corbet if (on) 109276398425SJonathan Corbet filp->f_flags |= FASYNC; 109376398425SJonathan Corbet else 109476398425SJonathan Corbet filp->f_flags &= ~FASYNC; 109576398425SJonathan Corbet spin_unlock(&filp->f_lock); 109676398425SJonathan Corbet 10971da177e4SLinus Torvalds prev = &(sock->fasync_list); 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds for (fa = *prev; fa != NULL; prev = &fa->fa_next, fa = *prev) 11001da177e4SLinus Torvalds if (fa->fa_file == filp) 11011da177e4SLinus Torvalds break; 11021da177e4SLinus Torvalds 110389bddce5SStephen Hemminger if (on) { 110489bddce5SStephen Hemminger if (fa != NULL) { 11051da177e4SLinus Torvalds write_lock_bh(&sk->sk_callback_lock); 11061da177e4SLinus Torvalds fa->fa_fd = fd; 11071da177e4SLinus Torvalds write_unlock_bh(&sk->sk_callback_lock); 11081da177e4SLinus Torvalds 11091da177e4SLinus Torvalds kfree(fna); 11101da177e4SLinus Torvalds goto out; 11111da177e4SLinus Torvalds } 11121da177e4SLinus Torvalds fna->fa_file = filp; 11131da177e4SLinus Torvalds fna->fa_fd = fd; 11141da177e4SLinus Torvalds fna->magic = FASYNC_MAGIC; 11151da177e4SLinus Torvalds fna->fa_next = sock->fasync_list; 11161da177e4SLinus Torvalds write_lock_bh(&sk->sk_callback_lock); 11171da177e4SLinus Torvalds sock->fasync_list = fna; 1118bcdce719SEric Dumazet sock_set_flag(sk, SOCK_FASYNC); 11191da177e4SLinus Torvalds write_unlock_bh(&sk->sk_callback_lock); 112089bddce5SStephen Hemminger } else { 112189bddce5SStephen Hemminger if (fa != NULL) { 11221da177e4SLinus Torvalds write_lock_bh(&sk->sk_callback_lock); 11231da177e4SLinus Torvalds *prev = fa->fa_next; 1124bcdce719SEric Dumazet if (!sock->fasync_list) 1125bcdce719SEric Dumazet sock_reset_flag(sk, SOCK_FASYNC); 11261da177e4SLinus Torvalds write_unlock_bh(&sk->sk_callback_lock); 11271da177e4SLinus Torvalds kfree(fa); 11281da177e4SLinus Torvalds } 11291da177e4SLinus Torvalds } 11301da177e4SLinus Torvalds 11311da177e4SLinus Torvalds out: 11321da177e4SLinus Torvalds release_sock(sock->sk); 11331da177e4SLinus Torvalds return 0; 11341da177e4SLinus Torvalds } 11351da177e4SLinus Torvalds 11361da177e4SLinus Torvalds /* This function may be called only under socket lock or callback_lock */ 11371da177e4SLinus Torvalds 11381da177e4SLinus Torvalds int sock_wake_async(struct socket *sock, int how, int band) 11391da177e4SLinus Torvalds { 11401da177e4SLinus Torvalds if (!sock || !sock->fasync_list) 11411da177e4SLinus Torvalds return -1; 114289bddce5SStephen Hemminger switch (how) { 11438d8ad9d7SPavel Emelyanov case SOCK_WAKE_WAITD: 11441da177e4SLinus Torvalds if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags)) 11451da177e4SLinus Torvalds break; 11461da177e4SLinus Torvalds goto call_kill; 11478d8ad9d7SPavel Emelyanov case SOCK_WAKE_SPACE: 11481da177e4SLinus Torvalds if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags)) 11491da177e4SLinus Torvalds break; 11501da177e4SLinus Torvalds /* fall through */ 11518d8ad9d7SPavel Emelyanov case SOCK_WAKE_IO: 11521da177e4SLinus Torvalds call_kill: 11531da177e4SLinus Torvalds __kill_fasync(sock->fasync_list, SIGIO, band); 11541da177e4SLinus Torvalds break; 11558d8ad9d7SPavel Emelyanov case SOCK_WAKE_URG: 11561da177e4SLinus Torvalds __kill_fasync(sock->fasync_list, SIGURG, band); 11571da177e4SLinus Torvalds } 11581da177e4SLinus Torvalds return 0; 11591da177e4SLinus Torvalds } 11601da177e4SLinus Torvalds 11611b8d7ae4SEric W. Biederman static int __sock_create(struct net *net, int family, int type, int protocol, 116289bddce5SStephen Hemminger struct socket **res, int kern) 11631da177e4SLinus Torvalds { 11641da177e4SLinus Torvalds int err; 11651da177e4SLinus Torvalds struct socket *sock; 116655737fdaSStephen Hemminger const struct net_proto_family *pf; 11671da177e4SLinus Torvalds 11681da177e4SLinus Torvalds /* 11691da177e4SLinus Torvalds * Check protocol is in range 11701da177e4SLinus Torvalds */ 11711da177e4SLinus Torvalds if (family < 0 || family >= NPROTO) 11721da177e4SLinus Torvalds return -EAFNOSUPPORT; 11731da177e4SLinus Torvalds if (type < 0 || type >= SOCK_MAX) 11741da177e4SLinus Torvalds return -EINVAL; 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds /* Compatibility. 11771da177e4SLinus Torvalds 11781da177e4SLinus Torvalds This uglymoron is moved from INET layer to here to avoid 11791da177e4SLinus Torvalds deadlock in module load. 11801da177e4SLinus Torvalds */ 11811da177e4SLinus Torvalds if (family == PF_INET && type == SOCK_PACKET) { 11821da177e4SLinus Torvalds static int warned; 11831da177e4SLinus Torvalds if (!warned) { 11841da177e4SLinus Torvalds warned = 1; 118589bddce5SStephen Hemminger printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n", 118689bddce5SStephen Hemminger current->comm); 11871da177e4SLinus Torvalds } 11881da177e4SLinus Torvalds family = PF_PACKET; 11891da177e4SLinus Torvalds } 11901da177e4SLinus Torvalds 11911da177e4SLinus Torvalds err = security_socket_create(family, type, protocol, kern); 11921da177e4SLinus Torvalds if (err) 11931da177e4SLinus Torvalds return err; 11941da177e4SLinus Torvalds 119555737fdaSStephen Hemminger /* 119655737fdaSStephen Hemminger * Allocate the socket and allow the family to set things up. if 119755737fdaSStephen Hemminger * the protocol is 0, the family is instructed to select an appropriate 119855737fdaSStephen Hemminger * default. 119955737fdaSStephen Hemminger */ 120055737fdaSStephen Hemminger sock = sock_alloc(); 120155737fdaSStephen Hemminger if (!sock) { 120255737fdaSStephen Hemminger if (net_ratelimit()) 120355737fdaSStephen Hemminger printk(KERN_WARNING "socket: no more sockets\n"); 120455737fdaSStephen Hemminger return -ENFILE; /* Not exactly a match, but its the 120555737fdaSStephen Hemminger closest posix thing */ 120655737fdaSStephen Hemminger } 120755737fdaSStephen Hemminger 120855737fdaSStephen Hemminger sock->type = type; 120955737fdaSStephen Hemminger 121095a5afcaSJohannes Berg #ifdef CONFIG_MODULES 12111da177e4SLinus Torvalds /* Attempt to load a protocol module if the find failed. 12121da177e4SLinus Torvalds * 12131da177e4SLinus Torvalds * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user 12141da177e4SLinus Torvalds * requested real, full-featured networking support upon configuration. 12151da177e4SLinus Torvalds * Otherwise module support will break! 12161da177e4SLinus Torvalds */ 121755737fdaSStephen Hemminger if (net_families[family] == NULL) 12181da177e4SLinus Torvalds request_module("net-pf-%d", family); 12191da177e4SLinus Torvalds #endif 12201da177e4SLinus Torvalds 122155737fdaSStephen Hemminger rcu_read_lock(); 122255737fdaSStephen Hemminger pf = rcu_dereference(net_families[family]); 12231da177e4SLinus Torvalds err = -EAFNOSUPPORT; 122455737fdaSStephen Hemminger if (!pf) 122555737fdaSStephen Hemminger goto out_release; 12261da177e4SLinus Torvalds 12271da177e4SLinus Torvalds /* 12281da177e4SLinus Torvalds * We will call the ->create function, that possibly is in a loadable 12291da177e4SLinus Torvalds * module, so we have to bump that loadable module refcnt first. 12301da177e4SLinus Torvalds */ 123155737fdaSStephen Hemminger if (!try_module_get(pf->owner)) 12321da177e4SLinus Torvalds goto out_release; 12331da177e4SLinus Torvalds 123455737fdaSStephen Hemminger /* Now protected by module ref count */ 123555737fdaSStephen Hemminger rcu_read_unlock(); 123655737fdaSStephen Hemminger 12371b8d7ae4SEric W. Biederman err = pf->create(net, sock, protocol); 123855737fdaSStephen Hemminger if (err < 0) 12391da177e4SLinus Torvalds goto out_module_put; 1240a79af59eSFrank Filz 12411da177e4SLinus Torvalds /* 12421da177e4SLinus Torvalds * Now to bump the refcnt of the [loadable] module that owns this 12431da177e4SLinus Torvalds * socket at sock_release time we decrement its refcnt. 12441da177e4SLinus Torvalds */ 124555737fdaSStephen Hemminger if (!try_module_get(sock->ops->owner)) 124655737fdaSStephen Hemminger goto out_module_busy; 124755737fdaSStephen Hemminger 12481da177e4SLinus Torvalds /* 12491da177e4SLinus Torvalds * Now that we're done with the ->create function, the [loadable] 12501da177e4SLinus Torvalds * module can have its refcnt decremented 12511da177e4SLinus Torvalds */ 125255737fdaSStephen Hemminger module_put(pf->owner); 12537420ed23SVenkat Yekkirala err = security_socket_post_create(sock, family, type, protocol, kern); 12547420ed23SVenkat Yekkirala if (err) 12553b185525SHerbert Xu goto out_sock_release; 125655737fdaSStephen Hemminger *res = sock; 12571da177e4SLinus Torvalds 125855737fdaSStephen Hemminger return 0; 125955737fdaSStephen Hemminger 126055737fdaSStephen Hemminger out_module_busy: 126155737fdaSStephen Hemminger err = -EAFNOSUPPORT; 12621da177e4SLinus Torvalds out_module_put: 126355737fdaSStephen Hemminger sock->ops = NULL; 126455737fdaSStephen Hemminger module_put(pf->owner); 126555737fdaSStephen Hemminger out_sock_release: 12661da177e4SLinus Torvalds sock_release(sock); 126755737fdaSStephen Hemminger return err; 126855737fdaSStephen Hemminger 126955737fdaSStephen Hemminger out_release: 127055737fdaSStephen Hemminger rcu_read_unlock(); 127155737fdaSStephen Hemminger goto out_sock_release; 12721da177e4SLinus Torvalds } 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds int sock_create(int family, int type, int protocol, struct socket **res) 12751da177e4SLinus Torvalds { 12761b8d7ae4SEric W. Biederman return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0); 12771da177e4SLinus Torvalds } 12781da177e4SLinus Torvalds 12791da177e4SLinus Torvalds int sock_create_kern(int family, int type, int protocol, struct socket **res) 12801da177e4SLinus Torvalds { 12811b8d7ae4SEric W. Biederman return __sock_create(&init_net, family, type, protocol, res, 1); 12821da177e4SLinus Torvalds } 12831da177e4SLinus Torvalds 12843e0fa65fSHeiko Carstens SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol) 12851da177e4SLinus Torvalds { 12861da177e4SLinus Torvalds int retval; 12871da177e4SLinus Torvalds struct socket *sock; 1288a677a039SUlrich Drepper int flags; 1289a677a039SUlrich Drepper 1290e38b36f3SUlrich Drepper /* Check the SOCK_* constants for consistency. */ 1291e38b36f3SUlrich Drepper BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC); 1292e38b36f3SUlrich Drepper BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK); 1293e38b36f3SUlrich Drepper BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK); 1294e38b36f3SUlrich Drepper BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK); 1295e38b36f3SUlrich Drepper 1296a677a039SUlrich Drepper flags = type & ~SOCK_TYPE_MASK; 129777d27200SUlrich Drepper if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) 1298a677a039SUlrich Drepper return -EINVAL; 1299a677a039SUlrich Drepper type &= SOCK_TYPE_MASK; 13001da177e4SLinus Torvalds 1301aaca0bdcSUlrich Drepper if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK)) 1302aaca0bdcSUlrich Drepper flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK; 1303aaca0bdcSUlrich Drepper 13041da177e4SLinus Torvalds retval = sock_create(family, type, protocol, &sock); 13051da177e4SLinus Torvalds if (retval < 0) 13061da177e4SLinus Torvalds goto out; 13071da177e4SLinus Torvalds 130877d27200SUlrich Drepper retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK)); 13091da177e4SLinus Torvalds if (retval < 0) 13101da177e4SLinus Torvalds goto out_release; 13111da177e4SLinus Torvalds 13121da177e4SLinus Torvalds out: 13131da177e4SLinus Torvalds /* It may be already another descriptor 8) Not kernel problem. */ 13141da177e4SLinus Torvalds return retval; 13151da177e4SLinus Torvalds 13161da177e4SLinus Torvalds out_release: 13171da177e4SLinus Torvalds sock_release(sock); 13181da177e4SLinus Torvalds return retval; 13191da177e4SLinus Torvalds } 13201da177e4SLinus Torvalds 13211da177e4SLinus Torvalds /* 13221da177e4SLinus Torvalds * Create a pair of connected sockets. 13231da177e4SLinus Torvalds */ 13241da177e4SLinus Torvalds 13253e0fa65fSHeiko Carstens SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol, 13263e0fa65fSHeiko Carstens int __user *, usockvec) 13271da177e4SLinus Torvalds { 13281da177e4SLinus Torvalds struct socket *sock1, *sock2; 13291da177e4SLinus Torvalds int fd1, fd2, err; 1330db349509SAl Viro struct file *newfile1, *newfile2; 1331a677a039SUlrich Drepper int flags; 1332a677a039SUlrich Drepper 1333a677a039SUlrich Drepper flags = type & ~SOCK_TYPE_MASK; 133477d27200SUlrich Drepper if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) 1335a677a039SUlrich Drepper return -EINVAL; 1336a677a039SUlrich Drepper type &= SOCK_TYPE_MASK; 13371da177e4SLinus Torvalds 1338aaca0bdcSUlrich Drepper if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK)) 1339aaca0bdcSUlrich Drepper flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK; 1340aaca0bdcSUlrich Drepper 13411da177e4SLinus Torvalds /* 13421da177e4SLinus Torvalds * Obtain the first socket and check if the underlying protocol 13431da177e4SLinus Torvalds * supports the socketpair call. 13441da177e4SLinus Torvalds */ 13451da177e4SLinus Torvalds 13461da177e4SLinus Torvalds err = sock_create(family, type, protocol, &sock1); 13471da177e4SLinus Torvalds if (err < 0) 13481da177e4SLinus Torvalds goto out; 13491da177e4SLinus Torvalds 13501da177e4SLinus Torvalds err = sock_create(family, type, protocol, &sock2); 13511da177e4SLinus Torvalds if (err < 0) 13521da177e4SLinus Torvalds goto out_release_1; 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds err = sock1->ops->socketpair(sock1, sock2); 13551da177e4SLinus Torvalds if (err < 0) 13561da177e4SLinus Torvalds goto out_release_both; 13571da177e4SLinus Torvalds 1358a677a039SUlrich Drepper fd1 = sock_alloc_fd(&newfile1, flags & O_CLOEXEC); 1359bf3c23d1SDavid S. Miller if (unlikely(fd1 < 0)) { 1360bf3c23d1SDavid S. Miller err = fd1; 13611da177e4SLinus Torvalds goto out_release_both; 1362bf3c23d1SDavid S. Miller } 13631da177e4SLinus Torvalds 1364a677a039SUlrich Drepper fd2 = sock_alloc_fd(&newfile2, flags & O_CLOEXEC); 1365db349509SAl Viro if (unlikely(fd2 < 0)) { 1366bf3c23d1SDavid S. Miller err = fd2; 1367db349509SAl Viro put_filp(newfile1); 1368db349509SAl Viro put_unused_fd(fd1); 1369db349509SAl Viro goto out_release_both; 1370db349509SAl Viro } 13711da177e4SLinus Torvalds 137277d27200SUlrich Drepper err = sock_attach_fd(sock1, newfile1, flags & O_NONBLOCK); 1373db349509SAl Viro if (unlikely(err < 0)) { 1374db349509SAl Viro goto out_fd2; 1375db349509SAl Viro } 1376db349509SAl Viro 137777d27200SUlrich Drepper err = sock_attach_fd(sock2, newfile2, flags & O_NONBLOCK); 1378db349509SAl Viro if (unlikely(err < 0)) { 1379db349509SAl Viro fput(newfile1); 1380db349509SAl Viro goto out_fd1; 1381db349509SAl Viro } 1382db349509SAl Viro 1383157cf649SAl Viro audit_fd_pair(fd1, fd2); 1384db349509SAl Viro fd_install(fd1, newfile1); 1385db349509SAl Viro fd_install(fd2, newfile2); 13861da177e4SLinus Torvalds /* fd1 and fd2 may be already another descriptors. 13871da177e4SLinus Torvalds * Not kernel problem. 13881da177e4SLinus Torvalds */ 13891da177e4SLinus Torvalds 13901da177e4SLinus Torvalds err = put_user(fd1, &usockvec[0]); 13911da177e4SLinus Torvalds if (!err) 13921da177e4SLinus Torvalds err = put_user(fd2, &usockvec[1]); 13931da177e4SLinus Torvalds if (!err) 13941da177e4SLinus Torvalds return 0; 13951da177e4SLinus Torvalds 13961da177e4SLinus Torvalds sys_close(fd2); 13971da177e4SLinus Torvalds sys_close(fd1); 13981da177e4SLinus Torvalds return err; 13991da177e4SLinus Torvalds 14001da177e4SLinus Torvalds out_release_both: 14011da177e4SLinus Torvalds sock_release(sock2); 14021da177e4SLinus Torvalds out_release_1: 14031da177e4SLinus Torvalds sock_release(sock1); 14041da177e4SLinus Torvalds out: 14051da177e4SLinus Torvalds return err; 1406db349509SAl Viro 1407db349509SAl Viro out_fd2: 1408db349509SAl Viro put_filp(newfile1); 1409db349509SAl Viro sock_release(sock1); 1410db349509SAl Viro out_fd1: 1411db349509SAl Viro put_filp(newfile2); 1412db349509SAl Viro sock_release(sock2); 1413db349509SAl Viro put_unused_fd(fd1); 1414db349509SAl Viro put_unused_fd(fd2); 1415db349509SAl Viro goto out; 14161da177e4SLinus Torvalds } 14171da177e4SLinus Torvalds 14181da177e4SLinus Torvalds /* 14191da177e4SLinus Torvalds * Bind a name to a socket. Nothing much to do here since it's 14201da177e4SLinus Torvalds * the protocol's responsibility to handle the local address. 14211da177e4SLinus Torvalds * 14221da177e4SLinus Torvalds * We move the socket address to kernel space before we call 14231da177e4SLinus Torvalds * the protocol layer (having also checked the address is ok). 14241da177e4SLinus Torvalds */ 14251da177e4SLinus Torvalds 142620f37034SHeiko Carstens SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen) 14271da177e4SLinus Torvalds { 14281da177e4SLinus Torvalds struct socket *sock; 1429230b1839SYOSHIFUJI Hideaki struct sockaddr_storage address; 14306cb153caSBenjamin LaHaise int err, fput_needed; 14311da177e4SLinus Torvalds 143289bddce5SStephen Hemminger sock = sockfd_lookup_light(fd, &err, &fput_needed); 143389bddce5SStephen Hemminger if (sock) { 1434230b1839SYOSHIFUJI Hideaki err = move_addr_to_kernel(umyaddr, addrlen, (struct sockaddr *)&address); 143589bddce5SStephen Hemminger if (err >= 0) { 143689bddce5SStephen Hemminger err = security_socket_bind(sock, 1437230b1839SYOSHIFUJI Hideaki (struct sockaddr *)&address, 143889bddce5SStephen Hemminger addrlen); 14396cb153caSBenjamin LaHaise if (!err) 14406cb153caSBenjamin LaHaise err = sock->ops->bind(sock, 144189bddce5SStephen Hemminger (struct sockaddr *) 1442230b1839SYOSHIFUJI Hideaki &address, addrlen); 14431da177e4SLinus Torvalds } 14446cb153caSBenjamin LaHaise fput_light(sock->file, fput_needed); 14451da177e4SLinus Torvalds } 14461da177e4SLinus Torvalds return err; 14471da177e4SLinus Torvalds } 14481da177e4SLinus Torvalds 14491da177e4SLinus Torvalds /* 14501da177e4SLinus Torvalds * Perform a listen. Basically, we allow the protocol to do anything 14511da177e4SLinus Torvalds * necessary for a listen, and if that works, we mark the socket as 14521da177e4SLinus Torvalds * ready for listening. 14531da177e4SLinus Torvalds */ 14541da177e4SLinus Torvalds 14553e0fa65fSHeiko Carstens SYSCALL_DEFINE2(listen, int, fd, int, backlog) 14561da177e4SLinus Torvalds { 14571da177e4SLinus Torvalds struct socket *sock; 14586cb153caSBenjamin LaHaise int err, fput_needed; 1459b8e1f9b5SPavel Emelyanov int somaxconn; 14601da177e4SLinus Torvalds 146189bddce5SStephen Hemminger sock = sockfd_lookup_light(fd, &err, &fput_needed); 146289bddce5SStephen Hemminger if (sock) { 14638efa6e93SPavel Emelyanov somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn; 1464b8e1f9b5SPavel Emelyanov if ((unsigned)backlog > somaxconn) 1465b8e1f9b5SPavel Emelyanov backlog = somaxconn; 14661da177e4SLinus Torvalds 14671da177e4SLinus Torvalds err = security_socket_listen(sock, backlog); 14686cb153caSBenjamin LaHaise if (!err) 14691da177e4SLinus Torvalds err = sock->ops->listen(sock, backlog); 14706cb153caSBenjamin LaHaise 14716cb153caSBenjamin LaHaise fput_light(sock->file, fput_needed); 14721da177e4SLinus Torvalds } 14731da177e4SLinus Torvalds return err; 14741da177e4SLinus Torvalds } 14751da177e4SLinus Torvalds 14761da177e4SLinus Torvalds /* 14771da177e4SLinus Torvalds * For accept, we attempt to create a new socket, set up the link 14781da177e4SLinus Torvalds * with the client, wake up the client, then return the new 14791da177e4SLinus Torvalds * connected fd. We collect the address of the connector in kernel 14801da177e4SLinus Torvalds * space and move it to user at the very end. This is unclean because 14811da177e4SLinus Torvalds * we open the socket then return an error. 14821da177e4SLinus Torvalds * 14831da177e4SLinus Torvalds * 1003.1g adds the ability to recvmsg() to query connection pending 14841da177e4SLinus Torvalds * status to recvmsg. We need to add that support in a way thats 14851da177e4SLinus Torvalds * clean when we restucture accept also. 14861da177e4SLinus Torvalds */ 14871da177e4SLinus Torvalds 148820f37034SHeiko Carstens SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr, 148920f37034SHeiko Carstens int __user *, upeer_addrlen, int, flags) 14901da177e4SLinus Torvalds { 14911da177e4SLinus Torvalds struct socket *sock, *newsock; 149239d8c1b6SDavid S. Miller struct file *newfile; 14936cb153caSBenjamin LaHaise int err, len, newfd, fput_needed; 1494230b1839SYOSHIFUJI Hideaki struct sockaddr_storage address; 14951da177e4SLinus Torvalds 149677d27200SUlrich Drepper if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) 1497aaca0bdcSUlrich Drepper return -EINVAL; 1498aaca0bdcSUlrich Drepper 1499aaca0bdcSUlrich Drepper if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK)) 1500aaca0bdcSUlrich Drepper flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK; 1501aaca0bdcSUlrich Drepper 15026cb153caSBenjamin LaHaise sock = sockfd_lookup_light(fd, &err, &fput_needed); 15031da177e4SLinus Torvalds if (!sock) 15041da177e4SLinus Torvalds goto out; 15051da177e4SLinus Torvalds 15061da177e4SLinus Torvalds err = -ENFILE; 15071da177e4SLinus Torvalds if (!(newsock = sock_alloc())) 15081da177e4SLinus Torvalds goto out_put; 15091da177e4SLinus Torvalds 15101da177e4SLinus Torvalds newsock->type = sock->type; 15111da177e4SLinus Torvalds newsock->ops = sock->ops; 15121da177e4SLinus Torvalds 15131da177e4SLinus Torvalds /* 15141da177e4SLinus Torvalds * We don't need try_module_get here, as the listening socket (sock) 15151da177e4SLinus Torvalds * has the protocol module (sock->ops->owner) held. 15161da177e4SLinus Torvalds */ 15171da177e4SLinus Torvalds __module_get(newsock->ops->owner); 15181da177e4SLinus Torvalds 1519aaca0bdcSUlrich Drepper newfd = sock_alloc_fd(&newfile, flags & O_CLOEXEC); 152039d8c1b6SDavid S. Miller if (unlikely(newfd < 0)) { 152139d8c1b6SDavid S. Miller err = newfd; 15229a1875e6SDavid S. Miller sock_release(newsock); 15239a1875e6SDavid S. Miller goto out_put; 152439d8c1b6SDavid S. Miller } 152539d8c1b6SDavid S. Miller 152677d27200SUlrich Drepper err = sock_attach_fd(newsock, newfile, flags & O_NONBLOCK); 152739d8c1b6SDavid S. Miller if (err < 0) 152879f4f642SAlexey Dobriyan goto out_fd_simple; 152939d8c1b6SDavid S. Miller 1530a79af59eSFrank Filz err = security_socket_accept(sock, newsock); 1531a79af59eSFrank Filz if (err) 153239d8c1b6SDavid S. Miller goto out_fd; 1533a79af59eSFrank Filz 15341da177e4SLinus Torvalds err = sock->ops->accept(sock, newsock, sock->file->f_flags); 15351da177e4SLinus Torvalds if (err < 0) 153639d8c1b6SDavid S. Miller goto out_fd; 15371da177e4SLinus Torvalds 15381da177e4SLinus Torvalds if (upeer_sockaddr) { 1539230b1839SYOSHIFUJI Hideaki if (newsock->ops->getname(newsock, (struct sockaddr *)&address, 154089bddce5SStephen Hemminger &len, 2) < 0) { 15411da177e4SLinus Torvalds err = -ECONNABORTED; 154239d8c1b6SDavid S. Miller goto out_fd; 15431da177e4SLinus Torvalds } 1544230b1839SYOSHIFUJI Hideaki err = move_addr_to_user((struct sockaddr *)&address, 1545230b1839SYOSHIFUJI Hideaki len, upeer_sockaddr, upeer_addrlen); 15461da177e4SLinus Torvalds if (err < 0) 154739d8c1b6SDavid S. Miller goto out_fd; 15481da177e4SLinus Torvalds } 15491da177e4SLinus Torvalds 15501da177e4SLinus Torvalds /* File flags are not inherited via accept() unlike another OSes. */ 15511da177e4SLinus Torvalds 155239d8c1b6SDavid S. Miller fd_install(newfd, newfile); 155339d8c1b6SDavid S. Miller err = newfd; 15541da177e4SLinus Torvalds 15551da177e4SLinus Torvalds out_put: 15566cb153caSBenjamin LaHaise fput_light(sock->file, fput_needed); 15571da177e4SLinus Torvalds out: 15581da177e4SLinus Torvalds return err; 155979f4f642SAlexey Dobriyan out_fd_simple: 156079f4f642SAlexey Dobriyan sock_release(newsock); 156179f4f642SAlexey Dobriyan put_filp(newfile); 156279f4f642SAlexey Dobriyan put_unused_fd(newfd); 156379f4f642SAlexey Dobriyan goto out_put; 156439d8c1b6SDavid S. Miller out_fd: 15659606a216SDavid S. Miller fput(newfile); 156639d8c1b6SDavid S. Miller put_unused_fd(newfd); 15671da177e4SLinus Torvalds goto out_put; 15681da177e4SLinus Torvalds } 15691da177e4SLinus Torvalds 157020f37034SHeiko Carstens SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr, 157120f37034SHeiko Carstens int __user *, upeer_addrlen) 1572aaca0bdcSUlrich Drepper { 1573de11defeSUlrich Drepper return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0); 1574aaca0bdcSUlrich Drepper } 1575aaca0bdcSUlrich Drepper 15761da177e4SLinus Torvalds /* 15771da177e4SLinus Torvalds * Attempt to connect to a socket with the server address. The address 15781da177e4SLinus Torvalds * is in user space so we verify it is OK and move it to kernel space. 15791da177e4SLinus Torvalds * 15801da177e4SLinus Torvalds * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to 15811da177e4SLinus Torvalds * break bindings 15821da177e4SLinus Torvalds * 15831da177e4SLinus Torvalds * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and 15841da177e4SLinus Torvalds * other SEQPACKET protocols that take time to connect() as it doesn't 15851da177e4SLinus Torvalds * include the -EINPROGRESS status for such sockets. 15861da177e4SLinus Torvalds */ 15871da177e4SLinus Torvalds 158820f37034SHeiko Carstens SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr, 158920f37034SHeiko Carstens int, addrlen) 15901da177e4SLinus Torvalds { 15911da177e4SLinus Torvalds struct socket *sock; 1592230b1839SYOSHIFUJI Hideaki struct sockaddr_storage address; 15936cb153caSBenjamin LaHaise int err, fput_needed; 15941da177e4SLinus Torvalds 15956cb153caSBenjamin LaHaise sock = sockfd_lookup_light(fd, &err, &fput_needed); 15961da177e4SLinus Torvalds if (!sock) 15971da177e4SLinus Torvalds goto out; 1598230b1839SYOSHIFUJI Hideaki err = move_addr_to_kernel(uservaddr, addrlen, (struct sockaddr *)&address); 15991da177e4SLinus Torvalds if (err < 0) 16001da177e4SLinus Torvalds goto out_put; 16011da177e4SLinus Torvalds 160289bddce5SStephen Hemminger err = 1603230b1839SYOSHIFUJI Hideaki security_socket_connect(sock, (struct sockaddr *)&address, addrlen); 16041da177e4SLinus Torvalds if (err) 16051da177e4SLinus Torvalds goto out_put; 16061da177e4SLinus Torvalds 1607230b1839SYOSHIFUJI Hideaki err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen, 16081da177e4SLinus Torvalds sock->file->f_flags); 16091da177e4SLinus Torvalds out_put: 16106cb153caSBenjamin LaHaise fput_light(sock->file, fput_needed); 16111da177e4SLinus Torvalds out: 16121da177e4SLinus Torvalds return err; 16131da177e4SLinus Torvalds } 16141da177e4SLinus Torvalds 16151da177e4SLinus Torvalds /* 16161da177e4SLinus Torvalds * Get the local address ('name') of a socket object. Move the obtained 16171da177e4SLinus Torvalds * name to user space. 16181da177e4SLinus Torvalds */ 16191da177e4SLinus Torvalds 162020f37034SHeiko Carstens SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr, 162120f37034SHeiko Carstens int __user *, usockaddr_len) 16221da177e4SLinus Torvalds { 16231da177e4SLinus Torvalds struct socket *sock; 1624230b1839SYOSHIFUJI Hideaki struct sockaddr_storage address; 16256cb153caSBenjamin LaHaise int len, err, fput_needed; 16261da177e4SLinus Torvalds 16276cb153caSBenjamin LaHaise sock = sockfd_lookup_light(fd, &err, &fput_needed); 16281da177e4SLinus Torvalds if (!sock) 16291da177e4SLinus Torvalds goto out; 16301da177e4SLinus Torvalds 16311da177e4SLinus Torvalds err = security_socket_getsockname(sock); 16321da177e4SLinus Torvalds if (err) 16331da177e4SLinus Torvalds goto out_put; 16341da177e4SLinus Torvalds 1635230b1839SYOSHIFUJI Hideaki err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0); 16361da177e4SLinus Torvalds if (err) 16371da177e4SLinus Torvalds goto out_put; 1638230b1839SYOSHIFUJI Hideaki err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr, usockaddr_len); 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds out_put: 16416cb153caSBenjamin LaHaise fput_light(sock->file, fput_needed); 16421da177e4SLinus Torvalds out: 16431da177e4SLinus Torvalds return err; 16441da177e4SLinus Torvalds } 16451da177e4SLinus Torvalds 16461da177e4SLinus Torvalds /* 16471da177e4SLinus Torvalds * Get the remote address ('name') of a socket object. Move the obtained 16481da177e4SLinus Torvalds * name to user space. 16491da177e4SLinus Torvalds */ 16501da177e4SLinus Torvalds 165120f37034SHeiko Carstens SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr, 165220f37034SHeiko Carstens int __user *, usockaddr_len) 16531da177e4SLinus Torvalds { 16541da177e4SLinus Torvalds struct socket *sock; 1655230b1839SYOSHIFUJI Hideaki struct sockaddr_storage address; 16566cb153caSBenjamin LaHaise int len, err, fput_needed; 16571da177e4SLinus Torvalds 165889bddce5SStephen Hemminger sock = sockfd_lookup_light(fd, &err, &fput_needed); 165989bddce5SStephen Hemminger if (sock != NULL) { 16601da177e4SLinus Torvalds err = security_socket_getpeername(sock); 16611da177e4SLinus Torvalds if (err) { 16626cb153caSBenjamin LaHaise fput_light(sock->file, fput_needed); 16631da177e4SLinus Torvalds return err; 16641da177e4SLinus Torvalds } 16651da177e4SLinus Torvalds 166689bddce5SStephen Hemminger err = 1667230b1839SYOSHIFUJI Hideaki sock->ops->getname(sock, (struct sockaddr *)&address, &len, 166889bddce5SStephen Hemminger 1); 16691da177e4SLinus Torvalds if (!err) 1670230b1839SYOSHIFUJI Hideaki err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr, 167189bddce5SStephen Hemminger usockaddr_len); 16726cb153caSBenjamin LaHaise fput_light(sock->file, fput_needed); 16731da177e4SLinus Torvalds } 16741da177e4SLinus Torvalds return err; 16751da177e4SLinus Torvalds } 16761da177e4SLinus Torvalds 16771da177e4SLinus Torvalds /* 16781da177e4SLinus Torvalds * Send a datagram to a given address. We move the address into kernel 16791da177e4SLinus Torvalds * space and check the user space data area is readable before invoking 16801da177e4SLinus Torvalds * the protocol. 16811da177e4SLinus Torvalds */ 16821da177e4SLinus Torvalds 16833e0fa65fSHeiko Carstens SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len, 16843e0fa65fSHeiko Carstens unsigned, flags, struct sockaddr __user *, addr, 16853e0fa65fSHeiko Carstens int, addr_len) 16861da177e4SLinus Torvalds { 16871da177e4SLinus Torvalds struct socket *sock; 1688230b1839SYOSHIFUJI Hideaki struct sockaddr_storage address; 16891da177e4SLinus Torvalds int err; 16901da177e4SLinus Torvalds struct msghdr msg; 16911da177e4SLinus Torvalds struct iovec iov; 16926cb153caSBenjamin LaHaise int fput_needed; 16931da177e4SLinus Torvalds 1694de0fa95cSPavel Emelyanov sock = sockfd_lookup_light(fd, &err, &fput_needed); 1695de0fa95cSPavel Emelyanov if (!sock) 16964387ff75SDavid S. Miller goto out; 16976cb153caSBenjamin LaHaise 16981da177e4SLinus Torvalds iov.iov_base = buff; 16991da177e4SLinus Torvalds iov.iov_len = len; 17001da177e4SLinus Torvalds msg.msg_name = NULL; 17011da177e4SLinus Torvalds msg.msg_iov = &iov; 17021da177e4SLinus Torvalds msg.msg_iovlen = 1; 17031da177e4SLinus Torvalds msg.msg_control = NULL; 17041da177e4SLinus Torvalds msg.msg_controllen = 0; 17051da177e4SLinus Torvalds msg.msg_namelen = 0; 17066cb153caSBenjamin LaHaise if (addr) { 1707230b1839SYOSHIFUJI Hideaki err = move_addr_to_kernel(addr, addr_len, (struct sockaddr *)&address); 17081da177e4SLinus Torvalds if (err < 0) 17091da177e4SLinus Torvalds goto out_put; 1710230b1839SYOSHIFUJI Hideaki msg.msg_name = (struct sockaddr *)&address; 17111da177e4SLinus Torvalds msg.msg_namelen = addr_len; 17121da177e4SLinus Torvalds } 17131da177e4SLinus Torvalds if (sock->file->f_flags & O_NONBLOCK) 17141da177e4SLinus Torvalds flags |= MSG_DONTWAIT; 17151da177e4SLinus Torvalds msg.msg_flags = flags; 17161da177e4SLinus Torvalds err = sock_sendmsg(sock, &msg, len); 17171da177e4SLinus Torvalds 17181da177e4SLinus Torvalds out_put: 1719de0fa95cSPavel Emelyanov fput_light(sock->file, fput_needed); 17204387ff75SDavid S. Miller out: 17211da177e4SLinus Torvalds return err; 17221da177e4SLinus Torvalds } 17231da177e4SLinus Torvalds 17241da177e4SLinus Torvalds /* 17251da177e4SLinus Torvalds * Send a datagram down a socket. 17261da177e4SLinus Torvalds */ 17271da177e4SLinus Torvalds 17283e0fa65fSHeiko Carstens SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len, 17293e0fa65fSHeiko Carstens unsigned, flags) 17301da177e4SLinus Torvalds { 17311da177e4SLinus Torvalds return sys_sendto(fd, buff, len, flags, NULL, 0); 17321da177e4SLinus Torvalds } 17331da177e4SLinus Torvalds 17341da177e4SLinus Torvalds /* 17351da177e4SLinus Torvalds * Receive a frame from the socket and optionally record the address of the 17361da177e4SLinus Torvalds * sender. We verify the buffers are writable and if needed move the 17371da177e4SLinus Torvalds * sender address from kernel to user space. 17381da177e4SLinus Torvalds */ 17391da177e4SLinus Torvalds 17403e0fa65fSHeiko Carstens SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size, 17413e0fa65fSHeiko Carstens unsigned, flags, struct sockaddr __user *, addr, 17423e0fa65fSHeiko Carstens int __user *, addr_len) 17431da177e4SLinus Torvalds { 17441da177e4SLinus Torvalds struct socket *sock; 17451da177e4SLinus Torvalds struct iovec iov; 17461da177e4SLinus Torvalds struct msghdr msg; 1747230b1839SYOSHIFUJI Hideaki struct sockaddr_storage address; 17481da177e4SLinus Torvalds int err, err2; 17496cb153caSBenjamin LaHaise int fput_needed; 17501da177e4SLinus Torvalds 1751de0fa95cSPavel Emelyanov sock = sockfd_lookup_light(fd, &err, &fput_needed); 17521da177e4SLinus Torvalds if (!sock) 1753de0fa95cSPavel Emelyanov goto out; 17541da177e4SLinus Torvalds 17551da177e4SLinus Torvalds msg.msg_control = NULL; 17561da177e4SLinus Torvalds msg.msg_controllen = 0; 17571da177e4SLinus Torvalds msg.msg_iovlen = 1; 17581da177e4SLinus Torvalds msg.msg_iov = &iov; 17591da177e4SLinus Torvalds iov.iov_len = size; 17601da177e4SLinus Torvalds iov.iov_base = ubuf; 1761230b1839SYOSHIFUJI Hideaki msg.msg_name = (struct sockaddr *)&address; 1762230b1839SYOSHIFUJI Hideaki msg.msg_namelen = sizeof(address); 17631da177e4SLinus Torvalds if (sock->file->f_flags & O_NONBLOCK) 17641da177e4SLinus Torvalds flags |= MSG_DONTWAIT; 17651da177e4SLinus Torvalds err = sock_recvmsg(sock, &msg, size, flags); 17661da177e4SLinus Torvalds 176789bddce5SStephen Hemminger if (err >= 0 && addr != NULL) { 1768230b1839SYOSHIFUJI Hideaki err2 = move_addr_to_user((struct sockaddr *)&address, 1769230b1839SYOSHIFUJI Hideaki msg.msg_namelen, addr, addr_len); 17701da177e4SLinus Torvalds if (err2 < 0) 17711da177e4SLinus Torvalds err = err2; 17721da177e4SLinus Torvalds } 1773de0fa95cSPavel Emelyanov 1774de0fa95cSPavel Emelyanov fput_light(sock->file, fput_needed); 17754387ff75SDavid S. Miller out: 17761da177e4SLinus Torvalds return err; 17771da177e4SLinus Torvalds } 17781da177e4SLinus Torvalds 17791da177e4SLinus Torvalds /* 17801da177e4SLinus Torvalds * Receive a datagram from a socket. 17811da177e4SLinus Torvalds */ 17821da177e4SLinus Torvalds 178389bddce5SStephen Hemminger asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size, 178489bddce5SStephen Hemminger unsigned flags) 17851da177e4SLinus Torvalds { 17861da177e4SLinus Torvalds return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL); 17871da177e4SLinus Torvalds } 17881da177e4SLinus Torvalds 17891da177e4SLinus Torvalds /* 17901da177e4SLinus Torvalds * Set a socket option. Because we don't know the option lengths we have 17911da177e4SLinus Torvalds * to pass the user mode parameter for the protocols to sort out. 17921da177e4SLinus Torvalds */ 17931da177e4SLinus Torvalds 179420f37034SHeiko Carstens SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname, 179520f37034SHeiko Carstens char __user *, optval, int, optlen) 17961da177e4SLinus Torvalds { 17976cb153caSBenjamin LaHaise int err, fput_needed; 17981da177e4SLinus Torvalds struct socket *sock; 17991da177e4SLinus Torvalds 18001da177e4SLinus Torvalds if (optlen < 0) 18011da177e4SLinus Torvalds return -EINVAL; 18021da177e4SLinus Torvalds 180389bddce5SStephen Hemminger sock = sockfd_lookup_light(fd, &err, &fput_needed); 180489bddce5SStephen Hemminger if (sock != NULL) { 18051da177e4SLinus Torvalds err = security_socket_setsockopt(sock, level, optname); 18066cb153caSBenjamin LaHaise if (err) 18076cb153caSBenjamin LaHaise goto out_put; 18081da177e4SLinus Torvalds 18091da177e4SLinus Torvalds if (level == SOL_SOCKET) 181089bddce5SStephen Hemminger err = 181189bddce5SStephen Hemminger sock_setsockopt(sock, level, optname, optval, 181289bddce5SStephen Hemminger optlen); 18131da177e4SLinus Torvalds else 181489bddce5SStephen Hemminger err = 181589bddce5SStephen Hemminger sock->ops->setsockopt(sock, level, optname, optval, 181689bddce5SStephen Hemminger optlen); 18176cb153caSBenjamin LaHaise out_put: 18186cb153caSBenjamin LaHaise fput_light(sock->file, fput_needed); 18191da177e4SLinus Torvalds } 18201da177e4SLinus Torvalds return err; 18211da177e4SLinus Torvalds } 18221da177e4SLinus Torvalds 18231da177e4SLinus Torvalds /* 18241da177e4SLinus Torvalds * Get a socket option. Because we don't know the option lengths we have 18251da177e4SLinus Torvalds * to pass a user mode parameter for the protocols to sort out. 18261da177e4SLinus Torvalds */ 18271da177e4SLinus Torvalds 182820f37034SHeiko Carstens SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname, 182920f37034SHeiko Carstens char __user *, optval, int __user *, optlen) 18301da177e4SLinus Torvalds { 18316cb153caSBenjamin LaHaise int err, fput_needed; 18321da177e4SLinus Torvalds struct socket *sock; 18331da177e4SLinus Torvalds 183489bddce5SStephen Hemminger sock = sockfd_lookup_light(fd, &err, &fput_needed); 183589bddce5SStephen Hemminger if (sock != NULL) { 18366cb153caSBenjamin LaHaise err = security_socket_getsockopt(sock, level, optname); 18376cb153caSBenjamin LaHaise if (err) 18386cb153caSBenjamin LaHaise goto out_put; 18391da177e4SLinus Torvalds 18401da177e4SLinus Torvalds if (level == SOL_SOCKET) 184189bddce5SStephen Hemminger err = 184289bddce5SStephen Hemminger sock_getsockopt(sock, level, optname, optval, 184389bddce5SStephen Hemminger optlen); 18441da177e4SLinus Torvalds else 184589bddce5SStephen Hemminger err = 184689bddce5SStephen Hemminger sock->ops->getsockopt(sock, level, optname, optval, 184789bddce5SStephen Hemminger optlen); 18486cb153caSBenjamin LaHaise out_put: 18496cb153caSBenjamin LaHaise fput_light(sock->file, fput_needed); 18501da177e4SLinus Torvalds } 18511da177e4SLinus Torvalds return err; 18521da177e4SLinus Torvalds } 18531da177e4SLinus Torvalds 18541da177e4SLinus Torvalds /* 18551da177e4SLinus Torvalds * Shutdown a socket. 18561da177e4SLinus Torvalds */ 18571da177e4SLinus Torvalds 1858754fe8d2SHeiko Carstens SYSCALL_DEFINE2(shutdown, int, fd, int, how) 18591da177e4SLinus Torvalds { 18606cb153caSBenjamin LaHaise int err, fput_needed; 18611da177e4SLinus Torvalds struct socket *sock; 18621da177e4SLinus Torvalds 186389bddce5SStephen Hemminger sock = sockfd_lookup_light(fd, &err, &fput_needed); 186489bddce5SStephen Hemminger if (sock != NULL) { 18651da177e4SLinus Torvalds err = security_socket_shutdown(sock, how); 18666cb153caSBenjamin LaHaise if (!err) 18671da177e4SLinus Torvalds err = sock->ops->shutdown(sock, how); 18686cb153caSBenjamin LaHaise fput_light(sock->file, fput_needed); 18691da177e4SLinus Torvalds } 18701da177e4SLinus Torvalds return err; 18711da177e4SLinus Torvalds } 18721da177e4SLinus Torvalds 18731da177e4SLinus Torvalds /* A couple of helpful macros for getting the address of the 32/64 bit 18741da177e4SLinus Torvalds * fields which are the same type (int / unsigned) on our platforms. 18751da177e4SLinus Torvalds */ 18761da177e4SLinus Torvalds #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member) 18771da177e4SLinus Torvalds #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen) 18781da177e4SLinus Torvalds #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags) 18791da177e4SLinus Torvalds 18801da177e4SLinus Torvalds /* 18811da177e4SLinus Torvalds * BSD sendmsg interface 18821da177e4SLinus Torvalds */ 18831da177e4SLinus Torvalds 18843e0fa65fSHeiko Carstens SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned, flags) 18851da177e4SLinus Torvalds { 188689bddce5SStephen Hemminger struct compat_msghdr __user *msg_compat = 188789bddce5SStephen Hemminger (struct compat_msghdr __user *)msg; 18881da177e4SLinus Torvalds struct socket *sock; 1889230b1839SYOSHIFUJI Hideaki struct sockaddr_storage address; 18901da177e4SLinus Torvalds struct iovec iovstack[UIO_FASTIOV], *iov = iovstack; 1891b9d717a7SAlex Williamson unsigned char ctl[sizeof(struct cmsghdr) + 20] 1892b9d717a7SAlex Williamson __attribute__ ((aligned(sizeof(__kernel_size_t)))); 1893b9d717a7SAlex Williamson /* 20 is size of ipv6_pktinfo */ 18941da177e4SLinus Torvalds unsigned char *ctl_buf = ctl; 18951da177e4SLinus Torvalds struct msghdr msg_sys; 18961da177e4SLinus Torvalds int err, ctl_len, iov_size, total_len; 18976cb153caSBenjamin LaHaise int fput_needed; 18981da177e4SLinus Torvalds 18991da177e4SLinus Torvalds err = -EFAULT; 19001da177e4SLinus Torvalds if (MSG_CMSG_COMPAT & flags) { 19011da177e4SLinus Torvalds if (get_compat_msghdr(&msg_sys, msg_compat)) 19021da177e4SLinus Torvalds return -EFAULT; 190389bddce5SStephen Hemminger } 190489bddce5SStephen Hemminger else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr))) 19051da177e4SLinus Torvalds return -EFAULT; 19061da177e4SLinus Torvalds 19076cb153caSBenjamin LaHaise sock = sockfd_lookup_light(fd, &err, &fput_needed); 19081da177e4SLinus Torvalds if (!sock) 19091da177e4SLinus Torvalds goto out; 19101da177e4SLinus Torvalds 19111da177e4SLinus Torvalds /* do not move before msg_sys is valid */ 19121da177e4SLinus Torvalds err = -EMSGSIZE; 19131da177e4SLinus Torvalds if (msg_sys.msg_iovlen > UIO_MAXIOV) 19141da177e4SLinus Torvalds goto out_put; 19151da177e4SLinus Torvalds 19161da177e4SLinus Torvalds /* Check whether to allocate the iovec area */ 19171da177e4SLinus Torvalds err = -ENOMEM; 19181da177e4SLinus Torvalds iov_size = msg_sys.msg_iovlen * sizeof(struct iovec); 19191da177e4SLinus Torvalds if (msg_sys.msg_iovlen > UIO_FASTIOV) { 19201da177e4SLinus Torvalds iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL); 19211da177e4SLinus Torvalds if (!iov) 19221da177e4SLinus Torvalds goto out_put; 19231da177e4SLinus Torvalds } 19241da177e4SLinus Torvalds 19251da177e4SLinus Torvalds /* This will also move the address data into kernel space */ 19261da177e4SLinus Torvalds if (MSG_CMSG_COMPAT & flags) { 1927230b1839SYOSHIFUJI Hideaki err = verify_compat_iovec(&msg_sys, iov, 1928230b1839SYOSHIFUJI Hideaki (struct sockaddr *)&address, 1929230b1839SYOSHIFUJI Hideaki VERIFY_READ); 19301da177e4SLinus Torvalds } else 1931230b1839SYOSHIFUJI Hideaki err = verify_iovec(&msg_sys, iov, 1932230b1839SYOSHIFUJI Hideaki (struct sockaddr *)&address, 1933230b1839SYOSHIFUJI Hideaki VERIFY_READ); 19341da177e4SLinus Torvalds if (err < 0) 19351da177e4SLinus Torvalds goto out_freeiov; 19361da177e4SLinus Torvalds total_len = err; 19371da177e4SLinus Torvalds 19381da177e4SLinus Torvalds err = -ENOBUFS; 19391da177e4SLinus Torvalds 19401da177e4SLinus Torvalds if (msg_sys.msg_controllen > INT_MAX) 19411da177e4SLinus Torvalds goto out_freeiov; 19421da177e4SLinus Torvalds ctl_len = msg_sys.msg_controllen; 19431da177e4SLinus Torvalds if ((MSG_CMSG_COMPAT & flags) && ctl_len) { 194489bddce5SStephen Hemminger err = 194589bddce5SStephen Hemminger cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl, 194689bddce5SStephen Hemminger sizeof(ctl)); 19471da177e4SLinus Torvalds if (err) 19481da177e4SLinus Torvalds goto out_freeiov; 19491da177e4SLinus Torvalds ctl_buf = msg_sys.msg_control; 19508920e8f9SAl Viro ctl_len = msg_sys.msg_controllen; 19511da177e4SLinus Torvalds } else if (ctl_len) { 195289bddce5SStephen Hemminger if (ctl_len > sizeof(ctl)) { 19531da177e4SLinus Torvalds ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL); 19541da177e4SLinus Torvalds if (ctl_buf == NULL) 19551da177e4SLinus Torvalds goto out_freeiov; 19561da177e4SLinus Torvalds } 19571da177e4SLinus Torvalds err = -EFAULT; 19581da177e4SLinus Torvalds /* 19591da177e4SLinus Torvalds * Careful! Before this, msg_sys.msg_control contains a user pointer. 19601da177e4SLinus Torvalds * Afterwards, it will be a kernel pointer. Thus the compiler-assisted 19611da177e4SLinus Torvalds * checking falls down on this. 19621da177e4SLinus Torvalds */ 196389bddce5SStephen Hemminger if (copy_from_user(ctl_buf, (void __user *)msg_sys.msg_control, 196489bddce5SStephen Hemminger ctl_len)) 19651da177e4SLinus Torvalds goto out_freectl; 19661da177e4SLinus Torvalds msg_sys.msg_control = ctl_buf; 19671da177e4SLinus Torvalds } 19681da177e4SLinus Torvalds msg_sys.msg_flags = flags; 19691da177e4SLinus Torvalds 19701da177e4SLinus Torvalds if (sock->file->f_flags & O_NONBLOCK) 19711da177e4SLinus Torvalds msg_sys.msg_flags |= MSG_DONTWAIT; 19721da177e4SLinus Torvalds err = sock_sendmsg(sock, &msg_sys, total_len); 19731da177e4SLinus Torvalds 19741da177e4SLinus Torvalds out_freectl: 19751da177e4SLinus Torvalds if (ctl_buf != ctl) 19761da177e4SLinus Torvalds sock_kfree_s(sock->sk, ctl_buf, ctl_len); 19771da177e4SLinus Torvalds out_freeiov: 19781da177e4SLinus Torvalds if (iov != iovstack) 19791da177e4SLinus Torvalds sock_kfree_s(sock->sk, iov, iov_size); 19801da177e4SLinus Torvalds out_put: 19816cb153caSBenjamin LaHaise fput_light(sock->file, fput_needed); 19821da177e4SLinus Torvalds out: 19831da177e4SLinus Torvalds return err; 19841da177e4SLinus Torvalds } 19851da177e4SLinus Torvalds 19861da177e4SLinus Torvalds /* 19871da177e4SLinus Torvalds * BSD recvmsg interface 19881da177e4SLinus Torvalds */ 19891da177e4SLinus Torvalds 19903e0fa65fSHeiko Carstens SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg, 19913e0fa65fSHeiko Carstens unsigned int, flags) 19921da177e4SLinus Torvalds { 199389bddce5SStephen Hemminger struct compat_msghdr __user *msg_compat = 199489bddce5SStephen Hemminger (struct compat_msghdr __user *)msg; 19951da177e4SLinus Torvalds struct socket *sock; 19961da177e4SLinus Torvalds struct iovec iovstack[UIO_FASTIOV]; 19971da177e4SLinus Torvalds struct iovec *iov = iovstack; 19981da177e4SLinus Torvalds struct msghdr msg_sys; 19991da177e4SLinus Torvalds unsigned long cmsg_ptr; 20001da177e4SLinus Torvalds int err, iov_size, total_len, len; 20016cb153caSBenjamin LaHaise int fput_needed; 20021da177e4SLinus Torvalds 20031da177e4SLinus Torvalds /* kernel mode address */ 2004230b1839SYOSHIFUJI Hideaki struct sockaddr_storage addr; 20051da177e4SLinus Torvalds 20061da177e4SLinus Torvalds /* user mode address pointers */ 20071da177e4SLinus Torvalds struct sockaddr __user *uaddr; 20081da177e4SLinus Torvalds int __user *uaddr_len; 20091da177e4SLinus Torvalds 20101da177e4SLinus Torvalds if (MSG_CMSG_COMPAT & flags) { 20111da177e4SLinus Torvalds if (get_compat_msghdr(&msg_sys, msg_compat)) 20121da177e4SLinus Torvalds return -EFAULT; 201389bddce5SStephen Hemminger } 201489bddce5SStephen Hemminger else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr))) 20151da177e4SLinus Torvalds return -EFAULT; 20161da177e4SLinus Torvalds 20176cb153caSBenjamin LaHaise sock = sockfd_lookup_light(fd, &err, &fput_needed); 20181da177e4SLinus Torvalds if (!sock) 20191da177e4SLinus Torvalds goto out; 20201da177e4SLinus Torvalds 20211da177e4SLinus Torvalds err = -EMSGSIZE; 20221da177e4SLinus Torvalds if (msg_sys.msg_iovlen > UIO_MAXIOV) 20231da177e4SLinus Torvalds goto out_put; 20241da177e4SLinus Torvalds 20251da177e4SLinus Torvalds /* Check whether to allocate the iovec area */ 20261da177e4SLinus Torvalds err = -ENOMEM; 20271da177e4SLinus Torvalds iov_size = msg_sys.msg_iovlen * sizeof(struct iovec); 20281da177e4SLinus Torvalds if (msg_sys.msg_iovlen > UIO_FASTIOV) { 20291da177e4SLinus Torvalds iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL); 20301da177e4SLinus Torvalds if (!iov) 20311da177e4SLinus Torvalds goto out_put; 20321da177e4SLinus Torvalds } 20331da177e4SLinus Torvalds 20341da177e4SLinus Torvalds /* 20351da177e4SLinus Torvalds * Save the user-mode address (verify_iovec will change the 20361da177e4SLinus Torvalds * kernel msghdr to use the kernel address space) 20371da177e4SLinus Torvalds */ 20381da177e4SLinus Torvalds 2039cfcabdccSStephen Hemminger uaddr = (__force void __user *)msg_sys.msg_name; 20401da177e4SLinus Torvalds uaddr_len = COMPAT_NAMELEN(msg); 20411da177e4SLinus Torvalds if (MSG_CMSG_COMPAT & flags) { 2042230b1839SYOSHIFUJI Hideaki err = verify_compat_iovec(&msg_sys, iov, 2043230b1839SYOSHIFUJI Hideaki (struct sockaddr *)&addr, 2044230b1839SYOSHIFUJI Hideaki VERIFY_WRITE); 20451da177e4SLinus Torvalds } else 2046230b1839SYOSHIFUJI Hideaki err = verify_iovec(&msg_sys, iov, 2047230b1839SYOSHIFUJI Hideaki (struct sockaddr *)&addr, 2048230b1839SYOSHIFUJI Hideaki VERIFY_WRITE); 20491da177e4SLinus Torvalds if (err < 0) 20501da177e4SLinus Torvalds goto out_freeiov; 20511da177e4SLinus Torvalds total_len = err; 20521da177e4SLinus Torvalds 20531da177e4SLinus Torvalds cmsg_ptr = (unsigned long)msg_sys.msg_control; 20544a19542eSUlrich Drepper msg_sys.msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT); 20551da177e4SLinus Torvalds 20561da177e4SLinus Torvalds if (sock->file->f_flags & O_NONBLOCK) 20571da177e4SLinus Torvalds flags |= MSG_DONTWAIT; 20581da177e4SLinus Torvalds err = sock_recvmsg(sock, &msg_sys, total_len, flags); 20591da177e4SLinus Torvalds if (err < 0) 20601da177e4SLinus Torvalds goto out_freeiov; 20611da177e4SLinus Torvalds len = err; 20621da177e4SLinus Torvalds 20631da177e4SLinus Torvalds if (uaddr != NULL) { 2064230b1839SYOSHIFUJI Hideaki err = move_addr_to_user((struct sockaddr *)&addr, 2065230b1839SYOSHIFUJI Hideaki msg_sys.msg_namelen, uaddr, 206689bddce5SStephen Hemminger uaddr_len); 20671da177e4SLinus Torvalds if (err < 0) 20681da177e4SLinus Torvalds goto out_freeiov; 20691da177e4SLinus Torvalds } 207037f7f421SDavid S. Miller err = __put_user((msg_sys.msg_flags & ~MSG_CMSG_COMPAT), 207137f7f421SDavid S. Miller COMPAT_FLAGS(msg)); 20721da177e4SLinus Torvalds if (err) 20731da177e4SLinus Torvalds goto out_freeiov; 20741da177e4SLinus Torvalds if (MSG_CMSG_COMPAT & flags) 20751da177e4SLinus Torvalds err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr, 20761da177e4SLinus Torvalds &msg_compat->msg_controllen); 20771da177e4SLinus Torvalds else 20781da177e4SLinus Torvalds err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr, 20791da177e4SLinus Torvalds &msg->msg_controllen); 20801da177e4SLinus Torvalds if (err) 20811da177e4SLinus Torvalds goto out_freeiov; 20821da177e4SLinus Torvalds err = len; 20831da177e4SLinus Torvalds 20841da177e4SLinus Torvalds out_freeiov: 20851da177e4SLinus Torvalds if (iov != iovstack) 20861da177e4SLinus Torvalds sock_kfree_s(sock->sk, iov, iov_size); 20871da177e4SLinus Torvalds out_put: 20886cb153caSBenjamin LaHaise fput_light(sock->file, fput_needed); 20891da177e4SLinus Torvalds out: 20901da177e4SLinus Torvalds return err; 20911da177e4SLinus Torvalds } 20921da177e4SLinus Torvalds 20931da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_SOCKETCALL 20941da177e4SLinus Torvalds 20951da177e4SLinus Torvalds /* Argument list sizes for sys_socketcall */ 20961da177e4SLinus Torvalds #define AL(x) ((x) * sizeof(unsigned long)) 2097aaca0bdcSUlrich Drepper static const unsigned char nargs[19]={ 209889bddce5SStephen Hemminger AL(0),AL(3),AL(3),AL(3),AL(2),AL(3), 20991da177e4SLinus Torvalds AL(3),AL(3),AL(4),AL(4),AL(4),AL(6), 2100aaca0bdcSUlrich Drepper AL(6),AL(2),AL(5),AL(5),AL(3),AL(3), 2101de11defeSUlrich Drepper AL(4) 210289bddce5SStephen Hemminger }; 210389bddce5SStephen Hemminger 21041da177e4SLinus Torvalds #undef AL 21051da177e4SLinus Torvalds 21061da177e4SLinus Torvalds /* 21071da177e4SLinus Torvalds * System call vectors. 21081da177e4SLinus Torvalds * 21091da177e4SLinus Torvalds * Argument checking cleaned up. Saved 20% in size. 21101da177e4SLinus Torvalds * This function doesn't need to set the kernel lock because 21111da177e4SLinus Torvalds * it is set by the callees. 21121da177e4SLinus Torvalds */ 21131da177e4SLinus Torvalds 21143e0fa65fSHeiko Carstens SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args) 21151da177e4SLinus Torvalds { 21161da177e4SLinus Torvalds unsigned long a[6]; 21171da177e4SLinus Torvalds unsigned long a0, a1; 21181da177e4SLinus Torvalds int err; 211947379052SArjan van de Ven unsigned int len; 21201da177e4SLinus Torvalds 2121de11defeSUlrich Drepper if (call < 1 || call > SYS_ACCEPT4) 21221da177e4SLinus Torvalds return -EINVAL; 21231da177e4SLinus Torvalds 212447379052SArjan van de Ven len = nargs[call]; 212547379052SArjan van de Ven if (len > sizeof(a)) 212647379052SArjan van de Ven return -EINVAL; 212747379052SArjan van de Ven 21281da177e4SLinus Torvalds /* copy_from_user should be SMP safe. */ 212947379052SArjan van de Ven if (copy_from_user(a, args, len)) 21301da177e4SLinus Torvalds return -EFAULT; 21311da177e4SLinus Torvalds 2132f3298dc4SAl Viro audit_socketcall(nargs[call] / sizeof(unsigned long), a); 21333ec3b2fbSDavid Woodhouse 21341da177e4SLinus Torvalds a0 = a[0]; 21351da177e4SLinus Torvalds a1 = a[1]; 21361da177e4SLinus Torvalds 213789bddce5SStephen Hemminger switch (call) { 21381da177e4SLinus Torvalds case SYS_SOCKET: 21391da177e4SLinus Torvalds err = sys_socket(a0, a1, a[2]); 21401da177e4SLinus Torvalds break; 21411da177e4SLinus Torvalds case SYS_BIND: 21421da177e4SLinus Torvalds err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]); 21431da177e4SLinus Torvalds break; 21441da177e4SLinus Torvalds case SYS_CONNECT: 21451da177e4SLinus Torvalds err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]); 21461da177e4SLinus Torvalds break; 21471da177e4SLinus Torvalds case SYS_LISTEN: 21481da177e4SLinus Torvalds err = sys_listen(a0, a1); 21491da177e4SLinus Torvalds break; 21501da177e4SLinus Torvalds case SYS_ACCEPT: 2151de11defeSUlrich Drepper err = sys_accept4(a0, (struct sockaddr __user *)a1, 2152aaca0bdcSUlrich Drepper (int __user *)a[2], 0); 21531da177e4SLinus Torvalds break; 21541da177e4SLinus Torvalds case SYS_GETSOCKNAME: 215589bddce5SStephen Hemminger err = 215689bddce5SStephen Hemminger sys_getsockname(a0, (struct sockaddr __user *)a1, 215789bddce5SStephen Hemminger (int __user *)a[2]); 21581da177e4SLinus Torvalds break; 21591da177e4SLinus Torvalds case SYS_GETPEERNAME: 216089bddce5SStephen Hemminger err = 216189bddce5SStephen Hemminger sys_getpeername(a0, (struct sockaddr __user *)a1, 216289bddce5SStephen Hemminger (int __user *)a[2]); 21631da177e4SLinus Torvalds break; 21641da177e4SLinus Torvalds case SYS_SOCKETPAIR: 21651da177e4SLinus Torvalds err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]); 21661da177e4SLinus Torvalds break; 21671da177e4SLinus Torvalds case SYS_SEND: 21681da177e4SLinus Torvalds err = sys_send(a0, (void __user *)a1, a[2], a[3]); 21691da177e4SLinus Torvalds break; 21701da177e4SLinus Torvalds case SYS_SENDTO: 21711da177e4SLinus Torvalds err = sys_sendto(a0, (void __user *)a1, a[2], a[3], 21721da177e4SLinus Torvalds (struct sockaddr __user *)a[4], a[5]); 21731da177e4SLinus Torvalds break; 21741da177e4SLinus Torvalds case SYS_RECV: 21751da177e4SLinus Torvalds err = sys_recv(a0, (void __user *)a1, a[2], a[3]); 21761da177e4SLinus Torvalds break; 21771da177e4SLinus Torvalds case SYS_RECVFROM: 21781da177e4SLinus Torvalds err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3], 217989bddce5SStephen Hemminger (struct sockaddr __user *)a[4], 218089bddce5SStephen Hemminger (int __user *)a[5]); 21811da177e4SLinus Torvalds break; 21821da177e4SLinus Torvalds case SYS_SHUTDOWN: 21831da177e4SLinus Torvalds err = sys_shutdown(a0, a1); 21841da177e4SLinus Torvalds break; 21851da177e4SLinus Torvalds case SYS_SETSOCKOPT: 21861da177e4SLinus Torvalds err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]); 21871da177e4SLinus Torvalds break; 21881da177e4SLinus Torvalds case SYS_GETSOCKOPT: 218989bddce5SStephen Hemminger err = 219089bddce5SStephen Hemminger sys_getsockopt(a0, a1, a[2], (char __user *)a[3], 219189bddce5SStephen Hemminger (int __user *)a[4]); 21921da177e4SLinus Torvalds break; 21931da177e4SLinus Torvalds case SYS_SENDMSG: 21941da177e4SLinus Torvalds err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]); 21951da177e4SLinus Torvalds break; 21961da177e4SLinus Torvalds case SYS_RECVMSG: 21971da177e4SLinus Torvalds err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]); 21981da177e4SLinus Torvalds break; 2199de11defeSUlrich Drepper case SYS_ACCEPT4: 2200de11defeSUlrich Drepper err = sys_accept4(a0, (struct sockaddr __user *)a1, 2201de11defeSUlrich Drepper (int __user *)a[2], a[3]); 2202aaca0bdcSUlrich Drepper break; 22031da177e4SLinus Torvalds default: 22041da177e4SLinus Torvalds err = -EINVAL; 22051da177e4SLinus Torvalds break; 22061da177e4SLinus Torvalds } 22071da177e4SLinus Torvalds return err; 22081da177e4SLinus Torvalds } 22091da177e4SLinus Torvalds 22101da177e4SLinus Torvalds #endif /* __ARCH_WANT_SYS_SOCKETCALL */ 22111da177e4SLinus Torvalds 221255737fdaSStephen Hemminger /** 221355737fdaSStephen Hemminger * sock_register - add a socket protocol handler 221455737fdaSStephen Hemminger * @ops: description of protocol 221555737fdaSStephen Hemminger * 22161da177e4SLinus Torvalds * This function is called by a protocol handler that wants to 22171da177e4SLinus Torvalds * advertise its address family, and have it linked into the 221855737fdaSStephen Hemminger * socket interface. The value ops->family coresponds to the 221955737fdaSStephen Hemminger * socket system call protocol family. 22201da177e4SLinus Torvalds */ 2221f0fd27d4SStephen Hemminger int sock_register(const struct net_proto_family *ops) 22221da177e4SLinus Torvalds { 22231da177e4SLinus Torvalds int err; 22241da177e4SLinus Torvalds 22251da177e4SLinus Torvalds if (ops->family >= NPROTO) { 222689bddce5SStephen Hemminger printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family, 222789bddce5SStephen Hemminger NPROTO); 22281da177e4SLinus Torvalds return -ENOBUFS; 22291da177e4SLinus Torvalds } 223055737fdaSStephen Hemminger 223155737fdaSStephen Hemminger spin_lock(&net_family_lock); 223255737fdaSStephen Hemminger if (net_families[ops->family]) 22331da177e4SLinus Torvalds err = -EEXIST; 223455737fdaSStephen Hemminger else { 22351da177e4SLinus Torvalds net_families[ops->family] = ops; 22361da177e4SLinus Torvalds err = 0; 22371da177e4SLinus Torvalds } 223855737fdaSStephen Hemminger spin_unlock(&net_family_lock); 223955737fdaSStephen Hemminger 224089bddce5SStephen Hemminger printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family); 22411da177e4SLinus Torvalds return err; 22421da177e4SLinus Torvalds } 22431da177e4SLinus Torvalds 224455737fdaSStephen Hemminger /** 224555737fdaSStephen Hemminger * sock_unregister - remove a protocol handler 224655737fdaSStephen Hemminger * @family: protocol family to remove 224755737fdaSStephen Hemminger * 22481da177e4SLinus Torvalds * This function is called by a protocol handler that wants to 22491da177e4SLinus Torvalds * remove its address family, and have it unlinked from the 225055737fdaSStephen Hemminger * new socket creation. 225155737fdaSStephen Hemminger * 225255737fdaSStephen Hemminger * If protocol handler is a module, then it can use module reference 225355737fdaSStephen Hemminger * counts to protect against new references. If protocol handler is not 225455737fdaSStephen Hemminger * a module then it needs to provide its own protection in 225555737fdaSStephen Hemminger * the ops->create routine. 22561da177e4SLinus Torvalds */ 2257f0fd27d4SStephen Hemminger void sock_unregister(int family) 22581da177e4SLinus Torvalds { 2259f0fd27d4SStephen Hemminger BUG_ON(family < 0 || family >= NPROTO); 22601da177e4SLinus Torvalds 226155737fdaSStephen Hemminger spin_lock(&net_family_lock); 22621da177e4SLinus Torvalds net_families[family] = NULL; 226355737fdaSStephen Hemminger spin_unlock(&net_family_lock); 226455737fdaSStephen Hemminger 226555737fdaSStephen Hemminger synchronize_rcu(); 226655737fdaSStephen Hemminger 226789bddce5SStephen Hemminger printk(KERN_INFO "NET: Unregistered protocol family %d\n", family); 22681da177e4SLinus Torvalds } 22691da177e4SLinus Torvalds 227077d76ea3SAndi Kleen static int __init sock_init(void) 22711da177e4SLinus Torvalds { 22721da177e4SLinus Torvalds /* 22731da177e4SLinus Torvalds * Initialize sock SLAB cache. 22741da177e4SLinus Torvalds */ 22751da177e4SLinus Torvalds 22761da177e4SLinus Torvalds sk_init(); 22771da177e4SLinus Torvalds 22781da177e4SLinus Torvalds /* 22791da177e4SLinus Torvalds * Initialize skbuff SLAB cache 22801da177e4SLinus Torvalds */ 22811da177e4SLinus Torvalds skb_init(); 22821da177e4SLinus Torvalds 22831da177e4SLinus Torvalds /* 22841da177e4SLinus Torvalds * Initialize the protocols module. 22851da177e4SLinus Torvalds */ 22861da177e4SLinus Torvalds 22871da177e4SLinus Torvalds init_inodecache(); 22881da177e4SLinus Torvalds register_filesystem(&sock_fs_type); 22891da177e4SLinus Torvalds sock_mnt = kern_mount(&sock_fs_type); 229077d76ea3SAndi Kleen 229177d76ea3SAndi Kleen /* The real protocol initialization is performed in later initcalls. 22921da177e4SLinus Torvalds */ 22931da177e4SLinus Torvalds 22941da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER 22951da177e4SLinus Torvalds netfilter_init(); 22961da177e4SLinus Torvalds #endif 2297cbeb321aSDavid S. Miller 2298cbeb321aSDavid S. Miller return 0; 22991da177e4SLinus Torvalds } 23001da177e4SLinus Torvalds 230177d76ea3SAndi Kleen core_initcall(sock_init); /* early initcall */ 230277d76ea3SAndi Kleen 23031da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 23041da177e4SLinus Torvalds void socket_seq_show(struct seq_file *seq) 23051da177e4SLinus Torvalds { 23061da177e4SLinus Torvalds int cpu; 23071da177e4SLinus Torvalds int counter = 0; 23081da177e4SLinus Torvalds 23096f912042SKAMEZAWA Hiroyuki for_each_possible_cpu(cpu) 23101da177e4SLinus Torvalds counter += per_cpu(sockets_in_use, cpu); 23111da177e4SLinus Torvalds 23121da177e4SLinus Torvalds /* It can be negative, by the way. 8) */ 23131da177e4SLinus Torvalds if (counter < 0) 23141da177e4SLinus Torvalds counter = 0; 23151da177e4SLinus Torvalds 23161da177e4SLinus Torvalds seq_printf(seq, "sockets: used %d\n", counter); 23171da177e4SLinus Torvalds } 23181da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 23191da177e4SLinus Torvalds 232089bbfc95SShaun Pereira #ifdef CONFIG_COMPAT 232189bbfc95SShaun Pereira static long compat_sock_ioctl(struct file *file, unsigned cmd, 232289bbfc95SShaun Pereira unsigned long arg) 232389bbfc95SShaun Pereira { 232489bbfc95SShaun Pereira struct socket *sock = file->private_data; 232589bbfc95SShaun Pereira int ret = -ENOIOCTLCMD; 232687de87d5SDavid S. Miller struct sock *sk; 232787de87d5SDavid S. Miller struct net *net; 232887de87d5SDavid S. Miller 232987de87d5SDavid S. Miller sk = sock->sk; 233087de87d5SDavid S. Miller net = sock_net(sk); 233189bbfc95SShaun Pereira 233289bbfc95SShaun Pereira if (sock->ops->compat_ioctl) 233389bbfc95SShaun Pereira ret = sock->ops->compat_ioctl(sock, cmd, arg); 233489bbfc95SShaun Pereira 233587de87d5SDavid S. Miller if (ret == -ENOIOCTLCMD && 233687de87d5SDavid S. Miller (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST)) 233787de87d5SDavid S. Miller ret = compat_wext_handle_ioctl(net, cmd, arg); 233887de87d5SDavid S. Miller 233989bbfc95SShaun Pereira return ret; 234089bbfc95SShaun Pereira } 234189bbfc95SShaun Pereira #endif 234289bbfc95SShaun Pereira 2343ac5a488eSSridhar Samudrala int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen) 2344ac5a488eSSridhar Samudrala { 2345ac5a488eSSridhar Samudrala return sock->ops->bind(sock, addr, addrlen); 2346ac5a488eSSridhar Samudrala } 2347ac5a488eSSridhar Samudrala 2348ac5a488eSSridhar Samudrala int kernel_listen(struct socket *sock, int backlog) 2349ac5a488eSSridhar Samudrala { 2350ac5a488eSSridhar Samudrala return sock->ops->listen(sock, backlog); 2351ac5a488eSSridhar Samudrala } 2352ac5a488eSSridhar Samudrala 2353ac5a488eSSridhar Samudrala int kernel_accept(struct socket *sock, struct socket **newsock, int flags) 2354ac5a488eSSridhar Samudrala { 2355ac5a488eSSridhar Samudrala struct sock *sk = sock->sk; 2356ac5a488eSSridhar Samudrala int err; 2357ac5a488eSSridhar Samudrala 2358ac5a488eSSridhar Samudrala err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol, 2359ac5a488eSSridhar Samudrala newsock); 2360ac5a488eSSridhar Samudrala if (err < 0) 2361ac5a488eSSridhar Samudrala goto done; 2362ac5a488eSSridhar Samudrala 2363ac5a488eSSridhar Samudrala err = sock->ops->accept(sock, *newsock, flags); 2364ac5a488eSSridhar Samudrala if (err < 0) { 2365ac5a488eSSridhar Samudrala sock_release(*newsock); 2366fa8705b0STony Battersby *newsock = NULL; 2367ac5a488eSSridhar Samudrala goto done; 2368ac5a488eSSridhar Samudrala } 2369ac5a488eSSridhar Samudrala 2370ac5a488eSSridhar Samudrala (*newsock)->ops = sock->ops; 23711b08534eSWei Yongjun __module_get((*newsock)->ops->owner); 2372ac5a488eSSridhar Samudrala 2373ac5a488eSSridhar Samudrala done: 2374ac5a488eSSridhar Samudrala return err; 2375ac5a488eSSridhar Samudrala } 2376ac5a488eSSridhar Samudrala 2377ac5a488eSSridhar Samudrala int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen, 2378ac5a488eSSridhar Samudrala int flags) 2379ac5a488eSSridhar Samudrala { 2380ac5a488eSSridhar Samudrala return sock->ops->connect(sock, addr, addrlen, flags); 2381ac5a488eSSridhar Samudrala } 2382ac5a488eSSridhar Samudrala 2383ac5a488eSSridhar Samudrala int kernel_getsockname(struct socket *sock, struct sockaddr *addr, 2384ac5a488eSSridhar Samudrala int *addrlen) 2385ac5a488eSSridhar Samudrala { 2386ac5a488eSSridhar Samudrala return sock->ops->getname(sock, addr, addrlen, 0); 2387ac5a488eSSridhar Samudrala } 2388ac5a488eSSridhar Samudrala 2389ac5a488eSSridhar Samudrala int kernel_getpeername(struct socket *sock, struct sockaddr *addr, 2390ac5a488eSSridhar Samudrala int *addrlen) 2391ac5a488eSSridhar Samudrala { 2392ac5a488eSSridhar Samudrala return sock->ops->getname(sock, addr, addrlen, 1); 2393ac5a488eSSridhar Samudrala } 2394ac5a488eSSridhar Samudrala 2395ac5a488eSSridhar Samudrala int kernel_getsockopt(struct socket *sock, int level, int optname, 2396ac5a488eSSridhar Samudrala char *optval, int *optlen) 2397ac5a488eSSridhar Samudrala { 2398ac5a488eSSridhar Samudrala mm_segment_t oldfs = get_fs(); 2399ac5a488eSSridhar Samudrala int err; 2400ac5a488eSSridhar Samudrala 2401ac5a488eSSridhar Samudrala set_fs(KERNEL_DS); 2402ac5a488eSSridhar Samudrala if (level == SOL_SOCKET) 2403ac5a488eSSridhar Samudrala err = sock_getsockopt(sock, level, optname, optval, optlen); 2404ac5a488eSSridhar Samudrala else 2405ac5a488eSSridhar Samudrala err = sock->ops->getsockopt(sock, level, optname, optval, 2406ac5a488eSSridhar Samudrala optlen); 2407ac5a488eSSridhar Samudrala set_fs(oldfs); 2408ac5a488eSSridhar Samudrala return err; 2409ac5a488eSSridhar Samudrala } 2410ac5a488eSSridhar Samudrala 2411ac5a488eSSridhar Samudrala int kernel_setsockopt(struct socket *sock, int level, int optname, 2412b7058842SDavid S. Miller char *optval, unsigned int optlen) 2413ac5a488eSSridhar Samudrala { 2414ac5a488eSSridhar Samudrala mm_segment_t oldfs = get_fs(); 2415ac5a488eSSridhar Samudrala int err; 2416ac5a488eSSridhar Samudrala 2417ac5a488eSSridhar Samudrala set_fs(KERNEL_DS); 2418ac5a488eSSridhar Samudrala if (level == SOL_SOCKET) 2419ac5a488eSSridhar Samudrala err = sock_setsockopt(sock, level, optname, optval, optlen); 2420ac5a488eSSridhar Samudrala else 2421ac5a488eSSridhar Samudrala err = sock->ops->setsockopt(sock, level, optname, optval, 2422ac5a488eSSridhar Samudrala optlen); 2423ac5a488eSSridhar Samudrala set_fs(oldfs); 2424ac5a488eSSridhar Samudrala return err; 2425ac5a488eSSridhar Samudrala } 2426ac5a488eSSridhar Samudrala 2427ac5a488eSSridhar Samudrala int kernel_sendpage(struct socket *sock, struct page *page, int offset, 2428ac5a488eSSridhar Samudrala size_t size, int flags) 2429ac5a488eSSridhar Samudrala { 2430ac5a488eSSridhar Samudrala if (sock->ops->sendpage) 2431ac5a488eSSridhar Samudrala return sock->ops->sendpage(sock, page, offset, size, flags); 2432ac5a488eSSridhar Samudrala 2433ac5a488eSSridhar Samudrala return sock_no_sendpage(sock, page, offset, size, flags); 2434ac5a488eSSridhar Samudrala } 2435ac5a488eSSridhar Samudrala 2436ac5a488eSSridhar Samudrala int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg) 2437ac5a488eSSridhar Samudrala { 2438ac5a488eSSridhar Samudrala mm_segment_t oldfs = get_fs(); 2439ac5a488eSSridhar Samudrala int err; 2440ac5a488eSSridhar Samudrala 2441ac5a488eSSridhar Samudrala set_fs(KERNEL_DS); 2442ac5a488eSSridhar Samudrala err = sock->ops->ioctl(sock, cmd, arg); 2443ac5a488eSSridhar Samudrala set_fs(oldfs); 2444ac5a488eSSridhar Samudrala 2445ac5a488eSSridhar Samudrala return err; 2446ac5a488eSSridhar Samudrala } 2447ac5a488eSSridhar Samudrala 244891cf45f0STrond Myklebust int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how) 244991cf45f0STrond Myklebust { 245091cf45f0STrond Myklebust return sock->ops->shutdown(sock, how); 245191cf45f0STrond Myklebust } 245291cf45f0STrond Myklebust 24531da177e4SLinus Torvalds EXPORT_SYMBOL(sock_create); 24541da177e4SLinus Torvalds EXPORT_SYMBOL(sock_create_kern); 24551da177e4SLinus Torvalds EXPORT_SYMBOL(sock_create_lite); 24561da177e4SLinus Torvalds EXPORT_SYMBOL(sock_map_fd); 24571da177e4SLinus Torvalds EXPORT_SYMBOL(sock_recvmsg); 24581da177e4SLinus Torvalds EXPORT_SYMBOL(sock_register); 24591da177e4SLinus Torvalds EXPORT_SYMBOL(sock_release); 24601da177e4SLinus Torvalds EXPORT_SYMBOL(sock_sendmsg); 24611da177e4SLinus Torvalds EXPORT_SYMBOL(sock_unregister); 24621da177e4SLinus Torvalds EXPORT_SYMBOL(sock_wake_async); 24631da177e4SLinus Torvalds EXPORT_SYMBOL(sockfd_lookup); 24641da177e4SLinus Torvalds EXPORT_SYMBOL(kernel_sendmsg); 24651da177e4SLinus Torvalds EXPORT_SYMBOL(kernel_recvmsg); 2466ac5a488eSSridhar Samudrala EXPORT_SYMBOL(kernel_bind); 2467ac5a488eSSridhar Samudrala EXPORT_SYMBOL(kernel_listen); 2468ac5a488eSSridhar Samudrala EXPORT_SYMBOL(kernel_accept); 2469ac5a488eSSridhar Samudrala EXPORT_SYMBOL(kernel_connect); 2470ac5a488eSSridhar Samudrala EXPORT_SYMBOL(kernel_getsockname); 2471ac5a488eSSridhar Samudrala EXPORT_SYMBOL(kernel_getpeername); 2472ac5a488eSSridhar Samudrala EXPORT_SYMBOL(kernel_getsockopt); 2473ac5a488eSSridhar Samudrala EXPORT_SYMBOL(kernel_setsockopt); 2474ac5a488eSSridhar Samudrala EXPORT_SYMBOL(kernel_sendpage); 2475ac5a488eSSridhar Samudrala EXPORT_SYMBOL(kernel_sock_ioctl); 247691cf45f0STrond Myklebust EXPORT_SYMBOL(kernel_sock_shutdown); 2477