Skip to contentSkip to conversation

iptables gateway match

PATCH In review Amin Azez 4 participants 9 messages Jun 1, 2007 – Jun 3, 2007

Subsystems (2)
lore
Share & tools
lore

Amin Azez

author
#0 lore raw

PATCH 1/1 iptables gateway match

This adds a gateway match to iptables that lets you match against the routed ipv4 gateway, it's very useful for SNAT if you want to avoid replicating your routing in your SNAT table.

e.g.

iptables -t nat -A POSTROUTING -m gateway --nexthop 172.16.1.1 -j SNAT --to-address 172.16.1.5
iptables -t nat -A POSTROUTING -m gateway --nexthop 192.168.1.1 -j SNAT --to-address 192.168.1.25

to help you choose the right SNAT address.

It works by comparing the to-be-matched gateway IP with the key in the neighbor table of the next-hop (the key is the layer 3 address).

--gateway 1.2.3.4

only matches if the packet is destined to 1.2.3.4 as a ROUTE, i.e. 1.2.3.4 is not also the target address.

--nexthop 1.2.3.4

matches if the next hop is specified as 1.2.3.4 either as a gateway or as a final destination.

It can't do magic, and match on non-routed aliases of routers, it only matches the targeted IP address from which the layer 2 address has been (or will be) actually derived.

The .gateway-test doesn't work for me because I don't build into my kernel source dir, but I tried to do it right for a public release.

Signed-of by: Sam Liddicott <[email protected]>

