Skip to content

Commit 002f217

Browse files
pvxeummakynes
authored andcommitted
netfilter: nf_tables: add userdata attributes to nft_chain
Enables storing userdata for nft_chain. Field udata points to user data and udlen stores its length. Adds new attribute flag NFTA_CHAIN_USERDATA. Signed-off-by: Jose M. Guisado Gomez <guigom@riseup.net> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 85db827 commit 002f217

3 files changed

Lines changed: 29 additions & 8 deletions

File tree

‎include/net/netfilter/nf_tables.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,8 @@ struct nft_chain {
945945
bound:1,
946946
genmask:2;
947947
char *name;
948+
u16 udlen;
949+
u8 *udata;
948950

949951
/* Only used during control plane commit phase: */
950952
struct nft_rule **rules_next;

‎include/uapi/linux/netfilter/nf_tables.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ enum nft_chain_flags {
208208
* @NFTA_CHAIN_COUNTERS: counter specification of the chain (NLA_NESTED: nft_counter_attributes)
209209
* @NFTA_CHAIN_FLAGS: chain flags
210210
* @NFTA_CHAIN_ID: uniquely identifies a chain in a transaction (NLA_U32)
211+
* @NFTA_CHAIN_USERDATA: user data (NLA_BINARY)
211212
*/
212213
enum nft_chain_attributes {
213214
NFTA_CHAIN_UNSPEC,
@@ -222,6 +223,7 @@ enum nft_chain_attributes {
222223
NFTA_CHAIN_PAD,
223224
NFTA_CHAIN_FLAGS,
224225
NFTA_CHAIN_ID,
226+
NFTA_CHAIN_USERDATA,
225227
__NFTA_CHAIN_MAX
226228
};
227229
#define NFTA_CHAIN_MAX (__NFTA_CHAIN_MAX - 1)

‎net/netfilter/nf_tables_api.c‎

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,8 @@ static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
13041304
[NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
13051305
[NFTA_CHAIN_FLAGS] = { .type = NLA_U32 },
13061306
[NFTA_CHAIN_ID] = { .type = NLA_U32 },
1307+
[NFTA_CHAIN_USERDATA] = { .type = NLA_BINARY,
1308+
.len = NFT_USERDATA_MAXLEN },
13071309
};
13081310

13091311
static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
@@ -1445,6 +1447,10 @@ static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net,
14451447
if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
14461448
goto nla_put_failure;
14471449

1450+
if (chain->udata &&
1451+
nla_put(skb, NFTA_CHAIN_USERDATA, chain->udlen, chain->udata))
1452+
goto nla_put_failure;
1453+
14481454
nlmsg_end(skb, nlh);
14491455
return 0;
14501456

@@ -1682,9 +1688,11 @@ void nf_tables_chain_destroy(struct nft_ctx *ctx)
16821688
free_percpu(rcu_dereference_raw(basechain->stats));
16831689
}
16841690
kfree(chain->name);
1691+
kfree(chain->udata);
16851692
kfree(basechain);
16861693
} else {
16871694
kfree(chain->name);
1695+
kfree(chain->udata);
16881696
kfree(chain);
16891697
}
16901698
}
@@ -2038,7 +2046,7 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask,
20382046
} else {
20392047
if (!(flags & NFT_CHAIN_BINDING)) {
20402048
err = -EINVAL;
2041-
goto err1;
2049+
goto err_destroy_chain;
20422050
}
20432051

20442052
snprintf(name, sizeof(name), "__chain%llu", ++chain_id);
@@ -2047,13 +2055,22 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask,
20472055

20482056
if (!chain->name) {
20492057
err = -ENOMEM;
2050-
goto err1;
2058+
goto err_destroy_chain;
2059+
}
2060+
2061+
if (nla[NFTA_CHAIN_USERDATA]) {
2062+
chain->udata = nla_memdup(nla[NFTA_CHAIN_USERDATA], GFP_KERNEL);
2063+
if (chain->udata == NULL) {
2064+
err = -ENOMEM;
2065+
goto err_destroy_chain;
2066+
}
2067+
chain->udlen = nla_len(nla[NFTA_CHAIN_USERDATA]);
20512068
}
20522069

20532070
rules = nf_tables_chain_alloc_rules(chain, 0);
20542071
if (!rules) {
20552072
err = -ENOMEM;
2056-
goto err1;
2073+
goto err_destroy_chain;
20572074
}
20582075

20592076
*rules = NULL;
@@ -2062,12 +2079,12 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask,
20622079

20632080
err = nf_tables_register_hook(net, table, chain);
20642081
if (err < 0)
2065-
goto err1;
2082+
goto err_destroy_chain;
20662083

20672084
trans = nft_trans_chain_add(ctx, NFT_MSG_NEWCHAIN);
20682085
if (IS_ERR(trans)) {
20692086
err = PTR_ERR(trans);
2070-
goto err2;
2087+
goto err_unregister_hook;
20712088
}
20722089

20732090
nft_trans_chain_policy(trans) = NFT_CHAIN_POLICY_UNSET;
@@ -2077,15 +2094,15 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask,
20772094
err = nft_chain_add(table, chain);
20782095
if (err < 0) {
20792096
nft_trans_destroy(trans);
2080-
goto err2;
2097+
goto err_unregister_hook;
20812098
}
20822099

20832100
table->use++;
20842101

20852102
return 0;
2086-
err2:
2103+
err_unregister_hook:
20872104
nf_tables_unregister_hook(net, table, chain);
2088-
err1:
2105+
err_destroy_chain:
20892106
nf_tables_chain_destroy(ctx);
20902107

20912108
return err;

0 commit comments

Comments
 (0)