xref: /linux/net/unix/af_unix.c (revision 4dbed85a35ed37d9608f4f32e5d69efa775d6223)
1 /*
2  * NET4:	Implementation of BSD Unix domain sockets.
3  *
4  * Authors:	Alan Cox, <alan.cox@linux.org>
5  *
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  *
11  * Version:	$Id: af_unix.c,v 1.133 2002/02/08 03:57:19 davem Exp $
12  *
13  * Fixes:
14  *		Linus Torvalds	:	Assorted bug cures.
15  *		Niibe Yutaka	:	async I/O support.
16  *		Carsten Paeth	:	PF_UNIX check, address fixes.
17  *		Alan Cox	:	Limit size of allocated blocks.
18  *		Alan Cox	:	Fixed the stupid socketpair bug.
19  *		Alan Cox	:	BSD compatibility fine tuning.
20  *		Alan Cox	:	Fixed a bug in connect when interrupted.
21  *		Alan Cox	:	Sorted out a proper draft version of
22  *					file descriptor passing hacked up from
23  *					Mike Shaver's work.
24  *		Marty Leisner	:	Fixes to fd passing
25  *		Nick Nevin	:	recvmsg bugfix.
26  *		Alan Cox	:	Started proper garbage collector
27  *		Heiko EiBfeldt	:	Missing verify_area check
28  *		Alan Cox	:	Started POSIXisms
29  *		Andreas Schwab	:	Replace inode by dentry for proper
30  *					reference counting
31  *		Kirk Petersen	:	Made this a module
32  *	    Christoph Rohland	:	Elegant non-blocking accept/connect algorithm.
33  *					Lots of bug fixes.
34  *	     Alexey Kuznetosv	:	Repaired (I hope) bugs introduces
35  *					by above two patches.
36  *	     Andrea Arcangeli	:	If possible we block in connect(2)
37  *					if the max backlog of the listen socket
38  *					is been reached. This won't break
39  *					old apps and it will avoid huge amount
40  *					of socks hashed (this for unix_gc()
41  *					performances reasons).
42  *					Security fix that limits the max
43  *					number of socks to 2*max_files and
44  *					the number of skb queueable in the
45  *					dgram receiver.
46  *		Artur Skawina   :	Hash function optimizations
47  *	     Alexey Kuznetsov   :	Full scale SMP. Lot of bugs are introduced 8)
48  *	      Malcolm Beattie   :	Set peercred for socketpair
49  *	     Michal Ostrowski   :       Module initialization cleanup.
50  *	     Arnaldo C. Melo	:	Remove MOD_{INC,DEC}_USE_COUNT,
51  *	     				the core infrastructure is doing that
52  *	     				for all net proto families now (2.5.69+)
53  *
54  *
55  * Known differences from reference BSD that was tested:
56  *
57  *	[TO FIX]
58  *	ECONNREFUSED is not returned from one end of a connected() socket to the
59  *		other the moment one end closes.
60  *	fstat() doesn't return st_dev=0, and give the blksize as high water mark
61  *		and a fake inode identifier (nor the BSD first socket fstat twice bug).
62  *	[NOT TO FIX]
63  *	accept() returns a path name even if the connecting socket has closed
64  *		in the meantime (BSD loses the path and gives up).
65  *	accept() returns 0 length path for an unbound connector. BSD returns 16
66  *		and a null first byte in the path (but not for gethost/peername - BSD bug ??)
67  *	socketpair(...SOCK_RAW..) doesn't panic the kernel.
68  *	BSD af_unix apparently has connect forgetting to block properly.
69  *		(need to check this with the POSIX spec in detail)
70  *
71  * Differences from 2.0.0-11-... (ANK)
72  *	Bug fixes and improvements.
73  *		- client shutdown killed server socket.
74  *		- removed all useless cli/sti pairs.
75  *
76  *	Semantic changes/extensions.
77  *		- generic control message passing.
78  *		- SCM_CREDENTIALS control message.
79  *		- "Abstract" (not FS based) socket bindings.
80  *		  Abstract names are sequences of bytes (not zero terminated)
81  *		  started by 0, so that this name space does not intersect
82  *		  with BSD names.
83  */
84 
85 #include <linux/module.h>
86 #include <linux/kernel.h>
87 #include <linux/signal.h>
88 #include <linux/sched.h>
89 #include <linux/errno.h>
90 #include <linux/string.h>
91 #include <linux/stat.h>
92 #include <linux/dcache.h>
93 #include <linux/namei.h>
94 #include <linux/socket.h>
95 #include <linux/un.h>
96 #include <linux/fcntl.h>
97 #include <linux/termios.h>
98 #include <linux/sockios.h>
99 #include <linux/net.h>
100 #include <linux/in.h>
101 #include <linux/fs.h>
102 #include <linux/slab.h>
103 #include <asm/uaccess.h>
104 #include <linux/skbuff.h>
105 #include <linux/netdevice.h>
106 #include <net/net_namespace.h>
107 #include <net/sock.h>
108 #include <net/tcp_states.h>
109 #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>
115 #include <linux/rtnetlink.h>
116 #include <linux/mount.h>
117 #include <net/checksum.h>
118 #include <linux/security.h>
119 
120 int sysctl_unix_max_dgram_qlen __read_mostly = 10;
121 
122 static struct hlist_head unix_socket_table[UNIX_HASH_SIZE + 1];
123 static DEFINE_SPINLOCK(unix_table_lock);
124 static atomic_t unix_nr_socks = ATOMIC_INIT(0);
125 
126 #define unix_sockets_unbound	(&unix_socket_table[UNIX_HASH_SIZE])
127 
128 #define UNIX_ABSTRACT(sk)	(unix_sk(sk)->addr->hash != UNIX_HASH_SIZE)
129 
130 static struct sock *first_unix_socket(int *i)
131 {
132 	for (*i = 0; *i <= UNIX_HASH_SIZE; (*i)++) {
133 		if (!hlist_empty(&unix_socket_table[*i]))
134 			return __sk_head(&unix_socket_table[*i]);
135 	}
136 	return NULL;
137 }
138 
139 static struct sock *next_unix_socket(int *i, struct sock *s)
140 {
141 	struct sock *next = sk_next(s);
142 	/* More in this chain? */
143 	if (next)
144 		return next;
145 	/* Look for next non-empty chain. */
146 	for ((*i)++; *i <= UNIX_HASH_SIZE; (*i)++) {
147 		if (!hlist_empty(&unix_socket_table[*i]))
148 			return __sk_head(&unix_socket_table[*i]);
149 	}
150 	return NULL;
151 }
152 
153 #define forall_unix_sockets(i, s) \
154 	for (s = first_unix_socket(&(i)); s; s = next_unix_socket(&(i),(s)))
155 
156 #ifdef CONFIG_SECURITY_NETWORK
157 static void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
158 {
159 	memcpy(UNIXSID(skb), &scm->secid, sizeof(u32));
160 }
161 
162 static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
163 {
164 	scm->secid = *UNIXSID(skb);
165 }
166 #else
167 static inline void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
168 { }
169 
170 static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
171 { }
172 #endif /* CONFIG_SECURITY_NETWORK */
173 
174 /*
175  *  SMP locking strategy:
176  *    hash table is protected with spinlock unix_table_lock
177  *    each socket state is protected by separate rwlock.
178  */
179 
180 static inline unsigned unix_hash_fold(__wsum n)
181 {
182 	unsigned hash = (__force unsigned)n;
183 	hash ^= hash>>16;
184 	hash ^= hash>>8;
185 	return hash&(UNIX_HASH_SIZE-1);
186 }
187 
188 #define unix_peer(sk) (unix_sk(sk)->peer)
189 
190 static inline int unix_our_peer(struct sock *sk, struct sock *osk)
191 {
192 	return unix_peer(osk) == sk;
193 }
194 
195 static inline int unix_may_send(struct sock *sk, struct sock *osk)
196 {
197 	return (unix_peer(osk) == NULL || unix_our_peer(sk, osk));
198 }
199 
200 static struct sock *unix_peer_get(struct sock *s)
201 {
202 	struct sock *peer;
203 
204 	unix_state_lock(s);
205 	peer = unix_peer(s);
206 	if (peer)
207 		sock_hold(peer);
208 	unix_state_unlock(s);
209 	return peer;
210 }
211 
212 static 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  */
224 
225 static int unix_mkname(struct sockaddr_un * sunaddr, int len, unsigned *hashp)
226 {
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.
235 		 * sun_path[108] doesnt as such exist.  However in kernel space
236 		 * we are guaranteed that it is a valid memory location in our
237 		 * kernel address buffer.
238 		 */
239 		((char *)sunaddr)[len]=0;
240 		len = strlen(sunaddr->sun_path)+1+sizeof(short);
241 		return len;
242 	}
243 
244 	*hashp = unix_hash_fold(csum_partial((char*)sunaddr, len, 0));
245 	return len;
246 }
247 
248 static void __unix_remove_socket(struct sock *sk)
249 {
250 	sk_del_node_init(sk);
251 }
252 
253 static void __unix_insert_socket(struct hlist_head *list, struct sock *sk)
254 {
255 	BUG_TRAP(sk_unhashed(sk));
256 	sk_add_node(sk, list);
257 }
258 
259 static inline void unix_remove_socket(struct sock *sk)
260 {
261 	spin_lock(&unix_table_lock);
262 	__unix_remove_socket(sk);
263 	spin_unlock(&unix_table_lock);
264 }
265 
266 static inline void unix_insert_socket(struct hlist_head *list, struct sock *sk)
267 {
268 	spin_lock(&unix_table_lock);
269 	__unix_insert_socket(list, sk);
270 	spin_unlock(&unix_table_lock);
271 }
272 
273 static struct sock *__unix_find_socket_byname(struct sockaddr_un *sunname,
274 					      int len, int type, unsigned hash)
275 {
276 	struct sock *s;
277 	struct hlist_node *node;
278 
279 	sk_for_each(s, node, &unix_socket_table[hash ^ type]) {
280 		struct unix_sock *u = unix_sk(s);
281 
282 		if (u->addr->len == len &&
283 		    !memcmp(u->addr->name, sunname, len))
284 			goto found;
285 	}
286 	s = NULL;
287 found:
288 	return s;
289 }
290 
291 static inline struct sock *unix_find_socket_byname(struct sockaddr_un *sunname,
292 						   int len, int type,
293 						   unsigned hash)
294 {
295 	struct sock *s;
296 
297 	spin_lock(&unix_table_lock);
298 	s = __unix_find_socket_byname(sunname, len, type, hash);
299 	if (s)
300 		sock_hold(s);
301 	spin_unlock(&unix_table_lock);
302 	return s;
303 }
304 
305 static struct sock *unix_find_socket_byinode(struct inode *i)
306 {
307 	struct sock *s;
308 	struct hlist_node *node;
309 
310 	spin_lock(&unix_table_lock);
311 	sk_for_each(s, node,
312 		    &unix_socket_table[i->i_ino & (UNIX_HASH_SIZE - 1)]) {
313 		struct dentry *dentry = unix_sk(s)->dentry;
314 
315 		if(dentry && dentry->d_inode == i)
316 		{
317 			sock_hold(s);
318 			goto found;
319 		}
320 	}
321 	s = NULL;
322 found:
323 	spin_unlock(&unix_table_lock);
324 	return s;
325 }
326 
327 static inline int unix_writable(struct sock *sk)
328 {
329 	return (atomic_read(&sk->sk_wmem_alloc) << 2) <= sk->sk_sndbuf;
330 }
331 
332 static void unix_write_space(struct sock *sk)
333 {
334 	read_lock(&sk->sk_callback_lock);
335 	if (unix_writable(sk)) {
336 		if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
337 			wake_up_interruptible_sync(sk->sk_sleep);
338 		sk_wake_async(sk, 2, POLL_OUT);
339 	}
340 	read_unlock(&sk->sk_callback_lock);
341 }
342 
343 /* When dgram socket disconnects (or changes its peer), we clear its receive
344  * queue of packets arrived from previous peer. First, it allows to do
345  * flow control based only on wmem_alloc; second, sk connected to peer
346  * may receive messages only from that peer. */
347 static void unix_dgram_disconnected(struct sock *sk, struct sock *other)
348 {
349 	if (!skb_queue_empty(&sk->sk_receive_queue)) {
350 		skb_queue_purge(&sk->sk_receive_queue);
351 		wake_up_interruptible_all(&unix_sk(sk)->peer_wait);
352 
353 		/* If one link of bidirectional dgram pipe is disconnected,
354 		 * we signal error. Messages are lost. Do not make this,
355 		 * when peer was not connected to us.
356 		 */
357 		if (!sock_flag(other, SOCK_DEAD) && unix_peer(other) == sk) {
358 			other->sk_err = ECONNRESET;
359 			other->sk_error_report(other);
360 		}
361 	}
362 }
363 
364 static void unix_sock_destructor(struct sock *sk)
365 {
366 	struct unix_sock *u = unix_sk(sk);
367 
368 	skb_queue_purge(&sk->sk_receive_queue);
369 
370 	BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
371 	BUG_TRAP(sk_unhashed(sk));
372 	BUG_TRAP(!sk->sk_socket);
373 	if (!sock_flag(sk, SOCK_DEAD)) {
374 		printk("Attempt to release alive unix socket: %p\n", sk);
375 		return;
376 	}
377 
378 	if (u->addr)
379 		unix_release_addr(u->addr);
380 
381 	atomic_dec(&unix_nr_socks);
382 #ifdef UNIX_REFCNT_DEBUG
383 	printk(KERN_DEBUG "UNIX %p is destroyed, %d are still alive.\n", sk, atomic_read(&unix_nr_socks));
384 #endif
385 }
386 
387 static int unix_release_sock (struct sock *sk, int embrion)
388 {
389 	struct unix_sock *u = unix_sk(sk);
390 	struct dentry *dentry;
391 	struct vfsmount *mnt;
392 	struct sock *skpair;
393 	struct sk_buff *skb;
394 	int state;
395 
396 	unix_remove_socket(sk);
397 
398 	/* Clear state */
399 	unix_state_lock(sk);
400 	sock_orphan(sk);
401 	sk->sk_shutdown = SHUTDOWN_MASK;
402 	dentry	     = u->dentry;
403 	u->dentry    = NULL;
404 	mnt	     = u->mnt;
405 	u->mnt	     = NULL;
406 	state = sk->sk_state;
407 	sk->sk_state = TCP_CLOSE;
408 	unix_state_unlock(sk);
409 
410 	wake_up_interruptible_all(&u->peer_wait);
411 
412 	skpair=unix_peer(sk);
413 
414 	if (skpair!=NULL) {
415 		if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) {
416 			unix_state_lock(skpair);
417 			/* No more writes */
418 			skpair->sk_shutdown = SHUTDOWN_MASK;
419 			if (!skb_queue_empty(&sk->sk_receive_queue) || embrion)
420 				skpair->sk_err = ECONNRESET;
421 			unix_state_unlock(skpair);
422 			skpair->sk_state_change(skpair);
423 			read_lock(&skpair->sk_callback_lock);
424 			sk_wake_async(skpair,1,POLL_HUP);
425 			read_unlock(&skpair->sk_callback_lock);
426 		}
427 		sock_put(skpair); /* It may now die */
428 		unix_peer(sk) = NULL;
429 	}
430 
431 	/* Try to flush out this socket. Throw out buffers at least */
432 
433 	while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
434 		if (state==TCP_LISTEN)
435 			unix_release_sock(skb->sk, 1);
436 		/* passed fds are erased in the kfree_skb hook	      */
437 		kfree_skb(skb);
438 	}
439 
440 	if (dentry) {
441 		dput(dentry);
442 		mntput(mnt);
443 	}
444 
445 	sock_put(sk);
446 
447 	/* ---- Socket is dead now and most probably destroyed ---- */
448 
449 	/*
450 	 * Fixme: BSD difference: In BSD all sockets connected to use get
451 	 *	  ECONNRESET and we die on the spot. In Linux we behave
452 	 *	  like files and pipes do and wait for the last
453 	 *	  dereference.
454 	 *
455 	 * Can't we simply set sock->err?
456 	 *
457 	 *	  What the above comment does talk about? --ANK(980817)
458 	 */
459 
460 	if (unix_tot_inflight)
461 		unix_gc();		/* Garbage collect fds */
462 
463 	return 0;
464 }
465 
466 static int unix_listen(struct socket *sock, int backlog)
467 {
468 	int err;
469 	struct sock *sk = sock->sk;
470 	struct unix_sock *u = unix_sk(sk);
471 
472 	err = -EOPNOTSUPP;
473 	if (sock->type!=SOCK_STREAM && sock->type!=SOCK_SEQPACKET)
474 		goto out;			/* Only stream/seqpacket sockets accept */
475 	err = -EINVAL;
476 	if (!u->addr)
477 		goto out;			/* No listens on an unbound socket */
478 	unix_state_lock(sk);
479 	if (sk->sk_state != TCP_CLOSE && sk->sk_state != TCP_LISTEN)
480 		goto out_unlock;
481 	if (backlog > sk->sk_max_ack_backlog)
482 		wake_up_interruptible_all(&u->peer_wait);
483 	sk->sk_max_ack_backlog	= backlog;
484 	sk->sk_state		= TCP_LISTEN;
485 	/* set credentials so connect can copy them */
486 	sk->sk_peercred.pid	= task_tgid_vnr(current);
487 	sk->sk_peercred.uid	= current->euid;
488 	sk->sk_peercred.gid	= current->egid;
489 	err = 0;
490 
491 out_unlock:
492 	unix_state_unlock(sk);
493 out:
494 	return err;
495 }
496 
497 static int unix_release(struct socket *);
498 static int unix_bind(struct socket *, struct sockaddr *, int);
499 static int unix_stream_connect(struct socket *, struct sockaddr *,
500 			       int addr_len, int flags);
501 static int unix_socketpair(struct socket *, struct socket *);
502 static int unix_accept(struct socket *, struct socket *, int);
503 static int unix_getname(struct socket *, struct sockaddr *, int *, int);
504 static unsigned int unix_poll(struct file *, struct socket *, poll_table *);
505 static int unix_ioctl(struct socket *, unsigned int, unsigned long);
506 static int unix_shutdown(struct socket *, int);
507 static int unix_stream_sendmsg(struct kiocb *, struct socket *,
508 			       struct msghdr *, size_t);
509 static int unix_stream_recvmsg(struct kiocb *, struct socket *,
510 			       struct msghdr *, size_t, int);
511 static int unix_dgram_sendmsg(struct kiocb *, struct socket *,
512 			      struct msghdr *, size_t);
513 static int unix_dgram_recvmsg(struct kiocb *, struct socket *,
514 			      struct msghdr *, size_t, int);
515 static int unix_dgram_connect(struct socket *, struct sockaddr *,
516 			      int, int);
517 static int unix_seqpacket_sendmsg(struct kiocb *, struct socket *,
518 				  struct msghdr *, size_t);
519 
520 static const struct proto_ops unix_stream_ops = {
521 	.family =	PF_UNIX,
522 	.owner =	THIS_MODULE,
523 	.release =	unix_release,
524 	.bind =		unix_bind,
525 	.connect =	unix_stream_connect,
526 	.socketpair =	unix_socketpair,
527 	.accept =	unix_accept,
528 	.getname =	unix_getname,
529 	.poll =		unix_poll,
530 	.ioctl =	unix_ioctl,
531 	.listen =	unix_listen,
532 	.shutdown =	unix_shutdown,
533 	.setsockopt =	sock_no_setsockopt,
534 	.getsockopt =	sock_no_getsockopt,
535 	.sendmsg =	unix_stream_sendmsg,
536 	.recvmsg =	unix_stream_recvmsg,
537 	.mmap =		sock_no_mmap,
538 	.sendpage =	sock_no_sendpage,
539 };
540 
541 static const struct proto_ops unix_dgram_ops = {
542 	.family =	PF_UNIX,
543 	.owner =	THIS_MODULE,
544 	.release =	unix_release,
545 	.bind =		unix_bind,
546 	.connect =	unix_dgram_connect,
547 	.socketpair =	unix_socketpair,
548 	.accept =	sock_no_accept,
549 	.getname =	unix_getname,
550 	.poll =		datagram_poll,
551 	.ioctl =	unix_ioctl,
552 	.listen =	sock_no_listen,
553 	.shutdown =	unix_shutdown,
554 	.setsockopt =	sock_no_setsockopt,
555 	.getsockopt =	sock_no_getsockopt,
556 	.sendmsg =	unix_dgram_sendmsg,
557 	.recvmsg =	unix_dgram_recvmsg,
558 	.mmap =		sock_no_mmap,
559 	.sendpage =	sock_no_sendpage,
560 };
561 
562 static const struct proto_ops unix_seqpacket_ops = {
563 	.family =	PF_UNIX,
564 	.owner =	THIS_MODULE,
565 	.release =	unix_release,
566 	.bind =		unix_bind,
567 	.connect =	unix_stream_connect,
568 	.socketpair =	unix_socketpair,
569 	.accept =	unix_accept,
570 	.getname =	unix_getname,
571 	.poll =		datagram_poll,
572 	.ioctl =	unix_ioctl,
573 	.listen =	unix_listen,
574 	.shutdown =	unix_shutdown,
575 	.setsockopt =	sock_no_setsockopt,
576 	.getsockopt =	sock_no_getsockopt,
577 	.sendmsg =	unix_seqpacket_sendmsg,
578 	.recvmsg =	unix_dgram_recvmsg,
579 	.mmap =		sock_no_mmap,
580 	.sendpage =	sock_no_sendpage,
581 };
582 
583 static struct proto unix_proto = {
584 	.name	  = "UNIX",
585 	.owner	  = THIS_MODULE,
586 	.obj_size = sizeof(struct unix_sock),
587 };
588 
589 /*
590  * AF_UNIX sockets do not interact with hardware, hence they
591  * dont trigger interrupts - so it's safe for them to have
592  * bh-unsafe locking for their sk_receive_queue.lock. Split off
593  * this special lock-class by reinitializing the spinlock key:
594  */
595 static struct lock_class_key af_unix_sk_receive_queue_lock_key;
596 
597 static struct sock * unix_create1(struct net *net, struct socket *sock)
598 {
599 	struct sock *sk = NULL;
600 	struct unix_sock *u;
601 
602 	atomic_inc(&unix_nr_socks);
603 	if (atomic_read(&unix_nr_socks) > 2 * get_max_files())
604 		goto out;
605 
606 	sk = sk_alloc(net, PF_UNIX, GFP_KERNEL, &unix_proto);
607 	if (!sk)
608 		goto out;
609 
610 	sock_init_data(sock,sk);
611 	lockdep_set_class(&sk->sk_receive_queue.lock,
612 				&af_unix_sk_receive_queue_lock_key);
613 
614 	sk->sk_write_space	= unix_write_space;
615 	sk->sk_max_ack_backlog	= sysctl_unix_max_dgram_qlen;
616 	sk->sk_destruct		= unix_sock_destructor;
617 	u	  = unix_sk(sk);
618 	u->dentry = NULL;
619 	u->mnt	  = NULL;
620 	spin_lock_init(&u->lock);
621 	atomic_set(&u->inflight, 0);
622 	INIT_LIST_HEAD(&u->link);
623 	mutex_init(&u->readlock); /* single task reading lock */
624 	init_waitqueue_head(&u->peer_wait);
625 	unix_insert_socket(unix_sockets_unbound, sk);
626 out:
627 	if (sk == NULL)
628 		atomic_dec(&unix_nr_socks);
629 	return sk;
630 }
631 
632 static int unix_create(struct net *net, struct socket *sock, int protocol)
633 {
634 	if (net != &init_net)
635 		return -EAFNOSUPPORT;
636 
637 	if (protocol && protocol != PF_UNIX)
638 		return -EPROTONOSUPPORT;
639 
640 	sock->state = SS_UNCONNECTED;
641 
642 	switch (sock->type) {
643 	case SOCK_STREAM:
644 		sock->ops = &unix_stream_ops;
645 		break;
646 		/*
647 		 *	Believe it or not BSD has AF_UNIX, SOCK_RAW though
648 		 *	nothing uses it.
649 		 */
650 	case SOCK_RAW:
651 		sock->type=SOCK_DGRAM;
652 	case SOCK_DGRAM:
653 		sock->ops = &unix_dgram_ops;
654 		break;
655 	case SOCK_SEQPACKET:
656 		sock->ops = &unix_seqpacket_ops;
657 		break;
658 	default:
659 		return -ESOCKTNOSUPPORT;
660 	}
661 
662 	return unix_create1(net, sock) ? 0 : -ENOMEM;
663 }
664 
665 static int unix_release(struct socket *sock)
666 {
667 	struct sock *sk = sock->sk;
668 
669 	if (!sk)
670 		return 0;
671 
672 	sock->sk = NULL;
673 
674 	return unix_release_sock (sk, 0);
675 }
676 
677 static int unix_autobind(struct socket *sock)
678 {
679 	struct sock *sk = sock->sk;
680 	struct unix_sock *u = unix_sk(sk);
681 	static u32 ordernum = 1;
682 	struct unix_address * addr;
683 	int err;
684 
685 	mutex_lock(&u->readlock);
686 
687 	err = 0;
688 	if (u->addr)
689 		goto out;
690 
691 	err = -ENOMEM;
692 	addr = kzalloc(sizeof(*addr) + sizeof(short) + 16, GFP_KERNEL);
693 	if (!addr)
694 		goto out;
695 
696 	addr->name->sun_family = AF_UNIX;
697 	atomic_set(&addr->refcnt, 1);
698 
699 retry:
700 	addr->len = sprintf(addr->name->sun_path+1, "%05x", ordernum) + 1 + sizeof(short);
701 	addr->hash = unix_hash_fold(csum_partial((void*)addr->name, addr->len, 0));
702 
703 	spin_lock(&unix_table_lock);
704 	ordernum = (ordernum+1)&0xFFFFF;
705 
706 	if (__unix_find_socket_byname(addr->name, addr->len, sock->type,
707 				      addr->hash)) {
708 		spin_unlock(&unix_table_lock);
709 		/* Sanity yield. It is unusual case, but yet... */
710 		if (!(ordernum&0xFF))
711 			yield();
712 		goto retry;
713 	}
714 	addr->hash ^= sk->sk_type;
715 
716 	__unix_remove_socket(sk);
717 	u->addr = addr;
718 	__unix_insert_socket(&unix_socket_table[addr->hash], sk);
719 	spin_unlock(&unix_table_lock);
720 	err = 0;
721 
722 out:	mutex_unlock(&u->readlock);
723 	return err;
724 }
725 
726 static struct sock *unix_find_other(struct sockaddr_un *sunname, int len,
727 				    int type, unsigned hash, int *error)
728 {
729 	struct sock *u;
730 	struct nameidata nd;
731 	int err = 0;
732 
733 	if (sunname->sun_path[0]) {
734 		err = path_lookup(sunname->sun_path, LOOKUP_FOLLOW, &nd);
735 		if (err)
736 			goto fail;
737 		err = vfs_permission(&nd, MAY_WRITE);
738 		if (err)
739 			goto put_fail;
740 
741 		err = -ECONNREFUSED;
742 		if (!S_ISSOCK(nd.dentry->d_inode->i_mode))
743 			goto put_fail;
744 		u=unix_find_socket_byinode(nd.dentry->d_inode);
745 		if (!u)
746 			goto put_fail;
747 
748 		if (u->sk_type == type)
749 			touch_atime(nd.mnt, nd.dentry);
750 
751 		path_release(&nd);
752 
753 		err=-EPROTOTYPE;
754 		if (u->sk_type != type) {
755 			sock_put(u);
756 			goto fail;
757 		}
758 	} else {
759 		err = -ECONNREFUSED;
760 		u=unix_find_socket_byname(sunname, len, type, hash);
761 		if (u) {
762 			struct dentry *dentry;
763 			dentry = unix_sk(u)->dentry;
764 			if (dentry)
765 				touch_atime(unix_sk(u)->mnt, dentry);
766 		} else
767 			goto fail;
768 	}
769 	return u;
770 
771 put_fail:
772 	path_release(&nd);
773 fail:
774 	*error=err;
775 	return NULL;
776 }
777 
778 
779 static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
780 {
781 	struct sock *sk = sock->sk;
782 	struct unix_sock *u = unix_sk(sk);
783 	struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
784 	struct dentry * dentry = NULL;
785 	struct nameidata nd;
786 	int err;
787 	unsigned hash;
788 	struct unix_address *addr;
789 	struct hlist_head *list;
790 
791 	err = -EINVAL;
792 	if (sunaddr->sun_family != AF_UNIX)
793 		goto out;
794 
795 	if (addr_len==sizeof(short)) {
796 		err = unix_autobind(sock);
797 		goto out;
798 	}
799 
800 	err = unix_mkname(sunaddr, addr_len, &hash);
801 	if (err < 0)
802 		goto out;
803 	addr_len = err;
804 
805 	mutex_lock(&u->readlock);
806 
807 	err = -EINVAL;
808 	if (u->addr)
809 		goto out_up;
810 
811 	err = -ENOMEM;
812 	addr = kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);
813 	if (!addr)
814 		goto out_up;
815 
816 	memcpy(addr->name, sunaddr, addr_len);
817 	addr->len = addr_len;
818 	addr->hash = hash ^ sk->sk_type;
819 	atomic_set(&addr->refcnt, 1);
820 
821 	if (sunaddr->sun_path[0]) {
822 		unsigned int mode;
823 		err = 0;
824 		/*
825 		 * Get the parent directory, calculate the hash for last
826 		 * component.
827 		 */
828 		err = path_lookup(sunaddr->sun_path, LOOKUP_PARENT, &nd);
829 		if (err)
830 			goto out_mknod_parent;
831 
832 		dentry = lookup_create(&nd, 0);
833 		err = PTR_ERR(dentry);
834 		if (IS_ERR(dentry))
835 			goto out_mknod_unlock;
836 
837 		/*
838 		 * All right, let's create it.
839 		 */
840 		mode = S_IFSOCK |
841 		       (SOCK_INODE(sock)->i_mode & ~current->fs->umask);
842 		err = vfs_mknod(nd.dentry->d_inode, dentry, mode, 0);
843 		if (err)
844 			goto out_mknod_dput;
845 		mutex_unlock(&nd.dentry->d_inode->i_mutex);
846 		dput(nd.dentry);
847 		nd.dentry = dentry;
848 
849 		addr->hash = UNIX_HASH_SIZE;
850 	}
851 
852 	spin_lock(&unix_table_lock);
853 
854 	if (!sunaddr->sun_path[0]) {
855 		err = -EADDRINUSE;
856 		if (__unix_find_socket_byname(sunaddr, addr_len,
857 					      sk->sk_type, hash)) {
858 			unix_release_addr(addr);
859 			goto out_unlock;
860 		}
861 
862 		list = &unix_socket_table[addr->hash];
863 	} else {
864 		list = &unix_socket_table[dentry->d_inode->i_ino & (UNIX_HASH_SIZE-1)];
865 		u->dentry = nd.dentry;
866 		u->mnt    = nd.mnt;
867 	}
868 
869 	err = 0;
870 	__unix_remove_socket(sk);
871 	u->addr = addr;
872 	__unix_insert_socket(list, sk);
873 
874 out_unlock:
875 	spin_unlock(&unix_table_lock);
876 out_up:
877 	mutex_unlock(&u->readlock);
878 out:
879 	return err;
880 
881 out_mknod_dput:
882 	dput(dentry);
883 out_mknod_unlock:
884 	mutex_unlock(&nd.dentry->d_inode->i_mutex);
885 	path_release(&nd);
886 out_mknod_parent:
887 	if (err==-EEXIST)
888 		err=-EADDRINUSE;
889 	unix_release_addr(addr);
890 	goto out_up;
891 }
892 
893 static void unix_state_double_lock(struct sock *sk1, struct sock *sk2)
894 {
895 	if (unlikely(sk1 == sk2) || !sk2) {
896 		unix_state_lock(sk1);
897 		return;
898 	}
899 	if (sk1 < sk2) {
900 		unix_state_lock(sk1);
901 		unix_state_lock_nested(sk2);
902 	} else {
903 		unix_state_lock(sk2);
904 		unix_state_lock_nested(sk1);
905 	}
906 }
907 
908 static void unix_state_double_unlock(struct sock *sk1, struct sock *sk2)
909 {
910 	if (unlikely(sk1 == sk2) || !sk2) {
911 		unix_state_unlock(sk1);
912 		return;
913 	}
914 	unix_state_unlock(sk1);
915 	unix_state_unlock(sk2);
916 }
917 
918 static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
919 			      int alen, int flags)
920 {
921 	struct sock *sk = sock->sk;
922 	struct sockaddr_un *sunaddr=(struct sockaddr_un*)addr;
923 	struct sock *other;
924 	unsigned hash;
925 	int err;
926 
927 	if (addr->sa_family != AF_UNSPEC) {
928 		err = unix_mkname(sunaddr, alen, &hash);
929 		if (err < 0)
930 			goto out;
931 		alen = err;
932 
933 		if (test_bit(SOCK_PASSCRED, &sock->flags) &&
934 		    !unix_sk(sk)->addr && (err = unix_autobind(sock)) != 0)
935 			goto out;
936 
937 restart:
938 		other=unix_find_other(sunaddr, alen, sock->type, hash, &err);
939 		if (!other)
940 			goto out;
941 
942 		unix_state_double_lock(sk, other);
943 
944 		/* Apparently VFS overslept socket death. Retry. */
945 		if (sock_flag(other, SOCK_DEAD)) {
946 			unix_state_double_unlock(sk, other);
947 			sock_put(other);
948 			goto restart;
949 		}
950 
951 		err = -EPERM;
952 		if (!unix_may_send(sk, other))
953 			goto out_unlock;
954 
955 		err = security_unix_may_send(sk->sk_socket, other->sk_socket);
956 		if (err)
957 			goto out_unlock;
958 
959 	} else {
960 		/*
961 		 *	1003.1g breaking connected state with AF_UNSPEC
962 		 */
963 		other = NULL;
964 		unix_state_double_lock(sk, other);
965 	}
966 
967 	/*
968 	 * If it was connected, reconnect.
969 	 */
970 	if (unix_peer(sk)) {
971 		struct sock *old_peer = unix_peer(sk);
972 		unix_peer(sk)=other;
973 		unix_state_double_unlock(sk, other);
974 
975 		if (other != old_peer)
976 			unix_dgram_disconnected(sk, old_peer);
977 		sock_put(old_peer);
978 	} else {
979 		unix_peer(sk)=other;
980 		unix_state_double_unlock(sk, other);
981 	}
982 	return 0;
983 
984 out_unlock:
985 	unix_state_double_unlock(sk, other);
986 	sock_put(other);
987 out:
988 	return err;
989 }
990 
991 static long unix_wait_for_peer(struct sock *other, long timeo)
992 {
993 	struct unix_sock *u = unix_sk(other);
994 	int sched;
995 	DEFINE_WAIT(wait);
996 
997 	prepare_to_wait_exclusive(&u->peer_wait, &wait, TASK_INTERRUPTIBLE);
998 
999 	sched = !sock_flag(other, SOCK_DEAD) &&
1000 		!(other->sk_shutdown & RCV_SHUTDOWN) &&
1001 		(skb_queue_len(&other->sk_receive_queue) >
1002 		 other->sk_max_ack_backlog);
1003 
1004 	unix_state_unlock(other);
1005 
1006 	if (sched)
1007 		timeo = schedule_timeout(timeo);
1008 
1009 	finish_wait(&u->peer_wait, &wait);
1010 	return timeo;
1011 }
1012 
1013 static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
1014 			       int addr_len, int flags)
1015 {
1016 	struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
1017 	struct sock *sk = sock->sk;
1018 	struct unix_sock *u = unix_sk(sk), *newu, *otheru;
1019 	struct sock *newsk = NULL;
1020 	struct sock *other = NULL;
1021 	struct sk_buff *skb = NULL;
1022 	unsigned hash;
1023 	int st;
1024 	int err;
1025 	long timeo;
1026 
1027 	err = unix_mkname(sunaddr, addr_len, &hash);
1028 	if (err < 0)
1029 		goto out;
1030 	addr_len = err;
1031 
1032 	if (test_bit(SOCK_PASSCRED, &sock->flags)
1033 		&& !u->addr && (err = unix_autobind(sock)) != 0)
1034 		goto out;
1035 
1036 	timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
1037 
1038 	/* First of all allocate resources.
1039 	   If we will make it after state is locked,
1040 	   we will have to recheck all again in any case.
1041 	 */
1042 
1043 	err = -ENOMEM;
1044 
1045 	/* create new sock for complete connection */
1046 	newsk = unix_create1(sk->sk_net, NULL);
1047 	if (newsk == NULL)
1048 		goto out;
1049 
1050 	/* Allocate skb for sending to listening sock */
1051 	skb = sock_wmalloc(newsk, 1, 0, GFP_KERNEL);
1052 	if (skb == NULL)
1053 		goto out;
1054 
1055 restart:
1056 	/*  Find listening sock. */
1057 	other = unix_find_other(sunaddr, addr_len, sk->sk_type, hash, &err);
1058 	if (!other)
1059 		goto out;
1060 
1061 	/* Latch state of peer */
1062 	unix_state_lock(other);
1063 
1064 	/* Apparently VFS overslept socket death. Retry. */
1065 	if (sock_flag(other, SOCK_DEAD)) {
1066 		unix_state_unlock(other);
1067 		sock_put(other);
1068 		goto restart;
1069 	}
1070 
1071 	err = -ECONNREFUSED;
1072 	if (other->sk_state != TCP_LISTEN)
1073 		goto out_unlock;
1074 
1075 	if (skb_queue_len(&other->sk_receive_queue) >
1076 	    other->sk_max_ack_backlog) {
1077 		err = -EAGAIN;
1078 		if (!timeo)
1079 			goto out_unlock;
1080 
1081 		timeo = unix_wait_for_peer(other, timeo);
1082 
1083 		err = sock_intr_errno(timeo);
1084 		if (signal_pending(current))
1085 			goto out;
1086 		sock_put(other);
1087 		goto restart;
1088 	}
1089 
1090 	/* Latch our state.
1091 
1092 	   It is tricky place. We need to grab write lock and cannot
1093 	   drop lock on peer. It is dangerous because deadlock is
1094 	   possible. Connect to self case and simultaneous
1095 	   attempt to connect are eliminated by checking socket
1096 	   state. other is TCP_LISTEN, if sk is TCP_LISTEN we
1097 	   check this before attempt to grab lock.
1098 
1099 	   Well, and we have to recheck the state after socket locked.
1100 	 */
1101 	st = sk->sk_state;
1102 
1103 	switch (st) {
1104 	case TCP_CLOSE:
1105 		/* This is ok... continue with connect */
1106 		break;
1107 	case TCP_ESTABLISHED:
1108 		/* Socket is already connected */
1109 		err = -EISCONN;
1110 		goto out_unlock;
1111 	default:
1112 		err = -EINVAL;
1113 		goto out_unlock;
1114 	}
1115 
1116 	unix_state_lock_nested(sk);
1117 
1118 	if (sk->sk_state != st) {
1119 		unix_state_unlock(sk);
1120 		unix_state_unlock(other);
1121 		sock_put(other);
1122 		goto restart;
1123 	}
1124 
1125 	err = security_unix_stream_connect(sock, other->sk_socket, newsk);
1126 	if (err) {
1127 		unix_state_unlock(sk);
1128 		goto out_unlock;
1129 	}
1130 
1131 	/* The way is open! Fastly set all the necessary fields... */
1132 
1133 	sock_hold(sk);
1134 	unix_peer(newsk)	= sk;
1135 	newsk->sk_state		= TCP_ESTABLISHED;
1136 	newsk->sk_type		= sk->sk_type;
1137 	newsk->sk_peercred.pid	= task_tgid_vnr(current);
1138 	newsk->sk_peercred.uid	= current->euid;
1139 	newsk->sk_peercred.gid	= current->egid;
1140 	newu = unix_sk(newsk);
1141 	newsk->sk_sleep		= &newu->peer_wait;
1142 	otheru = unix_sk(other);
1143 
1144 	/* copy address information from listening to new sock*/
1145 	if (otheru->addr) {
1146 		atomic_inc(&otheru->addr->refcnt);
1147 		newu->addr = otheru->addr;
1148 	}
1149 	if (otheru->dentry) {
1150 		newu->dentry	= dget(otheru->dentry);
1151 		newu->mnt	= mntget(otheru->mnt);
1152 	}
1153 
1154 	/* Set credentials */
1155 	sk->sk_peercred = other->sk_peercred;
1156 
1157 	sock->state	= SS_CONNECTED;
1158 	sk->sk_state	= TCP_ESTABLISHED;
1159 	sock_hold(newsk);
1160 
1161 	smp_mb__after_atomic_inc();	/* sock_hold() does an atomic_inc() */
1162 	unix_peer(sk)	= newsk;
1163 
1164 	unix_state_unlock(sk);
1165 
1166 	/* take ten and and send info to listening sock */
1167 	spin_lock(&other->sk_receive_queue.lock);
1168 	__skb_queue_tail(&other->sk_receive_queue, skb);
1169 	spin_unlock(&other->sk_receive_queue.lock);
1170 	unix_state_unlock(other);
1171 	other->sk_data_ready(other, 0);
1172 	sock_put(other);
1173 	return 0;
1174 
1175 out_unlock:
1176 	if (other)
1177 		unix_state_unlock(other);
1178 
1179 out:
1180 	if (skb)
1181 		kfree_skb(skb);
1182 	if (newsk)
1183 		unix_release_sock(newsk, 0);
1184 	if (other)
1185 		sock_put(other);
1186 	return err;
1187 }
1188 
1189 static int unix_socketpair(struct socket *socka, struct socket *sockb)
1190 {
1191 	struct sock *ska=socka->sk, *skb = sockb->sk;
1192 
1193 	/* Join our sockets back to back */
1194 	sock_hold(ska);
1195 	sock_hold(skb);
1196 	unix_peer(ska)=skb;
1197 	unix_peer(skb)=ska;
1198 	ska->sk_peercred.pid = skb->sk_peercred.pid = task_tgid_vnr(current);
1199 	ska->sk_peercred.uid = skb->sk_peercred.uid = current->euid;
1200 	ska->sk_peercred.gid = skb->sk_peercred.gid = current->egid;
1201 
1202 	if (ska->sk_type != SOCK_DGRAM) {
1203 		ska->sk_state = TCP_ESTABLISHED;
1204 		skb->sk_state = TCP_ESTABLISHED;
1205 		socka->state  = SS_CONNECTED;
1206 		sockb->state  = SS_CONNECTED;
1207 	}
1208 	return 0;
1209 }
1210 
1211 static int unix_accept(struct socket *sock, struct socket *newsock, int flags)
1212 {
1213 	struct sock *sk = sock->sk;
1214 	struct sock *tsk;
1215 	struct sk_buff *skb;
1216 	int err;
1217 
1218 	err = -EOPNOTSUPP;
1219 	if (sock->type!=SOCK_STREAM && sock->type!=SOCK_SEQPACKET)
1220 		goto out;
1221 
1222 	err = -EINVAL;
1223 	if (sk->sk_state != TCP_LISTEN)
1224 		goto out;
1225 
1226 	/* If socket state is TCP_LISTEN it cannot change (for now...),
1227 	 * so that no locks are necessary.
1228 	 */
1229 
1230 	skb = skb_recv_datagram(sk, 0, flags&O_NONBLOCK, &err);
1231 	if (!skb) {
1232 		/* This means receive shutdown. */
1233 		if (err == 0)
1234 			err = -EINVAL;
1235 		goto out;
1236 	}
1237 
1238 	tsk = skb->sk;
1239 	skb_free_datagram(sk, skb);
1240 	wake_up_interruptible(&unix_sk(sk)->peer_wait);
1241 
1242 	/* attach accepted sock to socket */
1243 	unix_state_lock(tsk);
1244 	newsock->state = SS_CONNECTED;
1245 	sock_graft(tsk, newsock);
1246 	unix_state_unlock(tsk);
1247 	return 0;
1248 
1249 out:
1250 	return err;
1251 }
1252 
1253 
1254 static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer)
1255 {
1256 	struct sock *sk = sock->sk;
1257 	struct unix_sock *u;
1258 	struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
1259 	int err = 0;
1260 
1261 	if (peer) {
1262 		sk = unix_peer_get(sk);
1263 
1264 		err = -ENOTCONN;
1265 		if (!sk)
1266 			goto out;
1267 		err = 0;
1268 	} else {
1269 		sock_hold(sk);
1270 	}
1271 
1272 	u = unix_sk(sk);
1273 	unix_state_lock(sk);
1274 	if (!u->addr) {
1275 		sunaddr->sun_family = AF_UNIX;
1276 		sunaddr->sun_path[0] = 0;
1277 		*uaddr_len = sizeof(short);
1278 	} else {
1279 		struct unix_address *addr = u->addr;
1280 
1281 		*uaddr_len = addr->len;
1282 		memcpy(sunaddr, addr->name, *uaddr_len);
1283 	}
1284 	unix_state_unlock(sk);
1285 	sock_put(sk);
1286 out:
1287 	return err;
1288 }
1289 
1290 static void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
1291 {
1292 	int i;
1293 
1294 	scm->fp = UNIXCB(skb).fp;
1295 	skb->destructor = sock_wfree;
1296 	UNIXCB(skb).fp = NULL;
1297 
1298 	for (i=scm->fp->count-1; i>=0; i--)
1299 		unix_notinflight(scm->fp->fp[i]);
1300 }
1301 
1302 static void unix_destruct_fds(struct sk_buff *skb)
1303 {
1304 	struct scm_cookie scm;
1305 	memset(&scm, 0, sizeof(scm));
1306 	unix_detach_fds(&scm, skb);
1307 
1308 	/* Alas, it calls VFS */
1309 	/* So fscking what? fput() had been SMP-safe since the last Summer */
1310 	scm_destroy(&scm);
1311 	sock_wfree(skb);
1312 }
1313 
1314 static void unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
1315 {
1316 	int i;
1317 	for (i=scm->fp->count-1; i>=0; i--)
1318 		unix_inflight(scm->fp->fp[i]);
1319 	UNIXCB(skb).fp = scm->fp;
1320 	skb->destructor = unix_destruct_fds;
1321 	scm->fp = NULL;
1322 }
1323 
1324 /*
1325  *	Send AF_UNIX data.
1326  */
1327 
1328 static int unix_dgram_sendmsg(struct kiocb *kiocb, struct socket *sock,
1329 			      struct msghdr *msg, size_t len)
1330 {
1331 	struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1332 	struct sock *sk = sock->sk;
1333 	struct unix_sock *u = unix_sk(sk);
1334 	struct sockaddr_un *sunaddr=msg->msg_name;
1335 	struct sock *other = NULL;
1336 	int namelen = 0; /* fake GCC */
1337 	int err;
1338 	unsigned hash;
1339 	struct sk_buff *skb;
1340 	long timeo;
1341 	struct scm_cookie tmp_scm;
1342 
1343 	if (NULL == siocb->scm)
1344 		siocb->scm = &tmp_scm;
1345 	err = scm_send(sock, msg, siocb->scm);
1346 	if (err < 0)
1347 		return err;
1348 
1349 	err = -EOPNOTSUPP;
1350 	if (msg->msg_flags&MSG_OOB)
1351 		goto out;
1352 
1353 	if (msg->msg_namelen) {
1354 		err = unix_mkname(sunaddr, msg->msg_namelen, &hash);
1355 		if (err < 0)
1356 			goto out;
1357 		namelen = err;
1358 	} else {
1359 		sunaddr = NULL;
1360 		err = -ENOTCONN;
1361 		other = unix_peer_get(sk);
1362 		if (!other)
1363 			goto out;
1364 	}
1365 
1366 	if (test_bit(SOCK_PASSCRED, &sock->flags)
1367 		&& !u->addr && (err = unix_autobind(sock)) != 0)
1368 		goto out;
1369 
1370 	err = -EMSGSIZE;
1371 	if (len > sk->sk_sndbuf - 32)
1372 		goto out;
1373 
1374 	skb = sock_alloc_send_skb(sk, len, msg->msg_flags&MSG_DONTWAIT, &err);
1375 	if (skb==NULL)
1376 		goto out;
1377 
1378 	memcpy(UNIXCREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
1379 	if (siocb->scm->fp)
1380 		unix_attach_fds(siocb->scm, skb);
1381 	unix_get_secdata(siocb->scm, skb);
1382 
1383 	skb_reset_transport_header(skb);
1384 	err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
1385 	if (err)
1386 		goto out_free;
1387 
1388 	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1389 
1390 restart:
1391 	if (!other) {
1392 		err = -ECONNRESET;
1393 		if (sunaddr == NULL)
1394 			goto out_free;
1395 
1396 		other = unix_find_other(sunaddr, namelen, sk->sk_type,
1397 					hash, &err);
1398 		if (other==NULL)
1399 			goto out_free;
1400 	}
1401 
1402 	unix_state_lock(other);
1403 	err = -EPERM;
1404 	if (!unix_may_send(sk, other))
1405 		goto out_unlock;
1406 
1407 	if (sock_flag(other, SOCK_DEAD)) {
1408 		/*
1409 		 *	Check with 1003.1g - what should
1410 		 *	datagram error
1411 		 */
1412 		unix_state_unlock(other);
1413 		sock_put(other);
1414 
1415 		err = 0;
1416 		unix_state_lock(sk);
1417 		if (unix_peer(sk) == other) {
1418 			unix_peer(sk)=NULL;
1419 			unix_state_unlock(sk);
1420 
1421 			unix_dgram_disconnected(sk, other);
1422 			sock_put(other);
1423 			err = -ECONNREFUSED;
1424 		} else {
1425 			unix_state_unlock(sk);
1426 		}
1427 
1428 		other = NULL;
1429 		if (err)
1430 			goto out_free;
1431 		goto restart;
1432 	}
1433 
1434 	err = -EPIPE;
1435 	if (other->sk_shutdown & RCV_SHUTDOWN)
1436 		goto out_unlock;
1437 
1438 	if (sk->sk_type != SOCK_SEQPACKET) {
1439 		err = security_unix_may_send(sk->sk_socket, other->sk_socket);
1440 		if (err)
1441 			goto out_unlock;
1442 	}
1443 
1444 	if (unix_peer(other) != sk &&
1445 	    (skb_queue_len(&other->sk_receive_queue) >
1446 	     other->sk_max_ack_backlog)) {
1447 		if (!timeo) {
1448 			err = -EAGAIN;
1449 			goto out_unlock;
1450 		}
1451 
1452 		timeo = unix_wait_for_peer(other, timeo);
1453 
1454 		err = sock_intr_errno(timeo);
1455 		if (signal_pending(current))
1456 			goto out_free;
1457 
1458 		goto restart;
1459 	}
1460 
1461 	skb_queue_tail(&other->sk_receive_queue, skb);
1462 	unix_state_unlock(other);
1463 	other->sk_data_ready(other, len);
1464 	sock_put(other);
1465 	scm_destroy(siocb->scm);
1466 	return len;
1467 
1468 out_unlock:
1469 	unix_state_unlock(other);
1470 out_free:
1471 	kfree_skb(skb);
1472 out:
1473 	if (other)
1474 		sock_put(other);
1475 	scm_destroy(siocb->scm);
1476 	return err;
1477 }
1478 
1479 
1480 static int unix_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
1481 			       struct msghdr *msg, size_t len)
1482 {
1483 	struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1484 	struct sock *sk = sock->sk;
1485 	struct sock *other = NULL;
1486 	struct sockaddr_un *sunaddr=msg->msg_name;
1487 	int err,size;
1488 	struct sk_buff *skb;
1489 	int sent=0;
1490 	struct scm_cookie tmp_scm;
1491 
1492 	if (NULL == siocb->scm)
1493 		siocb->scm = &tmp_scm;
1494 	err = scm_send(sock, msg, siocb->scm);
1495 	if (err < 0)
1496 		return err;
1497 
1498 	err = -EOPNOTSUPP;
1499 	if (msg->msg_flags&MSG_OOB)
1500 		goto out_err;
1501 
1502 	if (msg->msg_namelen) {
1503 		err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP;
1504 		goto out_err;
1505 	} else {
1506 		sunaddr = NULL;
1507 		err = -ENOTCONN;
1508 		other = unix_peer(sk);
1509 		if (!other)
1510 			goto out_err;
1511 	}
1512 
1513 	if (sk->sk_shutdown & SEND_SHUTDOWN)
1514 		goto pipe_err;
1515 
1516 	while(sent < len)
1517 	{
1518 		/*
1519 		 *	Optimisation for the fact that under 0.01% of X
1520 		 *	messages typically need breaking up.
1521 		 */
1522 
1523 		size = len-sent;
1524 
1525 		/* Keep two messages in the pipe so it schedules better */
1526 		if (size > ((sk->sk_sndbuf >> 1) - 64))
1527 			size = (sk->sk_sndbuf >> 1) - 64;
1528 
1529 		if (size > SKB_MAX_ALLOC)
1530 			size = SKB_MAX_ALLOC;
1531 
1532 		/*
1533 		 *	Grab a buffer
1534 		 */
1535 
1536 		skb=sock_alloc_send_skb(sk,size,msg->msg_flags&MSG_DONTWAIT, &err);
1537 
1538 		if (skb==NULL)
1539 			goto out_err;
1540 
1541 		/*
1542 		 *	If you pass two values to the sock_alloc_send_skb
1543 		 *	it tries to grab the large buffer with GFP_NOFS
1544 		 *	(which can fail easily), and if it fails grab the
1545 		 *	fallback size buffer which is under a page and will
1546 		 *	succeed. [Alan]
1547 		 */
1548 		size = min_t(int, size, skb_tailroom(skb));
1549 
1550 		memcpy(UNIXCREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
1551 		if (siocb->scm->fp)
1552 			unix_attach_fds(siocb->scm, skb);
1553 
1554 		if ((err = memcpy_fromiovec(skb_put(skb,size), msg->msg_iov, size)) != 0) {
1555 			kfree_skb(skb);
1556 			goto out_err;
1557 		}
1558 
1559 		unix_state_lock(other);
1560 
1561 		if (sock_flag(other, SOCK_DEAD) ||
1562 		    (other->sk_shutdown & RCV_SHUTDOWN))
1563 			goto pipe_err_free;
1564 
1565 		skb_queue_tail(&other->sk_receive_queue, skb);
1566 		unix_state_unlock(other);
1567 		other->sk_data_ready(other, size);
1568 		sent+=size;
1569 	}
1570 
1571 	scm_destroy(siocb->scm);
1572 	siocb->scm = NULL;
1573 
1574 	return sent;
1575 
1576 pipe_err_free:
1577 	unix_state_unlock(other);
1578 	kfree_skb(skb);
1579 pipe_err:
1580 	if (sent==0 && !(msg->msg_flags&MSG_NOSIGNAL))
1581 		send_sig(SIGPIPE,current,0);
1582 	err = -EPIPE;
1583 out_err:
1584 	scm_destroy(siocb->scm);
1585 	siocb->scm = NULL;
1586 	return sent ? : err;
1587 }
1588 
1589 static int unix_seqpacket_sendmsg(struct kiocb *kiocb, struct socket *sock,
1590 				  struct msghdr *msg, size_t len)
1591 {
1592 	int err;
1593 	struct sock *sk = sock->sk;
1594 
1595 	err = sock_error(sk);
1596 	if (err)
1597 		return err;
1598 
1599 	if (sk->sk_state != TCP_ESTABLISHED)
1600 		return -ENOTCONN;
1601 
1602 	if (msg->msg_namelen)
1603 		msg->msg_namelen = 0;
1604 
1605 	return unix_dgram_sendmsg(kiocb, sock, msg, len);
1606 }
1607 
1608 static void unix_copy_addr(struct msghdr *msg, struct sock *sk)
1609 {
1610 	struct unix_sock *u = unix_sk(sk);
1611 
1612 	msg->msg_namelen = 0;
1613 	if (u->addr) {
1614 		msg->msg_namelen = u->addr->len;
1615 		memcpy(msg->msg_name, u->addr->name, u->addr->len);
1616 	}
1617 }
1618 
1619 static int unix_dgram_recvmsg(struct kiocb *iocb, struct socket *sock,
1620 			      struct msghdr *msg, size_t size,
1621 			      int flags)
1622 {
1623 	struct sock_iocb *siocb = kiocb_to_siocb(iocb);
1624 	struct scm_cookie tmp_scm;
1625 	struct sock *sk = sock->sk;
1626 	struct unix_sock *u = unix_sk(sk);
1627 	int noblock = flags & MSG_DONTWAIT;
1628 	struct sk_buff *skb;
1629 	int err;
1630 
1631 	err = -EOPNOTSUPP;
1632 	if (flags&MSG_OOB)
1633 		goto out;
1634 
1635 	msg->msg_namelen = 0;
1636 
1637 	mutex_lock(&u->readlock);
1638 
1639 	skb = skb_recv_datagram(sk, flags, noblock, &err);
1640 	if (!skb) {
1641 		unix_state_lock(sk);
1642 		/* Signal EOF on disconnected non-blocking SEQPACKET socket. */
1643 		if (sk->sk_type == SOCK_SEQPACKET && err == -EAGAIN &&
1644 		    (sk->sk_shutdown & RCV_SHUTDOWN))
1645 			err = 0;
1646 		unix_state_unlock(sk);
1647 		goto out_unlock;
1648 	}
1649 
1650 	wake_up_interruptible_sync(&u->peer_wait);
1651 
1652 	if (msg->msg_name)
1653 		unix_copy_addr(msg, skb->sk);
1654 
1655 	if (size > skb->len)
1656 		size = skb->len;
1657 	else if (size < skb->len)
1658 		msg->msg_flags |= MSG_TRUNC;
1659 
1660 	err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, size);
1661 	if (err)
1662 		goto out_free;
1663 
1664 	if (!siocb->scm) {
1665 		siocb->scm = &tmp_scm;
1666 		memset(&tmp_scm, 0, sizeof(tmp_scm));
1667 	}
1668 	siocb->scm->creds = *UNIXCREDS(skb);
1669 	unix_set_secdata(siocb->scm, skb);
1670 
1671 	if (!(flags & MSG_PEEK))
1672 	{
1673 		if (UNIXCB(skb).fp)
1674 			unix_detach_fds(siocb->scm, skb);
1675 	}
1676 	else
1677 	{
1678 		/* It is questionable: on PEEK we could:
1679 		   - do not return fds - good, but too simple 8)
1680 		   - return fds, and do not return them on read (old strategy,
1681 		     apparently wrong)
1682 		   - clone fds (I chose it for now, it is the most universal
1683 		     solution)
1684 
1685 		   POSIX 1003.1g does not actually define this clearly
1686 		   at all. POSIX 1003.1g doesn't define a lot of things
1687 		   clearly however!
1688 
1689 		*/
1690 		if (UNIXCB(skb).fp)
1691 			siocb->scm->fp = scm_fp_dup(UNIXCB(skb).fp);
1692 	}
1693 	err = size;
1694 
1695 	scm_recv(sock, msg, siocb->scm, flags);
1696 
1697 out_free:
1698 	skb_free_datagram(sk,skb);
1699 out_unlock:
1700 	mutex_unlock(&u->readlock);
1701 out:
1702 	return err;
1703 }
1704 
1705 /*
1706  *	Sleep until data has arrive. But check for races..
1707  */
1708 
1709 static long unix_stream_data_wait(struct sock * sk, long timeo)
1710 {
1711 	DEFINE_WAIT(wait);
1712 
1713 	unix_state_lock(sk);
1714 
1715 	for (;;) {
1716 		prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
1717 
1718 		if (!skb_queue_empty(&sk->sk_receive_queue) ||
1719 		    sk->sk_err ||
1720 		    (sk->sk_shutdown & RCV_SHUTDOWN) ||
1721 		    signal_pending(current) ||
1722 		    !timeo)
1723 			break;
1724 
1725 		set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
1726 		unix_state_unlock(sk);
1727 		timeo = schedule_timeout(timeo);
1728 		unix_state_lock(sk);
1729 		clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
1730 	}
1731 
1732 	finish_wait(sk->sk_sleep, &wait);
1733 	unix_state_unlock(sk);
1734 	return timeo;
1735 }
1736 
1737 
1738 
1739 static int unix_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
1740 			       struct msghdr *msg, size_t size,
1741 			       int flags)
1742 {
1743 	struct sock_iocb *siocb = kiocb_to_siocb(iocb);
1744 	struct scm_cookie tmp_scm;
1745 	struct sock *sk = sock->sk;
1746 	struct unix_sock *u = unix_sk(sk);
1747 	struct sockaddr_un *sunaddr=msg->msg_name;
1748 	int copied = 0;
1749 	int check_creds = 0;
1750 	int target;
1751 	int err = 0;
1752 	long timeo;
1753 
1754 	err = -EINVAL;
1755 	if (sk->sk_state != TCP_ESTABLISHED)
1756 		goto out;
1757 
1758 	err = -EOPNOTSUPP;
1759 	if (flags&MSG_OOB)
1760 		goto out;
1761 
1762 	target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
1763 	timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
1764 
1765 	msg->msg_namelen = 0;
1766 
1767 	/* Lock the socket to prevent queue disordering
1768 	 * while sleeps in memcpy_tomsg
1769 	 */
1770 
1771 	if (!siocb->scm) {
1772 		siocb->scm = &tmp_scm;
1773 		memset(&tmp_scm, 0, sizeof(tmp_scm));
1774 	}
1775 
1776 	mutex_lock(&u->readlock);
1777 
1778 	do
1779 	{
1780 		int chunk;
1781 		struct sk_buff *skb;
1782 
1783 		unix_state_lock(sk);
1784 		skb = skb_dequeue(&sk->sk_receive_queue);
1785 		if (skb==NULL)
1786 		{
1787 			if (copied >= target)
1788 				goto unlock;
1789 
1790 			/*
1791 			 *	POSIX 1003.1g mandates this order.
1792 			 */
1793 
1794 			if ((err = sock_error(sk)) != 0)
1795 				goto unlock;
1796 			if (sk->sk_shutdown & RCV_SHUTDOWN)
1797 				goto unlock;
1798 
1799 			unix_state_unlock(sk);
1800 			err = -EAGAIN;
1801 			if (!timeo)
1802 				break;
1803 			mutex_unlock(&u->readlock);
1804 
1805 			timeo = unix_stream_data_wait(sk, timeo);
1806 
1807 			if (signal_pending(current)) {
1808 				err = sock_intr_errno(timeo);
1809 				goto out;
1810 			}
1811 			mutex_lock(&u->readlock);
1812 			continue;
1813  unlock:
1814 			unix_state_unlock(sk);
1815 			break;
1816 		}
1817 		unix_state_unlock(sk);
1818 
1819 		if (check_creds) {
1820 			/* Never glue messages from different writers */
1821 			if (memcmp(UNIXCREDS(skb), &siocb->scm->creds, sizeof(siocb->scm->creds)) != 0) {
1822 				skb_queue_head(&sk->sk_receive_queue, skb);
1823 				break;
1824 			}
1825 		} else {
1826 			/* Copy credentials */
1827 			siocb->scm->creds = *UNIXCREDS(skb);
1828 			check_creds = 1;
1829 		}
1830 
1831 		/* Copy address just once */
1832 		if (sunaddr)
1833 		{
1834 			unix_copy_addr(msg, skb->sk);
1835 			sunaddr = NULL;
1836 		}
1837 
1838 		chunk = min_t(unsigned int, skb->len, size);
1839 		if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
1840 			skb_queue_head(&sk->sk_receive_queue, skb);
1841 			if (copied == 0)
1842 				copied = -EFAULT;
1843 			break;
1844 		}
1845 		copied += chunk;
1846 		size -= chunk;
1847 
1848 		/* Mark read part of skb as used */
1849 		if (!(flags & MSG_PEEK))
1850 		{
1851 			skb_pull(skb, chunk);
1852 
1853 			if (UNIXCB(skb).fp)
1854 				unix_detach_fds(siocb->scm, skb);
1855 
1856 			/* put the skb back if we didn't use it up.. */
1857 			if (skb->len)
1858 			{
1859 				skb_queue_head(&sk->sk_receive_queue, skb);
1860 				break;
1861 			}
1862 
1863 			kfree_skb(skb);
1864 
1865 			if (siocb->scm->fp)
1866 				break;
1867 		}
1868 		else
1869 		{
1870 			/* It is questionable, see note in unix_dgram_recvmsg.
1871 			 */
1872 			if (UNIXCB(skb).fp)
1873 				siocb->scm->fp = scm_fp_dup(UNIXCB(skb).fp);
1874 
1875 			/* put message back and return */
1876 			skb_queue_head(&sk->sk_receive_queue, skb);
1877 			break;
1878 		}
1879 	} while (size);
1880 
1881 	mutex_unlock(&u->readlock);
1882 	scm_recv(sock, msg, siocb->scm, flags);
1883 out:
1884 	return copied ? : err;
1885 }
1886 
1887 static int unix_shutdown(struct socket *sock, int mode)
1888 {
1889 	struct sock *sk = sock->sk;
1890 	struct sock *other;
1891 
1892 	mode = (mode+1)&(RCV_SHUTDOWN|SEND_SHUTDOWN);
1893 
1894 	if (mode) {
1895 		unix_state_lock(sk);
1896 		sk->sk_shutdown |= mode;
1897 		other=unix_peer(sk);
1898 		if (other)
1899 			sock_hold(other);
1900 		unix_state_unlock(sk);
1901 		sk->sk_state_change(sk);
1902 
1903 		if (other &&
1904 			(sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET)) {
1905 
1906 			int peer_mode = 0;
1907 
1908 			if (mode&RCV_SHUTDOWN)
1909 				peer_mode |= SEND_SHUTDOWN;
1910 			if (mode&SEND_SHUTDOWN)
1911 				peer_mode |= RCV_SHUTDOWN;
1912 			unix_state_lock(other);
1913 			other->sk_shutdown |= peer_mode;
1914 			unix_state_unlock(other);
1915 			other->sk_state_change(other);
1916 			read_lock(&other->sk_callback_lock);
1917 			if (peer_mode == SHUTDOWN_MASK)
1918 				sk_wake_async(other,1,POLL_HUP);
1919 			else if (peer_mode & RCV_SHUTDOWN)
1920 				sk_wake_async(other,1,POLL_IN);
1921 			read_unlock(&other->sk_callback_lock);
1922 		}
1923 		if (other)
1924 			sock_put(other);
1925 	}
1926 	return 0;
1927 }
1928 
1929 static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1930 {
1931 	struct sock *sk = sock->sk;
1932 	long amount=0;
1933 	int err;
1934 
1935 	switch(cmd)
1936 	{
1937 		case SIOCOUTQ:
1938 			amount = atomic_read(&sk->sk_wmem_alloc);
1939 			err = put_user(amount, (int __user *)arg);
1940 			break;
1941 		case SIOCINQ:
1942 		{
1943 			struct sk_buff *skb;
1944 
1945 			if (sk->sk_state == TCP_LISTEN) {
1946 				err = -EINVAL;
1947 				break;
1948 			}
1949 
1950 			spin_lock(&sk->sk_receive_queue.lock);
1951 			if (sk->sk_type == SOCK_STREAM ||
1952 			    sk->sk_type == SOCK_SEQPACKET) {
1953 				skb_queue_walk(&sk->sk_receive_queue, skb)
1954 					amount += skb->len;
1955 			} else {
1956 				skb = skb_peek(&sk->sk_receive_queue);
1957 				if (skb)
1958 					amount=skb->len;
1959 			}
1960 			spin_unlock(&sk->sk_receive_queue.lock);
1961 			err = put_user(amount, (int __user *)arg);
1962 			break;
1963 		}
1964 
1965 		default:
1966 			err = -ENOIOCTLCMD;
1967 			break;
1968 	}
1969 	return err;
1970 }
1971 
1972 static unsigned int unix_poll(struct file * file, struct socket *sock, poll_table *wait)
1973 {
1974 	struct sock *sk = sock->sk;
1975 	unsigned int mask;
1976 
1977 	poll_wait(file, sk->sk_sleep, wait);
1978 	mask = 0;
1979 
1980 	/* exceptional events? */
1981 	if (sk->sk_err)
1982 		mask |= POLLERR;
1983 	if (sk->sk_shutdown == SHUTDOWN_MASK)
1984 		mask |= POLLHUP;
1985 	if (sk->sk_shutdown & RCV_SHUTDOWN)
1986 		mask |= POLLRDHUP;
1987 
1988 	/* readable? */
1989 	if (!skb_queue_empty(&sk->sk_receive_queue) ||
1990 	    (sk->sk_shutdown & RCV_SHUTDOWN))
1991 		mask |= POLLIN | POLLRDNORM;
1992 
1993 	/* Connection-based need to check for termination and startup */
1994 	if ((sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) && sk->sk_state == TCP_CLOSE)
1995 		mask |= POLLHUP;
1996 
1997 	/*
1998 	 * we set writable also when the other side has shut down the
1999 	 * connection. This prevents stuck sockets.
2000 	 */
2001 	if (unix_writable(sk))
2002 		mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
2003 
2004 	return mask;
2005 }
2006 
2007 
2008 #ifdef CONFIG_PROC_FS
2009 static struct sock *unix_seq_idx(int *iter, loff_t pos)
2010 {
2011 	loff_t off = 0;
2012 	struct sock *s;
2013 
2014 	for (s = first_unix_socket(iter); s; s = next_unix_socket(iter, s)) {
2015 		if (off == pos)
2016 			return s;
2017 		++off;
2018 	}
2019 	return NULL;
2020 }
2021 
2022 
2023 static void *unix_seq_start(struct seq_file *seq, loff_t *pos)
2024 {
2025 	spin_lock(&unix_table_lock);
2026 	return *pos ? unix_seq_idx(seq->private, *pos - 1) : ((void *) 1);
2027 }
2028 
2029 static void *unix_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2030 {
2031 	++*pos;
2032 
2033 	if (v == (void *)1)
2034 		return first_unix_socket(seq->private);
2035 	return next_unix_socket(seq->private, v);
2036 }
2037 
2038 static void unix_seq_stop(struct seq_file *seq, void *v)
2039 {
2040 	spin_unlock(&unix_table_lock);
2041 }
2042 
2043 static int unix_seq_show(struct seq_file *seq, void *v)
2044 {
2045 
2046 	if (v == (void *)1)
2047 		seq_puts(seq, "Num       RefCount Protocol Flags    Type St "
2048 			 "Inode Path\n");
2049 	else {
2050 		struct sock *s = v;
2051 		struct unix_sock *u = unix_sk(s);
2052 		unix_state_lock(s);
2053 
2054 		seq_printf(seq, "%p: %08X %08X %08X %04X %02X %5lu",
2055 			s,
2056 			atomic_read(&s->sk_refcnt),
2057 			0,
2058 			s->sk_state == TCP_LISTEN ? __SO_ACCEPTCON : 0,
2059 			s->sk_type,
2060 			s->sk_socket ?
2061 			(s->sk_state == TCP_ESTABLISHED ? SS_CONNECTED : SS_UNCONNECTED) :
2062 			(s->sk_state == TCP_ESTABLISHED ? SS_CONNECTING : SS_DISCONNECTING),
2063 			sock_i_ino(s));
2064 
2065 		if (u->addr) {
2066 			int i, len;
2067 			seq_putc(seq, ' ');
2068 
2069 			i = 0;
2070 			len = u->addr->len - sizeof(short);
2071 			if (!UNIX_ABSTRACT(s))
2072 				len--;
2073 			else {
2074 				seq_putc(seq, '@');
2075 				i++;
2076 			}
2077 			for ( ; i < len; i++)
2078 				seq_putc(seq, u->addr->name->sun_path[i]);
2079 		}
2080 		unix_state_unlock(s);
2081 		seq_putc(seq, '\n');
2082 	}
2083 
2084 	return 0;
2085 }
2086 
2087 static const struct seq_operations unix_seq_ops = {
2088 	.start  = unix_seq_start,
2089 	.next   = unix_seq_next,
2090 	.stop   = unix_seq_stop,
2091 	.show   = unix_seq_show,
2092 };
2093 
2094 
2095 static int unix_seq_open(struct inode *inode, struct file *file)
2096 {
2097 	return seq_open_private(file, &unix_seq_ops, sizeof(int));
2098 }
2099 
2100 static const struct file_operations unix_seq_fops = {
2101 	.owner		= THIS_MODULE,
2102 	.open		= unix_seq_open,
2103 	.read		= seq_read,
2104 	.llseek		= seq_lseek,
2105 	.release	= seq_release_private,
2106 };
2107 
2108 #endif
2109 
2110 static struct net_proto_family unix_family_ops = {
2111 	.family = PF_UNIX,
2112 	.create = unix_create,
2113 	.owner	= THIS_MODULE,
2114 };
2115 
2116 static int __init af_unix_init(void)
2117 {
2118 	int rc = -1;
2119 	struct sk_buff *dummy_skb;
2120 
2121 	BUILD_BUG_ON(sizeof(struct unix_skb_parms) > sizeof(dummy_skb->cb));
2122 
2123 	rc = proto_register(&unix_proto, 1);
2124 	if (rc != 0) {
2125 		printk(KERN_CRIT "%s: Cannot create unix_sock SLAB cache!\n",
2126 		       __FUNCTION__);
2127 		goto out;
2128 	}
2129 
2130 	sock_register(&unix_family_ops);
2131 #ifdef CONFIG_PROC_FS
2132 	proc_net_fops_create(&init_net, "unix", 0, &unix_seq_fops);
2133 #endif
2134 	unix_sysctl_register();
2135 out:
2136 	return rc;
2137 }
2138 
2139 static void __exit af_unix_exit(void)
2140 {
2141 	sock_unregister(PF_UNIX);
2142 	unix_sysctl_unregister();
2143 	proc_net_remove(&init_net, "unix");
2144 	proto_unregister(&unix_proto);
2145 }
2146 
2147 module_init(af_unix_init);
2148 module_exit(af_unix_exit);
2149 
2150 MODULE_LICENSE("GPL");
2151 MODULE_ALIAS_NETPROTO(PF_UNIX);
2152