Commit graph

27947 commits

Author SHA1 Message Date
Lubomir Rintel
4a082baf82 cloud-setup: actually pass the HTTP method in nm_http_client_poll_req()
https://bugzilla.redhat.com/show_bug.cgi?id=2179718

Fixes: 8b7e12c2d6 ('cloud-setup/ec2: start with requesting a IMDSv2 token')
Fixes: cd74d75002 ('cloud-setup: make nm_http_client_req() accept a method argument')
(cherry picked from commit f07da04cd9)
(cherry picked from commit d787c0c59d)
(cherry picked from commit 6abbdaaa64)
(cherry picked from commit 16dc184845)
(cherry picked from commit 35e76b509b)
(cherry picked from commit b369cdc5f2)
(cherry picked from commit 6e1566ffaf)
2023-09-18 09:22:45 +02:00
Lubomir Rintel
d75e307ebc cloud-setup/ec2: start with requesting a IMDSv2 token
The present version of the EC2 metadata API (IMDSv2) requires a header
with a token to be present in all requests. The token is essentially a
cookie that's not actually a cookie that's obtained with a PUT call that
doesn't put anything. Apparently it's too easy to trick someone into
calling a GET method.

EC2 now supports IMDSv2 everywhere with IMDSv1 being optional, so let's
just use IMDSv2 unconditionally. Also, the presence of a token API can
be used to detect the AWS EC2 cloud.

Conflicts: variable alignments only

https://bugzilla.redhat.com/show_bug.cgi?id=2151986
(cherry picked from commit 8b7e12c2d6)
(cherry picked from commit 429f36cd81)
(cherry picked from commit e3ac982b32)
(cherry picked from commit c5a3e739b1)
(cherry picked from commit ee157ad48b)
(cherry picked from commit ae3ec36462)
(cherry picked from commit 865fe0732e)
2023-09-18 09:22:45 +02:00
Lubomir Rintel
59b5a8fdcb cloud-setup: make nm_http_client_req() accept a method argument
We'll need to be able to issue PUT calls.

Conflicts: variable alignments only, missing nmcs-provider-aliyun

(cherry picked from commit cd74d75002)
(cherry picked from commit eff4372045)
(cherry picked from commit aaf66e9174)
(cherry picked from commit 3d94f4fdf9)
(cherry picked from commit 181466c6da)
(cherry picked from commit 7243307bb8)
(cherry picked from commit 1aa88024cb)
2023-09-18 09:22:24 +02:00
Lubomir Rintel
89ee76409b cloud-setup: rename get/Get identifiers to req and Req
We're going to extend those to issue methods other than GET.
Also, "request" would've been too long, "req" looks nicer.

Conflicts: variable alignments, missing trivial commit in provider-azure

(cherry picked from commit 85ce088616)
(cherry picked from commit 6e8cfbae32)
(cherry picked from commit 20cd11ee49)
(cherry picked from commit 9ce530fa7a)
(cherry picked from commit d6d161a31d)
(cherry picked from commit 977fc2c8c5)
2023-09-18 09:20:46 +02:00
Thomas Haller
23b03def98 cloud-setup: use nm_strv_dup_packed() in nm_http_client_poll_get()
No need to do a deep clone. The strv array is not ever modified and we
pack it together in one memory allocation.

Conflicts: nm_strv_dup_packed is still called nm_utils_strv_dup_packed

(cherry picked from commit 599fe234ea)
(cherry picked from commit 3787eacac9)
(cherry picked from commit 89a6ce575d)
(cherry picked from commit d14dc95be3)
(cherry picked from commit 7e516418e0)
(cherry picked from commit becb47826a)
(cherry picked from commit 4704e14100)
2023-09-18 08:49:20 +02:00
Lubomir Rintel
cab0b16d3c cloud_setup: unexport nm_http_client_get()
It's not used anywhere.

Conflicts: variable alignments only

(cherry picked from commit ce225b2c06)
(cherry picked from commit 23b9514080)
(cherry picked from commit 36d417af60)
(cherry picked from commit d83537bff5)
(cherry picked from commit f584b9c97b)
(cherry picked from commit f59f629431)
(cherry picked from commit 1885ff2c65)
2023-09-18 08:49:20 +02:00
Thomas Haller
bf2a3ff277 cloud-setup: track config-task-data in iface-data
Let NMCSProviderGetConfigIfaceData.get_config_data have a pointer to the
NMCSProviderGetConfigTaskData. This will allow two things:

- at several places we pass on `nm_utils_user_data_pack(get_config_data,
  config_iface_data)` as user data. We can avoid that, by just letting
  config_iface_data have a pointer to get_config_data.

- NMCSProviderGetConfigIfaceData contains a provider specific field
  "priv". That may also require special initialization or destruction,
  depending on the type. We thus need access to the provider type,
  which we have via iface_data->get_config_data->self.

