Commit graph

29961 commits

Author SHA1 Message Date
Thomas Haller
405bb67e3b dhcp/nettools: better track currently granted lease
When we accept/decline a lease, then that only works if we are in state
GRANTED. n-dhcp4 API also requires us, to provide the exact lease, that
we were announced earlier.

As such, we need to make sure that we don't accept/decline in the wrong
state. That means, to keep track of what we are doing more carefully.

The functions _dhcp_client_accept()/_dhcp_client_decline() now take
a l3cd argument, the one that we announced earlier. And we check that it
still matches.

(cherry picked from commit 52a0fe584c)
(cherry picked from commit 1f7bede222)
2022-10-14 11:29:58 +02:00
Thomas Haller
c2e25b30d0 dhcp: move accept/decline function inside "nm-dhcp-client.c"
They are no longer used from outside, NMDhcpClient fully handles this.
Make them static and internal.

Also, decline is currently unused. It will be used soon, with ACD
support.

(cherry picked from commit 4a256092ee)
(cherry picked from commit 27eb23ea44)
2022-10-14 11:29:58 +02:00
Thomas Haller
c19a0fa1f6 dhcp/trivial: rename connect_l3cfg_notify() to l3_cfg_notify_check_connected()
The function subscribes a callback l3_cfg_notify_cb(). Rename so that
related functions have a clearly related name.

(cherry picked from commit 9abcf3a53c)
(cherry picked from commit 344c0b3dfc)
2022-10-14 11:29:58 +02:00
Thomas Haller
3453bf09e8 dhcp: replace switch in l3_cfg_notify_cb() with if blocks
The l3_cfg_notify_cb() handler is used for different purposes, and
different events will be considered.

Usually a switch statement is very nice for enums, especially if all
enum values should be handled (because the compiler can warn about
unhandled cases). In this case, not all events are supposed to be
handled. At this point, it seems nicer to just use an if block. It
better composes.

The compiler should be able to optimize both variants to the same
result. In any case, checking some integers for equality is in any case
going to be efficient.

(cherry picked from commit 7db07faa5e)
(cherry picked from commit 9acb6f9082)
2022-10-14 11:29:58 +02:00
Thomas Haller
1cd5276952 dhcp: move addr-family specific data to union in NMDhcpClientPrivate
(cherry picked from commit e756533002)
(cherry picked from commit 962bcadec9)
2022-10-14 11:29:58 +02:00
Thomas Haller
517f1d7867 dhcp/trivial: drop obsolete code comment
This is done already.

(cherry picked from commit 05cc160494)
(cherry picked from commit 68b3868427)
2022-10-14 11:29:58 +02:00
Thomas Haller
3d7ba2327c dhcp: fix logging of event in _nm_dhcp_client_notify()
(cherry picked from commit cd09f3d364)
(cherry picked from commit 11d2b5597b)
2022-10-14 11:29:58 +02:00
Thomas Haller
9d1f40afc3 dhcp: merge nm_dhcp_client_start_ip4() and nm_dhcp_client_start_ip6() implementations
As almost always, there is a point in keeping IPv4 and IPv6 implementations
similar. Behave different where there is an actual difference, at the bottom
of the stack.

(cherry picked from commit 7f943f5fa6)
(cherry picked from commit 0edfa4456a)
2022-10-14 11:29:58 +02:00
Thomas Haller
c6decf3b45 dhcp: move code in "nm-dhcp-client.c" (2)
(cherry picked from commit 2b8aeba06d)
(cherry picked from commit 17425aa0dd)
2022-10-14 11:29:58 +02:00
Thomas Haller
387688379a dhcp: minor cleanup in config_init()
(cherry picked from commit 600467b96f)
(cherry picked from commit 97241b277a)
2022-10-14 11:29:58 +02:00
Thomas Haller
ac95e0a693 dhcp: remove assertion in nm_dhcp_client_handle_event()
Technically, g_warn_if_reached() may not be an assertion, according to
glib. However, there is G_DEBUG=fatal-warnings and we want to run with
that.

