Import 1.3.16
[davej-history.git] / net / socket.c
bloba644878cb3431da118f10625af30248188e3fd79
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.
38 * This program is free software; you can redistribute it and/or
39 * modify it under the terms of the GNU General Public License
40 * as published by the Free Software Foundation; either version
41 * 2 of the License, or (at your option) any later version.
44 * This module is effectively the top level interface to the BSD socket
45 * paradigm. Because it is very simple it works well for Unix domain sockets,
46 * but requires a whole layer of substructure for the other protocols.
48 * In addition it lacks an effective kernel -> kernel interface to go with
49 * the user one.
52 #include <linux/config.h>
53 #include <linux/signal.h>
54 #include <linux/errno.h>
55 #include <linux/sched.h>
56 #include <linux/mm.h>
57 #include <linux/kernel.h>
58 #include <linux/major.h>
59 #include <linux/stat.h>
60 #include <linux/socket.h>
61 #include <linux/fcntl.h>
62 #include <linux/net.h>
63 #include <linux/interrupt.h>
64 #include <linux/netdevice.h>
66 #include <asm/system.h>
67 #include <asm/segment.h>
69 static intsock_lseek(struct inode *inode,struct file *file, off_t offset,
70 int whence);
71 static intsock_read(struct inode *inode,struct file *file,char*buf,
72 int size);
73 static intsock_write(struct inode *inode,struct file *file,const char*buf,
74 int size);
76 static voidsock_close(struct inode *inode,struct file *file);
77 static intsock_select(struct inode *inode,struct file *file,int which, select_table *seltable);
78 static intsock_ioctl(struct inode *inode,struct file *file,
79 unsigned int cmd,unsigned long arg);
80 static intsock_fasync(struct inode *inode,struct file *filp,int on);
85 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
86 * in the operation structures but are done directly via the socketcall() multiplexor.
89 static struct file_operations socket_file_ops = {
90 sock_lseek,
91 sock_read,
92 sock_write,
93 NULL,/* readdir */
94 sock_select,
95 sock_ioctl,
96 NULL,/* mmap */
97 NULL,/* no special open code... */
98 sock_close,
99 NULL,/* no fsync */
100 sock_fasync
104 * The protocol list. Each protocol is registered in here.
106 static struct proto_ops *pops[NPROTO];
108 * Statistics counters of the socket lists
110 static int sockets_in_use =0;
113 * Support routines. Move socket addresses back and forth across the kernel/user
114 * divide and look after the messy bits.
117 #define MAX_SOCK_ADDR 128/* 108 for Unix domain - 16 for IP, 16 for IPX, about 80 for AX.25 */
119 intmove_addr_to_kernel(void*uaddr,int ulen,void*kaddr)
121 int err;
122 if(ulen<0||ulen>MAX_SOCK_ADDR)
123 return-EINVAL;
124 if(ulen==0)
125 return0;
126 if((err=verify_area(VERIFY_READ,uaddr,ulen))<0)
127 return err;
128 memcpy_fromfs(kaddr,uaddr,ulen);
129 return0;
132 intmove_addr_to_user(void*kaddr,int klen,void*uaddr,int*ulen)
134 int err;
135 int len;
138 if((err=verify_area(VERIFY_WRITE,ulen,sizeof(*ulen)))<0)
139 return err;
140 len=get_user(ulen);
141 if(len>klen)
142 len=klen;
143 if(len<0|| len> MAX_SOCK_ADDR)
144 return-EINVAL;
145 if(len)
147 if((err=verify_area(VERIFY_WRITE,uaddr,len))<0)
148 return err;
149 memcpy_tofs(uaddr,kaddr,len);
151 put_user(len,ulen);
152 return0;
156 * Obtains the first available file descriptor and sets it up for use.
159 static intget_fd(struct inode *inode)
161 int fd;
162 struct file *file;
165 * Find a file descriptor suitable for return to the user.
168 file =get_empty_filp();
169 if(!file)
170 return(-1);
172 for(fd =0; fd < NR_OPEN; ++fd)
173 if(!current->files->fd[fd])
174 break;
175 if(fd == NR_OPEN)
177 file->f_count =0;
178 return(-1);
181 FD_CLR(fd, &current->files->close_on_exec);
182 current->files->fd[fd] = file;
183 file->f_op = &socket_file_ops;
184 file->f_mode =3;
185 file->f_flags = O_RDWR;
186 file->f_count =1;
187 file->f_inode = inode;
188 if(inode)
189 inode->i_count++;
190 file->f_pos =0;
191 return(fd);
196 * Go from an inode to its socket slot.
198 * The original socket implementation wasn't very clever, which is
199 * why this exists at all..
201 inlinestruct socket *socki_lookup(struct inode *inode)
203 return&inode->u.socket_i;
207 * Go from a file number to its socket slot.
210 staticinlinestruct socket *sockfd_lookup(int fd,struct file **pfile)
212 struct file *file;
213 struct inode *inode;
215 if(fd <0|| fd >= NR_OPEN || !(file = current->files->fd[fd]))
216 return NULL;
218 inode = file->f_inode;
219 if(!inode || !inode->i_sock)
220 return NULL;
222 if(pfile)
223 *pfile = file;
225 returnsocki_lookup(inode);
229 * Allocate a socket.
232 struct socket *sock_alloc(void)
234 struct inode * inode;
235 struct socket * sock;
237 inode =get_empty_inode();
238 if(!inode)
239 return NULL;
241 inode->i_mode = S_IFSOCK;
242 inode->i_sock =1;
243 inode->i_uid = current->uid;
244 inode->i_gid = current->gid;
246 sock = &inode->u.socket_i;
247 sock->state = SS_UNCONNECTED;
248 sock->flags =0;
249 sock->ops = NULL;
250 sock->data = NULL;
251 sock->conn = NULL;
252 sock->iconn = NULL;
253 sock->next = NULL;
254 sock->wait = &inode->i_wait;
255 sock->inode = inode;/* "backlink": we could use pointer arithmetic instead */
256 sock->fasync_list = NULL;
257 sockets_in_use++;
258 return sock;
262 * Release a socket.
265 staticinlinevoidsock_release_peer(struct socket *peer)
267 peer->state = SS_DISCONNECTING;
268 wake_up_interruptible(peer->wait);
269 sock_wake_async(peer,1);
272 voidsock_release(struct socket *sock)
274 int oldstate;
275 struct socket *peersock, *nextsock;
277 if((oldstate = sock->state) != SS_UNCONNECTED)
278 sock->state = SS_DISCONNECTING;
281 * Wake up anyone waiting for connections.
284 for(peersock = sock->iconn; peersock; peersock = nextsock)
286 nextsock = peersock->next;
287 sock_release_peer(peersock);
291 * Wake up anyone we're connected to. First, we release the
292 * protocol, to give it a chance to flush data, etc.
295 peersock = (oldstate == SS_CONNECTED) ? sock->conn : NULL;
296 if(sock->ops)
297 sock->ops->release(sock, peersock);
298 if(peersock)
299 sock_release_peer(peersock);
300 --sockets_in_use;/* Bookkeeping.. */
301 iput(SOCK_INODE(sock));
305 * Sockets are not seekable.
308 static intsock_lseek(struct inode *inode,struct file *file, off_t offset,int whence)
310 return(-ESPIPE);
314 * Read data from a socket. ubuf is a user mode pointer. We make sure the user
315 * area ubuf...ubuf+size-1 is writable before asking the protocol.
318 static intsock_read(struct inode *inode,struct file *file,char*ubuf,int size)
320 struct socket *sock;
321 int err;
323 sock =socki_lookup(inode);
324 if(sock->flags & SO_ACCEPTCON)
325 return(-EINVAL);
327 if(size<0)
328 return-EINVAL;
329 if(size==0)/* Match SYS5 behaviour */
330 return0;
331 if((err=verify_area(VERIFY_WRITE,ubuf,size))<0)
332 return err;
333 return(sock->ops->read(sock, ubuf, size, (file->f_flags & O_NONBLOCK)));
337 * Write data to a socket. We verify that the user area ubuf..ubuf+size-1 is
338 * readable by the user process.
341 static intsock_write(struct inode *inode,struct file *file,const char*ubuf,int size)
343 struct socket *sock;
344 int err;
346 sock =socki_lookup(inode);
348 if(sock->flags & SO_ACCEPTCON)
349 return(-EINVAL);
351 if(size<0)
352 return-EINVAL;
353 if(size==0)/* Match SYS5 behaviour */
354 return0;
356 if((err=verify_area(VERIFY_READ,ubuf,size))<0)
357 return err;
358 return(sock->ops->write(sock, ubuf, size,(file->f_flags & O_NONBLOCK)));
362 * With an ioctl arg may well be a user mode pointer, but we don't know what to do
363 * with it - thats up to the protocol still.
366 intsock_ioctl(struct inode *inode,struct file *file,unsigned int cmd,
367 unsigned long arg)
369 struct socket *sock;
370 sock =socki_lookup(inode);
371 return(sock->ops->ioctl(sock, cmd, arg));
375 static intsock_select(struct inode *inode,struct file *file,int sel_type, select_table * wait)
377 struct socket *sock;
379 sock =socki_lookup(inode);
382 * We can't return errors to select, so it's either yes or no.
385 if(sock->ops->select)
386 return(sock->ops->select(sock, sel_type, wait));
387 return(0);
391 voidsock_close(struct inode *inode,struct file *filp)
394 * It's possible the inode is NULL if we're closing an unfinished socket.
397 if(!inode)
398 return;
399 sock_fasync(inode, filp,0);
400 sock_release(socki_lookup(inode));
404 * Update the socket async list
407 static intsock_fasync(struct inode *inode,struct file *filp,int on)
409 struct fasync_struct *fa, *fna=NULL, **prev;
410 struct socket *sock;
411 unsigned long flags;
413 if(on)
415 fna=(struct fasync_struct *)kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
416 if(fna==NULL)
417 return-ENOMEM;
420 sock =socki_lookup(inode);
422 prev=&(sock->fasync_list);
424 save_flags(flags);
425 cli();
427 for(fa=*prev; fa!=NULL; prev=&fa->fa_next,fa=*prev)
428 if(fa->fa_file==filp)
429 break;
431 if(on)
433 if(fa!=NULL)
435 kfree_s(fna,sizeof(struct fasync_struct));
436 restore_flags(flags);
437 return0;
439 fna->fa_file=filp;
440 fna->magic=FASYNC_MAGIC;
441 fna->fa_next=sock->fasync_list;
442 sock->fasync_list=fna;
444 else
446 if(fa!=NULL)
448 *prev=fa->fa_next;
449 kfree_s(fa,sizeof(struct fasync_struct));
452 restore_flags(flags);
453 return0;
456 intsock_wake_async(struct socket *sock,int how)
458 if(!sock || !sock->fasync_list)
459 return-1;
460 switch(how)
462 case0:
463 kill_fasync(sock->fasync_list, SIGIO);
464 break;
465 case1:
466 if(!(sock->flags & SO_WAITDATA))
467 kill_fasync(sock->fasync_list, SIGIO);
468 break;
469 case2:
470 if(sock->flags & SO_NOSPACE)
472 kill_fasync(sock->fasync_list, SIGIO);
473 sock->flags &= ~SO_NOSPACE;
475 break;
477 return0;
482 * Wait for a connection.
485 intsock_awaitconn(struct socket *mysock,struct socket *servsock,int flags)
487 struct socket *last;
490 * We must be listening
492 if(!(servsock->flags & SO_ACCEPTCON))
494 return(-EINVAL);
498 * Put ourselves on the server's incomplete connection queue.
501 mysock->next = NULL;
502 cli();
503 if(!(last = servsock->iconn))
504 servsock->iconn = mysock;
505 else
507 while(last->next)
508 last = last->next;
509 last->next = mysock;
511 mysock->state = SS_CONNECTING;
512 mysock->conn = servsock;
513 sti();
516 * Wake up server, then await connection. server will set state to
517 * SS_CONNECTED if we're connected.
519 wake_up_interruptible(servsock->wait);
520 sock_wake_async(servsock,0);
522 if(mysock->state != SS_CONNECTED)
524 if(flags & O_NONBLOCK)
525 return-EINPROGRESS;
527 interruptible_sleep_on(mysock->wait);
528 if(mysock->state != SS_CONNECTED &&
529 mysock->state != SS_DISCONNECTING)
532 * if we're not connected we could have been
533 * 1) interrupted, so we need to remove ourselves
534 * from the server list
535 * 2) rejected (mysock->conn == NULL), and have
536 * already been removed from the list
538 if(mysock->conn == servsock)
540 cli();
541 if((last = servsock->iconn) == mysock)
542 servsock->iconn = mysock->next;
543 else
545 while(last->next != mysock)
546 last = last->next;
547 last->next = mysock->next;
549 sti();
551 return(mysock->conn ? -EINTR : -EACCES);
554 return(0);
559 * Perform the socket system call. we locate the appropriate
560 * family, then create a fresh socket.
563 asmlinkage intsys_socket(int family,int type,int protocol)
565 int i, fd;
566 struct socket *sock;
567 struct proto_ops *ops;
569 /* Locate the correct protocol family. */
570 for(i =0; i < NPROTO; ++i)
572 if(pops[i] == NULL)continue;
573 if(pops[i]->family == family)
574 break;
577 if(i == NPROTO)
579 return-EINVAL;
582 ops = pops[i];
585 * Check that this is a type that we know how to manipulate and
586 * the protocol makes sense here. The family can still reject the
587 * protocol later.
590 if((type != SOCK_STREAM && type != SOCK_DGRAM &&
591 type != SOCK_SEQPACKET && type != SOCK_RAW &&
592 type != SOCK_PACKET) || protocol <0)
593 return(-EINVAL);
596 * Allocate the socket and allow the family to set things up. if
597 * the protocol is 0, the family is instructed to select an appropriate
598 * default.
601 if(!(sock =sock_alloc()))
603 printk("NET: sys_socket: no more sockets\n");
604 return(-ENOSR);/* Was: EAGAIN, but we are out of
605 system resources! */
608 sock->type = type;
609 sock->ops = ops;
610 if((i = sock->ops->create(sock, protocol)) <0)
612 sock_release(sock);
613 return(i);
616 if((fd =get_fd(SOCK_INODE(sock))) <0)
618 sock_release(sock);
619 return(-EINVAL);
622 return(fd);
626 * Create a pair of connected sockets.
629 asmlinkage intsys_socketpair(int family,int type,int protocol,int usockvec[2])
631 int fd1, fd2, i;
632 struct socket *sock1, *sock2;
633 int er;
636 * Obtain the first socket and check if the underlying protocol
637 * supports the socketpair call.
640 if((fd1 =sys_socket(family, type, protocol)) <0)
641 return(fd1);
642 sock1 =sockfd_lookup(fd1, NULL);
643 if(!sock1->ops->socketpair)
645 sys_close(fd1);
646 return(-EINVAL);
650 * Now grab another socket and try to connect the two together.
653 if((fd2 =sys_socket(family, type, protocol)) <0)
655 sys_close(fd1);
656 return(-EINVAL);
659 sock2 =sockfd_lookup(fd2, NULL);
660 if((i = sock1->ops->socketpair(sock1, sock2)) <0)
662 sys_close(fd1);
663 sys_close(fd2);
664 return(i);
667 sock1->conn = sock2;
668 sock2->conn = sock1;
669 sock1->state = SS_CONNECTED;
670 sock2->state = SS_CONNECTED;
672 er=verify_area(VERIFY_WRITE, usockvec,sizeof(usockvec));
673 if(er)
675 sys_close(fd1);
676 sys_close(fd2);
677 return er;
679 put_user(fd1, &usockvec[0]);
680 put_user(fd2, &usockvec[1]);
682 return(0);
687 * Bind a name to a socket. Nothing much to do here since it's
688 * the protocol's responsibility to handle the local address.
690 * We move the socket address to kernel space before we call
691 * the protocol layer (having also checked the address is ok).
694 asmlinkage intsys_bind(int fd,struct sockaddr *umyaddr,int addrlen)
696 struct socket *sock;
697 int i;
698 char address[MAX_SOCK_ADDR];
699 int err;
701 if(fd <0|| fd >= NR_OPEN || current->files->fd[fd] == NULL)
702 return(-EBADF);
704 if(!(sock =sockfd_lookup(fd, NULL)))
705 return(-ENOTSOCK);
707 if((err=move_addr_to_kernel(umyaddr,addrlen,address))<0)
708 return err;
710 if((i = sock->ops->bind(sock, (struct sockaddr *)address, addrlen)) <0)
712 return(i);
714 return(0);
719 * Perform a listen. Basically, we allow the protocol to do anything
720 * necessary for a listen, and if that works, we mark the socket as
721 * ready for listening.
724 asmlinkage intsys_listen(int fd,int backlog)
726 struct socket *sock;
728 if(fd <0|| fd >= NR_OPEN || current->files->fd[fd] == NULL)
729 return(-EBADF);
730 if(!(sock =sockfd_lookup(fd, NULL)))
731 return(-ENOTSOCK);
733 if(sock->state != SS_UNCONNECTED)
735 return(-EINVAL);
738 if(sock->ops && sock->ops->listen)
739 sock->ops->listen(sock, backlog);
740 sock->flags |= SO_ACCEPTCON;
741 return(0);
746 * For accept, we attempt to create a new socket, set up the link
747 * with the client, wake up the client, then return the new
748 * connected fd. We collect the address of the connector in kernel
749 * space and move it to user at the very end. This is buggy because
750 * we open the socket then return an error.
753 asmlinkage intsys_accept(int fd,struct sockaddr *upeer_sockaddr,int*upeer_addrlen)
755 struct file *file;
756 struct socket *sock, *newsock;
757 int i;
758 char address[MAX_SOCK_ADDR];
759 int len;
761 if(fd <0|| fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
762 return(-EBADF);
763 if(!(sock =sockfd_lookup(fd, &file)))
764 return(-ENOTSOCK);
765 if(sock->state != SS_UNCONNECTED)
767 return(-EINVAL);
769 if(!(sock->flags & SO_ACCEPTCON))
771 return(-EINVAL);
774 if(!(newsock =sock_alloc()))
776 printk("NET: sock_accept: no more sockets\n");
777 return(-ENOSR);/* Was: EAGAIN, but we are out of system
778 resources! */
780 newsock->type = sock->type;
781 newsock->ops = sock->ops;
782 if((i = sock->ops->dup(newsock, sock)) <0)
784 sock_release(newsock);
785 return(i);
788 i = newsock->ops->accept(sock, newsock, file->f_flags);
789 if( i <0)
791 sock_release(newsock);
792 return(i);
795 if((fd =get_fd(SOCK_INODE(newsock))) <0)
797 sock_release(newsock);
798 return(-EINVAL);
801 if(upeer_sockaddr)
803 newsock->ops->getname(newsock, (struct sockaddr *)address, &len,1);
804 move_addr_to_user(address,len, upeer_sockaddr, upeer_addrlen);
806 return(fd);
811 * Attempt to connect to a socket with the server address. The address
812 * is in user space so we verify it is OK and move it to kernel space.
815 asmlinkage intsys_connect(int fd,struct sockaddr *uservaddr,int addrlen)
817 struct socket *sock;
818 struct file *file;
819 int i;
820 char address[MAX_SOCK_ADDR];
821 int err;
823 if(fd <0|| fd >= NR_OPEN || (file=current->files->fd[fd]) == NULL)
824 return(-EBADF);
825 if(!(sock =sockfd_lookup(fd, &file)))
826 return(-ENOTSOCK);
828 if((err=move_addr_to_kernel(uservaddr,addrlen,address))<0)
829 return err;
831 switch(sock->state)
833 case SS_UNCONNECTED:
834 /* This is ok... continue with connect */
835 break;
836 case SS_CONNECTED:
837 /* Socket is already connected */
838 if(sock->type == SOCK_DGRAM)/* Hack for now - move this all into the protocol */
839 break;
840 return-EISCONN;
841 case SS_CONNECTING:
842 /* Not yet connected... we will check this. */
845 * FIXME: for all protocols what happens if you start
846 * an async connect fork and both children connect. Clean
847 * this up in the protocols!
849 break;
850 default:
851 return(-EINVAL);
853 i = sock->ops->connect(sock, (struct sockaddr *)address, addrlen, file->f_flags);
854 if(i <0)
856 return(i);
858 return(0);
862 * Get the local address ('name') of a socket object. Move the obtained
863 * name to user space.
866 asmlinkage intsys_getsockname(int fd,struct sockaddr *usockaddr,int*usockaddr_len)
868 struct socket *sock;
869 char address[MAX_SOCK_ADDR];
870 int len;
871 int err;
873 if(fd <0|| fd >= NR_OPEN || current->files->fd[fd] == NULL)
874 return(-EBADF);
875 if(!(sock =sockfd_lookup(fd, NULL)))
876 return(-ENOTSOCK);
878 err=sock->ops->getname(sock, (struct sockaddr *)address, &len,0);
879 if(err)
880 return err;
881 if((err=move_addr_to_user(address,len, usockaddr, usockaddr_len))<0)
882 return err;
883 return0;
887 * Get the remote address ('name') of a socket object. Move the obtained
888 * name to user space.
891 asmlinkage intsys_getpeername(int fd,struct sockaddr *usockaddr,int*usockaddr_len)
893 struct socket *sock;
894 char address[MAX_SOCK_ADDR];
895 int len;
896 int err;
898 if(fd <0|| fd >= NR_OPEN || current->files->fd[fd] == NULL)
899 return(-EBADF);
900 if(!(sock =sockfd_lookup(fd, NULL)))
901 return(-ENOTSOCK);
903 err=sock->ops->getname(sock, (struct sockaddr *)address, &len,1);
904 if(err)
905 return err;
906 if((err=move_addr_to_user(address,len, usockaddr, usockaddr_len))<0)
907 return err;
908 return0;
912 * Send a datagram down a socket. The datagram as with write() is
913 * in user space. We check it can be read.
916 asmlinkage intsys_send(int fd,void* buff,int len,unsigned flags)
918 struct socket *sock;
919 struct file *file;
920 int err;
922 if(fd <0|| fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
923 return(-EBADF);
924 if(!(sock =sockfd_lookup(fd, NULL)))
925 return(-ENOTSOCK);
927 if(len<0)
928 return-EINVAL;
929 err=verify_area(VERIFY_READ, buff, len);
930 if(err)
931 return err;
932 return(sock->ops->send(sock, buff, len, (file->f_flags & O_NONBLOCK), flags));
936 * Send a datagram to a given address. We move the address into kernel
937 * space and check the user space data area is readable before invoking
938 * the protocol.
941 asmlinkage intsys_sendto(int fd,void* buff,int len,unsigned flags,
942 struct sockaddr *addr,int addr_len)
944 struct socket *sock;
945 struct file *file;
946 char address[MAX_SOCK_ADDR];
947 int err;
949 if(fd <0|| fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
950 return(-EBADF);
951 if(!(sock =sockfd_lookup(fd, NULL)))
952 return(-ENOTSOCK);
954 if(len<0)
955 return-EINVAL;
956 err=verify_area(VERIFY_READ,buff,len);
957 if(err)
958 return err;
960 if((err=move_addr_to_kernel(addr,addr_len,address))<0)
961 return err;
963 return(sock->ops->sendto(sock, buff, len, (file->f_flags & O_NONBLOCK),
964 flags, (struct sockaddr *)address, addr_len));
969 * Receive a datagram from a socket. This isn't really right. The BSD manual
970 * pages explicitly state that recv is recvfrom with a NULL to argument. The
971 * Linux stack gets the right results for the wrong reason and this need to
972 * be tidied in the inet layer and removed from here.
973 * We check the buffer is writable and valid.
976 asmlinkage intsys_recv(int fd,void* buff,int len,unsigned flags)
978 struct socket *sock;
979 struct file *file;
980 int err;
982 if(fd <0|| fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
983 return(-EBADF);
985 if(!(sock =sockfd_lookup(fd, NULL)))
986 return(-ENOTSOCK);
988 if(len<0)
989 return-EINVAL;
990 if(len==0)
991 return0;
992 err=verify_area(VERIFY_WRITE, buff, len);
993 if(err)
994 return err;
996 return(sock->ops->recv(sock, buff, len,(file->f_flags & O_NONBLOCK), flags));
1000 * Receive a frame from the socket and optionally record the address of the
1001 * sender. We verify the buffers are writable and if needed move the
1002 * sender address from kernel to user space.
1005 asmlinkage intsys_recvfrom(int fd,void* buff,int len,unsigned flags,
1006 struct sockaddr *addr,int*addr_len)
1008 struct socket *sock;
1009 struct file *file;
1010 char address[MAX_SOCK_ADDR];
1011 int err;
1012 int alen;
1013 if(fd <0|| fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1014 return(-EBADF);
1015 if(!(sock =sockfd_lookup(fd, NULL)))
1016 return(-ENOTSOCK);
1017 if(len<0)
1018 return-EINVAL;
1019 if(len==0)
1020 return0;
1022 err=verify_area(VERIFY_WRITE,buff,len);
1023 if(err)
1024 return err;
1026 len=sock->ops->recvfrom(sock, buff, len, (file->f_flags & O_NONBLOCK),
1027 flags, (struct sockaddr *)address, &alen);
1029 if(len<0)
1030 return len;
1031 if(addr!=NULL && (err=move_addr_to_user(address,alen, addr, addr_len))<0)
1032 return err;
1034 return len;
1038 * Set a socket option. Because we don't know the option lengths we have
1039 * to pass the user mode parameter for the protocols to sort out.
1042 asmlinkage intsys_setsockopt(int fd,int level,int optname,char*optval,int optlen)
1044 struct socket *sock;
1045 struct file *file;
1047 if(fd <0|| fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1048 return(-EBADF);
1049 if(!(sock =sockfd_lookup(fd, NULL)))
1050 return(-ENOTSOCK);
1052 return(sock->ops->setsockopt(sock, level, optname, optval, optlen));
1056 * Get a socket option. Because we don't know the option lengths we have
1057 * to pass a user mode parameter for the protocols to sort out.
1060 asmlinkage intsys_getsockopt(int fd,int level,int optname,char*optval,int*optlen)
1062 struct socket *sock;
1063 struct file *file;
1065 if(fd <0|| fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1066 return(-EBADF);
1067 if(!(sock =sockfd_lookup(fd, NULL)))
1068 return(-ENOTSOCK);
1070 if(!sock->ops->getsockopt)
1071 return(0);
1072 return(sock->ops->getsockopt(sock, level, optname, optval, optlen));
1077 * Shutdown a socket.
1080 asmlinkage intsys_shutdown(int fd,int how)
1082 struct socket *sock;
1083 struct file *file;
1085 if(fd <0|| fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1086 return(-EBADF);
1087 if(!(sock =sockfd_lookup(fd, NULL)))
1088 return(-ENOTSOCK);
1090 return(sock->ops->shutdown(sock, how));
1094 * BSD sendmsg interface
1097 asmlinkage intsys_sendmsg(int fd,struct msghdr *msg,unsigned int flags)
1099 struct socket *sock;
1100 struct file *file;
1101 char address[MAX_SOCK_ADDR];
1102 struct iovec iov[MAX_IOVEC];
1103 struct msghdr msg_sys;
1104 int err;
1105 int total_len;
1107 if(fd <0|| fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1108 return(-EBADF);
1109 if(!(sock =sockfd_lookup(fd, NULL)))
1110 return(-ENOTSOCK);
1112 err=verify_area(VERIFY_READ, msg,sizeof(struct msghdr));
1113 if(err)
1114 return err;
1115 memcpy_fromfs(&msg_sys,msg,sizeof(struct msghdr));
1116 if(msg_sys.msg_iovlen>MAX_IOVEC)
1117 return-EINVAL;
1118 err=verify_iovec(&msg_sys,iov,address, VERIFY_READ);
1119 if(err<0)
1120 return err;
1121 total_len=err;
1123 if(sock->ops->sendmsg==NULL)
1124 return-EOPNOTSUPP;
1125 return sock->ops->sendmsg(sock, &msg_sys, total_len, (file->f_flags&O_NONBLOCK), flags);
1129 * BSD recvmsg interface
1132 asmlinkage intsys_recvmsg(int fd,struct msghdr *msg,unsigned int flags)
1134 struct socket *sock;
1135 struct file *file;
1136 char address[MAX_SOCK_ADDR];
1137 struct iovec iov[MAX_IOVEC];
1138 struct msghdr msg_sys;
1139 int err;
1140 int total_len;
1141 int addr_len;
1142 int len;
1144 if(fd <0|| fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1145 return(-EBADF);
1146 if(!(sock =sockfd_lookup(fd, NULL)))
1147 return(-ENOTSOCK);
1149 err=verify_area(VERIFY_READ, msg,sizeof(struct msghdr));
1150 if(err)
1151 return err;
1152 memcpy_fromfs(&msg_sys,msg,sizeof(struct msghdr));
1153 if(msg_sys.msg_iovlen>MAX_IOVEC)
1154 return-EINVAL;
1155 err=verify_iovec(&msg_sys,iov,address, VERIFY_WRITE);
1156 if(err<0)
1157 return err;
1158 total_len=err;
1160 if(sock->ops->recvmsg==NULL)
1161 return-EOPNOTSUPP;
1162 len=sock->ops->recvmsg(sock, &msg_sys, total_len, (file->f_flags&O_NONBLOCK), flags, &addr_len);
1163 if(len<0)
1164 return len;
1166 * Fixme: writing actual length into original msghdr.
1168 if(msg_sys.msg_name!=NULL && (err=move_addr_to_user(address,addr_len, msg_sys.msg_name, &msg_sys.msg_namelen))<0)
1169 return err;
1170 return len;
1175 * Perform a file control on a socket file descriptor.
1178 intsock_fcntl(struct file *filp,unsigned int cmd,unsigned long arg)
1180 struct socket *sock;
1182 sock =socki_lookup(filp->f_inode);
1183 if(sock != NULL && sock->ops != NULL && sock->ops->fcntl != NULL)
1184 return(sock->ops->fcntl(sock, cmd, arg));
1185 return(-EINVAL);
1190 * System call vectors. Since I (RIB) want to rewrite sockets as streams,
1191 * we have this level of indirection. Not a lot of overhead, since more of
1192 * the work is done via read/write/select directly.
1194 * I'm now expanding this up to a higher level to separate the assorted
1195 * kernel/user space manipulations and global assumptions from the protocol
1196 * layers proper - AC.
1198 * Argument checking cleaned up. Saved 20% in size.
1201 asmlinkage intsys_socketcall(int call,unsigned long*args)
1203 int er;
1204 unsigned char nargs[18]={0,3,3,3,2,3,3,3,
1205 4,4,4,6,6,2,5,5,3,3};
1207 unsigned long a0,a1;
1209 if(call<1||call>SYS_RECVMSG)
1210 return-EINVAL;
1212 er=verify_area(VERIFY_READ, args, nargs[call] *sizeof(unsigned long));
1213 if(er)
1214 return er;
1216 a0=get_user(args);
1217 a1=get_user(args+1);
1220 switch(call)
1222 case SYS_SOCKET:
1223 return(sys_socket(a0,a1,get_user(args+2)));
1224 case SYS_BIND:
1225 return(sys_bind(a0,(struct sockaddr *)a1,
1226 get_user(args+2)));
1227 case SYS_CONNECT:
1228 return(sys_connect(a0, (struct sockaddr *)a1,
1229 get_user(args+2)));
1230 case SYS_LISTEN:
1231 return(sys_listen(a0,a1));
1232 case SYS_ACCEPT:
1233 return(sys_accept(a0,(struct sockaddr *)a1,
1234 (int*)get_user(args+2)));
1235 case SYS_GETSOCKNAME:
1236 return(sys_getsockname(a0,(struct sockaddr *)a1,
1237 (int*)get_user(args+2)));
1238 case SYS_GETPEERNAME:
1239 return(sys_getpeername(a0, (struct sockaddr *)a1,
1240 (int*)get_user(args+2)));
1241 case SYS_SOCKETPAIR:
1242 return(sys_socketpair(a0,a1,
1243 get_user(args+2),
1244 (int*)get_user(args+3)));
1245 case SYS_SEND:
1246 return(sys_send(a0,
1247 (void*)a1,
1248 get_user(args+2),
1249 get_user(args+3)));
1250 case SYS_SENDTO:
1251 return(sys_sendto(a0,(void*)a1,
1252 get_user(args+2),
1253 get_user(args+3),
1254 (struct sockaddr *)get_user(args+4),
1255 get_user(args+5)));
1256 case SYS_RECV:
1257 return(sys_recv(a0,
1258 (void*)a1,
1259 get_user(args+2),
1260 get_user(args+3)));
1261 case SYS_RECVFROM:
1262 return(sys_recvfrom(a0,
1263 (void*)a1,
1264 get_user(args+2),
1265 get_user(args+3),
1266 (struct sockaddr *)get_user(args+4),
1267 (int*)get_user(args+5)));
1268 case SYS_SHUTDOWN:
1269 return(sys_shutdown(a0,a1));
1270 case SYS_SETSOCKOPT:
1271 return(sys_setsockopt(a0,
1273 get_user(args+2),
1274 (char*)get_user(args+3),
1275 get_user(args+4)));
1276 case SYS_GETSOCKOPT:
1277 return(sys_getsockopt(a0,
1279 get_user(args+2),
1280 (char*)get_user(args+3),
1281 (int*)get_user(args+4)));
1282 case SYS_SENDMSG:
1283 returnsys_sendmsg(a0,
1284 (struct msghdr *) a1,
1285 get_user(args+2));
1286 case SYS_RECVMSG:
1287 returnsys_recvmsg(a0,
1288 (struct msghdr *) a1,
1289 get_user(args+2));
1291 return-EINVAL;/* to keep gcc happy */
1295 * This function is called by a protocol handler that wants to
1296 * advertise its address family, and have it linked into the
1297 * SOCKET module.
1300 intsock_register(int family,struct proto_ops *ops)
1302 int i;
1304 cli();
1305 for(i =0; i < NPROTO; i++)
1307 if(pops[i] != NULL)
1308 continue;
1309 pops[i] = ops;
1310 pops[i]->family = family;
1311 sti();
1312 return(i);
1314 sti();
1315 return(-ENOMEM);
1319 * This function is called by a protocol handler that wants to
1320 * remove its address family, and have it unlinked from the
1321 * SOCKET module.
1324 intsock_unregister(int family)
1326 int i;
1328 cli();
1329 for(i =0; i < NPROTO; i++)
1331 if(pops[i] == NULL)
1332 continue;
1333 if(pops[i]->family == family)
1335 pops[i]=NULL;
1336 sti();
1337 return(i);
1340 sti();
1341 return(-ENOENT);
1344 voidproto_init(void)
1346 externstruct net_proto protocols[];/* Network protocols */
1347 struct net_proto *pro;
1349 /* Kick all configured protocols. */
1350 pro = protocols;
1351 while(pro->name != NULL)
1353 (*pro->init_func)(pro);
1354 pro++;
1356 /* We're all done... */
1360 voidsock_init(void)
1362 int i;
1364 printk("Swansea University Computer Society NET3.030 Snap #1 for Linux 1.3.4\n");
1367 * Initialize all address (protocol) families.
1370 for(i =0; i < NPROTO; ++i) pops[i] = NULL;
1373 * Initialize the protocols module.
1376 proto_init();
1378 #ifdef CONFIG_NET
1380 * Initialize the DEV module.
1383 dev_init();
1386 * And the bottom half handler
1389 bh_base[NET_BH].routine= net_bh;
1390 enable_bh(NET_BH);
1391 #endif
1394 intsocket_get_info(char*buffer,char**start, off_t offset,int length)
1396 int len =sprintf(buffer,"sockets: used %d\n", sockets_in_use);
1397 if(offset >= len)
1399 *start = buffer;
1400 return0;
1402 *start = buffer + offset;
1403 len -= offset;
1404 if(len > length)
1405 len = length;
1406 return len;
close