Commit graph

57 commits

Author SHA1 Message Date
Thomas Haller
2e00d161b2
wireguard: prefer last resolved IP from resolving endpoint from DNS
We periodically re-resolve the DNS name for entpoints. Since WireGuard
has no concept of being connected, we want to eventually pick up
if the DNS name resolves to a different IP address.

However, on resolution failure, we will never clear the endpoint we
already have. Thus, resolving names can only give a better endpoint,
not remove an IP address entirely.

DNS names might do Round-Robin load distribution and the name of the
endpoint might resolve to multiple IP addresses. Improve to stick to
the IP address that we already have -- provided that the IP address
is still among the new resolution result. Otherwise, we continue to
pick the first IP address that was resolved.

(cherry picked from commit 98348ee539)
2021-02-16 14:14:46 +01:00
Thomas Haller
29336aa2e2
platform: ensure NM_SOCK_ADDR_UNION_INIT_UNSPEC() fully initializes union
In C, initialization of a union does not define that excess memory
is initialized. Ensure that, by initializing the largest member of the
NMSockAddrUnion union.

(cherry picked from commit 7bf2ddf73f)
2021-02-16 14:14:46 +01:00
Thomas Haller
4113e93b3b
platform/tests: skip tests if "unshare(CLONE_NEWNET|CLONE_NEWNS)" fails
Inside a podman container (without `--priviledged`) we don't have
permissions for "unshare(CLONE_NEWNET|CLONE_NEWNS)".

It's not useful to fail tests in environments where they cannot run.
Skip them.

(cherry picked from commit ecdbb1ab84)
2021-02-15 21:18:04 +01:00
Andrew Zaborowski
210d2696a9
iwd: Fix the leaks in get_agent_request_network_path
Don't request new copies of strings from g_variant_get() to avoid
leaking memory as pointed out by Thomas Haller.

Fixes: dc0e31fb70 ('iwd: Add the wifi.iwd.autoconnect setting')
(cherry picked from commit 5ccb8ce17a)
2021-02-15 09:49:38 +01:00
Andrew Zaborowski
190ed7b2c9
iwd: Fix agent DBus method parameter types
The object path DBus type wasn't being used correctly in the parameters
signatures, fix them.
2021-02-11 16:34:09 +01:00
Andrew Zaborowski
c8b283b818
settings: Fix REASON_CLEAR_DEFAULT_WIRED comment typos 2021-02-11 16:34:08 +01:00
Thomas Haller
5ca018c0db
lldp/tests: try workaround failure with ioctl(TUNSETIFF)
On copr build, it seems possible that the ioctl fails with

  ERROR: src/core/devices/tests/test-lldp - Bail out! NetworkManager:ERROR:src/core/devices/tests/test-lldp.c:823:_test_recv_fixture_setup: assertion failed (errno == 0): (1 == 0)

(1 is EPERM). Unclear why this happens. But as it only affects the
test setup, retry a few times.
2021-02-11 16:04:46 +01:00
Thomas Haller
4b874019ad
dhcp: downgrade logging messages for DHCP to <debug>
Granted, for debugging this information is useful. However, to actually
debug an issue thoroughly, level=TRACE is anyway required. There is simply
no way how we can log useful debug information and not flood logging
messages for regular use.

For example, logging the DHCP lease options can easily print 30 lines.
And this, every time you get a lease update (e.g. every 30 minutes) and
for every interface that does DHCP.

It's simply too verbose. Downgrade the logging level.

Yes, now our default <info> level is even less useful to understand what
is going on. But the majority of time, users don't care so not spamming
the log is more important.

However, we still log the DHCP event (and the IP address) with <info>
level.
2021-02-11 14:33:32 +01:00
Thomas Haller
24abf13239
dhcp: binary search in nm_dhcp_option_find()
Let's use binary search.

