Skip to contentSkip to conversation

Find address type on the packet's interface

PATCH Superseded Laszlo Attila Toth 3 participants 10 messages Nov 19, 2007 – Nov 20, 2007

Subsystems (4)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover Find address type on the packet's interface

Hi Patrick,

This extension of addrtype match lets the address type checking be limited to the incoming or outgoing interface of the packets depending on the current hook.

In the FORWARD chain only one check is allowed but the user can choose which one would like to specifiy.

Because of this extension the match has a new revision. Rev 0 can be used by older tools and rev 1 is for the modified iptables match.

The iptables patch is for revision 1 only.

Both the kernel module and the iptables module moved to xtables, but the kernel module uses ipt_addrtype_info in revision 0.

Usage:
iptables -A INPUT -m addrtype ... --limit-iface-in -j ACCEPT iptables -A OUTPUT -m addrtype ... --limit-iface-out -j ACCEPT # 2 rules in the FORWARD chain
iptables -A FORWARD -m addrtype ... --limit-iface-in -j ACCEPT iptables -A FORWARD -m addrtype ... --limit-iface-out -j ACCEPT

Patches:
[kernel 1/2] Find address type on a specific or on any interface [kernel 2/2] Addrtype match: limit addrtype check to an interface.
Moved to xtables
[iptables] Adress type match: limited to incoming or outgoing interface.
Moved to xtables

Regards,
Attila

#1 lore raw

PATCH v4 1/2 Find address type on a specific or on any interface

Address type search can be limited to an interface by inet_dev_addr_type function.

---
 include/net/route.h     |    1 +
 net/ipv4/fib_frontend.c |   20 ++++++++++++++++++--
 2 files changed, 19 insertions(+), 2 deletions(-)
include/net/route.h +1 -0
index f7ce625..d155c29 100644
@@ -119,6 +119,7 @@ extern unsigned short	ip_rt_frag_needed(struct iphdr *iph, unsigned short new_mt
 extern void		ip_rt_send_redirect(struct sk_buff *skb);
 
 extern unsigned		inet_addr_type(__be32 addr);
+extern unsigned		inet_dev_addr_type(__be32 addr, const struct net_device *dev);
 extern void		ip_rt_multicast_event(struct in_device *);
 extern int		ip_rt_ioctl(unsigned int cmd, void __user *arg);
 extern void		ip_rt_get_source(u8 *src, struct rtable *rt);
net/ipv4/fib_frontend.c +18 -2
index 732d8f0..f116f61 100644
@@ -155,7 +155,11 @@ out:
 	return dev;
 }
 
-unsigned inet_addr_type(__be32 addr)
+/*
+ * Find address type as if only "dev" was present in the system. If
+ * on_dev is NULL then all interfaces are taken into consideration.
+ */
+static inline unsigned __inet_dev_addr_type(__be32 addr, const struct net_device *dev)
 {
 	struct flowi		fl = { .nl_u = { .ip4_u = { .daddr = addr } } };
 	struct fib_result	res;
@@ -175,13 +179,24 @@ unsigned inet_addr_type(__be32 addr)
 	if (local_table) {
 		ret = RTN_UNICAST;
 		if (!local_table->tb_lookup(local_table, &fl, &res)) {
-			ret = res.type;
+			if ((!dev || dev == res.fi->fib_dev))
+				ret = res.type;
 			fib_res_put(&res);
 		}
 	}
 	return ret;
 }
 
