Skip to contentSkip to conversation

Kernel netfilter and iptables gateway extension patch

PATCH Bryan Duff 2 participants 2 messages Sep 23, 2008

Subsystems (2)
lore
Share & tools
lore

Bryan Duff

author
#0 lore raw

From the manpage:

+This matches the gateway by IP address on routed packets. +It does not match packets that are not routed, or which +are directly addresses to the gateway.

With this patch it doesn't work quite like that. It now matches packets that are "directly addresses to the gateway". Because frankly, why not? Otherwise those addresses are missed, and it requires more rules. It also changes the iptables interface to allow the specifying of a mask on the cli.

//e.g. (assume my interface is 192.168.1.100, gateway 192.168.1.1):
iptables -t mangle -A POSTROUTING -o eth0 -m gateway --gateway 192.168.1.1/24 //end e.g.

Now any packets routed out eth0 to gateway 192.168.1.1 (or anything on that subnet - if I send to 192.168.1.2 - then that is the gateway, and also matches). This is most useful for multi-homed ports. The gateway and nexthop options do the same thing.

I'm not sure if this is useful to anyone but me. I can clean up the patches (fix the man page and whatever else).

Thanks.

-Bryan Duff
[email protected]

---
 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.

-

extensions/Makefile +1 -1
Index: iptables/extensions/Makefile
===================================================================
@@ -5,7 +5,7 @@
 # header files are present in the include/linux directory of this iptables
 # package (HW)
 #
-PF_EXT_SLIB:=ah addrtype conntrack ecn icmp iprange owner policy realm recent tos ttl unclean CLUSTERIP DNAT ECN LOG MASQUERADE MIRROR NETMAP REDIRECT REJECT SAME SNAT TOS TTL ULOG
+PF_EXT_SLIB:=ah addrtype conntrack ecn icmp iprange owner policy realm recent tos ttl unclean CLUSTERIP DNAT ECN LOG MASQUERADE MIRROR NETMAP REDIRECT REJECT SAME SNAT TOS TTL ULOG gateway
 PF6_EXT_SLIB:=ah dst eui64 frag hbh hl icmp6 ipv6header mh owner policy rt HL LOG REJECT
 PFX_EXT_SLIB:=connbytes connmark connlimit comment dccp dscp esp hashlimit helper length limit mac mark multiport physdev pkttype quota sctp state statistic standard string tcp tcpmss time u32 udp CLASSIFY CONNMARK DSCP MARK NFLOG NFQUEUE NOTRACK TCPMSS TRACE
 
include/linux/netfilter/xt_gateway.h +14 -0
diff -Naur linux-2.6.24-gentoo-r3/include/linux/netfilter/xt_gateway.h linux-2.6.24-gentoo-r3-NEW/include/linux/netfilter/xt_gateway.h
@@ -0,0 +1,14 @@
+#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;
+	uint32_t mask;
+	uint8_t flags;
+};
+
+#endif /* _XT_GATEWAY_H */
net/netfilter/Kconfig +9 -0
diff -Naur linux-2.6.24-gentoo-r3/net/netfilter/Kconfig linux-2.6.24-gentoo-r3-NEW/net/netfilter/Kconfig
@@ -498,6 +498,15 @@
 
 	  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
diff -Naur linux-2.6.24-gentoo-r3/net/netfilter/Makefile linux-2.6.24-gentoo-r3-NEW/net/netfilter/Makefile
@@ -59,6 +59,7 @@
 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_HASHLIMIT) += xt_hashlimit.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_HELPER) += xt_helper.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_LENGTH) += xt_length.o
net/netfilter/xt_gateway.c +84 -0

84 lines not on this page · raw message

#1 lore raw
Quoted 12 lines · Bryan Duff

From the manpage:

extensions/libipt_gateway.man new file +3 -0
+This matches the gateway by IP address on routed packets.
+It does not match packets that are not routed, or which
+are directly addresses to the gateway.

With this patch it doesn't work quite like that. It now matches packets that are "directly addresses to the gateway". Because frankly, why not? Otherwise those addresses are missed, and it requires more rules. It also changes the iptables interface to allow the specifying of a mask on the cli.

Quoted 8 lines

//e.g. (assume my interface is 192.168.1.100, gateway 192.168.1.1):
iptables -t mangle -A POSTROUTING -o eth0 -m gateway --gateway 192.168.1.1/24 //end e.g.

Now any packets routed out eth0 to gateway 192.168.1.1 (or anything on that subnet - if I send to 192.168.1.2 - then that is the gateway, and also matches). This is most useful for multi-homed ports. The gateway and nexthop options do the same thing.

It should not. Assuming you have a route "192.168.1.0/24 dev eth0", then gateway should be 0.0.0.0 for any packets to 192.168.1.2 and the nexthop shoul dbe 192.168.1.2.

For packets going to 123.45.67.89, gateway and nexthop would be 192.168.1.1.

Quoted 2 lines

I'm not sure if this is useful to anyone but me. I can clean up the patches (fix the man page and whatever else).

No, xt_gateway (you're even using the even-older ipt_gateway) has been deprecated a few days after the patch was posted, as you can use routing realms to mark based on gateway/nexthop.