2 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/netlink.h>
15 #include <linux/netfilter.h>
16 #include <linux/netfilter/nf_tables.h>
17 #include <net/netfilter/nf_tables_core.h>
18 #include <net/netfilter/nf_tables.h>
22 enum nft_registers sreg:8;
24 enum nft_cmp_ops op:8;
27 static void nft_cmp_eval(const struct nft_expr *expr,
28 struct nft_data data[NFT_REG_MAX + 1],
29 const struct nft_pktinfo *pkt)
31 const struct nft_cmp_expr *priv = nft_expr_priv(expr);
34 d = nft_data_cmp(&data[priv->sreg], &priv->data, priv->len);
62 data[NFT_REG_VERDICT].verdict = NFT_BREAK;
65 static const struct nla_policy nft_cmp_policy[NFTA_CMP_MAX + 1] = {
66 [NFTA_CMP_SREG] = { .type = NLA_U32 },
67 [NFTA_CMP_OP] = { .type = NLA_U32 },
68 [NFTA_CMP_DATA] = { .type = NLA_NESTED },
71 static int nft_cmp_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
72 const struct nlattr * const tb[])
74 struct nft_cmp_expr *priv = nft_expr_priv(expr);
75 struct nft_data_desc desc;
78 if (tb[NFTA_CMP_SREG] == NULL ||
79 tb[NFTA_CMP_OP] == NULL ||
80 tb[NFTA_CMP_DATA] == NULL)
83 priv->sreg = ntohl(nla_get_be32(tb[NFTA_CMP_SREG]));
84 err = nft_validate_input_register(priv->sreg);
88 priv->op = ntohl(nla_get_be32(tb[NFTA_CMP_OP]));
101 err = nft_data_init(NULL, &priv->data, &desc, tb[NFTA_CMP_DATA]);
105 priv->len = desc.len;
109 static int nft_cmp_dump(struct sk_buff *skb, const struct nft_expr *expr)
111 const struct nft_cmp_expr *priv = nft_expr_priv(expr);
113 if (nla_put_be32(skb, NFTA_CMP_SREG, htonl(priv->sreg)))
114 goto nla_put_failure;
115 if (nla_put_be32(skb, NFTA_CMP_OP, htonl(priv->op)))
116 goto nla_put_failure;
118 if (nft_data_dump(skb, NFTA_CMP_DATA, &priv->data,
119 NFT_DATA_VALUE, priv->len) < 0)
120 goto nla_put_failure;
127 static struct nft_expr_ops nft_cmp_ops __read_mostly = {
129 .size = NFT_EXPR_SIZE(sizeof(struct nft_cmp_expr)),
130 .owner = THIS_MODULE,
131 .eval = nft_cmp_eval,
132 .init = nft_cmp_init,
133 .dump = nft_cmp_dump,
134 .policy = nft_cmp_policy,
135 .maxattr = NFTA_CMP_MAX,
138 int __init nft_cmp_module_init(void)
140 return nft_register_expr(&nft_cmp_ops);
143 void nft_cmp_module_exit(void)
145 nft_unregister_expr(&nft_cmp_ops);