Similar to the US and its allies, Turkey relies on the right to self-defence and, more specifically, the„unwilling or unable“-doctrine, as the legal basis for striking against ISIS/ISIL inside Syria. Most interestingly, however, the letter to the Security Council does not refer to the threat emanating from Kurdish fighters but only ISIS/ISIL (Daesh):
The terrorist attack that took the lives of 32 Turkish citizens in Suruç on 20 July 2015 reaffirms that Turkey is under a clear and imminent threat of continuing attack from Daesh. Most recently, on 23 July 2015, Daesh attacked the border military post in Elbeyli and killed a Turkish soldier.
It is apparent that the regime in Syria is neither capable of nor willing to prevent these threats emanating from its territory, which clearly imperil the security of Turkey and the safety of its nationals.
Individual and collective self-defence is our inherent right under international law, as reflected in Article 51 of the Charter of the United Nations.
On this basis, Turkey has initiated necessary and proportionate military actions against Daesh in Syria, including in coordination with individual members of the Global Coalition, in order to counter the terrorist threat and to safeguard its territory and citizens.
The legal basis for its military strikes against Iraq, then, is the consent given by the Iraqi government:
His Excellency called on the Turkish government to coordinate with the Iraqi government about any military operation in those areas, stressing Iraq’s keenness on Turkey’s internal security and its people’s safety, praising the Turkish decision which allowed the use of its airbases by the international coalition to attack the terrorist Daesh organization.
Regarding the attacks against Kurdish fighters located in Iraq, however, the Iraqi government has voiced harsh criticism which comes close to a (partial) withdrawal of its consent, or at least a threat of doing so, denouncing these as „a dangerous escalation and an assault on Iraqi sovereignty“ and calling on Turkey to avoid further escalation and seek a resolution to the crisis.
As a reaction, Turkey has stated that Iraq was not fulfilling its duty to prevent any attacks/not to harbor such „terrorists“. All in all, it seems as if Turkey is stretching the Iraqi acceptance to conduct attacks against ISIS/ISIL and the Kurds on its territory quite far; however, as long as Iraq does not expliticitly withdraw its consent, there is a legal basis for the Turkish attack:
[…] the negative attitude adopted by the Iraqi Government regarding the steps taken by Turkey within the framework of international of law towards the terrorist attacks faced by our country and the operations conducted by Turkey, in this context, against the PKK terrorist organization located within the borders of Iraq has caused disappointment.
Although the Iraqi Government emphasizes its commitment for not allowing any attack towards Turkey from the Iraqi territories, it is clear that this commitment has not been fulfilled and numerous armed PKK militants have continued to harbor in the Iraqi territory for years. Therefore it is not possible to comprehend or accept that those, who cannot fulfill their commitments, do not have the possibility to keep their borders under control, and one-third of whose territories are under the control of the terrorist organization, have taken a stance against Turkey in its fight against the PKK terrorist organization perpetrating armed attacks towards its citizens and security forces.
That being said, although there is indeed a (somewhat shaky) legal justification – in line with those which have been invoked by other states in attacking ISIS/ISIL in the past, the problem is that Turkey does not restrict its attacks against this group. It is obvious that the balance between striking against ISIS/ISIL and, simultaneously, against the Kurds, the most reliable allies of the West and the US in particular, will cause political and, relatedly, perhaps also legal problems in the near future. Until now, however, Iraq would only withdraw or explicitly restrict its consent for military strikes by Turkey if the US allows it to do so (which seems highly unlikely for the time being).
Lastly, here are some good articles on the Syria/Iraq/ISIS/ISIL/Kurds-quagmire:
http://www.bbc.com/news/world-middle-east-33747980 (scroll down for some helpful infographics)
http://diepresse.com/home/politik/aussenpolitik/4785757/Analyse_Warum-Erdogan-die-PKK-angreift?_vl_backlink=%2Fhome%2Findex.do (in German)