| 1 |
So you're interested in hacking on NetworkManager? Here's some cool |
| 2 |
stuff you could do... |
| 3 |
|
| 4 |
|
| 5 |
Improve Shutdown of NetworkManager |
| 6 |
================================== |
| 7 |
|
| 8 |
NetworkManager quits when receiving SIGTERM. |
| 9 |
|
| 10 |
Currently, it stops iterating the GMainContext (g_main_loop_quit()) and performs |
| 11 |
some synchronous cleanup actions. |
| 12 |
|
| 13 |
That is problematic for the following reasons. |
| 14 |
|
| 15 |
- We generally avoid blocking operations in NetworkManager (except currently during shutdown). |
| 16 |
Hence it's normal at any time to have async operations pending. Async operations |
| 17 |
with glib basically mean that we will receive a callback from the mainloop. For that |
| 18 |
to work, we need to keep iterating the GMainContext. If we stop iterating, |
| 19 |
we cannot cleanup the pending operations and leak resources. It's not possible |
| 20 |
to free all resources, unless we iterate as long as we have pending operations. |
| 21 |
|
| 22 |
That is because even if you g_cancellable_cancel() an sync operation, you still |
| 23 |
get a callback. The fact that an async operation will always get (one) callback |
| 24 |
invocation is an important guarantee in glib. If we no longer have that guarantee, |
| 25 |
it would be effectively impossible to implement cancellation and proper cleanup |
| 26 |
and it would require to do that for all async operations (changing the guaranteed |
| 27 |
semantics of all async operations). |
| 28 |
|
| 29 |
Often it wouldn't matter whether we free all resources during shutdown. However, |
| 30 |
unless we have a strict policy and method for freeing all, we will inevitably |
| 31 |
leak resources where it does matter. |
| 32 |
|
| 33 |
It's anyway hard to move from a "running" state to a "shutdown" state. It's |
| 34 |
impossible to get right, if we have pending async operations that no longer can |
| 35 |
complete. |
| 36 |
|
| 37 |
- Once we stop iterating the mainloop, we also cannot make async operations anymore. |
| 38 |
This reduces our shutdown to blocking operations (or a string of async operations that |
| 39 |
get chained together to one blocking operation, e.g. by using a separate GMainContext). |
| 40 |
This is very limiting, also because it's getting really hard to do things in |
| 41 |
parallel (unless you strongly intertwine them or essentially re-implement a |
| 42 |
main loop). Doing things in parallel will be necessary, for example if deactivate |
| 43 |
two devices, then both should shutdown in parallel. |
| 44 |
|
| 45 |
The real problem is that our shutdown is really messy due to this. And this is a |
| 46 |
fundamental limitation of the current implementation. |
| 47 |
|
| 48 |
The solution will be the following. |
| 49 |
|
| 50 |
When we receive SIGTERM we go into shutdown mode. This may mean to reject new D-Bus |
| 51 |
requests and in general to move into a shutdown state. All the while we keep iterating |
| 52 |
the GMainContext, but we also start to tear down and cancel/complete pending operations. |
| 53 |
While we do that, we may need to start new async operations. For example, during |
| 54 |
shutdown we may want to kill dnsmasq, which itself is a new asynchronous operation. |
| 55 |
|
| 56 |
The API nm_shutdown_wait_obj_register_object() and family allow for things to register |
| 57 |
themselves to block shutdown. This works using weak pointers. Basically, NetworkManager will |
| 58 |
keep iterating the GMainContext as long as we have objects registered there. While shutting |
| 59 |
down, we expect those objects to complete and unregister themselves. |
| 60 |
|
| 61 |
Currently, our singleton objects (NM_DEFINE_SINGLETON_REGISTER) get unrefed after |
| 62 |
the `main()` functions. For some/all of those singletons, during SIGTERM we may |
| 63 |
want to register them as nm_shutdown_wait_obj_register_object() and unref them when |
| 64 |
we initiate the shutdown. |
| 65 |
Singletons also use weak pointers and can work together with nm_shutdown_wait_obj_register_object(). |
| 66 |
For that to work, we need that nobody is calling the singleton getter *after* shutdown |
| 67 |
starts. That means, instead of using the singleton getter, you need to get the reference |
| 68 |
from somebody. For example, NMDevice has a reference to a NMNetns and NMPlatform |
| 69 |
and should use those instead of NM_PLATFORM_GET(). For those singeltons that works |
| 70 |
this way (maybe all of them), the singleton getters only works reliably before |
| 71 |
shutdown starts. And no singleton getters work reliably after the main() function |
| 72 |
because singletons unref themselves. In general, avoid singleton getters and see |
| 73 |
that somebody hands you a reference. |
| 74 |
|
| 75 |
After NM_SHUTDOWN_TIMEOUT_MAX_MSEC we loose patience that it's taking too long. |
| 76 |
We now log a debug message about who is still blocking shutdown. |
| 77 |
We also cancel the cancellables from nm_shutdown_wait_obj_register_cancellable() |
| 78 |
and give NM_SHUTDOWN_TIMEOUT_ADDITIONAL_MSEC more time. If we then are still |
| 79 |
not complete, we log an error message about who is still blocking shutdown, |
| 80 |
and just exit with an assertion failure. We encountered a bug. |
| 81 |
|
| 82 |
This means, *all* async operations in NetworkManager must either be cancellable (and |
| 83 |
afterwards complete fast) or they must not take long to begin with. In particular, |
| 84 |
every individual async operation must be completed in at most NM_SHUTDOWN_TIMEOUT_MAX_MSEC, |
| 85 |
and all async cleanup operations must complete in NM_SHUTDOWN_TIMEOUT_MAX_MSEC too. |
| 86 |
|
| 87 |
So if you make an async operation not cancellable, but guarantee that you don't take |
| 88 |
longer than NM_SHUTDOWN_TIMEOUT_MAX_MSEC you are mostly fine (better would be to actually |
| 89 |
complete fast, if you can). That's why reaching NM_SHUTDOWN_TIMEOUT_MAX_MSEC timeout is |
| 90 |
still not a bug scenario. But reaching NM_SHUTDOWN_TIMEOUT_MAX_MSEC+NM_SHUTDOWN_TIMEOUT_ADDITIONAL_MSEC |
| 91 |
is a bug. |
| 92 |
|
| 93 |
As NM_SHUTDOWN_TIMEOUT_MAX_MSEC and nm_shutdown_wait_obj_register_object() API already exists, |
| 94 |
the first step is to ensure that all parts of NetworkManager can be shutdown and be terminated |
| 95 |
in a timely manner. |
| 96 |
|
| 97 |
The second step is to replace the current sync cleanup operations with iterating the |
| 98 |
GMainContext. This is gonna be difficult. |
| 99 |
|
| 100 |
Search for `FIXME(shutdown` for places that are related to this effort and that need |
| 101 |
consideration. |
| 102 |
|
| 103 |
|
| 104 |
Use netlink API instead of ioctl based ethtool |
| 105 |
============================================== |
| 106 |
|
| 107 |
NetworkManager uses ethtool API to set/obtain certain settings of network |
| 108 |
devices. This is an ioctl based API and implmented in "src/platform/nm-platform-utils.c". |
| 109 |
Recently, kernel got a netlink API for the same functionality |
| 110 |
(https://www.kernel.org/doc/html/latest/networking/ethtool-netlink.html). |
| 111 |
NetworkManager should use this API if present, and fallback to the old API |
| 112 |
when running on older kernels. The benefit if this is that netlink provides |
| 113 |
notifications when settings change. The ethtool command line tool |
| 114 |
also implements this API, however it is under an incompatible license, |
| 115 |
so better don't look and make sure not to use the code. |
| 116 |
|
| 117 |
|
| 118 |
Add 802-1x capability to nmtui |
| 119 |
============================== |
| 120 |
|
| 121 |
Add dialogs to nmtui for 802-1x. This will be useful for ethernet (with 802-1x |
| 122 |
port authentication), enterprise Wi-Fi and MACSec. From the GUI and dialog design, |
| 123 |
possibly get inspired by nm-connection-editor. |
| 124 |
|
| 125 |
|
| 126 |
Ethernet Network Auto-detection |
| 127 |
=============================== |
| 128 |
|
| 129 |
There are various methods we can use to autodetect which wired network connection |
| 130 |
to use if the user connects to more than one wired network on a frequent basis. |
| 131 |
First, 802.1x enterprise switches broadcast frames which can be used to indicate |
| 132 |
that the switch supports 802.1x and thus allow NetworkManager to select an |
| 133 |
802.1x connection instead of blindly trying DHCP. Second, NetworkManager could |
| 134 |
listen for traffic from a list of MAC addresses. Third, NetworkManager could |
| 135 |
integrate 'arping' functionality to determine if a given IP address matches a |
| 136 |
given MAC address, and thus automatically select that connection. All these |
| 137 |
methods can co-exist and be used in parallel. |
| 138 |
|
| 139 |
One small caveat is that MAC addresses are trivial to spoof, so just because |
| 140 |
NetworkManager has discovered a certain MAC address does not mean the network |
| 141 |
is authenticated; only 802.1x security can assure that a network is the network |
| 142 |
the user expects it to be. |
| 143 |
|
| 144 |
In any case, a new 'anchor-addresses' property of type string-array should be |
| 145 |
added to the NMSettingWired setting. Each string element of this property |
| 146 |
should be of the format "<ip>/<mac>" or simply "<mac>". The first format with |
| 147 |
an IP address would indicate that "arping"-type behavior should be used to |
| 148 |
actively detect the given MAC address; obviously if the given MAC address is |
| 149 |
used for passive discovery as well. The second format simply lists a MAC |
| 150 |
address to passively listen for. |
| 151 |
|
| 152 |
One drawback of listening or probing for known MAC addresses is an increase in |
| 153 |
latency during connections to ethernet networks. The probing/listening delay |
| 154 |
should be a reasonable amount of time, like 4 - 5 seconds or so, and should |
| 155 |
only be used when a visible connection has anchor addresses. |
| 156 |
|
| 157 |
Next a gboolean 'anchor-probing' variable should be added to the |
| 158 |
NMDeviceEthernetPrivate structure in src/nm-device-ethernet.c. This variable |
| 159 |
should be set to TRUE whenever the device's carrier turns on *and* there are |
| 160 |
visible NMConnections with anchor addresses (ie, connections which are system- |
| 161 |
wide or where one of the allowed users of that connection is logged in). Then |
| 162 |
probing and listening are started, which involves opening a low-level socket |
| 163 |
on the interface and starting the arping run or listening for MAC addresses. |
| 164 |
A timer is also started (don't forget to cache the timer's source ID in the |
| 165 |
NMDeviceEthernetPrivate data, and to cancel the timer whenever the device |
| 166 |
transitions to any state other than DISCONNECTED). |
| 167 |
|
| 168 |
If a known MAC address is discovered as a result of probing or listening, the |
| 169 |
probe/listen socket, timeout, and data are cleaned up, and NetworkManager |
| 170 |
would begin activation of the NMConnection that specified the found MAC address |
| 171 |
in the 'anchor-addresses' property. If two or more connections specify the |
| 172 |
same MAC address, the connection with the most recent timestamp should be |
| 173 |
preferred. |
| 174 |
|
| 175 |
Similarly, if the probing/listening process detects 802.1x frames the device |
| 176 |
should be marked as requiring 802.1x authentication until the carrier drops. |
| 177 |
This would be accomplished by adding a new property to the NMDeviceEthernet |
| 178 |
object and exporting that property through the |
| 179 |
introspection/nm-device-ethernet.xml file. This would allow clients like |
| 180 |
applets to ensure that users are aware that the device will not allow |
| 181 |
un-authenticated connections and that additional credentials are required to |
| 182 |
successfully connect to this network. |
| 183 |
|
| 184 |
|
| 185 |
VPN re-connect (bgo #349151) |
| 186 |
============================ |
| 187 |
|
| 188 |
NM should remember whether a VPN was connected if a connection disconnects |
| 189 |
(like Wi-Fi drops out or short carrier drop) or if the laptop goes to sleep. |
| 190 |
Upon reconnect, if the same Connection is again active, the previously |
| 191 |
connected VPN should be activated again as well. Basically, don't just drop |
| 192 |
the VPN because Wi-Fi choked for 10 seconds, but reconnect the VPN if it was |
| 193 |
connected before the drop. |
| 194 |
|
| 195 |
|
| 196 |
VPN IP Methods |
| 197 |
============== |
| 198 |
|
| 199 |
Some VPNs (openvpn with TAP for example) require that DHCP is run on a |
| 200 |
pseudo-ethernet device to obtain addressing information. Currenty, this is not |
| 201 |
possible, but NM already has all the code for DHCP. Thus, a new "method" |
| 202 |
key should be defined in include/NetworkManagerVPN.h to allow for DHCP to |
| 203 |
be performed if the VPN service daemon requests it in the IP4Config or IP6Config |
| 204 |
signals. In nm-vpn-connection.c, upon receipt of the D-Bus Ip4Config signal |
| 205 |
from the VPN plugin, NetworkManager would inspect the "method" property of the |
| 206 |
ip4 config dictionary. If that property was present and set to "auto" then |
| 207 |
DHCP would be started using the network interface returned in the dict. The |
| 208 |
nm_vpn_connection_ip4_config_get() function should be split up into two |
| 209 |
functions, one containing the existing code for static configuration, and a |
| 210 |
second for handling DHCP kickoff. Minimal parsing of the response should be |
| 211 |
handled in the newly reduced nm_vpn_connection_ip4_config_get() function. |
| 212 |
|
| 213 |
To handle DHCP, the NMVPNConnectionPrivate structure should have two members |
| 214 |
added: |
| 215 |
|
| 216 |
NMDHCPManager *dhcp_manager; |
| 217 |
NMDHCPClient *dhcp4_client; |
| 218 |
|
| 219 |
which would be initialized in the new DHCP handler code split off from |
| 220 |
nm_vpn_connection_ip4_config_get(). These new members would be disposed of in |
| 221 |
both vpn_cleanup() and dispose(), though remember to stop any ongoing DHCP |
| 222 |
transaction when doing so (see dhcp4_cleanup() in nm-device.c for example code). |
| 223 |
For basic code to start the DHCP transaction, see dhcp4_start() in nm-device.c |
| 224 |
as well. After calling nm_dhcp_manager_start_ip4() and connecting the signals |
| 225 |
to monitor success and failure, the VPN IP4 config handler would simply return |
| 226 |
without changing VPN state, unless a failure occurred. |
| 227 |
|
| 228 |
Then, when the DHCP transaction succeeds, which we'd know by checking the |
| 229 |
DHCP client state changes in the "state-changed" signal handler we attached to |
| 230 |
the DHCP client object returned from nm_dhcp_manager_start_ip4(), the code |
| 231 |
would retrieve the completed NMIP4Config object from the DHCP client using the |
| 232 |
nm_dhcp_client_get_ip4_config() function, and then proceed to execute |
| 233 |
essentially the bottom-half of the existing nm_vpn_connection_ip4_config_get() |
| 234 |
function to merge that config with user overrides and apply it to the VPN |
| 235 |
tunnel interface. Other state changes from the DHCP client might trigger a |
| 236 |
failure of the VPN connection, just like DHCP timeouts and lease-renewal |
| 237 |
failures do for other devices (see dhcp_state_changed() in nm-device.c). |
| 238 |
|
| 239 |
|
| 240 |
VPN Service Daemon Secret Requests |
| 241 |
================================== |
| 242 |
|
| 243 |
In addition to NM asking the service daemons whether more secrets are required, |
| 244 |
VPN service daemons (like nm-vpnc-service, nm-openvpn-service, etc) should be |
| 245 |
able to ask NetworkManager to provide secrets during the connection attempt. To |
| 246 |
do this, the plugin should advertise its ability to handle out-of-band secrets |
| 247 |
in its .service file via the key 'async-secrets=true'. NetworkManager would |
| 248 |
check that key and if present activate the VPN as normal, but skip the explicit |
| 249 |
NeedSecrets calls. |
| 250 |
|
| 251 |
Instead, a new "SecretsRequired" signal would be added to |
| 252 |
introspection/nm-vpn-plugin.xml (and corresponding helper code added to |
| 253 |
libnm-glib/nm-vpn-plugin.c) that would be emitted when the plugin determined |
| 254 |
that secrets were required. This signal would have D-Bus signature of "sas" |
| 255 |
for the arguments [ <s:uuid>, <as:secrets> ] with the <uuid> obviously being |
| 256 |
the connection UUID, and <secrets> being an array of strings of plugin-specific |
| 257 |
strings the plugin requires secrets for. This array of strings would then be |
| 258 |
passed as the "hints" parameter in nm-vpn-connection.c when secrets are |
| 259 |
requested from agents in a subsequent nm_settings_connection_get_secrets() call. |
| 260 |
At this time the agent code only allows one hint per request, so we may need to |
| 261 |
extend that to allow more than one hint. |
| 262 |
|
| 263 |
Thus when connecting if the plugin supported async secrets NetworkManager would |
| 264 |
still request existing secrets (without interactivity) and send them to the |
| 265 |
VPN service daemon in the Connect D-Bus method, then wait for the service daemon |
| 266 |
to either request secrets asynchronously via the SecretsRequired signal or to |
| 267 |
signal successful connection via the Ip4Config signal. |
| 268 |
|
| 269 |
The vpnc plugin would need to be reworked to open a pipe to vpnc's stdout and |
| 270 |
stdin file descriptors to capture any authentication messages, and to match |
| 271 |
these messages to known secrets request strings. When receiving one of these |
| 272 |
strings the plugin would determine which secret was being requested and then |
| 273 |
emit the SecretsRequired signal to NetworkManager. This would also imply that |
| 274 |
nm-vpnc-service exectutes vpnc with the "--xauth-inter" argument to enable |
| 275 |
challenge-response and does not use the "--non-inter" flag which suppresses that |
| 276 |
behavior. |
| 277 |
|
| 278 |
|
| 279 |
WPS |
| 280 |
=== |
| 281 |
|
| 282 |
wpa_supplicant has support for WPS (Wifi Protected Setup, basically Bluetooth- |
| 283 |
like PIN codes for setting up a wifi connection) and we should add support for |
| 284 |
this to NetworkManager too. APs that support WPS will say so in their beacon |
| 285 |
IEs which are contained in the "WPA" and "RSN" properties of the BSS object |
| 286 |
exported by the supplicant, and can be processed in src/nm-wifi-ap.c's |
| 287 |
foreach_property_cb() function. We should add some private fields to the |
| 288 |
NMAccessPoint object (defined in nm-wifi-ap.c) to remember whether a specific |
| 289 |
AP supports WPS and what WPS methods it supports, and expose that over D-Bus to |
| 290 |
GUI clients as well. |
| 291 |
|
| 292 |
There are two common WPS setup methods: PIN and button. For PIN, the router |
| 293 |
either displays a random PIN on an LCD or the router's web UI, or a static PIN |
| 294 |
is printed on the router itself. The user enters that PIN instead of a PSK |
| 295 |
when connecting. For the "button" method, the router has a physical button that |
| 296 |
when pushed, allows any client to connect for a short period of time. |
| 297 |
|
| 298 |
We'll then need to add some properties to the NMSettingWirelessSecurity setting |
| 299 |
for the WPS PIN code so that when the user enters it through the GUI, it can |
| 300 |
be passed back to NM. And we'll need to figure out some mechanism for passing |
| 301 |
back an indication that the user pushed the button on the router for the |
| 302 |
pushbutton method. |
| 303 |
|
| 304 |
When connecting to a new access point that supports WPS, the GUI client would |
| 305 |
call the AddAndActivateConnection method and wait for NM to request secrets. |
| 306 |
NM would determine that the AP supports WPS, and request WPS secrets from the |
| 307 |
applet. The applet would ask the user for a PIN, or to push the button on the |
| 308 |
AP, instead of asking for a passphrase or PSK. When the user has entered the |
| 309 |
PIN or pushed the button, the applet returns this information to NM, which |
| 310 |
proceeds with the connection. |
| 311 |
|
| 312 |
NM sends the correct wpa_supplicant config for WPS to the supplicant, and waits |
| 313 |
for the connection to occur. WPS can only be used the *first* time, so after a |
| 314 |
first successful connection, NM must request the actual hexadecimal PSK from |
| 315 |
wpa_supplicant via D-Bus, and store that PSK in the connection, clear any WPS |
| 316 |
PIN code from the connection, and save the connection to backing storage. |
| 317 |
|
| 318 |
Any applet GUI should also allow the user to enter the PSK instead of completing |
| 319 |
association using WPS, since quite a few routers out there are broken, or |
| 320 |
because the user has no physical access to the router itself, but has been given |
| 321 |
as passphrase/PSK instead. |
| 322 |
|
| 323 |
|
| 324 |
Better Tablet/Mobile Behavior |
| 325 |
============================= |
| 326 |
|
| 327 |
There are a few components to this: |
| 328 |
|
| 329 |
1) kernel driver and hardware capabilities: most mobile devices use periodic |
| 330 |
background scanning to quickly determine whether a known SSID is available and |
| 331 |
notify the connection manager to connect to it. This typically requires special |
| 332 |
capabilities and good powersave/sleep support from the Wi-Fi kernel driver. |
| 333 |
There is a background scanning API in nl80211, but we need to determine how many |
| 334 |
SSIDs each driver allows for background scanning, and based on that number, give |
| 335 |
the driver the most recent N SSIDs. We still need to periodically wake the |
| 336 |
device up and do a full scan just in case the user is near a known SSID that was |
| 337 |
not in the N top recently used networks. This is also beneficial to normal |
| 338 |
desktop use-cases. |
| 339 |
|
| 340 |
wpa_supplicant doesn't currently provide an explicit interface for sending SSIDs |
| 341 |
to the driver for background scanning, but could simply send a list using |
| 342 |
configured networks. However, NM currently does not send *all* configured |
| 343 |
connections' SSIDs to the supplicant, so that's something we should do first |
| 344 |
to optimize connection times. To do this, NM would need to order all networks |
| 345 |
using the NM timestamp and convert that into a supplicant priority number, which |
| 346 |
would need to be adjusted periodically when the timestamp was updated. This |
| 347 |
would involve tracking each network (exposed by the supplicant as a D-Bus |
| 348 |
object) and making sure they were added, deleted, and updated when the backing |
| 349 |
NMConnection objects changed. One complication is that the supplicant |
| 350 |
requires secrets for various network types when the network is added via D-Bus, |
| 351 |
and NetworkManager might not have those secrets yet. We may need to modify |
| 352 |
the supplicant allow for *all* secrets (PSKs, WEP keys, etc) to be requested |
| 353 |
on-demand, not just EAP secrets like 802.1x passwords. We then need to fix |
| 354 |
up the supplicant's D-Bus interface to actually send requests for secrets out |
| 355 |
over D-Bus (like wpa_supplicant_eap_param_needed() does for the socket-based |
| 356 |
control interface) and to handle the resulting reply from a D-Bus client like |
| 357 |
wpa_supplicant_ctrl_iface_ctrl_rsp() does. |
| 358 |
|
| 359 |
With the secrets request stuff and priority handling in place, wpa_supplicant |
| 360 |
would control network selection and roaming (based on the priorities NM gave it |
| 361 |
of course) instead of NetworkManager itself, and hopefully lead to a faster WiFi |
| 362 |
connection process. |
| 363 |
|
| 364 |
2) single-device-at-a-time with overlapping connections: this is also probably |
| 365 |
the best route to go for desktop use-cases as well. Instead of bringing all |
| 366 |
available connections up, only bring up the "best" connection at any given |
| 367 |
time based on the current priority list (which is roughly Ethernet > Wi-Fi > |
| 368 |
3G/Bluetooth). However, to ensure seamless connectivity, when one connection |
| 369 |
begins to degrade, the next-best connection should be started before the |
| 370 |
current one is terminated, such that there is a small amount of overlap. |
| 371 |
Consequently the same behavior should be used when a better connection becomes |
| 372 |
available. This behavior should be suspended when special connections like |
| 373 |
Internet Connection Sharing ones are started, where clearly the priorities |
| 374 |
are different (ie, for Mobile Hotspot 3G > WiFi). |