~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/netfilter/nft_cmp.c

Version: ~ [ linux-5.10-rc6 ] ~ [ linux-5.9.12 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.81 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.161 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.210 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.247 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.247 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-only
  2 /*
  3  * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
  4  *
  5  * Development of this code funded by Astaro AG (http://www.astaro.com/)
  6  */
  7 
  8 #include <linux/kernel.h>
  9 #include <linux/init.h>
 10 #include <linux/module.h>
 11 #include <linux/netlink.h>
 12 #include <linux/netfilter.h>
 13 #include <linux/if_arp.h>
 14 #include <linux/netfilter/nf_tables.h>
 15 #include <net/netfilter/nf_tables_core.h>
 16 #include <net/netfilter/nf_tables_offload.h>
 17 #include <net/netfilter/nf_tables.h>
 18 
 19 struct nft_cmp_expr {
 20         struct nft_data         data;
 21         enum nft_registers      sreg:8;
 22         u8                      len;
 23         enum nft_cmp_ops        op:8;
 24 };
 25 
 26 void nft_cmp_eval(const struct nft_expr *expr,
 27                   struct nft_regs *regs,
 28                   const struct nft_pktinfo *pkt)
 29 {
 30         const struct nft_cmp_expr *priv = nft_expr_priv(expr);
 31         int d;
 32 
 33         d = memcmp(&regs->data[priv->sreg], &priv->data, priv->len);
 34         switch (priv->op) {
 35         case NFT_CMP_EQ:
 36                 if (d != 0)
 37                         goto mismatch;
 38                 break;
 39         case NFT_CMP_NEQ:
 40                 if (d == 0)
 41                         goto mismatch;
 42                 break;
 43         case NFT_CMP_LT:
 44                 if (d == 0)
 45                         goto mismatch;
 46                 fallthrough;
 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                 fallthrough;
 55         case NFT_CMP_GTE:
 56                 if (d < 0)
 57                         goto mismatch;
 58                 break;
 59         }
 60         return;
 61 
 62 mismatch:
 63         regs->verdict.code = NFT_BREAK;
 64 }
 65 
 66 static const struct nla_policy nft_cmp_policy[NFTA_CMP_MAX + 1] = {
 67         [NFTA_CMP_SREG]         = { .type = NLA_U32 },
 68         [NFTA_CMP_OP]           = { .type = NLA_U32 },
 69         [NFTA_CMP_DATA]         = { .type = NLA_NESTED },
 70 };
 71 
 72 static int nft_cmp_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
 73                         const struct nlattr * const tb[])
 74 {
 75         struct nft_cmp_expr *priv = nft_expr_priv(expr);
 76         struct nft_data_desc desc;
 77         int err;
 78 
 79         err = nft_data_init(NULL, &priv->data, sizeof(priv->data), &desc,
 80                             tb[NFTA_CMP_DATA]);
 81         if (err < 0)
 82                 return err;
 83 
 84         if (desc.type != NFT_DATA_VALUE) {
 85                 err = -EINVAL;
 86                 nft_data_release(&priv->data, desc.type);
 87                 return err;
 88         }
 89 
 90         priv->sreg = nft_parse_register(tb[NFTA_CMP_SREG]);
 91         err = nft_validate_register_load(priv->sreg, desc.len);
 92         if (err < 0)
 93                 return err;
 94 
 95         priv->op  = ntohl(nla_get_be32(tb[NFTA_CMP_OP]));
 96         priv->len = desc.len;
 97         return 0;
 98 }
 99 
