Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 2 | /* |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 3 | * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net> |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4 | * |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 5 | * Development of this code funded by Astaro AG (http://www.astaro.com/) |
| 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/netlink.h> |
| 12 | #include <linux/netfilter.h> |
| 13 | #include <linux/netfilter/nf_tables.h> |
| 14 | #include <net/netfilter/nf_tables_core.h> |
| 15 | #include <net/netfilter/nf_tables.h> |
Pablo Neira Ayuso | c9626a2 | 2019-07-09 23:00:43 +0200 | [diff] [blame] | 16 | #include <net/netfilter/nf_tables_offload.h> |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 17 | |
Florian Westphal | 10870dd | 2019-01-08 17:35:34 +0100 | [diff] [blame] | 18 | void nft_immediate_eval(const struct nft_expr *expr, |
| 19 | struct nft_regs *regs, |
| 20 | const struct nft_pktinfo *pkt) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 21 | { |
| 22 | const struct nft_immediate_expr *priv = nft_expr_priv(expr); |
| 23 | |
Patrick McHardy | 49499c3 | 2015-04-11 02:27:37 +0100 | [diff] [blame] | 24 | nft_data_copy(®s->data[priv->dreg], &priv->data, priv->dlen); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | static const struct nla_policy nft_immediate_policy[NFTA_IMMEDIATE_MAX + 1] = { |
| 28 | [NFTA_IMMEDIATE_DREG] = { .type = NLA_U32 }, |
| 29 | [NFTA_IMMEDIATE_DATA] = { .type = NLA_NESTED }, |
| 30 | }; |
| 31 | |
| 32 | static int nft_immediate_init(const struct nft_ctx *ctx, |
| 33 | const struct nft_expr *expr, |
| 34 | const struct nlattr * const tb[]) |
| 35 | { |
| 36 | struct nft_immediate_expr *priv = nft_expr_priv(expr); |
| 37 | struct nft_data_desc desc; |
| 38 | int err; |
| 39 | |
| 40 | if (tb[NFTA_IMMEDIATE_DREG] == NULL || |
| 41 | tb[NFTA_IMMEDIATE_DATA] == NULL) |
| 42 | return -EINVAL; |
| 43 | |
Patrick McHardy | d0a11fc | 2015-04-11 02:27:38 +0100 | [diff] [blame] | 44 | err = nft_data_init(ctx, &priv->data, sizeof(priv->data), &desc, |
| 45 | tb[NFTA_IMMEDIATE_DATA]); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 46 | if (err < 0) |
| 47 | return err; |
Laura Garcia Liebana | 36b701f | 2016-09-14 15:00:02 +0200 | [diff] [blame] | 48 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 49 | priv->dlen = desc.len; |
| 50 | |
Patrick McHardy | b1c96ed | 2015-04-11 02:27:36 +0100 | [diff] [blame] | 51 | priv->dreg = nft_parse_register(tb[NFTA_IMMEDIATE_DREG]); |
Patrick McHardy | 1ec1021 | 2015-04-11 02:27:27 +0100 | [diff] [blame] | 52 | err = nft_validate_register_store(ctx, priv->dreg, &priv->data, |
| 53 | desc.type, desc.len); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 54 | if (err < 0) |
| 55 | goto err1; |
| 56 | |
| 57 | return 0; |
| 58 | |
| 59 | err1: |
Pablo Neira Ayuso | 5910544 | 2017-05-15 11:17:34 +0100 | [diff] [blame] | 60 | nft_data_release(&priv->data, desc.type); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 61 | return err; |
| 62 | } |
| 63 | |
Pablo Neira Ayuso | bb7b40a | 2018-05-08 02:43:57 +0200 | [diff] [blame] | 64 | static void nft_immediate_activate(const struct nft_ctx *ctx, |
| 65 | const struct nft_expr *expr) |
| 66 | { |
| 67 | const struct nft_immediate_expr *priv = nft_expr_priv(expr); |
| 68 | |
| 69 | return nft_data_hold(&priv->data, nft_dreg_to_type(priv->dreg)); |
| 70 | } |
| 71 | |
| 72 | static void nft_immediate_deactivate(const struct nft_ctx *ctx, |
Pablo Neira Ayuso | f6ac858 | 2019-02-02 10:49:13 +0100 | [diff] [blame] | 73 | const struct nft_expr *expr, |
| 74 | enum nft_trans_phase phase) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 75 | { |
| 76 | const struct nft_immediate_expr *priv = nft_expr_priv(expr); |
Pablo Neira Ayuso | 5910544 | 2017-05-15 11:17:34 +0100 | [diff] [blame] | 77 | |
Pablo Neira Ayuso | f6ac858 | 2019-02-02 10:49:13 +0100 | [diff] [blame] | 78 | if (phase == NFT_TRANS_COMMIT) |
| 79 | return; |
| 80 | |
Pablo Neira Ayuso | 5910544 | 2017-05-15 11:17:34 +0100 | [diff] [blame] | 81 | return nft_data_release(&priv->data, nft_dreg_to_type(priv->dreg)); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static int nft_immediate_dump(struct sk_buff *skb, const struct nft_expr *expr) |
| 85 | { |
| 86 | const struct nft_immediate_expr *priv = nft_expr_priv(expr); |
| 87 | |
Patrick McHardy | b1c96ed | 2015-04-11 02:27:36 +0100 | [diff] [blame] | 88 | if (nft_dump_register(skb, NFTA_IMMEDIATE_DREG, priv->dreg)) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 89 | goto nla_put_failure; |
| 90 | |
| 91 | return nft_data_dump(skb, NFTA_IMMEDIATE_DATA, &priv->data, |
| 92 | nft_dreg_to_type(priv->dreg), priv->dlen); |
| 93 | |
| 94 | nla_put_failure: |
| 95 | return -1; |
| 96 | } |
| 97 | |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 98 | static int nft_immediate_validate(const struct nft_ctx *ctx, |
| 99 | const struct nft_expr *expr, |
Pablo Neira Ayuso | a654de8 | 2018-05-30 20:18:57 +0200 | [diff] [blame] | 100 | const struct nft_data **d) |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 101 | { |
| 102 | const struct nft_immediate_expr *priv = nft_expr_priv(expr); |
Taehee Yoo | 26b2f55 | 2018-07-13 01:38:08 +0900 | [diff] [blame] | 103 | struct nft_ctx *pctx = (struct nft_ctx *)ctx; |
Pablo Neira Ayuso | a654de8 | 2018-05-30 20:18:57 +0200 | [diff] [blame] | 104 | const struct nft_data *data; |
| 105 | int err; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 106 | |
Pablo Neira Ayuso | a654de8 | 2018-05-30 20:18:57 +0200 | [diff] [blame] | 107 | if (priv->dreg != NFT_REG_VERDICT) |
| 108 | return 0; |
| 109 | |
| 110 | data = &priv->data; |
| 111 | |
| 112 | switch (data->verdict.code) { |
| 113 | case NFT_JUMP: |
| 114 | case NFT_GOTO: |
Taehee Yoo | 26b2f55 | 2018-07-13 01:38:08 +0900 | [diff] [blame] | 115 | pctx->level++; |
Pablo Neira Ayuso | a654de8 | 2018-05-30 20:18:57 +0200 | [diff] [blame] | 116 | err = nft_chain_validate(ctx, data->verdict.chain); |
| 117 | if (err < 0) |
| 118 | return err; |
Taehee Yoo | 26b2f55 | 2018-07-13 01:38:08 +0900 | [diff] [blame] | 119 | pctx->level--; |
Pablo Neira Ayuso | a654de8 | 2018-05-30 20:18:57 +0200 | [diff] [blame] | 120 | break; |
| 121 | default: |
| 122 | break; |
| 123 | } |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 124 | |
| 125 | return 0; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Pablo Neira Ayuso | 43dd16e | 2019-08-01 14:09:26 +0200 | [diff] [blame] | 128 | static int nft_immediate_offload_verdict(struct nft_offload_ctx *ctx, |
| 129 | struct nft_flow_rule *flow, |
| 130 | const struct nft_immediate_expr *priv) |
Pablo Neira Ayuso | c9626a2 | 2019-07-09 23:00:43 +0200 | [diff] [blame] | 131 | { |
Pablo Neira Ayuso | c9626a2 | 2019-07-09 23:00:43 +0200 | [diff] [blame] | 132 | struct flow_action_entry *entry; |
| 133 | const struct nft_data *data; |
| 134 | |
Pablo Neira Ayuso | c9626a2 | 2019-07-09 23:00:43 +0200 | [diff] [blame] | 135 | entry = &flow->rule->action.entries[ctx->num_actions++]; |
| 136 | |
| 137 | data = &priv->data; |
| 138 | switch (data->verdict.code) { |
| 139 | case NF_ACCEPT: |
| 140 | entry->id = FLOW_ACTION_ACCEPT; |
| 141 | break; |
| 142 | case NF_DROP: |
| 143 | entry->id = FLOW_ACTION_DROP; |
| 144 | break; |
| 145 | default: |
| 146 | return -EOPNOTSUPP; |
| 147 | } |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
Pablo Neira Ayuso | 43dd16e | 2019-08-01 14:09:26 +0200 | [diff] [blame] | 152 | static int nft_immediate_offload(struct nft_offload_ctx *ctx, |
| 153 | struct nft_flow_rule *flow, |
| 154 | const struct nft_expr *expr) |
| 155 | { |
| 156 | const struct nft_immediate_expr *priv = nft_expr_priv(expr); |
| 157 | |
| 158 | if (priv->dreg == NFT_REG_VERDICT) |
| 159 | return nft_immediate_offload_verdict(ctx, flow, priv); |
| 160 | |
| 161 | memcpy(&ctx->regs[priv->dreg].data, &priv->data, sizeof(priv->data)); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 166 | static const struct nft_expr_ops nft_imm_ops = { |
| 167 | .type = &nft_imm_type, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 168 | .size = NFT_EXPR_SIZE(sizeof(struct nft_immediate_expr)), |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 169 | .eval = nft_immediate_eval, |
| 170 | .init = nft_immediate_init, |
Pablo Neira Ayuso | bb7b40a | 2018-05-08 02:43:57 +0200 | [diff] [blame] | 171 | .activate = nft_immediate_activate, |
| 172 | .deactivate = nft_immediate_deactivate, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 173 | .dump = nft_immediate_dump, |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 174 | .validate = nft_immediate_validate, |
Pablo Neira Ayuso | c9626a2 | 2019-07-09 23:00:43 +0200 | [diff] [blame] | 175 | .offload = nft_immediate_offload, |
| 176 | .offload_flags = NFT_OFFLOAD_F_ACTION, |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 177 | }; |
| 178 | |
Liping Zhang | 4e24877 | 2016-11-06 21:15:51 +0800 | [diff] [blame] | 179 | struct nft_expr_type nft_imm_type __read_mostly = { |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 180 | .name = "immediate", |
| 181 | .ops = &nft_imm_ops, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 182 | .policy = nft_immediate_policy, |
| 183 | .maxattr = NFTA_IMMEDIATE_MAX, |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 184 | .owner = THIS_MODULE, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 185 | }; |