policy-routing: take ownership of externally configured rules

System Internals / NetworkManager - Thomas Haller [redhat.com] - 16 July 2019 08:16 EDT

IP addresses, routes, TC and QDiscs are all tied to a certain interface. So when NetworkManager manages an interface, it can be confident that all related entires should be managed, deleted and modified by NetworkManager.

Routing policy rules are global. For that we have NMPRulesManager which keeps track of whether NetworkManager owns a rule. This allows multiple connection profiles to specify the same rule, and NMPRulesManager can consolidate this information to know whether to add or remove the rule.

NMPRulesManager would also support to explicitly block a rule by tracking it with negative priority. However that is still unused at the moment. All that devices do is to add rules (track with positive priority) and remove them (untrack) once the profile gets deactivated.

As rules are not exclusively owned by NetworkManager, NetworkManager tries not to interfere with rules that it knows nothing about. That means in particular, when NetworkManager starts it will "weakly track" all rules that are present. "weakly track" is mostly interesting for two cases:

- when NMPRulesManager had the same rule explicitly tracked (added) by a device, then deactivating the device will leave the rule in place.

- when NMPRulesManager had the same rule explicitly blocked (tracked with negative priority), then it would restore the rule when that block gets removed (as said, currently nobody actually does this).

Note that when restarting NetworkManager, then the device may stay and the rules kept. However after restart, NetworkManager no longer knows that it previously added this route, so it would weakly track it and never remove them again.

That is a problem. Avoid that, by whenever explicitly tracking a rule we also make sure to no longer weakly track it. Most likely this rule was indeed previously managed by NetworkManager. If this was really a rule added by externally, then the user really should choose distinct rule priorities to avoid such conflicts altogether.

15b130447 policy-routing: take ownership of externally configured rules
src/devices/nm-device.c | 7 ++-
src/nm-netns.c | 16 +++++-
src/platform/nmp-rules-manager.c | 116 ++++++++++++++++++++++++++++++++-------
src/platform/nmp-rules-manager.h | 5 +-
src/platform/tests/test-route.c | 12 ++--
5 files changed, 128 insertions(+), 28 deletions(-)

Upstream: cgit.freedesktop.org

  • Share