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