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