Commit graph

95 commits

Author SHA1 Message Date
Beniamino Galvani
440a0b4078 initrd: set ipv6.method=auto when the autoconfiguration field is 'none'
The 7th field of:

 ip=<client-IP>:[<peer>]:<gateway-IP>:<netmask>:<client_hostname>:<interface>:{none|off|dhcp|on|any|dhcp6|auto6|ibft}:[:[<mtu>][:<macaddr>]]

specifies which kind of autoconfiguration to do. 'none' and 'off' mean
static addresses.

The old network module of dracut used to leave kernel IPv6
autoconfiguration enabled when IPv4 static addresses were
configured. With NM, this corresponds to enabling IPv6 auto method.

https://bugzilla.redhat.com/show_bug.cgi?id=1848943
(cherry picked from commit a39eb9ac14)
2020-06-24 14:54:50 +02:00
Beniamino Galvani
0e15a5e1be initrd: generate connections with IPv6 method 'auto' instead of 'ignore'
When the initrd generator creates a connection with IPv6 method
'ignore', the kernel will do IPv6 autoconfiguration on the
interface. However, it is preferable to let NetworkManager configure
the interface directly instead of relying on kernel. Therefore, change
the IPv6 method to 'auto'. Note that we still set ipv6.may-fail to
'yes' so that a failure during IPv6 autoconfiguration doesn't bring
down the interface.

(cherry picked from commit f6d654b18f)
2020-06-24 14:54:49 +02:00
Thomas Haller
e2f83d4e92
keyfile: add NMKeyfileHandlerFlags
nm_keyfile_read() and nm_keyfile_write() will be public API.
As such, it must be flexible and extendible for future needs.
There is already the handler callback that fully solves this
(e.g. a future handler event could request whether a certain
behavior is enabled or not).

As additional possibility for future extension, add a flags
argument. Currently no flags are implemented.
2020-06-05 09:17:21 +02:00
Thomas Haller
968b444603
initrd: drop unused include of platform header 2020-06-03 18:08:37 +02:00
Thomas Haller
1c2d9581e9
initrd: use nm_streq()/NM_IN_STRSET() instead of strcmp()
Code like

»···»···if (strcmp (tag, "net.ifnames") == 0)
»···»···»···net_ifnames = strcmp (argument, "0") != 0;

is really hard to understand (at least to me). Compare to

»···»···if (nm_streq (tag, "net.ifnames"))
»···»···»···net_ifnames = !nm_streq (argument, "0");
2020-06-03 18:00:05 +02:00
Beniamino Galvani
3957d40f54 initrd: don't generate new connections for rd.znet
The rd.znet specifies the s390 parameters of an existing
connection. If no matching connection exists, we should not create a
new one.

https://bugzilla.redhat.com/show_bug.cgi?id=1840287
2020-05-28 14:34:44 +02:00
Beniamino Galvani
fbf54ab182 initrd: parse 'rd.net.timeout.dhcp' option 2020-05-06 10:32:25 +02:00
Beniamino Galvani
93d7a9b005 initrd: parse rd.peerdns early
Instead of generating connections and then apply the rd.peerdns to
them, parse the option early and set it when each connection is
created.
2020-05-06 10:32:25 +02:00
Beniamino Galvani
ff70adf873 initrd: save hostname to a file in /run
Save the hostname read from command line to a file in /run so that it
can be applied later by the NM dracut module.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/issues/419
2020-05-06 10:32:25 +02:00
Beniamino Galvani
f14cc584d5 initrd: add command line option to specify initrd data directory 2020-05-06 10:32:25 +02:00
Thomas Haller
d506823d4f initrd: fix setting VLan ID in reader_parse_vlan()
g_ascii_strtoull() returns a guint64, which is very wrong to directly pass
to the variadic argument list of g_object_set(). We expect a guint there
and need to cast.

While at it, use _nm_utils_ascii_str_to_int64() to parse and validate the input.
2020-04-01 17:12:18 +02:00
Thomas Haller
46dd4d0fbf meson: merge branch 'inigomartinez/meson-license'
Add SPDX license headers for meson files.

