Merge remote-tracking branch 'goog/qcom/release/LA.UM.6.3.R1.00391.1' into HEAD

Change-Id: I2ed187125b06e4f2504a5b9122234a8c15b9734f
diff --git a/hal/src/HAL.cpp b/hal/src/HAL.cpp
index 0dd79e2..c4d9722 100644
--- a/hal/src/HAL.cpp
+++ b/hal/src/HAL.cpp
@@ -506,8 +506,7 @@
         BoolResult res = makeInputCheckFailure("Not initialized (setUpstreamParameters)");
         hidl_cb(res.success, res.errMsg);
         fl.setResult(res.success, res.errMsg);
-    }
-    else if (!v4AddrParser.addV4(v4Addr) && !v4Addr.empty()) {
+    } else if (!v4AddrParser.addV4(v4Addr) && !v4Addr.empty()) {
         BoolResult res = makeInputCheckFailure(v4AddrParser.getLastErrAsStr());
         hidl_cb(res.success, res.errMsg);
         fl.setResult(res.success, res.errMsg);
@@ -519,7 +518,7 @@
         BoolResult res = makeInputCheckFailure(v6GwParser.getLastErrAsStr());
         hidl_cb(res.success, res.errMsg);
         fl.setResult(res.success, res.errMsg);
-    } else {
+    } else if (iface.size()>= 1) {
         RET ipaReturn = mIPA->setUpstream(
                 iface.c_str(),
                 v4GwParser.getFirstPrefix(),
@@ -527,6 +526,15 @@
         BoolResult res = ipaResultToBoolResult(ipaReturn);
         hidl_cb(res.success, res.errMsg);
         fl.setResult(res.success, res.errMsg);
+    } else {
+	/* send NULL iface string when upstream down */
+        RET ipaReturn = mIPA->setUpstream(
+                NULL,
+                v4GwParser.getFirstPrefix(),
+                v6GwParser.getFirstPrefix());
+        BoolResult res = ipaResultToBoolResult(ipaReturn);
+        hidl_cb(res.success, res.errMsg);
+        fl.setResult(res.success, res.errMsg);
     }
 
     mLogs.addLog(fl);
diff --git a/ipacm/inc/IPACM_Config.h b/ipacm/inc/IPACM_Config.h
index 1aeeec5..a9524f6 100644
--- a/ipacm/inc/IPACM_Config.h
+++ b/ipacm/inc/IPACM_Config.h
@@ -252,6 +252,8 @@
 
 	int DelExtProp(ipa_ip_type ip_type);
 
+	enum ipa_hw_type GetIPAVer(bool get = false);
+
 	int Init(void);
 
 	inline bool isPrivateSubnet(uint32_t ip_addr)
@@ -347,6 +349,7 @@
 	static const char *DEVICE_NAME_ODU;
 
 private:
+	enum ipa_hw_type ver;
 	static IPACM_Config *pInstance;
 	static const char *DEVICE_NAME;
 	IPACM_Config(void);
diff --git a/ipacm/inc/IPACM_Conntrack_NATApp.h b/ipacm/inc/IPACM_Conntrack_NATApp.h
index e50b316..c3749e1 100644
--- a/ipacm/inc/IPACM_Conntrack_NATApp.h
+++ b/ipacm/inc/IPACM_Conntrack_NATApp.h
@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
+Copyright (c) 2013-2017, The Linux Foundation. All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are
@@ -102,11 +102,12 @@
 	bool isAlgPort(uint8_t, uint16_t);
 	void Reset();
 	bool isPwrSaveIf(uint32_t);
+	uint32_t GenerateMetdata(uint8_t mux_id);
 
 public:
 	static NatApp* GetInstance();
 
-	int AddTable(uint32_t);
+	int AddTable(uint32_t, uint8_t mux_id);
 	uint32_t GetTableHdl(uint32_t);
 	int DeleteTable(uint32_t);
 
diff --git a/ipacm/inc/IPACM_Defs.h b/ipacm/inc/IPACM_Defs.h
index fe252c9..2031fc5 100644
--- a/ipacm/inc/IPACM_Defs.h
+++ b/ipacm/inc/IPACM_Defs.h
@@ -341,6 +341,7 @@
 	uint32_t ipv6_prefix[2];
 	bool is_sta;
 	uint8_t xlat_mux_id;
+	uint8_t mux_id;
 }ipacm_event_iface_up;
 
 typedef struct _ipacm_event_iface_up_tether
