This source file includes following definitions.
- family_name
- get_fd
- toss_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_awaitconn
- sock_socket
- sock_socketpair
- sock_bind
- sock_listen
- sock_accept
- sock_connect
- sock_getsockname
- sock_getpeername
- sys_send
- sys_sendto
- sys_recv
- sys_recvfrom
- sys_setsockopt
- sys_getsockopt
- sys_shutdown
- sock_fcntl
- sys_socketcall
- sock_init
1
2 #include <linux/signal.h>
3 #include <linux/errno.h>
4 #include <linux/sched.h>
5 #include <linux/kernel.h>
6 #include <linux/stat.h>
7 #include <linux/socket.h>
8 #include <linux/fcntl.h>
9 #include <linux/termios.h>
10 #include <linux/config.h>
11
12 #include <asm/system.h>
13 #include <asm/segment.h>
14
15 #include "kern_sock.h"
16 #include "socketcall.h"
17
18 extern int sys_close(int fd);
19
20 extern struct proto_ops unix_proto_ops;
21 #ifdef CONFIG_TCPIP
22 extern struct proto_ops inet_proto_ops;
23 #endif
24
25 static struct {
26 short family;
27 char *name;
28 struct proto_ops *ops;
29 } proto_table[] = {
30 {AF_UNIX, "AF_UNIX", &unix_proto_ops},
31 #ifdef CONFIG_TCPIP
32 {AF_INET, "AF_INET", &inet_proto_ops},
33 #endif
34 };
35 #define NPROTO (sizeof(proto_table) / sizeof(proto_table[0]))
36
37 static char *
38 family_name(int family)
39 {
40 int i;
41
42 for (i = 0; i < NPROTO; ++i)
43 if (proto_table[i].family == family)
44 return proto_table[i].name;
45 return "UNKNOWN";
46 }
47
48 static int sock_lseek(struct inode *inode, struct file *file, off_t offset,
49 int whence);
50 static int sock_read(struct inode *inode, struct file *file, char *buf,
51 int size);
52 static int sock_write(struct inode *inode, struct file *file, char *buf,
53 int size);
54 static int sock_readdir(struct inode *inode, struct file *file,
55 struct dirent *dirent, int count);
56 static void sock_close(struct inode *inode, struct file *file);
57 static int sock_select(struct inode *inode, struct file *file, int which, select_table *seltable);
58 static int sock_ioctl(struct inode *inode, struct file *file,
59 unsigned int cmd, unsigned int arg);
60
61 static struct file_operations socket_file_ops = {
62 sock_lseek,
63 sock_read,
64 sock_write,
65 sock_readdir,
66 sock_select,
67 sock_ioctl,
68 NULL,
69 NULL,
70 sock_close
71 };
72
73 struct socket sockets[NSOCKETS];
74 static struct wait_queue *socket_wait_free = NULL;
75
76
77
78
79 static int
80 get_fd(struct inode *inode)
81 {
82 int fd;
83 struct file *file;
84
85
86
87
88 file = get_empty_filp();
89 if (!file)
90 return -1;
91 for (fd = 0; fd < NR_OPEN; ++fd)
92 if (!current->filp[fd])
93 break;
94 if (fd == NR_OPEN) {
95 file->f_count = 0;
96 return -1;
97 }
98 FD_CLR(fd, ¤t->close_on_exec);
99 current->filp[fd] = file;
100 file->f_op = &socket_file_ops;
101 file->f_mode = 3;
102 file->f_flags = 0;
103 file->f_count = 1;
104 file->f_inode = inode;
105 if (inode)
106 inode->i_count++;
107 file->f_pos = 0;
108 return fd;
109 }
110
111
112
113
114
115
116 static inline void
117 toss_fd(int fd)
118 {
119
120 sys_close(fd);
121 }
122
123 struct socket *
124 socki_lookup(struct inode *inode)
125 {
126 struct socket *sock;
127
128 for (sock = sockets; sock <= last_socket; ++sock)
129 if (sock->state != SS_FREE && SOCK_INODE(sock) == inode)
130 return sock;
131 return NULL;
132 }
133
134 static inline struct socket *
135 sockfd_lookup(int fd, struct file **pfile)
136 {
137 struct file *file;
138
139 if (fd < 0 || fd >= NR_OPEN || !(file = current->filp[fd]))
140 return NULL;
141 if (pfile)
142 *pfile = file;
143 return socki_lookup(file->f_inode);
144 }
145
146 static struct socket *
147 sock_alloc(int wait)
148 {
149 struct socket *sock;
150
151 while (1) {
152 cli();
153 for (sock = sockets; sock <= last_socket; ++sock)
154 if (sock->state == SS_FREE) {
155 sock->state = SS_UNCONNECTED;
156 sti();
157 sock->flags = 0;
158 sock->ops = NULL;
159 sock->data = NULL;
160 sock->conn = NULL;
161 sock->iconn = NULL;
162
163
164
165
166
167
168
169
170
171 if (!(SOCK_INODE(sock) = get_empty_inode())) {
172 printk("sock_alloc: no more inodes\n");
173 sock->state = SS_FREE;
174 return NULL;
175 }
176 SOCK_INODE(sock)->i_mode = S_IFSOCK;
177 SOCK_INODE(sock)->i_uid = current->euid;
178 SOCK_INODE(sock)->i_gid = current->egid;
179
180 sock->wait = &SOCK_INODE(sock)->i_wait;
181 PRINTK(("sock_alloc: socket 0x%x,inode 0x%x\n",
182 sock, SOCK_INODE(sock)));
183 return sock;
184 }
185 sti();
186 if (!wait)
187 return NULL;
188 PRINTK(("sock_alloc: no free sockets, sleeping...\n"));
189 interruptible_sleep_on(&socket_wait_free);
190 if (current->signal & ~current->blocked) {
191 PRINTK(("sock_alloc: sleep was interrupted\n"));
192 return NULL;
193 }
194 PRINTK(("sock_alloc: wakeup... trying again...\n"));
195 }
196 }
197
198 static inline void
199 sock_release_peer(struct socket *peer)
200 {
201 peer->state = SS_DISCONNECTING;
202 wake_up(peer->wait);
203 }
204
205 static void
206 sock_release(struct socket *sock)
207 {
208 int oldstate;
209 struct socket *peersock, *nextsock;
210
211 PRINTK(("sock_release: socket 0x%x, inode 0x%x\n", sock,
212 SOCK_INODE(sock)));
213 if ((oldstate = sock->state) != SS_UNCONNECTED)
214 sock->state = SS_DISCONNECTING;
215
216
217
218 for (peersock = sock->iconn; peersock; peersock = nextsock) {
219 nextsock = peersock->next;
220 sock_release_peer(peersock);
221 }
222
223
224
225
226 peersock = (oldstate == SS_CONNECTED) ? sock->conn : NULL;
227 if (sock->ops)
228 sock->ops->release(sock, peersock);
229 if (peersock)
230 sock_release_peer(peersock);
231 sock->state = SS_FREE;
232 wake_up(&socket_wait_free);
233 iput(SOCK_INODE(sock));
234
235 }
236
237 static int
238 sock_lseek(struct inode *inode, struct file *file, off_t offset, int whence)
239 {
240 PRINTK(("sock_lseek: huh?\n"));
241 return -ESPIPE;
242 }
243
244 static int
245 sock_read(struct inode *inode, struct file *file, char *ubuf, int size)
246 {
247 struct socket *sock;
248
249 PRINTK(("sock_read: buf=0x%x, size=%d\n", ubuf, size));
250 if (!(sock = socki_lookup(inode))) {
251 printk("sock_read: can't find socket for inode!\n");
252 return -EBADF;
253 }
254 if (sock->flags & SO_ACCEPTCON)
255 return -EINVAL;
256 return sock->ops->read(sock, ubuf, size, (file->f_flags & O_NONBLOCK));
257 }
258
259 static int
260 sock_write(struct inode *inode, struct file *file, char *ubuf, int size)
261 {
262 struct socket *sock;
263
264 PRINTK(("sock_write: buf=0x%x, size=%d\n", ubuf, size));
265 if (!(sock = socki_lookup(inode))) {
266 printk("sock_write: can't find socket for inode!\n");
267 return -EBADF;
268 }
269 if (sock->flags & SO_ACCEPTCON)
270 return -EINVAL;
271 return sock->ops->write(sock, ubuf, size,(file->f_flags & O_NONBLOCK));
272 }
273
274 static int
275 sock_readdir(struct inode *inode, struct file *file, struct dirent *dirent,
276 int count)
277 {
278 PRINTK(("sock_readdir: huh?\n"));
279 return -EBADF;
280 }
281
282 int
283 sock_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
284 unsigned int arg)
285 {
286 struct socket *sock;
287
288 PRINTK(("sock_ioctl: inode=0x%x cmd=0x%x arg=%d\n", inode, cmd, arg));
289 if (!(sock = socki_lookup(inode))) {
290 printk("sock_ioctl: can't find socket for inode!\n");
291 return -EBADF;
292 }
293 return sock->ops->ioctl(sock, cmd, arg);
294 }
295
296 static int
297 sock_select(struct inode *inode, struct file *file, int sel_type, select_table * wait)
298 {
299 struct socket *sock;
300
301 PRINTK(("sock_select: inode = 0x%x, kind = %s\n", inode,
302 (sel_type == SEL_IN) ? "in" :
303 (sel_type == SEL_OUT) ? "out" : "ex"));
304 if (!(sock = socki_lookup(inode))) {
305 printk("sock_select: can't find socket for inode!\n");
306 return 0;
307 }
308
309
310
311 if (sock->ops && sock->ops->select)
312 return sock->ops->select(sock, sel_type, wait);
313 return 0;
314 }
315
316 void
317 sock_close(struct inode *inode, struct file *file)
318 {
319 struct socket *sock;
320
321 PRINTK(("sock_close: inode=0x%x (cnt=%d)\n", inode, inode->i_count));
322
323
324
325
326 if (!inode)
327 return;
328 if (!(sock = socki_lookup(inode))) {
329 printk("sock_close: can't find socket for inode!\n");
330 return;
331 }
332 sock_release(sock);
333 }
334
335 int
336 sock_awaitconn(struct socket *mysock, struct socket *servsock)
337 {
338 struct socket *last;
339
340 PRINTK(("sock_awaitconn: trying to connect socket 0x%x to 0x%x\n",
341 mysock, servsock));
342 if (!(servsock->flags & SO_ACCEPTCON)) {
343 PRINTK(("sock_awaitconn: server not accepting connections\n"));
344 return -EINVAL;
345 }
346
347
348
349
350 mysock->next = NULL;
351 cli();
352 if (!(last = servsock->iconn))
353 servsock->iconn = mysock;
354 else {
355 while (last->next)
356 last = last->next;
357 last->next = mysock;
358 }
359 mysock->state = SS_CONNECTING;
360 mysock->conn = servsock;
361 sti();
362
363
364
365
366
367 wake_up(servsock->wait);
368 if (mysock->state != SS_CONNECTED) {
369 interruptible_sleep_on(mysock->wait);
370 if (mysock->state != SS_CONNECTED) {
371
372
373
374
375
376
377
378 if (mysock->conn == servsock) {
379 cli();
380 if ((last = servsock->iconn) == mysock)
381 servsock->iconn = mysock->next;
382 else {
383 while (last->next != mysock)
384 last = last->next;
385 last->next = mysock->next;
386 }
387 sti();
388 }
389 return mysock->conn ? -EINTR : -EACCES;
390 }
391 }
392 return 0;
393 }
394
395
396
397
398
399 static int
400 sock_socket(int family, int type, int protocol)
401 {
402 int i, fd;
403 struct socket *sock;
404 struct proto_ops *ops;
405
406 PRINTK(("sys_socket: family = %d (%s), type = %d, protocol = %d\n",
407 family, family_name(family), type, protocol));
408
409
410
411
412 for (i = 0; i < NPROTO; ++i)
413 if (proto_table[i].family == family)
414 break;
415 if (i == NPROTO) {
416 PRINTK(("sys_socket: family not found\n"));
417 return -EINVAL;
418 }
419 ops = proto_table[i].ops;
420
421
422
423
424
425
426 if ((type != SOCK_STREAM &&
427 type != SOCK_DGRAM &&
428 type != SOCK_SEQPACKET &&
429 type != SOCK_RAW) ||
430 protocol < 0)
431 return -EINVAL;
432
433
434
435
436
437
438 if (!(sock = sock_alloc(1))) {
439 printk("sys_socket: no more sockets\n");
440 return -EAGAIN;
441 }
442 sock->type = type;
443 sock->ops = ops;
444 if ((i = sock->ops->create(sock, protocol)) < 0) {
445 sock_release(sock);
446 return i;
447 }
448
449 if ((fd = get_fd(SOCK_INODE(sock))) < 0) {
450 sock_release(sock);
451 return -EINVAL;
452 }
453
454 return fd;
455 }
456
457 static int
458 sock_socketpair(int family, int type, int protocol, int usockvec[2])
459 {
460 int fd1, fd2, i;
461 struct socket *sock1, *sock2;
462
463 PRINTK(("sys_socketpair: family = %d, type = %d, protocol = %d\n",
464 family, type, protocol));
465
466
467
468
469
470 if ((fd1 = sock_socket(family, type, protocol)) < 0)
471 return fd1;
472 sock1 = sockfd_lookup(fd1, NULL);
473 if (!sock1->ops->socketpair) {
474 sys_close(fd1);
475 return -EINVAL;
476 }
477
478
479
480
481 if ((fd2 = sock_socket(family, type, protocol)) < 0) {
482 sys_close(fd1);
483 return -EINVAL;
484 }
485 sock2 = sockfd_lookup(fd2, NULL);
486 if ((i = sock1->ops->socketpair(sock1, sock2)) < 0) {
487 sys_close(fd1);
488 sys_close(fd2);
489 return i;
490 }
491 sock1->conn = sock2;
492 sock2->conn = sock1;
493 sock1->state = SS_CONNECTED;
494 sock2->state = SS_CONNECTED;
495
496 verify_area(VERIFY_WRITE,usockvec, 2 * sizeof(int));
497 put_fs_long(fd1, &usockvec[0]);
498 put_fs_long(fd2, &usockvec[1]);
499
500 return 0;
501 }
502
503
504
505
506
507 static int
508 sock_bind(int fd, struct sockaddr *umyaddr, int addrlen)
509 {
510 struct socket *sock;
511 int i;
512
513 PRINTK(("sys_bind: fd = %d\n", fd));
514 if (!(sock = sockfd_lookup(fd, NULL)))
515 return -EBADF;
516 if ((i = sock->ops->bind(sock, umyaddr, addrlen)) < 0) {
517 PRINTK(("sys_bind: bind failed\n"));
518 return i;
519 }
520 return 0;
521 }
522
523
524
525
526
527
528 static int
529 sock_listen(int fd, int backlog)
530 {
531 struct socket *sock;
532
533 PRINTK(("sys_listen: fd = %d\n", fd));
534 if (!(sock = sockfd_lookup(fd, NULL)))
535 return -EBADF;
536 if (sock->state != SS_UNCONNECTED) {
537 PRINTK(("sys_listen: socket isn't unconnected\n"));
538 return -EINVAL;
539 }
540 if (sock->flags & SO_ACCEPTCON) {
541 PRINTK(("sys_listen: socket already accepting connections!\n"));
542 return -EINVAL;
543 }
544 if (sock->ops && sock->ops->listen)
545 sock->ops->listen (sock, backlog);
546 sock->flags |= SO_ACCEPTCON;
547 return 0;
548 }
549
550
551
552
553
554 static int
555 sock_accept(int fd, struct sockaddr *upeer_sockaddr, int *upeer_addrlen)
556 {
557 struct file *file;
558 struct socket *sock, *newsock;
559 int i;
560
561 PRINTK(("sys_accept: fd = %d\n", fd));
562 if (!(sock = sockfd_lookup(fd, &file)))
563 return -EBADF;
564 if (sock->state != SS_UNCONNECTED) {
565 PRINTK(("sys_accept: socket isn't unconnected\n"));
566 return -EINVAL;
567 }
568 if (!(sock->flags & SO_ACCEPTCON)) {
569 PRINTK(("sys_accept: socket not accepting connections!\n"));
570 return -EINVAL;
571 }
572
573 if (!(newsock = sock_alloc(0))) {
574 printk("sys_accept: no more sockets\n");
575 return -EAGAIN;
576 }
577 newsock->type = sock->type;
578 newsock->ops = sock->ops;
579 if ((i = sock->ops->dup(newsock, sock)) < 0) {
580 sock_release(newsock);
581 return i;
582 }
583
584 i = newsock->ops->accept(sock, newsock, file->f_flags);
585
586 if ( i < 0)
587 {
588 sock_release(newsock);
589 return (i);
590 }
591
592 if ((fd = get_fd(SOCK_INODE(newsock))) < 0) {
593 sock_release(newsock);
594 return -EINVAL;
595 }
596
597 PRINTK(("sys_accept: connected socket 0x%x via 0x%x\n",
598 sock, newsock));
599
600 if (upeer_sockaddr)
601 newsock->ops->getname(newsock, upeer_sockaddr,
602 upeer_addrlen, 1);
603
604 return fd;
605 }
606
607
608
609
610 static int
611 sock_connect(int fd, struct sockaddr *uservaddr, int addrlen)
612 {
613 struct socket *sock;
614 struct file *file;
615 int i;
616
617 PRINTK(("sys_connect: fd = %d\n", fd));
618 if (!(sock = sockfd_lookup(fd, &file)))
619 return -EBADF;
620 switch (sock->state) {
621 case SS_UNCONNECTED:
622
623 break;
624 case SS_CONNECTED:
625
626 return -EISCONN;
627 case SS_CONNECTING:
628
629
630 return (sock->ops->connect(sock, uservaddr, addrlen, file->f_flags));
631 default:
632 PRINTK(("sys_connect: socket not unconnected\n"));
633 return -EINVAL;
634 }
635 i = sock->ops->connect(sock, uservaddr, addrlen, file->f_flags);
636 if (i < 0) {
637 PRINTK(("sys_connect: connect failed\n"));
638 return i;
639 }
640 return 0;
641 }
642
643 static int
644 sock_getsockname(int fd, struct sockaddr *usockaddr, int *usockaddr_len)
645 {
646 struct socket *sock;
647
648 PRINTK(("sys_getsockname: fd = %d\n", fd));
649 if (!(sock = sockfd_lookup(fd, NULL)))
650 return -EBADF;
651 return sock->ops->getname(sock, usockaddr, usockaddr_len, 0);
652 }
653
654 static int
655 sock_getpeername(int fd, struct sockaddr *usockaddr, int *usockaddr_len)
656 {
657 struct socket *sock;
658
659 PRINTK(("sys_getpeername: fd = %d\n", fd));
660 if (!(sock = sockfd_lookup(fd, NULL)))
661 return -EBADF;
662 return sock->ops->getname(sock, usockaddr, usockaddr_len, 1);
663 }
664
665
666
667
668 static int
669 sys_send( int fd, void * buff, int len, unsigned flags)
670 {
671 struct socket *sock;
672 struct file *file;
673
674 PRINTK(("sys_send (fd = %d, buff = %X, len = %d, flags = %X)\n",
675 fd, buff, len, flags));
676
677 if (fd < 0 || fd >= NR_OPEN || ((file = current->filp[fd]) == NULL))
678 return (-EBADF);
679
680 if (!(sock = sockfd_lookup(fd, NULL)))
681 return (-ENOTSOCK);
682
683 return (sock->ops->send (sock, buff, len, (file->f_flags & O_NONBLOCK),
684 flags));
685
686 }
687
688 static int
689 sys_sendto( int fd, void * buff, int len, unsigned flags,
690 struct sockaddr *addr, int addr_len)
691 {
692 struct socket *sock;
693 struct file *file;
694
695 PRINTK(("sys_sendto (fd = %d, buff = %X, len = %d, flags = %X,"
696 " addr=%X, alen = %d\n", fd, buff, len, flags, addr, addr_len));
697
698 if (fd < 0 || fd >= NR_OPEN || ((file = current->filp[fd]) == NULL))
699 return (-EBADF);
700
701 if (!(sock = sockfd_lookup(fd, NULL)))
702 return (-ENOTSOCK);
703
704 return (sock->ops->sendto (sock, buff, len,
705 (file->f_flags & O_NONBLOCK),
706 flags, addr, addr_len));
707
708 }
709
710
711 static int
712 sys_recv( int fd, void * buff, int len, unsigned flags)
713 {
714 struct socket *sock;
715 struct file *file;
716
717 PRINTK(("sys_recv (fd = %d, buff = %X, len = %d, flags = %X)\n",
718 fd, buff, len, flags));
719
720 if (fd < 0 || fd >= NR_OPEN || ((file = current->filp[fd]) == NULL))
721 return (-EBADF);
722
723 if (!(sock = sockfd_lookup(fd, NULL)))
724 return (-ENOTSOCK);
725
726 return (sock->ops->recv (sock, buff, len,(file->f_flags & O_NONBLOCK),
727 flags));
728
729 }
730
731 static int
732 sys_recvfrom( int fd, void * buff, int len, unsigned flags,
733 struct sockaddr *addr, int *addr_len)
734 {
735 struct socket *sock;
736 struct file *file;
737
738 PRINTK(("sys_recvfrom (fd = %d, buff = %X, len = %d, flags = %X,"
739 " addr=%X, alen=%X\n", fd, buff, len, flags, addr, addr_len));
740
741 if (fd < 0 || fd >= NR_OPEN || ((file = current->filp[fd]) == NULL))
742 return (-EBADF);
743
744 if (!(sock = sockfd_lookup(fd, NULL)))
745 return (-ENOTSOCK);
746
747 return (sock->ops->recvfrom (sock, buff, len,
748 (file->f_flags & O_NONBLOCK),
749 flags, addr, addr_len));
750
751 }
752
753
754 static int
755 sys_setsockopt (int fd, int level, int optname, char *optval, int optlen)
756 {
757 struct socket *sock;
758 struct file *file;
759
760 PRINTK (("sys_setsockopt(fd=%d, level=%d, optname=%d,\n",fd, level,
761 optname));
762 PRINTK ((" optval = %X, optlen = %d)\n", optval, optlen));
763
764 if (fd < 0 || fd >= NR_OPEN || ((file = current->filp[fd]) == NULL))
765 return (-EBADF);
766
767 if (!(sock = sockfd_lookup(fd, NULL)))
768 return (-ENOTSOCK);
769
770 return (sock->ops->setsockopt (sock, level, optname, optval, optlen));
771
772 }
773
774 static int
775 sys_getsockopt (int fd, int level, int optname, char *optval, int *optlen)
776 {
777 struct socket *sock;
778 struct file *file;
779 PRINTK (("sys_getsockopt(fd=%d, level=%d, optname=%d,\n",fd, level,
780 optname));
781 PRINTK ((" optval = %X, optlen = %X)\n", optval, optlen));
782
783 if (fd < 0 || fd >= NR_OPEN || ((file = current->filp[fd]) == NULL))
784 return (-EBADF);
785
786 if (!(sock = sockfd_lookup(fd, NULL)))
787 return (-ENOTSOCK);
788
789 if (!sock->ops || !sock->ops->getsockopt)
790 return 0;
791 return sock->ops->getsockopt(sock, level, optname, optval, optlen);
792
793 }
794
795
796 static int
797 sys_shutdown( int fd, int how)
798 {
799 struct socket *sock;
800 struct file *file;
801
802 PRINTK(("sys_shutdown (fd = %d, how = %d)\n",fd, how));
803
804 file = current->filp[fd];
805 if (fd < 0 || fd >= NR_OPEN || file == NULL)
806 return (-EBADF);
807
808 if (!(sock = sockfd_lookup(fd, NULL)))
809 return (-ENOTSOCK);
810
811 return (sock->ops->shutdown (sock, how));
812
813 }
814
815 int
816 sock_fcntl(struct file *filp, unsigned int cmd, unsigned long arg)
817 {
818 struct socket *sock;
819 sock = socki_lookup (filp->f_inode);
820
821 if (sock != NULL && sock->ops != NULL && sock->ops->fcntl != NULL)
822 return (sock->ops->fcntl (sock, cmd, arg));
823
824 return (-EINVAL);
825 }
826
827
828
829
830
831
832
833 int
834 sys_socketcall(int call, unsigned long *args)
835 {
836 switch (call) {
837 case SYS_SOCKET:
838 verify_area(VERIFY_WRITE,args, 3 * sizeof(long));
839 return sock_socket(get_fs_long(args+0),
840 get_fs_long(args+1),
841 get_fs_long(args+2));
842
843 case SYS_BIND:
844 verify_area(VERIFY_WRITE,args, 3 * sizeof(long));
845 return sock_bind(get_fs_long(args+0),
846 (struct sockaddr *)get_fs_long(args+1),
847 get_fs_long(args+2));
848
849 case SYS_CONNECT:
850 verify_area(VERIFY_WRITE,args, 3 * sizeof(long));
851 return sock_connect(get_fs_long(args+0),
852 (struct sockaddr *)get_fs_long(args+1),
853 get_fs_long(args+2));
854
855 case SYS_LISTEN:
856 verify_area(VERIFY_WRITE,args, 2 * sizeof(long));
857 return sock_listen(get_fs_long(args+0),
858 get_fs_long(args+1));
859
860 case SYS_ACCEPT:
861 verify_area(VERIFY_WRITE,args, 3 * sizeof(long));
862 return sock_accept(get_fs_long(args+0),
863 (struct sockaddr *)get_fs_long(args+1),
864 (int *)get_fs_long(args+2));
865
866 case SYS_GETSOCKNAME:
867 verify_area(VERIFY_WRITE,args, 3 * sizeof(long));
868 return sock_getsockname(get_fs_long(args+0),
869 (struct sockaddr *)get_fs_long(args+1),
870 (int *)get_fs_long(args+2));
871
872 case SYS_GETPEERNAME:
873 verify_area(VERIFY_WRITE,args, 3 * sizeof(long));
874 return sock_getpeername(get_fs_long(args+0),
875 (struct sockaddr *)get_fs_long(args+1),
876 (int *)get_fs_long(args+2));
877
878 case SYS_SOCKETPAIR:
879 verify_area(VERIFY_WRITE,args, 4 * sizeof(long));
880 return sock_socketpair(get_fs_long(args+0),
881 get_fs_long(args+1),
882 get_fs_long(args+2),
883 (int *)get_fs_long(args+3));
884
885 case SYS_SEND:
886 verify_area(VERIFY_WRITE,args, 4 * sizeof (unsigned long));
887 return ( sys_send (get_fs_long(args+0),
888 (void *)get_fs_long(args+1),
889 get_fs_long(args+2),
890 get_fs_long(args+3)));
891
892 case SYS_SENDTO:
893 verify_area(VERIFY_WRITE,args, 6 * sizeof (unsigned long));
894 return ( sys_sendto (get_fs_long(args+0),
895 (void *)get_fs_long(args+1),
896 get_fs_long(args+2),
897 get_fs_long(args+3),
898 (struct sockaddr *)get_fs_long(args+4),
899 get_fs_long(args+5)));
900
901
902 case SYS_RECV:
903 verify_area(VERIFY_WRITE,args, 4 * sizeof (unsigned long));
904 return ( sys_recv (get_fs_long(args+0),
905 (void *)get_fs_long(args+1),
906 get_fs_long(args+2),
907 get_fs_long(args+3)));
908
909 case SYS_RECVFROM:
910 verify_area(VERIFY_WRITE,args, 6 * sizeof (unsigned long));
911 return ( sys_recvfrom (get_fs_long(args+0),
912 (void *)get_fs_long(args+1),
913 get_fs_long(args+2),
914 get_fs_long(args+3),
915 (struct sockaddr *)get_fs_long(args+4),
916 (int *)get_fs_long(args+5)));
917
918 case SYS_SHUTDOWN:
919 verify_area(VERIFY_WRITE, args, 2* sizeof (unsigned long));
920 return ( sys_shutdown (get_fs_long (args+0),
921 get_fs_long (args+1)));
922
923 case SYS_SETSOCKOPT:
924 verify_area(VERIFY_WRITE, args, 5*sizeof (unsigned long));
925 return (sys_setsockopt (get_fs_long (args+0),
926 get_fs_long (args+1),
927 get_fs_long (args+2),
928 (char *)get_fs_long (args+3),
929 get_fs_long (args+4)));
930
931
932 case SYS_GETSOCKOPT:
933 verify_area(VERIFY_WRITE, args, 5*sizeof (unsigned long));
934 return (sys_getsockopt (get_fs_long (args+0),
935 get_fs_long (args+1),
936 get_fs_long (args+2),
937 (char *)get_fs_long (args+3),
938 (int *)get_fs_long (args+4)));
939
940 default:
941 return -EINVAL;
942 }
943 }
944
945 void
946 sock_init(void)
947 {
948 struct socket *sock;
949 int i, ok;
950
951 for (sock = sockets; sock <= last_socket; ++sock)
952 sock->state = SS_FREE;
953 for (i = ok = 0; i < NPROTO; ++i) {
954 if ((*proto_table[i].ops->init)() < 0) {
955 printk("sock_init: init failed family %d (%s)\n",
956 proto_table[i].family,
957 proto_table[i].name);
958 proto_table[i].family = -1;
959 }
960 else
961 ++ok;
962 }
963 if (!ok)
964 printk("sock_init: warning: no protocols initialized\n");
965 return;
966 }
967