6f495a0db62c76c7684f0675770093341936e096
[davej-history.git] / net / socket.c
blob6f495a0db62c76c7684f0675770093341936e096
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".
40 * This program is free software; you can redistribute it and/or
41 * modify it under the terms of the GNU General Public License
42 * as published by the Free Software Foundation; either version
43 * 2 of the License, or (at your option) any later version.
46 * This module is effectively the top level interface to the BSD socket
47 * paradigm. Because it is very simple it works well for Unix domain sockets,
48 * but requires a whole layer of substructure for the other protocols.
50 * In addition it lacks an effective kernel -> kernel interface to go with
51 * the user one.
54 #include <linux/config.h>
55 #include <linux/signal.h>
56 #include <linux/errno.h>
57 #include <linux/sched.h>
58 #include <linux/mm.h>
59 #include <linux/kernel.h>
60 #include <linux/major.h>
61 #include <linux/stat.h>
62 #include <linux/socket.h>
63 #include <linux/fcntl.h>
64 #include <linux/net.h>
65 #include <linux/interrupt.h>
66 #include <linux/netdevice.h>
67 #include <linux/proc_fs.h>
68 #include <linux/firewall.h>
70 #ifdef CONFIG_KERNELD
71 #include <linux/kerneld.h>
72 #endif
74 #include <net/netlink.h>
76 #include <asm/system.h>
77 #include <asm/segment.h>
79 #if defined(CONFIG_MODULES) && defined(CONFIG_NET)
80 externvoidexport_net_symbols(void);
81 #endif
83 static intsock_lseek(struct inode *inode,struct file *file, off_t offset,
84 int whence);
85 static intsock_read(struct inode *inode,struct file *file,char*buf,
86 int size);
87 static intsock_write(struct inode *inode,struct file *file,const char*buf,
88 int size);
90 static voidsock_close(struct inode *inode,struct file *file);
91 static intsock_select(struct inode *inode,struct file *file,int which, select_table *seltable);
92 static intsock_ioctl(struct inode *inode,struct file *file,
93 unsigned int cmd,unsigned long arg);
94 static intsock_fasync(struct inode *inode,struct file *filp,int on);
98 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
99 * in the operation structures but are done directly via the socketcall() multiplexor.
102 static struct file_operations socket_file_ops = {
103 sock_lseek,
104 sock_read,
105 sock_write,
106 NULL,/* readdir */
107 sock_select,
108 sock_ioctl,
109 NULL,/* mmap */
110 NULL,/* no special open code... */
111 sock_close,
112 NULL,/* no fsync */
113 sock_fasync
117 * The protocol list. Each protocol is registered in here.
119 static struct proto_ops *pops[NPROTO];
121 * Statistics counters of the socket lists
123 static int sockets_in_use =0;
126 * Support routines. Move socket addresses back and forth across the kernel/user
127 * divide and look after the messy bits.
130 #define MAX_SOCK_ADDR 128/* 108 for Unix domain - 16 for IP, 16 for IPX, about 80 for AX.25 */
132 intmove_addr_to_kernel(void*uaddr,int ulen,void*kaddr)
134 int err;
135 if(ulen<0||ulen>MAX_SOCK_ADDR)
136 return-EINVAL;
137 if(ulen==0)
138 return0;
139 if((err=verify_area(VERIFY_READ,uaddr,ulen))<0)
140 return err;
141 memcpy_fromfs(kaddr,uaddr,ulen);
142 return0;
145 intmove_addr_to_user(void*kaddr,int klen,void*uaddr,int*ulen)
147 int err;
148 int len;
151 if((err=verify_area(VERIFY_WRITE,ulen,sizeof(*ulen)))<0)
152 return err;
153 len=get_user(ulen);
154 if(len>klen)
155 len=klen;
156 if(len<0|| len> MAX_SOCK_ADDR)
157 return-EINVAL;
158 if(len)
160 if((err=verify_area(VERIFY_WRITE,uaddr,len))<0)
161 return err;
162 memcpy_tofs(uaddr,kaddr,len);
164 put_user(len,ulen);
165 return0;
169 * Obtains the first available file descriptor and sets it up for use.
172 static intget_fd(struct inode *inode)
174 int fd;
175 struct file *file;
178 * Find a file descriptor suitable for return to the user.
181 file =get_empty_filp();
182 if(!file)
183 return(-1);
185 for(fd =0; fd < NR_OPEN; ++fd)
186 if(!current->files->fd[fd])
187 break;
188 if(fd == NR_OPEN)
190 file->f_count =0;
191 return(-1);
194 FD_CLR(fd, &current->files->close_on_exec);
195 current->files->fd[fd] = file;
196 file->f_op = &socket_file_ops;
197 file->f_mode =3;
198 file->f_flags = O_RDWR;
199 file->f_count =1;
200 file->f_inode = inode;
201 if(inode)
202 inode->i_count++;
203 file->f_pos =0;
204 return(fd);
209 * Go from an inode to its socket slot.
211 * The original socket implementation wasn't very clever, which is
212 * why this exists at all..
215 __inline struct socket *socki_lookup(struct inode *inode)
217 return&inode->u.socket_i;
221 * Go from a file number to its socket slot.
224 extern __inline struct socket *sockfd_lookup(int fd,struct file **pfile)
226 struct file *file;
227 struct inode *inode;
229 if(fd <0|| fd >= NR_OPEN || !(file = current->files->fd[fd]))
230 return NULL;
232 inode = file->f_inode;
233 if(!inode || !inode->i_sock)
234 return NULL;
236 if(pfile)
237 *pfile = file;
239 returnsocki_lookup(inode);
243 * Allocate a socket.
246 struct socket *sock_alloc(void)
248 struct inode * inode;
249 struct socket * sock;
251 inode =get_empty_inode();
252 if(!inode)
253 return NULL;
255 inode->i_mode = S_IFSOCK;
256 inode->i_sock =1;
257 inode->i_uid = current->uid;
258 inode->i_gid = current->gid;
260 sock = &inode->u.socket_i;
261 sock->state = SS_UNCONNECTED;
262 sock->flags =0;
263 sock->ops = NULL;
264 sock->data = NULL;
265 sock->conn = NULL;
266 sock->iconn = NULL;
267 sock->next = NULL;
268 sock->file = NULL;
269 sock->wait = &inode->i_wait;
270 sock->inode = inode;/* "backlink": we could use pointer arithmetic instead */
271 sock->fasync_list = NULL;
272 sockets_in_use++;
273 return sock;
277 * Release a socket.
280 staticinlinevoidsock_release_peer(struct socket *peer)
282 peer->state = SS_DISCONNECTING;
283 wake_up_interruptible(peer->wait);
284 sock_wake_async(peer,1);
287 voidsock_release(struct socket *sock)
289 int oldstate;
290 struct socket *peersock, *nextsock;
292 if((oldstate = sock->state) != SS_UNCONNECTED)
293 sock->state = SS_DISCONNECTING;
296 * Wake up anyone waiting for connections.
299 for(peersock = sock->iconn; peersock; peersock = nextsock)
301 nextsock = peersock->next;
302 sock_release_peer(peersock);
306 * Wake up anyone we're connected to. First, we release the
307 * protocol, to give it a chance to flush data, etc.
310 peersock = (oldstate == SS_CONNECTED) ? sock->conn : NULL;
311 if(sock->ops)
312 sock->ops->release(sock, peersock);
313 if(peersock)
314 sock_release_peer(peersock);
315 --sockets_in_use;/* Bookkeeping.. */
316 sock->file=NULL;
317 iput(SOCK_INODE(sock));
321 * Sockets are not seekable.
324 static intsock_lseek(struct inode *inode,struct file *file, off_t offset,int whence)
326 return(-ESPIPE);
330 * Read data from a socket. ubuf is a user mode pointer. We make sure the user
331 * area ubuf...ubuf+size-1 is writable before asking the protocol.
334 static intsock_read(struct inode *inode,struct file *file,char*ubuf,int size)
336 struct socket *sock;
337 int err;
338 struct iovec iov;
339 struct msghdr msg;
341 sock =socki_lookup(inode);
342 if(sock->flags & SO_ACCEPTCON)
343 return(-EINVAL);
345 if(size<0)
346 return-EINVAL;
347 if(size==0)/* Match SYS5 behaviour */
348 return0;
349 if((err=verify_area(VERIFY_WRITE,ubuf,size))<0)
350 return err;
351 msg.msg_name=NULL;
352 msg.msg_iov=&iov;
353 msg.msg_iovlen=1;
354 msg.msg_control=NULL;
355 iov.iov_base=ubuf;
356 iov.iov_len=size;
358 return(sock->ops->recvmsg(sock, &msg, size,(file->f_flags & O_NONBLOCK),0,&msg.msg_namelen));
362 * Write data to a socket. We verify that the user area ubuf..ubuf+size-1 is
363 * readable by the user process.
366 static intsock_write(struct inode *inode,struct file *file,const char*ubuf,int size)
368 struct socket *sock;
369 int err;
370 struct msghdr msg;
371 struct iovec iov;
373 sock =socki_lookup(inode);
375 if(sock->flags & SO_ACCEPTCON)
376 return(-EINVAL);
378 if(size<0)
379 return-EINVAL;
380 if(size==0)/* Match SYS5 behaviour */
381 return0;
383 if((err=verify_area(VERIFY_READ,ubuf,size))<0)
384 return err;
386 msg.msg_name=NULL;
387 msg.msg_iov=&iov;
388 msg.msg_iovlen=1;
389 msg.msg_control=NULL;
390 iov.iov_base=(void*)ubuf;
391 iov.iov_len=size;
393 return(sock->ops->sendmsg(sock, &msg, size,(file->f_flags & O_NONBLOCK),0));
397 * With an ioctl arg may well be a user mode pointer, but we don't know what to do
398 * with it - that's up to the protocol still.
401 intsock_ioctl(struct inode *inode,struct file *file,unsigned int cmd,
402 unsigned long arg)
404 struct socket *sock;
405 sock =socki_lookup(inode);
406 return(sock->ops->ioctl(sock, cmd, arg));
410 static intsock_select(struct inode *inode,struct file *file,int sel_type, select_table * wait)
412 struct socket *sock;
414 sock =socki_lookup(inode);
417 * We can't return errors to select, so it's either yes or no.
420 if(sock->ops->select)
421 return(sock->ops->select(sock, sel_type, wait));
422 return(0);
426 voidsock_close(struct inode *inode,struct file *filp)
429 * It's possible the inode is NULL if we're closing an unfinished socket.
432 if(!inode)
433 return;
434 sock_fasync(inode, filp,0);
435 sock_release(socki_lookup(inode));
439 * Update the socket async list
442 static intsock_fasync(struct inode *inode,struct file *filp,int on)
444 struct fasync_struct *fa, *fna=NULL, **prev;
445 struct socket *sock;
446 unsigned long flags;
448 if(on)
450 fna=(struct fasync_struct *)kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
451 if(fna==NULL)
452 return-ENOMEM;
455 sock =socki_lookup(inode);
457 prev=&(sock->fasync_list);
459 save_flags(flags);
460 cli();
462 for(fa=*prev; fa!=NULL; prev=&fa->fa_next,fa=*prev)
463 if(fa->fa_file==filp)
464 break;
466 if(on)
468 if(fa!=NULL)
470 kfree_s(fna,sizeof(struct fasync_struct));
471 restore_flags(flags);
472 return0;
474 fna->fa_file=filp;
475 fna->magic=FASYNC_MAGIC;
476 fna->fa_next=sock->fasync_list;
477 sock->fasync_list=fna;
479 else
481 if(fa!=NULL)
483 *prev=fa->fa_next;
484 kfree_s(fa,sizeof(struct fasync_struct));
487 restore_flags(flags);
488 return0;
491 intsock_wake_async(struct socket *sock,int how)
493 if(!sock || !sock->fasync_list)
494 return-1;
495 switch(how)
497 case0:
498 kill_fasync(sock->fasync_list, SIGIO);
499 break;
500 case1:
501 if(!(sock->flags & SO_WAITDATA))
502 kill_fasync(sock->fasync_list, SIGIO);
503 break;
504 case2:
505 if(sock->flags & SO_NOSPACE)
507 kill_fasync(sock->fasync_list, SIGIO);
508 sock->flags &= ~SO_NOSPACE;
510 break;
512 return0;
517 * Perform the socket system call. we locate the appropriate
518 * family, then create a fresh socket.
521 static intfind_protocol_family(int family)
523 registerint i;
524 for(i =0; i < NPROTO; i++)
526 if(pops[i] == NULL)
527 continue;
528 if(pops[i]->family == family)
529 return i;
531 return-1;
534 asmlinkage intsys_socket(int family,int type,int protocol)
536 int i, fd;
537 struct socket *sock;
538 struct proto_ops *ops;
540 /* Locate the correct protocol family. */
541 i =find_protocol_family(family);
543 #ifdef CONFIG_KERNELD
544 /* Attempt to load a protocol module if the find failed. */
545 if(i <0)
547 char module_name[30];
548 sprintf(module_name,"net-pf-%d",family);
549 request_module(module_name);
550 i =find_protocol_family(family);
552 #endif
554 if(i <0)
556 return-EINVAL;
559 ops = pops[i];
562 * Check that this is a type that we know how to manipulate and
563 * the protocol makes sense here. The family can still reject the
564 * protocol later.
567 if((type != SOCK_STREAM && type != SOCK_DGRAM &&
568 type != SOCK_SEQPACKET && type != SOCK_RAW &&
569 type != SOCK_PACKET) || protocol <0)
570 return(-EINVAL);
573 * Allocate the socket and allow the family to set things up. if
574 * the protocol is 0, the family is instructed to select an appropriate
575 * default.
578 if(!(sock =sock_alloc()))
580 printk(KERN_WARNING "socket: no more sockets\n");
581 return(-ENOSR);/* Was: EAGAIN, but we are out of
582 system resources! */
585 sock->type = type;
586 sock->ops = ops;
587 if((i = sock->ops->create(sock, protocol)) <0)
589 sock_release(sock);
590 return(i);
593 if((fd =get_fd(SOCK_INODE(sock))) <0)
595 sock_release(sock);
596 return(-EINVAL);
599 sock->file=current->files->fd[fd];
601 return(fd);
605 * Create a pair of connected sockets.
608 asmlinkage intsys_socketpair(int family,int type,int protocol,int usockvec[2])
610 int fd1, fd2, i;
611 struct socket *sock1, *sock2;
612 int er;
615 * Obtain the first socket and check if the underlying protocol
616 * supports the socketpair call.
619 if((fd1 =sys_socket(family, type, protocol)) <0)
620 return(fd1);
621 sock1 =sockfd_lookup(fd1, NULL);
622 if(!sock1->ops->socketpair)
624 sys_close(fd1);
625 return(-EINVAL);
629 * Now grab another socket and try to connect the two together.
632 if((fd2 =sys_socket(family, type, protocol)) <0)
634 sys_close(fd1);
635 return(-EINVAL);
638 sock2 =sockfd_lookup(fd2, NULL);
639 if((i = sock1->ops->socketpair(sock1, sock2)) <0)
641 sys_close(fd1);
642 sys_close(fd2);
643 return(i);
646 sock1->conn = sock2;
647 sock2->conn = sock1;
648 sock1->state = SS_CONNECTED;
649 sock2->state = SS_CONNECTED;
651 er=verify_area(VERIFY_WRITE, usockvec,sizeof(usockvec));
652 if(er)
654 sys_close(fd1);
655 sys_close(fd2);
656 return er;
658 put_user(fd1, &usockvec[0]);
659 put_user(fd2, &usockvec[1]);
661 return(0);
666 * Bind a name to a socket. Nothing much to do here since it's
667 * the protocol's responsibility to handle the local address.
669 * We move the socket address to kernel space before we call
670 * the protocol layer (having also checked the address is ok).
673 asmlinkage intsys_bind(int fd,struct sockaddr *umyaddr,int addrlen)
675 struct socket *sock;
676 int i;
677 char address[MAX_SOCK_ADDR];
678 int err;
680 if(fd <0|| fd >= NR_OPEN || current->files->fd[fd] == NULL)
681 return(-EBADF);
683 if(!(sock =sockfd_lookup(fd, NULL)))
684 return(-ENOTSOCK);
686 if((err=move_addr_to_kernel(umyaddr,addrlen,address))<0)
687 return err;
689 if((i = sock->ops->bind(sock, (struct sockaddr *)address, addrlen)) <0)
691 return(i);
693 return(0);
698 * Perform a listen. Basically, we allow the protocol to do anything
699 * necessary for a listen, and if that works, we mark the socket as
700 * ready for listening.
703 asmlinkage intsys_listen(int fd,int backlog)
705 struct socket *sock;
706 int err=-EOPNOTSUPP;
708 if(fd <0|| fd >= NR_OPEN || current->files->fd[fd] == NULL)
709 return(-EBADF);
710 if(!(sock =sockfd_lookup(fd, NULL)))
711 return(-ENOTSOCK);
713 if(sock->state != SS_UNCONNECTED)
714 return(-EINVAL);
716 if(sock->ops && sock->ops->listen)
718 err=sock->ops->listen(sock, backlog);
719 if(!err)
720 sock->flags |= SO_ACCEPTCON;
722 return(err);
727 * For accept, we attempt to create a new socket, set up the link
728 * with the client, wake up the client, then return the new
729 * connected fd. We collect the address of the connector in kernel
730 * space and move it to user at the very end. This is buggy because
731 * we open the socket then return an error.
734 asmlinkage intsys_accept(int fd,struct sockaddr *upeer_sockaddr,int*upeer_addrlen)
736 struct file *file;
737 struct socket *sock, *newsock;
738 int i;
739 char address[MAX_SOCK_ADDR];
740 int len;
742 if(fd <0|| fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
743 return(-EBADF);
744 if(!(sock =sockfd_lookup(fd, &file)))
745 return(-ENOTSOCK);
746 if(sock->state != SS_UNCONNECTED)
748 return(-EINVAL);
750 if(!(sock->flags & SO_ACCEPTCON))
752 return(-EINVAL);
755 if(!(newsock =sock_alloc()))
757 printk(KERN_WARNING "accept: no more sockets\n");
758 return(-ENOSR);/* Was: EAGAIN, but we are out of system
759 resources! */
761 newsock->type = sock->type;
762 newsock->ops = sock->ops;
763 if((i = sock->ops->dup(newsock, sock)) <0)
765 sock_release(newsock);
766 return(i);
769 i = newsock->ops->accept(sock, newsock, file->f_flags);
770 if( i <0)
772 sock_release(newsock);
773 return(i);
776 if((fd =get_fd(SOCK_INODE(newsock))) <0)
778 sock_release(newsock);
779 return(-EINVAL);
781 newsock->file=current->files->fd[fd];
783 if(upeer_sockaddr)
785 newsock->ops->getname(newsock, (struct sockaddr *)address, &len,1);
786 move_addr_to_user(address,len, upeer_sockaddr, upeer_addrlen);
788 return(fd);
793 * Attempt to connect to a socket with the server address. The address
794 * is in user space so we verify it is OK and move it to kernel space.
797 asmlinkage intsys_connect(int fd,struct sockaddr *uservaddr,int addrlen)
799 struct socket *sock;
800 struct file *file;
801 int i;
802 char address[MAX_SOCK_ADDR];
803 int err;
805 if(fd <0|| fd >= NR_OPEN || (file=current->files->fd[fd]) == NULL)
806 return(-EBADF);
807 if(!(sock =sockfd_lookup(fd, &file)))
808 return(-ENOTSOCK);
810 if((err=move_addr_to_kernel(uservaddr,addrlen,address))<0)
811 return err;
813 switch(sock->state)
815 case SS_UNCONNECTED:
816 /* This is ok... continue with connect */
817 break;
818 case SS_CONNECTED:
819 /* Socket is already connected */
820 if(sock->type == SOCK_DGRAM)/* Hack for now - move this all into the protocol */
821 break;
822 return-EISCONN;
823 case SS_CONNECTING:
824 /* Not yet connected... we will check this. */
827 * FIXME: for all protocols what happens if you start
828 * an async connect fork and both children connect. Clean
829 * this up in the protocols!
831 break;
832 default:
833 return(-EINVAL);
835 i = sock->ops->connect(sock, (struct sockaddr *)address, addrlen, file->f_flags);
836 if(i <0)
838 return(i);
840 return(0);
844 * Get the local address ('name') of a socket object. Move the obtained
845 * name to user space.
848 asmlinkage intsys_getsockname(int fd,struct sockaddr *usockaddr,int*usockaddr_len)
850 struct socket *sock;
851 char address[MAX_SOCK_ADDR];
852 int len;
853 int err;
855 if(fd <0|| fd >= NR_OPEN || current->files->fd[fd] == NULL)
856 return(-EBADF);
857 if(!(sock =sockfd_lookup(fd, NULL)))
858 return(-ENOTSOCK);
860 err=sock->ops->getname(sock, (struct sockaddr *)address, &len,0);
861 if(err)
862 return err;
863 if((err=move_addr_to_user(address,len, usockaddr, usockaddr_len))<0)
864 return err;
865 return0;
869 * Get the remote address ('name') of a socket object. Move the obtained
870 * name to user space.
873 asmlinkage intsys_getpeername(int fd,struct sockaddr *usockaddr,int*usockaddr_len)
875 struct socket *sock;
876 char address[MAX_SOCK_ADDR];
877 int len;
878 int err;
880 if(fd <0|| fd >= NR_OPEN || current->files->fd[fd] == NULL)
881 return(-EBADF);
882 if(!(sock =sockfd_lookup(fd, NULL)))
883 return(-ENOTSOCK);
885 err=sock->ops->getname(sock, (struct sockaddr *)address, &len,1);
886 if(err)
887 return err;
888 if((err=move_addr_to_user(address,len, usockaddr, usockaddr_len))<0)
889 return err;
890 return0;
894 * Send a datagram down a socket. The datagram as with write() is
895 * in user space. We check it can be read.
898 asmlinkage intsys_send(int fd,void* buff,int len,unsigned flags)
900 struct socket *sock;
901 struct file *file;
902 int err;
903 struct msghdr msg;
904 struct iovec iov;
906 if(fd <0|| fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
907 return(-EBADF);
908 if(!(sock =sockfd_lookup(fd, NULL)))
909 return(-ENOTSOCK);
911 if(len<0)
912 return-EINVAL;
913 err=verify_area(VERIFY_READ, buff, len);
914 if(err)
915 return err;
917 iov.iov_base=buff;
918 iov.iov_len=len;
919 msg.msg_name=NULL;
920 msg.msg_iov=&iov;
921 msg.msg_iovlen=1;
922 msg.msg_control=NULL;
923 return(sock->ops->sendmsg(sock, &msg, len, (file->f_flags & O_NONBLOCK), flags));
927 * Send a datagram to a given address. We move the address into kernel
928 * space and check the user space data area is readable before invoking
929 * the protocol.
932 asmlinkage intsys_sendto(int fd,void* buff,int len,unsigned flags,
933 struct sockaddr *addr,int addr_len)
935 struct socket *sock;
936 struct file *file;
937 char address[MAX_SOCK_ADDR];
938 int err;
939 struct msghdr msg;
940 struct iovec iov;
942 if(fd <0|| fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
943 return(-EBADF);
944 if(!(sock =sockfd_lookup(fd, NULL)))
945 return(-ENOTSOCK);
947 if(len<0)
948 return-EINVAL;
949 err=verify_area(VERIFY_READ,buff,len);
950 if(err)
951 return err;
953 if((err=move_addr_to_kernel(addr,addr_len,address))<0)
954 return err;
956 iov.iov_base=buff;
957 iov.iov_len=len;
958 msg.msg_name=address;
959 msg.msg_namelen=addr_len;
960 msg.msg_iov=&iov;
961 msg.msg_iovlen=1;
962 msg.msg_control=NULL;
963 return(sock->ops->sendmsg(sock, &msg, len, (file->f_flags & O_NONBLOCK),
964 flags));
969 * Receive a datagram from a socket. Call the protocol recvmsg method
972 asmlinkage intsys_recv(int fd,void* ubuf,int size,unsigned flags)
974 struct iovec iov;
975 struct msghdr msg;
976 struct socket *sock;
977 struct file *file;
978 int err;
980 if(fd <0|| fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
981 return(-EBADF);
983 if(!(sock =sockfd_lookup(fd, NULL)))
984 return(-ENOTSOCK);
986 if(size<0)
987 return-EINVAL;
988 if(size==0)
989 return0;
990 err=verify_area(VERIFY_WRITE, ubuf, size);
991 if(err)
992 return err;
994 msg.msg_name=NULL;
995 msg.msg_iov=&iov;
996 msg.msg_iovlen=1;
997 msg.msg_control=NULL;
998 iov.iov_base=ubuf;
999 iov.iov_len=size;
1001 return(sock->ops->recvmsg(sock, &msg, size,(file->f_flags & O_NONBLOCK), flags,&msg.msg_namelen));
1005 * Receive a frame from the socket and optionally record the address of the
1006 * sender. We verify the buffers are writable and if needed move the
1007 * sender address from kernel to user space.
1010 asmlinkage intsys_recvfrom(int fd,void* ubuf,int size,unsigned flags,
1011 struct sockaddr *addr,int*addr_len)
1013 struct socket *sock;
1014 struct file *file;
1015 struct iovec iov;
1016 struct msghdr msg;
1017 char address[MAX_SOCK_ADDR];
1018 int err;
1019 int alen;
1020 if(fd <0|| fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1021 return(-EBADF);
1022 if(!(sock =sockfd_lookup(fd, NULL)))
1023 return(-ENOTSOCK);
1024 if(size<0)
1025 return-EINVAL;
1026 if(size==0)
1027 return0;
1029 err=verify_area(VERIFY_WRITE,ubuf,size);
1030 if(err)
1031 return err;
1033 msg.msg_control=NULL;
1034 msg.msg_iovlen=1;
1035 msg.msg_iov=&iov;
1036 iov.iov_len=size;
1037 iov.iov_base=ubuf;
1038 msg.msg_name=address;
1039 msg.msg_namelen=MAX_SOCK_ADDR;
1040 size=sock->ops->recvmsg(sock, &msg, size, (file->f_flags & O_NONBLOCK),
1041 flags, &alen);
1043 if(size<0)
1044 return size;
1045 if(addr!=NULL && (err=move_addr_to_user(address,alen, addr, addr_len))<0)
1046 return err;
1048 return size;
1052 * Set a socket option. Because we don't know the option lengths we have
1053 * to pass the user mode parameter for the protocols to sort out.
1056 asmlinkage intsys_setsockopt(int fd,int level,int optname,char*optval,int optlen)
1058 struct socket *sock;
1059 struct file *file;
1061 if(fd <0|| fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1062 return(-EBADF);
1063 if(!(sock =sockfd_lookup(fd, NULL)))
1064 return(-ENOTSOCK);
1066 return(sock->ops->setsockopt(sock, level, optname, optval, optlen));
1070 * Get a socket option. Because we don't know the option lengths we have
1071 * to pass a user mode parameter for the protocols to sort out.
1074 asmlinkage intsys_getsockopt(int fd,int level,int optname,char*optval,int*optlen)
1076 struct socket *sock;
1077 struct file *file;
1079 if(fd <0|| fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1080 return(-EBADF);
1081 if(!(sock =sockfd_lookup(fd, NULL)))
1082 return(-ENOTSOCK);
1084 if(!sock->ops->getsockopt)
1085 return(0);
1086 return(sock->ops->getsockopt(sock, level, optname, optval, optlen));
1091 * Shutdown a socket.
1094 asmlinkage intsys_shutdown(int fd,int how)
1096 struct socket *sock;
1097 struct file *file;
1099 if(fd <0|| fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1100 return(-EBADF);
1101 if(!(sock =sockfd_lookup(fd, NULL)))
1102 return(-ENOTSOCK);
1104 return(sock->ops->shutdown(sock, how));
1108 * BSD sendmsg interface
1111 asmlinkage intsys_sendmsg(int fd,struct msghdr *msg,unsigned int flags)
1113 struct socket *sock;
1114 struct file *file;
1115 char address[MAX_SOCK_ADDR];
1116 struct iovec iov[UIO_MAXIOV];
1117 struct msghdr msg_sys;
1118 int err;
1119 int total_len;
1121 if(fd <0|| fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1122 return(-EBADF);
1123 if(!(sock =sockfd_lookup(fd, NULL)))
1124 return(-ENOTSOCK);
1126 if(sock->ops->sendmsg==NULL)
1127 return-EOPNOTSUPP;
1130 err=verify_area(VERIFY_READ, msg,sizeof(struct msghdr));
1131 if(err)
1132 return err;
1134 memcpy_fromfs(&msg_sys,msg,sizeof(struct msghdr));
1136 /* do not move before msg_sys is valid */
1137 if(msg_sys.msg_iovlen>UIO_MAXIOV)
1138 return-EINVAL;
1140 /* This will also move the address data into kernel space */
1141 err =verify_iovec(&msg_sys, iov, address, VERIFY_READ);
1142 if(err <0)
1143 return err;
1144 total_len=err;
1146 return sock->ops->sendmsg(sock, &msg_sys, total_len, (file->f_flags&O_NONBLOCK), flags);
1150 * BSD recvmsg interface
1153 asmlinkage intsys_recvmsg(int fd,struct msghdr *msg,unsigned int flags)
1155 struct socket *sock;
1156 struct file *file;
1157 struct iovec iov[UIO_MAXIOV];
1158 struct msghdr msg_sys;
1159 int err;
1160 int total_len;
1161 int len;
1163 /* kernel mode address */
1164 char addr[MAX_SOCK_ADDR];
1165 int addr_len;
1167 /* user mode address pointers */
1168 struct sockaddr *uaddr;
1169 int*uaddr_len;
1171 if(fd <0|| fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1172 return(-EBADF);
1173 if(!(sock =sockfd_lookup(fd, NULL)))
1174 return(-ENOTSOCK);
1176 err=verify_area(VERIFY_READ, msg,sizeof(struct msghdr));
1177 if(err)
1178 return err;
1179 memcpy_fromfs(&msg_sys,msg,sizeof(struct msghdr));
1180 if(msg_sys.msg_iovlen>UIO_MAXIOV)
1181 return-EINVAL;
1184 * save the user-mode address (verify_iovec will change the
1185 * kernel msghdr to use the kernel address space)
1187 uaddr = msg_sys.msg_name;
1188 uaddr_len = &msg->msg_namelen;
1189 err=verify_iovec(&msg_sys,iov,addr, VERIFY_WRITE);
1190 if(err<0)
1191 return err;
1193 total_len=err;
1195 if(sock->ops->recvmsg==NULL)
1196 return-EOPNOTSUPP;
1197 len=sock->ops->recvmsg(sock, &msg_sys, total_len, (file->f_flags&O_NONBLOCK), flags, &addr_len);
1198 if(len<0)
1199 return len;
1201 if(uaddr != NULL) {
1202 err =move_addr_to_user(addr, addr_len, uaddr, uaddr_len);
1203 if(err)
1204 return err;
1206 return len;
1211 * Perform a file control on a socket file descriptor.
1214 intsock_fcntl(struct file *filp,unsigned int cmd,unsigned long arg)
1216 struct socket *sock;
1218 sock =socki_lookup(filp->f_inode);
1219 if(sock != NULL && sock->ops != NULL && sock->ops->fcntl != NULL)
1220 return(sock->ops->fcntl(sock, cmd, arg));
1221 return(-EINVAL);
1226 * System call vectors. Since I (RIB) want to rewrite sockets as streams,
1227 * we have this level of indirection. Not a lot of overhead, since more of
1228 * the work is done via read/write/select directly.
1230 * I'm now expanding this up to a higher level to separate the assorted
1231 * kernel/user space manipulations and global assumptions from the protocol
1232 * layers proper - AC.
1234 * Argument checking cleaned up. Saved 20% in size.
1237 asmlinkage intsys_socketcall(int call,unsigned long*args)
1239 int er;
1240 unsigned char nargs[18]={0,3,3,3,2,3,3,3,
1241 4,4,4,6,6,2,5,5,3,3};
1243 unsigned long a0,a1;
1245 if(call<1||call>SYS_RECVMSG)
1246 return-EINVAL;
1248 er=verify_area(VERIFY_READ, args, nargs[call] *sizeof(unsigned long));
1249 if(er)
1250 return er;
1252 a0=get_user(args);
1253 a1=get_user(args+1);
1256 switch(call)
1258 case SYS_SOCKET:
1259 return(sys_socket(a0,a1,get_user(args+2)));
1260 case SYS_BIND:
1261 return(sys_bind(a0,(struct sockaddr *)a1,
1262 get_user(args+2)));
1263 case SYS_CONNECT:
1264 return(sys_connect(a0, (struct sockaddr *)a1,
1265 get_user(args+2)));
1266 case SYS_LISTEN:
1267 return(sys_listen(a0,a1));
1268 case SYS_ACCEPT:
1269 return(sys_accept(a0,(struct sockaddr *)a1,
1270 (int*)get_user(args+2)));
1271 case SYS_GETSOCKNAME:
1272 return(sys_getsockname(a0,(struct sockaddr *)a1,
1273 (int*)get_user(args+2)));
1274 case SYS_GETPEERNAME:
1275 return(sys_getpeername(a0, (struct sockaddr *)a1,
1276 (int*)get_user(args+2)));
1277 case SYS_SOCKETPAIR:
1278 return(sys_socketpair(a0,a1,
1279 get_user(args+2),
1280 (int*)get_user(args+3)));
1281 case SYS_SEND:
1282 return(sys_send(a0,
1283 (void*)a1,
1284 get_user(args+2),
1285 get_user(args+3)));
1286 case SYS_SENDTO:
1287 return(sys_sendto(a0,(void*)a1,
1288 get_user(args+2),
1289 get_user(args+3),
1290 (struct sockaddr *)get_user(args+4),
1291 get_user(args+5)));
1292 case SYS_RECV:
1293 return(sys_recv(a0,
1294 (void*)a1,
1295 get_user(args+2),
1296 get_user(args+3)));
1297 case SYS_RECVFROM:
1298 return(sys_recvfrom(a0,
1299 (void*)a1,
1300 get_user(args+2),
1301 get_user(args+3),
1302 (struct sockaddr *)get_user(args+4),
1303 (int*)get_user(args+5)));
1304 case SYS_SHUTDOWN:
1305 return(sys_shutdown(a0,a1));
1306 case SYS_SETSOCKOPT:
1307 return(sys_setsockopt(a0,
1309 get_user(args+2),
1310 (char*)get_user(args+3),
1311 get_user(args+4)));
1312 case SYS_GETSOCKOPT:
1313 return(sys_getsockopt(a0,
1315 get_user(args+2),
1316 (char*)get_user(args+3),
1317 (int*)get_user(args+4)));
1318 case SYS_SENDMSG:
1319 returnsys_sendmsg(a0,
1320 (struct msghdr *) a1,
1321 get_user(args+2));
1322 case SYS_RECVMSG:
1323 returnsys_recvmsg(a0,
1324 (struct msghdr *) a1,
1325 get_user(args+2));
1327 return-EINVAL;/* to keep gcc happy */
1331 * This function is called by a protocol handler that wants to
1332 * advertise its address family, and have it linked into the
1333 * SOCKET module.
1336 intsock_register(int family,struct proto_ops *ops)
1338 int i;
1340 cli();
1341 for(i =0; i < NPROTO; i++)
1343 if(pops[i] != NULL)
1344 continue;
1345 pops[i] = ops;
1346 pops[i]->family = family;
1347 sti();
1348 return(i);
1350 sti();
1351 return(-ENOMEM);
1355 * This function is called by a protocol handler that wants to
1356 * remove its address family, and have it unlinked from the
1357 * SOCKET module.
1360 intsock_unregister(int family)
1362 int i;
1364 cli();
1365 for(i =0; i < NPROTO; i++)
1367 if(pops[i] == NULL)
1368 continue;
1369 if(pops[i]->family == family)
1371 pops[i]=NULL;
1372 sti();
1373 return(i);
1376 sti();
1377 return(-ENOENT);
1380 voidproto_init(void)
1382 externstruct net_proto protocols[];/* Network protocols */
1383 struct net_proto *pro;
1385 /* Kick all configured protocols. */
1386 pro = protocols;
1387 while(pro->name != NULL)
1389 (*pro->init_func)(pro);
1390 pro++;
1392 /* We're all done... */
1396 voidsock_init(void)
1398 int i;
1400 printk(KERN_INFO "Swansea University Computer Society NET3.035 for Linux 2.0\n");
1403 * Initialize all address (protocol) families.
1406 for(i =0; i < NPROTO; ++i) pops[i] = NULL;
1409 * The netlink device handler may be needed early.
1412 #ifdef CONFIG_NETLINK
1413 init_netlink();
1414 #endif
1416 * Attach the routing/device information port.
1419 #if defined(CONFIG_RTNETLINK)
1420 netlink_attach(NETLINK_ROUTE, netlink_donothing);
1421 #endif
1424 * Attach the firewall module if configured
1427 #ifdef CONFIG_FIREWALL
1428 fwchain_init();
1429 #endif
1432 * Initialize the protocols module.
1435 proto_init();
1438 * Export networking symbols to the world.
1441 #if defined(CONFIG_MODULES) && defined(CONFIG_NET)
1442 export_net_symbols();
1443 #endif
1446 intsocket_get_info(char*buffer,char**start, off_t offset,int length)
1448 int len =sprintf(buffer,"sockets: used %d\n", sockets_in_use);
1449 if(offset >= len)
1451 *start = buffer;
1452 return0;
1454 *start = buffer + offset;
1455 len -= offset;
1456 if(len > length)
1457 len = length;
1458 return len;
close