aboutsummaryrefslogtreecommitdiffstats
path: root/recipes/linux/linux-magicbox-2.6.19.2/160-netfilter_route.patch
blob: 7e8491c3e3b2673e5c6f910d53e0b9c11f65e594 (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
diff -urN linux-2.6.19.old/include/linux/netfilter_ipv4/ipt_ROUTE.h linux-2.6.19.dev/include/linux/netfilter_ipv4/ipt_ROUTE.h
--- linux-2.6.19.old/include/linux/netfilter_ipv4/ipt_ROUTE.h	1970-01-01 01:00:00.000000000 +0100
+++ linux-2.6.19.dev/include/linux/netfilter_ipv4/ipt_ROUTE.h	2006-12-14 03:13:49.000000000 +0100
@@ -0,0 +1,23 @@
+/* Header file for iptables ipt_ROUTE target
+ *
+ * (C) 2002 by C�dric de Launois <delaunois@info.ucl.ac.be>
+ *
+ * This software is distributed under GNU GPL v2, 1991
+ */
+#ifndef _IPT_ROUTE_H_target
+#define _IPT_ROUTE_H_target
+
+#define IPT_ROUTE_IFNAMSIZ 16
+
+struct ipt_route_target_info {
+       char      oif[IPT_ROUTE_IFNAMSIZ];      /* Output Interface Name */
+       char      iif[IPT_ROUTE_IFNAMSIZ];      /* Input Interface Name  */
+       u_int32_t gw;                           /* IP address of gateway */
+       u_int8_t  flags;
+};
+
+/* Values for "flags" field */
+#define IPT_ROUTE_CONTINUE        0x01
+#define IPT_ROUTE_TEE             0x02
+
+#endif /*_IPT_ROUTE_H_target*/
diff -urN linux-2.6.19.old/include/linux/netfilter_ipv6/ip6t_ROUTE.h linux-2.6.19.dev/include/linux/netfilter_ipv6/ip6t_ROUTE.h
--- linux-2.6.19.old/include/linux/netfilter_ipv6/ip6t_ROUTE.h	1970-01-01 01:00:00.000000000 +0100
+++ linux-2.6.19.dev/include/linux/netfilter_ipv6/ip6t_ROUTE.h	2006-12-14 03:13:49.000000000 +0100
@@ -0,0 +1,23 @@
+/* Header file for iptables ip6t_ROUTE target
+ *
+ * (C) 2003 by C�dric de Launois <delaunois@info.ucl.ac.be>
+ *
+ * This software is distributed under GNU GPL v2, 1991
+ */
+#ifndef _IPT_ROUTE_H_target
+#define _IPT_ROUTE_H_target
+
+#define IP6T_ROUTE_IFNAMSIZ 16
+
+struct ip6t_route_target_info {
+       char      oif[IP6T_ROUTE_IFNAMSIZ];     /* Output Interface Name */
+       char      iif[IP6T_ROUTE_IFNAMSIZ];     /* Input Interface Name  */
+       u_int32_t gw[4];                        /* IPv6 address of gateway */
+       u_int8_t  flags;
+};
+
+/* Values for "flags" field */
+#define IP6T_ROUTE_CONTINUE        0x01
+#define IP6T_ROUTE_TEE             0x02
+
+#endif /*_IP6T_ROUTE_H_target*/
diff -urN linux-2.6.19.old/net/ipv4/netfilter/ipt_ROUTE.c linux-2.6.19.dev/net/ipv4/netfilter/ipt_ROUTE.c
--- linux-2.6.19.old/net/ipv4/netfilter/ipt_ROUTE.c	1970-01-01 01:00:00.000000000 +0100
+++ linux-2.6.19.dev/net/ipv4/netfilter/ipt_ROUTE.c	2006-12-14 03:13:49.000000000 +0100
@@ -0,0 +1,455 @@
+/*
+ * This implements the ROUTE target, which enables you to setup unusual
+ * routes not supported by the standard kernel routing table.
+ *
+ * Copyright (C) 2002 Cedric de Launois <delaunois@info.ucl.ac.be>
+ *
+ * v 1.11 2004/11/23
+ *
+ * This software is distributed under GNU GPL v2, 1991
+ */
+
+#include <linux/module.h>
+#include <linux/skbuff.h>
+#include <linux/ip.h>
+#include <linux/netfilter_ipv4/ip_tables.h>
+#include <linux/netfilter_ipv4/ip_conntrack.h>
+#include <linux/netfilter_ipv4/ipt_ROUTE.h>
+#include <linux/netdevice.h>
+#include <linux/route.h>
+#include <linux/if_arp.h>
+#include <net/ip.h>
+#include <net/route.h>
+#include <net/icmp.h>
+#include <net/checksum.h>
+
+#if 0
+#define DEBUGP printk
+#else
+#define DEBUGP(format, args...)
+#endif
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Cedric de Launois <delaunois@info.ucl.ac.be>");
+MODULE_DESCRIPTION("iptables ROUTE target module");
+
+/* Try to route the packet according to the routing keys specified in
+ * route_info. Keys are :
+ *  - ifindex :
+ *      0 if no oif preferred,
+ *      otherwise set to the index of the desired oif
+ *  - route_info->gw :
+ *      0 if no gateway specified,
+ *      otherwise set to the next host to which the pkt must be routed
+ * If success, skb->dev is the output device to which the packet must
+ * be sent and skb->dst is not NULL
+ *
+ * RETURN: -1 if an error occured
+ *          1 if the packet was succesfully routed to the
+ *            destination desired
+ *          0 if the kernel routing table could not route the packet
+ *            according to the keys specified
+ */
+static int route(struct sk_buff *skb,
+                unsigned int ifindex,
+                const struct ipt_route_target_info *route_info)
+{
+       int err;
+       struct rtable *rt;
+       struct iphdr *iph = skb->nh.iph;
+       struct flowi fl = {
+               .oif = ifindex,
+               .nl_u = {
+                       .ip4_u = {
+                               .daddr = iph->daddr,
+                               .saddr = 0,
+                               .tos = RT_TOS(iph->tos),
+                               .scope = RT_SCOPE_UNIVERSE,
+                       }
+               }
+       };
+
+       /* The destination address may be overloaded by the target */
+       if (route_info->gw)
+               fl.fl4_dst = route_info->gw;
+
+       /* Trying to route the packet using the standard routing table. */
+       if ((err = ip_route_output_key(&rt, &fl))) {
+               if (net_ratelimit())
+                       DEBUGP("ipt_ROUTE: couldn't route pkt (err: %i)",err);
+               return -1;
+       }
+
+       /* Drop old route. */
+       dst_release(skb->dst);
+       skb->dst = NULL;
+
+       /* Success if no oif specified or if the oif correspond to the
+        * one desired */
+       if (!ifindex || rt->u.dst.dev->ifindex == ifindex) {
+               skb->dst = &rt->u.dst;
+               skb->dev = skb->dst->dev;
+               skb->protocol = htons(ETH_P_IP);
+               return 1;
+       }
+
+       /* The interface selected by the routing table is not the one
+        * specified by the user. This may happen because the dst address
+        * is one of our own addresses.
+        */
+       if (net_ratelimit())
+               DEBUGP("ipt_ROUTE: failed to route as desired gw=%u.%u.%u.%u oif=%i (got oif=%i)\n",
+                      NIPQUAD(route_info->gw), ifindex, rt->u.dst.dev->ifindex);
+
+       return 0;
+}
+
+
+/* Stolen from ip_finish_output2
+ * PRE : skb->dev is set to the device we are leaving by
+ *       skb->dst is not NULL
+ * POST: the packet is sent with the link layer header pushed
+ *       the packet is destroyed
+ */
+static void ip_direct_send(struct sk_buff *skb)
+{
+       struct dst_entry *dst = skb->dst;
+       struct hh_cache *hh = dst->hh;
+       struct net_device *dev = dst->dev;
+       int hh_len = LL_RESERVED_SPACE(dev);
+
+       /* Be paranoid, rather than too clever. */
+       if (unlikely(skb_headroom(skb) < hh_len && dev->hard_header)) {
+               struct sk_buff *skb2;
+
+               skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
+               if (skb2 == NULL) {
+                       kfree_skb(skb);
+                       return;
+               }
+               if (skb->sk)
+                       skb_set_owner_w(skb2, skb->sk);
+               kfree_skb(skb);
+               skb = skb2;
+       }
+
+       if (hh) {
+               int hh_alen;
+
+               read_lock_bh(&hh->hh_lock);
+               hh_alen = HH_DATA_ALIGN(hh->hh_len);
+               memcpy(skb->data - hh_alen, hh->hh_data, hh_alen);
+               read_unlock_bh(&hh->hh_lock);
+               skb_push(skb, hh->hh_len);
+               hh->hh_output(skb);
+       } else if (dst->neighbour)
+               dst->neighbour->output(skb);
+       else {
+               if (net_ratelimit())
+                       DEBUGP(KERN_DEBUG "ipt_ROUTE: no hdr & no neighbour cache!\n");
+               kfree_skb(skb);
+       }
+}
+
+
+/* PRE : skb->dev is set to the device we are leaving by
+ * POST: - the packet is directly sent to the skb->dev device, without
+ *         pushing the link layer header.
+ *       - the packet is destroyed
+ */
+static inline int dev_direct_send(struct sk_buff *skb)
+{
+       return dev_queue_xmit(skb);
+}
+
+
+static unsigned int route_oif(const struct ipt_route_target_info *route_info,
+                             struct sk_buff *skb)
+{
+       unsigned int ifindex = 0;
+       struct net_device *dev_out = NULL;
+
+       /* The user set the interface name to use.
+        * Getting the current interface index.
+        */
+       if ((dev_out = dev_get_by_name(route_info->oif))) {
+               ifindex = dev_out->ifindex;
+       } else {
+               /* Unknown interface name : packet dropped */
+               if (net_ratelimit())
+                       DEBUGP("ipt_ROUTE: oif interface %s not found\n", route_info->oif);
+               return NF_DROP;
+       }
+
+       /* Trying the standard way of routing packets */
+       switch (route(skb, ifindex, route_info)) {
+       case 1:
+               dev_put(dev_out);
+               if (route_info->flags & IPT_ROUTE_CONTINUE)
+                       return IPT_CONTINUE;
+
+               ip_direct_send(skb);
+               return NF_STOLEN;
+
+       case 0:
+               /* Failed to send to oif. Trying the hard way */
+               if (route_info->flags & IPT_ROUTE_CONTINUE)
+                       return NF_DROP;
+
+               if (net_ratelimit())
+                       DEBUGP("ipt_ROUTE: forcing the use of %i\n",
+                              ifindex);
+
+               /* We have to force the use of an interface.
+                * This interface must be a tunnel interface since
+                * otherwise we can't guess the hw address for
+                * the packet. For a tunnel interface, no hw address
+                * is needed.
+                */
+               if ((dev_out->type != ARPHRD_TUNNEL)
+                   && (dev_out->type != ARPHRD_IPGRE)) {
+                       if (net_ratelimit())
+                               DEBUGP("ipt_ROUTE: can't guess the hw addr !\n");
+                       dev_put(dev_out);
+                       return NF_DROP;
+               }
+
+               /* Send the packet. This will also free skb
+                * Do not go through the POST_ROUTING hook because
+                * skb->dst is not set and because it will probably
+                * get confused by the destination IP address.
+                */
+               skb->dev = dev_out;
+               dev_direct_send(skb);
+               dev_put(dev_out);
+               return NF_STOLEN;
+
+       default:
+               /* Unexpected error */
+               dev_put(dev_out);
+               return NF_DROP;
+       }
+}
+
+
+static unsigned int route_iif(const struct ipt_route_target_info *route_info,
+                             struct sk_buff *skb)
+{
+       struct net_device *dev_in = NULL;
+
+       /* Getting the current interface index. */
+       if (!(dev_in = dev_get_by_name(route_info->iif))) {
+               if (net_ratelimit())
+                       DEBUGP("ipt_ROUTE: iif interface %s not found\n", route_info->iif);
+               return NF_DROP;
+       }
+
+       skb->dev = dev_in;
+       dst_release(skb->dst);
+       skb->dst = NULL;
+
+       netif_rx(skb);
+       dev_put(dev_in);
+       return NF_STOLEN;
+}
+
+
+static unsigned int route_gw(const struct ipt_route_target_info *route_info,
+                            struct sk_buff *skb)
+{
+       if (route(skb, 0, route_info)!=1)
+               return NF_DROP;
+
+       if (route_info->flags & IPT_ROUTE_CONTINUE)
+               return IPT_CONTINUE;
+
+       ip_direct_send(skb);
+       return NF_STOLEN;
+}
+
+
+/* To detect and deter routed packet loopback when using the --tee option,
+ * we take a page out of the raw.patch book: on the copied skb, we set up
+ * a fake ->nfct entry, pointing to the local &route_tee_track. We skip
+ * routing packets when we see they already have that ->nfct.
+ */
+
+static struct ip_conntrack route_tee_track;
+
+static unsigned int ipt_route_target(struct sk_buff **pskb,
+                                    const struct net_device *in,
+                                    const struct net_device *out,
+                                    unsigned int hooknum,
+				    const struct xt_target *target,
+                                    const void *targinfo)
+{
+       const struct ipt_route_target_info *route_info = targinfo;
+       struct sk_buff *skb = *pskb;
+       unsigned int res;
+
+       if (skb->nfct == &route_tee_track.ct_general) {
+               /* Loopback - a packet we already routed, is to be
+                * routed another time. Avoid that, now.
+                */
+               if (net_ratelimit())
+                       DEBUGP(KERN_DEBUG "ipt_ROUTE: loopback - DROP!\n");
+               return NF_DROP;
+       }
+
+       /* If we are at PREROUTING or INPUT hook
+        * the TTL isn't decreased by the IP stack
+        */
+       if (hooknum == NF_IP_PRE_ROUTING ||
+           hooknum == NF_IP_LOCAL_IN) {
+
+               struct iphdr *iph = skb->nh.iph;
+
+               if (iph->ttl <= 1) {
+                       struct rtable *rt;
+                       struct flowi fl = {
+                               .oif = 0,
+                               .nl_u = {
+                                       .ip4_u = {
+                                               .daddr = iph->daddr,
+                                               .saddr = iph->saddr,
+                                               .tos = RT_TOS(iph->tos),
+                                               .scope = ((iph->tos & RTO_ONLINK) ?
+                                                         RT_SCOPE_LINK :
+                                                         RT_SCOPE_UNIVERSE)
+                                       }
+                               }
+                       };
+
+                       if (ip_route_output_key(&rt, &fl)) {
+                               return NF_DROP;
+                       }
+
+                       if (skb->dev == rt->u.dst.dev) {
+                               /* Drop old route. */
+                               dst_release(skb->dst);
+                               skb->dst = &rt->u.dst;
+
+                               /* this will traverse normal stack, and
+                                * thus call conntrack on the icmp packet */
+                               icmp_send(skb, ICMP_TIME_EXCEEDED,
+                                         ICMP_EXC_TTL, 0);
+                       }
+
+                       return NF_DROP;
+               }
+
+               /*
+                * If we are at INPUT the checksum must be recalculated since
+                * the length could change as the result of a defragmentation.
+                */
+               if(hooknum == NF_IP_LOCAL_IN) {
+                       iph->ttl = iph->ttl - 1;
+                       iph->check = 0;
+                       iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
+               } else {
+                       ip_decrease_ttl(iph);
+               }
+       }
+
+       if ((route_info->flags & IPT_ROUTE_TEE)) {
+               /*
+                * Copy the *pskb, and route the copy. Will later return
+                * IPT_CONTINUE for the original skb, which should continue
+                * on its way as if nothing happened. The copy should be
+                * independantly delivered to the ROUTE --gw.
+                */
+               skb = skb_copy(*pskb, GFP_ATOMIC);
+               if (!skb) {
+                       if (net_ratelimit())
+                               DEBUGP(KERN_DEBUG "ipt_ROUTE: copy failed!\n");
+                       return IPT_CONTINUE;
+               }
+       }
+
+       /* Tell conntrack to forget this packet since it may get confused
+        * when a packet is leaving with dst address == our address.
+        * Good idea ? Dunno. Need advice.
+        *
+        * NEW: mark the skb with our &route_tee_track, so we avoid looping
+        * on any already routed packet.
+        */
+       if (!(route_info->flags & IPT_ROUTE_CONTINUE)) {
+               nf_conntrack_put(skb->nfct);
+               skb->nfct = &route_tee_track.ct_general;
+               skb->nfctinfo = IP_CT_NEW;
+               nf_conntrack_get(skb->nfct);
+       }
+
+       if (route_info->oif[0] != '\0') {
+               res = route_oif(route_info, skb);
+       } else if (route_info->iif[0] != '\0') {
+               res = route_iif(route_info, skb);
+       } else if (route_info->gw) {
+               res = route_gw(route_info, skb);
+       } else {
+               if (net_ratelimit())
+                       DEBUGP(KERN_DEBUG "ipt_ROUTE: no parameter !\n");
+               res = IPT_CONTINUE;
+       }
+
+       if ((route_info->flags & IPT_ROUTE_TEE))
+               res = IPT_CONTINUE;
+
+       return res;
+}
+
+
+static int ipt_route_checkentry(const char *tablename,
+                               const void *e,
+			       const struct xt_target *target,
+                               void *targinfo,
+                               unsigned int hook_mask)
+{
+       if (strcmp(tablename, "mangle") != 0) {
+               printk("ipt_ROUTE: bad table `%s', use the `mangle' table.\n",
+                      tablename);
+               return 0;
+       }
+
+       if (hook_mask & ~(  (1 << NF_IP_PRE_ROUTING)
+                           | (1 << NF_IP_LOCAL_IN)
+                           | (1 << NF_IP_FORWARD)
+                           | (1 << NF_IP_LOCAL_OUT)
+                           | (1 << NF_IP_POST_ROUTING))) {
+               printk("ipt_ROUTE: bad hook\n");
+               return 0;
+       }
+
+       return 1;
+}
+
+
+static struct ipt_target ipt_route_reg = {
+       .name = "ROUTE",
+       .target = ipt_route_target,
+       .targetsize = sizeof(struct ipt_route_target_info),
+       .checkentry = ipt_route_checkentry,
+       .me = THIS_MODULE,
+};
+
+static int __init init(void)
+{
+       /* Set up fake conntrack (stolen from raw.patch):
+           - to never be deleted, not in any hashes */
+       atomic_set(&route_tee_track.ct_general.use, 1);
+       /*  - and look it like as a confirmed connection */
+       set_bit(IPS_CONFIRMED_BIT, &route_tee_track.status);
+       /* Initialize fake conntrack so that NAT will skip it */
+       route_tee_track.status |= IPS_NAT_DONE_MASK;
+
+       return ipt_register_target(&ipt_route_reg);
+}
+
+
+static void __exit fini(void)
+{
+       ipt_unregister_target(&ipt_route_reg);
+}
+
+module_init(init);
+module_exit(fini);
diff -urN linux-2.6.19.old/net/ipv4/netfilter/Kconfig linux-2.6.19.dev/net/ipv4/netfilter/Kconfig
--- linux-2.6.19.old/net/ipv4/netfilter/Kconfig	2006-12-14 03:13:49.000000000 +0100
+++ linux-2.6.19.dev/net/ipv4/netfilter/Kconfig	2006-12-14 03:13:49.000000000 +0100
@@ -494,6 +494,23 @@
 
 	  To compile it as a module, choose M here.  If unsure, say N.
 
+config IP_NF_TARGET_ROUTE
+        tristate  'ROUTE target support'
+        depends on IP_NF_MANGLE
+        help
+          This option adds a `ROUTE' target, which enables you to setup unusual
+          routes. For example, the ROUTE lets you route a received packet through
+          an interface or towards a host, even if the regular destination of the
+          packet is the router itself. The ROUTE target is also able to change the
+          incoming interface of a packet.
+
+          The target can be or not a final target. It has to be used inside the
+          mangle table.
+
+          If you want to compile it as a module, say M here and read
+          Documentation/modules.txt.  The module will be called ipt_ROUTE.o.
+          If unsure, say `N'.
+
 config IP_NF_TARGET_NETMAP
 	tristate "NETMAP target support"
 	depends on IP_NF_NAT
diff -urN linux-2.6.19.old/net/ipv4/netfilter/Makefile linux-2.6.19.dev/net/ipv4/netfilter/Makefile
--- linux-2.6.19.old/net/ipv4/netfilter/Makefile	2006-12-14 03:13:49.000000000 +0100
+++ linux-2.6.19.dev/net/ipv4/netfilter/Makefile	2006-12-14 03:13:49.000000000 +0100
@@ -74,6 +74,7 @@
 obj-$(CONFIG_IP_NF_TARGET_IMQ) += ipt_IMQ.o
 obj-$(CONFIG_IP_NF_TARGET_MASQUERADE) += ipt_MASQUERADE.o
 obj-$(CONFIG_IP_NF_TARGET_REDIRECT) += ipt_REDIRECT.o
+obj-$(CONFIG_IP_NF_TARGET_ROUTE) += ipt_ROUTE.o
 obj-$(CONFIG_IP_NF_TARGET_NETMAP) += ipt_NETMAP.o
 obj-$(CONFIG_IP_NF_TARGET_SAME) += ipt_SAME.o
 obj-$(CONFIG_IP_NF_NAT_SNMP_BASIC) += ip_nat_snmp_basic.o
diff -urN linux-2.6.19.old/net/ipv6/ipv6_syms.c linux-2.6.19.dev/net/ipv6/ipv6_syms.c
--- linux-2.6.19.old/net/ipv6/ipv6_syms.c	2006-11-29 22:57:37.000000000 +0100
+++ linux-2.6.19.dev/net/ipv6/ipv6_syms.c	2006-12-14 03:13:49.000000000 +0100
@@ -11,6 +11,7 @@
 EXPORT_SYMBOL(icmpv6_statistics);
 EXPORT_SYMBOL(icmpv6_err_convert);
 EXPORT_SYMBOL(ndisc_mc_map);
+EXPORT_SYMBOL(nd_tbl);
 EXPORT_SYMBOL(register_inet6addr_notifier);
 EXPORT_SYMBOL(unregister_inet6addr_notifier);
 EXPORT_SYMBOL(ip6_route_output);
diff -urN linux-2.6.19.old/net/ipv6/netfilter/ip6t_ROUTE.c linux-2.6.19.dev/net/ipv6/netfilter/ip6t_ROUTE.c
--- linux-2.6.19.old/net/ipv6/netfilter/ip6t_ROUTE.c	1970-01-01 01:00:00.000000000 +0100
+++ linux-2.6.19.dev/net/ipv6/netfilter/ip6t_ROUTE.c	2006-12-14 03:13:49.000000000 +0100
@@ -0,0 +1,302 @@
+/*
+ * This implements the ROUTE v6 target, which enables you to setup unusual
+ * routes not supported by the standard kernel routing table.
+ *
+ * Copyright (C) 2003 Cedric de Launois <delaunois@info.ucl.ac.be>
+ *
+ * v 1.1 2004/11/23
+ *
+ * This software is distributed under GNU GPL v2, 1991
+ */
+
+#include <linux/module.h>
+#include <linux/skbuff.h>
+#include <linux/ipv6.h>
+#include <linux/netfilter_ipv6/ip6_tables.h>
+#include <linux/netfilter_ipv6/ip6t_ROUTE.h>
+#include <linux/netdevice.h>
+#include <net/ipv6.h>
+#include <net/ndisc.h>
+#include <net/ip6_route.h>
+#include <linux/icmpv6.h>
+
+#if 1
+#define DEBUGP printk
+#else
+#define DEBUGP(format, args...)
+#endif
+
+#define NIP6(addr) \
+       ntohs((addr).s6_addr16[0]), \
+       ntohs((addr).s6_addr16[1]), \
+       ntohs((addr).s6_addr16[2]), \
+       ntohs((addr).s6_addr16[3]), \
+       ntohs((addr).s6_addr16[4]), \
+       ntohs((addr).s6_addr16[5]), \
+       ntohs((addr).s6_addr16[6]), \
+       ntohs((addr).s6_addr16[7])
+
+/* Route the packet according to the routing keys specified in
+ * route_info. Keys are :
+ *  - ifindex :
+ *      0 if no oif preferred,
+ *      otherwise set to the index of the desired oif
+ *  - route_info->gw :
+ *      0 if no gateway specified,
+ *      otherwise set to the next host to which the pkt must be routed
+ * If success, skb->dev is the output device to which the packet must
+ * be sent and skb->dst is not NULL
+ *
+ * RETURN:  1 if the packet was succesfully routed to the
+ *            destination desired
+ *          0 if the kernel routing table could not route the packet
+ *            according to the keys specified
+ */
+static int
+route6(struct sk_buff *skb,
+       unsigned int ifindex,
+       const struct ip6t_route_target_info *route_info)
+{
+       struct rt6_info *rt = NULL;
+       struct ipv6hdr *ipv6h = skb->nh.ipv6h;
+       struct in6_addr *gw = (struct in6_addr*)&route_info->gw;
+
+       DEBUGP("ip6t_ROUTE: called with: ");
+       DEBUGP("DST=%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ", NIP6(ipv6h->daddr));
+       DEBUGP("GATEWAY=%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ", NIP6(*gw));
+       DEBUGP("OUT=%s\n", route_info->oif);
+
+       if (ipv6_addr_any(gw))
+               rt = rt6_lookup(&ipv6h->daddr, &ipv6h->saddr, ifindex, 1);
+       else
+               rt = rt6_lookup(gw, &ipv6h->saddr, ifindex, 1);
+
+       if (!rt)
+               goto no_route;
+
+       DEBUGP("ip6t_ROUTE: routing gives: ");
+       DEBUGP("DST=%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ", NIP6(rt->rt6i_dst.addr));
+       DEBUGP("GATEWAY=%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ", NIP6(rt->rt6i_gateway));
+       DEBUGP("OUT=%s\n", rt->rt6i_dev->name);
+
+       if (ifindex && rt->rt6i_dev->ifindex!=ifindex)
+               goto wrong_route;
+
+       if (!rt->rt6i_nexthop) {
+               DEBUGP("ip6t_ROUTE: discovering neighbour\n");
+               rt->rt6i_nexthop = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_dst.addr);
+       }
+
+       /* Drop old route. */
+       dst_release(skb->dst);
+       skb->dst = &rt->u.dst;
+       skb->dev = rt->rt6i_dev;
+       return 1;
+
+ wrong_route:
+       dst_release(&rt->u.dst);
+ no_route:
+       if (!net_ratelimit())
+               return 0;
+
+       printk("ip6t_ROUTE: no explicit route found ");
+       if (ifindex)
+               printk("via interface %s ", route_info->oif);
+       if (!ipv6_addr_any(gw))
+               printk("via gateway %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x", NIP6(*gw));
+       printk("\n");
+       return 0;
+}
+
+
+/* Stolen from ip6_output_finish
+ * PRE : skb->dev is set to the device we are leaving by
+ *       skb->dst is not NULL
+ * POST: the packet is sent with the link layer header pushed
+ *       the packet is destroyed
+ */
+static void ip_direct_send(struct sk_buff *skb)
+{
+       struct dst_entry *dst = skb->dst;
+       struct hh_cache *hh = dst->hh;
+
+       if (hh) {
+               read_lock_bh(&hh->hh_lock);
+               memcpy(skb->data - 16, hh->hh_data, 16);
+               read_unlock_bh(&hh->hh_lock);
+               skb_push(skb, hh->hh_len);
+               hh->hh_output(skb);
+       } else if (dst->neighbour)
+               dst->neighbour->output(skb);
+       else {
+               if (net_ratelimit())
+                       DEBUGP(KERN_DEBUG "ip6t_ROUTE: no hdr & no neighbour cache!\n");
+               kfree_skb(skb);
+       }
+}
+
+
+static unsigned int
+route6_oif(const struct ip6t_route_target_info *route_info,
+          struct sk_buff *skb)
+{
+       unsigned int ifindex = 0;
+       struct net_device *dev_out = NULL;
+
+       /* The user set the interface name to use.
+        * Getting the current interface index.
+        */
+       if ((dev_out = dev_get_by_name(route_info->oif))) {
+               ifindex = dev_out->ifindex;
+       } else {
+               /* Unknown interface name : packet dropped */
+               if (net_ratelimit())
+                       DEBUGP("ip6t_ROUTE: oif interface %s not found\n", route_info->oif);
+
+               if (route_info->flags & IP6T_ROUTE_CONTINUE)
+                       return IP6T_CONTINUE;
+               else
+                       return NF_DROP;
+       }
+
+       /* Trying the standard way of routing packets */
+       if (route6(skb, ifindex, route_info)) {
+               dev_put(dev_out);
+               if (route_info->flags & IP6T_ROUTE_CONTINUE)
+                       return IP6T_CONTINUE;
+
+               ip_direct_send(skb);
+               return NF_STOLEN;
+       } else
+               return NF_DROP;
+}
+
+
+static unsigned int
+route6_gw(const struct ip6t_route_target_info *route_info,
+         struct sk_buff *skb)
+{
+       if (route6(skb, 0, route_info)) {
+               if (route_info->flags & IP6T_ROUTE_CONTINUE)
+                       return IP6T_CONTINUE;
+
+               ip_direct_send(skb);
+               return NF_STOLEN;
+       } else
+               return NF_DROP;
+}
+
+
+static unsigned int
+ip6t_route_target(struct sk_buff **pskb,
+                 const struct net_device *in,
+                 const struct net_device *out,
+                 unsigned int hooknum,
+		 const struct xt_target *target,
+                 const void *targinfo)
+{
+       const struct ip6t_route_target_info *route_info = targinfo;
+       struct sk_buff *skb = *pskb;
+       struct in6_addr *gw = (struct in6_addr*)&route_info->gw;
+       unsigned int res;
+
+       if (route_info->flags & IP6T_ROUTE_CONTINUE)
+               goto do_it;
+
+       /* If we are at PREROUTING or INPUT hook
+        * the TTL isn't decreased by the IP stack
+        */
+       if (hooknum == NF_IP6_PRE_ROUTING ||
+           hooknum == NF_IP6_LOCAL_IN) {
+
+               struct ipv6hdr *ipv6h = skb->nh.ipv6h;
+
+               if (ipv6h->hop_limit <= 1) {
+                       /* Force OUTPUT device used as source address */
+                       skb->dev = skb->dst->dev;
+
+                       icmpv6_send(skb, ICMPV6_TIME_EXCEED,
+                                   ICMPV6_EXC_HOPLIMIT, 0, skb->dev);
+
+                       return NF_DROP;
+               }
+
+               ipv6h->hop_limit--;
+       }
+
+       if ((route_info->flags & IP6T_ROUTE_TEE)) {
+               /*
+                * Copy the *pskb, and route the copy. Will later return
+                * IP6T_CONTINUE for the original skb, which should continue
+                * on its way as if nothing happened. The copy should be
+                * independantly delivered to the ROUTE --gw.
+                */
+               skb = skb_copy(*pskb, GFP_ATOMIC);
+               if (!skb) {
+                       if (net_ratelimit())
+                               DEBUGP(KERN_DEBUG "ip6t_ROUTE: copy failed!\n");
+                       return IP6T_CONTINUE;
+               }
+       }
+
+do_it:
+       if (route_info->oif[0]) {
+               res = route6_oif(route_info, skb);
+       } else if (!ipv6_addr_any(gw)) {
+               res = route6_gw(route_info, skb);
+       } else {
+               if (net_ratelimit())
+                       DEBUGP(KERN_DEBUG "ip6t_ROUTE: no parameter !\n");
+               res = IP6T_CONTINUE;
+       }
+
+       if ((route_info->flags & IP6T_ROUTE_TEE))
+               res = IP6T_CONTINUE;
+
+       return res;
+}
+
+
+static int
+ip6t_route_checkentry(const char *tablename,
+                     const void *e,
+		     const struct xt_target *target,
+                     void *targinfo,
+                     unsigned int hook_mask)
+{
+       if (strcmp(tablename, "mangle") != 0) {
+               printk("ip6t_ROUTE: can only be called from \"mangle\" table.\n");
+               return 0;
+       }
+
+       return 1;
+}
+
+
+static struct ip6t_target ip6t_route_reg = {
+       .name       = "ROUTE",
+       .target     = ip6t_route_target,
+       .targetsize = sizeof(struct ip6t_route_target_info),
+       .checkentry = ip6t_route_checkentry,
+       .me         = THIS_MODULE
+};
+
+
+static int __init init(void)
+{
+       printk(KERN_DEBUG "registering ipv6 ROUTE target\n");
+       if (ip6t_register_target(&ip6t_route_reg))
+               return -EINVAL;
+
+       return 0;
+}
+
+
+static void __exit fini(void)
+{
+       ip6t_unregister_target(&ip6t_route_reg);
+}
+
+module_init(init);
+module_exit(fini);
+MODULE_LICENSE("GPL");
diff -urN linux-2.6.19.old/net/ipv6/netfilter/Kconfig linux-2.6.19.dev/net/ipv6/netfilter/Kconfig
--- linux-2.6.19.old/net/ipv6/netfilter/Kconfig	2006-12-14 03:13:49.000000000 +0100
+++ linux-2.6.19.dev/net/ipv6/netfilter/Kconfig	2006-12-14 03:13:49.000000000 +0100
@@ -162,6 +162,19 @@
 
 	  To compile it as a module, choose M here.  If unsure, say N.
 