So this is an assertion to us. Also, logging to stderr/stdout is not a
useful thing to the daemon. Don't do this. Especially, since it depends
on user provided (untrusted) input.

(cherry picked from commit 892cde1436)
(cherry picked from commit 38b8fdb75c)
2022-10-14 11:29:58 +02:00
Thomas Haller
5686f7aa50 dhcp: move code in nm_dhcp_client_handle_event()
(cherry picked from commit 9097679aad)
(cherry picked from commit d0fcd690dc)
2022-10-14 11:29:58 +02:00
Thomas Haller
0c1a2f9408 dhcp: drop NMDhcpState enum
It's unused now.

(cherry picked from commit 802f343d9f)
(cherry picked from commit 116e53352b)
2022-10-14 11:29:58 +02:00
Thomas Haller
ea9b5d3d1d dhcp: rename/refactor nm_dhcp_client_set_state() to be notifications
Optimally we want stateless, pure code. Obviously, NMDhcpClient needs to
keep state to know what it's doing. However, we should well encapsulate
the state inside NMDhcpClient, and only accept events/notifications that
mutate the internal state according to certain rules.

Having a function public set_state(self, new_state) means that other
components (subclasses of NMDhcpClient) can directly mangle the state.
That means, you no longer need to only reason about the internal state
of NMDhcpClient (and the events/notifications/state-changes that it
implements). You also need to reason that other components take part of
maintaining that internal state.

Rename nm_dhcp_client_set_state() to nm_dhcp_client_notify(). Also, add
a new enum NMDhcpClientEventType with notification/event types.

In practice, this is only renaming. But naming is important, because it
suggests the reader how to think about the code.

(cherry picked from commit 97e65e4b50)
(cherry picked from commit 2dba874c5a)
2022-10-14 11:29:58 +02:00
Thomas Haller
87ee78e3cc dhcp: fix handling of NM_DHCP_STATE_NOOP
The "noop" state is almost unused, however, nm_dhcp_set_state()
has a check "if (new_state >= NM_DHCP_STATE_TIMEOUT)", so the order
of the NOOP state matters.

Fix that by reordering.

Also, just return right away from NOOP.

(cherry picked from commit 9761e38f7e)
(cherry picked from commit 62ae5c0d0d)
2022-10-14 11:29:58 +02:00
Thomas Haller
d15e64360a dhcp: drop most of NMDhcpState usage from nm_dhcp_client_handle_event()
NMDhcpState is very tied to events from dhclient. But most of these
states we don't care about, and NMDhcpClient definitely should abstract
and hide them.

We should repurpose NMDhcpState to simpler state. For that, first drop
the state from nm_dhcp_client_handle_event().

This is only the first step (which arguably makes the code more
complicated, because reason_to_state() gets spread out and the logic
happens more than once). That will be addressed next.

(cherry picked from commit f102051a29)
(cherry picked from commit 2c7f74ad94)
2022-10-14 11:29:58 +02:00
Thomas Haller
ab2846d22d dhcp/trivial: add comment about nm_dhcp_utils_merge_new_dhcp6_lease()
(cherry picked from commit 70cbf3dc1e)
(cherry picked from commit e042011be4)
2022-10-14 11:29:58 +02:00
Thomas Haller
4bcc079195 dhcp: move code in "nm-dhcp-client.c"
So that it makes more sense, related parts are closer together.

(cherry picked from commit 8d121b17b5)
(cherry picked from commit f82c6d06e5)
2022-10-14 11:29:58 +02:00
Thomas Haller
d4b1dff1eb glib-aux: add nm_g_bytes_ref() helper
g_bytes_ref() does not accept NULL. But doing so can be convenient,
add a helper for that.

Note that g_bytes_unref() does accept NULL, so there is no corresponding
helper.

