Merge with Linux 2.3.99-pre4.
[linux-2.6/linux-mips.git] / net / socket.c
blobfb51582416b2d6f18f8c39e4a741e53cd853d2d9
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)
45 * Tigran Aivazian : Made listen(2) backlog sanity checks
46 * protocol-independent
49 * This program is free software; you can redistribute it and/or
50 * modify it under the terms of the GNU General Public License
51 * as published by the Free Software Foundation; either version
52 * 2 of the License, or (at your option) any later version.
55 * This module is effectively the top level interface to the BSD socket
56 * paradigm.
60 #include <linux/config.h>
61 #include <linux/mm.h>
62 #include <linux/smp_lock.h>
63 #include <linux/socket.h>
64 #include <linux/file.h>
65 #include <linux/net.h>
66 #include <linux/interrupt.h>
67 #include <linux/netdevice.h>
68 #include <linux/proc_fs.h>
69 #include <linux/wanrouter.h>
70 #include <linux/init.h>
71 #include <linux/poll.h>
72 #include <linux/cache.h>
74 #if defined(CONFIG_KMOD) && defined(CONFIG_NET)
75 #include <linux/kmod.h>
76 #endif
78 #include <asm/uaccess.h>
80 #include <linux/inet.h>
81 #include <net/ip.h>
82 #include <net/sock.h>
83 #include <net/tcp.h>
84 #include <net/udp.h>
85 #include <net/scm.h>
86 #include <linux/netfilter.h>
88 static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
89 static loff_t sock_lseek(struct file *file, loff_t offset, int whence);
90 static ssize_t sock_read(struct file *file, char *buf,
91 size_t size, loff_t *ppos);
92 static ssize_t sock_write(struct file *file, const char *buf,
93 size_t size, loff_t *ppos);
94 static int sock_mmap(struct file *file, struct vm_area_struct * vma);
96 static int sock_close(struct inode *inode, struct file *file);
97 static unsigned int sock_poll(struct file *file,
98 struct poll_table_struct *wait);
99 static int sock_ioctl(struct inode *inode, struct file *file,
100 unsigned int cmd, unsigned long arg);
101 static int sock_fasync(int fd, struct file *filp, int on);
102 static ssize_t sock_readv(struct file *file, const struct iovec *vector,
103 unsigned long count, loff_t *ppos);
104 static ssize_t sock_writev(struct file *file, const struct iovec *vector,
105 unsigned long count, loff_t *ppos);
109 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
110 * in the operation structures but are done directly via the socketcall() multiplexor.
113 static struct file_operations socket_file_ops = {
114 llseek: sock_lseek,
115 read: sock_read,
116 write: sock_write,
117 poll: sock_poll,
118 ioctl: sock_ioctl,
119 mmap: sock_mmap,
120 open: sock_no_open, /* special open code to disallow open via /proc */
121 release: sock_close,
122 fasync: sock_fasync,
123 readv: sock_readv,
124 writev: sock_writev
128 * The protocol list. Each protocol is registered in here.
131 static struct net_proto_family *net_families[NPROTO];
133 #ifdef __SMP__
134 static atomic_t net_family_lockct = ATOMIC_INIT(0);
135 static spinlock_t net_family_lock = SPIN_LOCK_UNLOCKED;
137 /* The strategy is: modifications net_family vector are short, do not
138 sleep and veeery rare, but read access should be free of any exclusive
139 locks.
142 static void net_family_write_lock(void)
144 spin_lock(&net_family_lock);
145 while (atomic_read(&net_family_lockct) != 0) {
146 spin_unlock(&net_family_lock);
148 current->policy |= SCHED_YIELD;
149 schedule();
151 spin_lock(&net_family_lock);
155 static __inline__ void net_family_write_unlock(void)
157 spin_unlock(&net_family_lock);
160 static __inline__ void net_family_read_lock(void)
162 atomic_inc(&net_family_lockct);
163 spin_unlock_wait(&net_family_lock);
166 static __inline__ void net_family_read_unlock(void)
168 atomic_dec(&net_family_lockct);
171 #else
172 #define net_family_write_lock() do { } while(0)
173 #define net_family_write_unlock() do { } while(0)
174 #define net_family_read_lock() do { } while(0)
175 #define net_family_read_unlock() do { } while(0)
176 #endif
180 * Statistics counters of the socket lists
183 static union {
184 int counter;
185 char __pad[SMP_CACHE_BYTES];
186 } sockets_in_use[NR_CPUS] __cacheline_aligned = {{0}};
189 * Support routines. Move socket addresses back and forth across the kernel/user
190 * divide and look after the messy bits.
193 #define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
194 16 for IP, 16 for IPX,
195 24 for IPv6,
196 about 80 for AX.25
197 must be at least one bigger than
198 the AF_UNIX size (see net/unix/af_unix.c
199 :unix_mkname()).
202 int move_addr_to_kernel(void *uaddr, int ulen, void *kaddr)
204 if(ulen<0||ulen>MAX_SOCK_ADDR)
205 return -EINVAL;
206 if(ulen==0)
207 return 0;
208 if(copy_from_user(kaddr,uaddr,ulen))
209 return -EFAULT;
210 return 0;
213 int move_addr_to_user(void *kaddr, int klen, void *uaddr, int *ulen)
215 int err;
216 int len;
218 if((err=get_user(len, ulen)))
219 return err;
220 if(len>klen)
221 len=klen;
222 if(len<0 || len> MAX_SOCK_ADDR)
223 return -EINVAL;
224 if(len)
226 if(copy_to_user(uaddr,kaddr,len))
227 return -EFAULT;
230 * "fromlen shall refer to the value before truncation.."
231 * 1003.1g
233 return __put_user(klen, ulen);
237 * Obtains the first available file descriptor and sets it up for use.
239 * This functions creates file structure and maps it to fd space
240 * of current process. On success it returns file descriptor
241 * and file struct implicitly stored in sock->file.
242 * Note that another thread may close file descriptor before we return
243 * from this function. We use the fact that now we do not refer
244 * to socket after mapping. If one day we will need it, this
245 * function will inincrement ref. count on file by 1.
247 * In any case returned fd MAY BE not valid!
248 * This race condition is inavoidable
249 * with shared fd spaces, we cannot solve is inside kernel,
250 * but we take care of internal coherence yet.
253 static int sock_map_fd(struct socket *sock)
255 int fd;
258 * Find a file descriptor suitable for return to the user.
261 fd = get_unused_fd();
262 if (fd >= 0) {
263 struct file *file = get_empty_filp();
265 if (!file) {
266 put_unused_fd(fd);
267 fd = -ENFILE;
268 goto out;
271 file->f_dentry = d_alloc_root(sock->inode);
272 if (!file->f_dentry) {
273 put_filp(file);
274 put_unused_fd(fd);
275 fd = -ENOMEM;
276 goto out;
279 sock->file = file;
280 file->f_op = &socket_file_ops;
281 file->f_mode = 3;
282 file->f_flags = O_RDWR;
283 file->f_pos = 0;
284 fd_install(fd, file);
287 out:
288 return fd;
291 extern __inline__ struct socket *socki_lookup(struct inode *inode)
293 return &inode->u.socket_i;
297 * Go from a file number to its socket slot.
300 extern struct socket *sockfd_lookup(int fd, int *err)
302 struct file *file;
303 struct inode *inode;
304 struct socket *sock;
306 if (!(file = fget(fd)))
308 *err = -EBADF;
309 return NULL;
312 inode = file->f_dentry->d_inode;
313 if (!inode || !inode->i_sock || !(sock = socki_lookup(inode)))
315 *err = -ENOTSOCK;
316 fput(file);
317 return NULL;
320 if (sock->file != file) {
321 printk(KERN_ERR "socki_lookup: socket file changed!\n");
322 sock->file = file;
324 return sock;
327 extern __inline__ void sockfd_put(struct socket *sock)
329 fput(sock->file);
333 * Allocate a socket.
336 struct socket *sock_alloc(void)
338 struct inode * inode;
339 struct socket * sock;
341 inode = get_empty_inode();
342 if (!inode)
343 return NULL;
345 sock = socki_lookup(inode);
347 inode->i_mode = S_IFSOCK|S_IRWXUGO;
348 inode->i_sock = 1;
349 inode->i_uid = current->fsuid;
350 inode->i_gid = current->fsgid;
352 sock->inode = inode;
353 init_waitqueue_head(&sock->wait);
354 sock->fasync_list = NULL;
355 sock->state = SS_UNCONNECTED;
356 sock->flags = 0;
357 sock->ops = NULL;
358 sock->sk = NULL;
359 sock->file = NULL;
361 sockets_in_use[smp_processor_id()].counter++;
362 return sock;
366 * In theory you can't get an open on this inode, but /proc provides
367 * a back door. Remember to keep it shut otherwise you'll let the
368 * creepy crawlies in.
371 static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
373 return -ENXIO;
376 void sock_release(struct socket *sock)
378 if (sock->ops)
379 sock->ops->release(sock);
381 if (sock->fasync_list)
382 printk(KERN_ERR "sock_release: fasync list not empty!\n");
384 sockets_in_use[smp_processor_id()].counter--;
385 if (!sock->file) {
386 iput(sock->inode);
387 return;
389 sock->file=NULL;
392 int sock_sendmsg(struct socket *sock, struct msghdr *msg, int size)
394 int err;
395 struct scm_cookie scm;
397 err = scm_send(sock, msg, &scm);
398 if (err >= 0) {
399 err = sock->ops->sendmsg(sock, msg, size, &scm);
400 scm_destroy(&scm);
402 return err;
405 int sock_recvmsg(struct socket *sock, struct msghdr *msg, int size, int flags)
407 struct scm_cookie scm;
409 memset(&scm, 0, sizeof(scm));
411 size = sock->ops->recvmsg(sock, msg, size, flags, &scm);
412 if (size >= 0)
413 scm_recv(sock, msg, &scm, flags);
415 return size;
420 * Sockets are not seekable.
423 static loff_t sock_lseek(struct file *file, loff_t offset, int whence)
425 return -ESPIPE;
429 * Read data from a socket. ubuf is a user mode pointer. We make sure the user
430 * area ubuf...ubuf+size-1 is writable before asking the protocol.
433 static ssize_t sock_read(struct file *file, char *ubuf,
434 size_t size, loff_t *ppos)
436 struct socket *sock;
437 struct iovec iov;
438 struct msghdr msg;
439 int flags;
441 if (ppos != &file->f_pos)
442 return -ESPIPE;
443 if (size==0) /* Match SYS5 behaviour */
444 return 0;
446 sock = socki_lookup(file->f_dentry->d_inode);
448 msg.msg_name=NULL;
449 msg.msg_namelen=0;
450 msg.msg_iov=&iov;
451 msg.msg_iovlen=1;
452 msg.msg_control=NULL;
453 msg.msg_controllen=0;
454 iov.iov_base=ubuf;
455 iov.iov_len=size;
456 flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
458 return sock_recvmsg(sock, &msg, size, flags);
463 * Write data to a socket. We verify that the user area ubuf..ubuf+size-1
464 * is readable by the user process.
467 static ssize_t sock_write(struct file *file, const char *ubuf,
468 size_t size, loff_t *ppos)
470 struct socket *sock;
471 struct msghdr msg;
472 struct iovec iov;
474 if (ppos != &file->f_pos)
475 return -ESPIPE;
476 if(size==0) /* Match SYS5 behaviour */
477 return 0;
479 sock = socki_lookup(file->f_dentry->d_inode);
481 msg.msg_name=NULL;
482 msg.msg_namelen=0;
483 msg.msg_iov=&iov;
484 msg.msg_iovlen=1;
485 msg.msg_control=NULL;
486 msg.msg_controllen=0;
487 msg.msg_flags=!(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
488 if (sock->type == SOCK_SEQPACKET)
489 msg.msg_flags |= MSG_EOR;
490 iov.iov_base=(void *)ubuf;
491 iov.iov_len=size;
493 return sock_sendmsg(sock, &msg, size);
496 int sock_readv_writev(int type, struct inode * inode, struct file * file,
497 const struct iovec * iov, long count, long size)
499 struct msghdr msg;
500 struct socket *sock;
502 sock = socki_lookup(inode);
504 msg.msg_name = NULL;
505 msg.msg_namelen = 0;
506 msg.msg_control = NULL;
507 msg.msg_controllen = 0;
508 msg.msg_iov = (struct iovec *) iov;
509 msg.msg_iovlen = count;
510 msg.msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
512 /* read() does a VERIFY_WRITE */
513 if (type == VERIFY_WRITE)
514 return sock_recvmsg(sock, &msg, size, msg.msg_flags);
516 if (sock->type == SOCK_SEQPACKET)
517 msg.msg_flags |= MSG_EOR;
519 return sock_sendmsg(sock, &msg, size);
522 static ssize_t sock_readv(struct file *file, const struct iovec *vector,
523 unsigned long count, loff_t *ppos)
525 size_t tot_len = 0;
526 int i;
527 for (i = 0 ; i < count ; i++)
528 tot_len += vector[i].iov_len;
529 return sock_readv_writev(VERIFY_WRITE, file->f_dentry->d_inode,
530 file, vector, count, tot_len);
533 static ssize_t sock_writev(struct file *file, const struct iovec *vector,
534 unsigned long count, loff_t *ppos)
536 size_t tot_len = 0;
537 int i;
538 for (i = 0 ; i < count ; i++)
539 tot_len += vector[i].iov_len;
540 return sock_readv_writev(VERIFY_READ, file->f_dentry->d_inode,
541 file, vector, count, tot_len);
545 * With an ioctl arg may well be a user mode pointer, but we don't know what to do
546 * with it - that's up to the protocol still.
549 int sock_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
550 unsigned long arg)
552 struct socket *sock;
553 int err;
555 unlock_kernel();
556 sock = socki_lookup(inode);
557 err = sock->ops->ioctl(sock, cmd, arg);
558 lock_kernel();
560 return err;
564 static unsigned int sock_poll(struct file *file, poll_table * wait)
566 struct socket *sock;
567 int err;
569 unlock_kernel();
570 sock = socki_lookup(file->f_dentry->d_inode);
573 * We can't return errors to poll, so it's either yes or no.
576 err = sock->ops->poll(file, sock, wait);
577 lock_kernel();
578 return err;
581 static int sock_mmap(struct file * file, struct vm_area_struct * vma)
583 struct socket *sock = socki_lookup(file->f_dentry->d_inode);
585 return sock->ops->mmap(file, sock, vma);
588 int sock_close(struct inode *inode, struct file *filp)
591 * It was possible the inode is NULL we were
592 * closing an unfinished socket.
595 if (!inode)
597 printk(KERN_DEBUG "sock_close: NULL inode\n");
598 return 0;
600 unlock_kernel();
601 sock_fasync(-1, filp, 0);
602 sock_release(socki_lookup(inode));
603 lock_kernel();
604 return 0;
608 * Update the socket async list
610 * Fasync_list locking strategy.
612 * 1. fasync_list is modified only under process context socket lock
613 * i.e. under semaphore.
614 * 2. fasync_list is used under read_lock(&sk->callback_lock)
615 * or under socket lock.
616 * 3. fasync_list can be used from softirq context, so that
617 * modification under socket lock have to be enhanced with
618 * write_lock_bh(&sk->callback_lock).
619 * --ANK (990710)
622 static int sock_fasync(int fd, struct file *filp, int on)
624 struct fasync_struct *fa, *fna=NULL, **prev;
625 struct socket *sock;
626 struct sock *sk;
628 if (on)
630 fna=(struct fasync_struct *)kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
631 if(fna==NULL)
632 return -ENOMEM;
636 sock = socki_lookup(filp->f_dentry->d_inode);
638 if ((sk=sock->sk) == NULL)
639 return -EINVAL;
641 lock_sock(sk);
643 prev=&(sock->fasync_list);
645 for (fa=*prev; fa!=NULL; prev=&fa->fa_next,fa=*prev)
646 if (fa->fa_file==filp)
647 break;
649 if(on)
651 if(fa!=NULL)
653 write_lock_bh(&sk->callback_lock);
654 fa->fa_fd=fd;
655 write_unlock_bh(&sk->callback_lock);
657 kfree_s(fna,sizeof(struct fasync_struct));
658 goto out;
660 fna->fa_file=filp;
661 fna->fa_fd=fd;
662 fna->magic=FASYNC_MAGIC;
663 fna->fa_next=sock->fasync_list;
664 write_lock_bh(&sk->callback_lock);
665 sock->fasync_list=fna;
666 write_unlock_bh(&sk->callback_lock);
668 else
670 if (fa!=NULL)
672 write_lock_bh(&sk->callback_lock);
673 *prev=fa->fa_next;
674 write_unlock_bh(&sk->callback_lock);
675 kfree_s(fa,sizeof(struct fasync_struct));
679 out:
680 release_sock(sock->sk);
681 return 0;
684 /* This function may be called only under socket lock or callback_lock */
686 int sock_wake_async(struct socket *sock, int how, int band)
688 if (!sock || !sock->fasync_list)
689 return -1;
690 switch (how)
692 case 1:
694 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
695 break;
696 goto call_kill;
697 case 2:
698 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
699 break;
700 /* fall through */
701 case 0:
702 call_kill:
703 kill_fasync(sock->fasync_list, SIGIO, band);
704 break;
705 case 3:
706 kill_fasync(sock->fasync_list, SIGURG, band);
708 return 0;
712 int sock_create(int family, int type, int protocol, struct socket **res)
714 int i;
715 struct socket *sock;
718 * Check protocol is in range
720 if(family<0 || family>=NPROTO)
721 return -EINVAL;
723 /* Compatibility.
725 This uglymoron is moved from INET layer to here to avoid
726 deadlock in module load.
728 if (family == PF_INET && type == SOCK_PACKET) {
729 static int warned;
730 if (!warned) {
731 warned = 1;
732 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n", current->comm);
734 family = PF_PACKET;
737 #if defined(CONFIG_KMOD) && defined(CONFIG_NET)
738 /* Attempt to load a protocol module if the find failed.
740 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
741 * requested real, full-featured networking support upon configuration.
742 * Otherwise module support will break!
744 if (net_families[family]==NULL)
746 char module_name[30];
747 sprintf(module_name,"net-pf-%d",family);
748 request_module(module_name);
750 #endif
752 net_family_read_lock();
753 if (net_families[family] == NULL) {
754 i = -EINVAL;
755 goto out;
759 * Allocate the socket and allow the family to set things up. if
760 * the protocol is 0, the family is instructed to select an appropriate
761 * default.
764 if (!(sock = sock_alloc()))
766 printk(KERN_WARNING "socket: no more sockets\n");
767 i = -ENFILE; /* Not exactly a match, but its the
768 closest posix thing */
769 goto out;
772 sock->type = type;
774 if ((i = net_families[family]->create(sock, protocol)) < 0)
776 sock_release(sock);
777 goto out;
780 *res = sock;
782 out:
783 net_family_read_unlock();
784 return i;
787 asmlinkage long sys_socket(int family, int type, int protocol)
789 int retval;
790 struct socket *sock;
792 retval = sock_create(family, type, protocol, &sock);
793 if (retval < 0)
794 goto out;
796 retval = sock_map_fd(sock);
797 if (retval < 0)
798 goto out_release;
800 out:
801 /* It may be already another descriptor 8) Not kernel problem. */
802 return retval;
804 out_release:
805 sock_release(sock);
806 return retval;
810 * Create a pair of connected sockets.
813 asmlinkage long sys_socketpair(int family, int type, int protocol, int usockvec[2])
815 struct socket *sock1, *sock2;
816 int fd1, fd2, err;
819 * Obtain the first socket and check if the underlying protocol
820 * supports the socketpair call.
823 err = sock_create(family, type, protocol, &sock1);
824 if (err < 0)
825 goto out;
827 err = sock_create(family, type, protocol, &sock2);
828 if (err < 0)
829 goto out_release_1;
831 err = sock1->ops->socketpair(sock1, sock2);
832 if (err < 0)
833 goto out_release_both;
835 fd1 = fd2 = -1;
837 err = sock_map_fd(sock1);
838 if (err < 0)
839 goto out_release_both;
840 fd1 = err;
842 err = sock_map_fd(sock2);
843 if (err < 0)
844 goto out_close_1;
845 fd2 = err;
847 /* fd1 and fd2 may be already another descriptors.
848 * Not kernel problem.
851 err = put_user(fd1, &usockvec[0]);
852 if (!err)
853 err = put_user(fd2, &usockvec[1]);
854 if (!err)
855 return 0;
857 sys_close(fd2);
858 sys_close(fd1);
859 return err;
861 out_close_1:
862 sock_release(sock2);
863 sys_close(fd1);
864 return err;
866 out_release_both:
867 sock_release(sock2);
868 out_release_1:
869 sock_release(sock1);
870 out:
871 return err;
876 * Bind a name to a socket. Nothing much to do here since it's
877 * the protocol's responsibility to handle the local address.
879 * We move the socket address to kernel space before we call
880 * the protocol layer (having also checked the address is ok).
883 asmlinkage long sys_bind(int fd, struct sockaddr *umyaddr, int addrlen)
885 struct socket *sock;
886 char address[MAX_SOCK_ADDR];
887 int err;
889 if((sock = sockfd_lookup(fd,&err))!=NULL)
891 if((err=move_addr_to_kernel(umyaddr,addrlen,address))>=0)
892 err = sock->ops->bind(sock, (struct sockaddr *)address, addrlen);
893 sockfd_put(sock);
895 return err;
900 * Perform a listen. Basically, we allow the protocol to do anything
901 * necessary for a listen, and if that works, we mark the socket as
902 * ready for listening.
905 asmlinkage long sys_listen(int fd, int backlog)
907 struct socket *sock;
908 int err;
910 if ((sock = sockfd_lookup(fd, &err)) != NULL) {
911 if ((unsigned) backlog > SOMAXCONN)
912 backlog = SOMAXCONN;
913 err=sock->ops->listen(sock, backlog);
914 sockfd_put(sock);
916 return err;
921 * For accept, we attempt to create a new socket, set up the link
922 * with the client, wake up the client, then return the new
923 * connected fd. We collect the address of the connector in kernel
924 * space and move it to user at the very end. This is unclean because
925 * we open the socket then return an error.
927 * 1003.1g adds the ability to recvmsg() to query connection pending
928 * status to recvmsg. We need to add that support in a way thats
929 * clean when we restucture accept also.
932 asmlinkage long sys_accept(int fd, struct sockaddr *upeer_sockaddr, int *upeer_addrlen)
934 struct socket *sock, *newsock;
935 int err, len;
936 char address[MAX_SOCK_ADDR];
938 sock = sockfd_lookup(fd, &err);
939 if (!sock)
940 goto out;
942 err = -EMFILE;
943 if (!(newsock = sock_alloc()))
944 goto out_put;
946 newsock->type = sock->type;
947 newsock->ops = sock->ops;
949 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
950 if (err < 0)
951 goto out_release;
953 if (upeer_sockaddr) {
954 if(newsock->ops->getname(newsock, (struct sockaddr *)address, &len, 2)<0) {
955 err = -ECONNABORTED;
956 goto out_release;
958 err = move_addr_to_user(address, len, upeer_sockaddr, upeer_addrlen);
959 if (err < 0)
960 goto out_release;
963 /* File flags are not inherited via accept() unlike another OSes. */
965 if ((err = sock_map_fd(newsock)) < 0)
966 goto out_release;
968 out_put:
969 sockfd_put(sock);
970 out:
971 return err;
973 out_release:
974 sock_release(newsock);
975 goto out_put;
980 * Attempt to connect to a socket with the server address. The address
981 * is in user space so we verify it is OK and move it to kernel space.
983 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
984 * break bindings
986 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
987 * other SEQPACKET protocols that take time to connect() as it doesn't
988 * include the -EINPROGRESS status for such sockets.
991 asmlinkage long sys_connect(int fd, struct sockaddr *uservaddr, int addrlen)
993 struct socket *sock;
994 char address[MAX_SOCK_ADDR];
995 int err;
997 sock = sockfd_lookup(fd, &err);
998 if (!sock)
999 goto out;
1000 err = move_addr_to_kernel(uservaddr, addrlen, address);
1001 if (err < 0)
1002 goto out_put;
1003 err = sock->ops->connect(sock, (struct sockaddr *) address, addrlen,
1004 sock->file->f_flags);
1005 out_put:
1006 sockfd_put(sock);
1007 out:
1008 return err;
1012 * Get the local address ('name') of a socket object. Move the obtained
1013 * name to user space.
1016 asmlinkage long sys_getsockname(int fd, struct sockaddr *usockaddr, int *usockaddr_len)
1018 struct socket *sock;
1019 char address[MAX_SOCK_ADDR];
1020 int len, err;
1022 sock = sockfd_lookup(fd, &err);
1023 if (!sock)
1024 goto out;
1025 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 0);
1026 if (err)
1027 goto out_put;
1028 err = move_addr_to_user(address, len, usockaddr, usockaddr_len);
1030 out_put:
1031 sockfd_put(sock);
1032 out:
1033 return err;
1037 * Get the remote address ('name') of a socket object. Move the obtained
1038 * name to user space.
1041 asmlinkage long sys_getpeername(int fd, struct sockaddr *usockaddr, int *usockaddr_len)
1043 struct socket *sock;
1044 char address[MAX_SOCK_ADDR];
1045 int len, err;
1047 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1049 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 1);
1050 if (!err)
1051 err=move_addr_to_user(address,len, usockaddr, usockaddr_len);
1052 sockfd_put(sock);
1054 return err;
1058 * Send a datagram to a given address. We move the address into kernel
1059 * space and check the user space data area is readable before invoking
1060 * the protocol.
1063 asmlinkage long sys_sendto(int fd, void * buff, size_t len, unsigned flags,
1064 struct sockaddr *addr, int addr_len)
1066 struct socket *sock;
1067 char address[MAX_SOCK_ADDR];
1068 int err;
1069 struct msghdr msg;
1070 struct iovec iov;
1072 sock = sockfd_lookup(fd, &err);
1073 if (!sock)
1074 goto out;
1075 iov.iov_base=buff;
1076 iov.iov_len=len;
1077 msg.msg_name=NULL;
1078 msg.msg_iov=&iov;
1079 msg.msg_iovlen=1;
1080 msg.msg_control=NULL;
1081 msg.msg_controllen=0;
1082 msg.msg_namelen=addr_len;
1083 if(addr)
1085 err = move_addr_to_kernel(addr, addr_len, address);
1086 if (err < 0)
1087 goto out_put;
1088 msg.msg_name=address;
1090 if (sock->file->f_flags & O_NONBLOCK)
1091 flags |= MSG_DONTWAIT;
1092 msg.msg_flags = flags;
1093 err = sock_sendmsg(sock, &msg, len);
1095 out_put:
1096 sockfd_put(sock);
1097 out:
1098 return err;
1102 * Send a datagram down a socket.
1105 asmlinkage long sys_send(int fd, void * buff, size_t len, unsigned flags)
1107 return sys_sendto(fd, buff, len, flags, NULL, 0);
1111 * Receive a frame from the socket and optionally record the address of the
1112 * sender. We verify the buffers are writable and if needed move the
1113 * sender address from kernel to user space.
1116 asmlinkage long sys_recvfrom(int fd, void * ubuf, size_t size, unsigned flags,
1117 struct sockaddr *addr, int *addr_len)
1119 struct socket *sock;
1120 struct iovec iov;
1121 struct msghdr msg;
1122 char address[MAX_SOCK_ADDR];
1123 int err,err2;
1125 sock = sockfd_lookup(fd, &err);
1126 if (!sock)
1127 goto out;
1129 msg.msg_control=NULL;
1130 msg.msg_controllen=0;
1131 msg.msg_iovlen=1;
1132 msg.msg_iov=&iov;
1133 iov.iov_len=size;
1134 iov.iov_base=ubuf;
1135 msg.msg_name=address;
1136 msg.msg_namelen=MAX_SOCK_ADDR;
1137 if (sock->file->f_flags & O_NONBLOCK)
1138 flags |= MSG_DONTWAIT;
1139 err=sock_recvmsg(sock, &msg, size, flags);
1141 if(err >= 0 && addr != NULL && msg.msg_namelen)
1143 err2=move_addr_to_user(address, msg.msg_namelen, addr, addr_len);
1144 if(err2<0)
1145 err=err2;
1147 sockfd_put(sock);
1148 out:
1149 return err;
1153 * Receive a datagram from a socket.
1156 asmlinkage long sys_recv(int fd, void * ubuf, size_t size, unsigned flags)
1158 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1162 * Set a socket option. Because we don't know the option lengths we have
1163 * to pass the user mode parameter for the protocols to sort out.
1166 asmlinkage long sys_setsockopt(int fd, int level, int optname, char *optval, int optlen)
1168 int err;
1169 struct socket *sock;
1171 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1173 if (level == SOL_SOCKET)
1174 err=sock_setsockopt(sock,level,optname,optval,optlen);
1175 else
1176 err=sock->ops->setsockopt(sock, level, optname, optval, optlen);
1177 sockfd_put(sock);
1179 return err;
1183 * Get a socket option. Because we don't know the option lengths we have
1184 * to pass a user mode parameter for the protocols to sort out.
1187 asmlinkage long sys_getsockopt(int fd, int level, int optname, char *optval, int *optlen)
1189 int err;
1190 struct socket *sock;
1192 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1194 if (level == SOL_SOCKET)
1195 err=sock_getsockopt(sock,level,optname,optval,optlen);
1196 else
1197 err=sock->ops->getsockopt(sock, level, optname, optval, optlen);
1198 sockfd_put(sock);
1200 return err;
1205 * Shutdown a socket.
1208 asmlinkage long sys_shutdown(int fd, int how)
1210 int err;
1211 struct socket *sock;
1213 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1215 err=sock->ops->shutdown(sock, how);
1216 sockfd_put(sock);
1218 return err;
1222 * BSD sendmsg interface
1225 asmlinkage long sys_sendmsg(int fd, struct msghdr *msg, unsigned flags)
1227 struct socket *sock;
1228 char address[MAX_SOCK_ADDR];
1229 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1230 unsigned char ctl[sizeof(struct cmsghdr) + 20]; /* 20 is size of ipv6_pktinfo */
1231 unsigned char *ctl_buf = ctl;
1232 struct msghdr msg_sys;
1233 int err, ctl_len, iov_size, total_len;
1235 err = -EFAULT;
1236 if (copy_from_user(&msg_sys,msg,sizeof(struct msghdr)))
1237 goto out;
1239 sock = sockfd_lookup(fd, &err);
1240 if (!sock)
1241 goto out;
1243 /* do not move before msg_sys is valid */
1244 err = -EINVAL;
1245 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1246 goto out_put;
1248 /* Check whether to allocate the iovec area*/
1249 err = -ENOMEM;
1250 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1251 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1252 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1253 if (!iov)
1254 goto out_put;
1257 /* This will also move the address data into kernel space */
1258 err = verify_iovec(&msg_sys, iov, address, VERIFY_READ);
1259 if (err < 0)
1260 goto out_freeiov;
1261 total_len = err;
1263 err = -ENOBUFS;
1265 if (msg_sys.msg_controllen > INT_MAX)
1266 goto out_freeiov;
1267 ctl_len = msg_sys.msg_controllen;
1268 if (ctl_len)
1270 if (ctl_len > sizeof(ctl))
1272 err = -ENOBUFS;
1273 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
1274 if (ctl_buf == NULL)
1275 goto out_freeiov;
1277 err = -EFAULT;
1278 if (copy_from_user(ctl_buf, msg_sys.msg_control, ctl_len))
1279 goto out_freectl;
1280 msg_sys.msg_control = ctl_buf;
1282 msg_sys.msg_flags = flags;
1284 if (sock->file->f_flags & O_NONBLOCK)
1285 msg_sys.msg_flags |= MSG_DONTWAIT;
1286 err = sock_sendmsg(sock, &msg_sys, total_len);
1288 out_freectl:
1289 if (ctl_buf != ctl)
1290 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1291 out_freeiov:
1292 if (iov != iovstack)
1293 sock_kfree_s(sock->sk, iov, iov_size);
1294 out_put:
1295 sockfd_put(sock);
1296 out:
1297 return err;
1301 * BSD recvmsg interface
1304 asmlinkage long sys_recvmsg(int fd, struct msghdr *msg, unsigned int flags)
1306 struct socket *sock;
1307 struct iovec iovstack[UIO_FASTIOV];
1308 struct iovec *iov=iovstack;
1309 struct msghdr msg_sys;
1310 unsigned long cmsg_ptr;
1311 int err, iov_size, total_len, len;
1313 /* kernel mode address */
1314 char addr[MAX_SOCK_ADDR];
1316 /* user mode address pointers */
1317 struct sockaddr *uaddr;
1318 int *uaddr_len;
1320 err=-EFAULT;
1321 if (copy_from_user(&msg_sys,msg,sizeof(struct msghdr)))
1322 goto out;
1324 sock = sockfd_lookup(fd, &err);
1325 if (!sock)
1326 goto out;
1328 err = -EINVAL;
1329 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1330 goto out_put;
1332 /* Check whether to allocate the iovec area*/
1333 err = -ENOMEM;
1334 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1335 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1336 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1337 if (!iov)
1338 goto out_put;
1342 * Save the user-mode address (verify_iovec will change the
1343 * kernel msghdr to use the kernel address space)
1346 uaddr = msg_sys.msg_name;
1347 uaddr_len = &msg->msg_namelen;
1348 err = verify_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1349 if (err < 0)
1350 goto out_freeiov;
1351 total_len=err;
1353 cmsg_ptr = (unsigned long)msg_sys.msg_control;
1354 msg_sys.msg_flags = 0;
1356 if (sock->file->f_flags & O_NONBLOCK)
1357 flags |= MSG_DONTWAIT;
1358 err = sock_recvmsg(sock, &msg_sys, total_len, flags);
1359 if (err < 0)
1360 goto out_freeiov;
1361 len = err;
1363 if (uaddr != NULL && msg_sys.msg_namelen) {
1364 err = move_addr_to_user(addr, msg_sys.msg_namelen, uaddr, uaddr_len);
1365 if (err < 0)
1366 goto out_freeiov;
1368 err = __put_user(msg_sys.msg_flags, &msg->msg_flags);
1369 if (err)
1370 goto out_freeiov;
1371 err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr,
1372 &msg->msg_controllen);
1373 if (err)
1374 goto out_freeiov;
1375 err = len;
1377 out_freeiov:
1378 if (iov != iovstack)
1379 sock_kfree_s(sock->sk, iov, iov_size);
1380 out_put:
1381 sockfd_put(sock);
1382 out:
1383 return err;
1388 * Perform a file control on a socket file descriptor.
1390 * Doesn't aquire a fd lock, because no network fcntl
1391 * function sleeps currently.
1394 int sock_fcntl(struct file *filp, unsigned int cmd, unsigned long arg)
1396 struct socket *sock;
1398 sock = socki_lookup (filp->f_dentry->d_inode);
1399 if (sock && sock->ops)
1400 return sock->ops->fcntl(sock, cmd, arg);
1401 return(-EINVAL);
1404 /* Argument list sizes for sys_socketcall */
1405 #define AL(x) ((x) * sizeof(unsigned long))
1406 static unsigned char nargs[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
1407 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
1408 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)};
1409 #undef AL
1412 * System call vectors.
1414 * Argument checking cleaned up. Saved 20% in size.
1415 * This function doesn't need to set the kernel lock because
1416 * it is set by the callees.
1419 asmlinkage long sys_socketcall(int call, unsigned long *args)
1421 unsigned long a[6];
1422 unsigned long a0,a1;
1423 int err;
1425 if(call<1||call>SYS_RECVMSG)
1426 return -EINVAL;
1428 /* copy_from_user should be SMP safe. */
1429 if (copy_from_user(a, args, nargs[call]))
1430 return -EFAULT;
1432 a0=a[0];
1433 a1=a[1];
1435 switch(call)
1437 case SYS_SOCKET:
1438 err = sys_socket(a0,a1,a[2]);
1439 break;
1440 case SYS_BIND:
1441 err = sys_bind(a0,(struct sockaddr *)a1, a[2]);
1442 break;
1443 case SYS_CONNECT:
1444 err = sys_connect(a0, (struct sockaddr *)a1, a[2]);
1445 break;
1446 case SYS_LISTEN:
1447 err = sys_listen(a0,a1);
1448 break;
1449 case SYS_ACCEPT:
1450 err = sys_accept(a0,(struct sockaddr *)a1, (int *)a[2]);
1451 break;
1452 case SYS_GETSOCKNAME:
1453 err = sys_getsockname(a0,(struct sockaddr *)a1, (int *)a[2]);
1454 break;
1455 case SYS_GETPEERNAME:
1456 err = sys_getpeername(a0, (struct sockaddr *)a1, (int *)a[2]);
1457 break;
1458 case SYS_SOCKETPAIR:
1459 err = sys_socketpair(a0,a1, a[2], (int *)a[3]);
1460 break;
1461 case SYS_SEND:
1462 err = sys_send(a0, (void *)a1, a[2], a[3]);
1463 break;
1464 case SYS_SENDTO:
1465 err = sys_sendto(a0,(void *)a1, a[2], a[3],
1466 (struct sockaddr *)a[4], a[5]);
1467 break;
1468 case SYS_RECV:
1469 err = sys_recv(a0, (void *)a1, a[2], a[3]);
1470 break;
1471 case SYS_RECVFROM:
1472 err = sys_recvfrom(a0, (void *)a1, a[2], a[3],
1473 (struct sockaddr *)a[4], (int *)a[5]);
1474 break;
1475 case SYS_SHUTDOWN:
1476 err = sys_shutdown(a0,a1);
1477 break;
1478 case SYS_SETSOCKOPT:
1479 err = sys_setsockopt(a0, a1, a[2], (char *)a[3], a[4]);
1480 break;
1481 case SYS_GETSOCKOPT:
1482 err = sys_getsockopt(a0, a1, a[2], (char *)a[3], (int *)a[4]);
1483 break;
1484 case SYS_SENDMSG:
1485 err = sys_sendmsg(a0, (struct msghdr *) a1, a[2]);
1486 break;
1487 case SYS_RECVMSG:
1488 err = sys_recvmsg(a0, (struct msghdr *) a1, a[2]);
1489 break;
1490 default:
1491 err = -EINVAL;
1492 break;
1494 return err;
1498 * This function is called by a protocol handler that wants to
1499 * advertise its address family, and have it linked into the
1500 * SOCKET module.
1503 int sock_register(struct net_proto_family *ops)
1505 int err;
1507 if (ops->family >= NPROTO) {
1508 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
1509 return -ENOBUFS;
1511 net_family_write_lock();
1512 err = -EEXIST;
1513 if (net_families[ops->family] == NULL) {
1514 net_families[ops->family]=ops;
1515 err = 0;
1517 net_family_write_unlock();
1518 return err;
1522 * This function is called by a protocol handler that wants to
1523 * remove its address family, and have it unlinked from the
1524 * SOCKET module.
1527 int sock_unregister(int family)
1529 if (family < 0 || family >= NPROTO)
1530 return -1;
1532 net_family_write_lock();
1533 net_families[family]=NULL;
1534 net_family_write_unlock();
1535 return 0;
1538 void __init proto_init(void)
1540 extern struct net_proto protocols[]; /* Network protocols */
1541 struct net_proto *pro;
1543 /* Kick all configured protocols. */
1544 pro = protocols;
1545 while (pro->name != NULL)
1547 (*pro->init_func)(pro);
1548 pro++;
1550 /* We're all done... */
1553 extern void sk_init(void);
1554 #ifdef CONFIG_WAN_ROUTER
1555 extern void wanrouter_init(void);
1556 #endif
1558 void __init sock_init(void)
1560 int i;
1562 printk(KERN_INFO "Linux NET4.0 for Linux 2.3\n");
1563 printk(KERN_INFO "Based upon Swansea University Computer Society NET3.039\n");
1566 * Initialize all address (protocol) families.
1569 for (i = 0; i < NPROTO; i++)
1570 net_families[i] = NULL;
1573 * Initialize sock SLAB cache.
1576 sk_init();
1578 #ifdef SLAB_SKB
1580 * Initialize skbuff SLAB cache
1582 skb_init();
1583 #endif
1587 * Wan router layer.
1590 #ifdef CONFIG_WAN_ROUTER
1591 wanrouter_init();
1592 #endif
1595 * Initialize the protocols module.
1598 proto_init();
1601 * The netlink device handler may be needed early.
1604 #ifdef CONFIG_RTNETLINK
1605 rtnetlink_init();
1606 #endif
1607 #ifdef CONFIG_NETLINK_DEV
1608 init_netlink();
1609 #endif
1610 #ifdef CONFIG_NETFILTER
1611 netfilter_init();
1612 #endif
1615 int socket_get_info(char *buffer, char **start, off_t offset, int length)
1617 int len, cpu;
1618 int counter = 0;
1620 for (cpu=0; cpu<smp_num_cpus; cpu++)
1621 counter += sockets_in_use[cpu].counter;
1623 /* It can be negative, by the way. 8) */
1624 if (counter < 0)
1625 counter = 0;
1627 len = sprintf(buffer, "sockets: used %d\n", counter);
1628 if (offset >= len)
1630 *start = buffer;
1631 return 0;
1633 *start = buffer + offset;
1634 len -= offset;
1635 if (len > length)
1636 len = length;
1637 if (len < 0)
1638 len = 0;
1639 return len;