git: 8bc13d98db68 - stable/13 - LinuxKPI: 802.11 assign an(y) early chandef

From: Bjoern A. Zeeb <bz_at_FreeBSD.org>
Date: Sun, 20 Feb 2022 18:15:47 UTC
The branch stable/13 has been updated by bz:

URL: https://cgit.FreeBSD.org/src/commit/?id=8bc13d98db68862df459dd00cb73a64cf776b21c

commit 8bc13d98db68862df459dd00cb73a64cf776b21c
Author:     Bjoern A. Zeeb <bz@FreeBSD.org>
AuthorDate: 2022-02-16 03:48:54 +0000
Commit:     Bjoern A. Zeeb <bz@FreeBSD.org>
CommitDate: 2022-02-20 16:25:25 +0000

    LinuxKPI: 802.11 assign an(y) early chandef
    
    The Realtek driver assumes an early chandef to be set.  At the time
    of linuxkpi_ieee80211_ifattach() we do not really know one yet so
    try to find the first one which is available and set that.
    This prevents a NULL-deref panic.
    
    (cherry picked from commit c5b96b3eaede01a5117975af6de3483dc43673a4)
---
 sys/compat/linuxkpi/common/src/linux_80211.c | 32 +++++++++++++++++++++++++---
 1 file changed, 29 insertions(+), 3 deletions(-)

diff --git a/sys/compat/linuxkpi/common/src/linux_80211.c b/sys/compat/linuxkpi/common/src/linux_80211.c
index 473c98317694..fb113d770678 100644
--- a/sys/compat/linuxkpi/common/src/linux_80211.c
+++ b/sys/compat/linuxkpi/common/src/linux_80211.c
@@ -2818,9 +2818,7 @@ linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
 {
 	struct ieee80211com *ic;
 	struct lkpi_hw *lhw;
-#ifdef TRY_HW_CRYPTO
-	int i;
-#endif
+	int band, i;
 
 	lhw = HW_TO_LHW(hw);
 	ic = lhw->ic;
@@ -2919,6 +2917,34 @@ linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
 
 	lkpi_radiotap_attach(lhw);
 
+	/*
+	 * Assign the first possible channel for now;  seems Realtek drivers
+	 * expect one.
+	 */
+	for (band = 0; band < NUM_NL80211_BANDS &&
+	    hw->conf.chandef.chan == NULL; band++) {
+		struct ieee80211_supported_band *supband;
+		struct linuxkpi_ieee80211_channel *channels;
+
+		supband = hw->wiphy->bands[band];
+		if (supband == NULL || supband->n_channels == 0)
+			continue;
+
+		channels = supband->channels;
+		for (i = 0; i < supband->n_channels; i++) {
+			struct cfg80211_chan_def chandef;
+
+			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
+				continue;
+
+			memset(&chandef, 0, sizeof(chandef));
+			cfg80211_chandef_create(&chandef, &channels[i],
+			    NL80211_CHAN_NO_HT);
+			hw->conf.chandef = chandef;
+			break;
+		}
+	}
+
 	if (bootverbose)
 		ieee80211_announce(ic);
 }