diff options
author | Ralf Baechle <ralf@linux-mips.org> | 1997-01-07 02:33:00 +0000 |
---|---|---|
committer | <ralf@linux-mips.org> | 1997-01-07 02:33:00 +0000 |
commit | beb116954b9b7f3bb56412b2494b562f02b864b1 (patch) | |
tree | 120e997879884e1b9d93b265221b939d2ef1ade1 /net/socket.c | |
parent | 908d4681a1dc3792ecafbe64265783a86c4cccb6 (diff) |
Import of Linux/MIPS 2.1.14
Diffstat (limited to 'net/socket.c')
-rw-r--r-- | net/socket.c | 711 |
1 files changed, 454 insertions, 257 deletions
diff --git a/net/socket.c b/net/socket.c index c036b3dca..e96ec9d05 100644 --- a/net/socket.c +++ b/net/socket.c @@ -32,6 +32,9 @@ * Alan Cox : Made sock_alloc()/sock_release() public * for NetROM and future kernel nfsd type * stuff. + * Alan Cox : sendmsg/recvmsg basics. + * Tom Dyas : Export net symbols. + * Marcin Dalecki : Fixed problems with CONFIG_NET="n". * * * This program is free software; you can redistribute it and/or @@ -61,23 +64,34 @@ #include <linux/net.h> #include <linux/interrupt.h> #include <linux/netdevice.h> +#include <linux/proc_fs.h> +#include <linux/firewall.h> + +#if defined(CONFIG_KERNELD) && defined(CONFIG_NET) +#include <linux/kerneld.h> +#endif + +#include <net/netlink.h> #include <asm/system.h> -#include <asm/segment.h> +#include <asm/uaccess.h> + +#if defined(CONFIG_MODULES) && defined(CONFIG_NET) +extern void export_net_symbols(void); +#endif -static int sock_lseek(struct inode *inode, struct file *file, off_t offset, - int whence); -static int sock_read(struct inode *inode, struct file *file, char *buf, - int size); -static int sock_write(struct inode *inode, struct file *file, char *buf, - int size); +static long long sock_lseek(struct inode *inode, struct file *file, + long long offset, int whence); +static long sock_read(struct inode *inode, struct file *file, + char *buf, unsigned long size); +static long sock_write(struct inode *inode, struct file *file, + const char *buf, unsigned long size); static void sock_close(struct inode *inode, struct file *file); static int sock_select(struct inode *inode, struct file *file, int which, select_table *seltable); static int sock_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg); static int sock_fasync(struct inode *inode, struct file *filp, int on); - /* @@ -113,42 +127,43 @@ static int sockets_in_use = 0; * divide and look after the messy bits. */ -#define MAX_SOCK_ADDR 128 /* 108 for Unix domain - 16 for IP, 16 for IPX, about 80 for AX.25 */ +#define MAX_SOCK_ADDR 128 /* 108 for Unix domain - + 16 for IP, 16 for IPX, + 24 for IPv6, + about 80 for AX.25 */ -static int move_addr_to_kernel(void *uaddr, int ulen, void *kaddr) +int move_addr_to_kernel(void *uaddr, int ulen, void *kaddr) { - int err; if(ulen<0||ulen>MAX_SOCK_ADDR) return -EINVAL; if(ulen==0) return 0; - if((err=verify_area(VERIFY_READ,uaddr,ulen))<0) - return err; - memcpy_fromfs(kaddr,uaddr,ulen); + if(copy_from_user(kaddr,uaddr,ulen)) + return -EFAULT; return 0; } -static int move_addr_to_user(void *kaddr, int klen, void *uaddr, int *ulen) +int move_addr_to_user(void *kaddr, int klen, void *uaddr, int *ulen) { int err; int len; - - if((err=verify_area(VERIFY_WRITE,ulen,sizeof(*ulen)))<0) + if((err=get_user(len, ulen))) return err; - len=get_fs_long(ulen); if(len>klen) len=klen; if(len<0 || len> MAX_SOCK_ADDR) return -EINVAL; if(len) { - if((err=verify_area(VERIFY_WRITE,uaddr,len))<0) - return err; - memcpy_tofs(uaddr,kaddr,len); + if(copy_to_user(uaddr,kaddr,len)) + return -EFAULT; } - put_fs_long(len,ulen); - return 0; + /* + * "fromlen shall refer to the value before truncation.." + * 1003.1g + */ + return put_user(klen, ulen); } /* @@ -158,36 +173,31 @@ static int move_addr_to_user(void *kaddr, int klen, void *uaddr, int *ulen) static int get_fd(struct inode *inode) { int fd; - struct file *file; /* * Find a file descriptor suitable for return to the user. */ - file = get_empty_filp(); - if (!file) - return(-1); + fd = get_unused_fd(); + if (fd >= 0) { + struct file *file = get_empty_filp(); - for (fd = 0; fd < NR_OPEN; ++fd) - if (!current->files->fd[fd]) - break; - if (fd == NR_OPEN) - { - file->f_count = 0; - return(-1); - } + if (!file) { + put_unused_fd(fd); + return -ENFILE; + } - FD_CLR(fd, ¤t->files->close_on_exec); current->files->fd[fd] = file; - file->f_op = &socket_file_ops; - file->f_mode = 3; - file->f_flags = O_RDWR; - file->f_count = 1; - file->f_inode = inode; - if (inode) - inode->i_count++; - file->f_pos = 0; - return(fd); + file->f_op = &socket_file_ops; + file->f_mode = 3; + file->f_flags = O_RDWR; + file->f_count = 1; + file->f_inode = inode; + if (inode) + inode->i_count++; + file->f_pos = 0; + } + return fd; } @@ -197,7 +207,8 @@ static int get_fd(struct inode *inode) * The original socket implementation wasn't very clever, which is * why this exists at all.. */ -inline struct socket *socki_lookup(struct inode *inode) + +__inline struct socket *socki_lookup(struct inode *inode) { return &inode->u.socket_i; } @@ -206,7 +217,7 @@ inline struct socket *socki_lookup(struct inode *inode) * Go from a file number to its socket slot. */ -static inline struct socket *sockfd_lookup(int fd, struct file **pfile) +extern __inline struct socket *sockfd_lookup(int fd, struct file **pfile) { struct file *file; struct inode *inode; @@ -250,6 +261,7 @@ struct socket *sock_alloc(void) sock->conn = NULL; sock->iconn = NULL; sock->next = NULL; + sock->file = NULL; sock->wait = &inode->i_wait; sock->inode = inode; /* "backlink": we could use pointer arithmetic instead */ sock->fasync_list = NULL; @@ -297,6 +309,7 @@ void sock_release(struct socket *sock) if (peersock) sock_release_peer(peersock); --sockets_in_use; /* Bookkeeping.. */ + sock->file=NULL; iput(SOCK_INODE(sock)); } @@ -304,9 +317,10 @@ void sock_release(struct socket *sock) * Sockets are not seekable. */ -static int sock_lseek(struct inode *inode, struct file *file, off_t offset, int whence) +static long long sock_lseek(struct inode *inode, struct file *file, + long long offset, int whence) { - return(-ESPIPE); + return -ESPIPE; } /* @@ -314,10 +328,13 @@ static int sock_lseek(struct inode *inode, struct file *file, off_t offset, int * area ubuf...ubuf+size-1 is writable before asking the protocol. */ -static int sock_read(struct inode *inode, struct file *file, char *ubuf, int size) +static long sock_read(struct inode *inode, struct file *file, + char *ubuf, unsigned long size) { struct socket *sock; int err; + struct iovec iov; + struct msghdr msg; sock = socki_lookup(inode); if (sock->flags & SO_ACCEPTCON) @@ -329,7 +346,14 @@ static int sock_read(struct inode *inode, struct file *file, char *ubuf, int siz return 0; if ((err=verify_area(VERIFY_WRITE,ubuf,size))<0) return err; - return(sock->ops->read(sock, ubuf, size, (file->f_flags & O_NONBLOCK))); + msg.msg_name=NULL; + msg.msg_iov=&iov; + msg.msg_iovlen=1; + msg.msg_control=NULL; + iov.iov_base=ubuf; + iov.iov_len=size; + + return(sock->ops->recvmsg(sock, &msg, size,(file->f_flags & O_NONBLOCK), 0,&msg.msg_namelen)); } /* @@ -337,10 +361,13 @@ static int sock_read(struct inode *inode, struct file *file, char *ubuf, int siz * readable by the user process. */ -static int sock_write(struct inode *inode, struct file *file, char *ubuf, int size) +static long sock_write(struct inode *inode, struct file *file, + const char *ubuf, unsigned long size) { struct socket *sock; int err; + struct msghdr msg; + struct iovec iov; sock = socki_lookup(inode); @@ -351,15 +378,23 @@ static int sock_write(struct inode *inode, struct file *file, char *ubuf, int si return -EINVAL; if(size==0) /* Match SYS5 behaviour */ return 0; - + if ((err=verify_area(VERIFY_READ,ubuf,size))<0) return err; - return(sock->ops->write(sock, ubuf, size,(file->f_flags & O_NONBLOCK))); + + msg.msg_name=NULL; + msg.msg_iov=&iov; + msg.msg_iovlen=1; + msg.msg_control=NULL; + iov.iov_base=(void *)ubuf; + iov.iov_len=size; + + return(sock->ops->sendmsg(sock, &msg, size,(file->f_flags & O_NONBLOCK),0)); } /* * With an ioctl arg may well be a user mode pointer, but we don't know what to do - * with it - thats up to the protocol still. + * with it - that's up to the protocol still. */ int sock_ioctl(struct inode *inode, struct file *file, unsigned int cmd, @@ -476,104 +511,51 @@ int sock_wake_async(struct socket *sock, int how) return 0; } - + /* - * Wait for a connection. + * Perform the socket system call. we locate the appropriate + * family, then create a fresh socket. */ -int sock_awaitconn(struct socket *mysock, struct socket *servsock, int flags) +static int find_protocol_family(int family) { - struct socket *last; - - /* - * We must be listening - */ - if (!(servsock->flags & SO_ACCEPTCON)) - { - return(-EINVAL); - } - - /* - * Put ourselves on the server's incomplete connection queue. - */ - - mysock->next = NULL; - cli(); - if (!(last = servsock->iconn)) - servsock->iconn = mysock; - else + register int i; + for (i = 0; i < NPROTO; i++) { - while (last->next) - last = last->next; - last->next = mysock; - } - mysock->state = SS_CONNECTING; - mysock->conn = servsock; - sti(); - - /* - * Wake up server, then await connection. server will set state to - * SS_CONNECTED if we're connected. - */ - wake_up_interruptible(servsock->wait); - sock_wake_async(servsock, 0); - - if (mysock->state != SS_CONNECTED) - { - if (flags & O_NONBLOCK) - return -EINPROGRESS; - - interruptible_sleep_on(mysock->wait); - if (mysock->state != SS_CONNECTED && - mysock->state != SS_DISCONNECTING) - { - /* - * if we're not connected we could have been - * 1) interrupted, so we need to remove ourselves - * from the server list - * 2) rejected (mysock->conn == NULL), and have - * already been removed from the list - */ - if (mysock->conn == servsock) - { - cli(); - if ((last = servsock->iconn) == mysock) - servsock->iconn = mysock->next; - else - { - while (last->next != mysock) - last = last->next; - last->next = mysock->next; - } - sti(); - } - return(mysock->conn ? -EINTR : -EACCES); - } + if (pops[i] == NULL) + continue; + if (pops[i]->family == family) + return i; } - return(0); + return -1; } - -/* - * Perform the socket system call. we locate the appropriate - * family, then create a fresh socket. - */ - -static int sock_socket(int family, int type, int protocol) +asmlinkage int sys_socket(int family, int type, int protocol) { int i, fd; struct socket *sock; struct proto_ops *ops; /* Locate the correct protocol family. */ - for (i = 0; i < NPROTO; ++i) + i = find_protocol_family(family); + +#if defined(CONFIG_KERNELD) && defined(CONFIG_NET) + /* Attempt to load a protocol module if the find failed. + * + * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user + * requested real, full-featured networking support upon configuration. + * Otherwise module support will break! + */ + if (i < 0) { - if (pops[i] == NULL) continue; - if (pops[i]->family == family) - break; + char module_name[30]; + sprintf(module_name,"net-pf-%d",family); + request_module(module_name); + i = find_protocol_family(family); } +#endif - if (i == NPROTO) + if (i < 0) { return -EINVAL; } @@ -599,7 +581,7 @@ static int sock_socket(int family, int type, int protocol) if (!(sock = sock_alloc())) { - printk("NET: sock_socket: no more sockets\n"); + printk(KERN_WARNING "socket: no more sockets\n"); return(-ENOSR); /* Was: EAGAIN, but we are out of system resources! */ } @@ -618,6 +600,8 @@ static int sock_socket(int family, int type, int protocol) return(-EINVAL); } + sock->file=current->files->fd[fd]; + return(fd); } @@ -625,7 +609,7 @@ static int sock_socket(int family, int type, int protocol) * Create a pair of connected sockets. */ -static int sock_socketpair(int family, int type, int protocol, unsigned long usockvec[2]) +asmlinkage int sys_socketpair(int family, int type, int protocol, int usockvec[2]) { int fd1, fd2, i; struct socket *sock1, *sock2; @@ -636,7 +620,7 @@ static int sock_socketpair(int family, int type, int protocol, unsigned long uso * supports the socketpair call. */ - if ((fd1 = sock_socket(family, type, protocol)) < 0) + if ((fd1 = sys_socket(family, type, protocol)) < 0) return(fd1); sock1 = sockfd_lookup(fd1, NULL); if (!sock1->ops->socketpair) @@ -649,7 +633,7 @@ static int sock_socketpair(int family, int type, int protocol, unsigned long uso * Now grab another socket and try to connect the two together. */ - if ((fd2 = sock_socket(family, type, protocol)) < 0) + if ((fd2 = sys_socket(family, type, protocol)) < 0) { sys_close(fd1); return(-EINVAL); @@ -668,17 +652,14 @@ static int sock_socketpair(int family, int type, int protocol, unsigned long uso sock1->state = SS_CONNECTED; sock2->state = SS_CONNECTED; - er=verify_area(VERIFY_WRITE, usockvec, 2 * sizeof(int)); - if(er) - { + er = put_user(fd1, &usockvec[0]); + if (!er) + er = put_user(fd2, &usockvec[1]); + if (er) { sys_close(fd1); sys_close(fd2); - return er; } - put_fs_long(fd1, &usockvec[0]); - put_fs_long(fd2, &usockvec[1]); - - return(0); + return er; } @@ -690,7 +671,7 @@ static int sock_socketpair(int family, int type, int protocol, unsigned long uso * the protocol layer (having also checked the address is ok). */ -static int sock_bind(int fd, struct sockaddr *umyaddr, int addrlen) +asmlinkage int sys_bind(int fd, struct sockaddr *umyaddr, int addrlen) { struct socket *sock; int i; @@ -720,24 +701,26 @@ static int sock_bind(int fd, struct sockaddr *umyaddr, int addrlen) * ready for listening. */ -static int sock_listen(int fd, int backlog) +asmlinkage int sys_listen(int fd, int backlog) { struct socket *sock; - + int err=-EOPNOTSUPP; + if (fd < 0 || fd >= NR_OPEN || current->files->fd[fd] == NULL) return(-EBADF); if (!(sock = sockfd_lookup(fd, NULL))) return(-ENOTSOCK); if (sock->state != SS_UNCONNECTED) - { return(-EINVAL); - } if (sock->ops && sock->ops->listen) - sock->ops->listen(sock, backlog); - sock->flags |= SO_ACCEPTCON; - return(0); + { + err=sock->ops->listen(sock, backlog); + if(!err) + sock->flags |= SO_ACCEPTCON; + } + return(err); } @@ -745,11 +728,15 @@ static int sock_listen(int fd, int backlog) * For accept, we attempt to create a new socket, set up the link * with the client, wake up the client, then return the new * connected fd. We collect the address of the connector in kernel - * space and move it to user at the very end. This is buggy because + * space and move it to user at the very end. This is unclean because * we open the socket then return an error. + * + * 1003.1g addcs the ability to recvmsg() to query connection pending + * status to recvmsg. We need to add that support in a way thats + * clean when we restucture accept also. */ -static int sock_accept(int fd, struct sockaddr *upeer_sockaddr, int *upeer_addrlen) +asmlinkage int sys_accept(int fd, struct sockaddr *upeer_sockaddr, int *upeer_addrlen) { struct file *file; struct socket *sock, *newsock; @@ -772,7 +759,7 @@ static int sock_accept(int fd, struct sockaddr *upeer_sockaddr, int *upeer_addrl if (!(newsock = sock_alloc())) { - printk("NET: sock_accept: no more sockets\n"); + printk(KERN_WARNING "accept: no more sockets\n"); return(-ENOSR); /* Was: EAGAIN, but we are out of system resources! */ } @@ -796,7 +783,8 @@ static int sock_accept(int fd, struct sockaddr *upeer_sockaddr, int *upeer_addrl sock_release(newsock); return(-EINVAL); } - + newsock->file=current->files->fd[fd]; + if (upeer_sockaddr) { newsock->ops->getname(newsock, (struct sockaddr *)address, &len, 1); @@ -809,9 +797,16 @@ static int sock_accept(int fd, struct sockaddr *upeer_sockaddr, int *upeer_addrl /* * Attempt to connect to a socket with the server address. The address * is in user space so we verify it is OK and move it to kernel space. + * + * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to + * break bindings + * + * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and + * other SEQPACKET protocols that take time to connect() as it doesn't + * include the -EINPROGRESS status for such sockets. */ -static int sock_connect(int fd, struct sockaddr *uservaddr, int addrlen) +asmlinkage int sys_connect(int fd, struct sockaddr *uservaddr, int addrlen) { struct socket *sock; struct file *file; @@ -862,7 +857,7 @@ static int sock_connect(int fd, struct sockaddr *uservaddr, int addrlen) * name to user space. */ -static int sock_getsockname(int fd, struct sockaddr *usockaddr, int *usockaddr_len) +asmlinkage int sys_getsockname(int fd, struct sockaddr *usockaddr, int *usockaddr_len) { struct socket *sock; char address[MAX_SOCK_ADDR]; @@ -887,7 +882,7 @@ static int sock_getsockname(int fd, struct sockaddr *usockaddr, int *usockaddr_l * name to user space. */ -static int sock_getpeername(int fd, struct sockaddr *usockaddr, int *usockaddr_len) +asmlinkage int sys_getpeername(int fd, struct sockaddr *usockaddr, int *usockaddr_len) { struct socket *sock; char address[MAX_SOCK_ADDR]; @@ -912,11 +907,13 @@ static int sock_getpeername(int fd, struct sockaddr *usockaddr, int *usockaddr_l * in user space. We check it can be read. */ -static int sock_send(int fd, void * buff, int len, unsigned flags) +asmlinkage int sys_send(int fd, void * buff, size_t len, unsigned flags) { struct socket *sock; struct file *file; int err; + struct msghdr msg; + struct iovec iov; if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL)) return(-EBADF); @@ -928,7 +925,14 @@ static int sock_send(int fd, void * buff, int len, unsigned flags) err=verify_area(VERIFY_READ, buff, len); if(err) return err; - return(sock->ops->send(sock, buff, len, (file->f_flags & O_NONBLOCK), flags)); + + iov.iov_base=buff; + iov.iov_len=len; + msg.msg_name=NULL; + msg.msg_iov=&iov; + msg.msg_iovlen=1; + msg.msg_control=NULL; + return(sock->ops->sendmsg(sock, &msg, len, (file->f_flags & O_NONBLOCK), flags)); } /* @@ -937,13 +941,15 @@ static int sock_send(int fd, void * buff, int len, unsigned flags) * the protocol. */ -static int sock_sendto(int fd, void * buff, int len, unsigned flags, +asmlinkage int sys_sendto(int fd, void * buff, size_t len, unsigned flags, struct sockaddr *addr, int addr_len) { struct socket *sock; struct file *file; char address[MAX_SOCK_ADDR]; int err; + struct msghdr msg; + struct iovec iov; if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL)) return(-EBADF); @@ -955,25 +961,35 @@ static int sock_sendto(int fd, void * buff, int len, unsigned flags, err=verify_area(VERIFY_READ,buff,len); if(err) return err; - - if((err=move_addr_to_kernel(addr,addr_len,address))<0) - return err; - return(sock->ops->sendto(sock, buff, len, (file->f_flags & O_NONBLOCK), - flags, (struct sockaddr *)address, addr_len)); + iov.iov_base=buff; + iov.iov_len=len; + msg.msg_name = NULL; + msg.msg_namelen = 0; + msg.msg_iov=&iov; + msg.msg_iovlen=1; + msg.msg_control=NULL; + if (addr && addr_len) { + err=move_addr_to_kernel(addr,addr_len,address); + if (err < 0) + return err; + msg.msg_name=address; + msg.msg_namelen=addr_len; + } + + return(sock->ops->sendmsg(sock, &msg, len, (file->f_flags & O_NONBLOCK), + flags)); } /* - * Receive a datagram from a socket. This isn't really right. The BSD manual - * pages explicitly state that recv is recvfrom with a NULL to argument. The - * Linux stack gets the right results for the wrong reason and this need to - * be tidied in the inet layer and removed from here. - * We check the buffer is writable and valid. + * Receive a datagram from a socket. Call the protocol recvmsg method */ -static int sock_recv(int fd, void * buff, int len, unsigned flags) +asmlinkage int sys_recv(int fd, void * ubuf, size_t size, unsigned flags) { + struct iovec iov; + struct msghdr msg; struct socket *sock; struct file *file; int err; @@ -984,15 +1000,22 @@ static int sock_recv(int fd, void * buff, int len, unsigned flags) if (!(sock = sockfd_lookup(fd, NULL))) return(-ENOTSOCK); - if(len<0) + if(size<0) return -EINVAL; - if(len==0) + if(size==0) return 0; - err=verify_area(VERIFY_WRITE, buff, len); + err=verify_area(VERIFY_WRITE, ubuf, size); if(err) return err; - - return(sock->ops->recv(sock, buff, len,(file->f_flags & O_NONBLOCK), flags)); + + msg.msg_name=NULL; + msg.msg_iov=&iov; + msg.msg_iovlen=1; + msg.msg_control=NULL; + iov.iov_base=ubuf; + iov.iov_len=size; + + return(sock->ops->recvmsg(sock, &msg, size,(file->f_flags & O_NONBLOCK), flags,&msg.msg_namelen)); } /* @@ -1001,11 +1024,13 @@ static int sock_recv(int fd, void * buff, int len, unsigned flags) * sender address from kernel to user space. */ -static int sock_recvfrom(int fd, void * buff, int len, unsigned flags, +asmlinkage int sys_recvfrom(int fd, void * ubuf, size_t size, unsigned flags, struct sockaddr *addr, int *addr_len) { struct socket *sock; struct file *file; + struct iovec iov; + struct msghdr msg; char address[MAX_SOCK_ADDR]; int err; int alen; @@ -1013,24 +1038,31 @@ static int sock_recvfrom(int fd, void * buff, int len, unsigned flags, return(-EBADF); if (!(sock = sockfd_lookup(fd, NULL))) return(-ENOTSOCK); - if(len<0) + if(size<0) return -EINVAL; - if(len==0) + if(size==0) return 0; - err=verify_area(VERIFY_WRITE,buff,len); + err=verify_area(VERIFY_WRITE,ubuf,size); if(err) return err; - len=sock->ops->recvfrom(sock, buff, len, (file->f_flags & O_NONBLOCK), - flags, (struct sockaddr *)address, &alen); + msg.msg_control=NULL; + msg.msg_iovlen=1; + msg.msg_iov=&iov; + iov.iov_len=size; + iov.iov_base=ubuf; + msg.msg_name=address; + msg.msg_namelen=MAX_SOCK_ADDR; + size=sock->ops->recvmsg(sock, &msg, size, (file->f_flags & O_NONBLOCK), + flags, &alen); - if(len<0) - return len; + if(size<0) + return size; if(addr!=NULL && (err=move_addr_to_user(address,alen, addr, addr_len))<0) return err; - return len; + return size; } /* @@ -1038,7 +1070,7 @@ static int sock_recvfrom(int fd, void * buff, int len, unsigned flags, * to pass the user mode parameter for the protocols to sort out. */ -static int sock_setsockopt(int fd, int level, int optname, char *optval, int optlen) +asmlinkage int sys_setsockopt(int fd, int level, int optname, char *optval, int optlen) { struct socket *sock; struct file *file; @@ -1056,7 +1088,7 @@ static int sock_setsockopt(int fd, int level, int optname, char *optval, int opt * to pass a user mode parameter for the protocols to sort out. */ -static int sock_getsockopt(int fd, int level, int optname, char *optval, int *optlen) +asmlinkage int sys_getsockopt(int fd, int level, int optname, char *optval, int *optlen) { struct socket *sock; struct file *file; @@ -1076,7 +1108,7 @@ static int sock_getsockopt(int fd, int level, int optname, char *optval, int *op * Shutdown a socket. */ -static int sock_shutdown(int fd, int how) +asmlinkage int sys_shutdown(int fd, int how) { struct socket *sock; struct file *file; @@ -1089,6 +1121,149 @@ static int sock_shutdown(int fd, int how) return(sock->ops->shutdown(sock, how)); } +/* + * BSD sendmsg interface + */ + +asmlinkage int sys_sendmsg(int fd, struct msghdr *msg, unsigned int flags) +{ + struct socket *sock; + struct file *file; + char address[MAX_SOCK_ADDR]; + struct iovec iov[UIO_MAXIOV]; + struct msghdr msg_sys; + void * krn_msg_ctl = NULL; + int err; + int total_len; + + if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL)) + return(-EBADF); + if (!(sock = sockfd_lookup(fd, NULL))) + return(-ENOTSOCK); + + if(sock->ops->sendmsg==NULL) + return -EOPNOTSUPP; + + if (copy_from_user(&msg_sys,msg,sizeof(struct msghdr))) + return -EFAULT; + + /* do not move before msg_sys is valid */ + if(msg_sys.msg_iovlen>UIO_MAXIOV) + return -EINVAL; + + /* This will also move the address data into kernel space */ + err = verify_iovec(&msg_sys, iov, address, VERIFY_READ); + if (err < 0) + return err; + total_len=err; + + if (msg_sys.msg_control) + { + krn_msg_ctl = kmalloc(msg_sys.msg_controllen, GFP_KERNEL); + err = copy_from_user(krn_msg_ctl, msg_sys.msg_control, + msg_sys.msg_controllen); + if (err) + return -EFAULT; + msg_sys.msg_control = krn_msg_ctl; + } + + err = sock->ops->sendmsg(sock, &msg_sys, total_len, + (file->f_flags&O_NONBLOCK), flags); + + if (msg_sys.msg_control) + { + kfree(krn_msg_ctl); + } + + return err; +} + +/* + * BSD recvmsg interface + */ + +asmlinkage int sys_recvmsg(int fd, struct msghdr *msg, unsigned int flags) +{ + struct socket *sock; + struct file *file; + struct iovec iov[UIO_MAXIOV]; + struct msghdr msg_sys; + void *usr_msg_ctl = NULL; + void *krn_msg_ctl = NULL; + int err; + int total_len; + int len; + + /* kernel mode address */ + char addr[MAX_SOCK_ADDR]; + int addr_len; + + /* user mode address pointers */ + struct sockaddr *uaddr; + int *uaddr_len; + + if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL)) + return(-EBADF); + if (!(sock = sockfd_lookup(fd, NULL))) + return(-ENOTSOCK); + + if (copy_from_user(&msg_sys,msg,sizeof(struct msghdr))) + return -EFAULT; + + if(msg_sys.msg_iovlen>UIO_MAXIOV) + return -EINVAL; + + /* + * save the user-mode address (verify_iovec will change the + * kernel msghdr to use the kernel address space) + */ + uaddr = msg_sys.msg_name; + uaddr_len = &msg->msg_namelen; + err=verify_iovec(&msg_sys,iov,addr, VERIFY_WRITE); + if(err<0) + return err; + + total_len=err; + + + + if (msg_sys.msg_control) + { + usr_msg_ctl = msg_sys.msg_control; + krn_msg_ctl = kmalloc(msg_sys.msg_controllen, GFP_KERNEL); + err = copy_from_user(krn_msg_ctl, usr_msg_ctl, + msg_sys.msg_controllen); + if (err) + return -EFAULT; + msg_sys.msg_control = krn_msg_ctl; + } + + if(sock->ops->recvmsg==NULL) + return -EOPNOTSUPP; + len=sock->ops->recvmsg(sock, &msg_sys, total_len, (file->f_flags&O_NONBLOCK), flags, &addr_len); + if(len<0) + return len; + + if (uaddr != NULL) + { + err = move_addr_to_user(addr, addr_len, uaddr, uaddr_len); + } + + if (msg_sys.msg_control) + { + if (!err) + { + err = copy_to_user(usr_msg_ctl, krn_msg_ctl, + msg_sys.msg_controllen); + if (err) + err = -EFAULT; + } + kfree(krn_msg_ctl); + } + + return err ? err : len; +} + /* * Perform a file control on a socket file descriptor. @@ -1119,85 +1294,91 @@ int sock_fcntl(struct file *filp, unsigned int cmd, unsigned long arg) asmlinkage int sys_socketcall(int call, unsigned long *args) { - int er; - unsigned char nargs[16]={0,3,3,3,2,3,3,3, - 4,4,4,6,6,2,5,5}; - + unsigned char nargs[18]={0,3,3,3,2,3,3,3, + 4,4,4,6,6,2,5,5,3,3}; + unsigned long a[6]; unsigned long a0,a1; - if(call<1||call>SYS_GETSOCKOPT) + if(call<1||call>SYS_RECVMSG) return -EINVAL; - er=verify_area(VERIFY_READ, args, nargs[call] * sizeof(unsigned long)); - if(er) - return er; + if ((copy_from_user(a, args, nargs[call] * sizeof(unsigned long)))) + return -EFAULT; - a0=get_fs_long(args); - a1=get_fs_long(args+1); + a0=a[0]; + a1=a[1]; switch(call) { case SYS_SOCKET: - return(sock_socket(a0,a1,get_fs_long(args+2))); + return(sys_socket(a0,a1,a[2])); case SYS_BIND: - return(sock_bind(a0,(struct sockaddr *)a1, - get_fs_long(args+2))); + return(sys_bind(a0,(struct sockaddr *)a1, + a[2])); case SYS_CONNECT: - return(sock_connect(a0, (struct sockaddr *)a1, - get_fs_long(args+2))); + return(sys_connect(a0, (struct sockaddr *)a1, + a[2])); case SYS_LISTEN: - return(sock_listen(a0,a1)); + return(sys_listen(a0,a1)); case SYS_ACCEPT: - return(sock_accept(a0,(struct sockaddr *)a1, - (int *)get_fs_long(args+2))); + return(sys_accept(a0,(struct sockaddr *)a1, + (int *)a[2])); case SYS_GETSOCKNAME: - return(sock_getsockname(a0,(struct sockaddr *)a1, - (int *)get_fs_long(args+2))); + return(sys_getsockname(a0,(struct sockaddr *)a1, + (int *)a[2])); case SYS_GETPEERNAME: - return(sock_getpeername(a0, (struct sockaddr *)a1, - (int *)get_fs_long(args+2))); + return(sys_getpeername(a0, (struct sockaddr *)a1, + (int *)a[2])); case SYS_SOCKETPAIR: - return(sock_socketpair(a0,a1, - get_fs_long(args+2), - (unsigned long *)get_fs_long(args+3))); + return(sys_socketpair(a0,a1, + a[2], + (int *)a[3])); case SYS_SEND: - return(sock_send(a0, + return(sys_send(a0, (void *)a1, - get_fs_long(args+2), - get_fs_long(args+3))); + a[2], + a[3])); case SYS_SENDTO: - return(sock_sendto(a0,(void *)a1, - get_fs_long(args+2), - get_fs_long(args+3), - (struct sockaddr *)get_fs_long(args+4), - get_fs_long(args+5))); + return(sys_sendto(a0,(void *)a1, + a[2], + a[3], + (struct sockaddr *)a[4], + a[5])); case SYS_RECV: - return(sock_recv(a0, + return(sys_recv(a0, (void *)a1, - get_fs_long(args+2), - get_fs_long(args+3))); + a[2], + a[3])); case SYS_RECVFROM: - return(sock_recvfrom(a0, + return(sys_recvfrom(a0, (void *)a1, - get_fs_long(args+2), - get_fs_long(args+3), - (struct sockaddr *)get_fs_long(args+4), - (int *)get_fs_long(args+5))); + a[2], + a[3], + (struct sockaddr *)a[4], + (int *)a[5])); case SYS_SHUTDOWN: - return(sock_shutdown(a0,a1)); + return(sys_shutdown(a0,a1)); case SYS_SETSOCKOPT: - return(sock_setsockopt(a0, + return(sys_setsockopt(a0, a1, - get_fs_long(args+2), - (char *)get_fs_long(args+3), - get_fs_long(args+4))); + a[2], + (char *)a[3], + a[4])); case SYS_GETSOCKOPT: - return(sock_getsockopt(a0, + return(sys_getsockopt(a0, a1, - get_fs_long(args+2), - (char *)get_fs_long(args+3), - (int *)get_fs_long(args+4))); + a[2], + (char *)a[3], + (int *)a[4])); + case SYS_SENDMSG: + return sys_sendmsg(a0, + (struct msghdr *) a1, + a[2]); + case SYS_RECVMSG: + return sys_recvmsg(a0, + (struct msghdr *) a1, + a[2]); } return -EINVAL; /* to keep gcc happy */ } @@ -1241,7 +1422,7 @@ int sock_unregister(int family) { if (pops[i] == NULL) continue; - if(pops[i]->family == family) + if (pops[i]->family == family) { pops[i]=NULL; sti(); @@ -1272,34 +1453,50 @@ void sock_init(void) { int i; - printk("Swansea University Computer Society NET3.029 Snap #6 for Linux 1.3.0\n"); + printk(KERN_INFO "Swansea University Computer Society NET3.037 for Linux 2.1\n"); /* * Initialize all address (protocol) families. */ for (i = 0; i < NPROTO; ++i) pops[i] = NULL; + + /* + * The netlink device handler may be needed early. + */ +#ifdef CONFIG_NETLINK + init_netlink(); +#endif /* - * Initialize the protocols module. + * Attach the routing/device information port. */ - proto_init(); +#if defined(CONFIG_RTNETLINK) + netlink_attach(NETLINK_ROUTE, netlink_donothing); +#endif -#ifdef CONFIG_NET - /* - * Initialize the DEV module. + /* + * Attach the firewall module if configured */ + +#ifdef CONFIG_FIREWALL + fwchain_init(); +#endif + + /* + * Initialize the protocols module. + */ + + proto_init(); - dev_init(); - /* - * And the bottom half handler + * Export networking symbols to the world. */ - bh_base[NET_BH].routine= net_bh; - enable_bh(NET_BH); -#endif +#if defined(CONFIG_MODULES) && defined(CONFIG_NET) + export_net_symbols(); +#endif } int socket_get_info(char *buffer, char **start, off_t offset, int length) |