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)
47 * This program is free software; you can redistribute it and/or
48 * modify it under the terms of the GNU General Public License
49 * as published by the Free Software Foundation; either version
50 * 2 of the License, or (at your option) any later version.
53 * This module is effectively the top level interface to the BSD socket
58 #include <linux/config.h>
60 #include <linux/smp_lock.h>
61 #include <linux/socket.h>
62 #include <linux/file.h>
63 #include <linux/net.h>
64 #include <linux/interrupt.h>
65 #include <linux/netdevice.h>
66 #include <linux/proc_fs.h>
67 #include <linux/firewall.h>
68 #include <linux/wanrouter.h>
69 #include <linux/init.h>
70 #include <linux/poll.h>
72 #if defined(CONFIG_KMOD) && defined(CONFIG_NET)
73 #include <linux/kmod.h>
76 #include <asm/uaccess.h>
78 #include <linux/inet.h>
86 static int sock_no_open(struct inode
*irrelevant
, struct file
*dontcare
);
87 static long long sock_lseek(struct file
*file
, long long offset
, int whence
);
88 static ssize_t
sock_read(struct file
*file
, char *buf
,
89 size_t size
, loff_t
*ppos
);
90 static ssize_t
sock_write(struct file
*file
, const char *buf
,
91 size_t size
, loff_t
*ppos
);
93 static int sock_close(struct inode
*inode
, struct file
*file
);
94 static unsigned int sock_poll(struct file
*file
,
95 struct poll_table_struct
*wait
);
96 static int sock_ioctl(struct inode
*inode
, struct file
*file
,
97 unsigned int cmd
, unsigned long arg
);
98 static int sock_fasync(int fd
, struct file
*filp
, int on
);
102 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
103 * in the operation structures but are done directly via the socketcall() multiplexor.
106 static struct file_operations socket_file_ops
= {
114 sock_no_open
, /* special open code to disallow open via /proc */
122 * The protocol list. Each protocol is registered in here.
125 struct net_proto_family
*net_families
[NPROTO
];
128 * Statistics counters of the socket lists
131 static int sockets_in_use
= 0;
134 * Support routines. Move socket addresses back and forth across the kernel/user
135 * divide and look after the messy bits.
138 #define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
139 16 for IP, 16 for IPX,
142 must be at least one bigger than
143 the AF_UNIX size (see net/unix/af_unix.c
147 int move_addr_to_kernel(void *uaddr
, int ulen
, void *kaddr
)
149 if(ulen
<0||ulen
>MAX_SOCK_ADDR
)
153 if(copy_from_user(kaddr
,uaddr
,ulen
))
158 int move_addr_to_user(void *kaddr
, int klen
, void *uaddr
, int *ulen
)
163 if((err
=get_user(len
, ulen
)))
167 if(len
<0 || len
> MAX_SOCK_ADDR
)
171 if(copy_to_user(uaddr
,kaddr
,len
))
175 * "fromlen shall refer to the value before truncation.."
178 return __put_user(klen
, ulen
);
182 * Obtains the first available file descriptor and sets it up for use.
185 static int get_fd(struct inode
*inode
)
190 * Find a file descriptor suitable for return to the user.
193 fd
= get_unused_fd();
195 struct file
*file
= get_empty_filp();
202 file
->f_dentry
= d_alloc_root(inode
, NULL
);
203 if (!file
->f_dentry
) {
210 * The socket maintains a reference to the inode, so we
211 * have to increment the count.
215 fd_install(fd
, file
);
216 file
->f_op
= &socket_file_ops
;
218 file
->f_flags
= O_RDWR
;
224 extern __inline__
struct socket
*socki_lookup(struct inode
*inode
)
226 return &inode
->u
.socket_i
;
230 * Go from a file number to its socket slot.
233 extern struct socket
*sockfd_lookup(int fd
, int *err
)
239 if (!(file
= fget(fd
)))
245 inode
= file
->f_dentry
->d_inode
;
246 if (!inode
|| !inode
->i_sock
|| !(sock
= socki_lookup(inode
)))
253 if (sock
->file
!= file
) {
254 printk(KERN_ERR
"socki_lookup: socket file changed!\n");
260 extern __inline__
void sockfd_put(struct socket
*sock
)
269 struct socket
*sock_alloc(void)
271 struct inode
* inode
;
272 struct socket
* sock
;
274 inode
= get_empty_inode();
278 sock
= socki_lookup(inode
);
280 inode
->i_mode
= S_IFSOCK
|S_IRWXUGO
;
282 inode
->i_uid
= current
->fsuid
;
283 inode
->i_gid
= current
->fsgid
;
286 init_waitqueue_head(&sock
->wait
);
287 sock
->fasync_list
= NULL
;
288 sock
->state
= SS_UNCONNECTED
;
299 * In theory you can't get an open on this inode, but /proc provides
300 * a back door. Remember to keep it shut otherwise you'll let the
301 * creepy crawlies in.
304 static int sock_no_open(struct inode
*irrelevant
, struct file
*dontcare
)
309 void sock_release(struct socket
*sock
)
311 if (sock
->state
!= SS_UNCONNECTED
)
312 sock
->state
= SS_DISCONNECTING
;
315 sock
->ops
->release(sock
, NULL
);
317 if (sock
->fasync_list
)
318 printk(KERN_ERR
"sock_release: fasync list not empty!\n");
320 --sockets_in_use
; /* Bookkeeping.. */
325 int sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
, int size
)
328 struct scm_cookie scm
;
330 err
= scm_send(sock
, msg
, &scm
);
332 err
= sock
->ops
->sendmsg(sock
, msg
, size
, &scm
);
338 int sock_recvmsg(struct socket
*sock
, struct msghdr
*msg
, int size
, int flags
)
340 struct scm_cookie scm
;
342 memset(&scm
, 0, sizeof(scm
));
344 size
= sock
->ops
->recvmsg(sock
, msg
, size
, flags
, &scm
);
346 scm_recv(sock
, msg
, &scm
, flags
);
353 * Sockets are not seekable.
356 static long long sock_lseek(struct file
*file
,long long offset
, int whence
)
362 * Read data from a socket. ubuf is a user mode pointer. We make sure the user
363 * area ubuf...ubuf+size-1 is writable before asking the protocol.
366 static ssize_t
sock_read(struct file
*file
, char *ubuf
,
367 size_t size
, loff_t
*ppos
)
373 if (ppos
!= &file
->f_pos
)
375 if (size
==0) /* Match SYS5 behaviour */
378 sock
= socki_lookup(file
->f_dentry
->d_inode
);
384 msg
.msg_control
=NULL
;
385 msg
.msg_controllen
=0;
389 return sock_recvmsg(sock
, &msg
, size
,
390 !(file
->f_flags
& O_NONBLOCK
) ? 0 : MSG_DONTWAIT
);
395 * Write data to a socket. We verify that the user area ubuf..ubuf+size-1
396 * is readable by the user process.
399 static ssize_t
sock_write(struct file
*file
, const char *ubuf
,
400 size_t size
, loff_t
*ppos
)
406 if (ppos
!= &file
->f_pos
)
408 if(size
==0) /* Match SYS5 behaviour */
411 sock
= socki_lookup(file
->f_dentry
->d_inode
);
417 msg
.msg_control
=NULL
;
418 msg
.msg_controllen
=0;
419 msg
.msg_flags
=!(file
->f_flags
& O_NONBLOCK
) ? 0 : MSG_DONTWAIT
;
420 iov
.iov_base
=(void *)ubuf
;
423 return sock_sendmsg(sock
, &msg
, size
);
426 int sock_readv_writev(int type
, struct inode
* inode
, struct file
* file
,
427 const struct iovec
* iov
, long count
, long size
)
432 sock
= socki_lookup(inode
);
436 msg
.msg_control
= NULL
;
437 msg
.msg_controllen
= 0;
438 msg
.msg_iov
= (struct iovec
*) iov
;
439 msg
.msg_iovlen
= count
;
440 msg
.msg_flags
= (file
->f_flags
& O_NONBLOCK
) ? MSG_DONTWAIT
: 0;
442 /* read() does a VERIFY_WRITE */
443 if (type
== VERIFY_WRITE
)
444 return sock_recvmsg(sock
, &msg
, size
, msg
.msg_flags
);
445 return sock_sendmsg(sock
, &msg
, size
);
450 * With an ioctl arg may well be a user mode pointer, but we don't know what to do
451 * with it - that's up to the protocol still.
454 int sock_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
457 struct socket
*sock
= socki_lookup(inode
);
458 return sock
->ops
->ioctl(sock
, cmd
, arg
);
462 static unsigned int sock_poll(struct file
*file
, poll_table
* wait
)
466 sock
= socki_lookup(file
->f_dentry
->d_inode
);
469 * We can't return errors to poll, so it's either yes or no.
472 return sock
->ops
->poll(file
, sock
, wait
);
476 int sock_close(struct inode
*inode
, struct file
*filp
)
479 * It was possible the inode is NULL we were
480 * closing an unfinished socket.
485 printk(KERN_DEBUG
"sock_close: NULL inode\n");
488 sock_fasync(-1, filp
, 0);
489 sock_release(socki_lookup(inode
));
494 * Update the socket async list
497 static int sock_fasync(int fd
, struct file
*filp
, int on
)
499 struct fasync_struct
*fa
, *fna
=NULL
, **prev
;
504 fna
=(struct fasync_struct
*)kmalloc(sizeof(struct fasync_struct
), GFP_KERNEL
);
509 sock
= socki_lookup(filp
->f_dentry
->d_inode
);
511 prev
=&(sock
->fasync_list
);
515 for (fa
=*prev
; fa
!=NULL
; prev
=&fa
->fa_next
,fa
=*prev
)
516 if (fa
->fa_file
==filp
)
524 kfree_s(fna
,sizeof(struct fasync_struct
));
525 release_sock(sock
->sk
);
530 fna
->magic
=FASYNC_MAGIC
;
531 fna
->fa_next
=sock
->fasync_list
;
532 sock
->fasync_list
=fna
;
539 kfree_s(fa
,sizeof(struct fasync_struct
));
543 release_sock(sock
->sk
);
547 int sock_wake_async(struct socket
*sock
, int how
)
549 if (!sock
|| !sock
->fasync_list
)
554 if (sock
->flags
& SO_WAITDATA
)
558 if (!(sock
->flags
& SO_NOSPACE
))
560 sock
->flags
&= ~SO_NOSPACE
;
564 kill_fasync(sock
->fasync_list
, SIGIO
);
571 int sock_create(int family
, int type
, int protocol
, struct socket
**res
)
577 * Check protocol is in range
579 if(family
<0||family
>=NPROTO
)
582 #if defined(CONFIG_KMOD) && defined(CONFIG_NET)
583 /* Attempt to load a protocol module if the find failed.
585 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
586 * requested real, full-featured networking support upon configuration.
587 * Otherwise module support will break!
589 if (net_families
[family
]==NULL
)
591 char module_name
[30];
592 sprintf(module_name
,"net-pf-%d",family
);
593 request_module(module_name
);
597 if (net_families
[family
]==NULL
)
601 * Check that this is a type that we know how to manipulate and
602 * the protocol makes sense here. The family can still reject the
606 if ((type
!= SOCK_STREAM
&& type
!= SOCK_DGRAM
&&
607 type
!= SOCK_SEQPACKET
&& type
!= SOCK_RAW
&& type
!= SOCK_RDM
&&
611 type
!= SOCK_PACKET
) || protocol
< 0)
615 * Allocate the socket and allow the family to set things up. if
616 * the protocol is 0, the family is instructed to select an appropriate
620 if (!(sock
= sock_alloc()))
622 printk(KERN_WARNING
"socket: no more sockets\n");
623 return -ENFILE
; /* Not exactly a match, but its the
624 closest posix thing */
629 if ((i
= net_families
[family
]->create(sock
, protocol
)) < 0)
639 asmlinkage
int sys_socket(int family
, int type
, int protocol
)
646 retval
= sock_create(family
, type
, protocol
, &sock
);
650 retval
= get_fd(sock
->inode
);
653 sock
->file
= fcheck(retval
);
665 * Create a pair of connected sockets.
668 asmlinkage
int sys_socketpair(int family
, int type
, int protocol
, int usockvec
[2])
670 struct socket
*sock1
, *sock2
;
676 * Obtain the first socket and check if the underlying protocol
677 * supports the socketpair call.
680 err
= sys_socket(family
, type
, protocol
);
686 * Now grab another socket
689 fd2
= sys_socket(family
, type
, protocol
);
694 * Get the sockets for the two fd's
696 sock1
= sockfd_lookup(fd1
, &err
);
699 sock2
= sockfd_lookup(fd2
, &err
);
703 /* try to connect the two sockets together */
704 err
= sock1
->ops
->socketpair(sock1
, sock2
);
708 err
= put_user(fd1
, &usockvec
[0]);
711 err
= put_user(fd2
, &usockvec
[1]);
731 * Bind a name to a socket. Nothing much to do here since it's
732 * the protocol's responsibility to handle the local address.
734 * We move the socket address to kernel space before we call
735 * the protocol layer (having also checked the address is ok).
738 asmlinkage
int sys_bind(int fd
, struct sockaddr
*umyaddr
, int addrlen
)
741 char address
[MAX_SOCK_ADDR
];
745 if((sock
= sockfd_lookup(fd
,&err
))!=NULL
)
747 if((err
=move_addr_to_kernel(umyaddr
,addrlen
,address
))>=0)
748 err
= sock
->ops
->bind(sock
, (struct sockaddr
*)address
, addrlen
);
757 * Perform a listen. Basically, we allow the protocol to do anything
758 * necessary for a listen, and if that works, we mark the socket as
759 * ready for listening.
762 asmlinkage
int sys_listen(int fd
, int backlog
)
768 if((sock
= sockfd_lookup(fd
, &err
))!=NULL
)
770 err
=sock
->ops
->listen(sock
, backlog
);
779 * For accept, we attempt to create a new socket, set up the link
780 * with the client, wake up the client, then return the new
781 * connected fd. We collect the address of the connector in kernel
782 * space and move it to user at the very end. This is unclean because
783 * we open the socket then return an error.
785 * 1003.1g adds the ability to recvmsg() to query connection pending
786 * status to recvmsg. We need to add that support in a way thats
787 * clean when we restucture accept also.
790 asmlinkage
int sys_accept(int fd
, struct sockaddr
*upeer_sockaddr
, int *upeer_addrlen
)
793 struct socket
*sock
, *newsock
;
795 char address
[MAX_SOCK_ADDR
];
798 sock
= sockfd_lookup(fd
, &err
);
804 if (!(newsock
= sock_alloc()))
807 inode
= newsock
->inode
;
808 newsock
->type
= sock
->type
;
810 err
= sock
->ops
->dup(newsock
, sock
);
814 err
= newsock
->ops
->accept(sock
, newsock
, sock
->file
->f_flags
);
817 newsock
= socki_lookup(inode
);
819 if ((err
= get_fd(inode
)) < 0)
821 newsock
->file
= fcheck(err
);
825 /* Handle the race where the accept works and we
826 then getname after it has closed again */
827 if(newsock
->ops
->getname(newsock
, (struct sockaddr
*)address
, &len
, 1)<0)
832 /* N.B. Should check for errors here */
833 move_addr_to_user(address
, len
, upeer_sockaddr
, upeer_addrlen
);
843 sock_release(newsock
);
849 * Attempt to connect to a socket with the server address. The address
850 * is in user space so we verify it is OK and move it to kernel space.
852 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
855 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
856 * other SEQPACKET protocols that take time to connect() as it doesn't
857 * include the -EINPROGRESS status for such sockets.
860 asmlinkage
int sys_connect(int fd
, struct sockaddr
*uservaddr
, int addrlen
)
863 char address
[MAX_SOCK_ADDR
];
867 sock
= sockfd_lookup(fd
, &err
);
870 err
= move_addr_to_kernel(uservaddr
, addrlen
, address
);
873 err
= sock
->ops
->connect(sock
, (struct sockaddr
*) address
, addrlen
,
874 sock
->file
->f_flags
);
883 * Get the local address ('name') of a socket object. Move the obtained
884 * name to user space.
887 asmlinkage
int sys_getsockname(int fd
, struct sockaddr
*usockaddr
, int *usockaddr_len
)
890 char address
[MAX_SOCK_ADDR
];
894 sock
= sockfd_lookup(fd
, &err
);
897 err
= sock
->ops
->getname(sock
, (struct sockaddr
*)address
, &len
, 0);
900 err
= move_addr_to_user(address
, len
, usockaddr
, usockaddr_len
);
910 * Get the remote address ('name') of a socket object. Move the obtained
911 * name to user space.
914 asmlinkage
int sys_getpeername(int fd
, struct sockaddr
*usockaddr
, int *usockaddr_len
)
917 char address
[MAX_SOCK_ADDR
];
921 if ((sock
= sockfd_lookup(fd
, &err
))!=NULL
)
923 err
= sock
->ops
->getname(sock
, (struct sockaddr
*)address
, &len
, 1);
925 err
=move_addr_to_user(address
,len
, usockaddr
, usockaddr_len
);
933 * Send a datagram to a given address. We move the address into kernel
934 * space and check the user space data area is readable before invoking
938 asmlinkage
int sys_sendto(int fd
, void * buff
, size_t len
, unsigned flags
,
939 struct sockaddr
*addr
, int addr_len
)
942 char address
[MAX_SOCK_ADDR
];
948 sock
= sockfd_lookup(fd
, &err
);
956 msg
.msg_control
=NULL
;
957 msg
.msg_controllen
=0;
958 msg
.msg_namelen
=addr_len
;
961 err
= move_addr_to_kernel(addr
, addr_len
, address
);
964 msg
.msg_name
=address
;
966 if (sock
->file
->f_flags
& O_NONBLOCK
)
967 flags
|= MSG_DONTWAIT
;
968 msg
.msg_flags
= flags
;
969 err
= sock_sendmsg(sock
, &msg
, len
);
979 * Send a datagram down a socket.
982 asmlinkage
int sys_send(int fd
, void * buff
, size_t len
, unsigned flags
)
984 return sys_sendto(fd
, buff
, len
, flags
, NULL
, 0);
988 * Receive a frame from the socket and optionally record the address of the
989 * sender. We verify the buffers are writable and if needed move the
990 * sender address from kernel to user space.
993 asmlinkage
int sys_recvfrom(int fd
, void * ubuf
, size_t size
, unsigned flags
,
994 struct sockaddr
*addr
, int *addr_len
)
999 char address
[MAX_SOCK_ADDR
];
1003 sock
= sockfd_lookup(fd
, &err
);
1007 msg
.msg_control
=NULL
;
1008 msg
.msg_controllen
=0;
1013 msg
.msg_name
=address
;
1014 msg
.msg_namelen
=MAX_SOCK_ADDR
;
1015 if (sock
->file
->f_flags
& O_NONBLOCK
)
1016 flags
|= MSG_DONTWAIT
;
1017 err
=sock_recvmsg(sock
, &msg
, size
, flags
);
1019 if(err
>= 0 && addr
!= NULL
)
1021 err2
=move_addr_to_user(address
, msg
.msg_namelen
, addr
, addr_len
);
1032 * Receive a datagram from a socket.
1035 asmlinkage
int sys_recv(int fd
, void * ubuf
, size_t size
, unsigned flags
)
1037 return sys_recvfrom(fd
, ubuf
, size
, flags
, NULL
, NULL
);
1041 * Set a socket option. Because we don't know the option lengths we have
1042 * to pass the user mode parameter for the protocols to sort out.
1045 asmlinkage
int sys_setsockopt(int fd
, int level
, int optname
, char *optval
, int optlen
)
1048 struct socket
*sock
;
1051 if ((sock
= sockfd_lookup(fd
, &err
))!=NULL
)
1053 if (level
== SOL_SOCKET
)
1054 err
=sock_setsockopt(sock
,level
,optname
,optval
,optlen
);
1056 err
=sock
->ops
->setsockopt(sock
, level
, optname
, optval
, optlen
);
1064 * Get a socket option. Because we don't know the option lengths we have
1065 * to pass a user mode parameter for the protocols to sort out.
1068 asmlinkage
int sys_getsockopt(int fd
, int level
, int optname
, char *optval
, int *optlen
)
1071 struct socket
*sock
;
1074 if ((sock
= sockfd_lookup(fd
, &err
))!=NULL
)
1076 if (level
== SOL_SOCKET
)
1077 err
=sock_getsockopt(sock
,level
,optname
,optval
,optlen
);
1079 err
=sock
->ops
->getsockopt(sock
, level
, optname
, optval
, optlen
);
1088 * Shutdown a socket.
1091 asmlinkage
int sys_shutdown(int fd
, int how
)
1094 struct socket
*sock
;
1097 if ((sock
= sockfd_lookup(fd
, &err
))!=NULL
)
1099 err
=sock
->ops
->shutdown(sock
, how
);
1107 * BSD sendmsg interface
1110 asmlinkage
int sys_sendmsg(int fd
, struct msghdr
*msg
, unsigned flags
)
1112 struct socket
*sock
;
1113 char address
[MAX_SOCK_ADDR
];
1114 struct iovec iovstack
[UIO_FASTIOV
], *iov
= iovstack
;
1115 unsigned char ctl
[sizeof(struct cmsghdr
) + 20]; /* 20 is size of ipv6_pktinfo */
1116 unsigned char *ctl_buf
= ctl
;
1117 struct msghdr msg_sys
;
1118 int err
, ctl_len
, iov_size
, total_len
;
1123 if (copy_from_user(&msg_sys
,msg
,sizeof(struct msghdr
)))
1126 sock
= sockfd_lookup(fd
, &err
);
1130 /* do not move before msg_sys is valid */
1132 if (msg_sys
.msg_iovlen
> UIO_MAXIOV
)
1135 /* Check whether to allocate the iovec area*/
1137 iov_size
= msg_sys
.msg_iovlen
* sizeof(struct iovec
);
1138 if (msg_sys
.msg_iovlen
> UIO_FASTIOV
) {
1139 iov
= sock_kmalloc(sock
->sk
, iov_size
, GFP_KERNEL
);
1144 /* This will also move the address data into kernel space */
1145 err
= verify_iovec(&msg_sys
, iov
, address
, VERIFY_READ
);
1152 /* msg_controllen must fit to int */
1153 if (msg_sys
.msg_controllen
> INT_MAX
)
1155 ctl_len
= msg_sys
.msg_controllen
;
1158 if (ctl_len
> sizeof(ctl
))
1160 /* Suggested by the Advanced Sockets API for IPv6 draft:
1161 * Limit the msg_controllen size by the SO_SNDBUF size.
1163 /* Note - when this code becomes multithreaded on
1164 * SMP machines you have a race to fix here.
1167 ctl_buf
= sock_kmalloc(sock
->sk
, ctl_len
, GFP_KERNEL
);
1168 if (ctl_buf
== NULL
)
1172 if (copy_from_user(ctl_buf
, msg_sys
.msg_control
, ctl_len
))
1174 msg_sys
.msg_control
= ctl_buf
;
1176 msg_sys
.msg_flags
= flags
;
1178 if (sock
->file
->f_flags
& O_NONBLOCK
)
1179 msg_sys
.msg_flags
|= MSG_DONTWAIT
;
1180 err
= sock_sendmsg(sock
, &msg_sys
, total_len
);
1184 sock_kfree_s(sock
->sk
, ctl_buf
, ctl_len
);
1186 if (iov
!= iovstack
)
1187 sock_kfree_s(sock
->sk
, iov
, iov_size
);
1196 * BSD recvmsg interface
1199 asmlinkage
int sys_recvmsg(int fd
, struct msghdr
*msg
, unsigned int flags
)
1201 struct socket
*sock
;
1202 struct iovec iovstack
[UIO_FASTIOV
];
1203 struct iovec
*iov
=iovstack
;
1204 struct msghdr msg_sys
;
1205 unsigned long cmsg_ptr
;
1206 int err
, iov_size
, total_len
, len
;
1208 /* kernel mode address */
1209 char addr
[MAX_SOCK_ADDR
];
1211 /* user mode address pointers */
1212 struct sockaddr
*uaddr
;
1217 if (copy_from_user(&msg_sys
,msg
,sizeof(struct msghdr
)))
1220 sock
= sockfd_lookup(fd
, &err
);
1225 if (msg_sys
.msg_iovlen
> UIO_MAXIOV
)
1228 /* Check whether to allocate the iovec area*/
1230 iov_size
= msg_sys
.msg_iovlen
* sizeof(struct iovec
);
1231 if (msg_sys
.msg_iovlen
> UIO_FASTIOV
) {
1232 iov
= sock_kmalloc(sock
->sk
, iov_size
, GFP_KERNEL
);
1238 * Save the user-mode address (verify_iovec will change the
1239 * kernel msghdr to use the kernel address space)
1242 uaddr
= msg_sys
.msg_name
;
1243 uaddr_len
= &msg
->msg_namelen
;
1244 err
= verify_iovec(&msg_sys
, iov
, addr
, VERIFY_WRITE
);
1249 cmsg_ptr
= (unsigned long)msg_sys
.msg_control
;
1250 msg_sys
.msg_flags
= 0;
1252 if (sock
->file
->f_flags
& O_NONBLOCK
)
1253 flags
|= MSG_DONTWAIT
;
1254 err
= sock_recvmsg(sock
, &msg_sys
, total_len
, flags
);
1259 if (uaddr
!= NULL
) {
1260 err
= move_addr_to_user(addr
, msg_sys
.msg_namelen
, uaddr
, uaddr_len
);
1264 err
= __put_user(msg_sys
.msg_flags
, &msg
->msg_flags
);
1267 err
= __put_user((unsigned long)msg_sys
.msg_control
-cmsg_ptr
,
1268 &msg
->msg_controllen
);
1274 if (iov
!= iovstack
)
1275 sock_kfree_s(sock
->sk
, iov
, iov_size
);
1285 * Perform a file control on a socket file descriptor.
1287 * Doesn't aquire a fd lock, because no network fcntl
1288 * function sleeps currently.
1291 int sock_fcntl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
1293 struct socket
*sock
;
1295 sock
= socki_lookup (filp
->f_dentry
->d_inode
);
1296 if (sock
&& sock
->ops
)
1297 return sock
->ops
->fcntl(sock
, cmd
, arg
);
1301 /* Argument list sizes for sys_socketcall */
1302 #define AL(x) ((x) * sizeof(unsigned long))
1303 static unsigned char nargs
[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
1304 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
1305 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)};
1309 * System call vectors.
1311 * Argument checking cleaned up. Saved 20% in size.
1312 * This function doesn't need to set the kernel lock because
1313 * it is set by the callees.
1316 asmlinkage
int sys_socketcall(int call
, unsigned long *args
)
1319 unsigned long a0
,a1
;
1322 if(call
<1||call
>SYS_RECVMSG
)
1325 /* copy_from_user should be SMP safe. */
1326 if (copy_from_user(a
, args
, nargs
[call
]))
1335 err
= sys_socket(a0
,a1
,a
[2]);
1338 err
= sys_bind(a0
,(struct sockaddr
*)a1
, a
[2]);
1341 err
= sys_connect(a0
, (struct sockaddr
*)a1
, a
[2]);
1344 err
= sys_listen(a0
,a1
);
1347 err
= sys_accept(a0
,(struct sockaddr
*)a1
, (int *)a
[2]);
1349 case SYS_GETSOCKNAME
:
1350 err
= sys_getsockname(a0
,(struct sockaddr
*)a1
, (int *)a
[2]);
1352 case SYS_GETPEERNAME
:
1353 err
= sys_getpeername(a0
, (struct sockaddr
*)a1
, (int *)a
[2]);
1355 case SYS_SOCKETPAIR
:
1356 err
= sys_socketpair(a0
,a1
, a
[2], (int *)a
[3]);
1359 err
= sys_send(a0
, (void *)a1
, a
[2], a
[3]);
1362 err
= sys_sendto(a0
,(void *)a1
, a
[2], a
[3],
1363 (struct sockaddr
*)a
[4], a
[5]);
1366 err
= sys_recv(a0
, (void *)a1
, a
[2], a
[3]);
1369 err
= sys_recvfrom(a0
, (void *)a1
, a
[2], a
[3],
1370 (struct sockaddr
*)a
[4], (int *)a
[5]);
1373 err
= sys_shutdown(a0
,a1
);
1375 case SYS_SETSOCKOPT
:
1376 err
= sys_setsockopt(a0
, a1
, a
[2], (char *)a
[3], a
[4]);
1378 case SYS_GETSOCKOPT
:
1379 err
= sys_getsockopt(a0
, a1
, a
[2], (char *)a
[3], (int *)a
[4]);
1382 err
= sys_sendmsg(a0
, (struct msghdr
*) a1
, a
[2]);
1385 err
= sys_recvmsg(a0
, (struct msghdr
*) a1
, a
[2]);
1395 * This function is called by a protocol handler that wants to
1396 * advertise its address family, and have it linked into the
1400 int sock_register(struct net_proto_family
*ops
)
1402 if (ops
->family
>= NPROTO
) {
1403 printk(KERN_CRIT
"protocol %d >= NPROTO(%d)\n", ops
->family
, NPROTO
);
1406 net_families
[ops
->family
]=ops
;
1411 * This function is called by a protocol handler that wants to
1412 * remove its address family, and have it unlinked from the
1416 int sock_unregister(int family
)
1418 if (family
< 0 || family
>= NPROTO
)
1421 net_families
[family
]=NULL
;
1425 void __init
proto_init(void)
1427 extern struct net_proto protocols
[]; /* Network protocols */
1428 struct net_proto
*pro
;
1430 /* Kick all configured protocols. */
1432 while (pro
->name
!= NULL
)
1434 (*pro
->init_func
)(pro
);
1437 /* We're all done... */
1440 extern void sk_init(void);
1441 #ifdef CONFIG_WAN_ROUTER
1442 extern void wanrouter_init(void);
1445 void __init
sock_init(void)
1449 printk(KERN_INFO
"Linux NET4.0 for Linux 2.2\n");
1450 printk(KERN_INFO
"Based upon Swansea University Computer Society NET3.039\n");
1453 * Initialize all address (protocol) families.
1456 for (i
= 0; i
< NPROTO
; i
++)
1457 net_families
[i
] = NULL
;
1460 * Initialize sock SLAB cache.
1467 * Initialize skbuff SLAB cache
1477 #ifdef CONFIG_WAN_ROUTER
1482 * Attach the firewall module if configured
1485 #ifdef CONFIG_FIREWALL
1490 * Initialize the protocols module.
1496 * The netlink device handler may be needed early.
1499 #ifdef CONFIG_RTNETLINK
1502 #ifdef CONFIG_NETLINK_DEV
1507 int socket_get_info(char *buffer
, char **start
, off_t offset
, int length
)
1509 int len
= sprintf(buffer
, "sockets: used %d\n", sockets_in_use
);
1515 *start
= buffer
+ offset
;