Commit graph

28565 commits

Author SHA1 Message Date
Thomas Haller
91e6f4ff52
glib-aux: add NM_STRV_EMPTY_CC() helper macro
(cherry picked from commit 92daaff7d1)
2021-06-21 17:25:41 +02:00
Thomas Haller
feb262f24d
libnm: add helper functions for emitting signals in NMConnection
Not very useful, but it seems nicer to read. They anyway can be
inlined. After all, naming and structure is important and the places
where we emit signals are important. By having well-named helper
functions, these places are easier to find and reason about.

(cherry picked from commit 60957a4c8a)
2021-06-21 17:25:40 +02:00
Thomas Haller
bce9de78d9
core: add nm_settings_get_connection_sorted_by_autoconnect_priority()
Turns out, we call nm_settings_get_connection_clone() *a lot* with sort order
nm_settings_connection_cmp_autoconnect_priority_p_with_data().

As we cache the (differently sorted) list of connections, also cache
the presorted list. The only complication is that every time we still
need to check whether the list is still sorted, because it would be
more complicated to invalidate the list when an entry changes which
affects the sort order. Still, such a check is usually successful
and requires "only" N-1 comparisons.

(cherry picked from commit e7b5650eff)
2021-06-21 17:25:40 +02:00
Thomas Haller
ecdd64cb1d
core: add nm_settings_connection_cmp_autoconnect_priority_with_data() helper
(cherry picked from commit 1f09e13f43)
2021-06-21 17:25:39 +02:00
Thomas Haller
830c970196
glib-aux: add nm_utils_ptrarray_is_sorted() helper
(cherry picked from commit f51d3862f9)
2021-06-21 17:25:38 +02:00
Thomas Haller
c5914709f2
core: avoid undefined behavior comparing plain pointer values in _cmp_last_resort()
(cherry picked from commit 85df025e93)
2021-06-21 17:25:38 +02:00
Thomas Haller
5ccbe04788
libnm: optimize NM_CONNECTION_GET_PRIVATE() for NMSimpleConnection
NMConnection is a glib interface, implemented only by NMSimpleConnection
and NMRemoteConnection.

Inside the daemon, every NMConnection instance is always a NMSimpleConnection.

Using glib interfaces has an overhead, for example NM_IS_CONNECTION() needs
to search the implemented types for the pointer. And NM_CONNECTION_GET_PRIVATE()
is implemented by attaching user data to the GObject instance. Both have measurable
overhead.

Special case them for NMSimpleConnection.

This optimizes primarily the call to nm_connection_get_setting_connection(),
which easily gets called millions of times. This is easily measurable.

(cherry picked from commit 7a71aedf46)
2021-06-21 17:25:37 +02:00
Thomas Haller
1c6b50f6ea
libnm: add code comment about preserving ABI for libnm GObject structs
(cherry picked from commit f3abf2491a)
2021-06-21 17:25:36 +02:00
Thomas Haller
c4257c1c7f
libnm: avoid cloning buffer for nm_connection_get_settings() in nm_keyfile_write()
(cherry picked from commit b0f4bb84bf)
2021-06-21 17:25:36 +02:00
Thomas Haller
919618372c
libnm: add _nm_connection_get_settings_arr() helper
(cherry picked from commit 5aef93355f)
2021-06-21 17:25:36 +02:00
Thomas Haller
4949434dac
libnm: take reference to settings in nm_connection_for_each_setting_value()
As we iterate over the settings, let's ensure that they stay
alive while we call back to the user data.

(cherry picked from commit 207b101238)
2021-06-21 17:25:35 +02:00
Thomas Haller
eb10539b80
libnm: avoid cloning list of settings in nm_connection_to_dbus_full()
(cherry picked from commit d829849a7b)
2021-06-21 17:25:35 +02:00
Thomas Haller
07ba3cb0d7
libnm: implement nm_connection_get_setting*() via NMMetaSettingType
The NM_TYPE_SETTING_* macros are really function calls (to a GType/gsize which is
guarded by an atomic operation for thread safe initialization). Also, finding
the setting_info based on the GType requires additional lookups.

It's no longer necessary. We can directly find the setting using the
well known index.

(cherry picked from commit 97eef2bf6d)
2021-06-21 17:25:35 +02:00
Thomas Haller
61013c6995
libnm: avoid cloning list of settings in _nm_connection_verify()
(cherry picked from commit c8c606b323)
2021-06-21 17:25:35 +02:00
Thomas Haller
0a22f4e490
libnm: refactor tracking of NMSetting in NMConnection
A NMConnection tracks a list of NMSetting instances. For
each setting type, it only can track one instance, as is
clear by the API nm_connection_get_setting().

