linux/net/netfilter/nft_objref.c
<<
>>
Prefs
   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->net, ctx->table,
  42                             tb[NFTA_OBJREF_IMM_NAME], objtype,
  43                             genmask);
  44        if (IS_ERR(obj))
  45                return -ENOENT;
  46
  47        nft_objref_priv(expr) = obj;
  48        obj->use++;
  49
  50        return 0;
  51}
  52
  53static int nft_objref_dump(struct sk_buff *skb, const struct nft_expr *expr)
  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        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        obj->use++;
  86}
  87
  88static struct nft_expr_type nft_objref_type;
  89static const struct nft_expr_ops nft_objref_ops = {
  90        .type           = &nft_objref_type,
  91        .size           = NFT_EXPR_SIZE(sizeof(struct nft_object *)),
  92        .eval           = nft_objref_eval,
  93        .init           = nft_objref_init,
  94        .activate       = nft_objref_activate,
  95        .deactivate     = nft_objref_deactivate,
  96        .dump           = nft_objref_dump,
  97};
  98
  99struct nft_objref_map {
 100        struct nft_set          *set;
 101        enum nft_registers      sreg:8;
 102        struct nft_set_binding  binding;
 103};
 104
 105static void 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        const struct nft_set_ext *ext;
 112        struct nft_object *obj;
 113        bool found;
 114
 115        found = set->ops->lookup(nft_net(pkt), set, &regs->data[priv->sreg],
 116                                 &ext);
 117        if (!found) {
 118                regs->verdict.code = NFT_BREAK;
 119                return;
 120        }
 121        obj = *nft_set_ext_obj(ext);
 122        obj->ops->eval(obj, regs, pkt);
 123}
 124
 125static int nft_objref_map_init(const struct nft_ctx *ctx,
 126                               const struct nft_expr *expr,
 127                               const struct nlattr * const tb[])
 128{
 129        struct nft_objref_map *priv = nft_expr_priv(expr);
 130        u8 genmask = nft_genmask_next(ctx->net);
 131        struct nft_set *set;
 132        int err;
 133
 134        set = nft_set_lookup_global(ctx->net, ctx->table,
 135                                    tb[NFTA_OBJREF_SET_NAME],
 136                                    tb[NFTA_OBJREF_SET_ID], genmask);
 137        if (IS_ERR(set))
 138                return PTR_ERR(set);
 139
 140        if (!(set->flags & NFT_SET_OBJECT))
 141                return -EINVAL;
 142
 143        priv->sreg = nft_parse_register(tb[NFTA_OBJREF_SET_SREG]);
 144        err = nft_validate_register_load(priv->sreg, set->klen);
 145        if (err < 0)
 146                return err;
 147
 148        priv->binding.flags = set->flags & NFT_SET_OBJECT;
 149
 150        err = nf_tables_bind_set(ctx, set, &priv->binding);
 151        if (err < 0)
 152                return err;
 153
 154        priv->set = set;
 155        return 0;
 156}
 157
 158static int nft_objref_map_dump(struct sk_buff *skb, const struct nft_expr *expr)
 159{
 160        const struct nft_objref_map *priv = nft_expr_priv(expr);
 161
 162        if (nft_dump_register(skb, NFTA_OBJREF_SET_SREG, priv->sreg) ||
 163            nla_put_string(skb, NFTA_OBJREF_SET_NAME, priv->set->name))
 164                goto nla_put_failure;
 165
 166        return 0;
 167
 168nla_put_failure:
 169        return -1;
 170}
 171
 172static void nft_objref_map_deactivate(const struct nft_ctx *ctx,
 173                                      const struct nft_expr *expr,
 174                                      enum nft_trans_phase phase)
 175{
 176        struct nft_objref_map *priv = nft_expr_priv(expr);
 177
 178        if (phase == NFT_TRANS_PREPARE)
 179                return;
 180
 181        nf_tables_unbind_set(ctx, priv->set, &priv->binding,
 182                             phase == NFT_TRANS_COMMIT);
 183}
 184
 185static void nft_objref_map_destroy(const struct nft_ctx *ctx,
 186                                   const struct nft_expr *expr)
 187{
 188        struct nft_objref_map *priv = nft_expr_priv(expr);
 189
 190        nf_tables_destroy_set(ctx, priv->set);
 191}
 192
 193static struct nft_expr_type nft_objref_type;
 194static const struct nft_expr_ops nft_objref_map_ops = {
 195        .type           = &nft_objref_type,
 196        .size           = NFT_EXPR_SIZE(sizeof(struct nft_objref_map)),
 197        .eval           = nft_objref_map_eval,
 198        .init           = nft_objref_map_init,
 199        .deactivate     = nft_objref_map_deactivate,
 200        .destroy        = nft_objref_map_destroy,
 201        .dump           = nft_objref_map_dump,
 202};
 203
 204static const struct nft_expr_ops *
 205nft_objref_select_ops(const struct nft_ctx *ctx,
 206                      const struct nlattr * const tb[])
 207{
 208        if (tb[NFTA_OBJREF_SET_SREG] &&
 209            (tb[NFTA_OBJREF_SET_NAME] ||
 210             tb[NFTA_OBJREF_SET_ID]))
 211                return &nft_objref_map_ops;
 212        else if (tb[NFTA_OBJREF_IMM_NAME] &&
 213                 tb[NFTA_OBJREF_IMM_TYPE])
 214                return &nft_objref_ops;
 215
 216        return ERR_PTR(-EOPNOTSUPP);
 217}
 218
 219static const struct nla_policy nft_objref_policy[NFTA_OBJREF_MAX + 1] = {
 220        [NFTA_OBJREF_IMM_NAME]  = { .type = NLA_STRING,
 221                                    .len = NFT_OBJ_MAXNAMELEN - 1 },
 222        [NFTA_OBJREF_IMM_TYPE]  = { .type = NLA_U32 },
 223        [NFTA_OBJREF_SET_SREG]  = { .type = NLA_U32 },
 224        [NFTA_OBJREF_SET_NAME]  = { .type = NLA_STRING,
 225                                    .len = NFT_SET_MAXNAMELEN - 1 },
 226        [NFTA_OBJREF_SET_ID]    = { .type = NLA_U32 },
 227};
 228
 229static struct nft_expr_type nft_objref_type __read_mostly = {
 230        .name           = "objref",
 231        .select_ops     = nft_objref_select_ops,
 232        .policy         = nft_objref_policy,
 233        .maxattr        = NFTA_OBJREF_MAX,
 234        .owner          = THIS_MODULE,
 235};
 236
 237static int __init nft_objref_module_init(void)
 238{
 239        return nft_register_expr(&nft_objref_type);
 240}
 241
 242static void __exit nft_objref_module_exit(void)
 243{
 244        nft_unregister_expr(&nft_objref_type);
 245}
 246
 247module_init(nft_objref_module_init);
 248module_exit(nft_objref_module_exit);
 249
 250MODULE_LICENSE("GPL");
 251MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
 252MODULE_ALIAS_NFT_EXPR("objref");
 253