As far as I can tell, according to RELICENSE.md file, almost everybody
who contributed to the meson files agreed to the LGPL-2.1+ licensing.
This entails the vast majority of code in question.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/397
2020-03-28 12:45:19 +01:00
Beniamino Galvani
c84a4579b2 initrd: fix build error with assertions disabled
$ meson -Dmore_asserts=0 meson-build
$ ninja -C meson-build
[712/859] Compiling C object 'src/initrd/b383957@@nmi-core@sta/nmi-cmdline-reader.c.o'.
../src/initrd/nmi-cmdline-reader.c: In function ‘nmi_cmdline_reader_parse’:
../src/initrd/nmi-cmdline-reader.c:871:4: warning: ‘s_ip’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  871 |    nm_setting_ip_config_add_dns (s_ip, ns);
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../src/initrd/nmi-cmdline-reader.c:835:21: note: ‘s_ip’ was declared here
  835 |  NMSettingIPConfig *s_ip;
      |                     ^~~~

Fixes: 25a2b6e14f ('initrd: rework command line parsing')
2020-03-27 16:27:06 +01:00
Beniamino Galvani
99e0188886 initrd: add 'reader_' prefix to functions acting on Reader 2020-03-26 22:03:56 +01:00
Beniamino Galvani
25a2b6e14f initrd: rework command line parsing
The 'default_connection' created by the command line parser has
multiple purposes. It's the connection created for 'ip=' arguments
without command line, but is also created when there is a 'bootdev='
or for 'nameserver=' and no other connection exists at the moment the
argument is parsed. This is confusing and leads to a result that
depends on the order of parameters. For example:

 $ /usr/libexec/nm-initrd-generator -c connections -- bootdev=eth1 ip=eth0:dhcp
 $ ls connections/
 default_connection.nmconnection  eth0.nmconnection

 $ /usr/libexec/nm-initrd-generator -c connections -- ip=eth0:dhcp  bootdev=eth1
 $ ls connections/
 eth0.nmconnection  eth1.nmconnection

Make this more explicit by tracking 'bootdev_connection' and
'default_connection' individually.

Also fix handling of 'nameserver', 'rd.peerdns' and 'rd.route'
arguments. First process all connections, and then set those
properties. In particular, now nameservers are applied to all
connections.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/issues/391
2020-03-26 22:03:56 +01:00
Beniamino Galvani
f3c9ee921e initrd: sort ibft connections
Instead of adding ibft connections in a random order to the list, sort
them alphabetically.
2020-03-26 22:03:56 +01:00
Beniamino Galvani
9b5322b33c initrd: keep connections sorted
Connections are kept in a hash table indexed by name. This causes non
deterministic output in get_conn() when we have to decide a default
connection and no bootdev was specified on the command line.

Also add an array that stores the original order in which interfaces
appear in the command line, and use it when we have to loop through
connections. The return value of nmi_cmdline_reader_parse() is still a
hash table because once we have generated connections, their order
doesn't matter.
2020-03-26 22:03:56 +01:00
Beniamino Galvani
6a8c6ed674 initrd: fix indentation in iBFT reader 2020-03-26 22:03:56 +01:00
Beniamino Galvani
bba7663407 initrd: generate ipv6.method=ignore connection with ip=dhcp
The legacy network module used to leave kernel IPv6 autoconfiguration
enabled with ip=dhcp. Do the same for backwards compatibility.

https://github.com/dracutdevs/dracut/issues/700
2020-03-26 22:03:56 +01:00
Beniamino Galvani
d0e0213a08 initrd: don't add empty connection if iBFT parsing fails
Don't add an empty connection to the list if
nmi_ibft_update_connection_from_nic() fails when reading iBFT
information.

If the function fails in parse_ip(), continue with the existing
connection built from other command line options.

Also, fix a memory leak.
2020-03-26 22:03:56 +01:00
Thomas Haller
52dbab7d07 all: use nm_clear_pointer() instead of g_clear_pointer()
g_clear_pointer() would always cast the destroy notify function
pointer to GDestroyNotify. That means, it lost some type safety, like

   GPtrArray *ptr_arr = ...

   g_clear_pointer (&ptr_arr, g_array_unref);

Since glib 2.58 ([1]), g_clear_pointer() is also more type safe. But
this is not used by NetworkManager, because we don't set
GLIB_VERSION_MIN_REQUIRED to 2.58.

[1] f9a9902aac

We have nm_clear_pointer() to avoid this issue for a long time (pre
1.12.0). Possibly we should redefine in our source tree g_clear_pointer()
as nm_clear_pointer(). However, I don't like to patch glib functions
with our own variant. Arguably, we do patch g_clear_error() in
such a manner. But there the point is to make the function inlinable.

Also, nm_clear_pointer() returns a boolean that indicates whether
anything was cleared. That is sometimes useful. I think we should
just consistently use nm_clear_pointer() instead, which does always
the preferable thing.

