A Steam Deck LCD (RTL8822CE) on some 5 GHz APs goes to "limited connectivity" during large downloads. The device occasionally delivers a hardware-decrypted frame, with no CRC or ICV error and a well-formed CCMP header, whose PN is far beyond the transmitter's sequence (in the field: the low 16 bits follow the sequence, the upper 32 are garbage). mac80211 stores that PN as the key's replay counter and then drops every genuine frame as a replay until the next rekey. The previous patch only catches frames whose CCMP header is malformed. The device also reports some frames whose CCMP MIC failed as decrypted without error: with a wrong key installed, a few percent of the MPDUs from the 3rd-7th of an A-MPDU on come up with neither the ICV error nor the software-decryption bit set. So a frame's PN cannot be trusted just because the hardware passed it. A transmitter numbers all frames under a key from one PN counter shared by every TID, so a genuine frame can only be ahead of the highest PN received from it by the frames lost in between. Track that PN per transmitter for the pairwise and the group key, starting from mac80211's RX sequence counters when the key is installed, and drop a hardware-decrypted frame that jumps more than 2^24 beyond it. A dropped PN never becomes the reference. To cope with APs that announce a wrong group RSC, a run of jumping PNs that keep increasing in small steps is accepted as the real sequence; garbage PNs, and retransmissions of one, do not form such a run. The dropped frame is just skipped on PCI, USB and SDIO, through a new pkt_stat->drop, as an error return would abort the rest of a USB or SDIO aggregate. rtw89 has a similar workaround for abnormal PNs from some AP firmwares, see commit bda294ed0ed0 ("wifi: rtw89: Drop malformed AMPDU frames with abnormal PN"). Tested on a Steam Deck LCD by corrupting the CCMP header of received frames the way the field's frames are: without this patch one such frame stalls the link; with it the frame is dropped and throughput (~770 Mbit/s, VHT80 2SS) is unaffected, including when the corrupted frame is the first one under a new key. Link: https://lore.kernel.org/linux-wireless/20261016191100.1781056-1-bob.beckett@collabora.com/ Signed-off-by: Robert Beckett --- drivers/net/wireless/realtek/rtw88/mac80211.c | 3 + drivers/net/wireless/realtek/rtw88/main.c | 1 + drivers/net/wireless/realtek/rtw88/main.h | 30 +++++++ drivers/net/wireless/realtek/rtw88/pci.c | 2 +- drivers/net/wireless/realtek/rtw88/rx.c | 123 ++++++++++++++++++++++++++ drivers/net/wireless/realtek/rtw88/rx.h | 4 + drivers/net/wireless/realtek/rtw88/sdio.c | 5 ++ drivers/net/wireless/realtek/rtw88/usb.c | 3 + 8 files changed, 170 insertions(+), 1 deletion(-) diff --git a/drivers/net/wireless/realtek/rtw88/mac80211.c b/drivers/net/wireless/realtek/rtw88/mac80211.c index b14d7fffb98f..496184ed216c 100644 --- a/drivers/net/wireless/realtek/rtw88/mac80211.c +++ b/drivers/net/wireless/realtek/rtw88/mac80211.c @@ -14,6 +14,7 @@ #include "debug.h" #include "wow.h" #include "sar.h" +#include "rx.h" static void rtw_ops_tx(struct ieee80211_hw *hw, struct ieee80211_tx_control *control, @@ -580,6 +581,8 @@ static int rtw_ops_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd, u8 hw_key_idx; int ret = 0; + rtw_rx_pn_guard_set_key(rtwdev, cmd, vif, sta, key); + switch (key->cipher) { case WLAN_CIPHER_SUITE_WEP40: hw_key_type = RTW_CAM_WEP40; diff --git a/drivers/net/wireless/realtek/rtw88/main.c b/drivers/net/wireless/realtek/rtw88/main.c index 19b5c6f55daa..9f080fac55ad 100644 --- a/drivers/net/wireless/realtek/rtw88/main.c +++ b/drivers/net/wireless/realtek/rtw88/main.c @@ -2182,6 +2182,7 @@ int rtw_core_init(struct rtw_dev *rtwdev) skb_queue_head_init(&rtwdev->tx_report.queue); spin_lock_init(&rtwdev->txq_lock); + spin_lock_init(&rtwdev->rx_pn_seed_lock); spin_lock_init(&rtwdev->tx_report.q_lock); spin_lock_init(&rtwdev->hiq_lock); rtwdev->hiq_budget = RTW_TX_HIQ_BUDGET_MAX; diff --git a/drivers/net/wireless/realtek/rtw88/main.h b/drivers/net/wireless/realtek/rtw88/main.h index 1401eff4397c..f05bed08f913 100644 --- a/drivers/net/wireless/realtek/rtw88/main.h +++ b/drivers/net/wireless/realtek/rtw88/main.h @@ -639,6 +639,30 @@ struct rtw_tx_pkt_info { bool bt_null; }; +enum rtw_rx_pn_key { + RTW_RX_PN_PAIRWISE, + RTW_RX_PN_GROUP, + + RTW_RX_PN_KEYS, +}; + +/* highest CCMP PN received per transmitter and key, see rtw_rx_pn_guard() */ +struct rtw_rx_pn_guard { + u64 pn[RTW_RX_PN_KEYS]; + u8 ta[RTW_RX_PN_KEYS][ETH_ALEN]; + bool valid[RTW_RX_PN_KEYS]; + u64 run_pn[RTW_RX_PN_KEYS]; + u8 run_len[RTW_RX_PN_KEYS]; + u32 gen; +}; + +/* starting point for rtw_rx_pn_guard() when a CCMP key is installed */ +struct rtw_rx_pn_seed { + u64 pn; + u8 ta[ETH_ALEN]; + bool valid; +}; + struct rtw_rx_pkt_stat { bool phy_status; bool icv_err; @@ -646,6 +670,7 @@ struct rtw_rx_pkt_stat { bool decrypted; bool is_c2h; bool channel_invalid; + bool drop; /* rejected by the PN guard, don't pass it on */ s32 signal_power; u16 pkt_len; @@ -2074,6 +2099,11 @@ struct rtw_dev { struct rtw_fw_state fw; struct rtw_efuse efuse; struct rtw_sec_desc sec; + struct rtw_rx_pn_guard rx_pn_guard; + atomic_t rx_pn_guard_gen; + struct rtw_rx_pn_seed rx_pn_seed[RTW_RX_PN_KEYS]; + /* protects rx_pn_seed between set_key and the RX path */ + spinlock_t rx_pn_seed_lock; struct rtw_traffic_stats stats; struct rtw_regd regd; struct rtw_bf_info bf_info; diff --git a/drivers/net/wireless/realtek/rtw88/pci.c b/drivers/net/wireless/realtek/rtw88/pci.c index d612a6b3ac5f..9e7c8080770b 100644 --- a/drivers/net/wireless/realtek/rtw88/pci.c +++ b/drivers/net/wireless/realtek/rtw88/pci.c @@ -1091,7 +1091,7 @@ static u32 rtw_pci_rx_napi(struct rtw_dev *rtwdev, struct rtw_pci *rtwpci, rx_desc = skb->data; ret = rtw_rx_query_rx_desc(rtwdev, rx_desc, &pkt_stat, &rx_status); - if (ret) + if (ret || pkt_stat.drop) goto next_rp; /* offset from rx_desc to payload */ diff --git a/drivers/net/wireless/realtek/rtw88/rx.c b/drivers/net/wireless/realtek/rtw88/rx.c index 45ff18fa8c7b..7fbb110ca1ec 100644 --- a/drivers/net/wireless/realtek/rtw88/rx.c +++ b/drivers/net/wireless/realtek/rtw88/rx.c @@ -2,6 +2,8 @@ /* Copyright(c) 2018-2019 Realtek Corporation */ +#include + #include "main.h" #include "mac.h" #include "rx.h" @@ -282,6 +284,124 @@ static bool rtw_rx_ccmp_hdr_valid(struct rtw_rx_pkt_stat *pkt_stat, return iv[2] == 0 && (iv[3] & BIT(5)); } +static u64 rtw_rx_ccmp_pn(const u8 *iv) +{ + /* PN0-PN1, then the reserved and key ID octets, then PN2-PN5 */ + return get_unaligned_le16(iv) | (u64)get_unaligned_le32(iv + 4) << 16; +} + +#define RTW_RX_PN_GUARD_JUMP BIT_ULL(24) +#define RTW_RX_PN_GUARD_RUN 8 +#define RTW_RX_PN_GUARD_STEP BIT_ULL(16) + +void rtw_rx_pn_guard_set_key(struct rtw_dev *rtwdev, enum set_key_cmd cmd, + struct ieee80211_vif *vif, + struct ieee80211_sta *sta, + struct ieee80211_key_conf *key) +{ + int idx = key->flags & IEEE80211_KEY_FLAG_PAIRWISE ? + RTW_RX_PN_PAIRWISE : RTW_RX_PN_GROUP; + struct rtw_rx_pn_seed *seed = &rtwdev->rx_pn_seed[idx]; + struct ieee80211_key_seq seq; + u64 pn = 0; + int tid; + + if (key->cipher != WLAN_CIPHER_SUITE_CCMP) + return; + + if (cmd == SET_KEY) { + for (tid = 0; tid < IEEE80211_NUM_TIDS; tid++) { + ieee80211_get_key_rx_seq(key, tid, &seq); + pn = max(pn, get_unaligned_be48(seq.ccmp.pn)); + } + } + + guard(spinlock_bh)(&rtwdev->rx_pn_seed_lock); + if (cmd == SET_KEY) { + seed->pn = pn; + ether_addr_copy(seed->ta, sta ? sta->addr : vif->bss_conf.bssid); + } + seed->valid = cmd == SET_KEY; + atomic_inc(&rtwdev->rx_pn_guard_gen); +} + +static void rtw_rx_pn_guard_load(struct rtw_dev *rtwdev, + struct rtw_rx_pn_guard *pg) +{ + int i; + + guard(spinlock_bh)(&rtwdev->rx_pn_seed_lock); + for (i = 0; i < RTW_RX_PN_KEYS; i++) { + pg->pn[i] = rtwdev->rx_pn_seed[i].pn; + ether_addr_copy(pg->ta[i], rtwdev->rx_pn_seed[i].ta); + pg->valid[i] = rtwdev->rx_pn_seed[i].valid; + pg->run_len[i] = 0; + } +} + +/* + * A run of jumping PNs that keep increasing in small steps is the + * transmitter's real sequence, e.g. after an AP announced a wrong group RSC. + * Garbage PNs do not form one, nor do retransmissions, which repeat a PN. + */ +static bool rtw_rx_pn_guard_run(struct rtw_rx_pn_guard *pg, int idx, u64 pn) +{ + if (pg->run_len[idx] && pn > pg->run_pn[idx] && + pn - pg->run_pn[idx] <= RTW_RX_PN_GUARD_STEP) { + pg->run_pn[idx] = pn; + if (++pg->run_len[idx] < RTW_RX_PN_GUARD_RUN) + return false; + pg->run_len[idx] = 0; + return true; + } + + if (!pg->run_len[idx] || pn != pg->run_pn[idx]) { + pg->run_pn[idx] = pn; + pg->run_len[idx] = 1; + } + return false; +} + +static void rtw_rx_pn_guard(struct rtw_dev *rtwdev, + struct rtw_rx_pkt_stat *pkt_stat, + struct ieee80211_hdr *hdr) +{ + struct rtw_rx_pn_guard *pg = &rtwdev->rx_pn_guard; + u32 gen = atomic_read(&rtwdev->rx_pn_guard_gen); + int idx = is_multicast_ether_addr(hdr->addr1) ? + RTW_RX_PN_GROUP : RTW_RX_PN_PAIRWISE; + u64 pn, max; + + if (pg->gen != gen) { + pg->gen = gen; + rtw_rx_pn_guard_load(rtwdev, pg); + } + + if (!ieee80211_is_data(hdr->frame_control) || + !ieee80211_has_protected(hdr->frame_control)) + return; + + if (!pg->valid[idx] || !ether_addr_equal(pg->ta[idx], hdr->addr2)) + return; + + /* rtw_rx_ccmp_hdr_valid() checked that the CCMP header is there */ + pn = rtw_rx_ccmp_pn((u8 *)hdr + ieee80211_hdrlen(hdr->frame_control)); + max = pg->pn[idx]; + if (pn <= max) + return; + + if (pn - max > RTW_RX_PN_GUARD_JUMP && + !rtw_rx_pn_guard_run(pg, idx, pn)) { + pkt_stat->drop = true; + dev_warn_ratelimited(rtwdev->dev, + "dropping frame from %pM with CCMP PN 0x%012llx, expected about 0x%012llx\n", + hdr->addr2, pn, max); + return; + } + + pg->pn[idx] = pn; +} + int rtw_rx_query_rx_desc(struct rtw_dev *rtwdev, void *rx_desc8, struct rtw_rx_pkt_stat *pkt_stat, struct ieee80211_rx_status *rx_status) @@ -354,6 +474,9 @@ int rtw_rx_query_rx_desc(struct rtw_dev *rtwdev, void *rx_desc8, pkt_stat->decrypted = false; } + if (pkt_stat->decrypted && enc_type == RX_DESC_ENC_AES) + rtw_rx_pn_guard(rtwdev, pkt_stat, hdr); + rtw_rx_fill_rx_status(rtwdev, pkt_stat, hdr, rx_status); return 0; diff --git a/drivers/net/wireless/realtek/rtw88/rx.h b/drivers/net/wireless/realtek/rtw88/rx.h index 74359f641c76..cb574cf6a23b 100644 --- a/drivers/net/wireless/realtek/rtw88/rx.h +++ b/drivers/net/wireless/realtek/rtw88/rx.h @@ -48,6 +48,10 @@ void rtw_rx_stats(struct rtw_dev *rtwdev, struct ieee80211_vif *vif, int rtw_rx_query_rx_desc(struct rtw_dev *rtwdev, void *rx_desc8, struct rtw_rx_pkt_stat *pkt_stat, struct ieee80211_rx_status *rx_status); +void rtw_rx_pn_guard_set_key(struct rtw_dev *rtwdev, enum set_key_cmd cmd, + struct ieee80211_vif *vif, + struct ieee80211_sta *sta, + struct ieee80211_key_conf *key); void rtw_update_rx_freq_from_ie(struct rtw_dev *rtwdev, struct sk_buff *skb, struct ieee80211_rx_status *rx_status, struct rtw_rx_pkt_stat *pkt_stat); diff --git a/drivers/net/wireless/realtek/rtw88/sdio.c b/drivers/net/wireless/realtek/rtw88/sdio.c index 82d7de41b172..f614c6544bfb 100644 --- a/drivers/net/wireless/realtek/rtw88/sdio.c +++ b/drivers/net/wireless/realtek/rtw88/sdio.c @@ -1387,6 +1387,11 @@ static void rtw_sdio_rx_skb(struct rtw_dev *rtwdev, struct sk_buff *skb, u32 pkt_offset, struct rtw_rx_pkt_stat *pkt_stat, struct ieee80211_rx_status *rx_status) { + if (pkt_stat->drop) { + dev_kfree_skb_any(skb); + return; + } + if (pkt_stat->is_c2h) { skb_put(skb, pkt_stat->pkt_len + pkt_offset); rtw_fw_c2h_cmd_rx_irqsafe(rtwdev, pkt_offset, skb); diff --git a/drivers/net/wireless/realtek/rtw88/usb.c b/drivers/net/wireless/realtek/rtw88/usb.c index ef2a0686b7cf..00334530040c 100644 --- a/drivers/net/wireless/realtek/rtw88/usb.c +++ b/drivers/net/wireless/realtek/rtw88/usb.c @@ -725,6 +725,9 @@ static void rtw_usb_rx_handler(struct work_struct *work) break; } + if (pkt_stat.drop) + goto skip_packet; + skb = alloc_skb(skb_len, GFP_ATOMIC); if (!skb) { rtw_dbg(rtwdev, RTW_DBG_USB, -- 2.55.0