libipt_gateway.c new file +155 -0
@@ -0,0 +1,155 @@
+/* Shared library add-on to iptables to add gateway IP address matching support. 
+   Based on iprange
+   (C) UFO Mechanic <[email protected]> */
+#include <stdio.h>
+#include <netdb.h>
+#include <string.h>
+#include <stdlib.h>
+#include <getopt.h>
+
+#include <iptables.h>
+#include <linux/netfilter_ipv4/ipt_gateway.h>
+
+/* Function which prints out usage message. */
+static void
+help(void)
+{
+	printf(
+"gateway match v%s options:\n"
+"[!] --gateway ip        Match IP address of routed gateway\n"
+"\n",
+IPTABLES_VERSION);
+}
+
+static struct option opts[] = {
+	{ "gateway", 1, 0, '1' },
+	{ "nexthop", 1, 0, '2' },
+	{0}
+};
+
+/* Function which parses command options; returns true if it
+   ate an option */
+static int
+parse(int c, char **argv, int invert, unsigned int *flags,
+      const struct ipt_entry *entry,
+      unsigned int *nfcache,
+      struct ipt_entry_match **match)
+{
+	struct ipt_gateway_info *info = (struct ipt_gateway_info *)(*match)->data;
+	struct in_addr *ip;
+
+	switch (c) {
+	case '1':
+		if (*flags)
+			exit_error(PARAMETER_PROBLEM,
+				"gateway match: You can't specify `--gateway' and `--nexthop'");
+
+		check_inverse(optarg, &invert, &optind, 0);
+		if (invert) {
+			info->flags |= IPT_GATEWAY_INV;
+		}
+
+		ip = dotted_to_addr(optarg);
+		if (!ip)
+			exit_error(PARAMETER_PROBLEM, "gateway match: Bad IP address `%s'\n", 
+					optarg);
+		info->gateway = ip->s_addr;
+		info->flags |= IPT_GATEWAY_ROUTE;
+
+		*flags=1;
+
+		break;
+
+	case '2':
+		if (*flags)
+			exit_error(PARAMETER_PROBLEM,
+				"gateway match: You can't specify `--gateway' and `--nexthop'");
+
+		check_inverse(optarg, &invert, &optind, 0);
+		if (invert) {
+			info->flags |= IPT_GATEWAY_INV;
+		}
+
+		ip = dotted_to_addr(optarg);
+		if (!ip)
+			exit_error(PARAMETER_PROBLEM, "gateway match: Bad IP address `%s'\n", 
+					optarg);
+		info->gateway = ip->s_addr;
+		info->flags ^= info->flags & IPT_GATEWAY_ROUTE;
+
+		*flags=1;
+
+		break;
+
+	default:
+		return 0;
+	}
+	return 1;
+}
+
+/* Final check; must have specified --gateway */
+static void
+final_check(unsigned int flags)
+{
+	if (!flags)
+		exit_error(PARAMETER_PROBLEM,
+			"gateway match: You must specify `--gateway' or `--nexthop'");
+}
+
+/* Prints out the info. */
+static void
+print(const struct ipt_ip *ip,
+      const struct ipt_entry_match *match,
+      int numeric)
+{
+	struct ipt_gateway_info *info = (struct ipt_gateway_info *)match->data;
+	struct in_addr a;
+
+	a.s_addr = info->gateway;
+
+	if (info->flags & IPT_GATEWAY_ROUTE) {
+		printf("gateway ");
+	} else {
+		printf("nexthop ");
+	}
+	if (info->flags & IPT_GATEWAY_INV) printf("! ");
+	printf("%s", addr_to_dotted(&a));
+}
+
+/* Saves the union ipt_info in parsable form to stdout. */
+static void
+save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
+{
+	struct ipt_gateway_info *info = (struct ipt_gateway_info *)match->data;
+	struct in_addr a;
+
+	a.s_addr = info->gateway;
+
+	if (info->flags & IPT_GATEWAY_INV)
+		printf("! ");
+	if (info->flags & IPT_GATEWAY_ROUTE) {
+		printf("--gateway ");
+	} else {
+		printf("--nexthop ");
+	}
+	printf("%s ", addr_to_dotted(&a));
+}
+
+static struct iptables_match gateway = { 
+	.next		= NULL,
+	.name		= "gateway",
+	.version	= IPTABLES_VERSION,
+	.size		= IPT_ALIGN(sizeof(struct ipt_gateway_info)),
+	.userspacesize	= IPT_ALIGN(sizeof(struct ipt_gateway_info)),
+	.help		= &help,
+	.parse		= &parse,
+	.final_check	= &final_check,
+	.print		= &print,
+	.save		= &save,
+	.extra_opts	= opts
+};
+
+void _init(void)
+{
+	register_match(&gateway);
+}
.gateway-test new file +3 -0
@@ -0,0 +1,3 @@
+#!/bin/sh
+# True if gateway match patch is applied.
+[ -f $KERNEL_DIR/include/linux/netfilter_ipv4/ipt_gateway.h ] && echo gateway
extensions/libipt_gateway.man new file +9 -0
@@ -0,0 +1,9 @@
+This matches the gateway by IP address on routed packets.
+It does not mach packets that are not routed, or which
+are directly addresses to the gateway.
+.TP
+.BI "[!]" "--gateway " "ip"
+Check that the packet is routed to a gateway with the specified ip address.
+.BI "[!]" "--nexthop " "ip"
+Check if the packet is being directed to the specified ip address
+either directly or as a route.
include/linux/netfilter_ipv4/ipt_gateway.h new file +13 -0
Index: linux-2.6.17.1/include/linux/netfilter_ipv4/ipt_gateway.h
===================================================================
@@ -0,0 +1,13 @@
+#ifndef _IPT_GATEWAY_H
+#define _IPT_GATEWAY_H
+
+#define IPT_GATEWAY_INV		0x1	/* Negate the condition */
+#define IPT_GATEWAY_ROUTE	0x2	/* ...and the gateway is not the final hop */
+
+struct ipt_gateway_info {
+	/* Inclusive: network order. */
+	u_int32_t gateway;
+	u_int8_t flags;
+};
+
+#endif /* _IPT_GATEWAY_H */
net/ipv4/netfilter/Kconfig +9 -0
Index: linux-2.6.17.1/net/ipv4/netfilter/Kconfig
===================================================================
@@ -361,6 +361,15 @@ config IP_NF_MATCH_IPRANGE
 
 	  To compile it as a module, choose M here.  If unsure, say N.
 
+config IP_NF_MATCH_GATEWAY
+	tristate "IP gateway match support"
+	depends on IP_NF_IPTABLES
+	help
+	  This option makes possible to match the IP address of the
+	  routed gateway for routed packets.
+
+	  To compile it as a module, choose M here.  If unsure, say N.
+
 config IP_NF_MATCH_TOS
 	tristate "TOS match support"
 	depends on IP_NF_IPTABLES