Also let NMCSProviderGetConfigTaskData have a pointer "self" to the
NMCSProvider. While there was already the "task", which contains the
provider as source-object, this is more convenient.

Conflicts: missing nmcs-provider-aliyun

(cherry picked from commit 069946cda1)
(cherry picked from commit 061c05ca39)
(cherry picked from commit 1cf4fd0235)
(cherry picked from commit be1dce951e)
(cherry picked from commit 646bc7a10e)
2023-09-18 08:48:59 +02:00
Gris Ge
c5530797c2 nm-device: Format the code by nm-code-format.sh
Signed-off-by: Gris Ge <fge@redhat.com>
2023-06-28 16:00:56 +08:00
Gris Ge
9b4cffc559 setting-connection: Unblock autoconnect upon finish of Reapply
The activation of a connection will clear the block of autoconnect,
we should do the same for reapply.

Signed-off-by: Gris Ge <fge@redhat.com>
(cherry picked from commit 0486efd358)
(cherry picked from commit 18ce5f43bd)
(cherry picked from commit 2695396939)
(cherry picked from commit 32d2e3c14b)
(cherry picked from commit 387ae9d7ff)
(cherry picked from commit 6f2c7733ce)
(cherry picked from commit 34f7499f3c)
2023-06-27 20:04:10 +08:00
Beniamino Galvani
2c0529de8f
platform: fix routing rule test failure
Since kernel 5.18 there is a stricter validation [1][2] on the tos
field of routing rules, that must not include ECN bits.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=f55fbb6afb8d701e3185e31e73f5ea9503a66744
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=a410a0cf98854a698a519bfbeb604145da384c0e

Fixes the following failure:

  >>> src/core/platform/tests/test-route-linux
  >>> ...
  # NetworkManager-MESSAGE: <warn>  [1656321515.6604] platform-linux: do-add-rule: failure 22 (Invalid argument - Invalid dsfield (tos): ECN bits must be 0)
  >>> failing... errno=-22, rule=[routing-rule,0x13d6e80,1,+alive,+visible; [6] 0: from all tos 0xff fwmark 0x4/0 suppress_prefixlen -459579276 action-214 protocol 255]
  >>> existing rule: * [routing-rule,0x13d71e0,2,+alive,+visible; [6] 0: from all sport 65534 lookup 10009 suppress_prefixlen 0 none]
  >>> existing rule:   [routing-rule,0x13d7280,2,+alive,+visible; [4] 0: from all fwmark 0/0x9a7e9992 ipproto 255 suppress_prefixlen 0 realms 0x00000008 none protocol 71]
  >>> existing rule:   [routing-rule,0x13d7320,2,+alive,+visible; [6] 598928157: from all suppress_prefixlen 0 none]
  >>> existing rule:   [routing-rule,0x13d73c0,2,+alive,+visible; [4] 0: from 192.192.5.200/8 lookup 254 suppress_prefixlen 0 none protocol 9]
  >>> existing rule:   [routing-rule,0x13d7460,2,+alive,+visible; [4] 0: from all ipproto 3 suppress_prefixlen 0 realms 0xffffffff none protocol 5]
  >>> existing rule:   [routing-rule,0x13d7500,2,+alive,+visible; [4] 0: from all fwmark 0x1/0 lookup 254 suppress_prefixlen 0 action-124 protocol 4]
  >>> existing rule:   [routing-rule,0x13d75a0,2,+alive,+visible; [4] 0: from all suppress_prefixlen 0 action-109]
  0:      from all fwmark 0/0x9a7e9992 ipproto ipproto-255 realms 8 none proto 71
  0:      from 192.192.5.200/8 lookup main suppress_prefixlength 0 none proto ra
  0:      from all ipproto ggp realms 65535/65535 none proto 5
  0:      from all fwmark 0x1/0 lookup main suppress_prefixlength 0 124 proto static
  0:      from all 109
  0:      from all sport 65534 lookup 10009 suppress_prefixlength 0 none
  598928157:      from all none
  Bail out! nm:ERROR:../src/core/platform/tests/test-route.c:1787:test_rule: assertion failed (r == 0): (-22 == 0)

Fixes: 5ae2431b0f ('platform/tests: add tests for handling policy routing rules')
(cherry picked from commit bf9a2babb4)
(cherry picked from commit 09b0014a01)
(cherry picked from commit e1266b3b12)
(cherry picked from commit 8da69be278)
(cherry picked from commit d276884206)
2022-12-20 13:03:19 +01:00
Beniamino Galvani
b4746bf7a2 core: wait for carrier before resolving hostname via DNS
If there is no carrier on a device, don't try to resolve the hostname
on it. Instead, subscribe to carrier change notifications and retry
again once carrier goes up.

