This source file includes following definitions.
- unix_mkname
- unix_remove_socket
- unix_insert_socket
- unix_find_socket
- unix_destroy_timer
- unix_delayed_delete
- unix_destroy_socket
- unix_fcntl
- unix_setsockopt
- unix_getsockopt
- unix_listen
- def_callback1
- def_callback2
- def_callback3
- unix_create
- unix_dup
- unix_release
- unix_find_other
- unix_bind
- unix_connect
- unix_socketpair
- unix_accept
- unix_getname
- unix_copyrights
- unix_returnrights
- unix_fd_copy
- unix_fd_free
- unix_files_free
- unix_detach_fds
- unix_destruct_fds
- unix_attach_fds
- unix_sendmsg
- unix_data_wait
- unix_recvmsg
- unix_shutdown
- unix_select
- unix_ioctl
- unix_get_info
- unix_proto_init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 #include <linux/config.h>
48 #include <linux/kernel.h>
49 #include <linux/major.h>
50 #include <linux/signal.h>
51 #include <linux/sched.h>
52 #include <linux/errno.h>
53 #include <linux/string.h>
54 #include <linux/stat.h>
55 #include <linux/socket.h>
56 #include <linux/un.h>
57 #include <linux/fcntl.h>
58 #include <linux/termios.h>
59 #include <linux/socket.h>
60 #include <linux/sockios.h>
61 #include <linux/net.h>
62 #include <linux/in.h>
63 #include <linux/fs.h>
64 #include <linux/malloc.h>
65 #include <asm/segment.h>
66 #include <linux/skbuff.h>
67 #include <linux/netdevice.h>
68 #include <net/sock.h>
69 #include <net/tcp.h>
70 #include <net/af_unix.h>
71 #include <linux/proc_fs.h>
72
73 unix_socket *unix_socket_list=NULL;
74
75 #define min(a,b) (((a)<(b))?(a):(b))
76
77
78
79
80
81 static inline void unix_mkname(struct sockaddr_un * sunaddr, unsigned long len)
82 {
83 if (len >= sizeof(*sunaddr))
84 len = sizeof(*sunaddr)-1;
85 ((char *)sunaddr)[len]=0;
86 }
87
88
89
90
91
92
93
94 static void unix_remove_socket(unix_socket *sk)
95 {
96 unix_socket **s;
97
98 cli();
99 s=&unix_socket_list;
100
101 while(*s!=NULL)
102 {
103 if(*s==sk)
104 {
105 *s=sk->next;
106 sti();
107 return;
108 }
109 s=&((*s)->next);
110 }
111 sti();
112 }
113
114 static void unix_insert_socket(unix_socket *sk)
115 {
116 cli();
117 sk->next=unix_socket_list;
118 unix_socket_list=sk;
119 sti();
120 }
121
122 static unix_socket *unix_find_socket(struct inode *i)
123 {
124 unix_socket *s;
125 cli();
126 s=unix_socket_list;
127 while(s)
128 {
129 if(s->protinfo.af_unix.inode==i)
130 {
131 sti();
132 return(s);
133 }
134 s=s->next;
135 }
136 sti();
137 return(NULL);
138 }
139
140
141
142
143
144 static void unix_destroy_timer(unsigned long data)
145 {
146 unix_socket *sk=(unix_socket *)data;
147 if(sk->protinfo.af_unix.locks==0 && sk->wmem_alloc==0)
148 {
149 if(sk->protinfo.af_unix.name)
150 kfree(sk->protinfo.af_unix.name);
151 sk_free(sk);
152 return;
153 }
154
155
156
157
158
159 sk->timer.expires=jiffies+10*HZ;
160 add_timer(&sk->timer);
161 }
162
163
164 static void unix_delayed_delete(unix_socket *sk)
165 {
166 sk->timer.data=(unsigned long)sk;
167 sk->timer.expires=jiffies+HZ;
168 sk->timer.function=unix_destroy_timer;
169 add_timer(&sk->timer);
170 }
171
172 static void unix_destroy_socket(unix_socket *sk)
173 {
174 struct sk_buff *skb;
175
176 unix_remove_socket(sk);
177
178 while((skb=skb_dequeue(&sk->receive_queue))!=NULL)
179 {
180 if(sk->state==TCP_LISTEN)
181 {
182 unix_socket *osk=skb->sk;
183 osk->state=TCP_CLOSE;
184 kfree_skb(skb, FREE_WRITE);
185 osk->state_change(osk);
186
187 }
188 else
189 {
190
191 kfree_skb(skb,FREE_WRITE);
192 }
193 }
194
195 if(sk->protinfo.af_unix.inode!=NULL)
196 {
197 iput(sk->protinfo.af_unix.inode);
198 sk->protinfo.af_unix.inode=NULL;
199 }
200
201 if(--sk->protinfo.af_unix.locks==0 && sk->wmem_alloc==0)
202 {
203 if(sk->protinfo.af_unix.name)
204 kfree(sk->protinfo.af_unix.name);
205 sk_free(sk);
206 }
207 else
208 {
209 sk->dead=1;
210 unix_delayed_delete(sk);
211 }
212 }
213
214
215
216
217
218 static int unix_fcntl(struct socket *sock, unsigned int cmd, unsigned long arg)
219 {
220 return -EINVAL;
221 }
222
223
224
225
226
227 static int unix_setsockopt(struct socket *sock, int level, int optname, char *optval, int optlen)
228 {
229 unix_socket *sk=sock->data;
230 if(level!=SOL_SOCKET)
231 return -EOPNOTSUPP;
232 return sock_setsockopt(sk,level,optname,optval,optlen);
233 }
234
235 static int unix_getsockopt(struct socket *sock, int level, int optname, char *optval, int *optlen)
236 {
237 unix_socket *sk=sock->data;
238 if(level!=SOL_SOCKET)
239 return -EOPNOTSUPP;
240 return sock_getsockopt(sk,level,optname,optval,optlen);
241 }
242
243 static int unix_listen(struct socket *sock, int backlog)
244 {
245 unix_socket *sk=sock->data;
246 if(sk->type!=SOCK_STREAM)
247 return -EOPNOTSUPP;
248 if(sk->protinfo.af_unix.name==NULL)
249 return -EINVAL;
250 sk->max_ack_backlog=backlog;
251 sk->state=TCP_LISTEN;
252 return 0;
253 }
254
255 static void def_callback1(struct sock *sk)
256 {
257 if(!sk->dead)
258 wake_up_interruptible(sk->sleep);
259 }
260
261 static void def_callback2(struct sock *sk, int len)
262 {
263 if(!sk->dead)
264 {
265 wake_up_interruptible(sk->sleep);
266 sock_wake_async(sk->socket, 1);
267 }
268 }
269
270 static void def_callback3(struct sock *sk)
271 {
272 if(!sk->dead)
273 {
274 wake_up_interruptible(sk->sleep);
275 sock_wake_async(sk->socket, 2);
276 }
277 }
278
279 static int unix_create(struct socket *sock, int protocol)
280 {
281 unix_socket *sk;
282 if(protocol && protocol != PF_UNIX)
283 return -EPROTONOSUPPORT;
284 sk=(unix_socket *)sk_alloc(GFP_KERNEL);
285 if(sk==NULL)
286 return -ENOMEM;
287 switch(sock->type)
288 {
289 case SOCK_STREAM:
290 break;
291
292
293
294
295 case SOCK_RAW:
296 sock->type=SOCK_DGRAM;
297 case SOCK_DGRAM:
298 break;
299 default:
300 sk_free(sk);
301 return -ESOCKTNOSUPPORT;
302 }
303 sk->type=sock->type;
304 init_timer(&sk->timer);
305 skb_queue_head_init(&sk->write_queue);
306 skb_queue_head_init(&sk->receive_queue);
307 skb_queue_head_init(&sk->back_log);
308 sk->protinfo.af_unix.family=AF_UNIX;
309 sk->protinfo.af_unix.inode=NULL;
310 sk->protinfo.af_unix.locks=1;
311 sk->protinfo.af_unix.readsem=MUTEX;
312 sk->rcvbuf=SK_RMEM_MAX;
313 sk->sndbuf=SK_WMEM_MAX;
314 sk->allocation=GFP_KERNEL;
315 sk->state=TCP_CLOSE;
316 sk->priority=SOPRI_NORMAL;
317 sk->state_change=def_callback1;
318 sk->data_ready=def_callback2;
319 sk->write_space=def_callback3;
320 sk->error_report=def_callback1;
321 sk->mtu=4096;
322 sk->socket=sock;
323 sock->data=(void *)sk;
324 sk->sleep=sock->wait;
325 unix_insert_socket(sk);
326 return 0;
327 }
328
329 static int unix_dup(struct socket *newsock, struct socket *oldsock)
330 {
331 return unix_create(newsock,0);
332 }
333
334 static int unix_release(struct socket *sock, struct socket *peer)
335 {
336 unix_socket *sk=sock->data;
337 unix_socket *skpair;
338
339
340
341 if(sk==NULL)
342 return 0;
343
344 sk->state_change(sk);
345 sk->dead=1;
346 skpair=(unix_socket *)sk->protinfo.af_unix.other;
347 if(sk->type==SOCK_STREAM && skpair!=NULL && skpair->state!=TCP_LISTEN)
348 {
349 skpair->shutdown=SHUTDOWN_MASK;
350 skpair->state_change(skpair);
351 }
352 if(skpair!=NULL)
353 skpair->protinfo.af_unix.locks--;
354 sk->protinfo.af_unix.other=NULL;
355 unix_destroy_socket(sk);
356
357
358
359
360
361
362 return 0;
363 }
364
365
366 static unix_socket *unix_find_other(char *path, int *error)
367 {
368 int old_fs;
369 int err;
370 struct inode *inode;
371 unix_socket *u;
372
373 old_fs=get_fs();
374 set_fs(get_ds());
375 err = open_namei(path, 2, S_IFSOCK, &inode, NULL);
376 set_fs(old_fs);
377 if(err<0)
378 {
379 *error=err;
380 return NULL;
381 }
382 u=unix_find_socket(inode);
383 iput(inode);
384 if(u==NULL)
385 {
386 *error=-ECONNREFUSED;
387 return NULL;
388 }
389 return u;
390 }
391
392
393 static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
394 {
395 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
396 unix_socket *sk=sock->data;
397 int old_fs;
398 int err;
399
400 if(sk->protinfo.af_unix.name)
401 return -EINVAL;
402
403 if(addr_len>sizeof(struct sockaddr_un) || addr_len<3 || sunaddr->sun_family!=AF_UNIX)
404 return -EINVAL;
405 unix_mkname(sunaddr, addr_len);
406
407
408
409 if(sk->protinfo.af_unix.inode!=NULL)
410 return -EINVAL;
411
412 sk->protinfo.af_unix.name=kmalloc(addr_len+1, GFP_KERNEL);
413 if(sk->protinfo.af_unix.name==NULL)
414 return -ENOMEM;
415 memcpy(sk->protinfo.af_unix.name, sunaddr->sun_path, addr_len+1);
416
417 old_fs=get_fs();
418 set_fs(get_ds());
419
420 err=do_mknod(sk->protinfo.af_unix.name,S_IFSOCK|S_IRWXUGO,0);
421 if(err==0)
422 err=open_namei(sk->protinfo.af_unix.name, 2, S_IFSOCK, &sk->protinfo.af_unix.inode, NULL);
423
424 set_fs(old_fs);
425
426 if(err<0)
427 {
428 kfree_s(sk->protinfo.af_unix.name,addr_len+1);
429 sk->protinfo.af_unix.name=NULL;
430 if(err==-EEXIST)
431 return -EADDRINUSE;
432 else
433 return err;
434 }
435
436 return 0;
437
438 }
439
440 static int unix_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags)
441 {
442 unix_socket *sk=sock->data;
443 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
444 unix_socket *other;
445 struct sk_buff *skb;
446 int err;
447
448 if(sk->type==SOCK_STREAM && sk->protinfo.af_unix.other)
449 {
450 if(sock->state==SS_CONNECTING && sk->state==TCP_ESTABLISHED)
451 {
452 sock->state=SS_CONNECTED;
453 return 0;
454 }
455 if(sock->state==SS_CONNECTING && sk->state == TCP_CLOSE)
456 {
457 sock->state=SS_UNCONNECTED;
458 return -ECONNREFUSED;
459 }
460 if(sock->state!=SS_CONNECTING)
461 return -EISCONN;
462 if(flags&O_NONBLOCK)
463 return -EALREADY;
464
465
466
467 }
468
469 if(addr_len < sizeof(sunaddr->sun_family)+1 || sunaddr->sun_family!=AF_UNIX)
470 return -EINVAL;
471
472 unix_mkname(sunaddr, addr_len);
473
474 if(sk->type==SOCK_DGRAM)
475 {
476 if(sk->protinfo.af_unix.other)
477 {
478 sk->protinfo.af_unix.other->protinfo.af_unix.locks--;
479 sk->protinfo.af_unix.other=NULL;
480 sock->state=SS_UNCONNECTED;
481 }
482 other=unix_find_other(sunaddr->sun_path, &err);
483 if(other==NULL)
484 return err;
485 if(other->type!=sk->type)
486 return -EPROTOTYPE;
487 other->protinfo.af_unix.locks++;
488 sk->protinfo.af_unix.other=other;
489 sock->state=SS_CONNECTED;
490 sk->state=TCP_ESTABLISHED;
491 return 0;
492 }
493
494
495 if(sock->state==SS_UNCONNECTED)
496 {
497
498
499
500
501 skb=sock_alloc_send_skb(sk, 0, 0, 0, &err);
502 if(skb==NULL)
503 return err;
504 skb->sk=sk;
505 skb->free=1;
506 sk->state=TCP_CLOSE;
507 unix_mkname(sunaddr, addr_len);
508 other=unix_find_other(sunaddr->sun_path, &err);
509 if(other==NULL)
510 {
511 kfree_skb(skb, FREE_WRITE);
512 return err;
513 }
514 if(other->type!=sk->type)
515 {
516 kfree_skb(skb, FREE_WRITE);
517 return -EPROTOTYPE;
518 }
519 other->protinfo.af_unix.locks++;
520 other->ack_backlog++;
521 sk->protinfo.af_unix.other=other;
522 skb_queue_tail(&other->receive_queue,skb);
523 sk->state=TCP_SYN_SENT;
524 sock->state=SS_CONNECTING;
525 sti();
526 other->data_ready(other,0);
527 }
528
529
530
531
532 cli();
533 while(sk->state==TCP_SYN_SENT)
534 {
535 if(flags&O_NONBLOCK)
536 {
537 sti();
538 return -EINPROGRESS;
539 }
540 interruptible_sleep_on(sk->sleep);
541 if(current->signal & ~current->blocked)
542 {
543 sti();
544 return -ERESTARTSYS;
545 }
546 }
547
548
549
550
551
552 if(sk->state==TCP_CLOSE)
553 {
554 sk->protinfo.af_unix.other->protinfo.af_unix.locks--;
555 sk->protinfo.af_unix.other=NULL;
556 sock->state=SS_UNCONNECTED;
557 sti();
558 return -ECONNREFUSED;
559 }
560
561
562
563
564
565 sock->state=SS_CONNECTED;
566 sti();
567 return 0;
568
569 }
570
571 static int unix_socketpair(struct socket *a, struct socket *b)
572 {
573 unix_socket *ska,*skb;
574
575 ska=a->data;
576 skb=b->data;
577
578
579 ska->protinfo.af_unix.locks++;
580 skb->protinfo.af_unix.locks++;
581 ska->protinfo.af_unix.other=skb;
582 skb->protinfo.af_unix.other=ska;
583 ska->state=TCP_ESTABLISHED;
584 skb->state=TCP_ESTABLISHED;
585 return 0;
586 }
587
588 static int unix_accept(struct socket *sock, struct socket *newsock, int flags)
589 {
590 unix_socket *sk=sock->data;
591 unix_socket *newsk, *tsk;
592 struct sk_buff *skb;
593
594 if(sk->type!=SOCK_STREAM)
595 {
596 return -EOPNOTSUPP;
597 }
598 if(sk->state!=TCP_LISTEN)
599 {
600 return -EINVAL;
601 }
602
603 newsk=newsock->data;
604 if(sk->protinfo.af_unix.name!=NULL)
605 {
606 newsk->protinfo.af_unix.name=kmalloc(strlen(sk->protinfo.af_unix.name)+1, GFP_KERNEL);
607 if(newsk->protinfo.af_unix.name==NULL)
608 return -ENOMEM;
609 strcpy(newsk->protinfo.af_unix.name, sk->protinfo.af_unix.name);
610 }
611
612 do
613 {
614 cli();
615 skb=skb_dequeue(&sk->receive_queue);
616 if(skb==NULL)
617 {
618 if(flags&O_NONBLOCK)
619 {
620 sti();
621 return -EAGAIN;
622 }
623 interruptible_sleep_on(sk->sleep);
624 if(current->signal & ~current->blocked)
625 {
626 sti();
627 return -ERESTARTSYS;
628 }
629 sti();
630 }
631 }
632 while(skb==NULL);
633 tsk=skb->sk;
634 kfree_skb(skb, FREE_WRITE);
635 sk->ack_backlog--;
636 newsk->protinfo.af_unix.other=tsk;
637 tsk->protinfo.af_unix.other=newsk;
638 tsk->state=TCP_ESTABLISHED;
639 newsk->state=TCP_ESTABLISHED;
640 newsk->protinfo.af_unix.locks++;
641 sk->protinfo.af_unix.locks--;
642 tsk->protinfo.af_unix.locks++;
643 sti();
644 tsk->state_change(tsk);
645 sock_wake_async(tsk->socket, 0);
646 return 0;
647 }
648
649 static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer)
650 {
651 unix_socket *sk=sock->data;
652 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
653
654 if(peer)
655 {
656 if(sk->protinfo.af_unix.other==NULL)
657 return -ENOTCONN;
658 sk=sk->protinfo.af_unix.other;
659 }
660 sunaddr->sun_family=AF_UNIX;
661 if(sk->protinfo.af_unix.name==NULL)
662 {
663 *sunaddr->sun_path=0;
664 *uaddr_len=sizeof(sunaddr->sun_family)+1;
665 return 0;
666 }
667 *uaddr_len=sizeof(sunaddr->sun_family)+strlen(sk->protinfo.af_unix.name)+1;
668 strcpy(sunaddr->sun_path,sk->protinfo.af_unix.name);
669 return 0;
670 }
671
672
673
674
675
676 static struct cmsghdr *unix_copyrights(void *userp, int len)
677 {
678 struct cmsghdr *cm;
679
680 if(len>256|| len <=0)
681 return NULL;
682 cm=kmalloc(len, GFP_KERNEL);
683 memcpy_fromfs(cm, userp, len);
684 return cm;
685 }
686
687
688
689
690
691 static void unix_returnrights(void *userp, int len, struct cmsghdr *cm)
692 {
693 memcpy_tofs(userp, cm, len);
694 kfree(cm);
695 }
696
697
698
699
700
701
702 static int unix_fd_copy(struct sock *sk, struct cmsghdr *cmsg, struct file **fp)
703 {
704 int num=cmsg->cmsg_len-sizeof(struct cmsghdr);
705 int i;
706 int *fdp=(int *)cmsg->cmsg_data;
707 num/=4;
708
709
710 if(num>=UNIX_MAX_FD)
711 return -EINVAL;
712
713
714
715
716
717 for(i=0; i< num; i++)
718 {
719 int fd;
720
721 fd = fdp[i];
722 #if 0
723 printk("testing fd %d\n", fd);
724 #endif
725 if(fd < 0|| fd >=NR_OPEN)
726 return -EBADF;
727 if(current->files->fd[fd]==NULL)
728 return -EBADF;
729 }
730
731
732 for(i=0; i< num; i++)
733 {
734 fp[i]=current->files->fd[fdp[i]];
735 fp[i]->f_count++;
736 unix_inflight(fp[i]);
737 }
738
739 return num;
740 }
741
742
743
744
745
746 static void unix_fd_free(struct sock *sk, struct file **fp, int num)
747 {
748 int i;
749 for(i=0;i<num;i++)
750 {
751 close_fp(fp[i]);
752 unix_notinflight(fp[i]);
753 }
754 }
755
756
757
758
759
760
761
762
763 static int unix_files_free(void)
764 {
765 int i;
766 int n=0;
767 for (i=0;i<NR_OPEN;i++)
768 {
769 if(current->files->fd[i])
770 n++;
771 }
772
773 i=NR_OPEN;
774 if(i>current->rlim[RLIMIT_NOFILE].rlim_cur)
775 i=current->rlim[RLIMIT_NOFILE].rlim_cur;
776 if(n>=i)
777 return 0;
778 return i-n;
779 }
780
781
782
783
784
785
786 static void unix_detach_fds(struct sk_buff *skb, struct cmsghdr *cmsg)
787 {
788 int i;
789
790 int cmnum;
791 struct file **fp;
792 struct file **ufp;
793 int *cmfptr=NULL;
794
795 int fdnum;
796 int ffree;
797 int ufn=0;
798
799 if(cmsg==NULL)
800 cmnum=0;
801 else
802 {
803 cmnum=cmsg->cmsg_len-sizeof(struct cmsghdr);
804 cmnum/=sizeof(int);
805 cmfptr=(int *)&cmsg->cmsg_data;
806 }
807
808 memcpy(&fdnum,skb->h.filp,sizeof(int));
809 fp=(struct file **)(skb->h.filp+sizeof(int));
810 if(cmnum>fdnum)
811 cmnum=fdnum;
812 ffree=unix_files_free();
813 if(cmnum>ffree)
814 cmnum=ffree;
815 ufp=¤t->files->fd[0];
816
817
818
819
820 for(i=0;i<cmnum;i++)
821 {
822
823
824
825 while(ufp[ufn]!=NULL)
826 ufn++;
827 ufp[ufn]=fp[i];
828 *cmfptr++=ufn;
829 FD_CLR(ufn,¤t->files->close_on_exec);
830 unix_notinflight(fp[i]);
831 }
832
833
834
835 for(;i<fdnum;i++)
836 {
837 close_fp(fp[i]);
838 unix_notinflight(fp[i]);
839 }
840 kfree(skb->h.filp);
841 skb->h.filp=NULL;
842
843
844 skb->destructor = NULL;
845 }
846
847 static void unix_destruct_fds(struct sk_buff *skb)
848 {
849 unix_detach_fds(skb,NULL);
850 }
851
852
853
854
855 static void unix_attach_fds(int fpnum,struct file **fp,struct sk_buff *skb)
856 {
857
858 skb->h.filp=kmalloc(sizeof(int)+fpnum*sizeof(struct file *),
859 GFP_KERNEL);
860
861 memcpy(skb->h.filp,&fpnum,sizeof(int));
862
863 memcpy(skb->h.filp+sizeof(int),fp,fpnum*sizeof(struct file *));
864 skb->destructor = unix_destruct_fds;
865 }
866
867
868
869
870
871 static int unix_sendmsg(struct socket *sock, struct msghdr *msg, int len, int nonblock, int flags)
872 {
873 unix_socket *sk=sock->data;
874 unix_socket *other;
875 struct sockaddr_un *sunaddr=msg->msg_name;
876 int err,size;
877 struct sk_buff *skb;
878 int limit=0;
879 int sent=0;
880 struct file *fp[UNIX_MAX_FD];
881
882
883
884 int fpnum=0;
885
886 if(sk->err)
887 return sock_error(sk);
888
889 if(flags&MSG_OOB)
890 return -EOPNOTSUPP;
891
892 if(flags) {
893 return -EINVAL;
894 }
895
896 if(sunaddr!=NULL)
897 {
898 if(sock->type==SOCK_STREAM)
899 {
900 if(sk->state==TCP_ESTABLISHED)
901 return -EISCONN;
902 else
903 return -EOPNOTSUPP;
904 }
905 }
906
907 if(sunaddr==NULL)
908 {
909 if(sk->protinfo.af_unix.other==NULL)
910 return -ENOTCONN;
911 }
912
913
914
915
916 if(msg->msg_accrights)
917 {
918 struct cmsghdr *cm=unix_copyrights(msg->msg_accrights,
919 msg->msg_accrightslen);
920 if(cm==NULL || msg->msg_accrightslen<sizeof(struct cmsghdr) ||
921 cm->cmsg_type!=SCM_RIGHTS ||
922 cm->cmsg_level!=SOL_SOCKET ||
923 msg->msg_accrightslen!=cm->cmsg_len)
924 {
925 kfree(cm);
926 return -EINVAL;
927 }
928 fpnum=unix_fd_copy(sk,cm,fp);
929 kfree(cm);
930 if(fpnum<0) {
931 return fpnum;
932 }
933 }
934
935 while(sent < len)
936 {
937
938
939
940
941
942 size=len-sent;
943
944 if(size>(sk->sndbuf-sizeof(struct sk_buff))/2)
945 {
946 if(sock->type==SOCK_DGRAM)
947 {
948 unix_fd_free(sk,fp,fpnum);
949 return -EMSGSIZE;
950 }
951 size=(sk->sndbuf-sizeof(struct sk_buff))/2;
952 }
953
954
955
956
957
958
959 if(size > 4000 && sock->type!=SOCK_DGRAM)
960 limit = 4000;
961 else
962 limit = 0;
963
964
965
966
967
968 skb=sock_alloc_send_skb(sk,size,limit,nonblock, &err);
969
970 if(skb==NULL)
971 {
972 unix_fd_free(sk,fp,fpnum);
973 if(sent)
974 {
975 sk->err=-err;
976 return sent;
977 }
978 return err;
979 }
980 size=skb_tailroom(skb);
981
982 skb->sk=sk;
983 skb->free=1;
984
985 if(fpnum)
986 {
987 unix_attach_fds(fpnum,fp,skb);
988 fpnum=0;
989 }
990 else
991 skb->h.filp=NULL;
992
993 memcpy_fromiovec(skb_put(skb,size),msg->msg_iov, size);
994
995 cli();
996 if(sunaddr==NULL)
997 {
998 other=sk->protinfo.af_unix.other;
999 if(sock->type==SOCK_DGRAM && other->dead)
1000 {
1001 other->protinfo.af_unix.locks--;
1002 sk->protinfo.af_unix.other=NULL;
1003 sock->state=SS_UNCONNECTED;
1004 sti();
1005 kfree_skb(skb, FREE_WRITE);
1006 if(!sent)
1007 return -ECONNRESET;
1008 else
1009 return sent;
1010 }
1011 }
1012 else
1013 {
1014 unix_mkname(sunaddr, msg->msg_namelen);
1015 other=unix_find_other(sunaddr->sun_path, &err);
1016 if(other==NULL)
1017 {
1018 sti();
1019 kfree_skb(skb, FREE_WRITE);
1020 if(sent)
1021 return sent;
1022 else
1023 return err;
1024 }
1025 }
1026 skb_queue_tail(&other->receive_queue, skb);
1027 sti();
1028
1029 other->data_ready(other,size);
1030 sent+=size;
1031 }
1032 return sent;
1033 }
1034
1035
1036
1037
1038
1039 static void unix_data_wait(unix_socket * sk)
1040 {
1041 cli();
1042 if (!skb_peek(&sk->receive_queue)) {
1043 sk->socket->flags |= SO_WAITDATA;
1044 interruptible_sleep_on(sk->sleep);
1045 sk->socket->flags &= ~SO_WAITDATA;
1046 }
1047 sti();
1048 }
1049
1050 static int unix_recvmsg(struct socket *sock, struct msghdr *msg, int size, int noblock, int flags, int *addr_len)
1051 {
1052 unix_socket *sk=sock->data;
1053 struct sockaddr_un *sunaddr=msg->msg_name;
1054 struct sk_buff *skb;
1055 int copied=0;
1056 unsigned char *sp;
1057 int len;
1058 int num;
1059 struct iovec *iov=msg->msg_iov;
1060 struct cmsghdr *cm=NULL;
1061 int ct=msg->msg_iovlen;
1062
1063 if(flags&MSG_OOB)
1064 return -EOPNOTSUPP;
1065
1066 if(addr_len)
1067 *addr_len=0;
1068
1069 if(sk->err)
1070 return sock_error(sk);
1071
1072 if(msg->msg_accrights)
1073 {
1074 cm=unix_copyrights(msg->msg_accrights,
1075 msg->msg_accrightslen);
1076 if(msg->msg_accrightslen<sizeof(struct cmsghdr)
1077 #if 0
1078
1079 ||
1080 cm->cmsg_type!=SCM_RIGHTS ||
1081 cm->cmsg_level!=SOL_SOCKET ||
1082 msg->msg_accrightslen!=cm->cmsg_len
1083 #endif
1084 )
1085 {
1086 kfree(cm);
1087 printk("recvmsg: Bad msg_accrights\n");
1088 return -EINVAL;
1089 }
1090 }
1091
1092 down(&sk->protinfo.af_unix.readsem);
1093 while(ct--)
1094 {
1095 int done=0;
1096 sp=iov->iov_base;
1097 len=iov->iov_len;
1098 iov++;
1099
1100 while(done<len)
1101 {
1102 if (copied && (flags & MSG_PEEK))
1103 goto out;
1104 if (copied == size)
1105 goto out;
1106 skb=skb_dequeue(&sk->receive_queue);
1107 if(skb==NULL)
1108 {
1109 up(&sk->protinfo.af_unix.readsem);
1110 if(sk->shutdown & RCV_SHUTDOWN)
1111 return copied;
1112 if(copied)
1113 return copied;
1114 if(noblock)
1115 return -EAGAIN;
1116 if(current->signal & ~current->blocked)
1117 return -ERESTARTSYS;
1118 unix_data_wait(sk);
1119 down(&sk->protinfo.af_unix.readsem);
1120 continue;
1121 }
1122 if(msg->msg_name!=NULL)
1123 {
1124 sunaddr->sun_family=AF_UNIX;
1125 if(skb->sk->protinfo.af_unix.name)
1126 {
1127 memcpy(sunaddr->sun_path, skb->sk->protinfo.af_unix.name, 108);
1128 if(addr_len)
1129 *addr_len=strlen(sunaddr->sun_path)+sizeof(short);
1130 }
1131 else
1132 if(addr_len)
1133 *addr_len=sizeof(short);
1134 }
1135
1136 num=min(skb->len,len-done);
1137 memcpy_tofs(sp, skb->data, num);
1138
1139 if (skb->h.filp!=NULL)
1140 unix_detach_fds(skb,cm);
1141
1142 copied+=num;
1143 done+=num;
1144 sp+=num;
1145 if (!(flags & MSG_PEEK))
1146 skb_pull(skb, num);
1147
1148 if (skb->len) {
1149 skb_queue_head(&sk->receive_queue, skb);
1150 continue;
1151 }
1152 kfree_skb(skb, FREE_WRITE);
1153 if(sock->type==SOCK_DGRAM || cm)
1154 goto out;
1155 }
1156 }
1157 out:
1158 up(&sk->protinfo.af_unix.readsem);
1159 if(cm)
1160 unix_returnrights(msg->msg_accrights,msg->msg_accrightslen,cm);
1161 return copied;
1162 }
1163
1164 static int unix_shutdown(struct socket *sock, int mode)
1165 {
1166 unix_socket *sk=(unix_socket *)sock->data;
1167 unix_socket *other=sk->protinfo.af_unix.other;
1168 if(mode&SEND_SHUTDOWN)
1169 {
1170 sk->shutdown|=SEND_SHUTDOWN;
1171 sk->state_change(sk);
1172 if(other)
1173 {
1174 other->shutdown|=RCV_SHUTDOWN;
1175 other->state_change(other);
1176 }
1177 }
1178 other=sk->protinfo.af_unix.other;
1179 if(mode&RCV_SHUTDOWN)
1180 {
1181 sk->shutdown|=RCV_SHUTDOWN;
1182 sk->state_change(sk);
1183 if(other)
1184 {
1185 other->shutdown|=SEND_SHUTDOWN;
1186 other->state_change(other);
1187 }
1188 }
1189 return 0;
1190 }
1191
1192
1193 static int unix_select(struct socket *sock, int sel_type, select_table *wait)
1194 {
1195 return datagram_select(sock->data,sel_type,wait);
1196 }
1197
1198 static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1199 {
1200 unix_socket *sk=sock->data;
1201 int err;
1202 long amount=0;
1203
1204 switch(cmd)
1205 {
1206
1207 case TIOCOUTQ:
1208 err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(unsigned long));
1209 if(err)
1210 return err;
1211 amount=sk->sndbuf-sk->wmem_alloc;
1212 if(amount<0)
1213 amount=0;
1214 put_fs_long(amount,(unsigned long *)arg);
1215 return 0;
1216 case TIOCINQ:
1217 {
1218 struct sk_buff *skb;
1219 if(sk->state==TCP_LISTEN)
1220 return -EINVAL;
1221
1222 if((skb=skb_peek(&sk->receive_queue))!=NULL)
1223 amount=skb->len;
1224 err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(unsigned long));
1225 put_fs_long(amount,(unsigned long *)arg);
1226 return 0;
1227 }
1228
1229 default:
1230 return -EINVAL;
1231 }
1232
1233 return(0);
1234 }
1235
1236 #ifdef CONFIG_PROC_FS
1237 static int unix_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
1238 {
1239 off_t pos=0;
1240 off_t begin=0;
1241 int len=0;
1242 unix_socket *s=unix_socket_list;
1243
1244 len+= sprintf(buffer,"Num RefCount Protocol Flags Type St "
1245 "Inode Path\n");
1246
1247 while(s!=NULL)
1248 {
1249 len+=sprintf(buffer+len,"%p: %08X %08X %08lX %04X %02X %5ld",
1250 s,
1251 s->protinfo.af_unix.locks,
1252 0,
1253 s->socket->flags,
1254 s->socket->type,
1255 s->socket->state,
1256 s->socket->inode ? s->socket->inode->i_ino : 0);
1257 if(s->protinfo.af_unix.name!=NULL)
1258 len+=sprintf(buffer+len, " %s\n", s->protinfo.af_unix.name);
1259 else
1260 buffer[len++]='\n';
1261
1262 pos=begin+len;
1263 if(pos<offset)
1264 {
1265 len=0;
1266 begin=pos;
1267 }
1268 if(pos>offset+length)
1269 break;
1270 s=s->next;
1271 }
1272 *start=buffer+(offset-begin);
1273 len-=(offset-begin);
1274 if(len>length)
1275 len=length;
1276 return len;
1277 }
1278 #endif
1279
1280 struct proto_ops unix_proto_ops = {
1281 AF_UNIX,
1282
1283 unix_create,
1284 unix_dup,
1285 unix_release,
1286 unix_bind,
1287 unix_connect,
1288 unix_socketpair,
1289 unix_accept,
1290 unix_getname,
1291 unix_select,
1292 unix_ioctl,
1293 unix_listen,
1294 unix_shutdown,
1295 unix_setsockopt,
1296 unix_getsockopt,
1297 unix_fcntl,
1298 unix_sendmsg,
1299 unix_recvmsg
1300 };
1301
1302
1303 void unix_proto_init(struct net_proto *pro)
1304 {
1305 printk("NET3: Unix domain sockets 0.12 for Linux NET3.033.\n");
1306 sock_register(unix_proto_ops.family, &unix_proto_ops);
1307 #ifdef CONFIG_PROC_FS
1308 proc_net_register(&(struct proc_dir_entry) {
1309 PROC_NET_UNIX, 4, "unix",
1310 S_IFREG | S_IRUGO, 1, 0, 0,
1311 0, &proc_net_inode_operations,
1312 unix_get_info
1313 });
1314 #endif
1315 }
1316
1317
1318
1319
1320