This source file includes following definitions.
- sk_inuse
- get_new_socknum
- put_sock
- remove_sock
- destroy_sock
- inet_fcntl
- inet_setsockopt
- inet_getsockopt
- inet_autobind
- inet_listen
- def_callback1
- def_callback2
- inet_create
- inet_dup
- inet_release
- inet_bind
- inet_error
- inet_connect
- inet_socketpair
- inet_accept
- inet_getname
- inet_recvfrom
- inet_recv
- inet_read
- inet_send
- inet_write
- inet_sendto
- inet_shutdown
- inet_select
- inet_ioctl
- get_sock
- inet_proto_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 #include <linux/config.h>
32 #include <linux/errno.h>
33 #include <linux/types.h>
34 #include <linux/socket.h>
35 #include <linux/in.h>
36 #include <linux/kernel.h>
37 #include <linux/major.h>
38 #include <linux/sched.h>
39 #include <linux/timer.h>
40 #include <linux/string.h>
41 #include <linux/sockios.h>
42 #include <linux/net.h>
43 #include <linux/fcntl.h>
44 #include <linux/mm.h>
45 #include <linux/interrupt.h>
46
47 #include <asm/segment.h>
48 #include <asm/system.h>
49
50 #include <linux/inet.h>
51 #include <linux/netdevice.h>
52 #include "ip.h"
53 #include "protocol.h"
54 #include "arp.h"
55 #include "rarp.h"
56 #include "route.h"
57 #include "tcp.h"
58 #include "udp.h"
59 #include <linux/skbuff.h>
60 #include "sock.h"
61 #include "raw.h"
62 #include "icmp.h"
63
64 #define min(a,b) ((a)<(b)?(a):(b))
65
66 extern struct proto packet_prot;
67
68
69
70
71
72
73 static int sk_inuse(struct proto *prot, int num)
74 {
75 struct sock *sk;
76
77 for(sk = prot->sock_array[num & (SOCK_ARRAY_SIZE -1 )];
78 sk != NULL; sk=sk->next)
79 {
80 if (sk->num == num)
81 return(1);
82 }
83 return(0);
84 }
85
86
87
88
89
90
91 unsigned short get_new_socknum(struct proto *prot, unsigned short base)
92 {
93 static int start=0;
94
95
96
97
98
99
100 int i, j;
101 int best = 0;
102 int size = 32767;
103 struct sock *sk;
104
105 if (base == 0)
106 base = PROT_SOCK+1+(start % 1024);
107 if (base <= PROT_SOCK)
108 {
109 base += PROT_SOCK+(start % 1024);
110 }
111
112
113 for(i=0; i < SOCK_ARRAY_SIZE; i++)
114 {
115 j = 0;
116 sk = prot->sock_array[(i+base+1) &(SOCK_ARRAY_SIZE -1)];
117 while(sk != NULL)
118 {
119 sk = sk->next;
120 j++;
121 }
122 if (j == 0)
123 {
124 start =(i+1+start )%1024;
125 return(i+base+1);
126 }
127 if (j < size)
128 {
129 best = i;
130 size = j;
131 }
132 }
133
134
135
136 while(sk_inuse(prot, base +best+1))
137 {
138 best += SOCK_ARRAY_SIZE;
139 }
140 return(best+base+1);
141 }
142
143
144
145
146
147 void put_sock(unsigned short num, struct sock *sk)
148 {
149 struct sock *sk1;
150 struct sock *sk2;
151 int mask;
152
153 sk->num = num;
154 sk->next = NULL;
155 num = num &(SOCK_ARRAY_SIZE -1);
156
157
158 cli();
159 if (sk->prot->sock_array[num] == NULL)
160 {
161 sk->prot->sock_array[num] = sk;
162 sti();
163 return;
164 }
165 sti();
166 for(mask = 0xff000000; mask != 0xffffffff; mask = (mask >> 8) | mask)
167 {
168 if ((mask & sk->saddr) &&
169 (mask & sk->saddr) != (mask & 0xffffffff))
170 {
171 mask = mask << 8;
172 break;
173 }
174 }
175 cli();
176 sk1 = sk->prot->sock_array[num];
177 for(sk2 = sk1; sk2 != NULL; sk2=sk2->next)
178 {
179 if (!(sk2->saddr & mask))
180 {
181 if (sk2 == sk1)
182 {
183 sk->next = sk->prot->sock_array[num];
184 sk->prot->sock_array[num] = sk;
185 sti();
186 return;
187 }
188 sk->next = sk2;
189 sk1->next= sk;
190 sti();
191 return;
192 }
193 sk1 = sk2;
194 }
195
196
197 sk->next = NULL;
198 sk1->next = sk;
199 sti();
200 }
201
202
203
204
205
206 static void remove_sock(struct sock *sk1)
207 {
208 struct sock *sk2;
209
210 if (!sk1->prot)
211 {
212 printk("sock.c: remove_sock: sk1->prot == NULL\n");
213 return;
214 }
215
216
217 cli();
218 sk2 = sk1->prot->sock_array[sk1->num &(SOCK_ARRAY_SIZE -1)];
219 if (sk2 == sk1)
220 {
221 sk1->prot->sock_array[sk1->num &(SOCK_ARRAY_SIZE -1)] = sk1->next;
222 sti();
223 return;
224 }
225
226 while(sk2 && sk2->next != sk1)
227 {
228 sk2 = sk2->next;
229 }
230
231 if (sk2)
232 {
233 sk2->next = sk1->next;
234 sti();
235 return;
236 }
237 sti();
238 }
239
240
241
242
243
244 void destroy_sock(struct sock *sk)
245 {
246 struct sk_buff *skb;
247
248 sk->inuse = 1;
249
250
251 if (!sk->dead)
252 sk->write_space(sk);
253
254 remove_sock(sk);
255
256
257 delete_timer(sk);
258
259 while ((skb = tcp_dequeue_partial(sk)) != NULL) {
260 IS_SKB(skb);
261 kfree_skb(skb, FREE_WRITE);
262 }
263
264
265 while((skb = skb_dequeue(&sk->write_queue)) != NULL) {
266 IS_SKB(skb);
267 kfree_skb(skb, FREE_WRITE);
268 }
269
270
271
272
273
274
275 if (sk->dead)
276 {
277 while((skb=skb_dequeue(&sk->receive_queue))!=NULL)
278 {
279
280
281
282
283 if (skb->sk != NULL && skb->sk != sk)
284 {
285 IS_SKB(skb);
286 skb->sk->dead = 1;
287 skb->sk->prot->close(skb->sk, 0);
288 }
289 IS_SKB(skb);
290 kfree_skb(skb, FREE_READ);
291 }
292 }
293
294
295 cli();
296 for(skb = sk->send_head; skb != NULL; )
297 {
298 struct sk_buff *skb2;
299
300
301
302
303
304 if (skb->next && skb->prev) {
305
306 IS_SKB(skb);
307 skb_unlink(skb);
308 }
309 skb->dev = NULL;
310 skb2 = skb->link3;
311 kfree_skb(skb, FREE_WRITE);
312 skb = skb2;
313 }
314 sk->send_head = NULL;
315 sti();
316
317
318 while((skb=skb_dequeue(&sk->back_log))!=NULL)
319 {
320
321
322 kfree_skb(skb, FREE_READ);
323 }
324
325
326 if (sk->pair)
327 {
328 sk->pair->dead = 1;
329 sk->pair->prot->close(sk->pair, 0);
330 sk->pair = NULL;
331 }
332
333
334
335
336
337
338
339 if (sk->dead && sk->rmem_alloc == 0 && sk->wmem_alloc == 0)
340 {
341 kfree_s((void *)sk,sizeof(*sk));
342 }
343 else
344 {
345
346
347 sk->destroy = 1;
348 sk->ack_backlog = 0;
349 sk->inuse = 0;
350 reset_timer(sk, TIME_DESTROY, SOCK_DESTROY_TIME);
351 }
352 }
353
354
355
356
357
358
359
360 static int inet_fcntl(struct socket *sock, unsigned int cmd, unsigned long arg)
361 {
362 struct sock *sk;
363
364 sk = (struct sock *) sock->data;
365
366 switch(cmd)
367 {
368 case F_SETOWN:
369
370
371
372
373
374 if (!suser() && current->pgrp != -arg &&
375 current->pid != arg) return(-EPERM);
376 sk->proc = arg;
377 return(0);
378 case F_GETOWN:
379 return(sk->proc);
380 default:
381 return(-EINVAL);
382 }
383 }
384
385
386
387
388
389 static int inet_setsockopt(struct socket *sock, int level, int optname,
390 char *optval, int optlen)
391 {
392 struct sock *sk = (struct sock *) sock->data;
393 if (level == SOL_SOCKET)
394 return sock_setsockopt(sk,level,optname,optval,optlen);
395 if (sk->prot->setsockopt==NULL)
396 return(-EOPNOTSUPP);
397 else
398 return sk->prot->setsockopt(sk,level,optname,optval,optlen);
399 }
400
401
402
403
404
405 static int inet_getsockopt(struct socket *sock, int level, int optname,
406 char *optval, int *optlen)
407 {
408 struct sock *sk = (struct sock *) sock->data;
409 if (level == SOL_SOCKET)
410 return sock_getsockopt(sk,level,optname,optval,optlen);
411 if(sk->prot->getsockopt==NULL)
412 return(-EOPNOTSUPP);
413 else
414 return sk->prot->getsockopt(sk,level,optname,optval,optlen);
415 }
416
417
418
419
420
421 static int inet_autobind(struct sock *sk)
422 {
423
424 if (sk->num == 0)
425 {
426 sk->num = get_new_socknum(sk->prot, 0);
427 if (sk->num == 0)
428 return(-EAGAIN);
429 put_sock(sk->num, sk);
430 sk->dummy_th.source = ntohs(sk->num);
431 }
432 return 0;
433 }
434
435
436
437
438
439 static int inet_listen(struct socket *sock, int backlog)
440 {
441 struct sock *sk = (struct sock *) sock->data;
442
443 if(inet_autobind(sk)!=0)
444 return -EAGAIN;
445
446
447 sk->max_ack_backlog = backlog;
448 if (sk->state != TCP_LISTEN)
449 {
450 sk->ack_backlog = 0;
451 sk->state = TCP_LISTEN;
452 }
453 return(0);
454 }
455
456
457
458
459
460
461 static void def_callback1(struct sock *sk)
462 {
463 if(!sk->dead)
464 wake_up_interruptible(sk->sleep);
465 }
466
467 static void def_callback2(struct sock *sk,int len)
468 {
469 if(!sk->dead)
470 {
471 wake_up_interruptible(sk->sleep);
472 sock_wake_async(sk->socket);
473 }
474 }
475
476
477
478
479
480
481
482
483
484 static int inet_create(struct socket *sock, int protocol)
485 {
486 struct sock *sk;
487 struct proto *prot;
488 int err;
489
490 sk = (struct sock *) kmalloc(sizeof(*sk), GFP_KERNEL);
491 if (sk == NULL)
492 return(-ENOBUFS);
493 sk->num = 0;
494 sk->reuse = 0;
495 switch(sock->type)
496 {
497 case SOCK_STREAM:
498 case SOCK_SEQPACKET:
499 if (protocol && protocol != IPPROTO_TCP)
500 {
501 kfree_s((void *)sk, sizeof(*sk));
502 return(-EPROTONOSUPPORT);
503 }
504 protocol = IPPROTO_TCP;
505 sk->no_check = TCP_NO_CHECK;
506 prot = &tcp_prot;
507 break;
508
509 case SOCK_DGRAM:
510 if (protocol && protocol != IPPROTO_UDP)
511 {
512 kfree_s((void *)sk, sizeof(*sk));
513 return(-EPROTONOSUPPORT);
514 }
515 protocol = IPPROTO_UDP;
516 sk->no_check = UDP_NO_CHECK;
517 prot=&udp_prot;
518 break;
519
520 case SOCK_RAW:
521 if (!suser())
522 {
523 kfree_s((void *)sk, sizeof(*sk));
524 return(-EPERM);
525 }
526 if (!protocol)
527 {
528 kfree_s((void *)sk, sizeof(*sk));
529 return(-EPROTONOSUPPORT);
530 }
531 prot = &raw_prot;
532 sk->reuse = 1;
533 sk->no_check = 0;
534
535
536
537 sk->num = protocol;
538 break;
539
540 case SOCK_PACKET:
541 if (!suser())
542 {
543 kfree_s((void *)sk, sizeof(*sk));
544 return(-EPERM);
545 }
546 if (!protocol)
547 {
548 kfree_s((void *)sk, sizeof(*sk));
549 return(-EPROTONOSUPPORT);
550 }
551 prot = &packet_prot;
552 sk->reuse = 1;
553 sk->no_check = 0;
554
555
556 sk->num = protocol;
557 break;
558
559 default:
560 kfree_s((void *)sk, sizeof(*sk));
561 return(-ESOCKTNOSUPPORT);
562 }
563 sk->socket = sock;
564 #ifdef CONFIG_TCP_NAGLE_OFF
565 sk->nonagle = 1;
566 #else
567 sk->nonagle = 0;
568 #endif
569 sk->type = sock->type;
570 sk->stamp.tv_sec=0;
571 sk->protocol = protocol;
572 sk->wmem_alloc = 0;
573 sk->rmem_alloc = 0;
574 sk->sndbuf = SK_WMEM_MAX;
575 sk->rcvbuf = SK_RMEM_MAX;
576 sk->pair = NULL;
577 sk->opt = NULL;
578 sk->write_seq = 0;
579 sk->acked_seq = 0;
580 sk->copied_seq = 0;
581 sk->fin_seq = 0;
582 sk->urg_seq = 0;
583 sk->urg_data = 0;
584 sk->proc = 0;
585 sk->rtt = 0;
586 sk->rto = TCP_TIMEOUT_INIT;
587 sk->mdev = 0;
588 sk->backoff = 0;
589 sk->packets_out = 0;
590 sk->cong_window = 1;
591 sk->cong_count = 0;
592 sk->ssthresh = 0;
593 sk->max_window = 0;
594 sk->urginline = 0;
595 sk->intr = 0;
596 sk->linger = 0;
597 sk->destroy = 0;
598 sk->priority = 1;
599 sk->shutdown = 0;
600 sk->keepopen = 0;
601 sk->zapped = 0;
602 sk->done = 0;
603 sk->ack_backlog = 0;
604 sk->window = 0;
605 sk->bytes_rcv = 0;
606 sk->state = TCP_CLOSE;
607 sk->dead = 0;
608 sk->ack_timed = 0;
609 sk->partial = NULL;
610 sk->user_mss = 0;
611 sk->debug = 0;
612
613
614 sk->max_unacked = 2048;
615
616
617
618 sk->max_ack_backlog = 0;
619 sk->inuse = 0;
620 sk->delay_acks = 0;
621 skb_queue_head_init(&sk->write_queue);
622 skb_queue_head_init(&sk->receive_queue);
623 sk->mtu = 576;
624 sk->prot = prot;
625 sk->sleep = sock->wait;
626 sk->daddr = 0;
627 sk->saddr = ip_my_addr();
628 sk->err = 0;
629 sk->next = NULL;
630 sk->pair = NULL;
631 sk->send_tail = NULL;
632 sk->send_head = NULL;
633 sk->timeout = 0;
634 sk->broadcast = 0;
635 sk->localroute = 0;
636 init_timer(&sk->timer);
637 sk->timer.data = (unsigned long)sk;
638 sk->timer.function = &net_timer;
639 skb_queue_head_init(&sk->back_log);
640 sk->blog = 0;
641 sock->data =(void *) sk;
642 sk->dummy_th.doff = sizeof(sk->dummy_th)/4;
643 sk->dummy_th.res1=0;
644 sk->dummy_th.res2=0;
645 sk->dummy_th.urg_ptr = 0;
646 sk->dummy_th.fin = 0;
647 sk->dummy_th.syn = 0;
648 sk->dummy_th.rst = 0;
649 sk->dummy_th.psh = 0;
650 sk->dummy_th.ack = 0;
651 sk->dummy_th.urg = 0;
652 sk->dummy_th.dest = 0;
653 sk->ip_tos=0;
654 sk->ip_ttl=64;
655
656 sk->state_change = def_callback1;
657 sk->data_ready = def_callback2;
658 sk->write_space = def_callback1;
659 sk->error_report = def_callback1;
660
661 if (sk->num)
662 {
663
664
665
666
667
668
669 put_sock(sk->num, sk);
670 sk->dummy_th.source = ntohs(sk->num);
671 }
672
673 if (sk->prot->init)
674 {
675 err = sk->prot->init(sk);
676 if (err != 0)
677 {
678 destroy_sock(sk);
679 return(err);
680 }
681 }
682 return(0);
683 }
684
685
686
687
688
689
690 static int inet_dup(struct socket *newsock, struct socket *oldsock)
691 {
692 return(inet_create(newsock,((struct sock *)(oldsock->data))->protocol));
693 }
694
695
696
697
698
699
700
701
702 static int inet_release(struct socket *sock, struct socket *peer)
703 {
704 struct sock *sk = (struct sock *) sock->data;
705 if (sk == NULL)
706 return(0);
707
708 sk->state_change(sk);
709
710
711
712
713
714
715
716
717
718 if (sk->linger == 0)
719 {
720 sk->prot->close(sk,0);
721 sk->dead = 1;
722 }
723 else
724 {
725 sk->prot->close(sk, 0);
726 cli();
727 if (sk->lingertime)
728 current->timeout = jiffies + HZ*sk->lingertime;
729 while(sk->state != TCP_CLOSE && current->timeout>0)
730 {
731 interruptible_sleep_on(sk->sleep);
732 if (current->signal & ~current->blocked)
733 {
734 break;
735 #if 0
736
737 sti();
738 current->timeout=0;
739 return(-ERESTARTSYS);
740 #endif
741 }
742 }
743 current->timeout=0;
744 sti();
745 sk->dead = 1;
746 }
747 sk->inuse = 1;
748
749
750 release_sock(sk);
751 sock->data = NULL;
752 return(0);
753 }
754
755
756
757
758
759
760 static int inet_bind(struct socket *sock, struct sockaddr *uaddr,
761 int addr_len)
762 {
763 struct sockaddr_in *addr=(struct sockaddr_in *)uaddr;
764 struct sock *sk=(struct sock *)sock->data, *sk2;
765 unsigned short snum;
766 int chk_addr_ret;
767
768
769 if (sk->state != TCP_CLOSE)
770 return(-EIO);
771 if (sk->num != 0)
772 return(-EINVAL);
773
774 if(addr_len<sizeof(struct sockaddr_in))
775 return -EINVAL;
776
777 snum = ntohs(addr->sin_port);
778
779
780
781
782
783
784 if (snum == 0)
785 {
786 snum = get_new_socknum(sk->prot, 0);
787 }
788 if (snum < PROT_SOCK && !suser())
789 return(-EACCES);
790
791 chk_addr_ret = ip_chk_addr(addr->sin_addr.s_addr);
792 if (addr->sin_addr.s_addr != 0 && chk_addr_ret != IS_MYADDR)
793 return(-EADDRNOTAVAIL);
794
795 if (chk_addr_ret || addr->sin_addr.s_addr == 0)
796 sk->saddr = addr->sin_addr.s_addr;
797
798
799 cli();
800 outside_loop:
801 for(sk2 = sk->prot->sock_array[snum & (SOCK_ARRAY_SIZE -1)];
802 sk2 != NULL; sk2 = sk2->next)
803 {
804
805 if (sk2->num != snum) continue;
806 if (sk2->dead)
807 {
808 destroy_sock(sk2);
809 goto outside_loop;
810 }
811 if (!sk->reuse)
812 {
813 sti();
814 return(-EADDRINUSE);
815 }
816
817 if (sk2->num != snum)
818 continue;
819 if (sk2->saddr != sk->saddr)
820 continue;
821 if (!sk2->reuse)
822 {
823 sti();
824 return(-EADDRINUSE);
825 }
826 }
827 sti();
828
829 remove_sock(sk);
830 put_sock(snum, sk);
831 sk->dummy_th.source = ntohs(sk->num);
832 sk->daddr = 0;
833 sk->dummy_th.dest = 0;
834 return(0);
835 }
836
837
838
839
840
841 static int inet_error(struct sock *sk)
842 {
843 unsigned long flags;
844 int err;
845 save_flags(flags);
846 cli();
847 err=sk->err;
848 sk->err=0;
849 sti();
850 return -err;
851 }
852
853
854
855
856
857
858 static int inet_connect(struct socket *sock, struct sockaddr * uaddr,
859 int addr_len, int flags)
860 {
861 struct sock *sk=(struct sock *)sock->data;
862 int err;
863 sock->conn = NULL;
864
865 if (sock->state == SS_CONNECTING && tcp_connected(sk->state))
866 {
867 sock->state = SS_CONNECTED;
868
869 return 0;
870 }
871
872 if (sock->state == SS_CONNECTING && sk->protocol == IPPROTO_TCP && (flags & O_NONBLOCK))
873 return -EALREADY;
874
875 if (sock->state != SS_CONNECTING)
876 {
877
878 if(inet_autobind(sk)!=0)
879 return(-EAGAIN);
880 if (sk->prot->connect == NULL)
881 return(-EOPNOTSUPP);
882 err = sk->prot->connect(sk, (struct sockaddr_in *)uaddr, addr_len);
883 if (err < 0)
884 return(err);
885 sock->state = SS_CONNECTING;
886 }
887
888 if (sk->state > TCP_FIN_WAIT2 && sock->state==SS_CONNECTING)
889 {
890 sock->state=SS_UNCONNECTED;
891 cli();
892 err=sk->err;
893 sk->err=0;
894 sti();
895 return -err;
896 }
897
898 if (sk->state != TCP_ESTABLISHED &&(flags & O_NONBLOCK))
899 return(-EINPROGRESS);
900
901 cli();
902 while(sk->state == TCP_SYN_SENT || sk->state == TCP_SYN_RECV)
903 {
904 interruptible_sleep_on(sk->sleep);
905 if (current->signal & ~current->blocked)
906 {
907 sti();
908 return(-ERESTARTSYS);
909 }
910
911
912 if(sk->err && sk->protocol == IPPROTO_TCP)
913 {
914 sti();
915 sock->state = SS_UNCONNECTED;
916 err = -sk->err;
917 sk->err=0;
918 return err;
919 }
920 }
921 sti();
922 sock->state = SS_CONNECTED;
923
924 if (sk->state != TCP_ESTABLISHED && sk->err)
925 {
926 sock->state = SS_UNCONNECTED;
927 err=sk->err;
928 sk->err=0;
929 return(-err);
930 }
931 return(0);
932 }
933
934
935 static int inet_socketpair(struct socket *sock1, struct socket *sock2)
936 {
937 return(-EOPNOTSUPP);
938 }
939
940
941
942
943
944
945 static int inet_accept(struct socket *sock, struct socket *newsock, int flags)
946 {
947 struct sock *sk1, *sk2;
948 int err;
949
950 sk1 = (struct sock *) sock->data;
951
952
953
954
955
956
957 if (newsock->data)
958 {
959 struct sock *sk=(struct sock *)newsock->data;
960 newsock->data=NULL;
961 sk->dead = 1;
962 destroy_sock(sk);
963 }
964
965 if (sk1->prot->accept == NULL)
966 return(-EOPNOTSUPP);
967
968
969 if (sk1->pair != NULL )
970 {
971 sk2 = sk1->pair;
972 sk1->pair = NULL;
973 }
974 else
975 {
976 sk2 = sk1->prot->accept(sk1,flags);
977 if (sk2 == NULL)
978 {
979 if (sk1->err <= 0)
980 printk("Warning sock.c:sk1->err <= 0. Returning non-error.\n");
981 err=sk1->err;
982 sk1->err=0;
983 return(-err);
984 }
985 }
986 newsock->data = (void *)sk2;
987 sk2->sleep = newsock->wait;
988 sk2->socket = newsock;
989 newsock->conn = NULL;
990 if (flags & O_NONBLOCK)
991 return(0);
992
993 cli();
994 while(sk2->state == TCP_SYN_RECV)
995 {
996 interruptible_sleep_on(sk2->sleep);
997 if (current->signal & ~current->blocked)
998 {
999 sti();
1000 sk1->pair = sk2;
1001 sk2->sleep = NULL;
1002 sk2->socket=NULL;
1003 newsock->data = NULL;
1004 return(-ERESTARTSYS);
1005 }
1006 }
1007 sti();
1008
1009 if (sk2->state != TCP_ESTABLISHED && sk2->err > 0)
1010 {
1011 err = -sk2->err;
1012 sk2->err=0;
1013 sk2->dead=1;
1014 destroy_sock(sk2);
1015 newsock->data = NULL;
1016 return(err);
1017 }
1018 newsock->state = SS_CONNECTED;
1019 return(0);
1020 }
1021
1022
1023
1024
1025
1026
1027 static int inet_getname(struct socket *sock, struct sockaddr *uaddr,
1028 int *uaddr_len, int peer)
1029 {
1030 struct sockaddr_in *sin=(struct sockaddr_in *)uaddr;
1031 struct sock *sk;
1032
1033 sin->sin_family = AF_INET;
1034 sk = (struct sock *) sock->data;
1035 if (peer)
1036 {
1037 if (!tcp_connected(sk->state))
1038 return(-ENOTCONN);
1039 sin->sin_port = sk->dummy_th.dest;
1040 sin->sin_addr.s_addr = sk->daddr;
1041 }
1042 else
1043 {
1044 sin->sin_port = sk->dummy_th.source;
1045 if (sk->saddr == 0)
1046 sin->sin_addr.s_addr = ip_my_addr();
1047 else
1048 sin->sin_addr.s_addr = sk->saddr;
1049 }
1050 *uaddr_len = sizeof(*sin);
1051 return(0);
1052 }
1053
1054
1055
1056
1057
1058
1059 static int inet_recvfrom(struct socket *sock, void *ubuf, int size, int noblock,
1060 unsigned flags, struct sockaddr *sin, int *addr_len )
1061 {
1062 struct sock *sk = (struct sock *) sock->data;
1063
1064 if (sk->prot->recvfrom == NULL)
1065 return(-EOPNOTSUPP);
1066 if(sk->err)
1067 return inet_error(sk);
1068
1069 if(inet_autobind(sk)!=0)
1070 return(-EAGAIN);
1071 return(sk->prot->recvfrom(sk, (unsigned char *) ubuf, size, noblock, flags,
1072 (struct sockaddr_in*)sin, addr_len));
1073 }
1074
1075
1076 static int inet_recv(struct socket *sock, void *ubuf, int size, int noblock,
1077 unsigned flags)
1078 {
1079
1080 return inet_recvfrom(sock,ubuf,size,noblock,flags,NULL,NULL);
1081 }
1082
1083 static int inet_read(struct socket *sock, char *ubuf, int size, int noblock)
1084 {
1085 struct sock *sk = (struct sock *) sock->data;
1086
1087 if(sk->err)
1088 return inet_error(sk);
1089
1090 if(inet_autobind(sk))
1091 return(-EAGAIN);
1092 return(sk->prot->read(sk, (unsigned char *) ubuf, size, noblock, 0));
1093 }
1094
1095 static int inet_send(struct socket *sock, void *ubuf, int size, int noblock,
1096 unsigned flags)
1097 {
1098 struct sock *sk = (struct sock *) sock->data;
1099 if (sk->shutdown & SEND_SHUTDOWN)
1100 {
1101 send_sig(SIGPIPE, current, 1);
1102 return(-EPIPE);
1103 }
1104 if(sk->err)
1105 return inet_error(sk);
1106
1107 if(inet_autobind(sk)!=0)
1108 return(-EAGAIN);
1109 return(sk->prot->write(sk, (unsigned char *) ubuf, size, noblock, flags));
1110 }
1111
1112 static int inet_write(struct socket *sock, char *ubuf, int size, int noblock)
1113 {
1114 return inet_send(sock,ubuf,size,noblock,0);
1115 }
1116
1117 static int inet_sendto(struct socket *sock, void *ubuf, int size, int noblock,
1118 unsigned flags, struct sockaddr *sin, int addr_len)
1119 {
1120 struct sock *sk = (struct sock *) sock->data;
1121 if (sk->shutdown & SEND_SHUTDOWN)
1122 {
1123 send_sig(SIGPIPE, current, 1);
1124 return(-EPIPE);
1125 }
1126 if (sk->prot->sendto == NULL)
1127 return(-EOPNOTSUPP);
1128 if(sk->err)
1129 return inet_error(sk);
1130
1131 if(inet_autobind(sk)!=0)
1132 return -EAGAIN;
1133 return(sk->prot->sendto(sk, (unsigned char *) ubuf, size, noblock, flags,
1134 (struct sockaddr_in *)sin, addr_len));
1135 }
1136
1137
1138 static int inet_shutdown(struct socket *sock, int how)
1139 {
1140 struct sock *sk=(struct sock*)sock->data;
1141
1142
1143
1144
1145
1146 how++;
1147
1148
1149 if ((how & ~SHUTDOWN_MASK) || how==0)
1150 return(-EINVAL);
1151 if (sock->state == SS_CONNECTING && sk->state == TCP_ESTABLISHED)
1152 sock->state = SS_CONNECTED;
1153 if (!tcp_connected(sk->state))
1154 return(-ENOTCONN);
1155 sk->shutdown |= how;
1156 if (sk->prot->shutdown)
1157 sk->prot->shutdown(sk, how);
1158 return(0);
1159 }
1160
1161
1162 static int inet_select(struct socket *sock, int sel_type, select_table *wait )
1163 {
1164 struct sock *sk=(struct sock *) sock->data;
1165 if (sk->prot->select == NULL)
1166 {
1167 return(0);
1168 }
1169 return(sk->prot->select(sk, sel_type, wait));
1170 }
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182 static int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1183 {
1184 struct sock *sk=(struct sock *)sock->data;
1185 int err;
1186
1187 switch(cmd)
1188 {
1189 case FIOSETOWN:
1190 case SIOCSPGRP:
1191 err=verify_area(VERIFY_READ,(int *)arg,sizeof(long));
1192 if(err)
1193 return err;
1194 sk->proc = get_fs_long((int *) arg);
1195 return(0);
1196 case FIOGETOWN:
1197 case SIOCGPGRP:
1198 err=verify_area(VERIFY_WRITE,(void *) arg, sizeof(long));
1199 if(err)
1200 return err;
1201 put_fs_long(sk->proc,(int *)arg);
1202 return(0);
1203 case SIOCGSTAMP:
1204 if(sk->stamp.tv_sec==0)
1205 return -ENOENT;
1206 err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(struct timeval));
1207 if(err)
1208 return err;
1209 memcpy_tofs((void *)arg,&sk->stamp,sizeof(struct timeval));
1210 return 0;
1211 case SIOCADDRT: case SIOCADDRTOLD:
1212 case SIOCDELRT: case SIOCDELRTOLD:
1213 return(ip_rt_ioctl(cmd,(void *) arg));
1214 case SIOCDARP:
1215 case SIOCGARP:
1216 case SIOCSARP:
1217 return(arp_ioctl(cmd,(void *) arg));
1218 #ifdef CONFIG_INET_RARP
1219 case SIOCDRARP:
1220 case SIOCGRARP:
1221 case SIOCSRARP:
1222 return(rarp_ioctl(cmd,(void *) arg));
1223 #endif
1224 case SIOCGIFCONF:
1225 case SIOCGIFFLAGS:
1226 case SIOCSIFFLAGS:
1227 case SIOCGIFADDR:
1228 case SIOCSIFADDR:
1229 case SIOCGIFDSTADDR:
1230 case SIOCSIFDSTADDR:
1231 case SIOCGIFBRDADDR:
1232 case SIOCSIFBRDADDR:
1233 case SIOCGIFNETMASK:
1234 case SIOCSIFNETMASK:
1235 case SIOCGIFMETRIC:
1236 case SIOCSIFMETRIC:
1237 case SIOCGIFMEM:
1238 case SIOCSIFMEM:
1239 case SIOCGIFMTU:
1240 case SIOCSIFMTU:
1241 case SIOCSIFLINK:
1242 case SIOCGIFHWADDR:
1243 case SIOCSIFHWADDR:
1244 case OLD_SIOCGIFHWADDR:
1245 case SIOCSIFMAP:
1246 case SIOCGIFMAP:
1247 case SIOCDEVPRIVATE:
1248 case SIOCSIFSLAVE:
1249 case SIOCGIFSLAVE:
1250 return(dev_ioctl(cmd,(void *) arg));
1251
1252 default:
1253 if (sk->prot->ioctl==NULL)
1254 return(-EINVAL);
1255 return(sk->prot->ioctl(sk, cmd, arg));
1256 }
1257
1258 return(0);
1259 }
1260
1261
1262
1263
1264
1265
1266 struct sock *get_sock(struct proto *prot, unsigned short num,
1267 unsigned long raddr,
1268 unsigned short rnum, unsigned long laddr)
1269 {
1270 struct sock *s;
1271 unsigned short hnum;
1272
1273 hnum = ntohs(num);
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284 for(s = prot->sock_array[hnum & (SOCK_ARRAY_SIZE - 1)];
1285 s != NULL; s = s->next)
1286 {
1287 if (s->num != hnum)
1288 continue;
1289 if(s->dead && (s->state == TCP_CLOSE))
1290 continue;
1291 if(prot == &udp_prot)
1292 return s;
1293 if(ip_addr_match(s->daddr,raddr)==0)
1294 continue;
1295 if (s->dummy_th.dest != rnum && s->dummy_th.dest != 0)
1296 continue;
1297 if(ip_addr_match(s->saddr,laddr) == 0)
1298 continue;
1299 return(s);
1300 }
1301 return(NULL);
1302 }
1303
1304 static struct proto_ops inet_proto_ops = {
1305 AF_INET,
1306
1307 inet_create,
1308 inet_dup,
1309 inet_release,
1310 inet_bind,
1311 inet_connect,
1312 inet_socketpair,
1313 inet_accept,
1314 inet_getname,
1315 inet_read,
1316 inet_write,
1317 inet_select,
1318 inet_ioctl,
1319 inet_listen,
1320 inet_send,
1321 inet_recv,
1322 inet_sendto,
1323 inet_recvfrom,
1324 inet_shutdown,
1325 inet_setsockopt,
1326 inet_getsockopt,
1327 inet_fcntl,
1328 };
1329
1330 extern unsigned long seq_offset;
1331
1332
1333
1334
1335
1336 void inet_proto_init(struct net_proto *pro)
1337 {
1338 struct inet_protocol *p;
1339 int i;
1340
1341
1342 printk("Swansea University Computer Society TCP/IP for NET3.017\n");
1343
1344
1345
1346
1347
1348 (void) sock_register(inet_proto_ops.family, &inet_proto_ops);
1349
1350 seq_offset = CURRENT_TIME*250;
1351
1352
1353
1354
1355
1356 for(i = 0; i < SOCK_ARRAY_SIZE; i++)
1357 {
1358 tcp_prot.sock_array[i] = NULL;
1359 udp_prot.sock_array[i] = NULL;
1360 raw_prot.sock_array[i] = NULL;
1361 }
1362
1363 printk("IP Protocols: ");
1364 for(p = inet_protocol_base; p != NULL;)
1365 {
1366 struct inet_protocol *tmp = (struct inet_protocol *) p->next;
1367 inet_add_protocol(p);
1368 printk("%s%s",p->name,tmp?", ":"\n");
1369 p = tmp;
1370 }
1371
1372
1373
1374 arp_init();
1375
1376
1377
1378 ip_init();
1379 }
1380