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