Commit graph

1066 commits

Author SHA1 Message Date
Antonio Cardace
e04e5a5c2a
ifcfg-rh: add support for ethtool ring settings
Also update unit tests.

https://bugzilla.redhat.com/show_bug.cgi?id=1614700
2020-05-20 10:55:01 +02:00
Antonio Cardace
7109ac667a
ethtool: add ring settings
This is the initial support for ring settings and only allows
getting/setting, nothing is yet implemented in nm-device.

Also add unit test.

https://bugzilla.redhat.com/show_bug.cgi?id=1614700
2020-05-20 10:55:01 +02:00
Thomas Haller
0480448e66
n-dhcp4: style fix in n_dhcp4_client_probe_transition_accept()
The upstream fix also doesn't have this whitespace.
Keep the sources in sync.

0be7033dd9
2020-05-18 09:54:28 +02:00
Thomas Haller
ef9fe85096
shared: move _nm_utils_format_variant_attributes*() API to "shared/nm-glib-aux"
This has no dependency on libnm, libnm-core, or src. Move it to the
general purpose toolbox.
2020-05-14 17:21:12 +02:00
Thomas Haller
3c581cbb78
build: fix redefinition of VALGRIND define in "nm-sd-adapt-shared.h"
When configuring with sanitizers enabled, ./configure.ac sets
-DVALGRIND=1 in the CFLAGS.

This causes a compilation error later:

  $ /bin/sh ./libtool  --tag=CC   --mode=compile gcc ... -DVALGRIND=1 ... src/dhcp/nm-dhcp-nettools.c
  ...
  In file included from src/dhcp/nm-dhcp-nettools.c:16:
  ./shared/systemd/sd-adapt-shared/nm-sd-adapt-shared.h:73: error: "VALGRIND" redefined [-Werror]
   #define VALGRIND 0
2020-05-13 17:21:01 +02:00
Thomas Haller
bb19f6e29c
shared: add NM_UTILS_NAMED_VALUE_INIT() macro 2020-05-13 10:28:04 +02:00
Thomas Haller
0f22f77b1c
shared: support stripping whitespace from nm_utils_buf_utf8safe_unescape()
When parsing user input if is often convenient to allow stripping whitespace.
Especially with escaped strings, the user could still escape the whitespace,
if the space should be taken literally.

Add support for that to nm_utils_buf_utf8safe_unescape().

Note that this is not the same as calling g_strstrip() before/after
unescape. That is, because nm_utils_buf_utf8safe_unescape() correctly
preserves escaped whitespace. If you call g_strstrip() before/after
the unescape, you don't know whether the whitespace is escaped.
2020-05-13 10:28:04 +02:00
Thomas Haller
5fe447d4a6
shared: assert that nm_utils_buf_utf8safe_unescape() doesn't reallocate memory
We want to use the function to unescape (compress) secrets. As such, we want
to be sure that no secrets are leaked in memory due to growing the buffer with
realloc. In fact, reallocation should never happen. Assert for that.

As reallocation cannot happen, we could directly fill a buffer with
API like nm_utils_strbuf_*(). But NMStrBuf has low overhead even in this
case.
2020-05-13 10:28:04 +02:00
Antonio Cardace
b0240db068
ethtool: add coalesce settings
This is the initial support for coalesce settings and only allows
getting/setting, nothing is yet implemented in nm-device.

Also add unit test.

https://bugzilla.redhat.com/show_bug.cgi?id=1614700
2020-05-13 10:15:23 +02:00
Antonio Cardace
858fb211ad
keyfile: add support for uint32 variant types when using gendata
https://bugzilla.redhat.com/show_bug.cgi?id=1614700
2020-05-13 10:15:23 +02:00
Thomas Haller
0d7d2425c4
ethtool: accept %NULL as argument for nm_ethtool_data_get_by_optname()/nm_ethtool_id_get_by_name()
Often it is useful to not accept %NULL as input argument, to catch bugs.
For simple functions like nm_ethtool_id_get_by_name(), such limitations
are more annoying than helpful. Gracefully accept %NULL and treat is
like an invalid ethtool option.
2020-05-11 16:18:22 +02:00
Thomas Haller
2027ff50af
ethtool: use NM_MORE_ASSERT_ONCE() in _ASSERT_data() to assert immutable data only once
_ASSERT_data() checks static, immutable data. Even with more asserts enabled,
there is no need to do that every time. Use NM_MORE_ASSERT_ONCE().

