Merge "Prepares wpa_supplicant for use by a wifi HAL vendor APEX."
diff --git a/wpa_supplicant/Android.bp b/wpa_supplicant/Android.bp
new file mode 100644
index 0000000..8f60cbd
--- /dev/null
+++ b/wpa_supplicant/Android.bp
@@ -0,0 +1,414 @@
+// Copyright (C) 2021 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package {
+    default_applicable_licenses: [
+        "external_wpa_supplicant_8_wpa_supplicant_license",
+    ],
+}
+
+license {
+    name: "external_wpa_supplicant_8_wpa_supplicant_license",
+    license_kinds: [
+        "SPDX-license-identifier-BSD",
+        "SPDX-license-identifier-BSD-3-Clause",
+        "SPDX-license-identifier-ISC",
+        "legacy_unencumbered",
+    ],
+}
+
+cc_library_headers {
+    name: "wpa_supplicant_headers",
+    export_include_dirs: [
+        ".",
+        "src",
+        "src/common",
+        "src/drivers",
+        "src/eap_common",
+        "src/eapol_supp",
+        "src/eap_peer",
+        "src/eap_server",
+        "src/l2_packet",
+        "src/radius",
+        "src/rsn_supp",
+        "src/tls",
+        "src/utils",
+        "src/wps",
+    ],
+    soc_specific: true,
+}
+
+filegroup {
+    name: "wpa_supplicant_template.conf",
+    srcs: ["wpa_supplicant_template.conf"],
+}
+
+cc_defaults {
+    name: "wpa_supplicant_defaults",
+    defaults: ["wpa_supplicant_cflags_defaults"],
+    srcs: [":wpa_supplicant_srcs"],
+    shared_libs: [
+        "android.hardware.wifi.supplicant@1.0",
+        "android.hardware.wifi.supplicant@1.1",
+        "android.hardware.wifi.supplicant@1.2",
+        "android.hardware.wifi.supplicant@1.3",
+        "android.hardware.wifi.supplicant@1.4",
+        "libbase",
+        "libc",
+        "libcrypto",
+        "libcutils",
+        "libhidlbase",
+        "libkeystore-engine-wifi-hidl",
+        "libkeystore-wifi-hidl",
+        "liblog",
+        "libnl",
+        "libssl",
+        "libutils",
+    ],
+    relative_install_path: "hw",
+    soc_specific: true,
+    static_libs: [
+        "libwpa_hidl_bp",
+    ],
+    header_libs: [
+        "wpa_supplicant_headers",
+        "libwpa_hidl_headers",
+    ],
+}
+
+soong_config_module_type {
+    name: "wpa_supplicant_cflags_cc_defaults",
+    module_type: "cc_defaults",
+    config_namespace: "wpa_supplicant",
+    value_variables: [
+        "platform_version",
+        "nl80211_driver",
+    ],
+    properties: ["cflags"],
+}
+
+wpa_supplicant_cflags_cc_defaults {
+    name: "wpa_supplicant_cflags_defaults",
+    soong_config_variables: {
+        // Devices that include this module should set these soong config vars appropriately.
+        // For example:
+        //   $(call add_soong_config_namespace, wpa_supplicant)
+        //   $(call add_soong_config_var_value, wpa_supplicant, platform_version, $(PLATFORM_VERSION))
+        //   $(call add_soong_config_var_value, wpa_supplicant, nl80211_driver, CONFIG_DRIVER_NL80211_QCA)
+        platform_version: {
+            cflags: ["-DVERSION_STR_POSTFIX=\"-%s\""],
+            conditions_default: {
+                // Default value
+                cflags: ["-DVERSION_STR_POSTFIX=\"-Android\""],
+            },
+        },
+        nl80211_driver: {
+            cflags: ["-D%s"],
+            // Flag is optional, so no default value provided.
+        },
+    },
+    // Generated by building wpa_supplicant and printing LOCAL_CFLAGS.
+    cflags: [
+        "-DANDROID_LOG_NAME=\"wpa_supplicant\"",
+        "-DANDROID_P2P",
+        "-DCONFIG_ACS",
+        "-DCONFIG_ANDROID_LOG",
+        "-DCONFIG_AP",
+        "-DCONFIG_BACKEND_FILE",
+        "-DCONFIG_CTRL_IFACE",
+        "-DCONFIG_CTRL_IFACE_CLIENT_DIR=\"/data/vendor/wifi/wpa/sockets\"",
+        "-DCONFIG_CTRL_IFACE_DIR=\"/data/vendor/wifi/wpa/sockets\"",
+        "-DCONFIG_CTRL_IFACE_HIDL",
+        "-DCONFIG_CTRL_IFACE_UNIX",
+        "-DCONFIG_DPP",
+        "-DCONFIG_DPP2",
+        "-DCONFIG_DRIVER_NL80211",
+        "-DCONFIG_ECC",
+        "-DCONFIG_ERP",
+        "-DCONFIG_FILS",
+        "-DCONFIG_GAS",
+        "-DCONFIG_GAS_SERVER",
+        "-DCONFIG_HIDL",
+        "-DCONFIG_HMAC_SHA256_KDF",
+        "-DCONFIG_HMAC_SHA384_KDF",
+        "-DCONFIG_HMAC_SHA512_KDF",
+        "-DCONFIG_HS20",
+        "-DCONFIG_IEEE80211AC",
+        "-DCONFIG_IEEE80211R",
+        "-DCONFIG_INTERWORKING",
+        "-DCONFIG_IPV6",
+        "-DCONFIG_JSON",
+        "-DCONFIG_MBO",
+        "-DCONFIG_NO_ACCOUNTING",
+        "-DCONFIG_NO_RADIUS",
+        "-DCONFIG_NO_RADIUS",
+        "-DCONFIG_NO_RANDOM_POOL",
+        "-DCONFIG_NO_ROAMING",
+        "-DCONFIG_NO_VLAN",
+        "-DCONFIG_OFFCHANNEL",
+        "-DCONFIG_OWE",
+        "-DCONFIG_P2P",
+        "-DCONFIG_SAE",
+        "-DCONFIG_SAE_PK",
+        "-DCONFIG_SHA256",
+        "-DCONFIG_SHA384",
+        "-DCONFIG_SHA512",
+        "-DCONFIG_SMARTCARD",
+        "-DCONFIG_SME",
+        "-DCONFIG_SUITEB",
+        "-DCONFIG_SUITEB192",
+        "-DCONFIG_TDLS",
+        "-DCONFIG_WEP",
+        "-DCONFIG_WIFI_DISPLAY",
+        "-DCONFIG_WNM",
+        "-DCONFIG_WPA_CLI_HISTORY_DIR=\"/data/vendor/wifi/wpa\"",
+        "-DCONFIG_WPS",
+        "-DCONFIG_WPS_ER",
+        "-DCONFIG_WPS_NFC",
+        "-DCONFIG_WPS_OOB",
+        "-DCONFIG_WPS_UPNP",
+        "-DEAP_AKA",
+        "-DEAP_AKA_PRIME",
+        "-DEAP_GTC",
+        "-DEAP_LEAP",
+        "-DEAP_MD5",
+        "-DEAP_MSCHAPv2",
+        "-DEAP_OTP",
+        "-DEAP_PEAP",
+        "-DEAP_PWD",
+        "-DEAP_SERVER",
+        "-DEAP_SERVER_IDENTITY",
+        "-DEAP_SERVER_WSC",
+        "-DEAP_SIM",
+        "-DEAP_TLS",
+        "-DEAP_TLS_OPENSSL",
+        "-DEAP_TTLS",
+        "-DEAP_WSC",
+        "-DIEEE8021X_EAPOL",
+        "-DNEED_AP_MLME",
+        "-DPKCS12_FUNCS",
+        "-DTLS_DEFAULT_CIPHERS=\"DEFAULT:!EXP:!LOW\"",
+        "-DWPA_IGNORE_CONFIG_ERRORS",
+        "-Wall",
+        "-Werror",
+        "-Wno-error=sometimes-uninitialized",
+        "-Wno-incompatible-pointer-types",
+        "-Wno-incompatible-pointer-types-discards-qualifiers",
+        "-Wno-macro-redefined",
+        "-Wno-parentheses-equality",
+        "-Wno-sign-compare",
+        "-Wno-unused-function",
+        "-Wno-unused-parameter",
+        "-Wno-unused-variable",
+    ],
+}
+
+// Generated by building wpa_supplicant and printing LOCAL_SRC_FILES.
+filegroup {
+    name: "wpa_supplicant_srcs",
+    srcs: [
+        "ap.c",
+        "bss.c",
+        "bssid_ignore.c",
+        "config.c",
+        "config_file.c",
+        "ctrl_iface.c",
+        "ctrl_iface_unix.c",
+        "dpp_supplicant.c",
+        "eap_register.c",
+        "events.c",
+        "gas_query.c",
+        "hs20_supplicant.c",
+        "interworking.c",
+        "main.c",
+        "mbo.c",
+        "notify.c",
+        "offchannel.c",
+        "op_classes.c",
+        "p2p_supplicant.c",
+        "p2p_supplicant_sd.c",
+        "robust_av.c",
+        "rrm.c",
+        "scan.c",
+        "sme.c",
+        "src/ap/acs.c",
+        "src/ap/ap_config.c",
+        "src/ap/ap_drv_ops.c",
+        "src/ap/ap_list.c",
+        "src/ap/ap_mlme.c",
+        "src/ap/authsrv.c",
+        "src/ap/beacon.c",
+        "src/ap/bss_load.c",
+        "src/ap/ctrl_iface_ap.c",
+        "src/ap/dfs.c",
+        "src/ap/dpp_hostapd.c",
+        "src/ap/drv_callbacks.c",
+        "src/ap/eap_user_db.c",
+        "src/ap/fils_hlp.c",
+        "src/ap/gas_query_ap.c",
+        "src/ap/gas_serv.c",
+        "src/ap/hostapd.c",
+        "src/ap/hs20.c",
+        "src/ap/hw_features.c",
+        "src/ap/ieee802_11_auth.c",
+        "src/ap/ieee802_11.c",
+        "src/ap/ieee802_11_ht.c",
+        "src/ap/ieee802_11_shared.c",
+        "src/ap/ieee802_11_vht.c",
+        "src/ap/ieee802_1x.c",
+        "src/ap/mbo_ap.c",
+        "src/ap/neighbor_db.c",
+        "src/ap/p2p_hostapd.c",
+        "src/ap/pmksa_cache_auth.c",
+        "src/ap/rrm.c",
+        "src/ap/sta_info.c",
+        "src/ap/tkip_countermeasures.c",
+        "src/ap/utils.c",
+        "src/ap/wmm.c",
+        "src/ap/wpa_auth.c",
+        "src/ap/wpa_auth_glue.c",
+        "src/ap/wpa_auth_ie.c",
+        "src/ap/wps_hostapd.c",
+        "src/common/ctrl_iface_common.c",
+        "src/common/dpp_auth.c",
+        "src/common/dpp_backup.c",
+        "src/common/dpp.c",
+        "src/common/dpp_crypto.c",
+        "src/common/dpp_pkex.c",
+        "src/common/dpp_reconfig.c",
+        "src/common/dpp_tcp.c",
+        "src/common/dragonfly.c",
+        "src/common/gas.c",
+        "src/common/gas_server.c",
+        "src/common/hw_features_common.c",
+        "src/common/ieee802_11_common.c",
+        "src/common/sae.c",
+        "src/common/sae_pk.c",
+        "src/common/wpa_common.c",
+        "src/crypto/aes-ctr.c",
+        "src/crypto/aes-omac1.c",
+        "src/crypto/aes-siv.c",
+        "src/crypto/crypto_openssl.c",
+        "src/crypto/dh_groups.c",
+        "src/crypto/fips_prf_openssl.c",
+        "src/crypto/ms_funcs.c",
+        "src/crypto/sha1-prf.c",
+        "src/crypto/sha1-tlsprf.c",
+        "src/crypto/sha256-kdf.c",
+        "src/crypto/sha256-prf.c",
+        "src/crypto/sha256-tlsprf.c",
+        "src/crypto/sha384-kdf.c",
+        "src/crypto/sha384-prf.c",
+        "src/crypto/sha512-kdf.c",
+        "src/crypto/sha512-prf.c",
+        "src/crypto/tls_openssl.c",
+        "src/crypto/tls_openssl_ocsp.c",
+        "src/drivers/driver_common.c",
+        "src/drivers/driver_nl80211_android.c",
+        "src/drivers/driver_nl80211.c",
+        "src/drivers/driver_nl80211_capa.c",
+        "src/drivers/driver_nl80211_event.c",
+        "src/drivers/driver_nl80211_monitor.c",
+        "src/drivers/driver_nl80211_scan.c",
+        "src/drivers/drivers.c",
+        "src/drivers/linux_ioctl.c",
+        "src/drivers/netlink.c",
+        "src/drivers/rfkill.c",
+        "src/eap_common/chap.c",
+        "src/eap_common/eap_common.c",
+        "src/eap_common/eap_peap_common.c",
+        "src/eap_common/eap_pwd_common.c",
+        "src/eap_common/eap_sim_common.c",
+        "src/eap_common/eap_wsc_common.c",
+        "src/eapol_auth/eapol_auth_sm.c",
+        "src/eapol_supp/eapol_supp_sm.c",
+        "src/eap_peer/eap_aka.c",
+        "src/eap_peer/eap.c",
+        "src/eap_peer/eap_gtc.c",
+        "src/eap_peer/eap_leap.c",
+        "src/eap_peer/eap_md5.c",
+        "src/eap_peer/eap_methods.c",
+        "src/eap_peer/eap_mschapv2.c",
+        "src/eap_peer/eap_otp.c",
+        "src/eap_peer/eap_peap.c",
+        "src/eap_peer/eap_pwd.c",
+        "src/eap_peer/eap_sim.c",
+        "src/eap_peer/eap_tls.c",
+        "src/eap_peer/eap_tls_common.c",
+        "src/eap_peer/eap_ttls.c",
+        "src/eap_peer/eap_wsc.c",
+        "src/eap_peer/mschapv2.c",
+        "src/eap_server/eap_server.c",
+        "src/eap_server/eap_server_identity.c",
+        "src/eap_server/eap_server_methods.c",
+        "src/eap_server/eap_server_wsc.c",
+        "src/l2_packet/l2_packet_linux.c",
+        "src/p2p/p2p_build.c",
+        "src/p2p/p2p.c",
+        "src/p2p/p2p_dev_disc.c",
+        "src/p2p/p2p_go_neg.c",
+        "src/p2p/p2p_group.c",
+        "src/p2p/p2p_invitation.c",
+        "src/p2p/p2p_parse.c",
+        "src/p2p/p2p_pd.c",
+        "src/p2p/p2p_sd.c",
+        "src/p2p/p2p_utils.c",
+        "src/rsn_supp/pmksa_cache.c",
+        "src/rsn_supp/preauth.c",
+        "src/rsn_supp/tdls.c",
+        "src/rsn_supp/wpa.c",
+        "src/rsn_supp/wpa_ft.c",
+        "src/rsn_supp/wpa_ie.c",
+        "src/tls/asn1.c",
+        "src/utils/base64.c",
+        "src/utils/bitfield.c",
+        "src/utils/common.c",
+        "src/utils/config.c",
+        "src/utils/crc32.c",
+        "src/utils/eloop.c",
+        "src/utils/ip_addr.c",
+        "src/utils/json.c",
+        "src/utils/os_unix.c",
+        "src/utils/radiotap.c",
+        "src/utils/uuid.c",
+        "src/utils/wpabuf.c",
+        "src/utils/wpa_debug.c",
+        "src/wps/http_client.c",
+        "src/wps/httpread.c",
+        "src/wps/http_server.c",
+        "src/wps/ndef.c",
+        "src/wps/upnp_xml.c",
+        "src/wps/wps_attr_build.c",
+        "src/wps/wps_attr_parse.c",
+        "src/wps/wps_attr_process.c",
+        "src/wps/wps.c",
+        "src/wps/wps_common.c",
+        "src/wps/wps_dev_attr.c",
+        "src/wps/wps_enrollee.c",
+        "src/wps/wps_er.c",
+        "src/wps/wps_er_ssdp.c",
+        "src/wps/wps_registrar.c",
+        "src/wps/wps_upnp_ap.c",
+        "src/wps/wps_upnp.c",
+        "src/wps/wps_upnp_event.c",
+        "src/wps/wps_upnp_ssdp.c",
+        "src/wps/wps_upnp_web.c",
+        "wifi_display.c",
+        "wmm_ac.c",
+        "wnm_sta.c",
+        "wpas_glue.c",
+        "wpa_supplicant.c",
+        "wps_supplicant.c",
+    ],
+}
diff --git a/wpa_supplicant/hidl/1.4/Android.bp b/wpa_supplicant/hidl/1.4/Android.bp
new file mode 100644
index 0000000..943c957
--- /dev/null
+++ b/wpa_supplicant/hidl/1.4/Android.bp
@@ -0,0 +1,55 @@
+// Copyright (C) 2021 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+cc_library_headers {
+    name: "libwpa_hidl_headers",
+    export_include_dirs: ["."],
+    soc_specific: true,
+}
+
+cc_library_static {
+    name: "libwpa_hidl_bp",
+    srcs: ["*.cpp"],
+    defaults: ["wpa_supplicant_cflags_defaults"],
+    soc_specific: true,
+    shared_libs: [
+        "android.hardware.wifi.supplicant@1.0",
+        "android.hardware.wifi.supplicant@1.1",
+        "android.hardware.wifi.supplicant@1.2",
+        "android.hardware.wifi.supplicant@1.3",
+        "android.hardware.wifi.supplicant@1.4",
+        "libbase",
+        "libhidlbase",
+        "libutils",
+        "liblog",
+        "libssl",
+    ],
+    // Generated by building libwpa_hidl and printing LOCAL_CPPFLAGS.
+    cppflags: [
+        "-Wall",
+        "-Werror",
+        "-Wno-unused-parameter",
+        "-Wno-unused-private-field",
+        "-Wno-unused-variable",
+    ],
+    header_libs: [
+        "wpa_supplicant_headers",
+        "libwpa_hidl_headers",
+    ],
+}
+
+filegroup {
+    name: "android.hardware.wifi.supplicant.xml",
+    srcs: ["android.hardware.wifi.supplicant.xml"],
+}
diff --git a/wpa_supplicant/hidl/1.4/supplicant.cpp b/wpa_supplicant/hidl/1.4/supplicant.cpp
index 6f4cde6..a65b70e 100644
--- a/wpa_supplicant/hidl/1.4/supplicant.cpp
+++ b/wpa_supplicant/hidl/1.4/supplicant.cpp
@@ -23,21 +23,36 @@
 constexpr char kIfaceDriverName[] = "nl80211";
 constexpr char kStaIfaceConfPath[] =
     "/data/vendor/wifi/wpa/wpa_supplicant.conf";
