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>
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
61 #include <linux/config.h>
63 #include <linux/smp_lock.h>
64 #include <linux/socket.h>
65 #include <linux/file.h>
66 #include <linux/net.h>
67 #include <linux/interrupt.h>
68 #include <linux/netdevice.h>
69 #include <linux/proc_fs.h>
70 #include <linux/seq_file.h>
71 #include <linux/wanrouter.h>
72 #include <linux/if_bridge.h>
73 #include <linux/init.h>
74 #include <linux/poll.h>
75 #include <linux/cache.h>
76 #include <linux/module.h>
77 #include <linux/highmem.h>
78 #include <linux/divert.h>
79 #include <linux/mount.h>
80 #include <linux/security.h>
81 #include <linux/syscalls.h>
82 #include <linux/compat.h>
83 #include <linux/kmod.h>
85 #ifdef CONFIG_NET_RADIO
86 #include <linux/wireless.h> /* Note : will define WIRELESS_EXT */
87 #endif /* CONFIG_NET_RADIO */
89 #include <asm/uaccess.h>
90 #include <net/compat.h>
93 #include <linux/netfilter.h>
95 static int sock_no_open(struct inode
*irrelevant
, struct file
*dontcare
);
96 static ssize_t
sock_aio_read(struct kiocb
*iocb
, char __user
*buf
,
97 size_t size
, loff_t pos
);
98 static ssize_t
sock_aio_write(struct kiocb
*iocb
, const char __user
*buf
,
99 size_t size
, loff_t pos
);
100 static int sock_mmap(struct file
*file
, struct vm_area_struct
* vma
);
102 static int sock_close(struct inode
*inode
, struct file
*file
);
103 static unsigned int sock_poll(struct file
*file
,
104 struct poll_table_struct
*wait
);
105 static int sock_ioctl(struct inode
*inode
, struct file
*file
,
106 unsigned int cmd
, unsigned long arg
);
107 static int sock_fasync(int fd
, struct file
*filp
, int on
);
108 static ssize_t
sock_readv(struct file
*file
, const struct iovec
*vector
,
109 unsigned long count
, loff_t
*ppos
);
110 static ssize_t
sock_writev(struct file
*file
, const struct iovec
*vector
,
111 unsigned long count
, loff_t
*ppos
);
112 static ssize_t
sock_sendpage(struct file
*file
, struct page
*page
,
113 int offset
, size_t size
, loff_t
*ppos
, int more
);
117 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
118 * in the operation structures but are done directly via the socketcall() multiplexor.
121 static struct file_operations socket_file_ops
= {
122 .owner
= THIS_MODULE
,
124 .aio_read
= sock_aio_read
,
125 .aio_write
= sock_aio_write
,
129 .open
= sock_no_open
, /* special open code to disallow open via /proc */
130 .release
= sock_close
,
131 .fasync
= sock_fasync
,
133 .writev
= sock_writev
,
134 .sendpage
= sock_sendpage
138 * The protocol list. Each protocol is registered in here.
141 static struct net_proto_family
*net_families
[NPROTO
];
143 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
144 static atomic_t net_family_lockct
= ATOMIC_INIT(0);
145 static spinlock_t net_family_lock
= SPIN_LOCK_UNLOCKED
;
147 /* The strategy is: modifications net_family vector are short, do not
148 sleep and veeery rare, but read access should be free of any exclusive
152 static void net_family_write_lock(void)
154 spin_lock(&net_family_lock
);
155 while (atomic_read(&net_family_lockct
) != 0) {
156 spin_unlock(&net_family_lock
);
160 spin_lock(&net_family_lock
);
164 static __inline__
void net_family_write_unlock(void)
166 spin_unlock(&net_family_lock
);
169 static __inline__
void net_family_read_lock(void)
171 atomic_inc(&net_family_lockct
);
172 spin_unlock_wait(&net_family_lock
);
175 static __inline__
void net_family_read_unlock(void)
177 atomic_dec(&net_family_lockct
);
181 #define net_family_write_lock() do { } while(0)
182 #define net_family_write_unlock() do { } while(0)
183 #define net_family_read_lock() do { } while(0)
184 #define net_family_read_unlock() do { } while(0)
189 * Statistics counters of the socket lists
192 static DEFINE_PER_CPU(int, sockets_in_use
) = 0;
195 * Support routines. Move socket addresses back and forth across the kernel/user
196 * divide and look after the messy bits.
199 #define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
200 16 for IP, 16 for IPX,
203 must be at least one bigger than
204 the AF_UNIX size (see net/unix/af_unix.c
209 * move_addr_to_kernel - copy a socket address into kernel space
210 * @uaddr: Address in user space
211 * @kaddr: Address in kernel space
212 * @ulen: Length in user space
214 * The address is copied into kernel space. If the provided address is
215 * too long an error code of -EINVAL is returned. If the copy gives
216 * invalid addresses -EFAULT is returned. On a success 0 is returned.
219 int move_addr_to_kernel(void __user
*uaddr
, int ulen
, void *kaddr
)
221 if(ulen
<0||ulen
>MAX_SOCK_ADDR
)
225 if(copy_from_user(kaddr
,uaddr
,ulen
))
231 * move_addr_to_user - copy an address to user space
232 * @kaddr: kernel space address
233 * @klen: length of address in kernel
234 * @uaddr: user space address
235 * @ulen: pointer to user length field
237 * The value pointed to by ulen on entry is the buffer length available.
238 * This is overwritten with the buffer space used. -EINVAL is returned
239 * if an overlong buffer is specified or a negative buffer size. -EFAULT
240 * is returned if either the buffer or the length field are not
242 * After copying the data up to the limit the user specifies, the true
243 * length of the data is written over the length limit the user
244 * specified. Zero is returned for a success.
247 int move_addr_to_user(void *kaddr
, int klen
, void __user
*uaddr
, int __user
*ulen
)
252 if((err
=get_user(len
, ulen
)))
256 if(len
<0 || len
> MAX_SOCK_ADDR
)
260 if(copy_to_user(uaddr
,kaddr
,len
))
264 * "fromlen shall refer to the value before truncation.."
267 return __put_user(klen
, ulen
);
270 #define SOCKFS_MAGIC 0x534F434B
272 static kmem_cache_t
* sock_inode_cachep
;
274 static struct inode
*sock_alloc_inode(struct super_block
*sb
)
276 struct socket_alloc
*ei
;
277 ei
= (struct socket_alloc
*)kmem_cache_alloc(sock_inode_cachep
, SLAB_KERNEL
);
280 init_waitqueue_head(&ei
->socket
.wait
);
282 ei
->socket
.fasync_list
= NULL
;
283 ei
->socket
.state
= SS_UNCONNECTED
;
284 ei
->socket
.flags
= 0;
285 ei
->socket
.ops
= NULL
;
286 ei
->socket
.sk
= NULL
;
287 ei
->socket
.file
= NULL
;
288 ei
->socket
.passcred
= 0;
290 return &ei
->vfs_inode
;
293 static void sock_destroy_inode(struct inode
*inode
)
295 kmem_cache_free(sock_inode_cachep
,
296 container_of(inode
, struct socket_alloc
, vfs_inode
));
299 static void init_once(void * foo
, kmem_cache_t
* cachep
, unsigned long flags
)
301 struct socket_alloc
*ei
= (struct socket_alloc
*) foo
;
303 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
304 SLAB_CTOR_CONSTRUCTOR
)
305 inode_init_once(&ei
->vfs_inode
);
308 static int init_inodecache(void)
310 sock_inode_cachep
= kmem_cache_create("sock_inode_cache",
311 sizeof(struct socket_alloc
),
312 0, SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
,
314 if (sock_inode_cachep
== NULL
)
319 static struct super_operations sockfs_ops
= {
320 .alloc_inode
= sock_alloc_inode
,
321 .destroy_inode
=sock_destroy_inode
,
322 .statfs
= simple_statfs
,
325 static struct super_block
*sockfs_get_sb(struct file_system_type
*fs_type
,
326 int flags
, const char *dev_name
, void *data
)
328 return get_sb_pseudo(fs_type
, "socket:", &sockfs_ops
, SOCKFS_MAGIC
);
331 static struct vfsmount
*sock_mnt
;
333 static struct file_system_type sock_fs_type
= {
335 .get_sb
= sockfs_get_sb
,
336 .kill_sb
= kill_anon_super
,
338 static int sockfs_delete_dentry(struct dentry
*dentry
)
342 static struct dentry_operations sockfs_dentry_operations
= {
343 .d_delete
= sockfs_delete_dentry
,
347 * Obtains the first available file descriptor and sets it up for use.
349 * This function creates file structure and maps it to fd space
350 * of current process. On success it returns file descriptor
351 * and file struct implicitly stored in sock->file.
352 * Note that another thread may close file descriptor before we return
353 * from this function. We use the fact that now we do not refer
354 * to socket after mapping. If one day we will need it, this
355 * function will increment ref. count on file by 1.
357 * In any case returned fd MAY BE not valid!
358 * This race condition is unavoidable
359 * with shared fd spaces, we cannot solve it inside kernel,
360 * but we take care of internal coherence yet.
363 int sock_map_fd(struct socket
*sock
)
370 * Find a file descriptor suitable for return to the user.
373 fd
= get_unused_fd();
375 struct file
*file
= get_empty_filp();
383 sprintf(name
, "[%lu]", SOCK_INODE(sock
)->i_ino
);
385 this.len
= strlen(name
);
386 this.hash
= SOCK_INODE(sock
)->i_ino
;
388 file
->f_dentry
= d_alloc(sock_mnt
->mnt_sb
->s_root
, &this);
389 if (!file
->f_dentry
) {
395 file
->f_dentry
->d_op
= &sockfs_dentry_operations
;
396 d_add(file
->f_dentry
, SOCK_INODE(sock
));
397 file
->f_vfsmnt
= mntget(sock_mnt
);
398 file
->f_mapping
= file
->f_dentry
->d_inode
->i_mapping
;
401 file
->f_op
= SOCK_INODE(sock
)->i_fop
= &socket_file_ops
;
403 file
->f_flags
= O_RDWR
;
405 fd_install(fd
, file
);
413 * sockfd_lookup - Go from a file number to its socket slot
415 * @err: pointer to an error code return
417 * The file handle passed in is locked and the socket it is bound
418 * too is returned. If an error occurs the err pointer is overwritten
419 * with a negative errno code and NULL is returned. The function checks
420 * for both invalid handles and passing a handle which is not a socket.
422 * On a success the socket object pointer is returned.
425 struct socket
*sockfd_lookup(int fd
, int *err
)
431 if (!(file
= fget(fd
)))
437 inode
= file
->f_dentry
->d_inode
;
438 if (!inode
->i_sock
|| !(sock
= SOCKET_I(inode
)))
445 if (sock
->file
!= file
) {
446 printk(KERN_ERR
"socki_lookup: socket file changed!\n");
453 * sock_alloc - allocate a socket
455 * Allocate a new inode and socket object. The two are bound together
456 * and initialised. The socket is then returned. If we are out of inodes
460 struct socket
*sock_alloc(void)
462 struct inode
* inode
;
463 struct socket
* sock
;
465 inode
= new_inode(sock_mnt
->mnt_sb
);
469 sock
= SOCKET_I(inode
);
471 inode
->i_mode
= S_IFSOCK
|S_IRWXUGO
;
473 inode
->i_uid
= current
->fsuid
;
474 inode
->i_gid
= current
->fsgid
;
476 get_cpu_var(sockets_in_use
)++;
477 put_cpu_var(sockets_in_use
);
482 * In theory you can't get an open on this inode, but /proc provides
483 * a back door. Remember to keep it shut otherwise you'll let the
484 * creepy crawlies in.
487 static int sock_no_open(struct inode
*irrelevant
, struct file
*dontcare
)
492 struct file_operations bad_sock_fops
= {
493 .owner
= THIS_MODULE
,
494 .open
= sock_no_open
,
498 * sock_release - close a socket
499 * @sock: socket to close
501 * The socket is released from the protocol stack if it has a release
502 * callback, and the inode is then released if the socket is bound to
503 * an inode not a file.
506 void sock_release(struct socket
*sock
)
509 struct module
*owner
= sock
->ops
->owner
;
511 sock
->ops
->release(sock
);
516 if (sock
->fasync_list
)
517 printk(KERN_ERR
"sock_release: fasync list not empty!\n");
519 get_cpu_var(sockets_in_use
)--;
520 put_cpu_var(sockets_in_use
);
522 iput(SOCK_INODE(sock
));
528 static inline int __sock_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
529 struct msghdr
*msg
, size_t size
)
531 struct sock_iocb
*si
= kiocb_to_siocb(iocb
);
539 err
= security_socket_sendmsg(sock
, msg
, size
);
543 return sock
->ops
->sendmsg(iocb
, sock
, msg
, size
);
546 int sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t size
)
551 init_sync_kiocb(&iocb
, NULL
);
552 ret
= __sock_sendmsg(&iocb
, sock
, msg
, size
);
553 if (-EIOCBQUEUED
== ret
)
554 ret
= wait_on_sync_kiocb(&iocb
);
559 static inline int __sock_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
560 struct msghdr
*msg
, size_t size
, int flags
)
563 struct sock_iocb
*si
= kiocb_to_siocb(iocb
);
571 err
= security_socket_recvmsg(sock
, msg
, size
, flags
);
575 return sock
->ops
->recvmsg(iocb
, sock
, msg
, size
, flags
);
578 int sock_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
579 size_t size
, int flags
)
584 init_sync_kiocb(&iocb
, NULL
);
585 ret
= __sock_recvmsg(&iocb
, sock
, msg
, size
, flags
);
586 if (-EIOCBQUEUED
== ret
)
587 ret
= wait_on_sync_kiocb(&iocb
);
592 * Read data from a socket. ubuf is a user mode pointer. We make sure the user
593 * area ubuf...ubuf+size-1 is writable before asking the protocol.
596 static ssize_t
sock_aio_read(struct kiocb
*iocb
, char __user
*ubuf
,
597 size_t size
, loff_t pos
)
599 struct sock_iocb
*x
= kiocb_to_siocb(iocb
);
605 if (size
==0) /* Match SYS5 behaviour */
608 sock
= SOCKET_I(iocb
->ki_filp
->f_dentry
->d_inode
);
610 x
->async_msg
.msg_name
= NULL
;
611 x
->async_msg
.msg_namelen
= 0;
612 x
->async_msg
.msg_iov
= &x
->async_iov
;
613 x
->async_msg
.msg_iovlen
= 1;
614 x
->async_msg
.msg_control
= NULL
;
615 x
->async_msg
.msg_controllen
= 0;
616 x
->async_iov
.iov_base
= ubuf
;
617 x
->async_iov
.iov_len
= size
;
618 flags
= !(iocb
->ki_filp
->f_flags
& O_NONBLOCK
) ? 0 : MSG_DONTWAIT
;
620 return __sock_recvmsg(iocb
, sock
, &x
->async_msg
, size
, flags
);
625 * Write data to a socket. We verify that the user area ubuf..ubuf+size-1
626 * is readable by the user process.
629 static ssize_t
sock_aio_write(struct kiocb
*iocb
, const char __user
*ubuf
,
630 size_t size
, loff_t pos
)
632 struct sock_iocb
*x
= kiocb_to_siocb(iocb
);
637 if(size
==0) /* Match SYS5 behaviour */
640 sock
= SOCKET_I(iocb
->ki_filp
->f_dentry
->d_inode
);
642 x
->async_msg
.msg_name
= NULL
;
643 x
->async_msg
.msg_namelen
= 0;
644 x
->async_msg
.msg_iov
= &x
->async_iov
;
645 x
->async_msg
.msg_iovlen
= 1;
646 x
->async_msg
.msg_control
= NULL
;
647 x
->async_msg
.msg_controllen
= 0;
648 x
->async_msg
.msg_flags
= !(iocb
->ki_filp
->f_flags
& O_NONBLOCK
) ? 0 : MSG_DONTWAIT
;
649 if (sock
->type
== SOCK_SEQPACKET
)
650 x
->async_msg
.msg_flags
|= MSG_EOR
;
651 x
->async_iov
.iov_base
= (void __user
*)ubuf
;
652 x
->async_iov
.iov_len
= size
;
654 return __sock_sendmsg(iocb
, sock
, &x
->async_msg
, size
);
657 ssize_t
sock_sendpage(struct file
*file
, struct page
*page
,
658 int offset
, size_t size
, loff_t
*ppos
, int more
)
663 if (ppos
!= &file
->f_pos
)
666 sock
= SOCKET_I(file
->f_dentry
->d_inode
);
668 flags
= !(file
->f_flags
& O_NONBLOCK
) ? 0 : MSG_DONTWAIT
;
672 return sock
->ops
->sendpage(sock
, page
, offset
, size
, flags
);
675 int sock_readv_writev(int type
, struct inode
* inode
, struct file
* file
,
676 const struct iovec
* iov
, long count
, size_t size
)
681 sock
= SOCKET_I(inode
);
685 msg
.msg_control
= NULL
;
686 msg
.msg_controllen
= 0;
687 msg
.msg_iov
= (struct iovec
*) iov
;
688 msg
.msg_iovlen
= count
;
689 msg
.msg_flags
= (file
->f_flags
& O_NONBLOCK
) ? MSG_DONTWAIT
: 0;
691 /* read() does a VERIFY_WRITE */
692 if (type
== VERIFY_WRITE
)
693 return sock_recvmsg(sock
, &msg
, size
, msg
.msg_flags
);
695 if (sock
->type
== SOCK_SEQPACKET
)
696 msg
.msg_flags
|= MSG_EOR
;
698 return sock_sendmsg(sock
, &msg
, size
);
701 static ssize_t
sock_readv(struct file
*file
, const struct iovec
*vector
,
702 unsigned long count
, loff_t
*ppos
)
706 for (i
= 0 ; i
< count
; i
++)
707 tot_len
+= vector
[i
].iov_len
;
708 return sock_readv_writev(VERIFY_WRITE
, file
->f_dentry
->d_inode
,
709 file
, vector
, count
, tot_len
);
712 static ssize_t
sock_writev(struct file
*file
, const struct iovec
*vector
,
713 unsigned long count
, loff_t
*ppos
)
717 for (i
= 0 ; i
< count
; i
++)
718 tot_len
+= vector
[i
].iov_len
;
719 return sock_readv_writev(VERIFY_READ
, file
->f_dentry
->d_inode
,
720 file
, vector
, count
, tot_len
);
725 * Atomic setting of ioctl hooks to avoid race
726 * with module unload.
729 static DECLARE_MUTEX(br_ioctl_mutex
);
730 static int (*br_ioctl_hook
)(unsigned long arg
) = NULL
;
732 void brioctl_set(int (*hook
)(unsigned long))
734 down(&br_ioctl_mutex
);
735 br_ioctl_hook
= hook
;
738 EXPORT_SYMBOL(brioctl_set
);
740 static DECLARE_MUTEX(vlan_ioctl_mutex
);
741 static int (*vlan_ioctl_hook
)(unsigned long arg
);
743 void vlan_ioctl_set(int (*hook
)(unsigned long))
745 down(&vlan_ioctl_mutex
);
746 vlan_ioctl_hook
= hook
;
747 up(&vlan_ioctl_mutex
);
749 EXPORT_SYMBOL(vlan_ioctl_set
);
751 static DECLARE_MUTEX(dlci_ioctl_mutex
);
752 static int (*dlci_ioctl_hook
)(unsigned int, void *);
754 void dlci_ioctl_set(int (*hook
)(unsigned int, void *))
756 down(&dlci_ioctl_mutex
);
757 dlci_ioctl_hook
= hook
;
758 up(&dlci_ioctl_mutex
);
760 EXPORT_SYMBOL(dlci_ioctl_set
);
763 * With an ioctl, arg may well be a user mode pointer, but we don't know
764 * what to do with it - that's up to the protocol still.
767 static int sock_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
774 sock
= SOCKET_I(inode
);
775 if (cmd
>= SIOCDEVPRIVATE
&& cmd
<= (SIOCDEVPRIVATE
+ 15)) {
776 err
= dev_ioctl(cmd
, (void *)arg
);
779 if (cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
) {
780 err
= dev_ioctl(cmd
, (void *)arg
);
782 #endif /* WIRELESS_EXT */
787 if (get_user(pid
, (int *)arg
))
789 err
= f_setown(sock
->file
, pid
, 1);
793 err
= put_user(sock
->file
->f_owner
.pid
, (int *)arg
);
799 request_module("bridge");
801 down(&br_ioctl_mutex
);
803 err
= br_ioctl_hook(arg
);
809 if (!vlan_ioctl_hook
)
810 request_module("8021q");
812 down(&vlan_ioctl_mutex
);
814 err
= vlan_ioctl_hook(arg
);
815 up(&vlan_ioctl_mutex
);
819 /* Convert this to call through a hook */
820 err
= divert_ioctl(cmd
, (struct divert_cf
*)arg
);
825 if (!dlci_ioctl_hook
)
826 request_module("dlci");
828 if (dlci_ioctl_hook
) {
829 down(&dlci_ioctl_mutex
);
830 err
= dlci_ioctl_hook(cmd
, (void *)arg
);
831 up(&dlci_ioctl_mutex
);
835 err
= sock
->ops
->ioctl(sock
, cmd
, arg
);
844 /* No kernel lock held - perfect */
845 static unsigned int sock_poll(struct file
*file
, poll_table
* wait
)
850 * We can't return errors to poll, so it's either yes or no.
852 sock
= SOCKET_I(file
->f_dentry
->d_inode
);
853 return sock
->ops
->poll(file
, sock
, wait
);
856 static int sock_mmap(struct file
* file
, struct vm_area_struct
* vma
)
858 struct socket
*sock
= SOCKET_I(file
->f_dentry
->d_inode
);
860 return sock
->ops
->mmap(file
, sock
, vma
);
863 int sock_close(struct inode
*inode
, struct file
*filp
)
866 * It was possible the inode is NULL we were
867 * closing an unfinished socket.
872 printk(KERN_DEBUG
"sock_close: NULL inode\n");
875 sock_fasync(-1, filp
, 0);
876 sock_release(SOCKET_I(inode
));
881 * Update the socket async list
883 * Fasync_list locking strategy.
885 * 1. fasync_list is modified only under process context socket lock
886 * i.e. under semaphore.
887 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
888 * or under socket lock.
889 * 3. fasync_list can be used from softirq context, so that
890 * modification under socket lock have to be enhanced with
891 * write_lock_bh(&sk->sk_callback_lock).
895 static int sock_fasync(int fd
, struct file
*filp
, int on
)
897 struct fasync_struct
*fa
, *fna
=NULL
, **prev
;
903 fna
=(struct fasync_struct
*)kmalloc(sizeof(struct fasync_struct
), GFP_KERNEL
);
908 sock
= SOCKET_I(filp
->f_dentry
->d_inode
);
910 if ((sk
=sock
->sk
) == NULL
) {
918 prev
=&(sock
->fasync_list
);
920 for (fa
=*prev
; fa
!=NULL
; prev
=&fa
->fa_next
,fa
=*prev
)
921 if (fa
->fa_file
==filp
)
928 write_lock_bh(&sk
->sk_callback_lock
);
930 write_unlock_bh(&sk
->sk_callback_lock
);
937 fna
->magic
=FASYNC_MAGIC
;
938 fna
->fa_next
=sock
->fasync_list
;
939 write_lock_bh(&sk
->sk_callback_lock
);
940 sock
->fasync_list
=fna
;
941 write_unlock_bh(&sk
->sk_callback_lock
);
947 write_lock_bh(&sk
->sk_callback_lock
);
949 write_unlock_bh(&sk
->sk_callback_lock
);
955 release_sock(sock
->sk
);
959 /* This function may be called only under socket lock or callback_lock */
961 int sock_wake_async(struct socket
*sock
, int how
, int band
)
963 if (!sock
|| !sock
->fasync_list
)
969 if (test_bit(SOCK_ASYNC_WAITDATA
, &sock
->flags
))
973 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE
, &sock
->flags
))
978 __kill_fasync(sock
->fasync_list
, SIGIO
, band
);
981 __kill_fasync(sock
->fasync_list
, SIGURG
, band
);
987 int sock_create(int family
, int type
, int protocol
, struct socket
**res
)
994 * Check protocol is in range
996 if (family
< 0 || family
>= NPROTO
)
997 return -EAFNOSUPPORT
;
998 if (type
< 0 || type
>= SOCK_MAX
)
1003 This uglymoron is moved from INET layer to here to avoid
1004 deadlock in module load.
1006 if (family
== PF_INET
&& type
== SOCK_PACKET
) {
1010 printk(KERN_INFO
"%s uses obsolete (PF_INET,SOCK_PACKET)\n", current
->comm
);
1015 err
= security_socket_create(family
, type
, protocol
);
1019 #if defined(CONFIG_KMOD)
1020 /* Attempt to load a protocol module if the find failed.
1022 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1023 * requested real, full-featured networking support upon configuration.
1024 * Otherwise module support will break!
1026 if (net_families
[family
]==NULL
)
1028 request_module("net-pf-%d",family
);
1032 net_family_read_lock();
1033 if (net_families
[family
] == NULL
) {
1039 * Allocate the socket and allow the family to set things up. if
1040 * the protocol is 0, the family is instructed to select an appropriate
1044 if (!(sock
= sock_alloc()))
1046 printk(KERN_WARNING
"socket: no more sockets\n");
1047 i
= -ENFILE
; /* Not exactly a match, but its the
1048 closest posix thing */
1055 * We will call the ->create function, that possibly is in a loadable
1056 * module, so we have to bump that loadable module refcnt first.
1059 if (!try_module_get(net_families
[family
]->owner
))
1062 if ((i
= net_families
[family
]->create(sock
, protocol
)) < 0)
1063 goto out_module_put
;
1065 * Now to bump the refcnt of the [loadable] module that owns this
1066 * socket at sock_release time we decrement its refcnt.
1068 if (!try_module_get(sock
->ops
->owner
)) {
1070 goto out_module_put
;
1073 * Now that we're done with the ->create function, the [loadable]
1074 * module can have its refcnt decremented
1076 module_put(net_families
[family
]->owner
);
1078 security_socket_post_create(sock
, family
, type
, protocol
);
1081 net_family_read_unlock();
1084 module_put(net_families
[family
]->owner
);
1090 asmlinkage
long sys_socket(int family
, int type
, int protocol
)
1093 struct socket
*sock
;
1095 retval
= sock_create(family
, type
, protocol
, &sock
);
1099 retval
= sock_map_fd(sock
);
1104 /* It may be already another descriptor 8) Not kernel problem. */
1113 * Create a pair of connected sockets.
1116 asmlinkage
long sys_socketpair(int family
, int type
, int protocol
, int __user
*usockvec
)
1118 struct socket
*sock1
, *sock2
;
1122 * Obtain the first socket and check if the underlying protocol
1123 * supports the socketpair call.
1126 err
= sock_create(family
, type
, protocol
, &sock1
);
1130 err
= sock_create(family
, type
, protocol
, &sock2
);
1134 err
= sock1
->ops
->socketpair(sock1
, sock2
);
1136 goto out_release_both
;
1140 err
= sock_map_fd(sock1
);
1142 goto out_release_both
;
1145 err
= sock_map_fd(sock2
);
1150 /* fd1 and fd2 may be already another descriptors.
1151 * Not kernel problem.
1154 err
= put_user(fd1
, &usockvec
[0]);
1156 err
= put_user(fd2
, &usockvec
[1]);
1165 sock_release(sock2
);
1170 sock_release(sock2
);
1172 sock_release(sock1
);
1179 * Bind a name to a socket. Nothing much to do here since it's
1180 * the protocol's responsibility to handle the local address.
1182 * We move the socket address to kernel space before we call
1183 * the protocol layer (having also checked the address is ok).
1186 asmlinkage
long sys_bind(int fd
, struct sockaddr __user
*umyaddr
, int addrlen
)
1188 struct socket
*sock
;
1189 char address
[MAX_SOCK_ADDR
];
1192 if((sock
= sockfd_lookup(fd
,&err
))!=NULL
)
1194 if((err
=move_addr_to_kernel(umyaddr
,addrlen
,address
))>=0) {
1195 err
= security_socket_bind(sock
, (struct sockaddr
*)address
, addrlen
);
1200 err
= sock
->ops
->bind(sock
, (struct sockaddr
*)address
, addrlen
);
1209 * Perform a listen. Basically, we allow the protocol to do anything
1210 * necessary for a listen, and if that works, we mark the socket as
1211 * ready for listening.
1214 int sysctl_somaxconn
= SOMAXCONN
;
1216 asmlinkage
long sys_listen(int fd
, int backlog
)
1218 struct socket
*sock
;
1221 if ((sock
= sockfd_lookup(fd
, &err
)) != NULL
) {
1222 if ((unsigned) backlog
> sysctl_somaxconn
)
1223 backlog
= sysctl_somaxconn
;
1225 err
= security_socket_listen(sock
, backlog
);
1231 err
=sock
->ops
->listen(sock
, backlog
);
1239 * For accept, we attempt to create a new socket, set up the link
1240 * with the client, wake up the client, then return the new
1241 * connected fd. We collect the address of the connector in kernel
1242 * space and move it to user at the very end. This is unclean because
1243 * we open the socket then return an error.
1245 * 1003.1g adds the ability to recvmsg() to query connection pending
1246 * status to recvmsg. We need to add that support in a way thats
1247 * clean when we restucture accept also.
1250 asmlinkage
long sys_accept(int fd
, struct sockaddr __user
*upeer_sockaddr
, int __user
*upeer_addrlen
)
1252 struct socket
*sock
, *newsock
;
1254 char address
[MAX_SOCK_ADDR
];
1256 sock
= sockfd_lookup(fd
, &err
);
1261 if (!(newsock
= sock_alloc()))
1264 newsock
->type
= sock
->type
;
1265 newsock
->ops
= sock
->ops
;
1267 err
= security_socket_accept(sock
, newsock
);
1272 * We don't need try_module_get here, as the listening socket (sock)
1273 * has the protocol module (sock->ops->owner) held.
1275 __module_get(newsock
->ops
->owner
);
1277 err
= sock
->ops
->accept(sock
, newsock
, sock
->file
->f_flags
);
1281 if (upeer_sockaddr
) {
1282 if(newsock
->ops
->getname(newsock
, (struct sockaddr
*)address
, &len
, 2)<0) {
1283 err
= -ECONNABORTED
;
1286 err
= move_addr_to_user(address
, len
, upeer_sockaddr
, upeer_addrlen
);
1291 /* File flags are not inherited via accept() unlike another OSes. */
1293 if ((err
= sock_map_fd(newsock
)) < 0)
1296 security_socket_post_accept(sock
, newsock
);
1303 sock_release(newsock
);
1309 * Attempt to connect to a socket with the server address. The address
1310 * is in user space so we verify it is OK and move it to kernel space.
1312 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1315 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1316 * other SEQPACKET protocols that take time to connect() as it doesn't
1317 * include the -EINPROGRESS status for such sockets.
1320 asmlinkage
long sys_connect(int fd
, struct sockaddr __user
*uservaddr
, int addrlen
)
1322 struct socket
*sock
;
1323 char address
[MAX_SOCK_ADDR
];
1326 sock
= sockfd_lookup(fd
, &err
);
1329 err
= move_addr_to_kernel(uservaddr
, addrlen
, address
);
1333 err
= security_socket_connect(sock
, (struct sockaddr
*)address
, addrlen
);
1337 err
= sock
->ops
->connect(sock
, (struct sockaddr
*) address
, addrlen
,
1338 sock
->file
->f_flags
);
1346 * Get the local address ('name') of a socket object. Move the obtained
1347 * name to user space.
1350 asmlinkage
long sys_getsockname(int fd
, struct sockaddr __user
*usockaddr
, int __user
*usockaddr_len
)
1352 struct socket
*sock
;
1353 char address
[MAX_SOCK_ADDR
];
1356 sock
= sockfd_lookup(fd
, &err
);
1360 err
= security_socket_getsockname(sock
);
1364 err
= sock
->ops
->getname(sock
, (struct sockaddr
*)address
, &len
, 0);
1367 err
= move_addr_to_user(address
, len
, usockaddr
, usockaddr_len
);
1376 * Get the remote address ('name') of a socket object. Move the obtained
1377 * name to user space.
1380 asmlinkage
long sys_getpeername(int fd
, struct sockaddr __user
*usockaddr
, int __user
*usockaddr_len
)
1382 struct socket
*sock
;
1383 char address
[MAX_SOCK_ADDR
];
1386 if ((sock
= sockfd_lookup(fd
, &err
))!=NULL
)
1388 err
= security_socket_getpeername(sock
);
1394 err
= sock
->ops
->getname(sock
, (struct sockaddr
*)address
, &len
, 1);
1396 err
=move_addr_to_user(address
,len
, usockaddr
, usockaddr_len
);
1403 * Send a datagram to a given address. We move the address into kernel
1404 * space and check the user space data area is readable before invoking
1408 asmlinkage
long sys_sendto(int fd
, void __user
* buff
, size_t len
, unsigned flags
,
1409 struct sockaddr __user
*addr
, int addr_len
)
1411 struct socket
*sock
;
1412 char address
[MAX_SOCK_ADDR
];
1417 sock
= sockfd_lookup(fd
, &err
);
1425 msg
.msg_control
=NULL
;
1426 msg
.msg_controllen
=0;
1430 err
= move_addr_to_kernel(addr
, addr_len
, address
);
1433 msg
.msg_name
=address
;
1434 msg
.msg_namelen
=addr_len
;
1436 if (sock
->file
->f_flags
& O_NONBLOCK
)
1437 flags
|= MSG_DONTWAIT
;
1438 msg
.msg_flags
= flags
;
1439 err
= sock_sendmsg(sock
, &msg
, len
);
1448 * Send a datagram down a socket.
1451 asmlinkage
long sys_send(int fd
, void __user
* buff
, size_t len
, unsigned flags
)
1453 return sys_sendto(fd
, buff
, len
, flags
, NULL
, 0);
1457 * Receive a frame from the socket and optionally record the address of the
1458 * sender. We verify the buffers are writable and if needed move the
1459 * sender address from kernel to user space.
1462 asmlinkage
long sys_recvfrom(int fd
, void __user
* ubuf
, size_t size
, unsigned flags
,
1463 struct sockaddr __user
*addr
, int __user
*addr_len
)
1465 struct socket
*sock
;
1468 char address
[MAX_SOCK_ADDR
];
1471 sock
= sockfd_lookup(fd
, &err
);
1475 msg
.msg_control
=NULL
;
1476 msg
.msg_controllen
=0;
1481 msg
.msg_name
=address
;
1482 msg
.msg_namelen
=MAX_SOCK_ADDR
;
1483 if (sock
->file
->f_flags
& O_NONBLOCK
)
1484 flags
|= MSG_DONTWAIT
;
1485 err
=sock_recvmsg(sock
, &msg
, size
, flags
);
1487 if(err
>= 0 && addr
!= NULL
)
1489 err2
=move_addr_to_user(address
, msg
.msg_namelen
, addr
, addr_len
);
1499 * Receive a datagram from a socket.
1502 asmlinkage
long sys_recv(int fd
, void __user
* ubuf
, size_t size
, unsigned flags
)
1504 return sys_recvfrom(fd
, ubuf
, size
, flags
, NULL
, NULL
);
1508 * Set a socket option. Because we don't know the option lengths we have
1509 * to pass the user mode parameter for the protocols to sort out.
1512 asmlinkage
long sys_setsockopt(int fd
, int level
, int optname
, char __user
*optval
, int optlen
)
1515 struct socket
*sock
;
1520 if ((sock
= sockfd_lookup(fd
, &err
))!=NULL
)
1522 err
= security_socket_setsockopt(sock
,level
,optname
);
1528 if (level
== SOL_SOCKET
)
1529 err
=sock_setsockopt(sock
,level
,optname
,optval
,optlen
);
1531 err
=sock
->ops
->setsockopt(sock
, level
, optname
, optval
, optlen
);
1538 * Get a socket option. Because we don't know the option lengths we have
1539 * to pass a user mode parameter for the protocols to sort out.
1542 asmlinkage
long sys_getsockopt(int fd
, int level
, int optname
, char __user
*optval
, int __user
*optlen
)
1545 struct socket
*sock
;
1547 if ((sock
= sockfd_lookup(fd
, &err
))!=NULL
)
1549 err
= security_socket_getsockopt(sock
, level
,
1556 if (level
== SOL_SOCKET
)
1557 err
=sock_getsockopt(sock
,level
,optname
,optval
,optlen
);
1559 err
=sock
->ops
->getsockopt(sock
, level
, optname
, optval
, optlen
);
1567 * Shutdown a socket.
1570 asmlinkage
long sys_shutdown(int fd
, int how
)
1573 struct socket
*sock
;
1575 if ((sock
= sockfd_lookup(fd
, &err
))!=NULL
)
1577 err
= security_socket_shutdown(sock
, how
);
1583 err
=sock
->ops
->shutdown(sock
, how
);
1589 /* A couple of helpful macros for getting the address of the 32/64 bit
1590 * fields which are the same type (int / unsigned) on our platforms.
1592 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1593 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1594 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1598 * BSD sendmsg interface
1601 asmlinkage
long sys_sendmsg(int fd
, struct msghdr __user
*msg
, unsigned flags
)
1603 struct compat_msghdr __user
*msg_compat
= (struct compat_msghdr __user
*)msg
;
1604 struct socket
*sock
;
1605 char address
[MAX_SOCK_ADDR
];
1606 struct iovec iovstack
[UIO_FASTIOV
], *iov
= iovstack
;
1607 unsigned char ctl
[sizeof(struct cmsghdr
) + 20]; /* 20 is size of ipv6_pktinfo */
1608 unsigned char *ctl_buf
= ctl
;
1609 struct msghdr msg_sys
;
1610 int err
, ctl_len
, iov_size
, total_len
;
1613 if (MSG_CMSG_COMPAT
& flags
) {
1614 if (get_compat_msghdr(&msg_sys
, msg_compat
))
1616 } else if (copy_from_user(&msg_sys
, msg
, sizeof(struct msghdr
)))
1619 sock
= sockfd_lookup(fd
, &err
);
1623 /* do not move before msg_sys is valid */
1625 if (msg_sys
.msg_iovlen
> UIO_MAXIOV
)
1628 /* Check whether to allocate the iovec area*/
1630 iov_size
= msg_sys
.msg_iovlen
* sizeof(struct iovec
);
1631 if (msg_sys
.msg_iovlen
> UIO_FASTIOV
) {
1632 iov
= sock_kmalloc(sock
->sk
, iov_size
, GFP_KERNEL
);
1637 /* This will also move the address data into kernel space */
1638 if (MSG_CMSG_COMPAT
& flags
) {
1639 err
= verify_compat_iovec(&msg_sys
, iov
, address
, VERIFY_READ
);
1641 err
= verify_iovec(&msg_sys
, iov
, address
, VERIFY_READ
);
1648 if (msg_sys
.msg_controllen
> INT_MAX
)
1650 ctl_len
= msg_sys
.msg_controllen
;
1651 if ((MSG_CMSG_COMPAT
& flags
) && ctl_len
) {
1652 err
= cmsghdr_from_user_compat_to_kern(&msg_sys
, ctl
, sizeof(ctl
));
1655 ctl_buf
= msg_sys
.msg_control
;
1656 } else if (ctl_len
) {
1657 if (ctl_len
> sizeof(ctl
))
1659 ctl_buf
= sock_kmalloc(sock
->sk
, ctl_len
, GFP_KERNEL
);
1660 if (ctl_buf
== NULL
)
1665 * Careful! Before this, msg_sys.msg_control contains a user pointer.
1666 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1667 * checking falls down on this.
1669 if (copy_from_user(ctl_buf
, (void __user
*) msg_sys
.msg_control
, ctl_len
))
1671 msg_sys
.msg_control
= ctl_buf
;
1673 msg_sys
.msg_flags
= flags
;
1675 if (sock
->file
->f_flags
& O_NONBLOCK
)
1676 msg_sys
.msg_flags
|= MSG_DONTWAIT
;
1677 err
= sock_sendmsg(sock
, &msg_sys
, total_len
);
1681 sock_kfree_s(sock
->sk
, ctl_buf
, ctl_len
);
1683 if (iov
!= iovstack
)
1684 sock_kfree_s(sock
->sk
, iov
, iov_size
);
1692 * BSD recvmsg interface
1695 asmlinkage
long sys_recvmsg(int fd
, struct msghdr __user
*msg
, unsigned int flags
)
1697 struct compat_msghdr __user
*msg_compat
= (struct compat_msghdr __user
*)msg
;
1698 struct socket
*sock
;
1699 struct iovec iovstack
[UIO_FASTIOV
];
1700 struct iovec
*iov
=iovstack
;
1701 struct msghdr msg_sys
;
1702 unsigned long cmsg_ptr
;
1703 int err
, iov_size
, total_len
, len
;
1705 /* kernel mode address */
1706 char addr
[MAX_SOCK_ADDR
];
1708 /* user mode address pointers */
1709 struct sockaddr __user
*uaddr
;
1710 int __user
*uaddr_len
;
1712 if (MSG_CMSG_COMPAT
& flags
) {
1713 if (get_compat_msghdr(&msg_sys
, msg_compat
))
1716 if (copy_from_user(&msg_sys
,msg
,sizeof(struct msghdr
)))
1719 sock
= sockfd_lookup(fd
, &err
);
1724 if (msg_sys
.msg_iovlen
> UIO_MAXIOV
)
1727 /* Check whether to allocate the iovec area*/
1729 iov_size
= msg_sys
.msg_iovlen
* sizeof(struct iovec
);
1730 if (msg_sys
.msg_iovlen
> UIO_FASTIOV
) {
1731 iov
= sock_kmalloc(sock
->sk
, iov_size
, GFP_KERNEL
);
1737 * Save the user-mode address (verify_iovec will change the
1738 * kernel msghdr to use the kernel address space)
1741 uaddr
= (void __user
*) msg_sys
.msg_name
;
1742 uaddr_len
= COMPAT_NAMELEN(msg
);
1743 if (MSG_CMSG_COMPAT
& flags
) {
1744 err
= verify_compat_iovec(&msg_sys
, iov
, addr
, VERIFY_WRITE
);
1746 err
= verify_iovec(&msg_sys
, iov
, addr
, VERIFY_WRITE
);
1751 cmsg_ptr
= (unsigned long)msg_sys
.msg_control
;
1752 msg_sys
.msg_flags
= 0;
1753 if (MSG_CMSG_COMPAT
& flags
)
1754 msg_sys
.msg_flags
= MSG_CMSG_COMPAT
;
1756 if (sock
->file
->f_flags
& O_NONBLOCK
)
1757 flags
|= MSG_DONTWAIT
;
1758 err
= sock_recvmsg(sock
, &msg_sys
, total_len
, flags
);
1763 if (uaddr
!= NULL
) {
1764 err
= move_addr_to_user(addr
, msg_sys
.msg_namelen
, uaddr
, uaddr_len
);
1768 err
= __put_user(msg_sys
.msg_flags
, COMPAT_FLAGS(msg
));
1771 if (MSG_CMSG_COMPAT
& flags
)
1772 err
= __put_user((unsigned long)msg_sys
.msg_control
-cmsg_ptr
,
1773 &msg_compat
->msg_controllen
);
1775 err
= __put_user((unsigned long)msg_sys
.msg_control
-cmsg_ptr
,
1776 &msg
->msg_controllen
);
1782 if (iov
!= iovstack
)
1783 sock_kfree_s(sock
->sk
, iov
, iov_size
);
1790 /* Argument list sizes for sys_socketcall */
1791 #define AL(x) ((x) * sizeof(unsigned long))
1792 static unsigned char nargs
[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
1793 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
1794 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)};
1798 * System call vectors.
1800 * Argument checking cleaned up. Saved 20% in size.
1801 * This function doesn't need to set the kernel lock because
1802 * it is set by the callees.
1805 asmlinkage
long sys_socketcall(int call
, unsigned long __user
*args
)
1808 unsigned long a0
,a1
;
1811 if(call
<1||call
>SYS_RECVMSG
)
1814 /* copy_from_user should be SMP safe. */
1815 if (copy_from_user(a
, args
, nargs
[call
]))
1824 err
= sys_socket(a0
,a1
,a
[2]);
1827 err
= sys_bind(a0
,(struct sockaddr __user
*)a1
, a
[2]);
1830 err
= sys_connect(a0
, (struct sockaddr __user
*)a1
, a
[2]);
1833 err
= sys_listen(a0
,a1
);
1836 err
= sys_accept(a0
,(struct sockaddr __user
*)a1
, (int __user
*)a
[2]);
1838 case SYS_GETSOCKNAME
:
1839 err
= sys_getsockname(a0
,(struct sockaddr __user
*)a1
, (int __user
*)a
[2]);
1841 case SYS_GETPEERNAME
:
1842 err
= sys_getpeername(a0
, (struct sockaddr __user
*)a1
, (int __user
*)a
[2]);
1844 case SYS_SOCKETPAIR
:
1845 err
= sys_socketpair(a0
,a1
, a
[2], (int __user
*)a
[3]);
1848 err
= sys_send(a0
, (void __user
*)a1
, a
[2], a
[3]);
1851 err
= sys_sendto(a0
,(void __user
*)a1
, a
[2], a
[3],
1852 (struct sockaddr __user
*)a
[4], a
[5]);
1855 err
= sys_recv(a0
, (void __user
*)a1
, a
[2], a
[3]);
1858 err
= sys_recvfrom(a0
, (void __user
*)a1
, a
[2], a
[3],
1859 (struct sockaddr __user
*)a
[4], (int __user
*)a
[5]);
1862 err
= sys_shutdown(a0
,a1
);
1864 case SYS_SETSOCKOPT
:
1865 err
= sys_setsockopt(a0
, a1
, a
[2], (char __user
*)a
[3], a
[4]);
1867 case SYS_GETSOCKOPT
:
1868 err
= sys_getsockopt(a0
, a1
, a
[2], (char __user
*)a
[3], (int __user
*)a
[4]);
1871 err
= sys_sendmsg(a0
, (struct msghdr __user
*) a1
, a
[2]);
1874 err
= sys_recvmsg(a0
, (struct msghdr __user
*) a1
, a
[2]);
1884 * This function is called by a protocol handler that wants to
1885 * advertise its address family, and have it linked into the
1889 int sock_register(struct net_proto_family
*ops
)
1893 if (ops
->family
>= NPROTO
) {
1894 printk(KERN_CRIT
"protocol %d >= NPROTO(%d)\n", ops
->family
, NPROTO
);
1897 net_family_write_lock();
1899 if (net_families
[ops
->family
] == NULL
) {
1900 net_families
[ops
->family
]=ops
;
1903 net_family_write_unlock();
1904 printk(KERN_INFO
"NET: Registered protocol family %d\n",
1910 * This function is called by a protocol handler that wants to
1911 * remove its address family, and have it unlinked from the
1915 int sock_unregister(int family
)
1917 if (family
< 0 || family
>= NPROTO
)
1920 net_family_write_lock();
1921 net_families
[family
]=NULL
;
1922 net_family_write_unlock();
1923 printk(KERN_INFO
"NET: Unregistered protocol family %d\n",
1929 extern void sk_init(void);
1931 void __init
sock_init(void)
1936 * Initialize all address (protocol) families.
1939 for (i
= 0; i
< NPROTO
; i
++)
1940 net_families
[i
] = NULL
;
1943 * Initialize sock SLAB cache.
1950 * Initialize skbuff SLAB cache
1956 * Initialize the protocols module.
1960 register_filesystem(&sock_fs_type
);
1961 sock_mnt
= kern_mount(&sock_fs_type
);
1962 /* The real protocol initialization is performed when
1963 * do_initcalls is run.
1966 #ifdef CONFIG_NETFILTER
1971 #ifdef CONFIG_PROC_FS
1972 void socket_seq_show(struct seq_file
*seq
)
1977 for (cpu
= 0; cpu
< NR_CPUS
; cpu
++)
1978 counter
+= per_cpu(sockets_in_use
, cpu
);
1980 /* It can be negative, by the way. 8) */
1984 seq_printf(seq
, "sockets: used %d\n", counter
);
1986 #endif /* CONFIG_PROC_FS */
1988 /* ABI emulation layers need these two */
1989 EXPORT_SYMBOL(move_addr_to_kernel
);
1990 EXPORT_SYMBOL(move_addr_to_user
);
1991 EXPORT_SYMBOL(sock_alloc
);
1992 EXPORT_SYMBOL(sock_alloc_inode
);
1993 EXPORT_SYMBOL(sock_create
);
1994 EXPORT_SYMBOL(sock_map_fd
);
1995 EXPORT_SYMBOL(sock_recvmsg
);
1996 EXPORT_SYMBOL(sock_register
);
1997 EXPORT_SYMBOL(sock_release
);
1998 EXPORT_SYMBOL(sock_sendmsg
);
1999 EXPORT_SYMBOL(sock_unregister
);
2000 EXPORT_SYMBOL(sock_wake_async
);
2001 EXPORT_SYMBOL(sockfd_lookup
);