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