Test patch:

    diff --git a/src/core/dhcp/tests/test-dhcp-utils.c b/src/core/dhcp/tests/test-dhcp-utils.c
    index 9b54e2cd0228..007993341672 100644
    --- a/src/core/dhcp/tests/test-dhcp-utils.c
    +++ b/src/core/dhcp/tests/test-dhcp-utils.c
    @@ -788,6 +788,24 @@ NMTST_DEFINE();
     int
     main(int argc, char **argv)
     {
    +    int i;
    +    guint idx;
    +    guint c;
    +
    +    idx = 0;
    +    c = 0;
    +    for (i = 0; i < 1000000; i++) {
    +        const guint option = _nm_dhcp_option_dhcp4_options[idx % G_N_ELEMENTS(_nm_dhcp_option_dhcp4_options)].option_num;
    +
    +        idx += 2010055757;
    +
    +        if (nm_dhcp_option_find(AF_INET, option)->name)
    +            c++;
    +    }
    +    g_print(">%u\n", c);
    +
    +    return 0;
    +
         nmtst_init_assert_logging(&argc, &argv, "WARN", "DEFAULT");

         g_test_add_func("/dhcp/generic-options", test_generic_options);

Build:

  CFLAGS='-O2' ./autogen.sh --with-more-asserts=0
  make -j 10 src/core/dhcp/tests/test-dhcp-utils && \
    src/core/dhcp/tests/test-dhcp-utils && \
    perf stat -r 200 -B src/core/dhcp/tests/test-dhcp-utils

Before:

 Performance counter stats for 'src/core/dhcp/tests/test-dhcp-utils' (200 runs):

             82.83 msec task-clock:u              #    0.994 CPUs utilized            ( +-  0.21% )
                 0      context-switches:u        #    0.000 K/sec
                 0      cpu-migrations:u          #    0.000 K/sec
               579      page-faults:u             #    0.007 M/sec                    ( +-  0.03% )
       264,676,245      cycles:u                  #    3.195 GHz                      ( +-  0.06% )
       544,792,266      instructions:u            #    2.06  insn per cycle           ( +-  0.00% )
       151,624,848      branches:u                # 1830.472 M/sec                    ( +-  0.00% )
         1,083,780      branch-misses:u           #    0.71% of all branches          ( +-  0.01% )

          0.083328 +- 0.000178 seconds time elapsed  ( +-  0.21% )

After:

 Performance counter stats for 'src/core/dhcp/tests/test-dhcp-utils' (200 runs):

             39.21 msec task-clock:u              #    0.987 CPUs utilized            ( +-  0.57% )
                 0      context-switches:u        #    0.000 K/sec
                 0      cpu-migrations:u          #    0.000 K/sec
               579      page-faults:u             #    0.015 M/sec                    ( +-  0.03% )
       115,396,123      cycles:u                  #    2.943 GHz                      ( +-  0.23% )
       137,664,630      instructions:u            #    1.19  insn per cycle           ( +-  0.00% )
        25,866,025      branches:u                #  659.597 M/sec                    ( +-  0.00% )
         1,919,616      branch-misses:u           #    7.42% of all branches          ( +-  0.12% )

          0.039717 +- 0.000227 seconds time elapsed  ( +-  0.57% )
2021-02-11 13:00:18 +01:00
Thomas Haller
1cbe926c20
dhcp: rework DHCP options to not carry around option array
Previously, we would pass around the list of options. However,

- that isn't too nice to read. Also, usually when we want to treat
  IP address families generically, then we have an addr_family argument.
  Having to first resolve the addr_family to another set of variables
  is inconvenient.

- the option array itself doesn't have enough information. For example,
  we don't know how many elements there are, we don't know which address
  family it is (unless we compare it to one of the two well known
  lists).
  For example, I'd like to do a binary search for the option. But that's
  not immediately possible, because the length is unknown.

- in practice, there are only two address families: AF_INET and
  AF_INET6. It is extremely unlikely that we will require a third
  DHCP options list, and even if we had that, the addr_family argument
  still abstracts them nicely.
  We also don't need two different lists for one DHCP type. While that
  would currently be possible (and afterwards not anymore), it would
  be wrong to do.

- also add a new accessor nm_dhcp_option_find() to find the NMDhcpOption
  instance by option number.
2021-02-11 12:26:18 +01:00
Thomas Haller
53f137af6e
dhcp/nettools: accept any number of trailing NULs in string options
https://tools.ietf.org/html/rfc2132#section-2 says:

   Options containing NVT ASCII data SHOULD NOT include a trailing NULL;
   however, the receiver of such options MUST be prepared to delete trailing
   nulls if they exist.

It speaks in plurals.
2021-02-11 09:23:20 +01:00
Thomas Haller
3b8882b978
dhcp/nettools: use NMStrBuf in lease_save() 2021-02-11 09:23:19 +01:00
Thomas Haller
4707cf5fab
dhcp/nettools: cleanup lease_parse_search_domains() 2021-02-11 09:23:19 +01:00
Thomas Haller
8366fd87b9
dhcp/nettools: make data pointer const 2021-02-11 09:23:18 +01:00
Thomas Haller
ce72563a8c
dhcp/nettools: cleanup nm_dhcp_lease_data_parse_search_list() 2021-02-11 09:23:17 +01:00
Thomas Haller
6e0d2e5850
dhcp/nettools: move nm_dhcp_lease_data_parse_search_list() to nm-dhcp-utils.c 2021-02-11 09:23:16 +01:00
Thomas Haller
67dd25a396
shared,dhcp: add _nm_utils_ip4_get_default_prefix0() helper 2021-02-11 09:23:15 +01:00
Thomas Haller
94e474fa62
dhcp/nettools: cleanup lease_parse_routes() 2021-02-11 09:23:15 +01:00
Thomas Haller
2be43d79f7
dhcp/nettools: refactor parsing of DHCP lease (ntps) 2021-02-11 09:23:14 +01:00
Thomas Haller
f986d409f9
dhcp/nettools: cleanup lease_parse_address_list() 2021-02-11 09:23:14 +01:00
Thomas Haller
30911a305f
dhcp/nettools: cleanup lease_parse_address() 2021-02-11 09:23:13 +01:00
Thomas Haller
58b3b7ec3c
dhcp/nettools: refactor parsing of DHCP lease (server-id)
No change in behavior.
2021-02-11 09:23:12 +01:00
Thomas Haller
6850e3640e
dhcp/nettools: refactor parsing of DHCP lease (broadcast)
No change in behavior.
2021-02-11 09:23:12 +01:00
Thomas Haller
a24b7287d8
dhcp/nettools: validate domain-name option (15) differently 2021-02-11 09:23:12 +01:00
Thomas Haller
94c6f3c14e
dhcp/nettools: refactor parsing of DHCP lease (domain-name)
No change in behavior.
2021-02-11 09:23:11 +01:00
Thomas Haller
6c8a9e8bd6
dhcp/nettools: validate nis-domain option (40) differently
Previously, we would check that all characters are ASCII. But we would
also accept NUL characters (and truncate on the first NUL).

Now:

- reject any NUL characters inside the string (except trailing NUL).

- accept all characters, and if necessary backslash-encode non UTF-8.
2021-02-11 09:23:11 +01:00
Thomas Haller
0c93bff179
dhcp/nettools: refactor parsing of DHCP lease (nis-domain) 2021-02-11 09:23:10 +01:00
Thomas Haller
0ef37431cf
dhcp/nettools: validate root-path option (17) to not contain any NUL characters
And make it UTF-8 (by backslash escaping).
2021-02-11 09:23:10 +01:00
Thomas Haller
f2885cdf02
dhcp/nettools: refactor parsing of DHCP lease (root-path) 2021-02-11 09:23:10 +01:00
Thomas Haller
eb16cb6563
dhcp/nettools: validate proxy-autodiscovery option (252) to not contain any NUL characters 2021-02-11 09:23:09 +01:00
Thomas Haller
8f7a2a1ea0
dhcp/nettools: refactor parsing of DHCP lease (wpad) 2021-02-11 09:23:09 +01:00
Thomas Haller
784932550c
dhcp/nettools: validate and normalize Host Name Option (12)
The hostname is in the end a string, which means it must be in a known,
sensible encoding (UTF-8). Previously, we would not ensure the encoding,
nor that the hostname was valid.

Fix that. Follow what systemd does with lease_parse_domain().

See-also: https://tools.ietf.org/html/rfc2132#section-3.14
2021-02-11 09:23:09 +01:00
Thomas Haller
67110d1711
dhcp/nettools: refactor parsing of DHCP lease (hostname) 2021-02-11 09:23:08 +01:00
Thomas Haller
89773b8739
dhcp/nettools: refactor parsing of DHCP lease (metered) 2021-02-11 09:23:08 +01:00
Thomas Haller
de14a376ff
dhcp/nettools: refactor parsing of DHCP lease (mtu) 2021-02-11 09:23:08 +01:00
Thomas Haller
fc83acbd99
dhcp: add nm_dhcp_option_add_option_in_addr() helper 2021-02-11 09:23:07 +01:00
Thomas Haller
41634d5199
dhcp: add nm_dhcp_option_add_option_utf8safe_escape() helper 2021-02-11 09:23:07 +01:00
Thomas Haller
f0a9268718
dhcp: require options argument for nm_dhcp_option_add_option()
It's not clear why the option argument would be optional.
Also, it's not optional for nm_dhcp_option_take_option().

Add an nm_assert() to catch such wrong uses.
2021-02-11 09:23:07 +01:00
Thomas Haller
edc7c7204c
shared: remove "str" argument from nm_str_buf_reset()
NMStrBuf's API is all about convenience. When you reset the buffer,
is it convenient to immediately append a new string?

It seems not. Make nm_str_buf_reset() simpler by doing only one thing.
2021-02-11 09:23:06 +01:00
Beniamino Galvani
020a2707c4 dhcp: nettools: export broadcast and server-id options
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/issues/426
2021-02-10 09:13:03 +01:00
Andrew Zaborowski
9fd0f0c4fa
iwd: Match IWD networks to existing OWE and SAE connection
IWD's "open" networks can be either unsecured or use OWE and "psk"
networks may be using WPA2 personal or WPA3 personal so when looking for
an exsiting NMSettingsConnection matching an IWD KnownNetwork, also
check for these connection key_mgmt types.

Add explicit checks for AP and ADHOC connection modes to exclude OWE and
SAE as they're not supported by IWD in those modes and we don't want to
make it appear like a connection of this type was successfully
activated.
In Infrastructure mode there's won't be any way to know whether IWDxi
established an OWE or unsecured connection (or WPA2-PSK vs. SAE)
regardless of what was set in the NMConnection and it's not considered
to be meaningful (also isn't normally exposed in a GUI) although you
could argue OWE vs. unsecured is a big difference.
2021-02-09 17:09:32 +01:00
Andrew Zaborowski
4aea512b15
iwd: Rename NM_IWD_NETWORK_SECURITY_NONE to _OPEN
IWD doesn't expose on D-Bus, or in the network profile files, the
information on whether a network has no security or uses OWE so they
should be the same thing to the iwd backend (similarly WPA2-Personal and
WPA3-Personal/SAE).  But OWE implies some security against some attacks
so the NONE naming could be misleading.
2021-02-09 17:09:32 +01:00
Thomas Haller
c971ee2267
libnm: merge libnm-keyfile into libnm-core
Before there was a licensing conflict between the keyfile code
(libnm-keyfile) and libnm. The latter would require LGPL-2.1+ while
keyfile code was GPL-2.0+.

Consequently we were linking libnm-keyfile into the daemon, but not in
libnm.so.

This conflict has been resolved and keyfile API is part of libnm.so.
There is no more need to build a separate (intermediary) library. Merge
them.

This also makes sense because keyfile code needs access to private code
from libnm-core. It is closely tied to libnm-core, so that building them
separate makes no sense (anymore).
2021-02-09 12:38:19 +01:00
Thomas Haller
b13a2b27e9
all: move shared/nm-meta-setting.[hc] to libnm-core and clients
"shared/nm-meta-setting.[hc]" contains meta data about settings.
As such it is similarly used by libnm-core (as internal API) and
by clients (as extension of public API of libnm). However, it must
be compiled twice, because while it defines in both cases a
NMMetaSettingInfo type, these types are different between internal and
public API.
Hence, the files must also be compiled twice (and differently), once
against libnm-core and once against the client helper library.

Previously, the file was under "shared/", but there it's a bit odd
it doesn't clearly belong anywhere.

There are two goals here:

 - copy the file to the two places where it is used. We also have
   a "check-tree" unit test that ensures those files don't diverge in
   the future.

 - we no longer require CFLAGS set during built. Instead, the sources
   should control the build. For that we have new (simple) headers
   "nm-meta-setting-base.h" that define the right behavior for the
   impl files.

There is still an ugliness (among several): the files must be named the
same for libnm-core and clients/common. Preferably, all our sources have
unique names, but that is not possible with this scheme (without
introducing other ugliness). To mitigate that, include the files only at
one exact place.
2021-02-09 12:38:19 +01:00
Thomas Haller
66eccf7ad7
all: add "nm-default-systemd{,-shared}.h" as replacement for "nm-default.h" 2021-02-09 12:38:18 +01:00
Thomas Haller
dc2afc9b77
all: add "src/core/nm-default-daemon.h" as replacement for "nm-default.h" 2021-02-09 12:38:18 +01:00
Thomas Haller
d69f12f9e7
all: add "nm-glib-aux/nm-default-glib.h" as replacement for "nm-default.h" 2021-02-09 12:38:17 +01:00
Thomas Haller
0bcd453e8c
initrd/tests: drop special define for test directory
We got rid of all these redundant defines. All we need, is the base
source directory, which we already define in config.h as
NM_BUILD_SRCDIR. Use that.
2021-02-09 12:38:16 +01:00
Thomas Haller
1b8ef3282c
core/dhcp: don't include "nm-sd-adapt-shared.h" in "nm-dhcp-nettools.c"
The adapter header is not for direct inclusion. It's only for
the systemd sources.
2021-02-09 12:38:16 +01:00
Beniamino Galvani
16d649ea92 wifi: auto-activate devices as soon as the first scan finishes
Currently if we detect that a scan finished in
_scan_notify_is_scanning(), we call immediately _scan_kickoff() (which
might start a new scan) and then we check again whether the device can
autoactivate or whether to remove the wifi-scan pending action.

This means that if the scan takes long enough, when
_scan_notify_is_scanning() is called, it is already time to start
another scan and the device activation will be delayed. It will be
delayed until the scan duration becomes shorter than the
exponentially-growing periodic scan interval.

Fix this by delaying the next scan immediately after a scan result.

Co-authored-by: Thomas Haller <thaller@redhat.com>

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/issues/574
2021-02-09 08:55:52 +01:00