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
674 if (fd < 0 || fd >= NR_OPEN || current->files->fd[fd] == NULL)
675 return(-EBADF);
676 if (!(sock = sockfd_lookup(fd, NULL)))
677 return(-ENOTSOCK);
678
679 if (sock->state != SS_UNCONNECTED)
680 {
681 return(-EINVAL);
682 }
683
684 if (sock->ops && sock->ops->listen)
685 sock->ops->listen(sock, backlog);
686 sock->flags |= SO_ACCEPTCON;
687 return(0);
688 }
689
690
691
692
693
694
695
696
697
698
699 asmlinkage int sys_accept(int fd, struct sockaddr *upeer_sockaddr, int *upeer_addrlen)
700 {
701 struct file *file;
702 struct socket *sock, *newsock;
703 int i;
704 char address[MAX_SOCK_ADDR];
705 int len;
706
707 if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
708 return(-EBADF);
709 if (!(sock = sockfd_lookup(fd, &file)))
710 return(-ENOTSOCK);
711 if (sock->state != SS_UNCONNECTED)
712 {
713 return(-EINVAL);
714 }
715 if (!(sock->flags & SO_ACCEPTCON))
716 {
717 return(-EINVAL);
718 }
719
720 if (!(newsock = sock_alloc()))
721 {
722 printk("NET: sock_accept: no more sockets\n");
723 return(-ENOSR);
724
725 }
726 newsock->type = sock->type;
727 newsock->ops = sock->ops;
728 if ((i = sock->ops->dup(newsock, sock)) < 0)
729 {
730 sock_release(newsock);
731 return(i);
732 }
733
734 i = newsock->ops->accept(sock, newsock, file->f_flags);
735 if ( i < 0)
736 {
737 sock_release(newsock);
738 return(i);
739 }
740
741 if ((fd = get_fd(SOCK_INODE(newsock))) < 0)
742 {
743 sock_release(newsock);
744 return(-EINVAL);
745 }
746
747 if (upeer_sockaddr)
748 {
749 newsock->ops->getname(newsock, (struct sockaddr *)address, &len, 1);
750 move_addr_to_user(address,len, upeer_sockaddr, upeer_addrlen);
751 }
752 return(fd);
753 }
754
755
756
757
758
759
760
761 asmlinkage int sys_connect(int fd, struct sockaddr *uservaddr, int addrlen)
762 {
763 struct socket *sock;
764 struct file *file;
765 int i;
766 char address[MAX_SOCK_ADDR];
767 int err;
768
769 if (fd < 0 || fd >= NR_OPEN || (file=current->files->fd[fd]) == NULL)
770 return(-EBADF);
771 if (!(sock = sockfd_lookup(fd, &file)))
772 return(-ENOTSOCK);
773
774 if((err=move_addr_to_kernel(uservaddr,addrlen,address))<0)
775 return err;
776
777 switch(sock->state)
778 {
779 case SS_UNCONNECTED:
780
781 break;
782 case SS_CONNECTED:
783
784 if(sock->type == SOCK_DGRAM)
785 break;
786 return -EISCONN;
787 case SS_CONNECTING:
788
789
790
791
792
793
794
795 break;
796 default:
797 return(-EINVAL);
798 }
799 i = sock->ops->connect(sock, (struct sockaddr *)address, addrlen, file->f_flags);
800 if (i < 0)
801 {
802 return(i);
803 }
804 return(0);
805 }
806
807
808
809
810
811
812 asmlinkage int sys_getsockname(int fd, struct sockaddr *usockaddr, int *usockaddr_len)
813 {
814 struct socket *sock;
815 char address[MAX_SOCK_ADDR];
816 int len;
817 int err;
818
819 if (fd < 0 || fd >= NR_OPEN || current->files->fd[fd] == NULL)
820 return(-EBADF);
821 if (!(sock = sockfd_lookup(fd, NULL)))
822 return(-ENOTSOCK);
823
824 err=sock->ops->getname(sock, (struct sockaddr *)address, &len, 0);
825 if(err)
826 return err;
827 if((err=move_addr_to_user(address,len, usockaddr, usockaddr_len))<0)
828 return err;
829 return 0;
830 }
831
832
833
834
835
836
837 asmlinkage int sys_getpeername(int fd, struct sockaddr *usockaddr, int *usockaddr_len)
838 {
839 struct socket *sock;
840 char address[MAX_SOCK_ADDR];
841 int len;
842 int err;
843
844 if (fd < 0 || fd >= NR_OPEN || current->files->fd[fd] == NULL)
845 return(-EBADF);
846 if (!(sock = sockfd_lookup(fd, NULL)))
847 return(-ENOTSOCK);
848
849 err=sock->ops->getname(sock, (struct sockaddr *)address, &len, 1);
850 if(err)
851 return err;
852 if((err=move_addr_to_user(address,len, usockaddr, usockaddr_len))<0)
853 return err;
854 return 0;
855 }
856
857
858
859
860
861
862 asmlinkage int sys_send(int fd, void * buff, int len, unsigned flags)
863 {
864 struct socket *sock;
865 struct file *file;
866 int err;
867 struct msghdr msg;
868 struct iovec iov;
869
870 if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
871 return(-EBADF);
872 if (!(sock = sockfd_lookup(fd, NULL)))
873 return(-ENOTSOCK);
874
875 if(len<0)
876 return -EINVAL;
877 err=verify_area(VERIFY_READ, buff, len);
878 if(err)
879 return err;
880
881 iov.iov_base=buff;
882 iov.iov_len=len;
883 msg.msg_name=NULL;
884 msg.msg_iov=&iov;
885 msg.msg_iovlen=1;
886 msg.msg_accrights=NULL;
887 return(sock->ops->sendmsg(sock, &msg, len, (file->f_flags & O_NONBLOCK), flags));
888 }
889
890
891
892
893
894
895
896 asmlinkage int sys_sendto(int fd, void * buff, int len, unsigned flags,
897 struct sockaddr *addr, int addr_len)
898 {
899 struct socket *sock;
900 struct file *file;
901 char address[MAX_SOCK_ADDR];
902 int err;
903 struct msghdr msg;
904 struct iovec iov;
905
906 if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
907 return(-EBADF);
908 if (!(sock = sockfd_lookup(fd, NULL)))
909 return(-ENOTSOCK);
910
911 if(len<0)
912 return -EINVAL;
913 err=verify_area(VERIFY_READ,buff,len);
914 if(err)
915 return err;
916
917 if((err=move_addr_to_kernel(addr,addr_len,address))<0)
918 return err;
919
920 iov.iov_base=buff;
921 iov.iov_len=len;
922 msg.msg_name=address;
923 msg.msg_namelen=addr_len;
924 msg.msg_iov=&iov;
925 msg.msg_iovlen=1;
926 msg.msg_accrights=NULL;
927 return(sock->ops->sendmsg(sock, &msg, len, (file->f_flags & O_NONBLOCK),
928 flags));
929 }
930
931
932
933
934
935
936 asmlinkage int sys_recv(int fd, void * ubuf, int size, unsigned flags)
937 {
938 struct iovec iov;
939 struct msghdr msg;
940 struct socket *sock;
941 struct file *file;
942 int err;
943
944 if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
945 return(-EBADF);
946
947 if (!(sock = sockfd_lookup(fd, NULL)))
948 return(-ENOTSOCK);
949
950 if(size<0)
951 return -EINVAL;
952 if(size==0)
953 return 0;
954 err=verify_area(VERIFY_WRITE, ubuf, size);
955 if(err)
956 return err;
957
958 msg.msg_name=NULL;
959 msg.msg_iov=&iov;
960 msg.msg_iovlen=1;
961 msg.msg_accrights=NULL;
962 iov.iov_base=ubuf;
963 iov.iov_len=size;
964
965 return(sock->ops->recvmsg(sock, &msg, size,(file->f_flags & O_NONBLOCK), flags,&msg.msg_namelen));
966 }
967
968
969
970
971
972
973
974 asmlinkage int sys_recvfrom(int fd, void * ubuf, int size, unsigned flags,
975 struct sockaddr *addr, int *addr_len)
976 {
977 struct socket *sock;
978 struct file *file;
979 struct iovec iov;
980 struct msghdr msg;
981 char address[MAX_SOCK_ADDR];
982 int err;
983 int alen;
984 if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
985 return(-EBADF);
986 if (!(sock = sockfd_lookup(fd, NULL)))
987 return(-ENOTSOCK);
988 if(size<0)
989 return -EINVAL;
990 if(size==0)
991 return 0;
992
993 err=verify_area(VERIFY_WRITE,ubuf,size);
994 if(err)
995 return err;
996
997 msg.msg_accrights=NULL;
998 msg.msg_iovlen=0;
999 msg.msg_iov=&iov;
1000 iov.iov_len=size;
1001 iov.iov_base=ubuf;
1002 msg.msg_name=address;
1003 msg.msg_namelen=MAX_SOCK_ADDR;
1004 size=sock->ops->recvmsg(sock, &msg, size, (file->f_flags & O_NONBLOCK),
1005 flags, &alen);
1006
1007 if(size<0)
1008 return size;
1009 if(addr!=NULL && (err=move_addr_to_user(address,alen, addr, addr_len))<0)
1010 return err;
1011
1012 return size;
1013 }
1014
1015
1016
1017
1018
1019
1020 asmlinkage int sys_setsockopt(int fd, int level, int optname, char *optval, int optlen)
1021 {
1022 struct socket *sock;
1023 struct file *file;
1024
1025 if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1026 return(-EBADF);
1027 if (!(sock = sockfd_lookup(fd, NULL)))
1028 return(-ENOTSOCK);
1029
1030 return(sock->ops->setsockopt(sock, level, optname, optval, optlen));
1031 }
1032
1033
1034
1035
1036
1037
1038 asmlinkage int sys_getsockopt(int fd, int level, int optname, char *optval, int *optlen)
1039 {
1040 struct socket *sock;
1041 struct file *file;
1042
1043 if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1044 return(-EBADF);
1045 if (!(sock = sockfd_lookup(fd, NULL)))
1046 return(-ENOTSOCK);
1047
1048 if (!sock->ops->getsockopt)
1049 return(0);
1050 return(sock->ops->getsockopt(sock, level, optname, optval, optlen));
1051 }
1052
1053
1054
1055
1056
1057
1058 asmlinkage int sys_shutdown(int fd, int how)
1059 {
1060 struct socket *sock;
1061 struct file *file;
1062
1063 if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1064 return(-EBADF);
1065 if (!(sock = sockfd_lookup(fd, NULL)))
1066 return(-ENOTSOCK);
1067
1068 return(sock->ops->shutdown(sock, how));
1069 }
1070
1071
1072
1073
1074
1075 asmlinkage int sys_sendmsg(int fd, struct msghdr *msg, unsigned int flags)
1076 {
1077 struct socket *sock;
1078 struct file *file;
1079 char address[MAX_SOCK_ADDR];
1080 struct iovec iov[MAX_IOVEC];
1081 struct msghdr msg_sys;
1082 int err;
1083 int total_len;
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 err=verify_area(VERIFY_READ, msg,sizeof(struct msghdr));
1091 if(err)
1092 return err;
1093 memcpy_fromfs(&msg_sys,msg,sizeof(struct msghdr));
1094 if(msg_sys.msg_iovlen>MAX_IOVEC)
1095 return -EINVAL;
1096 err=verify_iovec(&msg_sys,iov,address, VERIFY_READ);
1097 if(err<0)
1098 return err;
1099 total_len=err;
1100
1101 if(sock->ops->sendmsg==NULL)
1102 return -EOPNOTSUPP;
1103 return sock->ops->sendmsg(sock, &msg_sys, total_len, (file->f_flags&O_NONBLOCK), flags);
1104 }
1105
1106
1107
1108
1109
1110 asmlinkage int sys_recvmsg(int fd, struct msghdr *msg, unsigned int flags)
1111 {
1112 struct socket *sock;
1113 struct file *file;
1114 char address[MAX_SOCK_ADDR];
1115 struct iovec iov[MAX_IOVEC];
1116 struct msghdr msg_sys;
1117 int err;
1118 int total_len;
1119 int addr_len;
1120 int len;
1121
1122 if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1123 return(-EBADF);
1124 if (!(sock = sockfd_lookup(fd, NULL)))
1125 return(-ENOTSOCK);
1126
1127 err=verify_area(VERIFY_READ, msg,sizeof(struct msghdr));
1128 if(err)
1129 return err;
1130 memcpy_fromfs(&msg_sys,msg,sizeof(struct msghdr));
1131 if(msg_sys.msg_iovlen>MAX_IOVEC)
1132 return -EINVAL;
1133 err=verify_iovec(&msg_sys,iov,address, VERIFY_WRITE);
1134 if(err<0)
1135 return err;
1136 total_len=err;
1137
1138 if(sock->ops->recvmsg==NULL)
1139 return -EOPNOTSUPP;
1140 len=sock->ops->recvmsg(sock, &msg_sys, total_len, (file->f_flags&O_NONBLOCK), flags, &addr_len);
1141 if(len<0)
1142 return len;
1143
1144
1145
1146 if(msg_sys.msg_name!=NULL && (err=move_addr_to_user(address,addr_len, msg_sys.msg_name, &msg_sys.msg_namelen))<0)
1147 return err;
1148 return len;
1149 }
1150
1151
1152
1153
1154
1155
1156 int sock_fcntl(struct file *filp, unsigned int cmd, unsigned long arg)
1157 {
1158 struct socket *sock;
1159
1160 sock = socki_lookup (filp->f_inode);
1161 if (sock != NULL && sock->ops != NULL && sock->ops->fcntl != NULL)
1162 return(sock->ops->fcntl(sock, cmd, arg));
1163 return(-EINVAL);
1164 }
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179 asmlinkage int sys_socketcall(int call, unsigned long *args)
1180 {
1181 int er;
1182 unsigned char nargs[18]={0,3,3,3,2,3,3,3,
1183 4,4,4,6,6,2,5,5,3,3};
1184
1185 unsigned long a0,a1;
1186
1187 if(call<1||call>SYS_RECVMSG)
1188 return -EINVAL;
1189
1190 er=verify_area(VERIFY_READ, args, nargs[call] * sizeof(unsigned long));
1191 if(er)
1192 return er;
1193
1194 a0=get_user(args);
1195 a1=get_user(args+1);
1196
1197
1198 switch(call)
1199 {
1200 case SYS_SOCKET:
1201 return(sys_socket(a0,a1,get_user(args+2)));
1202 case SYS_BIND:
1203 return(sys_bind(a0,(struct sockaddr *)a1,
1204 get_user(args+2)));
1205 case SYS_CONNECT:
1206 return(sys_connect(a0, (struct sockaddr *)a1,
1207 get_user(args+2)));
1208 case SYS_LISTEN:
1209 return(sys_listen(a0,a1));
1210 case SYS_ACCEPT:
1211 return(sys_accept(a0,(struct sockaddr *)a1,
1212 (int *)get_user(args+2)));
1213 case SYS_GETSOCKNAME:
1214 return(sys_getsockname(a0,(struct sockaddr *)a1,
1215 (int *)get_user(args+2)));
1216 case SYS_GETPEERNAME:
1217 return(sys_getpeername(a0, (struct sockaddr *)a1,
1218 (int *)get_user(args+2)));
1219 case SYS_SOCKETPAIR:
1220 return(sys_socketpair(a0,a1,
1221 get_user(args+2),
1222 (int *)get_user(args+3)));
1223 case SYS_SEND:
1224 return(sys_send(a0,
1225 (void *)a1,
1226 get_user(args+2),
1227 get_user(args+3)));
1228 case SYS_SENDTO:
1229 return(sys_sendto(a0,(void *)a1,
1230 get_user(args+2),
1231 get_user(args+3),
1232 (struct sockaddr *)get_user(args+4),
1233 get_user(args+5)));
1234 case SYS_RECV:
1235 return(sys_recv(a0,
1236 (void *)a1,
1237 get_user(args+2),
1238 get_user(args+3)));
1239 case SYS_RECVFROM:
1240 return(sys_recvfrom(a0,
1241 (void *)a1,
1242 get_user(args+2),
1243 get_user(args+3),
1244 (struct sockaddr *)get_user(args+4),
1245 (int *)get_user(args+5)));
1246 case SYS_SHUTDOWN:
1247 return(sys_shutdown(a0,a1));
1248 case SYS_SETSOCKOPT:
1249 return(sys_setsockopt(a0,
1250 a1,
1251 get_user(args+2),
1252 (char *)get_user(args+3),
1253 get_user(args+4)));
1254 case SYS_GETSOCKOPT:
1255 return(sys_getsockopt(a0,
1256 a1,
1257 get_user(args+2),
1258 (char *)get_user(args+3),
1259 (int *)get_user(args+4)));
1260 case SYS_SENDMSG:
1261 return sys_sendmsg(a0,
1262 (struct msghdr *) a1,
1263 get_user(args+2));
1264 case SYS_RECVMSG:
1265 return sys_recvmsg(a0,
1266 (struct msghdr *) a1,
1267 get_user(args+2));
1268 }
1269 return -EINVAL;
1270 }
1271
1272
1273
1274
1275
1276
1277
1278 int sock_register(int family, struct proto_ops *ops)
1279 {
1280 int i;
1281
1282 cli();
1283 for(i = 0; i < NPROTO; i++)
1284 {
1285 if (pops[i] != NULL)
1286 continue;
1287 pops[i] = ops;
1288 pops[i]->family = family;
1289 sti();
1290 return(i);
1291 }
1292 sti();
1293 return(-ENOMEM);
1294 }
1295
1296
1297
1298
1299
1300
1301
1302 int sock_unregister(int family)
1303 {
1304 int i;
1305
1306 cli();
1307 for(i = 0; i < NPROTO; i++)
1308 {
1309 if (pops[i] == NULL)
1310 continue;
1311 if (pops[i]->family == family)
1312 {
1313 pops[i]=NULL;
1314 sti();
1315 return(i);
1316 }
1317 }
1318 sti();
1319 return(-ENOENT);
1320 }
1321
1322 void proto_init(void)
1323 {
1324 extern struct net_proto protocols[];
1325 struct net_proto *pro;
1326
1327
1328 pro = protocols;
1329 while (pro->name != NULL)
1330 {
1331 (*pro->init_func)(pro);
1332 pro++;
1333 }
1334
1335 }
1336
1337
1338 void sock_init(void)
1339 {
1340 int i;
1341
1342 printk("Swansea University Computer Society NET3.033 for Linux 1.3.38\n");
1343
1344
1345
1346
1347
1348 for (i = 0; i < NPROTO; ++i) pops[i] = NULL;
1349
1350
1351
1352
1353
1354 #ifdef CONFIG_NETLINK
1355 init_netlink();
1356 #endif
1357
1358
1359
1360
1361 #if defined(CONFIG_RTNETLINK)
1362 netlink_attach(NETLINK_ROUTE, netlink_donothing);
1363 #endif
1364
1365
1366
1367
1368
1369 #ifdef CONFIG_FIREWALL
1370 fwchain_init();
1371 #endif
1372
1373
1374
1375
1376
1377 proto_init();
1378 }
1379
1380 int socket_get_info(char *buffer, char **start, off_t offset, int length)
1381 {
1382 int len = sprintf(buffer, "sockets: used %d\n", sockets_in_use);
1383 if (offset >= len)
1384 {
1385 *start = buffer;
1386 return 0;
1387 }
1388 *start = buffer + offset;
1389 len -= offset;
1390 if (len > length)
1391 len = length;
1392 return len;
1393 }