netfilter: nft_ct: Add support to set the connmark
[cascardo/linux.git] / net / netfilter / nft_ct.c
index 955f4e6..c7c1285 100644 (file)
 #include <net/netfilter/nf_conntrack.h>
 #include <net/netfilter/nf_conntrack_tuple.h>
 #include <net/netfilter/nf_conntrack_helper.h>
+#include <net/netfilter/nf_conntrack_ecache.h>
 
 struct nft_ct {
        enum nft_ct_keys        key:8;
        enum ip_conntrack_dir   dir:8;
-       enum nft_registers      dreg:8;
+       union{
+               enum nft_registers      dreg:8;
+               enum nft_registers      sreg:8;
+       };
        uint8_t                 family;
 };
 
-static void nft_ct_eval(const struct nft_expr *expr,
-                       struct nft_data data[NFT_REG_MAX + 1],
-                       const struct nft_pktinfo *pkt)
+static void nft_ct_get_eval(const struct nft_expr *expr,
+                           struct nft_data data[NFT_REG_MAX + 1],
+                           const struct nft_pktinfo *pkt)
 {
        const struct nft_ct *priv = nft_expr_priv(expr);
        struct nft_data *dest = &data[priv->dreg];
@@ -123,24 +127,77 @@ err:
        data[NFT_REG_VERDICT].verdict = NFT_BREAK;
 }
 
+static void nft_ct_set_eval(const struct nft_expr *expr,
+                           struct nft_data data[NFT_REG_MAX + 1],
+                           const struct nft_pktinfo *pkt)
+{
+       const struct nft_ct *priv = nft_expr_priv(expr);
+       struct sk_buff *skb = pkt->skb;
+       u32 value = data[priv->sreg].data[0];
+       enum ip_conntrack_info ctinfo;
+       struct nf_conn *ct;
+
+       ct = nf_ct_get(skb, &ctinfo);
+       if (ct == NULL)
+               return;
+
+       switch (priv->key) {
+#ifdef CONFIG_NF_CONNTRACK_MARK
+       case NFT_CT_MARK:
+               if (ct->mark != value) {
+                       ct->mark = value;
+                       nf_conntrack_event_cache(IPCT_MARK, ct);
+               }
+               break;
+#endif
+       }
+}
+
 static const struct nla_policy nft_ct_policy[NFTA_CT_MAX + 1] = {
        [NFTA_CT_DREG]          = { .type = NLA_U32 },
        [NFTA_CT_KEY]           = { .type = NLA_U32 },
        [NFTA_CT_DIRECTION]     = { .type = NLA_U8 },
+       [NFTA_CT_SREG]          = { .type = NLA_U32 },
 };
 
