at v6.17-rc2 243 lines 6.2 kB view raw
1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * Copyright (c) 2012-2016 Pablo Neira Ayuso <pablo@netfilter.org> 4 */ 5 6#include <linux/init.h> 7#include <linux/module.h> 8#include <linux/skbuff.h> 9#include <linux/netlink.h> 10#include <linux/netfilter.h> 11#include <linux/netfilter/nf_tables.h> 12#include <net/netfilter/nf_tables_core.h> 13 14#define nft_objref_priv(expr) *((struct nft_object **)nft_expr_priv(expr)) 15 16void nft_objref_eval(const struct nft_expr *expr, 17 struct nft_regs *regs, 18 const struct nft_pktinfo *pkt) 19{ 20 struct nft_object *obj = nft_objref_priv(expr); 21 22 obj->ops->eval(obj, regs, pkt); 23} 24 25static int nft_objref_init(const struct nft_ctx *ctx, 26 const struct nft_expr *expr, 27 const struct nlattr * const tb[]) 28{ 29 struct nft_object *obj = nft_objref_priv(expr); 30 u8 genmask = nft_genmask_next(ctx->net); 31 u32 objtype; 32 33 if (!tb[NFTA_OBJREF_IMM_NAME] || 34 !tb[NFTA_OBJREF_IMM_TYPE]) 35 return -EINVAL; 36 37 objtype = ntohl(nla_get_be32(tb[NFTA_OBJREF_IMM_TYPE])); 38 obj = nft_obj_lookup(ctx->net, ctx->table, 39 tb[NFTA_OBJREF_IMM_NAME], objtype, 40 genmask); 41 if (IS_ERR(obj)) 42 return -ENOENT; 43 44 if (!nft_use_inc(&obj->use)) 45 return -EMFILE; 46 47 nft_objref_priv(expr) = obj; 48 49 return 0; 50} 51 52static int nft_objref_dump(struct sk_buff *skb, 53 const struct nft_expr *expr, bool reset) 54{ 55 const struct nft_object *obj = nft_objref_priv(expr); 56 57 if (nla_put_string(skb, NFTA_OBJREF_IMM_NAME, obj->key.name) || 58 nla_put_be32(skb, NFTA_OBJREF_IMM_TYPE, 59 htonl(obj->ops->type->type))) 60 goto nla_put_failure; 61 62 return 0; 63 64nla_put_failure: 65 return -1; 66} 67 68static void nft_objref_deactivate(const struct nft_ctx *ctx, 69 const struct nft_expr *expr, 70 enum nft_trans_phase phase) 71{ 72 struct nft_object *obj = nft_objref_priv(expr); 73 74 if (phase == NFT_TRANS_COMMIT) 75 return; 76 77 nft_use_dec(&obj->use); 78} 79 80static void nft_objref_activate(const struct nft_ctx *ctx, 81 const struct nft_expr *expr) 82{ 83 struct nft_object *obj = nft_objref_priv(expr); 84 85 nft_use_inc_restore(&obj->use); 86} 87 88static const struct nft_expr_ops nft_objref_ops = { 89 .type = &nft_objref_type, 90 .size = NFT_EXPR_SIZE(sizeof(struct nft_object *)), 91 .eval = nft_objref_eval, 92 .init = nft_objref_init, 93 .activate = nft_objref_activate, 94 .deactivate = nft_objref_deactivate, 95 .dump = nft_objref_dump, 96 .reduce = NFT_REDUCE_READONLY, 97}; 98 99struct nft_objref_map { 100 struct nft_set *set; 101 u8 sreg; 102 struct nft_set_binding binding; 103}; 104 105void nft_objref_map_eval(const struct nft_expr *expr, 106 struct nft_regs *regs, 107 const struct nft_pktinfo *pkt) 108{ 109 struct nft_objref_map *priv = nft_expr_priv(expr); 110 const struct nft_set *set = priv->set; 111 struct net *net = nft_net(pkt); 112 const struct nft_set_ext *ext; 113 struct nft_object *obj; 114 115 ext = nft_set_do_lookup(net, set, &regs->data[priv->sreg]); 116 if (!ext) { 117 ext = nft_set_catchall_lookup(net, set); 118 if (!ext) { 119 regs->verdict.code = NFT_BREAK; 120 return; 121 } 122 } 123 obj = *nft_set_ext_obj(ext); 124 obj->ops->eval(obj, regs, pkt); 125} 126 127static int nft_objref_map_init(const struct nft_ctx *ctx, 128 const struct nft_expr *expr, 129 const struct nlattr * const tb[]) 130{ 131 struct nft_objref_map *priv = nft_expr_priv(expr); 132 u8 genmask = nft_genmask_next(ctx->net); 133 struct nft_set *set; 134 int err; 135 136 set = nft_set_lookup_global(ctx->net, ctx->table, 137 tb[NFTA_OBJREF_SET_NAME], 138 tb[NFTA_OBJREF_SET_ID], genmask); 139 if (IS_ERR(set)) 140 return PTR_ERR(set); 141 142 if (!(set->flags & NFT_SET_OBJECT)) 143 return -EINVAL; 144 145 err = nft_parse_register_load(ctx, tb[NFTA_OBJREF_SET_SREG], &priv->sreg, 146 set->klen); 147 if (err < 0) 148 return err; 149 150 priv->binding.flags = set->flags & NFT_SET_OBJECT; 151 152 err = nf_tables_bind_set(ctx, set, &priv->binding); 153 if (err < 0) 154 return err; 155 156 priv->set = set; 157 return 0; 158} 159 160static int nft_objref_map_dump(struct sk_buff *skb, 161 const struct nft_expr *expr, bool reset) 162{ 163 const struct nft_objref_map *priv = nft_expr_priv(expr); 164 165 if (nft_dump_register(skb, NFTA_OBJREF_SET_SREG, priv->sreg) || 166 nla_put_string(skb, NFTA_OBJREF_SET_NAME, priv->set->name)) 167 goto nla_put_failure; 168 169 return 0; 170 171nla_put_failure: 172 return -1; 173} 174 175static void nft_objref_map_deactivate(const struct nft_ctx *ctx, 176 const struct nft_expr *expr, 177 enum nft_trans_phase phase) 178{ 179 struct nft_objref_map *priv = nft_expr_priv(expr); 180 181 nf_tables_deactivate_set(ctx, priv->set, &priv->binding, phase); 182} 183 184static void nft_objref_map_activate(const struct nft_ctx *ctx, 185 const struct nft_expr *expr) 186{ 187 struct nft_objref_map *priv = nft_expr_priv(expr); 188 189 nf_tables_activate_set(ctx, priv->set); 190} 191 192static void nft_objref_map_destroy(const struct nft_ctx *ctx, 193 const struct nft_expr *expr) 194{ 195 struct nft_objref_map *priv = nft_expr_priv(expr); 196 197 nf_tables_destroy_set(ctx, priv->set); 198} 199 200static const struct nft_expr_ops nft_objref_map_ops = { 201 .type = &nft_objref_type, 202 .size = NFT_EXPR_SIZE(sizeof(struct nft_objref_map)), 203 .eval = nft_objref_map_eval, 204 .init = nft_objref_map_init, 205 .activate = nft_objref_map_activate, 206 .deactivate = nft_objref_map_deactivate, 207 .destroy = nft_objref_map_destroy, 208 .dump = nft_objref_map_dump, 209 .reduce = NFT_REDUCE_READONLY, 210}; 211 212static const struct nft_expr_ops * 213nft_objref_select_ops(const struct nft_ctx *ctx, 214 const struct nlattr * const tb[]) 215{ 216 if (tb[NFTA_OBJREF_SET_SREG] && 217 (tb[NFTA_OBJREF_SET_NAME] || 218 tb[NFTA_OBJREF_SET_ID])) 219 return &nft_objref_map_ops; 220 else if (tb[NFTA_OBJREF_IMM_NAME] && 221 tb[NFTA_OBJREF_IMM_TYPE]) 222 return &nft_objref_ops; 223 224 return ERR_PTR(-EOPNOTSUPP); 225} 226 227static const struct nla_policy nft_objref_policy[NFTA_OBJREF_MAX + 1] = { 228 [NFTA_OBJREF_IMM_NAME] = { .type = NLA_STRING, 229 .len = NFT_OBJ_MAXNAMELEN - 1 }, 230 [NFTA_OBJREF_IMM_TYPE] = { .type = NLA_U32 }, 231 [NFTA_OBJREF_SET_SREG] = { .type = NLA_U32 }, 232 [NFTA_OBJREF_SET_NAME] = { .type = NLA_STRING, 233 .len = NFT_SET_MAXNAMELEN - 1 }, 234 [NFTA_OBJREF_SET_ID] = { .type = NLA_U32 }, 235}; 236 237struct nft_expr_type nft_objref_type __read_mostly = { 238 .name = "objref", 239 .select_ops = nft_objref_select_ops, 240 .policy = nft_objref_policy, 241 .maxattr = NFTA_OBJREF_MAX, 242 .owner = THIS_MODULE, 243};