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