Linux 2.6.20.7
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / unix / af_unix.c
blob2f208c7f4d435aaef980a37795052609940f608d
1 /*
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.133 2002/02/08 03:57:19 davem Exp $
13 * Fixes:
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
23 * Mike Shaver's work.
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
30 * reference counting
31 * Kirk Petersen : Made this a module
32 * Christoph Rohland : Elegant non-blocking accept/connect algorithm.
33 * Lots of bug fixes.
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
45 * dgram receiver.
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.
50 * Arnaldo C. Melo : Remove MOD_{INC,DEC}_USE_COUNT,
51 * the core infrastructure is doing that
52 * for all net proto families now (2.5.69+)
55 * Known differences from reference BSD that was tested:
57 * [TO FIX]
58 * ECONNREFUSED is not returned from one end of a connected() socket to the
59 * other the moment one end closes.
60 * fstat() doesn't return st_dev=0, and give the blksize as high water mark
61 * and a fake inode identifier (nor the BSD first socket fstat twice bug).
62 * [NOT TO FIX]
63 * accept() returns a path name even if the connecting socket has closed
64 * in the meantime (BSD loses the path and gives up).
65 * accept() returns 0 length path for an unbound connector. BSD returns 16
66 * and a null first byte in the path (but not for gethost/peername - BSD bug ??)
67 * socketpair(...SOCK_RAW..) doesn't panic the kernel.
68 * BSD af_unix apparently has connect forgetting to block properly.
69 * (need to check this with the POSIX spec in detail)
71 * Differences from 2.0.0-11-... (ANK)
72 * Bug fixes and improvements.
73 * - client shutdown killed server socket.
74 * - removed all useless cli/sti pairs.
76 * Semantic changes/extensions.
77 * - generic control message passing.
78 * - SCM_CREDENTIALS control message.
79 * - "Abstract" (not FS based) socket bindings.
80 * Abstract names are sequences of bytes (not zero terminated)
81 * started by 0, so that this name space does not intersect
82 * with BSD names.
85 #include <linux/module.h>
86 #include <linux/kernel.h>
87 #include <linux/signal.h>
88 #include <linux/sched.h>
89 #include <linux/errno.h>
90 #include <linux/string.h>
91 #include <linux/stat.h>
92 #include <linux/dcache.h>
93 #include <linux/namei.h>
94 #include <linux/socket.h>
95 #include <linux/un.h>
96 #include <linux/fcntl.h>
97 #include <linux/termios.h>
98 #include <linux/sockios.h>
99 #include <linux/net.h>
100 #include <linux/in.h>
101 #include <linux/fs.h>
102 #include <linux/slab.h>
103 #include <asm/uaccess.h>
104 #include <linux/skbuff.h>
105 #include <linux/netdevice.h>
106 #include <net/sock.h>
107 #include <net/tcp_states.h>
108 #include <net/af_unix.h>
109 #include <linux/proc_fs.h>
110 #include <linux/seq_file.h>
111 #include <net/scm.h>
112 #include <linux/init.h>
113 #include <linux/poll.h>
114 #include <linux/smp_lock.h>
115 #include <linux/rtnetlink.h>
116 #include <linux/mount.h>
117 #include <net/checksum.h>
118 #include <linux/security.h>
120 int sysctl_unix_max_dgram_qlen __read_mostly = 10;
122 struct hlist_head unix_socket_table[UNIX_HASH_SIZE + 1];
123 DEFINE_SPINLOCK(unix_table_lock);
124 static atomic_t unix_nr_socks = ATOMIC_INIT(0);
126 #define unix_sockets_unbound (&unix_socket_table[UNIX_HASH_SIZE])
128 #define UNIX_ABSTRACT(sk) (unix_sk(sk)->addr->hash != UNIX_HASH_SIZE)
130 #ifdef CONFIG_SECURITY_NETWORK
131 static void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
133 memcpy(UNIXSID(skb), &scm->secid, sizeof(u32));
136 static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
138 scm->secid = *UNIXSID(skb);
140 #else
141 static inline void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
144 static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
146 #endif /* CONFIG_SECURITY_NETWORK */
149 * SMP locking strategy:
150 * hash table is protected with spinlock unix_table_lock
151 * each socket state is protected by separate rwlock.
154 static inline unsigned unix_hash_fold(__wsum n)
156 unsigned hash = (__force unsigned)n;
157 hash ^= hash>>16;
158 hash ^= hash>>8;
159 return hash&(UNIX_HASH_SIZE-1);
162 #define unix_peer(sk) (unix_sk(sk)->peer)
164 static inline int unix_our_peer(struct sock *sk, struct sock *osk)
166 return unix_peer(osk) == sk;
169 static inline int unix_may_send(struct sock *sk, struct sock *osk)
171 return (unix_peer(osk) == NULL || unix_our_peer(sk, osk));
174 static struct sock *unix_peer_get(struct sock *s)
176 struct sock *peer;
178 unix_state_rlock(s);
179 peer = unix_peer(s);
180 if (peer)
181 sock_hold(peer);
182 unix_state_runlock(s);
183 return peer;
186 static inline void unix_release_addr(struct unix_address *addr)
188 if (atomic_dec_and_test(&addr->refcnt))
189 kfree(addr);
193 * Check unix socket name:
194 * - should be not zero length.
195 * - if started by not zero, should be NULL terminated (FS object)
196 * - if started by zero, it is abstract name.
199 static int unix_mkname(struct sockaddr_un * sunaddr, int len, unsigned *hashp)
201 if (len <= sizeof(short) || len > sizeof(*sunaddr))
202 return -EINVAL;
203 if (!sunaddr || sunaddr->sun_family != AF_UNIX)
204 return -EINVAL;
205 if (sunaddr->sun_path[0]) {
207 * This may look like an off by one error but it is a bit more
208 * subtle. 108 is the longest valid AF_UNIX path for a binding.
209 * sun_path[108] doesnt as such exist. However in kernel space
210 * we are guaranteed that it is a valid memory location in our
211 * kernel address buffer.
213 ((char *)sunaddr)[len]=0;
214 len = strlen(sunaddr->sun_path)+1+sizeof(short);
215 return len;
218 *hashp = unix_hash_fold(csum_partial((char*)sunaddr, len, 0));
219 return len;
222 static void __unix_remove_socket(struct sock *sk)
224 sk_del_node_init(sk);
227 static void __unix_insert_socket(struct hlist_head *list, struct sock *sk)
229 BUG_TRAP(sk_unhashed(sk));
230 sk_add_node(sk, list);
233 static inline void unix_remove_socket(struct sock *sk)
235 spin_lock(&unix_table_lock);
236 __unix_remove_socket(sk);
237 spin_unlock(&unix_table_lock);
240 static inline void unix_insert_socket(struct hlist_head *list, struct sock *sk)
242 spin_lock(&unix_table_lock);
243 __unix_insert_socket(list, sk);
244 spin_unlock(&unix_table_lock);
247 static struct sock *__unix_find_socket_byname(struct sockaddr_un *sunname,
248 int len, int type, unsigned hash)
250 struct sock *s;
251 struct hlist_node *node;
253 sk_for_each(s, node, &unix_socket_table[hash ^ type]) {
254 struct unix_sock *u = unix_sk(s);
256 if (u->addr->len == len &&
257 !memcmp(u->addr->name, sunname, len))
258 goto found;
260 s = NULL;
261 found:
262 return s;
265 static inline struct sock *unix_find_socket_byname(struct sockaddr_un *sunname,
266 int len, int type,
267 unsigned hash)
269 struct sock *s;
271 spin_lock(&unix_table_lock);
272 s = __unix_find_socket_byname(sunname, len, type, hash);
273 if (s)
274 sock_hold(s);
275 spin_unlock(&unix_table_lock);
276 return s;
279 static struct sock *unix_find_socket_byinode(struct inode *i)
281 struct sock *s;
282 struct hlist_node *node;
284 spin_lock(&unix_table_lock);
285 sk_for_each(s, node,
286 &unix_socket_table[i->i_ino & (UNIX_HASH_SIZE - 1)]) {
287 struct dentry *dentry = unix_sk(s)->dentry;
289 if(dentry && dentry->d_inode == i)
291 sock_hold(s);
292 goto found;
295 s = NULL;
296 found:
297 spin_unlock(&unix_table_lock);
298 return s;
301 static inline int unix_writable(struct sock *sk)
303 return (atomic_read(&sk->sk_wmem_alloc) << 2) <= sk->sk_sndbuf;
306 static void unix_write_space(struct sock *sk)
308 read_lock(&sk->sk_callback_lock);
309 if (unix_writable(sk)) {
310 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
311 wake_up_interruptible(sk->sk_sleep);
312 sk_wake_async(sk, 2, POLL_OUT);
314 read_unlock(&sk->sk_callback_lock);
317 /* When dgram socket disconnects (or changes its peer), we clear its receive
318 * queue of packets arrived from previous peer. First, it allows to do
319 * flow control based only on wmem_alloc; second, sk connected to peer
320 * may receive messages only from that peer. */
321 static void unix_dgram_disconnected(struct sock *sk, struct sock *other)
323 if (!skb_queue_empty(&sk->sk_receive_queue)) {
324 skb_queue_purge(&sk->sk_receive_queue);
325 wake_up_interruptible_all(&unix_sk(sk)->peer_wait);
327 /* If one link of bidirectional dgram pipe is disconnected,
328 * we signal error. Messages are lost. Do not make this,
329 * when peer was not connected to us.
331 if (!sock_flag(other, SOCK_DEAD) && unix_peer(other) == sk) {
332 other->sk_err = ECONNRESET;
333 other->sk_error_report(other);
338 static void unix_sock_destructor(struct sock *sk)
340 struct unix_sock *u = unix_sk(sk);
342 skb_queue_purge(&sk->sk_receive_queue);
344 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
345 BUG_TRAP(sk_unhashed(sk));
346 BUG_TRAP(!sk->sk_socket);
347 if (!sock_flag(sk, SOCK_DEAD)) {
348 printk("Attempt to release alive unix socket: %p\n", sk);
349 return;
352 if (u->addr)
353 unix_release_addr(u->addr);
355 atomic_dec(&unix_nr_socks);
356 #ifdef UNIX_REFCNT_DEBUG
357 printk(KERN_DEBUG "UNIX %p is destroyed, %d are still alive.\n", sk, atomic_read(&unix_nr_socks));
358 #endif
361 static int unix_release_sock (struct sock *sk, int embrion)
363 struct unix_sock *u = unix_sk(sk);
364 struct dentry *dentry;
365 struct vfsmount *mnt;
366 struct sock *skpair;
367 struct sk_buff *skb;
368 int state;
370 unix_remove_socket(sk);
372 /* Clear state */
373 unix_state_wlock(sk);
374 sock_orphan(sk);
375 sk->sk_shutdown = SHUTDOWN_MASK;
376 dentry = u->dentry;
377 u->dentry = NULL;
378 mnt = u->mnt;
379 u->mnt = NULL;
380 state = sk->sk_state;
381 sk->sk_state = TCP_CLOSE;
382 unix_state_wunlock(sk);
384 wake_up_interruptible_all(&u->peer_wait);
386 skpair=unix_peer(sk);
388 if (skpair!=NULL) {
389 if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) {
390 unix_state_wlock(skpair);
391 /* No more writes */
392 skpair->sk_shutdown = SHUTDOWN_MASK;
393 if (!skb_queue_empty(&sk->sk_receive_queue) || embrion)
394 skpair->sk_err = ECONNRESET;
395 unix_state_wunlock(skpair);
396 skpair->sk_state_change(skpair);
397 read_lock(&skpair->sk_callback_lock);
398 sk_wake_async(skpair,1,POLL_HUP);
399 read_unlock(&skpair->sk_callback_lock);
401 sock_put(skpair); /* It may now die */
402 unix_peer(sk) = NULL;
405 /* Try to flush out this socket. Throw out buffers at least */
407 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
408 if (state==TCP_LISTEN)
409 unix_release_sock(skb->sk, 1);
410 /* passed fds are erased in the kfree_skb hook */
411 kfree_skb(skb);
414 if (dentry) {
415 dput(dentry);
416 mntput(mnt);
419 sock_put(sk);
421 /* ---- Socket is dead now and most probably destroyed ---- */
424 * Fixme: BSD difference: In BSD all sockets connected to use get
425 * ECONNRESET and we die on the spot. In Linux we behave
426 * like files and pipes do and wait for the last
427 * dereference.
429 * Can't we simply set sock->err?
431 * What the above comment does talk about? --ANK(980817)
434 if (atomic_read(&unix_tot_inflight))
435 unix_gc(); /* Garbage collect fds */
437 return 0;
440 static int unix_listen(struct socket *sock, int backlog)
442 int err;
443 struct sock *sk = sock->sk;
444 struct unix_sock *u = unix_sk(sk);
446 err = -EOPNOTSUPP;
447 if (sock->type!=SOCK_STREAM && sock->type!=SOCK_SEQPACKET)
448 goto out; /* Only stream/seqpacket sockets accept */
449 err = -EINVAL;
450 if (!u->addr)
451 goto out; /* No listens on an unbound socket */
452 unix_state_wlock(sk);
453 if (sk->sk_state != TCP_CLOSE && sk->sk_state != TCP_LISTEN)
454 goto out_unlock;
455 if (backlog > sk->sk_max_ack_backlog)
456 wake_up_interruptible_all(&u->peer_wait);
457 sk->sk_max_ack_backlog = backlog;
458 sk->sk_state = TCP_LISTEN;
459 /* set credentials so connect can copy them */
460 sk->sk_peercred.pid = current->tgid;
461 sk->sk_peercred.uid = current->euid;
462 sk->sk_peercred.gid = current->egid;
463 err = 0;
465 out_unlock:
466 unix_state_wunlock(sk);
467 out:
468 return err;
471 static int unix_release(struct socket *);
472 static int unix_bind(struct socket *, struct sockaddr *, int);
473 static int unix_stream_connect(struct socket *, struct sockaddr *,
474 int addr_len, int flags);
475 static int unix_socketpair(struct socket *, struct socket *);
476 static int unix_accept(struct socket *, struct socket *, int);
477 static int unix_getname(struct socket *, struct sockaddr *, int *, int);
478 static unsigned int unix_poll(struct file *, struct socket *, poll_table *);
479 static int unix_ioctl(struct socket *, unsigned int, unsigned long);
480 static int unix_shutdown(struct socket *, int);
481 static int unix_stream_sendmsg(struct kiocb *, struct socket *,
482 struct msghdr *, size_t);
483 static int unix_stream_recvmsg(struct kiocb *, struct socket *,
484 struct msghdr *, size_t, int);
485 static int unix_dgram_sendmsg(struct kiocb *, struct socket *,
486 struct msghdr *, size_t);
487 static int unix_dgram_recvmsg(struct kiocb *, struct socket *,
488 struct msghdr *, size_t, int);
489 static int unix_dgram_connect(struct socket *, struct sockaddr *,
490 int, int);
491 static int unix_seqpacket_sendmsg(struct kiocb *, struct socket *,
492 struct msghdr *, size_t);
494 static const struct proto_ops unix_stream_ops = {
495 .family = PF_UNIX,
496 .owner = THIS_MODULE,
497 .release = unix_release,
498 .bind = unix_bind,
499 .connect = unix_stream_connect,
500 .socketpair = unix_socketpair,
501 .accept = unix_accept,
502 .getname = unix_getname,
503 .poll = unix_poll,
504 .ioctl = unix_ioctl,
505 .listen = unix_listen,
506 .shutdown = unix_shutdown,
507 .setsockopt = sock_no_setsockopt,
508 .getsockopt = sock_no_getsockopt,
509 .sendmsg = unix_stream_sendmsg,
510 .recvmsg = unix_stream_recvmsg,
511 .mmap = sock_no_mmap,
512 .sendpage = sock_no_sendpage,
515 static const struct proto_ops unix_dgram_ops = {
516 .family = PF_UNIX,
517 .owner = THIS_MODULE,
518 .release = unix_release,
519 .bind = unix_bind,
520 .connect = unix_dgram_connect,
521 .socketpair = unix_socketpair,
522 .accept = sock_no_accept,
523 .getname = unix_getname,
524 .poll = datagram_poll,
525 .ioctl = unix_ioctl,
526 .listen = sock_no_listen,
527 .shutdown = unix_shutdown,
528 .setsockopt = sock_no_setsockopt,
529 .getsockopt = sock_no_getsockopt,
530 .sendmsg = unix_dgram_sendmsg,
531 .recvmsg = unix_dgram_recvmsg,
532 .mmap = sock_no_mmap,
533 .sendpage = sock_no_sendpage,
536 static const struct proto_ops unix_seqpacket_ops = {
537 .family = PF_UNIX,
538 .owner = THIS_MODULE,
539 .release = unix_release,
540 .bind = unix_bind,
541 .connect = unix_stream_connect,
542 .socketpair = unix_socketpair,
543 .accept = unix_accept,
544 .getname = unix_getname,
545 .poll = datagram_poll,
546 .ioctl = unix_ioctl,
547 .listen = unix_listen,
548 .shutdown = unix_shutdown,
549 .setsockopt = sock_no_setsockopt,
550 .getsockopt = sock_no_getsockopt,
551 .sendmsg = unix_seqpacket_sendmsg,
552 .recvmsg = unix_dgram_recvmsg,
553 .mmap = sock_no_mmap,
554 .sendpage = sock_no_sendpage,
557 static struct proto unix_proto = {
558 .name = "UNIX",
559 .owner = THIS_MODULE,
560 .obj_size = sizeof(struct unix_sock),
564 * AF_UNIX sockets do not interact with hardware, hence they
565 * dont trigger interrupts - so it's safe for them to have
566 * bh-unsafe locking for their sk_receive_queue.lock. Split off
567 * this special lock-class by reinitializing the spinlock key:
569 static struct lock_class_key af_unix_sk_receive_queue_lock_key;
571 static struct sock * unix_create1(struct socket *sock)
573 struct sock *sk = NULL;
574 struct unix_sock *u;
576 if (atomic_read(&unix_nr_socks) >= 2*get_max_files())
577 goto out;
579 sk = sk_alloc(PF_UNIX, GFP_KERNEL, &unix_proto, 1);
580 if (!sk)
581 goto out;
583 atomic_inc(&unix_nr_socks);
585 sock_init_data(sock,sk);
586 lockdep_set_class(&sk->sk_receive_queue.lock,
587 &af_unix_sk_receive_queue_lock_key);
589 sk->sk_write_space = unix_write_space;
590 sk->sk_max_ack_backlog = sysctl_unix_max_dgram_qlen;
591 sk->sk_destruct = unix_sock_destructor;
592 u = unix_sk(sk);
593 u->dentry = NULL;
594 u->mnt = NULL;
595 spin_lock_init(&u->lock);
596 atomic_set(&u->inflight, sock ? 0 : -1);
597 mutex_init(&u->readlock); /* single task reading lock */
598 init_waitqueue_head(&u->peer_wait);
599 unix_insert_socket(unix_sockets_unbound, sk);
600 out:
601 return sk;
604 static int unix_create(struct socket *sock, int protocol)
606 if (protocol && protocol != PF_UNIX)
607 return -EPROTONOSUPPORT;
609 sock->state = SS_UNCONNECTED;
611 switch (sock->type) {
612 case SOCK_STREAM:
613 sock->ops = &unix_stream_ops;
614 break;
616 * Believe it or not BSD has AF_UNIX, SOCK_RAW though
617 * nothing uses it.
619 case SOCK_RAW:
620 sock->type=SOCK_DGRAM;
621 case SOCK_DGRAM:
622 sock->ops = &unix_dgram_ops;
623 break;
624 case SOCK_SEQPACKET:
625 sock->ops = &unix_seqpacket_ops;
626 break;
627 default:
628 return -ESOCKTNOSUPPORT;
631 return unix_create1(sock) ? 0 : -ENOMEM;
634 static int unix_release(struct socket *sock)
636 struct sock *sk = sock->sk;
638 if (!sk)
639 return 0;
641 sock->sk = NULL;
643 return unix_release_sock (sk, 0);
646 static int unix_autobind(struct socket *sock)
648 struct sock *sk = sock->sk;
649 struct unix_sock *u = unix_sk(sk);
650 static u32 ordernum = 1;
651 struct unix_address * addr;
652 int err;
654 mutex_lock(&u->readlock);
656 err = 0;
657 if (u->addr)
658 goto out;
660 err = -ENOMEM;
661 addr = kzalloc(sizeof(*addr) + sizeof(short) + 16, GFP_KERNEL);
662 if (!addr)
663 goto out;
665 addr->name->sun_family = AF_UNIX;
666 atomic_set(&addr->refcnt, 1);
668 retry:
669 addr->len = sprintf(addr->name->sun_path+1, "%05x", ordernum) + 1 + sizeof(short);
670 addr->hash = unix_hash_fold(csum_partial((void*)addr->name, addr->len, 0));
672 spin_lock(&unix_table_lock);
673 ordernum = (ordernum+1)&0xFFFFF;
675 if (__unix_find_socket_byname(addr->name, addr->len, sock->type,
676 addr->hash)) {
677 spin_unlock(&unix_table_lock);
678 /* Sanity yield. It is unusual case, but yet... */
679 if (!(ordernum&0xFF))
680 yield();
681 goto retry;
683 addr->hash ^= sk->sk_type;
685 __unix_remove_socket(sk);
686 u->addr = addr;
687 __unix_insert_socket(&unix_socket_table[addr->hash], sk);
688 spin_unlock(&unix_table_lock);
689 err = 0;
691 out: mutex_unlock(&u->readlock);
692 return err;
695 static struct sock *unix_find_other(struct sockaddr_un *sunname, int len,
696 int type, unsigned hash, int *error)
698 struct sock *u;
699 struct nameidata nd;
700 int err = 0;
702 if (sunname->sun_path[0]) {
703 err = path_lookup(sunname->sun_path, LOOKUP_FOLLOW, &nd);
704 if (err)
705 goto fail;
706 err = vfs_permission(&nd, MAY_WRITE);
707 if (err)
708 goto put_fail;
710 err = -ECONNREFUSED;
711 if (!S_ISSOCK(nd.dentry->d_inode->i_mode))
712 goto put_fail;
713 u=unix_find_socket_byinode(nd.dentry->d_inode);
714 if (!u)
715 goto put_fail;
717 if (u->sk_type == type)
718 touch_atime(nd.mnt, nd.dentry);
720 path_release(&nd);
722 err=-EPROTOTYPE;
723 if (u->sk_type != type) {
724 sock_put(u);
725 goto fail;
727 } else {
728 err = -ECONNREFUSED;
729 u=unix_find_socket_byname(sunname, len, type, hash);
730 if (u) {
731 struct dentry *dentry;
732 dentry = unix_sk(u)->dentry;
733 if (dentry)
734 touch_atime(unix_sk(u)->mnt, dentry);
735 } else
736 goto fail;
738 return u;
740 put_fail:
741 path_release(&nd);
742 fail:
743 *error=err;
744 return NULL;
748 static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
750 struct sock *sk = sock->sk;
751 struct unix_sock *u = unix_sk(sk);
752 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
753 struct dentry * dentry = NULL;
754 struct nameidata nd;
755 int err;
756 unsigned hash;
757 struct unix_address *addr;
758 struct hlist_head *list;
760 err = -EINVAL;
761 if (sunaddr->sun_family != AF_UNIX)
762 goto out;
764 if (addr_len==sizeof(short)) {
765 err = unix_autobind(sock);
766 goto out;
769 err = unix_mkname(sunaddr, addr_len, &hash);
770 if (err < 0)
771 goto out;
772 addr_len = err;
774 mutex_lock(&u->readlock);
776 err = -EINVAL;
777 if (u->addr)
778 goto out_up;
780 err = -ENOMEM;
781 addr = kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);
782 if (!addr)
783 goto out_up;
785 memcpy(addr->name, sunaddr, addr_len);
786 addr->len = addr_len;
787 addr->hash = hash ^ sk->sk_type;
788 atomic_set(&addr->refcnt, 1);
790 if (sunaddr->sun_path[0]) {
791 unsigned int mode;
792 err = 0;
794 * Get the parent directory, calculate the hash for last
795 * component.
797 err = path_lookup(sunaddr->sun_path, LOOKUP_PARENT, &nd);
798 if (err)
799 goto out_mknod_parent;
801 dentry = lookup_create(&nd, 0);
802 err = PTR_ERR(dentry);
803 if (IS_ERR(dentry))
804 goto out_mknod_unlock;
807 * All right, let's create it.
809 mode = S_IFSOCK |
810 (SOCK_INODE(sock)->i_mode & ~current->fs->umask);
811 err = vfs_mknod(nd.dentry->d_inode, dentry, mode, 0);
812 if (err)
813 goto out_mknod_dput;
814 mutex_unlock(&nd.dentry->d_inode->i_mutex);
815 dput(nd.dentry);
816 nd.dentry = dentry;
818 addr->hash = UNIX_HASH_SIZE;
821 spin_lock(&unix_table_lock);
823 if (!sunaddr->sun_path[0]) {
824 err = -EADDRINUSE;
825 if (__unix_find_socket_byname(sunaddr, addr_len,
826 sk->sk_type, hash)) {
827 unix_release_addr(addr);
828 goto out_unlock;
831 list = &unix_socket_table[addr->hash];
832 } else {
833 list = &unix_socket_table[dentry->d_inode->i_ino & (UNIX_HASH_SIZE-1)];
834 u->dentry = nd.dentry;
835 u->mnt = nd.mnt;
838 err = 0;
839 __unix_remove_socket(sk);
840 u->addr = addr;
841 __unix_insert_socket(list, sk);
843 out_unlock:
844 spin_unlock(&unix_table_lock);
845 out_up:
846 mutex_unlock(&u->readlock);
847 out:
848 return err;
850 out_mknod_dput:
851 dput(dentry);
852 out_mknod_unlock:
853 mutex_unlock(&nd.dentry->d_inode->i_mutex);
854 path_release(&nd);
855 out_mknod_parent:
856 if (err==-EEXIST)
857 err=-EADDRINUSE;
858 unix_release_addr(addr);
859 goto out_up;
862 static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
863 int alen, int flags)
865 struct sock *sk = sock->sk;
866 struct sockaddr_un *sunaddr=(struct sockaddr_un*)addr;
867 struct sock *other;
868 unsigned hash;
869 int err;
871 if (addr->sa_family != AF_UNSPEC) {
872 err = unix_mkname(sunaddr, alen, &hash);
873 if (err < 0)
874 goto out;
875 alen = err;
877 if (test_bit(SOCK_PASSCRED, &sock->flags) &&
878 !unix_sk(sk)->addr && (err = unix_autobind(sock)) != 0)
879 goto out;
881 other=unix_find_other(sunaddr, alen, sock->type, hash, &err);
882 if (!other)
883 goto out;
885 unix_state_wlock(sk);
887 err = -EPERM;
888 if (!unix_may_send(sk, other))
889 goto out_unlock;
891 err = security_unix_may_send(sk->sk_socket, other->sk_socket);
892 if (err)
893 goto out_unlock;
895 } else {
897 * 1003.1g breaking connected state with AF_UNSPEC
899 other = NULL;
900 unix_state_wlock(sk);
904 * If it was connected, reconnect.
906 if (unix_peer(sk)) {
907 struct sock *old_peer = unix_peer(sk);
908 unix_peer(sk)=other;
909 unix_state_wunlock(sk);
911 if (other != old_peer)
912 unix_dgram_disconnected(sk, old_peer);
913 sock_put(old_peer);
914 } else {
915 unix_peer(sk)=other;
916 unix_state_wunlock(sk);
918 return 0;
920 out_unlock:
921 unix_state_wunlock(sk);
922 sock_put(other);
923 out:
924 return err;
927 static long unix_wait_for_peer(struct sock *other, long timeo)
929 struct unix_sock *u = unix_sk(other);
930 int sched;
931 DEFINE_WAIT(wait);
933 prepare_to_wait_exclusive(&u->peer_wait, &wait, TASK_INTERRUPTIBLE);
935 sched = !sock_flag(other, SOCK_DEAD) &&
936 !(other->sk_shutdown & RCV_SHUTDOWN) &&
937 (skb_queue_len(&other->sk_receive_queue) >
938 other->sk_max_ack_backlog);
940 unix_state_runlock(other);
942 if (sched)
943 timeo = schedule_timeout(timeo);
945 finish_wait(&u->peer_wait, &wait);
946 return timeo;
949 static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
950 int addr_len, int flags)
952 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
953 struct sock *sk = sock->sk;
954 struct unix_sock *u = unix_sk(sk), *newu, *otheru;
955 struct sock *newsk = NULL;
956 struct sock *other = NULL;
957 struct sk_buff *skb = NULL;
958 unsigned hash;
959 int st;
960 int err;
961 long timeo;
963 err = unix_mkname(sunaddr, addr_len, &hash);
964 if (err < 0)
965 goto out;
966 addr_len = err;
968 if (test_bit(SOCK_PASSCRED, &sock->flags)
969 && !u->addr && (err = unix_autobind(sock)) != 0)
970 goto out;
972 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
974 /* First of all allocate resources.
975 If we will make it after state is locked,
976 we will have to recheck all again in any case.
979 err = -ENOMEM;
981 /* create new sock for complete connection */
982 newsk = unix_create1(NULL);
983 if (newsk == NULL)
984 goto out;
986 /* Allocate skb for sending to listening sock */
987 skb = sock_wmalloc(newsk, 1, 0, GFP_KERNEL);
988 if (skb == NULL)
989 goto out;
991 restart:
992 /* Find listening sock. */
993 other = unix_find_other(sunaddr, addr_len, sk->sk_type, hash, &err);
994 if (!other)
995 goto out;
997 /* Latch state of peer */
998 unix_state_rlock(other);
1000 /* Apparently VFS overslept socket death. Retry. */
1001 if (sock_flag(other, SOCK_DEAD)) {
1002 unix_state_runlock(other);
1003 sock_put(other);
1004 goto restart;
1007 err = -ECONNREFUSED;
1008 if (other->sk_state != TCP_LISTEN)
1009 goto out_unlock;
1011 if (skb_queue_len(&other->sk_receive_queue) >
1012 other->sk_max_ack_backlog) {
1013 err = -EAGAIN;
1014 if (!timeo)
1015 goto out_unlock;
1017 timeo = unix_wait_for_peer(other, timeo);
1019 err = sock_intr_errno(timeo);
1020 if (signal_pending(current))
1021 goto out;
1022 sock_put(other);
1023 goto restart;
1026 /* Latch our state.
1028 It is tricky place. We need to grab write lock and cannot
1029 drop lock on peer. It is dangerous because deadlock is
1030 possible. Connect to self case and simultaneous
1031 attempt to connect are eliminated by checking socket
1032 state. other is TCP_LISTEN, if sk is TCP_LISTEN we
1033 check this before attempt to grab lock.
1035 Well, and we have to recheck the state after socket locked.
1037 st = sk->sk_state;
1039 switch (st) {
1040 case TCP_CLOSE:
1041 /* This is ok... continue with connect */
1042 break;
1043 case TCP_ESTABLISHED:
1044 /* Socket is already connected */
1045 err = -EISCONN;
1046 goto out_unlock;
1047 default:
1048 err = -EINVAL;
1049 goto out_unlock;
1052 unix_state_wlock_nested(sk);
1054 if (sk->sk_state != st) {
1055 unix_state_wunlock(sk);
1056 unix_state_runlock(other);
1057 sock_put(other);
1058 goto restart;
1061 err = security_unix_stream_connect(sock, other->sk_socket, newsk);
1062 if (err) {
1063 unix_state_wunlock(sk);
1064 goto out_unlock;
1067 /* The way is open! Fastly set all the necessary fields... */
1069 sock_hold(sk);
1070 unix_peer(newsk) = sk;
1071 newsk->sk_state = TCP_ESTABLISHED;
1072 newsk->sk_type = sk->sk_type;
1073 newsk->sk_peercred.pid = current->tgid;
1074 newsk->sk_peercred.uid = current->euid;
1075 newsk->sk_peercred.gid = current->egid;
1076 newu = unix_sk(newsk);
1077 newsk->sk_sleep = &newu->peer_wait;
1078 otheru = unix_sk(other);
1080 /* copy address information from listening to new sock*/
1081 if (otheru->addr) {
1082 atomic_inc(&otheru->addr->refcnt);
1083 newu->addr = otheru->addr;
1085 if (otheru->dentry) {
1086 newu->dentry = dget(otheru->dentry);
1087 newu->mnt = mntget(otheru->mnt);
1090 /* Set credentials */
1091 sk->sk_peercred = other->sk_peercred;
1093 sock->state = SS_CONNECTED;
1094 sk->sk_state = TCP_ESTABLISHED;
1095 sock_hold(newsk);
1097 smp_mb__after_atomic_inc(); /* sock_hold() does an atomic_inc() */
1098 unix_peer(sk) = newsk;
1100 unix_state_wunlock(sk);
1102 /* take ten and and send info to listening sock */
1103 spin_lock(&other->sk_receive_queue.lock);
1104 __skb_queue_tail(&other->sk_receive_queue, skb);
1105 /* Undo artificially decreased inflight after embrion
1106 * is installed to listening socket. */
1107 atomic_inc(&newu->inflight);
1108 spin_unlock(&other->sk_receive_queue.lock);
1109 unix_state_runlock(other);
1110 other->sk_data_ready(other, 0);
1111 sock_put(other);
1112 return 0;
1114 out_unlock:
1115 if (other)
1116 unix_state_runlock(other);
1118 out:
1119 if (skb)
1120 kfree_skb(skb);
1121 if (newsk)
1122 unix_release_sock(newsk, 0);
1123 if (other)
1124 sock_put(other);
1125 return err;
1128 static int unix_socketpair(struct socket *socka, struct socket *sockb)
1130 struct sock *ska=socka->sk, *skb = sockb->sk;
1132 /* Join our sockets back to back */
1133 sock_hold(ska);
1134 sock_hold(skb);
1135 unix_peer(ska)=skb;
1136 unix_peer(skb)=ska;
1137 ska->sk_peercred.pid = skb->sk_peercred.pid = current->tgid;
1138 ska->sk_peercred.uid = skb->sk_peercred.uid = current->euid;
1139 ska->sk_peercred.gid = skb->sk_peercred.gid = current->egid;
1141 if (ska->sk_type != SOCK_DGRAM) {
1142 ska->sk_state = TCP_ESTABLISHED;
1143 skb->sk_state = TCP_ESTABLISHED;
1144 socka->state = SS_CONNECTED;
1145 sockb->state = SS_CONNECTED;
1147 return 0;
1150 static int unix_accept(struct socket *sock, struct socket *newsock, int flags)
1152 struct sock *sk = sock->sk;
1153 struct sock *tsk;
1154 struct sk_buff *skb;
1155 int err;
1157 err = -EOPNOTSUPP;
1158 if (sock->type!=SOCK_STREAM && sock->type!=SOCK_SEQPACKET)
1159 goto out;
1161 err = -EINVAL;
1162 if (sk->sk_state != TCP_LISTEN)
1163 goto out;
1165 /* If socket state is TCP_LISTEN it cannot change (for now...),
1166 * so that no locks are necessary.
1169 skb = skb_recv_datagram(sk, 0, flags&O_NONBLOCK, &err);
1170 if (!skb) {
1171 /* This means receive shutdown. */
1172 if (err == 0)
1173 err = -EINVAL;
1174 goto out;
1177 tsk = skb->sk;
1178 skb_free_datagram(sk, skb);
1179 wake_up_interruptible(&unix_sk(sk)->peer_wait);
1181 /* attach accepted sock to socket */
1182 unix_state_wlock(tsk);
1183 newsock->state = SS_CONNECTED;
1184 sock_graft(tsk, newsock);
1185 unix_state_wunlock(tsk);
1186 return 0;
1188 out:
1189 return err;
1193 static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer)
1195 struct sock *sk = sock->sk;
1196 struct unix_sock *u;
1197 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
1198 int err = 0;
1200 if (peer) {
1201 sk = unix_peer_get(sk);
1203 err = -ENOTCONN;
1204 if (!sk)
1205 goto out;
1206 err = 0;
1207 } else {
1208 sock_hold(sk);
1211 u = unix_sk(sk);
1212 unix_state_rlock(sk);
1213 if (!u->addr) {
1214 sunaddr->sun_family = AF_UNIX;
1215 sunaddr->sun_path[0] = 0;
1216 *uaddr_len = sizeof(short);
1217 } else {
1218 struct unix_address *addr = u->addr;
1220 *uaddr_len = addr->len;
1221 memcpy(sunaddr, addr->name, *uaddr_len);
1223 unix_state_runlock(sk);
1224 sock_put(sk);
1225 out:
1226 return err;
1229 static void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
1231 int i;
1233 scm->fp = UNIXCB(skb).fp;
1234 skb->destructor = sock_wfree;
1235 UNIXCB(skb).fp = NULL;
1237 for (i=scm->fp->count-1; i>=0; i--)
1238 unix_notinflight(scm->fp->fp[i]);
1241 static void unix_destruct_fds(struct sk_buff *skb)
1243 struct scm_cookie scm;
1244 memset(&scm, 0, sizeof(scm));
1245 unix_detach_fds(&scm, skb);
1247 /* Alas, it calls VFS */
1248 /* So fscking what? fput() had been SMP-safe since the last Summer */
1249 scm_destroy(&scm);
1250 sock_wfree(skb);
1253 static void unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
1255 int i;
1256 for (i=scm->fp->count-1; i>=0; i--)
1257 unix_inflight(scm->fp->fp[i]);
1258 UNIXCB(skb).fp = scm->fp;
1259 skb->destructor = unix_destruct_fds;
1260 scm->fp = NULL;
1264 * Send AF_UNIX data.
1267 static int unix_dgram_sendmsg(struct kiocb *kiocb, struct socket *sock,
1268 struct msghdr *msg, size_t len)
1270 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1271 struct sock *sk = sock->sk;
1272 struct unix_sock *u = unix_sk(sk);
1273 struct sockaddr_un *sunaddr=msg->msg_name;
1274 struct sock *other = NULL;
1275 int namelen = 0; /* fake GCC */
1276 int err;
1277 unsigned hash;
1278 struct sk_buff *skb;
1279 long timeo;
1280 struct scm_cookie tmp_scm;
1282 if (NULL == siocb->scm)
1283 siocb->scm = &tmp_scm;
1284 err = scm_send(sock, msg, siocb->scm);
1285 if (err < 0)
1286 return err;
1288 err = -EOPNOTSUPP;
1289 if (msg->msg_flags&MSG_OOB)
1290 goto out;
1292 if (msg->msg_namelen) {
1293 err = unix_mkname(sunaddr, msg->msg_namelen, &hash);
1294 if (err < 0)
1295 goto out;
1296 namelen = err;
1297 } else {
1298 sunaddr = NULL;
1299 err = -ENOTCONN;
1300 other = unix_peer_get(sk);
1301 if (!other)
1302 goto out;
1305 if (test_bit(SOCK_PASSCRED, &sock->flags)
1306 && !u->addr && (err = unix_autobind(sock)) != 0)
1307 goto out;
1309 err = -EMSGSIZE;
1310 if (len > sk->sk_sndbuf - 32)
1311 goto out;
1313 skb = sock_alloc_send_skb(sk, len, msg->msg_flags&MSG_DONTWAIT, &err);
1314 if (skb==NULL)
1315 goto out;
1317 memcpy(UNIXCREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
1318 if (siocb->scm->fp)
1319 unix_attach_fds(siocb->scm, skb);
1320 unix_get_secdata(siocb->scm, skb);
1322 skb->h.raw = skb->data;
1323 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
1324 if (err)
1325 goto out_free;
1327 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1329 restart:
1330 if (!other) {
1331 err = -ECONNRESET;
1332 if (sunaddr == NULL)
1333 goto out_free;
1335 other = unix_find_other(sunaddr, namelen, sk->sk_type,
1336 hash, &err);
1337 if (other==NULL)
1338 goto out_free;
1341 unix_state_rlock(other);
1342 err = -EPERM;
1343 if (!unix_may_send(sk, other))
1344 goto out_unlock;
1346 if (sock_flag(other, SOCK_DEAD)) {
1348 * Check with 1003.1g - what should
1349 * datagram error
1351 unix_state_runlock(other);
1352 sock_put(other);
1354 err = 0;
1355 unix_state_wlock(sk);
1356 if (unix_peer(sk) == other) {
1357 unix_peer(sk)=NULL;
1358 unix_state_wunlock(sk);
1360 unix_dgram_disconnected(sk, other);
1361 sock_put(other);
1362 err = -ECONNREFUSED;
1363 } else {
1364 unix_state_wunlock(sk);
1367 other = NULL;
1368 if (err)
1369 goto out_free;
1370 goto restart;
1373 err = -EPIPE;
1374 if (other->sk_shutdown & RCV_SHUTDOWN)
1375 goto out_unlock;
1377 if (sk->sk_type != SOCK_SEQPACKET) {
1378 err = security_unix_may_send(sk->sk_socket, other->sk_socket);
1379 if (err)
1380 goto out_unlock;
1383 if (unix_peer(other) != sk &&
1384 (skb_queue_len(&other->sk_receive_queue) >
1385 other->sk_max_ack_backlog)) {
1386 if (!timeo) {
1387 err = -EAGAIN;
1388 goto out_unlock;
1391 timeo = unix_wait_for_peer(other, timeo);
1393 err = sock_intr_errno(timeo);
1394 if (signal_pending(current))
1395 goto out_free;
1397 goto restart;
1400 skb_queue_tail(&other->sk_receive_queue, skb);
1401 unix_state_runlock(other);
1402 other->sk_data_ready(other, len);
1403 sock_put(other);
1404 scm_destroy(siocb->scm);
1405 return len;
1407 out_unlock:
1408 unix_state_runlock(other);
1409 out_free:
1410 kfree_skb(skb);
1411 out:
1412 if (other)
1413 sock_put(other);
1414 scm_destroy(siocb->scm);
1415 return err;
1419 static int unix_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
1420 struct msghdr *msg, size_t len)
1422 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1423 struct sock *sk = sock->sk;
1424 struct sock *other = NULL;
1425 struct sockaddr_un *sunaddr=msg->msg_name;
1426 int err,size;
1427 struct sk_buff *skb;
1428 int sent=0;
1429 struct scm_cookie tmp_scm;
1431 if (NULL == siocb->scm)
1432 siocb->scm = &tmp_scm;
1433 err = scm_send(sock, msg, siocb->scm);
1434 if (err < 0)
1435 return err;
1437 err = -EOPNOTSUPP;
1438 if (msg->msg_flags&MSG_OOB)
1439 goto out_err;
1441 if (msg->msg_namelen) {
1442 err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP;
1443 goto out_err;
1444 } else {
1445 sunaddr = NULL;
1446 err = -ENOTCONN;
1447 other = unix_peer(sk);
1448 if (!other)
1449 goto out_err;
1452 if (sk->sk_shutdown & SEND_SHUTDOWN)
1453 goto pipe_err;
1455 while(sent < len)
1458 * Optimisation for the fact that under 0.01% of X
1459 * messages typically need breaking up.
1462 size = len-sent;
1464 /* Keep two messages in the pipe so it schedules better */
1465 if (size > ((sk->sk_sndbuf >> 1) - 64))
1466 size = (sk->sk_sndbuf >> 1) - 64;
1468 if (size > SKB_MAX_ALLOC)
1469 size = SKB_MAX_ALLOC;
1472 * Grab a buffer
1475 skb=sock_alloc_send_skb(sk,size,msg->msg_flags&MSG_DONTWAIT, &err);
1477 if (skb==NULL)
1478 goto out_err;
1481 * If you pass two values to the sock_alloc_send_skb
1482 * it tries to grab the large buffer with GFP_NOFS
1483 * (which can fail easily), and if it fails grab the
1484 * fallback size buffer which is under a page and will
1485 * succeed. [Alan]
1487 size = min_t(int, size, skb_tailroom(skb));
1489 memcpy(UNIXCREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
1490 if (siocb->scm->fp)
1491 unix_attach_fds(siocb->scm, skb);
1493 if ((err = memcpy_fromiovec(skb_put(skb,size), msg->msg_iov, size)) != 0) {
1494 kfree_skb(skb);
1495 goto out_err;
1498 unix_state_rlock(other);
1500 if (sock_flag(other, SOCK_DEAD) ||
1501 (other->sk_shutdown & RCV_SHUTDOWN))
1502 goto pipe_err_free;
1504 skb_queue_tail(&other->sk_receive_queue, skb);
1505 unix_state_runlock(other);
1506 other->sk_data_ready(other, size);
1507 sent+=size;
1510 scm_destroy(siocb->scm);
1511 siocb->scm = NULL;
1513 return sent;
1515 pipe_err_free:
1516 unix_state_runlock(other);
1517 kfree_skb(skb);
1518 pipe_err:
1519 if (sent==0 && !(msg->msg_flags&MSG_NOSIGNAL))
1520 send_sig(SIGPIPE,current,0);
1521 err = -EPIPE;
1522 out_err:
1523 scm_destroy(siocb->scm);
1524 siocb->scm = NULL;
1525 return sent ? : err;
1528 static int unix_seqpacket_sendmsg(struct kiocb *kiocb, struct socket *sock,
1529 struct msghdr *msg, size_t len)
1531 int err;
1532 struct sock *sk = sock->sk;
1534 err = sock_error(sk);
1535 if (err)
1536 return err;
1538 if (sk->sk_state != TCP_ESTABLISHED)
1539 return -ENOTCONN;
1541 if (msg->msg_namelen)
1542 msg->msg_namelen = 0;
1544 return unix_dgram_sendmsg(kiocb, sock, msg, len);
1547 static void unix_copy_addr(struct msghdr *msg, struct sock *sk)
1549 struct unix_sock *u = unix_sk(sk);
1551 msg->msg_namelen = 0;
1552 if (u->addr) {
1553 msg->msg_namelen = u->addr->len;
1554 memcpy(msg->msg_name, u->addr->name, u->addr->len);
1558 static int unix_dgram_recvmsg(struct kiocb *iocb, struct socket *sock,
1559 struct msghdr *msg, size_t size,
1560 int flags)
1562 struct sock_iocb *siocb = kiocb_to_siocb(iocb);
1563 struct scm_cookie tmp_scm;
1564 struct sock *sk = sock->sk;
1565 struct unix_sock *u = unix_sk(sk);
1566 int noblock = flags & MSG_DONTWAIT;
1567 struct sk_buff *skb;
1568 int err;
1570 err = -EOPNOTSUPP;
1571 if (flags&MSG_OOB)
1572 goto out;
1574 msg->msg_namelen = 0;
1576 mutex_lock(&u->readlock);
1578 skb = skb_recv_datagram(sk, flags, noblock, &err);
1579 if (!skb)
1580 goto out_unlock;
1582 wake_up_interruptible(&u->peer_wait);
1584 if (msg->msg_name)
1585 unix_copy_addr(msg, skb->sk);
1587 if (size > skb->len)
1588 size = skb->len;
1589 else if (size < skb->len)
1590 msg->msg_flags |= MSG_TRUNC;
1592 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, size);
1593 if (err)
1594 goto out_free;
1596 if (!siocb->scm) {
1597 siocb->scm = &tmp_scm;
1598 memset(&tmp_scm, 0, sizeof(tmp_scm));
1600 siocb->scm->creds = *UNIXCREDS(skb);
1601 unix_set_secdata(siocb->scm, skb);
1603 if (!(flags & MSG_PEEK))
1605 if (UNIXCB(skb).fp)
1606 unix_detach_fds(siocb->scm, skb);
1608 else
1610 /* It is questionable: on PEEK we could:
1611 - do not return fds - good, but too simple 8)
1612 - return fds, and do not return them on read (old strategy,
1613 apparently wrong)
1614 - clone fds (I chose it for now, it is the most universal
1615 solution)
1617 POSIX 1003.1g does not actually define this clearly
1618 at all. POSIX 1003.1g doesn't define a lot of things
1619 clearly however!
1622 if (UNIXCB(skb).fp)
1623 siocb->scm->fp = scm_fp_dup(UNIXCB(skb).fp);
1625 err = size;
1627 scm_recv(sock, msg, siocb->scm, flags);
1629 out_free:
1630 skb_free_datagram(sk,skb);
1631 out_unlock:
1632 mutex_unlock(&u->readlock);
1633 out:
1634 return err;
1638 * Sleep until data has arrive. But check for races..
1641 static long unix_stream_data_wait(struct sock * sk, long timeo)
1643 DEFINE_WAIT(wait);
1645 unix_state_rlock(sk);
1647 for (;;) {
1648 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
1650 if (!skb_queue_empty(&sk->sk_receive_queue) ||
1651 sk->sk_err ||
1652 (sk->sk_shutdown & RCV_SHUTDOWN) ||
1653 signal_pending(current) ||
1654 !timeo)
1655 break;
1657 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
1658 unix_state_runlock(sk);
1659 timeo = schedule_timeout(timeo);
1660 unix_state_rlock(sk);
1661 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
1664 finish_wait(sk->sk_sleep, &wait);
1665 unix_state_runlock(sk);
1666 return timeo;
1671 static int unix_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
1672 struct msghdr *msg, size_t size,
1673 int flags)
1675 struct sock_iocb *siocb = kiocb_to_siocb(iocb);
1676 struct scm_cookie tmp_scm;
1677 struct sock *sk = sock->sk;
1678 struct unix_sock *u = unix_sk(sk);
1679 struct sockaddr_un *sunaddr=msg->msg_name;
1680 int copied = 0;
1681 int check_creds = 0;
1682 int target;
1683 int err = 0;
1684 long timeo;
1686 err = -EINVAL;
1687 if (sk->sk_state != TCP_ESTABLISHED)
1688 goto out;
1690 err = -EOPNOTSUPP;
1691 if (flags&MSG_OOB)
1692 goto out;
1694 target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
1695 timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
1697 msg->msg_namelen = 0;
1699 /* Lock the socket to prevent queue disordering
1700 * while sleeps in memcpy_tomsg
1703 if (!siocb->scm) {
1704 siocb->scm = &tmp_scm;
1705 memset(&tmp_scm, 0, sizeof(tmp_scm));
1708 mutex_lock(&u->readlock);
1712 int chunk;
1713 struct sk_buff *skb;
1715 skb = skb_dequeue(&sk->sk_receive_queue);
1716 if (skb==NULL)
1718 if (copied >= target)
1719 break;
1722 * POSIX 1003.1g mandates this order.
1725 if ((err = sock_error(sk)) != 0)
1726 break;
1727 if (sk->sk_shutdown & RCV_SHUTDOWN)
1728 break;
1729 err = -EAGAIN;
1730 if (!timeo)
1731 break;
1732 mutex_unlock(&u->readlock);
1734 timeo = unix_stream_data_wait(sk, timeo);
1736 if (signal_pending(current)) {
1737 err = sock_intr_errno(timeo);
1738 goto out;
1740 mutex_lock(&u->readlock);
1741 continue;
1744 if (check_creds) {
1745 /* Never glue messages from different writers */
1746 if (memcmp(UNIXCREDS(skb), &siocb->scm->creds, sizeof(siocb->scm->creds)) != 0) {
1747 skb_queue_head(&sk->sk_receive_queue, skb);
1748 break;
1750 } else {
1751 /* Copy credentials */
1752 siocb->scm->creds = *UNIXCREDS(skb);
1753 check_creds = 1;
1756 /* Copy address just once */
1757 if (sunaddr)
1759 unix_copy_addr(msg, skb->sk);
1760 sunaddr = NULL;
1763 chunk = min_t(unsigned int, skb->len, size);
1764 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
1765 skb_queue_head(&sk->sk_receive_queue, skb);
1766 if (copied == 0)
1767 copied = -EFAULT;
1768 break;
1770 copied += chunk;
1771 size -= chunk;
1773 /* Mark read part of skb as used */
1774 if (!(flags & MSG_PEEK))
1776 skb_pull(skb, chunk);
1778 if (UNIXCB(skb).fp)
1779 unix_detach_fds(siocb->scm, skb);
1781 /* put the skb back if we didn't use it up.. */
1782 if (skb->len)
1784 skb_queue_head(&sk->sk_receive_queue, skb);
1785 break;
1788 kfree_skb(skb);
1790 if (siocb->scm->fp)
1791 break;
1793 else
1795 /* It is questionable, see note in unix_dgram_recvmsg.
1797 if (UNIXCB(skb).fp)
1798 siocb->scm->fp = scm_fp_dup(UNIXCB(skb).fp);
1800 /* put message back and return */
1801 skb_queue_head(&sk->sk_receive_queue, skb);
1802 break;
1804 } while (size);
1806 mutex_unlock(&u->readlock);
1807 scm_recv(sock, msg, siocb->scm, flags);
1808 out:
1809 return copied ? : err;
1812 static int unix_shutdown(struct socket *sock, int mode)
1814 struct sock *sk = sock->sk;
1815 struct sock *other;
1817 mode = (mode+1)&(RCV_SHUTDOWN|SEND_SHUTDOWN);
1819 if (mode) {
1820 unix_state_wlock(sk);
1821 sk->sk_shutdown |= mode;
1822 other=unix_peer(sk);
1823 if (other)
1824 sock_hold(other);
1825 unix_state_wunlock(sk);
1826 sk->sk_state_change(sk);
1828 if (other &&
1829 (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET)) {
1831 int peer_mode = 0;
1833 if (mode&RCV_SHUTDOWN)
1834 peer_mode |= SEND_SHUTDOWN;
1835 if (mode&SEND_SHUTDOWN)
1836 peer_mode |= RCV_SHUTDOWN;
1837 unix_state_wlock(other);
1838 other->sk_shutdown |= peer_mode;
1839 unix_state_wunlock(other);
1840 other->sk_state_change(other);
1841 read_lock(&other->sk_callback_lock);
1842 if (peer_mode == SHUTDOWN_MASK)
1843 sk_wake_async(other,1,POLL_HUP);
1844 else if (peer_mode & RCV_SHUTDOWN)
1845 sk_wake_async(other,1,POLL_IN);
1846 read_unlock(&other->sk_callback_lock);
1848 if (other)
1849 sock_put(other);
1851 return 0;
1854 static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1856 struct sock *sk = sock->sk;
1857 long amount=0;
1858 int err;
1860 switch(cmd)
1862 case SIOCOUTQ:
1863 amount = atomic_read(&sk->sk_wmem_alloc);
1864 err = put_user(amount, (int __user *)arg);
1865 break;
1866 case SIOCINQ:
1868 struct sk_buff *skb;
1870 if (sk->sk_state == TCP_LISTEN) {
1871 err = -EINVAL;
1872 break;
1875 spin_lock(&sk->sk_receive_queue.lock);
1876 if (sk->sk_type == SOCK_STREAM ||
1877 sk->sk_type == SOCK_SEQPACKET) {
1878 skb_queue_walk(&sk->sk_receive_queue, skb)
1879 amount += skb->len;
1880 } else {
1881 skb = skb_peek(&sk->sk_receive_queue);
1882 if (skb)
1883 amount=skb->len;
1885 spin_unlock(&sk->sk_receive_queue.lock);
1886 err = put_user(amount, (int __user *)arg);
1887 break;
1890 default:
1891 err = -ENOIOCTLCMD;
1892 break;
1894 return err;
1897 static unsigned int unix_poll(struct file * file, struct socket *sock, poll_table *wait)
1899 struct sock *sk = sock->sk;
1900 unsigned int mask;
1902 poll_wait(file, sk->sk_sleep, wait);
1903 mask = 0;
1905 /* exceptional events? */
1906 if (sk->sk_err)
1907 mask |= POLLERR;
1908 if (sk->sk_shutdown == SHUTDOWN_MASK)
1909 mask |= POLLHUP;
1910 if (sk->sk_shutdown & RCV_SHUTDOWN)
1911 mask |= POLLRDHUP;
1913 /* readable? */
1914 if (!skb_queue_empty(&sk->sk_receive_queue) ||
1915 (sk->sk_shutdown & RCV_SHUTDOWN))
1916 mask |= POLLIN | POLLRDNORM;
1918 /* Connection-based need to check for termination and startup */
1919 if ((sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) && sk->sk_state == TCP_CLOSE)
1920 mask |= POLLHUP;
1923 * we set writable also when the other side has shut down the
1924 * connection. This prevents stuck sockets.
1926 if (unix_writable(sk))
1927 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1929 return mask;
1933 #ifdef CONFIG_PROC_FS
1934 static struct sock *unix_seq_idx(int *iter, loff_t pos)
1936 loff_t off = 0;
1937 struct sock *s;
1939 for (s = first_unix_socket(iter); s; s = next_unix_socket(iter, s)) {
1940 if (off == pos)
1941 return s;
1942 ++off;
1944 return NULL;
1948 static void *unix_seq_start(struct seq_file *seq, loff_t *pos)
1950 spin_lock(&unix_table_lock);
1951 return *pos ? unix_seq_idx(seq->private, *pos - 1) : ((void *) 1);
1954 static void *unix_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1956 ++*pos;
1958 if (v == (void *)1)
1959 return first_unix_socket(seq->private);
1960 return next_unix_socket(seq->private, v);
1963 static void unix_seq_stop(struct seq_file *seq, void *v)
1965 spin_unlock(&unix_table_lock);
1968 static int unix_seq_show(struct seq_file *seq, void *v)
1971 if (v == (void *)1)
1972 seq_puts(seq, "Num RefCount Protocol Flags Type St "
1973 "Inode Path\n");
1974 else {
1975 struct sock *s = v;
1976 struct unix_sock *u = unix_sk(s);
1977 unix_state_rlock(s);
1979 seq_printf(seq, "%p: %08X %08X %08X %04X %02X %5lu",
1981 atomic_read(&s->sk_refcnt),
1983 s->sk_state == TCP_LISTEN ? __SO_ACCEPTCON : 0,
1984 s->sk_type,
1985 s->sk_socket ?
1986 (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTED : SS_UNCONNECTED) :
1987 (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTING : SS_DISCONNECTING),
1988 sock_i_ino(s));
1990 if (u->addr) {
1991 int i, len;
1992 seq_putc(seq, ' ');
1994 i = 0;
1995 len = u->addr->len - sizeof(short);
1996 if (!UNIX_ABSTRACT(s))
1997 len--;
1998 else {
1999 seq_putc(seq, '@');
2000 i++;
2002 for ( ; i < len; i++)
2003 seq_putc(seq, u->addr->name->sun_path[i]);
2005 unix_state_runlock(s);
2006 seq_putc(seq, '\n');
2009 return 0;
2012 static struct seq_operations unix_seq_ops = {
2013 .start = unix_seq_start,
2014 .next = unix_seq_next,
2015 .stop = unix_seq_stop,
2016 .show = unix_seq_show,
2020 static int unix_seq_open(struct inode *inode, struct file *file)
2022 struct seq_file *seq;
2023 int rc = -ENOMEM;
2024 int *iter = kmalloc(sizeof(int), GFP_KERNEL);
2026 if (!iter)
2027 goto out;
2029 rc = seq_open(file, &unix_seq_ops);
2030 if (rc)
2031 goto out_kfree;
2033 seq = file->private_data;
2034 seq->private = iter;
2035 *iter = 0;
2036 out:
2037 return rc;
2038 out_kfree:
2039 kfree(iter);
2040 goto out;
2043 static struct file_operations unix_seq_fops = {
2044 .owner = THIS_MODULE,
2045 .open = unix_seq_open,
2046 .read = seq_read,
2047 .llseek = seq_lseek,
2048 .release = seq_release_private,
2051 #endif
2053 static struct net_proto_family unix_family_ops = {
2054 .family = PF_UNIX,
2055 .create = unix_create,
2056 .owner = THIS_MODULE,
2059 static int __init af_unix_init(void)
2061 int rc = -1;
2062 struct sk_buff *dummy_skb;
2064 BUILD_BUG_ON(sizeof(struct unix_skb_parms) > sizeof(dummy_skb->cb));
2066 rc = proto_register(&unix_proto, 1);
2067 if (rc != 0) {
2068 printk(KERN_CRIT "%s: Cannot create unix_sock SLAB cache!\n",
2069 __FUNCTION__);
2070 goto out;
2073 sock_register(&unix_family_ops);
2074 #ifdef CONFIG_PROC_FS
2075 proc_net_fops_create("unix", 0, &unix_seq_fops);
2076 #endif
2077 unix_sysctl_register();
2078 out:
2079 return rc;
2082 static void __exit af_unix_exit(void)
2084 sock_unregister(PF_UNIX);
2085 unix_sysctl_unregister();
2086 proc_net_remove("unix");
2087 proto_unregister(&unix_proto);
2090 module_init(af_unix_init);
2091 module_exit(af_unix_exit);
2093 MODULE_LICENSE("GPL");
2094 MODULE_ALIAS_NETPROTO(PF_UNIX);