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>
8 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
11 * Anonymous : NOTSOCK/BADF cleanup. Error fix in
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
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
23 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
24 * Jeff Uphoff : Made max number of sockets command-line
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
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
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
58 * Based upon Swansea University Computer Society NET3.039
62 #include <linux/socket.h>
63 #include <linux/file.h>
64 #include <linux/net.h>
65 #include <linux/interrupt.h>
66 #include <linux/rcupdate.h>
67 #include <linux/netdevice.h>
68 #include <linux/proc_fs.h>
69 #include <linux/seq_file.h>
70 #include <linux/mutex.h>
71 #include <linux/wanrouter.h>
72 #include <linux/if_bridge.h>
73 #include <linux/if_frad.h>
74 #include <linux/if_vlan.h>
75 #include <linux/init.h>
76 #include <linux/poll.h>
77 #include <linux/cache.h>
78 #include <linux/module.h>
79 #include <linux/highmem.h>
80 #include <linux/divert.h>
81 #include <linux/mount.h>
82 #include <linux/security.h>
83 #include <linux/syscalls.h>
84 #include <linux/compat.h>
85 #include <linux/kmod.h>
86 #include <linux/audit.h>
87 #include <linux/wireless.h>
89 #include <asm/uaccess.h>
90 #include <asm/unistd.h>
92 #include <net/compat.h>
95 #include <linux/netfilter.h>
97 static int sock_no_open(struct inode
*irrelevant
, struct file
*dontcare
);
98 static ssize_t
sock_aio_read(struct kiocb
*iocb
, char __user
*buf
,
99 size_t size
, loff_t pos
);
100 static ssize_t
sock_aio_write(struct kiocb
*iocb
, const char __user
*buf
,
101 size_t size
, loff_t pos
);
102 static int sock_mmap(struct file
*file
, struct vm_area_struct
*vma
);
104 static int sock_close(struct inode
*inode
, struct file
*file
);
105 static unsigned int sock_poll(struct file
*file
,
106 struct poll_table_struct
*wait
);
107 static long sock_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
);
109 static long compat_sock_ioctl(struct file
*file
,
110 unsigned int cmd
, unsigned long arg
);
112 static int sock_fasync(int fd
, struct file
*filp
, int on
);
113 static ssize_t
sock_readv(struct file
*file
, const struct iovec
*vector
,
114 unsigned long count
, loff_t
*ppos
);
115 static ssize_t
sock_writev(struct file
*file
, const struct iovec
*vector
,
116 unsigned long count
, loff_t
*ppos
);
117 static ssize_t
sock_sendpage(struct file
*file
, struct page
*page
,
118 int offset
, size_t size
, loff_t
*ppos
, int more
);
121 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
122 * in the operation structures but are done directly via the socketcall() multiplexor.
125 static struct file_operations socket_file_ops
= {
126 .owner
= THIS_MODULE
,
128 .aio_read
= sock_aio_read
,
129 .aio_write
= sock_aio_write
,
131 .unlocked_ioctl
= sock_ioctl
,
133 .compat_ioctl
= compat_sock_ioctl
,
136 .open
= sock_no_open
, /* special open code to disallow open via /proc */
137 .release
= sock_close
,
138 .fasync
= sock_fasync
,
140 .writev
= sock_writev
,
141 .sendpage
= sock_sendpage
,
142 .splice_write
= generic_splice_sendpage
,
146 * The protocol list. Each protocol is registered in here.
149 static DEFINE_SPINLOCK(net_family_lock
);
150 static const struct net_proto_family
*net_families
[NPROTO
] __read_mostly
;
153 * Statistics counters of the socket lists
156 static DEFINE_PER_CPU(int, sockets_in_use
) = 0;
160 * Move socket addresses back and forth across the kernel/user
161 * divide and look after the messy bits.
164 #define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
165 16 for IP, 16 for IPX,
168 must be at least one bigger than
169 the AF_UNIX size (see net/unix/af_unix.c
174 * move_addr_to_kernel - copy a socket address into kernel space
175 * @uaddr: Address in user space
176 * @kaddr: Address in kernel space
177 * @ulen: Length in user space
179 * The address is copied into kernel space. If the provided address is
180 * too long an error code of -EINVAL is returned. If the copy gives
181 * invalid addresses -EFAULT is returned. On a success 0 is returned.
184 int move_addr_to_kernel(void __user
*uaddr
, int ulen
, void *kaddr
)
186 if (ulen
< 0 || ulen
> MAX_SOCK_ADDR
)
190 if (copy_from_user(kaddr
, uaddr
, ulen
))
192 return audit_sockaddr(ulen
, kaddr
);
196 * move_addr_to_user - copy an address to user space
197 * @kaddr: kernel space address
198 * @klen: length of address in kernel
199 * @uaddr: user space address
200 * @ulen: pointer to user length field
202 * The value pointed to by ulen on entry is the buffer length available.
203 * This is overwritten with the buffer space used. -EINVAL is returned
204 * if an overlong buffer is specified or a negative buffer size. -EFAULT
205 * is returned if either the buffer or the length field are not
207 * After copying the data up to the limit the user specifies, the true
208 * length of the data is written over the length limit the user
209 * specified. Zero is returned for a success.
212 int move_addr_to_user(void *kaddr
, int klen
, void __user
*uaddr
,
218 err
= get_user(len
, ulen
);
223 if (len
< 0 || len
> MAX_SOCK_ADDR
)
226 if (audit_sockaddr(klen
, kaddr
))
228 if (copy_to_user(uaddr
, kaddr
, len
))
232 * "fromlen shall refer to the value before truncation.."
235 return __put_user(klen
, ulen
);
238 #define SOCKFS_MAGIC 0x534F434B
240 static kmem_cache_t
*sock_inode_cachep __read_mostly
;
242 static struct inode
*sock_alloc_inode(struct super_block
*sb
)
244 struct socket_alloc
*ei
;
246 ei
= kmem_cache_alloc(sock_inode_cachep
, SLAB_KERNEL
);
249 init_waitqueue_head(&ei
->socket
.wait
);
251 ei
->socket
.fasync_list
= NULL
;
252 ei
->socket
.state
= SS_UNCONNECTED
;
253 ei
->socket
.flags
= 0;
254 ei
->socket
.ops
= NULL
;
255 ei
->socket
.sk
= NULL
;
256 ei
->socket
.file
= NULL
;
258 return &ei
->vfs_inode
;
261 static void sock_destroy_inode(struct inode
*inode
)
263 kmem_cache_free(sock_inode_cachep
,
264 container_of(inode
, struct socket_alloc
, vfs_inode
));
267 static void init_once(void *foo
, kmem_cache_t
*cachep
, unsigned long flags
)
269 struct socket_alloc
*ei
= (struct socket_alloc
*)foo
;
271 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
))
272 == SLAB_CTOR_CONSTRUCTOR
)
273 inode_init_once(&ei
->vfs_inode
);
276 static int init_inodecache(void)
278 sock_inode_cachep
= kmem_cache_create("sock_inode_cache",
279 sizeof(struct socket_alloc
),
281 (SLAB_HWCACHE_ALIGN
|
282 SLAB_RECLAIM_ACCOUNT
|
286 if (sock_inode_cachep
== NULL
)
291 static struct super_operations sockfs_ops
= {
292 .alloc_inode
= sock_alloc_inode
,
293 .destroy_inode
=sock_destroy_inode
,
294 .statfs
= simple_statfs
,
297 static int sockfs_get_sb(struct file_system_type
*fs_type
,
298 int flags
, const char *dev_name
, void *data
,
299 struct vfsmount
*mnt
)
301 return get_sb_pseudo(fs_type
, "socket:", &sockfs_ops
, SOCKFS_MAGIC
,
305 static struct vfsmount
*sock_mnt __read_mostly
;
307 static struct file_system_type sock_fs_type
= {
309 .get_sb
= sockfs_get_sb
,
310 .kill_sb
= kill_anon_super
,
313 static int sockfs_delete_dentry(struct dentry
*dentry
)
317 static struct dentry_operations sockfs_dentry_operations
= {
318 .d_delete
= sockfs_delete_dentry
,
322 * Obtains the first available file descriptor and sets it up for use.
324 * These functions create file structures and maps them to fd space
325 * of the current process. On success it returns file descriptor
326 * and file struct implicitly stored in sock->file.
327 * Note that another thread may close file descriptor before we return
328 * from this function. We use the fact that now we do not refer
329 * to socket after mapping. If one day we will need it, this
330 * function will increment ref. count on file by 1.
332 * In any case returned fd MAY BE not valid!
333 * This race condition is unavoidable
334 * with shared fd spaces, we cannot solve it inside kernel,
335 * but we take care of internal coherence yet.
338 static int sock_alloc_fd(struct file
**filep
)
342 fd
= get_unused_fd();
343 if (likely(fd
>= 0)) {
344 struct file
*file
= get_empty_filp();
347 if (unlikely(!file
)) {
356 static int sock_attach_fd(struct socket
*sock
, struct file
*file
)
361 this.len
= sprintf(name
, "[%lu]", SOCK_INODE(sock
)->i_ino
);
363 this.hash
= SOCK_INODE(sock
)->i_ino
;
365 file
->f_dentry
= d_alloc(sock_mnt
->mnt_sb
->s_root
, &this);
366 if (unlikely(!file
->f_dentry
))
369 file
->f_dentry
->d_op
= &sockfs_dentry_operations
;
370 d_add(file
->f_dentry
, SOCK_INODE(sock
));
371 file
->f_vfsmnt
= mntget(sock_mnt
);
372 file
->f_mapping
= file
->f_dentry
->d_inode
->i_mapping
;
375 file
->f_op
= SOCK_INODE(sock
)->i_fop
= &socket_file_ops
;
376 file
->f_mode
= FMODE_READ
| FMODE_WRITE
;
377 file
->f_flags
= O_RDWR
;
379 file
->private_data
= sock
;
384 int sock_map_fd(struct socket
*sock
)
386 struct file
*newfile
;
387 int fd
= sock_alloc_fd(&newfile
);
389 if (likely(fd
>= 0)) {
390 int err
= sock_attach_fd(sock
, newfile
);
392 if (unlikely(err
< 0)) {
397 fd_install(fd
, newfile
);
402 static struct socket
*sock_from_file(struct file
*file
, int *err
)
407 if (file
->f_op
== &socket_file_ops
)
408 return file
->private_data
; /* set in sock_map_fd */
410 inode
= file
->f_dentry
->d_inode
;
411 if (!S_ISSOCK(inode
->i_mode
)) {
416 sock
= SOCKET_I(inode
);
417 if (sock
->file
!= file
) {
418 printk(KERN_ERR
"socki_lookup: socket file changed!\n");
425 * sockfd_lookup - Go from a file number to its socket slot
427 * @err: pointer to an error code return
429 * The file handle passed in is locked and the socket it is bound
430 * too is returned. If an error occurs the err pointer is overwritten
431 * with a negative errno code and NULL is returned. The function checks
432 * for both invalid handles and passing a handle which is not a socket.
434 * On a success the socket object pointer is returned.
437 struct socket
*sockfd_lookup(int fd
, int *err
)
448 sock
= sock_from_file(file
, err
);
454 static struct socket
*sockfd_lookup_light(int fd
, int *err
, int *fput_needed
)
460 file
= fget_light(fd
, fput_needed
);
462 sock
= sock_from_file(file
, err
);
465 fput_light(file
, *fput_needed
);
471 * sock_alloc - allocate a socket
473 * Allocate a new inode and socket object. The two are bound together
474 * and initialised. The socket is then returned. If we are out of inodes
478 static struct socket
*sock_alloc(void)
483 inode
= new_inode(sock_mnt
->mnt_sb
);
487 sock
= SOCKET_I(inode
);
489 inode
->i_mode
= S_IFSOCK
| S_IRWXUGO
;
490 inode
->i_uid
= current
->fsuid
;
491 inode
->i_gid
= current
->fsgid
;
493 get_cpu_var(sockets_in_use
)++;
494 put_cpu_var(sockets_in_use
);
499 * In theory you can't get an open on this inode, but /proc provides
500 * a back door. Remember to keep it shut otherwise you'll let the
501 * creepy crawlies in.
504 static int sock_no_open(struct inode
*irrelevant
, struct file
*dontcare
)
509 const struct file_operations bad_sock_fops
= {
510 .owner
= THIS_MODULE
,
511 .open
= sock_no_open
,
515 * sock_release - close a socket
516 * @sock: socket to close
518 * The socket is released from the protocol stack if it has a release
519 * callback, and the inode is then released if the socket is bound to
520 * an inode not a file.
523 void sock_release(struct socket
*sock
)
526 struct module
*owner
= sock
->ops
->owner
;
528 sock
->ops
->release(sock
);
533 if (sock
->fasync_list
)
534 printk(KERN_ERR
"sock_release: fasync list not empty!\n");
536 get_cpu_var(sockets_in_use
)--;
537 put_cpu_var(sockets_in_use
);
539 iput(SOCK_INODE(sock
));
545 static inline int __sock_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
546 struct msghdr
*msg
, size_t size
)
548 struct sock_iocb
*si
= kiocb_to_siocb(iocb
);
556 err
= security_socket_sendmsg(sock
, msg
, size
);
560 return sock
->ops
->sendmsg(iocb
, sock
, msg
, size
);
563 int sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t size
)
566 struct sock_iocb siocb
;
569 init_sync_kiocb(&iocb
, NULL
);
570 iocb
.private = &siocb
;
571 ret
= __sock_sendmsg(&iocb
, sock
, msg
, size
);
572 if (-EIOCBQUEUED
== ret
)
573 ret
= wait_on_sync_kiocb(&iocb
);
577 int kernel_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
578 struct kvec
*vec
, size_t num
, size_t size
)
580 mm_segment_t oldfs
= get_fs();
585 * the following is safe, since for compiler definitions of kvec and
586 * iovec are identical, yielding the same in-core layout and alignment
588 msg
->msg_iov
= (struct iovec
*)vec
;
589 msg
->msg_iovlen
= num
;
590 result
= sock_sendmsg(sock
, msg
, size
);
595 static inline int __sock_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
596 struct msghdr
*msg
, size_t size
, int flags
)
599 struct sock_iocb
*si
= kiocb_to_siocb(iocb
);
607 err
= security_socket_recvmsg(sock
, msg
, size
, flags
);
611 return sock
->ops
->recvmsg(iocb
, sock
, msg
, size
, flags
);
614 int sock_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
615 size_t size
, int flags
)
618 struct sock_iocb siocb
;
621 init_sync_kiocb(&iocb
, NULL
);
622 iocb
.private = &siocb
;
623 ret
= __sock_recvmsg(&iocb
, sock
, msg
, size
, flags
);
624 if (-EIOCBQUEUED
== ret
)
625 ret
= wait_on_sync_kiocb(&iocb
);
629 int kernel_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
630 struct kvec
*vec
, size_t num
, size_t size
, int flags
)
632 mm_segment_t oldfs
= get_fs();
637 * the following is safe, since for compiler definitions of kvec and
638 * iovec are identical, yielding the same in-core layout and alignment
640 msg
->msg_iov
= (struct iovec
*)vec
, msg
->msg_iovlen
= num
;
641 result
= sock_recvmsg(sock
, msg
, size
, flags
);
646 static void sock_aio_dtor(struct kiocb
*iocb
)
648 kfree(iocb
->private);
651 static ssize_t
sock_sendpage(struct file
*file
, struct page
*page
,
652 int offset
, size_t size
, loff_t
*ppos
, int more
)
657 sock
= file
->private_data
;
659 flags
= !(file
->f_flags
& O_NONBLOCK
) ? 0 : MSG_DONTWAIT
;
663 return sock
->ops
->sendpage(sock
, page
, offset
, size
, flags
);
666 static struct sock_iocb
*alloc_sock_iocb(struct kiocb
*iocb
,
667 char __user
*ubuf
, size_t size
,
668 struct sock_iocb
*siocb
)
670 if (!is_sync_kiocb(iocb
)) {
671 siocb
= kmalloc(sizeof(*siocb
), GFP_KERNEL
);
674 iocb
->ki_dtor
= sock_aio_dtor
;
678 siocb
->async_iov
.iov_base
= ubuf
;
679 siocb
->async_iov
.iov_len
= size
;
681 iocb
->private = siocb
;
685 static ssize_t
do_sock_read(struct msghdr
*msg
, struct kiocb
*iocb
,
686 struct file
*file
, struct iovec
*iov
,
687 unsigned long nr_segs
)
689 struct socket
*sock
= file
->private_data
;
693 for (i
= 0; i
< nr_segs
; i
++)
694 size
+= iov
[i
].iov_len
;
696 msg
->msg_name
= NULL
;
697 msg
->msg_namelen
= 0;
698 msg
->msg_control
= NULL
;
699 msg
->msg_controllen
= 0;
700 msg
->msg_iov
= (struct iovec
*)iov
;
701 msg
->msg_iovlen
= nr_segs
;
702 msg
->msg_flags
= (file
->f_flags
& O_NONBLOCK
) ? MSG_DONTWAIT
: 0;
704 return __sock_recvmsg(iocb
, sock
, msg
, size
, msg
->msg_flags
);
707 static ssize_t
sock_readv(struct file
*file
, const struct iovec
*iov
,
708 unsigned long nr_segs
, loff_t
*ppos
)
711 struct sock_iocb siocb
;
715 init_sync_kiocb(&iocb
, NULL
);
716 iocb
.private = &siocb
;
718 ret
= do_sock_read(&msg
, &iocb
, file
, (struct iovec
*)iov
, nr_segs
);
719 if (-EIOCBQUEUED
== ret
)
720 ret
= wait_on_sync_kiocb(&iocb
);
724 static ssize_t
sock_aio_read(struct kiocb
*iocb
, char __user
*ubuf
,
725 size_t count
, loff_t pos
)
727 struct sock_iocb siocb
, *x
;
731 if (count
== 0) /* Match SYS5 behaviour */
734 x
= alloc_sock_iocb(iocb
, ubuf
, count
, &siocb
);
737 return do_sock_read(&x
->async_msg
, iocb
, iocb
->ki_filp
,
741 static ssize_t
do_sock_write(struct msghdr
*msg
, struct kiocb
*iocb
,
742 struct file
*file
, struct iovec
*iov
,
743 unsigned long nr_segs
)
745 struct socket
*sock
= file
->private_data
;
749 for (i
= 0; i
< nr_segs
; i
++)
750 size
+= iov
[i
].iov_len
;
752 msg
->msg_name
= NULL
;
753 msg
->msg_namelen
= 0;
754 msg
->msg_control
= NULL
;
755 msg
->msg_controllen
= 0;
756 msg
->msg_iov
= (struct iovec
*)iov
;
757 msg
->msg_iovlen
= nr_segs
;
758 msg
->msg_flags
= (file
->f_flags
& O_NONBLOCK
) ? MSG_DONTWAIT
: 0;
759 if (sock
->type
== SOCK_SEQPACKET
)
760 msg
->msg_flags
|= MSG_EOR
;
762 return __sock_sendmsg(iocb
, sock
, msg
, size
);
765 static ssize_t
sock_writev(struct file
*file
, const struct iovec
*iov
,
766 unsigned long nr_segs
, loff_t
*ppos
)
770 struct sock_iocb siocb
;
773 init_sync_kiocb(&iocb
, NULL
);
774 iocb
.private = &siocb
;
776 ret
= do_sock_write(&msg
, &iocb
, file
, (struct iovec
*)iov
, nr_segs
);
777 if (-EIOCBQUEUED
== ret
)
778 ret
= wait_on_sync_kiocb(&iocb
);
782 static ssize_t
sock_aio_write(struct kiocb
*iocb
, const char __user
*ubuf
,
783 size_t count
, loff_t pos
)
785 struct sock_iocb siocb
, *x
;
789 if (count
== 0) /* Match SYS5 behaviour */
792 x
= alloc_sock_iocb(iocb
, (void __user
*)ubuf
, count
, &siocb
);
796 return do_sock_write(&x
->async_msg
, iocb
, iocb
->ki_filp
,
801 * Atomic setting of ioctl hooks to avoid race
802 * with module unload.
805 static DEFINE_MUTEX(br_ioctl_mutex
);
806 static int (*br_ioctl_hook
) (unsigned int cmd
, void __user
*arg
) = NULL
;
808 void brioctl_set(int (*hook
) (unsigned int, void __user
*))
810 mutex_lock(&br_ioctl_mutex
);
811 br_ioctl_hook
= hook
;
812 mutex_unlock(&br_ioctl_mutex
);
815 EXPORT_SYMBOL(brioctl_set
);
817 static DEFINE_MUTEX(vlan_ioctl_mutex
);
818 static int (*vlan_ioctl_hook
) (void __user
*arg
);
820 void vlan_ioctl_set(int (*hook
) (void __user
*))
822 mutex_lock(&vlan_ioctl_mutex
);
823 vlan_ioctl_hook
= hook
;
824 mutex_unlock(&vlan_ioctl_mutex
);
827 EXPORT_SYMBOL(vlan_ioctl_set
);
829 static DEFINE_MUTEX(dlci_ioctl_mutex
);
830 static int (*dlci_ioctl_hook
) (unsigned int, void __user
*);
832 void dlci_ioctl_set(int (*hook
) (unsigned int, void __user
*))
834 mutex_lock(&dlci_ioctl_mutex
);
835 dlci_ioctl_hook
= hook
;
836 mutex_unlock(&dlci_ioctl_mutex
);
839 EXPORT_SYMBOL(dlci_ioctl_set
);
842 * With an ioctl, arg may well be a user mode pointer, but we don't know
843 * what to do with it - that's up to the protocol still.
846 static long sock_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
849 void __user
*argp
= (void __user
*)arg
;
852 sock
= file
->private_data
;
853 if (cmd
>= SIOCDEVPRIVATE
&& cmd
<= (SIOCDEVPRIVATE
+ 15)) {
854 err
= dev_ioctl(cmd
, argp
);
856 #ifdef CONFIG_WIRELESS_EXT
857 if (cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
) {
858 err
= dev_ioctl(cmd
, argp
);
860 #endif /* CONFIG_WIRELESS_EXT */
865 if (get_user(pid
, (int __user
*)argp
))
867 err
= f_setown(sock
->file
, pid
, 1);
871 err
= put_user(sock
->file
->f_owner
.pid
,
880 request_module("bridge");
882 mutex_lock(&br_ioctl_mutex
);
884 err
= br_ioctl_hook(cmd
, argp
);
885 mutex_unlock(&br_ioctl_mutex
);
890 if (!vlan_ioctl_hook
)
891 request_module("8021q");
893 mutex_lock(&vlan_ioctl_mutex
);
895 err
= vlan_ioctl_hook(argp
);
896 mutex_unlock(&vlan_ioctl_mutex
);
900 /* Convert this to call through a hook */
901 err
= divert_ioctl(cmd
, argp
);
906 if (!dlci_ioctl_hook
)
907 request_module("dlci");
909 if (dlci_ioctl_hook
) {
910 mutex_lock(&dlci_ioctl_mutex
);
911 err
= dlci_ioctl_hook(cmd
, argp
);
912 mutex_unlock(&dlci_ioctl_mutex
);
916 err
= sock
->ops
->ioctl(sock
, cmd
, arg
);
919 * If this ioctl is unknown try to hand it down
922 if (err
== -ENOIOCTLCMD
)
923 err
= dev_ioctl(cmd
, argp
);
929 int sock_create_lite(int family
, int type
, int protocol
, struct socket
**res
)
932 struct socket
*sock
= NULL
;
934 err
= security_socket_create(family
, type
, protocol
, 1);
945 err
= security_socket_post_create(sock
, family
, type
, protocol
, 1);
958 /* No kernel lock held - perfect */
959 static unsigned int sock_poll(struct file
*file
, poll_table
*wait
)
964 * We can't return errors to poll, so it's either yes or no.
966 sock
= file
->private_data
;
967 return sock
->ops
->poll(file
, sock
, wait
);
970 static int sock_mmap(struct file
*file
, struct vm_area_struct
*vma
)
972 struct socket
*sock
= file
->private_data
;
974 return sock
->ops
->mmap(file
, sock
, vma
);
977 static int sock_close(struct inode
*inode
, struct file
*filp
)
980 * It was possible the inode is NULL we were
981 * closing an unfinished socket.
985 printk(KERN_DEBUG
"sock_close: NULL inode\n");
988 sock_fasync(-1, filp
, 0);
989 sock_release(SOCKET_I(inode
));
994 * Update the socket async list
996 * Fasync_list locking strategy.
998 * 1. fasync_list is modified only under process context socket lock
999 * i.e. under semaphore.
1000 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1001 * or under socket lock.
1002 * 3. fasync_list can be used from softirq context, so that
1003 * modification under socket lock have to be enhanced with
1004 * write_lock_bh(&sk->sk_callback_lock).
1008 static int sock_fasync(int fd
, struct file
*filp
, int on
)
1010 struct fasync_struct
*fa
, *fna
= NULL
, **prev
;
1011 struct socket
*sock
;
1015 fna
= kmalloc(sizeof(struct fasync_struct
), GFP_KERNEL
);
1020 sock
= filp
->private_data
;
1030 prev
= &(sock
->fasync_list
);
1032 for (fa
= *prev
; fa
!= NULL
; prev
= &fa
->fa_next
, fa
= *prev
)
1033 if (fa
->fa_file
== filp
)
1038 write_lock_bh(&sk
->sk_callback_lock
);
1040 write_unlock_bh(&sk
->sk_callback_lock
);
1045 fna
->fa_file
= filp
;
1047 fna
->magic
= FASYNC_MAGIC
;
1048 fna
->fa_next
= sock
->fasync_list
;
1049 write_lock_bh(&sk
->sk_callback_lock
);
1050 sock
->fasync_list
= fna
;
1051 write_unlock_bh(&sk
->sk_callback_lock
);
1054 write_lock_bh(&sk
->sk_callback_lock
);
1055 *prev
= fa
->fa_next
;
1056 write_unlock_bh(&sk
->sk_callback_lock
);
1062 release_sock(sock
->sk
);
1066 /* This function may be called only under socket lock or callback_lock */
1068 int sock_wake_async(struct socket
*sock
, int how
, int band
)
1070 if (!sock
|| !sock
->fasync_list
)
1075 if (test_bit(SOCK_ASYNC_WAITDATA
, &sock
->flags
))
1079 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE
, &sock
->flags
))
1084 __kill_fasync(sock
->fasync_list
, SIGIO
, band
);
1087 __kill_fasync(sock
->fasync_list
, SIGURG
, band
);
1092 static int __sock_create(int family
, int type
, int protocol
,
1093 struct socket
**res
, int kern
)
1096 struct socket
*sock
;
1097 const struct net_proto_family
*pf
;
1100 * Check protocol is in range
1102 if (family
< 0 || family
>= NPROTO
)
1103 return -EAFNOSUPPORT
;
1104 if (type
< 0 || type
>= SOCK_MAX
)
1109 This uglymoron is moved from INET layer to here to avoid
1110 deadlock in module load.
1112 if (family
== PF_INET
&& type
== SOCK_PACKET
) {
1116 printk(KERN_INFO
"%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1122 err
= security_socket_create(family
, type
, protocol
, kern
);
1127 * Allocate the socket and allow the family to set things up. if
1128 * the protocol is 0, the family is instructed to select an appropriate
1131 sock
= sock_alloc();
1133 if (net_ratelimit())
1134 printk(KERN_WARNING
"socket: no more sockets\n");
1135 return -ENFILE
; /* Not exactly a match, but its the
1136 closest posix thing */
1141 #if defined(CONFIG_KMOD)
1142 /* Attempt to load a protocol module if the find failed.
1144 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1145 * requested real, full-featured networking support upon configuration.
1146 * Otherwise module support will break!
1148 if (net_families
[family
] == NULL
)
1149 request_module("net-pf-%d", family
);
1153 pf
= rcu_dereference(net_families
[family
]);
1154 err
= -EAFNOSUPPORT
;
1159 * We will call the ->create function, that possibly is in a loadable
1160 * module, so we have to bump that loadable module refcnt first.
1162 if (!try_module_get(pf
->owner
))
1165 /* Now protected by module ref count */
1168 err
= pf
->create(sock
, protocol
);
1170 goto out_module_put
;
1173 * Now to bump the refcnt of the [loadable] module that owns this
1174 * socket at sock_release time we decrement its refcnt.
1176 if (!try_module_get(sock
->ops
->owner
))
1177 goto out_module_busy
;
1180 * Now that we're done with the ->create function, the [loadable]
1181 * module can have its refcnt decremented
1183 module_put(pf
->owner
);
1184 err
= security_socket_post_create(sock
, family
, type
, protocol
, kern
);
1192 err
= -EAFNOSUPPORT
;
1195 module_put(pf
->owner
);
1202 goto out_sock_release
;
1205 int sock_create(int family
, int type
, int protocol
, struct socket
**res
)
1207 return __sock_create(family
, type
, protocol
, res
, 0);
1210 int sock_create_kern(int family
, int type
, int protocol
, struct socket
**res
)
1212 return __sock_create(family
, type
, protocol
, res
, 1);
1215 asmlinkage
long sys_socket(int family
, int type
, int protocol
)
1218 struct socket
*sock
;
1220 retval
= sock_create(family
, type
, protocol
, &sock
);
1224 retval
= sock_map_fd(sock
);
1229 /* It may be already another descriptor 8) Not kernel problem. */
1238 * Create a pair of connected sockets.
1241 asmlinkage
long sys_socketpair(int family
, int type
, int protocol
,
1242 int __user
*usockvec
)
1244 struct socket
*sock1
, *sock2
;
1248 * Obtain the first socket and check if the underlying protocol
1249 * supports the socketpair call.
1252 err
= sock_create(family
, type
, protocol
, &sock1
);
1256 err
= sock_create(family
, type
, protocol
, &sock2
);
1260 err
= sock1
->ops
->socketpair(sock1
, sock2
);
1262 goto out_release_both
;
1266 err
= sock_map_fd(sock1
);
1268 goto out_release_both
;
1271 err
= sock_map_fd(sock2
);
1276 /* fd1 and fd2 may be already another descriptors.
1277 * Not kernel problem.
1280 err
= put_user(fd1
, &usockvec
[0]);
1282 err
= put_user(fd2
, &usockvec
[1]);
1291 sock_release(sock2
);
1296 sock_release(sock2
);
1298 sock_release(sock1
);
1304 * Bind a name to a socket. Nothing much to do here since it's
1305 * the protocol's responsibility to handle the local address.
1307 * We move the socket address to kernel space before we call
1308 * the protocol layer (having also checked the address is ok).
1311 asmlinkage
long sys_bind(int fd
, struct sockaddr __user
*umyaddr
, int addrlen
)
1313 struct socket
*sock
;
1314 char address
[MAX_SOCK_ADDR
];
1315 int err
, fput_needed
;
1317 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1319 err
= move_addr_to_kernel(umyaddr
, addrlen
, address
);
1321 err
= security_socket_bind(sock
,
1322 (struct sockaddr
*)address
,
1325 err
= sock
->ops
->bind(sock
,
1329 fput_light(sock
->file
, fput_needed
);
1335 * Perform a listen. Basically, we allow the protocol to do anything
1336 * necessary for a listen, and if that works, we mark the socket as
1337 * ready for listening.
1340 int sysctl_somaxconn __read_mostly
= SOMAXCONN
;
1342 asmlinkage
long sys_listen(int fd
, int backlog
)
1344 struct socket
*sock
;
1345 int err
, fput_needed
;
1347 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1349 if ((unsigned)backlog
> sysctl_somaxconn
)
1350 backlog
= sysctl_somaxconn
;
1352 err
= security_socket_listen(sock
, backlog
);
1354 err
= sock
->ops
->listen(sock
, backlog
);
1356 fput_light(sock
->file
, fput_needed
);
1362 * For accept, we attempt to create a new socket, set up the link
1363 * with the client, wake up the client, then return the new
1364 * connected fd. We collect the address of the connector in kernel
1365 * space and move it to user at the very end. This is unclean because
1366 * we open the socket then return an error.
1368 * 1003.1g adds the ability to recvmsg() to query connection pending
1369 * status to recvmsg. We need to add that support in a way thats
1370 * clean when we restucture accept also.
1373 asmlinkage
long sys_accept(int fd
, struct sockaddr __user
*upeer_sockaddr
,
1374 int __user
*upeer_addrlen
)
1376 struct socket
*sock
, *newsock
;
1377 struct file
*newfile
;
1378 int err
, len
, newfd
, fput_needed
;
1379 char address
[MAX_SOCK_ADDR
];
1381 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1386 if (!(newsock
= sock_alloc()))
1389 newsock
->type
= sock
->type
;
1390 newsock
->ops
= sock
->ops
;
1393 * We don't need try_module_get here, as the listening socket (sock)
1394 * has the protocol module (sock->ops->owner) held.
1396 __module_get(newsock
->ops
->owner
);
1398 newfd
= sock_alloc_fd(&newfile
);
1399 if (unlikely(newfd
< 0)) {
1401 sock_release(newsock
);
1405 err
= sock_attach_fd(newsock
, newfile
);
1409 err
= security_socket_accept(sock
, newsock
);
1413 err
= sock
->ops
->accept(sock
, newsock
, sock
->file
->f_flags
);
1417 if (upeer_sockaddr
) {
1418 if (newsock
->ops
->getname(newsock
, (struct sockaddr
*)address
,
1420 err
= -ECONNABORTED
;
1423 err
= move_addr_to_user(address
, len
, upeer_sockaddr
,
1429 /* File flags are not inherited via accept() unlike another OSes. */
1431 fd_install(newfd
, newfile
);
1434 security_socket_post_accept(sock
, newsock
);
1437 fput_light(sock
->file
, fput_needed
);
1442 put_unused_fd(newfd
);
1447 * Attempt to connect to a socket with the server address. The address
1448 * is in user space so we verify it is OK and move it to kernel space.
1450 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1453 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1454 * other SEQPACKET protocols that take time to connect() as it doesn't
1455 * include the -EINPROGRESS status for such sockets.
1458 asmlinkage
long sys_connect(int fd
, struct sockaddr __user
*uservaddr
,
1461 struct socket
*sock
;
1462 char address
[MAX_SOCK_ADDR
];
1463 int err
, fput_needed
;
1465 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1468 err
= move_addr_to_kernel(uservaddr
, addrlen
, address
);
1473 security_socket_connect(sock
, (struct sockaddr
*)address
, addrlen
);
1477 err
= sock
->ops
->connect(sock
, (struct sockaddr
*)address
, addrlen
,
1478 sock
->file
->f_flags
);
1480 fput_light(sock
->file
, fput_needed
);
1486 * Get the local address ('name') of a socket object. Move the obtained
1487 * name to user space.
1490 asmlinkage
long sys_getsockname(int fd
, struct sockaddr __user
*usockaddr
,
1491 int __user
*usockaddr_len
)
1493 struct socket
*sock
;
1494 char address
[MAX_SOCK_ADDR
];
1495 int len
, err
, fput_needed
;
1497 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1501 err
= security_socket_getsockname(sock
);
1505 err
= sock
->ops
->getname(sock
, (struct sockaddr
*)address
, &len
, 0);
1508 err
= move_addr_to_user(address
, len
, usockaddr
, usockaddr_len
);
1511 fput_light(sock
->file
, fput_needed
);
1517 * Get the remote address ('name') of a socket object. Move the obtained
1518 * name to user space.
1521 asmlinkage
long sys_getpeername(int fd
, struct sockaddr __user
*usockaddr
,
1522 int __user
*usockaddr_len
)
1524 struct socket
*sock
;
1525 char address
[MAX_SOCK_ADDR
];
1526 int len
, err
, fput_needed
;
1528 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1530 err
= security_socket_getpeername(sock
);
1532 fput_light(sock
->file
, fput_needed
);
1537 sock
->ops
->getname(sock
, (struct sockaddr
*)address
, &len
,
1540 err
= move_addr_to_user(address
, len
, usockaddr
,
1542 fput_light(sock
->file
, fput_needed
);
1548 * Send a datagram to a given address. We move the address into kernel
1549 * space and check the user space data area is readable before invoking
1553 asmlinkage
long sys_sendto(int fd
, void __user
*buff
, size_t len
,
1554 unsigned flags
, struct sockaddr __user
*addr
,
1557 struct socket
*sock
;
1558 char address
[MAX_SOCK_ADDR
];
1563 struct file
*sock_file
;
1565 sock_file
= fget_light(fd
, &fput_needed
);
1569 sock
= sock_from_file(sock_file
, &err
);
1572 iov
.iov_base
= buff
;
1574 msg
.msg_name
= NULL
;
1577 msg
.msg_control
= NULL
;
1578 msg
.msg_controllen
= 0;
1579 msg
.msg_namelen
= 0;
1581 err
= move_addr_to_kernel(addr
, addr_len
, address
);
1584 msg
.msg_name
= address
;
1585 msg
.msg_namelen
= addr_len
;
1587 if (sock
->file
->f_flags
& O_NONBLOCK
)
1588 flags
|= MSG_DONTWAIT
;
1589 msg
.msg_flags
= flags
;
1590 err
= sock_sendmsg(sock
, &msg
, len
);
1593 fput_light(sock_file
, fput_needed
);
1598 * Send a datagram down a socket.
1601 asmlinkage
long sys_send(int fd
, void __user
*buff
, size_t len
, unsigned flags
)
1603 return sys_sendto(fd
, buff
, len
, flags
, NULL
, 0);
1607 * Receive a frame from the socket and optionally record the address of the
1608 * sender. We verify the buffers are writable and if needed move the
1609 * sender address from kernel to user space.
1612 asmlinkage
long sys_recvfrom(int fd
, void __user
*ubuf
, size_t size
,
1613 unsigned flags
, struct sockaddr __user
*addr
,
1614 int __user
*addr_len
)
1616 struct socket
*sock
;
1619 char address
[MAX_SOCK_ADDR
];
1621 struct file
*sock_file
;
1624 sock_file
= fget_light(fd
, &fput_needed
);
1628 sock
= sock_from_file(sock_file
, &err
);
1632 msg
.msg_control
= NULL
;
1633 msg
.msg_controllen
= 0;
1637 iov
.iov_base
= ubuf
;
1638 msg
.msg_name
= address
;
1639 msg
.msg_namelen
= MAX_SOCK_ADDR
;
1640 if (sock
->file
->f_flags
& O_NONBLOCK
)
1641 flags
|= MSG_DONTWAIT
;
1642 err
= sock_recvmsg(sock
, &msg
, size
, flags
);
1644 if (err
>= 0 && addr
!= NULL
) {
1645 err2
= move_addr_to_user(address
, msg
.msg_namelen
, addr
, addr_len
);
1650 fput_light(sock_file
, fput_needed
);
1655 * Receive a datagram from a socket.
1658 asmlinkage
long sys_recv(int fd
, void __user
*ubuf
, size_t size
,
1661 return sys_recvfrom(fd
, ubuf
, size
, flags
, NULL
, NULL
);
1665 * Set a socket option. Because we don't know the option lengths we have
1666 * to pass the user mode parameter for the protocols to sort out.
1669 asmlinkage
long sys_setsockopt(int fd
, int level
, int optname
,
1670 char __user
*optval
, int optlen
)
1672 int err
, fput_needed
;
1673 struct socket
*sock
;
1678 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1680 err
= security_socket_setsockopt(sock
, level
, optname
);
1684 if (level
== SOL_SOCKET
)
1686 sock_setsockopt(sock
, level
, optname
, optval
,
1690 sock
->ops
->setsockopt(sock
, level
, optname
, optval
,
1693 fput_light(sock
->file
, fput_needed
);
1699 * Get a socket option. Because we don't know the option lengths we have
1700 * to pass a user mode parameter for the protocols to sort out.
1703 asmlinkage
long sys_getsockopt(int fd
, int level
, int optname
,
1704 char __user
*optval
, int __user
*optlen
)
1706 int err
, fput_needed
;
1707 struct socket
*sock
;
1709 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1711 err
= security_socket_getsockopt(sock
, level
, optname
);
1715 if (level
== SOL_SOCKET
)
1717 sock_getsockopt(sock
, level
, optname
, optval
,
1721 sock
->ops
->getsockopt(sock
, level
, optname
, optval
,
1724 fput_light(sock
->file
, fput_needed
);
1730 * Shutdown a socket.
1733 asmlinkage
long sys_shutdown(int fd
, int how
)
1735 int err
, fput_needed
;
1736 struct socket
*sock
;
1738 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1740 err
= security_socket_shutdown(sock
, how
);
1742 err
= sock
->ops
->shutdown(sock
, how
);
1743 fput_light(sock
->file
, fput_needed
);
1748 /* A couple of helpful macros for getting the address of the 32/64 bit
1749 * fields which are the same type (int / unsigned) on our platforms.
1751 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1752 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1753 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1756 * BSD sendmsg interface
1759 asmlinkage
long sys_sendmsg(int fd
, struct msghdr __user
*msg
, unsigned flags
)
1761 struct compat_msghdr __user
*msg_compat
=
1762 (struct compat_msghdr __user
*)msg
;
1763 struct socket
*sock
;
1764 char address
[MAX_SOCK_ADDR
];
1765 struct iovec iovstack
[UIO_FASTIOV
], *iov
= iovstack
;
1766 unsigned char ctl
[sizeof(struct cmsghdr
) + 20]
1767 __attribute__ ((aligned(sizeof(__kernel_size_t
))));
1768 /* 20 is size of ipv6_pktinfo */
1769 unsigned char *ctl_buf
= ctl
;
1770 struct msghdr msg_sys
;
1771 int err
, ctl_len
, iov_size
, total_len
;
1775 if (MSG_CMSG_COMPAT
& flags
) {
1776 if (get_compat_msghdr(&msg_sys
, msg_compat
))
1779 else if (copy_from_user(&msg_sys
, msg
, sizeof(struct msghdr
)))
1782 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1786 /* do not move before msg_sys is valid */
1788 if (msg_sys
.msg_iovlen
> UIO_MAXIOV
)
1791 /* Check whether to allocate the iovec area */
1793 iov_size
= msg_sys
.msg_iovlen
* sizeof(struct iovec
);
1794 if (msg_sys
.msg_iovlen
> UIO_FASTIOV
) {
1795 iov
= sock_kmalloc(sock
->sk
, iov_size
, GFP_KERNEL
);
1800 /* This will also move the address data into kernel space */
1801 if (MSG_CMSG_COMPAT
& flags
) {
1802 err
= verify_compat_iovec(&msg_sys
, iov
, address
, VERIFY_READ
);
1804 err
= verify_iovec(&msg_sys
, iov
, address
, VERIFY_READ
);
1811 if (msg_sys
.msg_controllen
> INT_MAX
)
1813 ctl_len
= msg_sys
.msg_controllen
;
1814 if ((MSG_CMSG_COMPAT
& flags
) && ctl_len
) {
1816 cmsghdr_from_user_compat_to_kern(&msg_sys
, sock
->sk
, ctl
,
1820 ctl_buf
= msg_sys
.msg_control
;
1821 ctl_len
= msg_sys
.msg_controllen
;
1822 } else if (ctl_len
) {
1823 if (ctl_len
> sizeof(ctl
)) {
1824 ctl_buf
= sock_kmalloc(sock
->sk
, ctl_len
, GFP_KERNEL
);
1825 if (ctl_buf
== NULL
)
1830 * Careful! Before this, msg_sys.msg_control contains a user pointer.
1831 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1832 * checking falls down on this.
1834 if (copy_from_user(ctl_buf
, (void __user
*)msg_sys
.msg_control
,
1837 msg_sys
.msg_control
= ctl_buf
;
1839 msg_sys
.msg_flags
= flags
;
1841 if (sock
->file
->f_flags
& O_NONBLOCK
)
1842 msg_sys
.msg_flags
|= MSG_DONTWAIT
;
1843 err
= sock_sendmsg(sock
, &msg_sys
, total_len
);
1847 sock_kfree_s(sock
->sk
, ctl_buf
, ctl_len
);
1849 if (iov
!= iovstack
)
1850 sock_kfree_s(sock
->sk
, iov
, iov_size
);
1852 fput_light(sock
->file
, fput_needed
);
1858 * BSD recvmsg interface
1861 asmlinkage
long sys_recvmsg(int fd
, struct msghdr __user
*msg
,
1864 struct compat_msghdr __user
*msg_compat
=
1865 (struct compat_msghdr __user
*)msg
;
1866 struct socket
*sock
;
1867 struct iovec iovstack
[UIO_FASTIOV
];
1868 struct iovec
*iov
= iovstack
;
1869 struct msghdr msg_sys
;
1870 unsigned long cmsg_ptr
;
1871 int err
, iov_size
, total_len
, len
;
1874 /* kernel mode address */
1875 char addr
[MAX_SOCK_ADDR
];
1877 /* user mode address pointers */
1878 struct sockaddr __user
*uaddr
;
1879 int __user
*uaddr_len
;
1881 if (MSG_CMSG_COMPAT
& flags
) {
1882 if (get_compat_msghdr(&msg_sys
, msg_compat
))
1885 else if (copy_from_user(&msg_sys
, msg
, sizeof(struct msghdr
)))
1888 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1893 if (msg_sys
.msg_iovlen
> UIO_MAXIOV
)
1896 /* Check whether to allocate the iovec area */
1898 iov_size
= msg_sys
.msg_iovlen
* sizeof(struct iovec
);
1899 if (msg_sys
.msg_iovlen
> UIO_FASTIOV
) {
1900 iov
= sock_kmalloc(sock
->sk
, iov_size
, GFP_KERNEL
);
1906 * Save the user-mode address (verify_iovec will change the
1907 * kernel msghdr to use the kernel address space)
1910 uaddr
= (void __user
*)msg_sys
.msg_name
;
1911 uaddr_len
= COMPAT_NAMELEN(msg
);
1912 if (MSG_CMSG_COMPAT
& flags
) {
1913 err
= verify_compat_iovec(&msg_sys
, iov
, addr
, VERIFY_WRITE
);
1915 err
= verify_iovec(&msg_sys
, iov
, addr
, VERIFY_WRITE
);
1920 cmsg_ptr
= (unsigned long)msg_sys
.msg_control
;
1921 msg_sys
.msg_flags
= 0;
1922 if (MSG_CMSG_COMPAT
& flags
)
1923 msg_sys
.msg_flags
= MSG_CMSG_COMPAT
;
1925 if (sock
->file
->f_flags
& O_NONBLOCK
)
1926 flags
|= MSG_DONTWAIT
;
1927 err
= sock_recvmsg(sock
, &msg_sys
, total_len
, flags
);
1932 if (uaddr
!= NULL
) {
1933 err
= move_addr_to_user(addr
, msg_sys
.msg_namelen
, uaddr
,
1938 err
= __put_user((msg_sys
.msg_flags
& ~MSG_CMSG_COMPAT
),
1942 if (MSG_CMSG_COMPAT
& flags
)
1943 err
= __put_user((unsigned long)msg_sys
.msg_control
- cmsg_ptr
,
1944 &msg_compat
->msg_controllen
);
1946 err
= __put_user((unsigned long)msg_sys
.msg_control
- cmsg_ptr
,
1947 &msg
->msg_controllen
);
1953 if (iov
!= iovstack
)
1954 sock_kfree_s(sock
->sk
, iov
, iov_size
);
1956 fput_light(sock
->file
, fput_needed
);
1961 #ifdef __ARCH_WANT_SYS_SOCKETCALL
1963 /* Argument list sizes for sys_socketcall */
1964 #define AL(x) ((x) * sizeof(unsigned long))
1965 static const unsigned char nargs
[18]={
1966 AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
1967 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
1968 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)
1974 * System call vectors.
1976 * Argument checking cleaned up. Saved 20% in size.
1977 * This function doesn't need to set the kernel lock because
1978 * it is set by the callees.
1981 asmlinkage
long sys_socketcall(int call
, unsigned long __user
*args
)
1984 unsigned long a0
, a1
;
1987 if (call
< 1 || call
> SYS_RECVMSG
)
1990 /* copy_from_user should be SMP safe. */
1991 if (copy_from_user(a
, args
, nargs
[call
]))
1994 err
= audit_socketcall(nargs
[call
] / sizeof(unsigned long), a
);
2003 err
= sys_socket(a0
, a1
, a
[2]);
2006 err
= sys_bind(a0
, (struct sockaddr __user
*)a1
, a
[2]);
2009 err
= sys_connect(a0
, (struct sockaddr __user
*)a1
, a
[2]);
2012 err
= sys_listen(a0
, a1
);
2016 sys_accept(a0
, (struct sockaddr __user
*)a1
,
2017 (int __user
*)a
[2]);
2019 case SYS_GETSOCKNAME
:
2021 sys_getsockname(a0
, (struct sockaddr __user
*)a1
,
2022 (int __user
*)a
[2]);
2024 case SYS_GETPEERNAME
:
2026 sys_getpeername(a0
, (struct sockaddr __user
*)a1
,
2027 (int __user
*)a
[2]);
2029 case SYS_SOCKETPAIR
:
2030 err
= sys_socketpair(a0
, a1
, a
[2], (int __user
*)a
[3]);
2033 err
= sys_send(a0
, (void __user
*)a1
, a
[2], a
[3]);
2036 err
= sys_sendto(a0
, (void __user
*)a1
, a
[2], a
[3],
2037 (struct sockaddr __user
*)a
[4], a
[5]);
2040 err
= sys_recv(a0
, (void __user
*)a1
, a
[2], a
[3]);
2043 err
= sys_recvfrom(a0
, (void __user
*)a1
, a
[2], a
[3],
2044 (struct sockaddr __user
*)a
[4],
2045 (int __user
*)a
[5]);
2048 err
= sys_shutdown(a0
, a1
);
2050 case SYS_SETSOCKOPT
:
2051 err
= sys_setsockopt(a0
, a1
, a
[2], (char __user
*)a
[3], a
[4]);
2053 case SYS_GETSOCKOPT
:
2055 sys_getsockopt(a0
, a1
, a
[2], (char __user
*)a
[3],
2056 (int __user
*)a
[4]);
2059 err
= sys_sendmsg(a0
, (struct msghdr __user
*)a1
, a
[2]);
2062 err
= sys_recvmsg(a0
, (struct msghdr __user
*)a1
, a
[2]);
2071 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
2074 * sock_register - add a socket protocol handler
2075 * @ops: description of protocol
2077 * This function is called by a protocol handler that wants to
2078 * advertise its address family, and have it linked into the
2079 * socket interface. The value ops->family coresponds to the
2080 * socket system call protocol family.
2082 int sock_register(const struct net_proto_family
*ops
)
2086 if (ops
->family
>= NPROTO
) {
2087 printk(KERN_CRIT
"protocol %d >= NPROTO(%d)\n", ops
->family
,
2092 spin_lock(&net_family_lock
);
2093 if (net_families
[ops
->family
])
2096 net_families
[ops
->family
] = ops
;
2099 spin_unlock(&net_family_lock
);
2101 printk(KERN_INFO
"NET: Registered protocol family %d\n", ops
->family
);
2106 * sock_unregister - remove a protocol handler
2107 * @family: protocol family to remove
2109 * This function is called by a protocol handler that wants to
2110 * remove its address family, and have it unlinked from the
2111 * new socket creation.
2113 * If protocol handler is a module, then it can use module reference
2114 * counts to protect against new references. If protocol handler is not
2115 * a module then it needs to provide its own protection in
2116 * the ops->create routine.
2118 void sock_unregister(int family
)
2120 BUG_ON(family
< 0 || family
>= NPROTO
);
2122 spin_lock(&net_family_lock
);
2123 net_families
[family
] = NULL
;
2124 spin_unlock(&net_family_lock
);
2128 printk(KERN_INFO
"NET: Unregistered protocol family %d\n", family
);
2131 static int __init
sock_init(void)
2134 * Initialize sock SLAB cache.
2140 * Initialize skbuff SLAB cache
2145 * Initialize the protocols module.
2149 register_filesystem(&sock_fs_type
);
2150 sock_mnt
= kern_mount(&sock_fs_type
);
2152 /* The real protocol initialization is performed in later initcalls.
2155 #ifdef CONFIG_NETFILTER
2162 core_initcall(sock_init
); /* early initcall */
2164 #ifdef CONFIG_PROC_FS
2165 void socket_seq_show(struct seq_file
*seq
)
2170 for_each_possible_cpu(cpu
)
2171 counter
+= per_cpu(sockets_in_use
, cpu
);
2173 /* It can be negative, by the way. 8) */
2177 seq_printf(seq
, "sockets: used %d\n", counter
);
2179 #endif /* CONFIG_PROC_FS */
2181 #ifdef CONFIG_COMPAT
2182 static long compat_sock_ioctl(struct file
*file
, unsigned cmd
,
2185 struct socket
*sock
= file
->private_data
;
2186 int ret
= -ENOIOCTLCMD
;
2188 if (sock
->ops
->compat_ioctl
)
2189 ret
= sock
->ops
->compat_ioctl(sock
, cmd
, arg
);
2195 int kernel_bind(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
)
2197 return sock
->ops
->bind(sock
, addr
, addrlen
);
2200 int kernel_listen(struct socket
*sock
, int backlog
)
2202 return sock
->ops
->listen(sock
, backlog
);
2205 int kernel_accept(struct socket
*sock
, struct socket
**newsock
, int flags
)
2207 struct sock
*sk
= sock
->sk
;
2210 err
= sock_create_lite(sk
->sk_family
, sk
->sk_type
, sk
->sk_protocol
,
2215 err
= sock
->ops
->accept(sock
, *newsock
, flags
);
2217 sock_release(*newsock
);
2221 (*newsock
)->ops
= sock
->ops
;
2227 int kernel_connect(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
,
2230 return sock
->ops
->connect(sock
, addr
, addrlen
, flags
);
2233 int kernel_getsockname(struct socket
*sock
, struct sockaddr
*addr
,
2236 return sock
->ops
->getname(sock
, addr
, addrlen
, 0);
2239 int kernel_getpeername(struct socket
*sock
, struct sockaddr
*addr
,
2242 return sock
->ops
->getname(sock
, addr
, addrlen
, 1);
2245 int kernel_getsockopt(struct socket
*sock
, int level
, int optname
,
2246 char *optval
, int *optlen
)
2248 mm_segment_t oldfs
= get_fs();
2252 if (level
== SOL_SOCKET
)
2253 err
= sock_getsockopt(sock
, level
, optname
, optval
, optlen
);
2255 err
= sock
->ops
->getsockopt(sock
, level
, optname
, optval
,
2261 int kernel_setsockopt(struct socket
*sock
, int level
, int optname
,
2262 char *optval
, int optlen
)
2264 mm_segment_t oldfs
= get_fs();
2268 if (level
== SOL_SOCKET
)
2269 err
= sock_setsockopt(sock
, level
, optname
, optval
, optlen
);
2271 err
= sock
->ops
->setsockopt(sock
, level
, optname
, optval
,
2277 int kernel_sendpage(struct socket
*sock
, struct page
*page
, int offset
,
2278 size_t size
, int flags
)
2280 if (sock
->ops
->sendpage
)
2281 return sock
->ops
->sendpage(sock
, page
, offset
, size
, flags
);
2283 return sock_no_sendpage(sock
, page
, offset
, size
, flags
);
2286 int kernel_sock_ioctl(struct socket
*sock
, int cmd
, unsigned long arg
)
2288 mm_segment_t oldfs
= get_fs();
2292 err
= sock
->ops
->ioctl(sock
, cmd
, arg
);
2298 /* ABI emulation layers need these two */
2299 EXPORT_SYMBOL(move_addr_to_kernel
);
2300 EXPORT_SYMBOL(move_addr_to_user
);
2301 EXPORT_SYMBOL(sock_create
);
2302 EXPORT_SYMBOL(sock_create_kern
);
2303 EXPORT_SYMBOL(sock_create_lite
);
2304 EXPORT_SYMBOL(sock_map_fd
);
2305 EXPORT_SYMBOL(sock_recvmsg
);
2306 EXPORT_SYMBOL(sock_register
);
2307 EXPORT_SYMBOL(sock_release
);
2308 EXPORT_SYMBOL(sock_sendmsg
);
2309 EXPORT_SYMBOL(sock_unregister
);
2310 EXPORT_SYMBOL(sock_wake_async
);
2311 EXPORT_SYMBOL(sockfd_lookup
);
2312 EXPORT_SYMBOL(kernel_sendmsg
);
2313 EXPORT_SYMBOL(kernel_recvmsg
);
2314 EXPORT_SYMBOL(kernel_bind
);
2315 EXPORT_SYMBOL(kernel_listen
);
2316 EXPORT_SYMBOL(kernel_accept
);
2317 EXPORT_SYMBOL(kernel_connect
);
2318 EXPORT_SYMBOL(kernel_getsockname
);
2319 EXPORT_SYMBOL(kernel_getpeername
);
2320 EXPORT_SYMBOL(kernel_getsockopt
);
2321 EXPORT_SYMBOL(kernel_setsockopt
);
2322 EXPORT_SYMBOL(kernel_sendpage
);
2323 EXPORT_SYMBOL(kernel_sock_ioctl
);