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