linux-wireless.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 1/2] cfg80211: add helper fn for single rule channels
@ 2020-07-01 10:15 Markus Theil
  2020-07-01 10:15 ` [PATCH v2 2/2] cfg80211: add helper fn for adjacent " Markus Theil
  2020-07-30 11:27 ` [PATCH v2 1/2] cfg80211: add helper fn for single " Johannes Berg
  0 siblings, 2 replies; 3+ messages in thread
From: Markus Theil @ 2020-07-01 10:15 UTC (permalink / raw)
  To: johannes; +Cc: linux-wireless, Markus Theil

As a preparation to handle adjacent rule channels,
factor out handling channels located in a single
regulatory rule.

Signed-off-by: Markus Theil <markus.theil@tu-ilmenau.de>
---
 net/wireless/reg.c | 107 +++++++++++++++++++++++++--------------------
 1 file changed, 60 insertions(+), 47 deletions(-)

diff --git a/net/wireless/reg.c b/net/wireless/reg.c
index 0d74a31ef0ab..10c76f27f6e1 100644
--- a/net/wireless/reg.c
+++ b/net/wireless/reg.c
@@ -1691,57 +1691,18 @@ static uint32_t reg_rule_to_chan_bw_flags(const struct ieee80211_regdomain *regd
 	return bw_flags;
 }
 
-/*
- * Note that right now we assume the desired channel bandwidth
- * is always 20 MHz for each individual channel (HT40 uses 20 MHz
- * per channel, the primary and the extension channel).
- */
-static void handle_channel(struct wiphy *wiphy,
-			   enum nl80211_reg_initiator initiator,
-			   struct ieee80211_channel *chan)
+static void handle_channel_single_rule(struct wiphy *wiphy,
+				       enum nl80211_reg_initiator initiator,
+				       struct ieee80211_channel *chan,
+				       u32 flags,
+				       struct regulatory_request *lr,
+				       struct wiphy *request_wiphy,
+				       const struct ieee80211_reg_rule *reg_rule)
 {
-	u32 flags, bw_flags = 0;
-	const struct ieee80211_reg_rule *reg_rule = NULL;
+	u32 bw_flags = 0;
 	const struct ieee80211_power_rule *power_rule = NULL;
-	struct wiphy *request_wiphy = NULL;
-	struct regulatory_request *lr = get_last_request();
 	const struct ieee80211_regdomain *regd;
 
-	request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
-
-	flags = chan->orig_flags;
-
-	reg_rule = freq_reg_info(wiphy, ieee80211_channel_to_khz(chan));
-	if (IS_ERR(reg_rule)) {
-		/*
-		 * We will disable all channels that do not match our
-		 * received regulatory rule unless the hint is coming
-		 * from a Country IE and the Country IE had no information
-		 * about a band. The IEEE 802.11 spec allows for an AP
-		 * to send only a subset of the regulatory rules allowed,
-		 * so an AP in the US that only supports 2.4 GHz may only send
-		 * a country IE with information for the 2.4 GHz band
-		 * while 5 GHz is still supported.
-		 */
-		if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
-		    PTR_ERR(reg_rule) == -ERANGE)
-			return;
-
-		if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
-		    request_wiphy && request_wiphy == wiphy &&
-		    request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
-			pr_debug("Disabling freq %d.%03d MHz for good\n",
-				 chan->center_freq, chan->freq_offset);
-			chan->orig_flags |= IEEE80211_CHAN_DISABLED;
-			chan->flags = chan->orig_flags;
-		} else {
-			pr_debug("Disabling freq %d.%03d MHz\n",
-				 chan->center_freq, chan->freq_offset);
-			chan->flags |= IEEE80211_CHAN_DISABLED;
-		}
-		return;
-	}
-
 	regd = reg_get_regdomain(wiphy);
 
 	power_rule = &reg_rule->power_rule;
@@ -1803,6 +1764,58 @@ static void handle_channel(struct wiphy *wiphy,
 		chan->max_power = chan->max_reg_power;
 }
 
