forked from Kentzo/routeros-scripts-custom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipv6-npt.rsc
151 lines (135 loc) · 4.65 KB
/
ipv6-npt.rsc
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
#!rsc by RouterOS
#
# Set up NPTv6.
#
# GUA prefix is reserved by allocating a network from $argWanPool
# on $argLoopbackInt. Once GUA network is available, its address-prefix
# is used to set up mangling rules to perform network prefix translation
# with addresses from $argUlaPool.
#
# Since mangling is performed after connection tracking, all mangled packets
# continue processing with connection-state=invalid. Thus raw rules are added
# to mark these packets for notrack. Adjust your filter rules accordingly.
#
# Optional arg*Extra can be used to further narrow matching criteria.
# E.g. to perform translation only when the packet is routed through WAN:
#
# :global argSnptMangleExtra {"out-interface-list"="WAN"}
#
#
# Arguments:
# argLoopbackInt (str): name of the loopback interface
# argWanPool (str): name of the WAN pool
# argUlaPool (str): name of the ULA pool
# argManagedID (str): regex-escaped unique ID of the managed objects
# [argSnptMangleExtra] (array): Optional extra properties for the SNPT mangle rule
# [argDnptMangleExtra] (array): Optional extra properties for the DNPT mangle rule
# [argSnptRawExtra] (array): Optional extra properties for the SNPT raw rule
# [argDnptRawExtra] (array): Optional extra properties for the DNPT raw rule
#
#
# Affects:
# /ipv6/address
# /ipv6/firewall/mangle
# /ipv6/firewall/raw
#
#
# Requirements:
# - mod/ipv6-functions
:global GlobalFunctionsReady;
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
:local SetupRules do={
:global argLoopbackInt
:global argWanPool
:global argManagedID
:global argSnptMangleExtra
:global argDnptMangleExtra
:global argSnptRawExtra
:global argDnptRawExtra
:global WaitIP6Address
:global SetIfExistsElseAdd
:global SetIfExistsElseAddUnlessEqual
$SetIfExistsElseAddUnlessEqual /ipv6/address\
({"comment~\"$argManagedID\\\$\""})\
({\
interface="$argLoopbackInt";\
advertise=false;\
"from-pool"="$argWanPool"\
})\
({\
interface=$argLoopbackInt;\
advertise="no";\
"from-pool"=$argWanPool;\
comment="\"Managed: NPTv6 / $argManagedID\""\
})
:local varGuaPrefix [$WaitIP6Address $argLoopbackInt $2 ("$argManagedID\$")]
$SetIfExistsElseAdd /ipv6/firewall/mangle\
({"comment~\"snpt-$argManagedID\\\$\""})\
($argSnptMangleExtra , {\
chain="postrouting";\
action="snpt";\
"src-address"=$1;\
"src-prefix"=$1;\
"dst-prefix"=$varGuaPrefix;\
comment="\"Managed: NPTv6 / snpt-$argManagedID\""\
})
$SetIfExistsElseAdd /ipv6/firewall/mangle\
({"comment~\"dnpt-$argManagedID\\\$\""})\
($argDnptMangleExtra , {\
chain="prerouting";\
action="dnpt";\
"dst-address"=$varGuaPrefix;\
"src-prefix"=$varGuaPrefix;\
"dst-prefix"=$1;\
comment="\"Managed: NPTv6 / dnpt-$argManagedID\""\
})
$SetIfExistsElseAdd /ipv6/firewall/raw\
({"comment~\"snpt-$argManagedID\\\$\""})\
($argSnptRawExtra , {\
chain="prerouting";\
action="notrack";\
"src-address"=$1;\
comment="\"Managed: NPTv6 / snpt-$argManagedID\""\
})
$SetIfExistsElseAdd /ipv6/firewall/raw\
({"comment~\"dnpt-$argManagedID\\\$\""})\
($argDnptRawExtra , {\
chain="prerouting";\
action="notrack";\
"dst-address"=$varGuaPrefix;\
comment="\"Managed: NPTv6 / dnpt-$argManagedID\""\
})
}
:local TearDown do={
:global argManagedID
/ipv6/address/remove [find comment~"$argManagedID\$"]
/ipv6/firewall/mangle/remove [find comment~"$argManagedID\$"]
/ipv6/firewall/raw/remove [find comment~"$argManagedID\$"]
}
:global LogPrint
:global AssertNotEmpty
:global argLoopbackInt
:global argWanPool
:global argUlaPool
:global argManagedID
$AssertNotEmpty "argLoopbackInt" $argLoopbackInt
$AssertNotEmpty "argWanPool" $argWanPool
$AssertNotEmpty "argUlaPool" $argUlaPool
$AssertNotEmpty "argManagedID" $argManagedID
/ipv6/pool
:local varWanPrefix [get value-name=prefix $argWanPool]
:local varUlaPrefix [get value-name=prefix $argUlaPool]
:do {
$SetupRules $varUlaPrefix $varWanPrefix
$LogPrint info $0 ("Add NPT: $varUlaPrefix <-> $varWanPrefix")
} on-error={
$LogPrint warning $0 ("Failed to update NPTv6, retrying from scratch")
:do {
$TearDown
$SetupRules $varUlaPrefix $varWanPrefix
} on-error={
$TearDown
$LogPrint error $0 ("Failed to set up NPTv6")
:error "fatal error in ipv6-npt.rsc"
}
}