This source file includes following definitions.
- move_addr_to_kernel
- move_addr_to_user
- get_fd
- socki_lookup
- sockfd_lookup
- sock_alloc
- sock_release_peer
- sock_release
- sock_lseek
- sock_read
- sock_write
- sock_ioctl
- sock_select
- sock_close
- sock_fasync
- sock_wake_async
- sys_socket
- sys_socketpair
- sys_bind
- sys_listen
- sys_accept
- sys_connect
- sys_getsockname
- sys_getpeername
- sys_send
- sys_sendto
- sys_recv
- sys_recvfrom
- sys_setsockopt
- sys_getsockopt
- sys_shutdown
- sys_sendmsg
- sys_recvmsg
- sock_fcntl
- sys_socketcall
- sock_register
- sock_unregister
- proto_init
- sock_init
- socket_get_info
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
48
49
50
51
52 #include <linux/config.h>
53 #include <linux/signal.h>
54 #include <linux/errno.h>
55 #include <linux/sched.h>
56 #include <linux/mm.h>
57 #include <linux/kernel.h>
58 #include <linux/major.h>
59 #include <linux/stat.h>
60 #include <linux/socket.h>
61 #include <linux/fcntl.h>
62 #include <linux/net.h>
63 #include <linux/interrupt.h>
64 #include <linux/netdevice.h>
65 #include <linux/proc_fs.h>
66 #include <linux/firewall.h>
67
68 #include <net/netlink.h>
69
70 #include <asm/system.h>
71 #include <asm/segment.h>
72
73 static int sock_lseek(struct inode *inode, struct file *file, off_t offset,
74 int whence);
75 static int sock_read(struct inode *inode, struct file *file, char *buf,
76 int size);
77 static int sock_write(struct inode *inode, struct file *file, const char *buf,
78 int size);
79
80 static void sock_close(struct inode *inode, struct file *file);
81 static int sock_select(struct inode *inode, struct file *file, int which, select_table *seltable);
82 static int sock_ioctl(struct inode *inode, struct file *file,
83 unsigned int cmd, unsigned long arg);
84 static int sock_fasync(struct inode *inode, struct file *filp, int on);
85
86
87
88
89
90
91
92 static struct file_operations socket_file_ops = {
93 sock_lseek,
94 sock_read,
95 sock_write,
96 NULL,
97 sock_select,
98 sock_ioctl,
99 NULL,
100 NULL,
101 sock_close,
102 NULL,
103 sock_fasync
104 };
105
106
107
108
109 static struct proto_ops *pops[NPROTO];
110
111
112
113 static int sockets_in_use = 0;
114
115
116
117
118
119
120 #define MAX_SOCK_ADDR 128
121
122 int move_addr_to_kernel(void *uaddr, int ulen, void *kaddr)
123 {
124 int err;
125 if(ulen<0||ulen>MAX_SOCK_ADDR)
126 return -EINVAL;
127 if(ulen==0)
128 return 0;
129 if((err=verify_area(VERIFY_READ,uaddr,ulen))<0)
130 return err;
131 memcpy_fromfs(kaddr,uaddr,ulen);
132 return 0;
133 }
134
135 int move_addr_to_user(void *kaddr, int klen, void *uaddr, int *ulen)
136 {
137 int err;
138 int len;
139
140
141 if((err=verify_area(VERIFY_WRITE,ulen,sizeof(*ulen)))<0)
142 return err;
143 len=get_user(ulen);
144 if(len>klen)
145 len=klen;
146 if(len<0 || len> MAX_SOCK_ADDR)
147 return -EINVAL;
148 if(len)
149 {
150 if((err=verify_area(VERIFY_WRITE,uaddr,len))<0)
151 return err;
152 memcpy_tofs(uaddr,kaddr,len);
153 }
154 put_user(len,ulen);
155 return 0;
156 }
157
158
159
160
161
162 static int get_fd(struct inode *inode)
163 {
164 int fd;
165 struct file *file;
166
167
168
169
170
171 file = get_empty_filp();
172 if (!file)
173 return(-1);
174
175 for (fd = 0; fd < NR_OPEN; ++fd)
176 if (!current->files->fd[fd])
177 break;
178 if (fd == NR_OPEN)
179 {
180 file->f_count = 0;
181 return(-1);
182 }
183
184 FD_CLR(fd, ¤t->files->close_on_exec);
185 current->files->fd[fd] = file;
186 file->f_op = &socket_file_ops;
187 file->f_mode = 3;
188 file->f_flags = O_RDWR;
189 file->f_count = 1;
190 file->f_inode = inode;
191 if (inode)
192 inode->i_count++;
193 file->f_pos = 0;
194 return(fd);
195 }
196
197
198
199
200
201
202
203
204
205 __inline struct socket *socki_lookup(struct inode *inode)
206 {
207 return &inode->u.socket_i;
208 }
209
210
211
212
213
214 extern __inline struct socket *sockfd_lookup(int fd, struct file **pfile)
215 {
216 struct file *file;
217 struct inode *inode;
218
219 if (fd < 0 || fd >= NR_OPEN || !(file = current->files->fd[fd]))
220 return NULL;
221
222 inode = file->f_inode;
223 if (!inode || !inode->i_sock)
224 return NULL;
225
226 if (pfile)
227 *pfile = file;
228
229 return socki_lookup(inode);
230 }
231
232
233
234
235
236 struct socket *sock_alloc(void)
237 {
238 struct inode * inode;
239 struct socket * sock;
240
241 inode = get_empty_inode();
242 if (!inode)
243 return NULL;
244
245 inode->i_mode = S_IFSOCK;
246 inode->i_sock = 1;
247 inode->i_uid = current->uid;
248 inode->i_gid = current->gid;
249
250 sock = &inode->u.socket_i;
251 sock->state = SS_UNCONNECTED;
252 sock->flags = 0;
253 sock->ops = NULL;
254 sock->data = NULL;
255 sock->conn = NULL;
256 sock->iconn = NULL;
257 sock->next = NULL;
258 sock->wait = &inode->i_wait;
259 sock->inode = inode;
260 sock->fasync_list = NULL;
261 sockets_in_use++;
262 return sock;
263 }
264
265
266
267
268
269 static inline void sock_release_peer(struct socket *peer)
270 {
271 peer->state = SS_DISCONNECTING;
272 wake_up_interruptible(peer->wait);
273 sock_wake_async(peer, 1);
274 }
275
276 void sock_release(struct socket *sock)
277 {
278 int oldstate;
279 struct socket *peersock, *nextsock;
280
281 if ((oldstate = sock->state) != SS_UNCONNECTED)
282 sock->state = SS_DISCONNECTING;
283
284
285
286
287
288 for (peersock = sock->iconn; peersock; peersock = nextsock)
289 {
290 nextsock = peersock->next;
291 sock_release_peer(peersock);
292 }
293
294
295
296
297
298
299 peersock = (oldstate == SS_CONNECTED) ? sock->conn : NULL;
300 if (sock->ops)
301 sock->ops->release(sock, peersock);
302 if (peersock)
303 sock_release_peer(peersock);
304 --sockets_in_use;
305 iput(SOCK_INODE(sock));
306 }
307
308
309
310
311
312 static int sock_lseek(struct inode *inode, struct file *file, off_t offset, int whence)
313 {
314 return(-ESPIPE);
315 }
316
317
318
319
320
321
322 static int sock_read(struct inode *inode, struct file *file, char *ubuf, int size)
323 {
324 struct socket *sock;
325 int err;
326 struct iovec iov;
327 struct msghdr msg;
328
329 sock = socki_lookup(inode);
330 if (sock->flags & SO_ACCEPTCON)
331 return(-EINVAL);
332
333 if(size<0)
334 return -EINVAL;
335 if(size==0)
336 return 0;
337 if ((err=verify_area(VERIFY_WRITE,ubuf,size))<0)
338 return err;
339 msg.msg_name=NULL;
340 msg.msg_iov=&iov;
341 msg.msg_iovlen=1;
342 msg.msg_accrights=NULL;
343 iov.iov_base=ubuf;
344 iov.iov_len=size;
345
346 return(sock->ops->recvmsg(sock, &msg, size,(file->f_flags & O_NONBLOCK), 0,&msg.msg_namelen));
347 }
348
349
350
351
352
353
354 static int sock_write(struct inode *inode, struct file *file, const char *ubuf, int size)
355 {
356 struct socket *sock;
357 int err;
358 struct msghdr msg;
359 struct iovec iov;
360
361 sock = socki_lookup(inode);
362
363 if (sock->flags & SO_ACCEPTCON)
364 return(-EINVAL);
365
366 if(size<0)
367 return -EINVAL;
368 if(size==0)
369 return 0;
370
371 if ((err=verify_area(VERIFY_READ,ubuf,size))<0)
372 return err;
373
374 msg.msg_name=NULL;
375 msg.msg_iov=&iov;
376 msg.msg_iovlen=1;
377 msg.msg_accrights=NULL;
378 iov.iov_base=(void *)ubuf;
379 iov.iov_len=size;
380
381 return(sock->ops->sendmsg(sock, &msg, size,(file->f_flags & O_NONBLOCK),0));
382 }
383
384
385
386
387
388
389 int sock_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
390 unsigned long arg)
391 {
392 struct socket *sock;
393 sock = socki_lookup(inode);
394 return(sock->ops->ioctl(sock, cmd, arg));
395 }
396
397
398 static int sock_select(struct inode *inode, struct file *file, int sel_type, select_table * wait)
399 {
400 struct socket *sock;
401
402 sock = socki_lookup(inode);
403
404
405
406
407
408 if (sock->ops->select)
409 return(sock->ops->select(sock, sel_type, wait));
410 return(0);
411 }
412
413
414 void sock_close(struct inode *inode, struct file *filp)
415 {
416
417
418
419
420 if (!inode)
421 return;
422 sock_fasync(inode, filp, 0);
423 sock_release(socki_lookup(inode));
424 }
425
426
427
428
429
430 static int sock_fasync(struct inode *inode, struct file *filp, int on)
431 {
432 struct fasync_struct *fa, *fna=NULL, **prev;
433 struct socket *sock;
434 unsigned long flags;
435
436 if (on)
437 {
438 fna=(struct fasync_struct *)kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
439 if(fna==NULL)
440 return -ENOMEM;
441 }
442
443 sock = socki_lookup(inode);
444
445 prev=&(sock->fasync_list);
446
447 save_flags(flags);
448 cli();
449
450 for(fa=*prev; fa!=NULL; prev=&fa->fa_next,fa=*prev)
451 if(fa->fa_file==filp)
452 break;
453
454 if(on)
455 {
456 if(fa!=NULL)
457 {
458 kfree_s(fna,sizeof(struct fasync_struct));
459 restore_flags(flags);
460 return 0;
461 }
462 fna->fa_file=filp;
463 fna->magic=FASYNC_MAGIC;
464 fna->fa_next=sock->fasync_list;
465 sock->fasync_list=fna;
466 }
467 else
468 {
469 if(fa!=NULL)
470 {
471 *prev=fa->fa_next;
472 kfree_s(fa,sizeof(struct fasync_struct));
473 }
474 }
475 restore_flags(flags);
476 return 0;
477 }
478
479 int sock_wake_async(struct socket *sock, int how)
480 {
481 if (!sock || !sock->fasync_list)
482 return -1;
483 switch (how)
484 {
485 case 0:
486 kill_fasync(sock->fasync_list, SIGIO);
487 break;
488 case 1:
489 if (!(sock->flags & SO_WAITDATA))
490 kill_fasync(sock->fasync_list, SIGIO);
491 break;
492 case 2:
493 if (sock->flags & SO_NOSPACE)
494 {
495 kill_fasync(sock->fasync_list, SIGIO);
496 sock->flags &= ~SO_NOSPACE;
497 }
498 break;
499 }
500 return 0;
501 }
502
503
504
505
506
507
508
509 asmlinkage int sys_socket(int family, int type, int protocol)
510 {
511 int i, fd;
512 struct socket *sock;
513 struct proto_ops *ops;
514
515
516 for (i = 0; i < NPROTO; ++i)
517 {
518 if (pops[i] == NULL) continue;
519 if (pops[i]->family == family)
520 break;
521 }
522
523 if (i == NPROTO)
524 {
525 return -EINVAL;
526 }
527
528 ops = pops[i];
529
530
531
532
533
534
535
536 if ((type != SOCK_STREAM && type != SOCK_DGRAM &&
537 type != SOCK_SEQPACKET && type != SOCK_RAW &&
538 type != SOCK_PACKET) || protocol < 0)
539 return(-EINVAL);
540
541
542
543
544
545
546
547 if (!(sock = sock_alloc()))
548 {
549 printk("NET: sys_socket: no more sockets\n");
550 return(-ENOSR);
551
552 }
553
554 sock->type = type;
555 sock->ops = ops;
556 if ((i = sock->ops->create(sock, protocol)) < 0)
557 {
558 sock_release(sock);
559 return(i);
560 }
561
562 if ((fd = get_fd(SOCK_INODE(sock))) < 0)
563 {
564 sock_release(sock);
565 return(-EINVAL);
566 }
567
568 return(fd);
569 }
570
571
572
573
574
575 asmlinkage int sys_socketpair(int family, int type, int protocol, int usockvec[2])
576 {
577 int fd1, fd2, i;
578 struct socket *sock1, *sock2;
579 int er;
580
581
582
583
584
585
586 if ((fd1 = sys_socket(family, type, protocol)) < 0)
587 return(fd1);
588 sock1 = sockfd_lookup(fd1, NULL);
589 if (!sock1->ops->socketpair)
590 {
591 sys_close(fd1);
592 return(-EINVAL);
593 }
594
595
596
597
598
599 if ((fd2 = sys_socket(family, type, protocol)) < 0)
600 {
601 sys_close(fd1);
602 return(-EINVAL);
603 }
604
605 sock2 = sockfd_lookup(fd2, NULL);
606 if ((i = sock1->ops->socketpair(sock1, sock2)) < 0)
607 {
608 sys_close(fd1);
609 sys_close(fd2);
610 return(i);
611 }
612
613 sock1->conn = sock2;
614 sock2->conn = sock1;
615 sock1->state = SS_CONNECTED;
616 sock2->state = SS_CONNECTED;
617
618 er=verify_area(VERIFY_WRITE, usockvec, sizeof(usockvec));
619 if(er)
620 {
621 sys_close(fd1);
622 sys_close(fd2);
623 return er;
624 }
625 put_user(fd1, &usockvec[0]);
626 put_user(fd2, &usockvec[1]);
627
628 return(0);
629 }
630
631
632
633
634
635
636
637
638
639
640 asmlinkage int sys_bind(int fd, struct sockaddr *umyaddr, int addrlen)
641 {
642 struct socket *sock;
643 int i;
644 char address[MAX_SOCK_ADDR];
645 int err;
646
647 if (fd < 0 || fd >= NR_OPEN || current->files->fd[fd] == NULL)
648 return(-EBADF);
649
650 if (!(sock = sockfd_lookup(fd, NULL)))
651 return(-ENOTSOCK);
652
653 if((err=move_addr_to_kernel(umyaddr,addrlen,address))<0)
654 return err;
655
656 if ((i = sock->ops->bind(sock, (struct sockaddr *)address, addrlen)) < 0)
657 {
658 return(i);
659 }
660 return(0);
661 }
662
663
664
665
666
667
668
669
670 asmlinkage int sys_listen(int fd, int backlog)
671 {
672 struct socket *sock;
673 int err=-EOPNOTSUPP;
674
675 if (fd < 0 || fd >= NR_OPEN || current->files->fd[fd] == NULL)
676 return(-EBADF);
677 if (!(sock = sockfd_lookup(fd, NULL)))
678 return(-ENOTSOCK);
679
680 if (sock->state != SS_UNCONNECTED)
681 return(-EINVAL);
682
683 if (sock->ops && sock->ops->listen)
684 {
685 err=sock->ops->listen(sock, backlog);
686 if(!err)
687 sock->flags |= SO_ACCEPTCON;
688 }
689 return(err);
690 }
691
692
693
694
695
696
697
698
699
700
701 asmlinkage int sys_accept(int fd, struct sockaddr *upeer_sockaddr, int *upeer_addrlen)
702 {
703 struct file *file;
704 struct socket *sock, *newsock;
705 int i;
706 char address[MAX_SOCK_ADDR];
707 int len;
708
709 if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
710 return(-EBADF);
711 if (!(sock = sockfd_lookup(fd, &file)))
712 return(-ENOTSOCK);
713 if (sock->state != SS_UNCONNECTED)
714 {
715 return(-EINVAL);
716 }
717 if (!(sock->flags & SO_ACCEPTCON))
718 {
719 return(-EINVAL);
720 }
721
722 if (!(newsock = sock_alloc()))
723 {
724 printk("NET: sock_accept: no more sockets\n");
725 return(-ENOSR);
726
727 }
728 newsock->type = sock->type;
729 newsock->ops = sock->ops;
730 if ((i = sock->ops->dup(newsock, sock)) < 0)
731 {
732 sock_release(newsock);
733 return(i);
734 }
735
736 i = newsock->ops->accept(sock, newsock, file->f_flags);
737 if ( i < 0)
738 {
739 sock_release(newsock);
740 return(i);
741 }
742
743 if ((fd = get_fd(SOCK_INODE(newsock))) < 0)
744 {
745 sock_release(newsock);
746 return(-EINVAL);
747 }
748
749 if (upeer_sockaddr)
750 {
751 newsock->ops->getname(newsock, (struct sockaddr *)address, &len, 1);
752 move_addr_to_user(address,len, upeer_sockaddr, upeer_addrlen);
753 }
754 return(fd);
755 }
756
757
758
759
760
761
762
763 asmlinkage int sys_connect(int fd, struct sockaddr *uservaddr, int addrlen)
764 {
765 struct socket *sock;
766 struct file *file;
767 int i;
768 char address[MAX_SOCK_ADDR];
769 int err;
770
771 if (fd < 0 || fd >= NR_OPEN || (file=current->files->fd[fd]) == NULL)
772 return(-EBADF);
773 if (!(sock = sockfd_lookup(fd, &file)))
774 return(-ENOTSOCK);
775
776 if((err=move_addr_to_kernel(uservaddr,addrlen,address))<0)
777 return err;
778
779 switch(sock->state)
780 {
781 case SS_UNCONNECTED:
782
783 break;
784 case SS_CONNECTED:
785
786 if(sock->type == SOCK_DGRAM)
787 break;
788 return -EISCONN;
789 case SS_CONNECTING:
790
791
792
793
794
795
796
797 break;
798 default:
799 return(-EINVAL);
800 }
801 i = sock->ops->connect(sock, (struct sockaddr *)address, addrlen, file->f_flags);
802 if (i < 0)
803 {
804 return(i);
805 }
806 return(0);
807 }
808
809
810
811
812
813
814 asmlinkage int sys_getsockname(int fd, struct sockaddr *usockaddr, int *usockaddr_len)
815 {
816 struct socket *sock;
817 char address[MAX_SOCK_ADDR];
818 int len;
819 int err;
820
821 if (fd < 0 || fd >= NR_OPEN || current->files->fd[fd] == NULL)
822 return(-EBADF);
823 if (!(sock = sockfd_lookup(fd, NULL)))
824 return(-ENOTSOCK);
825
826 err=sock->ops->getname(sock, (struct sockaddr *)address, &len, 0);
827 if(err)
828 return err;
829 if((err=move_addr_to_user(address,len, usockaddr, usockaddr_len))<0)
830 return err;
831 return 0;
832 }
833
834
835
836
837
838
839 asmlinkage int sys_getpeername(int fd, struct sockaddr *usockaddr, int *usockaddr_len)
840 {
841 struct socket *sock;
842 char address[MAX_SOCK_ADDR];
843 int len;
844 int err;
845
846 if (fd < 0 || fd >= NR_OPEN || current->files->fd[fd] == NULL)
847 return(-EBADF);
848 if (!(sock = sockfd_lookup(fd, NULL)))
849 return(-ENOTSOCK);
850
851 err=sock->ops->getname(sock, (struct sockaddr *)address, &len, 1);
852 if(err)
853 return err;
854 if((err=move_addr_to_user(address,len, usockaddr, usockaddr_len))<0)
855 return err;
856 return 0;
857 }
858
859
860
861
862
863
864 asmlinkage int sys_send(int fd, void * buff, int len, unsigned flags)
865 {
866 struct socket *sock;
867 struct file *file;
868 int err;
869 struct msghdr msg;
870 struct iovec iov;
871
872 if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
873 return(-EBADF);
874 if (!(sock = sockfd_lookup(fd, NULL)))
875 return(-ENOTSOCK);
876
877 if(len<0)
878 return -EINVAL;
879 err=verify_area(VERIFY_READ, buff, len);
880 if(err)
881 return err;
882
883 iov.iov_base=buff;
884 iov.iov_len=len;
885 msg.msg_name=NULL;
886 msg.msg_iov=&iov;
887 msg.msg_iovlen=1;
888 msg.msg_accrights=NULL;
889 return(sock->ops->sendmsg(sock, &msg, len, (file->f_flags & O_NONBLOCK), flags));
890 }
891
892
893
894
895
896
897
898 asmlinkage int sys_sendto(int fd, void * buff, int len, unsigned flags,
899 struct sockaddr *addr, int addr_len)
900 {
901 struct socket *sock;
902 struct file *file;
903 char address[MAX_SOCK_ADDR];
904 int err;
905 struct msghdr msg;
906 struct iovec iov;
907
908 if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
909 return(-EBADF);
910 if (!(sock = sockfd_lookup(fd, NULL)))
911 return(-ENOTSOCK);
912
913 if(len<0)
914 return -EINVAL;
915 err=verify_area(VERIFY_READ,buff,len);
916 if(err)
917 return err;
918
919 if((err=move_addr_to_kernel(addr,addr_len,address))<0)
920 return err;
921
922 iov.iov_base=buff;
923 iov.iov_len=len;
924 msg.msg_name=address;
925 msg.msg_namelen=addr_len;
926 msg.msg_iov=&iov;
927 msg.msg_iovlen=1;
928 msg.msg_accrights=NULL;
929 return(sock->ops->sendmsg(sock, &msg, len, (file->f_flags & O_NONBLOCK),
930 flags));
931 }
932
933
934
935
936
937
938 asmlinkage int sys_recv(int fd, void * ubuf, int size, unsigned flags)
939 {
940 struct iovec iov;
941 struct msghdr msg;
942 struct socket *sock;
943 struct file *file;
944 int err;
945
946 if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
947 return(-EBADF);
948
949 if (!(sock = sockfd_lookup(fd, NULL)))
950 return(-ENOTSOCK);
951
952 if(size<0)
953 return -EINVAL;
954 if(size==0)
955 return 0;
956 err=verify_area(VERIFY_WRITE, ubuf, size);
957 if(err)
958 return err;
959
960 msg.msg_name=NULL;
961 msg.msg_iov=&iov;
962 msg.msg_iovlen=1;
963 msg.msg_accrights=NULL;
964 iov.iov_base=ubuf;
965 iov.iov_len=size;
966
967 return(sock->ops->recvmsg(sock, &msg, size,(file->f_flags & O_NONBLOCK), flags,&msg.msg_namelen));
968 }
969
970
971
972
973
974
975
976 asmlinkage int sys_recvfrom(int fd, void * ubuf, int size, unsigned flags,
977 struct sockaddr *addr, int *addr_len)
978 {
979 struct socket *sock;
980 struct file *file;
981 struct iovec iov;
982 struct msghdr msg;
983 char address[MAX_SOCK_ADDR];
984 int err;
985 int alen;
986 if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
987 return(-EBADF);
988 if (!(sock = sockfd_lookup(fd, NULL)))
989 return(-ENOTSOCK);
990 if(size<0)
991 return -EINVAL;
992 if(size==0)
993 return 0;
994
995 err=verify_area(VERIFY_WRITE,ubuf,size);
996 if(err)
997 return err;
998
999 msg.msg_accrights=NULL;
1000 msg.msg_iovlen=0;
1001 msg.msg_iov=&iov;
1002 iov.iov_len=size;
1003 iov.iov_base=ubuf;
1004 msg.msg_name=address;
1005 msg.msg_namelen=MAX_SOCK_ADDR;
1006 size=sock->ops->recvmsg(sock, &msg, size, (file->f_flags & O_NONBLOCK),
1007 flags, &alen);
1008
1009 if(size<0)
1010 return size;
1011 if(addr!=NULL && (err=move_addr_to_user(address,alen, addr, addr_len))<0)
1012 return err;
1013
1014 return size;
1015 }
1016
1017
1018
1019
1020
1021
1022 asmlinkage int sys_setsockopt(int fd, int level, int optname, char *optval, int optlen)
1023 {
1024 struct socket *sock;
1025 struct file *file;
1026
1027 if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1028 return(-EBADF);
1029 if (!(sock = sockfd_lookup(fd, NULL)))
1030 return(-ENOTSOCK);
1031
1032 return(sock->ops->setsockopt(sock, level, optname, optval, optlen));
1033 }
1034
1035
1036
1037
1038
1039
1040 asmlinkage int sys_getsockopt(int fd, int level, int optname, char *optval, int *optlen)
1041 {
1042 struct socket *sock;
1043 struct file *file;
1044
1045 if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1046 return(-EBADF);
1047 if (!(sock = sockfd_lookup(fd, NULL)))
1048 return(-ENOTSOCK);
1049
1050 if (!sock->ops->getsockopt)
1051 return(0);
1052 return(sock->ops->getsockopt(sock, level, optname, optval, optlen));
1053 }
1054
1055
1056
1057
1058
1059
1060 asmlinkage int sys_shutdown(int fd, int how)
1061 {
1062 struct socket *sock;
1063 struct file *file;
1064
1065 if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1066 return(-EBADF);
1067 if (!(sock = sockfd_lookup(fd, NULL)))
1068 return(-ENOTSOCK);
1069
1070 return(sock->ops->shutdown(sock, how));
1071 }
1072
1073
1074
1075
1076
1077 asmlinkage int sys_sendmsg(int fd, struct msghdr *msg, unsigned int flags)
1078 {
1079 struct socket *sock;
1080 struct file *file;
1081 char address[MAX_SOCK_ADDR];
1082 struct iovec iov[MAX_IOVEC];
1083 struct msghdr msg_sys;
1084 int err;
1085 int total_len;
1086
1087 if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1088 return(-EBADF);
1089 if (!(sock = sockfd_lookup(fd, NULL)))
1090 return(-ENOTSOCK);
1091
1092 if(sock->ops->sendmsg==NULL)
1093 return -EOPNOTSUPP;
1094
1095
1096 err=verify_area(VERIFY_READ, msg,sizeof(struct msghdr));
1097 if(err)
1098 return err;
1099
1100 memcpy_fromfs(&msg_sys,msg,sizeof(struct msghdr));
1101
1102
1103 if(msg_sys.msg_iovlen>MAX_IOVEC)
1104 return -EINVAL;
1105
1106 err=verify_iovec(&msg_sys,iov,address, VERIFY_READ);
1107 if(err<0)
1108 return err;
1109 total_len=err;
1110
1111 return sock->ops->sendmsg(sock, &msg_sys, total_len, (file->f_flags&O_NONBLOCK), flags);
1112 }
1113
1114
1115
1116
1117
1118 asmlinkage int sys_recvmsg(int fd, struct msghdr *msg, unsigned int flags)
1119 {
1120 struct socket *sock;
1121 struct file *file;
1122 char address[MAX_SOCK_ADDR];
1123 struct iovec iov[MAX_IOVEC];
1124 struct msghdr msg_sys;
1125 int err;
1126 int total_len;
1127 int addr_len;
1128 int len;
1129
1130 if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1131 return(-EBADF);
1132 if (!(sock = sockfd_lookup(fd, NULL)))
1133 return(-ENOTSOCK);
1134
1135 err=verify_area(VERIFY_READ, msg,sizeof(struct msghdr));
1136 if(err)
1137 return err;
1138 memcpy_fromfs(&msg_sys,msg,sizeof(struct msghdr));
1139 if(msg_sys.msg_iovlen>MAX_IOVEC)
1140 return -EINVAL;
1141 err=verify_iovec(&msg_sys,iov,address, VERIFY_WRITE);
1142 if(err<0)
1143 return err;
1144 total_len=err;
1145
1146 if(sock->ops->recvmsg==NULL)
1147 return -EOPNOTSUPP;
1148 len=sock->ops->recvmsg(sock, &msg_sys, total_len, (file->f_flags&O_NONBLOCK), flags, &addr_len);
1149 if(len<0)
1150 return len;
1151
1152
1153
1154 if(msg_sys.msg_name!=NULL && (err=move_addr_to_user(address,addr_len, msg_sys.msg_name, &msg_sys.msg_namelen))<0)
1155 return err;
1156 return len;
1157 }
1158
1159
1160
1161
1162
1163
1164 int sock_fcntl(struct file *filp, unsigned int cmd, unsigned long arg)
1165 {
1166 struct socket *sock;
1167
1168 sock = socki_lookup (filp->f_inode);
1169 if (sock != NULL && sock->ops != NULL && sock->ops->fcntl != NULL)
1170 return(sock->ops->fcntl(sock, cmd, arg));
1171 return(-EINVAL);
1172 }
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187 asmlinkage int sys_socketcall(int call, unsigned long *args)
1188 {
1189 int er;
1190 unsigned char nargs[18]={0,3,3,3,2,3,3,3,
1191 4,4,4,6,6,2,5,5,3,3};
1192
1193 unsigned long a0,a1;
1194
1195 if(call<1||call>SYS_RECVMSG)
1196 return -EINVAL;
1197
1198 er=verify_area(VERIFY_READ, args, nargs[call] * sizeof(unsigned long));
1199 if(er)
1200 return er;
1201
1202 a0=get_user(args);
1203 a1=get_user(args+1);
1204
1205
1206 switch(call)
1207 {
1208 case SYS_SOCKET:
1209 return(sys_socket(a0,a1,get_user(args+2)));
1210 case SYS_BIND:
1211 return(sys_bind(a0,(struct sockaddr *)a1,
1212 get_user(args+2)));
1213 case SYS_CONNECT:
1214 return(sys_connect(a0, (struct sockaddr *)a1,
1215 get_user(args+2)));
1216 case SYS_LISTEN:
1217 return(sys_listen(a0,a1));
1218 case SYS_ACCEPT:
1219 return(sys_accept(a0,(struct sockaddr *)a1,
1220 (int *)get_user(args+2)));
1221 case SYS_GETSOCKNAME:
1222 return(sys_getsockname(a0,(struct sockaddr *)a1,
1223 (int *)get_user(args+2)));
1224 case SYS_GETPEERNAME:
1225 return(sys_getpeername(a0, (struct sockaddr *)a1,
1226 (int *)get_user(args+2)));
1227 case SYS_SOCKETPAIR:
1228 return(sys_socketpair(a0,a1,
1229 get_user(args+2),
1230 (int *)get_user(args+3)));
1231 case SYS_SEND:
1232 return(sys_send(a0,
1233 (void *)a1,
1234 get_user(args+2),
1235 get_user(args+3)));
1236 case SYS_SENDTO:
1237 return(sys_sendto(a0,(void *)a1,
1238 get_user(args+2),
1239 get_user(args+3),
1240 (struct sockaddr *)get_user(args+4),
1241 get_user(args+5)));
1242 case SYS_RECV:
1243 return(sys_recv(a0,
1244 (void *)a1,
1245 get_user(args+2),
1246 get_user(args+3)));
1247 case SYS_RECVFROM:
1248 return(sys_recvfrom(a0,
1249 (void *)a1,
1250 get_user(args+2),
1251 get_user(args+3),
1252 (struct sockaddr *)get_user(args+4),
1253 (int *)get_user(args+5)));
1254 case SYS_SHUTDOWN:
1255 return(sys_shutdown(a0,a1));
1256 case SYS_SETSOCKOPT:
1257 return(sys_setsockopt(a0,
1258 a1,
1259 get_user(args+2),
1260 (char *)get_user(args+3),
1261 get_user(args+4)));
1262 case SYS_GETSOCKOPT:
1263 return(sys_getsockopt(a0,
1264 a1,
1265 get_user(args+2),
1266 (char *)get_user(args+3),
1267 (int *)get_user(args+4)));
1268 case SYS_SENDMSG:
1269 return sys_sendmsg(a0,
1270 (struct msghdr *) a1,
1271 get_user(args+2));
1272 case SYS_RECVMSG:
1273 return sys_recvmsg(a0,
1274 (struct msghdr *) a1,
1275 get_user(args+2));
1276 }
1277 return -EINVAL;
1278 }
1279
1280
1281
1282
1283
1284
1285
1286 int sock_register(int family, struct proto_ops *ops)
1287 {
1288 int i;
1289
1290 cli();
1291 for(i = 0; i < NPROTO; i++)
1292 {
1293 if (pops[i] != NULL)
1294 continue;
1295 pops[i] = ops;
1296 pops[i]->family = family;
1297 sti();
1298 return(i);
1299 }
1300 sti();
1301 return(-ENOMEM);
1302 }
1303
1304
1305
1306
1307
1308
1309
1310 int sock_unregister(int family)
1311 {
1312 int i;
1313
1314 cli();
1315 for(i = 0; i < NPROTO; i++)
1316 {
1317 if (pops[i] == NULL)
1318 continue;
1319 if (pops[i]->family == family)
1320 {
1321 pops[i]=NULL;
1322 sti();
1323 return(i);
1324 }
1325 }
1326 sti();
1327 return(-ENOENT);
1328 }
1329
1330 void proto_init(void)
1331 {
1332 extern struct net_proto protocols[];
1333 struct net_proto *pro;
1334
1335
1336 pro = protocols;
1337 while (pro->name != NULL)
1338 {
1339 (*pro->init_func)(pro);
1340 pro++;
1341 }
1342
1343 }
1344
1345
1346 void sock_init(void)
1347 {
1348 int i;
1349
1350 printk("Swansea University Computer Society NET3.033 for Linux 1.3.50\n");
1351
1352
1353
1354
1355
1356 for (i = 0; i < NPROTO; ++i) pops[i] = NULL;
1357
1358
1359
1360
1361
1362 #ifdef CONFIG_NETLINK
1363 init_netlink();
1364 #endif
1365
1366
1367
1368
1369 #if defined(CONFIG_RTNETLINK)
1370 netlink_attach(NETLINK_ROUTE, netlink_donothing);
1371 #endif
1372
1373
1374
1375
1376
1377 #ifdef CONFIG_FIREWALL
1378 fwchain_init();
1379 #endif
1380
1381
1382
1383
1384
1385 proto_init();
1386 }
1387
1388 int socket_get_info(char *buffer, char **start, off_t offset, int length)
1389 {
1390 int len = sprintf(buffer, "sockets: used %d\n", sockets_in_use);
1391 if (offset >= len)
1392 {
1393 *start = buffer;
1394 return 0;
1395 }
1396 *start = buffer + offset;
1397 len -= offset;
1398 if (len > length)
1399 len = length;
1400 return len;
1401 }