Commit Graph

25184 Commits

Author SHA1 Message Date
Thomas Haller
3d2b982fb7 cli: fix out of bounds access in _print_fill()
cols_len might be larger than header_row->len. That is when
the cols has entries that are not leaf entries (which currently
I think is never the case).

Fix it to use the right variable for the length of the row.
2020-03-16 13:44:12 +01:00
Thomas Haller
5bef7d7453 cli: minor cleanup dropping unnecessary local variables 2020-03-16 13:44:12 +01:00
Thomas Haller
3cc99c9f8c cli: return typed PrintDataCol array from _output_selection_parse()
It makes debugging and understanding the code slightly simpler, if we
have a pointer of correct type, instead of returning a GArray. We don't
need the GArray at this point anymore.
2020-03-16 13:44:09 +01:00
Thomas Haller
a01355ba64 cli: add get_type argument to ap_wpa_rsn_flags_to_string() for optional i18n
Will be used later.
2020-03-16 13:40:51 +01:00
Thomas Haller
49dacaa34e cli: use slice allocator in do_device_wifi_list() and designated initializers for data 2020-03-16 13:40:51 +01:00
Thomas Haller
30cf1885d4 cli: cleanup selecting Wi-Fi device for nmcli device wifi list
Refactor the selection of the Wi-Fi device by name. Avoid
find_wifi_device_by_iface() to lookup by name. We already
have a sorted list of candidate devices. The ifname is just
an additional filter to exclude devices. So, we shouldn't
use find_wifi_device_by_iface(), but instead check our prepared
list of devices, whether it contains matching candidates.
2020-03-16 13:40:51 +01:00
Thomas Haller
e0e39a7452 cli: take reference in sort_access_points() for "clients/cli/devices.c"
It's not really necessary, but it feels slightly more correct. The only
reason not to take a reference is to safe the overhead of increasing and
decreasing the reference counter. But that doesn't matter for nmcli
at this point (and is tiny anyway). Let the API make sure that the instances
are kept alive.
2020-03-16 13:40:51 +01:00
Thomas Haller
ca4b530742 cli: use NM_CMP*() macros for compare_aps() in "clients/cli/devices.c"
The compare pattern seems simple, but seems error prone and subtle.
NM_CMP*() avoids that.

For example, nm_access_point_get_strength() returns an uint8_t.
C will promote those values to "int" before doing the subtraction.
Likewise, nm_access_point_get_frequency() returns a uint32_t. This
gets promoted to unsigned int when doing the subtraction. Afterwards,
that is converted to a signed int.
So both cases were in fact correct. But such things are not obvious.

Also, as fallback sort by D-Bus path. While that is not semantically
useful, we should use a defined sort order.
2020-03-16 13:40:51 +01:00
Thomas Haller
652de3b8d2 cli: use nm_utils_bin2hexstr_full() in ssid_to_hex()
We already have an implementation for converting a binary
array to hex. And, it doesn't require a GString for constructing
the output that has an known length.
2020-03-16 13:40:51 +01:00
Thomas Haller
b7e171c1f7 libnm: fix nm_vpn_plugin_info_list_find_service_type()
Of course, having no list does not mean we cannot resolve the service-type.
That is, because we also have a hard-coded list of known VPNs.

Fixes: 67c00353d3 ('libnm: reuse _list_find_by_service() for searching NMVpnPluginInfo')
2020-03-15 09:16:19 +01:00
Thomas Haller
6b07b40c46 libnm: hide NMVpnPluginInfo structs from public API
The NMVpnPluginInfo class is not intended to be subclassed. An API that
allows to be subclassed needs to be designed in a certain manner for
that to be useful. NMVpnPluginInfo does not want to support that.

Only because a user technically could do that (as the structs were
in the public headers), it does not make it supported. Not everything
that is possible in C is guaranteed to work.

Also, of course there exist no users in practice that would rely on this.
So, hide the structs.

Also, this allows to embed the private data in the GObject struct
itself, which is useful for debugging and for performance.
2020-03-14 18:04:05 +01:00
Thomas Haller
67c00353d3 libnm: reuse _list_find_by_service() for searching NMVpnPluginInfo 2020-03-14 17:52:08 +01:00
Thomas Haller
2b814f4e87 libnm: let nm_vpn_plugin_info_new_search_file() call full list-load
list-load does some special handling, for example, it will avoid adding
duplicates. As such, two plugin infos cannot have the same name or
same service type.

nm_vpn_plugin_info_new_search_file() did not implement this, it merely
loaded each directory after the other, sort the plugin infos, and
returned the first match.

