Import 2.1.118
[davej-history.git] / net / socket.c
blob4ddb606f2d92b1acb5812093827db69c487f6aa1
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.
46 * This program is free software; you can redistribute it and/or
47 * modify it under the terms of the GNU General Public License
48 * as published by the Free Software Foundation; either version
49 * 2 of the License, or (at your option) any later version.
52 * This module is effectively the top level interface to the BSD socket
53 * paradigm.
57 #include <linux/config.h>
58 #include <linux/signal.h>
59 #include <linux/errno.h>
60 #include <linux/sched.h>
61 #include <linux/mm.h>
62 #include <linux/smp.h>
63 #include <linux/smp_lock.h>
64 #include <linux/kernel.h>
65 #include <linux/major.h>
66 #include <linux/stat.h>
67 #include <linux/socket.h>
68 #include <linux/fcntl.h>
69 #include <linux/file.h>
70 #include <linux/net.h>
71 #include <linux/interrupt.h>
72 #include <linux/netdevice.h>
73 #include <linux/proc_fs.h>
74 #include <linux/firewall.h>
75 #include <linux/wanrouter.h>
76 #include <linux/init.h>
77 #include <linux/poll.h>
79 #if defined(CONFIG_KMOD) && defined(CONFIG_NET)
80 #include <linux/kmod.h>
81 #endif
83 #include <asm/system.h>
84 #include <asm/uaccess.h>
86 #include <linux/inet.h>
87 #include <net/ip.h>
88 #include <net/protocol.h>
89 #include <net/rarp.h>
90 #include <net/tcp.h>
91 #include <net/udp.h>
92 #include <linux/skbuff.h>
93 #include <net/sock.h>
94 #include <net/scm.h>
97 static long long sock_lseek(struct file *file, long long offset, int whence);
98 static ssize_t sock_read(struct file *file, char *buf,
99 size_t size, loff_t *ppos);
100 static ssize_t sock_write(struct file *file, const char *buf,
101 size_t size, loff_t *ppos);
103 static int sock_close(struct inode *inode, struct file *file);
104 static unsigned int sock_poll(struct file *file,
105 struct poll_table_struct *wait);
106 static int sock_ioctl(struct inode *inode, struct file *file,
107 unsigned int cmd, unsigned long arg);
108 static int sock_fasync(int fd, struct file *filp, int on);
112 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
113 * in the operation structures but are done directly via the socketcall() multiplexor.
116 static struct file_operations socket_file_ops = {
117 sock_lseek,
118 sock_read,
119 sock_write,
120 NULL, /* readdir */
121 sock_poll,
122 sock_ioctl,
123 NULL, /* mmap */
124 NULL, /* no special open code... */
125 NULL, /* flush */
126 sock_close,
127 NULL, /* no fsync */
128 sock_fasync
132 * The protocol list. Each protocol is registered in here.
135 struct net_proto_family *net_families[NPROTO];
138 * Statistics counters of the socket lists
141 static int sockets_in_use = 0;
144 * Support routines. Move socket addresses back and forth across the kernel/user
145 * divide and look after the messy bits.
148 #define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
149 16 for IP, 16 for IPX,
150 24 for IPv6,
151 about 80 for AX.25
152 must be at least one bigger than
153 the AF_UNIX size (see net/unix/af_unix.c
154 :unix_mkname()).
157 int move_addr_to_kernel(void *uaddr, int ulen, void *kaddr)
159 if(ulen<0||ulen>MAX_SOCK_ADDR)
160 return -EINVAL;
161 if(ulen==0)
162 return 0;
163 if(copy_from_user(kaddr,uaddr,ulen))
164 return -EFAULT;
165 return 0;
168 int move_addr_to_user(void *kaddr, int klen, void *uaddr, int *ulen)
170 int err;
171 int len;
173 if((err=get_user(len, ulen)))
174 return err;
175 if(len>klen)
176 len=klen;
177 if(len<0 || len> MAX_SOCK_ADDR)
178 return -EINVAL;
179 if(len)
181 if(copy_to_user(uaddr,kaddr,len))
182 return -EFAULT;
185 * "fromlen shall refer to the value before truncation.."
186 * 1003.1g
188 return __put_user(klen, ulen);
192 * Obtains the first available file descriptor and sets it up for use.
195 static int get_fd(struct inode *inode)
197 int fd;
200 * Find a file descriptor suitable for return to the user.
203 fd = get_unused_fd();
204 if (fd >= 0) {
205 struct file *file = get_empty_filp();
207 if (!file) {
208 put_unused_fd(fd);
209 return -ENFILE;
212 file->f_dentry = d_alloc_root(inode, NULL);
213 if (!file->f_dentry) {
214 put_filp(file);
215 put_unused_fd(fd);
216 return -ENOMEM;
220 * The socket maintains a reference to the inode, so we
221 * have to increment the count.
223 inode->i_count++;
225 fd_install(fd, file);
226 file->f_op = &socket_file_ops;
227 file->f_mode = 3;
228 file->f_flags = O_RDWR;
229 file->f_pos = 0;
231 return fd;
234 extern __inline__ struct socket *socki_lookup(struct inode *inode)
236 return &inode->u.socket_i;
240 * Go from a file number to its socket slot.
243 extern struct socket *sockfd_lookup(int fd, int *err)
245 struct file *file;
246 struct inode *inode;
247 struct socket *sock;
249 if (!(file = fget(fd)))
251 *err = -EBADF;
252 return NULL;
255 inode = file->f_dentry->d_inode;
256 if (!inode || !inode->i_sock || !(sock = socki_lookup(inode)))
258 *err = -ENOTSOCK;
259 fput(file);
260 return NULL;
263 if (sock->file != file) {
264 printk(KERN_ERR "socki_lookup: socket file changed!\n");
265 sock->file = file;
267 return sock;
270 extern __inline__ void sockfd_put(struct socket *sock)
272 fput(sock->file);
276 * Allocate a socket.
279 struct socket *sock_alloc(void)
281 struct inode * inode;
282 struct socket * sock;
284 inode = get_empty_inode();
285 if (!inode)
286 return NULL;
288 sock = socki_lookup(inode);
290 inode->i_mode = S_IFSOCK;
291 inode->i_sock = 1;
292 inode->i_uid = current->uid;
293 inode->i_gid = current->gid;
295 sock->inode = inode;
296 init_waitqueue(&sock->wait);
297 sock->fasync_list = NULL;
298 sock->state = SS_UNCONNECTED;
299 sock->flags = 0;
300 sock->ops = NULL;
301 sock->sk = NULL;
302 sock->file = NULL;
304 sockets_in_use++;
305 return sock;
308 void sock_release(struct socket *sock)
310 if (sock->state != SS_UNCONNECTED)
311 sock->state = SS_DISCONNECTING;
313 if (sock->ops)
314 sock->ops->release(sock, NULL);
316 if (sock->fasync_list)
317 printk(KERN_ERR "sock_release: fasync list not empty!\n");
319 --sockets_in_use; /* Bookkeeping.. */
320 sock->file=NULL;
321 iput(sock->inode);
324 int sock_sendmsg(struct socket *sock, struct msghdr *msg, int size)
326 int err;
327 struct scm_cookie scm;
329 err = scm_send(sock, msg, &scm);
330 if (err >= 0) {
331 err = sock->ops->sendmsg(sock, msg, size, &scm);
332 scm_destroy(&scm);
334 return err;
337 int sock_recvmsg(struct socket *sock, struct msghdr *msg, int size, int flags)
339 struct scm_cookie scm;
341 memset(&scm, 0, sizeof(scm));
343 size = sock->ops->recvmsg(sock, msg, size, flags, &scm);
344 if (size >= 0)
345 scm_recv(sock, msg, &scm, flags);
347 return size;
352 * Sockets are not seekable.
355 static long long sock_lseek(struct file *file,long long offset, int whence)
357 return -ESPIPE;
361 * Read data from a socket. ubuf is a user mode pointer. We make sure the user
362 * area ubuf...ubuf+size-1 is writable before asking the protocol.
365 static ssize_t sock_read(struct file *file, char *ubuf,
366 size_t size, loff_t *ppos)
368 struct socket *sock;
369 struct iovec iov;
370 struct msghdr msg;
372 if (ppos != &file->f_pos)
373 return -ESPIPE;
374 if (size==0) /* Match SYS5 behaviour */
375 return 0;
377 sock = socki_lookup(file->f_dentry->d_inode);
379 msg.msg_name=NULL;
380 msg.msg_namelen=0;
381 msg.msg_iov=&iov;
382 msg.msg_iovlen=1;
383 msg.msg_control=NULL;
384 msg.msg_controllen=0;
385 iov.iov_base=ubuf;
386 iov.iov_len=size;
388 return sock_recvmsg(sock, &msg, size,
389 !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT);
394 * Write data to a socket. We verify that the user area ubuf..ubuf+size-1
395 * is readable by the user process.
398 static ssize_t sock_write(struct file *file, const char *ubuf,
399 size_t size, loff_t *ppos)
401 struct socket *sock;
402 struct msghdr msg;
403 struct iovec iov;
405 if (ppos != &file->f_pos)
406 return -ESPIPE;
407 if(size==0) /* Match SYS5 behaviour */
408 return 0;
410 sock = socki_lookup(file->f_dentry->d_inode);
412 msg.msg_name=NULL;
413 msg.msg_namelen=0;
414 msg.msg_iov=&iov;
415 msg.msg_iovlen=1;
416 msg.msg_control=NULL;
417 msg.msg_controllen=0;
418 msg.msg_flags=!(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
419 iov.iov_base=(void *)ubuf;
420 iov.iov_len=size;
422 return sock_sendmsg(sock, &msg, size);
425 int sock_readv_writev(int type, struct inode * inode, struct file * file,
426 const struct iovec * iov, long count, long size)
428 struct msghdr msg;
429 struct socket *sock;
431 sock = socki_lookup(inode);
433 msg.msg_name = NULL;
434 msg.msg_namelen = 0;
435 msg.msg_control = NULL;
436 msg.msg_controllen = 0;
437 msg.msg_iov = (struct iovec *) iov;
438 msg.msg_iovlen = count;
439 msg.msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
441 /* read() does a VERIFY_WRITE */
442 if (type == VERIFY_WRITE)
443 return sock_recvmsg(sock, &msg, size, msg.msg_flags);
444 return sock_sendmsg(sock, &msg, size);
449 * With an ioctl arg may well be a user mode pointer, but we don't know what to do
450 * with it - that's up to the protocol still.
453 int sock_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
454 unsigned long arg)
456 struct socket *sock = socki_lookup(inode);
457 return sock->ops->ioctl(sock, cmd, arg);
461 static unsigned int sock_poll(struct file *file, poll_table * wait)
463 struct socket *sock;
465 sock = socki_lookup(file->f_dentry->d_inode);
468 * We can't return errors to poll, so it's either yes or no.
471 return sock->ops->poll(file, sock, wait);
475 int sock_close(struct inode *inode, struct file *filp)
478 * It was possible the inode is NULL we were
479 * closing an unfinished socket.
482 if (!inode)
484 printk(KERN_DEBUG "sock_close: NULL inode\n");
485 return 0;
487 sock_fasync(-1, filp, 0);
488 sock_release(socki_lookup(inode));
489 return 0;
493 * Update the socket async list
496 static int sock_fasync(int fd, struct file *filp, int on)
498 struct fasync_struct *fa, *fna=NULL, **prev;
499 struct socket *sock;
500 unsigned long flags;
502 if (on)
504 fna=(struct fasync_struct *)kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
505 if(fna==NULL)
506 return -ENOMEM;
509 sock = socki_lookup(filp->f_dentry->d_inode);
511 prev=&(sock->fasync_list);
513 save_flags(flags);
514 cli();
516 for (fa=*prev; fa!=NULL; prev=&fa->fa_next,fa=*prev)
517 if (fa->fa_file==filp)
518 break;
520 if(on)
522 if(fa!=NULL)
524 fa->fa_fd=fd;
525 kfree_s(fna,sizeof(struct fasync_struct));
526 restore_flags(flags);
527 return 0;
529 fna->fa_file=filp;
530 fna->fa_fd=fd;
531 fna->magic=FASYNC_MAGIC;
532 fna->fa_next=sock->fasync_list;
533 sock->fasync_list=fna;
535 else
537 if (fa!=NULL)
539 *prev=fa->fa_next;
540 kfree_s(fa,sizeof(struct fasync_struct));
543 restore_flags(flags);
544 return 0;
547 int sock_wake_async(struct socket *sock, int how)
549 if (!sock || !sock->fasync_list)
550 return -1;
551 switch (how)
553 case 1:
554 if (sock->flags & SO_WAITDATA)
555 break;
556 goto call_kill;
557 case 2:
558 if (!(sock->flags & SO_NOSPACE))
559 break;
560 sock->flags &= ~SO_NOSPACE;
561 /* fall through */
562 case 0:
563 call_kill:
564 kill_fasync(sock->fasync_list, SIGIO);
565 break;
567 return 0;
571 int sock_create(int family, int type, int protocol, struct socket **res)
573 int i;
574 struct socket *sock;
577 * Check protocol is in range
579 if(family<0||family>=NPROTO)
580 return -EINVAL;
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);
595 #endif
597 if (net_families[family]==NULL)
598 return -EINVAL;
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
603 * protocol later.
606 if ((type != SOCK_STREAM && type != SOCK_DGRAM &&
607 type != SOCK_SEQPACKET && type != SOCK_RAW && type != SOCK_RDM &&
608 #ifdef CONFIG_XTP
609 type != SOCK_WEB &&
610 #endif
611 type != SOCK_PACKET) || protocol < 0)
612 return -EINVAL;
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
617 * default.
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 */
627 sock->type = type;
629 if ((i = net_families[family]->create(sock, protocol)) < 0)
631 sock_release(sock);
632 return i;
635 *res = sock;
636 return 0;
639 asmlinkage int sys_socket(int family, int type, int protocol)
641 int retval;
642 struct socket *sock;
644 lock_kernel();
646 retval = sock_create(family, type, protocol, &sock);
647 if (retval < 0)
648 goto out;
650 retval = get_fd(sock->inode);
651 if (retval < 0)
652 goto out_release;
653 sock->file = fcheck(retval);
655 out:
656 unlock_kernel();
657 return retval;
659 out_release:
660 sock_release(sock);
661 goto out;
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;
671 int fd1, fd2, err;
673 lock_kernel();
676 * Obtain the first socket and check if the underlying protocol
677 * supports the socketpair call.
680 err = sys_socket(family, type, protocol);
681 if (err < 0)
682 goto out;
683 fd1 = err;
686 * Now grab another socket
688 err = -EINVAL;
689 fd2 = sys_socket(family, type, protocol);
690 if (fd2 < 0)
691 goto out_close1;
694 * Get the sockets for the two fd's
696 sock1 = sockfd_lookup(fd1, &err);
697 if (!sock1)
698 goto out_close2;
699 sock2 = sockfd_lookup(fd2, &err);
700 if (!sock2)
701 goto out_put1;
703 /* try to connect the two sockets together */
704 err = sock1->ops->socketpair(sock1, sock2);
705 if (err < 0)
706 goto out_put2;
708 err = put_user(fd1, &usockvec[0]);
709 if (err)
710 goto out_put2;
711 err = put_user(fd2, &usockvec[1]);
713 out_put2:
714 sockfd_put(sock2);
715 out_put1:
716 sockfd_put(sock1);
718 if (err) {
719 out_close2:
720 sys_close(fd2);
721 out_close1:
722 sys_close(fd1);
724 out:
725 unlock_kernel();
726 return err;
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)
740 struct socket *sock;
741 char address[MAX_SOCK_ADDR];
742 int err;
744 lock_kernel();
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);
749 sockfd_put(sock);
751 unlock_kernel();
752 return err;
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)
764 struct socket *sock;
765 int err;
767 lock_kernel();
768 if((sock = sockfd_lookup(fd, &err))!=NULL)
770 err=sock->ops->listen(sock, backlog);
771 sockfd_put(sock);
773 unlock_kernel();
774 return err;
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)
792 struct inode *inode;
793 struct socket *sock, *newsock;
794 int err, len;
795 char address[MAX_SOCK_ADDR];
797 lock_kernel();
798 sock = sockfd_lookup(fd, &err);
799 if (!sock)
800 goto out;
802 restart:
803 err = -EMFILE;
804 if (!(newsock = sock_alloc()))
805 goto out_put;
807 inode = newsock->inode;
808 newsock->type = sock->type;
810 err = sock->ops->dup(newsock, sock);
811 if (err < 0)
812 goto out_release;
814 err = newsock->ops->accept(sock, newsock, sock->file->f_flags);
815 if (err < 0)
816 goto out_release;
817 newsock = socki_lookup(inode);
819 if ((err = get_fd(inode)) < 0)
820 goto out_release;
821 newsock->file = fcheck(err);
823 if (upeer_sockaddr)
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)
829 sys_close(err);
830 goto restart;
832 /* N.B. Should check for errors here */
833 move_addr_to_user(address, len, upeer_sockaddr, upeer_addrlen);
836 out_put:
837 sockfd_put(sock);
838 out:
839 unlock_kernel();
840 return err;
842 out_release:
843 sock_release(newsock);
844 goto out_put;
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
853 * break bindings
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)
862 struct socket *sock;
863 char address[MAX_SOCK_ADDR];
864 int err;
866 lock_kernel();
867 sock = sockfd_lookup(fd, &err);
868 if (!sock)
869 goto out;
870 err = move_addr_to_kernel(uservaddr, addrlen, address);
871 if (err < 0)
872 goto out_put;
873 err = sock->ops->connect(sock, (struct sockaddr *) address, addrlen,
874 sock->file->f_flags);
875 out_put:
876 sockfd_put(sock);
877 out:
878 unlock_kernel();
879 return err;
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)
889 struct socket *sock;
890 char address[MAX_SOCK_ADDR];
891 int len, err;
893 lock_kernel();
894 sock = sockfd_lookup(fd, &err);
895 if (!sock)
896 goto out;
897 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 0);
898 if (err)
899 goto out_put;
900 err = move_addr_to_user(address, len, usockaddr, usockaddr_len);
902 out_put:
903 sockfd_put(sock);
904 out:
905 unlock_kernel();
906 return err;
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)
916 struct socket *sock;
917 char address[MAX_SOCK_ADDR];
918 int len, err;
920 lock_kernel();
921 if ((sock = sockfd_lookup(fd, &err))!=NULL)
923 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 1);
924 if (!err)
925 err=move_addr_to_user(address,len, usockaddr, usockaddr_len);
926 sockfd_put(sock);
928 unlock_kernel();
929 return err;
933 * Send a datagram down a socket. The datagram as with write() is
934 * in user space. We check it can be read.
937 asmlinkage int sys_send(int fd, void * buff, size_t len, unsigned flags)
939 struct socket *sock;
940 int err;
941 struct msghdr msg;
942 struct iovec iov;
944 lock_kernel();
945 sock = sockfd_lookup(fd, &err);
946 if (sock) {
947 iov.iov_base=buff;
948 iov.iov_len=len;
949 msg.msg_name=NULL;
950 msg.msg_namelen=0;
951 msg.msg_iov=&iov;
952 msg.msg_iovlen=1;
953 msg.msg_control=NULL;
954 msg.msg_controllen=0;
955 if (sock->file->f_flags & O_NONBLOCK)
956 flags |= MSG_DONTWAIT;
957 msg.msg_flags = flags;
958 err = sock_sendmsg(sock, &msg, len);
960 sockfd_put(sock);
962 unlock_kernel();
963 return err;
967 * Send a datagram to a given address. We move the address into kernel
968 * space and check the user space data area is readable before invoking
969 * the protocol.
972 asmlinkage int sys_sendto(int fd, void * buff, size_t len, unsigned flags,
973 struct sockaddr *addr, int addr_len)
975 struct socket *sock;
976 char address[MAX_SOCK_ADDR];
977 int err;
978 struct msghdr msg;
979 struct iovec iov;
981 lock_kernel();
982 sock = sockfd_lookup(fd, &err);
983 if (!sock)
984 goto out;
985 iov.iov_base=buff;
986 iov.iov_len=len;
987 msg.msg_name=NULL;
988 msg.msg_iov=&iov;
989 msg.msg_iovlen=1;
990 msg.msg_control=NULL;
991 msg.msg_controllen=0;
992 msg.msg_namelen=addr_len;
993 if(addr)
995 err = move_addr_to_kernel(addr, addr_len, address);
996 if (err < 0)
997 goto out_put;
998 msg.msg_name=address;
1000 if (sock->file->f_flags & O_NONBLOCK)
1001 flags |= MSG_DONTWAIT;
1002 msg.msg_flags = flags;
1003 err = sock_sendmsg(sock, &msg, len);
1005 out_put:
1006 sockfd_put(sock);
1007 out:
1008 unlock_kernel();
1009 return err;
1014 * Receive a frame from the socket and optionally record the address of the
1015 * sender. We verify the buffers are writable and if needed move the
1016 * sender address from kernel to user space.
1019 asmlinkage int sys_recvfrom(int fd, void * ubuf, size_t size, unsigned flags,
1020 struct sockaddr *addr, int *addr_len)
1022 struct socket *sock;
1023 struct iovec iov;
1024 struct msghdr msg;
1025 char address[MAX_SOCK_ADDR];
1026 int err,err2;
1028 lock_kernel();
1029 sock = sockfd_lookup(fd, &err);
1030 if (!sock)
1031 goto out;
1033 msg.msg_control=NULL;
1034 msg.msg_controllen=0;
1035 msg.msg_iovlen=1;
1036 msg.msg_iov=&iov;
1037 iov.iov_len=size;
1038 iov.iov_base=ubuf;
1039 msg.msg_name=address;
1040 msg.msg_namelen=MAX_SOCK_ADDR;
1041 if (sock->file->f_flags & O_NONBLOCK)
1042 flags |= MSG_DONTWAIT;
1043 err=sock_recvmsg(sock, &msg, size, flags);
1045 if(err >= 0 && addr != NULL)
1047 err2=move_addr_to_user(address, msg.msg_namelen, addr, addr_len);
1048 if(err2<0)
1049 err=err2;
1051 sockfd_put(sock);
1052 out:
1053 unlock_kernel();
1054 return err;
1058 * Receive a datagram from a socket.
1061 asmlinkage int sys_recv(int fd, void * ubuf, size_t size, unsigned flags)
1063 return sys_recvfrom(fd,ubuf,size,flags, NULL, NULL);
1067 * Set a socket option. Because we don't know the option lengths we have
1068 * to pass the user mode parameter for the protocols to sort out.
1071 asmlinkage int sys_setsockopt(int fd, int level, int optname, char *optval, int optlen)
1073 int err;
1074 struct socket *sock;
1076 lock_kernel();
1077 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1079 if (level == SOL_SOCKET)
1080 err=sock_setsockopt(sock,level,optname,optval,optlen);
1081 else
1082 err=sock->ops->setsockopt(sock, level, optname, optval, optlen);
1083 sockfd_put(sock);
1085 unlock_kernel();
1086 return err;
1090 * Get a socket option. Because we don't know the option lengths we have
1091 * to pass a user mode parameter for the protocols to sort out.
1094 asmlinkage int sys_getsockopt(int fd, int level, int optname, char *optval, int *optlen)
1096 int err;
1097 struct socket *sock;
1099 lock_kernel();
1100 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1102 if (level == SOL_SOCKET)
1103 err=sock_getsockopt(sock,level,optname,optval,optlen);
1104 else
1105 err=sock->ops->getsockopt(sock, level, optname, optval, optlen);
1106 sockfd_put(sock);
1108 unlock_kernel();
1109 return err;
1114 * Shutdown a socket.
1117 asmlinkage int sys_shutdown(int fd, int how)
1119 int err;
1120 struct socket *sock;
1122 lock_kernel();
1123 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1125 err=sock->ops->shutdown(sock, how);
1126 sockfd_put(sock);
1128 unlock_kernel();
1129 return err;
1133 * BSD sendmsg interface
1136 asmlinkage int sys_sendmsg(int fd, struct msghdr *msg, unsigned flags)
1138 struct socket *sock;
1139 char address[MAX_SOCK_ADDR];
1140 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1141 unsigned char ctl[sizeof(struct cmsghdr) + 20]; /* 20 is size of ipv6_pktinfo */
1142 unsigned char *ctl_buf = ctl;
1143 struct msghdr msg_sys;
1144 int err, ctl_len, iov_size, total_len;
1146 lock_kernel();
1148 err = -EFAULT;
1149 if (copy_from_user(&msg_sys,msg,sizeof(struct msghdr)))
1150 goto out;
1152 sock = sockfd_lookup(fd, &err);
1153 if (!sock)
1154 goto out;
1156 /* do not move before msg_sys is valid */
1157 err = -EINVAL;
1158 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1159 goto out_put;
1161 /* Check whether to allocate the iovec area*/
1162 err = -ENOMEM;
1163 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1164 if (msg_sys.msg_iovlen > 1 /* UIO_FASTIOV */) {
1165 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1166 if (!iov)
1167 goto out_put;
1170 /* This will also move the address data into kernel space */
1171 err = verify_iovec(&msg_sys, iov, address, VERIFY_READ);
1172 if (err < 0)
1173 goto out_freeiov;
1174 total_len = err;
1176 ctl_len = msg_sys.msg_controllen;
1177 if (ctl_len)
1179 if (ctl_len > sizeof(ctl))
1181 /* Suggested by the Advanced Sockets API for IPv6 draft:
1182 * Limit the msg_controllen size by the SO_SNDBUF size.
1184 /* Note - when this code becomes multithreaded on
1185 * SMP machines you have a race to fix here.
1187 err = -ENOBUFS;
1188 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
1189 if (ctl_buf == NULL)
1190 goto out_freeiov;
1192 err = -EFAULT;
1193 if (copy_from_user(ctl_buf, msg_sys.msg_control, ctl_len))
1194 goto out_freectl;
1195 msg_sys.msg_control = ctl_buf;
1197 msg_sys.msg_flags = flags;
1199 if (sock->file->f_flags & O_NONBLOCK)
1200 msg_sys.msg_flags |= MSG_DONTWAIT;
1201 err = sock_sendmsg(sock, &msg_sys, total_len);
1203 out_freectl:
1204 if (ctl_buf != ctl)
1205 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1206 out_freeiov:
1207 if (iov != iovstack)
1208 sock_kfree_s(sock->sk, iov, iov_size);
1209 out_put:
1210 sockfd_put(sock);
1211 out:
1212 unlock_kernel();
1213 return err;
1217 * BSD recvmsg interface
1220 asmlinkage int sys_recvmsg(int fd, struct msghdr *msg, unsigned int flags)
1222 struct socket *sock;
1223 struct iovec iovstack[UIO_FASTIOV];
1224 struct iovec *iov=iovstack;
1225 struct msghdr msg_sys;
1226 unsigned long cmsg_ptr;
1227 int err, iov_size, total_len, len;
1229 /* kernel mode address */
1230 char addr[MAX_SOCK_ADDR];
1232 /* user mode address pointers */
1233 struct sockaddr *uaddr;
1234 int *uaddr_len;
1236 lock_kernel();
1237 err=-EFAULT;
1238 if (copy_from_user(&msg_sys,msg,sizeof(struct msghdr)))
1239 goto out;
1241 sock = sockfd_lookup(fd, &err);
1242 if (!sock)
1243 goto out;
1245 err = -EINVAL;
1246 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1247 goto out_put;
1249 /* Check whether to allocate the iovec area*/
1250 err = -ENOMEM;
1251 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1252 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1253 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1254 if (!iov)
1255 goto out_put;
1259 * Save the user-mode address (verify_iovec will change the
1260 * kernel msghdr to use the kernel address space)
1263 uaddr = msg_sys.msg_name;
1264 uaddr_len = &msg->msg_namelen;
1265 err = verify_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1266 if (err < 0)
1267 goto out_freeiov;
1268 total_len=err;
1270 cmsg_ptr = (unsigned long)msg_sys.msg_control;
1271 msg_sys.msg_flags = 0;
1273 if (sock->file->f_flags & O_NONBLOCK)
1274 flags |= MSG_DONTWAIT;
1275 err = sock_recvmsg(sock, &msg_sys, total_len, flags);
1276 if (err < 0)
1277 goto out_freeiov;
1278 len = err;
1280 if (uaddr != NULL) {
1281 err = move_addr_to_user(addr, msg_sys.msg_namelen, uaddr, uaddr_len);
1282 if (err < 0)
1283 goto out_freeiov;
1285 err = __put_user(msg_sys.msg_flags, &msg->msg_flags);
1286 if (err)
1287 goto out_freeiov;
1288 err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr,
1289 &msg->msg_controllen);
1290 if (err)
1291 goto out_freeiov;
1292 err = len;
1294 out_freeiov:
1295 if (iov != iovstack)
1296 sock_kfree_s(sock->sk, iov, iov_size);
1297 out_put:
1298 sockfd_put(sock);
1299 out:
1300 unlock_kernel();
1301 return err;
1306 * Perform a file control on a socket file descriptor.
1308 * FIXME: does this need an fd lock ?
1311 int sock_fcntl(struct file *filp, unsigned int cmd, unsigned long arg)
1313 struct socket *sock;
1315 sock = socki_lookup (filp->f_dentry->d_inode);
1316 if (sock && sock->ops)
1317 return sock->ops->fcntl(sock, cmd, arg);
1318 return(-EINVAL);
1321 /* Argument list sizes for sys_socketcall */
1322 #define AL(x) ((x) * sizeof(unsigned long))
1323 static unsigned char nargs[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
1324 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
1325 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)};
1326 #undef AL
1329 * System call vectors.
1331 * Argument checking cleaned up. Saved 20% in size.
1332 * This function doesn't need to set the kernel lock because
1333 * it is set by the callees.
1336 asmlinkage int sys_socketcall(int call, unsigned long *args)
1338 unsigned long a[6];
1339 unsigned long a0,a1;
1340 int err;
1342 if(call<1||call>SYS_RECVMSG)
1343 return -EINVAL;
1345 /* copy_from_user should be SMP safe. */
1346 if (copy_from_user(a, args, nargs[call]))
1347 return -EFAULT;
1349 a0=a[0];
1350 a1=a[1];
1352 switch(call)
1354 case SYS_SOCKET:
1355 err = sys_socket(a0,a1,a[2]);
1356 break;
1357 case SYS_BIND:
1358 err = sys_bind(a0,(struct sockaddr *)a1, a[2]);
1359 break;
1360 case SYS_CONNECT:
1361 err = sys_connect(a0, (struct sockaddr *)a1, a[2]);
1362 break;
1363 case SYS_LISTEN:
1364 err = sys_listen(a0,a1);
1365 break;
1366 case SYS_ACCEPT:
1367 err = sys_accept(a0,(struct sockaddr *)a1, (int *)a[2]);
1368 break;
1369 case SYS_GETSOCKNAME:
1370 err = sys_getsockname(a0,(struct sockaddr *)a1, (int *)a[2]);
1371 break;
1372 case SYS_GETPEERNAME:
1373 err = sys_getpeername(a0, (struct sockaddr *)a1, (int *)a[2]);
1374 break;
1375 case SYS_SOCKETPAIR:
1376 err = sys_socketpair(a0,a1, a[2], (int *)a[3]);
1377 break;
1378 case SYS_SEND:
1379 err = sys_send(a0, (void *)a1, a[2], a[3]);
1380 break;
1381 case SYS_SENDTO:
1382 err = sys_sendto(a0,(void *)a1, a[2], a[3],
1383 (struct sockaddr *)a[4], a[5]);
1384 break;
1385 case SYS_RECV:
1386 err = sys_recv(a0, (void *)a1, a[2], a[3]);
1387 break;
1388 case SYS_RECVFROM:
1389 err = sys_recvfrom(a0, (void *)a1, a[2], a[3],
1390 (struct sockaddr *)a[4], (int *)a[5]);
1391 break;
1392 case SYS_SHUTDOWN:
1393 err = sys_shutdown(a0,a1);
1394 break;
1395 case SYS_SETSOCKOPT:
1396 err = sys_setsockopt(a0, a1, a[2], (char *)a[3], a[4]);
1397 break;
1398 case SYS_GETSOCKOPT:
1399 err = sys_getsockopt(a0, a1, a[2], (char *)a[3], (int *)a[4]);
1400 break;
1401 case SYS_SENDMSG:
1402 err = sys_sendmsg(a0, (struct msghdr *) a1, a[2]);
1403 break;
1404 case SYS_RECVMSG:
1405 err = sys_recvmsg(a0, (struct msghdr *) a1, a[2]);
1406 break;
1407 default:
1408 err = -EINVAL;
1409 break;
1411 return err;
1415 * This function is called by a protocol handler that wants to
1416 * advertise its address family, and have it linked into the
1417 * SOCKET module.
1420 int sock_register(struct net_proto_family *ops)
1422 if (ops->family >= NPROTO) {
1423 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
1424 return -ENOBUFS;
1426 net_families[ops->family]=ops;
1427 return 0;
1431 * This function is called by a protocol handler that wants to
1432 * remove its address family, and have it unlinked from the
1433 * SOCKET module.
1436 int sock_unregister(int family)
1438 if (family < 0 || family >= NPROTO)
1439 return -1;
1441 net_families[family]=NULL;
1442 return 0;
1445 void __init proto_init(void)
1447 extern struct net_proto protocols[]; /* Network protocols */
1448 struct net_proto *pro;
1450 /* Kick all configured protocols. */
1451 pro = protocols;
1452 while (pro->name != NULL)
1454 (*pro->init_func)(pro);
1455 pro++;
1457 /* We're all done... */
1460 extern void sk_init(void);
1461 #ifdef CONFIG_WAN_ROUTER
1462 extern void wanrouter_init(void);
1463 #endif
1465 void __init sock_init(void)
1467 int i;
1469 printk(KERN_INFO "Swansea University Computer Society NET3.039 for Linux 2.1\n");
1472 * Initialize all address (protocol) families.
1475 for (i = 0; i < NPROTO; i++)
1476 net_families[i] = NULL;
1479 * Initialize sock SLAB cache.
1482 sk_init();
1484 #ifdef SLAB_SKB
1486 * Initialize skbuff SLAB cache
1488 skb_init();
1489 #endif
1493 * Wan router layer.
1496 #ifdef CONFIG_WAN_ROUTER
1497 wanrouter_init();
1498 #endif
1501 * Attach the firewall module if configured
1504 #ifdef CONFIG_FIREWALL
1505 fwchain_init();
1506 #endif
1509 * Initialize the protocols module.
1512 proto_init();
1515 * The netlink device handler may be needed early.
1518 #ifdef CONFIG_RTNETLINK
1519 rtnetlink_init();
1520 #endif
1521 #ifdef CONFIG_NETLINK_DEV
1522 init_netlink();
1523 #endif
1526 int socket_get_info(char *buffer, char **start, off_t offset, int length)
1528 int len = sprintf(buffer, "sockets: used %d\n", sockets_in_use);
1529 if (offset >= len)
1531 *start = buffer;
1532 return 0;
1534 *start = buffer + offset;
1535 len -= offset;
1536 if (len > length)
1537 len = length;
1538 return len;