Import 2.1.45pre10
[davej-history.git] / net / unix / af_unix.c
blobf41213ad644f9141bd78ccccf533c059d5645db0
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 * Fixes:
12 * Linus Torvalds : Assorted bug cures.
13 * Niibe Yutaka : async I/O support.
14 * Carsten Paeth : PF_UNIX check, address fixes.
15 * Alan Cox : Limit size of allocated blocks.
16 * Alan Cox : Fixed the stupid socketpair bug.
17 * Alan Cox : BSD compatibility fine tuning.
18 * Alan Cox : Fixed a bug in connect when interrupted.
19 * Alan Cox : Sorted out a proper draft version of
20 * file descriptor passing hacked up from
21 * Mike Shaver's work.
22 * Marty Leisner : Fixes to fd passing
23 * Nick Nevin : recvmsg bugfix.
24 * Alan Cox : Started proper garbage collector
25 * Heiko EiBfeldt : Missing verify_area check
26 * Alan Cox : Started POSIXisms
28 * Known differences from reference BSD that was tested:
30 * [TO FIX]
31 * ECONNREFUSED is not returned from one end of a connected() socket to the
32 * other the moment one end closes.
33 * fstat() doesn't return st_dev=NODEV, and give the blksize as high water mark
34 * and a fake inode identifier (nor the BSD first socket fstat twice bug).
35 * [NOT TO FIX]
36 * accept() returns a path name even if the connecting socket has closed
37 * in the meantime (BSD loses the path and gives up).
38 * accept() returns 0 length path for an unbound connector. BSD returns 16
39 * and a null first byte in the path (but not for gethost/peername - BSD bug ??)
40 * socketpair(...SOCK_RAW..) doesn't panic the kernel.
41 * BSD af_unix apparently has connect forgetting to block properly.
42 * (need to check this with the POSIX spec in detail)
44 * Differences from 2.0.0-11-... (ANK)
45 * Bug fixes and improvements.
46 * - client shutdown killed server socket.
47 * - removed all useless cli/sti pairs.
49 * Semantic changes/extensions.
50 * - generic control message passing.
51 * - SCM_CREDENTIALS control message.
52 * - "Abstract" (not FS based) socket bindings.
53 * Abstract names are sequences of bytes (not zero terminated)
54 * started by 0, so that this name space does not intersect
55 * with BSD names.
58 #include <linux/config.h>
59 #include <linux/kernel.h>
60 #include <linux/major.h>
61 #include <linux/signal.h>
62 #include <linux/sched.h>
63 #include <linux/errno.h>
64 #include <linux/string.h>
65 #include <linux/stat.h>
66 #include <linux/socket.h>
67 #include <linux/un.h>
68 #include <linux/fcntl.h>
69 #include <linux/termios.h>
70 #include <linux/socket.h>
71 #include <linux/sockios.h>
72 #include <linux/net.h>
73 #include <linux/in.h>
74 #include <linux/fs.h>
75 #include <linux/malloc.h>
76 #include <asm/uaccess.h>
77 #include <linux/skbuff.h>
78 #include <linux/netdevice.h>
79 #include <net/sock.h>
80 #include <net/tcp.h>
81 #include <net/af_unix.h>
82 #include <linux/proc_fs.h>
83 #include <net/scm.h>
84 #include <linux/init.h>
86 #include <asm/checksum.h>
88 #define min(a,b) (((a)<(b))?(a):(b))
90 int sysctl_unix_delete_delay = HZ;
91 int sysctl_unix_destroy_delay =10*HZ;
93 unix_socket *unix_socket_table[UNIX_HASH_SIZE+1];
95 #define unix_sockets_unbound (unix_socket_table[UNIX_HASH_SIZE])
97 #define UNIX_ABSTRACT(sk) ((sk)->protinfo.af_unix.addr->hash!=UNIX_HASH_SIZE)
99 extern __inline__ unsignedunix_hash_fold(unsigned hash)
101 hash ^= hash>>16;
102 hash ^= hash>>8;
103 hash ^= hash>>4;
104 return hash;
107 #define unix_peer(sk) ((sk)->pair)
109 extern __inline__ intunix_our_peer(unix_socket *sk, unix_socket *osk)
111 returnunix_peer(osk) == sk;
114 extern __inline__ intunix_may_send(unix_socket *sk, unix_socket *osk)
116 return(sk->type==osk->type);
119 extern __inline__ voidunix_lock(unix_socket *sk)
121 sk->sock_readers++;
124 extern __inline__ intunix_unlock(unix_socket *sk)
126 return sk->sock_readers--;
129 extern __inline__ intunix_locked(unix_socket *sk)
131 return sk->sock_readers;
134 extern __inline__ voidunix_release_addr(struct unix_address *addr)
136 if(addr)
138 if(atomic_dec_and_test(&addr->refcnt))
139 kfree(addr);
143 static voidunix_destruct_addr(struct sock *sk)
145 struct unix_address *addr = sk->protinfo.af_unix.addr;
147 unix_release_addr(addr);
151 * Check unix socket name:
152 * - should be not zero length.
153 * - if started by not zero, should be NULL terminated (FS object)
154 * - if started by zero, it is abstract name.
157 static intunix_mkname(struct sockaddr_un * sunaddr,int len,unsigned*hashp)
159 if(len <=sizeof(short) || len >sizeof(*sunaddr))
160 return-EINVAL;
161 if(!sunaddr || sunaddr->sun_family != AF_UNIX)
162 return-EINVAL;
163 if(sunaddr->sun_path[0])
166 * This may look like an off by one error but it is
167 * a bit more subtle. 108 is the longest valid AF_UNIX
168 * path for a binding. sun_path[108] doesnt as such
169 * exist. However in kernel space we are guaranteed that
170 * it is a valid memory location in our kernel
171 * address buffer.
173 if(len >sizeof(*sunaddr))
174 len =sizeof(*sunaddr);
175 ((char*)sunaddr)[len]=0;
176 len =strlen(sunaddr->sun_path)+1+sizeof(short);
177 return len;
180 *hashp =unix_hash_fold(csum_partial((char*)sunaddr, len,0));
181 return len;
184 static voidunix_remove_socket(unix_socket *sk)
186 unix_socket **list = sk->protinfo.af_unix.list;
187 if(sk->next)
188 sk->next->prev = sk->prev;
189 if(sk->prev)
190 sk->prev->next = sk->next;
191 if(*list == sk)
192 *list = sk->next;
193 sk->protinfo.af_unix.list = NULL;
194 sk->prev = NULL;
195 sk->next = NULL;
198 static voidunix_insert_socket(unix_socket *sk)
200 unix_socket **list = sk->protinfo.af_unix.list;
201 sk->prev = NULL;
202 sk->next = *list;
203 if(*list)
204 (*list)->prev = sk;
205 *list=sk;
208 static unix_socket *unix_find_socket_byname(struct sockaddr_un *sunname,
209 int len,int type,unsigned hash)
211 unix_socket *s;
213 for(s=unix_socket_table[(hash^type)&0xF]; s; s=s->next)
215 if(s->protinfo.af_unix.addr->len==len &&
216 memcmp(s->protinfo.af_unix.addr->name, sunname, len) ==0&&
217 s->type == type)
219 unix_lock(s);
220 return(s);
223 return(NULL);
226 static unix_socket *unix_find_socket_byinode(struct inode *i)
228 unix_socket *s;
230 for(s=unix_socket_table[i->i_ino &0xF]; s; s=s->next)
232 if(s->protinfo.af_unix.inode==i)
234 unix_lock(s);
235 return(s);
238 return(NULL);
242 * Delete a unix socket. We have to allow for deferring this on a timer.
245 static voidunix_destroy_timer(unsigned long data)
247 unix_socket *sk=(unix_socket *)data;
248 if(!unix_locked(sk) &&atomic_read(&sk->wmem_alloc) ==0)
250 sk_free(sk);
251 return;
255 * Retry;
258 sk->timer.expires=jiffies+sysctl_unix_destroy_delay;/* No real hurry try it every 10 seconds or so */
259 add_timer(&sk->timer);
263 static voidunix_delayed_delete(unix_socket *sk)
265 sk->timer.data=(unsigned long)sk;
266 sk->timer.expires=jiffies+sysctl_unix_delete_delay;/* Normally 1 second after will clean up. After that we try every 10 */
267 sk->timer.function=unix_destroy_timer;
268 add_timer(&sk->timer);
271 static voidunix_destroy_socket(unix_socket *sk)
273 struct sk_buff *skb;
275 unix_remove_socket(sk);
277 while((skb=skb_dequeue(&sk->receive_queue))!=NULL)
279 if(sk->state==TCP_LISTEN)
281 unix_socket *osk=skb->sk;
282 osk->state=TCP_CLOSE;
283 kfree_skb(skb, FREE_WRITE);/* Now surplus - free the skb first before the socket */
284 osk->state_change(osk);/* So the connect wakes and cleans up (if any) */
285 /* osk will be destroyed when it gets to close or the timer fires */
287 else
289 /* passed fds are erased in the kfree_skb hook */
290 kfree_skb(skb,FREE_WRITE);
294 if(sk->protinfo.af_unix.inode!=NULL)
296 iput(sk->protinfo.af_unix.inode);
297 sk->protinfo.af_unix.inode=NULL;
300 if(!unix_unlock(sk) &&atomic_read(&sk->wmem_alloc) ==0)
302 sk_free(sk);
304 else
306 sk->dead=1;
307 unix_delayed_delete(sk);/* Try every so often until buffers are all freed */
311 static intunix_listen(struct socket *sock,int backlog)
313 struct sock *sk = sock->sk;
315 if(sock->state != SS_UNCONNECTED)
316 return(-EINVAL);
317 if(sock->type!=SOCK_STREAM)
318 return-EOPNOTSUPP;/* Only stream sockets accept */
319 if(!sk->protinfo.af_unix.addr)
320 return-EINVAL;/* No listens on an unbound socket */
321 sk->max_ack_backlog=backlog;
322 if(sk->ack_backlog < backlog)
323 sk->state_change(sk);
324 sk->state=TCP_LISTEN;
325 sock->flags |= SO_ACCEPTCON;
326 return0;
329 externstruct proto_ops unix_stream_ops;
330 externstruct proto_ops unix_dgram_ops;
332 static intunix_create(struct socket *sock,int protocol)
334 struct sock *sk;
336 sock->state = SS_UNCONNECTED;
338 if(protocol && protocol != PF_UNIX)
339 return-EPROTONOSUPPORT;
341 switch(sock->type)
343 case SOCK_STREAM:
344 sock->ops = &unix_stream_ops;
345 break;
347 * Believe it or not BSD has AF_UNIX, SOCK_RAW though
348 * nothing uses it.
350 case SOCK_RAW:
351 sock->type=SOCK_DGRAM;
352 case SOCK_DGRAM:
353 sock->ops = &unix_dgram_ops;
354 break;
355 default:
356 return-ESOCKTNOSUPPORT;
358 sk =sk_alloc(GFP_KERNEL);
359 if(!sk)
360 return-ENOMEM;
362 sock_init_data(sock,sk);
364 sk->destruct = unix_destruct_addr;
365 sk->protinfo.af_unix.family=AF_UNIX;
366 sk->protinfo.af_unix.inode=NULL;
367 sk->sock_readers=1;/* Us */
368 sk->protinfo.af_unix.readsem=MUTEX;/* single task reading lock */
369 sk->mtu=4096;
370 sk->protinfo.af_unix.list=&unix_sockets_unbound;
371 unix_insert_socket(sk);
372 return0;
375 static intunix_dup(struct socket *newsock,struct socket *oldsock)
377 returnunix_create(newsock,0);
380 static intunix_release(struct socket *sock,struct socket *peer)
382 unix_socket *sk = sock->sk;
383 unix_socket *skpair;
385 if(!sk)
386 return0;
388 if(sock->state != SS_UNCONNECTED)
389 sock->state = SS_DISCONNECTING;
391 sk->state_change(sk);
392 sk->dead=1;
393 skpair=unix_peer(sk);
394 if(sock->type==SOCK_STREAM && skpair)
396 if(unix_our_peer(sk, skpair))
397 skpair->shutdown=SHUTDOWN_MASK;/* No more writes */
398 if(skpair->state!=TCP_LISTEN)
399 skpair->state_change(skpair);/* Wake any blocked writes */
401 if(skpair!=NULL)
402 unix_unlock(skpair);/* It may now die */
403 unix_peer(sk)=NULL;/* No pair */
404 unix_destroy_socket(sk);/* Try to flush out this socket. Throw out buffers at least */
405 unix_gc();/* Garbage collect fds */
408 * FIXME: BSD difference: In BSD all sockets connected to use get ECONNRESET and we die on the spot. In
409 * Linux we behave like files and pipes do and wait for the last dereference.
411 if(sk->socket)
413 sk->socket = NULL;
414 sock->sk = NULL;
417 return0;
420 static intunix_autobind(struct socket *sock)
422 struct sock *sk = sock->sk;
423 static u32 ordernum =1;
424 struct unix_address * addr;
425 unix_socket *osk;
427 addr =kmalloc(sizeof(*addr) +sizeof(short) +16, GFP_KERNEL);
428 if(!addr)
429 return-ENOBUFS;
430 if(sk->protinfo.af_unix.addr || sk->protinfo.af_unix.inode)
432 kfree(addr);
433 return-EINVAL;
435 memset(addr,0,sizeof(*addr) +sizeof(short) +16);
436 addr->name->sun_family = AF_UNIX;
437 atomic_set(&addr->refcnt,1);
439 retry:
440 addr->len =sprintf(addr->name->sun_path+1,"%08x", ordernum) +1+sizeof(short);
441 addr->hash =unix_hash_fold(csum_partial((void*)addr->name, addr->len,0));
442 ordernum++;
444 if((osk=unix_find_socket_byname(addr->name, addr->len, sock->type,
445 addr->hash)) != NULL)
447 unix_unlock(osk);
448 goto retry;
451 sk->protinfo.af_unix.addr = addr;
452 unix_remove_socket(sk);
453 sk->protinfo.af_unix.list = &unix_socket_table[(addr->hash ^ sk->type)&0xF];
454 unix_insert_socket(sk);
455 return0;
458 static unix_socket *unix_find_other(struct sockaddr_un *sunname,int len,
459 int type,unsigned hash,int*error)
461 unix_socket *u;
463 if(sunname->sun_path[0])
465 struct dentry *dentry;
466 dentry =open_namei(sunname->sun_path,2, S_IFSOCK);
467 if(IS_ERR(dentry)) {
468 *error =PTR_ERR(dentry);
469 return NULL;
471 u=unix_find_socket_byinode(dentry->d_inode);
472 dput(dentry);
473 if(u && u->type != type)
475 *error=-EPROTOTYPE;
476 unix_unlock(u);
477 return NULL;
480 else
481 u=unix_find_socket_byname(sunname, len, type, hash);
483 if(u==NULL)
485 *error=-ECONNREFUSED;
486 return NULL;
488 return u;
492 static intunix_bind(struct socket *sock,struct sockaddr *uaddr,int addr_len)
494 struct sock *sk = sock->sk;
495 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
496 struct dentry * dentry;
497 struct inode * inode = NULL;
498 int err;
499 unsigned hash;
500 struct unix_address *addr;
502 if(sk->protinfo.af_unix.addr || sk->protinfo.af_unix.inode ||
503 sunaddr->sun_family != AF_UNIX)
504 return-EINVAL;
506 if(addr_len==sizeof(short))
507 returnunix_autobind(sock);
509 addr_len =unix_mkname(sunaddr, addr_len, &hash);
510 if(addr_len <0)
511 return addr_len;
513 addr =kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);
514 if(!addr)
515 return-ENOBUFS;
517 /* We slept; recheck ... */
519 if(sk->protinfo.af_unix.addr || sk->protinfo.af_unix.inode)
521 kfree(addr);
522 return-EINVAL;/* Already bound */
525 memcpy(addr->name, sunaddr, addr_len);
526 addr->len = addr_len;
527 addr->hash = hash;
528 atomic_set(&addr->refcnt,1);
530 if(!sunaddr->sun_path[0])
532 unix_socket *osk =unix_find_socket_byname(sunaddr, addr_len,
533 sk->type, hash);
534 if(osk)
536 unix_unlock(osk);
537 kfree(addr);
538 return-EADDRINUSE;
540 unix_remove_socket(sk);
541 sk->protinfo.af_unix.addr = addr;
542 sk->protinfo.af_unix.list = &unix_socket_table[(hash^sk->type)&0xF];
543 unix_insert_socket(sk);
544 return0;
547 addr->hash = UNIX_HASH_SIZE;
548 sk->protinfo.af_unix.addr = addr;
551 dentry =do_mknod(sunaddr->sun_path, S_IFSOCK|S_IRWXUGO,0);
552 err =PTR_ERR(dentry);
553 if(!IS_ERR(dentry)) {
554 inode = dentry->d_inode;
555 inode->i_count++;/* HATEFUL - we should use the dentry */
556 dput(dentry);
557 err =0;
560 if(err<0)
562 unix_release_addr(addr);
563 sk->protinfo.af_unix.addr = NULL;
564 if(err==-EEXIST)
565 return-EADDRINUSE;
566 else
567 return err;
569 unix_remove_socket(sk);
570 sk->protinfo.af_unix.list = &unix_socket_table[inode->i_ino &0xF];
571 sk->protinfo.af_unix.inode = inode;
572 unix_insert_socket(sk);
574 return0;
577 static intunix_dgram_connect(struct socket *sock,struct sockaddr *addr,
578 int alen,int flags)
580 struct sock *sk = sock->sk;
581 struct sockaddr_un *sunaddr=(struct sockaddr_un*)addr;
582 struct sock *other;
583 unsigned hash;
584 int err;
587 * 1003.1g breaking connected state with AF_UNSPEC
590 if(addr->sa_family==AF_UNSPEC)
592 if(unix_peer(sk))
594 unix_unlock(unix_peer(sk));
595 unix_peer(sk) = NULL;
596 sock->state=SS_UNCONNECTED;
598 return0;
601 alen =unix_mkname(sunaddr, alen, &hash);
602 if(alen <0)
603 return alen;
605 other=unix_find_other(sunaddr, alen, sock->type, hash, &err);
606 if(!other)
607 return err;
608 if(!unix_may_send(sk, other))
610 unix_unlock(other);
611 return-EINVAL;
615 * If it was connected, reconnect.
617 if(unix_peer(sk))
619 unix_unlock(unix_peer(sk));
620 unix_peer(sk)=NULL;
622 unix_peer(sk)=other;
623 if(sock->passcred && !sk->protinfo.af_unix.addr)
624 unix_autobind(sock);
625 return0;
628 static intunix_stream_connect1(struct socket *sock,struct msghdr *msg,
629 int len,struct unix_skb_parms *cmsg,int nonblock)
631 struct sockaddr_un *sunaddr=(struct sockaddr_un *)msg->msg_name;
632 struct sock *sk = sock->sk;
633 unix_socket *other;
634 struct sk_buff *skb;
635 int err;
636 unsigned hash;
637 int addr_len;
639 addr_len =unix_mkname(sunaddr, msg->msg_namelen, &hash);
640 if(addr_len <0)
641 return addr_len;
643 switch(sock->state)
645 case SS_UNCONNECTED:
646 /* This is ok... continue with connect */
647 break;
648 case SS_CONNECTED:
649 /* Socket is already connected */
650 return-EISCONN;
651 case SS_CONNECTING:
652 /* Not yet connected... we will check this. */
653 break;
654 default:
655 return(-EINVAL);
659 if(unix_peer(sk))
661 if(sock->state==SS_CONNECTING && sk->state==TCP_ESTABLISHED)
663 sock->state=SS_CONNECTED;
664 if(!sk->protinfo.af_unix.addr)
665 unix_autobind(sock);
666 return0;
668 if(sock->state==SS_CONNECTING && sk->state == TCP_CLOSE)
670 sock->state=SS_UNCONNECTED;
671 return-ECONNREFUSED;
673 if(sock->state!=SS_CONNECTING)
674 return-EISCONN;
675 if(nonblock)
676 return-EALREADY;
678 * Drop through the connect up logic to the wait.
682 if(sock->state==SS_UNCONNECTED)
685 * Now ready to connect
688 skb=sock_alloc_send_skb(sk, len,0, nonblock, &err);/* Marker object */
689 if(skb==NULL)
690 return err;
691 memcpy(&UNIXCB(skb), cmsg,sizeof(*cmsg));
692 if(len)
693 memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
694 sk->state=TCP_CLOSE;
695 other=unix_find_other(sunaddr, addr_len, sk->type, hash, &err);
696 if(other==NULL)
698 kfree_skb(skb, FREE_WRITE);
699 return err;
701 other->ack_backlog++;
702 unix_peer(sk)=other;
703 skb_queue_tail(&other->receive_queue,skb);
704 sk->state=TCP_SYN_SENT;
705 sock->state=SS_CONNECTING;
706 other->data_ready(other,0);/* Wake up ! */
710 /* Wait for an accept */
712 while(sk->state==TCP_SYN_SENT)
714 if(nonblock)
715 return-EINPROGRESS;
716 interruptible_sleep_on(sk->sleep);
717 if(current->signal & ~current->blocked)
718 return-ERESTARTSYS;
722 * Has the other end closed on us ?
725 if(sk->state==TCP_CLOSE)
727 unix_unlock(unix_peer(sk));
728 unix_peer(sk)=NULL;
729 sock->state=SS_UNCONNECTED;
730 return-ECONNREFUSED;
734 * Amazingly it has worked
737 sock->state=SS_CONNECTED;
738 if(!sk->protinfo.af_unix.addr)
739 unix_autobind(sock);
740 return0;
744 static intunix_stream_connect(struct socket *sock,struct sockaddr *uaddr,
745 int addr_len,int flags)
747 struct msghdr msg;
748 struct unix_skb_parms cmsg;
750 msg.msg_name = uaddr;
751 msg.msg_namelen = addr_len;
752 cmsg.fp = NULL;
753 cmsg.attr = MSG_SYN;
754 cmsg.creds.pid = current->pid;
755 cmsg.creds.uid = current->euid;
756 cmsg.creds.gid = current->egid;
758 returnunix_stream_connect1(sock, &msg,0, &cmsg, flags&O_NONBLOCK);
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 if(sk->protinfo.af_unix.addr)
800 atomic_inc(&sk->protinfo.af_unix.addr->refcnt);
801 newsk->protinfo.af_unix.addr=sk->protinfo.af_unix.addr;
803 if(sk->protinfo.af_unix.inode)
805 sk->protinfo.af_unix.inode->i_count++;/* Should use dentry */
806 newsk->protinfo.af_unix.inode=sk->protinfo.af_unix.inode;
809 for(;;)
811 skb=skb_dequeue(&sk->receive_queue);
812 if(skb==NULL)
814 if(flags&O_NONBLOCK)
815 return-EAGAIN;
816 interruptible_sleep_on(sk->sleep);
817 if(current->signal & ~current->blocked)
818 return-ERESTARTSYS;
819 continue;
821 if(!(UNIXCB(skb).attr & MSG_SYN))
823 tsk=skb->sk;
824 tsk->state_change(tsk);
825 kfree_skb(skb, FREE_WRITE);
826 continue;
828 break;
831 tsk=skb->sk;
832 sk->ack_backlog--;
833 unix_peer(newsk)=tsk;
834 unix_peer(tsk)=newsk;
835 tsk->state=TCP_ESTABLISHED;
836 newsk->state=TCP_ESTABLISHED;
837 memcpy(&newsk->peercred,UNIXCREDS(skb),sizeof(struct ucred));
838 tsk->peercred.pid = current->pid;
839 tsk->peercred.uid = current->euid;
840 tsk->peercred.gid = current->egid;
841 unix_lock(newsk);/* Swap lock over */
842 unix_unlock(sk);/* Locked to child socket not master */
843 unix_lock(tsk);/* Back lock */
844 kfree_skb(skb, FREE_WRITE);/* The buffer is just used as a tag */
845 tsk->state_change(tsk);/* Wake up any sleeping connect */
846 sock_wake_async(tsk->socket,0);
847 return0;
851 static intunix_getname(struct socket *sock,struct sockaddr *uaddr,int*uaddr_len,int peer)
853 struct sock *sk = sock->sk;
854 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
856 if(peer)
858 if(!unix_peer(sk))
859 return-ENOTCONN;
860 sk=unix_peer(sk);
862 if(!sk->protinfo.af_unix.addr)
864 sunaddr->sun_family = AF_UNIX;
865 sunaddr->sun_path[0] =0;
866 *uaddr_len =sizeof(short);
867 return0;/* Not bound */
869 *uaddr_len = sk->protinfo.af_unix.addr->len;
870 memcpy(sunaddr, sk->protinfo.af_unix.addr->name, *uaddr_len);
871 return0;
874 static voidunix_detach_fds(struct scm_cookie *scm,struct sk_buff *skb)
876 int i;
878 scm->fp =UNIXCB(skb).fp;
879 skb->destructor = sock_wfree;
880 UNIXCB(skb).fp = NULL;
882 for(i=scm->fp->count-1; i>=0; i--)
883 unix_notinflight(scm->fp->fp[i]);
886 static voidunix_destruct_fds(struct sk_buff *skb)
888 struct scm_cookie scm;
889 memset(&scm,0,sizeof(scm));
890 unix_detach_fds(&scm, skb);
891 scm_destroy(&scm);
892 sock_wfree(skb);
895 static voidunix_attach_fds(struct scm_cookie *scm,struct sk_buff *skb)
897 int i;
898 for(i=scm->fp->count-1; i>=0; i--)
899 unix_inflight(scm->fp->fp[i]);
900 UNIXCB(skb).fp = scm->fp;
901 skb->destructor = unix_destruct_fds;
902 scm->fp = NULL;
907 * Send AF_UNIX data.
910 static intunix_dgram_sendmsg(struct socket *sock,struct msghdr *msg,int len,
911 struct scm_cookie *scm)
913 struct sock *sk = sock->sk;
914 unix_socket *other;
915 struct sockaddr_un *sunaddr=msg->msg_name;
916 int namelen =0;/* fake GCC */
917 int err;
918 unsigned hash;
919 struct sk_buff *skb;
921 if(msg->msg_flags&MSG_OOB)
922 return-EOPNOTSUPP;
924 if(msg->msg_flags&~MSG_DONTWAIT)
925 return-EINVAL;
927 if(msg->msg_namelen) {
928 namelen =unix_mkname(sunaddr, msg->msg_namelen, &hash);
929 if(namelen <0)
930 return namelen;
931 }else{
932 sunaddr = NULL;
933 if(!unix_peer(sk))
934 return-ENOTCONN;
937 if(sock->passcred && !sk->protinfo.af_unix.addr)
938 unix_autobind(sock);
940 skb =sock_alloc_send_skb(sk, len,0, msg->msg_flags&MSG_DONTWAIT, &err);
942 if(skb==NULL)
943 return err;
945 memcpy(UNIXCREDS(skb), &scm->creds,sizeof(struct ucred));
946 UNIXCB(skb).attr = msg->msg_flags;
947 if(scm->fp)
948 unix_attach_fds(scm, skb);
950 skb->h.raw = skb->data;
951 memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
953 other =unix_peer(sk);
954 if(other && other->dead)
957 * Check with 1003.1g - what should
958 * datagram error
960 unix_unlock(other);
961 unix_peer(sk)=NULL;
962 other = NULL;
963 if(sunaddr == NULL) {
964 kfree_skb(skb, FREE_WRITE);
965 return-ECONNRESET;
968 if(!other)
970 other =unix_find_other(sunaddr, namelen, sk->type, hash, &err);
972 if(other==NULL)
974 kfree_skb(skb, FREE_WRITE);
975 return err;
977 if(!unix_may_send(sk, other))
979 unix_unlock(other);
980 kfree_skb(skb, FREE_WRITE);
981 return-EINVAL;
985 skb_queue_tail(&other->receive_queue, skb);
986 other->data_ready(other,len);
988 if(!unix_peer(sk))
989 unix_unlock(other);
990 return len;
994 static intunix_stream_sendmsg(struct socket *sock,struct msghdr *msg,int len,
995 struct scm_cookie *scm)
997 struct sock *sk = sock->sk;
998 unix_socket *other;
999 struct sockaddr_un *sunaddr=msg->msg_name;
1000 int err,size;
1001 struct sk_buff *skb;
1002 int limit=0;
1003 int sent=0;
1005 if(sock->flags & SO_ACCEPTCON)
1006 return(-EINVAL);
1008 if(msg->msg_flags&MSG_OOB)
1009 return-EOPNOTSUPP;
1011 if(msg->msg_flags&~MSG_DONTWAIT)
1012 return-EINVAL;
1014 if(msg->msg_namelen) {
1015 if(sk->state==TCP_ESTABLISHED)
1016 return-EISCONN;
1017 else
1018 return-EOPNOTSUPP;
1019 }else{
1020 sunaddr = NULL;
1021 if(!unix_peer(sk))
1022 return-ENOTCONN;
1025 if(sk->shutdown&SEND_SHUTDOWN) {
1026 send_sig(SIGPIPE,current,0);
1027 return-EPIPE;
1030 while(sent < len)
1033 * Optimisation for the fact that under 0.01% of X messages typically
1034 * need breaking up.
1037 size=len-sent;
1039 if(size>(sk->sndbuf-sizeof(struct sk_buff))/2)/* Keep two messages in the pipe so it schedules better */
1040 size=(sk->sndbuf-sizeof(struct sk_buff))/2;
1043 * Keep to page sized kmalloc()'s as various people
1044 * have suggested. Big mallocs stress the vm too
1045 * much.
1048 if(size >3500)
1049 limit =3500;/* Fall back to a page if we can't grab a big buffer this instant */
1050 else
1051 limit =0;/* Otherwise just grab and wait */
1054 * Grab a buffer
1057 skb=sock_alloc_send_skb(sk,size,limit,msg->msg_flags&MSG_DONTWAIT, &err);
1059 if(skb==NULL)
1061 if(sent)
1062 return sent;
1063 return err;
1067 * If you pass two values to the sock_alloc_send_skb
1068 * it tries to grab the large buffer with GFP_BUFFER
1069 * (which can fail easily), and if it fails grab the
1070 * fallback size buffer which is under a page and will
1071 * succeed. [Alan]
1073 size =min(size,skb_tailroom(skb));
1075 memcpy(UNIXCREDS(skb), &scm->creds,sizeof(struct ucred));
1076 UNIXCB(skb).attr = msg->msg_flags;
1077 if(scm->fp)
1078 unix_attach_fds(scm, skb);
1080 memcpy_fromiovec(skb_put(skb,size), msg->msg_iov, size);
1082 other=unix_peer(sk);
1084 if(other->dead || (sk->shutdown & SEND_SHUTDOWN))
1086 kfree_skb(skb, FREE_WRITE);
1087 if(sent)
1088 return sent;
1089 send_sig(SIGPIPE,current,0);
1090 return-EPIPE;
1093 skb_queue_tail(&other->receive_queue, skb);
1094 other->data_ready(other,size);
1095 sent+=size;
1097 return sent;
1101 * Sleep until data has arrive. But check for races..
1104 static voidunix_data_wait(unix_socket * sk)
1106 if(!skb_peek(&sk->receive_queue))
1108 sk->socket->flags |= SO_WAITDATA;
1109 interruptible_sleep_on(sk->sleep);
1110 sk->socket->flags &= ~SO_WAITDATA;
1114 static intunix_dgram_recvmsg(struct socket *sock,struct msghdr *msg,int size,
1115 int flags,struct scm_cookie *scm)
1117 struct sock *sk = sock->sk;
1118 int noblock = flags & MSG_DONTWAIT;
1119 struct sk_buff *skb;
1120 int err;
1122 if(flags&MSG_OOB)
1123 return-EOPNOTSUPP;
1125 msg->msg_namelen =0;
1127 skb=skb_recv_datagram(sk, flags, noblock, &err);
1128 if(skb==NULL)
1129 return err;
1131 if(msg->msg_name)
1133 if(skb->sk->protinfo.af_unix.addr)
1135 memcpy(msg->msg_name, skb->sk->protinfo.af_unix.addr->name,
1136 skb->sk->protinfo.af_unix.addr->len);
1137 msg->msg_namelen=skb->sk->protinfo.af_unix.addr->len;
1139 else
1140 msg->msg_namelen=sizeof(short);
1143 if(size > skb->len)
1144 size = skb->len;
1145 else if(size < skb->len)
1146 msg->msg_flags |= MSG_TRUNC;
1148 if(skb_copy_datagram_iovec(skb,0, msg->msg_iov, size))
1149 return-EFAULT;
1151 scm->creds = *UNIXCREDS(skb);
1153 if(!(flags & MSG_PEEK))
1155 if(UNIXCB(skb).fp)
1156 unix_detach_fds(scm, skb);
1158 else
1160 /* It is questionable: on PEEK we could:
1161 - do not return fds - good, but too simple 8)
1162 - return fds, and do not return them on read (old strategy,
1163 apparently wrong)
1164 - clone fds (I choosed it for now, it is the most universal
1165 solution)
1167 POSIX 1003.1g does not actually define this clearly
1168 at all. POSIX 1003.1g doesn't define a lot of things
1169 clearly however!
1172 if(UNIXCB(skb).fp)
1173 scm->fp =scm_fp_dup(UNIXCB(skb).fp);
1175 skb_free_datagram(sk,skb);
1176 return size;
1180 static intunix_stream_recvmsg(struct socket *sock,struct msghdr *msg,int size,
1181 int flags,struct scm_cookie *scm)
1183 struct sock *sk = sock->sk;
1184 int noblock = flags & MSG_DONTWAIT;
1185 struct sockaddr_un *sunaddr=msg->msg_name;
1186 int copied =0;
1187 int check_creds =0;
1188 int target =1;
1190 if(sock->flags & SO_ACCEPTCON)
1191 return(-EINVAL);
1193 if(flags&MSG_OOB)
1194 return-EOPNOTSUPP;
1195 if(flags&MSG_WAITALL)
1196 target = size;
1199 msg->msg_namelen =0;
1201 /* Lock the socket to prevent queue disordering
1202 * while sleeps in memcpy_tomsg
1205 down(&sk->protinfo.af_unix.readsem);
1209 int chunk;
1210 struct sk_buff *skb;
1212 skb=skb_dequeue(&sk->receive_queue);
1213 if(skb==NULL)
1215 if(copied >= target)
1216 break;
1218 if(sk->err)
1219 returnsock_error(sk);
1221 if(sk->shutdown & RCV_SHUTDOWN)
1222 break;
1223 up(&sk->protinfo.af_unix.readsem);
1224 if(noblock)
1225 return-EAGAIN;
1226 unix_data_wait(sk);
1227 if(current->signal & ~current->blocked)
1228 return-ERESTARTSYS;
1229 down(&sk->protinfo.af_unix.readsem);
1230 continue;
1233 /* Never glue messages from different writers */
1234 if(check_creds &&
1235 memcmp(UNIXCREDS(skb), &scm->creds,sizeof(scm->creds)) !=0)
1237 skb_queue_head(&sk->receive_queue, skb);
1238 break;
1241 /* Copy address just once */
1242 if(sunaddr)
1244 if(skb->sk->protinfo.af_unix.addr)
1246 memcpy(sunaddr, skb->sk->protinfo.af_unix.addr->name,
1247 skb->sk->protinfo.af_unix.addr->len);
1248 msg->msg_namelen=skb->sk->protinfo.af_unix.addr->len;
1250 else
1251 msg->msg_namelen=sizeof(short);
1252 sunaddr = NULL;
1255 chunk =min(skb->len, size);
1256 memcpy_toiovec(msg->msg_iov, skb->data, chunk);
1257 copied += chunk;
1258 size -= chunk;
1260 /* Copy credentials */
1261 scm->creds = *UNIXCREDS(skb);
1262 check_creds =1;
1264 /* Mark read part of skb as used */
1265 if(!(flags & MSG_PEEK))
1267 skb_pull(skb, chunk);
1269 if(UNIXCB(skb).fp)
1270 unix_detach_fds(scm, skb);
1272 /* put the skb back if we didn't use it up.. */
1273 if(skb->len)
1275 skb_queue_head(&sk->receive_queue, skb);
1276 break;
1279 kfree_skb(skb, FREE_WRITE);
1281 if(scm->fp)
1282 break;
1284 else
1286 /* It is questionable, see note in unix_dgram_recvmsg.
1289 if(UNIXCB(skb).fp)
1290 scm->fp =scm_fp_dup(UNIXCB(skb).fp);
1292 /* put message back and return */
1293 skb_queue_head(&sk->receive_queue, skb);
1294 break;
1296 }while(size);
1298 up(&sk->protinfo.af_unix.readsem);
1299 return copied;
1302 static intunix_shutdown(struct socket *sock,int mode)
1304 struct sock *sk = sock->sk;
1305 unix_socket *other=unix_peer(sk);
1307 mode++;
1309 if(mode&SEND_SHUTDOWN)
1311 sk->shutdown|=SEND_SHUTDOWN;
1312 sk->state_change(sk);
1313 if(other && sk->type == SOCK_STREAM && other->state != TCP_LISTEN)
1315 if(unix_our_peer(sk, other))
1316 other->shutdown|=RCV_SHUTDOWN;
1317 other->state_change(other);
1320 other=unix_peer(sk);
1321 if(mode&RCV_SHUTDOWN)
1323 sk->shutdown|=RCV_SHUTDOWN;
1324 sk->state_change(sk);
1325 if(other && sk->type != SOCK_DGRAM && other->state != TCP_LISTEN)
1327 if(unix_our_peer(sk, other))
1328 other->shutdown|=SEND_SHUTDOWN;
1329 other->state_change(other);
1332 return0;
1336 static intunix_ioctl(struct socket *sock,unsigned int cmd,unsigned long arg)
1338 struct sock *sk = sock->sk;
1339 long amount=0;
1341 switch(cmd)
1344 case TIOCOUTQ:
1345 amount = sk->sndbuf -atomic_read(&sk->wmem_alloc);
1346 if(amount<0)
1347 amount=0;
1348 returnput_user(amount, (int*)arg);
1349 case TIOCINQ:
1351 struct sk_buff *skb;
1352 if(sk->state==TCP_LISTEN)
1353 return-EINVAL;
1355 * These two are safe on current systems as
1356 * only user tasks fiddle here
1358 if((skb=skb_peek(&sk->receive_queue))!=NULL)
1359 amount=skb->len;
1360 returnput_user(amount, (int*)arg);
1363 default:
1364 return-EINVAL;
1366 /*NOTREACHED*/
1367 return(0);
1370 #ifdef CONFIG_PROC_FS
1371 static intunix_read_proc(char*buffer,char**start, off_t offset,
1372 int length,int*eof,void*data)
1374 off_t pos=0;
1375 off_t begin=0;
1376 int len=0;
1377 int i;
1378 unix_socket *s;
1380 len+=sprintf(buffer,"Num RefCount Protocol Flags Type St "
1381 "Inode Path\n");
1383 forall_unix_sockets(i,s)
1385 len+=sprintf(buffer+len,"%p: %08X %08X %08lX %04X %02X %5ld",
1387 s->sock_readers,
1389 s->socket ? s->socket->flags :0,
1390 s->type,
1391 s->socket ? s->socket->state :0,
1392 s->socket ? s->socket->inode->i_ino :0);
1394 if(s->protinfo.af_unix.addr)
1396 buffer[len++] =' ';
1397 memcpy(buffer+len, s->protinfo.af_unix.addr->name->sun_path,
1398 s->protinfo.af_unix.addr->len-sizeof(short));
1399 if(!UNIX_ABSTRACT(s))
1400 len--;
1401 else
1402 buffer[len] ='@';
1403 len += s->protinfo.af_unix.addr->len -sizeof(short);
1405 buffer[len++]='\n';
1407 pos=begin+len;
1408 if(pos<offset)
1410 len=0;
1411 begin=pos;
1413 if(pos>offset+length)
1414 goto done;
1416 *eof =1;
1417 done:
1418 *start=buffer+(offset-begin);
1419 len-=(offset-begin);
1420 if(len>length)
1421 len=length;
1422 return len;
1424 #endif
1426 struct proto_ops unix_stream_ops = {
1427 AF_UNIX,
1429 unix_dup,
1430 unix_release,
1431 unix_bind,
1432 unix_stream_connect,
1433 unix_socketpair,
1434 unix_accept,
1435 unix_getname,
1436 datagram_poll,
1437 unix_ioctl,
1438 unix_listen,
1439 unix_shutdown,
1440 sock_no_setsockopt,
1441 sock_no_getsockopt,
1442 sock_no_fcntl,
1443 unix_stream_sendmsg,
1444 unix_stream_recvmsg
1447 struct proto_ops unix_dgram_ops = {
1448 AF_UNIX,
1450 unix_dup,
1451 unix_release,
1452 unix_bind,
1453 unix_dgram_connect,
1454 unix_socketpair,
1455 NULL,
1456 unix_getname,
1457 datagram_poll,
1458 unix_ioctl,
1459 sock_no_listen,
1460 unix_shutdown,
1461 sock_no_setsockopt,
1462 sock_no_getsockopt,
1463 sock_no_fcntl,
1464 unix_dgram_sendmsg,
1465 unix_dgram_recvmsg
1468 struct net_proto_family unix_family_ops = {
1469 AF_UNIX,
1470 unix_create
1473 __initfunc(voidunix_proto_init(struct net_proto *pro))
1475 struct sk_buff *dummy_skb;
1476 struct proc_dir_entry *ent;
1478 printk(KERN_INFO "NET3: Unix domain sockets 0.16 for Linux NET3.038.\n");
1479 if(sizeof(struct unix_skb_parms) >sizeof(dummy_skb->cb))
1481 printk(KERN_CRIT "unix_proto_init: panic\n");
1482 return;
1484 sock_register(&unix_family_ops);
1485 #ifdef CONFIG_PROC_FS
1486 ent =create_proc_entry("net/unix",0,0);
1487 ent->read_proc = unix_read_proc;
1488 #endif
1491 * Local variables:
1492 * compile-command: "gcc -g -D__KERNEL__ -Wall -O6 -I/usr/src/linux/include -c af_unix.c"
1493 * End:
close