That might mean, with unusual (duplicate) name files,
nm_vpn_plugin_info_new_search_file() might return a value
that would not otherwise be returned by
nm_vpn_plugin_info_list_load().

Let nm_vpn_plugin_info_new_search_file() call list-load, so that
the search result is always consistent.

The downside of this is that previously, if the searched plugin was
already found in /usr/lib, we would skip loading /etc. But
that is a minor optimization, in any case nm_vpn_plugin_info_new_search_file()
scales with the number of .name files on disk, which is expected to be small.
2020-03-14 17:26:28 +01:00
Antonio Cardace
1a76141e66 nm-device: merge branch 'ac/nm-device-hwaddr'
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/430
2020-03-13 10:23:13 +01:00
Antonio Cardace
067a3d6c08 nm-device: expose via D-Bus the 'hw-address' property
Drop device-specific 'hw-address' GObject properties which are now
redundant.

https://bugzilla.redhat.com/show_bug.cgi?id=1786937
2020-03-13 10:22:21 +01:00
Antonio Cardace
cbb45aaf90 libnm: cleanup 'NML_DBUS_META_IFACE_INIT_PROP' macro 2020-03-13 10:22:21 +01:00
Atul Anand
219ee9a9e8 license: add Atul to RELICENSE.md
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/436
2020-03-12 18:16:10 +01:00
Thomas Haller
f714ae7626 supplicant: merge branch 'th/supplicant'
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/426
2020-03-12 10:32:31 +01:00
Thomas Haller
ef5c109562 wifi: track access point via hash table for supplicant D-Bus path
Let's not do linear search. Use a hash table to find the AP by D-Bus
path.
2020-03-12 10:16:22 +01:00
Thomas Haller
4cfed38135 wifi: expose NMRefString for nm_wifi_ap_get_supplicant_path()
We internally track the string as NMRefString. Expose it, so that
users can directly use the reference counted string.
2020-03-12 10:16:22 +01:00
Thomas Haller
b83f07916a supplicant: large rework of wpa_supplicant handling
Avoid GDBusProxy, instead use GDBusConnection directly. I very much
prefer this because that way we have explicit control over what happens
on D-Bus. With GDBusProxy this is hidden under another layer of complex
code. The hardest part when using a D-Bus interface is to manage the
state via an asynchronous medium. GDBusProxy contains state about the
D-Bus interface and duplicate the state that we track. This makes it hard
to reason about things.

Rework creation of NMSupplicantInterface. Previously, a NMSupplicantInterface
had multiple initialization states. In particular, the first state would not
yet tie the interface to a certain D-Bus object path. Instead, NMSupplicantInterface
would try and retry to create the D-Bus object.
Now, NMSupplicantManager has an asynchronous method to create interface
instances. The manager only creates an interface instance after the D-Bus
path is known. That means, a NMSupplicantInterface instance is now
strongly tied to a name-owner and D-Bus path.

It follows that the state of NMSupplicantInterface can only go from STARTING,
via the supplicant states, to DOWN. Never back. That was already previously
the case that the state from DOWN was final and once the 3 initial
states were passed, the interface's state would never go back to the initial
state. Now this is more strict and more formalized. The 3 initialization states
are combined.

I think the tighter state handling simplifies users of NMSupplicantInterface.
See for example "nm-device-ethernet.c". It's still complicated, because handling
state is fundamentally difficult.

NMSupplicantManager will take care to D-Bus activate wpa_supplicant only
when necessary (poke). Previously, creating the manager instance
would always start suppliant service. Now, it's started on demand.
2020-03-12 10:16:22 +01:00
Thomas Haller
0586e9700d wifi: delay release of old wfd_ies array in nm_wifi_p2p_peer_set_wfd_ies()
We should first ref the new instance and emit the notify signal,
before unref the old value. It feels better in this order.
2020-03-12 10:16:22 +01:00
Rafael Fontenelle
e33b200880 po: update Brazilian Portuguese (pt_BR) translation
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/434
2020-03-10 20:29:37 +01:00
Antonio Cardace
fff100b452 bond: merge branch 'ac/fix_miimon_updelay'
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/424
2020-03-06 12:08:45 +01:00
Antonio Cardace
40ea0335d0 nmtui: only set 'mode' and enable 'miimon' when setting up a new connection
When creating a new connection before the user gets the chance to modify
the bond options let's just initialize 'mode' and 'miimon' (with related
'updelay' and 'downdelay').

