Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2009 Patrick McHardy <kaber@trash.net> |
| 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/list.h> |
| 14 | #include <linux/rbtree.h> |
| 15 | #include <linux/netlink.h> |
| 16 | #include <linux/netfilter.h> |
| 17 | #include <linux/netfilter/nf_tables.h> |
| 18 | #include <net/netfilter/nf_tables.h> |
Rashika Kheria | bd76ed3 | 2014-02-09 22:31:42 +0530 | [diff] [blame] | 19 | #include <net/netfilter/nf_tables_core.h> |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 20 | |
| 21 | struct nft_lookup { |
| 22 | struct nft_set *set; |
| 23 | enum nft_registers sreg:8; |
| 24 | enum nft_registers dreg:8; |
Arturo Borrero | 0071e184 | 2016-06-23 12:24:08 +0200 | [diff] [blame] | 25 | bool invert; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 26 | struct nft_set_binding binding; |
| 27 | }; |
| 28 | |
| 29 | static void nft_lookup_eval(const struct nft_expr *expr, |
Patrick McHardy | a55e22e | 2015-04-11 02:27:31 +0100 | [diff] [blame] | 30 | struct nft_regs *regs, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 31 | const struct nft_pktinfo *pkt) |
| 32 | { |
| 33 | const struct nft_lookup *priv = nft_expr_priv(expr); |
| 34 | const struct nft_set *set = priv->set; |
Patrick McHardy | b2832dd | 2015-03-25 14:08:48 +0000 | [diff] [blame] | 35 | const struct nft_set_ext *ext; |
Arturo Borrero | 0071e184 | 2016-06-23 12:24:08 +0200 | [diff] [blame] | 36 | bool found; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 37 | |
Pablo Neira Ayuso | 0e5a1c7 | 2016-11-03 10:56:26 +0100 | [diff] [blame] | 38 | found = set->ops->lookup(nft_net(pkt), set, ®s->data[priv->sreg], |
| 39 | &ext) ^ priv->invert; |
Arturo Borrero | 0071e184 | 2016-06-23 12:24:08 +0200 | [diff] [blame] | 40 | if (!found) { |
| 41 | regs->verdict.code = NFT_BREAK; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 42 | return; |
Patrick McHardy | b2832dd | 2015-03-25 14:08:48 +0000 | [diff] [blame] | 43 | } |
Arturo Borrero | 0071e184 | 2016-06-23 12:24:08 +0200 | [diff] [blame] | 44 | |
Pablo Neira Ayuso | 4004d5c | 2016-09-20 18:22:46 +0200 | [diff] [blame] | 45 | if (set->flags & NFT_SET_MAP) |
Arturo Borrero | 0071e184 | 2016-06-23 12:24:08 +0200 | [diff] [blame] | 46 | nft_data_copy(®s->data[priv->dreg], |
| 47 | nft_set_ext_data(ext), set->dlen); |
| 48 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static const struct nla_policy nft_lookup_policy[NFTA_LOOKUP_MAX + 1] = { |
Liping Zhang | b2fbd04 | 2017-01-20 21:03:03 +0800 | [diff] [blame] | 52 | [NFTA_LOOKUP_SET] = { .type = NLA_STRING, |
| 53 | .len = NFT_SET_MAXNAMELEN - 1 }, |
Patrick McHardy | 4c1017a | 2015-01-30 07:46:33 +0000 | [diff] [blame] | 54 | [NFTA_LOOKUP_SET_ID] = { .type = NLA_U32 }, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 55 | [NFTA_LOOKUP_SREG] = { .type = NLA_U32 }, |
| 56 | [NFTA_LOOKUP_DREG] = { .type = NLA_U32 }, |
Arturo Borrero | 0071e184 | 2016-06-23 12:24:08 +0200 | [diff] [blame] | 57 | [NFTA_LOOKUP_FLAGS] = { .type = NLA_U32 }, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | static int nft_lookup_init(const struct nft_ctx *ctx, |
| 61 | const struct nft_expr *expr, |
| 62 | const struct nlattr * const tb[]) |
| 63 | { |
| 64 | struct nft_lookup *priv = nft_expr_priv(expr); |
Pablo Neira Ayuso | 37a9cc5 | 2016-06-12 22:52:45 +0200 | [diff] [blame] | 65 | u8 genmask = nft_genmask_next(ctx->net); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 66 | struct nft_set *set; |
Arturo Borrero | 0071e184 | 2016-06-23 12:24:08 +0200 | [diff] [blame] | 67 | u32 flags; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 68 | int err; |
| 69 | |
| 70 | if (tb[NFTA_LOOKUP_SET] == NULL || |
| 71 | tb[NFTA_LOOKUP_SREG] == NULL) |
| 72 | return -EINVAL; |
| 73 | |
Pablo Neira Ayuso | 37a9cc5 | 2016-06-12 22:52:45 +0200 | [diff] [blame] | 74 | set = nf_tables_set_lookup(ctx->table, tb[NFTA_LOOKUP_SET], genmask); |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 75 | if (IS_ERR(set)) { |
| 76 | if (tb[NFTA_LOOKUP_SET_ID]) { |
| 77 | set = nf_tables_set_lookup_byid(ctx->net, |
Pablo Neira Ayuso | 37a9cc5 | 2016-06-12 22:52:45 +0200 | [diff] [blame] | 78 | tb[NFTA_LOOKUP_SET_ID], |
| 79 | genmask); |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 80 | } |
| 81 | if (IS_ERR(set)) |
| 82 | return PTR_ERR(set); |
| 83 | } |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 84 | |
Patrick McHardy | 7c6c6e9 | 2015-04-11 10:46:41 +0100 | [diff] [blame] | 85 | if (set->flags & NFT_SET_EVAL) |
| 86 | return -EOPNOTSUPP; |
| 87 | |
Patrick McHardy | b1c96ed | 2015-04-11 02:27:36 +0100 | [diff] [blame] | 88 | priv->sreg = nft_parse_register(tb[NFTA_LOOKUP_SREG]); |
Patrick McHardy | d07db98 | 2015-04-11 02:27:30 +0100 | [diff] [blame] | 89 | err = nft_validate_register_load(priv->sreg, set->klen); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 90 | if (err < 0) |
| 91 | return err; |
| 92 | |
Arturo Borrero | 0071e184 | 2016-06-23 12:24:08 +0200 | [diff] [blame] | 93 | if (tb[NFTA_LOOKUP_FLAGS]) { |
| 94 | flags = ntohl(nla_get_be32(tb[NFTA_LOOKUP_FLAGS])); |
| 95 | |
| 96 | if (flags & ~NFT_LOOKUP_F_INV) |
| 97 | return -EINVAL; |
| 98 | |
| 99 | if (flags & NFT_LOOKUP_F_INV) { |
| 100 | if (set->flags & NFT_SET_MAP) |
| 101 | return -EINVAL; |
| 102 | priv->invert = true; |
| 103 | } |
| 104 | } |
| 105 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 106 | if (tb[NFTA_LOOKUP_DREG] != NULL) { |
Arturo Borrero | 0071e184 | 2016-06-23 12:24:08 +0200 | [diff] [blame] | 107 | if (priv->invert) |
| 108 | return -EINVAL; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 109 | if (!(set->flags & NFT_SET_MAP)) |
| 110 | return -EINVAL; |
| 111 | |
Patrick McHardy | b1c96ed | 2015-04-11 02:27:36 +0100 | [diff] [blame] | 112 | priv->dreg = nft_parse_register(tb[NFTA_LOOKUP_DREG]); |
Patrick McHardy | 58f40ab | 2015-04-11 02:27:28 +0100 | [diff] [blame] | 113 | err = nft_validate_register_store(ctx, priv->dreg, NULL, |
| 114 | set->dtype, set->dlen); |
| 115 | if (err < 0) |
| 116 | return err; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 117 | } else if (set->flags & NFT_SET_MAP) |
| 118 | return -EINVAL; |
| 119 | |
Patrick McHardy | 11113e1 | 2015-04-05 14:41:07 +0200 | [diff] [blame] | 120 | priv->binding.flags = set->flags & NFT_SET_MAP; |
| 121 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 122 | err = nf_tables_bind_set(ctx, set, &priv->binding); |
| 123 | if (err < 0) |
| 124 | return err; |
| 125 | |
| 126 | priv->set = set; |
| 127 | return 0; |
| 128 | } |
| 129 | |
Patrick McHardy | 62472bc | 2014-03-07 19:08:30 +0100 | [diff] [blame] | 130 | static void nft_lookup_destroy(const struct nft_ctx *ctx, |
| 131 | const struct nft_expr *expr) |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 132 | { |
| 133 | struct nft_lookup *priv = nft_expr_priv(expr); |
| 134 | |
Patrick McHardy | ab9da5c1 | 2014-03-07 19:08:31 +0100 | [diff] [blame] | 135 | nf_tables_unbind_set(ctx, priv->set, &priv->binding); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static int nft_lookup_dump(struct sk_buff *skb, const struct nft_expr *expr) |
| 139 | { |
| 140 | const struct nft_lookup *priv = nft_expr_priv(expr); |
Arturo Borrero | 0071e184 | 2016-06-23 12:24:08 +0200 | [diff] [blame] | 141 | u32 flags = priv->invert ? NFT_LOOKUP_F_INV : 0; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 142 | |
| 143 | if (nla_put_string(skb, NFTA_LOOKUP_SET, priv->set->name)) |
| 144 | goto nla_put_failure; |
Patrick McHardy | b1c96ed | 2015-04-11 02:27:36 +0100 | [diff] [blame] | 145 | if (nft_dump_register(skb, NFTA_LOOKUP_SREG, priv->sreg)) |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 146 | goto nla_put_failure; |
| 147 | if (priv->set->flags & NFT_SET_MAP) |
Patrick McHardy | b1c96ed | 2015-04-11 02:27:36 +0100 | [diff] [blame] | 148 | if (nft_dump_register(skb, NFTA_LOOKUP_DREG, priv->dreg)) |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 149 | goto nla_put_failure; |
Arturo Borrero | 0071e184 | 2016-06-23 12:24:08 +0200 | [diff] [blame] | 150 | if (nla_put_be32(skb, NFTA_LOOKUP_FLAGS, htonl(flags))) |
| 151 | goto nla_put_failure; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 152 | return 0; |
| 153 | |
| 154 | nla_put_failure: |
| 155 | return -1; |
| 156 | } |
| 157 | |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 158 | static const struct nft_expr_ops nft_lookup_ops = { |
| 159 | .type = &nft_lookup_type, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 160 | .size = NFT_EXPR_SIZE(sizeof(struct nft_lookup)), |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 161 | .eval = nft_lookup_eval, |
| 162 | .init = nft_lookup_init, |
| 163 | .destroy = nft_lookup_destroy, |
| 164 | .dump = nft_lookup_dump, |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 165 | }; |
| 166 | |
Liping Zhang | 4e24877 | 2016-11-06 21:15:51 +0800 | [diff] [blame] | 167 | struct nft_expr_type nft_lookup_type __read_mostly = { |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 168 | .name = "lookup", |
| 169 | .ops = &nft_lookup_ops, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 170 | .policy = nft_lookup_policy, |
| 171 | .maxattr = NFTA_LOOKUP_MAX, |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 172 | .owner = THIS_MODULE, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 173 | }; |