Commit 0434ccdc authored by Florian Westphal's avatar Florian Westphal Committed by Pablo Neira Ayuso

netfilter: nf_tables: rework ct timeout set support

Using a private template is problematic:

1. We can't assign both a zone and a timeout policy
   (zone assigns a conntrack template, so we hit problem 1)
2. Using a template needs to take care of ct refcount, else we'll
   eventually free the private template due to ->use underflow.

This patch reworks template policy to instead work with existing conntrack.

As long as such conntrack has not yet been placed into the hash table
(unconfirmed) we can still add the timeout extension.

The only caveat is that we now need to update/correct ct->timeout to
reflect the initial/new state, otherwise the conntrack entry retains the
default 'new' timeout.

Side effect of this change is that setting the policy must
now occur from chains that are evaluated *after* the conntrack lookup
has taken place.

No released kernel contains the timeout policy feature yet, so this change
should be ok.

Changes since v2:
 - don't handle 'ct is confirmed case'
 - after previous patch, no need to special-case tcp/dccp/sctp timeout
   anymore
Signed-off-by: default avatarFlorian Westphal <fw@strlen.de>
Signed-off-by: default avatarPablo Neira Ayuso <pablo@netfilter.org>
parent ef39078d
...@@ -30,7 +30,7 @@ struct nf_conn_timeout { ...@@ -30,7 +30,7 @@ struct nf_conn_timeout {
}; };
static inline unsigned int * static inline unsigned int *
nf_ct_timeout_data(struct nf_conn_timeout *t) nf_ct_timeout_data(const struct nf_conn_timeout *t)
{ {
struct nf_ct_timeout *timeout; struct nf_ct_timeout *timeout;
......
...@@ -799,7 +799,7 @@ nft_ct_timeout_parse_policy(void *timeouts, ...@@ -799,7 +799,7 @@ nft_ct_timeout_parse_policy(void *timeouts,
} }
struct nft_ct_timeout_obj { struct nft_ct_timeout_obj {
struct nf_conn *tmpl; struct nf_ct_timeout *timeout;
u8 l4proto; u8 l4proto;
}; };
...@@ -809,26 +809,42 @@ static void nft_ct_timeout_obj_eval(struct nft_object *obj, ...@@ -809,26 +809,42 @@ static void nft_ct_timeout_obj_eval(struct nft_object *obj,
{ {
const struct nft_ct_timeout_obj *priv = nft_obj_data(obj); const struct nft_ct_timeout_obj *priv = nft_obj_data(obj);
struct nf_conn *ct = (struct nf_conn *)skb_nfct(pkt->skb); struct nf_conn *ct = (struct nf_conn *)skb_nfct(pkt->skb);
struct sk_buff *skb = pkt->skb; struct nf_conn_timeout *timeout;
const unsigned int *values;
if (priv->l4proto != pkt->tprot)
return;
if (ct || if (!ct || nf_ct_is_template(ct) || nf_ct_is_confirmed(ct))
priv->l4proto != pkt->tprot)
return; return;
nf_ct_set(skb, priv->tmpl, IP_CT_NEW); timeout = nf_ct_timeout_find(ct);
if (!timeout) {
timeout = nf_ct_timeout_ext_add(ct, priv->timeout, GFP_ATOMIC);
if (!timeout) {
regs->verdict.code = NF_DROP;
return;
}
}
rcu_assign_pointer(timeout->timeout, priv->timeout);
/* adjust the timeout as per 'new' state. ct is unconfirmed,
* so the current timestamp must not be added.
*/
values = nf_ct_timeout_data(timeout);
if (values)
nf_ct_refresh(ct, pkt->skb, values[0]);
} }
static int nft_ct_timeout_obj_init(const struct nft_ctx *ctx, static int nft_ct_timeout_obj_init(const struct nft_ctx *ctx,
const struct nlattr * const tb[], const struct nlattr * const tb[],
struct nft_object *obj) struct nft_object *obj)
{ {
const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
struct nft_ct_timeout_obj *priv = nft_obj_data(obj); struct nft_ct_timeout_obj *priv = nft_obj_data(obj);
const struct nf_conntrack_l4proto *l4proto; const struct nf_conntrack_l4proto *l4proto;
struct nf_conn_timeout *timeout_ext;
struct nf_ct_timeout *timeout; struct nf_ct_timeout *timeout;
int l3num = ctx->family; int l3num = ctx->family;
struct nf_conn *tmpl;
__u8 l4num; __u8 l4num;
int ret; int ret;
...@@ -863,28 +879,14 @@ static int nft_ct_timeout_obj_init(const struct nft_ctx *ctx, ...@@ -863,28 +879,14 @@ static int nft_ct_timeout_obj_init(const struct nft_ctx *ctx,
timeout->l3num = l3num; timeout->l3num = l3num;
timeout->l4proto = l4proto; timeout->l4proto = l4proto;
tmpl = nf_ct_tmpl_alloc(ctx->net, zone, GFP_ATOMIC);
if (!tmpl) {
ret = -ENOMEM;
goto err_free_timeout;
}
timeout_ext = nf_ct_timeout_ext_add(tmpl, timeout, GFP_ATOMIC);
if (!timeout_ext) {
ret = -ENOMEM;
goto err_free_tmpl;
}
ret = nf_ct_netns_get(ctx->net, ctx->family); ret = nf_ct_netns_get(ctx->net, ctx->family);
if (ret < 0) if (ret < 0)
goto err_free_tmpl; goto err_free_timeout;
priv->tmpl = tmpl;
priv->timeout = timeout;
return 0; return 0;
err_free_tmpl:
nf_ct_tmpl_free(tmpl);
err_free_timeout: err_free_timeout:
kfree(timeout); kfree(timeout);
err_proto_put: err_proto_put:
...@@ -896,22 +898,19 @@ static void nft_ct_timeout_obj_destroy(const struct nft_ctx *ctx, ...@@ -896,22 +898,19 @@ static void nft_ct_timeout_obj_destroy(const struct nft_ctx *ctx,
struct nft_object *obj) struct nft_object *obj)
{ {
struct nft_ct_timeout_obj *priv = nft_obj_data(obj); struct nft_ct_timeout_obj *priv = nft_obj_data(obj);
struct nf_conn_timeout *t = nf_ct_timeout_find(priv->tmpl); struct nf_ct_timeout *timeout = priv->timeout;
struct nf_ct_timeout *timeout;
timeout = rcu_dereference_raw(t->timeout);
nf_ct_untimeout(ctx->net, timeout); nf_ct_untimeout(ctx->net, timeout);
nf_ct_l4proto_put(timeout->l4proto); nf_ct_l4proto_put(timeout->l4proto);
nf_ct_netns_put(ctx->net, ctx->family); nf_ct_netns_put(ctx->net, ctx->family);
nf_ct_tmpl_free(priv->tmpl); kfree(priv->timeout);
} }
static int nft_ct_timeout_obj_dump(struct sk_buff *skb, static int nft_ct_timeout_obj_dump(struct sk_buff *skb,
struct nft_object *obj, bool reset) struct nft_object *obj, bool reset)
{ {
const struct nft_ct_timeout_obj *priv = nft_obj_data(obj); const struct nft_ct_timeout_obj *priv = nft_obj_data(obj);
const struct nf_conn_timeout *t = nf_ct_timeout_find(priv->tmpl); const struct nf_ct_timeout *timeout = priv->timeout;
const struct nf_ct_timeout *timeout = rcu_dereference_raw(t->timeout);
struct nlattr *nest_params; struct nlattr *nest_params;
int ret; int ret;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment