Commit graph

475 commits

Author SHA1 Message Date
Wen Liang
2b70e02ef5
platform: rename nm_platform_link_get_permanent_address()
Rename `nm_platform_link_get_permanent_address()`, `link_get_permanent_address()` to
`nm_platform_link_get_permanent_address_ethtool()`, `link_get_permanent_address_ethtool()`.

Signed-off-by: Wen Liang <liangwen12year@gmail.com>
2021-08-24 21:04:21 +02:00
Wen Liang
1605fa460d
platform: update nm_platform_link_get_permanent_address() to accept NMPLinkAddress argument
Replace the arguments "buf+length" of
`nm_platform_link_get_permanent_address()` with "NMPLinkAddress *out_addr"

Signed-off-by: Wen Liang <liangwen12year@gmail.com>
2021-08-24 21:04:21 +02:00
Thomas Haller
db13b93563
ifcfg-rh: fail nms_ifcfg_rh_writer_write_connection() without filename/dir
No actual caller should use the API without providing either a filename
or the directory name. I don't think this can actually happen, hence
fail and assert in that case.
2021-08-24 13:45:10 +02:00
Thomas Haller
1fa105eaef
ifcfg-rh: fix updating ifcfg file if file on disk is no longer present
Have an ifcfg file loaded in NetworkManager, then move/remove the file and try
to modify it. That will fail with:

  "failed to update connection: Could not read file '/etc/sysconfig/network-scripts/ifcfg-eth0': No such file or directory"

That is not right.

If the user didn't move/remove the file but merely modified it, NetworkManager
would silently overwrite it. There is no reason why move/remove should behave
differently and not just write a completely fresh file.

The reason why NetworkManager first loads the file before writing, is to
preserve comments and unrecognized shell variables. This is a certain effort
to play nice with users editing the file. It's not essential to load the file
first and a failure to do so should not result in a failure.

And of course, keyfile writer doesn't behave like this either.

This bug exists since 2009, but let's not add a "Fixes" comment for
commit 1974b257e0 ('ifcfg-rh: begin adding write support'), because
it seems not right to backport this patch to all the old releases.
2021-08-24 13:45:06 +02:00
Gris Ge
e69c5e4bab
libnm: Use _nm_connection_ensure_setting()
Use `_nm_connection_ensure_setting()` to eliminate the
duplicated codes. This function will retrieve the specific setting from
connection, if not found, create new one and attach to the connection.

