Commit | Line | Data |
---|---|---|
96518518 PM |
1 | /* |
2 | * Copyright (c) 2008 Patrick McHardy <kaber@trash.net> | |
3 | * | |
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. | |
7 | * | |
8 | * Development of this code funded by Astaro AG (http://www.astaro.com/) | |
9 | */ | |
10 | ||
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> | |
19 | ||
20 | struct nft_cmp_expr { | |
21 | struct nft_data data; | |
22 | enum nft_registers sreg:8; | |
23 | u8 len; | |
24 | enum nft_cmp_ops op:8; | |
25 | }; | |
26 | ||
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) | |
30 | { | |
31 | const struct nft_cmp_expr *priv = nft_expr_priv(expr); | |
32 | int d; | |
33 | ||
34 | d = nft_data_cmp(&data[priv->sreg], &priv->data, priv->len); | |
35 | switch (priv->op) { | |
36 | case NFT_CMP_EQ: | |
37 | if (d != 0) | |
38 | goto mismatch; | |
39 | break; | |
40 | case NFT_CMP_NEQ: | |
41 | if (d == 0) | |
42 | goto mismatch; | |
43 | break; | |
44 | case NFT_CMP_LT: | |
45 | if (d == 0) | |
46 | goto mismatch; | |
47 | case NFT_CMP_LTE: | |
48 | if (d > 0) | |
49 | goto mismatch; | |
50 | break; | |
51 | case NFT_CMP_GT: | |
52 | if (d == 0) | |
53 | goto mismatch; | |
54 | case NFT_CMP_GTE: | |
55 | if (d < 0) | |
56 | goto mismatch; | |
57 | break; | |
58 | } | |
59 | return; | |
60 | ||
61 | mismatch: | |
62 | data[NFT_REG_VERDICT].verdict = NFT_BREAK; | |
63 | } | |
64 | ||
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 }, | |
69 | }; | |
70 | ||
71 | static int nft_cmp_init(const struct nft_ctx *ctx, const struct nft_expr *expr, | |
72 | const struct nlattr * const tb[]) | |
73 | { | |
74 | struct nft_cmp_expr *priv = nft_expr_priv(expr); | |
75 | struct nft_data_desc desc; | |
76 | int err; | |
77 | ||
78 | if (tb[NFTA_CMP_SREG] == NULL || | |
79 | tb[NFTA_CMP_OP] == NULL || | |
80 | tb[NFTA_CMP_DATA] == NULL) | |
81 | return -EINVAL; | |
82 | ||
83 | priv->sreg = ntohl(nla_get_be32(tb[NFTA_CMP_SREG])); | |
84 | err = nft_validate_input_register(priv->sreg); | |
85 | if (err < 0) | |
86 | return err; | |
87 | ||
88 | priv->op = ntohl(nla_get_be32(tb[NFTA_CMP_OP])); | |
89 | switch (priv->op) { | |
90 | case NFT_CMP_EQ: | |
91 | case NFT_CMP_NEQ: | |
92 | case NFT_CMP_LT: | |
93 | case NFT_CMP_LTE: | |
94 | case NFT_CMP_GT: | |
95 | case NFT_CMP_GTE: | |
96 | break; | |
97 | default: | |
98 | return -EINVAL; | |
99 | } | |
100 | ||
101 | err = nft_data_init(NULL, &priv->data, &desc, tb[NFTA_CMP_DATA]); | |
102 | if (err < 0) | |
103 | return err; | |
104 | ||
105 | priv->len = desc.len; | |
106 | return 0; | |
107 | } | |
108 | ||
109 | static int nft_cmp_dump(struct sk_buff *skb, const struct nft_expr *expr) | |
110 | { | |
111 | const struct nft_cmp_expr *priv = nft_expr_priv(expr); | |
112 | ||
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; | |
117 | ||
118 | if (nft_data_dump(skb, NFTA_CMP_DATA, &priv->data, | |
119 | NFT_DATA_VALUE, priv->len) < 0) | |
120 | goto nla_put_failure; | |
121 | return 0; | |
122 | ||
123 | nla_put_failure: | |
124 | return -1; | |
125 | } | |
126 | ||
127 | static struct nft_expr_ops nft_cmp_ops __read_mostly = { | |
128 | .name = "cmp", | |
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, | |
136 | }; | |
137 | ||
138 | int __init nft_cmp_module_init(void) | |
139 | { | |
140 | return nft_register_expr(&nft_cmp_ops); | |
141 | } | |
142 | ||
143 | void nft_cmp_module_exit(void) | |
144 | { | |
145 | nft_unregister_expr(&nft_cmp_ops); | |
146 | } |