+config IP6_NF_TARGET_ROUTE
+        tristate "ROUTE target support"
+        depends on IP6_NF_MANGLE
+        help
+          This option adds a `ROUTE' target, which enables you to setup unusual
+          routes. The ROUTE target is also able to change the incoming interface
+          of a packet.
+
+          The target can be or not a final target. It has to be used inside the
+          mangle table.
+
+          Not working as a module.
+
 config IP6_NF_MANGLE
 	tristate "Packet mangling"
 	depends on IP6_NF_IPTABLES
diff -urN linux-2.6.19.old/net/ipv6/netfilter/Makefile linux-2.6.19.dev/net/ipv6/netfilter/Makefile
--- linux-2.6.19.old/net/ipv6/netfilter/Makefile	2006-12-14 03:13:49.000000000 +0100
+++ linux-2.6.19.dev/net/ipv6/netfilter/Makefile	2006-12-14 03:13:49.000000000 +0100
@@ -20,6 +20,7 @@
 obj-$(CONFIG_IP6_NF_RAW) += ip6table_raw.o
 obj-$(CONFIG_IP6_NF_MATCH_HL) += ip6t_hl.o
 obj-$(CONFIG_IP6_NF_TARGET_REJECT) += ip6t_REJECT.o
+obj-$(CONFIG_IP6_NF_TARGET_ROUTE) += ip6t_ROUTE.o
 
 # objects for l3 independent conntrack
 nf_conntrack_ipv6-objs  :=  nf_conntrack_l3proto_ipv6.o nf_conntrack_proto_icmpv6.o nf_conntrack_reasm.o