1 // 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>
11 struct nft_last_priv {
12 unsigned long last_jiffies;
13 unsigned int last_set;
16 static const struct nla_policy nft_last_policy[NFTA_LAST_MAX + 1] = {
17 [NFTA_LAST_SET] = { .type = NLA_U32 },
18 [NFTA_LAST_MSECS] = { .type = NLA_U64 },
21 static int nft_last_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
22 const struct nlattr * const tb[])
24 struct nft_last_priv *priv = nft_expr_priv(expr);
29 if (tb[NFTA_LAST_SET]) {
30 last_set = ntohl(nla_get_be32(tb[NFTA_LAST_SET]));
35 if (last_set && tb[NFTA_LAST_MSECS]) {
36 err = nf_msecs_to_jiffies64(tb[NFTA_LAST_MSECS], &last_jiffies);
40 priv->last_jiffies = jiffies - (unsigned long)last_jiffies;
46 static void nft_last_eval(const struct nft_expr *expr,
47 struct nft_regs *regs, const struct nft_pktinfo *pkt)
49 struct nft_last_priv *priv = nft_expr_priv(expr);
51 if (READ_ONCE(priv->last_jiffies) != jiffies)
52 WRITE_ONCE(priv->last_jiffies, jiffies);
53 if (READ_ONCE(priv->last_set) == 0)
54 WRITE_ONCE(priv->last_set, 1);
57 static int nft_last_dump(struct sk_buff *skb, const struct nft_expr *expr)
59 struct nft_last_priv *priv = nft_expr_priv(expr);
60 unsigned long last_jiffies = READ_ONCE(priv->last_jiffies);
61 u32 last_set = READ_ONCE(priv->last_set);
64 if (time_before(jiffies, last_jiffies)) {
65 WRITE_ONCE(priv->last_set, 0);
70 msecs = nf_jiffies64_to_msecs(jiffies - last_jiffies);
74 if (nla_put_be32(skb, NFTA_LAST_SET, htonl(last_set)) ||
75 nla_put_be64(skb, NFTA_LAST_MSECS, msecs, NFTA_LAST_PAD))
84 static const struct nft_expr_ops nft_last_ops = {
85 .type = &nft_last_type,
86 .size = NFT_EXPR_SIZE(sizeof(struct nft_last_priv)),
87 .eval = nft_last_eval,
88 .init = nft_last_init,
89 .dump = nft_last_dump,
92 struct nft_expr_type nft_last_type __read_mostly = {
95 .policy = nft_last_policy,
96 .maxattr = NFTA_LAST_MAX,
97 .flags = NFT_EXPR_STATEFUL,