The number of different setting types is known at compile time,
currently it is 52. Also, we have an NMMetaSettingType enum,
which assigns each type a number.

Previously, we were tracking the settings in a GHashTable.
Rework that, to instead use a fixed size array.

Now every NMConnection instance consumes 52 * sizeof(pointer)
for the settings array. Previously, the GHashTable required to malloc
the "struct _GHashTable" (on 64bit that is about the size of 12
pointers) and for N settings it allocated two buffers (for
the key and the values) plus one buffer for the hash values. So,
it may or may not consume a bit more memory now, but also can lookup
settings directly without hashing.

When looking at all settings, we iterate the entire array. Most
entries will be NULL, so it's a question whether this could be done
better. But as the array is of a fixed, small size, naive iteration
is probably still faster and simpler than anything else.

---

Test: compiled with -O2, x86_64:

  $ T=src/core/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh; \
    make -j 8 "$T" && \
    "$T" 1>/dev/null && \
    perf stat -r 200 -B "$T" 1>/dev/null

Before:

 Performance counter stats for 'src/core/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh' (200 runs):

            338.39 msec task-clock:u              #    0.962 CPUs utilized            ( +-  0.68% )
                 0      context-switches:u        #    0.000 K/sec
                 0      cpu-migrations:u          #    0.000 K/sec
             1,121      page-faults:u             #    0.003 M/sec                    ( +-  0.03% )
     1,060,001,815      cycles:u                  #    3.132 GHz                      ( +-  0.50% )
     1,877,905,122      instructions:u            #    1.77  insn per cycle           ( +-  0.01% )
       374,065,113      branches:u                # 1105.429 M/sec                    ( +-  0.01% )
         6,862,991      branch-misses:u           #    1.83% of all branches          ( +-  0.36% )

           0.35185 +- 0.00247 seconds time elapsed  ( +-  0.70% )

After:

 Performance counter stats for 'src/core/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh' (200 runs):

            328.07 msec task-clock:u              #    0.959 CPUs utilized            ( +-  0.39% )
                 0      context-switches:u        #    0.000 K/sec
                 0      cpu-migrations:u          #    0.000 K/sec
             1,130      page-faults:u             #    0.003 M/sec                    ( +-  0.03% )
     1,034,858,368      cycles:u                  #    3.154 GHz                      ( +-  0.33% )
     1,846,714,951      instructions:u            #    1.78  insn per cycle           ( +-  0.00% )
       369,754,267      branches:u                # 1127.052 M/sec                    ( +-  0.01% )
         6,594,396      branch-misses:u           #    1.78% of all branches          ( +-  0.23% )

           0.34193 +- 0.00145 seconds time elapsed  ( +-  0.42% )

(cherry picked from commit 91aacbef41)
2021-06-21 17:25:34 +02:00
Thomas Haller
5e3401fa31
libnm/tests: test consistency for nm_meta_setting_types_by_priority
(cherry picked from commit 042cd99049)
2021-06-21 17:25:34 +02:00
Thomas Haller
481e229450
libnm: add nm_meta_setting_types_by_priority array for sorting settings
nm_meta_setting_infos is a list of all NMMetaSettingInfo, sorted by name.
Add nm_meta_setting_types_by_priority which provides a mapping with a
different sort order (first by priority). We need that sometimes.

(cherry picked from commit b7a7cc1b13)
2021-06-21 17:25:34 +02:00
Thomas Haller
4b3ba8f82b
libnm: pack NMMetaSettingType enum
We keep the enum around in memory, so let's make it smaller/packed.

(cherry picked from commit 1a5a4838f1)
2021-06-21 17:25:34 +02:00
Thomas Haller
be9b6e8c01
libnm: add nm_meta_setting_info helpers
(cherry picked from commit 16b01233fa)
2021-06-21 17:25:34 +02:00
Thomas Haller
418ce1b27c
dhcp/systemd: ignore FORCERENEW requests for DHCPV4 to workaround CVE-2020-13529
The FORCERENEW reqest was not authenticated (because neither rfc3118 nor
rfc6704) is implemented. That is a potential security issue.

As workaround, patch the source to ignore those requests. Note that also
nettools implementation ignores FORCERENEW requests, so if there would
be a need to handle them, then it would be important to improve the
nettools code (which is the main implementation).

