Skip to contentSkip to conversation

netfilter: add support for matching IPv4 options

PATCH RESEND Superseded Stephen Suryaputra 2 participants 7 messages Jun 11, 2019 – Jun 19, 2019

Subsystems (4)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 netfilter: add support for matching IPv4 options

This is the kernel change for the overall changes with this description:
Add capability to have rules matching IPv4 options. This is developed mainly to support dropping of IP packets with loose and/or strict source route route options. Nevertheless, the implementation include others and ability to get specific fields in the option.

v2: Fix style issues. Make this work with NFPROTO_INET (inet tables),
NFPROTO_BRIDGE and the NFPROTO_NETDEV families. Check skb->protocol. Remove ability to input IP header offset for ipv4_find_option() function (all per Pablo Neira Ayuso).

---
 include/net/inet_sock.h                  |   2 +-
 include/uapi/linux/netfilter/nf_tables.h |   2 +
 net/ipv4/ip_options.c                    |   2 +
 net/netfilter/nft_exthdr.c               | 133 +++++++++++++++++++++++
 4 files changed, 138 insertions(+), 1 deletion(-)
include/net/inet_sock.h +1 -1
index e8eef85006aa..8db4f8639a33 100644
@@ -55,7 +55,7 @@ struct ip_options {
 			ts_needaddr:1;
 	unsigned char	router_alert;
 	unsigned char	cipso;
-	unsigned char	__pad2;
+	unsigned char	end;
 	unsigned char	__data[0];
 };
 