(cherry picked from commit 222f404928)
(cherry picked from commit 01bcde8ab0)
2022-10-14 11:29:58 +02:00
Thomas Haller
bbd902cb9a dhcp: minor code cleanups in "nm-dhcp-client.c"
(cherry picked from commit 1093e66776)
(cherry picked from commit fa4fcf6142)
2022-10-14 11:29:58 +02:00
Thomas Haller
c1fee605d8 dhcp: use GSource for watching child process instead of numeric source id
(cherry picked from commit c8542a5d50)
(cherry picked from commit 3f1b53c851)
2022-10-14 11:29:58 +02:00
Thomas Haller
6c2d34cd4f dhcp: cleanup reason_to_state() in "nm-dhcp-client.c"
- use NM_IN_STRSET_ASCII_CASE().
- don't use else block after we return.
- don't accept the "iface" argument just for logging. The caller
  can do the logging, if they wish.

(cherry picked from commit 9b9c07530c)
(cherry picked from commit ea7ad68ed2)
2022-10-14 11:29:58 +02:00
Thomas Haller
e64350bd2b dhcp: simplify nm_dhcp_client_set_effective_client_id()
The "take" parameter of _set_effective_client_id() was always "FALSE". Drop it.
Also, drop _set_effective_client_id() and just call nm_dhcp_client_set_effective_client_id()
directly.

(cherry picked from commit 6b191d6ea7)
2022-10-14 11:29:58 +02:00
Thomas Haller
871d17a6f3 dhcp: use nm_g_bytes_equal0() in _set_effective_client_id()
(cherry picked from commit 874403b588)
2022-10-14 11:29:58 +02:00
Beniamino Galvani
4c89c7df38 dns: merge branch 'bg/dns-sort'
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1427

(cherry picked from commit e03b8fa447)

(cherry picked from commit 925ea8b24c)

(cherry picked from commit 53eba18d60)
2022-10-14 10:53:34 +02:00
Beniamino Galvani
2ae89526ca dns: sort the ip-data list when a new element is added
In nm_dns_manager_set_ip_config() we try to avoid calling update_dns()
unless something changes, because updating DNS is expensive and can
trigger other actions such as a new hostname resolution.

When we add a new ip_data, even if the new element is equivalent to
the old one that was removed, we need to sort the list again.

Fixes: ce0a36d20f ('dns: better track l3cd changes')
https://bugzilla.redhat.com/show_bug.cgi?id=2098574
(cherry picked from commit 3cc7801779)
(cherry picked from commit db4c55c8d3)
(cherry picked from commit 6ac62a746f)
2022-10-14 10:53:34 +02:00
Beniamino Galvani
79c08e8d59 dns: add comment explaining the purpose of any_removed
(cherry picked from commit 4d1ecd8d6d)
(cherry picked from commit 01b4040a7a)
(cherry picked from commit ad8a6a59af)
2022-10-14 10:53:34 +02:00
Thomas Haller
cd855580f7
doc: fix bind-activation description for AddAndActivateConnection2 D-Bus API
Fixes: eb883e34a5 ('core: Add option to AddAndActivateConnection2 to bind the lifetime')
(cherry picked from commit 88f5e7518a)
(cherry picked from commit afe53b902f)
(cherry picked from commit 659ee74d8d)
2022-10-05 10:59:19 +02:00
Thomas Haller
f0b5345ab0
core: merge branch 'th/mlag-bonding-slb' (part 2)
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1385

(cherry picked from commit c5beec90a8)

(cherry picked from commit 278d32c7e7)

(cherry picked from commit fff62917b7)
2022-09-29 16:53:08 +02:00
Thomas Haller
ae34c8d32b
firewall: introduce helper function for add/flush/delete nft table command
(cherry picked from commit e796a67d6c)
(cherry picked from commit 56a503c879)
(cherry picked from commit 798d8ee832)
2022-09-29 16:53:06 +02:00
Thomas Haller
819a93591d
firewall: fail from nm_firewall_nft_call() on non-zero exit code
(cherry picked from commit 02feefb1df)
(cherry picked from commit d7bc25b3bd)
(cherry picked from commit 403c6de957)
2022-09-29 16:53:06 +02:00
Thomas Haller
c5089ebcc3
device: allow resetting the devip state via nm_device_devip_set_state()
There is no reason to disallow resetting the state.

