Skip to contentSkip to conversation

Find address type on the packet's interface

PATCH Superseded Laszlo Attila Toth 2 participants 5 messages Oct 24, 2007 – Nov 14, 2007

Subsystems (3)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover Find address type on the packet's interface

Hi Patrick,

This extension of ipt_addrtype match lets the address type checking be limited to the incoming or outgoing interface of the packets except in the FORWARD hook because either incoming or outgouing interface could be used.

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 latter one is for revision 1 only.

Usage:
iptables .... -m addrtype --...-type ... --limit-iface ...

Regards,
Attila

#1 lore raw

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

Address type search can be limited to any 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..4c82218 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 *on_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 78b514b..06ef507 100644
@@ -147,7 +147,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;
@@ -166,13 +170,24 @@ unsigned inet_addr_type(__be32 addr)
 		ret = RTN_UNICAST;
 		if (!ip_fib_local_table->tb_lookup(ip_fib_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.
@@ -922,4 +937,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 v2 2/2 Addrtype match extension: limit addrtype check on the packet's interface

Addrtype match has a new revision (1), which lets address type checking limit to the interface the current packet belongs to. The limitation cannot be applied in the FORWARD hook.

Revision 0 lets older userspace programs use the match as earlier.

---
 include/linux/netfilter_ipv4/ipt_addrtype.h |   15 ++++-
 net/ipv4/netfilter/ipt_addrtype.c           |   90 +++++++++++++++++++++-----
 2 files changed, 86 insertions(+), 19 deletions(-)
include/linux/netfilter_ipv4/ipt_addrtype.h +14 -1
index 166ed01..856c79d 100644
@@ -1,7 +1,20 @@
 #ifndef _IPT_ADDRTYPE_H
 #define _IPT_ADDRTYPE_H
 
-struct ipt_addrtype_info {
+enum
+{
+	IPT_ADDRTYPE_INVERT_SOURCE = 0x0001,
+	IPT_ADDRTYPE_INVERT_DEST   = 0x0002,
+	IPT_ADDRTYPE_LIMIT_IFACE   = 0x0004,
+};
+
+struct ipt_addrtype_info_v1 {
+	u_int16_t	source;		/* source-type mask */
+	u_int16_t	dest;		/* dest-type mask */
+	u_int32_t       flags;
+};
+
+struct ipt_addrtype_info_v0 {
 	u_int16_t	source;		/* source-type mask */
 	u_int16_t	dest;		/* dest-type mask */
 	u_int32_t	invert_source;
net/ipv4/netfilter/ipt_addrtype.c +72 -18

116 lines not on this page · raw message

Signature · 1 line
1.5.2.5
#3 lore raw

PATCH v2 1/1 Address type match: limited to incoming or outgoing interface

Address type checking can be limited to the incoming or outgoing interface depending on the current chain if it is not the FORWARD chain.

Older version (revision 0) of address type match is not supported.

---
extensions/libipt_addrtype.c | 74 +++++++++++++++++-----------

 extensions/libipt_addrtype.man              |   11 ++++
 include/linux/netfilter_ipv4/ipt_addrtype.h |   15 +++++
 3 files changed, 70 insertions(+), 30 deletions(-)
linux/netfilter_ipv4/ipt_addrtype.h +14 -1
Index: include/linux/netfilter_ipv4/ipt_addrtype.h
===================================================================
@@ -1,9 +1,22 @@
 #ifndef _IPT_ADDRTYPE_H
 #define _IPT_ADDRTYPE_H
 
-struct ipt_addrtype_info {
+enum
+{
+	IPT_ADDRTYPE_INVERT_SOURCE = 0x0001,
+	IPT_ADDRTYPE_INVERT_DEST   = 0x0002,
+	IPT_ADDRTYPE_LIMIT_IFACE   = 0x0004,
+};
+
+struct ipt_addrtype_info_v1 {
 	u_int16_t	source;		/* source-type mask */
 	u_int16_t	dest;		/* dest-type mask */
+	u_int32_t       flags;
+};
+
+struct ipt_addrtype_info_v0 {
+	u_int16_t	source;		/* source-type mask */
+	u_int16_t	dest;		/* dest-type mask */
 	u_int32_t	invert_source;
 	u_int32_t	invert_dest;
 };
libipt_addrtype.c +45 -29

179 lines not on this page · raw message

libipt_addrtype.man +11 -0
Index: extensions/libipt_addrtype.man
===================================================================
@@ -35,3 +35,14 @@
 .TP
 .BI "--dst-type " "type"
 Matches if the destination address is of given type
+.TP
+.BI "--limit-iface"
+The address type checing can be limited to the interface the packet is coming in in the
+.B PREROUTING
+and
+.B INPUT
+or going out in the
+.B OUTPUT
+and
+.B POSTROUTING
+chains and user-defined chains which are only called from those chains.
#4 lore raw

Re: [PATCHv2 2/2] Addrtype match extension: limit addrtype check on the packet's interface

In reply to Laszlo Attila Toth
Quoted 5 lines · Laszlo Attila Toth

Addrtype match has a new revision (1), which lets address type checking limit to the interface the current packet belongs to. The limitation cannot be applied in the FORWARD hook.

Revision 0 lets older userspace programs use the match as earlier.

Dave has opened his net-2.6.25 tree, so time to look at this again.

net/ipv4/netfilter/ipt_addrtype.c MODULE_LICENSE("GPL"); · line 59

Quoted 13 lines
net/ipv4/netfilter/ipt_addrtype.c +13 -0
+static bool addrtype_match_v1(const struct sk_buff *skb,
+		const struct net_device *in, const struct net_device *out,
+		const struct xt_match *match, const void *matchinfo,
+		int offset, unsigned int protoff, bool *hotdrop)
+{
+	const struct ipt_addrtype_info_v1 *info = matchinfo;
+	const struct iphdr *iph = ip_hdr(skb);
+	const struct net_device *limit_dev = NULL;
+	bool ret = true;
+
+	/* not valid in the FORWARD hook */
+	if (info->flags & IPT_ADDRTYPE_LIMIT_IFACE)
+		limit_dev = (in ? in : out); 

I would prefer if the user specifies the device to use (in/out) and have proper checks that its not used on hooks where its invalid. That would also allow to use it in the FORWARD hook. Using a single device also doesn't seem to make much sense in case the match is on both source and dest.

net/ipv4/netfilter/ipt_addrtype.c MODULE_LICENSE("GPL"); · line 64

Quoted 5 lines
net/ipv4/netfilter/ipt_addrtype.c +5 -0
+
+	if (info->source)
+		ret &= match_type(iph->saddr, limit_dev, info->source) ^
+			(info->flags & IPT_ADDRTYPE_INVERT_SOURCE);
+	if (ret && (info->dest))

Unnecessary parens.

Quoted 4 lines
net/ipv4/netfilter/ipt_addrtype.c +4 -0
+		ret &= match_type(iph->daddr, limit_dev, info->dest) ^
+			(info->flags & IPT_ADDRTYPE_INVERT_DEST);
+	
+	return ret;

net/ipv4/netfilter/ipt_addrtype.c MODULE_LICENSE("GPL"); · line 78

Quoted 10 lines
net/ipv4/netfilter/ipt_addrtype.c +10 -0
+}
+
+static bool addrtype_checkentry_v1(const char *tablename, const void *ip_void,
+		const struct xt_match *match,
+		void *matchinfo, unsigned int hook_mask)
+{
+	struct ipt_addrtype_info_v1 *info = matchinfo;
+
+	if (hook_mask & (1 << NF_IP_FORWARD)
+	   && info->flags & IPT_ADDRTYPE_LIMIT_IFACE) {

Please don't reintroduce the weird && on continuation line style, I try to get rid of it whenever I touch some code.