[PATCH] more typo fixes and dead old code removal
[linux-2.6/history.git] / net / socket.c
blob74fd11fdd7a8a7bd7e400fa3adbf94b6ecaff999
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/compat.h>
82 #include <linux/kmod.h>
84 #ifdef CONFIG_NET_RADIO
85 #include <linux/wireless.h> /* Note : will define WIRELESS_EXT */
86 #endif /* CONFIG_NET_RADIO */
88 #include <asm/uaccess.h>
89 #include <net/compat.h>
91 #include <net/sock.h>
92 #include <linux/netfilter.h>
94 static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
95 static ssize_t sock_aio_read(struct kiocb *iocb, char __user *buf,
96 size_t size, loff_t pos);
97 static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *buf,
98 size_t size, loff_t pos);
99 static int sock_mmap(struct file *file, struct vm_area_struct * vma);
101 static int sock_close(struct inode *inode, struct file *file);
102 static unsigned int sock_poll(struct file *file,
103 struct poll_table_struct *wait);
104 static int sock_ioctl(struct inode *inode, struct file *file,
105 unsigned int cmd, unsigned long arg);
106 static int sock_fasync(int fd, struct file *filp, int on);
107 static ssize_t sock_readv(struct file *file, const struct iovec *vector,
108 unsigned long count, loff_t *ppos);
109 static ssize_t sock_writev(struct file *file, const struct iovec *vector,
110 unsigned long count, loff_t *ppos);
111 static ssize_t sock_sendpage(struct file *file, struct page *page,
112 int offset, size_t size, loff_t *ppos, int more);
116 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
117 * in the operation structures but are done directly via the socketcall() multiplexor.
120 static struct file_operations socket_file_ops = {
121 .owner = THIS_MODULE,
122 .llseek = no_llseek,
123 .aio_read = sock_aio_read,
124 .aio_write = sock_aio_write,
125 .poll = sock_poll,
126 .ioctl = sock_ioctl,
127 .mmap = sock_mmap,
128 .open = sock_no_open, /* special open code to disallow open via /proc */
129 .release = sock_close,
130 .fasync = sock_fasync,
131 .readv = sock_readv,
132 .writev = sock_writev,
133 .sendpage = sock_sendpage
137 * The protocol list. Each protocol is registered in here.
140 static struct net_proto_family *net_families[NPROTO];
142 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
143 static atomic_t net_family_lockct = ATOMIC_INIT(0);
144 static spinlock_t net_family_lock = SPIN_LOCK_UNLOCKED;
146 /* The strategy is: modifications net_family vector are short, do not
147 sleep and veeery rare, but read access should be free of any exclusive
148 locks.
151 static void net_family_write_lock(void)
153 spin_lock(&net_family_lock);
154 while (atomic_read(&net_family_lockct) != 0) {
155 spin_unlock(&net_family_lock);
157 yield();
159 spin_lock(&net_family_lock);
163 static __inline__ void net_family_write_unlock(void)
165 spin_unlock(&net_family_lock);
168 static __inline__ void net_family_read_lock(void)
170 atomic_inc(&net_family_lockct);
171 spin_unlock_wait(&net_family_lock);
174 static __inline__ void net_family_read_unlock(void)
176 atomic_dec(&net_family_lockct);
179 #else
180 #define net_family_write_lock() do { } while(0)
181 #define net_family_write_unlock() do { } while(0)
182 #define net_family_read_lock() do { } while(0)
183 #define net_family_read_unlock() do { } while(0)
184 #endif
188 * Statistics counters of the socket lists
191 static DEFINE_PER_CPU(int, sockets_in_use) = 0;
194 * Support routines. Move socket addresses back and forth across the kernel/user
195 * divide and look after the messy bits.
198 #define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
199 16 for IP, 16 for IPX,
200 24 for IPv6,
201 about 80 for AX.25
202 must be at least one bigger than
203 the AF_UNIX size (see net/unix/af_unix.c
204 :unix_mkname()).
208 * move_addr_to_kernel - copy a socket address into kernel space
209 * @uaddr: Address in user space
210 * @kaddr: Address in kernel space
211 * @ulen: Length in user space
213 * The address is copied into kernel space. If the provided address is
214 * too long an error code of -EINVAL is returned. If the copy gives
215 * invalid addresses -EFAULT is returned. On a success 0 is returned.
218 int move_addr_to_kernel(void __user *uaddr, int ulen, void *kaddr)
220 if(ulen<0||ulen>MAX_SOCK_ADDR)
221 return -EINVAL;
222 if(ulen==0)
223 return 0;
224 if(copy_from_user(kaddr,uaddr,ulen))
225 return -EFAULT;
226 return 0;
230 * move_addr_to_user - copy an address to user space
231 * @kaddr: kernel space address
232 * @klen: length of address in kernel
233 * @uaddr: user space address
234 * @ulen: pointer to user length field
236 * The value pointed to by ulen on entry is the buffer length available.
237 * This is overwritten with the buffer space used. -EINVAL is returned
238 * if an overlong buffer is specified or a negative buffer size. -EFAULT
239 * is returned if either the buffer or the length field are not
240 * accessible.
241 * After copying the data up to the limit the user specifies, the true
242 * length of the data is written over the length limit the user
243 * specified. Zero is returned for a success.
246 int move_addr_to_user(void *kaddr, int klen, void __user *uaddr, int __user *ulen)
248 int err;
249 int len;
251 if((err=get_user(len, ulen)))
252 return err;
253 if(len>klen)
254 len=klen;
255 if(len<0 || len> MAX_SOCK_ADDR)
256 return -EINVAL;
257 if(len)
259 if(copy_to_user(uaddr,kaddr,len))
260 return -EFAULT;
263 * "fromlen shall refer to the value before truncation.."
264 * 1003.1g
266 return __put_user(klen, ulen);
269 #define SOCKFS_MAGIC 0x534F434B
271 static kmem_cache_t * sock_inode_cachep;
273 static struct inode *sock_alloc_inode(struct super_block *sb)
275 struct socket_alloc *ei;
276 ei = (struct socket_alloc *)kmem_cache_alloc(sock_inode_cachep, SLAB_KERNEL);
277 if (!ei)
278 return NULL;
279 init_waitqueue_head(&ei->socket.wait);
281 ei->socket.fasync_list = NULL;
282 ei->socket.state = SS_UNCONNECTED;
283 ei->socket.flags = 0;
284 ei->socket.ops = NULL;
285 ei->socket.sk = NULL;
286 ei->socket.file = NULL;
287 ei->socket.passcred = 0;
289 return &ei->vfs_inode;
292 static void sock_destroy_inode(struct inode *inode)
294 kmem_cache_free(sock_inode_cachep,
295 container_of(inode, struct socket_alloc, vfs_inode));
298 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
300 struct socket_alloc *ei = (struct socket_alloc *) foo;
302 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
303 SLAB_CTOR_CONSTRUCTOR)
304 inode_init_once(&ei->vfs_inode);
307 static int init_inodecache(void)
309 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
310 sizeof(struct socket_alloc),
311 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
312 init_once, NULL);
313 if (sock_inode_cachep == NULL)
314 return -ENOMEM;
315 return 0;
318 static struct super_operations sockfs_ops = {
319 .alloc_inode = sock_alloc_inode,
320 .destroy_inode =sock_destroy_inode,
321 .statfs = simple_statfs,
324 static struct super_block *sockfs_get_sb(struct file_system_type *fs_type,
325 int flags, const char *dev_name, void *data)
327 return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC);
330 static struct vfsmount *sock_mnt;
332 static struct file_system_type sock_fs_type = {
333 .name = "sockfs",
334 .get_sb = sockfs_get_sb,
335 .kill_sb = kill_anon_super,
337 static int sockfs_delete_dentry(struct dentry *dentry)
339 return 1;
341 static struct dentry_operations sockfs_dentry_operations = {
342 .d_delete = sockfs_delete_dentry,
346 * Obtains the first available file descriptor and sets it up for use.
348 * This function creates file structure and maps it to fd space
349 * of current process. On success it returns file descriptor
350 * and file struct implicitly stored in sock->file.
351 * Note that another thread may close file descriptor before we return
352 * from this function. We use the fact that now we do not refer
353 * to socket after mapping. If one day we will need it, this
354 * function will increment ref. count on file by 1.
356 * In any case returned fd MAY BE not valid!
357 * This race condition is unavoidable
358 * with shared fd spaces, we cannot solve it inside kernel,
359 * but we take care of internal coherence yet.
362 int sock_map_fd(struct socket *sock)
364 int fd;
365 struct qstr this;
366 char name[32];
369 * Find a file descriptor suitable for return to the user.
372 fd = get_unused_fd();
373 if (fd >= 0) {
374 struct file *file = get_empty_filp();
376 if (!file) {
377 put_unused_fd(fd);
378 fd = -ENFILE;
379 goto out;
382 sprintf(name, "[%lu]", SOCK_INODE(sock)->i_ino);
383 this.name = name;
384 this.len = strlen(name);
385 this.hash = SOCK_INODE(sock)->i_ino;
387 file->f_dentry = d_alloc(sock_mnt->mnt_sb->s_root, &this);
388 if (!file->f_dentry) {
389 put_filp(file);
390 put_unused_fd(fd);
391 fd = -ENOMEM;
392 goto out;
394 file->f_dentry->d_op = &sockfs_dentry_operations;
395 d_add(file->f_dentry, SOCK_INODE(sock));
396 file->f_vfsmnt = mntget(sock_mnt);
398 sock->file = file;
399 file->f_op = SOCK_INODE(sock)->i_fop = &socket_file_ops;
400 file->f_mode = 3;
401 file->f_flags = O_RDWR;
402 file->f_pos = 0;
403 fd_install(fd, file);
406 out:
407 return fd;
411 * sockfd_lookup - Go from a file number to its socket slot
412 * @fd: file handle
413 * @err: pointer to an error code return
415 * The file handle passed in is locked and the socket it is bound
416 * too is returned. If an error occurs the err pointer is overwritten
417 * with a negative errno code and NULL is returned. The function checks
418 * for both invalid handles and passing a handle which is not a socket.
420 * On a success the socket object pointer is returned.
423 struct socket *sockfd_lookup(int fd, int *err)
425 struct file *file;
426 struct inode *inode;
427 struct socket *sock;
429 if (!(file = fget(fd)))
431 *err = -EBADF;
432 return NULL;
435 inode = file->f_dentry->d_inode;
436 if (!inode->i_sock || !(sock = SOCKET_I(inode)))
438 *err = -ENOTSOCK;
439 fput(file);
440 return NULL;
443 if (sock->file != file) {
444 printk(KERN_ERR "socki_lookup: socket file changed!\n");
445 sock->file = file;
447 return sock;
451 * sock_alloc - allocate a socket
453 * Allocate a new inode and socket object. The two are bound together
454 * and initialised. The socket is then returned. If we are out of inodes
455 * NULL is returned.
458 struct socket *sock_alloc(void)
460 struct inode * inode;
461 struct socket * sock;
463 inode = new_inode(sock_mnt->mnt_sb);
464 if (!inode)
465 return NULL;
467 sock = SOCKET_I(inode);
469 inode->i_mode = S_IFSOCK|S_IRWXUGO;
470 inode->i_sock = 1;
471 inode->i_uid = current->fsuid;
472 inode->i_gid = current->fsgid;
474 get_cpu_var(sockets_in_use)++;
475 put_cpu_var(sockets_in_use);
476 return sock;
480 * In theory you can't get an open on this inode, but /proc provides
481 * a back door. Remember to keep it shut otherwise you'll let the
482 * creepy crawlies in.
485 static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
487 return -ENXIO;
490 struct file_operations bad_sock_fops = {
491 .owner = THIS_MODULE,
492 .open = sock_no_open,
496 * sock_release - close a socket
497 * @sock: socket to close
499 * The socket is released from the protocol stack if it has a release
500 * callback, and the inode is then released if the socket is bound to
501 * an inode not a file.
504 void sock_release(struct socket *sock)
506 if (sock->ops) {
507 struct module *owner = sock->ops->owner;
509 sock->ops->release(sock);
510 sock->ops = NULL;
511 module_put(owner);
514 if (sock->fasync_list)
515 printk(KERN_ERR "sock_release: fasync list not empty!\n");
517 get_cpu_var(sockets_in_use)--;
518 put_cpu_var(sockets_in_use);
519 if (!sock->file) {
520 iput(SOCK_INODE(sock));
521 return;
523 sock->file=NULL;
526 static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, int size)
528 struct sock_iocb *si = kiocb_to_siocb(iocb);
529 int err;
531 si->sock = sock;
532 si->scm = NULL;
533 si->msg = msg;
534 si->size = size;
536 err = security_socket_sendmsg(sock, msg, size);
537 if (err)
538 return err;
540 return sock->ops->sendmsg(iocb, sock, msg, size);
543 int sock_sendmsg(struct socket *sock, struct msghdr *msg, int size)
545 struct kiocb iocb;
546 int ret;
548 init_sync_kiocb(&iocb, NULL);
549 ret = __sock_sendmsg(&iocb, sock, msg, size);
550 if (-EIOCBQUEUED == ret)
551 ret = wait_on_sync_kiocb(&iocb);
552 return ret;
556 static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, int size, int flags)
558 int err;
559 struct sock_iocb *si = kiocb_to_siocb(iocb);
561 si->sock = sock;
562 si->scm = NULL;
563 si->msg = msg;
564 si->size = size;
565 si->flags = flags;
567 err = security_socket_recvmsg(sock, msg, size, flags);
568 if (err)
569 return err;
571 return sock->ops->recvmsg(iocb, sock, msg, size, flags);
574 int sock_recvmsg(struct socket *sock, struct msghdr *msg, int size, int flags)
576 struct kiocb iocb;
577 int ret;
579 init_sync_kiocb(&iocb, NULL);
580 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
581 if (-EIOCBQUEUED == ret)
582 ret = wait_on_sync_kiocb(&iocb);
583 return ret;
587 * Read data from a socket. ubuf is a user mode pointer. We make sure the user
588 * area ubuf...ubuf+size-1 is writable before asking the protocol.
591 static ssize_t sock_aio_read(struct kiocb *iocb, char __user *ubuf,
592 size_t size, loff_t pos)
594 struct sock_iocb *x = kiocb_to_siocb(iocb);
595 struct socket *sock;
596 int flags;
598 if (pos != 0)
599 return -ESPIPE;
600 if (size==0) /* Match SYS5 behaviour */
601 return 0;
603 sock = SOCKET_I(iocb->ki_filp->f_dentry->d_inode);
605 x->async_msg.msg_name = NULL;
606 x->async_msg.msg_namelen = 0;
607 x->async_msg.msg_iov = &x->async_iov;
608 x->async_msg.msg_iovlen = 1;
609 x->async_msg.msg_control = NULL;
610 x->async_msg.msg_controllen = 0;
611 x->async_iov.iov_base = ubuf;
612 x->async_iov.iov_len = size;
613 flags = !(iocb->ki_filp->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
615 return __sock_recvmsg(iocb, sock, &x->async_msg, size, flags);
620 * Write data to a socket. We verify that the user area ubuf..ubuf+size-1
621 * is readable by the user process.
624 static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *ubuf,
625 size_t size, loff_t pos)
627 struct sock_iocb *x = kiocb_to_siocb(iocb);
628 struct socket *sock;
630 if (pos != 0)
631 return -ESPIPE;
632 if(size==0) /* Match SYS5 behaviour */
633 return 0;
635 sock = SOCKET_I(iocb->ki_filp->f_dentry->d_inode);
637 x->async_msg.msg_name = NULL;
638 x->async_msg.msg_namelen = 0;
639 x->async_msg.msg_iov = &x->async_iov;
640 x->async_msg.msg_iovlen = 1;
641 x->async_msg.msg_control = NULL;
642 x->async_msg.msg_controllen = 0;
643 x->async_msg.msg_flags = !(iocb->ki_filp->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
644 if (sock->type == SOCK_SEQPACKET)
645 x->async_msg.msg_flags |= MSG_EOR;
646 x->async_iov.iov_base = (void __user *)ubuf;
647 x->async_iov.iov_len = size;
649 return __sock_sendmsg(iocb, sock, &x->async_msg, size);
652 ssize_t sock_sendpage(struct file *file, struct page *page,
653 int offset, size_t size, loff_t *ppos, int more)
655 struct socket *sock;
656 int flags;
658 if (ppos != &file->f_pos)
659 return -ESPIPE;
661 sock = SOCKET_I(file->f_dentry->d_inode);
663 flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
664 if (more)
665 flags |= MSG_MORE;
667 return sock->ops->sendpage(sock, page, offset, size, flags);
670 int sock_readv_writev(int type, struct inode * inode, struct file * file,
671 const struct iovec * iov, long count, long size)
673 struct msghdr msg;
674 struct socket *sock;
676 sock = SOCKET_I(inode);
678 msg.msg_name = NULL;
679 msg.msg_namelen = 0;
680 msg.msg_control = NULL;
681 msg.msg_controllen = 0;
682 msg.msg_iov = (struct iovec *) iov;
683 msg.msg_iovlen = count;
684 msg.msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
686 /* read() does a VERIFY_WRITE */
687 if (type == VERIFY_WRITE)
688 return sock_recvmsg(sock, &msg, size, msg.msg_flags);
690 if (sock->type == SOCK_SEQPACKET)
691 msg.msg_flags |= MSG_EOR;
693 return sock_sendmsg(sock, &msg, size);
696 static ssize_t sock_readv(struct file *file, const struct iovec *vector,
697 unsigned long count, loff_t *ppos)
699 size_t tot_len = 0;
700 int i;
701 for (i = 0 ; i < count ; i++)
702 tot_len += vector[i].iov_len;
703 return sock_readv_writev(VERIFY_WRITE, file->f_dentry->d_inode,
704 file, vector, count, tot_len);
707 static ssize_t sock_writev(struct file *file, const struct iovec *vector,
708 unsigned long count, loff_t *ppos)
710 size_t tot_len = 0;
711 int i;
712 for (i = 0 ; i < count ; i++)
713 tot_len += vector[i].iov_len;
714 return sock_readv_writev(VERIFY_READ, file->f_dentry->d_inode,
715 file, vector, count, tot_len);
720 * Atomic setting of ioctl hooks to avoid race
721 * with module unload.
724 static DECLARE_MUTEX(br_ioctl_mutex);
725 static int (*br_ioctl_hook)(unsigned long arg) = NULL;
727 void brioctl_set(int (*hook)(unsigned long))
729 down(&br_ioctl_mutex);
730 br_ioctl_hook = hook;
731 up(&br_ioctl_mutex);
733 EXPORT_SYMBOL(brioctl_set);
735 static DECLARE_MUTEX(vlan_ioctl_mutex);
736 static int (*vlan_ioctl_hook)(unsigned long arg);
738 void vlan_ioctl_set(int (*hook)(unsigned long))
740 down(&vlan_ioctl_mutex);
741 vlan_ioctl_hook = hook;
742 up(&vlan_ioctl_mutex);
744 EXPORT_SYMBOL(vlan_ioctl_set);
746 static DECLARE_MUTEX(dlci_ioctl_mutex);
747 static int (*dlci_ioctl_hook)(unsigned int, void *);
749 void dlci_ioctl_set(int (*hook)(unsigned int, void *))
751 down(&dlci_ioctl_mutex);
752 dlci_ioctl_hook = hook;
753 up(&dlci_ioctl_mutex);
755 EXPORT_SYMBOL(dlci_ioctl_set);
758 * With an ioctl, arg may well be a user mode pointer, but we don't know
759 * what to do with it - that's up to the protocol still.
762 static int sock_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
763 unsigned long arg)
765 struct socket *sock;
766 int pid, err;
768 unlock_kernel();
769 sock = SOCKET_I(inode);
770 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
771 err = dev_ioctl(cmd, (void *)arg);
772 } else
773 #ifdef WIRELESS_EXT
774 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
775 err = dev_ioctl(cmd, (void *)arg);
776 } else
777 #endif /* WIRELESS_EXT */
778 switch (cmd) {
779 case FIOSETOWN:
780 case SIOCSPGRP:
781 err = -EFAULT;
782 if (get_user(pid, (int *)arg))
783 break;
784 err = f_setown(sock->file, pid, 1);
785 break;
786 case FIOGETOWN:
787 case SIOCGPGRP:
788 err = put_user(sock->file->f_owner.pid, (int *)arg);
789 break;
790 case SIOCGIFBR:
791 case SIOCSIFBR:
792 err = -ENOPKG;
793 if (!br_ioctl_hook)
794 request_module("bridge");
796 down(&br_ioctl_mutex);
797 if (br_ioctl_hook)
798 err = br_ioctl_hook(arg);
799 up(&br_ioctl_mutex);
800 break;
801 case SIOCGIFVLAN:
802 case SIOCSIFVLAN:
803 err = -ENOPKG;
804 if (!vlan_ioctl_hook)
805 request_module("8021q");
807 down(&vlan_ioctl_mutex);
808 if (vlan_ioctl_hook)
809 err = vlan_ioctl_hook(arg);
810 up(&vlan_ioctl_mutex);
811 break;
812 case SIOCGIFDIVERT:
813 case SIOCSIFDIVERT:
814 /* Convert this to call through a hook */
815 err = divert_ioctl(cmd, (struct divert_cf *)arg);
816 break;
817 case SIOCADDDLCI:
818 case SIOCDELDLCI:
819 err = -ENOPKG;
820 if (!dlci_ioctl_hook)
821 request_module("dlci");
823 if (dlci_ioctl_hook) {
824 down(&dlci_ioctl_mutex);
825 err = dlci_ioctl_hook(cmd, (void *)arg);
826 up(&dlci_ioctl_mutex);
828 break;
829 default:
830 err = sock->ops->ioctl(sock, cmd, arg);
831 break;
833 lock_kernel();
835 return err;
839 /* No kernel lock held - perfect */
840 static unsigned int sock_poll(struct file *file, poll_table * wait)
842 struct socket *sock;
845 * We can't return errors to poll, so it's either yes or no.
847 sock = SOCKET_I(file->f_dentry->d_inode);
848 return sock->ops->poll(file, sock, wait);
851 static int sock_mmap(struct file * file, struct vm_area_struct * vma)
853 struct socket *sock = SOCKET_I(file->f_dentry->d_inode);
855 return sock->ops->mmap(file, sock, vma);
858 int sock_close(struct inode *inode, struct file *filp)
861 * It was possible the inode is NULL we were
862 * closing an unfinished socket.
865 if (!inode)
867 printk(KERN_DEBUG "sock_close: NULL inode\n");
868 return 0;
870 sock_fasync(-1, filp, 0);
871 sock_release(SOCKET_I(inode));
872 return 0;
876 * Update the socket async list
878 * Fasync_list locking strategy.
880 * 1. fasync_list is modified only under process context socket lock
881 * i.e. under semaphore.
882 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
883 * or under socket lock.
884 * 3. fasync_list can be used from softirq context, so that
885 * modification under socket lock have to be enhanced with
886 * write_lock_bh(&sk->sk_callback_lock).
887 * --ANK (990710)
890 static int sock_fasync(int fd, struct file *filp, int on)
892 struct fasync_struct *fa, *fna=NULL, **prev;
893 struct socket *sock;
894 struct sock *sk;
896 if (on)
898 fna=(struct fasync_struct *)kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
899 if(fna==NULL)
900 return -ENOMEM;
903 sock = SOCKET_I(filp->f_dentry->d_inode);
905 if ((sk=sock->sk) == NULL) {
906 if (fna)
907 kfree(fna);
908 return -EINVAL;
911 lock_sock(sk);
913 prev=&(sock->fasync_list);
915 for (fa=*prev; fa!=NULL; prev=&fa->fa_next,fa=*prev)
916 if (fa->fa_file==filp)
917 break;
919 if(on)
921 if(fa!=NULL)
923 write_lock_bh(&sk->sk_callback_lock);
924 fa->fa_fd=fd;
925 write_unlock_bh(&sk->sk_callback_lock);
927 kfree(fna);
928 goto out;
930 fna->fa_file=filp;
931 fna->fa_fd=fd;
932 fna->magic=FASYNC_MAGIC;
933 fna->fa_next=sock->fasync_list;
934 write_lock_bh(&sk->sk_callback_lock);
935 sock->fasync_list=fna;
936 write_unlock_bh(&sk->sk_callback_lock);
938 else
940 if (fa!=NULL)
942 write_lock_bh(&sk->sk_callback_lock);
943 *prev=fa->fa_next;
944 write_unlock_bh(&sk->sk_callback_lock);
945 kfree(fa);
949 out:
950 release_sock(sock->sk);
951 return 0;
954 /* This function may be called only under socket lock or callback_lock */
956 int sock_wake_async(struct socket *sock, int how, int band)
958 if (!sock || !sock->fasync_list)
959 return -1;
960 switch (how)
962 case 1:
964 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
965 break;
966 goto call_kill;
967 case 2:
968 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
969 break;
970 /* fall through */
971 case 0:
972 call_kill:
973 __kill_fasync(sock->fasync_list, SIGIO, band);
974 break;
975 case 3:
976 __kill_fasync(sock->fasync_list, SIGURG, band);
978 return 0;
982 int sock_create(int family, int type, int protocol, struct socket **res)
984 int i;
985 int err;
986 struct socket *sock;
989 * Check protocol is in range
991 if (family < 0 || family >= NPROTO)
992 return -EAFNOSUPPORT;
993 if (type < 0 || type >= SOCK_MAX)
994 return -EINVAL;
996 /* Compatibility.
998 This uglymoron is moved from INET layer to here to avoid
999 deadlock in module load.
1001 if (family == PF_INET && type == SOCK_PACKET) {
1002 static int warned;
1003 if (!warned) {
1004 warned = 1;
1005 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n", current->comm);
1007 family = PF_PACKET;
1010 err = security_socket_create(family, type, protocol);
1011 if (err)
1012 return err;
1014 #if defined(CONFIG_KMOD)
1015 /* Attempt to load a protocol module if the find failed.
1017 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1018 * requested real, full-featured networking support upon configuration.
1019 * Otherwise module support will break!
1021 if (net_families[family]==NULL)
1023 request_module("net-pf-%d",family);
1025 #endif
1027 net_family_read_lock();
1028 if (net_families[family] == NULL) {
1029 i = -EAFNOSUPPORT;
1030 goto out;
1034 * Allocate the socket and allow the family to set things up. if
1035 * the protocol is 0, the family is instructed to select an appropriate
1036 * default.
1039 if (!(sock = sock_alloc()))
1041 printk(KERN_WARNING "socket: no more sockets\n");
1042 i = -ENFILE; /* Not exactly a match, but its the
1043 closest posix thing */
1044 goto out;
1047 sock->type = type;
1050 * We will call the ->create function, that possibly is in a loadable
1051 * module, so we have to bump that loadable module refcnt first.
1053 i = -EAFNOSUPPORT;
1054 if (!try_module_get(net_families[family]->owner))
1055 goto out_release;
1057 if ((i = net_families[family]->create(sock, protocol)) < 0)
1058 goto out_module_put;
1060 * Now to bump the refcnt of the [loadable] module that owns this
1061 * socket at sock_release time we decrement its refcnt.
1063 if (!try_module_get(sock->ops->owner)) {
1064 sock->ops = NULL;
1065 goto out_module_put;
1068 * Now that we're done with the ->create function, the [loadable]
1069 * module can have its refcnt decremented
1071 module_put(net_families[family]->owner);
1072 *res = sock;
1073 security_socket_post_create(sock, family, type, protocol);
1075 out:
1076 net_family_read_unlock();
1077 return i;
1078 out_module_put:
1079 module_put(net_families[family]->owner);
1080 out_release:
1081 sock_release(sock);
1082 goto out;
1085 asmlinkage long sys_socket(int family, int type, int protocol)
1087 int retval;
1088 struct socket *sock;
1090 retval = sock_create(family, type, protocol, &sock);
1091 if (retval < 0)
1092 goto out;
1094 retval = sock_map_fd(sock);
1095 if (retval < 0)
1096 goto out_release;
1098 out:
1099 /* It may be already another descriptor 8) Not kernel problem. */
1100 return retval;
1102 out_release:
1103 sock_release(sock);
1104 return retval;
1108 * Create a pair of connected sockets.
1111 asmlinkage long sys_socketpair(int family, int type, int protocol, int __user *usockvec)
1113 struct socket *sock1, *sock2;
1114 int fd1, fd2, err;
1117 * Obtain the first socket and check if the underlying protocol
1118 * supports the socketpair call.
1121 err = sock_create(family, type, protocol, &sock1);
1122 if (err < 0)
1123 goto out;
1125 err = sock_create(family, type, protocol, &sock2);
1126 if (err < 0)
1127 goto out_release_1;
1129 err = sock1->ops->socketpair(sock1, sock2);
1130 if (err < 0)
1131 goto out_release_both;
1133 fd1 = fd2 = -1;
1135 err = sock_map_fd(sock1);
1136 if (err < 0)
1137 goto out_release_both;
1138 fd1 = err;
1140 err = sock_map_fd(sock2);
1141 if (err < 0)
1142 goto out_close_1;
1143 fd2 = err;
1145 /* fd1 and fd2 may be already another descriptors.
1146 * Not kernel problem.
1149 err = put_user(fd1, &usockvec[0]);
1150 if (!err)
1151 err = put_user(fd2, &usockvec[1]);
1152 if (!err)
1153 return 0;
1155 sys_close(fd2);
1156 sys_close(fd1);
1157 return err;
1159 out_close_1:
1160 sock_release(sock2);
1161 sys_close(fd1);
1162 return err;
1164 out_release_both:
1165 sock_release(sock2);
1166 out_release_1:
1167 sock_release(sock1);
1168 out:
1169 return err;
1174 * Bind a name to a socket. Nothing much to do here since it's
1175 * the protocol's responsibility to handle the local address.
1177 * We move the socket address to kernel space before we call
1178 * the protocol layer (having also checked the address is ok).
1181 asmlinkage long sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
1183 struct socket *sock;
1184 char address[MAX_SOCK_ADDR];
1185 int err;
1187 if((sock = sockfd_lookup(fd,&err))!=NULL)
1189 if((err=move_addr_to_kernel(umyaddr,addrlen,address))>=0) {
1190 err = security_socket_bind(sock, (struct sockaddr *)address, addrlen);
1191 if (err) {
1192 sockfd_put(sock);
1193 return err;
1195 err = sock->ops->bind(sock, (struct sockaddr *)address, addrlen);
1197 sockfd_put(sock);
1199 return err;
1204 * Perform a listen. Basically, we allow the protocol to do anything
1205 * necessary for a listen, and if that works, we mark the socket as
1206 * ready for listening.
1209 asmlinkage long sys_listen(int fd, int backlog)
1211 struct socket *sock;
1212 int err;
1214 if ((sock = sockfd_lookup(fd, &err)) != NULL) {
1215 if ((unsigned) backlog > SOMAXCONN)
1216 backlog = SOMAXCONN;
1218 err = security_socket_listen(sock, backlog);
1219 if (err) {
1220 sockfd_put(sock);
1221 return err;
1224 err=sock->ops->listen(sock, backlog);
1225 sockfd_put(sock);
1227 return err;
1232 * For accept, we attempt to create a new socket, set up the link
1233 * with the client, wake up the client, then return the new
1234 * connected fd. We collect the address of the connector in kernel
1235 * space and move it to user at the very end. This is unclean because
1236 * we open the socket then return an error.
1238 * 1003.1g adds the ability to recvmsg() to query connection pending
1239 * status to recvmsg. We need to add that support in a way thats
1240 * clean when we restucture accept also.
1243 asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr, int __user *upeer_addrlen)
1245 struct socket *sock, *newsock;
1246 int err, len;
1247 char address[MAX_SOCK_ADDR];
1249 sock = sockfd_lookup(fd, &err);
1250 if (!sock)
1251 goto out;
1253 err = -EMFILE;
1254 if (!(newsock = sock_alloc()))
1255 goto out_put;
1257 newsock->type = sock->type;
1258 newsock->ops = sock->ops;
1260 err = security_socket_accept(sock, newsock);
1261 if (err)
1262 goto out_release;
1265 * We don't need try_module_get here, as the listening socket (sock)
1266 * has the protocol module (sock->ops->owner) held.
1268 __module_get(newsock->ops->owner);
1270 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1271 if (err < 0)
1272 goto out_release;
1274 if (upeer_sockaddr) {
1275 if(newsock->ops->getname(newsock, (struct sockaddr *)address, &len, 2)<0) {
1276 err = -ECONNABORTED;
1277 goto out_release;
1279 err = move_addr_to_user(address, len, upeer_sockaddr, upeer_addrlen);
1280 if (err < 0)
1281 goto out_release;
1284 /* File flags are not inherited via accept() unlike another OSes. */
1286 if ((err = sock_map_fd(newsock)) < 0)
1287 goto out_release;
1289 security_socket_post_accept(sock, newsock);
1291 out_put:
1292 sockfd_put(sock);
1293 out:
1294 return err;
1295 out_release:
1296 sock_release(newsock);
1297 goto out_put;
1302 * Attempt to connect to a socket with the server address. The address
1303 * is in user space so we verify it is OK and move it to kernel space.
1305 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1306 * break bindings
1308 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1309 * other SEQPACKET protocols that take time to connect() as it doesn't
1310 * include the -EINPROGRESS status for such sockets.
1313 asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)
1315 struct socket *sock;
1316 char address[MAX_SOCK_ADDR];
1317 int err;
1319 sock = sockfd_lookup(fd, &err);
1320 if (!sock)
1321 goto out;
1322 err = move_addr_to_kernel(uservaddr, addrlen, address);
1323 if (err < 0)
1324 goto out_put;
1326 err = security_socket_connect(sock, (struct sockaddr *)address, addrlen);
1327 if (err)
1328 goto out_put;
1330 err = sock->ops->connect(sock, (struct sockaddr *) address, addrlen,
1331 sock->file->f_flags);
1332 out_put:
1333 sockfd_put(sock);
1334 out:
1335 return err;
1339 * Get the local address ('name') of a socket object. Move the obtained
1340 * name to user space.
1343 asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len)
1345 struct socket *sock;
1346 char address[MAX_SOCK_ADDR];
1347 int len, err;
1349 sock = sockfd_lookup(fd, &err);
1350 if (!sock)
1351 goto out;
1353 err = security_socket_getsockname(sock);
1354 if (err)
1355 goto out_put;
1357 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 0);
1358 if (err)
1359 goto out_put;
1360 err = move_addr_to_user(address, len, usockaddr, usockaddr_len);
1362 out_put:
1363 sockfd_put(sock);
1364 out:
1365 return err;
1369 * Get the remote address ('name') of a socket object. Move the obtained
1370 * name to user space.
1373 asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len)
1375 struct socket *sock;
1376 char address[MAX_SOCK_ADDR];
1377 int len, err;
1379 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1381 err = security_socket_getpeername(sock);
1382 if (err) {
1383 sockfd_put(sock);
1384 return err;
1387 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 1);
1388 if (!err)
1389 err=move_addr_to_user(address,len, usockaddr, usockaddr_len);
1390 sockfd_put(sock);
1392 return err;
1396 * Send a datagram to a given address. We move the address into kernel
1397 * space and check the user space data area is readable before invoking
1398 * the protocol.
1401 asmlinkage long sys_sendto(int fd, void __user * buff, size_t len, unsigned flags,
1402 struct sockaddr __user *addr, int addr_len)
1404 struct socket *sock;
1405 char address[MAX_SOCK_ADDR];
1406 int err;
1407 struct msghdr msg;
1408 struct iovec iov;
1410 sock = sockfd_lookup(fd, &err);
1411 if (!sock)
1412 goto out;
1413 iov.iov_base=buff;
1414 iov.iov_len=len;
1415 msg.msg_name=NULL;
1416 msg.msg_iov=&iov;
1417 msg.msg_iovlen=1;
1418 msg.msg_control=NULL;
1419 msg.msg_controllen=0;
1420 msg.msg_namelen=0;
1421 if(addr)
1423 err = move_addr_to_kernel(addr, addr_len, address);
1424 if (err < 0)
1425 goto out_put;
1426 msg.msg_name=address;
1427 msg.msg_namelen=addr_len;
1429 if (sock->file->f_flags & O_NONBLOCK)
1430 flags |= MSG_DONTWAIT;
1431 msg.msg_flags = flags;
1432 err = sock_sendmsg(sock, &msg, len);
1434 out_put:
1435 sockfd_put(sock);
1436 out:
1437 return err;
1441 * Send a datagram down a socket.
1444 asmlinkage long sys_send(int fd, void __user * buff, size_t len, unsigned flags)
1446 return sys_sendto(fd, buff, len, flags, NULL, 0);
1450 * Receive a frame from the socket and optionally record the address of the
1451 * sender. We verify the buffers are writable and if needed move the
1452 * sender address from kernel to user space.
1455 asmlinkage long sys_recvfrom(int fd, void __user * ubuf, size_t size, unsigned flags,
1456 struct sockaddr __user *addr, int __user *addr_len)
1458 struct socket *sock;
1459 struct iovec iov;
1460 struct msghdr msg;
1461 char address[MAX_SOCK_ADDR];
1462 int err,err2;
1464 sock = sockfd_lookup(fd, &err);
1465 if (!sock)
1466 goto out;
1468 msg.msg_control=NULL;
1469 msg.msg_controllen=0;
1470 msg.msg_iovlen=1;
1471 msg.msg_iov=&iov;
1472 iov.iov_len=size;
1473 iov.iov_base=ubuf;
1474 msg.msg_name=address;
1475 msg.msg_namelen=MAX_SOCK_ADDR;
1476 if (sock->file->f_flags & O_NONBLOCK)
1477 flags |= MSG_DONTWAIT;
1478 err=sock_recvmsg(sock, &msg, size, flags);
1480 if(err >= 0 && addr != NULL)
1482 err2=move_addr_to_user(address, msg.msg_namelen, addr, addr_len);
1483 if(err2<0)
1484 err=err2;
1486 sockfd_put(sock);
1487 out:
1488 return err;
1492 * Receive a datagram from a socket.
1495 asmlinkage long sys_recv(int fd, void __user * ubuf, size_t size, unsigned flags)
1497 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1501 * Set a socket option. Because we don't know the option lengths we have
1502 * to pass the user mode parameter for the protocols to sort out.
1505 asmlinkage long sys_setsockopt(int fd, int level, int optname, char __user *optval, int optlen)
1507 int err;
1508 struct socket *sock;
1510 if (optlen < 0)
1511 return -EINVAL;
1513 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1515 err = security_socket_setsockopt(sock,level,optname);
1516 if (err) {
1517 sockfd_put(sock);
1518 return err;
1521 if (level == SOL_SOCKET)
1522 err=sock_setsockopt(sock,level,optname,optval,optlen);
1523 else
1524 err=sock->ops->setsockopt(sock, level, optname, optval, optlen);
1525 sockfd_put(sock);
1527 return err;
1531 * Get a socket option. Because we don't know the option lengths we have
1532 * to pass a user mode parameter for the protocols to sort out.
1535 asmlinkage long sys_getsockopt(int fd, int level, int optname, char __user *optval, int __user *optlen)
1537 int err;
1538 struct socket *sock;
1540 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1542 err = security_socket_getsockopt(sock, level,
1543 optname);
1544 if (err) {
1545 sockfd_put(sock);
1546 return err;
1549 if (level == SOL_SOCKET)
1550 err=sock_getsockopt(sock,level,optname,optval,optlen);
1551 else
1552 err=sock->ops->getsockopt(sock, level, optname, optval, optlen);
1553 sockfd_put(sock);
1555 return err;
1560 * Shutdown a socket.
1563 asmlinkage long sys_shutdown(int fd, int how)
1565 int err;
1566 struct socket *sock;
1568 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1570 err = security_socket_shutdown(sock, how);
1571 if (err) {
1572 sockfd_put(sock);
1573 return err;
1576 err=sock->ops->shutdown(sock, how);
1577 sockfd_put(sock);
1579 return err;
1582 /* A couple of helpful macros for getting the address of the 32/64 bit
1583 * fields which are the same type (int / unsigned) on our platforms.
1585 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1586 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1587 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1591 * BSD sendmsg interface
1594 asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
1596 struct compat_msghdr __user *msg_compat = (struct compat_msghdr __user *)msg;
1597 struct socket *sock;
1598 char address[MAX_SOCK_ADDR];
1599 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1600 unsigned char ctl[sizeof(struct cmsghdr) + 20]; /* 20 is size of ipv6_pktinfo */
1601 unsigned char *ctl_buf = ctl;
1602 struct msghdr msg_sys;
1603 int err, ctl_len, iov_size, total_len;
1605 err = -EFAULT;
1606 if (MSG_CMSG_COMPAT & flags) {
1607 if (get_compat_msghdr(&msg_sys, msg_compat))
1608 return -EFAULT;
1609 } else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
1610 return -EFAULT;
1612 sock = sockfd_lookup(fd, &err);
1613 if (!sock)
1614 goto out;
1616 /* do not move before msg_sys is valid */
1617 err = -EMSGSIZE;
1618 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1619 goto out_put;
1621 /* Check whether to allocate the iovec area*/
1622 err = -ENOMEM;
1623 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1624 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1625 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1626 if (!iov)
1627 goto out_put;
1630 /* This will also move the address data into kernel space */
1631 if (MSG_CMSG_COMPAT & flags) {
1632 err = verify_compat_iovec(&msg_sys, iov, address, VERIFY_READ);
1633 } else
1634 err = verify_iovec(&msg_sys, iov, address, VERIFY_READ);
1635 if (err < 0)
1636 goto out_freeiov;
1637 total_len = err;
1639 err = -ENOBUFS;
1641 if (msg_sys.msg_controllen > INT_MAX)
1642 goto out_freeiov;
1643 ctl_len = msg_sys.msg_controllen;
1644 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
1645 err = cmsghdr_from_user_compat_to_kern(&msg_sys, ctl, sizeof(ctl));
1646 if (err)
1647 goto out_freeiov;
1648 ctl_buf = msg_sys.msg_control;
1649 } else if (ctl_len) {
1650 if (ctl_len > sizeof(ctl))
1652 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
1653 if (ctl_buf == NULL)
1654 goto out_freeiov;
1656 err = -EFAULT;
1658 * Careful! Before this, msg_sys.msg_control contains a user pointer.
1659 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1660 * checking falls down on this.
1662 if (copy_from_user(ctl_buf, (void __user *) msg_sys.msg_control, ctl_len))
1663 goto out_freectl;
1664 msg_sys.msg_control = ctl_buf;
1666 msg_sys.msg_flags = flags;
1668 if (sock->file->f_flags & O_NONBLOCK)
1669 msg_sys.msg_flags |= MSG_DONTWAIT;
1670 err = sock_sendmsg(sock, &msg_sys, total_len);
1672 out_freectl:
1673 if (ctl_buf != ctl)
1674 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1675 out_freeiov:
1676 if (iov != iovstack)
1677 sock_kfree_s(sock->sk, iov, iov_size);
1678 out_put:
1679 sockfd_put(sock);
1680 out:
1681 return err;
1685 * BSD recvmsg interface
1688 asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flags)
1690 struct compat_msghdr __user *msg_compat = (struct compat_msghdr __user *)msg;
1691 struct socket *sock;
1692 struct iovec iovstack[UIO_FASTIOV];
1693 struct iovec *iov=iovstack;
1694 struct msghdr msg_sys;
1695 unsigned long cmsg_ptr;
1696 int err, iov_size, total_len, len;
1698 /* kernel mode address */
1699 char addr[MAX_SOCK_ADDR];
1701 /* user mode address pointers */
1702 struct sockaddr __user *uaddr;
1703 int __user *uaddr_len;
1705 if (MSG_CMSG_COMPAT & flags) {
1706 if (get_compat_msghdr(&msg_sys, msg_compat))
1707 return -EFAULT;
1708 } else
1709 if (copy_from_user(&msg_sys,msg,sizeof(struct msghdr)))
1710 return -EFAULT;
1712 sock = sockfd_lookup(fd, &err);
1713 if (!sock)
1714 goto out;
1716 err = -EMSGSIZE;
1717 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1718 goto out_put;
1720 /* Check whether to allocate the iovec area*/
1721 err = -ENOMEM;
1722 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1723 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1724 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1725 if (!iov)
1726 goto out_put;
1730 * Save the user-mode address (verify_iovec will change the
1731 * kernel msghdr to use the kernel address space)
1734 uaddr = (void __user *) msg_sys.msg_name;
1735 uaddr_len = COMPAT_NAMELEN(msg);
1736 if (MSG_CMSG_COMPAT & flags) {
1737 err = verify_compat_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1738 } else
1739 err = verify_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1740 if (err < 0)
1741 goto out_freeiov;
1742 total_len=err;
1744 cmsg_ptr = (unsigned long)msg_sys.msg_control;
1745 msg_sys.msg_flags = 0;
1746 if (MSG_CMSG_COMPAT & flags)
1747 msg_sys.msg_flags = MSG_CMSG_COMPAT;
1749 if (sock->file->f_flags & O_NONBLOCK)
1750 flags |= MSG_DONTWAIT;
1751 err = sock_recvmsg(sock, &msg_sys, total_len, flags);
1752 if (err < 0)
1753 goto out_freeiov;
1754 len = err;
1756 if (uaddr != NULL) {
1757 err = move_addr_to_user(addr, msg_sys.msg_namelen, uaddr, uaddr_len);
1758 if (err < 0)
1759 goto out_freeiov;
1761 err = __put_user(msg_sys.msg_flags, COMPAT_FLAGS(msg));
1762 if (err)
1763 goto out_freeiov;
1764 if (MSG_CMSG_COMPAT & flags)
1765 err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr,
1766 &msg_compat->msg_controllen);
1767 else
1768 err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr,
1769 &msg->msg_controllen);
1770 if (err)
1771 goto out_freeiov;
1772 err = len;
1774 out_freeiov:
1775 if (iov != iovstack)
1776 sock_kfree_s(sock->sk, iov, iov_size);
1777 out_put:
1778 sockfd_put(sock);
1779 out:
1780 return err;
1783 /* Argument list sizes for sys_socketcall */
1784 #define AL(x) ((x) * sizeof(unsigned long))
1785 static unsigned char nargs[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
1786 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
1787 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)};
1788 #undef AL
1791 * System call vectors.
1793 * Argument checking cleaned up. Saved 20% in size.
1794 * This function doesn't need to set the kernel lock because
1795 * it is set by the callees.
1798 asmlinkage long sys_socketcall(int call, unsigned long __user *args)
1800 unsigned long a[6];
1801 unsigned long a0,a1;
1802 int err;
1804 if(call<1||call>SYS_RECVMSG)
1805 return -EINVAL;
1807 /* copy_from_user should be SMP safe. */
1808 if (copy_from_user(a, args, nargs[call]))
1809 return -EFAULT;
1811 a0=a[0];
1812 a1=a[1];
1814 switch(call)
1816 case SYS_SOCKET:
1817 err = sys_socket(a0,a1,a[2]);
1818 break;
1819 case SYS_BIND:
1820 err = sys_bind(a0,(struct sockaddr __user *)a1, a[2]);
1821 break;
1822 case SYS_CONNECT:
1823 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
1824 break;
1825 case SYS_LISTEN:
1826 err = sys_listen(a0,a1);
1827 break;
1828 case SYS_ACCEPT:
1829 err = sys_accept(a0,(struct sockaddr __user *)a1, (int __user *)a[2]);
1830 break;
1831 case SYS_GETSOCKNAME:
1832 err = sys_getsockname(a0,(struct sockaddr __user *)a1, (int __user *)a[2]);
1833 break;
1834 case SYS_GETPEERNAME:
1835 err = sys_getpeername(a0, (struct sockaddr __user *)a1, (int __user *)a[2]);
1836 break;
1837 case SYS_SOCKETPAIR:
1838 err = sys_socketpair(a0,a1, a[2], (int __user *)a[3]);
1839 break;
1840 case SYS_SEND:
1841 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
1842 break;
1843 case SYS_SENDTO:
1844 err = sys_sendto(a0,(void __user *)a1, a[2], a[3],
1845 (struct sockaddr __user *)a[4], a[5]);
1846 break;
1847 case SYS_RECV:
1848 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
1849 break;
1850 case SYS_RECVFROM:
1851 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
1852 (struct sockaddr __user *)a[4], (int __user *)a[5]);
1853 break;
1854 case SYS_SHUTDOWN:
1855 err = sys_shutdown(a0,a1);
1856 break;
1857 case SYS_SETSOCKOPT:
1858 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
1859 break;
1860 case SYS_GETSOCKOPT:
1861 err = sys_getsockopt(a0, a1, a[2], (char __user *)a[3], (int __user *)a[4]);
1862 break;
1863 case SYS_SENDMSG:
1864 err = sys_sendmsg(a0, (struct msghdr __user *) a1, a[2]);
1865 break;
1866 case SYS_RECVMSG:
1867 err = sys_recvmsg(a0, (struct msghdr __user *) a1, a[2]);
1868 break;
1869 default:
1870 err = -EINVAL;
1871 break;
1873 return err;
1877 * This function is called by a protocol handler that wants to
1878 * advertise its address family, and have it linked into the
1879 * SOCKET module.
1882 int sock_register(struct net_proto_family *ops)
1884 int err;
1886 if (ops->family >= NPROTO) {
1887 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
1888 return -ENOBUFS;
1890 net_family_write_lock();
1891 err = -EEXIST;
1892 if (net_families[ops->family] == NULL) {
1893 net_families[ops->family]=ops;
1894 err = 0;
1896 net_family_write_unlock();
1897 return err;
1901 * This function is called by a protocol handler that wants to
1902 * remove its address family, and have it unlinked from the
1903 * SOCKET module.
1906 int sock_unregister(int family)
1908 if (family < 0 || family >= NPROTO)
1909 return -1;
1911 net_family_write_lock();
1912 net_families[family]=NULL;
1913 net_family_write_unlock();
1914 return 0;
1918 extern void sk_init(void);
1920 #ifdef CONFIG_WAN_ROUTER
1921 extern void wanrouter_init(void);
1922 #endif
1924 void __init sock_init(void)
1926 int i;
1929 * Initialize all address (protocol) families.
1932 for (i = 0; i < NPROTO; i++)
1933 net_families[i] = NULL;
1936 * Initialize sock SLAB cache.
1939 sk_init();
1941 #ifdef SLAB_SKB
1943 * Initialize skbuff SLAB cache
1945 skb_init();
1946 #endif
1949 * Wan router layer.
1952 #ifdef CONFIG_WAN_ROUTER
1953 wanrouter_init();
1954 #endif
1957 * Initialize the protocols module.
1960 init_inodecache();
1961 register_filesystem(&sock_fs_type);
1962 sock_mnt = kern_mount(&sock_fs_type);
1963 /* The real protocol initialization is performed when
1964 * do_initcalls is run.
1967 #ifdef CONFIG_NETFILTER
1968 netfilter_init();
1969 #endif
1972 #ifdef CONFIG_PROC_FS
1973 void socket_seq_show(struct seq_file *seq)
1975 int cpu;
1976 int counter = 0;
1978 for (cpu = 0; cpu < NR_CPUS; cpu++)
1979 counter += per_cpu(sockets_in_use, cpu);
1981 /* It can be negative, by the way. 8) */
1982 if (counter < 0)
1983 counter = 0;
1985 seq_printf(seq, "sockets: used %d\n", counter);
1987 #endif /* CONFIG_PROC_FS */