Revert "ip6_vti: adjust vti mtu according to mtu of lower device"
[linux/fpc-iii.git] / net / netfilter / nft_quota.c
blobc00104c070958b8b3835274b8b99215828bdd126
1 /*
2 * Copyright (c) 2016 Pablo Neira Ayuso <pablo@netfilter.org>
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 */
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>
18 struct nft_quota {
19 u64 quota;
20 bool invert;
21 atomic64_t remain;
24 static inline bool nft_overquota(struct nft_quota *priv,
25 const struct nft_pktinfo *pkt)
27 return atomic64_sub_return(pkt->skb->len, &priv->remain) < 0;
30 static void nft_quota_eval(const struct nft_expr *expr,
31 struct nft_regs *regs,
32 const struct nft_pktinfo *pkt)
34 struct nft_quota *priv = nft_expr_priv(expr);
36 if (nft_overquota(priv, pkt) ^ priv->invert)
37 regs->verdict.code = NFT_BREAK;
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 },
45 static int nft_quota_init(const struct nft_ctx *ctx,
46 const struct nft_expr *expr,
47 const struct nlattr * const tb[])
49 struct nft_quota *priv = nft_expr_priv(expr);
50 u32 flags = 0;
51 u64 quota;
53 if (!tb[NFTA_QUOTA_BYTES])
54 return -EINVAL;
56 quota = be64_to_cpu(nla_get_be64(tb[NFTA_QUOTA_BYTES]));
57 if (quota > S64_MAX)
58 return -EOVERFLOW;
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;
66 priv->quota = quota;
67 priv->invert = (flags & NFT_QUOTA_F_INV) ? true : false;
68 atomic64_set(&priv->remain, quota);
70 return 0;
73 static int nft_quota_dump(struct sk_buff *skb, const struct nft_expr *expr)
75 const struct nft_quota *priv = nft_expr_priv(expr);
76 u32 flags = priv->invert ? NFT_QUOTA_F_INV : 0;
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;
84 nla_put_failure:
85 return -1;
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,
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,
106 static int __init nft_quota_module_init(void)
108 return nft_register_expr(&nft_quota_type);
111 static void __exit nft_quota_module_exit(void)
113 nft_unregister_expr(&nft_quota_type);
116 module_init(nft_quota_module_init);
117 module_exit(nft_quota_module_exit);
119 MODULE_LICENSE("GPL");
120 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
121 MODULE_ALIAS_NFT_EXPR("quota");