History

netfilter: nft_dynset: allow to invert match criteria

The dynset expression matches if we can fit a new entry into the set. If there is no room for it, then it breaks the rule evaluation. This patch introduces the inversion flag so you can add rules to explicitly drop packets that don't fit into the set. For example: # nft filter input flow table xyz size 4 { ip saddr timeout 120s counter } overflow drop This is useful to provide a replacement for connlimit. For the rule above, every new entry uses the IPv4 address as key in the set, this entry gets a timeout of 120 seconds that gets refresh on every packet seen. If we get new flow and our set already contains 4 entries already, then this packet is dropped. You can already express this in positive logic, assuming default policy to drop: # nft filter input flow table xyz size 4 { ip saddr timeout 10s counter } accept

Pablo Neira Ayuso committed 10 years ago

Commit dbd2be01 parent 70ca7672 files · +25 −1Browse files

Unified · Split

2 files
  1. include/uapi/linux/netfilter/nf_tables.h +6 −0
  2. net/netfilter/nft_dynset.c +19 −1
include/uapi/linux/netfilter/nf_tables.h+6−0
@@ -575,6 +575,10 @@ enum nft_dynset_ops { · 574 unmodified lines
575 NFT_DYNSET_OP_UPDATE,575 NFT_DYNSET_OP_UPDATE,
576};576};
577 577
578enum nft_dynset_flags {
579 NFT_DYNSET_F_INV = (1 << 0),
580};
581
578/**582/**
579 * enum nft_dynset_attributes - dynset expression attributes583 * enum nft_dynset_attributes - dynset expression attributes
580 *584 *
@@ -585,6 +589,7 @@ enum nft_dynset_ops { · 4 unmodified lines
585 * @NFTA_DYNSET_SREG_DATA: source register of the data (NLA_U32)589 * @NFTA_DYNSET_SREG_DATA: source register of the data (NLA_U32)
586 * @NFTA_DYNSET_TIMEOUT: timeout value for the new element (NLA_U64)590 * @NFTA_DYNSET_TIMEOUT: timeout value for the new element (NLA_U64)
587 * @NFTA_DYNSET_EXPR: expression (NLA_NESTED: nft_expr_attributes)591 * @NFTA_DYNSET_EXPR: expression (NLA_NESTED: nft_expr_attributes)
592 * @NFTA_DYNSET_FLAGS: flags (NLA_U32)
588 */593 */
589enum nft_dynset_attributes {594enum nft_dynset_attributes {
590 NFTA_DYNSET_UNSPEC,595 NFTA_DYNSET_UNSPEC,
@@ -596,6 +601,7 @@ enum nft_dynset_attributes { · 5 unmodified lines
596 NFTA_DYNSET_TIMEOUT,601 NFTA_DYNSET_TIMEOUT,
597 NFTA_DYNSET_EXPR,602 NFTA_DYNSET_EXPR,
598 NFTA_DYNSET_PAD,603 NFTA_DYNSET_PAD,
604 NFTA_DYNSET_FLAGS,
599 __NFTA_DYNSET_MAX,605 __NFTA_DYNSET_MAX,
600};606};
601#define NFTA_DYNSET_MAX (__NFTA_DYNSET_MAX - 1)607#define NFTA_DYNSET_MAX (__NFTA_DYNSET_MAX - 1)
net/netfilter/nft_dynset.c+19−1
@@ -22,6 +22,7 @@ struct nft_dynset { · 21 unmodified lines
22 enum nft_dynset_ops op:8;22 enum nft_dynset_ops op:8;
23 enum nft_registers sreg_key:8;23 enum nft_registers sreg_key:8;
24 enum nft_registers sreg_data:8;24 enum nft_registers sreg_data:8;
25 bool invert;
25 u64 timeout;26 u64 timeout;
26 struct nft_expr *expr;27 struct nft_expr *expr;
27 struct nft_set_binding binding;28 struct nft_set_binding binding;
@@ -82,10 +83,14 @@ static void nft_dynset_eval(const struct nft_expr *expr, · 54 unmodified lines
82 83
83 if (sexpr != NULL)84 if (sexpr != NULL)
84 sexpr->ops->eval(sexpr, regs, pkt);85 sexpr->ops->eval(sexpr, regs, pkt);
86
87 if (priv->invert)
88 regs->verdict.code = NFT_BREAK;
85 return;89 return;
86 }90 }
87out:91out:
88 regs->verdict.code = NFT_BREAK;92 if (!priv->invert)
93 regs->verdict.code = NFT_BREAK;
89}94}
90 95
91static const struct nla_policy nft_dynset_policy[NFTA_DYNSET_MAX + 1] = {96static const struct nla_policy nft_dynset_policy[NFTA_DYNSET_MAX + 1] = {
@@ -96,6 +101,7 @@ static const struct nla_policy nft_dynset_policy[NFTA_DYNSET_MAX + 1] = { · 4 unmodified lines
96 [NFTA_DYNSET_SREG_DATA] = { .type = NLA_U32 },101 [NFTA_DYNSET_SREG_DATA] = { .type = NLA_U32 },
97 [NFTA_DYNSET_TIMEOUT] = { .type = NLA_U64 },102 [NFTA_DYNSET_TIMEOUT] = { .type = NLA_U64 },
98 [NFTA_DYNSET_EXPR] = { .type = NLA_NESTED },103 [NFTA_DYNSET_EXPR] = { .type = NLA_NESTED },
104 [NFTA_DYNSET_FLAGS] = { .type = NLA_U32 },
99};105};
100 106
101static int nft_dynset_init(const struct nft_ctx *ctx,107static int nft_dynset_init(const struct nft_ctx *ctx,
@@ -113,6 +119,15 @@ static int nft_dynset_init(const struct nft_ctx *ctx, · 11 unmodified lines
113 tb[NFTA_DYNSET_SREG_KEY] == NULL)119 tb[NFTA_DYNSET_SREG_KEY] == NULL)
114 return -EINVAL;120 return -EINVAL;
115 121
122 if (tb[NFTA_DYNSET_FLAGS]) {
123 u32 flags = ntohl(nla_get_be32(tb[NFTA_DYNSET_FLAGS]));
124
125 if (flags & ~NFT_DYNSET_F_INV)
126 return -EINVAL;
127 if (flags & NFT_DYNSET_F_INV)
128 priv->invert = true;
129 }
130
116 set = nf_tables_set_lookup(ctx->table, tb[NFTA_DYNSET_SET_NAME],131 set = nf_tables_set_lookup(ctx->table, tb[NFTA_DYNSET_SET_NAME],
117 genmask);132 genmask);
118 if (IS_ERR(set)) {133 if (IS_ERR(set)) {
@@ -220,6 +235,7 @@ static void nft_dynset_destroy(const struct nft_ctx *ctx, · 101 unmodified lines
220static int nft_dynset_dump(struct sk_buff *skb, const struct nft_expr *expr)235static int nft_dynset_dump(struct sk_buff *skb, const struct nft_expr *expr)
221{236{
222 const struct nft_dynset *priv = nft_expr_priv(expr);237 const struct nft_dynset *priv = nft_expr_priv(expr);
238 u32 flags = priv->invert ? NFT_DYNSET_F_INV : 0;
223 239
224 if (nft_dump_register(skb, NFTA_DYNSET_SREG_KEY, priv->sreg_key))240 if (nft_dump_register(skb, NFTA_DYNSET_SREG_KEY, priv->sreg_key))
225 goto nla_put_failure;241 goto nla_put_failure;
@@ -235,6 +251,8 @@ static int nft_dynset_dump(struct sk_buff *skb, const struct nft_expr *expr) · 9 unmodified lines
235 goto nla_put_failure;251 goto nla_put_failure;
236 if (priv->expr && nft_expr_dump(skb, NFTA_DYNSET_EXPR, priv->expr))252 if (priv->expr && nft_expr_dump(skb, NFTA_DYNSET_EXPR, priv->expr))
237 goto nla_put_failure;253 goto nla_put_failure;
254 if (nla_put_be32(skb, NFTA_DYNSET_FLAGS, htonl(flags)))
255 goto nla_put_failure;
238 return 0;256 return 0;
239 257
240nla_put_failure:258nla_put_failure:

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t