@@ -112,7 +112,7 @@ IPNet ConnectionTracker::NormalizeAddressNoLock(const Address& address, bool ena
112
112
}
113
113
114
114
bool ConnectionTracker::ShouldNormalizeConnection (const Connection* conn) const {
115
- Endpoint local, remote = conn->remote ();
115
+ Endpoint remote = conn->remote ();
116
116
IPNet ipnet = NormalizeAddressNoLock (remote.address (), false );
117
117
118
118
return Address::IsCanonicalExternalIp (ipnet.address ());
@@ -136,30 +136,31 @@ void ConnectionTracker::CloseConnections(ConnMap* old_conn_state, ConnMap* delta
136
136
}
137
137
}
138
138
139
- /* *
140
- * Closes connections that have the 255.255.255.255 external IP address
141
- */
142
- void ConnectionTracker::CloseNormalizedConnections (ConnMap* old_conn_state, ConnMap* delta_conn) {
143
- CloseConnections (old_conn_state, delta_conn, [](const Connection* conn) {
144
- return Address::IsCanonicalExternalIp (conn->remote ().address ());
145
- });
146
- }
139
+ void ConnectionTracker::CloseConnectionsOnExternalIPsConfigChange (ExternalIPsConfig prev_config, ConnMap* old_conn_state, ConnMap* delta_conn) const {
140
+ bool ingress = external_ips_config_.IsEnabled (ExternalIPsConfig::Direction::INGRESS);
141
+ bool egress = external_ips_config_.IsEnabled (ExternalIPsConfig::Direction::EGRESS);
147
142
148
- /* *
149
- * Closes unnormalized connections that would be normalized to the canonical external
150
- * IP address if external IPs was enabled
151
- */
152
- void ConnectionTracker::CloseExternalUnnormalizedConnections (ConnMap* old_conn_state, ConnMap* delta_conn) {
153
- CloseConnections (old_conn_state, delta_conn, [ this ]( const Connection* conn) {
154
- return ShouldNormalizeConnection (conn) && !Address::IsCanonicalExternalIp (conn->remote ().address ());
155
- });
156
- }
143
+ auto should_close = [ this ]( const Connection* conn, bool enabling_extIPs) {
144
+ if (enabling_extIPs) {
145
+ // Enabling: Close connections previously normalized
146
+ return Address::IsCanonicalExternalIp (conn-> remote (). address ());
147
+ } else {
148
+ // Disabling: Close connections that should now be normalized
149
+ return !Address::IsCanonicalExternalIp (conn->remote ().address ()) && ShouldNormalizeConnection (conn );
150
+ }
151
+ };
157
152
158
- void ConnectionTracker::CloseConnectionsOnRuntimeConfigChange (ConnMap* old_conn_state, ConnMap* delta_conn, bool enableExternalIPs) {
159
- if (enableExternalIPs) {
160
- CloseNormalizedConnections (old_conn_state, delta_conn);
161
- } else {
162
- CloseExternalUnnormalizedConnections (old_conn_state, delta_conn);
153
+ if (egress != prev_config.IsEnabled (ExternalIPsConfig::Direction::EGRESS)) {
154
+ CloseConnections (old_conn_state, delta_conn, [egress, should_close](const Connection* conn) -> bool {
155
+ /* egress is when we are not server */
156
+ return !conn->is_server () && should_close (conn, egress);
157
+ });
158
+ }
159
+ if (ingress != prev_config.IsEnabled (ExternalIPsConfig::Direction::INGRESS)) {
160
+ CloseConnections (old_conn_state, delta_conn, [ingress, should_close](const Connection* conn) -> bool {
161
+ /* ingress is when we are server */
162
+ return conn->is_server () && should_close (conn, ingress);
163
+ });
163
164
}
164
165
}
165
166
@@ -171,15 +172,17 @@ Connection ConnectionTracker::NormalizeConnectionNoLock(const Connection& conn)
171
172
}
172
173
173
174
Endpoint local, remote = conn.remote ();
175
+ bool extIPs_ingress = external_ips_config_.IsEnabled (ExternalIPsConfig::Direction::INGRESS);
176
+ bool extIPs_egress = external_ips_config_.IsEnabled (ExternalIPsConfig::Direction::EGRESS);
174
177
175
178
if (is_server) {
176
179
// If this is the server, only the local port is relevant, while the remote port does not matter.
177
180
local = Endpoint (IPNet (Address ()), conn.local ().port ());
178
- remote = Endpoint (NormalizeAddressNoLock (conn.remote ().address (), enable_external_ips_ ), 0 );
181
+ remote = Endpoint (NormalizeAddressNoLock (conn.remote ().address (), extIPs_ingress ), 0 );
179
182
} else {
180
183
// If this is the client, the local port and address are not relevant.
181
184
local = Endpoint ();
182
- remote = Endpoint (NormalizeAddressNoLock (remote.address (), enable_external_ips_ ), remote.port ());
185
+ remote = Endpoint (NormalizeAddressNoLock (remote.address (), extIPs_egress ), remote.port ());
183
186
}
184
187
185
188
return Connection (conn.container (), local, remote, conn.l4proto (), is_server);
0 commit comments