Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1 | /* |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 2 | * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net> |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * Development of this code funded by Astaro AG (http://www.astaro.com/) |
| 9 | */ |
| 10 | |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/netlink.h> |
| 15 | #include <linux/netfilter.h> |
| 16 | #include <linux/netfilter/nf_tables.h> |
| 17 | #include <net/netfilter/nf_tables_core.h> |
| 18 | #include <net/netfilter/nf_tables.h> |
| 19 | |
| 20 | struct nft_immediate_expr { |
| 21 | struct nft_data data; |
| 22 | enum nft_registers dreg:8; |
| 23 | u8 dlen; |
| 24 | }; |
| 25 | |
| 26 | static void nft_immediate_eval(const struct nft_expr *expr, |
Patrick McHardy | a55e22e | 2015-04-11 02:27:31 +0100 | [diff] [blame] | 27 | struct nft_regs *regs, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 28 | const struct nft_pktinfo *pkt) |
| 29 | { |
| 30 | const struct nft_immediate_expr *priv = nft_expr_priv(expr); |
| 31 | |
Patrick McHardy | 49499c3 | 2015-04-11 02:27:37 +0100 | [diff] [blame] | 32 | nft_data_copy(®s->data[priv->dreg], &priv->data, priv->dlen); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | static const struct nla_policy nft_immediate_policy[NFTA_IMMEDIATE_MAX + 1] = { |
| 36 | [NFTA_IMMEDIATE_DREG] = { .type = NLA_U32 }, |
| 37 | [NFTA_IMMEDIATE_DATA] = { .type = NLA_NESTED }, |
| 38 | }; |
| 39 | |
| 40 | static int nft_immediate_init(const struct nft_ctx *ctx, |
| 41 | const struct nft_expr *expr, |
| 42 | const struct nlattr * const tb[]) |
| 43 | { |
| 44 | struct nft_immediate_expr *priv = nft_expr_priv(expr); |
| 45 | struct nft_data_desc desc; |
| 46 | int err; |
| 47 | |
| 48 | if (tb[NFTA_IMMEDIATE_DREG] == NULL || |
| 49 | tb[NFTA_IMMEDIATE_DATA] == NULL) |
| 50 | return -EINVAL; |
| 51 | |
Patrick McHardy | d0a11fc | 2015-04-11 02:27:38 +0100 | [diff] [blame] | 52 | err = nft_data_init(ctx, &priv->data, sizeof(priv->data), &desc, |
| 53 | tb[NFTA_IMMEDIATE_DATA]); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 54 | if (err < 0) |
| 55 | return err; |
Laura Garcia Liebana | 36b701f | 2016-09-14 15:00:02 +0200 | [diff] [blame] | 56 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 57 | priv->dlen = desc.len; |
| 58 | |
Patrick McHardy | b1c96ed | 2015-04-11 02:27:36 +0100 | [diff] [blame] | 59 | priv->dreg = nft_parse_register(tb[NFTA_IMMEDIATE_DREG]); |
Patrick McHardy | 1ec1021 | 2015-04-11 02:27:27 +0100 | [diff] [blame] | 60 | err = nft_validate_register_store(ctx, priv->dreg, &priv->data, |
| 61 | desc.type, desc.len); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 62 | if (err < 0) |
| 63 | goto err1; |
| 64 | |
| 65 | return 0; |
| 66 | |
| 67 | err1: |
| 68 | nft_data_uninit(&priv->data, desc.type); |
| 69 | return err; |
| 70 | } |
| 71 | |
Patrick McHardy | 62472bc | 2014-03-07 19:08:30 +0100 | [diff] [blame] | 72 | static void nft_immediate_destroy(const struct nft_ctx *ctx, |
| 73 | const struct nft_expr *expr) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 74 | { |
| 75 | const struct nft_immediate_expr *priv = nft_expr_priv(expr); |
| 76 | return nft_data_uninit(&priv->data, nft_dreg_to_type(priv->dreg)); |
| 77 | } |
| 78 | |
| 79 | static int nft_immediate_dump(struct sk_buff *skb, const struct nft_expr *expr) |
| 80 | { |
| 81 | const struct nft_immediate_expr *priv = nft_expr_priv(expr); |
| 82 | |
Patrick McHardy | b1c96ed | 2015-04-11 02:27:36 +0100 | [diff] [blame] | 83 | if (nft_dump_register(skb, NFTA_IMMEDIATE_DREG, priv->dreg)) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 84 | goto nla_put_failure; |
| 85 | |
| 86 | return nft_data_dump(skb, NFTA_IMMEDIATE_DATA, &priv->data, |
| 87 | nft_dreg_to_type(priv->dreg), priv->dlen); |
| 88 | |
| 89 | nla_put_failure: |
| 90 | return -1; |
| 91 | } |
| 92 | |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 93 | static int nft_immediate_validate(const struct nft_ctx *ctx, |
| 94 | const struct nft_expr *expr, |
| 95 | const struct nft_data **data) |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 96 | { |
| 97 | const struct nft_immediate_expr *priv = nft_expr_priv(expr); |
| 98 | |
| 99 | if (priv->dreg == NFT_REG_VERDICT) |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 100 | *data = &priv->data; |
| 101 | |
| 102 | return 0; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 105 | static const struct nft_expr_ops nft_imm_ops = { |
| 106 | .type = &nft_imm_type, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 107 | .size = NFT_EXPR_SIZE(sizeof(struct nft_immediate_expr)), |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 108 | .eval = nft_immediate_eval, |
| 109 | .init = nft_immediate_init, |
| 110 | .destroy = nft_immediate_destroy, |
| 111 | .dump = nft_immediate_dump, |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 112 | .validate = nft_immediate_validate, |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 113 | }; |
| 114 | |
Liping Zhang | 4e24877 | 2016-11-06 21:15:51 +0800 | [diff] [blame^] | 115 | struct nft_expr_type nft_imm_type __read_mostly = { |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 116 | .name = "immediate", |
| 117 | .ops = &nft_imm_ops, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 118 | .policy = nft_immediate_policy, |
| 119 | .maxattr = NFTA_IMMEDIATE_MAX, |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 120 | .owner = THIS_MODULE, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 121 | }; |