blob: e1b9358a211d65354d84c641070b5ef697bce0ff [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NET4: Implementation of BSD Unix domain sockets.
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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.
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Fixes:
12 * Linus Torvalds : Assorted bug cures.
13 * Niibe Yutaka : async I/O support.
14 * Carsten Paeth : PF_UNIX check, address fixes.
15 * Alan Cox : Limit size of allocated blocks.
16 * Alan Cox : Fixed the stupid socketpair bug.
17 * Alan Cox : BSD compatibility fine tuning.
18 * Alan Cox : Fixed a bug in connect when interrupted.
19 * Alan Cox : Sorted out a proper draft version of
20 * file descriptor passing hacked up from
21 * Mike Shaver's work.
22 * Marty Leisner : Fixes to fd passing
23 * Nick Nevin : recvmsg bugfix.
24 * Alan Cox : Started proper garbage collector
25 * Heiko EiBfeldt : Missing verify_area check
26 * Alan Cox : Started POSIXisms
27 * Andreas Schwab : Replace inode by dentry for proper
28 * reference counting
29 * Kirk Petersen : Made this a module
30 * Christoph Rohland : Elegant non-blocking accept/connect algorithm.
31 * Lots of bug fixes.
32 * Alexey Kuznetosv : Repaired (I hope) bugs introduces
33 * by above two patches.
34 * Andrea Arcangeli : If possible we block in connect(2)
35 * if the max backlog of the listen socket
36 * is been reached. This won't break
37 * old apps and it will avoid huge amount
38 * of socks hashed (this for unix_gc()
39 * performances reasons).
40 * Security fix that limits the max
41 * number of socks to 2*max_files and
42 * the number of skb queueable in the
43 * dgram receiver.
44 * Artur Skawina : Hash function optimizations
45 * Alexey Kuznetsov : Full scale SMP. Lot of bugs are introduced 8)
46 * Malcolm Beattie : Set peercred for socketpair
47 * Michal Ostrowski : Module initialization cleanup.
48 * Arnaldo C. Melo : Remove MOD_{INC,DEC}_USE_COUNT,
49 * the core infrastructure is doing that
50 * for all net proto families now (2.5.69+)
51 *
52 *
53 * Known differences from reference BSD that was tested:
54 *
55 * [TO FIX]
56 * ECONNREFUSED is not returned from one end of a connected() socket to the
57 * other the moment one end closes.
58 * fstat() doesn't return st_dev=0, and give the blksize as high water mark
59 * and a fake inode identifier (nor the BSD first socket fstat twice bug).
60 * [NOT TO FIX]
61 * accept() returns a path name even if the connecting socket has closed
62 * in the meantime (BSD loses the path and gives up).
63 * accept() returns 0 length path for an unbound connector. BSD returns 16
64 * and a null first byte in the path (but not for gethost/peername - BSD bug ??)
65 * socketpair(...SOCK_RAW..) doesn't panic the kernel.
66 * BSD af_unix apparently has connect forgetting to block properly.
67 * (need to check this with the POSIX spec in detail)
68 *
69 * Differences from 2.0.0-11-... (ANK)
70 * Bug fixes and improvements.
71 * - client shutdown killed server socket.
72 * - removed all useless cli/sti pairs.
73 *
74 * Semantic changes/extensions.
75 * - generic control message passing.
76 * - SCM_CREDENTIALS control message.
77 * - "Abstract" (not FS based) socket bindings.
78 * Abstract names are sequences of bytes (not zero terminated)
79 * started by 0, so that this name space does not intersect
80 * with BSD names.
81 */
82
83#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#include <linux/signal.h>
86#include <linux/sched.h>
87#include <linux/errno.h>
88#include <linux/string.h>
89#include <linux/stat.h>
90#include <linux/dcache.h>
91#include <linux/namei.h>
92#include <linux/socket.h>
93#include <linux/un.h>
94#include <linux/fcntl.h>
95#include <linux/termios.h>
96#include <linux/sockios.h>
97#include <linux/net.h>
98#include <linux/in.h>
99#include <linux/fs.h>
100#include <linux/slab.h>
101#include <asm/uaccess.h>
102#include <linux/skbuff.h>
103#include <linux/netdevice.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200104#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#include <net/sock.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -0700106#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#include <net/af_unix.h>
108#include <linux/proc_fs.h>
109#include <linux/seq_file.h>
110#include <net/scm.h>
111#include <linux/init.h>
112#include <linux/poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#include <linux/rtnetlink.h>
114#include <linux/mount.h>
115#include <net/checksum.h>
116#include <linux/security.h>
117
Pavel Emelyanovfa7ff562011-12-15 02:44:03 +0000118struct hlist_head unix_socket_table[UNIX_HASH_SIZE + 1];
119EXPORT_SYMBOL_GPL(unix_socket_table);
120DEFINE_SPINLOCK(unix_table_lock);
121EXPORT_SYMBOL_GPL(unix_table_lock);
Eric Dumazet518de9b2010-10-26 14:22:44 -0700122static atomic_long_t unix_nr_socks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124#define unix_sockets_unbound (&unix_socket_table[UNIX_HASH_SIZE])
125
126#define UNIX_ABSTRACT(sk) (unix_sk(sk)->addr->hash != UNIX_HASH_SIZE)
127
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700128#ifdef CONFIG_SECURITY_NETWORK
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700129static void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700130{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700131 memcpy(UNIXSID(skb), &scm->secid, sizeof(u32));
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700132}
133
134static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
135{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700136 scm->secid = *UNIXSID(skb);
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700137}
138#else
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700139static inline void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700140{ }
141
142static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
143{ }
144#endif /* CONFIG_SECURITY_NETWORK */
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/*
147 * SMP locking strategy:
David S. Millerfbe9cc42005-12-13 23:26:29 -0800148 * hash table is protected with spinlock unix_table_lock
Stephen Hemminger663717f2010-02-18 14:12:06 -0800149 * each socket state is protected by separate spin lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 */
151
Al Viro44bb9362006-11-14 21:36:14 -0800152static inline unsigned unix_hash_fold(__wsum n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Al Viro44bb9362006-11-14 21:36:14 -0800154 unsigned hash = (__force unsigned)n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 hash ^= hash>>16;
156 hash ^= hash>>8;
157 return hash&(UNIX_HASH_SIZE-1);
158}
159
160#define unix_peer(sk) (unix_sk(sk)->peer)
161
162static inline int unix_our_peer(struct sock *sk, struct sock *osk)
163{
164 return unix_peer(osk) == sk;
165}
166
167static inline int unix_may_send(struct sock *sk, struct sock *osk)
168{
Eric Dumazet6eba6a32008-11-16 22:58:44 -0800169 return unix_peer(osk) == NULL || unix_our_peer(sk, osk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Rainer Weikusat3c734192008-06-17 22:28:05 -0700172static inline int unix_recvq_full(struct sock const *sk)
173{
174 return skb_queue_len(&sk->sk_receive_queue) > sk->sk_max_ack_backlog;
175}
176
Pavel Emelyanovfa7ff562011-12-15 02:44:03 +0000177struct sock *unix_peer_get(struct sock *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 struct sock *peer;
180
David S. Miller1c92b4e2007-05-31 13:24:26 -0700181 unix_state_lock(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 peer = unix_peer(s);
183 if (peer)
184 sock_hold(peer);
David S. Miller1c92b4e2007-05-31 13:24:26 -0700185 unix_state_unlock(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return peer;
187}
Pavel Emelyanovfa7ff562011-12-15 02:44:03 +0000188EXPORT_SYMBOL_GPL(unix_peer_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190static inline void unix_release_addr(struct unix_address *addr)
191{
192 if (atomic_dec_and_test(&addr->refcnt))
193 kfree(addr);
194}
195
196/*
197 * Check unix socket name:
198 * - should be not zero length.
199 * - if started by not zero, should be NULL terminated (FS object)
200 * - if started by zero, it is abstract name.
201 */
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +0900202
Eric Dumazet6eba6a32008-11-16 22:58:44 -0800203static int unix_mkname(struct sockaddr_un *sunaddr, int len, unsigned *hashp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 if (len <= sizeof(short) || len > sizeof(*sunaddr))
206 return -EINVAL;
207 if (!sunaddr || sunaddr->sun_family != AF_UNIX)
208 return -EINVAL;
209 if (sunaddr->sun_path[0]) {
210 /*
211 * This may look like an off by one error but it is a bit more
212 * subtle. 108 is the longest valid AF_UNIX path for a binding.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300213 * sun_path[108] doesn't as such exist. However in kernel space
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 * we are guaranteed that it is a valid memory location in our
215 * kernel address buffer.
216 */
Jianjun Konge27dfce2008-11-01 21:38:31 -0700217 ((char *)sunaddr)[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 len = strlen(sunaddr->sun_path)+1+sizeof(short);
219 return len;
220 }
221
Joe Perches07f07572008-11-19 15:44:53 -0800222 *hashp = unix_hash_fold(csum_partial(sunaddr, len, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return len;
224}
225
226static void __unix_remove_socket(struct sock *sk)
227{
228 sk_del_node_init(sk);
229}
230
231static void __unix_insert_socket(struct hlist_head *list, struct sock *sk)
232{
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700233 WARN_ON(!sk_unhashed(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 sk_add_node(sk, list);
235}
236
237static inline void unix_remove_socket(struct sock *sk)
238{
David S. Millerfbe9cc42005-12-13 23:26:29 -0800239 spin_lock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 __unix_remove_socket(sk);
David S. Millerfbe9cc42005-12-13 23:26:29 -0800241 spin_unlock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
244static inline void unix_insert_socket(struct hlist_head *list, struct sock *sk)
245{
David S. Millerfbe9cc42005-12-13 23:26:29 -0800246 spin_lock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 __unix_insert_socket(list, sk);
David S. Millerfbe9cc42005-12-13 23:26:29 -0800248 spin_unlock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
Denis V. Lunev097e66c2007-11-19 22:29:30 -0800251static struct sock *__unix_find_socket_byname(struct net *net,
252 struct sockaddr_un *sunname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 int len, int type, unsigned hash)
254{
255 struct sock *s;
256 struct hlist_node *node;
257
258 sk_for_each(s, node, &unix_socket_table[hash ^ type]) {
259 struct unix_sock *u = unix_sk(s);
260
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +0900261 if (!net_eq(sock_net(s), net))
Denis V. Lunev097e66c2007-11-19 22:29:30 -0800262 continue;
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (u->addr->len == len &&
265 !memcmp(u->addr->name, sunname, len))
266 goto found;
267 }
268 s = NULL;
269found:
270 return s;
271}
272
Denis V. Lunev097e66c2007-11-19 22:29:30 -0800273static inline struct sock *unix_find_socket_byname(struct net *net,
274 struct sockaddr_un *sunname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 int len, int type,
276 unsigned hash)
277{
278 struct sock *s;
279
David S. Millerfbe9cc42005-12-13 23:26:29 -0800280 spin_lock(&unix_table_lock);
Denis V. Lunev097e66c2007-11-19 22:29:30 -0800281 s = __unix_find_socket_byname(net, sunname, len, type, hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (s)
283 sock_hold(s);
David S. Millerfbe9cc42005-12-13 23:26:29 -0800284 spin_unlock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return s;
286}
287
Eric W. Biederman6616f782010-06-13 03:35:48 +0000288static struct sock *unix_find_socket_byinode(struct inode *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 struct sock *s;
291 struct hlist_node *node;
292
David S. Millerfbe9cc42005-12-13 23:26:29 -0800293 spin_lock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 sk_for_each(s, node,
295 &unix_socket_table[i->i_ino & (UNIX_HASH_SIZE - 1)]) {
296 struct dentry *dentry = unix_sk(s)->dentry;
297
Eric Dumazet6eba6a32008-11-16 22:58:44 -0800298 if (dentry && dentry->d_inode == i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 sock_hold(s);
300 goto found;
301 }
302 }
303 s = NULL;
304found:
David S. Millerfbe9cc42005-12-13 23:26:29 -0800305 spin_unlock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return s;
307}
308
309static inline int unix_writable(struct sock *sk)
310{
311 return (atomic_read(&sk->sk_wmem_alloc) << 2) <= sk->sk_sndbuf;
312}
313
314static void unix_write_space(struct sock *sk)
315{
Eric Dumazet43815482010-04-29 11:01:49 +0000316 struct socket_wq *wq;
317
318 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (unix_writable(sk)) {
Eric Dumazet43815482010-04-29 11:01:49 +0000320 wq = rcu_dereference(sk->sk_wq);
321 if (wq_has_sleeper(wq))
Eric Dumazet67426b72010-10-29 20:44:44 +0000322 wake_up_interruptible_sync_poll(&wq->wait,
323 POLLOUT | POLLWRNORM | POLLWRBAND);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +0800324 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
Eric Dumazet43815482010-04-29 11:01:49 +0000326 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
329/* When dgram socket disconnects (or changes its peer), we clear its receive
330 * queue of packets arrived from previous peer. First, it allows to do
331 * flow control based only on wmem_alloc; second, sk connected to peer
332 * may receive messages only from that peer. */
333static void unix_dgram_disconnected(struct sock *sk, struct sock *other)
334{
David S. Millerb03efcf2005-07-08 14:57:23 -0700335 if (!skb_queue_empty(&sk->sk_receive_queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 skb_queue_purge(&sk->sk_receive_queue);
337 wake_up_interruptible_all(&unix_sk(sk)->peer_wait);
338
339 /* If one link of bidirectional dgram pipe is disconnected,
340 * we signal error. Messages are lost. Do not make this,
341 * when peer was not connected to us.
342 */
343 if (!sock_flag(other, SOCK_DEAD) && unix_peer(other) == sk) {
344 other->sk_err = ECONNRESET;
345 other->sk_error_report(other);
346 }
347 }
348}
349
350static void unix_sock_destructor(struct sock *sk)
351{
352 struct unix_sock *u = unix_sk(sk);
353
354 skb_queue_purge(&sk->sk_receive_queue);
355
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700356 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
357 WARN_ON(!sk_unhashed(sk));
358 WARN_ON(sk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (!sock_flag(sk, SOCK_DEAD)) {
Eric Dumazet6b41e7d2008-11-19 15:48:09 -0800360 printk(KERN_INFO "Attempt to release alive unix socket: %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return;
362 }
363
364 if (u->addr)
365 unix_release_addr(u->addr);
366
Eric Dumazet518de9b2010-10-26 14:22:44 -0700367 atomic_long_dec(&unix_nr_socks);
David S. Miller6f756a82008-11-23 17:34:03 -0800368 local_bh_disable();
Eric Dumazeta8076d82008-11-17 02:38:49 -0800369 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
David S. Miller6f756a82008-11-23 17:34:03 -0800370 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371#ifdef UNIX_REFCNT_DEBUG
Eric Dumazet518de9b2010-10-26 14:22:44 -0700372 printk(KERN_DEBUG "UNIX %p is destroyed, %ld are still alive.\n", sk,
373 atomic_long_read(&unix_nr_socks));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374#endif
375}
376
Eric Dumazet6eba6a32008-11-16 22:58:44 -0800377static int unix_release_sock(struct sock *sk, int embrion)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 struct unix_sock *u = unix_sk(sk);
380 struct dentry *dentry;
381 struct vfsmount *mnt;
382 struct sock *skpair;
383 struct sk_buff *skb;
384 int state;
385
386 unix_remove_socket(sk);
387
388 /* Clear state */
David S. Miller1c92b4e2007-05-31 13:24:26 -0700389 unix_state_lock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 sock_orphan(sk);
391 sk->sk_shutdown = SHUTDOWN_MASK;
392 dentry = u->dentry;
393 u->dentry = NULL;
394 mnt = u->mnt;
395 u->mnt = NULL;
396 state = sk->sk_state;
397 sk->sk_state = TCP_CLOSE;
David S. Miller1c92b4e2007-05-31 13:24:26 -0700398 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 wake_up_interruptible_all(&u->peer_wait);
401
Jianjun Konge27dfce2008-11-01 21:38:31 -0700402 skpair = unix_peer(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Jianjun Konge27dfce2008-11-01 21:38:31 -0700404 if (skpair != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) {
David S. Miller1c92b4e2007-05-31 13:24:26 -0700406 unix_state_lock(skpair);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /* No more writes */
408 skpair->sk_shutdown = SHUTDOWN_MASK;
409 if (!skb_queue_empty(&sk->sk_receive_queue) || embrion)
410 skpair->sk_err = ECONNRESET;
David S. Miller1c92b4e2007-05-31 13:24:26 -0700411 unix_state_unlock(skpair);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 skpair->sk_state_change(skpair);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +0800413 sk_wake_async(skpair, SOCK_WAKE_WAITD, POLL_HUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415 sock_put(skpair); /* It may now die */
416 unix_peer(sk) = NULL;
417 }
418
419 /* Try to flush out this socket. Throw out buffers at least */
420
421 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
Jianjun Konge27dfce2008-11-01 21:38:31 -0700422 if (state == TCP_LISTEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 unix_release_sock(skb->sk, 1);
424 /* passed fds are erased in the kfree_skb hook */
425 kfree_skb(skb);
426 }
427
428 if (dentry) {
429 dput(dentry);
430 mntput(mnt);
431 }
432
433 sock_put(sk);
434
435 /* ---- Socket is dead now and most probably destroyed ---- */
436
437 /*
438 * Fixme: BSD difference: In BSD all sockets connected to use get
439 * ECONNRESET and we die on the spot. In Linux we behave
440 * like files and pipes do and wait for the last
441 * dereference.
442 *
443 * Can't we simply set sock->err?
444 *
445 * What the above comment does talk about? --ANK(980817)
446 */
447
Pavel Emelyanov9305cfa2007-11-10 22:06:01 -0800448 if (unix_tot_inflight)
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +0900449 unix_gc(); /* Garbage collect fds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 return 0;
452}
453
Eric W. Biederman109f6e32010-06-13 03:30:14 +0000454static void init_peercred(struct sock *sk)
455{
456 put_pid(sk->sk_peer_pid);
457 if (sk->sk_peer_cred)
458 put_cred(sk->sk_peer_cred);
459 sk->sk_peer_pid = get_pid(task_tgid(current));
460 sk->sk_peer_cred = get_current_cred();
461}
462
463static void copy_peercred(struct sock *sk, struct sock *peersk)
464{
465 put_pid(sk->sk_peer_pid);
466 if (sk->sk_peer_cred)
467 put_cred(sk->sk_peer_cred);
468 sk->sk_peer_pid = get_pid(peersk->sk_peer_pid);
469 sk->sk_peer_cred = get_cred(peersk->sk_peer_cred);
470}
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472static int unix_listen(struct socket *sock, int backlog)
473{
474 int err;
475 struct sock *sk = sock->sk;
476 struct unix_sock *u = unix_sk(sk);
Eric W. Biederman109f6e32010-06-13 03:30:14 +0000477 struct pid *old_pid = NULL;
478 const struct cred *old_cred = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 err = -EOPNOTSUPP;
Eric Dumazet6eba6a32008-11-16 22:58:44 -0800481 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
482 goto out; /* Only stream/seqpacket sockets accept */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 err = -EINVAL;
484 if (!u->addr)
Eric Dumazet6eba6a32008-11-16 22:58:44 -0800485 goto out; /* No listens on an unbound socket */
David S. Miller1c92b4e2007-05-31 13:24:26 -0700486 unix_state_lock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (sk->sk_state != TCP_CLOSE && sk->sk_state != TCP_LISTEN)
488 goto out_unlock;
489 if (backlog > sk->sk_max_ack_backlog)
490 wake_up_interruptible_all(&u->peer_wait);
491 sk->sk_max_ack_backlog = backlog;
492 sk->sk_state = TCP_LISTEN;
493 /* set credentials so connect can copy them */
Eric W. Biederman109f6e32010-06-13 03:30:14 +0000494 init_peercred(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 err = 0;
496
497out_unlock:
David S. Miller1c92b4e2007-05-31 13:24:26 -0700498 unix_state_unlock(sk);
Eric W. Biederman109f6e32010-06-13 03:30:14 +0000499 put_pid(old_pid);
500 if (old_cred)
501 put_cred(old_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502out:
503 return err;
504}
505
506static int unix_release(struct socket *);
507static int unix_bind(struct socket *, struct sockaddr *, int);
508static int unix_stream_connect(struct socket *, struct sockaddr *,
509 int addr_len, int flags);
510static int unix_socketpair(struct socket *, struct socket *);
511static int unix_accept(struct socket *, struct socket *, int);
512static int unix_getname(struct socket *, struct sockaddr *, int *, int);
513static unsigned int unix_poll(struct file *, struct socket *, poll_table *);
Rainer Weikusatec0d2152008-06-27 19:34:18 -0700514static unsigned int unix_dgram_poll(struct file *, struct socket *,
515 poll_table *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516static int unix_ioctl(struct socket *, unsigned int, unsigned long);
517static int unix_shutdown(struct socket *, int);
518static int unix_stream_sendmsg(struct kiocb *, struct socket *,
519 struct msghdr *, size_t);
520static int unix_stream_recvmsg(struct kiocb *, struct socket *,
521 struct msghdr *, size_t, int);
522static int unix_dgram_sendmsg(struct kiocb *, struct socket *,
523 struct msghdr *, size_t);
524static int unix_dgram_recvmsg(struct kiocb *, struct socket *,
525 struct msghdr *, size_t, int);
526static int unix_dgram_connect(struct socket *, struct sockaddr *,
527 int, int);
528static int unix_seqpacket_sendmsg(struct kiocb *, struct socket *,
529 struct msghdr *, size_t);
Eric W. Biedermana05d2ad2011-04-24 01:54:57 +0000530static int unix_seqpacket_recvmsg(struct kiocb *, struct socket *,
531 struct msghdr *, size_t, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800533static const struct proto_ops unix_stream_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 .family = PF_UNIX,
535 .owner = THIS_MODULE,
536 .release = unix_release,
537 .bind = unix_bind,
538 .connect = unix_stream_connect,
539 .socketpair = unix_socketpair,
540 .accept = unix_accept,
541 .getname = unix_getname,
542 .poll = unix_poll,
543 .ioctl = unix_ioctl,
544 .listen = unix_listen,
545 .shutdown = unix_shutdown,
546 .setsockopt = sock_no_setsockopt,
547 .getsockopt = sock_no_getsockopt,
548 .sendmsg = unix_stream_sendmsg,
549 .recvmsg = unix_stream_recvmsg,
550 .mmap = sock_no_mmap,
551 .sendpage = sock_no_sendpage,
552};
553
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800554static const struct proto_ops unix_dgram_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 .family = PF_UNIX,
556 .owner = THIS_MODULE,
557 .release = unix_release,
558 .bind = unix_bind,
559 .connect = unix_dgram_connect,
560 .socketpair = unix_socketpair,
561 .accept = sock_no_accept,
562 .getname = unix_getname,
Rainer Weikusatec0d2152008-06-27 19:34:18 -0700563 .poll = unix_dgram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 .ioctl = unix_ioctl,
565 .listen = sock_no_listen,
566 .shutdown = unix_shutdown,
567 .setsockopt = sock_no_setsockopt,
568 .getsockopt = sock_no_getsockopt,
569 .sendmsg = unix_dgram_sendmsg,
570 .recvmsg = unix_dgram_recvmsg,
571 .mmap = sock_no_mmap,
572 .sendpage = sock_no_sendpage,
573};
574
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800575static const struct proto_ops unix_seqpacket_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 .family = PF_UNIX,
577 .owner = THIS_MODULE,
578 .release = unix_release,
579 .bind = unix_bind,
580 .connect = unix_stream_connect,
581 .socketpair = unix_socketpair,
582 .accept = unix_accept,
583 .getname = unix_getname,
Rainer Weikusatec0d2152008-06-27 19:34:18 -0700584 .poll = unix_dgram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 .ioctl = unix_ioctl,
586 .listen = unix_listen,
587 .shutdown = unix_shutdown,
588 .setsockopt = sock_no_setsockopt,
589 .getsockopt = sock_no_getsockopt,
590 .sendmsg = unix_seqpacket_sendmsg,
Eric W. Biedermana05d2ad2011-04-24 01:54:57 +0000591 .recvmsg = unix_seqpacket_recvmsg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 .mmap = sock_no_mmap,
593 .sendpage = sock_no_sendpage,
594};
595
596static struct proto unix_proto = {
Eric Dumazet248969a2008-11-17 00:00:30 -0800597 .name = "UNIX",
598 .owner = THIS_MODULE,
Eric Dumazet248969a2008-11-17 00:00:30 -0800599 .obj_size = sizeof(struct unix_sock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600};
601
Ingo Molnara09785a2006-07-03 00:25:12 -0700602/*
603 * AF_UNIX sockets do not interact with hardware, hence they
604 * dont trigger interrupts - so it's safe for them to have
605 * bh-unsafe locking for their sk_receive_queue.lock. Split off
606 * this special lock-class by reinitializing the spinlock key:
607 */
608static struct lock_class_key af_unix_sk_receive_queue_lock_key;
609
Eric Dumazet6eba6a32008-11-16 22:58:44 -0800610static struct sock *unix_create1(struct net *net, struct socket *sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 struct sock *sk = NULL;
613 struct unix_sock *u;
614
Eric Dumazet518de9b2010-10-26 14:22:44 -0700615 atomic_long_inc(&unix_nr_socks);
616 if (atomic_long_read(&unix_nr_socks) > 2 * get_max_files())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 goto out;
618
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700619 sk = sk_alloc(net, PF_UNIX, GFP_KERNEL, &unix_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (!sk)
621 goto out;
622
Eric Dumazet6eba6a32008-11-16 22:58:44 -0800623 sock_init_data(sock, sk);
Ingo Molnara09785a2006-07-03 00:25:12 -0700624 lockdep_set_class(&sk->sk_receive_queue.lock,
625 &af_unix_sk_receive_queue_lock_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 sk->sk_write_space = unix_write_space;
Denis V. Luneva0a53c82007-12-11 04:19:17 -0800628 sk->sk_max_ack_backlog = net->unx.sysctl_max_dgram_qlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 sk->sk_destruct = unix_sock_destructor;
630 u = unix_sk(sk);
631 u->dentry = NULL;
632 u->mnt = NULL;
Benjamin LaHaisefd19f322006-01-03 14:10:46 -0800633 spin_lock_init(&u->lock);
Al Viro516e0cc2008-07-26 00:39:17 -0400634 atomic_long_set(&u->inflight, 0);
Miklos Szeredi1fd05ba2007-07-11 14:22:39 -0700635 INIT_LIST_HEAD(&u->link);
Ingo Molnar57b47a52006-03-20 22:35:41 -0800636 mutex_init(&u->readlock); /* single task reading lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 init_waitqueue_head(&u->peer_wait);
638 unix_insert_socket(unix_sockets_unbound, sk);
639out:
Pavel Emelyanov284b3272007-11-10 22:08:30 -0800640 if (sk == NULL)
Eric Dumazet518de9b2010-10-26 14:22:44 -0700641 atomic_long_dec(&unix_nr_socks);
Eric Dumazet920de802008-11-24 00:09:29 -0800642 else {
643 local_bh_disable();
Eric Dumazeta8076d82008-11-17 02:38:49 -0800644 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
Eric Dumazet920de802008-11-24 00:09:29 -0800645 local_bh_enable();
646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return sk;
648}
649
Eric Paris3f378b62009-11-05 22:18:14 -0800650static int unix_create(struct net *net, struct socket *sock, int protocol,
651 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 if (protocol && protocol != PF_UNIX)
654 return -EPROTONOSUPPORT;
655
656 sock->state = SS_UNCONNECTED;
657
658 switch (sock->type) {
659 case SOCK_STREAM:
660 sock->ops = &unix_stream_ops;
661 break;
662 /*
663 * Believe it or not BSD has AF_UNIX, SOCK_RAW though
664 * nothing uses it.
665 */
666 case SOCK_RAW:
Jianjun Konge27dfce2008-11-01 21:38:31 -0700667 sock->type = SOCK_DGRAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 case SOCK_DGRAM:
669 sock->ops = &unix_dgram_ops;
670 break;
671 case SOCK_SEQPACKET:
672 sock->ops = &unix_seqpacket_ops;
673 break;
674 default:
675 return -ESOCKTNOSUPPORT;
676 }
677
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700678 return unix_create1(net, sock) ? 0 : -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
681static int unix_release(struct socket *sock)
682{
683 struct sock *sk = sock->sk;
684
685 if (!sk)
686 return 0;
687
688 sock->sk = NULL;
689
Eric Dumazet6eba6a32008-11-16 22:58:44 -0800690 return unix_release_sock(sk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
693static int unix_autobind(struct socket *sock)
694{
695 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900696 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 struct unix_sock *u = unix_sk(sk);
698 static u32 ordernum = 1;
Eric Dumazet6eba6a32008-11-16 22:58:44 -0800699 struct unix_address *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 int err;
Tetsuo Handa8df73ff2010-09-04 01:34:28 +0000701 unsigned int retries = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Ingo Molnar57b47a52006-03-20 22:35:41 -0800703 mutex_lock(&u->readlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 err = 0;
706 if (u->addr)
707 goto out;
708
709 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700710 addr = kzalloc(sizeof(*addr) + sizeof(short) + 16, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (!addr)
712 goto out;
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 addr->name->sun_family = AF_UNIX;
715 atomic_set(&addr->refcnt, 1);
716
717retry:
718 addr->len = sprintf(addr->name->sun_path+1, "%05x", ordernum) + 1 + sizeof(short);
Joe Perches07f07572008-11-19 15:44:53 -0800719 addr->hash = unix_hash_fold(csum_partial(addr->name, addr->len, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
David S. Millerfbe9cc42005-12-13 23:26:29 -0800721 spin_lock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 ordernum = (ordernum+1)&0xFFFFF;
723
Denis V. Lunev097e66c2007-11-19 22:29:30 -0800724 if (__unix_find_socket_byname(net, addr->name, addr->len, sock->type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 addr->hash)) {
David S. Millerfbe9cc42005-12-13 23:26:29 -0800726 spin_unlock(&unix_table_lock);
Tetsuo Handa8df73ff2010-09-04 01:34:28 +0000727 /*
728 * __unix_find_socket_byname() may take long time if many names
729 * are already in use.
730 */
731 cond_resched();
732 /* Give up if all names seems to be in use. */
733 if (retries++ == 0xFFFFF) {
734 err = -ENOSPC;
735 kfree(addr);
736 goto out;
737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 goto retry;
739 }
740 addr->hash ^= sk->sk_type;
741
742 __unix_remove_socket(sk);
743 u->addr = addr;
744 __unix_insert_socket(&unix_socket_table[addr->hash], sk);
David S. Millerfbe9cc42005-12-13 23:26:29 -0800745 spin_unlock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 err = 0;
747
Ingo Molnar57b47a52006-03-20 22:35:41 -0800748out: mutex_unlock(&u->readlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return err;
750}
751
Denis V. Lunev097e66c2007-11-19 22:29:30 -0800752static struct sock *unix_find_other(struct net *net,
753 struct sockaddr_un *sunname, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 int type, unsigned hash, int *error)
755{
756 struct sock *u;
Al Viro421748e2008-08-02 01:04:36 -0400757 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 int err = 0;
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +0900759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (sunname->sun_path[0]) {
Al Viro421748e2008-08-02 01:04:36 -0400761 struct inode *inode;
762 err = kern_path(sunname->sun_path, LOOKUP_FOLLOW, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (err)
764 goto fail;
Al Viro421748e2008-08-02 01:04:36 -0400765 inode = path.dentry->d_inode;
766 err = inode_permission(inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (err)
768 goto put_fail;
769
770 err = -ECONNREFUSED;
Al Viro421748e2008-08-02 01:04:36 -0400771 if (!S_ISSOCK(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 goto put_fail;
Eric W. Biederman6616f782010-06-13 03:35:48 +0000773 u = unix_find_socket_byinode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (!u)
775 goto put_fail;
776
777 if (u->sk_type == type)
Al Viro421748e2008-08-02 01:04:36 -0400778 touch_atime(path.mnt, path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Al Viro421748e2008-08-02 01:04:36 -0400780 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Jianjun Konge27dfce2008-11-01 21:38:31 -0700782 err = -EPROTOTYPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (u->sk_type != type) {
784 sock_put(u);
785 goto fail;
786 }
787 } else {
788 err = -ECONNREFUSED;
Jianjun Konge27dfce2008-11-01 21:38:31 -0700789 u = unix_find_socket_byname(net, sunname, len, type, hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (u) {
791 struct dentry *dentry;
792 dentry = unix_sk(u)->dentry;
793 if (dentry)
794 touch_atime(unix_sk(u)->mnt, dentry);
795 } else
796 goto fail;
797 }
798 return u;
799
800put_fail:
Al Viro421748e2008-08-02 01:04:36 -0400801 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802fail:
Jianjun Konge27dfce2008-11-01 21:38:31 -0700803 *error = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return NULL;
805}
806
807
808static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
809{
810 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900811 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 struct unix_sock *u = unix_sk(sk);
Jianjun Konge27dfce2008-11-01 21:38:31 -0700813 struct sockaddr_un *sunaddr = (struct sockaddr_un *)uaddr;
Al Virodae6ad82011-06-26 11:50:15 -0400814 char *sun_path = sunaddr->sun_path;
Eric Dumazet6eba6a32008-11-16 22:58:44 -0800815 struct dentry *dentry = NULL;
Al Virodae6ad82011-06-26 11:50:15 -0400816 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 int err;
818 unsigned hash;
819 struct unix_address *addr;
820 struct hlist_head *list;
821
822 err = -EINVAL;
823 if (sunaddr->sun_family != AF_UNIX)
824 goto out;
825
Jianjun Konge27dfce2008-11-01 21:38:31 -0700826 if (addr_len == sizeof(short)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 err = unix_autobind(sock);
828 goto out;
829 }
830
831 err = unix_mkname(sunaddr, addr_len, &hash);
832 if (err < 0)
833 goto out;
834 addr_len = err;
835
Ingo Molnar57b47a52006-03-20 22:35:41 -0800836 mutex_lock(&u->readlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 err = -EINVAL;
839 if (u->addr)
840 goto out_up;
841
842 err = -ENOMEM;
843 addr = kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);
844 if (!addr)
845 goto out_up;
846
847 memcpy(addr->name, sunaddr, addr_len);
848 addr->len = addr_len;
849 addr->hash = hash ^ sk->sk_type;
850 atomic_set(&addr->refcnt, 1);
851
Al Virodae6ad82011-06-26 11:50:15 -0400852 if (sun_path[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 unsigned int mode;
854 err = 0;
855 /*
856 * Get the parent directory, calculate the hash for last
857 * component.
858 */
Al Virodae6ad82011-06-26 11:50:15 -0400859 dentry = kern_path_create(AT_FDCWD, sun_path, &path, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 err = PTR_ERR(dentry);
861 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -0400862 goto out_mknod_parent;
Christoph Hellwigf81a0bf2005-05-19 12:26:43 -0700863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 /*
865 * All right, let's create it.
866 */
867 mode = S_IFSOCK |
Al Viroce3b0f82009-03-29 19:08:22 -0400868 (SOCK_INODE(sock)->i_mode & ~current_umask());
Al Virodae6ad82011-06-26 11:50:15 -0400869 err = mnt_want_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -0800870 if (err)
871 goto out_mknod_dput;
Al Virodae6ad82011-06-26 11:50:15 -0400872 err = security_path_mknod(&path, dentry, mode, 0);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +0900873 if (err)
874 goto out_mknod_drop_write;
Al Virodae6ad82011-06-26 11:50:15 -0400875 err = vfs_mknod(path.dentry->d_inode, dentry, mode, 0);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +0900876out_mknod_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -0400877 mnt_drop_write(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (err)
879 goto out_mknod_dput;
Al Virodae6ad82011-06-26 11:50:15 -0400880 mutex_unlock(&path.dentry->d_inode->i_mutex);
881 dput(path.dentry);
882 path.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 addr->hash = UNIX_HASH_SIZE;
885 }
886
David S. Millerfbe9cc42005-12-13 23:26:29 -0800887 spin_lock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Al Virodae6ad82011-06-26 11:50:15 -0400889 if (!sun_path[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 err = -EADDRINUSE;
Denis V. Lunev097e66c2007-11-19 22:29:30 -0800891 if (__unix_find_socket_byname(net, sunaddr, addr_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 sk->sk_type, hash)) {
893 unix_release_addr(addr);
894 goto out_unlock;
895 }
896
897 list = &unix_socket_table[addr->hash];
898 } else {
899 list = &unix_socket_table[dentry->d_inode->i_ino & (UNIX_HASH_SIZE-1)];
Al Virodae6ad82011-06-26 11:50:15 -0400900 u->dentry = path.dentry;
901 u->mnt = path.mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903
904 err = 0;
905 __unix_remove_socket(sk);
906 u->addr = addr;
907 __unix_insert_socket(list, sk);
908
909out_unlock:
David S. Millerfbe9cc42005-12-13 23:26:29 -0800910 spin_unlock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911out_up:
Ingo Molnar57b47a52006-03-20 22:35:41 -0800912 mutex_unlock(&u->readlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913out:
914 return err;
915
916out_mknod_dput:
917 dput(dentry);
Al Virodae6ad82011-06-26 11:50:15 -0400918 mutex_unlock(&path.dentry->d_inode->i_mutex);
919 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920out_mknod_parent:
Jianjun Konge27dfce2008-11-01 21:38:31 -0700921 if (err == -EEXIST)
922 err = -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 unix_release_addr(addr);
924 goto out_up;
925}
926
David S. Miller278a3de2007-05-31 15:19:20 -0700927static void unix_state_double_lock(struct sock *sk1, struct sock *sk2)
928{
929 if (unlikely(sk1 == sk2) || !sk2) {
930 unix_state_lock(sk1);
931 return;
932 }
933 if (sk1 < sk2) {
934 unix_state_lock(sk1);
935 unix_state_lock_nested(sk2);
936 } else {
937 unix_state_lock(sk2);
938 unix_state_lock_nested(sk1);
939 }
940}
941
942static void unix_state_double_unlock(struct sock *sk1, struct sock *sk2)
943{
944 if (unlikely(sk1 == sk2) || !sk2) {
945 unix_state_unlock(sk1);
946 return;
947 }
948 unix_state_unlock(sk1);
949 unix_state_unlock(sk2);
950}
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
953 int alen, int flags)
954{
955 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900956 struct net *net = sock_net(sk);
Jianjun Konge27dfce2008-11-01 21:38:31 -0700957 struct sockaddr_un *sunaddr = (struct sockaddr_un *)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 struct sock *other;
959 unsigned hash;
960 int err;
961
962 if (addr->sa_family != AF_UNSPEC) {
963 err = unix_mkname(sunaddr, alen, &hash);
964 if (err < 0)
965 goto out;
966 alen = err;
967
968 if (test_bit(SOCK_PASSCRED, &sock->flags) &&
969 !unix_sk(sk)->addr && (err = unix_autobind(sock)) != 0)
970 goto out;
971
David S. Miller278a3de2007-05-31 15:19:20 -0700972restart:
Jianjun Konge27dfce2008-11-01 21:38:31 -0700973 other = unix_find_other(net, sunaddr, alen, sock->type, hash, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (!other)
975 goto out;
976
David S. Miller278a3de2007-05-31 15:19:20 -0700977 unix_state_double_lock(sk, other);
978
979 /* Apparently VFS overslept socket death. Retry. */
980 if (sock_flag(other, SOCK_DEAD)) {
981 unix_state_double_unlock(sk, other);
982 sock_put(other);
983 goto restart;
984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 err = -EPERM;
987 if (!unix_may_send(sk, other))
988 goto out_unlock;
989
990 err = security_unix_may_send(sk->sk_socket, other->sk_socket);
991 if (err)
992 goto out_unlock;
993
994 } else {
995 /*
996 * 1003.1g breaking connected state with AF_UNSPEC
997 */
998 other = NULL;
David S. Miller278a3de2007-05-31 15:19:20 -0700999 unix_state_double_lock(sk, other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001
1002 /*
1003 * If it was connected, reconnect.
1004 */
1005 if (unix_peer(sk)) {
1006 struct sock *old_peer = unix_peer(sk);
Jianjun Konge27dfce2008-11-01 21:38:31 -07001007 unix_peer(sk) = other;
David S. Miller278a3de2007-05-31 15:19:20 -07001008 unix_state_double_unlock(sk, other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 if (other != old_peer)
1011 unix_dgram_disconnected(sk, old_peer);
1012 sock_put(old_peer);
1013 } else {
Jianjun Konge27dfce2008-11-01 21:38:31 -07001014 unix_peer(sk) = other;
David S. Miller278a3de2007-05-31 15:19:20 -07001015 unix_state_double_unlock(sk, other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09001017 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019out_unlock:
David S. Miller278a3de2007-05-31 15:19:20 -07001020 unix_state_double_unlock(sk, other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 sock_put(other);
1022out:
1023 return err;
1024}
1025
1026static long unix_wait_for_peer(struct sock *other, long timeo)
1027{
1028 struct unix_sock *u = unix_sk(other);
1029 int sched;
1030 DEFINE_WAIT(wait);
1031
1032 prepare_to_wait_exclusive(&u->peer_wait, &wait, TASK_INTERRUPTIBLE);
1033
1034 sched = !sock_flag(other, SOCK_DEAD) &&
1035 !(other->sk_shutdown & RCV_SHUTDOWN) &&
Rainer Weikusat3c734192008-06-17 22:28:05 -07001036 unix_recvq_full(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
David S. Miller1c92b4e2007-05-31 13:24:26 -07001038 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 if (sched)
1041 timeo = schedule_timeout(timeo);
1042
1043 finish_wait(&u->peer_wait, &wait);
1044 return timeo;
1045}
1046
1047static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
1048 int addr_len, int flags)
1049{
Jianjun Konge27dfce2008-11-01 21:38:31 -07001050 struct sockaddr_un *sunaddr = (struct sockaddr_un *)uaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001052 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 struct unix_sock *u = unix_sk(sk), *newu, *otheru;
1054 struct sock *newsk = NULL;
1055 struct sock *other = NULL;
1056 struct sk_buff *skb = NULL;
1057 unsigned hash;
1058 int st;
1059 int err;
1060 long timeo;
1061
1062 err = unix_mkname(sunaddr, addr_len, &hash);
1063 if (err < 0)
1064 goto out;
1065 addr_len = err;
1066
Joe Perchesf64f9e72009-11-29 16:55:45 -08001067 if (test_bit(SOCK_PASSCRED, &sock->flags) && !u->addr &&
1068 (err = unix_autobind(sock)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 goto out;
1070
1071 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
1072
1073 /* First of all allocate resources.
1074 If we will make it after state is locked,
1075 we will have to recheck all again in any case.
1076 */
1077
1078 err = -ENOMEM;
1079
1080 /* create new sock for complete connection */
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001081 newsk = unix_create1(sock_net(sk), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (newsk == NULL)
1083 goto out;
1084
1085 /* Allocate skb for sending to listening sock */
1086 skb = sock_wmalloc(newsk, 1, 0, GFP_KERNEL);
1087 if (skb == NULL)
1088 goto out;
1089
1090restart:
1091 /* Find listening sock. */
Denis V. Lunev097e66c2007-11-19 22:29:30 -08001092 other = unix_find_other(net, sunaddr, addr_len, sk->sk_type, hash, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (!other)
1094 goto out;
1095
1096 /* Latch state of peer */
David S. Miller1c92b4e2007-05-31 13:24:26 -07001097 unix_state_lock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 /* Apparently VFS overslept socket death. Retry. */
1100 if (sock_flag(other, SOCK_DEAD)) {
David S. Miller1c92b4e2007-05-31 13:24:26 -07001101 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 sock_put(other);
1103 goto restart;
1104 }
1105
1106 err = -ECONNREFUSED;
1107 if (other->sk_state != TCP_LISTEN)
1108 goto out_unlock;
Tomoki Sekiyama77238f22009-10-18 23:17:37 -07001109 if (other->sk_shutdown & RCV_SHUTDOWN)
1110 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Rainer Weikusat3c734192008-06-17 22:28:05 -07001112 if (unix_recvq_full(other)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 err = -EAGAIN;
1114 if (!timeo)
1115 goto out_unlock;
1116
1117 timeo = unix_wait_for_peer(other, timeo);
1118
1119 err = sock_intr_errno(timeo);
1120 if (signal_pending(current))
1121 goto out;
1122 sock_put(other);
1123 goto restart;
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09001124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 /* Latch our state.
1127
Daniel Balutae5537bf2011-03-14 15:25:33 -07001128 It is tricky place. We need to grab our state lock and cannot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 drop lock on peer. It is dangerous because deadlock is
1130 possible. Connect to self case and simultaneous
1131 attempt to connect are eliminated by checking socket
1132 state. other is TCP_LISTEN, if sk is TCP_LISTEN we
1133 check this before attempt to grab lock.
1134
1135 Well, and we have to recheck the state after socket locked.
1136 */
1137 st = sk->sk_state;
1138
1139 switch (st) {
1140 case TCP_CLOSE:
1141 /* This is ok... continue with connect */
1142 break;
1143 case TCP_ESTABLISHED:
1144 /* Socket is already connected */
1145 err = -EISCONN;
1146 goto out_unlock;
1147 default:
1148 err = -EINVAL;
1149 goto out_unlock;
1150 }
1151
David S. Miller1c92b4e2007-05-31 13:24:26 -07001152 unix_state_lock_nested(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 if (sk->sk_state != st) {
David S. Miller1c92b4e2007-05-31 13:24:26 -07001155 unix_state_unlock(sk);
1156 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 sock_put(other);
1158 goto restart;
1159 }
1160
David S. Miller3610cda2011-01-05 15:38:53 -08001161 err = security_unix_stream_connect(sk, other, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (err) {
David S. Miller1c92b4e2007-05-31 13:24:26 -07001163 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 goto out_unlock;
1165 }
1166
1167 /* The way is open! Fastly set all the necessary fields... */
1168
1169 sock_hold(sk);
1170 unix_peer(newsk) = sk;
1171 newsk->sk_state = TCP_ESTABLISHED;
1172 newsk->sk_type = sk->sk_type;
Eric W. Biederman109f6e32010-06-13 03:30:14 +00001173 init_peercred(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 newu = unix_sk(newsk);
Eric Dumazeteaefd112011-02-18 03:26:36 +00001175 RCU_INIT_POINTER(newsk->sk_wq, &newu->peer_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 otheru = unix_sk(other);
1177
1178 /* copy address information from listening to new sock*/
1179 if (otheru->addr) {
1180 atomic_inc(&otheru->addr->refcnt);
1181 newu->addr = otheru->addr;
1182 }
1183 if (otheru->dentry) {
1184 newu->dentry = dget(otheru->dentry);
1185 newu->mnt = mntget(otheru->mnt);
1186 }
1187
1188 /* Set credentials */
Eric W. Biederman109f6e32010-06-13 03:30:14 +00001189 copy_peercred(sk, other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 sock->state = SS_CONNECTED;
1192 sk->sk_state = TCP_ESTABLISHED;
Benjamin LaHaise830a1e52005-12-13 23:22:32 -08001193 sock_hold(newsk);
1194
1195 smp_mb__after_atomic_inc(); /* sock_hold() does an atomic_inc() */
1196 unix_peer(sk) = newsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
David S. Miller1c92b4e2007-05-31 13:24:26 -07001198 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 /* take ten and and send info to listening sock */
1201 spin_lock(&other->sk_receive_queue.lock);
1202 __skb_queue_tail(&other->sk_receive_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 spin_unlock(&other->sk_receive_queue.lock);
David S. Miller1c92b4e2007-05-31 13:24:26 -07001204 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 other->sk_data_ready(other, 0);
1206 sock_put(other);
1207 return 0;
1208
1209out_unlock:
1210 if (other)
David S. Miller1c92b4e2007-05-31 13:24:26 -07001211 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213out:
Wei Yongjun40d44442009-02-25 00:32:45 +00001214 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (newsk)
1216 unix_release_sock(newsk, 0);
1217 if (other)
1218 sock_put(other);
1219 return err;
1220}
1221
1222static int unix_socketpair(struct socket *socka, struct socket *sockb)
1223{
Jianjun Konge27dfce2008-11-01 21:38:31 -07001224 struct sock *ska = socka->sk, *skb = sockb->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 /* Join our sockets back to back */
1227 sock_hold(ska);
1228 sock_hold(skb);
Jianjun Konge27dfce2008-11-01 21:38:31 -07001229 unix_peer(ska) = skb;
1230 unix_peer(skb) = ska;
Eric W. Biederman109f6e32010-06-13 03:30:14 +00001231 init_peercred(ska);
1232 init_peercred(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 if (ska->sk_type != SOCK_DGRAM) {
1235 ska->sk_state = TCP_ESTABLISHED;
1236 skb->sk_state = TCP_ESTABLISHED;
1237 socka->state = SS_CONNECTED;
1238 sockb->state = SS_CONNECTED;
1239 }
1240 return 0;
1241}
1242
1243static int unix_accept(struct socket *sock, struct socket *newsock, int flags)
1244{
1245 struct sock *sk = sock->sk;
1246 struct sock *tsk;
1247 struct sk_buff *skb;
1248 int err;
1249
1250 err = -EOPNOTSUPP;
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001251 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 goto out;
1253
1254 err = -EINVAL;
1255 if (sk->sk_state != TCP_LISTEN)
1256 goto out;
1257
1258 /* If socket state is TCP_LISTEN it cannot change (for now...),
1259 * so that no locks are necessary.
1260 */
1261
1262 skb = skb_recv_datagram(sk, 0, flags&O_NONBLOCK, &err);
1263 if (!skb) {
1264 /* This means receive shutdown. */
1265 if (err == 0)
1266 err = -EINVAL;
1267 goto out;
1268 }
1269
1270 tsk = skb->sk;
1271 skb_free_datagram(sk, skb);
1272 wake_up_interruptible(&unix_sk(sk)->peer_wait);
1273
1274 /* attach accepted sock to socket */
David S. Miller1c92b4e2007-05-31 13:24:26 -07001275 unix_state_lock(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 newsock->state = SS_CONNECTED;
1277 sock_graft(tsk, newsock);
David S. Miller1c92b4e2007-05-31 13:24:26 -07001278 unix_state_unlock(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 return 0;
1280
1281out:
1282 return err;
1283}
1284
1285
1286static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer)
1287{
1288 struct sock *sk = sock->sk;
1289 struct unix_sock *u;
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00001290 DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 int err = 0;
1292
1293 if (peer) {
1294 sk = unix_peer_get(sk);
1295
1296 err = -ENOTCONN;
1297 if (!sk)
1298 goto out;
1299 err = 0;
1300 } else {
1301 sock_hold(sk);
1302 }
1303
1304 u = unix_sk(sk);
David S. Miller1c92b4e2007-05-31 13:24:26 -07001305 unix_state_lock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if (!u->addr) {
1307 sunaddr->sun_family = AF_UNIX;
1308 sunaddr->sun_path[0] = 0;
1309 *uaddr_len = sizeof(short);
1310 } else {
1311 struct unix_address *addr = u->addr;
1312
1313 *uaddr_len = addr->len;
1314 memcpy(sunaddr, addr->name, *uaddr_len);
1315 }
David S. Miller1c92b4e2007-05-31 13:24:26 -07001316 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 sock_put(sk);
1318out:
1319 return err;
1320}
1321
1322static void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
1323{
1324 int i;
1325
1326 scm->fp = UNIXCB(skb).fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 UNIXCB(skb).fp = NULL;
1328
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001329 for (i = scm->fp->count-1; i >= 0; i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 unix_notinflight(scm->fp->fp[i]);
1331}
1332
Eric W. Biederman7361c362010-06-13 03:34:33 +00001333static void unix_destruct_scm(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
1335 struct scm_cookie scm;
1336 memset(&scm, 0, sizeof(scm));
Eric W. Biederman7361c362010-06-13 03:34:33 +00001337 scm.pid = UNIXCB(skb).pid;
1338 scm.cred = UNIXCB(skb).cred;
1339 if (UNIXCB(skb).fp)
1340 unix_detach_fds(&scm, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 /* Alas, it calls VFS */
1343 /* So fscking what? fput() had been SMP-safe since the last Summer */
1344 scm_destroy(&scm);
1345 sock_wfree(skb);
1346}
1347
Eric Dumazet25888e32010-11-25 04:11:39 +00001348#define MAX_RECURSION_LEVEL 4
1349
Miklos Szeredi62093442008-11-09 15:23:57 +01001350static int unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 int i;
Eric Dumazet25888e32010-11-25 04:11:39 +00001353 unsigned char max_level = 0;
1354 int unix_sock_count = 0;
1355
1356 for (i = scm->fp->count - 1; i >= 0; i--) {
1357 struct sock *sk = unix_get_socket(scm->fp->fp[i]);
1358
1359 if (sk) {
1360 unix_sock_count++;
1361 max_level = max(max_level,
1362 unix_sk(sk)->recursion_level);
1363 }
1364 }
1365 if (unlikely(max_level > MAX_RECURSION_LEVEL))
1366 return -ETOOMANYREFS;
Miklos Szeredi62093442008-11-09 15:23:57 +01001367
1368 /*
1369 * Need to duplicate file references for the sake of garbage
1370 * collection. Otherwise a socket in the fps might become a
1371 * candidate for GC while the skb is not yet queued.
1372 */
1373 UNIXCB(skb).fp = scm_fp_dup(scm->fp);
1374 if (!UNIXCB(skb).fp)
1375 return -ENOMEM;
1376
Eric Dumazet25888e32010-11-25 04:11:39 +00001377 if (unix_sock_count) {
1378 for (i = scm->fp->count - 1; i >= 0; i--)
1379 unix_inflight(scm->fp->fp[i]);
1380 }
1381 return max_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
1383
David S. Millerf78a5fd2011-09-16 19:34:00 -04001384static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool send_fds)
Eric W. Biederman7361c362010-06-13 03:34:33 +00001385{
1386 int err = 0;
Eric Dumazet16e57262011-09-19 05:52:27 +00001387
David S. Millerf78a5fd2011-09-16 19:34:00 -04001388 UNIXCB(skb).pid = get_pid(scm->pid);
Eric Dumazet16e57262011-09-19 05:52:27 +00001389 if (scm->cred)
1390 UNIXCB(skb).cred = get_cred(scm->cred);
Eric W. Biederman7361c362010-06-13 03:34:33 +00001391 UNIXCB(skb).fp = NULL;
1392 if (scm->fp && send_fds)
1393 err = unix_attach_fds(scm, skb);
1394
1395 skb->destructor = unix_destruct_scm;
1396 return err;
1397}
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399/*
Eric Dumazet16e57262011-09-19 05:52:27 +00001400 * Some apps rely on write() giving SCM_CREDENTIALS
1401 * We include credentials if source or destination socket
1402 * asserted SOCK_PASSCRED.
1403 */
1404static void maybe_add_creds(struct sk_buff *skb, const struct socket *sock,
1405 const struct sock *other)
1406{
1407 if (UNIXCB(skb).cred)
1408 return;
1409 if (test_bit(SOCK_PASSCRED, &sock->flags) ||
1410 !other->sk_socket ||
1411 test_bit(SOCK_PASSCRED, &other->sk_socket->flags)) {
1412 UNIXCB(skb).pid = get_pid(task_tgid(current));
1413 UNIXCB(skb).cred = get_current_cred();
1414 }
1415}
1416
1417/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 * Send AF_UNIX data.
1419 */
1420
1421static int unix_dgram_sendmsg(struct kiocb *kiocb, struct socket *sock,
1422 struct msghdr *msg, size_t len)
1423{
1424 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1425 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001426 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 struct unix_sock *u = unix_sk(sk);
Jianjun Konge27dfce2008-11-01 21:38:31 -07001428 struct sockaddr_un *sunaddr = msg->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 struct sock *other = NULL;
1430 int namelen = 0; /* fake GCC */
1431 int err;
1432 unsigned hash;
David S. Millerf78a5fd2011-09-16 19:34:00 -04001433 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 long timeo;
1435 struct scm_cookie tmp_scm;
Eric Dumazet25888e32010-11-25 04:11:39 +00001436 int max_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 if (NULL == siocb->scm)
1439 siocb->scm = &tmp_scm;
dann frazier5f23b732008-11-26 15:32:27 -08001440 wait_for_unix_gc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 err = scm_send(sock, msg, siocb->scm);
1442 if (err < 0)
1443 return err;
1444
1445 err = -EOPNOTSUPP;
1446 if (msg->msg_flags&MSG_OOB)
1447 goto out;
1448
1449 if (msg->msg_namelen) {
1450 err = unix_mkname(sunaddr, msg->msg_namelen, &hash);
1451 if (err < 0)
1452 goto out;
1453 namelen = err;
1454 } else {
1455 sunaddr = NULL;
1456 err = -ENOTCONN;
1457 other = unix_peer_get(sk);
1458 if (!other)
1459 goto out;
1460 }
1461
Joe Perchesf64f9e72009-11-29 16:55:45 -08001462 if (test_bit(SOCK_PASSCRED, &sock->flags) && !u->addr
1463 && (err = unix_autobind(sock)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 goto out;
1465
1466 err = -EMSGSIZE;
1467 if (len > sk->sk_sndbuf - 32)
1468 goto out;
1469
1470 skb = sock_alloc_send_skb(sk, len, msg->msg_flags&MSG_DONTWAIT, &err);
Jianjun Konge27dfce2008-11-01 21:38:31 -07001471 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 goto out;
1473
David S. Millerf78a5fd2011-09-16 19:34:00 -04001474 err = unix_scm_to_skb(siocb->scm, skb, true);
Eric Dumazet25888e32010-11-25 04:11:39 +00001475 if (err < 0)
Eric W. Biederman7361c362010-06-13 03:34:33 +00001476 goto out_free;
Eric Dumazet25888e32010-11-25 04:11:39 +00001477 max_level = err + 1;
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07001478 unix_get_secdata(siocb->scm, skb);
Catherine Zhang877ce7c2006-06-29 12:27:47 -07001479
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001480 skb_reset_transport_header(skb);
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001481 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 if (err)
1483 goto out_free;
1484
1485 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1486
1487restart:
1488 if (!other) {
1489 err = -ECONNRESET;
1490 if (sunaddr == NULL)
1491 goto out_free;
1492
Denis V. Lunev097e66c2007-11-19 22:29:30 -08001493 other = unix_find_other(net, sunaddr, namelen, sk->sk_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 hash, &err);
Jianjun Konge27dfce2008-11-01 21:38:31 -07001495 if (other == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 goto out_free;
1497 }
1498
Alban Crequyd6ae3ba2011-01-18 06:39:15 +00001499 if (sk_filter(other, skb) < 0) {
1500 /* Toss the packet but do not return any error to the sender */
1501 err = len;
1502 goto out_free;
1503 }
1504
David S. Miller1c92b4e2007-05-31 13:24:26 -07001505 unix_state_lock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 err = -EPERM;
1507 if (!unix_may_send(sk, other))
1508 goto out_unlock;
1509
1510 if (sock_flag(other, SOCK_DEAD)) {
1511 /*
1512 * Check with 1003.1g - what should
1513 * datagram error
1514 */
David S. Miller1c92b4e2007-05-31 13:24:26 -07001515 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 sock_put(other);
1517
1518 err = 0;
David S. Miller1c92b4e2007-05-31 13:24:26 -07001519 unix_state_lock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if (unix_peer(sk) == other) {
Jianjun Konge27dfce2008-11-01 21:38:31 -07001521 unix_peer(sk) = NULL;
David S. Miller1c92b4e2007-05-31 13:24:26 -07001522 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 unix_dgram_disconnected(sk, other);
1525 sock_put(other);
1526 err = -ECONNREFUSED;
1527 } else {
David S. Miller1c92b4e2007-05-31 13:24:26 -07001528 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
1530
1531 other = NULL;
1532 if (err)
1533 goto out_free;
1534 goto restart;
1535 }
1536
1537 err = -EPIPE;
1538 if (other->sk_shutdown & RCV_SHUTDOWN)
1539 goto out_unlock;
1540
1541 if (sk->sk_type != SOCK_SEQPACKET) {
1542 err = security_unix_may_send(sk->sk_socket, other->sk_socket);
1543 if (err)
1544 goto out_unlock;
1545 }
1546
Rainer Weikusat3c734192008-06-17 22:28:05 -07001547 if (unix_peer(other) != sk && unix_recvq_full(other)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 if (!timeo) {
1549 err = -EAGAIN;
1550 goto out_unlock;
1551 }
1552
1553 timeo = unix_wait_for_peer(other, timeo);
1554
1555 err = sock_intr_errno(timeo);
1556 if (signal_pending(current))
1557 goto out_free;
1558
1559 goto restart;
1560 }
1561
Alban Crequy3f661162010-10-04 08:48:28 +00001562 if (sock_flag(other, SOCK_RCVTSTAMP))
1563 __net_timestamp(skb);
Eric Dumazet16e57262011-09-19 05:52:27 +00001564 maybe_add_creds(skb, sock, other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 skb_queue_tail(&other->sk_receive_queue, skb);
Eric Dumazet25888e32010-11-25 04:11:39 +00001566 if (max_level > unix_sk(other)->recursion_level)
1567 unix_sk(other)->recursion_level = max_level;
David S. Miller1c92b4e2007-05-31 13:24:26 -07001568 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 other->sk_data_ready(other, len);
1570 sock_put(other);
David S. Millerf78a5fd2011-09-16 19:34:00 -04001571 scm_destroy(siocb->scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 return len;
1573
1574out_unlock:
David S. Miller1c92b4e2007-05-31 13:24:26 -07001575 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576out_free:
1577 kfree_skb(skb);
1578out:
1579 if (other)
1580 sock_put(other);
David S. Millerf78a5fd2011-09-16 19:34:00 -04001581 scm_destroy(siocb->scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 return err;
1583}
1584
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09001585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586static int unix_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
1587 struct msghdr *msg, size_t len)
1588{
1589 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1590 struct sock *sk = sock->sk;
1591 struct sock *other = NULL;
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001592 int err, size;
David S. Millerf78a5fd2011-09-16 19:34:00 -04001593 struct sk_buff *skb;
Jianjun Konge27dfce2008-11-01 21:38:31 -07001594 int sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 struct scm_cookie tmp_scm;
Miklos Szeredi8ba69ba2009-09-11 11:31:45 -07001596 bool fds_sent = false;
Eric Dumazet25888e32010-11-25 04:11:39 +00001597 int max_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 if (NULL == siocb->scm)
1600 siocb->scm = &tmp_scm;
dann frazier5f23b732008-11-26 15:32:27 -08001601 wait_for_unix_gc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 err = scm_send(sock, msg, siocb->scm);
1603 if (err < 0)
1604 return err;
1605
1606 err = -EOPNOTSUPP;
1607 if (msg->msg_flags&MSG_OOB)
1608 goto out_err;
1609
1610 if (msg->msg_namelen) {
1611 err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP;
1612 goto out_err;
1613 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 err = -ENOTCONN;
Benjamin LaHaise830a1e52005-12-13 23:22:32 -08001615 other = unix_peer(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 if (!other)
1617 goto out_err;
1618 }
1619
1620 if (sk->sk_shutdown & SEND_SHUTDOWN)
1621 goto pipe_err;
1622
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001623 while (sent < len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 /*
Benjamin LaHaisee9df7d7f2006-03-20 21:29:05 -08001625 * Optimisation for the fact that under 0.01% of X
1626 * messages typically need breaking up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 */
1628
Benjamin LaHaisee9df7d7f2006-03-20 21:29:05 -08001629 size = len-sent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 /* Keep two messages in the pipe so it schedules better */
Benjamin LaHaisee9df7d7f2006-03-20 21:29:05 -08001632 if (size > ((sk->sk_sndbuf >> 1) - 64))
1633 size = (sk->sk_sndbuf >> 1) - 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 if (size > SKB_MAX_ALLOC)
1636 size = SKB_MAX_ALLOC;
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09001637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 /*
1639 * Grab a buffer
1640 */
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09001641
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001642 skb = sock_alloc_send_skb(sk, size, msg->msg_flags&MSG_DONTWAIT,
1643 &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Jianjun Konge27dfce2008-11-01 21:38:31 -07001645 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 goto out_err;
1647
1648 /*
1649 * If you pass two values to the sock_alloc_send_skb
1650 * it tries to grab the large buffer with GFP_NOFS
1651 * (which can fail easily), and if it fails grab the
1652 * fallback size buffer which is under a page and will
1653 * succeed. [Alan]
1654 */
1655 size = min_t(int, size, skb_tailroom(skb));
1656
Eric W. Biederman7361c362010-06-13 03:34:33 +00001657
David S. Millerf78a5fd2011-09-16 19:34:00 -04001658 /* Only send the fds in the first buffer */
1659 err = unix_scm_to_skb(siocb->scm, skb, !fds_sent);
Eric Dumazet25888e32010-11-25 04:11:39 +00001660 if (err < 0) {
Eric W. Biederman7361c362010-06-13 03:34:33 +00001661 kfree_skb(skb);
David S. Millerf78a5fd2011-09-16 19:34:00 -04001662 goto out_err;
Miklos Szeredi62093442008-11-09 15:23:57 +01001663 }
Eric Dumazet25888e32010-11-25 04:11:39 +00001664 max_level = err + 1;
Eric W. Biederman7361c362010-06-13 03:34:33 +00001665 fds_sent = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001667 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
1668 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 kfree_skb(skb);
David S. Millerf78a5fd2011-09-16 19:34:00 -04001670 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 }
1672
David S. Miller1c92b4e2007-05-31 13:24:26 -07001673 unix_state_lock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 if (sock_flag(other, SOCK_DEAD) ||
1676 (other->sk_shutdown & RCV_SHUTDOWN))
1677 goto pipe_err_free;
1678
Eric Dumazet16e57262011-09-19 05:52:27 +00001679 maybe_add_creds(skb, sock, other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 skb_queue_tail(&other->sk_receive_queue, skb);
Eric Dumazet25888e32010-11-25 04:11:39 +00001681 if (max_level > unix_sk(other)->recursion_level)
1682 unix_sk(other)->recursion_level = max_level;
David S. Miller1c92b4e2007-05-31 13:24:26 -07001683 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 other->sk_data_ready(other, size);
Jianjun Konge27dfce2008-11-01 21:38:31 -07001685 sent += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
David S. Millerf78a5fd2011-09-16 19:34:00 -04001688 scm_destroy(siocb->scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 siocb->scm = NULL;
1690
1691 return sent;
1692
1693pipe_err_free:
David S. Miller1c92b4e2007-05-31 13:24:26 -07001694 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 kfree_skb(skb);
1696pipe_err:
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001697 if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL))
1698 send_sig(SIGPIPE, current, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 err = -EPIPE;
1700out_err:
David S. Millerf78a5fd2011-09-16 19:34:00 -04001701 scm_destroy(siocb->scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 siocb->scm = NULL;
1703 return sent ? : err;
1704}
1705
1706static int unix_seqpacket_sendmsg(struct kiocb *kiocb, struct socket *sock,
1707 struct msghdr *msg, size_t len)
1708{
1709 int err;
1710 struct sock *sk = sock->sk;
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09001711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 err = sock_error(sk);
1713 if (err)
1714 return err;
1715
1716 if (sk->sk_state != TCP_ESTABLISHED)
1717 return -ENOTCONN;
1718
1719 if (msg->msg_namelen)
1720 msg->msg_namelen = 0;
1721
1722 return unix_dgram_sendmsg(kiocb, sock, msg, len);
1723}
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09001724
Eric W. Biedermana05d2ad2011-04-24 01:54:57 +00001725static int unix_seqpacket_recvmsg(struct kiocb *iocb, struct socket *sock,
1726 struct msghdr *msg, size_t size,
1727 int flags)
1728{
1729 struct sock *sk = sock->sk;
1730
1731 if (sk->sk_state != TCP_ESTABLISHED)
1732 return -ENOTCONN;
1733
1734 return unix_dgram_recvmsg(iocb, sock, msg, size, flags);
1735}
1736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737static void unix_copy_addr(struct msghdr *msg, struct sock *sk)
1738{
1739 struct unix_sock *u = unix_sk(sk);
1740
1741 msg->msg_namelen = 0;
1742 if (u->addr) {
1743 msg->msg_namelen = u->addr->len;
1744 memcpy(msg->msg_name, u->addr->name, u->addr->len);
1745 }
1746}
1747
1748static int unix_dgram_recvmsg(struct kiocb *iocb, struct socket *sock,
1749 struct msghdr *msg, size_t size,
1750 int flags)
1751{
1752 struct sock_iocb *siocb = kiocb_to_siocb(iocb);
1753 struct scm_cookie tmp_scm;
1754 struct sock *sk = sock->sk;
1755 struct unix_sock *u = unix_sk(sk);
1756 int noblock = flags & MSG_DONTWAIT;
1757 struct sk_buff *skb;
1758 int err;
1759
1760 err = -EOPNOTSUPP;
1761 if (flags&MSG_OOB)
1762 goto out;
1763
1764 msg->msg_namelen = 0;
1765
Rainer Weikusatb3ca9b02011-02-28 04:50:55 +00001766 err = mutex_lock_interruptible(&u->readlock);
1767 if (err) {
1768 err = sock_intr_errno(sock_rcvtimeo(sk, noblock));
1769 goto out;
1770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772 skb = skb_recv_datagram(sk, flags, noblock, &err);
Florian Zumbiehl0a112252007-11-29 23:19:23 +11001773 if (!skb) {
1774 unix_state_lock(sk);
1775 /* Signal EOF on disconnected non-blocking SEQPACKET socket. */
1776 if (sk->sk_type == SOCK_SEQPACKET && err == -EAGAIN &&
1777 (sk->sk_shutdown & RCV_SHUTDOWN))
1778 err = 0;
1779 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 goto out_unlock;
Florian Zumbiehl0a112252007-11-29 23:19:23 +11001781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Eric Dumazet67426b72010-10-29 20:44:44 +00001783 wake_up_interruptible_sync_poll(&u->peer_wait,
1784 POLLOUT | POLLWRNORM | POLLWRBAND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 if (msg->msg_name)
1787 unix_copy_addr(msg, skb->sk);
1788
1789 if (size > skb->len)
1790 size = skb->len;
1791 else if (size < skb->len)
1792 msg->msg_flags |= MSG_TRUNC;
1793
1794 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, size);
1795 if (err)
1796 goto out_free;
1797
Alban Crequy3f661162010-10-04 08:48:28 +00001798 if (sock_flag(sk, SOCK_RCVTSTAMP))
1799 __sock_recv_timestamp(msg, sk, skb);
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (!siocb->scm) {
1802 siocb->scm = &tmp_scm;
1803 memset(&tmp_scm, 0, sizeof(tmp_scm));
1804 }
David S. Millerf78a5fd2011-09-16 19:34:00 -04001805 scm_set_cred(siocb->scm, UNIXCB(skb).pid, UNIXCB(skb).cred);
Catherine Zhang877ce7c2006-06-29 12:27:47 -07001806 unix_set_secdata(siocb->scm, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001808 if (!(flags & MSG_PEEK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 if (UNIXCB(skb).fp)
1810 unix_detach_fds(siocb->scm, skb);
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001811 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 /* It is questionable: on PEEK we could:
1813 - do not return fds - good, but too simple 8)
1814 - return fds, and do not return them on read (old strategy,
1815 apparently wrong)
1816 - clone fds (I chose it for now, it is the most universal
1817 solution)
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09001818
1819 POSIX 1003.1g does not actually define this clearly
1820 at all. POSIX 1003.1g doesn't define a lot of things
1821 clearly however!
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 */
1824 if (UNIXCB(skb).fp)
1825 siocb->scm->fp = scm_fp_dup(UNIXCB(skb).fp);
1826 }
1827 err = size;
1828
1829 scm_recv(sock, msg, siocb->scm, flags);
1830
1831out_free:
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001832 skb_free_datagram(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833out_unlock:
Ingo Molnar57b47a52006-03-20 22:35:41 -08001834 mutex_unlock(&u->readlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835out:
1836 return err;
1837}
1838
1839/*
1840 * Sleep until data has arrive. But check for races..
1841 */
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09001842
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001843static long unix_stream_data_wait(struct sock *sk, long timeo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
1845 DEFINE_WAIT(wait);
1846
David S. Miller1c92b4e2007-05-31 13:24:26 -07001847 unix_state_lock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +00001850 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
David S. Millerb03efcf2005-07-08 14:57:23 -07001852 if (!skb_queue_empty(&sk->sk_receive_queue) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 sk->sk_err ||
1854 (sk->sk_shutdown & RCV_SHUTDOWN) ||
1855 signal_pending(current) ||
1856 !timeo)
1857 break;
1858
1859 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
David S. Miller1c92b4e2007-05-31 13:24:26 -07001860 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 timeo = schedule_timeout(timeo);
David S. Miller1c92b4e2007-05-31 13:24:26 -07001862 unix_state_lock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
1864 }
1865
Eric Dumazetaa395142010-04-20 13:03:51 +00001866 finish_wait(sk_sleep(sk), &wait);
David S. Miller1c92b4e2007-05-31 13:24:26 -07001867 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 return timeo;
1869}
1870
1871
1872
1873static int unix_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
1874 struct msghdr *msg, size_t size,
1875 int flags)
1876{
1877 struct sock_iocb *siocb = kiocb_to_siocb(iocb);
1878 struct scm_cookie tmp_scm;
1879 struct sock *sk = sock->sk;
1880 struct unix_sock *u = unix_sk(sk);
Jianjun Konge27dfce2008-11-01 21:38:31 -07001881 struct sockaddr_un *sunaddr = msg->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 int copied = 0;
1883 int check_creds = 0;
1884 int target;
1885 int err = 0;
1886 long timeo;
1887
1888 err = -EINVAL;
1889 if (sk->sk_state != TCP_ESTABLISHED)
1890 goto out;
1891
1892 err = -EOPNOTSUPP;
1893 if (flags&MSG_OOB)
1894 goto out;
1895
1896 target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
1897 timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
1898
1899 msg->msg_namelen = 0;
1900
1901 /* Lock the socket to prevent queue disordering
1902 * while sleeps in memcpy_tomsg
1903 */
1904
1905 if (!siocb->scm) {
1906 siocb->scm = &tmp_scm;
1907 memset(&tmp_scm, 0, sizeof(tmp_scm));
1908 }
1909
Rainer Weikusatb3ca9b02011-02-28 04:50:55 +00001910 err = mutex_lock_interruptible(&u->readlock);
1911 if (err) {
1912 err = sock_intr_errno(timeo);
1913 goto out;
1914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001916 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 int chunk;
1918 struct sk_buff *skb;
1919
Miklos Szeredi3c0d2f32007-06-05 13:10:29 -07001920 unix_state_lock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 skb = skb_dequeue(&sk->sk_receive_queue);
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001922 if (skb == NULL) {
Eric Dumazet25888e32010-11-25 04:11:39 +00001923 unix_sk(sk)->recursion_level = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 if (copied >= target)
Miklos Szeredi3c0d2f32007-06-05 13:10:29 -07001925 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927 /*
1928 * POSIX 1003.1g mandates this order.
1929 */
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09001930
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001931 err = sock_error(sk);
1932 if (err)
Miklos Szeredi3c0d2f32007-06-05 13:10:29 -07001933 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if (sk->sk_shutdown & RCV_SHUTDOWN)
Miklos Szeredi3c0d2f32007-06-05 13:10:29 -07001935 goto unlock;
1936
1937 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 err = -EAGAIN;
1939 if (!timeo)
1940 break;
Ingo Molnar57b47a52006-03-20 22:35:41 -08001941 mutex_unlock(&u->readlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
1943 timeo = unix_stream_data_wait(sk, timeo);
1944
Rainer Weikusatb3ca9b02011-02-28 04:50:55 +00001945 if (signal_pending(current)
1946 || mutex_lock_interruptible(&u->readlock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 err = sock_intr_errno(timeo);
1948 goto out;
1949 }
Rainer Weikusatb3ca9b02011-02-28 04:50:55 +00001950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 continue;
Miklos Szeredi3c0d2f32007-06-05 13:10:29 -07001952 unlock:
1953 unix_state_unlock(sk);
1954 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 }
Miklos Szeredi3c0d2f32007-06-05 13:10:29 -07001956 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 if (check_creds) {
1959 /* Never glue messages from different writers */
Eric W. Biederman7361c362010-06-13 03:34:33 +00001960 if ((UNIXCB(skb).pid != siocb->scm->pid) ||
1961 (UNIXCB(skb).cred != siocb->scm->cred)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 skb_queue_head(&sk->sk_receive_queue, skb);
Alexey Moiseytsev0884d7a2011-11-21 13:35:25 +00001963 sk->sk_data_ready(sk, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 break;
1965 }
1966 } else {
1967 /* Copy credentials */
David S. Millerf78a5fd2011-09-16 19:34:00 -04001968 scm_set_cred(siocb->scm, UNIXCB(skb).pid, UNIXCB(skb).cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 check_creds = 1;
1970 }
1971
1972 /* Copy address just once */
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001973 if (sunaddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 unix_copy_addr(msg, skb->sk);
1975 sunaddr = NULL;
1976 }
1977
1978 chunk = min_t(unsigned int, skb->len, size);
1979 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
1980 skb_queue_head(&sk->sk_receive_queue, skb);
Alexey Moiseytsev0884d7a2011-11-21 13:35:25 +00001981 sk->sk_data_ready(sk, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 if (copied == 0)
1983 copied = -EFAULT;
1984 break;
1985 }
1986 copied += chunk;
1987 size -= chunk;
1988
1989 /* Mark read part of skb as used */
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001990 if (!(flags & MSG_PEEK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 skb_pull(skb, chunk);
1992
1993 if (UNIXCB(skb).fp)
1994 unix_detach_fds(siocb->scm, skb);
1995
1996 /* put the skb back if we didn't use it up.. */
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001997 if (skb->len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 skb_queue_head(&sk->sk_receive_queue, skb);
Alexey Moiseytsev0884d7a2011-11-21 13:35:25 +00001999 sk->sk_data_ready(sk, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 break;
2001 }
2002
Neil Horman70d4bf62010-07-20 06:45:56 +00002003 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005 if (siocb->scm->fp)
2006 break;
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002007 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 /* It is questionable, see note in unix_dgram_recvmsg.
2009 */
2010 if (UNIXCB(skb).fp)
2011 siocb->scm->fp = scm_fp_dup(UNIXCB(skb).fp);
2012
2013 /* put message back and return */
2014 skb_queue_head(&sk->sk_receive_queue, skb);
Alexey Moiseytsev0884d7a2011-11-21 13:35:25 +00002015 sk->sk_data_ready(sk, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 break;
2017 }
2018 } while (size);
2019
Ingo Molnar57b47a52006-03-20 22:35:41 -08002020 mutex_unlock(&u->readlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 scm_recv(sock, msg, siocb->scm, flags);
2022out:
2023 return copied ? : err;
2024}
2025
2026static int unix_shutdown(struct socket *sock, int mode)
2027{
2028 struct sock *sk = sock->sk;
2029 struct sock *other;
2030
2031 mode = (mode+1)&(RCV_SHUTDOWN|SEND_SHUTDOWN);
2032
Alban Crequy7180a032011-01-19 04:56:36 +00002033 if (!mode)
2034 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Alban Crequy7180a032011-01-19 04:56:36 +00002036 unix_state_lock(sk);
2037 sk->sk_shutdown |= mode;
2038 other = unix_peer(sk);
2039 if (other)
2040 sock_hold(other);
2041 unix_state_unlock(sk);
2042 sk->sk_state_change(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Alban Crequy7180a032011-01-19 04:56:36 +00002044 if (other &&
2045 (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Alban Crequy7180a032011-01-19 04:56:36 +00002047 int peer_mode = 0;
2048
2049 if (mode&RCV_SHUTDOWN)
2050 peer_mode |= SEND_SHUTDOWN;
2051 if (mode&SEND_SHUTDOWN)
2052 peer_mode |= RCV_SHUTDOWN;
2053 unix_state_lock(other);
2054 other->sk_shutdown |= peer_mode;
2055 unix_state_unlock(other);
2056 other->sk_state_change(other);
2057 if (peer_mode == SHUTDOWN_MASK)
2058 sk_wake_async(other, SOCK_WAKE_WAITD, POLL_HUP);
2059 else if (peer_mode & RCV_SHUTDOWN)
2060 sk_wake_async(other, SOCK_WAKE_WAITD, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
Alban Crequy7180a032011-01-19 04:56:36 +00002062 if (other)
2063 sock_put(other);
2064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 return 0;
2066}
2067
2068static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
2069{
2070 struct sock *sk = sock->sk;
Jianjun Konge27dfce2008-11-01 21:38:31 -07002071 long amount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 int err;
2073
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002074 switch (cmd) {
2075 case SIOCOUTQ:
Eric Dumazet31e6d362009-06-17 19:05:41 -07002076 amount = sk_wmem_alloc_get(sk);
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002077 err = put_user(amount, (int __user *)arg);
2078 break;
2079 case SIOCINQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 {
2081 struct sk_buff *skb;
2082
2083 if (sk->sk_state == TCP_LISTEN) {
2084 err = -EINVAL;
2085 break;
2086 }
2087
2088 spin_lock(&sk->sk_receive_queue.lock);
2089 if (sk->sk_type == SOCK_STREAM ||
2090 sk->sk_type == SOCK_SEQPACKET) {
2091 skb_queue_walk(&sk->sk_receive_queue, skb)
2092 amount += skb->len;
2093 } else {
2094 skb = skb_peek(&sk->sk_receive_queue);
2095 if (skb)
Jianjun Konge27dfce2008-11-01 21:38:31 -07002096 amount = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 }
2098 spin_unlock(&sk->sk_receive_queue.lock);
2099 err = put_user(amount, (int __user *)arg);
2100 break;
2101 }
2102
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002103 default:
2104 err = -ENOIOCTLCMD;
2105 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 }
2107 return err;
2108}
2109
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002110static unsigned int unix_poll(struct file *file, struct socket *sock, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
2112 struct sock *sk = sock->sk;
2113 unsigned int mask;
2114
Eric Dumazetaa395142010-04-20 13:03:51 +00002115 sock_poll_wait(file, sk_sleep(sk), wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 mask = 0;
2117
2118 /* exceptional events? */
2119 if (sk->sk_err)
2120 mask |= POLLERR;
2121 if (sk->sk_shutdown == SHUTDOWN_MASK)
2122 mask |= POLLHUP;
Davide Libenzif348d702006-03-25 03:07:39 -08002123 if (sk->sk_shutdown & RCV_SHUTDOWN)
Eric Dumazetdb409802010-09-06 11:13:50 +00002124 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126 /* readable? */
Eric Dumazetdb409802010-09-06 11:13:50 +00002127 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 mask |= POLLIN | POLLRDNORM;
2129
2130 /* Connection-based need to check for termination and startup */
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002131 if ((sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) &&
2132 sk->sk_state == TCP_CLOSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 mask |= POLLHUP;
2134
2135 /*
2136 * we set writable also when the other side has shut down the
2137 * connection. This prevents stuck sockets.
2138 */
2139 if (unix_writable(sk))
2140 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
2141
2142 return mask;
2143}
2144
Rainer Weikusatec0d2152008-06-27 19:34:18 -07002145static unsigned int unix_dgram_poll(struct file *file, struct socket *sock,
2146 poll_table *wait)
Rainer Weikusat3c734192008-06-17 22:28:05 -07002147{
Rainer Weikusatec0d2152008-06-27 19:34:18 -07002148 struct sock *sk = sock->sk, *other;
2149 unsigned int mask, writable;
Rainer Weikusat3c734192008-06-17 22:28:05 -07002150
Eric Dumazetaa395142010-04-20 13:03:51 +00002151 sock_poll_wait(file, sk_sleep(sk), wait);
Rainer Weikusat3c734192008-06-17 22:28:05 -07002152 mask = 0;
2153
2154 /* exceptional events? */
2155 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
2156 mask |= POLLERR;
2157 if (sk->sk_shutdown & RCV_SHUTDOWN)
Eric Dumazet5456f092010-10-31 05:36:23 +00002158 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
Rainer Weikusat3c734192008-06-17 22:28:05 -07002159 if (sk->sk_shutdown == SHUTDOWN_MASK)
2160 mask |= POLLHUP;
2161
2162 /* readable? */
Eric Dumazet5456f092010-10-31 05:36:23 +00002163 if (!skb_queue_empty(&sk->sk_receive_queue))
Rainer Weikusat3c734192008-06-17 22:28:05 -07002164 mask |= POLLIN | POLLRDNORM;
2165
2166 /* Connection-based need to check for termination and startup */
2167 if (sk->sk_type == SOCK_SEQPACKET) {
2168 if (sk->sk_state == TCP_CLOSE)
2169 mask |= POLLHUP;
2170 /* connection hasn't started yet? */
2171 if (sk->sk_state == TCP_SYN_SENT)
2172 return mask;
2173 }
2174
Eric Dumazet973a34a2010-10-31 05:38:25 +00002175 /* No write status requested, avoid expensive OUT tests. */
2176 if (wait && !(wait->key & (POLLWRBAND | POLLWRNORM | POLLOUT)))
2177 return mask;
2178
Rainer Weikusatec0d2152008-06-27 19:34:18 -07002179 writable = unix_writable(sk);
Eric Dumazet5456f092010-10-31 05:36:23 +00002180 other = unix_peer_get(sk);
2181 if (other) {
2182 if (unix_peer(other) != sk) {
2183 sock_poll_wait(file, &unix_sk(other)->peer_wait, wait);
2184 if (unix_recvq_full(other))
2185 writable = 0;
Rainer Weikusatec0d2152008-06-27 19:34:18 -07002186 }
Eric Dumazet5456f092010-10-31 05:36:23 +00002187 sock_put(other);
Rainer Weikusatec0d2152008-06-27 19:34:18 -07002188 }
2189
2190 if (writable)
Rainer Weikusat3c734192008-06-17 22:28:05 -07002191 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
2192 else
2193 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
2194
Rainer Weikusat3c734192008-06-17 22:28:05 -07002195 return mask;
2196}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198#ifdef CONFIG_PROC_FS
Pavel Emelyanova53eb3f2007-11-23 20:30:01 +08002199static struct sock *first_unix_socket(int *i)
2200{
2201 for (*i = 0; *i <= UNIX_HASH_SIZE; (*i)++) {
2202 if (!hlist_empty(&unix_socket_table[*i]))
2203 return __sk_head(&unix_socket_table[*i]);
2204 }
2205 return NULL;
2206}
2207
2208static struct sock *next_unix_socket(int *i, struct sock *s)
2209{
2210 struct sock *next = sk_next(s);
2211 /* More in this chain? */
2212 if (next)
2213 return next;
2214 /* Look for next non-empty chain. */
2215 for ((*i)++; *i <= UNIX_HASH_SIZE; (*i)++) {
2216 if (!hlist_empty(&unix_socket_table[*i]))
2217 return __sk_head(&unix_socket_table[*i]);
2218 }
2219 return NULL;
2220}
2221
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002222struct unix_iter_state {
Denis V. Luneve372c412007-11-19 22:31:54 -08002223 struct seq_net_private p;
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002224 int i;
2225};
Jianjun Konge27dfce2008-11-01 21:38:31 -07002226
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002227static struct sock *unix_seq_idx(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002229 struct unix_iter_state *iter = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 loff_t off = 0;
2231 struct sock *s;
2232
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002233 for (s = first_unix_socket(&iter->i); s; s = next_unix_socket(&iter->i, s)) {
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002234 if (sock_net(s) != seq_file_net(seq))
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002235 continue;
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09002236 if (off == pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return s;
2238 ++off;
2239 }
2240 return NULL;
2241}
2242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243static void *unix_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002244 __acquires(unix_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
David S. Millerfbe9cc42005-12-13 23:26:29 -08002246 spin_lock(&unix_table_lock);
Joe Perchesb9f31242008-04-12 19:04:38 -07002247 return *pos ? unix_seq_idx(seq, *pos - 1) : SEQ_START_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248}
2249
2250static void *unix_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2251{
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002252 struct unix_iter_state *iter = seq->private;
2253 struct sock *sk = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 ++*pos;
2255
Joe Perchesb9f31242008-04-12 19:04:38 -07002256 if (v == SEQ_START_TOKEN)
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002257 sk = first_unix_socket(&iter->i);
2258 else
2259 sk = next_unix_socket(&iter->i, sk);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002260 while (sk && (sock_net(sk) != seq_file_net(seq)))
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002261 sk = next_unix_socket(&iter->i, sk);
2262 return sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263}
2264
2265static void unix_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002266 __releases(unix_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
David S. Millerfbe9cc42005-12-13 23:26:29 -08002268 spin_unlock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269}
2270
2271static int unix_seq_show(struct seq_file *seq, void *v)
2272{
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09002273
Joe Perchesb9f31242008-04-12 19:04:38 -07002274 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 seq_puts(seq, "Num RefCount Protocol Flags Type St "
2276 "Inode Path\n");
2277 else {
2278 struct sock *s = v;
2279 struct unix_sock *u = unix_sk(s);
David S. Miller1c92b4e2007-05-31 13:24:26 -07002280 unix_state_lock(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Dan Rosenberg71338aa2011-05-23 12:17:35 +00002282 seq_printf(seq, "%pK: %08X %08X %08X %04X %02X %5lu",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 s,
2284 atomic_read(&s->sk_refcnt),
2285 0,
2286 s->sk_state == TCP_LISTEN ? __SO_ACCEPTCON : 0,
2287 s->sk_type,
2288 s->sk_socket ?
2289 (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTED : SS_UNCONNECTED) :
2290 (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTING : SS_DISCONNECTING),
2291 sock_i_ino(s));
2292
2293 if (u->addr) {
2294 int i, len;
2295 seq_putc(seq, ' ');
2296
2297 i = 0;
2298 len = u->addr->len - sizeof(short);
2299 if (!UNIX_ABSTRACT(s))
2300 len--;
2301 else {
2302 seq_putc(seq, '@');
2303 i++;
2304 }
2305 for ( ; i < len; i++)
2306 seq_putc(seq, u->addr->name->sun_path[i]);
2307 }
David S. Miller1c92b4e2007-05-31 13:24:26 -07002308 unix_state_unlock(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 seq_putc(seq, '\n');
2310 }
2311
2312 return 0;
2313}
2314
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002315static const struct seq_operations unix_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 .start = unix_seq_start,
2317 .next = unix_seq_next,
2318 .stop = unix_seq_stop,
2319 .show = unix_seq_show,
2320};
2321
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322static int unix_seq_open(struct inode *inode, struct file *file)
2323{
Denis V. Luneve372c412007-11-19 22:31:54 -08002324 return seq_open_net(inode, file, &unix_seq_ops,
2325 sizeof(struct unix_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326}
2327
Arjan van de Venda7071d2007-02-12 00:55:36 -08002328static const struct file_operations unix_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 .owner = THIS_MODULE,
2330 .open = unix_seq_open,
2331 .read = seq_read,
2332 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08002333 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334};
2335
2336#endif
2337
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002338static const struct net_proto_family unix_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 .family = PF_UNIX,
2340 .create = unix_create,
2341 .owner = THIS_MODULE,
2342};
2343
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002344
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002345static int __net_init unix_net_init(struct net *net)
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002346{
2347 int error = -ENOMEM;
2348
Denis V. Luneva0a53c82007-12-11 04:19:17 -08002349 net->unx.sysctl_max_dgram_qlen = 10;
Pavel Emelyanov1597fbc2007-12-01 23:51:01 +11002350 if (unix_sysctl_register(net))
2351 goto out;
Pavel Emelyanovd392e492007-12-01 23:44:15 +11002352
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002353#ifdef CONFIG_PROC_FS
Pavel Emelyanov1597fbc2007-12-01 23:51:01 +11002354 if (!proc_net_fops_create(net, "unix", 0, &unix_seq_fops)) {
2355 unix_sysctl_unregister(net);
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002356 goto out;
Pavel Emelyanov1597fbc2007-12-01 23:51:01 +11002357 }
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002358#endif
2359 error = 0;
2360out:
Jianjun Kong48dcc33e2008-11-01 21:37:27 -07002361 return error;
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002362}
2363
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002364static void __net_exit unix_net_exit(struct net *net)
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002365{
Pavel Emelyanov1597fbc2007-12-01 23:51:01 +11002366 unix_sysctl_unregister(net);
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002367 proc_net_remove(net, "unix");
2368}
2369
2370static struct pernet_operations unix_net_ops = {
2371 .init = unix_net_init,
2372 .exit = unix_net_exit,
2373};
2374
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375static int __init af_unix_init(void)
2376{
2377 int rc = -1;
2378 struct sk_buff *dummy_skb;
2379
YOSHIFUJI Hideakief047f52006-09-01 00:29:06 -07002380 BUILD_BUG_ON(sizeof(struct unix_skb_parms) > sizeof(dummy_skb->cb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382 rc = proto_register(&unix_proto, 1);
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09002383 if (rc != 0) {
2384 printk(KERN_CRIT "%s: Cannot create unix_sock SLAB cache!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002385 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 goto out;
2387 }
2388
2389 sock_register(&unix_family_ops);
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002390 register_pernet_subsys(&unix_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391out:
2392 return rc;
2393}
2394
2395static void __exit af_unix_exit(void)
2396{
2397 sock_unregister(PF_UNIX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 proto_unregister(&unix_proto);
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002399 unregister_pernet_subsys(&unix_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400}
2401
David Woodhouse3d366962008-04-24 00:59:25 -07002402/* Earlier than device_initcall() so that other drivers invoking
2403 request_module() don't end up in a loop when modprobe tries
2404 to use a UNIX socket. But later than subsys_initcall() because
2405 we depend on stuff initialised there */
2406fs_initcall(af_unix_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407module_exit(af_unix_exit);
2408
2409MODULE_LICENSE("GPL");
2410MODULE_ALIAS_NETPROTO(PF_UNIX);