diff --git a/ipacm/inc/IPACM_OffloadManager.h b/ipacm/inc/IPACM_OffloadManager.h
index 72070dd..bade0aa 100644
--- a/ipacm/inc/IPACM_OffloadManager.h
+++ b/ipacm/inc/IPACM_OffloadManager.h
@@ -96,7 +96,7 @@
 
 private:
 
-	std::list<const char *> valid_ifaces;
+	std::list<std::string> valid_ifaces;
 
 	bool upstream_v4_up;
 
diff --git a/ipacm/src/Android.mk b/ipacm/src/Android.mk
index 01715f8..4f00e8a 100644
--- a/ipacm/src/Android.mk
+++ b/ipacm/src/Android.mk
@@ -1,4 +1,5 @@
 BOARD_PLATFORM_LIST := test
+BOARD_PLATFORM_LIST += msmnile
 BOARD_IPAv3_LIST := msm8998
 BOARD_IPAv3_LIST += sdm845
 BOARD_IPAv3_LIST += sdm670
diff --git a/ipacm/src/IPACM_Config.cpp b/ipacm/src/IPACM_Config.cpp
index d3fa58d..92f4880 100644
--- a/ipacm/src/IPACM_Config.cpp
+++ b/ipacm/src/IPACM_Config.cpp
@@ -157,6 +157,7 @@
 	memset(flt_rule_count_v4, 0, IPA_CLIENT_MAX*sizeof(int));
 	memset(flt_rule_count_v6, 0, IPA_CLIENT_MAX*sizeof(int));
 	memset(bridge_mac, 0, IPA_MAC_ADDR_SIZE*sizeof(uint8_t));
+	ver = GetIPAVer(true);
 
 	IPACMDBG_H(" create IPACM_Config constructor\n");
 	return;
@@ -847,3 +848,21 @@
 
 	return ipacm_event_name[event_id];
 }
+
+enum ipa_hw_type IPACM_Config::GetIPAVer(bool get)
+{
+	int ret;
+
+	if(!get)
+		return ver;
+
+	ret = ioctl(m_fd, IPA_IOC_GET_HW_VERSION, &ver);
+	if(ret != 0)
+	{
+		IPACMERR("Failed to get IPA version with error %d.\n", ret);
+		ver = IPA_HW_None;
+		return IPA_HW_None;
+	}
+	IPACMDBG_H("IPA version is %d.\n", ver);
+	return ver;
+}
diff --git a/ipacm/src/IPACM_ConntrackListener.cpp b/ipacm/src/IPACM_ConntrackListener.cpp
index 4f83b3d..e02ca10 100644
--- a/ipacm/src/IPACM_ConntrackListener.cpp
+++ b/ipacm/src/IPACM_ConntrackListener.cpp
@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
+Copyright (c) 2013-2017, The Linux Foundation. All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are
@@ -373,7 +373,7 @@
 
 	 if(nat_inst != NULL)
 	 {
-		 nat_inst->AddTable(wanup_data->ipv4_addr);
+		 nat_inst->AddTable(wanup_data->ipv4_addr, wanup_data->mux_id);
 	 }
 
 	 IPACMDBG("creating nat threads\n");
diff --git a/ipacm/src/IPACM_Conntrack_NATApp.cpp b/ipacm/src/IPACM_Conntrack_NATApp.cpp
index c13c48e..27bf95a 100644
--- a/ipacm/src/IPACM_Conntrack_NATApp.cpp
+++ b/ipacm/src/IPACM_Conntrack_NATApp.cpp
@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
+Copyright (c) 2013-2017, The Linux Foundation. All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are
@@ -31,9 +31,13 @@
 #ifdef FEATURE_IPACM_HAL
 #include "IPACM_OffloadManager.h"
 #endif
