Skip to content

Commit c9b8573

Browse files
committed
functestlib: make nmcli Wi-Fi connect robust with key-mgmt fallback
Some images fail the simple NetworkManager path: nmcli dev wifi connect <SSID> password <PSK> Error: 802-11-wireless-security.key-mgmt: property is missing. Extend wifi_connect_nmcli() to detect this error and automatically switch to an explicit profile flow: - Ensure Wi-Fi is enabled and device is managed. - Disconnect and rescan the SSID, delete any stale connection. - Create a profile with: * WPA2/PSK: wifi-sec.key-mgmt wpa-psk, wifi-sec.psk <pass> * Fallback: wifi-sec.key-mgmt sae (WPA3-Personal) if needed * Open nets: wifi-sec.key-mgmt none (no password) - Bring the connection up and return success on first working method. Signed-off-by: Srikanth Muppandam <[email protected]>
1 parent 8c93e63 commit c9b8573

File tree

1 file changed

+71
-6
lines changed

1 file changed

+71
-6
lines changed

Runner/utils/functestlib.sh

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,17 +1594,82 @@ retry_command() {
15941594
return 1
15951595
}
15961596

1597-
# Connect using nmcli with retries (returns 0 on success)
1597+
# Connect to Wi-Fi using nmcli, with fallback when key-mgmt is required
15981598
wifi_connect_nmcli() {
15991599
iface="$1"
16001600
ssid="$2"
16011601
pass="$3"
1602-
if command -v nmcli >/dev/null 2>&1; then
1603-
log_info "Trying to connect using nmcli..."
1604-
retry_command "nmcli dev wifi connect \"$ssid\" password \"$pass\" ifname \"$iface\" 2>&1 | tee nmcli.log" 3 3
1605-
return $?
1602+
1603+
if ! command -v nmcli >/dev/null 2>&1; then
1604+
return 1
16061605
fi
1607-
return 1
1606+
1607+
log_info "Trying to connect using nmcli..."
1608+
mkdir -p "${LOG_DIR:-.}" 2>/dev/null || true
1609+
nm_log="${LOG_DIR:-.}/nmcli_${iface}_$(printf '%s' "$ssid" | tr ' /' '__').log"
1610+
1611+
# First try the simple connect path (what you already had)
1612+
if [ -n "$pass" ]; then
1613+
retry_command "nmcli dev wifi connect \"$ssid\" password \"$pass\" ifname \"$iface\" 2>&1 | tee \"$nm_log\"" 3 3
1614+
else
1615+
retry_command "nmcli dev wifi connect \"$ssid\" ifname \"$iface\" 2>&1 | tee \"$nm_log\"" 3 3
1616+
fi
1617+
rc=$?
1618+
[ $rc -eq 0 ] && return 0
1619+
1620+
# Look for the specific error and fall back to creating a connection profile
1621+
if grep -qi '802-11-wireless-security\.key-mgmt.*missing' "$nm_log"; then
1622+
log_warn "nmcli connect complained about missing key-mgmt; creating an explicit connection profile..."
1623+
1624+
nmcli -t -f WIFI nm status >/dev/null 2>&1 || nmcli r wifi on >/dev/null 2>&1 || true
1625+
nmcli dev set "$iface" managed yes >/dev/null 2>&1 || true
1626+
nmcli dev disconnect "$iface" >/dev/null 2>&1 || true
1627+
nmcli dev wifi rescan >/dev/null 2>&1 || true
1628+
1629+
con_name="$ssid"
1630+
# If a connection with the same name exists, drop it to avoid conflicts
1631+
if nmcli -t -f NAME con show 2>/dev/null | grep -Fxq "$con_name"; then
1632+
nmcli con delete "$con_name" >/dev/null 2>&1 || true
1633+
fi
1634+
1635+
if [ -n "$pass" ]; then
1636+
# Try WPA2 PSK first (most common)
1637+
if nmcli con add type wifi ifname "$iface" con-name "$con_name" ssid "$ssid" \
1638+
wifi-sec.key-mgmt wpa-psk wifi-sec.psk "$pass" >>"$nm_log" 2>&1; then
1639+
if nmcli con up "$con_name" ifname "$iface" >>"$nm_log" 2>&1; then
1640+
log_pass "Connected to $ssid via explicit profile (wpa-psk)."
1641+
return 0
1642+
fi
1643+
fi
1644+
1645+
# If that failed, try WPA3-Personal (SAE), some APs require it
1646+
log_warn "Profile up failed; trying WPA3 (sae) profile..."
1647+
nmcli con delete "$con_name" >/dev/null 2>&1 || true
1648+
if nmcli con add type wifi ifname "$iface" con-name "$con_name" ssid "$ssid" \
1649+
wifi-sec.key-mgmt sae wifi-sec.psk "$pass" >>"$nm_log" 2>&1; then
1650+
if nmcli con up "$con_name" ifname "$iface" >>"$nm_log" 2>&1; then
1651+
log_pass "Connected to $ssid via explicit profile (sae)."
1652+
return 0
1653+
fi
1654+
fi
1655+
else
1656+
# Open network (no passphrase)
1657+
if nmcli con add type wifi ifname "$iface" con-name "$con_name" ssid "$ssid" \
1658+
wifi-sec.key-mgmt none >>"$nm_log" 2>&1; then
1659+
if nmcli con up "$con_name" ifname "$iface" >>"$nm_log" 2>&1; then
1660+
log_pass "Connected to open network $ssid."
1661+
return 0
1662+
fi
1663+
fi
1664+
fi
1665+
1666+
log_fail "Failed to connect to $ssid even after explicit key-mgmt profile. See $nm_log"
1667+
return 1
1668+
fi
1669+
1670+
# Different error — just bubble up the original failure
1671+
log_fail "nmcli failed to connect to $ssid. See $nm_log"
1672+
return $rc
16081673
}
16091674

16101675
# Connect using wpa_supplicant+udhcpc with retries (returns 0 on success)

0 commit comments

Comments
 (0)