Replace:

   sed 's/\<g_clear_pointer *(\([^;]*\), *\([a-z_A-Z0-9]\+\) *)/nm_clear_pointer (\1, \2)/g' $(git grep -l g_clear_pointer) -i
2020-03-23 11:22:38 +01:00
Thomas Haller
073994ca42 all: use nm_clear_g_free() instead of g_clear_pointer()
I think it's preferable to use nm_clear_g_free() instead of
g_clear_pointer(, g_free). The reasons are not very strong,
but I think it is overall preferable to have a shorthand for this
frequently used functionality.

   sed 's/\<g_clear_pointer *(\([^;]*\), *\(g_free\) *)/nm_clear_g_free (\1)/g' $(git grep -l g_clear_pointer) -i
2020-03-23 11:05:34 +01:00
Iñigo Martínez
648155e4a1 license: Add license using SPDX identifiers to meson build files
License is missing in meson build files. This has been added using
SPDX identifiers and licensed under LGPL-2.1+.
2020-02-17 13:16:57 +01:00
Thomas Haller
c69d703017 all: use g_ascii_strcasecmp() instead of the locale dependent strcasecmp()
In all the cases, we don't want to perform locale dependent comparison.

  $ sed -i 's/\<strcasecmp\>/g_ascii_\0/g' $(git grep -w -l strcasecmp -- ':(exclude)shared/systemd/' )
2020-02-11 15:23:06 +01:00
Thomas Haller
8f3b43f009 all: use nm_utils_ipaddr_is_valid() instead of nm_utils_ipaddr_valid()
We should use the same "is-valid" function everywhere.

Since nm_utils_ipaddr_valid() is part of libnm, it does not qualify.

Use nm_utils_ipaddr_is_valid() instead.
2020-01-28 11:17:41 +01:00
Thomas Haller
9f95b797f1 initrd/cmdline: minor style cleanups 2020-01-14 16:43:26 +01:00
Lubomir Rintel
39e1e723de initrd/cmdline: obey rd.iscsi.ibft
Do process the connections from the iBFT block if the rd.iscsi.ibft or
rd.iscsi.ibft=1 argument is present.

This is supposed to fix what was originally reported by Kairui Song
<kasong@redhat.com> here: https://github.com/dracutdevs/dracut/pull/697
2020-01-14 16:43:26 +01:00
Lubomir Rintel
59ead70952 initrd/ibft-reader: don't set con.interface-name in iBFT connections
If an argument in form ip=eth0:ibft is specified, we'd first create a
wired connection with con.interface-name and then proceed completing it
from the iBFT block. At that point we also add the MAC address, so the
interface-name is no longer necessary..

Worse even, for VLAN connections, it results in an attempt to create
a VLAN with the same name as the parent wired device. Ooops.