+#include "IPACM_Iface.h"
 
 #define INVALID_IP_ADDR 0x0
 
+#define HDR_METADATA_MUX_ID_BMASK 0x00FF0000
+#define HDR_METADATA_MUX_ID_SHFT 0x10
+
 /* NatApp class Implementation */
 NatApp *NatApp::pInstance = NULL;
 NatApp::NatApp()
@@ -127,9 +131,14 @@
 	return pInstance;
 }
 
+uint32_t NatApp::GenerateMetdata(uint8_t mux_id)
+{
+	return (mux_id << HDR_METADATA_MUX_ID_SHFT) & HDR_METADATA_MUX_ID_BMASK;
+}
+
 /* NAT APP related object function definitions */
 
-int NatApp::AddTable(uint32_t pub_ip)
+int NatApp::AddTable(uint32_t pub_ip, uint8_t mux_id)
 {
 	int ret;
 	int cnt = 0;
@@ -151,6 +160,19 @@
 		IPACMERR("unable to create nat table Error:%d\n", ret);
 		return ret;
 	}
+	if(IPACM_Iface::ipacmcfg->GetIPAVer() >= IPA_HW_v4_0) {
+		/* modify PDN 0 so it will hold the mux ID in the src metadata field */
+		ipa_nat_pdn_entry entry;
+
+		entry.dst_metadata = 0;
+		entry.src_metadata = GenerateMetdata(mux_id);
+		entry.public_ip = pub_ip;
+		ret = ipa_nat_modify_pdn(nat_table_hdl, 0, &entry);
+		if(ret)
+		{
+			IPACMERR("unable to modify PDN 0 entry Error:%d INIT_HDR_METADATA register values will be used!\n", ret);
+		}
+	}
 
 	/* Add back the cached NAT-entry */
 	if (pub_ip == pub_ip_addr_pre)
diff --git a/ipacm/src/IPACM_Lan.cpp b/ipacm/src/IPACM_Lan.cpp
index 7acb668..2f18f38 100644
--- a/ipacm/src/IPACM_Lan.cpp
+++ b/ipacm/src/IPACM_Lan.cpp
@@ -1687,9 +1687,10 @@
 	IPACM_Config* ipacm_config = IPACM_Iface::ipacmcfg;
 	struct ipa_ioc_write_qmapid mux;
 