The systemd DHCP plugin is no longer used by default. The user explicitly has to
enable it via the undocumented "[main].dhcp=systemd" option in NetworkManager.conf.
Hence, this change is probably not very important either way.

See-also: https://bugzilla.redhat.com/show_bug.cgi?id=1959398
See-also: https://github.com/systemd/systemd/issues/16774
https://bugzilla.redhat.com/show_bug.cgi?id=1966123
CVE-2020-13529

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/898
(cherry picked from commit 1441b04bba)
2021-06-21 16:26:04 +02:00
Beniamino Galvani
2632bb9273 merge: branch 'bg/rh1820770'
https://bugzilla.redhat.com/show_bug.cgi?id=1820770
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/889

(cherry picked from commit 5995653312)
2021-06-21 10:14:19 +02:00
Beniamino Galvani
c8c7e8320b device: prefer IPv6 not-deprecated addresses for hostname lookup
In presence of a IPv6 deprecated address and a non-deprecated one, the
latter will be used by kernel for new connections according to RFC
6724 section 5 (Source Address Selection). Prefer it also to lookup a
hostname via reverse DNS.

While at it, also prefer non-link-local addresses over link-local
ones.

(cherry picked from commit 3c55db886a)
2021-06-21 10:14:19 +02:00
Beniamino Galvani
2ca56c9bbd libnm-platform: add NM_PLATFORM_MATCH_WITH_ADDRSTATE_DEPRECATED
Add a new flag to match deprecated addresses. An address is deprecated
when its preferred lifetime has expired but its valid lifetime has
not.

Address deprecation is one of the criteria for source address
selection in IPv6. For IPv4 the deprecation doesn't have any
real effect.

Note that this commit changes the behavior of
nm_ip_config_get_first_address(WITH_ADDRSTATE_NORMAL), since now
deprecated addresses are not returned. However this should not impact
existing callers since they either:

 - request a IPv6 (WITH_ADDRTYPE_LINKLOCAL | WITH_ADDRSTATE_NORMAL)
   address; IPv6 link-local addresses are supposed to have infinite
   lifetimes;

 or

 - request a IPv6 (WITH_ADDRTYPE_NORMAL | WITH_ADDRSTATE__ANY)
   address.

(cherry picked from commit ff84a4736d)
2021-06-21 10:14:19 +02:00
Beniamino Galvani
a3a282677b core: add nm_ip_config_find_first_address()
Replace nm_ip6_config_find_first_address() with a version generic for
IPv4 and IPv6.

(cherry picked from commit ca31cbbc74)
2021-06-21 10:14:19 +02:00
Beniamino Galvani
786ab294db libnm-platform: add nm_platform_ip_address_match()
Replace nm_platform_ip6_address_match() with a version generic for
IPv4 and IPv6.

(cherry picked from commit 376c7f8315)
2021-06-21 10:14:19 +02:00
Thomas Haller
a1f4255f71
NEWS: belatedly add news entry for 1.32.0
(cherry picked from commit 89af6be1d6)
2021-06-21 10:00:44 +02:00
Beniamino Galvani
21051dc6d8 policy: prefer IPv4 to determine the hostname
When determining the hostname, it is preferable to evaluate devices in
a predictable order to avoid that the hostname changes between
different boots.

The current order is based first on hostname priority, then on the
presence of a best default route, and then on activation order.

The activation order is not a very strong condition, as it is
basically useless for devices that are autoactivated at boot.

As we already prefer IPv4 over IPv6 within the same connection, also
prefer it when 2 connections have the same priority and the same
default route status, to achieve better predictability.

https://bugzilla.redhat.com/show_bug.cgi?id=1970335
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/895
(cherry picked from commit 637a45e25b)
2021-06-21 08:57:30 +02:00
Thomas Haller
229b9a979c
release: bump version to 1.32.1 (development) 2021-06-16 19:48:59 +02:00
Thomas Haller
d9c0d43879
release: bump version to 1.32.0 2021-06-16 19:48:59 +02:00
Thomas Haller
f330413f8a
NEWS: update 2021-06-16 19:06:47 +02:00
Thomas Haller
2d46c10fa3
contrib/rpm: configure defaults for iptables/nftables when generating distribution tarball
(cherry picked from commit e831a67cbc)
2021-06-16 19:03:28 +02:00
Beniamino Galvani
6914f4213f nm-daemon-helper: fix indentation
Ops.

