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