Import 2.1.57
[davej-history.git] / net / unix / af_unix.c
blob936d6122017339dd1f94661f6295c9e479a750b7
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
30 * Known differences from reference BSD that was tested:
32 * [TO FIX]
33 * ECONNREFUSED is not returned from one end of a connected() socket to the
34 * other the moment one end closes.
35 * fstat() doesn't return st_dev=NODEV, and give the blksize as high water mark
36 * and a fake inode identifier (nor the BSD first socket fstat twice bug).
37 * [NOT TO FIX]
38 * accept() returns a path name even if the connecting socket has closed
39 * in the meantime (BSD loses the path and gives up).
40 * accept() returns 0 length path for an unbound connector. BSD returns 16
41 * and a null first byte in the path (but not for gethost/peername - BSD bug ??)
42 * socketpair(...SOCK_RAW..) doesn't panic the kernel.
43 * BSD af_unix apparently has connect forgetting to block properly.
44 * (need to check this with the POSIX spec in detail)
46 * Differences from 2.0.0-11-... (ANK)
47 * Bug fixes and improvements.
48 * - client shutdown killed server socket.
49 * - removed all useless cli/sti pairs.
51 * Semantic changes/extensions.
52 * - generic control message passing.
53 * - SCM_CREDENTIALS control message.
54 * - "Abstract" (not FS based) socket bindings.
55 * Abstract names are sequences of bytes (not zero terminated)
56 * started by 0, so that this name space does not intersect
57 * with BSD names.
60 #include <linux/config.h>
61 #include <linux/kernel.h>
62 #include <linux/major.h>
63 #include <linux/signal.h>
64 #include <linux/sched.h>
65 #include <linux/errno.h>
66 #include <linux/string.h>
67 #include <linux/stat.h>
68 #include <linux/socket.h>
69 #include <linux/un.h>
70 #include <linux/fcntl.h>
71 #include <linux/termios.h>
72 #include <linux/socket.h>
73 #include <linux/sockios.h>
74 #include <linux/net.h>
75 #include <linux/in.h>
76 #include <linux/fs.h>
77 #include <linux/malloc.h>
78 #include <asm/uaccess.h>
79 #include <linux/skbuff.h>
80 #include <linux/netdevice.h>
81 #include <net/sock.h>
82 #include <net/tcp.h>
83 #include <net/af_unix.h>
84 #include <linux/proc_fs.h>
85 #include <net/scm.h>
86 #include <linux/init.h>
88 #include <asm/checksum.h>
90 #define min(a,b) (((a)<(b))?(a):(b))
92 int sysctl_unix_delete_delay = HZ;
93 int sysctl_unix_destroy_delay =10*HZ;
95 unix_socket *unix_socket_table[UNIX_HASH_SIZE+1];
97 #define unix_sockets_unbound (unix_socket_table[UNIX_HASH_SIZE])
99 #define UNIX_ABSTRACT(sk) ((sk)->protinfo.af_unix.addr->hash!=UNIX_HASH_SIZE)
101 extern __inline__ unsignedunix_hash_fold(unsigned hash)
103 hash ^= hash>>16;
104 hash ^= hash>>8;
105 hash ^= hash>>4;
106 return hash;
109 #define unix_peer(sk) ((sk)->pair)
111 extern __inline__ intunix_our_peer(unix_socket *sk, unix_socket *osk)
113 returnunix_peer(osk) == sk;
116 extern __inline__ intunix_may_send(unix_socket *sk, unix_socket *osk)
118 return(sk->type==osk->type);
121 extern __inline__ voidunix_lock(unix_socket *sk)
123 sk->sock_readers++;
126 extern __inline__ intunix_unlock(unix_socket *sk)
128 return sk->sock_readers--;
131 extern __inline__ intunix_locked(unix_socket *sk)
133 return sk->sock_readers;
136 extern __inline__ voidunix_release_addr(struct unix_address *addr)
138 if(addr)
140 if(atomic_dec_and_test(&addr->refcnt))
141 kfree(addr);
145 static voidunix_destruct_addr(struct sock *sk)
147 struct unix_address *addr = sk->protinfo.af_unix.addr;
149 unix_release_addr(addr);
153 * Check unix socket name:
154 * - should be not zero length.
155 * - if started by not zero, should be NULL terminated (FS object)
156 * - if started by zero, it is abstract name.
159 static intunix_mkname(struct sockaddr_un * sunaddr,int len,unsigned*hashp)
161 if(len <=sizeof(short) || len >sizeof(*sunaddr))
162 return-EINVAL;
163 if(!sunaddr || sunaddr->sun_family != AF_UNIX)
164 return-EINVAL;
165 if(sunaddr->sun_path[0])
168 * This may look like an off by one error but it is
169 * a bit more subtle. 108 is the longest valid AF_UNIX
170 * path for a binding. sun_path[108] doesnt as such
171 * exist. However in kernel space we are guaranteed that
172 * it is a valid memory location in our kernel
173 * address buffer.
175 if(len >sizeof(*sunaddr))
176 len =sizeof(*sunaddr);
177 ((char*)sunaddr)[len]=0;
178 len =strlen(sunaddr->sun_path)+1+sizeof(short);
179 return len;
182 *hashp =unix_hash_fold(csum_partial((char*)sunaddr, len,0));
183 return len;
186 static voidunix_remove_socket(unix_socket *sk)
188 unix_socket **list = sk->protinfo.af_unix.list;
189 if(sk->next)
190 sk->next->prev = sk->prev;
191 if(sk->prev)
192 sk->prev->next = sk->next;
193 if(*list == sk)
194 *list = sk->next;
195 sk->protinfo.af_unix.list = NULL;
196 sk->prev = NULL;
197 sk->next = NULL;
200 static voidunix_insert_socket(unix_socket *sk)
202 unix_socket **list = sk->protinfo.af_unix.list;
203 sk->prev = NULL;
204 sk->next = *list;
205 if(*list)
206 (*list)->prev = sk;
207 *list=sk;
210 static unix_socket *unix_find_socket_byname(struct sockaddr_un *sunname,
211 int len,int type,unsigned hash)
213 unix_socket *s;
215 for(s=unix_socket_table[(hash^type)&0xF]; s; s=s->next)
217 if(s->protinfo.af_unix.addr->len==len &&
218 memcmp(s->protinfo.af_unix.addr->name, sunname, len) ==0&&
219 s->type == type)
221 unix_lock(s);
222 return(s);
225 return(NULL);
228 static unix_socket *unix_find_socket_byinode(struct inode *i)
230 unix_socket *s;
232 for(s=unix_socket_table[i->i_ino &0xF]; s; s=s->next)
234 struct dentry *dentry = s->protinfo.af_unix.dentry;
236 if(dentry && dentry->d_inode == i)
238 unix_lock(s);
239 return(s);
242 return(NULL);
246 * Delete a unix socket. We have to allow for deferring this on a timer.
249 static voidunix_destroy_timer(unsigned long data)
251 unix_socket *sk=(unix_socket *)data;
252 if(!unix_locked(sk) &&atomic_read(&sk->wmem_alloc) ==0)
254 sk_free(sk);
255 return;
259 * Retry;
262 sk->timer.expires=jiffies+sysctl_unix_destroy_delay;/* No real hurry try it every 10 seconds or so */
263 add_timer(&sk->timer);
267 static voidunix_delayed_delete(unix_socket *sk)
269 sk->timer.data=(unsigned long)sk;
270 sk->timer.expires=jiffies+sysctl_unix_delete_delay;/* Normally 1 second after will clean up. After that we try every 10 */
271 sk->timer.function=unix_destroy_timer;
272 add_timer(&sk->timer);
275 static voidunix_destroy_socket(unix_socket *sk)
277 struct sk_buff *skb;
279 unix_remove_socket(sk);
281 while((skb=skb_dequeue(&sk->receive_queue))!=NULL)
283 if(sk->state==TCP_LISTEN)
285 unix_socket *osk=skb->sk;
286 osk->state=TCP_CLOSE;
287 kfree_skb(skb, FREE_WRITE);/* Now surplus - free the skb first before the socket */
288 osk->state_change(osk);/* So the connect wakes and cleans up (if any) */
289 /* osk will be destroyed when it gets to close or the timer fires */
291 else
293 /* passed fds are erased in the kfree_skb hook */
294 kfree_skb(skb,FREE_WRITE);
298 if(sk->protinfo.af_unix.dentry!=NULL)
300 dput(sk->protinfo.af_unix.dentry);
301 sk->protinfo.af_unix.dentry=NULL;
304 if(!unix_unlock(sk) &&atomic_read(&sk->wmem_alloc) ==0)
306 sk_free(sk);
308 else
310 sk->dead=1;
311 unix_delayed_delete(sk);/* Try every so often until buffers are all freed */
315 static intunix_listen(struct socket *sock,int backlog)
317 struct sock *sk = sock->sk;
319 if(sock->state != SS_UNCONNECTED)
320 return(-EINVAL);
321 if(sock->type!=SOCK_STREAM)
322 return-EOPNOTSUPP;/* Only stream sockets accept */
323 if(!sk->protinfo.af_unix.addr)
324 return-EINVAL;/* No listens on an unbound socket */
325 sk->max_ack_backlog=backlog;
326 if(sk->ack_backlog < backlog)
327 sk->state_change(sk);
328 sk->state=TCP_LISTEN;
329 sock->flags |= SO_ACCEPTCON;
330 return0;
333 externstruct proto_ops unix_stream_ops;
334 externstruct proto_ops unix_dgram_ops;
336 static intunix_create(struct socket *sock,int protocol)
338 struct sock *sk;
340 sock->state = SS_UNCONNECTED;
342 if(protocol && protocol != PF_UNIX)
343 return-EPROTONOSUPPORT;
345 switch(sock->type)
347 case SOCK_STREAM:
348 sock->ops = &unix_stream_ops;
349 break;
351 * Believe it or not BSD has AF_UNIX, SOCK_RAW though
352 * nothing uses it.
354 case SOCK_RAW:
355 sock->type=SOCK_DGRAM;
356 case SOCK_DGRAM:
357 sock->ops = &unix_dgram_ops;
358 break;
359 default:
360 return-ESOCKTNOSUPPORT;
362 sk =sk_alloc(AF_UNIX, GFP_KERNEL);
363 if(!sk)
364 return-ENOMEM;
366 sock_init_data(sock,sk);
368 sk->destruct = unix_destruct_addr;
369 sk->protinfo.af_unix.family=AF_UNIX;
370 sk->protinfo.af_unix.dentry=NULL;
371 sk->sock_readers=1;/* Us */
372 sk->protinfo.af_unix.readsem=MUTEX;/* single task reading lock */
373 sk->mtu=4096;
374 sk->protinfo.af_unix.list=&unix_sockets_unbound;
375 unix_insert_socket(sk);
376 return0;
379 static intunix_release(struct socket *sock,struct socket *peer)
381 unix_socket *sk = sock->sk;
382 unix_socket *skpair;
384 if(!sk)
385 return0;
387 if(sock->state != SS_UNCONNECTED)
388 sock->state = SS_DISCONNECTING;
390 sk->state_change(sk);
391 sk->dead=1;
392 skpair=unix_peer(sk);
393 if(sock->type==SOCK_STREAM && skpair)
395 if(unix_our_peer(sk, skpair))
396 skpair->shutdown=SHUTDOWN_MASK;/* No more writes */
397 if(skpair->state!=TCP_LISTEN)
398 skpair->state_change(skpair);/* Wake any blocked writes */
400 if(skpair!=NULL)
401 unix_unlock(skpair);/* It may now die */
402 unix_peer(sk)=NULL;/* No pair */
403 unix_destroy_socket(sk);/* Try to flush out this socket. Throw out buffers at least */
404 unix_gc();/* Garbage collect fds */
407 * FIXME: BSD difference: In BSD all sockets connected to use get ECONNRESET and we die on the spot. In
408 * Linux we behave like files and pipes do and wait for the last dereference.
410 if(sk->socket)
412 sk->socket = NULL;
413 sock->sk = NULL;
416 return0;
419 static intunix_autobind(struct socket *sock)
421 struct sock *sk = sock->sk;
422 static u32 ordernum =1;
423 struct unix_address * addr;
424 unix_socket *osk;
426 addr =kmalloc(sizeof(*addr) +sizeof(short) +16, GFP_KERNEL);
427 if(!addr)
428 return-ENOBUFS;
429 if(sk->protinfo.af_unix.addr || sk->protinfo.af_unix.dentry)
431 kfree(addr);
432 return-EINVAL;
434 memset(addr,0,sizeof(*addr) +sizeof(short) +16);
435 addr->name->sun_family = AF_UNIX;
436 atomic_set(&addr->refcnt,1);
438 retry:
439 addr->len =sprintf(addr->name->sun_path+1,"%08x", ordernum) +1+sizeof(short);
440 addr->hash =unix_hash_fold(csum_partial((void*)addr->name, addr->len,0));
441 ordernum++;
443 if((osk=unix_find_socket_byname(addr->name, addr->len, sock->type,
444 addr->hash)) != NULL)
446 unix_unlock(osk);
447 goto retry;
450 sk->protinfo.af_unix.addr = addr;
451 unix_remove_socket(sk);
452 sk->protinfo.af_unix.list = &unix_socket_table[(addr->hash ^ sk->type)&0xF];
453 unix_insert_socket(sk);
454 return0;
457 static unix_socket *unix_find_other(struct sockaddr_un *sunname,int len,
458 int type,unsigned hash,int*error)
460 unix_socket *u;
462 if(sunname->sun_path[0])
464 struct dentry *dentry;
465 dentry =open_namei(sunname->sun_path,2, S_IFSOCK);
466 if(IS_ERR(dentry)) {
467 *error =PTR_ERR(dentry);
468 return NULL;
470 u=unix_find_socket_byinode(dentry->d_inode);
471 dput(dentry);
472 if(u && u->type != type)
474 *error=-EPROTOTYPE;
475 unix_unlock(u);
476 return NULL;
479 else
480 u=unix_find_socket_byname(sunname, len, type, hash);
482 if(u==NULL)
484 *error=-ECONNREFUSED;
485 return NULL;
487 return u;
491 static intunix_bind(struct socket *sock,struct sockaddr *uaddr,int addr_len)
493 struct sock *sk = sock->sk;
494 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
495 struct dentry * dentry;
496 int err;
497 unsigned hash;
498 struct unix_address *addr;
500 if(sk->protinfo.af_unix.addr || sk->protinfo.af_unix.dentry ||
501 sunaddr->sun_family != AF_UNIX)
502 return-EINVAL;
504 if(addr_len==sizeof(short))
505 returnunix_autobind(sock);
507 addr_len =unix_mkname(sunaddr, addr_len, &hash);
508 if(addr_len <0)
509 return addr_len;
511 addr =kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);
512 if(!addr)
513 return-ENOBUFS;
515 /* We slept; recheck ... */
517 if(sk->protinfo.af_unix.addr || sk->protinfo.af_unix.dentry)
519 kfree(addr);
520 return-EINVAL;/* Already bound */
523 memcpy(addr->name, sunaddr, addr_len);
524 addr->len = addr_len;
525 addr->hash = hash;
526 atomic_set(&addr->refcnt,1);
528 if(!sunaddr->sun_path[0])
530 unix_socket *osk =unix_find_socket_byname(sunaddr, addr_len,
531 sk->type, hash);
532 if(osk)
534 unix_unlock(osk);
535 kfree(addr);
536 return-EADDRINUSE;
538 unix_remove_socket(sk);
539 sk->protinfo.af_unix.addr = addr;
540 sk->protinfo.af_unix.list = &unix_socket_table[(hash^sk->type)&0xF];
541 unix_insert_socket(sk);
542 return0;
545 addr->hash = UNIX_HASH_SIZE;
546 sk->protinfo.af_unix.addr = addr;
549 dentry =do_mknod(sunaddr->sun_path, S_IFSOCK|S_IRWXUGO,0);
550 if(IS_ERR(dentry))
552 err =PTR_ERR(dentry);
553 unix_release_addr(addr);
554 sk->protinfo.af_unix.addr = NULL;
555 if(err==-EEXIST)
556 return-EADDRINUSE;
557 else
558 return err;
560 unix_remove_socket(sk);
561 sk->protinfo.af_unix.list = &unix_socket_table[dentry->d_inode->i_ino &0xF];
562 sk->protinfo.af_unix.dentry = dentry;
563 unix_insert_socket(sk);
565 return0;
568 static intunix_dgram_connect(struct socket *sock,struct sockaddr *addr,
569 int alen,int flags)
571 struct sock *sk = sock->sk;
572 struct sockaddr_un *sunaddr=(struct sockaddr_un*)addr;
573 struct sock *other;
574 unsigned hash;
575 int err;
578 * 1003.1g breaking connected state with AF_UNSPEC
581 if(addr->sa_family==AF_UNSPEC)
583 if(unix_peer(sk))
585 unix_unlock(unix_peer(sk));
586 unix_peer(sk) = NULL;
587 sock->state=SS_UNCONNECTED;
589 return0;
592 alen =unix_mkname(sunaddr, alen, &hash);
593 if(alen <0)
594 return alen;
596 other=unix_find_other(sunaddr, alen, sock->type, hash, &err);
597 if(!other)
598 return err;
599 if(!unix_may_send(sk, other))
601 unix_unlock(other);
602 return-EINVAL;
606 * If it was connected, reconnect.
608 if(unix_peer(sk))
610 unix_unlock(unix_peer(sk));
611 unix_peer(sk)=NULL;
613 unix_peer(sk)=other;
614 if(sock->passcred && !sk->protinfo.af_unix.addr)
615 unix_autobind(sock);
616 return0;
619 static intunix_stream_connect1(struct socket *sock,struct msghdr *msg,
620 int len,struct unix_skb_parms *cmsg,int nonblock)
622 struct sockaddr_un *sunaddr=(struct sockaddr_un *)msg->msg_name;
623 struct sock *sk = sock->sk;
624 unix_socket *other;
625 struct sk_buff *skb;
626 int err;
627 unsigned hash;
628 int addr_len;
630 addr_len =unix_mkname(sunaddr, msg->msg_namelen, &hash);
631 if(addr_len <0)
632 return addr_len;
634 switch(sock->state)
636 case SS_UNCONNECTED:
637 /* This is ok... continue with connect */
638 break;
639 case SS_CONNECTED:
640 /* Socket is already connected */
641 return-EISCONN;
642 case SS_CONNECTING:
643 /* Not yet connected... we will check this. */
644 break;
645 default:
646 return(-EINVAL);
650 if(unix_peer(sk))
652 if(sock->state==SS_CONNECTING && sk->state==TCP_ESTABLISHED)
654 sock->state=SS_CONNECTED;
655 if(!sk->protinfo.af_unix.addr)
656 unix_autobind(sock);
657 return0;
659 if(sock->state==SS_CONNECTING && sk->state == TCP_CLOSE)
661 sock->state=SS_UNCONNECTED;
662 return-ECONNREFUSED;
664 if(sock->state!=SS_CONNECTING)
665 return-EISCONN;
666 if(nonblock)
667 return-EALREADY;
669 * Drop through the connect up logic to the wait.
673 if(sock->state==SS_UNCONNECTED)
676 * Now ready to connect
679 skb=sock_alloc_send_skb(sk, len,0, nonblock, &err);/* Marker object */
680 if(skb==NULL)
681 return err;
682 memcpy(&UNIXCB(skb), cmsg,sizeof(*cmsg));
683 if(len)
684 memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
685 sk->state=TCP_CLOSE;
686 other=unix_find_other(sunaddr, addr_len, sk->type, hash, &err);
687 if(other==NULL)
689 kfree_skb(skb, FREE_WRITE);
690 return err;
692 other->ack_backlog++;
693 unix_peer(sk)=other;
694 skb_queue_tail(&other->receive_queue,skb);
695 sk->state=TCP_SYN_SENT;
696 sock->state=SS_CONNECTING;
697 other->data_ready(other,0);/* Wake up ! */
701 /* Wait for an accept */
703 while(sk->state==TCP_SYN_SENT)
705 if(nonblock)
706 return-EINPROGRESS;
707 interruptible_sleep_on(sk->sleep);
708 if(signal_pending(current))
709 return-ERESTARTSYS;
713 * Has the other end closed on us ?
716 if(sk->state==TCP_CLOSE)
718 unix_unlock(unix_peer(sk));
719 unix_peer(sk)=NULL;
720 sock->state=SS_UNCONNECTED;
721 return-ECONNREFUSED;
725 * Amazingly it has worked
728 sock->state=SS_CONNECTED;
729 if(!sk->protinfo.af_unix.addr)
730 unix_autobind(sock);
731 return0;
735 static intunix_stream_connect(struct socket *sock,struct sockaddr *uaddr,
736 int addr_len,int flags)
738 struct msghdr msg;
739 struct unix_skb_parms cmsg;
741 msg.msg_name = uaddr;
742 msg.msg_namelen = addr_len;
743 cmsg.fp = NULL;
744 cmsg.attr = MSG_SYN;
745 cmsg.creds.pid = current->pid;
746 cmsg.creds.uid = current->euid;
747 cmsg.creds.gid = current->egid;
749 returnunix_stream_connect1(sock, &msg,0, &cmsg, flags&O_NONBLOCK);
752 static intunix_socketpair(struct socket *socka,struct socket *sockb)
754 struct sock *ska=socka->sk, *skb = sockb->sk;
756 /* Join our sockets back to back */
757 unix_lock(ska);
758 unix_lock(skb);
759 unix_peer(ska)=skb;
760 unix_peer(skb)=ska;
762 if(ska->type != SOCK_DGRAM)
764 ska->state=TCP_ESTABLISHED;
765 skb->state=TCP_ESTABLISHED;
766 socka->state=SS_CONNECTED;
767 sockb->state=SS_CONNECTED;
769 return0;
772 static intunix_accept(struct socket *sock,struct socket *newsock,int flags)
774 unix_socket *sk = sock->sk;
775 unix_socket *newsk = newsock->sk;
776 unix_socket *tsk;
777 struct sk_buff *skb;
779 if(sock->state != SS_UNCONNECTED)
780 return(-EINVAL);
781 if(!(sock->flags & SO_ACCEPTCON))
782 return(-EINVAL);
784 if(sock->type!=SOCK_STREAM)
785 return-EOPNOTSUPP;
786 if(sk->state!=TCP_LISTEN)
787 return-EINVAL;
789 if(sk->protinfo.af_unix.addr)
791 atomic_inc(&sk->protinfo.af_unix.addr->refcnt);
792 newsk->protinfo.af_unix.addr=sk->protinfo.af_unix.addr;
794 if(sk->protinfo.af_unix.dentry)
795 newsk->protinfo.af_unix.dentry=dget(sk->protinfo.af_unix.dentry);
797 for(;;)
799 skb=skb_dequeue(&sk->receive_queue);
800 if(skb==NULL)
802 if(flags&O_NONBLOCK)
803 return-EAGAIN;
804 interruptible_sleep_on(sk->sleep);
805 if(signal_pending(current))
806 return-ERESTARTSYS;
807 continue;
809 if(!(UNIXCB(skb).attr & MSG_SYN))
811 tsk=skb->sk;
812 tsk->state_change(tsk);
813 kfree_skb(skb, FREE_WRITE);
814 continue;
816 break;
819 tsk=skb->sk;
820 sk->ack_backlog--;
821 unix_peer(newsk)=tsk;
822 unix_peer(tsk)=newsk;
823 tsk->state=TCP_ESTABLISHED;
824 newsk->state=TCP_ESTABLISHED;
825 memcpy(&newsk->peercred,UNIXCREDS(skb),sizeof(struct ucred));
826 tsk->peercred.pid = current->pid;
827 tsk->peercred.uid = current->euid;
828 tsk->peercred.gid = current->egid;
829 unix_lock(newsk);/* Swap lock over */
830 unix_unlock(sk);/* Locked to child socket not master */
831 unix_lock(tsk);/* Back lock */
832 kfree_skb(skb, FREE_WRITE);/* The buffer is just used as a tag */
833 tsk->state_change(tsk);/* Wake up any sleeping connect */
834 sock_wake_async(tsk->socket,0);
835 return0;
839 static intunix_getname(struct socket *sock,struct sockaddr *uaddr,int*uaddr_len,int peer)
841 struct sock *sk = sock->sk;
842 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
844 if(peer)
846 if(!unix_peer(sk))
847 return-ENOTCONN;
848 sk=unix_peer(sk);
850 if(!sk->protinfo.af_unix.addr)
852 sunaddr->sun_family = AF_UNIX;
853 sunaddr->sun_path[0] =0;
854 *uaddr_len =sizeof(short);
855 return0;/* Not bound */
857 *uaddr_len = sk->protinfo.af_unix.addr->len;
858 memcpy(sunaddr, sk->protinfo.af_unix.addr->name, *uaddr_len);
859 return0;
862 static voidunix_detach_fds(struct scm_cookie *scm,struct sk_buff *skb)
864 int i;
866 scm->fp =UNIXCB(skb).fp;
867 skb->destructor = sock_wfree;
868 UNIXCB(skb).fp = NULL;
870 for(i=scm->fp->count-1; i>=0; i--)
871 unix_notinflight(scm->fp->fp[i]);
874 static voidunix_destruct_fds(struct sk_buff *skb)
876 struct scm_cookie scm;
877 memset(&scm,0,sizeof(scm));
878 unix_detach_fds(&scm, skb);
879 scm_destroy(&scm);
880 sock_wfree(skb);
883 static voidunix_attach_fds(struct scm_cookie *scm,struct sk_buff *skb)
885 int i;
886 for(i=scm->fp->count-1; i>=0; i--)
887 unix_inflight(scm->fp->fp[i]);
888 UNIXCB(skb).fp = scm->fp;
889 skb->destructor = unix_destruct_fds;
890 scm->fp = NULL;
895 * Send AF_UNIX data.
898 static intunix_dgram_sendmsg(struct socket *sock,struct msghdr *msg,int len,
899 struct scm_cookie *scm)
901 struct sock *sk = sock->sk;
902 unix_socket *other;
903 struct sockaddr_un *sunaddr=msg->msg_name;
904 int namelen =0;/* fake GCC */
905 int err;
906 unsigned hash;
907 struct sk_buff *skb;
909 if(msg->msg_flags&MSG_OOB)
910 return-EOPNOTSUPP;
912 if(msg->msg_flags&~MSG_DONTWAIT)
913 return-EINVAL;
915 if(msg->msg_namelen) {
916 namelen =unix_mkname(sunaddr, msg->msg_namelen, &hash);
917 if(namelen <0)
918 return namelen;
919 }else{
920 sunaddr = NULL;
921 if(!unix_peer(sk))
922 return-ENOTCONN;
925 if(sock->passcred && !sk->protinfo.af_unix.addr)
926 unix_autobind(sock);
928 skb =sock_alloc_send_skb(sk, len,0, msg->msg_flags&MSG_DONTWAIT, &err);
930 if(skb==NULL)
931 return err;
933 memcpy(UNIXCREDS(skb), &scm->creds,sizeof(struct ucred));
934 UNIXCB(skb).attr = msg->msg_flags;
935 if(scm->fp)
936 unix_attach_fds(scm, skb);
938 skb->h.raw = skb->data;
939 memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
941 other =unix_peer(sk);
942 if(other && other->dead)
945 * Check with 1003.1g - what should
946 * datagram error
948 unix_unlock(other);
949 unix_peer(sk)=NULL;
950 other = NULL;
951 if(sunaddr == NULL) {
952 kfree_skb(skb, FREE_WRITE);
953 return-ECONNRESET;
956 if(!other)
958 other =unix_find_other(sunaddr, namelen, sk->type, hash, &err);
960 if(other==NULL)
962 kfree_skb(skb, FREE_WRITE);
963 return err;
965 if(!unix_may_send(sk, other))
967 unix_unlock(other);
968 kfree_skb(skb, FREE_WRITE);
969 return-EINVAL;
973 skb_queue_tail(&other->receive_queue, skb);
974 other->data_ready(other,len);
976 if(!unix_peer(sk))
977 unix_unlock(other);
978 return len;
982 static intunix_stream_sendmsg(struct socket *sock,struct msghdr *msg,int len,
983 struct scm_cookie *scm)
985 struct sock *sk = sock->sk;
986 unix_socket *other;
987 struct sockaddr_un *sunaddr=msg->msg_name;
988 int err,size;
989 struct sk_buff *skb;
990 int limit=0;
991 int sent=0;
993 if(sock->flags & SO_ACCEPTCON)
994 return(-EINVAL);
996 if(msg->msg_flags&MSG_OOB)
997 return-EOPNOTSUPP;
999 if(msg->msg_flags&~MSG_DONTWAIT)
1000 return-EINVAL;
1002 if(msg->msg_namelen) {
1003 if(sk->state==TCP_ESTABLISHED)
1004 return-EISCONN;
1005 else
1006 return-EOPNOTSUPP;
1007 }else{
1008 sunaddr = NULL;
1009 if(!unix_peer(sk))
1010 return-ENOTCONN;
1013 if(sk->shutdown&SEND_SHUTDOWN) {
1014 send_sig(SIGPIPE,current,0);
1015 return-EPIPE;
1018 while(sent < len)
1021 * Optimisation for the fact that under 0.01% of X messages typically
1022 * need breaking up.
1025 size=len-sent;
1027 if(size>(sk->sndbuf-sizeof(struct sk_buff))/2)/* Keep two messages in the pipe so it schedules better */
1028 size=(sk->sndbuf-sizeof(struct sk_buff))/2;
1031 * Keep to page sized kmalloc()'s as various people
1032 * have suggested. Big mallocs stress the vm too
1033 * much.
1036 if(size >3500)
1037 limit =3500;/* Fall back to a page if we can't grab a big buffer this instant */
1038 else
1039 limit =0;/* Otherwise just grab and wait */
1042 * Grab a buffer
1045 skb=sock_alloc_send_skb(sk,size,limit,msg->msg_flags&MSG_DONTWAIT, &err);
1047 if(skb==NULL)
1049 if(sent)
1050 return sent;
1051 return err;
1055 * If you pass two values to the sock_alloc_send_skb
1056 * it tries to grab the large buffer with GFP_BUFFER
1057 * (which can fail easily), and if it fails grab the
1058 * fallback size buffer which is under a page and will
1059 * succeed. [Alan]
1061 size =min(size,skb_tailroom(skb));
1063 memcpy(UNIXCREDS(skb), &scm->creds,sizeof(struct ucred));
1064 UNIXCB(skb).attr = msg->msg_flags;
1065 if(scm->fp)
1066 unix_attach_fds(scm, skb);
1068 memcpy_fromiovec(skb_put(skb,size), msg->msg_iov, size);
1070 other=unix_peer(sk);
1072 if(other->dead || (sk->shutdown & SEND_SHUTDOWN))
1074 kfree_skb(skb, FREE_WRITE);
1075 if(sent)
1076 return sent;
1077 send_sig(SIGPIPE,current,0);
1078 return-EPIPE;
1081 skb_queue_tail(&other->receive_queue, skb);
1082 other->data_ready(other,size);
1083 sent+=size;
1085 return sent;
1089 * Sleep until data has arrive. But check for races..
1092 static voidunix_data_wait(unix_socket * sk)
1094 if(!skb_peek(&sk->receive_queue))
1096 sk->socket->flags |= SO_WAITDATA;
1097 interruptible_sleep_on(sk->sleep);
1098 sk->socket->flags &= ~SO_WAITDATA;
1102 static intunix_dgram_recvmsg(struct socket *sock,struct msghdr *msg,int size,
1103 int flags,struct scm_cookie *scm)
1105 struct sock *sk = sock->sk;
1106 int noblock = flags & MSG_DONTWAIT;
1107 struct sk_buff *skb;
1108 int err;
1110 if(flags&MSG_OOB)
1111 return-EOPNOTSUPP;
1113 msg->msg_namelen =0;
1115 skb=skb_recv_datagram(sk, flags, noblock, &err);
1116 if(skb==NULL)
1117 return err;
1119 if(msg->msg_name)
1121 if(skb->sk->protinfo.af_unix.addr)
1123 memcpy(msg->msg_name, skb->sk->protinfo.af_unix.addr->name,
1124 skb->sk->protinfo.af_unix.addr->len);
1125 msg->msg_namelen=skb->sk->protinfo.af_unix.addr->len;
1127 else
1128 msg->msg_namelen=sizeof(short);
1131 if(size > skb->len)
1132 size = skb->len;
1133 else if(size < skb->len)
1134 msg->msg_flags |= MSG_TRUNC;
1136 if(skb_copy_datagram_iovec(skb,0, msg->msg_iov, size))
1137 return-EFAULT;
1139 scm->creds = *UNIXCREDS(skb);
1141 if(!(flags & MSG_PEEK))
1143 if(UNIXCB(skb).fp)
1144 unix_detach_fds(scm, skb);
1146 else
1148 /* It is questionable: on PEEK we could:
1149 - do not return fds - good, but too simple 8)
1150 - return fds, and do not return them on read (old strategy,
1151 apparently wrong)
1152 - clone fds (I choosed it for now, it is the most universal
1153 solution)
1155 POSIX 1003.1g does not actually define this clearly
1156 at all. POSIX 1003.1g doesn't define a lot of things
1157 clearly however!
1160 if(UNIXCB(skb).fp)
1161 scm->fp =scm_fp_dup(UNIXCB(skb).fp);
1163 skb_free_datagram(sk,skb);
1164 return size;
1168 static intunix_stream_recvmsg(struct socket *sock,struct msghdr *msg,int size,
1169 int flags,struct scm_cookie *scm)
1171 struct sock *sk = sock->sk;
1172 int noblock = flags & MSG_DONTWAIT;
1173 struct sockaddr_un *sunaddr=msg->msg_name;
1174 int copied =0;
1175 int check_creds =0;
1176 int target =1;
1178 if(sock->flags & SO_ACCEPTCON)
1179 return(-EINVAL);
1181 if(flags&MSG_OOB)
1182 return-EOPNOTSUPP;
1183 if(flags&MSG_WAITALL)
1184 target = size;
1187 msg->msg_namelen =0;
1189 /* Lock the socket to prevent queue disordering
1190 * while sleeps in memcpy_tomsg
1193 down(&sk->protinfo.af_unix.readsem);
1197 int chunk;
1198 struct sk_buff *skb;
1200 skb=skb_dequeue(&sk->receive_queue);
1201 if(skb==NULL)
1203 if(copied >= target)
1204 break;
1207 * POSIX 1003.1g mandates this order.
1210 if(sk->err)
1212 up(&sk->protinfo.af_unix.readsem);
1213 returnsock_error(sk);
1216 if(sk->shutdown & RCV_SHUTDOWN)
1217 break;
1218 up(&sk->protinfo.af_unix.readsem);
1219 if(noblock)
1220 return-EAGAIN;
1221 unix_data_wait(sk);
1222 if(signal_pending(current))
1223 return-ERESTARTSYS;
1224 down(&sk->protinfo.af_unix.readsem);
1225 continue;
1228 /* Never glue messages from different writers */
1229 if(check_creds &&
1230 memcmp(UNIXCREDS(skb), &scm->creds,sizeof(scm->creds)) !=0)
1232 skb_queue_head(&sk->receive_queue, skb);
1233 break;
1236 /* Copy address just once */
1237 if(sunaddr)
1239 if(skb->sk->protinfo.af_unix.addr)
1241 memcpy(sunaddr, skb->sk->protinfo.af_unix.addr->name,
1242 skb->sk->protinfo.af_unix.addr->len);
1243 msg->msg_namelen=skb->sk->protinfo.af_unix.addr->len;
1245 else
1246 msg->msg_namelen=sizeof(short);
1247 sunaddr = NULL;
1250 chunk =min(skb->len, size);
1251 memcpy_toiovec(msg->msg_iov, skb->data, chunk);
1252 copied += chunk;
1253 size -= chunk;
1255 /* Copy credentials */
1256 scm->creds = *UNIXCREDS(skb);
1257 check_creds =1;
1259 /* Mark read part of skb as used */
1260 if(!(flags & MSG_PEEK))
1262 skb_pull(skb, chunk);
1264 if(UNIXCB(skb).fp)
1265 unix_detach_fds(scm, skb);
1267 /* put the skb back if we didn't use it up.. */
1268 if(skb->len)
1270 skb_queue_head(&sk->receive_queue, skb);
1271 break;
1274 kfree_skb(skb, FREE_WRITE);
1276 if(scm->fp)
1277 break;
1279 else
1281 /* It is questionable, see note in unix_dgram_recvmsg.
1284 if(UNIXCB(skb).fp)
1285 scm->fp =scm_fp_dup(UNIXCB(skb).fp);
1287 /* put message back and return */
1288 skb_queue_head(&sk->receive_queue, skb);
1289 break;
1291 }while(size);
1293 up(&sk->protinfo.af_unix.readsem);
1294 return copied;
1297 static intunix_shutdown(struct socket *sock,int mode)
1299 struct sock *sk = sock->sk;
1300 unix_socket *other=unix_peer(sk);
1302 mode++;
1304 if(mode&SEND_SHUTDOWN)
1306 sk->shutdown|=SEND_SHUTDOWN;
1307 sk->state_change(sk);
1308 if(other && sk->type == SOCK_STREAM && other->state != TCP_LISTEN)
1310 if(unix_our_peer(sk, other))
1311 other->shutdown|=RCV_SHUTDOWN;
1312 other->state_change(other);
1315 other=unix_peer(sk);
1316 if(mode&RCV_SHUTDOWN)
1318 sk->shutdown|=RCV_SHUTDOWN;
1319 sk->state_change(sk);
1320 if(other && sk->type != SOCK_DGRAM && other->state != TCP_LISTEN)
1322 if(unix_our_peer(sk, other))
1323 other->shutdown|=SEND_SHUTDOWN;
1324 other->state_change(other);
1327 return0;
1331 static intunix_ioctl(struct socket *sock,unsigned int cmd,unsigned long arg)
1333 struct sock *sk = sock->sk;
1334 long amount=0;
1336 switch(cmd)
1339 case TIOCOUTQ:
1340 amount = sk->sndbuf -atomic_read(&sk->wmem_alloc);
1341 if(amount<0)
1342 amount=0;
1343 returnput_user(amount, (int*)arg);
1344 case TIOCINQ:
1346 struct sk_buff *skb;
1347 if(sk->state==TCP_LISTEN)
1348 return-EINVAL;
1350 * These two are safe on current systems as
1351 * only user tasks fiddle here
1353 if((skb=skb_peek(&sk->receive_queue))!=NULL)
1354 amount=skb->len;
1355 returnput_user(amount, (int*)arg);
1358 default:
1359 return-EINVAL;
1361 /*NOTREACHED*/
1362 return(0);
1365 #ifdef CONFIG_PROC_FS
1366 static intunix_read_proc(char*buffer,char**start, off_t offset,
1367 int length,int*eof,void*data)
1369 off_t pos=0;
1370 off_t begin=0;
1371 int len=0;
1372 int i;
1373 unix_socket *s;
1375 len+=sprintf(buffer,"Num RefCount Protocol Flags Type St "
1376 "Inode Path\n");
1378 forall_unix_sockets(i,s)
1380 len+=sprintf(buffer+len,"%p: %08X %08X %08lX %04X %02X %5ld",
1382 s->sock_readers,
1384 s->socket ? s->socket->flags :0,
1385 s->type,
1386 s->socket ? s->socket->state :0,
1387 s->socket ? s->socket->inode->i_ino :0);
1389 if(s->protinfo.af_unix.addr)
1391 buffer[len++] =' ';
1392 memcpy(buffer+len, s->protinfo.af_unix.addr->name->sun_path,
1393 s->protinfo.af_unix.addr->len-sizeof(short));
1394 if(!UNIX_ABSTRACT(s))
1395 len--;
1396 else
1397 buffer[len] ='@';
1398 len += s->protinfo.af_unix.addr->len -sizeof(short);
1400 buffer[len++]='\n';
1402 pos=begin+len;
1403 if(pos<offset)
1405 len=0;
1406 begin=pos;
1408 if(pos>offset+length)
1409 goto done;
1411 *eof =1;
1412 done:
1413 *start=buffer+(offset-begin);
1414 len-=(offset-begin);
1415 if(len>length)
1416 len=length;
1417 return len;
1419 #endif
1421 struct proto_ops unix_stream_ops = {
1422 AF_UNIX,
1424 sock_no_dup,
1425 unix_release,
1426 unix_bind,
1427 unix_stream_connect,
1428 unix_socketpair,
1429 unix_accept,
1430 unix_getname,
1431 datagram_poll,
1432 unix_ioctl,
1433 unix_listen,
1434 unix_shutdown,
1435 sock_no_setsockopt,
1436 sock_no_getsockopt,
1437 sock_no_fcntl,
1438 unix_stream_sendmsg,
1439 unix_stream_recvmsg
1442 struct proto_ops unix_dgram_ops = {
1443 AF_UNIX,
1445 sock_no_dup,
1446 unix_release,
1447 unix_bind,
1448 unix_dgram_connect,
1449 unix_socketpair,
1450 sock_no_accept,
1451 unix_getname,
1452 datagram_poll,
1453 unix_ioctl,
1454 sock_no_listen,
1455 unix_shutdown,
1456 sock_no_setsockopt,
1457 sock_no_getsockopt,
1458 sock_no_fcntl,
1459 unix_dgram_sendmsg,
1460 unix_dgram_recvmsg
1463 struct net_proto_family unix_family_ops = {
1464 AF_UNIX,
1465 unix_create
1468 __initfunc(voidunix_proto_init(struct net_proto *pro))
1470 struct sk_buff *dummy_skb;
1471 struct proc_dir_entry *ent;
1473 printk(KERN_INFO "NET3: Unix domain sockets 0.16 for Linux NET3.038.\n");
1474 if(sizeof(struct unix_skb_parms) >sizeof(dummy_skb->cb))
1476 printk(KERN_CRIT "unix_proto_init: panic\n");
1477 return;
1479 sock_register(&unix_family_ops);
1480 #ifdef CONFIG_PROC_FS
1481 ent =create_proc_entry("net/unix",0,0);
1482 ent->read_proc = unix_read_proc;
1483 #endif
1486 * Local variables:
1487 * compile-command: "gcc -g -D__KERNEL__ -Wall -O6 -I/usr/src/linux/include -c af_unix.c"
1488 * End:
close