Import 2.1.30
[davej-history.git] / net / unix / af_unix.c
blob6b6a081ca0bddac4fc1ab583ba20713dce7746dc
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>
85 #include <asm/checksum.h>
87 #define min(a,b) (((a)<(b))?(a):(b))
90 unix_socket *unix_socket_table[UNIX_HASH_SIZE+1];
92 #define unix_sockets_unbound (unix_socket_table[UNIX_HASH_SIZE])
94 #define UNIX_ABSTRACT(sk) ((sk)->protinfo.af_unix.addr->hash!=UNIX_HASH_SIZE)
96 extern __inline__ unsignedunix_hash_fold(unsigned hash)
98 hash ^= hash>>16;
99 hash ^= hash>>8;
100 hash ^= hash>>4;
101 return hash;
104 #define unix_peer(sk) ((sk)->pair)
106 extern __inline__ intunix_our_peer(unix_socket *sk, unix_socket *osk)
108 returnunix_peer(osk) == sk;
111 extern __inline__ intunix_may_send(unix_socket *sk, unix_socket *osk)
113 return(sk->type==osk->type);
116 extern __inline__ voidunix_lock(unix_socket *sk)
118 sk->sock_readers++;
121 extern __inline__ intunix_unlock(unix_socket *sk)
123 return sk->sock_readers--;
126 extern __inline__ intunix_locked(unix_socket *sk)
128 return sk->sock_readers;
131 extern __inline__ voidunix_release_addr(struct unix_address *addr)
133 if(addr)
135 if(atomic_dec_and_test(&addr->refcnt))
136 kfree(addr);
142 * Check unix socket name:
143 * - should be not zero length.
144 * - if started by not zero, should be NULL terminated (FS object)
145 * - if started by zero, it is abstract name.
148 static intunix_mkname(struct sockaddr_un * sunaddr,int len,unsigned*hashp)
150 if(len <=sizeof(short) || len >sizeof(*sunaddr))
151 return-EINVAL;
152 if(!sunaddr || sunaddr->sun_family != AF_UNIX)
153 return-EINVAL;
154 if(sunaddr->sun_path[0])
156 if(len >=sizeof(*sunaddr))
157 len =sizeof(*sunaddr)-1;
158 ((char*)sunaddr)[len]=0;
159 len =strlen(sunaddr->sun_path)+1+sizeof(short);
160 return len;
163 *hashp =unix_hash_fold(csum_partial((char*)sunaddr, len,0));
164 return len;
167 static voidunix_remove_socket(unix_socket *sk)
169 unix_socket **list = sk->protinfo.af_unix.list;
170 if(sk->next)
171 sk->next->prev = sk->prev;
172 if(sk->prev)
173 sk->prev->next = sk->next;
174 if(*list == sk)
175 *list = sk->next;
176 sk->protinfo.af_unix.list = NULL;
177 sk->prev = NULL;
178 sk->next = NULL;
181 static voidunix_insert_socket(unix_socket *sk)
183 unix_socket **list = sk->protinfo.af_unix.list;
184 sk->prev = NULL;
185 sk->next = *list;
186 if(*list)
187 (*list)->prev = sk;
188 *list=sk;
191 static unix_socket *unix_find_socket_byname(struct sockaddr_un *sunname,
192 int len,int type,unsigned hash)
194 unix_socket *s;
196 for(s=unix_socket_table[(hash^type)&0xF]; s; s=s->next)
198 if(s->protinfo.af_unix.addr->len==len &&
199 memcmp(s->protinfo.af_unix.addr->name, sunname, len) ==0&&
200 s->type == type)
202 unix_lock(s);
203 return(s);
206 return(NULL);
209 static unix_socket *unix_find_socket_byinode(struct inode *i)
211 unix_socket *s;
213 for(s=unix_socket_table[i->i_ino &0xF]; s; s=s->next)
215 if(s->protinfo.af_unix.inode==i)
217 unix_lock(s);
218 return(s);
221 return(NULL);
225 * Delete a unix socket. We have to allow for deferring this on a timer.
228 static voidunix_destroy_timer(unsigned long data)
230 unix_socket *sk=(unix_socket *)data;
231 if(!unix_locked(sk) && sk->wmem_alloc==0)
233 unix_release_addr(sk->protinfo.af_unix.addr);
234 sk_free(sk);
235 return;
239 * Retry;
242 sk->timer.expires=jiffies+10*HZ;/* No real hurry try it every 10 seconds or so */
243 add_timer(&sk->timer);
247 static voidunix_delayed_delete(unix_socket *sk)
249 sk->timer.data=(unsigned long)sk;
250 sk->timer.expires=jiffies+HZ;/* Normally 1 second after will clean up. After that we try every 10 */
251 sk->timer.function=unix_destroy_timer;
252 add_timer(&sk->timer);
255 static voidunix_destroy_socket(unix_socket *sk)
257 struct sk_buff *skb;
259 unix_remove_socket(sk);
261 while((skb=skb_dequeue(&sk->receive_queue))!=NULL)
263 if(sk->state==TCP_LISTEN)
265 unix_socket *osk=skb->sk;
266 osk->state=TCP_CLOSE;
267 kfree_skb(skb, FREE_WRITE);/* Now surplus - free the skb first before the socket */
268 osk->state_change(osk);/* So the connect wakes and cleans up (if any) */
269 /* osk will be destroyed when it gets to close or the timer fires */
271 else
273 /* passed fds are erased in the kfree_skb hook */
274 kfree_skb(skb,FREE_WRITE);
278 if(sk->protinfo.af_unix.inode!=NULL)
280 iput(sk->protinfo.af_unix.inode);
281 sk->protinfo.af_unix.inode=NULL;
284 if(!unix_unlock(sk) && sk->wmem_alloc==0)
286 unix_release_addr(sk->protinfo.af_unix.addr);
287 sk_free(sk);
289 else
291 sk->dead=1;
292 unix_delayed_delete(sk);/* Try every so often until buffers are all freed */
296 static intunix_listen(struct socket *sock,int backlog)
298 struct sock *sk = sock->sk;
300 if(sock->state != SS_UNCONNECTED)
301 return(-EINVAL);
302 if(sock->type!=SOCK_STREAM)
303 return-EOPNOTSUPP;/* Only stream sockets accept */
304 if(!sk->protinfo.af_unix.addr)
305 return-EINVAL;/* No listens on an unbound socket */
306 sk->max_ack_backlog=backlog;
307 if(sk->ack_backlog < backlog)
308 sk->state_change(sk);
309 sk->state=TCP_LISTEN;
310 sock->flags |= SO_ACCEPTCON;
311 return0;
314 externstruct proto_ops unix_stream_ops;
315 externstruct proto_ops unix_dgram_ops;
317 static intunix_create(struct socket *sock,int protocol)
319 struct sock *sk;
321 sock->state = SS_UNCONNECTED;
323 if(protocol && protocol != PF_UNIX)
324 return-EPROTONOSUPPORT;
326 switch(sock->type)
328 case SOCK_STREAM:
329 sock->ops = &unix_stream_ops;
330 break;
332 * Believe it or not BSD has AF_UNIX, SOCK_RAW though
333 * nothing uses it.
335 case SOCK_RAW:
336 sock->type=SOCK_DGRAM;
337 case SOCK_DGRAM:
338 sock->ops = &unix_dgram_ops;
339 break;
340 default:
341 return-ESOCKTNOSUPPORT;
343 sk =sk_alloc(GFP_KERNEL);
344 if(!sk)
345 return-ENOMEM;
347 sock_init_data(sock,sk);
349 sk->protinfo.af_unix.family=AF_UNIX;
350 sk->protinfo.af_unix.inode=NULL;
351 sk->sock_readers=1;/* Us */
352 sk->protinfo.af_unix.readsem=MUTEX;/* single task reading lock */
353 sk->mtu=4096;
354 sk->protinfo.af_unix.list=&unix_sockets_unbound;
355 unix_insert_socket(sk);
356 return0;
359 static intunix_dup(struct socket *newsock,struct socket *oldsock)
361 returnunix_create(newsock,0);
364 static intunix_release(struct socket *sock,struct socket *peer)
366 unix_socket *sk = sock->sk;
367 unix_socket *skpair;
369 if(!sk)
370 return0;
372 if(sock->state != SS_UNCONNECTED)
373 sock->state = SS_DISCONNECTING;
375 sk->state_change(sk);
376 sk->dead=1;
377 skpair=unix_peer(sk);
378 if(sock->type==SOCK_STREAM && skpair)
380 if(unix_our_peer(sk, skpair))
381 skpair->shutdown=SHUTDOWN_MASK;/* No more writes */
382 if(skpair->state!=TCP_LISTEN)
383 skpair->state_change(skpair);/* Wake any blocked writes */
385 if(skpair!=NULL)
386 unix_unlock(skpair);/* It may now die */
387 unix_peer(sk)=NULL;/* No pair */
388 unix_destroy_socket(sk);/* Try to flush out this socket. Throw out buffers at least */
389 unix_gc();/* Garbage collect fds */
392 * FIXME: BSD difference: In BSD all sockets connected to use get ECONNRESET and we die on the spot. In
393 * Linux we behave like files and pipes do and wait for the last dereference.
395 if(sk->socket)
397 sk->socket = NULL;
398 sock->sk = NULL;
401 return0;
404 static intunix_autobind(struct socket *sock)
406 struct sock *sk = sock->sk;
407 static u32 ordernum =1;
408 struct unix_address * addr;
409 unix_socket *osk;
411 addr =kmalloc(sizeof(*addr) +sizeof(short) +16, GFP_KERNEL);
412 if(!addr)
413 return-ENOBUFS;
414 if(sk->protinfo.af_unix.addr || sk->protinfo.af_unix.inode)
416 kfree(addr);
417 return-EINVAL;
419 memset(addr,0,sizeof(*addr) +sizeof(short) +16);
420 addr->name->sun_family = AF_UNIX;
421 addr->refcnt =1;
423 retry:
424 addr->len =sprintf(addr->name->sun_path+1,"%08x", ordernum) +1+sizeof(short);
425 addr->hash =unix_hash_fold(csum_partial((void*)addr->name, addr->len,0));
426 ordernum++;
428 if((osk=unix_find_socket_byname(addr->name, addr->len, sock->type,
429 addr->hash)) != NULL)
431 unix_unlock(osk);
432 goto retry;
435 sk->protinfo.af_unix.addr = addr;
436 unix_remove_socket(sk);
437 sk->protinfo.af_unix.list = &unix_socket_table[(addr->hash ^ sk->type)&0xF];
438 unix_insert_socket(sk);
439 return0;
442 static unix_socket *unix_find_other(struct sockaddr_un *sunname,int len,
443 int type,unsigned hash,int*error)
445 int old_fs;
446 int err;
447 struct inode *inode;
448 unix_socket *u;
450 if(sunname->sun_path[0])
452 old_fs=get_fs();
453 set_fs(get_ds());
454 err =open_namei(sunname->sun_path,2, S_IFSOCK, &inode, NULL);
455 set_fs(old_fs);
456 if(err<0)
458 *error=err;
459 return NULL;
461 u=unix_find_socket_byinode(inode);
462 iput(inode);
463 if(u && u->type != type)
465 *error=-EPROTOTYPE;
466 unix_unlock(u);
467 return NULL;
470 else
471 u=unix_find_socket_byname(sunname, len, type, hash);
473 if(u==NULL)
475 *error=-ECONNREFUSED;
476 return NULL;
478 return u;
482 static intunix_bind(struct socket *sock,struct sockaddr *uaddr,int addr_len)
484 struct sock *sk = sock->sk;
485 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
486 struct inode * inode;
487 int old_fs;
488 int err;
489 unsigned hash;
490 struct unix_address *addr;
492 if(sk->protinfo.af_unix.addr || sk->protinfo.af_unix.inode ||
493 sunaddr->sun_family != AF_UNIX)
494 return-EINVAL;
496 if(addr_len==sizeof(short))
497 returnunix_autobind(sock);
499 addr_len =unix_mkname(sunaddr, addr_len, &hash);
500 if(addr_len <0)
501 return addr_len;
503 addr =kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);
504 if(!addr)
505 return-ENOBUFS;
507 /* We sleeped; recheck ... */
509 if(sk->protinfo.af_unix.addr || sk->protinfo.af_unix.inode)
511 kfree(addr);
512 return-EINVAL;/* Already bound */
515 memcpy(addr->name, sunaddr, addr_len);
516 addr->len = addr_len;
517 addr->hash = hash;
518 addr->refcnt =1;
520 if(!sunaddr->sun_path[0])
522 unix_socket *osk =unix_find_socket_byname(sunaddr, addr_len,
523 sk->type, hash);
524 if(osk)
526 unix_unlock(osk);
527 kfree(addr);
528 return-EADDRINUSE;
530 unix_remove_socket(sk);
531 sk->protinfo.af_unix.addr = addr;
532 sk->protinfo.af_unix.list = &unix_socket_table[(hash^sk->type)&0xF];
533 unix_insert_socket(sk);
534 return0;
537 addr->hash = UNIX_HASH_SIZE;
538 sk->protinfo.af_unix.addr = addr;
540 old_fs=get_fs();
541 set_fs(get_ds());
543 err=do_mknod(sunaddr->sun_path, S_IFSOCK|S_IRWXUGO,0);
544 if(!err)
545 err=open_namei(sunaddr->sun_path,2, S_IFSOCK, &inode, NULL);
547 set_fs(old_fs);
549 if(err<0)
551 unix_release_addr(addr);
552 sk->protinfo.af_unix.addr = NULL;
553 if(err==-EEXIST)
554 return-EADDRINUSE;
555 else
556 return err;
558 unix_remove_socket(sk);
559 sk->protinfo.af_unix.list = &unix_socket_table[inode->i_ino &0xF];
560 sk->protinfo.af_unix.inode = inode;
561 unix_insert_socket(sk);
563 return0;
566 static intunix_dgram_connect(struct socket *sock,struct sockaddr *addr,
567 int alen,int flags)
569 struct sock *sk = sock->sk;
570 struct sockaddr_un *sunaddr=(struct sockaddr_un*)addr;
571 struct sock *other;
572 unsigned hash;
573 int err;
576 * 1003.1g breaking connected state with AF_UNSPEC
579 if(addr->sa_family==AF_UNSPEC)
581 if(unix_peer(sk))
583 unix_unlock(unix_peer(sk));
584 unix_peer(sk) = NULL;
585 sock->state=SS_UNCONNECTED;
587 return0;
590 alen =unix_mkname(sunaddr, alen, &hash);
591 if(alen <0)
592 return alen;
594 other=unix_find_other(sunaddr, alen, sock->type, hash, &err);
595 if(!other)
596 return err;
597 if(!unix_may_send(sk, other))
599 unix_unlock(other);
600 return-EINVAL;
604 * If it was connected, reconnect.
606 if(unix_peer(sk))
608 unix_unlock(unix_peer(sk));
609 unix_peer(sk)=NULL;
611 unix_peer(sk)=other;
612 if(sock->passcred && !sk->protinfo.af_unix.addr)
613 unix_autobind(sock);
614 return0;
617 static intunix_stream_connect1(struct socket *sock,struct msghdr *msg,
618 int len,struct unix_skb_parms *cmsg,int nonblock)
620 struct sockaddr_un *sunaddr=(struct sockaddr_un *)msg->msg_name;
621 struct sock *sk = sock->sk;
622 unix_socket *other;
623 struct sk_buff *skb;
624 int err;
625 unsigned hash;
626 int addr_len;
628 addr_len =unix_mkname(sunaddr, msg->msg_namelen, &hash);
629 if(addr_len <0)
630 return addr_len;
632 switch(sock->state)
634 case SS_UNCONNECTED:
635 /* This is ok... continue with connect */
636 break;
637 case SS_CONNECTED:
638 /* Socket is already connected */
639 return-EISCONN;
640 case SS_CONNECTING:
641 /* Not yet connected... we will check this. */
642 break;
643 default:
644 return(-EINVAL);
648 if(unix_peer(sk))
650 if(sock->state==SS_CONNECTING && sk->state==TCP_ESTABLISHED)
652 sock->state=SS_CONNECTED;
653 if(!sk->protinfo.af_unix.addr)
654 unix_autobind(sock);
655 return0;
657 if(sock->state==SS_CONNECTING && sk->state == TCP_CLOSE)
659 sock->state=SS_UNCONNECTED;
660 return-ECONNREFUSED;
662 if(sock->state!=SS_CONNECTING)
663 return-EISCONN;
664 if(nonblock)
665 return-EALREADY;
667 * Drop through the connect up logic to the wait.
671 if(sock->state==SS_UNCONNECTED)
674 * Now ready to connect
677 skb=sock_alloc_send_skb(sk, len,0, nonblock, &err);/* Marker object */
678 if(skb==NULL)
679 return err;
680 memcpy(&UNIXCB(skb), cmsg,sizeof(*cmsg));
681 if(len)
682 memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
683 sk->state=TCP_CLOSE;
684 other=unix_find_other(sunaddr, addr_len, sk->type, hash, &err);
685 if(other==NULL)
687 kfree_skb(skb, FREE_WRITE);
688 return err;
690 other->ack_backlog++;
691 unix_peer(sk)=other;
692 skb_queue_tail(&other->receive_queue,skb);
693 sk->state=TCP_SYN_SENT;
694 sock->state=SS_CONNECTING;
695 other->data_ready(other,0);/* Wake up ! */
699 /* Wait for an accept */
701 while(sk->state==TCP_SYN_SENT)
703 if(nonblock)
704 return-EINPROGRESS;
705 interruptible_sleep_on(sk->sleep);
706 if(current->signal & ~current->blocked)
707 return-ERESTARTSYS;
711 * Has the other end closed on us ?
714 if(sk->state==TCP_CLOSE)
716 unix_unlock(unix_peer(sk));
717 unix_peer(sk)=NULL;
718 sock->state=SS_UNCONNECTED;
719 return-ECONNREFUSED;
723 * Amazingly it has worked
726 sock->state=SS_CONNECTED;
727 if(!sk->protinfo.af_unix.addr)
728 unix_autobind(sock);
729 return0;
733 static intunix_stream_connect(struct socket *sock,struct sockaddr *uaddr,
734 int addr_len,int flags)
736 struct msghdr msg;
737 struct unix_skb_parms cmsg;
739 msg.msg_name = uaddr;
740 msg.msg_namelen = addr_len;
741 cmsg.fp = NULL;
742 cmsg.attr = MSG_SYN;
743 cmsg.creds.pid = current->pid;
744 cmsg.creds.uid = current->euid;
745 cmsg.creds.gid = current->egid;
747 returnunix_stream_connect1(sock, &msg,0, &cmsg, flags&O_NONBLOCK);
750 static intunix_socketpair(struct socket *socka,struct socket *sockb)
752 struct sock *ska=socka->sk, *skb = sockb->sk;
754 /* Join our sockets back to back */
755 unix_lock(ska);
756 unix_lock(skb);
757 unix_peer(ska)=skb;
758 unix_peer(skb)=ska;
760 if(ska->type != SOCK_DGRAM)
762 ska->state=TCP_ESTABLISHED;
763 skb->state=TCP_ESTABLISHED;
764 socka->state=SS_CONNECTED;
765 sockb->state=SS_CONNECTED;
767 return0;
770 static intunix_accept(struct socket *sock,struct socket *newsock,int flags)
772 unix_socket *sk = sock->sk;
773 unix_socket *newsk = newsock->sk;
774 unix_socket *tsk;
775 struct sk_buff *skb;
777 if(sock->state != SS_UNCONNECTED)
778 return(-EINVAL);
779 if(!(sock->flags & SO_ACCEPTCON))
780 return(-EINVAL);
782 if(sock->type!=SOCK_STREAM)
783 return-EOPNOTSUPP;
784 if(sk->state!=TCP_LISTEN)
785 return-EINVAL;
787 if(sk->protinfo.af_unix.addr)
789 atomic_inc(&sk->protinfo.af_unix.addr->refcnt);
790 newsk->protinfo.af_unix.addr=sk->protinfo.af_unix.addr;
792 if(sk->protinfo.af_unix.inode)
794 sk->protinfo.af_unix.inode->i_count++;
795 newsk->protinfo.af_unix.inode=sk->protinfo.af_unix.inode;
798 for(;;)
800 skb=skb_dequeue(&sk->receive_queue);
801 if(skb==NULL)
803 if(flags&O_NONBLOCK)
804 return-EAGAIN;
805 interruptible_sleep_on(sk->sleep);
806 if(current->signal & ~current->blocked)
807 return-ERESTARTSYS;
808 continue;
810 if(!(UNIXCB(skb).attr & MSG_SYN))
812 tsk=skb->sk;
813 tsk->state_change(tsk);
814 kfree_skb(skb, FREE_WRITE);
815 continue;
817 break;
820 tsk=skb->sk;
821 sk->ack_backlog--;
822 unix_peer(newsk)=tsk;
823 unix_peer(tsk)=newsk;
824 tsk->state=TCP_ESTABLISHED;
825 newsk->state=TCP_ESTABLISHED;
826 memcpy(&newsk->peercred,UNIXCREDS(skb),sizeof(struct ucred));
827 tsk->peercred.pid = current->pid;
828 tsk->peercred.uid = current->euid;
829 tsk->peercred.gid = current->egid;
830 unix_lock(newsk);/* Swap lock over */
831 unix_unlock(sk);/* Locked to child socket not master */
832 unix_lock(tsk);/* Back lock */
833 kfree_skb(skb, FREE_WRITE);/* The buffer is just used as a tag */
834 tsk->state_change(tsk);/* Wake up any sleeping connect */
835 sock_wake_async(tsk->socket,0);
836 return0;
840 static intunix_getname(struct socket *sock,struct sockaddr *uaddr,int*uaddr_len,int peer)
842 struct sock *sk = sock->sk;
843 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
845 if(peer)
847 if(!unix_peer(sk))
848 return-ENOTCONN;
849 sk=unix_peer(sk);
851 if(!sk->protinfo.af_unix.addr)
853 sunaddr->sun_family = AF_UNIX;
854 sunaddr->sun_path[0] =0;
855 *uaddr_len =sizeof(short);
856 return0;/* Not bound */
858 *uaddr_len = sk->protinfo.af_unix.addr->len;
859 memcpy(sunaddr, sk->protinfo.af_unix.addr->name, *uaddr_len);
860 return0;
863 static voidunix_detach_fds(struct scm_cookie *scm,struct sk_buff *skb)
865 int i;
867 scm->fp =UNIXCB(skb).fp;
868 skb->destructor = sock_wfree;
869 UNIXCB(skb).fp = NULL;
871 for(i=scm->fp->count-1; i>=0; i--)
872 unix_notinflight(scm->fp->fp[i]);
875 static voidunix_destruct_fds(struct sk_buff *skb)
877 struct scm_cookie scm;
878 memset(&scm,0,sizeof(scm));
879 unix_detach_fds(&scm, skb);
880 scm_destroy(&scm);
881 sock_wfree(skb);
884 static voidunix_attach_fds(struct scm_cookie *scm,struct sk_buff *skb)
886 int i;
887 for(i=scm->fp->count-1; i>=0; i--)
888 unix_inflight(scm->fp->fp[i]);
889 UNIXCB(skb).fp = scm->fp;
890 skb->destructor = unix_destruct_fds;
891 scm->fp = NULL;
896 * Send AF_UNIX data.
899 static intunix_dgram_sendmsg(struct socket *sock,struct msghdr *msg,int len,
900 struct scm_cookie *scm)
902 struct sock *sk = sock->sk;
903 unix_socket *other;
904 struct sockaddr_un *sunaddr=msg->msg_name;
905 int namelen =0;/* fake GCC */
906 int err;
907 unsigned hash;
908 struct sk_buff *skb;
910 if(msg->msg_flags&MSG_OOB)
911 return-EOPNOTSUPP;
913 if(msg->msg_flags&~MSG_DONTWAIT)
914 return-EINVAL;
916 if(msg->msg_namelen) {
917 namelen =unix_mkname(sunaddr, msg->msg_namelen, &hash);
918 if(namelen <0)
919 return namelen;
920 }else{
921 sunaddr = NULL;
922 if(!unix_peer(sk))
923 return-ENOTCONN;
926 if(sock->passcred && !sk->protinfo.af_unix.addr)
927 unix_autobind(sock);
929 skb =sock_alloc_send_skb(sk, len,0, msg->msg_flags&MSG_DONTWAIT, &err);
931 if(skb==NULL)
932 return err;
934 memcpy(UNIXCREDS(skb), &scm->creds,sizeof(struct ucred));
935 UNIXCB(skb).attr = msg->msg_flags;
936 if(scm->fp)
937 unix_attach_fds(scm, skb);
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;
1109 if(flags&MSG_OOB)
1110 return-EOPNOTSUPP;
1112 msg->msg_namelen =0;
1114 retry:
1115 skb=skb_dequeue(&sk->receive_queue);
1117 if(skb==NULL)
1119 if(sk->shutdown & RCV_SHUTDOWN)
1120 return0;
1121 if(noblock)
1122 return-EAGAIN;
1123 unix_data_wait(sk);
1124 if(current->signal & ~current->blocked)
1125 return-ERESTARTSYS;
1126 goto retry;
1129 if(msg->msg_name)
1131 if(skb->sk->protinfo.af_unix.addr)
1133 memcpy(msg->msg_name, skb->sk->protinfo.af_unix.addr->name,
1134 skb->sk->protinfo.af_unix.addr->len);
1135 msg->msg_namelen=skb->sk->protinfo.af_unix.addr->len;
1137 else
1138 msg->msg_namelen=sizeof(short);
1141 if(size > skb->len)
1142 size = skb->len;
1143 else if(size < skb->len)
1144 msg->msg_flags |= MSG_TRUNC;
1146 if(memcpy_toiovec(msg->msg_iov, skb->data, size)) {
1147 skb_queue_head(&sk->receive_queue, skb);
1148 return-EFAULT;
1151 scm->creds = *UNIXCREDS(skb);
1153 if(!(flags & MSG_PEEK))
1155 if(UNIXCB(skb).fp)
1156 unix_detach_fds(scm, skb);
1157 kfree_skb(skb, FREE_WRITE);
1158 return size;
1159 }else
1160 /* It is questionable: on PEEK we could:
1161 - do not return fds - good, but too simple 8)
1162 - return fds, and do not return them on read (old strategy,
1163 apparently wrong)
1164 - clone fds (I choosed it for now, it is the most universal
1165 solution)
1167 POSIX 1003.1g does not actually define this clearly
1168 at all. POSIX 1003.1g doesn't define a lot of things
1169 clearly however!
1172 if(UNIXCB(skb).fp)
1173 scm->fp =scm_fp_dup(UNIXCB(skb).fp);
1175 skb_queue_head(&sk->receive_queue, skb);
1176 return size;
1180 static intunix_stream_recvmsg(struct socket *sock,struct msghdr *msg,int size,
1181 int flags,struct scm_cookie *scm)
1183 struct sock *sk = sock->sk;
1184 int noblock = flags & MSG_DONTWAIT;
1185 struct sockaddr_un *sunaddr=msg->msg_name;
1186 int copied =0;
1187 int check_creds =0;
1188 int target =1;
1190 if(sock->flags & SO_ACCEPTCON)
1191 return(-EINVAL);
1193 if(flags&MSG_OOB)
1194 return-EOPNOTSUPP;
1195 if(flags&MSG_WAITALL)
1196 target = size;
1199 msg->msg_namelen =0;
1201 /* Lock the socket to prevent queue disordering
1202 * while sleeps in memcpy_tomsg
1205 down(&sk->protinfo.af_unix.readsem);
1209 int chunk;
1210 struct sk_buff *skb;
1212 skb=skb_dequeue(&sk->receive_queue);
1213 if(skb==NULL)
1215 if(copied >= target)
1216 break;
1218 if(sk->err)
1219 returnsock_error(sk);
1221 if(sk->shutdown & RCV_SHUTDOWN)
1222 break;
1223 up(&sk->protinfo.af_unix.readsem);
1224 if(noblock)
1225 return-EAGAIN;
1226 unix_data_wait(sk);
1227 if(current->signal & ~current->blocked)
1228 return-ERESTARTSYS;
1229 down(&sk->protinfo.af_unix.readsem);
1230 continue;
1233 /* Never glue messages from different writers */
1234 if(check_creds &&
1235 memcmp(UNIXCREDS(skb), &scm->creds,sizeof(scm->creds)) !=0)
1237 skb_queue_head(&sk->receive_queue, skb);
1238 break;
1241 /* Copy address just once */
1242 if(sunaddr)
1244 if(skb->sk->protinfo.af_unix.addr)
1246 memcpy(sunaddr, skb->sk->protinfo.af_unix.addr->name,
1247 skb->sk->protinfo.af_unix.addr->len);
1248 msg->msg_namelen=skb->sk->protinfo.af_unix.addr->len;
1250 else
1251 msg->msg_namelen=sizeof(short);
1252 sunaddr = NULL;
1255 chunk =min(skb->len, size);
1256 memcpy_toiovec(msg->msg_iov, skb->data, chunk);
1257 copied += chunk;
1258 size -= chunk;
1260 /* Copy credentials */
1261 scm->creds = *UNIXCREDS(skb);
1262 check_creds =1;
1264 /* Mark read part of skb as used */
1265 if(!(flags & MSG_PEEK))
1267 skb_pull(skb, chunk);
1269 if(UNIXCB(skb).fp)
1270 unix_detach_fds(scm, skb);
1272 /* put the skb back if we didn't use it up.. */
1273 if(skb->len)
1275 skb_queue_head(&sk->receive_queue, skb);
1276 break;
1279 kfree_skb(skb, FREE_WRITE);
1281 if(scm->fp)
1282 break;
1284 else
1286 /* It is questionable, see note in unix_dgram_recvmsg.
1289 if(UNIXCB(skb).fp)
1290 scm->fp =scm_fp_dup(UNIXCB(skb).fp);
1292 /* put message back and return */
1293 skb_queue_head(&sk->receive_queue, skb);
1294 break;
1296 }while(size);
1298 up(&sk->protinfo.af_unix.readsem);
1299 return copied;
1302 static intunix_shutdown(struct socket *sock,int mode)
1304 struct sock *sk = sock->sk;
1305 unix_socket *other=unix_peer(sk);
1307 mode++;
1309 if(mode&SEND_SHUTDOWN)
1311 sk->shutdown|=SEND_SHUTDOWN;
1312 sk->state_change(sk);
1313 if(other && sk->type == SOCK_STREAM && other->state != TCP_LISTEN)
1315 if(unix_our_peer(sk, other))
1316 other->shutdown|=RCV_SHUTDOWN;
1317 other->state_change(other);
1320 other=unix_peer(sk);
1321 if(mode&RCV_SHUTDOWN)
1323 sk->shutdown|=RCV_SHUTDOWN;
1324 sk->state_change(sk);
1325 if(other && sk->type != SOCK_DGRAM && other->state != TCP_LISTEN)
1327 if(unix_our_peer(sk, other))
1328 other->shutdown|=SEND_SHUTDOWN;
1329 other->state_change(other);
1332 return0;
1336 static intunix_ioctl(struct socket *sock,unsigned int cmd,unsigned long arg)
1338 struct sock *sk = sock->sk;
1339 long amount=0;
1341 switch(cmd)
1344 case TIOCOUTQ:
1345 amount=sk->sndbuf-sk->wmem_alloc;
1346 if(amount<0)
1347 amount=0;
1348 returnput_user(amount, (int*)arg);
1349 case TIOCINQ:
1351 struct sk_buff *skb;
1352 if(sk->state==TCP_LISTEN)
1353 return-EINVAL;
1355 * These two are safe on current systems as
1356 * only user tasks fiddle here
1358 if((skb=skb_peek(&sk->receive_queue))!=NULL)
1359 amount=skb->len;
1360 returnput_user(amount, (int*)arg);
1363 default:
1364 return-EINVAL;
1366 /*NOTREACHED*/
1367 return(0);
1370 #ifdef CONFIG_PROC_FS
1371 static intunix_read_proc(char*buffer,char**start, off_t offset,
1372 int length,int*eof,void*data)
1374 off_t pos=0;
1375 off_t begin=0;
1376 int len=0;
1377 int i;
1378 unix_socket *s;
1380 len+=sprintf(buffer,"Num RefCount Protocol Flags Type St "
1381 "Inode Path\n");
1383 forall_unix_sockets(i,s)
1385 len+=sprintf(buffer+len,"%p: %08X %08X %08lX %04X %02X %5ld",
1387 s->sock_readers,
1389 s->socket ? s->socket->flags :0,
1390 s->type,
1391 s->socket ? s->socket->state :0,
1392 s->socket ? s->socket->inode->i_ino :0);
1394 if(s->protinfo.af_unix.addr)
1396 buffer[len++] =' ';
1397 memcpy(buffer+len, s->protinfo.af_unix.addr->name->sun_path,
1398 s->protinfo.af_unix.addr->len-sizeof(short));
1399 if(!UNIX_ABSTRACT(s))
1400 len--;
1401 else
1402 buffer[len] ='@';
1403 len += s->protinfo.af_unix.addr->len -sizeof(short);
1405 buffer[len++]='\n';
1407 pos=begin+len;
1408 if(pos<offset)
1410 len=0;
1411 begin=pos;
1413 if(pos>offset+length)
1414 goto done;
1416 *eof =1;
1417 done:
1418 *start=buffer+(offset-begin);
1419 len-=(offset-begin);
1420 if(len>length)
1421 len=length;
1422 return len;
1424 #endif
1426 struct proto_ops unix_stream_ops = {
1427 AF_UNIX,
1429 unix_dup,
1430 unix_release,
1431 unix_bind,
1432 unix_stream_connect,
1433 unix_socketpair,
1434 unix_accept,
1435 unix_getname,
1436 datagram_poll,
1437 unix_ioctl,
1438 unix_listen,
1439 unix_shutdown,
1440 sock_no_setsockopt,
1441 sock_no_getsockopt,
1442 sock_no_fcntl,
1443 unix_stream_sendmsg,
1444 unix_stream_recvmsg
1447 struct proto_ops unix_dgram_ops = {
1448 AF_UNIX,
1450 unix_dup,
1451 unix_release,
1452 unix_bind,
1453 unix_dgram_connect,
1454 unix_socketpair,
1455 NULL,
1456 unix_getname,
1457 datagram_poll,
1458 unix_ioctl,
1459 sock_no_listen,
1460 unix_shutdown,
1461 sock_no_setsockopt,
1462 sock_no_getsockopt,
1463 sock_no_fcntl,
1464 unix_dgram_sendmsg,
1465 unix_dgram_recvmsg
1468 struct net_proto_family unix_family_ops = {
1469 AF_UNIX,
1470 unix_create
1473 voidunix_proto_init(struct net_proto *pro)
1475 struct sk_buff *dummy_skb;
1476 struct proc_dir_entry *ent;
1478 printk(KERN_INFO "NET3: Unix domain sockets 0.15 for Linux NET3.038.\n");
1479 if(sizeof(struct unix_skb_parms) >sizeof(dummy_skb->cb))
1481 printk(KERN_CRIT "unix_proto_init: panic\n");
1482 return;
1484 sock_register(&unix_family_ops);
1485 #ifdef CONFIG_PROC_FS
1486 ent =create_proc_entry("net/unix",0,0);
1487 ent->read_proc = unix_read_proc;
1488 #endif
1491 * Local variables:
1492 * compile-command: "gcc -g -D__KERNEL__ -Wall -O6 -I/usr/src/linux/include -c af_unix.c"
1493 * End:
close