Fixes: 9b802ff7f2 ('nm-daemon-helper: fix build with musl libc')
(cherry picked from commit e3e0afb011)
2021-06-16 18:33:59 +02:00
Beniamino Galvani
1d2f8f983c nm-daemon-helper: fix build with musl libc
NSS is not available in musl libc:

./src/nm-daemon-helper/nm-daemon-helper.c:9:10: fatal error: nss.h: No such file or directory
    9 | #include <nss.h>
      |          ^~~~~~~

(cherry picked from commit 9b802ff7f2)
2021-06-16 18:26:07 +02:00
Thomas Haller
798395ed33
NEWS: update
(cherry picked from commit 32bc29591c)
2021-06-16 16:58:44 +02:00
Beniamino Galvani
a06cd08430 NEWS: update
(cherry picked from commit e29d2f2200)
2021-06-16 16:19:23 +02:00
Beniamino Galvani
098b4abe06 firewall: fix double free
Fixes: 9ebdb967de ('firewall: implement masquerading for shared mode with nftables')
(cherry picked from commit 0cd20f94d0)
2021-06-15 19:45:48 +02:00
Thomas Haller
46533e7da4
firewall: use nm_g_main_context_push_thread_default_if_necessary() in _fw_nft_call_communicate_cb()
There is no need to push (and pop) the same main-context.

(cherry picked from commit 654bb92a69)
2021-06-15 19:13:54 +02:00
Thomas Haller
4badc1f33a
firewall: fix signalling timeout error reason from _fw_nft_call()
During timeout we cancel the (internal) GCancellable. But the overall
error reason is not cancellation by the user, it's timeout. Fix
the error reason.