100 static int nft_cmp_dump(struct sk_buff *skb, const struct nft_expr *expr)
101 {
102         const struct nft_cmp_expr *priv = nft_expr_priv(expr);
103 
104         if (nft_dump_register(skb, NFTA_CMP_SREG, priv->sreg))
105                 goto nla_put_failure;
106         if (nla_put_be32(skb, NFTA_CMP_OP, htonl(priv->op)))
107                 goto nla_put_failure;
108 
109         if (nft_data_dump(skb, NFTA_CMP_DATA, &priv->data,
110                           NFT_DATA_VALUE, priv->len) < 0)
111                 goto nla_put_failure;
112         return 0;
113 
114 nla_put_failure:
115         return -1;
116 }
117 
118 static int __nft_cmp_offload(struct nft_offload_ctx *ctx,
119                              struct nft_flow_rule *flow,
120                              const struct nft_cmp_expr *priv)
121 {
122         struct nft_offload_reg *reg = &ctx->regs[priv->sreg];
123         u8 *mask = (u8 *)&flow->match.mask;
124         u8 *key = (u8 *)&flow->match.key;
125 
126         if (priv->op != NFT_CMP_EQ || reg->len != priv->len)
127                 return -EOPNOTSUPP;
128 
129         memcpy(key + reg->offset, &priv->data, priv->len);
130         memcpy(mask + reg->offset, &reg->mask, priv->len);
131 
132         flow->match.dissector.used_keys |= BIT(reg->key);
133         flow->match.dissector.offset[reg->key] = reg->base_offset;
134 
135         if (reg->key == FLOW_DISSECTOR_KEY_META &&
136             reg->offset == offsetof(struct nft_flow_key, meta.ingress_iftype) &&
137             nft_reg_load16(priv->data.data) != ARPHRD_ETHER)
138                 return -EOPNOTSUPP;
139 
140         nft_offload_update_dependency(ctx, &priv->data, priv->len);
141 
142         return 0;
143 }
144 
145 static int nft_cmp_offload(struct nft_offload_ctx *ctx,
146                            struct nft_flow_rule *flow,
147                            const struct nft_expr *expr)
148 {
149         const struct nft_cmp_expr *priv = nft_expr_priv(expr);
150 
151         return __nft_cmp_offload(ctx, flow, priv);
152 }
153 
154 static const struct nft_expr_ops nft_cmp_ops = {
155         .type           = &nft_cmp_type,
156         .size           = NFT_EXPR_SIZE(sizeof(struct nft_cmp_expr)),
157         .eval           = nft_cmp_eval,
158         .init           = nft_cmp_init,
159         .dump           = nft_cmp_dump,
160         .offload        = nft_cmp_offload,
161 };
162 
163 static int nft_cmp_fast_init(const struct nft_ctx *ctx,
164                              const struct nft_expr *expr,
165                              const struct nlattr * const tb[])
166 {
167         struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
168         struct nft_data_desc desc;
169         struct nft_data data;
170         int err;
171 
172         err = nft_data_init(NULL, &data, sizeof(data), &desc,
173                             tb[NFTA_CMP_DATA]);
174         if (err < 0)
175                 return err;
176 
177         priv->sreg = nft_parse_register(tb[NFTA_CMP_SREG]);
178         err = nft_validate_register_load(priv->sreg, desc.len);
179         if (err < 0)
180                 return err;
181 
182         desc.len *= BITS_PER_BYTE;
183 
184         priv->mask = nft_cmp_fast_mask(desc.len);
185         priv->data = data.data[0] & priv->mask;
186         priv->len  = desc.len;
187         priv->inv  = ntohl(nla_get_be32(tb[NFTA_CMP_OP])) != NFT_CMP_EQ;
188         return 0;
189 }
190 
191 static int nft_cmp_fast_offload(struct nft_offload_ctx *ctx,
192                                 struct nft_flow_rule *flow,
193                                 const struct nft_expr *expr)
194 {
195         const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
196         struct nft_cmp_expr cmp = {
197                 .data   = {
198                         .data   = {
199                                 [0] = priv->data,
200                         },
201                 },
202                 .sreg   = priv->sreg,
203                 .len    = priv->len / BITS_PER_BYTE,
204                 .op     = priv->inv ? NFT_CMP_NEQ : NFT_CMP_EQ,
205         };
206 
207         return __nft_cmp_offload(ctx, flow, &cmp);
208 }
209 
210 static int nft_cmp_fast_dump(struct sk_buff *skb, const struct nft_expr *expr)
211 {
212         const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
213         enum nft_cmp_ops op = priv->inv ? NFT_CMP_NEQ : NFT_CMP_EQ;
214         struct nft_data data;
215 
216         if (nft_dump_register(skb, NFTA_CMP_SREG, priv->sreg))
217                 goto nla_put_failure;
218         if (nla_put_be32(skb, NFTA_CMP_OP, htonl(op)))
219                 goto nla_put_failure;
220 
221         data.data[0] = priv->data;
222         if (nft_data_dump(skb, NFTA_CMP_DATA, &data,
223                           NFT_DATA_VALUE, priv->len / BITS_PER_BYTE) < 0)
224                 goto nla_put_failure;
225         return 0;
226 
227 nla_put_failure:
228         return -1;
229 }
230 
231 const struct nft_expr_ops nft_cmp_fast_ops = {
232         .type           = &nft_cmp_type,
233         .size           = NFT_EXPR_SIZE(sizeof(struct nft_cmp_fast_expr)),
234         .eval           = NULL, /* inlined */
235         .init           = nft_cmp_fast_init,
236         .dump           = nft_cmp_fast_dump,
237         .offload        = nft_cmp_fast_offload,
238 };
239 
240 static const struct nft_expr_ops *
241 nft_cmp_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[])
242 {
243         struct nft_data_desc desc;
244         struct nft_data data;
245         enum nft_cmp_ops op;
246         int err;
247 
248         if (tb[NFTA_CMP_SREG] == NULL ||
249             tb[NFTA_CMP_OP] == NULL ||
250             tb[NFTA_CMP_DATA] == NULL)
251                 return ERR_PTR(-EINVAL);
252 
253         op = ntohl(nla_get_be32(tb[NFTA_CMP_OP]));
254         switch (op) {
255         case NFT_CMP_EQ:
256         case NFT_CMP_NEQ:
257         case NFT_CMP_LT:
258         case NFT_CMP_LTE:
259         case NFT_CMP_GT:
260         case NFT_CMP_GTE:
261                 break;
262         default:
263                 return ERR_PTR(-EINVAL);
264         }
265 
266         err = nft_data_init(NULL, &data, sizeof(data), &desc,
267                             tb[NFTA_CMP_DATA]);
268         if (err < 0)
269                 return ERR_PTR(err);
270 
271         if (desc.type != NFT_DATA_VALUE) {
272                 err = -EINVAL;
273                 goto err1;
274         }
275 
276         if (desc.len <= sizeof(u32) && (op == NFT_CMP_EQ || op == NFT_CMP_NEQ))
277                 return &nft_cmp_fast_ops;
278 
279         return &nft_cmp_ops;
280 err1:
281         nft_data_release(&data, desc.type);
282         return ERR_PTR(-EINVAL);
283 }
284 
285 struct nft_expr_type nft_cmp_type __read_mostly = {
286         .name           = "cmp",
287         .select_ops     = nft_cmp_select_ops,
288         .policy         = nft_cmp_policy,
289         .maxattr        = NFTA_CMP_MAX,
290         .owner          = THIS_MODULE,
291 };
292 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp