Import 2.2.5pre2
[davej-history.git] / net / unix / af_unix.c
blobc7f594fbba67ab35e333a2a5cc701828b25cf62e
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.75 1999/03/22 05:02:45 davem Exp $
13 * Fixes:
14 * Linus Torvalds : Assorted bug cures.
15 * Niibe Yutaka : async I/O support.
16 * Carsten Paeth : PF_UNIX check, address fixes.
17 * Alan Cox : Limit size of allocated blocks.
18 * Alan Cox : Fixed the stupid socketpair bug.
19 * Alan Cox : BSD compatibility fine tuning.
20 * Alan Cox : Fixed a bug in connect when interrupted.
21 * Alan Cox : Sorted out a proper draft version of
22 * file descriptor passing hacked up from
23 * Mike Shaver's work.
24 * Marty Leisner : Fixes to fd passing
25 * Nick Nevin : recvmsg bugfix.
26 * Alan Cox : Started proper garbage collector
27 * Heiko EiBfeldt : Missing verify_area check
28 * Alan Cox : Started POSIXisms
29 * Andreas Schwab : Replace inode by dentry for proper
30 * reference counting
31 * Kirk Petersen : Made this a module
32 * Christoph Rohland : Elegant non-blocking accept/connect algorithm.
33 * Lots of bug fixes.
34 * Alexey Kuznetosv : Repaired (I hope) bugs introduces
35 * by above two patches.
36 * Andrea Arcangeli : If possible we block in connect(2)
37 * if the max backlog of the listen socket
38 * is been reached. This won't break
39 * old apps and it will avoid huge amount
40 * of socks hashed (this for unix_gc()
41 * performances reasons).
42 * Security fix that limits the max
43 * number of socks to 2*max_files and
44 * the number of skb queueable in the
45 * dgram receiver.
47 * Known differences from reference BSD that was tested:
49 * [TO FIX]
50 * ECONNREFUSED is not returned from one end of a connected() socket to the
51 * other the moment one end closes.
52 * fstat() doesn't return st_dev=NODEV, and give the blksize as high water mark
53 * and a fake inode identifier (nor the BSD first socket fstat twice bug).
54 * [NOT TO FIX]
55 * accept() returns a path name even if the connecting socket has closed
56 * in the meantime (BSD loses the path and gives up).
57 * accept() returns 0 length path for an unbound connector. BSD returns 16
58 * and a null first byte in the path (but not for gethost/peername - BSD bug ??)
59 * socketpair(...SOCK_RAW..) doesn't panic the kernel.
60 * BSD af_unix apparently has connect forgetting to block properly.
61 * (need to check this with the POSIX spec in detail)
63 * Differences from 2.0.0-11-... (ANK)
64 * Bug fixes and improvements.
65 * - client shutdown killed server socket.
66 * - removed all useless cli/sti pairs.
68 * Semantic changes/extensions.
69 * - generic control message passing.
70 * - SCM_CREDENTIALS control message.
71 * - "Abstract" (not FS based) socket bindings.
72 * Abstract names are sequences of bytes (not zero terminated)
73 * started by 0, so that this name space does not intersect
74 * with BSD names.
77 #include <linux/module.h>
78 #include <linux/config.h>
79 #include <linux/kernel.h>
80 #include <linux/major.h>
81 #include <linux/signal.h>
82 #include <linux/sched.h>
83 #include <linux/errno.h>
84 #include <linux/string.h>
85 #include <linux/stat.h>
86 #include <linux/socket.h>
87 #include <linux/un.h>
88 #include <linux/fcntl.h>
89 #include <linux/termios.h>
90 #include <linux/socket.h>
91 #include <linux/sockios.h>
92 #include <linux/net.h>
93 #include <linux/in.h>
94 #include <linux/fs.h>
95 #include <linux/malloc.h>
96 #include <asm/uaccess.h>
97 #include <linux/skbuff.h>
98 #include <linux/netdevice.h>
99 #include <net/sock.h>
100 #include <net/tcp.h>
101 #include <net/af_unix.h>
102 #include <linux/proc_fs.h>
103 #include <net/scm.h>
104 #include <linux/init.h>
105 #include <linux/poll.h>
107 #include <asm/checksum.h>
109 #define min(a,b) (((a)<(b))?(a):(b))
111 int sysctl_unix_delete_delay = HZ;
112 int sysctl_unix_destroy_delay =10*HZ;
113 int sysctl_unix_max_dgram_qlen =10;
115 unix_socket *unix_socket_table[UNIX_HASH_SIZE+1];
116 static atomic_t unix_nr_socks =ATOMIC_INIT(0);
117 static struct wait_queue * unix_ack_wqueue = NULL;
118 static struct wait_queue * unix_dgram_wqueue = NULL;
120 #define unix_sockets_unbound (unix_socket_table[UNIX_HASH_SIZE])
122 #define UNIX_ABSTRACT(sk) ((sk)->protinfo.af_unix.addr->hash!=UNIX_HASH_SIZE)
124 static voidunix_destroy_socket(unix_socket *sk);
125 static voidunix_stream_write_space(struct sock *sk);
127 extern __inline__ unsignedunix_hash_fold(unsigned hash)
129 hash ^= hash>>16;
130 hash ^= hash>>8;
131 hash ^= hash>>4;
132 return hash;
135 #define unix_peer(sk) ((sk)->pair)
137 extern __inline__ intunix_our_peer(unix_socket *sk, unix_socket *osk)
139 returnunix_peer(osk) == sk;
142 extern __inline__ intunix_may_send(unix_socket *sk, unix_socket *osk)
144 return(unix_peer(osk) == NULL ||unix_our_peer(sk, osk));
147 extern __inline__ voidunix_lock(unix_socket *sk)
149 atomic_inc(&sk->sock_readers);
152 extern __inline__ voidunix_unlock(unix_socket *sk)
154 atomic_dec(&sk->sock_readers);
157 extern __inline__ intunix_locked(unix_socket *sk)
159 returnatomic_read(&sk->sock_readers);
162 extern __inline__ voidunix_release_addr(struct unix_address *addr)
164 if(addr)
166 if(atomic_dec_and_test(&addr->refcnt))
167 kfree(addr);
171 static voidunix_destruct_addr(struct sock *sk)
173 struct unix_address *addr = sk->protinfo.af_unix.addr;
175 unix_release_addr(addr);
179 * Check unix socket name:
180 * - should be not zero length.
181 * - if started by not zero, should be NULL terminated (FS object)
182 * - if started by zero, it is abstract name.
185 static intunix_mkname(struct sockaddr_un * sunaddr,int len,unsigned*hashp)
187 if(len <=sizeof(short) || len >sizeof(*sunaddr))
188 return-EINVAL;
189 if(!sunaddr || sunaddr->sun_family != AF_UNIX)
190 return-EINVAL;
191 if(sunaddr->sun_path[0])
194 * This may look like an off by one error but it is
195 * a bit more subtle. 108 is the longest valid AF_UNIX
196 * path for a binding. sun_path[108] doesnt as such
197 * exist. However in kernel space we are guaranteed that
198 * it is a valid memory location in our kernel
199 * address buffer.
201 if(len >sizeof(*sunaddr))
202 len =sizeof(*sunaddr);
203 ((char*)sunaddr)[len]=0;
204 len =strlen(sunaddr->sun_path)+1+sizeof(short);
205 return len;
208 *hashp =unix_hash_fold(csum_partial((char*)sunaddr, len,0));
209 return len;
212 static voidunix_remove_socket(unix_socket *sk)
214 unix_socket **list = sk->protinfo.af_unix.list;
215 if(sk->next)
216 sk->next->prev = sk->prev;
217 if(sk->prev)
218 sk->prev->next = sk->next;
219 if(*list == sk)
220 *list = sk->next;
221 sk->protinfo.af_unix.list = NULL;
222 sk->prev = NULL;
223 sk->next = NULL;
226 static voidunix_insert_socket(unix_socket *sk)
228 unix_socket **list = sk->protinfo.af_unix.list;
229 sk->prev = NULL;
230 sk->next = *list;
231 if(*list)
232 (*list)->prev = sk;
233 *list=sk;
236 static unix_socket *unix_find_socket_byname(struct sockaddr_un *sunname,
237 int len,int type,unsigned hash)
239 unix_socket *s;
241 for(s=unix_socket_table[(hash^type)&0xF]; s; s=s->next)
243 if(s->protinfo.af_unix.addr->len==len &&
244 memcmp(s->protinfo.af_unix.addr->name, sunname, len) ==0&&
245 s->type == type)
247 unix_lock(s);
248 return(s);
251 return(NULL);
254 static unix_socket *unix_find_socket_byinode(struct inode *i)
256 unix_socket *s;
258 for(s=unix_socket_table[i->i_ino &0xF]; s; s=s->next)
260 struct dentry *dentry = s->protinfo.af_unix.dentry;
262 if(dentry && dentry->d_inode == i)
264 unix_lock(s);
265 return(s);
268 return(NULL);
272 * Delete a unix socket. We have to allow for deferring this on a timer.
275 static voidunix_destroy_timer(unsigned long data)
277 unix_socket *sk=(unix_socket *)data;
278 if(!unix_locked(sk) &&atomic_read(&sk->wmem_alloc) ==0)
280 atomic_dec(&unix_nr_socks);
282 sk_free(sk);
284 /* socket destroyed, decrement count */
285 MOD_DEC_USE_COUNT;
286 return;
290 * Retry;
293 sk->timer.expires=jiffies+sysctl_unix_destroy_delay;/* No real hurry try it every 10 seconds or so */
294 add_timer(&sk->timer);
298 static voidunix_delayed_delete(unix_socket *sk)
300 sk->timer.data=(unsigned long)sk;
301 sk->timer.expires=jiffies+sysctl_unix_delete_delay;/* Normally 1 second after will clean up. After that we try every 10 */
302 sk->timer.function=unix_destroy_timer;
303 add_timer(&sk->timer);
306 static intunix_release_sock(unix_socket *sk)
308 unix_socket *skpair;
310 sk->state_change(sk);
311 sk->dead=1;
312 sk->socket = NULL;
314 if(sk->state == TCP_LISTEN)
315 wake_up_interruptible(&unix_ack_wqueue);
316 if(sk->type == SOCK_DGRAM)
317 wake_up_interruptible(&unix_dgram_wqueue);
319 skpair=unix_peer(sk);
321 if(skpair!=NULL)
323 if(sk->type==SOCK_STREAM &&unix_our_peer(sk, skpair))
325 skpair->state_change(skpair);
326 skpair->shutdown=SHUTDOWN_MASK;/* No more writes*/
328 unix_unlock(skpair);/* It may now die */
331 /* Try to flush out this socket. Throw out buffers at least */
332 unix_destroy_socket(sk);
335 * Fixme: BSD difference: In BSD all sockets connected to use get
336 * ECONNRESET and we die on the spot. In Linux we behave
337 * like files and pipes do and wait for the last
338 * dereference.
340 * Can't we simply set sock->err?
342 * What the above comment does talk about? --ANK(980817)
345 unix_gc();/* Garbage collect fds */
346 return0;
349 static voidunix_destroy_socket(unix_socket *sk)
351 struct sk_buff *skb;
353 unix_remove_socket(sk);
355 while((skb=skb_dequeue(&sk->receive_queue))!=NULL)
357 if(sk->state==TCP_LISTEN)
358 unix_release_sock(skb->sk);
359 /* passed fds are erased in the kfree_skb hook */
360 kfree_skb(skb);
363 if(sk->protinfo.af_unix.dentry!=NULL)
365 dput(sk->protinfo.af_unix.dentry);
366 sk->protinfo.af_unix.dentry=NULL;
369 if(!unix_locked(sk) &&atomic_read(&sk->wmem_alloc) ==0)
371 atomic_dec(&unix_nr_socks);
373 sk_free(sk);
375 /* socket destroyed, decrement count */
376 MOD_DEC_USE_COUNT;
378 else
380 sk->state=TCP_CLOSE;
381 sk->dead=1;
382 unix_delayed_delete(sk);/* Try every so often until buffers are all freed */
387 static intunix_listen(struct socket *sock,int backlog)
389 struct sock *sk = sock->sk;
391 if(sock->state != SS_UNCONNECTED)
392 return(-EINVAL);
393 if(sock->type!=SOCK_STREAM)
394 return-EOPNOTSUPP;/* Only stream sockets accept */
395 if(!sk->protinfo.af_unix.addr)
396 return-EINVAL;/* No listens on an unbound socket */
397 if((unsigned) backlog > SOMAXCONN)
398 backlog = SOMAXCONN;
399 sk->max_ack_backlog=backlog;
400 sk->state=TCP_LISTEN;
401 sock->flags |= SO_ACCEPTCON;
402 /* set credentials so connect can copy them */
403 sk->peercred.pid = current->pid;
404 sk->peercred.uid = current->euid;
405 sk->peercred.gid = current->egid;
406 return0;
409 externstruct proto_ops unix_stream_ops;
410 externstruct proto_ops unix_dgram_ops;
412 static struct sock *unix_create1(struct socket *sock,int stream)
414 struct sock *sk;
416 if(atomic_read(&unix_nr_socks) >=2*max_files)
417 return NULL;
419 MOD_INC_USE_COUNT;
420 sk =sk_alloc(PF_UNIX, GFP_KERNEL,1);
421 if(!sk) {
422 MOD_DEC_USE_COUNT;
423 return NULL;
426 atomic_inc(&unix_nr_socks);
428 sock_init_data(sock,sk);
430 if(stream)
431 sk->write_space = unix_stream_write_space;
433 sk->destruct = unix_destruct_addr;
434 sk->protinfo.af_unix.family=PF_UNIX;
435 sk->protinfo.af_unix.dentry=NULL;
436 sk->protinfo.af_unix.readsem=MUTEX;/* single task reading lock */
437 sk->protinfo.af_unix.list=&unix_sockets_unbound;
438 unix_insert_socket(sk);
440 return sk;
443 static intunix_create(struct socket *sock,int protocol)
445 int stream =0;
447 if(protocol && protocol != PF_UNIX)
448 return-EPROTONOSUPPORT;
450 sock->state = SS_UNCONNECTED;
452 switch(sock->type) {
453 case SOCK_STREAM:
454 sock->ops = &unix_stream_ops;
455 stream =1;
456 break;
458 * Believe it or not BSD has AF_UNIX, SOCK_RAW though
459 * nothing uses it.
461 case SOCK_RAW:
462 sock->type=SOCK_DGRAM;
463 case SOCK_DGRAM:
464 sock->ops = &unix_dgram_ops;
465 break;
466 default:
467 return-ESOCKTNOSUPPORT;
470 returnunix_create1(sock, stream) ?0: -ENOMEM;
473 static intunix_release(struct socket *sock,struct socket *peer)
475 unix_socket *sk = sock->sk;
477 if(!sk)
478 return0;
480 sock->sk = NULL;
481 if(sock->state != SS_UNCONNECTED)
482 sock->state = SS_DISCONNECTING;
484 returnunix_release_sock(sk);
487 static intunix_autobind(struct socket *sock)
489 struct sock *sk = sock->sk;
490 static u32 ordernum =1;
491 struct unix_address * addr;
492 unix_socket *osk;
494 addr =kmalloc(sizeof(*addr) +sizeof(short) +16, GFP_KERNEL);
495 if(!addr)
496 return-ENOMEM;
497 if(sk->protinfo.af_unix.addr || sk->protinfo.af_unix.dentry)
499 kfree(addr);
500 return-EINVAL;
502 memset(addr,0,sizeof(*addr) +sizeof(short) +16);
503 addr->name->sun_family = AF_UNIX;
504 atomic_set(&addr->refcnt,1);
506 retry:
507 addr->len =sprintf(addr->name->sun_path+1,"%08x", ordernum) +1+sizeof(short);
508 addr->hash =unix_hash_fold(csum_partial((void*)addr->name, addr->len,0));
509 ordernum++;
511 if((osk=unix_find_socket_byname(addr->name, addr->len, sock->type,
512 addr->hash)) != NULL)
514 unix_unlock(osk);
515 goto retry;
518 sk->protinfo.af_unix.addr = addr;
519 unix_remove_socket(sk);
520 sk->protinfo.af_unix.list = &unix_socket_table[(addr->hash ^ sk->type)&0xF];
521 unix_insert_socket(sk);
522 return0;
525 static unix_socket *unix_find_other(struct sockaddr_un *sunname,int len,
526 int type,unsigned hash,int*error)
528 unix_socket *u;
530 if(sunname->sun_path[0])
532 struct dentry *dentry;
533 dentry =open_namei(sunname->sun_path,2, S_IFSOCK);
534 if(IS_ERR(dentry)) {
535 *error =PTR_ERR(dentry);
536 return NULL;
538 u=unix_find_socket_byinode(dentry->d_inode);
539 dput(dentry);
540 if(u && u->type != type)
542 *error=-EPROTOTYPE;
543 unix_unlock(u);
544 return NULL;
547 else
548 u=unix_find_socket_byname(sunname, len, type, hash);
550 if(u==NULL)
552 *error=-ECONNREFUSED;
553 return NULL;
555 return u;
559 static intunix_bind(struct socket *sock,struct sockaddr *uaddr,int addr_len)
561 struct sock *sk = sock->sk;
562 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
563 struct dentry * dentry;
564 int err;
565 unsigned hash;
566 struct unix_address *addr;
568 if(sk->protinfo.af_unix.addr || sk->protinfo.af_unix.dentry ||
569 sunaddr->sun_family != AF_UNIX)
570 return-EINVAL;
572 if(addr_len==sizeof(short))
573 returnunix_autobind(sock);
575 addr_len =unix_mkname(sunaddr, addr_len, &hash);
576 if(addr_len <0)
577 return addr_len;
579 addr =kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);
580 if(!addr)
581 return-ENOMEM;
583 /* We slept; recheck ... */
585 if(sk->protinfo.af_unix.addr || sk->protinfo.af_unix.dentry)
587 kfree(addr);
588 return-EINVAL;/* Already bound */
591 memcpy(addr->name, sunaddr, addr_len);
592 addr->len = addr_len;
593 addr->hash = hash;
594 atomic_set(&addr->refcnt,1);
596 if(!sunaddr->sun_path[0])
598 unix_socket *osk =unix_find_socket_byname(sunaddr, addr_len,
599 sk->type, hash);
600 if(osk)
602 unix_unlock(osk);
603 kfree(addr);
604 return-EADDRINUSE;
606 unix_remove_socket(sk);
607 sk->protinfo.af_unix.addr = addr;
608 sk->protinfo.af_unix.list = &unix_socket_table[(hash^sk->type)&0xF];
609 unix_insert_socket(sk);
610 return0;
613 addr->hash = UNIX_HASH_SIZE;
614 sk->protinfo.af_unix.addr = addr;
617 dentry =do_mknod(sunaddr->sun_path, S_IFSOCK|sock->inode->i_mode,0);
618 if(IS_ERR(dentry))
620 err =PTR_ERR(dentry);
621 unix_release_addr(addr);
622 sk->protinfo.af_unix.addr = NULL;
623 if(err==-EEXIST)
624 return-EADDRINUSE;
625 else
626 return err;
628 unix_remove_socket(sk);
629 sk->protinfo.af_unix.list = &unix_socket_table[dentry->d_inode->i_ino &0xF];
630 sk->protinfo.af_unix.dentry = dentry;
631 unix_insert_socket(sk);
633 return0;
636 static intunix_dgram_connect(struct socket *sock,struct sockaddr *addr,
637 int alen,int flags)
639 struct sock *sk = sock->sk;
640 struct sockaddr_un *sunaddr=(struct sockaddr_un*)addr;
641 struct sock *other;
642 unsigned hash;
643 int err;
646 * 1003.1g breaking connected state with AF_UNSPEC
649 if(addr->sa_family==AF_UNSPEC)
651 if(unix_peer(sk))
653 unix_unlock(unix_peer(sk));
654 unix_peer(sk) = NULL;
655 sock->state=SS_UNCONNECTED;
657 return0;
660 alen =unix_mkname(sunaddr, alen, &hash);
661 if(alen <0)
662 return alen;
664 other=unix_find_other(sunaddr, alen, sock->type, hash, &err);
665 if(!other)
666 return err;
667 if(!unix_may_send(sk, other))
669 unix_unlock(other);
670 return-EINVAL;
674 * If it was connected, reconnect.
676 if(unix_peer(sk))
678 unix_unlock(unix_peer(sk));
679 unix_peer(sk)=NULL;
681 unix_peer(sk)=other;
682 if(sock->passcred && !sk->protinfo.af_unix.addr)
683 unix_autobind(sock);
684 return0;
687 static intunix_stream_connect(struct socket *sock,struct sockaddr *uaddr,
688 int addr_len,int flags)
690 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
691 struct sock *sk = sock->sk, *newsk;
692 unix_socket *other;
693 struct sk_buff *skb;
694 int err;
695 unsigned hash;
697 addr_len =unix_mkname(sunaddr, addr_len, &hash);
698 if(addr_len <0)
699 return addr_len;
701 /* First of all allocate resources.
702 If we will make it after state checks,
703 we will have to recheck all again in any case.
706 restart:
707 /* Find listening sock */
708 other=unix_find_other(sunaddr, addr_len, sk->type, hash, &err);
710 if(!other)
711 return-ECONNREFUSED;
713 while(other->ack_backlog >= other->max_ack_backlog) {
714 unix_unlock(other);
715 if(other->dead || other->state != TCP_LISTEN)
716 return-ECONNREFUSED;
717 if(flags & O_NONBLOCK)
718 return-EAGAIN;
719 interruptible_sleep_on(&unix_ack_wqueue);
720 if(signal_pending(current))
721 return-ERESTARTSYS;
722 goto restart;
725 /* create new sock for complete connection */
726 newsk =unix_create1(NULL,1);
728 /* Allocate skb for sending to listening sock */
729 skb = NULL;
730 if(newsk)
731 skb =sock_wmalloc(newsk,1,0, GFP_KERNEL);
733 switch(sock->state)
735 case SS_UNCONNECTED:
736 /* This is ok... continue with connect */
737 break;
738 case SS_CONNECTED:
739 /* Socket is already connected */
740 err = -EISCONN;
741 goto out;
742 default:
743 err = -EINVAL;
744 goto out;
747 err = -EINVAL;
748 if(sk->state != TCP_CLOSE)
749 goto out;
751 /* Check that listener is in valid state. */
752 err = -ECONNREFUSED;
753 if(other->dead || other->state != TCP_LISTEN)
754 goto out;
756 err = -ENOMEM;
757 if(newsk == NULL || skb == NULL)
758 goto out;
760 UNIXCB(skb).attr = MSG_SYN;
762 /* set up connecting socket */
763 sock->state=SS_CONNECTED;
764 if(!sk->protinfo.af_unix.addr)
765 unix_autobind(sock);
766 unix_peer(sk)=newsk;
767 unix_lock(sk);
768 sk->state=TCP_ESTABLISHED;
769 /* Set credentials */
770 sk->peercred = other->peercred;
772 /* set up newly created sock */
773 unix_peer(newsk)=sk;
774 unix_lock(newsk);
775 newsk->state=TCP_ESTABLISHED;
776 newsk->type=SOCK_STREAM;
777 newsk->peercred.pid = current->pid;
778 newsk->peercred.uid = current->euid;
779 newsk->peercred.gid = current->egid;
781 /* copy address information from listening to new sock*/
782 if(other->protinfo.af_unix.addr)
784 atomic_inc(&other->protinfo.af_unix.addr->refcnt);
785 newsk->protinfo.af_unix.addr=other->protinfo.af_unix.addr;
787 if(other->protinfo.af_unix.dentry)
788 newsk->protinfo.af_unix.dentry=dget(other->protinfo.af_unix.dentry);
790 /* send info to listening sock */
791 other->ack_backlog++;
792 skb_queue_tail(&other->receive_queue,skb);
793 other->data_ready(other,0);/* Wake up ! */
794 unix_unlock(other);
795 return0;
797 out:
798 if(skb)
799 kfree_skb(skb);
800 if(newsk)
801 unix_destroy_socket(newsk);
802 if(other)
803 unix_unlock(other);
804 return err;
807 static intunix_socketpair(struct socket *socka,struct socket *sockb)
809 struct sock *ska=socka->sk, *skb = sockb->sk;
811 /* Join our sockets back to back */
812 unix_lock(ska);
813 unix_lock(skb);
814 unix_peer(ska)=skb;
815 unix_peer(skb)=ska;
817 if(ska->type != SOCK_DGRAM)
819 ska->state=TCP_ESTABLISHED;
820 skb->state=TCP_ESTABLISHED;
821 socka->state=SS_CONNECTED;
822 sockb->state=SS_CONNECTED;
824 return0;
827 static intunix_accept(struct socket *sock,struct socket *newsock,int flags)
829 unix_socket *sk = sock->sk;
830 unix_socket *newsk = newsock->sk;
831 unix_socket *tsk;
832 struct sk_buff *skb;
834 if(sock->state != SS_UNCONNECTED)
835 return(-EINVAL);
836 if(!(sock->flags & SO_ACCEPTCON))
837 return(-EINVAL);
839 if(sock->type!=SOCK_STREAM)
840 return-EOPNOTSUPP;
841 if(sk->state!=TCP_LISTEN)
842 return-EINVAL;
844 for(;;)
846 skb=skb_dequeue(&sk->receive_queue);
847 if(skb==NULL)
849 if(flags&O_NONBLOCK)
850 return-EAGAIN;
851 interruptible_sleep_on(sk->sleep);
852 if(signal_pending(current))
853 return-ERESTARTSYS;
854 continue;
856 if(!(UNIXCB(skb).attr & MSG_SYN))
858 tsk=skb->sk;
859 tsk->state_change(tsk);
860 kfree_skb(skb);
861 continue;
863 tsk = skb->sk;
864 if(sk->max_ack_backlog == sk->ack_backlog--)
865 wake_up_interruptible(&unix_ack_wqueue);
866 kfree_skb(skb);
867 break;
871 /* attach accepted sock to socket */
872 newsock->state=SS_CONNECTED;
873 newsock->sk=tsk;
874 tsk->sleep=newsk->sleep;
875 tsk->socket=newsock;
877 /* destroy handed sock */
878 newsk->socket = NULL;
879 unix_destroy_socket(newsk);
881 return0;
885 static intunix_getname(struct socket *sock,struct sockaddr *uaddr,int*uaddr_len,int peer)
887 struct sock *sk = sock->sk;
888 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
890 if(peer)
892 if(!unix_peer(sk))
893 return-ENOTCONN;
894 sk=unix_peer(sk);
896 if(!sk->protinfo.af_unix.addr)
898 sunaddr->sun_family = AF_UNIX;
899 sunaddr->sun_path[0] =0;
900 *uaddr_len =sizeof(short);
901 return0;/* Not bound */
903 *uaddr_len = sk->protinfo.af_unix.addr->len;
904 memcpy(sunaddr, sk->protinfo.af_unix.addr->name, *uaddr_len);
905 return0;
908 static voidunix_detach_fds(struct scm_cookie *scm,struct sk_buff *skb)
910 int i;
912 scm->fp =UNIXCB(skb).fp;
913 skb->destructor = sock_wfree;
914 UNIXCB(skb).fp = NULL;
916 for(i=scm->fp->count-1; i>=0; i--)
917 unix_notinflight(scm->fp->fp[i]);
920 static voidunix_destruct_fds(struct sk_buff *skb)
922 struct scm_cookie scm;
923 memset(&scm,0,sizeof(scm));
924 unix_detach_fds(&scm, skb);
925 scm_destroy(&scm);
926 sock_wfree(skb);
929 static voidunix_attach_fds(struct scm_cookie *scm,struct sk_buff *skb)
931 int i;
932 for(i=scm->fp->count-1; i>=0; i--)
933 unix_inflight(scm->fp->fp[i]);
934 UNIXCB(skb).fp = scm->fp;
935 skb->destructor = unix_destruct_fds;
936 scm->fp = NULL;
941 * Send AF_UNIX data.
944 static intunix_dgram_sendmsg(struct socket *sock,struct msghdr *msg,int len,
945 struct scm_cookie *scm)
947 struct sock *sk = sock->sk;
948 struct sockaddr_un *sunaddr=msg->msg_name;
949 unix_socket *other;
950 int namelen =0;/* fake GCC */
951 int err;
952 unsigned hash;
953 struct sk_buff *skb;
955 if(msg->msg_flags&MSG_OOB)
956 return-EOPNOTSUPP;
958 if(msg->msg_flags&~(MSG_DONTWAIT|MSG_NOSIGNAL))
959 return-EINVAL;
961 if(msg->msg_namelen) {
962 namelen =unix_mkname(sunaddr, msg->msg_namelen, &hash);
963 if(namelen <0)
964 return namelen;
965 }else{
966 sunaddr = NULL;
967 if(!unix_peer(sk))
968 return-ENOTCONN;
971 if(sock->passcred && !sk->protinfo.af_unix.addr)
972 unix_autobind(sock);
974 skb =sock_alloc_send_skb(sk, len,0, msg->msg_flags&MSG_DONTWAIT, &err);
975 if(skb==NULL)
976 goto out;
978 memcpy(UNIXCREDS(skb), &scm->creds,sizeof(struct ucred));
979 UNIXCB(skb).attr = msg->msg_flags;
980 if(scm->fp)
981 unix_attach_fds(scm, skb);
983 skb->h.raw = skb->data;
984 err =memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
985 if(err)
986 goto out_free;
988 other =unix_peer(sk);
989 if(other && other->dead)
992 * Check with 1003.1g - what should
993 * datagram error
995 dead:
996 unix_unlock(other);
997 unix_peer(sk)=NULL;
998 other = NULL;
999 err = -ECONNRESET;
1000 if(sunaddr == NULL)
1001 goto out_free;
1003 if(!other)
1005 other =unix_find_other(sunaddr, namelen, sk->type, hash, &err);
1006 if(other==NULL)
1007 goto out_free;
1008 err = -EINVAL;
1009 if(!unix_may_send(sk, other))
1010 goto out_unlock;
1013 while(skb_queue_len(&other->receive_queue) >=
1014 sysctl_unix_max_dgram_qlen)
1016 if(sock->file->f_flags & O_NONBLOCK)
1018 err = -EAGAIN;
1019 goto out_unlock;
1021 interruptible_sleep_on(&unix_dgram_wqueue);
1022 if(other->dead)
1023 goto dead;
1024 if(sk->shutdown & SEND_SHUTDOWN)
1026 err = -EPIPE;
1027 goto out_unlock;
1029 if(signal_pending(current))
1031 err = -ERESTARTSYS;
1032 goto out_unlock;
1036 skb_queue_tail(&other->receive_queue, skb);
1037 other->data_ready(other,len);
1039 if(!unix_peer(sk))
1040 unix_unlock(other);
1041 return len;
1043 out_unlock:
1044 unix_unlock(other);
1045 out_free:
1046 kfree_skb(skb);
1047 out:
1048 return err;
1052 static intunix_stream_sendmsg(struct socket *sock,struct msghdr *msg,int len,
1053 struct scm_cookie *scm)
1055 struct sock *sk = sock->sk;
1056 unix_socket *other;
1057 struct sockaddr_un *sunaddr=msg->msg_name;
1058 int err,size;
1059 struct sk_buff *skb;
1060 int limit=0;
1061 int sent=0;
1063 if(sock->flags & SO_ACCEPTCON)
1064 return(-EINVAL);
1066 if(msg->msg_flags&MSG_OOB)
1067 return-EOPNOTSUPP;
1069 if(msg->msg_flags&~(MSG_DONTWAIT|MSG_NOSIGNAL))
1070 return-EINVAL;
1072 if(msg->msg_namelen) {
1073 if(sk->state==TCP_ESTABLISHED)
1074 return-EISCONN;
1075 else
1076 return-EOPNOTSUPP;
1077 }else{
1078 sunaddr = NULL;
1079 if(!unix_peer(sk))
1080 return-ENOTCONN;
1083 if(sk->shutdown&SEND_SHUTDOWN) {
1084 if(!(msg->msg_flags&MSG_NOSIGNAL))
1085 send_sig(SIGPIPE,current,0);
1086 return-EPIPE;
1089 while(sent < len)
1092 * Optimisation for the fact that under 0.01% of X messages typically
1093 * need breaking up.
1096 size=len-sent;
1098 /* Keep two messages in the pipe so it schedules better */
1099 if(size > sk->sndbuf/2-16)
1100 size = sk->sndbuf/2-16;
1103 * Keep to page sized kmalloc()'s as various people
1104 * have suggested. Big mallocs stress the vm too
1105 * much.
1108 if(size >4096-16)
1109 limit =4096-16;/* Fall back to a page if we can't grab a big buffer this instant */
1110 else
1111 limit =0;/* Otherwise just grab and wait */
1114 * Grab a buffer
1117 skb=sock_alloc_send_skb(sk,size,limit,msg->msg_flags&MSG_DONTWAIT, &err);
1119 if(skb==NULL)
1121 if(sent)
1122 goto out;
1123 return err;
1127 * If you pass two values to the sock_alloc_send_skb
1128 * it tries to grab the large buffer with GFP_BUFFER
1129 * (which can fail easily), and if it fails grab the
1130 * fallback size buffer which is under a page and will
1131 * succeed. [Alan]
1133 size =min(size,skb_tailroom(skb));
1135 memcpy(UNIXCREDS(skb), &scm->creds,sizeof(struct ucred));
1136 UNIXCB(skb).attr = msg->msg_flags;
1137 if(scm->fp)
1138 unix_attach_fds(scm, skb);
1140 if(memcpy_fromiovec(skb_put(skb,size), msg->msg_iov, size)) {
1141 kfree_skb(skb);
1142 if(sent)
1143 goto out;
1144 return-EFAULT;
1147 other=unix_peer(sk);
1149 if(other->dead || (sk->shutdown & SEND_SHUTDOWN))
1151 kfree_skb(skb);
1152 if(sent)
1153 goto out;
1154 if(!(msg->msg_flags&MSG_NOSIGNAL))
1155 send_sig(SIGPIPE,current,0);
1156 return-EPIPE;
1159 skb_queue_tail(&other->receive_queue, skb);
1160 other->data_ready(other,size);
1161 sent+=size;
1163 out:
1164 return sent;
1168 * Sleep until data has arrive. But check for races..
1171 static voidunix_data_wait(unix_socket * sk)
1173 if(!skb_peek(&sk->receive_queue))
1175 sk->socket->flags |= SO_WAITDATA;
1176 interruptible_sleep_on(sk->sleep);
1177 sk->socket->flags &= ~SO_WAITDATA;
1181 static intunix_dgram_recvmsg(struct socket *sock,struct msghdr *msg,int size,
1182 int flags,struct scm_cookie *scm)
1184 struct sock *sk = sock->sk;
1185 int noblock = flags & MSG_DONTWAIT;
1186 struct sk_buff *skb;
1187 int err;
1189 if(flags&MSG_OOB)
1190 return-EOPNOTSUPP;
1192 msg->msg_namelen =0;
1194 skb =skb_recv_datagram(sk, flags, noblock, &err);
1195 if(!skb)
1196 goto out;
1199 * sysctl_unix_max_dgram_qlen may change over the time we blocked
1200 * in the waitqueue so we must wakeup every time we shrink the
1201 * receiver queue. -arca
1203 wake_up_interruptible(&unix_dgram_wqueue);
1205 if(msg->msg_name)
1207 msg->msg_namelen =sizeof(short);
1208 if(skb->sk->protinfo.af_unix.addr)
1210 msg->msg_namelen=skb->sk->protinfo.af_unix.addr->len;
1211 memcpy(msg->msg_name,
1212 skb->sk->protinfo.af_unix.addr->name,
1213 skb->sk->protinfo.af_unix.addr->len);
1217 if(size > skb->len)
1218 size = skb->len;
1219 else if(size < skb->len)
1220 msg->msg_flags |= MSG_TRUNC;
1222 err =skb_copy_datagram_iovec(skb,0, msg->msg_iov, size);
1223 if(err)
1224 goto out_free;
1226 scm->creds = *UNIXCREDS(skb);
1228 if(!(flags & MSG_PEEK))
1230 if(UNIXCB(skb).fp)
1231 unix_detach_fds(scm, skb);
1233 else
1235 /* It is questionable: on PEEK we could:
1236 - do not return fds - good, but too simple 8)
1237 - return fds, and do not return them on read (old strategy,
1238 apparently wrong)
1239 - clone fds (I choosed it for now, it is the most universal
1240 solution)
1242 POSIX 1003.1g does not actually define this clearly
1243 at all. POSIX 1003.1g doesn't define a lot of things
1244 clearly however!
1247 if(UNIXCB(skb).fp)
1248 scm->fp =scm_fp_dup(UNIXCB(skb).fp);
1250 err = size;
1252 out_free:
1253 skb_free_datagram(sk,skb);
1254 out:
1255 return err;
1259 static intunix_stream_recvmsg(struct socket *sock,struct msghdr *msg,int size,
1260 int flags,struct scm_cookie *scm)
1262 struct sock *sk = sock->sk;
1263 int noblock = flags & MSG_DONTWAIT;
1264 struct sockaddr_un *sunaddr=msg->msg_name;
1265 int copied =0;
1266 int check_creds =0;
1267 int target =1;
1269 if(sock->flags & SO_ACCEPTCON)
1270 return(-EINVAL);
1272 if(flags&MSG_OOB)
1273 return-EOPNOTSUPP;
1274 if(flags&MSG_WAITALL)
1275 target = size;
1278 msg->msg_namelen =0;
1280 /* Lock the socket to prevent queue disordering
1281 * while sleeps in memcpy_tomsg
1284 down(&sk->protinfo.af_unix.readsem);
1288 int chunk;
1289 struct sk_buff *skb;
1291 skb=skb_dequeue(&sk->receive_queue);
1292 if(skb==NULL)
1294 if(copied >= target)
1295 break;
1298 * POSIX 1003.1g mandates this order.
1301 if(sk->err)
1303 up(&sk->protinfo.af_unix.readsem);
1304 returnsock_error(sk);
1307 if(sk->shutdown & RCV_SHUTDOWN)
1308 break;
1309 up(&sk->protinfo.af_unix.readsem);
1310 if(noblock)
1311 return-EAGAIN;
1312 unix_data_wait(sk);
1313 if(signal_pending(current))
1314 return-ERESTARTSYS;
1315 down(&sk->protinfo.af_unix.readsem);
1316 continue;
1319 /* Never glue messages from different writers */
1320 if(check_creds &&
1321 memcmp(UNIXCREDS(skb), &scm->creds,sizeof(scm->creds)) !=0)
1323 skb_queue_head(&sk->receive_queue, skb);
1324 break;
1327 /* Copy address just once */
1328 if(sunaddr)
1330 msg->msg_namelen =sizeof(short);
1331 if(skb->sk->protinfo.af_unix.addr)
1333 msg->msg_namelen=skb->sk->protinfo.af_unix.addr->len;
1334 memcpy(sunaddr,
1335 skb->sk->protinfo.af_unix.addr->name,
1336 skb->sk->protinfo.af_unix.addr->len);
1338 sunaddr = NULL;
1341 chunk =min(skb->len, size);
1342 if(memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
1343 skb_queue_head(&sk->receive_queue, skb);
1344 if(copied ==0)
1345 copied = -EFAULT;
1346 break;
1348 copied += chunk;
1349 size -= chunk;
1351 /* Copy credentials */
1352 scm->creds = *UNIXCREDS(skb);
1353 check_creds =1;
1355 /* Mark read part of skb as used */
1356 if(!(flags & MSG_PEEK))
1358 skb_pull(skb, chunk);
1360 if(UNIXCB(skb).fp)
1361 unix_detach_fds(scm, skb);
1363 /* put the skb back if we didn't use it up.. */
1364 if(skb->len)
1366 skb_queue_head(&sk->receive_queue, skb);
1367 break;
1370 kfree_skb(skb);
1372 if(scm->fp)
1373 break;
1375 else
1377 /* It is questionable, see note in unix_dgram_recvmsg.
1380 if(UNIXCB(skb).fp)
1381 scm->fp =scm_fp_dup(UNIXCB(skb).fp);
1383 /* put message back and return */
1384 skb_queue_head(&sk->receive_queue, skb);
1385 break;
1387 }while(size);
1389 up(&sk->protinfo.af_unix.readsem);
1390 return copied;
1393 static intunix_shutdown(struct socket *sock,int mode)
1395 struct sock *sk = sock->sk;
1396 unix_socket *other=unix_peer(sk);
1398 mode = (mode+1)&(RCV_SHUTDOWN|SEND_SHUTDOWN);
1400 if(mode) {
1401 sk->shutdown |= mode;
1402 sk->state_change(sk);
1403 if(other && sk->type == SOCK_STREAM &&
1404 unix_our_peer(sk, other)) {
1405 int peer_mode =0;
1407 if(mode&RCV_SHUTDOWN)
1408 peer_mode |= SEND_SHUTDOWN;
1409 if(mode&SEND_SHUTDOWN)
1410 peer_mode |= RCV_SHUTDOWN;
1411 other->shutdown |= peer_mode;
1412 other->state_change(other);
1415 return0;
1419 static intunix_ioctl(struct socket *sock,unsigned int cmd,unsigned long arg)
1421 struct sock *sk = sock->sk;
1422 long amount=0;
1424 switch(cmd)
1427 case TIOCOUTQ:
1428 amount = sk->sndbuf -atomic_read(&sk->wmem_alloc);
1429 if(amount<0)
1430 amount=0;
1431 returnput_user(amount, (int*)arg);
1432 case TIOCINQ:
1434 struct sk_buff *skb;
1435 if(sk->state==TCP_LISTEN)
1436 return-EINVAL;
1438 * These two are safe on current systems as
1439 * only user tasks fiddle here
1441 if((skb=skb_peek(&sk->receive_queue))!=NULL)
1442 amount=skb->len;
1443 returnput_user(amount, (int*)arg);
1446 default:
1447 return-EINVAL;
1449 /*NOTREACHED*/
1450 return(0);
1453 static unsigned intunix_poll(struct file * file,struct socket *sock, poll_table *wait)
1455 struct sock *sk = sock->sk;
1456 unsigned int mask;
1458 poll_wait(file, sk->sleep, wait);
1459 mask =0;
1461 /* exceptional events? */
1462 if(sk->err)
1463 mask |= POLLERR;
1464 if(sk->shutdown & RCV_SHUTDOWN)
1465 mask |= POLLHUP;
1467 /* readable? */
1468 if(!skb_queue_empty(&sk->receive_queue))
1469 mask |= POLLIN | POLLRDNORM;
1471 /* Connection-based need to check for termination and startup */
1472 if(sk->type == SOCK_STREAM && sk->state==TCP_CLOSE)
1473 mask |= POLLHUP;
1476 * we set writable also when the other side has shut down the
1477 * connection. This prevents stuck sockets.
1479 if(sk->sndbuf - (int)atomic_read(&sk->wmem_alloc) >= MIN_WRITE_SPACE)
1480 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1482 return mask;
1485 static voidunix_stream_write_space(struct sock *sk)
1487 if(sk->dead)
1488 return;
1489 wake_up_interruptible(sk->sleep);
1490 if(sk->sndbuf - (int)atomic_read(&sk->wmem_alloc) >= MIN_WRITE_SPACE)
1491 sock_wake_async(sk->socket,2);
1494 #ifdef CONFIG_PROC_FS
1495 static intunix_read_proc(char*buffer,char**start, off_t offset,
1496 int length,int*eof,void*data)
1498 off_t pos=0;
1499 off_t begin=0;
1500 int len=0;
1501 int i;
1502 unix_socket *s;
1504 len+=sprintf(buffer,"Num RefCount Protocol Flags Type St "
1505 "Inode Path\n");
1507 forall_unix_sockets(i,s)
1509 len+=sprintf(buffer+len,"%p: %08X %08X %08lX %04X %02X %5ld",
1511 atomic_read(&s->sock_readers),
1513 s->socket ? s->socket->flags :0,
1514 s->type,
1515 s->socket ? s->socket->state :
1516 (s->state == TCP_ESTABLISHED ?
1517 SS_CONNECTING : SS_DISCONNECTING),
1518 s->socket ? s->socket->inode->i_ino :0);
1520 if(s->protinfo.af_unix.addr)
1522 buffer[len++] =' ';
1523 memcpy(buffer+len, s->protinfo.af_unix.addr->name->sun_path,
1524 s->protinfo.af_unix.addr->len-sizeof(short));
1525 if(!UNIX_ABSTRACT(s))
1526 len--;
1527 else
1528 buffer[len] ='@';
1529 len += s->protinfo.af_unix.addr->len -sizeof(short);
1531 buffer[len++]='\n';
1533 pos = begin + len;
1534 if(pos<offset)
1536 len=0;
1537 begin=pos;
1539 if(pos>offset+length)
1540 goto done;
1542 *eof =1;
1543 done:
1544 *start=buffer+(offset-begin);
1545 len-=(offset-begin);
1546 if(len>length)
1547 len=length;
1548 if(len <0)
1549 len =0;
1550 return len;
1552 #endif
1554 struct proto_ops unix_stream_ops = {
1555 PF_UNIX,
1557 sock_no_dup,
1558 unix_release,
1559 unix_bind,
1560 unix_stream_connect,
1561 unix_socketpair,
1562 unix_accept,
1563 unix_getname,
1564 unix_poll,
1565 unix_ioctl,
1566 unix_listen,
1567 unix_shutdown,
1568 sock_no_setsockopt,
1569 sock_no_getsockopt,
1570 sock_no_fcntl,
1571 unix_stream_sendmsg,
1572 unix_stream_recvmsg
1575 struct proto_ops unix_dgram_ops = {
1576 PF_UNIX,
1578 sock_no_dup,
1579 unix_release,
1580 unix_bind,
1581 unix_dgram_connect,
1582 unix_socketpair,
1583 sock_no_accept,
1584 unix_getname,
1585 datagram_poll,
1586 unix_ioctl,
1587 sock_no_listen,
1588 unix_shutdown,
1589 sock_no_setsockopt,
1590 sock_no_getsockopt,
1591 sock_no_fcntl,
1592 unix_dgram_sendmsg,
1593 unix_dgram_recvmsg
1596 struct net_proto_family unix_family_ops = {
1597 PF_UNIX,
1598 unix_create
1601 #ifdef MODULE
1602 #ifdef CONFIG_SYSCTL
1603 externvoidunix_sysctl_register(void);
1604 externvoidunix_sysctl_unregister(void);
1605 #endif
1607 intinit_module(void)
1608 #else
1609 __initfunc(voidunix_proto_init(struct net_proto *pro))
1610 #endif
1612 struct sk_buff *dummy_skb;
1613 struct proc_dir_entry *ent;
1615 printk(KERN_INFO "NET4: Unix domain sockets 1.0 for Linux NET4.0.\n");
1616 if(sizeof(struct unix_skb_parms) >sizeof(dummy_skb->cb))
1618 printk(KERN_CRIT "unix_proto_init: panic\n");
1619 #ifdef MODULE
1620 return-1;
1621 #else
1622 return;
1623 #endif
1625 sock_register(&unix_family_ops);
1626 #ifdef CONFIG_PROC_FS
1627 ent =create_proc_entry("net/unix",0,0);
1628 ent->read_proc = unix_read_proc;
1629 #endif
1631 #ifdef MODULE
1632 #ifdef CONFIG_SYSCTL
1633 unix_sysctl_register();
1634 #endif
1636 return0;
1637 #endif
1640 #ifdef MODULE
1641 voidcleanup_module(void)
1643 sock_unregister(PF_UNIX);
1644 #ifdef CONFIG_SYSCTL
1645 unix_sysctl_unregister();
1646 #endif
1647 #ifdef CONFIG_PROC_FS
1648 remove_proc_entry("net/unix",0);
1649 #endif
1651 #endif
1654 * Local variables:
1655 * compile-command: "gcc -g -D__KERNEL__ -Wall -O6 -I/usr/src/linux/include -c af_unix.c"
1656 * End:
close