git: c5b96b3eaede - main - LinuxKPI: 802.11 assign an(y) early chandef

From: Bjoern A. Zeeb <bz_at_FreeBSD.org>
Date: Wed, 16 Feb 2022 03:51:20 UTC
The branch main has been updated by bz:

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

commit c5b96b3eaede01a5117975af6de3483dc43673a4
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-16 03:48:54 +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.
    
    MFC after:      3 days
---
 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 1bed7de0c454..5267cbf385c0 100644
--- a/sys/compat/linuxkpi/common/src/linux_80211.c
+++ b/sys/compat/linuxkpi/common/src/linux_80211.c
@@ -2830,9 +2830,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;
@@ -2936,6 +2934,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);