https://bugzilla.redhat.com/show_bug.cgi?id=2118817
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1402
(cherry picked from commit e3cf5083fb)
(cherry picked from commit 1673e3f051)
(cherry picked from commit 69e66102ce)
(cherry picked from commit a8f8ca01fd)
(cherry picked from commit bd951c5398)
(cherry picked from commit badd1aa0b0)
2022-12-16 17:51:26 +01:00
Beniamino Galvani
4849c1c91f dhcp: merge branch 'bg/restart-dhcp-on-mac-change'
https://bugzilla.redhat.com/show_bug.cgi?id=2110000

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1343

(cherry picked from commit 7f40eb1b04)

(cherry picked from commit 14633422e2)

(cherry picked from commit c8341aa3f2)

(cherry picked from commit 8b9a12e109)

(cherry picked from commit 693d835c05)

(cherry picked from commit dc39b9a364)
2022-08-31 10:28:53 +02:00
Beniamino Galvani
18c635779d device: restart DHCP when the MAC changes
If the MAC changes there is the possibility that the DHCP client will
not be able to renew the address because it uses the old MAC as
CHADDR. Depending on the implementation, the DHCP server might use
CHADDR (so, the old address) as the destination MAC for DHCP replies,
and those packets will be lost.

To avoid this problem, restart the DHCP client when the MAC changes.

https://bugzilla.redhat.com/show_bug.cgi?id=2110000
(cherry picked from commit 905adabdba)
(cherry picked from commit 5a49a2f6b2)
(cherry picked from commit d0fb3fbf8e)
(cherry picked from commit 59a52510f3)
(cherry picked from commit 0766d08db9)
(cherry picked from commit 25abc22ac9)
2022-08-31 10:28:53 +02:00
Beniamino Galvani
943e84a2c1 core: log when dynamic IP configuration is restarted and why
(cherry picked from commit 6cd69fde33)
(cherry picked from commit 2f8e4e2b06)
(cherry picked from commit 8011d0b32b)
(cherry picked from commit 3d02df8061)
(cherry picked from commit 2c358b89a2)
(cherry picked from commit eb0803de60)
2022-08-31 10:28:53 +02:00
Beniamino Galvani
ae85fe85f3 n-dhcp4/probe: forget lease after a NAK
If we have a lease and we get a NAK renewing/rebinding it, the lease
is lost.

Without this, probe->current_lease remains set and after the next
DISCOVER/OFFER round, any call to n_dhcp4_client_lease_select() will
fail at:

        if (lease->probe->current_lease)
                return -ENOTRECOVERABLE;

As in:

 [5325.1313] dhcp4 (veth0): send REQUEST of 172.25.1.200 to 255.255.255.255
 [5325.1434] dhcp4 (veth0): received NACK from 172.25.1.1
 [5325.1435] dhcp4 (veth0): client event 3 (RETRACTED)
 [5325.1436] dhcp4 (veth0): send DISCOVER to 255.255.255.255
 [5325.1641] dhcp4 (veth0): received OFFER of 172.25.1.200 from 172.25.1.1
 [5325.1641] dhcp4 (veth0): client event (OFFER)
 [5325.1641] dhcp4 (veth0): selecting lease failed: -131 (ENOTRECOVERABLE)

Upstream: https://github.com/nettools/n-dhcp4/pull/33
Upstream: e4af93228e

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/issues/993

Fixes: e43b1791a3 ('Merge commit 'e23b3c9c3ac86b065eef002fa5c4321cc4a87df2' as 'shared/n-dhcp4'')

(cherry picked from commit e141cd45d6)
(cherry picked from commit e056a68d21)
(cherry picked from commit 6636c792bd)
(cherry picked from commit 631dff08a0)
(cherry picked from commit 966d45bc7d)
2022-07-25 16:10:23 +02:00
Thomas Haller
dacb5be7ba platform: support IPv6 mulitpath routes and fix cache inconsistency
Add support for IPv6 multipath routes, by treating them as single-hop
routes. Otherwise, we can easily end up with an inconsistent platform
cache.

Background:
-----------

Routes are hard. We have NMPlatform which is a cache of netlink objects.
That means, we have a hash table and we cache objects based on some
identity (nmp_object_id_equal()). So those objects must have some immutable,
indistinguishable properties that determine whether an object is the
same or a different one.

For routes and routing rules, this identifying property is basically a subset
of the attributes (but not all!). That makes it very hard, because tomorrow
kernel could add an attribute that becomes part of the identity, and NetworkManager
wouldn't recognize it, resulting in cache inconsistency by wrongly
thinking two different routes are one and the same. Anyway.

The other point is that we rely on netlink events to maintain the cache.
So when we receive a RTM_NEWROUTE we add the object to the cache, and
delete it upon RTM_DELROUTE. When you do `ip route replace`, kernel
might replace a (different!) route, but only send one RTM_NEWROUTE message.
We handle that by somehow finding the route that was replaced/deleted. It's
ugly. Did I say, that routes are hard?

