| @@ -0,0 +1,112 @@ |
| 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 | + |
| 19 | +static 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->type->eval(obj, regs, pkt); |
| 26 | +} |
| 27 | + |
| 28 | +static 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 = nf_tables_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 | + |
| 52 | +static 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, htonl(obj->type->type))) |
| 58 | + goto nla_put_failure; |
| 59 | + |
| 60 | + return 0; |
| 61 | + |
| 62 | +nla_put_failure: |
| 63 | + return -1; |
| 64 | +} |
| 65 | + |
| 66 | +static void nft_objref_destroy(const struct nft_ctx *ctx, |
| 67 | + const struct nft_expr *expr) |
| 68 | +{ |
| 69 | + struct nft_object *obj = nft_objref_priv(expr); |
| 70 | + |
| 71 | + obj->use--; |
| 72 | +} |
| 73 | + |
| 74 | +static struct nft_expr_type nft_objref_type; |
| 75 | +static const struct nft_expr_ops nft_objref_ops = { |
| 76 | + .type = &nft_objref_type, |
| 77 | + .size = NFT_EXPR_SIZE(sizeof(struct nft_object *)), |
| 78 | + .eval = nft_objref_eval, |
| 79 | + .init = nft_objref_init, |
| 80 | + .destroy = nft_objref_destroy, |
| 81 | + .dump = nft_objref_dump, |
| 82 | +}; |
| 83 | + |
| 84 | +static const struct nla_policy nft_objref_policy[NFTA_OBJREF_MAX + 1] = { |
| 85 | + [NFTA_OBJREF_IMM_NAME] = { .type = NLA_STRING }, |
| 86 | + [NFTA_OBJREF_IMM_TYPE] = { .type = NLA_U32 }, |
| 87 | +}; |
| 88 | + |
| 89 | +static struct nft_expr_type nft_objref_type __read_mostly = { |
| 90 | + .name = "objref", |
| 91 | + .ops = &nft_objref_ops, |
| 92 | + .policy = nft_objref_policy, |
| 93 | + .maxattr = NFTA_OBJREF_MAX, |
| 94 | + .owner = THIS_MODULE, |
| 95 | +}; |
| 96 | + |
| 97 | +static int __init nft_objref_module_init(void) |
| 98 | +{ |
| 99 | + return nft_register_expr(&nft_objref_type); |
| 100 | +} |
| 101 | + |
| 102 | +static void __exit nft_objref_module_exit(void) |
| 103 | +{ |
| 104 | + nft_unregister_expr(&nft_objref_type); |
| 105 | +} |
| 106 | + |
| 107 | +module_init(nft_objref_module_init); |
| 108 | +module_exit(nft_objref_module_exit); |
| 109 | + |
| 110 | +MODULE_LICENSE("GPL"); |
| 111 | +MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>"); |
| 112 | +MODULE_ALIAS_NFT_EXPR("objref"); |