ifcfg-rh: handle missing PREFIX by generating one

Based on what the initscripts do with ipcalc, which was tripping
a few people up when they forgot to do PREFIX/NETMASK.
This commit is contained in:
Dan Williams
2010-01-20 16:34:23 -08:00
parent 79db492854
commit 14aef3678a
6 changed files with 177 additions and 3 deletions

View File

@@ -604,9 +604,28 @@ read_full_ip4_address (shvarFile *ifcfg,
nm_ip4_address_set_prefix (addr, nm_utils_ip4_netmask_to_prefix (tmp));
}
/* Try to autodetermine the prefix for the address' class */
if (!nm_ip4_address_get_prefix (addr)) {
guint32 tmp_addr, prefix = 0;
tmp_addr = nm_ip4_address_get_address (addr);
if (((ntohl(tmp_addr) & 0xFF000000) >> 24) <= 127)
prefix = 8;
else if (((ntohl(tmp_addr) & 0xFF000000) >> 24) <= 191)
prefix = 16;
else
prefix = 24;
nm_ip4_address_set_prefix (addr, prefix);
value = svGetValue (ifcfg, ip_tag, FALSE);
PLUGIN_WARN (IFCFG_PLUGIN_NAME, " warning: missing %s, assuming %s/%u",
prefix_tag, value, prefix);
g_free (value);
}
/* Validate the prefix */
if ( !nm_ip4_address_get_prefix (addr)
|| nm_ip4_address_get_prefix (addr) > 32) {
if (nm_ip4_address_get_prefix (addr) > 32) {
g_set_error (error, ifcfg_plugin_error_quark (), 0,
"Missing or invalid IP4 prefix '%d'",
nm_ip4_address_get_prefix (addr));

View File

@@ -54,7 +54,10 @@ EXTRA_DIST = \
ifcfg-test-wired-static-routes-legacy \
route-test-wired-static-routes-legacy \
ifcfg-test-wired-ipv6-manual \
route6-test-wired-ipv6-manual
route6-test-wired-ipv6-manual \
ifcfg-test-wired-static-no-prefix-8 \
ifcfg-test-wired-static-no-prefix-16 \
ifcfg-test-wired-static-no-prefix-24
check-local:
@for f in $(EXTRA_DIST); do \

View File

@@ -0,0 +1,14 @@
# Intel Corporation 82540EP Gigabit Ethernet Controller (Mobile)
TYPE=Ethernet
DEVICE=eth0
HWADDR=00:11:22:33:44:ee
BOOTPROTO=none
ONBOOT=yes
USERCTL=yes
MTU=1492
NM_CONTROLLED=yes
DNS1=4.2.2.1
DNS2=4.2.2.2
IPADDR=172.16.3.4
GATEWAY=172.16.3.1

View File

@@ -0,0 +1,14 @@
# Intel Corporation 82540EP Gigabit Ethernet Controller (Mobile)
TYPE=Ethernet
DEVICE=eth0
HWADDR=00:11:22:33:44:ee
BOOTPROTO=none
ONBOOT=yes
USERCTL=yes
MTU=1492
NM_CONTROLLED=yes
DNS1=4.2.2.1
DNS2=4.2.2.2
IPADDR=192.168.1.5
GATEWAY=192.168.1.1

View File

@@ -0,0 +1,14 @@
# Intel Corporation 82540EP Gigabit Ethernet Controller (Mobile)
TYPE=Ethernet
DEVICE=eth0
HWADDR=00:11:22:33:44:ee
BOOTPROTO=none
ONBOOT=yes
USERCTL=yes
MTU=1492
NM_CONTROLLED=yes
DNS1=4.2.2.1
DNS2=4.2.2.2
IPADDR=10.11.12.13
GATEWAY=10.0.0.1

View File

@@ -717,6 +717,113 @@ test_read_wired_static (const char *file, const char *expected_id)
g_object_unref (connection);
}
#define TEST_IFCFG_STATIC_NO_PREFIX TEST_IFCFG_DIR"/network-scripts/ifcfg-test-wired-static-no-prefix"
static void
test_read_wired_static_no_prefix (guint32 expected_prefix)
{
NMConnection *connection;
NMSettingConnection *s_con;
NMSettingIP4Config *s_ip4;
char *unmanaged = FALSE;
char *keyfile = NULL;
char *routefile = NULL;
char *route6file = NULL;
gboolean ignore_error = FALSE;
GError *error = NULL;
NMIP4Address *ip4_addr;
char *file, *expected_id;
const char *tmp;
file = g_strdup_printf (TEST_IFCFG_STATIC_NO_PREFIX "-%u", expected_prefix);
ASSERT (file != NULL,
"wired-static-no-prefix-read", "failed to create path to file");
expected_id = g_strdup_printf ("System test-wired-static-no-prefix-%u", expected_prefix);
ASSERT (expected_id != NULL,
"wired-static-no-prefix-read", "failed to expected connection ID");
connection = connection_from_file (file,
NULL,
TYPE_ETHERNET,
NULL,
&unmanaged,
&keyfile,
&routefile,
&route6file,
&error,
&ignore_error);
ASSERT (connection != NULL,
"wired-static-no-prefix-read", "failed to read %s: %s", file, error->message);
ASSERT (nm_connection_verify (connection, &error),
"wired-static-no-prefix-verify", "failed to verify %s: %s", file, error->message);
ASSERT (unmanaged == FALSE,
"wired-static-no-prefix-verify", "failed to verify %s: unexpected unmanaged value", file);
/* ===== CONNECTION SETTING ===== */
s_con = NM_SETTING_CONNECTION (nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION));
ASSERT (s_con != NULL,
"wired-static-no-prefix-verify-connection", "failed to verify %s: missing %s setting",
file,
NM_SETTING_CONNECTION_SETTING_NAME);
/* ID */
tmp = nm_setting_connection_get_id (s_con);
ASSERT (tmp != NULL,
"wired-static-no-prefix-verify-connection", "failed to verify %s: missing %s / %s key",
file,
NM_SETTING_CONNECTION_SETTING_NAME,
NM_SETTING_CONNECTION_ID);
ASSERT (strcmp (tmp, expected_id) == 0,
"wired-static-no-prefix-verify-connection", "failed to verify %s: unexpected %s / %s key value",
file,
NM_SETTING_CONNECTION_SETTING_NAME,
NM_SETTING_CONNECTION_ID);
g_free (expected_id);
/* ===== IPv4 SETTING ===== */
s_ip4 = NM_SETTING_IP4_CONFIG (nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG));
ASSERT (s_ip4 != NULL,
"wired-static-no-prefix-verify-ip4", "failed to verify %s: missing %s setting",
file,
NM_SETTING_IP4_CONFIG_SETTING_NAME);
/* Method */
tmp = nm_setting_ip4_config_get_method (s_ip4);
ASSERT (strcmp (tmp, NM_SETTING_IP4_CONFIG_METHOD_MANUAL) == 0,
"wired-static-no-prefix-verify-ip4", "failed to verify %s: unexpected %s / %s key value",
file,
NM_SETTING_IP4_CONFIG_SETTING_NAME,
NM_SETTING_IP4_CONFIG_METHOD);
ASSERT (nm_setting_ip4_config_get_num_addresses (s_ip4) == 1,
"wired-static-no-prefix-verify-ip4", "failed to verify %s: unexpected %s / %s key value",
file,
NM_SETTING_IP4_CONFIG_SETTING_NAME,
NM_SETTING_IP4_CONFIG_DNS);
/* Address #1 */
ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0);
ASSERT (ip4_addr,
"wired-static-no-prefix-verify-ip4", "failed to verify %s: missing IP4 address #1",
file,
NM_SETTING_IP4_CONFIG_SETTING_NAME,
NM_SETTING_IP4_CONFIG_ADDRESSES);
ASSERT (nm_ip4_address_get_prefix (ip4_addr) == expected_prefix,
"wired-static-no-prefix-verify-ip4", "failed to verify %s: unexpected IP4 address #1 prefix",
file,
NM_SETTING_IP4_CONFIG_SETTING_NAME,
NM_SETTING_IP4_CONFIG_ADDRESSES);
g_free (file);
g_object_unref (connection);
}
#define TEST_IFCFG_WIRED_DHCP TEST_IFCFG_DIR"/network-scripts/ifcfg-test-wired-dhcp"
static void
@@ -7695,6 +7802,9 @@ int main (int argc, char **argv)
test_read_minimal ();
test_read_wired_static (TEST_IFCFG_WIRED_STATIC, "System test-wired-static");
test_read_wired_static (TEST_IFCFG_WIRED_STATIC_BOOTPROTO, "System test-wired-static-bootproto");
test_read_wired_static_no_prefix (8);
test_read_wired_static_no_prefix (16);
test_read_wired_static_no_prefix (24);
test_read_wired_dhcp ();
test_read_wired_global_gateway ();
test_read_wired_never_default ();