@@ -112,7 +112,7 @@ IPNet ConnectionTracker::NormalizeAddressNoLock(const Address& address, bool ena
112112}
113113
114114bool ConnectionTracker::ShouldNormalizeConnection (const Connection* conn) const {
115- Endpoint local, remote = conn->remote ();
115+ Endpoint remote = conn->remote ();
116116 IPNet ipnet = NormalizeAddressNoLock (remote.address (), false );
117117
118118 return Address::IsCanonicalExternalIp (ipnet.address ());
@@ -136,30 +136,31 @@ void ConnectionTracker::CloseConnections(ConnMap* old_conn_state, ConnMap* delta
136136 }
137137}
138138
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);
147142
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+ };
157152
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+ });
163164 }
164165}
165166
@@ -171,15 +172,17 @@ Connection ConnectionTracker::NormalizeConnectionNoLock(const Connection& conn)
171172 }
172173
173174 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);
174177
175178 if (is_server) {
176179 // If this is the server, only the local port is relevant, while the remote port does not matter.
177180 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 );
179182 } else {
180183 // If this is the client, the local port and address are not relevant.
181184 local = Endpoint ();
182- remote = Endpoint (NormalizeAddressNoLock (remote.address (), enable_external_ips_ ), remote.port ());
185+ remote = Endpoint (NormalizeAddressNoLock (remote.address (), extIPs_egress ), remote.port ());
183186 }
184187
185188 return Connection (conn.container (), local, remote, conn.l4proto (), is_server);
0 commit comments