aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Westphal <fw@strlen.de>2018-08-24 02:43:36 -0400
committerPablo Neira Ayuso <pablo@netfilter.org>2018-08-29 07:04:38 -0400
commit0434ccdcf883e53ec7156a6843943e940dc1feb8 (patch)
tree2095369764120c3f736956a4b190f4b62f09a27c
parentef39078d6342deaddacdd550c4197421bd83fb76 (diff)
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: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
-rw-r--r--include/net/netfilter/nf_conntrack_timeout.h2
-rw-r--r--net/netfilter/nft_ct.c59
2 files changed, 30 insertions, 31 deletions
diff --git a/include/net/netfilter/nf_conntrack_timeout.h b/include/net/netfilter/nf_conntrack_timeout.h
index d5f62cc6c2ae..3394d75e1c80 100644
--- a/include/net/netfilter/nf_conntrack_timeout.h
+++ b/include/net/netfilter/nf_conntrack_timeout.h
@@ -30,7 +30,7 @@ struct nf_conn_timeout {
30}; 30};
31 31
32static inline unsigned int * 32static inline unsigned int *
33nf_ct_timeout_data(struct nf_conn_timeout *t) 33nf_ct_timeout_data(const struct nf_conn_timeout *t)
34{ 34{
35 struct nf_ct_timeout *timeout; 35 struct nf_ct_timeout *timeout;
36 36
diff --git a/net/netfilter/nft_ct.c b/net/netfilter/nft_ct.c
index 26a8baebd072..5dd87748afa8 100644
--- a/net/netfilter/nft_ct.c
+++ b/net/netfilter/nft_ct.c
@@ -799,7 +799,7 @@ err:
799} 799}
800 800
801struct nft_ct_timeout_obj { 801struct nft_ct_timeout_obj {
802 struct nf_conn *tmpl; 802 struct nf_ct_timeout *timeout;
803 u8 l4proto; 803 u8 l4proto;
804}; 804};
805 805
@@ -809,26 +809,42 @@ static void nft_ct_timeout_obj_eval(struct nft_object *obj,
809{ 809{
810 const struct nft_ct_timeout_obj *priv = nft_obj_data(obj); 810 const struct nft_ct_timeout_obj *priv = nft_obj_data(obj);
811 struct nf_conn *ct = (struct nf_conn *)skb_nfct(pkt->skb); 811 struct nf_conn *ct = (struct nf_conn *)skb_nfct(pkt->skb);
812 struct sk_buff *skb = pkt->skb; 812 struct nf_conn_timeout *timeout;
813 const unsigned int *values;
814
815 if (priv->l4proto != pkt->tprot)
816 return;
813 817
814 if (ct || 818 if (!ct || nf_ct_is_template(ct) || nf_ct_is_confirmed(ct))
815 priv->l4proto != pkt->tprot)
816 return; 819 return;
817 820
818 nf_ct_set(skb, priv->tmpl, IP_CT_NEW); 821 timeout = nf_ct_timeout_find(ct);
822 if (!timeout) {
823 timeout = nf_ct_timeout_ext_add(ct, priv->timeout, GFP_ATOMIC);
824 if (!timeout) {
825 regs->verdict.code = NF_DROP;
826 return;
827 }
828 }
829
830 rcu_assign_pointer(timeout->timeout, priv->timeout);
831
832 /* adjust the timeout as per 'new' state. ct is unconfirmed,
833 * so the current timestamp must not be added.
834 */
835 values = nf_ct_timeout_data(timeout);
836 if (values)
837 nf_ct_refresh(ct, pkt->skb, values[0]);
819} 838}
820 839
821static int nft_ct_timeout_obj_init(const struct nft_ctx *ctx, 840static int nft_ct_timeout_obj_init(const struct nft_ctx *ctx,
822 const struct nlattr * const tb[], 841 const struct nlattr * const tb[],
823 struct nft_object *obj) 842 struct nft_object *obj)
824{ 843{
825 const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
826 struct nft_ct_timeout_obj *priv = nft_obj_data(obj); 844 struct nft_ct_timeout_obj *priv = nft_obj_data(obj);
827 const struct nf_conntrack_l4proto *l4proto; 845 const struct nf_conntrack_l4proto *l4proto;
828 struct nf_conn_timeout *timeout_ext;
829 struct nf_ct_timeout *timeout; 846 struct nf_ct_timeout *timeout;
830 int l3num = ctx->family; 847 int l3num = ctx->family;
831 struct nf_conn *tmpl;
832 __u8 l4num; 848 __u8 l4num;
833 int ret; 849 int ret;
834 850
@@ -863,28 +879,14 @@ static int nft_ct_timeout_obj_init(const struct nft_ctx *ctx,
863 879
864 timeout->l3num = l3num; 880 timeout->l3num = l3num;
865 timeout->l4proto = l4proto; 881 timeout->l4proto = l4proto;
866 tmpl = nf_ct_tmpl_alloc(ctx->net, zone, GFP_ATOMIC);
867 if (!tmpl) {
868 ret = -ENOMEM;
869 goto err_free_timeout;
870 }
871
872 timeout_ext = nf_ct_timeout_ext_add(tmpl, timeout, GFP_ATOMIC);
873 if (!timeout_ext) {
874 ret = -ENOMEM;
875 goto err_free_tmpl;
876 }
877 882
878 ret = nf_ct_netns_get(ctx->net, ctx->family); 883 ret = nf_ct_netns_get(ctx->net, ctx->family);
879 if (ret < 0) 884 if (ret < 0)
880 goto err_free_tmpl; 885 goto err_free_timeout;
881
882 priv->tmpl = tmpl;
883 886
887 priv->timeout = timeout;
884 return 0; 888 return 0;
885 889
886err_free_tmpl:
887 nf_ct_tmpl_free(tmpl);
888err_free_timeout: 890err_free_timeout:
889 kfree(timeout); 891 kfree(timeout);
890err_proto_put: 892err_proto_put:
@@ -896,22 +898,19 @@ static void nft_ct_timeout_obj_destroy(const struct nft_ctx *ctx,
896 struct nft_object *obj) 898 struct nft_object *obj)
897{ 899{
898 struct nft_ct_timeout_obj *priv = nft_obj_data(obj); 900 struct nft_ct_timeout_obj *priv = nft_obj_data(obj);
899 struct nf_conn_timeout *t = nf_ct_timeout_find(priv->tmpl); 901 struct nf_ct_timeout *timeout = priv->timeout;
900 struct nf_ct_timeout *timeout;
901 902
902 timeout = rcu_dereference_raw(t->timeout);
903 nf_ct_untimeout(ctx->net, timeout); 903 nf_ct_untimeout(ctx->net, timeout);
904 nf_ct_l4proto_put(timeout->l4proto); 904 nf_ct_l4proto_put(timeout->l4proto);
905 nf_ct_netns_put(ctx->net, ctx->family); 905 nf_ct_netns_put(ctx->net, ctx->family);
906 nf_ct_tmpl_free(priv->tmpl); 906 kfree(priv->timeout);
907} 907}
908 908
909static int nft_ct_timeout_obj_dump(struct sk_buff *skb, 909static int nft_ct_timeout_obj_dump(struct sk_buff *skb,
910 struct nft_object *obj, bool reset) 910 struct nft_object *obj, bool reset)
911{ 911{
912 const struct nft_ct_timeout_obj *priv = nft_obj_data(obj); 912 const struct nft_ct_timeout_obj *priv = nft_obj_data(obj);
913 const struct nf_conn_timeout *t = nf_ct_timeout_find(priv->tmpl); 913 const struct nf_ct_timeout *timeout = priv->timeout;
914 const struct nf_ct_timeout *timeout = rcu_dereference_raw(t->timeout);
915 struct nlattr *nest_params; 914 struct nlattr *nest_params;
916 int ret; 915 int ret;
917 916