+unsigned inet_addr_type(__be32 addr)
+{
+	return __inet_dev_addr_type(addr, NULL);
+}
+
+unsigned inet_dev_addr_type(__be32 addr, const struct net_device *dev)
+{
+       return __inet_dev_addr_type(addr, dev);
+}
+
 /* Given (packet source, input interface) and optional (dst, oif, tos):
    - (main) check, that source is valid i.e. not broadcast or our local
      address.
@@ -925,4 +940,5 @@ void __init ip_fib_init(void)
 }
 
 EXPORT_SYMBOL(inet_addr_type);
+EXPORT_SYMBOL(inet_dev_addr_type);
 EXPORT_SYMBOL(ip_dev_find);
Signature · 1 line
1.5.2.5
#2 lore raw

PATCH v4 2/2 Addrtype match: limit addrtype check to an interface. Moved to xtables

Addrtype match has a new revision (1), which lets address type checking limited to the interface the current packet belongs to. Either incoming or outgoing interface can be used depending on the current hook. In the FORWARD hook two maches should be used if both interfaces have to be checked. This revision uses xt_addrtype.h and it is xt_addrtype.c

Revision 0 lets older userspace programs use the match as earlier. The old header, ipt_addrtype.h is used.

---
 include/linux/netfilter/xt_addrtype.h |   18 ++++
 net/ipv4/netfilter/Kconfig            |   10 --
 net/ipv4/netfilter/Makefile           |    1 -
 net/ipv4/netfilter/ipt_addrtype.c     |   66 ---------------
 net/netfilter/Kconfig                 |   12 +++-
 net/netfilter/Makefile                |    1 +
 net/netfilter/xt_addrtype.c           |  149 +++++++++++++++++++++++++++++++++
 7 files changed, 179 insertions(+), 78 deletions(-)
include/linux/netfilter/xt_addrtype.h new file +18 -0
new file mode 100644
index 0000000..e3f0fd5
@@ -0,0 +1,18 @@
+#ifndef _XT_ADDRTYPE_H
+#define _XT_ADDRTYPE_H
+
+enum
+{
+	XT_ADDRTYPE_INVERT_SOURCE	= 0x0001,
+	XT_ADDRTYPE_INVERT_DEST		= 0x0002,
+	XT_ADDRTYPE_LIMIT_IFACE_IN	= 0x0004,
+	XT_ADDRTYPE_LIMIT_IFACE_OUT	= 0x0008,
+};
+
+struct xt_addrtype_info {
+	u_int16_t	source;		/* source-type mask */
+	u_int16_t	dest;		/* dest-type mask */
+	u_int32_t	flags;
+};
+
+#endif
net/ipv4/netfilter/Kconfig +0 -10
index 9aca9c5..4086ab2 100644
@@ -120,16 +120,6 @@ config IP_NF_MATCH_OWNER
 
 	  To compile it as a module, choose M here.  If unsure, say N.
 
-config IP_NF_MATCH_ADDRTYPE
-	tristate  'address type match support'
-	depends on IP_NF_IPTABLES
-	help
-	  This option allows you to match what routing thinks of an address,
-	  eg. UNICAST, LOCAL, BROADCAST, ...
-	
-	  If you want to compile it as a module, say M here and read
-	  <file:Documentation/kbuild/modules.txt>.  If unsure, say `N'.
-
 # `filter', generic and specific targets
 config IP_NF_FILTER
 	tristate "Packet filtering"
net/ipv4/netfilter/Makefile +0 -1
index 7456833..f107ade 100644
@@ -41,7 +41,6 @@ obj-$(CONFIG_NF_NAT) += iptable_nat.o
 obj-$(CONFIG_IP_NF_RAW) += iptable_raw.o
 
 # matches
-obj-$(CONFIG_IP_NF_MATCH_ADDRTYPE) += ipt_addrtype.o
 obj-$(CONFIG_IP_NF_MATCH_AH) += ipt_ah.o
 obj-$(CONFIG_IP_NF_MATCH_ECN) += ipt_ecn.o
 obj-$(CONFIG_IP_NF_MATCH_IPRANGE) += ipt_iprange.o
net/ipv4/netfilter/ipt_addrtype.c deleted +0 -66

66 lines not on this page · raw message

net/netfilter/Kconfig +11 -1
index 21a9fcc..acff1db 100644
@@ -363,7 +363,7 @@ config NETFILTER_XT_TARGET_TRACE
 	  the tables, chains, rules.
 
 	  If you want to compile it as a module, say M here and read
-	  <file:Documentation/kbuild/modules.txt>.  If unsure, say `N'.
+	  <file:Documentation/kbuild/modules.txt>.  If unsure, say N.
 
 config NETFILTER_XT_TARGET_SECMARK
 	tristate '"SECMARK" target support'
@@ -411,6 +411,16 @@ config NETFILTER_XT_TARGET_TCPMSS
 
 	  To compile it as a module, choose M here.  If unsure, say N.
 
+config NETFILTER_XT_MATCH_ADDRTYPE
+	tristate  'address type match support'
+	depends on NETFILTER_XTABLES
+	help
+	  This option allows you to match what routing thinks of an address,
+	  eg. UNICAST, LOCAL, BROADCAST, ...
+	
+	  If you want to compile it as a module, say M here and read
+	  <file:Documentation/kbuild/modules.txt>.  If unsure, say N.
+
 config NETFILTER_XT_MATCH_COMMENT
 	tristate  '"comment" match support'
 	depends on NETFILTER_XTABLES
net/netfilter/Makefile +1 -0
index ad0e36e..22dac5c 100644
@@ -51,6 +51,7 @@ obj-$(CONFIG_NETFILTER_XT_TARGET_TCPMSS) += xt_TCPMSS.o
 obj-$(CONFIG_NETFILTER_XT_TARGET_TRACE) += xt_TRACE.o
 
 # matches
+obj-$(CONFIG_NETFILTER_XT_MATCH_ADDRTYPE)+= xt_addrtype.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_COMMENT) += xt_comment.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_CONNBYTES) += xt_connbytes.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_CONNLIMIT) += xt_connlimit.o
net/netfilter/xt_addrtype.c new file +149 -0

149 lines not on this page · raw message

Signature · 1 line
1.5.2.5
#3 lore raw

PATCH v4 1/1 Address type match: limited to incoming or outgoing interface. Moved to xtables

Address type checking can be limited to the incoming or outgoing interface depending on the current chain. In the FORWARD chain only one of them is allowed at the same time.

The match is moved to xtables and only the revision one is supported

The man page is updated to print the address in separate sections insted of a single paragraph.

---
 extensions/Makefile                         |    4 
 extensions/libipt_addrtype.c                |  203 -----------------------
 extensions/libipt_addrtype.man              |   35 ++++
 extensions/libxt_addrtype.c                 |  239 ++++++++++++++++++++++++++++
 include/linux/netfilter/xt_addrtype.h       |   18 ++
 include/linux/netfilter_ipv4/ipt_addrtype.h |   11 -
 6 files changed, 294 insertions(+), 216 deletions(-)
linux/netfilter/xt_addrtype.h +18 -0
Index: include/linux/netfilter/xt_addrtype.h
===================================================================
@@ -0,0 +1,18 @@
+#ifndef _XT_ADDRTYPE_H
+#define _XT_ADDRTYPE_H
+
+enum
+{
+	XT_ADDRTYPE_INVERT_SOURCE	= 0x0001,
+	XT_ADDRTYPE_INVERT_DEST		= 0x0002,
+	XT_ADDRTYPE_LIMIT_IFACE_IN	= 0x0004,
+	XT_ADDRTYPE_LIMIT_IFACE_OUT	= 0x0008,
+};
+
+struct xt_addrtype_info {
+	u_int16_t	source;		/* source-type mask */
+	u_int16_t	dest;		/* dest-type mask */
+	u_int32_t	flags;
+};
+
+#endif
linux/netfilter_ipv4/ipt_addrtype.h +0 -11
Index: include/linux/netfilter_ipv4/ipt_addrtype.h
===================================================================
@@ -1,11 +0,0 @@
-#ifndef _IPT_ADDRTYPE_H
-#define _IPT_ADDRTYPE_H
-
-struct ipt_addrtype_info {
-	u_int16_t	source;		/* source-type mask */
-	u_int16_t	dest;		/* dest-type mask */
-	u_int32_t	invert_source;
-	u_int32_t	invert_dest;
-};
-
-#endif
libipt_addrtype.c +0 -203

203 lines not on this page · raw message

libxt_addrtype.c +239 -0

239 lines not on this page · raw message

Makefile +2 -2
Index: extensions/Makefile
===================================================================
@@ -5,9 +5,9 @@
 # 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 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
 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
+PFX_EXT_SLIB:=addrtype 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
 
 PF_EXT_SELINUX_SLIB:=
 PF6_EXT_SELINUX_SLIB:=
libipt_addrtype.man +35 -0
Index: extensions/libipt_addrtype.man
===================================================================
@@ -7,31 +7,66 @@
 .TP
 .BI "UNSPEC"
 an unspecified address (i.e. 0.0.0.0)
+.TP
 .BI "UNICAST"
 an unicast address
+.TP
 .BI "LOCAL"
 a local address
+.TP
 .BI "BROADCAST"
 a broadcast address
+.TP
 .BI "ANYCAST"
 an anycast packet
+.TP
 .BI "MULTICAST"
 a multicast address
+.TP
 .BI "BLACKHOLE"
 a blackhole address
+.TP
 .BI "UNREACHABLE"
 an unreachable address
+.TP
 .BI "PROHIBIT"
 a prohibited address
+.TP
 .BI "THROW"
 FIXME
+.TP
 .BI "NAT"
 FIXME
+.TP
 .BI "XRESOLVE"
 FIXME
 .TP
+Options:
+.TP
 .BI "--src-type " "type"
 Matches if the source address is of given type
 .TP
 .BI "--dst-type " "type"
 Matches if the destination address is of given type
+.TP
+.BI "--limit-iface-in"
+The address type checking can be limited to the interface the packet is coming
+in. This option is only valid in the
+.BR PREROUTING ,
+.B INPUT
+and
+.B FORWARD
+chains. It cannot be specified with the
+.B "--limit-iface-out"
+option.
+.TP
+.BI "--limit-iface-out"
+The address type checiking can be limited to the interface the packet is going
+out. This option is only valid in the
+.BR POSTROUTING ,
+.B OUTPUT
+and
+.B FORWARD
+chains. It cannot be specified with the
+.B --limit-iface-in
+option.
#4 lore raw
In reply to Laszlo Attila Toth
Quoted 8 lines · Laszlo Attila Toth

Hi Patrick,

This extension of addrtype match lets the address type checking be limited to the incoming or outgoing interface of the packets depending on the current hook.

In the FORWARD chain only one check is allowed but the user can choose which one would like to specifiy.

Thanks for changing this.

Quoted 7 lines

Because of this extension the match has a new revision. Rev 0 can be used by older tools and rev 1 is for the modified iptables match.

The iptables patch is for revision 1 only.

Both the kernel module and the iptables module moved to xtables, but the kernel module uses ipt_addrtype_info in revision 0.

I just read up on your and Jan's discussion, but you were too fast for me :) I'm not sure whether this is really a good candidate for x_tables. IPv4 and IPv6 addrtype have different meanings, the IPv4 addrtype is based on routing, IPv6 solely on the address. Especially things like "--addrtype local" won't work, which is IMO the most useful feature. And since you don't actually add IPv6 support, I don't see any advantage in moving to x_tables. So I think for now I'd prefer a change to the ipt_addrtype match.

#5 lore raw
Quoted 9 lines · Patrick McHardy

I just read up on your and Jan's discussion, but you were too fast for me :) I'm not sure whether this is really a good candidate for x_tables. IPv4 and IPv6 addrtype have different meanings, the IPv4 addrtype is based on routing, IPv6 solely on the address. Especially things like "--addrtype local" won't work, which is IMO the most useful feature. And since you don't actually add IPv6 support, I don't see any advantage in moving to x_tables. So I think for now I'd prefer a change to the ipt_addrtype match.

IMHO it does not make any difference whether it is xt_*.c or ipt_*.c, the cost is quite the same.
I am all for xt_*.c, because that's the "new shiny" thing.

#6 lore raw
Quoted 10 lines · Jan Engelhardt
Quoted 8 lines · Patrick McHardy

I just read up on your and Jan's discussion, but you were too fast for me :) I'm not sure whether this is really a good candidate for x_tables. IPv4 and IPv6 addrtype have different meanings, the IPv4 addrtype is based on routing, IPv6 solely on the address. Especially things like "--addrtype local" won't work, which is IMO the most useful feature. And since you don't actually add IPv6 support, I don't see any advantage in moving to x_tables. So I think for now I'd prefer a change to the ipt_addrtype match.

Quoted 3 lines

IMHO it does not make any difference whether it is xt_*.c or ipt_*.c, the cost is quite the same.
I am all for xt_*.c, because that's the "new shiny" thing.

x_tables is meant for unified matches and targets, as long as theres nothing to unify, there's no point in moving it over. So far I think we only have a single xtables match that doesn't support both IPv4 and IPv6 (xt_conntrack), and I'd like to keep it that way.

#7 lore raw
Quoted 11 lines · Patrick McHardy
Quoted 10 lines · Jan Engelhardt
Quoted 8 lines · Patrick McHardy

I just read up on your and Jan's discussion, but you were too fast for me :) I'm not sure whether this is really a good candidate for x_tables. IPv4 and IPv6 addrtype have different meanings, the IPv4 addrtype is based on routing, IPv6 solely on the address. Especially things like "--addrtype local" won't work, which is IMO the most useful feature. And since you don't actually add IPv6 support, I don't see any advantage in moving to x_tables. So I think for now I'd prefer a change to the ipt_addrtype match.

Quoted 9 lines
Quoted 3 lines

IMHO it does not make any difference whether it is xt_*.c or ipt_*.c, the cost is quite the same.
I am all for xt_*.c, because that's the "new shiny" thing.

x_tables is meant for unified matches and targets, as long as theres nothing to unify, there's no point in moving it over. So far I think we only have a single xtables match that doesn't support both IPv4 and IPv6 (xt_conntrack), and I'd like to keep it that way.

Sorry, can't grant you that wish - I have plans to add IPv6 to xt_conntrack to obsolete ip6t_state, though maybe that takes a bit of time ;-)

#8 lore raw
Quoted 14 lines · Jan Engelhardt
Quoted 13 lines · Patrick McHardy
Quoted 12 lines · Jan Engelhardt
Quoted 8 lines · Patrick McHardy

I just read up on your and Jan's discussion, but you were too fast for me :) I'm not sure whether this is really a good candidate for x_tables. IPv4 and IPv6 addrtype have different meanings, the IPv4 addrtype is based on routing, IPv6 solely on the address. Especially things like "--addrtype local" won't work, which is IMO the most useful feature. And since you don't actually add IPv6 support, I don't see any advantage in moving to x_tables. So I think for now I'd prefer a change to the ipt_addrtype match.

IMHO it does not make any difference whether it is xt_*.c or ipt_*.c, the cost is quite the same.
I am all for xt_*.c, because that's the "new shiny" thing.

Quoted 7 lines
Quoted 5 lines

x_tables is meant for unified matches and targets, as long as theres nothing to unify, there's no point in moving it over. So far I think we only have a single xtables match that doesn't support both IPv4 and IPv6 (xt_conntrack), and I'd like to keep it that way.

Sorry, can't grant you that wish - I have plans to add IPv6 to xt_conntrack to obsolete ip6t_state, though maybe that takes a bit of time ;-)

The wish was not to add more pure IPv4 modules, I'm perfectly happy to finally add IPv6 support to xt_conntrack :)

#9 lore raw
In reply to Patrick McHardy

Patrick McHardy írta:

Quoted 11 lines
Quoted 10 lines · Jan Engelhardt
Quoted 8 lines · Patrick McHardy

I just read up on your and Jan's discussion, but you were too fast for me :) I'm not sure whether this is really a good candidate for x_tables. IPv4 and IPv6 addrtype have different meanings, the IPv4 addrtype is based on routing, IPv6 solely on the address. Especially things like "--addrtype local" won't work, which is IMO the most useful feature. And since you don't actually add IPv6 support, I don't see any advantage in moving to x_tables. So I think for now I'd prefer a change to the ipt_addrtype match.

Quoted 10 lines
Quoted 3 lines

IMHO it does not make any difference whether it is xt_*.c or ipt_*.c, the cost is quite the same.
I am all for xt_*.c, because that's the "new shiny" thing.

x_tables is meant for unified matches and targets, as long as theres nothing to unify, there's no point in moving it over. So far I think we only have a single xtables match that doesn't support both IPv4 and IPv6 (xt_conntrack), and I'd like to keep it that way.

I think x_tables is meant for similar functionality for IPv4 and IPv6 with minor differences. This eliminates possible code duplications, but the exactly same usage from the user's view is not required.

Signature · 6 lines
Attila

-
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html