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