blob: 8e6d7eaf9dc8b5c783b1a7591ff21608e41dcdaf [file] [log] [blame]
Pablo Neira Ayuso836382d2021-06-16 22:25:05 +02001// SPDX-License-Identifier: GPL-2.0-only
2#include <linux/kernel.h>
3#include <linux/init.h>
4#include <linux/module.h>
5#include <linux/netlink.h>
6#include <linux/netfilter.h>
7#include <linux/netfilter/nf_tables.h>
8#include <net/netfilter/nf_tables_core.h>
9#include <net/netfilter/nf_tables.h>
10
Pablo Neira Ayuso33a24de2022-01-09 17:11:14 +010011struct nft_last {
12 unsigned long jiffies;
13 unsigned int set;
14};
15
Pablo Neira Ayuso836382d2021-06-16 22:25:05 +020016struct nft_last_priv {
Pablo Neira Ayuso33a24de2022-01-09 17:11:14 +010017 struct nft_last *last;
Pablo Neira Ayuso836382d2021-06-16 22:25:05 +020018};
19
20static const struct nla_policy nft_last_policy[NFTA_LAST_MAX + 1] = {
21 [NFTA_LAST_SET] = { .type = NLA_U32 },
22 [NFTA_LAST_MSECS] = { .type = NLA_U64 },
23};
24
25static int nft_last_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
26 const struct nlattr * const tb[])
27{
28 struct nft_last_priv *priv = nft_expr_priv(expr);
Pablo Neira Ayuso33a24de2022-01-09 17:11:14 +010029 struct nft_last *last;
Pablo Neira Ayuso836382d2021-06-16 22:25:05 +020030 u64 last_jiffies;
31 int err;
32
Vasily Averin42193ff2022-04-02 12:50:37 +030033 last = kzalloc(sizeof(*last), GFP_KERNEL_ACCOUNT);
Pablo Neira Ayuso33a24de2022-01-09 17:11:14 +010034 if (!last)
35 return -ENOMEM;
Pablo Neira Ayuso6ac4bac2021-07-05 17:45:36 +020036
Pablo Neira Ayuso33a24de2022-01-09 17:11:14 +010037 if (tb[NFTA_LAST_SET])
38 last->set = ntohl(nla_get_be32(tb[NFTA_LAST_SET]));
39
40 if (last->set && tb[NFTA_LAST_MSECS]) {
Pablo Neira Ayuso836382d2021-06-16 22:25:05 +020041 err = nf_msecs_to_jiffies64(tb[NFTA_LAST_MSECS], &last_jiffies);
42 if (err < 0)
Pablo Neira Ayuso33a24de2022-01-09 17:11:14 +010043 goto err;
Pablo Neira Ayuso836382d2021-06-16 22:25:05 +020044
Pablo Neira Ayuso33a24de2022-01-09 17:11:14 +010045 last->jiffies = jiffies - (unsigned long)last_jiffies;
Pablo Neira Ayuso836382d2021-06-16 22:25:05 +020046 }
Pablo Neira Ayuso33a24de2022-01-09 17:11:14 +010047 priv->last = last;
Pablo Neira Ayuso836382d2021-06-16 22:25:05 +020048
49 return 0;
Pablo Neira Ayuso33a24de2022-01-09 17:11:14 +010050err:
51 kfree(last);
52
53 return err;
Pablo Neira Ayuso836382d2021-06-16 22:25:05 +020054}
55
56static void nft_last_eval(const struct nft_expr *expr,
57 struct nft_regs *regs, const struct nft_pktinfo *pkt)
58{
59 struct nft_last_priv *priv = nft_expr_priv(expr);
Pablo Neira Ayuso33a24de2022-01-09 17:11:14 +010060 struct nft_last *last = priv->last;
Pablo Neira Ayuso836382d2021-06-16 22:25:05 +020061
Pablo Neira Ayuso33a24de2022-01-09 17:11:14 +010062 if (READ_ONCE(last->jiffies) != jiffies)
63 WRITE_ONCE(last->jiffies, jiffies);
64 if (READ_ONCE(last->set) == 0)
65 WRITE_ONCE(last->set, 1);
Pablo Neira Ayuso836382d2021-06-16 22:25:05 +020066}
67
Phil Sutter7d34aa32022-10-14 23:45:58 +020068static int nft_last_dump(struct sk_buff *skb,
69 const struct nft_expr *expr, bool reset)
Pablo Neira Ayuso836382d2021-06-16 22:25:05 +020070{
71 struct nft_last_priv *priv = nft_expr_priv(expr);
Pablo Neira Ayuso33a24de2022-01-09 17:11:14 +010072 struct nft_last *last = priv->last;
73 unsigned long last_jiffies = READ_ONCE(last->jiffies);
74 u32 last_set = READ_ONCE(last->set);
Pablo Neira Ayuso836382d2021-06-16 22:25:05 +020075 __be64 msecs;
76
Pablo Neira Ayuso32953df2021-07-17 10:20:08 +020077 if (time_before(jiffies, last_jiffies)) {
Pablo Neira Ayuso33a24de2022-01-09 17:11:14 +010078 WRITE_ONCE(last->set, 0);
Pablo Neira Ayuso32953df2021-07-17 10:20:08 +020079 last_set = 0;
80 }
Pablo Neira Ayuso836382d2021-06-16 22:25:05 +020081
Pablo Neira Ayuso32953df2021-07-17 10:20:08 +020082 if (last_set)
83 msecs = nf_jiffies64_to_msecs(jiffies - last_jiffies);
Pablo Neira Ayuso836382d2021-06-16 22:25:05 +020084 else
85 msecs = 0;
86
Pablo Neira Ayuso32953df2021-07-17 10:20:08 +020087 if (nla_put_be32(skb, NFTA_LAST_SET, htonl(last_set)) ||
Pablo Neira Ayuso836382d2021-06-16 22:25:05 +020088 nla_put_be64(skb, NFTA_LAST_MSECS, msecs, NFTA_LAST_PAD))
89 goto nla_put_failure;
90
91 return 0;
92
93nla_put_failure:
94 return -1;
95}
96
Pablo Neira Ayuso33a24de2022-01-09 17:11:14 +010097static void nft_last_destroy(const struct nft_ctx *ctx,
98 const struct nft_expr *expr)
99{
100 struct nft_last_priv *priv = nft_expr_priv(expr);
101
102 kfree(priv->last);
103}
104
105static int nft_last_clone(struct nft_expr *dst, const struct nft_expr *src)
106{
107 struct nft_last_priv *priv_dst = nft_expr_priv(dst);
Pablo Neira Ayuso860e8742023-02-28 17:09:03 +0100108 struct nft_last_priv *priv_src = nft_expr_priv(src);
Pablo Neira Ayuso33a24de2022-01-09 17:11:14 +0100109
110 priv_dst->last = kzalloc(sizeof(*priv_dst->last), GFP_ATOMIC);
Pablo Neira Ayuso51edb2f2022-01-10 20:48:17 +0100111 if (!priv_dst->last)
Pablo Neira Ayuso33a24de2022-01-09 17:11:14 +0100112 return -ENOMEM;
113
Pablo Neira Ayuso860e8742023-02-28 17:09:03 +0100114 priv_dst->last->set = priv_src->last->set;
115 priv_dst->last->jiffies = priv_src->last->jiffies;
116
Pablo Neira Ayuso33a24de2022-01-09 17:11:14 +0100117 return 0;
118}
119
Pablo Neira Ayuso836382d2021-06-16 22:25:05 +0200120static const struct nft_expr_ops nft_last_ops = {
121 .type = &nft_last_type,
122 .size = NFT_EXPR_SIZE(sizeof(struct nft_last_priv)),
123 .eval = nft_last_eval,
124 .init = nft_last_init,
Pablo Neira Ayuso33a24de2022-01-09 17:11:14 +0100125 .destroy = nft_last_destroy,
126 .clone = nft_last_clone,
Pablo Neira Ayuso836382d2021-06-16 22:25:05 +0200127 .dump = nft_last_dump,
Pablo Neira Ayusob2d30652022-03-14 18:23:00 +0100128 .reduce = NFT_REDUCE_READONLY,
Pablo Neira Ayuso836382d2021-06-16 22:25:05 +0200129};
130
131struct nft_expr_type nft_last_type __read_mostly = {
132 .name = "last",
133 .ops = &nft_last_ops,
134 .policy = nft_last_policy,
135 .maxattr = NFTA_LAST_MAX,
136 .flags = NFT_EXPR_STATEFUL,
137 .owner = THIS_MODULE,
138};