Initializing also 'arp_interval', 'arp_ip_target' and 'primary' doesn't
make much sense as by default they're disabled or contain empty values.
2020-03-06 10:39:00 +01:00
Antonio Cardace
b868fee9cb nm-setting-bond: add API to libnm to get the normalized bond option value
Add 'nm_setting_bond_get_option_normalized()', the purpose of this API
is to retrieve a bond option normalized value which is the option that
NetworkManager will actually apply to the bond when activating the
connection, this takes into account default values for some options that
NM assumes.

For example, if you create a connection:
$ nmcli c add type bond con-name nm-bond ifname bond0 bond.options mode=0

Calling 'nm_setting_bond_get_option_normalized(s_bond, "miimon")' would
return "100" as even if not specified NetworkManager enables miimon for
bond connections.

Another example:
$ nmcli c add type bond con-name nm-bond ifname bond0 bond.options mode=0,arp_interval=100

Calling 'nm_setting_bond_get_option_normalized(s_bond, "miimon")' would
return NULL in this case because NetworkManager disables miimon if
'arp_interval' is set explicitly but 'miimon' is not.
2020-03-06 10:39:00 +01:00
Antonio Cardace
9bd07336ef bond: bond options logic rework
Add '_nm_setting_bond_get_option_or_default()' and move all the custom
policies applied by NM for bond options in there.

One such example of a custom policy is to set 'miimon' to 0 (instead of its
default value of 100) if 'arp_interval' is explicitly enabled
and 'miimon' is not.

This means removing every piece of logic from
nm_setting_bond_add_option() which used to clear out 'arp_interval' and
'arp_ip_target' if 'miimon' was set or clear out 'miimon' along with
'downdelay', 'updelay' and 'miimon' if 'arp_interval' was set.
This behaviour is a bug since the kernel allow setting any combination
of this options for bonds and NetworkManager should not limit the user
to do so.

Also use 'set_bond_attr_or_default()' instead of 'set_bond_attr()' as
the former calls '_nm_setting_bond_get_option_or_default()' to implement
the right logic to retrieve bond options according to current bond
configuration.
2020-03-06 10:39:00 +01:00
Antonio Cardace
57bdf68088 nm-setting-bond: let 'miimon' and 'arp_interval' coexist for verify()
Fix 'miimon' and 'arp_interval' validation, they can both be set indeed,
the kernel does not impose this limitation, nevertheless is sensible to
keep the defaults as previously (miimon=100, arp_interval=0).

Also add unit test.
2020-03-06 10:39:00 +01:00
Antonio Cardace
c07f3b518c nm-setting-bond: if unset consider bond options with default values for validation
Doing 'verify()' with options such as 'miimon' and 'num_grat_arp' set to
arbitrary values it's not consistent with what NetworkManager does to
bond options when activating the bond through 'apply_bonding_config()'
(at a later stage) because the said values do not
correspond to what the default values for those options are.

This leads to an inconsistency with the 'miimon' parameter for example,
where 'verify()' is done while assuming it's 0 if not set but its
default value is actually 100.

Fixes: 8775c25c33 ('libnm: verify bond option in defined order')
2020-03-06 10:39:00 +01:00
Thomas Haller
d482eec6b2 ifcfg-rh: use binary search for converting string to ethtool ID
Don't do a linear search through all names, but use binary search.

Upside: calling nms_ifcfg_rh_utils_get_ethtool_by_name() in a loop
(once over all 60 names) is 75% faster.

Downside: when adding a new feature, we have yet another line that we
need to add. Previously, adding a new feature required adding 7 lines,
not it is 8. But we didn't add a single feature since this was added,
so that happens very seldom.

Possible downside: is this code harder to read? Now we track both how to
convert the ID to name and back. This is redundant (and thus harder to
maintain). But it's really just one extra line per feature, for which there
is a unit test. So, when adding a new NMEthtoolID it would be pretty
hard to mess this up, because of all the tests and assertions.
So, maybe it's slightly harder to read. On the other hand, it unifies
handling for ethtool and kernel names, and the code has less logic
and is more descriptive. I don't think this is actually harder to maintain
and it should be easy to see that it is correct (readability).
2020-03-06 09:52:27 +01:00
Thomas Haller
a78b32a835 ifcfg-rh/tests: add test for consistency of ethtool ifcfg names 2020-03-06 09:49:32 +01:00
Thomas Haller
d1d50324a4 shared: expose size of nm_ethtool_data array in header 2020-03-06 09:49:32 +01:00
Thomas Haller
a8442c8243 man: show example for nmcli --ask device wifi connect in nmcli-example manual 2020-03-06 09:48:58 +01:00
Auke Kok
425293b13c man: show example for nmcli device wifi connect in nmcli-example manual
Add the only important example that this file should have

