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