| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Copyright (c) 2015 Patrick McHardy <kaber@trash.net> |
| 4 | */ |
| 5 | |
| 6 | #include <linux/kernel.h> |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/netlink.h> |
| 10 | #include <linux/netfilter.h> |
| 11 | #include <linux/netfilter/nf_tables.h> |
| 12 | #include <net/netfilter/nf_tables.h> |
| 13 | #include <net/netfilter/nf_tables_core.h> |
| 14 | |
| 15 | struct nft_dynset { |
| 16 | struct nft_set *set; |
| 17 | struct nft_set_ext_tmpl tmpl; |
| 18 | enum nft_dynset_ops op:8; |
| 19 | enum nft_registers sreg_key:8; |
| 20 | enum nft_registers sreg_data:8; |
| 21 | bool invert; |
| 22 | u64 timeout; |
| 23 | struct nft_expr *expr; |
| 24 | struct nft_set_binding binding; |
| 25 | }; |
| 26 | |
| 27 | static int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src) |
| 28 | { |
| 29 | int err; |
| 30 | |
| 31 | if (src->ops->clone) { |
| 32 | dst->ops = src->ops; |
| 33 | err = src->ops->clone(dst, src); |
| 34 | if (err < 0) |
| 35 | return err; |
| 36 | } else { |
| 37 | memcpy(dst, src, src->ops->size); |
| 38 | } |
| 39 | |
| 40 | __module_get(src->ops->type->owner); |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | static void *nft_dynset_new(struct nft_set *set, const struct nft_expr *expr, |
| 45 | struct nft_regs *regs) |
| 46 | { |
| 47 | const struct nft_dynset *priv = nft_expr_priv(expr); |
| 48 | struct nft_set_ext *ext; |
| 49 | u64 timeout; |
| 50 | void *elem; |
| 51 | |
| 52 | if (!atomic_add_unless(&set->nelems, 1, set->size)) |
| 53 | return NULL; |
| 54 | |
| 55 | timeout = priv->timeout ? : set->timeout; |
| 56 | elem = nft_set_elem_init(set, &priv->tmpl, |
| 57 | ®s->data[priv->sreg_key], NULL, |
| 58 | ®s->data[priv->sreg_data], |
| 59 | timeout, 0, GFP_ATOMIC); |
| 60 | if (elem == NULL) |
| 61 | goto err1; |
| 62 | |
| 63 | ext = nft_set_elem_ext(set, elem); |
| 64 | if (priv->expr != NULL && |
| 65 | nft_expr_clone(nft_set_ext_expr(ext), priv->expr) < 0) |
| 66 | goto err2; |
| 67 | |
| 68 | return elem; |
| 69 | |
| 70 | err2: |
| 71 | nft_set_elem_destroy(set, elem, false); |
| 72 | err1: |
| 73 | if (set->size) |
| 74 | atomic_dec(&set->nelems); |
| 75 | return NULL; |
| 76 | } |
| 77 | |
| 78 | void nft_dynset_eval(const struct nft_expr *expr, |
| 79 | struct nft_regs *regs, const struct nft_pktinfo *pkt) |
| 80 | { |
| 81 | const struct nft_dynset *priv = nft_expr_priv(expr); |
| 82 | struct nft_set *set = priv->set; |
| 83 | const struct nft_set_ext *ext; |
| 84 | u64 timeout; |
| 85 | |
| 86 | if (priv->op == NFT_DYNSET_OP_DELETE) { |
| 87 | set->ops->delete(set, ®s->data[priv->sreg_key]); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | if (set->ops->update(set, ®s->data[priv->sreg_key], nft_dynset_new, |
| 92 | expr, regs, &ext)) { |
| 93 | if (priv->op == NFT_DYNSET_OP_UPDATE && |
| 94 | nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) { |
| 95 | timeout = priv->timeout ? : set->timeout; |
| 96 | *nft_set_ext_expiration(ext) = get_jiffies_64() + timeout; |
| 97 | } |
| 98 | |
| 99 | nft_set_elem_update_expr(ext, regs, pkt); |
| 100 | |
| 101 | if (priv->invert) |
| 102 | regs->verdict.code = NFT_BREAK; |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | if (!priv->invert) |
| 107 | regs->verdict.code = NFT_BREAK; |
| 108 | } |
| 109 | |
| 110 | static const struct nla_policy nft_dynset_policy[NFTA_DYNSET_MAX + 1] = { |
| 111 | [NFTA_DYNSET_SET_NAME] = { .type = NLA_STRING, |
| 112 | .len = NFT_SET_MAXNAMELEN - 1 }, |
| 113 | [NFTA_DYNSET_SET_ID] = { .type = NLA_U32 }, |
| 114 | [NFTA_DYNSET_OP] = { .type = NLA_U32 }, |
| 115 | [NFTA_DYNSET_SREG_KEY] = { .type = NLA_U32 }, |
| 116 | [NFTA_DYNSET_SREG_DATA] = { .type = NLA_U32 }, |
| 117 | [NFTA_DYNSET_TIMEOUT] = { .type = NLA_U64 }, |
| 118 | [NFTA_DYNSET_EXPR] = { .type = NLA_NESTED }, |
| 119 | [NFTA_DYNSET_FLAGS] = { .type = NLA_U32 }, |
| 120 | }; |
| 121 | |
| 122 | static int nft_dynset_init(const struct nft_ctx *ctx, |
| 123 | const struct nft_expr *expr, |
| 124 | const struct nlattr * const tb[]) |
| 125 | { |
| 126 | struct nft_dynset *priv = nft_expr_priv(expr); |
| 127 | u8 genmask = nft_genmask_next(ctx->net); |
| 128 | struct nft_set *set; |
| 129 | u64 timeout; |
| 130 | int err; |
| 131 | |
| 132 | lockdep_assert_held(&ctx->net->nft.commit_mutex); |
| 133 | |
| 134 | if (tb[NFTA_DYNSET_SET_NAME] == NULL || |
| 135 | tb[NFTA_DYNSET_OP] == NULL || |
| 136 | tb[NFTA_DYNSET_SREG_KEY] == NULL) |
| 137 | return -EINVAL; |
| 138 | |
| 139 | if (tb[NFTA_DYNSET_FLAGS]) { |
| 140 | u32 flags = ntohl(nla_get_be32(tb[NFTA_DYNSET_FLAGS])); |
| 141 | |
| 142 | if (flags & ~NFT_DYNSET_F_INV) |
| 143 | return -EINVAL; |
| 144 | if (flags & NFT_DYNSET_F_INV) |
| 145 | priv->invert = true; |
| 146 | } |
| 147 | |
| 148 | set = nft_set_lookup_global(ctx->net, ctx->table, |
| 149 | tb[NFTA_DYNSET_SET_NAME], |
| 150 | tb[NFTA_DYNSET_SET_ID], genmask); |
| 151 | if (IS_ERR(set)) |
| 152 | return PTR_ERR(set); |
| 153 | |
| 154 | if (set->ops->update == NULL) |
| 155 | return -EOPNOTSUPP; |
| 156 | |
| 157 | if (set->flags & NFT_SET_CONSTANT) |
| 158 | return -EBUSY; |
| 159 | |
| 160 | priv->op = ntohl(nla_get_be32(tb[NFTA_DYNSET_OP])); |
| 161 | switch (priv->op) { |
| 162 | case NFT_DYNSET_OP_ADD: |
| 163 | case NFT_DYNSET_OP_DELETE: |
| 164 | break; |
| 165 | case NFT_DYNSET_OP_UPDATE: |
| 166 | if (!(set->flags & NFT_SET_TIMEOUT)) |
| 167 | return -EOPNOTSUPP; |
| 168 | break; |
| 169 | default: |
| 170 | return -EOPNOTSUPP; |
| 171 | } |
| 172 | |
| 173 | timeout = 0; |
| 174 | if (tb[NFTA_DYNSET_TIMEOUT] != NULL) { |
| 175 | if (!(set->flags & NFT_SET_TIMEOUT)) |
| 176 | return -EINVAL; |
| 177 | timeout = msecs_to_jiffies(be64_to_cpu(nla_get_be64( |
| 178 | tb[NFTA_DYNSET_TIMEOUT]))); |
| 179 | } |
| 180 | |
| 181 | priv->sreg_key = nft_parse_register(tb[NFTA_DYNSET_SREG_KEY]); |
| 182 | err = nft_validate_register_load(priv->sreg_key, set->klen); |
| 183 | if (err < 0) |
| 184 | return err; |
| 185 | |
| 186 | if (tb[NFTA_DYNSET_SREG_DATA] != NULL) { |
| 187 | if (!(set->flags & NFT_SET_MAP)) |
| 188 | return -EINVAL; |
| 189 | if (set->dtype == NFT_DATA_VERDICT) |
| 190 | return -EOPNOTSUPP; |
| 191 | |
| 192 | priv->sreg_data = nft_parse_register(tb[NFTA_DYNSET_SREG_DATA]); |
| 193 | err = nft_validate_register_load(priv->sreg_data, set->dlen); |
| 194 | if (err < 0) |
| 195 | return err; |
| 196 | } else if (set->flags & NFT_SET_MAP) |
| 197 | return -EINVAL; |
| 198 | |
| 199 | if (tb[NFTA_DYNSET_EXPR] != NULL) { |
| 200 | if (!(set->flags & NFT_SET_EVAL)) |
| 201 | return -EINVAL; |
| 202 | |
| 203 | priv->expr = nft_set_elem_expr_alloc(ctx, set, |
| 204 | tb[NFTA_DYNSET_EXPR]); |
| 205 | if (IS_ERR(priv->expr)) |
| 206 | return PTR_ERR(priv->expr); |
| 207 | } |
| 208 | |
| 209 | nft_set_ext_prepare(&priv->tmpl); |
| 210 | nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_KEY, set->klen); |
| 211 | if (set->flags & NFT_SET_MAP) |
| 212 | nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_DATA, set->dlen); |
| 213 | if (priv->expr != NULL) |
| 214 | nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_EXPR, |
| 215 | priv->expr->ops->size); |
| 216 | if (set->flags & NFT_SET_TIMEOUT) { |
| 217 | if (timeout || set->timeout) |
| 218 | nft_set_ext_add(&priv->tmpl, NFT_SET_EXT_EXPIRATION); |
| 219 | } |
| 220 | |
| 221 | priv->timeout = timeout; |
| 222 | |
| 223 | err = nf_tables_bind_set(ctx, set, &priv->binding); |
| 224 | if (err < 0) |
| 225 | goto err1; |
| 226 | |
| 227 | if (set->size == 0) |
| 228 | set->size = 0xffff; |
| 229 | |
| 230 | priv->set = set; |
| 231 | return 0; |
| 232 | |
| 233 | err1: |
| 234 | if (priv->expr != NULL) |
| 235 | nft_expr_destroy(ctx, priv->expr); |
| 236 | return err; |
| 237 | } |
| 238 | |
| 239 | static void nft_dynset_deactivate(const struct nft_ctx *ctx, |
| 240 | const struct nft_expr *expr, |
| 241 | enum nft_trans_phase phase) |
| 242 | { |
| 243 | struct nft_dynset *priv = nft_expr_priv(expr); |
| 244 | |
| 245 | nf_tables_deactivate_set(ctx, priv->set, &priv->binding, phase); |
| 246 | } |
| 247 | |
| 248 | static void nft_dynset_activate(const struct nft_ctx *ctx, |
| 249 | const struct nft_expr *expr) |
| 250 | { |
| 251 | struct nft_dynset *priv = nft_expr_priv(expr); |
| 252 | |
| 253 | priv->set->use++; |
| 254 | } |
| 255 | |
| 256 | static void nft_dynset_destroy(const struct nft_ctx *ctx, |
| 257 | const struct nft_expr *expr) |
| 258 | { |
| 259 | struct nft_dynset *priv = nft_expr_priv(expr); |
| 260 | |
| 261 | if (priv->expr != NULL) |
| 262 | nft_expr_destroy(ctx, priv->expr); |
| 263 | |
| 264 | nf_tables_destroy_set(ctx, priv->set); |
| 265 | } |
| 266 | |
| 267 | static int nft_dynset_dump(struct sk_buff *skb, const struct nft_expr *expr) |
| 268 | { |
| 269 | const struct nft_dynset *priv = nft_expr_priv(expr); |
| 270 | u32 flags = priv->invert ? NFT_DYNSET_F_INV : 0; |
| 271 | |
| 272 | if (nft_dump_register(skb, NFTA_DYNSET_SREG_KEY, priv->sreg_key)) |
| 273 | goto nla_put_failure; |
| 274 | if (priv->set->flags & NFT_SET_MAP && |
| 275 | nft_dump_register(skb, NFTA_DYNSET_SREG_DATA, priv->sreg_data)) |
| 276 | goto nla_put_failure; |
| 277 | if (nla_put_be32(skb, NFTA_DYNSET_OP, htonl(priv->op))) |
| 278 | goto nla_put_failure; |
| 279 | if (nla_put_string(skb, NFTA_DYNSET_SET_NAME, priv->set->name)) |
| 280 | goto nla_put_failure; |
| 281 | if (nla_put_be64(skb, NFTA_DYNSET_TIMEOUT, |
| 282 | cpu_to_be64(jiffies_to_msecs(priv->timeout)), |
| 283 | NFTA_DYNSET_PAD)) |
| 284 | goto nla_put_failure; |
| 285 | if (priv->expr && nft_expr_dump(skb, NFTA_DYNSET_EXPR, priv->expr)) |
| 286 | goto nla_put_failure; |
| 287 | if (nla_put_be32(skb, NFTA_DYNSET_FLAGS, htonl(flags))) |
| 288 | goto nla_put_failure; |
| 289 | return 0; |
| 290 | |
| 291 | nla_put_failure: |
| 292 | return -1; |
| 293 | } |
| 294 | |
| 295 | static const struct nft_expr_ops nft_dynset_ops = { |
| 296 | .type = &nft_dynset_type, |
| 297 | .size = NFT_EXPR_SIZE(sizeof(struct nft_dynset)), |
| 298 | .eval = nft_dynset_eval, |
| 299 | .init = nft_dynset_init, |
| 300 | .destroy = nft_dynset_destroy, |
| 301 | .activate = nft_dynset_activate, |
| 302 | .deactivate = nft_dynset_deactivate, |
| 303 | .dump = nft_dynset_dump, |
| 304 | }; |
| 305 | |
| 306 | struct nft_expr_type nft_dynset_type __read_mostly = { |
| 307 | .name = "dynset", |
| 308 | .ops = &nft_dynset_ops, |
| 309 | .policy = nft_dynset_policy, |
| 310 | .maxattr = NFTA_DYNSET_MAX, |
| 311 | .owner = THIS_MODULE, |
| 312 | }; |