Note that NM_MORE_ASSERT_ONCE() will return constant FALSE, when build
without a sufficiently high assertion level. That means, the compiler
will just optimize the rest away.
2020-05-11 14:47:43 +02:00
Thomas Haller
2929392855
all: use "int/char" instead of "gint/gchar" typedefs
This is also recommended by our checkpatch.pl script.
2020-05-08 12:56:29 +02:00
Thomas Haller
dbf14dc38c
shared: add nm_str_is_empty() helper
We have nm_str_not_empty() which is the inverse of that. The purpose
of nm_str_not_empty() is to normalize a string to either return
%NULL or a non-empty string, like

   const char *
   get_name (Object *obj)
   {
        return nm_str_not_empty (obj->name);
   }

Sometimes, we however want to check whether a string is not empty.
So, we previously had two choices:

1) use a temporary variable:

     const char *tmp;

     tmp = get_string ();
     if (tmp && tmp[0])
        ...

The problem with this variant is that it's more verbose (by requiring a
temporary variable). Another downside is that there are multiple ways
how to check for an empty string (!tmp[0], tmp[0] == '\0', !strlen (tmp),
strlen (tmp) == 0), and sure enough they are all in use.

2) use !nm_str_not_empty(). But this double negation looks really odd
and confusing.

Add nm_str_is_empty() instead.
2020-05-08 11:10:51 +02:00
Thomas Haller
0b2ecf5e35
shared: add NM_G_PARAM_SPEC_GET_DEFAULT_*() helper macros 2020-05-08 08:02:48 +02:00
Thomas Haller
a0b2955907
shared: add NM_ENSURE_NOT_NULL() macro 2020-05-08 08:00:41 +02:00
Thomas Haller
5cdb636301
keyfile: use nm_g_error_matches() in g_error_matches()
nm_g_error_matches() can be inlined and first checks whether the error
argument is not NULL. At least from the keyfile accessor functions, use
this macro, as they are called many times.
2020-05-07 14:08:32 +02:00
Thomas Haller
e08bb66b34
keyfile,config: use nm_keyfile_error_is_not_found() helper 2020-05-07 14:08:32 +02:00
Thomas Haller
df27164d5e
shared: add nm_keyfile_error_is_not_found() helper 2020-05-07 14:08:32 +02:00
Thomas Haller
e31b31e5e5
shared: add nm_g_error_matches() helper 2020-05-07 14:08:31 +02:00
Thomas Haller
a617177070
shared: add NM_STR_HAS_PREFIX_WITH_MORE() helper 2020-05-07 14:08:31 +02:00
Thomas Haller
5a09292f1f
shared: fix accessing "str" argument to NM_STR_HAS_PREFIX() macro twice
Macros preferably behave function-like, for example in that they evaluate
arguments exactly ones. Sometimes, we want to evaluate arguments
lazily, like in NM_IN_SET() or nm_g_set_error_take_lazy(). But it
is almost always undesirable to evaluate an argument more than once.

Fix NM_STR_HAS_PREFIX() for that.