Signed-off-by: Gris Ge <fge@redhat.com>
2021-08-20 19:02:23 +02:00
Thomas Haller
ea49b50651
all: add some README.md files describing the purpose of our sources 2021-08-19 17:51:11 +02:00
Thomas Haller
c380893dc6
platform: fix capturing addresses from platform for assuming after restart
Commit c631aa48f0 ('platform: capture NMIP[46]Config from platform
with correct (reversed) order of IP addresses') changed this for IPv6
and IPv4, but it's not correct for IPv4.

For IPv6, later `ip addr add` calls adds a new primary address, which
is also listed in `ip addr show` first. Hence, as NMIP6Config tracks
addresses in increasing priority, while NMPlatform tracks them as
exposed by kernel, the order when appending addresses form platform
to NMIP6Config must be reversed.

That is not the case for IPv4. For IPv4, later `ip addr add` calls
add a secondary IP address. Also, in `ip addr show` output they are
appended. Consequently, IPv4 addresses are tracked by NMPlatform with
decreasing priority (in the reverse order than for IPv6).

Fix constructing the NMIP4Config by fixing the address order. This is
important, because during restart devices get assumed and our code would
configure the order of addresses as it finds them.

Fixes: c631aa48f0 ('platform: capture NMIP[46]Config from platform with correct (reversed) order of IP addresses')
2021-08-19 13:48:22 +02:00
Thomas Haller
8733d22343
ifcfg: read/write full IPv6 settings also for method ignore/disabled
Do "bother" to read/write settings.

For the umpteenth time, it's not up to the reader/writer to decide
what properties are valid for a profile or which makes sense.

Only nm_connection_verify() can decide that. For example nm_connection_verify()
has no problem with ipv6.method=disabled while also setting ipv6.addr-gen-mode.
We cannot just shortcut the parsing/writing.

The reader only ignores addresses, dns and dns-searches, so that we don't start
parsing invalid files, where the setting would have been ignore
previously.

In particular,

   echo "DEVICE=eth0" > /etc/sysconfig/network-scripts/ifcfg-xxx
   nmcli connection load /etc/sysconfig/network-scripts/ifcfg-xxx
   nmcli -f ipv6.method,ipv6.addr-gen-mode connection show /etc/sysconfig/network-scripts/ifcfg-xxx

needs to show eui64 addr-gen-mode.
2021-08-19 08:54:54 +02:00
Thomas Haller
7de4322d51
ifcfg: don't let write_ip[46]_setting() fail 2021-08-19 08:54:54 +02:00
Thomas Haller
00f63074d6
ifcfg: don't limit parsing DNS elements to 10 entries
It's not the task of the ifcfg reader to pre-normalize profiles
to truncate the DNS server list. It's only nm_connection_verify()'s
task to indicate what is valid and what not.

Increase the number to something excessive. Note that the parsing
scales with O(n^2). So don't have it totally unbounded and have an
overall limit (of 10000 entries).
2021-08-19 08:54:40 +02:00
Thomas Haller
1abf512831
ifcfg: fix crash due to not setting error on failure to parse DNS
Fixes: c2ad294290 ('ifcfg-rh: fix error handing in some functions that expect error != NULL')
2021-08-17 20:07:34 +02:00
Thomas Haller
02832b03ee
ifcfg/tests: fix evaluating environment variable to regenerate test files
Fixes: 1ae6719cf1 ('ifcfg-rh/tests: evalute environment for $NMTST_IFCFG_RH_UPDATE_EXPECTED only once')
2021-08-17 20:05:25 +02:00
Thomas Haller
c631aa48f0
platform: capture NMIP[46]Config from platform with correct (reversed) order of IP addresses
Fix the order of IP addresses when assuming devices (service restart).

The order of IP addresses matters to kernel for selection of source IP
address.

If all other properties are equal ([1]), for IPv6, the address added *last*
will be preferred. That is the address you see *first*` in `ip -6 addr show`.
NMPlatform also preserves that order, so the address *first* is the most
important one.

On the other hand, in a connection profile, `ipv6.addresses` lists
addresses in increasing priority (the last address is the primary one).
That is for compatibility with initscripts, which iterates over the
list of addresses and calls `ip addr add` (meaning, the last address
will be added last and is thus preferred by kernel).

As the priority order in the profile is reversed, also the priority
order in NMIP[46]Config is reversed. Fix creating an NMIP[46]Config
instance from platform addresses to honor the priority.

This has real consequences. When restarting NetworkManager, the interface
stays up with the addresses configured in the right order. After
restart, the device gets assumed, which means that the NMIP[46]Config
instance from the connection is not yet set, only the config from the
platform gets synchronized. Previously the order was wrong, so during
restart the order of IP addresses was reverted.

[1] https://access.redhat.com/solutions/189153

https://bugzilla.redhat.com/show_bug.cgi?id=1988751
2021-08-17 19:56:39 +02:00
echengqi
97041efee1
core: free config_cli before exit() from main
[thaller@redhat.com: changed commit message]

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/954
2021-08-11 16:23:09 +02:00
Thomas Haller
f5dbf476e3
core: rename to_str() methods to to_string()
It's more common to name the to-string method *_to_string(). Rename.
2021-08-11 14:17:25 +02:00
Thomas Haller
ae117c588d
dhcp: ensure NMDhcpClient was stopped in destructor
The user of NMDhcpClient is supposed to call "nm_dhcp_client_stop()"
on the instance, also because it is a ref-counted GObject type. So we
wouldn't want to rely on the last unref to stop DHCP.

Still, in case that was not done, the code somehow made the assumption
it made any sense to possibly not stop the DHCP client. For the internal
client, there is of course nothing left after destroying the
NMDhcpClient instance, but what about dhclient? I don't think we should
ever leave dhclient running unsupervised. Even during restart of the
service, we need to stop it first, and restart it afterwards.

When we quit NetworkManager, we may want to leave the interface up and
configured. For that, we may need to take care that "nm_dhcp_client_stop()"
does not destory the IP configuration of the intrerface. But I don't
think that is a problem. What we still want to do, is kill the dhclient
instance.

NMDhcpClient is not supposed to be ever restarted. It starts when an
instance gets created, and it stops with "nm_dhcp_client_stop()". Hence,
the simple "is_stopped" flag is fine to prevent that multiple stop calls
are harmful.
2021-08-11 14:17:25 +02:00
Thomas Haller
359d207d95
dhcp: stop tracking NMDhcpClient instances from NMDhcpManager
NMDhcpManager was tracking DHCP clients. During start, it would check
whether an instance for the same ifindex is running, and stop it.

That seems unnecessary and wrong. Clearly, we cannot have multiple users
(like two `NMDevice`s) run DHCP on the same interface. But its up to
them to coordinate that. They also cannot configure IP addresses at the
same interface, and if they do, then there is a big problem already.

This comes from commit 1806235049 ('dhcp: convert dhcp backends to
classes'). Maybe back then there was also the idea that NetworkManager
could quit and leave dhclient running. That idea is also flawed. When
NetworkManager stops, it leaves the interface (possibly) up, so that
restart works without disruption. That does not mean that the DHCP
client needs to keep running. What works is to restart NetworkManager in
a timely manner, then NetworkManager will start a new DHCP client after
restart. What does not work is stop NetworkManager, do nothing (like
taking over the interface by running your own manager) and expect that
DHCP keeps working indefinitely. And of course, with the internal client
this cannot possibly work either. Don't stop NetworkManager for good, if
you expect NetworkManager to run DHCP on an interface.

A different things is that when NetworkManager crashes, that after
restart it kills the left over dhclient instances. That may require a
solution, for example systemd killing all processes or checking for
left-over PID files and kill the processes. But what was implemented in
NMDhcpManager was not a solution for that.

As such, it's not clear what conflicting instance we want to kill, or
why NMDhcpManager should even track NMDhcpClient instances.
2021-08-11 14:17:25 +02:00
Thomas Haller
dbdd8303fc
dhcp: replace NMDhcpClient's signals with "notify" and one notify data argument
NMDhcpClient communicates events via GObject signals. GObject signals in
principle could have several subscribers. In practice, a NMDhcpClient
instance has only one subscriber, because it was constructed with
certain parameters, so it's unlikely to be shared.

That one subscriber, always needs to subscribe to all signals
("state-changed" and "prefix-delegated"), Unless the subscriber only
creates a IPv4 client. In which case they won't subscribe to
"prefix-delegated", but that signal is also not invoked for IPv4
clients.

Combine the signals in one, and pass all parameters via a new
NMDhcpClientNotfiyData payload. I feel this is nicer, to pack all
parameters together. I find this more type-aware, where we can
switch (in the callback) based on a notify-type enum, instead
of subscribing multiple signal handlers.

With l3cfg work, DHCP handling will be refactored, where this model of
having one "generic" notify signal makes more sense than here. For the
moment, it is arguably pretty much the same. Also, because NMDhcpClient
subscribes two different handlers for IPv4 and IPv6. In the future,
there will be only one notify handler, and that can cover IPv4 and IPv6
and both "state-changed" and "prefix-delegated" (and other notification
types).
2021-08-11 14:17:24 +02:00
Thomas Haller
4d0e295317
ndisc: add nm_ndisc_dhcp_level_to_string() helper 2021-08-11 14:17:24 +02:00
Thomas Haller
30e7400528
ifup: extend ifup/ifdown to be smarter about NetworkManager profiles
Now that NetworkManager on Fedora 33 and RHEL 9 no longer writes
ifcfg-rh files by default ([1]), ifup/ifdown became less useful.

Possibly users shouldn't use it and it would be fine that new-style profiles
(keyfile) no longer work with these commands. But this is deemed as too
disruptive for users.

Note that our previous ifup/ifdown compat scripts only honored the argument
to be part of the ifcfg filename. That was not what initscripts were doing,
which called `need_config()` function that searched also the contents of
the files. With this extension, ifup/ifdown gets smarter too, to better
guess what the user might have wanted.

Extend the script by making it smarter, and to work with connection profile
names.

With this extension we further solidify ifup/ifdown as part of NetworkManager
command line API. That is problematic, because these tools pollute the
$PATH, by not having a clear NM-specific name. Also, these scripts
should only exist on Fedora/RHEL, which makes their usage non-portable
to other distros.
Also, other distros already ship different tools with name ifup/ifdown.
Extending the use of these scripts is thus undesirable, as it furthers
distro-specific commands.

Still, these arguments seem to not hold and users need to be "helped".
As Fedora users cannot be expected to unlearn "ifup" today, there is no
reason to assume they could in a few years. This likely means we will
never get rid of these scripts.

Also, if we truly would make ifup/ifdown part of NetworkManager, then a better
implementation would be that nmcli honors being called with these names.
That is not done, because nmcli's implementation currently is not as
nice to make that extension trivial (as it should be). It also would
mean to embrace ifup/ifdown officially. A shell script works well enough
as a hack.

[1] https://fedoraproject.org/wiki/Changes/NetworkManager_keyfile_instead_of_ifcfg_rh

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

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/936
2021-08-07 15:31:04 +02:00
Beniamino Galvani
3f42e2005a device: store the original MTU before force-setting it
In case the MTU is force-set (e.g. for bridges), priv->mtu_initial and
priv->ip6_mtu_initial must be initialized before changing the MTU,
otherwise the wrong value will be restored on deactivation.

Fixes: e23798a5e5 ('bridge: force (hack)-set of the MTU when explicitly set in the profile')

https://bugzilla.redhat.com/show_bug.cgi?id=1973536
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/955
2021-08-06 15:31:02 +02:00
Thomas Haller
0f100abd85
firewalld: listen to Reloaded signal and reconfigure firewall zones
During reload, firewalld drops the current runtime configuration.
NetworkManager should listen to that, and reconfigure the zones
that it cares about.
2021-08-06 14:35:35 +02:00
Thomas Haller
b2ed02dda9
firewalld: fix initialized_now argument for NMFirewalldManager's "state-changed" signal 2021-08-06 14:35:34 +02:00
Thomas Haller
3d949f98e4
firewalld: make D-Bus calls against unique name for firewalld service
As we keep track of the current name owner, use its unique name
for the D-Bus requests.

We also track when the name owner changes, so at the point when we make
the D-Bus call, the current name owner was still running. We should talk
to it directly. If at the same time, firewalld restarts, we go through
our usual tracking of the name owner and will retry -- but always
talking to the unique name.
2021-08-06 14:35:34 +02:00
Thomas Haller
9debc3d028
firewalld: track current name_owner in NMFirewalldManager
Not only track whether we have a name-owner, but also which.
2021-08-06 14:35:33 +02:00
Thomas Haller
b55f95abfa
firewalld: prefix firewalld logging messages with "firewalld"
It seems more apt than "firewall: ...".
2021-08-06 14:35:33 +02:00
Thomas Haller
8c7ab70915
dhcp: don't log plain pointer values for debugging
We avoid logging plain pointers. The logfile should not contain pointers
as that theoretically can defeat ASLR.
2021-08-05 15:52:01 +02:00
Thomas Haller
2cbaaed820
dhcp: add nm_dhcp_client_can_accept() function 2021-08-05 15:52:00 +02:00
Thomas Haller
320a1b5a79
l3cfg: add nm_l3cfg_remove_config_all_dirty() for removing dirty configs
The "only_dirty" parameter to a remove-all() function is odd.

For one, the function is called remove-all, but depending on a parameter
it does not remove all.

Also, setting remove-all(only_dirty=TRUE) means it will remove not
everything, so passing TRUE will remove only parts. That logic seems
confusing.

Avoid that, by removing the parameter from nm_l3cfg_remove_config_all()
and add nm_l3cfg_remove_config_all_dirty().
2021-08-05 14:59:19 +02:00
Thomas Haller
a3b7030d74
dispatcher: rename NM_DISPATCHER_ACTION_DHCP_CHANGE_X enums
add a NM_DISPATCHER_ACTION_DHCP_CHANGE_X() macro that can select the
right action based on a parameter.

Also rename the IPv4/IPv6 enum values, so that their naming scheme works
better with the NM_DISPATCHER_ACTION_DHCP_CHANGE_X() macro.
2021-08-05 14:59:17 +02:00
Thomas Haller
2979297519
dhcp: drop NM_DHCP_STATE_MAX enum value
These meta flags were not actually used. But when having a switch
statement, the compiler (rightly) asks us to handle them. Drop them.
2021-08-05 14:59:15 +02:00
Thomas Haller
b4e4b8d614
core: cleanup arguments for GObject signal of NMDhcpClient 2021-08-05 14:59:13 +02:00
Thomas Haller
3f6365f5d0
all: use G_CALLBACK() macro instead of plain cast 2021-08-05 14:59:11 +02:00
Thomas Haller
5e6b50ec73
device: track pending actions with a sorted string list instead of GSList
We call add/remove pending actions for every state change.

I think GSList is never the best choice of a data structure. Use a plain
array instead. Keep it sorted, so we can use binary search.
2021-08-05 14:59:10 +02:00
Thomas Haller
a29d8b712f
l3cfg: set NMIPConfigSource for NML3ConfigData at construct time
Each NML3ConfigData should have a source set, and in fact most callers
would call nm_l3_config_data_set_source() right after creating the
instance.

Move the source parameter to the new() constructor function. Also remove
the setter, making the source of an instance immutable.

As every l3cfg instance generally has a clear purpose, the source should
always be known from the start and doesn't need to change.
2021-08-03 20:36:08 +02:00
Thomas Haller
593cb57eb6
all: rename nm_utils_strdict_*() to nm_strdict_*() 2021-08-02 09:26:48 +02:00
Thomas Haller
3587cbd827
all: rename nm_utils_strsplit_set*() to nm_strsplit_set*() 2021-08-02 09:26:47 +02:00
Thomas Haller
4ac66a4215
all: rename nm_utils_strdup_reset*() to nm_strdup_reset*() 2021-08-02 09:26:47 +02:00
Thomas Haller
d0ba87a1ad
all: rename nm_utils_strbuf_*() API to nm_strbuf_*()
The "utils" part does not seem useful in the name.

Note that we also have NMStrBuf, which is named nm_str_buf_*().
There is an unfortunate similarity between the two, but it's still
distinct enough (in particular, because one takes an NMStrBuf and
the other not).
2021-08-02 09:26:42 +02:00
Thomas Haller
4c3aac899e
all: unify and rename strv helper API
Naming is important, because the name of a thing should give you a good
idea what it does. Also, to find a thing, it needs a good name in the
first place. But naming is also hard.

Historically, some strv helper API was named as nm_utils_strv_*(),
and some API had a leading underscore (as it is internal API).

This was all inconsistent. Do some renaming and try to unify things.

We get rid of the leading underscore if this is just a regular
(internal) helper. But not for example from _nm_strv_find_first(),
because that is the implementation of nm_strv_find_first().

  - _nm_utils_strv_cleanup()                 -> nm_strv_cleanup()
  - _nm_utils_strv_cleanup_const()           -> nm_strv_cleanup_const()
  - _nm_utils_strv_cmp_n()                   -> _nm_strv_cmp_n()
  - _nm_utils_strv_dup()                     -> _nm_strv_dup()
  - _nm_utils_strv_dup_packed()              -> _nm_strv_dup_packed()
  - _nm_utils_strv_find_first()              -> _nm_strv_find_first()
  - _nm_utils_strv_sort()                    -> _nm_strv_sort()
  - _nm_utils_strv_to_ptrarray()             -> nm_strv_to_ptrarray()
  - _nm_utils_strv_to_slist()                -> nm_strv_to_gslist()
  - nm_utils_strv_cmp_n()                    -> nm_strv_cmp_n()
  - nm_utils_strv_dup()                      -> nm_strv_dup()
  - nm_utils_strv_dup_packed()               -> nm_strv_dup_packed()
  - nm_utils_strv_dup_shallow_maybe_a()      -> nm_strv_dup_shallow_maybe_a()
  - nm_utils_strv_equal()                    -> nm_strv_equal()
  - nm_utils_strv_find_binary_search()       -> nm_strv_find_binary_search()
  - nm_utils_strv_find_first()               -> nm_strv_find_first()
  - nm_utils_strv_make_deep_copied()         -> nm_strv_make_deep_copied()
  - nm_utils_strv_make_deep_copied_n()       -> nm_strv_make_deep_copied_n()
  - nm_utils_strv_make_deep_copied_nonnull() -> nm_strv_make_deep_copied_nonnull()
  - nm_utils_strv_sort()                     -> nm_strv_sort()

Note that no names are swapped and none of the new names existed
previously. That means, all the new names are really new, which
simplifies to find errors due to this larger refactoring. E.g. if
you backport a patch from after this change to an old branch, you'll
get a compiler error and notice that something is missing.
2021-07-29 10:26:50 +02:00
Thomas Haller
3775f4395a
all: drop unnecessary casts from nm_utils_strv_find_first()
And, where the argument is a GPtrArray, use
nm_strv_ptrarray_find_first() instead.
2021-07-29 09:33:50 +02:00
Beniamino Galvani
bace14fe1f core: introduce device 'allowed-connections' property
Configuration can have [device*] and [connection*] settings and both
can include a 'match-device=' key, which is a list of device-specs.

Introduce a new 'allowed-connections' key for [device*] sections,
which specifies a list of connection-specs to indicate which
connections can be activated on the device.

With this, it becomes possible to have a device configuration like:

  [device-enp1s0]
  match-device=interface-name:enp1s0
  allowed-connections=except:origin:nm-initrd-generator

so that NM in the real root ignores connections created by the
nm-initrd-generator, and starts activating a persistent
connection. This requires also setting 'keep-configuration=no' to not
generate an assumed connection.
2021-07-27 17:43:45 +02:00
Beniamino Galvani
604c611cd0 core: add nm_utils_connection_match_spec_list()
Add function nm_utils_connection_match_spec_list() to check whether a
connection matches a spec list. Also document the supported syntax in
the man page.
2021-07-27 17:43:45 +02:00
Beniamino Galvani
df2fe15714 core: add 'keep-configuration' device configuration option
Add a new 'keep-configuration' device option, set to 'yes' by
default. When set to 'no', on startup NetworkManager ignores that the
interface is pre-configured and doesn't try to keep its
configuration. Instead, it activates one of the persistent
connections.
2021-07-27 16:36:48 +02:00
Beniamino Galvani
b1644fa826 manager: exit early in get_existing_connection()
Later the function will become more complex. Add a check to exit early
if the device can't assume connections.
2021-07-27 16:36:47 +02:00
Beniamino Galvani
bb37e30867 core: add comments about assuming connections 2021-07-27 16:36:47 +02:00
Beniamino Galvani
9a09c02012 core: persist the bootfile from DHCP
The bootfile location is needed by the anaconda dracut module; write
it to the device state file.
2021-07-27 09:36:33 +02:00
Beniamino Galvani
3c79944e15 dhcp: nettools: parse the filename and the bootfile-name option 2021-07-27 09:36:33 +02:00
Beniamino Galvani
0994a444e5 dhcp: add internal option for the boot file name
Add an internal option that specifies the boot file name from the DHCP
header. The option name 'filename' is the same as exposed by dhclient.
2021-07-27 09:36:32 +02:00
Beniamino Galvani
0c10e4f8b6 dhcp: escape control characters in DHCP options
Control characters (DEL (=127) or those below 32) could cause undesired
effects when a client displays or parses DHCP options. Escape them.
2021-07-27 09:35:58 +02:00