Commit Graph

957 Commits

Author SHA1 Message Date
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
294de8487e shared: add NMU_IFACE_OVS_OR_KERNEL for nm_utils_ifname_valid()
Depending on the type, OVS interfaces also have a corresponding netdev
in kernel (e.g. type "internal" does, type "patch" does not).

Such a case is neither NMU_IFACE_OVS nor NMU_IFACE_KERNEL (alone). There should
be a special type to represent those cases.

Add NMU_IFACE_OVS_OR_KERNEL for that.
2020-02-26 17:51:14 +01:00
Thomas Haller
807cddc754 shared: add NMU_IFACE_ANY for nm_utils_ifname_valid()
nm_utils_ifname_valid() is to validate "connection.interface-name"
property. But the exact validation depends on the connection type.

Add "NMU_IFACE_ANY" to validate the name to check whether it would be
valid for any connection type.

This is for completeness and for places where the caller might not know
the connection type.
2020-02-26 17:51:14 +01:00
Thomas Haller
d4d9e9e7bb shared: reject reserved names from "connection.interface-name"
"all" and "default" never works.

"bonding_masters" works if you unload the bonding module. Well,
that should not really be called working...

Reject these names.
2020-02-26 17:51:13 +01:00
Thomas Haller
ef567805bb shared: reject '%' from nm_utils_ifname_valid() for kernel names
Generally, it's dangerous to reject values that were accepted
previously. This will lead to NetworkManager being unable to load
a profile from disk, which was loadable previously.

On the other hand, kernel would not have treated this setting as
it was intended. So, I would argue that the such a setting was not
working (as intended) anyway.

We can only hope that users don't configure arbitrary interface names.
It generally isn't a good idea to do, so "breaking" such things is less
of a concern.
2020-02-26 17:51:13 +01:00
Beniamino Galvani
5a7b83ea0a n-dhcp4: keep trying after a failure in send()
Currently if an error is encountered during a send() of a message, the
client fails and there is no possibility of recover, since no timers
are armed after a failed event dispatch. An easy way to reproduce a
failure is to add a firewall rule like:

  iptables -A OUTPUT -p udp --dport 67 -j REJECT

which makes the send() fail with EPERM during the renew. In such case,
the client should continue (failing) until it reaches the rebind phase
at T2, when it will be able to renew the lease using the packet
socket.

