]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - net/netfilter/nft_osf.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
[mirror_ubuntu-hirsute-kernel.git] / net / netfilter / nft_osf.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
b96af92d
FFM
2#include <net/ip.h>
3#include <net/tcp.h>
4
5#include <net/netfilter/nf_tables.h>
ddba40be 6#include <linux/netfilter/nfnetlink_osf.h>
b96af92d 7
b96af92d
FFM
8struct nft_osf {
9 enum nft_registers dreg:8;
a218dc82 10 u8 ttl;
22c7652c 11 u32 flags;
b96af92d
FFM
12};
13
14static const struct nla_policy nft_osf_policy[NFTA_OSF_MAX + 1] = {
15 [NFTA_OSF_DREG] = { .type = NLA_U32 },
a218dc82 16 [NFTA_OSF_TTL] = { .type = NLA_U8 },
22c7652c 17 [NFTA_OSF_FLAGS] = { .type = NLA_U32 },
b96af92d
FFM
18};
19
20static void nft_osf_eval(const struct nft_expr *expr, struct nft_regs *regs,
21 const struct nft_pktinfo *pkt)
22{
23 struct nft_osf *priv = nft_expr_priv(expr);
24 u32 *dest = &regs->data[priv->dreg];
25 struct sk_buff *skb = pkt->skb;
22c7652c 26 char os_match[NFT_OSF_MAXGENRELEN + 1];
b96af92d 27 const struct tcphdr *tcp;
22c7652c 28 struct nf_osf_data data;
b96af92d 29 struct tcphdr _tcph;
b96af92d
FFM
30
31 tcp = skb_header_pointer(skb, ip_hdrlen(skb),
32 sizeof(struct tcphdr), &_tcph);
33 if (!tcp) {
34 regs->verdict.code = NFT_BREAK;
35 return;
36 }
37 if (!tcp->syn) {
38 regs->verdict.code = NFT_BREAK;
39 return;
40 }
41
22c7652c 42 if (!nf_osf_find(skb, nf_osf_fingers, priv->ttl, &data)) {
35a8a3bd 43 strncpy((char *)dest, "unknown", NFT_OSF_MAXGENRELEN);
22c7652c
FFM
44 } else {
45 if (priv->flags & NFT_OSF_F_VERSION)
46 snprintf(os_match, NFT_OSF_MAXGENRELEN, "%s:%s",
47 data.genre, data.version);
48 else
49 strlcpy(os_match, data.genre, NFT_OSF_MAXGENRELEN);
50
51 strncpy((char *)dest, os_match, NFT_OSF_MAXGENRELEN);
52 }
b96af92d
FFM
53}
54
55static int nft_osf_init(const struct nft_ctx *ctx,
56 const struct nft_expr *expr,
57 const struct nlattr * const tb[])
58{
59 struct nft_osf *priv = nft_expr_priv(expr);
22c7652c 60 u32 flags;
b96af92d 61 int err;
a218dc82
FFM
62 u8 ttl;
63
7eaecf79
FW
64 if (!tb[NFTA_OSF_DREG])
65 return -EINVAL;
66
5e91c9d9 67 if (tb[NFTA_OSF_TTL]) {
a218dc82
FFM
68 ttl = nla_get_u8(tb[NFTA_OSF_TTL]);
69 if (ttl > 2)
70 return -EINVAL;
71 priv->ttl = ttl;
72 }
b96af92d 73
22c7652c
FFM
74 if (tb[NFTA_OSF_FLAGS]) {
75 flags = ntohl(nla_get_be32(tb[NFTA_OSF_FLAGS]));
76 if (flags != NFT_OSF_F_VERSION)
77 return -EINVAL;
78 priv->flags = flags;
79 }
80
b96af92d
FFM
81 priv->dreg = nft_parse_register(tb[NFTA_OSF_DREG]);
82 err = nft_validate_register_store(ctx, priv->dreg, NULL,
bab43449 83 NFT_DATA_VALUE, NFT_OSF_MAXGENRELEN);
b96af92d
FFM
84 if (err < 0)
85 return err;
86
87 return 0;
88}
89
90static int nft_osf_dump(struct sk_buff *skb, const struct nft_expr *expr)
91{
92 const struct nft_osf *priv = nft_expr_priv(expr);
93
a218dc82
FFM
94 if (nla_put_u8(skb, NFTA_OSF_TTL, priv->ttl))
95 goto nla_put_failure;
96
22c7652c
FFM
97 if (nla_put_be32(skb, NFTA_OSF_FLAGS, ntohl(priv->flags)))
98 goto nla_put_failure;
99
b96af92d
FFM
100 if (nft_dump_register(skb, NFTA_OSF_DREG, priv->dreg))
101 goto nla_put_failure;
102
103 return 0;
104
105nla_put_failure:
106 return -1;
107}
108
4a3e71b7
FFM
109static int nft_osf_validate(const struct nft_ctx *ctx,
110 const struct nft_expr *expr,
111 const struct nft_data **data)
112{
113 return nft_chain_validate_hooks(ctx->chain, (1 << NF_INET_LOCAL_IN) |
114 (1 << NF_INET_PRE_ROUTING) |
115 (1 << NF_INET_FORWARD));
116}
117
b96af92d
FFM
118static struct nft_expr_type nft_osf_type;
119static const struct nft_expr_ops nft_osf_op = {
120 .eval = nft_osf_eval,
121 .size = NFT_EXPR_SIZE(sizeof(struct nft_osf)),
122 .init = nft_osf_init,
123 .dump = nft_osf_dump,
124 .type = &nft_osf_type,
4a3e71b7 125 .validate = nft_osf_validate,
b96af92d
FFM
126};
127
128static struct nft_expr_type nft_osf_type __read_mostly = {
129 .ops = &nft_osf_op,
130 .name = "osf",
131 .owner = THIS_MODULE,
132 .policy = nft_osf_policy,
133 .maxattr = NFTA_OSF_MAX,
134};
135
136static int __init nft_osf_module_init(void)
137{
138 return nft_register_expr(&nft_osf_type);
139}
140
141static void __exit nft_osf_module_exit(void)
142{
143 return nft_unregister_expr(&nft_osf_type);
144}
145
146module_init(nft_osf_module_init);
147module_exit(nft_osf_module_exit);
148
149MODULE_LICENSE("GPL");
150MODULE_AUTHOR("Fernando Fernandez <ffmancera@riseup.net>");
151MODULE_ALIAS_NFT_EXPR("osf");
4cacc395 152MODULE_DESCRIPTION("nftables passive OS fingerprint support");