2 * NET4: Implementation of BSD Unix domain sockets.
4 * Authors: Alan Cox, <alan.cox@linux.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 * Version: $Id: af_unix.c,v 1.126.2.5 2002/03/05 12:47:34 davem Exp $
14 * Linus Torvalds : Assorted bug cures.
15 * Niibe Yutaka : async I/O support.
16 * Carsten Paeth : PF_UNIX check, address fixes.
17 * Alan Cox : Limit size of allocated blocks.
18 * Alan Cox : Fixed the stupid socketpair bug.
19 * Alan Cox : BSD compatibility fine tuning.
20 * Alan Cox : Fixed a bug in connect when interrupted.
21 * Alan Cox : Sorted out a proper draft version of
22 * file descriptor passing hacked up from
24 * Marty Leisner : Fixes to fd passing
25 * Nick Nevin : recvmsg bugfix.
26 * Alan Cox : Started proper garbage collector
27 * Heiko EiBfeldt : Missing verify_area check
28 * Alan Cox : Started POSIXisms
29 * Andreas Schwab : Replace inode by dentry for proper
31 * Kirk Petersen : Made this a module
32 * Christoph Rohland : Elegant non-blocking accept/connect algorithm.
34 * Alexey Kuznetosv : Repaired (I hope) bugs introduces
35 * by above two patches.
36 * Andrea Arcangeli : If possible we block in connect(2)
37 * if the max backlog of the listen socket
38 * is been reached. This won't break
39 * old apps and it will avoid huge amount
40 * of socks hashed (this for unix_gc()
41 * performances reasons).
42 * Security fix that limits the max
43 * number of socks to 2*max_files and
44 * the number of skb queueable in the
46 * Artur Skawina : Hash function optimizations
47 * Alexey Kuznetsov : Full scale SMP. Lot of bugs are introduced 8)
48 * Malcolm Beattie : Set peercred for socketpair
49 * Michal Ostrowski : Module initialization cleanup.
52 * Known differences from reference BSD that was tested:
55 * ECONNREFUSED is not returned from one end of a connected() socket to the
56 * other the moment one end closes.
57 * fstat() doesn't return st_dev=NODEV, and give the blksize as high water mark
58 * and a fake inode identifier (nor the BSD first socket fstat twice bug).
60 * accept() returns a path name even if the connecting socket has closed
61 * in the meantime (BSD loses the path and gives up).
62 * accept() returns 0 length path for an unbound connector. BSD returns 16
63 * and a null first byte in the path (but not for gethost/peername - BSD bug ??)
64 * socketpair(...SOCK_RAW..) doesn't panic the kernel.
65 * BSD af_unix apparently has connect forgetting to block properly.
66 * (need to check this with the POSIX spec in detail)
68 * Differences from 2.0.0-11-... (ANK)
69 * Bug fixes and improvements.
70 * - client shutdown killed server socket.
71 * - removed all useless cli/sti pairs.
73 * Semantic changes/extensions.
74 * - generic control message passing.
75 * - SCM_CREDENTIALS control message.
76 * - "Abstract" (not FS based) socket bindings.
77 * Abstract names are sequences of bytes (not zero terminated)
78 * started by 0, so that this name space does not intersect
82 #include <linux/module.h>
83 #include <linux/config.h>
84 #include <linux/kernel.h>
85 #include <linux/major.h>
86 #include <linux/signal.h>
87 #include <linux/sched.h>
88 #include <linux/errno.h>
89 #include <linux/string.h>
90 #include <linux/stat.h>
91 #include <linux/socket.h>
93 #include <linux/fcntl.h>
94 #include <linux/termios.h>
95 #include <linux/sockios.h>
96 #include <linux/net.h>
99 #include <linux/slab.h>
100 #include <asm/uaccess.h>
101 #include <linux/skbuff.h>
102 #include <linux/netdevice.h>
103 #include <net/sock.h>
104 #include <linux/tcp.h>
105 #include <net/af_unix.h>
106 #include <linux/proc_fs.h>
108 #include <linux/init.h>
109 #include <linux/poll.h>
110 #include <linux/smp_lock.h>
111 #include <linux/rtnetlink.h>
113 #include <asm/checksum.h>
115 int sysctl_unix_max_dgram_qlen
= 10;
117 unix_socket
*unix_socket_table
[UNIX_HASH_SIZE
+1];
118 rwlock_t unix_table_lock
= RW_LOCK_UNLOCKED
;
119 static atomic_t unix_nr_socks
= ATOMIC_INIT(0);
121 #define unix_sockets_unbound (unix_socket_table[UNIX_HASH_SIZE])
123 #define UNIX_ABSTRACT(sk) ((sk)->protinfo.af_unix.addr->hash!=UNIX_HASH_SIZE)
126 * SMP locking strategy:
127 * hash table is protected with rwlock unix_table_lock
128 * each socket state is protected by separate rwlock.
131 static inline unsigned unix_hash_fold(unsigned hash
)
135 return hash
&(UNIX_HASH_SIZE
-1);
138 #define unix_peer(sk) ((sk)->pair)
140 static inline int unix_our_peer(unix_socket
*sk
, unix_socket
*osk
)
142 return unix_peer(osk
) == sk
;
145 static inline int unix_may_send(unix_socket
*sk
, unix_socket
*osk
)
147 return (unix_peer(osk
) == NULL
|| unix_our_peer(sk
, osk
));
150 static inline unix_socket
* unix_peer_get(unix_socket
*s
)
158 unix_state_runlock(s
);
162 extern inline void unix_release_addr(struct unix_address
*addr
)
164 if (atomic_dec_and_test(&addr
->refcnt
))
169 * Check unix socket name:
170 * - should be not zero length.
171 * - if started by not zero, should be NULL terminated (FS object)
172 * - if started by zero, it is abstract name.
175 static int unix_mkname(struct sockaddr_un
* sunaddr
, int len
, unsigned *hashp
)
177 if (len
<= sizeof(short) || len
> sizeof(*sunaddr
))
179 if (!sunaddr
|| sunaddr
->sun_family
!= AF_UNIX
)
181 if (sunaddr
->sun_path
[0]) {
182 ((char *)sunaddr
)[len
]=0;
183 len
= strlen(sunaddr
->sun_path
)+1+sizeof(short);
187 *hashp
= unix_hash_fold(csum_partial((char*)sunaddr
, len
, 0));
191 static void __unix_remove_socket(unix_socket
*sk
)
193 unix_socket
**list
= sk
->protinfo
.af_unix
.list
;
196 sk
->next
->prev
= sk
->prev
;
198 sk
->prev
->next
= sk
->next
;
201 sk
->protinfo
.af_unix
.list
= NULL
;
208 static void __unix_insert_socket(unix_socket
**list
, unix_socket
*sk
)
210 BUG_TRAP(sk
->protinfo
.af_unix
.list
==NULL
);
212 sk
->protinfo
.af_unix
.list
= list
;
221 static inline void unix_remove_socket(unix_socket
*sk
)
223 write_lock(&unix_table_lock
);
224 __unix_remove_socket(sk
);
225 write_unlock(&unix_table_lock
);
228 static inline void unix_insert_socket(unix_socket
**list
, unix_socket
*sk
)
230 write_lock(&unix_table_lock
);
231 __unix_insert_socket(list
, sk
);
232 write_unlock(&unix_table_lock
);
235 static unix_socket
*__unix_find_socket_byname(struct sockaddr_un
*sunname
,
236 int len
, int type
, unsigned hash
)
240 for (s
=unix_socket_table
[hash
^type
]; s
; s
=s
->next
) {
241 if(s
->protinfo
.af_unix
.addr
->len
==len
&&
242 memcmp(s
->protinfo
.af_unix
.addr
->name
, sunname
, len
) == 0)
248 static inline unix_socket
*
249 unix_find_socket_byname(struct sockaddr_un
*sunname
,
250 int len
, int type
, unsigned hash
)
254 read_lock(&unix_table_lock
);
255 s
= __unix_find_socket_byname(sunname
, len
, type
, hash
);
258 read_unlock(&unix_table_lock
);
262 static unix_socket
*unix_find_socket_byinode(struct inode
*i
)
266 read_lock(&unix_table_lock
);
267 for (s
=unix_socket_table
[i
->i_ino
& (UNIX_HASH_SIZE
-1)]; s
; s
=s
->next
)
269 struct dentry
*dentry
= s
->protinfo
.af_unix
.dentry
;
271 if(dentry
&& dentry
->d_inode
== i
)
277 read_unlock(&unix_table_lock
);
281 static inline int unix_writable(struct sock
*sk
)
283 return ((atomic_read(&sk
->wmem_alloc
)<<2) <= sk
->sndbuf
);
286 static void unix_write_space(struct sock
*sk
)
288 read_lock(&sk
->callback_lock
);
289 if (unix_writable(sk
)) {
290 if (sk
->sleep
&& waitqueue_active(sk
->sleep
))
291 wake_up_interruptible(sk
->sleep
);
292 sk_wake_async(sk
, 2, POLL_OUT
);
294 read_unlock(&sk
->callback_lock
);
297 /* When dgram socket disconnects (or changes its peer), we clear its receive
298 * queue of packets arrived from previous peer. First, it allows to do
299 * flow control based only on wmem_alloc; second, sk connected to peer
300 * may receive messages only from that peer. */
301 static void unix_dgram_disconnected(struct sock
*sk
, struct sock
*other
)
303 if (skb_queue_len(&sk
->receive_queue
)) {
304 skb_queue_purge(&sk
->receive_queue
);
305 wake_up_interruptible_all(&sk
->protinfo
.af_unix
.peer_wait
);
307 /* If one link of bidirectional dgram pipe is disconnected,
308 * we signal error. Messages are lost. Do not make this,
309 * when peer was not connected to us.
311 if (!other
->dead
&& unix_peer(other
) == sk
) {
312 other
->err
= ECONNRESET
;
313 other
->error_report(other
);
318 static void unix_sock_destructor(struct sock
*sk
)
320 skb_queue_purge(&sk
->receive_queue
);
322 BUG_TRAP(atomic_read(&sk
->wmem_alloc
) == 0);
323 BUG_TRAP(sk
->protinfo
.af_unix
.list
==NULL
);
324 BUG_TRAP(sk
->socket
==NULL
);
326 printk("Attempt to release alive unix socket: %p\n", sk
);
330 if (sk
->protinfo
.af_unix
.addr
)
331 unix_release_addr(sk
->protinfo
.af_unix
.addr
);
333 atomic_dec(&unix_nr_socks
);
334 #ifdef UNIX_REFCNT_DEBUG
335 printk(KERN_DEBUG
"UNIX %p is destroyed, %d are still alive.\n", sk
, atomic_read(&unix_nr_socks
));
340 static int unix_release_sock (unix_socket
*sk
, int embrion
)
342 struct dentry
*dentry
;
343 struct vfsmount
*mnt
;
348 unix_remove_socket(sk
);
351 unix_state_wlock(sk
);
353 sk
->shutdown
= SHUTDOWN_MASK
;
354 dentry
= sk
->protinfo
.af_unix
.dentry
;
355 sk
->protinfo
.af_unix
.dentry
=NULL
;
356 mnt
= sk
->protinfo
.af_unix
.mnt
;
357 sk
->protinfo
.af_unix
.mnt
=NULL
;
359 sk
->state
= TCP_CLOSE
;
360 unix_state_wunlock(sk
);
362 wake_up_interruptible_all(&sk
->protinfo
.af_unix
.peer_wait
);
364 skpair
=unix_peer(sk
);
367 if (sk
->type
==SOCK_STREAM
) {
368 unix_state_wlock(skpair
);
369 skpair
->shutdown
=SHUTDOWN_MASK
; /* No more writes*/
370 if (!skb_queue_empty(&sk
->receive_queue
) || embrion
)
371 skpair
->err
= ECONNRESET
;
372 unix_state_wunlock(skpair
);
373 skpair
->state_change(skpair
);
374 read_lock(&skpair
->callback_lock
);
375 sk_wake_async(skpair
,1,POLL_HUP
);
376 read_unlock(&skpair
->callback_lock
);
377 yield(); /* let the other side wake up */
379 sock_put(skpair
); /* It may now die */
380 unix_peer(sk
) = NULL
;
383 /* Try to flush out this socket. Throw out buffers at least */
385 while((skb
=skb_dequeue(&sk
->receive_queue
))!=NULL
)
387 if (state
==TCP_LISTEN
)
388 unix_release_sock(skb
->sk
, 1);
389 /* passed fds are erased in the kfree_skb hook */
400 /* ---- Socket is dead now and most probably destroyed ---- */
403 * Fixme: BSD difference: In BSD all sockets connected to use get
404 * ECONNRESET and we die on the spot. In Linux we behave
405 * like files and pipes do and wait for the last
408 * Can't we simply set sock->err?
410 * What the above comment does talk about? --ANK(980817)
413 if (atomic_read(&unix_tot_inflight
))
414 unix_gc(); /* Garbage collect fds */
419 static int unix_listen(struct socket
*sock
, int backlog
)
422 struct sock
*sk
= sock
->sk
;
425 if (sock
->type
!=SOCK_STREAM
)
426 goto out
; /* Only stream sockets accept */
428 if (!sk
->protinfo
.af_unix
.addr
)
429 goto out
; /* No listens on an unbound socket */
430 unix_state_wlock(sk
);
431 if (sk
->state
!= TCP_CLOSE
&& sk
->state
!= TCP_LISTEN
)
433 if (backlog
> sk
->max_ack_backlog
)
434 wake_up_interruptible_all(&sk
->protinfo
.af_unix
.peer_wait
);
435 sk
->max_ack_backlog
=backlog
;
436 sk
->state
=TCP_LISTEN
;
437 /* set credentials so connect can copy them */
438 sk
->peercred
.pid
= current
->pid
;
439 sk
->peercred
.uid
= current
->euid
;
440 sk
->peercred
.gid
= current
->egid
;
444 unix_state_wunlock(sk
);
449 extern struct proto_ops unix_stream_ops
;
450 extern struct proto_ops unix_dgram_ops
;
452 static struct sock
* unix_create1(struct socket
*sock
)
456 if (atomic_read(&unix_nr_socks
) >= 2*files_stat
.max_files
)
460 sk
= sk_alloc(PF_UNIX
, GFP_KERNEL
, 1);
466 atomic_inc(&unix_nr_socks
);
468 sock_init_data(sock
,sk
);
470 sk
->write_space
= unix_write_space
;
472 sk
->max_ack_backlog
= sysctl_unix_max_dgram_qlen
;
473 sk
->destruct
= unix_sock_destructor
;
474 sk
->protinfo
.af_unix
.dentry
=NULL
;
475 sk
->protinfo
.af_unix
.mnt
=NULL
;
476 sk
->protinfo
.af_unix
.lock
= RW_LOCK_UNLOCKED
;
477 atomic_set(&sk
->protinfo
.af_unix
.inflight
, sock
? 0 : -1);
478 init_MUTEX(&sk
->protinfo
.af_unix
.readsem
);/* single task reading lock */
479 init_waitqueue_head(&sk
->protinfo
.af_unix
.peer_wait
);
480 sk
->protinfo
.af_unix
.list
=NULL
;
481 unix_insert_socket(&unix_sockets_unbound
, sk
);
486 static int unix_create(struct socket
*sock
, int protocol
)
488 if (protocol
&& protocol
!= PF_UNIX
)
489 return -EPROTONOSUPPORT
;
491 sock
->state
= SS_UNCONNECTED
;
493 switch (sock
->type
) {
495 sock
->ops
= &unix_stream_ops
;
498 * Believe it or not BSD has AF_UNIX, SOCK_RAW though
502 sock
->type
=SOCK_DGRAM
;
504 sock
->ops
= &unix_dgram_ops
;
507 return -ESOCKTNOSUPPORT
;
510 return unix_create1(sock
) ? 0 : -ENOMEM
;
513 static int unix_release(struct socket
*sock
)
515 unix_socket
*sk
= sock
->sk
;
522 return unix_release_sock (sk
, 0);
525 static int unix_autobind(struct socket
*sock
)
527 struct sock
*sk
= sock
->sk
;
528 static u32 ordernum
= 1;
529 struct unix_address
* addr
;
532 down(&sk
->protinfo
.af_unix
.readsem
);
535 if (sk
->protinfo
.af_unix
.addr
)
539 addr
= kmalloc(sizeof(*addr
) + sizeof(short) + 16, GFP_KERNEL
);
543 memset(addr
, 0, sizeof(*addr
) + sizeof(short) + 16);
544 addr
->name
->sun_family
= AF_UNIX
;
545 atomic_set(&addr
->refcnt
, 1);
548 addr
->len
= sprintf(addr
->name
->sun_path
+1, "%05x", ordernum
) + 1 + sizeof(short);
549 addr
->hash
= unix_hash_fold(csum_partial((void*)addr
->name
, addr
->len
, 0));
551 write_lock(&unix_table_lock
);
552 ordernum
= (ordernum
+1)&0xFFFFF;
554 if (__unix_find_socket_byname(addr
->name
, addr
->len
, sock
->type
,
556 write_unlock(&unix_table_lock
);
557 /* Sanity yield. It is unusual case, but yet... */
558 if (!(ordernum
&0xFF))
562 addr
->hash
^= sk
->type
;
564 __unix_remove_socket(sk
);
565 sk
->protinfo
.af_unix
.addr
= addr
;
566 __unix_insert_socket(&unix_socket_table
[addr
->hash
], sk
);
567 write_unlock(&unix_table_lock
);
571 up(&sk
->protinfo
.af_unix
.readsem
);
575 static unix_socket
*unix_find_other(struct sockaddr_un
*sunname
, int len
,
576 int type
, unsigned hash
, int *error
)
582 if (sunname
->sun_path
[0]) {
583 if (path_init(sunname
->sun_path
,
584 LOOKUP_POSITIVE
|LOOKUP_FOLLOW
, &nd
))
585 err
= path_walk(sunname
->sun_path
, &nd
);
588 err
= permission(nd
.dentry
->d_inode
,MAY_WRITE
);
593 if (!S_ISSOCK(nd
.dentry
->d_inode
->i_mode
))
595 u
=unix_find_socket_byinode(nd
.dentry
->d_inode
);
600 UPDATE_ATIME(nd
.dentry
->d_inode
);
605 if (u
->type
!= type
) {
611 u
=unix_find_socket_byname(sunname
, len
, type
, hash
);
613 struct dentry
*dentry
;
614 dentry
= u
->protinfo
.af_unix
.dentry
;
616 UPDATE_ATIME(dentry
->d_inode
);
630 static int unix_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
632 struct sock
*sk
= sock
->sk
;
633 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
634 struct dentry
* dentry
= NULL
;
638 struct unix_address
*addr
;
642 if (sunaddr
->sun_family
!= AF_UNIX
)
645 if (addr_len
==sizeof(short)) {
646 err
= unix_autobind(sock
);
650 err
= unix_mkname(sunaddr
, addr_len
, &hash
);
655 down(&sk
->protinfo
.af_unix
.readsem
);
658 if (sk
->protinfo
.af_unix
.addr
)
662 addr
= kmalloc(sizeof(*addr
)+addr_len
, GFP_KERNEL
);
666 memcpy(addr
->name
, sunaddr
, addr_len
);
667 addr
->len
= addr_len
;
668 addr
->hash
= hash
^sk
->type
;
669 atomic_set(&addr
->refcnt
, 1);
671 if (sunaddr
->sun_path
[0]) {
675 * Get the parent directory, calculate the hash for last
678 if (path_init(sunaddr
->sun_path
, LOOKUP_PARENT
, &nd
))
679 err
= path_walk(sunaddr
->sun_path
, &nd
);
681 goto out_mknod_parent
;
683 * Yucky last component or no last component at all?
684 * (foo/., foo/.., /////)
687 if (nd
.last_type
!= LAST_NORM
)
690 * Lock the directory.
692 down(&nd
.dentry
->d_inode
->i_sem
);
694 * Do the final lookup.
696 dentry
= lookup_hash(&nd
.last
, nd
.dentry
);
697 err
= PTR_ERR(dentry
);
699 goto out_mknod_unlock
;
702 * Special case - lookup gave negative, but... we had foo/bar/
703 * From the vfs_mknod() POV we just have a negative dentry -
704 * all is fine. Let's be bastards - you had / on the end, you've
705 * been asking for (non-existent) directory. -ENOENT for you.
707 if (nd
.last
.name
[nd
.last
.len
] && !dentry
->d_inode
)
710 * All right, let's create it.
712 mode
= S_IFSOCK
| (sock
->inode
->i_mode
& ~current
->fs
->umask
);
713 err
= vfs_mknod(nd
.dentry
->d_inode
, dentry
, mode
, 0);
716 up(&nd
.dentry
->d_inode
->i_sem
);
720 addr
->hash
= UNIX_HASH_SIZE
;
723 write_lock(&unix_table_lock
);
725 if (!sunaddr
->sun_path
[0]) {
727 if (__unix_find_socket_byname(sunaddr
, addr_len
,
729 unix_release_addr(addr
);
733 list
= &unix_socket_table
[addr
->hash
];
735 list
= &unix_socket_table
[dentry
->d_inode
->i_ino
& (UNIX_HASH_SIZE
-1)];
736 sk
->protinfo
.af_unix
.dentry
= nd
.dentry
;
737 sk
->protinfo
.af_unix
.mnt
= nd
.mnt
;
741 __unix_remove_socket(sk
);
742 sk
->protinfo
.af_unix
.addr
= addr
;
743 __unix_insert_socket(list
, sk
);
746 write_unlock(&unix_table_lock
);
748 up(&sk
->protinfo
.af_unix
.readsem
);
755 up(&nd
.dentry
->d_inode
->i_sem
);
761 unix_release_addr(addr
);
765 static int unix_dgram_connect(struct socket
*sock
, struct sockaddr
*addr
,
768 struct sock
*sk
= sock
->sk
;
769 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)addr
;
774 if (addr
->sa_family
!= AF_UNSPEC
) {
775 err
= unix_mkname(sunaddr
, alen
, &hash
);
780 if (sock
->passcred
&& !sk
->protinfo
.af_unix
.addr
&&
781 (err
= unix_autobind(sock
)) != 0)
784 other
=unix_find_other(sunaddr
, alen
, sock
->type
, hash
, &err
);
788 unix_state_wlock(sk
);
791 if (!unix_may_send(sk
, other
))
795 * 1003.1g breaking connected state with AF_UNSPEC
798 unix_state_wlock(sk
);
802 * If it was connected, reconnect.
805 struct sock
*old_peer
= unix_peer(sk
);
807 unix_state_wunlock(sk
);
809 if (other
!= old_peer
)
810 unix_dgram_disconnected(sk
, old_peer
);
814 unix_state_wunlock(sk
);
819 unix_state_wunlock(sk
);
825 static long unix_wait_for_peer(unix_socket
*other
, long timeo
)
828 DECLARE_WAITQUEUE(wait
, current
);
830 __set_current_state(TASK_INTERRUPTIBLE
);
831 add_wait_queue_exclusive(&other
->protinfo
.af_unix
.peer_wait
, &wait
);
833 sched
= (!other
->dead
&&
834 !(other
->shutdown
&RCV_SHUTDOWN
) &&
835 skb_queue_len(&other
->receive_queue
) >= other
->max_ack_backlog
);
837 unix_state_runlock(other
);
840 timeo
= schedule_timeout(timeo
);
842 __set_current_state(TASK_RUNNING
);
843 remove_wait_queue(&other
->protinfo
.af_unix
.peer_wait
, &wait
);
847 static int unix_stream_connect(struct socket
*sock
, struct sockaddr
*uaddr
,
848 int addr_len
, int flags
)
850 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
851 struct sock
*sk
= sock
->sk
;
852 struct sock
*newsk
= NULL
;
853 unix_socket
*other
= NULL
;
854 struct sk_buff
*skb
= NULL
;
860 err
= unix_mkname(sunaddr
, addr_len
, &hash
);
865 if (sock
->passcred
&& !sk
->protinfo
.af_unix
.addr
&&
866 (err
= unix_autobind(sock
)) != 0)
869 timeo
= sock_sndtimeo(sk
, flags
& O_NONBLOCK
);
871 /* First of all allocate resources.
872 If we will make it after state is locked,
873 we will have to recheck all again in any case.
878 /* create new sock for complete connection */
879 newsk
= unix_create1(NULL
);
883 /* Allocate skb for sending to listening sock */
884 skb
= sock_wmalloc(newsk
, 1, 0, GFP_KERNEL
);
889 /* Find listening sock. */
890 other
=unix_find_other(sunaddr
, addr_len
, sk
->type
, hash
, &err
);
894 /* Latch state of peer */
895 unix_state_rlock(other
);
897 /* Apparently VFS overslept socket death. Retry. */
899 unix_state_runlock(other
);
905 if (other
->state
!= TCP_LISTEN
)
908 if (other
->shutdown
& RCV_SHUTDOWN
)
911 if (skb_queue_len(&other
->receive_queue
) >= other
->max_ack_backlog
) {
916 timeo
= unix_wait_for_peer(other
, timeo
);
918 err
= sock_intr_errno(timeo
);
919 if (signal_pending(current
))
927 It is tricky place. We need to grab write lock and cannot
928 drop lock on peer. It is dangerous because deadlock is
929 possible. Connect to self case and simultaneous
930 attempt to connect are eliminated by checking socket
931 state. other is TCP_LISTEN, if sk is TCP_LISTEN we
932 check this before attempt to grab lock.
934 Well, and we have to recheck the state after socket locked.
940 /* This is ok... continue with connect */
942 case TCP_ESTABLISHED
:
943 /* Socket is already connected */
951 unix_state_wlock(sk
);
953 if (sk
->state
!= st
) {
954 unix_state_wunlock(sk
);
955 unix_state_runlock(other
);
960 /* The way is open! Fastly set all the necessary fields... */
964 newsk
->state
=TCP_ESTABLISHED
;
965 newsk
->type
=SOCK_STREAM
;
966 newsk
->peercred
.pid
= current
->pid
;
967 newsk
->peercred
.uid
= current
->euid
;
968 newsk
->peercred
.gid
= current
->egid
;
969 newsk
->sleep
= &newsk
->protinfo
.af_unix
.peer_wait
;
971 /* copy address information from listening to new sock*/
972 if (other
->protinfo
.af_unix
.addr
)
974 atomic_inc(&other
->protinfo
.af_unix
.addr
->refcnt
);
975 newsk
->protinfo
.af_unix
.addr
=other
->protinfo
.af_unix
.addr
;
977 if (other
->protinfo
.af_unix
.dentry
) {
978 newsk
->protinfo
.af_unix
.dentry
=dget(other
->protinfo
.af_unix
.dentry
);
979 newsk
->protinfo
.af_unix
.mnt
=mntget(other
->protinfo
.af_unix
.mnt
);
982 /* Set credentials */
983 sk
->peercred
= other
->peercred
;
987 sock
->state
=SS_CONNECTED
;
988 sk
->state
=TCP_ESTABLISHED
;
990 unix_state_wunlock(sk
);
992 /* take ten and and send info to listening sock */
993 spin_lock(&other
->receive_queue
.lock
);
994 __skb_queue_tail(&other
->receive_queue
,skb
);
995 /* Undo artificially decreased inflight after embrion
996 * is installed to listening socket. */
997 atomic_inc(&newsk
->protinfo
.af_unix
.inflight
);
998 spin_unlock(&other
->receive_queue
.lock
);
999 unix_state_runlock(other
);
1000 other
->data_ready(other
, 0);
1006 unix_state_runlock(other
);
1012 unix_release_sock(newsk
, 0);
1018 static int unix_socketpair(struct socket
*socka
, struct socket
*sockb
)
1020 struct sock
*ska
=socka
->sk
, *skb
= sockb
->sk
;
1022 /* Join our sockets back to back */
1027 ska
->peercred
.pid
= skb
->peercred
.pid
= current
->pid
;
1028 ska
->peercred
.uid
= skb
->peercred
.uid
= current
->euid
;
1029 ska
->peercred
.gid
= skb
->peercred
.gid
= current
->egid
;
1031 if (ska
->type
!= SOCK_DGRAM
)
1033 ska
->state
=TCP_ESTABLISHED
;
1034 skb
->state
=TCP_ESTABLISHED
;
1035 socka
->state
=SS_CONNECTED
;
1036 sockb
->state
=SS_CONNECTED
;
1041 static int unix_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
1043 unix_socket
*sk
= sock
->sk
;
1045 struct sk_buff
*skb
;
1049 if (sock
->type
!=SOCK_STREAM
)
1053 if (sk
->state
!=TCP_LISTEN
)
1056 /* If socket state is TCP_LISTEN it cannot change (for now...),
1057 * so that no locks are necessary.
1060 skb
= skb_recv_datagram(sk
, 0, flags
&O_NONBLOCK
, &err
);
1062 /* This means receive shutdown. */
1069 skb_free_datagram(sk
, skb
);
1070 wake_up_interruptible(&sk
->protinfo
.af_unix
.peer_wait
);
1072 /* attach accepted sock to socket */
1073 unix_state_wlock(tsk
);
1074 newsock
->state
= SS_CONNECTED
;
1075 sock_graft(tsk
, newsock
);
1076 unix_state_wunlock(tsk
);
1084 static int unix_getname(struct socket
*sock
, struct sockaddr
*uaddr
, int *uaddr_len
, int peer
)
1086 struct sock
*sk
= sock
->sk
;
1087 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
1091 sk
= unix_peer_get(sk
);
1101 unix_state_rlock(sk
);
1102 if (!sk
->protinfo
.af_unix
.addr
) {
1103 sunaddr
->sun_family
= AF_UNIX
;
1104 sunaddr
->sun_path
[0] = 0;
1105 *uaddr_len
= sizeof(short);
1107 struct unix_address
*addr
= sk
->protinfo
.af_unix
.addr
;
1109 *uaddr_len
= addr
->len
;
1110 memcpy(sunaddr
, addr
->name
, *uaddr_len
);
1112 unix_state_runlock(sk
);
1118 static void unix_detach_fds(struct scm_cookie
*scm
, struct sk_buff
*skb
)
1122 scm
->fp
= UNIXCB(skb
).fp
;
1123 skb
->destructor
= sock_wfree
;
1124 UNIXCB(skb
).fp
= NULL
;
1126 for (i
=scm
->fp
->count
-1; i
>=0; i
--)
1127 unix_notinflight(scm
->fp
->fp
[i
]);
1130 static void unix_destruct_fds(struct sk_buff
*skb
)
1132 struct scm_cookie scm
;
1133 memset(&scm
, 0, sizeof(scm
));
1134 unix_detach_fds(&scm
, skb
);
1136 /* Alas, it calls VFS */
1137 /* So fscking what? fput() had been SMP-safe since the last Summer */
1142 static void unix_attach_fds(struct scm_cookie
*scm
, struct sk_buff
*skb
)
1145 for (i
=scm
->fp
->count
-1; i
>=0; i
--)
1146 unix_inflight(scm
->fp
->fp
[i
]);
1147 UNIXCB(skb
).fp
= scm
->fp
;
1148 skb
->destructor
= unix_destruct_fds
;
1153 * Send AF_UNIX data.
1156 static int unix_dgram_sendmsg(struct socket
*sock
, struct msghdr
*msg
, int len
,
1157 struct scm_cookie
*scm
)
1159 struct sock
*sk
= sock
->sk
;
1160 struct sockaddr_un
*sunaddr
=msg
->msg_name
;
1161 unix_socket
*other
= NULL
;
1162 int namelen
= 0; /* fake GCC */
1165 struct sk_buff
*skb
;
1171 if (msg
->msg_flags
&MSG_OOB
)
1174 if (msg
->msg_namelen
) {
1175 err
= unix_mkname(sunaddr
, msg
->msg_namelen
, &hash
);
1182 other
= unix_peer_get(sk
);
1187 if (sock
->passcred
&& !sk
->protinfo
.af_unix
.addr
&&
1188 (err
= unix_autobind(sock
)) != 0)
1192 if ((unsigned)len
> sk
->sndbuf
- 32)
1195 skb
= sock_alloc_send_skb(sk
, len
, msg
->msg_flags
&MSG_DONTWAIT
, &err
);
1199 memcpy(UNIXCREDS(skb
), &scm
->creds
, sizeof(struct ucred
));
1201 unix_attach_fds(scm
, skb
);
1203 skb
->h
.raw
= skb
->data
;
1204 err
= memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
);
1208 timeo
= sock_sndtimeo(sk
, msg
->msg_flags
& MSG_DONTWAIT
);
1213 if (sunaddr
== NULL
)
1216 other
= unix_find_other(sunaddr
, namelen
, sk
->type
, hash
, &err
);
1221 unix_state_rlock(other
);
1223 if (!unix_may_send(sk
, other
))
1228 * Check with 1003.1g - what should
1231 unix_state_runlock(other
);
1235 unix_state_wlock(sk
);
1236 if (unix_peer(sk
) == other
) {
1238 unix_state_wunlock(sk
);
1240 unix_dgram_disconnected(sk
, other
);
1242 err
= -ECONNREFUSED
;
1244 unix_state_wunlock(sk
);
1254 if (other
->shutdown
&RCV_SHUTDOWN
)
1257 if (unix_peer(other
) != sk
&&
1258 skb_queue_len(&other
->receive_queue
) >= other
->max_ack_backlog
) {
1264 timeo
= unix_wait_for_peer(other
, timeo
);
1266 err
= sock_intr_errno(timeo
);
1267 if (signal_pending(current
))
1273 skb_queue_tail(&other
->receive_queue
, skb
);
1274 unix_state_runlock(other
);
1275 other
->data_ready(other
, len
);
1280 unix_state_runlock(other
);
1290 static int unix_stream_sendmsg(struct socket
*sock
, struct msghdr
*msg
, int len
,
1291 struct scm_cookie
*scm
)
1293 struct sock
*sk
= sock
->sk
;
1294 unix_socket
*other
= NULL
;
1295 struct sockaddr_un
*sunaddr
=msg
->msg_name
;
1297 struct sk_buff
*skb
;
1303 if (msg
->msg_flags
&MSG_OOB
)
1306 if (msg
->msg_namelen
) {
1307 err
= (sk
->state
==TCP_ESTABLISHED
? -EISCONN
: -EOPNOTSUPP
);
1312 other
= unix_peer_get(sk
);
1317 if (sk
->shutdown
&SEND_SHUTDOWN
)
1323 * Optimisation for the fact that under 0.01% of X messages typically
1329 /* Keep two messages in the pipe so it schedules better */
1330 if (size
> sk
->sndbuf
/2 - 64)
1331 size
= sk
->sndbuf
/2 - 64;
1333 if (size
> SKB_MAX_ALLOC
)
1334 size
= SKB_MAX_ALLOC
;
1340 skb
=sock_alloc_send_skb(sk
,size
,msg
->msg_flags
&MSG_DONTWAIT
, &err
);
1346 * If you pass two values to the sock_alloc_send_skb
1347 * it tries to grab the large buffer with GFP_NOFS
1348 * (which can fail easily), and if it fails grab the
1349 * fallback size buffer which is under a page and will
1352 size
= min_t(int, size
, skb_tailroom(skb
));
1354 memcpy(UNIXCREDS(skb
), &scm
->creds
, sizeof(struct ucred
));
1356 unix_attach_fds(scm
, skb
);
1358 if ((err
= memcpy_fromiovec(skb_put(skb
,size
), msg
->msg_iov
, size
)) != 0) {
1363 unix_state_rlock(other
);
1365 if (other
->dead
|| (other
->shutdown
& RCV_SHUTDOWN
))
1368 skb_queue_tail(&other
->receive_queue
, skb
);
1369 unix_state_runlock(other
);
1370 other
->data_ready(other
, size
);
1377 unix_state_runlock(other
);
1380 if (sent
==0 && !(msg
->msg_flags
&MSG_NOSIGNAL
))
1381 send_sig(SIGPIPE
,current
,0);
1386 return sent
? : err
;
1389 static void unix_copy_addr(struct msghdr
*msg
, struct sock
*sk
)
1391 msg
->msg_namelen
= 0;
1392 if (sk
->protinfo
.af_unix
.addr
) {
1393 msg
->msg_namelen
=sk
->protinfo
.af_unix
.addr
->len
;
1394 memcpy(msg
->msg_name
,
1395 sk
->protinfo
.af_unix
.addr
->name
,
1396 sk
->protinfo
.af_unix
.addr
->len
);
1400 static int unix_dgram_recvmsg(struct socket
*sock
, struct msghdr
*msg
, int size
,
1401 int flags
, struct scm_cookie
*scm
)
1403 struct sock
*sk
= sock
->sk
;
1404 int noblock
= flags
& MSG_DONTWAIT
;
1405 struct sk_buff
*skb
;
1412 msg
->msg_namelen
= 0;
1414 down(&sk
->protinfo
.af_unix
.readsem
);
1416 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
1420 wake_up_interruptible(&sk
->protinfo
.af_unix
.peer_wait
);
1423 unix_copy_addr(msg
, skb
->sk
);
1425 if (size
> skb
->len
)
1427 else if (size
< skb
->len
)
1428 msg
->msg_flags
|= MSG_TRUNC
;
1430 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, size
);
1434 scm
->creds
= *UNIXCREDS(skb
);
1436 if (!(flags
& MSG_PEEK
))
1439 unix_detach_fds(scm
, skb
);
1443 /* It is questionable: on PEEK we could:
1444 - do not return fds - good, but too simple 8)
1445 - return fds, and do not return them on read (old strategy,
1447 - clone fds (I choosed it for now, it is the most universal
1450 POSIX 1003.1g does not actually define this clearly
1451 at all. POSIX 1003.1g doesn't define a lot of things
1456 scm
->fp
= scm_fp_dup(UNIXCB(skb
).fp
);
1461 skb_free_datagram(sk
,skb
);
1463 up(&sk
->protinfo
.af_unix
.readsem
);
1469 * Sleep until data has arrive. But check for races..
1472 static long unix_stream_data_wait(unix_socket
* sk
, long timeo
)
1474 DECLARE_WAITQUEUE(wait
, current
);
1476 unix_state_rlock(sk
);
1478 add_wait_queue(sk
->sleep
, &wait
);
1481 set_current_state(TASK_INTERRUPTIBLE
);
1483 if (skb_queue_len(&sk
->receive_queue
) ||
1485 (sk
->shutdown
& RCV_SHUTDOWN
) ||
1486 signal_pending(current
) ||
1490 set_bit(SOCK_ASYNC_WAITDATA
, &sk
->socket
->flags
);
1491 unix_state_runlock(sk
);
1492 timeo
= schedule_timeout(timeo
);
1493 unix_state_rlock(sk
);
1494 clear_bit(SOCK_ASYNC_WAITDATA
, &sk
->socket
->flags
);
1497 __set_current_state(TASK_RUNNING
);
1498 remove_wait_queue(sk
->sleep
, &wait
);
1499 unix_state_runlock(sk
);
1505 static int unix_stream_recvmsg(struct socket
*sock
, struct msghdr
*msg
, int size
,
1506 int flags
, struct scm_cookie
*scm
)
1508 struct sock
*sk
= sock
->sk
;
1509 struct sockaddr_un
*sunaddr
=msg
->msg_name
;
1511 int check_creds
= 0;
1517 if (sk
->state
!= TCP_ESTABLISHED
)
1524 target
= sock_rcvlowat(sk
, flags
&MSG_WAITALL
, size
);
1525 timeo
= sock_rcvtimeo(sk
, flags
&MSG_DONTWAIT
);
1527 msg
->msg_namelen
= 0;
1529 /* Lock the socket to prevent queue disordering
1530 * while sleeps in memcpy_tomsg
1533 down(&sk
->protinfo
.af_unix
.readsem
);
1538 struct sk_buff
*skb
;
1540 skb
=skb_dequeue(&sk
->receive_queue
);
1543 if (copied
>= target
)
1547 * POSIX 1003.1g mandates this order.
1550 if ((err
= sock_error(sk
)) != 0)
1552 if (sk
->shutdown
& RCV_SHUTDOWN
)
1557 up(&sk
->protinfo
.af_unix
.readsem
);
1559 timeo
= unix_stream_data_wait(sk
, timeo
);
1561 if (signal_pending(current
)) {
1562 err
= sock_intr_errno(timeo
);
1565 down(&sk
->protinfo
.af_unix
.readsem
);
1570 /* Never glue messages from different writers */
1571 if (memcmp(UNIXCREDS(skb
), &scm
->creds
, sizeof(scm
->creds
)) != 0) {
1572 skb_queue_head(&sk
->receive_queue
, skb
);
1576 /* Copy credentials */
1577 scm
->creds
= *UNIXCREDS(skb
);
1581 /* Copy address just once */
1584 unix_copy_addr(msg
, skb
->sk
);
1588 chunk
= min_t(unsigned int, skb
->len
, size
);
1589 if (memcpy_toiovec(msg
->msg_iov
, skb
->data
, chunk
)) {
1590 skb_queue_head(&sk
->receive_queue
, skb
);
1598 /* Mark read part of skb as used */
1599 if (!(flags
& MSG_PEEK
))
1601 skb_pull(skb
, chunk
);
1604 unix_detach_fds(scm
, skb
);
1606 /* put the skb back if we didn't use it up.. */
1609 skb_queue_head(&sk
->receive_queue
, skb
);
1620 /* It is questionable, see note in unix_dgram_recvmsg.
1623 scm
->fp
= scm_fp_dup(UNIXCB(skb
).fp
);
1625 /* put message back and return */
1626 skb_queue_head(&sk
->receive_queue
, skb
);
1631 up(&sk
->protinfo
.af_unix
.readsem
);
1633 return copied
? : err
;
1636 static int unix_shutdown(struct socket
*sock
, int mode
)
1638 struct sock
*sk
= sock
->sk
;
1641 mode
= (mode
+1)&(RCV_SHUTDOWN
|SEND_SHUTDOWN
);
1644 unix_state_wlock(sk
);
1645 sk
->shutdown
|= mode
;
1646 other
=unix_peer(sk
);
1649 unix_state_wunlock(sk
);
1650 sk
->state_change(sk
);
1652 if (other
&& sk
->type
== SOCK_STREAM
) {
1655 if (mode
&RCV_SHUTDOWN
)
1656 peer_mode
|= SEND_SHUTDOWN
;
1657 if (mode
&SEND_SHUTDOWN
)
1658 peer_mode
|= RCV_SHUTDOWN
;
1659 unix_state_wlock(other
);
1660 other
->shutdown
|= peer_mode
;
1661 unix_state_wunlock(other
);
1662 other
->state_change(other
);
1663 read_lock(&other
->callback_lock
);
1664 if (peer_mode
== SHUTDOWN_MASK
)
1665 sk_wake_async(other
,1,POLL_HUP
);
1666 else if (peer_mode
& RCV_SHUTDOWN
)
1667 sk_wake_async(other
,1,POLL_IN
);
1668 read_unlock(&other
->callback_lock
);
1676 static int unix_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
1678 struct sock
*sk
= sock
->sk
;
1685 amount
= atomic_read(&sk
->wmem_alloc
);
1686 err
= put_user(amount
, (int *)arg
);
1690 struct sk_buff
*skb
;
1691 if (sk
->state
==TCP_LISTEN
) {
1696 spin_lock(&sk
->receive_queue
.lock
);
1697 if (sk
->type
== SOCK_STREAM
) {
1698 skb_queue_walk(&sk
->receive_queue
, skb
)
1701 if((skb
=skb_peek(&sk
->receive_queue
))!=NULL
)
1704 spin_unlock(&sk
->receive_queue
.lock
);
1705 err
= put_user(amount
, (int *)arg
);
1710 err
= dev_ioctl(cmd
, (void *)arg
);
1716 static unsigned int unix_poll(struct file
* file
, struct socket
*sock
, poll_table
*wait
)
1718 struct sock
*sk
= sock
->sk
;
1721 poll_wait(file
, sk
->sleep
, wait
);
1724 /* exceptional events? */
1727 if (sk
->shutdown
== SHUTDOWN_MASK
)
1731 if (!skb_queue_empty(&sk
->receive_queue
) || (sk
->shutdown
&RCV_SHUTDOWN
))
1732 mask
|= POLLIN
| POLLRDNORM
;
1734 /* Connection-based need to check for termination and startup */
1735 if (sk
->type
== SOCK_STREAM
&& sk
->state
==TCP_CLOSE
)
1739 * we set writable also when the other side has shut down the
1740 * connection. This prevents stuck sockets.
1742 if (unix_writable(sk
))
1743 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
1749 #ifdef CONFIG_PROC_FS
1750 static int unix_read_proc(char *buffer
, char **start
, off_t offset
,
1751 int length
, int *eof
, void *data
)
1759 len
+= sprintf(buffer
,"Num RefCount Protocol Flags Type St "
1762 read_lock(&unix_table_lock
);
1763 forall_unix_sockets (i
,s
)
1765 unix_state_rlock(s
);
1767 len
+=sprintf(buffer
+len
,"%p: %08X %08X %08X %04X %02X %5lu",
1769 atomic_read(&s
->refcnt
),
1771 s
->state
== TCP_LISTEN
? __SO_ACCEPTCON
: 0,
1774 (s
->state
== TCP_ESTABLISHED
? SS_CONNECTED
: SS_UNCONNECTED
) :
1775 (s
->state
== TCP_ESTABLISHED
? SS_CONNECTING
: SS_DISCONNECTING
),
1778 if (s
->protinfo
.af_unix
.addr
)
1780 buffer
[len
++] = ' ';
1781 memcpy(buffer
+len
, s
->protinfo
.af_unix
.addr
->name
->sun_path
,
1782 s
->protinfo
.af_unix
.addr
->len
-sizeof(short));
1783 if (!UNIX_ABSTRACT(s
))
1787 len
+= s
->protinfo
.af_unix
.addr
->len
- sizeof(short);
1789 unix_state_runlock(s
);
1799 if(pos
>offset
+length
)
1804 read_unlock(&unix_table_lock
);
1805 *start
=buffer
+(offset
-begin
);
1806 len
-=(offset
-begin
);
1815 struct proto_ops unix_stream_ops
= {
1818 release
: unix_release
,
1820 connect
: unix_stream_connect
,
1821 socketpair
: unix_socketpair
,
1822 accept
: unix_accept
,
1823 getname
: unix_getname
,
1826 listen
: unix_listen
,
1827 shutdown
: unix_shutdown
,
1828 setsockopt
: sock_no_setsockopt
,
1829 getsockopt
: sock_no_getsockopt
,
1830 sendmsg
: unix_stream_sendmsg
,
1831 recvmsg
: unix_stream_recvmsg
,
1833 sendpage
: sock_no_sendpage
,
1836 struct proto_ops unix_dgram_ops
= {
1839 release
: unix_release
,
1841 connect
: unix_dgram_connect
,
1842 socketpair
: unix_socketpair
,
1843 accept
: sock_no_accept
,
1844 getname
: unix_getname
,
1845 poll
: datagram_poll
,
1847 listen
: sock_no_listen
,
1848 shutdown
: unix_shutdown
,
1849 setsockopt
: sock_no_setsockopt
,
1850 getsockopt
: sock_no_getsockopt
,
1851 sendmsg
: unix_dgram_sendmsg
,
1852 recvmsg
: unix_dgram_recvmsg
,
1854 sendpage
: sock_no_sendpage
,
1857 struct net_proto_family unix_family_ops
= {
1862 #ifdef CONFIG_SYSCTL
1863 extern void unix_sysctl_register(void);
1864 extern void unix_sysctl_unregister(void);
1866 static inline void unix_sysctl_register(void) {}
1867 static inline void unix_sysctl_unregister(void) {}
1870 static char banner
[] __initdata
= KERN_INFO
"NET4: Unix domain sockets 1.0/SMP for Linux NET4.0.\n";
1872 static int __init
af_unix_init(void)
1874 struct sk_buff
*dummy_skb
;
1877 if (sizeof(struct unix_skb_parms
) > sizeof(dummy_skb
->cb
))
1879 printk(KERN_CRIT
"unix_proto_init: panic\n");
1882 sock_register(&unix_family_ops
);
1883 #ifdef CONFIG_PROC_FS
1884 create_proc_read_entry("net/unix", 0, 0, unix_read_proc
, NULL
);
1886 unix_sysctl_register();
1890 static void __exit
af_unix_exit(void)
1892 sock_unregister(PF_UNIX
);
1893 unix_sysctl_unregister();
1894 remove_proc_entry("net/unix", 0);
1897 module_init(af_unix_init
);
1898 module_exit(af_unix_exit
);
1900 MODULE_LICENSE("GPL");