All other examples are nice. But when you install a console-only
machine and read the NM man pages, you are none the wiser, because
something as simple like this isn't covered in the man pages.

I've seen other users complain about it, and I've torn my hair out
over this several times.

[thaller@redhat.com: changed subject line of patch]
2020-03-06 09:48:58 +01:00
Thomas Haller
c5a6e07d07 core: merge branch 'th/prune-device-state-files'
https://bugzilla.redhat.com/show_bug.cgi?id=1810153

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/429
2020-03-04 16:58:11 +01:00
Thomas Haller
332df7a58e core: periodically cleanup unused device state files from /run
Otherwise, we only prune unused files when the service terminates.
Usually, NetworkManager service doesn't get restarted before shutdown
of the system (nor should it be). That means, if you create (and
destroy) a large number of software devices, the state files pile
up.

From time to time, go through the files on disk and delete those that
are no longer relevant.

In this case, "from time to time" means after we write/update state
files 100 times.
2020-03-04 16:53:15 +01:00
Thomas Haller
5477847eed core: return ifindex from nm_manager_write_device_state()
nm_manager_write_device_state() writes the device state to a file. The ifindex
is here important, because that is the identifier for the device and is also
used as file name. Return the ifindex that was used, instead of letting the
caller reimplement the knowledge which ifindex was used.
2020-03-04 16:53:04 +01:00
Thomas Haller
ecb0210e7a core/trivial: rename nm_config_device_state_prune_unseen() to nm_config_device_state_prune_stale()
It's just a more fitting name after the previous change.
2020-03-04 16:52:57 +01:00
Thomas Haller
ad9e748816 core: cleanup nm_config_device_state_prune_unseen() and accept NMPlatform for skipping pruning 2020-03-04 16:48:09 +01:00
Thomas Haller
627b543a37 dns: cleanup update_dns() for returning error
It was not very clear whether update_dns() would always set the error
output variable if (and only if) it would return false.

Rework the code to make it clearer.
2020-03-04 15:48:01 +01:00
Thomas Haller
abafea8682 dns: use gs_free_error for clearing error from update_dns()
Not using cleanup attribute is error prone.

