This source file includes following definitions.
- ip_ioctl
- ip_send
- ip_send_room
- ip_build_header
- ip_send_check
- ip_frag_create
- ip_find
- ip_free
- ip_expire
- ip_create
- ip_done
- ip_glue
- ip_defrag
- ip_fragment
- ip_forward
- ip_rcv
- ip_loopback
- ip_queue_xmit
- ip_mc_procinfo
- ip_mc_find_devfor
- ip_setsockopt
- ip_getsockopt
- ip_build_xmit
- ip_rt_event
- ip_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 #include <asm/segment.h>
119 #include <asm/system.h>
120 #include <linux/types.h>
121 #include <linux/kernel.h>
122 #include <linux/sched.h>
123 #include <linux/mm.h>
124 #include <linux/string.h>
125 #include <linux/errno.h>
126 #include <linux/config.h>
127
128 #include <linux/socket.h>
129 #include <linux/sockios.h>
130 #include <linux/in.h>
131 #include <linux/inet.h>
132 #include <linux/netdevice.h>
133 #include <linux/etherdevice.h>
134 #include <linux/proc_fs.h>
135 #include <linux/stat.h>
136
137 #include <net/snmp.h>
138 #include <net/ip.h>
139 #include <net/protocol.h>
140 #include <net/route.h>
141 #include <net/tcp.h>
142 #include <net/udp.h>
143 #include <linux/skbuff.h>
144 #include <net/sock.h>
145 #include <net/arp.h>
146 #include <net/icmp.h>
147 #include <net/raw.h>
148 #include <net/checksum.h>
149 #include <linux/igmp.h>
150 #include <linux/ip_fw.h>
151 #include <linux/mroute.h>
152
153 #define CONFIG_IP_DEFRAG
154
155 extern int last_retran;
156 extern void sort_send(struct sock *sk);
157
158 #define min(a,b) ((a)<(b)?(a):(b))
159
160
161
162
163
164 #ifdef CONFIG_IP_FORWARD
165 struct ip_mib ip_statistics={1,64,};
166 #else
167 struct ip_mib ip_statistics={2,64,};
168 #endif
169
170
171
172
173
174
175
176 int ip_ioctl(struct sock *sk, int cmd, unsigned long arg)
177 {
178 switch(cmd)
179 {
180 default:
181 return(-EINVAL);
182 }
183 }
184
185
186
187
188
189
190 static int ip_send(struct sk_buff *skb, __u32 daddr, int len, struct device *dev, __u32 saddr)
191 {
192 int mac = 0;
193
194 skb->dev = dev;
195 skb->arp = 1;
196 if (dev->hard_header)
197 {
198
199
200
201
202 skb_reserve(skb,(dev->hard_header_len+15)&~15);
203 mac = dev->hard_header(skb, dev, ETH_P_IP, NULL, NULL, len);
204 if (mac < 0)
205 {
206 mac = -mac;
207 skb->arp = 0;
208 skb->raddr = daddr;
209 }
210 }
211 return mac;
212 }
213
214 static int ip_send_room(struct sk_buff *skb, __u32 daddr, int len, struct device *dev, __u32 saddr)
215 {
216 int mac = 0;
217
218 skb->dev = dev;
219 skb->arp = 1;
220 if (dev->hard_header)
221 {
222 skb_reserve(skb,MAX_HEADER);
223 mac = dev->hard_header(skb, dev, ETH_P_IP, NULL, NULL, len);
224 if (mac < 0)
225 {
226 mac = -mac;
227 skb->arp = 0;
228 skb->raddr = daddr;
229 }
230 }
231 return mac;
232 }
233
234 int ip_id_count = 0;
235
236
237
238
239
240
241
242 int ip_build_header(struct sk_buff *skb, __u32 saddr, __u32 daddr,
243 struct device **dev, int type, struct options *opt, int len, int tos, int ttl)
244 {
245 struct rtable *rt;
246 __u32 raddr;
247 int tmp;
248 __u32 src;
249 struct iphdr *iph;
250
251
252
253
254
255 #ifdef CONFIG_IP_MULTICAST
256 if(MULTICAST(daddr) && *dev==NULL && skb->sk && *skb->sk->ip_mc_name)
257 *dev=dev_get(skb->sk->ip_mc_name);
258 #endif
259 if (*dev == NULL)
260 {
261 if(skb->localroute)
262 rt = ip_rt_local(daddr, NULL, &src);
263 else
264 rt = ip_rt_route(daddr, NULL, &src);
265 if (rt == NULL)
266 {
267 ip_statistics.IpOutNoRoutes++;
268 return(-ENETUNREACH);
269 }
270
271 *dev = rt->rt_dev;
272
273
274
275
276 if (LOOPBACK(saddr) && !LOOPBACK(daddr))
277 saddr = src;
278 raddr = rt->rt_gateway;
279
280 }
281 else
282 {
283
284
285
286 if(skb->localroute)
287 rt = ip_rt_local(daddr, NULL, &src);
288 else
289 rt = ip_rt_route(daddr, NULL, &src);
290
291
292
293
294 if (LOOPBACK(saddr) && !LOOPBACK(daddr))
295 saddr = src;
296
297 raddr = (rt == NULL) ? 0 : rt->rt_gateway;
298 }
299
300
301
302
303 if (saddr == 0)
304 saddr = src;
305
306
307
308
309 if (raddr == 0)
310 raddr = daddr;
311
312
313
314
315
316 if(type==IPPROTO_TCP)
317 tmp = ip_send_room(skb, raddr, len, *dev, saddr);
318 else
319 tmp = ip_send(skb, raddr, len, *dev, saddr);
320
321
322
323
324
325 skb->dev = *dev;
326 skb->saddr = saddr;
327 if (skb->sk)
328 skb->sk->saddr = saddr;
329
330
331
332
333
334
335
336
337
338
339 if(type == IPPROTO_RAW)
340 return (tmp);
341
342
343
344
345
346 iph=(struct iphdr *)skb_put(skb,sizeof(struct iphdr));
347
348 iph->version = 4;
349 iph->ihl = 5;
350 iph->tos = tos;
351 iph->frag_off = 0;
352 iph->ttl = ttl;
353 iph->daddr = daddr;
354 iph->saddr = saddr;
355 iph->protocol = type;
356 skb->ip_hdr = iph;
357
358 return(20 + tmp);
359 }
360
361
362
363
364
365
366 void ip_send_check(struct iphdr *iph)
367 {
368 iph->check = 0;
369 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
370 }
371
372
373
374
375
376
377
378
379
380 static struct ipq *ipqueue = NULL;
381
382
383
384
385
386 static struct ipfrag *ip_frag_create(int offset, int end, struct sk_buff *skb, unsigned char *ptr)
387 {
388 struct ipfrag *fp;
389
390 fp = (struct ipfrag *) kmalloc(sizeof(struct ipfrag), GFP_ATOMIC);
391 if (fp == NULL)
392 {
393 NETDEBUG(printk("IP: frag_create: no memory left !\n"));
394 return(NULL);
395 }
396 memset(fp, 0, sizeof(struct ipfrag));
397
398
399 fp->offset = offset;
400 fp->end = end;
401 fp->len = end - offset;
402 fp->skb = skb;
403 fp->ptr = ptr;
404
405 return(fp);
406 }
407
408
409
410
411
412
413
414 static struct ipq *ip_find(struct iphdr *iph)
415 {
416 struct ipq *qp;
417 struct ipq *qplast;
418
419 cli();
420 qplast = NULL;
421 for(qp = ipqueue; qp != NULL; qplast = qp, qp = qp->next)
422 {
423 if (iph->id== qp->iph->id && iph->saddr == qp->iph->saddr &&
424 iph->daddr == qp->iph->daddr && iph->protocol == qp->iph->protocol)
425 {
426 del_timer(&qp->timer);
427 sti();
428 return(qp);
429 }
430 }
431 sti();
432 return(NULL);
433 }
434
435
436
437
438
439
440
441
442 static void ip_free(struct ipq *qp)
443 {
444 struct ipfrag *fp;
445 struct ipfrag *xp;
446
447
448
449
450
451 del_timer(&qp->timer);
452
453
454 cli();
455 if (qp->prev == NULL)
456 {
457 ipqueue = qp->next;
458 if (ipqueue != NULL)
459 ipqueue->prev = NULL;
460 }
461 else
462 {
463 qp->prev->next = qp->next;
464 if (qp->next != NULL)
465 qp->next->prev = qp->prev;
466 }
467
468
469
470 fp = qp->fragments;
471 while (fp != NULL)
472 {
473 xp = fp->next;
474 IS_SKB(fp->skb);
475 kfree_skb(fp->skb,FREE_READ);
476 kfree_s(fp, sizeof(struct ipfrag));
477 fp = xp;
478 }
479
480
481 kfree_s(qp->iph, 64 + 8);
482
483
484 kfree_s(qp, sizeof(struct ipq));
485 sti();
486 }
487
488
489
490
491
492
493 static void ip_expire(unsigned long arg)
494 {
495 struct ipq *qp;
496
497 qp = (struct ipq *)arg;
498
499
500
501
502
503 ip_statistics.IpReasmTimeout++;
504 ip_statistics.IpReasmFails++;
505
506 if(qp->fragments!=NULL)
507 icmp_send(qp->fragments->skb,ICMP_TIME_EXCEEDED,
508 ICMP_EXC_FRAGTIME, 0, qp->dev);
509
510
511
512
513 ip_free(qp);
514 }
515
516
517
518
519
520
521
522
523
524 static struct ipq *ip_create(struct sk_buff *skb, struct iphdr *iph, struct device *dev)
525 {
526 struct ipq *qp;
527 int ihlen;
528
529 qp = (struct ipq *) kmalloc(sizeof(struct ipq), GFP_ATOMIC);
530 if (qp == NULL)
531 {
532 NETDEBUG(printk("IP: create: no memory left !\n"));
533 return(NULL);
534 skb->dev = qp->dev;
535 }
536 memset(qp, 0, sizeof(struct ipq));
537
538
539
540
541
542 ihlen = iph->ihl * 4;
543 qp->iph = (struct iphdr *) kmalloc(64 + 8, GFP_ATOMIC);
544 if (qp->iph == NULL)
545 {
546 NETDEBUG(printk("IP: create: no memory left !\n"));
547 kfree_s(qp, sizeof(struct ipq));
548 return(NULL);
549 }
550
551 memcpy(qp->iph, iph, ihlen + 8);
552 qp->len = 0;
553 qp->ihlen = ihlen;
554 qp->fragments = NULL;
555 qp->dev = dev;
556
557
558 qp->timer.expires = jiffies + IP_FRAG_TIME;
559 qp->timer.data = (unsigned long) qp;
560 qp->timer.function = ip_expire;
561 add_timer(&qp->timer);
562
563
564 qp->prev = NULL;
565 cli();
566 qp->next = ipqueue;
567 if (qp->next != NULL)
568 qp->next->prev = qp;
569 ipqueue = qp;
570 sti();
571 return(qp);
572 }
573
574
575
576
577
578
579 static int ip_done(struct ipq *qp)
580 {
581 struct ipfrag *fp;
582 int offset;
583
584
585 if (qp->len == 0)
586 return(0);
587
588
589 fp = qp->fragments;
590 offset = 0;
591 while (fp != NULL)
592 {
593 if (fp->offset > offset)
594 return(0);
595 offset = fp->end;
596 fp = fp->next;
597 }
598
599
600 return(1);
601 }
602
603
604
605
606
607
608
609
610
611
612 static struct sk_buff *ip_glue(struct ipq *qp)
613 {
614 struct sk_buff *skb;
615 struct iphdr *iph;
616 struct ipfrag *fp;
617 unsigned char *ptr;
618 int count, len;
619
620
621
622
623 len = qp->ihlen + qp->len;
624
625 if ((skb = dev_alloc_skb(len)) == NULL)
626 {
627 ip_statistics.IpReasmFails++;
628 NETDEBUG(printk("IP: queue_glue: no memory for gluing queue %p\n", qp));
629 ip_free(qp);
630 return(NULL);
631 }
632
633
634 skb_put(skb,len);
635 skb->h.raw = skb->data;
636 skb->free = 1;
637
638
639 ptr = (unsigned char *) skb->h.raw;
640 memcpy(ptr, ((unsigned char *) qp->iph), qp->ihlen);
641 ptr += qp->ihlen;
642
643 count = 0;
644
645
646 fp = qp->fragments;
647 while(fp != NULL)
648 {
649 if(count+fp->len > skb->len)
650 {
651 NETDEBUG(printk("Invalid fragment list: Fragment over size.\n"));
652 ip_free(qp);
653 kfree_skb(skb,FREE_WRITE);
654 ip_statistics.IpReasmFails++;
655 return NULL;
656 }
657 memcpy((ptr + fp->offset), fp->ptr, fp->len);
658 count += fp->len;
659 fp = fp->next;
660 }
661
662
663 ip_free(qp);
664
665
666 iph = skb->h.iph;
667 iph->frag_off = 0;
668 iph->tot_len = htons((iph->ihl * 4) + count);
669 skb->ip_hdr = iph;
670
671 ip_statistics.IpReasmOKs++;
672 return(skb);
673 }
674
675
676
677
678
679
680 static struct sk_buff *ip_defrag(struct iphdr *iph, struct sk_buff *skb, struct device *dev)
681 {
682 struct ipfrag *prev, *next, *tmp;
683 struct ipfrag *tfp;
684 struct ipq *qp;
685 struct sk_buff *skb2;
686 unsigned char *ptr;
687 int flags, offset;
688 int i, ihl, end;
689
690 ip_statistics.IpReasmReqds++;
691
692
693 qp = ip_find(iph);
694
695
696 offset = ntohs(iph->frag_off);
697 flags = offset & ~IP_OFFSET;
698 offset &= IP_OFFSET;
699 if (((flags & IP_MF) == 0) && (offset == 0))
700 {
701 if (qp != NULL)
702 ip_free(qp);
703 return(skb);
704 }
705
706 offset <<= 3;
707
708
709
710
711
712
713
714 if (qp != NULL)
715 {
716 del_timer(&qp->timer);
717 qp->timer.expires = jiffies + IP_FRAG_TIME;
718 qp->timer.data = (unsigned long) qp;
719 qp->timer.function = ip_expire;
720 add_timer(&qp->timer);
721 }
722 else
723 {
724
725
726
727 if ((qp = ip_create(skb, iph, dev)) == NULL)
728 {
729 skb->sk = NULL;
730 kfree_skb(skb, FREE_READ);
731 ip_statistics.IpReasmFails++;
732 return NULL;
733 }
734 }
735
736
737
738
739
740 ihl = iph->ihl * 4;
741 end = offset + ntohs(iph->tot_len) - ihl;
742
743
744
745
746
747 ptr = skb->data + ihl;
748
749
750
751
752
753 if ((flags & IP_MF) == 0)
754 qp->len = end;
755
756
757
758
759
760
761
762 prev = NULL;
763 for(next = qp->fragments; next != NULL; next = next->next)
764 {
765 if (next->offset > offset)
766 break;
767 prev = next;
768 }
769
770
771
772
773
774
775 if (prev != NULL && offset < prev->end)
776 {
777 i = prev->end - offset;
778 offset += i;
779 ptr += i;
780 }
781
782
783
784
785
786
787 for(tmp=next; tmp != NULL; tmp = tfp)
788 {
789 tfp = tmp->next;
790 if (tmp->offset >= end)
791 break;
792
793 i = end - next->offset;
794 tmp->len -= i;
795 tmp->offset += i;
796 tmp->ptr += i;
797
798
799
800
801 if (tmp->len <= 0)
802 {
803 if (tmp->prev != NULL)
804 tmp->prev->next = tmp->next;
805 else
806 qp->fragments = tmp->next;
807
808 if (tfp->next != NULL)
809 tmp->next->prev = tmp->prev;
810
811 next=tfp;
812
813 kfree_skb(tmp->skb,FREE_READ);
814 kfree_s(tmp, sizeof(struct ipfrag));
815 }
816 }
817
818
819
820
821
822 tfp = NULL;
823 tfp = ip_frag_create(offset, end, skb, ptr);
824
825
826
827
828
829 if (!tfp)
830 {
831 skb->sk = NULL;
832 kfree_skb(skb, FREE_READ);
833 return NULL;
834 }
835 tfp->prev = prev;
836 tfp->next = next;
837 if (prev != NULL)
838 prev->next = tfp;
839 else
840 qp->fragments = tfp;
841
842 if (next != NULL)
843 next->prev = tfp;
844
845
846
847
848
849
850
851 if (ip_done(qp))
852 {
853 skb2 = ip_glue(qp);
854 return(skb2);
855 }
856 return(NULL);
857 }
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874 void ip_fragment(struct sock *sk, struct sk_buff *skb, struct device *dev, int is_frag)
875 {
876 struct iphdr *iph;
877 unsigned char *raw;
878 unsigned char *ptr;
879 struct sk_buff *skb2;
880 int left, mtu, hlen, len;
881 int offset;
882 unsigned long flags;
883
884
885
886
887
888 raw = skb->data;
889 #if 0
890 iph = (struct iphdr *) (raw + dev->hard_header_len);
891 skb->ip_hdr = iph;
892 #else
893 iph = skb->ip_hdr;
894 #endif
895
896
897
898
899
900 hlen = iph->ihl * 4;
901 left = ntohs(iph->tot_len) - hlen;
902 hlen += dev->hard_header_len;
903 mtu = (dev->mtu - hlen);
904 ptr = (raw + hlen);
905
906
907
908
909
910 if (ntohs(iph->frag_off) & IP_DF)
911 {
912
913
914
915 ip_statistics.IpFragFails++;
916 icmp_send(skb,ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, dev->mtu, dev);
917 return;
918 }
919
920
921
922
923
924
925
926 if(mtu<8)
927 {
928
929 icmp_send(skb,ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED,dev->mtu, dev);
930 ip_statistics.IpFragFails++;
931 return;
932 }
933
934
935
936
937
938
939
940
941
942
943 if (is_frag & 2)
944 offset = (ntohs(iph->frag_off) & IP_OFFSET) << 3;
945 else
946 offset = 0;
947
948
949
950
951
952
953 while(left > 0)
954 {
955 len = left;
956
957 if (len > mtu)
958 len = mtu;
959
960
961 if (len < left)
962 {
963 len/=8;
964 len*=8;
965 }
966
967
968
969
970 if ((skb2 = alloc_skb(len + hlen+15,GFP_ATOMIC)) == NULL)
971 {
972 NETDEBUG(printk("IP: frag: no memory for new fragment!\n"));
973 ip_statistics.IpFragFails++;
974 return;
975 }
976
977
978
979
980
981 skb2->arp = skb->arp;
982 if(skb->free==0)
983 printk("IP fragmenter: BUG free!=1 in fragmenter\n");
984 skb2->free = 1;
985 skb_put(skb2,len + hlen);
986 skb2->h.raw=(char *) skb2->data;
987
988
989
990
991
992 save_flags(flags);
993 if (sk)
994 {
995 cli();
996 sk->wmem_alloc += skb2->truesize;
997 skb2->sk=sk;
998 }
999 restore_flags(flags);
1000 skb2->raddr = skb->raddr;
1001
1002
1003
1004
1005
1006 memcpy(skb2->h.raw, raw, hlen);
1007
1008
1009
1010
1011 memcpy(skb2->h.raw + hlen, ptr, len);
1012 left -= len;
1013
1014 skb2->h.raw+=dev->hard_header_len;
1015
1016
1017
1018
1019 iph = (struct iphdr *)(skb2->h.raw);
1020 iph->frag_off = htons((offset >> 3));
1021 skb2->ip_hdr = iph;
1022
1023
1024
1025
1026 if (left > 0 || (is_frag & 1))
1027 iph->frag_off |= htons(IP_MF);
1028 ptr += len;
1029 offset += len;
1030
1031
1032
1033
1034
1035 ip_statistics.IpFragCreates++;
1036
1037 ip_queue_xmit(sk, dev, skb2, 2);
1038 }
1039 ip_statistics.IpFragOKs++;
1040 }
1041
1042
1043
1044 #ifdef CONFIG_IP_FORWARD
1045
1046
1047
1048
1049
1050 int ip_forward(struct sk_buff *skb, struct device *dev, int is_frag, unsigned long target_addr, int target_strict)
1051 {
1052 struct device *dev2;
1053 struct iphdr *iph;
1054 struct sk_buff *skb2;
1055 struct rtable *rt;
1056 unsigned char *ptr;
1057 unsigned long raddr;
1058 #ifdef CONFIG_IP_FIREWALL
1059 int fw_res = 0;
1060 #ifdef CONFIG_IP_MASQUERADE
1061 struct sk_buff *skb_in = skb;
1062 #endif
1063
1064
1065
1066
1067
1068
1069
1070 if(!(is_frag&4))
1071 {
1072 fw_res=ip_fw_chk(skb->h.iph, dev, ip_fw_fwd_chain, ip_fw_fwd_policy, 0);
1073 switch (fw_res) {
1074 case FW_ACCEPT:
1075 #ifdef CONFIG_IP_MASQUERADE
1076 case FW_MASQUERADE:
1077 #endif
1078 break;
1079 case FW_REJECT:
1080 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0, dev);
1081
1082 default:
1083 return -1;
1084 }
1085 }
1086 #endif
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098 iph = skb->h.iph;
1099 iph->ttl--;
1100
1101
1102
1103
1104
1105
1106
1107 iph->check = ntohs(iph->check) + 0x0100;
1108 if ((iph->check & 0xFF00) == 0)
1109 iph->check++;
1110 iph->check = htons(iph->check);
1111
1112 if (iph->ttl <= 0)
1113 {
1114
1115 icmp_send(skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, 0, dev);
1116 return -1;
1117 }
1118
1119
1120
1121
1122
1123
1124 rt = ip_rt_route(target_addr, NULL, NULL);
1125 if (rt == NULL)
1126 {
1127
1128
1129
1130
1131 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_UNREACH, 0, dev);
1132 return -1;
1133 }
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144 raddr = rt->rt_gateway;
1145
1146 if (raddr != 0)
1147 {
1148
1149
1150
1151
1152 if(target_strict)
1153 {
1154 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_SR_FAILED, 0, dev);
1155 return -1;
1156 }
1157
1158
1159
1160
1161
1162
1163 rt = ip_rt_route(raddr, NULL, NULL);
1164 if (rt == NULL)
1165 {
1166
1167
1168
1169 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0, dev);
1170 return -1;
1171 }
1172 if (rt->rt_gateway != 0)
1173 raddr = rt->rt_gateway;
1174 }
1175 else
1176 raddr = target_addr;
1177
1178
1179
1180
1181
1182 dev2 = rt->rt_dev;
1183
1184
1185
1186
1187
1188
1189 #ifndef CONFIG_IP_NO_ICMP_REDIRECT
1190 if (dev == dev2 && !((iph->saddr^iph->daddr)&dev->pa_mask) && (rt->rt_flags&RTF_MODIFIED))
1191 icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, raddr, dev);
1192 #endif
1193
1194
1195
1196
1197
1198
1199 if (dev2->flags & IFF_UP)
1200 {
1201 #ifdef CONFIG_IP_MASQUERADE
1202
1203
1204
1205
1206 if (!(is_frag&4) && fw_res==2)
1207 ip_fw_masquerade(&skb, dev2);
1208 #endif
1209 IS_SKB(skb);
1210
1211 if(skb_headroom(skb)<dev2->hard_header_len)
1212 {
1213 skb2 = alloc_skb(dev2->hard_header_len + skb->len + 15, GFP_ATOMIC);
1214 IS_SKB(skb2);
1215
1216
1217
1218
1219
1220
1221 if (skb2 == NULL)
1222 {
1223 NETDEBUG(printk("\nIP: No memory available for IP forward\n"));
1224 return -1;
1225 }
1226
1227
1228
1229
1230
1231 ip_send(skb2,raddr,skb->len,dev2,dev2->pa_addr);
1232
1233
1234
1235
1236
1237
1238 ptr = skb_put(skb2,skb->len);
1239 skb2->free = 1;
1240 skb2->h.raw = ptr;
1241
1242
1243
1244
1245 memcpy(ptr, skb->h.raw, skb->len);
1246 }
1247 else
1248 {
1249
1250
1251
1252
1253 skb2 = skb;
1254 skb2->dev=dev2;
1255 skb->arp=1;
1256 skb->raddr=raddr;
1257 if(dev2->hard_header)
1258 {
1259 if(dev2->hard_header(skb, dev2, ETH_P_IP, NULL, NULL, skb->len)<0)
1260 skb->arp=0;
1261 }
1262 ip_statistics.IpForwDatagrams++;
1263 }
1264
1265
1266
1267
1268
1269
1270 if(skb2->len > dev2->mtu + dev2->hard_header_len)
1271 {
1272 ip_fragment(NULL,skb2,dev2, is_frag);
1273 kfree_skb(skb2,FREE_WRITE);
1274 }
1275 else
1276 {
1277 #ifdef CONFIG_IP_ACCT
1278
1279
1280
1281
1282 ip_fw_chk(iph,dev,ip_acct_chain,IP_FW_F_ACCEPT,1);
1283 #endif
1284
1285
1286
1287
1288
1289
1290 if(iph->tos & IPTOS_LOWDELAY)
1291 dev_queue_xmit(skb2, dev2, SOPRI_INTERACTIVE);
1292 else if(iph->tos & IPTOS_THROUGHPUT)
1293 dev_queue_xmit(skb2, dev2, SOPRI_BACKGROUND);
1294 else
1295 dev_queue_xmit(skb2, dev2, SOPRI_NORMAL);
1296 }
1297 }
1298 else
1299 return -1;
1300
1301
1302
1303
1304
1305 if(skb==skb2)
1306 return 0;
1307
1308 #ifdef CONFIG_IP_MASQUERADE
1309
1310
1311
1312
1313 if(skb!=skb_in)
1314 {
1315 kfree_skb(skb_in, FREE_WRITE);
1316 return 0;
1317 }
1318 #endif
1319 return 1;
1320 }
1321
1322
1323 #endif
1324
1325
1326
1327
1328
1329
1330
1331
1332 int ip_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
1333 {
1334 struct iphdr *iph = skb->h.iph;
1335 struct sock *raw_sk=NULL;
1336 unsigned char hash;
1337 unsigned char flag = 0;
1338 struct inet_protocol *ipprot;
1339 int brd=IS_MYADDR;
1340 unsigned long target_addr;
1341 int target_strict=0;
1342 int is_frag=0;
1343 #ifdef CONFIG_IP_FIREWALL
1344 int err;
1345 #endif
1346
1347 #ifdef CONFIG_NET_IPV6
1348
1349
1350
1351
1352 if(iph->version == 6)
1353 return ipv6_rcv(skb,dev,pt);
1354 #endif
1355
1356 ip_statistics.IpInReceives++;
1357
1358
1359
1360
1361
1362 skb->ip_hdr = iph;
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377 if (skb->len<sizeof(struct iphdr) || iph->ihl<5 || iph->version != 4 || ip_fast_csum((unsigned char *)iph, iph->ihl) !=0
1378 || skb->len < ntohs(iph->tot_len))
1379 {
1380 ip_statistics.IpInHdrErrors++;
1381 kfree_skb(skb, FREE_WRITE);
1382 return(0);
1383 }
1384
1385
1386
1387
1388
1389
1390
1391 skb_trim(skb,ntohs(iph->tot_len));
1392
1393
1394
1395
1396
1397 #ifdef CONFIG_IP_FIREWALL
1398
1399 if ((err=ip_fw_chk(iph,dev,ip_fw_blk_chain,ip_fw_blk_policy, 0))<FW_ACCEPT)
1400 {
1401 if(err==FW_REJECT)
1402 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0, dev);
1403 kfree_skb(skb, FREE_WRITE);
1404 return 0;
1405 }
1406
1407 #endif
1408
1409
1410
1411
1412
1413
1414
1415 target_addr = iph->daddr;
1416
1417 if (iph->ihl != 5)
1418 {
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429 int opt_space=4*(iph->ihl-5);
1430 int opt_size;
1431 unsigned char *opt_ptr=skb->h.raw+sizeof(struct iphdr);
1432
1433 skb->ip_summed=0;
1434
1435 while(opt_space>0)
1436 {
1437 if(*opt_ptr==IPOPT_NOOP)
1438 {
1439 opt_ptr++;
1440 opt_space--;
1441 continue;
1442 }
1443 if(*opt_ptr==IPOPT_END)
1444 break;
1445 if(opt_space<2 || (opt_size=opt_ptr[1])<2 || opt_ptr[1]>opt_space)
1446 {
1447
1448
1449
1450 icmp_send(skb, ICMP_PARAMETERPROB, 0, 0, skb->dev);
1451 kfree_skb(skb, FREE_READ);
1452 return -EINVAL;
1453 }
1454 switch(opt_ptr[0])
1455 {
1456 case IPOPT_SEC:
1457
1458 break;
1459 case IPOPT_SSRR:
1460 target_strict=1;
1461
1462 case IPOPT_LSRR:
1463 #ifdef CONFIG_IP_NOSR
1464 kfree_skb(skb, FREE_READ);
1465 return -EINVAL;
1466 #endif
1467 case IPOPT_RR:
1468
1469
1470
1471 if (iph->daddr!=skb->dev->pa_addr && (brd = ip_chk_addr(iph->daddr)) == 0)
1472 break;
1473 if((opt_size<3) || ( opt_ptr[0]==IPOPT_RR && opt_ptr[2] > opt_size-4 ))
1474 {
1475 if(ip_chk_addr(iph->daddr))
1476 icmp_send(skb, ICMP_PARAMETERPROB, 0, 0, skb->dev);
1477 kfree_skb(skb, FREE_READ);
1478 return -EINVAL;
1479 }
1480 if(opt_ptr[2] > opt_size-4 )
1481 break;
1482
1483
1484 if(opt_ptr[0]!=IPOPT_RR)
1485 {
1486 int t;
1487 target_addr=*(u32 *)(&opt_ptr[opt_ptr[2]]);
1488 t=ip_chk_addr(target_addr);
1489 if(t==IS_MULTICAST||t==IS_BROADCAST)
1490 {
1491 if(ip_chk_addr(iph->daddr))
1492 icmp_send(skb, ICMP_PARAMETERPROB, 0, 0, skb->dev);
1493 kfree_skb(skb,FREE_READ);
1494 return -EINVAL;
1495 }
1496 }
1497 *(u32 *)(&opt_ptr[opt_ptr[2]])=skb->dev->pa_addr;
1498 break;
1499 case IPOPT_TIMESTAMP:
1500
1501
1502
1503
1504 NETDEBUG(printk("ICMP: Someone finish the timestamp routine ;)\n"));
1505 break;
1506 default:
1507 break;
1508 }
1509 opt_ptr+=opt_size;
1510 opt_space-=opt_size;
1511 }
1512
1513 }
1514
1515
1516
1517
1518
1519
1520 if(iph->frag_off)
1521 {
1522 if (iph->frag_off & htons(IP_MF))
1523 is_frag|=1;
1524
1525
1526
1527
1528 if (iph->frag_off & htons(IP_OFFSET))
1529 is_frag|=2;
1530 }
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544 if ( iph->daddr == skb->dev->pa_addr || (brd = ip_chk_addr(iph->daddr)) != 0)
1545 {
1546 #ifdef CONFIG_IP_MULTICAST
1547 if(!(dev->flags&IFF_ALLMULTI) && brd==IS_MULTICAST && iph->daddr!=IGMP_ALL_HOSTS && !(dev->flags&IFF_LOOPBACK))
1548 {
1549
1550
1551
1552 struct ip_mc_list *ip_mc=dev->ip_mc_list;
1553 do
1554 {
1555 if(ip_mc==NULL)
1556 {
1557 kfree_skb(skb, FREE_WRITE);
1558 return 0;
1559 }
1560 if(ip_mc->multiaddr==iph->daddr)
1561 break;
1562 ip_mc=ip_mc->next;
1563 }
1564 while(1);
1565 }
1566 #endif
1567
1568 #ifdef CONFIG_IP_MASQUERADE
1569
1570
1571
1572 if (ip_fw_demasquerade(skb))
1573 {
1574 struct iphdr *iph=skb->h.iph;
1575 if(ip_forward(skb, dev, is_frag|4, iph->daddr, 0))
1576 kfree_skb(skb, FREE_WRITE);
1577 return(0);
1578 }
1579 #endif
1580
1581
1582
1583
1584
1585 #ifdef CONFIG_IP_ACCT
1586 ip_fw_chk(iph,dev,ip_acct_chain,IP_FW_F_ACCEPT,1);
1587 #endif
1588
1589
1590
1591
1592
1593 if(is_frag)
1594 {
1595
1596 skb=ip_defrag(iph,skb,dev);
1597 if(skb==NULL)
1598 return 0;
1599 skb->dev = dev;
1600 iph=skb->h.iph;
1601 }
1602
1603
1604
1605
1606
1607 skb->ip_hdr = iph;
1608 skb->h.raw += iph->ihl*4;
1609
1610
1611
1612
1613
1614
1615
1616 hash = iph->protocol & (SOCK_ARRAY_SIZE-1);
1617
1618
1619
1620
1621
1622 if((raw_sk=raw_prot.sock_array[hash])!=NULL)
1623 {
1624 struct sock *sknext=NULL;
1625 struct sk_buff *skb1;
1626 raw_sk=get_sock_raw(raw_sk, iph->protocol, iph->saddr, iph->daddr);
1627 if(raw_sk)
1628 {
1629 do
1630 {
1631
1632 sknext=get_sock_raw(raw_sk->next, iph->protocol, iph->saddr, iph->daddr);
1633 if(sknext)
1634 skb1=skb_clone(skb, GFP_ATOMIC);
1635 else
1636 break;
1637 if(skb1)
1638 raw_rcv(raw_sk, skb1, dev, iph->saddr,iph->daddr);
1639 raw_sk=sknext;
1640 }
1641 while(raw_sk!=NULL);
1642
1643
1644
1645
1646
1647
1648
1649
1650 }
1651 }
1652
1653
1654
1655
1656
1657 hash = iph->protocol & (MAX_INET_PROTOS -1);
1658 for (ipprot = (struct inet_protocol *)inet_protos[hash];ipprot != NULL;ipprot=(struct inet_protocol *)ipprot->next)
1659 {
1660 struct sk_buff *skb2;
1661
1662 if (ipprot->protocol != iph->protocol)
1663 continue;
1664
1665
1666
1667
1668
1669
1670
1671 if (ipprot->copy || raw_sk)
1672 {
1673 skb2 = skb_clone(skb, GFP_ATOMIC);
1674 if(skb2==NULL)
1675 continue;
1676 }
1677 else
1678 {
1679 skb2 = skb;
1680 }
1681 flag = 1;
1682
1683
1684
1685
1686
1687
1688
1689 ipprot->handler(skb2, dev, NULL, iph->daddr,
1690 (ntohs(iph->tot_len) - (iph->ihl * 4)),
1691 iph->saddr, 0, ipprot);
1692
1693 }
1694
1695
1696
1697
1698
1699
1700
1701
1702 if(raw_sk!=NULL)
1703 raw_rcv(raw_sk, skb, dev, iph->saddr, iph->daddr);
1704 else if (!flag)
1705 {
1706 if (brd != IS_BROADCAST && brd!=IS_MULTICAST)
1707 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, 0, dev);
1708 kfree_skb(skb, FREE_WRITE);
1709 }
1710
1711 return(0);
1712 }
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722 if(skb->pkt_type!=PACKET_HOST || brd==IS_BROADCAST)
1723 {
1724 kfree_skb(skb,FREE_WRITE);
1725 return 0;
1726 }
1727
1728
1729
1730
1731
1732 #ifdef CONFIG_IP_FORWARD
1733 if(ip_forward(skb, dev, is_frag, target_addr, target_strict))
1734 kfree_skb(skb, FREE_WRITE);
1735 #else
1736
1737
1738 ip_statistics.IpInAddrErrors++;
1739 kfree_skb(skb, FREE_WRITE);
1740 #endif
1741 return(0);
1742 }
1743
1744
1745
1746
1747
1748
1749 static void ip_loopback(struct device *old_dev, struct sk_buff *skb)
1750 {
1751 struct device *dev=&loopback_dev;
1752 int len=ntohs(skb->ip_hdr->tot_len);
1753 struct sk_buff *newskb=dev_alloc_skb(len+dev->hard_header_len+15);
1754
1755 if(newskb==NULL)
1756 return;
1757
1758 newskb->link3=NULL;
1759 newskb->sk=NULL;
1760 newskb->dev=dev;
1761 newskb->saddr=skb->saddr;
1762 newskb->daddr=skb->daddr;
1763 newskb->raddr=skb->raddr;
1764 newskb->free=1;
1765 newskb->lock=0;
1766 newskb->users=0;
1767 newskb->pkt_type=skb->pkt_type;
1768
1769
1770
1771
1772 ip_send(newskb, skb->ip_hdr->daddr, len, dev, skb->ip_hdr->saddr);
1773
1774
1775
1776 newskb->ip_hdr=(struct iphdr *)skb_put(newskb, len);
1777
1778
1779
1780 memcpy(newskb->ip_hdr,skb->ip_hdr,len);
1781
1782
1783
1784
1785 ip_queue_xmit(NULL, dev, newskb, 1);
1786 }
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798 void ip_queue_xmit(struct sock *sk, struct device *dev,
1799 struct sk_buff *skb, int free)
1800 {
1801 struct iphdr *iph;
1802
1803
1804
1805 if (dev == NULL)
1806 {
1807 NETDEBUG(printk("IP: ip_queue_xmit dev = NULL\n"));
1808 return;
1809 }
1810
1811 IS_SKB(skb);
1812
1813
1814
1815
1816
1817
1818 skb->dev = dev;
1819 skb->when = jiffies;
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829 #if 0
1830 ptr = skb->data;
1831 ptr += dev->hard_header_len;
1832 iph = (struct iphdr *)ptr;
1833 skb->ip_hdr = iph;
1834 #else
1835 iph = skb->ip_hdr;
1836 #endif
1837 iph->tot_len = ntohs(skb->len-(((unsigned char *)iph)-skb->data));
1838
1839 #ifdef CONFIG_IP_FIREWALL
1840 if(ip_fw_chk(iph, dev, ip_fw_blk_chain, ip_fw_blk_policy, 0) < FW_ACCEPT)
1841
1842 return;
1843 #endif
1844
1845
1846
1847
1848
1849 if(free!=2)
1850 iph->id = htons(ip_id_count++);
1851 else
1852 free=1;
1853
1854
1855 if (sk == NULL)
1856 free = 1;
1857
1858 skb->free = free;
1859
1860
1861
1862
1863
1864
1865
1866 if(ntohs(iph->tot_len)> dev->mtu)
1867 {
1868 ip_fragment(sk,skb,dev,0);
1869 IS_SKB(skb);
1870 kfree_skb(skb,FREE_WRITE);
1871 return;
1872 }
1873
1874
1875
1876
1877
1878 ip_send_check(iph);
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888 if (skb->next != NULL)
1889 {
1890 NETDEBUG(printk("ip_queue_xmit: next != NULL\n"));
1891 skb_unlink(skb);
1892 }
1893
1894
1895
1896
1897
1898
1899
1900
1901 if (!free)
1902 {
1903 unsigned long flags;
1904
1905
1906 sk->packets_out++;
1907
1908
1909 save_flags(flags);
1910 cli();
1911
1912 if (skb->link3 != NULL)
1913 {
1914 NETDEBUG(printk("ip.c: link3 != NULL\n"));
1915 skb->link3 = NULL;
1916 }
1917 if (sk->send_head == NULL)
1918 {
1919 sk->send_tail = skb;
1920 sk->send_head = skb;
1921 }
1922 else
1923 {
1924 sk->send_tail->link3 = skb;
1925 sk->send_tail = skb;
1926 }
1927
1928
1929
1930 restore_flags(flags);
1931 }
1932 else
1933
1934 skb->sk = sk;
1935
1936
1937
1938
1939
1940 ip_statistics.IpOutRequests++;
1941 #ifdef CONFIG_IP_ACCT
1942 ip_fw_chk(iph,dev,ip_acct_chain,IP_FW_F_ACCEPT,1);
1943 #endif
1944
1945 #ifdef CONFIG_IP_MULTICAST
1946
1947
1948
1949
1950
1951 if (MULTICAST(iph->daddr) && !(dev->flags&IFF_LOOPBACK))
1952 {
1953 if(sk==NULL || sk->ip_mc_loop)
1954 {
1955 if(iph->daddr==IGMP_ALL_HOSTS || (dev->flags&IFF_ALLMULTI))
1956 {
1957 ip_loopback(dev,skb);
1958 }
1959 else
1960 {
1961 struct ip_mc_list *imc=dev->ip_mc_list;
1962 while(imc!=NULL)
1963 {
1964 if(imc->multiaddr==iph->daddr)
1965 {
1966 ip_loopback(dev,skb);
1967 break;
1968 }
1969 imc=imc->next;
1970 }
1971 }
1972 }
1973
1974
1975 if(skb->ip_hdr->ttl==0)
1976 {
1977 kfree_skb(skb, FREE_READ);
1978 return;
1979 }
1980 }
1981 #endif
1982 if((dev->flags&IFF_BROADCAST) && (iph->daddr==dev->pa_brdaddr||iph->daddr==0xFFFFFFFF) && !(dev->flags&IFF_LOOPBACK))
1983 ip_loopback(dev,skb);
1984
1985 if (dev->flags & IFF_UP)
1986 {
1987
1988
1989
1990
1991
1992 if (sk != NULL)
1993 {
1994 dev_queue_xmit(skb, dev, sk->priority);
1995 }
1996 else
1997 {
1998 dev_queue_xmit(skb, dev, SOPRI_NORMAL);
1999 }
2000 }
2001 else
2002 {
2003 if(sk)
2004 sk->err = ENETDOWN;
2005 ip_statistics.IpOutDiscards++;
2006 if (free)
2007 kfree_skb(skb, FREE_WRITE);
2008 }
2009 }
2010
2011
2012
2013 #ifdef CONFIG_IP_MULTICAST
2014
2015
2016
2017
2018
2019
2020 int ip_mc_procinfo(char *buffer, char **start, off_t offset, int length, int dummy)
2021 {
2022 off_t pos=0, begin=0;
2023 struct ip_mc_list *im;
2024 unsigned long flags;
2025 int len=0;
2026 struct device *dev;
2027
2028 len=sprintf(buffer,"Device : Count\tGroup Users Timer\n");
2029 save_flags(flags);
2030 cli();
2031
2032 for(dev = dev_base; dev; dev = dev->next)
2033 {
2034 if((dev->flags&IFF_UP)&&(dev->flags&IFF_MULTICAST))
2035 {
2036 len+=sprintf(buffer+len,"%-10s: %5d\n",
2037 dev->name, dev->mc_count);
2038 for(im = dev->ip_mc_list; im; im = im->next)
2039 {
2040 len+=sprintf(buffer+len,
2041 "\t\t\t%08lX %5d %d:%08lX\n",
2042 im->multiaddr, im->users,
2043 im->tm_running, im->timer.expires-jiffies);
2044 pos=begin+len;
2045 if(pos<offset)
2046 {
2047 len=0;
2048 begin=pos;
2049 }
2050 if(pos>offset+length)
2051 break;
2052 }
2053 }
2054 }
2055 restore_flags(flags);
2056 *start=buffer+(offset-begin);
2057 len-=(offset-begin);
2058 if(len>length)
2059 len=length;
2060 return len;
2061 }
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073 static struct device *ip_mc_find_devfor(unsigned long addr)
2074 {
2075 struct device *dev;
2076 for(dev = dev_base; dev; dev = dev->next)
2077 {
2078 if((dev->flags&IFF_UP)&&(dev->flags&IFF_MULTICAST)&&
2079 (dev->pa_addr==addr))
2080 return dev;
2081 }
2082
2083 return NULL;
2084 }
2085
2086 #endif
2087
2088 int ip_setsockopt(struct sock *sk, int level, int optname, char *optval, int optlen)
2089 {
2090 int val,err;
2091 unsigned char ucval;
2092 #if defined(CONFIG_IP_FIREWALL) || defined(CONFIG_IP_ACCT)
2093 struct ip_fw tmp_fw;
2094 #endif
2095 if (optval == NULL)
2096 {
2097 val=0;
2098 ucval=0;
2099 }
2100 else
2101 {
2102 err=verify_area(VERIFY_READ, optval, sizeof(int));
2103 if(err)
2104 return err;
2105 val = get_user((int *) optval);
2106 ucval=get_user((unsigned char *) optval);
2107 }
2108
2109 if(level!=SOL_IP)
2110 return -EOPNOTSUPP;
2111 #ifdef CONFIG_IP_MROUTE
2112 if(optname>=MRT_BASE && optname <=MRT_BASE+10)
2113 {
2114 return ip_mroute_setsockopt(sk,optname,optval,optlen);
2115 }
2116 #endif
2117
2118 switch(optname)
2119 {
2120 case IP_TOS:
2121 if(val<0||val>255)
2122 return -EINVAL;
2123 sk->ip_tos=val;
2124 if(val==IPTOS_LOWDELAY)
2125 sk->priority=SOPRI_INTERACTIVE;
2126 if(val==IPTOS_THROUGHPUT)
2127 sk->priority=SOPRI_BACKGROUND;
2128 return 0;
2129 case IP_TTL:
2130 if(val<1||val>255)
2131 return -EINVAL;
2132 sk->ip_ttl=val;
2133 return 0;
2134 case IP_HDRINCL:
2135 if(sk->type!=SOCK_RAW)
2136 return -ENOPROTOOPT;
2137 sk->ip_hdrincl=val?1:0;
2138 return 0;
2139 #ifdef CONFIG_IP_MULTICAST
2140 case IP_MULTICAST_TTL:
2141 {
2142 sk->ip_mc_ttl=(int)ucval;
2143 return 0;
2144 }
2145 case IP_MULTICAST_LOOP:
2146 {
2147 if(ucval!=0 && ucval!=1)
2148 return -EINVAL;
2149 sk->ip_mc_loop=(int)ucval;
2150 return 0;
2151 }
2152 case IP_MULTICAST_IF:
2153 {
2154 struct in_addr addr;
2155 struct device *dev=NULL;
2156
2157
2158
2159
2160
2161 err=verify_area(VERIFY_READ, optval, sizeof(addr));
2162 if(err)
2163 return err;
2164
2165 memcpy_fromfs(&addr,optval,sizeof(addr));
2166
2167
2168
2169
2170
2171
2172 if(addr.s_addr==INADDR_ANY)
2173 {
2174 sk->ip_mc_name[0]=0;
2175 return 0;
2176 }
2177
2178
2179
2180
2181
2182 dev=ip_mc_find_devfor(addr.s_addr);
2183
2184
2185
2186
2187
2188 if(dev)
2189 {
2190 strcpy(sk->ip_mc_name,dev->name);
2191 return 0;
2192 }
2193 return -EADDRNOTAVAIL;
2194 }
2195
2196 case IP_ADD_MEMBERSHIP:
2197 {
2198
2199
2200
2201
2202 struct ip_mreq mreq;
2203 __u32 route_src;
2204 struct rtable *rt;
2205 struct device *dev=NULL;
2206
2207
2208
2209
2210
2211 err=verify_area(VERIFY_READ, optval, sizeof(mreq));
2212 if(err)
2213 return err;
2214
2215 memcpy_fromfs(&mreq,optval,sizeof(mreq));
2216
2217
2218
2219
2220
2221 if(mreq.imr_interface.s_addr==INADDR_ANY)
2222 {
2223
2224
2225
2226 if((rt=ip_rt_route(mreq.imr_multiaddr.s_addr,NULL, &route_src))!=NULL)
2227 {
2228 dev=rt->rt_dev;
2229 rt->rt_use--;
2230 }
2231 }
2232 else
2233 {
2234
2235
2236
2237
2238 dev=ip_mc_find_devfor(mreq.imr_interface.s_addr);
2239 }
2240
2241
2242
2243
2244
2245 if(!dev)
2246 return -ENODEV;
2247
2248
2249
2250
2251
2252 return ip_mc_join_group(sk,dev,mreq.imr_multiaddr.s_addr);
2253 }
2254
2255 case IP_DROP_MEMBERSHIP:
2256 {
2257 struct ip_mreq mreq;
2258 struct rtable *rt;
2259 __u32 route_src;
2260 struct device *dev=NULL;
2261
2262
2263
2264
2265
2266 err=verify_area(VERIFY_READ, optval, sizeof(mreq));
2267 if(err)
2268 return err;
2269
2270 memcpy_fromfs(&mreq,optval,sizeof(mreq));
2271
2272
2273
2274
2275
2276 if(mreq.imr_interface.s_addr==INADDR_ANY)
2277 {
2278 if((rt=ip_rt_route(mreq.imr_multiaddr.s_addr,NULL, &route_src))!=NULL)
2279 {
2280 dev=rt->rt_dev;
2281 rt->rt_use--;
2282 }
2283 }
2284 else
2285 {
2286
2287 dev=ip_mc_find_devfor(mreq.imr_interface.s_addr);
2288 }
2289
2290
2291
2292
2293
2294 if(!dev)
2295 return -ENODEV;
2296
2297
2298
2299
2300
2301 return ip_mc_leave_group(sk,dev,mreq.imr_multiaddr.s_addr);
2302 }
2303 #endif
2304 #ifdef CONFIG_IP_FIREWALL
2305 case IP_FW_ADD_BLK:
2306 case IP_FW_DEL_BLK:
2307 case IP_FW_ADD_FWD:
2308 case IP_FW_DEL_FWD:
2309 case IP_FW_CHK_BLK:
2310 case IP_FW_CHK_FWD:
2311 case IP_FW_FLUSH_BLK:
2312 case IP_FW_FLUSH_FWD:
2313 case IP_FW_ZERO_BLK:
2314 case IP_FW_ZERO_FWD:
2315 case IP_FW_POLICY_BLK:
2316 case IP_FW_POLICY_FWD:
2317 if(!suser())
2318 return -EPERM;
2319 if(optlen>sizeof(tmp_fw) || optlen<1)
2320 return -EINVAL;
2321 err=verify_area(VERIFY_READ,optval,optlen);
2322 if(err)
2323 return err;
2324 memcpy_fromfs(&tmp_fw,optval,optlen);
2325 err=ip_fw_ctl(optname, &tmp_fw,optlen);
2326 return -err;
2327
2328 #endif
2329 #ifdef CONFIG_IP_ACCT
2330 case IP_ACCT_DEL:
2331 case IP_ACCT_ADD:
2332 case IP_ACCT_FLUSH:
2333 case IP_ACCT_ZERO:
2334 if(!suser())
2335 return -EPERM;
2336 if(optlen>sizeof(tmp_fw) || optlen<1)
2337 return -EINVAL;
2338 err=verify_area(VERIFY_READ,optval,optlen);
2339 if(err)
2340 return err;
2341 memcpy_fromfs(&tmp_fw, optval,optlen);
2342 err=ip_acct_ctl(optname, &tmp_fw,optlen);
2343 return -err;
2344 #endif
2345
2346 default:
2347 return(-ENOPROTOOPT);
2348 }
2349 }
2350
2351
2352
2353
2354
2355
2356 int ip_getsockopt(struct sock *sk, int level, int optname, char *optval, int *optlen)
2357 {
2358 int val,err;
2359 #ifdef CONFIG_IP_MULTICAST
2360 int len;
2361 #endif
2362
2363 if(level!=SOL_IP)
2364 return -EOPNOTSUPP;
2365
2366 #ifdef CONFIG_IP_MROUTE
2367 if(optname>=MRT_BASE && optname <=MRT_BASE+10)
2368 {
2369 return ip_mroute_getsockopt(sk,optname,optval,optlen);
2370 }
2371 #endif
2372
2373 switch(optname)
2374 {
2375 case IP_TOS:
2376 val=sk->ip_tos;
2377 break;
2378 case IP_TTL:
2379 val=sk->ip_ttl;
2380 break;
2381 case IP_HDRINCL:
2382 val=sk->ip_hdrincl;
2383 break;
2384 #ifdef CONFIG_IP_MULTICAST
2385 case IP_MULTICAST_TTL:
2386 val=sk->ip_mc_ttl;
2387 break;
2388 case IP_MULTICAST_LOOP:
2389 val=sk->ip_mc_loop;
2390 break;
2391 case IP_MULTICAST_IF:
2392 err=verify_area(VERIFY_WRITE, optlen, sizeof(int));
2393 if(err)
2394 return err;
2395 len=strlen(sk->ip_mc_name);
2396 err=verify_area(VERIFY_WRITE, optval, len);
2397 if(err)
2398 return err;
2399 put_user(len,(int *) optlen);
2400 memcpy_tofs((void *)optval,sk->ip_mc_name, len);
2401 return 0;
2402 #endif
2403 default:
2404 return(-ENOPROTOOPT);
2405 }
2406 err=verify_area(VERIFY_WRITE, optlen, sizeof(int));
2407 if(err)
2408 return err;
2409 put_user(sizeof(int),(int *) optlen);
2410
2411 err=verify_area(VERIFY_WRITE, optval, sizeof(int));
2412 if(err)
2413 return err;
2414 put_user(val,(int *) optval);
2415
2416 return(0);
2417 }
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439 int ip_build_xmit(struct sock *sk,
2440 void getfrag (const void *,
2441 __u32,
2442 char *,
2443 unsigned int,
2444 unsigned int),
2445 const void *frag,
2446 unsigned short int length,
2447 __u32 daddr,
2448 int flags,
2449 int type)
2450 {
2451 struct rtable *rt;
2452 unsigned int fraglen, maxfraglen, fragheaderlen;
2453 int offset, mf;
2454 __u32 saddr;
2455 unsigned short id;
2456 struct iphdr *iph;
2457 int local=0;
2458 struct device *dev;
2459 int nfrags=0;
2460
2461 ip_statistics.IpOutRequests++;
2462
2463 #ifdef CONFIG_IP_MULTICAST
2464 if(sk && MULTICAST(daddr) && *sk->ip_mc_name)
2465 {
2466 dev=dev_get(sk->ip_mc_name);
2467 if(!dev)
2468 return -ENODEV;
2469 rt=NULL;
2470 if (sk->saddr && (!LOOPBACK(sk->saddr) || LOOPBACK(daddr)))
2471 saddr = sk->saddr;
2472 else
2473 saddr = dev->pa_addr;
2474 }
2475 else
2476 {
2477 #endif
2478
2479
2480
2481
2482 if(sk->localroute || flags&MSG_DONTROUTE)
2483 local=1;
2484
2485 rt = sk->ip_route_cache;
2486
2487
2488
2489
2490
2491
2492 saddr=sk->ip_route_saddr;
2493 if(!rt || sk->ip_route_stamp != rt_stamp || daddr!=sk->ip_route_daddr || sk->ip_route_local!=local || sk->saddr!=sk->ip_route_saddr)
2494 {
2495 if(local)
2496 rt = ip_rt_local(daddr, NULL, &saddr);
2497 else
2498 rt = ip_rt_route(daddr, NULL, &saddr);
2499 sk->ip_route_local=local;
2500 sk->ip_route_daddr=daddr;
2501 sk->ip_route_saddr=saddr;
2502 sk->ip_route_stamp=rt_stamp;
2503 sk->ip_route_cache=rt;
2504 sk->ip_hcache_ver=NULL;
2505 sk->ip_hcache_state= 0;
2506 }
2507 else if(rt)
2508 {
2509
2510
2511
2512
2513
2514
2515 if(rt->rt_dev->header_cache && sk->ip_hcache_state!= -1)
2516 {
2517 if(sk->ip_hcache_ver==NULL || sk->ip_hcache_stamp!=*sk->ip_hcache_ver)
2518 rt->rt_dev->header_cache(rt->rt_dev,sk,saddr,daddr);
2519 else
2520
2521 sk->ip_hcache_state= -1;
2522 }
2523 }
2524
2525 if (rt == NULL)
2526 {
2527 ip_statistics.IpOutNoRoutes++;
2528 return(-ENETUNREACH);
2529 }
2530
2531 if (sk->saddr && (!LOOPBACK(sk->saddr) || LOOPBACK(daddr)))
2532 saddr = sk->saddr;
2533
2534 dev=rt->rt_dev;
2535 #ifdef CONFIG_IP_MULTICAST
2536 }
2537 #endif
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548 if(length+20 <= dev->mtu && !MULTICAST(daddr) && daddr!=0xFFFFFFFF && daddr!=dev->pa_brdaddr)
2549 {
2550 int error;
2551 struct sk_buff *skb=sock_alloc_send_skb(sk, length+20+15+dev->hard_header_len,0, 0,&error);
2552 if(skb==NULL)
2553 {
2554 ip_statistics.IpOutDiscards++;
2555 return error;
2556 }
2557 skb->dev=dev;
2558 skb->free=1;
2559 skb->when=jiffies;
2560 skb->sk=sk;
2561 skb->arp=0;
2562 skb->saddr=saddr;
2563 length+=20;
2564 skb->raddr=(rt&&rt->rt_gateway)?rt->rt_gateway:daddr;
2565 skb_reserve(skb,(dev->hard_header_len+15)&~15);
2566 if(sk->ip_hcache_state>0)
2567 {
2568 memcpy(skb_push(skb,dev->hard_header_len),sk->ip_hcache_data,dev->hard_header_len);
2569 skb->arp=1;
2570 }
2571 else if(dev->hard_header)
2572 {
2573 if(dev->hard_header(skb,dev,ETH_P_IP,NULL,NULL,0)>0)
2574 skb->arp=1;
2575 }
2576 else
2577 skb->arp=1;
2578 skb->ip_hdr=iph=(struct iphdr *)skb_put(skb,length);
2579 dev_lock_list();
2580 if(!sk->ip_hdrincl)
2581 {
2582 iph->version=4;
2583 iph->ihl=5;
2584 iph->tos=sk->ip_tos;
2585 iph->tot_len = htons(length);
2586 iph->id=htons(ip_id_count++);
2587 iph->frag_off = 0;
2588 iph->ttl=sk->ip_ttl;
2589 iph->protocol=type;
2590 iph->saddr=saddr;
2591 iph->daddr=daddr;
2592 iph->check=0;
2593 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
2594 getfrag(frag,saddr,(void *)(iph+1),0, length-20);
2595 }
2596 else
2597 getfrag(frag,saddr,(void *)iph,0,length-20);
2598 dev_unlock_list();
2599 #ifdef CONFIG_IP_FIREWALL
2600 if(ip_fw_chk(iph, dev, ip_fw_blk_chain, ip_fw_blk_policy,0) < FW_ACCEPT)
2601 {
2602 kfree_skb(skb, FREE_WRITE);
2603 return -EPERM;
2604 }
2605 #endif
2606 #ifdef CONFIG_IP_ACCT
2607 ip_fw_chk((void *)skb->data,dev,ip_acct_chain, IP_FW_F_ACCEPT,1);
2608 #endif
2609 if(dev->flags&IFF_UP)
2610 dev_queue_xmit(skb,dev,sk->priority);
2611 else
2612 {
2613 ip_statistics.IpOutDiscards++;
2614 kfree_skb(skb, FREE_WRITE);
2615 }
2616 return 0;
2617 }
2618
2619
2620 fragheaderlen = dev->hard_header_len;
2621 if(!sk->ip_hdrincl)
2622 fragheaderlen += 20;
2623
2624
2625
2626
2627
2628
2629 maxfraglen = ((dev->mtu-20) & ~7) + fragheaderlen;
2630
2631
2632
2633
2634
2635 offset = length - (length % (maxfraglen - fragheaderlen));
2636
2637
2638
2639
2640
2641 fraglen = length - offset + fragheaderlen;
2642
2643 if(length-offset==0)
2644 {
2645 fraglen = maxfraglen;
2646 offset -= maxfraglen-fragheaderlen;
2647 }
2648
2649
2650
2651
2652
2653
2654 mf = 0;
2655
2656
2657
2658
2659
2660 if (sk->ip_hdrincl && offset > 0)
2661 return(-EMSGSIZE);
2662
2663
2664
2665
2666
2667 dev_lock_list();
2668
2669
2670
2671
2672
2673 id = htons(ip_id_count++);
2674
2675
2676
2677
2678
2679 do
2680 {
2681 struct sk_buff * skb;
2682 int error;
2683 char *data;
2684
2685
2686
2687
2688
2689 skb = sock_alloc_send_skb(sk, fraglen+15, 0, 0, &error);
2690 if (skb == NULL)
2691 {
2692 ip_statistics.IpOutDiscards++;
2693 if(nfrags>1)
2694 ip_statistics.IpFragCreates++;
2695 dev_unlock_list();
2696 return(error);
2697 }
2698
2699
2700
2701
2702
2703 skb->next = skb->prev = NULL;
2704 skb->dev = dev;
2705 skb->when = jiffies;
2706 skb->free = 1;
2707 skb->sk = sk;
2708 skb->arp = 0;
2709 skb->saddr = saddr;
2710 skb->raddr = (rt&&rt->rt_gateway) ? rt->rt_gateway : daddr;
2711 skb_reserve(skb,(dev->hard_header_len+15)&~15);
2712 data = skb_put(skb, fraglen-dev->hard_header_len);
2713
2714
2715
2716
2717
2718
2719
2720
2721 if(sk->ip_hcache_state>0)
2722 {
2723 memcpy(skb_push(skb,dev->hard_header_len),sk->ip_hcache_data, dev->hard_header_len);
2724 skb->arp=1;
2725 }
2726 else if (dev->hard_header)
2727 {
2728 if(dev->hard_header(skb, dev, ETH_P_IP,
2729 NULL, NULL, 0)>0)
2730 skb->arp=1;
2731 }
2732
2733
2734
2735
2736
2737 skb->ip_hdr = iph = (struct iphdr *)data;
2738
2739
2740
2741
2742
2743 if(!sk->ip_hdrincl)
2744 {
2745
2746 iph->version = 4;
2747 iph->ihl = 5;
2748 iph->tos = sk->ip_tos;
2749 iph->tot_len = htons(fraglen - fragheaderlen + iph->ihl*4);
2750 iph->id = id;
2751 iph->frag_off = htons(offset>>3);
2752 iph->frag_off |= mf;
2753 #ifdef CONFIG_IP_MULTICAST
2754 if (MULTICAST(daddr))
2755 iph->ttl = sk->ip_mc_ttl;
2756 else
2757 #endif
2758 iph->ttl = sk->ip_ttl;
2759 iph->protocol = type;
2760 iph->check = 0;
2761 iph->saddr = saddr;
2762 iph->daddr = daddr;
2763 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
2764 data += iph->ihl*4;
2765
2766
2767
2768
2769
2770 mf = htons(IP_MF);
2771 }
2772
2773
2774
2775
2776
2777 getfrag(frag, saddr, data, offset, fraglen-fragheaderlen);
2778
2779
2780
2781
2782
2783 #ifdef CONFIG_IP_FIREWALL
2784 if(!offset && ip_fw_chk(iph, dev, ip_fw_blk_chain, ip_fw_blk_policy,0) < FW_ACCEPT)
2785 {
2786 kfree_skb(skb, FREE_WRITE);
2787 dev_unlock_list();
2788 return -EPERM;
2789 }
2790 #endif
2791 #ifdef CONFIG_IP_ACCT
2792 if(!offset)
2793 ip_fw_chk(iph, dev, ip_acct_chain, IP_FW_F_ACCEPT, 1);
2794 #endif
2795 offset -= (maxfraglen-fragheaderlen);
2796 fraglen = maxfraglen;
2797
2798 #ifdef CONFIG_IP_MULTICAST
2799
2800
2801
2802
2803
2804 if (MULTICAST(daddr) && !(dev->flags&IFF_LOOPBACK))
2805 {
2806
2807
2808
2809
2810
2811
2812
2813 if(sk==NULL || sk->ip_mc_loop)
2814 {
2815 if(skb->daddr==IGMP_ALL_HOSTS || (dev->flags&IFF_ALLMULTI))
2816 ip_loopback(rt?rt->rt_dev:dev,skb);
2817 else
2818 {
2819 struct ip_mc_list *imc=rt?rt->rt_dev->ip_mc_list:dev->ip_mc_list;
2820 while(imc!=NULL)
2821 {
2822 if(imc->multiaddr==daddr)
2823 {
2824 ip_loopback(rt?rt->rt_dev:dev,skb);
2825 break;
2826 }
2827 imc=imc->next;
2828 }
2829 }
2830 }
2831
2832
2833
2834
2835
2836
2837 if(skb->ip_hdr->ttl==0)
2838 kfree_skb(skb, FREE_READ);
2839 }
2840 #endif
2841
2842 nfrags++;
2843
2844
2845
2846
2847
2848 if((dev->flags&IFF_BROADCAST) && (daddr==0xFFFFFFFF || daddr==dev->pa_brdaddr) && !(dev->flags&IFF_LOOPBACK))
2849 ip_loopback(dev,skb);
2850
2851
2852
2853
2854
2855 if (dev->flags & IFF_UP)
2856 {
2857 dev_queue_xmit(skb, dev, sk->priority);
2858 }
2859 else
2860 {
2861
2862
2863
2864
2865 ip_statistics.IpOutDiscards++;
2866 if(nfrags>1)
2867 ip_statistics.IpFragCreates+=nfrags;
2868 kfree_skb(skb, FREE_WRITE);
2869 dev_unlock_list();
2870
2871
2872
2873 if(sk!=NULL)
2874 sk->err=ENETDOWN;
2875 return(0);
2876 }
2877 }
2878 while (offset >= 0);
2879 if(nfrags>1)
2880 ip_statistics.IpFragCreates+=nfrags;
2881 dev_unlock_list();
2882 return(0);
2883 }
2884
2885
2886
2887
2888
2889
2890 static struct packet_type ip_packet_type =
2891 {
2892 0,
2893 NULL,
2894 ip_rcv,
2895 NULL,
2896 NULL,
2897 };
2898
2899
2900
2901
2902
2903 static int ip_rt_event(unsigned long event, void *ptr)
2904 {
2905 if(event==NETDEV_DOWN)
2906 ip_rt_flush(ptr);
2907 return NOTIFY_DONE;
2908 }
2909
2910 struct notifier_block ip_rt_notifier={
2911 ip_rt_event,
2912 NULL,
2913 0
2914 };
2915
2916
2917
2918
2919
2920 void ip_init(void)
2921 {
2922 ip_packet_type.type=htons(ETH_P_IP);
2923 dev_add_pack(&ip_packet_type);
2924
2925
2926 register_netdevice_notifier(&ip_rt_notifier);
2927
2928
2929
2930
2931
2932
2933 #ifdef CONFIG_IP_MULTICAST
2934 proc_net_register(&(struct proc_dir_entry) {
2935 PROC_NET_IGMP, 4, "igmp",
2936 S_IFREG | S_IRUGO, 1, 0, 0,
2937 0, &proc_net_inode_operations,
2938 ip_mc_procinfo
2939 });
2940 #endif
2941 }
2942