Commit | Line | Data |
---|---|---|
b96af92d FFM |
1 | #include <net/ip.h> |
2 | #include <net/tcp.h> | |
3 | ||
4 | #include <net/netfilter/nf_tables.h> | |
ddba40be | 5 | #include <linux/netfilter/nfnetlink_osf.h> |
b96af92d | 6 | |
b96af92d FFM |
7 | struct nft_osf { |
8 | enum nft_registers dreg:8; | |
a218dc82 | 9 | u8 ttl; |
b96af92d FFM |
10 | }; |
11 | ||
12 | static const struct nla_policy nft_osf_policy[NFTA_OSF_MAX + 1] = { | |
13 | [NFTA_OSF_DREG] = { .type = NLA_U32 }, | |
a218dc82 | 14 | [NFTA_OSF_TTL] = { .type = NLA_U8 }, |
b96af92d FFM |
15 | }; |
16 | ||
17 | static void nft_osf_eval(const struct nft_expr *expr, struct nft_regs *regs, | |
18 | const struct nft_pktinfo *pkt) | |
19 | { | |
20 | struct nft_osf *priv = nft_expr_priv(expr); | |
21 | u32 *dest = ®s->data[priv->dreg]; | |
22 | struct sk_buff *skb = pkt->skb; | |
23 | const struct tcphdr *tcp; | |
24 | struct tcphdr _tcph; | |
25 | const char *os_name; | |
26 | ||
27 | tcp = skb_header_pointer(skb, ip_hdrlen(skb), | |
28 | sizeof(struct tcphdr), &_tcph); | |
29 | if (!tcp) { | |
30 | regs->verdict.code = NFT_BREAK; | |
31 | return; | |
32 | } | |
33 | if (!tcp->syn) { | |
34 | regs->verdict.code = NFT_BREAK; | |
35 | return; | |
36 | } | |
37 | ||
a218dc82 | 38 | os_name = nf_osf_find(skb, nf_osf_fingers, priv->ttl); |
b96af92d | 39 | if (!os_name) |
35a8a3bd | 40 | strncpy((char *)dest, "unknown", NFT_OSF_MAXGENRELEN); |
b96af92d | 41 | else |
35a8a3bd | 42 | strncpy((char *)dest, os_name, NFT_OSF_MAXGENRELEN); |
b96af92d FFM |
43 | } |
44 | ||
45 | static int nft_osf_init(const struct nft_ctx *ctx, | |
46 | const struct nft_expr *expr, | |
47 | const struct nlattr * const tb[]) | |
48 | { | |
49 | struct nft_osf *priv = nft_expr_priv(expr); | |
50 | int err; | |
a218dc82 FFM |
51 | u8 ttl; |
52 | ||
5e91c9d9 | 53 | if (tb[NFTA_OSF_TTL]) { |
a218dc82 FFM |
54 | ttl = nla_get_u8(tb[NFTA_OSF_TTL]); |
55 | if (ttl > 2) | |
56 | return -EINVAL; | |
57 | priv->ttl = ttl; | |
58 | } | |
b96af92d FFM |
59 | |
60 | priv->dreg = nft_parse_register(tb[NFTA_OSF_DREG]); | |
61 | err = nft_validate_register_store(ctx, priv->dreg, NULL, | |
bab43449 | 62 | NFT_DATA_VALUE, NFT_OSF_MAXGENRELEN); |
b96af92d FFM |
63 | if (err < 0) |
64 | return err; | |
65 | ||
66 | return 0; | |
67 | } | |
68 | ||
69 | static int nft_osf_dump(struct sk_buff *skb, const struct nft_expr *expr) | |
70 | { | |
71 | const struct nft_osf *priv = nft_expr_priv(expr); | |
72 | ||
a218dc82 FFM |
73 | if (nla_put_u8(skb, NFTA_OSF_TTL, priv->ttl)) |
74 | goto nla_put_failure; | |
75 | ||
b96af92d FFM |
76 | if (nft_dump_register(skb, NFTA_OSF_DREG, priv->dreg)) |
77 | goto nla_put_failure; | |
78 | ||
79 | return 0; | |
80 | ||
81 | nla_put_failure: | |
82 | return -1; | |
83 | } | |
84 | ||
4a3e71b7 FFM |
85 | static int nft_osf_validate(const struct nft_ctx *ctx, |
86 | const struct nft_expr *expr, | |
87 | const struct nft_data **data) | |
88 | { | |
89 | return nft_chain_validate_hooks(ctx->chain, (1 << NF_INET_LOCAL_IN) | | |
90 | (1 << NF_INET_PRE_ROUTING) | | |
91 | (1 << NF_INET_FORWARD)); | |
92 | } | |
93 | ||
b96af92d FFM |
94 | static struct nft_expr_type nft_osf_type; |
95 | static const struct nft_expr_ops nft_osf_op = { | |
96 | .eval = nft_osf_eval, | |
97 | .size = NFT_EXPR_SIZE(sizeof(struct nft_osf)), | |
98 | .init = nft_osf_init, | |
99 | .dump = nft_osf_dump, | |
100 | .type = &nft_osf_type, | |
4a3e71b7 | 101 | .validate = nft_osf_validate, |
b96af92d FFM |
102 | }; |
103 | ||
104 | static struct nft_expr_type nft_osf_type __read_mostly = { | |
105 | .ops = &nft_osf_op, | |
106 | .name = "osf", | |
107 | .owner = THIS_MODULE, | |
108 | .policy = nft_osf_policy, | |
109 | .maxattr = NFTA_OSF_MAX, | |
110 | }; | |
111 | ||
112 | static int __init nft_osf_module_init(void) | |
113 | { | |
114 | return nft_register_expr(&nft_osf_type); | |
115 | } | |
116 | ||
117 | static void __exit nft_osf_module_exit(void) | |
118 | { | |
119 | return nft_unregister_expr(&nft_osf_type); | |
120 | } | |
121 | ||
122 | module_init(nft_osf_module_init); | |
123 | module_exit(nft_osf_module_exit); | |
124 | ||
125 | MODULE_LICENSE("GPL"); | |
126 | MODULE_AUTHOR("Fernando Fernandez <ffmancera@riseup.net>"); | |
127 | MODULE_ALIAS_NFT_EXPR("osf"); |