Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v6.17-rc1 266 lines 6.9 kB view raw
1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * Copyright (c) 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/list.h> 11#include <linux/rbtree.h> 12#include <linux/netlink.h> 13#include <linux/netfilter.h> 14#include <linux/netfilter/nf_tables.h> 15#include <net/netfilter/nf_tables.h> 16#include <net/netfilter/nf_tables_core.h> 17 18struct nft_lookup { 19 struct nft_set *set; 20 u8 sreg; 21 u8 dreg; 22 bool dreg_set; 23 bool invert; 24 struct nft_set_binding binding; 25}; 26 27#ifdef CONFIG_MITIGATION_RETPOLINE 28const struct nft_set_ext * 29nft_set_do_lookup(const struct net *net, const struct nft_set *set, 30 const u32 *key) 31{ 32 if (set->ops == &nft_set_hash_fast_type.ops) 33 return nft_hash_lookup_fast(net, set, key); 34 if (set->ops == &nft_set_hash_type.ops) 35 return nft_hash_lookup(net, set, key); 36 37 if (set->ops == &nft_set_rhash_type.ops) 38 return nft_rhash_lookup(net, set, key); 39 40 if (set->ops == &nft_set_bitmap_type.ops) 41 return nft_bitmap_lookup(net, set, key); 42 43 if (set->ops == &nft_set_pipapo_type.ops) 44 return nft_pipapo_lookup(net, set, key); 45#if defined(CONFIG_X86_64) && !defined(CONFIG_UML) 46 if (set->ops == &nft_set_pipapo_avx2_type.ops) 47 return nft_pipapo_avx2_lookup(net, set, key); 48#endif 49 50 if (set->ops == &nft_set_rbtree_type.ops) 51 return nft_rbtree_lookup(net, set, key); 52 53 WARN_ON_ONCE(1); 54 return set->ops->lookup(net, set, key); 55} 56EXPORT_SYMBOL_GPL(nft_set_do_lookup); 57#endif 58 59void nft_lookup_eval(const struct nft_expr *expr, 60 struct nft_regs *regs, 61 const struct nft_pktinfo *pkt) 62{ 63 const struct nft_lookup *priv = nft_expr_priv(expr); 64 const struct nft_set *set = priv->set; 65 const struct net *net = nft_net(pkt); 66 const struct nft_set_ext *ext; 67 bool found; 68 69 ext = nft_set_do_lookup(net, set, &regs->data[priv->sreg]); 70 found = !!ext ^ priv->invert; 71 if (!found) { 72 ext = nft_set_catchall_lookup(net, set); 73 if (!ext) { 74 regs->verdict.code = NFT_BREAK; 75 return; 76 } 77 } 78 79 if (ext) { 80 if (priv->dreg_set) 81 nft_data_copy(&regs->data[priv->dreg], 82 nft_set_ext_data(ext), set->dlen); 83 84 nft_set_elem_update_expr(ext, regs, pkt); 85 } 86} 87 88static const struct nla_policy nft_lookup_policy[NFTA_LOOKUP_MAX + 1] = { 89 [NFTA_LOOKUP_SET] = { .type = NLA_STRING, 90 .len = NFT_SET_MAXNAMELEN - 1 }, 91 [NFTA_LOOKUP_SET_ID] = { .type = NLA_U32 }, 92 [NFTA_LOOKUP_SREG] = { .type = NLA_U32 }, 93 [NFTA_LOOKUP_DREG] = { .type = NLA_U32 }, 94 [NFTA_LOOKUP_FLAGS] = 95 NLA_POLICY_MASK(NLA_BE32, NFT_LOOKUP_F_INV), 96}; 97 98static int nft_lookup_init(const struct nft_ctx *ctx, 99 const struct nft_expr *expr, 100 const struct nlattr * const tb[]) 101{ 102 struct nft_lookup *priv = nft_expr_priv(expr); 103 u8 genmask = nft_genmask_next(ctx->net); 104 struct nft_set *set; 105 u32 flags; 106 int err; 107 108 if (tb[NFTA_LOOKUP_SET] == NULL || 109 tb[NFTA_LOOKUP_SREG] == NULL) 110 return -EINVAL; 111 112 set = nft_set_lookup_global(ctx->net, ctx->table, tb[NFTA_LOOKUP_SET], 113 tb[NFTA_LOOKUP_SET_ID], genmask); 114 if (IS_ERR(set)) 115 return PTR_ERR(set); 116 117 err = nft_parse_register_load(ctx, tb[NFTA_LOOKUP_SREG], &priv->sreg, 118 set->klen); 119 if (err < 0) 120 return err; 121 122 if (tb[NFTA_LOOKUP_FLAGS]) { 123 flags = ntohl(nla_get_be32(tb[NFTA_LOOKUP_FLAGS])); 124 125 if (flags & NFT_LOOKUP_F_INV) 126 priv->invert = true; 127 } 128 129 if (tb[NFTA_LOOKUP_DREG] != NULL) { 130 if (priv->invert) 131 return -EINVAL; 132 if (!(set->flags & NFT_SET_MAP)) 133 return -EINVAL; 134 135 err = nft_parse_register_store(ctx, tb[NFTA_LOOKUP_DREG], 136 &priv->dreg, NULL, 137 nft_set_datatype(set), 138 set->dlen); 139 if (err < 0) 140 return err; 141 priv->dreg_set = true; 142 } else if (set->flags & NFT_SET_MAP) { 143 /* Map given, but user asks for lookup only (i.e. to 144 * ignore value assoicated with key). 145 * 146 * This makes no sense for anonymous maps since they are 147 * scoped to the rule, but for named sets this can be useful. 148 */ 149 if (set->flags & NFT_SET_ANONYMOUS) 150 return -EINVAL; 151 } 152 153 priv->binding.flags = set->flags & NFT_SET_MAP; 154 155 err = nf_tables_bind_set(ctx, set, &priv->binding); 156 if (err < 0) 157 return err; 158 159 priv->set = set; 160 return 0; 161} 162 163static void nft_lookup_deactivate(const struct nft_ctx *ctx, 164 const struct nft_expr *expr, 165 enum nft_trans_phase phase) 166{ 167 struct nft_lookup *priv = nft_expr_priv(expr); 168 169 nf_tables_deactivate_set(ctx, priv->set, &priv->binding, phase); 170} 171 172static void nft_lookup_activate(const struct nft_ctx *ctx, 173 const struct nft_expr *expr) 174{ 175 struct nft_lookup *priv = nft_expr_priv(expr); 176 177 nf_tables_activate_set(ctx, priv->set); 178} 179 180static void nft_lookup_destroy(const struct nft_ctx *ctx, 181 const struct nft_expr *expr) 182{ 183 struct nft_lookup *priv = nft_expr_priv(expr); 184 185 nf_tables_destroy_set(ctx, priv->set); 186} 187 188static int nft_lookup_dump(struct sk_buff *skb, 189 const struct nft_expr *expr, bool reset) 190{ 191 const struct nft_lookup *priv = nft_expr_priv(expr); 192 u32 flags = priv->invert ? NFT_LOOKUP_F_INV : 0; 193 194 if (nla_put_string(skb, NFTA_LOOKUP_SET, priv->set->name)) 195 goto nla_put_failure; 196 if (nft_dump_register(skb, NFTA_LOOKUP_SREG, priv->sreg)) 197 goto nla_put_failure; 198 if (priv->dreg_set) 199 if (nft_dump_register(skb, NFTA_LOOKUP_DREG, priv->dreg)) 200 goto nla_put_failure; 201 if (nla_put_be32(skb, NFTA_LOOKUP_FLAGS, htonl(flags))) 202 goto nla_put_failure; 203 return 0; 204 205nla_put_failure: 206 return -1; 207} 208 209static int nft_lookup_validate(const struct nft_ctx *ctx, 210 const struct nft_expr *expr) 211{ 212 const struct nft_lookup *priv = nft_expr_priv(expr); 213 struct nft_set_iter iter; 214 215 if (!(priv->set->flags & NFT_SET_MAP) || 216 priv->set->dtype != NFT_DATA_VERDICT) 217 return 0; 218 219 iter.genmask = nft_genmask_next(ctx->net); 220 iter.type = NFT_ITER_UPDATE; 221 iter.skip = 0; 222 iter.count = 0; 223 iter.err = 0; 224 iter.fn = nft_setelem_validate; 225 226 priv->set->ops->walk(ctx, priv->set, &iter); 227 if (!iter.err) 228 iter.err = nft_set_catchall_validate(ctx, priv->set); 229 230 if (iter.err < 0) 231 return iter.err; 232 233 return 0; 234} 235 236static bool nft_lookup_reduce(struct nft_regs_track *track, 237 const struct nft_expr *expr) 238{ 239 const struct nft_lookup *priv = nft_expr_priv(expr); 240 241 if (priv->set->flags & NFT_SET_MAP) 242 nft_reg_track_cancel(track, priv->dreg, priv->set->dlen); 243 244 return false; 245} 246 247static const struct nft_expr_ops nft_lookup_ops = { 248 .type = &nft_lookup_type, 249 .size = NFT_EXPR_SIZE(sizeof(struct nft_lookup)), 250 .eval = nft_lookup_eval, 251 .init = nft_lookup_init, 252 .activate = nft_lookup_activate, 253 .deactivate = nft_lookup_deactivate, 254 .destroy = nft_lookup_destroy, 255 .dump = nft_lookup_dump, 256 .validate = nft_lookup_validate, 257 .reduce = nft_lookup_reduce, 258}; 259 260struct nft_expr_type nft_lookup_type __read_mostly = { 261 .name = "lookup", 262 .ops = &nft_lookup_ops, 263 .policy = nft_lookup_policy, 264 .maxattr = NFTA_LOOKUP_MAX, 265 .owner = THIS_MODULE, 266};