-static int nft_ct_init(const struct nft_ctx *ctx,
-                      const struct nft_expr *expr,
-                      const struct nlattr * const tb[])
+static int nft_ct_l3proto_try_module_get(uint8_t family)
 {
-       struct nft_ct *priv = nft_expr_priv(expr);
        int err;
 
-       if (tb[NFTA_CT_DREG] == NULL ||
-           tb[NFTA_CT_KEY] == NULL)
-               return -EINVAL;
+       if (family == NFPROTO_INET) {
+               err = nf_ct_l3proto_try_module_get(NFPROTO_IPV4);
+               if (err < 0)
+                       goto err1;
+               err = nf_ct_l3proto_try_module_get(NFPROTO_IPV6);
+               if (err < 0)
+                       goto err2;
+       } else {
+               err = nf_ct_l3proto_try_module_get(family);
+               if (err < 0)
+                       goto err1;
+       }
+       return 0;
+
+err2:
+       nf_ct_l3proto_module_put(NFPROTO_IPV4);
+err1:
+       return err;
+}
+
+static void nft_ct_l3proto_module_put(uint8_t family)
+{
+       if (family == NFPROTO_INET) {
+               nf_ct_l3proto_module_put(NFPROTO_IPV4);
+               nf_ct_l3proto_module_put(NFPROTO_IPV6);
+       } else
+               nf_ct_l3proto_module_put(family);
+}
+
+static int nft_ct_init_validate_get(const struct nft_expr *expr,
+                                   const struct nlattr * const tb[])
+{
+       struct nft_ct *priv = nft_expr_priv(expr);
 
-       priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
        if (tb[NFTA_CT_DIRECTION] != NULL) {
                priv->dir = nla_get_u8(tb[NFTA_CT_DIRECTION]);
                switch (priv->dir) {
@@ -179,34 +236,72 @@ static int nft_ct_init(const struct nft_ctx *ctx,
                return -EOPNOTSUPP;
        }
 
-       err = nf_ct_l3proto_try_module_get(ctx->afi->family);
+       return 0;
+}
+
+static int nft_ct_init_validate_set(uint32_t key)
+{
+       switch (key) {
+       case NFT_CT_MARK:
+               break;
+       default:
+               return -EOPNOTSUPP;
+       }
+
+       return 0;
+}
+
+static int nft_ct_init(const struct nft_ctx *ctx,
+                      const struct nft_expr *expr,
+                      const struct nlattr * const tb[])
+{
+       struct nft_ct *priv = nft_expr_priv(expr);
+       int err;
+
+       priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
+
+       if (tb[NFTA_CT_DREG]) {
+               err = nft_ct_init_validate_get(expr, tb);
+               if (err < 0)
+                       return err;
+
+               priv->dreg = ntohl(nla_get_be32(tb[NFTA_CT_DREG]));
+               err = nft_validate_output_register(priv->dreg);
+               if (err < 0)
+                       return err;
+
+               err = nft_validate_data_load(ctx, priv->dreg, NULL,
+                                            NFT_DATA_VALUE);
+               if (err < 0)
+                       return err;
+       } else {
+               err = nft_ct_init_validate_set(priv->key);
+               if (err < 0)
+                       return err;
+
+               priv->sreg = ntohl(nla_get_be32(tb[NFTA_CT_SREG]));
+               err = nft_validate_input_register(priv->sreg);
+               if (err < 0)
+                       return err;
+       }
+
+       err = nft_ct_l3proto_try_module_get(ctx->afi->family);
        if (err < 0)
                return err;
-       priv->family = ctx->afi->family;
 
-       priv->dreg = ntohl(nla_get_be32(tb[NFTA_CT_DREG]));
-       err = nft_validate_output_register(priv->dreg);
-       if (err < 0)
-               goto err1;
+       priv->family = ctx->afi->family;
 
-       err = nft_validate_data_load(ctx, priv->dreg, NULL, NFT_DATA_VALUE);
-       if (err < 0)
-               goto err1;
        return 0;
-
-err1:
-       nf_ct_l3proto_module_put(ctx->afi->family);
-       return err;
 }
 
 static void nft_ct_destroy(const struct nft_expr *expr)
 {
        struct nft_ct *priv = nft_expr_priv(expr);
 
-       nf_ct_l3proto_module_put(priv->family);
+       nft_ct_l3proto_module_put(priv->family);
 }
 
-static int nft_ct_dump(struct sk_buff *skb, const struct nft_expr *expr)
+static int nft_ct_get_dump(struct sk_buff *skb, const struct nft_expr *expr)
 {
        const struct nft_ct *priv = nft_expr_priv(expr);
 
@@ -222,19 +317,61 @@ nla_put_failure:
        return -1;
 }
 
+static int nft_ct_set_dump(struct sk_buff *skb, const struct nft_expr *expr)
+{
+       const struct nft_ct *priv = nft_expr_priv(expr);
+
+       if (nla_put_be32(skb, NFTA_CT_SREG, htonl(priv->sreg)))
+               goto nla_put_failure;
+       if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key)))
+               goto nla_put_failure;
+       return 0;
+
+nla_put_failure:
+       return -1;
+}
+
 static struct nft_expr_type nft_ct_type;
-static const struct nft_expr_ops nft_ct_ops = {
+static const struct nft_expr_ops nft_ct_get_ops = {
        .type           = &nft_ct_type,
        .size           = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
-       .eval           = nft_ct_eval,
+       .eval           = nft_ct_get_eval,
        .init           = nft_ct_init,
        .destroy        = nft_ct_destroy,
-       .dump           = nft_ct_dump,
+       .dump           = nft_ct_get_dump,
 };
 
+static const struct nft_expr_ops nft_ct_set_ops = {
+       .type           = &nft_ct_type,
+       .size           = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
+       .eval           = nft_ct_set_eval,
+       .init           = nft_ct_init,
+       .destroy        = nft_ct_destroy,
+       .dump           = nft_ct_set_dump,
+};
+
+static const struct nft_expr_ops *
+nft_ct_select_ops(const struct nft_ctx *ctx,
+                   const struct nlattr * const tb[])
+{
+       if (tb[NFTA_CT_KEY] == NULL)
+               return ERR_PTR(-EINVAL);
+
+       if (tb[NFTA_CT_DREG] && tb[NFTA_CT_SREG])
+               return ERR_PTR(-EINVAL);
+
+       if (tb[NFTA_CT_DREG])
+               return &nft_ct_get_ops;
+
+       if (tb[NFTA_CT_SREG])
+               return &nft_ct_set_ops;
+
+       return ERR_PTR(-EINVAL);
+}
+
 static struct nft_expr_type nft_ct_type __read_mostly = {
        .name           = "ct",
-       .ops            = &nft_ct_ops,
+       .select_ops     = &nft_ct_select_ops,
        .policy         = nft_ct_policy,
        .maxattr        = NFTA_CT_MAX,
        .owner          = THIS_MODULE,