Also, rename the local variable to not use the name "_str",
which may be a common name that the caller would like to use.
2020-05-07 14:08:31 +02:00
Thomas Haller
9e6d6191d1
tests: add include guard to "nm-test-libnm-utils.h" header
It causes a warning on lgtm.com.
2020-05-07 13:58:10 +02:00
Thomas Haller
5056e0d3c8
shared: add nm_strvarray_*() helper API
GPtrArray does not support NULL terminating the pointer array. That
makes it cumbersome to use it for tracking a strv array. Add a few
helper functions nm_strvarray_*() that help using a GArray instead.
2020-05-06 15:19:27 +02:00
Thomas Haller
8dd74fe364
shared: add nm_indirect_g_free() helper 2020-05-06 15:19:25 +02:00
Thomas Haller
070535c6f6
shared: add nm_g_array_len() helper 2020-05-06 15:19:24 +02:00
Thomas Haller
46bee5298b
shared: add nm_g_ptr_array_len() helper 2020-05-06 15:19:23 +02:00
Thomas Haller
3ebfb67df4
keyfile: implement nm_keyfile_plugin_kf_get_string_list() directly without macro
There is only one user of the macro left. Drop it.
2020-05-04 13:12:43 +02:00
Thomas Haller
dade5055fb
keyfile: add nm_keyfile_plugin_kf_get_integer_list_uint() to parse a list of integers
We had three callers of nm_keyfile_plugin_kf_get_integer_list(). Two
only wanted to read values in range of guint8. One, wanted to read
unsigned integers (for which nm_keyfile_plugin_kf_get_integer_list()
was not suitable).

Instead, implement a integer list reader ourself.

One change is that g_key_file_get_integer_list() would accept list elements
with a number followed by a white space and garbage ([1]). We don't do that,
so there is a change in behavior here. That seems preferable, we don't
want to accept garbage.

The error reason text from the reader now also changes, and obviously we
no longer fail for integer values larger than G_MAXINT.

[1] c9bf247eb9/glib/gkeyfile.c (L4445)
2020-05-04 13:12:43 +02:00
Thomas Haller
bbdb47adaf
keyfile: implement nm_keyfile_plugin_kf_set_string_list() directly without macro
There is only one user of the macro left. Drop it.
2020-05-04 13:12:43 +02:00
Thomas Haller
93285a465f
keyfile: refactor writing of G_TYPE_ARRAY list of unsigned integers
Keyfile handles GObject properties of type G_TYPE_ARRAY as a GArray
of unsigned ints. That is correct, because all our properties of this
GType happen to be of this kind.

However, then the function was using nm_keyfile_plugin_kf_set_integer_list(),
which only can handle signed integers. There was thus an assertion that all
integers were non-negative. Which, probably was also correct, because NMSettingDcb
would validate that all values of such kind are in fact positive. Anyway, that
is an unexpected limitation (if not a bug).

Fix that by handling the array as unsigned list of integers.

Also, since glib doesn't provide an API for storing lists of unsigend
integers, we have to implement our own. but that is no loss. We probably
do it better anyway.
2020-05-04 12:47:11 +02:00
Thomas Haller
42aea87d51
keyfile: use NMStrBuf in nm_keyfile_plugin_kf_set_integer_list_uint8()
Previously, we were preallocating a string buffer of fixed size. For guint8
we reserved 3 characters per number, which is sufficient. However, it is
not obviously sufficient. NMStrBuf would grow as needed.

Next, I will add nm_keyfile_plugin_kf_set_integer_list_uint(), where it
is more unclear how large the string can be at most. To avoid that question
from the start, it will use NMStrBuf. To keep the implementations similar,
use NMStrBuf also in this case.
2020-05-04 12:47:11 +02:00
Thomas Haller
ff84211cf6
keyfile: refactor defining keyfile list getter/setter functions 2020-05-04 12:47:11 +02:00
Thomas Haller
867964d7e0
keyfile: refactor defining keyfile getter/setter functions
Split the macros to define the setter and getter so that setters
and getters are defined by separate macros. This will be used
to define the boolean getter differently, but still using the
macro to define the setter.

