IPsec_L2TP.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715
  1. // SoftEther VPN Source Code
  2. // Cedar Communication Module
  3. //
  4. // SoftEther VPN Server, Client and Bridge are free software under GPLv2.
  5. //
  6. // Copyright (c) 2012-2015 Daiyuu Nobori.
  7. // Copyright (c) 2012-2015 SoftEther VPN Project, University of Tsukuba, Japan.
  8. // Copyright (c) 2012-2015 SoftEther Corporation.
  9. //
  10. // All Rights Reserved.
  11. //
  12. // http://www.softether.org/
  13. //
  14. // Author: Daiyuu Nobori
  15. // Comments: Tetsuo Sugiyama, Ph.D.
  16. //
  17. // This program is free software; you can redistribute it and/or
  18. // modify it under the terms of the GNU General Public License
  19. // version 2 as published by the Free Software Foundation.
  20. //
  21. // This program is distributed in the hope that it will be useful,
  22. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. // GNU General Public License for more details.
  25. //
  26. // You should have received a copy of the GNU General Public License version 2
  27. // along with this program; if not, write to the Free Software
  28. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  29. //
  30. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  31. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  32. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  33. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  34. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  35. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  36. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  37. //
  38. // THE LICENSE AGREEMENT IS ATTACHED ON THE SOURCE-CODE PACKAGE
  39. // AS "LICENSE.TXT" FILE. READ THE TEXT FILE IN ADVANCE TO USE THE SOFTWARE.
  40. //
  41. //
  42. // THIS SOFTWARE IS DEVELOPED IN JAPAN, AND DISTRIBUTED FROM JAPAN,
  43. // UNDER JAPANESE LAWS. YOU MUST AGREE IN ADVANCE TO USE, COPY, MODIFY,
  44. // MERGE, PUBLISH, DISTRIBUTE, SUBLICENSE, AND/OR SELL COPIES OF THIS
  45. // SOFTWARE, THAT ANY JURIDICAL DISPUTES WHICH ARE CONCERNED TO THIS
  46. // SOFTWARE OR ITS CONTENTS, AGAINST US (SOFTETHER PROJECT, SOFTETHER
  47. // CORPORATION, DAIYUU NOBORI OR OTHER SUPPLIERS), OR ANY JURIDICAL
  48. // DISPUTES AGAINST US WHICH ARE CAUSED BY ANY KIND OF USING, COPYING,
  49. // MODIFYING, MERGING, PUBLISHING, DISTRIBUTING, SUBLICENSING, AND/OR
  50. // SELLING COPIES OF THIS SOFTWARE SHALL BE REGARDED AS BE CONSTRUED AND
  51. // CONTROLLED BY JAPANESE LAWS, AND YOU MUST FURTHER CONSENT TO
  52. // EXCLUSIVE JURISDICTION AND VENUE IN THE COURTS SITTING IN TOKYO,
  53. // JAPAN. YOU MUST WAIVE ALL DEFENSES OF LACK OF PERSONAL JURISDICTION
  54. // AND FORUM NON CONVENIENS. PROCESS MAY BE SERVED ON EITHER PARTY IN
  55. // THE MANNER AUTHORIZED BY APPLICABLE LAW OR COURT RULE.
  56. //
  57. // USE ONLY IN JAPAN. DO NOT USE THIS SOFTWARE IN ANOTHER COUNTRY UNLESS
  58. // YOU HAVE A CONFIRMATION THAT THIS SOFTWARE DOES NOT VIOLATE ANY
  59. // CRIMINAL LAWS OR CIVIL RIGHTS IN THAT PARTICULAR COUNTRY. USING THIS
  60. // SOFTWARE IN OTHER COUNTRIES IS COMPLETELY AT YOUR OWN RISK. THE
  61. // SOFTETHER VPN PROJECT HAS DEVELOPED AND DISTRIBUTED THIS SOFTWARE TO
  62. // COMPLY ONLY WITH THE JAPANESE LAWS AND EXISTING CIVIL RIGHTS INCLUDING
  63. // PATENTS WHICH ARE SUBJECTS APPLY IN JAPAN. OTHER COUNTRIES' LAWS OR
  64. // CIVIL RIGHTS ARE NONE OF OUR CONCERNS NOR RESPONSIBILITIES. WE HAVE
  65. // NEVER INVESTIGATED ANY CRIMINAL REGULATIONS, CIVIL LAWS OR
  66. // INTELLECTUAL PROPERTY RIGHTS INCLUDING PATENTS IN ANY OF OTHER 200+
  67. // COUNTRIES AND TERRITORIES. BY NATURE, THERE ARE 200+ REGIONS IN THE
  68. // WORLD, WITH DIFFERENT LAWS. IT IS IMPOSSIBLE TO VERIFY EVERY
  69. // COUNTRIES' LAWS, REGULATIONS AND CIVIL RIGHTS TO MAKE THE SOFTWARE
  70. // COMPLY WITH ALL COUNTRIES' LAWS BY THE PROJECT. EVEN IF YOU WILL BE
  71. // SUED BY A PRIVATE ENTITY OR BE DAMAGED BY A PUBLIC SERVANT IN YOUR
  72. // COUNTRY, THE DEVELOPERS OF THIS SOFTWARE WILL NEVER BE LIABLE TO
  73. // RECOVER OR COMPENSATE SUCH DAMAGES, CRIMINAL OR CIVIL
  74. // RESPONSIBILITIES. NOTE THAT THIS LINE IS NOT LICENSE RESTRICTION BUT
  75. // JUST A STATEMENT FOR WARNING AND DISCLAIMER.
  76. //
  77. //
  78. // SOURCE CODE CONTRIBUTION
  79. // ------------------------
  80. //
  81. // Your contribution to SoftEther VPN Project is much appreciated.
  82. // Please send patches to us through GitHub.
  83. // Read the SoftEther VPN Patch Acceptance Policy in advance:
  84. // http://www.softether.org/5-download/src/9.patch
  85. //
  86. //
  87. // DEAR SECURITY EXPERTS
  88. // ---------------------
  89. //
  90. // If you find a bug or a security vulnerability please kindly inform us
  91. // about the problem immediately so that we can fix the security problem
  92. // to protect a lot of users around the world as soon as possible.
  93. //
  94. // Our e-mail address for security reports is:
  95. // softether-vpn-security [at] softether.org
  96. //
  97. // Please note that the above e-mail address is not a technical support
  98. // inquiry address. If you need technical assistance, please visit
  99. // http://www.softether.org/ and ask your question on the users forum.
  100. //
  101. // Thank you for your cooperation.
  102. //
  103. //
  104. // NO MEMORY OR RESOURCE LEAKS
  105. // ---------------------------
  106. //
  107. // The memory-leaks and resource-leaks verification under the stress
  108. // test has been passed before release this source code.
  109. // IPsec_L2TP.c
  110. // L2TP protocol stack
  111. #include "CedarPch.h"
  112. // Release the L2TP AVP value
  113. void FreeL2TPAVP(L2TP_AVP *a)
  114. {
  115. // Validate arguments
  116. if (a == NULL)
  117. {
  118. return;
  119. }
  120. if (a->Data != NULL)
  121. {
  122. Free(a->Data);
  123. }
  124. Free(a);
  125. }
  126. // Release the L2TP packet
  127. void FreeL2TPPacket(L2TP_PACKET *p)
  128. {
  129. UINT i;
  130. // Validate arguments
  131. if (p == NULL)
  132. {
  133. return;
  134. }
  135. if (p->AvpList != NULL)
  136. {
  137. for (i = 0;i < LIST_NUM(p->AvpList);i++)
  138. {
  139. L2TP_AVP *a = LIST_DATA(p->AvpList, i);
  140. FreeL2TPAVP(a);
  141. }
  142. ReleaseList(p->AvpList);
  143. }
  144. if (p->Data != NULL)
  145. {
  146. Free(p->Data);
  147. }
  148. Free(p);
  149. }
  150. // Send an L2TP control packet
  151. void SendL2TPControlPacket(L2TP_SERVER *l2tp, L2TP_TUNNEL *t, UINT session_id, L2TP_PACKET *p)
  152. {
  153. BUF *buf;
  154. L2TP_QUEUE *q;
  155. // Validate arguments
  156. if (l2tp == NULL || t == NULL || p == NULL)
  157. {
  158. return;
  159. }
  160. p->IsControl = true;
  161. p->TunnelId = t->TunnelId1;
  162. p->SessionId = session_id;
  163. p->Ns = t->NextNs;
  164. t->NextNs++;
  165. p->Nr = t->LastNr + 1;
  166. buf = BuildL2TPPacketData(p, t);
  167. q = ZeroMalloc(sizeof(L2TP_QUEUE));
  168. q->Buf = buf;
  169. q->Ns = p->Ns;
  170. q->NextSendTick = l2tp->Now + (UINT64)L2TP_PACKET_RESEND_INTERVAL;
  171. SendL2TPControlPacketMain(l2tp, t, q);
  172. L2TPAddInterrupt(l2tp, q->NextSendTick);
  173. Add(t->SendQueue, q);
  174. }
  175. // Specify the interrupt occurrence time of the next
  176. void L2TPAddInterrupt(L2TP_SERVER *l2tp, UINT64 next_tick)
  177. {
  178. // Validate arguments
  179. if (l2tp == NULL || next_tick == 0)
  180. {
  181. return;
  182. }
  183. AddInterrupt(l2tp->Interrupts, next_tick);
  184. }
  185. // Send a L2TP data packet
  186. void SendL2TPDataPacket(L2TP_SERVER *l2tp, L2TP_TUNNEL *t, L2TP_SESSION *s, void *data, UINT size)
  187. {
  188. UDPPACKET *p;
  189. UCHAR *buf;
  190. UINT buf_size;
  191. // Validate arguments
  192. if (l2tp == NULL || t == NULL || s == NULL || (size != 0 && data == NULL))
  193. {
  194. return;
  195. }
  196. // Build a L2TP data packet
  197. if (s->IsV3 == false)
  198. {
  199. // L2TP Ver 2
  200. buf_size = 8 + size;
  201. buf = Malloc(buf_size);
  202. buf[0] = 0x40;
  203. buf[1] = 0x02;
  204. WRITE_USHORT(buf + 2, buf_size);
  205. WRITE_USHORT(buf + 4, t->TunnelId1);
  206. WRITE_USHORT(buf + 6, s->SessionId1);
  207. Copy(buf + 8, data, size);
  208. // Transmission
  209. p = NewUdpPacket(&t->ServerIp, t->ServerPort, &t->ClientIp, t->ClientPort, buf, buf_size);
  210. }
  211. else
  212. {
  213. // L2TPv3
  214. if (t->IsYamahaV3 == false)
  215. {
  216. buf_size = 4 + size;
  217. buf = Malloc(buf_size);
  218. WRITE_UINT(buf, s->SessionId1);
  219. Copy(buf + 4, data, size);
  220. // Transmission
  221. p = NewUdpPacket(&t->ServerIp, IPSEC_PORT_L2TPV3_VIRTUAL, &t->ClientIp, IPSEC_PORT_L2TPV3_VIRTUAL, buf, buf_size);
  222. }
  223. else
  224. {
  225. UINT header = 0x00030000;
  226. buf_size = 8 + size;
  227. buf = Malloc(buf_size);
  228. WRITE_UINT(buf, header);
  229. WRITE_UINT(buf + 4, s->SessionId1);
  230. Copy(buf + 8, data, size);
  231. // Transmission
  232. p = NewUdpPacket(&t->ServerIp, t->ServerPort, &t->ClientIp, t->ClientPort, buf, buf_size);
  233. }
  234. }
  235. L2TPSendUDP(l2tp, p);
  236. }
  237. // L2TP packet transmission main
  238. void SendL2TPControlPacketMain(L2TP_SERVER *l2tp, L2TP_TUNNEL *t, L2TP_QUEUE *q)
  239. {
  240. UDPPACKET *p;
  241. // Validate arguments
  242. if (l2tp == NULL || t == NULL || q == NULL)
  243. {
  244. return;
  245. }
  246. p = NewUdpPacket(&t->ServerIp, t->ServerPort, &t->ClientIp, t->ClientPort,
  247. Clone(q->Buf->Buf, q->Buf->Size), q->Buf->Size);
  248. // Update the received sequence number
  249. WRITE_USHORT(((UCHAR *)p->Data) + (p->SrcPort == IPSEC_PORT_L2TPV3_VIRTUAL ? 14 : 10), t->LastNr + 1);
  250. L2TPSendUDP(l2tp, p);
  251. }
  252. // Send a UDP packet
  253. void L2TPSendUDP(L2TP_SERVER *l2tp, UDPPACKET *p)
  254. {
  255. // Validate arguments
  256. if (l2tp == NULL || p == NULL)
  257. {
  258. return;
  259. }
  260. Add(l2tp->SendPacketList, p);
  261. }
  262. // Build a L2TP packet
  263. BUF *BuildL2TPPacketData(L2TP_PACKET *pp, L2TP_TUNNEL *t)
  264. {
  265. BUF *ret;
  266. UCHAR c;
  267. USHORT us;
  268. UINT ui;
  269. // Validate arguments
  270. if (pp == NULL || t == NULL)
  271. {
  272. return NULL;
  273. }
  274. ret = NewBuf();
  275. c = 0;
  276. if (pp->Ver == 3)
  277. {
  278. if (pp->SessionId != 0)
  279. {
  280. // Add the Remote Session ID AVP
  281. L2TP_AVP *a = GetAVPValue(pp, L2TP_AVP_TYPE_V3_SESSION_ID_REMOTE);
  282. if (a == NULL || a->DataSize != sizeof(UINT))
  283. {
  284. UINT ui = Endian32(pp->SessionId);
  285. Add(pp->AvpList, NewAVP(L2TP_AVP_TYPE_V3_SESSION_ID_REMOTE, true, 0, &ui, sizeof(UINT)));
  286. if (GetAVPValueEx(pp, L2TPV3_CISCO_AVP_SESSION_ID_LOCAL, L2TP_AVP_VENDOR_ID_CISCO) != NULL)
  287. {
  288. Add(pp->AvpList, NewAVP(L2TPV3_CISCO_AVP_SESSION_ID_REMOTE, true, L2TP_AVP_VENDOR_ID_CISCO, &ui, sizeof(UINT)));
  289. }
  290. }
  291. }
  292. }
  293. if (pp->Ver == 3)
  294. {
  295. if (t->IsYamahaV3 == false)
  296. {
  297. // Zero as Session ID
  298. ui = 0;
  299. WriteBuf(ret, &ui, sizeof(UINT));
  300. }
  301. }
  302. // Flags
  303. if (pp->IsControl)
  304. {
  305. c |= L2TP_HEADER_BIT_TYPE;
  306. c |= L2TP_HEADER_BIT_LENGTH;
  307. c |= L2TP_HEADER_BIT_SEQUENCE;
  308. }
  309. else
  310. {
  311. c |= L2TP_HEADER_BIT_OFFSET;
  312. }
  313. if (pp->IsControl == false && pp->Ver == 3 && t->IsYamahaV3)
  314. {
  315. c = 0;
  316. }
  317. WriteBuf(ret, &c, 1);
  318. // Ver
  319. c = 2;
  320. if (pp->Ver == 3)
  321. {
  322. c = 3;
  323. }
  324. WriteBuf(ret, &c, 1);
  325. // Length
  326. if (pp->IsControl)
  327. {
  328. us = 0;
  329. WriteBuf(ret, &us, sizeof(USHORT));
  330. }
  331. // Reserved
  332. if (pp->IsControl == false && pp->Ver == 3 && t->IsYamahaV3)
  333. {
  334. us = 0;
  335. WriteBuf(ret, &us, sizeof(USHORT));
  336. }
  337. // Tunnel ID
  338. if (pp->Ver != 3)
  339. {
  340. us = Endian16((USHORT)pp->TunnelId);
  341. WriteBuf(ret, &us, sizeof(USHORT));
  342. }
  343. else
  344. {
  345. ui = Endian32(pp->TunnelId);
  346. WriteBuf(ret, &ui, sizeof(UINT));
  347. }
  348. // Session ID
  349. if (pp->Ver != 3)
  350. {
  351. us = Endian16((USHORT)pp->SessionId);
  352. WriteBuf(ret, &us, sizeof(USHORT));
  353. }
  354. if (pp->IsControl)
  355. {
  356. // Ns
  357. us = Endian16(pp->Ns);
  358. WriteBuf(ret, &us, sizeof(USHORT));
  359. // Nr
  360. us = Endian16(pp->Nr);
  361. WriteBuf(ret, &us, sizeof(USHORT));
  362. }
  363. else
  364. {
  365. if (!(pp->IsControl == false && pp->Ver == 3 && t->IsYamahaV3))
  366. {
  367. // Offset Size = 0
  368. us = 0;
  369. WriteBuf(ret, &us, sizeof(USHORT));
  370. }
  371. }
  372. if (pp->IsControl)
  373. {
  374. // AVP
  375. UINT i;
  376. for (i = 0;i < LIST_NUM(pp->AvpList);i++)
  377. {
  378. L2TP_AVP *a = LIST_DATA(pp->AvpList, i);
  379. // Length and Flags
  380. us = Endian16(a->DataSize + 6);
  381. if (a->Mandatory)
  382. {
  383. *((UCHAR *)&us) |= L2TP_AVP_BIT_MANDATORY;
  384. }
  385. WriteBuf(ret, &us, sizeof(USHORT));
  386. // Vendor ID
  387. us = Endian16(a->VendorID);
  388. WriteBuf(ret, &us, sizeof(USHORT));
  389. // Type
  390. us = Endian16(a->Type);
  391. WriteBuf(ret, &us, sizeof(USHORT));
  392. // Data
  393. WriteBuf(ret, a->Data, a->DataSize);
  394. }
  395. }
  396. else
  397. {
  398. // Payload
  399. WriteBuf(ret, pp->Data, pp->DataSize);
  400. }
  401. if (pp->IsControl)
  402. {
  403. // Update Length
  404. bool l2tpv3_non_yamaha = ((pp->Ver == 3) && (t->IsYamahaV3 == false));
  405. WRITE_USHORT(((UCHAR *)ret->Buf) + 2 + (l2tpv3_non_yamaha ? sizeof(UINT) : 0), (USHORT)(ret->Size - (l2tpv3_non_yamaha ? sizeof(UINT) : 0)));
  406. }
  407. SeekBuf(ret, 0, 0);
  408. return ret;
  409. }
  410. // Parse the L2TP packet
  411. L2TP_PACKET *ParseL2TPPacket(UDPPACKET *p)
  412. {
  413. L2TP_PACKET *ret;
  414. UCHAR *buf;
  415. UINT size;
  416. bool is_l2tpv3 = false;
  417. bool is_l2tpv3_yamaha = false;
  418. // Validate arguments
  419. if (p == NULL)
  420. {
  421. return NULL;
  422. }
  423. ret = ZeroMalloc(sizeof(L2TP_PACKET));
  424. if (p->SrcPort == IPSEC_PORT_L2TPV3_VIRTUAL)
  425. {
  426. // L2TPv3 (Cisco)
  427. is_l2tpv3 = true;
  428. }
  429. buf = p->Data;
  430. size = p->Size;
  431. if (size >= 2 && ((buf[1] & L2TP_HEADER_BIT_VER) == 3))
  432. {
  433. if (p->SrcPort != IPSEC_PORT_L2TPV3_VIRTUAL)
  434. {
  435. // L2TPv3 (YAMAHA)
  436. is_l2tpv3 = true;
  437. is_l2tpv3_yamaha = true;
  438. }
  439. }
  440. if (is_l2tpv3 && (is_l2tpv3_yamaha == false))
  441. {
  442. // L2TPv3 (Cisco)
  443. UINT session_id;
  444. if (size < 4)
  445. {
  446. goto LABEL_ERROR;
  447. }
  448. session_id = READ_UINT(buf);
  449. if (session_id != 0)
  450. {
  451. // L2TPv3 data packet reception
  452. ret->SessionId = session_id;
  453. buf += sizeof(UINT);
  454. size -= sizeof(UINT);
  455. ret->Data = Clone(buf, size);
  456. ret->DataSize = size;
  457. ret->Ver = 3;
  458. return ret;
  459. }
  460. else
  461. {
  462. // L2TPv3 control packet reception
  463. buf += sizeof(UINT);
  464. size -= sizeof(UINT);
  465. }
  466. }
  467. // L2TP
  468. if (size < 6)
  469. {
  470. goto LABEL_ERROR;
  471. }
  472. if (*buf & L2TP_HEADER_BIT_TYPE)
  473. {
  474. ret->IsControl = true;
  475. }
  476. if (*buf & L2TP_HEADER_BIT_LENGTH)
  477. {
  478. ret->HasLength = true;
  479. }
  480. if (*buf & L2TP_HEADER_BIT_SEQUENCE)
  481. {
  482. ret->HasSequence = true;
  483. }
  484. if (is_l2tpv3 == false)
  485. {
  486. if (*buf & L2TP_HEADER_BIT_OFFSET)
  487. {
  488. ret->HasOffset = true;
  489. }
  490. if (*buf & L2TP_HEADER_BIT_PRIORITY)
  491. {
  492. ret->IsPriority = true;
  493. }
  494. }
  495. buf++;
  496. size--;
  497. ret->Ver = *buf & L2TP_HEADER_BIT_VER;
  498. buf++;
  499. size--;
  500. if (is_l2tpv3 == false)
  501. {
  502. // L2TP
  503. if (ret->Ver != 2)
  504. {
  505. goto LABEL_ERROR;
  506. }
  507. }
  508. else
  509. {
  510. // L2TPv3
  511. if (ret->Ver != 3)
  512. {
  513. goto LABEL_ERROR;
  514. }
  515. }
  516. if (ret->IsControl)
  517. {
  518. if (ret->HasLength == false || ret->HasSequence == false)
  519. {
  520. goto LABEL_ERROR;
  521. }
  522. }
  523. else
  524. {
  525. /*if (ret->HasSequence)
  526. {
  527. goto LABEL_ERROR;
  528. }*/
  529. }
  530. if (ret->HasLength)
  531. {
  532. // Length
  533. if (size < 2)
  534. {
  535. goto LABEL_ERROR;
  536. }
  537. ret->Length = READ_USHORT(buf);
  538. buf += 2;
  539. size -= 2;
  540. if (size < (ret->Length - 4))
  541. {
  542. goto LABEL_ERROR;
  543. }
  544. size = ret->Length - 4;
  545. }
  546. if (is_l2tpv3)
  547. {
  548. if (p->SrcPort != IPSEC_PORT_L2TPV3_VIRTUAL)
  549. {
  550. if (ret->IsControl == false)
  551. {
  552. // Reserved
  553. if (size < 2)
  554. {
  555. goto LABEL_ERROR;
  556. }
  557. buf += 2;
  558. size -= 2;
  559. }
  560. }
  561. }
  562. // Tunnel ID, Session ID
  563. if (size < 4)
  564. {
  565. goto LABEL_ERROR;
  566. }
  567. if (is_l2tpv3 == false)
  568. {
  569. // L2TP
  570. ret->TunnelId = READ_USHORT(buf);
  571. buf += 2;
  572. size -= 2;
  573. ret->SessionId = READ_USHORT(buf);
  574. buf += 2;
  575. size -= 2;
  576. }
  577. else
  578. {
  579. // L2TPv3: Only tunnel ID is written in the header
  580. ret->TunnelId = READ_UINT(buf);
  581. buf += 4;
  582. size -= 4;
  583. // The session ID is not written in the header
  584. ret->SessionId = 0;
  585. if (ret->IsControl == false)
  586. {
  587. ret->SessionId = ret->TunnelId;
  588. }
  589. }
  590. if (ret->HasSequence)
  591. {
  592. // Ns, Nr
  593. if (size < 4)
  594. {
  595. goto LABEL_ERROR;
  596. }
  597. ret->Ns = READ_USHORT(buf);
  598. buf += 2;
  599. size -= 2;
  600. ret->Nr = READ_USHORT(buf);
  601. buf += 2;
  602. size -= 2;
  603. }
  604. if (ret->HasOffset)
  605. {
  606. // Offset
  607. if (size < 2)
  608. {
  609. goto LABEL_ERROR;
  610. }
  611. ret->OffsetSize = READ_USHORT(buf);
  612. buf += 2;
  613. size -= 2;
  614. if (size < ret->OffsetSize)
  615. {
  616. goto LABEL_ERROR;
  617. }
  618. buf += ret->OffsetSize;
  619. size -= ret->OffsetSize;
  620. }
  621. ret->DataSize = size;
  622. ret->Data = Clone(buf, ret->DataSize);
  623. if (ret->IsControl == false)
  624. {
  625. if (ret->DataSize == 0)
  626. {
  627. goto LABEL_ERROR;
  628. }
  629. }
  630. if (ret->IsControl)
  631. {
  632. if (ret->DataSize == 0)
  633. {
  634. ret->IsZLB = true;
  635. }
  636. }
  637. if (ret->IsControl)
  638. {
  639. ret->AvpList = NewListFast(NULL);
  640. // Parse the AVP field
  641. while (size != 0)
  642. {
  643. L2TP_AVP a;
  644. Zero(&a, sizeof(a));
  645. // Header
  646. if (size < 6)
  647. {
  648. goto LABEL_ERROR;
  649. }
  650. if (*buf & L2TP_AVP_BIT_MANDATORY)
  651. {
  652. a.Mandatory = true;
  653. }
  654. if (*buf & L2TP_AVP_BIT_HIDDEN)
  655. {
  656. goto LABEL_ERROR;
  657. }
  658. a.Length = READ_USHORT(buf) & L2TP_AVP_LENGTH;
  659. if (a.Length < 6)
  660. {
  661. goto LABEL_ERROR;
  662. }
  663. buf += 2;
  664. size -= 2;
  665. a.VendorID = READ_USHORT(buf);
  666. buf += 2;
  667. size -= 2;
  668. a.Type = READ_USHORT(buf);
  669. buf += 2;
  670. size -= 2;
  671. a.DataSize = a.Length - 6;
  672. a.Data = Clone(buf, a.DataSize);
  673. buf += a.DataSize;
  674. size -= a.DataSize;
  675. Add(ret->AvpList, Clone(&a, sizeof(a)));
  676. }
  677. }
  678. if (ret->IsControl && ret->IsZLB == false)
  679. {
  680. // Get the MessageType in the case of Control packet
  681. L2TP_AVP *a = GetAVPValue(ret, L2TP_AVP_TYPE_MESSAGE_TYPE);
  682. if (a == NULL || a->DataSize != 2)
  683. {
  684. goto LABEL_ERROR;
  685. }
  686. ret->MessageType = READ_USHORT(a->Data);
  687. }
  688. if (ret->Ver == 3 && ret->IsControl)
  689. {
  690. // Get the Remote Session ID in the case of L2TPv3
  691. L2TP_AVP *a = GetAVPValue(ret, L2TP_AVP_TYPE_V3_SESSION_ID_REMOTE);
  692. if (a != NULL && a->DataSize == sizeof(UINT))
  693. {
  694. ret->SessionId = READ_UINT(a->Data);
  695. }
  696. }
  697. ret->IsYamahaV3 = is_l2tpv3_yamaha;
  698. return ret;
  699. LABEL_ERROR:
  700. FreeL2TPPacket(ret);
  701. return NULL;
  702. }
  703. // Get the AVP value
  704. L2TP_AVP *GetAVPValue(L2TP_PACKET *p, UINT type)
  705. {
  706. return GetAVPValueEx(p, type, 0);
  707. }
  708. L2TP_AVP *GetAVPValueEx(L2TP_PACKET *p, UINT type, UINT vendor_id)
  709. {
  710. UINT i;
  711. // Validate arguments
  712. if (p == NULL)
  713. {
  714. return NULL;
  715. }
  716. for (i = 0;i < LIST_NUM(p->AvpList);i++)
  717. {
  718. L2TP_AVP *a = LIST_DATA(p->AvpList, i);
  719. if (a->Type == type && a->VendorID == vendor_id)
  720. {
  721. return a;
  722. }
  723. }
  724. if (vendor_id == 0)
  725. {
  726. if (type == L2TP_AVP_TYPE_V3_TUNNEL_ID)
  727. {
  728. return GetAVPValueEx(p, L2TPV3_CISCO_AVP_TUNNEL_ID, L2TP_AVP_VENDOR_ID_CISCO);
  729. }
  730. else if (type == L2TP_AVP_TYPE_V3_SESSION_ID_LOCAL)
  731. {
  732. return GetAVPValueEx(p, L2TPV3_CISCO_AVP_SESSION_ID_LOCAL, L2TP_AVP_VENDOR_ID_CISCO);
  733. }
  734. else if (type == L2TP_AVP_TYPE_V3_SESSION_ID_REMOTE)
  735. {
  736. return GetAVPValueEx(p, L2TPV3_CISCO_AVP_SESSION_ID_REMOTE, L2TP_AVP_VENDOR_ID_CISCO);
  737. }
  738. }
  739. return NULL;
  740. }
  741. // Release the L2TP transmission queue
  742. void FreeL2TPQueue(L2TP_QUEUE *q)
  743. {
  744. // Validate arguments
  745. if (q == NULL)
  746. {
  747. return;
  748. }
  749. FreeBuf(q->Buf);
  750. FreeL2TPPacket(q->L2TPPacket);
  751. Free(q);
  752. }
  753. // Sort function of L2TP reception queue
  754. int CmpL2TPQueueForRecv(void *p1, void *p2)
  755. {
  756. L2TP_QUEUE *q1, *q2;
  757. // Validate arguments
  758. if (p1 == NULL || p2 == NULL)
  759. {
  760. return 0;
  761. }
  762. q1 = *(L2TP_QUEUE **)p1;
  763. q2 = *(L2TP_QUEUE **)p2;
  764. if (q1 == NULL || q2 == NULL)
  765. {
  766. return 0;
  767. }
  768. if (L2TP_SEQ_LT(q1->Ns, q2->Ns))
  769. {
  770. return -1;
  771. }
  772. else if (q1->Ns == q2->Ns)
  773. {
  774. return 0;
  775. }
  776. else
  777. {
  778. return 1;
  779. }
  780. }
  781. // Create a L2TP tunnel
  782. L2TP_TUNNEL *NewL2TPTunnel(L2TP_SERVER *l2tp, L2TP_PACKET *p, UDPPACKET *udp)
  783. {
  784. L2TP_TUNNEL *t;
  785. L2TP_AVP *a;
  786. // Validate arguments
  787. if (l2tp == NULL || p == NULL || udp == NULL)
  788. {
  789. return NULL;
  790. }
  791. t = ZeroMalloc(sizeof(L2TP_TUNNEL));
  792. if (p->Ver == 3)
  793. {
  794. t->IsV3 = true;
  795. }
  796. t->SessionList = NewList(NULL);
  797. Copy(&t->ClientIp, &udp->SrcIP, sizeof(IP));
  798. t->ClientPort = udp->SrcPort;
  799. Copy(&t->ServerIp, &udp->DstIP, sizeof(IP));
  800. t->ServerPort = udp->DestPort;
  801. // Hostname
  802. a = GetAVPValue(p, L2TP_AVP_TYPE_HOST_NAME);
  803. if (a != NULL && a->DataSize >= 1 && a->DataSize < sizeof(t->HostName))
  804. {
  805. Copy(t->HostName, a->Data, a->DataSize);
  806. }
  807. else
  808. {
  809. IPToStr(t->HostName, sizeof(t->HostName), &t->ClientIp);
  810. }
  811. // Vendor Name
  812. a = GetAVPValue(p, L2TP_AVP_TYPE_VENDOR_NAME);
  813. if (a != NULL && a->DataSize >= 1 && a->DataSize < sizeof(t->VendorName))
  814. {
  815. Copy(t->VendorName, a->Data, a->DataSize);
  816. }
  817. // Assigned Tunnel ID
  818. a = GetAVPValue(p, (p->Ver == 3 ? L2TP_AVP_TYPE_V3_TUNNEL_ID : L2TP_AVP_TYPE_ASSIGNED_TUNNEL));
  819. if (a == NULL || a->DataSize != (t->IsV3 ? sizeof(UINT) : sizeof(USHORT)))
  820. {
  821. goto LABEL_ERROR;
  822. }
  823. t->TunnelId1 = (t->IsV3 ? READ_UINT(a->Data) : READ_USHORT(a->Data));
  824. t->TunnelId2 = GenerateNewTunnelIdEx(l2tp, &t->ClientIp, t->IsV3);
  825. if (t->TunnelId2 == 0)
  826. {
  827. goto LABEL_ERROR;
  828. }
  829. if (p->Ver == 3)
  830. {
  831. // Identify whether it's Cisco
  832. a = GetAVPValueEx(p, L2TPV3_CISCO_AVP_TUNNEL_ID, L2TP_AVP_VENDOR_ID_CISCO);
  833. if (a != NULL)
  834. {
  835. t->IsCiscoV3 = true;
  836. }
  837. // L2TPv3 on YAMAHA
  838. t->IsYamahaV3 = p->IsYamahaV3;
  839. }
  840. // Transmission queue
  841. t->SendQueue = NewList(NULL);
  842. // Reception queue
  843. t->RecvQueue = NewList(CmpL2TPQueueForRecv);
  844. t->LastRecvTick = l2tp->Now;
  845. t->LastHelloSent = l2tp->Now;
  846. return t;
  847. LABEL_ERROR:
  848. FreeL2TPTunnel(t);
  849. return NULL;
  850. }
  851. // Search a tunnel
  852. L2TP_TUNNEL *GetTunnelFromId(L2TP_SERVER *l2tp, IP *client_ip, UINT tunnel_id, bool is_v3)
  853. {
  854. UINT i;
  855. // Validate arguments
  856. if (l2tp == NULL || client_ip == 0 || tunnel_id == 0)
  857. {
  858. return NULL;
  859. }
  860. for (i = 0;i < LIST_NUM(l2tp->TunnelList);i++)
  861. {
  862. L2TP_TUNNEL *t = LIST_DATA(l2tp->TunnelList, i);
  863. if (t->TunnelId2 == tunnel_id && CmpIpAddr(&t->ClientIp, client_ip) == 0)
  864. {
  865. if (EQUAL_BOOL(t->IsV3, is_v3))
  866. {
  867. return t;
  868. }
  869. }
  870. }
  871. return NULL;
  872. }
  873. // Search the tunnel by the tunnel ID that is assigned by the client
  874. L2TP_TUNNEL *GetTunnelFromIdOfAssignedByClient(L2TP_SERVER *l2tp, IP *client_ip, UINT tunnel_id)
  875. {
  876. UINT i;
  877. // Validate arguments
  878. if (l2tp == NULL || client_ip == 0 || tunnel_id == 0)
  879. {
  880. return NULL;
  881. }
  882. for (i = 0;i < LIST_NUM(l2tp->TunnelList);i++)
  883. {
  884. L2TP_TUNNEL *t = LIST_DATA(l2tp->TunnelList, i);
  885. if (t->TunnelId1 == tunnel_id && CmpIpAddr(&t->ClientIp, client_ip) == 0)
  886. {
  887. return t;
  888. }
  889. }
  890. return NULL;
  891. }
  892. L2TP_TUNNEL *GetTunnelFromIdOfAssignedByClientEx(L2TP_SERVER *l2tp, IP *client_ip, UINT tunnel_id, bool is_v3)
  893. {
  894. UINT i;
  895. // Validate arguments
  896. if (l2tp == NULL || client_ip == 0 || tunnel_id == 0)
  897. {
  898. return NULL;
  899. }
  900. for (i = 0;i < LIST_NUM(l2tp->TunnelList);i++)
  901. {
  902. L2TP_TUNNEL *t = LIST_DATA(l2tp->TunnelList, i);
  903. if (t->TunnelId1 == tunnel_id && CmpIpAddr(&t->ClientIp, client_ip) == 0)
  904. {
  905. if (EQUAL_BOOL(t->IsV3, is_v3))
  906. {
  907. return t;
  908. }
  909. }
  910. }
  911. return NULL;
  912. }
  913. // Create a new tunnel ID
  914. UINT GenerateNewTunnelId(L2TP_SERVER *l2tp, IP *client_ip)
  915. {
  916. return GenerateNewTunnelIdEx(l2tp, client_ip, false);
  917. }
  918. UINT GenerateNewTunnelIdEx(L2TP_SERVER *l2tp, IP *client_ip, bool is_32bit)
  919. {
  920. UINT id;
  921. UINT max_number = 0xffff;
  922. // Validate arguments
  923. if (l2tp == NULL || client_ip == NULL)
  924. {
  925. return 0;
  926. }
  927. if (is_32bit)
  928. {
  929. max_number = 0xfffffffe;
  930. }
  931. for (id = 1;id <= max_number;id++)
  932. {
  933. if (GetTunnelFromId(l2tp, client_ip, id, is_32bit) == NULL)
  934. {
  935. return id;
  936. }
  937. }
  938. return 0;
  939. }
  940. // Release the L2TP tunnel
  941. void FreeL2TPTunnel(L2TP_TUNNEL *t)
  942. {
  943. UINT i;
  944. // Validate arguments
  945. if (t == NULL)
  946. {
  947. return;
  948. }
  949. for (i = 0;i < LIST_NUM(t->SessionList);i++)
  950. {
  951. L2TP_SESSION *s = LIST_DATA(t->SessionList, i);
  952. FreeL2TPSession(s);
  953. }
  954. ReleaseList(t->SessionList);
  955. for (i = 0;i < LIST_NUM(t->SendQueue);i++)
  956. {
  957. L2TP_QUEUE *q = LIST_DATA(t->SendQueue, i);
  958. FreeL2TPQueue(q);
  959. }
  960. ReleaseList(t->SendQueue);
  961. for (i = 0;i < LIST_NUM(t->RecvQueue);i++)
  962. {
  963. L2TP_QUEUE *q = LIST_DATA(t->RecvQueue, i);
  964. FreeL2TPQueue(q);
  965. }
  966. ReleaseList(t->RecvQueue);
  967. Free(t);
  968. }
  969. // Generate a new L2TP control packet
  970. L2TP_PACKET *NewL2TPControlPacket(UINT message_type, bool is_v3)
  971. {
  972. L2TP_PACKET *p = ZeroMalloc(sizeof(L2TP_PACKET));
  973. p->IsControl = true;
  974. p->HasLength = true;
  975. p->HasSequence = true;
  976. p->Ver = (is_v3 ? 3 : 2);
  977. p->MessageType = message_type;
  978. p->AvpList = NewListFast(NULL);
  979. if (message_type != 0)
  980. {
  981. L2TP_AVP *a;
  982. USHORT us;
  983. a = ZeroMalloc(sizeof(L2TP_AVP));
  984. a->Type = L2TP_AVP_TYPE_MESSAGE_TYPE;
  985. a->Mandatory = true;
  986. us = Endian16(message_type);
  987. a->Data = Clone(&us, sizeof(USHORT));
  988. a->DataSize = sizeof(USHORT);
  989. Add(p->AvpList, a);
  990. }
  991. return p;
  992. }
  993. // Create a new AVP value
  994. L2TP_AVP *NewAVP(USHORT type, bool mandatory, USHORT vendor_id, void *data, UINT data_size)
  995. {
  996. L2TP_AVP *a;
  997. // Validate arguments
  998. if (data_size != 0 && data == NULL)
  999. {
  1000. return NULL;
  1001. }
  1002. a = ZeroMalloc(sizeof(L2TP_AVP));
  1003. a->Type = type;
  1004. a->Mandatory = mandatory;
  1005. a->VendorID = vendor_id;
  1006. a->Data = Clone(data, data_size);
  1007. a->DataSize = data_size;
  1008. return a;
  1009. }
  1010. // Process a received L2TP packet
  1011. void L2TPProcessRecvControlPacket(L2TP_SERVER *l2tp, L2TP_TUNNEL *t, L2TP_PACKET *p)
  1012. {
  1013. // Validate arguments
  1014. if (l2tp == NULL || t == NULL || p == NULL)
  1015. {
  1016. return;
  1017. }
  1018. if (p->SessionId == 0)
  1019. {
  1020. if (p->MessageType == L2TP_MESSAGE_TYPE_SCCCN && l2tp->Halt == false)
  1021. {
  1022. // Tunnel establishment completed
  1023. if (t->Established == false)
  1024. {
  1025. if (t->Disconnecting == false)
  1026. {
  1027. t->Established = true;
  1028. t->LastHelloSent = l2tp->Now;
  1029. }
  1030. }
  1031. }
  1032. if (t->Established)
  1033. {
  1034. if (p->MessageType == L2TP_MESSAGE_TYPE_ICRQ && t->WantToDisconnect == false && l2tp->Halt == false)
  1035. {
  1036. // Request to establish a new session arrives
  1037. L2TP_AVP *a = GetAVPValue(p,
  1038. (t->IsV3 ? L2TP_AVP_TYPE_V3_SESSION_ID_LOCAL : L2TP_AVP_TYPE_ASSIGNED_SESSION));
  1039. if (a != NULL && a->DataSize == (t->IsV3 ? sizeof(UINT) : sizeof(USHORT)) && IsZero(a->Data, (t->IsV3 ? sizeof(UINT) : sizeof(USHORT))) == false)
  1040. {
  1041. UINT session_id = (t->IsV3 ? READ_UINT(a->Data) : READ_USHORT(a->Data));
  1042. // Check whether there is other same session ID
  1043. if (GetSessionFromIdAssignedByClient(t, session_id) == NULL)
  1044. {
  1045. // Create a session
  1046. L2TP_SESSION *s = NewL2TPSession(l2tp, t, session_id);
  1047. if (s != NULL)
  1048. {
  1049. L2TP_PACKET *pp;
  1050. USHORT us;
  1051. UINT ui;
  1052. // Get the PseudowireType
  1053. if (t->IsV3)
  1054. {
  1055. s->PseudowireType = L2TPV3_PW_TYPE_ETHERNET;
  1056. a = GetAVPValue(p, L2TP_AVP_TYPE_V3_PW_TYPE);
  1057. if (a != NULL && a->DataSize == sizeof(USHORT))
  1058. {
  1059. ui = READ_USHORT(a->Data);
  1060. s->PseudowireType = ui;
  1061. }
  1062. }
  1063. Add(t->SessionList, s);
  1064. Debug("L2TP New Session: ID = %u/%u on Tunnel %u/%u\n", s->SessionId1, s->SessionId2,
  1065. t->TunnelId1, t->TunnelId2);
  1066. // Respond the session creation completion notice
  1067. pp = NewL2TPControlPacket(L2TP_MESSAGE_TYPE_ICRP, s->IsV3);
  1068. // Assigned Session AVP
  1069. if (s->IsV3 == false)
  1070. {
  1071. us = Endian16(s->SessionId2);
  1072. Add(pp->AvpList, NewAVP(L2TP_AVP_TYPE_ASSIGNED_SESSION, true, 0, &us, sizeof(USHORT)));
  1073. }
  1074. else
  1075. {
  1076. ui = Endian32(s->SessionId2);
  1077. Add(pp->AvpList, NewAVP(L2TP_AVP_TYPE_V3_SESSION_ID_LOCAL, true, 0, &ui, sizeof(UINT)));
  1078. if (s->IsCiscoV3)
  1079. {
  1080. Add(pp->AvpList, NewAVP(L2TPV3_CISCO_AVP_SESSION_ID_LOCAL, true, L2TP_AVP_VENDOR_ID_CISCO, &ui, sizeof(UINT)));
  1081. }
  1082. }
  1083. if (s->IsV3)
  1084. {
  1085. if (t->IsYamahaV3 == false)
  1086. {
  1087. // Pseudowire AVP
  1088. us = Endian16(s->PseudowireType);
  1089. Add(pp->AvpList, NewAVP(L2TP_AVP_TYPE_V3_PW_TYPE, true, 0, &us, sizeof(USHORT)));
  1090. }
  1091. if (s->IsCiscoV3)
  1092. {
  1093. Add(pp->AvpList, NewAVP(L2TPV3_CISCO_AVP_PW_TYPE, true, L2TP_AVP_VENDOR_ID_CISCO, &us, sizeof(USHORT)));
  1094. }
  1095. if (t->IsYamahaV3)
  1096. {
  1097. us = Endian16(0x0003);
  1098. Add(pp->AvpList, NewAVP(L2TP_AVP_TYPE_V3_CIRCUIT_STATUS, true, 0, &us, sizeof(USHORT)));
  1099. }
  1100. }
  1101. SendL2TPControlPacket(l2tp, t, session_id, pp);
  1102. FreeL2TPPacket(pp);
  1103. }
  1104. }
  1105. }
  1106. }
  1107. else if (p->MessageType == L2TP_MESSAGE_TYPE_STOPCCN)
  1108. {
  1109. // Tunnel disconnect request arrives
  1110. L2TP_AVP *a = GetAVPValue(p, (t->IsV3 ? L2TP_AVP_TYPE_V3_TUNNEL_ID : L2TP_AVP_TYPE_ASSIGNED_TUNNEL));
  1111. if (a != NULL && a->DataSize == (t->IsV3 ? sizeof(UINT) : sizeof(USHORT)))
  1112. {
  1113. UINT ui = (t->IsV3 ? READ_UINT(a->Data) : READ_USHORT(a->Data));
  1114. if (ui == t->TunnelId1)
  1115. {
  1116. // Disconnect the tunnel
  1117. DisconnectL2TPTunnel(t);
  1118. }
  1119. }
  1120. }
  1121. }
  1122. }
  1123. else
  1124. {
  1125. // Search a session
  1126. L2TP_SESSION *s = GetSessionFromId(t, p->SessionId);
  1127. if (s != NULL)
  1128. {
  1129. if (s->Established == false)
  1130. {
  1131. if (p->MessageType == L2TP_MESSAGE_TYPE_ICCN)
  1132. {
  1133. // Session establishment completed
  1134. if (s->Disconnecting == false)
  1135. {
  1136. s->Established = true;
  1137. }
  1138. }
  1139. }
  1140. else
  1141. {
  1142. if (p->MessageType == L2TP_MESSAGE_TYPE_CDN)
  1143. {
  1144. // Received a session disconnection request
  1145. L2TP_AVP *a = GetAVPValue(p,
  1146. (t->IsV3 ? L2TP_AVP_TYPE_V3_SESSION_ID_LOCAL : L2TP_AVP_TYPE_ASSIGNED_SESSION));
  1147. if (a != NULL && a->DataSize == (t->IsV3 ? sizeof(UINT) : sizeof(USHORT)))
  1148. {
  1149. UINT ui = (t->IsV3 ? READ_UINT(a->Data) : READ_USHORT(a->Data));
  1150. if (ui == s->SessionId1)
  1151. {
  1152. // Disconnect the session
  1153. DisconnectL2TPSession(t, s);
  1154. }
  1155. }
  1156. }
  1157. }
  1158. }
  1159. else
  1160. {
  1161. Debug("Session ID %u not found in Tunnel ID %u/%u\n", p->SessionId, t->TunnelId1, t->TunnelId2);
  1162. }
  1163. }
  1164. }
  1165. // Disconnect the L2TP tunnel
  1166. void DisconnectL2TPTunnel(L2TP_TUNNEL *t)
  1167. {
  1168. // Validate arguments
  1169. if (t == NULL)
  1170. {
  1171. return;
  1172. }
  1173. if (/*t->Established && */t->Disconnecting == false && t->WantToDisconnect == false)
  1174. {
  1175. UINT i;
  1176. Debug("Trying to Disconnect Tunnel ID %u/%u\n", t->TunnelId1, t->TunnelId2);
  1177. t->WantToDisconnect = true;
  1178. // Disconnect all sessions within the tunnel
  1179. for (i = 0;i < LIST_NUM(t->SessionList);i++)
  1180. {
  1181. L2TP_SESSION *s = LIST_DATA(t->SessionList, i);
  1182. DisconnectL2TPSession(t, s);
  1183. }
  1184. }
  1185. }
  1186. // Disconnect the L2TP session
  1187. void DisconnectL2TPSession(L2TP_TUNNEL *t, L2TP_SESSION *s)
  1188. {
  1189. // Validate arguments
  1190. if (t == NULL || s == NULL)
  1191. {
  1192. return;
  1193. }
  1194. if (s->Established && s->Disconnecting == false && s->WantToDisconnect == false)
  1195. {
  1196. Debug("Trying to Disconnect Session ID %u/%u on Tunnel %u/%u\n", s->SessionId1, s->SessionId2,
  1197. t->TunnelId1, t->TunnelId2);
  1198. s->WantToDisconnect = true;
  1199. }
  1200. }
  1201. // Create a new session
  1202. L2TP_SESSION *NewL2TPSession(L2TP_SERVER *l2tp, L2TP_TUNNEL *t, UINT session_id_by_client)
  1203. {
  1204. L2TP_SESSION *s;
  1205. UINT session_id_by_server;
  1206. // Validate arguments
  1207. if (l2tp == NULL || t == NULL || session_id_by_client == 0)
  1208. {
  1209. return NULL;
  1210. }
  1211. if (LIST_NUM(t->SessionList) >= L2TP_QUOTA_MAX_NUM_SESSIONS_PER_TUNNEL)
  1212. {
  1213. return NULL;
  1214. }
  1215. if (t->IsV3 == false)
  1216. {
  1217. session_id_by_server = GenerateNewSessionIdEx(t, t->IsV3);
  1218. }
  1219. else
  1220. {
  1221. session_id_by_server = GenerateNewSessionIdForL2TPv3(l2tp);
  1222. }
  1223. if (session_id_by_server == 0)
  1224. {
  1225. return NULL;
  1226. }
  1227. s = ZeroMalloc(sizeof(L2TP_SESSION));
  1228. s->SessionId1 = session_id_by_client;
  1229. s->SessionId2 = session_id_by_server;
  1230. s->IsV3 = t->IsV3;
  1231. s->IsCiscoV3 = t->IsCiscoV3;
  1232. s->Tunnel = t;
  1233. return s;
  1234. }
  1235. // Retrieve a session from L2TP session ID
  1236. L2TP_SESSION *SearchL2TPSessionById(L2TP_SERVER *l2tp, bool is_v3, UINT id)
  1237. {
  1238. UINT i, j;
  1239. // Validate arguments
  1240. if (l2tp == NULL || id == 0)
  1241. {
  1242. return NULL;
  1243. }
  1244. for (i = 0;i < LIST_NUM(l2tp->TunnelList);i++)
  1245. {
  1246. L2TP_TUNNEL *t = LIST_DATA(l2tp->TunnelList, i);
  1247. for (j = 0;j < LIST_NUM(t->SessionList);j++)
  1248. {
  1249. L2TP_SESSION *s = LIST_DATA(t->SessionList, j);
  1250. if (s->SessionId2 == id)
  1251. {
  1252. if (EQUAL_BOOL(s->IsV3, is_v3))
  1253. {
  1254. return s;
  1255. }
  1256. }
  1257. }
  1258. }
  1259. return NULL;
  1260. }
  1261. // Create a new session ID
  1262. UINT GenerateNewSessionId(L2TP_TUNNEL *t)
  1263. {
  1264. return GenerateNewSessionIdEx(t, false);
  1265. }
  1266. UINT GenerateNewSessionIdEx(L2TP_TUNNEL *t, bool is_32bit)
  1267. {
  1268. UINT i;
  1269. UINT max_number = 0xffff;
  1270. // Validate arguments
  1271. if (t == NULL)
  1272. {
  1273. return 0;
  1274. }
  1275. if (is_32bit)
  1276. {
  1277. max_number = 0xfffffffe;
  1278. }
  1279. for (i = 1;i <= max_number;i++)
  1280. {
  1281. if (GetSessionFromId(t, i) == NULL)
  1282. {
  1283. return i;
  1284. }
  1285. }
  1286. return 0;
  1287. }
  1288. UINT GenerateNewSessionIdForL2TPv3(L2TP_SERVER *l2tp)
  1289. {
  1290. // Validate arguments
  1291. if (l2tp == NULL)
  1292. {
  1293. return 0;
  1294. }
  1295. while (true)
  1296. {
  1297. UINT id = Rand32();
  1298. if (id == 0 || id == 0xffffffff)
  1299. {
  1300. continue;
  1301. }
  1302. if (SearchL2TPSessionById(l2tp, true, id) == false)
  1303. {
  1304. return id;
  1305. }
  1306. }
  1307. }
  1308. // Release the session
  1309. void FreeL2TPSession(L2TP_SESSION *s)
  1310. {
  1311. // Validate arguments
  1312. if (s == NULL)
  1313. {
  1314. return;
  1315. }
  1316. Free(s);
  1317. }
  1318. // Search a session from the session ID
  1319. L2TP_SESSION *GetSessionFromId(L2TP_TUNNEL *t, UINT session_id)
  1320. {
  1321. UINT i;
  1322. // Validate arguments
  1323. if (t == NULL || session_id == 0)
  1324. {
  1325. return NULL;
  1326. }
  1327. for (i = 0;i < LIST_NUM(t->SessionList);i++)
  1328. {
  1329. L2TP_SESSION *s = LIST_DATA(t->SessionList, i);
  1330. if (s->SessionId2 == session_id)
  1331. {
  1332. return s;
  1333. }
  1334. }
  1335. return NULL;
  1336. }
  1337. // Search a session from the session ID (Search by ID assigned from the client side)
  1338. L2TP_SESSION *GetSessionFromIdAssignedByClient(L2TP_TUNNEL *t, UINT session_id)
  1339. {
  1340. UINT i;
  1341. // Validate arguments
  1342. if (t == NULL || session_id == 0)
  1343. {
  1344. return NULL;
  1345. }
  1346. for (i = 0;i < LIST_NUM(t->SessionList);i++)
  1347. {
  1348. L2TP_SESSION *s = LIST_DATA(t->SessionList, i);
  1349. if (s->SessionId1 == session_id)
  1350. {
  1351. return s;
  1352. }
  1353. }
  1354. return NULL;
  1355. }
  1356. // Get the number of L2TP sessions connected from the client IP address
  1357. UINT GetNumL2TPTunnelsByClientIP(L2TP_SERVER *l2tp, IP *client_ip)
  1358. {
  1359. UINT i, ret;
  1360. // Validate arguments
  1361. if (l2tp == NULL || client_ip == NULL)
  1362. {
  1363. return 0;
  1364. }
  1365. ret = 0;
  1366. for (i = 0;i < LIST_NUM(l2tp->TunnelList);i++)
  1367. {
  1368. L2TP_TUNNEL *t = LIST_DATA(l2tp->TunnelList, i);
  1369. if (CmpIpAddr(&t->ClientIp, client_ip) == 0)
  1370. {
  1371. ret++;
  1372. }
  1373. }
  1374. return ret;
  1375. }
  1376. // Performs processing L2TP received packets.
  1377. void ProcL2TPPacketRecv(L2TP_SERVER *l2tp, UDPPACKET *p)
  1378. {
  1379. L2TP_PACKET *pp;
  1380. bool no_free = false;
  1381. // Validate arguments
  1382. if (l2tp == NULL || p == NULL)
  1383. {
  1384. return;
  1385. }
  1386. // Parse a packet.
  1387. pp = ParseL2TPPacket(p);
  1388. if (pp == NULL)
  1389. {
  1390. return;
  1391. }
  1392. if (pp->MessageType == L2TP_MESSAGE_TYPE_SCCRQ && pp->SessionId == 0 && pp->TunnelId == 0 &&
  1393. pp->Nr == 0 && pp->Ns == 0 && l2tp->Halt == false)
  1394. {
  1395. {
  1396. L2TP_AVP *a = GetAVPValue(pp, (pp->Ver == 3 ? L2TP_AVP_TYPE_V3_TUNNEL_ID : L2TP_AVP_TYPE_ASSIGNED_TUNNEL));
  1397. if (a != NULL && a->DataSize == (pp->Ver == 3 ? sizeof(UINT) : sizeof(USHORT)))
  1398. {
  1399. UINT client_assigned_id = (pp->Ver == 3 ? READ_UINT(a->Data) : READ_USHORT(a->Data));
  1400. if (GetTunnelFromIdOfAssignedByClient(l2tp, &p->SrcIP, client_assigned_id) == NULL)
  1401. {
  1402. if (LIST_NUM(l2tp->TunnelList) < L2TP_QUOTA_MAX_NUM_TUNNELS && GetNumL2TPTunnelsByClientIP(l2tp, &p->SrcIP) < L2TP_QUOTA_MAX_NUM_TUNNELS_PER_IP)
  1403. {
  1404. char ipstr[MAX_SIZE];
  1405. L2TP_PACKET *pp2;
  1406. UCHAR protocol_version[2];
  1407. UCHAR caps_data[4];
  1408. USHORT us;
  1409. char hostname[MAX_SIZE];
  1410. // Begin Tunneling
  1411. L2TP_TUNNEL *t = NewL2TPTunnel(l2tp, pp, p);
  1412. if (t != NULL)
  1413. {
  1414. IPToStr(ipstr, sizeof(ipstr), &t->ClientIp);
  1415. Debug("L2TP New Tunnel From %s (%s, %s): New Tunnel ID = %u/%u\n", ipstr, t->HostName, t->VendorName,
  1416. t->TunnelId1, t->TunnelId2);
  1417. // Add the tunnel to the list
  1418. Add(l2tp->TunnelList, t);
  1419. // Respond with SCCEP to SCCRQ
  1420. pp2 = NewL2TPControlPacket(L2TP_MESSAGE_TYPE_SCCRP, t->IsV3);
  1421. if (t->IsYamahaV3 == false)
  1422. {
  1423. // Protocol Version
  1424. protocol_version[0] = 1;
  1425. protocol_version[1] = 0;
  1426. Add(pp2->AvpList, NewAVP(L2TP_AVP_TYPE_PROTOCOL_VERSION, true, 0, protocol_version, sizeof(protocol_version)));
  1427. // Framing Capabilities
  1428. Zero(caps_data, sizeof(caps_data));
  1429. if (t->IsV3 == false)
  1430. {
  1431. caps_data[3] = 3;
  1432. }
  1433. Add(pp2->AvpList, NewAVP(L2TP_AVP_TYPE_FRAME_CAP, false, 0, caps_data, sizeof(caps_data)));
  1434. }
  1435. if (t->IsV3 == false)
  1436. {
  1437. // Bearer Capabilities
  1438. Zero(caps_data, sizeof(caps_data));
  1439. caps_data[3] = 3;
  1440. Add(pp2->AvpList, NewAVP(L2TP_AVP_TYPE_BEARER_CAP, false, 0, caps_data, sizeof(caps_data)));
  1441. }
  1442. // Host Name
  1443. GetMachineHostName(hostname, sizeof(hostname));
  1444. if (IsEmptyStr(hostname))
  1445. {
  1446. StrCpy(hostname, sizeof(hostname), "vpn");
  1447. }
  1448. Add(pp2->AvpList, NewAVP(L2TP_AVP_TYPE_HOST_NAME, true, 0, hostname, StrLen(hostname)));
  1449. // Vendor Name
  1450. if (t->IsYamahaV3 == false)
  1451. {
  1452. Add(pp2->AvpList, NewAVP(L2TP_AVP_TYPE_VENDOR_NAME, false, 0, L2TP_VENDOR_NAME, StrLen(L2TP_VENDOR_NAME)));
  1453. }
  1454. else
  1455. {
  1456. char *yamaha_str = "YAMAHA Corporation";
  1457. Add(pp2->AvpList, NewAVP(L2TP_AVP_TYPE_VENDOR_NAME, false, 0, yamaha_str, StrLen(yamaha_str)));
  1458. }
  1459. if (t->IsYamahaV3)
  1460. {
  1461. UINT zero = 0;
  1462. Add(pp2->AvpList, NewAVP(L2TP_AVP_TYPE_V3_ROUTER_ID, true, 0, &zero, sizeof(UINT)));
  1463. }
  1464. // Assigned Tunnel ID
  1465. if (t->IsV3 == false)
  1466. {
  1467. us = Endian16(t->TunnelId2);
  1468. Add(pp2->AvpList, NewAVP(L2TP_AVP_TYPE_ASSIGNED_TUNNEL, true, 0, &us, sizeof(USHORT)));
  1469. }
  1470. else
  1471. {
  1472. UINT ui = Endian32(t->TunnelId2);
  1473. Add(pp2->AvpList, NewAVP(L2TP_AVP_TYPE_V3_TUNNEL_ID, true, 0, &ui, sizeof(UINT)));
  1474. if (t->IsCiscoV3)
  1475. {
  1476. Add(pp2->AvpList, NewAVP(L2TPV3_CISCO_AVP_TUNNEL_ID, true, L2TP_AVP_VENDOR_ID_CISCO, &ui, sizeof(UINT)));
  1477. }
  1478. }
  1479. // Pseudowire Capabilities List
  1480. if (t->IsV3)
  1481. {
  1482. // Only Ethernet
  1483. USHORT cap_list[2];
  1484. cap_list[0] = Endian16(L2TPV3_PW_TYPE_ETHERNET);
  1485. cap_list[1] = Endian16(L2TPV3_PW_TYPE_ETHERNET_VLAN);
  1486. Add(pp2->AvpList, NewAVP(L2TP_AVP_TYPE_V3_PW_CAP_LIST, true, 0, cap_list, sizeof(cap_list)));
  1487. if (t->IsCiscoV3)
  1488. {
  1489. Add(pp2->AvpList, NewAVP(L2TPV3_CISCO_AVP_PW_CAP_LIST, true, L2TP_AVP_VENDOR_ID_CISCO, cap_list, sizeof(cap_list)));
  1490. }
  1491. }
  1492. // Cisco AVP
  1493. if (t->IsCiscoV3)
  1494. {
  1495. USHORT us = Endian16(1);
  1496. Add(pp2->AvpList, NewAVP(L2TPV3_CISCO_AVP_DRAFT_AVP_VERSION, true, L2TP_AVP_VENDOR_ID_CISCO, &us, sizeof(USHORT)));
  1497. }
  1498. // Recv Window Size
  1499. if (t->IsYamahaV3 == false)
  1500. {
  1501. us = Endian16(L2TP_WINDOW_SIZE);
  1502. Add(pp2->AvpList, NewAVP(L2TP_AVP_TYPE_RECV_WINDOW_SIZE, false, 0, &us, sizeof(USHORT)));
  1503. }
  1504. SendL2TPControlPacket(l2tp, t, 0, pp2);
  1505. FreeL2TPPacket(pp2);
  1506. }
  1507. }
  1508. }
  1509. }
  1510. }
  1511. }
  1512. else
  1513. {
  1514. // Process related to the existing tunnel
  1515. // Find the tunnel
  1516. L2TP_TUNNEL *t = NULL;
  1517. L2TP_SESSION *l2tpv3_session = NULL;
  1518. if (pp->IsControl || pp->Ver != 3)
  1519. {
  1520. t = GetTunnelFromId(l2tp, &p->SrcIP, pp->TunnelId, pp->Ver == 3);
  1521. }
  1522. else
  1523. {
  1524. l2tpv3_session = SearchL2TPSessionById(l2tp, true, pp->SessionId);
  1525. if (l2tpv3_session != NULL)
  1526. {
  1527. t = l2tpv3_session->Tunnel;
  1528. pp->TunnelId = t->TunnelId2;
  1529. }
  1530. }
  1531. if (t == NULL)
  1532. {
  1533. char ipstr[MAX_SIZE];
  1534. IPToStr(ipstr, sizeof(ipstr), &p->SrcIP);
  1535. Debug("L2TP Tunnel From %s ID=%u Not Found on the Table.\n", ipstr, pp->TunnelId);
  1536. }
  1537. else
  1538. {
  1539. // Update last reception time
  1540. t->LastRecvTick = l2tp->Now;
  1541. if (pp->IsControl)
  1542. {
  1543. // Control packet
  1544. UINT i;
  1545. LIST *o = NULL;
  1546. L2TP_QUEUE *q;
  1547. L2TP_QUEUE tt;
  1548. // Delete the queue that the other party has already received from the retransmission queue
  1549. for (i = 0;i < LIST_NUM(t->SendQueue);i++)
  1550. {
  1551. L2TP_QUEUE *q = LIST_DATA(t->SendQueue, i);
  1552. if (L2TP_SEQ_LT(q->Ns, pp->Nr))
  1553. {
  1554. if (o == NULL)
  1555. {
  1556. o = NewListFast(NULL);
  1557. }
  1558. Add(o, q);
  1559. }
  1560. }
  1561. if (o != NULL)
  1562. {
  1563. for (i = 0;i < LIST_NUM(o);i++)
  1564. {
  1565. L2TP_QUEUE *q = LIST_DATA(o, i);
  1566. Delete(t->SendQueue, q);
  1567. FreeL2TPQueue(q);
  1568. }
  1569. ReleaseList(o);
  1570. }
  1571. if ((!L2TP_SEQ_LT(pp->Ns, t->LastNr)) && (pp->Ns != t->LastNr))
  1572. {
  1573. // Add the packet received from the opposite to the queue
  1574. if (LIST_NUM(t->RecvQueue) < L2TP_WINDOW_SIZE)
  1575. {
  1576. Zero(&tt, sizeof(tt));
  1577. tt.Ns = pp->Ns;
  1578. if (Search(t->RecvQueue, &tt) == NULL)
  1579. {
  1580. q = ZeroMalloc(sizeof(L2TP_QUEUE));
  1581. q->Ns = pp->Ns;
  1582. q->L2TPPacket = pp;
  1583. no_free = true;
  1584. Insert(t->RecvQueue, q);
  1585. // Read to the end of completed part from the head of the queue
  1586. while (TRUE)
  1587. {
  1588. L2TP_QUEUE *q;
  1589. if (LIST_NUM(t->RecvQueue) == 0)
  1590. {
  1591. break;
  1592. }
  1593. q = LIST_DATA(t->RecvQueue, 0);
  1594. if (!L2TP_SEQ_EQ(q->Ns, t->LastNr + 1))
  1595. {
  1596. break;
  1597. }
  1598. if (q->L2TPPacket->IsZLB == false)
  1599. {
  1600. t->LastNr = q->Ns;
  1601. // The packet other than ZLB is treated
  1602. t->StateChanged = true;
  1603. }
  1604. Delete(t->RecvQueue, q);
  1605. // Process the received packet
  1606. L2TPProcessRecvControlPacket(l2tp, t, q->L2TPPacket);
  1607. FreeL2TPQueue(q);
  1608. }
  1609. }
  1610. }
  1611. }
  1612. else
  1613. {
  1614. // Reply ACK for already-received packets
  1615. if (pp->IsZLB == false)
  1616. {
  1617. // The packet other than ZLB is treated
  1618. t->StateChanged = true;
  1619. }
  1620. }
  1621. }
  1622. else
  1623. {
  1624. // Data packet
  1625. L2TP_SESSION *s = GetSessionFromId(t, pp->SessionId);
  1626. if (s != NULL && s->Established)
  1627. {
  1628. if (s->IsV3 == false)
  1629. {
  1630. // Start the L2TP thread (If not already started)
  1631. StartL2TPThread(l2tp, t, s);
  1632. // Pass the data
  1633. TubeSendEx(s->TubeRecv, pp->Data, pp->DataSize, NULL, true);
  1634. AddTubeToFlushList(l2tp->FlushList, s->TubeRecv);
  1635. }
  1636. else
  1637. {
  1638. BLOCK *b;
  1639. // Start the EtherIP session (If it's not have yet started)
  1640. L2TPSessionManageEtherIPServer(l2tp, s);
  1641. // Pass the data
  1642. b = NewBlock(pp->Data, pp->DataSize, 0);
  1643. EtherIPProcRecvPackets(s->EtherIP, b);
  1644. Free(b);
  1645. }
  1646. }
  1647. }
  1648. }
  1649. }
  1650. if (no_free == false)
  1651. {
  1652. FreeL2TPPacket(pp);
  1653. }
  1654. }
  1655. // Manage the EtherIP server that is associated with the L2TP session
  1656. void L2TPSessionManageEtherIPServer(L2TP_SERVER *l2tp, L2TP_SESSION *s)
  1657. {
  1658. IKE_SERVER *ike;
  1659. IKE_CLIENT *c;
  1660. // Validate arguments
  1661. if (l2tp == NULL || s == NULL)
  1662. {
  1663. return;
  1664. }
  1665. if (l2tp->IkeClient == NULL || l2tp->IkeServer == NULL)
  1666. {
  1667. return;
  1668. }
  1669. ike = l2tp->IkeServer;
  1670. c = l2tp->IkeClient;
  1671. if (s->EtherIP == NULL)
  1672. {
  1673. char crypt_name[MAX_SIZE];
  1674. UINT crypt_block_size = IKE_MAX_BLOCK_SIZE;
  1675. Zero(crypt_name, sizeof(crypt_name));
  1676. if (c->CurrentIpSecSaRecv != NULL)
  1677. {
  1678. Format(crypt_name, sizeof(crypt_name),
  1679. "IPsec - %s (%u bits)",
  1680. c->CurrentIpSecSaRecv->TransformSetting.Crypto->Name,
  1681. c->CurrentIpSecSaRecv->TransformSetting.CryptoKeySize * 8);
  1682. crypt_block_size = c->CurrentIpSecSaRecv->TransformSetting.Crypto->BlockSize;
  1683. }
  1684. s->EtherIP = NewEtherIPServer(ike->Cedar, ike->IPsec, ike,
  1685. &c->ClientIP, c->ClientPort,
  1686. &c->ServerIP, c->ServerPort, crypt_name,
  1687. c->IsL2TPOnIPsecTunnelMode, crypt_block_size, c->ClientId,
  1688. ++ike->CurrentEtherId);
  1689. StrCpy(s->EtherIP->VendorName, sizeof(s->EtherIP->VendorName), s->Tunnel->VendorName);
  1690. s->EtherIP->L2TPv3 = true;
  1691. Debug("IKE_CLIENT 0x%X: EtherIP Server Started.\n", c);
  1692. IPsecLog(ike, c, NULL, NULL, NULL, "LI_ETHERIP_SERVER_STARTED", ike->CurrentEtherId);
  1693. }
  1694. else
  1695. {
  1696. StrCpy(s->EtherIP->ClientId, sizeof(s->EtherIP->ClientId), c->ClientId);
  1697. }
  1698. if (s->EtherIP->Interrupts == NULL)
  1699. {
  1700. s->EtherIP->Interrupts = l2tp->Interrupts;
  1701. }
  1702. if (s->EtherIP->SockEvent == NULL)
  1703. {
  1704. SetEtherIPServerSockEvent(s->EtherIP, l2tp->SockEvent);
  1705. }
  1706. s->EtherIP->Now = l2tp->Now;
  1707. }
  1708. // Calculate the appropriate MSS of the L2TP
  1709. UINT CalcL2TPMss(L2TP_SERVER *l2tp, L2TP_TUNNEL *t, L2TP_SESSION *s)
  1710. {
  1711. UINT ret;
  1712. // Validate arguments
  1713. if (l2tp == NULL || t == NULL || s == NULL)
  1714. {
  1715. return 0;
  1716. }
  1717. ret = MTU_FOR_PPPOE;
  1718. if (l2tp->IkeServer != NULL)
  1719. {
  1720. // On IPsec
  1721. if (l2tp->IsIPsecIPv6)
  1722. {
  1723. ret -= 40;
  1724. }
  1725. else
  1726. {
  1727. ret -= 20;
  1728. }
  1729. // UDP
  1730. ret -= 8;
  1731. // ESP
  1732. ret -= 20 + l2tp->CryptBlockSize * 2;
  1733. }
  1734. else
  1735. {
  1736. // Raw L2TP
  1737. if (IsIP6(&t->ClientIp))
  1738. {
  1739. ret -= 40;
  1740. }
  1741. else
  1742. {
  1743. ret -= 20;
  1744. }
  1745. }
  1746. // L2TP UDP
  1747. ret -= 8;
  1748. // L2TP
  1749. ret -= 8;
  1750. // PPP
  1751. ret -= 4;
  1752. // Target communication
  1753. ret -= 20;
  1754. // TCP header
  1755. ret -= 20;
  1756. return ret;
  1757. }
  1758. // Start the L2TP thread
  1759. void StartL2TPThread(L2TP_SERVER *l2tp, L2TP_TUNNEL *t, L2TP_SESSION *s)
  1760. {
  1761. // Validate arguments
  1762. if (l2tp == NULL || t == NULL || s == NULL)
  1763. {
  1764. return;
  1765. }
  1766. if (s->HasThread == false)
  1767. {
  1768. char tmp[MAX_SIZE];
  1769. Debug("Thread Created for Session %u/%u on Tunnel %u/%u\n",
  1770. s->SessionId1, s->SessionId2, t->TunnelId1, t->TunnelId2);
  1771. s->HasThread = true;
  1772. NewTubePair(&s->TubeSend, &s->TubeRecv, 0);
  1773. SetTubeSockEvent(s->TubeSend, l2tp->SockEvent);
  1774. if (IsEmptyStr(t->VendorName) == false)
  1775. {
  1776. Format(tmp, sizeof(tmp), L2TP_IPC_CLIENT_NAME_TAG, t->VendorName);
  1777. }
  1778. else
  1779. {
  1780. StrCpy(tmp, sizeof(tmp), L2TP_IPC_CLIENT_NAME_NO_TAG);
  1781. }
  1782. // Create a PPP thread
  1783. s->Thread = NewPPPSession(l2tp->Cedar, &t->ClientIp, t->ClientPort, &t->ServerIp, t->ServerPort,
  1784. s->TubeSend, s->TubeRecv, L2TP_IPC_POSTFIX, tmp, t->HostName, l2tp->CryptName,
  1785. CalcL2TPMss(l2tp, t, s));
  1786. }
  1787. }
  1788. // Stop the L2TP thread
  1789. void StopL2TPThread(L2TP_SERVER *l2tp, L2TP_TUNNEL *t, L2TP_SESSION *s)
  1790. {
  1791. THREAD *thread;
  1792. // Validate arguments
  1793. if (l2tp == NULL || t == NULL || s == NULL)
  1794. {
  1795. return;
  1796. }
  1797. if (s->IsV3)
  1798. {
  1799. // Process the L2TPv3
  1800. if (s->EtherIP != NULL)
  1801. {
  1802. // Release the EtherIP server
  1803. ReleaseEtherIPServer(s->EtherIP);
  1804. s->EtherIP = NULL;
  1805. }
  1806. return;
  1807. }
  1808. if (s->HasThread == false)
  1809. {
  1810. return;
  1811. }
  1812. thread = s->Thread;
  1813. s->Thread = NULL;
  1814. s->HasThread = false;
  1815. // Disconnect the tube
  1816. TubeDisconnect(s->TubeRecv);
  1817. TubeDisconnect(s->TubeSend);
  1818. // Release the tube
  1819. ReleaseTube(s->TubeRecv);
  1820. ReleaseTube(s->TubeSend);
  1821. s->TubeRecv = NULL;
  1822. s->TubeSend = NULL;
  1823. // Pass the thread to termination list
  1824. if (l2tp->IkeServer == NULL)
  1825. {
  1826. AddThreadToThreadList(l2tp->ThreadList, thread);
  1827. }
  1828. else
  1829. {
  1830. AddThreadToThreadList(l2tp->IkeServer->ThreadList, thread);
  1831. }
  1832. Debug("Thread Stopped for Session %u/%u on Tunnel %u/%u\n",
  1833. s->SessionId1, s->SessionId2, t->TunnelId1, t->TunnelId2);
  1834. // Release the thread
  1835. ReleaseThread(thread);
  1836. }
  1837. // Interrupt processing of L2TP server
  1838. void L2TPProcessInterrupts(L2TP_SERVER *l2tp)
  1839. {
  1840. UINT i, j;
  1841. LIST *delete_tunnel_list = NULL;
  1842. // Validate arguments
  1843. if (l2tp == NULL)
  1844. {
  1845. return;
  1846. }
  1847. if (l2tp->Halt)
  1848. {
  1849. if (l2tp->Halting == false)
  1850. {
  1851. l2tp->Halting = true;
  1852. // Disconnect all tunnels
  1853. for (i = 0;i < LIST_NUM(l2tp->TunnelList);i++)
  1854. {
  1855. L2TP_TUNNEL *t = LIST_DATA(l2tp->TunnelList, i);
  1856. DisconnectL2TPTunnel(t);
  1857. }
  1858. }
  1859. }
  1860. // Flush
  1861. FlushTubeFlushList(l2tp->FlushList);
  1862. // Enumerate all tunnels
  1863. for (i = 0;i < LIST_NUM(l2tp->TunnelList);i++)
  1864. {
  1865. L2TP_TUNNEL *t = LIST_DATA(l2tp->TunnelList, i);
  1866. LIST *delete_session_list = NULL;
  1867. if ((l2tp->Now >= (t->LastRecvTick + (UINT64)L2TP_TUNNEL_TIMEOUT)) && t->Timedout == false)
  1868. {
  1869. // Disconnect the tunnel forcibly if data can not be received for a certain period of time
  1870. t->Timedout = true;
  1871. Debug("L2TP Tunnel %u/%u Timed out.\n", t->TunnelId1, t->TunnelId2);
  1872. DisconnectL2TPTunnel(t);
  1873. }
  1874. if (t->Established && (l2tp->Now >= (t->LastHelloSent + (UINT64)L2TP_HELLO_INTERVAL)))
  1875. {
  1876. if (LIST_NUM(t->SendQueue) <= L2TP_HELLO_SUPRESS_MAX_THRETHORD_NUM_SEND_QUEUE)
  1877. {
  1878. L2TP_PACKET *pp = NewL2TPControlPacket(L2TP_MESSAGE_TYPE_HELLO, t->IsV3);
  1879. // Send a Hello message
  1880. t->LastHelloSent = l2tp->Now;
  1881. //Debug("L2TP Sending Hello %u/%u: tick=%I64u\n", t->TunnelId1, t->TunnelId2, l2tp->Now);
  1882. SendL2TPControlPacket(l2tp, t, 0, pp);
  1883. FreeL2TPPacket(pp);
  1884. L2TPAddInterrupt(l2tp, t->LastHelloSent + (UINT64)L2TP_HELLO_INTERVAL);
  1885. }
  1886. }
  1887. // Enumerate all sessions
  1888. for (j = 0;j < LIST_NUM(t->SessionList);j++)
  1889. {
  1890. L2TP_SESSION *s = LIST_DATA(t->SessionList, j);
  1891. if (s->HasThread)
  1892. {
  1893. // Send packet data
  1894. while (true)
  1895. {
  1896. TUBEDATA *d = TubeRecvAsync(s->TubeSend);
  1897. if (d == NULL)
  1898. {
  1899. break;
  1900. }
  1901. SendL2TPDataPacket(l2tp, t, s, d->Data, d->DataSize);
  1902. FreeTubeData(d);
  1903. }
  1904. if (IsTubeConnected(s->TubeSend) == false)
  1905. {
  1906. // Disconnect the this session because the PPP thread ends
  1907. DisconnectL2TPSession(t, s);
  1908. }
  1909. }
  1910. if (s->IsV3)
  1911. {
  1912. if (s->EtherIP != NULL)
  1913. {
  1914. UINT k;
  1915. L2TPSessionManageEtherIPServer(l2tp, s);
  1916. // Notify an interrupt to the EtherIP module
  1917. EtherIPProcInterrupts(s->EtherIP);
  1918. // Send an EtherIP packet data
  1919. for (k = 0;k < LIST_NUM(s->EtherIP->SendPacketList);k++)
  1920. {
  1921. BLOCK *b = LIST_DATA(s->EtherIP->SendPacketList, k);
  1922. SendL2TPDataPacket(l2tp, t, s, b->Buf, b->Size);
  1923. FreeBlock(b);
  1924. }
  1925. DeleteAll(s->EtherIP->SendPacketList);
  1926. }
  1927. }
  1928. if (s->WantToDisconnect && s->Disconnecting == false)
  1929. {
  1930. // Disconnect the session
  1931. UCHAR error_data[4];
  1932. USHORT us;
  1933. UINT ui;
  1934. UINT ppp_error_1 = 0, ppp_error_2 = 0;
  1935. // Send the session disconnection response
  1936. L2TP_PACKET *pp = NewL2TPControlPacket(L2TP_MESSAGE_TYPE_CDN, s->IsV3);
  1937. if (s->TubeRecv != NULL)
  1938. {
  1939. ppp_error_1 = s->TubeRecv->IntParam1;
  1940. ppp_error_2 = s->TubeRecv->IntParam2;
  1941. }
  1942. // Assigned Session ID
  1943. if (s->IsV3 == false)
  1944. {
  1945. us = Endian16(s->SessionId2);
  1946. Add(pp->AvpList, NewAVP(L2TP_AVP_TYPE_ASSIGNED_SESSION, true, 0,
  1947. &us, sizeof(USHORT)));
  1948. }
  1949. else
  1950. {
  1951. ui = Endian16(s->SessionId2);
  1952. Add(pp->AvpList, NewAVP(L2TP_AVP_TYPE_V3_SESSION_ID_LOCAL, true, 0,
  1953. &ui, sizeof(UINT)));
  1954. if (t->IsCiscoV3)
  1955. {
  1956. Add(pp->AvpList, NewAVP(L2TPV3_CISCO_AVP_SESSION_ID_LOCAL, true, L2TP_AVP_VENDOR_ID_CISCO,
  1957. &ui, sizeof(UINT)));
  1958. }
  1959. }
  1960. // Result-Error Code
  1961. Zero(error_data, sizeof(error_data));
  1962. error_data[1] = 0x03;
  1963. Add(pp->AvpList, NewAVP(L2TP_AVP_TYPE_RESULT_CODE, true, 0,
  1964. error_data, sizeof(error_data)));
  1965. if (ppp_error_1 != 0)
  1966. {
  1967. // PPP Disconnect Cause Code AVP
  1968. BUF *b = NewBuf();
  1969. UCHAR uc;
  1970. USHORT us;
  1971. // Disconnect Code
  1972. us = Endian16(ppp_error_1);
  1973. WriteBuf(b, &us, sizeof(USHORT));
  1974. // Control Protocol Number
  1975. us = Endian16(0xc021);
  1976. WriteBuf(b, &us, sizeof(USHORT));
  1977. // Direction
  1978. uc = (UCHAR)ppp_error_2;
  1979. WriteBuf(b, &uc, sizeof(UCHAR));
  1980. Add(pp->AvpList, NewAVP(L2TP_AVP_TYPE_PPP_DISCONNECT_CAUSE, false, 0,
  1981. b->Buf, b->Size));
  1982. FreeBuf(b);
  1983. }
  1984. SendL2TPControlPacket(l2tp, t, s->SessionId1, pp);
  1985. FreeL2TPPacket(pp);
  1986. // Disconnect the session
  1987. Debug("L2TP Session %u/%u on Tunnel %u/%u Disconnected.\n", s->SessionId1, s->SessionId2,
  1988. t->TunnelId1, t->TunnelId2);
  1989. s->Disconnecting = true;
  1990. s->Established = false;
  1991. s->DisconnectTimeout = l2tp->Now + (UINT64)L2TP_TUNNEL_DISCONNECT_TIMEOUT;
  1992. // Stop the thread
  1993. StopL2TPThread(l2tp, t, s);
  1994. L2TPAddInterrupt(l2tp, s->DisconnectTimeout);
  1995. }
  1996. if (s->Disconnecting && ((l2tp->Now >= s->DisconnectTimeout) || LIST_NUM(t->SendQueue) == 0))
  1997. {
  1998. // Delete the session if synchronization between the client
  1999. // and the server is complete or a time-out occurs
  2000. if (delete_session_list == NULL)
  2001. {
  2002. delete_session_list = NewListFast(NULL);
  2003. }
  2004. Add(delete_session_list, s);
  2005. }
  2006. }
  2007. if (delete_session_list != NULL)
  2008. {
  2009. // Session deletion process
  2010. for (j = 0;j < LIST_NUM(delete_session_list);j++)
  2011. {
  2012. L2TP_SESSION *s = LIST_DATA(delete_session_list, j);
  2013. Debug("L2TP Session %u/%u on Tunnel %u/%u Cleaned up.\n", s->SessionId1, s->SessionId2,
  2014. t->TunnelId1, t->TunnelId2);
  2015. FreeL2TPSession(s);
  2016. Delete(t->SessionList, s);
  2017. }
  2018. ReleaseList(delete_session_list);
  2019. }
  2020. if (t->WantToDisconnect && t->Disconnecting == false)
  2021. {
  2022. // Disconnect the tunnel
  2023. USHORT error_data[4];
  2024. USHORT us;
  2025. UINT ui;
  2026. // Reply the tunnel disconnection response
  2027. L2TP_PACKET *pp = NewL2TPControlPacket(L2TP_MESSAGE_TYPE_STOPCCN, t->IsV3);
  2028. // Assigned Tunnel ID
  2029. if (t->IsV3 == false)
  2030. {
  2031. us = Endian16(t->TunnelId2);
  2032. Add(pp->AvpList, NewAVP(L2TP_AVP_TYPE_ASSIGNED_TUNNEL, true, 0,
  2033. &us, sizeof(USHORT)));
  2034. }
  2035. else
  2036. {
  2037. ui = Endian32(t->TunnelId2);
  2038. Add(pp->AvpList, NewAVP(L2TP_AVP_TYPE_V3_TUNNEL_ID, true, 0,
  2039. &ui, sizeof(UINT)));
  2040. if (t->IsCiscoV3)
  2041. {
  2042. Add(pp->AvpList, NewAVP(L2TPV3_CISCO_AVP_TUNNEL_ID, true, L2TP_AVP_VENDOR_ID_CISCO,
  2043. &ui, sizeof(UINT)));
  2044. }
  2045. }
  2046. // Result-Error Code
  2047. Zero(error_data, sizeof(error_data));
  2048. error_data[1] = 0x06;
  2049. Add(pp->AvpList, NewAVP(L2TP_AVP_TYPE_RESULT_CODE, true, 0,
  2050. error_data, sizeof(error_data)));
  2051. SendL2TPControlPacket(l2tp, t, 0, pp);
  2052. FreeL2TPPacket(pp);
  2053. Debug("L2TP Tunnel %u/%u is Disconnected.\n", t->TunnelId1, t->TunnelId2);
  2054. t->Disconnecting = true;
  2055. t->Established = false;
  2056. t->DisconnectTimeout = l2tp->Now + (UINT64)L2TP_TUNNEL_DISCONNECT_TIMEOUT;
  2057. L2TPAddInterrupt(l2tp, t->DisconnectTimeout);
  2058. }
  2059. if (t->Disconnecting && (((LIST_NUM(t->SendQueue) == 0) && LIST_NUM(t->SessionList) == 0) || (l2tp->Now >= t->DisconnectTimeout)))
  2060. {
  2061. // Delete the tunnel if there is no session in the tunnel when synchronization
  2062. // between the client and the server has been completed or a time-out occurs
  2063. if (delete_tunnel_list == NULL)
  2064. {
  2065. delete_tunnel_list = NewListFast(NULL);
  2066. }
  2067. Add(delete_tunnel_list, t);
  2068. }
  2069. }
  2070. if (delete_tunnel_list != NULL)
  2071. {
  2072. for (i = 0;i < LIST_NUM(delete_tunnel_list);i++)
  2073. {
  2074. L2TP_TUNNEL *t = LIST_DATA(delete_tunnel_list, i);
  2075. Debug("L2TP Tunnel %u/%u Cleaned up.\n", t->TunnelId1, t->TunnelId2);
  2076. FreeL2TPTunnel(t);
  2077. Delete(l2tp->TunnelList, t);
  2078. }
  2079. ReleaseList(delete_tunnel_list);
  2080. }
  2081. // Re-transmit packets
  2082. for (i = 0;i < LIST_NUM(l2tp->TunnelList);i++)
  2083. {
  2084. L2TP_TUNNEL *t = LIST_DATA(l2tp->TunnelList, i);
  2085. UINT j;
  2086. if (LIST_NUM(t->SendQueue) >= 1)
  2087. {
  2088. // Packet to be transmitted exists one or more
  2089. for (j = 0;j < LIST_NUM(t->SendQueue);j++)
  2090. {
  2091. L2TP_QUEUE *q = LIST_DATA(t->SendQueue, j);
  2092. if (l2tp->Now >= q->NextSendTick)
  2093. {
  2094. q->NextSendTick = l2tp->Now + (UINT64)L2TP_PACKET_RESEND_INTERVAL;
  2095. L2TPAddInterrupt(l2tp, q->NextSendTick);
  2096. SendL2TPControlPacketMain(l2tp, t, q);
  2097. }
  2098. }
  2099. }
  2100. else
  2101. {
  2102. // There is no packet to be transmitted, but the state of the tunnel is changed
  2103. if (t->StateChanged)
  2104. {
  2105. // Send a ZLB
  2106. L2TP_QUEUE *q = ZeroMalloc(sizeof(L2TP_QUEUE));
  2107. L2TP_PACKET *pp = NewL2TPControlPacket(0, t->IsV3);
  2108. pp->TunnelId = t->TunnelId1;
  2109. pp->Ns = t->NextNs;
  2110. q->Buf = BuildL2TPPacketData(pp, t);
  2111. SendL2TPControlPacketMain(l2tp, t, q);
  2112. FreeL2TPQueue(q);
  2113. FreeL2TPPacket(pp);
  2114. }
  2115. }
  2116. t->StateChanged = false;
  2117. }
  2118. if (l2tp->Halting)
  2119. {
  2120. if (LIST_NUM(l2tp->TunnelList) == 0)
  2121. {
  2122. // Stop all the L2TP tunnel completed
  2123. if (l2tp->HaltCompleted == false)
  2124. {
  2125. l2tp->HaltCompleted = true;
  2126. Set(l2tp->HaltCompletedEvent);
  2127. }
  2128. }
  2129. }
  2130. // Maintenance the thread list
  2131. if (l2tp->IkeServer == NULL)
  2132. {
  2133. MainteThreadList(l2tp->ThreadList);
  2134. //Debug("l2tp->ThreadList: %u\n", LIST_NUM(l2tp->ThreadList));
  2135. }
  2136. }
  2137. // Create a new L2TP server
  2138. L2TP_SERVER *NewL2TPServer(CEDAR *cedar)
  2139. {
  2140. return NewL2TPServerEx(cedar, NULL, false, 0);
  2141. }
  2142. L2TP_SERVER *NewL2TPServerEx(CEDAR *cedar, IKE_SERVER *ike, bool is_ipv6, UINT crypt_block_size)
  2143. {
  2144. L2TP_SERVER *l2tp;
  2145. // Validate arguments
  2146. if (cedar == NULL)
  2147. {
  2148. return NULL;
  2149. }
  2150. l2tp = ZeroMalloc(sizeof(L2TP_SERVER));
  2151. l2tp->FlushList = NewTubeFlushList();
  2152. l2tp->Cedar = cedar;
  2153. AddRef(l2tp->Cedar->ref);
  2154. l2tp->SendPacketList = NewList(NULL);
  2155. l2tp->TunnelList = NewList(NULL);
  2156. l2tp->HaltCompletedEvent = NewEvent();
  2157. l2tp->ThreadList = NewThreadList();
  2158. l2tp->IkeServer = ike;
  2159. l2tp->IsIPsecIPv6 = is_ipv6;
  2160. l2tp->CryptBlockSize = crypt_block_size;
  2161. return l2tp;
  2162. }
  2163. // Stop the L2TP server
  2164. void StopL2TPServer(L2TP_SERVER *l2tp, bool no_wait)
  2165. {
  2166. // Validate arguments
  2167. if (l2tp == NULL)
  2168. {
  2169. return;
  2170. }
  2171. if (l2tp->Halt)
  2172. {
  2173. return;
  2174. }
  2175. // Begin to shut down
  2176. l2tp->Halt = true;
  2177. Debug("Shutting down L2TP Server...\n");
  2178. // Hit the event
  2179. SetSockEvent(l2tp->SockEvent);
  2180. if (no_wait == false)
  2181. {
  2182. // Wait until complete stopping all tunnels
  2183. Wait(l2tp->HaltCompletedEvent, INFINITE);
  2184. }
  2185. else
  2186. {
  2187. UINT i, j;
  2188. // Kill the thread of all sessions
  2189. for (i = 0;i < LIST_NUM(l2tp->TunnelList);i++)
  2190. {
  2191. L2TP_TUNNEL *t = LIST_DATA(l2tp->TunnelList, i);
  2192. for (j = 0;j < LIST_NUM(t->SessionList);j++)
  2193. {
  2194. L2TP_SESSION *s = LIST_DATA(t->SessionList, j);
  2195. StopL2TPThread(l2tp, t, s);
  2196. }
  2197. }
  2198. }
  2199. // Thread stop
  2200. Debug("Stopping all L2TP PPP Threads...\n");
  2201. StopThreadList(l2tp->ThreadList);
  2202. Debug("L2TP Server Shutdown Completed.\n");
  2203. }
  2204. // Release the L2TP server
  2205. void FreeL2TPServer(L2TP_SERVER *l2tp)
  2206. {
  2207. UINT i;
  2208. // Validate arguments
  2209. if (l2tp == NULL)
  2210. {
  2211. return;
  2212. }
  2213. FreeThreadList(l2tp->ThreadList);
  2214. for (i = 0;i < LIST_NUM(l2tp->SendPacketList);i++)
  2215. {
  2216. UDPPACKET *p = LIST_DATA(l2tp->SendPacketList, i);
  2217. FreeUdpPacket(p);
  2218. }
  2219. ReleaseList(l2tp->SendPacketList);
  2220. for (i = 0;i < LIST_NUM(l2tp->TunnelList);i++)
  2221. {
  2222. L2TP_TUNNEL *t = LIST_DATA(l2tp->TunnelList, i);
  2223. FreeL2TPTunnel(t);
  2224. }
  2225. ReleaseList(l2tp->TunnelList);
  2226. ReleaseSockEvent(l2tp->SockEvent);
  2227. ReleaseEvent(l2tp->HaltCompletedEvent);
  2228. ReleaseCedar(l2tp->Cedar);
  2229. FreeTubeFlushList(l2tp->FlushList);
  2230. Free(l2tp);
  2231. }
  2232. // Set a SockEvent to the L2TP server
  2233. void SetL2TPServerSockEvent(L2TP_SERVER *l2tp, SOCK_EVENT *e)
  2234. {
  2235. // Validate arguments
  2236. if (l2tp == NULL)
  2237. {
  2238. return;
  2239. }
  2240. if (e != NULL)
  2241. {
  2242. AddRef(e->ref);
  2243. }
  2244. if (l2tp->SockEvent != NULL)
  2245. {
  2246. ReleaseSockEvent(l2tp->SockEvent);
  2247. l2tp->SockEvent = NULL;
  2248. }
  2249. l2tp->SockEvent = e;
  2250. }
  2251. // Developed by SoftEther VPN Project at University of Tsukuba in Japan.
  2252. // Department of Computer Science has dozens of overly-enthusiastic geeks.
  2253. // Join us: http://www.tsukuba.ac.jp/english/admission/