-
Notifications
You must be signed in to change notification settings - Fork 20
/
natflow_path.h
376 lines (320 loc) · 9.54 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* 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 <asm/byteorder.h>
#include "natflow.h"
#include "natflow_common.h"
extern unsigned short hwnat;
extern unsigned short hwnat_wed_disabled;
extern unsigned int delay_pkts;
extern unsigned int go_slowpath_if_no_qos;
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_ipv6_l4proto(struct sk_buff *skb, struct ipv6hdr *ipv6h,
unsigned int thoff, struct in6_addr *addr, struct in6_addr *new_addr)
{
int i;
switch (ipv6h->nexthdr) {
case IPPROTO_TCP:
for (i = 0; i < 4; i++)
natflow_nat_ip_tcp(skb, thoff, addr->s6_addr32[i], new_addr->s6_addr32[i]);
break;
case IPPROTO_UDP:
for (i = 0; i < 4; i++)
natflow_nat_ip_udp(skb, thoff, addr->s6_addr32[i], new_addr->s6_addr32[i]);
break;
default:
break;
}
return 0;
}
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:
port = TCPH(l4)->source;
TCPH(l4)->source = ct->tuplehash[!dir].tuple.dst.u.all;
if (natflow_nat_port(skb, iph->ihl * 4, IPPROTO_TCP, port, TCPH(l4)->source) != 0) {
return -1;
}
break;
case IPPROTO_UDP:
port = UDPH(l4)->source;
UDPH(l4)->source = ct->tuplehash[!dir].tuple.dst.u.all;
if (natflow_nat_port(skb, iph->ihl * 4, IPPROTO_UDP, port, UDPH(l4)->source) != 0) {
return -1;
}
break;
default:
return -1;
}
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:
port = TCPH(l4)->dest;
TCPH(l4)->dest = ct->tuplehash[!dir].tuple.src.u.all;
if (natflow_nat_port(skb, iph->ihl * 4, IPPROTO_TCP, 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, IPPROTO_UDP, port, UDPH(l4)->dest) != 0) {
return -1;
}
break;
default:
return -1;
}
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 int natflow_do_snat6(struct sk_buff *skb, struct nf_conn *ct, int dir) {
struct ipv6hdr *ipv6h;
void *l4;
__be16 port = 0;
ipv6h = ipv6_hdr(skb);
l4 = (void *)ipv6h + sizeof(struct ipv6hdr);
switch(ipv6h->nexthdr) {
case IPPROTO_TCP:
port = TCPH(l4)->source;
TCPH(l4)->source = ct->tuplehash[!dir].tuple.dst.u.all;
if (natflow_nat_port(skb, sizeof(struct ipv6hdr), IPPROTO_TCP, port, TCPH(l4)->source) != 0) {
return -1;
}
break;
case IPPROTO_UDP:
port = UDPH(l4)->source;
UDPH(l4)->source = ct->tuplehash[!dir].tuple.dst.u.all;
if (natflow_nat_port(skb, sizeof(struct ipv6hdr), IPPROTO_UDP, port, UDPH(l4)->source) != 0) {
return -1;
}
break;
default:
return -1;
}
if (natflow_nat_ipv6_l4proto(skb, ipv6h, sizeof(struct ipv6hdr), &ipv6h->saddr, &ct->tuplehash[!dir].tuple.dst.u3.in6) != 0) {
return -1;
}
ipv6h->saddr = ct->tuplehash[!dir].tuple.dst.u3.in6;
return 0;
}
static inline int natflow_do_dnat6(struct sk_buff *skb, struct nf_conn *ct, int dir) {
struct ipv6hdr *ipv6h;
void *l4;
__be16 port = 0;
ipv6h = ipv6_hdr(skb);
l4 = (void *)ipv6h + sizeof(struct ipv6hdr);
switch(ipv6h->nexthdr) {
case IPPROTO_TCP:
port = TCPH(l4)->dest;
TCPH(l4)->dest = ct->tuplehash[!dir].tuple.src.u.all;
if (natflow_nat_port(skb, sizeof(struct ipv6hdr), IPPROTO_TCP, 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, sizeof(struct ipv6hdr), IPPROTO_UDP, port, UDPH(l4)->dest) != 0) {
return -1;
}
break;
default:
return -1;
}
if (natflow_nat_ipv6_l4proto(skb, ipv6h, sizeof(struct ipv6hdr), &ipv6h->daddr, &ct->tuplehash[!dir].tuple.src.u3.in6) != 0) {
return -1;
}
ipv6h->daddr = ct->tuplehash[!dir].tuple.src.u3.in6;
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);
/* define IFF_PPPOE indicate pppoe dev (ref: include/uapi/linux/if.h) */
#define IFF_PPPOE (1<<25)
#define IFF_IFNAME_GROUP (1<<26)
#define IFF_VLINE_L2_PORT (1<<27)
#define IFF_VLINE_FAMILY_IPV4 (1<<28)
#define IFF_VLINE_FAMILY_IPV6 (1<<29)
#define IFF_IS_LAN (1<<30)
extern int ifname_group_type;
extern void ifname_group_clear(void);
extern int ifname_group_add(const unsigned char *ifname);
extern struct net_device *ifname_group_get(int idx);
#define VLINE_L3_PORT 0
#define VLINE_L2_PORT 1
extern unsigned char (*vline_fwd_map_config_get(unsigned int idx, unsigned char *family))[2][IFNAMSIZ];
extern int vline_fwd_map_config_add(const unsigned char *dst_ifname, const unsigned char *src_ifname, unsigned char family);
extern void vline_fwd_map_config_clear(void);
extern int vline_fwd_map_config_apply(void);
#define VLINE_FAMILY_ALL 0
#define VLINE_FAMILY_IPV4 1
#define VLINE_FAMILY_IPV6 2
extern void list_net_device_debug(void);
extern int natflow_path_init(void);
extern void natflow_path_exit(void);
#endif /* _NATFLOW_PATH_H_ */