net/ipv4/netfilter/Makefile +1 -0
Index: linux-2.6.17.1/net/ipv4/netfilter/Makefile
===================================================================
@@ -53,6 +53,7 @@ obj-$(CONFIG_IP_NF_RAW) += iptable_raw.o
 # matches
 obj-$(CONFIG_IP_NF_MATCH_HASHLIMIT) += ipt_hashlimit.o
 obj-$(CONFIG_IP_NF_MATCH_IPRANGE) += ipt_iprange.o
+obj-$(CONFIG_IP_NF_MATCH_GATEWAY) += ipt_gateway.o
 obj-$(CONFIG_IP_NF_MATCH_OWNER) += ipt_owner.o
 obj-$(CONFIG_IP_NF_MATCH_TOS) += ipt_tos.o
 obj-$(CONFIG_IP_NF_MATCH_RECENT) += ipt_recent.o
net/ipv4/netfilter/ipt_gateway.c new file +69 -0

69 lines not on this page · raw message

#1 lore raw
Quoted 10 lines · Amin Azez

This adds a gateway match to iptables that lets you match against the routed ipv4 gateway, it's very useful for SNAT if you want to avoid replicating your routing in your SNAT table.

e.g.

iptables -t nat -A POSTROUTING -m gateway --nexthop 172.16.1.1 -j SNAT --to-address 172.16.1.5
iptables -t nat -A POSTROUTING -m gateway --nexthop 192.168.1.1 -j SNAT --to-address 192.168.1.25

I'm wondering whether we really need a new match for this. It should be possible to do the same using routing realms and the realm match.

author
#2 lore raw
Quoted 15 lines · Patrick McHardy
Quoted 11 lines · Amin Azez

This adds a gateway match to iptables that lets you match against the routed ipv4 gateway, it's very useful for SNAT if you want to avoid replicating your routing in your SNAT table.

e.g.

iptables -t nat -A POSTROUTING -m gateway --nexthop 172.16.1.1 -j SNAT --to-address 172.16.1.5
iptables -t nat -A POSTROUTING -m gateway --nexthop 192.168.1.1 -j SNAT --to-address 192.168.1.25

Quoted 3 lines

I'm wondering whether we really need a new match for this. It should be possible to do the same using routing realms and the realm match.

It's possible that it could be managed using realms, but THAT would be a hack, and one very hard for rule generating systems to use, especially if realms were already in use.

The match as used here is purely for ip<->ip routing compatability, easy auto generation of SNATing rules when the next hop router doesn't have a reverse route.

It's also useful for collecting per-gateway statistics (esp. with load balancing) and debugging complex routing.

It will also be useful to most people who won't or can't bend realms to their will.

I don't know if realms will help in the load balancing routing case anyway.

Sam

#3 lore raw
Quoted 9 lines · Amin Azez
Quoted 3 lines · Patrick McHardy

I'm wondering whether we really need a new match for this. It should be possible to do the same using routing realms and the realm match.

It's possible that it could be managed using realms, but THAT would be a hack, and one very hard for rule generating systems to use, especially if realms were already in use.

I don't consider that this a hack. Its even more useful since you can do masked matches. I also don't see the problem for generated rules, in fact I used them for exactly this (and other) purpose in a rule generating system.

Quoted 11 lines

The match as used here is purely for ip<->ip routing compatability, easy auto generation of SNATing rules when the next hop router doesn't have a reverse route.

It's also useful for collecting per-gateway statistics (esp. with load balancing) and debugging complex routing.

It will also be useful to most people who won't or can't bend realms to their will.

I don't know if realms will help in the load balancing routing case anyway.

Yes, you can use one realm per nexthop.

#4 lore raw
In reply to Amin Azez

fre 2007-06-01 klockan 17:47 +0100 skrev Amin Azez:

Quoted 10 lines

This adds a gateway match to iptables that lets you match against the routed ipv4 gateway, it's very useful for SNAT if you want to avoid replicating your routing in your SNAT table.

e.g.

