[][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-wifi6-mac80211-generate-hostapd-setting-from-ap-cap.patch b/autobuild_mac80211_release/0001-wifi6-mac80211-generate-hostapd-setting-from-ap-cap.patch
index 0314194..9bbb5c3 100644
--- a/autobuild_mac80211_release/0001-wifi6-mac80211-generate-hostapd-setting-from-ap-cap.patch
+++ b/autobuild_mac80211_release/0001-wifi6-mac80211-generate-hostapd-setting-from-ap-cap.patch
@@ -32,7 +32,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 ed28052..13cde50 100644
+index ed28052..c353d6a 100644
 --- a/package/kernel/mac80211/files/lib/netifd/wireless/mac80211.sh
 +++ b/package/kernel/mac80211/files/lib/netifd/wireless/mac80211.sh
 @@ -27,6 +27,7 @@ drv_mac80211_init_device_config() {
@@ -422,7 +422,7 @@
  }
  
  get_freq() {
-@@ -1122,6 +1273,10 @@ drv_mac80211_cleanup() {
+@@ -1122,6 +1273,25 @@ drv_mac80211_cleanup() {
  	hostapd_common_cleanup
  }
  
@@ -430,10 +430,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 \
-@@ -1129,7 +1284,8 @@ drv_mac80211_setup() {
+@@ -1129,7 +1299,8 @@ drv_mac80211_setup() {
  		country chanbw distance \
  		txpower antenna_gain \
  		rxantenna txantenna \
@@ -443,7 +458,7 @@
  	json_get_values basic_rate_list basic_rate
  	json_get_values scan_list scan_list
  	json_select ..
-@@ -1180,6 +1336,7 @@ drv_mac80211_setup() {
+@@ -1180,6 +1351,7 @@ drv_mac80211_setup() {
  	no_ap=1
  	macidx=0
  	staidx=0
@@ -451,7 +466,7 @@
  
  	[ -n "$chanbw" ] && {
  		for file in /sys/kernel/debug/ieee80211/$phy/ath9k*/chanbw /sys/kernel/debug/ieee80211/$phy/ath5k/bwmode; do
-@@ -1219,6 +1376,16 @@ drv_mac80211_setup() {
+@@ -1219,6 +1391,16 @@ drv_mac80211_setup() {
  	for_each_interface "sta adhoc mesh" mac80211_set_noscan
  	[ -n "$has_ap" ] && mac80211_hostapd_setup_base "$phy"
  
@@ -468,8 +483,16 @@
  	mac80211_prepare_iw_htmode
  	for_each_interface "sta adhoc mesh monitor" mac80211_prepare_vif
  	NEWAPLIST=
-@@ -1251,6 +1418,14 @@ drv_mac80211_setup() {
+@@ -1249,8 +1431,22 @@ drv_mac80211_setup() {
+ 			}
+ 		fi
  		if [ "$no_reload" != "0" ]; then
++			country_consistent_check
++			if [ "$inconsistent_country" == "1" ]; then
++				echo "ERROR: Please use the same country for all the radios."
++				wireless_setup_failed HOSTAPD_START_FAILED
++				return
++			fi
  			add_ap=1
  			ubus wait_for hostapd
 +
@@ -483,7 +506,7 @@
  			local hostapd_res="$(ubus call hostapd config_add "{\"iface\":\"$primary_ap\", \"config\":\"${hostapd_conf_file}\"}")"
  			ret="$?"
  			[ "$ret" != 0 -o -z "$hostapd_res" ] && {
-@@ -1266,6 +1441,9 @@ drv_mac80211_setup() {
+@@ -1266,6 +1462,9 @@ drv_mac80211_setup() {
  	[ "${add_ap}" = 1 ] && sleep 1
  	for_each_interface "ap" mac80211_setup_vif