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