Import 2.3.4
[davej-history.git] / net / socket.c
blob8402e0480d19071a1408a71d68ca8230de376b35
1 /*
2 * NET An implementation of the SOCKET network access protocol.
4 * Version: @(#)socket.c 1.1.93 18/02/95
6 * Authors: Orest Zborowski, <obz@Kodak.COM>
7 * Ross Biro, <bir7@leland.Stanford.Edu>
8 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Fixes:
11 * Anonymous : NOTSOCK/BADF cleanup. Error fix in
12 * shutdown()
13 * Alan Cox : verify_area() fixes
14 * Alan Cox : Removed DDI
15 * Jonathan Kamens : SOCK_DGRAM reconnect bug
16 * Alan Cox : Moved a load of checks to the very
17 * top level.
18 * Alan Cox : Move address structures to/from user
19 * mode above the protocol layers.
20 * Rob Janssen : Allow 0 length sends.
21 * Alan Cox : Asynchronous I/O support (cribbed from the
22 * tty drivers).
23 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
24 * Jeff Uphoff : Made max number of sockets command-line
25 * configurable.
26 * Matti Aarnio : Made the number of sockets dynamic,
27 * to be allocated when needed, and mr.
28 * Uphoff's max is used as max to be
29 * allowed to allocate.
30 * Linus : Argh. removed all the socket allocation
31 * altogether: it's in the inode now.
32 * Alan Cox : Made sock_alloc()/sock_release() public
33 * for NetROM and future kernel nfsd type
34 * stuff.
35 * Alan Cox : sendmsg/recvmsg basics.
36 * Tom Dyas : Export net symbols.
37 * Marcin Dalecki : Fixed problems with CONFIG_NET="n".
38 * Alan Cox : Added thread locking to sys_* calls
39 * for sockets. May have errors at the
40 * moment.
41 * Kevin Buhr : Fixed the dumb errors in the above.
42 * Andi Kleen : Some small cleanups, optimizations,
43 * and fixed a copy_from_user() bug.
44 * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
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
54 * paradigm.
58 #include <linux/config.h>
59 #include <linux/mm.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>
74 #endif
76 #include <asm/uaccess.h>
78 #include <linux/inet.h>
79 #include <net/ip.h>
80 #include <net/sock.h>
81 #include <net/rarp.h>
82 #include <net/tcp.h>
83 #include <net/udp.h>
84 #include <net/scm.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 = {
107 sock_lseek,
108 sock_read,
109 sock_write,
110 NULL, /* readdir */
111 sock_poll,
112 sock_ioctl,
113 NULL, /* mmap */
114 sock_no_open, /* special open code to disallow open via /proc */
115 NULL, /* flush */
116 sock_close,
117 NULL, /* no fsync */
118 sock_fasync
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 * Socket hashing lock.
136 rwlock_t sockhash_lock = RW_LOCK_UNLOCKED;
139 * Support routines. Move socket addresses back and forth across the kernel/user
140 * divide and look after the messy bits.
143 #define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
144 16 for IP, 16 for IPX,
145 24 for IPv6,
146 about 80 for AX.25
147 must be at least one bigger than
148 the AF_UNIX size (see net/unix/af_unix.c
149 :unix_mkname()).
152 int move_addr_to_kernel(void *uaddr, int ulen, void *kaddr)
154 if(ulen<0||ulen>MAX_SOCK_ADDR)
155 return -EINVAL;
156 if(ulen==0)
157 return 0;
158 if(copy_from_user(kaddr,uaddr,ulen))
159 return -EFAULT;
160 return 0;
163 int move_addr_to_user(void *kaddr, int klen, void *uaddr, int *ulen)
165 int err;
166 int len;
168 if((err=get_user(len, ulen)))
169 return err;
170 if(len>klen)
171 len=klen;
172 if(len<0 || len> MAX_SOCK_ADDR)
173 return -EINVAL;
174 if(len)
176 if(copy_to_user(uaddr,kaddr,len))
177 return -EFAULT;
180 * "fromlen shall refer to the value before truncation.."
181 * 1003.1g
183 return __put_user(klen, ulen);
187 * Obtains the first available file descriptor and sets it up for use.
190 static int get_fd(struct inode *inode)
192 int fd;
195 * Find a file descriptor suitable for return to the user.
198 fd = get_unused_fd();
199 if (fd >= 0) {
200 struct file *file = get_empty_filp();
202 if (!file) {
203 put_unused_fd(fd);
204 return -ENFILE;
207 file->f_dentry = d_alloc_root(inode, NULL);
208 if (!file->f_dentry) {
209 put_filp(file);
210 put_unused_fd(fd);
211 return -ENOMEM;
215 * The socket maintains a reference to the inode, so we
216 * have to increment the count.
218 inode->i_count++;
220 fd_install(fd, file);
221 file->f_op = &socket_file_ops;
222 file->f_mode = 3;
223 file->f_flags = O_RDWR;
224 file->f_pos = 0;
226 return fd;
229 extern __inline__ struct socket *socki_lookup(struct inode *inode)
231 return &inode->u.socket_i;
235 * Go from a file number to its socket slot.
238 extern struct socket *sockfd_lookup(int fd, int *err)
240 struct file *file;
241 struct inode *inode;
242 struct socket *sock;
244 if (!(file = fget(fd)))
246 *err = -EBADF;
247 return NULL;
250 inode = file->f_dentry->d_inode;
251 if (!inode || !inode->i_sock || !(sock = socki_lookup(inode)))
253 *err = -ENOTSOCK;
254 fput(file);
255 return NULL;
258 if (sock->file != file) {
259 printk(KERN_ERR "socki_lookup: socket file changed!\n");
260 sock->file = file;
262 return sock;
265 extern __inline__ void sockfd_put(struct socket *sock)
267 fput(sock->file);
271 * Allocate a socket.
274 struct socket *sock_alloc(void)
276 struct inode * inode;
277 struct socket * sock;
279 inode = get_empty_inode();
280 if (!inode)
281 return NULL;
283 sock = socki_lookup(inode);
285 inode->i_mode = S_IFSOCK|S_IRWXUGO;
286 inode->i_sock = 1;
287 inode->i_uid = current->fsuid;
288 inode->i_gid = current->fsgid;
290 sock->inode = inode;
291 init_waitqueue_head(&sock->wait);
292 sock->fasync_list = NULL;
293 sock->state = SS_UNCONNECTED;
294 sock->flags = 0;
295 sock->ops = NULL;
296 sock->sk = NULL;
297 sock->file = NULL;
299 sockets_in_use++;
300 return sock;
304 * In theory you can't get an open on this inode, but /proc provides
305 * a back door. Remember to keep it shut otherwise you'll let the
306 * creepy crawlies in.
309 static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
311 return -ENXIO;
314 void sock_release(struct socket *sock)
316 if (sock->state != SS_UNCONNECTED)
317 sock->state = SS_DISCONNECTING;
319 if (sock->ops)
320 sock->ops->release(sock, NULL);
322 if (sock->fasync_list)
323 printk(KERN_ERR "sock_release: fasync list not empty!\n");
325 --sockets_in_use; /* Bookkeeping.. */
326 sock->file=NULL;
327 iput(sock->inode);
330 int sock_sendmsg(struct socket *sock, struct msghdr *msg, int size)
332 int err;
333 struct scm_cookie scm;
335 err = scm_send(sock, msg, &scm);
336 if (err >= 0) {
337 err = sock->ops->sendmsg(sock, msg, size, &scm);
338 scm_destroy(&scm);
340 return err;
343 int sock_recvmsg(struct socket *sock, struct msghdr *msg, int size, int flags)
345 struct scm_cookie scm;
347 memset(&scm, 0, sizeof(scm));
349 size = sock->ops->recvmsg(sock, msg, size, flags, &scm);
350 if (size >= 0)
351 scm_recv(sock, msg, &scm, flags);
353 return size;
358 * Sockets are not seekable.
361 static long long sock_lseek(struct file *file,long long offset, int whence)
363 return -ESPIPE;
367 * Read data from a socket. ubuf is a user mode pointer. We make sure the user
368 * area ubuf...ubuf+size-1 is writable before asking the protocol.
371 static ssize_t sock_read(struct file *file, char *ubuf,
372 size_t size, loff_t *ppos)
374 struct socket *sock;
375 struct iovec iov;
376 struct msghdr msg;
378 if (ppos != &file->f_pos)
379 return -ESPIPE;
380 if (size==0) /* Match SYS5 behaviour */
381 return 0;
383 sock = socki_lookup(file->f_dentry->d_inode);
385 msg.msg_name=NULL;
386 msg.msg_namelen=0;
387 msg.msg_iov=&iov;
388 msg.msg_iovlen=1;
389 msg.msg_control=NULL;
390 msg.msg_controllen=0;
391 iov.iov_base=ubuf;
392 iov.iov_len=size;
394 return sock_recvmsg(sock, &msg, size,
395 !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT);
400 * Write data to a socket. We verify that the user area ubuf..ubuf+size-1
401 * is readable by the user process.
404 static ssize_t sock_write(struct file *file, const char *ubuf,
405 size_t size, loff_t *ppos)
407 struct socket *sock;
408 struct msghdr msg;
409 struct iovec iov;
411 if (ppos != &file->f_pos)
412 return -ESPIPE;
413 if(size==0) /* Match SYS5 behaviour */
414 return 0;
416 sock = socki_lookup(file->f_dentry->d_inode);
418 msg.msg_name=NULL;
419 msg.msg_namelen=0;
420 msg.msg_iov=&iov;
421 msg.msg_iovlen=1;
422 msg.msg_control=NULL;
423 msg.msg_controllen=0;
424 msg.msg_flags=!(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
425 iov.iov_base=(void *)ubuf;
426 iov.iov_len=size;
428 return sock_sendmsg(sock, &msg, size);
431 int sock_readv_writev(int type, struct inode * inode, struct file * file,
432 const struct iovec * iov, long count, long size)
434 struct msghdr msg;
435 struct socket *sock;
437 sock = socki_lookup(inode);
439 msg.msg_name = NULL;
440 msg.msg_namelen = 0;
441 msg.msg_control = NULL;
442 msg.msg_controllen = 0;
443 msg.msg_iov = (struct iovec *) iov;
444 msg.msg_iovlen = count;
445 msg.msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
447 /* read() does a VERIFY_WRITE */
448 if (type == VERIFY_WRITE)
449 return sock_recvmsg(sock, &msg, size, msg.msg_flags);
450 return sock_sendmsg(sock, &msg, size);
455 * With an ioctl arg may well be a user mode pointer, but we don't know what to do
456 * with it - that's up to the protocol still.
459 int sock_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
460 unsigned long arg)
462 struct socket *sock = socki_lookup(inode);
463 return sock->ops->ioctl(sock, cmd, arg);
467 static unsigned int sock_poll(struct file *file, poll_table * wait)
469 struct socket *sock;
471 sock = socki_lookup(file->f_dentry->d_inode);
474 * We can't return errors to poll, so it's either yes or no.
477 return sock->ops->poll(file, sock, wait);
481 int sock_close(struct inode *inode, struct file *filp)
484 * It was possible the inode is NULL we were
485 * closing an unfinished socket.
488 if (!inode)
490 printk(KERN_DEBUG "sock_close: NULL inode\n");
491 return 0;
493 sock_fasync(-1, filp, 0);
494 sock_release(socki_lookup(inode));
495 return 0;
499 * Update the socket async list
502 static int sock_fasync(int fd, struct file *filp, int on)
504 struct fasync_struct *fa, *fna=NULL, **prev;
505 struct socket *sock;
507 if (on)
509 fna=(struct fasync_struct *)kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
510 if(fna==NULL)
511 return -ENOMEM;
514 sock = socki_lookup(filp->f_dentry->d_inode);
516 prev=&(sock->fasync_list);
518 lock_sock(sock->sk);
520 for (fa=*prev; fa!=NULL; prev=&fa->fa_next,fa=*prev)
521 if (fa->fa_file==filp)
522 break;
524 if(on)
526 if(fa!=NULL)
528 fa->fa_fd=fd;
529 kfree_s(fna,sizeof(struct fasync_struct));
530 release_sock(sock->sk);
531 return 0;
533 fna->fa_file=filp;
534 fna->fa_fd=fd;
535 fna->magic=FASYNC_MAGIC;
536 fna->fa_next=sock->fasync_list;
537 sock->fasync_list=fna;
539 else
541 if (fa!=NULL)
543 *prev=fa->fa_next;
544 kfree_s(fa,sizeof(struct fasync_struct));
548 release_sock(sock->sk);
549 return 0;
552 int sock_wake_async(struct socket *sock, int how)
554 if (!sock || !sock->fasync_list)
555 return -1;
556 switch (how)
558 case 1:
559 if (sock->flags & SO_WAITDATA)
560 break;
561 goto call_kill;
562 case 2:
563 if (!(sock->flags & SO_NOSPACE))
564 break;
565 sock->flags &= ~SO_NOSPACE;
566 /* fall through */
567 case 0:
568 call_kill:
569 if(sock->fasync_list != NULL)
570 kill_fasync(sock->fasync_list, SIGIO);
571 break;
573 return 0;
577 int sock_create(int family, int type, int protocol, struct socket **res)
579 int i;
580 struct socket *sock;
583 * Check protocol is in range
585 if(family<0||family>=NPROTO)
586 return -EINVAL;
588 #if defined(CONFIG_KMOD) && defined(CONFIG_NET)
589 /* Attempt to load a protocol module if the find failed.
591 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
592 * requested real, full-featured networking support upon configuration.
593 * Otherwise module support will break!
595 if (net_families[family]==NULL)
597 char module_name[30];
598 sprintf(module_name,"net-pf-%d",family);
599 request_module(module_name);
601 #endif
603 if (net_families[family]==NULL)
604 return -EINVAL;
607 * Check that this is a type that we know how to manipulate and
608 * the protocol makes sense here. The family can still reject the
609 * protocol later.
612 if ((type != SOCK_STREAM && type != SOCK_DGRAM &&
613 type != SOCK_SEQPACKET && type != SOCK_RAW && type != SOCK_RDM &&
614 #ifdef CONFIG_XTP
615 type != SOCK_WEB &&
616 #endif
617 type != SOCK_PACKET) || protocol < 0)
618 return -EINVAL;
621 * Allocate the socket and allow the family to set things up. if
622 * the protocol is 0, the family is instructed to select an appropriate
623 * default.
626 if (!(sock = sock_alloc()))
628 printk(KERN_WARNING "socket: no more sockets\n");
629 return -ENFILE; /* Not exactly a match, but its the
630 closest posix thing */
633 sock->type = type;
635 if ((i = net_families[family]->create(sock, protocol)) < 0)
637 sock_release(sock);
638 return i;
641 *res = sock;
642 return 0;
645 asmlinkage int sys_socket(int family, int type, int protocol)
647 int retval;
648 struct socket *sock;
650 lock_kernel();
652 retval = sock_create(family, type, protocol, &sock);
653 if (retval < 0)
654 goto out;
656 retval = get_fd(sock->inode);
657 if (retval < 0)
658 goto out_release;
659 sock->file = fcheck(retval);
661 out:
662 unlock_kernel();
663 return retval;
665 out_release:
666 sock_release(sock);
667 goto out;
671 * Create a pair of connected sockets.
674 asmlinkage int sys_socketpair(int family, int type, int protocol, int usockvec[2])
676 struct socket *sock1, *sock2;
677 int fd1, fd2, err;
679 lock_kernel();
682 * Obtain the first socket and check if the underlying protocol
683 * supports the socketpair call.
686 err = sys_socket(family, type, protocol);
687 if (err < 0)
688 goto out;
689 fd1 = err;
692 * Now grab another socket
694 err = -EINVAL;
695 fd2 = sys_socket(family, type, protocol);
696 if (fd2 < 0)
697 goto out_close1;
700 * Get the sockets for the two fd's
702 sock1 = sockfd_lookup(fd1, &err);
703 if (!sock1)
704 goto out_close2;
705 sock2 = sockfd_lookup(fd2, &err);
706 if (!sock2)
707 goto out_put1;
709 /* try to connect the two sockets together */
710 err = sock1->ops->socketpair(sock1, sock2);
711 if (err < 0)
712 goto out_put2;
714 err = put_user(fd1, &usockvec[0]);
715 if (err)
716 goto out_put2;
717 err = put_user(fd2, &usockvec[1]);
719 out_put2:
720 sockfd_put(sock2);
721 out_put1:
722 sockfd_put(sock1);
724 if (err) {
725 out_close2:
726 sys_close(fd2);
727 out_close1:
728 sys_close(fd1);
730 out:
731 unlock_kernel();
732 return err;
737 * Bind a name to a socket. Nothing much to do here since it's
738 * the protocol's responsibility to handle the local address.
740 * We move the socket address to kernel space before we call
741 * the protocol layer (having also checked the address is ok).
744 asmlinkage int sys_bind(int fd, struct sockaddr *umyaddr, int addrlen)
746 struct socket *sock;
747 char address[MAX_SOCK_ADDR];
748 int err;
750 lock_kernel();
751 if((sock = sockfd_lookup(fd,&err))!=NULL)
753 if((err=move_addr_to_kernel(umyaddr,addrlen,address))>=0)
754 err = sock->ops->bind(sock, (struct sockaddr *)address, addrlen);
755 sockfd_put(sock);
757 unlock_kernel();
758 return err;
763 * Perform a listen. Basically, we allow the protocol to do anything
764 * necessary for a listen, and if that works, we mark the socket as
765 * ready for listening.
768 asmlinkage int sys_listen(int fd, int backlog)
770 struct socket *sock;
771 int err;
773 lock_kernel();
774 if((sock = sockfd_lookup(fd, &err))!=NULL)
776 err=sock->ops->listen(sock, backlog);
777 sockfd_put(sock);
779 unlock_kernel();
780 return err;
785 * For accept, we attempt to create a new socket, set up the link
786 * with the client, wake up the client, then return the new
787 * connected fd. We collect the address of the connector in kernel
788 * space and move it to user at the very end. This is unclean because
789 * we open the socket then return an error.
791 * 1003.1g adds the ability to recvmsg() to query connection pending
792 * status to recvmsg. We need to add that support in a way thats
793 * clean when we restucture accept also.
796 asmlinkage int sys_accept(int fd, struct sockaddr *upeer_sockaddr, int *upeer_addrlen)
798 struct inode *inode;
799 struct socket *sock, *newsock;
800 int err, len;
801 char address[MAX_SOCK_ADDR];
803 lock_kernel();
804 sock = sockfd_lookup(fd, &err);
805 if (!sock)
806 goto out;
808 restart:
809 err = -EMFILE;
810 if (!(newsock = sock_alloc()))
811 goto out_put;
813 inode = newsock->inode;
814 newsock->type = sock->type;
816 err = sock->ops->dup(newsock, sock);
817 if (err < 0)
818 goto out_release;
820 err = newsock->ops->accept(sock, newsock, sock->file->f_flags);
821 if (err < 0)
822 goto out_release;
823 newsock = socki_lookup(inode);
825 if ((err = get_fd(inode)) < 0)
826 goto out_release;
827 newsock->file = fcheck(err);
829 if (upeer_sockaddr)
831 /* Handle the race where the accept works and we
832 then getname after it has closed again */
833 if(newsock->ops->getname(newsock, (struct sockaddr *)address, &len, 1)<0)
835 sys_close(err);
836 goto restart;
838 /* N.B. Should check for errors here */
839 move_addr_to_user(address, len, upeer_sockaddr, upeer_addrlen);
842 out_put:
843 sockfd_put(sock);
844 out:
845 unlock_kernel();
846 return err;
848 out_release:
849 sock_release(newsock);
850 goto out_put;
855 * Attempt to connect to a socket with the server address. The address
856 * is in user space so we verify it is OK and move it to kernel space.
858 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
859 * break bindings
861 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
862 * other SEQPACKET protocols that take time to connect() as it doesn't
863 * include the -EINPROGRESS status for such sockets.
866 asmlinkage int sys_connect(int fd, struct sockaddr *uservaddr, int addrlen)
868 struct socket *sock;
869 char address[MAX_SOCK_ADDR];
870 int err;
872 lock_kernel();
873 sock = sockfd_lookup(fd, &err);
874 if (!sock)
875 goto out;
876 err = move_addr_to_kernel(uservaddr, addrlen, address);
877 if (err < 0)
878 goto out_put;
879 err = sock->ops->connect(sock, (struct sockaddr *) address, addrlen,
880 sock->file->f_flags);
881 out_put:
882 sockfd_put(sock);
883 out:
884 unlock_kernel();
885 return err;
889 * Get the local address ('name') of a socket object. Move the obtained
890 * name to user space.
893 asmlinkage int sys_getsockname(int fd, struct sockaddr *usockaddr, int *usockaddr_len)
895 struct socket *sock;
896 char address[MAX_SOCK_ADDR];
897 int len, err;
899 lock_kernel();
900 sock = sockfd_lookup(fd, &err);
901 if (!sock)
902 goto out;
903 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 0);
904 if (err)
905 goto out_put;
906 err = move_addr_to_user(address, len, usockaddr, usockaddr_len);
908 out_put:
909 sockfd_put(sock);
910 out:
911 unlock_kernel();
912 return err;
916 * Get the remote address ('name') of a socket object. Move the obtained
917 * name to user space.
920 asmlinkage int sys_getpeername(int fd, struct sockaddr *usockaddr, int *usockaddr_len)
922 struct socket *sock;
923 char address[MAX_SOCK_ADDR];
924 int len, err;
926 lock_kernel();
927 if ((sock = sockfd_lookup(fd, &err))!=NULL)
929 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 1);
930 if (!err)
931 err=move_addr_to_user(address,len, usockaddr, usockaddr_len);
932 sockfd_put(sock);
934 unlock_kernel();
935 return err;
939 * Send a datagram to a given address. We move the address into kernel
940 * space and check the user space data area is readable before invoking
941 * the protocol.
944 asmlinkage int sys_sendto(int fd, void * buff, size_t len, unsigned flags,
945 struct sockaddr *addr, int addr_len)
947 struct socket *sock;
948 char address[MAX_SOCK_ADDR];
949 int err;
950 struct msghdr msg;
951 struct iovec iov;
953 lock_kernel();
954 sock = sockfd_lookup(fd, &err);
955 if (!sock)
956 goto out;
957 iov.iov_base=buff;
958 iov.iov_len=len;
959 msg.msg_name=NULL;
960 msg.msg_iov=&iov;
961 msg.msg_iovlen=1;
962 msg.msg_control=NULL;
963 msg.msg_controllen=0;
964 msg.msg_namelen=addr_len;
965 if(addr)
967 err = move_addr_to_kernel(addr, addr_len, address);
968 if (err < 0)
969 goto out_put;
970 msg.msg_name=address;
972 if (sock->file->f_flags & O_NONBLOCK)
973 flags |= MSG_DONTWAIT;
974 msg.msg_flags = flags;
975 err = sock_sendmsg(sock, &msg, len);
977 out_put:
978 sockfd_put(sock);
979 out:
980 unlock_kernel();
981 return err;
985 * Send a datagram down a socket.
988 asmlinkage int sys_send(int fd, void * buff, size_t len, unsigned flags)
990 return sys_sendto(fd, buff, len, flags, NULL, 0);
994 * Receive a frame from the socket and optionally record the address of the
995 * sender. We verify the buffers are writable and if needed move the
996 * sender address from kernel to user space.
999 asmlinkage int sys_recvfrom(int fd, void * ubuf, size_t size, unsigned flags,
1000 struct sockaddr *addr, int *addr_len)
1002 struct socket *sock;
1003 struct iovec iov;
1004 struct msghdr msg;
1005 char address[MAX_SOCK_ADDR];
1006 int err,err2;
1008 lock_kernel();
1009 sock = sockfd_lookup(fd, &err);
1010 if (!sock)
1011 goto out;
1013 msg.msg_control=NULL;
1014 msg.msg_controllen=0;
1015 msg.msg_iovlen=1;
1016 msg.msg_iov=&iov;
1017 iov.iov_len=size;
1018 iov.iov_base=ubuf;
1019 msg.msg_name=address;
1020 msg.msg_namelen=MAX_SOCK_ADDR;
1021 if (sock->file->f_flags & O_NONBLOCK)
1022 flags |= MSG_DONTWAIT;
1023 err=sock_recvmsg(sock, &msg, size, flags);
1025 if(err >= 0 && addr != NULL)
1027 err2=move_addr_to_user(address, msg.msg_namelen, addr, addr_len);
1028 if(err2<0)
1029 err=err2;
1031 sockfd_put(sock);
1032 out:
1033 unlock_kernel();
1034 return err;
1038 * Receive a datagram from a socket.
1041 asmlinkage int sys_recv(int fd, void * ubuf, size_t size, unsigned flags)
1043 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1047 * Set a socket option. Because we don't know the option lengths we have
1048 * to pass the user mode parameter for the protocols to sort out.
1051 asmlinkage int sys_setsockopt(int fd, int level, int optname, char *optval, int optlen)
1053 int err;
1054 struct socket *sock;
1056 lock_kernel();
1057 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1059 if (level == SOL_SOCKET)
1060 err=sock_setsockopt(sock,level,optname,optval,optlen);
1061 else
1062 err=sock->ops->setsockopt(sock, level, optname, optval, optlen);
1063 sockfd_put(sock);
1065 unlock_kernel();
1066 return err;
1070 * Get a socket option. Because we don't know the option lengths we have
1071 * to pass a user mode parameter for the protocols to sort out.
1074 asmlinkage int sys_getsockopt(int fd, int level, int optname, char *optval, int *optlen)
1076 int err;
1077 struct socket *sock;
1079 lock_kernel();
1080 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1082 if (level == SOL_SOCKET)
1083 err=sock_getsockopt(sock,level,optname,optval,optlen);
1084 else
1085 err=sock->ops->getsockopt(sock, level, optname, optval, optlen);
1086 sockfd_put(sock);
1088 unlock_kernel();
1089 return err;
1094 * Shutdown a socket.
1097 asmlinkage int sys_shutdown(int fd, int how)
1099 int err;
1100 struct socket *sock;
1102 lock_kernel();
1103 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1105 err=sock->ops->shutdown(sock, how);
1106 sockfd_put(sock);
1108 unlock_kernel();
1109 return err;
1113 * BSD sendmsg interface
1116 asmlinkage int sys_sendmsg(int fd, struct msghdr *msg, unsigned flags)
1118 struct socket *sock;
1119 char address[MAX_SOCK_ADDR];
1120 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1121 unsigned char ctl[sizeof(struct cmsghdr) + 20]; /* 20 is size of ipv6_pktinfo */
1122 unsigned char *ctl_buf = ctl;
1123 struct msghdr msg_sys;
1124 int err, ctl_len, iov_size, total_len;
1126 lock_kernel();
1128 err = -EFAULT;
1129 if (copy_from_user(&msg_sys,msg,sizeof(struct msghdr)))
1130 goto out;
1132 sock = sockfd_lookup(fd, &err);
1133 if (!sock)
1134 goto out;
1136 /* do not move before msg_sys is valid */
1137 err = -EINVAL;
1138 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1139 goto out_put;
1141 /* Check whether to allocate the iovec area*/
1142 err = -ENOMEM;
1143 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1144 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1145 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1146 if (!iov)
1147 goto out_put;
1150 /* This will also move the address data into kernel space */
1151 err = verify_iovec(&msg_sys, iov, address, VERIFY_READ);
1152 if (err < 0)
1153 goto out_freeiov;
1154 total_len = err;
1156 err = -ENOBUFS;
1158 /* msg_controllen must fit to int */
1159 if (msg_sys.msg_controllen > INT_MAX)
1160 goto out_freeiov;
1161 ctl_len = msg_sys.msg_controllen;
1162 if (ctl_len)
1164 if (ctl_len > sizeof(ctl))
1166 /* Suggested by the Advanced Sockets API for IPv6 draft:
1167 * Limit the msg_controllen size by the SO_SNDBUF size.
1169 /* Note - when this code becomes multithreaded on
1170 * SMP machines you have a race to fix here.
1172 err = -ENOBUFS;
1173 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
1174 if (ctl_buf == NULL)
1175 goto out_freeiov;
1177 err = -EFAULT;
1178 if (copy_from_user(ctl_buf, msg_sys.msg_control, ctl_len))
1179 goto out_freectl;
1180 msg_sys.msg_control = ctl_buf;
1182 msg_sys.msg_flags = flags;
1184 if (sock->file->f_flags & O_NONBLOCK)
1185 msg_sys.msg_flags |= MSG_DONTWAIT;
1186 err = sock_sendmsg(sock, &msg_sys, total_len);
1188 out_freectl:
1189 if (ctl_buf != ctl)
1190 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1191 out_freeiov:
1192 if (iov != iovstack)
1193 sock_kfree_s(sock->sk, iov, iov_size);
1194 out_put:
1195 sockfd_put(sock);
1196 out:
1197 unlock_kernel();
1198 return err;
1202 * BSD recvmsg interface
1205 asmlinkage int sys_recvmsg(int fd, struct msghdr *msg, unsigned int flags)
1207 struct socket *sock;
1208 struct iovec iovstack[UIO_FASTIOV];
1209 struct iovec *iov=iovstack;
1210 struct msghdr msg_sys;
1211 unsigned long cmsg_ptr;
1212 int err, iov_size, total_len, len;
1214 /* kernel mode address */
1215 char addr[MAX_SOCK_ADDR];
1217 /* user mode address pointers */
1218 struct sockaddr *uaddr;
1219 int *uaddr_len;
1221 lock_kernel();
1222 err=-EFAULT;
1223 if (copy_from_user(&msg_sys,msg,sizeof(struct msghdr)))
1224 goto out;
1226 sock = sockfd_lookup(fd, &err);
1227 if (!sock)
1228 goto out;
1230 err = -EINVAL;
1231 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1232 goto out_put;
1234 /* Check whether to allocate the iovec area*/
1235 err = -ENOMEM;
1236 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1237 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1238 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1239 if (!iov)
1240 goto out_put;
1244 * Save the user-mode address (verify_iovec will change the
1245 * kernel msghdr to use the kernel address space)
1248 uaddr = msg_sys.msg_name;
1249 uaddr_len = &msg->msg_namelen;
1250 err = verify_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1251 if (err < 0)
1252 goto out_freeiov;
1253 total_len=err;
1255 cmsg_ptr = (unsigned long)msg_sys.msg_control;
1256 msg_sys.msg_flags = 0;
1258 if (sock->file->f_flags & O_NONBLOCK)
1259 flags |= MSG_DONTWAIT;
1260 err = sock_recvmsg(sock, &msg_sys, total_len, flags);
1261 if (err < 0)
1262 goto out_freeiov;
1263 len = err;
1265 if (uaddr != NULL) {
1266 err = move_addr_to_user(addr, msg_sys.msg_namelen, uaddr, uaddr_len);
1267 if (err < 0)
1268 goto out_freeiov;
1270 err = __put_user(msg_sys.msg_flags, &msg->msg_flags);
1271 if (err)
1272 goto out_freeiov;
1273 err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr,
1274 &msg->msg_controllen);
1275 if (err)
1276 goto out_freeiov;
1277 err = len;
1279 out_freeiov:
1280 if (iov != iovstack)
1281 sock_kfree_s(sock->sk, iov, iov_size);
1282 out_put:
1283 sockfd_put(sock);
1284 out:
1285 unlock_kernel();
1286 return err;
1291 * Perform a file control on a socket file descriptor.
1293 * Doesn't aquire a fd lock, because no network fcntl
1294 * function sleeps currently.
1297 int sock_fcntl(struct file *filp, unsigned int cmd, unsigned long arg)
1299 struct socket *sock;
1301 sock = socki_lookup (filp->f_dentry->d_inode);
1302 if (sock && sock->ops)
1303 return sock->ops->fcntl(sock, cmd, arg);
1304 return(-EINVAL);
1307 /* Argument list sizes for sys_socketcall */
1308 #define AL(x) ((x) * sizeof(unsigned long))
1309 static unsigned char nargs[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
1310 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
1311 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)};
1312 #undef AL
1315 * System call vectors.
1317 * Argument checking cleaned up. Saved 20% in size.
1318 * This function doesn't need to set the kernel lock because
1319 * it is set by the callees.
1322 asmlinkage int sys_socketcall(int call, unsigned long *args)
1324 unsigned long a[6];
1325 unsigned long a0,a1;
1326 int err;
1328 if(call<1||call>SYS_RECVMSG)
1329 return -EINVAL;
1331 /* copy_from_user should be SMP safe. */
1332 if (copy_from_user(a, args, nargs[call]))
1333 return -EFAULT;
1335 a0=a[0];
1336 a1=a[1];
1338 switch(call)
1340 case SYS_SOCKET:
1341 err = sys_socket(a0,a1,a[2]);
1342 break;
1343 case SYS_BIND:
1344 err = sys_bind(a0,(struct sockaddr *)a1, a[2]);
1345 break;
1346 case SYS_CONNECT:
1347 err = sys_connect(a0, (struct sockaddr *)a1, a[2]);
1348 break;
1349 case SYS_LISTEN:
1350 err = sys_listen(a0,a1);
1351 break;
1352 case SYS_ACCEPT:
1353 err = sys_accept(a0,(struct sockaddr *)a1, (int *)a[2]);
1354 break;
1355 case SYS_GETSOCKNAME:
1356 err = sys_getsockname(a0,(struct sockaddr *)a1, (int *)a[2]);
1357 break;
1358 case SYS_GETPEERNAME:
1359 err = sys_getpeername(a0, (struct sockaddr *)a1, (int *)a[2]);
1360 break;
1361 case SYS_SOCKETPAIR:
1362 err = sys_socketpair(a0,a1, a[2], (int *)a[3]);
1363 break;
1364 case SYS_SEND:
1365 err = sys_send(a0, (void *)a1, a[2], a[3]);
1366 break;
1367 case SYS_SENDTO:
1368 err = sys_sendto(a0,(void *)a1, a[2], a[3],
1369 (struct sockaddr *)a[4], a[5]);
1370 break;
1371 case SYS_RECV:
1372 err = sys_recv(a0, (void *)a1, a[2], a[3]);
1373 break;
1374 case SYS_RECVFROM:
1375 err = sys_recvfrom(a0, (void *)a1, a[2], a[3],
1376 (struct sockaddr *)a[4], (int *)a[5]);
1377 break;
1378 case SYS_SHUTDOWN:
1379 err = sys_shutdown(a0,a1);
1380 break;
1381 case SYS_SETSOCKOPT:
1382 err = sys_setsockopt(a0, a1, a[2], (char *)a[3], a[4]);
1383 break;
1384 case SYS_GETSOCKOPT:
1385 err = sys_getsockopt(a0, a1, a[2], (char *)a[3], (int *)a[4]);
1386 break;
1387 case SYS_SENDMSG:
1388 err = sys_sendmsg(a0, (struct msghdr *) a1, a[2]);
1389 break;
1390 case SYS_RECVMSG:
1391 err = sys_recvmsg(a0, (struct msghdr *) a1, a[2]);
1392 break;
1393 default:
1394 err = -EINVAL;
1395 break;
1397 return err;
1401 * This function is called by a protocol handler that wants to
1402 * advertise its address family, and have it linked into the
1403 * SOCKET module.
1406 int sock_register(struct net_proto_family *ops)
1408 if (ops->family >= NPROTO) {
1409 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
1410 return -ENOBUFS;
1412 net_families[ops->family]=ops;
1413 return 0;
1417 * This function is called by a protocol handler that wants to
1418 * remove its address family, and have it unlinked from the
1419 * SOCKET module.
1422 int sock_unregister(int family)
1424 if (family < 0 || family >= NPROTO)
1425 return -1;
1427 net_families[family]=NULL;
1428 return 0;
1431 void __init proto_init(void)
1433 extern struct net_proto protocols[]; /* Network protocols */
1434 struct net_proto *pro;
1436 /* Kick all configured protocols. */
1437 pro = protocols;
1438 while (pro->name != NULL)
1440 (*pro->init_func)(pro);
1441 pro++;
1443 /* We're all done... */
1446 extern void sk_init(void);
1447 #ifdef CONFIG_WAN_ROUTER
1448 extern void wanrouter_init(void);
1449 #endif
1451 void __init sock_init(void)
1453 int i;
1455 printk(KERN_INFO "Linux NET4.0 for Linux 2.2\n");
1456 printk(KERN_INFO "Based upon Swansea University Computer Society NET3.039\n");
1459 * Initialize all address (protocol) families.
1462 for (i = 0; i < NPROTO; i++)
1463 net_families[i] = NULL;
1466 * Initialize sock SLAB cache.
1469 sk_init();
1471 #ifdef SLAB_SKB
1473 * Initialize skbuff SLAB cache
1475 skb_init();
1476 #endif
1480 * Wan router layer.
1483 #ifdef CONFIG_WAN_ROUTER
1484 wanrouter_init();
1485 #endif
1488 * Attach the firewall module if configured
1491 #ifdef CONFIG_FIREWALL
1492 fwchain_init();
1493 #endif
1496 * Initialize the protocols module.
1499 proto_init();
1502 * The netlink device handler may be needed early.
1505 #ifdef CONFIG_RTNETLINK
1506 rtnetlink_init();
1507 #endif
1508 #ifdef CONFIG_NETLINK_DEV
1509 init_netlink();
1510 #endif
1513 int socket_get_info(char *buffer, char **start, off_t offset, int length)
1515 int len = sprintf(buffer, "sockets: used %d\n", sockets_in_use);
1516 if (offset >= len)
1518 *start = buffer;
1519 return 0;
1521 *start = buffer + offset;
1522 len -= offset;
1523 if (len > length)
1524 len = length;
1525 return len;