diff options
Diffstat (limited to 'src/initrd')
29 files changed, 786 insertions, 178 deletions
diff --git a/src/initrd/meson.build b/src/initrd/meson.build index fa8c016b..09036f27 100644 --- a/src/initrd/meson.build +++ b/src/initrd/meson.build @@ -1,26 +1,29 @@ sources = files( 'nmi-cmdline-reader.c', + 'nmi-dt-reader.c', 'nmi-ibft-reader.c', ) -nm_cflags = ['-DNETWORKMANAGER_COMPILATION=NM_NETWORKMANAGER_COMPILATION_DAEMON'] - libnmi_core = static_library( 'nmi-core', - c_args: nm_cflags, sources: sources, - include_directories: src_inc, - dependencies: libnm_core_dep, + dependencies: daemon_nm_default_dep, + c_args: daemon_c_flags, ) name = 'nm-initrd-generator' + +links = [ + libnetwork_manager_base, + libnmi_core, +] + executable( name, name + '.c', - c_args: nm_cflags, - include_directories: src_inc, - dependencies: [ libnm_core_dep ], - link_with: [libnetwork_manager_base, libnmi_core], + dependencies: daemon_nm_default_dep, + c_args: daemon_c_flags, + link_with: links, link_args: ldflags_linker_script_binary, link_depends: linker_script_binary, install: true, diff --git a/src/initrd/nm-initrd-generator.c b/src/initrd/nm-initrd-generator.c index 0ff3428e..f3f53acf 100644 --- a/src/initrd/nm-initrd-generator.c +++ b/src/initrd/nm-initrd-generator.c @@ -1,20 +1,5 @@ -/* NetworkManager initrd configuration generator - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301 USA. - * +// SPDX-License-Identifier: LGPL-2.1+ +/* * Copyright (C) 2018 Red Hat, Inc. */ @@ -63,7 +48,12 @@ output_conn (gpointer key, gpointer value, gpointer user_data) filename = nm_keyfile_utils_create_filename (basename, TRUE); full_filename = g_build_filename (connections_dir, filename, NULL); - if (!nm_utils_file_set_contents (full_filename, data, len, 0600, &error)) + if (!nm_utils_file_set_contents (full_filename, + data, + len, + 0600, + NULL, + &error)) goto err_out; } else g_print ("\n*** Connection '%s' ***\n\n%s", basename, data); diff --git a/src/initrd/nm-initrd-generator.h b/src/initrd/nm-initrd-generator.h index 7ff9a92e..cba383d1 100644 --- a/src/initrd/nm-initrd-generator.h +++ b/src/initrd/nm-initrd-generator.h @@ -1,20 +1,5 @@ -/* NetworkManager initrd configuration generator - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301 USA. - * +// SPDX-License-Identifier: LGPL-2.1+ +/* * Copyright (C) 2014, 2018 Red Hat, Inc. */ @@ -41,6 +26,8 @@ GHashTable *nmi_ibft_read (const char *sysfs_dir); gboolean nmi_ibft_update_connection_from_nic (NMConnection *connection, GHashTable *nic, GError **error); +NMConnection *nmi_dt_reader_parse (const char *sysfs_dir); + GHashTable *nmi_cmdline_reader_parse (const char *sysfs_dir, const char *const*argv); #endif /* __NM_INITRD_GENERATOR_H__ */ diff --git a/src/initrd/nmi-cmdline-reader.c b/src/initrd/nmi-cmdline-reader.c index b90ee203..c32ef1cf 100644 --- a/src/initrd/nmi-cmdline-reader.c +++ b/src/initrd/nmi-cmdline-reader.c @@ -1,20 +1,5 @@ -/* NetworkManager initrd configuration generator - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301 USA. - * +// SPDX-License-Identifier: LGPL-2.1+ +/* * Copyright (C) 2018 Red Hat, Inc. */ @@ -47,6 +32,50 @@ _connection_matches_type (gpointer key, gpointer value, gpointer user_data) } static NMConnection * +add_conn (GHashTable *connections, + const char *basename, + const char *id, + const char *ifname, + const char *type_name, + NMConnectionMultiConnect multi_connect) +{ + NMConnection *connection; + NMSetting *setting; + + connection = nm_simple_connection_new (); + g_hash_table_insert (connections, g_strdup (basename), connection); + + /* Start off assuming dynamic IP configurations. */ + + setting = nm_setting_ip4_config_new (); + nm_connection_add_setting (connection, setting); + g_object_set (setting, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, + NULL); + + setting = nm_setting_ip6_config_new (); + nm_connection_add_setting (connection, setting); + g_object_set (setting, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP6_CONFIG_ADDR_GEN_MODE, (int) NM_SETTING_IP6_CONFIG_ADDR_GEN_MODE_EUI64, + NULL); + + setting = nm_setting_connection_new (); + nm_connection_add_setting (connection, setting); + g_object_set (setting, + NM_SETTING_CONNECTION_ID, id, + NM_SETTING_CONNECTION_UUID, nm_utils_uuid_generate_a (), + NM_SETTING_CONNECTION_INTERFACE_NAME, ifname, + NM_SETTING_CONNECTION_TYPE, type_name, + NM_SETTING_CONNECTION_MULTI_CONNECT, multi_connect, + NULL); + + return connection; +} + +static NMConnection * get_conn (GHashTable *connections, const char *ifname, const char *type_name) { NMConnection *connection; @@ -76,40 +105,15 @@ get_conn (GHashTable *connections, const char *ifname, const char *type_name) (gpointer) type_name); } - if (connection) { - setting = (NMSetting *)nm_connection_get_setting_connection (connection); - } else { - connection = nm_simple_connection_new (); - g_hash_table_insert (connections, g_strdup (basename), connection); - - /* Start off assuming dynamic IP configurations. */ - - setting = nm_setting_ip4_config_new (); - nm_connection_add_setting (connection, setting); - g_object_set (setting, - NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, - NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, - NULL); - - setting = nm_setting_ip6_config_new (); - nm_connection_add_setting (connection, setting); - g_object_set (setting, - NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, - NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, - NULL); - - setting = nm_setting_connection_new (); - nm_connection_add_setting (connection, setting); - g_object_set (setting, - NM_SETTING_CONNECTION_ID, ifname ?: "Wired Connection", - NM_SETTING_CONNECTION_UUID, nm_utils_uuid_generate_a (), - NM_SETTING_CONNECTION_INTERFACE_NAME, ifname, - NM_SETTING_CONNECTION_MULTI_CONNECT, multi_connect, - NULL); - + if (!connection) { if (!type_name) type_name = NM_SETTING_WIRED_SETTING_NAME; + + connection = add_conn (connections, basename, + ifname ?: "Wired Connection", + ifname, type_name, multi_connect); } + setting = (NMSetting *)nm_connection_get_setting_connection (connection); if (type_name) { g_object_set (setting, NM_SETTING_CONNECTION_TYPE, type_name, NULL); @@ -252,7 +256,8 @@ parse_ip (GHashTable *connections, const char *sysfs_dir, char *argument) } } - if (ifname == NULL && g_strcmp0 (kind, "ibft") == 0) { + if (ifname == NULL && ( g_strcmp0 (kind, "fw") == 0 + || g_strcmp0 (kind, "ibft") == 0)) { GHashTableIter iter; const char *mac; GHashTable *nic; @@ -284,6 +289,13 @@ parse_ip (GHashTable *connections, const char *sysfs_dir, char *argument) connection); } + connection = nmi_dt_reader_parse (sysfs_dir); + if (connection) { + g_hash_table_insert (connections, + g_strdup ("ofw"), + connection); + } + return; } @@ -480,7 +492,10 @@ parse_ip (GHashTable *connections, const char *sysfs_dir, char *argument) } static void -parse_master (GHashTable *connections, char *argument, const char *type_name) +parse_master (GHashTable *connections, + char *argument, + const char *type_name, + const char *default_name) { NMConnection *connection; NMSettingConnection *s_con; @@ -496,7 +511,7 @@ parse_master (GHashTable *connections, char *argument, const char *type_name) master = get_word (&argument, ':'); if (!master) - master = master_to_free = g_strdup_printf ("%s0", type_name); + master = master_to_free = g_strdup_printf ("%s0", default_name ?: type_name); slaves = get_word (&argument, ':'); connection = get_conn (connections, master, type_name); @@ -634,6 +649,13 @@ parse_bootdev (GHashTable *connections, char *argument) connection = get_conn (connections, NULL, NULL); + if ( nm_connection_get_interface_name (connection) + && strcmp (nm_connection_get_interface_name (connection), argument) != 0) { + /* If the default connection already has an interface name, + * we should not overwrite it. Create a new one instead. */ + connection = get_conn (connections, argument, NULL); + } + s_con = nm_connection_get_setting_connection (connection); g_object_set (s_con, NM_SETTING_CONNECTION_INTERFACE_NAME, argument, @@ -803,11 +825,11 @@ nmi_cmdline_reader_parse (const char *sysfs_dir, const char *const*argv) else if (strcmp (tag, "rd.route") == 0) parse_rd_route (connections, argument); else if (strcmp (tag, "bridge") == 0) - parse_master (connections, argument, NM_SETTING_BRIDGE_SETTING_NAME); + parse_master (connections, argument, NM_SETTING_BRIDGE_SETTING_NAME, "br"); else if (strcmp (tag, "bond") == 0) - parse_master (connections, argument, NM_SETTING_BOND_SETTING_NAME); + parse_master (connections, argument, NM_SETTING_BOND_SETTING_NAME, NULL); else if (strcmp (tag, "team") == 0) - parse_master (connections, argument, NM_SETTING_TEAM_SETTING_NAME); + parse_master (connections, argument, NM_SETTING_TEAM_SETTING_NAME, NULL); else if (strcmp (tag, "vlan") == 0) parse_vlan (connections, argument); else if (strcmp (tag, "bootdev") == 0) @@ -846,8 +868,19 @@ nmi_cmdline_reader_parse (const char *sysfs_dir, const char *const*argv) } connection = get_conn (connections, NULL, NM_SETTING_WIRED_SETTING_NAME); - s_wired = nm_connection_get_setting_wired (connection); + + if ( nm_connection_get_interface_name (connection) + || ( nm_setting_wired_get_mac_address (s_wired) + && !nm_utils_hwaddr_matches (nm_setting_wired_get_mac_address (s_wired), -1, + bootif, -1))) { + connection = add_conn (connections, "bootif_connection", "BOOTIF Connection", + NULL, NM_SETTING_WIRED_SETTING_NAME, + NM_CONNECTION_MULTI_CONNECT_SINGLE); + s_wired = (NMSettingWired *) nm_setting_wired_new (); + nm_connection_add_setting (connection, (NMSetting *) s_wired); + } + g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, bootif, NULL); diff --git a/src/initrd/nmi-dt-reader.c b/src/initrd/nmi-dt-reader.c new file mode 100644 index 00000000..a8677ed9 --- /dev/null +++ b/src/initrd/nmi-dt-reader.c @@ -0,0 +1,370 @@ +// SPDX-License-Identifier: LGPL-2.1+ +/* + * Copyright (C) 2019 Red Hat, Inc. + */ + +#include "nm-default.h" + +#include "nm-initrd-generator.h" + +#include <arpa/inet.h> + +#include "nm-core-internal.h" + +/*****************************************************************************/ + +#define _NMLOG(level, domain, ...) \ + nm_log ((level), (domain), NULL, NULL, \ + "dt-reader: " _NM_UTILS_MACRO_FIRST (__VA_ARGS__) \ + _NM_UTILS_MACRO_REST (__VA_ARGS__)) + +/*****************************************************************************/ + +static gboolean +dt_get_property (const char *base, + const char *dev, + const char *prop, + char **contents, + size_t *length) +{ + gs_free char *filename = g_build_filename (base, dev, prop, NULL); + gs_free_error GError *error = NULL; + + if (!g_file_test (filename, G_FILE_TEST_EXISTS)) + return FALSE; + + if (!contents) + return TRUE; + + if (!g_file_get_contents (filename, contents, length, &error)) { + _LOGW (LOGD_CORE, "%s: Can not read the %s property: %s", + dev, prop, error->message); + return FALSE; + } + + return TRUE; +} + +static NMIPAddress * +dt_get_ipaddr_property (const char *base, + const char *dev, + const char *prop, + int *family) +{ + gs_free char *buf = NULL; + size_t len; + int f; + + if (!dt_get_property (base, dev, prop, &buf, &len)) + return NULL; + + f = nm_utils_addr_family_from_size (len); + if ( f == AF_UNSPEC + || ( *family != AF_UNSPEC + && *family != f)) { + _LOGW (LOGD_CORE, "%s: Address %s has unrecognized length (%zd)", + dev, prop, len); + return NULL; + } + + *family = f; + return nm_ip_address_new_binary (f, buf, 0, NULL); +} + +static char * +dt_get_hwaddr_property (const char *base, + const char *dev, + const char *prop) +{ + gs_free guint8 *buf = NULL; + size_t len; + + if (!dt_get_property (base, dev, prop, (char **) &buf, &len)) + return NULL; + + if (len != ETH_ALEN) { + _LOGW (LOGD_CORE, "%s: MAC address %s has unrecognized length (%zd)", + dev, prop, len); + return NULL; + } + + return g_strdup_printf ("%02x:%02x:%02x:%02x:%02x:%02x", + buf[0], buf[1], buf[2], + buf[3], buf[4], buf[4]); +} + +static NMIPAddress * +str_addr (const char *str, int *family) +{ + NMIPAddr addr_bin; + + if (!nm_utils_parse_inaddr_bin_full (*family, + TRUE, + str, + family, + &addr_bin)) { + _LOGW (LOGD_CORE, "Malformed IP address: '%s'", str); + return NULL; + } + return nm_ip_address_new_binary (*family, &addr_bin, 0, NULL); +} + +NMConnection * +nmi_dt_reader_parse (const char *sysfs_dir) +{ + gs_unref_object NMConnection *connection = NULL; + gs_free char *base = NULL; + gs_free char *bootpath = NULL; + gs_strfreev char **tokens = NULL; + char *path = NULL; + gboolean bootp = FALSE; + const char *s_ipaddr = NULL; + const char *s_netmask = NULL; + const char *s_gateway = NULL; + nm_auto_unref_ip_address NMIPAddress *ipaddr = NULL; + nm_auto_unref_ip_address NMIPAddress *gateway = NULL; + const char *duplex = NULL; + gs_free char *hwaddr = NULL; + gs_free char *local_hwaddr = NULL; + gs_free char *hostname = NULL; + guint32 speed = 0; + int prefix = -1; + NMSettingIPConfig *s_ip = NULL; + NMSetting *s_ip4 = NULL; + NMSetting *s_ip6 = NULL; + NMSetting *s_wired = NULL; + int family = AF_UNSPEC; + int i = 0; + char *c; + gs_free_error GError *error = NULL; + + base = g_build_filename (sysfs_dir, "firmware", "devicetree", + "base", NULL); + + if (!dt_get_property (base, "chosen", "bootpath", &bootpath, NULL)) + return NULL; + + c = strchr (bootpath, ':'); + if (c) { + *c = '\0'; + path = c + 1; + } else { + path = ""; + } + + dt_get_property (base, "chosen", "client-name", &hostname, NULL); + + local_hwaddr = dt_get_hwaddr_property (base, bootpath, "local-mac-address"); + hwaddr = dt_get_hwaddr_property (base, bootpath, "mac-address"); + if (g_strcmp0 (local_hwaddr, hwaddr) == 0) + g_clear_pointer (&local_hwaddr, g_free); + + tokens = g_strsplit (path, ",", 0); + + /* + * Ethernet device settings. Defined by "Open Firmware, + * Recommended Practice: Device Support Extensions, Version 1.0 [1] + * [1] https://www.devicetree.org/open-firmware/practice/devicex/dse1_0a.ps + */ + + for (i = 0; tokens[i]; i++) { + /* Skip these. They have magical meaning for OpenFirmware. */ + if ( strcmp (tokens[i], "nfs") == 0 + || strcmp (tokens[i], "last") == 0) + continue; + if (strcmp (tokens[i], "promiscuous") == 0) { + /* Ignore. */ + continue; + } + + if (g_str_has_prefix (tokens[i], "speed=")) { + speed = _nm_utils_ascii_str_to_int64 (tokens[i] + 6, + 10, 0, G_MAXUINT32, 0); + continue; + } + + if (g_str_has_prefix (tokens[i], "duplex=auto")) { + continue; + } else if ( g_str_has_prefix (tokens[i], "duplex=half") + || g_str_has_prefix (tokens[i], "duplex=full")) { + duplex = tokens[i] + 7; + continue; + } + + break; + } + + /* + * Network boot configuration. Defined by "Open Firmware, + * Recommended Practice: TFTP Booting Extension, Version 1.0 [1] + * [1] https://www.devicetree.org/open-firmware/practice/obp-tftp/tftp1_0.pdf + */ + + for (; tokens[i]; i++) { + if ( strcmp (tokens[i], "bootp") == 0 + || strcmp (tokens[i], "dhcp") == 0 + || strcmp (tokens[i], "rarp") == 0) { + bootp = TRUE; + continue; + } + break; + } + + /* s-iaddr, or perhaps a raw absolute filename */ + if (tokens[i] && tokens[i][0] != '/') + i++; + + /* filename */ + if (tokens[i]) + i++; + + /* c-iaddr */ + if (tokens[i]) { + s_ipaddr = tokens[i]; + i++; + } + + /* g-iaddr */ + if (tokens[i]) { + s_gateway = tokens[i]; + i++; + } + + if (tokens[i] && ( strchr (tokens[i], '.') + || strchr (tokens[i], ':'))) { + /* yaboot claims the mask can be specified here, + * though it doesn't support it. */ + s_netmask = tokens[i]; + i++; + } + + /* bootp-retries */ + if (tokens[i]) + i++; + + /* tftp-retries */ + if (tokens[i]) + i++; + + if (tokens[i]) { + /* yaboot accepts a mask here */ + s_netmask = tokens[i]; + i++; + } + + connection = nm_simple_connection_new (); + + nm_connection_add_setting (connection, + g_object_new (NM_TYPE_SETTING_CONNECTION, + NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRED_SETTING_NAME, + NM_SETTING_CONNECTION_ID, "OpenFirmware Connection", + NULL)); + + s_ip4 = nm_setting_ip4_config_new (); + nm_connection_add_setting (connection, s_ip4); + + s_ip6 = nm_setting_ip6_config_new (); + nm_connection_add_setting (connection, s_ip6); + + g_object_set (s_ip6, + NM_SETTING_IP6_CONFIG_ADDR_GEN_MODE, (int) NM_SETTING_IP6_CONFIG_ADDR_GEN_MODE_EUI64, + NULL); + + if (!bootp && dt_get_property (base, "chosen", "bootp-response", NULL, NULL)) + bootp = TRUE; + + if (!bootp) { + nm_auto_unref_ip_address NMIPAddress *netmask = NULL; + + netmask = dt_get_ipaddr_property (base, "chosen", "netmask-ip", &family); + gateway = dt_get_ipaddr_property (base, "chosen", "gateway-ip", &family); + if (gateway) + s_gateway = nm_ip_address_get_address (gateway); + ipaddr = dt_get_ipaddr_property (base, "chosen", "client-ip", &family); + + if (family == AF_UNSPEC) { + nm_assert (netmask == NULL); + nm_assert (gateway == NULL); + nm_assert (ipaddr == NULL); + + netmask = str_addr (s_netmask, &family); + ipaddr = str_addr (s_ipaddr, &family); + + prefix = _nm_utils_ascii_str_to_int64 (s_netmask, 10, 0, 128, -1); + } + + if (prefix == -1 && family == AF_INET && netmask) { + guint32 netmask_v4; + + nm_ip_address_get_address_binary (netmask, &netmask_v4); + prefix = nm_utils_ip4_netmask_to_prefix (netmask_v4); + } + + if (prefix == -1) + _LOGW (LOGD_CORE, "Unable to determine the network prefix"); + else + nm_ip_address_set_prefix (ipaddr, prefix); + } + + if (!ipaddr) { + family = AF_UNSPEC; + bootp = TRUE; + } + + if (bootp) { + g_object_set (s_ip4, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_DHCP_HOSTNAME, hostname, + NULL); + g_object_set (s_ip6, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_DHCP_HOSTNAME, hostname, + NULL); + } else { + switch (family) { + case AF_INET: + s_ip = (NMSettingIPConfig *) s_ip4; + g_object_set (s_ip4, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NULL); + g_object_set (s_ip6, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_DISABLED, + NULL); + break; + case AF_INET6: + s_ip = (NMSettingIPConfig *) s_ip6; + g_object_set (s_ip4, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_DISABLED, + NULL); + g_object_set (s_ip6, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_MANUAL, + NULL); + break; + default: + g_return_val_if_reached (NULL); + } + + nm_setting_ip_config_add_address (s_ip, ipaddr); + g_object_set (s_ip, NM_SETTING_IP_CONFIG_GATEWAY, s_gateway, NULL); + } + + if (duplex || speed || hwaddr || local_hwaddr) { + s_wired = nm_setting_wired_new (); + nm_connection_add_setting (connection, s_wired); + + g_object_set (s_wired, + NM_SETTING_WIRED_SPEED, speed, + NM_SETTING_WIRED_DUPLEX, duplex, + NM_SETTING_WIRED_MAC_ADDRESS, hwaddr, + NM_SETTING_WIRED_CLONED_MAC_ADDRESS, local_hwaddr, + NULL); + } + + if (!nm_connection_normalize (connection, NULL, NULL, &error)) { + _LOGW (LOGD_CORE, "Generated an invalid connection: %s", + error->message); + g_clear_pointer (&connection, g_object_unref); + } + + return g_steal_pointer (&connection); +} diff --git a/src/initrd/nmi-ibft-reader.c b/src/initrd/nmi-ibft-reader.c index 2db38e2c..ffce98fc 100644 --- a/src/initrd/nmi-ibft-reader.c +++ b/src/initrd/nmi-ibft-reader.c @@ -1,21 +1,6 @@ -/* NetworkManager initrd configuration generator - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301 USA. - * - * Copyright 2014 - 2018 Red Hat, Inc. +// SPDX-License-Identifier: LGPL-2.1+ +/* + * Copyright (C) 2014 - 2018 Red Hat, Inc. */ #include "nm-default.h" @@ -172,6 +157,10 @@ ip_setting_add_from_block (GHashTable *nic, if (!s_ip6) { s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, (NMSetting *) s_ip6); + + g_object_set (s_ip6, + NM_SETTING_IP6_CONFIG_ADDR_GEN_MODE, (int) NM_SETTING_IP6_CONFIG_ADDR_GEN_MODE_EUI64, + NULL); } family = guess_ip_address_family (s_ipaddr); diff --git a/src/initrd/tests/meson.build b/src/initrd/tests/meson.build index 20cf6af2..5dd2873c 100644 --- a/src/initrd/tests/meson.build +++ b/src/initrd/tests/meson.build @@ -1,20 +1,20 @@ +c_flags = test_c_flags + ['-DTEST_INITRD_DIR="@0@"'.format(meson.current_source_dir())] + test_units = [ + 'test-dt-reader', 'test-ibft-reader', 'test-cmdline-reader', ] -cflags = [ - '-DTEST_INITRD_DIR="@0@"'.format(meson.current_source_dir()), -] - foreach test_unit : test_units exe = executable( test_unit, test_unit + '.c', - dependencies: test_nm_dep, - c_args: cflags, + dependencies: libnetwork_manager_test_dep, + c_args: c_flags, link_with: libnmi_core, ) + test( 'initrd/' + test_unit, test_script, diff --git a/src/initrd/tests/sysfs-dt-tftp/firmware/devicetree/base/chosen/bootpath b/src/initrd/tests/sysfs-dt-tftp/firmware/devicetree/base/chosen/bootpath new file mode 100644 index 00000000..6f069ae0 --- /dev/null +++ b/src/initrd/tests/sysfs-dt-tftp/firmware/devicetree/base/chosen/bootpath Binary files differdiff --git a/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/bootp-request b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/bootp-request new file mode 100644 index 00000000..034d423e --- /dev/null +++ b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/bootp-request Binary files differdiff --git a/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/bootp-response b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/bootp-response new file mode 100644 index 00000000..25982d6f --- /dev/null +++ b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/bootp-response Binary files differdiff --git a/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/bootpath b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/bootpath new file mode 100644 index 00000000..db880702 --- /dev/null +++ b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/bootpath Binary files differdiff --git a/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/broadcast-ip b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/broadcast-ip new file mode 100644 index 00000000..7bde8641 --- /dev/null +++ b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/broadcast-ip @@ -0,0 +1 @@ +ÿÿÿÿ \ No newline at end of file diff --git a/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/client-ip b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/client-ip new file mode 100644 index 00000000..f108ba9b --- /dev/null +++ b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/client-ip @@ -0,0 +1,2 @@ + ++ \ No newline at end of file diff --git a/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/client-name b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/client-name new file mode 100644 index 00000000..fe0f76b8 --- /dev/null +++ b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/client-name Binary files differdiff --git a/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/domain-name b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/domain-name new file mode 100644 index 00000000..b34f9f96 --- /dev/null +++ b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/domain-name Binary files differdiff --git a/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/gateway-ip b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/gateway-ip new file mode 100644 index 00000000..f0361835 --- /dev/null +++ b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/gateway-ip @@ -0,0 +1,2 @@ + ++þ \ No newline at end of file diff --git a/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/name b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/name new file mode 100644 index 00000000..f3e58052 --- /dev/null +++ b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/name Binary files differdiff --git a/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/netmask-ip b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/netmask-ip new file mode 100644 index 00000000..d441cd83 --- /dev/null +++ b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/netmask-ip Binary files differdiff --git a/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/root-path b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/root-path new file mode 100644 index 00000000..f76dd238 --- /dev/null +++ b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/root-path Binary files differdiff --git a/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/server-ip b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/server-ip new file mode 100644 index 00000000..a2a4b2e8 --- /dev/null +++ b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/server-ip @@ -0,0 +1,2 @@ + +& \ No newline at end of file diff --git a/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/tftp-file b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/tftp-file new file mode 100644 index 00000000..c0e0e330 --- /dev/null +++ b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/tftp-file Binary files differdiff --git a/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/vendor-options b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/vendor-options new file mode 100644 index 00000000..f76dd238 --- /dev/null +++ b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/chosen/vendor-options Binary files differdiff --git a/src/initrd/tests/sysfs-dt/firmware/devicetree/base/ethernet/device_type b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/ethernet/device_type new file mode 100644 index 00000000..df3c9d9f --- /dev/null +++ b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/ethernet/device_type Binary files differdiff --git a/src/initrd/tests/sysfs-dt/firmware/devicetree/base/ethernet/local-mac-address b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/ethernet/local-mac-address new file mode 100644 index 00000000..c983e752 --- /dev/null +++ b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/ethernet/local-mac-address @@ -0,0 +1 @@ +¬>åØ \ No newline at end of file diff --git a/src/initrd/tests/sysfs-dt/firmware/devicetree/base/ethernet/mac-address b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/ethernet/mac-address new file mode 100644 index 00000000..c983e752 --- /dev/null +++ b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/ethernet/mac-address @@ -0,0 +1 @@ +¬>åØ \ No newline at end of file diff --git a/src/initrd/tests/sysfs-dt/firmware/devicetree/base/ethernet/name b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/ethernet/name new file mode 100644 index 00000000..88eaddfe --- /dev/null +++ b/src/initrd/tests/sysfs-dt/firmware/devicetree/base/ethernet/name Binary files differdiff --git a/src/initrd/tests/test-cmdline-reader.c b/src/initrd/tests/test-cmdline-reader.c index 5ac82f50..1d4bb9a6 100644 --- a/src/initrd/tests/test-cmdline-reader.c +++ b/src/initrd/tests/test-cmdline-reader.c @@ -1,21 +1,6 @@ -/* NetworkManager initrd configuration generator - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301 USA. - * - * Copyright 2018 Red Hat, Inc. +// SPDX-License-Identifier: LGPL-2.1+ +/* + * Copyright (C) 2018 Red Hat, Inc. */ #include "nm-default.h" @@ -283,8 +268,7 @@ test_multiple (void) { gs_unref_hashtable GHashTable *connections = NULL; const char *const*ARGV = NM_MAKE_STRV ("ip=192.0.2.2:::::eth0", - "ip=[2001:db8::2]:::::eth0", - "BOOTIF=00:53:AB:cd:02:03"); + "ip=[2001:db8::2]:::::eth0"); NMConnection *connection; NMSettingWired *s_wired; NMSettingIPConfig *s_ip4; @@ -302,7 +286,6 @@ test_multiple (void) s_wired = nm_connection_get_setting_wired (connection); g_assert (s_wired); - g_assert_cmpstr (nm_setting_wired_get_mac_address (s_wired), ==, "00:53:AB:CD:02:03"); s_ip4 = nm_connection_get_setting_ip4_config (connection); g_assert (s_ip4); @@ -324,12 +307,45 @@ test_multiple (void) } static void +test_bootdev (void) +{ + gs_unref_hashtable GHashTable *connections = NULL; + const char *const*ARGV = NM_MAKE_STRV ("vlan=vlan2:ens5", "bootdev=ens3"); + NMConnection *connection; + NMSettingConnection *s_con; + + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + g_assert (connections); + g_assert_cmpint (g_hash_table_size (connections), ==, 2); + + connection = g_hash_table_lookup (connections, "ens3"); + g_assert (connection); + nmtst_assert_connection_verifies_without_normalization (connection); + + s_con = nm_connection_get_setting_connection (connection); + g_assert (s_con); + g_assert_cmpstr (nm_setting_connection_get_connection_type (s_con), ==, NM_SETTING_WIRED_SETTING_NAME); + g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "ens3"); + g_assert_cmpstr (nm_setting_connection_get_interface_name (s_con), ==, "ens3"); + + connection = g_hash_table_lookup (connections, "vlan2"); + g_assert (connection); + nmtst_assert_connection_verifies_without_normalization (connection); + + s_con = nm_connection_get_setting_connection (connection); + g_assert (s_con); + g_assert_cmpstr (nm_setting_connection_get_connection_type (s_con), ==, NM_SETTING_VLAN_SETTING_NAME); + g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "vlan2"); + g_assert_cmpstr (nm_setting_connection_get_interface_name (s_con), ==, "vlan2"); +} + +static void test_some_more (void) { gs_unref_hashtable GHashTable *connections = NULL; const char *const*ARGV = NM_MAKE_STRV ("bootdev=eth1", "hail", "nameserver=[2001:DB8:3::53]", "satan", "nameserver=192.0.2.53", "worship", - "BOOTIF=01-00-53-AB-cd-02-03", "doom", "rd.peerdns=0", + "doom", "rd.peerdns=0", "rd.route=[2001:DB8:3::/48]:[2001:DB8:2::1]:ens10"); NMConnection *connection; NMSettingConnection *s_con; @@ -355,7 +371,6 @@ test_some_more (void) s_wired = nm_connection_get_setting_wired (connection); g_assert (s_wired); - g_assert_cmpstr (nm_setting_wired_get_mac_address (s_wired), ==, "00:53:AB:CD:02:03"); s_ip4 = nm_connection_get_setting_ip4_config (connection); g_assert (s_ip4); @@ -406,17 +421,6 @@ test_some_more (void) } static void -test_no_bootif (void) -{ - gs_unref_hashtable GHashTable *connections = NULL; - const char *const*ARGV = NM_MAKE_STRV ("BOOTIF=01-00-53-AB-cd-02-03", "rd.bootif=0"); - - connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); - g_assert (connections); - g_assert_cmpint (g_hash_table_size (connections), ==, 0); -} - -static void test_bond (void) { gs_unref_hashtable GHashTable *connections = NULL; @@ -651,12 +655,12 @@ test_bridge_default (void) g_assert (connections); g_assert_cmpint (g_hash_table_size (connections), ==, 2); - connection = g_hash_table_lookup (connections, "bridge0"); + connection = g_hash_table_lookup (connections, "br0"); g_assert (connection); nmtst_assert_connection_verifies_without_normalization (connection); g_assert_cmpstr (nm_connection_get_connection_type (connection), ==, NM_SETTING_BRIDGE_SETTING_NAME); - g_assert_cmpstr (nm_connection_get_id (connection), ==, "bridge0"); + g_assert_cmpstr (nm_connection_get_id (connection), ==, "br0"); master_uuid = nm_connection_get_uuid (connection); g_assert (master_uuid); @@ -917,6 +921,109 @@ test_rd_znet_legacy (void) nmtst_assert_connection_verifies_without_normalization (connection); } +static void +test_bootif (void) +{ + gs_unref_hashtable GHashTable *connections = NULL; + const char *const*ARGV = NM_MAKE_STRV ("BOOTIF=00:53:AB:cd:02:03", + "ip=dhcp"); + NMConnection *connection; + NMSettingWired *s_wired; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; + + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + g_assert (connections); + g_assert_cmpint (g_hash_table_size (connections), ==, 1); + + connection = g_hash_table_lookup (connections, "default_connection"); + g_assert (connection); + nmtst_assert_connection_verifies_without_normalization (connection); + g_assert_cmpstr (nm_connection_get_id (connection), ==, "Wired Connection"); + + s_wired = nm_connection_get_setting_wired (connection); + g_assert_cmpstr (nm_setting_wired_get_mac_address (s_wired), ==, "00:53:AB:CD:02:03"); + g_assert (s_wired); + + s_ip4 = nm_connection_get_setting_ip4_config (connection); + g_assert (s_ip4); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_AUTO); + g_assert (!nm_setting_ip_config_get_ignore_auto_dns (s_ip4)); + g_assert (!nm_setting_ip_config_get_may_fail (s_ip4)); + + s_ip6 = nm_connection_get_setting_ip6_config (connection); + g_assert (s_ip6); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_DISABLED); + g_assert (!nm_setting_ip_config_get_ignore_auto_dns (s_ip6)); +} + +static void +test_bootif_hwtype (void) +{ + gs_unref_hashtable GHashTable *connections = NULL; + const char *const*ARGV = NM_MAKE_STRV ("ip=eth0:dhcp", + "BOOTIF=01-00-53-AB-cd-02-03"); + NMConnection *connection; + NMSettingWired *s_wired; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; + + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + g_assert (connections); + g_assert_cmpint (g_hash_table_size (connections), ==, 2); + + connection = g_hash_table_lookup (connections, "eth0"); + g_assert (connection); + nmtst_assert_connection_verifies_without_normalization (connection); + g_assert_cmpstr (nm_connection_get_id (connection), ==, "eth0"); + + s_wired = nm_connection_get_setting_wired (connection); + g_assert (!nm_setting_wired_get_mac_address (s_wired)); + g_assert (s_wired); + + s_ip4 = nm_connection_get_setting_ip4_config (connection); + g_assert (s_ip4); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_AUTO); + g_assert (!nm_setting_ip_config_get_ignore_auto_dns (s_ip4)); + g_assert (!nm_setting_ip_config_get_may_fail (s_ip4)); + + s_ip6 = nm_connection_get_setting_ip6_config (connection); + g_assert (s_ip6); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_DISABLED); + g_assert (!nm_setting_ip_config_get_ignore_auto_dns (s_ip6)); + + connection = g_hash_table_lookup (connections, "bootif_connection"); + g_assert (connection); + nmtst_assert_connection_verifies_without_normalization (connection); + g_assert_cmpstr (nm_connection_get_id (connection), ==, "BOOTIF Connection"); + + s_wired = nm_connection_get_setting_wired (connection); + g_assert_cmpstr (nm_setting_wired_get_mac_address (s_wired), ==, "00:53:AB:CD:02:03"); + g_assert (s_wired); + + s_ip4 = nm_connection_get_setting_ip4_config (connection); + g_assert (s_ip4); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_AUTO); + g_assert (!nm_setting_ip_config_get_ignore_auto_dns (s_ip4)); + g_assert (nm_setting_ip_config_get_may_fail (s_ip4)); + + s_ip6 = nm_connection_get_setting_ip6_config (connection); + g_assert (s_ip6); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_AUTO); + g_assert (!nm_setting_ip_config_get_ignore_auto_dns (s_ip6)); + g_assert (nm_setting_ip_config_get_may_fail (s_ip6)); +} + +static void +test_bootif_off (void) +{ + gs_unref_hashtable GHashTable *connections = NULL; + const char *const*ARGV = NM_MAKE_STRV ("BOOTIF=01-00-53-AB-cd-02-03", "rd.bootif=0"); + + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + g_assert (connections); + g_assert_cmpint (g_hash_table_size (connections), ==, 0); +} NMTST_DEFINE (); @@ -932,7 +1039,7 @@ int main (int argc, char **argv) g_test_add_func ("/initrd/cmdline/if_ip6_manual", test_if_ip6_manual); g_test_add_func ("/initrd/cmdline/multiple", test_multiple); g_test_add_func ("/initrd/cmdline/some_more", test_some_more); - g_test_add_func ("/initrd/cmdline/no_bootif", test_no_bootif); + g_test_add_func ("/initrd/cmdline/bootdev", test_bootdev); g_test_add_func ("/initrd/cmdline/bond", test_bond); g_test_add_func ("/initrd/cmdline/bond/default", test_bond_default); g_test_add_func ("/initrd/cmdline/team", test_team); @@ -942,6 +1049,9 @@ int main (int argc, char **argv) g_test_add_func ("/initrd/cmdline/ignore_extra", test_ignore_extra); g_test_add_func ("/initrd/cmdline/rd_znet", test_rd_znet); g_test_add_func ("/initrd/cmdline/rd_znet/legacy", test_rd_znet_legacy); + g_test_add_func ("/initrd/cmdline/bootif", test_bootif); + g_test_add_func ("/initrd/cmdline/bootif/hwtype", test_bootif_hwtype); + g_test_add_func ("/initrd/cmdline/bootif/off", test_bootif_off); return g_test_run (); } diff --git a/src/initrd/tests/test-dt-reader.c b/src/initrd/tests/test-dt-reader.c new file mode 100644 index 00000000..9a4dc735 --- /dev/null +++ b/src/initrd/tests/test-dt-reader.c @@ -0,0 +1,132 @@ +// SPDX-License-Identifier: LGPL-2.1+ +/* + * Copyright (C) 2014 - 2018 Red Hat, Inc. + */ + +#include "nm-default.h" + +#include <stdio.h> +#include <stdarg.h> +#include <unistd.h> +#include <netinet/ether.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <sys/socket.h> + +#include "nm-core-internal.h" +#include "NetworkManagerUtils.h" + +#include "../nm-initrd-generator.h" + +#include "nm-test-utils-core.h" + +static void +test_read_dt_ofw (void) +{ + NMConnection *connection; + NMSettingConnection *s_con; + NMSettingWired *s_wired; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; + const char *mac_address; + + connection = nmi_dt_reader_parse (TEST_INITRD_DIR "/sysfs-dt"); + g_assert (connection); + nmtst_assert_connection_verifies (connection); + + s_con = nm_connection_get_setting_connection (connection); + g_assert (s_con); + g_assert_cmpstr (nm_setting_connection_get_connection_type (s_con), ==, NM_SETTING_WIRED_SETTING_NAME); + g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "OpenFirmware Connection"); + g_assert_cmpint (nm_setting_connection_get_timestamp (s_con), ==, 0); + g_assert (nm_setting_connection_get_autoconnect (s_con)); + + s_wired = nm_connection_get_setting_wired (connection); + g_assert (s_wired); + mac_address = nm_setting_wired_get_mac_address (s_wired); + g_assert (mac_address); + g_assert (nm_utils_hwaddr_matches (mac_address, -1, "ac:7f:3e:e5:d8:d8", -1)); + g_assert (!nm_setting_wired_get_duplex (s_wired)); + g_assert_cmpint (nm_setting_wired_get_speed (s_wired), ==, 0); + g_assert_cmpint (nm_setting_wired_get_mtu (s_wired), ==, 0); + + s_ip4 = nm_connection_get_setting_ip4_config (connection); + g_assert (s_ip4); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_AUTO); + g_assert_cmpstr (nm_setting_ip_config_get_dhcp_hostname (s_ip4), ==, "demiurge"); + + s_ip6 = nm_connection_get_setting_ip6_config (connection); + g_assert (s_ip6); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_AUTO); + + g_object_unref (connection); +} + +static void +test_read_dt_slof (void) +{ + NMConnection *connection; + NMSettingConnection *s_con; + NMSettingWired *s_wired; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; + NMIPAddress *ip4_addr; + + connection = nmi_dt_reader_parse (TEST_INITRD_DIR "/sysfs-dt-tftp"); + g_assert (connection); + nmtst_assert_connection_verifies (connection); + + s_con = nm_connection_get_setting_connection (connection); + g_assert (s_con); + g_assert_cmpstr (nm_setting_connection_get_connection_type (s_con), ==, NM_SETTING_WIRED_SETTING_NAME); + g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "OpenFirmware Connection"); + g_assert_cmpint (nm_setting_connection_get_timestamp (s_con), ==, 0); + g_assert (nm_setting_connection_get_autoconnect (s_con)); + + s_wired = nm_connection_get_setting_wired (connection); + g_assert (s_wired); + g_assert (!nm_setting_wired_get_mac_address (s_wired)); + g_assert_cmpstr (nm_setting_wired_get_duplex (s_wired), ==, "half"); + g_assert_cmpint (nm_setting_wired_get_speed (s_wired), ==, 10); + g_assert_cmpint (nm_setting_wired_get_mtu (s_wired), ==, 0); + + s_ip4 = nm_connection_get_setting_ip4_config (connection); + g_assert (s_ip4); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_MANUAL); + + g_assert_cmpint (nm_setting_ip_config_get_num_addresses (s_ip4), ==, 1); + ip4_addr = nm_setting_ip_config_get_address (s_ip4, 0); + g_assert (ip4_addr); + g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "192.168.32.2"); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 16); + + g_assert_cmpstr (nm_setting_ip_config_get_gateway (s_ip4), ==, "192.168.32.1"); + + s_ip6 = nm_connection_get_setting_ip6_config (connection); + g_assert (s_ip6); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_DISABLED); + + g_object_unref (connection); +} + +static void +test_read_dt_none (void) +{ + NMConnection *connection; + + connection = nmi_dt_reader_parse (TEST_INITRD_DIR "/sysfs"); + g_assert (!connection); +} + +NMTST_DEFINE (); + +int main (int argc, char **argv) +{ + nmtst_init_assert_logging (&argc, &argv, "INFO", "DEFAULT"); + + g_test_add_func ("/initrd/dt/ofw", test_read_dt_ofw); + g_test_add_func ("/initrd/dt/slof", test_read_dt_slof); + g_test_add_func ("/initrd/dt/none", test_read_dt_none); + + return g_test_run (); +} diff --git a/src/initrd/tests/test-ibft-reader.c b/src/initrd/tests/test-ibft-reader.c index a49d0abf..932c1a48 100644 --- a/src/initrd/tests/test-ibft-reader.c +++ b/src/initrd/tests/test-ibft-reader.c @@ -1,21 +1,6 @@ -/* NetworkManager initrd configuration generator - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301 USA. - * - * Copyright 2014 - 2018 Red Hat, Inc. +// SPDX-License-Identifier: LGPL-2.1+ +/* + * Copyright (C) 2014 - 2018 Red Hat, Inc. */ #include "nm-default.h" |