include/uapi/linux/netfilter/nf_tables.h +2 -0
index 505393c6e959..168d741f42c5 100644
@@ -730,10 +730,12 @@ enum nft_exthdr_flags {
  *
  * @NFT_EXTHDR_OP_IPV6: match against ipv6 extension headers
  * @NFT_EXTHDR_OP_TCP: match against tcp options
+ * @NFT_EXTHDR_OP_IPV4: match against ipv4 options
  */
 enum nft_exthdr_op {
 	NFT_EXTHDR_OP_IPV6,
 	NFT_EXTHDR_OP_TCPOPT,
+	NFT_EXTHDR_OP_IPV4,
 	__NFT_EXTHDR_OP_MAX
 };
 #define NFT_EXTHDR_OP_MAX	(__NFT_EXTHDR_OP_MAX - 1)
net/ipv4/ip_options.c +2 -0
index 3db31bb9df50..fc0e694aa97c 100644
@@ -272,6 +272,7 @@ int __ip_options_compile(struct net *net,
 	for (l = opt->optlen; l > 0; ) {
 		switch (*optptr) {
 		case IPOPT_END:
+			opt->end = optptr - iph;
 			for (optptr++, l--; l > 0; optptr++, l--) {
 				if (*optptr != IPOPT_END) {
 					*optptr = IPOPT_END;
@@ -473,6 +474,7 @@ int __ip_options_compile(struct net *net,
 		*info = htonl((pp_ptr-iph)<<24);
 	return -EINVAL;
 }
+EXPORT_SYMBOL(__ip_options_compile);
 
 int ip_options_compile(struct net *net,
 		       struct ip_options *opt, struct sk_buff *skb)
net/netfilter/nft_exthdr.c +133 -0

151 lines not on this page · raw message

Signature · 1 line
2.17.1
#1 lore raw

On Tue, Jun 11, 2019 at 08:09:12AM -0400, Stephen Suryaputra wrote:
[...]

Quoted 10 lines
net/netfilter/nft_exthdr.c +2 -0
index a940c9fd9045..4155a32fade7 100644
@@ -62,6 +62,125 @@ static void nft_exthdr_ipv6_eval(const struct nft_expr *expr,
 	regs->verdict.code = NFT_BREAK;
 }
 
+/* find the offset to specified option or the header beyond the options
+ * if target < 0.

net/netfilter/nft_exthdr.c static void nft_exthdr_ipv6_eval(const struct nft_expr *expr, · line 76

Quoted 10 lines
net/netfilter/nft_exthdr.c +10 -0
+ *
+ * If target header is found, its offset is set in *offset and return option
+ * number. Otherwise, return negative error.
+ *
+ * If the first fragment doesn't contain the End of Options it is considered
+ * invalid.
+ */
+static int ipv4_find_option(struct net *net, struct sk_buff *skb,
+			    unsigned int *offset, int target,
+			    unsigned short *fragoff, int *flags)

flags is never used, please remove it.

net/netfilter/nft_exthdr.c static void nft_exthdr_ipv6_eval(const struct nft_expr *expr, · line 78

Quoted 2 lines
net/netfilter/nft_exthdr.c +2 -0
+{
+	unsigned char optbuf[sizeof(struct ip_options) + 41];

In other parts of the kernel this is + 40:

net/ipv4/cipso_ipv4.c: unsigned char optbuf[sizeof(struct ip_options) + 40];

here it is + 41.

net/netfilter/nft_exthdr.c static void nft_exthdr_ipv6_eval(const struct nft_expr *expr, · line 87

Quoted 9 lines
net/netfilter/nft_exthdr.c +9 -0
+	struct ip_options *opt = (struct ip_options *)optbuf;
+	struct iphdr *iph, _iph;
+	unsigned int start;
+	bool found = false;
+	__be32 info;
+	int optlen;
+
+	if (fragoff)
+		*fragoff = 0;

fragoff is set and never used. Please, remove this parameter.

net/netfilter/nft_exthdr.c static void nft_exthdr_ipv6_eval(const struct nft_expr *expr, · line 100

Quoted 12 lines
net/netfilter/nft_exthdr.c +12 -0
+	iph = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
+	if (!iph || iph->version != 4)
+		return -EBADMSG;
+	start = sizeof(struct iphdr);
+
+	optlen = iph->ihl * 4 - (int)sizeof(struct iphdr);
+	if (optlen <= 0)
+		return -ENOENT;
+
+	memset(opt, 0, sizeof(struct ip_options));
+	/* Copy the options since __ip_options_compile() modifies
+	 * the options. Get one byte beyond the option for target < 0

How does this "one byte beyond the option" trick works?

Quoted 32 lines
net/netfilter/nft_exthdr.c +32 -0
+	 */
+	if (skb_copy_bits(skb, start, opt->__data, optlen + 1))
+		return -EBADMSG;
+	opt->optlen = optlen;
+
+	if (__ip_options_compile(net, opt, NULL, &info))
+		return -EBADMSG;
+
+	switch (target) {
+	case IPOPT_SSRR:
+	case IPOPT_LSRR:
+		if (!opt->srr)
+			break;
+		found = target == IPOPT_SSRR ? opt->is_strictroute :
+					       !opt->is_strictroute;
+		if (found)
+			*offset = opt->srr + start;
+		break;
+	case IPOPT_RR:
+		if (opt->rr)
+			break;
+		*offset = opt->rr + start;
+		found = true;
+		break;
+	case IPOPT_RA:
+		if (opt->router_alert)
+			break;
+		*offset = opt->router_alert + start;
+		found = true;
+		break;
+	default:
+		/* Either not supported or not a specific search, treated as

net/netfilter/nft_exthdr.c static void nft_exthdr_ipv6_eval(const struct nft_expr *expr, · line 142

Quoted 10 lines
net/netfilter/nft_exthdr.c +10 -0
+		 * found
+		 */
+		found = true;
+		if (target >= 0) {
+			target = -EOPNOTSUPP;
+			break;
+		}
+		if (opt->end) {
+			*offset = opt->end + start;
+			target = IPOPT_END;

May I ask, what's the purpose of IPOPT_END? :-)

net/netfilter/nft_exthdr.c static void nft_exthdr_ipv6_eval(const struct nft_expr *expr, · line 151

Quoted 9 lines
net/netfilter/nft_exthdr.c +9 -0
+		} else {
+			/* Point to beyond the options. */
+			*offset = optlen + start;
+			target = opt->__data[optlen];
+		}
+	}
+	if (!found)
+		target = -ENOENT;
+	return target;

nitpick: Probably replace code above.

        return found ? target : -ENOENT;

Apart from the above, this looks good to me.

Thanks!

#2 lore raw
Quoted 7 lines · Pablo Neira Ayuso

net/netfilter/nft_exthdr.c static void nft_exthdr_ipv6_eval(const struct nft_expr *expr, · line 78

Quoted 2 lines
net/netfilter/nft_exthdr.c +2 -0
+{
+	unsigned char optbuf[sizeof(struct ip_options) + 41];

In other parts of the kernel this is + 40:

net/ipv4/cipso_ipv4.c: unsigned char optbuf[sizeof(struct ip_options) + 40];

Quoted 8 lines

here it is + 41.

...

net/netfilter/nft_exthdr.c static void nft_exthdr_ipv6_eval(const struct nft_expr *expr, · line 100

Quoted 2 lines
net/netfilter/nft_exthdr.c +2 -0
+	/* Copy the options since __ip_options_compile() modifies
+	 * the options. Get one byte beyond the option for target < 0

How does this "one byte beyond the option" trick works?

I used ipv6_find_hdr() as a reference. There if target is set to less than 0, then the offset points to the byte beyond the extension header. In this function, it points to the byte beyond the option. I wanted to be as close as a working code as possible. Also, why +41 instead of +40.

Quoted 5 lines

net/netfilter/nft_exthdr.c static void nft_exthdr_ipv6_eval(const struct nft_expr *expr, · line 142

Quoted 3 lines
net/netfilter/nft_exthdr.c +3 -0
+		if (opt->end) {
+			*offset = opt->end + start;
+			target = IPOPT_END;

May I ask, what's the purpose of IPOPT_END? :-)

My understanding is that in ipv6_find_hdr() if the nexthdr is NEXTHDR_NONE, then that's the one being returned. The same here: target is the return value.

Quoted 1 line

Apart from the above, this looks good to me.

AOK for other comments. I can spin another version.

Thank you,

Stephen.

#3 lore raw
Quoted 17 lines · Stephen Suryaputra
Quoted 7 lines · Pablo Neira Ayuso

net/netfilter/nft_exthdr.c static void nft_exthdr_ipv6_eval(const struct nft_expr *expr, · line 78

Quoted 2 lines
net/netfilter/nft_exthdr.c +2 -0
+{
+	unsigned char optbuf[sizeof(struct ip_options) + 41];

In other parts of the kernel this is + 40:

net/ipv4/cipso_ipv4.c: unsigned char optbuf[sizeof(struct ip_options) + 40];

Quoted 8 lines

here it is + 41.

...

net/netfilter/nft_exthdr.c static void nft_exthdr_ipv6_eval(const struct nft_expr *expr, · line 100

Quoted 2 lines
net/netfilter/nft_exthdr.c +2 -0
+	/* Copy the options since __ip_options_compile() modifies
+	 * the options. Get one byte beyond the option for target < 0

How does this "one byte beyond the option" trick works?

Quoted 4 lines

I used ipv6_find_hdr() as a reference. There if target is set to less than 0, then the offset points to the byte beyond the extension header. In this function, it points to the byte beyond the option. I wanted to be as close as a working code as possible. Also, why +41 instead of +40.

OK. But this is never used in this new extension, priv->type is always set. I mean, we already have a pointer to the transport header via nft_pktinfo->xt.thoff.

Quoted 9 lines
Quoted 5 lines

net/netfilter/nft_exthdr.c static void nft_exthdr_ipv6_eval(const struct nft_expr *expr, · line 142

Quoted 3 lines
net/netfilter/nft_exthdr.c +3 -0
+		if (opt->end) {
+			*offset = opt->end + start;
+			target = IPOPT_END;

May I ask, what's the purpose of IPOPT_END? :-)

My understanding is that in ipv6_find_hdr() if the nexthdr is NEXTHDR_NONE, then that's the one being returned. The same here: target is the return value.

Code that falls under the default: case that deals with IPOPT_END is never exercised, right?

priv->type is always set to >= 0, so the opt->end never happens. Hence, we can remove the chunk in net/ipv4/ip_options.c.

Quoted 3 lines
Quoted 1 line

Apart from the above, this looks good to me.

AOK for other comments. I can spin another version.

Please, do.

Thanks for explaining. I understand motivation is to mimic ipv6_find_hdr() which is a good idea indeed... if this functions becomes used away from the netfilter tree at some point that would be a good pattern to extend it. However, so far - please correct me if I'm mistaken - for the requirements of nft_exthdr to support IPv4 options, we don't seem to need it, so I would prefer to start with the bare minimum code that nft_exthdr needs, if possible.

Thanks!

#4 lore raw
In reply to Stephen Suryaputra

On Tue, Jun 11, 2019 at 08:09:12AM -0400, Stephen Suryaputra wrote:
[...]

Quoted 4 lines
net/netfilter/nft_exthdr.c +0 -0
index a940c9fd9045..4155a32fade7 100644

[...]

Quoted 4 lines
net/netfilter/nft_exthdr.c +4 -0
+static int ipv4_find_option(struct net *net, struct sk_buff *skb,
+			    unsigned int *offset, int target,
+			    unsigned short *fragoff, int *flags)
+{

[...]

Quoted 3 lines
net/netfilter/nft_exthdr.c +3 -0
+	switch (target) {
+	case IPOPT_SSRR:
+	case IPOPT_LSRR:

[...]

Quoted 1 line
net/netfilter/nft_exthdr.c +1 -0
+	case IPOPT_RR:

[...]

Quoted 1 line
net/netfilter/nft_exthdr.c +1 -0
+	case IPOPT_RA:

[...]

net/netfilter/nft_exthdr.c static void nft_exthdr_ipv6_eval(const struct nft_expr *expr, · line 139

Quoted 9 lines
net/netfilter/nft_exthdr.c +9 -0
+	default:
+		/* Either not supported or not a specific search, treated as
+		 * found
+		 */
+		found = true;
+		if (target >= 0) {
+			target = -EOPNOTSUPP;
+			break;
+		}

Rules with this options will load fine:

ip option eol type 1
ip option noop type 1
ip option sec type 1
ip option timestamp type 1
ip option rr type 1
ip option sid type 1

However, they will not ever match I think.

found is set to true, but target is set to EOPNOTSUPP, then...

[...]

Quoted 4 lines
net/netfilter/nft_exthdr.c +4 -0
+static void nft_exthdr_ipv4_eval(const struct nft_expr *expr,
+				 struct nft_regs *regs,
+				 const struct nft_pktinfo *pkt)
+{

net/netfilter/nft_exthdr.c static void nft_exthdr_ipv6_eval(const struct nft_expr *expr, · line 167

Quoted 10 lines
net/netfilter/nft_exthdr.c +10 -0
+	struct nft_exthdr *priv = nft_expr_priv(expr);
+	u32 *dest = &regs->data[priv->dreg];
+	struct sk_buff *skb = pkt->skb;
+	unsigned int offset;
+	int err;
+
+	if (skb->protocol != htons(ETH_P_IP))
+		goto err;
+
+	err = ipv4_find_option(nft_net(pkt), skb, &offset, priv->type, NULL, NULL);

... ipv4_find_option() returns -EOPNOTSUPP which says header does not exist.

Quoted 6 lines
net/netfilter/nft_exthdr.c +6 -0
+	if (priv->flags & NFT_EXTHDR_F_PRESENT) {
+		*dest = (err >= 0);
+		return;
+	} else if (err < 0) {
+		goto err;
+	}
#5 lore raw
Quoted 12 lines · Pablo Neira Ayuso

Rules with this options will load fine:

ip option eol type 1
ip option noop type 1
ip option sec type 1
ip option timestamp type 1
ip option rr type 1
ip option sid type 1

However, they will not ever match I think.

Quoted 8 lines

found is set to true, but target is set to EOPNOTSUPP, then...

[...]

net/netfilter/nft_exthdr.c static void nft_exthdr_ipv6_eval(const struct nft_expr *expr, · line 167

Quoted 1 line
net/netfilter/nft_exthdr.c +1 -0
+	err = ipv4_find_option(nft_net(pkt), skb, &offset, priv->type, NULL, NULL);

... ipv4_find_option() returns -EOPNOTSUPP which says header does not exist.

Yes. My goal in writing this is mainly to block loose and/or strict source routing. The system also will need to block RA and RR. Others are not fully supported since we (my employer) don't need it. They can be added later on if desired...

#6 lore raw
Quoted 21 lines · Stephen Suryaputra
Quoted 12 lines · Pablo Neira Ayuso

Rules with this options will load fine:

ip option eol type 1
ip option noop type 1
ip option sec type 1
ip option timestamp type 1
ip option rr type 1
ip option sid type 1

However, they will not ever match I think.

Quoted 8 lines

found is set to true, but target is set to EOPNOTSUPP, then...

[...]

net/netfilter/nft_exthdr.c static void nft_exthdr_ipv6_eval(const struct nft_expr *expr, · line 167

Quoted 1 line
net/netfilter/nft_exthdr.c +1 -0
+	err = ipv4_find_option(nft_net(pkt), skb, &offset, priv->type, NULL, NULL);

... ipv4_find_option() returns -EOPNOTSUPP which says header does not exist.

Quoted 4 lines

Yes. My goal in writing this is mainly to block loose and/or strict source routing. The system also will need to block RA and RR. Others are not fully supported since we (my employer) don't need it. They can be added later on if desired...

OK, that's fine. Then I'd suggest you remove support from eol, noop, sec, timestamp and sid from the userspace patches.

Thanks!