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