The Kenyan capital became much more vulnerable to retaliation when Kenyan troops and tanks, supported by airstrikes, moved into Somalia in October 2011 in response to growing cross-border violence. Al-Shabaab immediately warned that the incursion would have "cataclysmic consequences."
What was meant to be a limited engagement dragged on. It took a year for Kenyan forces to capture the port of Kismayo, but in doing so they dramatically raised the stakes for Al-Shabaab. According to the U.N., Al-Shabaab used to collect an estimated $35 million to $50 million annually in custom tolls and taxes on businesses in Kismayo and two secondary ports higher up the coast -- about half its entire estimated annual income.
Its expulsion from Kismayo changed the dynamics for Al-Shabaab. Previously the group held off plotting large-scale attacks in Kenya because of Kenya's importance for recruitment, logistics and fund-raising. Al-Shabaab commanders realized a crackdown by law enforcement on Somali interests in Kenya would be devastating to the Somali business community, creating a backlash against it in Somalia. But after they lost control of Kismayo, the gloves came off.
In March, Al-Shabaab warned Kenyans they would not "sleep safely" in Nairobi as long as their soldiers were in Somalia. And in the midst of the siege, the group tweeted: "For long we have waged war against the Kenyans in our land, now it's time to shift the battleground and take the war to their land."
"The attack at #WestgateMall is just a very tiny fraction of what Muslims in Somalia experience at the hands of Kenyan invaders," another tweet said.
It's notable that Al-Shabaab was able to plan and train for such a sophisticated attack despite losing much territory in southern Somalia and around the capital, Mogadishu. As it has lost ground, the group has resorted to suicide bombings. Earlier this month it carried out a bomb attack against a restaurant popular with Westerners in Mogadishu, killing more than a dozen people.
A U.N. report issued in July noted that Al-Shabaab "has shifted its strategic posture to asymmetrical warfare in both urban centres and the countryside" but added that it "continues to control most of southern and central Somalia." The report estimated the military strength of Al-Shabaab at about 5,000 fighters, with a functioning chain of command, and said it had "preserved the core of its fighting force and resources."
After years of infighting and feuds, the Nairobi attack may also confirm the ascendancy of Al-Shabaab's most militant faction and its leader Mukhtar Abu al Zubayr (aka Ahmed Abdi Godane). Zubayr attended a madrassa in Pakistan as a young man and merged the group with al Qaeda in February 2012. He sees Al-Shabaab as part of al Qaeda's global jihad.
Dissenters have defected or been killed. Sheikh Hassan Dahir Aweys of Al-Shabaab's old guard surrendered to Somali authorities. And earlier this month Zubayr reportedly ordered the killing of two Western militants who were critical of his leadership style and had aligned themselves with Aweys -- Omar Hammami and Osama al Brittani. Hammami was an American from Alabama who had become a prominent mouthpiece for Al-Shabaab before publicly criticizing Zubayr last year.
Zubayr's increasingly tight grip on Al-Shabaab -- thanks to his ruthless use of the group's intelligence wing in hunting down opponents -- appears to have forestalled the collapse of Al-Shabaab, and may have made it more dangerous.
Zubayr has threatened a direct attack on the United States, and last year the U.S. offered a $7 million reward for information locating him. It would be very surprising if the attack in Nairobi did not receive his blessing, and it may be a sign of things to come as Al-Shabaab takes its war to other parts of East Africa.
Of more immediate concern to Kenyan authorities, in a country where political violence can explode quickly, is a likely backlash against Somali and Kenyan Muslims, which could create a new cycle of radicalization and unrest.