iptables -t nat -A POSTROUTING -m gateway --nexthop 172.16.1.1 -j SNAT --to-address 172.16.1.5
iptables -t nat -A POSTROUTING -m gateway --nexthop 192.168.1.1 -j SNAT --to-address 192.168.1.25

Cool. Been needing something like this many times in complex environments with multiple links needing NAT based on the link and not source/destination.. Helps keeping NAT table nice and clean with a direct mapping to the network infrastructure rather than addresses..

Regards
Henrik

#5 lore raw

PATCH 1/1 xt_gateway match

In reply to Amin Azez
Quoted 7 lines · Amin Azez

This adds a gateway match to iptables that lets you match against the routed ipv4 gateway, it's very useful for SNAT if you want to avoid replicating your routing in your SNAT table.

e.g.

Quoted 7 lines

iptables -t nat -A POSTROUTING -m gateway --nexthop 172.16.1.1 -j SNAT --to-address 172.16.1.5
iptables -t nat -A POSTROUTING -m gateway --nexthop 192.168.1.1 -j SNAT --to-address 192.168.1.25

The .gateway-test doesn't work for me because I don't build into my kernel source dir, but I tried to do it right for a public release.

(Neither do I, just run KERNEL_DIR=/ws/linux-2.6.22-rc3 make in the iptables directory.)

+ info->flags ^= info->flags & IPT_GATEWAY_ROUTE;

Stunning line.

So, I refreshed this to be xtables-style, xt_gateway. It builds cleanly, but only done limited testing on it yet. Especially, I decoupled that bigass return statement to make it easier to read. I hope I got all the conditions right.

How things look:
inet 192.168.222.36/24
default gw 192.168.222.1

What I did:
iptables -A OUTPUT -m gateway --gateway 192.168.222.1 iptables -A OUTPUT -m gateway --nexthop 192.168.222.1 ping -c1 192.168.222.1
iptables -nvL

    +1 for the --gateway rule
    +1 for the --nexthop rule
 ping -c1 134.76.13.21
    +1 for the --gateway rule
    +0 for the --nexthop rule

Route to 134.76.13.21 is:
(192.168.222.36)

    192.168.222.1
    10.10.96.1
    134.76.63.254
    134.76.13.21

Does xt_gateway still do the right thing? Please check, thanks!

(patches as a response to this mail,
or svnized for now @
https://dev.computergmbh.de/svn/misc_kernel/xt_gateway/trunk/ )

Jan

#6 lore raw

Re: [PATCH] xt_gateway match (kernel)

Originally from Amin Azez <[email protected]>,
http://lists.netfilter.org/pipermail/netfilter-devel/2007-June/027954.html

This adds a gateway match to iptables that lets you match against the routed ipv4 gateway, it's very useful for SNAT if you want to avoid replicating your routing in your SNAT table.

e.g.

	iptables -t nat -A POSTROUTING -m gateway --nexthop 172.16.1.1 \
		-j SNAT --to-address 172.16.1.5
	iptables -t nat -A POSTROUTING -m gateway --nexthop 192.168.1.1 \
		-j SNAT --to-address 192.168.1.25

to help you choose the right SNAT address.

It works by comparing the to-be-matched gateway IP with the key in the neighbor table of the next-hop (the key is the layer 3 address).

	--gateway 1.2.3.4

only matches if the packet is destined to 1.2.3.4 as a ROUTE, i.e. 1.2.3.4 is not also the target address.

	--nexthop 1.2.3.4

matches if the next hop is specified as 1.2.3.4 either as a gateway or as a final destination.

It cannot do magic, and match on non-routed aliases of routers, it only matches the targeted IP address from which the layer 2 address has been (or will be) actually derived.

[Posted to LKML/NF-DEV on 2007-06-02]
---
 include/linux/netfilter/xt_gateway.h |   13 +++++
 net/netfilter/Kconfig                |    9 +++
 net/netfilter/Makefile               |    1 
 net/netfilter/xt_gateway.c           |   85 +++++++++++++++++++++++++++++++++++
 4 files changed, 108 insertions(+)
include/linux/netfilter/xt_gateway.h new file +13 -0
Index: linux-2.6.22-rc3-git6/include/linux/netfilter/xt_gateway.h
===================================================================
@@ -0,0 +1,13 @@
+#ifndef _XT_GATEWAY_H
+#define _XT_GATEWAY_H
+
+#define XT_GATEWAY_INV		0x1	/* Negate the condition */
+#define XT_GATEWAY_ROUTE	0x2	/* ...and the gateway is not the final hop */
+
+struct xt_gateway_info {
+	/* Inclusive: network order. */
+	uint32_t gateway;
+	uint8_t flags;
+};
+
+#endif /* _XT_GATEWAY_H */
net/netfilter/Kconfig +9 -0
Index: linux-2.6.22-rc3-git6/net/netfilter/Kconfig
===================================================================
@@ -468,6 +468,15 @@ config NETFILTER_XT_MATCH_ESP
 
 	  To compile it as a module, choose M here.  If unsure, say N.
 
+config NETFILTER_XT_MATCH_GATEWAY
+	tristate '"gateway" match support'
+	depends on NETFILTER_XTABLES
+	---help---
+	  This option makes possible to match the IP address of the
+	  routed gateway for routed packets.
+
+	  To compile it as a module, choose M here. If unsure, say N.
+
 config NETFILTER_XT_MATCH_HELPER
 	tristate '"helper" match support'
 	depends on NETFILTER_XTABLES
net/netfilter/Makefile +1 -0
Index: linux-2.6.22-rc3-git6/net/netfilter/Makefile
===================================================================
@@ -56,6 +56,7 @@ obj-$(CONFIG_NETFILTER_XT_MATCH_CONNTRAC
 obj-$(CONFIG_NETFILTER_XT_MATCH_DCCP) += xt_dccp.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_DSCP) += xt_dscp.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_ESP) += xt_esp.o
