Merge branch 'akpm' (patches from Andrew)
[linux-2.6-microblaze.git] / net / netfilter / nft_last.c
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>
10
11 struct nft_last_priv {
12         unsigned long   last_jiffies;
13         unsigned int    last_set;
14 };
15
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 },
19 };
20
21 static int nft_last_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
22                          const struct nlattr * const tb[])
23 {
24         struct nft_last_priv *priv = nft_expr_priv(expr);
25         u64 last_jiffies;
26         u32 last_set = 0;
27         int err;
28
29         if (tb[NFTA_LAST_SET]) {
30                 last_set = ntohl(nla_get_be32(tb[NFTA_LAST_SET]));
31                 if (last_set == 1)
32                         priv->last_set = 1;
33         }
34
35         if (last_set && tb[NFTA_LAST_MSECS]) {
36                 err = nf_msecs_to_jiffies64(tb[NFTA_LAST_MSECS], &last_jiffies);
37                 if (err < 0)
38                         return err;
39
40                 priv->last_jiffies = jiffies - (unsigned long)last_jiffies;
41         }
42
43         return 0;
44 }
45
46 static void nft_last_eval(const struct nft_expr *expr,
47                           struct nft_regs *regs, const struct nft_pktinfo *pkt)
48 {
49         struct nft_last_priv *priv = nft_expr_priv(expr);
50
51         priv->last_jiffies = jiffies;
52         priv->last_set = 1;
53 }
54
55 static int nft_last_dump(struct sk_buff *skb, const struct nft_expr *expr)
56 {
57         struct nft_last_priv *priv = nft_expr_priv(expr);
58         __be64 msecs;
59
60         if (time_before(jiffies, priv->last_jiffies))
61                 priv->last_set = 0;
62
63         if (priv->last_set)
64                 msecs = nf_jiffies64_to_msecs(jiffies - priv->last_jiffies);
65         else
66                 msecs = 0;
67
68         if (nla_put_be32(skb, NFTA_LAST_SET, htonl(priv->last_set)) ||
69             nla_put_be64(skb, NFTA_LAST_MSECS, msecs, NFTA_LAST_PAD))
70                 goto nla_put_failure;
71
72         return 0;
73
74 nla_put_failure:
75         return -1;
76 }
77
78 static const struct nft_expr_ops nft_last_ops = {
79         .type           = &nft_last_type,
80         .size           = NFT_EXPR_SIZE(sizeof(struct nft_last_priv)),
81         .eval           = nft_last_eval,
82         .init           = nft_last_init,
83         .dump           = nft_last_dump,
84 };
85
86 struct nft_expr_type nft_last_type __read_mostly = {
87         .name           = "last",
88         .ops            = &nft_last_ops,
89         .policy         = nft_last_policy,
90         .maxattr        = NFTA_LAST_MAX,
91         .flags          = NFT_EXPR_STATEFUL,
92         .owner          = THIS_MODULE,
93 };