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/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>
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
,
123 .aio_read
= sock_aio_read
,
124 .aio_write
= sock_aio_write
,
128 .open
= sock_no_open
, /* special open code to disallow open via /proc */
129 .release
= sock_close
,
130 .fasync
= sock_fasync
,
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
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
);
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
);
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)
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,
202 must be at least one bigger than
203 the AF_UNIX size (see net/unix/af_unix.c
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
)
224 if(copy_from_user(kaddr
,uaddr
,ulen
))
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
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
)
251 if((err
=get_user(len
, ulen
)))
255 if(len
<0 || len
> MAX_SOCK_ADDR
)
259 if(copy_to_user(uaddr
,kaddr
,len
))
263 * "fromlen shall refer to the value before truncation.."
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
);
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
,
313 if (sock_inode_cachep
== NULL
)
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
= {
334 .get_sb
= sockfs_get_sb
,
335 .kill_sb
= kill_anon_super
,
337 static int sockfs_delete_dentry(struct dentry
*dentry
)
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
)
369 * Find a file descriptor suitable for return to the user.
372 fd
= get_unused_fd();
374 struct file
*file
= get_empty_filp();
382 sprintf(name
, "[%lu]", SOCK_INODE(sock
)->i_ino
);
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
) {
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
);
399 file
->f_op
= SOCK_INODE(sock
)->i_fop
= &socket_file_ops
;
401 file
->f_flags
= O_RDWR
;
403 fd_install(fd
, file
);
411 * sockfd_lookup - Go from a file number to its socket slot
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
)
429 if (!(file
= fget(fd
)))
435 inode
= file
->f_dentry
->d_inode
;
436 if (!inode
->i_sock
|| !(sock
= SOCKET_I(inode
)))
443 if (sock
->file
!= file
) {
444 printk(KERN_ERR
"socki_lookup: socket file changed!\n");
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
458 struct socket
*sock_alloc(void)
460 struct inode
* inode
;
461 struct socket
* sock
;
463 inode
= new_inode(sock_mnt
->mnt_sb
);
467 sock
= SOCKET_I(inode
);
469 inode
->i_mode
= S_IFSOCK
|S_IRWXUGO
;
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
);
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
)
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
)
507 struct module
*owner
= sock
->ops
->owner
;
509 sock
->ops
->release(sock
);
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
);
520 iput(SOCK_INODE(sock
));
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
);
536 err
= security_socket_sendmsg(sock
, msg
, size
);
540 return sock
->ops
->sendmsg(iocb
, sock
, msg
, size
);
543 int sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
, int size
)
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
);
556 static inline int __sock_recvmsg(struct kiocb
*iocb
, struct socket
*sock
, struct msghdr
*msg
, int size
, int flags
)
559 struct sock_iocb
*si
= kiocb_to_siocb(iocb
);
567 err
= security_socket_recvmsg(sock
, msg
, size
, flags
);
571 return sock
->ops
->recvmsg(iocb
, sock
, msg
, size
, flags
);
574 int sock_recvmsg(struct socket
*sock
, struct msghdr
*msg
, int size
, int flags
)
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
);
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
);
600 if (size
==0) /* Match SYS5 behaviour */
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
);
632 if(size
==0) /* Match SYS5 behaviour */
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
)
658 if (ppos
!= &file
->f_pos
)
661 sock
= SOCKET_I(file
->f_dentry
->d_inode
);
663 flags
= !(file
->f_flags
& O_NONBLOCK
) ? 0 : MSG_DONTWAIT
;
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
)
676 sock
= SOCKET_I(inode
);
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
)
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
)
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
;
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
,
769 sock
= SOCKET_I(inode
);
770 if (cmd
>= SIOCDEVPRIVATE
&& cmd
<= (SIOCDEVPRIVATE
+ 15)) {
771 err
= dev_ioctl(cmd
, (void *)arg
);
774 if (cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
) {
775 err
= dev_ioctl(cmd
, (void *)arg
);
777 #endif /* WIRELESS_EXT */
782 if (get_user(pid
, (int *)arg
))
784 err
= f_setown(sock
->file
, pid
, 1);
788 err
= put_user(sock
->file
->f_owner
.pid
, (int *)arg
);
794 request_module("bridge");
796 down(&br_ioctl_mutex
);
798 err
= br_ioctl_hook(arg
);
804 if (!vlan_ioctl_hook
)
805 request_module("8021q");
807 down(&vlan_ioctl_mutex
);
809 err
= vlan_ioctl_hook(arg
);
810 up(&vlan_ioctl_mutex
);
814 /* Convert this to call through a hook */
815 err
= divert_ioctl(cmd
, (struct divert_cf
*)arg
);
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
);
830 err
= sock
->ops
->ioctl(sock
, cmd
, arg
);
839 /* No kernel lock held - perfect */
840 static unsigned int sock_poll(struct file
*file
, poll_table
* wait
)
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.
867 printk(KERN_DEBUG
"sock_close: NULL inode\n");
870 sock_fasync(-1, filp
, 0);
871 sock_release(SOCKET_I(inode
));
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).
890 static int sock_fasync(int fd
, struct file
*filp
, int on
)
892 struct fasync_struct
*fa
, *fna
=NULL
, **prev
;
898 fna
=(struct fasync_struct
*)kmalloc(sizeof(struct fasync_struct
), GFP_KERNEL
);
903 sock
= SOCKET_I(filp
->f_dentry
->d_inode
);
905 if ((sk
=sock
->sk
) == NULL
) {
913 prev
=&(sock
->fasync_list
);
915 for (fa
=*prev
; fa
!=NULL
; prev
=&fa
->fa_next
,fa
=*prev
)
916 if (fa
->fa_file
==filp
)
923 write_lock_bh(&sk
->sk_callback_lock
);
925 write_unlock_bh(&sk
->sk_callback_lock
);
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
);
942 write_lock_bh(&sk
->sk_callback_lock
);
944 write_unlock_bh(&sk
->sk_callback_lock
);
950 release_sock(sock
->sk
);
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
)
964 if (test_bit(SOCK_ASYNC_WAITDATA
, &sock
->flags
))
968 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE
, &sock
->flags
))
973 __kill_fasync(sock
->fasync_list
, SIGIO
, band
);
976 __kill_fasync(sock
->fasync_list
, SIGURG
, band
);
982 int sock_create(int family
, int type
, int protocol
, struct socket
**res
)
989 * Check protocol is in range
991 if (family
< 0 || family
>= NPROTO
)
992 return -EAFNOSUPPORT
;
993 if (type
< 0 || type
>= SOCK_MAX
)
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
) {
1005 printk(KERN_INFO
"%s uses obsolete (PF_INET,SOCK_PACKET)\n", current
->comm
);
1010 err
= security_socket_create(family
, type
, protocol
);
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
);
1027 net_family_read_lock();
1028 if (net_families
[family
] == NULL
) {
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
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 */
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.
1054 if (!try_module_get(net_families
[family
]->owner
))
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
)) {
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
);
1073 security_socket_post_create(sock
, family
, type
, protocol
);
1076 net_family_read_unlock();
1079 module_put(net_families
[family
]->owner
);
1085 asmlinkage
long sys_socket(int family
, int type
, int protocol
)
1088 struct socket
*sock
;
1090 retval
= sock_create(family
, type
, protocol
, &sock
);
1094 retval
= sock_map_fd(sock
);
1099 /* It may be already another descriptor 8) Not kernel problem. */
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
;
1117 * Obtain the first socket and check if the underlying protocol
1118 * supports the socketpair call.
1121 err
= sock_create(family
, type
, protocol
, &sock1
);
1125 err
= sock_create(family
, type
, protocol
, &sock2
);
1129 err
= sock1
->ops
->socketpair(sock1
, sock2
);
1131 goto out_release_both
;
1135 err
= sock_map_fd(sock1
);
1137 goto out_release_both
;
1140 err
= sock_map_fd(sock2
);
1145 /* fd1 and fd2 may be already another descriptors.
1146 * Not kernel problem.
1149 err
= put_user(fd1
, &usockvec
[0]);
1151 err
= put_user(fd2
, &usockvec
[1]);
1160 sock_release(sock2
);
1165 sock_release(sock2
);
1167 sock_release(sock1
);
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
];
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
);
1195 err
= sock
->ops
->bind(sock
, (struct sockaddr
*)address
, addrlen
);
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
;
1214 if ((sock
= sockfd_lookup(fd
, &err
)) != NULL
) {
1215 if ((unsigned) backlog
> SOMAXCONN
)
1216 backlog
= SOMAXCONN
;
1218 err
= security_socket_listen(sock
, backlog
);
1224 err
=sock
->ops
->listen(sock
, backlog
);
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
;
1247 char address
[MAX_SOCK_ADDR
];
1249 sock
= sockfd_lookup(fd
, &err
);
1254 if (!(newsock
= sock_alloc()))
1257 newsock
->type
= sock
->type
;
1258 newsock
->ops
= sock
->ops
;
1260 err
= security_socket_accept(sock
, newsock
);
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
);
1274 if (upeer_sockaddr
) {
1275 if(newsock
->ops
->getname(newsock
, (struct sockaddr
*)address
, &len
, 2)<0) {
1276 err
= -ECONNABORTED
;
1279 err
= move_addr_to_user(address
, len
, upeer_sockaddr
, upeer_addrlen
);
1284 /* File flags are not inherited via accept() unlike another OSes. */
1286 if ((err
= sock_map_fd(newsock
)) < 0)
1289 security_socket_post_accept(sock
, newsock
);
1296 sock_release(newsock
);
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
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
];
1319 sock
= sockfd_lookup(fd
, &err
);
1322 err
= move_addr_to_kernel(uservaddr
, addrlen
, address
);
1326 err
= security_socket_connect(sock
, (struct sockaddr
*)address
, addrlen
);
1330 err
= sock
->ops
->connect(sock
, (struct sockaddr
*) address
, addrlen
,
1331 sock
->file
->f_flags
);
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
];
1349 sock
= sockfd_lookup(fd
, &err
);
1353 err
= security_socket_getsockname(sock
);
1357 err
= sock
->ops
->getname(sock
, (struct sockaddr
*)address
, &len
, 0);
1360 err
= move_addr_to_user(address
, len
, usockaddr
, usockaddr_len
);
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
];
1379 if ((sock
= sockfd_lookup(fd
, &err
))!=NULL
)
1381 err
= security_socket_getpeername(sock
);
1387 err
= sock
->ops
->getname(sock
, (struct sockaddr
*)address
, &len
, 1);
1389 err
=move_addr_to_user(address
,len
, usockaddr
, usockaddr_len
);
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
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
];
1410 sock
= sockfd_lookup(fd
, &err
);
1418 msg
.msg_control
=NULL
;
1419 msg
.msg_controllen
=0;
1423 err
= move_addr_to_kernel(addr
, addr_len
, address
);
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
);
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
;
1461 char address
[MAX_SOCK_ADDR
];
1464 sock
= sockfd_lookup(fd
, &err
);
1468 msg
.msg_control
=NULL
;
1469 msg
.msg_controllen
=0;
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
);
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
)
1508 struct socket
*sock
;
1513 if ((sock
= sockfd_lookup(fd
, &err
))!=NULL
)
1515 err
= security_socket_setsockopt(sock
,level
,optname
);
1521 if (level
== SOL_SOCKET
)
1522 err
=sock_setsockopt(sock
,level
,optname
,optval
,optlen
);
1524 err
=sock
->ops
->setsockopt(sock
, level
, optname
, optval
, optlen
);
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
)
1538 struct socket
*sock
;
1540 if ((sock
= sockfd_lookup(fd
, &err
))!=NULL
)
1542 err
= security_socket_getsockopt(sock
, level
,
1549 if (level
== SOL_SOCKET
)
1550 err
=sock_getsockopt(sock
,level
,optname
,optval
,optlen
);
1552 err
=sock
->ops
->getsockopt(sock
, level
, optname
, optval
, optlen
);
1560 * Shutdown a socket.
1563 asmlinkage
long sys_shutdown(int fd
, int how
)
1566 struct socket
*sock
;
1568 if ((sock
= sockfd_lookup(fd
, &err
))!=NULL
)
1570 err
= security_socket_shutdown(sock
, how
);
1576 err
=sock
->ops
->shutdown(sock
, how
);
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
;
1606 if (MSG_CMSG_COMPAT
& flags
) {
1607 if (get_compat_msghdr(&msg_sys
, msg_compat
))
1609 } else if (copy_from_user(&msg_sys
, msg
, sizeof(struct msghdr
)))
1612 sock
= sockfd_lookup(fd
, &err
);
1616 /* do not move before msg_sys is valid */
1618 if (msg_sys
.msg_iovlen
> UIO_MAXIOV
)
1621 /* Check whether to allocate the iovec area*/
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
);
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
);
1634 err
= verify_iovec(&msg_sys
, iov
, address
, VERIFY_READ
);
1641 if (msg_sys
.msg_controllen
> INT_MAX
)
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
));
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
)
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
))
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
);
1674 sock_kfree_s(sock
->sk
, ctl_buf
, ctl_len
);
1676 if (iov
!= iovstack
)
1677 sock_kfree_s(sock
->sk
, iov
, iov_size
);
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
))
1709 if (copy_from_user(&msg_sys
,msg
,sizeof(struct msghdr
)))
1712 sock
= sockfd_lookup(fd
, &err
);
1717 if (msg_sys
.msg_iovlen
> UIO_MAXIOV
)
1720 /* Check whether to allocate the iovec area*/
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
);
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
);
1739 err
= verify_iovec(&msg_sys
, iov
, addr
, VERIFY_WRITE
);
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
);
1756 if (uaddr
!= NULL
) {
1757 err
= move_addr_to_user(addr
, msg_sys
.msg_namelen
, uaddr
, uaddr_len
);
1761 err
= __put_user(msg_sys
.msg_flags
, COMPAT_FLAGS(msg
));
1764 if (MSG_CMSG_COMPAT
& flags
)
1765 err
= __put_user((unsigned long)msg_sys
.msg_control
-cmsg_ptr
,
1766 &msg_compat
->msg_controllen
);
1768 err
= __put_user((unsigned long)msg_sys
.msg_control
-cmsg_ptr
,
1769 &msg
->msg_controllen
);
1775 if (iov
!= iovstack
)
1776 sock_kfree_s(sock
->sk
, iov
, iov_size
);
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)};
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
)
1801 unsigned long a0
,a1
;
1804 if(call
<1||call
>SYS_RECVMSG
)
1807 /* copy_from_user should be SMP safe. */
1808 if (copy_from_user(a
, args
, nargs
[call
]))
1817 err
= sys_socket(a0
,a1
,a
[2]);
1820 err
= sys_bind(a0
,(struct sockaddr __user
*)a1
, a
[2]);
1823 err
= sys_connect(a0
, (struct sockaddr __user
*)a1
, a
[2]);
1826 err
= sys_listen(a0
,a1
);
1829 err
= sys_accept(a0
,(struct sockaddr __user
*)a1
, (int __user
*)a
[2]);
1831 case SYS_GETSOCKNAME
:
1832 err
= sys_getsockname(a0
,(struct sockaddr __user
*)a1
, (int __user
*)a
[2]);
1834 case SYS_GETPEERNAME
:
1835 err
= sys_getpeername(a0
, (struct sockaddr __user
*)a1
, (int __user
*)a
[2]);
1837 case SYS_SOCKETPAIR
:
1838 err
= sys_socketpair(a0
,a1
, a
[2], (int __user
*)a
[3]);
1841 err
= sys_send(a0
, (void __user
*)a1
, a
[2], a
[3]);
1844 err
= sys_sendto(a0
,(void __user
*)a1
, a
[2], a
[3],
1845 (struct sockaddr __user
*)a
[4], a
[5]);
1848 err
= sys_recv(a0
, (void __user
*)a1
, a
[2], a
[3]);
1851 err
= sys_recvfrom(a0
, (void __user
*)a1
, a
[2], a
[3],
1852 (struct sockaddr __user
*)a
[4], (int __user
*)a
[5]);
1855 err
= sys_shutdown(a0
,a1
);
1857 case SYS_SETSOCKOPT
:
1858 err
= sys_setsockopt(a0
, a1
, a
[2], (char __user
*)a
[3], a
[4]);
1860 case SYS_GETSOCKOPT
:
1861 err
= sys_getsockopt(a0
, a1
, a
[2], (char __user
*)a
[3], (int __user
*)a
[4]);
1864 err
= sys_sendmsg(a0
, (struct msghdr __user
*) a1
, a
[2]);
1867 err
= sys_recvmsg(a0
, (struct msghdr __user
*) a1
, a
[2]);
1877 * This function is called by a protocol handler that wants to
1878 * advertise its address family, and have it linked into the
1882 int sock_register(struct net_proto_family
*ops
)
1886 if (ops
->family
>= NPROTO
) {
1887 printk(KERN_CRIT
"protocol %d >= NPROTO(%d)\n", ops
->family
, NPROTO
);
1890 net_family_write_lock();
1892 if (net_families
[ops
->family
] == NULL
) {
1893 net_families
[ops
->family
]=ops
;
1896 net_family_write_unlock();
1901 * This function is called by a protocol handler that wants to
1902 * remove its address family, and have it unlinked from the
1906 int sock_unregister(int family
)
1908 if (family
< 0 || family
>= NPROTO
)
1911 net_family_write_lock();
1912 net_families
[family
]=NULL
;
1913 net_family_write_unlock();
1918 extern void sk_init(void);
1920 #ifdef CONFIG_WAN_ROUTER
1921 extern void wanrouter_init(void);
1924 void __init
sock_init(void)
1929 * Initialize all address (protocol) families.
1932 for (i
= 0; i
< NPROTO
; i
++)
1933 net_families
[i
] = NULL
;
1936 * Initialize sock SLAB cache.
1943 * Initialize skbuff SLAB cache
1952 #ifdef CONFIG_WAN_ROUTER
1957 * Initialize the protocols module.
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
1972 #ifdef CONFIG_PROC_FS
1973 void socket_seq_show(struct seq_file
*seq
)
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) */
1985 seq_printf(seq
, "sockets: used %d\n", counter
);
1987 #endif /* CONFIG_PROC_FS */