(cherry picked from commit 607a9544cb)
(cherry picked from commit 6af0233a21)
(cherry picked from commit aebfb3461e)
2022-09-29 16:53:06 +02:00
Thomas Haller
6c7010a3a9
glib-aux: add NM_UTILS_ERROR_COMMAND_FAILED error code
(cherry picked from commit a5f125f8cb)
(cherry picked from commit f4126a12cd)
(cherry picked from commit 9b982c4ea3)
2022-09-29 16:53:05 +02:00
Thomas Haller
299fad171e
glib-aux: add nm_utils_get_process_exit_status_desc_buf() helper
(cherry picked from commit 95e6ebec66)
(cherry picked from commit 431c219ad1)
(cherry picked from commit f3f99e03f5)
2022-09-29 16:53:05 +02:00
Thomas Haller
ce39ec3c3b
firewall: merge branch 'th/firewall-cleanup'
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1379

(cherry picked from commit a939324a78)

(cherry picked from commit 234ec6f263)

(cherry picked from commit e3330ba1d4)
2022-09-29 16:49:18 +02:00
Thomas Haller
b0a567e350
firewall: move logging stdin argument to nft call
(cherry picked from commit b74e2cbfaa)
(cherry picked from commit 8139b27584)
(cherry picked from commit ff04405391)
2022-09-29 16:49:16 +02:00
Thomas Haller
d7f504518f
firewall: expose nm_firewall_nft_call() in header file
(cherry picked from commit cfeecbedff)
(cherry picked from commit 07c519c37f)
(cherry picked from commit 4ac934cbe4)
2022-09-29 16:49:16 +02:00
Thomas Haller
9fcef5ae34
firewall-utils: move _append() macro to be used by other places
(cherry picked from commit 0a0c197916)
(cherry picked from commit 49ae45f838)
(cherry picked from commit 8ec2391f9a)
2022-09-29 16:49:15 +02:00
Thomas Haller
abf19e8f8c
firewall/trivial: rename nm_firewall_config_apply() to nm_firewall_config_apply_sync()
Sync/blocking methods are ugly. Their name should highlight this.
Also, we may have an async variant, so we will need the "good" name
for apply() and apply_finish().

(cherry picked from commit dc66fb7d04)
(cherry picked from commit 558bcd5aae)
(cherry picked from commit 5235dce259)
2022-09-29 16:49:15 +02:00
Thomas Haller
730385e68c
firewall: more renaming and splitting _fw_nft_set_shared()
Blocking calls are ugly. Rename those to have a "_sync()" suffix.
Also, split from _fw_nft_set_shared() the part that constructs the
stdin for nft.

(cherry picked from commit 7362ad6266)
(cherry picked from commit bbf3d01e82)
(cherry picked from commit 61ed013e7b)
2022-09-29 16:49:15 +02:00
Thomas Haller
07f29a6b34
firewall/trivial: rename nm_firewall_config_new() to nm_firewall_config_new_shared()
(cherry picked from commit 7ad3fb1956)
(cherry picked from commit bfb4452f7d)
(cherry picked from commit a5fdaec774)
2022-09-29 16:49:14 +02:00
Thomas Haller
a99591fdad
firewall/trivial: rename "shared"/"add" argument in firewall utils to "up"
(cherry picked from commit e185f7966d)
(cherry picked from commit 6fa0068c1e)
(cherry picked from commit 4dfe52762c)
2022-09-29 16:49:14 +02:00
Thomas Haller
a865a8689f
core: transfer ownership of strbuf data in _fw_nft_set()
In practice there is little difference.

Previously, "strbuf" would own the string until the end of the function,
when the "nm_auto_str_buf" cleanup attribute destroys it. In the
meantime, we would pass it on to _fw_nft_call_sync(), which in fact
won't access the string after returning.

