Answer
In principle, if a Muslim country is attacked, they have a right to defend
themselves and declare Jihaad. Jihad will be Fardh-e-Ayn upon the males of
that area (in this instance, Afghanistan).
If they are unable to defend themselves, then the obligations extends to
those closest to them, Pakistan. Thereafter, the obligation will extend
accordingly.
and Allah Ta’ala Knows Best
Mufti Ebrahim Desai