+obj-$(CONFIG_NETFILTER_XT_MATCH_GATEWAY) += xt_gateway.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_HELPER) += xt_helper.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_LENGTH) += xt_length.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_LIMIT) += xt_limit.o
net/netfilter/xt_gateway.c new file +85 -0

85 lines not on this page · raw message

#7 lore raw

Re: [PATCH] xt_gateway match (kernel,2)

Quoted 6 lines · Jan Engelhardt

---

include/linux/netfilter/xt_gateway.h |   13 +++++
net/netfilter/Kconfig                |    9 +++
net/netfilter/Makefile               |    1 
net/netfilter/xt_gateway.c           |   85 +++++++++++++++++++++++++++++++++++
4 files changed, 108 insertions(+)

A MODULE_ALIAS was missing, causing iptables/netfilter not to autoload the module.
SVN tree¹ updated (grab the refreshed patch from there, not reposting for this minimal change).

Jan

Signature · 1 line
[¹] https://dev.computergmbh.de/svn/misc_kernel/xt_gateway/trunk/
#8 lore raw

Re: [PATCH] xt_gateway match (iptables)

In reply to Jan Engelhardt
---
 extensions/.gateway-test      |    3 
 extensions/libipt_gateway.c   |  157 ++++++++++++++++++++++++++++++++++++++++++
 extensions/libipt_gateway.man |    9 ++
 3 files changed, 169 insertions(+)
extensions/.gateway-test new file +3 -0
Index: iptables/extensions/.gateway-test
===================================================================
@@ -0,0 +1,3 @@
+#!/bin/sh
+# True if gateway match patch is applied.
+[ -f "$KERNEL_DIR/include/linux/netfilter/xt_gateway.h" ] && echo gateway
extensions/libipt_gateway.c new file +157 -0

157 lines not on this page · raw message

extensions/libipt_gateway.man new file +9 -0
Index: iptables/extensions/libipt_gateway.man
===================================================================
@@ -0,0 +1,9 @@
+This matches the gateway by IP address on routed packets.
+It does not mach packets that are not routed, or which
+are directly addresses to the gateway.
+.TP
+.BI "[!]" "--gateway " "ip"
+Check that the packet is routed to a gateway with the specified ip address.
+.BI "[!]" "--nexthop " "ip"
+Check if the packet is being directed to the specified ip address
+either directly or as a route.