Update uci-defaults and add 2 patches for hostapd on BPi-R4

Update a way of generating MAC to use generated MAC - 1, so it correctly resolves with new network config.
Add 2 patches related to eht_required into hostapd for BPi-R4. It allow to pass require_eht=1 as hostapd_options

BUG=b/442409863
TEST=build image and upload on BPi with sysupgrade

Change-Id: I43e81171228a838dcee9c405788a3841fa62fd71
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/dev-util/+/6923571
Reviewed-by: Shijin Abraham <[email protected]>
Tested-by: Alexandr Astrouski <[email protected]>
Commit-Queue: Alexandr Astrouski <[email protected]>
diff --git a/contrib/cros_openwrt_sources/image_generator/configs/BananaPi-R4/9998-hostapd-add_require_eht.patch b/contrib/cros_openwrt_sources/image_generator/configs/BananaPi-R4/9998-hostapd-add_require_eht.patch
new file mode 100644
index 0000000..60ec917
--- /dev/null
+++ b/contrib/cros_openwrt_sources/image_generator/configs/BananaPi-R4/9998-hostapd-add_require_eht.patch
@@ -0,0 +1,90 @@
+Index: hostapd-2025.02.09~c8c7d56a/hostapd/config_file.c
+===================================================================
+--- hostapd-2025.02.09~c8c7d56a.orig/hostapd/config_file.c
++++ hostapd-2025.02.09~c8c7d56a/hostapd/config_file.c
+@@ -5456,6 +5456,8 @@ static int hostapd_config_fill(struct ho
+ #ifdef CONFIG_IEEE80211BE
+ 	} else if (os_strcmp(buf, "ieee80211be") == 0) {
+ 		conf->ieee80211be = atoi(pos);
++	} else if (os_strcmp(buf, "require_eht") == 0) {
++		conf->require_eht = atoi(pos);
+ 	} else if (os_strcmp(buf, "eht_oper_chwidth") == 0) {
+ 		conf->eht_oper_chwidth = atoi(pos);
+ 	} else if (os_strcmp(buf, "eht_oper_centr_freq_seg0_idx") == 0) {
+Index: hostapd-2025.02.09~c8c7d56a/hostapd/hostapd.conf
+===================================================================
+--- hostapd-2025.02.09~c8c7d56a.orig/hostapd/hostapd.conf
++++ hostapd-2025.02.09~c8c7d56a/hostapd/hostapd.conf
+@@ -1096,6 +1096,9 @@ wmm_ac_vo_acm=0
+ # 1 = enabled
+ #ieee80211be=1
+
++# Require stations to support EHT PHY (reject association if they do not)
++#require_eht=1
++
+ #disable_11be: Boolean (0/1) to disable EHT for a specific BSS
+ #disable_11be=0
+
+Index: hostapd-2025.02.09~c8c7d56a/src/ap/ap_config.h
+===================================================================
+--- hostapd-2025.02.09~c8c7d56a.orig/src/ap/ap_config.h
++++ hostapd-2025.02.09~c8c7d56a/src/ap/ap_config.h
+@@ -1282,6 +1282,7 @@ struct hostapd_config {
+ 	enum oper_chan_width eht_oper_chwidth;
+ 	u8 eht_oper_centr_freq_seg0_idx;
+ 	struct eht_phy_capabilities_info eht_phy_capab;
++	int require_eht;
+ 	u16 punct_bitmap; /* a bitmap of disabled 20 MHz channels */
+ 	u8 punct_acs_threshold;
+ 	u8 eht_default_pe_duration;
+Index: hostapd-2025.02.09~c8c7d56a/src/ap/ieee802_11.c
+===================================================================
+--- hostapd-2025.02.09~c8c7d56a.orig/src/ap/ieee802_11.c
++++ hostapd-2025.02.09~c8c7d56a/src/ap/ieee802_11.c
+@@ -153,6 +153,12 @@ static size_t hostapd_supp_rates(struct
+                *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HE_PHY;
+ #endif /* CONFIG_IEEE80211AX */
+
++#ifdef CONFIG_IEEE80211BE
++       if (hapd->iconf->ieee80211be && hapd->iconf->require_eht) {
++               *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_EHT_PHY;
++       }
++#endif
++
+ #ifdef CONFIG_SAE
+        if ((hapd->conf->sae_pwe == SAE_PWE_HASH_TO_ELEMENT ||
+                hostapd_sae_pw_id_in_use(hapd->conf) == 2) &&
+@@ -4222,6 +4222,13 @@ static int __check_assoc_ies(struct host
+ 			if (resp != WLAN_STATUS_SUCCESS)
+ 				return resp;
+ 		}
++
++		if (hapd->iconf->require_eht && !(sta->flags & WLAN_STA_EHT)) {
++			hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
++							HOSTAPD_LEVEL_INFO, "Station does not support "
++							"mandatory EHT PHY - reject association");
++			return WLAN_STATUS_DENIED_EHT_NOT_SUPPORTED;
++		}
+ 	}
+ #endif /* CONFIG_IEEE80211BE */
+
+Index: hostapd-2025.02.09~c8c7d56a/src/common/ieee802_11_defs.h
+===================================================================
+--- hostapd-2025.02.09~c8c7d56a.orig/src/common/ieee802_11_defs.h
++++ hostapd-2025.02.09~c8c7d56a/src/common/ieee802_11_defs.h
+@@ -221,6 +221,7 @@
+ #define WLAN_STATUS_DENIED_HE_NOT_SUPPORTED 124
+ #define WLAN_STATUS_SAE_HASH_TO_ELEMENT 126
+ #define WLAN_STATUS_SAE_PK 127
++#define WLAN_STATUS_DENIED_EHT_NOT_SUPPORTED 135
+ #define WLAN_STATUS_DENIED_STA_AFF_WITH_MLD_WITH_EXISTING_ASSOC 130
+ #define WLAN_STATUS_EPCS_DENIED_UNAUTHORIZED 131
+ #define WLAN_STATUS_EPCS_DENIED 132
+@@ -1441,6 +1442,7 @@ struct ieee80211_ampe_ie {
+ #define HT_OPER_PARAM_PCO_PHASE                                ((u16) BIT(11))
+ /* B36..B39 - Reserved */
+
++#define BSS_MEMBERSHIP_SELECTOR_EHT_PHY 121
+ #define BSS_MEMBERSHIP_SELECTOR_HE_PHY 122
+ #define BSS_MEMBERSHIP_SELECTOR_SAE_H2E_ONLY 123
+ #define BSS_MEMBERSHIP_SELECTOR_EPD 124
diff --git a/contrib/cros_openwrt_sources/image_generator/configs/BananaPi-R4/9999-hostapd-wpa_supplicant_add_eht_members.patch b/contrib/cros_openwrt_sources/image_generator/configs/BananaPi-R4/9999-hostapd-wpa_supplicant_add_eht_members.patch
new file mode 100644
index 0000000..24b7553
--- /dev/null
+++ b/contrib/cros_openwrt_sources/image_generator/configs/BananaPi-R4/9999-hostapd-wpa_supplicant_add_eht_members.patch
@@ -0,0 +1,82 @@
+Index: hostapd-2025.02.09~c8c7d56a/src/drivers/driver.h
+===================================================================
+--- hostapd-2025.02.09~c8c7d56a.orig/src/drivers/driver.h
++++ hostapd-2025.02.09~c8c7d56a/src/drivers/driver.h
+@@ -254,7 +254,7 @@ struct eht_capabilities {
+ #define HOSTAPD_MODE_FLAG_HT_INFO_KNOWN BIT(0)
+ #define HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN BIT(1)
+ #define HOSTAPD_MODE_FLAG_HE_INFO_KNOWN BIT(2)
+-
++#define HOSTAPD_MODE_FLAG_EHT_INFO_KNOWN BIT(3)
+
+ enum ieee80211_op_mode {
+ 	IEEE80211_MODE_INFRA = 0,
+@@ -7544,6 +7544,8 @@ int ht_supported(const struct hostapd_hw
+ int vht_supported(const struct hostapd_hw_modes *mode);
+ bool he_supported(const struct hostapd_hw_modes *hw_mode,
+ 		  enum ieee80211_op_mode op_mode);
++int eht_supported(const struct hostapd_hw_modes *hw_mode,
++		  enum ieee80211_op_mode op_mode);
+
+ struct wowlan_triggers *
+ wpa_get_wowlan_triggers(const char *wowlan_triggers,
+Index: hostapd-2025.02.09~c8c7d56a/src/drivers/driver_common.c
+===================================================================
+--- hostapd-2025.02.09~c8c7d56a.orig/src/drivers/driver_common.c
++++ hostapd-2025.02.09~c8c7d56a/src/drivers/driver_common.c
+@@ -202,6 +202,19 @@ bool he_supported(const struct hostapd_h
+ 	return hw_mode->he_capab[op_mode].he_supported;
+ }
+
++int eht_supported(const struct hostapd_hw_modes *hw_mode,
++		  enum ieee80211_op_mode op_mode)
++{
++       if (!(hw_mode->flags & HOSTAPD_MODE_FLAG_EHT_INFO_KNOWN)) {
++               /*
++                * The driver did not indicate whether it supports EHT. Assume
++                * it does to avoid connection issues.
++                */
++               return 1;
++       }
++
++       return hw_mode->eht_capab[op_mode].eht_supported;
++}
+
+ static int wpa_check_wowlan_trigger(const char *start, const char *trigger,
+ 				    int capa_trigger, u8 *param_trigger)
+Index: hostapd-2025.02.09~c8c7d56a/src/drivers/driver_nl80211_capa.c
+===================================================================
+--- hostapd-2025.02.09~c8c7d56a.orig/src/drivers/driver_nl80211_capa.c
++++ hostapd-2025.02.09~c8c7d56a/src/drivers/driver_nl80211_capa.c
+@@ -2143,7 +2143,8 @@ static int phy_info_band(struct phy_info
+ 		mode->mode = NUM_HOSTAPD_MODES;
+ 		mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN |
+ 			HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN |
+-			HOSTAPD_MODE_FLAG_HE_INFO_KNOWN;
++			HOSTAPD_MODE_FLAG_HE_INFO_KNOWN |
++			HOSTAPD_MODE_FLAG_EHT_INFO_KNOWN;
+
+ 		/*
+ 		 * Unsupported VHT MCS stream is defined as value 3, so the VHT
+Index: hostapd-2025.02.09~c8c7d56a/wpa_supplicant/events.c
+===================================================================
+--- hostapd-2025.02.09~c8c7d56a.orig/wpa_supplicant/events.c
++++ hostapd-2025.02.09~c8c7d56a/wpa_supplicant/events.c
+@@ -1043,6 +1043,17 @@ static int rate_match(struct wpa_supplic
+ 				continue;
+ 			}
+
++			if (flagged && ((rate_ie[j] & 0x7f) ==
++						BSS_MEMBERSHIP_SELECTOR_EHT_PHY)) {
++				if (!eht_supported(mode, IEEE80211_MODE_INFRA)) {
++						if (debug_print)
++								wpa_dbg(wpa_s, MSG_DEBUG,
++										"   hardware does not support EHT PHY");
++						return 0;
++				}
++				continue;
++			}
++
+ #ifdef CONFIG_SAE
+ 			if (flagged && ((rate_ie[j] & 0x7f) ==
+ 					BSS_MEMBERSHIP_SELECTOR_SAE_H2E_ONLY)) {
diff --git a/contrib/cros_openwrt_sources/image_generator/configs/BananaPi-R4/run.sh b/contrib/cros_openwrt_sources/image_generator/configs/BananaPi-R4/run.sh
index eca27d2..754a786 100755
--- a/contrib/cros_openwrt_sources/image_generator/configs/BananaPi-R4/run.sh
+++ b/contrib/cros_openwrt_sources/image_generator/configs/BananaPi-R4/run.sh
@@ -19,5 +19,8 @@
 cp "${DEVICE_CONFIG_DIR}/defconfig_common" "${MTK_AUTOBUILD_DIR}/filogic/mac80211/mt7988_rfb/24.10/defconfig"
 cp -f "${DEVICE_CONFIG_DIR}/0003-hostapd-package-makefile-ucode-files.patch" "${MTK_AUTOBUILD_DIR}/filogic/mac80211/24.10/patches-base/0003-hostapd-package-makefile-ucode-files.patch"
 
+cp "${DEVICE_CONFIG_DIR}/9998-hostapd-add_require_eht.patch" "${MTK_AUTOBUILD_DIR}/filogic/mac80211/24.10/files/package/network/services/hostapd/patches/"
+cp "${DEVICE_CONFIG_DIR}/9999-hostapd-wpa_supplicant_add_eht_members.patch" "${MTK_AUTOBUILD_DIR}/filogic/mac80211/24.10/files/package/network/services/hostapd/patches/"
+
 echo "Start autobuild from MTK"
 bash ../mtk-openwrt-feeds/autobuild/unified/autobuild.sh filogic-mac80211-mt7988_rfb-mt7996 log_file=logs/make_bpir4
diff --git a/contrib/cros_openwrt_sources/image_generator/custom_files/etc/uci-defaults/97_assign_static_mac_address.sh b/contrib/cros_openwrt_sources/image_generator/custom_files/etc/uci-defaults/97_assign_static_mac_address.sh
index 55ad3d8..efb9464 100644
--- a/contrib/cros_openwrt_sources/image_generator/custom_files/etc/uci-defaults/97_assign_static_mac_address.sh
+++ b/contrib/cros_openwrt_sources/image_generator/custom_files/etc/uci-defaults/97_assign_static_mac_address.sh
@@ -16,7 +16,7 @@
   local MAC_PREFIX="02:b4:79"
 
   if [ -z "${hw_id}" ]; then
-      exit 0
+    exit 0
   fi
 
   OLD_IFS="${IFS}"
@@ -24,7 +24,6 @@
   set -- ${hw_id}
   IFS="${OLD_IFS}"
 
-  # $1 $2 %3 $4 ould store all components like e5510793, 216655bc, 731c89c8, 61a9e4bc
   val_1=$1
   val_2=$2
   val_3=$3
@@ -32,7 +31,29 @@
   byte_part2=${val_2:start_char:2}
   byte_part3=${val_3:start_char:2}
 
-  echo "${MAC_PREFIX}:${byte_part1}:${byte_part2}:${byte_part3}"
+  current_mac="${MAC_PREFIX}:${byte_part1}:${byte_part2}:${byte_part3}"
+
+  # This hack added to keep MAC address on br-wan same as it was on first image were wan had same MAC as eth0
+  # Now eth0 would be 1 less, and uci scripts would assign eth0 MAC + 1 to eth2 and wan,
+  # so br-wan would be same as before but different from eth0 and no updates required for already flashed BPi's
+
+  # --- START: REPLACEMENT CODE ---
+  # Remove the colons from the MAC address string
+  mac_no_colons=$(echo "${current_mac}" | tr -d ':')
+
+  # Use shell arithmetic to convert from hex to decimal, subtract 1
+  # The shell understands "0x..." as a hex number inside ((...))
+  prev_mac_decimal=$((0x${mac_no_colons} - 1))
+
+  # Use printf to convert the new decimal value back to a 12-character
+  # hexadecimal string, padded with leading zeros.
+  prev_mac_hex=$(printf "%012X" "${prev_mac_decimal}")
+  # --- END: REPLACEMENT CODE ---
+
+  # Finally, use sed to insert the colons every two characters.
+  prev_mac=$(echo "${prev_mac_hex}" | sed 's/../&:/g;s/:$//')
+
+  echo "${prev_mac}"
   return 0
 }
 
diff --git a/contrib/cros_openwrt_sources/image_generator/custom_files/etc/uci-defaults/99_cros_customizations.sh b/contrib/cros_openwrt_sources/image_generator/custom_files/etc/uci-defaults/99_cros_customizations.sh
index 99e6c31..8a5860a 100755
--- a/contrib/cros_openwrt_sources/image_generator/custom_files/etc/uci-defaults/99_cros_customizations.sh
+++ b/contrib/cros_openwrt_sources/image_generator/custom_files/etc/uci-defaults/99_cros_customizations.sh
@@ -63,8 +63,7 @@
 setup_br_lan() {
   local board="$1"
   case ${board} in
-    bananapi,bpi-r3|\
-    bananapi,bpi-r4)
+    bananapi,bpi-r3)
       # Delete existing br-lan device which services the bpi internal LAN and rename
       # br-wan --> br-lan which to stay in line with other OpenWrt images.
       # Remove br-lan only if br-wan exists, prevent from unnecessary delete if run several times
@@ -92,6 +91,16 @@
       fi
 
     ;;
+    bananapi,bpi-r4)
+      echo "Create static lan interface"
+
+      uci set network.lan='interface'
+      uci set network.lan.device='br-lan'
+      uci set network.lan.proto='static'
+      uci set network.lan.ipaddr='192.168.1.1'
+      uci set network.lan.netmask='255.255.255.0'
+      uci set network.lan.ip6assign='60'
+    ;;
     *)
       BR_LAN_L2_PORT="eth0"
       if [ -f /sys/class/net/lan/operstate ]; then
@@ -117,7 +126,12 @@
 # Add an interface to connect to lab network (wan) as a DHCP client.
 uci set network.wan=interface
 uci set network.wan.proto='dhcp'
-uci set network.wan.device='br-lan'
+
+if [ "$(board_name)" != 'bananapi,bpi-r4' ]; then
+  uci set network.wan.device='br-lan'
+else
+  uci set network.wan.device='br-wan'
+fi
 
 # Turn on wireless radios on by default.
 uci set wireless.radio0.disabled='0'
@@ -150,7 +164,7 @@
 # Configure cros init script.
 CROS_TEST_SERVICE_PATH="/etc/init.d/z_cros_test.sh"
 
-if [ -f "${CROS_TEST_SERVICE_PATH}" ]; then
+if [ -f "${CROS_TEST_SERVICE_PATH}" ] && [ "$(board_name)" != 'bananapi,bpi-r4' ]; then
   chmod 755 "${CROS_TEST_SERVICE_PATH}"
   ${CROS_TEST_SERVICE_PATH} enable
   ${CROS_TEST_SERVICE_PATH} start