[PATCH] DVB: Documentation and Kconfig updazes
[linux-2.6/history.git] / net / socket.c
blob312de3b6b45c4acb48cc6271576333db82469779
1 /*
2 * NET An implementation of the SOCKET network access protocol.
4 * Version: @(#)socket.c 1.1.93 18/02/95
6 * Authors: Orest Zborowski, <obz@Kodak.COM>
7 * Ross Biro, <bir7@leland.Stanford.Edu>
8 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Fixes:
11 * Anonymous : NOTSOCK/BADF cleanup. Error fix in
12 * shutdown()
13 * Alan Cox : verify_area() fixes
14 * Alan Cox : Removed DDI
15 * Jonathan Kamens : SOCK_DGRAM reconnect bug
16 * Alan Cox : Moved a load of checks to the very
17 * top level.
18 * Alan Cox : Move address structures to/from user
19 * mode above the protocol layers.
20 * Rob Janssen : Allow 0 length sends.
21 * Alan Cox : Asynchronous I/O support (cribbed from the
22 * tty drivers).
23 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
24 * Jeff Uphoff : Made max number of sockets command-line
25 * configurable.
26 * Matti Aarnio : Made the number of sockets dynamic,
27 * to be allocated when needed, and mr.
28 * Uphoff's max is used as max to be
29 * allowed to allocate.
30 * Linus : Argh. removed all the socket allocation
31 * altogether: it's in the inode now.
32 * Alan Cox : Made sock_alloc()/sock_release() public
33 * for NetROM and future kernel nfsd type
34 * stuff.
35 * Alan Cox : sendmsg/recvmsg basics.
36 * Tom Dyas : Export net symbols.
37 * Marcin Dalecki : Fixed problems with CONFIG_NET="n".
38 * Alan Cox : Added thread locking to sys_* calls
39 * for sockets. May have errors at the
40 * moment.
41 * Kevin Buhr : Fixed the dumb errors in the above.
42 * Andi Kleen : Some small cleanups, optimizations,
43 * and fixed a copy_from_user() bug.
44 * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
45 * Tigran Aivazian : Made listen(2) backlog sanity checks
46 * protocol-independent
49 * This program is free software; you can redistribute it and/or
50 * modify it under the terms of the GNU General Public License
51 * as published by the Free Software Foundation; either version
52 * 2 of the License, or (at your option) any later version.
55 * This module is effectively the top level interface to the BSD socket
56 * paradigm.
58 * Based upon Swansea University Computer Society NET3.039
61 #include <linux/config.h>
62 #include <linux/mm.h>
63 #include <linux/smp_lock.h>
64 #include <linux/socket.h>
65 #include <linux/file.h>
66 #include <linux/net.h>
67 #include <linux/interrupt.h>
68 #include <linux/netdevice.h>
69 #include <linux/proc_fs.h>
70 #include <linux/seq_file.h>
71 #include <linux/wanrouter.h>
72 #include <linux/if_bridge.h>
73 #include <linux/init.h>
74 #include <linux/poll.h>
75 #include <linux/cache.h>
76 #include <linux/module.h>
77 #include <linux/highmem.h>
78 #include <linux/divert.h>
79 #include <linux/mount.h>
80 #include <linux/security.h>
81 #include <linux/syscalls.h>
82 #include <linux/compat.h>
83 #include <linux/kmod.h>
85 #ifdef CONFIG_NET_RADIO
86 #include <linux/wireless.h> /* Note : will define WIRELESS_EXT */
87 #endif /* CONFIG_NET_RADIO */
89 #include <asm/uaccess.h>
90 #include <net/compat.h>
92 #include <net/sock.h>
93 #include <linux/netfilter.h>
95 static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
96 static ssize_t sock_aio_read(struct kiocb *iocb, char __user *buf,
97 size_t size, loff_t pos);
98 static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *buf,
99 size_t size, loff_t pos);
100 static int sock_mmap(struct file *file, struct vm_area_struct * vma);
102 static int sock_close(struct inode *inode, struct file *file);
103 static unsigned int sock_poll(struct file *file,
104 struct poll_table_struct *wait);
105 static int sock_ioctl(struct inode *inode, struct file *file,
106 unsigned int cmd, unsigned long arg);
107 static int sock_fasync(int fd, struct file *filp, int on);
108 static ssize_t sock_readv(struct file *file, const struct iovec *vector,
109 unsigned long count, loff_t *ppos);
110 static ssize_t sock_writev(struct file *file, const struct iovec *vector,
111 unsigned long count, loff_t *ppos);
112 static ssize_t sock_sendpage(struct file *file, struct page *page,
113 int offset, size_t size, loff_t *ppos, int more);
117 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
118 * in the operation structures but are done directly via the socketcall() multiplexor.
121 static struct file_operations socket_file_ops = {
122 .owner = THIS_MODULE,
123 .llseek = no_llseek,
124 .aio_read = sock_aio_read,
125 .aio_write = sock_aio_write,
126 .poll = sock_poll,
127 .ioctl = sock_ioctl,
128 .mmap = sock_mmap,
129 .open = sock_no_open, /* special open code to disallow open via /proc */
130 .release = sock_close,
131 .fasync = sock_fasync,
132 .readv = sock_readv,
133 .writev = sock_writev,
134 .sendpage = sock_sendpage
138 * The protocol list. Each protocol is registered in here.
141 static struct net_proto_family *net_families[NPROTO];
143 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
144 static atomic_t net_family_lockct = ATOMIC_INIT(0);
145 static spinlock_t net_family_lock = SPIN_LOCK_UNLOCKED;
147 /* The strategy is: modifications net_family vector are short, do not
148 sleep and veeery rare, but read access should be free of any exclusive
149 locks.
152 static void net_family_write_lock(void)
154 spin_lock(&net_family_lock);
155 while (atomic_read(&net_family_lockct) != 0) {
156 spin_unlock(&net_family_lock);
158 yield();
160 spin_lock(&net_family_lock);
164 static __inline__ void net_family_write_unlock(void)
166 spin_unlock(&net_family_lock);
169 static __inline__ void net_family_read_lock(void)
171 atomic_inc(&net_family_lockct);
172 spin_unlock_wait(&net_family_lock);
175 static __inline__ void net_family_read_unlock(void)
177 atomic_dec(&net_family_lockct);
180 #else
181 #define net_family_write_lock() do { } while(0)
182 #define net_family_write_unlock() do { } while(0)
183 #define net_family_read_lock() do { } while(0)
184 #define net_family_read_unlock() do { } while(0)
185 #endif
189 * Statistics counters of the socket lists
192 static DEFINE_PER_CPU(int, sockets_in_use) = 0;
195 * Support routines. Move socket addresses back and forth across the kernel/user
196 * divide and look after the messy bits.
199 #define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
200 16 for IP, 16 for IPX,
201 24 for IPv6,
202 about 80 for AX.25
203 must be at least one bigger than
204 the AF_UNIX size (see net/unix/af_unix.c
205 :unix_mkname()).
209 * move_addr_to_kernel - copy a socket address into kernel space
210 * @uaddr: Address in user space
211 * @kaddr: Address in kernel space
212 * @ulen: Length in user space
214 * The address is copied into kernel space. If the provided address is
215 * too long an error code of -EINVAL is returned. If the copy gives
216 * invalid addresses -EFAULT is returned. On a success 0 is returned.
219 int move_addr_to_kernel(void __user *uaddr, int ulen, void *kaddr)
221 if(ulen<0||ulen>MAX_SOCK_ADDR)
222 return -EINVAL;
223 if(ulen==0)
224 return 0;
225 if(copy_from_user(kaddr,uaddr,ulen))
226 return -EFAULT;
227 return 0;
231 * move_addr_to_user - copy an address to user space
232 * @kaddr: kernel space address
233 * @klen: length of address in kernel
234 * @uaddr: user space address
235 * @ulen: pointer to user length field
237 * The value pointed to by ulen on entry is the buffer length available.
238 * This is overwritten with the buffer space used. -EINVAL is returned
239 * if an overlong buffer is specified or a negative buffer size. -EFAULT
240 * is returned if either the buffer or the length field are not
241 * accessible.
242 * After copying the data up to the limit the user specifies, the true
243 * length of the data is written over the length limit the user
244 * specified. Zero is returned for a success.
247 int move_addr_to_user(void *kaddr, int klen, void __user *uaddr, int __user *ulen)
249 int err;
250 int len;
252 if((err=get_user(len, ulen)))
253 return err;
254 if(len>klen)
255 len=klen;
256 if(len<0 || len> MAX_SOCK_ADDR)
257 return -EINVAL;
258 if(len)
260 if(copy_to_user(uaddr,kaddr,len))
261 return -EFAULT;
264 * "fromlen shall refer to the value before truncation.."
265 * 1003.1g
267 return __put_user(klen, ulen);
270 #define SOCKFS_MAGIC 0x534F434B
272 static kmem_cache_t * sock_inode_cachep;
274 static struct inode *sock_alloc_inode(struct super_block *sb)
276 struct socket_alloc *ei;
277 ei = (struct socket_alloc *)kmem_cache_alloc(sock_inode_cachep, SLAB_KERNEL);
278 if (!ei)
279 return NULL;
280 init_waitqueue_head(&ei->socket.wait);
282 ei->socket.fasync_list = NULL;
283 ei->socket.state = SS_UNCONNECTED;
284 ei->socket.flags = 0;
285 ei->socket.ops = NULL;
286 ei->socket.sk = NULL;
287 ei->socket.file = NULL;
288 ei->socket.passcred = 0;
290 return &ei->vfs_inode;
293 static void sock_destroy_inode(struct inode *inode)
295 kmem_cache_free(sock_inode_cachep,
296 container_of(inode, struct socket_alloc, vfs_inode));
299 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
301 struct socket_alloc *ei = (struct socket_alloc *) foo;
303 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
304 SLAB_CTOR_CONSTRUCTOR)
305 inode_init_once(&ei->vfs_inode);
308 static int init_inodecache(void)
310 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
311 sizeof(struct socket_alloc),
312 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
313 init_once, NULL);
314 if (sock_inode_cachep == NULL)
315 return -ENOMEM;
316 return 0;
319 static struct super_operations sockfs_ops = {
320 .alloc_inode = sock_alloc_inode,
321 .destroy_inode =sock_destroy_inode,
322 .statfs = simple_statfs,
325 static struct super_block *sockfs_get_sb(struct file_system_type *fs_type,
326 int flags, const char *dev_name, void *data)
328 return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC);
331 static struct vfsmount *sock_mnt;
333 static struct file_system_type sock_fs_type = {
334 .name = "sockfs",
335 .get_sb = sockfs_get_sb,
336 .kill_sb = kill_anon_super,
338 static int sockfs_delete_dentry(struct dentry *dentry)
340 return 1;
342 static struct dentry_operations sockfs_dentry_operations = {
343 .d_delete = sockfs_delete_dentry,
347 * Obtains the first available file descriptor and sets it up for use.
349 * This function creates file structure and maps it to fd space
350 * of current process. On success it returns file descriptor
351 * and file struct implicitly stored in sock->file.
352 * Note that another thread may close file descriptor before we return
353 * from this function. We use the fact that now we do not refer
354 * to socket after mapping. If one day we will need it, this
355 * function will increment ref. count on file by 1.
357 * In any case returned fd MAY BE not valid!
358 * This race condition is unavoidable
359 * with shared fd spaces, we cannot solve it inside kernel,
360 * but we take care of internal coherence yet.
363 int sock_map_fd(struct socket *sock)
365 int fd;
366 struct qstr this;
367 char name[32];
370 * Find a file descriptor suitable for return to the user.
373 fd = get_unused_fd();
374 if (fd >= 0) {
375 struct file *file = get_empty_filp();
377 if (!file) {
378 put_unused_fd(fd);
379 fd = -ENFILE;
380 goto out;
383 sprintf(name, "[%lu]", SOCK_INODE(sock)->i_ino);
384 this.name = name;
385 this.len = strlen(name);
386 this.hash = SOCK_INODE(sock)->i_ino;
388 file->f_dentry = d_alloc(sock_mnt->mnt_sb->s_root, &this);
389 if (!file->f_dentry) {
390 put_filp(file);
391 put_unused_fd(fd);
392 fd = -ENOMEM;
393 goto out;
395 file->f_dentry->d_op = &sockfs_dentry_operations;
396 d_add(file->f_dentry, SOCK_INODE(sock));
397 file->f_vfsmnt = mntget(sock_mnt);
398 file->f_mapping = file->f_dentry->d_inode->i_mapping;
400 sock->file = file;
401 file->f_op = SOCK_INODE(sock)->i_fop = &socket_file_ops;
402 file->f_mode = 3;
403 file->f_flags = O_RDWR;
404 file->f_pos = 0;
405 fd_install(fd, file);
408 out:
409 return fd;
413 * sockfd_lookup - Go from a file number to its socket slot
414 * @fd: file handle
415 * @err: pointer to an error code return
417 * The file handle passed in is locked and the socket it is bound
418 * too is returned. If an error occurs the err pointer is overwritten
419 * with a negative errno code and NULL is returned. The function checks
420 * for both invalid handles and passing a handle which is not a socket.
422 * On a success the socket object pointer is returned.
425 struct socket *sockfd_lookup(int fd, int *err)
427 struct file *file;
428 struct inode *inode;
429 struct socket *sock;
431 if (!(file = fget(fd)))
433 *err = -EBADF;
434 return NULL;
437 inode = file->f_dentry->d_inode;
438 if (!inode->i_sock || !(sock = SOCKET_I(inode)))
440 *err = -ENOTSOCK;
441 fput(file);
442 return NULL;
445 if (sock->file != file) {
446 printk(KERN_ERR "socki_lookup: socket file changed!\n");
447 sock->file = file;
449 return sock;
453 * sock_alloc - allocate a socket
455 * Allocate a new inode and socket object. The two are bound together
456 * and initialised. The socket is then returned. If we are out of inodes
457 * NULL is returned.
460 struct socket *sock_alloc(void)
462 struct inode * inode;
463 struct socket * sock;
465 inode = new_inode(sock_mnt->mnt_sb);
466 if (!inode)
467 return NULL;
469 sock = SOCKET_I(inode);
471 inode->i_mode = S_IFSOCK|S_IRWXUGO;
472 inode->i_sock = 1;
473 inode->i_uid = current->fsuid;
474 inode->i_gid = current->fsgid;
476 get_cpu_var(sockets_in_use)++;
477 put_cpu_var(sockets_in_use);
478 return sock;
482 * In theory you can't get an open on this inode, but /proc provides
483 * a back door. Remember to keep it shut otherwise you'll let the
484 * creepy crawlies in.
487 static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
489 return -ENXIO;
492 struct file_operations bad_sock_fops = {
493 .owner = THIS_MODULE,
494 .open = sock_no_open,
498 * sock_release - close a socket
499 * @sock: socket to close
501 * The socket is released from the protocol stack if it has a release
502 * callback, and the inode is then released if the socket is bound to
503 * an inode not a file.
506 void sock_release(struct socket *sock)
508 if (sock->ops) {
509 struct module *owner = sock->ops->owner;
511 sock->ops->release(sock);
512 sock->ops = NULL;
513 module_put(owner);
516 if (sock->fasync_list)
517 printk(KERN_ERR "sock_release: fasync list not empty!\n");
519 get_cpu_var(sockets_in_use)--;
520 put_cpu_var(sockets_in_use);
521 if (!sock->file) {
522 iput(SOCK_INODE(sock));
523 return;
525 sock->file=NULL;
528 static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
529 struct msghdr *msg, size_t size)
531 struct sock_iocb *si = kiocb_to_siocb(iocb);
532 int err;
534 si->sock = sock;
535 si->scm = NULL;
536 si->msg = msg;
537 si->size = size;
539 err = security_socket_sendmsg(sock, msg, size);
540 if (err)
541 return err;
543 return sock->ops->sendmsg(iocb, sock, msg, size);
546 int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
548 struct kiocb iocb;
549 int ret;
551 init_sync_kiocb(&iocb, NULL);
552 ret = __sock_sendmsg(&iocb, sock, msg, size);
553 if (-EIOCBQUEUED == ret)
554 ret = wait_on_sync_kiocb(&iocb);
555 return ret;
559 static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
560 struct msghdr *msg, size_t size, int flags)
562 int err;
563 struct sock_iocb *si = kiocb_to_siocb(iocb);
565 si->sock = sock;
566 si->scm = NULL;
567 si->msg = msg;
568 si->size = size;
569 si->flags = flags;
571 err = security_socket_recvmsg(sock, msg, size, flags);
572 if (err)
573 return err;
575 return sock->ops->recvmsg(iocb, sock, msg, size, flags);
578 int sock_recvmsg(struct socket *sock, struct msghdr *msg,
579 size_t size, int flags)
581 struct kiocb iocb;
582 int ret;
584 init_sync_kiocb(&iocb, NULL);
585 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
586 if (-EIOCBQUEUED == ret)
587 ret = wait_on_sync_kiocb(&iocb);
588 return ret;
592 * Read data from a socket. ubuf is a user mode pointer. We make sure the user
593 * area ubuf...ubuf+size-1 is writable before asking the protocol.
596 static ssize_t sock_aio_read(struct kiocb *iocb, char __user *ubuf,
597 size_t size, loff_t pos)
599 struct sock_iocb *x = kiocb_to_siocb(iocb);
600 struct socket *sock;
601 int flags;
603 if (pos != 0)
604 return -ESPIPE;
605 if (size==0) /* Match SYS5 behaviour */
606 return 0;
608 sock = SOCKET_I(iocb->ki_filp->f_dentry->d_inode);
610 x->async_msg.msg_name = NULL;
611 x->async_msg.msg_namelen = 0;
612 x->async_msg.msg_iov = &x->async_iov;
613 x->async_msg.msg_iovlen = 1;
614 x->async_msg.msg_control = NULL;
615 x->async_msg.msg_controllen = 0;
616 x->async_iov.iov_base = ubuf;
617 x->async_iov.iov_len = size;
618 flags = !(iocb->ki_filp->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
620 return __sock_recvmsg(iocb, sock, &x->async_msg, size, flags);
625 * Write data to a socket. We verify that the user area ubuf..ubuf+size-1
626 * is readable by the user process.
629 static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *ubuf,
630 size_t size, loff_t pos)
632 struct sock_iocb *x = kiocb_to_siocb(iocb);
633 struct socket *sock;
635 if (pos != 0)
636 return -ESPIPE;
637 if(size==0) /* Match SYS5 behaviour */
638 return 0;
640 sock = SOCKET_I(iocb->ki_filp->f_dentry->d_inode);
642 x->async_msg.msg_name = NULL;
643 x->async_msg.msg_namelen = 0;
644 x->async_msg.msg_iov = &x->async_iov;
645 x->async_msg.msg_iovlen = 1;
646 x->async_msg.msg_control = NULL;
647 x->async_msg.msg_controllen = 0;
648 x->async_msg.msg_flags = !(iocb->ki_filp->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
649 if (sock->type == SOCK_SEQPACKET)
650 x->async_msg.msg_flags |= MSG_EOR;
651 x->async_iov.iov_base = (void __user *)ubuf;
652 x->async_iov.iov_len = size;
654 return __sock_sendmsg(iocb, sock, &x->async_msg, size);
657 ssize_t sock_sendpage(struct file *file, struct page *page,
658 int offset, size_t size, loff_t *ppos, int more)
660 struct socket *sock;
661 int flags;
663 if (ppos != &file->f_pos)
664 return -ESPIPE;
666 sock = SOCKET_I(file->f_dentry->d_inode);
668 flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
669 if (more)
670 flags |= MSG_MORE;
672 return sock->ops->sendpage(sock, page, offset, size, flags);
675 int sock_readv_writev(int type, struct inode * inode, struct file * file,
676 const struct iovec * iov, long count, size_t size)
678 struct msghdr msg;
679 struct socket *sock;
681 sock = SOCKET_I(inode);
683 msg.msg_name = NULL;
684 msg.msg_namelen = 0;
685 msg.msg_control = NULL;
686 msg.msg_controllen = 0;
687 msg.msg_iov = (struct iovec *) iov;
688 msg.msg_iovlen = count;
689 msg.msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
691 /* read() does a VERIFY_WRITE */
692 if (type == VERIFY_WRITE)
693 return sock_recvmsg(sock, &msg, size, msg.msg_flags);
695 if (sock->type == SOCK_SEQPACKET)
696 msg.msg_flags |= MSG_EOR;
698 return sock_sendmsg(sock, &msg, size);
701 static ssize_t sock_readv(struct file *file, const struct iovec *vector,
702 unsigned long count, loff_t *ppos)
704 size_t tot_len = 0;
705 int i;
706 for (i = 0 ; i < count ; i++)
707 tot_len += vector[i].iov_len;
708 return sock_readv_writev(VERIFY_WRITE, file->f_dentry->d_inode,
709 file, vector, count, tot_len);
712 static ssize_t sock_writev(struct file *file, const struct iovec *vector,
713 unsigned long count, loff_t *ppos)
715 size_t tot_len = 0;
716 int i;
717 for (i = 0 ; i < count ; i++)
718 tot_len += vector[i].iov_len;
719 return sock_readv_writev(VERIFY_READ, file->f_dentry->d_inode,
720 file, vector, count, tot_len);
725 * Atomic setting of ioctl hooks to avoid race
726 * with module unload.
729 static DECLARE_MUTEX(br_ioctl_mutex);
730 static int (*br_ioctl_hook)(unsigned long arg) = NULL;
732 void brioctl_set(int (*hook)(unsigned long))
734 down(&br_ioctl_mutex);
735 br_ioctl_hook = hook;
736 up(&br_ioctl_mutex);
738 EXPORT_SYMBOL(brioctl_set);
740 static DECLARE_MUTEX(vlan_ioctl_mutex);
741 static int (*vlan_ioctl_hook)(unsigned long arg);
743 void vlan_ioctl_set(int (*hook)(unsigned long))
745 down(&vlan_ioctl_mutex);
746 vlan_ioctl_hook = hook;
747 up(&vlan_ioctl_mutex);
749 EXPORT_SYMBOL(vlan_ioctl_set);
751 static DECLARE_MUTEX(dlci_ioctl_mutex);
752 static int (*dlci_ioctl_hook)(unsigned int, void *);
754 void dlci_ioctl_set(int (*hook)(unsigned int, void *))
756 down(&dlci_ioctl_mutex);
757 dlci_ioctl_hook = hook;
758 up(&dlci_ioctl_mutex);
760 EXPORT_SYMBOL(dlci_ioctl_set);
763 * With an ioctl, arg may well be a user mode pointer, but we don't know
764 * what to do with it - that's up to the protocol still.
767 static int sock_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
768 unsigned long arg)
770 struct socket *sock;
771 int pid, err;
773 unlock_kernel();
774 sock = SOCKET_I(inode);
775 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
776 err = dev_ioctl(cmd, (void *)arg);
777 } else
778 #ifdef WIRELESS_EXT
779 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
780 err = dev_ioctl(cmd, (void *)arg);
781 } else
782 #endif /* WIRELESS_EXT */
783 switch (cmd) {
784 case FIOSETOWN:
785 case SIOCSPGRP:
786 err = -EFAULT;
787 if (get_user(pid, (int *)arg))
788 break;
789 err = f_setown(sock->file, pid, 1);
790 break;
791 case FIOGETOWN:
792 case SIOCGPGRP:
793 err = put_user(sock->file->f_owner.pid, (int *)arg);
794 break;
795 case SIOCGIFBR:
796 case SIOCSIFBR:
797 err = -ENOPKG;
798 if (!br_ioctl_hook)
799 request_module("bridge");
801 down(&br_ioctl_mutex);
802 if (br_ioctl_hook)
803 err = br_ioctl_hook(arg);
804 up(&br_ioctl_mutex);
805 break;
806 case SIOCGIFVLAN:
807 case SIOCSIFVLAN:
808 err = -ENOPKG;
809 if (!vlan_ioctl_hook)
810 request_module("8021q");
812 down(&vlan_ioctl_mutex);
813 if (vlan_ioctl_hook)
814 err = vlan_ioctl_hook(arg);
815 up(&vlan_ioctl_mutex);
816 break;
817 case SIOCGIFDIVERT:
818 case SIOCSIFDIVERT:
819 /* Convert this to call through a hook */
820 err = divert_ioctl(cmd, (struct divert_cf *)arg);
821 break;
822 case SIOCADDDLCI:
823 case SIOCDELDLCI:
824 err = -ENOPKG;
825 if (!dlci_ioctl_hook)
826 request_module("dlci");
828 if (dlci_ioctl_hook) {
829 down(&dlci_ioctl_mutex);
830 err = dlci_ioctl_hook(cmd, (void *)arg);
831 up(&dlci_ioctl_mutex);
833 break;
834 default:
835 err = sock->ops->ioctl(sock, cmd, arg);
836 break;
838 lock_kernel();
840 return err;
844 /* No kernel lock held - perfect */
845 static unsigned int sock_poll(struct file *file, poll_table * wait)
847 struct socket *sock;
850 * We can't return errors to poll, so it's either yes or no.
852 sock = SOCKET_I(file->f_dentry->d_inode);
853 return sock->ops->poll(file, sock, wait);
856 static int sock_mmap(struct file * file, struct vm_area_struct * vma)
858 struct socket *sock = SOCKET_I(file->f_dentry->d_inode);
860 return sock->ops->mmap(file, sock, vma);
863 int sock_close(struct inode *inode, struct file *filp)
866 * It was possible the inode is NULL we were
867 * closing an unfinished socket.
870 if (!inode)
872 printk(KERN_DEBUG "sock_close: NULL inode\n");
873 return 0;
875 sock_fasync(-1, filp, 0);
876 sock_release(SOCKET_I(inode));
877 return 0;
881 * Update the socket async list
883 * Fasync_list locking strategy.
885 * 1. fasync_list is modified only under process context socket lock
886 * i.e. under semaphore.
887 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
888 * or under socket lock.
889 * 3. fasync_list can be used from softirq context, so that
890 * modification under socket lock have to be enhanced with
891 * write_lock_bh(&sk->sk_callback_lock).
892 * --ANK (990710)
895 static int sock_fasync(int fd, struct file *filp, int on)
897 struct fasync_struct *fa, *fna=NULL, **prev;
898 struct socket *sock;
899 struct sock *sk;
901 if (on)
903 fna=(struct fasync_struct *)kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
904 if(fna==NULL)
905 return -ENOMEM;
908 sock = SOCKET_I(filp->f_dentry->d_inode);
910 if ((sk=sock->sk) == NULL) {
911 if (fna)
912 kfree(fna);
913 return -EINVAL;
916 lock_sock(sk);
918 prev=&(sock->fasync_list);
920 for (fa=*prev; fa!=NULL; prev=&fa->fa_next,fa=*prev)
921 if (fa->fa_file==filp)
922 break;
924 if(on)
926 if(fa!=NULL)
928 write_lock_bh(&sk->sk_callback_lock);
929 fa->fa_fd=fd;
930 write_unlock_bh(&sk->sk_callback_lock);
932 kfree(fna);
933 goto out;
935 fna->fa_file=filp;
936 fna->fa_fd=fd;
937 fna->magic=FASYNC_MAGIC;
938 fna->fa_next=sock->fasync_list;
939 write_lock_bh(&sk->sk_callback_lock);
940 sock->fasync_list=fna;
941 write_unlock_bh(&sk->sk_callback_lock);
943 else
945 if (fa!=NULL)
947 write_lock_bh(&sk->sk_callback_lock);
948 *prev=fa->fa_next;
949 write_unlock_bh(&sk->sk_callback_lock);
950 kfree(fa);
954 out:
955 release_sock(sock->sk);
956 return 0;
959 /* This function may be called only under socket lock or callback_lock */
961 int sock_wake_async(struct socket *sock, int how, int band)
963 if (!sock || !sock->fasync_list)
964 return -1;
965 switch (how)
967 case 1:
969 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
970 break;
971 goto call_kill;
972 case 2:
973 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
974 break;
975 /* fall through */
976 case 0:
977 call_kill:
978 __kill_fasync(sock->fasync_list, SIGIO, band);
979 break;
980 case 3:
981 __kill_fasync(sock->fasync_list, SIGURG, band);
983 return 0;
987 int sock_create(int family, int type, int protocol, struct socket **res)
989 int i;
990 int err;
991 struct socket *sock;
994 * Check protocol is in range
996 if (family < 0 || family >= NPROTO)
997 return -EAFNOSUPPORT;
998 if (type < 0 || type >= SOCK_MAX)
999 return -EINVAL;
1001 /* Compatibility.
1003 This uglymoron is moved from INET layer to here to avoid
1004 deadlock in module load.
1006 if (family == PF_INET && type == SOCK_PACKET) {
1007 static int warned;
1008 if (!warned) {
1009 warned = 1;
1010 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n", current->comm);
1012 family = PF_PACKET;
1015 err = security_socket_create(family, type, protocol);
1016 if (err)
1017 return err;
1019 #if defined(CONFIG_KMOD)
1020 /* Attempt to load a protocol module if the find failed.
1022 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1023 * requested real, full-featured networking support upon configuration.
1024 * Otherwise module support will break!
1026 if (net_families[family]==NULL)
1028 request_module("net-pf-%d",family);
1030 #endif
1032 net_family_read_lock();
1033 if (net_families[family] == NULL) {
1034 i = -EAFNOSUPPORT;
1035 goto out;
1039 * Allocate the socket and allow the family to set things up. if
1040 * the protocol is 0, the family is instructed to select an appropriate
1041 * default.
1044 if (!(sock = sock_alloc()))
1046 printk(KERN_WARNING "socket: no more sockets\n");
1047 i = -ENFILE; /* Not exactly a match, but its the
1048 closest posix thing */
1049 goto out;
1052 sock->type = type;
1055 * We will call the ->create function, that possibly is in a loadable
1056 * module, so we have to bump that loadable module refcnt first.
1058 i = -EAFNOSUPPORT;
1059 if (!try_module_get(net_families[family]->owner))
1060 goto out_release;
1062 if ((i = net_families[family]->create(sock, protocol)) < 0)
1063 goto out_module_put;
1065 * Now to bump the refcnt of the [loadable] module that owns this
1066 * socket at sock_release time we decrement its refcnt.
1068 if (!try_module_get(sock->ops->owner)) {
1069 sock->ops = NULL;
1070 goto out_module_put;
1073 * Now that we're done with the ->create function, the [loadable]
1074 * module can have its refcnt decremented
1076 module_put(net_families[family]->owner);
1077 *res = sock;
1078 security_socket_post_create(sock, family, type, protocol);
1080 out:
1081 net_family_read_unlock();
1082 return i;
1083 out_module_put:
1084 module_put(net_families[family]->owner);
1085 out_release:
1086 sock_release(sock);
1087 goto out;
1090 asmlinkage long sys_socket(int family, int type, int protocol)
1092 int retval;
1093 struct socket *sock;
1095 retval = sock_create(family, type, protocol, &sock);
1096 if (retval < 0)
1097 goto out;
1099 retval = sock_map_fd(sock);
1100 if (retval < 0)
1101 goto out_release;
1103 out:
1104 /* It may be already another descriptor 8) Not kernel problem. */
1105 return retval;
1107 out_release:
1108 sock_release(sock);
1109 return retval;
1113 * Create a pair of connected sockets.
1116 asmlinkage long sys_socketpair(int family, int type, int protocol, int __user *usockvec)
1118 struct socket *sock1, *sock2;
1119 int fd1, fd2, err;
1122 * Obtain the first socket and check if the underlying protocol
1123 * supports the socketpair call.
1126 err = sock_create(family, type, protocol, &sock1);
1127 if (err < 0)
1128 goto out;
1130 err = sock_create(family, type, protocol, &sock2);
1131 if (err < 0)
1132 goto out_release_1;
1134 err = sock1->ops->socketpair(sock1, sock2);
1135 if (err < 0)
1136 goto out_release_both;
1138 fd1 = fd2 = -1;
1140 err = sock_map_fd(sock1);
1141 if (err < 0)
1142 goto out_release_both;
1143 fd1 = err;
1145 err = sock_map_fd(sock2);
1146 if (err < 0)
1147 goto out_close_1;
1148 fd2 = err;
1150 /* fd1 and fd2 may be already another descriptors.
1151 * Not kernel problem.
1154 err = put_user(fd1, &usockvec[0]);
1155 if (!err)
1156 err = put_user(fd2, &usockvec[1]);
1157 if (!err)
1158 return 0;
1160 sys_close(fd2);
1161 sys_close(fd1);
1162 return err;
1164 out_close_1:
1165 sock_release(sock2);
1166 sys_close(fd1);
1167 return err;
1169 out_release_both:
1170 sock_release(sock2);
1171 out_release_1:
1172 sock_release(sock1);
1173 out:
1174 return err;
1179 * Bind a name to a socket. Nothing much to do here since it's
1180 * the protocol's responsibility to handle the local address.
1182 * We move the socket address to kernel space before we call
1183 * the protocol layer (having also checked the address is ok).
1186 asmlinkage long sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
1188 struct socket *sock;
1189 char address[MAX_SOCK_ADDR];
1190 int err;
1192 if((sock = sockfd_lookup(fd,&err))!=NULL)
1194 if((err=move_addr_to_kernel(umyaddr,addrlen,address))>=0) {
1195 err = security_socket_bind(sock, (struct sockaddr *)address, addrlen);
1196 if (err) {
1197 sockfd_put(sock);
1198 return err;
1200 err = sock->ops->bind(sock, (struct sockaddr *)address, addrlen);
1202 sockfd_put(sock);
1204 return err;
1209 * Perform a listen. Basically, we allow the protocol to do anything
1210 * necessary for a listen, and if that works, we mark the socket as
1211 * ready for listening.
1214 int sysctl_somaxconn = SOMAXCONN;
1216 asmlinkage long sys_listen(int fd, int backlog)
1218 struct socket *sock;
1219 int err;
1221 if ((sock = sockfd_lookup(fd, &err)) != NULL) {
1222 if ((unsigned) backlog > sysctl_somaxconn)
1223 backlog = sysctl_somaxconn;
1225 err = security_socket_listen(sock, backlog);
1226 if (err) {
1227 sockfd_put(sock);
1228 return err;
1231 err=sock->ops->listen(sock, backlog);
1232 sockfd_put(sock);
1234 return err;
1239 * For accept, we attempt to create a new socket, set up the link
1240 * with the client, wake up the client, then return the new
1241 * connected fd. We collect the address of the connector in kernel
1242 * space and move it to user at the very end. This is unclean because
1243 * we open the socket then return an error.
1245 * 1003.1g adds the ability to recvmsg() to query connection pending
1246 * status to recvmsg. We need to add that support in a way thats
1247 * clean when we restucture accept also.
1250 asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr, int __user *upeer_addrlen)
1252 struct socket *sock, *newsock;
1253 int err, len;
1254 char address[MAX_SOCK_ADDR];
1256 sock = sockfd_lookup(fd, &err);
1257 if (!sock)
1258 goto out;
1260 err = -EMFILE;
1261 if (!(newsock = sock_alloc()))
1262 goto out_put;
1264 newsock->type = sock->type;
1265 newsock->ops = sock->ops;
1267 err = security_socket_accept(sock, newsock);
1268 if (err)
1269 goto out_release;
1272 * We don't need try_module_get here, as the listening socket (sock)
1273 * has the protocol module (sock->ops->owner) held.
1275 __module_get(newsock->ops->owner);
1277 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1278 if (err < 0)
1279 goto out_release;
1281 if (upeer_sockaddr) {
1282 if(newsock->ops->getname(newsock, (struct sockaddr *)address, &len, 2)<0) {
1283 err = -ECONNABORTED;
1284 goto out_release;
1286 err = move_addr_to_user(address, len, upeer_sockaddr, upeer_addrlen);
1287 if (err < 0)
1288 goto out_release;
1291 /* File flags are not inherited via accept() unlike another OSes. */
1293 if ((err = sock_map_fd(newsock)) < 0)
1294 goto out_release;
1296 security_socket_post_accept(sock, newsock);
1298 out_put:
1299 sockfd_put(sock);
1300 out:
1301 return err;
1302 out_release:
1303 sock_release(newsock);
1304 goto out_put;
1309 * Attempt to connect to a socket with the server address. The address
1310 * is in user space so we verify it is OK and move it to kernel space.
1312 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1313 * break bindings
1315 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1316 * other SEQPACKET protocols that take time to connect() as it doesn't
1317 * include the -EINPROGRESS status for such sockets.
1320 asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)
1322 struct socket *sock;
1323 char address[MAX_SOCK_ADDR];
1324 int err;
1326 sock = sockfd_lookup(fd, &err);
1327 if (!sock)
1328 goto out;
1329 err = move_addr_to_kernel(uservaddr, addrlen, address);
1330 if (err < 0)
1331 goto out_put;
1333 err = security_socket_connect(sock, (struct sockaddr *)address, addrlen);
1334 if (err)
1335 goto out_put;
1337 err = sock->ops->connect(sock, (struct sockaddr *) address, addrlen,
1338 sock->file->f_flags);
1339 out_put:
1340 sockfd_put(sock);
1341 out:
1342 return err;
1346 * Get the local address ('name') of a socket object. Move the obtained
1347 * name to user space.
1350 asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len)
1352 struct socket *sock;
1353 char address[MAX_SOCK_ADDR];
1354 int len, err;
1356 sock = sockfd_lookup(fd, &err);
1357 if (!sock)
1358 goto out;
1360 err = security_socket_getsockname(sock);
1361 if (err)
1362 goto out_put;
1364 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 0);
1365 if (err)
1366 goto out_put;
1367 err = move_addr_to_user(address, len, usockaddr, usockaddr_len);
1369 out_put:
1370 sockfd_put(sock);
1371 out:
1372 return err;
1376 * Get the remote address ('name') of a socket object. Move the obtained
1377 * name to user space.
1380 asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len)
1382 struct socket *sock;
1383 char address[MAX_SOCK_ADDR];
1384 int len, err;
1386 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1388 err = security_socket_getpeername(sock);
1389 if (err) {
1390 sockfd_put(sock);
1391 return err;
1394 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 1);
1395 if (!err)
1396 err=move_addr_to_user(address,len, usockaddr, usockaddr_len);
1397 sockfd_put(sock);
1399 return err;
1403 * Send a datagram to a given address. We move the address into kernel
1404 * space and check the user space data area is readable before invoking
1405 * the protocol.
1408 asmlinkage long sys_sendto(int fd, void __user * buff, size_t len, unsigned flags,
1409 struct sockaddr __user *addr, int addr_len)
1411 struct socket *sock;
1412 char address[MAX_SOCK_ADDR];
1413 int err;
1414 struct msghdr msg;
1415 struct iovec iov;
1417 sock = sockfd_lookup(fd, &err);
1418 if (!sock)
1419 goto out;
1420 iov.iov_base=buff;
1421 iov.iov_len=len;
1422 msg.msg_name=NULL;
1423 msg.msg_iov=&iov;
1424 msg.msg_iovlen=1;
1425 msg.msg_control=NULL;
1426 msg.msg_controllen=0;
1427 msg.msg_namelen=0;
1428 if(addr)
1430 err = move_addr_to_kernel(addr, addr_len, address);
1431 if (err < 0)
1432 goto out_put;
1433 msg.msg_name=address;
1434 msg.msg_namelen=addr_len;
1436 if (sock->file->f_flags & O_NONBLOCK)
1437 flags |= MSG_DONTWAIT;
1438 msg.msg_flags = flags;
1439 err = sock_sendmsg(sock, &msg, len);
1441 out_put:
1442 sockfd_put(sock);
1443 out:
1444 return err;
1448 * Send a datagram down a socket.
1451 asmlinkage long sys_send(int fd, void __user * buff, size_t len, unsigned flags)
1453 return sys_sendto(fd, buff, len, flags, NULL, 0);
1457 * Receive a frame from the socket and optionally record the address of the
1458 * sender. We verify the buffers are writable and if needed move the
1459 * sender address from kernel to user space.
1462 asmlinkage long sys_recvfrom(int fd, void __user * ubuf, size_t size, unsigned flags,
1463 struct sockaddr __user *addr, int __user *addr_len)
1465 struct socket *sock;
1466 struct iovec iov;
1467 struct msghdr msg;
1468 char address[MAX_SOCK_ADDR];
1469 int err,err2;
1471 sock = sockfd_lookup(fd, &err);
1472 if (!sock)
1473 goto out;
1475 msg.msg_control=NULL;
1476 msg.msg_controllen=0;
1477 msg.msg_iovlen=1;
1478 msg.msg_iov=&iov;
1479 iov.iov_len=size;
1480 iov.iov_base=ubuf;
1481 msg.msg_name=address;
1482 msg.msg_namelen=MAX_SOCK_ADDR;
1483 if (sock->file->f_flags & O_NONBLOCK)
1484 flags |= MSG_DONTWAIT;
1485 err=sock_recvmsg(sock, &msg, size, flags);
1487 if(err >= 0 && addr != NULL)
1489 err2=move_addr_to_user(address, msg.msg_namelen, addr, addr_len);
1490 if(err2<0)
1491 err=err2;
1493 sockfd_put(sock);
1494 out:
1495 return err;
1499 * Receive a datagram from a socket.
1502 asmlinkage long sys_recv(int fd, void __user * ubuf, size_t size, unsigned flags)
1504 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1508 * Set a socket option. Because we don't know the option lengths we have
1509 * to pass the user mode parameter for the protocols to sort out.
1512 asmlinkage long sys_setsockopt(int fd, int level, int optname, char __user *optval, int optlen)
1514 int err;
1515 struct socket *sock;
1517 if (optlen < 0)
1518 return -EINVAL;
1520 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1522 err = security_socket_setsockopt(sock,level,optname);
1523 if (err) {
1524 sockfd_put(sock);
1525 return err;
1528 if (level == SOL_SOCKET)
1529 err=sock_setsockopt(sock,level,optname,optval,optlen);
1530 else
1531 err=sock->ops->setsockopt(sock, level, optname, optval, optlen);
1532 sockfd_put(sock);
1534 return err;
1538 * Get a socket option. Because we don't know the option lengths we have
1539 * to pass a user mode parameter for the protocols to sort out.
1542 asmlinkage long sys_getsockopt(int fd, int level, int optname, char __user *optval, int __user *optlen)
1544 int err;
1545 struct socket *sock;
1547 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1549 err = security_socket_getsockopt(sock, level,
1550 optname);
1551 if (err) {
1552 sockfd_put(sock);
1553 return err;
1556 if (level == SOL_SOCKET)
1557 err=sock_getsockopt(sock,level,optname,optval,optlen);
1558 else
1559 err=sock->ops->getsockopt(sock, level, optname, optval, optlen);
1560 sockfd_put(sock);
1562 return err;
1567 * Shutdown a socket.
1570 asmlinkage long sys_shutdown(int fd, int how)
1572 int err;
1573 struct socket *sock;
1575 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1577 err = security_socket_shutdown(sock, how);
1578 if (err) {
1579 sockfd_put(sock);
1580 return err;
1583 err=sock->ops->shutdown(sock, how);
1584 sockfd_put(sock);
1586 return err;
1589 /* A couple of helpful macros for getting the address of the 32/64 bit
1590 * fields which are the same type (int / unsigned) on our platforms.
1592 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1593 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1594 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1598 * BSD sendmsg interface
1601 asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
1603 struct compat_msghdr __user *msg_compat = (struct compat_msghdr __user *)msg;
1604 struct socket *sock;
1605 char address[MAX_SOCK_ADDR];
1606 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1607 unsigned char ctl[sizeof(struct cmsghdr) + 20]; /* 20 is size of ipv6_pktinfo */
1608 unsigned char *ctl_buf = ctl;
1609 struct msghdr msg_sys;
1610 int err, ctl_len, iov_size, total_len;
1612 err = -EFAULT;
1613 if (MSG_CMSG_COMPAT & flags) {
1614 if (get_compat_msghdr(&msg_sys, msg_compat))
1615 return -EFAULT;
1616 } else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
1617 return -EFAULT;
1619 sock = sockfd_lookup(fd, &err);
1620 if (!sock)
1621 goto out;
1623 /* do not move before msg_sys is valid */
1624 err = -EMSGSIZE;
1625 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1626 goto out_put;
1628 /* Check whether to allocate the iovec area*/
1629 err = -ENOMEM;
1630 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1631 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1632 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1633 if (!iov)
1634 goto out_put;
1637 /* This will also move the address data into kernel space */
1638 if (MSG_CMSG_COMPAT & flags) {
1639 err = verify_compat_iovec(&msg_sys, iov, address, VERIFY_READ);
1640 } else
1641 err = verify_iovec(&msg_sys, iov, address, VERIFY_READ);
1642 if (err < 0)
1643 goto out_freeiov;
1644 total_len = err;
1646 err = -ENOBUFS;
1648 if (msg_sys.msg_controllen > INT_MAX)
1649 goto out_freeiov;
1650 ctl_len = msg_sys.msg_controllen;
1651 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
1652 err = cmsghdr_from_user_compat_to_kern(&msg_sys, ctl, sizeof(ctl));
1653 if (err)
1654 goto out_freeiov;
1655 ctl_buf = msg_sys.msg_control;
1656 } else if (ctl_len) {
1657 if (ctl_len > sizeof(ctl))
1659 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
1660 if (ctl_buf == NULL)
1661 goto out_freeiov;
1663 err = -EFAULT;
1665 * Careful! Before this, msg_sys.msg_control contains a user pointer.
1666 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1667 * checking falls down on this.
1669 if (copy_from_user(ctl_buf, (void __user *) msg_sys.msg_control, ctl_len))
1670 goto out_freectl;
1671 msg_sys.msg_control = ctl_buf;
1673 msg_sys.msg_flags = flags;
1675 if (sock->file->f_flags & O_NONBLOCK)
1676 msg_sys.msg_flags |= MSG_DONTWAIT;
1677 err = sock_sendmsg(sock, &msg_sys, total_len);
1679 out_freectl:
1680 if (ctl_buf != ctl)
1681 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1682 out_freeiov:
1683 if (iov != iovstack)
1684 sock_kfree_s(sock->sk, iov, iov_size);
1685 out_put:
1686 sockfd_put(sock);
1687 out:
1688 return err;
1692 * BSD recvmsg interface
1695 asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flags)
1697 struct compat_msghdr __user *msg_compat = (struct compat_msghdr __user *)msg;
1698 struct socket *sock;
1699 struct iovec iovstack[UIO_FASTIOV];
1700 struct iovec *iov=iovstack;
1701 struct msghdr msg_sys;
1702 unsigned long cmsg_ptr;
1703 int err, iov_size, total_len, len;
1705 /* kernel mode address */
1706 char addr[MAX_SOCK_ADDR];
1708 /* user mode address pointers */
1709 struct sockaddr __user *uaddr;
1710 int __user *uaddr_len;
1712 if (MSG_CMSG_COMPAT & flags) {
1713 if (get_compat_msghdr(&msg_sys, msg_compat))
1714 return -EFAULT;
1715 } else
1716 if (copy_from_user(&msg_sys,msg,sizeof(struct msghdr)))
1717 return -EFAULT;
1719 sock = sockfd_lookup(fd, &err);
1720 if (!sock)
1721 goto out;
1723 err = -EMSGSIZE;
1724 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1725 goto out_put;
1727 /* Check whether to allocate the iovec area*/
1728 err = -ENOMEM;
1729 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1730 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1731 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1732 if (!iov)
1733 goto out_put;
1737 * Save the user-mode address (verify_iovec will change the
1738 * kernel msghdr to use the kernel address space)
1741 uaddr = (void __user *) msg_sys.msg_name;
1742 uaddr_len = COMPAT_NAMELEN(msg);
1743 if (MSG_CMSG_COMPAT & flags) {
1744 err = verify_compat_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1745 } else
1746 err = verify_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1747 if (err < 0)
1748 goto out_freeiov;
1749 total_len=err;
1751 cmsg_ptr = (unsigned long)msg_sys.msg_control;
1752 msg_sys.msg_flags = 0;
1753 if (MSG_CMSG_COMPAT & flags)
1754 msg_sys.msg_flags = MSG_CMSG_COMPAT;
1756 if (sock->file->f_flags & O_NONBLOCK)
1757 flags |= MSG_DONTWAIT;
1758 err = sock_recvmsg(sock, &msg_sys, total_len, flags);
1759 if (err < 0)
1760 goto out_freeiov;
1761 len = err;
1763 if (uaddr != NULL) {
1764 err = move_addr_to_user(addr, msg_sys.msg_namelen, uaddr, uaddr_len);
1765 if (err < 0)
1766 goto out_freeiov;
1768 err = __put_user(msg_sys.msg_flags, COMPAT_FLAGS(msg));
1769 if (err)
1770 goto out_freeiov;
1771 if (MSG_CMSG_COMPAT & flags)
1772 err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr,
1773 &msg_compat->msg_controllen);
1774 else
1775 err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr,
1776 &msg->msg_controllen);
1777 if (err)
1778 goto out_freeiov;
1779 err = len;
1781 out_freeiov:
1782 if (iov != iovstack)
1783 sock_kfree_s(sock->sk, iov, iov_size);
1784 out_put:
1785 sockfd_put(sock);
1786 out:
1787 return err;
1790 /* Argument list sizes for sys_socketcall */
1791 #define AL(x) ((x) * sizeof(unsigned long))
1792 static unsigned char nargs[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
1793 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
1794 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)};
1795 #undef AL
1798 * System call vectors.
1800 * Argument checking cleaned up. Saved 20% in size.
1801 * This function doesn't need to set the kernel lock because
1802 * it is set by the callees.
1805 asmlinkage long sys_socketcall(int call, unsigned long __user *args)
1807 unsigned long a[6];
1808 unsigned long a0,a1;
1809 int err;
1811 if(call<1||call>SYS_RECVMSG)
1812 return -EINVAL;
1814 /* copy_from_user should be SMP safe. */
1815 if (copy_from_user(a, args, nargs[call]))
1816 return -EFAULT;
1818 a0=a[0];
1819 a1=a[1];
1821 switch(call)
1823 case SYS_SOCKET:
1824 err = sys_socket(a0,a1,a[2]);
1825 break;
1826 case SYS_BIND:
1827 err = sys_bind(a0,(struct sockaddr __user *)a1, a[2]);
1828 break;
1829 case SYS_CONNECT:
1830 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
1831 break;
1832 case SYS_LISTEN:
1833 err = sys_listen(a0,a1);
1834 break;
1835 case SYS_ACCEPT:
1836 err = sys_accept(a0,(struct sockaddr __user *)a1, (int __user *)a[2]);
1837 break;
1838 case SYS_GETSOCKNAME:
1839 err = sys_getsockname(a0,(struct sockaddr __user *)a1, (int __user *)a[2]);
1840 break;
1841 case SYS_GETPEERNAME:
1842 err = sys_getpeername(a0, (struct sockaddr __user *)a1, (int __user *)a[2]);
1843 break;
1844 case SYS_SOCKETPAIR:
1845 err = sys_socketpair(a0,a1, a[2], (int __user *)a[3]);
1846 break;
1847 case SYS_SEND:
1848 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
1849 break;
1850 case SYS_SENDTO:
1851 err = sys_sendto(a0,(void __user *)a1, a[2], a[3],
1852 (struct sockaddr __user *)a[4], a[5]);
1853 break;
1854 case SYS_RECV:
1855 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
1856 break;
1857 case SYS_RECVFROM:
1858 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
1859 (struct sockaddr __user *)a[4], (int __user *)a[5]);
1860 break;
1861 case SYS_SHUTDOWN:
1862 err = sys_shutdown(a0,a1);
1863 break;
1864 case SYS_SETSOCKOPT:
1865 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
1866 break;
1867 case SYS_GETSOCKOPT:
1868 err = sys_getsockopt(a0, a1, a[2], (char __user *)a[3], (int __user *)a[4]);
1869 break;
1870 case SYS_SENDMSG:
1871 err = sys_sendmsg(a0, (struct msghdr __user *) a1, a[2]);
1872 break;
1873 case SYS_RECVMSG:
1874 err = sys_recvmsg(a0, (struct msghdr __user *) a1, a[2]);
1875 break;
1876 default:
1877 err = -EINVAL;
1878 break;
1880 return err;
1884 * This function is called by a protocol handler that wants to
1885 * advertise its address family, and have it linked into the
1886 * SOCKET module.
1889 int sock_register(struct net_proto_family *ops)
1891 int err;
1893 if (ops->family >= NPROTO) {
1894 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
1895 return -ENOBUFS;
1897 net_family_write_lock();
1898 err = -EEXIST;
1899 if (net_families[ops->family] == NULL) {
1900 net_families[ops->family]=ops;
1901 err = 0;
1903 net_family_write_unlock();
1904 printk(KERN_INFO "NET: Registered protocol family %d\n",
1905 ops->family);
1906 return err;
1910 * This function is called by a protocol handler that wants to
1911 * remove its address family, and have it unlinked from the
1912 * SOCKET module.
1915 int sock_unregister(int family)
1917 if (family < 0 || family >= NPROTO)
1918 return -1;
1920 net_family_write_lock();
1921 net_families[family]=NULL;
1922 net_family_write_unlock();
1923 printk(KERN_INFO "NET: Unregistered protocol family %d\n",
1924 family);
1925 return 0;
1929 extern void sk_init(void);
1931 void __init sock_init(void)
1933 int i;
1936 * Initialize all address (protocol) families.
1939 for (i = 0; i < NPROTO; i++)
1940 net_families[i] = NULL;
1943 * Initialize sock SLAB cache.
1946 sk_init();
1948 #ifdef SLAB_SKB
1950 * Initialize skbuff SLAB cache
1952 skb_init();
1953 #endif
1956 * Initialize the protocols module.
1959 init_inodecache();
1960 register_filesystem(&sock_fs_type);
1961 sock_mnt = kern_mount(&sock_fs_type);
1962 /* The real protocol initialization is performed when
1963 * do_initcalls is run.
1966 #ifdef CONFIG_NETFILTER
1967 netfilter_init();
1968 #endif
1971 #ifdef CONFIG_PROC_FS
1972 void socket_seq_show(struct seq_file *seq)
1974 int cpu;
1975 int counter = 0;
1977 for (cpu = 0; cpu < NR_CPUS; cpu++)
1978 counter += per_cpu(sockets_in_use, cpu);
1980 /* It can be negative, by the way. 8) */
1981 if (counter < 0)
1982 counter = 0;
1984 seq_printf(seq, "sockets: used %d\n", counter);
1986 #endif /* CONFIG_PROC_FS */
1988 /* ABI emulation layers need these two */
1989 EXPORT_SYMBOL(move_addr_to_kernel);
1990 EXPORT_SYMBOL(move_addr_to_user);
1991 EXPORT_SYMBOL(sock_alloc);
1992 EXPORT_SYMBOL(sock_alloc_inode);
1993 EXPORT_SYMBOL(sock_create);
1994 EXPORT_SYMBOL(sock_map_fd);
1995 EXPORT_SYMBOL(sock_recvmsg);
1996 EXPORT_SYMBOL(sock_register);
1997 EXPORT_SYMBOL(sock_release);
1998 EXPORT_SYMBOL(sock_sendmsg);
1999 EXPORT_SYMBOL(sock_unregister);
2000 EXPORT_SYMBOL(sock_wake_async);
2001 EXPORT_SYMBOL(sockfd_lookup);