+/*
+ * Note that right now we assume the desired channel bandwidth
+ * is always 20 MHz for each individual channel (HT40 uses 20 MHz
+ * per channel, the primary and the extension channel).
+ */
+static void handle_channel(struct wiphy *wiphy,
+			   enum nl80211_reg_initiator initiator,
+			   struct ieee80211_channel *chan)
+{
+	u32 flags = 0;
+	const struct ieee80211_reg_rule *reg_rule = NULL;
+	struct wiphy *request_wiphy = NULL;
+	struct regulatory_request *lr = get_last_request();
+
+	request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
+
+	flags = chan->orig_flags;
+
+	reg_rule = freq_reg_info(wiphy, ieee80211_channel_to_khz(chan));
+	if (IS_ERR(reg_rule)) {
+		/*
+		 * We will disable all channels that do not match our
+		 * received regulatory rule unless the hint is coming
+		 * from a Country IE and the Country IE had no information
+		 * about a band. The IEEE 802.11 spec allows for an AP
+		 * to send only a subset of the regulatory rules allowed,
+		 * so an AP in the US that only supports 2.4 GHz may only send
+		 * a country IE with information for the 2.4 GHz band
+		 * while 5 GHz is still supported.
+		 */
+		if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
+		    PTR_ERR(reg_rule) == -ERANGE)
+			return;
+
+		if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
+		    request_wiphy && request_wiphy == wiphy &&
+		    request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
+			pr_debug("Disabling freq %d.%03d MHz for good\n",
+				 chan->center_freq, chan->freq_offset);
+			chan->orig_flags |= IEEE80211_CHAN_DISABLED;
+			chan->flags = chan->orig_flags;
+		} else {
+			pr_debug("Disabling freq %d.%03d MHz\n",
+				 chan->center_freq, chan->freq_offset);
+			chan->flags |= IEEE80211_CHAN_DISABLED;
+		}
+		return;
+	}
+
+	handle_channel_single_rule(wiphy, initiator, chan, flags, lr, request_wiphy, reg_rule);
+}
+
 static void handle_band(struct wiphy *wiphy,
 			enum nl80211_reg_initiator initiator,
 			struct ieee80211_supported_band *sband)
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH v2 2/2] cfg80211: add helper fn for adjacent rule channels
  2020-07-01 10:15 [PATCH v2 1/2] cfg80211: add helper fn for single rule channels Markus Theil
@ 2020-07-01 10:15 ` Markus Theil
  2020-07-30 11:27 ` [PATCH v2 1/2] cfg80211: add helper fn for single " Johannes Berg
  1 sibling, 0 replies; 3+ messages in thread
From: Markus Theil @ 2020-07-01 10:15 UTC (permalink / raw)
  To: johannes; +Cc: linux-wireless, Markus Theil

Some usable channels are located in the union of adjacent
regulatory rules, for example channel 144 in Germany.

Enable them, by also checking if a channel spans two adjacent
regulatory rules/frequency ranges.

All flags involved are disabling things, therefore we can build
the maximum by or-ing them together. Furthermore, take the maximum
of DFS CAC time values and the minimum of allowed power of both
adjacent channels in order to comply with both regulatory rules at
the same time.

Signed-off-by: Markus Theil <markus.theil@tu-ilmenau.de>
---
v2: add check for 10 and 20 MHz flags

 net/wireless/reg.c | 133 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 133 insertions(+)

diff --git a/net/wireless/reg.c b/net/wireless/reg.c
index 10c76f27f6e1..b86a29c76a2b 100644
--- a/net/wireless/reg.c
+++ b/net/wireless/reg.c
@@ -1764,6 +1764,108 @@ static void handle_channel_single_rule(struct wiphy *wiphy,
 		chan->max_power = chan->max_reg_power;
 }
 
