Skip to content

Commit 133dc20

Browse files
Phil Sutterummakynes
authored andcommitted
netfilter: nft_exthdr: Support SCTP chunks
Chunks are SCTP header extensions similar in implementation to IPv6 extension headers or TCP options. Reusing exthdr expression to find and extract field values from them is therefore pretty straightforward. For now, this supports extracting data from chunks at a fixed offset (and length) only - chunks themselves are an extensible data structure; in order to make all fields available, a nested extension search is needed. Signed-off-by: Phil Sutter <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent af9207a commit 133dc20

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

include/uapi/linux/netfilter/nf_tables.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,11 +813,13 @@ enum nft_exthdr_flags {
813813
* @NFT_EXTHDR_OP_IPV6: match against ipv6 extension headers
814814
* @NFT_EXTHDR_OP_TCP: match against tcp options
815815
* @NFT_EXTHDR_OP_IPV4: match against ipv4 options
816+
* @NFT_EXTHDR_OP_SCTP: match against sctp chunks
816817
*/
817818
enum nft_exthdr_op {
818819
NFT_EXTHDR_OP_IPV6,
819820
NFT_EXTHDR_OP_TCPOPT,
820821
NFT_EXTHDR_OP_IPV4,
822+
NFT_EXTHDR_OP_SCTP,
821823
__NFT_EXTHDR_OP_MAX
822824
};
823825
#define NFT_EXTHDR_OP_MAX (__NFT_EXTHDR_OP_MAX - 1)

net/netfilter/nft_exthdr.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
#include <linux/netlink.h>
1111
#include <linux/netfilter.h>
1212
#include <linux/netfilter/nf_tables.h>
13+
#include <linux/sctp.h>
1314
#include <net/netfilter/nf_tables_core.h>
1415
#include <net/netfilter/nf_tables.h>
16+
#include <net/sctp/sctp.h>
1517
#include <net/tcp.h>
1618

1719
struct nft_exthdr {
@@ -300,6 +302,43 @@ static void nft_exthdr_tcp_set_eval(const struct nft_expr *expr,
300302
}
301303
}
302304

305+
static void nft_exthdr_sctp_eval(const struct nft_expr *expr,
306+
struct nft_regs *regs,
307+
const struct nft_pktinfo *pkt)
308+
{
309+
unsigned int offset = pkt->xt.thoff + sizeof(struct sctphdr);
310+
struct nft_exthdr *priv = nft_expr_priv(expr);
311+
u32 *dest = &regs->data[priv->dreg];
312+
const struct sctp_chunkhdr *sch;
313+
struct sctp_chunkhdr _sch;
314+
315+
do {
316+
sch = skb_header_pointer(pkt->skb, offset, sizeof(_sch), &_sch);
317+
if (!sch || !sch->length)
318+
break;
319+
320+
if (sch->type == priv->type) {
321+
if (priv->flags & NFT_EXTHDR_F_PRESENT) {
322+
nft_reg_store8(dest, true);
323+
return;
324+
}
325+
if (priv->offset + priv->len > ntohs(sch->length) ||
326+
offset + ntohs(sch->length) > pkt->skb->len)
327+
break;
328+
329+
dest[priv->len / NFT_REG32_SIZE] = 0;
330+
memcpy(dest, (char *)sch + priv->offset, priv->len);
331+
return;
332+
}
333+
offset += SCTP_PAD4(ntohs(sch->length));
334+
} while (offset < pkt->skb->len);
335+
336+
if (priv->flags & NFT_EXTHDR_F_PRESENT)
337+
nft_reg_store8(dest, false);
338+
else
339+
regs->verdict.code = NFT_BREAK;
340+
}
341+
303342
static const struct nla_policy nft_exthdr_policy[NFTA_EXTHDR_MAX + 1] = {
304343
[NFTA_EXTHDR_DREG] = { .type = NLA_U32 },
305344
[NFTA_EXTHDR_TYPE] = { .type = NLA_U8 },
@@ -499,6 +538,14 @@ static const struct nft_expr_ops nft_exthdr_tcp_set_ops = {
499538
.dump = nft_exthdr_dump_set,
500539
};
501540

541+
static const struct nft_expr_ops nft_exthdr_sctp_ops = {
542+
.type = &nft_exthdr_type,
543+
.size = NFT_EXPR_SIZE(sizeof(struct nft_exthdr)),
544+
.eval = nft_exthdr_sctp_eval,
545+
.init = nft_exthdr_init,
546+
.dump = nft_exthdr_dump,
547+
};
548+
502549
static const struct nft_expr_ops *
503550
nft_exthdr_select_ops(const struct nft_ctx *ctx,
504551
const struct nlattr * const tb[])
@@ -529,6 +576,10 @@ nft_exthdr_select_ops(const struct nft_ctx *ctx,
529576
return &nft_exthdr_ipv4_ops;
530577
}
531578
break;
579+
case NFT_EXTHDR_OP_SCTP:
580+
if (tb[NFTA_EXTHDR_DREG])
581+
return &nft_exthdr_sctp_ops;
582+
break;
532583
}
533584

534585
return ERR_PTR(-EOPNOTSUPP);

0 commit comments

Comments
 (0)