In general, a failure to send a packet should not cause the failure of
the client.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/merge_requests/419
https://bugzilla.redhat.com/show_bug.cgi?id=1806516
2020-02-24 14:58:33 +01:00
Beniamino Galvani
910267cf5f n-dhcp4: fix logging macro
The level can be a complex expression, don't use it directly in the
macro.
2020-02-24 14:58:24 +01:00
Thomas Haller
9f2014ac64 shared: fix suffix for NM_GOBJECT_PROPERTIES_DEFINE_BASE*() for multiple definitions
To really use multiple NM_GOBJECT_PROPERTIES_DEFINE_BASE*() defines in
the same source file, several fixes to the suffix handling are
necessary. This fixes commit f13c7e3bbd ('shared: extend
NM_GOBJECT_PROPERTIES_DEFINE*() macros to append suffix to defined
names') to really work.

Fixes: f13c7e3bbd ('shared: extend NM_GOBJECT_PROPERTIES_DEFINE*() macros to append suffix to defined names')
2020-02-24 13:13:34 +01:00
Beniamino Galvani
cf6940665d shared: add more debug messages to nm_utils_parse_inaddr_bin_full()
Log the reason for the assertion failure to debug the crash in [1].

[1] https://bugzilla.redhat.com/show_bug.cgi?id=1797915
2020-02-21 16:54:53 +01:00
Thomas Haller
f13c7e3bbd shared: extend NM_GOBJECT_PROPERTIES_DEFINE*() macros to append suffix to defined names
This way, we will be able to use the macro multiple times in the same
source file by using different suffixes.
2020-02-21 15:59:44 +01:00
Thomas Haller
cba938f3ee shared: add nm_utils_named_values_from_str_dict_full() to allow different sort order (or none) 2020-02-19 16:24:55 +01:00
Thomas Haller
f4d12f7b59 shared: add NM_UTILS_STRING_TABLE_LOOKUP_STRUCT_DEFINE() macro for lookup of structs 2020-02-19 15:35:07 +01:00
Thomas Haller
14461e7217 shared: add NM_MORE_ASSERT_ONCE() macro 2020-02-19 15:35:07 +01:00
Thomas Haller
eb74d5f65f shared/trivial: add code comment to nm_utils_ifname_valid_kernel() 2020-02-18 13:23:50 +01:00
Thomas Haller
82fb8b00b4 shared: check for valid UTF-8 in nm_utils_ifname_valid()
The interface name might come from the command line or from a filename
(like during nm_vpn_wireguard_import()). It's not clear that this
is valid UTF-8. Asserting against that requires the caller to ensure
that the encoding is valid. That is cumbersome, especially since we anyway
check. Just report a regular error.
2020-02-18 13:23:50 +01:00
Antonio Cardace
ed5a647ad1 nm-shared-utils: relax ovs ifname check to accept any (non-space) ASCII printable char
quoting 'man ovs-vswitchd.conf.db':
"The name must be alphanumeric and must not contain forward or backward
slashes."

OVS actually accepts a wider range of chars (all printable UTF-8 chars),
NetworkManager restricts this to ASCII char as it's a safer option for
now since OVS is not well documented on this matter.

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

Fixes: e7d72a14f6 ('libnm-core: use different ifname validation function for OVS bridges, ports and interfaces')
2020-02-18 13:11:21 +01:00
Beniamino Galvani
3286918bd9 n-dhcp4: request previous address after expiration
If the lease expires and the client start again sending a discover,
request the previous address.
2020-02-17 18:58:47 +01:00
Thomas Haller
037507f790 shared: fix returning out_len from nm_utils_hash_values_to_array()
The only affected caller is nm_modem_manager_get_modems(), which
is used by NMDeviceBt for DUN connections. This is rather bad.

Fixes: 4154d9618c ('bluetooth: refactor BlueZ handling and let NMBluezManager cache ObjectManager data')
Fixes: e688e70b37 ('shared: add nm_utils_hash_values_to_array() helper')
2020-02-17 17:54:10 +01:00
Antonio Cardace
550f538564 nm-shared-utils: add nm_utils_ifname_valid*() to shared utils
Move the body of nm_utils_is_valid_iface_name() to
nm_utils_ifname_valid_kernel() so that it's shared between NM and
clients.
2020-02-17 15:27:35 +01:00
Thomas Haller
eae862e385 shared: add LOGD_DHCP_from_addr_family() helper 2020-02-17 14:45:09 +01:00
Thomas Haller
2dde55203b license: relicense "shared/systemd/sd-adapt-shared/nm-sd-adapt-shared.h" under LGPL-2.1+
$ git shortlog -n -s a3e75f3294 -- shared/systemd/sd-adapt-shared/nm-sd-adapt-shared.h src/systemd/sd-adapt-core/nm-sd-adapt-core.c src/systemd/sd-adapt/nm-sd-adapt.'[hc]' src/systemd/nm-sd-adapt.'[hc]' src/dhcp-manager/systemd-dhcp/nm-sd-adapt.'[hc]'
      46    Thomas Haller
       7    Beniamino Galvani
       3    Lubomir Rintel
       2    Dan Williams
       2    Dan Winship
       1    Glenn Washburn
       1    Jiří Klimeš

Note that the contribution to the file by Glenn was removed in
commit e27b15c00d ('all: remove CLOCK_BOOTTIME defintions'), so
it is no longer relevant.

All other contributors agreed to the relicensing according to "RELICENSE.md".
2020-02-16 15:45:08 +01:00
Thomas Haller
46da89cb54 license: relicense "shared/nm-utils/nm-vpn-editor-plugin-call.h" under LGPL-2.1+
$ git shortlog -n -s a3e75f3294 -- shared/nm-utils/nm-vpn-editor-plugin-call.h shared/nm-vpn-editor-plugin-call.h
       5    Thomas Haller

All contributors agreed to the relicensing according to "RELICENSE.md".
2020-02-16 15:41:49 +01:00
Thomas Haller
3eef0cf387 license: relicense "shared/nm-libnm-core-intern-aux/nm-auth-subject.[hc]" under LGPL-2.1+
$ git shortlog -n -s a3e75f3294 -- shared/nm-libnm-core-intern/nm-auth-subject.'[hc]' src/nm-auth-subject.'[hc]'
      28    Thomas Haller
       7    Dan Winship
       1    Beniamino Galvani
       1    Dan Williams
       1    Jiří Klimeš

All contributors agreed to the relicensing according to "RELICENSE.md".
2020-02-16 15:41:44 +01:00
Thomas Haller
fee50e17b2 license: relicense "shared/nm-glib-aux/nm-jansson.h" under LGPL-2.1+
$ git shortlog -n -s a3e75f3294 -- shared/nm-glib-aux/nm-jansson.h shared/nm-utils/nm-jansson.h
       7    Thomas Haller
       1    Lubomir Rintel
       1    Beniamino Galvani
       1    Francesco Giudici

All contributors agreed to the relicensing according to "RELICENSE.md".
2020-02-16 15:41:40 +01:00
Thomas Haller
e70ce4042c license: relicense "shared/nm-glib-aux/nm-glib.h" under LGPL-2.1+
$ git shortlog -n -s a3e75f3294 -- shared/nm-glib-aux/nm-glib.h shared/nm-utils/nm-glib.h shared/nm-glib.h include/nm-glib.h include/nm-glib-compat.h
      35    Thomas Haller
      12    Dan Winship
       5    Dan Williams
       3    Lubomir Rintel
       2    Beniamino Galvani
       1    Jan Kantert
       1    Thomas Bechtold

The last two contributions by Thomas and Jan are no longer present in any
form, because they were for an older version of glib which is no longer
supported. All other contributors agree to the licences change according
to "RELICENSE.md" file.
2020-02-16 15:41:36 +01:00
Thomas Haller
de62da297e all: drop explicit casts from _GET_PRIVATE() macro calls
The _GET_PRIVATE() macros are all implemented based on
_NM_GET_PRIVATE(). That macro tries to be more type safe and uses
_Generic() to do the right thing. Explicitly casting is not only
unnecessary, it defeats these (static) type checks.

Don't do that.
2020-02-14 11:04:46 +01:00
Thomas Haller
d36df06c24 shared: don't require callback argument to nm_dbus_connection_call_set()
It's fine to call propery-set without caring about the result.
2020-02-14 09:48:37 +01:00
Thomas Haller
829f8e8314 shared: drop compat implementation for G_DEFINE_QUARK()
For one, we by now require glib >= 2.34.0, so this is not used.

Also, I think G_DEFINE_QUARK() is rather ugly because it constructs
the defined function name (so you cannot grep for it). We should use
NM_CACHED_QUARK_FCN() instead.
2020-02-13 17:17:07 +01:00
Thomas Haller
cd31437024 shared: drop _STATIC variant of macros that define functions
Several macros are used to define function. They had a "_STATIC" variant,
to define the function as static.

I think those macros should not try to abstract entirely what they do.
They should not accept the function scope as argument (or have two
variants per scope). This also because it might make sense to add
additional __attribute__(()) to the function. That only works, if
the macro does not pretend to *not* define a plain function.

Instead, embrace what the function does and let the users place the
function scope as they see fit.

This also follows what is already done with

    static NM_CACHED_QUARK_FCN ("autoconnect-root", autoconnect_root_quark)
2020-02-13 17:17:07 +01:00
Thomas Haller
17d9b852c8 shared: explicitly implement binary search in NM_UTILS_STRING_TABLE_LOOKUP_DEFINE*()
When looking at nm_utils_array_find_binary_search(), we see that binary
search really isn't complicated. In nm_utils_array_find_binary_search()
it looks complicated, because that is our general purpose function which
accepts arbitrary lists, uses an opaque compare function, accepts a user
data argument, and returns the insertion position.

This is unnecessary for the narrow purpose in NM_UTILS_STRING_TABLE_LOOKUP_DEFINE*().
When we inline the binary search, it can be simplified, and the remaining
parts is simple enough to prefer this duplication of binary search over
using our general purpose function.

Also, this gives the compiler more chance for optimization. For
example, we can use "unsigned" as index type instead of gssize, because
we know (at compile time), that this type will always be large enough
for our LIST. Also, we can directly call strcmp().

The result is that the macro's implementation is also fast in the best
case (where the needle is found with only one strcmp()) and in the cases
where there is a small number of items to search.
It thus alleviates concerns that using the macro might be slower than
an optimized implementation.

The binary size of the defined function increases slightly (from 112
bytes to 192 bytes, on x86_64, GCC, -O2). But according to my tests it
is slightly and measurably faster.
2020-02-13 14:49:45 +01:00
Thomas Haller
760551e3fc shared/tests: add test for NM_UTILS_STRING_TABLE_LOOKUP_DEFINE*() 2020-02-13 14:20:26 +01:00
Thomas Haller
487141d4a8 shared: add entry_cmd argument to NM_UTILS_STRING_TABLE_LOOKUP_DEFINE*() macro
This extra argument allows to tweak whether to assert for the input argument name.
2020-02-13 10:46:34 +01:00
Beniamino Galvani
43016d6ebd n-dhcp4: avoid double free of NDhcp4Outgoing
n_dhcp4_c_connection_start_request() should take ownership of the
request only on success. On failure the request is freed by the
caller.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/issues/355
2020-02-11 09:26:07 +01:00
Thomas Haller
0d4eedda07 shared/tests: add nmtst_get_rand_uint64() util 2020-02-10 19:11:50 +01:00
Thomas Haller
190a8ed425 shared: add nm_ref_string_equals_str() helper 2020-02-10 19:11:50 +01:00
Thomas Haller
333e6f66fb shared: rename local variable in _NM_GET_PRIVATE_PTR() macro
The _NM_GET_PRIVATE_PTR() macro is possibly used from other macros. And
"_self" is a pretty good name to use. Don't let the lower layer macro
use this name.
2020-02-10 19:11:50 +01:00
Thomas Haller
8c23586a77 shared: drop nm_utils_dbus_normalize_object_path() in favor of nm_dbus_path_not_empty()
They do the same thing. Unify and drop one.
2020-02-10 19:11:50 +01:00
Thomas Haller
c7e6573eb4 shared: add nm_g_variant_lookup() and nm_g_variant_lookup_value() helpers
It's often convenient to accept %NULL as dictionary argument.
2020-02-10 19:11:50 +01:00
Thomas Haller
455cec9986 shared: add nm_utils_strdup_reset() helper 2020-02-10 19:11:50 +01:00
Thomas Haller
45a832ac50 shared: add NM_FLAGS_ASSIGN_MASK() helper 2020-02-10 19:11:50 +01:00
Thomas Haller
5528086ec5 shared: add nm_dbus_connection_call_set() util 2020-02-10 19:11:50 +01:00
Thomas Haller
5ce7ea38ff shared: add NM_PRINT_FMT_QUOTE_REF_STRING() macro 2020-02-10 19:11:50 +01:00
Thomas Haller
f4fa003434 shared: add NM_UTILS_STRING_TABLE_LOOKUP_DEFINE() macro to simplify defining string lookup functions 2020-02-10 19:11:50 +01:00
Thomas Haller
9153e7912f shared: add nm_utils_get_monotonic_timestamp_msec_cached() helper 2020-02-10 19:11:50 +01:00
Thomas Haller
8b63b229dd shared/trivial: rename time related functions to use "nsec"/"msec" abbreviation instead of "ns"/"ms"
The "ns" abbreviation doesn't look too nice. We mostly use "nsec" at other
places. Rename.
2020-02-10 19:11:50 +01:00
Thomas Haller
53f6858a27 all: add nm_utils_error_is_cancelled() and nm_utils_error_is_cancelled_or_disposing()
Most callers would pass FALSE to nm_utils_error_is_cancelled(). That's
not very useful. Split the two functions and have nm_utils_error_is_cancelled()
and nm_utils_error_is_cancelled_is_disposing().
2020-02-10 19:11:50 +01:00
Thomas Haller
90c5d1d99c shared: add debugging statement for assertion in nm_utils_parse_inaddr_bin_full()
There are crashes where this assertion fails, but it's not clear
how that could happen (because the input text seems a usual IPv4 address).

Try to collect some more information about what failed. It's only
enabled with NM_MORE_ASSERTS anyway.

Related: https://bugzilla.redhat.com/show_bug.cgi?id=1797915
2020-02-10 16:44:17 +01:00
Beniamino Galvani
df6129d93a n-dhcp4: fix initialization of the 'secs' DHCP header field
Due to wrong type conversions, the value was always zero.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/issues/341
2020-02-10 16:36:25 +01:00
Beniamino Galvani
7f801685b2 shared: redefine G_SOURCE_FUNC
G_SOURCE_FUNC has attribute GLIB_AVAILABLE_MACRO_IN_2_58, which means
that the compiler will emit a warning when GLIB_VERSION_MAX_ALLOWED <
GLIB_VERSION_2_58. We currently define GLIB_VERSION_MAX_ALLOWED as
GLIB_VERSION_2_40. Redefine the macro to fix the following build error
when using glib >= 2.63.5 (the version in which the attribute was
added):

  CC       shared/nm-glib-aux/libnm_glib_aux_la-nm-shared-utils.lo
 shared/nm-glib-aux/nm-shared-utils.c: In function ‘nm_g_unix_fd_source_new’:
 shared/nm-glib-aux/nm-shared-utils.c:3679:13: error: Not available before  [-Werror]
  3679 |  g_source_set_callback (source, G_SOURCE_FUNC (source_func), user_data, destroy_notify);

Fixes: 9c5741ccd2 ('shared/nm-glib: add compat implementation for G_SOURCE_FUNC()')
2020-02-05 14:31:46 +01:00