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. 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 52 #include <linux/config.h> 53 #include <linux/signal.h> 54 #include <linux/errno.h> 55 #include <linux/sched.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
, 71 static intsock_read(struct inode
*inode
,struct file
*file
,char*buf
, 73 static intsock_write(struct inode
*inode
,struct file
*file
,const char*buf
, 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
= { 97 NULL
,/* no special open code... */ 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
) 122 if(ulen
<0||ulen
>MAX_SOCK_ADDR
) 126 if((err
=verify_area(VERIFY_READ
,uaddr
,ulen
))<0) 128 memcpy_fromfs(kaddr
,uaddr
,ulen
); 132 intmove_addr_to_user(void*kaddr
,int klen
,void*uaddr
,int*ulen
) 138 if((err
=verify_area(VERIFY_WRITE
,ulen
,sizeof(*ulen
)))<0) 143 if(len
<0|| len
> MAX_SOCK_ADDR
) 147 if((err
=verify_area(VERIFY_WRITE
,uaddr
,len
))<0) 149 memcpy_tofs(uaddr
,kaddr
,len
); 156 * Obtains the first available file descriptor and sets it up for use. 159 static intget_fd(struct inode
*inode
) 165 * Find a file descriptor suitable for return to the user. 168 file
=get_empty_filp(); 172 for(fd
=0; fd
< NR_OPEN
; ++fd
) 173 if(!current
->files
->fd
[fd
]) 181 FD_CLR(fd
, ¤t
->files
->close_on_exec
); 182 current
->files
->fd
[fd
] = file
; 183 file
->f_op
= &socket_file_ops
; 185 file
->f_flags
= O_RDWR
; 187 file
->f_inode
= inode
; 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
) 215 if(fd
<0|| fd
>= NR_OPEN
|| !(file
= current
->files
->fd
[fd
])) 218 inode
= file
->f_inode
; 219 if(!inode
|| !inode
->i_sock
) 225 returnsocki_lookup(inode
); 232 struct socket
*sock_alloc(void) 234 struct inode
* inode
; 235 struct socket
* sock
; 237 inode
=get_empty_inode(); 241 inode
->i_mode
= S_IFSOCK
; 243 inode
->i_uid
= current
->uid
; 244 inode
->i_gid
= current
->gid
; 246 sock
= &inode
->u
.socket_i
; 247 sock
->state
= SS_UNCONNECTED
; 254 sock
->wait
= &inode
->i_wait
; 255 sock
->inode
= inode
;/* "backlink": we could use pointer arithmetic instead */ 256 sock
->fasync_list
= NULL
; 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
) 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
; 297 sock
->ops
->release(sock
, 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
) 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
) 323 sock
=socki_lookup(inode
); 324 if(sock
->flags
& SO_ACCEPTCON
) 329 if(size
==0)/* Match SYS5 behaviour */ 331 if((err
=verify_area(VERIFY_WRITE
,ubuf
,size
))<0) 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
) 346 sock
=socki_lookup(inode
); 348 if(sock
->flags
& SO_ACCEPTCON
) 353 if(size
==0)/* Match SYS5 behaviour */ 356 if((err
=verify_area(VERIFY_READ
,ubuf
,size
))<0) 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
, 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
) 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
)); 391 voidsock_close(struct inode
*inode
,struct file
*filp
) 394 * It's possible the inode is NULL if we're closing an unfinished socket. 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
; 415 fna
=(struct fasync_struct
*)kmalloc(sizeof(struct fasync_struct
), GFP_KERNEL
); 420 sock
=socki_lookup(inode
); 422 prev
=&(sock
->fasync_list
); 427 for(fa
=*prev
; fa
!=NULL
; prev
=&fa
->fa_next
,fa
=*prev
) 428 if(fa
->fa_file
==filp
) 435 kfree_s(fna
,sizeof(struct fasync_struct
)); 436 restore_flags(flags
); 440 fna
->magic
=FASYNC_MAGIC
; 441 fna
->fa_next
=sock
->fasync_list
; 442 sock
->fasync_list
=fna
; 449 kfree_s(fa
,sizeof(struct fasync_struct
)); 452 restore_flags(flags
); 456 intsock_wake_async(struct socket
*sock
,int how
) 458 if(!sock
|| !sock
->fasync_list
) 463 kill_fasync(sock
->fasync_list
, SIGIO
); 466 if(!(sock
->flags
& SO_WAITDATA
)) 467 kill_fasync(sock
->fasync_list
, SIGIO
); 470 if(sock
->flags
& SO_NOSPACE
) 472 kill_fasync(sock
->fasync_list
, SIGIO
); 473 sock
->flags
&= ~SO_NOSPACE
; 482 * Wait for a connection. 485 intsock_awaitconn(struct socket
*mysock
,struct socket
*servsock
,int flags
) 490 * We must be listening 492 if(!(servsock
->flags
& SO_ACCEPTCON
)) 498 * Put ourselves on the server's incomplete connection queue. 503 if(!(last
= servsock
->iconn
)) 504 servsock
->iconn
= mysock
; 511 mysock
->state
= SS_CONNECTING
; 512 mysock
->conn
= servsock
; 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
) 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
) 541 if((last
= servsock
->iconn
) == mysock
) 542 servsock
->iconn
= mysock
->next
; 545 while(last
->next
!= mysock
) 547 last
->next
= mysock
->next
; 551 return(mysock
->conn
? -EINTR
: -EACCES
); 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
) 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
) 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 590 if((type
!= SOCK_STREAM
&& type
!= SOCK_DGRAM
&& 591 type
!= SOCK_SEQPACKET
&& type
!= SOCK_RAW
&& 592 type
!= SOCK_PACKET
) || protocol
<0) 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 601 if(!(sock
=sock_alloc())) 603 printk("NET: sys_socket: no more sockets\n"); 604 return(-ENOSR
);/* Was: EAGAIN, but we are out of 610 if((i
= sock
->ops
->create(sock
, protocol
)) <0) 616 if((fd
=get_fd(SOCK_INODE(sock
))) <0) 626 * Create a pair of connected sockets. 629 asmlinkage
intsys_socketpair(int family
,int type
,int protocol
,int usockvec
[2]) 632 struct socket
*sock1
, *sock2
; 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) 642 sock1
=sockfd_lookup(fd1
, NULL
); 643 if(!sock1
->ops
->socketpair
) 650 * Now grab another socket and try to connect the two together. 653 if((fd2
=sys_socket(family
, type
, protocol
)) <0) 659 sock2
=sockfd_lookup(fd2
, NULL
); 660 if((i
= sock1
->ops
->socketpair(sock1
, sock2
)) <0) 669 sock1
->state
= SS_CONNECTED
; 670 sock2
->state
= SS_CONNECTED
; 672 er
=verify_area(VERIFY_WRITE
, usockvec
,sizeof(usockvec
)); 679 put_user(fd1
, &usockvec
[0]); 680 put_user(fd2
, &usockvec
[1]); 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
) 698 char address
[MAX_SOCK_ADDR
]; 701 if(fd
<0|| fd
>= NR_OPEN
|| current
->files
->fd
[fd
] == NULL
) 704 if(!(sock
=sockfd_lookup(fd
, NULL
))) 707 if((err
=move_addr_to_kernel(umyaddr
,addrlen
,address
))<0) 710 if((i
= sock
->ops
->bind(sock
, (struct sockaddr
*)address
, addrlen
)) <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
) 728 if(fd
<0|| fd
>= NR_OPEN
|| current
->files
->fd
[fd
] == NULL
) 730 if(!(sock
=sockfd_lookup(fd
, NULL
))) 733 if(sock
->state
!= SS_UNCONNECTED
) 738 if(sock
->ops
&& sock
->ops
->listen
) 739 sock
->ops
->listen(sock
, backlog
); 740 sock
->flags
|= SO_ACCEPTCON
; 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
) 756 struct socket
*sock
, *newsock
; 758 char address
[MAX_SOCK_ADDR
]; 761 if(fd
<0|| fd
>= NR_OPEN
|| ((file
= current
->files
->fd
[fd
]) == NULL
)) 763 if(!(sock
=sockfd_lookup(fd
, &file
))) 765 if(sock
->state
!= SS_UNCONNECTED
) 769 if(!(sock
->flags
& SO_ACCEPTCON
)) 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 780 newsock
->type
= sock
->type
; 781 newsock
->ops
= sock
->ops
; 782 if((i
= sock
->ops
->dup(newsock
, sock
)) <0) 784 sock_release(newsock
); 788 i
= newsock
->ops
->accept(sock
, newsock
, file
->f_flags
); 791 sock_release(newsock
); 795 if((fd
=get_fd(SOCK_INODE(newsock
))) <0) 797 sock_release(newsock
); 803 newsock
->ops
->getname(newsock
, (struct sockaddr
*)address
, &len
,1); 804 move_addr_to_user(address
,len
, upeer_sockaddr
, upeer_addrlen
); 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
) 820 char address
[MAX_SOCK_ADDR
]; 823 if(fd
<0|| fd
>= NR_OPEN
|| (file
=current
->files
->fd
[fd
]) == NULL
) 825 if(!(sock
=sockfd_lookup(fd
, &file
))) 828 if((err
=move_addr_to_kernel(uservaddr
,addrlen
,address
))<0) 834 /* This is ok... continue with connect */ 837 /* Socket is already connected */ 838 if(sock
->type
== SOCK_DGRAM
)/* Hack for now - move this all into the protocol */ 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! 853 i
= sock
->ops
->connect(sock
, (struct sockaddr
*)address
, addrlen
, file
->f_flags
); 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
) 869 char address
[MAX_SOCK_ADDR
]; 873 if(fd
<0|| fd
>= NR_OPEN
|| current
->files
->fd
[fd
] == NULL
) 875 if(!(sock
=sockfd_lookup(fd
, NULL
))) 878 err
=sock
->ops
->getname(sock
, (struct sockaddr
*)address
, &len
,0); 881 if((err
=move_addr_to_user(address
,len
, usockaddr
, usockaddr_len
))<0) 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
) 894 char address
[MAX_SOCK_ADDR
]; 898 if(fd
<0|| fd
>= NR_OPEN
|| current
->files
->fd
[fd
] == NULL
) 900 if(!(sock
=sockfd_lookup(fd
, NULL
))) 903 err
=sock
->ops
->getname(sock
, (struct sockaddr
*)address
, &len
,1); 906 if((err
=move_addr_to_user(address
,len
, usockaddr
, usockaddr_len
))<0) 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
) 922 if(fd
<0|| fd
>= NR_OPEN
|| ((file
= current
->files
->fd
[fd
]) == NULL
)) 924 if(!(sock
=sockfd_lookup(fd
, NULL
))) 929 err
=verify_area(VERIFY_READ
, buff
, len
); 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 941 asmlinkage
intsys_sendto(int fd
,void* buff
,int len
,unsigned flags
, 942 struct sockaddr
*addr
,int addr_len
) 946 char address
[MAX_SOCK_ADDR
]; 949 if(fd
<0|| fd
>= NR_OPEN
|| ((file
= current
->files
->fd
[fd
]) == NULL
)) 951 if(!(sock
=sockfd_lookup(fd
, NULL
))) 956 err
=verify_area(VERIFY_READ
,buff
,len
); 960 if((err
=move_addr_to_kernel(addr
,addr_len
,address
))<0) 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
) 982 if(fd
<0|| fd
>= NR_OPEN
|| ((file
= current
->files
->fd
[fd
]) == NULL
)) 985 if(!(sock
=sockfd_lookup(fd
, NULL
))) 992 err
=verify_area(VERIFY_WRITE
, buff
, len
); 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
; 1010 char address
[MAX_SOCK_ADDR
]; 1013 if(fd
<0|| fd
>= NR_OPEN
|| ((file
= current
->files
->fd
[fd
]) == NULL
)) 1015 if(!(sock
=sockfd_lookup(fd
, NULL
))) 1022 err
=verify_area(VERIFY_WRITE
,buff
,len
); 1026 len
=sock
->ops
->recvfrom(sock
, buff
, len
, (file
->f_flags
& O_NONBLOCK
), 1027 flags
, (struct sockaddr
*)address
, &alen
); 1031 if(addr
!=NULL
&& (err
=move_addr_to_user(address
,alen
, addr
, addr_len
))<0) 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
; 1047 if(fd
<0|| fd
>= NR_OPEN
|| ((file
= current
->files
->fd
[fd
]) == NULL
)) 1049 if(!(sock
=sockfd_lookup(fd
, NULL
))) 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
; 1065 if(fd
<0|| fd
>= NR_OPEN
|| ((file
= current
->files
->fd
[fd
]) == NULL
)) 1067 if(!(sock
=sockfd_lookup(fd
, NULL
))) 1070 if(!sock
->ops
->getsockopt
) 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
; 1085 if(fd
<0|| fd
>= NR_OPEN
|| ((file
= current
->files
->fd
[fd
]) == NULL
)) 1087 if(!(sock
=sockfd_lookup(fd
, NULL
))) 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
; 1101 char address
[MAX_SOCK_ADDR
]; 1102 struct iovec iov
[MAX_IOVEC
]; 1103 struct msghdr msg_sys
; 1107 if(fd
<0|| fd
>= NR_OPEN
|| ((file
= current
->files
->fd
[fd
]) == NULL
)) 1109 if(!(sock
=sockfd_lookup(fd
, NULL
))) 1112 err
=verify_area(VERIFY_READ
, msg
,sizeof(struct msghdr
)); 1115 memcpy_fromfs(&msg_sys
,msg
,sizeof(struct msghdr
)); 1116 if(msg_sys
.msg_iovlen
>MAX_IOVEC
) 1118 err
=verify_iovec(&msg_sys
,iov
,address
, VERIFY_READ
); 1123 if(sock
->ops
->sendmsg
==NULL
) 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
; 1136 char address
[MAX_SOCK_ADDR
]; 1137 struct iovec iov
[MAX_IOVEC
]; 1138 struct msghdr msg_sys
; 1144 if(fd
<0|| fd
>= NR_OPEN
|| ((file
= current
->files
->fd
[fd
]) == NULL
)) 1146 if(!(sock
=sockfd_lookup(fd
, NULL
))) 1149 err
=verify_area(VERIFY_READ
, msg
,sizeof(struct msghdr
)); 1152 memcpy_fromfs(&msg_sys
,msg
,sizeof(struct msghdr
)); 1153 if(msg_sys
.msg_iovlen
>MAX_IOVEC
) 1155 err
=verify_iovec(&msg_sys
,iov
,address
, VERIFY_WRITE
); 1160 if(sock
->ops
->recvmsg
==NULL
) 1162 len
=sock
->ops
->recvmsg(sock
, &msg_sys
, total_len
, (file
->f_flags
&O_NONBLOCK
), flags
, &addr_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) 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
)); 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
) 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
) 1212 er
=verify_area(VERIFY_READ
, args
, nargs
[call
] *sizeof(unsigned long)); 1217 a1
=get_user(args
+1); 1223 return(sys_socket(a0
,a1
,get_user(args
+2))); 1225 return(sys_bind(a0
,(struct sockaddr
*)a1
, 1228 return(sys_connect(a0
, (struct sockaddr
*)a1
, 1231 return(sys_listen(a0
,a1
)); 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
, 1244 (int*)get_user(args
+3))); 1251 return(sys_sendto(a0
,(void*)a1
, 1254 (struct sockaddr
*)get_user(args
+4), 1262 return(sys_recvfrom(a0
, 1266 (struct sockaddr
*)get_user(args
+4), 1267 (int*)get_user(args
+5))); 1269 return(sys_shutdown(a0
,a1
)); 1270 case SYS_SETSOCKOPT
: 1271 return(sys_setsockopt(a0
, 1274 (char*)get_user(args
+3), 1276 case SYS_GETSOCKOPT
: 1277 return(sys_getsockopt(a0
, 1280 (char*)get_user(args
+3), 1281 (int*)get_user(args
+4))); 1283 returnsys_sendmsg(a0
, 1284 (struct msghdr
*) a1
, 1287 returnsys_recvmsg(a0
, 1288 (struct msghdr
*) a1
, 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 1300 intsock_register(int family
,struct proto_ops
*ops
) 1305 for(i
=0; i
< NPROTO
; i
++) 1310 pops
[i
]->family
= family
; 1319 * This function is called by a protocol handler that wants to 1320 * remove its address family, and have it unlinked from the 1324 intsock_unregister(int family
) 1329 for(i
=0; i
< NPROTO
; i
++) 1333 if(pops
[i
]->family
== family
) 1344 voidproto_init(void) 1346 externstruct net_proto protocols
[];/* Network protocols */ 1347 struct net_proto
*pro
; 1349 /* Kick all configured protocols. */ 1351 while(pro
->name
!= NULL
) 1353 (*pro
->init_func
)(pro
); 1356 /* We're all done... */ 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. 1380 * Initialize the DEV module. 1386 * And the bottom half handler 1389 bh_base
[NET_BH
].routine
= net_bh
; 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
); 1402 *start
= buffer
+ offset
;