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> 11 * Anonymous : NOTSOCK/BADF cleanup. Error fix in 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 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 23 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style) 24 * Jeff Uphoff : Made max number of sockets command-line 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 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 54 #include <linux/config.h> 55 #include <linux/signal.h> 56 #include <linux/errno.h> 57 #include <linux/sched.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> 71 #include <linux/kerneld.h> 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); 83 static intsock_lseek(struct inode
*inode
,struct file
*file
, off_t offset
, 85 static intsock_read(struct inode
*inode
,struct file
*file
,char*buf
, 87 static intsock_write(struct inode
*inode
,struct file
*file
,const char*buf
, 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
= { 110 NULL
,/* no special open code... */ 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
) 135 if(ulen
<0||ulen
>MAX_SOCK_ADDR
) 139 if((err
=verify_area(VERIFY_READ
,uaddr
,ulen
))<0) 141 memcpy_fromfs(kaddr
,uaddr
,ulen
); 145 intmove_addr_to_user(void*kaddr
,int klen
,void*uaddr
,int*ulen
) 151 if((err
=verify_area(VERIFY_WRITE
,ulen
,sizeof(*ulen
)))<0) 156 if(len
<0|| len
> MAX_SOCK_ADDR
) 160 if((err
=verify_area(VERIFY_WRITE
,uaddr
,len
))<0) 162 memcpy_tofs(uaddr
,kaddr
,len
); 169 * Obtains the first available file descriptor and sets it up for use. 172 static intget_fd(struct inode
*inode
) 178 * Find a file descriptor suitable for return to the user. 181 file
=get_empty_filp(); 185 for(fd
=0; fd
< NR_OPEN
; ++fd
) 186 if(!current
->files
->fd
[fd
]) 194 FD_CLR(fd
, ¤t
->files
->close_on_exec
); 195 current
->files
->fd
[fd
] = file
; 196 file
->f_op
= &socket_file_ops
; 198 file
->f_flags
= O_RDWR
; 200 file
->f_inode
= inode
; 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
) 229 if(fd
<0|| fd
>= NR_OPEN
|| !(file
= current
->files
->fd
[fd
])) 232 inode
= file
->f_inode
; 233 if(!inode
|| !inode
->i_sock
) 239 returnsocki_lookup(inode
); 246 struct socket
*sock_alloc(void) 248 struct inode
* inode
; 249 struct socket
* sock
; 251 inode
=get_empty_inode(); 255 inode
->i_mode
= S_IFSOCK
; 257 inode
->i_uid
= current
->uid
; 258 inode
->i_gid
= current
->gid
; 260 sock
= &inode
->u
.socket_i
; 261 sock
->state
= SS_UNCONNECTED
; 269 sock
->wait
= &inode
->i_wait
; 270 sock
->inode
= inode
;/* "backlink": we could use pointer arithmetic instead */ 271 sock
->fasync_list
= NULL
; 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
) 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
; 312 sock
->ops
->release(sock
, peersock
); 314 sock_release_peer(peersock
); 315 --sockets_in_use
;/* Bookkeeping.. */ 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
) 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
) 341 sock
=socki_lookup(inode
); 342 if(sock
->flags
& SO_ACCEPTCON
) 347 if(size
==0)/* Match SYS5 behaviour */ 349 if((err
=verify_area(VERIFY_WRITE
,ubuf
,size
))<0) 354 msg
.msg_control
=NULL
; 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
) 373 sock
=socki_lookup(inode
); 375 if(sock
->flags
& SO_ACCEPTCON
) 380 if(size
==0)/* Match SYS5 behaviour */ 383 if((err
=verify_area(VERIFY_READ
,ubuf
,size
))<0) 389 msg
.msg_control
=NULL
; 390 iov
.iov_base
=(void*)ubuf
; 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
, 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
) 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
)); 426 voidsock_close(struct inode
*inode
,struct file
*filp
) 429 * It's possible the inode is NULL if we're closing an unfinished socket. 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
; 450 fna
=(struct fasync_struct
*)kmalloc(sizeof(struct fasync_struct
), GFP_KERNEL
); 455 sock
=socki_lookup(inode
); 457 prev
=&(sock
->fasync_list
); 462 for(fa
=*prev
; fa
!=NULL
; prev
=&fa
->fa_next
,fa
=*prev
) 463 if(fa
->fa_file
==filp
) 470 kfree_s(fna
,sizeof(struct fasync_struct
)); 471 restore_flags(flags
); 475 fna
->magic
=FASYNC_MAGIC
; 476 fna
->fa_next
=sock
->fasync_list
; 477 sock
->fasync_list
=fna
; 484 kfree_s(fa
,sizeof(struct fasync_struct
)); 487 restore_flags(flags
); 491 intsock_wake_async(struct socket
*sock
,int how
) 493 if(!sock
|| !sock
->fasync_list
) 498 kill_fasync(sock
->fasync_list
, SIGIO
); 501 if(!(sock
->flags
& SO_WAITDATA
)) 502 kill_fasync(sock
->fasync_list
, SIGIO
); 505 if(sock
->flags
& SO_NOSPACE
) 507 kill_fasync(sock
->fasync_list
, SIGIO
); 508 sock
->flags
&= ~SO_NOSPACE
; 517 * Perform the socket system call. we locate the appropriate 518 * family, then create a fresh socket. 521 static intfind_protocol_family(int family
) 524 for(i
=0; i
< NPROTO
; i
++) 528 if(pops
[i
]->family
== family
) 534 asmlinkage
intsys_socket(int family
,int type
,int protocol
) 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. */ 547 char module_name
[30]; 548 sprintf(module_name
,"net-pf-%d",family
); 549 request_module(module_name
); 550 i
=find_protocol_family(family
); 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 567 if((type
!= SOCK_STREAM
&& type
!= SOCK_DGRAM
&& 568 type
!= SOCK_SEQPACKET
&& type
!= SOCK_RAW
&& 569 type
!= SOCK_PACKET
) || protocol
<0) 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 578 if(!(sock
=sock_alloc())) 580 printk(KERN_WARNING
"socket: no more sockets\n"); 581 return(-ENOSR
);/* Was: EAGAIN, but we are out of 587 if((i
= sock
->ops
->create(sock
, protocol
)) <0) 593 if((fd
=get_fd(SOCK_INODE(sock
))) <0) 599 sock
->file
=current
->files
->fd
[fd
]; 605 * Create a pair of connected sockets. 608 asmlinkage
intsys_socketpair(int family
,int type
,int protocol
,int usockvec
[2]) 611 struct socket
*sock1
, *sock2
; 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) 621 sock1
=sockfd_lookup(fd1
, NULL
); 622 if(!sock1
->ops
->socketpair
) 629 * Now grab another socket and try to connect the two together. 632 if((fd2
=sys_socket(family
, type
, protocol
)) <0) 638 sock2
=sockfd_lookup(fd2
, NULL
); 639 if((i
= sock1
->ops
->socketpair(sock1
, sock2
)) <0) 648 sock1
->state
= SS_CONNECTED
; 649 sock2
->state
= SS_CONNECTED
; 651 er
=verify_area(VERIFY_WRITE
, usockvec
,sizeof(usockvec
)); 658 put_user(fd1
, &usockvec
[0]); 659 put_user(fd2
, &usockvec
[1]); 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
) 677 char address
[MAX_SOCK_ADDR
]; 680 if(fd
<0|| fd
>= NR_OPEN
|| current
->files
->fd
[fd
] == NULL
) 683 if(!(sock
=sockfd_lookup(fd
, NULL
))) 686 if((err
=move_addr_to_kernel(umyaddr
,addrlen
,address
))<0) 689 if((i
= sock
->ops
->bind(sock
, (struct sockaddr
*)address
, addrlen
)) <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
) 708 if(fd
<0|| fd
>= NR_OPEN
|| current
->files
->fd
[fd
] == NULL
) 710 if(!(sock
=sockfd_lookup(fd
, NULL
))) 713 if(sock
->state
!= SS_UNCONNECTED
) 716 if(sock
->ops
&& sock
->ops
->listen
) 718 err
=sock
->ops
->listen(sock
, backlog
); 720 sock
->flags
|= SO_ACCEPTCON
; 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
) 737 struct socket
*sock
, *newsock
; 739 char address
[MAX_SOCK_ADDR
]; 742 if(fd
<0|| fd
>= NR_OPEN
|| ((file
= current
->files
->fd
[fd
]) == NULL
)) 744 if(!(sock
=sockfd_lookup(fd
, &file
))) 746 if(sock
->state
!= SS_UNCONNECTED
) 750 if(!(sock
->flags
& SO_ACCEPTCON
)) 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 761 newsock
->type
= sock
->type
; 762 newsock
->ops
= sock
->ops
; 763 if((i
= sock
->ops
->dup(newsock
, sock
)) <0) 765 sock_release(newsock
); 769 i
= newsock
->ops
->accept(sock
, newsock
, file
->f_flags
); 772 sock_release(newsock
); 776 if((fd
=get_fd(SOCK_INODE(newsock
))) <0) 778 sock_release(newsock
); 781 newsock
->file
=current
->files
->fd
[fd
]; 785 newsock
->ops
->getname(newsock
, (struct sockaddr
*)address
, &len
,1); 786 move_addr_to_user(address
,len
, upeer_sockaddr
, upeer_addrlen
); 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
) 802 char address
[MAX_SOCK_ADDR
]; 805 if(fd
<0|| fd
>= NR_OPEN
|| (file
=current
->files
->fd
[fd
]) == NULL
) 807 if(!(sock
=sockfd_lookup(fd
, &file
))) 810 if((err
=move_addr_to_kernel(uservaddr
,addrlen
,address
))<0) 816 /* This is ok... continue with connect */ 819 /* Socket is already connected */ 820 if(sock
->type
== SOCK_DGRAM
)/* Hack for now - move this all into the protocol */ 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! 835 i
= sock
->ops
->connect(sock
, (struct sockaddr
*)address
, addrlen
, file
->f_flags
); 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
) 851 char address
[MAX_SOCK_ADDR
]; 855 if(fd
<0|| fd
>= NR_OPEN
|| current
->files
->fd
[fd
] == NULL
) 857 if(!(sock
=sockfd_lookup(fd
, NULL
))) 860 err
=sock
->ops
->getname(sock
, (struct sockaddr
*)address
, &len
,0); 863 if((err
=move_addr_to_user(address
,len
, usockaddr
, usockaddr_len
))<0) 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
) 876 char address
[MAX_SOCK_ADDR
]; 880 if(fd
<0|| fd
>= NR_OPEN
|| current
->files
->fd
[fd
] == NULL
) 882 if(!(sock
=sockfd_lookup(fd
, NULL
))) 885 err
=sock
->ops
->getname(sock
, (struct sockaddr
*)address
, &len
,1); 888 if((err
=move_addr_to_user(address
,len
, usockaddr
, usockaddr_len
))<0) 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
) 906 if(fd
<0|| fd
>= NR_OPEN
|| ((file
= current
->files
->fd
[fd
]) == NULL
)) 908 if(!(sock
=sockfd_lookup(fd
, NULL
))) 913 err
=verify_area(VERIFY_READ
, buff
, len
); 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 932 asmlinkage
intsys_sendto(int fd
,void* buff
,int len
,unsigned flags
, 933 struct sockaddr
*addr
,int addr_len
) 937 char address
[MAX_SOCK_ADDR
]; 942 if(fd
<0|| fd
>= NR_OPEN
|| ((file
= current
->files
->fd
[fd
]) == NULL
)) 944 if(!(sock
=sockfd_lookup(fd
, NULL
))) 949 err
=verify_area(VERIFY_READ
,buff
,len
); 953 if((err
=move_addr_to_kernel(addr
,addr_len
,address
))<0) 958 msg
.msg_name
=address
; 959 msg
.msg_namelen
=addr_len
; 962 msg
.msg_control
=NULL
; 963 return(sock
->ops
->sendmsg(sock
, &msg
, len
, (file
->f_flags
& O_NONBLOCK
), 969 * Receive a datagram from a socket. Call the protocol recvmsg method 972 asmlinkage
intsys_recv(int fd
,void* ubuf
,int size
,unsigned flags
) 980 if(fd
<0|| fd
>= NR_OPEN
|| ((file
= current
->files
->fd
[fd
]) == NULL
)) 983 if(!(sock
=sockfd_lookup(fd
, NULL
))) 990 err
=verify_area(VERIFY_WRITE
, ubuf
, size
); 997 msg
.msg_control
=NULL
; 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
; 1017 char address
[MAX_SOCK_ADDR
]; 1020 if(fd
<0|| fd
>= NR_OPEN
|| ((file
= current
->files
->fd
[fd
]) == NULL
)) 1022 if(!(sock
=sockfd_lookup(fd
, NULL
))) 1029 err
=verify_area(VERIFY_WRITE
,ubuf
,size
); 1033 msg
.msg_control
=NULL
; 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
), 1045 if(addr
!=NULL
&& (err
=move_addr_to_user(address
,alen
, addr
, addr_len
))<0) 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
; 1061 if(fd
<0|| fd
>= NR_OPEN
|| ((file
= current
->files
->fd
[fd
]) == NULL
)) 1063 if(!(sock
=sockfd_lookup(fd
, NULL
))) 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
; 1079 if(fd
<0|| fd
>= NR_OPEN
|| ((file
= current
->files
->fd
[fd
]) == NULL
)) 1081 if(!(sock
=sockfd_lookup(fd
, NULL
))) 1084 if(!sock
->ops
->getsockopt
) 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
; 1099 if(fd
<0|| fd
>= NR_OPEN
|| ((file
= current
->files
->fd
[fd
]) == NULL
)) 1101 if(!(sock
=sockfd_lookup(fd
, NULL
))) 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
; 1115 char address
[MAX_SOCK_ADDR
]; 1116 struct iovec iov
[UIO_MAXIOV
]; 1117 struct msghdr msg_sys
; 1121 if(fd
<0|| fd
>= NR_OPEN
|| ((file
= current
->files
->fd
[fd
]) == NULL
)) 1123 if(!(sock
=sockfd_lookup(fd
, NULL
))) 1126 if(sock
->ops
->sendmsg
==NULL
) 1130 err
=verify_area(VERIFY_READ
, msg
,sizeof(struct msghdr
)); 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
) 1140 /* This will also move the address data into kernel space */ 1141 err
=verify_iovec(&msg_sys
, iov
, address
, VERIFY_READ
); 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
; 1157 struct iovec iov
[UIO_MAXIOV
]; 1158 struct msghdr msg_sys
; 1163 /* kernel mode address */ 1164 char addr
[MAX_SOCK_ADDR
]; 1167 /* user mode address pointers */ 1168 struct sockaddr
*uaddr
; 1171 if(fd
<0|| fd
>= NR_OPEN
|| ((file
= current
->files
->fd
[fd
]) == NULL
)) 1173 if(!(sock
=sockfd_lookup(fd
, NULL
))) 1176 err
=verify_area(VERIFY_READ
, msg
,sizeof(struct msghdr
)); 1179 memcpy_fromfs(&msg_sys
,msg
,sizeof(struct msghdr
)); 1180 if(msg_sys
.msg_iovlen
>UIO_MAXIOV
) 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
); 1195 if(sock
->ops
->recvmsg
==NULL
) 1197 len
=sock
->ops
->recvmsg(sock
, &msg_sys
, total_len
, (file
->f_flags
&O_NONBLOCK
), flags
, &addr_len
); 1202 err
=move_addr_to_user(addr
, addr_len
, uaddr
, uaddr_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
)); 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
) 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
) 1248 er
=verify_area(VERIFY_READ
, args
, nargs
[call
] *sizeof(unsigned long)); 1253 a1
=get_user(args
+1); 1259 return(sys_socket(a0
,a1
,get_user(args
+2))); 1261 return(sys_bind(a0
,(struct sockaddr
*)a1
, 1264 return(sys_connect(a0
, (struct sockaddr
*)a1
, 1267 return(sys_listen(a0
,a1
)); 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
, 1280 (int*)get_user(args
+3))); 1287 return(sys_sendto(a0
,(void*)a1
, 1290 (struct sockaddr
*)get_user(args
+4), 1298 return(sys_recvfrom(a0
, 1302 (struct sockaddr
*)get_user(args
+4), 1303 (int*)get_user(args
+5))); 1305 return(sys_shutdown(a0
,a1
)); 1306 case SYS_SETSOCKOPT
: 1307 return(sys_setsockopt(a0
, 1310 (char*)get_user(args
+3), 1312 case SYS_GETSOCKOPT
: 1313 return(sys_getsockopt(a0
, 1316 (char*)get_user(args
+3), 1317 (int*)get_user(args
+4))); 1319 returnsys_sendmsg(a0
, 1320 (struct msghdr
*) a1
, 1323 returnsys_recvmsg(a0
, 1324 (struct msghdr
*) a1
, 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 1336 intsock_register(int family
,struct proto_ops
*ops
) 1341 for(i
=0; i
< NPROTO
; i
++) 1346 pops
[i
]->family
= family
; 1355 * This function is called by a protocol handler that wants to 1356 * remove its address family, and have it unlinked from the 1360 intsock_unregister(int family
) 1365 for(i
=0; i
< NPROTO
; i
++) 1369 if(pops
[i
]->family
== family
) 1380 voidproto_init(void) 1382 externstruct net_proto protocols
[];/* Network protocols */ 1383 struct net_proto
*pro
; 1385 /* Kick all configured protocols. */ 1387 while(pro
->name
!= NULL
) 1389 (*pro
->init_func
)(pro
); 1392 /* We're all done... */ 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 1416 * Attach the routing/device information port. 1419 #if defined(CONFIG_RTNETLINK) 1420 netlink_attach(NETLINK_ROUTE
, netlink_donothing
); 1424 * Attach the firewall module if configured 1427 #ifdef CONFIG_FIREWALL 1432 * Initialize the protocols module. 1438 * Export networking symbols to the world. 1441 #if defined(CONFIG_MODULES) && defined(CONFIG_NET) 1442 export_net_symbols(); 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
); 1454 *start
= buffer
+ offset
;