Iran and the Taliban have long had their ups and downs. In 1998, the two sides nearly came to a direct clash when Taliban forces killed Iranian diplomats, though the incident ended without a major conflict.

However, the 2001 U.S. invasion of Afghanistan, the fear of a resurgent ISIS in Afghanistan and water issues have prompted Tehran to ramp up its engagement with the Taliban. This tactical alliance will enable Iran to further expand its influence in Afghanistan.

Iran has had covert contacts with the Taliban, the most dangerous terror group in Afghanistan, for many years. But recently, Ali Shamkhani, the secretary of Iran’s Supreme National Security Council (SNSC), went public with the relationship, claiming that contacts had been made with the aim of “curbing the security problems in Afghanistan.”

The announcement came as a surprise not because the public was unaware of Iran’s secret relations with the Taliban, but because Tehran has always tried to keep its ties to terror groups an “open secret” in an attempt to maintain plausible deniability. Why did Tehran decide to go public about the Taliban connection now?

A review of the relationship’s history may help to explain the mullahs’ thinking. During the period of Taliban rule, Iran saw the group as a threat to its interests. The two sides almost came to a direct clash in September 1998, when Taliban forces kidnapped and killed nine Iranian diplomats, and one journalist, in the Iranian consulate in Mazar-i-Sharif. Iran’s Islamic Revolutionary Guards (IRGC) vowed revenge and prepared to launch an all-out attack. But the crisis ended without a major clash, perhaps due to the fear that Islamabad would retaliate in support of the Taliban or that Afghanistan might become a quagmire for Iranian forces similar to that experienced by the Soviet Union in 1979-89.

The 2001 U.S.-led military operation that led to the collapse of Taliban rule prompted the Iranian leadership to reconsider its original calculation and recalibrate its approach. It welcomed high-level Taliban figures who escaped to Iran (e.g., Abdul Qayum Zakir and Mullah Naim Barich) and began extending support to Taliban fighters.

While the two sides are on different ends of the religious spectrum, Tehran views the Taliban as a useful point of leverage against the United States. The International Security Assistance Force (ISAF), a NATO force composed of American, British, Canadian and other troops, was created by the United Nations in 2002 and tasked with training the fledgling Afghan army and protecting the government of Hamid Karzai and his successor, Ashraf Ghani. The Iranian regime viewed the ISAF with concern, as it feared that the United States might use Afghanistan as a base from which to launch a kinetic attack on Iran. The Taliban insurgency thus became viewed by Tehran as a tool with which to keep American forces preoccupied.

To assist in the Taliban’s fighting of the ISAF, Iran allowed the Afghan terror group to open an office in Tehran and invited its leaders to attend a two-day International Islamic Unity Conference held by the World Forum for Proximity of Islamic Schools of Thought in Tehran.

Iran’s support for the Taliban did not terminate even when President Barack Obama assured the mullahs that the military option was no longer official U.S. policy towards Iran. Intelligence reports indicate that Tehran’s military and financial support for the Taliban has in fact escalated ever since. Afghan military officials have accused the Revolutionary Guards of providing military, financial, and logistical support to the terror group, to the extent that Tehran’s support enabled the Taliban to capture districts in western Afghanistan, including the provinces of Farah and Ghor, and the Taywara district. There are also reports indicating that Quds Force operatives had a “physical presence” in Ghor assisting Taliban fighters in their offensive against the central government.

Fighting ISAF was only one of the goals of the Quds Force in Afghanistan. Drug smuggling from Afghanistan to Iran has been a profitable business for the Quds Force, which is known for its extensive ties to drug cartels in South America. In 2012, the U.S. Department of the Treasury (DOT) designated Brig. Gen. Gholamreza Baghbani, the chief of the Quds Force in the Zahedan office, a narcotics trafficker. The DOT document noted that in return for Iranian business, Afghan traffickers moved weapons to the Taliban.

Financial incentives aside, the emergence of ISIS in Afghanistan, especially in provinces that border Iran, such as Herat, Farah and Nimruz—rattled the Iranian regime, prompting the leadership to ramp up its engagement with the Taliban. Unlike Al Qaeda and the more malleable Taliban, the radical anti-Shi’ite ISIS poses a real threat to Iran’s interests in Afghanistan. Providing better training for the Taliban was thus not only a way to undermine the American-led ISAF, but a barrier to a new ISIS caliphate across the Afghan border.

Various reports indicate that the IRGC created a training camp in South Khorasan province (Khorasan Jonobi) to train Taliban fighters, providing them with weapons and explosives. The Imam Khomeini Relief Foundation (Komite Emdad Imam Khomeini) in the same province is said to be donating untold amounts of capital to the terror group in addition to calling for volunteers to fight alongside Taliban forces.

Some observers have directly linked improvements in the Taliban’s performance, and ISIS’s consequent inability to establish a strong foothold in Afghanistan, to Iranian support. Since mid-2017, Taliban and ISIS forces have regularly clashed in eastern Nangarhar province, with the Taliban easily defeating ISIS thanks to the military support it has received from the Quds Force. As one commentator put it, the “scale, quality and length of training is unprecedented and marks not only a shift in the proxy war between the United States and Iran in Afghanistan, but also a potential change in Iran’s ability and will to affect the outcome of the Afghan war.”

Other commentators have noted that Iran’s backing of the Taliban’s assaults on government forces were linked to water issues. Iran has been attempting to enable the Taliban to derail energy projects that are currently under construction, namely the Poze Lich Hydropower plant in Ghor, and the Bakhshabad and Salma dams in the neighboring province of Farah and Herat, respectively. The construction of these dams, which would massively boost local energy and water supplies, is not acceptable to Iran. On July 5, 2017, President Hassan Rouhani declared that Iran “cannot remain indifferent to the issue [water dams], which will damage our environment.”

According to Rouhani, “construction of several dams in Afghanistan would affect Khorasan and Sistan-Baluchistan provinces,” and Tehran “is not going to stand idly by.”

It is worthy of note that the publicizing by Iran of its ties to the Taliban came days after reports appeared on talks between the United States and the Taliban over proposals for a ceasefire in Afghanistan. Iran is sending a message to Washington and Kabul that if its concerns are not addressed, it can sabotage any attempt at a permanent peace in Afghanistan. Certainly, given Iran’s ties to the Taliban and the new regional arrangements (i.e., Trump’s decision to withdraw half of U.S. forces from Afghanistan), Iran will be able to further expand its political, economic and sectarian influence in that country.

Dr. Farhad Rezaei is a member of the  Association for the Study of the Middle East and Africa (ASMEA) in Washington, D.C., and the co-author of Iran, Israel, and the United States: The Politics of Counter-Proliferation Intelligence (Rowman & Littlefield, N.Y.). @Farhadrezaeii