-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathnatcap_knock.c
295 lines (271 loc) · 8.42 KB
/
natcap_knock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* Author: Chen Minqiang <[email protected]>
* Date : Sun, 05 Jun 2016 16:24:04 +0800
*
* This file is part of the natcap.
*
* natcap is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* natcap is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with natcap; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <linux/ctype.h>
#include <linux/device.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <linux/if_arp.h>
#include <linux/init.h>
#include <linux/ip.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/netfilter.h>
#include <linux/skbuff.h>
#include <linux/string.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/version.h>
#include <net/netfilter/nf_conntrack.h>
#include "natcap_common.h"
#include "natcap_knock.h"
unsigned short knock_port = __constant_htons(65535);
unsigned int knock_flood = 0;
unsigned short knock_encryption = 0;
unsigned char knock_tcp_encode = TCP_ENCODE;
unsigned char knock_udp_encode = UDP_ENCODE;
void natcap_knock_info_select(__be32 ip, __be16 port, struct tuple *dst)
{
dst->ip = ip;
dst->port = knock_port;
dst->encryption = knock_encryption;
dst->tcp_encode = knock_tcp_encode;
dst->udp_encode = knock_udp_encode;
if (dst->port == __constant_htons(0)) {
dst->port = port;
} else if (dst->port == __constant_htons(65535)) {
dst->port = ((jiffies^ip) & 0xffff);
dst->port = htons(ntohs(dst->port) % (65536 - 1024) + 1024);
}
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
static unsigned int natcap_knock_dnat_hook(unsigned int hooknum,
struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
#elif LINUX_VERSION_CODE < KERNEL_VERSION(4, 1, 0)
static unsigned int natcap_knock_dnat_hook(const struct nf_hook_ops *ops,
struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
unsigned int hooknum = ops->hooknum;
#elif LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0)
static unsigned int natcap_knock_dnat_hook(const struct nf_hook_ops *ops,
struct sk_buff *skb,
const struct nf_hook_state *state)
{
unsigned int hooknum = state->hook;
const struct net_device *in = state->in;
const struct net_device *out = state->out;
#else
static unsigned int natcap_knock_dnat_hook(void *priv,
struct sk_buff *skb,
const struct nf_hook_state *state)
{
unsigned int hooknum = state->hook;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0)
const struct net_device *in = state->in;
const struct net_device *out = state->out;
#endif
#endif
enum ip_conntrack_info ctinfo;
struct nf_conn *ct;
struct iphdr *iph;
void *l4;
struct tuple server;
if (disabled)
return NF_ACCEPT;
iph = ip_hdr(skb);
if (iph->protocol != IPPROTO_TCP) {
return NF_ACCEPT;
}
l4 = (void *)iph + iph->ihl * 4;
ct = nf_ct_get(skb, &ctinfo);
if (NULL == ct) {
return NF_ACCEPT;
}
if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) {
return NF_ACCEPT;
}
if ((IPS_NATCAP_BYPASS & ct->status)) {
return NF_ACCEPT;
}
if ((IPS_NATCAP & ct->status)) {
return NF_ACCEPT;
}
if (!nf_ct_is_confirmed(ct)) {
if (skb->mark & natcap_ignore_mask) {
set_bit(IPS_NATCAP_PRE_BIT, &ct->status);
set_bit(IPS_NATCAP_BYPASS_BIT, &ct->status);
set_bit(IPS_NATCAP_SERVER_BIT, &ct->status);
return NF_ACCEPT;
}
}
if (!skb_make_writable(skb, iph->ihl * 4 + sizeof(struct tcphdr))) {
return NF_DROP;
}
iph = ip_hdr(skb);
l4 = (void *)iph + iph->ihl * 4;
if (TCPH(l4)->doff * 4 < sizeof(struct tcphdr)) {
return NF_DROP;
}
if (IP_SET_test_dst_ip(state, in, out, skb, "knocklist") > 0) {
natcap_knock_info_select(iph->daddr, ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u.all, &server);
NATCAP_INFO("(KD)" DEBUG_TCP_FMT ": new connection, before encode, server=" TUPLE_FMT "\n", DEBUG_TCP_ARG(iph,l4), TUPLE_ARG(&server));
} else {
set_bit(IPS_NATCAP_BYPASS_BIT, &ct->status);
return NF_ACCEPT;
}
if (!(IPS_NATCAP & ct->status) && !test_and_set_bit(IPS_NATCAP_BIT, &ct->status)) { /* first time out */
if (!TCPH(l4)->syn || TCPH(l4)->ack) {
NATCAP_INFO("(KD)" DEBUG_TCP_FMT ": first packet in but not syn, bypass\n", DEBUG_TCP_ARG(iph,l4));
set_bit(IPS_NATCAP_BYPASS_BIT, &ct->status);
return NF_ACCEPT;
}
if (natcap_dnat_setup(ct, server.ip, server.port) != NF_ACCEPT) {
NATCAP_ERROR("(KD)" DEBUG_TCP_FMT ": natcap_dnat_setup failed, server=" TUPLE_FMT "\n", DEBUG_TCP_ARG(iph,l4), TUPLE_ARG(&server));
set_bit(IPS_NATCAP_BYPASS_BIT, &ct->status);
return NF_DROP;
}
}
NATCAP_DEBUG("(KD)" DEBUG_TCP_FMT ": after encode\n", DEBUG_TCP_ARG(iph,l4));
return NF_ACCEPT;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
static unsigned int natcap_knock_post_out_hook(unsigned int hooknum,
struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
#elif LINUX_VERSION_CODE < KERNEL_VERSION(4, 1, 0)
static unsigned int natcap_knock_post_out_hook(const struct nf_hook_ops *ops,
struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
unsigned int hooknum = ops->hooknum;
#elif LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0)
static unsigned int natcap_knock_post_out_hook(const struct nf_hook_ops *ops,
struct sk_buff *skb,
const struct nf_hook_state *state)
{
unsigned int hooknum = state->hook;
//const struct net_device *in = state->in;
//const struct net_device *out = state->out;
#else
static unsigned int natcap_knock_post_out_hook(void *priv,
struct sk_buff *skb,
const struct nf_hook_state *state)
{
unsigned int hooknum = state->hook;
//const struct net_device *in = state->in;
//const struct net_device *out = state->out;
#endif
int ret = 0;
enum ip_conntrack_info ctinfo;
unsigned long status = NATCAP_CLIENT_MODE;
struct nf_conn *ct;
struct iphdr *iph;
void *l4;
struct natcap_TCPOPT tcpopt = { };
if (disabled)
return NF_ACCEPT;
iph = ip_hdr(skb);
if (iph->protocol != IPPROTO_TCP) {
return NF_ACCEPT;
}
l4 = (void *)iph + iph->ihl * 4;
ct = nf_ct_get(skb, &ctinfo);
if (NULL == ct) {
return NF_ACCEPT;
}
if ((IPS_NATCAP_BYPASS & ct->status)) {
return NF_ACCEPT;
}
if (!(IPS_NATCAP & ct->status)) {
return NF_ACCEPT;
}
if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) {
return NF_ACCEPT;
}
ret = natcap_tcpopt_setup(status, skb, ct, &tcpopt, ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.ip, ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u.tcp.port);
if (ret == 0) {
if (iph->daddr == ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.ip) {
tcpopt.header.type |= NATCAP_TCPOPT_TARGET;
}
ret = natcap_tcp_encode(ct, skb, &tcpopt, IP_CT_DIR_ORIGINAL);
iph = ip_hdr(skb);
l4 = (void *)iph + iph->ihl * 4;
}
if (ret != 0) {
NATCAP_ERROR("(KPO)" DEBUG_TCP_FMT ": natcap_tcp_encode() ret=%d\n", DEBUG_TCP_ARG(iph,l4), ret);
return NF_DROP;
}
NATCAP_DEBUG("(KPO)" DEBUG_TCP_FMT ": after encode\n", DEBUG_TCP_ARG(iph,l4));
return NF_ACCEPT;
}
static struct nf_hook_ops knock_hooks[] = {
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0)
.owner = THIS_MODULE,
#endif
.hook = natcap_knock_dnat_hook,
.pf = PF_INET,
.hooknum = NF_INET_PRE_ROUTING,
.priority = NF_IP_PRI_NAT_DST - 10 - 2,
},
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0)
.owner = THIS_MODULE,
#endif
.hook = natcap_knock_dnat_hook,
.pf = PF_INET,
.hooknum = NF_INET_LOCAL_OUT,
.priority = NF_IP_PRI_NAT_DST - 10 - 2,
},
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0)
.owner = THIS_MODULE,
#endif
.hook = natcap_knock_post_out_hook,
.pf = PF_INET,
.hooknum = NF_INET_POST_ROUTING,
.priority = NF_IP_PRI_LAST,
},
};
int natcap_knock_init(void)
{
int ret = 0;
need_conntrack();
ret = nf_register_hooks(knock_hooks, ARRAY_SIZE(knock_hooks));
return ret;
}
void natcap_knock_exit(void)
{
nf_unregister_hooks(knock_hooks, ARRAY_SIZE(knock_hooks));
}