-
Notifications
You must be signed in to change notification settings - Fork 1
/
post.c
executable file
·326 lines (275 loc) · 10.8 KB
/
post.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include <linux/types.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/skbuff.h>
#include <linux/netfilter_bridge.h>
#include <linux/skbuff.h>
#include <linux/if_ether.h>
#include <linux/if_vlan.h>
#include <linux/if_arp.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/netfilter_arp.h>
#include <linux/ip.h>
#include <linux/udp.h>
#include <linux/tcp.h>
#include <net/checksum.h>
#define DRIVER_AUTHOR "a_r_g_v"
#define DRIVER_DESC "sugoi"
#define ADD_VLAN 50
static struct nf_hook_ops arp_in_nfho;
static struct nf_hook_ops arp_out_nfho;
static struct nf_hook_ops nfho;
struct arp_body {
unsigned char src_mac[6];
unsigned char src_ip[4];
unsigned char dest_mac[6];
unsigned char dest_ip[4];
};
struct arpbdy {
__be16 ar_hrd; /* format of hardware address */
__be16 ar_pro; /* format of protocol address */
unsigned char ar_hln; /* length of hardware address */
unsigned char ar_pln; /* length of protocol address */
__be16 ar_op; /* ARP opcode (command) */
unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
unsigned char saddr[4]; /* sender IP address */
unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
unsigned char daddr[4]; /* target IP address */
};
static unsigned int arp_in_hook_func(void *priv,
struct sk_buff *skb,
const struct nf_hook_state *state)
{
struct ethhdr *ehdr = eth_hdr(skb);
int team_id;
unsigned int vlan_id;
// IP
if (ehdr->h_proto == 0x0008) {
struct iphdr *iph = ip_hdr(skb);
if (iph == NULL || iph->daddr == NULL || iph->saddr == NULL) {
return NF_ACCEPT;
}
uint32_t daddr = ntohl(iph->daddr);
uint32_t saddr = ntohl(iph->saddr);
vlan_id = skb_vlan_tag_get_id(skb);
team_id = vlan_id / 100;
//printk(KERN_INFO "[Before IP IN] daddr %pI4, saddr %pI4 vlan_id: %d, team_id: %d \n", &iph->daddr, &iph->saddr, vlan_id, team_id);
bool flag = false;
//printk(KERN_INFO "daddr %x saddr %x \n",daddr, saddr );
//
if (daddr >= 0xc0a80000 && daddr <= 0xc0a8ffff &&
saddr >= 0x0a000000 && saddr <= 0x0a0fffff ) {
//printk(KERN_INFO "DROP IP \n" );
return NF_DROP;
}
if (saddr >= 0xc0a80000 && saddr <= 0xc0a8ffff &&
daddr >= 0x0a000000 && daddr <= 0x0a0fffff ) {
// printk(KERN_INFO "DROP IP \n" );
return NF_DROP;
}
// DADDR: Incoming 192.168.0.0 ~ 192.168.255.255 , Rewrite 10.team_id.x.y
if (daddr >= 0xc0a80000 && daddr <= 0xc0a8ffff) {
daddr = daddr & 0x0000ffff; // 0.0.x.y
daddr |= 0x0a000000; // 10.0.x.y
daddr |= team_id << 16; //10.team_id.x.y
csum_replace2(&iph->check, iph->daddr, htonl(daddr)); // rewrite checksum
// TCP Rewrite Checksum
if(iph->protocol == 0x6) {
struct tcphdr *tcph = tcp_hdr(skb);
if(tcph == NULL || tcph->check == NULL) {
return NF_ACCEPT;
}
csum_replace2(&tcph->check, iph->daddr, htonl(daddr));
//printk(KERN_INFO "[After TCP DEST] tcp_check: %08x, old_check: %08x \n", tcph->check, old_check);
}
// UDP Rewrite Checksum
else if (iph->protocol == 17) {
struct udphdr *udph = udp_hdr(skb);
if(udph == NULL) {
return NF_ACCEPT;
}
csum_replace2(&udph->check, iph->daddr, htonl(daddr));
//printk(KERN_INFO "[After UDP DEST] udp_check: %08x, old_check: %08x \n", udph->check, old_check);
}
iph->daddr = htonl(daddr);
flag = true;
}
// SADDR: Incoming 192.168.0.0 ~ 192.168.255.255 , Rewrite 10.team_id.x.y
if (saddr >= 0xc0a80000 && saddr <= 0xc0a8ffff) {
saddr = saddr & 0x0000ffff; // 0.0.x.y
saddr |= 0x0a000000; // 10.0.x.y
saddr |= team_id << 16; //10.team_id.x.y
csum_replace2(&iph->check, iph->saddr, htonl(saddr));
// TCP, Calucate Checksum
if(iph->protocol == 0x6) {
struct tcphdr *tcph = tcp_hdr(skb);
if(tcph == NULL || tcph->check == NULL) {
return NF_ACCEPT;
}
csum_replace2(&tcph->check, iph->saddr, htonl(saddr));
//printk(KERN_INFO "[After TCP SRC] tcp_check: %08x, old_check: %08x \n", tcph->check, old_check);
// UDP, Calucate Checksum
} else if (iph->protocol == 17) {
struct udphdr *udph = udp_hdr(skb);
if(udph == NULL) {
return NF_ACCEPT;
}
csum_replace2(&udph->check, iph->saddr, htonl(saddr));
//printk(KERN_INFO "[After UDP SRC] udp_check: %08x, old_check: %08x \n", udph->check, old_check);
}
iph->saddr = htonl(saddr);
flag = true;
}
if(flag){
skb->vlan_tci += 2000 + ADD_VLAN;
}
//printk(KERN_INFO "[After IP OUT] daddr %pI4, saddr %pI4 vlan_id: %d, team_id: %d \n", &iph->daddr, &iph->saddr, skb_vlan_tag_get_id(skb), team_id);
return NF_ACCEPT;
}
// ARP
else if (ehdr->h_proto == 0x0608) {
struct arpbdy *arpb = (struct arpbdy *)arp_hdr(skb);
if (!arpb){
return NF_ACCEPT;
}
vlan_id = skb_vlan_tag_get_id(skb);
team_id = vlan_id / 100;
//printk(KERN_INFO "[Before ARP IN] daddr %pI4, saddr %pI4 vlan_id: %d, team_id: %d \n", arpb->daddr, arpb->saddr, vlan_id, team_id);
if (!arpb->daddr || !arpb->saddr) {
return NF_ACCEPT;
}
if (arpb->daddr[0] == 192 && arpb->daddr[1] == 168 &&
arpb->saddr[0] == 10 && (arpb->saddr[1] >= 0 && arpb->saddr[1] <= 15)) {
return NF_DROP;
}
if (arpb->saddr[0] == 192 && arpb->saddr[1] == 168 &&
arpb->daddr[0] == 10 && (arpb->daddr[1] >= 0 && arpb->daddr[1] <= 15)) {
return NF_DROP;
}
// if addr 192.168.0.0 ~ 192.168.255.255, rewrite 10.team_id.x.y
if (arpb->daddr[0] == 192 && arpb->daddr[1] == 168 &&
arpb->saddr[0] == 192 && arpb->saddr[1] == 168) {
arpb->daddr[0] = arpb->saddr[0] = 10;
arpb->daddr[1] = arpb->saddr[1] = team_id;
skb->vlan_tci += 2000 + ADD_VLAN;
}
//printk(KERN_INFO "[After ARP IN] daddr %pI4, saddr %pI4 vlan_id: %d, team_id: %d \n", arpb->daddr, arpb->saddr, vlan_id, team_id);
}
return NF_ACCEPT;
}
static unsigned int arp_out_hook_func(void *priv,
struct sk_buff *skb,
const struct nf_hook_state *state)
{
struct ethhdr *ehdr = eth_hdr(skb);
unsigned int vlan_id;
int team_id;
// IP
if (ehdr->h_proto == 0x0008) {
struct iphdr *iph = ip_hdr(skb);
if (iph == NULL || iph->daddr == NULL || iph->saddr == NULL) {
return NF_ACCEPT;
}
vlan_id = skb_vlan_tag_get_id(skb); // 2000
bool flag = false;
uint32_t daddr = ntohl(iph->daddr);
uint32_t saddr = ntohl(iph->saddr);
//printk(KERN_INFO "[Before IP OUT] daddr %pI4, saddr %pI4 vlan_id: %d, team_id: %d \n", &iph->daddr, &iph->saddr, skb_vlan_tag_get_id(skb), team_id);
if (daddr >= 0x0a000000 && daddr <= 0x0a0fffff && vlan_id < 2000) {
daddr = daddr & 0x0000ffff; // 0.0.x.y
daddr |= 0xc0a80000; // 192.168.x.y
csum_replace2(&iph->check, iph->daddr, htonl(daddr));
if(iph->protocol == 0x6) {
struct tcphdr *tcph = tcp_hdr(skb);
csum_replace2(&tcph->check, iph->daddr, htonl(daddr));
//printk(KERN_INFO "[After TCP DEST] tcp_check: %08x, old_check: %08x \n", tcph->check, old_check);
} else if (iph->protocol == 17) {
struct udphdr *udph = udp_hdr(skb);
csum_replace2(&udph->check, iph->daddr, htonl(daddr));
//printk(KERN_INFO "[After UDP DEST] udp_check: %08x, old_check: %08x \n", udph->check, old_check);
}
iph->daddr = htonl(daddr);
flag = true;
}
if (saddr >= 0x0a000000 && saddr <= 0x0a0fffff && vlan_id < 2000) {
saddr = saddr & 0x0000ffff; // 0.0.x.y
saddr |= 0xc0a80000; // 192.168.x.y
csum_replace2(&iph->check, iph->saddr, htonl(saddr));
if(iph->protocol == 0x6) {
struct tcphdr *tcph = tcp_hdr(skb);
if (tcph == NULL || tcph->check == NULL ) {
return NF_ACCEPT;
}
csum_replace2(&tcph->check, iph->saddr, htonl(saddr));
//printk(KERN_INFO "[After TCP DEST] tcp_check: %08x, old_check: %08x \n", tcph->check, old_check);
} else if (iph->protocol == 17) {
struct udphdr *udph = udp_hdr(skb);
if (udph == NULL) {
return NF_ACCEPT;
}
csum_replace2(&udph->check, iph->saddr, htonl(saddr));
//printk(KERN_INFO "[After UDP DEST] udp_check: %08x, old_check: %08x \n", udph->check, old_check);
}
iph->saddr = htonl(saddr);
flag = true;
}
if(flag) {
skb->vlan_tci -= ADD_VLAN;
}
if (vlan_id >= 2000) {
skb->vlan_tci -= 2000;
}
//printk(KERN_INFO "[After IP OUT] daddr %pI4, saddr %pI4 vlan_id: %d, team_id: %d \n", &iph->daddr, &iph->saddr, skb_vlan_tag_get_id(skb), team_id);
}
// ARP
else if (ehdr->h_proto == 0x0608) {
struct arpbdy *arpb = (struct arpbdy *)arp_hdr(skb);
if (!arpb){
return NF_ACCEPT;
}
vlan_id = skb_vlan_tag_get_id(skb); // 2000
team_id = vlan_id / 100;
//printk(KERN_INFO "[Before ARP OUT] daddr %pI4, saddr %pI4 vlan_id: %d, team_id: %d \n", arpb->daddr, arpb->saddr, vlan_id, team_id);
if (!arpb->daddr || !arpb->saddr) {
return NF_ACCEPT;
}
if (vlan_id >= 2000) {
skb->vlan_tci -= 2000;
}
// if addr 10.1.0.0 ~ 10.15.255.255, rewrite 192.168.x.y
else if (arpb->daddr[0] == 10 && arpb->daddr[1] >= 0 && arpb->daddr[1] <= 15 &&
arpb->saddr[0] == 10 && arpb->saddr[1] >= 0 && arpb->saddr[1] <= 15 ) {
arpb->daddr[0] = arpb->saddr[0] = 192;
arpb->daddr[1] = arpb->saddr[1] = 168;
skb->vlan_tci -= ADD_VLAN;
}
//printk(KERN_INFO "[After ARP OUT] daddr %pI4, saddr %pI4 vlan_id: %d, team_id: %d !!!vlan_id!!!: %d \n", arpb->daddr, arpb->saddr, vlan_id, team_id, skb_vlan_tag_get_id(skb));
}
return NF_ACCEPT;
}
//----------------------------------
static int __init nfe_init(void)
{
arp_in_nfho.hook = arp_in_hook_func;
arp_in_nfho.hooknum =NF_BR_PRE_ROUTING;
arp_in_nfho.pf = PF_BRIDGE;
nf_register_hook(&arp_in_nfho);
arp_out_nfho.hook = arp_out_hook_func;
arp_out_nfho.hooknum =NF_BR_FORWARD;
arp_out_nfho.pf = PF_BRIDGE;
nf_register_hook(&arp_out_nfho);
return 0;
}
//----------------------------------
static void __exit nfe_exit(void)
{
nf_unregister_hook(&arp_in_nfho);
nf_unregister_hook(&arp_out_nfho);
}
module_init(nfe_init);
module_exit(nfe_exit);
MODULE_LICENSE("GPLv3");
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);