TcpInput.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  1. /** @file
  2. TCP input process routines.
  3. Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "TcpMain.h"
  7. /**
  8. Check whether the sequence number of the incoming segment is acceptable.
  9. @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
  10. @param[in] Seg Pointer to the incoming segment.
  11. @retval 1 The sequence number is acceptable.
  12. @retval 0 The sequence number is not acceptable.
  13. **/
  14. INTN
  15. TcpSeqAcceptable (
  16. IN TCP_CB *Tcb,
  17. IN TCP_SEG *Seg
  18. )
  19. {
  20. return (TCP_SEQ_LEQ (Tcb->RcvNxt, Seg->End) &&
  21. TCP_SEQ_LT (Seg->Seq, Tcb->RcvWl2 + Tcb->RcvWnd));
  22. }
  23. /**
  24. NewReno fast recovery defined in RFC3782.
  25. @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
  26. @param[in] Seg Segment that triggers the fast recovery.
  27. **/
  28. VOID
  29. TcpFastRecover (
  30. IN OUT TCP_CB *Tcb,
  31. IN TCP_SEG *Seg
  32. )
  33. {
  34. UINT32 FlightSize;
  35. UINT32 Acked;
  36. //
  37. // Step 1: Three duplicate ACKs and not in fast recovery
  38. //
  39. if (Tcb->CongestState != TCP_CONGEST_RECOVER) {
  40. //
  41. // Step 1A: Invoking fast retransmission.
  42. //
  43. FlightSize = TCP_SUB_SEQ (Tcb->SndNxt, Tcb->SndUna);
  44. Tcb->Ssthresh = MAX (FlightSize >> 1, (UINT32)(2 * Tcb->SndMss));
  45. Tcb->Recover = Tcb->SndNxt;
  46. Tcb->CongestState = TCP_CONGEST_RECOVER;
  47. TCP_CLEAR_FLG (Tcb->CtrlFlag, TCP_CTRL_RTT_ON);
  48. //
  49. // Step 2: Entering fast retransmission
  50. //
  51. TcpRetransmit (Tcb, Tcb->SndUna);
  52. Tcb->CWnd = Tcb->Ssthresh + 3 * Tcb->SndMss;
  53. DEBUG (
  54. (DEBUG_NET,
  55. "TcpFastRecover: enter fast retransmission for TCB %p, recover point is %d\n",
  56. Tcb,
  57. Tcb->Recover)
  58. );
  59. return;
  60. }
  61. //
  62. // During fast recovery, execute Step 3, 4, 5 of RFC3782
  63. //
  64. if (Seg->Ack == Tcb->SndUna) {
  65. //
  66. // Step 3: Fast Recovery,
  67. // If this is a duplicated ACK, increse Cwnd by SMSS.
  68. //
  69. // Step 4 is skipped here only to be executed later
  70. // by TcpToSendData
  71. //
  72. Tcb->CWnd += Tcb->SndMss;
  73. DEBUG (
  74. (DEBUG_NET,
  75. "TcpFastRecover: received another duplicated ACK (%d) for TCB %p\n",
  76. Seg->Ack,
  77. Tcb)
  78. );
  79. } else {
  80. //
  81. // New data is ACKed, check whether it is a
  82. // full ACK or partial ACK
  83. //
  84. if (TCP_SEQ_GEQ (Seg->Ack, Tcb->Recover)) {
  85. //
  86. // Step 5 - Full ACK:
  87. // deflate the congestion window, and exit fast recovery
  88. //
  89. FlightSize = TCP_SUB_SEQ (Tcb->SndNxt, Tcb->SndUna);
  90. Tcb->CWnd = MIN (Tcb->Ssthresh, FlightSize + Tcb->SndMss);
  91. Tcb->CongestState = TCP_CONGEST_OPEN;
  92. DEBUG (
  93. (DEBUG_NET,
  94. "TcpFastRecover: received a full ACK(%d) for TCB %p, exit fast recovery\n",
  95. Seg->Ack,
  96. Tcb)
  97. );
  98. } else {
  99. //
  100. // Step 5 - Partial ACK:
  101. // fast retransmit the first unacknowledge field
  102. // , then deflate the CWnd
  103. //
  104. TcpRetransmit (Tcb, Seg->Ack);
  105. Acked = TCP_SUB_SEQ (Seg->Ack, Tcb->SndUna);
  106. //
  107. // Deflate the CWnd by the amount of new data
  108. // ACKed by SEG.ACK. If more than one SMSS data
  109. // is ACKed, add back SMSS byte to CWnd after
  110. //
  111. if (Acked >= Tcb->SndMss) {
  112. Acked -= Tcb->SndMss;
  113. }
  114. Tcb->CWnd -= Acked;
  115. DEBUG (
  116. (DEBUG_NET,
  117. "TcpFastRecover: received a partial ACK(%d) for TCB %p\n",
  118. Seg->Ack,
  119. Tcb)
  120. );
  121. }
  122. }
  123. }
  124. /**
  125. NewReno fast loss recovery defined in RFC3792.
  126. @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
  127. @param[in] Seg Segment that triggers the fast loss recovery.
  128. **/
  129. VOID
  130. TcpFastLossRecover (
  131. IN OUT TCP_CB *Tcb,
  132. IN TCP_SEG *Seg
  133. )
  134. {
  135. if (TCP_SEQ_GT (Seg->Ack, Tcb->SndUna)) {
  136. //
  137. // New data is ACKed, check whether it is a
  138. // full ACK or partial ACK
  139. //
  140. if (TCP_SEQ_GEQ (Seg->Ack, Tcb->LossRecover)) {
  141. //
  142. // Full ACK: exit the loss recovery.
  143. //
  144. Tcb->LossTimes = 0;
  145. Tcb->CongestState = TCP_CONGEST_OPEN;
  146. DEBUG (
  147. (DEBUG_NET,
  148. "TcpFastLossRecover: received a full ACK(%d) for TCB %p\n",
  149. Seg->Ack,
  150. Tcb)
  151. );
  152. } else {
  153. //
  154. // Partial ACK:
  155. // fast retransmit the first unacknowledge field.
  156. //
  157. TcpRetransmit (Tcb, Seg->Ack);
  158. DEBUG (
  159. (DEBUG_NET,
  160. "TcpFastLossRecover: received a partial ACK(%d) for TCB %p\n",
  161. Seg->Ack,
  162. Tcb)
  163. );
  164. }
  165. }
  166. }
  167. /**
  168. Compute the RTT as specified in RFC2988.
  169. @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
  170. @param[in] Measure Currently measured RTT in heartbeats.
  171. **/
  172. VOID
  173. TcpComputeRtt (
  174. IN OUT TCP_CB *Tcb,
  175. IN UINT32 Measure
  176. )
  177. {
  178. INT32 Var;
  179. //
  180. // Step 2.3: Compute the RTO for subsequent RTT measurement.
  181. //
  182. if (Tcb->SRtt != 0) {
  183. Var = Tcb->SRtt - (Measure << TCP_RTT_SHIFT);
  184. if (Var < 0) {
  185. Var = -Var;
  186. }
  187. Tcb->RttVar = (3 * Tcb->RttVar + Var) >> 2;
  188. Tcb->SRtt = 7 * (Tcb->SRtt >> 3) + Measure;
  189. } else {
  190. //
  191. // Step 2.2: compute the first RTT measure
  192. //
  193. Tcb->SRtt = Measure << TCP_RTT_SHIFT;
  194. Tcb->RttVar = Measure << (TCP_RTT_SHIFT - 1);
  195. }
  196. Tcb->Rto = (Tcb->SRtt + MAX (8, 4 * Tcb->RttVar)) >> TCP_RTT_SHIFT;
  197. //
  198. // Step 2.4: Limit the RTO to at least 1 second
  199. // Step 2.5: Limit the RTO to a maximum value that
  200. // is at least 60 second
  201. //
  202. if (Tcb->Rto < TCP_RTO_MIN) {
  203. Tcb->Rto = TCP_RTO_MIN;
  204. } else if (Tcb->Rto > TCP_RTO_MAX) {
  205. Tcb->Rto = TCP_RTO_MAX;
  206. }
  207. DEBUG (
  208. (DEBUG_NET,
  209. "TcpComputeRtt: new RTT for TCB %p computed SRTT: %d RTTVAR: %d RTO: %d\n",
  210. Tcb,
  211. Tcb->SRtt,
  212. Tcb->RttVar,
  213. Tcb->Rto)
  214. );
  215. }
  216. /**
  217. Trim the data; SYN and FIN to fit into the window defined by Left and Right.
  218. @param[in] Nbuf The buffer that contains a received TCP segment without an IP header.
  219. @param[in] Left The sequence number of the window's left edge.
  220. @param[in] Right The sequence number of the window's right edge.
  221. @retval 0 The segment is broken.
  222. @retval 1 The segment is in good shape.
  223. **/
  224. INTN
  225. TcpTrimSegment (
  226. IN NET_BUF *Nbuf,
  227. IN TCP_SEQNO Left,
  228. IN TCP_SEQNO Right
  229. )
  230. {
  231. TCP_SEG *Seg;
  232. TCP_SEQNO Urg;
  233. UINT32 Drop;
  234. Seg = TCPSEG_NETBUF (Nbuf);
  235. //
  236. // If the segment is completely out of window,
  237. // truncate every thing, include SYN and FIN.
  238. //
  239. if (TCP_SEQ_LEQ (Seg->End, Left) || TCP_SEQ_LEQ (Right, Seg->Seq)) {
  240. TCP_CLEAR_FLG (Seg->Flag, TCP_FLG_SYN);
  241. TCP_CLEAR_FLG (Seg->Flag, TCP_FLG_FIN);
  242. Seg->Seq = Seg->End;
  243. NetbufTrim (Nbuf, Nbuf->TotalSize, NET_BUF_HEAD);
  244. return 1;
  245. }
  246. //
  247. // Adjust the buffer header
  248. //
  249. if (TCP_SEQ_LT (Seg->Seq, Left)) {
  250. Drop = TCP_SUB_SEQ (Left, Seg->Seq);
  251. Urg = Seg->Seq + Seg->Urg;
  252. Seg->Seq = Left;
  253. if (TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN)) {
  254. TCP_CLEAR_FLG (Seg->Flag, TCP_FLG_SYN);
  255. Drop--;
  256. }
  257. //
  258. // Adjust the urgent point
  259. //
  260. if (TCP_FLG_ON (Seg->Flag, TCP_FLG_URG)) {
  261. if (TCP_SEQ_LT (Urg, Seg->Seq)) {
  262. TCP_CLEAR_FLG (Seg->Flag, TCP_FLG_URG);
  263. } else {
  264. Seg->Urg = (UINT16)TCP_SUB_SEQ (Urg, Seg->Seq);
  265. }
  266. }
  267. if (Drop != 0) {
  268. NetbufTrim (Nbuf, Drop, NET_BUF_HEAD);
  269. }
  270. }
  271. //
  272. // Adjust the buffer tail
  273. //
  274. if (TCP_SEQ_GT (Seg->End, Right)) {
  275. Drop = TCP_SUB_SEQ (Seg->End, Right);
  276. Seg->End = Right;
  277. if (TCP_FLG_ON (Seg->Flag, TCP_FLG_FIN)) {
  278. TCP_CLEAR_FLG (Seg->Flag, TCP_FLG_FIN);
  279. Drop--;
  280. }
  281. if (Drop != 0) {
  282. NetbufTrim (Nbuf, Drop, NET_BUF_TAIL);
  283. }
  284. }
  285. return TcpVerifySegment (Nbuf);
  286. }
  287. /**
  288. Trim off the data outside the tcb's receive window.
  289. @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
  290. @param[in] Nbuf Pointer to the NET_BUF containing the received tcp segment.
  291. @retval 0 The segment is broken.
  292. @retval 1 The segment is in good shape.
  293. **/
  294. INTN
  295. TcpTrimInWnd (
  296. IN TCP_CB *Tcb,
  297. IN NET_BUF *Nbuf
  298. )
  299. {
  300. return TcpTrimSegment (Nbuf, Tcb->RcvNxt, Tcb->RcvWl2 + Tcb->RcvWnd);
  301. }
  302. /**
  303. Process the data and FIN flag, and check whether to deliver
  304. data to the socket layer.
  305. @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
  306. @retval 0 No error occurred to deliver data.
  307. @retval -1 An error condition occurred. The proper response is to reset the
  308. connection.
  309. **/
  310. INTN
  311. TcpDeliverData (
  312. IN OUT TCP_CB *Tcb
  313. )
  314. {
  315. LIST_ENTRY *Entry;
  316. NET_BUF *Nbuf;
  317. TCP_SEQNO Seq;
  318. TCP_SEG *Seg;
  319. UINT32 Urgent;
  320. ASSERT ((Tcb != NULL) && (Tcb->Sk != NULL));
  321. //
  322. // make sure there is some data queued,
  323. // and TCP is in a proper state
  324. //
  325. if (IsListEmpty (&Tcb->RcvQue) || !TCP_CONNECTED (Tcb->State)) {
  326. return 0;
  327. }
  328. //
  329. // Deliver data to the socket layer
  330. //
  331. Entry = Tcb->RcvQue.ForwardLink;
  332. Seq = Tcb->RcvNxt;
  333. while (Entry != &Tcb->RcvQue) {
  334. Nbuf = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);
  335. Seg = TCPSEG_NETBUF (Nbuf);
  336. if (TcpVerifySegment (Nbuf) == 0) {
  337. DEBUG (
  338. (DEBUG_ERROR,
  339. "TcpToSendData: discard a broken segment for TCB %p\n",
  340. Tcb)
  341. );
  342. NetbufFree (Nbuf);
  343. return -1;
  344. }
  345. ASSERT (Nbuf->Tcp == NULL);
  346. if (TCP_SEQ_GT (Seg->Seq, Seq)) {
  347. break;
  348. }
  349. Entry = Entry->ForwardLink;
  350. Seq = Seg->End;
  351. Tcb->RcvNxt = Seq;
  352. RemoveEntryList (&Nbuf->List);
  353. //
  354. // RFC793 Eighth step: process FIN in sequence
  355. //
  356. if (TCP_FLG_ON (Seg->Flag, TCP_FLG_FIN)) {
  357. //
  358. // The peer sends to us junky data after FIN,
  359. // reset the connection.
  360. //
  361. if (!IsListEmpty (&Tcb->RcvQue)) {
  362. DEBUG (
  363. (DEBUG_ERROR,
  364. "TcpDeliverData: data received after FIN from peer of TCB %p, reset connection\n",
  365. Tcb)
  366. );
  367. NetbufFree (Nbuf);
  368. return -1;
  369. }
  370. DEBUG (
  371. (DEBUG_NET,
  372. "TcpDeliverData: processing FIN from peer of TCB %p\n",
  373. Tcb)
  374. );
  375. switch (Tcb->State) {
  376. case TCP_SYN_RCVD:
  377. case TCP_ESTABLISHED:
  378. TcpSetState (Tcb, TCP_CLOSE_WAIT);
  379. break;
  380. case TCP_FIN_WAIT_1:
  381. if (!TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_FIN_ACKED)) {
  382. TcpSetState (Tcb, TCP_CLOSING);
  383. break;
  384. }
  385. //
  386. // fall through
  387. //
  388. case TCP_FIN_WAIT_2:
  389. TcpSetState (Tcb, TCP_TIME_WAIT);
  390. TcpClearAllTimer (Tcb);
  391. if (Tcb->TimeWaitTimeout != 0) {
  392. TcpSetTimer (Tcb, TCP_TIMER_2MSL, Tcb->TimeWaitTimeout);
  393. } else {
  394. DEBUG (
  395. (DEBUG_WARN,
  396. "Connection closed immediately because app disables TIME_WAIT timer for %p\n",
  397. Tcb)
  398. );
  399. TcpSendAck (Tcb);
  400. TcpClose (Tcb);
  401. }
  402. break;
  403. case TCP_CLOSE_WAIT:
  404. case TCP_CLOSING:
  405. case TCP_LAST_ACK:
  406. case TCP_TIME_WAIT:
  407. //
  408. // The peer sends to us junk FIN byte. Discard
  409. // the buffer then reset the connection
  410. //
  411. NetbufFree (Nbuf);
  412. return -1;
  413. break;
  414. default:
  415. break;
  416. }
  417. TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW);
  418. Seg->End--;
  419. }
  420. //
  421. // Don't delay the ack if PUSH flag is on.
  422. //
  423. if (TCP_FLG_ON (Seg->Flag, TCP_FLG_PSH)) {
  424. TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW);
  425. }
  426. if (Nbuf->TotalSize != 0) {
  427. Urgent = 0;
  428. if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_RCVD_URG) &&
  429. TCP_SEQ_LEQ (Seg->Seq, Tcb->RcvUp))
  430. {
  431. if (TCP_SEQ_LEQ (Seg->End, Tcb->RcvUp)) {
  432. Urgent = Nbuf->TotalSize;
  433. } else {
  434. Urgent = TCP_SUB_SEQ (Tcb->RcvUp, Seg->Seq) + 1;
  435. }
  436. }
  437. SockDataRcvd (Tcb->Sk, Nbuf, Urgent);
  438. }
  439. if (TCP_FIN_RCVD (Tcb->State)) {
  440. SockNoMoreData (Tcb->Sk);
  441. }
  442. NetbufFree (Nbuf);
  443. }
  444. return 0;
  445. }
  446. /**
  447. Store the data into the reassemble queue.
  448. @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
  449. @param[in] Nbuf Pointer to the buffer containing the data to be queued.
  450. @retval 0 An error condition occurred.
  451. @retval 1 No error occurred to queue data.
  452. **/
  453. INTN
  454. TcpQueueData (
  455. IN OUT TCP_CB *Tcb,
  456. IN NET_BUF *Nbuf
  457. )
  458. {
  459. TCP_SEG *Seg;
  460. LIST_ENTRY *Head;
  461. LIST_ENTRY *Prev;
  462. LIST_ENTRY *Cur;
  463. NET_BUF *Node;
  464. ASSERT ((Tcb != NULL) && (Nbuf != NULL) && (Nbuf->Tcp == NULL));
  465. NET_GET_REF (Nbuf);
  466. Seg = TCPSEG_NETBUF (Nbuf);
  467. Head = &Tcb->RcvQue;
  468. //
  469. // Fast path to process normal case. That is,
  470. // no out-of-order segments are received.
  471. //
  472. if (IsListEmpty (Head)) {
  473. InsertTailList (Head, &Nbuf->List);
  474. return 1;
  475. }
  476. //
  477. // Find the point to insert the buffer
  478. //
  479. for (Prev = Head, Cur = Head->ForwardLink;
  480. Cur != Head;
  481. Prev = Cur, Cur = Cur->ForwardLink)
  482. {
  483. Node = NET_LIST_USER_STRUCT (Cur, NET_BUF, List);
  484. if (TCP_SEQ_LT (Seg->Seq, TCPSEG_NETBUF (Node)->Seq)) {
  485. break;
  486. }
  487. }
  488. //
  489. // Check whether the current segment overlaps with the
  490. // previous segment.
  491. //
  492. if (Prev != Head) {
  493. Node = NET_LIST_USER_STRUCT (Prev, NET_BUF, List);
  494. if (TCP_SEQ_LT (Seg->Seq, TCPSEG_NETBUF (Node)->End)) {
  495. if (TCP_SEQ_LEQ (Seg->End, TCPSEG_NETBUF (Node)->End)) {
  496. return 1;
  497. }
  498. if (TcpTrimSegment (Nbuf, TCPSEG_NETBUF (Node)->End, Seg->End) == 0) {
  499. return 0;
  500. }
  501. }
  502. }
  503. InsertHeadList (Prev, &Nbuf->List);
  504. TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW);
  505. //
  506. // Check the segments after the insert point.
  507. //
  508. while (Cur != Head) {
  509. Node = NET_LIST_USER_STRUCT (Cur, NET_BUF, List);
  510. if (TCP_SEQ_LEQ (TCPSEG_NETBUF (Node)->End, Seg->End)) {
  511. Cur = Cur->ForwardLink;
  512. RemoveEntryList (&Node->List);
  513. NetbufFree (Node);
  514. continue;
  515. }
  516. if (TCP_SEQ_LT (TCPSEG_NETBUF (Node)->Seq, Seg->End)) {
  517. if (TCP_SEQ_LEQ (TCPSEG_NETBUF (Node)->Seq, Seg->Seq)) {
  518. RemoveEntryList (&Nbuf->List);
  519. return 1;
  520. }
  521. if (TcpTrimSegment (Nbuf, Seg->Seq, TCPSEG_NETBUF (Node)->Seq) == 0) {
  522. RemoveEntryList (&Nbuf->List);
  523. return 0;
  524. }
  525. break;
  526. }
  527. Cur = Cur->ForwardLink;
  528. }
  529. return 1;
  530. }
  531. /**
  532. Adjust the send queue or the retransmit queue.
  533. @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
  534. @param[in] Ack The acknowledge sequence number of the received segment.
  535. @retval 0 An error condition occurred.
  536. @retval 1 No error occurred.
  537. **/
  538. INTN
  539. TcpAdjustSndQue (
  540. IN TCP_CB *Tcb,
  541. IN TCP_SEQNO Ack
  542. )
  543. {
  544. LIST_ENTRY *Head;
  545. LIST_ENTRY *Cur;
  546. NET_BUF *Node;
  547. TCP_SEG *Seg;
  548. Head = &Tcb->SndQue;
  549. Cur = Head->ForwardLink;
  550. while (Cur != Head) {
  551. Node = NET_LIST_USER_STRUCT (Cur, NET_BUF, List);
  552. Seg = TCPSEG_NETBUF (Node);
  553. if (TCP_SEQ_GEQ (Seg->Seq, Ack)) {
  554. break;
  555. }
  556. //
  557. // Remove completely ACKed segments
  558. //
  559. if (TCP_SEQ_LEQ (Seg->End, Ack)) {
  560. Cur = Cur->ForwardLink;
  561. RemoveEntryList (&Node->List);
  562. NetbufFree (Node);
  563. continue;
  564. }
  565. return TcpTrimSegment (Node, Ack, Seg->End);
  566. }
  567. return 1;
  568. }
  569. /**
  570. Process the received TCP segments.
  571. @param[in] Nbuf Buffer that contains received a TCP segment without an IP header.
  572. @param[in] Src Source address of the segment, or the peer's IP address.
  573. @param[in] Dst Destination address of the segment, or the local end's IP
  574. address.
  575. @param[in] Version IP_VERSION_4 indicates IP4 stack. IP_VERSION_6 indicates
  576. IP6 stack.
  577. @retval 0 Segment processed successfully. It is either accepted or
  578. discarded. However, no connection is reset by the segment.
  579. @retval -1 A connection is reset by the segment.
  580. **/
  581. INTN
  582. TcpInput (
  583. IN NET_BUF *Nbuf,
  584. IN EFI_IP_ADDRESS *Src,
  585. IN EFI_IP_ADDRESS *Dst,
  586. IN UINT8 Version
  587. )
  588. {
  589. TCP_CB *Tcb;
  590. TCP_CB *Parent;
  591. TCP_OPTION Option;
  592. TCP_HEAD *Head;
  593. INT32 Len;
  594. TCP_SEG *Seg;
  595. TCP_SEQNO Right;
  596. TCP_SEQNO Urg;
  597. UINT16 Checksum;
  598. INT32 Usable;
  599. ASSERT ((Version == IP_VERSION_4) || (Version == IP_VERSION_6));
  600. NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE);
  601. Parent = NULL;
  602. Tcb = NULL;
  603. Head = (TCP_HEAD *)NetbufGetByte (Nbuf, 0, NULL);
  604. ASSERT (Head != NULL);
  605. if (Nbuf->TotalSize < sizeof (TCP_HEAD)) {
  606. DEBUG ((DEBUG_NET, "TcpInput: received a malformed packet\n"));
  607. goto DISCARD;
  608. }
  609. Len = Nbuf->TotalSize - (Head->HeadLen << 2);
  610. if ((Head->HeadLen < 5) || (Len < 0)) {
  611. DEBUG ((DEBUG_NET, "TcpInput: received a malformed packet\n"));
  612. goto DISCARD;
  613. }
  614. if (Version == IP_VERSION_4) {
  615. Checksum = NetPseudoHeadChecksum (Src->Addr[0], Dst->Addr[0], 6, 0);
  616. } else {
  617. Checksum = NetIp6PseudoHeadChecksum (&Src->v6, &Dst->v6, 6, 0);
  618. }
  619. Checksum = TcpChecksum (Nbuf, Checksum);
  620. if (Checksum != 0) {
  621. DEBUG ((DEBUG_ERROR, "TcpInput: received a checksum error packet\n"));
  622. goto DISCARD;
  623. }
  624. if (TCP_FLG_ON (Head->Flag, TCP_FLG_SYN)) {
  625. Len++;
  626. }
  627. if (TCP_FLG_ON (Head->Flag, TCP_FLG_FIN)) {
  628. Len++;
  629. }
  630. Tcb = TcpLocateTcb (
  631. Head->DstPort,
  632. Dst,
  633. Head->SrcPort,
  634. Src,
  635. Version,
  636. (BOOLEAN)TCP_FLG_ON (Head->Flag, TCP_FLG_SYN)
  637. );
  638. if ((Tcb == NULL) || (Tcb->State == TCP_CLOSED)) {
  639. DEBUG ((DEBUG_NET, "TcpInput: send reset because no TCB found\n"));
  640. Tcb = NULL;
  641. goto SEND_RESET;
  642. }
  643. Seg = TcpFormatNetbuf (Tcb, Nbuf);
  644. //
  645. // RFC1122 recommended reaction to illegal option
  646. // (in fact, an illegal option length) is reset.
  647. //
  648. if (TcpParseOption (Nbuf->Tcp, &Option) == -1) {
  649. DEBUG (
  650. (DEBUG_ERROR,
  651. "TcpInput: reset the peer because of malformed option for TCB %p\n",
  652. Tcb)
  653. );
  654. goto SEND_RESET;
  655. }
  656. //
  657. // From now on, the segment is headless
  658. //
  659. NetbufTrim (Nbuf, (Head->HeadLen << 2), NET_BUF_HEAD);
  660. Nbuf->Tcp = NULL;
  661. //
  662. // Process the segment in LISTEN state.
  663. //
  664. if (Tcb->State == TCP_LISTEN) {
  665. //
  666. // First step: Check RST
  667. //
  668. if (TCP_FLG_ON (Seg->Flag, TCP_FLG_RST)) {
  669. DEBUG (
  670. (DEBUG_WARN,
  671. "TcpInput: discard a reset segment for TCB %p in listening\n",
  672. Tcb)
  673. );
  674. goto DISCARD;
  675. }
  676. //
  677. // Second step: Check ACK.
  678. // Any ACK sent to TCP in LISTEN is reseted.
  679. //
  680. if (TCP_FLG_ON (Seg->Flag, TCP_FLG_ACK)) {
  681. DEBUG (
  682. (DEBUG_WARN,
  683. "TcpInput: send reset because of segment with ACK for TCB %p in listening\n",
  684. Tcb)
  685. );
  686. goto SEND_RESET;
  687. }
  688. //
  689. // Third step: Check SYN
  690. //
  691. if (TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN)) {
  692. //
  693. // create a child TCB to handle the data
  694. //
  695. Parent = Tcb;
  696. Tcb = TcpCloneTcb (Parent);
  697. if (Tcb == NULL) {
  698. DEBUG (
  699. (DEBUG_ERROR,
  700. "TcpInput: discard a segment because failed to clone a child for TCB %p\n",
  701. Tcb)
  702. );
  703. goto DISCARD;
  704. }
  705. DEBUG (
  706. (DEBUG_NET,
  707. "TcpInput: create a child for TCB %p in listening\n",
  708. Tcb)
  709. );
  710. //
  711. // init the TCB structure
  712. //
  713. IP6_COPY_ADDRESS (&Tcb->LocalEnd.Ip, Dst);
  714. IP6_COPY_ADDRESS (&Tcb->RemoteEnd.Ip, Src);
  715. Tcb->LocalEnd.Port = Head->DstPort;
  716. Tcb->RemoteEnd.Port = Head->SrcPort;
  717. TcpInitTcbLocal (Tcb);
  718. TcpInitTcbPeer (Tcb, Seg, &Option);
  719. TcpSetState (Tcb, TCP_SYN_RCVD);
  720. TcpSetTimer (Tcb, TCP_TIMER_CONNECT, Tcb->ConnectTimeout);
  721. if (TcpTrimInWnd (Tcb, Nbuf) == 0) {
  722. DEBUG (
  723. (DEBUG_ERROR,
  724. "TcpInput: discard a broken segment for TCB %p\n",
  725. Tcb)
  726. );
  727. goto DISCARD;
  728. }
  729. goto StepSix;
  730. }
  731. goto DISCARD;
  732. } else if (Tcb->State == TCP_SYN_SENT) {
  733. //
  734. // First step: Check ACK bit
  735. //
  736. if (TCP_FLG_ON (Seg->Flag, TCP_FLG_ACK) && (Seg->Ack != Tcb->Iss + 1)) {
  737. DEBUG (
  738. (DEBUG_WARN,
  739. "TcpInput: send reset because of wrong ACK received for TCB %p in SYN_SENT\n",
  740. Tcb)
  741. );
  742. goto SEND_RESET;
  743. }
  744. //
  745. // Second step: Check RST bit
  746. //
  747. if (TCP_FLG_ON (Seg->Flag, TCP_FLG_RST)) {
  748. if (TCP_FLG_ON (Seg->Flag, TCP_FLG_ACK)) {
  749. DEBUG (
  750. (DEBUG_WARN,
  751. "TcpInput: connection reset by peer for TCB %p in SYN_SENT\n",
  752. Tcb)
  753. );
  754. SOCK_ERROR (Tcb->Sk, EFI_CONNECTION_RESET);
  755. goto DROP_CONNECTION;
  756. } else {
  757. DEBUG (
  758. (DEBUG_WARN,
  759. "TcpInput: discard a reset segment because of no ACK for TCB %p in SYN_SENT\n",
  760. Tcb)
  761. );
  762. goto DISCARD;
  763. }
  764. }
  765. //
  766. // Third step: Check security and precedence. Skipped
  767. //
  768. //
  769. // Fourth step: Check SYN. Pay attention to simultaneous open
  770. //
  771. if (TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN)) {
  772. TcpInitTcbPeer (Tcb, Seg, &Option);
  773. if (TCP_FLG_ON (Seg->Flag, TCP_FLG_ACK)) {
  774. Tcb->SndUna = Seg->Ack;
  775. }
  776. TcpClearTimer (Tcb, TCP_TIMER_REXMIT);
  777. if (TCP_SEQ_GT (Tcb->SndUna, Tcb->Iss)) {
  778. TcpSetState (Tcb, TCP_ESTABLISHED);
  779. TcpClearTimer (Tcb, TCP_TIMER_CONNECT);
  780. TcpDeliverData (Tcb);
  781. if ((Tcb->CongestState == TCP_CONGEST_OPEN) &&
  782. TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_RTT_ON))
  783. {
  784. TcpComputeRtt (Tcb, Tcb->RttMeasure);
  785. TCP_CLEAR_FLG (Tcb->CtrlFlag, TCP_CTRL_RTT_ON);
  786. }
  787. if (TcpTrimInWnd (Tcb, Nbuf) == 0) {
  788. DEBUG (
  789. (DEBUG_ERROR,
  790. "TcpInput: discard a broken segment for TCB %p\n",
  791. Tcb)
  792. );
  793. goto DISCARD;
  794. }
  795. TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW);
  796. DEBUG (
  797. (DEBUG_NET,
  798. "TcpInput: connection established for TCB %p in SYN_SENT\n",
  799. Tcb)
  800. );
  801. goto StepSix;
  802. } else {
  803. //
  804. // Received a SYN segment without ACK, simultaneous open.
  805. //
  806. TcpSetState (Tcb, TCP_SYN_RCVD);
  807. ASSERT (Tcb->SndNxt == Tcb->Iss + 1);
  808. if ((TcpAdjustSndQue (Tcb, Tcb->SndNxt) == 0) || (TcpTrimInWnd (Tcb, Nbuf) == 0)) {
  809. DEBUG (
  810. (DEBUG_ERROR,
  811. "TcpInput: discard a broken segment for TCB %p\n",
  812. Tcb)
  813. );
  814. goto DISCARD;
  815. }
  816. DEBUG (
  817. (DEBUG_WARN,
  818. "TcpInput: simultaneous open for TCB %p in SYN_SENT\n",
  819. Tcb)
  820. );
  821. goto StepSix;
  822. }
  823. }
  824. goto DISCARD;
  825. }
  826. //
  827. // Process segment in SYN_RCVD or TCP_CONNECTED states
  828. //
  829. //
  830. // Clear probe timer since the RecvWindow is opened.
  831. //
  832. if (Tcb->ProbeTimerOn && (Seg->Wnd != 0)) {
  833. TcpClearTimer (Tcb, TCP_TIMER_PROBE);
  834. Tcb->ProbeTimerOn = FALSE;
  835. }
  836. //
  837. // First step: Check whether SEG.SEQ is acceptable
  838. //
  839. if (TcpSeqAcceptable (Tcb, Seg) == 0) {
  840. DEBUG (
  841. (DEBUG_WARN,
  842. "TcpInput: sequence acceptance test failed for segment of TCB %p\n",
  843. Tcb)
  844. );
  845. if (!TCP_FLG_ON (Seg->Flag, TCP_FLG_RST)) {
  846. TcpSendAck (Tcb);
  847. }
  848. goto DISCARD;
  849. }
  850. if ((TCP_SEQ_LT (Seg->Seq, Tcb->RcvWl2)) &&
  851. (Tcb->RcvWl2 == Seg->End) &&
  852. !TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN | TCP_FLG_FIN))
  853. {
  854. TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW);
  855. }
  856. //
  857. // Second step: Check the RST
  858. //
  859. if (TCP_FLG_ON (Seg->Flag, TCP_FLG_RST)) {
  860. DEBUG ((DEBUG_WARN, "TcpInput: connection reset for TCB %p\n", Tcb));
  861. if (Tcb->State == TCP_SYN_RCVD) {
  862. SOCK_ERROR (Tcb->Sk, EFI_CONNECTION_REFUSED);
  863. //
  864. // This TCB comes from either a LISTEN TCB,
  865. // or active open TCB with simultaneous open.
  866. // Do NOT signal user CONNECTION refused
  867. // if it comes from a LISTEN TCB.
  868. //
  869. } else if ((Tcb->State == TCP_ESTABLISHED) ||
  870. (Tcb->State == TCP_FIN_WAIT_1) ||
  871. (Tcb->State == TCP_FIN_WAIT_2) ||
  872. (Tcb->State == TCP_CLOSE_WAIT))
  873. {
  874. SOCK_ERROR (Tcb->Sk, EFI_CONNECTION_RESET);
  875. } else {
  876. }
  877. goto DROP_CONNECTION;
  878. }
  879. //
  880. // Trim the data and flags.
  881. //
  882. if (TcpTrimInWnd (Tcb, Nbuf) == 0) {
  883. DEBUG (
  884. (DEBUG_ERROR,
  885. "TcpInput: discard a broken segment for TCB %p\n",
  886. Tcb)
  887. );
  888. goto DISCARD;
  889. }
  890. //
  891. // Third step: Check security and precedence, Ignored
  892. //
  893. //
  894. // Fourth step: Check the SYN bit.
  895. //
  896. if (TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN)) {
  897. DEBUG (
  898. (DEBUG_WARN,
  899. "TcpInput: connection reset because received extra SYN for TCB %p\n",
  900. Tcb)
  901. );
  902. SOCK_ERROR (Tcb->Sk, EFI_CONNECTION_RESET);
  903. goto RESET_THEN_DROP;
  904. }
  905. //
  906. // Fifth step: Check the ACK
  907. //
  908. if (!TCP_FLG_ON (Seg->Flag, TCP_FLG_ACK)) {
  909. DEBUG (
  910. (DEBUG_WARN,
  911. "TcpInput: segment discard because of no ACK for connected TCB %p\n",
  912. Tcb)
  913. );
  914. goto DISCARD;
  915. } else {
  916. if ((Tcb->IpInfo->IpVersion == IP_VERSION_6) && (Tcb->Tick == 0)) {
  917. Tcp6RefreshNeighbor (Tcb, Src, TCP6_KEEP_NEIGHBOR_TIME * TICKS_PER_SECOND);
  918. Tcb->Tick = TCP6_REFRESH_NEIGHBOR_TICK;
  919. }
  920. }
  921. if (Tcb->State == TCP_SYN_RCVD) {
  922. if (TCP_SEQ_LT (Tcb->SndUna, Seg->Ack) &&
  923. TCP_SEQ_LEQ (Seg->Ack, Tcb->SndNxt))
  924. {
  925. Tcb->SndWnd = Seg->Wnd;
  926. Tcb->SndWndMax = MAX (Tcb->SndWnd, Tcb->SndWndMax);
  927. Tcb->SndWl1 = Seg->Seq;
  928. Tcb->SndWl2 = Seg->Ack;
  929. TcpSetState (Tcb, TCP_ESTABLISHED);
  930. TcpClearTimer (Tcb, TCP_TIMER_CONNECT);
  931. TcpDeliverData (Tcb);
  932. DEBUG (
  933. (DEBUG_NET,
  934. "TcpInput: connection established for TCB %p in SYN_RCVD\n",
  935. Tcb)
  936. );
  937. //
  938. // Continue the process as ESTABLISHED state
  939. //
  940. } else {
  941. DEBUG (
  942. (DEBUG_WARN,
  943. "TcpInput: send reset because of wrong ACK for TCB %p in SYN_RCVD\n",
  944. Tcb)
  945. );
  946. goto SEND_RESET;
  947. }
  948. }
  949. if (TCP_SEQ_LT (Seg->Ack, Tcb->SndUna)) {
  950. DEBUG (
  951. (DEBUG_WARN,
  952. "TcpInput: ignore the out-of-data ACK for connected TCB %p\n",
  953. Tcb)
  954. );
  955. goto StepSix;
  956. } else if (TCP_SEQ_GT (Seg->Ack, Tcb->SndNxt)) {
  957. DEBUG (
  958. (DEBUG_WARN,
  959. "TcpInput: discard segment for future ACK for connected TCB %p\n",
  960. Tcb)
  961. );
  962. TcpSendAck (Tcb);
  963. goto DISCARD;
  964. }
  965. //
  966. // From now on: SND.UNA <= SEG.ACK <= SND.NXT.
  967. //
  968. if (TCP_FLG_ON (Option.Flag, TCP_OPTION_RCVD_TS)) {
  969. //
  970. // update TsRecent as specified in page 16 RFC1323.
  971. // RcvWl2 equals to the variable "LastAckSent"
  972. // defined there.
  973. //
  974. if (TCP_SEQ_LEQ (Seg->Seq, Tcb->RcvWl2) &&
  975. TCP_SEQ_LT (Tcb->RcvWl2, Seg->End))
  976. {
  977. Tcb->TsRecent = Option.TSVal;
  978. Tcb->TsRecentAge = mTcpTick;
  979. }
  980. TcpComputeRtt (Tcb, TCP_SUB_TIME (mTcpTick, Option.TSEcr));
  981. } else if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_RTT_ON)) {
  982. ASSERT (Tcb->CongestState == TCP_CONGEST_OPEN);
  983. TcpComputeRtt (Tcb, Tcb->RttMeasure);
  984. TCP_CLEAR_FLG (Tcb->CtrlFlag, TCP_CTRL_RTT_ON);
  985. }
  986. if (Seg->Ack == Tcb->SndNxt) {
  987. TcpClearTimer (Tcb, TCP_TIMER_REXMIT);
  988. } else {
  989. TcpSetTimer (Tcb, TCP_TIMER_REXMIT, Tcb->Rto);
  990. }
  991. //
  992. // Count duplicate acks.
  993. //
  994. if ((Seg->Ack == Tcb->SndUna) &&
  995. (Tcb->SndUna != Tcb->SndNxt) &&
  996. (Seg->Wnd == Tcb->SndWnd) &&
  997. (0 == Len))
  998. {
  999. Tcb->DupAck++;
  1000. } else {
  1001. Tcb->DupAck = 0;
  1002. }
  1003. //
  1004. // Congestion avoidance, fast recovery and fast retransmission.
  1005. //
  1006. if (((Tcb->CongestState == TCP_CONGEST_OPEN) && (Tcb->DupAck < 3)) ||
  1007. (Tcb->CongestState == TCP_CONGEST_LOSS))
  1008. {
  1009. if (TCP_SEQ_GT (Seg->Ack, Tcb->SndUna)) {
  1010. if (Tcb->CWnd < Tcb->Ssthresh) {
  1011. Tcb->CWnd += Tcb->SndMss;
  1012. } else {
  1013. Tcb->CWnd += MAX (Tcb->SndMss * Tcb->SndMss / Tcb->CWnd, 1);
  1014. }
  1015. Tcb->CWnd = MIN (Tcb->CWnd, TCP_MAX_WIN << Tcb->SndWndScale);
  1016. }
  1017. if (Tcb->CongestState == TCP_CONGEST_LOSS) {
  1018. TcpFastLossRecover (Tcb, Seg);
  1019. }
  1020. } else {
  1021. TcpFastRecover (Tcb, Seg);
  1022. }
  1023. if (TCP_SEQ_GT (Seg->Ack, Tcb->SndUna)) {
  1024. if (TcpAdjustSndQue (Tcb, Seg->Ack) == 0) {
  1025. DEBUG (
  1026. (DEBUG_ERROR,
  1027. "TcpInput: discard a broken segment for TCB %p\n",
  1028. Tcb)
  1029. );
  1030. goto DISCARD;
  1031. }
  1032. Tcb->SndUna = Seg->Ack;
  1033. if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_SND_URG) &&
  1034. TCP_SEQ_LT (Tcb->SndUp, Seg->Ack))
  1035. {
  1036. TCP_CLEAR_FLG (Tcb->CtrlFlag, TCP_CTRL_SND_URG);
  1037. }
  1038. }
  1039. //
  1040. // Update window info
  1041. //
  1042. if (TCP_SEQ_LT (Tcb->SndWl1, Seg->Seq) ||
  1043. ((Tcb->SndWl1 == Seg->Seq) && TCP_SEQ_LEQ (Tcb->SndWl2, Seg->Ack)))
  1044. {
  1045. Right = Seg->Ack + Seg->Wnd;
  1046. if (TCP_SEQ_LT (Right, Tcb->SndWl2 + Tcb->SndWnd)) {
  1047. if ((Tcb->SndWl1 == Seg->Seq) &&
  1048. (Tcb->SndWl2 == Seg->Ack) &&
  1049. (Len == 0))
  1050. {
  1051. goto NO_UPDATE;
  1052. }
  1053. DEBUG (
  1054. (DEBUG_WARN,
  1055. "TcpInput: peer shrinks the window for connected TCB %p\n",
  1056. Tcb)
  1057. );
  1058. if ((Tcb->CongestState == TCP_CONGEST_RECOVER) &&
  1059. (TCP_SEQ_LT (Right, Tcb->Recover)))
  1060. {
  1061. Tcb->Recover = Right;
  1062. }
  1063. if ((Tcb->CongestState == TCP_CONGEST_LOSS) &&
  1064. (TCP_SEQ_LT (Right, Tcb->LossRecover)))
  1065. {
  1066. Tcb->LossRecover = Right;
  1067. }
  1068. if (TCP_SEQ_LT (Right, Tcb->SndNxt)) {
  1069. //
  1070. // Check for Window Retraction in RFC7923 section 2.4.
  1071. // The lower n bits of the peer's actual receive window is wiped out if TCP
  1072. // window scale is enabled, it will look like the peer is shrinking the window.
  1073. // Check whether the SndNxt is out of the advertised receive window by more than
  1074. // 2^Rcv.Wind.Shift before moving the SndNxt to the left.
  1075. //
  1076. DEBUG (
  1077. (DEBUG_WARN,
  1078. "TcpInput: peer advise negative useable window for connected TCB %p\n",
  1079. Tcb)
  1080. );
  1081. Usable = TCP_SUB_SEQ (Tcb->SndNxt, Right);
  1082. if ((Usable >> Tcb->SndWndScale) > 0) {
  1083. DEBUG (
  1084. (DEBUG_WARN,
  1085. "TcpInput: SndNxt is out of window by more than window scale for TCB %p\n",
  1086. Tcb)
  1087. );
  1088. Tcb->SndNxt = Right;
  1089. }
  1090. if (Right == Tcb->SndUna) {
  1091. TcpClearTimer (Tcb, TCP_TIMER_REXMIT);
  1092. TcpSetProbeTimer (Tcb);
  1093. }
  1094. }
  1095. }
  1096. Tcb->SndWnd = Seg->Wnd;
  1097. Tcb->SndWndMax = MAX (Tcb->SndWnd, Tcb->SndWndMax);
  1098. Tcb->SndWl1 = Seg->Seq;
  1099. Tcb->SndWl2 = Seg->Ack;
  1100. }
  1101. NO_UPDATE:
  1102. if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_FIN_SENT) &&
  1103. (Tcb->SndUna == Tcb->SndNxt))
  1104. {
  1105. DEBUG (
  1106. (DEBUG_NET,
  1107. "TcpInput: local FIN is ACKed by peer for connected TCB %p\n",
  1108. Tcb)
  1109. );
  1110. TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_FIN_ACKED);
  1111. }
  1112. //
  1113. // Transit the state if proper.
  1114. //
  1115. switch (Tcb->State) {
  1116. case TCP_FIN_WAIT_1:
  1117. if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_FIN_ACKED)) {
  1118. TcpSetState (Tcb, TCP_FIN_WAIT_2);
  1119. TcpClearAllTimer (Tcb);
  1120. TcpSetTimer (Tcb, TCP_TIMER_FINWAIT2, Tcb->FinWait2Timeout);
  1121. }
  1122. case TCP_FIN_WAIT_2:
  1123. break;
  1124. case TCP_CLOSE_WAIT:
  1125. break;
  1126. case TCP_CLOSING:
  1127. if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_FIN_ACKED)) {
  1128. TcpSetState (Tcb, TCP_TIME_WAIT);
  1129. TcpClearAllTimer (Tcb);
  1130. if (Tcb->TimeWaitTimeout != 0) {
  1131. TcpSetTimer (Tcb, TCP_TIMER_2MSL, Tcb->TimeWaitTimeout);
  1132. } else {
  1133. DEBUG (
  1134. (DEBUG_WARN,
  1135. "Connection closed immediately because app disables TIME_WAIT timer for %p\n",
  1136. Tcb)
  1137. );
  1138. TcpClose (Tcb);
  1139. }
  1140. }
  1141. break;
  1142. case TCP_LAST_ACK:
  1143. if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_FIN_ACKED)) {
  1144. TcpSetState (Tcb, TCP_CLOSED);
  1145. }
  1146. break;
  1147. case TCP_TIME_WAIT:
  1148. TcpSendAck (Tcb);
  1149. if (Tcb->TimeWaitTimeout != 0) {
  1150. TcpSetTimer (Tcb, TCP_TIMER_2MSL, Tcb->TimeWaitTimeout);
  1151. } else {
  1152. DEBUG (
  1153. (DEBUG_WARN,
  1154. "Connection closed immediately because app disables TIME_WAIT timer for %p\n",
  1155. Tcb)
  1156. );
  1157. TcpClose (Tcb);
  1158. }
  1159. break;
  1160. default:
  1161. break;
  1162. }
  1163. //
  1164. // Sixth step: Check the URG bit.update the Urg point
  1165. // if in TCP_CAN_RECV, otherwise, leave the RcvUp intact.
  1166. //
  1167. StepSix:
  1168. Tcb->Idle = 0;
  1169. TcpSetKeepaliveTimer (Tcb);
  1170. if (TCP_FLG_ON (Seg->Flag, TCP_FLG_URG) && !TCP_FIN_RCVD (Tcb->State)) {
  1171. DEBUG (
  1172. (DEBUG_NET,
  1173. "TcpInput: received urgent data from peer for connected TCB %p\n",
  1174. Tcb)
  1175. );
  1176. Urg = Seg->Seq + Seg->Urg;
  1177. if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_RCVD_URG) &&
  1178. TCP_SEQ_GT (Urg, Tcb->RcvUp))
  1179. {
  1180. Tcb->RcvUp = Urg;
  1181. } else {
  1182. Tcb->RcvUp = Urg;
  1183. TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_RCVD_URG);
  1184. }
  1185. }
  1186. //
  1187. // Seventh step: Process the segment data
  1188. //
  1189. if (Seg->End != Seg->Seq) {
  1190. if (TCP_FIN_RCVD (Tcb->State)) {
  1191. DEBUG (
  1192. (DEBUG_WARN,
  1193. "TcpInput: connection reset because data is lost for connected TCB %p\n",
  1194. Tcb)
  1195. );
  1196. goto RESET_THEN_DROP;
  1197. }
  1198. if (TCP_LOCAL_CLOSED (Tcb->State) && (Nbuf->TotalSize != 0)) {
  1199. DEBUG (
  1200. (DEBUG_WARN,
  1201. "TcpInput: connection reset because data is lost for connected TCB %p\n",
  1202. Tcb)
  1203. );
  1204. goto RESET_THEN_DROP;
  1205. }
  1206. if (TcpQueueData (Tcb, Nbuf) == 0) {
  1207. DEBUG (
  1208. (DEBUG_ERROR,
  1209. "TcpInput: discard a broken segment for TCB %p\n",
  1210. Tcb)
  1211. );
  1212. goto DISCARD;
  1213. }
  1214. if (TcpDeliverData (Tcb) == -1) {
  1215. goto RESET_THEN_DROP;
  1216. }
  1217. if (!IsListEmpty (&Tcb->RcvQue)) {
  1218. TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW);
  1219. }
  1220. }
  1221. //
  1222. // Eighth step: check the FIN.
  1223. // This step is moved to TcpDeliverData. FIN will be
  1224. // processed in sequence there. Check the comments in
  1225. // the beginning of the file header for information.
  1226. //
  1227. //
  1228. // Tcb is a new child of the listening Parent,
  1229. // commit it.
  1230. //
  1231. if (Parent != NULL) {
  1232. Tcb->Parent = Parent;
  1233. TcpInsertTcb (Tcb);
  1234. }
  1235. if ((Tcb->State != TCP_CLOSED) &&
  1236. (TcpToSendData (Tcb, 0) == 0) &&
  1237. (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW) || (Nbuf->TotalSize != 0)))
  1238. {
  1239. TcpToSendAck (Tcb);
  1240. }
  1241. NetbufFree (Nbuf);
  1242. return 0;
  1243. RESET_THEN_DROP:
  1244. TcpSendReset (Tcb, Head, Len, Dst, Src, Version);
  1245. DROP_CONNECTION:
  1246. ASSERT ((Tcb != NULL) && (Tcb->Sk != NULL));
  1247. NetbufFree (Nbuf);
  1248. TcpClose (Tcb);
  1249. return -1;
  1250. SEND_RESET:
  1251. TcpSendReset (Tcb, Head, Len, Dst, Src, Version);
  1252. DISCARD:
  1253. //
  1254. // Tcb is a child of Parent, and it doesn't survive
  1255. //
  1256. DEBUG ((DEBUG_WARN, "TcpInput: Discard a packet\n"));
  1257. NetbufFree (Nbuf);
  1258. if ((Parent != NULL) && (Tcb != NULL)) {
  1259. ASSERT (Tcb->Sk != NULL);
  1260. TcpClose (Tcb);
  1261. }
  1262. return 0;
  1263. }
  1264. /**
  1265. Process the received ICMP error messages for TCP.
  1266. @param[in] Nbuf The buffer that contains part of the TCP segment without an IP header
  1267. truncated from the ICMP error packet.
  1268. @param[in] IcmpErr The ICMP error code interpreted from an ICMP error packet.
  1269. @param[in] Src Source address of the ICMP error message.
  1270. @param[in] Dst Destination address of the ICMP error message.
  1271. @param[in] Version IP_VERSION_4 indicates IP4 stack. IP_VERSION_6 indicates
  1272. IP6 stack.
  1273. **/
  1274. VOID
  1275. TcpIcmpInput (
  1276. IN NET_BUF *Nbuf,
  1277. IN UINT8 IcmpErr,
  1278. IN EFI_IP_ADDRESS *Src,
  1279. IN EFI_IP_ADDRESS *Dst,
  1280. IN UINT8 Version
  1281. )
  1282. {
  1283. TCP_HEAD *Head;
  1284. TCP_CB *Tcb;
  1285. TCP_SEQNO Seq;
  1286. EFI_STATUS IcmpErrStatus;
  1287. BOOLEAN IcmpErrIsHard;
  1288. BOOLEAN IcmpErrNotify;
  1289. if (Nbuf->TotalSize < sizeof (TCP_HEAD)) {
  1290. goto CLEAN_EXIT;
  1291. }
  1292. Head = (TCP_HEAD *)NetbufGetByte (Nbuf, 0, NULL);
  1293. ASSERT (Head != NULL);
  1294. Tcb = TcpLocateTcb (
  1295. Head->DstPort,
  1296. Dst,
  1297. Head->SrcPort,
  1298. Src,
  1299. Version,
  1300. FALSE
  1301. );
  1302. if ((Tcb == NULL) || (Tcb->State == TCP_CLOSED)) {
  1303. goto CLEAN_EXIT;
  1304. }
  1305. //
  1306. // Validate the sequence number.
  1307. //
  1308. Seq = NTOHL (Head->Seq);
  1309. if (!(TCP_SEQ_LEQ (Tcb->SndUna, Seq) && TCP_SEQ_LT (Seq, Tcb->SndNxt))) {
  1310. goto CLEAN_EXIT;
  1311. }
  1312. IcmpErrStatus = IpIoGetIcmpErrStatus (
  1313. IcmpErr,
  1314. Tcb->Sk->IpVersion,
  1315. &IcmpErrIsHard,
  1316. &IcmpErrNotify
  1317. );
  1318. if (IcmpErrNotify) {
  1319. SOCK_ERROR (Tcb->Sk, IcmpErrStatus);
  1320. }
  1321. if (IcmpErrIsHard) {
  1322. TcpClose (Tcb);
  1323. }
  1324. CLEAN_EXIT:
  1325. NetbufFree (Nbuf);
  1326. }