- Revert TCP delayed ACK fix, and fix correctly.
[davej-history.git] / net / unix / af_unix.c
blobb04072d808a4effe8775a0330c3b67e903a9bd3f
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
27 * Andreas Schwab : Replace inode by dentry for proper
28 * reference counting
29 * Kirk Petersen : Made this a module
31 * Known differences from reference BSD that was tested:
33 * [TO FIX]
34 * ECONNREFUSED is not returned from one end of a connected() socket to the
35 * other the moment one end closes.
36 * fstat() doesn't return st_dev=NODEV, and give the blksize as high water mark
37 * and a fake inode identifier (nor the BSD first socket fstat twice bug).
38 * [NOT TO FIX]
39 * accept() returns a path name even if the connecting socket has closed
40 * in the meantime (BSD loses the path and gives up).
41 * accept() returns 0 length path for an unbound connector. BSD returns 16
42 * and a null first byte in the path (but not for gethost/peername - BSD bug ??)
43 * socketpair(...SOCK_RAW..) doesn't panic the kernel.
44 * BSD af_unix apparently has connect forgetting to block properly.
45 * (need to check this with the POSIX spec in detail)
47 * Differences from 2.0.0-11-... (ANK)
48 * Bug fixes and improvements.
49 * - client shutdown killed server socket.
50 * - removed all useless cli/sti pairs.
52 * Semantic changes/extensions.
53 * - generic control message passing.
54 * - SCM_CREDENTIALS control message.
55 * - "Abstract" (not FS based) socket bindings.
56 * Abstract names are sequences of bytes (not zero terminated)
57 * started by 0, so that this name space does not intersect
58 * with BSD names.
61 #include <linux/module.h>
62 #include <linux/config.h>
63 #include <linux/kernel.h>
64 #include <linux/major.h>
65 #include <linux/signal.h>
66 #include <linux/sched.h>
67 #include <linux/errno.h>
68 #include <linux/string.h>
69 #include <linux/stat.h>
70 #include <linux/socket.h>
71 #include <linux/un.h>
72 #include <linux/fcntl.h>
73 #include <linux/termios.h>
74 #include <linux/socket.h>
75 #include <linux/sockios.h>
76 #include <linux/net.h>
77 #include <linux/in.h>
78 #include <linux/fs.h>
79 #include <linux/malloc.h>
80 #include <asm/uaccess.h>
81 #include <linux/skbuff.h>
82 #include <linux/netdevice.h>
83 #include <net/sock.h>
84 #include <net/tcp.h>
85 #include <net/af_unix.h>
86 #include <linux/proc_fs.h>
87 #include <net/scm.h>
88 #include <linux/init.h>
90 #include <asm/checksum.h>
92 #define min(a,b) (((a)<(b))?(a):(b))
94 int sysctl_unix_delete_delay = HZ;
95 int sysctl_unix_destroy_delay =10*HZ;
97 unix_socket *unix_socket_table[UNIX_HASH_SIZE+1];
99 #define unix_sockets_unbound (unix_socket_table[UNIX_HASH_SIZE])
101 #define UNIX_ABSTRACT(sk) ((sk)->protinfo.af_unix.addr->hash!=UNIX_HASH_SIZE)
103 extern __inline__ unsignedunix_hash_fold(unsigned hash)
105 hash ^= hash>>16;
106 hash ^= hash>>8;
107 hash ^= hash>>4;
108 return hash;
111 #define unix_peer(sk) ((sk)->pair)
113 extern __inline__ intunix_our_peer(unix_socket *sk, unix_socket *osk)
115 returnunix_peer(osk) == sk;
118 extern __inline__ intunix_may_send(unix_socket *sk, unix_socket *osk)
120 return(sk->type==osk->type);
123 extern __inline__ voidunix_lock(unix_socket *sk)
125 sk->sock_readers++;
128 extern __inline__ intunix_unlock(unix_socket *sk)
130 return sk->sock_readers--;
133 extern __inline__ intunix_locked(unix_socket *sk)
135 return sk->sock_readers;
138 extern __inline__ voidunix_release_addr(struct unix_address *addr)
140 if(addr)
142 if(atomic_dec_and_test(&addr->refcnt))
143 kfree(addr);
147 static voidunix_destruct_addr(struct sock *sk)
149 struct unix_address *addr = sk->protinfo.af_unix.addr;
151 unix_release_addr(addr);
155 * Check unix socket name:
156 * - should be not zero length.
157 * - if started by not zero, should be NULL terminated (FS object)
158 * - if started by zero, it is abstract name.
161 static intunix_mkname(struct sockaddr_un * sunaddr,int len,unsigned*hashp)
163 if(len <=sizeof(short) || len >sizeof(*sunaddr))
164 return-EINVAL;
165 if(!sunaddr || sunaddr->sun_family != AF_UNIX)
166 return-EINVAL;
167 if(sunaddr->sun_path[0])
170 * This may look like an off by one error but it is
171 * a bit more subtle. 108 is the longest valid AF_UNIX
172 * path for a binding. sun_path[108] doesnt as such
173 * exist. However in kernel space we are guaranteed that
174 * it is a valid memory location in our kernel
175 * address buffer.
177 if(len >sizeof(*sunaddr))
178 len =sizeof(*sunaddr);
179 ((char*)sunaddr)[len]=0;
180 len =strlen(sunaddr->sun_path)+1+sizeof(short);
181 return len;
184 *hashp =unix_hash_fold(csum_partial((char*)sunaddr, len,0));
185 return len;
188 static voidunix_remove_socket(unix_socket *sk)
190 unix_socket **list = sk->protinfo.af_unix.list;
191 if(sk->next)
192 sk->next->prev = sk->prev;
193 if(sk->prev)
194 sk->prev->next = sk->next;
195 if(*list == sk)
196 *list = sk->next;
197 sk->protinfo.af_unix.list = NULL;
198 sk->prev = NULL;
199 sk->next = NULL;
202 static voidunix_insert_socket(unix_socket *sk)
204 unix_socket **list = sk->protinfo.af_unix.list;
205 sk->prev = NULL;
206 sk->next = *list;
207 if(*list)
208 (*list)->prev = sk;
209 *list=sk;
212 static unix_socket *unix_find_socket_byname(struct sockaddr_un *sunname,
213 int len,int type,unsigned hash)
215 unix_socket *s;
217 for(s=unix_socket_table[(hash^type)&0xF]; s; s=s->next)
219 if(s->protinfo.af_unix.addr->len==len &&
220 memcmp(s->protinfo.af_unix.addr->name, sunname, len) ==0&&
221 s->type == type)
223 unix_lock(s);
224 return(s);
227 return(NULL);
230 static unix_socket *unix_find_socket_byinode(struct inode *i)
232 unix_socket *s;
234 for(s=unix_socket_table[i->i_ino &0xF]; s; s=s->next)
236 struct dentry *dentry = s->protinfo.af_unix.dentry;
238 if(dentry && dentry->d_inode == i)
240 unix_lock(s);
241 return(s);
244 return(NULL);
248 * Delete a unix socket. We have to allow for deferring this on a timer.
251 static voidunix_destroy_timer(unsigned long data)
253 unix_socket *sk=(unix_socket *)data;
254 if(!unix_locked(sk) &&atomic_read(&sk->wmem_alloc) ==0)
256 sk_free(sk);
257 return;
261 * Retry;
264 sk->timer.expires=jiffies+sysctl_unix_destroy_delay;/* No real hurry try it every 10 seconds or so */
265 add_timer(&sk->timer);
269 static voidunix_delayed_delete(unix_socket *sk)
271 sk->timer.data=(unsigned long)sk;
272 sk->timer.expires=jiffies+sysctl_unix_delete_delay;/* Normally 1 second after will clean up. After that we try every 10 */
273 sk->timer.function=unix_destroy_timer;
274 add_timer(&sk->timer);
277 static voidunix_destroy_socket(unix_socket *sk)
279 struct sk_buff *skb;
281 unix_remove_socket(sk);
283 while((skb=skb_dequeue(&sk->receive_queue))!=NULL)
285 if(sk->state==TCP_LISTEN)
287 unix_socket *osk=skb->sk;
288 osk->state=TCP_CLOSE;
289 kfree_skb(skb);/* Now surplus - free the skb first before the socket */
290 osk->state_change(osk);/* So the connect wakes and cleans up (if any) */
291 /* osk will be destroyed when it gets to close or the timer fires */
293 else
295 /* passed fds are erased in the kfree_skb hook */
296 kfree_skb(skb);
300 if(sk->protinfo.af_unix.dentry!=NULL)
302 dput(sk->protinfo.af_unix.dentry);
303 sk->protinfo.af_unix.dentry=NULL;
306 if(!unix_unlock(sk) &&atomic_read(&sk->wmem_alloc) ==0)
308 sk_free(sk);
310 else
312 sk->dead=1;
313 unix_delayed_delete(sk);/* Try every so often until buffers are all freed */
316 /* socket destroyed, decrement count */
317 MOD_DEC_USE_COUNT;
320 static intunix_listen(struct socket *sock,int backlog)
322 struct sock *sk = sock->sk;
324 if(sock->state != SS_UNCONNECTED)
325 return(-EINVAL);
326 if(sock->type!=SOCK_STREAM)
327 return-EOPNOTSUPP;/* Only stream sockets accept */
328 if(!sk->protinfo.af_unix.addr)
329 return-EINVAL;/* No listens on an unbound socket */
330 sk->max_ack_backlog=backlog;
331 if(sk->ack_backlog < backlog)
332 sk->state_change(sk);
333 sk->state=TCP_LISTEN;
334 sock->flags |= SO_ACCEPTCON;
335 return0;
338 externstruct proto_ops unix_stream_ops;
339 externstruct proto_ops unix_dgram_ops;
341 static intunix_create(struct socket *sock,int protocol)
343 struct sock *sk;
345 sock->state = SS_UNCONNECTED;
347 if(protocol && protocol != PF_UNIX)
348 return-EPROTONOSUPPORT;
350 switch(sock->type)
352 case SOCK_STREAM:
353 sock->ops = &unix_stream_ops;
354 break;
356 * Believe it or not BSD has AF_UNIX, SOCK_RAW though
357 * nothing uses it.
359 case SOCK_RAW:
360 sock->type=SOCK_DGRAM;
361 case SOCK_DGRAM:
362 sock->ops = &unix_dgram_ops;
363 break;
364 default:
365 return-ESOCKTNOSUPPORT;
367 sk =sk_alloc(AF_UNIX, GFP_KERNEL,1);
368 if(!sk)
369 return-ENOMEM;
371 sock_init_data(sock,sk);
373 sk->destruct = unix_destruct_addr;
374 sk->protinfo.af_unix.family=AF_UNIX;
375 sk->protinfo.af_unix.dentry=NULL;
376 sk->sock_readers=1;/* Us */
377 sk->protinfo.af_unix.readsem=MUTEX;/* single task reading lock */
378 sk->mtu=4096;
379 sk->protinfo.af_unix.list=&unix_sockets_unbound;
380 unix_insert_socket(sk);
382 /* socket created, increment count */
383 MOD_INC_USE_COUNT;
385 return0;
388 static intunix_release(struct socket *sock,struct socket *peer)
390 unix_socket *sk = sock->sk;
391 unix_socket *skpair;
393 if(!sk)
394 return0;
396 if(sock->state != SS_UNCONNECTED)
397 sock->state = SS_DISCONNECTING;
399 sk->state_change(sk);
400 sk->dead=1;
401 skpair=unix_peer(sk);
402 if(sock->type==SOCK_STREAM && skpair)
404 if(unix_our_peer(sk, skpair))
405 skpair->shutdown=SHUTDOWN_MASK;/* No more writes */
406 if(skpair->state!=TCP_LISTEN)
407 skpair->state_change(skpair);/* Wake any blocked writes */
409 if(skpair!=NULL)
410 unix_unlock(skpair);/* It may now die */
411 unix_peer(sk)=NULL;/* No pair */
412 unix_destroy_socket(sk);/* Try to flush out this socket. Throw out buffers at least */
413 unix_gc();/* Garbage collect fds */
416 * FIXME: BSD difference: In BSD all sockets connected to use get ECONNRESET and we die on the spot. In
417 * Linux we behave like files and pipes do and wait for the last dereference.
419 if(sk->socket)
421 sk->socket = NULL;
422 sock->sk = NULL;
425 return0;
428 static intunix_autobind(struct socket *sock)
430 struct sock *sk = sock->sk;
431 static u32 ordernum =1;
432 struct unix_address * addr;
433 unix_socket *osk;
435 addr =kmalloc(sizeof(*addr) +sizeof(short) +16, GFP_KERNEL);
436 if(!addr)
437 return-ENOBUFS;
438 if(sk->protinfo.af_unix.addr || sk->protinfo.af_unix.dentry)
440 kfree(addr);
441 return-EINVAL;
443 memset(addr,0,sizeof(*addr) +sizeof(short) +16);
444 addr->name->sun_family = AF_UNIX;
445 atomic_set(&addr->refcnt,1);
447 retry:
448 addr->len =sprintf(addr->name->sun_path+1,"%08x", ordernum) +1+sizeof(short);
449 addr->hash =unix_hash_fold(csum_partial((void*)addr->name, addr->len,0));
450 ordernum++;
452 if((osk=unix_find_socket_byname(addr->name, addr->len, sock->type,
453 addr->hash)) != NULL)
455 unix_unlock(osk);
456 goto retry;
459 sk->protinfo.af_unix.addr = addr;
460 unix_remove_socket(sk);
461 sk->protinfo.af_unix.list = &unix_socket_table[(addr->hash ^ sk->type)&0xF];
462 unix_insert_socket(sk);
463 return0;
466 static unix_socket *unix_find_other(struct sockaddr_un *sunname,int len,
467 int type,unsigned hash,int*error)
469 unix_socket *u;
471 if(sunname->sun_path[0])
473 struct dentry *dentry;
474 dentry =open_namei(sunname->sun_path,2, S_IFSOCK);
475 if(IS_ERR(dentry)) {
476 *error =PTR_ERR(dentry);
477 return NULL;
479 u=unix_find_socket_byinode(dentry->d_inode);
480 dput(dentry);
481 if(u && u->type != type)
483 *error=-EPROTOTYPE;
484 unix_unlock(u);
485 return NULL;
488 else
489 u=unix_find_socket_byname(sunname, len, type, hash);
491 if(u==NULL)
493 *error=-ECONNREFUSED;
494 return NULL;
496 return u;
500 static intunix_bind(struct socket *sock,struct sockaddr *uaddr,int addr_len)
502 struct sock *sk = sock->sk;
503 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
504 struct dentry * dentry;
505 int err;
506 unsigned hash;
507 struct unix_address *addr;
509 if(sk->protinfo.af_unix.addr || sk->protinfo.af_unix.dentry ||
510 sunaddr->sun_family != AF_UNIX)
511 return-EINVAL;
513 if(addr_len==sizeof(short))
514 returnunix_autobind(sock);
516 addr_len =unix_mkname(sunaddr, addr_len, &hash);
517 if(addr_len <0)
518 return addr_len;
520 addr =kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);
521 if(!addr)
522 return-ENOBUFS;
524 /* We slept; recheck ... */
526 if(sk->protinfo.af_unix.addr || sk->protinfo.af_unix.dentry)
528 kfree(addr);
529 return-EINVAL;/* Already bound */
532 memcpy(addr->name, sunaddr, addr_len);
533 addr->len = addr_len;
534 addr->hash = hash;
535 atomic_set(&addr->refcnt,1);
537 if(!sunaddr->sun_path[0])
539 unix_socket *osk =unix_find_socket_byname(sunaddr, addr_len,
540 sk->type, hash);
541 if(osk)
543 unix_unlock(osk);
544 kfree(addr);
545 return-EADDRINUSE;
547 unix_remove_socket(sk);
548 sk->protinfo.af_unix.addr = addr;
549 sk->protinfo.af_unix.list = &unix_socket_table[(hash^sk->type)&0xF];
550 unix_insert_socket(sk);
551 return0;
554 addr->hash = UNIX_HASH_SIZE;
555 sk->protinfo.af_unix.addr = addr;
558 dentry =do_mknod(sunaddr->sun_path, S_IFSOCK|S_IRWXUGO,0);
559 if(IS_ERR(dentry))
561 err =PTR_ERR(dentry);
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[dentry->d_inode->i_ino &0xF];
571 sk->protinfo.af_unix.dentry = dentry;
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);
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(signal_pending(current))
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.dentry)
804 newsk->protinfo.af_unix.dentry=dget(sk->protinfo.af_unix.dentry);
806 for(;;)
808 skb=skb_dequeue(&sk->receive_queue);
809 if(skb==NULL)
811 if(flags&O_NONBLOCK)
812 return-EAGAIN;
813 interruptible_sleep_on(sk->sleep);
814 if(signal_pending(current))
815 return-ERESTARTSYS;
816 continue;
818 if(!(UNIXCB(skb).attr & MSG_SYN))
820 tsk=skb->sk;
821 tsk->state_change(tsk);
822 kfree_skb(skb);
823 continue;
825 break;
828 tsk=skb->sk;
829 sk->ack_backlog--;
830 unix_peer(newsk)=tsk;
831 unix_peer(tsk)=newsk;
832 tsk->state=TCP_ESTABLISHED;
833 newsk->state=TCP_ESTABLISHED;
834 memcpy(&newsk->peercred,UNIXCREDS(skb),sizeof(struct ucred));
835 tsk->peercred.pid = current->pid;
836 tsk->peercred.uid = current->euid;
837 tsk->peercred.gid = current->egid;
838 unix_lock(newsk);/* Swap lock over */
839 unix_unlock(sk);/* Locked to child socket not master */
840 unix_lock(tsk);/* Back lock */
841 kfree_skb(skb);/* The buffer is just used as a tag */
842 tsk->state_change(tsk);/* Wake up any sleeping connect */
843 sock_wake_async(tsk->socket,0);
844 return0;
848 static intunix_getname(struct socket *sock,struct sockaddr *uaddr,int*uaddr_len,int peer)
850 struct sock *sk = sock->sk;
851 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
853 if(peer)
855 if(!unix_peer(sk))
856 return-ENOTCONN;
857 sk=unix_peer(sk);
859 if(!sk->protinfo.af_unix.addr)
861 sunaddr->sun_family = AF_UNIX;
862 sunaddr->sun_path[0] =0;
863 *uaddr_len =sizeof(short);
864 return0;/* Not bound */
866 *uaddr_len = sk->protinfo.af_unix.addr->len;
867 memcpy(sunaddr, sk->protinfo.af_unix.addr->name, *uaddr_len);
868 return0;
871 static voidunix_detach_fds(struct scm_cookie *scm,struct sk_buff *skb)
873 int i;
875 scm->fp =UNIXCB(skb).fp;
876 skb->destructor = sock_wfree;
877 UNIXCB(skb).fp = NULL;
879 for(i=scm->fp->count-1; i>=0; i--)
880 unix_notinflight(scm->fp->fp[i]);
883 static voidunix_destruct_fds(struct sk_buff *skb)
885 struct scm_cookie scm;
886 memset(&scm,0,sizeof(scm));
887 unix_detach_fds(&scm, skb);
888 scm_destroy(&scm);
889 sock_wfree(skb);
892 static voidunix_attach_fds(struct scm_cookie *scm,struct sk_buff *skb)
894 int i;
895 for(i=scm->fp->count-1; i>=0; i--)
896 unix_inflight(scm->fp->fp[i]);
897 UNIXCB(skb).fp = scm->fp;
898 skb->destructor = unix_destruct_fds;
899 scm->fp = NULL;
904 * Send AF_UNIX data.
907 static intunix_dgram_sendmsg(struct socket *sock,struct msghdr *msg,int len,
908 struct scm_cookie *scm)
910 struct sock *sk = sock->sk;
911 unix_socket *other;
912 struct sockaddr_un *sunaddr=msg->msg_name;
913 int namelen =0;/* fake GCC */
914 int err;
915 unsigned hash;
916 struct sk_buff *skb;
918 if(msg->msg_flags&MSG_OOB)
919 return-EOPNOTSUPP;
921 if(msg->msg_flags&~MSG_DONTWAIT)
922 return-EINVAL;
924 if(msg->msg_namelen) {
925 namelen =unix_mkname(sunaddr, msg->msg_namelen, &hash);
926 if(namelen <0)
927 return namelen;
928 }else{
929 sunaddr = NULL;
930 if(!unix_peer(sk))
931 return-ENOTCONN;
934 if(sock->passcred && !sk->protinfo.af_unix.addr)
935 unix_autobind(sock);
937 skb =sock_alloc_send_skb(sk, len,0, msg->msg_flags&MSG_DONTWAIT, &err);
939 if(skb==NULL)
940 return err;
942 memcpy(UNIXCREDS(skb), &scm->creds,sizeof(struct ucred));
943 UNIXCB(skb).attr = msg->msg_flags;
944 if(scm->fp)
945 unix_attach_fds(scm, skb);
947 skb->h.raw = skb->data;
948 memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
950 other =unix_peer(sk);
951 if(other && other->dead)
954 * Check with 1003.1g - what should
955 * datagram error
957 unix_unlock(other);
958 unix_peer(sk)=NULL;
959 other = NULL;
960 if(sunaddr == NULL) {
961 kfree_skb(skb);
962 return-ECONNRESET;
965 if(!other)
967 other =unix_find_other(sunaddr, namelen, sk->type, hash, &err);
969 if(other==NULL)
971 kfree_skb(skb);
972 return err;
974 if(!unix_may_send(sk, other))
976 unix_unlock(other);
977 kfree_skb(skb);
978 return-EINVAL;
982 skb_queue_tail(&other->receive_queue, skb);
983 other->data_ready(other,len);
985 if(!unix_peer(sk))
986 unix_unlock(other);
987 return len;
991 static intunix_stream_sendmsg(struct socket *sock,struct msghdr *msg,int len,
992 struct scm_cookie *scm)
994 struct sock *sk = sock->sk;
995 unix_socket *other;
996 struct sockaddr_un *sunaddr=msg->msg_name;
997 int err,size;
998 struct sk_buff *skb;
999 int limit=0;
1000 int sent=0;
1002 if(sock->flags & SO_ACCEPTCON)
1003 return(-EINVAL);
1005 if(msg->msg_flags&MSG_OOB)
1006 return-EOPNOTSUPP;
1008 if(msg->msg_flags&~MSG_DONTWAIT)
1009 return-EINVAL;
1011 if(msg->msg_namelen) {
1012 if(sk->state==TCP_ESTABLISHED)
1013 return-EISCONN;
1014 else
1015 return-EOPNOTSUPP;
1016 }else{
1017 sunaddr = NULL;
1018 if(!unix_peer(sk))
1019 return-ENOTCONN;
1022 if(sk->shutdown&SEND_SHUTDOWN) {
1023 send_sig(SIGPIPE,current,0);
1024 return-EPIPE;
1027 while(sent < len)
1030 * Optimisation for the fact that under 0.01% of X messages typically
1031 * need breaking up.
1034 size=len-sent;
1036 /* Keep two messages in the pipe so it schedules better */
1037 if(size > (sk->sndbuf -sizeof(struct sk_buff)) /2)
1038 size = (sk->sndbuf -sizeof(struct sk_buff)) /2;
1041 * Keep to page sized kmalloc()'s as various people
1042 * have suggested. Big mallocs stress the vm too
1043 * much.
1046 if(size >3500)
1047 limit =3500;/* Fall back to a page if we can't grab a big buffer this instant */
1048 else
1049 limit =0;/* Otherwise just grab and wait */
1052 * Grab a buffer
1055 skb=sock_alloc_send_skb(sk,size,limit,msg->msg_flags&MSG_DONTWAIT, &err);
1057 if(skb==NULL)
1059 if(sent)
1060 goto out;
1061 return err;
1065 * If you pass two values to the sock_alloc_send_skb
1066 * it tries to grab the large buffer with GFP_BUFFER
1067 * (which can fail easily), and if it fails grab the
1068 * fallback size buffer which is under a page and will
1069 * succeed. [Alan]
1071 size =min(size,skb_tailroom(skb));
1073 memcpy(UNIXCREDS(skb), &scm->creds,sizeof(struct ucred));
1074 UNIXCB(skb).attr = msg->msg_flags;
1075 if(scm->fp)
1076 unix_attach_fds(scm, skb);
1078 /* N.B. this could fail with -EFAULT */
1079 memcpy_fromiovec(skb_put(skb,size), msg->msg_iov, size);
1081 other=unix_peer(sk);
1083 if(other->dead || (sk->shutdown & SEND_SHUTDOWN))
1085 kfree_skb(skb);
1086 if(sent)
1087 goto out;
1088 send_sig(SIGPIPE,current,0);
1089 return-EPIPE;
1092 skb_queue_tail(&other->receive_queue, skb);
1093 other->data_ready(other,size);
1094 sent+=size;
1096 out:
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)
1129 goto out;
1131 if(msg->msg_name)
1133 msg->msg_namelen =sizeof(short);
1134 if(skb->sk->protinfo.af_unix.addr)
1136 msg->msg_namelen=skb->sk->protinfo.af_unix.addr->len;
1137 memcpy(msg->msg_name,
1138 skb->sk->protinfo.af_unix.addr->name,
1139 skb->sk->protinfo.af_unix.addr->len);
1143 if(size > skb->len)
1144 size = skb->len;
1145 else if(size < skb->len)
1146 msg->msg_flags |= MSG_TRUNC;
1148 err =skb_copy_datagram_iovec(skb,0, msg->msg_iov, size);
1149 if(err)
1150 goto out_free;
1152 scm->creds = *UNIXCREDS(skb);
1154 if(!(flags & MSG_PEEK))
1156 if(UNIXCB(skb).fp)
1157 unix_detach_fds(scm, skb);
1159 else
1161 /* It is questionable: on PEEK we could:
1162 - do not return fds - good, but too simple 8)
1163 - return fds, and do not return them on read (old strategy,
1164 apparently wrong)
1165 - clone fds (I choosed it for now, it is the most universal
1166 solution)
1168 POSIX 1003.1g does not actually define this clearly
1169 at all. POSIX 1003.1g doesn't define a lot of things
1170 clearly however!
1173 if(UNIXCB(skb).fp)
1174 scm->fp =scm_fp_dup(UNIXCB(skb).fp);
1176 err = size;
1178 out_free:
1179 skb_free_datagram(sk,skb);
1180 out:
1181 return err;
1185 static intunix_stream_recvmsg(struct socket *sock,struct msghdr *msg,int size,
1186 int flags,struct scm_cookie *scm)
1188 struct sock *sk = sock->sk;
1189 int noblock = flags & MSG_DONTWAIT;
1190 struct sockaddr_un *sunaddr=msg->msg_name;
1191 int copied =0;
1192 int check_creds =0;
1193 int target =1;
1195 if(sock->flags & SO_ACCEPTCON)
1196 return(-EINVAL);
1198 if(flags&MSG_OOB)
1199 return-EOPNOTSUPP;
1200 if(flags&MSG_WAITALL)
1201 target = size;
1204 msg->msg_namelen =0;
1206 /* Lock the socket to prevent queue disordering
1207 * while sleeps in memcpy_tomsg
1210 down(&sk->protinfo.af_unix.readsem);
1214 int chunk;
1215 struct sk_buff *skb;
1217 skb=skb_dequeue(&sk->receive_queue);
1218 if(skb==NULL)
1220 if(copied >= target)
1221 break;
1224 * POSIX 1003.1g mandates this order.
1227 if(sk->err)
1229 up(&sk->protinfo.af_unix.readsem);
1230 returnsock_error(sk);
1233 if(sk->shutdown & RCV_SHUTDOWN)
1234 break;
1235 up(&sk->protinfo.af_unix.readsem);
1236 if(noblock)
1237 return-EAGAIN;
1238 unix_data_wait(sk);
1239 if(signal_pending(current))
1240 return-ERESTARTSYS;
1241 down(&sk->protinfo.af_unix.readsem);
1242 continue;
1245 /* Never glue messages from different writers */
1246 if(check_creds &&
1247 memcmp(UNIXCREDS(skb), &scm->creds,sizeof(scm->creds)) !=0)
1249 skb_queue_head(&sk->receive_queue, skb);
1250 break;
1253 /* Copy address just once */
1254 if(sunaddr)
1256 msg->msg_namelen =sizeof(short);
1257 if(skb->sk->protinfo.af_unix.addr)
1259 msg->msg_namelen=skb->sk->protinfo.af_unix.addr->len;
1260 memcpy(sunaddr,
1261 skb->sk->protinfo.af_unix.addr->name,
1262 skb->sk->protinfo.af_unix.addr->len);
1264 sunaddr = NULL;
1267 chunk =min(skb->len, size);
1268 /* N.B. This could fail with a non-zero value (which means -EFAULT
1269 * and the non-zero value is the number of bytes not copied).
1271 memcpy_toiovec(msg->msg_iov, skb->data, chunk);
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++;
1324 if(mode&SEND_SHUTDOWN)
1326 sk->shutdown|=SEND_SHUTDOWN;
1327 sk->state_change(sk);
1328 if(other && sk->type == SOCK_STREAM && other->state != TCP_LISTEN)
1330 if(unix_our_peer(sk, other))
1331 other->shutdown|=RCV_SHUTDOWN;
1332 other->state_change(other);
1335 other=unix_peer(sk);
1336 if(mode&RCV_SHUTDOWN)
1338 sk->shutdown|=RCV_SHUTDOWN;
1339 sk->state_change(sk);
1340 if(other && sk->type != SOCK_DGRAM && other->state != TCP_LISTEN)
1342 if(unix_our_peer(sk, other))
1343 other->shutdown|=SEND_SHUTDOWN;
1344 other->state_change(other);
1347 return0;
1351 static intunix_ioctl(struct socket *sock,unsigned int cmd,unsigned long arg)
1353 struct sock *sk = sock->sk;
1354 long amount=0;
1356 switch(cmd)
1359 case TIOCOUTQ:
1360 amount = sk->sndbuf -atomic_read(&sk->wmem_alloc);
1361 if(amount<0)
1362 amount=0;
1363 returnput_user(amount, (int*)arg);
1364 case TIOCINQ:
1366 struct sk_buff *skb;
1367 if(sk->state==TCP_LISTEN)
1368 return-EINVAL;
1370 * These two are safe on current systems as
1371 * only user tasks fiddle here
1373 if((skb=skb_peek(&sk->receive_queue))!=NULL)
1374 amount=skb->len;
1375 returnput_user(amount, (int*)arg);
1378 default:
1379 return-EINVAL;
1381 /*NOTREACHED*/
1382 return(0);
1385 #ifdef CONFIG_PROC_FS
1386 static intunix_read_proc(char*buffer,char**start, off_t offset,
1387 int length,int*eof,void*data)
1389 off_t pos=0;
1390 off_t begin=0;
1391 int len=0;
1392 int i;
1393 unix_socket *s;
1395 len+=sprintf(buffer,"Num RefCount Protocol Flags Type St "
1396 "Inode Path\n");
1398 forall_unix_sockets(i,s)
1400 len+=sprintf(buffer+len,"%p: %08X %08X %08lX %04X %02X %5ld",
1402 s->sock_readers,
1404 s->socket ? s->socket->flags :0,
1405 s->type,
1406 s->socket ? s->socket->state :0,
1407 s->socket ? s->socket->inode->i_ino :0);
1409 if(s->protinfo.af_unix.addr)
1411 buffer[len++] =' ';
1412 memcpy(buffer+len, s->protinfo.af_unix.addr->name->sun_path,
1413 s->protinfo.af_unix.addr->len-sizeof(short));
1414 if(!UNIX_ABSTRACT(s))
1415 len--;
1416 else
1417 buffer[len] ='@';
1418 len += s->protinfo.af_unix.addr->len -sizeof(short);
1420 buffer[len++]='\n';
1422 pos=begin+len;
1423 if(pos<offset)
1425 len=0;
1426 begin=pos;
1428 if(pos>offset+length)
1429 goto done;
1431 *eof =1;
1432 done:
1433 *start=buffer+(offset-begin);
1434 len-=(offset-begin);
1435 if(len>length)
1436 len=length;
1437 return len;
1439 #endif
1441 struct proto_ops unix_stream_ops = {
1442 AF_UNIX,
1444 sock_no_dup,
1445 unix_release,
1446 unix_bind,
1447 unix_stream_connect,
1448 unix_socketpair,
1449 unix_accept,
1450 unix_getname,
1451 datagram_poll,
1452 unix_ioctl,
1453 unix_listen,
1454 unix_shutdown,
1455 sock_no_setsockopt,
1456 sock_no_getsockopt,
1457 sock_no_fcntl,
1458 unix_stream_sendmsg,
1459 unix_stream_recvmsg
1462 struct proto_ops unix_dgram_ops = {
1463 AF_UNIX,
1465 sock_no_dup,
1466 unix_release,
1467 unix_bind,
1468 unix_dgram_connect,
1469 unix_socketpair,
1470 sock_no_accept,
1471 unix_getname,
1472 datagram_poll,
1473 unix_ioctl,
1474 sock_no_listen,
1475 unix_shutdown,
1476 sock_no_setsockopt,
1477 sock_no_getsockopt,
1478 sock_no_fcntl,
1479 unix_dgram_sendmsg,
1480 unix_dgram_recvmsg
1483 struct net_proto_family unix_family_ops = {
1484 AF_UNIX,
1485 unix_create
1488 #ifdef MODULE
1489 #ifdef CONFIG_SYSCTL
1490 externvoidunix_sysctl_register(void);
1491 externvoidunix_sysctl_unregister(void);
1492 #endif
1494 intinit_module(void)
1495 #else
1496 __initfunc(voidunix_proto_init(struct net_proto *pro))
1497 #endif
1499 struct sk_buff *dummy_skb;
1500 struct proc_dir_entry *ent;
1502 printk(KERN_INFO "NET3: Unix domain sockets 0.16 for Linux NET3.038.\n");
1503 if(sizeof(struct unix_skb_parms) >sizeof(dummy_skb->cb))
1505 printk(KERN_CRIT "unix_proto_init: panic\n");
1506 #ifdef MODULE
1507 return-1;
1508 #else
1509 return;
1510 #endif
1512 sock_register(&unix_family_ops);
1513 #ifdef CONFIG_PROC_FS
1514 ent =create_proc_entry("net/unix",0,0);
1515 ent->read_proc = unix_read_proc;
1516 #endif
1518 #ifdef MODULE
1519 #ifdef CONFIG_SYSCTL
1520 unix_sysctl_register();
1521 #endif
1523 return0;
1524 #endif
1527 #ifdef MODULE
1528 voidcleanup_module(void)
1530 sock_unregister(AF_UNIX);
1531 #ifdef CONFIG_SYSCTL
1532 unix_sysctl_unregister();
1533 #endif
1535 #endif
1538 * Local variables:
1539 * compile-command: "gcc -g -D__KERNEL__ -Wall -O6 -I/usr/src/linux/include -c af_unix.c"
1540 * End:
close