Skip to contentSkip to conversation

ROUTE --tee target extension

PATCH In review Patrick Schaaf 1 participant 2 messages Nov 23, 2004 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ROUTE --tee target extension

Hello all,

independant of the two ROUTE target patches I sent earlier, here is something new for all to consider.

The appended patches, made against the SVN repository as of 2004/11/20, extend the ROUTE target with a new option, called --tee. This option is used like --continue (and mutually exclusive with --continue). When --tee is used, a COPY of the skb is made, and it is this copy which is routed to the given --gw destination. The original packet continues to traverse the chains unmodified, it is NOT rerouted.

This extension of the ROUTE target can be used to selectively copy packets to a sniffer box in an adjacent LAN, without the overhead of going through userlevel.

I have tested this extension for IPv4 with 2.4.27 and 2.6.10-rc1 kernels. The IPv6 parts are tested to compile, but not tested to work, as I have no usable IPv6 setup to test on.

Please review this extension. I would especially like to have comments on the recursion avoidance approach, which I stole from raw/NOTRACK; maybe there is a nicer way to do that, which would not depend on conntrack structure?

best regards
Patrick

ROUTE/help +8 -1
diff --exclude=CVS --exclude=.svn -urN svn-20041120-patch-o-matic-ng/ROUTE/help patch-o-matic-ng/ROUTE/help
@@ -13,8 +13,10 @@
   --iif   ifname    Change the packet's incoming interface to `ifname'.
   --gw    ip        Route the packet via this gateway.
   --continue        Route the packet and continue traversing the rules.
+  --tee             Route a copy of the packet, but continue traversing
+                    the rules with the original packet, undisturbed.
 
-  Note that --iif and --continue can't be used together.
+  Note that --iif, --continue, and --tee, are mutually exclusive.
 
   Examples :
 
@@ -32,4 +34,9 @@
   # To change the incoming network interface from eth0 to eth1 for all icmp
   # packets (final target) :
   iptables -A PREROUTING -t mangle -p icmp -i eth0 -j ROUTE --iif eth1
+
+  # To copy (duplicate) all traffic from and to a local ECHO server
+  # to a second box (nonfinal target)
+  iptables -A PREROUTING -t mangle -p tcp --dport 7 -j ROUTE --gw 1.2.3.4 --tee
+  iptables -A POSTROUTING -t mangle -p tcp --sport 7 -j ROUTE --gw 1.2.3.4 --tee
 
ROUTE/linux/include/linux/netfilter_ipv4/ipt_ROUTE.h +1 -0
diff --exclude=CVS --exclude=.svn -urN svn-20041120-patch-o-matic-ng/ROUTE/linux/include/linux/netfilter_ipv4/ipt_ROUTE.h patch-o-matic-ng/ROUTE/linux/include/linux/netfilter_ipv4/ipt_ROUTE.h
@@ -18,5 +18,6 @@
 
 /* Values for "flags" field */
 #define IPT_ROUTE_CONTINUE        0x01
+#define IPT_ROUTE_TEE             0x02
 
 #endif /*_IPT_ROUTE_H_target*/
ROUTE/linux/include/linux/netfilter_ipv6/ip6t_ROUTE.h +1 -0
diff --exclude=CVS --exclude=.svn -urN svn-20041120-patch-o-matic-ng/ROUTE/linux/include/linux/netfilter_ipv6/ip6t_ROUTE.h patch-o-matic-ng/ROUTE/linux/include/linux/netfilter_ipv6/ip6t_ROUTE.h
@@ -18,5 +18,6 @@
 
 /* Values for "flags" field */
 #define IP6T_ROUTE_CONTINUE        0x01
+#define IP6T_ROUTE_TEE             0x02
 
 #endif /*_IP6T_ROUTE_H_target*/
ROUTE/linux/net/ipv4/netfilter/ipt_ROUTE.c +58 -13

121 lines not on this page · raw message

ROUTE/linux/net/ipv6/netfilter/ip6t_ROUTE.c +29 -10
diff --exclude=CVS --exclude=.svn -urN svn-20041120-patch-o-matic-ng/ROUTE/linux/net/ipv6/netfilter/ip6t_ROUTE.c patch-o-matic-ng/ROUTE/linux/net/ipv6/netfilter/ip6t_ROUTE.c
@@ -4,7 +4,7 @@
  *
  * Copyright (C) 2003 Cedric de Launois <[email protected]>
  *
- * v 1.0 2003/08/05
+ * v 1.1 2004/11/23
  *
  * This software is distributed under GNU GPL v2, 1991
  */
@@ -198,6 +198,7 @@
 	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;
@@ -223,18 +224,36 @@
 		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]) 