Also, don't construct function names in the macro. Instead, pass
the full names as argument to the macro. This helps with the problem
where ctags/cscope is unable to locate the implementation of the
function. Since we define the function with macro, the tools still
don't recognize this as the location of the definition. But at least
when showing all occurrences of the name, it can be found.
2020-05-04 12:47:07 +02:00
Beniamino Galvani
c5d1d4c498 n-dhcp4: don't fail dispatch in case of receive errors
Currently any error encountered in n_dhcp4_c_connection_dispatch_io()
causes a dispatch failure and interrupts the library state
machine. The recvmsg() on the socket can fail for different reasons;
one of these is for example that the UDP request previously sent got a
ICMP port-unreachable response. This can be reproduced in the
following way:

 ip netns add ns1
 ip link add veth0 type veth peer name veth1
 ip link set veth1 netns ns1
 ip link set veth0 up

 cat > dhcpd.conf <<EOF
 server-identifier 172.25.0.1;
 max-lease-time 120;
 default-lease-time 120;
 subnet 172.25.0.0 netmask 255.255.255.0 {
        range 172.25.0.100 172.25.0.200;
 }
 EOF

 ip -n ns1 link set veth1 up
 ip -n ns1 address add dev veth1 172.25.0.1/24
 ip netns exec ns1 iptables -A INPUT -p udp --dport 67 -j REJECT
 ip netns exec ns1 dhcpd -4 -cf dhcpd.conf -pf /tmp/dhcp-server.pid

If a client is started on veth0, it is able to obtain a lease despite
the firewall rule blocking DHCP, because dhcpd uses a packet
socket. Then it fails during the renewal because the recvmsg() fails:

 dhcp4 (veth0): send REQUEST of 172.25.0.178 to 172.25.0.1
 dhcp4 (veth0): error -111 dispatching events
 dhcp4 (veth0): state changed bound -> fail

The client should consider such errors non fatal and keep running.

https://bugzilla.redhat.com/show_bug.cgi?id=1829178
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/486
2020-04-30 18:12:08 +02:00
Thomas Haller
7151575920 keyfile: don't declare keyfile getters/setter functions with macro
In general, I like macros. But in this case it seems the make the code harder
to understand than it needs to be. There are repeated patterns in these declarations,
but I feel they are better recognizible by aligning the lines nicely.
2020-04-30 13:58:14 +02:00
Thomas Haller
ee7fbc954e shared/glib: prevent users to use g_cancellable_reset()
When handling a GCancellable, you make decisions based on when the cancelled
property of a GCancellable changes. Correctly handling a cancellable becoming
uncancelled again is really complicated, nor is it clear what it even means:
should the flipping be treated as cancellation or not? Probably if the
cancelled property gets reset, you already start aborting and there is
no way back. So, you would want that a cancellation is always handled.
But it's hard to implement that correctly, and it's odd to claim
something was cancelled, if g_cancellable_is_cancelled() doesn't agree
(anymore).

Avoid such problems by preventing users to call g_cancellable_reset().
2020-04-28 18:35:59 +02:00
Thomas Haller
32664c72a5 shared: add nm_gbytes_get_empty() singleton getter 2020-04-28 18:35:59 +02:00
Thomas Haller
2a26562ec8 shared: add nm_gbytes_hash() and nm_gbytes_equal() 2020-04-28 18:35:59 +02:00
Thomas Haller
9b295f0df5 dhcp: make connection.mud-url configurable as global connection default
Conceptionally, the MUD URL really depends on the device, and not so
much the connection profile. That is, when you have a specific IoT
device, then this device probably should use the same MUD URL for all
profiles (at least by default).

We already have a mechanism for that: global connection defaults. Use
that. This allows a vendor drop pre-install a file
"/usr/lib/NetworkManager/conf.d/10-mud-url.conf" with

  [connection-10-mud-url]
  connection.mud-url=https://example.com

Note that we introduce the special "connection.mud-url" value "none", to
indicate not to use a MUD URL (but also not to consult the global connection
default).
2020-04-28 13:01:18 +02:00
Thomas Haller
dec1678fec dhcp: enforce MUD URL to use "https://" scheme
nm_sd_http_url_is_valid_https() is rather clunky, but it is
this way, because we must not disagree with systemd code
about what makes a valid URL.