-	if(rx_prop != NULL)
+	/* not  needed for newer versions since it will be overridden by NAT metadata replacement for IPAv4 and up */
+	if((IPACM_Iface::ipacmcfg->GetIPAVer() < IPA_HW_v4_0) && (rx_prop != NULL))
 	{
-		/* give mud ID to IPA-driver for WLAN/LAN pkts */
+		/* give mux ID of the default PDN to IPA-driver for WLAN/LAN pkts */
 		fd = open(IPA_DEVICE_NAME, O_RDWR);
 		if (0 == fd)
 		{
@@ -2212,9 +2213,12 @@
 		   	    rt_rule_entry->rule.hdr_hdl = get_client_memptr(eth_client, eth_index)->hdr_hdl_v4;
 				rt_rule_entry->rule.attrib.u.v4.dst_addr = get_client_memptr(eth_client, eth_index)->v4_addr;
 				rt_rule_entry->rule.attrib.u.v4.dst_addr_mask = 0xFFFFFFFF;
-#ifdef FEATURE_IPA_V3
-				rt_rule_entry->rule.hashable = false;
-#endif
+
+				if(IPACM_Iface::ipacmcfg->GetIPAVer() >= IPA_HW_v4_0)
+				{
+					rt_rule_entry->rule.hashable = true;
+				}
+
 				if (false == m_routing.AddRoutingRule(rt_rule))
 				{
 					IPACMERR("Routing rule addition failed!\n");
@@ -3194,6 +3198,10 @@
 		else
 		{
 			flt_rule_entry.rule.action = IPA_PASS_TO_SRC_NAT;
+
+			/* NAT block will set the proper MUX ID in the metadata according to the relevant PDN */
+			if (IPACM_Iface::ipacmcfg->GetIPAVer() >= IPA_HW_v4_0)
+				flt_rule_entry.rule.set_metadata = true;
 		}
 	}
 	else if(iptype == IPA_IP_v6)
diff --git a/ipacm/src/IPACM_Main.cpp b/ipacm/src/IPACM_Main.cpp
index 5d0f83b..1f92bfa 100644
--- a/ipacm/src/IPACM_Main.cpp
+++ b/ipacm/src/IPACM_Main.cpp
@@ -700,6 +700,7 @@
 			if (OffloadMng->elrInstance == NULL) {
 				IPACMERR("OffloadMng->elrInstance is NULL, can't forward to framework!\n");
 			} else {
+				IPACMERR("calling OffloadMng->elrInstance->onLimitReached \n");
 				OffloadMng->elrInstance->onLimitReached();
 			}
 			continue;
@@ -709,6 +710,7 @@
 			if (OffloadMng->elrInstance == NULL) {
 				IPACMERR("OffloadMng->elrInstance is NULL, can't forward to framework!\n");
 			} else {
+				IPACMERR("calling OffloadMng->elrInstance->onOffloadStopped \n");
 				OffloadMng->elrInstance->onOffloadStopped(IpaEventRelay::ERROR);
 			}
 			continue;
@@ -718,6 +720,7 @@
 			if (OffloadMng->elrInstance == NULL) {
 				IPACMERR("OffloadMng->elrInstance is NULL, can't forward to framework!\n");
 			} else {
+				IPACMERR("calling OffloadMng->elrInstance->onOffloadSupportAvailable \n");
 				OffloadMng->elrInstance->onOffloadSupportAvailable();
 			}
 			continue;
diff --git a/ipacm/src/IPACM_OffloadManager.cpp b/ipacm/src/IPACM_OffloadManager.cpp
index 32606b9..4b85c6e 100644
--- a/ipacm/src/IPACM_OffloadManager.cpp
+++ b/ipacm/src/IPACM_OffloadManager.cpp
@@ -59,6 +59,8 @@
 	upstream_v6_up = false;
 	memset(event_cache, 0, MAX_EVENT_CACHE*sizeof(framework_event_cache));
 	latest_cache_index = 0;
+	elrInstance = NULL;
+	touInstance = NULL;
 	return ;
 }
 
@@ -213,6 +215,14 @@
 		return FAIL_INPUT_CHECK;
 	}
 
+	/* Iface is valid, add to list if not present */
+	if (std::find(valid_ifaces.begin(), valid_ifaces.end(), std::string(downstream_name)) == valid_ifaces.end())
+	{
+		/* Iface is new, add it to the list */
+		valid_ifaces.push_back(downstream_name);
+		IPACMDBG_H("add iface(%s) to list\n", downstream_name);
+	}
+
 	/* check if downstream netdev driver finished its configuration on IPA-HW */
 	if (IPACM_Iface::ipacmcfg->CheckNatIfaces(downstream_name))
 	{
@@ -263,13 +273,6 @@
 		return SUCCESS;
 	}
 
-	/* Iface is valid, add to list if not present */
-	if (std::find(valid_ifaces.begin(), valid_ifaces.end(), downstream_name) == valid_ifaces.end())
-	{
-		/* Iface is new, add it to the list */
-		valid_ifaces.push_back(downstream_name);
-	}
-
 	evt_data = (ipacm_event_ipahal_stream*)malloc(sizeof(ipacm_event_ipahal_stream));
 	if(evt_data == NULL)
 	{
@@ -303,7 +306,7 @@
 		IPACMERR("iface length is 0.\n");
 		return FAIL_HARDWARE;
 	}
