diff --git a/cli/src/connections.c b/cli/src/connections.c index b01fcbdae..a7b41e58a 100644 --- a/cli/src/connections.c +++ b/cli/src/connections.c @@ -217,9 +217,9 @@ usage_connection_add (void) " OPTIONS := COMMON_OPTIONS TYPE_SPECIFIC_OPTIONS IP_OPTIONS\n\n" " COMMON_OPTIONS:\n" " type \n" + " ifname | \"*\"\n" " [con-name ]\n" - " [autoconnect yes|no]\n" - " [ifname ]\n\n" + " [autoconnect yes|no]\n\n" " TYPE_SPECIFIC_OPTIONS:\n" " ethernet: [mac ]\n" " [cloned-mac ]\n" @@ -2925,6 +2925,8 @@ do_connection_add (NmCli *nmc, int argc, char **argv) const char *autoconnect = NULL; gboolean auto_bool = TRUE; const char *ifname = NULL; + char *ifname_ask = NULL; + gboolean ifname_mandatory = TRUE; AddConnectionInfo *info = NULL; const char *setting_name; GError *error = NULL; @@ -2972,13 +2974,31 @@ do_connection_add (NmCli *nmc, int argc, char **argv) goto error; } } + + /* ifname is mandatory for all connection types except virtual ones (bond, bridge, vlan) */ + if ( strcmp (type, NM_SETTING_BOND_SETTING_NAME) == 0 + || strcmp (type, NM_SETTING_BRIDGE_SETTING_NAME) == 0 + || strcmp (type, NM_SETTING_VLAN_SETTING_NAME) == 0) + ifname_mandatory = FALSE; + + if (!ifname && ifname_mandatory && nmc->ask) + ifname = ifname_ask = nmc_get_user_input (_("Interface name: ")); + if (!ifname && ifname_mandatory) { + g_string_printf (nmc->return_text, _("Error: 'ifname' argument is required.")); + nmc->return_value = NMC_RESULT_ERROR_USER_INPUT; + goto error; + } if (ifname) { - if (!nm_utils_iface_valid_name (ifname)) { + if (!nm_utils_iface_valid_name (ifname) && strcmp (ifname, "*") != 0) { g_string_printf (nmc->return_text, - _("Error: 'ifname': '%s' is not a valid interface."), + _("Error: 'ifname': '%s' is not a valid interface nor '*'."), ifname); + nmc->return_value = NMC_RESULT_ERROR_USER_INPUT; goto error; } + /* Special value of '*' means no specific interface name */ + if (strcmp (ifname, "*") == 0) + ifname = NULL; } /* Create a new connection object */ @@ -3041,6 +3061,7 @@ error: if (connection) g_object_unref (connection); g_free (type_ask); + g_free (ifname_ask); nmc->should_wait = FALSE; return nmc->return_value; diff --git a/man/nmcli.1.in b/man/nmcli.1.in index f40895f31..77675d3c6 100644 --- a/man/nmcli.1.in +++ b/man/nmcli.1.in @@ -325,12 +325,15 @@ Add a connection for NetworkManager. Arguments differ according to connection ty .B COMMON_OPTIONS: .IP "\fItype \fP" 42 \(en connection type; see bellow \fBTYPE_SPECIFIC_OPTIONS\fP for allowed values; (mandatory) +.IP "\fIifname | \(dq\&*\(dq\&\fP" 42 +\(en interface to bind the connection to. The connection will only be applicable to this +interface name. A special value of "\fB*\fP" can be used for interface-independent connections. +The \fIifname\fP argument is mandatory for all connection types except bond, bridge and vlan. +Note: use quotes around \fB*\fP to suppress shell expansion. .IP "\fI[con-name ]\fP" 42 \(en connection name (when not provided a default name is generated: [-][-]) .IP "\fI[autoconnect yes|no]\fP" 42 \(en whether the connection can auto-connect or not -.IP "\fI[ifname ]\fP" 42 -\(en interface to bind the connection to .RE .RS .TP