-constexpr char kStaIfaceConfOverlayPath[] =
-    "/vendor/etc/wifi/wpa_supplicant_overlay.conf";
+static const char* kStaIfaceConfOverlayPaths[] = {
+    "/apex/com.android.wifi.hal/etc/wifi/wpa_supplicant_overlay.conf",
+    "/vendor/etc/wifi/wpa_supplicant_overlay.conf",
+};
 constexpr char kP2pIfaceConfPath[] =
     "/data/vendor/wifi/wpa/p2p_supplicant.conf";
-constexpr char kP2pIfaceConfOverlayPath[] =
-    "/vendor/etc/wifi/p2p_supplicant_overlay.conf";
+static const char* kP2pIfaceConfOverlayPaths[] = {
+    "/apex/com.android.wifi.hal/etc/wifi/p2p_supplicant_overlay.conf",
+    "/vendor/etc/wifi/p2p_supplicant_overlay.conf",
+};
 // Migrate conf files for existing devices.
-constexpr char kSystemTemplateConfPath[] =
-    "/system/etc/wifi/wpa_supplicant.conf";
-constexpr char kVendorTemplateConfPath[] =
-    "/vendor/etc/wifi/wpa_supplicant.conf";
+static const char* kTemplateConfPaths[] = {
+    "/apex/com.android.wifi.hal/etc/wifi/wpa_supplicant.conf",
+    "/vendor/etc/wifi/wpa_supplicant.conf",
+    "/system/etc/wifi/wpa_supplicant.conf",
+};
 constexpr char kOldStaIfaceConfPath[] = "/data/misc/wifi/wpa_supplicant.conf";
 constexpr char kOldP2pIfaceConfPath[] = "/data/misc/wifi/p2p_supplicant.conf";
 constexpr mode_t kConfigFileMode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
 