-	if (std::find(valid_ifaces.begin(), valid_ifaces.end(), downstream_name) == valid_ifaces.end())
+	if (std::find(valid_ifaces.begin(), valid_ifaces.end(), std::string(downstream_name)) == valid_ifaces.end())
 	{
 		IPACMERR("iface is not present in list.\n");
 		return FAIL_HARDWARE;
@@ -508,6 +511,7 @@
 	upstream_v6_up = false;
 	memset(event_cache, 0, MAX_EVENT_CACHE*sizeof(framework_event_cache));
 	latest_cache_index = 0;
+	valid_ifaces.clear();
 	return result;
 }
 
diff --git a/ipacm/src/IPACM_Wan.cpp b/ipacm/src/IPACM_Wan.cpp
index 79f88cd..6d57c39 100644
--- a/ipacm/src/IPACM_Wan.cpp
+++ b/ipacm/src/IPACM_Wan.cpp
@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2013, The Linux Foundation. All rights reserved.
+Copyright (c) 2013, 2017 The Linux Foundation. All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are
@@ -1604,6 +1604,7 @@
 		{
 			IPACM_Wan::xlat_mux_id = 0;
 			wanup_data->xlat_mux_id = 0;
+			wanup_data->mux_id = ext_prop->ext[0].mux_id;
 			IPACMDBG_H("No xlat configuratio:\n");
 		}
 		evt_data.event = IPA_HANDLE_WAN_UP;
diff --git a/ipacm/src/IPACM_Wlan.cpp b/ipacm/src/IPACM_Wlan.cpp
index a594fe5..14fe0b7 100644
--- a/ipacm/src/IPACM_Wlan.cpp
+++ b/ipacm/src/IPACM_Wlan.cpp
@@ -1436,9 +1436,12 @@
 				rt_rule_entry->rule.hdr_hdl = get_client_memptr(wlan_client, wlan_index)->hdr_hdl_v4;
 				rt_rule_entry->rule.attrib.u.v4.dst_addr = get_client_memptr(wlan_client, wlan_index)->v4_addr;
 				rt_rule_entry->rule.attrib.u.v4.dst_addr_mask = 0xFFFFFFFF;
-#ifdef FEATURE_IPA_V3
-				rt_rule_entry->rule.hashable = false;
-#endif
+
+				if(IPACM_Iface::ipacmcfg->GetIPAVer() >= IPA_HW_v4_0)
+				{
+					rt_rule_entry->rule.hashable = true;
+				}
+
 				if (false == m_routing.AddRoutingRule(rt_rule))
 				{
 					IPACMERR("Routing rule addition failed!\n");
diff --git a/ipanat/src/Android.mk b/ipanat/src/Android.mk
index 157e40c..7a45a92 100644
--- a/ipanat/src/Android.mk
+++ b/ipanat/src/Android.mk
@@ -1,5 +1,6 @@
 BOARD_PLATFORM_LIST := msm8916
 BOARD_PLATFORM_LIST += msm8909
+BOARD_PLATFORM_LIST += msmnile
 ifneq ($(call is-board-platform-in-list,$(BOARD_PLATFORM_LIST)),true)
 ifneq (,$(filter $(QCOM_BOARD_PLATFORMS),$(TARGET_BOARD_PLATFORM)))
 ifneq (, $(filter aarch64 arm arm64, $(TARGET_ARCH)))
diff --git a/ipanat/src/ipa_nat_drvi.c b/ipanat/src/ipa_nat_drvi.c
index 9b06028..675dcf5 100644
--- a/ipanat/src/ipa_nat_drvi.c
+++ b/ipanat/src/ipa_nat_drvi.c
@@ -35,11 +35,16 @@
 #define strlcpy g_strlcpy
 #else
 #ifndef FEATURE_IPA_ANDROID
-static size_t strlcpy(char * dst, const char * src, size_t size) {
+static size_t strlcpy(char * dst, const char * src, size_t size)
+{
+	size_t i;
+
 	if (size < 1)
 		return 0;
-	strncpy(dst, src, size - 1);
-	dst[size - 1] = 0;
+	for (i = 0; i < (size - 1) && src[i] != '\0'; i++)
+		dst[i] = src[i];
+	for (; i < size; i++)
+		dst[i] = '\0';
 	return strlen(dst);
 }
 #endif