From cef880c66f24a18a30a02b583af3d85e245c359a Mon Sep 17 00:00:00 2001 From: Dylan Van Assche Date: Sat, 23 Jul 2022 08:39:37 +0200 Subject: [PATCH 1/2] nm-dispatcher: dispatch on reapply Trigger a dispatcher event when a connection is reapplied on a NM device. Some devices such as phones have already a DHCP client running for accepting connections when they are plugged into USB to transfer data over SSH. When NetworkManager switches the connection IP method to shared, it spawns a dnsmasq process to handle DHCP and DNS for that connection. However, a dispatcher event is needed to disable the external DHCP server for these USB connections as NetworkManager's dnsmasq handles them now. Moreover, when the connection method is switched to a different mode, the external DHCP server needs to be spawned again to make sure that SSH connections are still possible to the device. To achieve this, add a new NetworkManager Dispatcher event 'reapply' which is triggered when a connection is reapplied on a NM device. This way, a dispatcher script can handle the case above by inspecting the IP method in the dispatcher script. --- src/core/devices/nm-device.c | 4 ++++ src/core/nm-dispatcher.c | 3 ++- src/core/nm-dispatcher.h | 3 ++- src/libnm-core-aux-extern/nm-dispatcher-api.h | 1 + 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/core/devices/nm-device.c b/src/core/devices/nm-device.c index 248f2364f..db0d2dd24 100644 --- a/src/core/devices/nm-device.c +++ b/src/core/devices/nm-device.c @@ -13041,6 +13041,10 @@ check_and_reapply_connection(NMDevice *self, if (priv->state >= NM_DEVICE_STATE_ACTIVATED) nm_device_update_metered(self); + /* Notify dispatcher when re-applied */ + _LOGD(LOGD_DEVICE, "Notifying re-apply complete"); + nm_dispatcher_call_device(NM_DISPATCHER_ACTION_REAPPLY, self, NULL, NULL, NULL, NULL); + return TRUE; } diff --git a/src/core/nm-dispatcher.c b/src/core/nm-dispatcher.c index ab361faa7..01a638260 100644 --- a/src/core/nm-dispatcher.c +++ b/src/core/nm-dispatcher.c @@ -468,7 +468,8 @@ static const char *action_table[] = {[NM_DISPATCHER_ACTION_HOSTNAME] = NMD_ [NM_DISPATCHER_ACTION_DHCP_CHANGE_4] = NMD_ACTION_DHCP4_CHANGE, [NM_DISPATCHER_ACTION_DHCP_CHANGE_6] = NMD_ACTION_DHCP6_CHANGE, [NM_DISPATCHER_ACTION_CONNECTIVITY_CHANGE] = - NMD_ACTION_CONNECTIVITY_CHANGE}; + NMD_ACTION_CONNECTIVITY_CHANGE, + [NM_DISPATCHER_ACTION_REAPPLY] = NMD_ACTION_REAPPLY}; static const char * action_to_string(NMDispatcherAction action) diff --git a/src/core/nm-dispatcher.h b/src/core/nm-dispatcher.h index 73e0599a7..307163992 100644 --- a/src/core/nm-dispatcher.h +++ b/src/core/nm-dispatcher.h @@ -21,7 +21,8 @@ typedef enum { NM_DISPATCHER_ACTION_VPN_DOWN, NM_DISPATCHER_ACTION_DHCP_CHANGE_4, NM_DISPATCHER_ACTION_DHCP_CHANGE_6, - NM_DISPATCHER_ACTION_CONNECTIVITY_CHANGE + NM_DISPATCHER_ACTION_CONNECTIVITY_CHANGE, + NM_DISPATCHER_ACTION_REAPPLY, } NMDispatcherAction; #define NM_DISPATCHER_ACTION_DHCP_CHANGE_X(IS_IPv4) \ diff --git a/src/libnm-core-aux-extern/nm-dispatcher-api.h b/src/libnm-core-aux-extern/nm-dispatcher-api.h index 7776c84f9..56da5fba7 100644 --- a/src/libnm-core-aux-extern/nm-dispatcher-api.h +++ b/src/libnm-core-aux-extern/nm-dispatcher-api.h @@ -33,6 +33,7 @@ #define NMD_ACTION_DHCP4_CHANGE "dhcp4-change" #define NMD_ACTION_DHCP6_CHANGE "dhcp6-change" #define NMD_ACTION_CONNECTIVITY_CHANGE "connectivity-change" +#define NMD_ACTION_REAPPLY "reapply" typedef enum { DISPATCH_RESULT_UNKNOWN = 0, From 0e3d2c367c1cb640e79eba13a38feae0996507be Mon Sep 17 00:00:00 2001 From: Dylan Van Assche Date: Sun, 22 Jan 2023 19:35:35 +0100 Subject: [PATCH 2/2] man: NetworkManager-dispatcher: document reapply action NetworkManager dispatcher will now run dispatcher scripts with 'reapply' as action as well. Each time the connection is reapplied on a device, this action is triggered. Document this action in the documentation. --- man/NetworkManager-dispatcher.xml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/man/NetworkManager-dispatcher.xml b/man/NetworkManager-dispatcher.xml index 4a603b156..036b3c8dc 100644 --- a/man/NetworkManager-dispatcher.xml +++ b/man/NetworkManager-dispatcher.xml @@ -164,6 +164,12 @@ The network connectivity state has changed (no connectivity, went online, etc). + + reapply + + The connection was reapplied on the device. + + The environment contains more information about the interface and the connection.