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