-		return route6_oif(route_info, *pskb);
-	
-	if (!ipv6_addr_any(gw))
-		return route6_gw(route_info, *pskb);
+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 (net_ratelimit()) 
-		DEBUGP(KERN_DEBUG "ip6t_ROUTE: no parameter !\n");
+	if ((route_info->flags & IP6T_ROUTE_TEE))
+		res = IP6T_CONTINUE;
 
-	return IP6T_CONTINUE;
+	return res;
 }
 
 
ROUTE/linux-2.6/include/linux/netfilter_ipv4/ipt_ROUTE.h +1 -0
diff --exclude=CVS --exclude=.svn -urN svn-20041120-patch-o-matic-ng/ROUTE/linux-2.6/include/linux/netfilter_ipv4/ipt_ROUTE.h patch-o-matic-ng/ROUTE/linux-2.6/include/linux/netfilter_ipv4/ipt_ROUTE.h
@@ -18,5 +18,6 @@
 
 /* Values for "flags" field */
 #define IPT_ROUTE_CONTINUE        0x01
+#define IPT_ROUTE_TEE             0x02
 
 #endif /*_IPT_ROUTE_H_target*/
ROUTE/linux-2.6/include/linux/netfilter_ipv6/ip6t_ROUTE.h +1 -0
diff --exclude=CVS --exclude=.svn -urN svn-20041120-patch-o-matic-ng/ROUTE/linux-2.6/include/linux/netfilter_ipv6/ip6t_ROUTE.h patch-o-matic-ng/ROUTE/linux-2.6/include/linux/netfilter_ipv6/ip6t_ROUTE.h
@@ -18,5 +18,6 @@
 
 /* Values for "flags" field */
 #define IP6T_ROUTE_CONTINUE        0x01
+#define IP6T_ROUTE_TEE             0x02
 
 #endif /*_IP6T_ROUTE_H_target*/
ROUTE/linux-2.6/net/ipv4/netfilter/ipt_ROUTE.c +64 -13

127 lines not on this page · raw message

ROUTE/linux-2.6/net/ipv6/netfilter/ip6t_ROUTE.c +29 -10
diff --exclude=CVS --exclude=.svn -urN svn-20041120-patch-o-matic-ng/ROUTE/linux-2.6/net/ipv6/netfilter/ip6t_ROUTE.c patch-o-matic-ng/ROUTE/linux-2.6/net/ipv6/netfilter/ip6t_ROUTE.c
@@ -4,7 +4,7 @@
  *
  * Copyright (C) 2003 Cedric de Launois <[email protected]>
  *
- * v 1.0 2003/08/05
+ * v 1.1 2004/11/23
  *
  * This software is distributed under GNU GPL v2, 1991
  */
@@ -198,6 +198,7 @@
 	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;
@@ -223,18 +224,36 @@
 		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]) 
-		return route6_oif(route_info, *pskb);
-	
-	if (!ipv6_addr_any(gw))
-		return route6_gw(route_info, *pskb);
+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 (net_ratelimit()) 
-		DEBUGP(KERN_DEBUG "ip6t_ROUTE: no parameter !\n");
+	if ((route_info->flags & IP6T_ROUTE_TEE))
+		res = IP6T_CONTINUE;
 
-	return IP6T_CONTINUE;
+	return res;
 }
 
 
extensions/libip6t_ROUTE.c +36 -3
diff --exclude=CVS --exclude=.svn -urN svn-20041120-iptables/extensions/libip6t_ROUTE.c iptables/extensions/libip6t_ROUTE.c
@@ -1,6 +1,6 @@
 /* Shared library add-on to iptables to add ROUTE v6 target support.
  * Author : Cedric de Launois, <[email protected]>
- * v 1.0 2003/06/24
+ * v 1.1 2004/11/23
  */
 
 #include <stdio.h>
@@ -15,6 +15,11 @@
 #include <linux/netfilter_ipv6/ip6_tables.h>
 #include <linux/netfilter_ipv6/ip6t_ROUTE.h>
 
+/* compile IP6T_ROUTE_TEE support even if kernel headers are unpatched */
+#ifndef IP6T_ROUTE_TEE
+#define IP6T_ROUTE_TEE		0x02
+#endif
+
 /* Function which prints out usage message. */
 static void
 help(void)
@@ -23,9 +28,13 @@
 "ROUTE target v%s options:\n"
 "    --oif   \tifname \t\tRoute the packet through `ifname' network interface\n"
 "    --gw    \tip     \t\tRoute the packet via this gateway\n"
-"    --continue\t     \t\tRoute the packet and continue traversing the rules.\n"
+"    --continue\t     \t\tRoute packet and continue traversing the\n"
+"            \t       \t\trules. Not valid with --iif or --tee.\n"
+"    --tee\t  \t\tDuplicate packet, route the duplicate,\n"
+"            \t       \t\tcontinue traversing with original packet.\n"
+"            \t       \t\tNot valid with --iif or --continue.\n"
 "\n",
