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