Instead, we can just transfer ownership to the GBytes instance. That seems
more logical and safer than aliasing the buffer owned by NMStrBuf with
a g_bytes_new_static(). That way, we don't add a non-obvious restriction
on the lifetime of the string. The lifetime is now guarded by the GBytes
instance, which, could be referenced and kept alive longer.

There is also no runtime/memory overhead in doing this.

(cherry picked from commit 6a04bcc59d)
(cherry picked from commit c598f0ff0f)
2022-09-29 16:49:10 +02:00
Thomas Haller
ec58546e8b
glib-aux: fix nm_str_buf_finalize() for cloning buffer
NMStrBuf can also contains NUL characters. We thus cannot use g_strndup(),
which uses strncpy() and truncates at the first NUL.

Fixes: 13d25f9d0b ('glib-aux: add support for starting with stack-allocated buffer in NMStrBuf')
(cherry picked from commit 520411623d)
(cherry picked from commit 7a3de841b8)
(cherry picked from commit 51b9f0ad4c)
2022-09-29 16:40:57 +02:00
Thomas Haller
4c93a3167a
glib-aux: fix spurious semicolon after NM_STR_BUF_INIT() macros
It's wrong, and it breaks certain uses.

Fixes: 13d25f9d0b ('glib-aux: add support for starting with stack-allocated buffer in NMStrBuf')
(cherry picked from commit c5ec4ebd77)
(cherry picked from commit 7b487e6951)
(cherry picked from commit fed6e78d05)
2022-09-29 16:40:55 +02:00
Thomas Haller
bfcc6a7537
glib-aux: add nm_str_buf_append_printfv()
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1321
(cherry picked from commit d5b31a05e6)
(cherry picked from commit 6c2c3fdfcc)
2022-09-29 16:40:52 +02:00
Thomas Haller
dfbdf3d477
glib-aux: merge branch 'th/str-buf-stack-allocated'
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1203

(cherry picked from commit c6e41b2df3)

(cherry picked from commit c744607c60)
2022-09-29 16:40:31 +02:00
Thomas Haller
84a0ba7469
glib-aux: avoid #if in "nm-str-buf.h"
NM_MORE_ASSERT is a compile time constant. The compiler can optimize
it away just fine.

(cherry picked from commit 560feecb4c)
(cherry picked from commit de6da97e9d)
2022-09-29 16:40:29 +02:00
Thomas Haller
e7acc59e84
glib-aux: drop nm_str_buf_init() for NM_STR_BUF_INIT()
NM_STR_BUF_INIT() and nm_str_buf_init() were pretty much redundant. Drop one of
them.

Usually our pattern is that we don't have functions that return structs.
But NM_STR_BUF_INIT() returns a struct, because it's convenient to use
with

  nm_auto_str_buf NMStrBuf strbuf = NM_STR_BUF_INIT(...);

So use that variant instead.

(cherry picked from commit 532f3e34a8)
(cherry picked from commit 90255a8aa8)
2022-09-29 16:40:28 +02:00
Thomas Haller
c331e4a9b5
glib-aux: add support for starting with stack-allocated buffer in NMStrBuf
Allow to initialize NMStrBuf with an externally allocated array.
Usually a stack buffer. If the NMStrBuf grows beyond the size of
that initial buffer, then it would switch using malloc.

The idea is to support the common case where the result is small enough
to fit on the stack.

I always wanted to do such optimization because the main purpose of
NMStrBuf is to put it on the stack and ad-hoc construct a string.
I just figured, it would complicate the implementation and add
a runtime overhead. But turns out, it doesn't really.
The biggest question is how NMStrBuf should behave with a pre-allocated
buffer? Turns out, most choices can be made in a rather obvious way.
The only non-obvious thing is that nm_str_buf_finalize() would malloc()
a buffer, but that too seems consistent and what a user would probably
expect. As such, this doesn't seem to add unexpected semantics to the API.

(cherry picked from commit 13d25f9d0b)
(cherry picked from commit 51393413b4)
2022-09-29 16:40:28 +02:00