2 * NET3: Implementation of BSD Unix domain sockets. 4 * Authors: Alan Cox, <alan@cymru.net> 6 * Currently this contains all but the file descriptor passing code. 7 * Before that goes in the odd bugs in the iovec handlers need 8 * fixing, and this bit testing. BSD fd passing is a trivial part 11 * This program is free software; you can redistribute it and/or 12 * modify it under the terms of the GNU General Public License 13 * as published by the Free Software Foundation; either version 14 * 2 of the License, or (at your option) any later version. 17 #include <linux/config.h> 18 #include <linux/kernel.h> 19 #include <linux/major.h> 20 #include <linux/signal.h> 21 #include <linux/sched.h> 22 #include <linux/errno.h> 23 #include <linux/string.h> 24 #include <linux/stat.h> 25 #include <linux/socket.h> 27 #include <linux/fcntl.h> 28 #include <linux/termios.h> 29 #include <linux/socket.h> 30 #include <linux/sockios.h> 31 #include <linux/net.h> 34 #include <linux/malloc.h> 35 #include <asm/segment.h> 36 #include <linux/skbuff.h> 37 /*#include <linux/netprotocol.h>*/ 38 #include <linux/netdevice.h> 41 #include <net/af_unix.h> 43 static unix_socket
*volatile unix_socket_list
=NULL
; 45 #define min(a,b) (((a)<(b))?(a):(b)) 48 * Make sure the unix name is null-terminated. 50 staticinlinevoidunix_mkname(struct sockaddr_un
* sun
,unsigned long len
) 52 if(len
>=sizeof(*sun
)) 58 * Note: Sockets may not be removed _during_ an interrupt or net_bh 59 * handler using this technique. They can be added although we do not 63 static voidunix_remove_socket(unix_socket
*sk
) 71 unix_socket_list
=s
->next
; 88 static voidunix_insert_socket(unix_socket
*sk
) 91 sk
->next
=unix_socket_list
; 96 static unix_socket
*unix_find_socket(struct inode
*i
) 103 if(s
->protinfo
.af_unix
.inode
==i
) 115 * Delete a unix socket. We have to allow for deferring this on a timer. 118 static voidunix_destroy_timer(unsigned long data
) 120 unix_socket
*sk
=(unix_socket
*)data
; 121 if(sk
->protinfo
.af_unix
.locks
==0&& sk
->wmem_alloc
==0) 123 if(sk
->protinfo
.af_unix
.name
) 124 kfree(sk
->protinfo
.af_unix
.name
); 125 kfree_s(sk
,sizeof(*sk
)); 133 init_timer(&sk
->timer
); 134 sk
->timer
.expires
=jiffies
+10*HZ
;/* No real hurry try it every 10 seconds or so */ 135 add_timer(&sk
->timer
); 139 static voidunix_delayed_delete(unix_socket
*sk
) 141 init_timer(&sk
->timer
); 142 sk
->timer
.data
=(unsigned long)sk
; 143 sk
->timer
.expires
=jiffies
+HZ
;/* Normally 1 second after will clean up. After that we try every 10 */ 144 sk
->timer
.function
=unix_destroy_timer
; 145 add_timer(&sk
->timer
); 148 static voidunix_destroy_socket(unix_socket
*sk
) 151 unix_remove_socket(sk
); 153 while((skb
=skb_dequeue(&sk
->receive_queue
))!=NULL
) 155 if(sk
->state
==TCP_LISTEN
) 157 unix_socket
*osk
=skb
->sk
; 158 osk
->state
=TCP_CLOSE
; 159 kfree_skb(skb
, FREE_WRITE
);/* Now surplus - free the skb first before the socket */ 160 osk
->state_change(osk
);/* So the connect wakes and cleans up (if any) */ 161 /* osk will be destroyed when it gets to close or the timer fires */ 165 /* unix_kill_credentials(skb); *//* Throw out any passed fd's */ 166 kfree_skb(skb
,FREE_WRITE
); 170 if(sk
->protinfo
.af_unix
.inode
!=NULL
) 172 iput(sk
->protinfo
.af_unix
.inode
); 173 sk
->protinfo
.af_unix
.inode
=NULL
; 176 if(--sk
->protinfo
.af_unix
.locks
==0&& sk
->wmem_alloc
==0) 178 if(sk
->protinfo
.af_unix
.name
) 179 kfree(sk
->protinfo
.af_unix
.name
); 180 kfree_s(sk
,sizeof(*sk
)); 185 unix_delayed_delete(sk
);/* Try every so often until buffers are all freed */ 190 * Fixme: We need async I/O on AF_UNIX doing next. 193 static intunix_fcntl(struct socket
*sock
,unsigned int cmd
,unsigned long arg
) 199 * Yes socket options work with the new unix domain socketry!!!!!!! 202 static intunix_setsockopt(struct socket
*sock
,int level
,int optname
,char*optval
,int optlen
) 204 unix_socket
*sk
=sock
->data
; 205 if(level
!=SOL_SOCKET
) 207 returnsock_setsockopt(sk
,level
,optname
,optval
,optlen
); 210 static intunix_getsockopt(struct socket
*sock
,int level
,int optname
,char*optval
,int*optlen
) 212 unix_socket
*sk
=sock
->data
; 213 if(level
!=SOL_SOCKET
) 215 returnsock_getsockopt(sk
,level
,optname
,optval
,optlen
); 218 static intunix_listen(struct socket
*sock
,int backlog
) 220 unix_socket
*sk
=sock
->data
; 221 if(sk
->type
!=SOCK_STREAM
) 222 return-EOPNOTSUPP
;/* Only stream sockets accept */ 223 sk
->max_ack_backlog
=backlog
; 224 sk
->state
=TCP_LISTEN
; 228 static voiddef_callback1(struct sock
*sk
) 231 wake_up_interruptible(sk
->sleep
); 234 static voiddef_callback2(struct sock
*sk
,int len
) 237 wake_up_interruptible(sk
->sleep
); 240 static intunix_create(struct socket
*sock
,int protocol
) 243 /* printk("Unix create\n");*/ 245 return-EPROTONOSUPPORT
; 246 sk
=(unix_socket
*)kmalloc(sizeof(*sk
),GFP_KERNEL
); 257 kfree_s(sk
,sizeof(*sk
)); 258 return-ESOCKTNOSUPPORT
; 260 skb_queue_head_init(&sk
->write_queue
); 261 skb_queue_head_init(&sk
->receive_queue
); 262 skb_queue_head_init(&sk
->back_log
); 263 sk
->protinfo
.af_unix
.family
=AF_UNIX
; 264 sk
->protinfo
.af_unix
.inode
=NULL
; 265 sk
->protinfo
.af_unix
.locks
=1;/* Us */ 266 sk
->protinfo
.af_unix
.readsem
=MUTEX
;/* single task reading lock */ 267 sk
->protinfo
.af_unix
.name
=NULL
; 268 sk
->protinfo
.af_unix
.other
=NULL
; 275 sk
->rcvbuf
=SK_RMEM_MAX
; 276 sk
->sndbuf
=SK_WMEM_MAX
; 284 sk
->priority
=SOPRI_NORMAL
; 287 sk
->state_change
=def_callback1
; 288 sk
->data_ready
=def_callback2
; 289 sk
->write_space
=def_callback1
; 290 sk
->error_report
=def_callback1
; 293 sock
->data
=(void*)sk
; 294 sk
->sleep
=sock
->wait
; 296 unix_insert_socket(sk
); 300 static intunix_dup(struct socket
*newsock
,struct socket
*oldsock
) 302 returnunix_create(newsock
,0); 305 static intunix_release(struct socket
*sock
,struct socket
*peer
) 307 unix_socket
*sk
=sock
->data
; 310 /* May not have data attached */ 315 sk
->state_change(sk
); 317 skpair
=(unix_socket
*)sk
->protinfo
.af_unix
.other
;/* Person we send to (default) */ 318 if(sk
->type
==SOCK_STREAM
&& skpair
!=NULL
&& skpair
->state
!=TCP_LISTEN
) 320 skpair
->shutdown
=SHUTDOWN_MASK
;/* No more writes */ 321 skpair
->state_change(skpair
);/* Wake any blocked writes */ 324 skpair
->protinfo
.af_unix
.locks
--;/* It may now die */ 325 sk
->protinfo
.af_unix
.other
=NULL
;/* No pair */ 326 unix_destroy_socket(sk
);/* Try and flush out this socket. Throw our buffers at least */ 331 static unix_socket
*unix_find_other(char*path
,int*error
) 340 err
=open_namei(path
,2, S_IFSOCK
, &inode
, NULL
); 347 u
=unix_find_socket(inode
); 351 *error
=-ECONNREFUSED
; 358 static intunix_bind(struct socket
*sock
,struct sockaddr
*uaddr
,int addr_len
) 360 struct sockaddr_un
*sun
=(struct sockaddr_un
*)uaddr
; 361 unix_socket
*sk
=sock
->data
; 365 if(addr_len
>sizeof(struct sockaddr_un
) || addr_len
<3|| sun
->sun_family
!=AF_UNIX
) 367 unix_mkname(sun
, addr_len
); 369 * Put ourselves in the filesystem 371 if(sk
->protinfo
.af_unix
.inode
!=NULL
) 374 sk
->protinfo
.af_unix
.name
=kmalloc(addr_len
+1, GFP_KERNEL
); 375 if(sk
->protinfo
.af_unix
.name
==NULL
) 377 memcpy(sk
->protinfo
.af_unix
.name
, sun
->sun_path
, addr_len
+1); 382 err
=do_mknod(sk
->protinfo
.af_unix
.name
,S_IFSOCK
|S_IRWXUGO
,0); 384 err
=open_namei(sk
->protinfo
.af_unix
.name
,2, S_IFSOCK
, &sk
->protinfo
.af_unix
.inode
, NULL
); 390 kfree_s(sk
->protinfo
.af_unix
.name
,addr_len
+1); 391 sk
->protinfo
.af_unix
.name
=NULL
; 402 static intunix_connect(struct socket
*sock
,struct sockaddr
*uaddr
,int addr_len
,int flags
) 404 unix_socket
*sk
=sock
->data
; 405 struct sockaddr_un
*sun
=(struct sockaddr_un
*)uaddr
; 410 unix_mkname(sun
, addr_len
); 411 if(sk
->type
==SOCK_STREAM
&& sk
->protinfo
.af_unix
.other
) 413 if(sock
->state
==SS_CONNECTING
&& sk
->state
==TCP_ESTABLISHED
) 415 sock
->state
=SS_CONNECTED
; 418 if(sock
->state
==SS_CONNECTING
&& sk
->state
== TCP_CLOSE
) 420 sock
->state
=SS_UNCONNECTED
; 423 if(sock
->state
==SS_CONNECTING
) 428 if(sun
->sun_family
!=AF_UNIX
) 431 if(sk
->type
==SOCK_DGRAM
&& sk
->protinfo
.af_unix
.other
) 433 sk
->protinfo
.af_unix
.other
->protinfo
.af_unix
.locks
--; 434 sk
->protinfo
.af_unix
.other
=NULL
; 435 sock
->state
=SS_UNCONNECTED
; 438 if(sock
->type
==SOCK_DGRAM
) 440 sock
->state
=SS_CONNECTED
; 441 sk
->state
=TCP_ESTABLISHED
; 446 if(sock
->state
==SS_UNCONNECTED
) 449 * Now ready to connect 452 skb
=sock_alloc_send_skb(sk
,0,0, &err
);/* Marker object */ 455 skb
->sk
=sk
;/* So they know it is us */ 458 unix_mkname(sun
, addr_len
); 459 other
=unix_find_other(sun
->sun_path
, &err
); 462 kfree_skb(skb
, FREE_WRITE
); 465 other
->protinfo
.af_unix
.locks
++;/* Lock the other socket so it doesn't run off for a moment */ 466 other
->ack_backlog
++; 467 sk
->protinfo
.af_unix
.other
=other
; 468 skb_queue_tail(&other
->receive_queue
,skb
); 469 sk
->state
=TCP_SYN_SENT
; 470 sock
->state
=SS_CONNECTING
; 472 other
->data_ready(other
,0);/* Wake up ! */ 476 /* Wait for an accept */ 479 while(sk
->state
==TCP_SYN_SENT
) 486 interruptible_sleep_on(sk
->sleep
); 487 if(current
->signal
& ~current
->blocked
) 495 * Has the other end closed on us ? 498 if(sk
->state
==TCP_CLOSE
) 500 sk
->protinfo
.af_unix
.other
->protinfo
.af_unix
.locks
--; 501 sk
->protinfo
.af_unix
.other
=NULL
; 502 sock
->state
=SS_UNCONNECTED
; 507 * Amazingly it has worked 510 sock
->state
=SS_CONNECTED
; 515 static intunix_socketpair(struct socket
*a
,struct socket
*b
) 518 unix_socket
*ska
,*skb
; 520 err
=unix_create(a
,0); 523 err
=unix_create(b
,0); 526 unix_release(a
, NULL
); 534 /* Join our sockets back to back */ 535 ska
->protinfo
.af_unix
.locks
++; 536 skb
->protinfo
.af_unix
.locks
++; 537 ska
->protinfo
.af_unix
.other
=skb
; 538 skb
->protinfo
.af_unix
.other
=ska
; 539 ska
->state
=TCP_ESTABLISHED
; 540 skb
->state
=TCP_ESTABLISHED
; 544 static intunix_accept(struct socket
*sock
,struct socket
*newsock
,int flags
) 546 unix_socket
*sk
=sock
->data
; 547 unix_socket
*newsk
, *tsk
; 550 if(sk
->type
!=SOCK_STREAM
) 554 if(sk
->state
!=TCP_LISTEN
) 560 if(sk
->protinfo
.af_unix
.name
!=NULL
) 562 newsk
->protinfo
.af_unix
.name
=kmalloc(strlen(sk
->protinfo
.af_unix
.name
)+1, GFP_KERNEL
); 563 if(newsk
->protinfo
.af_unix
.name
==NULL
) 565 strcpy(newsk
->protinfo
.af_unix
.name
, sk
->protinfo
.af_unix
.name
); 571 skb
=skb_dequeue(&sk
->receive_queue
); 579 interruptible_sleep_on(sk
->sleep
); 580 if(current
->signal
& ~current
->blocked
) 590 kfree_skb(skb
, FREE_WRITE
);/* The buffer is just used as a tag */ 592 newsk
->protinfo
.af_unix
.other
=tsk
; 593 tsk
->protinfo
.af_unix
.other
=newsk
; 594 tsk
->state
=TCP_ESTABLISHED
; 595 newsk
->state
=TCP_ESTABLISHED
; 596 newsk
->protinfo
.af_unix
.locks
++;/* Swap lock over */ 597 sk
->protinfo
.af_unix
.locks
--;/* Locked to child socket not master */ 598 tsk
->protinfo
.af_unix
.locks
++;/* Back lock */ 600 tsk
->state_change(tsk
);/* Wake up any sleeping connect */ 604 static intunix_getname(struct socket
*sock
,struct sockaddr
*uaddr
,int*uaddr_len
,int peer
) 606 unix_socket
*sk
=sock
->data
; 607 struct sockaddr_un
*sun
=(struct sockaddr_un
*)uaddr
; 611 if(sk
->protinfo
.af_unix
.other
==NULL
) 613 sk
=sk
->protinfo
.af_unix
.other
; 615 sun
->sun_family
=AF_UNIX
; 616 if(sk
->protinfo
.af_unix
.name
==NULL
) 620 return0;/* Not bound */ 622 *uaddr_len
=sizeof(short)+strlen(sk
->protinfo
.af_unix
.name
)+1; 623 strcpy(sun
->sun_path
,sk
->protinfo
.af_unix
.name
);/* 108 byte limited */ 627 static intunix_sendmsg(struct socket
*sock
,struct msghdr
*msg
,int len
,int nonblock
,int flags
) 629 unix_socket
*sk
=sock
->data
; 631 struct sockaddr_un
*sun
=msg
->msg_name
; 644 if(flags
|| msg
->msg_accrights
)/* For now */ 649 if(sock
->type
==SOCK_STREAM
) 651 if(sk
->state
==TCP_ESTABLISHED
) 659 if(sk
->protinfo
.af_unix
.other
==NULL
) 664 * Optimisation for the fact that under 0.01% of X messages typically 668 if(len
>(sk
->sndbuf
-sizeof(struct sk_buff
))/2)/* Keep two messages in the pipe so it schedules better */ 670 if(sock
->type
==SOCK_DGRAM
) 672 len
=(sk
->sndbuf
-sizeof(struct sk_buff
))/2; 675 size
=/*protocol_size(&proto_unix)+*/len
; 676 skb
=sock_alloc_send_skb(sk
,size
,nonblock
, &err
); 679 /* protocol_adjust(skb,&proto_unix);*/ 682 memcpy_fromiovec(skb_put(skb
,len
),msg
->msg_iov
, len
); 687 other
=sk
->protinfo
.af_unix
.other
; 691 unix_mkname(sun
, msg
->msg_namelen
); 692 other
=unix_find_other(sun
->sun_path
, &err
); 695 kfree_skb(skb
, FREE_WRITE
); 699 skb_queue_tail(&other
->receive_queue
, skb
); 701 other
->data_ready(other
,len
); 705 static intunix_recvmsg(struct socket
*sock
,struct msghdr
*msg
,int size
,int noblock
,int flags
,int*addr_len
) 707 unix_socket
*sk
=sock
->data
; 708 struct sockaddr_un
*sun
=msg
->msg_name
; 715 struct iovec
*iov
=msg
->msg_iov
; 716 int ct
=msg
->msg_iovlen
; 730 /* printk("get rcv sem\n");*/ 731 down(&sk
->protinfo
.af_unix
.readsem
);/* Lock the socket */ 732 /* printk("got rcv sem\n");*/ 743 if(copied
& (flags
&MSG_PEEK
)) 745 up(&sk
->protinfo
.af_unix
.readsem
); 749 skb
=skb_peek(&sk
->receive_queue
); 752 up(&sk
->protinfo
.af_unix
.readsem
); 753 if(sk
->shutdown
&RCV_SHUTDOWN
) 761 interruptible_sleep_on(sk
->sleep
); 762 if( current
->signal
& ~current
->blocked
) 770 down(&sk
->protinfo
.af_unix
.readsem
); 773 if(msg
->msg_name
!=NULL
) 775 sun
->sun_family
=AF_UNIX
; 776 if(skb
->sk
->protinfo
.af_unix
.name
) 778 memcpy(sun
->sun_path
, skb
->sk
->protinfo
.af_unix
.name
,108); 780 *addr_len
=strlen(sun
->sun_path
)+sizeof(short); 784 *addr_len
=sizeof(short); 786 num
=min(skb
->len
,size
-copied
); 791 memcpy_tofs(sp
, skb
->data
, num
); 796 memcpy_tofs(sp
, skb
->data
,num
); 802 kfree_skb(skb
, FREE_WRITE
); 803 if(sock
->type
==SOCK_DGRAM
) 809 up(&sk
->protinfo
.af_unix
.readsem
); 813 static intunix_shutdown(struct socket
*sock
,int mode
) 815 unix_socket
*sk
=(unix_socket
*)sock
->data
; 816 unix_socket
*other
=sk
->protinfo
.af_unix
.other
; 817 if(mode
&SEND_SHUTDOWN
) 819 sk
->shutdown
|=SEND_SHUTDOWN
; 820 sk
->state_change(sk
); 823 other
->shutdown
|=RCV_SHUTDOWN
; 824 other
->state_change(other
); 827 other
=sk
->protinfo
.af_unix
.other
; 828 if(mode
&RCV_SHUTDOWN
) 830 sk
->shutdown
|=RCV_SHUTDOWN
; 831 sk
->state_change(sk
); 834 other
->shutdown
|=SEND_SHUTDOWN
; 835 other
->state_change(other
); 842 static intunix_select(struct socket
*sock
,int sel_type
, select_table
*wait
) 844 returndatagram_select(sock
->data
,sel_type
,wait
); 847 static intunix_ioctl(struct socket
*sock
,unsigned int cmd
,unsigned long arg
) 849 unix_socket
*sk
=sock
->data
; 857 err
=verify_area(VERIFY_WRITE
,(void*)arg
,sizeof(unsigned long)); 860 amount
=sk
->sndbuf
-sk
->wmem_alloc
; 863 put_fs_long(amount
,(unsigned long*)arg
); 868 if(sk
->state
==TCP_LISTEN
) 870 /* These two are safe on a single CPU system as only user tasks fiddle here */ 871 if((skb
=skb_peek(&sk
->receive_queue
))!=NULL
) 873 err
=verify_area(VERIFY_WRITE
,(void*)arg
,sizeof(unsigned long)); 874 put_fs_long(amount
,(unsigned long*)arg
); 885 /* Exported for procfs. */ 887 intunix_get_info(char*buffer
,char**start
, off_t offset
,int length
) 892 unix_socket
*s
=unix_socket_list
; 894 len
+=sprintf(buffer
,"Num RefCount Protocol Flags Type St Path\n"); 898 len
+=sprintf(buffer
+len
,"%p: %08X %08X %08lX %04X %02X", 900 s
->protinfo
.af_unix
.locks
, 905 if(s
->protinfo
.af_unix
.name
!=NULL
) 906 len
+=sprintf(buffer
+len
," %s\n", s
->protinfo
.af_unix
.name
); 916 if(pos
>offset
+length
) 920 *start
=buffer
+(offset
-begin
); 928 * For AF_UNIX we flip everything into an iovec. If this doesnt do any speed harm then it will 929 * be easier for all the low levels to be totally iovec based. 932 static intunix_recvfrom(struct socket
*sock
,void*ubuf
,int size
,int noblock
,unsigned flags
, 933 struct sockaddr
*sa
,int*addr_len
) 939 msg
.msg_name
=(void*)sa
; 940 msg
.msg_namelen
=get_user(addr_len
); 941 msg
.msg_accrights
=NULL
; 944 returnunix_recvmsg(sock
,&msg
,size
,noblock
,flags
,addr_len
); 947 static intunix_read(struct socket
*sock
,char*ubuf
,int size
,int noblock
) 949 returnunix_recvfrom(sock
,ubuf
,size
,noblock
,0,NULL
,NULL
); 952 static intunix_recv(struct socket
*sock
,void*ubuf
,int size
,int noblock
,unsigned int flags
) 954 returnunix_recvfrom(sock
,ubuf
,size
,noblock
,flags
,NULL
,NULL
); 957 static intunix_sendto(struct socket
*sock
,const void*ubuf
,int size
,int noblock
,unsigned flags
, 958 struct sockaddr
*sa
,int addr_len
) 962 iov
.iov_base
=(void*)ubuf
; 964 msg
.msg_name
=(void*)sa
; 965 msg
.msg_namelen
=addr_len
; 966 msg
.msg_accrights
=NULL
; 969 returnunix_sendmsg(sock
,&msg
,size
,noblock
,flags
); 972 static intunix_write(struct socket
*sock
,const char*ubuf
,int size
,int noblock
) 974 returnunix_sendto(sock
,ubuf
,size
,noblock
,0, NULL
,0); 977 static intunix_send(struct socket
*sock
,const void*ubuf
,int size
,int noblock
,unsigned int flags
) 979 returnunix_sendto(sock
,ubuf
,size
,noblock
, flags
, NULL
,0); 983 static struct proto_ops unix_proto_ops
= { 1012 voidunix_proto_init(struct net_proto
*pro
) 1014 printk("NET3: Unix domain sockets 0.07 BETA for Linux NET3.030.\n"); 1015 sock_register(unix_proto_ops
.family
, &unix_proto_ops
);