forked from ptpt52/natflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnatflow_path.h
268 lines (229 loc) · 6.7 KB
/
natflow_path.h
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
/*
* Author: Chen Minqiang <[email protected]>
* Date : Mon, 14 May 2018 14:49:40 +0800
*/
#ifndef _NATFLOW_PATH_H_
#define _NATFLOW_PATH_H_
#include <linux/version.h>
#include <linux/ctype.h>
#include <linux/device.h>
#include <linux/skbuff.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/icmp.h>
#include <linux/netfilter.h>
#include <net/ip.h>
#include <net/tcp.h>
#include <net/udp.h>
#include <net/icmp.h>
#include <net/netfilter/nf_conntrack.h>
#include <net/netfilter/nf_conntrack_core.h>
#include <net/netfilter/nf_conntrack_zones.h>
#include <net/netfilter/nf_nat.h>
#include <linux/if_macvlan.h>
#include "natflow.h"
#include "natflow_common.h"
extern unsigned int hwnat;
extern unsigned int delay_pkts;
extern void natflow_disabled_set(int v);
extern int natflow_disabled_get(void);
extern void natflow_update_magic(int init);
static inline int natflow_nat_ip_tcp(struct sk_buff *skb, unsigned int thoff,
__be32 addr, __be32 new_addr)
{
struct tcphdr *tcph;
tcph = (void *)(skb_network_header(skb) + thoff);
inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr, true);
return 0;
}
static inline int natflow_nat_ip_udp(struct sk_buff *skb, unsigned int thoff,
__be32 addr, __be32 new_addr)
{
struct udphdr *udph;
udph = (void *)(skb_network_header(skb) + thoff);
if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
inet_proto_csum_replace4(&udph->check, skb, addr,
new_addr, true);
if (!udph->check)
udph->check = CSUM_MANGLED_0;
}
return 0;
}
static inline int natflow_nat_ip_l4proto(struct sk_buff *skb, struct iphdr *iph,
unsigned int thoff, __be32 addr,
__be32 new_addr)
{
switch (iph->protocol) {
case IPPROTO_TCP:
return natflow_nat_ip_tcp(skb, thoff, addr, new_addr);
case IPPROTO_UDP:
return natflow_nat_ip_udp(skb, thoff, addr, new_addr);
default:
break;
}
return -1;
}
static inline int natflow_nat_port_tcp(struct sk_buff *skb, unsigned int thoff,
__be16 port, __be16 new_port)
{
struct tcphdr *tcph;
tcph = (void *)(skb_network_header(skb) + thoff);
inet_proto_csum_replace2(&tcph->check, skb, port, new_port, true);
return 0;
}
static inline int natflow_nat_port_udp(struct sk_buff *skb, unsigned int thoff,
__be16 port, __be16 new_port)
{
struct udphdr *udph;
udph = (void *)(skb_network_header(skb) + thoff);
if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
inet_proto_csum_replace2(&udph->check, skb, port,
new_port, true);
if (!udph->check)
udph->check = CSUM_MANGLED_0;
}
return 0;
}
static inline int natflow_nat_port(struct sk_buff *skb, unsigned int thoff,
u8 protocol, __be16 port, __be16 new_port)
{
switch (protocol) {
case IPPROTO_TCP:
return natflow_nat_port_tcp(skb, thoff, port, new_port);
case IPPROTO_UDP:
return natflow_nat_port_udp(skb, thoff, port, new_port);
default:
break;
}
return -1;
}
static inline int natflow_do_snat(struct sk_buff *skb, struct nf_conn *ct, int dir) {
struct iphdr *iph;
void *l4;
__be32 addr;
__be16 port = 0;
iph = ip_hdr(skb);
l4 = (void *)iph + iph->ihl * 4;
switch(iph->protocol) {
case IPPROTO_TCP:
if (!pskb_may_pull(skb, iph->ihl * 4 + sizeof(struct tcphdr)) ||
skb_try_make_writable(skb, iph->ihl * 4 + sizeof(struct tcphdr)))
return -1;
port = TCPH(l4)->source;
TCPH(l4)->source = ct->tuplehash[!dir].tuple.dst.u.all;
if (natflow_nat_port(skb, iph->ihl * 4, iph->protocol, port, TCPH(l4)->source) != 0) {
return -1;
}
break;
case IPPROTO_UDP:
if (!pskb_may_pull(skb, iph->ihl * 4 + sizeof(struct udphdr)) ||
skb_try_make_writable(skb, iph->ihl * 4 + sizeof(struct udphdr)))
return -1;
port = UDPH(l4)->source;
UDPH(l4)->source = ct->tuplehash[!dir].tuple.dst.u.all;
if (natflow_nat_port(skb, iph->ihl * 4, iph->protocol, port, UDPH(l4)->source) != 0) {
return -1;
}
break;
default:
return -1;
}
iph = ip_hdr(skb);
addr = iph->saddr;
iph->saddr = ct->tuplehash[!dir].tuple.dst.u3.ip;
csum_replace4(&iph->check, addr, iph->saddr);
if (natflow_nat_ip_l4proto(skb, iph, iph->ihl * 4, addr, iph->saddr) != 0) {
return -1;
}
return 0;
}
static inline int natflow_do_dnat(struct sk_buff *skb, struct nf_conn *ct, int dir) {
struct iphdr *iph;
void *l4;
__be32 addr;
__be16 port = 0;
iph = ip_hdr(skb);
l4 = (void *)iph + iph->ihl * 4;
switch(iph->protocol) {
case IPPROTO_TCP:
if (!pskb_may_pull(skb, iph->ihl * 4 + sizeof(struct tcphdr)) ||
skb_try_make_writable(skb, iph->ihl * 4 + sizeof(struct tcphdr)))
return -1;
port = TCPH(l4)->dest;
TCPH(l4)->dest = ct->tuplehash[!dir].tuple.src.u.all;
if (natflow_nat_port(skb, iph->ihl * 4, iph->protocol, port, TCPH(l4)->dest) != 0) {
return -1;
}
break;
case IPPROTO_UDP:
port = UDPH(l4)->dest;
UDPH(l4)->dest = ct->tuplehash[!dir].tuple.src.u.all;
if (natflow_nat_port(skb, iph->ihl * 4, iph->protocol, port, UDPH(l4)->dest) != 0) {
return -1;
}
break;
default:
return -1;
}
iph = ip_hdr(skb);
addr = iph->daddr;
iph->daddr = ct->tuplehash[!dir].tuple.src.u3.ip;
csum_replace4(&iph->check, addr, iph->daddr);
if (natflow_nat_ip_l4proto(skb, iph, iph->ihl * 4, addr, iph->daddr) != 0) {
return -1;
}
return 0;
}
static inline struct net_device *vlan_lookup_dev(struct net_device *dev, unsigned short vlan_id)
{
struct net_device *vlan_dev;
vlan_dev = first_net_device(dev_net(dev));
while (vlan_dev) {
if (is_vlan_dev(vlan_dev)) {
struct vlan_dev_priv *vlan = vlan_dev_priv(vlan_dev);
if (vlan->vlan_id == vlan_id && vlan->real_dev == dev) {
return vlan_dev;
}
}
vlan_dev = next_net_device(vlan_dev);
}
return NULL;
}
static inline struct net_device *get_vlan_real_dev(struct net_device *dev)
{
if (is_vlan_dev(dev)) {
struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
return vlan->real_dev;
}
return dev;
}
static inline __be16 get_vlan_vid(struct net_device *dev)
{
if (is_vlan_dev(dev)) {
struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
return vlan->vlan_id;
}
return 0;
}
static inline __be16 get_vlan_proto(struct net_device *dev)
{
if (is_vlan_dev(dev)) {
struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
return vlan->vlan_proto;
}
return 0;
}
static inline struct net_device *get_macvlan_real_dev(struct net_device *dev)
{
#if IS_ENABLED(CONFIG_MACVLAN)
if (netif_is_macvlan(dev)) {
return macvlan_dev_real_dev(dev);
}
#endif
return dev;
}
void natflow_session_learn(struct sk_buff *skb, struct nf_conn *ct, natflow_t *nf, int dir);
extern int natflow_path_init(void);
extern void natflow_path_exit(void);
#endif /* _NATFLOW_PATH_H_ */