-"1.0");
+"1.1");
 }
 
 static struct option opts[] = {
@@ -33,6 +42,7 @@
 	{ "iif", 1, 0, '2' },
 	{ "gw", 1, 0, '3' },
 	{ "continue", 0, 0, '4' },
+	{ "tee", 0, 0, '5' },
 	{ 0 }
 };
 
@@ -57,6 +67,7 @@
 #define IP6T_ROUTE_OPT_IIF      0x02
 #define IP6T_ROUTE_OPT_GW       0x04
 #define IP6T_ROUTE_OPT_CONTINUE 0x08
+#define IP6T_ROUTE_OPT_TEE      0x10
 
 /* Function which parses command options; returns true if it
    ate an option */
@@ -114,12 +125,28 @@
 		if (*flags & IP6T_ROUTE_OPT_CONTINUE)
 			exit_error(PARAMETER_PROBLEM,
 				   "Can't specify --continue twice");
+		if (*flags & IP6T_ROUTE_OPT_TEE)
+			exit_error(PARAMETER_PROBLEM,
+				   "Can't specify --continue AND --tee");
 
 		route_info->flags |= IP6T_ROUTE_CONTINUE;
 		*flags |= IP6T_ROUTE_OPT_CONTINUE;
 
 		break;
 
+	case '5':
+		if (*flags & IP6T_ROUTE_OPT_TEE)
+			exit_error(PARAMETER_PROBLEM,
+				   "Can't specify --tee twice");
+		if (*flags & IP6T_ROUTE_OPT_CONTINUE)
+			exit_error(PARAMETER_PROBLEM,
+				   "Can't specify --tee AND --continue");
+
+		route_info->flags |= IP6T_ROUTE_TEE;
+		*flags |= IP6T_ROUTE_OPT_TEE;
+
+		break;
+
 	default:
 		return 0;
 	}
@@ -162,6 +189,9 @@
 	if (route_info->flags & IP6T_ROUTE_CONTINUE)
 		printf("continue");
 
+	if (route_info->flags & IP6T_ROUTE_TEE)
+		printf("tee");
+
 }
 
 
@@ -184,6 +214,9 @@
 
 	if (route_info->flags & IP6T_ROUTE_CONTINUE)
 		printf("--continue ");
+
+	if (route_info->flags & IP6T_ROUTE_TEE)
+		printf("--tee ");
 }
 
 
extensions/libip6t_ROUTE.man +4 -1
diff --exclude=CVS --exclude=.svn -urN svn-20041120-iptables/extensions/libip6t_ROUTE.man iptables/extensions/libip6t_ROUTE.man
@@ -9,4 +9,7 @@
 Route the packet via this gateway
 .TP
 .BI "--continue "
-Behave like a non-terminating target and continue traversing the rules
+Behave like a non-terminating target and continue traversing the rules. Not valid in combination with `--tee'
+.TP
+.BI "--tee "
+Make a copy of the packet, and route that copy to the given destination. For the original, uncopied packet, behave like a non-terminating target and continue traversing the rules.  Not valid in combination with `--continue'
extensions/libipt_ROUTE.c +40 -8

108 lines not on this page · raw message

extensions/libipt_ROUTE.man +4 -1
diff --exclude=CVS --exclude=.svn -urN svn-20041120-iptables/extensions/libipt_ROUTE.man iptables/extensions/libipt_ROUTE.man
@@ -12,4 +12,7 @@
 Route the packet via this gateway
 .TP
 .BI "--continue "
-Behave like a non-terminating target and continue traversing the rules.  Not valid in combination with `--iif'
+Behave like a non-terminating target and continue traversing the rules.  Not valid in combination with `--iif' or `--tee'
+.TP
+.BI "--tee "
+Make a copy of the packet, and route that copy to the given destination. For the original, uncopied packet, behave like a non-terminating target and continue traversing the rules.  Not valid in combination with `--iif' or `--continue'
extensions/xxx +15 -0
diff --exclude=CVS --exclude=.svn -urN svn-20041120-iptables/extensions/xxx iptables/extensions/xxx
@@ -0,0 +1,15 @@
+This is used to explicitly override the core network stack's routing decision.
+.B mangle
+table.
+.TP
+.BI "--oif " "ifname"
+Route the packet through `ifname' network interface
+.TP
+.BI "--iif " "ifname"
+Change the packet's incoming interface to `ifname'
+.TP
+.BI "--gw " "IP_address"
+Route the packet via this gateway
+.TP
+.BI "--continue "
+Behave like a non-terminating target and continue traversing the rules.  Not valid in combination with `--iif'
#1 lore raw

Grr... My dainbramage... Please disregard this hunk from my previous message:

Quoted 4 lines

diff --exclude=CVS --exclude=.svn -urN svn-20041120-iptables/extensions/xxx iptables/extensions/xxx

extensions/xxx +0 -0
@@ -0,0 +1,15 @@

...