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