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