[][MAC80211][WiFi6/7][Misc][Add country setting consistent check before enable AP.]

[Description]
Add country setting consistent check before enable AP.
Because the script is called per phy, we must need to parsing /etc/config/wireless manually.
For wifi7 code flow, hostapd down/up interface in ubus call config_set, so if we want
to remove the invalid config we need to call additional remove function.

[Release-log]
N/A

Change-Id: I69f53180961fcdd0d3141a1f68f6557e9a5089c4
Reviewed-on: https://gerrit.mediatek.inc/c/openwrt/feeds/mtk_openwrt_feeds/+/8304256
diff --git a/autobuild_mac80211_release/0001-wifi7-mac80211-generate-hostapd-setting-from-ap-cap.patch b/autobuild_mac80211_release/0001-wifi7-mac80211-generate-hostapd-setting-from-ap-cap.patch
index 3b1fd04..1d20cb7 100644
--- a/autobuild_mac80211_release/0001-wifi7-mac80211-generate-hostapd-setting-from-ap-cap.patch
+++ b/autobuild_mac80211_release/0001-wifi7-mac80211-generate-hostapd-setting-from-ap-cap.patch
@@ -30,7 +30,7 @@
  		$(PKG_BUILD_DIR)/include/linux/ssb \
  		$(PKG_BUILD_DIR)/include/linux/bcma \
 diff --git a/package/kernel/mac80211/files/lib/netifd/wireless/mac80211.sh b/package/kernel/mac80211/files/lib/netifd/wireless/mac80211.sh
-index 1bf4db6e..9176706d 100644
+index 1bf4db6e..4ce420a5 100644
 --- a/package/kernel/mac80211/files/lib/netifd/wireless/mac80211.sh
 +++ b/package/kernel/mac80211/files/lib/netifd/wireless/mac80211.sh
 @@ -25,10 +25,12 @@ drv_mac80211_init_device_config() {
@@ -458,7 +458,21 @@
  		macaddr="$(mac80211_generate_mac $phy)"
  		macidx="$(($macidx + 1))"
  		default_macaddr=1
-@@ -913,6 +1073,14 @@ hostapd_set_config() {
+@@ -907,12 +1067,28 @@ wpa_supplicant_set_config() {
+ }
+ 
+ hostapd_set_config() {
++
++	if [ "$inconsistent_country" -eq 1 ]; then
++		echo "ERROR: Please use the same country for all the radios."
++		wireless_setup_failed HOSTAPD_START_FAILED
++		drv_mac80211_teardown
++		return
++	fi
++
+ 	[ -n "$hostapd_ctrl" ] || {
+ 		ubus call hostapd config_set '{ "phy": "'"$phy"'", "config": "", "prev_config": "'"${hostapd_conf_file}.prev"'" }' > /dev/null
+ 		return 0;
  	}
  
  	ubus wait_for hostapd
@@ -473,7 +487,7 @@
  	local hostapd_res="$(ubus call hostapd config_set "{ \"phy\": \"$phy\", \"config\":\"${hostapd_conf_file}\", \"prev_config\": \"${hostapd_conf_file}.prev\"}")"
  	ret="$?"
  	[ "$ret" != 0 -o -z "$hostapd_res" ] && {
-@@ -990,6 +1158,9 @@ mac80211_setup_vif() {
+@@ -990,6 +1166,9 @@ mac80211_setup_vif() {
  
  	json_select ..
  	[ -n "$failed" ] || wireless_add_vif "$name" "$ifname"
@@ -483,7 +497,7 @@
  }
  
  get_freq() {
-@@ -1041,6 +1212,10 @@ mac80211_reset_config() {
+@@ -1041,6 +1220,25 @@ mac80211_reset_config() {
  	wdev_tool "$phy" set_config '{}'
  }
  
@@ -491,10 +505,25 @@
 +	total_num_ap=$(($total_num_ap + 1))
 +}
 +
++country_consistent_check() {
++	local i
++	inconsistent_country=0
++	country_list="$(cat /etc/config/wireless | grep country | cut -d ' ' -f3 | tr -s "'\n" ' ')"
++	for i in $country_list
++	do
++		ret="$(echo $country_list | awk '{print ($2 == "" || $1 == $2)}')"
++		[ $ret = '0' ] && {
++			inconsistent_country=1
++			return
++		}
++		country_list="$(echo $country_list | sed -r 's/[A-Z]{2}( )*//')"
++	done
++}
++
  drv_mac80211_setup() {
  	json_select config
  	json_get_vars \
-@@ -1049,7 +1224,8 @@ drv_mac80211_setup() {
+@@ -1049,7 +1247,8 @@ drv_mac80211_setup() {
  		txpower \
  		rxantenna txantenna \
  		frag rts beacon_int:100 htmode \
@@ -504,7 +533,7 @@
  	json_get_values basic_rate_list basic_rate
  	json_get_values scan_list scan_list
  	json_select ..
-@@ -1084,6 +1260,7 @@ drv_mac80211_setup() {
+@@ -1084,6 +1283,7 @@ drv_mac80211_setup() {
  
  	macidx=0
  	staidx=0
@@ -512,7 +541,7 @@
  
  	[ -n "$chanbw" ] && {
  		for file in /sys/kernel/debug/ieee80211/$phy/ath9k*/chanbw /sys/kernel/debug/ieee80211/$phy/ath5k/bwmode; do
-@@ -1132,6 +1309,16 @@ drv_mac80211_setup() {
+@@ -1132,14 +1332,29 @@ drv_mac80211_setup() {
  
  	wpa_supplicant_init_config
  
@@ -529,7 +558,10 @@
  	mac80211_prepare_iw_htmode
  	active_ifnames=
  	for_each_interface "ap sta adhoc mesh monitor" mac80211_prepare_vif
-@@ -1140,6 +1327,9 @@ drv_mac80211_setup() {
+ 	for_each_interface "ap sta adhoc mesh monitor" mac80211_setup_vif
+ 
++	country_consistent_check
++
  	[ -x /usr/sbin/wpa_supplicant ] && wpa_supplicant_set_config "$phy"
  	[ -x /usr/sbin/hostapd ] && hostapd_set_config "$phy"