Pablo Neira Ayuso | 3d2f30a | 2016-08-18 01:46:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 Pablo Neira Ayuso <pablo@netfilter.org> |
| 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 | |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/atomic.h> |
| 13 | #include <linux/netlink.h> |
| 14 | #include <linux/netfilter.h> |
| 15 | #include <linux/netfilter/nf_tables.h> |
| 16 | #include <net/netfilter/nf_tables.h> |
| 17 | |
| 18 | struct nft_quota { |
| 19 | u64 quota; |
| 20 | bool invert; |
| 21 | atomic64_t remain; |
| 22 | }; |
| 23 | |
Pablo Neira Ayuso | 22609b4 | 2016-09-02 21:00:59 +0200 | [diff] [blame] | 24 | static inline bool nft_overquota(struct nft_quota *priv, |
| 25 | const struct nft_pktinfo *pkt) |
Pablo Neira Ayuso | 3d2f30a | 2016-08-18 01:46:06 +0200 | [diff] [blame] | 26 | { |
Pablo Neira Ayuso | 22609b4 | 2016-09-02 21:00:59 +0200 | [diff] [blame] | 27 | return atomic64_sub_return(pkt->skb->len, &priv->remain) < 0; |
Pablo Neira Ayuso | 3d2f30a | 2016-08-18 01:46:06 +0200 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | static void nft_quota_eval(const struct nft_expr *expr, |
| 31 | struct nft_regs *regs, |
| 32 | const struct nft_pktinfo *pkt) |
| 33 | { |
| 34 | struct nft_quota *priv = nft_expr_priv(expr); |
| 35 | |
Pablo Neira Ayuso | 22609b4 | 2016-09-02 21:00:59 +0200 | [diff] [blame] | 36 | if (nft_overquota(priv, pkt) ^ priv->invert) |
Pablo Neira Ayuso | 3d2f30a | 2016-08-18 01:46:06 +0200 | [diff] [blame] | 37 | regs->verdict.code = NFT_BREAK; |
| 38 | } |
| 39 | |
| 40 | static const struct nla_policy nft_quota_policy[NFTA_QUOTA_MAX + 1] = { |
| 41 | [NFTA_QUOTA_BYTES] = { .type = NLA_U64 }, |
| 42 | [NFTA_QUOTA_FLAGS] = { .type = NLA_U32 }, |
| 43 | }; |
| 44 | |
| 45 | static int nft_quota_init(const struct nft_ctx *ctx, |
| 46 | const struct nft_expr *expr, |
| 47 | const struct nlattr * const tb[]) |
| 48 | { |
| 49 | struct nft_quota *priv = nft_expr_priv(expr); |
| 50 | u32 flags = 0; |
| 51 | u64 quota; |
| 52 | |
| 53 | if (!tb[NFTA_QUOTA_BYTES]) |
| 54 | return -EINVAL; |
| 55 | |
| 56 | quota = be64_to_cpu(nla_get_be64(tb[NFTA_QUOTA_BYTES])); |
| 57 | if (quota > S64_MAX) |
| 58 | return -EOVERFLOW; |
| 59 | |
| 60 | if (tb[NFTA_QUOTA_FLAGS]) { |
| 61 | flags = ntohl(nla_get_be32(tb[NFTA_QUOTA_FLAGS])); |
| 62 | if (flags & ~NFT_QUOTA_F_INV) |
| 63 | return -EINVAL; |
| 64 | } |
| 65 | |
| 66 | priv->quota = quota; |
| 67 | priv->invert = (flags & NFT_QUOTA_F_INV) ? true : false; |
| 68 | atomic64_set(&priv->remain, quota); |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static int nft_quota_dump(struct sk_buff *skb, const struct nft_expr *expr) |
| 74 | { |
| 75 | const struct nft_quota *priv = nft_expr_priv(expr); |
| 76 | u32 flags = priv->invert ? NFT_QUOTA_F_INV : 0; |
| 77 | |
| 78 | if (nla_put_be64(skb, NFTA_QUOTA_BYTES, cpu_to_be64(priv->quota), |
| 79 | NFTA_QUOTA_PAD) || |
| 80 | nla_put_be32(skb, NFTA_QUOTA_FLAGS, htonl(flags))) |
| 81 | goto nla_put_failure; |
| 82 | return 0; |
| 83 | |
| 84 | nla_put_failure: |
| 85 | return -1; |
| 86 | } |
| 87 | |
| 88 | static struct nft_expr_type nft_quota_type; |
| 89 | static const struct nft_expr_ops nft_quota_ops = { |
| 90 | .type = &nft_quota_type, |
| 91 | .size = NFT_EXPR_SIZE(sizeof(struct nft_quota)), |
| 92 | .eval = nft_quota_eval, |
| 93 | .init = nft_quota_init, |
| 94 | .dump = nft_quota_dump, |
| 95 | }; |
| 96 | |
| 97 | static struct nft_expr_type nft_quota_type __read_mostly = { |
| 98 | .name = "quota", |
| 99 | .ops = &nft_quota_ops, |
| 100 | .policy = nft_quota_policy, |
| 101 | .maxattr = NFTA_QUOTA_MAX, |
| 102 | .flags = NFT_EXPR_STATEFUL, |
| 103 | .owner = THIS_MODULE, |
| 104 | }; |
| 105 | |
| 106 | static int __init nft_quota_module_init(void) |
| 107 | { |
| 108 | return nft_register_expr(&nft_quota_type); |
| 109 | } |
| 110 | |
| 111 | static void __exit nft_quota_module_exit(void) |
| 112 | { |
| 113 | nft_unregister_expr(&nft_quota_type); |
| 114 | } |
| 115 | |
| 116 | module_init(nft_quota_module_init); |
| 117 | module_exit(nft_quota_module_exit); |
| 118 | |
| 119 | MODULE_LICENSE("GPL"); |
| 120 | MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>"); |
| 121 | MODULE_ALIAS_NFT_EXPR("quota"); |