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