RFC 8520 says "MUD URLs MUST use the "https" scheme".

See-also: https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/463#note_476190

Fixes: cedcea5ee8 ('libnm: fix verification of connection:mud-url property')
2020-04-24 20:54:13 +02:00
Thomas Haller
cd5157a0c3 shared: add nm_utils_invoke_on_timeout()
Add nm_utils_invoke_on_timeout() beside nm_utils_invoke_on_idle().
They are fundamentally similar, except one schedules an idle handler
and the other a timeout.

Also, use the current g_main_context_get_thread_default() as context
instead of the singleton instance. That is a change in behavior, but
the only caller of nm_utils_invoke_on_idle() is the daemon, which
doesn't use different main contexts. Anyway, to avoid anybody being
tripped up by this also change the order of arguments. It anyway
seems nicer to first pass the cancellable, and the callback and user
data as last arguments. It's more in line with glib's asynchronous
methods.

Also, in the unlikely case that the cancellable is already cancelled
from the start, always schedule an idle action to complete fast.
2020-04-24 13:58:46 +02:00
Thomas Haller
468c2e01ab systemd: add nm_sd_http_url_is_valid() to access internal http_url_is_valid() 2020-04-24 10:09:50 +02:00
Thomas Haller
95ccfdb69a shared: add NM_CMP_DIRECT_PTR() macro 2020-04-22 09:49:45 +02:00
Thomas Haller
2cf31bfef0 keyfile: minor cleanup handling error in read_array_of_uint()
Why "if (length > G_MAXUINT)"? This is never going to hit. Also,
we probably should actual missing keys handle differently from
empty lists. If @error is set, return without setting the property.
2020-04-15 22:37:51 +02:00
Thomas Haller
8f46425b11 keyfile: avoid assertion failure in nm_keyfile_plugin_kf_get_{string,integer}_list()
g_key_file_get_integer_list() can return %NULL without setting an error.
That is the case if the key is set to an empty value.

For X sake, this API. Read the documentation and figure out whether
the function can return %NULL without reporting an error.

Anyway, avoid the assertion failure.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/issues/412
2020-04-15 22:37:24 +02:00
Thomas Haller
12c2aacea7 keyfile: cleanup mac_address_parser() 2020-04-15 11:25:23 +02:00
Beniamino Galvani
f2756b930e release: bump version to 1.25.0 (development) 2020-04-10 18:08:10 +02:00
Thomas Haller
f3ca61e6e4 shared/trivial: fix typo in code comment and reword 2020-04-10 10:55:22 +02:00
Thomas Haller
3e1e63e57d cli/polkit: make parsing polkit-agent-helper-1 protocol more conforming
- in io_watch_have_data(), ensure that we handle incomplete lines
that don't yet have a newline by waiting for more data. That means,
if the current content of the in_buffer does not have a newline, we
wait longer.

- in io_watch_have_data(), implement (and ignore) certain commands
instead of failing the request.

- in io_watch_have_data(), no longer g_compress() the entire line.
"polkitagenthelper-pam.c" never backslash escapes the command, it
only escapes the arguments. Of course, there should be no difference
in practice, except that we don't want to handle escape sequences
in the commands.

- in io_watch_have_data(), compare SUCCESS/FAILURE literally.
"polkitagenthelper-pam.c" never appends any trailing garbage to these
commands, and we shouldn't handle that (although "polkitagentsession.c"
does).

- when io_watch_have_data() completes with success, we cannot destroy
AuthRequest right away. It probably still has data pending that we first
need to write to the polkit helper. Wait longer, and let io_watch_can_write()
complete the request.

- ensure we always answer the GDBusMethodInvocation. Otherwise, it gets
leaked.

- use NMStrBuf instead of GString.
2020-04-10 10:44:57 +02:00