xref: /openbmc/linux/net/netfilter/nft_osf.c (revision e1e38ea1)
1 #include <net/ip.h>
2 #include <net/tcp.h>
3 
4 #include <net/netfilter/nf_tables.h>
5 #include <linux/netfilter/nfnetlink_osf.h>
6 
7 struct nft_osf {
8 	enum nft_registers	dreg:8;
9 };
10 
11 static const struct nla_policy nft_osf_policy[NFTA_OSF_MAX + 1] = {
12 	[NFTA_OSF_DREG]		= { .type = NLA_U32 },
13 };
14 
15 static void nft_osf_eval(const struct nft_expr *expr, struct nft_regs *regs,
16 			 const struct nft_pktinfo *pkt)
17 {
18 	struct nft_osf *priv = nft_expr_priv(expr);
19 	u32 *dest = &regs->data[priv->dreg];
20 	struct sk_buff *skb = pkt->skb;
21 	const struct tcphdr *tcp;
22 	struct tcphdr _tcph;
23 	const char *os_name;
24 
25 	tcp = skb_header_pointer(skb, ip_hdrlen(skb),
26 				 sizeof(struct tcphdr), &_tcph);
27 	if (!tcp) {
28 		regs->verdict.code = NFT_BREAK;
29 		return;
30 	}
31 	if (!tcp->syn) {
32 		regs->verdict.code = NFT_BREAK;
33 		return;
34 	}
35 
36 	os_name = nf_osf_find(skb, nf_osf_fingers);
37 	if (!os_name)
38 		strncpy((char *)dest, "unknown", NFT_OSF_MAXGENRELEN);
39 	else
40 		strncpy((char *)dest, os_name, NFT_OSF_MAXGENRELEN);
41 }
42 
43 static int nft_osf_init(const struct nft_ctx *ctx,
44 			const struct nft_expr *expr,
45 			const struct nlattr * const tb[])
46 {
47 	struct nft_osf *priv = nft_expr_priv(expr);
48 	int err;
49 
50 	priv->dreg = nft_parse_register(tb[NFTA_OSF_DREG]);
51 	err = nft_validate_register_store(ctx, priv->dreg, NULL,
52 					  NFTA_DATA_VALUE, NFT_OSF_MAXGENRELEN);
53 	if (err < 0)
54 		return err;
55 
56 	return 0;
57 }
58 
59 static int nft_osf_dump(struct sk_buff *skb, const struct nft_expr *expr)
60 {
61 	const struct nft_osf *priv = nft_expr_priv(expr);
62 
63 	if (nft_dump_register(skb, NFTA_OSF_DREG, priv->dreg))
64 		goto nla_put_failure;
65 
66 	return 0;
67 
68 nla_put_failure:
69 	return -1;
70 }
71 
72 static struct nft_expr_type nft_osf_type;
73 static const struct nft_expr_ops nft_osf_op = {
74 	.eval		= nft_osf_eval,
75 	.size		= NFT_EXPR_SIZE(sizeof(struct nft_osf)),
76 	.init		= nft_osf_init,
77 	.dump		= nft_osf_dump,
78 	.type		= &nft_osf_type,
79 };
80 
81 static struct nft_expr_type nft_osf_type __read_mostly = {
82 	.ops		= &nft_osf_op,
83 	.name		= "osf",
84 	.owner		= THIS_MODULE,
85 	.policy		= nft_osf_policy,
86 	.maxattr	= NFTA_OSF_MAX,
87 };
88 
89 static int __init nft_osf_module_init(void)
90 {
91 	return nft_register_expr(&nft_osf_type);
92 }
93 
94 static void __exit nft_osf_module_exit(void)
95 {
96 	return nft_unregister_expr(&nft_osf_type);
97 }
98 
99 module_init(nft_osf_module_init);
100 module_exit(nft_osf_module_exit);
101 
102 MODULE_LICENSE("GPL");
103 MODULE_AUTHOR("Fernando Fernandez <ffmancera@riseup.net>");
104 MODULE_ALIAS_NFT_EXPR("osf");
105