Import 2.1.43pre1
[davej-history.git] / net / unix / af_unix.c
blob1dfdf1832528ae0f60ddc6c2087c3a7c9ed65321
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])
165 if(len >=sizeof(*sunaddr))
166 len =sizeof(*sunaddr)-1;
167 ((char*)sunaddr)[len]=0;
168 len =strlen(sunaddr->sun_path)+1+sizeof(short);
169 return len;
172 *hashp =unix_hash_fold(csum_partial((char*)sunaddr, len,0));
173 return len;
176 static voidunix_remove_socket(unix_socket *sk)
178 unix_socket **list = sk->protinfo.af_unix.list;
179 if(sk->next)
180 sk->next->prev = sk->prev;
181 if(sk->prev)
182 sk->prev->next = sk->next;
183 if(*list == sk)
184 *list = sk->next;
185 sk->protinfo.af_unix.list = NULL;
186 sk->prev = NULL;
187 sk->next = NULL;
190 static voidunix_insert_socket(unix_socket *sk)
192 unix_socket **list = sk->protinfo.af_unix.list;
193 sk->prev = NULL;
194 sk->next = *list;
195 if(*list)
196 (*list)->prev = sk;
197 *list=sk;
200 static unix_socket *unix_find_socket_byname(struct sockaddr_un *sunname,
201 int len,int type,unsigned hash)
203 unix_socket *s;
205 for(s=unix_socket_table[(hash^type)&0xF]; s; s=s->next)
207 if(s->protinfo.af_unix.addr->len==len &&
208 memcmp(s->protinfo.af_unix.addr->name, sunname, len) ==0&&
209 s->type == type)
211 unix_lock(s);
212 return(s);
215 return(NULL);
218 static unix_socket *unix_find_socket_byinode(struct inode *i)
220 unix_socket *s;
222 for(s=unix_socket_table[i->i_ino &0xF]; s; s=s->next)
224 if(s->protinfo.af_unix.inode==i)
226 unix_lock(s);
227 return(s);
230 return(NULL);
234 * Delete a unix socket. We have to allow for deferring this on a timer.
237 static voidunix_destroy_timer(unsigned long data)
239 unix_socket *sk=(unix_socket *)data;
240 if(!unix_locked(sk) &&atomic_read(&sk->wmem_alloc) ==0)
242 sk_free(sk);
243 return;
247 * Retry;
250 sk->timer.expires=jiffies+sysctl_unix_destroy_delay;/* No real hurry try it every 10 seconds or so */
251 add_timer(&sk->timer);
255 static voidunix_delayed_delete(unix_socket *sk)
257 sk->timer.data=(unsigned long)sk;
258 sk->timer.expires=jiffies+sysctl_unix_delete_delay;/* Normally 1 second after will clean up. After that we try every 10 */
259 sk->timer.function=unix_destroy_timer;
260 add_timer(&sk->timer);
263 static voidunix_destroy_socket(unix_socket *sk)
265 struct sk_buff *skb;
267 unix_remove_socket(sk);
269 while((skb=skb_dequeue(&sk->receive_queue))!=NULL)
271 if(sk->state==TCP_LISTEN)
273 unix_socket *osk=skb->sk;
274 osk->state=TCP_CLOSE;
275 kfree_skb(skb, FREE_WRITE);/* Now surplus - free the skb first before the socket */
276 osk->state_change(osk);/* So the connect wakes and cleans up (if any) */
277 /* osk will be destroyed when it gets to close or the timer fires */
279 else
281 /* passed fds are erased in the kfree_skb hook */
282 kfree_skb(skb,FREE_WRITE);
286 if(sk->protinfo.af_unix.inode!=NULL)
288 iput(sk->protinfo.af_unix.inode);
289 sk->protinfo.af_unix.inode=NULL;
292 if(!unix_unlock(sk) &&atomic_read(&sk->wmem_alloc) ==0)
294 sk_free(sk);
296 else
298 sk->dead=1;
299 unix_delayed_delete(sk);/* Try every so often until buffers are all freed */
303 static intunix_listen(struct socket *sock,int backlog)
305 struct sock *sk = sock->sk;
307 if(sock->state != SS_UNCONNECTED)
308 return(-EINVAL);
309 if(sock->type!=SOCK_STREAM)
310 return-EOPNOTSUPP;/* Only stream sockets accept */
311 if(!sk->protinfo.af_unix.addr)
312 return-EINVAL;/* No listens on an unbound socket */
313 sk->max_ack_backlog=backlog;
314 if(sk->ack_backlog < backlog)
315 sk->state_change(sk);
316 sk->state=TCP_LISTEN;
317 sock->flags |= SO_ACCEPTCON;
318 return0;
321 externstruct proto_ops unix_stream_ops;
322 externstruct proto_ops unix_dgram_ops;
324 static intunix_create(struct socket *sock,int protocol)
326 struct sock *sk;
328 sock->state = SS_UNCONNECTED;
330 if(protocol && protocol != PF_UNIX)
331 return-EPROTONOSUPPORT;
333 switch(sock->type)
335 case SOCK_STREAM:
336 sock->ops = &unix_stream_ops;
337 break;
339 * Believe it or not BSD has AF_UNIX, SOCK_RAW though
340 * nothing uses it.
342 case SOCK_RAW:
343 sock->type=SOCK_DGRAM;
344 case SOCK_DGRAM:
345 sock->ops = &unix_dgram_ops;
346 break;
347 default:
348 return-ESOCKTNOSUPPORT;
350 sk =sk_alloc(GFP_KERNEL);
351 if(!sk)
352 return-ENOMEM;
354 sock_init_data(sock,sk);
356 sk->destruct = unix_destruct_addr;
357 sk->protinfo.af_unix.family=AF_UNIX;
358 sk->protinfo.af_unix.inode=NULL;
359 sk->sock_readers=1;/* Us */
360 sk->protinfo.af_unix.readsem=MUTEX;/* single task reading lock */
361 sk->mtu=4096;
362 sk->protinfo.af_unix.list=&unix_sockets_unbound;
363 unix_insert_socket(sk);
364 return0;
367 static intunix_dup(struct socket *newsock,struct socket *oldsock)
369 returnunix_create(newsock,0);
372 static intunix_release(struct socket *sock,struct socket *peer)
374 unix_socket *sk = sock->sk;
375 unix_socket *skpair;
377 if(!sk)
378 return0;
380 if(sock->state != SS_UNCONNECTED)
381 sock->state = SS_DISCONNECTING;
383 sk->state_change(sk);
384 sk->dead=1;
385 skpair=unix_peer(sk);
386 if(sock->type==SOCK_STREAM && skpair)
388 if(unix_our_peer(sk, skpair))
389 skpair->shutdown=SHUTDOWN_MASK;/* No more writes */
390 if(skpair->state!=TCP_LISTEN)
391 skpair->state_change(skpair);/* Wake any blocked writes */
393 if(skpair!=NULL)
394 unix_unlock(skpair);/* It may now die */
395 unix_peer(sk)=NULL;/* No pair */
396 unix_destroy_socket(sk);/* Try to flush out this socket. Throw out buffers at least */
397 unix_gc();/* Garbage collect fds */
400 * FIXME: BSD difference: In BSD all sockets connected to use get ECONNRESET and we die on the spot. In
401 * Linux we behave like files and pipes do and wait for the last dereference.
403 if(sk->socket)
405 sk->socket = NULL;
406 sock->sk = NULL;
409 return0;
412 static intunix_autobind(struct socket *sock)
414 struct sock *sk = sock->sk;
415 static u32 ordernum =1;
416 struct unix_address * addr;
417 unix_socket *osk;
419 addr =kmalloc(sizeof(*addr) +sizeof(short) +16, GFP_KERNEL);
420 if(!addr)
421 return-ENOBUFS;
422 if(sk->protinfo.af_unix.addr || sk->protinfo.af_unix.inode)
424 kfree(addr);
425 return-EINVAL;
427 memset(addr,0,sizeof(*addr) +sizeof(short) +16);
428 addr->name->sun_family = AF_UNIX;
429 atomic_set(&addr->refcnt,1);
431 retry:
432 addr->len =sprintf(addr->name->sun_path+1,"%08x", ordernum) +1+sizeof(short);
433 addr->hash =unix_hash_fold(csum_partial((void*)addr->name, addr->len,0));
434 ordernum++;
436 if((osk=unix_find_socket_byname(addr->name, addr->len, sock->type,
437 addr->hash)) != NULL)
439 unix_unlock(osk);
440 goto retry;
443 sk->protinfo.af_unix.addr = addr;
444 unix_remove_socket(sk);
445 sk->protinfo.af_unix.list = &unix_socket_table[(addr->hash ^ sk->type)&0xF];
446 unix_insert_socket(sk);
447 return0;
450 static unix_socket *unix_find_other(struct sockaddr_un *sunname,int len,
451 int type,unsigned hash,int*error)
453 int old_fs;
454 int err;
455 struct inode *inode;
456 unix_socket *u;
458 if(sunname->sun_path[0])
460 old_fs=get_fs();
461 set_fs(get_ds());
462 err =open_namei(sunname->sun_path,2, S_IFSOCK, &inode, NULL);
463 set_fs(old_fs);
464 if(err<0)
466 *error=err;
467 return NULL;
469 u=unix_find_socket_byinode(inode);
470 iput(inode);
471 if(u && u->type != type)
473 *error=-EPROTOTYPE;
474 unix_unlock(u);
475 return NULL;
478 else
479 u=unix_find_socket_byname(sunname, len, type, hash);
481 if(u==NULL)
483 *error=-ECONNREFUSED;
484 return NULL;
486 return u;
490 static intunix_bind(struct socket *sock,struct sockaddr *uaddr,int addr_len)
492 struct sock *sk = sock->sk;
493 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
494 struct inode * inode;
495 int old_fs;
496 int err;
497 unsigned hash;
498 struct unix_address *addr;
500 if(sk->protinfo.af_unix.addr || sk->protinfo.af_unix.inode ||
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.inode)
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;
548 old_fs=get_fs();
549 set_fs(get_ds());
551 err=do_mknod(sunaddr->sun_path, S_IFSOCK|S_IRWXUGO,0);
552 if(!err)
553 err=open_namei(sunaddr->sun_path,2, S_IFSOCK, &inode, NULL);
555 set_fs(old_fs);
557 if(err<0)
559 unix_release_addr(addr);
560 sk->protinfo.af_unix.addr = NULL;
561 if(err==-EEXIST)
562 return-EADDRINUSE;
563 else
564 return err;
566 unix_remove_socket(sk);
567 sk->protinfo.af_unix.list = &unix_socket_table[inode->i_ino &0xF];
568 sk->protinfo.af_unix.inode = inode;
569 unix_insert_socket(sk);
571 return0;
574 static intunix_dgram_connect(struct socket *sock,struct sockaddr *addr,
575 int alen,int flags)
577 struct sock *sk = sock->sk;
578 struct sockaddr_un *sunaddr=(struct sockaddr_un*)addr;
579 struct sock *other;
580 unsigned hash;
581 int err;
584 * 1003.1g breaking connected state with AF_UNSPEC
587 if(addr->sa_family==AF_UNSPEC)
589 if(unix_peer(sk))
591 unix_unlock(unix_peer(sk));
592 unix_peer(sk) = NULL;
593 sock->state=SS_UNCONNECTED;
595 return0;
598 alen =unix_mkname(sunaddr, alen, &hash);
599 if(alen <0)
600 return alen;
602 other=unix_find_other(sunaddr, alen, sock->type, hash, &err);
603 if(!other)
604 return err;
605 if(!unix_may_send(sk, other))
607 unix_unlock(other);
608 return-EINVAL;
612 * If it was connected, reconnect.
614 if(unix_peer(sk))
616 unix_unlock(unix_peer(sk));
617 unix_peer(sk)=NULL;
619 unix_peer(sk)=other;
620 if(sock->passcred && !sk->protinfo.af_unix.addr)
621 unix_autobind(sock);
622 return0;
625 static intunix_stream_connect1(struct socket *sock,struct msghdr *msg,
626 int len,struct unix_skb_parms *cmsg,int nonblock)
628 struct sockaddr_un *sunaddr=(struct sockaddr_un *)msg->msg_name;
629 struct sock *sk = sock->sk;
630 unix_socket *other;
631 struct sk_buff *skb;
632 int err;
633 unsigned hash;
634 int addr_len;
636 addr_len =unix_mkname(sunaddr, msg->msg_namelen, &hash);
637 if(addr_len <0)
638 return addr_len;
640 switch(sock->state)
642 case SS_UNCONNECTED:
643 /* This is ok... continue with connect */
644 break;
645 case SS_CONNECTED:
646 /* Socket is already connected */
647 return-EISCONN;
648 case SS_CONNECTING:
649 /* Not yet connected... we will check this. */
650 break;
651 default:
652 return(-EINVAL);
656 if(unix_peer(sk))
658 if(sock->state==SS_CONNECTING && sk->state==TCP_ESTABLISHED)
660 sock->state=SS_CONNECTED;
661 if(!sk->protinfo.af_unix.addr)
662 unix_autobind(sock);
663 return0;
665 if(sock->state==SS_CONNECTING && sk->state == TCP_CLOSE)
667 sock->state=SS_UNCONNECTED;
668 return-ECONNREFUSED;
670 if(sock->state!=SS_CONNECTING)
671 return-EISCONN;
672 if(nonblock)
673 return-EALREADY;
675 * Drop through the connect up logic to the wait.
679 if(sock->state==SS_UNCONNECTED)
682 * Now ready to connect
685 skb=sock_alloc_send_skb(sk, len,0, nonblock, &err);/* Marker object */
686 if(skb==NULL)
687 return err;
688 memcpy(&UNIXCB(skb), cmsg,sizeof(*cmsg));
689 if(len)
690 memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
691 sk->state=TCP_CLOSE;
692 other=unix_find_other(sunaddr, addr_len, sk->type, hash, &err);
693 if(other==NULL)
695 kfree_skb(skb, FREE_WRITE);
696 return err;
698 other->ack_backlog++;
699 unix_peer(sk)=other;
700 skb_queue_tail(&other->receive_queue,skb);
701 sk->state=TCP_SYN_SENT;
702 sock->state=SS_CONNECTING;
703 other->data_ready(other,0);/* Wake up ! */
707 /* Wait for an accept */
709 while(sk->state==TCP_SYN_SENT)
711 if(nonblock)
712 return-EINPROGRESS;
713 interruptible_sleep_on(sk->sleep);
714 if(current->signal & ~current->blocked)
715 return-ERESTARTSYS;
719 * Has the other end closed on us ?
722 if(sk->state==TCP_CLOSE)
724 unix_unlock(unix_peer(sk));
725 unix_peer(sk)=NULL;
726 sock->state=SS_UNCONNECTED;
727 return-ECONNREFUSED;
731 * Amazingly it has worked
734 sock->state=SS_CONNECTED;
735 if(!sk->protinfo.af_unix.addr)
736 unix_autobind(sock);
737 return0;
741 static intunix_stream_connect(struct socket *sock,struct sockaddr *uaddr,
742 int addr_len,int flags)
744 struct msghdr msg;
745 struct unix_skb_parms cmsg;
747 msg.msg_name = uaddr;
748 msg.msg_namelen = addr_len;
749 cmsg.fp = NULL;
750 cmsg.attr = MSG_SYN;
751 cmsg.creds.pid = current->pid;
752 cmsg.creds.uid = current->euid;
753 cmsg.creds.gid = current->egid;
755 returnunix_stream_connect1(sock, &msg,0, &cmsg, flags&O_NONBLOCK);
758 static intunix_socketpair(struct socket *socka,struct socket *sockb)
760 struct sock *ska=socka->sk, *skb = sockb->sk;
762 /* Join our sockets back to back */
763 unix_lock(ska);
764 unix_lock(skb);
765 unix_peer(ska)=skb;
766 unix_peer(skb)=ska;
768 if(ska->type != SOCK_DGRAM)
770 ska->state=TCP_ESTABLISHED;
771 skb->state=TCP_ESTABLISHED;
772 socka->state=SS_CONNECTED;
773 sockb->state=SS_CONNECTED;
775 return0;
778 static intunix_accept(struct socket *sock,struct socket *newsock,int flags)
780 unix_socket *sk = sock->sk;
781 unix_socket *newsk = newsock->sk;
782 unix_socket *tsk;
783 struct sk_buff *skb;
785 if(sock->state != SS_UNCONNECTED)
786 return(-EINVAL);
787 if(!(sock->flags & SO_ACCEPTCON))
788 return(-EINVAL);
790 if(sock->type!=SOCK_STREAM)
791 return-EOPNOTSUPP;
792 if(sk->state!=TCP_LISTEN)
793 return-EINVAL;
795 if(sk->protinfo.af_unix.addr)
797 atomic_inc(&sk->protinfo.af_unix.addr->refcnt);
798 newsk->protinfo.af_unix.addr=sk->protinfo.af_unix.addr;
800 if(sk->protinfo.af_unix.inode)
802 atomic_inc(&sk->protinfo.af_unix.inode->i_count);
803 newsk->protinfo.af_unix.inode=sk->protinfo.af_unix.inode;
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(current->signal & ~current->blocked)
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, FREE_WRITE);
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, FREE_WRITE);/* 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, FREE_WRITE);
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, FREE_WRITE);
972 return err;
974 if(!unix_may_send(sk, other))
976 unix_unlock(other);
977 kfree_skb(skb, FREE_WRITE);
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 if(size>(sk->sndbuf-sizeof(struct sk_buff))/2)/* Keep two messages in the pipe so it schedules better */
1037 size=(sk->sndbuf-sizeof(struct sk_buff))/2;
1040 * Keep to page sized kmalloc()'s as various people
1041 * have suggested. Big mallocs stress the vm too
1042 * much.
1045 if(size >3500)
1046 limit =3500;/* Fall back to a page if we can't grab a big buffer this instant */
1047 else
1048 limit =0;/* Otherwise just grab and wait */
1051 * Grab a buffer
1054 skb=sock_alloc_send_skb(sk,size,limit,msg->msg_flags&MSG_DONTWAIT, &err);
1056 if(skb==NULL)
1058 if(sent)
1059 return sent;
1060 return err;
1064 * If you pass two values to the sock_alloc_send_skb
1065 * it tries to grab the large buffer with GFP_BUFFER
1066 * (which can fail easily), and if it fails grab the
1067 * fallback size buffer which is under a page and will
1068 * succeed. [Alan]
1070 size =min(size,skb_tailroom(skb));
1072 memcpy(UNIXCREDS(skb), &scm->creds,sizeof(struct ucred));
1073 UNIXCB(skb).attr = msg->msg_flags;
1074 if(scm->fp)
1075 unix_attach_fds(scm, skb);
1077 memcpy_fromiovec(skb_put(skb,size), msg->msg_iov, size);
1079 other=unix_peer(sk);
1081 if(other->dead || (sk->shutdown & SEND_SHUTDOWN))
1083 kfree_skb(skb, FREE_WRITE);
1084 if(sent)
1085 return sent;
1086 send_sig(SIGPIPE,current,0);
1087 return-EPIPE;
1090 skb_queue_tail(&other->receive_queue, skb);
1091 other->data_ready(other,size);
1092 sent+=size;
1094 return sent;
1098 * Sleep until data has arrive. But check for races..
1101 static voidunix_data_wait(unix_socket * sk)
1103 if(!skb_peek(&sk->receive_queue))
1105 sk->socket->flags |= SO_WAITDATA;
1106 interruptible_sleep_on(sk->sleep);
1107 sk->socket->flags &= ~SO_WAITDATA;
1111 static intunix_dgram_recvmsg(struct socket *sock,struct msghdr *msg,int size,
1112 int flags,struct scm_cookie *scm)
1114 struct sock *sk = sock->sk;
1115 int noblock = flags & MSG_DONTWAIT;
1116 struct sk_buff *skb;
1117 int err;
1119 if(flags&MSG_OOB)
1120 return-EOPNOTSUPP;
1122 msg->msg_namelen =0;
1124 skb=skb_recv_datagram(sk, flags, noblock, &err);
1125 if(skb==NULL)
1126 return err;
1128 if(msg->msg_name)
1130 if(skb->sk->protinfo.af_unix.addr)
1132 memcpy(msg->msg_name, skb->sk->protinfo.af_unix.addr->name,
1133 skb->sk->protinfo.af_unix.addr->len);
1134 msg->msg_namelen=skb->sk->protinfo.af_unix.addr->len;
1136 else
1137 msg->msg_namelen=sizeof(short);
1140 if(size > skb->len)
1141 size = skb->len;
1142 else if(size < skb->len)
1143 msg->msg_flags |= MSG_TRUNC;
1145 if(skb_copy_datagram_iovec(skb,0, msg->msg_iov, size))
1146 return-EFAULT;
1148 scm->creds = *UNIXCREDS(skb);
1150 if(!(flags & MSG_PEEK))
1152 if(UNIXCB(skb).fp)
1153 unix_detach_fds(scm, skb);
1155 else
1157 /* It is questionable: on PEEK we could:
1158 - do not return fds - good, but too simple 8)
1159 - return fds, and do not return them on read (old strategy,
1160 apparently wrong)
1161 - clone fds (I choosed it for now, it is the most universal
1162 solution)
1164 POSIX 1003.1g does not actually define this clearly
1165 at all. POSIX 1003.1g doesn't define a lot of things
1166 clearly however!
1169 if(UNIXCB(skb).fp)
1170 scm->fp =scm_fp_dup(UNIXCB(skb).fp);
1172 skb_free_datagram(sk,skb);
1173 return size;
1177 static intunix_stream_recvmsg(struct socket *sock,struct msghdr *msg,int size,
1178 int flags,struct scm_cookie *scm)
1180 struct sock *sk = sock->sk;
1181 int noblock = flags & MSG_DONTWAIT;
1182 struct sockaddr_un *sunaddr=msg->msg_name;
1183 int copied =0;
1184 int check_creds =0;
1185 int target =1;
1187 if(sock->flags & SO_ACCEPTCON)
1188 return(-EINVAL);
1190 if(flags&MSG_OOB)
1191 return-EOPNOTSUPP;
1192 if(flags&MSG_WAITALL)
1193 target = size;
1196 msg->msg_namelen =0;
1198 /* Lock the socket to prevent queue disordering
1199 * while sleeps in memcpy_tomsg
1202 down(&sk->protinfo.af_unix.readsem);
1206 int chunk;
1207 struct sk_buff *skb;
1209 skb=skb_dequeue(&sk->receive_queue);
1210 if(skb==NULL)
1212 if(copied >= target)
1213 break;
1215 if(sk->err)
1216 returnsock_error(sk);
1218 if(sk->shutdown & RCV_SHUTDOWN)
1219 break;
1220 up(&sk->protinfo.af_unix.readsem);
1221 if(noblock)
1222 return-EAGAIN;
1223 unix_data_wait(sk);
1224 if(current->signal & ~current->blocked)
1225 return-ERESTARTSYS;
1226 down(&sk->protinfo.af_unix.readsem);
1227 continue;
1230 /* Never glue messages from different writers */
1231 if(check_creds &&
1232 memcmp(UNIXCREDS(skb), &scm->creds,sizeof(scm->creds)) !=0)
1234 skb_queue_head(&sk->receive_queue, skb);
1235 break;
1238 /* Copy address just once */
1239 if(sunaddr)
1241 if(skb->sk->protinfo.af_unix.addr)
1243 memcpy(sunaddr, skb->sk->protinfo.af_unix.addr->name,
1244 skb->sk->protinfo.af_unix.addr->len);
1245 msg->msg_namelen=skb->sk->protinfo.af_unix.addr->len;
1247 else
1248 msg->msg_namelen=sizeof(short);
1249 sunaddr = NULL;
1252 chunk =min(skb->len, size);
1253 memcpy_toiovec(msg->msg_iov, skb->data, chunk);
1254 copied += chunk;
1255 size -= chunk;
1257 /* Copy credentials */
1258 scm->creds = *UNIXCREDS(skb);
1259 check_creds =1;
1261 /* Mark read part of skb as used */
1262 if(!(flags & MSG_PEEK))
1264 skb_pull(skb, chunk);
1266 if(UNIXCB(skb).fp)
1267 unix_detach_fds(scm, skb);
1269 /* put the skb back if we didn't use it up.. */
1270 if(skb->len)
1272 skb_queue_head(&sk->receive_queue, skb);
1273 break;
1276 kfree_skb(skb, FREE_WRITE);
1278 if(scm->fp)
1279 break;
1281 else
1283 /* It is questionable, see note in unix_dgram_recvmsg.
1286 if(UNIXCB(skb).fp)
1287 scm->fp =scm_fp_dup(UNIXCB(skb).fp);
1289 /* put message back and return */
1290 skb_queue_head(&sk->receive_queue, skb);
1291 break;
1293 }while(size);
1295 up(&sk->protinfo.af_unix.readsem);
1296 return copied;
1299 static intunix_shutdown(struct socket *sock,int mode)
1301 struct sock *sk = sock->sk;
1302 unix_socket *other=unix_peer(sk);
1304 mode++;
1306 if(mode&SEND_SHUTDOWN)
1308 sk->shutdown|=SEND_SHUTDOWN;
1309 sk->state_change(sk);
1310 if(other && sk->type == SOCK_STREAM && other->state != TCP_LISTEN)
1312 if(unix_our_peer(sk, other))
1313 other->shutdown|=RCV_SHUTDOWN;
1314 other->state_change(other);
1317 other=unix_peer(sk);
1318 if(mode&RCV_SHUTDOWN)
1320 sk->shutdown|=RCV_SHUTDOWN;
1321 sk->state_change(sk);
1322 if(other && sk->type != SOCK_DGRAM && other->state != TCP_LISTEN)
1324 if(unix_our_peer(sk, other))
1325 other->shutdown|=SEND_SHUTDOWN;
1326 other->state_change(other);
1329 return0;
1333 static intunix_ioctl(struct socket *sock,unsigned int cmd,unsigned long arg)
1335 struct sock *sk = sock->sk;
1336 long amount=0;
1338 switch(cmd)
1341 case TIOCOUTQ:
1342 amount = sk->sndbuf -atomic_read(&sk->wmem_alloc);
1343 if(amount<0)
1344 amount=0;
1345 returnput_user(amount, (int*)arg);
1346 case TIOCINQ:
1348 struct sk_buff *skb;
1349 if(sk->state==TCP_LISTEN)
1350 return-EINVAL;
1352 * These two are safe on current systems as
1353 * only user tasks fiddle here
1355 if((skb=skb_peek(&sk->receive_queue))!=NULL)
1356 amount=skb->len;
1357 returnput_user(amount, (int*)arg);
1360 default:
1361 return-EINVAL;
1363 /*NOTREACHED*/
1364 return(0);
1367 #ifdef CONFIG_PROC_FS
1368 static intunix_read_proc(char*buffer,char**start, off_t offset,
1369 int length,int*eof,void*data)
1371 off_t pos=0;
1372 off_t begin=0;
1373 int len=0;
1374 int i;
1375 unix_socket *s;
1377 len+=sprintf(buffer,"Num RefCount Protocol Flags Type St "
1378 "Inode Path\n");
1380 forall_unix_sockets(i,s)
1382 len+=sprintf(buffer+len,"%p: %08X %08X %08lX %04X %02X %5ld",
1384 s->sock_readers,
1386 s->socket ? s->socket->flags :0,
1387 s->type,
1388 s->socket ? s->socket->state :0,
1389 s->socket ? s->socket->inode->i_ino :0);
1391 if(s->protinfo.af_unix.addr)
1393 buffer[len++] =' ';
1394 memcpy(buffer+len, s->protinfo.af_unix.addr->name->sun_path,
1395 s->protinfo.af_unix.addr->len-sizeof(short));
1396 if(!UNIX_ABSTRACT(s))
1397 len--;
1398 else
1399 buffer[len] ='@';
1400 len += s->protinfo.af_unix.addr->len -sizeof(short);
1402 buffer[len++]='\n';
1404 pos=begin+len;
1405 if(pos<offset)
1407 len=0;
1408 begin=pos;
1410 if(pos>offset+length)
1411 goto done;
1413 *eof =1;
1414 done:
1415 *start=buffer+(offset-begin);
1416 len-=(offset-begin);
1417 if(len>length)
1418 len=length;
1419 return len;
1421 #endif
1423 struct proto_ops unix_stream_ops = {
1424 AF_UNIX,
1426 unix_dup,
1427 unix_release,
1428 unix_bind,
1429 unix_stream_connect,
1430 unix_socketpair,
1431 unix_accept,
1432 unix_getname,
1433 datagram_poll,
1434 unix_ioctl,
1435 unix_listen,
1436 unix_shutdown,
1437 sock_no_setsockopt,
1438 sock_no_getsockopt,
1439 sock_no_fcntl,
1440 unix_stream_sendmsg,
1441 unix_stream_recvmsg
1444 struct proto_ops unix_dgram_ops = {
1445 AF_UNIX,
1447 unix_dup,
1448 unix_release,
1449 unix_bind,
1450 unix_dgram_connect,
1451 unix_socketpair,
1452 NULL,
1453 unix_getname,
1454 datagram_poll,
1455 unix_ioctl,
1456 sock_no_listen,
1457 unix_shutdown,
1458 sock_no_setsockopt,
1459 sock_no_getsockopt,
1460 sock_no_fcntl,
1461 unix_dgram_sendmsg,
1462 unix_dgram_recvmsg
1465 struct net_proto_family unix_family_ops = {
1466 AF_UNIX,
1467 unix_create
1470 __initfunc(voidunix_proto_init(struct net_proto *pro))
1472 struct sk_buff *dummy_skb;
1473 struct proc_dir_entry *ent;
1475 printk(KERN_INFO "NET3: Unix domain sockets 0.16 for Linux NET3.038.\n");
1476 if(sizeof(struct unix_skb_parms) >sizeof(dummy_skb->cb))
1478 printk(KERN_CRIT "unix_proto_init: panic\n");
1479 return;
1481 sock_register(&unix_family_ops);
1482 #ifdef CONFIG_PROC_FS
1483 ent =create_proc_entry("net/unix",0,0);
1484 ent->read_proc = unix_read_proc;
1485 #endif
1488 * Local variables:
1489 * compile-command: "gcc -g -D__KERNEL__ -Wall -O6 -I/usr/src/linux/include -c af_unix.c"
1490 * End:
close