+const char* resolvePath(const char* paths[], size_t size)
+{
+	for (int i = 0; i < size; ++i) {
+		if (access(paths[i], R_OK) == 0) {
+			return paths[i];
+		}
+	}
+	return nullptr;
+}
+
 int copyFile(
     const std::string& src_file_path, const std::string& dest_file_path)
 {
@@ -128,25 +143,19 @@
 		unlink(config_file_path.c_str());
 		return -1;
 	}
-	ret = copyFileIfItExists(kVendorTemplateConfPath, config_file_path);
-	if (ret == 0) {
-		wpa_printf(
-		    MSG_INFO, "Copied template conf file from %s to %s",
-		    kVendorTemplateConfPath, config_file_path.c_str());
-		return 0;
-	} else if (ret == -1) {
-		unlink(config_file_path.c_str());
-		return -1;
-	}
-	ret = copyFileIfItExists(kSystemTemplateConfPath, config_file_path);
-	if (ret == 0) {
-		wpa_printf(
-		    MSG_INFO, "Copied template conf file from %s to %s",
-		    kSystemTemplateConfPath, config_file_path.c_str());
-		return 0;
-	} else if (ret == -1) {
-		unlink(config_file_path.c_str());
-		return -1;
+	const char* path =
+	    resolvePath(kTemplateConfPaths, sizeof(kTemplateConfPaths));
+	if (path != nullptr) {
+		ret = copyFileIfItExists(path, config_file_path);
+		if (ret == 0) {
+			wpa_printf(
+			    MSG_INFO, "Copied template conf file from %s to %s",
+			    path, config_file_path.c_str());
+			return 0;
+		} else if (ret == -1) {
+			unlink(config_file_path.c_str());
+			return -1;
+		}
 	}
 	// Did not create the conf file.
 	return -1;
@@ -318,9 +327,11 @@
 				{}};
 		}
 		iface_params.confname = kP2pIfaceConfPath;
-		int ret = access(kP2pIfaceConfOverlayPath, R_OK);
-		if (ret == 0) {
-			iface_params.confanother = kP2pIfaceConfOverlayPath;
+		const char* path = resolvePath(
+		    kP2pIfaceConfOverlayPaths,
+		    sizeof(kP2pIfaceConfOverlayPaths));
+		if (path != nullptr) {
+			iface_params.confanother = path;
 		}
 	} else {
 		if (ensureConfigFileExists(
@@ -333,9 +344,11 @@
 				{}};
 		}
 		iface_params.confname = kStaIfaceConfPath;
-		int ret = access(kStaIfaceConfOverlayPath, R_OK);
-		if (ret == 0) {
-			iface_params.confanother = kStaIfaceConfOverlayPath;
+		const char* path = resolvePath(
+		    kStaIfaceConfOverlayPaths,
+		    sizeof(kStaIfaceConfOverlayPaths));
+		if (path != nullptr) {
+			iface_params.confanother = path;
 		}
 	}
 	iface_params.ifname = iface_info.name.c_str();