Let's just drop it. MAC address is guarranteed to be there and does the
right thing for both plain wired devices as well as VLANs.
2020-01-14 16:43:26 +01:00
Thomas Haller
d964decbbd libnm/keyfile: build keyfile code as separate GPL licensed internal library
Keyfile support was initially added under GPL-2.0+ license as part of
core. It was moved to "libnm-core" in commit 59eb5312a5 ('keyfile: merge
branch 'th/libnm-keyfile-bgo744699'').

"libnm-core" is statically linked with by core and "libnm". In
the former case under terms of GPL-2.0+ (good) and in the latter case
under terms of LGPL-2.1+ (bad).

In fact, to this day, "libnm" doesn't actually use the code. The linker
will probably remove all the GPL-2.0+ symbols when compiled with
gc-sections or LTO. Still, linking them together in the first place
makes "libnm" only available under GPL code (despite the code
not actually being used).

Instead, move the GPL code to a separate static library
"shared/nm-keyfile/libnm-keyfile.la" and only link it to the part
that actually uses the code (and which is GPL licensed too).

This fixes the license violation.

Eventually, it would be very useful to be able to expose keyfile
handling via "libnm". However that is not straight forward due to the
licensing conflict.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/merge_requests/381
2020-01-07 13:17:47 +01:00
Thomas Haller
9f76f5eb81 initrd: don't use inet_aton() to parse IPv4 address
inet_aton() is very accepting when parsing the address. For example,
it accepts addresses with fewer octets (interpreting the last octet
as a number in network byte order for multiple bytes). It also ignores
any trailing garbage after the first delimiting whitespace (at least,
the glibc implementation). It also accepts octets in hex and octal
notation.

For the initrd reader we want to be more forgiving than inet_pton()
and also accept addresses like 255.000.000.000 (octal notation). For
that we would want to use inet_aton(). But we should not accept all the
craziness that inet_aton() otherwise accepts.

Use nm_utils_parse_inaddr_bin_full() instead. This function implements
our way how we want to interpret IP addresses in string representation.
Under the hood, of course it also uses inet_pton() and even inet_aton(),
but it is stricter than inet_aton() and only accepts certain formats.

(cherry picked from commit d68373c305)
2019-12-05 13:12:04 +01:00
Thomas Haller
4f3e31f7c5 initrd: fix out-of-bounds read when detecting address family in dt_get_ipaddr_property()
The @family argument is an input and output argument.

Initially, the family is set to AF_UNSPEC, in which case the family
gets detected based on the IP address. However, we call
dt_get_ipaddr_property() multiple times to parse the netmask, the
gateway and the IP address.

That means, after the first successfull call, the @family is set to
AF_INET or AF_INET6.

Note that the previous code (in the switch block) would only check that
the family is set to AF_UNSPEC, but it would not check that the @family
matches the expected binary address length @len. Later, we then might call
nm_ip_address_new_binary() with a family and a binary address of
unexpected length.

Also drop the error checking for nm_ip_address_new_binary().
nm_ip_address_new_binary() can only fail if the prefix length is larger
than 32/128. The function has no way to validate the input arguments
beyond that and can thus not fail (short of undefined behavior).

(cherry picked from commit 9618f1bb4b)
2019-12-05 13:12:04 +01:00
Thomas Haller
90c33b798e initrd: use cleanup attribute in nmi_dt_reader_parse()
(cherry picked from commit e7cf22be3e)
2019-12-05 13:12:04 +01:00
Thomas Haller
f970039122 initrd: fix use-after-free for variable "s_gateway" in nmi_dt_reader_parse()
(cherry picked from commit 321a323df4)
2019-12-05 13:12:04 +01:00
Thomas Haller
1367e8a1b8 initrd: generate IPv6 profiles with ipv6.addr-gen-mode=eui64
https://bugzilla.redhat.com/show_bug.cgi?id=1779389
(cherry picked from commit ea4e95ec33)
2019-12-04 18:28:47 +01:00
Lubomir Rintel
cdfa3d3428 initrd/tests: put the bootif=no next to the other bootif tests
A cosmetical improvement.
2019-11-18 13:40:48 +01:00
Lubomir Rintel
45548bc670 dracut/cmdline: don't combine BOOTIF with on with a name or a MAC address
This fixes the dracut test suite.
2019-11-18 13:40:48 +01:00
Lubomir Rintel
e1a068e93c initrd/cmdline: split add_conn() from get_conn()
No change in behavior. Will be useful when we'll want to unconditionally
add new connection without the matching behavior.
2019-11-18 13:40:48 +01:00
Lubomir Rintel
30f8154319 initrd: don't overwrite just any connection's name with bootdev= argument
It is really not clear what the user could have meant by specifying a
bootdev= argument, and we deal with it just by ensuring a device with
that name whould come up.

We therefore pick a default connection if there's one (that is a
conneciton that we create if the device name is unspecified, as in
"ip=auto"), otherwise we create a new one.
2019-11-18 13:40:48 +01:00
Lubomir Rintel
f581756af6 initrd: default bridge name to br0 as opposed to bridge0
For compatibility. The dracut.cmdline(7) manual says:

  Bridge without parameters assumes bridge=br0:eth0
2019-11-18 13:40:48 +01:00
Lubomir Rintel
927ae6d927 initrd/tests: test that we generate the s390 interface names correctly 2019-11-04 16:22:14 +01:00
Dan Horák
22e388d90e initrd: handle rd.znet with legacy interface names
Handle rd.znet with legacy interface names too, the index for eth or ctc
corresponds to the position on the command line.
2019-11-04 16:22:14 +01:00
Dan Horák
c7423dca89 initrd: prepare interface in rd.znet only if persistent interface names are enabled
When processing the rd.znet option set the interface name only in case when
the persistent interface names feature isn't disabled via net.ifnames=0

