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