Import 2.1.122
[davej-history.git] / net / unix / af_unix.c
blob01c8851689d38331d8e32c66a76770147ab110a9
1 /*
2 * NET3: Implementation of BSD Unix domain sockets.
4 * Authors: Alan Cox, <alan.cox@linux.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 * Version: $Id: af_unix.c,v 1.69 1998/08/28 01:15:41 davem Exp $
13 * Fixes:
14 * Linus Torvalds : Assorted bug cures.
15 * Niibe Yutaka : async I/O support.
16 * Carsten Paeth : PF_UNIX check, address fixes.
17 * Alan Cox : Limit size of allocated blocks.
18 * Alan Cox : Fixed the stupid socketpair bug.
19 * Alan Cox : BSD compatibility fine tuning.
20 * Alan Cox : Fixed a bug in connect when interrupted.
21 * Alan Cox : Sorted out a proper draft version of
22 * file descriptor passing hacked up from
23 * Mike Shaver's work.
24 * Marty Leisner : Fixes to fd passing
25 * Nick Nevin : recvmsg bugfix.
26 * Alan Cox : Started proper garbage collector
27 * Heiko EiBfeldt : Missing verify_area check
28 * Alan Cox : Started POSIXisms
29 * Andreas Schwab : Replace inode by dentry for proper
30 * reference counting
31 * Kirk Petersen : Made this a module
32 * Christoph Rohland : Elegant non-blocking accept/connect algorithm.
33 * Lots of bug fixes.
34 * Alexey Kuznetosv : Repaired (I hope) bugs introduces
35 * by above two patches.
37 * Known differences from reference BSD that was tested:
39 * [TO FIX]
40 * ECONNREFUSED is not returned from one end of a connected() socket to the
41 * other the moment one end closes.
42 * fstat() doesn't return st_dev=NODEV, and give the blksize as high water mark
43 * and a fake inode identifier (nor the BSD first socket fstat twice bug).
44 * [NOT TO FIX]
45 * accept() returns a path name even if the connecting socket has closed
46 * in the meantime (BSD loses the path and gives up).
47 * accept() returns 0 length path for an unbound connector. BSD returns 16
48 * and a null first byte in the path (but not for gethost/peername - BSD bug ??)
49 * socketpair(...SOCK_RAW..) doesn't panic the kernel.
50 * BSD af_unix apparently has connect forgetting to block properly.
51 * (need to check this with the POSIX spec in detail)
53 * Differences from 2.0.0-11-... (ANK)
54 * Bug fixes and improvements.
55 * - client shutdown killed server socket.
56 * - removed all useless cli/sti pairs.
58 * Semantic changes/extensions.
59 * - generic control message passing.
60 * - SCM_CREDENTIALS control message.
61 * - "Abstract" (not FS based) socket bindings.
62 * Abstract names are sequences of bytes (not zero terminated)
63 * started by 0, so that this name space does not intersect
64 * with BSD names.
67 #include <linux/module.h>
68 #include <linux/config.h>
69 #include <linux/kernel.h>
70 #include <linux/major.h>
71 #include <linux/signal.h>
72 #include <linux/sched.h>
73 #include <linux/errno.h>
74 #include <linux/string.h>
75 #include <linux/stat.h>
76 #include <linux/socket.h>
77 #include <linux/un.h>
78 #include <linux/fcntl.h>
79 #include <linux/termios.h>
80 #include <linux/socket.h>
81 #include <linux/sockios.h>
82 #include <linux/net.h>
83 #include <linux/in.h>
84 #include <linux/fs.h>
85 #include <linux/malloc.h>
86 #include <asm/uaccess.h>
87 #include <linux/skbuff.h>
88 #include <linux/netdevice.h>
89 #include <net/sock.h>
90 #include <net/tcp.h>
91 #include <net/af_unix.h>
92 #include <linux/proc_fs.h>
93 #include <net/scm.h>
94 #include <linux/init.h>
95 #include <linux/poll.h>
97 #include <asm/checksum.h>
99 #define min(a,b) (((a)<(b))?(a):(b))
101 int sysctl_unix_delete_delay = HZ;
102 int sysctl_unix_destroy_delay =10*HZ;
104 unix_socket *unix_socket_table[UNIX_HASH_SIZE+1];
106 #define unix_sockets_unbound (unix_socket_table[UNIX_HASH_SIZE])
108 #define UNIX_ABSTRACT(sk) ((sk)->protinfo.af_unix.addr->hash!=UNIX_HASH_SIZE)
110 static voidunix_destroy_socket(unix_socket *sk);
111 static voidunix_stream_write_space(struct sock *sk);
113 extern __inline__ unsignedunix_hash_fold(unsigned hash)
115 hash ^= hash>>16;
116 hash ^= hash>>8;
117 hash ^= hash>>4;
118 return hash;
121 #define unix_peer(sk) ((sk)->pair)
123 extern __inline__ intunix_our_peer(unix_socket *sk, unix_socket *osk)
125 returnunix_peer(osk) == sk;
128 extern __inline__ intunix_may_send(unix_socket *sk, unix_socket *osk)
130 return(unix_peer(osk) == NULL ||unix_our_peer(sk, osk));
133 extern __inline__ voidunix_lock(unix_socket *sk)
135 atomic_inc(&sk->sock_readers);
138 extern __inline__ voidunix_unlock(unix_socket *sk)
140 atomic_dec(&sk->sock_readers);
143 extern __inline__ intunix_locked(unix_socket *sk)
145 returnatomic_read(&sk->sock_readers);
148 extern __inline__ voidunix_release_addr(struct unix_address *addr)
150 if(addr)
152 if(atomic_dec_and_test(&addr->refcnt))
153 kfree(addr);
157 static voidunix_destruct_addr(struct sock *sk)
159 struct unix_address *addr = sk->protinfo.af_unix.addr;
161 unix_release_addr(addr);
165 * Check unix socket name:
166 * - should be not zero length.
167 * - if started by not zero, should be NULL terminated (FS object)
168 * - if started by zero, it is abstract name.
171 static intunix_mkname(struct sockaddr_un * sunaddr,int len,unsigned*hashp)
173 if(len <=sizeof(short) || len >sizeof(*sunaddr))
174 return-EINVAL;
175 if(!sunaddr || sunaddr->sun_family != AF_UNIX)
176 return-EINVAL;
177 if(sunaddr->sun_path[0])
180 * This may look like an off by one error but it is
181 * a bit more subtle. 108 is the longest valid AF_UNIX
182 * path for a binding. sun_path[108] doesnt as such
183 * exist. However in kernel space we are guaranteed that
184 * it is a valid memory location in our kernel
185 * address buffer.
187 if(len >sizeof(*sunaddr))
188 len =sizeof(*sunaddr);
189 ((char*)sunaddr)[len]=0;
190 len =strlen(sunaddr->sun_path)+1+sizeof(short);
191 return len;
194 *hashp =unix_hash_fold(csum_partial((char*)sunaddr, len,0));
195 return len;
198 static voidunix_remove_socket(unix_socket *sk)
200 unix_socket **list = sk->protinfo.af_unix.list;
201 if(sk->next)
202 sk->next->prev = sk->prev;
203 if(sk->prev)
204 sk->prev->next = sk->next;
205 if(*list == sk)
206 *list = sk->next;
207 sk->protinfo.af_unix.list = NULL;
208 sk->prev = NULL;
209 sk->next = NULL;
212 static voidunix_insert_socket(unix_socket *sk)
214 unix_socket **list = sk->protinfo.af_unix.list;
215 sk->prev = NULL;
216 sk->next = *list;
217 if(*list)
218 (*list)->prev = sk;
219 *list=sk;
222 static unix_socket *unix_find_socket_byname(struct sockaddr_un *sunname,
223 int len,int type,unsigned hash)
225 unix_socket *s;
227 for(s=unix_socket_table[(hash^type)&0xF]; s; s=s->next)
229 if(s->protinfo.af_unix.addr->len==len &&
230 memcmp(s->protinfo.af_unix.addr->name, sunname, len) ==0&&
231 s->type == type)
233 unix_lock(s);
234 return(s);
237 return(NULL);
240 static unix_socket *unix_find_socket_byinode(struct inode *i)
242 unix_socket *s;
244 for(s=unix_socket_table[i->i_ino &0xF]; s; s=s->next)
246 struct dentry *dentry = s->protinfo.af_unix.dentry;
248 if(dentry && dentry->d_inode == i)
250 unix_lock(s);
251 return(s);
254 return(NULL);
258 * Delete a unix socket. We have to allow for deferring this on a timer.
261 static voidunix_destroy_timer(unsigned long data)
263 unix_socket *sk=(unix_socket *)data;
264 if(!unix_locked(sk) &&atomic_read(&sk->wmem_alloc) ==0)
266 sk_free(sk);
268 /* socket destroyed, decrement count */
269 MOD_DEC_USE_COUNT;
270 return;
274 * Retry;
277 sk->timer.expires=jiffies+sysctl_unix_destroy_delay;/* No real hurry try it every 10 seconds or so */
278 add_timer(&sk->timer);
282 static voidunix_delayed_delete(unix_socket *sk)
284 sk->timer.data=(unsigned long)sk;
285 sk->timer.expires=jiffies+sysctl_unix_delete_delay;/* Normally 1 second after will clean up. After that we try every 10 */
286 sk->timer.function=unix_destroy_timer;
287 add_timer(&sk->timer);
290 static intunix_release_sock(unix_socket *sk)
292 unix_socket *skpair;
294 sk->state_change(sk);
295 sk->dead=1;
296 sk->socket = NULL;
298 skpair=unix_peer(sk);
300 if(skpair!=NULL)
302 if(sk->type==SOCK_STREAM &&unix_our_peer(sk, skpair))
304 skpair->state_change(skpair);
305 skpair->shutdown=SHUTDOWN_MASK;/* No more writes*/
307 unix_unlock(skpair);/* It may now die */
310 /* Try to flush out this socket. Throw out buffers at least */
311 unix_destroy_socket(sk);
314 * Fixme: BSD difference: In BSD all sockets connected to use get
315 * ECONNRESET and we die on the spot. In Linux we behave
316 * like files and pipes do and wait for the last
317 * dereference.
319 * Can't we simply set sock->err?
321 * What the above comment does talk about? --ANK(980817)
324 unix_gc();/* Garbage collect fds */
325 return0;
328 static voidunix_destroy_socket(unix_socket *sk)
330 struct sk_buff *skb;
332 unix_remove_socket(sk);
334 while((skb=skb_dequeue(&sk->receive_queue))!=NULL)
336 if(sk->state==TCP_LISTEN)
337 unix_release_sock(skb->sk);
338 /* passed fds are erased in the kfree_skb hook */
339 kfree_skb(skb);
342 if(sk->protinfo.af_unix.dentry!=NULL)
344 dput(sk->protinfo.af_unix.dentry);
345 sk->protinfo.af_unix.dentry=NULL;
348 if(!unix_locked(sk) &&atomic_read(&sk->wmem_alloc) ==0)
350 sk_free(sk);
352 /* socket destroyed, decrement count */
353 MOD_DEC_USE_COUNT;
355 else
357 sk->state=TCP_CLOSE;
358 sk->dead=1;
359 unix_delayed_delete(sk);/* Try every so often until buffers are all freed */
364 static intunix_listen(struct socket *sock,int backlog)
366 struct sock *sk = sock->sk;
368 if(sock->state != SS_UNCONNECTED)
369 return(-EINVAL);
370 if(sock->type!=SOCK_STREAM)
371 return-EOPNOTSUPP;/* Only stream sockets accept */
372 if(!sk->protinfo.af_unix.addr)
373 return-EINVAL;/* No listens on an unbound socket */
374 sk->max_ack_backlog=backlog;
375 sk->state=TCP_LISTEN;
376 sock->flags |= SO_ACCEPTCON;
377 /* set credentials so connect can copy them */
378 sk->peercred.pid = current->pid;
379 sk->peercred.uid = current->euid;
380 sk->peercred.gid = current->egid;
381 return0;
384 externstruct proto_ops unix_stream_ops;
385 externstruct proto_ops unix_dgram_ops;
387 static struct sock *unix_create1(struct socket *sock,int stream)
389 struct sock *sk;
391 MOD_INC_USE_COUNT;
392 sk =sk_alloc(PF_UNIX, GFP_KERNEL,1);
393 if(!sk) {
394 MOD_DEC_USE_COUNT;
395 return NULL;
398 sock_init_data(sock,sk);
400 if(stream)
401 sk->write_space = unix_stream_write_space;
403 sk->destruct = unix_destruct_addr;
404 sk->protinfo.af_unix.family=PF_UNIX;
405 sk->protinfo.af_unix.dentry=NULL;
406 sk->protinfo.af_unix.readsem=MUTEX;/* single task reading lock */
407 sk->protinfo.af_unix.list=&unix_sockets_unbound;
408 unix_insert_socket(sk);
410 return sk;
413 static intunix_create(struct socket *sock,int protocol)
415 int stream =0;
417 if(protocol && protocol != PF_UNIX)
418 return-EPROTONOSUPPORT;
420 sock->state = SS_UNCONNECTED;
422 switch(sock->type) {
423 case SOCK_STREAM:
424 sock->ops = &unix_stream_ops;
425 stream =1;
426 break;
428 * Believe it or not BSD has AF_UNIX, SOCK_RAW though
429 * nothing uses it.
431 case SOCK_RAW:
432 sock->type=SOCK_DGRAM;
433 case SOCK_DGRAM:
434 sock->ops = &unix_dgram_ops;
435 break;
436 default:
437 return-ESOCKTNOSUPPORT;
440 returnunix_create1(sock, stream) ?0: -ENOMEM;
443 static intunix_release(struct socket *sock,struct socket *peer)
445 unix_socket *sk = sock->sk;
447 if(!sk)
448 return0;
450 sock->sk = NULL;
451 if(sock->state != SS_UNCONNECTED)
452 sock->state = SS_DISCONNECTING;
454 returnunix_release_sock(sk);
457 static intunix_autobind(struct socket *sock)
459 struct sock *sk = sock->sk;
460 static u32 ordernum =1;
461 struct unix_address * addr;
462 unix_socket *osk;
464 addr =kmalloc(sizeof(*addr) +sizeof(short) +16, GFP_KERNEL);
465 if(!addr)
466 return-ENOBUFS;
467 if(sk->protinfo.af_unix.addr || sk->protinfo.af_unix.dentry)
469 kfree(addr);
470 return-EINVAL;
472 memset(addr,0,sizeof(*addr) +sizeof(short) +16);
473 addr->name->sun_family = AF_UNIX;
474 atomic_set(&addr->refcnt,1);
476 retry:
477 addr->len =sprintf(addr->name->sun_path+1,"%08x", ordernum) +1+sizeof(short);
478 addr->hash =unix_hash_fold(csum_partial((void*)addr->name, addr->len,0));
479 ordernum++;
481 if((osk=unix_find_socket_byname(addr->name, addr->len, sock->type,
482 addr->hash)) != NULL)
484 unix_unlock(osk);
485 goto retry;
488 sk->protinfo.af_unix.addr = addr;
489 unix_remove_socket(sk);
490 sk->protinfo.af_unix.list = &unix_socket_table[(addr->hash ^ sk->type)&0xF];
491 unix_insert_socket(sk);
492 return0;
495 static unix_socket *unix_find_other(struct sockaddr_un *sunname,int len,
496 int type,unsigned hash,int*error)
498 unix_socket *u;
500 if(sunname->sun_path[0])
502 struct dentry *dentry;
503 dentry =open_namei(sunname->sun_path,2, S_IFSOCK);
504 if(IS_ERR(dentry)) {
505 *error =PTR_ERR(dentry);
506 return NULL;
508 u=unix_find_socket_byinode(dentry->d_inode);
509 dput(dentry);
510 if(u && u->type != type)
512 *error=-EPROTOTYPE;
513 unix_unlock(u);
514 return NULL;
517 else
518 u=unix_find_socket_byname(sunname, len, type, hash);
520 if(u==NULL)
522 *error=-ECONNREFUSED;
523 return NULL;
525 return u;
529 static intunix_bind(struct socket *sock,struct sockaddr *uaddr,int addr_len)
531 struct sock *sk = sock->sk;
532 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
533 struct dentry * dentry;
534 int err;
535 unsigned hash;
536 struct unix_address *addr;
538 if(sk->protinfo.af_unix.addr || sk->protinfo.af_unix.dentry ||
539 sunaddr->sun_family != AF_UNIX)
540 return-EINVAL;
542 if(addr_len==sizeof(short))
543 returnunix_autobind(sock);
545 addr_len =unix_mkname(sunaddr, addr_len, &hash);
546 if(addr_len <0)
547 return addr_len;
549 addr =kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);
550 if(!addr)
551 return-ENOBUFS;
553 /* We slept; recheck ... */
555 if(sk->protinfo.af_unix.addr || sk->protinfo.af_unix.dentry)
557 kfree(addr);
558 return-EINVAL;/* Already bound */
561 memcpy(addr->name, sunaddr, addr_len);
562 addr->len = addr_len;
563 addr->hash = hash;
564 atomic_set(&addr->refcnt,1);
566 if(!sunaddr->sun_path[0])
568 unix_socket *osk =unix_find_socket_byname(sunaddr, addr_len,
569 sk->type, hash);
570 if(osk)
572 unix_unlock(osk);
573 kfree(addr);
574 return-EADDRINUSE;
576 unix_remove_socket(sk);
577 sk->protinfo.af_unix.addr = addr;
578 sk->protinfo.af_unix.list = &unix_socket_table[(hash^sk->type)&0xF];
579 unix_insert_socket(sk);
580 return0;
583 addr->hash = UNIX_HASH_SIZE;
584 sk->protinfo.af_unix.addr = addr;
587 dentry =do_mknod(sunaddr->sun_path, S_IFSOCK|S_IRWXUGO,0);
588 if(IS_ERR(dentry))
590 err =PTR_ERR(dentry);
591 unix_release_addr(addr);
592 sk->protinfo.af_unix.addr = NULL;
593 if(err==-EEXIST)
594 return-EADDRINUSE;
595 else
596 return err;
598 unix_remove_socket(sk);
599 sk->protinfo.af_unix.list = &unix_socket_table[dentry->d_inode->i_ino &0xF];
600 sk->protinfo.af_unix.dentry = dentry;
601 unix_insert_socket(sk);
603 return0;
606 static intunix_dgram_connect(struct socket *sock,struct sockaddr *addr,
607 int alen,int flags)
609 struct sock *sk = sock->sk;
610 struct sockaddr_un *sunaddr=(struct sockaddr_un*)addr;
611 struct sock *other;
612 unsigned hash;
613 int err;
616 * 1003.1g breaking connected state with AF_UNSPEC
619 if(addr->sa_family==AF_UNSPEC)
621 if(unix_peer(sk))
623 unix_unlock(unix_peer(sk));
624 unix_peer(sk) = NULL;
625 sock->state=SS_UNCONNECTED;
627 return0;
630 alen =unix_mkname(sunaddr, alen, &hash);
631 if(alen <0)
632 return alen;
634 other=unix_find_other(sunaddr, alen, sock->type, hash, &err);
635 if(!other)
636 return err;
637 if(!unix_may_send(sk, other))
639 unix_unlock(other);
640 return-EINVAL;
644 * If it was connected, reconnect.
646 if(unix_peer(sk))
648 unix_unlock(unix_peer(sk));
649 unix_peer(sk)=NULL;
651 unix_peer(sk)=other;
652 if(sock->passcred && !sk->protinfo.af_unix.addr)
653 unix_autobind(sock);
654 return0;
657 static intunix_stream_connect(struct socket *sock,struct sockaddr *uaddr,
658 int addr_len,int flags)
660 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
661 struct sock *sk = sock->sk, *newsk;
662 unix_socket *other;
663 struct sk_buff *skb;
664 int err;
665 unsigned hash;
667 addr_len =unix_mkname(sunaddr, addr_len, &hash);
668 if(addr_len <0)
669 return addr_len;
671 /* First of all allocate resources.
672 If we will make it after state checks,
673 we will have to recheck all again in any case.
676 /* Find listening sock */
677 other=unix_find_other(sunaddr, addr_len, sk->type, hash, &err);
679 /* create new sock for complete connection */
680 newsk =unix_create1(NULL,1);
682 /* Allocate skb for sending to listening sock */
683 skb = NULL;
684 if(newsk)
685 skb =sock_wmalloc(newsk,1,0, GFP_KERNEL);
687 switch(sock->state)
689 case SS_UNCONNECTED:
690 /* This is ok... continue with connect */
691 break;
692 case SS_CONNECTED:
693 /* Socket is already connected */
694 err = -EISCONN;
695 goto out;
696 default:
697 err = -EINVAL;
698 goto out;
701 err = -EINVAL;
702 if(sk->state != TCP_CLOSE)
703 goto out;
705 /* Check that listener is in valid state. */
706 err = -ECONNREFUSED;
707 if(other == NULL || other->dead || other->state != TCP_LISTEN)
708 goto out;
710 err = -ENOMEM;
711 if(newsk == NULL || skb == NULL)
712 goto out;
714 UNIXCB(skb).attr = MSG_SYN;
716 /* set up connecting socket */
717 sock->state=SS_CONNECTED;
718 if(!sk->protinfo.af_unix.addr)
719 unix_autobind(sock);
720 unix_peer(sk)=newsk;
721 unix_lock(sk);
722 sk->state=TCP_ESTABLISHED;
723 /* Set credentials */
724 sk->peercred = other->peercred;
726 /* set up newly created sock */
727 unix_peer(newsk)=sk;
728 unix_lock(newsk);
729 newsk->state=TCP_ESTABLISHED;
730 newsk->type=SOCK_STREAM;
731 newsk->peercred.pid = current->pid;
732 newsk->peercred.uid = current->euid;
733 newsk->peercred.gid = current->egid;
735 /* copy address information from listening to new sock*/
736 if(other->protinfo.af_unix.addr)
738 atomic_inc(&other->protinfo.af_unix.addr->refcnt);
739 newsk->protinfo.af_unix.addr=other->protinfo.af_unix.addr;
741 if(other->protinfo.af_unix.dentry)
742 newsk->protinfo.af_unix.dentry=dget(other->protinfo.af_unix.dentry);
744 /* send info to listening sock */
745 other->ack_backlog++;
746 skb_queue_tail(&other->receive_queue,skb);
747 other->data_ready(other,0);/* Wake up ! */
748 unix_unlock(other);
749 return0;
751 out:
752 if(skb)
753 kfree_skb(skb);
754 if(newsk)
755 unix_destroy_socket(newsk);
756 if(other)
757 unix_unlock(other);
758 return err;
761 static intunix_socketpair(struct socket *socka,struct socket *sockb)
763 struct sock *ska=socka->sk, *skb = sockb->sk;
765 /* Join our sockets back to back */
766 unix_lock(ska);
767 unix_lock(skb);
768 unix_peer(ska)=skb;
769 unix_peer(skb)=ska;
771 if(ska->type != SOCK_DGRAM)
773 ska->state=TCP_ESTABLISHED;
774 skb->state=TCP_ESTABLISHED;
775 socka->state=SS_CONNECTED;
776 sockb->state=SS_CONNECTED;
778 return0;
781 static intunix_accept(struct socket *sock,struct socket *newsock,int flags)
783 unix_socket *sk = sock->sk;
784 unix_socket *newsk = newsock->sk;
785 unix_socket *tsk;
786 struct sk_buff *skb;
788 if(sock->state != SS_UNCONNECTED)
789 return(-EINVAL);
790 if(!(sock->flags & SO_ACCEPTCON))
791 return(-EINVAL);
793 if(sock->type!=SOCK_STREAM)
794 return-EOPNOTSUPP;
795 if(sk->state!=TCP_LISTEN)
796 return-EINVAL;
798 for(;;)
800 skb=skb_dequeue(&sk->receive_queue);
801 if(skb==NULL)
803 if(flags&O_NONBLOCK)
804 return-EAGAIN;
805 interruptible_sleep_on(sk->sleep);
806 if(signal_pending(current))
807 return-ERESTARTSYS;
808 continue;
810 if(!(UNIXCB(skb).attr & MSG_SYN))
812 tsk=skb->sk;
813 tsk->state_change(tsk);
814 kfree_skb(skb);
815 continue;
817 tsk = skb->sk;
818 sk->ack_backlog--;
819 kfree_skb(skb);
820 if(!tsk->dead)
821 break;
822 unix_release_sock(tsk);
826 /* attach accepted sock to socket */
827 newsock->state=SS_CONNECTED;
828 newsock->sk=tsk;
829 tsk->sleep=newsk->sleep;
830 tsk->socket=newsock;
832 /* destroy handed sock */
833 newsk->socket = NULL;
834 unix_destroy_socket(newsk);
836 return0;
840 static intunix_getname(struct socket *sock,struct sockaddr *uaddr,int*uaddr_len,int peer)
842 struct sock *sk = sock->sk;
843 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
845 if(peer)
847 if(!unix_peer(sk))
848 return-ENOTCONN;
849 sk=unix_peer(sk);
851 if(!sk->protinfo.af_unix.addr)
853 sunaddr->sun_family = AF_UNIX;
854 sunaddr->sun_path[0] =0;
855 *uaddr_len =sizeof(short);
856 return0;/* Not bound */
858 *uaddr_len = sk->protinfo.af_unix.addr->len;
859 memcpy(sunaddr, sk->protinfo.af_unix.addr->name, *uaddr_len);
860 return0;
863 static voidunix_detach_fds(struct scm_cookie *scm,struct sk_buff *skb)
865 int i;
867 scm->fp =UNIXCB(skb).fp;
868 skb->destructor = sock_wfree;
869 UNIXCB(skb).fp = NULL;
871 for(i=scm->fp->count-1; i>=0; i--)
872 unix_notinflight(scm->fp->fp[i]);
875 static voidunix_destruct_fds(struct sk_buff *skb)
877 struct scm_cookie scm;
878 memset(&scm,0,sizeof(scm));
879 unix_detach_fds(&scm, skb);
880 scm_destroy(&scm);
881 sock_wfree(skb);
884 static voidunix_attach_fds(struct scm_cookie *scm,struct sk_buff *skb)
886 int i;
887 for(i=scm->fp->count-1; i>=0; i--)
888 unix_inflight(scm->fp->fp[i]);
889 UNIXCB(skb).fp = scm->fp;
890 skb->destructor = unix_destruct_fds;
891 scm->fp = NULL;
896 * Send AF_UNIX data.
899 static intunix_dgram_sendmsg(struct socket *sock,struct msghdr *msg,int len,
900 struct scm_cookie *scm)
902 struct sock *sk = sock->sk;
903 struct sockaddr_un *sunaddr=msg->msg_name;
904 unix_socket *other;
905 int namelen =0;/* fake GCC */
906 int err;
907 unsigned hash;
908 struct sk_buff *skb;
910 if(msg->msg_flags&MSG_OOB)
911 return-EOPNOTSUPP;
913 if(msg->msg_flags&~(MSG_DONTWAIT|MSG_NOSIGNAL))
914 return-EINVAL;
916 if(msg->msg_namelen) {
917 namelen =unix_mkname(sunaddr, msg->msg_namelen, &hash);
918 if(namelen <0)
919 return namelen;
920 }else{
921 sunaddr = NULL;
922 if(!unix_peer(sk))
923 return-ENOTCONN;
926 if(sock->passcred && !sk->protinfo.af_unix.addr)
927 unix_autobind(sock);
929 skb =sock_alloc_send_skb(sk, len,0, msg->msg_flags&MSG_DONTWAIT, &err);
930 if(skb==NULL)
931 goto out;
933 memcpy(UNIXCREDS(skb), &scm->creds,sizeof(struct ucred));
934 UNIXCB(skb).attr = msg->msg_flags;
935 if(scm->fp)
936 unix_attach_fds(scm, skb);
938 skb->h.raw = skb->data;
939 err =memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
940 if(err)
941 goto out_free;
943 other =unix_peer(sk);
944 if(other && other->dead)
947 * Check with 1003.1g - what should
948 * datagram error
950 unix_unlock(other);
951 unix_peer(sk)=NULL;
952 other = NULL;
953 err = -ECONNRESET;
954 if(sunaddr == NULL)
955 goto out_free;
957 if(!other)
959 other =unix_find_other(sunaddr, namelen, sk->type, hash, &err);
960 if(other==NULL)
961 goto out_free;
962 err = -EINVAL;
963 if(!unix_may_send(sk, other))
964 goto out_unlock;
967 skb_queue_tail(&other->receive_queue, skb);
968 other->data_ready(other,len);
970 if(!unix_peer(sk))
971 unix_unlock(other);
972 return len;
974 out_unlock:
975 unix_unlock(other);
976 out_free:
977 kfree_skb(skb);
978 out:
979 return err;
983 static intunix_stream_sendmsg(struct socket *sock,struct msghdr *msg,int len,
984 struct scm_cookie *scm)
986 struct sock *sk = sock->sk;
987 unix_socket *other;
988 struct sockaddr_un *sunaddr=msg->msg_name;
989 int err,size;
990 struct sk_buff *skb;
991 int limit=0;
992 int sent=0;
994 if(sock->flags & SO_ACCEPTCON)
995 return(-EINVAL);
997 if(msg->msg_flags&MSG_OOB)
998 return-EOPNOTSUPP;
1000 if(msg->msg_flags&~(MSG_DONTWAIT|MSG_NOSIGNAL))
1001 return-EINVAL;
1003 if(msg->msg_namelen) {
1004 if(sk->state==TCP_ESTABLISHED)
1005 return-EISCONN;
1006 else
1007 return-EOPNOTSUPP;
1008 }else{
1009 sunaddr = NULL;
1010 if(!unix_peer(sk))
1011 return-ENOTCONN;
1014 if(sk->shutdown&SEND_SHUTDOWN) {
1015 if(!(msg->msg_flags&MSG_NOSIGNAL))
1016 send_sig(SIGPIPE,current,0);
1017 return-EPIPE;
1020 while(sent < len)
1023 * Optimisation for the fact that under 0.01% of X messages typically
1024 * need breaking up.
1027 size=len-sent;
1029 /* Keep two messages in the pipe so it schedules better */
1030 if(size > sk->sndbuf/2-16)
1031 size = sk->sndbuf/2-16;
1034 * Keep to page sized kmalloc()'s as various people
1035 * have suggested. Big mallocs stress the vm too
1036 * much.
1039 if(size >4096-16)
1040 limit =4096-16;/* Fall back to a page if we can't grab a big buffer this instant */
1041 else
1042 limit =0;/* Otherwise just grab and wait */
1045 * Grab a buffer
1048 skb=sock_alloc_send_skb(sk,size,limit,msg->msg_flags&MSG_DONTWAIT, &err);
1050 if(skb==NULL)
1052 if(sent)
1053 goto out;
1054 return err;
1058 * If you pass two values to the sock_alloc_send_skb
1059 * it tries to grab the large buffer with GFP_BUFFER
1060 * (which can fail easily), and if it fails grab the
1061 * fallback size buffer which is under a page and will
1062 * succeed. [Alan]
1064 size =min(size,skb_tailroom(skb));
1066 memcpy(UNIXCREDS(skb), &scm->creds,sizeof(struct ucred));
1067 UNIXCB(skb).attr = msg->msg_flags;
1068 if(scm->fp)
1069 unix_attach_fds(scm, skb);
1071 if(memcpy_fromiovec(skb_put(skb,size), msg->msg_iov, size)) {
1072 kfree_skb(skb);
1073 if(sent)
1074 goto out;
1075 return-EFAULT;
1078 other=unix_peer(sk);
1080 if(other->dead || (sk->shutdown & SEND_SHUTDOWN))
1082 kfree_skb(skb);
1083 if(sent)
1084 goto out;
1085 if(!(msg->msg_flags&MSG_NOSIGNAL))
1086 send_sig(SIGPIPE,current,0);
1087 return-EPIPE;
1090 skb_queue_tail(&other->receive_queue, skb);
1091 other->data_ready(other,size);
1092 sent+=size;
1094 out:
1095 return sent;
1099 * Sleep until data has arrive. But check for races..
1102 static voidunix_data_wait(unix_socket * sk)
1104 if(!skb_peek(&sk->receive_queue))
1106 sk->socket->flags |= SO_WAITDATA;
1107 interruptible_sleep_on(sk->sleep);
1108 sk->socket->flags &= ~SO_WAITDATA;
1112 static intunix_dgram_recvmsg(struct socket *sock,struct msghdr *msg,int size,
1113 int flags,struct scm_cookie *scm)
1115 struct sock *sk = sock->sk;
1116 int noblock = flags & MSG_DONTWAIT;
1117 struct sk_buff *skb;
1118 int err;
1120 if(flags&MSG_OOB)
1121 return-EOPNOTSUPP;
1123 msg->msg_namelen =0;
1125 skb =skb_recv_datagram(sk, flags, noblock, &err);
1126 if(!skb)
1127 goto out;
1129 if(msg->msg_name)
1131 msg->msg_namelen =sizeof(short);
1132 if(skb->sk->protinfo.af_unix.addr)
1134 msg->msg_namelen=skb->sk->protinfo.af_unix.addr->len;
1135 memcpy(msg->msg_name,
1136 skb->sk->protinfo.af_unix.addr->name,
1137 skb->sk->protinfo.af_unix.addr->len);
1141 if(size > skb->len)
1142 size = skb->len;
1143 else if(size < skb->len)
1144 msg->msg_flags |= MSG_TRUNC;
1146 err =skb_copy_datagram_iovec(skb,0, msg->msg_iov, size);
1147 if(err)
1148 goto out_free;
1150 scm->creds = *UNIXCREDS(skb);
1152 if(!(flags & MSG_PEEK))
1154 if(UNIXCB(skb).fp)
1155 unix_detach_fds(scm, skb);
1157 else
1159 /* It is questionable: on PEEK we could:
1160 - do not return fds - good, but too simple 8)
1161 - return fds, and do not return them on read (old strategy,
1162 apparently wrong)
1163 - clone fds (I choosed it for now, it is the most universal
1164 solution)
1166 POSIX 1003.1g does not actually define this clearly
1167 at all. POSIX 1003.1g doesn't define a lot of things
1168 clearly however!
1171 if(UNIXCB(skb).fp)
1172 scm->fp =scm_fp_dup(UNIXCB(skb).fp);
1174 err = size;
1176 out_free:
1177 skb_free_datagram(sk,skb);
1178 out:
1179 return err;
1183 static intunix_stream_recvmsg(struct socket *sock,struct msghdr *msg,int size,
1184 int flags,struct scm_cookie *scm)
1186 struct sock *sk = sock->sk;
1187 int noblock = flags & MSG_DONTWAIT;
1188 struct sockaddr_un *sunaddr=msg->msg_name;
1189 int copied =0;
1190 int check_creds =0;
1191 int target =1;
1193 if(sock->flags & SO_ACCEPTCON)
1194 return(-EINVAL);
1196 if(flags&MSG_OOB)
1197 return-EOPNOTSUPP;
1198 if(flags&MSG_WAITALL)
1199 target = size;
1202 msg->msg_namelen =0;
1204 /* Lock the socket to prevent queue disordering
1205 * while sleeps in memcpy_tomsg
1208 down(&sk->protinfo.af_unix.readsem);
1212 int chunk;
1213 struct sk_buff *skb;
1215 skb=skb_dequeue(&sk->receive_queue);
1216 if(skb==NULL)
1218 if(copied >= target)
1219 break;
1222 * POSIX 1003.1g mandates this order.
1225 if(sk->err)
1227 up(&sk->protinfo.af_unix.readsem);
1228 returnsock_error(sk);
1231 if(sk->shutdown & RCV_SHUTDOWN)
1232 break;
1233 up(&sk->protinfo.af_unix.readsem);
1234 if(noblock)
1235 return-EAGAIN;
1236 unix_data_wait(sk);
1237 if(signal_pending(current))
1238 return-ERESTARTSYS;
1239 down(&sk->protinfo.af_unix.readsem);
1240 continue;
1243 /* Never glue messages from different writers */
1244 if(check_creds &&
1245 memcmp(UNIXCREDS(skb), &scm->creds,sizeof(scm->creds)) !=0)
1247 skb_queue_head(&sk->receive_queue, skb);
1248 break;
1251 /* Copy address just once */
1252 if(sunaddr)
1254 msg->msg_namelen =sizeof(short);
1255 if(skb->sk->protinfo.af_unix.addr)
1257 msg->msg_namelen=skb->sk->protinfo.af_unix.addr->len;
1258 memcpy(sunaddr,
1259 skb->sk->protinfo.af_unix.addr->name,
1260 skb->sk->protinfo.af_unix.addr->len);
1262 sunaddr = NULL;
1265 chunk =min(skb->len, size);
1266 if(memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
1267 skb_queue_head(&sk->receive_queue, skb);
1268 if(copied ==0)
1269 copied = -EFAULT;
1270 break;
1272 copied += chunk;
1273 size -= chunk;
1275 /* Copy credentials */
1276 scm->creds = *UNIXCREDS(skb);
1277 check_creds =1;
1279 /* Mark read part of skb as used */
1280 if(!(flags & MSG_PEEK))
1282 skb_pull(skb, chunk);
1284 if(UNIXCB(skb).fp)
1285 unix_detach_fds(scm, skb);
1287 /* put the skb back if we didn't use it up.. */
1288 if(skb->len)
1290 skb_queue_head(&sk->receive_queue, skb);
1291 break;
1294 kfree_skb(skb);
1296 if(scm->fp)
1297 break;
1299 else
1301 /* It is questionable, see note in unix_dgram_recvmsg.
1304 if(UNIXCB(skb).fp)
1305 scm->fp =scm_fp_dup(UNIXCB(skb).fp);
1307 /* put message back and return */
1308 skb_queue_head(&sk->receive_queue, skb);
1309 break;
1311 }while(size);
1313 up(&sk->protinfo.af_unix.readsem);
1314 return copied;
1317 static intunix_shutdown(struct socket *sock,int mode)
1319 struct sock *sk = sock->sk;
1320 unix_socket *other=unix_peer(sk);
1322 mode = (mode+1)&(RCV_SHUTDOWN|SEND_SHUTDOWN);
1324 if(mode) {
1325 sk->shutdown |= mode;
1326 sk->state_change(sk);
1327 if(other && sk->type == SOCK_STREAM &&
1328 unix_our_peer(sk, other)) {
1329 int peer_mode =0;
1331 if(mode&RCV_SHUTDOWN)
1332 peer_mode |= SEND_SHUTDOWN;
1333 if(mode&SEND_SHUTDOWN)
1334 peer_mode |= RCV_SHUTDOWN;
1335 other->shutdown |= mode;
1336 other->state_change(other);
1339 return0;
1343 static intunix_ioctl(struct socket *sock,unsigned int cmd,unsigned long arg)
1345 struct sock *sk = sock->sk;
1346 long amount=0;
1348 switch(cmd)
1351 case TIOCOUTQ:
1352 amount = sk->sndbuf -atomic_read(&sk->wmem_alloc);
1353 if(amount<0)
1354 amount=0;
1355 returnput_user(amount, (int*)arg);
1356 case TIOCINQ:
1358 struct sk_buff *skb;
1359 if(sk->state==TCP_LISTEN)
1360 return-EINVAL;
1362 * These two are safe on current systems as
1363 * only user tasks fiddle here
1365 if((skb=skb_peek(&sk->receive_queue))!=NULL)
1366 amount=skb->len;
1367 returnput_user(amount, (int*)arg);
1370 default:
1371 return-EINVAL;
1373 /*NOTREACHED*/
1374 return(0);
1377 static unsigned intunix_poll(struct file * file,struct socket *sock, poll_table *wait)
1379 struct sock *sk = sock->sk;
1380 unsigned int mask;
1382 poll_wait(file, sk->sleep, wait);
1383 mask =0;
1385 /* exceptional events? */
1386 if(sk->err)
1387 mask |= POLLERR;
1388 if(sk->shutdown & RCV_SHUTDOWN)
1389 mask |= POLLHUP;
1391 /* readable? */
1392 if(!skb_queue_empty(&sk->receive_queue))
1393 mask |= POLLIN | POLLRDNORM;
1395 /* Connection-based need to check for termination and startup */
1396 if(sk->type == SOCK_STREAM && sk->state==TCP_CLOSE)
1397 mask |= POLLHUP;
1400 * we set writable also when the other side has shut down the
1401 * connection. This prevents stuck sockets.
1403 if(sk->sndbuf - (int)atomic_read(&sk->wmem_alloc) >= MIN_WRITE_SPACE)
1404 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1406 return mask;
1409 static voidunix_stream_write_space(struct sock *sk)
1411 if(sk->dead)
1412 return;
1413 wake_up_interruptible(sk->sleep);
1414 if(sk->sndbuf - (int)atomic_read(&sk->wmem_alloc) >= MIN_WRITE_SPACE)
1415 sock_wake_async(sk->socket,2);
1418 #ifdef CONFIG_PROC_FS
1419 static intunix_read_proc(char*buffer,char**start, off_t offset,
1420 int length,int*eof,void*data)
1422 off_t pos=0;
1423 off_t begin=0;
1424 int len=0;
1425 int i;
1426 unix_socket *s;
1428 len+=sprintf(buffer,"Num RefCount Protocol Flags Type St "
1429 "Inode Path\n");
1431 forall_unix_sockets(i,s)
1433 len+=sprintf(buffer+len,"%p: %08X %08X %08lX %04X %02X %5ld",
1435 atomic_read(&s->sock_readers),
1437 s->socket ? s->socket->flags :0,
1438 s->type,
1439 s->socket ? s->socket->state :
1440 (s->state == TCP_ESTABLISHED ?
1441 SS_CONNECTING : SS_DISCONNECTING),
1442 s->socket ? s->socket->inode->i_ino :0);
1444 if(s->protinfo.af_unix.addr)
1446 buffer[len++] =' ';
1447 memcpy(buffer+len, s->protinfo.af_unix.addr->name->sun_path,
1448 s->protinfo.af_unix.addr->len-sizeof(short));
1449 if(!UNIX_ABSTRACT(s))
1450 len--;
1451 else
1452 buffer[len] ='@';
1453 len += s->protinfo.af_unix.addr->len -sizeof(short);
1455 buffer[len++]='\n';
1457 pos = begin + len;
1458 if(pos<offset)
1460 len=0;
1461 begin=pos;
1463 if(pos>offset+length)
1464 goto done;
1466 *eof =1;
1467 done:
1468 *start=buffer+(offset-begin);
1469 len-=(offset-begin);
1470 if(len>length)
1471 len=length;
1472 if(len <0)
1473 len =0;
1474 return len;
1476 #endif
1478 struct proto_ops unix_stream_ops = {
1479 PF_UNIX,
1481 sock_no_dup,
1482 unix_release,
1483 unix_bind,
1484 unix_stream_connect,
1485 unix_socketpair,
1486 unix_accept,
1487 unix_getname,
1488 unix_poll,
1489 unix_ioctl,
1490 unix_listen,
1491 unix_shutdown,
1492 sock_no_setsockopt,
1493 sock_no_getsockopt,
1494 sock_no_fcntl,
1495 unix_stream_sendmsg,
1496 unix_stream_recvmsg
1499 struct proto_ops unix_dgram_ops = {
1500 PF_UNIX,
1502 sock_no_dup,
1503 unix_release,
1504 unix_bind,
1505 unix_dgram_connect,
1506 unix_socketpair,
1507 sock_no_accept,
1508 unix_getname,
1509 datagram_poll,
1510 unix_ioctl,
1511 sock_no_listen,
1512 unix_shutdown,
1513 sock_no_setsockopt,
1514 sock_no_getsockopt,
1515 sock_no_fcntl,
1516 unix_dgram_sendmsg,
1517 unix_dgram_recvmsg
1520 struct net_proto_family unix_family_ops = {
1521 PF_UNIX,
1522 unix_create
1525 #ifdef MODULE
1526 #ifdef CONFIG_SYSCTL
1527 externvoidunix_sysctl_register(void);
1528 externvoidunix_sysctl_unregister(void);
1529 #endif
1531 intinit_module(void)
1532 #else
1533 __initfunc(voidunix_proto_init(struct net_proto *pro))
1534 #endif
1536 struct sk_buff *dummy_skb;
1537 struct proc_dir_entry *ent;
1539 printk(KERN_INFO "NET3: Unix domain sockets 0.16 for Linux NET3.038.\n");
1540 if(sizeof(struct unix_skb_parms) >sizeof(dummy_skb->cb))
1542 printk(KERN_CRIT "unix_proto_init: panic\n");
1543 #ifdef MODULE
1544 return-1;
1545 #else
1546 return;
1547 #endif
1549 sock_register(&unix_family_ops);
1550 #ifdef CONFIG_PROC_FS
1551 ent =create_proc_entry("net/unix",0,0);
1552 ent->read_proc = unix_read_proc;
1553 #endif
1555 #ifdef MODULE
1556 #ifdef CONFIG_SYSCTL
1557 unix_sysctl_register();
1558 #endif
1560 return0;
1561 #endif
1564 #ifdef MODULE
1565 voidcleanup_module(void)
1567 sock_unregister(PF_UNIX);
1568 #ifdef CONFIG_SYSCTL
1569 unix_sysctl_unregister();
1570 #endif
1571 #ifdef CONFIG_PROC_FS
1572 remove_proc_entry("net/unix",0);
1573 #endif
1575 #endif
1578 * Local variables:
1579 * compile-command: "gcc -g -D__KERNEL__ -Wall -O6 -I/usr/src/linux/include -c af_unix.c"
1580 * End:
close