blob: 955ec152cb71eac8c91e06f1c922d1df20f6e1f0 [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
wangweidong5cc208b2013-12-06 18:03:36 +080083#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#include <linux/signal.h>
88#include <linux/sched.h>
89#include <linux/errno.h>
90#include <linux/string.h>
91#include <linux/stat.h>
92#include <linux/dcache.h>
93#include <linux/namei.h>
94#include <linux/socket.h>
95#include <linux/un.h>
96#include <linux/fcntl.h>
97#include <linux/termios.h>
98#include <linux/sockios.h>
99#include <linux/net.h>
100#include <linux/in.h>
101#include <linux/fs.h>
102#include <linux/slab.h>
103#include <asm/uaccess.h>
104#include <linux/skbuff.h>
105#include <linux/netdevice.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200106#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#include <net/sock.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -0700108#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#include <net/af_unix.h>
110#include <linux/proc_fs.h>
111#include <linux/seq_file.h>
112#include <net/scm.h>
113#include <linux/init.h>
114#include <linux/poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#include <linux/rtnetlink.h>
116#include <linux/mount.h>
117#include <net/checksum.h>
118#include <linux/security.h>
Colin Cross2b15af62013-05-06 23:50:21 +0000119#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Eric Dumazet7123aaa2012-06-08 05:03:21 +0000121struct hlist_head unix_socket_table[2 * UNIX_HASH_SIZE];
Pavel Emelyanovfa7ff562011-12-15 02:44:03 +0000122EXPORT_SYMBOL_GPL(unix_socket_table);
123DEFINE_SPINLOCK(unix_table_lock);
124EXPORT_SYMBOL_GPL(unix_table_lock);
Eric Dumazet518de9b2010-10-26 14:22:44 -0700125static atomic_long_t unix_nr_socks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Eric Dumazet7123aaa2012-06-08 05:03:21 +0000128static struct hlist_head *unix_sockets_unbound(void *addr)
129{
130 unsigned long hash = (unsigned long)addr;
131
132 hash ^= hash >> 16;
133 hash ^= hash >> 8;
134 hash %= UNIX_HASH_SIZE;
135 return &unix_socket_table[UNIX_HASH_SIZE + hash];
136}
137
138#define UNIX_ABSTRACT(sk) (unix_sk(sk)->addr->hash < UNIX_HASH_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700140#ifdef CONFIG_SECURITY_NETWORK
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700141static void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700142{
Stephen Smalley37a9a8d2015-06-10 08:44:59 -0400143 UNIXCB(skb).secid = scm->secid;
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700144}
145
146static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
147{
Stephen Smalley37a9a8d2015-06-10 08:44:59 -0400148 scm->secid = UNIXCB(skb).secid;
149}
150
151static inline bool unix_secdata_eq(struct scm_cookie *scm, struct sk_buff *skb)
152{
153 return (scm->secid == UNIXCB(skb).secid);
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700154}
155#else
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700156static inline void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700157{ }
158
159static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
160{ }
Stephen Smalley37a9a8d2015-06-10 08:44:59 -0400161
162static inline bool unix_secdata_eq(struct scm_cookie *scm, struct sk_buff *skb)
163{
164 return true;
165}
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700166#endif /* CONFIG_SECURITY_NETWORK */
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/*
169 * SMP locking strategy:
David S. Millerfbe9cc42005-12-13 23:26:29 -0800170 * hash table is protected with spinlock unix_table_lock
Stephen Hemminger663717f2010-02-18 14:12:06 -0800171 * each socket state is protected by separate spin lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 */
173
Eric Dumazet95c96172012-04-15 05:58:06 +0000174static inline unsigned int unix_hash_fold(__wsum n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Anton Blanchard0a134042014-03-05 14:29:58 +1100176 unsigned int hash = (__force unsigned int)csum_fold(n);
Eric Dumazet95c96172012-04-15 05:58:06 +0000177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 hash ^= hash>>8;
179 return hash&(UNIX_HASH_SIZE-1);
180}
181
182#define unix_peer(sk) (unix_sk(sk)->peer)
183
184static inline int unix_our_peer(struct sock *sk, struct sock *osk)
185{
186 return unix_peer(osk) == sk;
187}
188
189static inline int unix_may_send(struct sock *sk, struct sock *osk)
190{
Eric Dumazet6eba6a32008-11-16 22:58:44 -0800191 return unix_peer(osk) == NULL || unix_our_peer(sk, osk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Rainer Weikusat3c734192008-06-17 22:28:05 -0700194static inline int unix_recvq_full(struct sock const *sk)
195{
196 return skb_queue_len(&sk->sk_receive_queue) > sk->sk_max_ack_backlog;
197}
198
Pavel Emelyanovfa7ff562011-12-15 02:44:03 +0000199struct sock *unix_peer_get(struct sock *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 struct sock *peer;
202
David S. Miller1c92b4e2007-05-31 13:24:26 -0700203 unix_state_lock(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 peer = unix_peer(s);
205 if (peer)
206 sock_hold(peer);
David S. Miller1c92b4e2007-05-31 13:24:26 -0700207 unix_state_unlock(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return peer;
209}
Pavel Emelyanovfa7ff562011-12-15 02:44:03 +0000210EXPORT_SYMBOL_GPL(unix_peer_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212static inline void unix_release_addr(struct unix_address *addr)
213{
214 if (atomic_dec_and_test(&addr->refcnt))
215 kfree(addr);
216}
217
218/*
219 * Check unix socket name:
220 * - should be not zero length.
221 * - if started by not zero, should be NULL terminated (FS object)
222 * - if started by zero, it is abstract name.
223 */
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +0900224
Eric Dumazet95c96172012-04-15 05:58:06 +0000225static int unix_mkname(struct sockaddr_un *sunaddr, int len, unsigned int *hashp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 if (len <= sizeof(short) || len > sizeof(*sunaddr))
228 return -EINVAL;
229 if (!sunaddr || sunaddr->sun_family != AF_UNIX)
230 return -EINVAL;
231 if (sunaddr->sun_path[0]) {
232 /*
233 * This may look like an off by one error but it is a bit more
234 * subtle. 108 is the longest valid AF_UNIX path for a binding.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300235 * sun_path[108] doesn't as such exist. However in kernel space
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 * we are guaranteed that it is a valid memory location in our
237 * kernel address buffer.
238 */
Jianjun Konge27dfce2008-11-01 21:38:31 -0700239 ((char *)sunaddr)[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 len = strlen(sunaddr->sun_path)+1+sizeof(short);
241 return len;
242 }
243
Joe Perches07f07572008-11-19 15:44:53 -0800244 *hashp = unix_hash_fold(csum_partial(sunaddr, len, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return len;
246}
247
248static void __unix_remove_socket(struct sock *sk)
249{
250 sk_del_node_init(sk);
251}
252
253static void __unix_insert_socket(struct hlist_head *list, struct sock *sk)
254{
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700255 WARN_ON(!sk_unhashed(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 sk_add_node(sk, list);
257}
258
259static inline void unix_remove_socket(struct sock *sk)
260{
David S. Millerfbe9cc42005-12-13 23:26:29 -0800261 spin_lock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 __unix_remove_socket(sk);
David S. Millerfbe9cc42005-12-13 23:26:29 -0800263 spin_unlock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266static inline void unix_insert_socket(struct hlist_head *list, struct sock *sk)
267{
David S. Millerfbe9cc42005-12-13 23:26:29 -0800268 spin_lock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 __unix_insert_socket(list, sk);
David S. Millerfbe9cc42005-12-13 23:26:29 -0800270 spin_unlock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
Denis V. Lunev097e66c2007-11-19 22:29:30 -0800273static struct sock *__unix_find_socket_byname(struct net *net,
274 struct sockaddr_un *sunname,
Eric Dumazet95c96172012-04-15 05:58:06 +0000275 int len, int type, unsigned int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Sasha Levinb67bfe02013-02-27 17:06:00 -0800279 sk_for_each(s, &unix_socket_table[hash ^ type]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 struct unix_sock *u = unix_sk(s);
281
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +0900282 if (!net_eq(sock_net(s), net))
Denis V. Lunev097e66c2007-11-19 22:29:30 -0800283 continue;
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (u->addr->len == len &&
286 !memcmp(u->addr->name, sunname, len))
287 goto found;
288 }
289 s = NULL;
290found:
291 return s;
292}
293
Denis V. Lunev097e66c2007-11-19 22:29:30 -0800294static inline struct sock *unix_find_socket_byname(struct net *net,
295 struct sockaddr_un *sunname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 int len, int type,
Eric Dumazet95c96172012-04-15 05:58:06 +0000297 unsigned int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 struct sock *s;
300
David S. Millerfbe9cc42005-12-13 23:26:29 -0800301 spin_lock(&unix_table_lock);
Denis V. Lunev097e66c2007-11-19 22:29:30 -0800302 s = __unix_find_socket_byname(net, sunname, len, type, hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (s)
304 sock_hold(s);
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
Eric W. Biederman6616f782010-06-13 03:35:48 +0000309static struct sock *unix_find_socket_byinode(struct inode *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
David S. Millerfbe9cc42005-12-13 23:26:29 -0800313 spin_lock(&unix_table_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800314 sk_for_each(s,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 &unix_socket_table[i->i_ino & (UNIX_HASH_SIZE - 1)]) {
Al Viro40ffe672012-03-14 21:54:32 -0400316 struct dentry *dentry = unix_sk(s)->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
David Howellsa25b3762015-03-17 22:26:21 +0000318 if (dentry && d_backing_inode(dentry) == i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 sock_hold(s);
320 goto found;
321 }
322 }
323 s = NULL;
324found:
David S. Millerfbe9cc42005-12-13 23:26:29 -0800325 spin_unlock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return s;
327}
328
Eric Dumazet1586a582015-10-23 10:59:16 -0700329static int unix_writable(const struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Eric Dumazet1586a582015-10-23 10:59:16 -0700331 return sk->sk_state != TCP_LISTEN &&
332 (atomic_read(&sk->sk_wmem_alloc) << 2) <= sk->sk_sndbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
335static void unix_write_space(struct sock *sk)
336{
Eric Dumazet43815482010-04-29 11:01:49 +0000337 struct socket_wq *wq;
338
339 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (unix_writable(sk)) {
Eric Dumazet43815482010-04-29 11:01:49 +0000341 wq = rcu_dereference(sk->sk_wq);
342 if (wq_has_sleeper(wq))
Eric Dumazet67426b72010-10-29 20:44:44 +0000343 wake_up_interruptible_sync_poll(&wq->wait,
344 POLLOUT | POLLWRNORM | POLLWRBAND);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +0800345 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
Eric Dumazet43815482010-04-29 11:01:49 +0000347 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
350/* When dgram socket disconnects (or changes its peer), we clear its receive
351 * queue of packets arrived from previous peer. First, it allows to do
352 * flow control based only on wmem_alloc; second, sk connected to peer
353 * may receive messages only from that peer. */
354static void unix_dgram_disconnected(struct sock *sk, struct sock *other)
355{
David S. Millerb03efcf2005-07-08 14:57:23 -0700356 if (!skb_queue_empty(&sk->sk_receive_queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 skb_queue_purge(&sk->sk_receive_queue);
358 wake_up_interruptible_all(&unix_sk(sk)->peer_wait);
359
360 /* If one link of bidirectional dgram pipe is disconnected,
361 * we signal error. Messages are lost. Do not make this,
362 * when peer was not connected to us.
363 */
364 if (!sock_flag(other, SOCK_DEAD) && unix_peer(other) == sk) {
365 other->sk_err = ECONNRESET;
366 other->sk_error_report(other);
367 }
368 }
369}
370
371static void unix_sock_destructor(struct sock *sk)
372{
373 struct unix_sock *u = unix_sk(sk);
374
375 skb_queue_purge(&sk->sk_receive_queue);
376
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700377 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
378 WARN_ON(!sk_unhashed(sk));
379 WARN_ON(sk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (!sock_flag(sk, SOCK_DEAD)) {
wangweidong5cc208b2013-12-06 18:03:36 +0800381 pr_info("Attempt to release alive unix socket: %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return;
383 }
384
385 if (u->addr)
386 unix_release_addr(u->addr);
387
Eric Dumazet518de9b2010-10-26 14:22:44 -0700388 atomic_long_dec(&unix_nr_socks);
David S. Miller6f756a82008-11-23 17:34:03 -0800389 local_bh_disable();
Eric Dumazeta8076d82008-11-17 02:38:49 -0800390 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
David S. Miller6f756a82008-11-23 17:34:03 -0800391 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392#ifdef UNIX_REFCNT_DEBUG
wangweidong5cc208b2013-12-06 18:03:36 +0800393 pr_debug("UNIX %p is destroyed, %ld are still alive.\n", sk,
Eric Dumazet518de9b2010-10-26 14:22:44 -0700394 atomic_long_read(&unix_nr_socks));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395#endif
396}
397
Paul Mooreded34e02013-03-25 03:18:33 +0000398static void unix_release_sock(struct sock *sk, int embrion)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 struct unix_sock *u = unix_sk(sk);
Al Viro40ffe672012-03-14 21:54:32 -0400401 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 struct sock *skpair;
403 struct sk_buff *skb;
404 int state;
405
406 unix_remove_socket(sk);
407
408 /* Clear state */
David S. Miller1c92b4e2007-05-31 13:24:26 -0700409 unix_state_lock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 sock_orphan(sk);
411 sk->sk_shutdown = SHUTDOWN_MASK;
Al Viro40ffe672012-03-14 21:54:32 -0400412 path = u->path;
413 u->path.dentry = NULL;
414 u->path.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 state = sk->sk_state;
416 sk->sk_state = TCP_CLOSE;
David S. Miller1c92b4e2007-05-31 13:24:26 -0700417 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 wake_up_interruptible_all(&u->peer_wait);
420
Jianjun Konge27dfce2008-11-01 21:38:31 -0700421 skpair = unix_peer(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Jianjun Konge27dfce2008-11-01 21:38:31 -0700423 if (skpair != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) {
David S. Miller1c92b4e2007-05-31 13:24:26 -0700425 unix_state_lock(skpair);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 /* No more writes */
427 skpair->sk_shutdown = SHUTDOWN_MASK;
428 if (!skb_queue_empty(&sk->sk_receive_queue) || embrion)
429 skpair->sk_err = ECONNRESET;
David S. Miller1c92b4e2007-05-31 13:24:26 -0700430 unix_state_unlock(skpair);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 skpair->sk_state_change(skpair);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +0800432 sk_wake_async(skpair, SOCK_WAKE_WAITD, POLL_HUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434 sock_put(skpair); /* It may now die */
435 unix_peer(sk) = NULL;
436 }
437
438 /* Try to flush out this socket. Throw out buffers at least */
439
440 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
Jianjun Konge27dfce2008-11-01 21:38:31 -0700441 if (state == TCP_LISTEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 unix_release_sock(skb->sk, 1);
443 /* passed fds are erased in the kfree_skb hook */
Hannes Frederic Sowa73ed5d22015-11-10 16:23:15 +0100444 UNIXCB(skb).consumed = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 kfree_skb(skb);
446 }
447
Al Viro40ffe672012-03-14 21:54:32 -0400448 if (path.dentry)
449 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 sock_put(sk);
452
453 /* ---- Socket is dead now and most probably destroyed ---- */
454
455 /*
Alan Coxe04dae82012-09-17 00:52:41 +0000456 * Fixme: BSD difference: In BSD all sockets connected to us get
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 * ECONNRESET and we die on the spot. In Linux we behave
458 * like files and pipes do and wait for the last
459 * dereference.
460 *
461 * Can't we simply set sock->err?
462 *
463 * What the above comment does talk about? --ANK(980817)
464 */
465
Pavel Emelyanov9305cfa2007-11-10 22:06:01 -0800466 if (unix_tot_inflight)
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +0900467 unix_gc(); /* Garbage collect fds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Eric W. Biederman109f6e32010-06-13 03:30:14 +0000470static void init_peercred(struct sock *sk)
471{
472 put_pid(sk->sk_peer_pid);
473 if (sk->sk_peer_cred)
474 put_cred(sk->sk_peer_cred);
475 sk->sk_peer_pid = get_pid(task_tgid(current));
476 sk->sk_peer_cred = get_current_cred();
477}
478
479static void copy_peercred(struct sock *sk, struct sock *peersk)
480{
481 put_pid(sk->sk_peer_pid);
482 if (sk->sk_peer_cred)
483 put_cred(sk->sk_peer_cred);
484 sk->sk_peer_pid = get_pid(peersk->sk_peer_pid);
485 sk->sk_peer_cred = get_cred(peersk->sk_peer_cred);
486}
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488static int unix_listen(struct socket *sock, int backlog)
489{
490 int err;
491 struct sock *sk = sock->sk;
492 struct unix_sock *u = unix_sk(sk);
Eric W. Biederman109f6e32010-06-13 03:30:14 +0000493 struct pid *old_pid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 err = -EOPNOTSUPP;
Eric Dumazet6eba6a32008-11-16 22:58:44 -0800496 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
497 goto out; /* Only stream/seqpacket sockets accept */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 err = -EINVAL;
499 if (!u->addr)
Eric Dumazet6eba6a32008-11-16 22:58:44 -0800500 goto out; /* No listens on an unbound socket */
David S. Miller1c92b4e2007-05-31 13:24:26 -0700501 unix_state_lock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (sk->sk_state != TCP_CLOSE && sk->sk_state != TCP_LISTEN)
503 goto out_unlock;
504 if (backlog > sk->sk_max_ack_backlog)
505 wake_up_interruptible_all(&u->peer_wait);
506 sk->sk_max_ack_backlog = backlog;
507 sk->sk_state = TCP_LISTEN;
508 /* set credentials so connect can copy them */
Eric W. Biederman109f6e32010-06-13 03:30:14 +0000509 init_peercred(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 err = 0;
511
512out_unlock:
David S. Miller1c92b4e2007-05-31 13:24:26 -0700513 unix_state_unlock(sk);
Eric W. Biederman109f6e32010-06-13 03:30:14 +0000514 put_pid(old_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515out:
516 return err;
517}
518
519static int unix_release(struct socket *);
520static int unix_bind(struct socket *, struct sockaddr *, int);
521static int unix_stream_connect(struct socket *, struct sockaddr *,
522 int addr_len, int flags);
523static int unix_socketpair(struct socket *, struct socket *);
524static int unix_accept(struct socket *, struct socket *, int);
525static int unix_getname(struct socket *, struct sockaddr *, int *, int);
526static unsigned int unix_poll(struct file *, struct socket *, poll_table *);
Rainer Weikusatec0d2152008-06-27 19:34:18 -0700527static unsigned int unix_dgram_poll(struct file *, struct socket *,
528 poll_table *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529static int unix_ioctl(struct socket *, unsigned int, unsigned long);
530static int unix_shutdown(struct socket *, int);
Ying Xue1b784142015-03-02 15:37:48 +0800531static int unix_stream_sendmsg(struct socket *, struct msghdr *, size_t);
532static int unix_stream_recvmsg(struct socket *, struct msghdr *, size_t, int);
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +0200533static ssize_t unix_stream_sendpage(struct socket *, struct page *, int offset,
534 size_t size, int flags);
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +0200535static ssize_t unix_stream_splice_read(struct socket *, loff_t *ppos,
536 struct pipe_inode_info *, size_t size,
537 unsigned int flags);
Ying Xue1b784142015-03-02 15:37:48 +0800538static int unix_dgram_sendmsg(struct socket *, struct msghdr *, size_t);
539static int unix_dgram_recvmsg(struct socket *, struct msghdr *, size_t, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540static int unix_dgram_connect(struct socket *, struct sockaddr *,
541 int, int);
Ying Xue1b784142015-03-02 15:37:48 +0800542static int unix_seqpacket_sendmsg(struct socket *, struct msghdr *, size_t);
543static int unix_seqpacket_recvmsg(struct socket *, struct msghdr *, size_t,
544 int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Sasha Levin12663bf2013-12-07 17:26:27 -0500546static int unix_set_peek_off(struct sock *sk, int val)
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +0000547{
548 struct unix_sock *u = unix_sk(sk);
549
Sasha Levin12663bf2013-12-07 17:26:27 -0500550 if (mutex_lock_interruptible(&u->readlock))
551 return -EINTR;
552
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +0000553 sk->sk_peek_off = val;
554 mutex_unlock(&u->readlock);
Sasha Levin12663bf2013-12-07 17:26:27 -0500555
556 return 0;
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +0000557}
558
559
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800560static const struct proto_ops unix_stream_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 .family = PF_UNIX,
562 .owner = THIS_MODULE,
563 .release = unix_release,
564 .bind = unix_bind,
565 .connect = unix_stream_connect,
566 .socketpair = unix_socketpair,
567 .accept = unix_accept,
568 .getname = unix_getname,
569 .poll = unix_poll,
570 .ioctl = unix_ioctl,
571 .listen = unix_listen,
572 .shutdown = unix_shutdown,
573 .setsockopt = sock_no_setsockopt,
574 .getsockopt = sock_no_getsockopt,
575 .sendmsg = unix_stream_sendmsg,
576 .recvmsg = unix_stream_recvmsg,
577 .mmap = sock_no_mmap,
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +0200578 .sendpage = unix_stream_sendpage,
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +0200579 .splice_read = unix_stream_splice_read,
Pavel Emelyanovfc0d7532012-02-21 07:32:06 +0000580 .set_peek_off = unix_set_peek_off,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581};
582
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800583static const struct proto_ops unix_dgram_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 .family = PF_UNIX,
585 .owner = THIS_MODULE,
586 .release = unix_release,
587 .bind = unix_bind,
588 .connect = unix_dgram_connect,
589 .socketpair = unix_socketpair,
590 .accept = sock_no_accept,
591 .getname = unix_getname,
Rainer Weikusatec0d2152008-06-27 19:34:18 -0700592 .poll = unix_dgram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 .ioctl = unix_ioctl,
594 .listen = sock_no_listen,
595 .shutdown = unix_shutdown,
596 .setsockopt = sock_no_setsockopt,
597 .getsockopt = sock_no_getsockopt,
598 .sendmsg = unix_dgram_sendmsg,
599 .recvmsg = unix_dgram_recvmsg,
600 .mmap = sock_no_mmap,
601 .sendpage = sock_no_sendpage,
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +0000602 .set_peek_off = unix_set_peek_off,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603};
604
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800605static const struct proto_ops unix_seqpacket_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 .family = PF_UNIX,
607 .owner = THIS_MODULE,
608 .release = unix_release,
609 .bind = unix_bind,
610 .connect = unix_stream_connect,
611 .socketpair = unix_socketpair,
612 .accept = unix_accept,
613 .getname = unix_getname,
Rainer Weikusatec0d2152008-06-27 19:34:18 -0700614 .poll = unix_dgram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 .ioctl = unix_ioctl,
616 .listen = unix_listen,
617 .shutdown = unix_shutdown,
618 .setsockopt = sock_no_setsockopt,
619 .getsockopt = sock_no_getsockopt,
620 .sendmsg = unix_seqpacket_sendmsg,
Eric W. Biedermana05d2ad2011-04-24 01:54:57 +0000621 .recvmsg = unix_seqpacket_recvmsg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 .mmap = sock_no_mmap,
623 .sendpage = sock_no_sendpage,
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +0000624 .set_peek_off = unix_set_peek_off,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625};
626
627static struct proto unix_proto = {
Eric Dumazet248969a2008-11-17 00:00:30 -0800628 .name = "UNIX",
629 .owner = THIS_MODULE,
Eric Dumazet248969a2008-11-17 00:00:30 -0800630 .obj_size = sizeof(struct unix_sock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631};
632
Ingo Molnara09785a2006-07-03 00:25:12 -0700633/*
634 * AF_UNIX sockets do not interact with hardware, hence they
635 * dont trigger interrupts - so it's safe for them to have
636 * bh-unsafe locking for their sk_receive_queue.lock. Split off
637 * this special lock-class by reinitializing the spinlock key:
638 */
639static struct lock_class_key af_unix_sk_receive_queue_lock_key;
640
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500641static struct sock *unix_create1(struct net *net, struct socket *sock, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 struct sock *sk = NULL;
644 struct unix_sock *u;
645
Eric Dumazet518de9b2010-10-26 14:22:44 -0700646 atomic_long_inc(&unix_nr_socks);
647 if (atomic_long_read(&unix_nr_socks) > 2 * get_max_files())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 goto out;
649
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500650 sk = sk_alloc(net, PF_UNIX, GFP_KERNEL, &unix_proto, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (!sk)
652 goto out;
653
Eric Dumazet6eba6a32008-11-16 22:58:44 -0800654 sock_init_data(sock, sk);
Ingo Molnara09785a2006-07-03 00:25:12 -0700655 lockdep_set_class(&sk->sk_receive_queue.lock,
656 &af_unix_sk_receive_queue_lock_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 sk->sk_write_space = unix_write_space;
Denis V. Luneva0a53c82007-12-11 04:19:17 -0800659 sk->sk_max_ack_backlog = net->unx.sysctl_max_dgram_qlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 sk->sk_destruct = unix_sock_destructor;
661 u = unix_sk(sk);
Al Viro40ffe672012-03-14 21:54:32 -0400662 u->path.dentry = NULL;
663 u->path.mnt = NULL;
Benjamin LaHaisefd19f322006-01-03 14:10:46 -0800664 spin_lock_init(&u->lock);
Al Viro516e0cc2008-07-26 00:39:17 -0400665 atomic_long_set(&u->inflight, 0);
Miklos Szeredi1fd05ba2007-07-11 14:22:39 -0700666 INIT_LIST_HEAD(&u->link);
Ingo Molnar57b47a52006-03-20 22:35:41 -0800667 mutex_init(&u->readlock); /* single task reading lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 init_waitqueue_head(&u->peer_wait);
Eric Dumazet7123aaa2012-06-08 05:03:21 +0000669 unix_insert_socket(unix_sockets_unbound(sk), sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670out:
Pavel Emelyanov284b3272007-11-10 22:08:30 -0800671 if (sk == NULL)
Eric Dumazet518de9b2010-10-26 14:22:44 -0700672 atomic_long_dec(&unix_nr_socks);
Eric Dumazet920de802008-11-24 00:09:29 -0800673 else {
674 local_bh_disable();
Eric Dumazeta8076d82008-11-17 02:38:49 -0800675 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
Eric Dumazet920de802008-11-24 00:09:29 -0800676 local_bh_enable();
677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return sk;
679}
680
Eric Paris3f378b62009-11-05 22:18:14 -0800681static int unix_create(struct net *net, struct socket *sock, int protocol,
682 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 if (protocol && protocol != PF_UNIX)
685 return -EPROTONOSUPPORT;
686
687 sock->state = SS_UNCONNECTED;
688
689 switch (sock->type) {
690 case SOCK_STREAM:
691 sock->ops = &unix_stream_ops;
692 break;
693 /*
694 * Believe it or not BSD has AF_UNIX, SOCK_RAW though
695 * nothing uses it.
696 */
697 case SOCK_RAW:
Jianjun Konge27dfce2008-11-01 21:38:31 -0700698 sock->type = SOCK_DGRAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 case SOCK_DGRAM:
700 sock->ops = &unix_dgram_ops;
701 break;
702 case SOCK_SEQPACKET:
703 sock->ops = &unix_seqpacket_ops;
704 break;
705 default:
706 return -ESOCKTNOSUPPORT;
707 }
708
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500709 return unix_create1(net, sock, kern) ? 0 : -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
712static int unix_release(struct socket *sock)
713{
714 struct sock *sk = sock->sk;
715
716 if (!sk)
717 return 0;
718
Paul Mooreded34e02013-03-25 03:18:33 +0000719 unix_release_sock(sk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 sock->sk = NULL;
721
Paul Mooreded34e02013-03-25 03:18:33 +0000722 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
725static int unix_autobind(struct socket *sock)
726{
727 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900728 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 struct unix_sock *u = unix_sk(sk);
730 static u32 ordernum = 1;
Eric Dumazet6eba6a32008-11-16 22:58:44 -0800731 struct unix_address *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 int err;
Tetsuo Handa8df73ff2010-09-04 01:34:28 +0000733 unsigned int retries = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Sasha Levin37ab4fa2013-12-13 10:54:22 -0500735 err = mutex_lock_interruptible(&u->readlock);
736 if (err)
737 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 err = 0;
740 if (u->addr)
741 goto out;
742
743 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700744 addr = kzalloc(sizeof(*addr) + sizeof(short) + 16, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (!addr)
746 goto out;
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 addr->name->sun_family = AF_UNIX;
749 atomic_set(&addr->refcnt, 1);
750
751retry:
752 addr->len = sprintf(addr->name->sun_path+1, "%05x", ordernum) + 1 + sizeof(short);
Joe Perches07f07572008-11-19 15:44:53 -0800753 addr->hash = unix_hash_fold(csum_partial(addr->name, addr->len, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
David S. Millerfbe9cc42005-12-13 23:26:29 -0800755 spin_lock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 ordernum = (ordernum+1)&0xFFFFF;
757
Denis V. Lunev097e66c2007-11-19 22:29:30 -0800758 if (__unix_find_socket_byname(net, addr->name, addr->len, sock->type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 addr->hash)) {
David S. Millerfbe9cc42005-12-13 23:26:29 -0800760 spin_unlock(&unix_table_lock);
Tetsuo Handa8df73ff2010-09-04 01:34:28 +0000761 /*
762 * __unix_find_socket_byname() may take long time if many names
763 * are already in use.
764 */
765 cond_resched();
766 /* Give up if all names seems to be in use. */
767 if (retries++ == 0xFFFFF) {
768 err = -ENOSPC;
769 kfree(addr);
770 goto out;
771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 goto retry;
773 }
774 addr->hash ^= sk->sk_type;
775
776 __unix_remove_socket(sk);
777 u->addr = addr;
778 __unix_insert_socket(&unix_socket_table[addr->hash], sk);
David S. Millerfbe9cc42005-12-13 23:26:29 -0800779 spin_unlock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 err = 0;
781
Ingo Molnar57b47a52006-03-20 22:35:41 -0800782out: mutex_unlock(&u->readlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return err;
784}
785
Denis V. Lunev097e66c2007-11-19 22:29:30 -0800786static struct sock *unix_find_other(struct net *net,
787 struct sockaddr_un *sunname, int len,
Eric Dumazet95c96172012-04-15 05:58:06 +0000788 int type, unsigned int hash, int *error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
790 struct sock *u;
Al Viro421748e2008-08-02 01:04:36 -0400791 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 int err = 0;
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +0900793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (sunname->sun_path[0]) {
Al Viro421748e2008-08-02 01:04:36 -0400795 struct inode *inode;
796 err = kern_path(sunname->sun_path, LOOKUP_FOLLOW, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (err)
798 goto fail;
David Howellsa25b3762015-03-17 22:26:21 +0000799 inode = d_backing_inode(path.dentry);
Al Viro421748e2008-08-02 01:04:36 -0400800 err = inode_permission(inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (err)
802 goto put_fail;
803
804 err = -ECONNREFUSED;
Al Viro421748e2008-08-02 01:04:36 -0400805 if (!S_ISSOCK(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 goto put_fail;
Eric W. Biederman6616f782010-06-13 03:35:48 +0000807 u = unix_find_socket_byinode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (!u)
809 goto put_fail;
810
811 if (u->sk_type == type)
Al Viro68ac1232012-03-15 08:21:57 -0400812 touch_atime(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Al Viro421748e2008-08-02 01:04:36 -0400814 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Jianjun Konge27dfce2008-11-01 21:38:31 -0700816 err = -EPROTOTYPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (u->sk_type != type) {
818 sock_put(u);
819 goto fail;
820 }
821 } else {
822 err = -ECONNREFUSED;
Jianjun Konge27dfce2008-11-01 21:38:31 -0700823 u = unix_find_socket_byname(net, sunname, len, type, hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (u) {
825 struct dentry *dentry;
Al Viro40ffe672012-03-14 21:54:32 -0400826 dentry = unix_sk(u)->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (dentry)
Al Viro68ac1232012-03-15 08:21:57 -0400828 touch_atime(&unix_sk(u)->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 } else
830 goto fail;
831 }
832 return u;
833
834put_fail:
Al Viro421748e2008-08-02 01:04:36 -0400835 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836fail:
Jianjun Konge27dfce2008-11-01 21:38:31 -0700837 *error = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return NULL;
839}
840
Al Virofaf02012012-07-20 02:37:29 +0400841static int unix_mknod(const char *sun_path, umode_t mode, struct path *res)
842{
843 struct dentry *dentry;
844 struct path path;
845 int err = 0;
846 /*
847 * Get the parent directory, calculate the hash for last
848 * component.
849 */
850 dentry = kern_path_create(AT_FDCWD, sun_path, &path, 0);
851 err = PTR_ERR(dentry);
852 if (IS_ERR(dentry))
853 return err;
854
855 /*
856 * All right, let's create it.
857 */
858 err = security_path_mknod(&path, dentry, mode, 0);
859 if (!err) {
David Howellsee8ac4d2015-03-06 14:05:26 +0000860 err = vfs_mknod(d_inode(path.dentry), dentry, mode, 0);
Al Virofaf02012012-07-20 02:37:29 +0400861 if (!err) {
862 res->mnt = mntget(path.mnt);
863 res->dentry = dget(dentry);
864 }
865 }
866 done_path_create(&path, dentry);
867 return err;
868}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
871{
872 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900873 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 struct unix_sock *u = unix_sk(sk);
Jianjun Konge27dfce2008-11-01 21:38:31 -0700875 struct sockaddr_un *sunaddr = (struct sockaddr_un *)uaddr;
Al Virodae6ad82011-06-26 11:50:15 -0400876 char *sun_path = sunaddr->sun_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 int err;
Eric Dumazet95c96172012-04-15 05:58:06 +0000878 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 struct unix_address *addr;
880 struct hlist_head *list;
881
882 err = -EINVAL;
883 if (sunaddr->sun_family != AF_UNIX)
884 goto out;
885
Jianjun Konge27dfce2008-11-01 21:38:31 -0700886 if (addr_len == sizeof(short)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 err = unix_autobind(sock);
888 goto out;
889 }
890
891 err = unix_mkname(sunaddr, addr_len, &hash);
892 if (err < 0)
893 goto out;
894 addr_len = err;
895
Sasha Levin37ab4fa2013-12-13 10:54:22 -0500896 err = mutex_lock_interruptible(&u->readlock);
897 if (err)
898 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 err = -EINVAL;
901 if (u->addr)
902 goto out_up;
903
904 err = -ENOMEM;
905 addr = kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);
906 if (!addr)
907 goto out_up;
908
909 memcpy(addr->name, sunaddr, addr_len);
910 addr->len = addr_len;
911 addr->hash = hash ^ sk->sk_type;
912 atomic_set(&addr->refcnt, 1);
913
Al Virodae6ad82011-06-26 11:50:15 -0400914 if (sun_path[0]) {
Al Virofaf02012012-07-20 02:37:29 +0400915 struct path path;
916 umode_t mode = S_IFSOCK |
Al Viroce3b0f82009-03-29 19:08:22 -0400917 (SOCK_INODE(sock)->i_mode & ~current_umask());
Al Virofaf02012012-07-20 02:37:29 +0400918 err = unix_mknod(sun_path, mode, &path);
919 if (err) {
920 if (err == -EEXIST)
921 err = -EADDRINUSE;
922 unix_release_addr(addr);
923 goto out_up;
924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 addr->hash = UNIX_HASH_SIZE;
David Howellsa25b3762015-03-17 22:26:21 +0000926 hash = d_backing_inode(path.dentry)->i_ino & (UNIX_HASH_SIZE-1);
Al Virofaf02012012-07-20 02:37:29 +0400927 spin_lock(&unix_table_lock);
928 u->path = path;
929 list = &unix_socket_table[hash];
930 } else {
931 spin_lock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 err = -EADDRINUSE;
Denis V. Lunev097e66c2007-11-19 22:29:30 -0800933 if (__unix_find_socket_byname(net, sunaddr, addr_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 sk->sk_type, hash)) {
935 unix_release_addr(addr);
936 goto out_unlock;
937 }
938
939 list = &unix_socket_table[addr->hash];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
941
942 err = 0;
943 __unix_remove_socket(sk);
944 u->addr = addr;
945 __unix_insert_socket(list, sk);
946
947out_unlock:
David S. Millerfbe9cc42005-12-13 23:26:29 -0800948 spin_unlock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949out_up:
Ingo Molnar57b47a52006-03-20 22:35:41 -0800950 mutex_unlock(&u->readlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951out:
952 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
David S. Miller278a3de2007-05-31 15:19:20 -0700955static void unix_state_double_lock(struct sock *sk1, struct sock *sk2)
956{
957 if (unlikely(sk1 == sk2) || !sk2) {
958 unix_state_lock(sk1);
959 return;
960 }
961 if (sk1 < sk2) {
962 unix_state_lock(sk1);
963 unix_state_lock_nested(sk2);
964 } else {
965 unix_state_lock(sk2);
966 unix_state_lock_nested(sk1);
967 }
968}
969
970static void unix_state_double_unlock(struct sock *sk1, struct sock *sk2)
971{
972 if (unlikely(sk1 == sk2) || !sk2) {
973 unix_state_unlock(sk1);
974 return;
975 }
976 unix_state_unlock(sk1);
977 unix_state_unlock(sk2);
978}
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
981 int alen, int flags)
982{
983 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900984 struct net *net = sock_net(sk);
Jianjun Konge27dfce2008-11-01 21:38:31 -0700985 struct sockaddr_un *sunaddr = (struct sockaddr_un *)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 struct sock *other;
Eric Dumazet95c96172012-04-15 05:58:06 +0000987 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 int err;
989
990 if (addr->sa_family != AF_UNSPEC) {
991 err = unix_mkname(sunaddr, alen, &hash);
992 if (err < 0)
993 goto out;
994 alen = err;
995
996 if (test_bit(SOCK_PASSCRED, &sock->flags) &&
997 !unix_sk(sk)->addr && (err = unix_autobind(sock)) != 0)
998 goto out;
999
David S. Miller278a3de2007-05-31 15:19:20 -07001000restart:
Jianjun Konge27dfce2008-11-01 21:38:31 -07001001 other = unix_find_other(net, sunaddr, alen, sock->type, hash, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (!other)
1003 goto out;
1004
David S. Miller278a3de2007-05-31 15:19:20 -07001005 unix_state_double_lock(sk, other);
1006
1007 /* Apparently VFS overslept socket death. Retry. */
1008 if (sock_flag(other, SOCK_DEAD)) {
1009 unix_state_double_unlock(sk, other);
1010 sock_put(other);
1011 goto restart;
1012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 err = -EPERM;
1015 if (!unix_may_send(sk, other))
1016 goto out_unlock;
1017
1018 err = security_unix_may_send(sk->sk_socket, other->sk_socket);
1019 if (err)
1020 goto out_unlock;
1021
1022 } else {
1023 /*
1024 * 1003.1g breaking connected state with AF_UNSPEC
1025 */
1026 other = NULL;
David S. Miller278a3de2007-05-31 15:19:20 -07001027 unix_state_double_lock(sk, other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029
1030 /*
1031 * If it was connected, reconnect.
1032 */
1033 if (unix_peer(sk)) {
1034 struct sock *old_peer = unix_peer(sk);
Jianjun Konge27dfce2008-11-01 21:38:31 -07001035 unix_peer(sk) = other;
David S. Miller278a3de2007-05-31 15:19:20 -07001036 unix_state_double_unlock(sk, other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 if (other != old_peer)
1039 unix_dgram_disconnected(sk, old_peer);
1040 sock_put(old_peer);
1041 } else {
Jianjun Konge27dfce2008-11-01 21:38:31 -07001042 unix_peer(sk) = other;
David S. Miller278a3de2007-05-31 15:19:20 -07001043 unix_state_double_unlock(sk, other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09001045 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047out_unlock:
David S. Miller278a3de2007-05-31 15:19:20 -07001048 unix_state_double_unlock(sk, other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 sock_put(other);
1050out:
1051 return err;
1052}
1053
1054static long unix_wait_for_peer(struct sock *other, long timeo)
1055{
1056 struct unix_sock *u = unix_sk(other);
1057 int sched;
1058 DEFINE_WAIT(wait);
1059
1060 prepare_to_wait_exclusive(&u->peer_wait, &wait, TASK_INTERRUPTIBLE);
1061
1062 sched = !sock_flag(other, SOCK_DEAD) &&
1063 !(other->sk_shutdown & RCV_SHUTDOWN) &&
Rainer Weikusat3c734192008-06-17 22:28:05 -07001064 unix_recvq_full(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
David S. Miller1c92b4e2007-05-31 13:24:26 -07001066 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 if (sched)
1069 timeo = schedule_timeout(timeo);
1070
1071 finish_wait(&u->peer_wait, &wait);
1072 return timeo;
1073}
1074
1075static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
1076 int addr_len, int flags)
1077{
Jianjun Konge27dfce2008-11-01 21:38:31 -07001078 struct sockaddr_un *sunaddr = (struct sockaddr_un *)uaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001080 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 struct unix_sock *u = unix_sk(sk), *newu, *otheru;
1082 struct sock *newsk = NULL;
1083 struct sock *other = NULL;
1084 struct sk_buff *skb = NULL;
Eric Dumazet95c96172012-04-15 05:58:06 +00001085 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 int st;
1087 int err;
1088 long timeo;
1089
1090 err = unix_mkname(sunaddr, addr_len, &hash);
1091 if (err < 0)
1092 goto out;
1093 addr_len = err;
1094
Joe Perchesf64f9e72009-11-29 16:55:45 -08001095 if (test_bit(SOCK_PASSCRED, &sock->flags) && !u->addr &&
1096 (err = unix_autobind(sock)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 goto out;
1098
1099 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
1100
1101 /* First of all allocate resources.
1102 If we will make it after state is locked,
1103 we will have to recheck all again in any case.
1104 */
1105
1106 err = -ENOMEM;
1107
1108 /* create new sock for complete connection */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05001109 newsk = unix_create1(sock_net(sk), NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (newsk == NULL)
1111 goto out;
1112
1113 /* Allocate skb for sending to listening sock */
1114 skb = sock_wmalloc(newsk, 1, 0, GFP_KERNEL);
1115 if (skb == NULL)
1116 goto out;
1117
1118restart:
1119 /* Find listening sock. */
Denis V. Lunev097e66c2007-11-19 22:29:30 -08001120 other = unix_find_other(net, sunaddr, addr_len, sk->sk_type, hash, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (!other)
1122 goto out;
1123
1124 /* Latch state of peer */
David S. Miller1c92b4e2007-05-31 13:24:26 -07001125 unix_state_lock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 /* Apparently VFS overslept socket death. Retry. */
1128 if (sock_flag(other, SOCK_DEAD)) {
David S. Miller1c92b4e2007-05-31 13:24:26 -07001129 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 sock_put(other);
1131 goto restart;
1132 }
1133
1134 err = -ECONNREFUSED;
1135 if (other->sk_state != TCP_LISTEN)
1136 goto out_unlock;
Tomoki Sekiyama77238f22009-10-18 23:17:37 -07001137 if (other->sk_shutdown & RCV_SHUTDOWN)
1138 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Rainer Weikusat3c734192008-06-17 22:28:05 -07001140 if (unix_recvq_full(other)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 err = -EAGAIN;
1142 if (!timeo)
1143 goto out_unlock;
1144
1145 timeo = unix_wait_for_peer(other, timeo);
1146
1147 err = sock_intr_errno(timeo);
1148 if (signal_pending(current))
1149 goto out;
1150 sock_put(other);
1151 goto restart;
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09001152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 /* Latch our state.
1155
Daniel Balutae5537bf2011-03-14 15:25:33 -07001156 It is tricky place. We need to grab our state lock and cannot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 drop lock on peer. It is dangerous because deadlock is
1158 possible. Connect to self case and simultaneous
1159 attempt to connect are eliminated by checking socket
1160 state. other is TCP_LISTEN, if sk is TCP_LISTEN we
1161 check this before attempt to grab lock.
1162
1163 Well, and we have to recheck the state after socket locked.
1164 */
1165 st = sk->sk_state;
1166
1167 switch (st) {
1168 case TCP_CLOSE:
1169 /* This is ok... continue with connect */
1170 break;
1171 case TCP_ESTABLISHED:
1172 /* Socket is already connected */
1173 err = -EISCONN;
1174 goto out_unlock;
1175 default:
1176 err = -EINVAL;
1177 goto out_unlock;
1178 }
1179
David S. Miller1c92b4e2007-05-31 13:24:26 -07001180 unix_state_lock_nested(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 if (sk->sk_state != st) {
David S. Miller1c92b4e2007-05-31 13:24:26 -07001183 unix_state_unlock(sk);
1184 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 sock_put(other);
1186 goto restart;
1187 }
1188
David S. Miller3610cda2011-01-05 15:38:53 -08001189 err = security_unix_stream_connect(sk, other, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (err) {
David S. Miller1c92b4e2007-05-31 13:24:26 -07001191 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 goto out_unlock;
1193 }
1194
1195 /* The way is open! Fastly set all the necessary fields... */
1196
1197 sock_hold(sk);
1198 unix_peer(newsk) = sk;
1199 newsk->sk_state = TCP_ESTABLISHED;
1200 newsk->sk_type = sk->sk_type;
Eric W. Biederman109f6e32010-06-13 03:30:14 +00001201 init_peercred(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 newu = unix_sk(newsk);
Eric Dumazeteaefd112011-02-18 03:26:36 +00001203 RCU_INIT_POINTER(newsk->sk_wq, &newu->peer_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 otheru = unix_sk(other);
1205
1206 /* copy address information from listening to new sock*/
1207 if (otheru->addr) {
1208 atomic_inc(&otheru->addr->refcnt);
1209 newu->addr = otheru->addr;
1210 }
Al Viro40ffe672012-03-14 21:54:32 -04001211 if (otheru->path.dentry) {
1212 path_get(&otheru->path);
1213 newu->path = otheru->path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
1215
1216 /* Set credentials */
Eric W. Biederman109f6e32010-06-13 03:30:14 +00001217 copy_peercred(sk, other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 sock->state = SS_CONNECTED;
1220 sk->sk_state = TCP_ESTABLISHED;
Benjamin LaHaise830a1e52005-12-13 23:22:32 -08001221 sock_hold(newsk);
1222
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001223 smp_mb__after_atomic(); /* sock_hold() does an atomic_inc() */
Benjamin LaHaise830a1e52005-12-13 23:22:32 -08001224 unix_peer(sk) = newsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
David S. Miller1c92b4e2007-05-31 13:24:26 -07001226 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 /* take ten and and send info to listening sock */
1229 spin_lock(&other->sk_receive_queue.lock);
1230 __skb_queue_tail(&other->sk_receive_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 spin_unlock(&other->sk_receive_queue.lock);
David S. Miller1c92b4e2007-05-31 13:24:26 -07001232 unix_state_unlock(other);
David S. Miller676d2362014-04-11 16:15:36 -04001233 other->sk_data_ready(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 sock_put(other);
1235 return 0;
1236
1237out_unlock:
1238 if (other)
David S. Miller1c92b4e2007-05-31 13:24:26 -07001239 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241out:
Wei Yongjun40d44442009-02-25 00:32:45 +00001242 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (newsk)
1244 unix_release_sock(newsk, 0);
1245 if (other)
1246 sock_put(other);
1247 return err;
1248}
1249
1250static int unix_socketpair(struct socket *socka, struct socket *sockb)
1251{
Jianjun Konge27dfce2008-11-01 21:38:31 -07001252 struct sock *ska = socka->sk, *skb = sockb->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 /* Join our sockets back to back */
1255 sock_hold(ska);
1256 sock_hold(skb);
Jianjun Konge27dfce2008-11-01 21:38:31 -07001257 unix_peer(ska) = skb;
1258 unix_peer(skb) = ska;
Eric W. Biederman109f6e32010-06-13 03:30:14 +00001259 init_peercred(ska);
1260 init_peercred(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 if (ska->sk_type != SOCK_DGRAM) {
1263 ska->sk_state = TCP_ESTABLISHED;
1264 skb->sk_state = TCP_ESTABLISHED;
1265 socka->state = SS_CONNECTED;
1266 sockb->state = SS_CONNECTED;
1267 }
1268 return 0;
1269}
1270
Daniel Borkmann90c6bd32013-10-17 22:51:31 +02001271static void unix_sock_inherit_flags(const struct socket *old,
1272 struct socket *new)
1273{
1274 if (test_bit(SOCK_PASSCRED, &old->flags))
1275 set_bit(SOCK_PASSCRED, &new->flags);
1276 if (test_bit(SOCK_PASSSEC, &old->flags))
1277 set_bit(SOCK_PASSSEC, &new->flags);
1278}
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280static int unix_accept(struct socket *sock, struct socket *newsock, int flags)
1281{
1282 struct sock *sk = sock->sk;
1283 struct sock *tsk;
1284 struct sk_buff *skb;
1285 int err;
1286
1287 err = -EOPNOTSUPP;
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001288 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 goto out;
1290
1291 err = -EINVAL;
1292 if (sk->sk_state != TCP_LISTEN)
1293 goto out;
1294
1295 /* If socket state is TCP_LISTEN it cannot change (for now...),
1296 * so that no locks are necessary.
1297 */
1298
1299 skb = skb_recv_datagram(sk, 0, flags&O_NONBLOCK, &err);
1300 if (!skb) {
1301 /* This means receive shutdown. */
1302 if (err == 0)
1303 err = -EINVAL;
1304 goto out;
1305 }
1306
1307 tsk = skb->sk;
1308 skb_free_datagram(sk, skb);
1309 wake_up_interruptible(&unix_sk(sk)->peer_wait);
1310
1311 /* attach accepted sock to socket */
David S. Miller1c92b4e2007-05-31 13:24:26 -07001312 unix_state_lock(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 newsock->state = SS_CONNECTED;
Daniel Borkmann90c6bd32013-10-17 22:51:31 +02001314 unix_sock_inherit_flags(sock, newsock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 sock_graft(tsk, newsock);
David S. Miller1c92b4e2007-05-31 13:24:26 -07001316 unix_state_unlock(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 return 0;
1318
1319out:
1320 return err;
1321}
1322
1323
1324static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer)
1325{
1326 struct sock *sk = sock->sk;
1327 struct unix_sock *u;
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00001328 DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 int err = 0;
1330
1331 if (peer) {
1332 sk = unix_peer_get(sk);
1333
1334 err = -ENOTCONN;
1335 if (!sk)
1336 goto out;
1337 err = 0;
1338 } else {
1339 sock_hold(sk);
1340 }
1341
1342 u = unix_sk(sk);
David S. Miller1c92b4e2007-05-31 13:24:26 -07001343 unix_state_lock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (!u->addr) {
1345 sunaddr->sun_family = AF_UNIX;
1346 sunaddr->sun_path[0] = 0;
1347 *uaddr_len = sizeof(short);
1348 } else {
1349 struct unix_address *addr = u->addr;
1350
1351 *uaddr_len = addr->len;
1352 memcpy(sunaddr, addr->name, *uaddr_len);
1353 }
David S. Miller1c92b4e2007-05-31 13:24:26 -07001354 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 sock_put(sk);
1356out:
1357 return err;
1358}
1359
1360static void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
1361{
1362 int i;
1363
1364 scm->fp = UNIXCB(skb).fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 UNIXCB(skb).fp = NULL;
1366
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001367 for (i = scm->fp->count-1; i >= 0; i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 unix_notinflight(scm->fp->fp[i]);
1369}
1370
Eric W. Biederman7361c362010-06-13 03:34:33 +00001371static void unix_destruct_scm(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
1373 struct scm_cookie scm;
1374 memset(&scm, 0, sizeof(scm));
Eric W. Biederman7361c362010-06-13 03:34:33 +00001375 scm.pid = UNIXCB(skb).pid;
Eric W. Biederman7361c362010-06-13 03:34:33 +00001376 if (UNIXCB(skb).fp)
1377 unix_detach_fds(&scm, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 /* Alas, it calls VFS */
1380 /* So fscking what? fput() had been SMP-safe since the last Summer */
1381 scm_destroy(&scm);
1382 sock_wfree(skb);
1383}
1384
Eric Dumazet25888e32010-11-25 04:11:39 +00001385#define MAX_RECURSION_LEVEL 4
1386
Miklos Szeredi62093442008-11-09 15:23:57 +01001387static int unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
1389 int i;
Eric Dumazet25888e32010-11-25 04:11:39 +00001390 unsigned char max_level = 0;
1391 int unix_sock_count = 0;
1392
1393 for (i = scm->fp->count - 1; i >= 0; i--) {
1394 struct sock *sk = unix_get_socket(scm->fp->fp[i]);
1395
1396 if (sk) {
1397 unix_sock_count++;
1398 max_level = max(max_level,
1399 unix_sk(sk)->recursion_level);
1400 }
1401 }
1402 if (unlikely(max_level > MAX_RECURSION_LEVEL))
1403 return -ETOOMANYREFS;
Miklos Szeredi62093442008-11-09 15:23:57 +01001404
1405 /*
1406 * Need to duplicate file references for the sake of garbage
1407 * collection. Otherwise a socket in the fps might become a
1408 * candidate for GC while the skb is not yet queued.
1409 */
1410 UNIXCB(skb).fp = scm_fp_dup(scm->fp);
1411 if (!UNIXCB(skb).fp)
1412 return -ENOMEM;
1413
Eric Dumazet25888e32010-11-25 04:11:39 +00001414 if (unix_sock_count) {
1415 for (i = scm->fp->count - 1; i >= 0; i--)
1416 unix_inflight(scm->fp->fp[i]);
1417 }
1418 return max_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419}
1420
David S. Millerf78a5fd2011-09-16 19:34:00 -04001421static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool send_fds)
Eric W. Biederman7361c362010-06-13 03:34:33 +00001422{
1423 int err = 0;
Eric Dumazet16e57262011-09-19 05:52:27 +00001424
David S. Millerf78a5fd2011-09-16 19:34:00 -04001425 UNIXCB(skb).pid = get_pid(scm->pid);
Eric W. Biederman6b0ee8c02013-04-03 17:28:16 +00001426 UNIXCB(skb).uid = scm->creds.uid;
1427 UNIXCB(skb).gid = scm->creds.gid;
Eric W. Biederman7361c362010-06-13 03:34:33 +00001428 UNIXCB(skb).fp = NULL;
Stephen Smalley37a9a8d2015-06-10 08:44:59 -04001429 unix_get_secdata(scm, skb);
Eric W. Biederman7361c362010-06-13 03:34:33 +00001430 if (scm->fp && send_fds)
1431 err = unix_attach_fds(scm, skb);
1432
1433 skb->destructor = unix_destruct_scm;
1434 return err;
1435}
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437/*
Eric Dumazet16e57262011-09-19 05:52:27 +00001438 * Some apps rely on write() giving SCM_CREDENTIALS
1439 * We include credentials if source or destination socket
1440 * asserted SOCK_PASSCRED.
1441 */
1442static void maybe_add_creds(struct sk_buff *skb, const struct socket *sock,
1443 const struct sock *other)
1444{
Eric W. Biederman6b0ee8c02013-04-03 17:28:16 +00001445 if (UNIXCB(skb).pid)
Eric Dumazet16e57262011-09-19 05:52:27 +00001446 return;
1447 if (test_bit(SOCK_PASSCRED, &sock->flags) ||
Eric W. Biederman25da0e32013-04-03 16:13:35 +00001448 !other->sk_socket ||
1449 test_bit(SOCK_PASSCRED, &other->sk_socket->flags)) {
Eric Dumazet16e57262011-09-19 05:52:27 +00001450 UNIXCB(skb).pid = get_pid(task_tgid(current));
David S. Miller6e0895c2013-04-22 20:32:51 -04001451 current_uid_gid(&UNIXCB(skb).uid, &UNIXCB(skb).gid);
Eric Dumazet16e57262011-09-19 05:52:27 +00001452 }
1453}
1454
1455/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 * Send AF_UNIX data.
1457 */
1458
Ying Xue1b784142015-03-02 15:37:48 +08001459static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
1460 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001463 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 struct unix_sock *u = unix_sk(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001465 DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 struct sock *other = NULL;
1467 int namelen = 0; /* fake GCC */
1468 int err;
Eric Dumazet95c96172012-04-15 05:58:06 +00001469 unsigned int hash;
David S. Millerf78a5fd2011-09-16 19:34:00 -04001470 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 long timeo;
Christoph Hellwig7cc05662015-01-28 18:04:53 +01001472 struct scm_cookie scm;
Eric Dumazet25888e32010-11-25 04:11:39 +00001473 int max_level;
Eric Dumazeteb6a2482012-04-03 05:28:28 +00001474 int data_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
dann frazier5f23b732008-11-26 15:32:27 -08001476 wait_for_unix_gc();
Christoph Hellwig7cc05662015-01-28 18:04:53 +01001477 err = scm_send(sock, msg, &scm, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 if (err < 0)
1479 return err;
1480
1481 err = -EOPNOTSUPP;
1482 if (msg->msg_flags&MSG_OOB)
1483 goto out;
1484
1485 if (msg->msg_namelen) {
1486 err = unix_mkname(sunaddr, msg->msg_namelen, &hash);
1487 if (err < 0)
1488 goto out;
1489 namelen = err;
1490 } else {
1491 sunaddr = NULL;
1492 err = -ENOTCONN;
1493 other = unix_peer_get(sk);
1494 if (!other)
1495 goto out;
1496 }
1497
Joe Perchesf64f9e72009-11-29 16:55:45 -08001498 if (test_bit(SOCK_PASSCRED, &sock->flags) && !u->addr
1499 && (err = unix_autobind(sock)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 goto out;
1501
1502 err = -EMSGSIZE;
1503 if (len > sk->sk_sndbuf - 32)
1504 goto out;
1505
Kirill Tkhai31ff6aa2014-05-15 19:56:28 +04001506 if (len > SKB_MAX_ALLOC) {
Eric Dumazeteb6a2482012-04-03 05:28:28 +00001507 data_len = min_t(size_t,
1508 len - SKB_MAX_ALLOC,
1509 MAX_SKB_FRAGS * PAGE_SIZE);
Kirill Tkhai31ff6aa2014-05-15 19:56:28 +04001510 data_len = PAGE_ALIGN(data_len);
1511
1512 BUILD_BUG_ON(SKB_MAX_ALLOC < PAGE_SIZE);
1513 }
Eric Dumazeteb6a2482012-04-03 05:28:28 +00001514
1515 skb = sock_alloc_send_pskb(sk, len - data_len, data_len,
Eric Dumazet28d64272013-08-08 14:38:47 -07001516 msg->msg_flags & MSG_DONTWAIT, &err,
1517 PAGE_ALLOC_COSTLY_ORDER);
Jianjun Konge27dfce2008-11-01 21:38:31 -07001518 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 goto out;
1520
Christoph Hellwig7cc05662015-01-28 18:04:53 +01001521 err = unix_scm_to_skb(&scm, skb, true);
Eric Dumazet25888e32010-11-25 04:11:39 +00001522 if (err < 0)
Eric W. Biederman7361c362010-06-13 03:34:33 +00001523 goto out_free;
Eric Dumazet25888e32010-11-25 04:11:39 +00001524 max_level = err + 1;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07001525
Eric Dumazeteb6a2482012-04-03 05:28:28 +00001526 skb_put(skb, len - data_len);
1527 skb->data_len = data_len;
1528 skb->len = len;
Al Viroc0371da2014-11-24 10:42:55 -05001529 err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 if (err)
1531 goto out_free;
1532
1533 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1534
1535restart:
1536 if (!other) {
1537 err = -ECONNRESET;
1538 if (sunaddr == NULL)
1539 goto out_free;
1540
Denis V. Lunev097e66c2007-11-19 22:29:30 -08001541 other = unix_find_other(net, sunaddr, namelen, sk->sk_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 hash, &err);
Jianjun Konge27dfce2008-11-01 21:38:31 -07001543 if (other == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 goto out_free;
1545 }
1546
Alban Crequyd6ae3ba2011-01-18 06:39:15 +00001547 if (sk_filter(other, skb) < 0) {
1548 /* Toss the packet but do not return any error to the sender */
1549 err = len;
1550 goto out_free;
1551 }
1552
David S. Miller1c92b4e2007-05-31 13:24:26 -07001553 unix_state_lock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 err = -EPERM;
1555 if (!unix_may_send(sk, other))
1556 goto out_unlock;
1557
1558 if (sock_flag(other, SOCK_DEAD)) {
1559 /*
1560 * Check with 1003.1g - what should
1561 * datagram error
1562 */
David S. Miller1c92b4e2007-05-31 13:24:26 -07001563 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 sock_put(other);
1565
1566 err = 0;
David S. Miller1c92b4e2007-05-31 13:24:26 -07001567 unix_state_lock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (unix_peer(sk) == other) {
Jianjun Konge27dfce2008-11-01 21:38:31 -07001569 unix_peer(sk) = NULL;
David S. Miller1c92b4e2007-05-31 13:24:26 -07001570 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 unix_dgram_disconnected(sk, other);
1573 sock_put(other);
1574 err = -ECONNREFUSED;
1575 } else {
David S. Miller1c92b4e2007-05-31 13:24:26 -07001576 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 }
1578
1579 other = NULL;
1580 if (err)
1581 goto out_free;
1582 goto restart;
1583 }
1584
1585 err = -EPIPE;
1586 if (other->sk_shutdown & RCV_SHUTDOWN)
1587 goto out_unlock;
1588
1589 if (sk->sk_type != SOCK_SEQPACKET) {
1590 err = security_unix_may_send(sk->sk_socket, other->sk_socket);
1591 if (err)
1592 goto out_unlock;
1593 }
1594
Rainer Weikusat3c734192008-06-17 22:28:05 -07001595 if (unix_peer(other) != sk && unix_recvq_full(other)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 if (!timeo) {
1597 err = -EAGAIN;
1598 goto out_unlock;
1599 }
1600
1601 timeo = unix_wait_for_peer(other, timeo);
1602
1603 err = sock_intr_errno(timeo);
1604 if (signal_pending(current))
1605 goto out_free;
1606
1607 goto restart;
1608 }
1609
Alban Crequy3f661162010-10-04 08:48:28 +00001610 if (sock_flag(other, SOCK_RCVTSTAMP))
1611 __net_timestamp(skb);
Eric Dumazet16e57262011-09-19 05:52:27 +00001612 maybe_add_creds(skb, sock, other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 skb_queue_tail(&other->sk_receive_queue, skb);
Eric Dumazet25888e32010-11-25 04:11:39 +00001614 if (max_level > unix_sk(other)->recursion_level)
1615 unix_sk(other)->recursion_level = max_level;
David S. Miller1c92b4e2007-05-31 13:24:26 -07001616 unix_state_unlock(other);
David S. Miller676d2362014-04-11 16:15:36 -04001617 other->sk_data_ready(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 sock_put(other);
Christoph Hellwig7cc05662015-01-28 18:04:53 +01001619 scm_destroy(&scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 return len;
1621
1622out_unlock:
David S. Miller1c92b4e2007-05-31 13:24:26 -07001623 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624out_free:
1625 kfree_skb(skb);
1626out:
1627 if (other)
1628 sock_put(other);
Christoph Hellwig7cc05662015-01-28 18:04:53 +01001629 scm_destroy(&scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 return err;
1631}
1632
Eric Dumazete370a722013-08-08 14:37:32 -07001633/* We use paged skbs for stream sockets, and limit occupancy to 32768
1634 * bytes, and a minimun of a full page.
1635 */
1636#define UNIX_SKB_FRAGS_SZ (PAGE_SIZE << get_order(32768))
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09001637
Ying Xue1b784142015-03-02 15:37:48 +08001638static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg,
1639 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 struct sock *sk = sock->sk;
1642 struct sock *other = NULL;
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001643 int err, size;
David S. Millerf78a5fd2011-09-16 19:34:00 -04001644 struct sk_buff *skb;
Jianjun Konge27dfce2008-11-01 21:38:31 -07001645 int sent = 0;
Christoph Hellwig7cc05662015-01-28 18:04:53 +01001646 struct scm_cookie scm;
Miklos Szeredi8ba69ba2009-09-11 11:31:45 -07001647 bool fds_sent = false;
Eric Dumazet25888e32010-11-25 04:11:39 +00001648 int max_level;
Eric Dumazete370a722013-08-08 14:37:32 -07001649 int data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
dann frazier5f23b732008-11-26 15:32:27 -08001651 wait_for_unix_gc();
Christoph Hellwig7cc05662015-01-28 18:04:53 +01001652 err = scm_send(sock, msg, &scm, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 if (err < 0)
1654 return err;
1655
1656 err = -EOPNOTSUPP;
1657 if (msg->msg_flags&MSG_OOB)
1658 goto out_err;
1659
1660 if (msg->msg_namelen) {
1661 err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP;
1662 goto out_err;
1663 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 err = -ENOTCONN;
Benjamin LaHaise830a1e52005-12-13 23:22:32 -08001665 other = unix_peer(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 if (!other)
1667 goto out_err;
1668 }
1669
1670 if (sk->sk_shutdown & SEND_SHUTDOWN)
1671 goto pipe_err;
1672
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001673 while (sent < len) {
Eric Dumazete370a722013-08-08 14:37:32 -07001674 size = len - sent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 /* Keep two messages in the pipe so it schedules better */
Eric Dumazete370a722013-08-08 14:37:32 -07001677 size = min_t(int, size, (sk->sk_sndbuf >> 1) - 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Eric Dumazete370a722013-08-08 14:37:32 -07001679 /* allow fallback to order-0 allocations */
1680 size = min_t(int, size, SKB_MAX_HEAD(0) + UNIX_SKB_FRAGS_SZ);
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09001681
Eric Dumazete370a722013-08-08 14:37:32 -07001682 data_len = max_t(int, 0, size - SKB_MAX_HEAD(0));
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09001683
Kirill Tkhai31ff6aa2014-05-15 19:56:28 +04001684 data_len = min_t(size_t, size, PAGE_ALIGN(data_len));
1685
Eric Dumazete370a722013-08-08 14:37:32 -07001686 skb = sock_alloc_send_pskb(sk, size - data_len, data_len,
Eric Dumazet28d64272013-08-08 14:38:47 -07001687 msg->msg_flags & MSG_DONTWAIT, &err,
1688 get_order(UNIX_SKB_FRAGS_SZ));
Eric Dumazete370a722013-08-08 14:37:32 -07001689 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 goto out_err;
1691
David S. Millerf78a5fd2011-09-16 19:34:00 -04001692 /* Only send the fds in the first buffer */
Christoph Hellwig7cc05662015-01-28 18:04:53 +01001693 err = unix_scm_to_skb(&scm, skb, !fds_sent);
Eric Dumazet25888e32010-11-25 04:11:39 +00001694 if (err < 0) {
Eric W. Biederman7361c362010-06-13 03:34:33 +00001695 kfree_skb(skb);
David S. Millerf78a5fd2011-09-16 19:34:00 -04001696 goto out_err;
Miklos Szeredi62093442008-11-09 15:23:57 +01001697 }
Eric Dumazet25888e32010-11-25 04:11:39 +00001698 max_level = err + 1;
Eric W. Biederman7361c362010-06-13 03:34:33 +00001699 fds_sent = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Eric Dumazete370a722013-08-08 14:37:32 -07001701 skb_put(skb, size - data_len);
1702 skb->data_len = data_len;
1703 skb->len = size;
Al Viroc0371da2014-11-24 10:42:55 -05001704 err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, size);
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001705 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 kfree_skb(skb);
David S. Millerf78a5fd2011-09-16 19:34:00 -04001707 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
1709
David S. Miller1c92b4e2007-05-31 13:24:26 -07001710 unix_state_lock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 if (sock_flag(other, SOCK_DEAD) ||
1713 (other->sk_shutdown & RCV_SHUTDOWN))
1714 goto pipe_err_free;
1715
Eric Dumazet16e57262011-09-19 05:52:27 +00001716 maybe_add_creds(skb, sock, other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 skb_queue_tail(&other->sk_receive_queue, skb);
Eric Dumazet25888e32010-11-25 04:11:39 +00001718 if (max_level > unix_sk(other)->recursion_level)
1719 unix_sk(other)->recursion_level = max_level;
David S. Miller1c92b4e2007-05-31 13:24:26 -07001720 unix_state_unlock(other);
David S. Miller676d2362014-04-11 16:15:36 -04001721 other->sk_data_ready(other);
Jianjun Konge27dfce2008-11-01 21:38:31 -07001722 sent += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Christoph Hellwig7cc05662015-01-28 18:04:53 +01001725 scm_destroy(&scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 return sent;
1728
1729pipe_err_free:
David S. Miller1c92b4e2007-05-31 13:24:26 -07001730 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 kfree_skb(skb);
1732pipe_err:
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001733 if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL))
1734 send_sig(SIGPIPE, current, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 err = -EPIPE;
1736out_err:
Christoph Hellwig7cc05662015-01-28 18:04:53 +01001737 scm_destroy(&scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 return sent ? : err;
1739}
1740
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02001741static ssize_t unix_stream_sendpage(struct socket *socket, struct page *page,
1742 int offset, size_t size, int flags)
1743{
1744 int err = 0;
1745 bool send_sigpipe = true;
1746 struct sock *other, *sk = socket->sk;
1747 struct sk_buff *skb, *newskb = NULL, *tail = NULL;
1748
1749 if (flags & MSG_OOB)
1750 return -EOPNOTSUPP;
1751
1752 other = unix_peer(sk);
1753 if (!other || sk->sk_state != TCP_ESTABLISHED)
1754 return -ENOTCONN;
1755
1756 if (false) {
1757alloc_skb:
1758 unix_state_unlock(other);
1759 mutex_unlock(&unix_sk(other)->readlock);
1760 newskb = sock_alloc_send_pskb(sk, 0, 0, flags & MSG_DONTWAIT,
1761 &err, 0);
1762 if (!newskb)
1763 return err;
1764 }
1765
1766 /* we must acquire readlock as we modify already present
1767 * skbs in the sk_receive_queue and mess with skb->len
1768 */
1769 err = mutex_lock_interruptible(&unix_sk(other)->readlock);
1770 if (err) {
1771 err = flags & MSG_DONTWAIT ? -EAGAIN : -ERESTARTSYS;
1772 send_sigpipe = false;
1773 goto err;
1774 }
1775
1776 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1777 err = -EPIPE;
1778 goto err_unlock;
1779 }
1780
1781 unix_state_lock(other);
1782
1783 if (sock_flag(other, SOCK_DEAD) ||
1784 other->sk_shutdown & RCV_SHUTDOWN) {
1785 err = -EPIPE;
1786 goto err_state_unlock;
1787 }
1788
1789 skb = skb_peek_tail(&other->sk_receive_queue);
1790 if (tail && tail == skb) {
1791 skb = newskb;
1792 } else if (!skb) {
1793 if (newskb)
1794 skb = newskb;
1795 else
1796 goto alloc_skb;
1797 } else if (newskb) {
1798 /* this is fast path, we don't necessarily need to
1799 * call to kfree_skb even though with newskb == NULL
1800 * this - does no harm
1801 */
1802 consume_skb(newskb);
Hannes Frederic Sowa8844f972015-11-16 16:25:56 +01001803 newskb = NULL;
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02001804 }
1805
1806 if (skb_append_pagefrags(skb, page, offset, size)) {
1807 tail = skb;
1808 goto alloc_skb;
1809 }
1810
1811 skb->len += size;
1812 skb->data_len += size;
1813 skb->truesize += size;
1814 atomic_add(size, &sk->sk_wmem_alloc);
1815
Hannes Frederic Sowaa3a116e2015-11-17 15:10:59 +01001816 if (newskb) {
1817 spin_lock(&other->sk_receive_queue.lock);
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02001818 __skb_queue_tail(&other->sk_receive_queue, newskb);
Hannes Frederic Sowaa3a116e2015-11-17 15:10:59 +01001819 spin_unlock(&other->sk_receive_queue.lock);
1820 }
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02001821
1822 unix_state_unlock(other);
1823 mutex_unlock(&unix_sk(other)->readlock);
1824
1825 other->sk_data_ready(other);
1826
1827 return size;
1828
1829err_state_unlock:
1830 unix_state_unlock(other);
1831err_unlock:
1832 mutex_unlock(&unix_sk(other)->readlock);
1833err:
1834 kfree_skb(newskb);
1835 if (send_sigpipe && !(flags & MSG_NOSIGNAL))
1836 send_sig(SIGPIPE, current, 0);
1837 return err;
1838}
1839
Ying Xue1b784142015-03-02 15:37:48 +08001840static int unix_seqpacket_sendmsg(struct socket *sock, struct msghdr *msg,
1841 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
1843 int err;
1844 struct sock *sk = sock->sk;
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09001845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 err = sock_error(sk);
1847 if (err)
1848 return err;
1849
1850 if (sk->sk_state != TCP_ESTABLISHED)
1851 return -ENOTCONN;
1852
1853 if (msg->msg_namelen)
1854 msg->msg_namelen = 0;
1855
Ying Xue1b784142015-03-02 15:37:48 +08001856 return unix_dgram_sendmsg(sock, msg, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857}
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09001858
Ying Xue1b784142015-03-02 15:37:48 +08001859static int unix_seqpacket_recvmsg(struct socket *sock, struct msghdr *msg,
1860 size_t size, int flags)
Eric W. Biedermana05d2ad2011-04-24 01:54:57 +00001861{
1862 struct sock *sk = sock->sk;
1863
1864 if (sk->sk_state != TCP_ESTABLISHED)
1865 return -ENOTCONN;
1866
Ying Xue1b784142015-03-02 15:37:48 +08001867 return unix_dgram_recvmsg(sock, msg, size, flags);
Eric W. Biedermana05d2ad2011-04-24 01:54:57 +00001868}
1869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870static void unix_copy_addr(struct msghdr *msg, struct sock *sk)
1871{
1872 struct unix_sock *u = unix_sk(sk);
1873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (u->addr) {
1875 msg->msg_namelen = u->addr->len;
1876 memcpy(msg->msg_name, u->addr->name, u->addr->len);
1877 }
1878}
1879
Ying Xue1b784142015-03-02 15:37:48 +08001880static int unix_dgram_recvmsg(struct socket *sock, struct msghdr *msg,
1881 size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
Christoph Hellwig7cc05662015-01-28 18:04:53 +01001883 struct scm_cookie scm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 struct sock *sk = sock->sk;
1885 struct unix_sock *u = unix_sk(sk);
1886 int noblock = flags & MSG_DONTWAIT;
1887 struct sk_buff *skb;
1888 int err;
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +00001889 int peeked, skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
1891 err = -EOPNOTSUPP;
1892 if (flags&MSG_OOB)
1893 goto out;
1894
Rainer Weikusatb3ca9b02011-02-28 04:50:55 +00001895 err = mutex_lock_interruptible(&u->readlock);
Eric Dumazetde144392014-03-25 18:42:27 -07001896 if (unlikely(err)) {
1897 /* recvmsg() in non blocking mode is supposed to return -EAGAIN
1898 * sk_rcvtimeo is not honored by mutex_lock_interruptible()
1899 */
1900 err = noblock ? -EAGAIN : -ERESTARTSYS;
Rainer Weikusatb3ca9b02011-02-28 04:50:55 +00001901 goto out;
1902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +00001904 skip = sk_peek_offset(sk, flags);
1905
1906 skb = __skb_recv_datagram(sk, flags, &peeked, &skip, &err);
Florian Zumbiehl0a112252007-11-29 23:19:23 +11001907 if (!skb) {
1908 unix_state_lock(sk);
1909 /* Signal EOF on disconnected non-blocking SEQPACKET socket. */
1910 if (sk->sk_type == SOCK_SEQPACKET && err == -EAGAIN &&
1911 (sk->sk_shutdown & RCV_SHUTDOWN))
1912 err = 0;
1913 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 goto out_unlock;
Florian Zumbiehl0a112252007-11-29 23:19:23 +11001915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Eric Dumazet67426b72010-10-29 20:44:44 +00001917 wake_up_interruptible_sync_poll(&u->peer_wait,
1918 POLLOUT | POLLWRNORM | POLLWRBAND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920 if (msg->msg_name)
1921 unix_copy_addr(msg, skb->sk);
1922
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +00001923 if (size > skb->len - skip)
1924 size = skb->len - skip;
1925 else if (size < skb->len - skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 msg->msg_flags |= MSG_TRUNC;
1927
David S. Miller51f3d022014-11-05 16:46:40 -05001928 err = skb_copy_datagram_msg(skb, skip, msg, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 if (err)
1930 goto out_free;
1931
Alban Crequy3f661162010-10-04 08:48:28 +00001932 if (sock_flag(sk, SOCK_RCVTSTAMP))
1933 __sock_recv_timestamp(msg, sk, skb);
1934
Christoph Hellwig7cc05662015-01-28 18:04:53 +01001935 memset(&scm, 0, sizeof(scm));
1936
1937 scm_set_cred(&scm, UNIXCB(skb).pid, UNIXCB(skb).uid, UNIXCB(skb).gid);
1938 unix_set_secdata(&scm, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001940 if (!(flags & MSG_PEEK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (UNIXCB(skb).fp)
Christoph Hellwig7cc05662015-01-28 18:04:53 +01001942 unix_detach_fds(&scm, skb);
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +00001943
1944 sk_peek_offset_bwd(sk, skb->len);
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001945 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 /* It is questionable: on PEEK we could:
1947 - do not return fds - good, but too simple 8)
1948 - return fds, and do not return them on read (old strategy,
1949 apparently wrong)
1950 - clone fds (I chose it for now, it is the most universal
1951 solution)
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09001952
1953 POSIX 1003.1g does not actually define this clearly
1954 at all. POSIX 1003.1g doesn't define a lot of things
1955 clearly however!
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 */
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +00001958
1959 sk_peek_offset_fwd(sk, size);
1960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 if (UNIXCB(skb).fp)
Christoph Hellwig7cc05662015-01-28 18:04:53 +01001962 scm.fp = scm_fp_dup(UNIXCB(skb).fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 }
Eric Dumazet9f6f9af2012-02-21 23:24:55 +00001964 err = (flags & MSG_TRUNC) ? skb->len - skip : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Christoph Hellwig7cc05662015-01-28 18:04:53 +01001966 scm_recv(sock, msg, &scm, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968out_free:
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001969 skb_free_datagram(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970out_unlock:
Ingo Molnar57b47a52006-03-20 22:35:41 -08001971 mutex_unlock(&u->readlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972out:
1973 return err;
1974}
1975
1976/*
Benjamin Poirier79f632c2013-04-29 11:42:14 +00001977 * Sleep until more data has arrived. But check for races..
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 */
Benjamin Poirier79f632c2013-04-29 11:42:14 +00001979static long unix_stream_data_wait(struct sock *sk, long timeo,
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02001980 struct sk_buff *last, unsigned int last_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02001982 struct sk_buff *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 DEFINE_WAIT(wait);
1984
David S. Miller1c92b4e2007-05-31 13:24:26 -07001985 unix_state_lock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +00001988 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02001990 tail = skb_peek_tail(&sk->sk_receive_queue);
1991 if (tail != last ||
1992 (tail && tail->len != last_len) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 sk->sk_err ||
1994 (sk->sk_shutdown & RCV_SHUTDOWN) ||
1995 signal_pending(current) ||
1996 !timeo)
1997 break;
1998
1999 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
David S. Miller1c92b4e2007-05-31 13:24:26 -07002000 unix_state_unlock(sk);
Colin Cross2b15af62013-05-06 23:50:21 +00002001 timeo = freezable_schedule_timeout(timeo);
David S. Miller1c92b4e2007-05-31 13:24:26 -07002002 unix_state_lock(sk);
Mark Salyzynb48732e2015-05-26 08:22:19 -07002003
2004 if (sock_flag(sk, SOCK_DEAD))
2005 break;
2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
2008 }
2009
Eric Dumazetaa395142010-04-20 13:03:51 +00002010 finish_wait(sk_sleep(sk), &wait);
David S. Miller1c92b4e2007-05-31 13:24:26 -07002011 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 return timeo;
2013}
2014
Eric Dumazete370a722013-08-08 14:37:32 -07002015static unsigned int unix_skb_len(const struct sk_buff *skb)
2016{
2017 return skb->len - UNIXCB(skb).consumed;
2018}
2019
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002020struct unix_stream_read_state {
2021 int (*recv_actor)(struct sk_buff *, int, int,
2022 struct unix_stream_read_state *);
2023 struct socket *socket;
2024 struct msghdr *msg;
2025 struct pipe_inode_info *pipe;
2026 size_t size;
2027 int flags;
2028 unsigned int splice_flags;
2029};
2030
2031static int unix_stream_read_generic(struct unix_stream_read_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002033 struct scm_cookie scm;
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002034 struct socket *sock = state->socket;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 struct sock *sk = sock->sk;
2036 struct unix_sock *u = unix_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 int copied = 0;
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002038 int flags = state->flags;
Eric Dumazetde144392014-03-25 18:42:27 -07002039 int noblock = flags & MSG_DONTWAIT;
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002040 bool check_creds = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 int target;
2042 int err = 0;
2043 long timeo;
Pavel Emelyanovfc0d7532012-02-21 07:32:06 +00002044 int skip;
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002045 size_t size = state->size;
2046 unsigned int last_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 err = -EINVAL;
2049 if (sk->sk_state != TCP_ESTABLISHED)
2050 goto out;
2051
2052 err = -EOPNOTSUPP;
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002053 if (flags & MSG_OOB)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 goto out;
2055
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002056 target = sock_rcvlowat(sk, flags & MSG_WAITALL, size);
Eric Dumazetde144392014-03-25 18:42:27 -07002057 timeo = sock_rcvtimeo(sk, noblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002059 memset(&scm, 0, sizeof(scm));
2060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 /* Lock the socket to prevent queue disordering
2062 * while sleeps in memcpy_tomsg
2063 */
Rainer Weikusatb3ca9b02011-02-28 04:50:55 +00002064 err = mutex_lock_interruptible(&u->readlock);
Eric Dumazetde144392014-03-25 18:42:27 -07002065 if (unlikely(err)) {
2066 /* recvmsg() in non blocking mode is supposed to return -EAGAIN
2067 * sk_rcvtimeo is not honored by mutex_lock_interruptible()
2068 */
2069 err = noblock ? -EAGAIN : -ERESTARTSYS;
Rainer Weikusatb3ca9b02011-02-28 04:50:55 +00002070 goto out;
2071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Andrey Vagine9193d62015-10-02 00:05:36 +03002073 if (flags & MSG_PEEK)
2074 skip = sk_peek_offset(sk, flags);
2075 else
2076 skip = 0;
2077
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002078 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 int chunk;
Hannes Frederic Sowa73ed5d22015-11-10 16:23:15 +01002080 bool drop_skb;
Benjamin Poirier79f632c2013-04-29 11:42:14 +00002081 struct sk_buff *skb, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Miklos Szeredi3c0d2f32007-06-05 13:10:29 -07002083 unix_state_lock(sk);
Mark Salyzynb48732e2015-05-26 08:22:19 -07002084 if (sock_flag(sk, SOCK_DEAD)) {
2085 err = -ECONNRESET;
2086 goto unlock;
2087 }
Benjamin Poirier79f632c2013-04-29 11:42:14 +00002088 last = skb = skb_peek(&sk->sk_receive_queue);
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002089 last_len = last ? last->len : 0;
Pavel Emelyanovfc0d7532012-02-21 07:32:06 +00002090again:
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002091 if (skb == NULL) {
Eric Dumazet25888e32010-11-25 04:11:39 +00002092 unix_sk(sk)->recursion_level = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 if (copied >= target)
Miklos Szeredi3c0d2f32007-06-05 13:10:29 -07002094 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
2096 /*
2097 * POSIX 1003.1g mandates this order.
2098 */
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09002099
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002100 err = sock_error(sk);
2101 if (err)
Miklos Szeredi3c0d2f32007-06-05 13:10:29 -07002102 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 if (sk->sk_shutdown & RCV_SHUTDOWN)
Miklos Szeredi3c0d2f32007-06-05 13:10:29 -07002104 goto unlock;
2105
2106 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 err = -EAGAIN;
2108 if (!timeo)
2109 break;
Ingo Molnar57b47a52006-03-20 22:35:41 -08002110 mutex_unlock(&u->readlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002112 timeo = unix_stream_data_wait(sk, timeo, last,
2113 last_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002115 if (signal_pending(current) ||
2116 mutex_lock_interruptible(&u->readlock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 err = sock_intr_errno(timeo);
2118 goto out;
2119 }
Rainer Weikusatb3ca9b02011-02-28 04:50:55 +00002120
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 continue;
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002122unlock:
Miklos Szeredi3c0d2f32007-06-05 13:10:29 -07002123 unix_state_unlock(sk);
2124 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
Pavel Emelyanovfc0d7532012-02-21 07:32:06 +00002126
Eric Dumazete370a722013-08-08 14:37:32 -07002127 while (skip >= unix_skb_len(skb)) {
2128 skip -= unix_skb_len(skb);
Benjamin Poirier79f632c2013-04-29 11:42:14 +00002129 last = skb;
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002130 last_len = skb->len;
Pavel Emelyanovfc0d7532012-02-21 07:32:06 +00002131 skb = skb_peek_next(skb, &sk->sk_receive_queue);
Benjamin Poirier79f632c2013-04-29 11:42:14 +00002132 if (!skb)
2133 goto again;
Pavel Emelyanovfc0d7532012-02-21 07:32:06 +00002134 }
2135
Miklos Szeredi3c0d2f32007-06-05 13:10:29 -07002136 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
2138 if (check_creds) {
2139 /* Never glue messages from different writers */
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002140 if ((UNIXCB(skb).pid != scm.pid) ||
2141 !uid_eq(UNIXCB(skb).uid, scm.creds.uid) ||
Stephen Smalley37a9a8d2015-06-10 08:44:59 -04002142 !gid_eq(UNIXCB(skb).gid, scm.creds.gid) ||
2143 !unix_secdata_eq(&scm, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 break;
Eric W. Biederman0e82e7f6d2013-04-03 16:14:47 +00002145 } else if (test_bit(SOCK_PASSCRED, &sock->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 /* Copy credentials */
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002147 scm_set_cred(&scm, UNIXCB(skb).pid, UNIXCB(skb).uid, UNIXCB(skb).gid);
Stephen Smalley37a9a8d2015-06-10 08:44:59 -04002148 unix_set_secdata(&scm, skb);
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002149 check_creds = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 }
2151
2152 /* Copy address just once */
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002153 if (state->msg && state->msg->msg_name) {
2154 DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr,
2155 state->msg->msg_name);
2156 unix_copy_addr(state->msg, skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 sunaddr = NULL;
2158 }
2159
Eric Dumazete370a722013-08-08 14:37:32 -07002160 chunk = min_t(unsigned int, unix_skb_len(skb) - skip, size);
Hannes Frederic Sowa73ed5d22015-11-10 16:23:15 +01002161 skb_get(skb);
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002162 chunk = state->recv_actor(skb, skip, chunk, state);
Hannes Frederic Sowa73ed5d22015-11-10 16:23:15 +01002163 drop_skb = !unix_skb_len(skb);
2164 /* skb is only safe to use if !drop_skb */
2165 consume_skb(skb);
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002166 if (chunk < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 if (copied == 0)
2168 copied = -EFAULT;
2169 break;
2170 }
2171 copied += chunk;
2172 size -= chunk;
2173
Hannes Frederic Sowa73ed5d22015-11-10 16:23:15 +01002174 if (drop_skb) {
2175 /* the skb was touched by a concurrent reader;
2176 * we should not expect anything from this skb
2177 * anymore and assume it invalid - we can be
2178 * sure it was dropped from the socket queue
2179 *
2180 * let's report a short read
2181 */
2182 err = 0;
2183 break;
2184 }
2185
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 /* Mark read part of skb as used */
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002187 if (!(flags & MSG_PEEK)) {
Eric Dumazete370a722013-08-08 14:37:32 -07002188 UNIXCB(skb).consumed += chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Pavel Emelyanovfc0d7532012-02-21 07:32:06 +00002190 sk_peek_offset_bwd(sk, chunk);
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 if (UNIXCB(skb).fp)
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002193 unix_detach_fds(&scm, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Eric Dumazete370a722013-08-08 14:37:32 -07002195 if (unix_skb_len(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Eric Dumazet6f01fd62012-01-28 16:11:03 +00002198 skb_unlink(skb, &sk->sk_receive_queue);
Neil Horman70d4bf62010-07-20 06:45:56 +00002199 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002201 if (scm.fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 break;
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002203 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 /* It is questionable, see note in unix_dgram_recvmsg.
2205 */
2206 if (UNIXCB(skb).fp)
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002207 scm.fp = scm_fp_dup(UNIXCB(skb).fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Andrey Vagine9193d62015-10-02 00:05:36 +03002209 sk_peek_offset_fwd(sk, chunk);
Pavel Emelyanovfc0d7532012-02-21 07:32:06 +00002210
Aaron Conole9f389e32015-09-26 18:50:43 -04002211 if (UNIXCB(skb).fp)
2212 break;
2213
Andrey Vagine9193d62015-10-02 00:05:36 +03002214 skip = 0;
Aaron Conole9f389e32015-09-26 18:50:43 -04002215 last = skb;
2216 last_len = skb->len;
2217 unix_state_lock(sk);
2218 skb = skb_peek_next(skb, &sk->sk_receive_queue);
2219 if (skb)
2220 goto again;
2221 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 break;
2223 }
2224 } while (size);
2225
Ingo Molnar57b47a52006-03-20 22:35:41 -08002226 mutex_unlock(&u->readlock);
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002227 if (state->msg)
2228 scm_recv(sock, state->msg, &scm, flags);
2229 else
2230 scm_destroy(&scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231out:
2232 return copied ? : err;
2233}
2234
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002235static int unix_stream_read_actor(struct sk_buff *skb,
2236 int skip, int chunk,
2237 struct unix_stream_read_state *state)
2238{
2239 int ret;
2240
2241 ret = skb_copy_datagram_msg(skb, UNIXCB(skb).consumed + skip,
2242 state->msg, chunk);
2243 return ret ?: chunk;
2244}
2245
2246static int unix_stream_recvmsg(struct socket *sock, struct msghdr *msg,
2247 size_t size, int flags)
2248{
2249 struct unix_stream_read_state state = {
2250 .recv_actor = unix_stream_read_actor,
2251 .socket = sock,
2252 .msg = msg,
2253 .size = size,
2254 .flags = flags
2255 };
2256
2257 return unix_stream_read_generic(&state);
2258}
2259
2260static ssize_t skb_unix_socket_splice(struct sock *sk,
2261 struct pipe_inode_info *pipe,
2262 struct splice_pipe_desc *spd)
2263{
2264 int ret;
2265 struct unix_sock *u = unix_sk(sk);
2266
2267 mutex_unlock(&u->readlock);
2268 ret = splice_to_pipe(pipe, spd);
2269 mutex_lock(&u->readlock);
2270
2271 return ret;
2272}
2273
2274static int unix_stream_splice_actor(struct sk_buff *skb,
2275 int skip, int chunk,
2276 struct unix_stream_read_state *state)
2277{
2278 return skb_splice_bits(skb, state->socket->sk,
2279 UNIXCB(skb).consumed + skip,
2280 state->pipe, chunk, state->splice_flags,
2281 skb_unix_socket_splice);
2282}
2283
2284static ssize_t unix_stream_splice_read(struct socket *sock, loff_t *ppos,
2285 struct pipe_inode_info *pipe,
2286 size_t size, unsigned int flags)
2287{
2288 struct unix_stream_read_state state = {
2289 .recv_actor = unix_stream_splice_actor,
2290 .socket = sock,
2291 .pipe = pipe,
2292 .size = size,
2293 .splice_flags = flags,
2294 };
2295
2296 if (unlikely(*ppos))
2297 return -ESPIPE;
2298
2299 if (sock->file->f_flags & O_NONBLOCK ||
2300 flags & SPLICE_F_NONBLOCK)
2301 state.flags = MSG_DONTWAIT;
2302
2303 return unix_stream_read_generic(&state);
2304}
2305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306static int unix_shutdown(struct socket *sock, int mode)
2307{
2308 struct sock *sk = sock->sk;
2309 struct sock *other;
2310
Xi Wangfc61b922012-08-26 16:47:13 +00002311 if (mode < SHUT_RD || mode > SHUT_RDWR)
2312 return -EINVAL;
2313 /* This maps:
2314 * SHUT_RD (0) -> RCV_SHUTDOWN (1)
2315 * SHUT_WR (1) -> SEND_SHUTDOWN (2)
2316 * SHUT_RDWR (2) -> SHUTDOWN_MASK (3)
2317 */
2318 ++mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Alban Crequy7180a032011-01-19 04:56:36 +00002320 unix_state_lock(sk);
2321 sk->sk_shutdown |= mode;
2322 other = unix_peer(sk);
2323 if (other)
2324 sock_hold(other);
2325 unix_state_unlock(sk);
2326 sk->sk_state_change(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Alban Crequy7180a032011-01-19 04:56:36 +00002328 if (other &&
2329 (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Alban Crequy7180a032011-01-19 04:56:36 +00002331 int peer_mode = 0;
2332
2333 if (mode&RCV_SHUTDOWN)
2334 peer_mode |= SEND_SHUTDOWN;
2335 if (mode&SEND_SHUTDOWN)
2336 peer_mode |= RCV_SHUTDOWN;
2337 unix_state_lock(other);
2338 other->sk_shutdown |= peer_mode;
2339 unix_state_unlock(other);
2340 other->sk_state_change(other);
2341 if (peer_mode == SHUTDOWN_MASK)
2342 sk_wake_async(other, SOCK_WAKE_WAITD, POLL_HUP);
2343 else if (peer_mode & RCV_SHUTDOWN)
2344 sk_wake_async(other, SOCK_WAKE_WAITD, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 }
Alban Crequy7180a032011-01-19 04:56:36 +00002346 if (other)
2347 sock_put(other);
2348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 return 0;
2350}
2351
Pavel Emelyanov885ee742011-12-30 00:54:11 +00002352long unix_inq_len(struct sock *sk)
2353{
2354 struct sk_buff *skb;
2355 long amount = 0;
2356
2357 if (sk->sk_state == TCP_LISTEN)
2358 return -EINVAL;
2359
2360 spin_lock(&sk->sk_receive_queue.lock);
2361 if (sk->sk_type == SOCK_STREAM ||
2362 sk->sk_type == SOCK_SEQPACKET) {
2363 skb_queue_walk(&sk->sk_receive_queue, skb)
Eric Dumazete370a722013-08-08 14:37:32 -07002364 amount += unix_skb_len(skb);
Pavel Emelyanov885ee742011-12-30 00:54:11 +00002365 } else {
2366 skb = skb_peek(&sk->sk_receive_queue);
2367 if (skb)
2368 amount = skb->len;
2369 }
2370 spin_unlock(&sk->sk_receive_queue.lock);
2371
2372 return amount;
2373}
2374EXPORT_SYMBOL_GPL(unix_inq_len);
2375
2376long unix_outq_len(struct sock *sk)
2377{
2378 return sk_wmem_alloc_get(sk);
2379}
2380EXPORT_SYMBOL_GPL(unix_outq_len);
2381
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
2383{
2384 struct sock *sk = sock->sk;
Jianjun Konge27dfce2008-11-01 21:38:31 -07002385 long amount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 int err;
2387
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002388 switch (cmd) {
2389 case SIOCOUTQ:
Pavel Emelyanov885ee742011-12-30 00:54:11 +00002390 amount = unix_outq_len(sk);
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002391 err = put_user(amount, (int __user *)arg);
2392 break;
2393 case SIOCINQ:
Pavel Emelyanov885ee742011-12-30 00:54:11 +00002394 amount = unix_inq_len(sk);
2395 if (amount < 0)
2396 err = amount;
2397 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 err = put_user(amount, (int __user *)arg);
Pavel Emelyanov885ee742011-12-30 00:54:11 +00002399 break;
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002400 default:
2401 err = -ENOIOCTLCMD;
2402 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 }
2404 return err;
2405}
2406
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002407static unsigned int unix_poll(struct file *file, struct socket *sock, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408{
2409 struct sock *sk = sock->sk;
2410 unsigned int mask;
2411
Eric Dumazetaa395142010-04-20 13:03:51 +00002412 sock_poll_wait(file, sk_sleep(sk), wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 mask = 0;
2414
2415 /* exceptional events? */
2416 if (sk->sk_err)
2417 mask |= POLLERR;
2418 if (sk->sk_shutdown == SHUTDOWN_MASK)
2419 mask |= POLLHUP;
Davide Libenzif348d702006-03-25 03:07:39 -08002420 if (sk->sk_shutdown & RCV_SHUTDOWN)
Eric Dumazetdb409802010-09-06 11:13:50 +00002421 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
2423 /* readable? */
Eric Dumazetdb409802010-09-06 11:13:50 +00002424 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 mask |= POLLIN | POLLRDNORM;
2426
2427 /* Connection-based need to check for termination and startup */
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002428 if ((sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) &&
2429 sk->sk_state == TCP_CLOSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 mask |= POLLHUP;
2431
2432 /*
2433 * we set writable also when the other side has shut down the
2434 * connection. This prevents stuck sockets.
2435 */
2436 if (unix_writable(sk))
2437 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
2438
2439 return mask;
2440}
2441
Rainer Weikusatec0d2152008-06-27 19:34:18 -07002442static unsigned int unix_dgram_poll(struct file *file, struct socket *sock,
2443 poll_table *wait)
Rainer Weikusat3c734192008-06-17 22:28:05 -07002444{
Rainer Weikusatec0d2152008-06-27 19:34:18 -07002445 struct sock *sk = sock->sk, *other;
2446 unsigned int mask, writable;
Rainer Weikusat3c734192008-06-17 22:28:05 -07002447
Eric Dumazetaa395142010-04-20 13:03:51 +00002448 sock_poll_wait(file, sk_sleep(sk), wait);
Rainer Weikusat3c734192008-06-17 22:28:05 -07002449 mask = 0;
2450
2451 /* exceptional events? */
2452 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
Keller, Jacob E7d4c04f2013-03-28 11:19:25 +00002453 mask |= POLLERR |
Jacob Keller8facd5f2013-04-02 13:55:40 -07002454 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
Keller, Jacob E7d4c04f2013-03-28 11:19:25 +00002455
Rainer Weikusat3c734192008-06-17 22:28:05 -07002456 if (sk->sk_shutdown & RCV_SHUTDOWN)
Eric Dumazet5456f092010-10-31 05:36:23 +00002457 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
Rainer Weikusat3c734192008-06-17 22:28:05 -07002458 if (sk->sk_shutdown == SHUTDOWN_MASK)
2459 mask |= POLLHUP;
2460
2461 /* readable? */
Eric Dumazet5456f092010-10-31 05:36:23 +00002462 if (!skb_queue_empty(&sk->sk_receive_queue))
Rainer Weikusat3c734192008-06-17 22:28:05 -07002463 mask |= POLLIN | POLLRDNORM;
2464
2465 /* Connection-based need to check for termination and startup */
2466 if (sk->sk_type == SOCK_SEQPACKET) {
2467 if (sk->sk_state == TCP_CLOSE)
2468 mask |= POLLHUP;
2469 /* connection hasn't started yet? */
2470 if (sk->sk_state == TCP_SYN_SENT)
2471 return mask;
2472 }
2473
Eric Dumazet973a34a2010-10-31 05:38:25 +00002474 /* No write status requested, avoid expensive OUT tests. */
Hans Verkuil626cf232012-03-23 15:02:27 -07002475 if (!(poll_requested_events(wait) & (POLLWRBAND|POLLWRNORM|POLLOUT)))
Eric Dumazet973a34a2010-10-31 05:38:25 +00002476 return mask;
2477
Rainer Weikusatec0d2152008-06-27 19:34:18 -07002478 writable = unix_writable(sk);
Eric Dumazet5456f092010-10-31 05:36:23 +00002479 other = unix_peer_get(sk);
2480 if (other) {
2481 if (unix_peer(other) != sk) {
2482 sock_poll_wait(file, &unix_sk(other)->peer_wait, wait);
2483 if (unix_recvq_full(other))
2484 writable = 0;
Rainer Weikusatec0d2152008-06-27 19:34:18 -07002485 }
Eric Dumazet5456f092010-10-31 05:36:23 +00002486 sock_put(other);
Rainer Weikusatec0d2152008-06-27 19:34:18 -07002487 }
2488
2489 if (writable)
Rainer Weikusat3c734192008-06-17 22:28:05 -07002490 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
2491 else
2492 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
2493
Rainer Weikusat3c734192008-06-17 22:28:05 -07002494 return mask;
2495}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
2497#ifdef CONFIG_PROC_FS
Pavel Emelyanova53eb3f2007-11-23 20:30:01 +08002498
Eric Dumazet7123aaa2012-06-08 05:03:21 +00002499#define BUCKET_SPACE (BITS_PER_LONG - (UNIX_HASH_BITS + 1) - 1)
2500
2501#define get_bucket(x) ((x) >> BUCKET_SPACE)
2502#define get_offset(x) ((x) & ((1L << BUCKET_SPACE) - 1))
2503#define set_bucket_offset(b, o) ((b) << BUCKET_SPACE | (o))
Pavel Emelyanova53eb3f2007-11-23 20:30:01 +08002504
Eric Dumazet7123aaa2012-06-08 05:03:21 +00002505static struct sock *unix_from_bucket(struct seq_file *seq, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506{
Eric Dumazet7123aaa2012-06-08 05:03:21 +00002507 unsigned long offset = get_offset(*pos);
2508 unsigned long bucket = get_bucket(*pos);
2509 struct sock *sk;
2510 unsigned long count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Eric Dumazet7123aaa2012-06-08 05:03:21 +00002512 for (sk = sk_head(&unix_socket_table[bucket]); sk; sk = sk_next(sk)) {
2513 if (sock_net(sk) != seq_file_net(seq))
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002514 continue;
Eric Dumazet7123aaa2012-06-08 05:03:21 +00002515 if (++count == offset)
2516 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 }
Eric Dumazet7123aaa2012-06-08 05:03:21 +00002518
2519 return sk;
2520}
2521
2522static struct sock *unix_next_socket(struct seq_file *seq,
2523 struct sock *sk,
2524 loff_t *pos)
2525{
2526 unsigned long bucket;
2527
2528 while (sk > (struct sock *)SEQ_START_TOKEN) {
2529 sk = sk_next(sk);
2530 if (!sk)
2531 goto next_bucket;
2532 if (sock_net(sk) == seq_file_net(seq))
2533 return sk;
2534 }
2535
2536 do {
2537 sk = unix_from_bucket(seq, pos);
2538 if (sk)
2539 return sk;
2540
2541next_bucket:
2542 bucket = get_bucket(*pos) + 1;
2543 *pos = set_bucket_offset(bucket, 1);
2544 } while (bucket < ARRAY_SIZE(unix_socket_table));
2545
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 return NULL;
2547}
2548
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549static void *unix_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002550 __acquires(unix_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551{
David S. Millerfbe9cc42005-12-13 23:26:29 -08002552 spin_lock(&unix_table_lock);
Eric Dumazet7123aaa2012-06-08 05:03:21 +00002553
2554 if (!*pos)
2555 return SEQ_START_TOKEN;
2556
2557 if (get_bucket(*pos) >= ARRAY_SIZE(unix_socket_table))
2558 return NULL;
2559
2560 return unix_next_socket(seq, NULL, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561}
2562
2563static void *unix_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2564{
2565 ++*pos;
Eric Dumazet7123aaa2012-06-08 05:03:21 +00002566 return unix_next_socket(seq, v, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567}
2568
2569static void unix_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002570 __releases(unix_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
David S. Millerfbe9cc42005-12-13 23:26:29 -08002572 spin_unlock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573}
2574
2575static int unix_seq_show(struct seq_file *seq, void *v)
2576{
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09002577
Joe Perchesb9f31242008-04-12 19:04:38 -07002578 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 seq_puts(seq, "Num RefCount Protocol Flags Type St "
2580 "Inode Path\n");
2581 else {
2582 struct sock *s = v;
2583 struct unix_sock *u = unix_sk(s);
David S. Miller1c92b4e2007-05-31 13:24:26 -07002584 unix_state_lock(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
Dan Rosenberg71338aa2011-05-23 12:17:35 +00002586 seq_printf(seq, "%pK: %08X %08X %08X %04X %02X %5lu",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 s,
2588 atomic_read(&s->sk_refcnt),
2589 0,
2590 s->sk_state == TCP_LISTEN ? __SO_ACCEPTCON : 0,
2591 s->sk_type,
2592 s->sk_socket ?
2593 (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTED : SS_UNCONNECTED) :
2594 (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTING : SS_DISCONNECTING),
2595 sock_i_ino(s));
2596
2597 if (u->addr) {
2598 int i, len;
2599 seq_putc(seq, ' ');
2600
2601 i = 0;
2602 len = u->addr->len - sizeof(short);
2603 if (!UNIX_ABSTRACT(s))
2604 len--;
2605 else {
2606 seq_putc(seq, '@');
2607 i++;
2608 }
2609 for ( ; i < len; i++)
2610 seq_putc(seq, u->addr->name->sun_path[i]);
2611 }
David S. Miller1c92b4e2007-05-31 13:24:26 -07002612 unix_state_unlock(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 seq_putc(seq, '\n');
2614 }
2615
2616 return 0;
2617}
2618
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002619static const struct seq_operations unix_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 .start = unix_seq_start,
2621 .next = unix_seq_next,
2622 .stop = unix_seq_stop,
2623 .show = unix_seq_show,
2624};
2625
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626static int unix_seq_open(struct inode *inode, struct file *file)
2627{
Denis V. Luneve372c412007-11-19 22:31:54 -08002628 return seq_open_net(inode, file, &unix_seq_ops,
Eric Dumazet8b51b062012-06-08 22:10:20 +00002629 sizeof(struct seq_net_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630}
2631
Arjan van de Venda7071d2007-02-12 00:55:36 -08002632static const struct file_operations unix_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 .owner = THIS_MODULE,
2634 .open = unix_seq_open,
2635 .read = seq_read,
2636 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08002637 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638};
2639
2640#endif
2641
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002642static const struct net_proto_family unix_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 .family = PF_UNIX,
2644 .create = unix_create,
2645 .owner = THIS_MODULE,
2646};
2647
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002648
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002649static int __net_init unix_net_init(struct net *net)
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002650{
2651 int error = -ENOMEM;
2652
Denis V. Luneva0a53c82007-12-11 04:19:17 -08002653 net->unx.sysctl_max_dgram_qlen = 10;
Pavel Emelyanov1597fbc2007-12-01 23:51:01 +11002654 if (unix_sysctl_register(net))
2655 goto out;
Pavel Emelyanovd392e492007-12-01 23:44:15 +11002656
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002657#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +00002658 if (!proc_create("unix", 0, net->proc_net, &unix_seq_fops)) {
Pavel Emelyanov1597fbc2007-12-01 23:51:01 +11002659 unix_sysctl_unregister(net);
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002660 goto out;
Pavel Emelyanov1597fbc2007-12-01 23:51:01 +11002661 }
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002662#endif
2663 error = 0;
2664out:
Jianjun Kong48dcc33e2008-11-01 21:37:27 -07002665 return error;
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002666}
2667
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002668static void __net_exit unix_net_exit(struct net *net)
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002669{
Pavel Emelyanov1597fbc2007-12-01 23:51:01 +11002670 unix_sysctl_unregister(net);
Gao fengece31ff2013-02-18 01:34:56 +00002671 remove_proc_entry("unix", net->proc_net);
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002672}
2673
2674static struct pernet_operations unix_net_ops = {
2675 .init = unix_net_init,
2676 .exit = unix_net_exit,
2677};
2678
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679static int __init af_unix_init(void)
2680{
2681 int rc = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
YOSHIFUJI Hideaki / 吉藤英明b4fff5f2013-01-09 07:20:07 +00002683 BUILD_BUG_ON(sizeof(struct unix_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
2685 rc = proto_register(&unix_proto, 1);
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09002686 if (rc != 0) {
wangweidong5cc208b2013-12-06 18:03:36 +08002687 pr_crit("%s: Cannot create unix_sock SLAB cache!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 goto out;
2689 }
2690
2691 sock_register(&unix_family_ops);
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002692 register_pernet_subsys(&unix_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693out:
2694 return rc;
2695}
2696
2697static void __exit af_unix_exit(void)
2698{
2699 sock_unregister(PF_UNIX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 proto_unregister(&unix_proto);
Denis V. Lunev097e66c2007-11-19 22:29:30 -08002701 unregister_pernet_subsys(&unix_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702}
2703
David Woodhouse3d366962008-04-24 00:59:25 -07002704/* Earlier than device_initcall() so that other drivers invoking
2705 request_module() don't end up in a loop when modprobe tries
2706 to use a UNIX socket. But later than subsys_initcall() because
2707 we depend on stuff initialised there */
2708fs_initcall(af_unix_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709module_exit(af_unix_exit);
2710
2711MODULE_LICENSE("GPL");
2712MODULE_ALIAS_NETPROTO(PF_UNIX);