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