Also, for IPv4 routes, multipath attributes are just a part of the
routes identity. That is, you add two different routes that only differ
by their multipath list, and then kernel does as you would expect.
NetworkManager does not support IPv4 multihop routes and just ignores
them.
Also, a multipath route can have next hops on different interfaces,
which goes against our current assumption, that an NMPlatformIP4Route
has an interface (or no interface, in case of blackhole routes). That
makes it hard to meaningfully support IPv4 routes. But we probably don't
have to, because we can just pretend that such routes don't exist and
our cache stays consistent (at least, until somebody calls `ip route
replace` *sigh*).

Not so for IPv6. When you add (`ip route append`) an IPv6 route that is
identical to an existing route -- except their multipath attribute -- then it
behaves as if the existing route was modified and the result is the
merged route with more next-hops. Note that in this case kernel will
only send a RTM_NEWROUTE message with the full multipath list. If we
would treat the multipath list as part of the route's identity, this
would be as if kernel deleted one routes and created a different one (the
merged one), but only sending one notification. That's a bit similar to
what happens during `ip route replace`, but it would be nightmare to
find out which route was thereby replaced.
Likewise, when you delete a route, then kernel will "subtract" the
next-hop and sent a RTM_DELROUTE notification only about the next-hop that
was deleted. To handle that, you would have to find the full multihop
route, and replace it with the remainder after the subtraction.

NetworkManager so far ignored IPv6 routes with more than one next-hop, this
means you can start with one single-hop route (that NetworkManger sees
and has in the platform cache). Then you create a similar route (only
differing by the next-hop). Kernel will merge the routes, but not notify
NetworkManager that the single-hop route is not longer a single-hop
route. This can easily cause a cache inconsistency and subtle bugs. For
IPv6 we MUST handle multihop routes.

Kernels behavior makes little sense, if you expect that routes have an
immutable identity and want to get notifications about addition/removal.
We can however make sense by it by pretending that all IPv6 routes are
single-hop! With only the twist that a single RTM_NEWROUTE notification
might notify about multiple routes at the same time. This is what the
patch does.

The Patch
---------

Now one RTM_NEWROUTE message can contain multiple IPv6 routes
(NMPObject). That would mean that nmp_object_new_from_nl() needs to
return a list of objects. But it's not implemented that way. Instead,
we still call nmp_object_new_from_nl(), and the parsing code can
indicate that there is something more, indicating the caller to call
nmp_object_new_from_nl() again in a loop to fetch more objects.

In practice, I think all RTM_DELROUTE messages for IPv6 routes are
single-hop. Still, we implement it to handle also multi-hop messages the
same way.

Note that we just parse the netlink message again from scratch. The alternative
would be to parse the first object once, and then clone the object and
only update the next-hop. That would be more efficient, but probably
harder to understand/implement.

https://bugzilla.redhat.com/show_bug.cgi?id=1837254#c20
(cherry picked from commit dac12a8d61)
(cherry picked from commit 698cf1092c)
(cherry picked from commit 87fe255c89)
(cherry picked from commit c8a1d9ca73)
2022-06-09 10:08:09 +02:00
Thomas Haller
11756a6192 platform: rename variable "IS_IPv4" in platform code
The variable with this purpose is usually called "IS_IPv4".

It's upper case, because usually this is a const variable, and because
it reminds of the NM_IS_IPv4(addr_family) macro. That letter case
is unusual, but it makes sense to me for the special purpose that this
variable has.

Anyway. The naming of this variable is a different point. Let's
use the variable name that is consistent and widely used.

(cherry picked from commit 8085c0121f)
(cherry picked from commit eec32669a9)
(cherry picked from commit 99cd6ed25e)
(cherry picked from commit d64930f7eb)
2022-06-09 10:08:07 +02:00
Thomas Haller
bd2fe272bb platform: fix parsing RTA_MULTIHOP netlink attribute to use no policy
To parse the RTA_MULTIHOP message, "policy" is not right (which is used
to parse the overall message). Instead, we don't really have a special
policy that we should use.

This was not a severe issue, because the allocated buffer (with
G_N_ELEMENTS(policy) elements) was larger than need be. And apparently,
using the wrong policy also didn't cause us to reject important
messages.

(cherry picked from commit 997d72932d)
(cherry picked from commit 21b1978072)
(cherry picked from commit ef1587bd88)
(cherry picked from commit 6aa3d40199)
2022-06-09 10:08:06 +02:00
Thomas Haller
57342756ed
device: initialize nm_auto variable in _ethtool_features_reset()
The compiler is often adament to warn about maybe-uninitialized.

