-
Notifications
You must be signed in to change notification settings - Fork 456
/
Copy pathnat-gateway.sh
489 lines (449 loc) · 14.6 KB
/
nat-gateway.sh
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#!/usr/bin/env bash
iptables_cmd=$(which iptables)
iptables_save_cmd=$(which iptables-save)
if iptables-legacy -t nat -S INPUT 1 2>/dev/null; then
# use iptables-legacy for centos 7
iptables_cmd=$(which iptables-legacy)
iptables_save_cmd=$(which iptables-legacy-save)
fi
function exec_cmd() {
cmd=${@:1:${#}}
$cmd
ret=$?
if [ $ret -ne 0 ]; then
>&2 echo "failed to exec command \"$cmd\""
exit $ret
fi
}
function check_inited() {
$iptables_save_cmd -t nat | grep SNAT_FILTER | grep SHARED_SNAT
if [ $? -ne 0 ]; then
>&2 echo "nat gateway not initialized"
exit 1
fi
}
function init() {
# run once is enough
$iptables_save_cmd | grep DNAT_FILTER && exit 0
# add static chain
# this also a flag to make sure init once
$iptables_cmd -t nat -N DNAT_FILTER
# add static chain
$iptables_cmd -t nat -N SNAT_FILTER
$iptables_cmd -t nat -N EXCLUSIVE_DNAT # floatingIp DNAT
$iptables_cmd -t nat -N EXCLUSIVE_SNAT # floatingIp SNAT
$iptables_cmd -t nat -N SHARED_DNAT
$iptables_cmd -t nat -N SHARED_SNAT
$iptables_cmd -t nat -A PREROUTING -j DNAT_FILTER
$iptables_cmd -t nat -A DNAT_FILTER -j EXCLUSIVE_DNAT
$iptables_cmd -t nat -A DNAT_FILTER -j SHARED_DNAT
$iptables_cmd -t nat -A POSTROUTING -j SNAT_FILTER
$iptables_cmd -t nat -A SNAT_FILTER -j EXCLUSIVE_SNAT
$iptables_cmd -t nat -A SNAT_FILTER -j SHARED_SNAT
}
function get_iptables_version() {
exec_cmd "$iptables_cmd --version"
}
function add_vpc_internal_route() {
# make sure inited
check_inited
for rule in $@
do
arr=(${rule//,/ })
cidr=${arr[0]}
nextHop=${arr[1]}
exec_cmd "ip route replace $cidr via $nextHop dev eth0"
done
}
function del_vpc_internal_route() {
# make sure inited
check_inited
for rule in $@
do
arr=(${rule//,/ })
cidr=${arr[0]}
exec_cmd "ip route del $cidr dev eth0"
done
}
function del_vpc_external_route() {
# make sure inited
check_inited
# never do this, if deleted, will cause error
# for rule in $@
# do
# arr=(${rule//,/ })
# cidr=${arr[0]}
# exec_cmd "ip route del $cidr dev net1"
# sleep 1
# exec_cmd "ip route del default dev net1"
# done
}
function add_eip() {
# make sure inited
check_inited
for rule in $@
do
eip=${rule}
eip_without_prefix=(${eip//\// })
exec_cmd "ip addr replace $eip dev net1"
exec_cmd "arping -I net1 -c 3 -D $eip_without_prefix"
done
}
function del_eip() {
# make sure inited
check_inited
for rule in $@
do
arr=(${rule//,/ })
eip=${arr[0]}
ipCidr=`ip addr show net1 | grep $eip | awk '{print $2 }'`
if [ -n "$ipCidr" ]; then
exec_cmd "ip addr del $ipCidr dev net1"
fi
done
}
function add_floating_ip() {
# make sure inited
check_inited
for rule in $@
do
arr=(${rule//,/ })
eip=(${arr[0]//\// })
internalIp=${arr[1]}
# check if already exist
$iptables_save_cmd | grep EXCLUSIVE_DNAT | grep -w "\-d $eip/32" | grep destination && exit 0
exec_cmd "$iptables_cmd -t nat -A EXCLUSIVE_DNAT -d $eip -j DNAT --to-destination $internalIp"
exec_cmd "$iptables_cmd -t nat -A EXCLUSIVE_SNAT -s $internalIp -j SNAT --to-source $eip"
done
}
function del_floating_ip() {
# make sure inited
check_inited
for rule in $@
do
arr=(${rule//,/ })
eip=(${arr[0]//\// })
internalIp=${arr[1]}
# check if already exist
$iptables_save_cmd | grep EXCLUSIVE_DNAT | grep -w "\-d $eip/32" | grep destination
if [ "$?" -eq 0 ];then
exec_cmd "$iptables_cmd -t nat -D EXCLUSIVE_DNAT -d $eip -j DNAT --to-destination $internalIp"
exec_cmd "$iptables_cmd -t nat -D EXCLUSIVE_SNAT -s $internalIp -j SNAT --to-source $eip"
conntrack -D -d $eip 2>/dev/nul || true
fi
done
}
function add_snat() {
# make sure inited
check_inited
# iptables -t nat -F SHARED_SNAT
for rule in $@
do
arr=(${rule//,/ })
eip=(${arr[0]//\// })
internalCIDR=${arr[1]}
randomFullyOption=${arr[2]}
# check if already exist
$iptables_save_cmd | grep SHARED_SNAT | grep "\-s $internalCIDR" | grep "source $eip" && exit 0
exec_cmd "$iptables_cmd -t nat -A SHARED_SNAT -o net1 -s $internalCIDR -j SNAT --to-source $eip $randomFullyOption"
done
}
function del_snat() {
# make sure inited
check_inited
# iptables -t nat -F SHARED_SNAT
for rule in $@
do
arr=(${rule//,/ })
eip=(${arr[0]//\// })
internalCIDR=${arr[1]}
# check if already exist
ruleMatch=$($iptables_save_cmd | grep SHARED_SNAT | grep "\-s $internalCIDR" | grep "source $eip")
if [ "$?" -eq 0 ];then
ruleMatch=$(echo $ruleMatch | sed 's/-A //')
exec_cmd "$iptables_cmd -t nat -D $ruleMatch"
fi
done
}
function add_dnat() {
# make sure inited
check_inited
for rule in $@
do
arr=(${rule//,/ })
eip=(${arr[0]//\// })
dport=${arr[1]}
protocol=${arr[2]}
internalIp=${arr[3]}
internalPort=${arr[4]}
# check if already exist
$iptables_save_cmd | grep SHARED_DNAT | grep -w "\-d $eip/32" | grep "p $protocol" | grep -w "dport $dport"| grep -w "destination $internalIp:$internalPort" && exit 0
exec_cmd "$iptables_cmd -t nat -A SHARED_DNAT -p $protocol -d $eip --dport $dport -j DNAT --to-destination $internalIp:$internalPort"
done
}
function del_dnat() {
# make sure inited
check_inited
for rule in $@
do
arr=(${rule//,/ })
eip=(${arr[0]//\// })
dport=${arr[1]}
protocol=${arr[2]}
internalIp=${arr[3]}
internalPort=${arr[4]}
# check if already exist
$iptables_save_cmd | grep SHARED_DNAT | grep -w "\-d $eip/32" | grep "p $protocol" | grep -w "dport $dport"| grep -w "destination $internalIp:$internalPort"
if [ "$?" -eq 0 ];then
exec_cmd "$iptables_cmd -t nat -D SHARED_DNAT -p $protocol -d $eip --dport $dport -j DNAT --to-destination $internalIp:$internalPort"
fi
done
}
# example usage:
# delete_tc_u32_filter "net1" "1:0" "192.168.1.1" "src"
function delete_tc_u32_filter() {
dev=$1
qdisc_id=$2
cidr=$3
matchDirection=$4
# tc -p -s -d filter show dev net1 parent $qdisc_id
# filter protocol ip pref 10 u32 chain 0
# filter protocol ip pref 10 u32 chain 0 fh 800: ht divisor 1
# filter protocol ip pref 10 u32 chain 0 fh 800::800 order 2048 key ht 800 bkt 0 *flowid :1 not_in_hw
# match IP dst 172.18.11.2/32
# police 0x1 rate 10Mbit burst 10Mb mtu 2Kb action drop overhead 0b linklayer ethernet
# ref 1 bind 1 installed 47118 sec used 47118 sec firstused 18113444 sec
# Sent 0 bytes 0 pkts (dropped 0, overlimits 0)
# get the corresponding filterID by the EIP, and use the filterID to delete the corresponding filtering rule.
ipList=$(tc -p -s -d filter show dev $dev parent $qdisc_id | grep -E "match IP src|dst ([0-9]{1,3}\.){3}[0-9]{1,3}/[0-9]{1,2}")
i=0
echo "$ipList" | while read line; do
i=$((i+1))
if echo "$line" | grep "$matchDirection $cidr"; then
result=$(tc -p -s -d filter show dev $dev parent $qdisc_id | grep "filter protocol ip pref [0-9]\+ u32 \(fh\|chain [0-9]\+ fh\) \(\w\+::\w\+\) *" | awk '{print $5,$10}' | sed -n $i"p")
arr=($result)
pref=${arr[0]}
filterID=${arr[1]}
exec_cmd "tc filter del dev $dev parent $qdisc_id protocol ip prio $pref handle $filterID u32"
break
fi
done
}
function eip_ingress_qos_add() {
# ingress:
# external --> net1 --> qos -->
# dst ip is iptables eip on net1
for rule in $@
do
arr=(${rule//,/ })
v4ip=(${arr[0]//\// })
priority=${arr[1]}
rate=${arr[2]}
burst=${arr[3]}
dev="net1"
matchDirection="dst"
tc qdisc add dev $dev ingress 2>/dev/nul || true
# get qdisc id
qdisc_id=$(tc qdisc show dev $dev ingress | awk '{print $3}')
# del old filter
tc -p -s -d filter show dev $dev parent $qdisc_id | grep -w $v4ip
if [ "$?" -eq 0 ];then
delete_tc_u32_filter $dev $qdisc_id $v4ip $matchDirection
fi
exec_cmd "tc filter add dev $dev parent $qdisc_id protocol ip prio $priority u32 match ip $matchDirection $v4ip police rate "$rate"Mbit burst "$burst"Mb drop flowid :1"
done
}
function eip_egress_qos_add() {
# egress:
# net1 --> qos --> external
# src ip is iptables eip on net1
for rule in $@
do
arr=(${rule//,/ })
v4ip=(${arr[0]//\// })
priority=${arr[1]}
rate=${arr[2]}
burst=${arr[3]}
qdisc_id="1:0"
matchDirection="src"
dev="net1"
tc qdisc add dev $dev root handle $qdisc_id htb 2>/dev/nul || true
# del old filter
tc -p -s -d filter show dev $dev parent $qdisc_id | grep -w $v4ip
if [ "$?" -eq 0 ];then
delete_tc_u32_filter $dev $qdisc_id $v4ip $matchDirection
fi
exec_cmd "tc filter add dev $dev parent $qdisc_id protocol ip prio $priority u32 match ip $matchDirection $v4ip police rate "$rate"Mbit burst "$burst"Mb drop flowid :1"
done
}
function qos_add() {
for rule in $@
do
IFS=',' read -r -a arr <<< "$rule"
local qdiscType=(${arr[0]})
local dev=${arr[1]}
local priority=${arr[2]}
local classifierType=${arr[3]}
local matchType=${arr[4]}
local matchDirection=${arr[5]}
local cidr=${arr[6]}
local rate=${arr[7]}
local burst=${arr[8]}
if [ "$qdiscType" == "ingress" ];then
tc qdisc add dev $dev ingress 2>/dev/null || true
# get qdisc id
qdisc_id=$(tc qdisc show dev $dev ingress | awk '{print $3}')
elif [ "$qdiscType" == "egress" ];then
qdisc_id="1:0"
tc qdisc add dev $dev root handle $qdisc_id htb 2>/dev/null || true
fi
if [ "$classifierType" == "u32" ];then
tc -p -s -d filter show dev $dev parent $qdisc_id | grep -w $cidr
if [ "$?" -ne 0 ];then
exec_cmd "tc filter add dev $dev parent $qdisc_id protocol ip prio $priority u32 match $matchType $matchDirection $cidr police rate "$rate"Mbit burst "$burst"Mb drop flowid :1"
fi
elif [ "$classifierType" == "matchall" ];then
tc -p -s -d filter show dev $dev parent $qdisc_id | grep -w matchall
if [ "$?" -ne 0 ];then
exec_cmd "tc filter add dev $dev parent $qdisc_id protocol ip prio $priority matchall action police rate "$rate"Mbit burst "$burst"Mb drop flowid :1"
fi
fi
done
}
function qos_del() {
for rule in $@
do
IFS=',' read -r -a arr <<< "$rule"
local qdiscType=(${arr[0]})
local dev=${arr[1]}
local priority=${arr[2]}
local classifierType=${arr[3]}
local matchType=${arr[4]}
local matchDirection=${arr[5]}
local cidr=${arr[6]}
local rate=${arr[7]}
local burst=${arr[8]}
if [ "$qdiscType" == "ingress" ];then
qdisc_id=$(tc qdisc show dev $dev ingress | awk '{print $3}')
if [ -z "$qdisc_id" ]; then
exit 0
fi
elif [ "$qdiscType" == "egress" ];then
qdisc_id="1:0"
fi
# if qdisc_id is empty, this means ingress qdisc is not added, so we don't need to delete filter.
if [ "$classifierType" == "u32" ];then
delete_tc_u32_filter $dev $qdisc_id $cidr $matchDirection
elif [ "$classifierType" == "matchall" ];then
tc -p -s -d filter show dev $dev parent $qdisc_id | grep -w matchall
if [ "$?" -eq 0 ];then
exec_cmd "tc filter del dev $dev parent $qdisc_id protocol ip prio $priority matchall"
fi
fi
done
}
function eip_ingress_qos_del() {
for rule in $@
do
arr=(${rule//,/ })
cidr=(${arr[0]//\// })
matchDirection="dst"
dev="net1"
qdisc_id=$(tc qdisc show dev $dev ingress | awk '{print $3}')
# if qdisc_id is empty, this means ingress qdisc is not added, so we don't need to delete filter.
if [ -n "$qdisc_id" ]; then
delete_tc_u32_filter $dev $qdisc_id $cidr $matchDirection
fi
done
}
function eip_egress_qos_del() {
for rule in $@
do
arr=(${rule//,/ })
cidr=(${arr[0]//\// })
matchDirection="src"
qdisc_id="1:0"
dev="net1"
delete_tc_u32_filter $dev $qdisc_id $cidr $matchDirection
done
}
rules=${@:2:${#}}
opt=$1
case $opt in
init)
echo "init $rules"
init $rules
;;
subnet-route-add)
echo "subnet-route-add $rules"
add_vpc_internal_route $rules
;;
subnet-route-del)
echo "subnet-route-del $rules"
del_vpc_internal_route $rules
;;
eip-add)
echo "eip-add $rules"
add_eip $rules
;;
eip-del)
echo "eip-del $rules"
del_eip $rules
;;
dnat-add)
echo "dnat-add $rules"
add_dnat $rules
;;
dnat-del)
echo "dnat-del $rules"
del_dnat $rules
;;
snat-add)
echo "snat-add $rules"
add_snat $rules
;;
snat-del)
echo "snat-del $rules"
del_snat $rules
;;
floating-ip-add)
echo "floating-ip-add $rules"
add_floating_ip $rules
;;
floating-ip-del)
echo "floating-ip-del $rules"
del_floating_ip $rules
;;
get-iptables-version)
echo "get-iptables-version $rules"
get_iptables_version $rules
;;
eip-ingress-qos-add)
echo "eip-ingress-qos-add $rules"
eip_ingress_qos_add $rules
;;
eip-egress-qos-add)
echo "eip-egress-qos-add $rules"
eip_egress_qos_add $rules
;;
eip-ingress-qos-del)
echo "eip-ingress-qos-del $rules"
eip_ingress_qos_del $rules
;;
eip-egress-qos-del)
echo "eip-egress-qos-del $rules"
eip_egress_qos_del $rules
;;
qos-add)
echo "qos-add $rules"
qos_add $rules
;;
qos-del)
echo "qos-del $rules"
qos_del $rules
;;
*)
echo "Usage: $0 [init|subnet-route-add|subnet-route-del|eip-add|eip-del|floating-ip-add|floating-ip-del|dnat-add|dnat-del|snat-add|snat-del] ..."
exit 1
;;
esac