Fixes: 9ebdb967de ('firewall: implement masquerading for shared mode with nftables')
(cherry picked from commit 897c6a5744)
2021-06-15 19:13:53 +02:00
Thomas Haller
eea912dfb3
libnm: expose nm_ip_address_dup(), nm_ip_route_dup() API in libnm
This fixes commit 21c8a6b20e ('libnm-core, all: merge IPv4 and IPv6
address/route types'), which introduced this API but didn't export it
in the library. In practice this API is thus only usable since 1.32.0.

(cherry picked from commit 05f2a0b024)
2021-06-15 19:13:27 +02:00
Thomas Haller
123b408c32
firewall: change default for firewall-backend to "nftables""
It seems that the nftables backend works well. Let's change the default.
This will also be backported to nm-1-32 branch, for 1.32.0 release.

This reverts commit 0609f1f31c.

(cherry picked from commit 79d9441567)
2021-06-14 13:56:21 +02:00
Thomas Haller
f353cb95e5
all: merge branch 'th/coverity-fixes'
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/883

(cherry picked from commit c47ad0f754)
2021-06-11 22:44:32 +02:00
Thomas Haller
6df0c8e9bb
libnm/tests: avoid coverity warning in test_setting_connection_secondaries_verify()
nm_strvarray_get_strv() returns the input pointer itself, if _secondaries is NULL.
It does so intentionally and correctly to create an artificial empty strv array.

Coverity doesn't like this. Try to workaround the warning:

    Error: ARRAY_VS_SINGLETON (CWE-119): [#def484]
    NetworkManager-1.31.90/src/libnm-core-impl/tests/test-setting.c:4544: address_of: Taking address with "&_secondaries" yields a singleton pointer.
    NetworkManager-1.31.90/src/libnm-core-impl/tests/test-setting.c:4544: identity_transfer: Passing "&_secondaries" as argument 1 to function "nm_strvarray_get_strv", which returns that argument.
    NetworkManager-1.31.90/src/libnm-core-impl/tests/test-setting.c:4544: callee_ptr_arith: Passing "_Generic (nm_strvarray_get_strv(&_secondaries, NULL), char const * const * : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), char const ** : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), char * const * : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), char ** : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), void const * : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), void * : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), char const * const * const : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), char const ** const : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), char * const * const : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), char ** const : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), void const * const : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), void * const : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL))" to function "_nm_utils_strv_cmp_n" which uses it as an array. This might corrupt or misinterpret adjacent memory locations.
    # 4542|       G_STMT_END
    # 4543|
    # 4544|->         _assert_secondaries(s_con, (const char *const *) arr->pdata);
    # 4545|
    # 4546|           /* reimplement the normalization that we expect to happen and

(cherry picked from commit 14ec96f262)
2021-06-11 22:44:32 +02:00
Thomas Haller
91f5c5e763
cli: avoid coverity warning in do_connection_down()
Error: USE_AFTER_FREE (CWE-416): [#def729] [important]
    NetworkManager-1.31.90/src/nmcli/connections.c:3288: freed_arg: "connection_cb_info_finish" frees "info".
    NetworkManager-1.31.90/src/nmcli/connections.c:3287: pass_freed_arg: Passing freed pointer "info" as an argument to "g_signal_handlers_disconnect_matched".
    # 3285|
    # 3286|               if (info) {
    # 3287|->                 g_signal_handlers_disconnect_by_func(active, down_active_connection_state_cb, info);
    # 3288|                   connection_cb_info_finish(info, active);
    # 3289|               }

(cherry picked from commit 627503ad86)
2021-06-11 22:44:31 +02:00
Thomas Haller
c47c823c9d
glib-aux/tests: avoid coverity warning in test_nm_g_source_sentinel()
Coverity wrongly think there is a use after free in the test:

    Error: USE_AFTER_FREE (CWE-416): [#def559] [important]
    NetworkManager-1.31.90/src/libnm-glib-aux/tests/test-shared-general.c:1305: alias: Assigning: "s1" = "_s". Now both point to the same storage.
    NetworkManager-1.31.90/src/libnm-glib-aux/tests/test-shared-general.c:1324: freed_arg: "g_source_unref" frees "s1".
    NetworkManager-1.31.90/src/libnm-glib-aux/tests/test-shared-general.c:1330: deref_after_free: Dereferencing freed pointer "s1".
    # 1328|               s2 = nm_g_source_sentinel_get(0);
    # 1329|               g_assert(s2 == s1);
    # 1330|->             g_assert_cmpint(g_atomic_int_get(&s1->ref_count), >=, 1);
    # 1331|           }
    # 1332|       }

Rework the code in the hope to avoid the false warning.

(cherry picked from commit 7825609f1f)
2021-06-11 22:44:31 +02:00
Thomas Haller
025a3a60b4
platform: avoid wrong coverity warning in nmp_utils_sysctl_open_netdir()
The warning is wrong, because we already assert for the string length a few
lines earlier.

  Error: STRING_OVERFLOW (CWE-120): [#def595]
  NetworkManager-1.31.90/src/libnm-platform/nm-platform-utils.c:1896: fixed_size_dest: You might overrun the 16-character fixed-size string "ifname_buf_last_try" by copying "ifname" without checking the length.
  # 1894|           if (nm_streq(ifname, ifname_buf_last_try))
  # 1895|               return -1;
  # 1896|->         strcpy(ifname_buf_last_try, ifname);
  # 1897|
  # 1898|           fd_dir = open(sysdir, O_DIRECTORY | O_CLOEXEC);

(cherry picked from commit c87433ebd2)
2021-06-11 22:44:31 +02:00
Thomas Haller
a46c401dbf
libnm: merge branch 'th/libnm-core-hide-structs'
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/888

(cherry picked from commit b57167fe74)
2021-06-11 22:36:54 +02:00
Thomas Haller
915639a5f5
NEWS: update
(cherry picked from commit 0206642ec3)
2021-06-11 22:36:52 +02:00
Thomas Haller
fd2c614b71
libnm: hide NMSimpleConnection type from public headers
(cherry picked from commit 860b280248)
2021-06-11 22:36:51 +02:00
Thomas Haller
65278461a3
libnm: hide NMSetting types from public headers
When subclassing a GObject type, the class and object structs
must be available and defined in the header.

For libnm, and in particular for NMSetting classes, we don't want
users to subclass NMSetting. It also doesn't work, because libnm
has internal code that is necessary to hook up the NMSetting class.
You cannot define your own type and make it work together with
libnm.

Having the structs in public headers limits what we can do with them.
For example, we could embed the private data directly in the structures
and avoid the additional indirection.

This is an API break, but for something that most likely nobody cares
about. Or better, nobody should care about. API is not what is
accidentally defined in a header, API was the library provides to
meaningfully use. Subclassing these types is not meaningful and was
only accidentally possible so far.

Only hide the structs for now. More cleanup is possible later. We shall
however aim to keep the padding and struct layout to not also break ABI.

(cherry picked from commit e46d484fae)
2021-06-11 22:36:50 +02:00
Thomas Haller
f07c135c42
core: avoid accessing opaque NMSetting type
Next, NMSetting will be hidden from public headers and become an opaque type.
We cannot do typeof(*setting) anymore.

(cherry picked from commit 0d6c35a6d9)
2021-06-11 22:36:50 +02:00