(cherry picked from commit 3dd854eb1b)
(cherry picked from commit 471e987add)
(cherry picked from commit 4fa6001c60)
2022-04-05 12:23:55 +02:00
Thomas Haller
606703209f
libnm/tests: fix maybe-uninitialized warning in "test-libnmc-setting"
(cherry picked from commit 87bce61bad)
(cherry picked from commit aadf0fb64f)
(cherry picked from commit 688c67a24f)
2022-04-05 12:23:55 +02:00
Thomas Haller
40f51f0ffa
libnm/tests: fix maybe-uninitialized warning in "test-setting"
In function '_nm_auto_g_free',
      inlined from 'test_tc_config_tfilter_matchall_mirred' at src/libnm-core-impl/tests/test-setting.c:2955:24:
  ./src/libnm-glib-aux/nm-macros-internal.h:58:1: error: 'str' may be used uninitialized [-Werror=maybe-uninitialized]
     58 | NM_AUTO_DEFINE_FCN_VOID0(void *, _nm_auto_g_free, g_free);
        | ^
  src/libnm-core-impl/tests/test-setting.c: In function 'test_tc_config_tfilter_matchall_mirred':
  src/libnm-core-impl/tests/test-setting.c:2955:24: note: 'str' was declared here
   2955 |     gs_free char      *str;
        |                        ^
  lto1: all warnings being treated as errors
  lto-wrapper: fatal error: gcc returned 1 exit status

(cherry picked from commit 6f0e22a64a)
(cherry picked from commit 6329f1db5a)
(cherry picked from commit d3fdb854f9)
2022-04-05 12:23:55 +02:00
Thomas Haller
c65a57108e
Revert "supplicant/config: fix setting "saw" for "wifi-sec.key-mgnt=sae""
This "fix" was wrong, because at the beginning of the if-else-block
there is already `key_mgmt_conf = g_string_new(key_mgmt);`.

This reverts commit a6e06171ad.

a6e06171ad ('supplicant/config: fix setting "saw" for "wifi-sec.key-mgnt=sae"')
2022-04-05 09:02:51 +02:00
Thomas Haller
09669f0045
examples/python: avoid Python2 "print" statement
Recent python-black (22.0) dropped support for Python 2 and thus fail
for those files. Make the examples Python3 compatible.

