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