[lkundrak@v3.sk: minor tweaks to the net.ifnames=0 parsing]
2019-11-04 16:21:58 +01:00
Dan Horák
c27f5030e9 initrd/tests: use a valid combination of device and interface name for testing 2019-11-04 16:21:16 +01:00
Dan Horák
adcc52c3da initrd: use proper interface when adding s390 specific details
The current solution for s390 specific details relies on an interface to
exist before adding the s390 details. It means the ip= option must precede
the rd.znet= option. Also only a single interface can be configured. With
this change the s390 details are put to the right interface and properly
named interface is created if it hasn't existed yet.
2019-11-04 15:44:58 +01:00
Thomas Haller
3b69f02164 all: unify format of our Copyright source code comments
```bash

readarray -d '' FILES < <(
  git ls-files -z \
    ':(exclude)po' \
    ':(exclude)shared/c-rbtree' \
    ':(exclude)shared/c-list' \
    ':(exclude)shared/c-siphash' \
    ':(exclude)shared/c-stdaux' \
    ':(exclude)shared/n-acd' \
    ':(exclude)shared/n-dhcp4' \
    ':(exclude)src/systemd/src' \
    ':(exclude)shared/systemd/src' \
    ':(exclude)m4' \
    ':(exclude)COPYING*'
  )

sed \
  -e 's/^\(--\|#\| \*\) *\(([cC]) *\)\?Copyright \+\(\(([cC])\) \+\)\?\(\(20\|19\)[0-9][0-9]\) *[-–] *\(\(20\|19\)[0-9][0-9]\) \+\([^ ].*\)$/\1 C1pyright#\5 - \7#\9/' \
  -e 's/^\(--\|#\| \*\) *\(([cC]) *\)\?Copyright \+\(\(([cC])\) \+\)\?\(\(20\|19\)[0-9][0-9]\) *[,] *\(\(20\|19\)[0-9][0-9]\) \+\([^ ].*\)$/\1 C2pyright#\5, \7#\9/' \
  -e 's/^\(--\|#\| \*\) *\(([cC]) *\)\?Copyright \+\(\(([cC])\) \+\)\?\(\(20\|19\)[0-9][0-9]\) \+\([^ ].*\)$/\1 C3pyright#\5#\7/' \
  -e 's/^Copyright \(\(20\|19\)[0-9][0-9]\) \+\([^ ].*\)$/C4pyright#\1#\3/' \
  -i \
  "${FILES[@]}"

echo ">>> untouched Copyright lines"
git grep Copyright "${FILES[@]}"

echo ">>> Copyright lines with unusual extra"
git grep '\<C[0-9]pyright#' "${FILES[@]}" | grep -i reserved

sed \
  -e 's/\<C[0-9]pyright#\([^#]*\)#\(.*\)$/Copyright (C) \1 \2/' \
  -i \
  "${FILES[@]}"

```

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/merge_requests/298
2019-10-02 17:03:52 +02:00
Iñigo Martínez
4e5b1e257e meson: Improve nm-initrd-generator target
The set of link targets used when building `nm-initrd-generator`
target has been grouped together.
2019-10-01 09:49:33 +02:00
Iñigo Martínez
31f1516760 meson: Improve the src build file
The targets that involve the use of the `NetworkManager` library,
built in the `src` build file have been improved by applying a set
of changes:

- Indentation has been fixed.
- Set of objects used in targets have been grouped together.
- Aritificial dependencies used to group dependencies and custom
  compiler flags have been removed and their use replaced with
  proper dependencies and compiler flags to avoid any confussion.
2019-10-01 09:49:33 +02:00
Iñigo Martínez
f427f4771e meson: Improve the libnm-core build file
The `libnm-core` build file has been improved by applying a set of
changes:

- Indentation has been fixed to be consistent.
- Library variable names have been changed to `lib{name}` pattern
  following their filename pattern.
- `shared` prefix has been removed from all variables using it.
- Dependencies have been reviewed to store the necessary data.
- The use of the libraries and dependencies created in this file
  has been reviewed through the entire source code. This has
  required the addition or the removal of different libraries and
  dependencies in different targets.
- Some files used directly with the `files` function have been moved
  to their nearest path build file because meson stores their full
  path seamessly and they can be used anywhere later.
2019-10-01 09:49:33 +02:00
Iñigo Martínez
70a34c54fe meson: Use dependency for nm-default header
The `nm-default.h` header is used widely in the code by many
targets. This header includes different headers and needs different
libraries depending the compilation flags.

A new set of `*nm_default_dep` dependencies have been created to
ease the inclusion of different directorires and libraries.

This allows cleaner build files and avoiding linking unnecessary
libraries so this has been applied allowing the removal of some
dependencies involving the linking of unnecessary libraries.
2019-10-01 09:49:33 +02:00
Thomas Haller
abff46cacf all: manually drop code comments with file description 2019-10-01 07:50:52 +02:00