In theory, a function should only return a GError if (and only if) it
signals a failure. However, for example in commit 324f67956a ('dns:
ensure to log a warning when writing /etc/resolv.conf fails') due to
a bug we was violated. In that case, it resulted in a leak.

Avoid explicit frees and use the gs_free_error cleanup attribute
instead. That would also work correctly in face of such a bug and in
general it seems preferable to explicitly assign ownership to auto
variables on the stack.
2020-03-04 15:45:16 +01:00
Thomas Haller
324f67956a dns: ensure to log a warning when writing /etc/resolv.conf fails
When setting "main.rc-manager=symlink" (the default) and /etc/resolv.conf
is a file, NetworkManager tries to write the file directly. When that fails,
we need to make sure to propagate the error so that we log a warning about that.

With this change:

    <debug> [1583320004.3122] dns-mgr: update-dns: updating plugin systemd-resolved
    <trace> [1583320004.3123] dns-sd-resolved[f9e3febb7424575d]: send-updates: start 8 requests
    <trace> [1583320004.3129] dns-mgr: update-resolv-no-stub: '/var/run/NetworkManager/no-stub-resolv.conf' successfully written
    <trace> [1583320004.3130] dns-mgr: update-resolv-conf: write to /etc/resolv.conf failed (rc-manager=symlink, $ERROR_REASON)
    <trace> [1583320004.3132] dns-mgr: update-resolv-conf: write internal file /var/run/NetworkManager/resolv.conf succeeded
    <trace> [1583320004.3133] dns-mgr: current configuration: [{ [...] }]
    <warn>  [1583320004.3133] dns-mgr: could not commit DNS changes: $ERROR_REASON
    <info>  [1583320004.3134] device (eth0): Activation: successful, device activated.

https://bugzilla.redhat.com/show_bug.cgi?id=1809181
2020-03-04 12:15:25 +01:00
Thomas Haller
0549351111 dhcp: clean source on dispatch failure (fix leak)
The GSource must also be unrefed. Also, first clear the field
before invoking callbacks to the upper layers.

Fixes: 843d696e46 ('dhcp: clean source on dispatch failure')
2020-03-03 09:53:17 +01:00
Beniamino Galvani
843d696e46 dhcp: clean source on dispatch failure
Fix the following warning:

 NetworkManager[1524461]: Source ID 3844 was not found when attempting to remove it

 g_logv (log_domain=0x7f2816fa676e "GLib", log_level=G_LOG_LEVEL_CRITICAL, format=<optimized out>, args=args@entry=0x7ffe697374d0) at gmessages.c:1391
 g_log (log_domain=log_domain@entry=0x7f2816fa676e "GLib", log_level=log_level@entry=G_LOG_LEVEL_CRITICAL, format=format@entry=0x7f2816fae240 "Source ID %u was not found when attempting to remove it") at gmessages.c:1432
 g_source_remove (tag=519) at gmain.c:2352
 nm_clear_g_source (id=<optimized out>) at ./shared/nm-glib-aux/nm-macros-internal.h:1198
 dispose (object=0x55f7289b1ca0) at src/dhcp/nm-dhcp-nettools.c:1433
 g_object_unref (_object=<optimized out>) at gobject.c:3303
 g_object_unref (_object=0x55f7289b1ca0) at gobject.c:3232
 dhcp4_cleanup (self=self@entry=0x55f728af3b20, cleanup_type=cleanup_type@entry=CLEANUP_TYPE_DECONFIGURE, release=release@entry=0) at src/devices/nm-device.c:7565
 ...

Fixes: 45521b1b38 ('dhcp: nettools: move to failed state if event dispatch fails')
2020-03-03 09:34:04 +01:00
Beniamino Galvani
105abf27c1 Revert "dispatcher/systemd: order NetworkManager-dispatcher.service Before=NetworkManager.service"
The 'Before' dependency between NM-dispatcher and NM causes a deadlock
when stopping the NM service. When terminating, NM wants to D-Bus
activate NM-dispatcher to synchronously handle pre-down events; but
NM-dispatcher start is ordered after NM shutdown due to the following
behavior described in systemd.unit(5) man page:

  Given two units with any ordering dependency between them, if one
  unit is shut down and the other is started up, the shutdown is
  ordered before the start-up. It doesn't matter if the ordering
  dependency is After= or Before=, in this case. It also doesn't
  matter which of the two is shut down, as long as one is shut down
  and the other is started up; the shutdown is ordered before the
  start-up in all cases.

So, NM is waiting NM-dispatcher to start and NM-dispatcher is queued
by systemd, waiting that NM is stopped. The result is a 90 seconds
delay, after which systemd kills NM and continues.

The dependency was added so that during shutdown NM-dispatcher would
be stopped after NM. I don't think it worked as expected because
NM-dispatcher is not supposed to be active most of the times, and so
it doesn't need a dependency that delays its stop after NM.

This reverts commit acc335aad4.
2020-03-02 17:47:29 +01:00
Thomas Haller
4087024a9b shared: move assertion in _NM_UTILS_STRING_TABLE_LOOKUP_DEFINE()
Move the assertion for valid LIST first. It only checks static data,
and regardless of the entry_cmd, it should be done first.

Fixes: f4d12f7b59 ('shared: add NM_UTILS_STRING_TABLE_LOOKUP_STRUCT_DEFINE() macro for lookup of structs')
2020-03-02 16:20:50 +01:00
Thomas Haller
8b63fd8cc0 shared: fix handling %NULL argument in nm_ref_string_equals_str()
Fixes: 190a8ed425 ('shared: add nm_ref_string_equals_str() helper')
2020-03-02 15:49:41 +01:00
Thomas Haller
30e90b0d95 license: add Tambet to RELICENSE.md
https://mail.gnome.org/archives/networkmanager-list/2020-March/msg00000.html
2020-03-02 12:33:46 +01:00
Antonio Cardace
50da785be1 nm-setting-bond: fix '[up|down]delay', 'miimon' validation
Just looking at the hashtable entry of 'updelay' and 'downdelay' options
is wrong, we have to inspect their values to check if they're
actually enabled or not.

Otherwise bond connections with valid settings will fail
when created:

$ nmcli c add type bond ifname bond99 bond.options miimon=0,updelay=0,mode=0
Error: Failed to add 'bond-bond99' connection: bond.options: 'updelay' option requires 'miimon' option to be set

Also add unit tests.

https://bugzilla.redhat.com/show_bug.cgi?id=1805184

Fixes: d595f7843e ('libnm: add libnm/libnm-core (part 1)')
2020-02-28 15:45:34 +01:00
Thomas Haller
7625e2483f license: add Pavel to RELICENSE.md
https://mail.gnome.org/archives/networkmanager-list/2020-February/msg00040.html
2020-02-28 08:45:44 +01:00