+static void handle_channel_adjacent_rules(struct wiphy *wiphy,
+					  enum nl80211_reg_initiator initiator,
+					  struct ieee80211_channel *chan,
+					  u32 flags,
+					  struct regulatory_request *lr,
+					  struct wiphy *request_wiphy,
+					  const struct ieee80211_reg_rule *reg_rule1,
+					  const struct ieee80211_reg_rule *reg_rule2,
+					  struct ieee80211_freq_range *combined_range)
+{
+	u32 bw_flags1 = 0;
+	u32 bw_flags2 = 0;
+	const struct ieee80211_power_rule *power_rule1 = NULL;
+	const struct ieee80211_power_rule *power_rule2 = NULL;
+	const struct ieee80211_regdomain *regd;
+
+	regd = reg_get_regdomain(wiphy);
+
+	power_rule1 = &reg_rule1->power_rule;
+	power_rule2 = &reg_rule2->power_rule;
+	bw_flags1 = reg_rule_to_chan_bw_flags(regd, reg_rule1, chan);
+	bw_flags2 = reg_rule_to_chan_bw_flags(regd, reg_rule2, chan);
+
+	if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
+	    request_wiphy && request_wiphy == wiphy &&
+	    request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
+		/*
+		 * This guarantees the driver's requested regulatory domain
+		 * will always be used as a base for further regulatory
+		 * settings
+		 */
+		chan->flags = chan->orig_flags =
+			map_regdom_flags(reg_rule1->flags) |
+			map_regdom_flags(reg_rule2->flags) |
+			bw_flags1 |
+			bw_flags2;
+		chan->max_antenna_gain = chan->orig_mag =
+			min_t(int, MBI_TO_DBI(power_rule1->max_antenna_gain),
+			      MBI_TO_DBI(power_rule2->max_antenna_gain));
+		chan->max_reg_power = chan->max_power = chan->orig_mpwr =
+			min_t(int, MBM_TO_DBM(power_rule1->max_eirp),
+			      MBM_TO_DBM(power_rule2->max_eirp));
+
+		if (chan->flags & IEEE80211_CHAN_RADAR) {
+			chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
+			if (reg_rule1->dfs_cac_ms || reg_rule2->dfs_cac_ms)
+				chan->dfs_cac_ms = max_t(unsigned int, reg_rule1->dfs_cac_ms, reg_rule2->dfs_cac_ms);
+		}
+
+		return;
+	}
+
+	chan->dfs_state = NL80211_DFS_USABLE;
+	chan->dfs_state_entered = jiffies;
+
+	chan->beacon_found = false;
+	chan->flags = flags | bw_flags1 | bw_flags2 |
+		      map_regdom_flags(reg_rule1->flags) |
+		      map_regdom_flags(reg_rule2->flags);
+
+	/* reg_rule_to_chan_bw_flags may forbids 10 and forbids 20 MHz (otherwise no adj. rule case),
+	   recheck therefore */
+	if (cfg80211_does_bw_fit_range(combined_range,
+				       ieee80211_channel_to_khz(chan),
+				       MHZ_TO_KHZ(10)))
+		chan->flags &= ~IEEE80211_CHAN_NO_10MHZ;
+	if (cfg80211_does_bw_fit_range(combined_range,
+				       ieee80211_channel_to_khz(chan),
+				       MHZ_TO_KHZ(20)))
+		chan->flags &= ~IEEE80211_CHAN_NO_20MHZ;
+
+	chan->max_antenna_gain =
+		min_t(int, chan->orig_mag,
+		      min_t(int,
+			    MBI_TO_DBI(power_rule1->max_antenna_gain),
+			    MBI_TO_DBI(power_rule2->max_antenna_gain)));
+	chan->max_reg_power = min_t(int,
+				    MBM_TO_DBM(power_rule1->max_eirp),
+				    MBM_TO_DBM(power_rule2->max_eirp));
+
+	if (chan->flags & IEEE80211_CHAN_RADAR) {
+		if (reg_rule1->dfs_cac_ms || reg_rule2->dfs_cac_ms)
+			chan->dfs_cac_ms = max_t(unsigned int, reg_rule1->dfs_cac_ms, reg_rule2->dfs_cac_ms);
+		else
+			chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
+	}
+
+	if (chan->orig_mpwr) {
+		/*
+		 * Devices that use REGULATORY_COUNTRY_IE_FOLLOW_POWER
+		 * will always follow the passed country IE power settings.
+		 */
+		if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
+		    wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_FOLLOW_POWER)
+			chan->max_power = chan->max_reg_power;
+		else
+			chan->max_power = min(chan->orig_mpwr,
+					      chan->max_reg_power);
+	} else
+		chan->max_power = chan->max_reg_power;
+}
+
 /*
  * Note that right now we assume the desired channel bandwidth
  * is always 20 MHz for each individual channel (HT40 uses 20 MHz
@@ -1775,6 +1877,8 @@ static void handle_channel(struct wiphy *wiphy,
 {
 	u32 flags = 0;
 	const struct ieee80211_reg_rule *reg_rule = NULL;
+	const struct ieee80211_reg_rule *reg_rule1 = NULL;
+	const struct ieee80211_reg_rule *reg_rule2 = NULL;
 	struct wiphy *request_wiphy = NULL;
 	struct regulatory_request *lr = get_last_request();
 
@@ -1784,6 +1888,35 @@ static void handle_channel(struct wiphy *wiphy,
 
 	reg_rule = freq_reg_info(wiphy, ieee80211_channel_to_khz(chan));
 	if (IS_ERR(reg_rule)) {
+		/*
+		 * check for adjacent match, therefore get rules for chan - 20 MHz
+		 * and chan + 20 MHz and test if reg rules are adjacent
+		 */
+		reg_rule1 = freq_reg_info(wiphy, ieee80211_channel_to_khz(chan) - MHZ_TO_KHZ(20));
+		reg_rule2 = freq_reg_info(wiphy, ieee80211_channel_to_khz(chan) + MHZ_TO_KHZ(20));
+		if (!IS_ERR(reg_rule1) && !IS_ERR(reg_rule2)) {
+			struct ieee80211_freq_range combined_range;
+
+			if (reg_rule1->freq_range.end_freq_khz !=
+			    reg_rule2->freq_range.start_freq_khz)
+				goto disable_chan;
+
+			combined_range.start_freq_khz = reg_rule1->freq_range.start_freq_khz;
+			combined_range.end_freq_khz = reg_rule2->freq_range.end_freq_khz;
+			combined_range.max_bandwidth_khz = min_t(u32,
+								 reg_rule1->freq_range.max_bandwidth_khz,
+								 reg_rule2->freq_range.max_bandwidth_khz);
+
+			if (!cfg80211_does_bw_fit_range(&combined_range, ieee80211_channel_to_khz(chan), MHZ_TO_KHZ(20)))
+				goto disable_chan;
+
+			handle_channel_adjacent_rules(wiphy, initiator, chan, flags, lr,
+						      request_wiphy, reg_rule1, reg_rule2,
+						      &combined_range);
+			return;
+		}
+
+disable_chan:
 		/*
 		 * We will disable all channels that do not match our
 		 * received regulatory rule unless the hint is coming
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2 1/2] cfg80211: add helper fn for single rule channels
  2020-07-01 10:15 [PATCH v2 1/2] cfg80211: add helper fn for single rule channels Markus Theil
  2020-07-01 10:15 ` [PATCH v2 2/2] cfg80211: add helper fn for adjacent " Markus Theil
@ 2020-07-30 11:27 ` Johannes Berg
  1 sibling, 0 replies; 3+ messages in thread
From: Johannes Berg @ 2020-07-30 11:27 UTC (permalink / raw)
  To: Markus Theil; +Cc: linux-wireless

Hi,


> +
> +	handle_channel_single_rule(wiphy, initiator, chan, flags, lr, request_wiphy, reg_rule);

Even if checkpatch probably stopped warning, please don't create
gratuitously long lines. 80 columns is still something to aim for. This
can easily be broken into two or three.

Same applies to the second patch.

johannes


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-07-30 11:27 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-01 10:15 [PATCH v2 1/2] cfg80211: add helper fn for single rule channels Markus Theil
2020-07-01 10:15 ` [PATCH v2 2/2] cfg80211: add helper fn for adjacent " Markus Theil
2020-07-30 11:27 ` [PATCH v2 1/2] cfg80211: add helper fn for single " Johannes Berg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).