(cherry picked from commit 95e6a0a6e2)
(cherry picked from commit 2e4d1e8dc6)
(cherry picked from commit b78ca328d2)
(cherry picked from commit 25062ff17b)
2022-04-04 21:53:30 +02:00
Thomas Haller
a6e06171ad
supplicant/config: fix setting "saw" for "wifi-sec.key-mgnt=sae"
This is a partial backport of commit 5f146b40f3 ('supplicant/config:
Refactor key_mgmt config generation').

Based-on-patch-by: Jonas Dreßler <verdre@v0yd.nl>

Fixes: d17a0a0905 ('supplicant: allow fast transition for WPA-PSK and WPA-EAP')

(cherry picked from commit 5f146b40f3)
2022-04-01 19:50:10 +02:00
Beniamino Galvani
1350134947 n-dhcp4: discard NAKs from other servers in SELECTING
I got a report of a scenario where multiple servers reply to a REQUEST
in SELECTING, and all servers send NAKs except the one which sent the
offer, which replies with a ACK. In that scenario, n-dhcp4 is not able
to obtain a lease because it restarts from INIT as soon as the first
NAK is received. For comparison, dhclient can get a lease because it
ignores all NAKs in SELECTING.

Arguably, the network is misconfigured there, but it would be great if
n-dhcp4 could still work in such scenario.

According to RFC 2131, ACK and NAK messages from server must contain a
server-id option. The RFC doesn't explicitly say that the client
should check the option, but I think it's a reasonable thing to do, at
least for NAKs.

This patch stores the server-id of the REQUEST in SELECTING, and
compares it with the server-id from NAKs, to discard other servers'
replies.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1144
(cherry picked from commit 118561e284)
(cherry picked from commit 3abfdbab33)
(cherry picked from commit c499412ec4)
(cherry picked from commit bfca239a27)
2022-03-24 15:55:51 +01:00
Beniamino Galvani
5f9921182e initrd: handle ip=dhcp,dhcp6 specially
With "ip=dhcp,dhcp6" the legacy dracut module does first DHCPv4 and
then IPv6 autoconf (even if DHCPv4 succeeded) [1]. In this way, there
is the guarantee that an address family is always configured if the
network supports it.

Currently "ip=dhcp,dhcp6" is treated a bit differently by NM, which
generates a connection with only ipv4.required-timeout=20s. Therefore
it's possible that NM in initrd quits (or signals startup-complete)
without an IPv6 even if the network is configured for IPv6.

Make NM's behavior similar to the legacy module by also setting an
ipv6.required-timeout for "ip=dhcp,dhcp6".

Note that if the command line contains "rd.neednet=1" without an "ip="
argument, we still generate a default connection with IPv4 preferred
over IPv6 (i.e. only ipv4.required-timeout set). That's similar to
what the legacy module does [2]. See [3] for a description of
different scenarios for "rd.neednet=1".

[1] https://github.com/dracutdevs/dracut/blob/055/modules.d/35network-legacy/ifup.sh#L459-L484
[2] https://github.com/dracutdevs/dracut/blob/055/modules.d/35network-legacy/ifup.sh#L529-L537
[3] https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/issues/729

https://bugzilla.redhat.com/show_bug.cgi?id=1961666
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/994
(cherry picked from commit 28770eb394)
(cherry picked from commit 02adbf5124)
2021-12-01 14:59:52 +01:00
Vojtech Bubela
af3dadf95c libnm: fix crash in _nm_ip_route_validate_all for invalid route
backtrace from coredump, NetworkManager-1.30.6-1.fc34

  #0  verify
      (setting=0x55d081fe8690, connection=<optimized out>, error=0x7ffe0fa06870)
      at libnm-core/nm-setting-ip-config.c:5249
  #1  0x000055d081ab98d4 in verify
      (setting=0x55d081fe8690, connection=0x55d0820a2b80, error=0x7ffe0fa06870)
      at libnm-core/nm-setting-ip4-config.c:119
  #2  0x000055d081aa3d54 in _nm_connection_verify
      (connection=0x55d0820a2b80, error=0x7ffe0fa068c0)
      at libnm-core/nm-connection.c:1441
  #3  0x000055d081aa78ec in nm_connection_normalize
      (connection=0x55d0820a2b80, parameters=0x0, modified=0x0, error=0x7ffe0fa06de8)
      at libnm-core/nm-connection.c:1688
  #4  0x000055d081aa81f4 in _nm_connection_replace_settings
      (connection=0x55d0820a2b80, new_settings=<optimized out>, parse_flags=_NM_SETTING_PARSE_FLAGS_LAST, error=0x7ffe0fa06de8) at libnm-core/nm-connection.c:432
  #5  0x000055d081aa83a6 in _nm_simple_connection_new_from_dbus
      (dict=0x55d082089950, error=0x7ffe0fa06de8, parse_flags=_NM_SETTING_PARSE_FLAGS_LAST) at libnm-core/nm-simple-connection.c:77
  #6  0x000055d081bbf942 in settings_connection_update
      (self=0x55d081fdd9f0, is_update2=1, context=0x7fc06c021dd0, new_settings=0x55d082089950, flags=NM_SETTINGS_UPDATE2_FLAG_TO_DISK)
      at src/core/settings/nm-settings-connection.c:1637
  #7  0x000055d081bbfb09 in impl_settings_connection_update2
      (obj=0x55d081fdd9f0, interface_info=<optimized out>, method_info=<optimized out>, connection=<optimized out>, sender=<optimized out>, invocation=0x7fc06c021dd0, parameters=0x55d0820f5e60) at src/core/settings/nm-settings-connection.c:1796
  #8  0x00007fc08a9db482 in call_in_idle_cb.lto_priv () at /lib64/libgio-2.0.so.0

Fixes: bb6c2d7371 ('libnm: ensure stable behavior in _nm_ip_route_attribute_validate_all()')
(cherry picked from commit 0ed099374d)
(cherry picked from commit 00723dd5e8)
2021-12-01 14:57:20 +01:00
Thomas Haller
e8d39ebf0f cloud-setup: merge branch 'th/cloud-setup-azure-fix-gateway'
https://bugzilla.redhat.com/show_bug.cgi?id=1912236

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/821

(cherry picked from commit 840d46b34c)
2021-10-14 14:34:57 -03:00
Thomas Haller
5d112092bc cloud-setup/azure: fix detecting the gateway address
The code never set "iface_get_config->cidr_addr", despite
setting "cidr_prefix" and "has_cidr". As a result, cloud-setup
would think that the subnet is "0.0.0.0/$PLEN", and calculate
the gateway as "0.0.0.1".

As a result it would add a default route to table 30400 via 0.0.0.1,
which is obviously wrong.

How to detect the right gateway? Let's try obtain the subnet also via
the meta data. That seems mostly correct, except that we only access
subnet at index 0. What if there are multiple ones? I don't know.

https://bugzilla.redhat.com/show_bug.cgi?id=1912236
(cherry picked from commit c2629f72b0)
2021-10-14 13:02:00 -03:00
Thomas Haller
783d470b6f cloud-setup/azure: refactor callback for _get_config_ips_prefix_list_cb()
(cherry picked from commit 889498c12c)
2021-10-14 12:59:19 -03:00
Thomas Haller
57c6a4fddc cloud-setup/azure: cleanup constructing URI in _get_config_ips_prefix_list_cb()
(cherry picked from commit c9fc3f5b03)
2021-10-14 07:24:30 -03:00
Thomas Haller
3256239b1f cloud-setup: remove redundant check in Azure's _get_net_ifaces_list_cb()
This condition always true, because there is a check above.

(cherry picked from commit d3f07d5ca2)
2021-10-14 07:23:49 -03:00
Fernando Fernández Mancera
9366a4404f cloud-setup: merge branch 'backport_1_30_mr_974'
NetworkManager/NetworkManager!987
2021-10-05 08:47:23 +00:00
Fernando Fernandez Mancera
9cb01cdefd cloud-setup: follow the clang-format
Signed-off-by: Fernando Fernandez Mancera <ffmancera@riseup.net>
2021-10-05 09:35:48 +02:00
Thomas Haller
6fef8c7235 cloud-setup: use suppress_prefixlength rule to honor non-default-routes in the main table
Background
==========

Imagine you run a container on your machine. Then the routing table
might look like:

    default via 10.0.10.1 dev eth0 proto dhcp metric 100
    10.0.10.0/28 dev eth0 proto kernel scope link src 10.0.10.5 metric 100
    [...]
    10.42.0.0/24 via 10.42.0.0 dev flannel.1 onlink
    10.42.1.2 dev cali02ad7e68ce1 scope link
    10.42.1.3 dev cali8fcecf5aaff scope link
    10.42.2.0/24 via 10.42.2.0 dev flannel.1 onlink
    10.42.3.0/24 via 10.42.3.0 dev flannel.1 onlink

That is, there are another interfaces with subnets and specific routes.

If nm-cloud-setup now configures rules:

    0:  from all lookup local
    30400:  from 10.0.10.5 lookup 30400
    32766:  from all lookup main
    32767:  from all lookup default

and

    default via 10.0.10.1 dev eth0 table 30400 proto static metric 10
    10.0.10.1 dev eth0 table 30400 proto static scope link metric 10

then these other subnets will also be reached via the default route.

This container example is just one case where this is a problem. In
general, if you have specific routes on another interface, then the
default route in the 30400+ table will interfere badly.

The idea of nm-cloud-setup is to automatically configure the network for
secondary IP addresses. When the user has special requirements, then
they should disable nm-cloud-setup and configure whatever they want.
But the container use case is popular and important. It is not something
where the user actively configures the network. This case needs to work better,
out of the box. In general, nm-cloud-setup should work better with the
existing network configuration.

Change
======

Add new routing tables 30200+ with the individual subnets of the
interface:

    10.0.10.0/24 dev eth0 table 30200 proto static metric 10
    [...]
    default via 10.0.10.1 dev eth0 table 30400 proto static metric 10
    10.0.10.1 dev eth0 table 30400 proto static scope link metric 10

Also add more important routing rules with priority 30200+, which select
these tables based on the source address:

    30200:  from 10.0.10.5 lookup 30200

These will do source based routing for the subnets on these
interfaces.

Then, add a rule with priority 30350

    30350:  lookup main suppress_prefixlength 0

which processes the routes from the main table, but ignores the default
routes. 30350 was chosen, because it's in between the rules 30200+ and
30400+, leaving a range for the user to configure their own rules.

Then, as before, the rules 30400+ again look at the corresponding 30400+
table, to find a default route.

Finally, process the main table again, this time honoring the default
route. That is for packets that have a different source address.

This change means that the source based routing is used for the
subnets that are configured on the interface and for the default route.
Whereas, if there are any more specific routes in the main table, they will
be preferred over the default route.

Apparently Amazon Linux solves this differently, by not configuring a
routing table for addresses on interface "eth0". That might be an
alternative, but it's not clear to me what is special about eth0 to
warrant this treatment. It also would imply that we somehow recognize
this primary interface. In practise that would be doable by selecting
the interface with "iface_idx" zero.

Instead choose this approach. This is remotely similar to what WireGuard does
for configuring the default route ([1]), however WireGuard uses fwmark to match
the packets instead of the source address.

[1] https://www.wireguard.com/netns/#improved-rule-based-routing

(cherry picked from commit fe80b2d1ec)
(cherry picked from commit 58e58361bd)
2021-10-05 09:35:48 +02:00
Thomas Haller
b33eac5281 cloud-setup: cleanup configuring addresses/routes/rules in _nmc_mangle_connection()
(cherry picked from commit 0978be5e43)
(cherry picked from commit ce24b4bca5)
2021-10-05 09:35:48 +02:00
Wen Liang
a9e6aa663e aliyun: reuse ipv4 gateway address returned by metadata server
The default ipv4 gateway address of the VPC in Aliyun cloud is not the
first IP address in the CIDR subnet block, we should instead use the
ipv4 gateway address retrieved from the metadata server in
`_nmc_mangle_connection()`.

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

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/958

Signed-off-by: Wen Liang <liangwen12year@gmail.com>
(cherry picked from commit 778e1f8493)
(cherry picked from commit 59633dbe11)
2021-10-05 09:35:48 +02:00
Thomas Haller
661da869b3 cloud-setup: limit number of supported interfaces to avoid overlapping table numbers
The table number is chosen as 30400 + iface_idx. That is, the range is
limited and we shouldn't handle more than 100 devices. Add a check for
that and error out.

(cherry picked from commit b68d694b78)
(cherry picked from commit 292233e16e)
2021-10-05 09:35:48 +02:00
Thomas Haller
67a83b54cd cloud-setup: process iface-datas in sorted order
The routes/rules that are configured are independent of the
order in which we process the devices. That is, because they
use the "iface_idx" for cases where there is ambiguity.

Still, it feels nicer to always process them in a defined order.

(cherry picked from commit a95ea0eb29)
(cherry picked from commit 6302cd416d)
2021-10-05 09:35:48 +02:00
Thomas Haller
48e79fb4b2 cloud-setup: track sorted list of NMCSProviderGetConfigIfaceData
Sorted by iface_idx. The iface_idx is probably something useful and
stable, provided by the provider. E.g. it's the order in which
interfaces are exposed on the meta data.

(cherry picked from commit 1c5cb9d3c2)
(cherry picked from commit 0a2ed62703)
2021-10-05 09:35:48 +02:00
Thomas Haller
c36e42dbd9 cloud-setup: add "hwaddr" to NMCSProviderGetConfigIfaceData struct
get-config() gives a NMCSProviderGetConfigResult structure, and the
main part of data is the GHashTable of MAC addresses and
NMCSProviderGetConfigIfaceData instances.

Let NMCSProviderGetConfigIfaceData also have a reference to the MAC
address. This way, I'll be able to create a (sorted) list of interface
datas, that also contain the MAC address.

(cherry picked from commit ec56fe60fb)
(cherry picked from commit cc289e5369)
2021-10-05 09:35:48 +02:00
Thomas Haller
8bad924931 cloud-setup/trivial: rename variables in Azure's _get_config_fetch_done_cb()
The previous name seem not very expressive/fitting. Naming is hard, but
I think these are better names.

(cherry picked from commit 89f3267859)
2021-10-05 09:35:48 +02:00
Thomas Haller
91ddd5f5ba cloud-setup: use _nm_utils_ascii_str_to_int64_bin() in Azure's _get_config_fetch_done_cb()
(cherry picked from commit a2fded3cee)
2021-10-05 09:35:48 +02:00
Thomas Haller
2e4b73c7fe cloud-setup: skip configuring policy routing if there is only one interface/address
nm-cloud-setup automatically configures the network. That may conflict
with what the user wants. In case the user configures some specific
setup, they are encouraged to disable nm-cloud-setup (and its
automatism).

Still, what we do by default matters, and should play as well with
user's expectations. Configuring policy routing and a higher priority
table (30400+) that hijacks the traffic can cause problems.

If the system only has one IPv4 address and one interface, then there
is no point in configuring policy routing at all. Detect that, and skip
the change in that case.

Note that of course we need to handle the case where previously multiple
IP addresses were configured and an update gives only one address. In
that case we need to clear the previously configured rules/routes. The
patch achieves this.

(cherry picked from commit 5f047968d7)
(cherry picked from commit 8bc8a0f56b)
2021-10-05 09:35:48 +02:00
Thomas Haller
2b96e9314c cloud-setup: preserve IPv4 addresses/routes/rules from profile
nm-cloud-setup automatically detects routes, addresses and rules and configures them
on the device using the emphermal Reapply() API. That is, it does not modify the
existing profile (on disk), but changes the runtime configuration only.

As such, it used to wipe otherwise statically configured IP addresses, routes and
rules. That seems unnecessary. Let's keep the configuration from the (persistent)
configuration.

There is of course the problem that nm-cloud-setup doesn't really
understand the existing IP configuration, and it can only hope that
it can be meaningfully combined with what nm-cloud-setup wants to
configure. This should cover most simple cases, for more complex setups,
the user probably should disable nm-cloud-setup and configure the
network explicitly to their liking.

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

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/893
(cherry picked from commit 4201ee5119)
(cherry picked from commit 9541b0bea4)
2021-10-05 09:35:48 +02:00
Thomas Haller
ef7a97977a cloud-setup: count numbers of valid IPv4 addresses in get-config result
Will be used next.

(cherry picked from commit 7969ae1a82)
(cherry picked from commit ae504433f1)
2021-10-05 09:35:48 +02:00
Thomas Haller
7fcc89db6e cloud-setup: cache number of valid interfaces in get-config result
Now that we return a struct from get_config(), we can have system-wide
properties returned.

Let it count and cache the number of valid iface_datas.

Currently that is not yet used, but it will be.

(cherry picked from commit a3cd66d3fa)
(cherry picked from commit e74375fc3b)
2021-10-05 09:35:48 +02:00
Thomas Haller
b2ed9e7d5d cloud-setup: return structure for get_config() result instead of generic hash table
Returning a struct seems easier to understand, because then the result
is typed.

Also, we might return additional results, which are system wide and not
per-interface.

(cherry picked from commit 323e182768)
(cherry picked from commit c94b1c43d4)
2021-10-05 09:35:48 +02:00
Thomas Haller
2585e34e59 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)
(cherry picked from commit eea912dfb3)
2021-10-05 09:35:48 +02:00
Thomas Haller
b8bb585052 glib-aux: add _nm_utils_ascii_str_to_int64_bin() helper
(cherry picked from commit 70b7ad1a76)
2021-10-05 09:35:48 +02:00